1 //===-- llvm/iMemory.h - Memory Operator node definitions --------*- C++ -*--=//
3 // This file contains the declarations of all of the memory related operators.
4 // This includes: malloc, free, alloca, load, store, getfield, putfield
6 //===----------------------------------------------------------------------===//
11 #include "llvm/Instruction.h"
12 #include "llvm/DerivedTypes.h"
14 //===----------------------------------------------------------------------===//
15 // AllocationInst Class
16 //===----------------------------------------------------------------------===//
18 // AllocationInst - This class is the common base class of MallocInst and
21 class AllocationInst : public Instruction {
23 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
24 const string &Name = "")
25 : Instruction(Ty, iTy, Name) {
26 assert(Ty->isPointerType() && "Can't allocate a non pointer type!");
29 // Make sure they didn't try to specify a size for !(unsized array) type
30 assert((getType()->getValueType()->isArrayType() &&
31 ((const ArrayType*)getType()->getValueType())->isUnsized()) &&
32 "Trying to allocate something other than unsized array, with size!");
35 Operands.push_back(Use(ArraySize, this));
39 // getType - Overload to return most specific pointer type...
40 inline const PointerType *getType() const {
41 return (const PointerType*)Instruction::getType();
44 virtual Instruction *clone() const = 0;
48 //===----------------------------------------------------------------------===//
50 //===----------------------------------------------------------------------===//
52 class MallocInst : public AllocationInst {
54 MallocInst(const Type *Ty, Value *ArraySize = 0, const string &Name = "")
55 : AllocationInst(Ty, ArraySize, Malloc, Name) {}
57 virtual Instruction *clone() const {
58 return new MallocInst(getType(), Operands.size() ? Operands[1] : 0);
61 virtual const char *getOpcodeName() const { return "malloc"; }
65 //===----------------------------------------------------------------------===//
67 //===----------------------------------------------------------------------===//
69 class AllocaInst : public AllocationInst {
71 AllocaInst(const Type *Ty, Value *ArraySize = 0, const string &Name = "")
72 : AllocationInst(Ty, ArraySize, Alloca, Name) {}
74 virtual Instruction *clone() const {
75 return new AllocaInst(getType(), Operands.size() ? Operands[1] : 0);
78 virtual const char *getOpcodeName() const { return "alloca"; }
82 //===----------------------------------------------------------------------===//
84 //===----------------------------------------------------------------------===//
86 class FreeInst : public Instruction {
88 FreeInst(Value *Ptr, const string &Name = "")
89 : Instruction(Type::VoidTy, Free, Name) {
90 assert(Ptr->getType()->isPointerType() && "Can't free nonpointer!");
92 Operands.push_back(Use(Ptr, this));
95 virtual Instruction *clone() const { return new FreeInst(Operands[0]); }
97 virtual const char *getOpcodeName() const { return "free"; }
99 virtual bool hasSideEffects() const { return true; }
103 //===----------------------------------------------------------------------===//
104 // MemAccessInst Class
105 //===----------------------------------------------------------------------===//
107 // MemAccessInst - Common base class of LoadInst, StoreInst, and
108 // GetElementPtrInst...
110 class MemAccessInst : public Instruction {
112 inline MemAccessInst(const Type *Ty, unsigned Opcode, const string &Nam = "")
113 : Instruction(Ty, Opcode, Nam) {}
115 // getIndexedType - Returns the type of the element that would be loaded with
116 // a load instruction with the specified parameters.
118 // A null type is returned if the indices are invalid for the specified
121 static const Type *getIndexedType(const Type *Ptr,
122 const vector<ConstPoolVal*> &Indices,
123 bool AllowStructLeaf = false);
127 //===----------------------------------------------------------------------===//
129 //===----------------------------------------------------------------------===//
131 class LoadInst : public MemAccessInst {
132 LoadInst(const LoadInst &LI) : MemAccessInst(LI.getType(), Load) {
133 Operands.reserve(LI.Operands.size());
134 for (unsigned i = 0, E = LI.Operands.size(); i != E; ++i)
135 Operands.push_back(Use(LI.Operands[i], this));
138 LoadInst(Value *Ptr, const vector<ConstPoolVal*> &Idx,
139 const string &Name = "");
140 virtual Instruction *clone() const { return new LoadInst(*this); }
141 virtual const char *getOpcodeName() const { return "load"; }
145 //===----------------------------------------------------------------------===//
147 //===----------------------------------------------------------------------===//
149 class StoreInst : public MemAccessInst {
150 StoreInst(const StoreInst &SI) : MemAccessInst(SI.getType(), Store) {
151 Operands.reserve(SI.Operands.size());
152 for (unsigned i = 0, E = SI.Operands.size(); i != E; ++i)
153 Operands.push_back(Use(SI.Operands[i], this));
156 StoreInst(Value *Val, Value *Ptr, const vector<ConstPoolVal*> &Idx,
157 const string &Name = "");
158 virtual Instruction *clone() const { return new StoreInst(*this); }
159 virtual const char *getOpcodeName() const { return "store"; }
161 virtual bool hasSideEffects() const { return true; }
165 //===----------------------------------------------------------------------===//
166 // GetElementPtrInst Class
167 //===----------------------------------------------------------------------===//
169 class GetElementPtrInst : public MemAccessInst {
170 GetElementPtrInst(const GetElementPtrInst &EPI)
171 : MemAccessInst(EPI.getType(), GetElementPtr) {
172 Operands.reserve(EPI.Operands.size());
173 for (unsigned i = 0, E = EPI.Operands.size(); i != E; ++i)
174 Operands.push_back(Use(EPI.Operands[i], this));
177 GetElementPtrInst(Value *Ptr, const vector<ConstPoolVal*> &Idx,
178 const string &Name = "");
179 virtual Instruction *clone() const { return new GetElementPtrInst(*this); }
180 virtual const char *getOpcodeName() const { return "getelementptr"; }
183 #endif // LLVM_IMEMORY_H