OSDN Git Service

[InstSimplify] reduce code duplication for fmin/fmax folds; NFC
authorSanjay Patel <spatel@rotateright.com>
Mon, 14 Sep 2020 14:32:11 +0000 (10:32 -0400)
committerSanjay Patel <spatel@rotateright.com>
Mon, 14 Sep 2020 14:32:11 +0000 (10:32 -0400)
We use the same code structure for folding integer min/max.

llvm/lib/Analysis/InstructionSimplify.cpp

index 271e79d..9933360 100644 (file)
@@ -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: