[llvm] 9e5cf6b - [InstSimplify] fold variations of max-of-min with common operand
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 3 12:15:47 PDT 2020
Author: Sanjay Patel
Date: 2020-08-03T15:02:46-04:00
New Revision: 9e5cf6bde5963f14a38117061c7a4df064453088
URL: https://github.com/llvm/llvm-project/commit/9e5cf6bde5963f14a38117061c7a4df064453088
DIFF: https://github.com/llvm/llvm-project/commit/9e5cf6bde5963f14a38117061c7a4df064453088.diff
LOG: [InstSimplify] fold variations of max-of-min with common operand
https://alive2.llvm.org/ce/z/ZtxpZ3
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 6e75478d52af..f827f0230a3e 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -5198,6 +5198,16 @@ static Value *simplifyUnaryIntrinsic(Function *F, Value *Op0,
return nullptr;
}
+static Intrinsic::ID getMaxMinOpposite(Intrinsic::ID ID) {
+ switch (ID) {
+ case Intrinsic::smax: return Intrinsic::smin;
+ case Intrinsic::smin: return Intrinsic::smax;
+ case Intrinsic::umax: return Intrinsic::umin;
+ case Intrinsic::umin: return Intrinsic::umax;
+ default: llvm_unreachable("Unexpected intrinsic");
+ }
+}
+
static Value *simplifyBinaryIntrinsic(Function *F, Value *Op0, Value *Op1,
const SimplifyQuery &Q) {
Intrinsic::ID IID = F->getIntrinsicID();
@@ -5239,16 +5249,27 @@ static Value *simplifyBinaryIntrinsic(Function *F, Value *Op0, Value *Op1,
return ConstantInt::get(ReturnType, APInt::getMinValue(BitWidth));
}
+ auto hasSpecificOperand = [](IntrinsicInst *II, Value *V) {
+ return II->getOperand(0) == V || II->getOperand(1) == V;
+ };
+
// For 4 commuted variants of each intrinsic:
// max (max X, Y), X --> max X, Y
- if (auto *MinMax0 = dyn_cast<IntrinsicInst>(Op0))
- if (MinMax0->getIntrinsicID() == IID &&
- (MinMax0->getOperand(0) == Op1 || MinMax0->getOperand(1) == Op1))
+ // max (min X, Y), X --> X
+ if (auto *MinMax0 = dyn_cast<IntrinsicInst>(Op0)) {
+ Intrinsic::ID InnerID = MinMax0->getIntrinsicID();
+ if (InnerID == IID && hasSpecificOperand(MinMax0, Op1))
return MinMax0;
- if (auto *MinMax1 = dyn_cast<IntrinsicInst>(Op1))
- if (MinMax1->getIntrinsicID() == IID &&
- (MinMax1->getOperand(0) == Op0 || MinMax1->getOperand(1) == Op0))
+ if (InnerID == getMaxMinOpposite(IID) && hasSpecificOperand(MinMax0, Op1))
+ return Op1;
+ }
+ if (auto *MinMax1 = dyn_cast<IntrinsicInst>(Op1)) {
+ Intrinsic::ID InnerID = MinMax1->getIntrinsicID();
+ if (InnerID == IID && hasSpecificOperand(MinMax1, Op0))
return MinMax1;
+ if (InnerID == getMaxMinOpposite(IID) && hasSpecificOperand(MinMax1, Op0))
+ return Op0;
+ }
const APInt *C;
if (!match(Op1, m_APIntAllowUndef(C)))
diff --git a/llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll b/llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll
index 7c79357a0bd0..7a31a4dcb9a3 100644
--- a/llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll
+++ b/llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll
@@ -430,9 +430,7 @@ define i8 @smin_smin_commute3(i8 %x, i8 %y) {
define i8 @umax_umin(i8 %x, i8 %y) {
; CHECK-LABEL: @umax_umin(
-; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.umax.i8(i8 [[X:%.*]], i8 [[Y:%.*]])
-; CHECK-NEXT: [[M2:%.*]] = call i8 @llvm.umin.i8(i8 [[X]], i8 [[M]])
-; CHECK-NEXT: ret i8 [[M2]]
+; CHECK-NEXT: ret i8 [[X:%.*]]
;
%m = call i8 @llvm.umax.i8(i8 %x, i8 %y)
%m2 = call i8 @llvm.umin.i8(i8 %x, i8 %m)
@@ -441,9 +439,7 @@ define i8 @umax_umin(i8 %x, i8 %y) {
define i8 @umax_umin_commute1(i8 %x, i8 %y) {
; CHECK-LABEL: @umax_umin_commute1(
-; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.umax.i8(i8 [[Y:%.*]], i8 [[X:%.*]])
-; CHECK-NEXT: [[M2:%.*]] = call i8 @llvm.umin.i8(i8 [[X]], i8 [[M]])
-; CHECK-NEXT: ret i8 [[M2]]
+; CHECK-NEXT: ret i8 [[X:%.*]]
;
%m = call i8 @llvm.umax.i8(i8 %y, i8 %x)
%m2 = call i8 @llvm.umin.i8(i8 %x, i8 %m)
@@ -452,9 +448,7 @@ define i8 @umax_umin_commute1(i8 %x, i8 %y) {
define i8 @umax_umin_commute2(i8 %x, i8 %y) {
; CHECK-LABEL: @umax_umin_commute2(
-; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.umax.i8(i8 [[X:%.*]], i8 [[Y:%.*]])
-; CHECK-NEXT: [[M2:%.*]] = call i8 @llvm.umin.i8(i8 [[M]], i8 [[X]])
-; CHECK-NEXT: ret i8 [[M2]]
+; CHECK-NEXT: ret i8 [[X:%.*]]
;
%m = call i8 @llvm.umax.i8(i8 %x, i8 %y)
%m2 = call i8 @llvm.umin.i8(i8 %m, i8 %x)
@@ -463,9 +457,7 @@ define i8 @umax_umin_commute2(i8 %x, i8 %y) {
define <2 x i8> @umax_umin_commute3(<2 x i8> %x, <2 x i8> %y) {
; CHECK-LABEL: @umax_umin_commute3(
-; CHECK-NEXT: [[M:%.*]] = call <2 x i8> @llvm.umax.v2i8(<2 x i8> [[Y:%.*]], <2 x i8> [[X:%.*]])
-; CHECK-NEXT: [[M2:%.*]] = call <2 x i8> @llvm.umin.v2i8(<2 x i8> [[M]], <2 x i8> [[X]])
-; CHECK-NEXT: ret <2 x i8> [[M2]]
+; CHECK-NEXT: ret <2 x i8> [[X:%.*]]
;
%m = call <2 x i8> @llvm.umax.v2i8(<2 x i8> %y, <2 x i8> %x)
%m2 = call <2 x i8> @llvm.umin.v2i8(<2 x i8> %m, <2 x i8> %x)
@@ -474,9 +466,7 @@ define <2 x i8> @umax_umin_commute3(<2 x i8> %x, <2 x i8> %y) {
define i8 @umin_umax(i8 %x, i8 %y) {
; CHECK-LABEL: @umin_umax(
-; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.umin.i8(i8 [[X:%.*]], i8 [[Y:%.*]])
-; CHECK-NEXT: [[M2:%.*]] = call i8 @llvm.umax.i8(i8 [[X]], i8 [[M]])
-; CHECK-NEXT: ret i8 [[M2]]
+; CHECK-NEXT: ret i8 [[X:%.*]]
;
%m = call i8 @llvm.umin.i8(i8 %x, i8 %y)
%m2 = call i8 @llvm.umax.i8(i8 %x, i8 %m)
@@ -485,9 +475,7 @@ define i8 @umin_umax(i8 %x, i8 %y) {
define i8 @umin_umax_commute1(i8 %x, i8 %y) {
; CHECK-LABEL: @umin_umax_commute1(
-; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.umin.i8(i8 [[Y:%.*]], i8 [[X:%.*]])
-; CHECK-NEXT: [[M2:%.*]] = call i8 @llvm.umax.i8(i8 [[X]], i8 [[M]])
-; CHECK-NEXT: ret i8 [[M2]]
+; CHECK-NEXT: ret i8 [[X:%.*]]
;
%m = call i8 @llvm.umin.i8(i8 %y, i8 %x)
%m2 = call i8 @llvm.umax.i8(i8 %x, i8 %m)
@@ -496,9 +484,7 @@ define i8 @umin_umax_commute1(i8 %x, i8 %y) {
define <2 x i8> @umin_umax_commute2(<2 x i8> %x, <2 x i8> %y) {
; CHECK-LABEL: @umin_umax_commute2(
-; CHECK-NEXT: [[M:%.*]] = call <2 x i8> @llvm.umin.v2i8(<2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]])
-; CHECK-NEXT: [[M2:%.*]] = call <2 x i8> @llvm.umax.v2i8(<2 x i8> [[M]], <2 x i8> [[X]])
-; CHECK-NEXT: ret <2 x i8> [[M2]]
+; CHECK-NEXT: ret <2 x i8> [[X:%.*]]
;
%m = call <2 x i8> @llvm.umin.v2i8(<2 x i8> %x, <2 x i8> %y)
%m2 = call <2 x i8> @llvm.umax.v2i8(<2 x i8> %m, <2 x i8> %x)
@@ -507,9 +493,7 @@ define <2 x i8> @umin_umax_commute2(<2 x i8> %x, <2 x i8> %y) {
define i8 @umin_umax_commute3(i8 %x, i8 %y) {
; CHECK-LABEL: @umin_umax_commute3(
-; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.umin.i8(i8 [[Y:%.*]], i8 [[X:%.*]])
-; CHECK-NEXT: [[M2:%.*]] = call i8 @llvm.umax.i8(i8 [[M]], i8 [[X]])
-; CHECK-NEXT: ret i8 [[M2]]
+; CHECK-NEXT: ret i8 [[X:%.*]]
;
%m = call i8 @llvm.umin.i8(i8 %y, i8 %x)
%m2 = call i8 @llvm.umax.i8(i8 %m, i8 %x)
@@ -518,9 +502,7 @@ define i8 @umin_umax_commute3(i8 %x, i8 %y) {
define i8 @smax_smin(i8 %x, i8 %y) {
; CHECK-LABEL: @smax_smin(
-; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.smax.i8(i8 [[X:%.*]], i8 [[Y:%.*]])
-; CHECK-NEXT: [[M2:%.*]] = call i8 @llvm.smin.i8(i8 [[X]], i8 [[M]])
-; CHECK-NEXT: ret i8 [[M2]]
+; CHECK-NEXT: ret i8 [[X:%.*]]
;
%m = call i8 @llvm.smax.i8(i8 %x, i8 %y)
%m2 = call i8 @llvm.smin.i8(i8 %x, i8 %m)
@@ -529,9 +511,7 @@ define i8 @smax_smin(i8 %x, i8 %y) {
define <2 x i8> @smax_smin_commute1(<2 x i8> %x, <2 x i8> %y) {
; CHECK-LABEL: @smax_smin_commute1(
-; CHECK-NEXT: [[M:%.*]] = call <2 x i8> @llvm.smax.v2i8(<2 x i8> [[Y:%.*]], <2 x i8> [[X:%.*]])
-; CHECK-NEXT: [[M2:%.*]] = call <2 x i8> @llvm.smin.v2i8(<2 x i8> [[X]], <2 x i8> [[M]])
-; CHECK-NEXT: ret <2 x i8> [[M2]]
+; CHECK-NEXT: ret <2 x i8> [[X:%.*]]
;
%m = call <2 x i8> @llvm.smax.v2i8(<2 x i8> %y, <2 x i8> %x)
%m2 = call <2 x i8> @llvm.smin.v2i8(<2 x i8> %x, <2 x i8> %m)
@@ -540,9 +520,7 @@ define <2 x i8> @smax_smin_commute1(<2 x i8> %x, <2 x i8> %y) {
define i8 @smax_smin_commute2(i8 %x, i8 %y) {
; CHECK-LABEL: @smax_smin_commute2(
-; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.smax.i8(i8 [[X:%.*]], i8 [[Y:%.*]])
-; CHECK-NEXT: [[M2:%.*]] = call i8 @llvm.smin.i8(i8 [[M]], i8 [[X]])
-; CHECK-NEXT: ret i8 [[M2]]
+; CHECK-NEXT: ret i8 [[X:%.*]]
;
%m = call i8 @llvm.smax.i8(i8 %x, i8 %y)
%m2 = call i8 @llvm.smin.i8(i8 %m, i8 %x)
@@ -551,9 +529,7 @@ define i8 @smax_smin_commute2(i8 %x, i8 %y) {
define i8 @smax_smin_commute3(i8 %x, i8 %y) {
; CHECK-LABEL: @smax_smin_commute3(
-; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.smax.i8(i8 [[Y:%.*]], i8 [[X:%.*]])
-; CHECK-NEXT: [[M2:%.*]] = call i8 @llvm.smin.i8(i8 [[M]], i8 [[X]])
-; CHECK-NEXT: ret i8 [[M2]]
+; CHECK-NEXT: ret i8 [[X:%.*]]
;
%m = call i8 @llvm.smax.i8(i8 %y, i8 %x)
%m2 = call i8 @llvm.smin.i8(i8 %m, i8 %x)
@@ -562,9 +538,7 @@ define i8 @smax_smin_commute3(i8 %x, i8 %y) {
define <2 x i8> @smin_smax(<2 x i8> %x, <2 x i8> %y) {
; CHECK-LABEL: @smin_smax(
-; CHECK-NEXT: [[M:%.*]] = call <2 x i8> @llvm.smin.v2i8(<2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]])
-; CHECK-NEXT: [[M2:%.*]] = call <2 x i8> @llvm.smax.v2i8(<2 x i8> [[X]], <2 x i8> [[M]])
-; CHECK-NEXT: ret <2 x i8> [[M2]]
+; CHECK-NEXT: ret <2 x i8> [[X:%.*]]
;
%m = call <2 x i8> @llvm.smin.v2i8(<2 x i8> %x, <2 x i8> %y)
%m2 = call <2 x i8> @llvm.smax.v2i8(<2 x i8> %x, <2 x i8> %m)
@@ -573,9 +547,7 @@ define <2 x i8> @smin_smax(<2 x i8> %x, <2 x i8> %y) {
define i8 @smin_smax_commute1(i8 %x, i8 %y) {
; CHECK-LABEL: @smin_smax_commute1(
-; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.smin.i8(i8 [[Y:%.*]], i8 [[X:%.*]])
-; CHECK-NEXT: [[M2:%.*]] = call i8 @llvm.smax.i8(i8 [[X]], i8 [[M]])
-; CHECK-NEXT: ret i8 [[M2]]
+; CHECK-NEXT: ret i8 [[X:%.*]]
;
%m = call i8 @llvm.smin.i8(i8 %y, i8 %x)
%m2 = call i8 @llvm.smax.i8(i8 %x, i8 %m)
@@ -584,9 +556,7 @@ define i8 @smin_smax_commute1(i8 %x, i8 %y) {
define i8 @smin_smax_commute2(i8 %x, i8 %y) {
; CHECK-LABEL: @smin_smax_commute2(
-; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.smin.i8(i8 [[X:%.*]], i8 [[Y:%.*]])
-; CHECK-NEXT: [[M2:%.*]] = call i8 @llvm.smax.i8(i8 [[M]], i8 [[X]])
-; CHECK-NEXT: ret i8 [[M2]]
+; CHECK-NEXT: ret i8 [[X:%.*]]
;
%m = call i8 @llvm.smin.i8(i8 %x, i8 %y)
%m2 = call i8 @llvm.smax.i8(i8 %m, i8 %x)
@@ -595,9 +565,7 @@ define i8 @smin_smax_commute2(i8 %x, i8 %y) {
define i8 @smin_smax_commute3(i8 %x, i8 %y) {
; CHECK-LABEL: @smin_smax_commute3(
-; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.smin.i8(i8 [[Y:%.*]], i8 [[X:%.*]])
-; CHECK-NEXT: [[M2:%.*]] = call i8 @llvm.smax.i8(i8 [[M]], i8 [[X]])
-; CHECK-NEXT: ret i8 [[M2]]
+; CHECK-NEXT: ret i8 [[X:%.*]]
;
%m = call i8 @llvm.smin.i8(i8 %y, i8 %x)
%m2 = call i8 @llvm.smax.i8(i8 %m, i8 %x)
More information about the llvm-commits
mailing list