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