[llvm] fef513f - [InstSimplify] fold min/max intrinsic with undef operand

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 29 14:04:58 PDT 2020


Author: Sanjay Patel
Date: 2020-07-29T17:03:50-04:00
New Revision: fef513f5ccb799853a8aed46df2463de1cb4aed9

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

LOG: [InstSimplify] fold min/max intrinsic with undef operand

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 9d9999ea4372..178b5bed2006 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -5253,6 +5253,7 @@ static Value *simplifyBinaryIntrinsic(Function *F, Value *Op0, Value *Op1,
                                       const SimplifyQuery &Q) {
   Intrinsic::ID IID = F->getIntrinsicID();
   Type *ReturnType = F->getReturnType();
+  unsigned BitWidth = ReturnType->getScalarSizeInBits();
   switch (IID) {
   case Intrinsic::smax:
   case Intrinsic::smin:
@@ -5266,6 +5267,18 @@ static Value *simplifyBinaryIntrinsic(Function *F, Value *Op0, Value *Op1,
     if (isa<Constant>(Op0))
       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));
+    }
+
     const APInt *C;
     if (!match(Op1, m_APIntAllowUndef(C)))
       break;

diff  --git a/llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll b/llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll
index a376ef8adf1a..40dacb2d07ba 100644
--- a/llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll
+++ b/llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll
@@ -46,8 +46,7 @@ define <2 x i8> @umin_sameval(<2 x i8> %x) {
 
 define i81 @smax_undef(i81 %x) {
 ; CHECK-LABEL: @smax_undef(
-; CHECK-NEXT:    [[R:%.*]] = call i81 @llvm.smax.i81(i81 undef, i81 [[X:%.*]])
-; CHECK-NEXT:    ret i81 [[R]]
+; CHECK-NEXT:    ret i81 1208925819614629174706175
 ;
   %r = call i81 @llvm.smax.i81(i81 undef, i81 %x)
   ret i81 %r
@@ -55,8 +54,7 @@ define i81 @smax_undef(i81 %x) {
 
 define i3 @smin_undef(i3 %x) {
 ; CHECK-LABEL: @smin_undef(
-; CHECK-NEXT:    [[R:%.*]] = call i3 @llvm.smin.i3(i3 [[X:%.*]], i3 undef)
-; CHECK-NEXT:    ret i3 [[R]]
+; CHECK-NEXT:    ret i3 -4
 ;
   %r = call i3 @llvm.smin.i3(i3 %x, i3 undef)
   ret i3 %r
@@ -64,8 +62,7 @@ define i3 @smin_undef(i3 %x) {
 
 define <2 x i8> @umax_undef(<2 x i8> %x) {
 ; CHECK-LABEL: @umax_undef(
-; CHECK-NEXT:    [[R:%.*]] = call <2 x i8> @llvm.umax.v2i8(<2 x i8> undef, <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> undef, <2 x i8> %x)
   ret <2 x i8> %r
@@ -73,8 +70,7 @@ define <2 x i8> @umax_undef(<2 x i8> %x) {
 
 define <2 x i8> @umin_undef(<2 x i8> %x) {
 ; CHECK-LABEL: @umin_undef(
-; CHECK-NEXT:    [[R:%.*]] = call <2 x i8> @llvm.umin.v2i8(<2 x i8> [[X:%.*]], <2 x i8> undef)
-; 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> undef)
   ret <2 x i8> %r


        


More information about the llvm-commits mailing list