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 = "");
27 // isArrayAllocation - Return true if there is an allocation size parameter
28 // to the allocation instruction that is not 1.
30 bool isArrayAllocation() const;
32 // getArraySize - Get the number of element allocated, for a simple allocation
33 // of a single element, this will return a constant 1 value.
35 inline const Value *getArraySize() const { return Operands[0]; }
36 inline Value *getArraySize() { return Operands[0]; }
38 // getType - Overload to return most specific pointer type...
39 inline const PointerType *getType() const {
40 return (const PointerType*)Instruction::getType();
43 // getAllocatedType - Return the type that is being allocated by the
45 inline const Type *getAllocatedType() const {
46 return getType()->getElementType();
49 virtual Instruction *clone() const = 0;
51 // Methods for support type inquiry through isa, cast, and dyn_cast:
52 static inline bool classof(const AllocationInst *) { return true; }
53 static inline bool classof(const Instruction *I) {
54 return I->getOpcode() == Instruction::Alloca ||
55 I->getOpcode() == Instruction::Malloc;
57 static inline bool classof(const Value *V) {
58 return isa<Instruction>(V) && classof(cast<Instruction>(V));
63 //===----------------------------------------------------------------------===//
65 //===----------------------------------------------------------------------===//
67 class MallocInst : public AllocationInst {
69 MallocInst(const Type *Ty, Value *ArraySize = 0, const std::string &Name = "")
70 : AllocationInst(Ty, ArraySize, Malloc, Name) {}
72 virtual Instruction *clone() const {
73 return new MallocInst(getType(), (Value*)Operands[0].get());
76 virtual const char *getOpcodeName() const { return "malloc"; }
78 // Methods for support type inquiry through isa, cast, and dyn_cast:
79 static inline bool classof(const MallocInst *) { return true; }
80 static inline bool classof(const Instruction *I) {
81 return (I->getOpcode() == Instruction::Malloc);
83 static inline bool classof(const Value *V) {
84 return isa<Instruction>(V) && classof(cast<Instruction>(V));
89 //===----------------------------------------------------------------------===//
91 //===----------------------------------------------------------------------===//
93 class AllocaInst : public AllocationInst {
95 AllocaInst(const Type *Ty, Value *ArraySize = 0, const std::string &Name = "")
96 : AllocationInst(Ty, ArraySize, Alloca, Name) {}
98 virtual Instruction *clone() const {
99 return new AllocaInst(getType(), (Value*)Operands[0].get());
102 virtual const char *getOpcodeName() const { return "alloca"; }
104 // Methods for support type inquiry through isa, cast, and dyn_cast:
105 static inline bool classof(const AllocaInst *) { return true; }
106 static inline bool classof(const Instruction *I) {
107 return (I->getOpcode() == Instruction::Alloca);
109 static inline bool classof(const Value *V) {
110 return isa<Instruction>(V) && classof(cast<Instruction>(V));
115 //===----------------------------------------------------------------------===//
117 //===----------------------------------------------------------------------===//
119 class FreeInst : public Instruction {
121 FreeInst(Value *Ptr) : Instruction(Type::VoidTy, Free, "") {
122 assert(Ptr->getType()->isPointerType() && "Can't free nonpointer!");
124 Operands.push_back(Use(Ptr, this));
127 virtual Instruction *clone() const { return new FreeInst(Operands[0]); }
129 virtual const char *getOpcodeName() const { return "free"; }
131 virtual bool hasSideEffects() const { return true; }
133 // Methods for support type inquiry through isa, cast, and dyn_cast:
134 static inline bool classof(const FreeInst *) { return true; }
135 static inline bool classof(const Instruction *I) {
136 return (I->getOpcode() == Instruction::Free);
138 static inline bool classof(const Value *V) {
139 return isa<Instruction>(V) && classof(cast<Instruction>(V));
144 //===----------------------------------------------------------------------===//
145 // MemAccessInst Class
146 //===----------------------------------------------------------------------===//
148 // MemAccessInst - Common base class of LoadInst, StoreInst, and
149 // GetElementPtrInst...
151 class MemAccessInst : public Instruction {
153 inline MemAccessInst(const Type *Ty, unsigned Opcode,
154 const std::string &Nam = "")
155 : Instruction(Ty, Opcode, Nam) {}
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 std::vector<Value*> &Indices,
165 bool AllowStructLeaf = false);
167 inline op_iterator idx_begin() {
168 return op_begin()+getFirstIndexOperandNumber();
170 inline const_op_iterator idx_begin() const {
171 return op_begin()+getFirstIndexOperandNumber();
173 inline op_iterator idx_end() { return op_end(); }
174 inline const_op_iterator idx_end() const { return op_end(); }
177 std::vector<Value*> copyIndices() const {
178 return std::vector<Value*>(idx_begin(), idx_end());
181 Value *getPointerOperand() {
182 return getOperand(getFirstIndexOperandNumber()-1);
184 const Value *getPointerOperand() const {
185 return getOperand(getFirstIndexOperandNumber()-1);
188 virtual unsigned getFirstIndexOperandNumber() const = 0;
190 inline bool hasIndices() const {
191 return getNumOperands() > getFirstIndexOperandNumber();
194 // Methods for support type inquiry through isa, cast, and dyn_cast:
195 static inline bool classof(const MemAccessInst *) { return true; }
196 static inline bool classof(const Instruction *I) {
197 return I->getOpcode() == Load || I->getOpcode() == Store ||
198 I->getOpcode() == GetElementPtr;
200 static inline bool classof(const Value *V) {
201 return isa<Instruction>(V) && classof(cast<Instruction>(V));
206 //===----------------------------------------------------------------------===//
208 //===----------------------------------------------------------------------===//
210 class LoadInst : public MemAccessInst {
211 LoadInst(const LoadInst &LI) : MemAccessInst(LI.getType(), Load) {
212 Operands.reserve(LI.Operands.size());
213 for (unsigned i = 0, E = LI.Operands.size(); i != E; ++i)
214 Operands.push_back(Use(LI.Operands[i], this));
217 LoadInst(Value *Ptr, const std::vector<Value*> &Ix, const std::string & = "");
218 LoadInst(Value *Ptr, const std::string &Name = "");
220 virtual Instruction *clone() const { return new LoadInst(*this); }
221 virtual const char *getOpcodeName() const { return "load"; }
223 virtual unsigned getFirstIndexOperandNumber() const { return 1; }
225 // Methods for support type inquiry through isa, cast, and dyn_cast:
226 static inline bool classof(const LoadInst *) { return true; }
227 static inline bool classof(const Instruction *I) {
228 return (I->getOpcode() == Instruction::Load);
230 static inline bool classof(const Value *V) {
231 return isa<Instruction>(V) && classof(cast<Instruction>(V));
236 //===----------------------------------------------------------------------===//
238 //===----------------------------------------------------------------------===//
240 class StoreInst : public MemAccessInst {
241 StoreInst(const StoreInst &SI) : MemAccessInst(SI.getType(), Store) {
242 Operands.reserve(SI.Operands.size());
243 for (unsigned i = 0, E = SI.Operands.size(); i != E; ++i)
244 Operands.push_back(Use(SI.Operands[i], this));
247 StoreInst(Value *Val, Value *Ptr, const std::vector<Value*> &Idx,
248 const std::string &Name = "");
249 StoreInst(Value *Val, Value *Ptr, const std::string &Name = "");
250 virtual Instruction *clone() const { return new StoreInst(*this); }
252 virtual const char *getOpcodeName() const { return "store"; }
254 virtual bool hasSideEffects() const { return true; }
255 virtual unsigned getFirstIndexOperandNumber() const { return 2; }
257 // Methods for support type inquiry through isa, cast, and dyn_cast:
258 static inline bool classof(const StoreInst *) { return true; }
259 static inline bool classof(const Instruction *I) {
260 return (I->getOpcode() == Instruction::Store);
262 static inline bool classof(const Value *V) {
263 return isa<Instruction>(V) && classof(cast<Instruction>(V));
268 //===----------------------------------------------------------------------===//
269 // GetElementPtrInst Class
270 //===----------------------------------------------------------------------===//
272 class GetElementPtrInst : public MemAccessInst {
273 GetElementPtrInst(const GetElementPtrInst &EPI)
274 : MemAccessInst(EPI.getType(), GetElementPtr) {
275 Operands.reserve(EPI.Operands.size());
276 for (unsigned i = 0, E = EPI.Operands.size(); i != E; ++i)
277 Operands.push_back(Use(EPI.Operands[i], this));
280 GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
281 const std::string &Name = "");
282 virtual Instruction *clone() const { return new GetElementPtrInst(*this); }
283 virtual const char *getOpcodeName() const { return "getelementptr"; }
284 virtual unsigned getFirstIndexOperandNumber() const { return 1; }
286 inline bool isArraySelector() const { return !isStructSelector(); }
287 bool isStructSelector() const;
289 // getType - Overload to return most specific pointer type...
290 inline const PointerType *getType() const {
291 return cast<const PointerType>(Instruction::getType());
294 // Methods for support type inquiry through isa, cast, and dyn_cast:
295 static inline bool classof(const GetElementPtrInst *) { return true; }
296 static inline bool classof(const Instruction *I) {
297 return (I->getOpcode() == Instruction::GetElementPtr);
299 static inline bool classof(const Value *V) {
300 return isa<Instruction>(V) && classof(cast<Instruction>(V));
304 #endif // LLVM_IMEMORY_H