From 130de7af7f41b561e9f63bf29b8d9628d3f86369 Mon Sep 17 00:00:00 2001 From: Teresa Johnson Date: Tue, 24 Nov 2015 19:55:04 +0000 Subject: [ThinLTO] Enable iterative importing in FunctionImport pass Analyze imported function bodies and add any new external calls to the worklist for importing. Currently no controls on the importing so this will end up importing everything possible in the call tree below the importing module. Basic profitability checks coming next. Update test to check for iteratively inlined functions. llvm-svn: 254011 --- llvm/lib/Transforms/IPO/FunctionImport.cpp | 38 ++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) (limited to 'llvm/lib/Transforms') diff --git a/llvm/lib/Transforms/IPO/FunctionImport.cpp b/llvm/lib/Transforms/IPO/FunctionImport.cpp index c874e4f5da7..bd6eb8391dc 100644 --- a/llvm/lib/Transforms/IPO/FunctionImport.cpp +++ b/llvm/lib/Transforms/IPO/FunctionImport.cpp @@ -131,10 +131,24 @@ bool FunctionImporter::importFunctions(Module &M) { // The function that we will import! GlobalValue *SGV = Module.getNamedValue(CalledFunctionName); + StringRef ImportFunctionName = CalledFunctionName; + if (!SGV) { + // Might be local in source Module, promoted/renamed in dest Module M. + std::pair Split = + CalledFunctionName.split(".llvm."); + SGV = Module.getNamedValue(Split.first); +#ifndef NDEBUG + // Assert that Split.second is module id + uint64_t ModuleId; + assert(!Split.second.getAsInteger(10, ModuleId)); + assert(ModuleId == Index.getModuleId(FileName)); +#endif + } Function *F = dyn_cast(SGV); if (!F && isa(SGV)) { auto *SGA = dyn_cast(SGV); F = dyn_cast(SGA->getBaseObject()); + ImportFunctionName = F->getName(); } if (!F) { errs() << "Can't load function '" << CalledFunctionName << "' in Module '" @@ -156,8 +170,28 @@ bool FunctionImporter::importFunctions(Module &M) { if (L.linkInModule(&Module, Linker::Flags::None, &Index, F)) report_fatal_error("Function Import: link error"); - // TODO: Process the newly imported function and add callees to the - // worklist. + // Process the newly imported function and add callees to the worklist. + GlobalValue *NewGV = M.getNamedValue(ImportFunctionName); + 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()); + } + } + } + } Changed = true; } -- cgit v1.2.3