blob: 39d0fca94f5d6647543efb7742228d35113403c6 [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 Lattner78a996a2004-03-14 02:37:16 +000017#include "llvm/Transforms/IPO.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000018#include "llvm/Instructions.h"
Misha Brukman03a11342004-02-28 03:33:01 +000019#include "llvm/Module.h"
20#include "llvm/Pass.h"
Chris Lattnere9235d22004-03-18 03:48:06 +000021#include "llvm/Analysis/Dominators.h"
Misha Brukman03a11342004-02-28 03:33:01 +000022#include "llvm/Analysis/LoopInfo.h"
Chris Lattner6c3e8c72004-03-14 04:01:06 +000023#include "llvm/Transforms/Scalar.h"
Misha Brukman03a11342004-02-28 03:33:01 +000024#include "llvm/Transforms/Utils/FunctionUtils.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000025#include "llvm/ADT/Statistic.h"
Misha Brukman03a11342004-02-28 03:33:01 +000026using namespace llvm;
27
28namespace {
Chris Lattnere8369352004-03-18 05:46:10 +000029 Statistic<> NumExtracted("loop-extract", "Number of loops extracted");
Misha Brukmanb1c93172005-04-21 23:48:37 +000030
Chris Lattnera1672c12004-03-14 20:01:36 +000031 // FIXME: This is not a function pass, but the PassManager doesn't allow
32 // Module passes to require FunctionPasses, so we can't get loop info if we're
33 // not a function pass.
Chris Lattner692a47a2004-03-14 02:34:07 +000034 struct LoopExtractor : public FunctionPass {
Chris Lattnera1672c12004-03-14 20:01:36 +000035 unsigned NumLoops;
36
37 LoopExtractor(unsigned numLoops = ~0) : NumLoops(numLoops) {}
38
Chris Lattner692a47a2004-03-14 02:34:07 +000039 virtual bool runOnFunction(Function &F);
Misha Brukmanb1c93172005-04-21 23:48:37 +000040
Chris Lattner692a47a2004-03-14 02:34:07 +000041 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner5bce0c82004-03-18 05:43:18 +000042 AU.addRequiredID(BreakCriticalEdgesID);
43 AU.addRequiredID(LoopSimplifyID);
Chris Lattnere9235d22004-03-18 03:48:06 +000044 AU.addRequired<DominatorSet>();
Chris Lattner692a47a2004-03-14 02:34:07 +000045 AU.addRequired<LoopInfo>();
46 }
47 };
Misha Brukman03a11342004-02-28 03:33:01 +000048
Misha Brukmanb1c93172005-04-21 23:48:37 +000049 RegisterOpt<LoopExtractor>
Chris Lattner692a47a2004-03-14 02:34:07 +000050 X("loop-extract", "Extract loops into new functions");
Chris Lattnera1672c12004-03-14 20:01:36 +000051
52 /// SingleLoopExtractor - For bugpoint.
53 struct SingleLoopExtractor : public LoopExtractor {
54 SingleLoopExtractor() : LoopExtractor(1) {}
55 };
56
Misha Brukmanb1c93172005-04-21 23:48:37 +000057 RegisterOpt<SingleLoopExtractor>
Chris Lattnera1672c12004-03-14 20:01:36 +000058 Y("loop-extract-single", "Extract at most one loop into a new function");
Misha Brukmanb1c93172005-04-21 23:48:37 +000059} // End anonymous namespace
Misha Brukman03a11342004-02-28 03:33:01 +000060
Jeff Cohen677babc2005-01-08 17:21:40 +000061// createLoopExtractorPass - This pass extracts all natural loops from the
62// program into a function if it can.
63//
Jeff Cohen3e62e7c2005-01-10 04:23:32 +000064FunctionPass *llvm::createLoopExtractorPass() { return new LoopExtractor(); }
Jeff Cohen677babc2005-01-08 17:21:40 +000065
Misha Brukman03a11342004-02-28 03:33:01 +000066bool LoopExtractor::runOnFunction(Function &F) {
Misha Brukman03a11342004-02-28 03:33:01 +000067 LoopInfo &LI = getAnalysis<LoopInfo>();
68
Chris Lattner2f155d82004-03-15 00:02:02 +000069 // If this function has no loops, there is nothing to do.
70 if (LI.begin() == LI.end())
Misha Brukman03a11342004-02-28 03:33:01 +000071 return false;
72
Chris Lattnere9235d22004-03-18 03:48:06 +000073 DominatorSet &DS = getAnalysis<DominatorSet>();
74
Chris Lattner2f155d82004-03-15 00:02:02 +000075 // If there is more than one top-level loop in this function, extract all of
76 // the loops.
Misha Brukman03a11342004-02-28 03:33:01 +000077 bool Changed = false;
Chris Lattner2f155d82004-03-15 00:02:02 +000078 if (LI.end()-LI.begin() > 1) {
79 for (LoopInfo::iterator i = LI.begin(), e = LI.end(); i != e; ++i) {
80 if (NumLoops == 0) return Changed;
81 --NumLoops;
Chris Lattnere9235d22004-03-18 03:48:06 +000082 Changed |= ExtractLoop(DS, *i) != 0;
Chris Lattnere8369352004-03-18 05:46:10 +000083 ++NumExtracted;
Chris Lattner2f155d82004-03-15 00:02:02 +000084 }
85 } else {
86 // Otherwise there is exactly one top-level loop. If this function is more
87 // than a minimal wrapper around the loop, extract the loop.
88 Loop *TLL = *LI.begin();
89 bool ShouldExtractLoop = false;
Misha Brukmanb1c93172005-04-21 23:48:37 +000090
Chris Lattner2f155d82004-03-15 00:02:02 +000091 // Extract the loop if the entry block doesn't branch to the loop header.
92 TerminatorInst *EntryTI = F.getEntryBlock().getTerminator();
93 if (!isa<BranchInst>(EntryTI) ||
Misha Brukmanb1c93172005-04-21 23:48:37 +000094 !cast<BranchInst>(EntryTI)->isUnconditional() ||
Chris Lattner2f155d82004-03-15 00:02:02 +000095 EntryTI->getSuccessor(0) != TLL->getHeader())
96 ShouldExtractLoop = true;
97 else {
98 // Check to see if any exits from the loop are more than just return
99 // blocks.
Chris Lattnerd72c3eb2004-04-18 22:14:10 +0000100 std::vector<BasicBlock*> ExitBlocks;
101 TLL->getExitBlocks(ExitBlocks);
102 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
103 if (!isa<ReturnInst>(ExitBlocks[i]->getTerminator())) {
Chris Lattner2f155d82004-03-15 00:02:02 +0000104 ShouldExtractLoop = true;
105 break;
106 }
107 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000108
Chris Lattner2f155d82004-03-15 00:02:02 +0000109 if (ShouldExtractLoop) {
110 if (NumLoops == 0) return Changed;
111 --NumLoops;
Chris Lattnere9235d22004-03-18 03:48:06 +0000112 Changed |= ExtractLoop(DS, TLL) != 0;
Chris Lattnere8369352004-03-18 05:46:10 +0000113 ++NumExtracted;
Chris Lattner2f155d82004-03-15 00:02:02 +0000114 } else {
115 // Okay, this function is a minimal container around the specified loop.
116 // If we extract the loop, we will continue to just keep extracting it
117 // infinitely... so don't extract it. However, if the loop contains any
118 // subloops, extract them.
119 for (Loop::iterator i = TLL->begin(), e = TLL->end(); i != e; ++i) {
120 if (NumLoops == 0) return Changed;
121 --NumLoops;
Chris Lattnere9235d22004-03-18 03:48:06 +0000122 Changed |= ExtractLoop(DS, *i) != 0;
Chris Lattnere8369352004-03-18 05:46:10 +0000123 ++NumExtracted;
Chris Lattner2f155d82004-03-15 00:02:02 +0000124 }
125 }
Chris Lattnera1672c12004-03-14 20:01:36 +0000126 }
Misha Brukman03a11342004-02-28 03:33:01 +0000127
128 return Changed;
129}
130
Chris Lattnera1672c12004-03-14 20:01:36 +0000131// createSingleLoopExtractorPass - This pass extracts one natural loop from the
132// program into a function if it can. This is used by bugpoint.
133//
Jeff Cohen3e62e7c2005-01-10 04:23:32 +0000134FunctionPass *llvm::createSingleLoopExtractorPass() {
Chris Lattnera1672c12004-03-14 20:01:36 +0000135 return new SingleLoopExtractor();
Misha Brukman03a11342004-02-28 03:33:01 +0000136}
Chris Lattner7386e632004-08-13 03:05:17 +0000137
138
139namespace {
140 /// BlockExtractorPass - This pass is used by bugpoint to extract all blocks
141 /// from the module into their own functions except for those specified by the
142 /// BlocksToNotExtract list.
Chris Lattner4f2cf032004-09-20 04:48:05 +0000143 class BlockExtractorPass : public ModulePass {
Chris Lattner7386e632004-08-13 03:05:17 +0000144 std::vector<BasicBlock*> BlocksToNotExtract;
145 public:
146 BlockExtractorPass(std::vector<BasicBlock*> &B) : BlocksToNotExtract(B) {}
147 BlockExtractorPass() {}
148
Chris Lattner4f2cf032004-09-20 04:48:05 +0000149 bool runOnModule(Module &M);
Chris Lattner7386e632004-08-13 03:05:17 +0000150 };
151 RegisterOpt<BlockExtractorPass>
152 XX("extract-blocks", "Extract Basic Blocks From Module (for bugpoint use)");
153}
154
155// createBlockExtractorPass - This pass extracts all blocks (except those
156// specified in the argument list) from the functions in the module.
157//
Chris Lattner4f2cf032004-09-20 04:48:05 +0000158ModulePass *llvm::createBlockExtractorPass(std::vector<BasicBlock*> &BTNE) {
Chris Lattner7386e632004-08-13 03:05:17 +0000159 return new BlockExtractorPass(BTNE);
160}
161
Chris Lattner4f2cf032004-09-20 04:48:05 +0000162bool BlockExtractorPass::runOnModule(Module &M) {
Chris Lattner7386e632004-08-13 03:05:17 +0000163 std::set<BasicBlock*> TranslatedBlocksToNotExtract;
164 for (unsigned i = 0, e = BlocksToNotExtract.size(); i != e; ++i) {
165 BasicBlock *BB = BlocksToNotExtract[i];
166 Function *F = BB->getParent();
167
168 // Map the corresponding function in this module.
169 Function *MF = M.getFunction(F->getName(), F->getFunctionType());
170
171 // Figure out which index the basic block is in its function.
172 Function::iterator BBI = MF->begin();
173 std::advance(BBI, std::distance(F->begin(), Function::iterator(BB)));
174 TranslatedBlocksToNotExtract.insert(BBI);
175 }
176
177 // Now that we know which blocks to not extract, figure out which ones we WANT
178 // to extract.
179 std::vector<BasicBlock*> BlocksToExtract;
180 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
181 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
182 if (!TranslatedBlocksToNotExtract.count(BB))
183 BlocksToExtract.push_back(BB);
184
185 for (unsigned i = 0, e = BlocksToExtract.size(); i != e; ++i)
186 ExtractBasicBlock(BlocksToExtract[i]);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000187
Chris Lattner7386e632004-08-13 03:05:17 +0000188 return !BlocksToExtract.empty();
189}