blob: da97b5f62c29d51e3e820854b5d3c033d27a6a28 [file] [log] [blame]
Misha Brukman03a11342004-02-28 03:33:01 +00001//===- 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>
15using namespace llvm;
16
17namespace {
18
19// FIXME: PassManager should allow Module passes to require FunctionPasses
Misha Brukman8a2c28f2004-02-28 03:37:58 +000020struct LoopExtractor : public FunctionPass {
Misha Brukman03a11342004-02-28 03:33:01 +000021
22public:
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
33RegisterOpt<LoopExtractor>
34X("loop-extract", "Extract loops into new functions");
35
36bool 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
43bool 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///
67FunctionPass* llvm::createLoopExtractorPass() {
Misha Brukman8a2c28f2004-02-28 03:37:58 +000068 return new LoopExtractor();
Misha Brukman03a11342004-02-28 03:33:01 +000069}