Add a new BasicBlockPass::doInitialization/Finalization(Function &) pair of
[oota-llvm.git] / include / llvm / iMemory.h
1 //===-- llvm/iMemory.h - Memory Operator node definitions --------*- C++ -*--=//
2 //
3 // This file contains the declarations of all of the memory related operators.
4 // This includes: malloc, free, alloca, load, store, getfield, putfield
5 //
6 //===----------------------------------------------------------------------===//
7
8 #ifndef LLVM_IMEMORY_H
9 #define LLVM_IMEMORY_H
10
11 #include "llvm/Instruction.h"
12 class PointerType;
13
14 //===----------------------------------------------------------------------===//
15 //                             AllocationInst Class
16 //===----------------------------------------------------------------------===//
17 //
18 // AllocationInst - This class is the common base class of MallocInst and
19 // AllocaInst.
20 //
21 class AllocationInst : public Instruction {
22 protected:
23   AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, 
24                  const std::string &Name = "", Instruction *InsertBefore = 0);
25 public:
26
27   // isArrayAllocation - Return true if there is an allocation size parameter
28   // to the allocation instruction that is not 1.
29   //
30   bool isArrayAllocation() const;
31
32   // getArraySize - Get the number of element allocated, for a simple allocation
33   // of a single element, this will return a constant 1 value.
34   //
35   inline const Value *getArraySize() const { return Operands[0]; }
36   inline Value *getArraySize() { return Operands[0]; }
37
38   // getType - Overload to return most specific pointer type...
39   inline const PointerType *getType() const {
40     return (const PointerType*)Instruction::getType(); 
41   }
42
43   // getAllocatedType - Return the type that is being allocated by the
44   // instruction.
45   //
46   const Type *getAllocatedType() const;
47
48   virtual Instruction *clone() const = 0;
49
50   // Methods for support type inquiry through isa, cast, and dyn_cast:
51   static inline bool classof(const AllocationInst *) { return true; }
52   static inline bool classof(const Instruction *I) {
53     return I->getOpcode() == Instruction::Alloca ||
54            I->getOpcode() == Instruction::Malloc;
55   }
56   static inline bool classof(const Value *V) {
57     return isa<Instruction>(V) && classof(cast<Instruction>(V));
58   }
59 };
60
61
62 //===----------------------------------------------------------------------===//
63 //                                MallocInst Class
64 //===----------------------------------------------------------------------===//
65
66 struct MallocInst : public AllocationInst {
67   MallocInst(const Type *Ty, Value *ArraySize = 0, const std::string &Name = "",
68              Instruction *InsertBefore = 0)
69     : AllocationInst(Ty, ArraySize, Malloc, Name, InsertBefore) {}
70
71   virtual Instruction *clone() const { 
72     return new MallocInst((Type*)getType(), (Value*)Operands[0].get());
73   }
74
75   // Methods for support type inquiry through isa, cast, and dyn_cast:
76   static inline bool classof(const MallocInst *) { return true; }
77   static inline bool classof(const Instruction *I) {
78     return (I->getOpcode() == Instruction::Malloc);
79   }
80   static inline bool classof(const Value *V) {
81     return isa<Instruction>(V) && classof(cast<Instruction>(V));
82   }
83 };
84
85
86 //===----------------------------------------------------------------------===//
87 //                                AllocaInst Class
88 //===----------------------------------------------------------------------===//
89
90 struct AllocaInst : public AllocationInst {
91   AllocaInst(const Type *Ty, Value *ArraySize = 0, const std::string &Name = "",
92              Instruction *InsertBefore = 0)
93     : AllocationInst(Ty, ArraySize, Alloca, Name, InsertBefore) {}
94
95   virtual Instruction *clone() const { 
96     return new AllocaInst((Type*)getType(), (Value*)Operands[0].get());
97   }
98
99   // Methods for support type inquiry through isa, cast, and dyn_cast:
100   static inline bool classof(const AllocaInst *) { return true; }
101   static inline bool classof(const Instruction *I) {
102     return (I->getOpcode() == Instruction::Alloca);
103   }
104   static inline bool classof(const Value *V) {
105     return isa<Instruction>(V) && classof(cast<Instruction>(V));
106   }
107 };
108
109
110 //===----------------------------------------------------------------------===//
111 //                                 FreeInst Class
112 //===----------------------------------------------------------------------===//
113
114 struct FreeInst : public Instruction {
115   FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
116
117   virtual Instruction *clone() const { return new FreeInst(Operands[0]); }
118
119   virtual bool hasSideEffects() const { return true; }
120
121   // Methods for support type inquiry through isa, cast, and dyn_cast:
122   static inline bool classof(const FreeInst *) { return true; }
123   static inline bool classof(const Instruction *I) {
124     return (I->getOpcode() == Instruction::Free);
125   }
126   static inline bool classof(const Value *V) {
127     return isa<Instruction>(V) && classof(cast<Instruction>(V));
128   }
129 };
130
131
132 //===----------------------------------------------------------------------===//
133 //                                LoadInst Class
134 //===----------------------------------------------------------------------===//
135
136 class LoadInst : public Instruction {
137   LoadInst(const LoadInst &LI) : Instruction(LI.getType(), Load) {
138     Operands.reserve(1);
139     Operands.push_back(Use(LI.Operands[0], this));
140   }
141 public:
142   LoadInst(Value *Ptr, const std::string &Name = "",
143            Instruction *InsertBefore = 0);
144
145   virtual Instruction *clone() const { return new LoadInst(*this); }
146
147   Value *getPointerOperand() { return getOperand(0); }
148   const Value *getPointerOperand() const { return getOperand(0); }
149
150   // Methods for support type inquiry through isa, cast, and dyn_cast:
151   static inline bool classof(const LoadInst *) { return true; }
152   static inline bool classof(const Instruction *I) {
153     return (I->getOpcode() == Instruction::Load);
154   }
155   static inline bool classof(const Value *V) {
156     return isa<Instruction>(V) && classof(cast<Instruction>(V));
157   }
158 };
159
160
161 //===----------------------------------------------------------------------===//
162 //                                StoreInst Class
163 //===----------------------------------------------------------------------===//
164
165 class StoreInst : public Instruction {
166   StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store) {
167     Operands.reserve(2);
168     Operands.push_back(Use(SI.Operands[0], this));
169     Operands.push_back(Use(SI.Operands[1], this));
170   }
171 public:
172   StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore = 0);
173   virtual Instruction *clone() const { return new StoreInst(*this); }
174
175   virtual bool hasSideEffects() const { return true; }
176
177   Value *getPointerOperand() { return getOperand(1); }
178   const Value *getPointerOperand() const { return getOperand(1); }
179
180   // Methods for support type inquiry through isa, cast, and dyn_cast:
181   static inline bool classof(const StoreInst *) { return true; }
182   static inline bool classof(const Instruction *I) {
183     return (I->getOpcode() == Instruction::Store);
184   }
185   static inline bool classof(const Value *V) {
186     return isa<Instruction>(V) && classof(cast<Instruction>(V));
187   }
188 };
189
190
191 //===----------------------------------------------------------------------===//
192 //                             GetElementPtrInst Class
193 //===----------------------------------------------------------------------===//
194
195 class GetElementPtrInst : public Instruction {
196   GetElementPtrInst(const GetElementPtrInst &EPI)
197     : Instruction((Type*)EPI.getType(), GetElementPtr) {
198     Operands.reserve(EPI.Operands.size());
199     for (unsigned i = 0, E = EPI.Operands.size(); i != E; ++i)
200       Operands.push_back(Use(EPI.Operands[i], this));
201   }
202 public:
203   GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
204                     const std::string &Name = "", Instruction *InsertBefore =0);
205   virtual Instruction *clone() const { return new GetElementPtrInst(*this); }
206   
207   // getType - Overload to return most specific pointer type...
208   inline const PointerType *getType() const {
209     return (PointerType*)Instruction::getType();
210   }
211
212   /// getIndexedType - Returns the type of the element that would be loaded with
213   /// a load instruction with the specified parameters.
214   ///
215   /// A null type is returned if the indices are invalid for the specified 
216   /// pointer type.
217   ///
218   static const Type *getIndexedType(const Type *Ptr, 
219                                     const std::vector<Value*> &Indices,
220                                     bool AllowStructLeaf = false);
221   
222   inline op_iterator       idx_begin()       {
223     return op_begin()+1;
224   }
225   inline const_op_iterator idx_begin() const {
226     return op_begin()+1;
227   }
228   inline op_iterator       idx_end()         { return op_end(); }
229   inline const_op_iterator idx_end()   const { return op_end(); }
230
231   Value *getPointerOperand() {
232     return getOperand(0);
233   }
234   const Value *getPointerOperand() const {
235     return getOperand(0);
236   }
237   
238   inline unsigned getNumIndices() const {  // Note: always non-negative
239     return getNumOperands() - 1;
240   }
241   
242   inline bool hasIndices() const {
243     return getNumOperands() > 1;
244   }
245
246   // Methods for support type inquiry through isa, cast, and dyn_cast:
247   static inline bool classof(const GetElementPtrInst *) { return true; }
248   static inline bool classof(const Instruction *I) {
249     return (I->getOpcode() == Instruction::GetElementPtr);
250   }
251   static inline bool classof(const Value *V) {
252     return isa<Instruction>(V) && classof(cast<Instruction>(V));
253   }
254 };
255
256 #endif // LLVM_IMEMORY_H