diff options
author | Misha Brukman <brukman+llvm@gmail.com> | 2004-02-28 03:33:01 +0000 |
---|---|---|
committer | Misha Brukman <brukman+llvm@gmail.com> | 2004-02-28 03:33:01 +0000 |
commit | 9401deb32079a855d291d041901c57bd332bfa87 (patch) | |
tree | 7eac49692f011b5b1fa0322948767f8ad3be25c8 /lib/Transforms/Utils | |
parent | e6336031b8b85a438e2fcaa9e56e7392afbbb7dc (diff) | |
download | external_llvm-9401deb32079a855d291d041901c57bd332bfa87.zip external_llvm-9401deb32079a855d291d041901c57bd332bfa87.tar.gz external_llvm-9401deb32079a855d291d041901c57bd332bfa87.tar.bz2 |
A pass that uses the generic CodeExtractor to rip out *every* loop in every
function, as long as the loop isn't the only one in that function. This should
help debugging passes easier with BugPoint.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@11936 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms/Utils')
-rw-r--r-- | lib/Transforms/Utils/LoopExtractor.cpp | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/lib/Transforms/Utils/LoopExtractor.cpp b/lib/Transforms/Utils/LoopExtractor.cpp new file mode 100644 index 0000000..aae7ccb --- /dev/null +++ b/lib/Transforms/Utils/LoopExtractor.cpp @@ -0,0 +1,69 @@ +//===- LoopExtractor.cpp - Extract each loop into a new function ----------===// +// +// A pass wrapper around the ExtractLoop() scalar transformation to extract each +// top-level loop into its own new function. If the loop is the ONLY loop in a +// given function, it is not touched. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Module.h" +#include "llvm/Pass.h" +#include "llvm/Analysis/LoopInfo.h" +#include "llvm/Transforms/Scalar.h" +#include "llvm/Transforms/Utils/FunctionUtils.h" +#include <vector> +using namespace llvm; + +namespace { + +// FIXME: PassManager should allow Module passes to require FunctionPasses +struct LoopExtraction : public FunctionPass { + +public: + LoopExtractor() {} + virtual bool run(Module &M); + virtual bool runOnFunction(Function &F); + + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.addRequired<LoopInfo>(); + } + +}; + +RegisterOpt<LoopExtractor> +X("loop-extract", "Extract loops into new functions"); + +bool LoopExtractor::run(Module &M) { + bool Changed = false; + for (Module::iterator i = M.begin(), e = M.end(); i != e; ++i) + Changed |= runOnFunction(*i); + return Changed; +} + +bool LoopExtractor::runOnFunction(Function &F) { + std::cerr << F.getName() << "\n"; + + LoopInfo &LI = getAnalysis<LoopInfo>(); + + // We don't want to keep extracting the only loop of a function into a new one + if (LI.begin() == LI.end() || LI.begin() + 1 == LI.end()) + return false; + + bool Changed = false; + + // Try to move each loop out of the code into separate function + for (LoopInfo::iterator i = LI.begin(), e = LI.end(); i != e; ++i) + Changed |= (ExtractLoop(*i) != 0); + + return Changed; +} + + + +} // End anonymous namespace + +/// createLoopExtractorPass +/// +FunctionPass* llvm::createLoopExtractorPass() { + return new LoopExtractor; +} |