Skip to content

[RISCV] Add a check in lowerSELECT after foldBinOpIntoSelectIfProfitable #97391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion llvm/lib/Target/RISCV/RISCVISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7664,7 +7664,11 @@ SDValue RISCVTargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
if (SDValue NewSel = foldBinOpIntoSelectIfProfitable(*Op->use_begin(),
DAG, Subtarget)) {
DAG.ReplaceAllUsesWith(BinOp, &NewSel);
return lowerSELECT(NewSel, DAG);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a comment explaining why the check is needed - it doesn't seem obvious.

// Opcode check is necessary because foldBinOpIntoSelectIfProfitable
// may return a constant node and cause crash in lowerSELECT.
if (NewSel.getOpcode() == ISD::SELECT)
return lowerSELECT(NewSel, DAG);
return NewSel;
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions llvm/test/CodeGen/RISCV/fold-binop-into-select-return-constant.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
; RUN: llc -mtriple=riscv64 -mattr=+m < %s | FileCheck %s

define i64 @fold_binop_into_select_return_constant(i1 %c) {
; CHECK-LABEL: fold_binop_into_select_return_constant:
; CHECK: # %bb.0: # %entry
; CHECK-NEXT: li a0, 0
; CHECK-NEXT: ret
entry:
%select1 = select i1 %c, i32 4, i32 8
%select2 = sext i32 %select1 to i64
%div1 = sdiv i64 %select2, -5141143369814759789
ret i64 %div1
}
Loading