Skip to content

Commit 121baba

Browse files
ecneliseszmodem
authored andcommitted
[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. (cherry picked from commit b326d4f)
1 parent f80e6d6 commit 121baba

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
@@ -5751,10 +5751,8 @@ SDValue TargetLowering::getNegatedExpression(SDValue Op, SelectionDAG &DAG,
57515751

57525752
// If we already have the use of the negated floating constant, it is free
57535753
// to negate it even it has multiple uses.
5754-
if (!Op.hasOneUse() && CFP.use_empty()) {
5755-
RemoveDeadNode(CFP);
5754+
if (!Op.hasOneUse() && CFP.use_empty())
57565755
break;
5757-
}
57585756
Cost = NegatibleCost::Neutral;
57595757
return CFP;
57605758
}

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)