Skip to content

Commit 3b721ca

Browse files
committed
Refine const zero casting logic
1 parent 082474e commit 3b721ca

File tree

2 files changed

+94
-7
lines changed

2 files changed

+94
-7
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9584,7 +9584,7 @@ SDValue RISCVTargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
95849584
if (SDValue V = lowerSelectToBinOp(Op.getNode(), DAG, Subtarget))
95859585
return V;
95869586

9587-
// When there is no cost for GPR <-> FGPR, we can use zicond select for
9587+
// When there is no cost for GPR <-> FPR, we can use zicond select for
95889588
// floating value when CondV is int type
95899589
bool FPinGPR = Subtarget.hasStdExtZfinx();
95909590

@@ -9599,12 +9599,9 @@ SDValue RISCVTargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
95999599
MVT XLenIntVT = Subtarget.getXLenVT();
96009600

96019601
auto CastToInt = [&](SDValue V) -> SDValue {
9602-
// Treat +0.0 as integer 0 to enable single 'czero' instruction
9603-
// generation.
9604-
if (auto *CFP = dyn_cast<ConstantFPSDNode>(V)) {
9605-
if (CFP->isZero() && !CFP->isNegative())
9606-
return DAG.getConstant(0, DL, XLenIntVT);
9607-
}
9602+
// Treat +0.0 as int 0 to enable single 'czero' instruction generation.
9603+
if (isNullFPConstant(V))
9604+
return DAG.getConstant(0, DL, XLenIntVT);
96089605

96099606
if (VT == MVT::f16)
96109607
return DAG.getNode(RISCVISD::FMV_X_ANYEXTH, DL, XLenIntVT, V);

llvm/test/CodeGen/RISCV/zicond-fp-select-zfinx.ll

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,3 +546,93 @@ entry:
546546
%sel = select i1 %cond, float %t, float 0.000000e+00
547547
ret float %sel
548548
}
549+
550+
; -----------------------------------------------------------------------------
551+
; Test select with i1 condition and zero ret val for half fp (cond ? a : 0)
552+
; -----------------------------------------------------------------------------
553+
define dso_local noundef half @select_i1_half_0(i1 %cond, half %val) nounwind {
554+
; RV64ZDINX_ZICOND-LABEL: select_i1_half_0:
555+
; RV64ZDINX_ZICOND: # %bb.0: # %entry
556+
; RV64ZDINX_ZICOND-NEXT: # kill: def $x11_w killed $x11_w def $x11
557+
; RV64ZDINX_ZICOND-NEXT: andi a0, a0, 1
558+
; RV64ZDINX_ZICOND-NEXT: czero.eqz a0, a1, a0
559+
; RV64ZDINX_ZICOND-NEXT: lui a1, 1048560
560+
; RV64ZDINX_ZICOND-NEXT: or a0, a0, a1
561+
; RV64ZDINX_ZICOND-NEXT: # kill: def $x10_w killed $x10_w killed $x10
562+
; RV64ZDINX_ZICOND-NEXT: ret
563+
;
564+
; RV64ZDINX_NOZICOND-LABEL: select_i1_half_0:
565+
; RV64ZDINX_NOZICOND: # %bb.0: # %entry
566+
; RV64ZDINX_NOZICOND-NEXT: # kill: def $x11_w killed $x11_w def $x11
567+
; RV64ZDINX_NOZICOND-NEXT: slli a0, a0, 63
568+
; RV64ZDINX_NOZICOND-NEXT: srai a0, a0, 63
569+
; RV64ZDINX_NOZICOND-NEXT: and a0, a0, a1
570+
; RV64ZDINX_NOZICOND-NEXT: lui a1, 1048560
571+
; RV64ZDINX_NOZICOND-NEXT: or a0, a0, a1
572+
; RV64ZDINX_NOZICOND-NEXT: # kill: def $x10_w killed $x10_w killed $x10
573+
; RV64ZDINX_NOZICOND-NEXT: ret
574+
;
575+
; RV64ZHINX_ZICOND-LABEL: select_i1_half_0:
576+
; RV64ZHINX_ZICOND: # %bb.0: # %entry
577+
; RV64ZHINX_ZICOND-NEXT: # kill: def $x11_h killed $x11_h def $x11
578+
; RV64ZHINX_ZICOND-NEXT: andi a0, a0, 1
579+
; RV64ZHINX_ZICOND-NEXT: czero.eqz a0, a1, a0
580+
; RV64ZHINX_ZICOND-NEXT: # kill: def $x10_h killed $x10_h killed $x10
581+
; RV64ZHINX_ZICOND-NEXT: ret
582+
;
583+
; RV64FD-LABEL: select_i1_half_0:
584+
; RV64FD: # %bb.0: # %entry
585+
; RV64FD-NEXT: fmv.x.w a1, fa0
586+
; RV64FD-NEXT: slli a0, a0, 63
587+
; RV64FD-NEXT: srai a0, a0, 63
588+
; RV64FD-NEXT: and a0, a0, a1
589+
; RV64FD-NEXT: lui a1, 1048560
590+
; RV64FD-NEXT: or a0, a0, a1
591+
; RV64FD-NEXT: fmv.w.x fa0, a0
592+
; RV64FD-NEXT: ret
593+
;
594+
; RV32ZFINX_ZICOND-LABEL: select_i1_half_0:
595+
; RV32ZFINX_ZICOND: # %bb.0: # %entry
596+
; RV32ZFINX_ZICOND-NEXT: # kill: def $x11_w killed $x11_w def $x11
597+
; RV32ZFINX_ZICOND-NEXT: andi a0, a0, 1
598+
; RV32ZFINX_ZICOND-NEXT: czero.eqz a0, a1, a0
599+
; RV32ZFINX_ZICOND-NEXT: lui a1, 1048560
600+
; RV32ZFINX_ZICOND-NEXT: or a0, a0, a1
601+
; RV32ZFINX_ZICOND-NEXT: # kill: def $x10_w killed $x10_w killed $x10
602+
; RV32ZFINX_ZICOND-NEXT: ret
603+
;
604+
; RV32ZFINX_NOZICOND-LABEL: select_i1_half_0:
605+
; RV32ZFINX_NOZICOND: # %bb.0: # %entry
606+
; RV32ZFINX_NOZICOND-NEXT: # kill: def $x11_w killed $x11_w def $x11
607+
; RV32ZFINX_NOZICOND-NEXT: slli a0, a0, 31
608+
; RV32ZFINX_NOZICOND-NEXT: srai a0, a0, 31
609+
; RV32ZFINX_NOZICOND-NEXT: and a0, a0, a1
610+
; RV32ZFINX_NOZICOND-NEXT: lui a1, 1048560
611+
; RV32ZFINX_NOZICOND-NEXT: or a0, a0, a1
612+
; RV32ZFINX_NOZICOND-NEXT: # kill: def $x10_w killed $x10_w killed $x10
613+
; RV32ZFINX_NOZICOND-NEXT: ret
614+
;
615+
; RV32ZDINX_ZICOND-LABEL: select_i1_half_0:
616+
; RV32ZDINX_ZICOND: # %bb.0: # %entry
617+
; RV32ZDINX_ZICOND-NEXT: # kill: def $x11_w killed $x11_w def $x11
618+
; RV32ZDINX_ZICOND-NEXT: andi a0, a0, 1
619+
; RV32ZDINX_ZICOND-NEXT: czero.eqz a0, a1, a0
620+
; RV32ZDINX_ZICOND-NEXT: lui a1, 1048560
621+
; RV32ZDINX_ZICOND-NEXT: or a0, a0, a1
622+
; RV32ZDINX_ZICOND-NEXT: # kill: def $x10_w killed $x10_w killed $x10
623+
; RV32ZDINX_ZICOND-NEXT: ret
624+
;
625+
; RV32ZDINX_NOZICOND-LABEL: select_i1_half_0:
626+
; RV32ZDINX_NOZICOND: # %bb.0: # %entry
627+
; RV32ZDINX_NOZICOND-NEXT: # kill: def $x11_w killed $x11_w def $x11
628+
; RV32ZDINX_NOZICOND-NEXT: slli a0, a0, 31
629+
; RV32ZDINX_NOZICOND-NEXT: srai a0, a0, 31
630+
; RV32ZDINX_NOZICOND-NEXT: and a0, a0, a1
631+
; RV32ZDINX_NOZICOND-NEXT: lui a1, 1048560
632+
; RV32ZDINX_NOZICOND-NEXT: or a0, a0, a1
633+
; RV32ZDINX_NOZICOND-NEXT: # kill: def $x10_w killed $x10_w killed $x10
634+
; RV32ZDINX_NOZICOND-NEXT: ret
635+
entry:
636+
%sel = select i1 %cond, half %val, half 0xH0000
637+
ret half %sel
638+
}

0 commit comments

Comments
 (0)