Add assertion to check for
[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               ((const 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              ((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(), Operands.size() ? Operands[1] : 0);
64   }
65
66   virtual const char *getOpcodeName() const { return "malloc"; }
67 };
68
69
70 //===----------------------------------------------------------------------===//
71 //                                AllocaInst Class
72 //===----------------------------------------------------------------------===//
73
74 class AllocaInst : public AllocationInst {
75 public:
76   AllocaInst(const Type *Ty, Value *ArraySize = 0, const string &Name = "") 
77     : AllocationInst(Ty, ArraySize, Alloca, Name) {}
78
79   virtual Instruction *clone() const { 
80     return new AllocaInst(getType(), Operands.size() ? Operands[1] : 0);
81   }
82
83   virtual const char *getOpcodeName() const { return "alloca"; }
84 };
85
86
87 //===----------------------------------------------------------------------===//
88 //                                 FreeInst Class
89 //===----------------------------------------------------------------------===//
90
91 class FreeInst : public Instruction {
92 public:
93   FreeInst(Value *Ptr, const string &Name = "") 
94     : Instruction(Type::VoidTy, Free, Name) {
95     assert(Ptr->getType()->isPointerType() && "Can't free nonpointer!");
96     Operands.reserve(1);
97     Operands.push_back(Use(Ptr, this));
98   }
99
100   virtual Instruction *clone() const { return new FreeInst(Operands[0]); }
101
102   virtual const char *getOpcodeName() const { return "free"; }
103
104   virtual bool hasSideEffects() const { return true; }
105 };
106
107
108 //===----------------------------------------------------------------------===//
109 //                              MemAccessInst Class
110 //===----------------------------------------------------------------------===//
111 //
112 // MemAccessInst - Common base class of LoadInst, StoreInst, and
113 // GetElementPtrInst...
114 //
115 class MemAccessInst : public Instruction {
116 protected:
117   inline MemAccessInst(const Type *Ty, unsigned Opcode,
118                        const vector<ConstPoolVal*> &Idx,
119                        const string &Nam = "")
120     : Instruction(Ty, Opcode, Nam),
121       indexVec(Idx)
122   {}
123   
124 protected:
125   vector<ConstPoolVal*> indexVec;
126   
127 public:
128   // getIndexedType - Returns the type of the element that would be loaded with
129   // a load instruction with the specified parameters.
130   //
131   // A null type is returned if the indices are invalid for the specified 
132   // pointer type.
133   //
134   static const Type *getIndexedType(const Type *Ptr, 
135                                     const vector<ConstPoolVal*> &Indices,
136                                     bool AllowStructLeaf = false);
137   
138   const vector<ConstPoolVal*>& getIndexVec() const { return indexVec; }
139   
140   virtual Value* getPtrOperand() = 0;
141   
142   virtual int   getFirstOffsetIdx() const = 0;
143 };
144
145
146 //===----------------------------------------------------------------------===//
147 //                                LoadInst Class
148 //===----------------------------------------------------------------------===//
149
150 class LoadInst : public MemAccessInst {
151   LoadInst(const LoadInst &LI) : MemAccessInst(LI.getType(), Load, LI.getIndexVec()) {
152     Operands.reserve(LI.Operands.size());
153     for (unsigned i = 0, E = LI.Operands.size(); i != E; ++i)
154       Operands.push_back(Use(LI.Operands[i], this));
155   }
156 public:
157   LoadInst(Value *Ptr, const vector<ConstPoolVal*> &Idx,
158            const string &Name = "");
159   virtual Instruction*  clone() const { return new LoadInst(*this); }
160   virtual const char*   getOpcodeName() const { return "load"; }  
161   virtual Value*        getPtrOperand() { return this->getOperand(0); }
162   virtual int getFirstOffsetIdx() const { return (this->getNumOperands() > 1)? 1 : -1;}
163 };
164
165
166 //===----------------------------------------------------------------------===//
167 //                                StoreInst Class
168 //===----------------------------------------------------------------------===//
169
170 class StoreInst : public MemAccessInst {
171   StoreInst(const StoreInst &SI) : MemAccessInst(SI.getType(), Store, SI.getIndexVec()) {
172     Operands.reserve(SI.Operands.size());
173     for (unsigned i = 0, E = SI.Operands.size(); i != E; ++i)
174       Operands.push_back(Use(SI.Operands[i], this));
175   }
176 public:
177   StoreInst(Value *Val, Value *Ptr, const vector<ConstPoolVal*> &Idx,
178             const string &Name = "");
179   virtual Instruction *clone() const { return new StoreInst(*this); }
180   virtual const char *getOpcodeName() const { return "store"; }  
181   
182   virtual bool hasSideEffects() const { return true; }
183   virtual Value*        getPtrOperand() { return this->getOperand(1); }
184   virtual int getFirstOffsetIdx() const { return (this->getNumOperands() > 2)? 2 : -1;}
185 };
186
187
188 //===----------------------------------------------------------------------===//
189 //                             GetElementPtrInst Class
190 //===----------------------------------------------------------------------===//
191
192 class GetElementPtrInst : public MemAccessInst {
193   GetElementPtrInst(const GetElementPtrInst &EPI)
194     : MemAccessInst(EPI.getType(), GetElementPtr, EPI.getIndexVec()) {
195     Operands.reserve(EPI.Operands.size());
196     for (unsigned i = 0, E = EPI.Operands.size(); i != E; ++i)
197       Operands.push_back(Use(EPI.Operands[i], this));
198   }
199 public:
200   GetElementPtrInst(Value *Ptr, const vector<ConstPoolVal*> &Idx,
201                     const string &Name = "");
202   virtual Instruction *clone() const { return new GetElementPtrInst(*this); }
203   virtual const char *getOpcodeName() const { return "getelementptr"; }  
204   virtual Value*        getPtrOperand() { return this->getOperand(0); }
205   virtual int getFirstOffsetIdx() const { return (this->getNumOperands() > 1)? 1 : -1;}
206   
207   inline bool isArraySelector() const { return !isStructSelector(); }
208   bool isStructSelector() const;
209 };
210
211 #endif // LLVM_IMEMORY_H