@@ -4386,34 +4386,50 @@ void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
43864386 // it.
43874387 IdxN = DAG.getSExtOrTrunc (IdxN, dl, N.getValueType ());
43884388
4389+ SDNodeFlags ScaleFlags;
4390+ // The multiplication of an index by the type size does not wrap the
4391+ // pointer index type in a signed sense (mul nsw).
4392+ ScaleFlags.setNoSignedWrap (NW.hasNoUnsignedSignedWrap ());
4393+
4394+ // The multiplication of an index by the type size does not wrap the
4395+ // pointer index type in an unsigned sense (mul nuw).
4396+ ScaleFlags.setNoUnsignedWrap (NW.hasNoUnsignedWrap ());
4397+
43894398 if (ElementScalable) {
43904399 EVT VScaleTy = N.getValueType ().getScalarType ();
43914400 SDValue VScale = DAG.getNode (
43924401 ISD::VSCALE, dl, VScaleTy,
43934402 DAG.getConstant (ElementMul.getZExtValue (), dl, VScaleTy));
43944403 if (IsVectorGEP)
43954404 VScale = DAG.getSplatVector (N.getValueType (), dl, VScale);
4396- IdxN = DAG.getNode (ISD::MUL, dl, N.getValueType (), IdxN, VScale);
4405+ IdxN = DAG.getNode (ISD::MUL, dl, N.getValueType (), IdxN, VScale,
4406+ ScaleFlags);
43974407 } else {
43984408 // If this is a multiply by a power of two, turn it into a shl
43994409 // immediately. This is a very common case.
44004410 if (ElementMul != 1 ) {
44014411 if (ElementMul.isPowerOf2 ()) {
44024412 unsigned Amt = ElementMul.logBase2 ();
4403- IdxN = DAG.getNode (ISD::SHL, dl,
4404- N. getValueType (), IdxN,
4405- DAG. getConstant (Amt, dl, IdxN. getValueType ()) );
4413+ IdxN = DAG.getNode (ISD::SHL, dl, N. getValueType (), IdxN,
4414+ DAG. getConstant (Amt, dl, IdxN. getValueType ()) ,
4415+ ScaleFlags );
44064416 } else {
44074417 SDValue Scale = DAG.getConstant (ElementMul.getZExtValue (), dl,
44084418 IdxN.getValueType ());
4409- IdxN = DAG.getNode (ISD::MUL, dl,
4410- N. getValueType (), IdxN, Scale );
4419+ IdxN = DAG.getNode (ISD::MUL, dl, N. getValueType (), IdxN, Scale,
4420+ ScaleFlags );
44114421 }
44124422 }
44134423 }
44144424
4415- N = DAG.getNode (ISD::ADD, dl,
4416- N.getValueType (), N, IdxN);
4425+ // The successive addition of the current address, truncated to the
4426+ // pointer index type and interpreted as an unsigned number, and each
4427+ // offset, also interpreted as an unsigned number, does not wrap the
4428+ // pointer index type (add nuw).
4429+ SDNodeFlags AddFlags;
4430+ AddFlags.setNoUnsignedWrap (NW.hasNoUnsignedWrap ());
4431+
4432+ N = DAG.getNode (ISD::ADD, dl, N.getValueType (), N, IdxN, AddFlags);
44174433 }
44184434 }
44194435
0 commit comments