blob: 10ec3065af7ca81ecbb640aac141f2ef4352a215 [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
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"
Nick Lewyckyc6243022007-11-14 06:47:06 +000024#include "llvm/Support/CommandLine.h"
Reid Spencer557ab152007-02-05 23:32:05 +000025#include "llvm/Support/Compiler.h"
Chris Lattner6c3e8c72004-03-14 04:01:06 +000026#include "llvm/Transforms/Scalar.h"
Misha Brukman03a11342004-02-28 03:33:01 +000027#include "llvm/Transforms/Utils/FunctionUtils.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000028#include "llvm/ADT/Statistic.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
Chris Lattner1631bcb2006-12-19 22:09:18 +000033STATISTIC(NumExtracted, "Number of loops extracted");
Misha Brukmanb1c93172005-04-21 23:48:37 +000034
Chris Lattner1631bcb2006-12-19 22:09:18 +000035namespace {
Chris Lattnera1672c12004-03-14 20:01:36 +000036 // FIXME: This is not a function pass, but the PassManager doesn't allow
37 // Module passes to require FunctionPasses, so we can't get loop info if we're
38 // not a function pass.
Reid Spencer557ab152007-02-05 23:32:05 +000039 struct VISIBILITY_HIDDEN LoopExtractor : public FunctionPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000040 static char ID; // Pass identification, replacement for typeid
Chris Lattnera1672c12004-03-14 20:01:36 +000041 unsigned NumLoops;
42
Dan Gohman34d442f2007-08-01 15:32:29 +000043 explicit LoopExtractor(unsigned numLoops = ~0)
Devang Patel09f162c2007-05-01 21:15:47 +000044 : FunctionPass((intptr_t)&ID), NumLoops(numLoops) {}
Chris Lattnera1672c12004-03-14 20:01:36 +000045
Chris Lattner692a47a2004-03-14 02:34:07 +000046 virtual bool runOnFunction(Function &F);
Misha Brukmanb1c93172005-04-21 23:48:37 +000047
Chris Lattner692a47a2004-03-14 02:34:07 +000048 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner5bce0c82004-03-18 05:43:18 +000049 AU.addRequiredID(BreakCriticalEdgesID);
50 AU.addRequiredID(LoopSimplifyID);
Owen Andersonf095bf32007-04-07 05:31:27 +000051 AU.addRequired<DominatorTree>();
Chris Lattner692a47a2004-03-14 02:34:07 +000052 AU.addRequired<LoopInfo>();
53 }
54 };
Dan Gohmand78c4002008-05-13 00:00:25 +000055}
Misha Brukman03a11342004-02-28 03:33:01 +000056
Dan Gohmand78c4002008-05-13 00:00:25 +000057char LoopExtractor::ID = 0;
58static RegisterPass<LoopExtractor>
59X("loop-extract", "Extract loops into new functions");
Chris Lattnera1672c12004-03-14 20:01:36 +000060
Dan Gohmand78c4002008-05-13 00:00:25 +000061namespace {
Chris Lattnera1672c12004-03-14 20:01:36 +000062 /// SingleLoopExtractor - For bugpoint.
63 struct SingleLoopExtractor : public LoopExtractor {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000064 static char ID; // Pass identification, replacement for typeid
Chris Lattnera1672c12004-03-14 20:01:36 +000065 SingleLoopExtractor() : LoopExtractor(1) {}
66 };
Misha Brukmanb1c93172005-04-21 23:48:37 +000067} // End anonymous namespace
Misha Brukman03a11342004-02-28 03:33:01 +000068
Dan Gohmand78c4002008-05-13 00:00:25 +000069char SingleLoopExtractor::ID = 0;
70static RegisterPass<SingleLoopExtractor>
71Y("loop-extract-single", "Extract at most one loop into a new function");
72
Jeff Cohen677babc2005-01-08 17:21:40 +000073// createLoopExtractorPass - This pass extracts all natural loops from the
74// program into a function if it can.
75//
Jeff Cohen3e62e7c2005-01-10 04:23:32 +000076FunctionPass *llvm::createLoopExtractorPass() { return new LoopExtractor(); }
Jeff Cohen677babc2005-01-08 17:21:40 +000077
Misha Brukman03a11342004-02-28 03:33:01 +000078bool LoopExtractor::runOnFunction(Function &F) {
Misha Brukman03a11342004-02-28 03:33:01 +000079 LoopInfo &LI = getAnalysis<LoopInfo>();
80
Chris Lattner2f155d82004-03-15 00:02:02 +000081 // If this function has no loops, there is nothing to do.
82 if (LI.begin() == LI.end())
Misha Brukman03a11342004-02-28 03:33:01 +000083 return false;
84
Owen Andersonf095bf32007-04-07 05:31:27 +000085 DominatorTree &DT = getAnalysis<DominatorTree>();
Chris Lattnere9235d22004-03-18 03:48:06 +000086
Chris Lattner2f155d82004-03-15 00:02:02 +000087 // If there is more than one top-level loop in this function, extract all of
88 // the loops.
Misha Brukman03a11342004-02-28 03:33:01 +000089 bool Changed = false;
Chris Lattner2f155d82004-03-15 00:02:02 +000090 if (LI.end()-LI.begin() > 1) {
91 for (LoopInfo::iterator i = LI.begin(), e = LI.end(); i != e; ++i) {
92 if (NumLoops == 0) return Changed;
93 --NumLoops;
Devang Patelcf470e52007-06-07 22:17:16 +000094 Changed |= ExtractLoop(DT, *i) != 0;
Chris Lattnere8369352004-03-18 05:46:10 +000095 ++NumExtracted;
Chris Lattner2f155d82004-03-15 00:02:02 +000096 }
97 } else {
98 // Otherwise there is exactly one top-level loop. If this function is more
99 // than a minimal wrapper around the loop, extract the loop.
100 Loop *TLL = *LI.begin();
101 bool ShouldExtractLoop = false;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000102
Chris Lattner2f155d82004-03-15 00:02:02 +0000103 // Extract the loop if the entry block doesn't branch to the loop header.
104 TerminatorInst *EntryTI = F.getEntryBlock().getTerminator();
105 if (!isa<BranchInst>(EntryTI) ||
Misha Brukmanb1c93172005-04-21 23:48:37 +0000106 !cast<BranchInst>(EntryTI)->isUnconditional() ||
Chris Lattner2f155d82004-03-15 00:02:02 +0000107 EntryTI->getSuccessor(0) != TLL->getHeader())
108 ShouldExtractLoop = true;
109 else {
110 // Check to see if any exits from the loop are more than just return
111 // blocks.
Devang Patelb5933bb2007-08-21 00:31:24 +0000112 SmallVector<BasicBlock*, 8> ExitBlocks;
Chris Lattnerd72c3eb2004-04-18 22:14:10 +0000113 TLL->getExitBlocks(ExitBlocks);
114 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
115 if (!isa<ReturnInst>(ExitBlocks[i]->getTerminator())) {
Chris Lattner2f155d82004-03-15 00:02:02 +0000116 ShouldExtractLoop = true;
117 break;
118 }
119 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000120
Chris Lattner2f155d82004-03-15 00:02:02 +0000121 if (ShouldExtractLoop) {
122 if (NumLoops == 0) return Changed;
123 --NumLoops;
Devang Patelcf470e52007-06-07 22:17:16 +0000124 Changed |= ExtractLoop(DT, TLL) != 0;
Chris Lattnere8369352004-03-18 05:46:10 +0000125 ++NumExtracted;
Chris Lattner2f155d82004-03-15 00:02:02 +0000126 } else {
127 // Okay, this function is a minimal container around the specified loop.
128 // If we extract the loop, we will continue to just keep extracting it
129 // infinitely... so don't extract it. However, if the loop contains any
130 // subloops, extract them.
131 for (Loop::iterator i = TLL->begin(), e = TLL->end(); i != e; ++i) {
132 if (NumLoops == 0) return Changed;
133 --NumLoops;
Devang Patelcf470e52007-06-07 22:17:16 +0000134 Changed |= ExtractLoop(DT, *i) != 0;
Chris Lattnere8369352004-03-18 05:46:10 +0000135 ++NumExtracted;
Chris Lattner2f155d82004-03-15 00:02:02 +0000136 }
137 }
Chris Lattnera1672c12004-03-14 20:01:36 +0000138 }
Misha Brukman03a11342004-02-28 03:33:01 +0000139
140 return Changed;
141}
142
Chris Lattnera1672c12004-03-14 20:01:36 +0000143// createSingleLoopExtractorPass - This pass extracts one natural loop from the
144// program into a function if it can. This is used by bugpoint.
145//
Jeff Cohen3e62e7c2005-01-10 04:23:32 +0000146FunctionPass *llvm::createSingleLoopExtractorPass() {
Chris Lattnera1672c12004-03-14 20:01:36 +0000147 return new SingleLoopExtractor();
Misha Brukman03a11342004-02-28 03:33:01 +0000148}
Chris Lattner7386e632004-08-13 03:05:17 +0000149
150
Dan Gohmand78c4002008-05-13 00:00:25 +0000151// BlockFile - A file which contains a list of blocks that should not be
152// extracted.
153static cl::opt<std::string>
154BlockFile("extract-blocks-file", cl::value_desc("filename"),
155 cl::desc("A file containing list of basic blocks to not extract"),
156 cl::Hidden);
Nick Lewyckyc6243022007-11-14 06:47:06 +0000157
Dan Gohmand78c4002008-05-13 00:00:25 +0000158namespace {
Chris Lattner7386e632004-08-13 03:05:17 +0000159 /// BlockExtractorPass - This pass is used by bugpoint to extract all blocks
160 /// from the module into their own functions except for those specified by the
161 /// BlocksToNotExtract list.
Chris Lattner4f2cf032004-09-20 04:48:05 +0000162 class BlockExtractorPass : public ModulePass {
Nick Lewyckyc6243022007-11-14 06:47:06 +0000163 void LoadFile(const char *Filename);
164
Chris Lattner7386e632004-08-13 03:05:17 +0000165 std::vector<BasicBlock*> BlocksToNotExtract;
Nick Lewyckyc6243022007-11-14 06:47:06 +0000166 std::vector<std::pair<std::string, std::string> > BlocksToNotExtractByName;
Chris Lattner7386e632004-08-13 03:05:17 +0000167 public:
Nick Lewyckye7da2d62007-05-06 13:37:16 +0000168 static char ID; // Pass identification, replacement for typeid
Gordon Henriksen2ed067a2007-11-05 01:54:05 +0000169 explicit BlockExtractorPass(const std::vector<BasicBlock*> &B)
Nick Lewyckyc6243022007-11-14 06:47:06 +0000170 : ModulePass((intptr_t)&ID), BlocksToNotExtract(B) {
171 if (!BlockFile.empty())
172 LoadFile(BlockFile.c_str());
173 }
Devang Patel09f162c2007-05-01 21:15:47 +0000174 BlockExtractorPass() : ModulePass((intptr_t)&ID) {}
Chris Lattner7386e632004-08-13 03:05:17 +0000175
Chris Lattner4f2cf032004-09-20 04:48:05 +0000176 bool runOnModule(Module &M);
Chris Lattner7386e632004-08-13 03:05:17 +0000177 };
Chris Lattner7386e632004-08-13 03:05:17 +0000178}
179
Dan Gohmand78c4002008-05-13 00:00:25 +0000180char BlockExtractorPass::ID = 0;
181static RegisterPass<BlockExtractorPass>
182XX("extract-blocks", "Extract Basic Blocks From Module (for bugpoint use)");
183
Chris Lattner7386e632004-08-13 03:05:17 +0000184// createBlockExtractorPass - This pass extracts all blocks (except those
185// specified in the argument list) from the functions in the module.
186//
Gordon Henriksen2ed067a2007-11-05 01:54:05 +0000187ModulePass *llvm::createBlockExtractorPass(const std::vector<BasicBlock*> &BTNE)
188{
Chris Lattner7386e632004-08-13 03:05:17 +0000189 return new BlockExtractorPass(BTNE);
190}
191
Nick Lewyckyc6243022007-11-14 06:47:06 +0000192void BlockExtractorPass::LoadFile(const char *Filename) {
193 // Load the BlockFile...
194 std::ifstream In(Filename);
195 if (!In.good()) {
196 cerr << "WARNING: BlockExtractor couldn't load file '" << Filename
197 << "'!\n";
198 return;
199 }
200 while (In) {
201 std::string FunctionName, BlockName;
202 In >> FunctionName;
203 In >> BlockName;
204 if (!BlockName.empty())
205 BlocksToNotExtractByName.push_back(
206 std::make_pair(FunctionName, BlockName));
207 }
208}
209
Chris Lattner4f2cf032004-09-20 04:48:05 +0000210bool BlockExtractorPass::runOnModule(Module &M) {
Chris Lattner7386e632004-08-13 03:05:17 +0000211 std::set<BasicBlock*> TranslatedBlocksToNotExtract;
212 for (unsigned i = 0, e = BlocksToNotExtract.size(); i != e; ++i) {
213 BasicBlock *BB = BlocksToNotExtract[i];
214 Function *F = BB->getParent();
215
216 // Map the corresponding function in this module.
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000217 Function *MF = M.getFunction(F->getName());
218 assert(MF->getFunctionType() == F->getFunctionType() && "Wrong function?");
Chris Lattner7386e632004-08-13 03:05:17 +0000219
220 // Figure out which index the basic block is in its function.
221 Function::iterator BBI = MF->begin();
222 std::advance(BBI, std::distance(F->begin(), Function::iterator(BB)));
223 TranslatedBlocksToNotExtract.insert(BBI);
224 }
225
Nick Lewyckyc6243022007-11-14 06:47:06 +0000226 while (!BlocksToNotExtractByName.empty()) {
227 // There's no way to find BBs by name without looking at every BB inside
228 // every Function. Fortunately, this is always empty except when used by
229 // bugpoint in which case correctness is more important than performance.
230
231 std::string &FuncName = BlocksToNotExtractByName.back().first;
232 std::string &BlockName = BlocksToNotExtractByName.back().second;
233
234 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
235 Function &F = *FI;
236 if (F.getName() != FuncName) continue;
237
238 for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) {
239 BasicBlock &BB = *BI;
240 if (BB.getName() != BlockName) continue;
241
242 TranslatedBlocksToNotExtract.insert(BI);
243 }
244 }
245
246 BlocksToNotExtractByName.pop_back();
247 }
248
Chris Lattner7386e632004-08-13 03:05:17 +0000249 // Now that we know which blocks to not extract, figure out which ones we WANT
250 // to extract.
251 std::vector<BasicBlock*> BlocksToExtract;
252 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
253 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
254 if (!TranslatedBlocksToNotExtract.count(BB))
255 BlocksToExtract.push_back(BB);
256
257 for (unsigned i = 0, e = BlocksToExtract.size(); i != e; ++i)
258 ExtractBasicBlock(BlocksToExtract[i]);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000259
Chris Lattner7386e632004-08-13 03:05:17 +0000260 return !BlocksToExtract.empty();
261}