* ValueHolder now takes 3 arguments
[oota-llvm.git] / lib / VMCore / iMemory.cpp
1 //===-- iMemory.cpp - Implement Memory instructions --------------*- C++ -*--=//
2 //
3 // This file implements the various memory related classes defined in iMemory.h
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/iMemory.h"
8 #include "llvm/ConstPoolVals.h"
9
10 //===----------------------------------------------------------------------===//
11 //                        MemAccessInst Implementation
12 //===----------------------------------------------------------------------===//
13
14 // getIndexedType - Returns the type of the element that would be loaded with
15 // a load instruction with the specified parameters.
16 //
17 // A null type is returned if the indices are invalid for the specified 
18 // pointer type.
19 //
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!
24  
25   // Get the type pointed to...
26   Ptr = ((const PointerType*)Ptr)->getValueType();
27
28   if (Ptr->isStructType()) {
29     unsigned CurIDX = 0;
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();
35       
36       const StructType *ST = (const StructType *)Ptr;
37       Ptr = ST->getElementTypes()[NextIdx];
38     }
39     return Ptr;
40   } else if (Ptr->isArrayType()) {
41     assert(0 && "Loading from arrays not implemented yet!");
42   } else {
43     return (Idx.size() == 0) ? Ptr : 0;  // Load directly through ptr
44   }
45 }
46
47
48 //===----------------------------------------------------------------------===//
49 //                           LoadInst Implementation
50 //===----------------------------------------------------------------------===//
51
52 LoadInst::LoadInst(Value *Ptr, const vector<ConstPoolVal*> &Idx,
53                    const string &Name = "")
54   : MemAccessInst(getIndexedType(Ptr->getType(), Idx), Load, Name) {
55   assert(getIndexedType(Ptr->getType(), Idx) && "Load operands invalid!");
56   Operands.reserve(1+Idx.size());
57   Operands.push_back(Use(Ptr, this));
58
59   for (unsigned i = 0, E = Idx.size(); i != E; ++i)
60     Operands.push_back(Use(Idx[i], this));
61 }
62
63
64 //===----------------------------------------------------------------------===//
65 //                           StoreInst Implementation
66 //===----------------------------------------------------------------------===//
67
68 StoreInst::StoreInst(Value *Val, Value *Ptr, const vector<ConstPoolVal*> &Idx,
69                      const string &Name = "")
70   : MemAccessInst(Type::VoidTy, Store, Name) {
71   assert(getIndexedType(Ptr->getType(), Idx) && "Store operands invalid!");
72   
73   Operands.reserve(2+Idx.size());
74   Operands.push_back(Use(Val, this));
75   Operands.push_back(Use(Ptr, this));
76
77   for (unsigned i = 0, E = Idx.size(); i != E; ++i)
78     Operands.push_back(Use(Idx[i], this));
79 }
80
81
82 //===----------------------------------------------------------------------===//
83 //                       GetElementPtrInst Implementation
84 //===----------------------------------------------------------------------===//
85
86 GetElementPtrInst::GetElementPtrInst(Value *Ptr, 
87                                      const vector<ConstPoolVal*> &Idx,
88                                      const string &Name = "")
89   : MemAccessInst(PointerType::getPointerType(getIndexedType(Ptr->getType(), Idx, true)), GetElementPtr, Name) {
90   assert(getIndexedType(Ptr->getType(), Idx, true) && "gep operands invalid!");
91   Operands.reserve(1+Idx.size());
92   Operands.push_back(Use(Ptr, this));
93
94   for (unsigned i = 0, E = Idx.size(); i != E; ++i)
95     Operands.push_back(Use(Idx[i], this));
96 }
97
98 bool GetElementPtrInst::isStructSelector() const {
99   return ((PointerType*)Operands[0]->getType())->getValueType()->isStructType();
100 }