Skip to content

Commit 3eaf500

Browse files
committedSep 16, 2018
[DAGCombiner] try to convert pow(x, 1/3) to cbrt(x)
This is a follow-up suggested in D51630 and originally proposed as an IR transform in D49040. Copying the motivational statement by @evandro from that patch: "This transformation helps some benchmarks in SPEC CPU2000 and CPU2006, such as 188.ammp, 447.dealII, 453.povray, and especially 300.twolf, as well as some proprietary benchmarks. Otherwise, no regressions on x86-64 or A64." I'm proposing to add only the minimum support for a DAG node here. Since we don't have an LLVM IR intrinsic for cbrt, and there are no other DAG ways to create a FCBRT node yet, I don't think we need to worry about DAG builder, legalization, a strict variant, etc. We should be able to expand as needed when adding more functionality/transforms. For reference, these are transform suggestions currently listed in SimplifyLibCalls.cpp: // * cbrt(expN(X)) -> expN(x/3) // * cbrt(sqrt(x)) -> pow(x,1/6) // * cbrt(cbrt(x)) -> pow(x,1/9) Also, given that we bail out on long double for now, there should not be any logical differences between platforms (unless there's some platform out there that has pow() but not cbrt()). Differential Revision: https://reviews.llvm.org/D51753 llvm-svn: 342348
1 parent bfee5a9 commit 3eaf500

File tree

7 files changed

+88
-10
lines changed

7 files changed

+88
-10
lines changed
 

‎llvm/include/llvm/CodeGen/ISDOpcodes.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -550,11 +550,8 @@ namespace ISD {
550550
/// is often a storage-only type but has native conversions.
551551
FP16_TO_FP, FP_TO_FP16,
552552

553-
/// FNEG, FABS, FSQRT, FSIN, FCOS, FPOWI, FPOW,
554-
/// FLOG, FLOG2, FLOG10, FEXP, FEXP2,
555-
/// FCEIL, FTRUNC, FRINT, FNEARBYINT, FROUND, FFLOOR - Perform various unary
556-
/// floating point operations. These are inspired by libm.
557-
FNEG, FABS, FSQRT, FSIN, FCOS, FPOWI, FPOW,
553+
/// Perform various unary floating-point operations inspired by libm.
554+
FNEG, FABS, FSQRT, FCBRT, FSIN, FCOS, FPOWI, FPOW,
558555
FLOG, FLOG2, FLOG10, FEXP, FEXP2,
559556
FCEIL, FTRUNC, FRINT, FNEARBYINT, FROUND, FFLOOR,
560557
/// FMINNUM/FMAXNUM - Perform floating-point minimum or maximum on two

‎llvm/include/llvm/IR/RuntimeLibcalls.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ HANDLE_LIBCALL(SQRT_F64, "sqrt")
128128
HANDLE_LIBCALL(SQRT_F80, "sqrtl")
129129
HANDLE_LIBCALL(SQRT_F128, "sqrtl")
130130
HANDLE_LIBCALL(SQRT_PPCF128, "sqrtl")
131+
HANDLE_LIBCALL(CBRT_F32, "cbrtf")
132+
HANDLE_LIBCALL(CBRT_F64, "cbrt")
133+
HANDLE_LIBCALL(CBRT_F80, "cbrtl")
134+
HANDLE_LIBCALL(CBRT_F128, "cbrtl")
135+
HANDLE_LIBCALL(CBRT_PPCF128, "cbrtl")
131136
HANDLE_LIBCALL(LOG_F32, "logf")
132137
HANDLE_LIBCALL(LOG_F64, "log")
133138
HANDLE_LIBCALL(LOG_F80, "logl")

‎llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11571,6 +11571,34 @@ SDValue DAGCombiner::visitFPOW(SDNode *N) {
1157111571
if (!ExponentC)
1157211572
return SDValue();
1157311573

11574+
// Try to convert x ** (1/3) into cube root.
11575+
// TODO: Handle the various flavors of long double.
11576+
// TODO: Since we're approximating, we don't need an exact 1/3 exponent.
11577+
// Some range near 1/3 should be fine.
11578+
EVT VT = N->getValueType(0);
11579+
if ((VT == MVT::f32 && ExponentC->getValueAPF().isExactlyValue(1.0f/3.0f)) ||
11580+
(VT == MVT::f64 && ExponentC->getValueAPF().isExactlyValue(1.0/3.0))) {
11581+
// pow(-0.0, 1/3) = +0.0; cbrt(-0.0) = -0.0.
11582+
// pow(-inf, 1/3) = +inf; cbrt(-inf) = -inf.
11583+
// pow(-val, 1/3) = nan; cbrt(-val) = -num.
11584+
// For regular numbers, rounding may cause the results to differ.
11585+
// Therefore, we require { nsz ninf nnan afn } for this transform.
11586+
// TODO: We could select out the special cases if we don't have nsz/ninf.
11587+
SDNodeFlags Flags = N->getFlags();
11588+
if (!Flags.hasNoSignedZeros() || !Flags.hasNoInfs() || !Flags.hasNoNaNs() ||
11589+
!Flags.hasApproximateFuncs())
11590+
return SDValue();
11591+
11592+
// Do not create a cbrt() libcall if the target does not have it, and do not
11593+
// turn a pow that has lowering support into a cbrt() libcall.
11594+
if (!DAG.getLibInfo().has(LibFunc_cbrt) ||
11595+
(!DAG.getTargetLoweringInfo().isOperationExpand(ISD::FPOW, VT) &&
11596+
DAG.getTargetLoweringInfo().isOperationExpand(ISD::FCBRT, VT)))
11597+
return SDValue();
11598+
11599+
return DAG.getNode(ISD::FCBRT, SDLoc(N), VT, N->getOperand(0), Flags);
11600+
}
11601+
1157411602
// Try to convert x ** (1/4) into square roots.
1157511603
// x ** (1/2) is canonicalized to sqrt, so we do not bother with that case.
1157611604
// TODO: This could be extended (using a target hook) to handle smaller
@@ -11587,7 +11615,6 @@ SDValue DAGCombiner::visitFPOW(SDNode *N) {
1158711615
return SDValue();
1158811616

1158911617
// Don't double the number of libcalls. We are trying to inline fast code.
11590-
EVT VT = N->getValueType(0);
1159111618
if (!DAG.getTargetLoweringInfo().isOperationLegalOrCustom(ISD::FSQRT, VT))
1159211619
return SDValue();
1159311620

‎llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4047,6 +4047,11 @@ void SelectionDAGLegalize::ConvertNodeToLibcall(SDNode *Node) {
40474047
RTLIB::SQRT_F80, RTLIB::SQRT_F128,
40484048
RTLIB::SQRT_PPCF128));
40494049
break;
4050+
case ISD::FCBRT:
4051+
Results.push_back(ExpandFPLibCall(Node, RTLIB::CBRT_F32, RTLIB::CBRT_F64,
4052+
RTLIB::CBRT_F80, RTLIB::CBRT_F128,
4053+
RTLIB::CBRT_PPCF128));
4054+
break;
40504055
case ISD::FSIN:
40514056
case ISD::STRICT_FSIN:
40524057
Results.push_back(ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,

‎llvm/lib/CodeGen/SelectionDAG/SelectionDAGDumper.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ std::string SDNode::getOperationName(const SelectionDAG *G) const {
181181
case ISD::FNEG: return "fneg";
182182
case ISD::FSQRT: return "fsqrt";
183183
case ISD::STRICT_FSQRT: return "strict_fsqrt";
184+
case ISD::FCBRT: return "fcbrt";
184185
case ISD::FSIN: return "fsin";
185186
case ISD::STRICT_FSIN: return "strict_fsin";
186187
case ISD::FCOS: return "fcos";

‎llvm/lib/CodeGen/TargetLoweringBase.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,7 @@ void TargetLoweringBase::initActions() {
666666

667667
// These library functions default to expand.
668668
for (MVT VT : {MVT::f32, MVT::f64, MVT::f128}) {
669+
setOperationAction(ISD::FCBRT, VT, Expand);
669670
setOperationAction(ISD::FLOG , VT, Expand);
670671
setOperationAction(ISD::FLOG2, VT, Expand);
671672
setOperationAction(ISD::FLOG10, VT, Expand);

‎llvm/test/CodeGen/X86/pow.ll

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ declare <4 x float> @llvm.pow.v4f32(<4 x float>, <4 x float>)
77
declare double @llvm.pow.f64(double, double)
88
declare <2 x double> @llvm.pow.v2f64(<2 x double>, <2 x double>)
99

10+
declare x86_fp80 @llvm.pow.f80(x86_fp80, x86_fp80)
11+
1012
define float @pow_f32_one_fourth_fmf(float %x) nounwind {
1113
; CHECK-LABEL: pow_f32_one_fourth_fmf:
1214
; CHECK: # %bb.0:
@@ -165,8 +167,7 @@ define <2 x double> @pow_v2f64_one_fourth_not_enough_fmf(<2 x double> %x) nounwi
165167
define float @pow_f32_one_third_fmf(float %x) nounwind {
166168
; CHECK-LABEL: pow_f32_one_third_fmf:
167169
; CHECK: # %bb.0:
168-
; CHECK-NEXT: movss {{.*#+}} xmm1 = mem[0],zero,zero,zero
169-
; CHECK-NEXT: jmp powf # TAILCALL
170+
; CHECK-NEXT: jmp cbrtf # TAILCALL
170171
%one = uitofp i32 1 to float
171172
%three = uitofp i32 3 to float
172173
%exp = fdiv float %one, %three
@@ -177,12 +178,53 @@ define float @pow_f32_one_third_fmf(float %x) nounwind {
177178
define double @pow_f64_one_third_fmf(double %x) nounwind {
178179
; CHECK-LABEL: pow_f64_one_third_fmf:
179180
; CHECK: # %bb.0:
180-
; CHECK-NEXT: movsd {{.*#+}} xmm1 = mem[0],zero
181-
; CHECK-NEXT: jmp pow # TAILCALL
181+
; CHECK-NEXT: jmp cbrt # TAILCALL
182182
%one = uitofp i32 1 to double
183183
%three = uitofp i32 3 to double
184184
%exp = fdiv double %one, %three
185185
%r = call nsz nnan ninf afn double @llvm.pow.f64(double %x, double %exp)
186186
ret double %r
187187
}
188188

189+
; TODO: We could turn this into cbrtl, but currently we only handle float/double types.
190+
191+
define x86_fp80 @pow_f80_one_third_fmf(x86_fp80 %x) nounwind {
192+
; CHECK-LABEL: pow_f80_one_third_fmf:
193+
; CHECK: # %bb.0:
194+
; CHECK-NEXT: subq $40, %rsp
195+
; CHECK-NEXT: fldt {{[0-9]+}}(%rsp)
196+
; CHECK-NEXT: fldt {{.*}}(%rip)
197+
; CHECK-NEXT: fstpt {{[0-9]+}}(%rsp)
198+
; CHECK-NEXT: fstpt (%rsp)
199+
; CHECK-NEXT: callq powl
200+
; CHECK-NEXT: addq $40, %rsp
201+
; CHECK-NEXT: retq
202+
%one = uitofp i32 1 to x86_fp80
203+
%three = uitofp i32 3 to x86_fp80
204+
%exp = fdiv x86_fp80 %one, %three
205+
%r = call nsz nnan ninf afn x86_fp80 @llvm.pow.f80(x86_fp80 %x, x86_fp80 %exp)
206+
ret x86_fp80 %r
207+
}
208+
209+
; We might want to allow this. The exact hex value for 1/3 as a double is 0x3fd5555555555555.
210+
211+
define double @pow_f64_not_exactly_one_third_fmf(double %x) nounwind {
212+
; CHECK-LABEL: pow_f64_not_exactly_one_third_fmf:
213+
; CHECK: # %bb.0:
214+
; CHECK-NEXT: movsd {{.*#+}} xmm1 = mem[0],zero
215+
; CHECK-NEXT: jmp pow # TAILCALL
216+
%r = call nsz nnan ninf afn double @llvm.pow.f64(double %x, double 0x3fd5555555555556)
217+
ret double %r
218+
}
219+
220+
; We require all 4 of nsz, ninf, nnan, afn.
221+
222+
define double @pow_f64_not_enough_fmf(double %x) nounwind {
223+
; CHECK-LABEL: pow_f64_not_enough_fmf:
224+
; CHECK: # %bb.0:
225+
; CHECK-NEXT: movsd {{.*#+}} xmm1 = mem[0],zero
226+
; CHECK-NEXT: jmp pow # TAILCALL
227+
%r = call nsz ninf afn double @llvm.pow.f64(double %x, double 0x3fd5555555555555)
228+
ret double %r
229+
}
230+

0 commit comments

Comments
 (0)
Please sign in to comment.