LoopExtractor.cpp revision c3c8703c3bf0d4fff3420ee3ce00caea992a354c
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 5// given function, it is not touched. This is a pass most useful for debugging 6// via bugpoint. 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> 16using namespace llvm; 17 18namespace { 19 20// FIXME: PassManager should allow Module passes to require FunctionPasses 21struct LoopExtractor : public FunctionPass { 22 23public: 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 34RegisterOpt<LoopExtractor> 35X("loop-extract", "Extract loops into new functions"); 36 37bool 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 44bool 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 62} // End anonymous namespace 63 64/// createLoopExtractorPass 65/// 66FunctionPass* llvm::createLoopExtractorPass() { 67 return new LoopExtractor(); 68} 69