The code extractor needs dominator info. Provide it
[oota-llvm.git] / lib / Transforms / IPO / LoopExtractor.cpp
1 //===- LoopExtractor.cpp - Extract each loop into a new function ----------===//
2 // 
3 //                     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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
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
13 // via bugpoint.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "llvm/Transforms/IPO.h"
18 #include "llvm/iTerminators.h"
19 #include "llvm/Module.h"
20 #include "llvm/Pass.h"
21 #include "llvm/Analysis/Dominators.h"
22 #include "llvm/Analysis/LoopInfo.h"
23 #include "llvm/Transforms/Scalar.h"
24 #include "llvm/Transforms/Utils/FunctionUtils.h"
25 using namespace llvm;
26
27 namespace {
28   // FIXME: This is not a function pass, but the PassManager doesn't allow
29   // Module passes to require FunctionPasses, so we can't get loop info if we're
30   // not a function pass.
31   struct LoopExtractor : public FunctionPass {
32     unsigned NumLoops;
33
34     LoopExtractor(unsigned numLoops = ~0) : NumLoops(numLoops) {}
35
36     virtual bool runOnFunction(Function &F);
37     
38     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
39       AU.addRequired<DominatorSet>();
40       AU.addRequired<LoopInfo>();
41       AU.addRequiredID(LoopSimplifyID);
42     }
43   };
44
45   RegisterOpt<LoopExtractor> 
46   X("loop-extract", "Extract loops into new functions");
47
48   /// SingleLoopExtractor - For bugpoint.
49   struct SingleLoopExtractor : public LoopExtractor {
50     SingleLoopExtractor() : LoopExtractor(1) {}
51   };
52
53   RegisterOpt<SingleLoopExtractor> 
54   Y("loop-extract-single", "Extract at most one loop into a new function");
55 } // End anonymous namespace 
56
57 bool LoopExtractor::runOnFunction(Function &F) {
58   LoopInfo &LI = getAnalysis<LoopInfo>();
59
60   // If this function has no loops, there is nothing to do.
61   if (LI.begin() == LI.end())
62     return false;
63
64   DominatorSet &DS = getAnalysis<DominatorSet>();
65
66   // If there is more than one top-level loop in this function, extract all of
67   // the loops.
68   bool Changed = false;
69   if (LI.end()-LI.begin() > 1) {
70     for (LoopInfo::iterator i = LI.begin(), e = LI.end(); i != e; ++i) {
71       if (NumLoops == 0) return Changed;
72       --NumLoops;
73       Changed |= ExtractLoop(DS, *i) != 0;
74     }
75   } else {
76     // Otherwise there is exactly one top-level loop.  If this function is more
77     // than a minimal wrapper around the loop, extract the loop.
78     Loop *TLL = *LI.begin();
79     bool ShouldExtractLoop = false;
80     
81     // Extract the loop if the entry block doesn't branch to the loop header.
82     TerminatorInst *EntryTI = F.getEntryBlock().getTerminator();
83     if (!isa<BranchInst>(EntryTI) ||
84         !cast<BranchInst>(EntryTI)->isUnconditional() || 
85         EntryTI->getSuccessor(0) != TLL->getHeader())
86       ShouldExtractLoop = true;
87     else {
88       // Check to see if any exits from the loop are more than just return
89       // blocks.
90       for (unsigned i = 0, e = TLL->getExitBlocks().size(); i != e; ++i)
91         if (!isa<ReturnInst>(TLL->getExitBlocks()[i]->getTerminator())) {
92           ShouldExtractLoop = true;
93           break;
94         }
95     }
96     
97     if (ShouldExtractLoop) {
98       if (NumLoops == 0) return Changed;
99       --NumLoops;
100       Changed |= ExtractLoop(DS, TLL) != 0;
101     } else {
102       // Okay, this function is a minimal container around the specified loop.
103       // If we extract the loop, we will continue to just keep extracting it
104       // infinitely... so don't extract it.  However, if the loop contains any
105       // subloops, extract them.
106       for (Loop::iterator i = TLL->begin(), e = TLL->end(); i != e; ++i) {
107         if (NumLoops == 0) return Changed;
108         --NumLoops;
109         Changed |= ExtractLoop(DS, *i) != 0;
110       }
111     }
112   }
113
114   return Changed;
115 }
116
117 // createSingleLoopExtractorPass - This pass extracts one natural loop from the
118 // program into a function if it can.  This is used by bugpoint.
119 //
120 Pass *llvm::createSingleLoopExtractorPass() {
121   return new SingleLoopExtractor();
122 }