Skip to content

Commit

Permalink
[LegalizeTypes][RISCV] Soften FCOPYSIGN operand
Browse files Browse the repository at this point in the history
Summary: Adds support for softening FCOPYSIGN operands.
Adds RISC-V tests that exercise the new softening code.

Reviewers: asb, lenary, efriedma
Reviewed By: efriedma
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D70679
  • Loading branch information
luismarques committed Nov 26, 2019
1 parent d7be3ea commit 6fd4c42
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
35 changes: 35 additions & 0 deletions llvm/lib/CodeGen/SelectionDAG/LegalizeFloatTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,7 @@ bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
case ISD::SELECT_CC: Res = SoftenFloatOp_SELECT_CC(N); break;
case ISD::SETCC: Res = SoftenFloatOp_SETCC(N); break;
case ISD::STORE: Res = SoftenFloatOp_STORE(N, OpNo); break;
case ISD::FCOPYSIGN: Res = SoftenFloatOp_FCOPYSIGN(N); break;
}

// If the result is null, the sub-method took care of registering results etc.
Expand Down Expand Up @@ -1036,6 +1037,40 @@ SDValue DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
ST->getMemOperand());
}

SDValue DAGTypeLegalizer::SoftenFloatOp_FCOPYSIGN(SDNode *N) {
SDValue LHS = N->getOperand(0);
SDValue RHS = BitConvertToInteger(N->getOperand(1));
SDLoc dl(N);

EVT LVT = LHS.getValueType();
EVT ILVT = EVT::getIntegerVT(*DAG.getContext(), LVT.getSizeInBits());
EVT RVT = RHS.getValueType();

unsigned LSize = LVT.getSizeInBits();
unsigned RSize = RVT.getSizeInBits();

// Shift right or sign-extend it if the two operands have different types.
int SizeDiff = RSize - LSize;
if (SizeDiff > 0) {
RHS =
DAG.getNode(ISD::SRL, dl, RVT, RHS,
DAG.getConstant(SizeDiff, dl,
TLI.getShiftAmountTy(RHS.getValueType(),
DAG.getDataLayout())));
RHS = DAG.getNode(ISD::TRUNCATE, dl, ILVT, RHS);
} else if (SizeDiff < 0) {
RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LVT, RHS);
RHS =
DAG.getNode(ISD::SHL, dl, ILVT, RHS,
DAG.getConstant(-SizeDiff, dl,
TLI.getShiftAmountTy(RHS.getValueType(),
DAG.getDataLayout())));
}

RHS = DAG.getBitcast(LVT, RHS);
return DAG.getNode(ISD::FCOPYSIGN, dl, LVT, LHS, RHS);
}

SDValue DAGTypeLegalizer::SoftenFloatOp_LROUND(SDNode *N) {
EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));

Expand Down
1 change: 1 addition & 0 deletions llvm/lib/CodeGen/SelectionDAG/LegalizeTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ class LLVM_LIBRARY_VISIBILITY DAGTypeLegalizer {
SDValue SoftenFloatOp_SELECT_CC(SDNode *N);
SDValue SoftenFloatOp_SETCC(SDNode *N);
SDValue SoftenFloatOp_STORE(SDNode *N, unsigned OpNo);
SDValue SoftenFloatOp_FCOPYSIGN(SDNode *N);

//===--------------------------------------------------------------------===//
// Float Expansion Support: LegalizeFloatTypes.cpp
Expand Down
18 changes: 18 additions & 0 deletions llvm/test/CodeGen/RISCV/copysign-casts.ll
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
; RUN: | FileCheck %s -check-prefix=RV32I
; RUN: llc -mtriple=riscv64 -verify-machineinstrs < %s \
; RUN: | FileCheck %s -check-prefix=RV64I
; RUN: llc -mtriple=riscv32 -verify-machineinstrs -mattr=+f \
; RUN: -target-abi ilp32f < %s | FileCheck %s -check-prefix=RV32IF
; RUN: llc -mtriple=riscv32 -verify-machineinstrs -mattr=+f -mattr=+d \
; RUN: -target-abi ilp32d < %s | FileCheck %s -check-prefix=RV32IFD
; RUN: llc -mtriple=riscv64 -verify-machineinstrs -mattr=+f -mattr=+d \
Expand Down Expand Up @@ -37,6 +39,16 @@ define double @fold_promote(double %a, float %b) nounwind {
; RV64I-NEXT: or a0, a0, a1
; RV64I-NEXT: ret
;
; RV32IF-LABEL: fold_promote:
; RV32IF: # %bb.0:
; RV32IF-NEXT: fmv.x.w a2, fa0
; RV32IF-NEXT: lui a3, 524288
; RV32IF-NEXT: and a2, a2, a3
; RV32IF-NEXT: addi a3, a3, -1
; RV32IF-NEXT: and a1, a1, a3
; RV32IF-NEXT: or a1, a1, a2
; RV32IF-NEXT: ret
;
; RV32IFD-LABEL: fold_promote:
; RV32IFD: # %bb.0:
; RV32IFD-NEXT: fcvt.d.s ft0, fa1
Expand Down Expand Up @@ -75,6 +87,12 @@ define float @fold_demote(float %a, double %b) nounwind {
; RV64I-NEXT: or a0, a0, a1
; RV64I-NEXT: ret
;
; RV32IF-LABEL: fold_demote:
; RV32IF: # %bb.0:
; RV32IF-NEXT: fmv.w.x ft0, a1
; RV32IF-NEXT: fsgnj.s fa0, fa0, ft0
; RV32IF-NEXT: ret
;
; RV32IFD-LABEL: fold_demote:
; RV32IFD: # %bb.0:
; RV32IFD-NEXT: fcvt.s.d ft0, fa1
Expand Down

0 comments on commit 6fd4c42

Please sign in to comment.