| Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 1 | //===- LoopExtractor.cpp - Extract each loop into a new function ----------===// |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 6 | // |
| Chris Lattner | 692a47a | 2004-03-14 02:34:07 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
| Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 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 |
| Misha Brukman | f272f9b | 2004-03-02 00:19:09 +0000 | [diff] [blame] | 11 | // given function, it is not touched. This is a pass most useful for debugging |
| 12 | // via bugpoint. |
| Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Statistic.h" |
| Sergey Dmitriev | 807960e | 2019-02-08 06:55:18 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/AssumptionCache.h" |
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/LoopPass.h" |
| Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Dominators.h" |
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Instructions.h" |
| 21 | #include "llvm/IR/Module.h" |
| Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 22 | #include "llvm/Pass.h" |
| Nick Lewycky | c624302 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CommandLine.h" |
| Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 24 | #include "llvm/Transforms/IPO.h" |
| Chris Lattner | 6c3e8c7 | 2004-03-14 04:01:06 +0000 | [diff] [blame] | 25 | #include "llvm/Transforms/Scalar.h" |
| David Blaikie | a373d18 | 2018-03-28 17:44:36 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/Utils.h" |
| Bill Wendling | c1da6ea | 2011-09-20 19:10:24 +0000 | [diff] [blame] | 27 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 28 | #include "llvm/Transforms/Utils/CodeExtractor.h" |
| Nick Lewycky | c624302 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 29 | #include <fstream> |
| 30 | #include <set> |
| Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | |
| Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 33 | #define DEBUG_TYPE "loop-extract" |
| 34 | |
| Chris Lattner | 1631bcb | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 35 | STATISTIC(NumExtracted, "Number of loops extracted"); |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 36 | |
| Chris Lattner | 1631bcb | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 37 | namespace { |
| Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 38 | struct LoopExtractor : public LoopPass { |
| Nick Lewycky | e7da2d6 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 39 | static char ID; // Pass identification, replacement for typeid |
| Chris Lattner | a1672c1 | 2004-03-14 20:01:36 +0000 | [diff] [blame] | 40 | unsigned NumLoops; |
| 41 | |
| Justin Bogner | e9fb228d | 2016-01-08 19:08:53 +0000 | [diff] [blame] | 42 | explicit LoopExtractor(unsigned numLoops = ~0) |
| Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 43 | : LoopPass(ID), NumLoops(numLoops) { |
| 44 | initializeLoopExtractorPass(*PassRegistry::getPassRegistry()); |
| 45 | } |
| Chris Lattner | a1672c1 | 2004-03-14 20:01:36 +0000 | [diff] [blame] | 46 | |
| Justin Bogner | 883a3ea | 2015-12-16 18:40:20 +0000 | [diff] [blame] | 47 | bool runOnLoop(Loop *L, LPPassManager &) override; |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 48 | |
| Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 49 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| Chris Lattner | 5bce0c8 | 2004-03-18 05:43:18 +0000 | [diff] [blame] | 50 | AU.addRequiredID(BreakCriticalEdgesID); |
| 51 | AU.addRequiredID(LoopSimplifyID); |
| Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 52 | AU.addRequired<DominatorTreeWrapperPass>(); |
| Justin Bogner | 883a3ea | 2015-12-16 18:40:20 +0000 | [diff] [blame] | 53 | AU.addRequired<LoopInfoWrapperPass>(); |
| Sergey Dmitriev | 807960e | 2019-02-08 06:55:18 +0000 | [diff] [blame] | 54 | AU.addUsedIfAvailable<AssumptionCacheTracker>(); |
| Chris Lattner | 692a47a | 2004-03-14 02:34:07 +0000 | [diff] [blame] | 55 | } |
| 56 | }; |
| Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 57 | } |
| Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 58 | |
| Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 59 | char LoopExtractor::ID = 0; |
| Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 60 | INITIALIZE_PASS_BEGIN(LoopExtractor, "loop-extract", |
| Bill Wendling | c1da6ea | 2011-09-20 19:10:24 +0000 | [diff] [blame] | 61 | "Extract loops into new functions", false, false) |
| Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 62 | INITIALIZE_PASS_DEPENDENCY(BreakCriticalEdges) |
| 63 | INITIALIZE_PASS_DEPENDENCY(LoopSimplify) |
| Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 64 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 65 | INITIALIZE_PASS_END(LoopExtractor, "loop-extract", |
| Bill Wendling | c1da6ea | 2011-09-20 19:10:24 +0000 | [diff] [blame] | 66 | "Extract loops into new functions", false, false) |
| Chris Lattner | a1672c1 | 2004-03-14 20:01:36 +0000 | [diff] [blame] | 67 | |
| Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 68 | namespace { |
| Chris Lattner | a1672c1 | 2004-03-14 20:01:36 +0000 | [diff] [blame] | 69 | /// SingleLoopExtractor - For bugpoint. |
| 70 | struct SingleLoopExtractor : public LoopExtractor { |
| Nick Lewycky | e7da2d6 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 71 | static char ID; // Pass identification, replacement for typeid |
| Chris Lattner | a1672c1 | 2004-03-14 20:01:36 +0000 | [diff] [blame] | 72 | SingleLoopExtractor() : LoopExtractor(1) {} |
| 73 | }; |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 74 | } // End anonymous namespace |
| Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 75 | |
| Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 76 | char SingleLoopExtractor::ID = 0; |
| Owen Anderson | a57b97e | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 77 | INITIALIZE_PASS(SingleLoopExtractor, "loop-extract-single", |
| Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 78 | "Extract at most one loop into a new function", false, false) |
| Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 79 | |
| Jeff Cohen | 677babc | 2005-01-08 17:21:40 +0000 | [diff] [blame] | 80 | // createLoopExtractorPass - This pass extracts all natural loops from the |
| 81 | // program into a function if it can. |
| 82 | // |
| Dan Gohman | 9a7320c | 2009-09-28 14:37:51 +0000 | [diff] [blame] | 83 | Pass *llvm::createLoopExtractorPass() { return new LoopExtractor(); } |
| Jeff Cohen | 677babc | 2005-01-08 17:21:40 +0000 | [diff] [blame] | 84 | |
| Sanjoy Das | def1729 | 2017-09-28 02:45:42 +0000 | [diff] [blame] | 85 | bool LoopExtractor::runOnLoop(Loop *L, LPPassManager &LPM) { |
| Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 86 | if (skipLoop(L)) |
| Paul Robinson | af4e64d | 2014-02-06 00:07:05 +0000 | [diff] [blame] | 87 | return false; |
| 88 | |
| Dan Gohman | 9a7320c | 2009-09-28 14:37:51 +0000 | [diff] [blame] | 89 | // Only visit top-level loops. |
| 90 | if (L->getParentLoop()) |
| Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 91 | return false; |
| 92 | |
| Dan Gohman | a83ac2d | 2009-11-05 21:11:53 +0000 | [diff] [blame] | 93 | // If LoopSimplify form is not available, stay out of trouble. |
| 94 | if (!L->isLoopSimplifyForm()) |
| 95 | return false; |
| 96 | |
| Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 97 | DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| Justin Bogner | 883a3ea | 2015-12-16 18:40:20 +0000 | [diff] [blame] | 98 | LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| Dan Gohman | 9a7320c | 2009-09-28 14:37:51 +0000 | [diff] [blame] | 99 | bool Changed = false; |
| Chris Lattner | e9235d2 | 2004-03-18 03:48:06 +0000 | [diff] [blame] | 100 | |
| Chris Lattner | 2f155d8 | 2004-03-15 00:02:02 +0000 | [diff] [blame] | 101 | // If there is more than one top-level loop in this function, extract all of |
| Dan Gohman | 9a7320c | 2009-09-28 14:37:51 +0000 | [diff] [blame] | 102 | // the loops. Otherwise there is exactly one top-level loop; in this case if |
| 103 | // this function is more than a minimal wrapper around the loop, extract |
| 104 | // the loop. |
| 105 | bool ShouldExtractLoop = false; |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 106 | |
| Dan Gohman | 9a7320c | 2009-09-28 14:37:51 +0000 | [diff] [blame] | 107 | // Extract the loop if the entry block doesn't branch to the loop header. |
| Chandler Carruth | edb12a8 | 2018-10-15 10:04:59 +0000 | [diff] [blame] | 108 | Instruction *EntryTI = |
| 109 | L->getHeader()->getParent()->getEntryBlock().getTerminator(); |
| Dan Gohman | 9a7320c | 2009-09-28 14:37:51 +0000 | [diff] [blame] | 110 | if (!isa<BranchInst>(EntryTI) || |
| 111 | !cast<BranchInst>(EntryTI)->isUnconditional() || |
| Bill Wendling | 0058520 | 2011-09-20 22:23:09 +0000 | [diff] [blame] | 112 | EntryTI->getSuccessor(0) != L->getHeader()) { |
| Dan Gohman | 9a7320c | 2009-09-28 14:37:51 +0000 | [diff] [blame] | 113 | ShouldExtractLoop = true; |
| Bill Wendling | 0058520 | 2011-09-20 22:23:09 +0000 | [diff] [blame] | 114 | } else { |
| Dan Gohman | 9a7320c | 2009-09-28 14:37:51 +0000 | [diff] [blame] | 115 | // Check to see if any exits from the loop are more than just return |
| Bill Wendling | 04289fc | 2011-09-20 22:27:16 +0000 | [diff] [blame] | 116 | // blocks. |
| 117 | SmallVector<BasicBlock*, 8> ExitBlocks; |
| 118 | L->getExitBlocks(ExitBlocks); |
| 119 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) |
| 120 | if (!isa<ReturnInst>(ExitBlocks[i]->getTerminator())) { |
| 121 | ShouldExtractLoop = true; |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | if (ShouldExtractLoop) { |
| David Majnemer | eb518bd | 2015-08-04 08:21:40 +0000 | [diff] [blame] | 127 | // We must omit EH pads. EH pads must accompany the invoke |
| Bill Wendling | 04289fc | 2011-09-20 22:27:16 +0000 | [diff] [blame] | 128 | // instruction. But this would result in a loop in the extracted |
| Bill Wendling | 0058520 | 2011-09-20 22:23:09 +0000 | [diff] [blame] | 129 | // function. An infinite cycle occurs when it tries to extract that loop as |
| 130 | // well. |
| Dan Gohman | 9a7320c | 2009-09-28 14:37:51 +0000 | [diff] [blame] | 131 | SmallVector<BasicBlock*, 8> ExitBlocks; |
| 132 | L->getExitBlocks(ExitBlocks); |
| Bill Wendling | 04289fc | 2011-09-20 22:27:16 +0000 | [diff] [blame] | 133 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) |
| David Majnemer | eb518bd | 2015-08-04 08:21:40 +0000 | [diff] [blame] | 134 | if (ExitBlocks[i]->isEHPad()) { |
| Bill Wendling | 0058520 | 2011-09-20 22:23:09 +0000 | [diff] [blame] | 135 | ShouldExtractLoop = false; |
| Dan Gohman | 9a7320c | 2009-09-28 14:37:51 +0000 | [diff] [blame] | 136 | break; |
| Chris Lattner | 2f155d8 | 2004-03-15 00:02:02 +0000 | [diff] [blame] | 137 | } |
| Dan Gohman | 9a7320c | 2009-09-28 14:37:51 +0000 | [diff] [blame] | 138 | } |
| Bill Wendling | 04289fc | 2011-09-20 22:27:16 +0000 | [diff] [blame] | 139 | |
| Dan Gohman | 9a7320c | 2009-09-28 14:37:51 +0000 | [diff] [blame] | 140 | if (ShouldExtractLoop) { |
| 141 | if (NumLoops == 0) return Changed; |
| 142 | --NumLoops; |
| Sergey Dmitriev | 807960e | 2019-02-08 06:55:18 +0000 | [diff] [blame] | 143 | AssumptionCache *AC = nullptr; |
| 144 | if (auto *ACT = getAnalysisIfAvailable<AssumptionCacheTracker>()) |
| 145 | AC = ACT->lookupAssumptionCache(*L->getHeader()->getParent()); |
| 146 | CodeExtractor Extractor(DT, *L, false, nullptr, nullptr, AC); |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 147 | if (Extractor.extractCodeRegion() != nullptr) { |
| Dan Gohman | 9a7320c | 2009-09-28 14:37:51 +0000 | [diff] [blame] | 148 | Changed = true; |
| 149 | // After extraction, the loop is replaced by a function call, so |
| 150 | // we shouldn't try to run any more loop passes on it. |
| Sanjoy Das | def1729 | 2017-09-28 02:45:42 +0000 | [diff] [blame] | 151 | LPM.markLoopAsDeleted(*L); |
| Sanjoy Das | 388b012 | 2017-09-22 01:47:41 +0000 | [diff] [blame] | 152 | LI.erase(L); |
| Chris Lattner | 2f155d8 | 2004-03-15 00:02:02 +0000 | [diff] [blame] | 153 | } |
| Dan Gohman | 9a7320c | 2009-09-28 14:37:51 +0000 | [diff] [blame] | 154 | ++NumExtracted; |
| Chris Lattner | a1672c1 | 2004-03-14 20:01:36 +0000 | [diff] [blame] | 155 | } |
| Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 156 | |
| 157 | return Changed; |
| 158 | } |
| 159 | |
| Chris Lattner | a1672c1 | 2004-03-14 20:01:36 +0000 | [diff] [blame] | 160 | // createSingleLoopExtractorPass - This pass extracts one natural loop from the |
| 161 | // program into a function if it can. This is used by bugpoint. |
| 162 | // |
| Dan Gohman | 9a7320c | 2009-09-28 14:37:51 +0000 | [diff] [blame] | 163 | Pass *llvm::createSingleLoopExtractorPass() { |
| Chris Lattner | a1672c1 | 2004-03-14 20:01:36 +0000 | [diff] [blame] | 164 | return new SingleLoopExtractor(); |
| Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 165 | } |