diff options
author | Craig Topper <craig.topper@intel.com> | 2018-09-07 16:19:50 +0000 |
---|---|---|
committer | Craig Topper <craig.topper@intel.com> | 2018-09-07 16:19:50 +0000 |
commit | 040c2b0acf1ddf4330faadcc160ff9d6f9103ee8 (patch) | |
tree | e16085e2c1b37f3f6ed1e43ad8654ca36536b499 /llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp | |
parent | 110df11a1af095482a04ae876eae78bcd0d26b01 (diff) | |
download | bcm5719-llvm-040c2b0acf1ddf4330faadcc160ff9d6f9103ee8.tar.gz bcm5719-llvm-040c2b0acf1ddf4330faadcc160ff9d6f9103ee8.zip |
[InstCombine] Fold (min/max ~X, Y) -> ~(max/min X, ~Y) when Y is freely invertible
If the ~X wasn't able to simplify above the max/min, we might be able to simplify it by moving it below the max/min.
I had to modify the ~(min/max ~X, Y) transform to prevent getting stuck in a loop when we saw the new ~(max/min X, ~Y) before the ~Y had been folded away to remove the new not.
Differential Revision: https://reviews.llvm.org/D51398
llvm-svn: 341674
Diffstat (limited to 'llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp')
-rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp | 43 |
1 files changed, 35 insertions, 8 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp index 622b6eace5d..e710c486706 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -1827,15 +1827,42 @@ Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { } // MAX(~a, ~b) -> ~MIN(a, b) + // MAX(~a, C) -> ~MIN(a, ~C) // MIN(~a, ~b) -> ~MAX(a, b) - Value *A, *B; - if (match(LHS, m_Not(m_Value(A))) && match(RHS, m_Not(m_Value(B))) && - (!LHS->hasNUsesOrMore(3) || !RHS->hasNUsesOrMore(3))) { - CmpInst::Predicate InvertedPred = getInverseMinMaxPred(SPF); - Value *InvertedCmp = Builder.CreateICmp(InvertedPred, A, B); - Value *NewSel = Builder.CreateSelect(InvertedCmp, A, B); - return BinaryOperator::CreateNot(NewSel); - } + // MIN(~a, C) -> ~MAX(a, ~C) + auto moveNotAfterMinMax = [&](Value *X, Value *Y, + bool Swapped) -> Instruction * { + Value *A; + if (match(X, m_Not(m_Value(A))) && !X->hasNUsesOrMore(3) && + // Passing false to only consider m_Not and constants. + IsFreeToInvert(Y, false)) { + Value *B = Builder.CreateNot(Y); + Value *NewMinMax = createMinMax(Builder, getInverseMinMaxFlavor(SPF), + A, B); + // Copy the profile metadata. + if (MDNode *MD = SI.getMetadata(LLVMContext::MD_prof)) { + cast<SelectInst>(NewMinMax)->setMetadata(LLVMContext::MD_prof, MD); + // Swap the metadata if the operands are swapped. + if (Swapped) { + assert(X == SI.getFalseValue() && Y == SI.getTrueValue() && + "Unexpected operands."); + cast<SelectInst>(NewMinMax)->swapProfMetadata(); + } else { + assert(X == SI.getTrueValue() && Y == SI.getFalseValue() && + "Unexpected operands."); + } + } + + return BinaryOperator::CreateNot(NewMinMax); + } + + return nullptr; + }; + + if (Instruction *I = moveNotAfterMinMax(LHS, RHS, /*Swapped*/false)) + return I; + if (Instruction *I = moveNotAfterMinMax(RHS, LHS, /*Swapped*/true)) + return I; if (Instruction *I = factorizeMinMaxTree(SPF, LHS, RHS, Builder)) return I; |