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 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) {
63 Operands.push_back(Use(Ptr, this));
67 //===----------------------------------------------------------------------===//
68 // StoreInst Implementation
69 //===----------------------------------------------------------------------===//
71 StoreInst::StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore)
72 : Instruction(Type::VoidTy, Store, "", InsertBefore) {
75 Operands.push_back(Use(Val, this));
76 Operands.push_back(Use(Ptr, this));
80 //===----------------------------------------------------------------------===//
81 // GetElementPtrInst Implementation
82 //===----------------------------------------------------------------------===//
84 // checkType - Simple wrapper function to give a better assertion failure
85 // message on bad indexes for a gep instruction.
87 static inline const Type *checkType(const Type *Ty) {
88 assert(Ty && "Invalid indices for type!");
92 GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
93 const std::string &Name, Instruction *InBe)
94 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
96 GetElementPtr, Name, InBe) {
97 assert(getIndexedType(Ptr->getType(), Idx, true) && "gep operands invalid!");
98 Operands.reserve(1+Idx.size());
99 Operands.push_back(Use(Ptr, this));
101 for (unsigned i = 0, E = Idx.size(); i != E; ++i)
102 Operands.push_back(Use(Idx[i], this));
105 // getIndexedType - Returns the type of the element that would be loaded with
106 // a load instruction with the specified parameters.
108 // A null type is returned if the indices are invalid for the specified
111 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
112 const std::vector<Value*> &Idx,
113 bool AllowCompositeLeaf) {
114 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
116 // Handle the special case of the empty set index set...
117 if (Idx.empty()) return cast<PointerType>(Ptr)->getElementType();
120 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
121 if (Idx.size() == CurIDX) {
122 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
123 return 0; // Can't load a whole structure or array!?!?
126 Value *Index = Idx[CurIDX++];
127 if (!CT->indexValid(Index)) return 0;
128 Ptr = CT->getTypeAtIndex(Index);
130 return CurIDX == Idx.size() ? Ptr : 0;