blob: 7169b503b21cd9a772105ff702673ebe5877242f [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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
Chris Lattner1631bcb2006-12-19 22:09:18 +000017#define DEBUG_TYPE "loop-extract"
Chris Lattner78a996a2004-03-14 02:37:16 +000018#include "llvm/Transforms/IPO.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000019#include "llvm/Instructions.h"
Misha Brukman03a11342004-02-28 03:33:01 +000020#include "llvm/Module.h"
21#include "llvm/Pass.h"
Chris Lattnere9235d22004-03-18 03:48:06 +000022#include "llvm/Analysis/Dominators.h"
Misha Brukman03a11342004-02-28 03:33:01 +000023#include "llvm/Analysis/LoopInfo.h"
Chris Lattner6c3e8c72004-03-14 04:01:06 +000024#include "llvm/Transforms/Scalar.h"
Misha Brukman03a11342004-02-28 03:33:01 +000025#include "llvm/Transforms/Utils/FunctionUtils.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000026#include "llvm/ADT/Statistic.h"
Misha Brukman03a11342004-02-28 03:33:01 +000027using namespace llvm;
28
Chris Lattner1631bcb2006-12-19 22:09:18 +000029STATISTIC(NumExtracted, "Number of loops extracted");
Misha Brukmanb1c93172005-04-21 23:48:37 +000030
Chris Lattner1631bcb2006-12-19 22:09:18 +000031namespace {
Chris Lattnera1672c12004-03-14 20:01:36 +000032 // FIXME: This is not a function pass, but the PassManager doesn't allow
33 // Module passes to require FunctionPasses, so we can't get loop info if we're
34 // not a function pass.
Chris Lattner692a47a2004-03-14 02:34:07 +000035 struct LoopExtractor : public FunctionPass {
Chris Lattnera1672c12004-03-14 20:01:36 +000036 unsigned NumLoops;
37
38 LoopExtractor(unsigned numLoops = ~0) : NumLoops(numLoops) {}
39
Chris Lattner692a47a2004-03-14 02:34:07 +000040 virtual bool runOnFunction(Function &F);
Misha Brukmanb1c93172005-04-21 23:48:37 +000041
Chris Lattner692a47a2004-03-14 02:34:07 +000042 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner5bce0c82004-03-18 05:43:18 +000043 AU.addRequiredID(BreakCriticalEdgesID);
44 AU.addRequiredID(LoopSimplifyID);
Chris Lattnere9235d22004-03-18 03:48:06 +000045 AU.addRequired<DominatorSet>();
Chris Lattner692a47a2004-03-14 02:34:07 +000046 AU.addRequired<LoopInfo>();
47 }
48 };
Misha Brukman03a11342004-02-28 03:33:01 +000049
Chris Lattnerc2d3d312006-08-27 22:42:52 +000050 RegisterPass<LoopExtractor>
Chris Lattner692a47a2004-03-14 02:34:07 +000051 X("loop-extract", "Extract loops into new functions");
Chris Lattnera1672c12004-03-14 20:01:36 +000052
53 /// SingleLoopExtractor - For bugpoint.
54 struct SingleLoopExtractor : public LoopExtractor {
55 SingleLoopExtractor() : LoopExtractor(1) {}
56 };
57
Chris Lattnerc2d3d312006-08-27 22:42:52 +000058 RegisterPass<SingleLoopExtractor>
Chris Lattnera1672c12004-03-14 20:01:36 +000059 Y("loop-extract-single", "Extract at most one loop into a new function");
Misha Brukmanb1c93172005-04-21 23:48:37 +000060} // End anonymous namespace
Misha Brukman03a11342004-02-28 03:33:01 +000061
Jeff Cohen677babc2005-01-08 17:21:40 +000062// createLoopExtractorPass - This pass extracts all natural loops from the
63// program into a function if it can.
64//
Jeff Cohen3e62e7c2005-01-10 04:23:32 +000065FunctionPass *llvm::createLoopExtractorPass() { return new LoopExtractor(); }
Jeff Cohen677babc2005-01-08 17:21:40 +000066
Misha Brukman03a11342004-02-28 03:33:01 +000067bool LoopExtractor::runOnFunction(Function &F) {
Misha Brukman03a11342004-02-28 03:33:01 +000068 LoopInfo &LI = getAnalysis<LoopInfo>();
69
Chris Lattner2f155d82004-03-15 00:02:02 +000070 // If this function has no loops, there is nothing to do.
71 if (LI.begin() == LI.end())
Misha Brukman03a11342004-02-28 03:33:01 +000072 return false;
73
Chris Lattnere9235d22004-03-18 03:48:06 +000074 DominatorSet &DS = getAnalysis<DominatorSet>();
75
Chris Lattner2f155d82004-03-15 00:02:02 +000076 // If there is more than one top-level loop in this function, extract all of
77 // the loops.
Misha Brukman03a11342004-02-28 03:33:01 +000078 bool Changed = false;
Chris Lattner2f155d82004-03-15 00:02:02 +000079 if (LI.end()-LI.begin() > 1) {
80 for (LoopInfo::iterator i = LI.begin(), e = LI.end(); i != e; ++i) {
81 if (NumLoops == 0) return Changed;
82 --NumLoops;
Chris Lattnere9235d22004-03-18 03:48:06 +000083 Changed |= ExtractLoop(DS, *i) != 0;
Chris Lattnere8369352004-03-18 05:46:10 +000084 ++NumExtracted;
Chris Lattner2f155d82004-03-15 00:02:02 +000085 }
86 } else {
87 // Otherwise there is exactly one top-level loop. If this function is more
88 // than a minimal wrapper around the loop, extract the loop.
89 Loop *TLL = *LI.begin();
90 bool ShouldExtractLoop = false;
Misha Brukmanb1c93172005-04-21 23:48:37 +000091
Chris Lattner2f155d82004-03-15 00:02:02 +000092 // Extract the loop if the entry block doesn't branch to the loop header.
93 TerminatorInst *EntryTI = F.getEntryBlock().getTerminator();
94 if (!isa<BranchInst>(EntryTI) ||
Misha Brukmanb1c93172005-04-21 23:48:37 +000095 !cast<BranchInst>(EntryTI)->isUnconditional() ||
Chris Lattner2f155d82004-03-15 00:02:02 +000096 EntryTI->getSuccessor(0) != TLL->getHeader())
97 ShouldExtractLoop = true;
98 else {
99 // Check to see if any exits from the loop are more than just return
100 // blocks.
Chris Lattnerd72c3eb2004-04-18 22:14:10 +0000101 std::vector<BasicBlock*> ExitBlocks;
102 TLL->getExitBlocks(ExitBlocks);
103 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
104 if (!isa<ReturnInst>(ExitBlocks[i]->getTerminator())) {
Chris Lattner2f155d82004-03-15 00:02:02 +0000105 ShouldExtractLoop = true;
106 break;
107 }
108 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000109
Chris Lattner2f155d82004-03-15 00:02:02 +0000110 if (ShouldExtractLoop) {
111 if (NumLoops == 0) return Changed;
112 --NumLoops;
Chris Lattnere9235d22004-03-18 03:48:06 +0000113 Changed |= ExtractLoop(DS, TLL) != 0;
Chris Lattnere8369352004-03-18 05:46:10 +0000114 ++NumExtracted;
Chris Lattner2f155d82004-03-15 00:02:02 +0000115 } else {
116 // Okay, this function is a minimal container around the specified loop.
117 // If we extract the loop, we will continue to just keep extracting it
118 // infinitely... so don't extract it. However, if the loop contains any
119 // subloops, extract them.
120 for (Loop::iterator i = TLL->begin(), e = TLL->end(); i != e; ++i) {
121 if (NumLoops == 0) return Changed;
122 --NumLoops;
Chris Lattnere9235d22004-03-18 03:48:06 +0000123 Changed |= ExtractLoop(DS, *i) != 0;
Chris Lattnere8369352004-03-18 05:46:10 +0000124 ++NumExtracted;
Chris Lattner2f155d82004-03-15 00:02:02 +0000125 }
126 }
Chris Lattnera1672c12004-03-14 20:01:36 +0000127 }
Misha Brukman03a11342004-02-28 03:33:01 +0000128
129 return Changed;
130}
131
Chris Lattnera1672c12004-03-14 20:01:36 +0000132// createSingleLoopExtractorPass - This pass extracts one natural loop from the
133// program into a function if it can. This is used by bugpoint.
134//
Jeff Cohen3e62e7c2005-01-10 04:23:32 +0000135FunctionPass *llvm::createSingleLoopExtractorPass() {
Chris Lattnera1672c12004-03-14 20:01:36 +0000136 return new SingleLoopExtractor();
Misha Brukman03a11342004-02-28 03:33:01 +0000137}
Chris Lattner7386e632004-08-13 03:05:17 +0000138
139
140namespace {
141 /// BlockExtractorPass - This pass is used by bugpoint to extract all blocks
142 /// from the module into their own functions except for those specified by the
143 /// BlocksToNotExtract list.
Chris Lattner4f2cf032004-09-20 04:48:05 +0000144 class BlockExtractorPass : public ModulePass {
Chris Lattner7386e632004-08-13 03:05:17 +0000145 std::vector<BasicBlock*> BlocksToNotExtract;
146 public:
147 BlockExtractorPass(std::vector<BasicBlock*> &B) : BlocksToNotExtract(B) {}
148 BlockExtractorPass() {}
149
Chris Lattner4f2cf032004-09-20 04:48:05 +0000150 bool runOnModule(Module &M);
Chris Lattner7386e632004-08-13 03:05:17 +0000151 };
Chris Lattnerc2d3d312006-08-27 22:42:52 +0000152 RegisterPass<BlockExtractorPass>
Chris Lattner7386e632004-08-13 03:05:17 +0000153 XX("extract-blocks", "Extract Basic Blocks From Module (for bugpoint use)");
154}
155
156// createBlockExtractorPass - This pass extracts all blocks (except those
157// specified in the argument list) from the functions in the module.
158//
Chris Lattner4f2cf032004-09-20 04:48:05 +0000159ModulePass *llvm::createBlockExtractorPass(std::vector<BasicBlock*> &BTNE) {
Chris Lattner7386e632004-08-13 03:05:17 +0000160 return new BlockExtractorPass(BTNE);
161}
162
Chris Lattner4f2cf032004-09-20 04:48:05 +0000163bool BlockExtractorPass::runOnModule(Module &M) {
Chris Lattner7386e632004-08-13 03:05:17 +0000164 std::set<BasicBlock*> TranslatedBlocksToNotExtract;
165 for (unsigned i = 0, e = BlocksToNotExtract.size(); i != e; ++i) {
166 BasicBlock *BB = BlocksToNotExtract[i];
167 Function *F = BB->getParent();
168
169 // Map the corresponding function in this module.
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000170 Function *MF = M.getFunction(F->getName());
171 assert(MF->getFunctionType() == F->getFunctionType() && "Wrong function?");
Chris Lattner7386e632004-08-13 03:05:17 +0000172
173 // Figure out which index the basic block is in its function.
174 Function::iterator BBI = MF->begin();
175 std::advance(BBI, std::distance(F->begin(), Function::iterator(BB)));
176 TranslatedBlocksToNotExtract.insert(BBI);
177 }
178
179 // Now that we know which blocks to not extract, figure out which ones we WANT
180 // to extract.
181 std::vector<BasicBlock*> BlocksToExtract;
182 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
183 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
184 if (!TranslatedBlocksToNotExtract.count(BB))
185 BlocksToExtract.push_back(BB);
186
187 for (unsigned i = 0, e = BlocksToExtract.size(); i != e; ++i)
188 ExtractBasicBlock(BlocksToExtract[i]);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000189
Chris Lattner7386e632004-08-13 03:05:17 +0000190 return !BlocksToExtract.empty();
191}