e980d600d466976911de33a0c3f85538fd53d164
[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 (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];
37     }
38     return Ptr;
39   } else if (Ptr->isArrayType()) {
40     assert(0 && "Loading from arrays not implemented yet!");
41   } else {
42     return (Idx.size() == 0) ? Ptr : 0;  // Load directly through ptr
43   }
44 }
45
46 //===----------------------------------------------------------------------===//
47 //                           LoadInst Implementation
48 //===----------------------------------------------------------------------===//
49
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));
56   
57   for (unsigned i = 0, E = Idx.size(); i != E; ++i)
58     Operands.push_back(Use(Idx[i], this));
59   
60 }
61
62 LoadInst::LoadInst(Value *Ptr, const string &Name = "")
63   : MemAccessInst(cast<PointerType>(Ptr->getType())->getValueType(),
64                   Load, vector<ConstPoolVal*>(), Name) {
65   Operands.reserve(1);
66   Operands.push_back(Use(Ptr, this));
67 }
68
69
70 //===----------------------------------------------------------------------===//
71 //                           StoreInst Implementation
72 //===----------------------------------------------------------------------===//
73
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!");
78   
79   Operands.reserve(2+Idx.size());
80   Operands.push_back(Use(Val, this));
81   Operands.push_back(Use(Ptr, this));
82
83   for (unsigned i = 0, E = Idx.size(); i != E; ++i)
84     Operands.push_back(Use(Idx[i], this));
85 }
86
87 StoreInst::StoreInst(Value *Val, Value *Ptr, const string &Name = "")
88   : MemAccessInst(Type::VoidTy, Store, vector<ConstPoolVal*>(), Name) {
89   
90   Operands.reserve(2);
91   Operands.push_back(Use(Val, this));
92   Operands.push_back(Use(Ptr, this));
93 }
94
95
96 //===----------------------------------------------------------------------===//
97 //                       GetElementPtrInst Implementation
98 //===----------------------------------------------------------------------===//
99
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));
108
109   for (unsigned i = 0, E = Idx.size(); i != E; ++i)
110     Operands.push_back(Use(Idx[i], this));
111 }
112
113 bool GetElementPtrInst::isStructSelector() const {
114   return ((PointerType*)Operands[0]->getType())->getValueType()->isStructType();
115 }