ModuleInliner.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. //===- ModuleInliner.cpp - Code related to module inliner -----------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the mechanics required to implement inlining without
  10. // missing any calls in the module level. It doesn't need any infromation about
  11. // SCC or call graph, which is different from the SCC inliner. The decisions of
  12. // which calls are profitable to inline are implemented elsewhere.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "llvm/Transforms/IPO/ModuleInliner.h"
  16. #include "llvm/ADT/ScopeExit.h"
  17. #include "llvm/ADT/SmallVector.h"
  18. #include "llvm/ADT/Statistic.h"
  19. #include "llvm/Analysis/AliasAnalysis.h"
  20. #include "llvm/Analysis/AssumptionCache.h"
  21. #include "llvm/Analysis/BlockFrequencyInfo.h"
  22. #include "llvm/Analysis/InlineAdvisor.h"
  23. #include "llvm/Analysis/InlineCost.h"
  24. #include "llvm/Analysis/InlineOrder.h"
  25. #include "llvm/Analysis/OptimizationRemarkEmitter.h"
  26. #include "llvm/Analysis/ProfileSummaryInfo.h"
  27. #include "llvm/Analysis/ReplayInlineAdvisor.h"
  28. #include "llvm/Analysis/TargetLibraryInfo.h"
  29. #include "llvm/IR/DiagnosticInfo.h"
  30. #include "llvm/IR/Function.h"
  31. #include "llvm/IR/InstIterator.h"
  32. #include "llvm/IR/Instruction.h"
  33. #include "llvm/IR/IntrinsicInst.h"
  34. #include "llvm/IR/Module.h"
  35. #include "llvm/IR/PassManager.h"
  36. #include "llvm/Support/CommandLine.h"
  37. #include "llvm/Support/Debug.h"
  38. #include "llvm/Support/raw_ostream.h"
  39. #include "llvm/Transforms/Utils/CallPromotionUtils.h"
  40. #include "llvm/Transforms/Utils/Cloning.h"
  41. #include <cassert>
  42. using namespace llvm;
  43. #define DEBUG_TYPE "module-inline"
  44. STATISTIC(NumInlined, "Number of functions inlined");
  45. STATISTIC(NumDeleted, "Number of functions deleted because all callers found");
  46. /// Return true if the specified inline history ID
  47. /// indicates an inline history that includes the specified function.
  48. static bool inlineHistoryIncludes(
  49. Function *F, int InlineHistoryID,
  50. const SmallVectorImpl<std::pair<Function *, int>> &InlineHistory) {
  51. while (InlineHistoryID != -1) {
  52. assert(unsigned(InlineHistoryID) < InlineHistory.size() &&
  53. "Invalid inline history ID");
  54. if (InlineHistory[InlineHistoryID].first == F)
  55. return true;
  56. InlineHistoryID = InlineHistory[InlineHistoryID].second;
  57. }
  58. return false;
  59. }
  60. InlineAdvisor &ModuleInlinerPass::getAdvisor(const ModuleAnalysisManager &MAM,
  61. FunctionAnalysisManager &FAM,
  62. Module &M) {
  63. if (OwnedAdvisor)
  64. return *OwnedAdvisor;
  65. auto *IAA = MAM.getCachedResult<InlineAdvisorAnalysis>(M);
  66. if (!IAA) {
  67. // It should still be possible to run the inliner as a stand-alone module
  68. // pass, for test scenarios. In that case, we default to the
  69. // DefaultInlineAdvisor, which doesn't need to keep state between module
  70. // pass runs. It also uses just the default InlineParams. In this case, we
  71. // need to use the provided FAM, which is valid for the duration of the
  72. // inliner pass, and thus the lifetime of the owned advisor. The one we
  73. // would get from the MAM can be invalidated as a result of the inliner's
  74. // activity.
  75. OwnedAdvisor = std::make_unique<DefaultInlineAdvisor>(
  76. M, FAM, Params, InlineContext{LTOPhase, InlinePass::ModuleInliner});
  77. return *OwnedAdvisor;
  78. }
  79. assert(IAA->getAdvisor() &&
  80. "Expected a present InlineAdvisorAnalysis also have an "
  81. "InlineAdvisor initialized");
  82. return *IAA->getAdvisor();
  83. }
  84. static bool isKnownLibFunction(Function &F, TargetLibraryInfo &TLI) {
  85. LibFunc LF;
  86. // Either this is a normal library function or a "vectorizable"
  87. // function. Not using the VFDatabase here because this query
  88. // is related only to libraries handled via the TLI.
  89. return TLI.getLibFunc(F, LF) ||
  90. TLI.isKnownVectorFunctionInLibrary(F.getName());
  91. }
  92. PreservedAnalyses ModuleInlinerPass::run(Module &M,
  93. ModuleAnalysisManager &MAM) {
  94. LLVM_DEBUG(dbgs() << "---- Module Inliner is Running ---- \n");
  95. auto &IAA = MAM.getResult<InlineAdvisorAnalysis>(M);
  96. if (!IAA.tryCreate(Params, Mode, {},
  97. InlineContext{LTOPhase, InlinePass::ModuleInliner})) {
  98. M.getContext().emitError(
  99. "Could not setup Inlining Advisor for the requested "
  100. "mode and/or options");
  101. return PreservedAnalyses::all();
  102. }
  103. bool Changed = false;
  104. ProfileSummaryInfo *PSI = MAM.getCachedResult<ProfileSummaryAnalysis>(M);
  105. FunctionAnalysisManager &FAM =
  106. MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
  107. auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
  108. return FAM.getResult<TargetLibraryAnalysis>(F);
  109. };
  110. InlineAdvisor &Advisor = getAdvisor(MAM, FAM, M);
  111. Advisor.onPassEntry();
  112. auto AdvisorOnExit = make_scope_exit([&] { Advisor.onPassExit(); });
  113. // In the module inliner, a priority-based worklist is used for calls across
  114. // the entire Module. With this module inliner, the inline order is not
  115. // limited to bottom-up order. More globally scope inline order is enabled.
  116. // Also, the inline deferral logic become unnecessary in this module inliner.
  117. // It is possible to use other priority heuristics, e.g. profile-based
  118. // heuristic.
  119. //
  120. // TODO: Here is a huge amount duplicate code between the module inliner and
  121. // the SCC inliner, which need some refactoring.
  122. auto Calls = getInlineOrder(FAM, Params);
  123. assert(Calls != nullptr && "Expected an initialized InlineOrder");
  124. // Populate the initial list of calls in this module.
  125. for (Function &F : M) {
  126. auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(F);
  127. // We want to generally process call sites top-down in order for
  128. // simplifications stemming from replacing the call with the returned value
  129. // after inlining to be visible to subsequent inlining decisions.
  130. // FIXME: Using instructions sequence is a really bad way to do this.
  131. // Instead we should do an actual RPO walk of the function body.
  132. for (Instruction &I : instructions(F))
  133. if (auto *CB = dyn_cast<CallBase>(&I))
  134. if (Function *Callee = CB->getCalledFunction()) {
  135. if (!Callee->isDeclaration())
  136. Calls->push({CB, -1});
  137. else if (!isa<IntrinsicInst>(I)) {
  138. using namespace ore;
  139. setInlineRemark(*CB, "unavailable definition");
  140. ORE.emit([&]() {
  141. return OptimizationRemarkMissed(DEBUG_TYPE, "NoDefinition", &I)
  142. << NV("Callee", Callee) << " will not be inlined into "
  143. << NV("Caller", CB->getCaller())
  144. << " because its definition is unavailable"
  145. << setIsVerbose();
  146. });
  147. }
  148. }
  149. }
  150. if (Calls->empty())
  151. return PreservedAnalyses::all();
  152. // When inlining a callee produces new call sites, we want to keep track of
  153. // the fact that they were inlined from the callee. This allows us to avoid
  154. // infinite inlining in some obscure cases. To represent this, we use an
  155. // index into the InlineHistory vector.
  156. SmallVector<std::pair<Function *, int>, 16> InlineHistory;
  157. // Track the dead functions to delete once finished with inlining calls. We
  158. // defer deleting these to make it easier to handle the call graph updates.
  159. SmallVector<Function *, 4> DeadFunctions;
  160. // Loop forward over all of the calls.
  161. while (!Calls->empty()) {
  162. auto P = Calls->pop();
  163. CallBase *CB = P.first;
  164. const int InlineHistoryID = P.second;
  165. Function &F = *CB->getCaller();
  166. Function &Callee = *CB->getCalledFunction();
  167. LLVM_DEBUG(dbgs() << "Inlining calls in: " << F.getName() << "\n"
  168. << " Function size: " << F.getInstructionCount()
  169. << "\n");
  170. (void)F;
  171. auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & {
  172. return FAM.getResult<AssumptionAnalysis>(F);
  173. };
  174. if (InlineHistoryID != -1 &&
  175. inlineHistoryIncludes(&Callee, InlineHistoryID, InlineHistory)) {
  176. setInlineRemark(*CB, "recursive");
  177. continue;
  178. }
  179. auto Advice = Advisor.getAdvice(*CB, /*OnlyMandatory*/ false);
  180. // Check whether we want to inline this callsite.
  181. if (!Advice->isInliningRecommended()) {
  182. Advice->recordUnattemptedInlining();
  183. continue;
  184. }
  185. // Setup the data structure used to plumb customization into the
  186. // `InlineFunction` routine.
  187. InlineFunctionInfo IFI(
  188. /*cg=*/nullptr, GetAssumptionCache, PSI,
  189. &FAM.getResult<BlockFrequencyAnalysis>(*(CB->getCaller())),
  190. &FAM.getResult<BlockFrequencyAnalysis>(Callee));
  191. InlineResult IR =
  192. InlineFunction(*CB, IFI, /*MergeAttributes=*/true,
  193. &FAM.getResult<AAManager>(*CB->getCaller()));
  194. if (!IR.isSuccess()) {
  195. Advice->recordUnsuccessfulInlining(IR);
  196. continue;
  197. }
  198. Changed = true;
  199. ++NumInlined;
  200. LLVM_DEBUG(dbgs() << " Size after inlining: " << F.getInstructionCount()
  201. << "\n");
  202. // Add any new callsites to defined functions to the worklist.
  203. if (!IFI.InlinedCallSites.empty()) {
  204. int NewHistoryID = InlineHistory.size();
  205. InlineHistory.push_back({&Callee, InlineHistoryID});
  206. for (CallBase *ICB : reverse(IFI.InlinedCallSites)) {
  207. Function *NewCallee = ICB->getCalledFunction();
  208. if (!NewCallee) {
  209. // Try to promote an indirect (virtual) call without waiting for
  210. // the post-inline cleanup and the next DevirtSCCRepeatedPass
  211. // iteration because the next iteration may not happen and we may
  212. // miss inlining it.
  213. if (tryPromoteCall(*ICB))
  214. NewCallee = ICB->getCalledFunction();
  215. }
  216. if (NewCallee)
  217. if (!NewCallee->isDeclaration())
  218. Calls->push({ICB, NewHistoryID});
  219. }
  220. }
  221. // For local functions, check whether this makes the callee trivially
  222. // dead. In that case, we can drop the body of the function eagerly
  223. // which may reduce the number of callers of other functions to one,
  224. // changing inline cost thresholds.
  225. bool CalleeWasDeleted = false;
  226. if (Callee.hasLocalLinkage()) {
  227. // To check this we also need to nuke any dead constant uses (perhaps
  228. // made dead by this operation on other functions).
  229. Callee.removeDeadConstantUsers();
  230. // if (Callee.use_empty() && !CG.isLibFunction(Callee)) {
  231. if (Callee.use_empty() && !isKnownLibFunction(Callee, GetTLI(Callee))) {
  232. Calls->erase_if([&](const std::pair<CallBase *, int> &Call) {
  233. return Call.first->getCaller() == &Callee;
  234. });
  235. // Clear the body and queue the function itself for deletion when we
  236. // finish inlining.
  237. // Note that after this point, it is an error to do anything other
  238. // than use the callee's address or delete it.
  239. Callee.dropAllReferences();
  240. assert(!is_contained(DeadFunctions, &Callee) &&
  241. "Cannot put cause a function to become dead twice!");
  242. DeadFunctions.push_back(&Callee);
  243. CalleeWasDeleted = true;
  244. }
  245. }
  246. if (CalleeWasDeleted)
  247. Advice->recordInliningWithCalleeDeleted();
  248. else
  249. Advice->recordInlining();
  250. }
  251. // Now that we've finished inlining all of the calls across this module,
  252. // delete all of the trivially dead functions.
  253. //
  254. // Note that this walks a pointer set which has non-deterministic order but
  255. // that is OK as all we do is delete things and add pointers to unordered
  256. // sets.
  257. for (Function *DeadF : DeadFunctions) {
  258. // Clear out any cached analyses.
  259. FAM.clear(*DeadF, DeadF->getName());
  260. // And delete the actual function from the module.
  261. M.getFunctionList().erase(DeadF);
  262. ++NumDeleted;
  263. }
  264. if (!Changed)
  265. return PreservedAnalyses::all();
  266. return PreservedAnalyses::none();
  267. }