diff --git a/llvm/include/llvm/IR/Module.h b/llvm/include/llvm/IR/Module.h --- a/llvm/include/llvm/IR/Module.h +++ b/llvm/include/llvm/IR/Module.h @@ -888,11 +888,6 @@ void setPartialSampleProfileRatio(const ModuleSummaryIndex &Index); }; -/// Given "llvm.used" or "llvm.compiler.used" as a global name, collect -/// the initializer elements of that global in Set and return the global itself. -GlobalVariable *collectUsedGlobalVariables(const Module &M, - SmallPtrSetImpl &Set, - bool CompilerUsed); /// Given "llvm.used" or "llvm.compiler.used" as a global name, collect the /// initializer elements of that global in a SmallVector and return the global /// itself. diff --git a/llvm/include/llvm/Transforms/Utils/FunctionImportUtils.h b/llvm/include/llvm/Transforms/Utils/FunctionImportUtils.h --- a/llvm/include/llvm/Transforms/Utils/FunctionImportUtils.h +++ b/llvm/include/llvm/Transforms/Utils/FunctionImportUtils.h @@ -54,7 +54,7 @@ /// Set of llvm.*used values, in order to validate that we don't try /// to promote any non-renamable values. - SmallPtrSet Used; + SmallPtrSet Used; /// Keep track of any COMDATs that require renaming (because COMDAT /// leader was promoted and renamed). Maps from original COMDAT to one @@ -111,10 +111,12 @@ HasExportedFunctions = ImportIndex.hasExportedFunctions(M); #ifndef NDEBUG + SmallVector Vec; // First collect those in the llvm.used set. - collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false); + collectUsedGlobalVariables(M, Vec, /*CompilerUsed=*/false); // Next collect those in the llvm.compiler.used set. - collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ true); + collectUsedGlobalVariables(M, Vec, /*CompilerUsed=*/true); + Used = {Vec.begin(), Vec.end()}; #endif } diff --git a/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp b/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp --- a/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp +++ b/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp @@ -662,12 +662,12 @@ // promotion, but we may have opaque uses e.g. in inline asm. We collect them // here because we use this information to mark functions containing inline // assembly calls as not importable. - SmallPtrSet LocalsUsed; - SmallPtrSet Used; + SmallPtrSet LocalsUsed; + SmallVector Used; // First collect those in the llvm.used set. - collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false); + collectUsedGlobalVariables(M, Used, /*CompilerUsed=*/false); // Next collect those in the llvm.compiler.used set. - collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ true); + collectUsedGlobalVariables(M, Used, /*CompilerUsed=*/true); DenseSet CantBePromoted; for (auto *V : Used) { if (V->hasLocalLinkage()) { diff --git a/llvm/lib/IR/Module.cpp b/llvm/lib/IR/Module.cpp --- a/llvm/lib/IR/Module.cpp +++ b/llvm/lib/IR/Module.cpp @@ -673,21 +673,6 @@ return GV; } -GlobalVariable *llvm::collectUsedGlobalVariables( - const Module &M, SmallPtrSetImpl &Set, bool CompilerUsed) { - const char *Name = CompilerUsed ? "llvm.compiler.used" : "llvm.used"; - GlobalVariable *GV = M.getGlobalVariable(Name); - if (!GV || !GV->hasInitializer()) - return GV; - - const ConstantArray *Init = cast(GV->getInitializer()); - for (Value *Op : Init->operands()) { - GlobalValue *G = cast(Op->stripPointerCasts()); - Set.insert(G); - } - return GV; -} - void Module::setPartialSampleProfileRatio(const ModuleSummaryIndex &Index) { if (auto *SummaryMD = getProfileSummary(/*IsCS*/ false)) { std::unique_ptr ProfileSummary( diff --git a/llvm/lib/Object/IRSymtab.cpp b/llvm/lib/Object/IRSymtab.cpp --- a/llvm/lib/Object/IRSymtab.cpp +++ b/llvm/lib/Object/IRSymtab.cpp @@ -108,7 +108,7 @@ Error addModule(Module *M); Error addSymbol(const ModuleSymbolTable &Msymtab, - const SmallPtrSet &Used, + const SmallPtrSet &Used, ModuleSymbolTable::Symbol Sym); Error build(ArrayRef Mods); @@ -119,8 +119,9 @@ return make_error("input module has no datalayout", inconvertibleErrorCode()); - SmallPtrSet Used; - collectUsedGlobalVariables(*M, Used, /*CompilerUsed*/ false); + SmallVector UsedV; + collectUsedGlobalVariables(*M, UsedV, /*CompilerUsed=*/false); + SmallPtrSet Used(UsedV.begin(), UsedV.end()); ModuleSymbolTable Msymtab; Msymtab.addModule(M); @@ -193,7 +194,7 @@ } Error Builder::addSymbol(const ModuleSymbolTable &Msymtab, - const SmallPtrSet &Used, + const SmallPtrSet &Used, ModuleSymbolTable::Symbol Msym) { Syms.emplace_back(); storage::Symbol &Sym = Syms.back(); diff --git a/llvm/lib/Transforms/IPO/GlobalOpt.cpp b/llvm/lib/Transforms/IPO/GlobalOpt.cpp --- a/llvm/lib/Transforms/IPO/GlobalOpt.cpp +++ b/llvm/lib/Transforms/IPO/GlobalOpt.cpp @@ -2788,18 +2788,22 @@ /// An easy to access representation of llvm.used and llvm.compiler.used. class LLVMUsed { - SmallPtrSet Used; - SmallPtrSet CompilerUsed; + SmallPtrSet Used; + SmallPtrSet CompilerUsed; GlobalVariable *UsedV; GlobalVariable *CompilerUsedV; public: LLVMUsed(Module &M) { - UsedV = collectUsedGlobalVariables(M, Used, false); - CompilerUsedV = collectUsedGlobalVariables(M, CompilerUsed, true); + SmallVector Vec; + UsedV = collectUsedGlobalVariables(M, Vec, false); + Used = {Vec.begin(), Vec.end()}; + Vec.clear(); + CompilerUsedV = collectUsedGlobalVariables(M, Vec, true); + CompilerUsed = {Vec.begin(), Vec.end()}; } - using iterator = SmallPtrSet::iterator; + using iterator = SmallPtrSet::iterator; using used_iterator_range = iterator_range; iterator usedBegin() { return Used.begin(); } diff --git a/llvm/lib/Transforms/IPO/Internalize.cpp b/llvm/lib/Transforms/IPO/Internalize.cpp --- a/llvm/lib/Transforms/IPO/Internalize.cpp +++ b/llvm/lib/Transforms/IPO/Internalize.cpp @@ -151,7 +151,7 @@ bool Changed = false; CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : nullptr; - SmallPtrSet Used; + SmallVector Used; collectUsedGlobalVariables(M, Used, false); // Collect comdat visiblity information for the module.