@@ -4386,34 +4386,59 @@ 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
+ if (NW.hasNoUnsignedSignedWrap ())
4393
+ ScaleFlags.setNoSignedWrap (true );
4394
+
4395
+ // The multiplication of an index by the type size does not wrap the
4396
+ // pointer index type in an unsigned sense (mul nuw).
4397
+ if (NW.hasNoUnsignedWrap ())
4398
+ ScaleFlags.setNoUnsignedWrap (true );
4399
+
4389
4400
if (ElementScalable) {
4390
4401
EVT VScaleTy = N.getValueType ().getScalarType ();
4391
4402
SDValue VScale = DAG.getNode (
4392
4403
ISD::VSCALE, dl, VScaleTy,
4393
4404
DAG.getConstant (ElementMul.getZExtValue (), dl, VScaleTy));
4394
4405
if (IsVectorGEP)
4395
4406
VScale = DAG.getSplatVector (N.getValueType (), dl, VScale);
4396
- IdxN = DAG.getNode (ISD::MUL, dl, N.getValueType (), IdxN, VScale);
4407
+ IdxN = DAG.getNode (ISD::MUL, dl, N.getValueType (), IdxN, VScale,
4408
+ ScaleFlags);
4397
4409
} else {
4398
4410
// If this is a multiply by a power of two, turn it into a shl
4399
4411
// immediately. This is a very common case.
4400
4412
if (ElementMul != 1 ) {
4401
4413
if (ElementMul.isPowerOf2 ()) {
4402
4414
unsigned Amt = ElementMul.logBase2 ();
4403
- IdxN = DAG.getNode (ISD::SHL, dl,
4404
- N. getValueType (), IdxN,
4405
- DAG. getConstant (Amt, dl, IdxN. getValueType ()) );
4415
+ IdxN = DAG.getNode (ISD::SHL, dl, N. getValueType (), IdxN,
4416
+ DAG. getConstant (Amt, dl, IdxN. getValueType ()) ,
4417
+ ScaleFlags );
4406
4418
} else {
4407
4419
SDValue Scale = DAG.getConstant (ElementMul.getZExtValue (), dl,
4408
4420
IdxN.getValueType ());
4409
- IdxN = DAG.getNode (ISD::MUL, dl,
4410
- N. getValueType (), IdxN, Scale );
4421
+ IdxN = DAG.getNode (ISD::MUL, dl, N. getValueType (), IdxN, Scale,
4422
+ ScaleFlags );
4411
4423
}
4412
4424
}
4413
4425
}
4414
4426
4415
- N = DAG.getNode (ISD::ADD, dl,
4416
- N.getValueType (), N, IdxN);
4427
+ SDNodeFlags AddFlags;
4428
+
4429
+ // The successive addition of each offset (without adding the base
4430
+ // address) does not wrap the pointer index type in a signed sense (add
4431
+ // nsw).
4432
+ if (NW.hasNoUnsignedSignedWrap ())
4433
+ AddFlags.setNoSignedWrap (true );
4434
+
4435
+ // The successive addition of each offset (without adding the base
4436
+ // address) does not wrap the pointer index type in an unsigned sense (add
4437
+ // nuw).
4438
+ if (NW.hasNoUnsignedWrap ())
4439
+ AddFlags.setNoUnsignedWrap (true );
4440
+
4441
+ N = DAG.getNode (ISD::ADD, dl, N.getValueType (), N, IdxN, AddFlags);
4417
4442
}
4418
4443
}
4419
4444
0 commit comments