@@ -4386,34 +4386,50 @@ void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
4386
4386
// it.
4387
4387
IdxN = DAG.getSExtOrTrunc (IdxN, dl, N.getValueType ());
4388
4388
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
+
4389
4398
if (ElementScalable) {
4390
4399
EVT VScaleTy = N.getValueType ().getScalarType ();
4391
4400
SDValue VScale = DAG.getNode (
4392
4401
ISD::VSCALE, dl, VScaleTy,
4393
4402
DAG.getConstant (ElementMul.getZExtValue (), dl, VScaleTy));
4394
4403
if (IsVectorGEP)
4395
4404
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);
4397
4407
} else {
4398
4408
// If this is a multiply by a power of two, turn it into a shl
4399
4409
// immediately. This is a very common case.
4400
4410
if (ElementMul != 1 ) {
4401
4411
if (ElementMul.isPowerOf2 ()) {
4402
4412
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 );
4406
4416
} else {
4407
4417
SDValue Scale = DAG.getConstant (ElementMul.getZExtValue (), dl,
4408
4418
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 );
4411
4421
}
4412
4422
}
4413
4423
}
4414
4424
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);
4417
4433
}
4418
4434
}
4419
4435
0 commit comments