From ab2599111fc5deb70112e0ce8a153a51b516e429 Mon Sep 17 00:00:00 2001 From: Simon Pilgrim Date: Mon, 12 Oct 2020 15:10:16 +0100 Subject: [PATCH] [InstCombine] matchFunnelShift - canonicalize to OR(SHL,LSHR). NFCI. Simplify the shift amount matching code by canonicalizing the shift ops first. --- .../InstCombine/InstCombineAndOrXor.cpp | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp index 52b81ad2164..06168e2424e 100644 --- a/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ b/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp @@ -2067,13 +2067,19 @@ static Instruction *matchFunnelShift(Instruction &Or) { Value *ShVal0, *ShVal1, *ShAmt0, *ShAmt1; if (!match(Or0, m_OneUse(m_LogicalShift(m_Value(ShVal0), m_Value(ShAmt0)))) || - !match(Or1, m_OneUse(m_LogicalShift(m_Value(ShVal1), m_Value(ShAmt1))))) + !match(Or1, m_OneUse(m_LogicalShift(m_Value(ShVal1), m_Value(ShAmt1)))) || + Or0->getOpcode() == Or1->getOpcode()) return nullptr; - BinaryOperator::BinaryOps ShiftOpcode0 = Or0->getOpcode(); - BinaryOperator::BinaryOps ShiftOpcode1 = Or1->getOpcode(); - if (ShiftOpcode0 == ShiftOpcode1) - return nullptr; + // Canonicalize to or(shl(ShVal0, ShAmt0), lshr(ShVal1, ShAmt1)). + if (Or0->getOpcode() == BinaryOperator::LShr) { + std::swap(Or0, Or1); + std::swap(ShVal0, ShVal1); + std::swap(ShAmt0, ShAmt1); + } + assert(Or0->getOpcode() == BinaryOperator::Shl && + Or1->getOpcode() == BinaryOperator::LShr && + "Illegal or(shift,shift) pair"); // Match the shift amount operands for a funnel shift pattern. This always // matches a subtraction on the R operand. @@ -2124,16 +2130,14 @@ static Instruction *matchFunnelShift(Instruction &Or) { }; Value *ShAmt = matchShiftAmount(ShAmt0, ShAmt1, Width); - bool SubIsOnLHS = false; + bool IsFshl = true; // Sub on LSHR. if (!ShAmt) { ShAmt = matchShiftAmount(ShAmt1, ShAmt0, Width); - SubIsOnLHS = true; + IsFshl = false; // Sub on SHL. } if (!ShAmt) return nullptr; - bool IsFshl = (!SubIsOnLHS && ShiftOpcode0 == BinaryOperator::Shl) || - (SubIsOnLHS && ShiftOpcode1 == BinaryOperator::Shl); Intrinsic::ID IID = IsFshl ? Intrinsic::fshl : Intrinsic::fshr; Function *F = Intrinsic::getDeclaration(Or.getModule(), IID, Or.getType()); return IntrinsicInst::Create(