From: Sanjay Patel Date: Tue, 7 May 2019 18:58:07 +0000 (+0000) Subject: [InstCombine] allow sinking fneg operands through an FP min/max X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d568215bb216ea6522168629689f5ad4b0cb6138;p=llvm [InstCombine] allow sinking fneg operands through an FP min/max Fundamentally/generally, we should not have to rely on bailouts/crippling of folds. In this particular case, I think we always recognize the inverted predicate min/max pattern, so there should not be any loss of optimization. Codegen looks better because we are eliminating an fneg. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@360180 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/InstCombine/InstCombineCompares.cpp b/lib/Transforms/InstCombine/InstCombineCompares.cpp index ef72f3bbb72..ce6b38c90ca 100644 --- a/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -5518,6 +5518,11 @@ Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) { } } + // fcmp pred (fneg X), (fneg Y) -> fcmp swap(pred) X, Y + Value *X, *Y; + if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_FNeg(m_Value(Y)))) + return new FCmpInst(I.getSwappedPredicate(), X, Y, "", &I); + // Test if the FCmpInst instruction is used exclusively by a select as // part of a minimum or maximum operation. If so, refrain from doing // any other folding. This helps out other analyses which understand @@ -5576,12 +5581,7 @@ Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) { if (Instruction *R = foldFabsWithFcmpZero(I)) return R; - Value *X, *Y; if (match(Op0, m_FNeg(m_Value(X)))) { - // fcmp pred (fneg X), (fneg Y) -> fcmp swap(pred) X, Y - if (match(Op1, m_FNeg(m_Value(Y)))) - return new FCmpInst(I.getSwappedPredicate(), X, Y, "", &I); - // fcmp pred (fneg X), C --> fcmp swap(pred) X, -C Constant *C; if (match(Op1, m_Constant(C))) { diff --git a/test/Transforms/InstCombine/minmax-fp.ll b/test/Transforms/InstCombine/minmax-fp.ll index 9cdbf4f2d80..e139137727c 100644 --- a/test/Transforms/InstCombine/minmax-fp.ll +++ b/test/Transforms/InstCombine/minmax-fp.ll @@ -257,10 +257,9 @@ define double @t17(i32 %x) { define float @fneg_fmax(float %x, float %y) { ; CHECK-LABEL: @fneg_fmax( -; CHECK-NEXT: [[N1:%.*]] = fneg float [[X:%.*]] -; CHECK-NEXT: [[N2:%.*]] = fneg float [[Y:%.*]] -; CHECK-NEXT: [[COND:%.*]] = fcmp nnan ogt float [[N1]], [[N2]] -; CHECK-NEXT: [[MAX:%.*]] = select i1 [[COND]], float [[N1]], float [[N2]] +; CHECK-NEXT: [[COND:%.*]] = fcmp nnan olt float [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: [[MAX_V:%.*]] = select i1 [[COND]], float [[X]], float [[Y]] +; CHECK-NEXT: [[MAX:%.*]] = fneg float [[MAX_V]] ; CHECK-NEXT: ret float [[MAX]] ; %n1 = fneg float %x @@ -272,10 +271,9 @@ define float @fneg_fmax(float %x, float %y) { define <2 x float> @fsub_fmax(<2 x float> %x, <2 x float> %y) { ; CHECK-LABEL: @fsub_fmax( -; CHECK-NEXT: [[N1:%.*]] = fsub <2 x float> , [[X:%.*]] -; CHECK-NEXT: [[N2:%.*]] = fsub <2 x float> , [[Y:%.*]] -; CHECK-NEXT: [[COND_INV:%.*]] = fcmp nnan nsz olt <2 x float> [[N1]], [[N2]] -; CHECK-NEXT: [[MAX:%.*]] = select <2 x i1> [[COND_INV]], <2 x float> [[N2]], <2 x float> [[N1]] +; CHECK-NEXT: [[COND_INV:%.*]] = fcmp nnan nsz ogt <2 x float> [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: [[MAX_V:%.*]] = select <2 x i1> [[COND_INV]], <2 x float> [[Y]], <2 x float> [[X]] +; CHECK-NEXT: [[MAX:%.*]] = fsub <2 x float> , [[MAX_V]] ; CHECK-NEXT: ret <2 x float> [[MAX]] ; %n1 = fsub <2 x float> , %x @@ -287,10 +285,9 @@ define <2 x float> @fsub_fmax(<2 x float> %x, <2 x float> %y) { define <2 x double> @fsub_fmin(<2 x double> %x, <2 x double> %y) { ; CHECK-LABEL: @fsub_fmin( -; CHECK-NEXT: [[N1:%.*]] = fsub <2 x double> , [[X:%.*]] -; CHECK-NEXT: [[N2:%.*]] = fsub <2 x double> , [[Y:%.*]] -; CHECK-NEXT: [[COND:%.*]] = fcmp nnan olt <2 x double> [[N1]], [[N2]] -; CHECK-NEXT: [[MAX:%.*]] = select <2 x i1> [[COND]], <2 x double> [[N1]], <2 x double> [[N2]] +; CHECK-NEXT: [[COND:%.*]] = fcmp nnan ogt <2 x double> [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: [[MAX_V:%.*]] = select <2 x i1> [[COND]], <2 x double> [[X]], <2 x double> [[Y]] +; CHECK-NEXT: [[MAX:%.*]] = fsub <2 x double> , [[MAX_V]] ; CHECK-NEXT: ret <2 x double> [[MAX]] ; %n1 = fsub <2 x double> , %x @@ -302,10 +299,9 @@ define <2 x double> @fsub_fmin(<2 x double> %x, <2 x double> %y) { define double @fneg_fmin(double %x, double %y) { ; CHECK-LABEL: @fneg_fmin( -; CHECK-NEXT: [[N1:%.*]] = fneg double [[X:%.*]] -; CHECK-NEXT: [[N2:%.*]] = fneg double [[Y:%.*]] -; CHECK-NEXT: [[COND_INV:%.*]] = fcmp nnan nsz ogt double [[N1]], [[N2]] -; CHECK-NEXT: [[MAX:%.*]] = select i1 [[COND_INV]], double [[N2]], double [[N1]] +; CHECK-NEXT: [[COND_INV:%.*]] = fcmp nnan nsz olt double [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: [[MAX_V:%.*]] = select i1 [[COND_INV]], double [[Y]], double [[X]] +; CHECK-NEXT: [[MAX:%.*]] = fneg double [[MAX_V]] ; CHECK-NEXT: ret double [[MAX]] ; %n1 = fneg double %x