Move to the IPO library. Utils shouldn't contain passes.
[oota-llvm.git] / lib / Transforms / IPO / LoopExtractor.cpp
1 //===- LoopExtractor.cpp - Extract each loop into a new function ----------===//
2 //
3 // A pass wrapper around the ExtractLoop() scalar transformation to extract each
4 // top-level loop into its own new function. If the loop is the ONLY loop in a
5 // given function, it is not touched. This is a pass most useful for debugging
6 // via bugpoint.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/Module.h"
11 #include "llvm/Pass.h"
12 #include "llvm/Analysis/LoopInfo.h"
13 #include "llvm/Transforms/Scalar.h"
14 #include "llvm/Transforms/Utils/FunctionUtils.h"
15 #include <vector>
16 using namespace llvm;
17
18 namespace {
19
20 // FIXME: PassManager should allow Module passes to require FunctionPasses
21 struct LoopExtractor : public FunctionPass {
22
23 public:
24   LoopExtractor() {}
25   virtual bool run(Module &M);
26   virtual bool runOnFunction(Function &F);
27
28   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
29     AU.addRequired<LoopInfo>();
30   }
31
32 };
33
34 RegisterOpt<LoopExtractor> 
35 X("loop-extract", "Extract loops into new functions");
36
37 bool LoopExtractor::run(Module &M) {
38   bool Changed = false;
39   for (Module::iterator i = M.begin(), e = M.end(); i != e; ++i)
40     Changed |= runOnFunction(*i);
41   return Changed;
42 }
43
44 bool LoopExtractor::runOnFunction(Function &F) {
45   std::cerr << F.getName() << "\n";
46
47   LoopInfo &LI = getAnalysis<LoopInfo>();
48
49   // We don't want to keep extracting the only loop of a function into a new one
50   if (LI.begin() == LI.end() || LI.begin() + 1 == LI.end())
51     return false;
52
53   bool Changed = false;
54
55   // Try to move each loop out of the code into separate function
56   for (LoopInfo::iterator i = LI.begin(), e = LI.end(); i != e; ++i)
57     Changed |= (ExtractLoop(*i) != 0);
58
59   return Changed;
60 }
61
62 } // End anonymous namespace 
63
64 /// createLoopExtractorPass 
65 ///
66 FunctionPass* llvm::createLoopExtractorPass() {
67   return new LoopExtractor();
68 }