1 //===- LoopExtractor.cpp - Extract each loop into a new function ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
12 // given function, it is not touched. This is a pass most useful for debugging
15 //===----------------------------------------------------------------------===//
17 #define DEBUG_TYPE "loop-extract"
18 #include "llvm/Transforms/IPO.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/Analysis/LoopPass.h"
21 #include "llvm/IR/Dominators.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/Pass.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Transforms/Scalar.h"
27 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
28 #include "llvm/Transforms/Utils/CodeExtractor.h"
33 STATISTIC(NumExtracted, "Number of loops extracted");
36 struct LoopExtractor : public LoopPass {
37 static char ID; // Pass identification, replacement for typeid
40 explicit LoopExtractor(unsigned numLoops = ~0)
41 : LoopPass(ID), NumLoops(numLoops) {
42 initializeLoopExtractorPass(*PassRegistry::getPassRegistry());
45 bool runOnLoop(Loop *L, LPPassManager &LPM) override;
47 void getAnalysisUsage(AnalysisUsage &AU) const override {
48 AU.addRequiredID(BreakCriticalEdgesID);
49 AU.addRequiredID(LoopSimplifyID);
50 AU.addRequired<DominatorTreeWrapperPass>();
55 char LoopExtractor::ID = 0;
56 INITIALIZE_PASS_BEGIN(LoopExtractor, "loop-extract",
57 "Extract loops into new functions", false, false)
58 INITIALIZE_PASS_DEPENDENCY(BreakCriticalEdges)
59 INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
60 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
61 INITIALIZE_PASS_END(LoopExtractor, "loop-extract",
62 "Extract loops into new functions", false, false)
65 /// SingleLoopExtractor - For bugpoint.
66 struct SingleLoopExtractor : public LoopExtractor {
67 static char ID; // Pass identification, replacement for typeid
68 SingleLoopExtractor() : LoopExtractor(1) {}
70 } // End anonymous namespace
72 char SingleLoopExtractor::ID = 0;
73 INITIALIZE_PASS(SingleLoopExtractor, "loop-extract-single",
74 "Extract at most one loop into a new function", false, false)
76 // createLoopExtractorPass - This pass extracts all natural loops from the
77 // program into a function if it can.
79 Pass *llvm::createLoopExtractorPass() { return new LoopExtractor(); }
81 bool LoopExtractor::runOnLoop(Loop *L, LPPassManager &LPM) {
82 if (skipOptnoneFunction(L))
85 // Only visit top-level loops.
86 if (L->getParentLoop())
89 // If LoopSimplify form is not available, stay out of trouble.
90 if (!L->isLoopSimplifyForm())
93 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
96 // If there is more than one top-level loop in this function, extract all of
97 // the loops. Otherwise there is exactly one top-level loop; in this case if
98 // this function is more than a minimal wrapper around the loop, extract
100 bool ShouldExtractLoop = false;
102 // Extract the loop if the entry block doesn't branch to the loop header.
103 TerminatorInst *EntryTI =
104 L->getHeader()->getParent()->getEntryBlock().getTerminator();
105 if (!isa<BranchInst>(EntryTI) ||
106 !cast<BranchInst>(EntryTI)->isUnconditional() ||
107 EntryTI->getSuccessor(0) != L->getHeader()) {
108 ShouldExtractLoop = true;
110 // Check to see if any exits from the loop are more than just return
112 SmallVector<BasicBlock*, 8> ExitBlocks;
113 L->getExitBlocks(ExitBlocks);
114 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
115 if (!isa<ReturnInst>(ExitBlocks[i]->getTerminator())) {
116 ShouldExtractLoop = true;
121 if (ShouldExtractLoop) {
122 // We must omit landing pads. Landing pads must accompany the invoke
123 // instruction. But this would result in a loop in the extracted
124 // function. An infinite cycle occurs when it tries to extract that loop as
126 SmallVector<BasicBlock*, 8> ExitBlocks;
127 L->getExitBlocks(ExitBlocks);
128 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
129 if (ExitBlocks[i]->isLandingPad()) {
130 ShouldExtractLoop = false;
135 if (ShouldExtractLoop) {
136 if (NumLoops == 0) return Changed;
138 CodeExtractor Extractor(DT, *L);
139 if (Extractor.extractCodeRegion() != 0) {
141 // After extraction, the loop is replaced by a function call, so
142 // we shouldn't try to run any more loop passes on it.
143 LPM.deleteLoopFromQueue(L);
151 // createSingleLoopExtractorPass - This pass extracts one natural loop from the
152 // program into a function if it can. This is used by bugpoint.
154 Pass *llvm::createSingleLoopExtractorPass() {
155 return new SingleLoopExtractor();
159 // BlockFile - A file which contains a list of blocks that should not be
161 static cl::opt<std::string>
162 BlockFile("extract-blocks-file", cl::value_desc("filename"),
163 cl::desc("A file containing list of basic blocks to not extract"),
167 /// BlockExtractorPass - This pass is used by bugpoint to extract all blocks
168 /// from the module into their own functions except for those specified by the
169 /// BlocksToNotExtract list.
170 class BlockExtractorPass : public ModulePass {
171 void LoadFile(const char *Filename);
172 void SplitLandingPadPreds(Function *F);
174 std::vector<BasicBlock*> BlocksToNotExtract;
175 std::vector<std::pair<std::string, std::string> > BlocksToNotExtractByName;
177 static char ID; // Pass identification, replacement for typeid
178 BlockExtractorPass() : ModulePass(ID) {
179 if (!BlockFile.empty())
180 LoadFile(BlockFile.c_str());
183 bool runOnModule(Module &M) override;
187 char BlockExtractorPass::ID = 0;
188 INITIALIZE_PASS(BlockExtractorPass, "extract-blocks",
189 "Extract Basic Blocks From Module (for bugpoint use)",
192 // createBlockExtractorPass - This pass extracts all blocks (except those
193 // specified in the argument list) from the functions in the module.
195 ModulePass *llvm::createBlockExtractorPass() {
196 return new BlockExtractorPass();
199 void BlockExtractorPass::LoadFile(const char *Filename) {
200 // Load the BlockFile...
201 std::ifstream In(Filename);
203 errs() << "WARNING: BlockExtractor couldn't load file '" << Filename
208 std::string FunctionName, BlockName;
211 if (!BlockName.empty())
212 BlocksToNotExtractByName.push_back(
213 std::make_pair(FunctionName, BlockName));
217 /// SplitLandingPadPreds - The landing pad needs to be extracted with the invoke
218 /// instruction. The critical edge breaker will refuse to break critical edges
219 /// to a landing pad. So do them here. After this method runs, all landing pads
220 /// should have only one predecessor.
221 void BlockExtractorPass::SplitLandingPadPreds(Function *F) {
222 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
223 InvokeInst *II = dyn_cast<InvokeInst>(I);
225 BasicBlock *Parent = II->getParent();
226 BasicBlock *LPad = II->getUnwindDest();
228 // Look through the landing pad's predecessors. If one of them ends in an
229 // 'invoke', then we want to split the landing pad.
232 PI = pred_begin(LPad), PE = pred_end(LPad); PI != PE; ++PI) {
233 BasicBlock *BB = *PI;
234 if (BB->isLandingPad() && BB != Parent &&
235 isa<InvokeInst>(Parent->getTerminator())) {
241 if (!Split) continue;
243 SmallVector<BasicBlock*, 2> NewBBs;
244 SplitLandingPadPredecessors(LPad, Parent, ".1", ".2", 0, NewBBs);
248 bool BlockExtractorPass::runOnModule(Module &M) {
249 std::set<BasicBlock*> TranslatedBlocksToNotExtract;
250 for (unsigned i = 0, e = BlocksToNotExtract.size(); i != e; ++i) {
251 BasicBlock *BB = BlocksToNotExtract[i];
252 Function *F = BB->getParent();
254 // Map the corresponding function in this module.
255 Function *MF = M.getFunction(F->getName());
256 assert(MF->getFunctionType() == F->getFunctionType() && "Wrong function?");
258 // Figure out which index the basic block is in its function.
259 Function::iterator BBI = MF->begin();
260 std::advance(BBI, std::distance(F->begin(), Function::iterator(BB)));
261 TranslatedBlocksToNotExtract.insert(BBI);
264 while (!BlocksToNotExtractByName.empty()) {
265 // There's no way to find BBs by name without looking at every BB inside
266 // every Function. Fortunately, this is always empty except when used by
267 // bugpoint in which case correctness is more important than performance.
269 std::string &FuncName = BlocksToNotExtractByName.back().first;
270 std::string &BlockName = BlocksToNotExtractByName.back().second;
272 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
274 if (F.getName() != FuncName) continue;
276 for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) {
277 BasicBlock &BB = *BI;
278 if (BB.getName() != BlockName) continue;
280 TranslatedBlocksToNotExtract.insert(BI);
284 BlocksToNotExtractByName.pop_back();
287 // Now that we know which blocks to not extract, figure out which ones we WANT
289 std::vector<BasicBlock*> BlocksToExtract;
290 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
291 SplitLandingPadPreds(&*F);
292 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
293 if (!TranslatedBlocksToNotExtract.count(BB))
294 BlocksToExtract.push_back(BB);
297 for (unsigned i = 0, e = BlocksToExtract.size(); i != e; ++i) {
298 SmallVector<BasicBlock*, 2> BlocksToExtractVec;
299 BlocksToExtractVec.push_back(BlocksToExtract[i]);
300 if (const InvokeInst *II =
301 dyn_cast<InvokeInst>(BlocksToExtract[i]->getTerminator()))
302 BlocksToExtractVec.push_back(II->getUnwindDest());
303 CodeExtractor(BlocksToExtractVec).extractCodeRegion();
306 return !BlocksToExtract.empty();