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