[llvm] [DA] Add tests for nsw doesn't hold on entier iteration (PR #162281)

Ryotaro Kasuga via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 11 02:51:15 PST 2025


kasuga-fj wrote:

> It is not clear to me where we make this assumption and what are the consequences of that. But before discussing that I have another question: I believe you are talking about this example from your testcase:
> 
> ```
> ; for (i = 0; i < INT64_MAX - 1; i++)
> ;   if (i < 1000)
> ;     for (j = 0; j < 2000; j++)
> ;       a[i + j] = 0;
> ```
> 
> and you are saying for DA to be correct we need to check that a loop guard exists (or may be check what the loop guard is) and bail out. Now I can change your example to the following:
> 
> ```
> for (i = 0; i < INT64_MAX - 1; i++)
>    if (i < 1000) {
>      for (k = 0;  k < 5000; k++) {
>            // do something in the loop
>       }
>      // some complex code possibly involving control flow 
>      for (j = 0; j < 2000; j++)   
>        a[i + j] = 0;
>      }
>   } // if (i < 1000)
> ```
> 
> Your comment is applicable to this example as well. Here `j` loop has no guard. How do you want to handle this case? Checking the guard doesn't seem to work here. Also please answer the same question about the following example
> 
> ```
> for (i = 0; i < INT64_MAX - 1; i++)
>    
>      for (k = 0;  k < 5000; k++) {
>            if (i > 1000) goto X; // X is a label outside the loopnest.
>       }
>      // some complex code possibly involving control flow 
>      for (j = 0; j < 2000; j++)   
>        a[i + j] = 0;
>      }
>   } 
> ```

I think we can ensure it by checking the (post-)dominance for headers and/or latches between the outer loop and the inner loop. It might also be worth noting that `ScalarEvolution::LoopGuards` already implements the function that collects the conditions required to enter the loop.

https://github.com/llvm/llvm-project/pull/162281


More information about the llvm-commits mailing list