From: Teresa Johnson Date: Tue, 24 Nov 2015 21:15:19 +0000 (+0000) Subject: [ThinLTO] Refactor function body scan during importing into helper (NFC) X-Git-Url: http://plrg.eecs.uci.edu/git/?p=oota-llvm.git;a=commitdiff_plain;h=c44b0f4b6b2d949caf02c3a00e26f4f7499ee2d7 [ThinLTO] Refactor function body scan during importing into helper (NFC) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@254020 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/IPO/FunctionImport.cpp b/lib/Transforms/IPO/FunctionImport.cpp index bd6eb8391dc..1f88da5e12a 100644 --- a/lib/Transforms/IPO/FunctionImport.cpp +++ b/lib/Transforms/IPO/FunctionImport.cpp @@ -53,6 +53,29 @@ Module &FunctionImporter::getOrLoadModule(StringRef FileName) { return *Module; } +/// Walk through the instructions in \p F looking for external +/// calls not already in the \p CalledFunctions set. If any are +/// found they are added to the \p Worklist for importing. +static void findExternalCalls(const Function &F, StringSet<> &CalledFunctions, + SmallVector &Worklist) { + for (auto &BB : F) { + for (auto &I : BB) { + if (isa(I)) { + DEBUG(dbgs() << "Found a call: '" << I << "'\n"); + auto CalledFunction = cast(I).getCalledFunction(); + // Insert any new external calls that have not already been + // added to set/worklist. + if (CalledFunction && CalledFunction->hasName() && + CalledFunction->isDeclaration() && + !CalledFunctions.count(CalledFunction->getName())) { + CalledFunctions.insert(CalledFunction->getName()); + Worklist.push_back(CalledFunction->getName()); + } + } + } + } +} + // Automatically import functions in Module \p M based on the summaries index. // // The current implementation imports every called functions that exists in the @@ -62,23 +85,13 @@ bool FunctionImporter::importFunctions(Module &M) { bool Changed = false; - /// First step is collecting the called functions and the one defined in this - /// module. + /// First step is collecting the called external functions. StringSet<> CalledFunctions; + SmallVector Worklist; for (auto &F : M) { if (F.isDeclaration() || F.hasFnAttribute(Attribute::OptimizeNone)) continue; - for (auto &BB : F) { - for (auto &I : BB) { - if (isa(I)) { - DEBUG(dbgs() << "Found a call: '" << I << "'\n"); - auto CalledFunction = cast(I).getCalledFunction(); - if (CalledFunction && CalledFunction->hasName() && - CalledFunction->isDeclaration()) - CalledFunctions.insert(CalledFunction->getName()); - } - } - } + findExternalCalls(F, CalledFunctions, Worklist); } /// Second step: for every call to an external function, try to import it. @@ -86,12 +99,6 @@ bool FunctionImporter::importFunctions(Module &M) { // Linker that will be used for importing function Linker L(&M, DiagnosticHandler); - /// Insert initial called function set in a worklist, so that we can add - /// transively called functions when importing. - SmallVector Worklist; - for (auto &CalledFunction : CalledFunctions) - Worklist.push_back(CalledFunction.first()); - while (!Worklist.empty()) { auto CalledFunctionName = Worklist.pop_back_val(); DEBUG(dbgs() << "Process import for " << CalledFunctionName << "\n"); @@ -175,23 +182,7 @@ bool FunctionImporter::importFunctions(Module &M) { assert(NewGV); Function *NewF = dyn_cast(NewGV); assert(NewF); - - for (auto &BB : *NewF) { - for (auto &I : BB) { - if (isa(I)) { - DEBUG(dbgs() << "Found a call: '" << I << "'\n"); - auto CalledFunction = cast(I).getCalledFunction(); - // Insert any new external calls that have not already been - // added to set/worklist. - if (CalledFunction && CalledFunction->hasName() && - CalledFunction->isDeclaration() && - !CalledFunctions.count(CalledFunction->getName())) { - CalledFunctions.insert(CalledFunction->getName()); - Worklist.push_back(CalledFunction->getName()); - } - } - } - } + findExternalCalls(*NewF, CalledFunctions, Worklist); Changed = true; }