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

Michael Kruse via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 10 04:47:53 PST 2025


Meinersbur wrote:

> > DA doesn't have an equivalent of execution domains, it has to assume Src/Dst may be executed for any `i`. I don't think we need to bail out only because of the simple presence of conditionals/loop guards, it just means we cannot prove monotonicity in this case which may then cause to not be analyzable
> 
> @Meinersbur consider a loop like this
> 
> ```
> for (i = 0; i < n; i++) {
> 
>     if (i <  3000) {
> 
>               for (j = .....) {
>               }
>               // some code with no control flow
>               for (k = .....) {
>               }
>     }
> }
> ```
> 
> I may have optimizations that infer nsw flags for computations in `k` loop by looking at the `if (i < 3000)` condition. If you think monotonicity cannot be proven in the presennce of loop guards, then monotonicity cannot be proven in this case either. Should we check for existence of a conditino like this and then bail out and say we cannot prove monotonicity if we encounter a condition like this?

Two cases:

1. The monotonicity check is control-flow insensitive. Then we cannot use `i < 3000` to prove monotonicity because we do not know whether where in the CFG we are: before/after the `br`? If after the `br`, which branch did we take? DA is currently only considers cyclic control flow. Loop guards are acyclic control flow, which DA currently does not handle.

2. The monotonicity check is made control-flow sensitive. In addition to the SCEV, we pass where in the CFG the SCEV is being evaluated. Only if this information is passed over as well we have the information whether `i < 3000` has been checked at that location.

The presence of a loop guard does not guarantee that the property holds globally everywhere. For instance:
```c
for (i = 0; i < n; i++) {
     if (i <  3000) {
               for (j = .....) {
                  ... j + i ...;
               }
       }
	   for (k = .....) {
	      ... k + i ...
	   }
}
```
Obviously we cannot assume that `i < 3000` when `k + i` is evaluated.

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


More information about the llvm-commits mailing list