Canonicalization.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //===---- Canonicalization.cpp - Run canonicalization passes --------------===//
  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. // Run the set of default canonicalization passes.
  10. //
  11. // This pass is mainly used for debugging.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "polly/Canonicalization.h"
  15. #include "polly/LinkAllPasses.h"
  16. #include "polly/Options.h"
  17. #include "llvm/Analysis/GlobalsModRef.h"
  18. #include "llvm/Analysis/ProfileSummaryInfo.h"
  19. #include "llvm/IR/LegacyPassManager.h"
  20. #include "llvm/Transforms/IPO.h"
  21. #include "llvm/Transforms/IPO/FunctionAttrs.h"
  22. #include "llvm/Transforms/InstCombine/InstCombine.h"
  23. #include "llvm/Transforms/Scalar.h"
  24. #include "llvm/Transforms/Scalar/EarlyCSE.h"
  25. #include "llvm/Transforms/Scalar/IndVarSimplify.h"
  26. #include "llvm/Transforms/Scalar/LoopRotation.h"
  27. #include "llvm/Transforms/Scalar/Reassociate.h"
  28. #include "llvm/Transforms/Scalar/SimplifyCFG.h"
  29. #include "llvm/Transforms/Scalar/TailRecursionElimination.h"
  30. #include "llvm/Transforms/Utils.h"
  31. #include "llvm/Transforms/Utils/Mem2Reg.h"
  32. using namespace llvm;
  33. using namespace polly;
  34. static cl::opt<bool>
  35. PollyInliner("polly-run-inliner",
  36. cl::desc("Run an early inliner pass before Polly"), cl::Hidden,
  37. cl::cat(PollyCategory));
  38. void polly::registerCanonicalicationPasses(llvm::legacy::PassManagerBase &PM) {
  39. bool UseMemSSA = true;
  40. PM.add(llvm::createPromoteMemoryToRegisterPass());
  41. PM.add(llvm::createEarlyCSEPass(UseMemSSA));
  42. PM.add(llvm::createInstructionCombiningPass());
  43. PM.add(llvm::createCFGSimplificationPass());
  44. PM.add(llvm::createTailCallEliminationPass());
  45. PM.add(llvm::createCFGSimplificationPass());
  46. PM.add(llvm::createReassociatePass());
  47. PM.add(llvm::createLoopRotatePass());
  48. if (PollyInliner) {
  49. PM.add(llvm::createFunctionInliningPass(200));
  50. PM.add(llvm::createPromoteMemoryToRegisterPass());
  51. PM.add(llvm::createCFGSimplificationPass());
  52. PM.add(llvm::createInstructionCombiningPass());
  53. PM.add(createBarrierNoopPass());
  54. }
  55. PM.add(llvm::createInstructionCombiningPass());
  56. PM.add(llvm::createIndVarSimplifyPass());
  57. }
  58. /// Adapted from llvm::PassBuilder::buildInlinerPipeline
  59. static ModuleInlinerWrapperPass
  60. buildInlinePasses(llvm::OptimizationLevel Level) {
  61. InlineParams IP = getInlineParams(200);
  62. ModuleInlinerWrapperPass MIWP(IP);
  63. // Require the GlobalsAA analysis for the module so we can query it within
  64. // the CGSCC pipeline.
  65. MIWP.addModulePass(RequireAnalysisPass<GlobalsAA, Module>());
  66. // Invalidate AAManager so it can be recreated and pick up the newly available
  67. // GlobalsAA.
  68. MIWP.addModulePass(
  69. createModuleToFunctionPassAdaptor(InvalidateAnalysisPass<AAManager>()));
  70. // Require the ProfileSummaryAnalysis for the module so we can query it within
  71. // the inliner pass.
  72. MIWP.addModulePass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
  73. // Now begin the main postorder CGSCC pipeline.
  74. // FIXME: The current CGSCC pipeline has its origins in the legacy pass
  75. // manager and trying to emulate its precise behavior. Much of this doesn't
  76. // make a lot of sense and we should revisit the core CGSCC structure.
  77. CGSCCPassManager &MainCGPipeline = MIWP.getPM();
  78. // Now deduce any function attributes based in the current code.
  79. MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
  80. return MIWP;
  81. }
  82. FunctionPassManager
  83. polly::buildCanonicalicationPassesForNPM(llvm::ModulePassManager &MPM,
  84. llvm::OptimizationLevel Level) {
  85. FunctionPassManager FPM;
  86. bool UseMemSSA = true;
  87. FPM.addPass(PromotePass());
  88. FPM.addPass(EarlyCSEPass(UseMemSSA));
  89. FPM.addPass(InstCombinePass());
  90. FPM.addPass(SimplifyCFGPass());
  91. FPM.addPass(TailCallElimPass());
  92. FPM.addPass(SimplifyCFGPass());
  93. FPM.addPass(ReassociatePass());
  94. {
  95. LoopPassManager LPM;
  96. LPM.addPass(LoopRotatePass(Level != OptimizationLevel::Oz));
  97. FPM.addPass(createFunctionToLoopPassAdaptor<LoopPassManager>(
  98. std::move(LPM), /*UseMemorySSA=*/false,
  99. /*UseBlockFrequencyInfo=*/false));
  100. }
  101. if (PollyInliner) {
  102. MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
  103. MPM.addPass(buildInlinePasses(Level));
  104. FPM = FunctionPassManager();
  105. FPM.addPass(PromotePass());
  106. FPM.addPass(SimplifyCFGPass());
  107. FPM.addPass(InstCombinePass());
  108. }
  109. FPM.addPass(InstCombinePass());
  110. {
  111. LoopPassManager LPM;
  112. LPM.addPass(IndVarSimplifyPass());
  113. FPM.addPass(createFunctionToLoopPassAdaptor<LoopPassManager>(
  114. std::move(LPM), /*UseMemorySSA=*/false,
  115. /*UseBlockFrequencyInfo=*/true));
  116. }
  117. return FPM;
  118. }
  119. namespace {
  120. class PollyCanonicalize final : public ModulePass {
  121. PollyCanonicalize(const PollyCanonicalize &) = delete;
  122. const PollyCanonicalize &operator=(const PollyCanonicalize &) = delete;
  123. public:
  124. static char ID;
  125. explicit PollyCanonicalize() : ModulePass(ID) {}
  126. ~PollyCanonicalize();
  127. /// @name FunctionPass interface.
  128. //@{
  129. void getAnalysisUsage(AnalysisUsage &AU) const override;
  130. void releaseMemory() override;
  131. bool runOnModule(Module &M) override;
  132. void print(raw_ostream &OS, const Module *) const override;
  133. //@}
  134. };
  135. } // namespace
  136. PollyCanonicalize::~PollyCanonicalize() {}
  137. void PollyCanonicalize::getAnalysisUsage(AnalysisUsage &AU) const {}
  138. void PollyCanonicalize::releaseMemory() {}
  139. bool PollyCanonicalize::runOnModule(Module &M) {
  140. legacy::PassManager PM;
  141. registerCanonicalicationPasses(PM);
  142. PM.run(M);
  143. return true;
  144. }
  145. void PollyCanonicalize::print(raw_ostream &OS, const Module *) const {}
  146. char PollyCanonicalize::ID = 0;
  147. Pass *polly::createPollyCanonicalizePass() { return new PollyCanonicalize(); }
  148. INITIALIZE_PASS_BEGIN(PollyCanonicalize, "polly-canonicalize",
  149. "Polly - Run canonicalization passes", false, false)
  150. INITIALIZE_PASS_END(PollyCanonicalize, "polly-canonicalize",
  151. "Polly - Run canonicalization passes", false, false)