[llvm] 4ec7c02 - [InstSimplify] fix bug in poison propagation for FP ops

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 6 11:07:17 PDT 2021


Author: Sanjay Patel
Date: 2021-07-06T14:06:50-04:00
New Revision: 4ec7c021970d83ce49273ca5623953665720515f

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

LOG: [InstSimplify] fix bug in poison propagation for FP ops

If any operand of a math op is poison, that takes
precedence over general undef/NaN.

This should not be visible with binary ops because
it requires 2 constant operands to trigger (and if
both operands of a binop are constant, that should
get handled first in ConstantFolding).

Added: 
    

Modified: 
    llvm/lib/Analysis/InstructionSimplify.cpp
    llvm/test/Transforms/InstSimplify/call.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 35d1e34188d9..bd22dafa68a4 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -4857,12 +4857,12 @@ static Constant *propagateNaN(Constant *In) {
 /// 
diff erence to the result.
 static Constant *simplifyFPOp(ArrayRef<Value *> Ops, FastMathFlags FMF,
                               const SimplifyQuery &Q) {
-  for (Value *V : Ops) {
-    // Poison is independent of anything else. It always propagates from an
-    // operand to a math result.
-    if (match(V, m_Poison()))
-      return PoisonValue::get(V->getType());
+  // Poison is independent of anything else. It always propagates from an
+  // operand to a math result.
+  if (any_of(Ops, [](Value *V) { return match(V, m_Poison()); }))
+    return PoisonValue::get(Ops[0]->getType());
 
+  for (Value *V : Ops) {
     bool IsNan = match(V, m_NaN());
     bool IsInf = match(V, m_Inf());
     bool IsUndef = Q.isUndefValue(V);

diff  --git a/llvm/test/Transforms/InstSimplify/call.ll b/llvm/test/Transforms/InstSimplify/call.ll
index ac7637a61db1..8b9a8cb63d8d 100644
--- a/llvm/test/Transforms/InstSimplify/call.ll
+++ b/llvm/test/Transforms/InstSimplify/call.ll
@@ -1011,7 +1011,7 @@ define double @fma_poison_op2(double %x, double %y) {
 
 define double @fma_undef_op0_poison_op1(double %x) {
 ; CHECK-LABEL: @fma_undef_op0_poison_op1(
-; CHECK-NEXT:    ret double 0x7FF8000000000000
+; CHECK-NEXT:    ret double poison
 ;
   %r = call double @llvm.fma.f64(double undef, double poison, double %x)
   ret double %r
@@ -1019,7 +1019,7 @@ define double @fma_undef_op0_poison_op1(double %x) {
 
 define double @fma_undef_op0_poison_op2(double %x) {
 ; CHECK-LABEL: @fma_undef_op0_poison_op2(
-; CHECK-NEXT:    ret double 0x7FF8000000000000
+; CHECK-NEXT:    ret double poison
 ;
   %r = call double @llvm.fma.f64(double undef, double %x, double poison)
   ret double %r
@@ -1075,7 +1075,7 @@ define double @fmuladd_poison_op2(double %x, double %y) {
 
 define double @fmuladd_nan_op0_poison_op1(double %x) {
 ; CHECK-LABEL: @fmuladd_nan_op0_poison_op1(
-; CHECK-NEXT:    ret double 0x7FF8000000000000
+; CHECK-NEXT:    ret double poison
 ;
   %r = call double @llvm.fmuladd.f64(double 0x7ff8000000000000, double poison, double %x)
   ret double %r
@@ -1083,7 +1083,7 @@ define double @fmuladd_nan_op0_poison_op1(double %x) {
 
 define double @fmuladd_nan_op1_poison_op2(double %x) {
 ; CHECK-LABEL: @fmuladd_nan_op1_poison_op2(
-; CHECK-NEXT:    ret double 0x7FF8000000000000
+; CHECK-NEXT:    ret double poison
 ;
   %r = call double @llvm.fmuladd.f64(double %x, double 0x7ff8000000000000, double poison)
   ret double %r


        


More information about the llvm-commits mailing list