- Renamed Type::isIntegral() to Type::isInteger()
[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 //                           LoadInst Implementation
43 //===----------------------------------------------------------------------===//
44
45 LoadInst::LoadInst(Value *Ptr, const std::string &Name)
46   : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
47                 Load, Name) {
48   Operands.reserve(1);
49   Operands.push_back(Use(Ptr, this));
50 }
51
52
53 //===----------------------------------------------------------------------===//
54 //                           StoreInst Implementation
55 //===----------------------------------------------------------------------===//
56
57 StoreInst::StoreInst(Value *Val, Value *Ptr)
58   : Instruction(Type::VoidTy, Store, "") {
59   
60   Operands.reserve(2);
61   Operands.push_back(Use(Val, this));
62   Operands.push_back(Use(Ptr, this));
63 }
64
65
66 //===----------------------------------------------------------------------===//
67 //                       GetElementPtrInst Implementation
68 //===----------------------------------------------------------------------===//
69
70 GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
71                                      const std::string &Name)
72   : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
73                                                             Idx, true))),
74                   GetElementPtr, Name) {
75   assert(getIndexedType(Ptr->getType(), Idx, true) && "gep 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 // getIndexedType - Returns the type of the element that would be loaded with
84 // a load instruction with the specified parameters.
85 //
86 // A null type is returned if the indices are invalid for the specified 
87 // pointer type.
88 //
89 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, 
90                                               const std::vector<Value*> &Idx,
91                                               bool AllowCompositeLeaf) {
92   if (!isa<PointerType>(Ptr)) return 0;   // Type isn't a pointer type!
93
94   // Handle the special case of the empty set index set...
95   if (Idx.empty()) return cast<PointerType>(Ptr)->getElementType();
96  
97   unsigned CurIDX = 0;
98   while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
99     if (Idx.size() == CurIDX) {
100       if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
101       return 0;   // Can't load a whole structure or array!?!?
102     }
103
104     Value *Index = Idx[CurIDX++];
105     if (!CT->indexValid(Index)) return 0;
106     Ptr = CT->getTypeAtIndex(Index);
107   }
108   return CurIDX == Idx.size() ? Ptr : 0;
109 }
110
111
112 //===----------------------------------------------------------------------===//
113 //                             FreeInst Implementation
114 //===----------------------------------------------------------------------===//
115
116 FreeInst::FreeInst(Value *Ptr) : Instruction(Type::VoidTy, Free, "") {
117   assert(isa<PointerType>(Ptr->getType()) && "Can't free nonpointer!");
118   Operands.reserve(1);
119   Operands.push_back(Use(Ptr, this));
120 }
121