[llvm] 20c71e5 - [InstSimplify] reduce code for min/max analysis; NFC

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 4 05:18:44 PDT 2020


Author: Sanjay Patel
Date: 2020-08-04T08:02:33-04:00
New Revision: 20c71e55aad5bf6008c7f5ed63c90ed98907fa99

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

LOG: [InstSimplify] reduce code for min/max analysis; NFC

This should probably be moved up to some common area eventually
when there's another user.

Added: 
    

Modified: 
    llvm/lib/Analysis/InstructionSimplify.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index f827f0230a3e..2119ddcc7649 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -5208,6 +5208,16 @@ static Intrinsic::ID getMaxMinOpposite(Intrinsic::ID ID) {
   }
 }
 
+static APInt getMaxMinLimit(Intrinsic::ID ID, unsigned BitWidth) {
+  switch (ID) {
+  case Intrinsic::smax: return APInt::getSignedMaxValue(BitWidth);
+  case Intrinsic::smin: return APInt::getSignedMinValue(BitWidth);
+  case Intrinsic::umax: return APInt::getMaxValue(BitWidth);
+  case Intrinsic::umin: return APInt::getMinValue(BitWidth);
+  default: llvm_unreachable("Unexpected intrinsic");
+  }
+}
+
 static Value *simplifyBinaryIntrinsic(Function *F, Value *Op0, Value *Op1,
                                       const SimplifyQuery &Q) {
   Intrinsic::ID IID = F->getIntrinsicID();
@@ -5238,16 +5248,8 @@ static Value *simplifyBinaryIntrinsic(Function *F, Value *Op0, Value *Op1,
       std::swap(Op0, Op1);
 
     // Assume undef is the limit value.
-    if (isa<UndefValue>(Op1)) {
-      if (IID == Intrinsic::smax)
-        return ConstantInt::get(ReturnType, APInt::getSignedMaxValue(BitWidth));
-      if (IID == Intrinsic::smin)
-        return ConstantInt::get(ReturnType, APInt::getSignedMinValue(BitWidth));
-      if (IID == Intrinsic::umax)
-        return ConstantInt::get(ReturnType, APInt::getMaxValue(BitWidth));
-      if (IID == Intrinsic::umin)
-        return ConstantInt::get(ReturnType, APInt::getMinValue(BitWidth));
-    }
+    if (isa<UndefValue>(Op1))
+      return ConstantInt::get(ReturnType, getMaxMinLimit(IID, BitWidth));
 
     auto hasSpecificOperand = [](IntrinsicInst *II, Value *V) {
       return II->getOperand(0) == V || II->getOperand(1) == V;
@@ -5272,25 +5274,18 @@ static Value *simplifyBinaryIntrinsic(Function *F, Value *Op0, Value *Op1,
     }
 
     const APInt *C;
-    if (!match(Op1, m_APIntAllowUndef(C)))
-      break;
-
-    // Clamp to limit value. For example:
-    // umax(i8 %x, i8 255) --> 255
-    if ((IID == Intrinsic::smax && C->isMaxSignedValue()) ||
-        (IID == Intrinsic::smin && C->isMinSignedValue()) ||
-        (IID == Intrinsic::umax && C->isMaxValue()) ||
-        (IID == Intrinsic::umin && C->isMinValue()))
-      return ConstantInt::get(ReturnType, *C);
-
-    // If the constant op is the opposite of the limit value, the other must be
-    // larger/smaller or equal. For example:
-    // umin(i8 %x, i8 255) --> %x
-    if ((IID == Intrinsic::smax && C->isMinSignedValue()) ||
-        (IID == Intrinsic::smin && C->isMaxSignedValue()) ||
-        (IID == Intrinsic::umax && C->isMinValue()) ||
-        (IID == Intrinsic::umin && C->isMaxValue()))
-      return Op0;
+    if (match(Op1, m_APIntAllowUndef(C))) {
+      // Clamp to limit value. For example:
+      // umax(i8 %x, i8 255) --> 255
+      if (*C == getMaxMinLimit(IID, BitWidth))
+        return ConstantInt::get(ReturnType, *C);
+
+      // If the constant op is the opposite of the limit value, the other must
+      // be larger/smaller or equal. For example:
+      // umin(i8 %x, i8 255) --> %x
+      if (*C == getMaxMinLimit(getMaxMinOpposite(IID), BitWidth))
+        return Op0;
+    }
 
     break;
   }


        


More information about the llvm-commits mailing list