[llvm] 22c583c - [InstSimplify] reduce code duplication for fmin/fmax folds; NFC

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 14 07:33:33 PDT 2020


Author: Sanjay Patel
Date: 2020-09-14T10:32:11-04:00
New Revision: 22c583c3d03a6750d6474ad46e5d52eb9974e2b0

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

LOG: [InstSimplify] reduce code duplication for fmin/fmax folds; NFC

We use the same code structure for folding integer min/max.

Added: 
    

Modified: 
    llvm/lib/Analysis/InstructionSimplify.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 271e79df7153..9933360a3a1a 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -5447,19 +5447,32 @@ static Value *simplifyBinaryIntrinsic(Function *F, Value *Op0, Value *Op1,
     // If the arguments are the same, this is a no-op.
     if (Op0 == Op1) return Op0;
 
-    // If one argument is undef, return the other argument.
-    if (Q.isUndefValue(Op0))
-      return Op1;
+    // Canonicalize constant operand as Op1.
+    if (isa<Constant>(Op0))
+      std::swap(Op0, Op1);
+
+    // If an argument is undef, return the other argument.
     if (Q.isUndefValue(Op1))
       return Op0;
 
-    // If one argument is NaN, return other or NaN appropriately.
+    // If an argument is NaN, return other or NaN appropriately.
     bool PropagateNaN = IID == Intrinsic::minimum || IID == Intrinsic::maximum;
-    if (match(Op0, m_NaN()))
-      return PropagateNaN ? Op0 : Op1;
     if (match(Op1, m_NaN()))
       return PropagateNaN ? Op1 : Op0;
 
+    // min(X, -Inf) --> -Inf
+    // max(X, +Inf) --> +Inf
+    bool UseNegInf = IID == Intrinsic::minnum || IID == Intrinsic::minimum;
+    const APFloat *C;
+    if (match(Op1, m_APFloat(C)) && C->isInfinity() &&
+        C->isNegative() == UseNegInf && !PropagateNaN)
+      return ConstantFP::getInfinity(ReturnType, UseNegInf);
+
+    // TODO: minimum(nnan x, inf) -> x
+    // TODO: minnum(nnan ninf x, flt_max) -> x
+    // TODO: maximum(nnan x, -inf) -> x
+    // TODO: maxnum(nnan ninf x, -flt_max) -> x
+
     // Min/max of the same operation with common operand:
     // m(m(X, Y)), X --> m(X, Y) (4 commuted variants)
     if (auto *M0 = dyn_cast<IntrinsicInst>(Op0))
@@ -5471,20 +5484,6 @@ static Value *simplifyBinaryIntrinsic(Function *F, Value *Op0, Value *Op1,
           (M1->getOperand(0) == Op0 || M1->getOperand(1) == Op0))
         return Op1;
 
-    // min(X, -Inf) --> -Inf (and commuted variant)
-    // max(X, +Inf) --> +Inf (and commuted variant)
-    bool UseNegInf = IID == Intrinsic::minnum || IID == Intrinsic::minimum;
-    const APFloat *C;
-    if ((match(Op0, m_APFloat(C)) && C->isInfinity() &&
-         C->isNegative() == UseNegInf && !PropagateNaN) ||
-        (match(Op1, m_APFloat(C)) && C->isInfinity() &&
-         C->isNegative() == UseNegInf && !PropagateNaN))
-      return ConstantFP::getInfinity(ReturnType, UseNegInf);
-
-    // TODO: minnum(nnan x, inf) -> x
-    // TODO: minnum(nnan ninf x, flt_max) -> x
-    // TODO: maxnum(nnan x, -inf) -> x
-    // TODO: maxnum(nnan ninf x, -flt_max) -> x
     break;
   }
   default:


        


More information about the llvm-commits mailing list