Renamed inst_const_iterator -> const_inst_iterator
[oota-llvm.git] / include / llvm / iMemory.h
1 //===-- llvm/iMemory.h - Memory Operator node definitions --------*- C++ -*--=//
2 //
3 // This file contains the declarations of all of the memory related operators.
4 // This includes: malloc, free, alloca, load, store, getfield, putfield
5 //
6 //===----------------------------------------------------------------------===//
7
8 #ifndef LLVM_IMEMORY_H
9 #define LLVM_IMEMORY_H
10
11 #include "llvm/Instruction.h"
12 #include "llvm/DerivedTypes.h"
13
14 //===----------------------------------------------------------------------===//
15 //                             AllocationInst Class
16 //===----------------------------------------------------------------------===//
17 //
18 // AllocationInst - This class is the common base class of MallocInst and
19 // AllocaInst.
20 //
21 class AllocationInst : public Instruction {
22 public:
23   AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, 
24                  const string &Name = "")
25     : Instruction(Ty, iTy, Name) {
26     assert(Ty->isPointerType() && "Can't allocate a non pointer type!");
27
28     if (ArraySize) {
29       // Make sure they didn't try to specify a size for !(unsized array) type
30       assert(getType()->getElementType()->isArrayType() && 
31              cast<ArrayType>(getType()->getElementType())->isUnsized() && 
32            "Trying to allocate something other than unsized array, with size!");
33       assert(ArraySize->getType() == Type::UIntTy &&
34              "Malloc/Allocation array size != UIntTy!");
35
36       Operands.reserve(1);
37       Operands.push_back(Use(ArraySize, this));
38     } else {
39       // Make sure that the pointer is not to an unsized array!
40       assert(!getType()->getElementType()->isArrayType() ||
41              cast<const ArrayType>(getType()->getElementType())->isSized() && 
42              "Trying to allocate unsized array without size!");
43     }
44   }
45
46   // isArrayAllocation - Return true if there is an allocation size parameter
47   // to the allocation instruction.
48   //
49   inline bool isArrayAllocation() const { return Operands.size() == 1; }
50
51   inline const Value *getArraySize() const {
52     assert(isArrayAllocation()); return Operands[0];
53   }
54   inline Value *getArraySize() {
55     assert(isArrayAllocation()); return Operands[0];
56   }
57
58   // getType - Overload to return most specific pointer type...
59   inline const PointerType *getType() const {
60     return (const PointerType*)Instruction::getType(); 
61   }
62
63   // getAllocatedType - Return the type that is being allocated by the
64   // instruction.
65   inline const Type *getAllocatedType() const {
66     return getType()->getElementType();
67   }
68
69   virtual Instruction *clone() const = 0;
70 };
71
72
73 //===----------------------------------------------------------------------===//
74 //                                MallocInst Class
75 //===----------------------------------------------------------------------===//
76
77 class MallocInst : public AllocationInst {
78 public:
79   MallocInst(const Type *Ty, Value *ArraySize = 0, const string &Name = "") 
80     : AllocationInst(Ty, ArraySize, Malloc, Name) {}
81
82   virtual Instruction *clone() const { 
83     return new MallocInst(getType(), 
84                           Operands.size() ? (Value*)Operands[0].get() : 0);
85   }
86
87   virtual const char *getOpcodeName() const { return "malloc"; }
88
89   // Methods for support type inquiry through isa, cast, and dyn_cast:
90   static inline bool classof(const MallocInst *) { return true; }
91   static inline bool classof(const Instruction *I) {
92     return (I->getOpcode() == Instruction::Malloc);
93   }
94   static inline bool classof(const Value *V) {
95     return isa<Instruction>(V) && classof(cast<Instruction>(V));
96   }
97 };
98
99
100 //===----------------------------------------------------------------------===//
101 //                                AllocaInst Class
102 //===----------------------------------------------------------------------===//
103
104 class AllocaInst : public AllocationInst {
105 public:
106   AllocaInst(const Type *Ty, Value *ArraySize = 0, const string &Name = "") 
107     : AllocationInst(Ty, ArraySize, Alloca, Name) {}
108
109   virtual Instruction *clone() const { 
110     return new AllocaInst(getType(),
111                           Operands.size() ? (Value*)Operands[0].get() : 0);
112   }
113
114   virtual const char *getOpcodeName() const { return "alloca"; }
115
116   // Methods for support type inquiry through isa, cast, and dyn_cast:
117   static inline bool classof(const AllocaInst *) { return true; }
118   static inline bool classof(const Instruction *I) {
119     return (I->getOpcode() == Instruction::Alloca);
120   }
121   static inline bool classof(const Value *V) {
122     return isa<Instruction>(V) && classof(cast<Instruction>(V));
123   }
124 };
125
126
127 //===----------------------------------------------------------------------===//
128 //                                 FreeInst Class
129 //===----------------------------------------------------------------------===//
130
131 class FreeInst : public Instruction {
132 public:
133   FreeInst(Value *Ptr, const string &Name = "") 
134     : Instruction(Type::VoidTy, Free, Name) {
135     assert(Ptr->getType()->isPointerType() && "Can't free nonpointer!");
136     Operands.reserve(1);
137     Operands.push_back(Use(Ptr, this));
138   }
139
140   virtual Instruction *clone() const { return new FreeInst(Operands[0]); }
141
142   virtual const char *getOpcodeName() const { return "free"; }
143
144   virtual bool hasSideEffects() const { return true; }
145
146   // Methods for support type inquiry through isa, cast, and dyn_cast:
147   static inline bool classof(const FreeInst *) { return true; }
148   static inline bool classof(const Instruction *I) {
149     return (I->getOpcode() == Instruction::Free);
150   }
151   static inline bool classof(const Value *V) {
152     return isa<Instruction>(V) && classof(cast<Instruction>(V));
153   }
154 };
155
156
157 //===----------------------------------------------------------------------===//
158 //                              MemAccessInst Class
159 //===----------------------------------------------------------------------===//
160 //
161 // MemAccessInst - Common base class of LoadInst, StoreInst, and
162 // GetElementPtrInst...
163 //
164 class MemAccessInst : public Instruction {
165 protected:
166   inline MemAccessInst(const Type *Ty, unsigned Opcode,
167                        const string &Nam = "")
168     : Instruction(Ty, Opcode, Nam) {}
169 public:
170   // getIndexedType - Returns the type of the element that would be loaded with
171   // a load instruction with the specified parameters.
172   //
173   // A null type is returned if the indices are invalid for the specified 
174   // pointer type.
175   //
176   static const Type *getIndexedType(const Type *Ptr, 
177                                     const vector<Value*> &Indices,
178                                     bool AllowStructLeaf = false);
179
180   const vector<Constant*> getIndicesBROKEN() const;
181   
182
183   inline op_iterator       idx_begin()       {
184     return op_begin()+getFirstIndexOperandNumber();
185   }
186   inline const_op_iterator idx_begin() const {
187     return op_begin()+getFirstIndexOperandNumber();
188   }
189   inline op_iterator       idx_end()         { return op_end(); }
190   inline const_op_iterator idx_end()   const { return op_end(); }
191
192
193   vector<Value*> copyIndices() const {
194     return vector<Value*>(idx_begin(), idx_end());
195   }
196
197   Value *getPointerOperand() {
198     return getOperand(getFirstIndexOperandNumber()-1);
199   }
200   const Value *getPointerOperand() const {
201     return getOperand(getFirstIndexOperandNumber()-1);
202   }
203   
204   virtual unsigned getFirstIndexOperandNumber() const = 0;
205
206   inline bool hasIndices() const {
207     return getNumOperands() > getFirstIndexOperandNumber();
208   }
209 };
210
211
212 //===----------------------------------------------------------------------===//
213 //                                LoadInst Class
214 //===----------------------------------------------------------------------===//
215
216 class LoadInst : public MemAccessInst {
217   LoadInst(const LoadInst &LI) : MemAccessInst(LI.getType(), Load) {
218     Operands.reserve(LI.Operands.size());
219     for (unsigned i = 0, E = LI.Operands.size(); i != E; ++i)
220       Operands.push_back(Use(LI.Operands[i], this));
221   }
222 public:
223   LoadInst(Value *Ptr, const vector<Value*> &Idx, const string &Name = "");
224   LoadInst(Value *Ptr, const string &Name = "");
225
226   virtual Instruction *clone() const { return new LoadInst(*this); }
227   virtual const char *getOpcodeName() const { return "load"; }  
228
229   virtual unsigned getFirstIndexOperandNumber() const { return 1; }
230
231   // Methods for support type inquiry through isa, cast, and dyn_cast:
232   static inline bool classof(const LoadInst *) { return true; }
233   static inline bool classof(const Instruction *I) {
234     return (I->getOpcode() == Instruction::Load);
235   }
236   static inline bool classof(const Value *V) {
237     return isa<Instruction>(V) && classof(cast<Instruction>(V));
238   }
239 };
240
241
242 //===----------------------------------------------------------------------===//
243 //                                StoreInst Class
244 //===----------------------------------------------------------------------===//
245
246 class StoreInst : public MemAccessInst {
247   StoreInst(const StoreInst &SI) : MemAccessInst(SI.getType(), Store) {
248     Operands.reserve(SI.Operands.size());
249     for (unsigned i = 0, E = SI.Operands.size(); i != E; ++i)
250       Operands.push_back(Use(SI.Operands[i], this));
251   }
252 public:
253   StoreInst(Value *Val, Value *Ptr, const vector<Value*> &Idx,
254             const string &Name = "");
255   StoreInst(Value *Val, Value *Ptr, const string &Name = "");
256   virtual Instruction *clone() const { return new StoreInst(*this); }
257
258   virtual const char *getOpcodeName() const { return "store"; }  
259   
260   virtual bool hasSideEffects() const { return true; }
261   virtual unsigned getFirstIndexOperandNumber() const { return 2; }
262
263   // Methods for support type inquiry through isa, cast, and dyn_cast:
264   static inline bool classof(const StoreInst *) { return true; }
265   static inline bool classof(const Instruction *I) {
266     return (I->getOpcode() == Instruction::Store);
267   }
268   static inline bool classof(const Value *V) {
269     return isa<Instruction>(V) && classof(cast<Instruction>(V));
270   }
271 };
272
273
274 //===----------------------------------------------------------------------===//
275 //                             GetElementPtrInst Class
276 //===----------------------------------------------------------------------===//
277
278 class GetElementPtrInst : public MemAccessInst {
279   GetElementPtrInst(const GetElementPtrInst &EPI)
280     : MemAccessInst(EPI.getType(), GetElementPtr) {
281     Operands.reserve(EPI.Operands.size());
282     for (unsigned i = 0, E = EPI.Operands.size(); i != E; ++i)
283       Operands.push_back(Use(EPI.Operands[i], this));
284   }
285 public:
286   GetElementPtrInst(Value *Ptr, const vector<Value*> &Idx,
287                     const string &Name = "");
288   virtual Instruction *clone() const { return new GetElementPtrInst(*this); }
289   virtual const char *getOpcodeName() const { return "getelementptr"; }  
290   virtual unsigned getFirstIndexOperandNumber() const { return 1; }
291   
292   inline bool isArraySelector() const { return !isStructSelector(); }
293   bool isStructSelector() const;
294
295   // getType - Overload to return most specific pointer type...
296   inline const PointerType *getType() const {
297     return cast<const PointerType>(Instruction::getType());
298   }
299
300   // Methods for support type inquiry through isa, cast, and dyn_cast:
301   static inline bool classof(const GetElementPtrInst *) { return true; }
302   static inline bool classof(const Instruction *I) {
303     return (I->getOpcode() == Instruction::GetElementPtr);
304   }
305   static inline bool classof(const Value *V) {
306     return isa<Instruction>(V) && classof(cast<Instruction>(V));
307   }
308 };
309
310 #endif // LLVM_IMEMORY_H