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
20 const Type* MemAccessInst::getIndexedType(const Type *Ptr,
21 const vector<ConstPoolVal*> &Idx,
22 bool AllowStructLeaf = false) {
23 if (!Ptr->isPointerType()) return 0; // Type isn't a pointer type!
25 // Get the type pointed to...
26 Ptr = ((const PointerType*)Ptr)->getValueType();
28 if (Ptr->isStructType()) {
30 while (Ptr->isStructType()) {
31 if (Idx.size() == CurIDX)
32 return AllowStructLeaf ? Ptr : 0; // Can't load a whole structure!?!?
33 if (Idx[CurIDX]->getType() != Type::UByteTy) return 0; // Illegal idx
34 unsigned NextIdx = ((ConstPoolUInt*)Idx[CurIDX++])->getValue();
36 const StructType *ST = (const StructType *)Ptr;
37 Ptr = ST->getElementTypes()[NextIdx];
40 } else if (Ptr->isArrayType()) {
41 assert(0 && "Loading from arrays not implemented yet!");
43 return (Idx.size() == 0) ? Ptr : 0; // Load directly through ptr
47 //===----------------------------------------------------------------------===//
48 // LoadInst Implementation
49 //===----------------------------------------------------------------------===//
51 LoadInst::LoadInst(Value *Ptr, const vector<ConstPoolVal*> &Idx,
52 const string &Name = "")
53 : MemAccessInst(getIndexedType(Ptr->getType(), Idx), Load, Idx, 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));
64 //===----------------------------------------------------------------------===//
65 // StoreInst Implementation
66 //===----------------------------------------------------------------------===//
68 StoreInst::StoreInst(Value *Val, Value *Ptr, const vector<ConstPoolVal*> &Idx,
69 const string &Name = "")
70 : MemAccessInst(Type::VoidTy, Store, Idx, Name) {
71 assert(getIndexedType(Ptr->getType(), Idx) && "Store operands invalid!");
73 Operands.reserve(2+Idx.size());
74 Operands.push_back(Use(Val, this));
75 Operands.push_back(Use(Ptr, this));
77 for (unsigned i = 0, E = Idx.size(); i != E; ++i)
78 Operands.push_back(Use(Idx[i], this));
82 //===----------------------------------------------------------------------===//
83 // GetElementPtrInst Implementation
84 //===----------------------------------------------------------------------===//
86 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
87 const vector<ConstPoolVal*> &Idx,
88 const string &Name = "")
89 : MemAccessInst(PointerType::get(getIndexedType(Ptr->getType(), Idx, true)),
90 GetElementPtr, Idx, Name) {
91 assert(getIndexedType(Ptr->getType(), Idx, true) && "gep operands invalid!");
92 Operands.reserve(1+Idx.size());
93 Operands.push_back(Use(Ptr, this));
95 for (unsigned i = 0, E = Idx.size(); i != E; ++i)
96 Operands.push_back(Use(Idx[i], this));
99 bool GetElementPtrInst::isStructSelector() const {
100 return ((PointerType*)Operands[0]->getType())->getValueType()->isStructType();