[llvm] 01ae6e5 - [InstCombine] sink min/max intrinsics with common op after select

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 28 10:23:27 PDT 2021


Author: Sanjay Patel
Date: 2021-03-28T13:13:04-04:00
New Revision: 01ae6e5ead64c033134a1ee68fb0bf6ec93b4c40

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

LOG: [InstCombine] sink min/max intrinsics with common op after select

This is another step towards parity with cmp+select min/max idioms.

See D98152.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
    llvm/test/Transforms/InstCombine/select-min-max.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index 7d7a52b4de31..a1ec11e95081 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -327,6 +327,35 @@ Instruction *InstCombinerImpl::foldSelectOpOp(SelectInst &SI, Instruction *TI,
     return UnaryOperator::CreateFNegFMF(NewSel, TI);
   }
 
+  // Min/max intrinsic with a common operand can have the common operand pulled
+  // after the select. This is the same transform as below for binops, but
+  // specialized for intrinsic matching and without the restrictive uses clause.
+  auto *TII = dyn_cast<IntrinsicInst>(TI);
+  auto *FII = dyn_cast<IntrinsicInst>(FI);
+  if (TII && FII && TII->getIntrinsicID() == FII->getIntrinsicID() &&
+      (TII->hasOneUse() || FII->hasOneUse())) {
+    Value *T0, *T1, *F0, *F1;
+    if (match(TII, m_MaxOrMin(m_Value(T0), m_Value(T1))) &&
+        match(FII, m_MaxOrMin(m_Value(F0), m_Value(F1)))) {
+      if (T0 == F0) {
+        Value *NewSel = Builder.CreateSelect(Cond, T1, F1, "minmaxop", &SI);
+        return CallInst::Create(TII->getCalledFunction(), {NewSel, T0});
+      }
+      if (T0 == F1) {
+        Value *NewSel = Builder.CreateSelect(Cond, T1, F0, "minmaxop", &SI);
+        return CallInst::Create(TII->getCalledFunction(), {NewSel, T0});
+      }
+      if (T1 == F0) {
+        Value *NewSel = Builder.CreateSelect(Cond, T0, F1, "minmaxop", &SI);
+        return CallInst::Create(TII->getCalledFunction(), {NewSel, T1});
+      }
+      if (T1 == F1) {
+        Value *NewSel = Builder.CreateSelect(Cond, T0, F0, "minmaxop", &SI);
+        return CallInst::Create(TII->getCalledFunction(), {NewSel, T1});
+      }
+    }
+  }
+
   // Only handle binary operators (including two-operand getelementptr) with
   // one-use here. As with the cast case above, it may be possible to relax the
   // one-use constraint, but that needs be examined carefully since it may not

diff  --git a/llvm/test/Transforms/InstCombine/select-min-max.ll b/llvm/test/Transforms/InstCombine/select-min-max.ll
index 2fc0a4367cfc..d4e6ff92a0c4 100644
--- a/llvm/test/Transforms/InstCombine/select-min-max.ll
+++ b/llvm/test/Transforms/InstCombine/select-min-max.ll
@@ -8,9 +8,8 @@ declare <3 x i5> @llvm.umax.v3i5(<3 x i5>, <3 x i5>)
 
 define i5 @smin_smin_common_op_00(i1 %cond, i5 %x, i5 %y, i5 %z) {
 ; CHECK-LABEL: @smin_smin_common_op_00(
-; CHECK-NEXT:    [[M1:%.*]] = call i5 @llvm.smin.i5(i5 [[Z:%.*]], i5 [[X:%.*]])
-; CHECK-NEXT:    [[M2:%.*]] = call i5 @llvm.smin.i5(i5 [[Z]], i5 [[Y:%.*]])
-; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[COND:%.*]], i5 [[M1]], i5 [[M2]]
+; CHECK-NEXT:    [[MINMAXOP:%.*]] = select i1 [[COND:%.*]], i5 [[X:%.*]], i5 [[Y:%.*]]
+; CHECK-NEXT:    [[SEL:%.*]] = call i5 @llvm.smin.i5(i5 [[MINMAXOP]], i5 [[Z:%.*]])
 ; CHECK-NEXT:    ret i5 [[SEL]]
 ;
   %m1 = call i5 @llvm.smin.i5(i5 %z, i5 %x)
@@ -21,9 +20,8 @@ define i5 @smin_smin_common_op_00(i1 %cond, i5 %x, i5 %y, i5 %z) {
 
 define <2 x i8> @smax_smax_common_op_01(<2 x i1> %cond, <2 x i8> %x, <2 x i8> %y, <2 x i8> %z) {
 ; CHECK-LABEL: @smax_smax_common_op_01(
-; CHECK-NEXT:    [[M1:%.*]] = call <2 x i8> @llvm.smax.v2i8(<2 x i8> [[Z:%.*]], <2 x i8> [[X:%.*]])
-; CHECK-NEXT:    [[M2:%.*]] = call <2 x i8> @llvm.smax.v2i8(<2 x i8> [[Y:%.*]], <2 x i8> [[Z]])
-; CHECK-NEXT:    [[SEL:%.*]] = select <2 x i1> [[COND:%.*]], <2 x i8> [[M1]], <2 x i8> [[M2]]
+; CHECK-NEXT:    [[MINMAXOP:%.*]] = select <2 x i1> [[COND:%.*]], <2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]]
+; CHECK-NEXT:    [[SEL:%.*]] = call <2 x i8> @llvm.smax.v2i8(<2 x i8> [[MINMAXOP]], <2 x i8> [[Z:%.*]])
 ; CHECK-NEXT:    ret <2 x i8> [[SEL]]
 ;
   %m1 = call <2 x i8> @llvm.smax.v2i8(<2 x i8> %z, <2 x i8> %x)
@@ -36,8 +34,8 @@ define i5 @umin_umin_common_op_10(i1 %cond, i5 %x, i5 %y, i5 %z, i5* %p) {
 ; CHECK-LABEL: @umin_umin_common_op_10(
 ; CHECK-NEXT:    [[M1:%.*]] = call i5 @llvm.umin.i5(i5 [[X:%.*]], i5 [[Z:%.*]])
 ; CHECK-NEXT:    store i5 [[M1]], i5* [[P:%.*]], align 1
-; CHECK-NEXT:    [[M2:%.*]] = call i5 @llvm.umin.i5(i5 [[Z]], i5 [[Y:%.*]])
-; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[COND:%.*]], i5 [[M1]], i5 [[M2]]
+; CHECK-NEXT:    [[MINMAXOP:%.*]] = select i1 [[COND:%.*]], i5 [[X]], i5 [[Y:%.*]]
+; CHECK-NEXT:    [[SEL:%.*]] = call i5 @llvm.umin.i5(i5 [[MINMAXOP]], i5 [[Z]])
 ; CHECK-NEXT:    ret i5 [[SEL]]
 ;
   %m1 = call i5 @llvm.umin.i5(i5 %x, i5 %z)
@@ -49,10 +47,10 @@ define i5 @umin_umin_common_op_10(i1 %cond, i5 %x, i5 %y, i5 %z, i5* %p) {
 
 define <3 x i5> @umax_umax_common_op_11(i1 %cond, <3 x i5> %x, <3 x i5> %y, <3 x i5> %z, <3 x i5>* %p) {
 ; CHECK-LABEL: @umax_umax_common_op_11(
-; CHECK-NEXT:    [[M1:%.*]] = call <3 x i5> @llvm.umax.v3i5(<3 x i5> [[X:%.*]], <3 x i5> [[Z:%.*]])
-; CHECK-NEXT:    [[M2:%.*]] = call <3 x i5> @llvm.umax.v3i5(<3 x i5> [[Y:%.*]], <3 x i5> [[Z]])
+; CHECK-NEXT:    [[M2:%.*]] = call <3 x i5> @llvm.umax.v3i5(<3 x i5> [[Y:%.*]], <3 x i5> [[Z:%.*]])
 ; CHECK-NEXT:    store <3 x i5> [[M2]], <3 x i5>* [[P:%.*]], align 4
-; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[COND:%.*]], <3 x i5> [[M1]], <3 x i5> [[M2]]
+; CHECK-NEXT:    [[MINMAXOP:%.*]] = select i1 [[COND:%.*]], <3 x i5> [[X:%.*]], <3 x i5> [[Y]]
+; CHECK-NEXT:    [[SEL:%.*]] = call <3 x i5> @llvm.umax.v3i5(<3 x i5> [[MINMAXOP]], <3 x i5> [[Z]])
 ; CHECK-NEXT:    ret <3 x i5> [[SEL]]
 ;
   %m1 = call <3 x i5> @llvm.umax.v3i5(<3 x i5> %x, <3 x i5> %z)
@@ -62,6 +60,8 @@ define <3 x i5> @umax_umax_common_op_11(i1 %cond, <3 x i5> %x, <3 x i5> %y, <3 x
   ret <3 x i5> %sel
 }
 
+; negative test - intrinsic mismatch
+
 define i5 @smin_umin_common_op_11(i1 %cond, i5 %x, i5 %y, i5 %z) {
 ; CHECK-LABEL: @smin_umin_common_op_11(
 ; CHECK-NEXT:    [[M1:%.*]] = call i5 @llvm.smin.i5(i5 [[X:%.*]], i5 [[Z:%.*]])
@@ -75,6 +75,8 @@ define i5 @smin_umin_common_op_11(i1 %cond, i5 %x, i5 %y, i5 %z) {
   ret i5 %sel
 }
 
+; negative test - require shared operand
+
 define i5 @smin_smin_no_common_op(i1 %cond, i5 %x, i5 %y, i5 %z, i5 %w) {
 ; CHECK-LABEL: @smin_smin_no_common_op(
 ; CHECK-NEXT:    [[M1:%.*]] = call i5 @llvm.smin.i5(i5 [[Z:%.*]], i5 [[X:%.*]])
@@ -88,6 +90,8 @@ define i5 @smin_smin_no_common_op(i1 %cond, i5 %x, i5 %y, i5 %z, i5 %w) {
   ret i5 %sel
 }
 
+; negative test - too many uses
+
 define i5 @umin_umin_common_op_10_uses(i1 %cond, i5 %x, i5 %y, i5 %z, i5* %p1, i5* %p2) {
 ; CHECK-LABEL: @umin_umin_common_op_10_uses(
 ; CHECK-NEXT:    [[M1:%.*]] = call i5 @llvm.umin.i5(i5 [[X:%.*]], i5 [[Z:%.*]])


        


More information about the llvm-commits mailing list