[llvm] 73104b0 - [InstSimplify] Fold min/max based on dominating condition
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Sat Sep 5 07:18:02 PDT 2020
Author: Nikita Popov
Date: 2020-09-05T16:16:40+02:00
New Revision: 73104b0751a1c1dd499550bf44e47d29882fbb32
URL: https://github.com/llvm/llvm-project/commit/73104b0751a1c1dd499550bf44e47d29882fbb32
DIFF: https://github.com/llvm/llvm-project/commit/73104b0751a1c1dd499550bf44e47d29882fbb32.diff
LOG: [InstSimplify] Fold min/max based on dominating condition
If we have a dominating condition that x >= y, then umax(x, y) is x,
etc. I'm doing this in InstSimplify as the corresponding transform
for the select form is also done there.
Differential Revision: https://reviews.llvm.org/D87168
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 843f0608a963..3139b5a96b27 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -5326,6 +5326,13 @@ static Value *simplifyBinaryIntrinsic(Function *F, Value *Op0, Value *Op1,
if (isICmpTrue(Pred, Op1, Op0, Q.getWithoutUndef(), RecursionLimit))
return Op1;
+ if (Optional<bool> Imp =
+ isImpliedByDomCondition(Pred, Op0, Op1, Q.CxtI, Q.DL))
+ return *Imp ? Op0 : Op1;
+ if (Optional<bool> Imp =
+ isImpliedByDomCondition(Pred, Op1, Op0, Q.CxtI, Q.DL))
+ return *Imp ? Op1 : Op0;
+
break;
}
case Intrinsic::usub_with_overflow:
diff --git a/llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll b/llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll
index d1d711c1c1bd..d646334887b2 100644
--- a/llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll
+++ b/llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll
@@ -2139,11 +2139,9 @@ define i8 @umax_dom_cond_uge(i8 %x, i8 %y) {
; CHECK-NEXT: [[CMP:%.*]] = icmp uge i8 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: br i1 [[CMP]], label [[TRUE:%.*]], label [[FALSE:%.*]]
; CHECK: true:
-; CHECK-NEXT: [[M1:%.*]] = call i8 @llvm.umax.i8(i8 [[X]], i8 [[Y]])
-; CHECK-NEXT: ret i8 [[M1]]
+; CHECK-NEXT: ret i8 [[X]]
; CHECK: false:
-; CHECK-NEXT: [[M2:%.*]] = call i8 @llvm.umax.i8(i8 [[X]], i8 [[Y]])
-; CHECK-NEXT: ret i8 [[M2]]
+; CHECK-NEXT: ret i8 [[Y]]
;
%cmp = icmp uge i8 %x, %y
br i1 %cmp, label %true, label %false
@@ -2162,11 +2160,9 @@ define i8 @umax_dom_cond_ugt(i8 %x, i8 %y) {
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i8 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: br i1 [[CMP]], label [[TRUE:%.*]], label [[FALSE:%.*]]
; CHECK: true:
-; CHECK-NEXT: [[M1:%.*]] = call i8 @llvm.umax.i8(i8 [[X]], i8 [[Y]])
-; CHECK-NEXT: ret i8 [[M1]]
+; CHECK-NEXT: ret i8 [[X]]
; CHECK: false:
-; CHECK-NEXT: [[M2:%.*]] = call i8 @llvm.umax.i8(i8 [[X]], i8 [[Y]])
-; CHECK-NEXT: ret i8 [[M2]]
+; CHECK-NEXT: ret i8 [[Y]]
;
%cmp = icmp ugt i8 %x, %y
br i1 %cmp, label %true, label %false
@@ -2185,11 +2181,9 @@ define i8 @umax_dom_cond_ule(i8 %x, i8 %y) {
; CHECK-NEXT: [[CMP:%.*]] = icmp ule i8 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: br i1 [[CMP]], label [[TRUE:%.*]], label [[FALSE:%.*]]
; CHECK: true:
-; CHECK-NEXT: [[M1:%.*]] = call i8 @llvm.umax.i8(i8 [[X]], i8 [[Y]])
-; CHECK-NEXT: ret i8 [[M1]]
+; CHECK-NEXT: ret i8 [[Y]]
; CHECK: false:
-; CHECK-NEXT: [[M2:%.*]] = call i8 @llvm.umax.i8(i8 [[X]], i8 [[Y]])
-; CHECK-NEXT: ret i8 [[M2]]
+; CHECK-NEXT: ret i8 [[X]]
;
%cmp = icmp ule i8 %x, %y
br i1 %cmp, label %true, label %false
@@ -2208,11 +2202,9 @@ define i8 @umax_dom_cond_ult(i8 %x, i8 %y) {
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i8 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: br i1 [[CMP]], label [[TRUE:%.*]], label [[FALSE:%.*]]
; CHECK: true:
-; CHECK-NEXT: [[M1:%.*]] = call i8 @llvm.umax.i8(i8 [[X]], i8 [[Y]])
-; CHECK-NEXT: ret i8 [[M1]]
+; CHECK-NEXT: ret i8 [[Y]]
; CHECK: false:
-; CHECK-NEXT: [[M2:%.*]] = call i8 @llvm.umax.i8(i8 [[X]], i8 [[Y]])
-; CHECK-NEXT: ret i8 [[M2]]
+; CHECK-NEXT: ret i8 [[X]]
;
%cmp = icmp ult i8 %x, %y
br i1 %cmp, label %true, label %false
@@ -2231,11 +2223,9 @@ define i8 @umin_dom_cond_uge(i8 %x, i8 %y) {
; CHECK-NEXT: [[CMP:%.*]] = icmp uge i8 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: br i1 [[CMP]], label [[TRUE:%.*]], label [[FALSE:%.*]]
; CHECK: true:
-; CHECK-NEXT: [[M1:%.*]] = call i8 @llvm.umin.i8(i8 [[X]], i8 [[Y]])
-; CHECK-NEXT: ret i8 [[M1]]
+; CHECK-NEXT: ret i8 [[Y]]
; CHECK: false:
-; CHECK-NEXT: [[M2:%.*]] = call i8 @llvm.umin.i8(i8 [[X]], i8 [[Y]])
-; CHECK-NEXT: ret i8 [[M2]]
+; CHECK-NEXT: ret i8 [[X]]
;
%cmp = icmp uge i8 %x, %y
br i1 %cmp, label %true, label %false
@@ -2254,11 +2244,9 @@ define i8 @smax_dom_cond_sge(i8 %x, i8 %y) {
; CHECK-NEXT: [[CMP:%.*]] = icmp sge i8 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: br i1 [[CMP]], label [[TRUE:%.*]], label [[FALSE:%.*]]
; CHECK: true:
-; CHECK-NEXT: [[M1:%.*]] = call i8 @llvm.smax.i8(i8 [[X]], i8 [[Y]])
-; CHECK-NEXT: ret i8 [[M1]]
+; CHECK-NEXT: ret i8 [[X]]
; CHECK: false:
-; CHECK-NEXT: [[M2:%.*]] = call i8 @llvm.smax.i8(i8 [[X]], i8 [[Y]])
-; CHECK-NEXT: ret i8 [[M2]]
+; CHECK-NEXT: ret i8 [[Y]]
;
%cmp = icmp sge i8 %x, %y
br i1 %cmp, label %true, label %false
@@ -2277,11 +2265,9 @@ define i8 @smin_dom_cond_sge(i8 %x, i8 %y) {
; CHECK-NEXT: [[CMP:%.*]] = icmp sge i8 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: br i1 [[CMP]], label [[TRUE:%.*]], label [[FALSE:%.*]]
; CHECK: true:
-; CHECK-NEXT: [[M1:%.*]] = call i8 @llvm.smin.i8(i8 [[X]], i8 [[Y]])
-; CHECK-NEXT: ret i8 [[M1]]
+; CHECK-NEXT: ret i8 [[Y]]
; CHECK: false:
-; CHECK-NEXT: [[M2:%.*]] = call i8 @llvm.smin.i8(i8 [[X]], i8 [[Y]])
-; CHECK-NEXT: ret i8 [[M2]]
+; CHECK-NEXT: ret i8 [[X]]
;
%cmp = icmp sge i8 %x, %y
br i1 %cmp, label %true, label %false
More information about the llvm-commits
mailing list