[llvm] [DA] getBackedgeTakenCount in isKnownLessThan can return CouldNotCompute (PR #162495)

Sjoerd Meijer via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 10 04:56:15 PDT 2025


https://github.com/sjoerdmeijer updated https://github.com/llvm/llvm-project/pull/162495

>From ad56a507d2dcd5cbe04ebc9770323028e690bfbc Mon Sep 17 00:00:00 2001
From: Sjoerd Meijer <smeijer at nvidia.com>
Date: Wed, 8 Oct 2025 07:30:52 -0700
Subject: [PATCH 1/2] [DA] getBackedgeTakenCount in isKnownLessThan can return
 CouldNotCompute

Bail out when the backedge taken count is a CouldNotCompute SCEV in
function isKnownLessThan; we cannot and do not want to query things
like its Type.
---
 llvm/lib/Analysis/DependenceAnalysis.cpp      |  2 ++
 .../becount-couldnotcompute.ll                | 25 +++++++++++++++++++
 2 files changed, 27 insertions(+)
 create mode 100644 llvm/test/Analysis/DependenceAnalysis/becount-couldnotcompute.ll

diff --git a/llvm/lib/Analysis/DependenceAnalysis.cpp b/llvm/lib/Analysis/DependenceAnalysis.cpp
index 1f0da8d1830d3..9e474769abd83 100644
--- a/llvm/lib/Analysis/DependenceAnalysis.cpp
+++ b/llvm/lib/Analysis/DependenceAnalysis.cpp
@@ -1184,6 +1184,8 @@ bool DependenceInfo::isKnownLessThan(const SCEV *S, const SCEV *Size) const {
   if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S))
     if (AddRec->isAffine() && AddRec->hasNoSignedWrap()) {
       const SCEV *BECount = SE->getBackedgeTakenCount(AddRec->getLoop());
+      if (isa<SCEVCouldNotCompute>(BECount))
+        return false;
       const SCEV *Start = AddRec->getStart();
       const SCEV *Step = AddRec->getStepRecurrence(*SE);
       const SCEV *End = AddRec->evaluateAtIteration(BECount, *SE);
diff --git a/llvm/test/Analysis/DependenceAnalysis/becount-couldnotcompute.ll b/llvm/test/Analysis/DependenceAnalysis/becount-couldnotcompute.ll
new file mode 100644
index 0000000000000..4828858897072
--- /dev/null
+++ b/llvm/test/Analysis/DependenceAnalysis/becount-couldnotcompute.ll
@@ -0,0 +1,25 @@
+; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -disable-output "-passes=print<da>" -aa-pipeline=basic-aa 2>&1 | FileCheck %s
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32"
+target triple = "aarch64-unknown-linux-gnu"
+
+; Test for function isKnownLessThan that calculates a back-edge taken count,
+; which can return a CouldNotCompute SCEV.
+
+define void @test(i64 %conv, ptr %a) {
+; CHECK-LABEL: 'test'
+; CHECK-NEXT:  Src: %.pre.pre.pre = load i32, ptr %arrayidx12, align 4 --> Dst: %.pre.pre.pre = load i32, ptr %arrayidx12, align 4
+; CHECK-NEXT:    da analyze - none!
+;
+entry:
+  %sub = add i64 %conv, 1
+  br label %for.cond
+
+for.cond:
+  %i.0 = phi i64 [ %add26, %for.cond ], [ 0, %entry ]
+  %arrayidx12 = getelementptr i32, ptr %a, i64 %i.0
+  %.pre.pre.pre = load i32, ptr %arrayidx12, align 4
+  %add26 = add nsw i64 %sub, %i.0
+  br label %for.cond
+}

>From 0ecb0c77780c3f6011a9cad614493bf3d9d18b86 Mon Sep 17 00:00:00 2001
From: Sjoerd Meijer <smeijer at nvidia.com>
Date: Fri, 10 Oct 2025 04:55:08 -0700
Subject: [PATCH 2/2] Check base case, clean-up the test a bit.

---
 llvm/lib/Analysis/DependenceAnalysis.cpp      |  3 ++-
 .../becount-couldnotcompute.ll                | 19 ++++++++-----------
 2 files changed, 10 insertions(+), 12 deletions(-)

diff --git a/llvm/lib/Analysis/DependenceAnalysis.cpp b/llvm/lib/Analysis/DependenceAnalysis.cpp
index 9e474769abd83..60be55080418c 100644
--- a/llvm/lib/Analysis/DependenceAnalysis.cpp
+++ b/llvm/lib/Analysis/DependenceAnalysis.cpp
@@ -1184,8 +1184,9 @@ bool DependenceInfo::isKnownLessThan(const SCEV *S, const SCEV *Size) const {
   if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S))
     if (AddRec->isAffine() && AddRec->hasNoSignedWrap()) {
       const SCEV *BECount = SE->getBackedgeTakenCount(AddRec->getLoop());
+      // If the BTC cannot be computed, check the base case for S.
       if (isa<SCEVCouldNotCompute>(BECount))
-        return false;
+        return SE->isKnownNegative(SE->getMinusSCEV(S, Size));
       const SCEV *Start = AddRec->getStart();
       const SCEV *Step = AddRec->getStepRecurrence(*SE);
       const SCEV *End = AddRec->evaluateAtIteration(BECount, *SE);
diff --git a/llvm/test/Analysis/DependenceAnalysis/becount-couldnotcompute.ll b/llvm/test/Analysis/DependenceAnalysis/becount-couldnotcompute.ll
index 4828858897072..49fbad3510ae6 100644
--- a/llvm/test/Analysis/DependenceAnalysis/becount-couldnotcompute.ll
+++ b/llvm/test/Analysis/DependenceAnalysis/becount-couldnotcompute.ll
@@ -1,25 +1,22 @@
 ; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py UTC_ARGS: --version 6
 ; RUN: opt < %s -disable-output "-passes=print<da>" -aa-pipeline=basic-aa 2>&1 | FileCheck %s
 
-target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32"
-target triple = "aarch64-unknown-linux-gnu"
-
 ; Test for function isKnownLessThan that calculates a back-edge taken count,
 ; which can return a CouldNotCompute SCEV.
 
 define void @test(i64 %conv, ptr %a) {
 ; CHECK-LABEL: 'test'
-; CHECK-NEXT:  Src: %.pre.pre.pre = load i32, ptr %arrayidx12, align 4 --> Dst: %.pre.pre.pre = load i32, ptr %arrayidx12, align 4
+; CHECK-NEXT:  Src: %ld = load i32, ptr %arrayidx12, align 4 --> Dst: %ld = load i32, ptr %arrayidx12, align 4
 ; CHECK-NEXT:    da analyze - none!
 ;
 entry:
   %sub = add i64 %conv, 1
-  br label %for.cond
+  br label %loop
 
-for.cond:
-  %i.0 = phi i64 [ %add26, %for.cond ], [ 0, %entry ]
-  %arrayidx12 = getelementptr i32, ptr %a, i64 %i.0
-  %.pre.pre.pre = load i32, ptr %arrayidx12, align 4
-  %add26 = add nsw i64 %sub, %i.0
-  br label %for.cond
+loop:
+  %i = phi i64 [ %add26, %loop ], [ 0, %entry ]
+  %arrayidx12 = getelementptr i32, ptr %a, i64 %i
+  %ld = load i32, ptr %arrayidx12, align 4
+  %add26 = add nsw i64 %sub, %i
+  br label %loop
 }



More information about the llvm-commits mailing list