Skip to content

Commit 36d9bba

Browse files
author
git apple-llvm automerger
committed
Merge commit '89657b3a3b57' from llvm.org/master into apple/main
2 parents 04c6d18 + 89657b3 commit 36d9bba

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -531,19 +531,25 @@ Instruction *InstCombinerImpl::narrowRotate(TruncInst &Trunc) {
531531

532532
// First, find an or'd pair of opposite shifts with the same shifted operand:
533533
// trunc (or (lshr ShVal, ShAmt0), (shl ShVal, ShAmt1))
534-
Value *Or0, *Or1;
535-
if (!match(Trunc.getOperand(0), m_OneUse(m_Or(m_Value(Or0), m_Value(Or1)))))
534+
BinaryOperator *Or0, *Or1;
535+
if (!match(Trunc.getOperand(0), m_OneUse(m_Or(m_BinOp(Or0), m_BinOp(Or1)))))
536536
return nullptr;
537537

538538
Value *ShVal, *ShAmt0, *ShAmt1;
539539
if (!match(Or0, m_OneUse(m_LogicalShift(m_Value(ShVal), m_Value(ShAmt0)))) ||
540-
!match(Or1, m_OneUse(m_LogicalShift(m_Specific(ShVal), m_Value(ShAmt1)))))
540+
!match(Or1,
541+
m_OneUse(m_LogicalShift(m_Specific(ShVal), m_Value(ShAmt1)))) ||
542+
Or0->getOpcode() == Or1->getOpcode())
541543
return nullptr;
542544

543-
auto ShiftOpcode0 = cast<BinaryOperator>(Or0)->getOpcode();
544-
auto ShiftOpcode1 = cast<BinaryOperator>(Or1)->getOpcode();
545-
if (ShiftOpcode0 == ShiftOpcode1)
546-
return nullptr;
545+
// Canonicalize to or(shl(ShVal, ShAmt0), lshr(ShVal, ShAmt1)).
546+
if (Or0->getOpcode() == BinaryOperator::LShr) {
547+
std::swap(Or0, Or1);
548+
std::swap(ShAmt0, ShAmt1);
549+
}
550+
assert(Or0->getOpcode() == BinaryOperator::Shl &&
551+
Or1->getOpcode() == BinaryOperator::LShr &&
552+
"Illegal or(shift,shift) pair");
547553

548554
// Match the shift amount operands for a rotate pattern. This always matches
549555
// a subtraction on the R operand.
@@ -570,10 +576,10 @@ Instruction *InstCombinerImpl::narrowRotate(TruncInst &Trunc) {
570576
};
571577

572578
Value *ShAmt = matchShiftAmount(ShAmt0, ShAmt1, NarrowWidth);
573-
bool SubIsOnLHS = false;
579+
bool IsFshl = true; // Sub on LSHR.
574580
if (!ShAmt) {
575581
ShAmt = matchShiftAmount(ShAmt1, ShAmt0, NarrowWidth);
576-
SubIsOnLHS = true;
582+
IsFshl = false; // Sub on SHL.
577583
}
578584
if (!ShAmt)
579585
return nullptr;
@@ -591,8 +597,6 @@ Instruction *InstCombinerImpl::narrowRotate(TruncInst &Trunc) {
591597
// llvm.fshl.i8(trunc(ShVal), trunc(ShVal), trunc(ShAmt))
592598
Value *NarrowShAmt = Builder.CreateTrunc(ShAmt, DestTy);
593599
Value *X = Builder.CreateTrunc(ShVal, DestTy);
594-
bool IsFshl = (!SubIsOnLHS && ShiftOpcode0 == BinaryOperator::Shl) ||
595-
(SubIsOnLHS && ShiftOpcode1 == BinaryOperator::Shl);
596600
Intrinsic::ID IID = IsFshl ? Intrinsic::fshl : Intrinsic::fshr;
597601
Function *F = Intrinsic::getDeclaration(Trunc.getModule(), IID, DestTy);
598602
return IntrinsicInst::Create(F, { X, X, NarrowShAmt });

0 commit comments

Comments
 (0)