blob: e5f391b6e211ede22795634f64952811b0f08d26 [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,
Vikram S. Advef66723f2002-05-19 15:28:02 +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(),
45 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,
Chris Lattner0e7ac162004-02-26 08:02:17 +000082 unsigned char ByteAl) {
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;
100}
101
Chris Lattner0e7ac162004-02-26 08:02:17 +0000102TargetData::TargetData(const std::string &ToolName, const Module *M) {
Chris Lattner030574f2003-08-24 13:49:22 +0000103 LittleEndian = M->getEndianness() != Module::BigEndian;
104 PointerSize = M->getPointerSize() != Module::Pointer64 ? 4 : 8;
Chris Lattner53a0c382003-04-24 19:09:05 +0000105 PointerAlignment = PointerSize;
Chris Lattnerdd7253c2003-04-25 06:06:43 +0000106 DoubleAlignment = PointerSize;
Chris Lattner53a0c382003-04-24 19:09:05 +0000107 FloatAlignment = 4;
108 LongAlignment = 8;
109 IntAlignment = 4;
110 ShortAlignment = 2;
111 ByteAlignment = 1;
112}
113
Chris Lattner0e7ac162004-02-26 08:02:17 +0000114static std::map<std::pair<const TargetData*,const StructType*>,
115 StructLayout> *Layouts = 0;
116
117
Chris Lattnere7fb3602001-08-27 16:00:15 +0000118TargetData::~TargetData() {
Chris Lattner0e7ac162004-02-26 08:02:17 +0000119 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
133const 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 }
Chris Lattnere7fb3602001-08-27 16:00:15 +0000146}
147
148static inline void getTypeInfo(const Type *Ty, const TargetData *TD,
Vikram S. Advef66723f2002-05-19 15:28:02 +0000149 uint64_t &Size, unsigned char &Alignment) {
Chris Lattnerf59ce922001-12-13 00:46:11 +0000150 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000151 switch (Ty->getTypeID()) {
Chris Lattnere7fb3602001-08-27 16:00:15 +0000152 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: {
Chris Lattner59b00672004-07-01 17:32:59 +0000169 const ArrayType *ATy = cast<ArrayType>(Ty);
Chris Lattnere7fb3602001-08-27 16:00:15 +0000170 getTypeInfo(ATy->getElementType(), TD, Size, Alignment);
Brian Gaekee0e35892004-07-02 07:01:31 +0000171 unsigned AlignedSize = (Size + Alignment - 1)/Alignment*Alignment;
Chris Lattner59b00672004-07-01 17:32:59 +0000172 Size = AlignedSize*ATy->getNumElements();
Chris Lattnere7fb3602001-08-27 16:00:15 +0000173 return;
174 }
175 case Type::StructTyID: {
176 // Get the layout annotation... which is lazily created on demand.
Chris Lattner59b00672004-07-01 17:32:59 +0000177 const StructLayout *Layout = TD->getStructLayout(cast<StructType>(Ty));
Chris Lattnere7fb3602001-08-27 16:00:15 +0000178 Size = Layout->StructSize; Alignment = Layout->StructAlignment;
179 return;
180 }
181
Chris Lattnere7fb3602001-08-27 16:00:15 +0000182 default:
183 assert(0 && "Bad type for getTypeInfo!!!");
184 return;
185 }
186}
187
Vikram S. Advef66723f2002-05-19 15:28:02 +0000188uint64_t TargetData::getTypeSize(const Type *Ty) const {
189 uint64_t Size;
190 unsigned char Align;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000191 getTypeInfo(Ty, this, Size, Align);
192 return Size;
193}
194
195unsigned char TargetData::getTypeAlignment(const Type *Ty) const {
Vikram S. Advef66723f2002-05-19 15:28:02 +0000196 uint64_t Size;
197 unsigned char Align;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000198 getTypeInfo(Ty, this, Size, Align);
199 return Align;
200}
201
Chris Lattnerf0453282003-12-22 05:01:15 +0000202/// getIntPtrType - Return an unsigned integer type that is the same size or
203/// greater to the host pointer size.
204const 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
Vikram S. Advef66723f2002-05-19 15:28:02 +0000214uint64_t TargetData::getIndexedOffset(const Type *ptrTy,
Chris Lattner697954c2002-01-20 22:54:45 +0000215 const std::vector<Value*> &Idx) const {
Vikram S. Adveed0030e2002-08-04 20:52:39 +0000216 const Type *Ty = ptrTy;
217 assert(isa<PointerType>(Ty) && "Illegal argument for getIndexedOffset()");
Vikram S. Advef66723f2002-05-19 15:28:02 +0000218 uint64_t Result = 0;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000219
Chris Lattner28977af2004-04-05 01:30:19 +0000220 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");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000225 unsigned FieldNo = cast<ConstantUInt>(Idx[CurIDX])->getValue();
Chris Lattnere7fb3602001-08-27 16:00:15 +0000226
227 // Get structure layout information...
228 const StructLayout *Layout = getStructLayout(STy);
229
230 // Add in the offset, as calculated by the structure layout info...
Vikram S. Advef66723f2002-05-19 15:28:02 +0000231 assert(FieldNo < Layout->MemberOffsets.size() &&"FieldNo out of range!");
Chris Lattnere7fb3602001-08-27 16:00:15 +0000232 Result += Layout->MemberOffsets[FieldNo];
Vikram S. Adveed0030e2002-08-04 20:52:39 +0000233
Chris Lattnere7fb3602001-08-27 16:00:15 +0000234 // Update Ty to refer to current element
Chris Lattnerd21cd802004-02-09 04:37:31 +0000235 Ty = STy->getElementType(FieldNo);
Chris Lattner28977af2004-04-05 01:30:19 +0000236 } 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);
Chris Lattnere7fb3602001-08-27 16:00:15 +0000243 }
244 }
245
246 return Result;
247}
Brian Gaeked0fde302003-11-11 22:41:34 +0000248