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