blob: 5e9167b11baf54e2d83d801be84b23a167be7379 [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"
Dan Gohman9a7320c2009-09-28 14:37:51 +000023#include "llvm/Analysis/LoopPass.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 {
Nick Lewycky02d5f772009-10-25 06:33:48 +000036 struct LoopExtractor : public LoopPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000037 static char ID; // Pass identification, replacement for typeid
Chris Lattnera1672c12004-03-14 20:01:36 +000038 unsigned NumLoops;
39
Dan Gohman34d442f2007-08-01 15:32:29 +000040 explicit LoopExtractor(unsigned numLoops = ~0)
Dan Gohman9a7320c2009-09-28 14:37:51 +000041 : LoopPass(&ID), NumLoops(numLoops) {}
Chris Lattnera1672c12004-03-14 20:01:36 +000042
Dan Gohman9a7320c2009-09-28 14:37:51 +000043 virtual bool runOnLoop(Loop *L, LPPassManager &LPM);
Misha Brukmanb1c93172005-04-21 23:48:37 +000044
Chris Lattner692a47a2004-03-14 02:34:07 +000045 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner5bce0c82004-03-18 05:43:18 +000046 AU.addRequiredID(BreakCriticalEdgesID);
47 AU.addRequiredID(LoopSimplifyID);
Owen Andersonf095bf32007-04-07 05:31:27 +000048 AU.addRequired<DominatorTree>();
Chris Lattner692a47a2004-03-14 02:34:07 +000049 }
50 };
Dan Gohmand78c4002008-05-13 00:00:25 +000051}
Misha Brukman03a11342004-02-28 03:33:01 +000052
Dan Gohmand78c4002008-05-13 00:00:25 +000053char LoopExtractor::ID = 0;
54static RegisterPass<LoopExtractor>
55X("loop-extract", "Extract loops into new functions");
Chris Lattnera1672c12004-03-14 20:01:36 +000056
Dan Gohmand78c4002008-05-13 00:00:25 +000057namespace {
Chris Lattnera1672c12004-03-14 20:01:36 +000058 /// SingleLoopExtractor - For bugpoint.
59 struct SingleLoopExtractor : public LoopExtractor {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000060 static char ID; // Pass identification, replacement for typeid
Chris Lattnera1672c12004-03-14 20:01:36 +000061 SingleLoopExtractor() : LoopExtractor(1) {}
62 };
Misha Brukmanb1c93172005-04-21 23:48:37 +000063} // End anonymous namespace
Misha Brukman03a11342004-02-28 03:33:01 +000064
Dan Gohmand78c4002008-05-13 00:00:25 +000065char SingleLoopExtractor::ID = 0;
66static RegisterPass<SingleLoopExtractor>
67Y("loop-extract-single", "Extract at most one loop into a new function");
68
Jeff Cohen677babc2005-01-08 17:21:40 +000069// createLoopExtractorPass - This pass extracts all natural loops from the
70// program into a function if it can.
71//
Dan Gohman9a7320c2009-09-28 14:37:51 +000072Pass *llvm::createLoopExtractorPass() { return new LoopExtractor(); }
Jeff Cohen677babc2005-01-08 17:21:40 +000073
Dan Gohman9a7320c2009-09-28 14:37:51 +000074bool LoopExtractor::runOnLoop(Loop *L, LPPassManager &LPM) {
75 // Only visit top-level loops.
76 if (L->getParentLoop())
Misha Brukman03a11342004-02-28 03:33:01 +000077 return false;
78
Owen Andersonf095bf32007-04-07 05:31:27 +000079 DominatorTree &DT = getAnalysis<DominatorTree>();
Dan Gohman9a7320c2009-09-28 14:37:51 +000080 bool Changed = false;
Chris Lattnere9235d22004-03-18 03:48:06 +000081
Chris Lattner2f155d82004-03-15 00:02:02 +000082 // If there is more than one top-level loop in this function, extract all of
Dan Gohman9a7320c2009-09-28 14:37:51 +000083 // the loops. Otherwise there is exactly one top-level loop; in this case if
84 // this function is more than a minimal wrapper around the loop, extract
85 // the loop.
86 bool ShouldExtractLoop = false;
Misha Brukmanb1c93172005-04-21 23:48:37 +000087
Dan Gohman9a7320c2009-09-28 14:37:51 +000088 // Extract the loop if the entry block doesn't branch to the loop header.
89 TerminatorInst *EntryTI =
90 L->getHeader()->getParent()->getEntryBlock().getTerminator();
91 if (!isa<BranchInst>(EntryTI) ||
92 !cast<BranchInst>(EntryTI)->isUnconditional() ||
93 EntryTI->getSuccessor(0) != L->getHeader())
94 ShouldExtractLoop = true;
95 else {
96 // Check to see if any exits from the loop are more than just return
97 // blocks.
98 SmallVector<BasicBlock*, 8> ExitBlocks;
99 L->getExitBlocks(ExitBlocks);
100 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
101 if (!isa<ReturnInst>(ExitBlocks[i]->getTerminator())) {
102 ShouldExtractLoop = true;
103 break;
Chris Lattner2f155d82004-03-15 00:02:02 +0000104 }
Dan Gohman9a7320c2009-09-28 14:37:51 +0000105 }
106 if (ShouldExtractLoop) {
107 if (NumLoops == 0) return Changed;
108 --NumLoops;
109 if (ExtractLoop(DT, L) != 0) {
110 Changed = true;
111 // After extraction, the loop is replaced by a function call, so
112 // we shouldn't try to run any more loop passes on it.
113 LPM.deleteLoopFromQueue(L);
Chris Lattner2f155d82004-03-15 00:02:02 +0000114 }
Dan Gohman9a7320c2009-09-28 14:37:51 +0000115 ++NumExtracted;
Chris Lattnera1672c12004-03-14 20:01:36 +0000116 }
Misha Brukman03a11342004-02-28 03:33:01 +0000117
118 return Changed;
119}
120
Chris Lattnera1672c12004-03-14 20:01:36 +0000121// createSingleLoopExtractorPass - This pass extracts one natural loop from the
122// program into a function if it can. This is used by bugpoint.
123//
Dan Gohman9a7320c2009-09-28 14:37:51 +0000124Pass *llvm::createSingleLoopExtractorPass() {
Chris Lattnera1672c12004-03-14 20:01:36 +0000125 return new SingleLoopExtractor();
Misha Brukman03a11342004-02-28 03:33:01 +0000126}
Chris Lattner7386e632004-08-13 03:05:17 +0000127
128
Dan Gohmand78c4002008-05-13 00:00:25 +0000129// BlockFile - A file which contains a list of blocks that should not be
130// extracted.
131static cl::opt<std::string>
132BlockFile("extract-blocks-file", cl::value_desc("filename"),
133 cl::desc("A file containing list of basic blocks to not extract"),
134 cl::Hidden);
Nick Lewyckyc6243022007-11-14 06:47:06 +0000135
Dan Gohmand78c4002008-05-13 00:00:25 +0000136namespace {
Chris Lattner7386e632004-08-13 03:05:17 +0000137 /// BlockExtractorPass - This pass is used by bugpoint to extract all blocks
138 /// from the module into their own functions except for those specified by the
139 /// BlocksToNotExtract list.
Chris Lattner4f2cf032004-09-20 04:48:05 +0000140 class BlockExtractorPass : public ModulePass {
Nick Lewyckyc6243022007-11-14 06:47:06 +0000141 void LoadFile(const char *Filename);
142
Chris Lattner7386e632004-08-13 03:05:17 +0000143 std::vector<BasicBlock*> BlocksToNotExtract;
Nick Lewyckyc6243022007-11-14 06:47:06 +0000144 std::vector<std::pair<std::string, std::string> > BlocksToNotExtractByName;
Chris Lattner7386e632004-08-13 03:05:17 +0000145 public:
Nick Lewyckye7da2d62007-05-06 13:37:16 +0000146 static char ID; // Pass identification, replacement for typeid
Gordon Henriksen2ed067a2007-11-05 01:54:05 +0000147 explicit BlockExtractorPass(const std::vector<BasicBlock*> &B)
Dan Gohmana79db302008-09-04 17:05:41 +0000148 : ModulePass(&ID), BlocksToNotExtract(B) {
Nick Lewyckyc6243022007-11-14 06:47:06 +0000149 if (!BlockFile.empty())
150 LoadFile(BlockFile.c_str());
151 }
Dan Gohmana79db302008-09-04 17:05:41 +0000152 BlockExtractorPass() : ModulePass(&ID) {}
Chris Lattner7386e632004-08-13 03:05:17 +0000153
Chris Lattner4f2cf032004-09-20 04:48:05 +0000154 bool runOnModule(Module &M);
Chris Lattner7386e632004-08-13 03:05:17 +0000155 };
Chris Lattner7386e632004-08-13 03:05:17 +0000156}
157
Dan Gohmand78c4002008-05-13 00:00:25 +0000158char BlockExtractorPass::ID = 0;
159static RegisterPass<BlockExtractorPass>
160XX("extract-blocks", "Extract Basic Blocks From Module (for bugpoint use)");
161
Chris Lattner7386e632004-08-13 03:05:17 +0000162// createBlockExtractorPass - This pass extracts all blocks (except those
163// specified in the argument list) from the functions in the module.
164//
Gordon Henriksen2ed067a2007-11-05 01:54:05 +0000165ModulePass *llvm::createBlockExtractorPass(const std::vector<BasicBlock*> &BTNE)
166{
Chris Lattner7386e632004-08-13 03:05:17 +0000167 return new BlockExtractorPass(BTNE);
168}
169
Nick Lewyckyc6243022007-11-14 06:47:06 +0000170void BlockExtractorPass::LoadFile(const char *Filename) {
171 // Load the BlockFile...
172 std::ifstream In(Filename);
173 if (!In.good()) {
Chris Lattner4883d902009-08-23 07:19:13 +0000174 errs() << "WARNING: BlockExtractor couldn't load file '" << Filename
175 << "'!\n";
Nick Lewyckyc6243022007-11-14 06:47:06 +0000176 return;
177 }
178 while (In) {
179 std::string FunctionName, BlockName;
180 In >> FunctionName;
181 In >> BlockName;
182 if (!BlockName.empty())
183 BlocksToNotExtractByName.push_back(
184 std::make_pair(FunctionName, BlockName));
185 }
186}
187
Chris Lattner4f2cf032004-09-20 04:48:05 +0000188bool BlockExtractorPass::runOnModule(Module &M) {
Chris Lattner7386e632004-08-13 03:05:17 +0000189 std::set<BasicBlock*> TranslatedBlocksToNotExtract;
190 for (unsigned i = 0, e = BlocksToNotExtract.size(); i != e; ++i) {
191 BasicBlock *BB = BlocksToNotExtract[i];
192 Function *F = BB->getParent();
193
194 // Map the corresponding function in this module.
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000195 Function *MF = M.getFunction(F->getName());
196 assert(MF->getFunctionType() == F->getFunctionType() && "Wrong function?");
Chris Lattner7386e632004-08-13 03:05:17 +0000197
198 // Figure out which index the basic block is in its function.
199 Function::iterator BBI = MF->begin();
200 std::advance(BBI, std::distance(F->begin(), Function::iterator(BB)));
201 TranslatedBlocksToNotExtract.insert(BBI);
202 }
203
Nick Lewyckyc6243022007-11-14 06:47:06 +0000204 while (!BlocksToNotExtractByName.empty()) {
205 // There's no way to find BBs by name without looking at every BB inside
206 // every Function. Fortunately, this is always empty except when used by
207 // bugpoint in which case correctness is more important than performance.
208
209 std::string &FuncName = BlocksToNotExtractByName.back().first;
210 std::string &BlockName = BlocksToNotExtractByName.back().second;
211
212 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
213 Function &F = *FI;
214 if (F.getName() != FuncName) continue;
215
216 for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) {
217 BasicBlock &BB = *BI;
218 if (BB.getName() != BlockName) continue;
219
220 TranslatedBlocksToNotExtract.insert(BI);
221 }
222 }
223
224 BlocksToNotExtractByName.pop_back();
225 }
226
Chris Lattner7386e632004-08-13 03:05:17 +0000227 // Now that we know which blocks to not extract, figure out which ones we WANT
228 // to extract.
229 std::vector<BasicBlock*> BlocksToExtract;
230 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
231 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
232 if (!TranslatedBlocksToNotExtract.count(BB))
233 BlocksToExtract.push_back(BB);
234
235 for (unsigned i = 0, e = BlocksToExtract.size(); i != e; ++i)
236 ExtractBasicBlock(BlocksToExtract[i]);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000237
Chris Lattner7386e632004-08-13 03:05:17 +0000238 return !BlocksToExtract.empty();
239}