From 1eb2ecc5c6f85e0975aaf8b3b82e262239614a92 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Fri, 4 Sep 2020 23:44:58 +0200 Subject: [PATCH] [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 --- lib/Analysis/InstructionSimplify.cpp | 7 ++++ .../InstSimplify/maxmin_intrinsics.ll | 42 +++++++------------ 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/lib/Analysis/InstructionSimplify.cpp b/lib/Analysis/InstructionSimplify.cpp index 843f0608a96..3139b5a96b2 100644 --- a/lib/Analysis/InstructionSimplify.cpp +++ b/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 Imp = + isImpliedByDomCondition(Pred, Op0, Op1, Q.CxtI, Q.DL)) + return *Imp ? Op0 : Op1; + if (Optional Imp = + isImpliedByDomCondition(Pred, Op1, Op0, Q.CxtI, Q.DL)) + return *Imp ? Op1 : Op0; + break; } case Intrinsic::usub_with_overflow: diff --git a/test/Transforms/InstSimplify/maxmin_intrinsics.ll b/test/Transforms/InstSimplify/maxmin_intrinsics.ll index d1d711c1c1b..d646334887b 100644 --- a/test/Transforms/InstSimplify/maxmin_intrinsics.ll +++ b/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