1 //===-- iMemory.cpp - Implement Memory instructions -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the various memory related classes defined in iMemory.h
12 //===----------------------------------------------------------------------===//
14 #include "llvm/iMemory.h"
15 #include "llvm/Constants.h"
16 #include "llvm/DerivedTypes.h"
18 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
19 const std::string &Name, Instruction *InsertBef)
20 : Instruction(PointerType::get(Ty), iTy, Name, InsertBef) {
22 // ArraySize defaults to 1.
23 if (!ArraySize) ArraySize = ConstantUInt::get(Type::UIntTy, 1);
26 assert(ArraySize->getType() == Type::UIntTy &&
27 "Malloc/Allocation array size != UIntTy!");
29 Operands.push_back(Use(ArraySize, this));
32 bool AllocationInst::isArrayAllocation() const {
33 return getOperand(0) != ConstantUInt::get(Type::UIntTy, 1);
36 const Type *AllocationInst::getAllocatedType() const {
37 return getType()->getElementType();
40 AllocaInst::AllocaInst(const AllocaInst &AI)
41 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
42 Instruction::Alloca) {
45 MallocInst::MallocInst(const MallocInst &MI)
46 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
47 Instruction::Malloc) {
50 //===----------------------------------------------------------------------===//
51 // FreeInst Implementation
52 //===----------------------------------------------------------------------===//
54 FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
55 : Instruction(Type::VoidTy, Free, "", InsertBefore) {
56 assert(isa<PointerType>(Ptr->getType()) && "Can't free nonpointer!");
58 Operands.push_back(Use(Ptr, this));
62 //===----------------------------------------------------------------------===//
63 // LoadInst Implementation
64 //===----------------------------------------------------------------------===//
66 LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
67 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
68 Load, Name, InsertBef), Volatile(false) {
70 Operands.push_back(Use(Ptr, this));
73 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
74 Instruction *InsertBef)
75 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
76 Load, Name, InsertBef), Volatile(isVolatile) {
78 Operands.push_back(Use(Ptr, this));
81 //===----------------------------------------------------------------------===//
82 // StoreInst Implementation
83 //===----------------------------------------------------------------------===//
85 StoreInst::StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore)
86 : Instruction(Type::VoidTy, Store, "", InsertBefore), Volatile(false) {
89 Operands.push_back(Use(Val, this));
90 Operands.push_back(Use(Ptr, this));
93 StoreInst::StoreInst(Value *Val, Value *Ptr, bool isVolatile,
94 Instruction *InsertBefore)
95 : Instruction(Type::VoidTy, Store, "", InsertBefore), Volatile(isVolatile) {
98 Operands.push_back(Use(Val, this));
99 Operands.push_back(Use(Ptr, this));
103 //===----------------------------------------------------------------------===//
104 // GetElementPtrInst Implementation
105 //===----------------------------------------------------------------------===//
107 // checkType - Simple wrapper function to give a better assertion failure
108 // message on bad indexes for a gep instruction.
110 static inline const Type *checkType(const Type *Ty) {
111 assert(Ty && "Invalid indices for type!");
115 GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
116 const std::string &Name, Instruction *InBe)
117 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
119 GetElementPtr, Name, InBe) {
120 Operands.reserve(1+Idx.size());
121 Operands.push_back(Use(Ptr, this));
123 for (unsigned i = 0, E = Idx.size(); i != E; ++i)
124 Operands.push_back(Use(Idx[i], this));
127 // getIndexedType - Returns the type of the element that would be loaded with
128 // a load instruction with the specified parameters.
130 // A null type is returned if the indices are invalid for the specified
133 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
134 const std::vector<Value*> &Idx,
135 bool AllowCompositeLeaf) {
136 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
138 // Handle the special case of the empty set index set...
139 if (Idx.empty()) return cast<PointerType>(Ptr)->getElementType();
142 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
143 if (Idx.size() == CurIdx) {
144 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
145 return 0; // Can't load a whole structure or array!?!?
148 Value *Index = Idx[CurIdx++];
149 if (isa<PointerType>(CT) && CurIdx != 1)
150 return 0; // Can only index into pointer types at the first index!
151 if (!CT->indexValid(Index)) return 0;
152 Ptr = CT->getTypeAtIndex(Index);
154 return CurIdx == Idx.size() ? Ptr : 0;