Skip to content

Commit d760910

Browse files
committed
[RISCV] Add another potential combine to {double,float}-bitmanip-dagcombines.ll
(fcopysign a, (fneg b)) will be expanded to bitwise operations by DAGTypeLegalizer::SoftenFloatRes_FCOPYSIGN if the floating point type isn't legal. Arguably it might be worth doing a combine even if it is legal. llvm-svn: 352240
1 parent 8bed74b commit d760910

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

llvm/test/CodeGen/RISCV/double-bitmanip-dagcombines.ll

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,51 @@ define double @fabs(double %a) nounwind {
7878
%1 = call double @llvm.fabs.f64(double %a)
7979
ret double %1
8080
}
81+
82+
declare double @llvm.copysign.f64(double, double)
83+
84+
; DAGTypeLegalizer::SoftenFloatRes_FCOPYSIGN will convert to bitwise
85+
; operations if floating point isn't supported. A combine could be written to
86+
; do the same even when f64 is legal.
87+
88+
define double @fcopysign_fneg(double %a, double %b) nounwind {
89+
; RV32I-LABEL: fcopysign_fneg:
90+
; RV32I: # %bb.0:
91+
; RV32I-NEXT: not a2, a3
92+
; RV32I-NEXT: lui a3, 524288
93+
; RV32I-NEXT: and a2, a2, a3
94+
; RV32I-NEXT: addi a3, a3, -1
95+
; RV32I-NEXT: and a1, a1, a3
96+
; RV32I-NEXT: or a1, a1, a2
97+
; RV32I-NEXT: ret
98+
;
99+
; RV32IFD-LABEL: fcopysign_fneg:
100+
; RV32IFD: # %bb.0:
101+
; RV32IFD-NEXT: addi sp, sp, -16
102+
; RV32IFD-NEXT: sw a2, 8(sp)
103+
; RV32IFD-NEXT: sw a3, 12(sp)
104+
; RV32IFD-NEXT: fld ft0, 8(sp)
105+
; RV32IFD-NEXT: sw a0, 8(sp)
106+
; RV32IFD-NEXT: sw a1, 12(sp)
107+
; RV32IFD-NEXT: fld ft1, 8(sp)
108+
; RV32IFD-NEXT: fsgnjn.d ft0, ft1, ft0
109+
; RV32IFD-NEXT: fsd ft0, 8(sp)
110+
; RV32IFD-NEXT: lw a0, 8(sp)
111+
; RV32IFD-NEXT: lw a1, 12(sp)
112+
; RV32IFD-NEXT: addi sp, sp, 16
113+
; RV32IFD-NEXT: ret
114+
;
115+
; RV64I-LABEL: fcopysign_fneg:
116+
; RV64I: # %bb.0:
117+
; RV64I-NEXT: addi a2, zero, -1
118+
; RV64I-NEXT: slli a2, a2, 63
119+
; RV64I-NEXT: not a1, a1
120+
; RV64I-NEXT: and a1, a1, a2
121+
; RV64I-NEXT: addi a2, a2, -1
122+
; RV64I-NEXT: and a0, a0, a2
123+
; RV64I-NEXT: or a0, a0, a1
124+
; RV64I-NEXT: ret
125+
%1 = fneg double %b
126+
%2 = call double @llvm.copysign.f64(double %a, double %1)
127+
ret double %2
128+
}

llvm/test/CodeGen/RISCV/float-bitmanip-dagcombines.ll

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
; RUN: llc -mtriple=riscv32 -verify-machineinstrs < %s \
33
; RUN: | FileCheck -check-prefix=RV32I %s
44
; RUN: llc -mtriple=riscv32 -mattr=+f -verify-machineinstrs < %s \
5-
; RUN: | FileCheck -check-prefix=RV32I %s
5+
; RUN: | FileCheck -check-prefix=RV32IF %s
66
; RUN: llc -mtriple=riscv64 -verify-machineinstrs < %s \
77
; RUN: | FileCheck -check-prefix=RV64I %s
88

@@ -19,6 +19,12 @@ define float @fneg(float %a) nounwind {
1919
; RV32I-NEXT: xor a0, a0, a1
2020
; RV32I-NEXT: ret
2121
;
22+
; RV32IF-LABEL: fneg:
23+
; RV32IF: # %bb.0:
24+
; RV32IF-NEXT: lui a1, 524288
25+
; RV32IF-NEXT: xor a0, a0, a1
26+
; RV32IF-NEXT: ret
27+
;
2228
; RV64I-LABEL: fneg:
2329
; RV64I: # %bb.0:
2430
; RV64I-NEXT: lui a1, 524288
@@ -38,6 +44,13 @@ define float @fabs(float %a) nounwind {
3844
; RV32I-NEXT: and a0, a0, a1
3945
; RV32I-NEXT: ret
4046
;
47+
; RV32IF-LABEL: fabs:
48+
; RV32IF: # %bb.0:
49+
; RV32IF-NEXT: lui a1, 524288
50+
; RV32IF-NEXT: addi a1, a1, -1
51+
; RV32IF-NEXT: and a0, a0, a1
52+
; RV32IF-NEXT: ret
53+
;
4154
; RV64I-LABEL: fabs:
4255
; RV64I: # %bb.0:
4356
; RV64I-NEXT: lui a1, 524288
@@ -47,3 +60,44 @@ define float @fabs(float %a) nounwind {
4760
%1 = call float @llvm.fabs.f32(float %a)
4861
ret float %1
4962
}
63+
64+
declare float @llvm.copysign.f32(float, float)
65+
66+
; DAGTypeLegalizer::SoftenFloatRes_FCOPYSIGN will convert to bitwise
67+
; operations if floating point isn't supported. A combine could be written to
68+
; do the same even when f32 is legal.
69+
70+
define float @fcopysign_fneg(float %a, float %b) nounwind {
71+
; RV32I-LABEL: fcopysign_fneg:
72+
; RV32I: # %bb.0:
73+
; RV32I-NEXT: not a1, a1
74+
; RV32I-NEXT: lui a2, 524288
75+
; RV32I-NEXT: and a1, a1, a2
76+
; RV32I-NEXT: addi a2, a2, -1
77+
; RV32I-NEXT: and a0, a0, a2
78+
; RV32I-NEXT: or a0, a0, a1
79+
; RV32I-NEXT: ret
80+
;
81+
; RV32IF-LABEL: fcopysign_fneg:
82+
; RV32IF: # %bb.0:
83+
; RV32IF-NEXT: lui a2, 524288
84+
; RV32IF-NEXT: xor a1, a1, a2
85+
; RV32IF-NEXT: fmv.w.x ft0, a1
86+
; RV32IF-NEXT: fmv.w.x ft1, a0
87+
; RV32IF-NEXT: fsgnj.s ft0, ft1, ft0
88+
; RV32IF-NEXT: fmv.x.w a0, ft0
89+
; RV32IF-NEXT: ret
90+
;
91+
; RV64I-LABEL: fcopysign_fneg:
92+
; RV64I: # %bb.0:
93+
; RV64I-NEXT: not a1, a1
94+
; RV64I-NEXT: lui a2, 524288
95+
; RV64I-NEXT: and a1, a1, a2
96+
; RV64I-NEXT: addiw a2, a2, -1
97+
; RV64I-NEXT: and a0, a0, a2
98+
; RV64I-NEXT: or a0, a0, a1
99+
; RV64I-NEXT: ret
100+
%1 = fneg float %b
101+
%2 = call float @llvm.copysign.f32(float %a, float %1)
102+
ret float %2
103+
}

0 commit comments

Comments
 (0)