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 allocation
43 // 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...
49 inline const PointerType *getType() const {
50 return reinterpret_cast<const PointerType*>(Instruction::getType());
53 // getAllocatedType - Return the type that is being allocated by the
56 const Type *getAllocatedType() const;
58 virtual Instruction *clone() const = 0;
60 // Methods for support type inquiry through isa, cast, and dyn_cast:
61 static inline bool classof(const AllocationInst *) { return true; }
62 static inline bool classof(const Instruction *I) {
63 return I->getOpcode() == Instruction::Alloca ||
64 I->getOpcode() == Instruction::Malloc;
66 static inline bool classof(const Value *V) {
67 return isa<Instruction>(V) && classof(cast<Instruction>(V));
72 //===----------------------------------------------------------------------===//
74 //===----------------------------------------------------------------------===//
76 class MallocInst : public AllocationInst {
77 MallocInst(const MallocInst &MI);
79 MallocInst(const Type *Ty, Value *ArraySize = 0, const std::string &Name = "",
80 Instruction *InsertBefore = 0)
81 : AllocationInst(Ty, ArraySize, Malloc, Name, InsertBefore) {}
83 virtual Instruction *clone() const {
84 return new MallocInst(*this);
87 // Methods for support type inquiry through isa, cast, and dyn_cast:
88 static inline bool classof(const MallocInst *) { return true; }
89 static inline bool classof(const Instruction *I) {
90 return (I->getOpcode() == Instruction::Malloc);
92 static inline bool classof(const Value *V) {
93 return isa<Instruction>(V) && classof(cast<Instruction>(V));
98 //===----------------------------------------------------------------------===//
100 //===----------------------------------------------------------------------===//
102 class AllocaInst : public AllocationInst {
103 AllocaInst(const AllocaInst &);
105 AllocaInst(const Type *Ty, Value *ArraySize = 0, const std::string &Name = "",
106 Instruction *InsertBefore = 0)
107 : AllocationInst(Ty, ArraySize, Alloca, Name, InsertBefore) {}
109 virtual Instruction *clone() const {
110 return new AllocaInst(*this);
113 // Methods for support type inquiry through isa, cast, and dyn_cast:
114 static inline bool classof(const AllocaInst *) { return true; }
115 static inline bool classof(const Instruction *I) {
116 return (I->getOpcode() == Instruction::Alloca);
118 static inline bool classof(const Value *V) {
119 return isa<Instruction>(V) && classof(cast<Instruction>(V));
124 //===----------------------------------------------------------------------===//
126 //===----------------------------------------------------------------------===//
128 struct FreeInst : public Instruction {
129 FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
131 virtual Instruction *clone() const { return new FreeInst(Operands[0]); }
133 virtual bool mayWriteToMemory() const { return true; }
135 // Methods for support type inquiry through isa, cast, and dyn_cast:
136 static inline bool classof(const FreeInst *) { return true; }
137 static inline bool classof(const Instruction *I) {
138 return (I->getOpcode() == Instruction::Free);
140 static inline bool classof(const Value *V) {
141 return isa<Instruction>(V) && classof(cast<Instruction>(V));
146 //===----------------------------------------------------------------------===//
148 //===----------------------------------------------------------------------===//
150 class LoadInst : public Instruction {
151 LoadInst(const LoadInst &LI) : Instruction(LI.getType(), Load) {
152 Volatile = LI.isVolatile();
154 Operands.push_back(Use(LI.Operands[0], this));
156 bool Volatile; // True if this is a volatile load
158 LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBefore);
159 LoadInst(Value *Ptr, const std::string &Name = "", bool isVolatile = false,
160 Instruction *InsertBefore = 0);
162 /// isVolatile - Return true if this is a load from a volatile memory
164 bool isVolatile() const { return Volatile; }
166 /// setVolatile - Specify whether this is a volatile load or not.
168 void setVolatile(bool V) { Volatile = V; }
170 virtual Instruction *clone() const { return new LoadInst(*this); }
172 virtual bool mayWriteToMemory() const { return isVolatile(); }
174 Value *getPointerOperand() { return getOperand(0); }
175 const Value *getPointerOperand() const { return getOperand(0); }
176 static unsigned getPointerOperandIndex() { return 0U; }
178 // Methods for support type inquiry through isa, cast, and dyn_cast:
179 static inline bool classof(const LoadInst *) { return true; }
180 static inline bool classof(const Instruction *I) {
181 return I->getOpcode() == Instruction::Load;
183 static inline bool classof(const Value *V) {
184 return isa<Instruction>(V) && classof(cast<Instruction>(V));
189 //===----------------------------------------------------------------------===//
191 //===----------------------------------------------------------------------===//
193 class StoreInst : public Instruction {
194 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store) {
195 Volatile = SI.isVolatile();
197 Operands.push_back(Use(SI.Operands[0], this));
198 Operands.push_back(Use(SI.Operands[1], this));
200 bool Volatile; // True if this is a volatile store
202 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
203 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
204 Instruction *InsertBefore = 0);
207 /// isVolatile - Return true if this is a load from a volatile memory
209 bool isVolatile() const { return Volatile; }
211 /// setVolatile - Specify whether this is a volatile load or not.
213 void setVolatile(bool V) { Volatile = V; }
215 virtual Instruction *clone() const { return new StoreInst(*this); }
217 virtual bool mayWriteToMemory() const { return true; }
219 Value *getPointerOperand() { return getOperand(1); }
220 const Value *getPointerOperand() const { return getOperand(1); }
221 static unsigned getPointerOperandIndex() { return 1U; }
223 // Methods for support type inquiry through isa, cast, and dyn_cast:
224 static inline bool classof(const StoreInst *) { return true; }
225 static inline bool classof(const Instruction *I) {
226 return I->getOpcode() == Instruction::Store;
228 static inline bool classof(const Value *V) {
229 return isa<Instruction>(V) && classof(cast<Instruction>(V));
234 //===----------------------------------------------------------------------===//
235 // GetElementPtrInst Class
236 //===----------------------------------------------------------------------===//
238 class GetElementPtrInst : public Instruction {
239 GetElementPtrInst(const GetElementPtrInst &EPI)
240 : Instruction(reinterpret_cast<const Type*>(EPI.getType()), GetElementPtr) {
241 Operands.reserve(EPI.Operands.size());
242 for (unsigned i = 0, E = EPI.Operands.size(); i != E; ++i)
243 Operands.push_back(Use(EPI.Operands[i], this));
246 GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
247 const std::string &Name = "", Instruction *InsertBefore =0);
248 virtual Instruction *clone() const { return new GetElementPtrInst(*this); }
250 // getType - Overload to return most specific pointer type...
251 inline const PointerType *getType() const {
252 return reinterpret_cast<const PointerType*>(Instruction::getType());
255 /// getIndexedType - Returns the type of the element that would be loaded with
256 /// a load instruction with the specified parameters.
258 /// A null type is returned if the indices are invalid for the specified
261 static const Type *getIndexedType(const Type *Ptr,
262 const std::vector<Value*> &Indices,
263 bool AllowStructLeaf = false);
265 inline op_iterator idx_begin() {
268 inline const_op_iterator idx_begin() const {
271 inline op_iterator idx_end() { return op_end(); }
272 inline const_op_iterator idx_end() const { return op_end(); }
274 Value *getPointerOperand() {
275 return getOperand(0);
277 const Value *getPointerOperand() const {
278 return getOperand(0);
280 static unsigned getPointerOperandIndex() {
281 return 0U; // get index for modifying correct operand
284 inline unsigned getNumIndices() const { // Note: always non-negative
285 return getNumOperands() - 1;
288 inline bool hasIndices() const {
289 return getNumOperands() > 1;
292 // Methods for support type inquiry through isa, cast, and dyn_cast:
293 static inline bool classof(const GetElementPtrInst *) { return true; }
294 static inline bool classof(const Instruction *I) {
295 return (I->getOpcode() == Instruction::GetElementPtr);
297 static inline bool classof(const Value *V) {
298 return isa<Instruction>(V) && classof(cast<Instruction>(V));
302 } // End llvm namespace
304 #endif // LLVM_IMEMORY_H