blob: 271b6d8f975b9e578395300e624050ef2be0c630 [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 Lattnerf0453282003-12-22 05:01:15 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Misha Brukman5560c9d2003-08-18 14:43:39 +000025// Handle the Pass registration stuff necessary to use TargetData's.
Chris Lattneraa31ad02002-09-25 23:46:55 +000026namespace {
27 // Register the default SparcV9 implementation...
28 RegisterPass<TargetData> X("targetdata", "Target Data Layout");
29}
30
Chris Lattnere7fb3602001-08-27 16:00:15 +000031static inline void getTypeInfo(const Type *Ty, const TargetData *TD,
Vikram S. Advef66723f2002-05-19 15:28:02 +000032 uint64_t &Size, unsigned char &Alignment);
Chris Lattnere7fb3602001-08-27 16:00:15 +000033
34//===----------------------------------------------------------------------===//
Chris Lattner0e7ac162004-02-26 08:02:17 +000035// Support for StructLayout
Chris Lattnere7fb3602001-08-27 16:00:15 +000036//===----------------------------------------------------------------------===//
37
Chris Lattner0e7ac162004-02-26 08:02:17 +000038StructLayout::StructLayout(const StructType *ST, const TargetData &TD) {
Chris Lattnere7fb3602001-08-27 16:00:15 +000039 StructAlignment = 0;
40 StructSize = 0;
41
42 // Loop over each of the elements, placing them in memory...
Chris Lattnerd21cd802004-02-09 04:37:31 +000043 for (StructType::element_iterator TI = ST->element_begin(),
44 TE = ST->element_end(); TI != TE; ++TI) {
Chris Lattnere7fb3602001-08-27 16:00:15 +000045 const Type *Ty = *TI;
46 unsigned char A;
Vikram S. Advef66723f2002-05-19 15:28:02 +000047 unsigned TyAlign;
48 uint64_t TySize;
49 getTypeInfo(Ty, &TD, TySize, A);
50 TyAlign = A;
Chris Lattnere7fb3602001-08-27 16:00:15 +000051
Misha Brukman5560c9d2003-08-18 14:43:39 +000052 // Add padding if necessary to make the data element aligned properly...
Chris Lattnere7fb3602001-08-27 16:00:15 +000053 if (StructSize % TyAlign != 0)
54 StructSize = (StructSize/TyAlign + 1) * TyAlign; // Add padding...
55
56 // Keep track of maximum alignment constraint
Chris Lattner697954c2002-01-20 22:54:45 +000057 StructAlignment = std::max(TyAlign, StructAlignment);
Chris Lattnere7fb3602001-08-27 16:00:15 +000058
59 MemberOffsets.push_back(StructSize);
Vikram S. Advef66723f2002-05-19 15:28:02 +000060 StructSize += TySize; // Consume space for this data item
Chris Lattnere7fb3602001-08-27 16:00:15 +000061 }
62
Chris Lattner4e840d42003-05-21 18:08:44 +000063 // Empty structures have alignment of 1 byte.
64 if (StructAlignment == 0) StructAlignment = 1;
65
Chris Lattnere7fb3602001-08-27 16:00:15 +000066 // Add padding to the end of the struct so that it could be put in an array
67 // and all array elements would be aligned correctly.
68 if (StructSize % StructAlignment != 0)
69 StructSize = (StructSize/StructAlignment + 1) * StructAlignment;
Chris Lattnere7fb3602001-08-27 16:00:15 +000070}
71
Chris Lattnere7fb3602001-08-27 16:00:15 +000072//===----------------------------------------------------------------------===//
73// TargetData Class Implementation
74//===----------------------------------------------------------------------===//
75
Vikram S. Advef66723f2002-05-19 15:28:02 +000076TargetData::TargetData(const std::string &TargetName,
Chris Lattner10daaa12003-04-26 20:11:09 +000077 bool isLittleEndian, unsigned char PtrSize,
Chris Lattner85131c82002-10-14 22:41:13 +000078 unsigned char PtrAl, unsigned char DoubleAl,
79 unsigned char FloatAl, unsigned char LongAl,
80 unsigned char IntAl, unsigned char ShortAl,
Chris Lattner0e7ac162004-02-26 08:02:17 +000081 unsigned char ByteAl) {
Chris Lattnere7fb3602001-08-27 16:00:15 +000082
Chris Lattner46326d92003-04-25 02:50:45 +000083 // If this assert triggers, a pass "required" TargetData information, but the
84 // top level tool did not provide once for it. We do not want to default
85 // construct, or else we might end up using a bad endianness or pointer size!
86 //
87 assert(!TargetName.empty() &&
88 "ERROR: Tool did not specify a target data to use!");
89
Chris Lattner85131c82002-10-14 22:41:13 +000090 LittleEndian = isLittleEndian;
Chris Lattnere7fb3602001-08-27 16:00:15 +000091 PointerSize = PtrSize;
92 PointerAlignment = PtrAl;
93 DoubleAlignment = DoubleAl;
Chris Lattnerdd7253c2003-04-25 06:06:43 +000094 assert(DoubleAlignment == PtrAl &&
95 "Double alignment and pointer alignment agree for now!");
Chris Lattnere7fb3602001-08-27 16:00:15 +000096 FloatAlignment = FloatAl;
97 LongAlignment = LongAl;
98 IntAlignment = IntAl;
99 ShortAlignment = ShortAl;
100 ByteAlignment = ByteAl;
101}
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;
113}
114
Chris Lattner0e7ac162004-02-26 08:02:17 +0000115static std::map<std::pair<const TargetData*,const StructType*>,
116 StructLayout> *Layouts = 0;
117
118
Chris Lattnere7fb3602001-08-27 16:00:15 +0000119TargetData::~TargetData() {
Chris Lattner0e7ac162004-02-26 08:02:17 +0000120 if (Layouts) {
121 // Remove any layouts for this TD.
122 std::map<std::pair<const TargetData*,
123 const StructType*>, StructLayout>::iterator
124 I = Layouts->lower_bound(std::make_pair(this, (const StructType*)0));
125 while (I != Layouts->end() && I->first.first == this)
126 Layouts->erase(I++);
127 if (Layouts->empty()) {
128 delete Layouts;
129 Layouts = 0;
130 }
131 }
132}
133
134const StructLayout *TargetData::getStructLayout(const StructType *Ty) const {
135 if (Layouts == 0)
136 Layouts = new std::map<std::pair<const TargetData*,const StructType*>,
137 StructLayout>();
138 std::map<std::pair<const TargetData*,const StructType*>,
139 StructLayout>::iterator
140 I = Layouts->lower_bound(std::make_pair(this, Ty));
141 if (I != Layouts->end() && I->first.first == this && I->first.second == Ty)
142 return &I->second;
143 else {
144 return &Layouts->insert(I, std::make_pair(std::make_pair(this, Ty),
145 StructLayout(Ty, *this)))->second;
146 }
Chris Lattnere7fb3602001-08-27 16:00:15 +0000147}
148
149static inline void getTypeInfo(const Type *Ty, const TargetData *TD,
Vikram S. Advef66723f2002-05-19 15:28:02 +0000150 uint64_t &Size, unsigned char &Alignment) {
Chris Lattnerf59ce922001-12-13 00:46:11 +0000151 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!");
Chris Lattnere7fb3602001-08-27 16:00:15 +0000152 switch (Ty->getPrimitiveID()) {
153 case Type::VoidTyID:
154 case Type::BoolTyID:
155 case Type::UByteTyID:
156 case Type::SByteTyID: Size = 1; Alignment = TD->getByteAlignment(); return;
157 case Type::UShortTyID:
158 case Type::ShortTyID: Size = 2; Alignment = TD->getShortAlignment(); return;
159 case Type::UIntTyID:
160 case Type::IntTyID: Size = 4; Alignment = TD->getIntAlignment(); return;
161 case Type::ULongTyID:
162 case Type::LongTyID: Size = 8; Alignment = TD->getLongAlignment(); return;
163 case Type::FloatTyID: Size = 4; Alignment = TD->getFloatAlignment(); return;
164 case Type::DoubleTyID: Size = 8; Alignment = TD->getDoubleAlignment(); return;
165 case Type::LabelTyID:
166 case Type::PointerTyID:
167 Size = TD->getPointerSize(); Alignment = TD->getPointerAlignment();
168 return;
169 case Type::ArrayTyID: {
170 const ArrayType *ATy = (const ArrayType *)Ty;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000171 getTypeInfo(ATy->getElementType(), TD, Size, Alignment);
172 Size *= 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((const StructType*)Ty);
178 Size = Layout->StructSize; Alignment = Layout->StructAlignment;
179 return;
180 }
181
182 case Type::TypeTyID:
183 default:
184 assert(0 && "Bad type for getTypeInfo!!!");
185 return;
186 }
187}
188
Vikram S. Advef66723f2002-05-19 15:28:02 +0000189uint64_t TargetData::getTypeSize(const Type *Ty) const {
190 uint64_t Size;
191 unsigned char Align;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000192 getTypeInfo(Ty, this, Size, Align);
193 return Size;
194}
195
196unsigned char TargetData::getTypeAlignment(const Type *Ty) const {
Vikram S. Advef66723f2002-05-19 15:28:02 +0000197 uint64_t Size;
198 unsigned char Align;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000199 getTypeInfo(Ty, this, Size, Align);
200 return Align;
201}
202
Chris Lattnerf0453282003-12-22 05:01:15 +0000203/// getIntPtrType - Return an unsigned integer type that is the same size or
204/// greater to the host pointer size.
205const Type *TargetData::getIntPtrType() const {
206 switch (getPointerSize()) {
207 default: assert(0 && "Unknown pointer size!");
208 case 2: return Type::UShortTy;
209 case 4: return Type::UIntTy;
210 case 8: return Type::ULongTy;
211 }
212}
213
214
Vikram S. Advef66723f2002-05-19 15:28:02 +0000215uint64_t TargetData::getIndexedOffset(const Type *ptrTy,
Chris Lattner697954c2002-01-20 22:54:45 +0000216 const std::vector<Value*> &Idx) const {
Vikram S. Adveed0030e2002-08-04 20:52:39 +0000217 const Type *Ty = ptrTy;
218 assert(isa<PointerType>(Ty) && "Illegal argument for getIndexedOffset()");
Vikram S. Advef66723f2002-05-19 15:28:02 +0000219 uint64_t Result = 0;
Chris Lattnere7fb3602001-08-27 16:00:15 +0000220
Chris Lattner3cac88a2002-09-11 01:21:33 +0000221 for (unsigned CurIDX = 0; CurIDX != Idx.size(); ++CurIDX) {
222 if (Idx[CurIDX]->getType() == Type::LongTy) {
Vikram S. Adveca710e92002-08-13 18:17:56 +0000223 // Update Ty to refer to current element
224 Ty = cast<SequentialType>(Ty)->getElementType();
225
Vikram S. Advef66723f2002-05-19 15:28:02 +0000226 // Get the array index and the size of each array element.
Chris Lattner3cac88a2002-09-11 01:21:33 +0000227 int64_t arrayIdx = cast<ConstantSInt>(Idx[CurIDX])->getValue();
Chris Lattner94a51182003-06-04 02:35:35 +0000228 Result += arrayIdx * (int64_t)getTypeSize(Ty);
Chris Lattner4954f042003-06-02 05:21:06 +0000229 } else {
230 const StructType *STy = cast<StructType>(Ty);
Chris Lattnere7fb3602001-08-27 16:00:15 +0000231 assert(Idx[CurIDX]->getType() == Type::UByteTy && "Illegal struct idx");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000232 unsigned FieldNo = cast<ConstantUInt>(Idx[CurIDX])->getValue();
Chris Lattnere7fb3602001-08-27 16:00:15 +0000233
234 // Get structure layout information...
235 const StructLayout *Layout = getStructLayout(STy);
236
237 // Add in the offset, as calculated by the structure layout info...
Vikram S. Advef66723f2002-05-19 15:28:02 +0000238 assert(FieldNo < Layout->MemberOffsets.size() &&"FieldNo out of range!");
Chris Lattnere7fb3602001-08-27 16:00:15 +0000239 Result += Layout->MemberOffsets[FieldNo];
Vikram S. Adveed0030e2002-08-04 20:52:39 +0000240
Chris Lattnere7fb3602001-08-27 16:00:15 +0000241 // Update Ty to refer to current element
Chris Lattnerd21cd802004-02-09 04:37:31 +0000242 Ty = STy->getElementType(FieldNo);
Chris Lattnere7fb3602001-08-27 16:00:15 +0000243 }
244 }
245
246 return Result;
247}
Brian Gaeked0fde302003-11-11 22:41:34 +0000248