blob: cb813303facb9de95a7df8b993d63bfddbfcc96d [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"
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"
Nick Lewyckyc6243022007-11-14 06:47:06 +000028#include <fstream>
29#include <set>
Misha Brukman03a11342004-02-28 03:33:01 +000030using namespace llvm;
31
Chris Lattner1631bcb2006-12-19 22:09:18 +000032STATISTIC(NumExtracted, "Number of loops extracted");
Misha Brukmanb1c93172005-04-21 23:48:37 +000033
Chris Lattner1631bcb2006-12-19 22:09:18 +000034namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000035 struct LoopExtractor : public LoopPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000036 static char ID; // Pass identification, replacement for typeid
Chris Lattnera1672c12004-03-14 20:01:36 +000037 unsigned NumLoops;
38
Dan Gohman34d442f2007-08-01 15:32:29 +000039 explicit LoopExtractor(unsigned numLoops = ~0)
Dan Gohman9a7320c2009-09-28 14:37:51 +000040 : LoopPass(&ID), NumLoops(numLoops) {}
Chris Lattnera1672c12004-03-14 20:01:36 +000041
Dan Gohman9a7320c2009-09-28 14:37:51 +000042 virtual bool runOnLoop(Loop *L, LPPassManager &LPM);
Misha Brukmanb1c93172005-04-21 23:48:37 +000043
Chris Lattner692a47a2004-03-14 02:34:07 +000044 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner5bce0c82004-03-18 05:43:18 +000045 AU.addRequiredID(BreakCriticalEdgesID);
46 AU.addRequiredID(LoopSimplifyID);
Owen Andersonf095bf32007-04-07 05:31:27 +000047 AU.addRequired<DominatorTree>();
Chris Lattner692a47a2004-03-14 02:34:07 +000048 }
49 };
Dan Gohmand78c4002008-05-13 00:00:25 +000050}
Misha Brukman03a11342004-02-28 03:33:01 +000051
Dan Gohmand78c4002008-05-13 00:00:25 +000052char LoopExtractor::ID = 0;
53static RegisterPass<LoopExtractor>
54X("loop-extract", "Extract loops into new functions");
Chris Lattnera1672c12004-03-14 20:01:36 +000055
Dan Gohmand78c4002008-05-13 00:00:25 +000056namespace {
Chris Lattnera1672c12004-03-14 20:01:36 +000057 /// SingleLoopExtractor - For bugpoint.
58 struct SingleLoopExtractor : public LoopExtractor {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000059 static char ID; // Pass identification, replacement for typeid
Chris Lattnera1672c12004-03-14 20:01:36 +000060 SingleLoopExtractor() : LoopExtractor(1) {}
61 };
Misha Brukmanb1c93172005-04-21 23:48:37 +000062} // End anonymous namespace
Misha Brukman03a11342004-02-28 03:33:01 +000063
Dan Gohmand78c4002008-05-13 00:00:25 +000064char SingleLoopExtractor::ID = 0;
65static RegisterPass<SingleLoopExtractor>
66Y("loop-extract-single", "Extract at most one loop into a new function");
67
Jeff Cohen677babc2005-01-08 17:21:40 +000068// createLoopExtractorPass - This pass extracts all natural loops from the
69// program into a function if it can.
70//
Dan Gohman9a7320c2009-09-28 14:37:51 +000071Pass *llvm::createLoopExtractorPass() { return new LoopExtractor(); }
Jeff Cohen677babc2005-01-08 17:21:40 +000072
Dan Gohman9a7320c2009-09-28 14:37:51 +000073bool LoopExtractor::runOnLoop(Loop *L, LPPassManager &LPM) {
74 // Only visit top-level loops.
75 if (L->getParentLoop())
Misha Brukman03a11342004-02-28 03:33:01 +000076 return false;
77
Dan Gohmana83ac2d2009-11-05 21:11:53 +000078 // If LoopSimplify form is not available, stay out of trouble.
79 if (!L->isLoopSimplifyForm())
80 return false;
81
Owen Andersonf095bf32007-04-07 05:31:27 +000082 DominatorTree &DT = getAnalysis<DominatorTree>();
Dan Gohman9a7320c2009-09-28 14:37:51 +000083 bool Changed = false;
Chris Lattnere9235d22004-03-18 03:48:06 +000084
Chris Lattner2f155d82004-03-15 00:02:02 +000085 // If there is more than one top-level loop in this function, extract all of
Dan Gohman9a7320c2009-09-28 14:37:51 +000086 // the loops. Otherwise there is exactly one top-level loop; in this case if
87 // this function is more than a minimal wrapper around the loop, extract
88 // the loop.
89 bool ShouldExtractLoop = false;
Misha Brukmanb1c93172005-04-21 23:48:37 +000090
Dan Gohman9a7320c2009-09-28 14:37:51 +000091 // Extract the loop if the entry block doesn't branch to the loop header.
92 TerminatorInst *EntryTI =
93 L->getHeader()->getParent()->getEntryBlock().getTerminator();
94 if (!isa<BranchInst>(EntryTI) ||
95 !cast<BranchInst>(EntryTI)->isUnconditional() ||
96 EntryTI->getSuccessor(0) != L->getHeader())
97 ShouldExtractLoop = true;
98 else {
99 // Check to see if any exits from the loop are more than just return
100 // blocks.
101 SmallVector<BasicBlock*, 8> ExitBlocks;
102 L->getExitBlocks(ExitBlocks);
103 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
104 if (!isa<ReturnInst>(ExitBlocks[i]->getTerminator())) {
105 ShouldExtractLoop = true;
106 break;
Chris Lattner2f155d82004-03-15 00:02:02 +0000107 }
Dan Gohman9a7320c2009-09-28 14:37:51 +0000108 }
109 if (ShouldExtractLoop) {
110 if (NumLoops == 0) return Changed;
111 --NumLoops;
112 if (ExtractLoop(DT, L) != 0) {
113 Changed = true;
114 // After extraction, the loop is replaced by a function call, so
115 // we shouldn't try to run any more loop passes on it.
116 LPM.deleteLoopFromQueue(L);
Chris Lattner2f155d82004-03-15 00:02:02 +0000117 }
Dan Gohman9a7320c2009-09-28 14:37:51 +0000118 ++NumExtracted;
Chris Lattnera1672c12004-03-14 20:01:36 +0000119 }
Misha Brukman03a11342004-02-28 03:33:01 +0000120
121 return Changed;
122}
123
Chris Lattnera1672c12004-03-14 20:01:36 +0000124// createSingleLoopExtractorPass - This pass extracts one natural loop from the
125// program into a function if it can. This is used by bugpoint.
126//
Dan Gohman9a7320c2009-09-28 14:37:51 +0000127Pass *llvm::createSingleLoopExtractorPass() {
Chris Lattnera1672c12004-03-14 20:01:36 +0000128 return new SingleLoopExtractor();
Misha Brukman03a11342004-02-28 03:33:01 +0000129}
Chris Lattner7386e632004-08-13 03:05:17 +0000130
131
Dan Gohmand78c4002008-05-13 00:00:25 +0000132// BlockFile - A file which contains a list of blocks that should not be
133// extracted.
134static cl::opt<std::string>
135BlockFile("extract-blocks-file", cl::value_desc("filename"),
136 cl::desc("A file containing list of basic blocks to not extract"),
137 cl::Hidden);
Nick Lewyckyc6243022007-11-14 06:47:06 +0000138
Dan Gohmand78c4002008-05-13 00:00:25 +0000139namespace {
Chris Lattner7386e632004-08-13 03:05:17 +0000140 /// 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 {
Nick Lewyckyc6243022007-11-14 06:47:06 +0000144 void LoadFile(const char *Filename);
145
Chris Lattner7386e632004-08-13 03:05:17 +0000146 std::vector<BasicBlock*> BlocksToNotExtract;
Nick Lewyckyc6243022007-11-14 06:47:06 +0000147 std::vector<std::pair<std::string, std::string> > BlocksToNotExtractByName;
Chris Lattner7386e632004-08-13 03:05:17 +0000148 public:
Nick Lewyckye7da2d62007-05-06 13:37:16 +0000149 static char ID; // Pass identification, replacement for typeid
Gordon Henriksen2ed067a2007-11-05 01:54:05 +0000150 explicit BlockExtractorPass(const std::vector<BasicBlock*> &B)
Dan Gohmana79db302008-09-04 17:05:41 +0000151 : ModulePass(&ID), BlocksToNotExtract(B) {
Nick Lewyckyc6243022007-11-14 06:47:06 +0000152 if (!BlockFile.empty())
153 LoadFile(BlockFile.c_str());
154 }
Dan Gohmana79db302008-09-04 17:05:41 +0000155 BlockExtractorPass() : ModulePass(&ID) {}
Chris Lattner7386e632004-08-13 03:05:17 +0000156
Chris Lattner4f2cf032004-09-20 04:48:05 +0000157 bool runOnModule(Module &M);
Chris Lattner7386e632004-08-13 03:05:17 +0000158 };
Chris Lattner7386e632004-08-13 03:05:17 +0000159}
160
Dan Gohmand78c4002008-05-13 00:00:25 +0000161char BlockExtractorPass::ID = 0;
162static RegisterPass<BlockExtractorPass>
163XX("extract-blocks", "Extract Basic Blocks From Module (for bugpoint use)");
164
Chris Lattner7386e632004-08-13 03:05:17 +0000165// createBlockExtractorPass - This pass extracts all blocks (except those
166// specified in the argument list) from the functions in the module.
167//
Gordon Henriksen2ed067a2007-11-05 01:54:05 +0000168ModulePass *llvm::createBlockExtractorPass(const std::vector<BasicBlock*> &BTNE)
169{
Chris Lattner7386e632004-08-13 03:05:17 +0000170 return new BlockExtractorPass(BTNE);
171}
172
Nick Lewyckyc6243022007-11-14 06:47:06 +0000173void BlockExtractorPass::LoadFile(const char *Filename) {
174 // Load the BlockFile...
175 std::ifstream In(Filename);
176 if (!In.good()) {
Chris Lattner4883d902009-08-23 07:19:13 +0000177 errs() << "WARNING: BlockExtractor couldn't load file '" << Filename
178 << "'!\n";
Nick Lewyckyc6243022007-11-14 06:47:06 +0000179 return;
180 }
181 while (In) {
182 std::string FunctionName, BlockName;
183 In >> FunctionName;
184 In >> BlockName;
185 if (!BlockName.empty())
186 BlocksToNotExtractByName.push_back(
187 std::make_pair(FunctionName, BlockName));
188 }
189}
190
Chris Lattner4f2cf032004-09-20 04:48:05 +0000191bool BlockExtractorPass::runOnModule(Module &M) {
Chris Lattner7386e632004-08-13 03:05:17 +0000192 std::set<BasicBlock*> TranslatedBlocksToNotExtract;
193 for (unsigned i = 0, e = BlocksToNotExtract.size(); i != e; ++i) {
194 BasicBlock *BB = BlocksToNotExtract[i];
195 Function *F = BB->getParent();
196
197 // Map the corresponding function in this module.
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000198 Function *MF = M.getFunction(F->getName());
199 assert(MF->getFunctionType() == F->getFunctionType() && "Wrong function?");
Chris Lattner7386e632004-08-13 03:05:17 +0000200
201 // Figure out which index the basic block is in its function.
202 Function::iterator BBI = MF->begin();
203 std::advance(BBI, std::distance(F->begin(), Function::iterator(BB)));
204 TranslatedBlocksToNotExtract.insert(BBI);
205 }
206
Nick Lewyckyc6243022007-11-14 06:47:06 +0000207 while (!BlocksToNotExtractByName.empty()) {
208 // There's no way to find BBs by name without looking at every BB inside
209 // every Function. Fortunately, this is always empty except when used by
210 // bugpoint in which case correctness is more important than performance.
211
212 std::string &FuncName = BlocksToNotExtractByName.back().first;
213 std::string &BlockName = BlocksToNotExtractByName.back().second;
214
215 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
216 Function &F = *FI;
217 if (F.getName() != FuncName) continue;
218
219 for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) {
220 BasicBlock &BB = *BI;
221 if (BB.getName() != BlockName) continue;
222
223 TranslatedBlocksToNotExtract.insert(BI);
224 }
225 }
226
227 BlocksToNotExtractByName.pop_back();
228 }
229
Chris Lattner7386e632004-08-13 03:05:17 +0000230 // Now that we know which blocks to not extract, figure out which ones we WANT
231 // to extract.
232 std::vector<BasicBlock*> BlocksToExtract;
233 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
234 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
235 if (!TranslatedBlocksToNotExtract.count(BB))
236 BlocksToExtract.push_back(BB);
237
238 for (unsigned i = 0, e = BlocksToExtract.size(); i != e; ++i)
239 ExtractBasicBlock(BlocksToExtract[i]);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000240
Chris Lattner7386e632004-08-13 03:05:17 +0000241 return !BlocksToExtract.empty();
242}