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/ConstantVals.h"
10 static inline const Type *checkType(const Type *Ty) {
11 assert(Ty && "Invalid indices for type!");
15 bool AllocationInst::isArrayAllocation() const {
16 return getNumOperands() == 1 &&
17 getOperand(0) != ConstantUInt::get(Type::UIntTy, 1);
20 //===----------------------------------------------------------------------===//
21 // MemAccessInst Implementation
22 //===----------------------------------------------------------------------===//
24 // getIndexedType - Returns the type of the element that would be loaded with
25 // a load instruction with the specified parameters.
27 // A null type is returned if the indices are invalid for the specified
30 const Type* MemAccessInst::getIndexedType(const Type *Ptr,
31 const std::vector<Value*> &Idx,
32 bool AllowCompositeLeaf = false) {
33 if (!Ptr->isPointerType()) return 0; // Type isn't a pointer type!
35 // Handle the special case of the empty set index set...
36 if (Idx.empty()) return cast<PointerType>(Ptr)->getElementType();
39 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
40 if (Idx.size() == CurIDX) {
41 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
42 return 0; // Can't load a whole structure or array!?!?
45 Value *Index = Idx[CurIDX++];
46 if (!CT->indexValid(Index)) return 0;
47 Ptr = CT->getTypeAtIndex(Index);
49 return CurIDX == Idx.size() ? Ptr : 0;
53 //===----------------------------------------------------------------------===//
54 // LoadInst Implementation
55 //===----------------------------------------------------------------------===//
57 LoadInst::LoadInst(Value *Ptr, const std::vector<Value*> &Idx,
58 const std::string &Name = "")
59 : MemAccessInst(checkType(getIndexedType(Ptr->getType(), Idx)), Load, Name) {
60 assert(getIndexedType(Ptr->getType(), Idx) && "Load operands invalid!");
61 Operands.reserve(1+Idx.size());
62 Operands.push_back(Use(Ptr, this));
64 for (unsigned i = 0, E = Idx.size(); i != E; ++i)
65 Operands.push_back(Use(Idx[i], this));
69 LoadInst::LoadInst(Value *Ptr, const std::string &Name = "")
70 : MemAccessInst(cast<PointerType>(Ptr->getType())->getElementType(),
73 Operands.push_back(Use(Ptr, this));
77 //===----------------------------------------------------------------------===//
78 // StoreInst Implementation
79 //===----------------------------------------------------------------------===//
81 StoreInst::StoreInst(Value *Val, Value *Ptr, const std::vector<Value*> &Idx,
82 const std::string &Name = "")
83 : MemAccessInst(Type::VoidTy, Store, Name) {
84 assert(getIndexedType(Ptr->getType(), Idx) && "Store operands invalid!");
86 Operands.reserve(2+Idx.size());
87 Operands.push_back(Use(Val, this));
88 Operands.push_back(Use(Ptr, this));
90 for (unsigned i = 0, E = Idx.size(); i != E; ++i)
91 Operands.push_back(Use(Idx[i], this));
94 StoreInst::StoreInst(Value *Val, Value *Ptr, const std::string &Name = "")
95 : MemAccessInst(Type::VoidTy, Store, Name) {
98 Operands.push_back(Use(Val, this));
99 Operands.push_back(Use(Ptr, this));
103 //===----------------------------------------------------------------------===//
104 // GetElementPtrInst Implementation
105 //===----------------------------------------------------------------------===//
107 GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
108 const std::string &Name = "")
109 : MemAccessInst(PointerType::get(checkType(getIndexedType(Ptr->getType(),
111 GetElementPtr, Name) {
112 assert(getIndexedType(Ptr->getType(), Idx, true) && "gep operands invalid!");
113 Operands.reserve(1+Idx.size());
114 Operands.push_back(Use(Ptr, this));
116 for (unsigned i = 0, E = Idx.size(); i != E; ++i)
117 Operands.push_back(Use(Idx[i], this));
120 bool GetElementPtrInst::isStructSelector() const {
121 return ((PointerType*)Operands[0]->getType())->getElementType()->isStructType();