1 //===-- iMemory.cpp - Implement Memory instructions -----------------------===//
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 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
12 const std::string &Name, Instruction *InsertBef)
13 : Instruction(PointerType::get(Ty), iTy, Name, InsertBef) {
15 // ArraySize defaults to 1.
16 if (!ArraySize) ArraySize = ConstantUInt::get(Type::UIntTy, 1);
19 assert(ArraySize->getType() == Type::UIntTy &&
20 "Malloc/Allocation array size != UIntTy!");
22 Operands.push_back(Use(ArraySize, this));
25 bool AllocationInst::isArrayAllocation() const {
26 return getOperand(0) != ConstantUInt::get(Type::UIntTy, 1);
29 const Type *AllocationInst::getAllocatedType() const {
30 return getType()->getElementType();
33 AllocaInst::AllocaInst(const AllocaInst &AI)
34 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
35 Instruction::Alloca) {
38 MallocInst::MallocInst(const MallocInst &MI)
39 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
40 Instruction::Malloc) {
43 //===----------------------------------------------------------------------===//
44 // FreeInst Implementation
45 //===----------------------------------------------------------------------===//
47 FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
48 : Instruction(Type::VoidTy, Free, "", InsertBefore) {
49 assert(isa<PointerType>(Ptr->getType()) && "Can't free nonpointer!");
51 Operands.push_back(Use(Ptr, this));
55 //===----------------------------------------------------------------------===//
56 // LoadInst Implementation
57 //===----------------------------------------------------------------------===//
59 LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
60 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
61 Load, Name, InsertBef), Volatile(false) {
63 Operands.push_back(Use(Ptr, this));
66 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
67 Instruction *InsertBef)
68 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
69 Load, Name, InsertBef), Volatile(isVolatile) {
71 Operands.push_back(Use(Ptr, this));
74 //===----------------------------------------------------------------------===//
75 // StoreInst Implementation
76 //===----------------------------------------------------------------------===//
78 StoreInst::StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore)
79 : Instruction(Type::VoidTy, Store, "", InsertBefore), Volatile(false) {
82 Operands.push_back(Use(Val, this));
83 Operands.push_back(Use(Ptr, this));
86 StoreInst::StoreInst(Value *Val, Value *Ptr, bool isVolatile,
87 Instruction *InsertBefore)
88 : Instruction(Type::VoidTy, Store, "", InsertBefore), Volatile(isVolatile) {
91 Operands.push_back(Use(Val, this));
92 Operands.push_back(Use(Ptr, this));
96 //===----------------------------------------------------------------------===//
97 // GetElementPtrInst Implementation
98 //===----------------------------------------------------------------------===//
100 // checkType - Simple wrapper function to give a better assertion failure
101 // message on bad indexes for a gep instruction.
103 static inline const Type *checkType(const Type *Ty) {
104 assert(Ty && "Invalid indices for type!");
108 GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
109 const std::string &Name, Instruction *InBe)
110 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
112 GetElementPtr, Name, InBe) {
113 Operands.reserve(1+Idx.size());
114 Operands.push_back(Use(Ptr, this));
116 for (unsigned i = 0, E = Idx.size(); i != E; ++i)
117 Operands.push_back(Use(Idx[i], this));
120 // getIndexedType - Returns the type of the element that would be loaded with
121 // a load instruction with the specified parameters.
123 // A null type is returned if the indices are invalid for the specified
126 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
127 const std::vector<Value*> &Idx,
128 bool AllowCompositeLeaf) {
129 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
131 // Handle the special case of the empty set index set...
132 if (Idx.empty()) return cast<PointerType>(Ptr)->getElementType();
135 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
136 if (Idx.size() == CurIdx) {
137 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
138 return 0; // Can't load a whole structure or array!?!?
141 Value *Index = Idx[CurIdx++];
142 if (isa<PointerType>(CT) && CurIdx != 1)
143 return 0; // Can only index into pointer types at the first index!
144 if (!CT->indexValid(Index)) return 0;
145 Ptr = CT->getTypeAtIndex(Index);
147 return CurIdx == Idx.size() ? Ptr : 0;