[llvm] 7c141e2 - [ValueTracking] Add missing check for two-value PN recurrence matching (#152700)

via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 8 08:39:27 PDT 2025


Author: Ivan R. Ivanov
Date: 2025-08-08T17:39:24+02:00
New Revision: 7c141e2118f9387e941478e6d4da133868876cf9

URL: https://github.com/llvm/llvm-project/commit/7c141e2118f9387e941478e6d4da133868876cf9
DIFF: https://github.com/llvm/llvm-project/commit/7c141e2118f9387e941478e6d4da133868876cf9.diff

LOG: [ValueTracking] Add missing check for two-value PN recurrence matching (#152700)

When InstTy is a type like IntrinsicInst which can have a variable
number of arguments, we can encounter a case where Operation will have
fewer than two arguments and error at the getOperand() calls.

Fixes: https://github.com/llvm/llvm-project/issues/152725.

Added: 
    llvm/test/Analysis/ValueTracking/pr152700.ll

Modified: 
    llvm/lib/Analysis/ValueTracking.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 1e70228905c33..b0e4b009f3501 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -9147,7 +9147,8 @@ static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst,
     return false;
 
   for (unsigned I = 0; I != 2; ++I) {
-    if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I))) {
+    if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
+        Operation && Operation->getNumOperands() >= 2) {
       Value *LHS = Operation->getOperand(0);
       Value *RHS = Operation->getOperand(1);
       if (LHS != PN && RHS != PN)

diff  --git a/llvm/test/Analysis/ValueTracking/pr152700.ll b/llvm/test/Analysis/ValueTracking/pr152700.ll
new file mode 100644
index 0000000000000..91644c543a173
--- /dev/null
+++ b/llvm/test/Analysis/ValueTracking/pr152700.ll
@@ -0,0 +1,28 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+declare noundef i32 @llvm.nvvm.read.ptx.sreg.nctaid.x()
+declare i32 @llvm.umin.i32(i32, i32)
+define i32 @foo(i1 %c, i32 %arg) {
+; CHECK-LABEL: define i32 @foo(
+; CHECK-SAME: i1 [[C:%.*]], i32 [[ARG:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:    [[I:%.*]] = call i32 @llvm.nvvm.read.ptx.sreg.nctaid.x()
+; CHECK-NEXT:    br i1 [[C]], label %[[BB_1:.*]], label %[[BB_2:.*]]
+; CHECK:       [[BB_1]]:
+; CHECK-NEXT:    br label %[[BB_2]]
+; CHECK:       [[BB_2]]:
+; CHECK-NEXT:    [[PHI:%.*]] = phi i32 [ [[I]], %[[ENTRY]] ], [ 0, %[[BB_1]] ]
+; CHECK-NEXT:    [[RES:%.*]] = call i32 @llvm.umin.i32(i32 [[PHI]], i32 [[ARG]])
+; CHECK-NEXT:    ret i32 [[RES]]
+;
+entry:
+  %i = call i32 @llvm.nvvm.read.ptx.sreg.nctaid.x()
+  br i1 %c, label %bb.1, label %bb.2
+bb.1:
+  br label %bb.2
+bb.2:
+  %phi = phi i32 [ %i, %entry ], [ 0, %bb.1 ]
+  %res = call i32 @llvm.umin.i32(i32 %phi, i32 %arg)
+  ret i32 %res
+}


        


More information about the llvm-commits mailing list