From: Sanjay Patel Date: Fri, 2 Feb 2018 19:08:12 +0000 (+0000) Subject: [InstCombine] simplify logic for swapMayExposeCSEOpportunities; NFCI X-Git-Tag: android-x86-7.1-r4~5494 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=de02ec6d5b5f509066aee6a6df0f89489b22fe5a;p=android-x86%2Fexternal-llvm.git [InstCombine] simplify logic for swapMayExposeCSEOpportunities; NFCI git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@324122 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/InstCombine/InstCombineCompares.cpp b/lib/Transforms/InstCombine/InstCombineCompares.cpp index 31e8dd23639..f90bb08b045 100644 --- a/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -3897,31 +3897,17 @@ static bool swapMayExposeCSEOpportunities(const Value *Op0, const Value *Op1) { // FIXME: we may want to go through inttoptrs or bitcasts. if (Op0->getType()->isPointerTy()) return false; - // Count all uses of both Op0 and Op1 in a subtract. - // Each time Op0 is the first operand, count -1: swapping is bad, the - // subtract has already the same layout as the compare. - // Each time Op0 is the second operand, count +1: swapping is good, the - // subtract has a different layout as the compare. - // At the end, if the benefit is greater than 0, Op0 should come second to - // expose more CSE opportunities. - int GlobalSwapBenefits = 0; + // If a subtract already has the same operands as a compare, swapping would be + // bad. If a subtract has the same operands as a compare but in reverse order, + // then swapping is good. + int GoodToSwap = 0; for (const User *U : Op0->users()) { - const BinaryOperator *BinOp = dyn_cast(U); - if (!BinOp || BinOp->getOpcode() != Instruction::Sub) - continue; - // If Op0 is the first argument, this is not beneficial to swap the - // arguments. - int LocalSwapBenefits = -1; - unsigned Op1Idx = 1; - if (BinOp->getOperand(Op1Idx) == Op0) { - Op1Idx = 0; - LocalSwapBenefits = 1; - } - if (BinOp->getOperand(Op1Idx) != Op1) - continue; - GlobalSwapBenefits += LocalSwapBenefits; + if (match(U, m_Sub(m_Specific(Op1), m_Specific(Op0)))) + GoodToSwap++; + else if (match(U, m_Sub(m_Specific(Op0), m_Specific(Op1)))) + GoodToSwap--; } - return GlobalSwapBenefits > 0; + return GoodToSwap > 0; } /// \brief Check that one use is in the same block as the definition and all