Skip to content

Commit 62e91bf

Browse files
venkataramanankumarrotateright
authored andcommitted
[DAGCombine]: Fold X/Sqrt(X) to Sqrt(X)
With FMF ( "nsz" and " reassoc") fold X/Sqrt(X) to Sqrt(X). This is done after targets have the chance to produce a reciprocal sqrt estimate sequence because that expansion is probably more efficient than an expansion of a non-reciprocal sqrt. That is also why we deferred doing this transform in IR (D85709). Differential Revision: https://reviews.llvm.org/D86403
1 parent a74dc59 commit 62e91bf

File tree

3 files changed

+20
-23
lines changed

3 files changed

+20
-23
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13356,6 +13356,12 @@ SDValue DAGCombiner::visitFDIV(SDNode *N) {
1335613356
return RV;
1335713357
}
1335813358

13359+
// Fold X/Sqrt(X) -> Sqrt(X)
13360+
if ((Options.NoSignedZerosFPMath || Flags.hasNoSignedZeros()) &&
13361+
(Options.UnsafeFPMath || Flags.hasAllowReassociation()))
13362+
if (N1.getOpcode() == ISD::FSQRT && N0 == N1.getOperand(0))
13363+
return N1;
13364+
1335913365
// (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y)
1336013366
TargetLowering::NegatibleCost CostN0 =
1336113367
TargetLowering::NegatibleCost::Expensive;

llvm/test/CodeGen/AArch64/sqrt-fastmath.ll

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,7 @@ define <4 x double> @d4rsqrt(<4 x double> %a) #0 {
448448
define double @sqrt_fdiv_common_operand(double %x) nounwind {
449449
; FAULT-LABEL: sqrt_fdiv_common_operand:
450450
; FAULT: // %bb.0:
451-
; FAULT-NEXT: fsqrt d1, d0
452-
; FAULT-NEXT: fdiv d0, d0, d1
451+
; FAULT-NEXT: fsqrt d0, d0
453452
; FAULT-NEXT: ret
454453
;
455454
; CHECK-LABEL: sqrt_fdiv_common_operand:
@@ -474,8 +473,7 @@ define double @sqrt_fdiv_common_operand(double %x) nounwind {
474473
define <2 x double> @sqrt_fdiv_common_operand_vec(<2 x double> %x) nounwind {
475474
; FAULT-LABEL: sqrt_fdiv_common_operand_vec:
476475
; FAULT: // %bb.0:
477-
; FAULT-NEXT: fsqrt v1.2d, v0.2d
478-
; FAULT-NEXT: fdiv v0.2d, v0.2d, v1.2d
476+
; FAULT-NEXT: fsqrt v0.2d, v0.2d
479477
; FAULT-NEXT: ret
480478
;
481479
; CHECK-LABEL: sqrt_fdiv_common_operand_vec:
@@ -493,16 +491,15 @@ define <2 x double> @sqrt_fdiv_common_operand_vec(<2 x double> %x) nounwind {
493491
; CHECK-NEXT: fmul v0.2d, v0.2d, v1.2d
494492
; CHECK-NEXT: ret
495493
%sqrt = call <2 x double> @llvm.sqrt.v2f64(<2 x double> %x)
496-
%r = fdiv nsz arcp reassoc <2 x double> %x, %sqrt
494+
%r = fdiv arcp nsz reassoc <2 x double> %x, %sqrt
497495
ret <2 x double> %r
498496
}
499497

500498
define double @sqrt_fdiv_common_operand_extra_use(double %x, double* %p) nounwind {
501499
; FAULT-LABEL: sqrt_fdiv_common_operand_extra_use:
502500
; FAULT: // %bb.0:
503-
; FAULT-NEXT: fsqrt d1, d0
504-
; FAULT-NEXT: fdiv d0, d0, d1
505-
; FAULT-NEXT: str d1, [x0]
501+
; FAULT-NEXT: fsqrt d0, d0
502+
; FAULT-NEXT: str d0, [x0]
506503
; FAULT-NEXT: ret
507504
;
508505
; CHECK-LABEL: sqrt_fdiv_common_operand_extra_use:

llvm/test/CodeGen/X86/sqrt-fastmath.ll

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -903,14 +903,12 @@ define <4 x float> @div_sqrt_v4f32(<4 x float> %x, <4 x float> %y) {
903903
define double @sqrt_fdiv_common_operand(double %x) nounwind {
904904
; SSE-LABEL: sqrt_fdiv_common_operand:
905905
; SSE: # %bb.0:
906-
; SSE-NEXT: sqrtsd %xmm0, %xmm1
907-
; SSE-NEXT: divsd %xmm1, %xmm0
906+
; SSE-NEXT: sqrtsd %xmm0, %xmm0
908907
; SSE-NEXT: retq
909908
;
910909
; AVX-LABEL: sqrt_fdiv_common_operand:
911910
; AVX: # %bb.0:
912-
; AVX-NEXT: vsqrtsd %xmm0, %xmm0, %xmm1
913-
; AVX-NEXT: vdivsd %xmm1, %xmm0, %xmm0
911+
; AVX-NEXT: vsqrtsd %xmm0, %xmm0, %xmm0
914912
; AVX-NEXT: retq
915913
%sqrt = call fast double @llvm.sqrt.f64(double %x)
916914
%r = fdiv fast double %x, %sqrt
@@ -920,33 +918,29 @@ define double @sqrt_fdiv_common_operand(double %x) nounwind {
920918
define <2 x double> @sqrt_fdiv_common_operand_vec(<2 x double> %x) nounwind {
921919
; SSE-LABEL: sqrt_fdiv_common_operand_vec:
922920
; SSE: # %bb.0:
923-
; SSE-NEXT: sqrtpd %xmm0, %xmm1
924-
; SSE-NEXT: divpd %xmm1, %xmm0
921+
; SSE-NEXT: sqrtpd %xmm0, %xmm0
925922
; SSE-NEXT: retq
926923
;
927924
; AVX-LABEL: sqrt_fdiv_common_operand_vec:
928925
; AVX: # %bb.0:
929-
; AVX-NEXT: vsqrtpd %xmm0, %xmm1
930-
; AVX-NEXT: vdivpd %xmm1, %xmm0, %xmm0
926+
; AVX-NEXT: vsqrtpd %xmm0, %xmm0
931927
; AVX-NEXT: retq
932928
%sqrt = call <2 x double> @llvm.sqrt.v2f64(<2 x double> %x)
933-
%r = fdiv nsz arcp reassoc <2 x double> %x, %sqrt
929+
%r = fdiv arcp nsz reassoc <2 x double> %x, %sqrt
934930
ret <2 x double> %r
935931
}
936932

937933
define double @sqrt_fdiv_common_operand_extra_use(double %x, double* %p) nounwind {
938934
; SSE-LABEL: sqrt_fdiv_common_operand_extra_use:
939935
; SSE: # %bb.0:
940-
; SSE-NEXT: sqrtsd %xmm0, %xmm1
941-
; SSE-NEXT: movsd %xmm1, (%rdi)
942-
; SSE-NEXT: divsd %xmm1, %xmm0
936+
; SSE-NEXT: sqrtsd %xmm0, %xmm0
937+
; SSE-NEXT: movsd %xmm0, (%rdi)
943938
; SSE-NEXT: retq
944939
;
945940
; AVX-LABEL: sqrt_fdiv_common_operand_extra_use:
946941
; AVX: # %bb.0:
947-
; AVX-NEXT: vsqrtsd %xmm0, %xmm0, %xmm1
948-
; AVX-NEXT: vmovsd %xmm1, (%rdi)
949-
; AVX-NEXT: vdivsd %xmm1, %xmm0, %xmm0
942+
; AVX-NEXT: vsqrtsd %xmm0, %xmm0, %xmm0
943+
; AVX-NEXT: vmovsd %xmm0, (%rdi)
950944
; AVX-NEXT: retq
951945
%sqrt = call fast double @llvm.sqrt.f64(double %x)
952946
store double %sqrt, double* %p

0 commit comments

Comments
 (0)