[llvm] [DA] Introduce domain for monotonicity (PR #176367)
Michael Kruse via llvm-commits
llvm-commits at lists.llvm.org
Wed Feb 4 11:51:02 PST 2026
Meinersbur wrote:
A writedown of the potentional correctness issue from the meeting. Using @kasuga-fj's example:
```c
for (int64_t i = 0; i < INT64_MAX; i++)
if (i < 1000)
for (int64_t j = 0; j < INT64_MAX - 1000; j++)
A[i + j] = 0;
```
When ignoring the loop guard during analysis, one might conclude that executing this code will unconditionally execute undefined behaviour due to the overflow of the nsw integer expression `i+j`, e.g. with the last iteration `i==j==INT64_MAX-1`. Since it is undefined behavior, we can do anything with it, e.g. delete the the entire loop nest. This is clearly wrong, since `if (i < 1000)` would avoid such wrap execution. Hence, we cannot always ignore the condition.
I don't think this applies to DA though. The undefined behavior of some expressions with `i` and `j` in them could at most the conclusion that dependencies with those instances do not exist, which is fine. Another source of information that some dependencies do not exist is `if (i < 1000)`, which in this case are (almost) identical. They complement each other.
Independent of adding "effective domain" information, we need to add wrap checks when there is no `nsw` flag, i.e. cannot assume that wrapping behavior is undefined.
```c
for (uint64_t i = 0; i < INT64_MAX; i++)
if (i < 2000)
for (uint64_t j = 0; j < INT64_MAX - 1000; j++)
A[i + j] = 0;
```
this wraps when the integers are interpreted as signed, without nsw flag, with- and without taking the conditional into account. IIUC, @amehsan [is going to add those](https://github.com/llvm/llvm-project/pull/176367#issuecomment-3826432292) assuming the more conservative $0 \le i \le BTC$ domain.
https://github.com/llvm/llvm-project/pull/176367
More information about the llvm-commits
mailing list