blob: 480f0f7c03d79bf818c4aebd0d377c5ab57880a1 [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
17#include "llvm/Module.h"
18#include "llvm/Pass.h"
19#include "llvm/Analysis/LoopInfo.h"
20#include "llvm/Transforms/Scalar.h"
21#include "llvm/Transforms/Utils/FunctionUtils.h"
22#include <vector>
23using namespace llvm;
24
25namespace {
Chris Lattner692a47a2004-03-14 02:34:07 +000026 // FIXME: PassManager should allow Module passes to require FunctionPasses
27 struct LoopExtractor : public FunctionPass {
28 virtual bool run(Module &M);
29 virtual bool runOnFunction(Function &F);
30
31 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
32 AU.addRequired<LoopInfo>();
33 }
34 };
Misha Brukman03a11342004-02-28 03:33:01 +000035
Chris Lattner692a47a2004-03-14 02:34:07 +000036 RegisterOpt<LoopExtractor>
37 X("loop-extract", "Extract loops into new functions");
38} // End anonymous namespace
Misha Brukman03a11342004-02-28 03:33:01 +000039
40bool LoopExtractor::run(Module &M) {
41 bool Changed = false;
42 for (Module::iterator i = M.begin(), e = M.end(); i != e; ++i)
43 Changed |= runOnFunction(*i);
44 return Changed;
45}
46
47bool LoopExtractor::runOnFunction(Function &F) {
48 std::cerr << F.getName() << "\n";
49
50 LoopInfo &LI = getAnalysis<LoopInfo>();
51
52 // We don't want to keep extracting the only loop of a function into a new one
53 if (LI.begin() == LI.end() || LI.begin() + 1 == LI.end())
54 return false;
55
56 bool Changed = false;
57
58 // Try to move each loop out of the code into separate function
59 for (LoopInfo::iterator i = LI.begin(), e = LI.end(); i != e; ++i)
60 Changed |= (ExtractLoop(*i) != 0);
61
62 return Changed;
63}
64
Misha Brukman03a11342004-02-28 03:33:01 +000065/// createLoopExtractorPass
66///
67FunctionPass* llvm::createLoopExtractorPass() {
Misha Brukman8a2c28f2004-02-28 03:37:58 +000068 return new LoopExtractor();
Misha Brukman03a11342004-02-28 03:33:01 +000069}