CodegenCleanup.cpp 4.7 KB

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