Skip to content

Commit fa10f1e

Browse files
committed
[RISCV] Add a check in lowerSELECT after foldBinOpIntoSelectIfProfitable
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.
1 parent 7ee421d commit fa10f1e

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
@@ -7664,7 +7664,11 @@ SDValue RISCVTargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
76647664
if (SDValue NewSel = foldBinOpIntoSelectIfProfitable(*Op->use_begin(),
76657665
DAG, Subtarget)) {
76667666
DAG.ReplaceAllUsesWith(BinOp, &NewSel);
7667-
return lowerSELECT(NewSel, DAG);
7667+
// Opcode check is necessary because foldBinOpIntoSelectIfProfitable
7668+
// may return a constant node and cause crash in lowerSELECT.
7669+
if (NewSel.getOpcode() == ISD::SELECT)
7670+
return lowerSELECT(NewSel, DAG);
7671+
return NewSel;
76687672
}
76697673
}
76707674
}
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)