1 //===-- llvm/iMemory.h - Memory Operator node definitions -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the declarations of all of the memory related operators.
11 // This includes: malloc, free, alloca, load, store, and getelementptr
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_IMEMORY_H
16 #define LLVM_IMEMORY_H
18 #include "llvm/Instruction.h"
21 //===----------------------------------------------------------------------===//
22 // AllocationInst Class
23 //===----------------------------------------------------------------------===//
25 // AllocationInst - This class is the common base class of MallocInst and
28 class AllocationInst : public Instruction {
30 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
31 const std::string &Name = "", Instruction *InsertBefore = 0);
34 // isArrayAllocation - Return true if there is an allocation size parameter
35 // to the allocation instruction that is not 1.
37 bool isArrayAllocation() const;
39 // getArraySize - Get the number of element allocated, for a simple allocation
40 // of a single element, this will return a constant 1 value.
42 inline const Value *getArraySize() const { return Operands[0]; }
43 inline Value *getArraySize() { return Operands[0]; }
45 // getType - Overload to return most specific pointer type...
46 inline const PointerType *getType() const {
47 return (const PointerType*)Instruction::getType();
50 // getAllocatedType - Return the type that is being allocated by the
53 const Type *getAllocatedType() const;
55 virtual Instruction *clone() const = 0;
57 // Methods for support type inquiry through isa, cast, and dyn_cast:
58 static inline bool classof(const AllocationInst *) { return true; }
59 static inline bool classof(const Instruction *I) {
60 return I->getOpcode() == Instruction::Alloca ||
61 I->getOpcode() == Instruction::Malloc;
63 static inline bool classof(const Value *V) {
64 return isa<Instruction>(V) && classof(cast<Instruction>(V));
69 //===----------------------------------------------------------------------===//
71 //===----------------------------------------------------------------------===//
73 class MallocInst : public AllocationInst {
74 MallocInst(const MallocInst &MI);
76 MallocInst(const Type *Ty, Value *ArraySize = 0, const std::string &Name = "",
77 Instruction *InsertBefore = 0)
78 : AllocationInst(Ty, ArraySize, Malloc, Name, InsertBefore) {}
80 virtual Instruction *clone() const {
81 return new MallocInst(*this);
84 // Methods for support type inquiry through isa, cast, and dyn_cast:
85 static inline bool classof(const MallocInst *) { return true; }
86 static inline bool classof(const Instruction *I) {
87 return (I->getOpcode() == Instruction::Malloc);
89 static inline bool classof(const Value *V) {
90 return isa<Instruction>(V) && classof(cast<Instruction>(V));
95 //===----------------------------------------------------------------------===//
97 //===----------------------------------------------------------------------===//
99 class AllocaInst : public AllocationInst {
100 AllocaInst(const AllocaInst &);
102 AllocaInst(const Type *Ty, Value *ArraySize = 0, const std::string &Name = "",
103 Instruction *InsertBefore = 0)
104 : AllocationInst(Ty, ArraySize, Alloca, Name, InsertBefore) {}
106 virtual Instruction *clone() const {
107 return new AllocaInst(*this);
110 // Methods for support type inquiry through isa, cast, and dyn_cast:
111 static inline bool classof(const AllocaInst *) { return true; }
112 static inline bool classof(const Instruction *I) {
113 return (I->getOpcode() == Instruction::Alloca);
115 static inline bool classof(const Value *V) {
116 return isa<Instruction>(V) && classof(cast<Instruction>(V));
121 //===----------------------------------------------------------------------===//
123 //===----------------------------------------------------------------------===//
125 struct FreeInst : public Instruction {
126 FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
128 virtual Instruction *clone() const { return new FreeInst(Operands[0]); }
130 virtual bool mayWriteToMemory() const { return true; }
132 // Methods for support type inquiry through isa, cast, and dyn_cast:
133 static inline bool classof(const FreeInst *) { return true; }
134 static inline bool classof(const Instruction *I) {
135 return (I->getOpcode() == Instruction::Free);
137 static inline bool classof(const Value *V) {
138 return isa<Instruction>(V) && classof(cast<Instruction>(V));
143 //===----------------------------------------------------------------------===//
145 //===----------------------------------------------------------------------===//
147 class LoadInst : public Instruction {
148 LoadInst(const LoadInst &LI) : Instruction(LI.getType(), Load) {
149 Volatile = LI.isVolatile();
151 Operands.push_back(Use(LI.Operands[0], this));
153 bool Volatile; // True if this is a volatile load
155 LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBefore);
156 LoadInst(Value *Ptr, const std::string &Name = "", bool isVolatile = false,
157 Instruction *InsertBefore = 0);
159 /// isVolatile - Return true if this is a load from a volatile memory
161 bool isVolatile() const { return Volatile; }
163 /// setVolatile - Specify whether this is a volatile load or not.
165 void setVolatile(bool V) { Volatile = V; }
167 virtual Instruction *clone() const { return new LoadInst(*this); }
169 virtual bool mayWriteToMemory() const { return isVolatile(); }
171 Value *getPointerOperand() { return getOperand(0); }
172 const Value *getPointerOperand() const { return getOperand(0); }
173 static unsigned getPointerOperandIndex() { return 0U; }
175 // Methods for support type inquiry through isa, cast, and dyn_cast:
176 static inline bool classof(const LoadInst *) { return true; }
177 static inline bool classof(const Instruction *I) {
178 return I->getOpcode() == Instruction::Load;
180 static inline bool classof(const Value *V) {
181 return isa<Instruction>(V) && classof(cast<Instruction>(V));
186 //===----------------------------------------------------------------------===//
188 //===----------------------------------------------------------------------===//
190 class StoreInst : public Instruction {
191 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store) {
192 Volatile = SI.isVolatile();
194 Operands.push_back(Use(SI.Operands[0], this));
195 Operands.push_back(Use(SI.Operands[1], this));
197 bool Volatile; // True if this is a volatile store
199 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
200 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
201 Instruction *InsertBefore = 0);
204 /// isVolatile - Return true if this is a load from a volatile memory
206 bool isVolatile() const { return Volatile; }
208 /// setVolatile - Specify whether this is a volatile load or not.
210 void setVolatile(bool V) { Volatile = V; }
212 virtual Instruction *clone() const { return new StoreInst(*this); }
214 virtual bool mayWriteToMemory() const { return true; }
216 Value *getPointerOperand() { return getOperand(1); }
217 const Value *getPointerOperand() const { return getOperand(1); }
218 static unsigned getPointerOperandIndex() { return 1U; }
220 // Methods for support type inquiry through isa, cast, and dyn_cast:
221 static inline bool classof(const StoreInst *) { return true; }
222 static inline bool classof(const Instruction *I) {
223 return I->getOpcode() == Instruction::Store;
225 static inline bool classof(const Value *V) {
226 return isa<Instruction>(V) && classof(cast<Instruction>(V));
231 //===----------------------------------------------------------------------===//
232 // GetElementPtrInst Class
233 //===----------------------------------------------------------------------===//
235 class GetElementPtrInst : public Instruction {
236 GetElementPtrInst(const GetElementPtrInst &EPI)
237 : Instruction((Type*)EPI.getType(), GetElementPtr) {
238 Operands.reserve(EPI.Operands.size());
239 for (unsigned i = 0, E = EPI.Operands.size(); i != E; ++i)
240 Operands.push_back(Use(EPI.Operands[i], this));
243 GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
244 const std::string &Name = "", Instruction *InsertBefore =0);
245 virtual Instruction *clone() const { return new GetElementPtrInst(*this); }
247 // getType - Overload to return most specific pointer type...
248 inline const PointerType *getType() const {
249 return (PointerType*)Instruction::getType();
252 /// getIndexedType - Returns the type of the element that would be loaded with
253 /// a load instruction with the specified parameters.
255 /// A null type is returned if the indices are invalid for the specified
258 static const Type *getIndexedType(const Type *Ptr,
259 const std::vector<Value*> &Indices,
260 bool AllowStructLeaf = false);
262 inline op_iterator idx_begin() {
265 inline const_op_iterator idx_begin() const {
268 inline op_iterator idx_end() { return op_end(); }
269 inline const_op_iterator idx_end() const { return op_end(); }
271 Value *getPointerOperand() {
272 return getOperand(0);
274 const Value *getPointerOperand() const {
275 return getOperand(0);
277 static unsigned getPointerOperandIndex() {
278 return 0U; // get index for modifying correct operand
281 inline unsigned getNumIndices() const { // Note: always non-negative
282 return getNumOperands() - 1;
285 inline bool hasIndices() const {
286 return getNumOperands() > 1;
289 // Methods for support type inquiry through isa, cast, and dyn_cast:
290 static inline bool classof(const GetElementPtrInst *) { return true; }
291 static inline bool classof(const Instruction *I) {
292 return (I->getOpcode() == Instruction::GetElementPtr);
294 static inline bool classof(const Value *V) {
295 return isa<Instruction>(V) && classof(cast<Instruction>(V));
299 #endif // LLVM_IMEMORY_H