From 938e129fc2ea7820fd3e7e6a9fd1ed5f02e79820 Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Tue, 8 Sep 2015 22:33:23 +0000 Subject: [PATCH] Revert r247080, "LowerBitSets: Extend pass to support functions as bitset members." as it causes test failures on a number of bots. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@247088 91177308-0d34-0410-b5e6-96231b3b80d8 --- docs/BitSets.rst | 65 +-- docs/LangRef.rst | 2 +- include/llvm/Transforms/IPO/LowerBitSets.h | 4 +- lib/Transforms/IPO/LowerBitSets.cpp | 533 +++++-------------- test/Transforms/LowerBitSets/function-ext.ll | 22 - test/Transforms/LowerBitSets/function.ll | 35 -- test/Transforms/LowerBitSets/nonstring.ll | 34 -- test/Transforms/LowerBitSets/simple.ll | 8 +- 8 files changed, 144 insertions(+), 559 deletions(-) delete mode 100644 test/Transforms/LowerBitSets/function-ext.ll delete mode 100644 test/Transforms/LowerBitSets/function.ll delete mode 100644 test/Transforms/LowerBitSets/nonstring.ll diff --git a/docs/BitSets.rst b/docs/BitSets.rst index 18dbf6df563..c6ffdbdb8a1 100644 --- a/docs/BitSets.rst +++ b/docs/BitSets.rst @@ -10,41 +10,17 @@ for the type of the class or its derived classes. To use the mechanism, a client creates a global metadata node named ``llvm.bitsets``. Each element is a metadata node with three elements: - -1. a metadata object representing an identifier for the bitset -2. either a global variable or a function -3. a byte offset into the global (generally zero for functions) - -Each bitset must exclusively contain either global variables or functions. - -.. admonition:: Limitation - - The current implementation only supports functions as members of bitsets on - the x86-32 and x86-64 architectures. +the first is a metadata string containing an identifier for the bitset, +the second is a global variable and the third is a byte offset into the +global variable. This will cause a link-time optimization pass to generate bitsets from the -memory addresses referenced from the elements of the bitset metadata. The -pass will lay out referenced global variables consecutively, so their -definitions must be available at LTO time. - -A bit set containing functions is transformed into a jump table, which -is a block of code consisting of one branch instruction for each of the -functions in the bit set that branches to the target function, and redirect -any taken function addresses to the corresponding jump table entry. In the -object file's symbol table, the jump table entries take the identities of -the original functions, so that addresses taken outside the module will pass -any verification done inside the module. - -Jump tables may call external functions, so their definitions need not -be available at LTO time. Note that if an externally defined function is a -member of a bitset, there is no guarantee that its identity within the module -will be the same as its identity outside of the module, as the former will -be the jump table entry if a jump table is necessary. - -The `GlobalLayoutBuilder`_ class is responsible for laying out the globals -efficiently to minimize the sizes of the underlying bitsets. An intrinsic, -:ref:`llvm.bitset.test `, generates code to test whether a -given pointer is a member of a bitset. +memory addresses referenced from the elements of the bitset metadata. The pass +will lay out the referenced globals consecutively, so their definitions must +be available at LTO time. The `GlobalLayoutBuilder`_ class is responsible for +laying out the globals efficiently to minimize the sizes of the underlying +bitsets. An intrinsic, :ref:`llvm.bitset.test `, generates code +to test whether a given pointer is a member of a bitset. :Example: @@ -57,25 +33,13 @@ given pointer is a member of a bitset. @c = internal global i32 0 @d = internal global [2 x i32] [i32 0, i32 0] - define void @e() { - ret void - } - - define void @f() { - ret void - } - - declare void @g() - - !llvm.bitsets = !{!0, !1, !2, !3, !4, !5, !6} + !llvm.bitsets = !{!0, !1, !2, !3, !4} !0 = !{!"bitset1", i32* @a, i32 0} !1 = !{!"bitset1", i32* @b, i32 0} !2 = !{!"bitset2", i32* @b, i32 0} !3 = !{!"bitset2", i32* @c, i32 0} !4 = !{!"bitset2", i32* @d, i32 4} - !5 = !{!"bitset3", void ()* @e, i32 0} - !6 = !{!"bitset3", void ()* @g, i32 0} declare i1 @llvm.bitset.test(i8* %ptr, metadata %bitset) nounwind readnone @@ -91,12 +55,6 @@ given pointer is a member of a bitset. ret i1 %x } - define i1 @baz(void ()* %p) { - %pi8 = bitcast void ()* %p to i8* - %x = call i1 @llvm.bitset.test(i8* %pi8, metadata !"bitset3") - ret i1 %x - } - define void @main() { %a1 = call i1 @foo(i32* @a) ; returns 1 %b1 = call i1 @foo(i32* @b) ; returns 1 @@ -106,9 +64,6 @@ given pointer is a member of a bitset. %c2 = call i1 @bar(i32* @c) ; returns 1 %d02 = call i1 @bar(i32* getelementptr ([2 x i32]* @d, i32 0, i32 0)) ; returns 0 %d12 = call i1 @bar(i32* getelementptr ([2 x i32]* @d, i32 0, i32 1)) ; returns 1 - %e = call i1 @baz(void ()* @e) ; returns 1 - %f = call i1 @baz(void ()* @f) ; returns 0 - %g = call i1 @baz(void ()* @g) ; returns 1 ret void } diff --git a/docs/LangRef.rst b/docs/LangRef.rst index 5e63daca706..fd41eb99bed 100644 --- a/docs/LangRef.rst +++ b/docs/LangRef.rst @@ -11837,7 +11837,7 @@ Arguments: """""""""" The first argument is a pointer to be tested. The second argument is a -metadata object representing an identifier for a :doc:`bitset `. +metadata string containing the name of a :doc:`bitset `. Overview: """"""""" diff --git a/include/llvm/Transforms/IPO/LowerBitSets.h b/include/llvm/Transforms/IPO/LowerBitSets.h index e5fb7b98fcb..f678e86e686 100644 --- a/include/llvm/Transforms/IPO/LowerBitSets.h +++ b/include/llvm/Transforms/IPO/LowerBitSets.h @@ -26,7 +26,7 @@ namespace llvm { class DataLayout; -class GlobalObject; +class GlobalVariable; class Value; class raw_ostream; @@ -56,7 +56,7 @@ struct BitSetInfo { bool containsGlobalOffset(uint64_t Offset) const; bool containsValue(const DataLayout &DL, - const DenseMap &GlobalLayout, + const DenseMap &GlobalLayout, Value *V, uint64_t COffset = 0) const; void print(raw_ostream &OS) const; diff --git a/lib/Transforms/IPO/LowerBitSets.cpp b/lib/Transforms/IPO/LowerBitSets.cpp index 786c9cb124e..bf386a6c618 100644 --- a/lib/Transforms/IPO/LowerBitSets.cpp +++ b/lib/Transforms/IPO/LowerBitSets.cpp @@ -19,8 +19,6 @@ #include "llvm/ADT/Triple.h" #include "llvm/IR/Constant.h" #include "llvm/IR/Constants.h" -#include "llvm/IR/Function.h" -#include "llvm/IR/GlobalObject.h" #include "llvm/IR/GlobalVariable.h" #include "llvm/IR/IRBuilder.h" #include "llvm/IR/Instructions.h" @@ -63,9 +61,9 @@ bool BitSetInfo::containsGlobalOffset(uint64_t Offset) const { bool BitSetInfo::containsValue( const DataLayout &DL, - const DenseMap &GlobalLayout, Value *V, + const DenseMap &GlobalLayout, Value *V, uint64_t COffset) const { - if (auto GV = dyn_cast(V)) { + if (auto GV = dyn_cast(V)) { auto I = GlobalLayout.find(GV); if (I == GlobalLayout.end()) return false; @@ -213,48 +211,34 @@ struct LowerBitSets : public ModulePass { Module *M; bool LinkerSubsectionsViaSymbols; - Triple::ArchType Arch; - Triple::ObjectFormatType ObjectFormat; IntegerType *Int1Ty; IntegerType *Int8Ty; IntegerType *Int32Ty; Type *Int32PtrTy; IntegerType *Int64Ty; - IntegerType *IntPtrTy; + Type *IntPtrTy; // The llvm.bitsets named metadata. NamedMDNode *BitSetNM; - // Mapping from bitset identifiers to the call sites that test them. - DenseMap> BitSetTestCallSites; + // Mapping from bitset mdstrings to the call sites that test them. + DenseMap> BitSetTestCallSites; std::vector ByteArrayInfos; BitSetInfo - buildBitSet(Metadata *BitSet, - const DenseMap &GlobalLayout); + buildBitSet(MDString *BitSet, + const DenseMap &GlobalLayout); ByteArrayInfo *createByteArray(BitSetInfo &BSI); void allocateByteArrays(); Value *createBitSetTest(IRBuilder<> &B, BitSetInfo &BSI, ByteArrayInfo *&BAI, Value *BitOffset); - void lowerBitSetCalls(ArrayRef BitSets, - Constant *CombinedGlobalAddr, - const DenseMap &GlobalLayout); Value * lowerBitSetCall(CallInst *CI, BitSetInfo &BSI, ByteArrayInfo *&BAI, - Constant *CombinedGlobal, - const DenseMap &GlobalLayout); - void buildBitSetsFromGlobalVariables(ArrayRef BitSets, - ArrayRef Globals); - unsigned getJumpTableEntrySize(); - Type *getJumpTableEntryType(); - Constant *createJumpTableEntry(GlobalObject *Src, Function *Dest, - unsigned Distance); - void verifyBitSetMDNode(MDNode *Op); - void buildBitSetsFromFunctions(ArrayRef BitSets, - ArrayRef Functions); - void buildBitSetsFromDisjointSet(ArrayRef BitSets, - ArrayRef Globals); + GlobalVariable *CombinedGlobal, + const DenseMap &GlobalLayout); + void buildBitSetsFromGlobals(const std::vector &BitSets, + const std::vector &Globals); bool buildBitSets(); bool eraseBitSetMetadata(); @@ -278,8 +262,6 @@ bool LowerBitSets::doInitialization(Module &Mod) { Triple TargetTriple(M->getTargetTriple()); LinkerSubsectionsViaSymbols = TargetTriple.isMacOSX(); - Arch = TargetTriple.getArch(); - ObjectFormat = TargetTriple.getObjectFormat(); Int1Ty = Type::getInt1Ty(M->getContext()); Int8Ty = Type::getInt8Ty(M->getContext()); @@ -298,8 +280,8 @@ bool LowerBitSets::doInitialization(Module &Mod) { /// Build a bit set for BitSet using the object layouts in /// GlobalLayout. BitSetInfo LowerBitSets::buildBitSet( - Metadata *BitSet, - const DenseMap &GlobalLayout) { + MDString *BitSet, + const DenseMap &GlobalLayout) { BitSetBuilder BSB; // Compute the byte offset of each element of this bitset. @@ -307,11 +289,8 @@ BitSetInfo LowerBitSets::buildBitSet( for (MDNode *Op : BitSetNM->operands()) { if (Op->getOperand(0) != BitSet || !Op->getOperand(1)) continue; - Constant *OpConst = - cast(Op->getOperand(1))->getValue(); - if (auto GA = dyn_cast(OpConst)) - OpConst = GA->getAliasee(); - auto OpGlobal = dyn_cast(OpConst); + auto OpGlobal = dyn_cast( + cast(Op->getOperand(1))->getValue()); if (!OpGlobal) continue; uint64_t Offset = @@ -460,16 +439,17 @@ Value *LowerBitSets::createBitSetTest(IRBuilder<> &B, BitSetInfo &BSI, /// replace the call with. Value *LowerBitSets::lowerBitSetCall( CallInst *CI, BitSetInfo &BSI, ByteArrayInfo *&BAI, - Constant *CombinedGlobalIntAddr, - const DenseMap &GlobalLayout) { + GlobalVariable *CombinedGlobal, + const DenseMap &GlobalLayout) { Value *Ptr = CI->getArgOperand(0); const DataLayout &DL = M->getDataLayout(); if (BSI.containsValue(DL, GlobalLayout, Ptr)) - return ConstantInt::getTrue(M->getContext()); + return ConstantInt::getTrue(CombinedGlobal->getParent()->getContext()); + Constant *GlobalAsInt = ConstantExpr::getPtrToInt(CombinedGlobal, IntPtrTy); Constant *OffsetedGlobalAsInt = ConstantExpr::getAdd( - CombinedGlobalIntAddr, ConstantInt::get(IntPtrTy, BSI.ByteOffset)); + GlobalAsInt, ConstantInt::get(IntPtrTy, BSI.ByteOffset)); BasicBlock *InitialBB = CI->getParent(); @@ -528,19 +508,18 @@ Value *LowerBitSets::lowerBitSetCall( /// Given a disjoint set of bitsets and globals, layout the globals, build the /// bit sets and lower the llvm.bitset.test calls. -void LowerBitSets::buildBitSetsFromGlobalVariables( - ArrayRef BitSets, ArrayRef Globals) { +void LowerBitSets::buildBitSetsFromGlobals( + const std::vector &BitSets, + const std::vector &Globals) { // Build a new global with the combined contents of the referenced globals. - // This global is a struct whose even-indexed elements contain the original - // contents of the referenced globals and whose odd-indexed elements contain - // any padding required to align the next element to the next power of 2. std::vector GlobalInits; const DataLayout &DL = M->getDataLayout(); for (GlobalVariable *G : Globals) { GlobalInits.push_back(G->getInitializer()); uint64_t InitSize = DL.getTypeAllocSize(G->getInitializer()->getType()); - // Compute the amount of padding required. + // Compute the amount of padding required to align the next element to the + // next power of 2. uint64_t Padding = NextPowerOf2(InitSize - 1) - InitSize; // Cap at 128 was found experimentally to have a good data/instruction @@ -562,12 +541,30 @@ void LowerBitSets::buildBitSetsFromGlobalVariables( DL.getStructLayout(cast(NewInit->getType())); // Compute the offsets of the original globals within the new global. - DenseMap GlobalLayout; + DenseMap GlobalLayout; for (unsigned I = 0; I != Globals.size(); ++I) // Multiply by 2 to account for padding elements. GlobalLayout[Globals[I]] = CombinedGlobalLayout->getElementOffset(I * 2); - lowerBitSetCalls(BitSets, CombinedGlobal, GlobalLayout); + // For each bitset in this disjoint set... + for (MDString *BS : BitSets) { + // Build the bitset. + BitSetInfo BSI = buildBitSet(BS, GlobalLayout); + DEBUG({ + dbgs() << BS->getString() << ": "; + BSI.print(dbgs()); + }); + + ByteArrayInfo *BAI = 0; + + // Lower each call to llvm.bitset.test for this bitset. + for (CallInst *CI : BitSetTestCallSites[BS]) { + ++NumBitSetCallsLowered; + Value *Lowered = lowerBitSetCall(CI, BSI, BAI, CombinedGlobal, GlobalLayout); + CI->replaceAllUsesWith(Lowered); + CI->eraseFromParent(); + } + } // Build aliases pointing to offsets into the combined global for each // global from which we built the combined global, and replace references @@ -584,7 +581,6 @@ void LowerBitSets::buildBitSetsFromGlobalVariables( GlobalAlias *GAlias = GlobalAlias::create(Globals[I]->getType(), Globals[I]->getLinkage(), "", CombinedGlobalElemPtr, M); - GAlias->setVisibility(Globals[I]->getVisibility()); GAlias->takeName(Globals[I]); Globals[I]->replaceAllUsesWith(GAlias); } @@ -592,330 +588,6 @@ void LowerBitSets::buildBitSetsFromGlobalVariables( } } -void LowerBitSets::lowerBitSetCalls( - ArrayRef BitSets, Constant *CombinedGlobalAddr, - const DenseMap &GlobalLayout) { - Constant *CombinedGlobalIntAddr = - ConstantExpr::getPtrToInt(CombinedGlobalAddr, IntPtrTy); - - // For each bitset in this disjoint set... - for (Metadata *BS : BitSets) { - // Build the bitset. - BitSetInfo BSI = buildBitSet(BS, GlobalLayout); - DEBUG({ - if (auto BSS = dyn_cast(BS)) - dbgs() << BSS->getString() << ": "; - else - dbgs() << ": "; - BSI.print(dbgs()); - }); - - ByteArrayInfo *BAI = 0; - - // Lower each call to llvm.bitset.test for this bitset. - for (CallInst *CI : BitSetTestCallSites[BS]) { - ++NumBitSetCallsLowered; - Value *Lowered = - lowerBitSetCall(CI, BSI, BAI, CombinedGlobalIntAddr, GlobalLayout); - CI->replaceAllUsesWith(Lowered); - CI->eraseFromParent(); - } - } -} - -void LowerBitSets::verifyBitSetMDNode(MDNode *Op) { - if (Op->getNumOperands() != 3) - report_fatal_error( - "All operands of llvm.bitsets metadata must have 3 elements"); - if (!Op->getOperand(1)) - return; - - auto OpConstMD = dyn_cast(Op->getOperand(1)); - if (!OpConstMD) - report_fatal_error("Bit set element must be a constant"); - auto OpGlobal = dyn_cast(OpConstMD->getValue()); - if (!OpGlobal) - return; - - if (OpGlobal->isThreadLocal()) - report_fatal_error("Bit set element may not be thread-local"); - if (OpGlobal->hasSection()) - report_fatal_error("Bit set element may not have an explicit section"); - - if (isa(OpGlobal) && OpGlobal->isDeclarationForLinker()) - report_fatal_error("Bit set global var element must be a definition"); - - auto OffsetConstMD = dyn_cast(Op->getOperand(2)); - if (!OffsetConstMD) - report_fatal_error("Bit set element offset must be a constant"); - auto OffsetInt = dyn_cast(OffsetConstMD->getValue()); - if (!OffsetInt) - report_fatal_error("Bit set element offset must be an integer constant"); -} - -static const unsigned kX86JumpTableEntrySize = 8; - -unsigned LowerBitSets::getJumpTableEntrySize() { - if (Arch != Triple::x86 && Arch != Triple::x86_64) - report_fatal_error("Unsupported architecture for jump tables"); - - return kX86JumpTableEntrySize; -} - -// Create a constant representing a jump table entry for the target. This -// consists of an instruction sequence containing a relative branch to Dest. The -// constant will be laid out at address Src+(Len*Distance) where Len is the -// target-specific jump table entry size. -Constant *LowerBitSets::createJumpTableEntry(GlobalObject *Src, Function *Dest, - unsigned Distance) { - if (Arch != Triple::x86 && Arch != Triple::x86_64) - report_fatal_error("Unsupported architecture for jump tables"); - - const unsigned kJmpPCRel32Code = 0xe9; - const unsigned kInt3Code = 0xcc; - - ConstantInt *Jmp = ConstantInt::get(Int8Ty, kJmpPCRel32Code); - - // Build a constant representing the displacement between the constant's - // address and Dest. This will resolve to a PC32 relocation referring to Dest. - Constant *DestInt = ConstantExpr::getPtrToInt(Dest, IntPtrTy); - Constant *SrcInt = ConstantExpr::getPtrToInt(Src, IntPtrTy); - Constant *Disp = ConstantExpr::getSub(DestInt, SrcInt); - ConstantInt *DispOffset = - ConstantInt::get(IntPtrTy, Distance * kX86JumpTableEntrySize + 5); - Constant *OffsetedDisp = ConstantExpr::getSub(Disp, DispOffset); - OffsetedDisp = ConstantExpr::getTrunc(OffsetedDisp, Int32Ty); - - ConstantInt *Int3 = ConstantInt::get(Int8Ty, kInt3Code); - - Constant *Fields[] = { - Jmp, OffsetedDisp, Int3, Int3, Int3, - }; - return ConstantStruct::getAnon(Fields, /*Packed=*/true); -} - -Type *LowerBitSets::getJumpTableEntryType() { - if (Arch != Triple::x86 && Arch != Triple::x86_64) - report_fatal_error("Unsupported architecture for jump tables"); - - return StructType::get(M->getContext(), - {Int8Ty, Int32Ty, Int8Ty, Int8Ty, Int8Ty}, - /*Packed=*/true); -} - -/// Given a disjoint set of bitsets and functions, build a jump table for the -/// functions, build the bit sets and lower the llvm.bitset.test calls. -void LowerBitSets::buildBitSetsFromFunctions(ArrayRef BitSets, - ArrayRef Functions) { - // Unlike the global bitset builder, the function bitset builder cannot - // re-arrange functions in a particular order and base its calculations on the - // layout of the functions' entry points, as we have no idea how large a - // particular function will end up being (the size could even depend on what - // this pass does!) Instead, we build a jump table, which is a block of code - // consisting of one branch instruction for each of the functions in the bit - // set that branches to the target function, and redirect any taken function - // addresses to the corresponding jump table entry. In the object file's - // symbol table, the symbols for the target functions also refer to the jump - // table entries, so that addresses taken outside the module will pass any - // verification done inside the module. - // - // In more concrete terms, suppose we have three functions f, g, h which are - // members of a single bitset, and a function foo that returns their - // addresses: - // - // f: - // mov 0, %eax - // ret - // - // g: - // mov 1, %eax - // ret - // - // h: - // mov 2, %eax - // ret - // - // foo: - // mov f, %eax - // mov g, %edx - // mov h, %ecx - // ret - // - // To create a jump table for these functions, we instruct the LLVM code - // generator to output a jump table in the .text section. This is done by - // representing the instructions in the jump table as an LLVM constant and - // placing them in a global variable in the .text section. The end result will - // (conceptually) look like this: - // - // f: - // jmp .Ltmp0 ; 5 bytes - // int3 ; 1 byte - // int3 ; 1 byte - // int3 ; 1 byte - // - // g: - // jmp .Ltmp1 ; 5 bytes - // int3 ; 1 byte - // int3 ; 1 byte - // int3 ; 1 byte - // - // h: - // jmp .Ltmp2 ; 5 bytes - // int3 ; 1 byte - // int3 ; 1 byte - // int3 ; 1 byte - // - // .Ltmp0: - // mov 0, %eax - // ret - // - // .Ltmp1: - // mov 1, %eax - // ret - // - // .Ltmp2: - // mov 2, %eax - // ret - // - // foo: - // mov f, %eax - // mov g, %edx - // mov h, %ecx - // ret - // - // Because the addresses of f, g, h are evenly spaced at a power of 2, in the - // normal case the check can be carried out using the same kind of simple - // arithmetic that we normally use for globals. - - assert(!Functions.empty()); - - // Build a simple layout based on the regular layout of jump tables. - DenseMap GlobalLayout; - unsigned EntrySize = getJumpTableEntrySize(); - for (unsigned I = 0; I != Functions.size(); ++I) - GlobalLayout[Functions[I]] = I * EntrySize; - - // Create a constant to hold the jump table. - ArrayType *JumpTableType = - ArrayType::get(getJumpTableEntryType(), Functions.size()); - auto JumpTable = new GlobalVariable(*M, JumpTableType, - /*isConstant=*/true, - GlobalValue::PrivateLinkage, nullptr); - JumpTable->setSection(ObjectFormat == Triple::MachO - ? "__TEXT,__text,regular,pure_instructions" - : ".text"); - lowerBitSetCalls(BitSets, JumpTable, GlobalLayout); - - // Build aliases pointing to offsets into the jump table, and replace - // references to the original functions with references to the aliases. - for (unsigned I = 0; I != Functions.size(); ++I) { - Constant *CombinedGlobalElemPtr = ConstantExpr::getBitCast( - ConstantExpr::getGetElementPtr( - JumpTableType, JumpTable, - ArrayRef{ConstantInt::get(IntPtrTy, 0), - ConstantInt::get(IntPtrTy, I)}), - Functions[I]->getType()); - if (LinkerSubsectionsViaSymbols || Functions[I]->isDeclarationForLinker()) { - Functions[I]->replaceAllUsesWith(CombinedGlobalElemPtr); - } else { - GlobalAlias *GAlias = GlobalAlias::create(Functions[I]->getType(), - Functions[I]->getLinkage(), "", - CombinedGlobalElemPtr, M); - GAlias->setVisibility(Functions[I]->getVisibility()); - GAlias->takeName(Functions[I]); - Functions[I]->replaceAllUsesWith(GAlias); - } - if (!Functions[I]->isDeclarationForLinker()) - Functions[I]->setLinkage(GlobalValue::PrivateLinkage); - } - - // Build and set the jump table's initializer. - std::vector JumpTableEntries; - for (unsigned I = 0; I != Functions.size(); ++I) - JumpTableEntries.push_back( - createJumpTableEntry(JumpTable, Functions[I], I)); - JumpTable->setInitializer( - ConstantArray::get(JumpTableType, JumpTableEntries)); -} - -void LowerBitSets::buildBitSetsFromDisjointSet( - ArrayRef BitSets, ArrayRef Globals) { - llvm::DenseMap BitSetIndices; - llvm::DenseMap GlobalIndices; - for (auto B : BitSets) - BitSetIndices[B] = BitSetIndices.size(); - for (auto G : Globals) - GlobalIndices[G] = GlobalIndices.size(); - - // For each bitset, build a set of indices that refer to globals referenced by - // the bitset. - std::vector> BitSetMembers(BitSets.size()); - if (BitSetNM) { - for (MDNode *Op : BitSetNM->operands()) { - // Op = { bitset name, global, offset } - if (!Op->getOperand(1)) - continue; - auto I = BitSetIndices.find(Op->getOperand(0)); - if (I == BitSetIndices.end()) - continue; - - auto OpGlobal = dyn_cast( - cast(Op->getOperand(1))->getValue()); - if (!OpGlobal) - continue; - BitSetMembers[I->second].insert(GlobalIndices[OpGlobal]); - } - } - - // Order the sets of indices by size. The GlobalLayoutBuilder works best - // when given small index sets first. - std::stable_sort( - BitSetMembers.begin(), BitSetMembers.end(), - [](const std::set &O1, const std::set &O2) { - return O1.size() < O2.size(); - }); - - // Create a GlobalLayoutBuilder and provide it with index sets as layout - // fragments. The GlobalLayoutBuilder tries to lay out members of fragments as - // close together as possible. - GlobalLayoutBuilder GLB(Globals.size()); - for (auto &&MemSet : BitSetMembers) - GLB.addFragment(MemSet); - - // Build the bitsets from this disjoint set. - if (Globals.empty() || isa(Globals[0])) { - // Build a vector of global variables with the computed layout. - std::vector OrderedGVs(Globals.size()); - auto OGI = OrderedGVs.begin(); - for (auto &&F : GLB.Fragments) { - for (auto &&Offset : F) { - auto GV = dyn_cast(Globals[Offset]); - if (!GV) - report_fatal_error( - "Bit set may not contain both global variables and functions"); - *OGI++ = GV; - } - } - - buildBitSetsFromGlobalVariables(BitSets, OrderedGVs); - } else { - // Build a vector of functions with the computed layout. - std::vector OrderedFns(Globals.size()); - auto OFI = OrderedFns.begin(); - for (auto &&F : GLB.Fragments) { - for (auto &&Offset : F) { - auto Fn = dyn_cast(Globals[Offset]); - if (!Fn) - report_fatal_error( - "Bit set may not contain both global variables and functions"); - *OFI++ = Fn; - } - } - - buildBitSetsFromFunctions(BitSets, OrderedFns); - } -} - /// Lower all bit sets in this module. bool LowerBitSets::buildBitSets() { Function *BitSetTestFunc = @@ -926,36 +598,24 @@ bool LowerBitSets::buildBitSets() { // Equivalence class set containing bitsets and the globals they reference. // This is used to partition the set of bitsets in the module into disjoint // sets. - typedef EquivalenceClasses> + typedef EquivalenceClasses> GlobalClassesTy; GlobalClassesTy GlobalClasses; - // Verify the bitset metadata and build a mapping from bitset identifiers to - // their last observed index in BitSetNM. This will used later to - // deterministically order the list of bitset identifiers. - llvm::DenseMap BitSetIdIndices; - if (BitSetNM) { - for (unsigned I = 0, E = BitSetNM->getNumOperands(); I != E; ++I) { - MDNode *Op = BitSetNM->getOperand(I); - verifyBitSetMDNode(Op); - BitSetIdIndices[Op] = I; - } - } - for (const Use &U : BitSetTestFunc->uses()) { auto CI = cast(U.getUser()); auto BitSetMDVal = dyn_cast(CI->getArgOperand(1)); - if (!BitSetMDVal) + if (!BitSetMDVal || !isa(BitSetMDVal->getMetadata())) report_fatal_error( - "Second argument of llvm.bitset.test must be metadata"); - auto BitSet = BitSetMDVal->getMetadata(); + "Second argument of llvm.bitset.test must be metadata string"); + auto BitSet = cast(BitSetMDVal->getMetadata()); // Add the call site to the list of call sites for this bit set. We also use // BitSetTestCallSites to keep track of whether we have seen this bit set // before. If we have, we don't need to re-add the referenced globals to the // equivalence class. - std::pair>::iterator, + std::pair>::iterator, bool> Ins = BitSetTestCallSites.insert( std::make_pair(BitSet, std::vector())); @@ -970,16 +630,31 @@ bool LowerBitSets::buildBitSets() { if (!BitSetNM) continue; - // Add the referenced globals to the bitset's equivalence class. + // Verify the bitset metadata and add the referenced globals to the bitset's + // equivalence class. for (MDNode *Op : BitSetNM->operands()) { + if (Op->getNumOperands() != 3) + report_fatal_error( + "All operands of llvm.bitsets metadata must have 3 elements"); + if (Op->getOperand(0) != BitSet || !Op->getOperand(1)) continue; - auto OpGlobal = dyn_cast( - cast(Op->getOperand(1))->getValue()); + auto OpConstMD = dyn_cast(Op->getOperand(1)); + if (!OpConstMD) + report_fatal_error("Bit set element must be a constant"); + auto OpGlobal = dyn_cast(OpConstMD->getValue()); if (!OpGlobal) continue; + auto OffsetConstMD = dyn_cast(Op->getOperand(2)); + if (!OffsetConstMD) + report_fatal_error("Bit set element offset must be a constant"); + auto OffsetInt = dyn_cast(OffsetConstMD->getValue()); + if (!OffsetInt) + report_fatal_error( + "Bit set element offset must be an integer constant"); + CurSet = GlobalClasses.unionSets( CurSet, GlobalClasses.findLeader(GlobalClasses.insert(OpGlobal))); } @@ -996,25 +671,71 @@ bool LowerBitSets::buildBitSets() { ++NumBitSetDisjointSets; - // Build the list of bitsets in this disjoint set. - std::vector BitSets; - std::vector Globals; + // Build the list of bitsets and referenced globals in this disjoint set. + std::vector BitSets; + std::vector Globals; + llvm::DenseMap BitSetIndices; + llvm::DenseMap GlobalIndices; for (GlobalClassesTy::member_iterator MI = GlobalClasses.member_begin(I); MI != GlobalClasses.member_end(); ++MI) { - if ((*MI).is()) - BitSets.push_back(MI->get()); - else - Globals.push_back(MI->get()); + if ((*MI).is()) { + BitSetIndices[MI->get()] = BitSets.size(); + BitSets.push_back(MI->get()); + } else { + GlobalIndices[MI->get()] = Globals.size(); + Globals.push_back(MI->get()); + } + } + + // For each bitset, build a set of indices that refer to globals referenced + // by the bitset. + std::vector> BitSetMembers(BitSets.size()); + if (BitSetNM) { + for (MDNode *Op : BitSetNM->operands()) { + // Op = { bitset name, global, offset } + if (!Op->getOperand(1)) + continue; + auto I = BitSetIndices.find(cast(Op->getOperand(0))); + if (I == BitSetIndices.end()) + continue; + + auto OpGlobal = dyn_cast( + cast(Op->getOperand(1))->getValue()); + if (!OpGlobal) + continue; + BitSetMembers[I->second].insert(GlobalIndices[OpGlobal]); + } } - // Order bitsets by BitSetNM index for determinism. This ordering is stable - // as there is a one-to-one mapping between metadata and indices. - std::sort(BitSets.begin(), BitSets.end(), [&](Metadata *M1, Metadata *M2) { - return BitSetIdIndices[M1] < BitSetIdIndices[M2]; + // Order the sets of indices by size. The GlobalLayoutBuilder works best + // when given small index sets first. + std::stable_sort( + BitSetMembers.begin(), BitSetMembers.end(), + [](const std::set &O1, const std::set &O2) { + return O1.size() < O2.size(); + }); + + // Create a GlobalLayoutBuilder and provide it with index sets as layout + // fragments. The GlobalLayoutBuilder tries to lay out members of fragments + // as close together as possible. + GlobalLayoutBuilder GLB(Globals.size()); + for (auto &&MemSet : BitSetMembers) + GLB.addFragment(MemSet); + + // Build a vector of globals with the computed layout. + std::vector OrderedGlobals(Globals.size()); + auto OGI = OrderedGlobals.begin(); + for (auto &&F : GLB.Fragments) + for (auto &&Offset : F) + *OGI++ = Globals[Offset]; + + // Order bitsets by name for determinism. + std::sort(BitSets.begin(), BitSets.end(), [](MDString *S1, MDString *S2) { + return S1->getString() < S2->getString(); }); - // Lower the bitsets in this disjoint set. - buildBitSetsFromDisjointSet(BitSets, Globals); + // Build the bitsets from this disjoint set. + buildBitSetsFromGlobals(BitSets, OrderedGlobals); } allocateByteArrays(); diff --git a/test/Transforms/LowerBitSets/function-ext.ll b/test/Transforms/LowerBitSets/function-ext.ll deleted file mode 100644 index 2a83bef2f07..00000000000 --- a/test/Transforms/LowerBitSets/function-ext.ll +++ /dev/null @@ -1,22 +0,0 @@ -; RUN: opt -S -lowerbitsets < %s | FileCheck %s - -; Tests that we correctly handle external references, including the case where -; all functions in a bitset are external references. - -target triple = "x86_64-unknown-linux-gnu" - -declare void @foo() - -; CHECK: @[[JT:.*]] = private constant [1 x <{ i8, i32, i8, i8, i8 }>] [<{ i8, i32, i8, i8, i8 }> <{ i8 -23, i32 trunc (i64 sub (i64 sub (i64 ptrtoint (void ()* @foo to i64), i64 ptrtoint ([1 x <{ i8, i32, i8, i8, i8 }>]* @[[JT]] to i64)), i64 5) to i32), i8 -52, i8 -52, i8 -52 }>], section ".text" - -define i1 @bar(i8* %ptr) { - ; CHECK: icmp eq i64 {{.*}}, ptrtoint ([1 x <{ i8, i32, i8, i8, i8 }>]* @[[JT]] to i64) - %p = call i1 @llvm.bitset.test(i8* %ptr, metadata !"void") - ret i1 %p -} - -declare i1 @llvm.bitset.test(i8* %ptr, metadata %bitset) nounwind readnone - -!0 = !{!"void", void ()* @foo, i64 0} - -!llvm.bitsets = !{!0} diff --git a/test/Transforms/LowerBitSets/function.ll b/test/Transforms/LowerBitSets/function.ll deleted file mode 100644 index 0aa947e5a2d..00000000000 --- a/test/Transforms/LowerBitSets/function.ll +++ /dev/null @@ -1,35 +0,0 @@ -; RUN: opt -S -lowerbitsets < %s | FileCheck %s - -; Tests that we correctly create a jump table for bitsets containing 2 or more -; functions. - -target triple = "x86_64-unknown-linux-gnu" -target datalayout = "e-p:64:64" - -; CHECK: @[[JT:.*]] = private constant [2 x <{ i8, i32, i8, i8, i8 }>] [<{ i8, i32, i8, i8, i8 }> <{ i8 -23, i32 trunc (i64 sub (i64 sub (i64 ptrtoint (void ()* @[[FNAME:.*]] to i64), i64 ptrtoint ([2 x <{ i8, i32, i8, i8, i8 }>]* @[[JT]] to i64)), i64 5) to i32), i8 -52, i8 -52, i8 -52 }>, <{ i8, i32, i8, i8, i8 }> <{ i8 -23, i32 trunc (i64 sub (i64 sub (i64 ptrtoint (void ()* @[[GNAME:.*]] to i64), i64 ptrtoint ([2 x <{ i8, i32, i8, i8, i8 }>]* @[[JT]] to i64)), i64 13) to i32), i8 -52, i8 -52, i8 -52 }>], section ".text" - -; CHECK: @f = alias bitcast ([2 x <{ i8, i32, i8, i8, i8 }>]* @[[JT]] to void ()*) -; CHECK: @g = alias bitcast (<{ i8, i32, i8, i8, i8 }>* getelementptr inbounds ([2 x <{ i8, i32, i8, i8, i8 }>], [2 x <{ i8, i32, i8, i8, i8 }>]* @[[JT]], i64 0, i64 1) to void ()*) - -; CHECK: define private void @[[FNAME]]() { -define void @f() { - ret void -} - -; CHECK: define private void @[[GNAME]]() { -define void @g() { - ret void -} - -!0 = !{!"bitset1", void ()* @f, i32 0} -!1 = !{!"bitset1", void ()* @g, i32 0} - -!llvm.bitsets = !{ !0, !1 } - -declare i1 @llvm.bitset.test(i8* %ptr, metadata %bitset) nounwind readnone - -define i1 @foo(i8* %p) { - ; CHECK: sub i64 {{.*}}, ptrtoint ([2 x <{ i8, i32, i8, i8, i8 }>]* @[[JT]] to i64) - %x = call i1 @llvm.bitset.test(i8* %p, metadata !"bitset1") - ret i1 %x -} diff --git a/test/Transforms/LowerBitSets/nonstring.ll b/test/Transforms/LowerBitSets/nonstring.ll deleted file mode 100644 index 0d655da3bb0..00000000000 --- a/test/Transforms/LowerBitSets/nonstring.ll +++ /dev/null @@ -1,34 +0,0 @@ -; RUN: opt -S -lowerbitsets < %s | FileCheck %s - -; Tests that non-string metadata nodes may be used as bitset identifiers. - -target datalayout = "e-p:32:32" - -; CHECK: @[[BNAME:.*]] = private constant { [2 x i32] } -; CHECK: @[[ANAME:.*]] = private constant { i32 } - -@a = constant i32 1 -@b = constant [2 x i32] [i32 2, i32 3] - -!0 = !{!2, i32* @a, i32 0} -!1 = !{!3, [2 x i32]* @b, i32 0} -!2 = distinct !{} -!3 = distinct !{} - -!llvm.bitsets = !{ !0, !1 } - -declare i1 @llvm.bitset.test(i8* %ptr, metadata %bitset) nounwind readnone - -; CHECK-LABEL: @foo -define i1 @foo(i8* %p) { - ; CHECK: icmp eq i32 {{.*}}, ptrtoint ({ i32 }* @[[ANAME]] to i32) - %x = call i1 @llvm.bitset.test(i8* %p, metadata !2) - ret i1 %x -} - -; CHECK-LABEL: @bar -define i1 @bar(i8* %p) { - ; CHECK: icmp eq i32 {{.*}}, ptrtoint ({ [2 x i32] }* @[[BNAME]] to i32) - %x = call i1 @llvm.bitset.test(i8* %p, metadata !3) - ret i1 %x -} diff --git a/test/Transforms/LowerBitSets/simple.ll b/test/Transforms/LowerBitSets/simple.ll index df2ccf98786..0fcdf0b36d6 100644 --- a/test/Transforms/LowerBitSets/simple.ll +++ b/test/Transforms/LowerBitSets/simple.ll @@ -6,8 +6,8 @@ target datalayout = "e-p:32:32" ; CHECK: [[G:@[^ ]*]] = private constant { i32, [0 x i8], [63 x i32], [4 x i8], i32, [0 x i8], [2 x i32] } { i32 1, [0 x i8] zeroinitializer, [63 x i32] zeroinitializer, [4 x i8] zeroinitializer, i32 3, [0 x i8] zeroinitializer, [2 x i32] [i32 4, i32 5] } @a = constant i32 1 -@b = hidden constant [63 x i32] zeroinitializer -@c = protected constant i32 3 +@b = constant [63 x i32] zeroinitializer +@c = constant i32 3 @d = constant [2 x i32] [i32 4, i32 5] ; CHECK: [[BA:@[^ ]*]] = private constant [68 x i8] c"\03\01\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\02\00\01" @@ -43,8 +43,8 @@ target datalayout = "e-p:32:32" ; CHECK: @bits_use.{{[0-9]*}} = private alias i8* @bits{{[0-9]*}} ; CHECK: @a = alias getelementptr inbounds ({ i32, [0 x i8], [63 x i32], [4 x i8], i32, [0 x i8], [2 x i32] }, { i32, [0 x i8], [63 x i32], [4 x i8], i32, [0 x i8], [2 x i32] }* [[G]], i32 0, i32 0) -; CHECK: @b = hidden alias getelementptr inbounds ({ i32, [0 x i8], [63 x i32], [4 x i8], i32, [0 x i8], [2 x i32] }, { i32, [0 x i8], [63 x i32], [4 x i8], i32, [0 x i8], [2 x i32] }* [[G]], i32 0, i32 2) -; CHECK: @c = protected alias getelementptr inbounds ({ i32, [0 x i8], [63 x i32], [4 x i8], i32, [0 x i8], [2 x i32] }, { i32, [0 x i8], [63 x i32], [4 x i8], i32, [0 x i8], [2 x i32] }* [[G]], i32 0, i32 4) +; CHECK: @b = alias getelementptr inbounds ({ i32, [0 x i8], [63 x i32], [4 x i8], i32, [0 x i8], [2 x i32] }, { i32, [0 x i8], [63 x i32], [4 x i8], i32, [0 x i8], [2 x i32] }* [[G]], i32 0, i32 2) +; CHECK: @c = alias getelementptr inbounds ({ i32, [0 x i8], [63 x i32], [4 x i8], i32, [0 x i8], [2 x i32] }, { i32, [0 x i8], [63 x i32], [4 x i8], i32, [0 x i8], [2 x i32] }* [[G]], i32 0, i32 4) ; CHECK: @d = alias getelementptr inbounds ({ i32, [0 x i8], [63 x i32], [4 x i8], i32, [0 x i8], [2 x i32] }, { i32, [0 x i8], [63 x i32], [4 x i8], i32, [0 x i8], [2 x i32] }* [[G]], i32 0, i32 6) ; CHECK-DARWIN: @aptr = constant i32* getelementptr inbounds ({ i32, [0 x i8], [63 x i32], [4 x i8], i32, [0 x i8], [2 x i32] }, { i32, [0 x i8], [63 x i32], [4 x i8], i32, [0 x i8], [2 x i32] }* [[G:@[^ ]*]], i32 0, i32 0) -- 2.34.1