blob: e99fa78a7d228f873f480ec3931b1a5af5237849 [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);
Chris Lattnere9235d22004-03-18 03:48:06 +000046 AU.addRequired<DominatorSet>();
Chris Lattner692a47a2004-03-14 02:34:07 +000047 AU.addRequired<LoopInfo>();
48 }
49 };
Misha Brukman03a11342004-02-28 03:33:01 +000050
Chris Lattnerc2d3d312006-08-27 22:42:52 +000051 RegisterPass<LoopExtractor>
Chris Lattner692a47a2004-03-14 02:34:07 +000052 X("loop-extract", "Extract loops into new functions");
Chris Lattnera1672c12004-03-14 20:01:36 +000053
54 /// SingleLoopExtractor - For bugpoint.
55 struct SingleLoopExtractor : public LoopExtractor {
56 SingleLoopExtractor() : LoopExtractor(1) {}
57 };
58
Chris Lattnerc2d3d312006-08-27 22:42:52 +000059 RegisterPass<SingleLoopExtractor>
Chris Lattnera1672c12004-03-14 20:01:36 +000060 Y("loop-extract-single", "Extract at most one loop into a new function");
Misha Brukmanb1c93172005-04-21 23:48:37 +000061} // End anonymous namespace
Misha Brukman03a11342004-02-28 03:33:01 +000062
Jeff Cohen677babc2005-01-08 17:21:40 +000063// createLoopExtractorPass - This pass extracts all natural loops from the
64// program into a function if it can.
65//
Jeff Cohen3e62e7c2005-01-10 04:23:32 +000066FunctionPass *llvm::createLoopExtractorPass() { return new LoopExtractor(); }
Jeff Cohen677babc2005-01-08 17:21:40 +000067
Misha Brukman03a11342004-02-28 03:33:01 +000068bool LoopExtractor::runOnFunction(Function &F) {
Misha Brukman03a11342004-02-28 03:33:01 +000069 LoopInfo &LI = getAnalysis<LoopInfo>();
70
Chris Lattner2f155d82004-03-15 00:02:02 +000071 // If this function has no loops, there is nothing to do.
72 if (LI.begin() == LI.end())
Misha Brukman03a11342004-02-28 03:33:01 +000073 return false;
74
Chris Lattnere9235d22004-03-18 03:48:06 +000075 DominatorSet &DS = getAnalysis<DominatorSet>();
76
Chris Lattner2f155d82004-03-15 00:02:02 +000077 // If there is more than one top-level loop in this function, extract all of
78 // the loops.
Misha Brukman03a11342004-02-28 03:33:01 +000079 bool Changed = false;
Chris Lattner2f155d82004-03-15 00:02:02 +000080 if (LI.end()-LI.begin() > 1) {
81 for (LoopInfo::iterator i = LI.begin(), e = LI.end(); i != e; ++i) {
82 if (NumLoops == 0) return Changed;
83 --NumLoops;
Chris Lattnere9235d22004-03-18 03:48:06 +000084 Changed |= ExtractLoop(DS, *i) != 0;
Chris Lattnere8369352004-03-18 05:46:10 +000085 ++NumExtracted;
Chris Lattner2f155d82004-03-15 00:02:02 +000086 }
87 } else {
88 // Otherwise there is exactly one top-level loop. If this function is more
89 // than a minimal wrapper around the loop, extract the loop.
90 Loop *TLL = *LI.begin();
91 bool ShouldExtractLoop = false;
Misha Brukmanb1c93172005-04-21 23:48:37 +000092
Chris Lattner2f155d82004-03-15 00:02:02 +000093 // Extract the loop if the entry block doesn't branch to the loop header.
94 TerminatorInst *EntryTI = F.getEntryBlock().getTerminator();
95 if (!isa<BranchInst>(EntryTI) ||
Misha Brukmanb1c93172005-04-21 23:48:37 +000096 !cast<BranchInst>(EntryTI)->isUnconditional() ||
Chris Lattner2f155d82004-03-15 00:02:02 +000097 EntryTI->getSuccessor(0) != TLL->getHeader())
98 ShouldExtractLoop = true;
99 else {
100 // Check to see if any exits from the loop are more than just return
101 // blocks.
Chris Lattnerd72c3eb2004-04-18 22:14:10 +0000102 std::vector<BasicBlock*> ExitBlocks;
103 TLL->getExitBlocks(ExitBlocks);
104 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
105 if (!isa<ReturnInst>(ExitBlocks[i]->getTerminator())) {
Chris Lattner2f155d82004-03-15 00:02:02 +0000106 ShouldExtractLoop = true;
107 break;
108 }
109 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000110
Chris Lattner2f155d82004-03-15 00:02:02 +0000111 if (ShouldExtractLoop) {
112 if (NumLoops == 0) return Changed;
113 --NumLoops;
Chris Lattnere9235d22004-03-18 03:48:06 +0000114 Changed |= ExtractLoop(DS, TLL) != 0;
Chris Lattnere8369352004-03-18 05:46:10 +0000115 ++NumExtracted;
Chris Lattner2f155d82004-03-15 00:02:02 +0000116 } else {
117 // Okay, this function is a minimal container around the specified loop.
118 // If we extract the loop, we will continue to just keep extracting it
119 // infinitely... so don't extract it. However, if the loop contains any
120 // subloops, extract them.
121 for (Loop::iterator i = TLL->begin(), e = TLL->end(); i != e; ++i) {
122 if (NumLoops == 0) return Changed;
123 --NumLoops;
Chris Lattnere9235d22004-03-18 03:48:06 +0000124 Changed |= ExtractLoop(DS, *i) != 0;
Chris Lattnere8369352004-03-18 05:46:10 +0000125 ++NumExtracted;
Chris Lattner2f155d82004-03-15 00:02:02 +0000126 }
127 }
Chris Lattnera1672c12004-03-14 20:01:36 +0000128 }
Misha Brukman03a11342004-02-28 03:33:01 +0000129
130 return Changed;
131}
132
Chris Lattnera1672c12004-03-14 20:01:36 +0000133// createSingleLoopExtractorPass - This pass extracts one natural loop from the
134// program into a function if it can. This is used by bugpoint.
135//
Jeff Cohen3e62e7c2005-01-10 04:23:32 +0000136FunctionPass *llvm::createSingleLoopExtractorPass() {
Chris Lattnera1672c12004-03-14 20:01:36 +0000137 return new SingleLoopExtractor();
Misha Brukman03a11342004-02-28 03:33:01 +0000138}
Chris Lattner7386e632004-08-13 03:05:17 +0000139
140
141namespace {
142 /// BlockExtractorPass - This pass is used by bugpoint to extract all blocks
143 /// from the module into their own functions except for those specified by the
144 /// BlocksToNotExtract list.
Chris Lattner4f2cf032004-09-20 04:48:05 +0000145 class BlockExtractorPass : public ModulePass {
Chris Lattner7386e632004-08-13 03:05:17 +0000146 std::vector<BasicBlock*> BlocksToNotExtract;
147 public:
148 BlockExtractorPass(std::vector<BasicBlock*> &B) : BlocksToNotExtract(B) {}
149 BlockExtractorPass() {}
150
Chris Lattner4f2cf032004-09-20 04:48:05 +0000151 bool runOnModule(Module &M);
Chris Lattner7386e632004-08-13 03:05:17 +0000152 };
Chris Lattnerc2d3d312006-08-27 22:42:52 +0000153 RegisterPass<BlockExtractorPass>
Chris Lattner7386e632004-08-13 03:05:17 +0000154 XX("extract-blocks", "Extract Basic Blocks From Module (for bugpoint use)");
155}
156
157// createBlockExtractorPass - This pass extracts all blocks (except those
158// specified in the argument list) from the functions in the module.
159//
Chris Lattner4f2cf032004-09-20 04:48:05 +0000160ModulePass *llvm::createBlockExtractorPass(std::vector<BasicBlock*> &BTNE) {
Chris Lattner7386e632004-08-13 03:05:17 +0000161 return new BlockExtractorPass(BTNE);
162}
163
Chris Lattner4f2cf032004-09-20 04:48:05 +0000164bool BlockExtractorPass::runOnModule(Module &M) {
Chris Lattner7386e632004-08-13 03:05:17 +0000165 std::set<BasicBlock*> TranslatedBlocksToNotExtract;
166 for (unsigned i = 0, e = BlocksToNotExtract.size(); i != e; ++i) {
167 BasicBlock *BB = BlocksToNotExtract[i];
168 Function *F = BB->getParent();
169
170 // Map the corresponding function in this module.
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000171 Function *MF = M.getFunction(F->getName());
172 assert(MF->getFunctionType() == F->getFunctionType() && "Wrong function?");
Chris Lattner7386e632004-08-13 03:05:17 +0000173
174 // Figure out which index the basic block is in its function.
175 Function::iterator BBI = MF->begin();
176 std::advance(BBI, std::distance(F->begin(), Function::iterator(BB)));
177 TranslatedBlocksToNotExtract.insert(BBI);
178 }
179
180 // Now that we know which blocks to not extract, figure out which ones we WANT
181 // to extract.
182 std::vector<BasicBlock*> BlocksToExtract;
183 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
184 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
185 if (!TranslatedBlocksToNotExtract.count(BB))
186 BlocksToExtract.push_back(BB);
187
188 for (unsigned i = 0, e = BlocksToExtract.size(); i != e; ++i)
189 ExtractBasicBlock(BlocksToExtract[i]);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000190
Chris Lattner7386e632004-08-13 03:05:17 +0000191 return !BlocksToExtract.empty();
192}