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 // Make sure they didn't try to specify a size for !(unsized array) type
30 assert(getType()->getValueType()->isArrayType() &&
31 cast<ArrayType>(getType()->getValueType())->isUnsized() &&
32 "Trying to allocate something other than unsized array, with size!");
35 Operands.push_back(Use(ArraySize, this));
37 // Make sure that the pointer is not to an unsized array!
38 assert(!getType()->getValueType()->isArrayType() ||
39 cast<const ArrayType>(getType()->getValueType())->isSized() &&
40 "Trying to allocate unsized array without size!");
44 // getType - Overload to return most specific pointer type...
45 inline const PointerType *getType() const {
46 return (const PointerType*)Instruction::getType();
49 virtual Instruction *clone() const = 0;
53 //===----------------------------------------------------------------------===//
55 //===----------------------------------------------------------------------===//
57 class MallocInst : public AllocationInst {
59 MallocInst(const Type *Ty, Value *ArraySize = 0, const string &Name = "")
60 : AllocationInst(Ty, ArraySize, Malloc, Name) {}
62 virtual Instruction *clone() const {
63 return new MallocInst(getType(),
64 Operands.size() ? (Value*)Operands[1].get() : 0);
67 virtual const char *getOpcodeName() const { return "malloc"; }
69 // Methods for support type inquiry through isa, cast, and dyn_cast:
70 static inline bool classof(const MallocInst *) { return true; }
71 static inline bool classof(const Instruction *I) {
72 return (I->getOpcode() == Instruction::Malloc);
74 static inline bool classof(const Value *V) {
75 return isa<Instruction>(V) && classof(cast<Instruction>(V));
80 //===----------------------------------------------------------------------===//
82 //===----------------------------------------------------------------------===//
84 class AllocaInst : public AllocationInst {
86 AllocaInst(const Type *Ty, Value *ArraySize = 0, const string &Name = "")
87 : AllocationInst(Ty, ArraySize, Alloca, Name) {}
89 virtual Instruction *clone() const {
90 return new AllocaInst(getType(),
91 Operands.size() ? (Value*)Operands[1].get() : 0);
94 virtual const char *getOpcodeName() const { return "alloca"; }
96 // Methods for support type inquiry through isa, cast, and dyn_cast:
97 static inline bool classof(const AllocaInst *) { return true; }
98 static inline bool classof(const Instruction *I) {
99 return (I->getOpcode() == Instruction::Alloca);
101 static inline bool classof(const Value *V) {
102 return isa<Instruction>(V) && classof(cast<Instruction>(V));
107 //===----------------------------------------------------------------------===//
109 //===----------------------------------------------------------------------===//
111 class FreeInst : public Instruction {
113 FreeInst(Value *Ptr, const string &Name = "")
114 : Instruction(Type::VoidTy, Free, Name) {
115 assert(Ptr->getType()->isPointerType() && "Can't free nonpointer!");
117 Operands.push_back(Use(Ptr, this));
120 virtual Instruction *clone() const { return new FreeInst(Operands[0]); }
122 virtual const char *getOpcodeName() const { return "free"; }
124 virtual bool hasSideEffects() const { return true; }
126 // Methods for support type inquiry through isa, cast, and dyn_cast:
127 static inline bool classof(const FreeInst *) { return true; }
128 static inline bool classof(const Instruction *I) {
129 return (I->getOpcode() == Instruction::Free);
131 static inline bool classof(const Value *V) {
132 return isa<Instruction>(V) && classof(cast<Instruction>(V));
137 //===----------------------------------------------------------------------===//
138 // MemAccessInst Class
139 //===----------------------------------------------------------------------===//
141 // MemAccessInst - Common base class of LoadInst, StoreInst, and
142 // GetElementPtrInst...
144 class MemAccessInst : public Instruction {
146 inline MemAccessInst(const Type *Ty, unsigned Opcode,
147 const vector<ConstPoolVal*> &Idx,
148 const string &Nam = "")
149 : Instruction(Ty, Opcode, Nam),
154 vector<ConstPoolVal*> indexVec;
157 // getIndexedType - Returns the type of the element that would be loaded with
158 // a load instruction with the specified parameters.
160 // A null type is returned if the indices are invalid for the specified
163 static const Type *getIndexedType(const Type *Ptr,
164 const vector<ConstPoolVal*> &Indices,
165 bool AllowStructLeaf = false);
167 const vector<ConstPoolVal*>& getIndexVec() const { return indexVec; }
169 virtual Value* getPtrOperand() = 0;
171 virtual int getFirstOffsetIdx() const = 0;
175 //===----------------------------------------------------------------------===//
177 //===----------------------------------------------------------------------===//
179 class LoadInst : public MemAccessInst {
180 LoadInst(const LoadInst &LI) : MemAccessInst(LI.getType(), Load, LI.getIndexVec()) {
181 Operands.reserve(LI.Operands.size());
182 for (unsigned i = 0, E = LI.Operands.size(); i != E; ++i)
183 Operands.push_back(Use(LI.Operands[i], this));
186 LoadInst(Value *Ptr, const vector<ConstPoolVal*> &Idx,
187 const string &Name = "");
188 virtual Instruction* clone() const { return new LoadInst(*this); }
189 virtual const char* getOpcodeName() const { return "load"; }
190 virtual Value* getPtrOperand() { return this->getOperand(0); }
191 virtual int getFirstOffsetIdx() const { return (this->getNumOperands() > 1)? 1 : -1;}
193 // Methods for support type inquiry through isa, cast, and dyn_cast:
194 static inline bool classof(const LoadInst *) { return true; }
195 static inline bool classof(const Instruction *I) {
196 return (I->getOpcode() == Instruction::Load);
198 static inline bool classof(const Value *V) {
199 return isa<Instruction>(V) && classof(cast<Instruction>(V));
204 //===----------------------------------------------------------------------===//
206 //===----------------------------------------------------------------------===//
208 class StoreInst : public MemAccessInst {
209 StoreInst(const StoreInst &SI) : MemAccessInst(SI.getType(), Store, SI.getIndexVec()) {
210 Operands.reserve(SI.Operands.size());
211 for (unsigned i = 0, E = SI.Operands.size(); i != E; ++i)
212 Operands.push_back(Use(SI.Operands[i], this));
215 StoreInst(Value *Val, Value *Ptr, const vector<ConstPoolVal*> &Idx,
216 const string &Name = "");
217 virtual Instruction *clone() const { return new StoreInst(*this); }
218 virtual const char *getOpcodeName() const { return "store"; }
220 virtual bool hasSideEffects() const { return true; }
221 virtual Value* getPtrOperand() { return this->getOperand(1); }
222 virtual int getFirstOffsetIdx() const { return (this->getNumOperands() > 2)? 2 : -1;}
224 // Methods for support type inquiry through isa, cast, and dyn_cast:
225 static inline bool classof(const StoreInst *) { return true; }
226 static inline bool classof(const Instruction *I) {
227 return (I->getOpcode() == Instruction::Store);
229 static inline bool classof(const Value *V) {
230 return isa<Instruction>(V) && classof(cast<Instruction>(V));
235 //===----------------------------------------------------------------------===//
236 // GetElementPtrInst Class
237 //===----------------------------------------------------------------------===//
239 class GetElementPtrInst : public MemAccessInst {
240 GetElementPtrInst(const GetElementPtrInst &EPI)
241 : MemAccessInst(EPI.getType(), GetElementPtr, EPI.getIndexVec()) {
242 Operands.reserve(EPI.Operands.size());
243 for (unsigned i = 0, E = EPI.Operands.size(); i != E; ++i)
244 Operands.push_back(Use(EPI.Operands[i], this));
247 GetElementPtrInst(Value *Ptr, const vector<ConstPoolVal*> &Idx,
248 const string &Name = "");
249 virtual Instruction *clone() const { return new GetElementPtrInst(*this); }
250 virtual const char *getOpcodeName() const { return "getelementptr"; }
251 virtual Value* getPtrOperand() { return this->getOperand(0); }
252 virtual int getFirstOffsetIdx() const { return (this->getNumOperands() > 1)? 1 : -1;}
254 inline bool isArraySelector() const { return !isStructSelector(); }
255 bool isStructSelector() const;
258 // Methods for support type inquiry through isa, cast, and dyn_cast:
259 static inline bool classof(const GetElementPtrInst *) { return true; }
260 static inline bool classof(const Instruction *I) {
261 return (I->getOpcode() == Instruction::GetElementPtr);
263 static inline bool classof(const Value *V) {
264 return isa<Instruction>(V) && classof(cast<Instruction>(V));
268 #endif // LLVM_IMEMORY_H