From 60baa2e015c4254e0fd1791cf2a53e6e4c709d78 Mon Sep 17 00:00:00 2001 From: Alex Bradbury Date: Thu, 12 Apr 2018 05:47:15 +0000 Subject: [PATCH] [RISCV] Codegen support for RV32D floating point conversion operations This also includes support and a test for truncating stores, which are now possible thanks to the fpround pattern. llvm-svn: 329876 --- llvm/lib/Target/RISCV/RISCVISelLowering.cpp | 1 + llvm/lib/Target/RISCV/RISCVInstrInfoD.td | 14 ++++ llvm/test/CodeGen/RISCV/double-convert.ll | 89 +++++++++++++++++++++ llvm/test/CodeGen/RISCV/double-mem.ll | 17 ++++ 4 files changed, 121 insertions(+) create mode 100644 llvm/test/CodeGen/RISCV/double-convert.ll diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp index 8b006d2d3212..27e0d7f00855 100644 --- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp +++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp @@ -125,6 +125,7 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM, setOperationAction(ISD::FMINNUM, MVT::f64, Legal); setOperationAction(ISD::FMAXNUM, MVT::f64, Legal); setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f32, Expand); + setTruncStoreAction(MVT::f64, MVT::f32, Expand); } setOperationAction(ISD::GlobalAddress, XLenVT, Custom); diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfoD.td b/llvm/lib/Target/RISCV/RISCVInstrInfoD.td index 13e8a3456d69..8f6c7c85a13c 100644 --- a/llvm/lib/Target/RISCV/RISCVInstrInfoD.td +++ b/llvm/lib/Target/RISCV/RISCVInstrInfoD.td @@ -199,6 +199,20 @@ class PatFpr64Fpr64DynFrm let Predicates = [HasStdExtD] in { +/// Float conversion operations + +// f64 -> f32, f32 -> f64 +def : Pat<(fpround FPR64:$rs1), (FCVT_S_D FPR64:$rs1, 0b111)>; +def : Pat<(fpextend FPR32:$rs1), (FCVT_D_S FPR32:$rs1)>; + +// FP->[u]int. Round-to-zero must be used +def : Pat<(fp_to_sint FPR64:$rs1), (FCVT_W_D FPR64:$rs1, 0b001)>; +def : Pat<(fp_to_uint FPR64:$rs1), (FCVT_WU_D FPR64:$rs1, 0b001)>; + +// [u]int->fp +def : Pat<(sint_to_fp GPR:$rs1), (FCVT_D_W GPR:$rs1)>; +def : Pat<(uint_to_fp GPR:$rs1), (FCVT_D_WU GPR:$rs1)>; + /// Float arithmetic operations def : PatFpr64Fpr64DynFrm; diff --git a/llvm/test/CodeGen/RISCV/double-convert.ll b/llvm/test/CodeGen/RISCV/double-convert.ll new file mode 100644 index 000000000000..44de78dc7402 --- /dev/null +++ b/llvm/test/CodeGen/RISCV/double-convert.ll @@ -0,0 +1,89 @@ +; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py +; RUN: llc -mtriple=riscv32 -mattr=+d -verify-machineinstrs < %s \ +; RUN: | FileCheck -check-prefix=RV32IFD %s + +define float @fcvt_s_d(double %a) nounwind { +; RV32IFD-LABEL: fcvt_s_d: +; RV32IFD: # %bb.0: +; RV32IFD-NEXT: addi sp, sp, -16 +; RV32IFD-NEXT: sw a0, 8(sp) +; RV32IFD-NEXT: sw a1, 12(sp) +; RV32IFD-NEXT: fld ft0, 8(sp) +; RV32IFD-NEXT: fcvt.s.d ft0, ft0 +; RV32IFD-NEXT: fmv.x.w a0, ft0 +; RV32IFD-NEXT: addi sp, sp, 16 +; RV32IFD-NEXT: ret + %1 = fptrunc double %a to float + ret float %1 +} + +define double @fcvt_d_s(float %a) nounwind { +; RV32IFD-LABEL: fcvt_d_s: +; RV32IFD: # %bb.0: +; RV32IFD-NEXT: addi sp, sp, -16 +; RV32IFD-NEXT: fmv.w.x ft0, a0 +; RV32IFD-NEXT: fcvt.d.s ft0, ft0 +; RV32IFD-NEXT: fsd ft0, 8(sp) +; RV32IFD-NEXT: lw a0, 8(sp) +; RV32IFD-NEXT: lw a1, 12(sp) +; RV32IFD-NEXT: addi sp, sp, 16 +; RV32IFD-NEXT: ret + %1 = fpext float %a to double + ret double %1 +} + +define i32 @fcvt_w_d(double %a) nounwind { +; RV32IFD-LABEL: fcvt_w_d: +; RV32IFD: # %bb.0: +; RV32IFD-NEXT: addi sp, sp, -16 +; RV32IFD-NEXT: sw a0, 8(sp) +; RV32IFD-NEXT: sw a1, 12(sp) +; RV32IFD-NEXT: fld ft0, 8(sp) +; RV32IFD-NEXT: fcvt.w.d a0, ft0, rtz +; RV32IFD-NEXT: addi sp, sp, 16 +; RV32IFD-NEXT: ret + %1 = fptosi double %a to i32 + ret i32 %1 +} + +define i32 @fcvt_wu_d(double %a) nounwind { +; RV32IFD-LABEL: fcvt_wu_d: +; RV32IFD: # %bb.0: +; RV32IFD-NEXT: addi sp, sp, -16 +; RV32IFD-NEXT: sw a0, 8(sp) +; RV32IFD-NEXT: sw a1, 12(sp) +; RV32IFD-NEXT: fld ft0, 8(sp) +; RV32IFD-NEXT: fcvt.wu.d a0, ft0, rtz +; RV32IFD-NEXT: addi sp, sp, 16 +; RV32IFD-NEXT: ret + %1 = fptoui double %a to i32 + ret i32 %1 +} + +define double @fcvt_d_w(i32 %a) nounwind { +; RV32IFD-LABEL: fcvt_d_w: +; RV32IFD: # %bb.0: +; RV32IFD-NEXT: addi sp, sp, -16 +; RV32IFD-NEXT: fcvt.d.w ft0, a0 +; RV32IFD-NEXT: fsd ft0, 8(sp) +; RV32IFD-NEXT: lw a0, 8(sp) +; RV32IFD-NEXT: lw a1, 12(sp) +; RV32IFD-NEXT: addi sp, sp, 16 +; RV32IFD-NEXT: ret + %1 = sitofp i32 %a to double + ret double %1 +} + +define double @fcvt_d_wu(i32 %a) nounwind { +; RV32IFD-LABEL: fcvt_d_wu: +; RV32IFD: # %bb.0: +; RV32IFD-NEXT: addi sp, sp, -16 +; RV32IFD-NEXT: fcvt.d.wu ft0, a0 +; RV32IFD-NEXT: fsd ft0, 8(sp) +; RV32IFD-NEXT: lw a0, 8(sp) +; RV32IFD-NEXT: lw a1, 12(sp) +; RV32IFD-NEXT: addi sp, sp, 16 +; RV32IFD-NEXT: ret + %1 = uitofp i32 %a to double + ret double %1 +} diff --git a/llvm/test/CodeGen/RISCV/double-mem.ll b/llvm/test/CodeGen/RISCV/double-mem.ll index a1f79c883c43..40aae5bf3d60 100644 --- a/llvm/test/CodeGen/RISCV/double-mem.ll +++ b/llvm/test/CodeGen/RISCV/double-mem.ll @@ -170,3 +170,20 @@ define void @fsd_stack(double %a, double %b) nounwind { call void @notdead(i8* %3) ret void } + +; Test selection of store, .. +define void @fsd_trunc(float* %a, double %b) nounwind noinline optnone { +; RV32IFD-LABEL: fsd_trunc: +; RV32IFD: # %bb.0: +; RV32IFD-NEXT: addi sp, sp, -16 +; RV32IFD-NEXT: sw a1, 8(sp) +; RV32IFD-NEXT: sw a2, 12(sp) +; RV32IFD-NEXT: fld ft0, 8(sp) +; RV32IFD-NEXT: fcvt.s.d ft0, ft0 +; RV32IFD-NEXT: fsw ft0, 0(a0) +; RV32IFD-NEXT: addi sp, sp, 16 +; RV32IFD-NEXT: ret + %1 = fptrunc double %b to float + store float %1, float* %a, align 4 + ret void +}