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"
20 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
21 const std::string &Name, Instruction *InsertBef)
22 : Instruction(PointerType::get(Ty), iTy, Name, InsertBef) {
24 // ArraySize defaults to 1.
25 if (!ArraySize) ArraySize = ConstantUInt::get(Type::UIntTy, 1);
28 assert(ArraySize->getType() == Type::UIntTy &&
29 "Malloc/Allocation array size != UIntTy!");
31 Operands.push_back(Use(ArraySize, this));
34 bool AllocationInst::isArrayAllocation() const {
35 return getOperand(0) != ConstantUInt::get(Type::UIntTy, 1);
38 const Type *AllocationInst::getAllocatedType() const {
39 return getType()->getElementType();
42 AllocaInst::AllocaInst(const AllocaInst &AI)
43 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
44 Instruction::Alloca) {
47 MallocInst::MallocInst(const MallocInst &MI)
48 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
49 Instruction::Malloc) {
52 //===----------------------------------------------------------------------===//
53 // FreeInst Implementation
54 //===----------------------------------------------------------------------===//
56 FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
57 : Instruction(Type::VoidTy, Free, "", InsertBefore) {
58 assert(isa<PointerType>(Ptr->getType()) && "Can't free nonpointer!");
60 Operands.push_back(Use(Ptr, this));
64 //===----------------------------------------------------------------------===//
65 // LoadInst Implementation
66 //===----------------------------------------------------------------------===//
68 LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
69 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
70 Load, Name, InsertBef), Volatile(false) {
72 Operands.push_back(Use(Ptr, this));
75 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
76 Instruction *InsertBef)
77 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
78 Load, Name, InsertBef), Volatile(isVolatile) {
80 Operands.push_back(Use(Ptr, this));
83 //===----------------------------------------------------------------------===//
84 // StoreInst Implementation
85 //===----------------------------------------------------------------------===//
87 StoreInst::StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore)
88 : Instruction(Type::VoidTy, Store, "", InsertBefore), Volatile(false) {
91 Operands.push_back(Use(Val, this));
92 Operands.push_back(Use(Ptr, this));
95 StoreInst::StoreInst(Value *Val, Value *Ptr, bool isVolatile,
96 Instruction *InsertBefore)
97 : Instruction(Type::VoidTy, Store, "", InsertBefore), Volatile(isVolatile) {
100 Operands.push_back(Use(Val, this));
101 Operands.push_back(Use(Ptr, this));
105 //===----------------------------------------------------------------------===//
106 // GetElementPtrInst Implementation
107 //===----------------------------------------------------------------------===//
109 // checkType - Simple wrapper function to give a better assertion failure
110 // message on bad indexes for a gep instruction.
112 static inline const Type *checkType(const Type *Ty) {
113 assert(Ty && "Invalid indices for type!");
117 GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
118 const std::string &Name, Instruction *InBe)
119 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
121 GetElementPtr, Name, InBe) {
122 Operands.reserve(1+Idx.size());
123 Operands.push_back(Use(Ptr, this));
125 for (unsigned i = 0, E = Idx.size(); i != E; ++i)
126 Operands.push_back(Use(Idx[i], this));
129 // getIndexedType - Returns the type of the element that would be loaded with
130 // a load instruction with the specified parameters.
132 // A null type is returned if the indices are invalid for the specified
135 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
136 const std::vector<Value*> &Idx,
137 bool AllowCompositeLeaf) {
138 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
140 // Handle the special case of the empty set index set...
141 if (Idx.empty()) return cast<PointerType>(Ptr)->getElementType();
144 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
145 if (Idx.size() == CurIdx) {
146 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
147 return 0; // Can't load a whole structure or array!?!?
150 Value *Index = Idx[CurIdx++];
151 if (isa<PointerType>(CT) && CurIdx != 1)
152 return 0; // Can only index into pointer types at the first index!
153 if (!CT->indexValid(Index)) return 0;
154 Ptr = CT->getTypeAtIndex(Index);
156 return CurIdx == Idx.size() ? Ptr : 0;