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