blob: ce4d0f7e328a675d5be35cf96173841cde7fc781 [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"
Chris Lattner6c3e8c72004-03-14 04:01:06 +000021#include "llvm/Transforms/Scalar.h"
Misha Brukman03a11342004-02-28 03:33:01 +000022#include "llvm/Transforms/Utils/FunctionUtils.h"
Misha Brukman03a11342004-02-28 03:33:01 +000023using namespace llvm;
24
25namespace {
Chris Lattnera1672c12004-03-14 20:01:36 +000026 // FIXME: This is not a function pass, but the PassManager doesn't allow
27 // Module passes to require FunctionPasses, so we can't get loop info if we're
28 // not a function pass.
Chris Lattner692a47a2004-03-14 02:34:07 +000029 struct LoopExtractor : public FunctionPass {
Chris Lattnera1672c12004-03-14 20:01:36 +000030 unsigned NumLoops;
31
32 LoopExtractor(unsigned numLoops = ~0) : NumLoops(numLoops) {}
33
Chris Lattner692a47a2004-03-14 02:34:07 +000034 virtual bool runOnFunction(Function &F);
35
36 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
37 AU.addRequired<LoopInfo>();
Chris Lattner6c3e8c72004-03-14 04:01:06 +000038 AU.addRequiredID(LoopSimplifyID);
Chris Lattner692a47a2004-03-14 02:34:07 +000039 }
40 };
Misha Brukman03a11342004-02-28 03:33:01 +000041
Chris Lattner692a47a2004-03-14 02:34:07 +000042 RegisterOpt<LoopExtractor>
43 X("loop-extract", "Extract loops into new functions");
Chris Lattnera1672c12004-03-14 20:01:36 +000044
45 /// SingleLoopExtractor - For bugpoint.
46 struct SingleLoopExtractor : public LoopExtractor {
47 SingleLoopExtractor() : LoopExtractor(1) {}
48 };
49
50 RegisterOpt<SingleLoopExtractor>
51 Y("loop-extract-single", "Extract at most one loop into a new function");
Chris Lattner692a47a2004-03-14 02:34:07 +000052} // End anonymous namespace
Misha Brukman03a11342004-02-28 03:33:01 +000053
Misha Brukman03a11342004-02-28 03:33:01 +000054bool LoopExtractor::runOnFunction(Function &F) {
Misha Brukman03a11342004-02-28 03:33:01 +000055 LoopInfo &LI = getAnalysis<LoopInfo>();
56
57 // We don't want to keep extracting the only loop of a function into a new one
58 if (LI.begin() == LI.end() || LI.begin() + 1 == LI.end())
59 return false;
60
61 bool Changed = false;
62
63 // Try to move each loop out of the code into separate function
Chris Lattnera1672c12004-03-14 20:01:36 +000064 for (LoopInfo::iterator i = LI.begin(), e = LI.end(); i != e; ++i) {
65 if (NumLoops == 0) return Changed;
66 --NumLoops;
Misha Brukman03a11342004-02-28 03:33:01 +000067 Changed |= (ExtractLoop(*i) != 0);
Chris Lattnera1672c12004-03-14 20:01:36 +000068 }
Misha Brukman03a11342004-02-28 03:33:01 +000069
70 return Changed;
71}
72
Chris Lattnera1672c12004-03-14 20:01:36 +000073// createSingleLoopExtractorPass - This pass extracts one natural loop from the
74// program into a function if it can. This is used by bugpoint.
75//
76Pass *llvm::createSingleLoopExtractorPass() {
77 return new SingleLoopExtractor();
Misha Brukman03a11342004-02-28 03:33:01 +000078}