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.
7 //===----------------------------------------------------------------------===//
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"
19 // FIXME: PassManager should allow Module passes to require FunctionPasses
20 struct LoopExtractor : public FunctionPass {
24 virtual bool run(Module &M);
25 virtual bool runOnFunction(Function &F);
27 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
28 AU.addRequired<LoopInfo>();
33 RegisterOpt<LoopExtractor>
34 X("loop-extract", "Extract loops into new functions");
36 bool LoopExtractor::run(Module &M) {
38 for (Module::iterator i = M.begin(), e = M.end(); i != e; ++i)
39 Changed |= runOnFunction(*i);
43 bool LoopExtractor::runOnFunction(Function &F) {
44 std::cerr << F.getName() << "\n";
46 LoopInfo &LI = getAnalysis<LoopInfo>();
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())
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);
63 } // End anonymous namespace
65 /// createLoopExtractorPass
67 FunctionPass* llvm::createLoopExtractorPass() {
68 return new LoopExtractor();