@@ -531,19 +531,25 @@ Instruction *InstCombinerImpl::narrowRotate(TruncInst &Trunc) {
531
531
532
532
// First, find an or'd pair of opposite shifts with the same shifted operand:
533
533
// 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)))))
536
536
return nullptr ;
537
537
538
538
Value *ShVal, *ShAmt0, *ShAmt1;
539
539
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 ())
541
543
return nullptr ;
542
544
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" );
547
553
548
554
// Match the shift amount operands for a rotate pattern. This always matches
549
555
// a subtraction on the R operand.
@@ -570,10 +576,10 @@ Instruction *InstCombinerImpl::narrowRotate(TruncInst &Trunc) {
570
576
};
571
577
572
578
Value *ShAmt = matchShiftAmount (ShAmt0, ShAmt1, NarrowWidth);
573
- bool SubIsOnLHS = false ;
579
+ bool IsFshl = true ; // Sub on LSHR.
574
580
if (!ShAmt) {
575
581
ShAmt = matchShiftAmount (ShAmt1, ShAmt0, NarrowWidth);
576
- SubIsOnLHS = true ;
582
+ IsFshl = false ; // Sub on SHL.
577
583
}
578
584
if (!ShAmt)
579
585
return nullptr ;
@@ -591,8 +597,6 @@ Instruction *InstCombinerImpl::narrowRotate(TruncInst &Trunc) {
591
597
// llvm.fshl.i8(trunc(ShVal), trunc(ShVal), trunc(ShAmt))
592
598
Value *NarrowShAmt = Builder.CreateTrunc (ShAmt, DestTy);
593
599
Value *X = Builder.CreateTrunc (ShVal, DestTy);
594
- bool IsFshl = (!SubIsOnLHS && ShiftOpcode0 == BinaryOperator::Shl) ||
595
- (SubIsOnLHS && ShiftOpcode1 == BinaryOperator::Shl);
596
600
Intrinsic::ID IID = IsFshl ? Intrinsic::fshl : Intrinsic::fshr;
597
601
Function *F = Intrinsic::getDeclaration (Trunc.getModule (), IID, DestTy);
598
602
return IntrinsicInst::Create (F, { X, X, NarrowShAmt });
0 commit comments