f0d79e1a06ef6b8179739f96c69bc29a51bb5d48
[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/Module.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Analysis/LoopInfo.h"
21 #include "llvm/Transforms/Scalar.h"
22 #include "llvm/Transforms/Utils/FunctionUtils.h"
23 using namespace llvm;
24
25 namespace {
26   // FIXME: PassManager should allow Module passes to require FunctionPasses
27   struct LoopExtractor : public FunctionPass {
28     virtual bool runOnFunction(Function &F);
29     
30     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
31       AU.addRequired<LoopInfo>();
32       AU.addRequiredID(LoopSimplifyID);
33     }
34   };
35
36   RegisterOpt<LoopExtractor> 
37   X("loop-extract", "Extract loops into new functions");
38 } // End anonymous namespace 
39
40 bool LoopExtractor::runOnFunction(Function &F) {
41   LoopInfo &LI = getAnalysis<LoopInfo>();
42
43   // We don't want to keep extracting the only loop of a function into a new one
44   if (LI.begin() == LI.end() || LI.begin() + 1 == LI.end())
45     return false;
46
47   bool Changed = false;
48
49   // Try to move each loop out of the code into separate function
50   for (LoopInfo::iterator i = LI.begin(), e = LI.end(); i != e; ++i)
51     Changed |= (ExtractLoop(*i) != 0);
52
53   return Changed;
54 }
55
56 /// createLoopExtractorPass 
57 ///
58 Pass* llvm::createLoopExtractorPass() {
59   return new LoopExtractor();
60 }