1 //===-- iMemory.cpp - Implement Memory instructions --------------*- C++ -*--=//
3 // This file implements the various memory related classes defined in iMemory.h
5 //===----------------------------------------------------------------------===//
7 #include "llvm/iMemory.h"
8 #include "llvm/Constants.h"
9 #include "llvm/DerivedTypes.h"
11 static inline const Type *checkType(const Type *Ty) {
12 assert(Ty && "Invalid indices for type!");
16 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
17 const std::string &Name)
18 : Instruction(Ty, iTy, Name) {
19 assert(isa<PointerType>(Ty) && "Can't allocate a non pointer type!");
21 // ArraySize defaults to 1.
22 if (!ArraySize) ArraySize = ConstantUInt::get(Type::UIntTy, 1);
25 assert(ArraySize->getType() == Type::UIntTy &&
26 "Malloc/Allocation array size != UIntTy!");
28 Operands.push_back(Use(ArraySize, this));
31 bool AllocationInst::isArrayAllocation() const {
32 return getNumOperands() == 1 &&
33 getOperand(0) != ConstantUInt::get(Type::UIntTy, 1);
36 const Type *AllocationInst::getAllocatedType() const {
37 return getType()->getElementType();
41 //===----------------------------------------------------------------------===//
42 // MemAccessInst Implementation
43 //===----------------------------------------------------------------------===//
45 // getIndexedType - Returns the type of the element that would be loaded with
46 // a load instruction with the specified parameters.
48 // A null type is returned if the indices are invalid for the specified
51 const Type* MemAccessInst::getIndexedType(const Type *Ptr,
52 const std::vector<Value*> &Idx,
53 bool AllowCompositeLeaf) {
54 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
56 // Handle the special case of the empty set index set...
57 if (Idx.empty()) return cast<PointerType>(Ptr)->getElementType();
60 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
61 if (Idx.size() == CurIDX) {
62 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
63 return 0; // Can't load a whole structure or array!?!?
66 Value *Index = Idx[CurIDX++];
67 if (!CT->indexValid(Index)) return 0;
68 Ptr = CT->getTypeAtIndex(Index);
70 return CurIDX == Idx.size() ? Ptr : 0;
74 //===----------------------------------------------------------------------===//
75 // LoadInst Implementation
76 //===----------------------------------------------------------------------===//
78 LoadInst::LoadInst(Value *Ptr, const std::vector<Value*> &Idx,
79 const std::string &Name)
80 : MemAccessInst(checkType(getIndexedType(Ptr->getType(), Idx)), Load, Name) {
81 assert(getIndexedType(Ptr->getType(), Idx) && "Load operands invalid!");
82 Operands.reserve(1+Idx.size());
83 Operands.push_back(Use(Ptr, this));
85 for (unsigned i = 0, E = Idx.size(); i != E; ++i)
86 Operands.push_back(Use(Idx[i], this));
90 LoadInst::LoadInst(Value *Ptr, const std::string &Name)
91 : MemAccessInst(cast<PointerType>(Ptr->getType())->getElementType(),
94 Operands.push_back(Use(Ptr, this));
98 //===----------------------------------------------------------------------===//
99 // StoreInst Implementation
100 //===----------------------------------------------------------------------===//
102 StoreInst::StoreInst(Value *Val, Value *Ptr, const std::vector<Value*> &Idx)
103 : MemAccessInst(Type::VoidTy, Store, "") {
104 assert(getIndexedType(Ptr->getType(), Idx) && "Store operands invalid!");
106 Operands.reserve(2+Idx.size());
107 Operands.push_back(Use(Val, this));
108 Operands.push_back(Use(Ptr, this));
110 for (unsigned i = 0, E = Idx.size(); i != E; ++i)
111 Operands.push_back(Use(Idx[i], this));
114 StoreInst::StoreInst(Value *Val, Value *Ptr)
115 : MemAccessInst(Type::VoidTy, Store, "") {
118 Operands.push_back(Use(Val, this));
119 Operands.push_back(Use(Ptr, this));
123 //===----------------------------------------------------------------------===//
124 // GetElementPtrInst Implementation
125 //===----------------------------------------------------------------------===//
127 GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
128 const std::string &Name)
129 : MemAccessInst(PointerType::get(checkType(getIndexedType(Ptr->getType(),
131 GetElementPtr, Name) {
132 assert(getIndexedType(Ptr->getType(), Idx, true) && "gep operands invalid!");
133 Operands.reserve(1+Idx.size());
134 Operands.push_back(Use(Ptr, this));
136 for (unsigned i = 0, E = Idx.size(); i != E; ++i)
137 Operands.push_back(Use(Idx[i], this));
141 //===----------------------------------------------------------------------===//
142 // FreeInst Implementation
143 //===----------------------------------------------------------------------===//
145 FreeInst::FreeInst(Value *Ptr) : Instruction(Type::VoidTy, Free, "") {
146 assert(isa<PointerType>(Ptr->getType()) && "Can't free nonpointer!");
148 Operands.push_back(Use(Ptr, this));