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