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 std::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 that is not 1.
40 bool isArrayAllocation() const;
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;
62 // Methods for support type inquiry through isa, cast, and dyn_cast:
63 static inline bool classof(const AllocationInst *) { return true; }
64 static inline bool classof(const Instruction *I) {
65 return I->getOpcode() == Instruction::Alloca ||
66 I->getOpcode() == Instruction::Malloc;
68 static inline bool classof(const Value *V) {
69 return isa<Instruction>(V) && classof(cast<Instruction>(V));
74 //===----------------------------------------------------------------------===//
76 //===----------------------------------------------------------------------===//
78 class MallocInst : public AllocationInst {
80 MallocInst(const Type *Ty, Value *ArraySize = 0, const std::string &Name = "")
81 : AllocationInst(Ty, ArraySize, Malloc, Name) {}
83 virtual Instruction *clone() const {
84 return new MallocInst(getType(),
85 Operands.size() ? (Value*)Operands[0].get() : 0);
88 virtual const char *getOpcodeName() const { return "malloc"; }
90 // Methods for support type inquiry through isa, cast, and dyn_cast:
91 static inline bool classof(const MallocInst *) { return true; }
92 static inline bool classof(const Instruction *I) {
93 return (I->getOpcode() == Instruction::Malloc);
95 static inline bool classof(const Value *V) {
96 return isa<Instruction>(V) && classof(cast<Instruction>(V));
101 //===----------------------------------------------------------------------===//
103 //===----------------------------------------------------------------------===//
105 class AllocaInst : public AllocationInst {
107 AllocaInst(const Type *Ty, Value *ArraySize = 0, const std::string &Name = "")
108 : AllocationInst(Ty, ArraySize, Alloca, Name) {}
110 virtual Instruction *clone() const {
111 return new AllocaInst(getType(),
112 Operands.size() ? (Value*)Operands[0].get() : 0);
115 virtual const char *getOpcodeName() const { return "alloca"; }
117 // Methods for support type inquiry through isa, cast, and dyn_cast:
118 static inline bool classof(const AllocaInst *) { return true; }
119 static inline bool classof(const Instruction *I) {
120 return (I->getOpcode() == Instruction::Alloca);
122 static inline bool classof(const Value *V) {
123 return isa<Instruction>(V) && classof(cast<Instruction>(V));
128 //===----------------------------------------------------------------------===//
130 //===----------------------------------------------------------------------===//
132 class FreeInst : public Instruction {
134 FreeInst(Value *Ptr) : Instruction(Type::VoidTy, Free, "") {
135 assert(Ptr->getType()->isPointerType() && "Can't free nonpointer!");
137 Operands.push_back(Use(Ptr, this));
140 virtual Instruction *clone() const { return new FreeInst(Operands[0]); }
142 virtual const char *getOpcodeName() const { return "free"; }
144 virtual bool hasSideEffects() const { return true; }
146 // Methods for support type inquiry through isa, cast, and dyn_cast:
147 static inline bool classof(const FreeInst *) { return true; }
148 static inline bool classof(const Instruction *I) {
149 return (I->getOpcode() == Instruction::Free);
151 static inline bool classof(const Value *V) {
152 return isa<Instruction>(V) && classof(cast<Instruction>(V));
157 //===----------------------------------------------------------------------===//
158 // MemAccessInst Class
159 //===----------------------------------------------------------------------===//
161 // MemAccessInst - Common base class of LoadInst, StoreInst, and
162 // GetElementPtrInst...
164 class MemAccessInst : public Instruction {
166 inline MemAccessInst(const Type *Ty, unsigned Opcode,
167 const std::string &Nam = "")
168 : Instruction(Ty, Opcode, Nam) {}
170 // getIndexedType - Returns the type of the element that would be loaded with
171 // a load instruction with the specified parameters.
173 // A null type is returned if the indices are invalid for the specified
176 static const Type *getIndexedType(const Type *Ptr,
177 const std::vector<Value*> &Indices,
178 bool AllowStructLeaf = false);
180 inline op_iterator idx_begin() {
181 return op_begin()+getFirstIndexOperandNumber();
183 inline const_op_iterator idx_begin() const {
184 return op_begin()+getFirstIndexOperandNumber();
186 inline op_iterator idx_end() { return op_end(); }
187 inline const_op_iterator idx_end() const { return op_end(); }
190 std::vector<Value*> copyIndices() const {
191 return std::vector<Value*>(idx_begin(), idx_end());
194 Value *getPointerOperand() {
195 return getOperand(getFirstIndexOperandNumber()-1);
197 const Value *getPointerOperand() const {
198 return getOperand(getFirstIndexOperandNumber()-1);
201 virtual unsigned getFirstIndexOperandNumber() const = 0;
203 inline bool hasIndices() const {
204 return getNumOperands() > getFirstIndexOperandNumber();
207 // Methods for support type inquiry through isa, cast, and dyn_cast:
208 static inline bool classof(const MemAccessInst *) { return true; }
209 static inline bool classof(const Instruction *I) {
210 return I->getOpcode() == Load || I->getOpcode() == Store ||
211 I->getOpcode() == GetElementPtr;
213 static inline bool classof(const Value *V) {
214 return isa<Instruction>(V) && classof(cast<Instruction>(V));
219 //===----------------------------------------------------------------------===//
221 //===----------------------------------------------------------------------===//
223 class LoadInst : public MemAccessInst {
224 LoadInst(const LoadInst &LI) : MemAccessInst(LI.getType(), Load) {
225 Operands.reserve(LI.Operands.size());
226 for (unsigned i = 0, E = LI.Operands.size(); i != E; ++i)
227 Operands.push_back(Use(LI.Operands[i], this));
230 LoadInst(Value *Ptr, const std::vector<Value*> &Ix, const std::string & = "");
231 LoadInst(Value *Ptr, const std::string &Name = "");
233 virtual Instruction *clone() const { return new LoadInst(*this); }
234 virtual const char *getOpcodeName() const { return "load"; }
236 virtual unsigned getFirstIndexOperandNumber() const { return 1; }
238 // Methods for support type inquiry through isa, cast, and dyn_cast:
239 static inline bool classof(const LoadInst *) { return true; }
240 static inline bool classof(const Instruction *I) {
241 return (I->getOpcode() == Instruction::Load);
243 static inline bool classof(const Value *V) {
244 return isa<Instruction>(V) && classof(cast<Instruction>(V));
249 //===----------------------------------------------------------------------===//
251 //===----------------------------------------------------------------------===//
253 class StoreInst : public MemAccessInst {
254 StoreInst(const StoreInst &SI) : MemAccessInst(SI.getType(), Store) {
255 Operands.reserve(SI.Operands.size());
256 for (unsigned i = 0, E = SI.Operands.size(); i != E; ++i)
257 Operands.push_back(Use(SI.Operands[i], this));
260 StoreInst(Value *Val, Value *Ptr, const std::vector<Value*> &Idx,
261 const std::string &Name = "");
262 StoreInst(Value *Val, Value *Ptr, const std::string &Name = "");
263 virtual Instruction *clone() const { return new StoreInst(*this); }
265 virtual const char *getOpcodeName() const { return "store"; }
267 virtual bool hasSideEffects() const { return true; }
268 virtual unsigned getFirstIndexOperandNumber() const { return 2; }
270 // Methods for support type inquiry through isa, cast, and dyn_cast:
271 static inline bool classof(const StoreInst *) { return true; }
272 static inline bool classof(const Instruction *I) {
273 return (I->getOpcode() == Instruction::Store);
275 static inline bool classof(const Value *V) {
276 return isa<Instruction>(V) && classof(cast<Instruction>(V));
281 //===----------------------------------------------------------------------===//
282 // GetElementPtrInst Class
283 //===----------------------------------------------------------------------===//
285 class GetElementPtrInst : public MemAccessInst {
286 GetElementPtrInst(const GetElementPtrInst &EPI)
287 : MemAccessInst(EPI.getType(), GetElementPtr) {
288 Operands.reserve(EPI.Operands.size());
289 for (unsigned i = 0, E = EPI.Operands.size(); i != E; ++i)
290 Operands.push_back(Use(EPI.Operands[i], this));
293 GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
294 const std::string &Name = "");
295 virtual Instruction *clone() const { return new GetElementPtrInst(*this); }
296 virtual const char *getOpcodeName() const { return "getelementptr"; }
297 virtual unsigned getFirstIndexOperandNumber() const { return 1; }
299 inline bool isArraySelector() const { return !isStructSelector(); }
300 bool isStructSelector() const;
302 // getType - Overload to return most specific pointer type...
303 inline const PointerType *getType() const {
304 return cast<const PointerType>(Instruction::getType());
307 // Methods for support type inquiry through isa, cast, and dyn_cast:
308 static inline bool classof(const GetElementPtrInst *) { return true; }
309 static inline bool classof(const Instruction *I) {
310 return (I->getOpcode() == Instruction::GetElementPtr);
312 static inline bool classof(const Value *V) {
313 return isa<Instruction>(V) && classof(cast<Instruction>(V));
317 #endif // LLVM_IMEMORY_H