blob: 3aa5686b21000408ef21354cea481878f5a171ed [file] [log] [blame]
Misha Brukman03a11342004-02-28 03:33:01 +00001//===- LoopExtractor.cpp - Extract each loop into a new function ----------===//
Chris Lattner692a47a2004-03-14 02:34:07 +00002//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
Misha Brukman03a11342004-02-28 03:33:01 +00009//
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
Misha Brukmanf272f9b2004-03-02 00:19:09 +000012// given function, it is not touched. This is a pass most useful for debugging
13// via bugpoint.
Misha Brukman03a11342004-02-28 03:33:01 +000014//
15//===----------------------------------------------------------------------===//
16
Chris Lattner78a996a2004-03-14 02:37:16 +000017#include "llvm/Transforms/IPO.h"
Misha Brukman03a11342004-02-28 03:33:01 +000018#include "llvm/Module.h"
19#include "llvm/Pass.h"
20#include "llvm/Analysis/LoopInfo.h"
Misha Brukman03a11342004-02-28 03:33:01 +000021#include "llvm/Transforms/Utils/FunctionUtils.h"
Misha Brukman03a11342004-02-28 03:33:01 +000022using namespace llvm;
23
24namespace {
Chris Lattner692a47a2004-03-14 02:34:07 +000025 // 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);
29
30 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
31 AU.addRequired<LoopInfo>();
32 }
33 };
Misha Brukman03a11342004-02-28 03:33:01 +000034
Chris Lattner692a47a2004-03-14 02:34:07 +000035 RegisterOpt<LoopExtractor>
36 X("loop-extract", "Extract loops into new functions");
37} // End anonymous namespace
Misha Brukman03a11342004-02-28 03:33:01 +000038
39bool LoopExtractor::run(Module &M) {
40 bool Changed = false;
41 for (Module::iterator i = M.begin(), e = M.end(); i != e; ++i)
42 Changed |= runOnFunction(*i);
43 return Changed;
44}
45
46bool LoopExtractor::runOnFunction(Function &F) {
47 std::cerr << F.getName() << "\n";
48
49 LoopInfo &LI = getAnalysis<LoopInfo>();
50
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())
53 return false;
54
55 bool Changed = false;
56
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);
60
61 return Changed;
62}
63
Misha Brukman03a11342004-02-28 03:33:01 +000064/// createLoopExtractorPass
65///
Chris Lattner78a996a2004-03-14 02:37:16 +000066Pass* llvm::createLoopExtractorPass() {
Misha Brukman8a2c28f2004-02-28 03:37:58 +000067 return new LoopExtractor();
Misha Brukman03a11342004-02-28 03:33:01 +000068}