Rename the intrinsic enum values for llvm.va_* from Intrinsic::va_* to
[oota-llvm.git] / include / llvm / iMemory.h
1 //===-- llvm/iMemory.h - Memory Operator node definitions -------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the declarations of all of the memory related operators.
11 // This includes: malloc, free, alloca, load, store, and getelementptr
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_IMEMORY_H
16 #define LLVM_IMEMORY_H
17
18 #include "llvm/Instruction.h"
19
20 namespace llvm {
21
22 class PointerType;
23
24 //===----------------------------------------------------------------------===//
25 //                             AllocationInst Class
26 //===----------------------------------------------------------------------===//
27
28 /// AllocationInst - This class is the common base class of MallocInst and
29 /// AllocaInst.
30 ///
31 class AllocationInst : public Instruction {
32 protected:
33   AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, 
34                  const std::string &Name = "", Instruction *InsertBefore = 0);
35 public:
36
37   /// isArrayAllocation - Return true if there is an allocation size parameter
38   /// to the allocation instruction that is not 1.
39   ///
40   bool isArrayAllocation() const;
41
42   /// getArraySize - Get the number of element allocated, for a simple
43   /// allocation of a single element, this will return a constant 1 value.
44   ///
45   inline const Value *getArraySize() const { return Operands[0]; }
46   inline Value *getArraySize() { return Operands[0]; }
47
48   /// getType - Overload to return most specific pointer type
49   ///
50   inline const PointerType *getType() const {
51     return reinterpret_cast<const PointerType*>(Instruction::getType()); 
52   }
53
54   /// getAllocatedType - Return the type that is being allocated by the
55   /// instruction.
56   ///
57   const Type *getAllocatedType() const;
58
59   virtual Instruction *clone() const = 0;
60
61   // Methods for support type inquiry through isa, cast, and dyn_cast:
62   static inline bool classof(const AllocationInst *) { return true; }
63   static inline bool classof(const Instruction *I) {
64     return I->getOpcode() == Instruction::Alloca ||
65            I->getOpcode() == Instruction::Malloc;
66   }
67   static inline bool classof(const Value *V) {
68     return isa<Instruction>(V) && classof(cast<Instruction>(V));
69   }
70 };
71
72
73 //===----------------------------------------------------------------------===//
74 //                                MallocInst Class
75 //===----------------------------------------------------------------------===//
76
77 /// MallocInst - an instruction to allocated memory on the heap
78 ///
79 class MallocInst : public AllocationInst {
80   MallocInst(const MallocInst &MI);
81 public:
82   MallocInst(const Type *Ty, Value *ArraySize = 0, const std::string &Name = "",
83              Instruction *InsertBefore = 0)
84     : AllocationInst(Ty, ArraySize, Malloc, Name, InsertBefore) {}
85
86   virtual Instruction *clone() const { 
87     return new MallocInst(*this);
88   }
89
90   // Methods for support type inquiry through isa, cast, and dyn_cast:
91   static inline bool classof(const MallocInst *) { return true; }
92   static inline bool classof(const Instruction *I) {
93     return (I->getOpcode() == Instruction::Malloc);
94   }
95   static inline bool classof(const Value *V) {
96     return isa<Instruction>(V) && classof(cast<Instruction>(V));
97   }
98 };
99
100
101 //===----------------------------------------------------------------------===//
102 //                                AllocaInst Class
103 //===----------------------------------------------------------------------===//
104
105 /// AllocaInst - an instruction to allocate memory on the stack
106 ///
107 class AllocaInst : public AllocationInst {
108   AllocaInst(const AllocaInst &);
109 public:
110   AllocaInst(const Type *Ty, Value *ArraySize = 0, const std::string &Name = "",
111              Instruction *InsertBefore = 0)
112     : AllocationInst(Ty, ArraySize, Alloca, Name, InsertBefore) {}
113
114   virtual Instruction *clone() const { 
115     return new AllocaInst(*this);
116   }
117
118   // Methods for support type inquiry through isa, cast, and dyn_cast:
119   static inline bool classof(const AllocaInst *) { return true; }
120   static inline bool classof(const Instruction *I) {
121     return (I->getOpcode() == Instruction::Alloca);
122   }
123   static inline bool classof(const Value *V) {
124     return isa<Instruction>(V) && classof(cast<Instruction>(V));
125   }
126 };
127
128
129 //===----------------------------------------------------------------------===//
130 //                                 FreeInst Class
131 //===----------------------------------------------------------------------===//
132
133 /// FreeInst - an instruction to deallocate memory
134 ///
135 struct FreeInst : public Instruction {
136   FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
137
138   virtual Instruction *clone() const { return new FreeInst(Operands[0]); }
139
140   virtual bool mayWriteToMemory() const { return true; }
141
142   // Methods for support type inquiry through isa, cast, and dyn_cast:
143   static inline bool classof(const FreeInst *) { return true; }
144   static inline bool classof(const Instruction *I) {
145     return (I->getOpcode() == Instruction::Free);
146   }
147   static inline bool classof(const Value *V) {
148     return isa<Instruction>(V) && classof(cast<Instruction>(V));
149   }
150 };
151
152
153 //===----------------------------------------------------------------------===//
154 //                                LoadInst Class
155 //===----------------------------------------------------------------------===//
156
157 /// LoadInst - an instruction for reading from memory 
158 ///
159 class LoadInst : public Instruction {
160   LoadInst(const LoadInst &LI) : Instruction(LI.getType(), Load) {
161     Volatile = LI.isVolatile();
162     Operands.reserve(1);
163     Operands.push_back(Use(LI.Operands[0], this));
164   }
165   bool Volatile;   // True if this is a volatile load
166 public:
167   LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBefore);
168   LoadInst(Value *Ptr, const std::string &Name = "", bool isVolatile = false,
169            Instruction *InsertBefore = 0);
170
171   /// isVolatile - Return true if this is a load from a volatile memory
172   /// location.
173   ///
174   bool isVolatile() const { return Volatile; }
175
176   /// setVolatile - Specify whether this is a volatile load or not.
177   ///
178   void setVolatile(bool V) { Volatile = V; }
179
180   virtual Instruction *clone() const { return new LoadInst(*this); }
181
182   virtual bool mayWriteToMemory() const { return isVolatile(); }
183
184   Value *getPointerOperand() { return getOperand(0); }
185   const Value *getPointerOperand() const { return getOperand(0); }
186   static unsigned getPointerOperandIndex() { return 0U; }
187
188   // Methods for support type inquiry through isa, cast, and dyn_cast:
189   static inline bool classof(const LoadInst *) { return true; }
190   static inline bool classof(const Instruction *I) {
191     return I->getOpcode() == Instruction::Load;
192   }
193   static inline bool classof(const Value *V) {
194     return isa<Instruction>(V) && classof(cast<Instruction>(V));
195   }
196 };
197
198
199 //===----------------------------------------------------------------------===//
200 //                                StoreInst Class
201 //===----------------------------------------------------------------------===//
202
203 /// StoreInst - an instruction for storing to memory 
204 ///
205 class StoreInst : public Instruction {
206   StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store) {
207     Volatile = SI.isVolatile();
208     Operands.reserve(2);
209     Operands.push_back(Use(SI.Operands[0], this));
210     Operands.push_back(Use(SI.Operands[1], this));
211   }
212   bool Volatile;   // True if this is a volatile store
213 public:
214   StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
215   StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
216             Instruction *InsertBefore = 0);
217
218
219   /// isVolatile - Return true if this is a load from a volatile memory
220   /// location.
221   ///
222   bool isVolatile() const { return Volatile; }
223
224   /// setVolatile - Specify whether this is a volatile load or not.
225   ///
226   void setVolatile(bool V) { Volatile = V; }
227
228   virtual Instruction *clone() const { return new StoreInst(*this); }
229
230   virtual bool mayWriteToMemory() const { return true; }
231
232   Value *getPointerOperand() { return getOperand(1); }
233   const Value *getPointerOperand() const { return getOperand(1); }
234   static unsigned getPointerOperandIndex() { return 1U; }
235
236   // Methods for support type inquiry through isa, cast, and dyn_cast:
237   static inline bool classof(const StoreInst *) { return true; }
238   static inline bool classof(const Instruction *I) {
239     return I->getOpcode() == Instruction::Store;
240   }
241   static inline bool classof(const Value *V) {
242     return isa<Instruction>(V) && classof(cast<Instruction>(V));
243   }
244 };
245
246
247 //===----------------------------------------------------------------------===//
248 //                             GetElementPtrInst Class
249 //===----------------------------------------------------------------------===//
250
251 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
252 /// access elements of arrays and structs
253 ///
254 class GetElementPtrInst : public Instruction {
255   GetElementPtrInst(const GetElementPtrInst &EPI)
256     : Instruction(reinterpret_cast<const Type*>(EPI.getType()), GetElementPtr) {
257     Operands.reserve(EPI.Operands.size());
258     for (unsigned i = 0, E = EPI.Operands.size(); i != E; ++i)
259       Operands.push_back(Use(EPI.Operands[i], this));
260   }
261 public:
262   GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
263                     const std::string &Name = "", Instruction *InsertBefore =0);
264   virtual Instruction *clone() const { return new GetElementPtrInst(*this); }
265   
266   // getType - Overload to return most specific pointer type...
267   inline const PointerType *getType() const {
268     return reinterpret_cast<const PointerType*>(Instruction::getType());
269   }
270
271   /// getIndexedType - Returns the type of the element that would be loaded with
272   /// a load instruction with the specified parameters.
273   ///
274   /// A null type is returned if the indices are invalid for the specified 
275   /// pointer type.
276   ///
277   static const Type *getIndexedType(const Type *Ptr, 
278                                     const std::vector<Value*> &Indices,
279                                     bool AllowStructLeaf = false);
280   
281   inline op_iterator       idx_begin()       { return op_begin()+1; }
282   inline const_op_iterator idx_begin() const { return op_begin()+1; }
283   inline op_iterator       idx_end()         { return op_end(); }
284   inline const_op_iterator idx_end()   const { return op_end(); }
285
286   Value *getPointerOperand() {
287     return getOperand(0);
288   }
289   const Value *getPointerOperand() const {
290     return getOperand(0);
291   }
292   static unsigned getPointerOperandIndex() {
293     return 0U;                      // get index for modifying correct operand
294   }
295
296   inline unsigned getNumIndices() const {  // Note: always non-negative
297     return getNumOperands() - 1;
298   }
299   
300   inline bool hasIndices() const {
301     return getNumOperands() > 1;
302   }
303
304   // Methods for support type inquiry through isa, cast, and dyn_cast:
305   static inline bool classof(const GetElementPtrInst *) { return true; }
306   static inline bool classof(const Instruction *I) {
307     return (I->getOpcode() == Instruction::GetElementPtr);
308   }
309   static inline bool classof(const Value *V) {
310     return isa<Instruction>(V) && classof(cast<Instruction>(V));
311   }
312 };
313
314 } // End llvm namespace
315
316 #endif // LLVM_IMEMORY_H