An "autoconf wrapper" for the infamous windows.h file
[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     Operands.reserve(1);
179     Operands.push_back(Use(LI.Operands[0], this));
180   }
181   bool Volatile;   // True if this is a volatile load
182   void init(Value *Ptr) {
183     Operands.reserve(1);
184     Operands.push_back(Use(Ptr, this));
185   }
186 public:
187   LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBefore);
188   LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAtEnd);
189   LoadInst(Value *Ptr, const std::string &Name = "", bool isVolatile = false,
190            Instruction *InsertBefore = 0);
191   LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
192            BasicBlock *InsertAtEnd);
193
194   /// isVolatile - Return true if this is a load from a volatile memory
195   /// location.
196   ///
197   bool isVolatile() const { return Volatile; }
198
199   /// setVolatile - Specify whether this is a volatile load or not.
200   ///
201   void setVolatile(bool V) { Volatile = V; }
202
203   virtual Instruction *clone() const { return new LoadInst(*this); }
204
205   virtual bool mayWriteToMemory() const { return isVolatile(); }
206
207   Value *getPointerOperand() { return getOperand(0); }
208   const Value *getPointerOperand() const { return getOperand(0); }
209   static unsigned getPointerOperandIndex() { return 0U; }
210
211   // Methods for support type inquiry through isa, cast, and dyn_cast:
212   static inline bool classof(const LoadInst *) { return true; }
213   static inline bool classof(const Instruction *I) {
214     return I->getOpcode() == Instruction::Load;
215   }
216   static inline bool classof(const Value *V) {
217     return isa<Instruction>(V) && classof(cast<Instruction>(V));
218   }
219 };
220
221
222 //===----------------------------------------------------------------------===//
223 //                                StoreInst Class
224 //===----------------------------------------------------------------------===//
225
226 /// StoreInst - an instruction for storing to memory 
227 ///
228 class StoreInst : public Instruction {
229   StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store) {
230     Volatile = SI.isVolatile();
231     Operands.reserve(2);
232     Operands.push_back(Use(SI.Operands[0], this));
233     Operands.push_back(Use(SI.Operands[1], this));
234   }
235   bool Volatile;   // True if this is a volatile store
236   void init(Value *Val, Value *Ptr) {
237     Operands.reserve(2);
238     Operands.push_back(Use(Val, this));
239     Operands.push_back(Use(Ptr, this));
240   }
241 public:
242   StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
243   StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
244   StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
245             Instruction *InsertBefore = 0);
246   StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
247
248
249   /// isVolatile - Return true if this is a load from a volatile memory
250   /// location.
251   ///
252   bool isVolatile() const { return Volatile; }
253
254   /// setVolatile - Specify whether this is a volatile load or not.
255   ///
256   void setVolatile(bool V) { Volatile = V; }
257
258   virtual Instruction *clone() const { return new StoreInst(*this); }
259
260   virtual bool mayWriteToMemory() const { return true; }
261
262   Value *getPointerOperand() { return getOperand(1); }
263   const Value *getPointerOperand() const { return getOperand(1); }
264   static unsigned getPointerOperandIndex() { return 1U; }
265
266   // Methods for support type inquiry through isa, cast, and dyn_cast:
267   static inline bool classof(const StoreInst *) { return true; }
268   static inline bool classof(const Instruction *I) {
269     return I->getOpcode() == Instruction::Store;
270   }
271   static inline bool classof(const Value *V) {
272     return isa<Instruction>(V) && classof(cast<Instruction>(V));
273   }
274 };
275
276
277 //===----------------------------------------------------------------------===//
278 //                             GetElementPtrInst Class
279 //===----------------------------------------------------------------------===//
280
281 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
282 /// access elements of arrays and structs
283 ///
284 class GetElementPtrInst : public Instruction {
285   GetElementPtrInst(const GetElementPtrInst &EPI)
286     : Instruction(reinterpret_cast<const Type*>(EPI.getType()), GetElementPtr) {
287     Operands.reserve(EPI.Operands.size());
288     for (unsigned i = 0, E = EPI.Operands.size(); i != E; ++i)
289       Operands.push_back(Use(EPI.Operands[i], this));
290   }
291   void init(Value *Ptr, const std::vector<Value*> &Idx);
292
293 public:
294   GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
295                     const std::string &Name = "", Instruction *InsertBefore =0);
296   GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
297                     const std::string &Name, BasicBlock *InsertAtEnd);
298   virtual Instruction *clone() const { return new GetElementPtrInst(*this); }
299   
300   // getType - Overload to return most specific pointer type...
301   inline const PointerType *getType() const {
302     return reinterpret_cast<const PointerType*>(Instruction::getType());
303   }
304
305   /// getIndexedType - Returns the type of the element that would be loaded with
306   /// a load instruction with the specified parameters.
307   ///
308   /// A null type is returned if the indices are invalid for the specified 
309   /// pointer type.
310   ///
311   static const Type *getIndexedType(const Type *Ptr, 
312                                     const std::vector<Value*> &Indices,
313                                     bool AllowStructLeaf = false);
314   
315   inline op_iterator       idx_begin()       { return op_begin()+1; }
316   inline const_op_iterator idx_begin() const { return op_begin()+1; }
317   inline op_iterator       idx_end()         { return op_end(); }
318   inline const_op_iterator idx_end()   const { return op_end(); }
319
320   Value *getPointerOperand() {
321     return getOperand(0);
322   }
323   const Value *getPointerOperand() const {
324     return getOperand(0);
325   }
326   static unsigned getPointerOperandIndex() {
327     return 0U;                      // get index for modifying correct operand
328   }
329
330   inline unsigned getNumIndices() const {  // Note: always non-negative
331     return getNumOperands() - 1;
332   }
333   
334   inline bool hasIndices() const {
335     return getNumOperands() > 1;
336   }
337
338   // Methods for support type inquiry through isa, cast, and dyn_cast:
339   static inline bool classof(const GetElementPtrInst *) { return true; }
340   static inline bool classof(const Instruction *I) {
341     return (I->getOpcode() == Instruction::GetElementPtr);
342   }
343   static inline bool classof(const Value *V) {
344     return isa<Instruction>(V) && classof(cast<Instruction>(V));
345   }
346 };
347
348 } // End llvm namespace
349
350 #endif // LLVM_IMEMORY_H