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