Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 1 | //===- LoopExtractor.cpp - Extract each loop into a new function ----------===// |
Chris Lattner | 692a47a | 2004-03-14 02:34:07 +0000 | [diff] [blame] | 2 | // |
| 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 Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 9 | // |
| 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 Brukman | f272f9b | 2004-03-02 00:19:09 +0000 | [diff] [blame] | 12 | // given function, it is not touched. This is a pass most useful for debugging |
| 13 | // via bugpoint. |
Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chris Lattner | 78a996a | 2004-03-14 02:37:16 +0000 | [diff] [blame] | 17 | #include "llvm/Transforms/IPO.h" |
Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
| 19 | #include "llvm/Pass.h" |
| 20 | #include "llvm/Analysis/LoopInfo.h" |
Chris Lattner | 6c3e8c7 | 2004-03-14 04:01:06 +0000 | [diff] [blame^] | 21 | #include "llvm/Transforms/Scalar.h" |
Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 22 | #include "llvm/Transforms/Utils/FunctionUtils.h" |
Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
| 25 | namespace { |
Chris Lattner | 692a47a | 2004-03-14 02:34:07 +0000 | [diff] [blame] | 26 | // FIXME: PassManager should allow Module passes to require FunctionPasses |
| 27 | struct LoopExtractor : public FunctionPass { |
Chris Lattner | 692a47a | 2004-03-14 02:34:07 +0000 | [diff] [blame] | 28 | virtual bool runOnFunction(Function &F); |
| 29 | |
| 30 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 31 | AU.addRequired<LoopInfo>(); |
Chris Lattner | 6c3e8c7 | 2004-03-14 04:01:06 +0000 | [diff] [blame^] | 32 | AU.addRequiredID(LoopSimplifyID); |
Chris Lattner | 692a47a | 2004-03-14 02:34:07 +0000 | [diff] [blame] | 33 | } |
| 34 | }; |
Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 35 | |
Chris Lattner | 692a47a | 2004-03-14 02:34:07 +0000 | [diff] [blame] | 36 | RegisterOpt<LoopExtractor> |
| 37 | X("loop-extract", "Extract loops into new functions"); |
| 38 | } // End anonymous namespace |
Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 39 | |
Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 40 | bool LoopExtractor::runOnFunction(Function &F) { |
| 41 | std::cerr << F.getName() << "\n"; |
| 42 | |
| 43 | LoopInfo &LI = getAnalysis<LoopInfo>(); |
| 44 | |
| 45 | // We don't want to keep extracting the only loop of a function into a new one |
| 46 | if (LI.begin() == LI.end() || LI.begin() + 1 == LI.end()) |
| 47 | return false; |
| 48 | |
| 49 | bool Changed = false; |
| 50 | |
| 51 | // Try to move each loop out of the code into separate function |
| 52 | for (LoopInfo::iterator i = LI.begin(), e = LI.end(); i != e; ++i) |
| 53 | Changed |= (ExtractLoop(*i) != 0); |
| 54 | |
| 55 | return Changed; |
| 56 | } |
| 57 | |
Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 58 | /// createLoopExtractorPass |
| 59 | /// |
Chris Lattner | 78a996a | 2004-03-14 02:37:16 +0000 | [diff] [blame] | 60 | Pass* llvm::createLoopExtractorPass() { |
Misha Brukman | 8a2c28f | 2004-02-28 03:37:58 +0000 | [diff] [blame] | 61 | return new LoopExtractor(); |
Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 62 | } |