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 void AllocationInst::init(const Type *Ty, Value *ArraySize, unsigned iTy) {
20 // ArraySize defaults to 1.
21 if (!ArraySize) ArraySize = ConstantUInt::get(Type::UIntTy, 1);
24 assert(ArraySize->getType() == Type::UIntTy &&
25 "Malloc/Allocation array size != UIntTy!");
27 Operands.push_back(Use(ArraySize, this));
30 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
31 const std::string &Name,
32 Instruction *InsertBefore)
33 : Instruction(PointerType::get(Ty), iTy, Name, InsertBefore) {
34 init(Ty, ArraySize, iTy);
37 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
38 const std::string &Name,
39 BasicBlock *InsertAtEnd)
40 : Instruction(PointerType::get(Ty), iTy, Name, InsertAtEnd) {
41 init(Ty, ArraySize, iTy);
44 bool AllocationInst::isArrayAllocation() const {
45 return getOperand(0) != ConstantUInt::get(Type::UIntTy, 1);
48 const Type *AllocationInst::getAllocatedType() const {
49 return getType()->getElementType();
52 AllocaInst::AllocaInst(const AllocaInst &AI)
53 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
54 Instruction::Alloca) {
57 MallocInst::MallocInst(const MallocInst &MI)
58 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
59 Instruction::Malloc) {
62 //===----------------------------------------------------------------------===//
63 // FreeInst Implementation
64 //===----------------------------------------------------------------------===//
66 void FreeInst::init(Value *Ptr)
68 assert(Ptr && isa<PointerType>(Ptr->getType()) && "Can't free nonpointer!");
70 Operands.push_back(Use(Ptr, this));
73 FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
74 : Instruction(Type::VoidTy, Free, "", InsertBefore) {
78 FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
79 : Instruction(Type::VoidTy, Free, "", InsertAtEnd) {
84 //===----------------------------------------------------------------------===//
85 // LoadInst Implementation
86 //===----------------------------------------------------------------------===//
88 void LoadInst::init(Value *Ptr) {
89 assert(Ptr && isa<PointerType>(Ptr->getType()) &&
90 "Ptr must have pointer type.");
92 Operands.push_back(Use(Ptr, this));
95 LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
96 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
97 Load, Name, InsertBef), Volatile(false) {
101 LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
102 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
103 Load, Name, InsertAE), Volatile(false) {
107 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
108 Instruction *InsertBef)
109 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
110 Load, Name, InsertBef), Volatile(isVolatile) {
114 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
115 BasicBlock *InsertAE)
116 : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
117 Load, Name, InsertAE), Volatile(isVolatile) {
122 //===----------------------------------------------------------------------===//
123 // StoreInst Implementation
124 //===----------------------------------------------------------------------===//
126 StoreInst::StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore)
127 : Instruction(Type::VoidTy, Store, "", InsertBefore), Volatile(false) {
131 StoreInst::StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd)
132 : Instruction(Type::VoidTy, Store, "", InsertAtEnd), Volatile(false) {
136 StoreInst::StoreInst(Value *Val, Value *Ptr, bool isVolatile,
137 Instruction *InsertBefore)
138 : Instruction(Type::VoidTy, Store, "", InsertBefore), Volatile(isVolatile) {
142 StoreInst::StoreInst(Value *Val, Value *Ptr, bool isVolatile,
143 BasicBlock *InsertAtEnd)
144 : Instruction(Type::VoidTy, Store, "", InsertAtEnd), Volatile(isVolatile) {
148 void StoreInst::init(Value *Val, Value *Ptr) {
149 assert(isa<PointerType>(Ptr->getType()) &&
150 Val->getType() == cast<PointerType>(Ptr->getType())->getElementType()
151 && "Ptr must have pointer type.");
154 Operands.push_back(Use(Val, this));
155 Operands.push_back(Use(Ptr, this));
158 //===----------------------------------------------------------------------===//
159 // GetElementPtrInst Implementation
160 //===----------------------------------------------------------------------===//
162 // checkType - Simple wrapper function to give a better assertion failure
163 // message on bad indexes for a gep instruction.
165 static inline const Type *checkType(const Type *Ty) {
166 assert(Ty && "Invalid indices for type!");
170 void GetElementPtrInst::init(Value *Ptr, const std::vector<Value*> &Idx)
172 Operands.reserve(1+Idx.size());
173 Operands.push_back(Use(Ptr, this));
175 for (unsigned i = 0, E = Idx.size(); i != E; ++i)
176 Operands.push_back(Use(Idx[i], this));
179 void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) {
181 Operands.push_back(Use(Ptr, this));
182 Operands.push_back(Use(Idx0, this));
183 Operands.push_back(Use(Idx1, this));
186 GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
187 const std::string &Name, Instruction *InBe)
188 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
190 GetElementPtr, Name, InBe) {
194 GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
195 const std::string &Name, BasicBlock *IAE)
196 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
198 GetElementPtr, Name, IAE) {
202 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
203 const std::string &Name, Instruction *InBe)
204 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
206 GetElementPtr, Name, InBe) {
207 init(Ptr, Idx0, Idx1);
210 GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
211 const std::string &Name, BasicBlock *IAE)
212 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
214 GetElementPtr, Name, IAE) {
215 init(Ptr, Idx0, Idx1);
218 // getIndexedType - Returns the type of the element that would be loaded with
219 // a load instruction with the specified parameters.
221 // A null type is returned if the indices are invalid for the specified
224 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
225 const std::vector<Value*> &Idx,
226 bool AllowCompositeLeaf) {
227 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
229 // Handle the special case of the empty set index set...
231 if (AllowCompositeLeaf ||
232 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
233 return cast<PointerType>(Ptr)->getElementType();
238 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
239 if (Idx.size() == CurIdx) {
240 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
241 return 0; // Can't load a whole structure or array!?!?
244 Value *Index = Idx[CurIdx++];
245 if (isa<PointerType>(CT) && CurIdx != 1)
246 return 0; // Can only index into pointer types at the first index!
247 if (!CT->indexValid(Index)) return 0;
248 Ptr = CT->getTypeAtIndex(Index);
250 // If the new type forwards to another type, then it is in the middle
251 // of being refined to another type (and hence, may have dropped all
252 // references to what it was using before). So, use the new forwarded
254 if (const Type * Ty = Ptr->getForwardedType()) {
258 return CurIdx == Idx.size() ? Ptr : 0;
261 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
262 Value *Idx0, Value *Idx1,
263 bool AllowCompositeLeaf) {
264 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
265 if (!PTy) return 0; // Type isn't a pointer type!
267 // Check the pointer index.
268 if (!PTy->indexValid(Idx0)) return 0;
270 const CompositeType *CT = dyn_cast<CompositeType>(PTy->getElementType());
271 if (!CT || !CT->indexValid(Idx1)) return 0;
273 const Type *ElTy = CT->getTypeAtIndex(Idx1);
274 if (AllowCompositeLeaf || ElTy->isFirstClassType())