blob: 3eda45265d237323fba8a519940eb645a7013f95 [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
Misha Brukmanf272f9b2004-03-02 00:19:09 +00005// given function, it is not touched. This is a pass most useful for debugging
6// via bugpoint.
Misha Brukman03a11342004-02-28 03:33:01 +00007//
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>
16using namespace llvm;
17
18namespace {
19
20// FIXME: PassManager should allow Module passes to require FunctionPasses
Misha Brukman8a2c28f2004-02-28 03:37:58 +000021struct LoopExtractor : public FunctionPass {
Misha Brukman03a11342004-02-28 03:33:01 +000022
23public:
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
34RegisterOpt<LoopExtractor>
35X("loop-extract", "Extract loops into new functions");
36
37bool 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
44bool 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
Misha Brukman03a11342004-02-28 03:33:01 +000062} // End anonymous namespace
63
64/// createLoopExtractorPass
65///
66FunctionPass* llvm::createLoopExtractorPass() {
Misha Brukman8a2c28f2004-02-28 03:37:58 +000067 return new LoopExtractor();
Misha Brukman03a11342004-02-28 03:33:01 +000068}