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 void init(const Type *Ty, Value *ArraySize, unsigned iTy);
34 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
35 const std::string &Name = "", Instruction *InsertBefore = 0);
36 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
37 const std::string &Name, BasicBlock *InsertAtEnd);
41 /// isArrayAllocation - Return true if there is an allocation size parameter
42 /// to the allocation instruction that is not 1.
44 bool isArrayAllocation() const;
46 /// getArraySize - Get the number of element allocated, for a simple
47 /// allocation of a single element, this will return a constant 1 value.
49 inline const Value *getArraySize() const { return Operands[0]; }
50 inline Value *getArraySize() { return Operands[0]; }
52 /// getType - Overload to return most specific pointer type
54 inline const PointerType *getType() const {
55 return reinterpret_cast<const PointerType*>(Instruction::getType());
58 /// getAllocatedType - Return the type that is being allocated by the
61 const Type *getAllocatedType() const;
63 virtual Instruction *clone() const = 0;
65 // Methods for support type inquiry through isa, cast, and dyn_cast:
66 static inline bool classof(const AllocationInst *) { return true; }
67 static inline bool classof(const Instruction *I) {
68 return I->getOpcode() == Instruction::Alloca ||
69 I->getOpcode() == Instruction::Malloc;
71 static inline bool classof(const Value *V) {
72 return isa<Instruction>(V) && classof(cast<Instruction>(V));
77 //===----------------------------------------------------------------------===//
79 //===----------------------------------------------------------------------===//
81 /// MallocInst - an instruction to allocated memory on the heap
83 class MallocInst : public AllocationInst {
84 MallocInst(const MallocInst &MI);
86 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
87 const std::string &Name = "",
88 Instruction *InsertBefore = 0)
89 : AllocationInst(Ty, ArraySize, Malloc, Name, InsertBefore) {}
90 MallocInst(const Type *Ty, Value *ArraySize, const std::string &Name,
91 BasicBlock *InsertAtEnd)
92 : AllocationInst(Ty, ArraySize, Malloc, Name, InsertAtEnd) {}
94 virtual Instruction *clone() const {
95 return new MallocInst(*this);
98 // Methods for support type inquiry through isa, cast, and dyn_cast:
99 static inline bool classof(const MallocInst *) { return true; }
100 static inline bool classof(const Instruction *I) {
101 return (I->getOpcode() == Instruction::Malloc);
103 static inline bool classof(const Value *V) {
104 return isa<Instruction>(V) && classof(cast<Instruction>(V));
109 //===----------------------------------------------------------------------===//
111 //===----------------------------------------------------------------------===//
113 /// AllocaInst - an instruction to allocate memory on the stack
115 class AllocaInst : public AllocationInst {
116 AllocaInst(const AllocaInst &);
118 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
119 const std::string &Name = "",
120 Instruction *InsertBefore = 0)
121 : AllocationInst(Ty, ArraySize, Alloca, Name, InsertBefore) {}
122 AllocaInst(const Type *Ty, Value *ArraySize, const std::string &Name,
123 BasicBlock *InsertAtEnd)
124 : AllocationInst(Ty, ArraySize, Alloca, Name, InsertAtEnd) {}
126 virtual Instruction *clone() const {
127 return new AllocaInst(*this);
130 // Methods for support type inquiry through isa, cast, and dyn_cast:
131 static inline bool classof(const AllocaInst *) { return true; }
132 static inline bool classof(const Instruction *I) {
133 return (I->getOpcode() == Instruction::Alloca);
135 static inline bool classof(const Value *V) {
136 return isa<Instruction>(V) && classof(cast<Instruction>(V));
141 //===----------------------------------------------------------------------===//
143 //===----------------------------------------------------------------------===//
145 /// FreeInst - an instruction to deallocate memory
147 class FreeInst : public Instruction {
148 void init(Value *Ptr);
151 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
152 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
154 virtual Instruction *clone() const { return new FreeInst(Operands[0]); }
156 virtual bool mayWriteToMemory() const { return true; }
158 // Methods for support type inquiry through isa, cast, and dyn_cast:
159 static inline bool classof(const FreeInst *) { return true; }
160 static inline bool classof(const Instruction *I) {
161 return (I->getOpcode() == Instruction::Free);
163 static inline bool classof(const Value *V) {
164 return isa<Instruction>(V) && classof(cast<Instruction>(V));
169 //===----------------------------------------------------------------------===//
171 //===----------------------------------------------------------------------===//
173 /// LoadInst - an instruction for reading from memory
175 class LoadInst : public Instruction {
176 LoadInst(const LoadInst &LI) : Instruction(LI.getType(), Load) {
177 Volatile = LI.isVolatile();
179 Operands.push_back(Use(LI.Operands[0], this));
181 bool Volatile; // True if this is a volatile load
182 void init(Value *Ptr) {
184 Operands.push_back(Use(Ptr, this));
187 LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBefore);
188 LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAtEnd);
189 LoadInst(Value *Ptr, const std::string &Name = "", bool isVolatile = false,
190 Instruction *InsertBefore = 0);
191 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
192 BasicBlock *InsertAtEnd);
194 /// isVolatile - Return true if this is a load from a volatile memory
197 bool isVolatile() const { return Volatile; }
199 /// setVolatile - Specify whether this is a volatile load or not.
201 void setVolatile(bool V) { Volatile = V; }
203 virtual Instruction *clone() const { return new LoadInst(*this); }
205 virtual bool mayWriteToMemory() const { return isVolatile(); }
207 Value *getPointerOperand() { return getOperand(0); }
208 const Value *getPointerOperand() const { return getOperand(0); }
209 static unsigned getPointerOperandIndex() { return 0U; }
211 // Methods for support type inquiry through isa, cast, and dyn_cast:
212 static inline bool classof(const LoadInst *) { return true; }
213 static inline bool classof(const Instruction *I) {
214 return I->getOpcode() == Instruction::Load;
216 static inline bool classof(const Value *V) {
217 return isa<Instruction>(V) && classof(cast<Instruction>(V));
222 //===----------------------------------------------------------------------===//
224 //===----------------------------------------------------------------------===//
226 /// StoreInst - an instruction for storing to memory
228 class StoreInst : public Instruction {
229 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store) {
230 Volatile = SI.isVolatile();
232 Operands.push_back(Use(SI.Operands[0], this));
233 Operands.push_back(Use(SI.Operands[1], this));
235 bool Volatile; // True if this is a volatile store
236 void init(Value *Val, Value *Ptr) {
238 Operands.push_back(Use(Val, this));
239 Operands.push_back(Use(Ptr, this));
242 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
243 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
244 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
245 Instruction *InsertBefore = 0);
246 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
249 /// isVolatile - Return true if this is a load from a volatile memory
252 bool isVolatile() const { return Volatile; }
254 /// setVolatile - Specify whether this is a volatile load or not.
256 void setVolatile(bool V) { Volatile = V; }
258 virtual Instruction *clone() const { return new StoreInst(*this); }
260 virtual bool mayWriteToMemory() const { return true; }
262 Value *getPointerOperand() { return getOperand(1); }
263 const Value *getPointerOperand() const { return getOperand(1); }
264 static unsigned getPointerOperandIndex() { return 1U; }
266 // Methods for support type inquiry through isa, cast, and dyn_cast:
267 static inline bool classof(const StoreInst *) { return true; }
268 static inline bool classof(const Instruction *I) {
269 return I->getOpcode() == Instruction::Store;
271 static inline bool classof(const Value *V) {
272 return isa<Instruction>(V) && classof(cast<Instruction>(V));
277 //===----------------------------------------------------------------------===//
278 // GetElementPtrInst Class
279 //===----------------------------------------------------------------------===//
281 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
282 /// access elements of arrays and structs
284 class GetElementPtrInst : public Instruction {
285 GetElementPtrInst(const GetElementPtrInst &EPI)
286 : Instruction(reinterpret_cast<const Type*>(EPI.getType()), GetElementPtr) {
287 Operands.reserve(EPI.Operands.size());
288 for (unsigned i = 0, E = EPI.Operands.size(); i != E; ++i)
289 Operands.push_back(Use(EPI.Operands[i], this));
291 void init(Value *Ptr, const std::vector<Value*> &Idx);
294 GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
295 const std::string &Name = "", Instruction *InsertBefore =0);
296 GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
297 const std::string &Name, BasicBlock *InsertAtEnd);
298 virtual Instruction *clone() const { return new GetElementPtrInst(*this); }
300 // getType - Overload to return most specific pointer type...
301 inline const PointerType *getType() const {
302 return reinterpret_cast<const PointerType*>(Instruction::getType());
305 /// getIndexedType - Returns the type of the element that would be loaded with
306 /// a load instruction with the specified parameters.
308 /// A null type is returned if the indices are invalid for the specified
311 static const Type *getIndexedType(const Type *Ptr,
312 const std::vector<Value*> &Indices,
313 bool AllowStructLeaf = false);
315 inline op_iterator idx_begin() { return op_begin()+1; }
316 inline const_op_iterator idx_begin() const { return op_begin()+1; }
317 inline op_iterator idx_end() { return op_end(); }
318 inline const_op_iterator idx_end() const { return op_end(); }
320 Value *getPointerOperand() {
321 return getOperand(0);
323 const Value *getPointerOperand() const {
324 return getOperand(0);
326 static unsigned getPointerOperandIndex() {
327 return 0U; // get index for modifying correct operand
330 inline unsigned getNumIndices() const { // Note: always non-negative
331 return getNumOperands() - 1;
334 inline bool hasIndices() const {
335 return getNumOperands() > 1;
338 // Methods for support type inquiry through isa, cast, and dyn_cast:
339 static inline bool classof(const GetElementPtrInst *) { return true; }
340 static inline bool classof(const Instruction *I) {
341 return (I->getOpcode() == Instruction::GetElementPtr);
343 static inline bool classof(const Value *V) {
344 return isa<Instruction>(V) && classof(cast<Instruction>(V));
348 } // End llvm namespace
350 #endif // LLVM_IMEMORY_H