1 //===-- llvm/iMemory.h - Memory Operator node definitions --------*- C++ -*--=//
3 // This file contains the declarations of all of the memory related operators.
4 // This includes: malloc, free, alloca, load, store, getfield, putfield
6 //===----------------------------------------------------------------------===//
11 #include "llvm/Instruction.h"
12 #include "llvm/DerivedTypes.h"
14 //===----------------------------------------------------------------------===//
15 // AllocationInst Class
16 //===----------------------------------------------------------------------===//
18 // AllocationInst - This class is the common base class of MallocInst and
21 class AllocationInst : public Instruction {
23 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
24 const string &Name = "")
25 : Instruction(Ty, iTy, Name) {
26 assert(Ty->isPointerType() && "Can't allocate a non pointer type!");
29 assert(ArraySize->getType() == Type::UIntTy &&
30 "Malloc/Allocation array size != UIntTy!");
33 Operands.push_back(Use(ArraySize, this));
37 // isArrayAllocation - Return true if there is an allocation size parameter
38 // to the allocation instruction.
40 inline bool isArrayAllocation() const { return Operands.size() == 1; }
42 inline const Value *getArraySize() const {
43 assert(isArrayAllocation()); return Operands[0];
45 inline Value *getArraySize() {
46 assert(isArrayAllocation()); return Operands[0];
49 // getType - Overload to return most specific pointer type...
50 inline const PointerType *getType() const {
51 return (const PointerType*)Instruction::getType();
54 // getAllocatedType - Return the type that is being allocated by the
56 inline const Type *getAllocatedType() const {
57 return getType()->getElementType();
60 virtual Instruction *clone() const = 0;
64 //===----------------------------------------------------------------------===//
66 //===----------------------------------------------------------------------===//
68 class MallocInst : public AllocationInst {
70 MallocInst(const Type *Ty, Value *ArraySize = 0, const string &Name = "")
71 : AllocationInst(Ty, ArraySize, Malloc, Name) {}
73 virtual Instruction *clone() const {
74 return new MallocInst(getType(),
75 Operands.size() ? (Value*)Operands[0].get() : 0);
78 virtual const char *getOpcodeName() const { return "malloc"; }
80 // Methods for support type inquiry through isa, cast, and dyn_cast:
81 static inline bool classof(const MallocInst *) { return true; }
82 static inline bool classof(const Instruction *I) {
83 return (I->getOpcode() == Instruction::Malloc);
85 static inline bool classof(const Value *V) {
86 return isa<Instruction>(V) && classof(cast<Instruction>(V));
91 //===----------------------------------------------------------------------===//
93 //===----------------------------------------------------------------------===//
95 class AllocaInst : public AllocationInst {
97 AllocaInst(const Type *Ty, Value *ArraySize = 0, const string &Name = "")
98 : AllocationInst(Ty, ArraySize, Alloca, Name) {}
100 virtual Instruction *clone() const {
101 return new AllocaInst(getType(),
102 Operands.size() ? (Value*)Operands[0].get() : 0);
105 virtual const char *getOpcodeName() const { return "alloca"; }
107 // Methods for support type inquiry through isa, cast, and dyn_cast:
108 static inline bool classof(const AllocaInst *) { return true; }
109 static inline bool classof(const Instruction *I) {
110 return (I->getOpcode() == Instruction::Alloca);
112 static inline bool classof(const Value *V) {
113 return isa<Instruction>(V) && classof(cast<Instruction>(V));
118 //===----------------------------------------------------------------------===//
120 //===----------------------------------------------------------------------===//
122 class FreeInst : public Instruction {
124 FreeInst(Value *Ptr) : Instruction(Type::VoidTy, Free, "") {
125 assert(Ptr->getType()->isPointerType() && "Can't free nonpointer!");
127 Operands.push_back(Use(Ptr, this));
130 virtual Instruction *clone() const { return new FreeInst(Operands[0]); }
132 virtual const char *getOpcodeName() const { return "free"; }
134 virtual bool hasSideEffects() const { return true; }
136 // Methods for support type inquiry through isa, cast, and dyn_cast:
137 static inline bool classof(const FreeInst *) { return true; }
138 static inline bool classof(const Instruction *I) {
139 return (I->getOpcode() == Instruction::Free);
141 static inline bool classof(const Value *V) {
142 return isa<Instruction>(V) && classof(cast<Instruction>(V));
147 //===----------------------------------------------------------------------===//
148 // MemAccessInst Class
149 //===----------------------------------------------------------------------===//
151 // MemAccessInst - Common base class of LoadInst, StoreInst, and
152 // GetElementPtrInst...
154 class MemAccessInst : public Instruction {
156 inline MemAccessInst(const Type *Ty, unsigned Opcode,
157 const string &Nam = "")
158 : Instruction(Ty, Opcode, Nam) {}
160 // getIndexedType - Returns the type of the element that would be loaded with
161 // a load instruction with the specified parameters.
163 // A null type is returned if the indices are invalid for the specified
166 static const Type *getIndexedType(const Type *Ptr,
167 const vector<Value*> &Indices,
168 bool AllowStructLeaf = false);
170 const vector<Constant*> getIndicesBROKEN() const;
173 inline op_iterator idx_begin() {
174 return op_begin()+getFirstIndexOperandNumber();
176 inline const_op_iterator idx_begin() const {
177 return op_begin()+getFirstIndexOperandNumber();
179 inline op_iterator idx_end() { return op_end(); }
180 inline const_op_iterator idx_end() const { return op_end(); }
183 vector<Value*> copyIndices() const {
184 return vector<Value*>(idx_begin(), idx_end());
187 Value *getPointerOperand() {
188 return getOperand(getFirstIndexOperandNumber()-1);
190 const Value *getPointerOperand() const {
191 return getOperand(getFirstIndexOperandNumber()-1);
194 virtual unsigned getFirstIndexOperandNumber() const = 0;
196 inline bool hasIndices() const {
197 return getNumOperands() > getFirstIndexOperandNumber();
200 // Methods for support type inquiry through isa, cast, and dyn_cast:
201 static inline bool classof(const MemAccessInst *) { return true; }
202 static inline bool classof(const Instruction *I) {
203 return I->getOpcode() == Load || I->getOpcode() == Store ||
204 I->getOpcode() == GetElementPtr;
206 static inline bool classof(const Value *V) {
207 return isa<Instruction>(V) && classof(cast<Instruction>(V));
212 //===----------------------------------------------------------------------===//
214 //===----------------------------------------------------------------------===//
216 class LoadInst : public MemAccessInst {
217 LoadInst(const LoadInst &LI) : MemAccessInst(LI.getType(), Load) {
218 Operands.reserve(LI.Operands.size());
219 for (unsigned i = 0, E = LI.Operands.size(); i != E; ++i)
220 Operands.push_back(Use(LI.Operands[i], this));
223 LoadInst(Value *Ptr, const vector<Value*> &Idx, const string &Name = "");
224 LoadInst(Value *Ptr, const string &Name = "");
226 virtual Instruction *clone() const { return new LoadInst(*this); }
227 virtual const char *getOpcodeName() const { return "load"; }
229 virtual unsigned getFirstIndexOperandNumber() const { return 1; }
231 // Methods for support type inquiry through isa, cast, and dyn_cast:
232 static inline bool classof(const LoadInst *) { return true; }
233 static inline bool classof(const Instruction *I) {
234 return (I->getOpcode() == Instruction::Load);
236 static inline bool classof(const Value *V) {
237 return isa<Instruction>(V) && classof(cast<Instruction>(V));
242 //===----------------------------------------------------------------------===//
244 //===----------------------------------------------------------------------===//
246 class StoreInst : public MemAccessInst {
247 StoreInst(const StoreInst &SI) : MemAccessInst(SI.getType(), Store) {
248 Operands.reserve(SI.Operands.size());
249 for (unsigned i = 0, E = SI.Operands.size(); i != E; ++i)
250 Operands.push_back(Use(SI.Operands[i], this));
253 StoreInst(Value *Val, Value *Ptr, const vector<Value*> &Idx,
254 const string &Name = "");
255 StoreInst(Value *Val, Value *Ptr, const string &Name = "");
256 virtual Instruction *clone() const { return new StoreInst(*this); }
258 virtual const char *getOpcodeName() const { return "store"; }
260 virtual bool hasSideEffects() const { return true; }
261 virtual unsigned getFirstIndexOperandNumber() const { return 2; }
263 // Methods for support type inquiry through isa, cast, and dyn_cast:
264 static inline bool classof(const StoreInst *) { return true; }
265 static inline bool classof(const Instruction *I) {
266 return (I->getOpcode() == Instruction::Store);
268 static inline bool classof(const Value *V) {
269 return isa<Instruction>(V) && classof(cast<Instruction>(V));
274 //===----------------------------------------------------------------------===//
275 // GetElementPtrInst Class
276 //===----------------------------------------------------------------------===//
278 class GetElementPtrInst : public MemAccessInst {
279 GetElementPtrInst(const GetElementPtrInst &EPI)
280 : MemAccessInst(EPI.getType(), GetElementPtr) {
281 Operands.reserve(EPI.Operands.size());
282 for (unsigned i = 0, E = EPI.Operands.size(); i != E; ++i)
283 Operands.push_back(Use(EPI.Operands[i], this));
286 GetElementPtrInst(Value *Ptr, const vector<Value*> &Idx,
287 const string &Name = "");
288 virtual Instruction *clone() const { return new GetElementPtrInst(*this); }
289 virtual const char *getOpcodeName() const { return "getelementptr"; }
290 virtual unsigned getFirstIndexOperandNumber() const { return 1; }
292 inline bool isArraySelector() const { return !isStructSelector(); }
293 bool isStructSelector() const;
295 // getType - Overload to return most specific pointer type...
296 inline const PointerType *getType() const {
297 return cast<const PointerType>(Instruction::getType());
300 // Methods for support type inquiry through isa, cast, and dyn_cast:
301 static inline bool classof(const GetElementPtrInst *) { return true; }
302 static inline bool classof(const Instruction *I) {
303 return (I->getOpcode() == Instruction::GetElementPtr);
305 static inline bool classof(const Value *V) {
306 return isa<Instruction>(V) && classof(cast<Instruction>(V));
310 #endif // LLVM_IMEMORY_H