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 struct MallocInst : public AllocationInst {
67 MallocInst(const Type *Ty, Value *ArraySize = 0, const std::string &Name = "",
68 Instruction *InsertBefore = 0)
69 : AllocationInst(Ty, ArraySize, Malloc, Name, InsertBefore) {}
71 virtual Instruction *clone() const {
72 return new MallocInst((Type*)getType(), (Value*)Operands[0].get());
75 // Methods for support type inquiry through isa, cast, and dyn_cast:
76 static inline bool classof(const MallocInst *) { return true; }
77 static inline bool classof(const Instruction *I) {
78 return (I->getOpcode() == Instruction::Malloc);
80 static inline bool classof(const Value *V) {
81 return isa<Instruction>(V) && classof(cast<Instruction>(V));
86 //===----------------------------------------------------------------------===//
88 //===----------------------------------------------------------------------===//
90 struct AllocaInst : public AllocationInst {
91 AllocaInst(const Type *Ty, Value *ArraySize = 0, const std::string &Name = "",
92 Instruction *InsertBefore = 0)
93 : AllocationInst(Ty, ArraySize, Alloca, Name, InsertBefore) {}
95 virtual Instruction *clone() const {
96 return new AllocaInst((Type*)getType(), (Value*)Operands[0].get());
99 // Methods for support type inquiry through isa, cast, and dyn_cast:
100 static inline bool classof(const AllocaInst *) { return true; }
101 static inline bool classof(const Instruction *I) {
102 return (I->getOpcode() == Instruction::Alloca);
104 static inline bool classof(const Value *V) {
105 return isa<Instruction>(V) && classof(cast<Instruction>(V));
110 //===----------------------------------------------------------------------===//
112 //===----------------------------------------------------------------------===//
114 struct FreeInst : public Instruction {
115 FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
117 virtual Instruction *clone() const { return new FreeInst(Operands[0]); }
119 virtual bool hasSideEffects() const { return true; }
121 // Methods for support type inquiry through isa, cast, and dyn_cast:
122 static inline bool classof(const FreeInst *) { return true; }
123 static inline bool classof(const Instruction *I) {
124 return (I->getOpcode() == Instruction::Free);
126 static inline bool classof(const Value *V) {
127 return isa<Instruction>(V) && classof(cast<Instruction>(V));
132 //===----------------------------------------------------------------------===//
134 //===----------------------------------------------------------------------===//
136 class LoadInst : public Instruction {
137 LoadInst(const LoadInst &LI) : Instruction(LI.getType(), Load) {
139 Operands.push_back(Use(LI.Operands[0], this));
142 LoadInst(Value *Ptr, const std::string &Name = "",
143 Instruction *InsertBefore = 0);
145 virtual Instruction *clone() const { return new LoadInst(*this); }
147 Value *getPointerOperand() { return getOperand(0); }
148 const Value *getPointerOperand() const { return getOperand(0); }
150 // Methods for support type inquiry through isa, cast, and dyn_cast:
151 static inline bool classof(const LoadInst *) { return true; }
152 static inline bool classof(const Instruction *I) {
153 return (I->getOpcode() == Instruction::Load);
155 static inline bool classof(const Value *V) {
156 return isa<Instruction>(V) && classof(cast<Instruction>(V));
161 //===----------------------------------------------------------------------===//
163 //===----------------------------------------------------------------------===//
165 class StoreInst : public Instruction {
166 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store) {
168 Operands.push_back(Use(SI.Operands[0], this));
169 Operands.push_back(Use(SI.Operands[1], this));
172 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore = 0);
173 virtual Instruction *clone() const { return new StoreInst(*this); }
175 virtual bool hasSideEffects() const { return true; }
177 Value *getPointerOperand() { return getOperand(1); }
178 const Value *getPointerOperand() const { return getOperand(1); }
180 // Methods for support type inquiry through isa, cast, and dyn_cast:
181 static inline bool classof(const StoreInst *) { return true; }
182 static inline bool classof(const Instruction *I) {
183 return (I->getOpcode() == Instruction::Store);
185 static inline bool classof(const Value *V) {
186 return isa<Instruction>(V) && classof(cast<Instruction>(V));
191 //===----------------------------------------------------------------------===//
192 // GetElementPtrInst Class
193 //===----------------------------------------------------------------------===//
195 class GetElementPtrInst : public Instruction {
196 GetElementPtrInst(const GetElementPtrInst &EPI)
197 : Instruction((Type*)EPI.getType(), GetElementPtr) {
198 Operands.reserve(EPI.Operands.size());
199 for (unsigned i = 0, E = EPI.Operands.size(); i != E; ++i)
200 Operands.push_back(Use(EPI.Operands[i], this));
203 GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
204 const std::string &Name = "", Instruction *InsertBefore =0);
205 virtual Instruction *clone() const { return new GetElementPtrInst(*this); }
207 // getType - Overload to return most specific pointer type...
208 inline const PointerType *getType() const {
209 return (PointerType*)Instruction::getType();
212 /// getIndexedType - Returns the type of the element that would be loaded with
213 /// a load instruction with the specified parameters.
215 /// A null type is returned if the indices are invalid for the specified
218 static const Type *getIndexedType(const Type *Ptr,
219 const std::vector<Value*> &Indices,
220 bool AllowStructLeaf = false);
222 inline op_iterator idx_begin() {
225 inline const_op_iterator idx_begin() const {
228 inline op_iterator idx_end() { return op_end(); }
229 inline const_op_iterator idx_end() const { return op_end(); }
231 Value *getPointerOperand() {
232 return getOperand(0);
234 const Value *getPointerOperand() const {
235 return getOperand(0);
238 inline unsigned getNumIndices() const { // Note: always non-negative
239 return getNumOperands() - 1;
242 inline bool hasIndices() const {
243 return getNumOperands() > 1;
246 // Methods for support type inquiry through isa, cast, and dyn_cast:
247 static inline bool classof(const GetElementPtrInst *) { return true; }
248 static inline bool classof(const Instruction *I) {
249 return (I->getOpcode() == Instruction::GetElementPtr);
251 static inline bool classof(const Value *V) {
252 return isa<Instruction>(V) && classof(cast<Instruction>(V));
256 #endif // LLVM_IMEMORY_H