1 //===- LoopExtractor.cpp - Extract each loop into a new function ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // A pass wrapper around the ExtractLoop() scalar transformation to extract each
11 // top-level loop into its own new function. If the loop is the ONLY loop in a
12 // given function, it is not touched. This is a pass most useful for debugging
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Transforms/IPO.h"
18 #include "llvm/Module.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Analysis/LoopInfo.h"
21 #include "llvm/Transforms/Utils/FunctionUtils.h"
25 // FIXME: PassManager should allow Module passes to require FunctionPasses
26 struct LoopExtractor : public FunctionPass {
27 virtual bool run(Module &M);
28 virtual bool runOnFunction(Function &F);
30 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
31 AU.addRequired<LoopInfo>();
35 RegisterOpt<LoopExtractor>
36 X("loop-extract", "Extract loops into new functions");
37 } // End anonymous namespace
39 bool LoopExtractor::run(Module &M) {
41 for (Module::iterator i = M.begin(), e = M.end(); i != e; ++i)
42 Changed |= runOnFunction(*i);
46 bool LoopExtractor::runOnFunction(Function &F) {
47 std::cerr << F.getName() << "\n";
49 LoopInfo &LI = getAnalysis<LoopInfo>();
51 // We don't want to keep extracting the only loop of a function into a new one
52 if (LI.begin() == LI.end() || LI.begin() + 1 == LI.end())
57 // Try to move each loop out of the code into separate function
58 for (LoopInfo::iterator i = LI.begin(), e = LI.end(); i != e; ++i)
59 Changed |= (ExtractLoop(*i) != 0);
64 /// createLoopExtractorPass
66 Pass* llvm::createLoopExtractorPass() {
67 return new LoopExtractor();