blob: 80ce1fdd75113425b53d9a8ad66f519a2a201e77 [file] [log] [blame]
Misha Brukman9401deb2004-02-28 03:33:01 +00001//===- LoopExtractor.cpp - Extract each loop into a new function ----------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
Chris Lattnerefddcfa2004-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 Brukmanfd939082005-04-21 23:48:37 +00007//
Chris Lattnerefddcfa2004-03-14 02:34:07 +00008//===----------------------------------------------------------------------===//
Misha Brukman9401deb2004-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 Brukman38b8fd12004-03-02 00:19:09 +000012// given function, it is not touched. This is a pass most useful for debugging
13// via bugpoint.
Misha Brukman9401deb2004-02-28 03:33:01 +000014//
15//===----------------------------------------------------------------------===//
16
Chris Lattner86453c52006-12-19 22:09:18 +000017#define DEBUG_TYPE "loop-extract"
Chris Lattner1e3cb342004-03-14 02:37:16 +000018#include "llvm/Transforms/IPO.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000019#include "llvm/Instructions.h"
Misha Brukman9401deb2004-02-28 03:33:01 +000020#include "llvm/Module.h"
21#include "llvm/Pass.h"
Chris Lattner369287b2004-03-18 03:48:06 +000022#include "llvm/Analysis/Dominators.h"
Misha Brukman9401deb2004-02-28 03:33:01 +000023#include "llvm/Analysis/LoopInfo.h"
Nick Lewycky6fa98b12007-11-14 06:47:06 +000024#include "llvm/Support/CommandLine.h"
Reid Spencer9133fe22007-02-05 23:32:05 +000025#include "llvm/Support/Compiler.h"
Chris Lattner16d0eb02004-03-14 04:01:06 +000026#include "llvm/Transforms/Scalar.h"
Misha Brukman9401deb2004-02-28 03:33:01 +000027#include "llvm/Transforms/Utils/FunctionUtils.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000028#include "llvm/ADT/Statistic.h"
Nick Lewycky6fa98b12007-11-14 06:47:06 +000029#include <fstream>
30#include <set>
Misha Brukman9401deb2004-02-28 03:33:01 +000031using namespace llvm;
32
Chris Lattner86453c52006-12-19 22:09:18 +000033STATISTIC(NumExtracted, "Number of loops extracted");
Misha Brukmanfd939082005-04-21 23:48:37 +000034
Chris Lattner86453c52006-12-19 22:09:18 +000035namespace {
Chris Lattner41bc0b02004-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 Spencer9133fe22007-02-05 23:32:05 +000039 struct VISIBILITY_HIDDEN LoopExtractor : public FunctionPass {
Nick Lewyckyecd94c82007-05-06 13:37:16 +000040 static char ID; // Pass identification, replacement for typeid
Chris Lattner41bc0b02004-03-14 20:01:36 +000041 unsigned NumLoops;
42
Dan Gohmanc2bbfc12007-08-01 15:32:29 +000043 explicit LoopExtractor(unsigned numLoops = ~0)
Devang Patel794fd752007-05-01 21:15:47 +000044 : FunctionPass((intptr_t)&ID), NumLoops(numLoops) {}
Chris Lattner41bc0b02004-03-14 20:01:36 +000045
Chris Lattnerefddcfa2004-03-14 02:34:07 +000046 virtual bool runOnFunction(Function &F);
Misha Brukmanfd939082005-04-21 23:48:37 +000047
Chris Lattnerefddcfa2004-03-14 02:34:07 +000048 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerfdded9f2004-03-18 05:43:18 +000049 AU.addRequiredID(BreakCriticalEdgesID);
50 AU.addRequiredID(LoopSimplifyID);
Owen Andersonc6fcf292007-04-07 05:31:27 +000051 AU.addRequired<DominatorTree>();
Chris Lattnerefddcfa2004-03-14 02:34:07 +000052 AU.addRequired<LoopInfo>();
53 }
54 };
Misha Brukman9401deb2004-02-28 03:33:01 +000055
Devang Patel19974732007-05-03 01:11:54 +000056 char LoopExtractor::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +000057 RegisterPass<LoopExtractor>
Chris Lattnerefddcfa2004-03-14 02:34:07 +000058 X("loop-extract", "Extract loops into new functions");
Chris Lattner41bc0b02004-03-14 20:01:36 +000059
60 /// SingleLoopExtractor - For bugpoint.
61 struct SingleLoopExtractor : public LoopExtractor {
Nick Lewyckyecd94c82007-05-06 13:37:16 +000062 static char ID; // Pass identification, replacement for typeid
Chris Lattner41bc0b02004-03-14 20:01:36 +000063 SingleLoopExtractor() : LoopExtractor(1) {}
64 };
65
Devang Patel19974732007-05-03 01:11:54 +000066 char SingleLoopExtractor::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +000067 RegisterPass<SingleLoopExtractor>
Chris Lattner41bc0b02004-03-14 20:01:36 +000068 Y("loop-extract-single", "Extract at most one loop into a new function");
Misha Brukmanfd939082005-04-21 23:48:37 +000069} // End anonymous namespace
Misha Brukman9401deb2004-02-28 03:33:01 +000070
Jeff Cohenbf652682005-01-08 17:21:40 +000071// createLoopExtractorPass - This pass extracts all natural loops from the
72// program into a function if it can.
73//
Jeff Cohenecc1cef2005-01-10 04:23:32 +000074FunctionPass *llvm::createLoopExtractorPass() { return new LoopExtractor(); }
Jeff Cohenbf652682005-01-08 17:21:40 +000075
Misha Brukman9401deb2004-02-28 03:33:01 +000076bool LoopExtractor::runOnFunction(Function &F) {
Misha Brukman9401deb2004-02-28 03:33:01 +000077 LoopInfo &LI = getAnalysis<LoopInfo>();
78
Chris Lattner5156c392004-03-15 00:02:02 +000079 // If this function has no loops, there is nothing to do.
80 if (LI.begin() == LI.end())
Misha Brukman9401deb2004-02-28 03:33:01 +000081 return false;
82
Owen Andersonc6fcf292007-04-07 05:31:27 +000083 DominatorTree &DT = getAnalysis<DominatorTree>();
Chris Lattner369287b2004-03-18 03:48:06 +000084
Chris Lattner5156c392004-03-15 00:02:02 +000085 // If there is more than one top-level loop in this function, extract all of
86 // the loops.
Misha Brukman9401deb2004-02-28 03:33:01 +000087 bool Changed = false;
Chris Lattner5156c392004-03-15 00:02:02 +000088 if (LI.end()-LI.begin() > 1) {
89 for (LoopInfo::iterator i = LI.begin(), e = LI.end(); i != e; ++i) {
90 if (NumLoops == 0) return Changed;
91 --NumLoops;
Devang Patel4b90e3a2007-06-07 22:17:16 +000092 Changed |= ExtractLoop(DT, *i) != 0;
Chris Lattnerf6e43bc2004-03-18 05:46:10 +000093 ++NumExtracted;
Chris Lattner5156c392004-03-15 00:02:02 +000094 }
95 } else {
96 // Otherwise there is exactly one top-level loop. If this function is more
97 // than a minimal wrapper around the loop, extract the loop.
98 Loop *TLL = *LI.begin();
99 bool ShouldExtractLoop = false;
Misha Brukmanfd939082005-04-21 23:48:37 +0000100
Chris Lattner5156c392004-03-15 00:02:02 +0000101 // Extract the loop if the entry block doesn't branch to the loop header.
102 TerminatorInst *EntryTI = F.getEntryBlock().getTerminator();
103 if (!isa<BranchInst>(EntryTI) ||
Misha Brukmanfd939082005-04-21 23:48:37 +0000104 !cast<BranchInst>(EntryTI)->isUnconditional() ||
Chris Lattner5156c392004-03-15 00:02:02 +0000105 EntryTI->getSuccessor(0) != TLL->getHeader())
106 ShouldExtractLoop = true;
107 else {
108 // Check to see if any exits from the loop are more than just return
109 // blocks.
Devang Patelb7211a22007-08-21 00:31:24 +0000110 SmallVector<BasicBlock*, 8> ExitBlocks;
Chris Lattnerf1ab4b42004-04-18 22:14:10 +0000111 TLL->getExitBlocks(ExitBlocks);
112 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
113 if (!isa<ReturnInst>(ExitBlocks[i]->getTerminator())) {
Chris Lattner5156c392004-03-15 00:02:02 +0000114 ShouldExtractLoop = true;
115 break;
116 }
117 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000118
Chris Lattner5156c392004-03-15 00:02:02 +0000119 if (ShouldExtractLoop) {
120 if (NumLoops == 0) return Changed;
121 --NumLoops;
Devang Patel4b90e3a2007-06-07 22:17:16 +0000122 Changed |= ExtractLoop(DT, TLL) != 0;
Chris Lattnerf6e43bc2004-03-18 05:46:10 +0000123 ++NumExtracted;
Chris Lattner5156c392004-03-15 00:02:02 +0000124 } else {
125 // Okay, this function is a minimal container around the specified loop.
126 // If we extract the loop, we will continue to just keep extracting it
127 // infinitely... so don't extract it. However, if the loop contains any
128 // subloops, extract them.
129 for (Loop::iterator i = TLL->begin(), e = TLL->end(); i != e; ++i) {
130 if (NumLoops == 0) return Changed;
131 --NumLoops;
Devang Patel4b90e3a2007-06-07 22:17:16 +0000132 Changed |= ExtractLoop(DT, *i) != 0;
Chris Lattnerf6e43bc2004-03-18 05:46:10 +0000133 ++NumExtracted;
Chris Lattner5156c392004-03-15 00:02:02 +0000134 }
135 }
Chris Lattner41bc0b02004-03-14 20:01:36 +0000136 }
Misha Brukman9401deb2004-02-28 03:33:01 +0000137
138 return Changed;
139}
140
Chris Lattner41bc0b02004-03-14 20:01:36 +0000141// createSingleLoopExtractorPass - This pass extracts one natural loop from the
142// program into a function if it can. This is used by bugpoint.
143//
Jeff Cohenecc1cef2005-01-10 04:23:32 +0000144FunctionPass *llvm::createSingleLoopExtractorPass() {
Chris Lattner41bc0b02004-03-14 20:01:36 +0000145 return new SingleLoopExtractor();
Misha Brukman9401deb2004-02-28 03:33:01 +0000146}
Chris Lattner85286722004-08-13 03:05:17 +0000147
148
149namespace {
Nick Lewycky6fa98b12007-11-14 06:47:06 +0000150 // BlockFile - A file which contains a list of blocks that should not be
151 // extracted.
152 cl::opt<std::string>
153 BlockFile("extract-blocks-file", cl::value_desc("filename"),
154 cl::desc("A file containing list of basic blocks to not extract"),
155 cl::Hidden);
156
Chris Lattner85286722004-08-13 03:05:17 +0000157 /// BlockExtractorPass - This pass is used by bugpoint to extract all blocks
158 /// from the module into their own functions except for those specified by the
159 /// BlocksToNotExtract list.
Chris Lattnerb12914b2004-09-20 04:48:05 +0000160 class BlockExtractorPass : public ModulePass {
Nick Lewycky6fa98b12007-11-14 06:47:06 +0000161 void LoadFile(const char *Filename);
162
Chris Lattner85286722004-08-13 03:05:17 +0000163 std::vector<BasicBlock*> BlocksToNotExtract;
Nick Lewycky6fa98b12007-11-14 06:47:06 +0000164 std::vector<std::pair<std::string, std::string> > BlocksToNotExtractByName;
Chris Lattner85286722004-08-13 03:05:17 +0000165 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +0000166 static char ID; // Pass identification, replacement for typeid
Gordon Henriksen4e1be6d2007-11-05 01:54:05 +0000167 explicit BlockExtractorPass(const std::vector<BasicBlock*> &B)
Nick Lewycky6fa98b12007-11-14 06:47:06 +0000168 : ModulePass((intptr_t)&ID), BlocksToNotExtract(B) {
169 if (!BlockFile.empty())
170 LoadFile(BlockFile.c_str());
171 }
Devang Patel794fd752007-05-01 21:15:47 +0000172 BlockExtractorPass() : ModulePass((intptr_t)&ID) {}
Chris Lattner85286722004-08-13 03:05:17 +0000173
Chris Lattnerb12914b2004-09-20 04:48:05 +0000174 bool runOnModule(Module &M);
Chris Lattner85286722004-08-13 03:05:17 +0000175 };
Devang Patel794fd752007-05-01 21:15:47 +0000176
Devang Patel19974732007-05-03 01:11:54 +0000177 char BlockExtractorPass::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +0000178 RegisterPass<BlockExtractorPass>
Chris Lattner85286722004-08-13 03:05:17 +0000179 XX("extract-blocks", "Extract Basic Blocks From Module (for bugpoint use)");
180}
181
182// createBlockExtractorPass - This pass extracts all blocks (except those
183// specified in the argument list) from the functions in the module.
184//
Gordon Henriksen4e1be6d2007-11-05 01:54:05 +0000185ModulePass *llvm::createBlockExtractorPass(const std::vector<BasicBlock*> &BTNE)
186{
Chris Lattner85286722004-08-13 03:05:17 +0000187 return new BlockExtractorPass(BTNE);
188}
189
Nick Lewycky6fa98b12007-11-14 06:47:06 +0000190void BlockExtractorPass::LoadFile(const char *Filename) {
191 // Load the BlockFile...
192 std::ifstream In(Filename);
193 if (!In.good()) {
194 cerr << "WARNING: BlockExtractor couldn't load file '" << Filename
195 << "'!\n";
196 return;
197 }
198 while (In) {
199 std::string FunctionName, BlockName;
200 In >> FunctionName;
201 In >> BlockName;
202 if (!BlockName.empty())
203 BlocksToNotExtractByName.push_back(
204 std::make_pair(FunctionName, BlockName));
205 }
206}
207
Chris Lattnerb12914b2004-09-20 04:48:05 +0000208bool BlockExtractorPass::runOnModule(Module &M) {
Chris Lattner85286722004-08-13 03:05:17 +0000209 std::set<BasicBlock*> TranslatedBlocksToNotExtract;
210 for (unsigned i = 0, e = BlocksToNotExtract.size(); i != e; ++i) {
211 BasicBlock *BB = BlocksToNotExtract[i];
212 Function *F = BB->getParent();
213
214 // Map the corresponding function in this module.
Reid Spenceref9b9a72007-02-05 20:47:22 +0000215 Function *MF = M.getFunction(F->getName());
216 assert(MF->getFunctionType() == F->getFunctionType() && "Wrong function?");
Chris Lattner85286722004-08-13 03:05:17 +0000217
218 // Figure out which index the basic block is in its function.
219 Function::iterator BBI = MF->begin();
220 std::advance(BBI, std::distance(F->begin(), Function::iterator(BB)));
221 TranslatedBlocksToNotExtract.insert(BBI);
222 }
223
Nick Lewycky6fa98b12007-11-14 06:47:06 +0000224 while (!BlocksToNotExtractByName.empty()) {
225 // There's no way to find BBs by name without looking at every BB inside
226 // every Function. Fortunately, this is always empty except when used by
227 // bugpoint in which case correctness is more important than performance.
228
229 std::string &FuncName = BlocksToNotExtractByName.back().first;
230 std::string &BlockName = BlocksToNotExtractByName.back().second;
231
232 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
233 Function &F = *FI;
234 if (F.getName() != FuncName) continue;
235
236 for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) {
237 BasicBlock &BB = *BI;
238 if (BB.getName() != BlockName) continue;
239
240 TranslatedBlocksToNotExtract.insert(BI);
241 }
242 }
243
244 BlocksToNotExtractByName.pop_back();
245 }
246
Chris Lattner85286722004-08-13 03:05:17 +0000247 // Now that we know which blocks to not extract, figure out which ones we WANT
248 // to extract.
249 std::vector<BasicBlock*> BlocksToExtract;
250 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
251 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
252 if (!TranslatedBlocksToNotExtract.count(BB))
253 BlocksToExtract.push_back(BB);
254
255 for (unsigned i = 0, e = BlocksToExtract.size(); i != e; ++i)
256 ExtractBasicBlock(BlocksToExtract[i]);
Misha Brukmanfd939082005-04-21 23:48:37 +0000257
Chris Lattner85286722004-08-13 03:05:17 +0000258 return !BlocksToExtract.empty();
259}