[llvm] [SCEV] Do not attempt to collect loop guards for loops without predecessor. (PR #123662)

Julian Nagele via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 22 09:19:42 PST 2025


https://github.com/juliannagele updated https://github.com/llvm/llvm-project/pull/123662

>From ed95df9a50554a0968b51d57a4342805d2276317 Mon Sep 17 00:00:00 2001
From: Julian Nagele <j.nagele at apple.com>
Date: Mon, 20 Jan 2025 18:38:36 +0000
Subject: [PATCH 1/2] [SCEV] Do not attempt to collect loop guards for loops
 without predecessor.

---
 llvm/lib/Analysis/ScalarEvolution.cpp         |  2 ++
 ...t-guard-info-with-multiple-predecessors.ll | 28 +++++++++++++++++++
 2 files changed, 30 insertions(+)

diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 7673c354817579..210c7cab965edb 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -15328,6 +15328,8 @@ ScalarEvolution::LoopGuards::collect(const Loop *L, ScalarEvolution &SE) {
   BasicBlock *Header = L->getHeader();
   BasicBlock *Pred = L->getLoopPredecessor();
   LoopGuards Guards(SE);
+  if (!Pred)
+    return Guards;
   SmallPtrSet<const BasicBlock *, 8> VisitedBlocks;
   collectFromBlock(SE, Guards, Header, Pred, VisitedBlocks);
   return Guards;
diff --git a/llvm/test/Analysis/ScalarEvolution/backedge-taken-count-guard-info-with-multiple-predecessors.ll b/llvm/test/Analysis/ScalarEvolution/backedge-taken-count-guard-info-with-multiple-predecessors.ll
index 46dccf454f21ac..6b100bfe5a8701 100644
--- a/llvm/test/Analysis/ScalarEvolution/backedge-taken-count-guard-info-with-multiple-predecessors.ll
+++ b/llvm/test/Analysis/ScalarEvolution/backedge-taken-count-guard-info-with-multiple-predecessors.ll
@@ -336,3 +336,31 @@ exit:
   ret void
 
 }
+
+; Checks correct traversal for loops without a unique predecessor
+; outside the loop.
+define void @pr122913() {
+; CHECK-LABEL: pr122913
+; CHECK-NEXT:  Determining loop execution counts for: @pr122913
+; CHECK-NEXT:  Loop %header: backedge-taken count is i1 false
+; CHECK-NEXT:  Loop %header: constant max backedge-taken count is i1 false
+; CHECK-NEXT:  Loop %header: symbolic max backedge-taken count is i1 false
+; CHECK-NEXT:  Loop %header: Trip multiple is 1
+entry:
+  br label %header
+
+bb:
+  br i1 1, label %exit, label %header
+
+header:
+  %0 = phi i32 [ %1, %body ], [ 0, %bb ], [ 0, %entry ]
+  br label %body
+
+body:
+  %1 = add i32 %0, 1
+  %2 = icmp ult i32 %1, 0
+  br i1 %2, label %header, label %exit
+
+exit:
+  ret void
+}

>From c9505905fb96fd11d41988313364e7fa191fe044 Mon Sep 17 00:00:00 2001
From: Julian Nagele <j.nagele at apple.com>
Date: Wed, 22 Jan 2025 17:19:07 +0000
Subject: [PATCH 2/2] fixup! [SCEV] Do not attempt to collect loop guards for
 loops without predecessor.

---
 ...ackedge-taken-count-guard-info-with-multiple-predecessors.ll | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/test/Analysis/ScalarEvolution/backedge-taken-count-guard-info-with-multiple-predecessors.ll b/llvm/test/Analysis/ScalarEvolution/backedge-taken-count-guard-info-with-multiple-predecessors.ll
index 6b100bfe5a8701..28035b05303db3 100644
--- a/llvm/test/Analysis/ScalarEvolution/backedge-taken-count-guard-info-with-multiple-predecessors.ll
+++ b/llvm/test/Analysis/ScalarEvolution/backedge-taken-count-guard-info-with-multiple-predecessors.ll
@@ -347,7 +347,7 @@ define void @pr122913() {
 ; CHECK-NEXT:  Loop %header: symbolic max backedge-taken count is i1 false
 ; CHECK-NEXT:  Loop %header: Trip multiple is 1
 entry:
-  br label %header
+  br i1 1, label %bb, label %header
 
 bb:
   br i1 1, label %exit, label %header



More information about the llvm-commits mailing list