[llvm] [LAA] Only use inbounds/nusw in isNoWrap if the GEP is dereferenced. (PR #161445)

Ryotaro Kasuga via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 1 03:56:29 PDT 2025


================
@@ -1021,9 +1024,22 @@ static bool isNoWrap(PredicatedScalarEvolution &PSE, const SCEVAddRecExpr *AR,
   // the distance between the previously accessed location and the wrapped
   // location will be larger than half the pointer index type space. In that
   // case, the GEP would be  poison and any memory access dependent on it would
-  // be immediate UB when executed.
+  // be immediate UB when executed. The reasoning can only be applied if the
+  // pointer is dereferenced at least at the last iteration. For now, check if
+  // it is dereferenced in every iteration.
----------------
kasuga-fj wrote:

> The reasoning can only be applied if the pointer is dereferenced at least at the last iteration.

I think I'm missing something here, but this seems insufficient to me. Consider the following code:

```c
unsigned long long offset = 0;
for (int i = 0; i <= 100; i++) {
    if (i <= 1 || i % 4 == 0)
      A[offset] = 0;
    offset += (1ULL << 62) + 1;
}
```

In this case, `A[offset]` will access memory locations in the order of `A[0], A[(1ULL << 62) + 1], A[4], A[8], A[12], ..., A[100]`. IIUC, although the pointer is dereferenced in the last iteration, maybe `isNoWrap` should not return true?

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


More information about the llvm-commits mailing list