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 (const StructType *ST = dyn_cast<StructType>(Ptr)) {
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();
35 if (NextIdx >= ST->getElementTypes().size()) return 0;
36 Ptr = ST->getElementTypes()[NextIdx];
39 } else if (Ptr->isArrayType()) {
40 assert(0 && "Loading from arrays not implemented yet!");
42 return (Idx.size() == 0) ? Ptr : 0; // Load directly through ptr
46 //===----------------------------------------------------------------------===//
47 // LoadInst Implementation
48 //===----------------------------------------------------------------------===//
50 LoadInst::LoadInst(Value *Ptr, const vector<ConstPoolVal*> &Idx,
51 const string &Name = "")
52 : MemAccessInst(getIndexedType(Ptr->getType(), Idx), Load, Idx, Name) {
53 assert(getIndexedType(Ptr->getType(), Idx) && "Load operands invalid!");
54 Operands.reserve(1+Idx.size());
55 Operands.push_back(Use(Ptr, this));
57 for (unsigned i = 0, E = Idx.size(); i != E; ++i)
58 Operands.push_back(Use(Idx[i], this));
62 LoadInst::LoadInst(Value *Ptr, const string &Name = "")
63 : MemAccessInst(cast<PointerType>(Ptr->getType())->getValueType(),
64 Load, vector<ConstPoolVal*>(), Name) {
66 Operands.push_back(Use(Ptr, this));
70 //===----------------------------------------------------------------------===//
71 // StoreInst Implementation
72 //===----------------------------------------------------------------------===//
74 StoreInst::StoreInst(Value *Val, Value *Ptr, const vector<ConstPoolVal*> &Idx,
75 const string &Name = "")
76 : MemAccessInst(Type::VoidTy, Store, Idx, Name) {
77 assert(getIndexedType(Ptr->getType(), Idx) && "Store operands invalid!");
79 Operands.reserve(2+Idx.size());
80 Operands.push_back(Use(Val, this));
81 Operands.push_back(Use(Ptr, this));
83 for (unsigned i = 0, E = Idx.size(); i != E; ++i)
84 Operands.push_back(Use(Idx[i], this));
87 StoreInst::StoreInst(Value *Val, Value *Ptr, const string &Name = "")
88 : MemAccessInst(Type::VoidTy, Store, vector<ConstPoolVal*>(), Name) {
91 Operands.push_back(Use(Val, this));
92 Operands.push_back(Use(Ptr, this));
96 //===----------------------------------------------------------------------===//
97 // GetElementPtrInst Implementation
98 //===----------------------------------------------------------------------===//
100 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
101 const vector<ConstPoolVal*> &Idx,
102 const string &Name = "")
103 : MemAccessInst(PointerType::get(getIndexedType(Ptr->getType(), Idx, true)),
104 GetElementPtr, Idx, Name) {
105 assert(getIndexedType(Ptr->getType(), Idx, true) && "gep operands invalid!");
106 Operands.reserve(1+Idx.size());
107 Operands.push_back(Use(Ptr, this));
109 for (unsigned i = 0, E = Idx.size(); i != E; ++i)
110 Operands.push_back(Use(Idx[i], this));
113 bool GetElementPtrInst::isStructSelector() const {
114 return ((PointerType*)Operands[0]->getType())->getValueType()->isStructType();