blob: c5f4fca12af2c28e44596db6ab7a0d1082901afb [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"
Reid Spencer557ab152007-02-05 23:32:05 +000024#include "llvm/Support/Compiler.h"
Chris Lattner6c3e8c72004-03-14 04:01:06 +000025#include "llvm/Transforms/Scalar.h"
Misha Brukman03a11342004-02-28 03:33:01 +000026#include "llvm/Transforms/Utils/FunctionUtils.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000027#include "llvm/ADT/Statistic.h"
Misha Brukman03a11342004-02-28 03:33:01 +000028using namespace llvm;
29
Chris Lattner1631bcb2006-12-19 22:09:18 +000030STATISTIC(NumExtracted, "Number of loops extracted");
Misha Brukmanb1c93172005-04-21 23:48:37 +000031
Chris Lattner1631bcb2006-12-19 22:09:18 +000032namespace {
Chris Lattnera1672c12004-03-14 20:01:36 +000033 // FIXME: This is not a function pass, but the PassManager doesn't allow
34 // Module passes to require FunctionPasses, so we can't get loop info if we're
35 // not a function pass.
Reid Spencer557ab152007-02-05 23:32:05 +000036 struct VISIBILITY_HIDDEN LoopExtractor : public FunctionPass {
Chris Lattnera1672c12004-03-14 20:01:36 +000037 unsigned NumLoops;
38
39 LoopExtractor(unsigned numLoops = ~0) : NumLoops(numLoops) {}
40
Chris Lattner692a47a2004-03-14 02:34:07 +000041 virtual bool runOnFunction(Function &F);
Misha Brukmanb1c93172005-04-21 23:48:37 +000042
Chris Lattner692a47a2004-03-14 02:34:07 +000043 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner5bce0c82004-03-18 05:43:18 +000044 AU.addRequiredID(BreakCriticalEdgesID);
45 AU.addRequiredID(LoopSimplifyID);
Owen Andersonf095bf32007-04-07 05:31:27 +000046 AU.addRequired<ETForest>();
47 AU.addRequired<DominatorTree>();
Chris Lattner692a47a2004-03-14 02:34:07 +000048 AU.addRequired<LoopInfo>();
49 }
50 };
Misha Brukman03a11342004-02-28 03:33:01 +000051
Chris Lattnerc2d3d312006-08-27 22:42:52 +000052 RegisterPass<LoopExtractor>
Chris Lattner692a47a2004-03-14 02:34:07 +000053 X("loop-extract", "Extract loops into new functions");
Chris Lattnera1672c12004-03-14 20:01:36 +000054
55 /// SingleLoopExtractor - For bugpoint.
56 struct SingleLoopExtractor : public LoopExtractor {
57 SingleLoopExtractor() : LoopExtractor(1) {}
58 };
59
Chris Lattnerc2d3d312006-08-27 22:42:52 +000060 RegisterPass<SingleLoopExtractor>
Chris Lattnera1672c12004-03-14 20:01:36 +000061 Y("loop-extract-single", "Extract at most one loop into a new function");
Misha Brukmanb1c93172005-04-21 23:48:37 +000062} // End anonymous namespace
Misha Brukman03a11342004-02-28 03:33:01 +000063
Jeff Cohen677babc2005-01-08 17:21:40 +000064// createLoopExtractorPass - This pass extracts all natural loops from the
65// program into a function if it can.
66//
Jeff Cohen3e62e7c2005-01-10 04:23:32 +000067FunctionPass *llvm::createLoopExtractorPass() { return new LoopExtractor(); }
Jeff Cohen677babc2005-01-08 17:21:40 +000068
Misha Brukman03a11342004-02-28 03:33:01 +000069bool LoopExtractor::runOnFunction(Function &F) {
Misha Brukman03a11342004-02-28 03:33:01 +000070 LoopInfo &LI = getAnalysis<LoopInfo>();
71
Chris Lattner2f155d82004-03-15 00:02:02 +000072 // If this function has no loops, there is nothing to do.
73 if (LI.begin() == LI.end())
Misha Brukman03a11342004-02-28 03:33:01 +000074 return false;
75
Owen Andersonf095bf32007-04-07 05:31:27 +000076 ETForest &EF = getAnalysis<ETForest>();
77 DominatorTree &DT = getAnalysis<DominatorTree>();
Chris Lattnere9235d22004-03-18 03:48:06 +000078
Chris Lattner2f155d82004-03-15 00:02:02 +000079 // If there is more than one top-level loop in this function, extract all of
80 // the loops.
Misha Brukman03a11342004-02-28 03:33:01 +000081 bool Changed = false;
Chris Lattner2f155d82004-03-15 00:02:02 +000082 if (LI.end()-LI.begin() > 1) {
83 for (LoopInfo::iterator i = LI.begin(), e = LI.end(); i != e; ++i) {
84 if (NumLoops == 0) return Changed;
85 --NumLoops;
Owen Andersonf095bf32007-04-07 05:31:27 +000086 Changed |= ExtractLoop(EF, DT, *i) != 0;
Chris Lattnere8369352004-03-18 05:46:10 +000087 ++NumExtracted;
Chris Lattner2f155d82004-03-15 00:02:02 +000088 }
89 } else {
90 // Otherwise there is exactly one top-level loop. If this function is more
91 // than a minimal wrapper around the loop, extract the loop.
92 Loop *TLL = *LI.begin();
93 bool ShouldExtractLoop = false;
Misha Brukmanb1c93172005-04-21 23:48:37 +000094
Chris Lattner2f155d82004-03-15 00:02:02 +000095 // Extract the loop if the entry block doesn't branch to the loop header.
96 TerminatorInst *EntryTI = F.getEntryBlock().getTerminator();
97 if (!isa<BranchInst>(EntryTI) ||
Misha Brukmanb1c93172005-04-21 23:48:37 +000098 !cast<BranchInst>(EntryTI)->isUnconditional() ||
Chris Lattner2f155d82004-03-15 00:02:02 +000099 EntryTI->getSuccessor(0) != TLL->getHeader())
100 ShouldExtractLoop = true;
101 else {
102 // Check to see if any exits from the loop are more than just return
103 // blocks.
Chris Lattnerd72c3eb2004-04-18 22:14:10 +0000104 std::vector<BasicBlock*> ExitBlocks;
105 TLL->getExitBlocks(ExitBlocks);
106 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
107 if (!isa<ReturnInst>(ExitBlocks[i]->getTerminator())) {
Chris Lattner2f155d82004-03-15 00:02:02 +0000108 ShouldExtractLoop = true;
109 break;
110 }
111 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000112
Chris Lattner2f155d82004-03-15 00:02:02 +0000113 if (ShouldExtractLoop) {
114 if (NumLoops == 0) return Changed;
115 --NumLoops;
Owen Andersonf095bf32007-04-07 05:31:27 +0000116 Changed |= ExtractLoop(EF, DT, TLL) != 0;
Chris Lattnere8369352004-03-18 05:46:10 +0000117 ++NumExtracted;
Chris Lattner2f155d82004-03-15 00:02:02 +0000118 } else {
119 // Okay, this function is a minimal container around the specified loop.
120 // If we extract the loop, we will continue to just keep extracting it
121 // infinitely... so don't extract it. However, if the loop contains any
122 // subloops, extract them.
123 for (Loop::iterator i = TLL->begin(), e = TLL->end(); i != e; ++i) {
124 if (NumLoops == 0) return Changed;
125 --NumLoops;
Owen Andersonf095bf32007-04-07 05:31:27 +0000126 Changed |= ExtractLoop(EF, DT, *i) != 0;
Chris Lattnere8369352004-03-18 05:46:10 +0000127 ++NumExtracted;
Chris Lattner2f155d82004-03-15 00:02:02 +0000128 }
129 }
Chris Lattnera1672c12004-03-14 20:01:36 +0000130 }
Misha Brukman03a11342004-02-28 03:33:01 +0000131
132 return Changed;
133}
134
Chris Lattnera1672c12004-03-14 20:01:36 +0000135// createSingleLoopExtractorPass - This pass extracts one natural loop from the
136// program into a function if it can. This is used by bugpoint.
137//
Jeff Cohen3e62e7c2005-01-10 04:23:32 +0000138FunctionPass *llvm::createSingleLoopExtractorPass() {
Chris Lattnera1672c12004-03-14 20:01:36 +0000139 return new SingleLoopExtractor();
Misha Brukman03a11342004-02-28 03:33:01 +0000140}
Chris Lattner7386e632004-08-13 03:05:17 +0000141
142
143namespace {
144 /// BlockExtractorPass - This pass is used by bugpoint to extract all blocks
145 /// from the module into their own functions except for those specified by the
146 /// BlocksToNotExtract list.
Chris Lattner4f2cf032004-09-20 04:48:05 +0000147 class BlockExtractorPass : public ModulePass {
Chris Lattner7386e632004-08-13 03:05:17 +0000148 std::vector<BasicBlock*> BlocksToNotExtract;
149 public:
150 BlockExtractorPass(std::vector<BasicBlock*> &B) : BlocksToNotExtract(B) {}
151 BlockExtractorPass() {}
152
Chris Lattner4f2cf032004-09-20 04:48:05 +0000153 bool runOnModule(Module &M);
Chris Lattner7386e632004-08-13 03:05:17 +0000154 };
Chris Lattnerc2d3d312006-08-27 22:42:52 +0000155 RegisterPass<BlockExtractorPass>
Chris Lattner7386e632004-08-13 03:05:17 +0000156 XX("extract-blocks", "Extract Basic Blocks From Module (for bugpoint use)");
157}
158
159// createBlockExtractorPass - This pass extracts all blocks (except those
160// specified in the argument list) from the functions in the module.
161//
Chris Lattner4f2cf032004-09-20 04:48:05 +0000162ModulePass *llvm::createBlockExtractorPass(std::vector<BasicBlock*> &BTNE) {
Chris Lattner7386e632004-08-13 03:05:17 +0000163 return new BlockExtractorPass(BTNE);
164}
165
Chris Lattner4f2cf032004-09-20 04:48:05 +0000166bool BlockExtractorPass::runOnModule(Module &M) {
Chris Lattner7386e632004-08-13 03:05:17 +0000167 std::set<BasicBlock*> TranslatedBlocksToNotExtract;
168 for (unsigned i = 0, e = BlocksToNotExtract.size(); i != e; ++i) {
169 BasicBlock *BB = BlocksToNotExtract[i];
170 Function *F = BB->getParent();
171
172 // Map the corresponding function in this module.
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000173 Function *MF = M.getFunction(F->getName());
174 assert(MF->getFunctionType() == F->getFunctionType() && "Wrong function?");
Chris Lattner7386e632004-08-13 03:05:17 +0000175
176 // Figure out which index the basic block is in its function.
177 Function::iterator BBI = MF->begin();
178 std::advance(BBI, std::distance(F->begin(), Function::iterator(BB)));
179 TranslatedBlocksToNotExtract.insert(BBI);
180 }
181
182 // Now that we know which blocks to not extract, figure out which ones we WANT
183 // to extract.
184 std::vector<BasicBlock*> BlocksToExtract;
185 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
186 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
187 if (!TranslatedBlocksToNotExtract.count(BB))
188 BlocksToExtract.push_back(BB);
189
190 for (unsigned i = 0, e = BlocksToExtract.size(); i != e; ++i)
191 ExtractBasicBlock(BlocksToExtract[i]);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000192
Chris Lattner7386e632004-08-13 03:05:17 +0000193 return !BlocksToExtract.empty();
194}