Skip to content

Commit 7c141e2

Browse files
authored
[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: #152725.
1 parent 66734f4 commit 7c141e2

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9147,7 +9147,8 @@ static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst,
91479147
return false;
91489148

91499149
for (unsigned I = 0; I != 2; ++I) {
9150-
if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I))) {
9150+
if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
9151+
Operation && Operation->getNumOperands() >= 2) {
91519152
Value *LHS = Operation->getOperand(0);
91529153
Value *RHS = Operation->getOperand(1);
91539154
if (LHS != PN && RHS != PN)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
3+
4+
declare noundef i32 @llvm.nvvm.read.ptx.sreg.nctaid.x()
5+
declare i32 @llvm.umin.i32(i32, i32)
6+
define i32 @foo(i1 %c, i32 %arg) {
7+
; CHECK-LABEL: define i32 @foo(
8+
; CHECK-SAME: i1 [[C:%.*]], i32 [[ARG:%.*]]) {
9+
; CHECK-NEXT: [[ENTRY:.*]]:
10+
; CHECK-NEXT: [[I:%.*]] = call i32 @llvm.nvvm.read.ptx.sreg.nctaid.x()
11+
; CHECK-NEXT: br i1 [[C]], label %[[BB_1:.*]], label %[[BB_2:.*]]
12+
; CHECK: [[BB_1]]:
13+
; CHECK-NEXT: br label %[[BB_2]]
14+
; CHECK: [[BB_2]]:
15+
; CHECK-NEXT: [[PHI:%.*]] = phi i32 [ [[I]], %[[ENTRY]] ], [ 0, %[[BB_1]] ]
16+
; CHECK-NEXT: [[RES:%.*]] = call i32 @llvm.umin.i32(i32 [[PHI]], i32 [[ARG]])
17+
; CHECK-NEXT: ret i32 [[RES]]
18+
;
19+
entry:
20+
%i = call i32 @llvm.nvvm.read.ptx.sreg.nctaid.x()
21+
br i1 %c, label %bb.1, label %bb.2
22+
bb.1:
23+
br label %bb.2
24+
bb.2:
25+
%phi = phi i32 [ %i, %entry ], [ 0, %bb.1 ]
26+
%res = call i32 @llvm.umin.i32(i32 %phi, i32 %arg)
27+
ret i32 %res
28+
}

0 commit comments

Comments
 (0)