@@ -15107,7 +15107,15 @@ SDValue SITargetLowering::performPtrAddCombine(SDNode *N,
1510715107 // Adapted from DAGCombiner::visitADDLikeCommutative.
1510815108 SDValue V, K;
1510915109 if (sd_match(N1, m_Shl(m_Neg(m_Value(V)), m_Value(K)))) {
15110- SDValue Inner = DAG.getNode(ISD::SHL, DL, VT, V, K);
15110+ SDNodeFlags ShlFlags = N1->getFlags();
15111+ // If the original shl is NUW and NSW, the first k+1 bits of 0-v are all 0,
15112+ // so v is either 0 or the first k+1 bits of v are all 1 -> NSW can be
15113+ // preserved.
15114+ SDNodeFlags NewShlFlags =
15115+ ShlFlags.hasNoUnsignedWrap() && ShlFlags.hasNoSignedWrap()
15116+ ? SDNodeFlags::NoSignedWrap
15117+ : SDNodeFlags();
15118+ SDValue Inner = DAG.getNode(ISD::SHL, DL, VT, V, K, NewShlFlags);
1511115119 DCI.AddToWorklist(Inner.getNode());
1511215120 return DAG.getNode(ISD::SUB, DL, VT, N0, Inner);
1511315121 }
@@ -15136,8 +15144,7 @@ SDValue SITargetLowering::performPtrAddCombine(SDNode *N,
1513615144 SDValue GAValue = N0.getOperand(0);
1513715145 if (const GlobalAddressSDNode *GA =
1513815146 dyn_cast<GlobalAddressSDNode>(GAValue)) {
15139- const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15140- if (DCI.isBeforeLegalizeOps() && TLI.isOffsetFoldingLegal(GA)) {
15147+ if (DCI.isBeforeLegalizeOps() && isOffsetFoldingLegal(GA)) {
1514115148 // If both additions in the original were NUW, reassociation preserves
1514215149 // that.
1514315150 SDNodeFlags Flags =
0 commit comments