[SDAG] fold x * 0.0 at node creation time

In the motivating case from https://llvm.org/PR47517
we create a node that does not get constant folded
before getNegatedExpression is attempted from some
other node, and we crash.

By moving the fold into SelectionDAG::simplifyFPBinop(),
we get the constant fold sooner and avoid the problem.
This commit is contained in:
Sanjay Patel 2020-10-04 11:07:27 -04:00
parent 2c48dd7c3a
commit 2ccbf3dbd5
4 changed files with 26 additions and 20 deletions

View File

@ -13040,13 +13040,6 @@ SDValue DAGCombiner::visitFMUL(SDNode *N) {
if (SDValue NewSel = foldBinOpIntoSelect(N))
return NewSel;
if ((Options.NoNaNsFPMath && Options.NoSignedZerosFPMath) ||
(Flags.hasNoNaNs() && Flags.hasNoSignedZeros())) {
// fold (fmul A, 0) -> 0
if (N1CFP && N1CFP->isZero())
return N1;
}
if (Options.UnsafeFPMath || Flags.hasAllowReassociation()) {
// fmul (fmul X, C1), C2 -> fmul X, C1 * C2
if (isConstantFPBuildVectorOrConstantFP(N1) &&

View File

@ -7475,6 +7475,11 @@ SDValue SelectionDAG::simplifyFPBinop(unsigned Opcode, SDValue X, SDValue Y,
if (YC->getValueAPF().isExactlyValue(1.0))
return X;
// X * 0.0 --> 0.0
if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
if (YC->getValueAPF().isZero())
return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
return SDValue();
}

View File

@ -9,24 +9,13 @@ target triple = "thumbv7em-arm-none-eabi"
define hidden void @fn1() nounwind #0 {
; CHECK-LABEL: fn1:
; CHECK: @ %bb.0: @ %entry
; CHECK-NEXT: .save {r7, lr}
; CHECK-NEXT: push {r7, lr}
; CHECK-NEXT: vldr d0, .LCPI0_0
; CHECK-NEXT: vmov r0, r1, d0
; CHECK-NEXT: mov r2, r0
; CHECK-NEXT: mov r3, r1
; CHECK-NEXT: bl __aeabi_dcmpeq
; CHECK-NEXT: movs r0, #1
; CHECK-NEXT: cbnz r0, .LBB0_2
; CHECK-NEXT: b .LBB0_1
; CHECK-NEXT: .LBB0_1: @ %land.rhs
; CHECK-NEXT: b .LBB0_2
; CHECK-NEXT: .LBB0_2: @ %land.end
; CHECK-NEXT: pop {r7, pc}
; CHECK-NEXT: .p2align 3
; CHECK-NEXT: @ %bb.3:
; CHECK-NEXT: .LCPI0_0:
; CHECK-NEXT: .long 0 @ double 0
; CHECK-NEXT: .long 0
; CHECK-NEXT: bx lr
entry:
%0 = load i32, i32* @a, align 4
%conv = sitofp i32 %0 to double

View File

@ -261,3 +261,22 @@ define <4 x float> @fmul_fneg_fneg_v4f32(<4 x float> %x, <4 x float> %y) {
%mul = fmul <4 x float> %x.neg, %y.neg
ret <4 x float> %mul
}
; PR47517 - this could crash if we create 'fmul x, 0.0' nodes
; that do not constant fold in a particular order.
define float @getNegatedExpression_crash(float* %p) {
; CHECK-LABEL: getNegatedExpression_crash:
; CHECK: # %bb.0:
; CHECK-NEXT: movl $0, (%rdi)
; CHECK-NEXT: xorps %xmm0, %xmm0
; CHECK-NEXT: retq
store float 0.0, float* %p, align 1
%real = load float, float* %p, align 1
%r2 = fmul fast float %real, %real
%t1 = fmul fast float %real, 42.0
%t2 = fmul fast float %real, %t1
%mul_ac56 = fmul fast float %t2, %t1
%mul_ac72 = fmul fast float %r2, %mul_ac56
ret float %mul_ac72
}