blob: 8c86f7cb806a0c9d394c91faf47382f3a7448b3e [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//
Chris Lattner692a47a2004-03-14 02:34:07 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Chris Lattner692a47a2004-03-14 02:34:07 +00008//===----------------------------------------------------------------------===//
Misha Brukman03a11342004-02-28 03:33:01 +00009//
10// A pass wrapper around the ExtractLoop() scalar transformation to extract each
11// top-level loop into its own new function. If the loop is the ONLY loop in a
Misha Brukmanf272f9b2004-03-02 00:19:09 +000012// given function, it is not touched. This is a pass most useful for debugging
13// via bugpoint.
Misha Brukman03a11342004-02-28 03:33:01 +000014//
15//===----------------------------------------------------------------------===//
16
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/Statistic.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>();
Chris Lattner692a47a2004-03-14 02:34:07 +000054 }
55 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000056}
Misha Brukman03a11342004-02-28 03:33:01 +000057
Dan Gohmand78c4002008-05-13 00:00:25 +000058char LoopExtractor::ID = 0;
Owen Anderson8ac477f2010-10-12 19:48:12 +000059INITIALIZE_PASS_BEGIN(LoopExtractor, "loop-extract",
Bill Wendlingc1da6ea2011-09-20 19:10:24 +000060 "Extract loops into new functions", false, false)
Owen Anderson8ac477f2010-10-12 19:48:12 +000061INITIALIZE_PASS_DEPENDENCY(BreakCriticalEdges)
62INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
Chandler Carruth73523022014-01-13 13:07:17 +000063INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Owen Anderson8ac477f2010-10-12 19:48:12 +000064INITIALIZE_PASS_END(LoopExtractor, "loop-extract",
Bill Wendlingc1da6ea2011-09-20 19:10:24 +000065 "Extract loops into new functions", false, false)
Chris Lattnera1672c12004-03-14 20:01:36 +000066
Dan Gohmand78c4002008-05-13 00:00:25 +000067namespace {
Chris Lattnera1672c12004-03-14 20:01:36 +000068 /// SingleLoopExtractor - For bugpoint.
69 struct SingleLoopExtractor : public LoopExtractor {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000070 static char ID; // Pass identification, replacement for typeid
Chris Lattnera1672c12004-03-14 20:01:36 +000071 SingleLoopExtractor() : LoopExtractor(1) {}
72 };
Misha Brukmanb1c93172005-04-21 23:48:37 +000073} // End anonymous namespace
Misha Brukman03a11342004-02-28 03:33:01 +000074
Dan Gohmand78c4002008-05-13 00:00:25 +000075char SingleLoopExtractor::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +000076INITIALIZE_PASS(SingleLoopExtractor, "loop-extract-single",
Owen Andersondf7a4f22010-10-07 22:25:06 +000077 "Extract at most one loop into a new function", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000078
Jeff Cohen677babc2005-01-08 17:21:40 +000079// createLoopExtractorPass - This pass extracts all natural loops from the
80// program into a function if it can.
81//
Dan Gohman9a7320c2009-09-28 14:37:51 +000082Pass *llvm::createLoopExtractorPass() { return new LoopExtractor(); }
Jeff Cohen677babc2005-01-08 17:21:40 +000083
Sanjoy Dasdef17292017-09-28 02:45:42 +000084bool LoopExtractor::runOnLoop(Loop *L, LPPassManager &LPM) {
Andrew Kayloraa641a52016-04-22 22:06:11 +000085 if (skipLoop(L))
Paul Robinsonaf4e64d2014-02-06 00:07:05 +000086 return false;
87
Dan Gohman9a7320c2009-09-28 14:37:51 +000088 // Only visit top-level loops.
89 if (L->getParentLoop())
Misha Brukman03a11342004-02-28 03:33:01 +000090 return false;
91
Dan Gohmana83ac2d2009-11-05 21:11:53 +000092 // If LoopSimplify form is not available, stay out of trouble.
93 if (!L->isLoopSimplifyForm())
94 return false;
95
Chandler Carruth73523022014-01-13 13:07:17 +000096 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Justin Bogner883a3ea2015-12-16 18:40:20 +000097 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Dan Gohman9a7320c2009-09-28 14:37:51 +000098 bool Changed = false;
Chris Lattnere9235d22004-03-18 03:48:06 +000099
Chris Lattner2f155d82004-03-15 00:02:02 +0000100 // If there is more than one top-level loop in this function, extract all of
Dan Gohman9a7320c2009-09-28 14:37:51 +0000101 // the loops. Otherwise there is exactly one top-level loop; in this case if
102 // this function is more than a minimal wrapper around the loop, extract
103 // the loop.
104 bool ShouldExtractLoop = false;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000105
Dan Gohman9a7320c2009-09-28 14:37:51 +0000106 // Extract the loop if the entry block doesn't branch to the loop header.
107 TerminatorInst *EntryTI =
108 L->getHeader()->getParent()->getEntryBlock().getTerminator();
109 if (!isa<BranchInst>(EntryTI) ||
110 !cast<BranchInst>(EntryTI)->isUnconditional() ||
Bill Wendling00585202011-09-20 22:23:09 +0000111 EntryTI->getSuccessor(0) != L->getHeader()) {
Dan Gohman9a7320c2009-09-28 14:37:51 +0000112 ShouldExtractLoop = true;
Bill Wendling00585202011-09-20 22:23:09 +0000113 } else {
Dan Gohman9a7320c2009-09-28 14:37:51 +0000114 // Check to see if any exits from the loop are more than just return
Bill Wendling04289fc2011-09-20 22:27:16 +0000115 // blocks.
116 SmallVector<BasicBlock*, 8> ExitBlocks;
117 L->getExitBlocks(ExitBlocks);
118 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
119 if (!isa<ReturnInst>(ExitBlocks[i]->getTerminator())) {
120 ShouldExtractLoop = true;
121 break;
122 }
123 }
124
125 if (ShouldExtractLoop) {
David Majnemereb518bd2015-08-04 08:21:40 +0000126 // We must omit EH pads. EH pads must accompany the invoke
Bill Wendling04289fc2011-09-20 22:27:16 +0000127 // instruction. But this would result in a loop in the extracted
Bill Wendling00585202011-09-20 22:23:09 +0000128 // function. An infinite cycle occurs when it tries to extract that loop as
129 // well.
Dan Gohman9a7320c2009-09-28 14:37:51 +0000130 SmallVector<BasicBlock*, 8> ExitBlocks;
131 L->getExitBlocks(ExitBlocks);
Bill Wendling04289fc2011-09-20 22:27:16 +0000132 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
David Majnemereb518bd2015-08-04 08:21:40 +0000133 if (ExitBlocks[i]->isEHPad()) {
Bill Wendling00585202011-09-20 22:23:09 +0000134 ShouldExtractLoop = false;
Dan Gohman9a7320c2009-09-28 14:37:51 +0000135 break;
Chris Lattner2f155d82004-03-15 00:02:02 +0000136 }
Dan Gohman9a7320c2009-09-28 14:37:51 +0000137 }
Bill Wendling04289fc2011-09-20 22:27:16 +0000138
Dan Gohman9a7320c2009-09-28 14:37:51 +0000139 if (ShouldExtractLoop) {
140 if (NumLoops == 0) return Changed;
141 --NumLoops;
Chandler Carruth0fde0012012-05-04 10:18:49 +0000142 CodeExtractor Extractor(DT, *L);
Craig Topperf40110f2014-04-25 05:29:35 +0000143 if (Extractor.extractCodeRegion() != nullptr) {
Dan Gohman9a7320c2009-09-28 14:37:51 +0000144 Changed = true;
145 // After extraction, the loop is replaced by a function call, so
146 // we shouldn't try to run any more loop passes on it.
Sanjoy Dasdef17292017-09-28 02:45:42 +0000147 LPM.markLoopAsDeleted(*L);
Sanjoy Das388b0122017-09-22 01:47:41 +0000148 LI.erase(L);
Chris Lattner2f155d82004-03-15 00:02:02 +0000149 }
Dan Gohman9a7320c2009-09-28 14:37:51 +0000150 ++NumExtracted;
Chris Lattnera1672c12004-03-14 20:01:36 +0000151 }
Misha Brukman03a11342004-02-28 03:33:01 +0000152
153 return Changed;
154}
155
Chris Lattnera1672c12004-03-14 20:01:36 +0000156// createSingleLoopExtractorPass - This pass extracts one natural loop from the
157// program into a function if it can. This is used by bugpoint.
158//
Dan Gohman9a7320c2009-09-28 14:37:51 +0000159Pass *llvm::createSingleLoopExtractorPass() {
Chris Lattnera1672c12004-03-14 20:01:36 +0000160 return new SingleLoopExtractor();
Misha Brukman03a11342004-02-28 03:33:01 +0000161}