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