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