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"
24 //===----------------------------------------------------------------------===//
25 // AllocationInst Class
26 //===----------------------------------------------------------------------===//
28 /// AllocationInst - This class is the common base class of MallocInst and
31 class AllocationInst : public Instruction {
33 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
34 const std::string &Name = "", Instruction *InsertBefore = 0);
37 /// isArrayAllocation - Return true if there is an allocation size parameter
38 /// to the allocation instruction that is not 1.
40 bool isArrayAllocation() const;
42 /// getArraySize - Get the number of element allocated, for a simple
43 /// allocation of a single element, this will return a constant 1 value.
45 inline const Value *getArraySize() const { return Operands[0]; }
46 inline Value *getArraySize() { return Operands[0]; }
48 /// getType - Overload to return most specific pointer type
50 inline const PointerType *getType() const {
51 return reinterpret_cast<const PointerType*>(Instruction::getType());
54 /// getAllocatedType - Return the type that is being allocated by the
57 const Type *getAllocatedType() const;
59 virtual Instruction *clone() const = 0;
61 // Methods for support type inquiry through isa, cast, and dyn_cast:
62 static inline bool classof(const AllocationInst *) { return true; }
63 static inline bool classof(const Instruction *I) {
64 return I->getOpcode() == Instruction::Alloca ||
65 I->getOpcode() == Instruction::Malloc;
67 static inline bool classof(const Value *V) {
68 return isa<Instruction>(V) && classof(cast<Instruction>(V));
73 //===----------------------------------------------------------------------===//
75 //===----------------------------------------------------------------------===//
77 /// MallocInst - an instruction to allocated memory on the heap
79 class MallocInst : public AllocationInst {
80 MallocInst(const MallocInst &MI);
82 MallocInst(const Type *Ty, Value *ArraySize = 0, const std::string &Name = "",
83 Instruction *InsertBefore = 0)
84 : AllocationInst(Ty, ArraySize, Malloc, Name, InsertBefore) {}
86 virtual Instruction *clone() const {
87 return new MallocInst(*this);
90 // Methods for support type inquiry through isa, cast, and dyn_cast:
91 static inline bool classof(const MallocInst *) { return true; }
92 static inline bool classof(const Instruction *I) {
93 return (I->getOpcode() == Instruction::Malloc);
95 static inline bool classof(const Value *V) {
96 return isa<Instruction>(V) && classof(cast<Instruction>(V));
101 //===----------------------------------------------------------------------===//
103 //===----------------------------------------------------------------------===//
105 /// AllocaInst - an instruction to allocate memory on the stack
107 class AllocaInst : public AllocationInst {
108 AllocaInst(const AllocaInst &);
110 AllocaInst(const Type *Ty, Value *ArraySize = 0, const std::string &Name = "",
111 Instruction *InsertBefore = 0)
112 : AllocationInst(Ty, ArraySize, Alloca, Name, InsertBefore) {}
114 virtual Instruction *clone() const {
115 return new AllocaInst(*this);
118 // Methods for support type inquiry through isa, cast, and dyn_cast:
119 static inline bool classof(const AllocaInst *) { return true; }
120 static inline bool classof(const Instruction *I) {
121 return (I->getOpcode() == Instruction::Alloca);
123 static inline bool classof(const Value *V) {
124 return isa<Instruction>(V) && classof(cast<Instruction>(V));
129 //===----------------------------------------------------------------------===//
131 //===----------------------------------------------------------------------===//
133 /// FreeInst - an instruction to deallocate memory
135 struct FreeInst : public Instruction {
136 FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
138 virtual Instruction *clone() const { return new FreeInst(Operands[0]); }
140 virtual bool mayWriteToMemory() const { return true; }
142 // Methods for support type inquiry through isa, cast, and dyn_cast:
143 static inline bool classof(const FreeInst *) { return true; }
144 static inline bool classof(const Instruction *I) {
145 return (I->getOpcode() == Instruction::Free);
147 static inline bool classof(const Value *V) {
148 return isa<Instruction>(V) && classof(cast<Instruction>(V));
153 //===----------------------------------------------------------------------===//
155 //===----------------------------------------------------------------------===//
157 /// LoadInst - an instruction for reading from memory
159 class LoadInst : public Instruction {
160 LoadInst(const LoadInst &LI) : Instruction(LI.getType(), Load) {
161 Volatile = LI.isVolatile();
163 Operands.push_back(Use(LI.Operands[0], this));
165 bool Volatile; // True if this is a volatile load
167 LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBefore);
168 LoadInst(Value *Ptr, const std::string &Name = "", bool isVolatile = false,
169 Instruction *InsertBefore = 0);
171 /// isVolatile - Return true if this is a load from a volatile memory
174 bool isVolatile() const { return Volatile; }
176 /// setVolatile - Specify whether this is a volatile load or not.
178 void setVolatile(bool V) { Volatile = V; }
180 virtual Instruction *clone() const { return new LoadInst(*this); }
182 virtual bool mayWriteToMemory() const { return isVolatile(); }
184 Value *getPointerOperand() { return getOperand(0); }
185 const Value *getPointerOperand() const { return getOperand(0); }
186 static unsigned getPointerOperandIndex() { return 0U; }
188 // Methods for support type inquiry through isa, cast, and dyn_cast:
189 static inline bool classof(const LoadInst *) { return true; }
190 static inline bool classof(const Instruction *I) {
191 return I->getOpcode() == Instruction::Load;
193 static inline bool classof(const Value *V) {
194 return isa<Instruction>(V) && classof(cast<Instruction>(V));
199 //===----------------------------------------------------------------------===//
201 //===----------------------------------------------------------------------===//
203 /// StoreInst - an instruction for storing to memory
205 class StoreInst : public Instruction {
206 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store) {
207 Volatile = SI.isVolatile();
209 Operands.push_back(Use(SI.Operands[0], this));
210 Operands.push_back(Use(SI.Operands[1], this));
212 bool Volatile; // True if this is a volatile store
214 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
215 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
216 Instruction *InsertBefore = 0);
219 /// isVolatile - Return true if this is a load from a volatile memory
222 bool isVolatile() const { return Volatile; }
224 /// setVolatile - Specify whether this is a volatile load or not.
226 void setVolatile(bool V) { Volatile = V; }
228 virtual Instruction *clone() const { return new StoreInst(*this); }
230 virtual bool mayWriteToMemory() const { return true; }
232 Value *getPointerOperand() { return getOperand(1); }
233 const Value *getPointerOperand() const { return getOperand(1); }
234 static unsigned getPointerOperandIndex() { return 1U; }
236 // Methods for support type inquiry through isa, cast, and dyn_cast:
237 static inline bool classof(const StoreInst *) { return true; }
238 static inline bool classof(const Instruction *I) {
239 return I->getOpcode() == Instruction::Store;
241 static inline bool classof(const Value *V) {
242 return isa<Instruction>(V) && classof(cast<Instruction>(V));
247 //===----------------------------------------------------------------------===//
248 // GetElementPtrInst Class
249 //===----------------------------------------------------------------------===//
251 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
252 /// access elements of arrays and structs
254 class GetElementPtrInst : public Instruction {
255 GetElementPtrInst(const GetElementPtrInst &EPI)
256 : Instruction(reinterpret_cast<const 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 = "", Instruction *InsertBefore =0);
264 virtual Instruction *clone() const { return new GetElementPtrInst(*this); }
266 // getType - Overload to return most specific pointer type...
267 inline const PointerType *getType() const {
268 return reinterpret_cast<const PointerType*>(Instruction::getType());
271 /// getIndexedType - Returns the type of the element that would be loaded with
272 /// a load instruction with the specified parameters.
274 /// A null type is returned if the indices are invalid for the specified
277 static const Type *getIndexedType(const Type *Ptr,
278 const std::vector<Value*> &Indices,
279 bool AllowStructLeaf = false);
281 inline op_iterator idx_begin() { return op_begin()+1; }
282 inline const_op_iterator idx_begin() const { return op_begin()+1; }
283 inline op_iterator idx_end() { return op_end(); }
284 inline const_op_iterator idx_end() const { return op_end(); }
286 Value *getPointerOperand() {
287 return getOperand(0);
289 const Value *getPointerOperand() const {
290 return getOperand(0);
292 static unsigned getPointerOperandIndex() {
293 return 0U; // get index for modifying correct operand
296 inline unsigned getNumIndices() const { // Note: always non-negative
297 return getNumOperands() - 1;
300 inline bool hasIndices() const {
301 return getNumOperands() > 1;
304 // Methods for support type inquiry through isa, cast, and dyn_cast:
305 static inline bool classof(const GetElementPtrInst *) { return true; }
306 static inline bool classof(const Instruction *I) {
307 return (I->getOpcode() == Instruction::GetElementPtr);
309 static inline bool classof(const Value *V) {
310 return isa<Instruction>(V) && classof(cast<Instruction>(V));
314 } // End llvm namespace
316 #endif // LLVM_IMEMORY_H