Remove gep::isStructSelector
[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/ConstantVals.h"
9
10 static inline const Type *checkType(const Type *Ty) {
11   assert(Ty && "Invalid indices for type!");
12   return Ty;
13 }
14
15 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, 
16                                const std::string &Name = "")
17   : Instruction(Ty, iTy, Name) {
18   assert(Ty->isPointerType() && "Can't allocate a non pointer type!");
19
20   // ArraySize defaults to 1.
21   if (!ArraySize) ArraySize = ConstantUInt::get(Type::UIntTy, 1);
22
23   Operands.reserve(1);
24   assert(ArraySize->getType() == Type::UIntTy &&
25          "Malloc/Allocation array size != UIntTy!");
26
27   Operands.push_back(Use(ArraySize, this));
28 }
29
30 bool AllocationInst::isArrayAllocation() const {
31   return getNumOperands() == 1 &&
32          getOperand(0) != ConstantUInt::get(Type::UIntTy, 1);
33 }
34
35 //===----------------------------------------------------------------------===//
36 //                        MemAccessInst Implementation
37 //===----------------------------------------------------------------------===//
38
39 // getIndexedType - Returns the type of the element that would be loaded with
40 // a load instruction with the specified parameters.
41 //
42 // A null type is returned if the indices are invalid for the specified 
43 // pointer type.
44 //
45 const Type* MemAccessInst::getIndexedType(const Type *Ptr, 
46                                           const std::vector<Value*> &Idx,
47                                           bool AllowCompositeLeaf = false) {
48   if (!Ptr->isPointerType()) return 0;   // Type isn't a pointer type!
49
50   // Handle the special case of the empty set index set...
51   if (Idx.empty()) return cast<PointerType>(Ptr)->getElementType();
52  
53   unsigned CurIDX = 0;
54   while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
55     if (Idx.size() == CurIDX) {
56       if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
57       return 0;   // Can't load a whole structure or array!?!?
58     }
59
60     Value *Index = Idx[CurIDX++];
61     if (!CT->indexValid(Index)) return 0;
62     Ptr = CT->getTypeAtIndex(Index);
63   }
64   return CurIDX == Idx.size() ? Ptr : 0;
65 }
66
67
68 //===----------------------------------------------------------------------===//
69 //                           LoadInst Implementation
70 //===----------------------------------------------------------------------===//
71
72 LoadInst::LoadInst(Value *Ptr, const std::vector<Value*> &Idx,
73                    const std::string &Name = "")
74   : MemAccessInst(checkType(getIndexedType(Ptr->getType(), Idx)), Load, Name) {
75   assert(getIndexedType(Ptr->getType(), Idx) && "Load operands invalid!");
76   Operands.reserve(1+Idx.size());
77   Operands.push_back(Use(Ptr, this));
78   
79   for (unsigned i = 0, E = Idx.size(); i != E; ++i)
80     Operands.push_back(Use(Idx[i], this));
81   
82 }
83
84 LoadInst::LoadInst(Value *Ptr, const std::string &Name = "")
85   : MemAccessInst(cast<PointerType>(Ptr->getType())->getElementType(),
86                   Load, Name) {
87   Operands.reserve(1);
88   Operands.push_back(Use(Ptr, this));
89 }
90
91
92 //===----------------------------------------------------------------------===//
93 //                           StoreInst Implementation
94 //===----------------------------------------------------------------------===//
95
96 StoreInst::StoreInst(Value *Val, Value *Ptr, const std::vector<Value*> &Idx)
97   : MemAccessInst(Type::VoidTy, Store, "") {
98   assert(getIndexedType(Ptr->getType(), Idx) && "Store operands invalid!");
99   
100   Operands.reserve(2+Idx.size());
101   Operands.push_back(Use(Val, this));
102   Operands.push_back(Use(Ptr, this));
103
104   for (unsigned i = 0, E = Idx.size(); i != E; ++i)
105     Operands.push_back(Use(Idx[i], this));
106 }
107
108 StoreInst::StoreInst(Value *Val, Value *Ptr)
109   : MemAccessInst(Type::VoidTy, Store, "") {
110   
111   Operands.reserve(2);
112   Operands.push_back(Use(Val, this));
113   Operands.push_back(Use(Ptr, this));
114 }
115
116
117 //===----------------------------------------------------------------------===//
118 //                       GetElementPtrInst Implementation
119 //===----------------------------------------------------------------------===//
120
121 GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
122                                      const std::string &Name = "")
123   : MemAccessInst(PointerType::get(checkType(getIndexedType(Ptr->getType(),
124                                                             Idx, true))),
125                   GetElementPtr, Name) {
126   assert(getIndexedType(Ptr->getType(), Idx, true) && "gep operands invalid!");
127   Operands.reserve(1+Idx.size());
128   Operands.push_back(Use(Ptr, this));
129
130   for (unsigned i = 0, E = Idx.size(); i != E; ++i)
131     Operands.push_back(Use(Idx[i], this));
132 }