CodePreparation.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //===---- CodePreparation.cpp - Code preparation for Scop Detection -------===//
  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. // The Polly code preparation pass is executed before SCoP detection. Its
  10. // currently only splits the entry block of the SCoP to make room for alloc
  11. // instructions as they are generated during code generation.
  12. //
  13. // XXX: In the future, we should remove the need for this pass entirely and
  14. // instead add this spitting to the code generation pass.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #include "polly/CodePreparation.h"
  18. #include "polly/LinkAllPasses.h"
  19. #include "polly/Support/ScopHelper.h"
  20. #include "llvm/Analysis/DominanceFrontier.h"
  21. #include "llvm/Analysis/LoopInfo.h"
  22. #include "llvm/Analysis/RegionInfo.h"
  23. #include "llvm/Analysis/ScalarEvolution.h"
  24. #include "llvm/InitializePasses.h"
  25. using namespace llvm;
  26. using namespace polly;
  27. namespace {
  28. /// Prepare the IR for the scop detection.
  29. ///
  30. class CodePreparation final : public FunctionPass {
  31. CodePreparation(const CodePreparation &) = delete;
  32. const CodePreparation &operator=(const CodePreparation &) = delete;
  33. LoopInfo *LI;
  34. ScalarEvolution *SE;
  35. void clear();
  36. public:
  37. static char ID;
  38. explicit CodePreparation() : FunctionPass(ID) {}
  39. ~CodePreparation();
  40. /// @name FunctionPass interface.
  41. //@{
  42. void getAnalysisUsage(AnalysisUsage &AU) const override;
  43. void releaseMemory() override;
  44. bool runOnFunction(Function &F) override;
  45. void print(raw_ostream &OS, const Module *) const override;
  46. //@}
  47. };
  48. } // namespace
  49. PreservedAnalyses CodePreparationPass::run(Function &F,
  50. FunctionAnalysisManager &FAM) {
  51. // Find first non-alloca instruction. Every basic block has a non-alloca
  52. // instruction, as every well formed basic block has a terminator.
  53. auto &EntryBlock = F.getEntryBlock();
  54. BasicBlock::iterator I = EntryBlock.begin();
  55. while (isa<AllocaInst>(I))
  56. ++I;
  57. auto &DT = FAM.getResult<DominatorTreeAnalysis>(F);
  58. auto &LI = FAM.getResult<LoopAnalysis>(F);
  59. // splitBlock updates DT, LI and RI.
  60. splitEntryBlockForAlloca(&EntryBlock, &DT, &LI, nullptr);
  61. PreservedAnalyses PA;
  62. PA.preserve<DominatorTreeAnalysis>();
  63. PA.preserve<LoopAnalysis>();
  64. return PA;
  65. }
  66. void CodePreparation::clear() {}
  67. CodePreparation::~CodePreparation() { clear(); }
  68. void CodePreparation::getAnalysisUsage(AnalysisUsage &AU) const {
  69. AU.addRequired<LoopInfoWrapperPass>();
  70. AU.addRequired<ScalarEvolutionWrapperPass>();
  71. AU.addPreserved<LoopInfoWrapperPass>();
  72. AU.addPreserved<RegionInfoPass>();
  73. AU.addPreserved<DominatorTreeWrapperPass>();
  74. AU.addPreserved<DominanceFrontierWrapperPass>();
  75. }
  76. bool CodePreparation::runOnFunction(Function &F) {
  77. if (skipFunction(F))
  78. return false;
  79. LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
  80. SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
  81. splitEntryBlockForAlloca(&F.getEntryBlock(), this);
  82. return true;
  83. }
  84. void CodePreparation::releaseMemory() { clear(); }
  85. void CodePreparation::print(raw_ostream &OS, const Module *) const {}
  86. char CodePreparation::ID = 0;
  87. char &polly::CodePreparationID = CodePreparation::ID;
  88. Pass *polly::createCodePreparationPass() { return new CodePreparation(); }
  89. INITIALIZE_PASS_BEGIN(CodePreparation, "polly-prepare",
  90. "Polly - Prepare code for polly", false, false)
  91. INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
  92. INITIALIZE_PASS_END(CodePreparation, "polly-prepare",
  93. "Polly - Prepare code for polly", false, false)