CodegenCleanup.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //===- CodegenCleanup.cpp -------------------------------------------------===//
  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. #include "polly/CodeGen/CodegenCleanup.h"
  9. #include "llvm/Analysis/ScopedNoAliasAA.h"
  10. #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
  11. #include "llvm/IR/Function.h"
  12. #include "llvm/IR/LegacyPassManager.h"
  13. #include "llvm/Pass.h"
  14. #include "llvm/Support/Debug.h"
  15. #include "llvm/Transforms/InstCombine/InstCombine.h"
  16. #include "llvm/Transforms/Scalar.h"
  17. #include "llvm/Transforms/Scalar/GVN.h"
  18. #include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
  19. #include "llvm/Transforms/Utils.h"
  20. #define DEBUG_TYPE "polly-cleanup"
  21. using namespace llvm;
  22. using namespace polly;
  23. namespace {
  24. class CodegenCleanup final : public FunctionPass {
  25. private:
  26. CodegenCleanup(const CodegenCleanup &) = delete;
  27. const CodegenCleanup &operator=(const CodegenCleanup &) = delete;
  28. llvm::legacy::FunctionPassManager *FPM;
  29. public:
  30. static char ID;
  31. explicit CodegenCleanup() : FunctionPass(ID), FPM(nullptr) {}
  32. /// @name FunctionPass interface
  33. //@{
  34. void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {}
  35. bool doInitialization(Module &M) override {
  36. assert(!FPM);
  37. FPM = new llvm::legacy::FunctionPassManager(&M);
  38. // TODO: How to make parent passes discoverable?
  39. // TODO: Should be sensitive to compiler options in PassManagerBuilder, to
  40. // which we do not have access here.
  41. FPM->add(createScopedNoAliasAAWrapperPass());
  42. FPM->add(createTypeBasedAAWrapperPass());
  43. FPM->add(createAAResultsWrapperPass());
  44. // TODO: These are non-conditional passes that run between
  45. // EP_ModuleOptimizerEarly and EP_VectorizerStart just to ensure we do not
  46. // miss any optimization that would have run after Polly with
  47. // -polly-position=early. This can probably be reduced to a more compact set
  48. // of passes.
  49. FPM->add(createCFGSimplificationPass());
  50. FPM->add(createSROAPass());
  51. FPM->add(createEarlyCSEPass());
  52. FPM->add(createPromoteMemoryToRegisterPass());
  53. FPM->add(createInstructionCombiningPass(true));
  54. FPM->add(createCFGSimplificationPass());
  55. FPM->add(createSROAPass());
  56. FPM->add(createEarlyCSEPass(true));
  57. FPM->add(createSpeculativeExecutionIfHasBranchDivergencePass());
  58. FPM->add(createJumpThreadingPass());
  59. FPM->add(createCorrelatedValuePropagationPass());
  60. FPM->add(createCFGSimplificationPass());
  61. FPM->add(createInstructionCombiningPass(true));
  62. FPM->add(createLibCallsShrinkWrapPass());
  63. FPM->add(createTailCallEliminationPass());
  64. FPM->add(createCFGSimplificationPass());
  65. FPM->add(createReassociatePass());
  66. FPM->add(createLoopRotatePass(-1));
  67. FPM->add(createGVNPass());
  68. FPM->add(createLICMPass());
  69. FPM->add(createSimpleLoopUnswitchLegacyPass());
  70. FPM->add(createCFGSimplificationPass());
  71. FPM->add(createInstructionCombiningPass(true));
  72. FPM->add(createIndVarSimplifyPass());
  73. FPM->add(createLoopIdiomPass());
  74. FPM->add(createLoopDeletionPass());
  75. FPM->add(createCFGSimplificationPass());
  76. FPM->add(createSimpleLoopUnrollPass(3));
  77. FPM->add(createMergedLoadStoreMotionPass());
  78. FPM->add(createGVNPass());
  79. FPM->add(createMemCpyOptPass());
  80. FPM->add(createSCCPPass());
  81. FPM->add(createBitTrackingDCEPass());
  82. FPM->add(createInstructionCombiningPass(true));
  83. FPM->add(createJumpThreadingPass());
  84. FPM->add(createCorrelatedValuePropagationPass());
  85. FPM->add(createDeadStoreEliminationPass());
  86. FPM->add(createLICMPass());
  87. FPM->add(createAggressiveDCEPass());
  88. FPM->add(createCFGSimplificationPass());
  89. FPM->add(createInstructionCombiningPass(true));
  90. FPM->add(createFloat2IntPass());
  91. return FPM->doInitialization();
  92. }
  93. bool doFinalization(Module &M) override {
  94. bool Result = FPM->doFinalization();
  95. delete FPM;
  96. FPM = nullptr;
  97. return Result;
  98. }
  99. bool runOnFunction(llvm::Function &F) override {
  100. if (!F.hasFnAttribute("polly-optimized")) {
  101. LLVM_DEBUG(
  102. dbgs() << F.getName()
  103. << ": Skipping cleanup because Polly did not optimize it.");
  104. return false;
  105. }
  106. LLVM_DEBUG(dbgs() << F.getName() << ": Running codegen cleanup...");
  107. return FPM->run(F);
  108. }
  109. //@}
  110. };
  111. char CodegenCleanup::ID;
  112. } // namespace
  113. FunctionPass *polly::createCodegenCleanupPass() { return new CodegenCleanup(); }
  114. INITIALIZE_PASS_BEGIN(CodegenCleanup, "polly-cleanup",
  115. "Polly - Cleanup after code generation", false, false)
  116. INITIALIZE_PASS_END(CodegenCleanup, "polly-cleanup",
  117. "Polly - Cleanup after code generation", false, false)