Skip to content

Commit cdf5cfe

Browse files
committed
Revert "[SDAG] remove use restriction in isNegatibleForFree() when called from getNegatedExpression()"
This reverts commit d1f0bdf. The patch can cause infinite loops in DAGCombiner.
1 parent 34ef51b commit cdf5cfe

File tree

6 files changed

+20
-72
lines changed

6 files changed

+20
-72
lines changed

llvm/include/llvm/CodeGen/TargetLowering.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3442,16 +3442,8 @@ class TargetLowering : public TargetLoweringBase {
34423442
/// Return 1 if we can compute the negated form of the specified expression
34433443
/// for the same cost as the expression itself, or 2 if we can compute the
34443444
/// negated form more cheaply than the expression itself. Else return 0.
3445-
///
3446-
/// EnableUseCheck specifies whether the number of uses of a value affects
3447-
/// if negation is considered free. This is needed because the number of uses
3448-
/// of any value may change as we rewrite the expression. Therefore, when
3449-
/// called from getNegatedExpression(), we must explicitly set EnableUseCheck
3450-
/// to false to avoid getting a different answer than when called from other
3451-
/// contexts.
34523445
virtual char isNegatibleForFree(SDValue Op, SelectionDAG &DAG,
34533446
bool LegalOperations, bool ForCodeSize,
3454-
bool EnableUseCheck = true,
34553447
unsigned Depth = 0) const;
34563448

34573449
/// If isNegatibleForFree returns true, return the newly negated expression.

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5413,21 +5413,18 @@ verifyReturnAddressArgumentIsConstant(SDValue Op, SelectionDAG &DAG) const {
54135413

54145414
char TargetLowering::isNegatibleForFree(SDValue Op, SelectionDAG &DAG,
54155415
bool LegalOperations, bool ForCodeSize,
5416-
bool EnableUseCheck,
54175416
unsigned Depth) const {
54185417
// fneg is removable even if it has multiple uses.
54195418
if (Op.getOpcode() == ISD::FNEG)
54205419
return 2;
54215420

5422-
// If the caller requires checking uses, don't allow anything with multiple
5423-
// uses unless we know it is free.
5421+
// Don't allow anything with multiple uses unless we know it is free.
54245422
EVT VT = Op.getValueType();
54255423
const SDNodeFlags Flags = Op->getFlags();
54265424
const TargetOptions &Options = DAG.getTarget().Options;
5427-
if (EnableUseCheck)
5428-
if (!Op.hasOneUse() && !(Op.getOpcode() == ISD::FP_EXTEND &&
5429-
isFPExtFree(VT, Op.getOperand(0).getValueType())))
5430-
return 0;
5425+
if (!Op.hasOneUse() && !(Op.getOpcode() == ISD::FP_EXTEND &&
5426+
isFPExtFree(VT, Op.getOperand(0).getValueType())))
5427+
return 0;
54315428

54325429
// Don't recurse exponentially.
54335430
if (Depth > SelectionDAG::MaxRecursionDepth)
@@ -5471,11 +5468,11 @@ char TargetLowering::isNegatibleForFree(SDValue Op, SelectionDAG &DAG,
54715468

54725469
// fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
54735470
if (char V = isNegatibleForFree(Op.getOperand(0), DAG, LegalOperations,
5474-
ForCodeSize, EnableUseCheck, Depth + 1))
5471+
ForCodeSize, Depth + 1))
54755472
return V;
54765473
// fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
54775474
return isNegatibleForFree(Op.getOperand(1), DAG, LegalOperations,
5478-
ForCodeSize, EnableUseCheck, Depth + 1);
5475+
ForCodeSize, Depth + 1);
54795476
case ISD::FSUB:
54805477
// We can't turn -(A-B) into B-A when we honor signed zeros.
54815478
if (!Options.NoSignedZerosFPMath && !Flags.hasNoSignedZeros())
@@ -5488,7 +5485,7 @@ char TargetLowering::isNegatibleForFree(SDValue Op, SelectionDAG &DAG,
54885485
case ISD::FDIV:
54895486
// fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) or (fmul X, (fneg Y))
54905487
if (char V = isNegatibleForFree(Op.getOperand(0), DAG, LegalOperations,
5491-
ForCodeSize, EnableUseCheck, Depth + 1))
5488+
ForCodeSize, Depth + 1))
54925489
return V;
54935490

54945491
// Ignore X * 2.0 because that is expected to be canonicalized to X + X.
@@ -5497,7 +5494,7 @@ char TargetLowering::isNegatibleForFree(SDValue Op, SelectionDAG &DAG,
54975494
return 0;
54985495

54995496
return isNegatibleForFree(Op.getOperand(1), DAG, LegalOperations,
5500-
ForCodeSize, EnableUseCheck, Depth + 1);
5497+
ForCodeSize, Depth + 1);
55015498

55025499
case ISD::FMA:
55035500
case ISD::FMAD: {
@@ -5507,15 +5504,15 @@ char TargetLowering::isNegatibleForFree(SDValue Op, SelectionDAG &DAG,
55075504
// fold (fneg (fma X, Y, Z)) -> (fma (fneg X), Y, (fneg Z))
55085505
// fold (fneg (fma X, Y, Z)) -> (fma X, (fneg Y), (fneg Z))
55095506
char V2 = isNegatibleForFree(Op.getOperand(2), DAG, LegalOperations,
5510-
ForCodeSize, EnableUseCheck, Depth + 1);
5507+
ForCodeSize, Depth + 1);
55115508
if (!V2)
55125509
return 0;
55135510

55145511
// One of Op0/Op1 must be cheaply negatible, then select the cheapest.
55155512
char V0 = isNegatibleForFree(Op.getOperand(0), DAG, LegalOperations,
5516-
ForCodeSize, EnableUseCheck, Depth + 1);
5513+
ForCodeSize, Depth + 1);
55175514
char V1 = isNegatibleForFree(Op.getOperand(1), DAG, LegalOperations,
5518-
ForCodeSize, EnableUseCheck, Depth + 1);
5515+
ForCodeSize, Depth + 1);
55195516
char V01 = std::max(V0, V1);
55205517
return V01 ? std::max(V01, V2) : 0;
55215518
}
@@ -5524,7 +5521,7 @@ char TargetLowering::isNegatibleForFree(SDValue Op, SelectionDAG &DAG,
55245521
case ISD::FP_ROUND:
55255522
case ISD::FSIN:
55265523
return isNegatibleForFree(Op.getOperand(0), DAG, LegalOperations,
5527-
ForCodeSize, EnableUseCheck, Depth + 1);
5524+
ForCodeSize, Depth + 1);
55285525
}
55295526

55305527
return 0;
@@ -5568,7 +5565,7 @@ SDValue TargetLowering::getNegatedExpression(SDValue Op, SelectionDAG &DAG,
55685565

55695566
// fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
55705567
if (isNegatibleForFree(Op.getOperand(0), DAG, LegalOperations, ForCodeSize,
5571-
false, Depth + 1))
5568+
Depth + 1))
55725569
return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
55735570
getNegatedExpression(Op.getOperand(0), DAG,
55745571
LegalOperations, ForCodeSize,
@@ -5595,7 +5592,7 @@ SDValue TargetLowering::getNegatedExpression(SDValue Op, SelectionDAG &DAG,
55955592
case ISD::FDIV:
55965593
// fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y)
55975594
if (isNegatibleForFree(Op.getOperand(0), DAG, LegalOperations, ForCodeSize,
5598-
false, Depth + 1))
5595+
Depth + 1))
55995596
return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
56005597
getNegatedExpression(Op.getOperand(0), DAG,
56015598
LegalOperations, ForCodeSize,
@@ -5619,9 +5616,9 @@ SDValue TargetLowering::getNegatedExpression(SDValue Op, SelectionDAG &DAG,
56195616
ForCodeSize, Depth + 1);
56205617

56215618
char V0 = isNegatibleForFree(Op.getOperand(0), DAG, LegalOperations,
5622-
ForCodeSize, false, Depth + 1);
5619+
ForCodeSize, Depth + 1);
56235620
char V1 = isNegatibleForFree(Op.getOperand(1), DAG, LegalOperations,
5624-
ForCodeSize, false, Depth + 1);
5621+
ForCodeSize, Depth + 1);
56255622
if (V0 >= V1) {
56265623
// fold (fneg (fma X, Y, Z)) -> (fma (fneg X), Y, (fneg Z))
56275624
SDValue Neg0 = getNegatedExpression(

llvm/lib/Target/X86/X86ISelLowering.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41898,7 +41898,6 @@ static SDValue combineFneg(SDNode *N, SelectionDAG &DAG,
4189841898
char X86TargetLowering::isNegatibleForFree(SDValue Op, SelectionDAG &DAG,
4189941899
bool LegalOperations,
4190041900
bool ForCodeSize,
41901-
bool EnableUseCheck,
4190241901
unsigned Depth) const {
4190341902
// fneg patterns are removable even if they have multiple uses.
4190441903
if (isFNEG(DAG, Op.getNode(), Depth))
@@ -41927,7 +41926,7 @@ char X86TargetLowering::isNegatibleForFree(SDValue Op, SelectionDAG &DAG,
4192741926
// extra operand negations as well.
4192841927
for (int i = 0; i != 3; ++i) {
4192941928
char V = isNegatibleForFree(Op.getOperand(i), DAG, LegalOperations,
41930-
ForCodeSize, EnableUseCheck, Depth + 1);
41929+
ForCodeSize, Depth + 1);
4193141930
if (V == 2)
4193241931
return V;
4193341932
}
@@ -41936,8 +41935,7 @@ char X86TargetLowering::isNegatibleForFree(SDValue Op, SelectionDAG &DAG,
4193641935
}
4193741936

4193841937
return TargetLowering::isNegatibleForFree(Op, DAG, LegalOperations,
41939-
ForCodeSize, EnableUseCheck,
41940-
Depth);
41938+
ForCodeSize, Depth);
4194141939
}
4194241940

4194341941
SDValue X86TargetLowering::getNegatedExpression(SDValue Op, SelectionDAG &DAG,
@@ -41969,7 +41967,7 @@ SDValue X86TargetLowering::getNegatedExpression(SDValue Op, SelectionDAG &DAG,
4196941967
SmallVector<SDValue, 4> NewOps(Op.getNumOperands(), SDValue());
4197041968
for (int i = 0; i != 3; ++i) {
4197141969
char V = isNegatibleForFree(Op.getOperand(i), DAG, LegalOperations,
41972-
ForCodeSize, false, Depth + 1);
41970+
ForCodeSize, Depth + 1);
4197341971
if (V == 2)
4197441972
NewOps[i] = getNegatedExpression(Op.getOperand(i), DAG, LegalOperations,
4197541973
ForCodeSize, Depth + 1);

llvm/lib/Target/X86/X86ISelLowering.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -809,8 +809,7 @@ namespace llvm {
809809
/// for the same cost as the expression itself, or 2 if we can compute the
810810
/// negated form more cheaply than the expression itself. Else return 0.
811811
char isNegatibleForFree(SDValue Op, SelectionDAG &DAG, bool LegalOperations,
812-
bool ForCodeSize, bool EnableUseCheck,
813-
unsigned Depth) const override;
812+
bool ForCodeSize, unsigned Depth) const override;
814813

815814
/// If isNegatibleForFree returns true, return the newly negated expression.
816815
SDValue getNegatedExpression(SDValue Op, SelectionDAG &DAG,

llvm/test/CodeGen/AArch64/arm64-fmadd.ll

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -88,23 +88,5 @@ entry:
8888
ret double %0
8989
}
9090

91-
; This would crash while trying getNegatedExpression().
92-
93-
define float @negated_constant(float %x) {
94-
; CHECK-LABEL: negated_constant:
95-
; CHECK: // %bb.0:
96-
; CHECK-NEXT: mov w8, #-1037565952
97-
; CHECK-NEXT: mov w9, #1109917696
98-
; CHECK-NEXT: fmov s1, w8
99-
; CHECK-NEXT: fmul s1, s0, s1
100-
; CHECK-NEXT: fmov s2, w9
101-
; CHECK-NEXT: fmadd s0, s0, s2, s1
102-
; CHECK-NEXT: ret
103-
%m = fmul float %x, 42.0
104-
%fma = call nsz float @llvm.fma.f32(float %x, float -42.0, float %m)
105-
%nfma = fneg float %fma
106-
ret float %nfma
107-
}
108-
10991
declare float @llvm.fma.f32(float, float, float) nounwind readnone
11092
declare double @llvm.fma.f64(double, double, double) nounwind readnone

llvm/test/CodeGen/X86/fma-fneg-combine-2.ll

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,24 +86,4 @@ entry:
8686
ret float %1
8787
}
8888

89-
; This would crash while trying getNegatedExpression().
90-
91-
define float @negated_constant(float %x) {
92-
; FMA3-LABEL: negated_constant:
93-
; FMA3: # %bb.0:
94-
; FMA3-NEXT: vmulss {{.*}}(%rip), %xmm0, %xmm1
95-
; FMA3-NEXT: vfmadd132ss {{.*#+}} xmm0 = (xmm0 * mem) + xmm1
96-
; FMA3-NEXT: retq
97-
;
98-
; FMA4-LABEL: negated_constant:
99-
; FMA4: # %bb.0:
100-
; FMA4-NEXT: vmulss {{.*}}(%rip), %xmm0, %xmm1
101-
; FMA4-NEXT: vfmaddss %xmm1, {{.*}}(%rip), %xmm0, %xmm0
102-
; FMA4-NEXT: retq
103-
%m = fmul float %x, 42.0
104-
%fma = call nsz float @llvm.fma.f32(float %x, float -42.0, float %m)
105-
%nfma = fneg float %fma
106-
ret float %nfma
107-
}
108-
10989
declare float @llvm.fma.f32(float, float, float)

0 commit comments

Comments
 (0)