Nuke a clearly bogus assertion
[oota-llvm.git] / lib / Target / TargetData.cpp
1 //===-- TargetData.cpp - Data size & alignment routines --------------------==//
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 defines target properties related to datatype size/offset/alignment
11 // information.
12 //
13 // This structure should be created once, filled in if the defaults are not
14 // correct and then passed around by const&.  None of the members functions
15 // require modification to the object.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #include "llvm/Target/TargetData.h"
20 #include "llvm/Module.h"
21 #include "llvm/DerivedTypes.h"
22 #include "llvm/Constants.h"
23 #include "llvm/Support/GetElementPtrTypeIterator.h"
24 using namespace llvm;
25
26 // Handle the Pass registration stuff necessary to use TargetData's.
27 namespace {
28   // Register the default SparcV9 implementation...
29   RegisterPass<TargetData> X("targetdata", "Target Data Layout");
30 }
31
32 static inline void getTypeInfo(const Type *Ty, const TargetData *TD,
33                                uint64_t &Size, unsigned char &Alignment);
34
35 //===----------------------------------------------------------------------===//
36 // Support for StructLayout
37 //===----------------------------------------------------------------------===//
38
39 StructLayout::StructLayout(const StructType *ST, const TargetData &TD) {
40   StructAlignment = 0;
41   StructSize = 0;
42
43   // Loop over each of the elements, placing them in memory...
44   for (StructType::element_iterator TI = ST->element_begin(), 
45          TE = ST->element_end(); TI != TE; ++TI) {
46     const Type *Ty = *TI;
47     unsigned char A;
48     unsigned TyAlign;
49     uint64_t TySize;
50     getTypeInfo(Ty, &TD, TySize, A);
51     TyAlign = A;
52
53     // Add padding if necessary to make the data element aligned properly...
54     if (StructSize % TyAlign != 0)
55       StructSize = (StructSize/TyAlign + 1) * TyAlign;   // Add padding...
56
57     // Keep track of maximum alignment constraint
58     StructAlignment = std::max(TyAlign, StructAlignment);
59
60     MemberOffsets.push_back(StructSize);
61     StructSize += TySize;                 // Consume space for this data item
62   }
63
64   // Empty structures have alignment of 1 byte.
65   if (StructAlignment == 0) StructAlignment = 1;
66
67   // Add padding to the end of the struct so that it could be put in an array
68   // and all array elements would be aligned correctly.
69   if (StructSize % StructAlignment != 0)
70     StructSize = (StructSize/StructAlignment + 1) * StructAlignment;
71 }
72
73 //===----------------------------------------------------------------------===//
74 //                       TargetData Class Implementation
75 //===----------------------------------------------------------------------===//
76
77 TargetData::TargetData(const std::string &TargetName,
78                        bool isLittleEndian, unsigned char PtrSize,
79                        unsigned char PtrAl, unsigned char DoubleAl,
80                        unsigned char FloatAl, unsigned char LongAl, 
81                        unsigned char IntAl, unsigned char ShortAl,
82                        unsigned char ByteAl) {
83
84   // If this assert triggers, a pass "required" TargetData information, but the
85   // top level tool did not provide one for it.  We do not want to default
86   // construct, or else we might end up using a bad endianness or pointer size!
87   //
88   assert(!TargetName.empty() &&
89          "ERROR: Tool did not specify a target data to use!");
90
91   LittleEndian     = isLittleEndian;
92   PointerSize      = PtrSize;
93   PointerAlignment = PtrAl;
94   DoubleAlignment  = DoubleAl;
95   FloatAlignment   = FloatAl;
96   LongAlignment    = LongAl;
97   IntAlignment     = IntAl;
98   ShortAlignment   = ShortAl;
99   ByteAlignment    = ByteAl;
100 }
101
102 TargetData::TargetData(const std::string &ToolName, const Module *M) {
103   LittleEndian     = M->getEndianness() != Module::BigEndian;
104   PointerSize      = M->getPointerSize() != Module::Pointer64 ? 4 : 8;
105   PointerAlignment = PointerSize;
106   DoubleAlignment  = PointerSize;
107   FloatAlignment   = 4;
108   LongAlignment    = 8;
109   IntAlignment     = 4;
110   ShortAlignment   = 2;
111   ByteAlignment    = 1;
112 }
113
114 static std::map<std::pair<const TargetData*,const StructType*>,
115                 StructLayout> *Layouts = 0;
116
117
118 TargetData::~TargetData() {
119   if (Layouts) {
120     // Remove any layouts for this TD.
121     std::map<std::pair<const TargetData*,
122       const StructType*>, StructLayout>::iterator
123       I = Layouts->lower_bound(std::make_pair(this, (const StructType*)0));
124     while (I != Layouts->end() && I->first.first == this)
125       Layouts->erase(I++);
126     if (Layouts->empty()) {
127       delete Layouts;
128       Layouts = 0;
129     }
130   }
131 }
132
133 const StructLayout *TargetData::getStructLayout(const StructType *Ty) const {
134   if (Layouts == 0)
135     Layouts = new std::map<std::pair<const TargetData*,const StructType*>,
136                            StructLayout>();
137   std::map<std::pair<const TargetData*,const StructType*>,
138                      StructLayout>::iterator
139     I = Layouts->lower_bound(std::make_pair(this, Ty));
140   if (I != Layouts->end() && I->first.first == this && I->first.second == Ty)
141     return &I->second;
142   else {
143     return &Layouts->insert(I, std::make_pair(std::make_pair(this, Ty),
144                                               StructLayout(Ty, *this)))->second;
145   }
146 }
147
148 static inline void getTypeInfo(const Type *Ty, const TargetData *TD,
149                                uint64_t &Size, unsigned char &Alignment) {
150   assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
151   switch (Ty->getTypeID()) {
152   case Type::VoidTyID:
153   case Type::BoolTyID:
154   case Type::UByteTyID:
155   case Type::SByteTyID:  Size = 1; Alignment = TD->getByteAlignment(); return;
156   case Type::UShortTyID:
157   case Type::ShortTyID:  Size = 2; Alignment = TD->getShortAlignment(); return;
158   case Type::UIntTyID:
159   case Type::IntTyID:    Size = 4; Alignment = TD->getIntAlignment(); return;
160   case Type::ULongTyID:
161   case Type::LongTyID:   Size = 8; Alignment = TD->getLongAlignment(); return;
162   case Type::FloatTyID:  Size = 4; Alignment = TD->getFloatAlignment(); return;
163   case Type::DoubleTyID: Size = 8; Alignment = TD->getDoubleAlignment(); return;
164   case Type::LabelTyID:
165   case Type::PointerTyID:
166     Size = TD->getPointerSize(); Alignment = TD->getPointerAlignment();
167     return;
168   case Type::ArrayTyID: {
169     const ArrayType *ATy = cast<ArrayType>(Ty);
170     getTypeInfo(ATy->getElementType(), TD, Size, Alignment);
171     unsigned AlignedSize = (Size + Alignment - 1)/Alignment*Alignment;
172     Size = AlignedSize*ATy->getNumElements();
173     return;
174   }
175   case Type::StructTyID: {
176     // Get the layout annotation... which is lazily created on demand.
177     const StructLayout *Layout = TD->getStructLayout(cast<StructType>(Ty));
178     Size = Layout->StructSize; Alignment = Layout->StructAlignment;
179     return;
180   }
181     
182   default:
183     assert(0 && "Bad type for getTypeInfo!!!");
184     return;
185   }
186 }
187
188 uint64_t TargetData::getTypeSize(const Type *Ty) const {
189   uint64_t Size;
190   unsigned char Align;
191   getTypeInfo(Ty, this, Size, Align);
192   return Size;
193 }
194
195 unsigned char TargetData::getTypeAlignment(const Type *Ty) const {
196   uint64_t Size;
197   unsigned char Align;
198   getTypeInfo(Ty, this, Size, Align);
199   return Align;
200 }
201
202 /// getIntPtrType - Return an unsigned integer type that is the same size or
203 /// greater to the host pointer size.
204 const Type *TargetData::getIntPtrType() const {
205   switch (getPointerSize()) {
206   default: assert(0 && "Unknown pointer size!");
207   case 2: return Type::UShortTy;
208   case 4: return Type::UIntTy;
209   case 8: return Type::ULongTy;
210   }
211 }
212
213
214 uint64_t TargetData::getIndexedOffset(const Type *ptrTy,
215                                       const std::vector<Value*> &Idx) const {
216   const Type *Ty = ptrTy;
217   assert(isa<PointerType>(Ty) && "Illegal argument for getIndexedOffset()");
218   uint64_t Result = 0;
219
220   generic_gep_type_iterator<std::vector<Value*>::const_iterator>
221     TI = gep_type_begin(ptrTy, Idx.begin(), Idx.end());
222   for (unsigned CurIDX = 0; CurIDX != Idx.size(); ++CurIDX, ++TI) {
223     if (const StructType *STy = dyn_cast<StructType>(*TI)) {
224       assert(Idx[CurIDX]->getType() == Type::UIntTy && "Illegal struct idx");
225       unsigned FieldNo = cast<ConstantUInt>(Idx[CurIDX])->getValue();
226
227       // Get structure layout information...
228       const StructLayout *Layout = getStructLayout(STy);
229
230       // Add in the offset, as calculated by the structure layout info...
231       assert(FieldNo < Layout->MemberOffsets.size() &&"FieldNo out of range!");
232       Result += Layout->MemberOffsets[FieldNo];
233
234       // Update Ty to refer to current element
235       Ty = STy->getElementType(FieldNo);
236     } else {
237       // Update Ty to refer to current element
238       Ty = cast<SequentialType>(Ty)->getElementType();
239
240       // Get the array index and the size of each array element.
241       int64_t arrayIdx = cast<ConstantInt>(Idx[CurIDX])->getRawValue();
242       Result += arrayIdx * (int64_t)getTypeSize(Ty);
243     }
244   }
245
246   return Result;
247 }
248