aboutsummaryrefslogtreecommitdiff
path: root/lib/Transforms/IPO/LoopExtractor.cpp
blob: 480f0f7c03d79bf818c4aebd0d377c5ab57880a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//===- LoopExtractor.cpp - Extract each loop into a new function ----------===//
// 
//                     The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
// 
//===----------------------------------------------------------------------===//
//
// A pass wrapper around the ExtractLoop() scalar transformation to extract each
// top-level loop into its own new function. If the loop is the ONLY loop in a
// given function, it is not touched. This is a pass most useful for debugging
// via bugpoint.
//
//===----------------------------------------------------------------------===//

#include "llvm/Module.h"
#include "llvm/Pass.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Utils/FunctionUtils.h"
#include <vector>
using namespace llvm;

namespace {
  // FIXME: PassManager should allow Module passes to require FunctionPasses
  struct LoopExtractor : public FunctionPass {
    virtual bool run(Module &M);
    virtual bool runOnFunction(Function &F);
    
    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
      AU.addRequired<LoopInfo>();
    }
  };

  RegisterOpt<LoopExtractor> 
  X("loop-extract", "Extract loops into new functions");
} // End anonymous namespace 

bool LoopExtractor::run(Module &M) {
  bool Changed = false;
  for (Module::iterator i = M.begin(), e = M.end(); i != e; ++i)
    Changed |= runOnFunction(*i);
  return Changed;
}

bool LoopExtractor::runOnFunction(Function &F) {
  std::cerr << F.getName() << "\n";

  LoopInfo &LI = getAnalysis<LoopInfo>();

  // We don't want to keep extracting the only loop of a function into a new one
  if (LI.begin() == LI.end() || LI.begin() + 1 == LI.end())
    return false;

  bool Changed = false;

  // Try to move each loop out of the code into separate function
  for (LoopInfo::iterator i = LI.begin(), e = LI.end(); i != e; ++i)
    Changed |= (ExtractLoop(*i) != 0);

  return Changed;
}

/// createLoopExtractorPass 
///
FunctionPass* llvm::createLoopExtractorPass() {
  return new LoopExtractor();
}