@@ -52131,11 +52131,12 @@ static SDValue combineSignExtendInReg(SDNode *N, SelectionDAG &DAG,
5213152131 return SDValue();
5213252132}
5213352133
52134- /// sext(add_nsw(x, C)) --> add(sext(x), C_sext)
52135- /// zext(add_nuw(x, C)) --> add(zext(x), C_zext)
52136- /// Promoting a sign/zero extension ahead of a no overflow 'add' exposes
52137- /// opportunities to combine math ops, use an LEA, or use a complex addressing
52138- /// mode. This can eliminate extend, add, and shift instructions.
52134+ /// sext(add_nsw(x, C)) --> add_nsw(sext(x), C_sext)
52135+ /// zext(add_nuw(x, C)) --> add_nuw(zext(x), C_zext)
52136+ /// zext(addlike(x, C)) --> add(zext(x), C_zext)
52137+ /// Promoting a sign/zero extension ahead of a no overflow 'add' or 'addlike'
52138+ /// exposes opportunities to combine math ops, use an LEA, or use a complex
52139+ /// addressing mode. This can eliminate extend, add, and shift instructions.
5213952140static SDValue promoteExtBeforeAdd(SDNode *Ext, SelectionDAG &DAG,
5214052141 const X86Subtarget &Subtarget) {
5214152142 if (Ext->getOpcode() != ISD::SIGN_EXTEND &&
@@ -52147,17 +52148,19 @@ static SDValue promoteExtBeforeAdd(SDNode *Ext, SelectionDAG &DAG,
5214752148 if (VT != MVT::i64)
5214852149 return SDValue();
5214952150
52150- SDValue Add = Ext->getOperand(0);
52151- if (Add.getOpcode() != ISD::ADD)
52152- return SDValue();
52153-
52151+ bool NSW = false, NUW = false;
5215452152 bool Sext = Ext->getOpcode() == ISD::SIGN_EXTEND;
52155- bool NSW = Add->getFlags().hasNoSignedWrap();
52156- bool NUW = Add->getFlags().hasNoUnsignedWrap();
5215752153
52158- // We need an 'add nsw' feeding into the 'sext' or 'add nuw' feeding
52159- // into the 'zext'
52160- if ((Sext && !NSW) || (!Sext && !NUW))
52154+ SDValue Add = Ext->getOperand(0);
52155+ unsigned AddOpc = Add->getOpcode();
52156+ if (AddOpc == ISD::ADD) {
52157+ NSW = Add->getFlags().hasNoSignedWrap();
52158+ NUW = Add->getFlags().hasNoUnsignedWrap();
52159+ // We need an 'add nsw' feeding into the 'sext' or 'add nuw' feeding
52160+ // into the 'zext'
52161+ if ((Sext && !NSW) || (!Sext && !NUW))
52162+ return SDValue();
52163+ } else if (!(!Sext && DAG.isADDLike(Add)))
5216152164 return SDValue();
5216252165
5216352166 // Having a constant operand to the 'add' ensures that we are not increasing
@@ -52193,7 +52196,7 @@ static SDValue promoteExtBeforeAdd(SDNode *Ext, SelectionDAG &DAG,
5219352196 SDNodeFlags Flags;
5219452197 Flags.setNoSignedWrap(NSW);
5219552198 Flags.setNoUnsignedWrap(NUW);
52196- return DAG.getNode(ISD::ADD , SDLoc(Add), VT, NewExt, NewConstant, Flags);
52199+ return DAG.getNode(AddOpc , SDLoc(Add), VT, NewExt, NewConstant, Flags);
5219752200}
5219852201
5219952202// If we face {ANY,SIGN,ZERO}_EXTEND that is applied to a CMOV with constant
0 commit comments