Skip to content

Commit 7cc3fbc

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.
1 parent 7ee421d commit 7cc3fbc

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7664,7 +7664,10 @@ 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+
if (NewSel.getOpcode() == ISD::SELECT)
7668+
return lowerSELECT(NewSel, DAG);
7669+
else
7670+
return NewSel;
76687671
}
76697672
}
76707673
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
; RUN: llc -mtriple=riscv64 -mattr=+m < %s | FileCheck %s
2+
3+
define i64 @fold_binop_into_select_return_constant(i1 %c) {
4+
; CHECK-LABEL: fold_binop_into_select_return_constant:
5+
; CHECK: # %bb.0: # %entry
6+
; CHECK-NEXT: li a0, 0
7+
; CHECK-NEXT: ret
8+
entry:
9+
%select1 = select i1 %c, i32 4, i32 8
10+
%select2 = sext i32 %select1 to i64
11+
%div1 = sdiv i64 %select2, -5141143369814759789
12+
ret i64 %div1
13+
}

0 commit comments

Comments
 (0)