From e5d26179ed16fa3725b50490b0b819c9f1ba6aef Mon Sep 17 00:00:00 2001 From: Kit Barton Date: Fri, 11 Oct 2019 15:20:30 -0500 Subject: [PATCH 1/3] Step 2: Add isCandidate method and call it from run. --- .../llvm/Transforms/Scalar/LoopOptTutorial.h | 4 +++ .../lib/Transforms/Scalar/LoopOptTutorial.cpp | 33 ++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/llvm/include/llvm/Transforms/Scalar/LoopOptTutorial.h b/llvm/include/llvm/Transforms/Scalar/LoopOptTutorial.h index cad77541dfd6f1..4ed633dd336ec7 100644 --- a/llvm/include/llvm/Transforms/Scalar/LoopOptTutorial.h +++ b/llvm/include/llvm/Transforms/Scalar/LoopOptTutorial.h @@ -33,6 +33,10 @@ class LoopSplit { private: LoopInfo &LI; + + /// Determines if \p L is a candidate for splitting + bool isCandidate(const Loop &L) const; + }; class LoopOptTutorialPass : public PassInfoMixin { diff --git a/llvm/lib/Transforms/Scalar/LoopOptTutorial.cpp b/llvm/lib/Transforms/Scalar/LoopOptTutorial.cpp index c441a7123ec087..c8cf0a2c19093a 100644 --- a/llvm/lib/Transforms/Scalar/LoopOptTutorial.cpp +++ b/llvm/lib/Transforms/Scalar/LoopOptTutorial.cpp @@ -27,11 +27,42 @@ bool LoopSplit::run(Loop &L) const { LLVM_DEBUG(dbgs() << "Entering " << __func__ << "\n"); - LLVM_DEBUG(dbgs() << "TODO: Need to check if Loop is a valid candidate\n"); + if (isCandidate(L)) + LLVM_DEBUG(dbgs() << "Loop " << L.getName() + << " is a candidate for splitting!\n"); + else + LLVM_DEBUG(dbgs() << "Loop " << L.getName() + << " is not a candidate for splitting.\n"); return false; } +bool LoopSplit::isCandidate(const Loop &L) const { + // Require loops with preheaders and dedicated exits + if (!L.isLoopSimplifyForm()) + return false; + + // Since we use cloning to split the loop, it has to be safe to clone + if (!L.isSafeToClone()) + return false; + + // If the loop has multiple exiting blocks, do not split + if (!L.getExitingBlock()) + return false; + + // If loop has multiple exit blocks, do not split. + if (!L.getExitBlock()) + return false; + + // Only split innermost loops. Thus, if the loop has any children, it cannot + // be split. + //auto Children = L.getSubLoops(); + if (!L.getSubLoops().empty()) + return false; + + return true; +} + PreservedAnalyses LoopOptTutorialPass::run(Loop &L, LoopAnalysisManager &LAM, LoopStandardAnalysisResults &AR, LPMUpdater &U) { From 5e7e75a3baff15ed38a984222ce5da0aa24f385b Mon Sep 17 00:00:00 2001 From: Kit Barton Date: Thu, 17 Oct 2019 08:39:23 -0500 Subject: [PATCH 2/3] Incorporate minor changes from Tutorial_etiotto branch. --- .../lib/Transforms/Scalar/LoopOptTutorial.cpp | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/llvm/lib/Transforms/Scalar/LoopOptTutorial.cpp b/llvm/lib/Transforms/Scalar/LoopOptTutorial.cpp index c8cf0a2c19093a..e657ef163366c4 100644 --- a/llvm/lib/Transforms/Scalar/LoopOptTutorial.cpp +++ b/llvm/lib/Transforms/Scalar/LoopOptTutorial.cpp @@ -27,26 +27,28 @@ bool LoopSplit::run(Loop &L) const { LLVM_DEBUG(dbgs() << "Entering " << __func__ << "\n"); - if (isCandidate(L)) - LLVM_DEBUG(dbgs() << "Loop " << L.getName() - << " is a candidate for splitting!\n"); - else + if (!isCandidate(L)) { LLVM_DEBUG(dbgs() << "Loop " << L.getName() << " is not a candidate for splitting.\n"); + return false; + } + + LLVM_DEBUG(dbgs() << "Loop " << L.getName() + << " is a candidate for splitting!\n"); return false; } bool LoopSplit::isCandidate(const Loop &L) const { - // Require loops with preheaders and dedicated exits + // Require loops with preheaders and dedicated exits. if (!L.isLoopSimplifyForm()) return false; - // Since we use cloning to split the loop, it has to be safe to clone + // Since we use cloning to split the loop, it has to be safe to clone. if (!L.isSafeToClone()) return false; - // If the loop has multiple exiting blocks, do not split + // If the loop has multiple exiting blocks, do not split. if (!L.getExitingBlock()) return false; @@ -56,7 +58,6 @@ bool LoopSplit::isCandidate(const Loop &L) const { // Only split innermost loops. Thus, if the loop has any children, it cannot // be split. - //auto Children = L.getSubLoops(); if (!L.getSubLoops().empty()) return false; @@ -66,14 +67,10 @@ bool LoopSplit::isCandidate(const Loop &L) const { PreservedAnalyses LoopOptTutorialPass::run(Loop &L, LoopAnalysisManager &LAM, LoopStandardAnalysisResults &AR, LPMUpdater &U) { - bool Changed = false; - LLVM_DEBUG(dbgs() << "Entering LoopOptTutorialPass::run\n"); LLVM_DEBUG(dbgs() << "Loop: "; L.dump(); dbgs() << "\n"); - LoopSplit LS(AR.LI); - - Changed = LS.run(L); + bool Changed = LS(AR.LI).run(L); if (!Changed) return PreservedAnalyses::all(); From 19a0cd8cd0caf53f074e8b8f7f92c09dc4b7e2c8 Mon Sep 17 00:00:00 2001 From: Kit Barton Date: Thu, 17 Oct 2019 15:56:46 -0500 Subject: [PATCH 3/3] Incorporate minor changes from Tutorial_etiotto branch. --- llvm/lib/Transforms/Scalar/LoopOptTutorial.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/lib/Transforms/Scalar/LoopOptTutorial.cpp b/llvm/lib/Transforms/Scalar/LoopOptTutorial.cpp index e657ef163366c4..8e07f355384d13 100644 --- a/llvm/lib/Transforms/Scalar/LoopOptTutorial.cpp +++ b/llvm/lib/Transforms/Scalar/LoopOptTutorial.cpp @@ -70,7 +70,7 @@ PreservedAnalyses LoopOptTutorialPass::run(Loop &L, LoopAnalysisManager &LAM, LLVM_DEBUG(dbgs() << "Entering LoopOptTutorialPass::run\n"); LLVM_DEBUG(dbgs() << "Loop: "; L.dump(); dbgs() << "\n"); - bool Changed = LS(AR.LI).run(L); + bool Changed = LoopSplit(AR.LI).run(L); if (!Changed) return PreservedAnalyses::all();