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