diff --git a/llvm/lib/Transforms/Utils/LoopUnroll.cpp b/llvm/lib/Transforms/Utils/LoopUnroll.cpp index 4d6112787a5261..17e4f0933ec86b 100644 --- a/llvm/lib/Transforms/Utils/LoopUnroll.cpp +++ b/llvm/lib/Transforms/Utils/LoopUnroll.cpp @@ -296,11 +296,6 @@ LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI, return LoopUnrollResult::Unmodified; } - if (ULO.TripCount != 0) - LLVM_DEBUG(dbgs() << " Trip Count = " << ULO.TripCount << "\n"); - if (ULO.TripMultiple != 1) - LLVM_DEBUG(dbgs() << " Trip Multiple = " << ULO.TripMultiple << "\n"); - // Don't enter the unroll code if there is nothing to do. if (ULO.TripCount == 0 && ULO.Count < 2) { LLVM_DEBUG(dbgs() << "Won't unroll; almost nothing to do\n"); @@ -357,6 +352,10 @@ LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI, } Info.ExitOnTrue = !L->contains(BI->getSuccessor(0)); Info.ExitingBlocks.push_back(ExitingBlock); + LLVM_DEBUG(dbgs() << " Exiting block %" << ExitingBlock->getName() + << ": TripCount=" << Info.TripCount + << ", TripMultiple=" << Info.TripMultiple + << ", BreakoutTrip=" << Info.BreakoutTrip << "\n"); } // Are we eliminating the loop control altogether? Note that we can know @@ -369,8 +368,8 @@ LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI, // We assume a run-time trip count if the compiler cannot // figure out the loop trip count and the unroll-runtime // flag is specified. - bool RuntimeTripCount = - !CompletelyUnroll && ULO.TripCount == 0 && ULO.AllowRuntime; + bool RuntimeTripCount = !CompletelyUnroll && ULO.TripCount == 0 && + ULO.TripMultiple % ULO.Count != 0 && ULO.AllowRuntime; // Go through all exits of L and see if there are any phi-nodes there. We just // conservatively assume that they're inserted to preserve LCSSA form, which @@ -418,7 +417,7 @@ LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI, UnrollRuntimeEpilog.getNumOccurrences() ? UnrollRuntimeEpilog : isEpilogProfitable(L); - if (RuntimeTripCount && ULO.TripMultiple % ULO.Count != 0 && + if (RuntimeTripCount && !UnrollRuntimeLoopRemainder(L, ULO.Count, ULO.AllowExpensiveTripCount, EpilogProfitability, ULO.UnrollRemainder, ULO.ForgetAllSCEV, LI, SE, DT, AC, TTI, @@ -432,62 +431,34 @@ LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI, } } - // If we know the trip count, we know the multiple... - // TODO: This is only used for the ORE code, remove it. - unsigned BreakoutTrip = 0; - if (ULO.TripCount != 0) { - BreakoutTrip = ULO.TripCount % ULO.Count; - ULO.TripMultiple = 0; - } else { - // Figure out what multiple to use. - BreakoutTrip = ULO.TripMultiple = - (unsigned)GreatestCommonDivisor64(ULO.Count, ULO.TripMultiple); - } - using namespace ore; // Report the unrolling decision. if (CompletelyUnroll) { LLVM_DEBUG(dbgs() << "COMPLETELY UNROLLING loop %" << Header->getName() - << " with trip count " << ULO.TripCount << "!\n"); + << " with trip count " << ULO.Count << "!\n"); if (ORE) ORE->emit([&]() { return OptimizationRemark(DEBUG_TYPE, "FullyUnrolled", L->getStartLoc(), L->getHeader()) << "completely unrolled loop with " - << NV("UnrollCount", ULO.TripCount) << " iterations"; + << NV("UnrollCount", ULO.Count) << " iterations"; }); } else { - auto DiagBuilder = [&]() { - OptimizationRemark Diag(DEBUG_TYPE, "PartialUnrolled", L->getStartLoc(), - L->getHeader()); - return Diag << "unrolled loop by a factor of " - << NV("UnrollCount", ULO.Count); - }; - LLVM_DEBUG(dbgs() << "UNROLLING loop %" << Header->getName() << " by " << ULO.Count); - if (ULO.TripMultiple == 0 || BreakoutTrip != ULO.TripMultiple) { - LLVM_DEBUG(dbgs() << " with a breakout at trip " << BreakoutTrip); - if (ORE) - ORE->emit([&]() { - return DiagBuilder() << " with a breakout at trip " - << NV("BreakoutTrip", BreakoutTrip); - }); - } else if (ULO.TripMultiple != 1) { - LLVM_DEBUG(dbgs() << " with " << ULO.TripMultiple << " trips per branch"); - if (ORE) - ORE->emit([&]() { - return DiagBuilder() - << " with " << NV("TripMultiple", ULO.TripMultiple) - << " trips per branch"; - }); - } else if (RuntimeTripCount) { + if (RuntimeTripCount) LLVM_DEBUG(dbgs() << " with run-time trip count"); - if (ORE) - ORE->emit( - [&]() { return DiagBuilder() << " with run-time trip count"; }); - } LLVM_DEBUG(dbgs() << "!\n"); + + if (ORE) + ORE->emit([&]() { + OptimizationRemark Diag(DEBUG_TYPE, "PartialUnrolled", L->getStartLoc(), + L->getHeader()); + Diag << "unrolled loop by a factor of " << NV("UnrollCount", ULO.Count); + if (RuntimeTripCount) + Diag << " with run-time trip count"; + return Diag; + }); } // We are going to make changes to this loop. SCEV may be keeping cached info diff --git a/llvm/test/CodeGen/AArch64/loop-micro-op-buffer-size-t99.ll b/llvm/test/CodeGen/AArch64/loop-micro-op-buffer-size-t99.ll index d64d533f97321d..73aae534299be9 100644 --- a/llvm/test/CodeGen/AArch64/loop-micro-op-buffer-size-t99.ll +++ b/llvm/test/CodeGen/AArch64/loop-micro-op-buffer-size-t99.ll @@ -5,15 +5,13 @@ target triple = "aarch64-unknown-linux-gnu" ; CHECK: Loop Unroll: F[foo] Loop %loop.header ; CHECK: Loop Size = 18 -; CHECK: Trip Count = 512 -; CHECK: Trip Multiple = 512 -; CHECK: UNROLLING loop %loop.header by 4 with a breakout at trip 0 +; CHECK: Exiting block %loop.inc: TripCount=512, TripMultiple=0, BreakoutTrip=0 +; CHECK: UNROLLING loop %loop.header by 4 ; CHECK: Merging: ; CHECK: Loop Unroll: F[foo] Loop %loop.2.header -; CHECK: Loop Size = 19 -; CHECK: Trip Count = 512 -; CHECK: Trip Multiple = 512 -; CHECK: UNROLLING loop %loop.2.header by 4 with a breakout at trip 0 +; CHECK: Loop Size = 19 +; CHECK: Exiting block %loop.2.inc: TripCount=512, TripMultiple=0, BreakoutTrip=0 +; CHECK: UNROLLING loop %loop.2.header by 4 ; CHECK: Merging: ; CHECK: %counter = phi i32 [ 0, %entry ], [ %inc.3, %loop.inc.3 ] ; CHECK: %val = add nuw nsw i32 %counter, 5 diff --git a/llvm/test/Transforms/LoopUnroll/loop-remarks-with-hotness.ll b/llvm/test/Transforms/LoopUnroll/loop-remarks-with-hotness.ll index eb3d05c08e7eda..58c7bd75f841a6 100644 --- a/llvm/test/Transforms/LoopUnroll/loop-remarks-with-hotness.ll +++ b/llvm/test/Transforms/LoopUnroll/loop-remarks-with-hotness.ll @@ -2,7 +2,7 @@ ; RUN: opt < %s -S -loop-unroll -pass-remarks=loop-unroll -pass-remarks-with-hotness -unroll-count=4 2>&1 | FileCheck -check-prefix=PARTIAL-UNROLL %s ; COMPLETE-UNROLL: remark: {{.*}}: completely unrolled loop with 16 iterations (hotness: 300) -; PARTIAL-UNROLL: remark: {{.*}}: unrolled loop by a factor of 4 {{.*}} (hotness: 300) +; PARTIAL-UNROLL: remark: {{.*}}: unrolled loop by a factor of 4 (hotness: 300) define i32 @sum() !prof !0 { entry: