Changes to build successfully with GCC 3.02
[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.
39   //
40   inline bool isArrayAllocation() const { return Operands.size() == 1; }
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
63
64 //===----------------------------------------------------------------------===//
65 //                                MallocInst Class
66 //===----------------------------------------------------------------------===//
67
68 class MallocInst : public AllocationInst {
69 public:
70   MallocInst(const Type *Ty, Value *ArraySize = 0, const std::string &Name = "")
71     : AllocationInst(Ty, ArraySize, Malloc, Name) {}
72
73   virtual Instruction *clone() const { 
74     return new MallocInst(getType(), 
75                           Operands.size() ? (Value*)Operands[0].get() : 0);
76   }
77
78   virtual const char *getOpcodeName() const { return "malloc"; }
79
80   // Methods for support type inquiry through isa, cast, and dyn_cast:
81   static inline bool classof(const MallocInst *) { return true; }
82   static inline bool classof(const Instruction *I) {
83     return (I->getOpcode() == Instruction::Malloc);
84   }
85   static inline bool classof(const Value *V) {
86     return isa<Instruction>(V) && classof(cast<Instruction>(V));
87   }
88 };
89
90
91 //===----------------------------------------------------------------------===//
92 //                                AllocaInst Class
93 //===----------------------------------------------------------------------===//
94
95 class AllocaInst : public AllocationInst {
96 public:
97   AllocaInst(const Type *Ty, Value *ArraySize = 0, const std::string &Name = "")
98     : AllocationInst(Ty, ArraySize, Alloca, Name) {}
99
100   virtual Instruction *clone() const { 
101     return new AllocaInst(getType(),
102                           Operands.size() ? (Value*)Operands[0].get() : 0);
103   }
104
105   virtual const char *getOpcodeName() const { return "alloca"; }
106
107   // Methods for support type inquiry through isa, cast, and dyn_cast:
108   static inline bool classof(const AllocaInst *) { return true; }
109   static inline bool classof(const Instruction *I) {
110     return (I->getOpcode() == Instruction::Alloca);
111   }
112   static inline bool classof(const Value *V) {
113     return isa<Instruction>(V) && classof(cast<Instruction>(V));
114   }
115 };
116
117
118 //===----------------------------------------------------------------------===//
119 //                                 FreeInst Class
120 //===----------------------------------------------------------------------===//
121
122 class FreeInst : public Instruction {
123 public:
124   FreeInst(Value *Ptr) : Instruction(Type::VoidTy, Free, "") {
125     assert(Ptr->getType()->isPointerType() && "Can't free nonpointer!");
126     Operands.reserve(1);
127     Operands.push_back(Use(Ptr, this));
128   }
129
130   virtual Instruction *clone() const { return new FreeInst(Operands[0]); }
131
132   virtual const char *getOpcodeName() const { return "free"; }
133
134   virtual bool hasSideEffects() const { return true; }
135
136   // Methods for support type inquiry through isa, cast, and dyn_cast:
137   static inline bool classof(const FreeInst *) { return true; }
138   static inline bool classof(const Instruction *I) {
139     return (I->getOpcode() == Instruction::Free);
140   }
141   static inline bool classof(const Value *V) {
142     return isa<Instruction>(V) && classof(cast<Instruction>(V));
143   }
144 };
145
146
147 //===----------------------------------------------------------------------===//
148 //                              MemAccessInst Class
149 //===----------------------------------------------------------------------===//
150 //
151 // MemAccessInst - Common base class of LoadInst, StoreInst, and
152 // GetElementPtrInst...
153 //
154 class MemAccessInst : public Instruction {
155 protected:
156   inline MemAccessInst(const Type *Ty, unsigned Opcode,
157                        const std::string &Nam = "")
158     : Instruction(Ty, Opcode, Nam) {}
159 public:
160   // getIndexedType - Returns the type of the element that would be loaded with
161   // a load instruction with the specified parameters.
162   //
163   // A null type is returned if the indices are invalid for the specified 
164   // pointer type.
165   //
166   static const Type *getIndexedType(const Type *Ptr, 
167                                     const std::vector<Value*> &Indices,
168                                     bool AllowStructLeaf = false);
169
170   inline op_iterator       idx_begin()       {
171     return op_begin()+getFirstIndexOperandNumber();
172   }
173   inline const_op_iterator idx_begin() const {
174     return op_begin()+getFirstIndexOperandNumber();
175   }
176   inline op_iterator       idx_end()         { return op_end(); }
177   inline const_op_iterator idx_end()   const { return op_end(); }
178
179
180   std::vector<Value*> copyIndices() const {
181     return std::vector<Value*>(idx_begin(), idx_end());
182   }
183
184   Value *getPointerOperand() {
185     return getOperand(getFirstIndexOperandNumber()-1);
186   }
187   const Value *getPointerOperand() const {
188     return getOperand(getFirstIndexOperandNumber()-1);
189   }
190   
191   virtual unsigned getFirstIndexOperandNumber() const = 0;
192
193   inline bool hasIndices() const {
194     return getNumOperands() > getFirstIndexOperandNumber();
195   }
196
197   // Methods for support type inquiry through isa, cast, and dyn_cast:
198   static inline bool classof(const MemAccessInst *) { return true; }
199   static inline bool classof(const Instruction *I) {
200     return I->getOpcode() == Load || I->getOpcode() == Store ||
201            I->getOpcode() == GetElementPtr;
202   }
203   static inline bool classof(const Value *V) {
204     return isa<Instruction>(V) && classof(cast<Instruction>(V));
205   }
206 };
207
208
209 //===----------------------------------------------------------------------===//
210 //                                LoadInst Class
211 //===----------------------------------------------------------------------===//
212
213 class LoadInst : public MemAccessInst {
214   LoadInst(const LoadInst &LI) : MemAccessInst(LI.getType(), Load) {
215     Operands.reserve(LI.Operands.size());
216     for (unsigned i = 0, E = LI.Operands.size(); i != E; ++i)
217       Operands.push_back(Use(LI.Operands[i], this));
218   }
219 public:
220   LoadInst(Value *Ptr, const std::vector<Value*> &Ix, const std::string & = "");
221   LoadInst(Value *Ptr, const std::string &Name = "");
222
223   virtual Instruction *clone() const { return new LoadInst(*this); }
224   virtual const char *getOpcodeName() const { return "load"; }  
225
226   virtual unsigned getFirstIndexOperandNumber() const { return 1; }
227
228   // Methods for support type inquiry through isa, cast, and dyn_cast:
229   static inline bool classof(const LoadInst *) { return true; }
230   static inline bool classof(const Instruction *I) {
231     return (I->getOpcode() == Instruction::Load);
232   }
233   static inline bool classof(const Value *V) {
234     return isa<Instruction>(V) && classof(cast<Instruction>(V));
235   }
236 };
237
238
239 //===----------------------------------------------------------------------===//
240 //                                StoreInst Class
241 //===----------------------------------------------------------------------===//
242
243 class StoreInst : public MemAccessInst {
244   StoreInst(const StoreInst &SI) : MemAccessInst(SI.getType(), Store) {
245     Operands.reserve(SI.Operands.size());
246     for (unsigned i = 0, E = SI.Operands.size(); i != E; ++i)
247       Operands.push_back(Use(SI.Operands[i], this));
248   }
249 public:
250   StoreInst(Value *Val, Value *Ptr, const std::vector<Value*> &Idx,
251             const std::string &Name = "");
252   StoreInst(Value *Val, Value *Ptr, const std::string &Name = "");
253   virtual Instruction *clone() const { return new StoreInst(*this); }
254
255   virtual const char *getOpcodeName() const { return "store"; }  
256   
257   virtual bool hasSideEffects() const { return true; }
258   virtual unsigned getFirstIndexOperandNumber() const { return 2; }
259
260   // Methods for support type inquiry through isa, cast, and dyn_cast:
261   static inline bool classof(const StoreInst *) { return true; }
262   static inline bool classof(const Instruction *I) {
263     return (I->getOpcode() == Instruction::Store);
264   }
265   static inline bool classof(const Value *V) {
266     return isa<Instruction>(V) && classof(cast<Instruction>(V));
267   }
268 };
269
270
271 //===----------------------------------------------------------------------===//
272 //                             GetElementPtrInst Class
273 //===----------------------------------------------------------------------===//
274
275 class GetElementPtrInst : public MemAccessInst {
276   GetElementPtrInst(const GetElementPtrInst &EPI)
277     : MemAccessInst(EPI.getType(), GetElementPtr) {
278     Operands.reserve(EPI.Operands.size());
279     for (unsigned i = 0, E = EPI.Operands.size(); i != E; ++i)
280       Operands.push_back(Use(EPI.Operands[i], this));
281   }
282 public:
283   GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
284                     const std::string &Name = "");
285   virtual Instruction *clone() const { return new GetElementPtrInst(*this); }
286   virtual const char *getOpcodeName() const { return "getelementptr"; }  
287   virtual unsigned getFirstIndexOperandNumber() const { return 1; }
288   
289   inline bool isArraySelector() const { return !isStructSelector(); }
290   bool isStructSelector() const;
291
292   // getType - Overload to return most specific pointer type...
293   inline const PointerType *getType() const {
294     return cast<const PointerType>(Instruction::getType());
295   }
296
297   // Methods for support type inquiry through isa, cast, and dyn_cast:
298   static inline bool classof(const GetElementPtrInst *) { return true; }
299   static inline bool classof(const Instruction *I) {
300     return (I->getOpcode() == Instruction::GetElementPtr);
301   }
302   static inline bool classof(const Value *V) {
303     return isa<Instruction>(V) && classof(cast<Instruction>(V));
304   }
305 };
306
307 #endif // LLVM_IMEMORY_H