Index: docs/BitSets.rst =================================================================== --- docs/BitSets.rst +++ docs/BitSets.rst @@ -10,17 +10,28 @@ To use the mechanism, a client creates a global metadata node named ``llvm.bitsets``. Each element is a metadata node with three elements: -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. +the first is a metadata object representing an identifier for the bitset, +the second is either a global variable or a function and the third is a +byte offset into the global (generally zero for functions). Each bitset must +exclusively contain either global variables or functions. 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 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. +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, but functions in bit sets may +refer to external entities. + +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. However, the identity of functions defined +within the module will be consistent within and outside of the module, +as the jump table entry will always act as the function entry point. + +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: Index: include/llvm/Transforms/IPO/LowerBitSets.h =================================================================== --- include/llvm/Transforms/IPO/LowerBitSets.h +++ include/llvm/Transforms/IPO/LowerBitSets.h @@ -26,7 +26,7 @@ namespace llvm { class DataLayout; -class GlobalVariable; +class GlobalObject; class Value; class raw_ostream; @@ -56,7 +56,7 @@ 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; Index: lib/Transforms/IPO/LowerBitSets.cpp =================================================================== --- lib/Transforms/IPO/LowerBitSets.cpp +++ lib/Transforms/IPO/LowerBitSets.cpp @@ -19,6 +19,8 @@ #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" @@ -61,9 +63,9 @@ 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; @@ -211,6 +213,7 @@ Module *M; bool LinkerSubsectionsViaSymbols; + Triple::ArchType Arch; IntegerType *Int1Ty; IntegerType *Int8Ty; IntegerType *Int32Ty; @@ -221,24 +224,32 @@ // The llvm.bitsets named metadata. NamedMDNode *BitSetNM; - // Mapping from bitset mdstrings to the call sites that test them. - DenseMap> BitSetTestCallSites; + // Mapping from bitset identifiers to the call sites that test them. + DenseMap> BitSetTestCallSites; std::vector ByteArrayInfos; BitSetInfo - buildBitSet(MDString *BitSet, - const DenseMap &GlobalLayout); + buildBitSet(Metadata *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 *CombinedGlobalIntAddr, + const DenseMap &GlobalLayout); Value * lowerBitSetCall(CallInst *CI, BitSetInfo &BSI, ByteArrayInfo *&BAI, - GlobalVariable *CombinedGlobal, - const DenseMap &GlobalLayout); - void buildBitSetsFromGlobals(const std::vector &BitSets, - const std::vector &Globals); + Constant *CombinedGlobal, + const DenseMap &GlobalLayout); + void buildBitSetsFromGlobalVariables(ArrayRef BitSets, + ArrayRef Globals); + unsigned getJumpTableEntrySize(); + Constant *createJumpTableEntry(Function *Src, Function *Dest, + unsigned Distance); + void buildBitSetsFromFunctions(ArrayRef BitSets, + ArrayRef Globals); bool buildBitSets(); bool eraseBitSetMetadata(); @@ -262,6 +273,7 @@ Triple TargetTriple(M->getTargetTriple()); LinkerSubsectionsViaSymbols = TargetTriple.isMacOSX(); + Arch = TargetTriple.getArch(); Int1Ty = Type::getInt1Ty(M->getContext()); Int8Ty = Type::getInt8Ty(M->getContext()); @@ -280,8 +292,8 @@ /// Build a bit set for BitSet using the object layouts in /// GlobalLayout. BitSetInfo LowerBitSets::buildBitSet( - MDString *BitSet, - const DenseMap &GlobalLayout) { + Metadata *BitSet, + const DenseMap &GlobalLayout) { BitSetBuilder BSB; // Compute the byte offset of each element of this bitset. @@ -289,8 +301,11 @@ for (MDNode *Op : BitSetNM->operands()) { if (Op->getOperand(0) != BitSet || !Op->getOperand(1)) continue; - auto OpGlobal = dyn_cast( - cast(Op->getOperand(1))->getValue()); + Constant *OpConst = + cast(Op->getOperand(1))->getValue(); + if (auto GA = dyn_cast(OpConst)) + OpConst = GA->getAliasee(); + auto OpGlobal = dyn_cast(OpConst); if (!OpGlobal) continue; uint64_t Offset = @@ -439,17 +454,16 @@ /// replace the call with. Value *LowerBitSets::lowerBitSetCall( CallInst *CI, BitSetInfo &BSI, ByteArrayInfo *&BAI, - GlobalVariable *CombinedGlobal, - const DenseMap &GlobalLayout) { + Constant *CombinedGlobalIntAddr, + const DenseMap &GlobalLayout) { Value *Ptr = CI->getArgOperand(0); const DataLayout &DL = M->getDataLayout(); if (BSI.containsValue(DL, GlobalLayout, Ptr)) - return ConstantInt::getTrue(CombinedGlobal->getParent()->getContext()); + return ConstantInt::getTrue(M->getContext()); - Constant *GlobalAsInt = ConstantExpr::getPtrToInt(CombinedGlobal, IntPtrTy); Constant *OffsetedGlobalAsInt = ConstantExpr::getAdd( - GlobalAsInt, ConstantInt::get(IntPtrTy, BSI.ByteOffset)); + CombinedGlobalIntAddr, ConstantInt::get(IntPtrTy, BSI.ByteOffset)); BasicBlock *InitialBB = CI->getParent(); @@ -508,18 +522,19 @@ /// Given a disjoint set of bitsets and globals, layout the globals, build the /// bit sets and lower the llvm.bitset.test calls. -void LowerBitSets::buildBitSetsFromGlobals( - const std::vector &BitSets, - const std::vector &Globals) { +void LowerBitSets::buildBitSetsFromGlobalVariables( + ArrayRef BitSets, ArrayRef 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 to align the next element to the - // next power of 2. + // Compute the amount of padding required. uint64_t Padding = NextPowerOf2(InitSize - 1) - InitSize; // Cap at 128 was found experimentally to have a good data/instruction @@ -541,17 +556,84 @@ 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); + Constant *GlobalAsInt = ConstantExpr::getPtrToInt(CombinedGlobal, IntPtrTy); + lowerBitSetCalls(BitSets, GlobalAsInt, GlobalLayout); + + // Build aliases pointing to offsets into the combined global for each + // global from which we built the combined global, and replace references + // to the original globals with references to the aliases. + for (unsigned I = 0; I != Globals.size(); ++I) { + // Multiply by 2 to account for padding elements. + Constant *CombinedGlobalIdxs[] = {ConstantInt::get(Int32Ty, 0), + ConstantInt::get(Int32Ty, I * 2)}; + Constant *CombinedGlobalElemPtr = ConstantExpr::getGetElementPtr( + NewInit->getType(), CombinedGlobal, CombinedGlobalIdxs); + if (LinkerSubsectionsViaSymbols) { + Globals[I]->replaceAllUsesWith(CombinedGlobalElemPtr); + } else { + 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); + } + Globals[I]->eraseFromParent(); + } +} + +unsigned LowerBitSets::getJumpTableEntrySize() { + if (Arch != Triple::x86 && Arch != Triple::x86_64) + report_fatal_error("Unsupported architecture for jump tables"); + + return 8; +} + +// 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(Function *Src, Function *Dest, + unsigned Distance) { + if (Arch != Triple::x86 && Arch != Triple::x86_64) + report_fatal_error("Unsupported architecture for jump tables"); + + ConstantInt *Jmp = ConstantInt::get(Int8Ty, 0xe9); + + // 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, Int64Ty); + Constant *SrcInt = ConstantExpr::getPtrToInt(Src, Int64Ty); + Constant *Disp = ConstantExpr::getSub(DestInt, SrcInt); + ConstantInt *DispOffset = ConstantInt::get(Int64Ty, Distance * 8 - 5); + Constant *OffsetedDisp = ConstantExpr::getAdd(Disp, DispOffset); + OffsetedDisp = ConstantExpr::getTrunc(OffsetedDisp, Int32Ty); + + ConstantInt *Int3 = ConstantInt::get(Int8Ty, 0xcc); + + Constant *Fields[] = { + Jmp, OffsetedDisp, Int3, Int3, Int3, + }; + return ConstantStruct::getAnon(Fields, /*Packed=*/true); +} + +void LowerBitSets::lowerBitSetCalls( + ArrayRef BitSets, Constant *CombinedGlobalIntAddr, + const DenseMap &GlobalLayout) { // For each bitset in this disjoint set... - for (MDString *BS : BitSets) { + for (Metadata *BS : BitSets) { // Build the bitset. BitSetInfo BSI = buildBitSet(BS, GlobalLayout); DEBUG({ - dbgs() << BS->getString() << ": "; + if (auto BSS = dyn_cast(BS)) + dbgs() << BSS->getString() << ": "; + else + dbgs() << ": "; BSI.print(dbgs()); }); @@ -560,32 +642,94 @@ // Lower each call to llvm.bitset.test for this bitset. for (CallInst *CI : BitSetTestCallSites[BS]) { ++NumBitSetCallsLowered; - Value *Lowered = lowerBitSetCall(CI, BSI, BAI, CombinedGlobal, GlobalLayout); + Value *Lowered = + lowerBitSetCall(CI, BSI, BAI, CombinedGlobalIntAddr, 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 - // to the original globals with references to the aliases. - for (unsigned I = 0; I != Globals.size(); ++I) { - // Multiply by 2 to account for padding elements. - Constant *CombinedGlobalIdxs[] = {ConstantInt::get(Int32Ty, 0), - ConstantInt::get(Int32Ty, I * 2)}; - Constant *CombinedGlobalElemPtr = ConstantExpr::getGetElementPtr( - NewInit->getType(), CombinedGlobal, CombinedGlobalIdxs); - if (LinkerSubsectionsViaSymbols) { +/// 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. +/// The function array must be non-empty (the buildBitSetsFromGlobalVariables +/// function handles any empty bit sets). +void LowerBitSets::buildBitSetsFromFunctions(ArrayRef BitSets, + ArrayRef Globals) { + // This simple layout is based on the regular layout of jump tables. + DenseMap GlobalLayout; + unsigned EntrySize = getJumpTableEntrySize(); + for (unsigned I = 0; I != Globals.size(); ++I) + GlobalLayout[Globals[I]] = I * EntrySize; + + std::vector NewGlobals; + // If our last function is an external reference, we won't have a function to + // hang our jump table onto. To handle this special case, we create a dummy + // function and use that as the last function. + if (Globals.back()->isDeclarationForLinker()) { + NewGlobals.insert(NewGlobals.end(), Globals.begin(), Globals.end()); + Function *NewFn = Function::Create( + FunctionType::get(Type::getVoidTy(M->getContext()), /*isVarArg=*/false), + Function::PrivateLinkage, "__function_bitset_dummy", M); + BasicBlock *BB = BasicBlock::Create(M->getContext(), "", NewFn); + IRBuilder<> IRB(BB); + IRB.CreateUnreachable(); + NewGlobals.push_back(NewFn); + Globals = NewGlobals; + } + + Function *LastFn = Globals.back(); + + // Create the alias for the last function first, so that any references to + // LastFn created below use the real LastFn rather than being RAUW'd. + if (!LinkerSubsectionsViaSymbols) { + GlobalAlias *GAlias = GlobalAlias::create( + LastFn->getType(), LastFn->getLinkage(), "", nullptr, M); + GAlias->setVisibility(LastFn->getVisibility()); + GAlias->takeName(LastFn); + LastFn->replaceAllUsesWith(GAlias); + GAlias->setAliasee(LastFn); + } + LastFn->setLinkage(GlobalValue::PrivateLinkage); + + Constant *JumpTableBegin = ConstantExpr::getSub( + ConstantExpr::getPtrToInt(LastFn, Int64Ty), + ConstantInt::get(Int64Ty, EntrySize * (Globals.size() - 1))); + lowerBitSetCalls(BitSets, JumpTableBegin, 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 != Globals.size() - 1; ++I) { + Constant *CombinedGlobalElemPtr = ConstantExpr::getIntToPtr( + ConstantExpr::getSub( + ConstantExpr::getPtrToInt(LastFn, Int64Ty), + ConstantInt::get(Int64Ty, EntrySize * (Globals.size() - 1 - I))), + Globals[I]->getType()); + if (LinkerSubsectionsViaSymbols || Globals[I]->isDeclarationForLinker()) { Globals[I]->replaceAllUsesWith(CombinedGlobalElemPtr); } else { 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); } - Globals[I]->eraseFromParent(); + if (!Globals[I]->isDeclarationForLinker()) + Globals[I]->setLinkage(GlobalValue::PrivateLinkage); } + + // Modify the prefix data of the last function in Globals to contain jump + // table entries for every other element. + std::vector JumpTableEntries; + for (unsigned I = 0; I != Globals.size() - 1; ++I) { + JumpTableEntries.push_back( + createJumpTableEntry(LastFn, Globals[I], Globals.size() - 1 - I)); + } + if (!JumpTableEntries.empty()) + LastFn->setPrefixData(ConstantArray::get( + ArrayType::get(JumpTableEntries[0]->getType(), JumpTableEntries.size()), + JumpTableEntries)); } /// Lower all bit sets in this module. @@ -598,24 +742,64 @@ // 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); + if (Op->getNumOperands() != 3) + report_fatal_error( + "All operands of llvm.bitsets metadata must have 3 elements"); + if (!Op->getOperand(1)) + continue; + + 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; + + 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"); + + BitSetIdIndices[Op] = I; + } + } + for (const Use &U : BitSetTestFunc->uses()) { auto CI = cast(U.getUser()); auto BitSetMDVal = dyn_cast(CI->getArgOperand(1)); - if (!BitSetMDVal || !isa(BitSetMDVal->getMetadata())) + if (!BitSetMDVal) report_fatal_error( - "Second argument of llvm.bitset.test must be metadata string"); - auto BitSet = cast(BitSetMDVal->getMetadata()); + "Second argument of llvm.bitset.test must be metadata"); + auto BitSet = 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())); @@ -630,31 +814,16 @@ if (!BitSetNM) continue; - // Verify the bitset metadata and add the referenced globals to the bitset's - // equivalence class. + // 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 OpConstMD = dyn_cast(Op->getOperand(1)); - if (!OpConstMD) - report_fatal_error("Bit set element must be a constant"); - auto OpGlobal = dyn_cast(OpConstMD->getValue()); + auto OpGlobal = dyn_cast( + cast(Op->getOperand(1))->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))); } @@ -672,18 +841,18 @@ ++NumBitSetDisjointSets; // Build the list of bitsets and referenced globals in this disjoint set. - std::vector BitSets; - std::vector Globals; - llvm::DenseMap BitSetIndices; - llvm::DenseMap GlobalIndices; + 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()) { - BitSetIndices[MI->get()] = BitSets.size(); - BitSets.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()); + GlobalIndices[MI->get()] = Globals.size(); + Globals.push_back(MI->get()); } } @@ -695,11 +864,11 @@ // Op = { bitset name, global, offset } if (!Op->getOperand(1)) continue; - auto I = BitSetIndices.find(cast(Op->getOperand(0))); + auto I = BitSetIndices.find(Op->getOperand(0)); if (I == BitSetIndices.end()) continue; - auto OpGlobal = dyn_cast( + auto OpGlobal = dyn_cast( cast(Op->getOperand(1))->getValue()); if (!OpGlobal) continue; @@ -707,6 +876,12 @@ } } + // 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( @@ -722,20 +897,38 @@ 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]; + // 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; + } + } - // Order bitsets by name for determinism. - std::sort(BitSets.begin(), BitSets.end(), [](MDString *S1, MDString *S2) { - return S1->getString() < S2->getString(); - }); + 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; + } + } - // Build the bitsets from this disjoint set. - buildBitSetsFromGlobals(BitSets, OrderedGlobals); + buildBitSetsFromFunctions(BitSets, OrderedFns); + } } allocateByteArrays(); Index: test/Transforms/LowerBitSets/function-ext.ll =================================================================== --- /dev/null +++ test/Transforms/LowerBitSets/function-ext.ll @@ -0,0 +1,22 @@ +; RUN: opt -S -lowerbitsets < %s | FileCheck %s + +target triple = "x86_64-unknown-linux-gnu" + +; CHECK: @__function_bitset_dummy = private alias void ()* @[[DUMMYNAME:.*]] + +declare void @foo() + +define i1 @bar(i8* %ptr) { + ; CHECK: icmp eq i64 {{.*}}, sub (i64 ptrtoint (void ()* @[[DUMMYNAME]] to i64), i64 8) + %p = call i1 @llvm.bitset.test(i8* %ptr, metadata !"void") + ret i1 %p +} + +; CHECK: define private void @[[DUMMYNAME]]() prefix [1 x <{ i8, i32, i8, i8, i8 }>] [<{ i8, i32, i8, i8, i8 }> <{ i8 -23, i32 trunc (i64 add (i64 sub (i64 ptrtoint (void ()* @foo to i64), i64 ptrtoint (void ()* @[[DUMMYNAME]] to i64)), i64 3) to i32), i8 -52, i8 -52, i8 -52 }>] +; CHECK-NEXT: unreachable + +declare i1 @llvm.bitset.test(i8* %ptr, metadata %bitset) nounwind readnone + +!0 = !{!"void", void ()* @foo, i64 0} + +!llvm.bitsets = !{!0} Index: test/Transforms/LowerBitSets/function.ll =================================================================== --- /dev/null +++ test/Transforms/LowerBitSets/function.ll @@ -0,0 +1,30 @@ +; RUN: opt -S -lowerbitsets < %s | FileCheck %s + +target triple = "x86_64-unknown-linux-gnu" +target datalayout = "e-p:64:64" + +; CHECK: @g = alias void ()* @[[GNAME:.*]] +; CHECK: @f = alias inttoptr (i64 sub (i64 ptrtoint (void ()* @[[GNAME]] to i64), i64 8) to void ()*) + +; CHECK: define private void @[[FNAME:.*]]() { +define void @f() { + ret void +} + +; CHECK: define private void @[[GNAME]]() prefix [1 x <{ i8, i32, i8, i8, i8 }>] [<{ i8, i32, i8, i8, i8 }> <{ i8 -23, i32 trunc (i64 add (i64 sub (i64 ptrtoint (void ()* @[[FNAME]] to i64), i64 ptrtoint (void ()* @[[GNAME]] to i64)), i64 3) to i32), i8 -52, i8 -52, i8 -52 }>] { +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 %1, sub (i64 ptrtoint (void ()* @[[GNAME]] to i64), i64 8) + %x = call i1 @llvm.bitset.test(i8* %p, metadata !"bitset1") + ret i1 %x +} Index: test/Transforms/LowerBitSets/function1.ll =================================================================== --- /dev/null +++ test/Transforms/LowerBitSets/function1.ll @@ -0,0 +1,23 @@ +; RUN: opt -S -lowerbitsets < %s | FileCheck %s + +target triple = "x86_64-unknown-linux-gnu" +target datalayout = "e-p:64:64" + +; CHECK: @f = alias void ()* @[[FNAME:.*]] + +; CHECK: define private void @[[FNAME]]() { +define void @f() { + ret void +} + +!0 = !{!"bitset1", void ()* @f, i32 0} + +!llvm.bitsets = !{ !0 } + +declare i1 @llvm.bitset.test(i8* %ptr, metadata %bitset) nounwind readnone + +define i1 @foo(i8* %p) { + ; CHECK: icmp eq i64 %{{.*}}, ptrtoint (void ()* @[[FNAME]] to i64) + %x = call i1 @llvm.bitset.test(i8* %p, metadata !"bitset1") + ret i1 %x +} Index: test/Transforms/LowerBitSets/nonstring.ll =================================================================== --- /dev/null +++ test/Transforms/LowerBitSets/nonstring.ll @@ -0,0 +1,28 @@ +; RUN: opt -S -lowerbitsets < %s | FileCheck %s + +target datalayout = "e-p:32:32" + +; CHECK-DAG: private constant { i32 } +@a = constant i32 1 + +; CHECK-DAG: private constant { [2 x i32] } +@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 + +define i1 @foo(i8* %p) { + %x = call i1 @llvm.bitset.test(i8* %p, metadata !2) + ret i1 %x +} + +define i1 @bar(i8* %p) { + %x = call i1 @llvm.bitset.test(i8* %p, metadata !3) + ret i1 %x +} Index: test/Transforms/LowerBitSets/simple.ll =================================================================== --- test/Transforms/LowerBitSets/simple.ll +++ test/Transforms/LowerBitSets/simple.ll @@ -6,8 +6,8 @@ ; 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 = constant [63 x i32] zeroinitializer -@c = constant i32 3 +@b = hidden constant [63 x i32] zeroinitializer +@c = protected 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 @@ ; 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 = 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: @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: @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)