[llvm] b326d4f - [SelectionDAG] Don't remove unused negated constant immediately

Qiu Chaofan via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 5 10:16:58 PDT 2020


Author: Qiu Chaofan
Date: 2020-10-06T01:16:45+08:00
New Revision: b326d4ff946d2061a566a3fcce9f33b484759fe0

URL: https://github.com/llvm/llvm-project/commit/b326d4ff946d2061a566a3fcce9f33b484759fe0
DIFF: https://github.com/llvm/llvm-project/commit/b326d4ff946d2061a566a3fcce9f33b484759fe0.diff

LOG: [SelectionDAG] Don't remove unused negated constant immediately

This reverts partial of a2fb5446 (actually, 2508ef01) 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.

Added: 
    

Modified: 
    llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
    llvm/test/CodeGen/X86/pr47517.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
index cd49d5bfd98b9..80d92dbe886d9 100644
--- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
@@ -5773,10 +5773,8 @@ SDValue TargetLowering::getNegatedExpression(SDValue Op, SelectionDAG &DAG,
 
     // If we already have the use of the negated floating constant, it is free
     // to negate it even it has multiple uses.
-    if (!Op.hasOneUse() && CFP.use_empty()) {
-      RemoveDeadNode(CFP);
+    if (!Op.hasOneUse() && CFP.use_empty())
       break;
-    }
     Cost = NegatibleCost::Neutral;
     return CFP;
   }

diff  --git a/llvm/test/CodeGen/X86/pr47517.ll b/llvm/test/CodeGen/X86/pr47517.ll
index 5672fbc69a41d..afc27b49ab2a4 100644
--- a/llvm/test/CodeGen/X86/pr47517.ll
+++ b/llvm/test/CodeGen/X86/pr47517.ll
@@ -26,3 +26,16 @@ entry:
   %fmul6 = fmul fast float %fmul3, %fadd4
   ret float %fmul6
 }
+
+; To ensure negated result will not be removed when NegX=NegY and
+; NegX is needed
+define float @test2(float %x, float %y) {
+  %add = fadd fast float %x, 750.0
+  %sub = fsub fast float %x, %add
+  %mul = fmul fast float %sub, %sub
+  %mul2 = fmul fast float %mul, %sub
+  %add2 = fadd fast float %mul2, 1.0
+  %add3 = fadd fast float %mul2, %add2
+  %mul3 = fmul fast float %y, %add3
+  ret float %mul3
+}


        


More information about the llvm-commits mailing list