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 // LoadInst Implementation
43 //===----------------------------------------------------------------------===//
45 LoadInst::LoadInst(Value *Ptr, const std::string &Name)
46 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
49 Operands.push_back(Use(Ptr, this));
53 //===----------------------------------------------------------------------===//
54 // StoreInst Implementation
55 //===----------------------------------------------------------------------===//
57 StoreInst::StoreInst(Value *Val, Value *Ptr)
58 : Instruction(Type::VoidTy, Store, "") {
61 Operands.push_back(Use(Val, this));
62 Operands.push_back(Use(Ptr, this));
66 //===----------------------------------------------------------------------===//
67 // GetElementPtrInst Implementation
68 //===----------------------------------------------------------------------===//
70 GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
71 const std::string &Name)
72 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
74 GetElementPtr, Name) {
75 assert(getIndexedType(Ptr->getType(), Idx, true) && "gep 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));
83 // getIndexedType - Returns the type of the element that would be loaded with
84 // a load instruction with the specified parameters.
86 // A null type is returned if the indices are invalid for the specified
89 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
90 const std::vector<Value*> &Idx,
91 bool AllowCompositeLeaf) {
92 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
94 // Handle the special case of the empty set index set...
95 if (Idx.empty()) return cast<PointerType>(Ptr)->getElementType();
98 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
99 if (Idx.size() == CurIDX) {
100 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
101 return 0; // Can't load a whole structure or array!?!?
104 Value *Index = Idx[CurIDX++];
105 if (!CT->indexValid(Index)) return 0;
106 Ptr = CT->getTypeAtIndex(Index);
108 return CurIDX == Idx.size() ? Ptr : 0;
112 //===----------------------------------------------------------------------===//
113 // FreeInst Implementation
114 //===----------------------------------------------------------------------===//
116 FreeInst::FreeInst(Value *Ptr) : Instruction(Type::VoidTy, Free, "") {
117 assert(isa<PointerType>(Ptr->getType()) && "Can't free nonpointer!");
119 Operands.push_back(Use(Ptr, this));