Fixed the dynamic generation of the list of subdirectories to compile.
[oota-llvm.git] / lib / VMCore / iMemory.cpp
1 //===-- iMemory.cpp - Implement Memory instructions -----------------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the various memory related classes defined in iMemory.h
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/iMemory.h"
15 #include "llvm/Constants.h"
16 #include "llvm/DerivedTypes.h"
17 using namespace llvm;
18
19 AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, 
20                                const std::string &Name, Instruction *InsertBef)
21   : Instruction(PointerType::get(Ty), iTy, Name, InsertBef) {
22
23   // ArraySize defaults to 1.
24   if (!ArraySize) ArraySize = ConstantUInt::get(Type::UIntTy, 1);
25
26   Operands.reserve(1);
27   assert(ArraySize->getType() == Type::UIntTy &&
28          "Malloc/Allocation array size != UIntTy!");
29
30   Operands.push_back(Use(ArraySize, this));
31 }
32
33 bool AllocationInst::isArrayAllocation() const {
34   return getOperand(0) != ConstantUInt::get(Type::UIntTy, 1);
35 }
36
37 const Type *AllocationInst::getAllocatedType() const {
38   return getType()->getElementType();
39 }
40
41 AllocaInst::AllocaInst(const AllocaInst &AI)
42   : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
43                    Instruction::Alloca) {
44 }
45
46 MallocInst::MallocInst(const MallocInst &MI)
47   : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
48                    Instruction::Malloc) {
49 }
50
51 //===----------------------------------------------------------------------===//
52 //                             FreeInst Implementation
53 //===----------------------------------------------------------------------===//
54
55 FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
56   : Instruction(Type::VoidTy, Free, "", InsertBefore) {
57   assert(isa<PointerType>(Ptr->getType()) && "Can't free nonpointer!");
58   Operands.reserve(1);
59   Operands.push_back(Use(Ptr, this));
60 }
61
62
63 //===----------------------------------------------------------------------===//
64 //                           LoadInst Implementation
65 //===----------------------------------------------------------------------===//
66
67 LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
68   : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
69                 Load, Name, InsertBef), Volatile(false) {
70   Operands.reserve(1);
71   Operands.push_back(Use(Ptr, this));
72 }
73
74 LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
75                    Instruction *InsertBef)
76   : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
77                 Load, Name, InsertBef), Volatile(isVolatile) {
78   Operands.reserve(1);
79   Operands.push_back(Use(Ptr, this));
80 }
81
82 //===----------------------------------------------------------------------===//
83 //                           StoreInst Implementation
84 //===----------------------------------------------------------------------===//
85
86 StoreInst::StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore)
87   : Instruction(Type::VoidTy, Store, "", InsertBefore), Volatile(false) {
88   
89   Operands.reserve(2);
90   Operands.push_back(Use(Val, this));
91   Operands.push_back(Use(Ptr, this));
92 }
93
94 StoreInst::StoreInst(Value *Val, Value *Ptr, bool isVolatile, 
95                      Instruction *InsertBefore)
96   : Instruction(Type::VoidTy, Store, "", InsertBefore), Volatile(isVolatile) {
97   
98   Operands.reserve(2);
99   Operands.push_back(Use(Val, this));
100   Operands.push_back(Use(Ptr, this));
101 }
102
103
104 //===----------------------------------------------------------------------===//
105 //                       GetElementPtrInst Implementation
106 //===----------------------------------------------------------------------===//
107
108 // checkType - Simple wrapper function to give a better assertion failure
109 // message on bad indexes for a gep instruction.
110 //
111 static inline const Type *checkType(const Type *Ty) {
112   assert(Ty && "Invalid indices for type!");
113   return Ty;
114 }
115
116 GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
117                                      const std::string &Name, Instruction *InBe)
118   : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
119                                                             Idx, true))),
120                   GetElementPtr, Name, InBe) {
121   Operands.reserve(1+Idx.size());
122   Operands.push_back(Use(Ptr, this));
123
124   for (unsigned i = 0, E = Idx.size(); i != E; ++i)
125     Operands.push_back(Use(Idx[i], this));
126 }
127
128 // getIndexedType - Returns the type of the element that would be loaded with
129 // a load instruction with the specified parameters.
130 //
131 // A null type is returned if the indices are invalid for the specified 
132 // pointer type.
133 //
134 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, 
135                                               const std::vector<Value*> &Idx,
136                                               bool AllowCompositeLeaf) {
137   if (!isa<PointerType>(Ptr)) return 0;   // Type isn't a pointer type!
138
139   // Handle the special case of the empty set index set...
140   if (Idx.empty()) return cast<PointerType>(Ptr)->getElementType();
141  
142   unsigned CurIdx = 0;
143   while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
144     if (Idx.size() == CurIdx) {
145       if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
146       return 0;   // Can't load a whole structure or array!?!?
147     }
148
149     Value *Index = Idx[CurIdx++];
150     if (isa<PointerType>(CT) && CurIdx != 1)
151       return 0;  // Can only index into pointer types at the first index!
152     if (!CT->indexValid(Index)) return 0;
153     Ptr = CT->getTypeAtIndex(Index);
154   }
155   return CurIdx == Idx.size() ? Ptr : 0;
156 }