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();
178 init(LI.Operands[0]);
180 bool Volatile; // True if this is a volatile load
181 void init(Value *Ptr);
183 LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBefore);
184 LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAtEnd);
185 LoadInst(Value *Ptr, const std::string &Name = "", bool isVolatile = false,
186 Instruction *InsertBefore = 0);
187 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
188 BasicBlock *InsertAtEnd);
190 /// isVolatile - Return true if this is a load from a volatile memory
193 bool isVolatile() const { return Volatile; }
195 /// setVolatile - Specify whether this is a volatile load or not.
197 void setVolatile(bool V) { Volatile = V; }
199 virtual Instruction *clone() const { return new LoadInst(*this); }
201 virtual bool mayWriteToMemory() const { return isVolatile(); }
203 Value *getPointerOperand() { return getOperand(0); }
204 const Value *getPointerOperand() const { return getOperand(0); }
205 static unsigned getPointerOperandIndex() { return 0U; }
207 // Methods for support type inquiry through isa, cast, and dyn_cast:
208 static inline bool classof(const LoadInst *) { return true; }
209 static inline bool classof(const Instruction *I) {
210 return I->getOpcode() == Instruction::Load;
212 static inline bool classof(const Value *V) {
213 return isa<Instruction>(V) && classof(cast<Instruction>(V));
218 //===----------------------------------------------------------------------===//
220 //===----------------------------------------------------------------------===//
222 /// StoreInst - an instruction for storing to memory
224 class StoreInst : public Instruction {
225 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store) {
226 Volatile = SI.isVolatile();
227 init(SI.Operands[0], SI.Operands[1]);
229 bool Volatile; // True if this is a volatile store
230 void init(Value *Val, Value *Ptr);
232 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
233 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
234 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
235 Instruction *InsertBefore = 0);
236 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
239 /// isVolatile - Return true if this is a load from a volatile memory
242 bool isVolatile() const { return Volatile; }
244 /// setVolatile - Specify whether this is a volatile load or not.
246 void setVolatile(bool V) { Volatile = V; }
248 virtual Instruction *clone() const { return new StoreInst(*this); }
250 virtual bool mayWriteToMemory() const { return true; }
252 Value *getPointerOperand() { return getOperand(1); }
253 const Value *getPointerOperand() const { return getOperand(1); }
254 static unsigned getPointerOperandIndex() { return 1U; }
256 // Methods for support type inquiry through isa, cast, and dyn_cast:
257 static inline bool classof(const StoreInst *) { return true; }
258 static inline bool classof(const Instruction *I) {
259 return I->getOpcode() == Instruction::Store;
261 static inline bool classof(const Value *V) {
262 return isa<Instruction>(V) && classof(cast<Instruction>(V));
267 //===----------------------------------------------------------------------===//
268 // GetElementPtrInst Class
269 //===----------------------------------------------------------------------===//
271 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
272 /// access elements of arrays and structs
274 class GetElementPtrInst : public Instruction {
275 GetElementPtrInst(const GetElementPtrInst &EPI)
276 : Instruction((static_cast<const Instruction*>(&EPI)->getType()),
278 Operands.reserve(EPI.Operands.size());
279 for (unsigned i = 0, E = EPI.Operands.size(); i != E; ++i)
280 Operands.push_back(Use(EPI.Operands[i], this));
282 void init(Value *Ptr, const std::vector<Value*> &Idx);
283 void init(Value *Ptr, Value *Idx0, Value *Idx1);
285 /// Constructors - Create a getelementptr instruction with a base pointer an
286 /// list of indices. The first ctor can optionally insert before an existing
287 /// instruction, the second appends the new instruction to the specified
289 GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
290 const std::string &Name = "", Instruction *InsertBefore =0);
291 GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
292 const std::string &Name, BasicBlock *InsertAtEnd);
294 /// Constructors - These two constructors are convenience methods because two
295 /// index getelementptr instructions are so common.
296 GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
297 const std::string &Name = "", Instruction *InsertBefore =0);
298 GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
299 const std::string &Name, BasicBlock *InsertAtEnd);
301 virtual Instruction *clone() const { return new GetElementPtrInst(*this); }
303 // getType - Overload to return most specific pointer type...
304 inline const PointerType *getType() const {
305 return reinterpret_cast<const PointerType*>(Instruction::getType());
308 /// getIndexedType - Returns the type of the element that would be loaded with
309 /// a load instruction with the specified parameters.
311 /// A null type is returned if the indices are invalid for the specified
314 static const Type *getIndexedType(const Type *Ptr,
315 const std::vector<Value*> &Indices,
316 bool AllowStructLeaf = false);
317 static const Type *getIndexedType(const Type *Ptr, Value *Idx0, Value *Idx1,
318 bool AllowStructLeaf = false);
320 inline op_iterator idx_begin() { return op_begin()+1; }
321 inline const_op_iterator idx_begin() const { return op_begin()+1; }
322 inline op_iterator idx_end() { return op_end(); }
323 inline const_op_iterator idx_end() const { return op_end(); }
325 Value *getPointerOperand() {
326 return getOperand(0);
328 const Value *getPointerOperand() const {
329 return getOperand(0);
331 static unsigned getPointerOperandIndex() {
332 return 0U; // get index for modifying correct operand
335 inline unsigned getNumIndices() const { // Note: always non-negative
336 return getNumOperands() - 1;
339 inline bool hasIndices() const {
340 return getNumOperands() > 1;
343 // Methods for support type inquiry through isa, cast, and dyn_cast:
344 static inline bool classof(const GetElementPtrInst *) { return true; }
345 static inline bool classof(const Instruction *I) {
346 return (I->getOpcode() == Instruction::GetElementPtr);
348 static inline bool classof(const Value *V) {
349 return isa<Instruction>(V) && classof(cast<Instruction>(V));
353 } // End llvm namespace
355 #endif // LLVM_IMEMORY_H