InlineSimple.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //===- InlineSimple.cpp - Code to perform simple function inlining --------===//
  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 bottom-up inlining of functions into callees.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Analysis/AssumptionCache.h"
  13. #include "llvm/Analysis/InlineCost.h"
  14. #include "llvm/Analysis/OptimizationRemarkEmitter.h"
  15. #include "llvm/Analysis/TargetTransformInfo.h"
  16. #include "llvm/InitializePasses.h"
  17. #include "llvm/Transforms/IPO.h"
  18. #include "llvm/Transforms/IPO/Inliner.h"
  19. using namespace llvm;
  20. #define DEBUG_TYPE "inline"
  21. namespace {
  22. /// Actual inliner pass implementation.
  23. ///
  24. /// The common implementation of the inlining logic is shared between this
  25. /// inliner pass and the always inliner pass. The two passes use different cost
  26. /// analyses to determine when to inline.
  27. class SimpleInliner : public LegacyInlinerBase {
  28. InlineParams Params;
  29. public:
  30. SimpleInliner() : LegacyInlinerBase(ID), Params(llvm::getInlineParams()) {
  31. initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
  32. }
  33. explicit SimpleInliner(InlineParams Params)
  34. : LegacyInlinerBase(ID), Params(std::move(Params)) {
  35. initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
  36. }
  37. static char ID; // Pass identification, replacement for typeid
  38. InlineCost getInlineCost(CallBase &CB) override {
  39. Function *Callee = CB.getCalledFunction();
  40. TargetTransformInfo &TTI = TTIWP->getTTI(*Callee);
  41. bool RemarksEnabled = false;
  42. const auto &BBs = *CB.getCaller();
  43. if (!BBs.empty()) {
  44. auto DI = OptimizationRemark(DEBUG_TYPE, "", DebugLoc(), &BBs.front());
  45. if (DI.isEnabled())
  46. RemarksEnabled = true;
  47. }
  48. OptimizationRemarkEmitter ORE(CB.getCaller());
  49. std::function<AssumptionCache &(Function &)> GetAssumptionCache =
  50. [&](Function &F) -> AssumptionCache & {
  51. return ACT->getAssumptionCache(F);
  52. };
  53. return llvm::getInlineCost(CB, Params, TTI, GetAssumptionCache, GetTLI,
  54. /*GetBFI=*/nullptr, PSI,
  55. RemarksEnabled ? &ORE : nullptr);
  56. }
  57. bool runOnSCC(CallGraphSCC &SCC) override;
  58. void getAnalysisUsage(AnalysisUsage &AU) const override;
  59. private:
  60. TargetTransformInfoWrapperPass *TTIWP;
  61. };
  62. } // end anonymous namespace
  63. char SimpleInliner::ID = 0;
  64. INITIALIZE_PASS_BEGIN(SimpleInliner, "inline", "Function Integration/Inlining",
  65. false, false)
  66. INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
  67. INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
  68. INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
  69. INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
  70. INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
  71. INITIALIZE_PASS_END(SimpleInliner, "inline", "Function Integration/Inlining",
  72. false, false)
  73. Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); }
  74. Pass *llvm::createFunctionInliningPass(int Threshold) {
  75. return new SimpleInliner(llvm::getInlineParams(Threshold));
  76. }
  77. Pass *llvm::createFunctionInliningPass(unsigned OptLevel,
  78. unsigned SizeOptLevel,
  79. bool DisableInlineHotCallSite) {
  80. auto Param = llvm::getInlineParams(OptLevel, SizeOptLevel);
  81. if (DisableInlineHotCallSite)
  82. Param.HotCallSiteThreshold = 0;
  83. return new SimpleInliner(Param);
  84. }
  85. Pass *llvm::createFunctionInliningPass(InlineParams &Params) {
  86. return new SimpleInliner(Params);
  87. }
  88. bool SimpleInliner::runOnSCC(CallGraphSCC &SCC) {
  89. TTIWP = &getAnalysis<TargetTransformInfoWrapperPass>();
  90. return LegacyInlinerBase::runOnSCC(SCC);
  91. }
  92. void SimpleInliner::getAnalysisUsage(AnalysisUsage &AU) const {
  93. AU.addRequired<TargetTransformInfoWrapperPass>();
  94. LegacyInlinerBase::getAnalysisUsage(AU);
  95. }