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 = "", Instruction *InsertBefore = 0);
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 class MallocInst : public AllocationInst {
67 MallocInst(const MallocInst &MI);
69 MallocInst(const Type *Ty, Value *ArraySize = 0, const std::string &Name = "",
70 Instruction *InsertBefore = 0)
71 : AllocationInst(Ty, ArraySize, Malloc, Name, InsertBefore) {}
73 virtual Instruction *clone() const {
74 return new MallocInst(*this);
77 // Methods for support type inquiry through isa, cast, and dyn_cast:
78 static inline bool classof(const MallocInst *) { return true; }
79 static inline bool classof(const Instruction *I) {
80 return (I->getOpcode() == Instruction::Malloc);
82 static inline bool classof(const Value *V) {
83 return isa<Instruction>(V) && classof(cast<Instruction>(V));
88 //===----------------------------------------------------------------------===//
90 //===----------------------------------------------------------------------===//
92 class AllocaInst : public AllocationInst {
93 AllocaInst(const AllocaInst &);
95 AllocaInst(const Type *Ty, Value *ArraySize = 0, const std::string &Name = "",
96 Instruction *InsertBefore = 0)
97 : AllocationInst(Ty, ArraySize, Alloca, Name, InsertBefore) {}
99 virtual Instruction *clone() const {
100 return new AllocaInst(*this);
103 // Methods for support type inquiry through isa, cast, and dyn_cast:
104 static inline bool classof(const AllocaInst *) { return true; }
105 static inline bool classof(const Instruction *I) {
106 return (I->getOpcode() == Instruction::Alloca);
108 static inline bool classof(const Value *V) {
109 return isa<Instruction>(V) && classof(cast<Instruction>(V));
114 //===----------------------------------------------------------------------===//
116 //===----------------------------------------------------------------------===//
118 struct FreeInst : public Instruction {
119 FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
121 virtual Instruction *clone() const { return new FreeInst(Operands[0]); }
123 virtual bool mayWriteToMemory() const { return true; }
125 // Methods for support type inquiry through isa, cast, and dyn_cast:
126 static inline bool classof(const FreeInst *) { return true; }
127 static inline bool classof(const Instruction *I) {
128 return (I->getOpcode() == Instruction::Free);
130 static inline bool classof(const Value *V) {
131 return isa<Instruction>(V) && classof(cast<Instruction>(V));
136 //===----------------------------------------------------------------------===//
138 //===----------------------------------------------------------------------===//
140 class LoadInst : public Instruction {
141 LoadInst(const LoadInst &LI) : Instruction(LI.getType(), Load) {
143 Operands.push_back(Use(LI.Operands[0], this));
146 LoadInst(Value *Ptr, const std::string &Name = "",
147 Instruction *InsertBefore = 0);
149 virtual Instruction *clone() const { return new LoadInst(*this); }
151 Value *getPointerOperand() { return getOperand(0); }
152 const Value *getPointerOperand() const { return getOperand(0); }
153 static unsigned getPointerOperandIndex() { return 0U; }
155 // Methods for support type inquiry through isa, cast, and dyn_cast:
156 static inline bool classof(const LoadInst *) { return true; }
157 static inline bool classof(const Instruction *I) {
158 return (I->getOpcode() == Instruction::Load);
160 static inline bool classof(const Value *V) {
161 return isa<Instruction>(V) && classof(cast<Instruction>(V));
166 //===----------------------------------------------------------------------===//
168 //===----------------------------------------------------------------------===//
170 class StoreInst : public Instruction {
171 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store) {
173 Operands.push_back(Use(SI.Operands[0], this));
174 Operands.push_back(Use(SI.Operands[1], this));
177 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore = 0);
178 virtual Instruction *clone() const { return new StoreInst(*this); }
180 virtual bool mayWriteToMemory() const { return true; }
182 Value *getPointerOperand() { return getOperand(1); }
183 const Value *getPointerOperand() const { return getOperand(1); }
184 static unsigned getPointerOperandIndex() { return 1U; }
186 // Methods for support type inquiry through isa, cast, and dyn_cast:
187 static inline bool classof(const StoreInst *) { return true; }
188 static inline bool classof(const Instruction *I) {
189 return (I->getOpcode() == Instruction::Store);
191 static inline bool classof(const Value *V) {
192 return isa<Instruction>(V) && classof(cast<Instruction>(V));
197 //===----------------------------------------------------------------------===//
198 // GetElementPtrInst Class
199 //===----------------------------------------------------------------------===//
201 class GetElementPtrInst : public Instruction {
202 GetElementPtrInst(const GetElementPtrInst &EPI)
203 : Instruction((Type*)EPI.getType(), GetElementPtr) {
204 Operands.reserve(EPI.Operands.size());
205 for (unsigned i = 0, E = EPI.Operands.size(); i != E; ++i)
206 Operands.push_back(Use(EPI.Operands[i], this));
209 GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
210 const std::string &Name = "", Instruction *InsertBefore =0);
211 virtual Instruction *clone() const { return new GetElementPtrInst(*this); }
213 // getType - Overload to return most specific pointer type...
214 inline const PointerType *getType() const {
215 return (PointerType*)Instruction::getType();
218 /// getIndexedType - Returns the type of the element that would be loaded with
219 /// a load instruction with the specified parameters.
221 /// A null type is returned if the indices are invalid for the specified
224 static const Type *getIndexedType(const Type *Ptr,
225 const std::vector<Value*> &Indices,
226 bool AllowStructLeaf = false);
228 inline op_iterator idx_begin() {
231 inline const_op_iterator idx_begin() const {
234 inline op_iterator idx_end() { return op_end(); }
235 inline const_op_iterator idx_end() const { return op_end(); }
237 Value *getPointerOperand() {
238 return getOperand(0);
240 const Value *getPointerOperand() const {
241 return getOperand(0);
243 static unsigned getPointerOperandIndex() {
244 return 0U; // get index for modifying correct operand
247 inline unsigned getNumIndices() const { // Note: always non-negative
248 return getNumOperands() - 1;
251 inline bool hasIndices() const {
252 return getNumOperands() > 1;
255 // Methods for support type inquiry through isa, cast, and dyn_cast:
256 static inline bool classof(const GetElementPtrInst *) { return true; }
257 static inline bool classof(const Instruction *I) {
258 return (I->getOpcode() == Instruction::GetElementPtr);
260 static inline bool classof(const Value *V) {
261 return isa<Instruction>(V) && classof(cast<Instruction>(V));
265 #endif // LLVM_IMEMORY_H