@@ -3414,9 +3414,24 @@ SCEVSignedMonotonicityChecker::SCEVSignedMonotonicityChecker(
3414
3414
ScalarEvolution *SE, const Loop *OutermostLoop, const Value *Ptr)
3415
3415
: SE(SE), OutermostLoop(OutermostLoop) {
3416
3416
if (Ptr) {
3417
- // TODO: This seems incorrect. Maybe we should check the reachability from
3418
- // the GEP to the target instruction. E.g., in the following case, maybe
3419
- // no-wrap is not guaranteed:
3417
+ // Perform reasoning similar to LoopAccessAnalysis. If an AddRec would wrap
3418
+ // and the GEP would have nusw, the wrapped memory location would become
3419
+ // like as follows (in the mathmatical sense, assuming the step recurrence
3420
+ // is positive):
3421
+ //
3422
+ // (previously accessed location) + (step recurrence) - 2^N
3423
+ //
3424
+ // where N is the size of the pointer index type. Since the value of step
3425
+ // recurrence is less than 2^(N-1), the distance between the previously
3426
+ // accessed location and the wrapped location will be greater than 2^(N-1),
3427
+ // which is larger than half the pointer index type space. The size of
3428
+ // allocated object must not exceed the largest signed integer that fits
3429
+ // into the index type, so the GEP value would be poison and any memory
3430
+ // access using it would be immediate UB when executed.
3431
+ //
3432
+ // TODO: We don't check if the result of the GEP is always used. Maybe we
3433
+ // should check the reachability from the GEP to the target instruction.
3434
+ // E.g., in the following case, no-wrap would not trigger immediate UB:
3420
3435
//
3421
3436
// entry:
3422
3437
// ...
@@ -3441,8 +3456,6 @@ MonotonicityType SCEVSignedMonotonicityChecker::checkMonotonicity(
3441
3456
const Value *Ptr) {
3442
3457
SCEVSignedMonotonicityChecker Checker (SE, OutermostLoop, Ptr);
3443
3458
MonotonicityType MT = Checker.visit (Expr);
3444
- if (MT == MonotonicityType::Unknown && Checker.NoWrapFromGEP )
3445
- MT = MonotonicityType::NoSignedWrap;
3446
3459
3447
3460
#ifndef NDEBUG
3448
3461
switch (MT) {
@@ -3532,8 +3545,6 @@ SCEVSignedMonotonicityChecker::visitAddRecExpr(const SCEVAddRecExpr *Expr) {
3532
3545
const SCEV *Start = Expr->getStart ();
3533
3546
const SCEV *Step = Expr->getStepRecurrence (*SE);
3534
3547
3535
- bool IsNSW = Expr->hasNoSignedWrap ();
3536
-
3537
3548
MonotonicityType StartRes = visit (Start);
3538
3549
if (StartRes == MonotonicityType::Unknown)
3539
3550
return unknownMonotonicity (Expr);
@@ -3543,7 +3554,7 @@ SCEVSignedMonotonicityChecker::visitAddRecExpr(const SCEVAddRecExpr *Expr) {
3543
3554
return unknownMonotonicity (Expr);
3544
3555
3545
3556
// TODO: Enhance the inference here.
3546
- if (!IsNSW ) {
3557
+ if (!Expr-> hasNoSignedWrap () && !NoWrapFromGEP ) {
3547
3558
if (!SE->isKnownNegative (Step))
3548
3559
// If the coefficient can be positive value, ensure that the AddRec is
3549
3560
// monotonically increasing.
@@ -3787,9 +3798,10 @@ bool DependenceInfo::tryDelinearizeParametricSize(
3787
3798
LI->getLoopFor (Src->getParent ())->getOutermostLoop ();
3788
3799
3789
3800
// TODO: In general, reasoning about monotonicity of a subscript from the
3790
- // base pointer would not be allowed. Probably we need to check the loops
3791
- // associated with this subscript are disjoint from those associated with
3792
- // the other subscripts. The validation would be something like:
3801
+ // base pointer would lead incorrect result. Probably we need to check
3802
+ // the loops associated with this subscript are disjoint from those
3803
+ // associated with the other subscripts. The validation would be
3804
+ // something like:
3793
3805
//
3794
3806
// LoopsI = collectCommonLoops(SrcSubscripts[I])
3795
3807
// LoopsOthers = collectCommonLoops(SrcSCEV - SrcSubscripts[I])
0 commit comments