Issue 233233
Summary [ConstraintElim] `gep nuw` with a type size > 2^63 is decomposed with a negative scale, folding `icmp ule ptr %g, %p` to true
Labels miscompilation, llvm:transforms, generated by fuzzer
Assignees
Reporter ChoKyuWon
    ### reproducer
godbolt: https://godbolt.org/z/Pbe8vK753
alive: https://alive2.llvm.org/ce/z/GmfF42

```llvm
define i1 @f(ptr %p, i64 %i) {
  %g = getelementptr nuw [9223372036854775809 x i8], ptr %p, i64 %i
  %c = icmp ule ptr %g, %p
  ret i1 %c
}

; control: stride 2^62+1 (sign bit clear) is not folded
define i1 @g(ptr %p, i64 %i) {
  %g = getelementptr nuw [4611686018427387905 x i8], ptr %p, i64 %i
  %c = icmp ule ptr %g, %p
 ret i1 %c
}
```

```console
$ opt -passes=constraint-elimination -S repro.ll
define i1 @f(ptr %p, i64 %i) {
  %g = getelementptr nuw [9223372036854775809 x i8], ptr %p, i64 %i
  ret i1 true
}

define i1 @g(ptr %p, i64 %i) {
  %g = getelementptr nuw [4611686018427387905 x i8], ptr %p, i64 %i
  %c = icmp ule ptr %g, %p
  ret i1 %c
}

$ alive-tv --disable-undef-input --disable-poison-input --passes=constraint-elimination repro.ll
ERROR: Value mismatch
Example:
ptr %p = null
i64 %i = #x0000000000000001 (1)
Source:
ptr %g = pointer(non-local, block_id=0, offset=-9223372036854775807) / Address=#x8000000000000001
i1 %c = #x0 (0)
Source value: #x0 (0)
Target value: #x1 (1)
```

### suggested fix
`decomposeGEP` (ConstraintElimination.cpp) converts each variable-index scale with `Scale.getSExtValue()`, so a nuw-only stride of `2^63+1` becomes the coefficient `-(2^63-1)` and the unsigned system concludes `%g <= %p`.
For a GEP without `nusw`/`inbounds`, bail out when `Scale.isNegative()`, as is already done for `ConstantOffset` a few lines above.

> Disclaimer: This test case was generated by a fuzzer, and the report was written with LLM assistance.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to