Change the Opcode enum for PHI nodes from "Instruction::PHINode" to "Instruction...
[oota-llvm.git] / lib / VMCore / iMemory.cpp
1 //===-- iMemory.cpp - Implement Memory instructions -----------------------===//
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(PointerType::get(Ty), iTy, Name, InsertBef) {
14
15   // ArraySize defaults to 1.
16   if (!ArraySize) ArraySize = ConstantUInt::get(Type::UIntTy, 1);
17
18   Operands.reserve(1);
19   assert(ArraySize->getType() == Type::UIntTy &&
20          "Malloc/Allocation array size != UIntTy!");
21
22   Operands.push_back(Use(ArraySize, this));
23 }
24
25 bool AllocationInst::isArrayAllocation() const {
26   return getOperand(0) != ConstantUInt::get(Type::UIntTy, 1);
27 }
28
29 const Type *AllocationInst::getAllocatedType() const {
30   return getType()->getElementType();
31 }
32
33 AllocaInst::AllocaInst(const AllocaInst &AI)
34   : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
35                    Instruction::Alloca) {
36 }
37
38 MallocInst::MallocInst(const MallocInst &MI)
39   : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
40                    Instruction::Malloc) {
41 }
42
43 //===----------------------------------------------------------------------===//
44 //                             FreeInst Implementation
45 //===----------------------------------------------------------------------===//
46
47 FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
48   : Instruction(Type::VoidTy, Free, "", InsertBefore) {
49   assert(isa<PointerType>(Ptr->getType()) && "Can't free nonpointer!");
50   Operands.reserve(1);
51   Operands.push_back(Use(Ptr, this));
52 }
53
54
55 //===----------------------------------------------------------------------===//
56 //                           LoadInst Implementation
57 //===----------------------------------------------------------------------===//
58
59 LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
60   : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
61                 Load, Name, InsertBef), Volatile(false) {
62   Operands.reserve(1);
63   Operands.push_back(Use(Ptr, this));
64 }
65
66 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
67                    Instruction *InsertBef)
68   : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
69                 Load, Name, InsertBef), Volatile(isVolatile) {
70   Operands.reserve(1);
71   Operands.push_back(Use(Ptr, this));
72 }
73
74 //===----------------------------------------------------------------------===//
75 //                           StoreInst Implementation
76 //===----------------------------------------------------------------------===//
77
78 StoreInst::StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore)
79   : Instruction(Type::VoidTy, Store, "", InsertBefore), Volatile(false) {
80   
81   Operands.reserve(2);
82   Operands.push_back(Use(Val, this));
83   Operands.push_back(Use(Ptr, this));
84 }
85
86 StoreInst::StoreInst(Value *Val, Value *Ptr, bool isVolatile, 
87                      Instruction *InsertBefore)
88   : Instruction(Type::VoidTy, Store, "", InsertBefore), Volatile(isVolatile) {
89   
90   Operands.reserve(2);
91   Operands.push_back(Use(Val, this));
92   Operands.push_back(Use(Ptr, this));
93 }
94
95
96 //===----------------------------------------------------------------------===//
97 //                       GetElementPtrInst Implementation
98 //===----------------------------------------------------------------------===//
99
100 // checkType - Simple wrapper function to give a better assertion failure
101 // message on bad indexes for a gep instruction.
102 //
103 static inline const Type *checkType(const Type *Ty) {
104   assert(Ty && "Invalid indices for type!");
105   return Ty;
106 }
107
108 GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
109                                      const std::string &Name, Instruction *InBe)
110   : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
111                                                             Idx, true))),
112                   GetElementPtr, Name, InBe) {
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 // getIndexedType - Returns the type of the element that would be loaded with
121 // a load instruction with the specified parameters.
122 //
123 // A null type is returned if the indices are invalid for the specified 
124 // pointer type.
125 //
126 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, 
127                                               const std::vector<Value*> &Idx,
128                                               bool AllowCompositeLeaf) {
129   if (!isa<PointerType>(Ptr)) return 0;   // Type isn't a pointer type!
130
131   // Handle the special case of the empty set index set...
132   if (Idx.empty()) return cast<PointerType>(Ptr)->getElementType();
133  
134   unsigned CurIdx = 0;
135   while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
136     if (Idx.size() == CurIdx) {
137       if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
138       return 0;   // Can't load a whole structure or array!?!?
139     }
140
141     Value *Index = Idx[CurIdx++];
142     if (isa<PointerType>(CT) && CurIdx != 1)
143       return 0;  // Can only index into pointer types at the first index!
144     if (!CT->indexValid(Index)) return 0;
145     Ptr = CT->getTypeAtIndex(Index);
146   }
147   return CurIdx == Idx.size() ? Ptr : 0;
148 }