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