LoopExtractor.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //===- LoopExtractor.cpp - Extract each loop into a new function ----------===//
  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. // A pass wrapper around the ExtractLoop() scalar transformation to extract each
  10. // top-level loop into its own new function. If the loop is the ONLY loop in a
  11. // given function, it is not touched. This is a pass most useful for debugging
  12. // via bugpoint.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "llvm/ADT/Statistic.h"
  16. #include "llvm/Analysis/AssumptionCache.h"
  17. #include "llvm/Analysis/LoopPass.h"
  18. #include "llvm/IR/Dominators.h"
  19. #include "llvm/IR/Instructions.h"
  20. #include "llvm/IR/Module.h"
  21. #include "llvm/Pass.h"
  22. #include "llvm/Support/CommandLine.h"
  23. #include "llvm/Transforms/IPO.h"
  24. #include "llvm/Transforms/Scalar.h"
  25. #include "llvm/Transforms/Utils.h"
  26. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  27. #include "llvm/Transforms/Utils/CodeExtractor.h"
  28. #include <fstream>
  29. #include <set>
  30. using namespace llvm;
  31. #define DEBUG_TYPE "loop-extract"
  32. STATISTIC(NumExtracted, "Number of loops extracted");
  33. namespace {
  34. struct LoopExtractor : public LoopPass {
  35. static char ID; // Pass identification, replacement for typeid
  36. unsigned NumLoops;
  37. explicit LoopExtractor(unsigned numLoops = ~0)
  38. : LoopPass(ID), NumLoops(numLoops) {
  39. initializeLoopExtractorPass(*PassRegistry::getPassRegistry());
  40. }
  41. bool runOnLoop(Loop *L, LPPassManager &) override;
  42. void getAnalysisUsage(AnalysisUsage &AU) const override {
  43. AU.addRequiredID(BreakCriticalEdgesID);
  44. AU.addRequiredID(LoopSimplifyID);
  45. AU.addRequired<DominatorTreeWrapperPass>();
  46. AU.addRequired<LoopInfoWrapperPass>();
  47. AU.addUsedIfAvailable<AssumptionCacheTracker>();
  48. }
  49. };
  50. }
  51. char LoopExtractor::ID = 0;
  52. INITIALIZE_PASS_BEGIN(LoopExtractor, "loop-extract",
  53. "Extract loops into new functions", false, false)
  54. INITIALIZE_PASS_DEPENDENCY(BreakCriticalEdges)
  55. INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
  56. INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
  57. INITIALIZE_PASS_END(LoopExtractor, "loop-extract",
  58. "Extract loops into new functions", false, false)
  59. namespace {
  60. /// SingleLoopExtractor - For bugpoint.
  61. struct SingleLoopExtractor : public LoopExtractor {
  62. static char ID; // Pass identification, replacement for typeid
  63. SingleLoopExtractor() : LoopExtractor(1) {}
  64. };
  65. } // End anonymous namespace
  66. char SingleLoopExtractor::ID = 0;
  67. INITIALIZE_PASS(SingleLoopExtractor, "loop-extract-single",
  68. "Extract at most one loop into a new function", false, false)
  69. // createLoopExtractorPass - This pass extracts all natural loops from the
  70. // program into a function if it can.
  71. //
  72. Pass *llvm::createLoopExtractorPass() { return new LoopExtractor(); }
  73. bool LoopExtractor::runOnLoop(Loop *L, LPPassManager &LPM) {
  74. if (skipLoop(L))
  75. return false;
  76. // Only visit top-level loops.
  77. if (L->getParentLoop())
  78. return false;
  79. // If LoopSimplify form is not available, stay out of trouble.
  80. if (!L->isLoopSimplifyForm())
  81. return false;
  82. DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  83. LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
  84. bool Changed = false;
  85. // If there is more than one top-level loop in this function, extract all of
  86. // the loops. Otherwise there is exactly one top-level loop; in this case if
  87. // this function is more than a minimal wrapper around the loop, extract
  88. // the loop.
  89. bool ShouldExtractLoop = false;
  90. // Extract the loop if the entry block doesn't branch to the loop header.
  91. Instruction *EntryTI =
  92. L->getHeader()->getParent()->getEntryBlock().getTerminator();
  93. if (!isa<BranchInst>(EntryTI) ||
  94. !cast<BranchInst>(EntryTI)->isUnconditional() ||
  95. EntryTI->getSuccessor(0) != L->getHeader()) {
  96. ShouldExtractLoop = true;
  97. } else {
  98. // Check to see if any exits from the loop are more than just return
  99. // blocks.
  100. SmallVector<BasicBlock*, 8> ExitBlocks;
  101. L->getExitBlocks(ExitBlocks);
  102. for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
  103. if (!isa<ReturnInst>(ExitBlocks[i]->getTerminator())) {
  104. ShouldExtractLoop = true;
  105. break;
  106. }
  107. }
  108. if (ShouldExtractLoop) {
  109. // We must omit EH pads. EH pads must accompany the invoke
  110. // instruction. But this would result in a loop in the extracted
  111. // function. An infinite cycle occurs when it tries to extract that loop as
  112. // well.
  113. SmallVector<BasicBlock*, 8> ExitBlocks;
  114. L->getExitBlocks(ExitBlocks);
  115. for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
  116. if (ExitBlocks[i]->isEHPad()) {
  117. ShouldExtractLoop = false;
  118. break;
  119. }
  120. }
  121. if (ShouldExtractLoop) {
  122. if (NumLoops == 0) return Changed;
  123. --NumLoops;
  124. AssumptionCache *AC = nullptr;
  125. Function &Func = *L->getHeader()->getParent();
  126. if (auto *ACT = getAnalysisIfAvailable<AssumptionCacheTracker>())
  127. AC = ACT->lookupAssumptionCache(Func);
  128. CodeExtractorAnalysisCache CEAC(Func);
  129. CodeExtractor Extractor(DT, *L, false, nullptr, nullptr, AC);
  130. if (Extractor.extractCodeRegion(CEAC) != nullptr) {
  131. Changed = true;
  132. // After extraction, the loop is replaced by a function call, so
  133. // we shouldn't try to run any more loop passes on it.
  134. LPM.markLoopAsDeleted(*L);
  135. LI.erase(L);
  136. }
  137. ++NumExtracted;
  138. }
  139. return Changed;
  140. }
  141. // createSingleLoopExtractorPass - This pass extracts one natural loop from the
  142. // program into a function if it can. This is used by bugpoint.
  143. //
  144. Pass *llvm::createSingleLoopExtractorPass() {
  145. return new SingleLoopExtractor();
  146. }