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"
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
46 const Type *getAllocatedType() const;
48 virtual Instruction *clone() const = 0;
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;
56 static inline bool classof(const Value *V) {
57 return isa<Instruction>(V) && classof(cast<Instruction>(V));
62 //===----------------------------------------------------------------------===//
64 //===----------------------------------------------------------------------===//
66 struct MallocInst : public AllocationInst {
67 MallocInst(const Type *Ty, Value *ArraySize = 0, const std::string &Name = "")
68 : AllocationInst(Ty, ArraySize, Malloc, Name) {}
70 virtual Instruction *clone() const {
71 return new MallocInst((Type*)getType(), (Value*)Operands[0].get());
74 // Methods for support type inquiry through isa, cast, and dyn_cast:
75 static inline bool classof(const MallocInst *) { return true; }
76 static inline bool classof(const Instruction *I) {
77 return (I->getOpcode() == Instruction::Malloc);
79 static inline bool classof(const Value *V) {
80 return isa<Instruction>(V) && classof(cast<Instruction>(V));
85 //===----------------------------------------------------------------------===//
87 //===----------------------------------------------------------------------===//
89 struct AllocaInst : public AllocationInst {
90 AllocaInst(const Type *Ty, Value *ArraySize = 0, const std::string &Name = "")
91 : AllocationInst(Ty, ArraySize, Alloca, Name) {}
93 virtual Instruction *clone() const {
94 return new AllocaInst((Type*)getType(), (Value*)Operands[0].get());
97 // Methods for support type inquiry through isa, cast, and dyn_cast:
98 static inline bool classof(const AllocaInst *) { return true; }
99 static inline bool classof(const Instruction *I) {
100 return (I->getOpcode() == Instruction::Alloca);
102 static inline bool classof(const Value *V) {
103 return isa<Instruction>(V) && classof(cast<Instruction>(V));
108 //===----------------------------------------------------------------------===//
110 //===----------------------------------------------------------------------===//
112 struct FreeInst : public Instruction {
113 FreeInst(Value *Ptr);
115 virtual Instruction *clone() const { return new FreeInst(Operands[0]); }
117 virtual bool hasSideEffects() const { return true; }
119 // Methods for support type inquiry through isa, cast, and dyn_cast:
120 static inline bool classof(const FreeInst *) { return true; }
121 static inline bool classof(const Instruction *I) {
122 return (I->getOpcode() == Instruction::Free);
124 static inline bool classof(const Value *V) {
125 return isa<Instruction>(V) && classof(cast<Instruction>(V));
130 //===----------------------------------------------------------------------===//
131 // MemAccessInst Class
132 //===----------------------------------------------------------------------===//
134 // MemAccessInst - Common base class of LoadInst, StoreInst, and
135 // GetElementPtrInst...
137 class MemAccessInst : public Instruction {
139 inline MemAccessInst(const Type *Ty, unsigned Opcode,
140 const std::string &Nam = "")
141 : Instruction(Ty, Opcode, Nam) {}
143 // getIndexedType - Returns the type of the element that would be loaded with
144 // a load instruction with the specified parameters.
146 // A null type is returned if the indices are invalid for the specified
149 static const Type *getIndexedType(const Type *Ptr,
150 const std::vector<Value*> &Indices,
151 bool AllowStructLeaf = false);
153 inline op_iterator idx_begin() {
154 return op_begin()+getFirstIndexOperandNumber();
156 inline const_op_iterator idx_begin() const {
157 return op_begin()+getFirstIndexOperandNumber();
159 inline op_iterator idx_end() { return op_end(); }
160 inline const_op_iterator idx_end() const { return op_end(); }
163 std::vector<Value*> copyIndices() const {
164 return std::vector<Value*>(idx_begin(), idx_end());
167 Value *getPointerOperand() {
168 return getOperand(getFirstIndexOperandNumber()-1);
170 const Value *getPointerOperand() const {
171 return getOperand(getFirstIndexOperandNumber()-1);
174 virtual unsigned getFirstIndexOperandNumber() const = 0;
176 inline bool hasIndices() const {
177 return getNumOperands() > getFirstIndexOperandNumber();
180 // Methods for support type inquiry through isa, cast, and dyn_cast:
181 static inline bool classof(const MemAccessInst *) { return true; }
182 static inline bool classof(const Instruction *I) {
183 return I->getOpcode() == Load || I->getOpcode() == Store ||
184 I->getOpcode() == GetElementPtr;
186 static inline bool classof(const Value *V) {
187 return isa<Instruction>(V) && classof(cast<Instruction>(V));
192 //===----------------------------------------------------------------------===//
194 //===----------------------------------------------------------------------===//
196 class LoadInst : public MemAccessInst {
197 LoadInst(const LoadInst &LI) : MemAccessInst(LI.getType(), Load) {
198 Operands.reserve(LI.Operands.size());
199 for (unsigned i = 0, E = LI.Operands.size(); i != E; ++i)
200 Operands.push_back(Use(LI.Operands[i], this));
203 LoadInst(Value *Ptr, const std::vector<Value*> &Ix, const std::string & = "");
204 LoadInst(Value *Ptr, const std::string &Name = "");
206 virtual Instruction *clone() const { return new LoadInst(*this); }
208 virtual unsigned getFirstIndexOperandNumber() const { return 1; }
210 // Methods for support type inquiry through isa, cast, and dyn_cast:
211 static inline bool classof(const LoadInst *) { return true; }
212 static inline bool classof(const Instruction *I) {
213 return (I->getOpcode() == Instruction::Load);
215 static inline bool classof(const Value *V) {
216 return isa<Instruction>(V) && classof(cast<Instruction>(V));
221 //===----------------------------------------------------------------------===//
223 //===----------------------------------------------------------------------===//
225 class StoreInst : public MemAccessInst {
226 StoreInst(const StoreInst &SI) : MemAccessInst(SI.getType(), Store) {
227 Operands.reserve(SI.Operands.size());
228 for (unsigned i = 0, E = SI.Operands.size(); i != E; ++i)
229 Operands.push_back(Use(SI.Operands[i], this));
232 StoreInst(Value *Val, Value *Ptr, const std::vector<Value*> &Idx);
233 StoreInst(Value *Val, Value *Ptr);
234 virtual Instruction *clone() const { return new StoreInst(*this); }
236 virtual bool hasSideEffects() const { return true; }
237 virtual unsigned getFirstIndexOperandNumber() const { return 2; }
239 // Methods for support type inquiry through isa, cast, and dyn_cast:
240 static inline bool classof(const StoreInst *) { return true; }
241 static inline bool classof(const Instruction *I) {
242 return (I->getOpcode() == Instruction::Store);
244 static inline bool classof(const Value *V) {
245 return isa<Instruction>(V) && classof(cast<Instruction>(V));
250 //===----------------------------------------------------------------------===//
251 // GetElementPtrInst Class
252 //===----------------------------------------------------------------------===//
254 class GetElementPtrInst : public MemAccessInst {
255 GetElementPtrInst(const GetElementPtrInst &EPI)
256 : MemAccessInst((Type*)EPI.getType(), GetElementPtr) {
257 Operands.reserve(EPI.Operands.size());
258 for (unsigned i = 0, E = EPI.Operands.size(); i != E; ++i)
259 Operands.push_back(Use(EPI.Operands[i], this));
262 GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
263 const std::string &Name = "");
264 virtual Instruction *clone() const { return new GetElementPtrInst(*this); }
265 virtual unsigned getFirstIndexOperandNumber() const { return 1; }
267 // getType - Overload to return most specific pointer type...
268 inline const PointerType *getType() const {
269 return (PointerType*)Instruction::getType();
272 // Methods for support type inquiry through isa, cast, and dyn_cast:
273 static inline bool classof(const GetElementPtrInst *) { return true; }
274 static inline bool classof(const Instruction *I) {
275 return (I->getOpcode() == Instruction::GetElementPtr);
277 static inline bool classof(const Value *V) {
278 return isa<Instruction>(V) && classof(cast<Instruction>(V));
282 #endif // LLVM_IMEMORY_H