Add isa,cast,dyncast support for AllocationInst.
[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 std::string &Name = "")
25     : Instruction(Ty, iTy, Name) {
26     assert(Ty->isPointerType() && "Can't allocate a non pointer type!");
27
28     if (ArraySize) {
29       assert(ArraySize->getType() == Type::UIntTy &&
30              "Malloc/Allocation array size != UIntTy!");
31
32       Operands.reserve(1);
33       Operands.push_back(Use(ArraySize, this));
34     }
35   }
36
37   // isArrayAllocation - Return true if there is an allocation size parameter
38   // to the allocation instruction that is not 1.
39   //
40   bool isArrayAllocation() const;
41
42   inline const Value *getArraySize() const {
43     assert(isArrayAllocation()); return Operands[0];
44   }
45   inline Value *getArraySize() {
46     assert(isArrayAllocation()); return Operands[0];
47   }
48
49   // getType - Overload to return most specific pointer type...
50   inline const PointerType *getType() const {
51     return (const PointerType*)Instruction::getType(); 
52   }
53
54   // getAllocatedType - Return the type that is being allocated by the
55   // instruction.
56   inline const Type *getAllocatedType() const {
57     return getType()->getElementType();
58   }
59
60   virtual Instruction *clone() const = 0;
61
62   // Methods for support type inquiry through isa, cast, and dyn_cast:
63   static inline bool classof(const AllocationInst *) { return true; }
64   static inline bool classof(const Instruction *I) {
65     return I->getOpcode() == Instruction::Alloca ||
66            I->getOpcode() == Instruction::Malloc;
67   }
68   static inline bool classof(const Value *V) {
69     return isa<Instruction>(V) && classof(cast<Instruction>(V));
70   }
71 };
72
73
74 //===----------------------------------------------------------------------===//
75 //                                MallocInst Class
76 //===----------------------------------------------------------------------===//
77
78 class MallocInst : public AllocationInst {
79 public:
80   MallocInst(const Type *Ty, Value *ArraySize = 0, const std::string &Name = "")
81     : AllocationInst(Ty, ArraySize, Malloc, Name) {}
82
83   virtual Instruction *clone() const { 
84     return new MallocInst(getType(), 
85                           Operands.size() ? (Value*)Operands[0].get() : 0);
86   }
87
88   virtual const char *getOpcodeName() const { return "malloc"; }
89
90   // Methods for support type inquiry through isa, cast, and dyn_cast:
91   static inline bool classof(const MallocInst *) { return true; }
92   static inline bool classof(const Instruction *I) {
93     return (I->getOpcode() == Instruction::Malloc);
94   }
95   static inline bool classof(const Value *V) {
96     return isa<Instruction>(V) && classof(cast<Instruction>(V));
97   }
98 };
99
100
101 //===----------------------------------------------------------------------===//
102 //                                AllocaInst Class
103 //===----------------------------------------------------------------------===//
104
105 class AllocaInst : public AllocationInst {
106 public:
107   AllocaInst(const Type *Ty, Value *ArraySize = 0, const std::string &Name = "")
108     : AllocationInst(Ty, ArraySize, Alloca, Name) {}
109
110   virtual Instruction *clone() const { 
111     return new AllocaInst(getType(),
112                           Operands.size() ? (Value*)Operands[0].get() : 0);
113   }
114
115   virtual const char *getOpcodeName() const { return "alloca"; }
116
117   // Methods for support type inquiry through isa, cast, and dyn_cast:
118   static inline bool classof(const AllocaInst *) { return true; }
119   static inline bool classof(const Instruction *I) {
120     return (I->getOpcode() == Instruction::Alloca);
121   }
122   static inline bool classof(const Value *V) {
123     return isa<Instruction>(V) && classof(cast<Instruction>(V));
124   }
125 };
126
127
128 //===----------------------------------------------------------------------===//
129 //                                 FreeInst Class
130 //===----------------------------------------------------------------------===//
131
132 class FreeInst : public Instruction {
133 public:
134   FreeInst(Value *Ptr) : Instruction(Type::VoidTy, Free, "") {
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 std::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 std::vector<Value*> &Indices,
178                                     bool AllowStructLeaf = false);
179
180   inline op_iterator       idx_begin()       {
181     return op_begin()+getFirstIndexOperandNumber();
182   }
183   inline const_op_iterator idx_begin() const {
184     return op_begin()+getFirstIndexOperandNumber();
185   }
186   inline op_iterator       idx_end()         { return op_end(); }
187   inline const_op_iterator idx_end()   const { return op_end(); }
188
189
190   std::vector<Value*> copyIndices() const {
191     return std::vector<Value*>(idx_begin(), idx_end());
192   }
193
194   Value *getPointerOperand() {
195     return getOperand(getFirstIndexOperandNumber()-1);
196   }
197   const Value *getPointerOperand() const {
198     return getOperand(getFirstIndexOperandNumber()-1);
199   }
200   
201   virtual unsigned getFirstIndexOperandNumber() const = 0;
202
203   inline bool hasIndices() const {
204     return getNumOperands() > getFirstIndexOperandNumber();
205   }
206
207   // Methods for support type inquiry through isa, cast, and dyn_cast:
208   static inline bool classof(const MemAccessInst *) { return true; }
209   static inline bool classof(const Instruction *I) {
210     return I->getOpcode() == Load || I->getOpcode() == Store ||
211            I->getOpcode() == GetElementPtr;
212   }
213   static inline bool classof(const Value *V) {
214     return isa<Instruction>(V) && classof(cast<Instruction>(V));
215   }
216 };
217
218
219 //===----------------------------------------------------------------------===//
220 //                                LoadInst Class
221 //===----------------------------------------------------------------------===//
222
223 class LoadInst : public MemAccessInst {
224   LoadInst(const LoadInst &LI) : MemAccessInst(LI.getType(), Load) {
225     Operands.reserve(LI.Operands.size());
226     for (unsigned i = 0, E = LI.Operands.size(); i != E; ++i)
227       Operands.push_back(Use(LI.Operands[i], this));
228   }
229 public:
230   LoadInst(Value *Ptr, const std::vector<Value*> &Ix, const std::string & = "");
231   LoadInst(Value *Ptr, const std::string &Name = "");
232
233   virtual Instruction *clone() const { return new LoadInst(*this); }
234   virtual const char *getOpcodeName() const { return "load"; }  
235
236   virtual unsigned getFirstIndexOperandNumber() const { return 1; }
237
238   // Methods for support type inquiry through isa, cast, and dyn_cast:
239   static inline bool classof(const LoadInst *) { return true; }
240   static inline bool classof(const Instruction *I) {
241     return (I->getOpcode() == Instruction::Load);
242   }
243   static inline bool classof(const Value *V) {
244     return isa<Instruction>(V) && classof(cast<Instruction>(V));
245   }
246 };
247
248
249 //===----------------------------------------------------------------------===//
250 //                                StoreInst Class
251 //===----------------------------------------------------------------------===//
252
253 class StoreInst : public MemAccessInst {
254   StoreInst(const StoreInst &SI) : MemAccessInst(SI.getType(), Store) {
255     Operands.reserve(SI.Operands.size());
256     for (unsigned i = 0, E = SI.Operands.size(); i != E; ++i)
257       Operands.push_back(Use(SI.Operands[i], this));
258   }
259 public:
260   StoreInst(Value *Val, Value *Ptr, const std::vector<Value*> &Idx,
261             const std::string &Name = "");
262   StoreInst(Value *Val, Value *Ptr, const std::string &Name = "");
263   virtual Instruction *clone() const { return new StoreInst(*this); }
264
265   virtual const char *getOpcodeName() const { return "store"; }  
266   
267   virtual bool hasSideEffects() const { return true; }
268   virtual unsigned getFirstIndexOperandNumber() const { return 2; }
269
270   // Methods for support type inquiry through isa, cast, and dyn_cast:
271   static inline bool classof(const StoreInst *) { return true; }
272   static inline bool classof(const Instruction *I) {
273     return (I->getOpcode() == Instruction::Store);
274   }
275   static inline bool classof(const Value *V) {
276     return isa<Instruction>(V) && classof(cast<Instruction>(V));
277   }
278 };
279
280
281 //===----------------------------------------------------------------------===//
282 //                             GetElementPtrInst Class
283 //===----------------------------------------------------------------------===//
284
285 class GetElementPtrInst : public MemAccessInst {
286   GetElementPtrInst(const GetElementPtrInst &EPI)
287     : MemAccessInst(EPI.getType(), GetElementPtr) {
288     Operands.reserve(EPI.Operands.size());
289     for (unsigned i = 0, E = EPI.Operands.size(); i != E; ++i)
290       Operands.push_back(Use(EPI.Operands[i], this));
291   }
292 public:
293   GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
294                     const std::string &Name = "");
295   virtual Instruction *clone() const { return new GetElementPtrInst(*this); }
296   virtual const char *getOpcodeName() const { return "getelementptr"; }  
297   virtual unsigned getFirstIndexOperandNumber() const { return 1; }
298   
299   inline bool isArraySelector() const { return !isStructSelector(); }
300   bool isStructSelector() const;
301
302   // getType - Overload to return most specific pointer type...
303   inline const PointerType *getType() const {
304     return cast<const PointerType>(Instruction::getType());
305   }
306
307   // Methods for support type inquiry through isa, cast, and dyn_cast:
308   static inline bool classof(const GetElementPtrInst *) { return true; }
309   static inline bool classof(const Instruction *I) {
310     return (I->getOpcode() == Instruction::GetElementPtr);
311   }
312   static inline bool classof(const Value *V) {
313     return isa<Instruction>(V) && classof(cast<Instruction>(V));
314   }
315 };
316
317 #endif // LLVM_IMEMORY_H