Put all LLVM code into the llvm namespace, as per bug 109.
[oota-llvm.git] / lib / VMCore / iMemory.cpp
1 //===-- iMemory.cpp - Implement Memory instructions -----------------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the various memory related classes defined in iMemory.h
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/iMemory.h"
15 #include "llvm/Constants.h"
16 #include "llvm/DerivedTypes.h"
17
18 using namespace llvm;
19
20 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, 
21                                const std::string &Name, Instruction *InsertBef)
22   : Instruction(PointerType::get(Ty), iTy, Name, InsertBef) {
23
24   // ArraySize defaults to 1.
25   if (!ArraySize) ArraySize = ConstantUInt::get(Type::UIntTy, 1);
26
27   Operands.reserve(1);
28   assert(ArraySize->getType() == Type::UIntTy &&
29          "Malloc/Allocation array size != UIntTy!");
30
31   Operands.push_back(Use(ArraySize, this));
32 }
33
34 bool AllocationInst::isArrayAllocation() const {
35   return getOperand(0) != ConstantUInt::get(Type::UIntTy, 1);
36 }
37
38 const Type *AllocationInst::getAllocatedType() const {
39   return getType()->getElementType();
40 }
41
42 AllocaInst::AllocaInst(const AllocaInst &AI)
43   : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
44                    Instruction::Alloca) {
45 }
46
47 MallocInst::MallocInst(const MallocInst &MI)
48   : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
49                    Instruction::Malloc) {
50 }
51
52 //===----------------------------------------------------------------------===//
53 //                             FreeInst Implementation
54 //===----------------------------------------------------------------------===//
55
56 FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
57   : Instruction(Type::VoidTy, Free, "", InsertBefore) {
58   assert(isa<PointerType>(Ptr->getType()) && "Can't free nonpointer!");
59   Operands.reserve(1);
60   Operands.push_back(Use(Ptr, this));
61 }
62
63
64 //===----------------------------------------------------------------------===//
65 //                           LoadInst Implementation
66 //===----------------------------------------------------------------------===//
67
68 LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
69   : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
70                 Load, Name, InsertBef), Volatile(false) {
71   Operands.reserve(1);
72   Operands.push_back(Use(Ptr, this));
73 }
74
75 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
76                    Instruction *InsertBef)
77   : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
78                 Load, Name, InsertBef), Volatile(isVolatile) {
79   Operands.reserve(1);
80   Operands.push_back(Use(Ptr, this));
81 }
82
83 //===----------------------------------------------------------------------===//
84 //                           StoreInst Implementation
85 //===----------------------------------------------------------------------===//
86
87 StoreInst::StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore)
88   : Instruction(Type::VoidTy, Store, "", InsertBefore), Volatile(false) {
89   
90   Operands.reserve(2);
91   Operands.push_back(Use(Val, this));
92   Operands.push_back(Use(Ptr, this));
93 }
94
95 StoreInst::StoreInst(Value *Val, Value *Ptr, bool isVolatile, 
96                      Instruction *InsertBefore)
97   : Instruction(Type::VoidTy, Store, "", InsertBefore), Volatile(isVolatile) {
98   
99   Operands.reserve(2);
100   Operands.push_back(Use(Val, this));
101   Operands.push_back(Use(Ptr, this));
102 }
103
104
105 //===----------------------------------------------------------------------===//
106 //                       GetElementPtrInst Implementation
107 //===----------------------------------------------------------------------===//
108
109 // checkType - Simple wrapper function to give a better assertion failure
110 // message on bad indexes for a gep instruction.
111 //
112 static inline const Type *checkType(const Type *Ty) {
113   assert(Ty && "Invalid indices for type!");
114   return Ty;
115 }
116
117 GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
118                                      const std::string &Name, Instruction *InBe)
119   : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
120                                                             Idx, true))),
121                   GetElementPtr, Name, InBe) {
122   Operands.reserve(1+Idx.size());
123   Operands.push_back(Use(Ptr, this));
124
125   for (unsigned i = 0, E = Idx.size(); i != E; ++i)
126     Operands.push_back(Use(Idx[i], this));
127 }
128
129 // getIndexedType - Returns the type of the element that would be loaded with
130 // a load instruction with the specified parameters.
131 //
132 // A null type is returned if the indices are invalid for the specified 
133 // pointer type.
134 //
135 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, 
136                                               const std::vector<Value*> &Idx,
137                                               bool AllowCompositeLeaf) {
138   if (!isa<PointerType>(Ptr)) return 0;   // Type isn't a pointer type!
139
140   // Handle the special case of the empty set index set...
141   if (Idx.empty()) return cast<PointerType>(Ptr)->getElementType();
142  
143   unsigned CurIdx = 0;
144   while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
145     if (Idx.size() == CurIdx) {
146       if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
147       return 0;   // Can't load a whole structure or array!?!?
148     }
149
150     Value *Index = Idx[CurIdx++];
151     if (isa<PointerType>(CT) && CurIdx != 1)
152       return 0;  // Can only index into pointer types at the first index!
153     if (!CT->indexValid(Index)) return 0;
154     Ptr = CT->getTypeAtIndex(Index);
155   }
156   return CurIdx == Idx.size() ? Ptr : 0;
157 }