InlineSimple.cpp 4.2 KB

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