[PATCH] D22377: [SCEV] trip count calculation for loops with unknown stride

Pankaj Chawla via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 22 13:54:26 PDT 2016


pankajchawla added a comment.

Hi Sanjoy,

Thanks for explaining how we mark nuw/nsw for the AddRecs!

I have added logic to check for side effects in the loop in loophasNoSideEffects().

Regarding the loop entry guard check:
Please note that I haven't changed this check and I believe the original check is correct. The loop entry guard tells us whether the loop will be executed at least once. It cannot tell us whether the loop will be executed more than once. Please also note that the backedge taken count for the loop mentioned in the description is: ((-1 + %n) /u %s)
A more generic formula with unknown initial value is: ((-1 + %n - %init) /u %s)

Now lets go through your examples:

> The example I was thinking of was:

> 

>   for (unsigned i = 1; i < 5; i += (-2)) {

>   }

>    

> 

> ==

> 

>   i = 1;

>   if (i < 5) {

>     do {

>       i += (-2); // NUW

>     } while (i u< 5);

>   }

>    

> 

> then the pre-increment IV is {1,+,-2}.  It does not unsigned-overflow on the first increment (1 + (-2) == -1), and then it exits the loop.


The backedge taken count for this loop according to the formula above will be:
(5 -1 -1) /u (-2)  ==> 0

So it is correct.

Probably the confusion is because I am using the existing terminology in the code of a positive or negative stride. May be we should be classifying them as `count up` or `count down` loops instead.

Here's your second example-

> 

> 

>   i = INT_MIN

>   if (i s< 5) {

>     do {

>       i += (-1);

>     } while (i s< 5);

>   }

>    


I believe this loop has undefined behavior since adding INT_MIN and -1 leads to signed underflow.

I guess my point is that once we have checked the loop entry guard, single trip loops will be handled just as well as multi-trip loops.
Please let me know if I am still missing anything.

Please note that the nsw marking on the IV AddRec is quite conservative. If I initialize the IV to anything else than 0 in the original loop, we lose the marking and the backedge taken count is not computed.


https://reviews.llvm.org/D22377





More information about the llvm-commits mailing list