Skip to content

Commit ffc459d

Browse files
authored
[RISCV] Add a check in lowerSELECT after foldBinOpIntoSelectIfProfitable (llvm#97391)
In certain case foldBinOpIntoSelectIfProfitable may return a constant node, the node will be lowered in lowerSELECT and lead to crash. This patch fix the bug by adding an extra check before lowerSELECT that do lowerSELECT as before when foldBinOpIntoSelectIfProfitable returns a select node, and return the node directly when foldBinOpIntoSelectIfProfitable returns a constant node. Fixes llvm#97390
1 parent 33112cb commit ffc459d

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7685,7 +7685,11 @@ SDValue RISCVTargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
76857685
if (SDValue NewSel = foldBinOpIntoSelectIfProfitable(*Op->use_begin(),
76867686
DAG, Subtarget)) {
76877687
DAG.ReplaceAllUsesWith(BinOp, &NewSel);
7688-
return lowerSELECT(NewSel, DAG);
7688+
// Opcode check is necessary because foldBinOpIntoSelectIfProfitable
7689+
// may return a constant node and cause crash in lowerSELECT.
7690+
if (NewSel.getOpcode() == ISD::SELECT)
7691+
return lowerSELECT(NewSel, DAG);
7692+
return NewSel;
76897693
}
76907694
}
76917695
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
2+
; RUN: llc -mtriple=riscv64 -mattr=+m < %s | FileCheck %s
3+
4+
define i64 @fold_binop_into_select_return_constant(i1 %c) {
5+
; CHECK-LABEL: fold_binop_into_select_return_constant:
6+
; CHECK: # %bb.0: # %entry
7+
; CHECK-NEXT: li a0, 0
8+
; CHECK-NEXT: ret
9+
entry:
10+
%select1 = select i1 %c, i32 4, i32 8
11+
%select2 = sext i32 %select1 to i64
12+
%div1 = sdiv i64 %select2, -5141143369814759789
13+
ret i64 %div1
14+
}

0 commit comments

Comments
 (0)