Add support for tracking whether a module is 64/32 bit and big/little endian
[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(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) {
62   Operands.reserve(1);
63   Operands.push_back(Use(Ptr, this));
64 }
65
66
67 //===----------------------------------------------------------------------===//
68 //                           StoreInst Implementation
69 //===----------------------------------------------------------------------===//
70
71 StoreInst::StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore)
72   : Instruction(Type::VoidTy, Store, "", InsertBefore) {
73   
74   Operands.reserve(2);
75   Operands.push_back(Use(Val, this));
76   Operands.push_back(Use(Ptr, this));
77 }
78
79
80 //===----------------------------------------------------------------------===//
81 //                       GetElementPtrInst Implementation
82 //===----------------------------------------------------------------------===//
83
84 // checkType - Simple wrapper function to give a better assertion failure
85 // message on bad indexes for a gep instruction.
86 //
87 static inline const Type *checkType(const Type *Ty) {
88   assert(Ty && "Invalid indices for type!");
89   return Ty;
90 }
91
92 GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
93                                      const std::string &Name, Instruction *InBe)
94   : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
95                                                             Idx, true))),
96                   GetElementPtr, Name, InBe) {
97   assert(getIndexedType(Ptr->getType(), Idx, true) && "gep operands invalid!");
98   Operands.reserve(1+Idx.size());
99   Operands.push_back(Use(Ptr, this));
100
101   for (unsigned i = 0, E = Idx.size(); i != E; ++i)
102     Operands.push_back(Use(Idx[i], this));
103 }
104
105 // getIndexedType - Returns the type of the element that would be loaded with
106 // a load instruction with the specified parameters.
107 //
108 // A null type is returned if the indices are invalid for the specified 
109 // pointer type.
110 //
111 const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, 
112                                               const std::vector<Value*> &Idx,
113                                               bool AllowCompositeLeaf) {
114   if (!isa<PointerType>(Ptr)) return 0;   // Type isn't a pointer type!
115
116   // Handle the special case of the empty set index set...
117   if (Idx.empty()) return cast<PointerType>(Ptr)->getElementType();
118  
119   unsigned CurIdx = 0;
120   while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
121     if (Idx.size() == CurIdx) {
122       if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
123       return 0;   // Can't load a whole structure or array!?!?
124     }
125
126     Value *Index = Idx[CurIdx++];
127     if (isa<PointerType>(CT) && CurIdx != 1)
128       return 0;  // Can only index into pointer types at the first index!
129     if (!CT->indexValid(Index)) return 0;
130     Ptr = CT->getTypeAtIndex(Index);
131   }
132   return CurIdx == Idx.size() ? Ptr : 0;
133 }