blob: 91c7b5f5f135db6d674933023ad236a31099db15 [file] [log] [blame]
Misha Brukman03a11342004-02-28 03:33:01 +00001//===- LoopExtractor.cpp - Extract each loop into a new function ----------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Brukmanb1c93172005-04-21 23:48:37 +00006//
Chris Lattner692a47a2004-03-14 02:34:07 +00007//===----------------------------------------------------------------------===//
Misha Brukman03a11342004-02-28 03:33:01 +00008//
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 Brukmanf272f9b2004-03-02 00:19:09 +000011// given function, it is not touched. This is a pass most useful for debugging
12// via bugpoint.
Misha Brukman03a11342004-02-28 03:33:01 +000013//
14//===----------------------------------------------------------------------===//
15
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/Statistic.h"
Sergey Dmitriev807960e2019-02-08 06:55:18 +000017#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/Analysis/LoopPass.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000019#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/Instructions.h"
21#include "llvm/IR/Module.h"
Misha Brukman03a11342004-02-28 03:33:01 +000022#include "llvm/Pass.h"
Nick Lewyckyc6243022007-11-14 06:47:06 +000023#include "llvm/Support/CommandLine.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000024#include "llvm/Transforms/IPO.h"
Chris Lattner6c3e8c72004-03-14 04:01:06 +000025#include "llvm/Transforms/Scalar.h"
David Blaikiea373d182018-03-28 17:44:36 +000026#include "llvm/Transforms/Utils.h"
Bill Wendlingc1da6ea2011-09-20 19:10:24 +000027#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Chandler Carruth0fde0012012-05-04 10:18:49 +000028#include "llvm/Transforms/Utils/CodeExtractor.h"
Nick Lewyckyc6243022007-11-14 06:47:06 +000029#include <fstream>
30#include <set>
Misha Brukman03a11342004-02-28 03:33:01 +000031using namespace llvm;
32
Chandler Carruth964daaa2014-04-22 02:55:47 +000033#define DEBUG_TYPE "loop-extract"
34
Chris Lattner1631bcb2006-12-19 22:09:18 +000035STATISTIC(NumExtracted, "Number of loops extracted");
Misha Brukmanb1c93172005-04-21 23:48:37 +000036
Chris Lattner1631bcb2006-12-19 22:09:18 +000037namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000038 struct LoopExtractor : public LoopPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000039 static char ID; // Pass identification, replacement for typeid
Chris Lattnera1672c12004-03-14 20:01:36 +000040 unsigned NumLoops;
41
Justin Bognere9fb228d2016-01-08 19:08:53 +000042 explicit LoopExtractor(unsigned numLoops = ~0)
Owen Anderson6c18d1a2010-10-19 17:21:58 +000043 : LoopPass(ID), NumLoops(numLoops) {
44 initializeLoopExtractorPass(*PassRegistry::getPassRegistry());
45 }
Chris Lattnera1672c12004-03-14 20:01:36 +000046
Justin Bogner883a3ea2015-12-16 18:40:20 +000047 bool runOnLoop(Loop *L, LPPassManager &) override;
Misha Brukmanb1c93172005-04-21 23:48:37 +000048
Craig Topper3e4c6972014-03-05 09:10:37 +000049 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner5bce0c82004-03-18 05:43:18 +000050 AU.addRequiredID(BreakCriticalEdgesID);
51 AU.addRequiredID(LoopSimplifyID);
Chandler Carruth73523022014-01-13 13:07:17 +000052 AU.addRequired<DominatorTreeWrapperPass>();
Justin Bogner883a3ea2015-12-16 18:40:20 +000053 AU.addRequired<LoopInfoWrapperPass>();
Sergey Dmitriev807960e2019-02-08 06:55:18 +000054 AU.addUsedIfAvailable<AssumptionCacheTracker>();
Chris Lattner692a47a2004-03-14 02:34:07 +000055 }
56 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000057}
Misha Brukman03a11342004-02-28 03:33:01 +000058
Dan Gohmand78c4002008-05-13 00:00:25 +000059char LoopExtractor::ID = 0;
Owen Anderson8ac477f2010-10-12 19:48:12 +000060INITIALIZE_PASS_BEGIN(LoopExtractor, "loop-extract",
Bill Wendlingc1da6ea2011-09-20 19:10:24 +000061 "Extract loops into new functions", false, false)
Owen Anderson8ac477f2010-10-12 19:48:12 +000062INITIALIZE_PASS_DEPENDENCY(BreakCriticalEdges)
63INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
Chandler Carruth73523022014-01-13 13:07:17 +000064INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Owen Anderson8ac477f2010-10-12 19:48:12 +000065INITIALIZE_PASS_END(LoopExtractor, "loop-extract",
Bill Wendlingc1da6ea2011-09-20 19:10:24 +000066 "Extract loops into new functions", false, false)
Chris Lattnera1672c12004-03-14 20:01:36 +000067
Dan Gohmand78c4002008-05-13 00:00:25 +000068namespace {
Chris Lattnera1672c12004-03-14 20:01:36 +000069 /// SingleLoopExtractor - For bugpoint.
70 struct SingleLoopExtractor : public LoopExtractor {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000071 static char ID; // Pass identification, replacement for typeid
Chris Lattnera1672c12004-03-14 20:01:36 +000072 SingleLoopExtractor() : LoopExtractor(1) {}
73 };
Misha Brukmanb1c93172005-04-21 23:48:37 +000074} // End anonymous namespace
Misha Brukman03a11342004-02-28 03:33:01 +000075
Dan Gohmand78c4002008-05-13 00:00:25 +000076char SingleLoopExtractor::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +000077INITIALIZE_PASS(SingleLoopExtractor, "loop-extract-single",
Owen Andersondf7a4f22010-10-07 22:25:06 +000078 "Extract at most one loop into a new function", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000079
Jeff Cohen677babc2005-01-08 17:21:40 +000080// createLoopExtractorPass - This pass extracts all natural loops from the
81// program into a function if it can.
82//
Dan Gohman9a7320c2009-09-28 14:37:51 +000083Pass *llvm::createLoopExtractorPass() { return new LoopExtractor(); }
Jeff Cohen677babc2005-01-08 17:21:40 +000084
Sanjoy Dasdef17292017-09-28 02:45:42 +000085bool LoopExtractor::runOnLoop(Loop *L, LPPassManager &LPM) {
Andrew Kayloraa641a52016-04-22 22:06:11 +000086 if (skipLoop(L))
Paul Robinsonaf4e64d2014-02-06 00:07:05 +000087 return false;
88
Dan Gohman9a7320c2009-09-28 14:37:51 +000089 // Only visit top-level loops.
90 if (L->getParentLoop())
Misha Brukman03a11342004-02-28 03:33:01 +000091 return false;
92
Dan Gohmana83ac2d2009-11-05 21:11:53 +000093 // If LoopSimplify form is not available, stay out of trouble.
94 if (!L->isLoopSimplifyForm())
95 return false;
96
Chandler Carruth73523022014-01-13 13:07:17 +000097 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Justin Bogner883a3ea2015-12-16 18:40:20 +000098 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Dan Gohman9a7320c2009-09-28 14:37:51 +000099 bool Changed = false;
Chris Lattnere9235d22004-03-18 03:48:06 +0000100
Chris Lattner2f155d82004-03-15 00:02:02 +0000101 // If there is more than one top-level loop in this function, extract all of
Dan Gohman9a7320c2009-09-28 14:37:51 +0000102 // 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 Brukmanb1c93172005-04-21 23:48:37 +0000106
Dan Gohman9a7320c2009-09-28 14:37:51 +0000107 // Extract the loop if the entry block doesn't branch to the loop header.
Chandler Carruthedb12a82018-10-15 10:04:59 +0000108 Instruction *EntryTI =
109 L->getHeader()->getParent()->getEntryBlock().getTerminator();
Dan Gohman9a7320c2009-09-28 14:37:51 +0000110 if (!isa<BranchInst>(EntryTI) ||
111 !cast<BranchInst>(EntryTI)->isUnconditional() ||
Bill Wendling00585202011-09-20 22:23:09 +0000112 EntryTI->getSuccessor(0) != L->getHeader()) {
Dan Gohman9a7320c2009-09-28 14:37:51 +0000113 ShouldExtractLoop = true;
Bill Wendling00585202011-09-20 22:23:09 +0000114 } else {
Dan Gohman9a7320c2009-09-28 14:37:51 +0000115 // Check to see if any exits from the loop are more than just return
Bill Wendling04289fc2011-09-20 22:27:16 +0000116 // 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 Majnemereb518bd2015-08-04 08:21:40 +0000127 // We must omit EH pads. EH pads must accompany the invoke
Bill Wendling04289fc2011-09-20 22:27:16 +0000128 // instruction. But this would result in a loop in the extracted
Bill Wendling00585202011-09-20 22:23:09 +0000129 // function. An infinite cycle occurs when it tries to extract that loop as
130 // well.
Dan Gohman9a7320c2009-09-28 14:37:51 +0000131 SmallVector<BasicBlock*, 8> ExitBlocks;
132 L->getExitBlocks(ExitBlocks);
Bill Wendling04289fc2011-09-20 22:27:16 +0000133 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
David Majnemereb518bd2015-08-04 08:21:40 +0000134 if (ExitBlocks[i]->isEHPad()) {
Bill Wendling00585202011-09-20 22:23:09 +0000135 ShouldExtractLoop = false;
Dan Gohman9a7320c2009-09-28 14:37:51 +0000136 break;
Chris Lattner2f155d82004-03-15 00:02:02 +0000137 }
Dan Gohman9a7320c2009-09-28 14:37:51 +0000138 }
Bill Wendling04289fc2011-09-20 22:27:16 +0000139
Dan Gohman9a7320c2009-09-28 14:37:51 +0000140 if (ShouldExtractLoop) {
141 if (NumLoops == 0) return Changed;
142 --NumLoops;
Sergey Dmitriev807960e2019-02-08 06:55:18 +0000143 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 Topperf40110f2014-04-25 05:29:35 +0000147 if (Extractor.extractCodeRegion() != nullptr) {
Dan Gohman9a7320c2009-09-28 14:37:51 +0000148 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 Dasdef17292017-09-28 02:45:42 +0000151 LPM.markLoopAsDeleted(*L);
Sanjoy Das388b0122017-09-22 01:47:41 +0000152 LI.erase(L);
Chris Lattner2f155d82004-03-15 00:02:02 +0000153 }
Dan Gohman9a7320c2009-09-28 14:37:51 +0000154 ++NumExtracted;
Chris Lattnera1672c12004-03-14 20:01:36 +0000155 }
Misha Brukman03a11342004-02-28 03:33:01 +0000156
157 return Changed;
158}
159
Chris Lattnera1672c12004-03-14 20:01:36 +0000160// createSingleLoopExtractorPass - This pass extracts one natural loop from the
161// program into a function if it can. This is used by bugpoint.
162//
Dan Gohman9a7320c2009-09-28 14:37:51 +0000163Pass *llvm::createSingleLoopExtractorPass() {
Chris Lattnera1672c12004-03-14 20:01:36 +0000164 return new SingleLoopExtractor();
Misha Brukman03a11342004-02-28 03:33:01 +0000165}