Load and Store now no longer derive from MemAccessInst. Indexing a load or
[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::string &Name)
79   : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
80                 Load, Name) {
81   Operands.reserve(1);
82   Operands.push_back(Use(Ptr, this));
83 }
84
85
86 //===----------------------------------------------------------------------===//
87 //                           StoreInst Implementation
88 //===----------------------------------------------------------------------===//
89
90 StoreInst::StoreInst(Value *Val, Value *Ptr)
91   : Instruction(Type::VoidTy, Store, "") {
92   
93   Operands.reserve(2);
94   Operands.push_back(Use(Val, this));
95   Operands.push_back(Use(Ptr, this));
96 }
97
98
99 //===----------------------------------------------------------------------===//
100 //                       GetElementPtrInst Implementation
101 //===----------------------------------------------------------------------===//
102
103 GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
104                                      const std::string &Name)
105   : MemAccessInst(PointerType::get(checkType(getIndexedType(Ptr->getType(),
106                                                             Idx, true))),
107                   GetElementPtr, Name) {
108   assert(getIndexedType(Ptr->getType(), Idx, true) && "gep operands invalid!");
109   Operands.reserve(1+Idx.size());
110   Operands.push_back(Use(Ptr, this));
111
112   for (unsigned i = 0, E = Idx.size(); i != E; ++i)
113     Operands.push_back(Use(Idx[i], this));
114 }
115
116
117 //===----------------------------------------------------------------------===//
118 //                             FreeInst Implementation
119 //===----------------------------------------------------------------------===//
120
121 FreeInst::FreeInst(Value *Ptr) : Instruction(Type::VoidTy, Free, "") {
122   assert(isa<PointerType>(Ptr->getType()) && "Can't free nonpointer!");
123   Operands.reserve(1);
124   Operands.push_back(Use(Ptr, this));
125 }
126