Skip to content

Commit 21100f8

Browse files
committed
[InstCombine] FoldShiftByConstant - use PatternMatch for logicalshift(trunc(shift(x,c1)),c2) fold. NFCI.
1 parent 0b402e9 commit 21100f8

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -683,25 +683,27 @@ Instruction *InstCombinerImpl::FoldShiftByConstant(Value *Op0, Constant *Op1,
683683
return FoldedShift;
684684

685685
// Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2))
686-
if (TruncInst *TI = dyn_cast<TruncInst>(Op0)) {
687-
Instruction *TrOp = dyn_cast<Instruction>(TI->getOperand(0));
686+
if (auto *TI = dyn_cast<TruncInst>(Op0)) {
688687
// If 'shift2' is an ashr, we would have to get the sign bit into a funny
689688
// place. Don't try to do this transformation in this case. Also, we
690689
// require that the input operand is a shift-by-constant so that we have
691690
// confidence that the shifts will get folded together. We could do this
692691
// xform in more cases, but it is unlikely to be profitable.
693-
if (TrOp && I.isLogicalShift() && TrOp->isShift() &&
694-
isa<ConstantInt>(TrOp->getOperand(1))) {
692+
if (I.isLogicalShift() &&
693+
match(TI->getOperand(0), m_Shift(m_Value(), m_ConstantInt()))) {
694+
auto *TrOp = cast<Instruction>(TI->getOperand(0));
695+
Type *SrcTy = TrOp->getType();
696+
695697
// Okay, we'll do this xform. Make the shift of shift.
696-
Constant *ShAmt = ConstantExpr::getZExt(Op1, TrOp->getType());
698+
Constant *ShAmt = ConstantExpr::getZExt(Op1, SrcTy);
697699
// (shift2 (shift1 & 0x00FF), c2)
698700
Value *NSh = Builder.CreateBinOp(I.getOpcode(), TrOp, ShAmt, I.getName());
699701

700702
// For logical shifts, the truncation has the effect of making the high
701703
// part of the register be zeros. Emulate this by inserting an AND to
702704
// clear the top bits as needed. This 'and' will usually be zapped by
703705
// other xforms later if dead.
704-
unsigned SrcSize = TrOp->getType()->getScalarSizeInBits();
706+
unsigned SrcSize = SrcTy->getScalarSizeInBits();
705707
unsigned DstSize = TI->getType()->getScalarSizeInBits();
706708
APInt MaskV(APInt::getLowBitsSet(SrcSize, DstSize));
707709

0 commit comments

Comments
 (0)