Skip to content

Commit b326d4f

Browse files
committed
[SelectionDAG] Don't remove unused negated constant immediately
This reverts partial of a2fb544 (actually, 2508ef0) about removing negated FP constant immediately if it has no uses. However, as discussed in bug 47517, there're cases when NegX is folded into constant from other places while NegY is removed by that line of code and NegX is equal to NegY. In these cases, NegX is deleted before used and crash happens. So revert the code and add necessary test case.
1 parent 4ce6114 commit b326d4f

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5773,10 +5773,8 @@ SDValue TargetLowering::getNegatedExpression(SDValue Op, SelectionDAG &DAG,
57735773

57745774
// If we already have the use of the negated floating constant, it is free
57755775
// to negate it even it has multiple uses.
5776-
if (!Op.hasOneUse() && CFP.use_empty()) {
5777-
RemoveDeadNode(CFP);
5776+
if (!Op.hasOneUse() && CFP.use_empty())
57785777
break;
5779-
}
57805778
Cost = NegatibleCost::Neutral;
57815779
return CFP;
57825780
}

llvm/test/CodeGen/X86/pr47517.ll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,16 @@ entry:
2626
%fmul6 = fmul fast float %fmul3, %fadd4
2727
ret float %fmul6
2828
}
29+
30+
; To ensure negated result will not be removed when NegX=NegY and
31+
; NegX is needed
32+
define float @test2(float %x, float %y) {
33+
%add = fadd fast float %x, 750.0
34+
%sub = fsub fast float %x, %add
35+
%mul = fmul fast float %sub, %sub
36+
%mul2 = fmul fast float %mul, %sub
37+
%add2 = fadd fast float %mul2, 1.0
38+
%add3 = fadd fast float %mul2, %add2
39+
%mul3 = fmul fast float %y, %add3
40+
ret float %mul3
41+
}

0 commit comments

Comments
 (0)