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"
19 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
20 const std::string &Name, Instruction *InsertBef)
21 : Instruction(PointerType::get(Ty), iTy, Name, InsertBef) {
23 // ArraySize defaults to 1.
24 if (!ArraySize) ArraySize = ConstantUInt::get(Type::UIntTy, 1);
27 assert(ArraySize->getType() == Type::UIntTy &&
28 "Malloc/Allocation array size != UIntTy!");
30 Operands.push_back(Use(ArraySize, this));
33 bool AllocationInst::isArrayAllocation() const {
34 return getOperand(0) != ConstantUInt::get(Type::UIntTy, 1);
37 const Type *AllocationInst::getAllocatedType() const {
38 return getType()->getElementType();
41 AllocaInst::AllocaInst(const AllocaInst &AI)
42 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
43 Instruction::Alloca) {
46 MallocInst::MallocInst(const MallocInst &MI)
47 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
48 Instruction::Malloc) {
51 //===----------------------------------------------------------------------===//
52 // FreeInst Implementation
53 //===----------------------------------------------------------------------===//
55 FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
56 : Instruction(Type::VoidTy, Free, "", InsertBefore) {
57 assert(isa<PointerType>(Ptr->getType()) && "Can't free nonpointer!");
59 Operands.push_back(Use(Ptr, this));
63 //===----------------------------------------------------------------------===//
64 // LoadInst Implementation
65 //===----------------------------------------------------------------------===//
67 LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
68 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
69 Load, Name, InsertBef), Volatile(false) {
71 Operands.push_back(Use(Ptr, this));
74 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
75 Instruction *InsertBef)
76 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
77 Load, Name, InsertBef), Volatile(isVolatile) {
79 Operands.push_back(Use(Ptr, this));
82 //===----------------------------------------------------------------------===//
83 // StoreInst Implementation
84 //===----------------------------------------------------------------------===//
86 StoreInst::StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore)
87 : Instruction(Type::VoidTy, Store, "", InsertBefore), Volatile(false) {
90 Operands.push_back(Use(Val, this));
91 Operands.push_back(Use(Ptr, this));
94 StoreInst::StoreInst(Value *Val, Value *Ptr, bool isVolatile,
95 Instruction *InsertBefore)
96 : Instruction(Type::VoidTy, Store, "", InsertBefore), Volatile(isVolatile) {
99 Operands.push_back(Use(Val, this));
100 Operands.push_back(Use(Ptr, this));
104 //===----------------------------------------------------------------------===//
105 // GetElementPtrInst Implementation
106 //===----------------------------------------------------------------------===//
108 // checkType - Simple wrapper function to give a better assertion failure
109 // message on bad indexes for a gep instruction.
111 static inline const Type *checkType(const Type *Ty) {
112 assert(Ty && "Invalid indices for type!");
116 GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
117 const std::string &Name, Instruction *InBe)
118 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
120 GetElementPtr, Name, InBe) {
121 Operands.reserve(1+Idx.size());
122 Operands.push_back(Use(Ptr, this));
124 for (unsigned i = 0, E = Idx.size(); i != E; ++i)
125 Operands.push_back(Use(Idx[i], this));
128 // getIndexedType - Returns the type of the element that would be loaded with
129 // a load instruction with the specified parameters.
131 // A null type is returned if the indices are invalid for the specified
134 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
135 const std::vector<Value*> &Idx,
136 bool AllowCompositeLeaf) {
137 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
139 // Handle the special case of the empty set index set...
140 if (Idx.empty()) return cast<PointerType>(Ptr)->getElementType();
143 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
144 if (Idx.size() == CurIdx) {
145 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
146 return 0; // Can't load a whole structure or array!?!?
149 Value *Index = Idx[CurIdx++];
150 if (isa<PointerType>(CT) && CurIdx != 1)
151 return 0; // Can only index into pointer types at the first index!
152 if (!CT->indexValid(Index)) return 0;
153 Ptr = CT->getTypeAtIndex(Index);
155 return CurIdx == Idx.size() ? Ptr : 0;