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