1 //===- LoopExtractor.cpp - Extract each loop into a new function ----------===//
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
8 //===----------------------------------------------------------------------===//
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"
20 // FIXME: PassManager should allow Module passes to require FunctionPasses
21 struct LoopExtractor : public FunctionPass {
25 virtual bool run(Module &M);
26 virtual bool runOnFunction(Function &F);
28 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
29 AU.addRequired<LoopInfo>();
34 RegisterOpt<LoopExtractor>
35 X("loop-extract", "Extract loops into new functions");
37 bool LoopExtractor::run(Module &M) {
39 for (Module::iterator i = M.begin(), e = M.end(); i != e; ++i)
40 Changed |= runOnFunction(*i);
44 bool LoopExtractor::runOnFunction(Function &F) {
45 std::cerr << F.getName() << "\n";
47 LoopInfo &LI = getAnalysis<LoopInfo>();
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())
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);
62 } // End anonymous namespace
64 /// createLoopExtractorPass
66 FunctionPass* llvm::createLoopExtractorPass() {
67 return new LoopExtractor();