blob: 8aea15430833caadf223e2d771db5e4263f96674 [file] [log] [blame]
Chris Lattnere7fb3602001-08-27 16:00:15 +00001//===-- TargetData.cpp - Data size & alignment routines --------------------==//
John Criswellb576c942003-10-20 19:43:21 +00002//
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//===----------------------------------------------------------------------===//
Chris Lattnere7fb3602001-08-27 16:00:15 +00009//
10// This file defines target properties related to datatype size/offset/alignment
Chris Lattner0e7ac162004-02-26 08:02:17 +000011// information.
Chris Lattnere7fb3602001-08-27 16:00:15 +000012//
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
Vikram S. Adve0799fc42001-09-18 12:58:33 +000019#include "llvm/Target/TargetData.h"
Chris Lattner53a0c382003-04-24 19:09:05 +000020#include "llvm/Module.h"
Chris Lattnere7fb3602001-08-27 16:00:15 +000021#include "llvm/DerivedTypes.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000022#include "llvm/Constants.h"
Chris Lattner28977af2004-04-05 01:30:19 +000023#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattnerf0453282003-12-22 05:01:15 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Misha Brukman5560c9d2003-08-18 14:43:39 +000026// Handle the Pass registration stuff necessary to use TargetData's.
Chris Lattneraa31ad02002-09-25 23:46:55 +000027namespace {
28 // Register the default SparcV9 implementation...
29 RegisterPass<TargetData> X("targetdata", "Target Data Layout");
30}
31
Chris Lattnere7fb3602001-08-27 16:00:15 +000032static inline void getTypeInfo(const Type *Ty, const TargetData *TD,
Misha Brukmanc8e87642004-07-23 01:09:52 +000033 uint64_t &Size, unsigned char &Alignment);
Chris Lattnere7fb3602001-08-27 16:00:15 +000034
35//===----------------------------------------------------------------------===//
Chris Lattner0e7ac162004-02-26 08:02:17 +000036// Support for StructLayout
Chris Lattnere7fb3602001-08-27 16:00:15 +000037//===----------------------------------------------------------------------===//
38
Chris Lattner0e7ac162004-02-26 08:02:17 +000039StructLayout::StructLayout(const StructType *ST, const TargetData &TD) {
Chris Lattnere7fb3602001-08-27 16:00:15 +000040 StructAlignment = 0;
41 StructSize = 0;
42
43 // Loop over each of the elements, placing them in memory...
Chris Lattnerd21cd802004-02-09 04:37:31 +000044 for (StructType::element_iterator TI = ST->element_begin(),
Misha Brukmanc8e87642004-07-23 01:09:52 +000045 TE = ST->element_end(); TI != TE; ++TI) {
Chris Lattnere7fb3602001-08-27 16:00:15 +000046 const Type *Ty = *TI;
47 unsigned char A;
Vikram S. Advef66723f2002-05-19 15:28:02 +000048 unsigned TyAlign;
49 uint64_t TySize;
50 getTypeInfo(Ty, &TD, TySize, A);
51 TyAlign = A;
Chris Lattnere7fb3602001-08-27 16:00:15 +000052
Misha Brukman5560c9d2003-08-18 14:43:39 +000053 // Add padding if necessary to make the data element aligned properly...
Chris Lattnere7fb3602001-08-27 16:00:15 +000054 if (StructSize % TyAlign != 0)
55 StructSize = (StructSize/TyAlign + 1) * TyAlign; // Add padding...
56
57 // Keep track of maximum alignment constraint
Chris Lattner697954c2002-01-20 22:54:45 +000058 StructAlignment = std::max(TyAlign, StructAlignment);
Chris Lattnere7fb3602001-08-27 16:00:15 +000059
60 MemberOffsets.push_back(StructSize);
Vikram S. Advef66723f2002-05-19 15:28:02 +000061 StructSize += TySize; // Consume space for this data item
Chris Lattnere7fb3602001-08-27 16:00:15 +000062 }
63
Chris Lattner4e840d42003-05-21 18:08:44 +000064 // Empty structures have alignment of 1 byte.
65 if (StructAlignment == 0) StructAlignment = 1;
66
Chris Lattnere7fb3602001-08-27 16:00:15 +000067 // 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;
Chris Lattnere7fb3602001-08-27 16:00:15 +000071}
72
Chris Lattnere7fb3602001-08-27 16:00:15 +000073//===----------------------------------------------------------------------===//
74// TargetData Class Implementation
75//===----------------------------------------------------------------------===//
76
Vikram S. Advef66723f2002-05-19 15:28:02 +000077TargetData::TargetData(const std::string &TargetName,
Chris Lattner10daaa12003-04-26 20:11:09 +000078 bool isLittleEndian, unsigned char PtrSize,
Chris Lattner85131c82002-10-14 22:41:13 +000079 unsigned char PtrAl, unsigned char DoubleAl,
80 unsigned char FloatAl, unsigned char LongAl,
81 unsigned char IntAl, unsigned char ShortAl,
Misha Brukmanc8e87642004-07-23 01:09:52 +000082 unsigned char ByteAl, unsigned char BoolAl) {
Chris Lattnere7fb3602001-08-27 16:00:15 +000083
Chris Lattner46326d92003-04-25 02:50:45 +000084 // If this assert triggers, a pass "required" TargetData information, but the
Brian Gaeke8121fcd2004-04-14 21:21:56 +000085 // top level tool did not provide one for it. We do not want to default
Chris Lattner46326d92003-04-25 02:50:45 +000086 // 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
Chris Lattner85131c82002-10-14 22:41:13 +000091 LittleEndian = isLittleEndian;
Chris Lattnere7fb3602001-08-27 16:00:15 +000092 PointerSize = PtrSize;
93 PointerAlignment = PtrAl;
94 DoubleAlignment = DoubleAl;
95 FloatAlignment = FloatAl;
96 LongAlignment = LongAl;
97 IntAlignment = IntAl;
98 ShortAlignment = ShortAl;
99 ByteAlignment = ByteAl;
Misha Brukmanc8e87642004-07-23 01:09:52 +0000100 BoolAlignment = BoolAl;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000101}
102
Chris Lattner0e7ac162004-02-26 08:02:17 +0000103TargetData::TargetData(const std::string &ToolName, const Module *M) {
Chris Lattner030574f2003-08-24 13:49:22 +0000104 LittleEndian = M->getEndianness() != Module::BigEndian;
105 PointerSize = M->getPointerSize() != Module::Pointer64 ? 4 : 8;
Chris Lattner53a0c382003-04-24 19:09:05 +0000106 PointerAlignment = PointerSize;
Chris Lattnerdd7253c2003-04-25 06:06:43 +0000107 DoubleAlignment = PointerSize;
Chris Lattner53a0c382003-04-24 19:09:05 +0000108 FloatAlignment = 4;
109 LongAlignment = 8;
110 IntAlignment = 4;
111 ShortAlignment = 2;
112 ByteAlignment = 1;
Misha Brukmanc8e87642004-07-23 01:09:52 +0000113 BoolAlignment = 1;
Chris Lattner53a0c382003-04-24 19:09:05 +0000114}
115
Chris Lattner0e7ac162004-02-26 08:02:17 +0000116static std::map<std::pair<const TargetData*,const StructType*>,
117 StructLayout> *Layouts = 0;
118
119
Chris Lattnere7fb3602001-08-27 16:00:15 +0000120TargetData::~TargetData() {
Chris Lattner0e7ac162004-02-26 08:02:17 +0000121 if (Layouts) {
122 // Remove any layouts for this TD.
123 std::map<std::pair<const TargetData*,
124 const StructType*>, StructLayout>::iterator
125 I = Layouts->lower_bound(std::make_pair(this, (const StructType*)0));
126 while (I != Layouts->end() && I->first.first == this)
127 Layouts->erase(I++);
128 if (Layouts->empty()) {
129 delete Layouts;
130 Layouts = 0;
131 }
132 }
133}
134
135const StructLayout *TargetData::getStructLayout(const StructType *Ty) const {
136 if (Layouts == 0)
137 Layouts = new std::map<std::pair<const TargetData*,const StructType*>,
138 StructLayout>();
139 std::map<std::pair<const TargetData*,const StructType*>,
140 StructLayout>::iterator
141 I = Layouts->lower_bound(std::make_pair(this, Ty));
142 if (I != Layouts->end() && I->first.first == this && I->first.second == Ty)
143 return &I->second;
144 else {
145 return &Layouts->insert(I, std::make_pair(std::make_pair(this, Ty),
146 StructLayout(Ty, *this)))->second;
147 }
Chris Lattnere7fb3602001-08-27 16:00:15 +0000148}
149
150static inline void getTypeInfo(const Type *Ty, const TargetData *TD,
Misha Brukmanc8e87642004-07-23 01:09:52 +0000151 uint64_t &Size, unsigned char &Alignment) {
Chris Lattnerf59ce922001-12-13 00:46:11 +0000152 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000153 switch (Ty->getTypeID()) {
Misha Brukmanc8e87642004-07-23 01:09:52 +0000154 case Type::BoolTyID: Size = 1; Alignment = TD->getBoolAlignment(); return;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000155 case Type::VoidTyID:
Chris Lattnere7fb3602001-08-27 16:00:15 +0000156 case Type::UByteTyID:
157 case Type::SByteTyID: Size = 1; Alignment = TD->getByteAlignment(); return;
158 case Type::UShortTyID:
159 case Type::ShortTyID: Size = 2; Alignment = TD->getShortAlignment(); return;
160 case Type::UIntTyID:
161 case Type::IntTyID: Size = 4; Alignment = TD->getIntAlignment(); return;
162 case Type::ULongTyID:
163 case Type::LongTyID: Size = 8; Alignment = TD->getLongAlignment(); return;
164 case Type::FloatTyID: Size = 4; Alignment = TD->getFloatAlignment(); return;
165 case Type::DoubleTyID: Size = 8; Alignment = TD->getDoubleAlignment(); return;
166 case Type::LabelTyID:
167 case Type::PointerTyID:
168 Size = TD->getPointerSize(); Alignment = TD->getPointerAlignment();
169 return;
170 case Type::ArrayTyID: {
Chris Lattner59b00672004-07-01 17:32:59 +0000171 const ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattnere7fb3602001-08-27 16:00:15 +0000172 getTypeInfo(ATy->getElementType(), TD, Size, Alignment);
Brian Gaekee0e35892004-07-02 07:01:31 +0000173 unsigned AlignedSize = (Size + Alignment - 1)/Alignment*Alignment;
Chris Lattner59b00672004-07-01 17:32:59 +0000174 Size = AlignedSize*ATy->getNumElements();
Chris Lattnere7fb3602001-08-27 16:00:15 +0000175 return;
176 }
177 case Type::StructTyID: {
178 // Get the layout annotation... which is lazily created on demand.
Chris Lattner59b00672004-07-01 17:32:59 +0000179 const StructLayout *Layout = TD->getStructLayout(cast<StructType>(Ty));
Chris Lattnere7fb3602001-08-27 16:00:15 +0000180 Size = Layout->StructSize; Alignment = Layout->StructAlignment;
181 return;
182 }
183
Chris Lattnere7fb3602001-08-27 16:00:15 +0000184 default:
185 assert(0 && "Bad type for getTypeInfo!!!");
186 return;
187 }
188}
189
Vikram S. Advef66723f2002-05-19 15:28:02 +0000190uint64_t TargetData::getTypeSize(const Type *Ty) const {
191 uint64_t Size;
192 unsigned char Align;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000193 getTypeInfo(Ty, this, Size, Align);
194 return Size;
195}
196
197unsigned char TargetData::getTypeAlignment(const Type *Ty) const {
Vikram S. Advef66723f2002-05-19 15:28:02 +0000198 uint64_t Size;
199 unsigned char Align;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000200 getTypeInfo(Ty, this, Size, Align);
201 return Align;
202}
203
Chris Lattnerf0453282003-12-22 05:01:15 +0000204/// getIntPtrType - Return an unsigned integer type that is the same size or
205/// greater to the host pointer size.
206const Type *TargetData::getIntPtrType() const {
207 switch (getPointerSize()) {
208 default: assert(0 && "Unknown pointer size!");
209 case 2: return Type::UShortTy;
210 case 4: return Type::UIntTy;
211 case 8: return Type::ULongTy;
212 }
213}
214
215
Vikram S. Advef66723f2002-05-19 15:28:02 +0000216uint64_t TargetData::getIndexedOffset(const Type *ptrTy,
Misha Brukmanc8e87642004-07-23 01:09:52 +0000217 const std::vector<Value*> &Idx) const {
Vikram S. Adveed0030e2002-08-04 20:52:39 +0000218 const Type *Ty = ptrTy;
219 assert(isa<PointerType>(Ty) && "Illegal argument for getIndexedOffset()");
Vikram S. Advef66723f2002-05-19 15:28:02 +0000220 uint64_t Result = 0;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000221
Chris Lattner28977af2004-04-05 01:30:19 +0000222 generic_gep_type_iterator<std::vector<Value*>::const_iterator>
223 TI = gep_type_begin(ptrTy, Idx.begin(), Idx.end());
224 for (unsigned CurIDX = 0; CurIDX != Idx.size(); ++CurIDX, ++TI) {
225 if (const StructType *STy = dyn_cast<StructType>(*TI)) {
226 assert(Idx[CurIDX]->getType() == Type::UIntTy && "Illegal struct idx");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000227 unsigned FieldNo = cast<ConstantUInt>(Idx[CurIDX])->getValue();
Chris Lattnere7fb3602001-08-27 16:00:15 +0000228
229 // Get structure layout information...
230 const StructLayout *Layout = getStructLayout(STy);
231
232 // Add in the offset, as calculated by the structure layout info...
Vikram S. Advef66723f2002-05-19 15:28:02 +0000233 assert(FieldNo < Layout->MemberOffsets.size() &&"FieldNo out of range!");
Chris Lattnere7fb3602001-08-27 16:00:15 +0000234 Result += Layout->MemberOffsets[FieldNo];
Vikram S. Adveed0030e2002-08-04 20:52:39 +0000235
Chris Lattnere7fb3602001-08-27 16:00:15 +0000236 // Update Ty to refer to current element
Chris Lattnerd21cd802004-02-09 04:37:31 +0000237 Ty = STy->getElementType(FieldNo);
Chris Lattner28977af2004-04-05 01:30:19 +0000238 } else {
239 // Update Ty to refer to current element
240 Ty = cast<SequentialType>(Ty)->getElementType();
241
242 // Get the array index and the size of each array element.
243 int64_t arrayIdx = cast<ConstantInt>(Idx[CurIDX])->getRawValue();
244 Result += arrayIdx * (int64_t)getTypeSize(Ty);
Chris Lattnere7fb3602001-08-27 16:00:15 +0000245 }
246 }
247
248 return Result;
249}
Brian Gaeked0fde302003-11-11 22:41:34 +0000250