[llvm] 0481e1a - [InstSimplify] fold integer min/max intrinsics with limit constant

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 26 06:42:03 PDT 2020


Author: Sanjay Patel
Date: 2020-07-26T09:41:54-04:00
New Revision: 0481e1ae3c17a8faa6c8e319495a51a76b77b76b

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

LOG: [InstSimplify] fold integer min/max intrinsics with limit constant

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 396fc22920cd..c920fb3f52be 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -5254,6 +5254,27 @@ static Value *simplifyBinaryIntrinsic(Function *F, Value *Op0, Value *Op1,
   Intrinsic::ID IID = F->getIntrinsicID();
   Type *ReturnType = F->getReturnType();
   switch (IID) {
+  case Intrinsic::smax:
+  case Intrinsic::smin:
+  case Intrinsic::umax:
+  case Intrinsic::umin: {
+    // Canonicalize constant operand as Op1.
+    if (isa<Constant>(Op0))
+      std::swap(Op0, Op1);
+
+    // TODO: Allow partial undef vector constants.
+    const APInt *C;
+    if (!match(Op1, m_APInt(C)))
+      break;
+
+    if ((IID == Intrinsic::smax && C->isMaxSignedValue()) ||
+        (IID == Intrinsic::smin && C->isMinSignedValue()) ||
+        (IID == Intrinsic::umax && C->isMaxValue()) ||
+        (IID == Intrinsic::umin && C->isMinValue()))
+      return Op1;
+
+    break;
+  }
   case Intrinsic::usub_with_overflow:
   case Intrinsic::ssub_with_overflow:
     // X - X -> { 0, false }

diff  --git a/llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll b/llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll
index fe940ef7bc17..99a8656b34aa 100644
--- a/llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll
+++ b/llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll
@@ -12,8 +12,7 @@ declare <2 x i8> @llvm.umin.v2i8(<2 x i8>, <2 x i8>)
 
 define i8 @smax_maxval(i8 %x) {
 ; CHECK-LABEL: @smax_maxval(
-; CHECK-NEXT:    [[R:%.*]] = call i8 @llvm.smax.i8(i8 [[X:%.*]], i8 127)
-; CHECK-NEXT:    ret i8 [[R]]
+; CHECK-NEXT:    ret i8 127
 ;
   %r = call i8 @llvm.smax.i8(i8 %x, i8 127)
   ret i8 %r
@@ -21,8 +20,7 @@ define i8 @smax_maxval(i8 %x) {
 
 define <2 x i8> @smax_maxval_commute(<2 x i8> %x) {
 ; CHECK-LABEL: @smax_maxval_commute(
-; CHECK-NEXT:    [[R:%.*]] = call <2 x i8> @llvm.smax.v2i8(<2 x i8> <i8 127, i8 127>, <2 x i8> [[X:%.*]])
-; CHECK-NEXT:    ret <2 x i8> [[R]]
+; CHECK-NEXT:    ret <2 x i8> <i8 127, i8 127>
 ;
   %r = call <2 x i8> @llvm.smax.v2i8(<2 x i8> <i8 127, i8 127>, <2 x i8> %x)
   ret <2 x i8> %r
@@ -30,8 +28,7 @@ define <2 x i8> @smax_maxval_commute(<2 x i8> %x) {
 
 define i8 @smin_minval(i8 %x) {
 ; CHECK-LABEL: @smin_minval(
-; CHECK-NEXT:    [[R:%.*]] = call i8 @llvm.smin.i8(i8 -128, i8 [[X:%.*]])
-; CHECK-NEXT:    ret i8 [[R]]
+; CHECK-NEXT:    ret i8 -128
 ;
   %r = call i8 @llvm.smin.i8(i8 -128, i8 %x)
   ret i8 %r
@@ -39,8 +36,7 @@ define i8 @smin_minval(i8 %x) {
 
 define <2 x i8> @smin_minval_commute(<2 x i8> %x) {
 ; CHECK-LABEL: @smin_minval_commute(
-; CHECK-NEXT:    [[R:%.*]] = call <2 x i8> @llvm.smin.v2i8(<2 x i8> [[X:%.*]], <2 x i8> <i8 -128, i8 -128>)
-; CHECK-NEXT:    ret <2 x i8> [[R]]
+; CHECK-NEXT:    ret <2 x i8> <i8 -128, i8 -128>
 ;
   %r = call <2 x i8> @llvm.smin.v2i8(<2 x i8> %x, <2 x i8> <i8 -128, i8 -128>)
   ret <2 x i8> %r
@@ -48,8 +44,7 @@ define <2 x i8> @smin_minval_commute(<2 x i8> %x) {
 
 define i8 @umax_maxval(i8 %x) {
 ; CHECK-LABEL: @umax_maxval(
-; CHECK-NEXT:    [[R:%.*]] = call i8 @llvm.umax.i8(i8 [[X:%.*]], i8 -1)
-; CHECK-NEXT:    ret i8 [[R]]
+; CHECK-NEXT:    ret i8 -1
 ;
   %r = call i8 @llvm.umax.i8(i8 %x, i8 255)
   ret i8 %r
@@ -57,8 +52,7 @@ define i8 @umax_maxval(i8 %x) {
 
 define <2 x i8> @umax_maxval_commute(<2 x i8> %x) {
 ; CHECK-LABEL: @umax_maxval_commute(
-; CHECK-NEXT:    [[R:%.*]] = call <2 x i8> @llvm.umax.v2i8(<2 x i8> <i8 -1, i8 -1>, <2 x i8> [[X:%.*]])
-; CHECK-NEXT:    ret <2 x i8> [[R]]
+; CHECK-NEXT:    ret <2 x i8> <i8 -1, i8 -1>
 ;
   %r = call <2 x i8> @llvm.umax.v2i8(<2 x i8> <i8 255, i8 255>, <2 x i8> %x)
   ret <2 x i8> %r
@@ -66,8 +60,7 @@ define <2 x i8> @umax_maxval_commute(<2 x i8> %x) {
 
 define i8 @umin_minval(i8 %x) {
 ; CHECK-LABEL: @umin_minval(
-; CHECK-NEXT:    [[R:%.*]] = call i8 @llvm.umin.i8(i8 0, i8 [[X:%.*]])
-; CHECK-NEXT:    ret i8 [[R]]
+; CHECK-NEXT:    ret i8 0
 ;
   %r = call i8 @llvm.umin.i8(i8 0, i8 %x)
   ret i8 %r
@@ -75,8 +68,7 @@ define i8 @umin_minval(i8 %x) {
 
 define <2 x i8> @umin_minval_commute(<2 x i8> %x) {
 ; CHECK-LABEL: @umin_minval_commute(
-; CHECK-NEXT:    [[R:%.*]] = call <2 x i8> @llvm.umin.v2i8(<2 x i8> [[X:%.*]], <2 x i8> zeroinitializer)
-; CHECK-NEXT:    ret <2 x i8> [[R]]
+; CHECK-NEXT:    ret <2 x i8> zeroinitializer
 ;
   %r = call <2 x i8> @llvm.umin.v2i8(<2 x i8> %x, <2 x i8> zeroinitializer)
   ret <2 x i8> %r


        


More information about the llvm-commits mailing list