X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FIR%2FDataLayout.cpp;h=cde393777a649c93f484b5d0307a3169fef64b02;hb=8d8b8915002f4154876106433be6764c78f14411;hp=ed7c12fc9df52447862536010118a95e9dee40a1;hpb=3c4c95e522ab2efca47df170d38bc2c19aa173ad;p=oota-llvm.git diff --git a/lib/IR/DataLayout.cpp b/lib/IR/DataLayout.cpp index ed7c12fc9df..cde393777a6 100644 --- a/lib/IR/DataLayout.cpp +++ b/lib/IR/DataLayout.cpp @@ -22,9 +22,9 @@ #include "llvm/ADT/Triple.h" #include "llvm/IR/Constants.h" #include "llvm/IR/DerivedTypes.h" +#include "llvm/IR/GetElementPtrTypeIterator.h" #include "llvm/IR/Module.h" #include "llvm/Support/ErrorHandling.h" -#include "llvm/Support/GetElementPtrTypeIterator.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/MathExtras.h" #include "llvm/Support/Mutex.h" @@ -55,7 +55,7 @@ StructLayout::StructLayout(StructType *ST, const DataLayout &DL) { // Add padding if necessary to align the data element properly. if ((StructSize & (TyAlign-1)) != 0) - StructSize = DataLayout::RoundUpAlignment(StructSize, TyAlign); + StructSize = RoundUpToAlignment(StructSize, TyAlign); // Keep track of maximum alignment constraint. StructAlignment = std::max(TyAlign, StructAlignment); @@ -70,7 +70,7 @@ StructLayout::StructLayout(StructType *ST, const DataLayout &DL) { // Add padding to the end of the struct so that it could be put in an array // and all array elements would be aligned correctly. if ((StructSize & (StructAlignment-1)) != 0) - StructSize = DataLayout::RoundUpAlignment(StructSize, StructAlignment); + StructSize = RoundUpToAlignment(StructSize, StructAlignment); } @@ -155,10 +155,9 @@ DataLayout::InvalidPointerElem = { 0U, 0U, 0U, ~0U }; const char *DataLayout::getManglingComponent(const Triple &T) { if (T.isOSBinFormatMachO()) return "-m:o"; - if (T.isOSBinFormatELF() || T.isArch64Bit()) - return "-m:e"; - assert(T.isOSBinFormatCOFF()); - return "-m:w"; + if (T.isOSWindows() && T.getArch() == Triple::x86 && T.isOSBinFormatCOFF()) + return "-m:w"; + return "-m:e"; } static const LayoutAlignElem DefaultAlignments[] = { @@ -179,14 +178,13 @@ static const LayoutAlignElem DefaultAlignments[] = { void DataLayout::reset(StringRef Desc) { clear(); - LayoutMap = 0; - LittleEndian = false; + LayoutMap = nullptr; + BigEndian = false; StackNaturalAlign = 0; ManglingMode = MM_None; // Default alignments - for (int I = 0, N = array_lengthof(DefaultAlignments); I < N; ++I) { - const LayoutAlignElem &E = DefaultAlignments[I]; + for (const LayoutAlignElem &E : DefaultAlignments) { setAlignment((AlignTypeEnum)E.AlignType, E.ABIAlign, E.PrefAlign, E.TypeBitWidth); } @@ -199,8 +197,10 @@ void DataLayout::reset(StringRef Desc) { static std::pair split(StringRef Str, char Separator) { assert(!Str.empty() && "parse error, string can't be empty here"); std::pair Split = Str.split(Separator); - assert((!Split.second.empty() || Split.first == Str) && - "a trailing separator is not allowed"); + if (Split.second.empty() && Split.first != Str) + report_fatal_error("Trailing separator in datalayout string"); + if (!Split.second.empty() && Split.first.empty()) + report_fatal_error("Expected token before separator in datalayout string"); return Split; } @@ -215,7 +215,8 @@ static unsigned getInt(StringRef R) { /// Convert bits into bytes. Assert if not a byte width multiple. static unsigned inBytes(unsigned Bits) { - assert(Bits % 8 == 0 && "number of bits must be a byte width multiple"); + if (Bits % 8) + report_fatal_error("number of bits must be a byte width multiple"); return Bits / 8; } @@ -241,22 +242,28 @@ void DataLayout::parseSpecifier(StringRef Desc) { // FIXME: remove this on LLVM 4.0. break; case 'E': - LittleEndian = false; + BigEndian = true; break; case 'e': - LittleEndian = true; + BigEndian = false; break; case 'p': { // Address space. unsigned AddrSpace = Tok.empty() ? 0 : getInt(Tok); - assert(AddrSpace < 1 << 24 && - "Invalid address space, must be a 24bit integer"); + if (!isUInt<24>(AddrSpace)) + report_fatal_error("Invalid address space, must be a 24bit integer"); // Size. + if (Rest.empty()) + report_fatal_error( + "Missing size specification for pointer in datalayout string"); Split = split(Rest, ':'); unsigned PointerMemSize = inBytes(getInt(Tok)); // ABI alignment. + if (Rest.empty()) + report_fatal_error( + "Missing alignment specification for pointer in datalayout string"); Split = split(Rest, ':'); unsigned PointerABIAlign = inBytes(getInt(Tok)); @@ -287,10 +294,14 @@ void DataLayout::parseSpecifier(StringRef Desc) { // Bit size. unsigned Size = Tok.empty() ? 0 : getInt(Tok); - assert((AlignType != AGGREGATE_ALIGN || Size == 0) && - "These specifications don't have a size"); + if (AlignType == AGGREGATE_ALIGN && Size != 0) + report_fatal_error( + "Sized aggregate specification in datalayout string"); // ABI alignment. + if (Rest.empty()) + report_fatal_error( + "Missing alignment specification in datalayout string"); Split = split(Rest, ':'); unsigned ABIAlign = inBytes(getInt(Tok)); @@ -308,7 +319,9 @@ void DataLayout::parseSpecifier(StringRef Desc) { case 'n': // Native integer types. for (;;) { unsigned Width = getInt(Tok); - assert(Width != 0 && "width must be non-zero"); + if (Width == 0) + report_fatal_error( + "Zero width native integer type in datalayout string"); LegalIntWidths.push_back(Width); if (Rest.empty()) break; @@ -320,11 +333,15 @@ void DataLayout::parseSpecifier(StringRef Desc) { break; } case 'm': - assert(Tok.empty()); - assert(Rest.size() == 1); + if (!Tok.empty()) + report_fatal_error("Unexpected trailing characters after mangling specifier in datalayout string"); + if (Rest.empty()) + report_fatal_error("Expected mangling specifier in datalayout string"); + if (Rest.size() > 1) + report_fatal_error("Unknown mangling specifier in datalayout string"); switch(Rest[0]) { default: - llvm_unreachable("Unknown mangling in datalayout string"); + report_fatal_error("Unknown mangling in datalayout string"); case 'e': ManglingMode = MM_ELF; break; @@ -340,13 +357,17 @@ void DataLayout::parseSpecifier(StringRef Desc) { } break; default: - llvm_unreachable("Unknown specifier in datalayout string"); + report_fatal_error("Unknown specifier in datalayout string"); break; } } } -DataLayout::DataLayout(const Module *M) : LayoutMap(0) { +DataLayout::DataLayout(const Module *M) : LayoutMap(nullptr) { + init(M); +} + +void DataLayout::init(const Module *M) { const DataLayout *Other = M->getDataLayout(); if (Other) *this = *Other; @@ -354,18 +375,28 @@ DataLayout::DataLayout(const Module *M) : LayoutMap(0) { reset(""); } +bool DataLayout::operator==(const DataLayout &Other) const { + bool Ret = BigEndian == Other.BigEndian && + StackNaturalAlign == Other.StackNaturalAlign && + ManglingMode == Other.ManglingMode && + LegalIntWidths == Other.LegalIntWidths && + Alignments == Other.Alignments && Pointers == Other.Pointers; + assert(Ret == (getStringRepresentation() == Other.getStringRepresentation())); + return Ret; +} + void DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align, unsigned pref_align, uint32_t bit_width) { assert(abi_align <= pref_align && "Preferred alignment worse than ABI!"); assert(pref_align < (1 << 16) && "Alignment doesn't fit in bitfield"); assert(bit_width < (1 << 24) && "Bit width doesn't fit in bitfield"); - for (unsigned i = 0, e = Alignments.size(); i != e; ++i) { - if (Alignments[i].AlignType == (unsigned)align_type && - Alignments[i].TypeBitWidth == bit_width) { + for (LayoutAlignElem &Elem : Alignments) { + if (Elem.AlignType == (unsigned)align_type && + Elem.TypeBitWidth == bit_width) { // Update the abi, preferred alignments. - Alignments[i].ABIAlign = abi_align; - Alignments[i].PrefAlign = pref_align; + Elem.ABIAlign = abi_align; + Elem.PrefAlign = pref_align; return; } } @@ -374,18 +405,26 @@ DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align, pref_align, bit_width)); } +DataLayout::PointersTy::iterator +DataLayout::findPointerLowerBound(uint32_t AddressSpace) { + return std::lower_bound(Pointers.begin(), Pointers.end(), AddressSpace, + [](const PointerAlignElem &A, uint32_t AddressSpace) { + return A.AddressSpace < AddressSpace; + }); +} + void DataLayout::setPointerAlignment(uint32_t AddrSpace, unsigned ABIAlign, unsigned PrefAlign, uint32_t TypeByteWidth) { assert(ABIAlign <= PrefAlign && "Preferred alignment worse than ABI!"); - DenseMap::iterator val = Pointers.find(AddrSpace); - if (val == Pointers.end()) { - Pointers[AddrSpace] = - PointerAlignElem::get(AddrSpace, ABIAlign, PrefAlign, TypeByteWidth); + PointersTy::iterator I = findPointerLowerBound(AddrSpace); + if (I == Pointers.end() || I->AddressSpace != AddrSpace) { + Pointers.insert(I, PointerAlignElem::get(AddrSpace, ABIAlign, PrefAlign, + TypeByteWidth)); } else { - val->second.ABIAlign = ABIAlign; - val->second.PrefAlign = PrefAlign; - val->second.TypeByteWidth = TypeByteWidth; + I->ABIAlign = ABIAlign; + I->PrefAlign = PrefAlign; + I->TypeByteWidth = TypeByteWidth; } } @@ -451,11 +490,10 @@ class StructLayoutMap { LayoutInfoTy LayoutInfo; public: - virtual ~StructLayoutMap() { + ~StructLayoutMap() { // Remove any layouts. - for (LayoutInfoTy::iterator I = LayoutInfo.begin(), E = LayoutInfo.end(); - I != E; ++I) { - StructLayout *Value = I->second; + for (const auto &I : LayoutInfo) { + StructLayout *Value = I.second; Value->~StructLayout(); free(Value); } @@ -464,9 +502,6 @@ public: StructLayout *&operator[](StructType *STy) { return LayoutInfo[STy]; } - - // for debugging... - virtual void dump() const {} }; } // end anonymous namespace @@ -476,7 +511,7 @@ void DataLayout::clear() { Alignments.clear(); Pointers.clear(); delete static_cast(LayoutMap); - LayoutMap = 0; + LayoutMap = nullptr; } DataLayout::~DataLayout() { @@ -510,7 +545,7 @@ std::string DataLayout::getStringRepresentation() const { std::string Result; raw_string_ostream OS(Result); - OS << (LittleEndian ? "e" : "E"); + OS << (BigEndian ? "E" : "e"); switch (ManglingMode) { case MM_None: @@ -529,20 +564,7 @@ std::string DataLayout::getStringRepresentation() const { break; } - SmallVector addrSpaces; - // Lets get all of the known address spaces and sort them - // into increasing order so that we can emit the string - // in a cleaner format. - for (DenseMap::const_iterator - pib = Pointers.begin(), pie = Pointers.end(); - pib != pie; ++pib) { - addrSpaces.push_back(pib->first); - } - std::sort(addrSpaces.begin(), addrSpaces.end()); - for (SmallVectorImpl::iterator asb = addrSpaces.begin(), - ase = addrSpaces.end(); asb != ase; ++asb) { - const PointerAlignElem &PI = Pointers.find(*asb)->second; - + for (const PointerAlignElem &PI : Pointers) { // Skip default. if (PI.AddressSpace == 0 && PI.ABIAlign == 8 && PI.PrefAlign == 8 && PI.TypeByteWidth == 8) @@ -557,12 +579,9 @@ std::string DataLayout::getStringRepresentation() const { OS << ':' << PI.PrefAlign*8; } - const LayoutAlignElem *DefaultStart = DefaultAlignments; - const LayoutAlignElem *DefaultEnd = - DefaultStart + array_lengthof(DefaultAlignments); - for (unsigned i = 0, e = Alignments.size(); i != e; ++i) { - const LayoutAlignElem &AI = Alignments[i]; - if (std::find(DefaultStart, DefaultEnd, AI) != DefaultEnd) + for (const LayoutAlignElem &AI : Alignments) { + if (std::find(std::begin(DefaultAlignments), std::end(DefaultAlignments), + AI) != std::end(DefaultAlignments)) continue; OS << '-' << (char)AI.AlignType; if (AI.TypeBitWidth) @@ -586,27 +605,30 @@ std::string DataLayout::getStringRepresentation() const { } unsigned DataLayout::getPointerABIAlignment(unsigned AS) const { - DenseMap::const_iterator val = Pointers.find(AS); - if (val == Pointers.end()) { - val = Pointers.find(0); + PointersTy::const_iterator I = findPointerLowerBound(AS); + if (I == Pointers.end() || I->AddressSpace != AS) { + I = findPointerLowerBound(0); + assert(I->AddressSpace == 0); } - return val->second.ABIAlign; + return I->ABIAlign; } unsigned DataLayout::getPointerPrefAlignment(unsigned AS) const { - DenseMap::const_iterator val = Pointers.find(AS); - if (val == Pointers.end()) { - val = Pointers.find(0); + PointersTy::const_iterator I = findPointerLowerBound(AS); + if (I == Pointers.end() || I->AddressSpace != AS) { + I = findPointerLowerBound(0); + assert(I->AddressSpace == 0); } - return val->second.PrefAlign; + return I->PrefAlign; } unsigned DataLayout::getPointerSize(unsigned AS) const { - DenseMap::const_iterator val = Pointers.find(AS); - if (val == Pointers.end()) { - val = Pointers.find(0); + PointersTy::const_iterator I = findPointerLowerBound(AS); + if (I == Pointers.end() || I->AddressSpace != AS) { + I = findPointerLowerBound(0); + assert(I->AddressSpace == 0); } - return val->second.TypeByteWidth; + return I->TypeByteWidth; } unsigned DataLayout::getPointerTypeSizeInBits(Type *Ty) const { @@ -638,7 +660,7 @@ unsigned DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const { ? getPointerABIAlignment(0) : getPointerPrefAlignment(0)); case Type::PointerTyID: { - unsigned AS = dyn_cast(Ty)->getAddressSpace(); + unsigned AS = cast(Ty)->getAddressSpace(); return (abi_or_pref ? getPointerABIAlignment(AS) : getPointerPrefAlignment(AS)); @@ -688,7 +710,7 @@ unsigned DataLayout::getABITypeAlignment(Type *Ty) const { /// getABIIntegerTypeAlignment - Return the minimum ABI-required alignment for /// an integer type of the specified bitwidth. unsigned DataLayout::getABIIntegerTypeAlignment(unsigned BitWidth) const { - return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, 0); + return getAlignmentInfo(INTEGER_ALIGN, BitWidth, true, nullptr); } unsigned DataLayout::getPrefTypeAlignment(Type *Ty) const { @@ -709,7 +731,7 @@ IntegerType *DataLayout::getIntPtrType(LLVMContext &C, Type *DataLayout::getIntPtrType(Type *Ty) const { assert(Ty->isPtrOrPtrVectorTy() && "Expected a pointer or pointer vector type."); - unsigned NumBits = getTypeSizeInBits(Ty->getScalarType()); + unsigned NumBits = getPointerTypeSizeInBits(Ty); IntegerType *IntTy = IntegerType::get(Ty->getContext(), NumBits); if (VectorType *VecTy = dyn_cast(Ty)) return VectorType::get(IntTy, VecTy->getNumElements()); @@ -717,17 +739,15 @@ Type *DataLayout::getIntPtrType(Type *Ty) const { } Type *DataLayout::getSmallestLegalIntType(LLVMContext &C, unsigned Width) const { - for (unsigned i = 0, e = (unsigned)LegalIntWidths.size(); i != e; ++i) - if (Width <= LegalIntWidths[i]) - return Type::getIntNTy(C, LegalIntWidths[i]); - return 0; + for (unsigned LegalIntWidth : LegalIntWidths) + if (Width <= LegalIntWidth) + return Type::getIntNTy(C, LegalIntWidth); + return nullptr; } unsigned DataLayout::getLargestLegalIntTypeSize() const { - unsigned MaxWidth = 0; - for (unsigned i = 0, e = (unsigned)LegalIntWidths.size(); i != e; ++i) - MaxWidth = std::max(MaxWidth, LegalIntWidths[i]); - return MaxWidth; + auto Max = std::max_element(LegalIntWidths.begin(), LegalIntWidths.end()); + return Max != LegalIntWidths.end() ? *Max : 0; } uint64_t DataLayout::getIndexedOffset(Type *ptrTy, @@ -799,17 +819,17 @@ unsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable *GV) const { } DataLayoutPass::DataLayoutPass() : ImmutablePass(ID), DL("") { - report_fatal_error("Bad DataLayoutPass ctor used. Tool did not specify a " - "DataLayout to use?"); + initializeDataLayoutPassPass(*PassRegistry::getPassRegistry()); } DataLayoutPass::~DataLayoutPass() {} -DataLayoutPass::DataLayoutPass(const DataLayout &DL) - : ImmutablePass(ID), DL(DL) { - initializeDataLayoutPassPass(*PassRegistry::getPassRegistry()); +bool DataLayoutPass::doInitialization(Module &M) { + DL.init(&M); + return false; } -DataLayoutPass::DataLayoutPass(const Module *M) : ImmutablePass(ID), DL(M) { - initializeDataLayoutPassPass(*PassRegistry::getPassRegistry()); +bool DataLayoutPass::doFinalization(Module &M) { + DL.reset(""); + return false; }