@@ -52207,9 +52207,13 @@ static SDValue promoteExtBeforeAdd(SDNode *Ext, SelectionDAG &DAG,
52207
52207
if (Add.getOpcode() != ISD::ADD)
52208
52208
return SDValue();
52209
52209
52210
+ SDValue AddOp0 = Add.getOperand(0);
52211
+ SDValue AddOp1 = Add.getOperand(1);
52210
52212
bool Sext = Ext->getOpcode() == ISD::SIGN_EXTEND;
52211
52213
bool NSW = Add->getFlags().hasNoSignedWrap();
52212
52214
bool NUW = Add->getFlags().hasNoUnsignedWrap();
52215
+ NSW = NSW || (Sext && DAG.willNotOverflowAdd(true, AddOp0, AddOp1));
52216
+ NUW = NUW || (!Sext && DAG.willNotOverflowAdd(false, AddOp0, AddOp1));
52213
52217
52214
52218
// We need an 'add nsw' feeding into the 'sext' or 'add nuw' feeding
52215
52219
// into the 'zext'
@@ -52219,8 +52223,8 @@ static SDValue promoteExtBeforeAdd(SDNode *Ext, SelectionDAG &DAG,
52219
52223
// Having a constant operand to the 'add' ensures that we are not increasing
52220
52224
// the instruction count because the constant is extended for free below.
52221
52225
// A constant operand can also become the displacement field of an LEA.
52222
- auto *AddOp1 = dyn_cast<ConstantSDNode>(Add.getOperand(1) );
52223
- if (!AddOp1 )
52226
+ auto *AddOp1C = dyn_cast<ConstantSDNode>(AddOp1 );
52227
+ if (!AddOp1C )
52224
52228
return SDValue();
52225
52229
52226
52230
// Don't make the 'add' bigger if there's no hope of combining it with some
@@ -52239,10 +52243,9 @@ static SDValue promoteExtBeforeAdd(SDNode *Ext, SelectionDAG &DAG,
52239
52243
return SDValue();
52240
52244
52241
52245
// Everything looks good, so pull the '{s|z}ext' ahead of the 'add'.
52242
- int64_t AddConstant = Sext ? AddOp1->getSExtValue() : AddOp1->getZExtValue();
52243
- SDValue AddOp0 = Add.getOperand(0);
52246
+ int64_t AddC = Sext ? AddOp1C->getSExtValue() : AddOp1C->getZExtValue();
52244
52247
SDValue NewExt = DAG.getNode(Ext->getOpcode(), SDLoc(Ext), VT, AddOp0);
52245
- SDValue NewConstant = DAG.getConstant(AddConstant , SDLoc(Add), VT);
52248
+ SDValue NewConstant = DAG.getConstant(AddC , SDLoc(Add), VT);
52246
52249
52247
52250
// The wider add is guaranteed to not wrap because both operands are
52248
52251
// sign-extended.
0 commit comments