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