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/ConstPoolVals.h"
10 //===----------------------------------------------------------------------===//
11 // MemAccessInst Implementation
12 //===----------------------------------------------------------------------===//
14 // getIndexedType - Returns the type of the element that would be loaded with
15 // a load instruction with the specified parameters.
17 // A null type is returned if the indices are invalid for the specified
21 const Type* MemAccessInst::getIndexedType(const Type *Ptr,
22 const vector<ConstPoolVal*> &Idx,
23 bool AllowStructLeaf = false) {
24 if (!Ptr->isPointerType()) return 0; // Type isn't a pointer type!
26 // Get the type pointed to...
27 Ptr = ((const PointerType*)Ptr)->getValueType();
29 if (Ptr->isStructType()) {
31 while (Ptr->isStructType()) {
32 if (Idx.size() == CurIDX)
33 return AllowStructLeaf ? Ptr : 0; // Can't load a whole structure!?!?
34 if (Idx[CurIDX]->getType() != Type::UByteTy) return 0; // Illegal idx
35 unsigned NextIdx = ((ConstPoolUInt*)Idx[CurIDX++])->getValue();
37 const StructType *ST = (const StructType *)Ptr;
38 Ptr = ST->getElementTypes()[NextIdx];
41 } else if (Ptr->isArrayType()) {
42 assert(0 && "Loading from arrays not implemented yet!");
44 return (Idx.size() == 0) ? Ptr : 0; // Load directly through ptr
50 MemAccessInst::getIndexedOfsetForTarget(const Type *Ptr,
51 const vector<ConstPoolVal*> &Idx,
52 const TargetMachine& targetMachine)
54 if (!Ptr->isPointerType())
55 return 0; // Type isn't a pointer type!
57 unsigned int curOffset = 0;
59 // Get the type pointed to...
60 Ptr = ((const PointerType*) Ptr)->getValueType();
62 if (Ptr->isStructType()) {
63 unsigned CurIDX = 0; // which element of Idx vector
64 while (Ptr->isStructType()) {
65 const StructType * SPtr = (StructType *) Ptr;
67 if (Idx.size() == CurIDX)
70 assert (Idx[CurIDX]->getType() == Type::UByteTy && "Illegal struct idx");
71 unsigned NextIdx = ((ConstPoolUInt*)Idx[CurIDX++])->getValue();
73 // add the offset for the current element
74 curOffset += SPtr->getElementOffset(NextIdx, targetMachine);
76 // and update Ptr to refer to current element
77 Ptr = SPtr->getElementTypes()[NextIdx];
80 } else if (Ptr->isArrayType()) {
81 assert(0 && "Loading from arrays not implemented yet!");
83 assert (Idx.size() == 0 && "Indexing type that is not struct or array?");
84 return 0; // Load directly through ptr
89 //===----------------------------------------------------------------------===//
90 // LoadInst Implementation
91 //===----------------------------------------------------------------------===//
93 LoadInst::LoadInst(Value *Ptr, const vector<ConstPoolVal*> &Idx,
94 const string &Name = "")
95 : MemAccessInst(getIndexedType(Ptr->getType(), Idx), Load, Idx, Name)
97 assert(getIndexedType(Ptr->getType(), Idx) && "Load operands invalid!");
98 Operands.reserve(1+Idx.size());
99 Operands.push_back(Use(Ptr, this));
101 for (unsigned i = 0, E = Idx.size(); i != E; ++i)
102 Operands.push_back(Use(Idx[i], this));
107 //===----------------------------------------------------------------------===//
108 // StoreInst Implementation
109 //===----------------------------------------------------------------------===//
111 StoreInst::StoreInst(Value *Val, Value *Ptr, const vector<ConstPoolVal*> &Idx,
112 const string &Name = "")
113 : MemAccessInst(Type::VoidTy, Store, Idx, Name) {
114 assert(getIndexedType(Ptr->getType(), Idx) && "Store operands invalid!");
116 Operands.reserve(2+Idx.size());
117 Operands.push_back(Use(Val, this));
118 Operands.push_back(Use(Ptr, this));
120 for (unsigned i = 0, E = Idx.size(); i != E; ++i)
121 Operands.push_back(Use(Idx[i], this));
125 //===----------------------------------------------------------------------===//
126 // GetElementPtrInst Implementation
127 //===----------------------------------------------------------------------===//
129 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
130 const vector<ConstPoolVal*> &Idx,
131 const string &Name = "")
132 : MemAccessInst(PointerType::getPointerType(getIndexedType(Ptr->getType(),
134 GetElementPtr, Idx, Name) {
135 assert(getIndexedType(Ptr->getType(), Idx, true) && "gep operands invalid!");
136 Operands.reserve(1+Idx.size());
137 Operands.push_back(Use(Ptr, this));
139 for (unsigned i = 0, E = Idx.size(); i != E; ++i)
140 Operands.push_back(Use(Idx[i], this));
143 bool GetElementPtrInst::isStructSelector() const {
144 return ((PointerType*)Operands[0]->getType())->getValueType()->isStructType();