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