DataLayout.cpp revision 249259
1139749Simp//===-- DataLayout.cpp - Data size & alignment routines --------------------==// 2113584Ssimokawa// 3103285Sikob// The LLVM Compiler Infrastructure 4103285Sikob// 5103285Sikob// This file is distributed under the University of Illinois Open Source 6103285Sikob// License. See LICENSE.TXT for details. 7103285Sikob// 8103285Sikob//===----------------------------------------------------------------------===// 9103285Sikob// 10103285Sikob// This file defines layout properties related to datatype size/offset/alignment 11103285Sikob// information. 12103285Sikob// 13103285Sikob// This structure should be created once, filled in if the defaults are not 14103285Sikob// correct and then passed around by const&. None of the members functions 15103285Sikob// require modification to the object. 16103285Sikob// 17103285Sikob//===----------------------------------------------------------------------===// 18103285Sikob 19103285Sikob#include "llvm/IR/DataLayout.h" 20103285Sikob#include "llvm/ADT/DenseMap.h" 21103285Sikob#include "llvm/IR/Constants.h" 22103285Sikob#include "llvm/IR/DerivedTypes.h" 23103285Sikob#include "llvm/IR/Module.h" 24103285Sikob#include "llvm/Support/ErrorHandling.h" 25103285Sikob#include "llvm/Support/GetElementPtrTypeIterator.h" 26103285Sikob#include "llvm/Support/ManagedStatic.h" 27103285Sikob#include "llvm/Support/MathExtras.h" 28103285Sikob#include "llvm/Support/Mutex.h" 29103285Sikob#include "llvm/Support/raw_ostream.h" 30103285Sikob#include <algorithm> 31103285Sikob#include <cstdlib> 32103285Sikobusing namespace llvm; 33103285Sikob 34103285Sikob// Handle the Pass registration stuff necessary to use DataLayout's. 35103285Sikob 36127468Ssimokawa// Register the default SparcV9 implementation... 37119418SobrienINITIALIZE_PASS(DataLayout, "datalayout", "Data Layout", false, true) 38119418Sobrienchar DataLayout::ID = 0; 39127468Ssimokawa 40119418Sobrien//===----------------------------------------------------------------------===// 41103285Sikob// Support for StructLayout 42103285Sikob//===----------------------------------------------------------------------===// 43103285Sikob 44103285SikobStructLayout::StructLayout(StructType *ST, const DataLayout &TD) { 45103285Sikob assert(!ST->isOpaque() && "Cannot get layout of opaque structs"); 46103285Sikob StructAlignment = 0; 47103285Sikob StructSize = 0; 48103285Sikob NumElements = ST->getNumElements(); 49127468Ssimokawa 50120660Ssimokawa // Loop over each of the elements, placing them in memory. 51120660Ssimokawa for (unsigned i = 0, e = NumElements; i != e; ++i) { 52120660Ssimokawa Type *Ty = ST->getElementType(i); 53120660Ssimokawa unsigned TyAlign = ST->isPacked() ? 1 : TD.getABITypeAlignment(Ty); 54103285Sikob 55103285Sikob // Add padding if necessary to align the data element properly. 56113584Ssimokawa if ((StructSize & (TyAlign-1)) != 0) 57103285Sikob StructSize = DataLayout::RoundUpAlignment(StructSize, TyAlign); 58103285Sikob 59103285Sikob // Keep track of maximum alignment constraint. 60103285Sikob StructAlignment = std::max(TyAlign, StructAlignment); 61122228Ssimokawa 62103285Sikob MemberOffsets[i] = StructSize; 63127468Ssimokawa StructSize += TD.getTypeAllocSize(Ty); // Consume space for this data item 64127468Ssimokawa } 65127468Ssimokawa 66127468Ssimokawa // Empty structures have alignment of 1 byte. 67127468Ssimokawa if (StructAlignment == 0) StructAlignment = 1; 68103285Sikob 69103285Sikob // Add padding to the end of the struct so that it could be put in an array 70103285Sikob // and all array elements would be aligned correctly. 71127468Ssimokawa if ((StructSize & (StructAlignment-1)) != 0) 72103285Sikob StructSize = DataLayout::RoundUpAlignment(StructSize, StructAlignment); 73106810Ssimokawa} 74106810Ssimokawa 75103285Sikob 76227309Sed/// getElementContainingOffset - Given a valid offset into the structure, 77108281Ssimokawa/// return the structure index that contains it. 78106810Ssimokawaunsigned StructLayout::getElementContainingOffset(uint64_t Offset) const { 79106810Ssimokawa const uint64_t *SI = 80110582Ssimokawa std::upper_bound(&MemberOffsets[0], &MemberOffsets[NumElements], Offset); 81106810Ssimokawa assert(SI != &MemberOffsets[0] && "Offset not in structure type!"); 82103285Sikob --SI; 83103285Sikob assert(*SI <= Offset && "upper_bound didn't work"); 84103285Sikob assert((SI == &MemberOffsets[0] || *(SI-1) <= Offset) && 85103285Sikob (SI+1 == &MemberOffsets[NumElements] || *(SI+1) > Offset) && 86103285Sikob "Upper bound didn't work!"); 87227293Sed 88124145Ssimokawa // Multiple fields can have the same offset if any of them are zero sized. 89120660Ssimokawa // For example, in { i32, [0 x i32], i32 }, searching for offset 4 will stop 90120660Ssimokawa // at the i32 element, because it is the last element at that offset. This is 91122228Ssimokawa // the right one to return, because anything after it will have a higher 92122228Ssimokawa // offset, implying that this element is non-empty. 93170374Ssimokawa return SI-&MemberOffsets[0]; 94122228Ssimokawa} 95122228Ssimokawa 96122228Ssimokawa//===----------------------------------------------------------------------===// 97106816Ssimokawa// LayoutAlignElem, LayoutAlign support 98106816Ssimokawa//===----------------------------------------------------------------------===// 99106810Ssimokawa 100106816SsimokawaLayoutAlignElem 101106816SsimokawaLayoutAlignElem::get(AlignTypeEnum align_type, unsigned abi_align, 102113584Ssimokawa unsigned pref_align, uint32_t bit_width) { 103113584Ssimokawa assert(abi_align <= pref_align && "Preferred alignment worse than ABI!"); 104106816Ssimokawa LayoutAlignElem retval; 105103285Sikob retval.AlignType = align_type; 106103285Sikob retval.ABIAlign = abi_align; 107103285Sikob retval.PrefAlign = pref_align; 108124145Ssimokawa retval.TypeBitWidth = bit_width; 109106804Ssimokawa return retval; 110103285Sikob} 111106804Ssimokawa 112106810Ssimokawabool 113120660SsimokawaLayoutAlignElem::operator==(const LayoutAlignElem &rhs) const { 114106816Ssimokawa return (AlignType == rhs.AlignType 115120660Ssimokawa && ABIAlign == rhs.ABIAlign 116106816Ssimokawa && PrefAlign == rhs.PrefAlign 117120660Ssimokawa && TypeBitWidth == rhs.TypeBitWidth); 118167632Ssimokawa} 119106816Ssimokawa 120120660Ssimokawaconst LayoutAlignElem 121120660SsimokawaDataLayout::InvalidAlignmentElem = LayoutAlignElem::get(INVALID_ALIGN, 0, 0, 0); 122103285Sikob 123106816Ssimokawa//===----------------------------------------------------------------------===// 124106816Ssimokawa// PointerAlignElem, PointerAlign support 125106816Ssimokawa//===----------------------------------------------------------------------===// 126106816Ssimokawa 127106816SsimokawaPointerAlignElem 128106816SsimokawaPointerAlignElem::get(uint32_t addr_space, unsigned abi_align, 129106816Ssimokawa unsigned pref_align, uint32_t bit_width) { 130129585Sdfr assert(abi_align <= pref_align && "Preferred alignment worse than ABI!"); 131129585Sdfr PointerAlignElem retval; 132129585Sdfr retval.AddressSpace = addr_space; 133120660Ssimokawa retval.ABIAlign = abi_align; 134106816Ssimokawa retval.PrefAlign = pref_align; 135106816Ssimokawa retval.TypeBitWidth = bit_width; 136106816Ssimokawa return retval; 137106816Ssimokawa} 138106816Ssimokawa 139120660Ssimokawabool 140120660SsimokawaPointerAlignElem::operator==(const PointerAlignElem &rhs) const { 141106816Ssimokawa return (ABIAlign == rhs.ABIAlign 142120660Ssimokawa && AddressSpace == rhs.AddressSpace 143106816Ssimokawa && PrefAlign == rhs.PrefAlign 144120660Ssimokawa && TypeBitWidth == rhs.TypeBitWidth); 145103285Sikob} 146113584Ssimokawa 147113584Ssimokawaconst PointerAlignElem 148103285SikobDataLayout::InvalidPointerElem = PointerAlignElem::get(~0U, 0U, 0U, 0U); 149120660Ssimokawa 150129585Sdfr//===----------------------------------------------------------------------===// 151120660Ssimokawa// DataLayout Class Implementation 152103285Sikob//===----------------------------------------------------------------------===// 153106816Ssimokawa 154106816Ssimokawavoid DataLayout::init(StringRef Desc) { 155106804Ssimokawa initializeDataLayoutPass(*PassRegistry::getPassRegistry()); 156106810Ssimokawa 157103285Sikob LayoutMap = 0; 158106804Ssimokawa LittleEndian = false; 159103285Sikob StackNaturalAlign = 0; 160103285Sikob 161103285Sikob // Default alignments 162103285Sikob setAlignment(INTEGER_ALIGN, 1, 1, 1); // i1 163103285Sikob setAlignment(INTEGER_ALIGN, 1, 1, 8); // i8 164106810Ssimokawa setAlignment(INTEGER_ALIGN, 2, 2, 16); // i16 165106810Ssimokawa setAlignment(INTEGER_ALIGN, 4, 4, 32); // i32 166106810Ssimokawa setAlignment(INTEGER_ALIGN, 4, 8, 64); // i64 167129585Sdfr setAlignment(FLOAT_ALIGN, 2, 2, 16); // half 168129585Sdfr setAlignment(FLOAT_ALIGN, 4, 4, 32); // float 169129585Sdfr setAlignment(FLOAT_ALIGN, 8, 8, 64); // double 170120660Ssimokawa setAlignment(FLOAT_ALIGN, 16, 16, 128); // ppcf128, quad, ... 171106810Ssimokawa setAlignment(VECTOR_ALIGN, 8, 8, 64); // v2i32, v1i64, ... 172106810Ssimokawa setAlignment(VECTOR_ALIGN, 16, 16, 128); // v16i8, v8i16, v4i32, ... 173106810Ssimokawa setAlignment(AGGREGATE_ALIGN, 0, 8, 0); // struct 174106810Ssimokawa setPointerAlignment(0, 8, 8, 8); 175106810Ssimokawa 176120660Ssimokawa parseSpecifier(Desc); 177106810Ssimokawa} 178106810Ssimokawa 179106810Ssimokawa/// Checked version of split, to ensure mandatory subparts. 180120660Ssimokawastatic std::pair<StringRef, StringRef> split(StringRef Str, char Separator) { 181110072Ssimokawa assert(!Str.empty() && "parse error, string can't be empty here"); 182113584Ssimokawa std::pair<StringRef, StringRef> Split = Str.split(Separator); 183113584Ssimokawa assert((!Split.second.empty() || Split.first == Str) && 184129585Sdfr "a trailing separator is not allowed"); 185106810Ssimokawa return Split; 186120660Ssimokawa} 187106810Ssimokawa 188106810Ssimokawa/// Get an unsinged integer, including error checks. 189106816Ssimokawastatic unsigned getInt(StringRef R) { 190129585Sdfr unsigned Result; 191106810Ssimokawa bool error = R.getAsInteger(10, Result); (void)error; 192106810Ssimokawa assert(!error && "not a number, or does not fit in an unsigned int"); 193106810Ssimokawa return Result; 194106810Ssimokawa} 195106810Ssimokawa 196106810Ssimokawa/// Convert bits into bytes. Assert if not a byte width multiple. 197106810Ssimokawastatic unsigned inBytes(unsigned Bits) { 198106810Ssimokawa assert(Bits % 8 == 0 && "number of bits must be a byte width multiple"); 199106810Ssimokawa return Bits / 8; 200103285Sikob} 201106810Ssimokawa 202106810Ssimokawavoid DataLayout::parseSpecifier(StringRef Desc) { 203129585Sdfr 204129585Sdfr while (!Desc.empty()) { 205129585Sdfr 206106804Ssimokawa // Split at '-'. 207120660Ssimokawa std::pair<StringRef, StringRef> Split = split(Desc, '-'); 208106804Ssimokawa Desc = Split.second; 209103285Sikob 210103285Sikob // Split at ':'. 211103285Sikob Split = split(Split.first, ':'); 212120660Ssimokawa 213120660Ssimokawa // Aliases used below. 214106804Ssimokawa StringRef &Tok = Split.first; // Current token. 215103285Sikob StringRef &Rest = Split.second; // The rest of the string. 216106804Ssimokawa 217120660Ssimokawa char Specifier = Tok.front(); 218103285Sikob Tok = Tok.substr(1); 219113584Ssimokawa 220113584Ssimokawa switch (Specifier) { 221113584Ssimokawa case 'E': 222120660Ssimokawa LittleEndian = false; 223103285Sikob break; 224120660Ssimokawa case 'e': 225120660Ssimokawa LittleEndian = true; 226120660Ssimokawa break; 227103285Sikob case 'p': { 228106816Ssimokawa // Address space. 229106810Ssimokawa unsigned AddrSpace = Tok.empty() ? 0 : getInt(Tok); 230106810Ssimokawa assert(AddrSpace < 1 << 24 && 231103285Sikob "Invalid address space, must be a 24bit integer"); 232106804Ssimokawa 233103285Sikob // Size. 234103285Sikob Split = split(Rest, ':'); 235103285Sikob unsigned PointerMemSize = inBytes(getInt(Tok)); 236103285Sikob 237110337Ssimokawa // ABI alignment. 238110337Ssimokawa Split = split(Rest, ':'); 239110337Ssimokawa unsigned PointerABIAlign = inBytes(getInt(Tok)); 240110337Ssimokawa 241129585Sdfr // Preferred alignment. 242129585Sdfr unsigned PointerPrefAlign = PointerABIAlign; 243129585Sdfr if (!Rest.empty()) { 244110337Ssimokawa Split = split(Rest, ':'); 245120660Ssimokawa PointerPrefAlign = inBytes(getInt(Tok)); 246110337Ssimokawa } 247110337Ssimokawa 248110337Ssimokawa setPointerAlignment(AddrSpace, PointerABIAlign, PointerPrefAlign, 249110337Ssimokawa PointerMemSize); 250110337Ssimokawa break; 251120660Ssimokawa } 252110337Ssimokawa case 'i': 253110337Ssimokawa case 'v': 254110337Ssimokawa case 'f': 255120660Ssimokawa case 'a': 256110406Ssimokawa case 's': { 257113584Ssimokawa AlignTypeEnum AlignType; 258113584Ssimokawa switch (Specifier) { 259113584Ssimokawa default: 260120660Ssimokawa case 'i': AlignType = INTEGER_ALIGN; break; 261110337Ssimokawa case 'v': AlignType = VECTOR_ALIGN; break; 262120660Ssimokawa case 'f': AlignType = FLOAT_ALIGN; break; 263120660Ssimokawa case 'a': AlignType = AGGREGATE_ALIGN; break; 264120660Ssimokawa case 's': AlignType = STACK_ALIGN; break; 265110337Ssimokawa } 266110337Ssimokawa 267110337Ssimokawa // Bit size. 268110337Ssimokawa unsigned Size = Tok.empty() ? 0 : getInt(Tok); 269110337Ssimokawa 270110337Ssimokawa // ABI alignment. 271110337Ssimokawa Split = split(Rest, ':'); 272110337Ssimokawa unsigned ABIAlign = inBytes(getInt(Tok)); 273110337Ssimokawa 274110337Ssimokawa // Preferred alignment. 275110337Ssimokawa unsigned PrefAlign = ABIAlign; 276103285Sikob if (!Rest.empty()) { 277130585Sphk Split = split(Rest, ':'); 278103285Sikob PrefAlign = inBytes(getInt(Tok)); 279122228Ssimokawa } 280170374Ssimokawa 281170374Ssimokawa setAlignment(AlignType, ABIAlign, PrefAlign, Size); 282110582Ssimokawa 283170374Ssimokawa break; 284170374Ssimokawa } 285170374Ssimokawa case 'n': // Native integer types. 286170374Ssimokawa for (;;) { 287170374Ssimokawa unsigned Width = getInt(Tok); 288122228Ssimokawa assert(Width != 0 && "width must be non-zero"); 289170374Ssimokawa LegalIntWidths.push_back(Width); 290170374Ssimokawa if (Rest.empty()) 291170374Ssimokawa break; 292170374Ssimokawa Split = split(Rest, ':'); 293170374Ssimokawa } 294122228Ssimokawa break; 295122228Ssimokawa case 'S': { // Stack natural alignment. 296122228Ssimokawa StackNaturalAlign = inBytes(getInt(Tok)); 297170374Ssimokawa break; 298170374Ssimokawa } 299170374Ssimokawa default: 300170374Ssimokawa llvm_unreachable("Unknown specifier in datalayout string"); 301170374Ssimokawa break; 302170374Ssimokawa } 303170374Ssimokawa } 304170374Ssimokawa} 305122228Ssimokawa 306170374Ssimokawa/// Default ctor. 307122228Ssimokawa/// 308122228Ssimokawa/// @note This has to exist, because this is a pass, but it should never be 309122228Ssimokawa/// used. 310127468SsimokawaDataLayout::DataLayout() : ImmutablePass(ID) { 311110582Ssimokawa report_fatal_error("Bad DataLayout ctor used. " 312110582Ssimokawa "Tool did not specify a DataLayout to use?"); 313103285Sikob} 314103285Sikob 315103285SikobDataLayout::DataLayout(const Module *M) 316130585Sphk : ImmutablePass(ID) { 317103285Sikob init(M->getDataLayout()); 318122228Ssimokawa} 319115786Ssimokawa 320122228Ssimokawavoid 321170374SsimokawaDataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align, 322170374Ssimokawa unsigned pref_align, uint32_t bit_width) { 323122228Ssimokawa assert(abi_align <= pref_align && "Preferred alignment worse than ABI!"); 324170374Ssimokawa assert(pref_align < (1 << 16) && "Alignment doesn't fit in bitfield"); 325122228Ssimokawa assert(bit_width < (1 << 24) && "Bit width doesn't fit in bitfield"); 326127468Ssimokawa for (unsigned i = 0, e = Alignments.size(); i != e; ++i) { 327122228Ssimokawa if (Alignments[i].AlignType == (unsigned)align_type && 328137503Ssimokawa Alignments[i].TypeBitWidth == bit_width) { 329122228Ssimokawa // Update the abi, preferred alignments. 330122228Ssimokawa Alignments[i].ABIAlign = abi_align; 331122228Ssimokawa Alignments[i].PrefAlign = pref_align; 332110582Ssimokawa return; 333103285Sikob } 334103285Sikob } 335120660Ssimokawa 336120660Ssimokawa Alignments.push_back(LayoutAlignElem::get(align_type, abi_align, 337120660Ssimokawa pref_align, bit_width)); 338103285Sikob} 339120660Ssimokawa 340103285Sikobvoid 341120660SsimokawaDataLayout::setPointerAlignment(uint32_t addr_space, unsigned abi_align, 342120660Ssimokawa unsigned pref_align, uint32_t bit_width) { 343120660Ssimokawa assert(abi_align <= pref_align && "Preferred alignment worse than ABI!"); 344120660Ssimokawa DenseMap<unsigned,PointerAlignElem>::iterator val = Pointers.find(addr_space); 345121381Ssimokawa if (val == Pointers.end()) { 346127468Ssimokawa Pointers[addr_space] = PointerAlignElem::get(addr_space, 347120660Ssimokawa abi_align, pref_align, bit_width); 348120660Ssimokawa } else { 349106810Ssimokawa val->second.ABIAlign = abi_align; 350103285Sikob val->second.PrefAlign = pref_align; 351120660Ssimokawa val->second.TypeBitWidth = bit_width; 352120660Ssimokawa } 353103285Sikob} 354120660Ssimokawa 355120660Ssimokawa/// getAlignmentInfo - Return the alignment (either ABI if ABIInfo = true or 356120660Ssimokawa/// preferred if ABIInfo = false) the layout wants for the specified datatype. 357103285Sikobunsigned DataLayout::getAlignmentInfo(AlignTypeEnum AlignType, 358122228Ssimokawa uint32_t BitWidth, bool ABIInfo, 359110337Ssimokawa Type *Ty) const { 360110337Ssimokawa // Check to see if we have an exact match and remember the best match we see. 361130585Sphk int BestMatchIdx = -1; 362170374Ssimokawa int LargestInt = -1; 363110337Ssimokawa for (unsigned i = 0, e = Alignments.size(); i != e; ++i) { 364120660Ssimokawa if (Alignments[i].AlignType == (unsigned)AlignType && 365120660Ssimokawa Alignments[i].TypeBitWidth == BitWidth) 366120660Ssimokawa return ABIInfo ? Alignments[i].ABIAlign : Alignments[i].PrefAlign; 367120660Ssimokawa 368122228Ssimokawa // The best match so far depends on what we're looking for. 369170374Ssimokawa if (AlignType == INTEGER_ALIGN && 370110337Ssimokawa Alignments[i].AlignType == INTEGER_ALIGN) { 371110577Ssimokawa // The "best match" for integers is the smallest size that is larger than 372110577Ssimokawa // the BitWidth requested. 373122228Ssimokawa if (Alignments[i].TypeBitWidth > BitWidth && (BestMatchIdx == -1 || 374120660Ssimokawa Alignments[i].TypeBitWidth < Alignments[BestMatchIdx].TypeBitWidth)) 375120660Ssimokawa BestMatchIdx = i; 376110337Ssimokawa // However, if there isn't one that's larger, then we must use the 377110337Ssimokawa // largest one we have (see below) 378120660Ssimokawa if (LargestInt == -1 || 379120660Ssimokawa Alignments[i].TypeBitWidth > Alignments[LargestInt].TypeBitWidth) 380120660Ssimokawa LargestInt = i; 381120660Ssimokawa } 382120660Ssimokawa } 383120660Ssimokawa 384120660Ssimokawa // Okay, we didn't find an exact solution. Fall back here depending on what 385120660Ssimokawa // is being looked for. 386120660Ssimokawa if (BestMatchIdx == -1) { 387120660Ssimokawa // If we didn't find an integer alignment, fall back on most conservative. 388120660Ssimokawa if (AlignType == INTEGER_ALIGN) { 389120660Ssimokawa BestMatchIdx = LargestInt; 390120660Ssimokawa } else { 391120660Ssimokawa assert(AlignType == VECTOR_ALIGN && "Unknown alignment type!"); 392120660Ssimokawa 393120660Ssimokawa // By default, use natural alignment for vector types. This is consistent 394120660Ssimokawa // with what clang and llvm-gcc do. 395120660Ssimokawa unsigned Align = getTypeAllocSize(cast<VectorType>(Ty)->getElementType()); 396120660Ssimokawa Align *= cast<VectorType>(Ty)->getNumElements(); 397120660Ssimokawa // If the alignment is not a power of 2, round up to the next power of 2. 398120660Ssimokawa // This happens for non-power-of-2 length vectors. 399120660Ssimokawa if (Align & (Align-1)) 400120660Ssimokawa Align = NextPowerOf2(Align); 401110337Ssimokawa return Align; 402120660Ssimokawa } 403120660Ssimokawa } 404120660Ssimokawa 405120660Ssimokawa // Since we got a "best match" index, just return it. 406120660Ssimokawa return ABIInfo ? Alignments[BestMatchIdx].ABIAlign 407120660Ssimokawa : Alignments[BestMatchIdx].PrefAlign; 408120660Ssimokawa} 409120660Ssimokawa 410120660Ssimokawanamespace { 411121381Ssimokawa 412127468Ssimokawaclass StructLayoutMap { 413120660Ssimokawa typedef DenseMap<StructType*, StructLayout*> LayoutInfoTy; 414120660Ssimokawa LayoutInfoTy LayoutInfo; 415120660Ssimokawa 416120660Ssimokawapublic: 417120660Ssimokawa virtual ~StructLayoutMap() { 418103285Sikob // Remove any layouts. 419110337Ssimokawa for (LayoutInfoTy::iterator I = LayoutInfo.begin(), E = LayoutInfo.end(); 420103285Sikob I != E; ++I) { 421130585Sphk StructLayout *Value = I->second; 422103285Sikob Value->~StructLayout(); 423122228Ssimokawa free(Value); 424110582Ssimokawa } 425122228Ssimokawa } 426122228Ssimokawa 427110582Ssimokawa StructLayout *&operator[](StructType *STy) { 428110582Ssimokawa return LayoutInfo[STy]; 429122228Ssimokawa } 430110582Ssimokawa 431110582Ssimokawa // for debugging... 432122228Ssimokawa virtual void dump() const {} 433110582Ssimokawa}; 434110582Ssimokawa 435110582Ssimokawa} // end anonymous namespace 436110582Ssimokawa 437110582SsimokawaDataLayout::~DataLayout() { 438103285Sikob delete static_cast<StructLayoutMap*>(LayoutMap); 439103285Sikob} 440130585Sphk 441103285Sikobbool DataLayout::doFinalization(Module &M) { 442103285Sikob delete static_cast<StructLayoutMap*>(LayoutMap); 443103285Sikob LayoutMap = 0; 444103285Sikob return false; 445127468Ssimokawa} 446130585Sphk 447111615Ssimokawaconst StructLayout *DataLayout::getStructLayout(StructType *Ty) const { 448201223Srnoland if (!LayoutMap) 449201223Srnoland LayoutMap = new StructLayoutMap(); 450111615Ssimokawa 451103285Sikob StructLayoutMap *STM = static_cast<StructLayoutMap*>(LayoutMap); 452103285Sikob StructLayout *&SL = (*STM)[Ty]; 453103285Sikob if (SL) return SL; 454 455 // Otherwise, create the struct layout. Because it is variable length, we 456 // malloc it, then use placement new. 457 int NumElts = Ty->getNumElements(); 458 StructLayout *L = 459 (StructLayout *)malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t)); 460 461 // Set SL before calling StructLayout's ctor. The ctor could cause other 462 // entries to be added to TheMap, invalidating our reference. 463 SL = L; 464 465 new (L) StructLayout(Ty, *this); 466 467 return L; 468} 469 470std::string DataLayout::getStringRepresentation() const { 471 std::string Result; 472 raw_string_ostream OS(Result); 473 474 OS << (LittleEndian ? "e" : "E"); 475 SmallVector<unsigned, 8> addrSpaces; 476 // Lets get all of the known address spaces and sort them 477 // into increasing order so that we can emit the string 478 // in a cleaner format. 479 for (DenseMap<unsigned, PointerAlignElem>::const_iterator 480 pib = Pointers.begin(), pie = Pointers.end(); 481 pib != pie; ++pib) { 482 addrSpaces.push_back(pib->first); 483 } 484 std::sort(addrSpaces.begin(), addrSpaces.end()); 485 for (SmallVector<unsigned, 8>::iterator asb = addrSpaces.begin(), 486 ase = addrSpaces.end(); asb != ase; ++asb) { 487 const PointerAlignElem &PI = Pointers.find(*asb)->second; 488 OS << "-p"; 489 if (PI.AddressSpace) { 490 OS << PI.AddressSpace; 491 } 492 OS << ":" << PI.TypeBitWidth*8 << ':' << PI.ABIAlign*8 493 << ':' << PI.PrefAlign*8; 494 } 495 OS << "-S" << StackNaturalAlign*8; 496 497 for (unsigned i = 0, e = Alignments.size(); i != e; ++i) { 498 const LayoutAlignElem &AI = Alignments[i]; 499 OS << '-' << (char)AI.AlignType << AI.TypeBitWidth << ':' 500 << AI.ABIAlign*8 << ':' << AI.PrefAlign*8; 501 } 502 503 if (!LegalIntWidths.empty()) { 504 OS << "-n" << (unsigned)LegalIntWidths[0]; 505 506 for (unsigned i = 1, e = LegalIntWidths.size(); i != e; ++i) 507 OS << ':' << (unsigned)LegalIntWidths[i]; 508 } 509 return OS.str(); 510} 511 512 513/*! 514 \param abi_or_pref Flag that determines which alignment is returned. true 515 returns the ABI alignment, false returns the preferred alignment. 516 \param Ty The underlying type for which alignment is determined. 517 518 Get the ABI (\a abi_or_pref == true) or preferred alignment (\a abi_or_pref 519 == false) for the requested type \a Ty. 520 */ 521unsigned DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const { 522 int AlignType = -1; 523 524 assert(Ty->isSized() && "Cannot getTypeInfo() on a type that is unsized!"); 525 switch (Ty->getTypeID()) { 526 // Early escape for the non-numeric types. 527 case Type::LabelTyID: 528 return (abi_or_pref 529 ? getPointerABIAlignment(0) 530 : getPointerPrefAlignment(0)); 531 case Type::PointerTyID: { 532 unsigned AS = dyn_cast<PointerType>(Ty)->getAddressSpace(); 533 return (abi_or_pref 534 ? getPointerABIAlignment(AS) 535 : getPointerPrefAlignment(AS)); 536 } 537 case Type::ArrayTyID: 538 return getAlignment(cast<ArrayType>(Ty)->getElementType(), abi_or_pref); 539 540 case Type::StructTyID: { 541 // Packed structure types always have an ABI alignment of one. 542 if (cast<StructType>(Ty)->isPacked() && abi_or_pref) 543 return 1; 544 545 // Get the layout annotation... which is lazily created on demand. 546 const StructLayout *Layout = getStructLayout(cast<StructType>(Ty)); 547 unsigned Align = getAlignmentInfo(AGGREGATE_ALIGN, 0, abi_or_pref, Ty); 548 return std::max(Align, Layout->getAlignment()); 549 } 550 case Type::IntegerTyID: 551 AlignType = INTEGER_ALIGN; 552 break; 553 case Type::HalfTyID: 554 case Type::FloatTyID: 555 case Type::DoubleTyID: 556 // PPC_FP128TyID and FP128TyID have different data contents, but the 557 // same size and alignment, so they look the same here. 558 case Type::PPC_FP128TyID: 559 case Type::FP128TyID: 560 case Type::X86_FP80TyID: 561 AlignType = FLOAT_ALIGN; 562 break; 563 case Type::X86_MMXTyID: 564 case Type::VectorTyID: 565 AlignType = VECTOR_ALIGN; 566 break; 567 default: 568 llvm_unreachable("Bad type for getAlignment!!!"); 569 } 570 571 return getAlignmentInfo((AlignTypeEnum)AlignType, getTypeSizeInBits(Ty), 572 abi_or_pref, Ty); 573} 574 575unsigned DataLayout::getABITypeAlignment(Type *Ty) const { 576 return getAlignment(Ty, true); 577} 578 579/// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for 580/// an integer type of the specified bitwidth. 581unsigned DataLayout::getABIIntegerTypeAlignment(unsigned BitWidth) const { 582 return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, 0); 583} 584 585 586unsigned DataLayout::getCallFrameTypeAlignment(Type *Ty) const { 587 for (unsigned i = 0, e = Alignments.size(); i != e; ++i) 588 if (Alignments[i].AlignType == STACK_ALIGN) 589 return Alignments[i].ABIAlign; 590 591 return getABITypeAlignment(Ty); 592} 593 594unsigned DataLayout::getPrefTypeAlignment(Type *Ty) const { 595 return getAlignment(Ty, false); 596} 597 598unsigned DataLayout::getPreferredTypeAlignmentShift(Type *Ty) const { 599 unsigned Align = getPrefTypeAlignment(Ty); 600 assert(!(Align & (Align-1)) && "Alignment is not a power of two!"); 601 return Log2_32(Align); 602} 603 604/// getIntPtrType - Return an integer type with size at least as big as that 605/// of a pointer in the given address space. 606IntegerType *DataLayout::getIntPtrType(LLVMContext &C, 607 unsigned AddressSpace) const { 608 return IntegerType::get(C, getPointerSizeInBits(AddressSpace)); 609} 610 611/// getIntPtrType - Return an integer (vector of integer) type with size at 612/// least as big as that of a pointer of the given pointer (vector of pointer) 613/// type. 614Type *DataLayout::getIntPtrType(Type *Ty) const { 615 assert(Ty->isPtrOrPtrVectorTy() && 616 "Expected a pointer or pointer vector type."); 617 unsigned NumBits = getTypeSizeInBits(Ty->getScalarType()); 618 IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits); 619 if (VectorType *VecTy = dyn_cast<VectorType>(Ty)) 620 return VectorType::get(IntTy, VecTy->getNumElements()); 621 return IntTy; 622} 623 624Type *DataLayout::getSmallestLegalIntType(LLVMContext &C, unsigned Width) const { 625 for (unsigned i = 0, e = (unsigned)LegalIntWidths.size(); i != e; ++i) 626 if (Width <= LegalIntWidths[i]) 627 return Type::getIntNTy(C, LegalIntWidths[i]); 628 return 0; 629} 630 631uint64_t DataLayout::getIndexedOffset(Type *ptrTy, 632 ArrayRef<Value *> Indices) const { 633 Type *Ty = ptrTy; 634 assert(Ty->isPointerTy() && "Illegal argument for getIndexedOffset()"); 635 uint64_t Result = 0; 636 637 generic_gep_type_iterator<Value* const*> 638 TI = gep_type_begin(ptrTy, Indices); 639 for (unsigned CurIDX = 0, EndIDX = Indices.size(); CurIDX != EndIDX; 640 ++CurIDX, ++TI) { 641 if (StructType *STy = dyn_cast<StructType>(*TI)) { 642 assert(Indices[CurIDX]->getType() == 643 Type::getInt32Ty(ptrTy->getContext()) && 644 "Illegal struct idx"); 645 unsigned FieldNo = cast<ConstantInt>(Indices[CurIDX])->getZExtValue(); 646 647 // Get structure layout information... 648 const StructLayout *Layout = getStructLayout(STy); 649 650 // Add in the offset, as calculated by the structure layout info... 651 Result += Layout->getElementOffset(FieldNo); 652 653 // Update Ty to refer to current element 654 Ty = STy->getElementType(FieldNo); 655 } else { 656 // Update Ty to refer to current element 657 Ty = cast<SequentialType>(Ty)->getElementType(); 658 659 // Get the array index and the size of each array element. 660 if (int64_t arrayIdx = cast<ConstantInt>(Indices[CurIDX])->getSExtValue()) 661 Result += (uint64_t)arrayIdx * getTypeAllocSize(Ty); 662 } 663 } 664 665 return Result; 666} 667 668/// getPreferredAlignment - Return the preferred alignment of the specified 669/// global. This includes an explicitly requested alignment (if the global 670/// has one). 671unsigned DataLayout::getPreferredAlignment(const GlobalVariable *GV) const { 672 Type *ElemType = GV->getType()->getElementType(); 673 unsigned Alignment = getPrefTypeAlignment(ElemType); 674 unsigned GVAlignment = GV->getAlignment(); 675 if (GVAlignment >= Alignment) { 676 Alignment = GVAlignment; 677 } else if (GVAlignment != 0) { 678 Alignment = std::max(GVAlignment, getABITypeAlignment(ElemType)); 679 } 680 681 if (GV->hasInitializer() && GVAlignment == 0) { 682 if (Alignment < 16) { 683 // If the global is not external, see if it is large. If so, give it a 684 // larger alignment. 685 if (getTypeSizeInBits(ElemType) > 128) 686 Alignment = 16; // 16-byte alignment. 687 } 688 } 689 return Alignment; 690} 691 692/// getPreferredAlignmentLog - Return the preferred alignment of the 693/// specified global, returned in log form. This includes an explicitly 694/// requested alignment (if the global has one). 695unsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable *GV) const { 696 return Log2_32(getPreferredAlignment(GV)); 697} 698