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/ConstantVals.h"
10 static inline const Type *checkType(const Type *Ty) {
11 assert(Ty && "Invalid indices for type!");
15 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
16 const std::string &Name = "")
17 : Instruction(Ty, iTy, Name) {
18 assert(Ty->isPointerType() && "Can't allocate a non pointer type!");
20 // ArraySize defaults to 1.
21 if (!ArraySize) ArraySize = ConstantUInt::get(Type::UIntTy, 1);
24 assert(ArraySize->getType() == Type::UIntTy &&
25 "Malloc/Allocation array size != UIntTy!");
27 Operands.push_back(Use(ArraySize, this));
30 bool AllocationInst::isArrayAllocation() const {
31 return getNumOperands() == 1 &&
32 getOperand(0) != ConstantUInt::get(Type::UIntTy, 1);
35 //===----------------------------------------------------------------------===//
36 // MemAccessInst Implementation
37 //===----------------------------------------------------------------------===//
39 // getIndexedType - Returns the type of the element that would be loaded with
40 // a load instruction with the specified parameters.
42 // A null type is returned if the indices are invalid for the specified
45 const Type* MemAccessInst::getIndexedType(const Type *Ptr,
46 const std::vector<Value*> &Idx,
47 bool AllowCompositeLeaf = false) {
48 if (!Ptr->isPointerType()) return 0; // Type isn't a pointer type!
50 // Handle the special case of the empty set index set...
51 if (Idx.empty()) return cast<PointerType>(Ptr)->getElementType();
54 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
55 if (Idx.size() == CurIDX) {
56 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
57 return 0; // Can't load a whole structure or array!?!?
60 Value *Index = Idx[CurIDX++];
61 if (!CT->indexValid(Index)) return 0;
62 Ptr = CT->getTypeAtIndex(Index);
64 return CurIDX == Idx.size() ? Ptr : 0;
68 //===----------------------------------------------------------------------===//
69 // LoadInst Implementation
70 //===----------------------------------------------------------------------===//
72 LoadInst::LoadInst(Value *Ptr, const std::vector<Value*> &Idx,
73 const std::string &Name = "")
74 : MemAccessInst(checkType(getIndexedType(Ptr->getType(), Idx)), Load, Name) {
75 assert(getIndexedType(Ptr->getType(), Idx) && "Load operands invalid!");
76 Operands.reserve(1+Idx.size());
77 Operands.push_back(Use(Ptr, this));
79 for (unsigned i = 0, E = Idx.size(); i != E; ++i)
80 Operands.push_back(Use(Idx[i], this));
84 LoadInst::LoadInst(Value *Ptr, const std::string &Name = "")
85 : MemAccessInst(cast<PointerType>(Ptr->getType())->getElementType(),
88 Operands.push_back(Use(Ptr, this));
92 //===----------------------------------------------------------------------===//
93 // StoreInst Implementation
94 //===----------------------------------------------------------------------===//
96 StoreInst::StoreInst(Value *Val, Value *Ptr, const std::vector<Value*> &Idx,
97 const std::string &Name = "")
98 : MemAccessInst(Type::VoidTy, Store, Name) {
99 assert(getIndexedType(Ptr->getType(), Idx) && "Store operands invalid!");
101 Operands.reserve(2+Idx.size());
102 Operands.push_back(Use(Val, this));
103 Operands.push_back(Use(Ptr, this));
105 for (unsigned i = 0, E = Idx.size(); i != E; ++i)
106 Operands.push_back(Use(Idx[i], this));
109 StoreInst::StoreInst(Value *Val, Value *Ptr, const std::string &Name = "")
110 : MemAccessInst(Type::VoidTy, Store, Name) {
113 Operands.push_back(Use(Val, this));
114 Operands.push_back(Use(Ptr, this));
118 //===----------------------------------------------------------------------===//
119 // GetElementPtrInst Implementation
120 //===----------------------------------------------------------------------===//
122 GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
123 const std::string &Name = "")
124 : MemAccessInst(PointerType::get(checkType(getIndexedType(Ptr->getType(),
126 GetElementPtr, Name) {
127 assert(getIndexedType(Ptr->getType(), Idx, true) && "gep operands invalid!");
128 Operands.reserve(1+Idx.size());
129 Operands.push_back(Use(Ptr, this));
131 for (unsigned i = 0, E = Idx.size(); i != E; ++i)
132 Operands.push_back(Use(Idx[i], this));
135 bool GetElementPtrInst::isStructSelector() const {
136 return ((PointerType*)Operands[0]->getType())->getElementType()->isStructType();