InlineSimple.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //===- InlineSimple.cpp - Code to perform simple function inlining --------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements bottom-up inlining of functions into callees.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Transforms/IPO.h"
  14. #include "llvm/Analysis/AliasAnalysis.h"
  15. #include "llvm/Analysis/AssumptionCache.h"
  16. #include "llvm/Analysis/CallGraph.h"
  17. #include "llvm/Analysis/InlineCost.h"
  18. #include "llvm/IR/CallSite.h"
  19. #include "llvm/IR/CallingConv.h"
  20. #include "llvm/IR/DataLayout.h"
  21. #include "llvm/IR/Instructions.h"
  22. #include "llvm/IR/IntrinsicInst.h"
  23. #include "llvm/IR/Module.h"
  24. #include "llvm/IR/Type.h"
  25. #include "llvm/Transforms/IPO/InlinerPass.h"
  26. #include "llvm/Support/raw_ostream.h"
  27. using namespace llvm;
  28. #define DEBUG_TYPE "inline"
  29. namespace {
  30. /// \brief Actual inliner pass implementation.
  31. ///
  32. /// The common implementation of the inlining logic is shared between this
  33. /// inliner pass and the always inliner pass. The two passes use different cost
  34. /// analyses to determine when to inline.
  35. class SimpleInliner : public Inliner {
  36. InlineCostAnalysis *ICA;
  37. public:
  38. SimpleInliner() : Inliner(ID), ICA(nullptr) {
  39. initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
  40. }
  41. SimpleInliner(int Threshold)
  42. : Inliner(ID, Threshold, /*InsertLifetime*/ true), ICA(nullptr) {
  43. initializeSimpleInlinerPass(*PassRegistry::getPassRegistry());
  44. }
  45. static char ID; // Pass identification, replacement for typeid
  46. InlineCost getInlineCost(CallSite CS) override {
  47. return ICA->getInlineCost(CS, getInlineThreshold(CS));
  48. }
  49. bool runOnSCC(CallGraphSCC &SCC) override;
  50. void getAnalysisUsage(AnalysisUsage &AU) const override;
  51. };
  52. static int computeThresholdFromOptLevels(unsigned OptLevel,
  53. unsigned SizeOptLevel) {
  54. if (OptLevel > 2)
  55. return 275;
  56. if (SizeOptLevel == 1) // -Os
  57. return 75;
  58. if (SizeOptLevel == 2) // -Oz
  59. return 25;
  60. return 225;
  61. }
  62. } // end anonymous namespace
  63. char SimpleInliner::ID = 0;
  64. INITIALIZE_PASS_BEGIN(SimpleInliner, "inline",
  65. "Function Integration/Inlining", false, false)
  66. INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
  67. INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
  68. INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
  69. INITIALIZE_PASS_DEPENDENCY(InlineCostAnalysis)
  70. INITIALIZE_PASS_END(SimpleInliner, "inline",
  71. "Function Integration/Inlining", false, false)
  72. Pass *llvm::createFunctionInliningPass() { return new SimpleInliner(); }
  73. Pass *llvm::createFunctionInliningPass(int Threshold) {
  74. return new SimpleInliner(Threshold);
  75. }
  76. Pass *llvm::createFunctionInliningPass(unsigned OptLevel,
  77. unsigned SizeOptLevel) {
  78. return new SimpleInliner(
  79. computeThresholdFromOptLevels(OptLevel, SizeOptLevel));
  80. }
  81. bool SimpleInliner::runOnSCC(CallGraphSCC &SCC) {
  82. ICA = &getAnalysis<InlineCostAnalysis>();
  83. return Inliner::runOnSCC(SCC);
  84. }
  85. void SimpleInliner::getAnalysisUsage(AnalysisUsage &AU) const {
  86. AU.addRequired<InlineCostAnalysis>();
  87. Inliner::getAnalysisUsage(AU);
  88. }
  89. // HLSL Change Starts
  90. void Inliner::applyOptions(PassOptions O) {
  91. GetPassOptionUnsigned(O, "InlineThreshold", &InlineThreshold, InlineThreshold);
  92. GetPassOptionBool(O, "InsertLifetime", &InsertLifetime, InsertLifetime);
  93. }
  94. void Inliner::dumpConfig(raw_ostream &OS) {
  95. CallGraphSCCPass::dumpConfig(OS);
  96. OS << ",InlineThreshold=" << InlineThreshold;
  97. OS << ",InsertLifetime=" << (InsertLifetime ? 't' : 'f');
  98. }
  99. // HLSL Change Ends