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