Eliminated the MemAccessInst class, folding contents into GEP class.
[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 //                                LoadInst Class
132 //===----------------------------------------------------------------------===//
133
134 class LoadInst : public Instruction {
135   LoadInst(const LoadInst &LI) : Instruction(LI.getType(), Load) {
136     Operands.reserve(1);
137     Operands.push_back(Use(LI.Operands[0], this));
138   }
139 public:
140   LoadInst(Value *Ptr, const std::string &Name = "");
141
142   virtual Instruction *clone() const { return new LoadInst(*this); }
143
144   Value *getPointerOperand() { return getOperand(0); }
145   const Value *getPointerOperand() const { return getOperand(0); }
146
147   // Methods for support type inquiry through isa, cast, and dyn_cast:
148   static inline bool classof(const LoadInst *) { return true; }
149   static inline bool classof(const Instruction *I) {
150     return (I->getOpcode() == Instruction::Load);
151   }
152   static inline bool classof(const Value *V) {
153     return isa<Instruction>(V) && classof(cast<Instruction>(V));
154   }
155 };
156
157
158 //===----------------------------------------------------------------------===//
159 //                                StoreInst Class
160 //===----------------------------------------------------------------------===//
161
162 class StoreInst : public Instruction {
163   StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store) {
164     Operands.reserve(2);
165     Operands.push_back(Use(SI.Operands[0], this));
166     Operands.push_back(Use(SI.Operands[1], this));
167   }
168 public:
169   StoreInst(Value *Val, Value *Ptr);
170   virtual Instruction *clone() const { return new StoreInst(*this); }
171
172   virtual bool hasSideEffects() const { return true; }
173
174   Value *getPointerOperand() { return getOperand(1); }
175   const Value *getPointerOperand() const { return getOperand(1); }
176
177   // Methods for support type inquiry through isa, cast, and dyn_cast:
178   static inline bool classof(const StoreInst *) { return true; }
179   static inline bool classof(const Instruction *I) {
180     return (I->getOpcode() == Instruction::Store);
181   }
182   static inline bool classof(const Value *V) {
183     return isa<Instruction>(V) && classof(cast<Instruction>(V));
184   }
185 };
186
187
188 //===----------------------------------------------------------------------===//
189 //                             GetElementPtrInst Class
190 //===----------------------------------------------------------------------===//
191
192 class GetElementPtrInst : public Instruction {
193   GetElementPtrInst(const GetElementPtrInst &EPI)
194     : Instruction((Type*)EPI.getType(), GetElementPtr) {
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 std::vector<Value*> &Idx,
201                     const std::string &Name = "");
202   virtual Instruction *clone() const { return new GetElementPtrInst(*this); }
203   
204   // getType - Overload to return most specific pointer type...
205   inline const PointerType *getType() const {
206     return (PointerType*)Instruction::getType();
207   }
208
209   /// getIndexedType - Returns the type of the element that would be loaded with
210   /// a load instruction with the specified parameters.
211   ///
212   /// A null type is returned if the indices are invalid for the specified 
213   /// pointer type.
214   ///
215   static const Type *getIndexedType(const Type *Ptr, 
216                                     const std::vector<Value*> &Indices,
217                                     bool AllowStructLeaf = false);
218   
219   inline op_iterator       idx_begin()       {
220     return op_begin()+1;
221   }
222   inline const_op_iterator idx_begin() const {
223     return op_begin()+1;
224   }
225   inline op_iterator       idx_end()         { return op_end(); }
226   inline const_op_iterator idx_end()   const { return op_end(); }
227
228   Value *getPointerOperand() {
229     return getOperand(0);
230   }
231   const Value *getPointerOperand() const {
232     return getOperand(0);
233   }
234   
235   inline unsigned getNumIndices() const {  // Note: always non-negative
236     return getNumOperands() - 1;
237   }
238   
239   inline bool hasIndices() const {
240     return getNumOperands() > 1;
241   }
242
243   // Methods for support type inquiry through isa, cast, and dyn_cast:
244   static inline bool classof(const GetElementPtrInst *) { return true; }
245   static inline bool classof(const Instruction *I) {
246     return (I->getOpcode() == Instruction::GetElementPtr);
247   }
248   static inline bool classof(const Value *V) {
249     return isa<Instruction>(V) && classof(cast<Instruction>(V));
250   }
251 };
252
253 #endif // LLVM_IMEMORY_H