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