@@ -47406,10 +47406,13 @@ static SDValue combineShiftRightArithmetic(SDNode *N, SelectionDAG &DAG,
4740647406 return DAG.getNode(X86ISD::VSRAV, DL, N->getVTList(), N0, ShrAmtVal);
4740747407 }
4740847408
47409- // fold (ashr (shl, a, [56,48,32,24,16]), SarConst)
47410- // into (shl, (sext (a), [56,48,32,24,16] - SarConst)) or
47411- // into (lshr, (sext (a), SarConst - [56,48,32,24,16]))
47412- // depending on sign of (SarConst - [56,48,32,24,16])
47409+ // fold (SRA (SHL X, ShlConst), SraConst)
47410+ // into (SHL (sext_in_reg X), ShlConst - SraConst)
47411+ // or (sext_in_reg X)
47412+ // or (SRA (sext_in_reg X), SraConst - ShlConst)
47413+ // depending on relation between SraConst and ShlConst.
47414+ // We only do this if (Size - ShlConst) is equal to 8, 16 or 32. That allows
47415+ // us to do the sext_in_reg from corresponding bit.
4741347416
4741447417 // sexts in X86 are MOVs. The MOVs have the same code size
4741547418 // as above SHIFTs (only SHIFT on 1 has lower code size).
@@ -47425,29 +47428,29 @@ static SDValue combineShiftRightArithmetic(SDNode *N, SelectionDAG &DAG,
4742547428 SDValue N00 = N0.getOperand(0);
4742647429 SDValue N01 = N0.getOperand(1);
4742747430 APInt ShlConst = N01->getAsAPIntVal();
47428- APInt SarConst = N1->getAsAPIntVal();
47431+ APInt SraConst = N1->getAsAPIntVal();
4742947432 EVT CVT = N1.getValueType();
4743047433
47431- if (SarConst.isNegative())
47434+ if (CVT != N01.getValueType())
47435+ return SDValue();
47436+ if (SraConst.isNegative())
4743247437 return SDValue();
4743347438
4743447439 for (MVT SVT : { MVT::i8, MVT::i16, MVT::i32 }) {
4743547440 unsigned ShiftSize = SVT.getSizeInBits();
47436- // skipping types without corresponding sext/zext and
47437- // ShlConst that is not one of [56,48,32,24,16]
47441+ // Only deal with (Size - ShlConst) being equal to 8, 16 or 32.
4743847442 if (ShiftSize >= Size || ShlConst != Size - ShiftSize)
4743947443 continue;
4744047444 SDLoc DL(N);
4744147445 SDValue NN =
4744247446 DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, N00, DAG.getValueType(SVT));
47443- SarConst = SarConst - (Size - ShiftSize);
47444- if (SarConst == 0)
47447+ if (SraConst.eq(ShlConst))
4744547448 return NN;
47446- if (SarConst.isNegative( ))
47449+ if (SraConst.ult(ShlConst ))
4744747450 return DAG.getNode(ISD::SHL, DL, VT, NN,
47448- DAG.getConstant(-SarConst , DL, CVT));
47451+ DAG.getConstant(ShlConst - SraConst , DL, CVT));
4744947452 return DAG.getNode(ISD::SRA, DL, VT, NN,
47450- DAG.getConstant(SarConst , DL, CVT));
47453+ DAG.getConstant(SraConst - ShlConst , DL, CVT));
4745147454 }
4745247455 return SDValue();
4745347456}
0 commit comments