* Change code to use a static_cast instead of reinterpret_cast
[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   void init(const Type *Ty, Value *ArraySize, unsigned iTy);
34   AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, 
35                  const std::string &Name = "", Instruction *InsertBefore = 0);
36   AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, 
37                  const std::string &Name, BasicBlock *InsertAtEnd);
38
39 public:
40
41   /// isArrayAllocation - Return true if there is an allocation size parameter
42   /// to the allocation instruction that is not 1.
43   ///
44   bool isArrayAllocation() const;
45
46   /// getArraySize - Get the number of element allocated, for a simple
47   /// allocation of a single element, this will return a constant 1 value.
48   ///
49   inline const Value *getArraySize() const { return Operands[0]; }
50   inline Value *getArraySize() { return Operands[0]; }
51
52   /// getType - Overload to return most specific pointer type
53   ///
54   inline const PointerType *getType() const {
55     return reinterpret_cast<const PointerType*>(Instruction::getType()); 
56   }
57
58   /// getAllocatedType - Return the type that is being allocated by the
59   /// instruction.
60   ///
61   const Type *getAllocatedType() const;
62
63   virtual Instruction *clone() const = 0;
64
65   // Methods for support type inquiry through isa, cast, and dyn_cast:
66   static inline bool classof(const AllocationInst *) { return true; }
67   static inline bool classof(const Instruction *I) {
68     return I->getOpcode() == Instruction::Alloca ||
69            I->getOpcode() == Instruction::Malloc;
70   }
71   static inline bool classof(const Value *V) {
72     return isa<Instruction>(V) && classof(cast<Instruction>(V));
73   }
74 };
75
76
77 //===----------------------------------------------------------------------===//
78 //                                MallocInst Class
79 //===----------------------------------------------------------------------===//
80
81 /// MallocInst - an instruction to allocated memory on the heap
82 ///
83 class MallocInst : public AllocationInst {
84   MallocInst(const MallocInst &MI);
85 public:
86   explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
87                       const std::string &Name = "",
88                       Instruction *InsertBefore = 0)
89     : AllocationInst(Ty, ArraySize, Malloc, Name, InsertBefore) {}
90   MallocInst(const Type *Ty, Value *ArraySize, const std::string &Name,
91              BasicBlock *InsertAtEnd)
92     : AllocationInst(Ty, ArraySize, Malloc, Name, InsertAtEnd) {}
93
94   virtual Instruction *clone() const { 
95     return new MallocInst(*this);
96   }
97
98   // Methods for support type inquiry through isa, cast, and dyn_cast:
99   static inline bool classof(const MallocInst *) { return true; }
100   static inline bool classof(const Instruction *I) {
101     return (I->getOpcode() == Instruction::Malloc);
102   }
103   static inline bool classof(const Value *V) {
104     return isa<Instruction>(V) && classof(cast<Instruction>(V));
105   }
106 };
107
108
109 //===----------------------------------------------------------------------===//
110 //                                AllocaInst Class
111 //===----------------------------------------------------------------------===//
112
113 /// AllocaInst - an instruction to allocate memory on the stack
114 ///
115 class AllocaInst : public AllocationInst {
116   AllocaInst(const AllocaInst &);
117 public:
118   explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
119                       const std::string &Name = "",
120                       Instruction *InsertBefore = 0)
121     : AllocationInst(Ty, ArraySize, Alloca, Name, InsertBefore) {}
122   AllocaInst(const Type *Ty, Value *ArraySize, const std::string &Name,
123              BasicBlock *InsertAtEnd)
124     : AllocationInst(Ty, ArraySize, Alloca, Name, InsertAtEnd) {}
125
126   virtual Instruction *clone() const { 
127     return new AllocaInst(*this);
128   }
129
130   // Methods for support type inquiry through isa, cast, and dyn_cast:
131   static inline bool classof(const AllocaInst *) { return true; }
132   static inline bool classof(const Instruction *I) {
133     return (I->getOpcode() == Instruction::Alloca);
134   }
135   static inline bool classof(const Value *V) {
136     return isa<Instruction>(V) && classof(cast<Instruction>(V));
137   }
138 };
139
140
141 //===----------------------------------------------------------------------===//
142 //                                 FreeInst Class
143 //===----------------------------------------------------------------------===//
144
145 /// FreeInst - an instruction to deallocate memory
146 ///
147 class FreeInst : public Instruction {
148   void init(Value *Ptr);
149
150 public:
151   explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
152   FreeInst(Value *Ptr, BasicBlock *InsertAfter);
153
154   virtual Instruction *clone() const { return new FreeInst(Operands[0]); }
155
156   virtual bool mayWriteToMemory() const { return true; }
157
158   // Methods for support type inquiry through isa, cast, and dyn_cast:
159   static inline bool classof(const FreeInst *) { return true; }
160   static inline bool classof(const Instruction *I) {
161     return (I->getOpcode() == Instruction::Free);
162   }
163   static inline bool classof(const Value *V) {
164     return isa<Instruction>(V) && classof(cast<Instruction>(V));
165   }
166 };
167
168
169 //===----------------------------------------------------------------------===//
170 //                                LoadInst Class
171 //===----------------------------------------------------------------------===//
172
173 /// LoadInst - an instruction for reading from memory 
174 ///
175 class LoadInst : public Instruction {
176   LoadInst(const LoadInst &LI) : Instruction(LI.getType(), Load) {
177     Volatile = LI.isVolatile();
178     init(LI.Operands[0]);
179   }
180   bool Volatile;   // True if this is a volatile load
181   void init(Value *Ptr);
182 public:
183   LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBefore);
184   LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAtEnd);
185   LoadInst(Value *Ptr, const std::string &Name = "", bool isVolatile = false,
186            Instruction *InsertBefore = 0);
187   LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
188            BasicBlock *InsertAtEnd);
189
190   /// isVolatile - Return true if this is a load from a volatile memory
191   /// location.
192   ///
193   bool isVolatile() const { return Volatile; }
194
195   /// setVolatile - Specify whether this is a volatile load or not.
196   ///
197   void setVolatile(bool V) { Volatile = V; }
198
199   virtual Instruction *clone() const { return new LoadInst(*this); }
200
201   virtual bool mayWriteToMemory() const { return isVolatile(); }
202
203   Value *getPointerOperand() { return getOperand(0); }
204   const Value *getPointerOperand() const { return getOperand(0); }
205   static unsigned getPointerOperandIndex() { return 0U; }
206
207   // Methods for support type inquiry through isa, cast, and dyn_cast:
208   static inline bool classof(const LoadInst *) { return true; }
209   static inline bool classof(const Instruction *I) {
210     return I->getOpcode() == Instruction::Load;
211   }
212   static inline bool classof(const Value *V) {
213     return isa<Instruction>(V) && classof(cast<Instruction>(V));
214   }
215 };
216
217
218 //===----------------------------------------------------------------------===//
219 //                                StoreInst Class
220 //===----------------------------------------------------------------------===//
221
222 /// StoreInst - an instruction for storing to memory 
223 ///
224 class StoreInst : public Instruction {
225   StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store) {
226     Volatile = SI.isVolatile();
227     init(SI.Operands[0], SI.Operands[1]);
228   }
229   bool Volatile;   // True if this is a volatile store
230   void init(Value *Val, Value *Ptr);
231 public:
232   StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
233   StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
234   StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
235             Instruction *InsertBefore = 0);
236   StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
237
238
239   /// isVolatile - Return true if this is a load from a volatile memory
240   /// location.
241   ///
242   bool isVolatile() const { return Volatile; }
243
244   /// setVolatile - Specify whether this is a volatile load or not.
245   ///
246   void setVolatile(bool V) { Volatile = V; }
247
248   virtual Instruction *clone() const { return new StoreInst(*this); }
249
250   virtual bool mayWriteToMemory() const { return true; }
251
252   Value *getPointerOperand() { return getOperand(1); }
253   const Value *getPointerOperand() const { return getOperand(1); }
254   static unsigned getPointerOperandIndex() { return 1U; }
255
256   // Methods for support type inquiry through isa, cast, and dyn_cast:
257   static inline bool classof(const StoreInst *) { return true; }
258   static inline bool classof(const Instruction *I) {
259     return I->getOpcode() == Instruction::Store;
260   }
261   static inline bool classof(const Value *V) {
262     return isa<Instruction>(V) && classof(cast<Instruction>(V));
263   }
264 };
265
266
267 //===----------------------------------------------------------------------===//
268 //                             GetElementPtrInst Class
269 //===----------------------------------------------------------------------===//
270
271 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
272 /// access elements of arrays and structs
273 ///
274 class GetElementPtrInst : public Instruction {
275   GetElementPtrInst(const GetElementPtrInst &EPI)
276     : Instruction((static_cast<const Instruction*>(&EPI)->getType()),
277                   GetElementPtr) {
278     Operands.reserve(EPI.Operands.size());
279     for (unsigned i = 0, E = EPI.Operands.size(); i != E; ++i)
280       Operands.push_back(Use(EPI.Operands[i], this));
281   }
282   void init(Value *Ptr, const std::vector<Value*> &Idx);
283   void init(Value *Ptr, Value *Idx0, Value *Idx1);
284 public:
285   /// Constructors - Create a getelementptr instruction with a base pointer an
286   /// list of indices.  The first ctor can optionally insert before an existing
287   /// instruction, the second appends the new instruction to the specified
288   /// BasicBlock.
289   GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
290                     const std::string &Name = "", Instruction *InsertBefore =0);
291   GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
292                     const std::string &Name, BasicBlock *InsertAtEnd);
293
294   /// Constructors - These two constructors are convenience methods because two
295   /// index getelementptr instructions are so common.
296   GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
297                     const std::string &Name = "", Instruction *InsertBefore =0);
298   GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
299                     const std::string &Name, BasicBlock *InsertAtEnd);
300
301   virtual Instruction *clone() const { return new GetElementPtrInst(*this); }
302   
303   // getType - Overload to return most specific pointer type...
304   inline const PointerType *getType() const {
305     return reinterpret_cast<const PointerType*>(Instruction::getType());
306   }
307
308   /// getIndexedType - Returns the type of the element that would be loaded with
309   /// a load instruction with the specified parameters.
310   ///
311   /// A null type is returned if the indices are invalid for the specified 
312   /// pointer type.
313   ///
314   static const Type *getIndexedType(const Type *Ptr, 
315                                     const std::vector<Value*> &Indices,
316                                     bool AllowStructLeaf = false);
317   static const Type *getIndexedType(const Type *Ptr, Value *Idx0, Value *Idx1,
318                                     bool AllowStructLeaf = false);
319   
320   inline op_iterator       idx_begin()       { return op_begin()+1; }
321   inline const_op_iterator idx_begin() const { return op_begin()+1; }
322   inline op_iterator       idx_end()         { return op_end(); }
323   inline const_op_iterator idx_end()   const { return op_end(); }
324
325   Value *getPointerOperand() {
326     return getOperand(0);
327   }
328   const Value *getPointerOperand() const {
329     return getOperand(0);
330   }
331   static unsigned getPointerOperandIndex() {
332     return 0U;                      // get index for modifying correct operand
333   }
334
335   inline unsigned getNumIndices() const {  // Note: always non-negative
336     return getNumOperands() - 1;
337   }
338   
339   inline bool hasIndices() const {
340     return getNumOperands() > 1;
341   }
342
343   // Methods for support type inquiry through isa, cast, and dyn_cast:
344   static inline bool classof(const GetElementPtrInst *) { return true; }
345   static inline bool classof(const Instruction *I) {
346     return (I->getOpcode() == Instruction::GetElementPtr);
347   }
348   static inline bool classof(const Value *V) {
349     return isa<Instruction>(V) && classof(cast<Instruction>(V));
350   }
351 };
352
353 } // End llvm namespace
354
355 #endif // LLVM_IMEMORY_H