blob: 6afcf29f9cc7816bb56194415c06be20531a5ee8 [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 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"
Reid Spencer557ab152007-02-05 23:32:05 +000024#include "llvm/Support/Compiler.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"
Misha Brukman03a11342004-02-28 03:33:01 +000028using namespace llvm;
29
Chris Lattner1631bcb2006-12-19 22:09:18 +000030STATISTIC(NumExtracted, "Number of loops extracted");
Misha Brukmanb1c93172005-04-21 23:48:37 +000031
Chris Lattner1631bcb2006-12-19 22:09:18 +000032namespace {
Chris Lattnera1672c12004-03-14 20:01:36 +000033 // FIXME: This is not a function pass, but the PassManager doesn't allow
34 // Module passes to require FunctionPasses, so we can't get loop info if we're
35 // not a function pass.
Reid Spencer557ab152007-02-05 23:32:05 +000036 struct VISIBILITY_HIDDEN LoopExtractor : public FunctionPass {
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)
Devang Patel09f162c2007-05-01 21:15:47 +000041 : FunctionPass((intptr_t)&ID), NumLoops(numLoops) {}
Chris Lattnera1672c12004-03-14 20:01:36 +000042
Chris Lattner692a47a2004-03-14 02:34:07 +000043 virtual bool runOnFunction(Function &F);
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 AU.addRequired<LoopInfo>();
50 }
51 };
Misha Brukman03a11342004-02-28 03:33:01 +000052
Devang Patel8c78a0b2007-05-03 01:11:54 +000053 char LoopExtractor::ID = 0;
Chris Lattnerc2d3d312006-08-27 22:42:52 +000054 RegisterPass<LoopExtractor>
Chris Lattner692a47a2004-03-14 02:34:07 +000055 X("loop-extract", "Extract loops into new functions");
Chris Lattnera1672c12004-03-14 20:01:36 +000056
57 /// 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 };
62
Devang Patel8c78a0b2007-05-03 01:11:54 +000063 char SingleLoopExtractor::ID = 0;
Chris Lattnerc2d3d312006-08-27 22:42:52 +000064 RegisterPass<SingleLoopExtractor>
Chris Lattnera1672c12004-03-14 20:01:36 +000065 Y("loop-extract-single", "Extract at most one loop into a new function");
Misha Brukmanb1c93172005-04-21 23:48:37 +000066} // End anonymous namespace
Misha Brukman03a11342004-02-28 03:33:01 +000067
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//
Jeff Cohen3e62e7c2005-01-10 04:23:32 +000071FunctionPass *llvm::createLoopExtractorPass() { return new LoopExtractor(); }
Jeff Cohen677babc2005-01-08 17:21:40 +000072
Misha Brukman03a11342004-02-28 03:33:01 +000073bool LoopExtractor::runOnFunction(Function &F) {
Misha Brukman03a11342004-02-28 03:33:01 +000074 LoopInfo &LI = getAnalysis<LoopInfo>();
75
Chris Lattner2f155d82004-03-15 00:02:02 +000076 // If this function has no loops, there is nothing to do.
77 if (LI.begin() == LI.end())
Misha Brukman03a11342004-02-28 03:33:01 +000078 return false;
79
Owen Andersonf095bf32007-04-07 05:31:27 +000080 DominatorTree &DT = getAnalysis<DominatorTree>();
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
83 // the loops.
Misha Brukman03a11342004-02-28 03:33:01 +000084 bool Changed = false;
Chris Lattner2f155d82004-03-15 00:02:02 +000085 if (LI.end()-LI.begin() > 1) {
86 for (LoopInfo::iterator i = LI.begin(), e = LI.end(); i != e; ++i) {
87 if (NumLoops == 0) return Changed;
88 --NumLoops;
Devang Patelcf470e52007-06-07 22:17:16 +000089 Changed |= ExtractLoop(DT, *i) != 0;
Chris Lattnere8369352004-03-18 05:46:10 +000090 ++NumExtracted;
Chris Lattner2f155d82004-03-15 00:02:02 +000091 }
92 } else {
93 // Otherwise there is exactly one top-level loop. If this function is more
94 // than a minimal wrapper around the loop, extract the loop.
95 Loop *TLL = *LI.begin();
96 bool ShouldExtractLoop = false;
Misha Brukmanb1c93172005-04-21 23:48:37 +000097
Chris Lattner2f155d82004-03-15 00:02:02 +000098 // Extract the loop if the entry block doesn't branch to the loop header.
99 TerminatorInst *EntryTI = F.getEntryBlock().getTerminator();
100 if (!isa<BranchInst>(EntryTI) ||
Misha Brukmanb1c93172005-04-21 23:48:37 +0000101 !cast<BranchInst>(EntryTI)->isUnconditional() ||
Chris Lattner2f155d82004-03-15 00:02:02 +0000102 EntryTI->getSuccessor(0) != TLL->getHeader())
103 ShouldExtractLoop = true;
104 else {
105 // Check to see if any exits from the loop are more than just return
106 // blocks.
Devang Patelb5933bb2007-08-21 00:31:24 +0000107 SmallVector<BasicBlock*, 8> ExitBlocks;
Chris Lattnerd72c3eb2004-04-18 22:14:10 +0000108 TLL->getExitBlocks(ExitBlocks);
109 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
110 if (!isa<ReturnInst>(ExitBlocks[i]->getTerminator())) {
Chris Lattner2f155d82004-03-15 00:02:02 +0000111 ShouldExtractLoop = true;
112 break;
113 }
114 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000115
Chris Lattner2f155d82004-03-15 00:02:02 +0000116 if (ShouldExtractLoop) {
117 if (NumLoops == 0) return Changed;
118 --NumLoops;
Devang Patelcf470e52007-06-07 22:17:16 +0000119 Changed |= ExtractLoop(DT, TLL) != 0;
Chris Lattnere8369352004-03-18 05:46:10 +0000120 ++NumExtracted;
Chris Lattner2f155d82004-03-15 00:02:02 +0000121 } else {
122 // Okay, this function is a minimal container around the specified loop.
123 // If we extract the loop, we will continue to just keep extracting it
124 // infinitely... so don't extract it. However, if the loop contains any
125 // subloops, extract them.
126 for (Loop::iterator i = TLL->begin(), e = TLL->end(); i != e; ++i) {
127 if (NumLoops == 0) return Changed;
128 --NumLoops;
Devang Patelcf470e52007-06-07 22:17:16 +0000129 Changed |= ExtractLoop(DT, *i) != 0;
Chris Lattnere8369352004-03-18 05:46:10 +0000130 ++NumExtracted;
Chris Lattner2f155d82004-03-15 00:02:02 +0000131 }
132 }
Chris Lattnera1672c12004-03-14 20:01:36 +0000133 }
Misha Brukman03a11342004-02-28 03:33:01 +0000134
135 return Changed;
136}
137
Chris Lattnera1672c12004-03-14 20:01:36 +0000138// createSingleLoopExtractorPass - This pass extracts one natural loop from the
139// program into a function if it can. This is used by bugpoint.
140//
Jeff Cohen3e62e7c2005-01-10 04:23:32 +0000141FunctionPass *llvm::createSingleLoopExtractorPass() {
Chris Lattnera1672c12004-03-14 20:01:36 +0000142 return new SingleLoopExtractor();
Misha Brukman03a11342004-02-28 03:33:01 +0000143}
Chris Lattner7386e632004-08-13 03:05:17 +0000144
145
146namespace {
147 /// BlockExtractorPass - This pass is used by bugpoint to extract all blocks
148 /// from the module into their own functions except for those specified by the
149 /// BlocksToNotExtract list.
Chris Lattner4f2cf032004-09-20 04:48:05 +0000150 class BlockExtractorPass : public ModulePass {
Chris Lattner7386e632004-08-13 03:05:17 +0000151 std::vector<BasicBlock*> BlocksToNotExtract;
152 public:
Nick Lewyckye7da2d62007-05-06 13:37:16 +0000153 static char ID; // Pass identification, replacement for typeid
Dan Gohman34d442f2007-08-01 15:32:29 +0000154 explicit BlockExtractorPass(std::vector<BasicBlock*> &B)
Devang Patel09f162c2007-05-01 21:15:47 +0000155 : ModulePass((intptr_t)&ID), BlocksToNotExtract(B) {}
156 BlockExtractorPass() : ModulePass((intptr_t)&ID) {}
Chris Lattner7386e632004-08-13 03:05:17 +0000157
Chris Lattner4f2cf032004-09-20 04:48:05 +0000158 bool runOnModule(Module &M);
Chris Lattner7386e632004-08-13 03:05:17 +0000159 };
Devang Patel09f162c2007-05-01 21:15:47 +0000160
Devang Patel8c78a0b2007-05-03 01:11:54 +0000161 char BlockExtractorPass::ID = 0;
Chris Lattnerc2d3d312006-08-27 22:42:52 +0000162 RegisterPass<BlockExtractorPass>
Chris Lattner7386e632004-08-13 03:05:17 +0000163 XX("extract-blocks", "Extract Basic Blocks From Module (for bugpoint use)");
164}
165
166// createBlockExtractorPass - This pass extracts all blocks (except those
167// specified in the argument list) from the functions in the module.
168//
Chris Lattner4f2cf032004-09-20 04:48:05 +0000169ModulePass *llvm::createBlockExtractorPass(std::vector<BasicBlock*> &BTNE) {
Chris Lattner7386e632004-08-13 03:05:17 +0000170 return new BlockExtractorPass(BTNE);
171}
172
Chris Lattner4f2cf032004-09-20 04:48:05 +0000173bool BlockExtractorPass::runOnModule(Module &M) {
Chris Lattner7386e632004-08-13 03:05:17 +0000174 std::set<BasicBlock*> TranslatedBlocksToNotExtract;
175 for (unsigned i = 0, e = BlocksToNotExtract.size(); i != e; ++i) {
176 BasicBlock *BB = BlocksToNotExtract[i];
177 Function *F = BB->getParent();
178
179 // Map the corresponding function in this module.
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000180 Function *MF = M.getFunction(F->getName());
181 assert(MF->getFunctionType() == F->getFunctionType() && "Wrong function?");
Chris Lattner7386e632004-08-13 03:05:17 +0000182
183 // Figure out which index the basic block is in its function.
184 Function::iterator BBI = MF->begin();
185 std::advance(BBI, std::distance(F->begin(), Function::iterator(BB)));
186 TranslatedBlocksToNotExtract.insert(BBI);
187 }
188
189 // Now that we know which blocks to not extract, figure out which ones we WANT
190 // to extract.
191 std::vector<BasicBlock*> BlocksToExtract;
192 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
193 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
194 if (!TranslatedBlocksToNotExtract.count(BB))
195 BlocksToExtract.push_back(BB);
196
197 for (unsigned i = 0, e = BlocksToExtract.size(); i != e; ++i)
198 ExtractBasicBlock(BlocksToExtract[i]);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000199
Chris Lattner7386e632004-08-13 03:05:17 +0000200 return !BlocksToExtract.empty();
201}