blob: 7b10186231d9120b8fcf686103a25142ccea84c8 [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"
Reid Spencer9133fe22007-02-05 23:32:05 +000024#include "llvm/Support/Compiler.h"
Chris Lattner16d0eb02004-03-14 04:01:06 +000025#include "llvm/Transforms/Scalar.h"
Misha Brukman9401deb2004-02-28 03:33:01 +000026#include "llvm/Transforms/Utils/FunctionUtils.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000027#include "llvm/ADT/Statistic.h"
Misha Brukman9401deb2004-02-28 03:33:01 +000028using namespace llvm;
29
Chris Lattner86453c52006-12-19 22:09:18 +000030STATISTIC(NumExtracted, "Number of loops extracted");
Misha Brukmanfd939082005-04-21 23:48:37 +000031
Chris Lattner86453c52006-12-19 22:09:18 +000032namespace {
Chris Lattner41bc0b02004-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 Spencer9133fe22007-02-05 23:32:05 +000036 struct VISIBILITY_HIDDEN LoopExtractor : public FunctionPass {
Devang Patel19974732007-05-03 01:11:54 +000037 static char ID; // Pass identifcation, replacement for typeid
Chris Lattner41bc0b02004-03-14 20:01:36 +000038 unsigned NumLoops;
39
Devang Patel794fd752007-05-01 21:15:47 +000040 LoopExtractor(unsigned numLoops = ~0)
41 : FunctionPass((intptr_t)&ID), NumLoops(numLoops) {}
Chris Lattner41bc0b02004-03-14 20:01:36 +000042
Chris Lattnerefddcfa2004-03-14 02:34:07 +000043 virtual bool runOnFunction(Function &F);
Misha Brukmanfd939082005-04-21 23:48:37 +000044
Chris Lattnerefddcfa2004-03-14 02:34:07 +000045 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerfdded9f2004-03-18 05:43:18 +000046 AU.addRequiredID(BreakCriticalEdgesID);
47 AU.addRequiredID(LoopSimplifyID);
Owen Andersonc6fcf292007-04-07 05:31:27 +000048 AU.addRequired<ETForest>();
49 AU.addRequired<DominatorTree>();
Chris Lattnerefddcfa2004-03-14 02:34:07 +000050 AU.addRequired<LoopInfo>();
51 }
52 };
Misha Brukman9401deb2004-02-28 03:33:01 +000053
Devang Patel19974732007-05-03 01:11:54 +000054 char LoopExtractor::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +000055 RegisterPass<LoopExtractor>
Chris Lattnerefddcfa2004-03-14 02:34:07 +000056 X("loop-extract", "Extract loops into new functions");
Chris Lattner41bc0b02004-03-14 20:01:36 +000057
58 /// SingleLoopExtractor - For bugpoint.
59 struct SingleLoopExtractor : public LoopExtractor {
Devang Patel19974732007-05-03 01:11:54 +000060 static char ID; // Pass identifcation, replacement for typeid
Chris Lattner41bc0b02004-03-14 20:01:36 +000061 SingleLoopExtractor() : LoopExtractor(1) {}
62 };
63
Devang Patel19974732007-05-03 01:11:54 +000064 char SingleLoopExtractor::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +000065 RegisterPass<SingleLoopExtractor>
Chris Lattner41bc0b02004-03-14 20:01:36 +000066 Y("loop-extract-single", "Extract at most one loop into a new function");
Misha Brukmanfd939082005-04-21 23:48:37 +000067} // End anonymous namespace
Misha Brukman9401deb2004-02-28 03:33:01 +000068
Jeff Cohenbf652682005-01-08 17:21:40 +000069// createLoopExtractorPass - This pass extracts all natural loops from the
70// program into a function if it can.
71//
Jeff Cohenecc1cef2005-01-10 04:23:32 +000072FunctionPass *llvm::createLoopExtractorPass() { return new LoopExtractor(); }
Jeff Cohenbf652682005-01-08 17:21:40 +000073
Misha Brukman9401deb2004-02-28 03:33:01 +000074bool LoopExtractor::runOnFunction(Function &F) {
Misha Brukman9401deb2004-02-28 03:33:01 +000075 LoopInfo &LI = getAnalysis<LoopInfo>();
76
Chris Lattner5156c392004-03-15 00:02:02 +000077 // If this function has no loops, there is nothing to do.
78 if (LI.begin() == LI.end())
Misha Brukman9401deb2004-02-28 03:33:01 +000079 return false;
80
Owen Andersonc6fcf292007-04-07 05:31:27 +000081 ETForest &EF = getAnalysis<ETForest>();
82 DominatorTree &DT = getAnalysis<DominatorTree>();
Chris Lattner369287b2004-03-18 03:48:06 +000083
Chris Lattner5156c392004-03-15 00:02:02 +000084 // If there is more than one top-level loop in this function, extract all of
85 // the loops.
Misha Brukman9401deb2004-02-28 03:33:01 +000086 bool Changed = false;
Chris Lattner5156c392004-03-15 00:02:02 +000087 if (LI.end()-LI.begin() > 1) {
88 for (LoopInfo::iterator i = LI.begin(), e = LI.end(); i != e; ++i) {
89 if (NumLoops == 0) return Changed;
90 --NumLoops;
Owen Andersonc6fcf292007-04-07 05:31:27 +000091 Changed |= ExtractLoop(EF, DT, *i) != 0;
Chris Lattnerf6e43bc2004-03-18 05:46:10 +000092 ++NumExtracted;
Chris Lattner5156c392004-03-15 00:02:02 +000093 }
94 } else {
95 // Otherwise there is exactly one top-level loop. If this function is more
96 // than a minimal wrapper around the loop, extract the loop.
97 Loop *TLL = *LI.begin();
98 bool ShouldExtractLoop = false;
Misha Brukmanfd939082005-04-21 23:48:37 +000099
Chris Lattner5156c392004-03-15 00:02:02 +0000100 // Extract the loop if the entry block doesn't branch to the loop header.
101 TerminatorInst *EntryTI = F.getEntryBlock().getTerminator();
102 if (!isa<BranchInst>(EntryTI) ||
Misha Brukmanfd939082005-04-21 23:48:37 +0000103 !cast<BranchInst>(EntryTI)->isUnconditional() ||
Chris Lattner5156c392004-03-15 00:02:02 +0000104 EntryTI->getSuccessor(0) != TLL->getHeader())
105 ShouldExtractLoop = true;
106 else {
107 // Check to see if any exits from the loop are more than just return
108 // blocks.
Chris Lattnerf1ab4b42004-04-18 22:14:10 +0000109 std::vector<BasicBlock*> ExitBlocks;
110 TLL->getExitBlocks(ExitBlocks);
111 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
112 if (!isa<ReturnInst>(ExitBlocks[i]->getTerminator())) {
Chris Lattner5156c392004-03-15 00:02:02 +0000113 ShouldExtractLoop = true;
114 break;
115 }
116 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000117
Chris Lattner5156c392004-03-15 00:02:02 +0000118 if (ShouldExtractLoop) {
119 if (NumLoops == 0) return Changed;
120 --NumLoops;
Owen Andersonc6fcf292007-04-07 05:31:27 +0000121 Changed |= ExtractLoop(EF, DT, TLL) != 0;
Chris Lattnerf6e43bc2004-03-18 05:46:10 +0000122 ++NumExtracted;
Chris Lattner5156c392004-03-15 00:02:02 +0000123 } else {
124 // Okay, this function is a minimal container around the specified loop.
125 // If we extract the loop, we will continue to just keep extracting it
126 // infinitely... so don't extract it. However, if the loop contains any
127 // subloops, extract them.
128 for (Loop::iterator i = TLL->begin(), e = TLL->end(); i != e; ++i) {
129 if (NumLoops == 0) return Changed;
130 --NumLoops;
Owen Andersonc6fcf292007-04-07 05:31:27 +0000131 Changed |= ExtractLoop(EF, DT, *i) != 0;
Chris Lattnerf6e43bc2004-03-18 05:46:10 +0000132 ++NumExtracted;
Chris Lattner5156c392004-03-15 00:02:02 +0000133 }
134 }
Chris Lattner41bc0b02004-03-14 20:01:36 +0000135 }
Misha Brukman9401deb2004-02-28 03:33:01 +0000136
137 return Changed;
138}
139
Chris Lattner41bc0b02004-03-14 20:01:36 +0000140// createSingleLoopExtractorPass - This pass extracts one natural loop from the
141// program into a function if it can. This is used by bugpoint.
142//
Jeff Cohenecc1cef2005-01-10 04:23:32 +0000143FunctionPass *llvm::createSingleLoopExtractorPass() {
Chris Lattner41bc0b02004-03-14 20:01:36 +0000144 return new SingleLoopExtractor();
Misha Brukman9401deb2004-02-28 03:33:01 +0000145}
Chris Lattner85286722004-08-13 03:05:17 +0000146
147
148namespace {
149 /// BlockExtractorPass - This pass is used by bugpoint to extract all blocks
150 /// from the module into their own functions except for those specified by the
151 /// BlocksToNotExtract list.
Chris Lattnerb12914b2004-09-20 04:48:05 +0000152 class BlockExtractorPass : public ModulePass {
Chris Lattner85286722004-08-13 03:05:17 +0000153 std::vector<BasicBlock*> BlocksToNotExtract;
154 public:
Devang Patel19974732007-05-03 01:11:54 +0000155 static char ID; // Pass identifcation, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +0000156 BlockExtractorPass(std::vector<BasicBlock*> &B)
157 : ModulePass((intptr_t)&ID), BlocksToNotExtract(B) {}
158 BlockExtractorPass() : ModulePass((intptr_t)&ID) {}
Chris Lattner85286722004-08-13 03:05:17 +0000159
Chris Lattnerb12914b2004-09-20 04:48:05 +0000160 bool runOnModule(Module &M);
Chris Lattner85286722004-08-13 03:05:17 +0000161 };
Devang Patel794fd752007-05-01 21:15:47 +0000162
Devang Patel19974732007-05-03 01:11:54 +0000163 char BlockExtractorPass::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +0000164 RegisterPass<BlockExtractorPass>
Chris Lattner85286722004-08-13 03:05:17 +0000165 XX("extract-blocks", "Extract Basic Blocks From Module (for bugpoint use)");
166}
167
168// createBlockExtractorPass - This pass extracts all blocks (except those
169// specified in the argument list) from the functions in the module.
170//
Chris Lattnerb12914b2004-09-20 04:48:05 +0000171ModulePass *llvm::createBlockExtractorPass(std::vector<BasicBlock*> &BTNE) {
Chris Lattner85286722004-08-13 03:05:17 +0000172 return new BlockExtractorPass(BTNE);
173}
174
Chris Lattnerb12914b2004-09-20 04:48:05 +0000175bool BlockExtractorPass::runOnModule(Module &M) {
Chris Lattner85286722004-08-13 03:05:17 +0000176 std::set<BasicBlock*> TranslatedBlocksToNotExtract;
177 for (unsigned i = 0, e = BlocksToNotExtract.size(); i != e; ++i) {
178 BasicBlock *BB = BlocksToNotExtract[i];
179 Function *F = BB->getParent();
180
181 // Map the corresponding function in this module.
Reid Spenceref9b9a72007-02-05 20:47:22 +0000182 Function *MF = M.getFunction(F->getName());
183 assert(MF->getFunctionType() == F->getFunctionType() && "Wrong function?");
Chris Lattner85286722004-08-13 03:05:17 +0000184
185 // Figure out which index the basic block is in its function.
186 Function::iterator BBI = MF->begin();
187 std::advance(BBI, std::distance(F->begin(), Function::iterator(BB)));
188 TranslatedBlocksToNotExtract.insert(BBI);
189 }
190
191 // Now that we know which blocks to not extract, figure out which ones we WANT
192 // to extract.
193 std::vector<BasicBlock*> BlocksToExtract;
194 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
195 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
196 if (!TranslatedBlocksToNotExtract.count(BB))
197 BlocksToExtract.push_back(BB);
198
199 for (unsigned i = 0, e = BlocksToExtract.size(); i != e; ++i)
200 ExtractBasicBlock(BlocksToExtract[i]);
Misha Brukmanfd939082005-04-21 23:48:37 +0000201
Chris Lattner85286722004-08-13 03:05:17 +0000202 return !BlocksToExtract.empty();
203}