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"
9 static inline const Type *checkType(const Type *Ty) {
10 assert(Ty && "Invalid indices for type!");
14 //===----------------------------------------------------------------------===//
15 // MemAccessInst Implementation
16 //===----------------------------------------------------------------------===//
18 // getIndexedType - Returns the type of the element that would be loaded with
19 // a load instruction with the specified parameters.
21 // A null type is returned if the indices are invalid for the specified
24 const Type* MemAccessInst::getIndexedType(const Type *Ptr,
25 const std::vector<Value*> &Idx,
26 bool AllowCompositeLeaf = false) {
27 if (!Ptr->isPointerType()) return 0; // Type isn't a pointer type!
29 // Handle the special case of the empty set index set...
30 if (Idx.empty()) return cast<PointerType>(Ptr)->getElementType();
33 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
34 if (Idx.size() == CurIDX) {
35 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
36 return 0; // Can't load a whole structure or array!?!?
39 Value *Index = Idx[CurIDX++];
40 if (!CT->indexValid(Index)) return 0;
41 Ptr = CT->getTypeAtIndex(Index);
43 return CurIDX == Idx.size() ? Ptr : 0;
47 //===----------------------------------------------------------------------===//
48 // LoadInst Implementation
49 //===----------------------------------------------------------------------===//
51 LoadInst::LoadInst(Value *Ptr, const std::vector<Value*> &Idx,
52 const std::string &Name = "")
53 : MemAccessInst(checkType(getIndexedType(Ptr->getType(), Idx)), Load, Name) {
54 assert(getIndexedType(Ptr->getType(), Idx) && "Load operands invalid!");
55 Operands.reserve(1+Idx.size());
56 Operands.push_back(Use(Ptr, this));
58 for (unsigned i = 0, E = Idx.size(); i != E; ++i)
59 Operands.push_back(Use(Idx[i], this));
63 LoadInst::LoadInst(Value *Ptr, const std::string &Name = "")
64 : MemAccessInst(cast<PointerType>(Ptr->getType())->getElementType(),
67 Operands.push_back(Use(Ptr, this));
71 //===----------------------------------------------------------------------===//
72 // StoreInst Implementation
73 //===----------------------------------------------------------------------===//
75 StoreInst::StoreInst(Value *Val, Value *Ptr, const std::vector<Value*> &Idx,
76 const std::string &Name = "")
77 : MemAccessInst(Type::VoidTy, Store, Name) {
78 assert(getIndexedType(Ptr->getType(), Idx) && "Store operands invalid!");
80 Operands.reserve(2+Idx.size());
81 Operands.push_back(Use(Val, this));
82 Operands.push_back(Use(Ptr, this));
84 for (unsigned i = 0, E = Idx.size(); i != E; ++i)
85 Operands.push_back(Use(Idx[i], this));
88 StoreInst::StoreInst(Value *Val, Value *Ptr, const std::string &Name = "")
89 : MemAccessInst(Type::VoidTy, Store, Name) {
92 Operands.push_back(Use(Val, this));
93 Operands.push_back(Use(Ptr, this));
97 //===----------------------------------------------------------------------===//
98 // GetElementPtrInst Implementation
99 //===----------------------------------------------------------------------===//
101 GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
102 const std::string &Name = "")
103 : MemAccessInst(PointerType::get(checkType(getIndexedType(Ptr->getType(),
105 GetElementPtr, Name) {
106 assert(getIndexedType(Ptr->getType(), Idx, true) && "gep operands invalid!");
107 Operands.reserve(1+Idx.size());
108 Operands.push_back(Use(Ptr, this));
110 for (unsigned i = 0, E = Idx.size(); i != E; ++i)
111 Operands.push_back(Use(Idx[i], this));
114 bool GetElementPtrInst::isStructSelector() const {
115 return ((PointerType*)Operands[0]->getType())->getElementType()->isStructType();