blob: 848944dc9381ba48c92f3d542e962c4567e46f79 [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)
Owen Anderson6c18d1a2010-10-19 17:21:58 +000040 : LoopPass(ID), NumLoops(numLoops) {
41 initializeLoopExtractorPass(*PassRegistry::getPassRegistry());
42 }
Chris Lattnera1672c12004-03-14 20:01:36 +000043
Dan Gohman9a7320c2009-09-28 14:37:51 +000044 virtual bool runOnLoop(Loop *L, LPPassManager &LPM);
Misha Brukmanb1c93172005-04-21 23:48:37 +000045
Chris Lattner692a47a2004-03-14 02:34:07 +000046 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner5bce0c82004-03-18 05:43:18 +000047 AU.addRequiredID(BreakCriticalEdgesID);
48 AU.addRequiredID(LoopSimplifyID);
Owen Andersonf095bf32007-04-07 05:31:27 +000049 AU.addRequired<DominatorTree>();
Chris Lattner692a47a2004-03-14 02:34:07 +000050 }
51 };
Dan Gohmand78c4002008-05-13 00:00:25 +000052}
Misha Brukman03a11342004-02-28 03:33:01 +000053
Dan Gohmand78c4002008-05-13 00:00:25 +000054char LoopExtractor::ID = 0;
Owen Anderson8ac477f2010-10-12 19:48:12 +000055INITIALIZE_PASS_BEGIN(LoopExtractor, "loop-extract",
56 "Extract loops into new functions", false, false)
57INITIALIZE_PASS_DEPENDENCY(BreakCriticalEdges)
58INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
59INITIALIZE_PASS_DEPENDENCY(DominatorTree)
60INITIALIZE_PASS_END(LoopExtractor, "loop-extract",
Owen Andersondf7a4f22010-10-07 22:25:06 +000061 "Extract loops into new functions", false, false)
Chris Lattnera1672c12004-03-14 20:01:36 +000062
Dan Gohmand78c4002008-05-13 00:00:25 +000063namespace {
Chris Lattnera1672c12004-03-14 20:01:36 +000064 /// SingleLoopExtractor - For bugpoint.
65 struct SingleLoopExtractor : public LoopExtractor {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000066 static char ID; // Pass identification, replacement for typeid
Chris Lattnera1672c12004-03-14 20:01:36 +000067 SingleLoopExtractor() : LoopExtractor(1) {}
68 };
Misha Brukmanb1c93172005-04-21 23:48:37 +000069} // End anonymous namespace
Misha Brukman03a11342004-02-28 03:33:01 +000070
Dan Gohmand78c4002008-05-13 00:00:25 +000071char SingleLoopExtractor::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +000072INITIALIZE_PASS(SingleLoopExtractor, "loop-extract-single",
Owen Andersondf7a4f22010-10-07 22:25:06 +000073 "Extract at most one loop into a new function", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +000074
Jeff Cohen677babc2005-01-08 17:21:40 +000075// createLoopExtractorPass - This pass extracts all natural loops from the
76// program into a function if it can.
77//
Dan Gohman9a7320c2009-09-28 14:37:51 +000078Pass *llvm::createLoopExtractorPass() { return new LoopExtractor(); }
Jeff Cohen677babc2005-01-08 17:21:40 +000079
Dan Gohman9a7320c2009-09-28 14:37:51 +000080bool LoopExtractor::runOnLoop(Loop *L, LPPassManager &LPM) {
81 // Only visit top-level loops.
82 if (L->getParentLoop())
Misha Brukman03a11342004-02-28 03:33:01 +000083 return false;
84
Dan Gohmana83ac2d2009-11-05 21:11:53 +000085 // If LoopSimplify form is not available, stay out of trouble.
86 if (!L->isLoopSimplifyForm())
87 return false;
88
Owen Andersonf095bf32007-04-07 05:31:27 +000089 DominatorTree &DT = getAnalysis<DominatorTree>();
Dan Gohman9a7320c2009-09-28 14:37:51 +000090 bool Changed = false;
Chris Lattnere9235d22004-03-18 03:48:06 +000091
Chris Lattner2f155d82004-03-15 00:02:02 +000092 // If there is more than one top-level loop in this function, extract all of
Dan Gohman9a7320c2009-09-28 14:37:51 +000093 // the loops. Otherwise there is exactly one top-level loop; in this case if
94 // this function is more than a minimal wrapper around the loop, extract
95 // the loop.
96 bool ShouldExtractLoop = false;
Misha Brukmanb1c93172005-04-21 23:48:37 +000097
Dan Gohman9a7320c2009-09-28 14:37:51 +000098 // Extract the loop if the entry block doesn't branch to the loop header.
99 TerminatorInst *EntryTI =
100 L->getHeader()->getParent()->getEntryBlock().getTerminator();
101 if (!isa<BranchInst>(EntryTI) ||
102 !cast<BranchInst>(EntryTI)->isUnconditional() ||
103 EntryTI->getSuccessor(0) != L->getHeader())
104 ShouldExtractLoop = true;
105 else {
106 // Check to see if any exits from the loop are more than just return
107 // blocks.
108 SmallVector<BasicBlock*, 8> ExitBlocks;
109 L->getExitBlocks(ExitBlocks);
110 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
111 if (!isa<ReturnInst>(ExitBlocks[i]->getTerminator())) {
112 ShouldExtractLoop = true;
113 break;
Chris Lattner2f155d82004-03-15 00:02:02 +0000114 }
Dan Gohman9a7320c2009-09-28 14:37:51 +0000115 }
116 if (ShouldExtractLoop) {
117 if (NumLoops == 0) return Changed;
118 --NumLoops;
119 if (ExtractLoop(DT, L) != 0) {
120 Changed = true;
121 // After extraction, the loop is replaced by a function call, so
122 // we shouldn't try to run any more loop passes on it.
123 LPM.deleteLoopFromQueue(L);
Chris Lattner2f155d82004-03-15 00:02:02 +0000124 }
Dan Gohman9a7320c2009-09-28 14:37:51 +0000125 ++NumExtracted;
Chris Lattnera1672c12004-03-14 20:01:36 +0000126 }
Misha Brukman03a11342004-02-28 03:33:01 +0000127
128 return Changed;
129}
130
Chris Lattnera1672c12004-03-14 20:01:36 +0000131// createSingleLoopExtractorPass - This pass extracts one natural loop from the
132// program into a function if it can. This is used by bugpoint.
133//
Dan Gohman9a7320c2009-09-28 14:37:51 +0000134Pass *llvm::createSingleLoopExtractorPass() {
Chris Lattnera1672c12004-03-14 20:01:36 +0000135 return new SingleLoopExtractor();
Misha Brukman03a11342004-02-28 03:33:01 +0000136}
Chris Lattner7386e632004-08-13 03:05:17 +0000137
138
Dan Gohmand78c4002008-05-13 00:00:25 +0000139// BlockFile - A file which contains a list of blocks that should not be
140// extracted.
141static cl::opt<std::string>
142BlockFile("extract-blocks-file", cl::value_desc("filename"),
143 cl::desc("A file containing list of basic blocks to not extract"),
144 cl::Hidden);
Nick Lewyckyc6243022007-11-14 06:47:06 +0000145
Dan Gohmand78c4002008-05-13 00:00:25 +0000146namespace {
Chris Lattner7386e632004-08-13 03:05:17 +0000147 /// 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 {
Nick Lewyckyc6243022007-11-14 06:47:06 +0000151 void LoadFile(const char *Filename);
152
Chris Lattner7386e632004-08-13 03:05:17 +0000153 std::vector<BasicBlock*> BlocksToNotExtract;
Nick Lewyckyc6243022007-11-14 06:47:06 +0000154 std::vector<std::pair<std::string, std::string> > BlocksToNotExtractByName;
Chris Lattner7386e632004-08-13 03:05:17 +0000155 public:
Nick Lewyckye7da2d62007-05-06 13:37:16 +0000156 static char ID; // Pass identification, replacement for typeid
Owen Andersona7aed182010-08-06 18:33:48 +0000157 BlockExtractorPass() : ModulePass(ID) {
Nick Lewyckyc6243022007-11-14 06:47:06 +0000158 if (!BlockFile.empty())
159 LoadFile(BlockFile.c_str());
160 }
Chris Lattner7386e632004-08-13 03:05:17 +0000161
Chris Lattner4f2cf032004-09-20 04:48:05 +0000162 bool runOnModule(Module &M);
Chris Lattner7386e632004-08-13 03:05:17 +0000163 };
Chris Lattner7386e632004-08-13 03:05:17 +0000164}
165
Dan Gohmand78c4002008-05-13 00:00:25 +0000166char BlockExtractorPass::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000167INITIALIZE_PASS(BlockExtractorPass, "extract-blocks",
168 "Extract Basic Blocks From Module (for bugpoint use)",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000169 false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +0000170
Chris Lattner7386e632004-08-13 03:05:17 +0000171// createBlockExtractorPass - This pass extracts all blocks (except those
172// specified in the argument list) from the functions in the module.
173//
Rafael Espindola40f18832010-07-31 00:32:17 +0000174ModulePass *llvm::createBlockExtractorPass()
Gordon Henriksen2ed067a2007-11-05 01:54:05 +0000175{
Rafael Espindola40f18832010-07-31 00:32:17 +0000176 return new BlockExtractorPass();
Chris Lattner7386e632004-08-13 03:05:17 +0000177}
178
Nick Lewyckyc6243022007-11-14 06:47:06 +0000179void BlockExtractorPass::LoadFile(const char *Filename) {
180 // Load the BlockFile...
181 std::ifstream In(Filename);
182 if (!In.good()) {
Chris Lattner4883d902009-08-23 07:19:13 +0000183 errs() << "WARNING: BlockExtractor couldn't load file '" << Filename
184 << "'!\n";
Nick Lewyckyc6243022007-11-14 06:47:06 +0000185 return;
186 }
187 while (In) {
188 std::string FunctionName, BlockName;
189 In >> FunctionName;
190 In >> BlockName;
191 if (!BlockName.empty())
192 BlocksToNotExtractByName.push_back(
193 std::make_pair(FunctionName, BlockName));
194 }
195}
196
Chris Lattner4f2cf032004-09-20 04:48:05 +0000197bool BlockExtractorPass::runOnModule(Module &M) {
Chris Lattner7386e632004-08-13 03:05:17 +0000198 std::set<BasicBlock*> TranslatedBlocksToNotExtract;
199 for (unsigned i = 0, e = BlocksToNotExtract.size(); i != e; ++i) {
200 BasicBlock *BB = BlocksToNotExtract[i];
201 Function *F = BB->getParent();
202
203 // Map the corresponding function in this module.
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000204 Function *MF = M.getFunction(F->getName());
205 assert(MF->getFunctionType() == F->getFunctionType() && "Wrong function?");
Chris Lattner7386e632004-08-13 03:05:17 +0000206
207 // Figure out which index the basic block is in its function.
208 Function::iterator BBI = MF->begin();
209 std::advance(BBI, std::distance(F->begin(), Function::iterator(BB)));
210 TranslatedBlocksToNotExtract.insert(BBI);
211 }
212
Nick Lewyckyc6243022007-11-14 06:47:06 +0000213 while (!BlocksToNotExtractByName.empty()) {
214 // There's no way to find BBs by name without looking at every BB inside
215 // every Function. Fortunately, this is always empty except when used by
216 // bugpoint in which case correctness is more important than performance.
217
218 std::string &FuncName = BlocksToNotExtractByName.back().first;
219 std::string &BlockName = BlocksToNotExtractByName.back().second;
220
221 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
222 Function &F = *FI;
223 if (F.getName() != FuncName) continue;
224
225 for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) {
226 BasicBlock &BB = *BI;
227 if (BB.getName() != BlockName) continue;
228
229 TranslatedBlocksToNotExtract.insert(BI);
230 }
231 }
232
233 BlocksToNotExtractByName.pop_back();
234 }
235
Chris Lattner7386e632004-08-13 03:05:17 +0000236 // Now that we know which blocks to not extract, figure out which ones we WANT
237 // to extract.
238 std::vector<BasicBlock*> BlocksToExtract;
239 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
240 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
241 if (!TranslatedBlocksToNotExtract.count(BB))
242 BlocksToExtract.push_back(BB);
243
244 for (unsigned i = 0, e = BlocksToExtract.size(); i != e; ++i)
245 ExtractBasicBlock(BlocksToExtract[i]);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000246
Chris Lattner7386e632004-08-13 03:05:17 +0000247 return !BlocksToExtract.empty();
248}