Issue 235469
Summary [SelectionDAG] BaseIndexOffset does not wrap constant offsets at the pointer width, so an address and itself are NoAlias on i386
Labels miscompilation, llvm:SelectionDAG, generated by fuzzer
Assignees
Reporter ChoKyuWon
    ### reproducer
godbolt: https://godbolt.org/z/M4e3TP54M

```llvm
target datalayout = "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-i128:128-f64:32:64-f80:32-n8:16:32-S128"
target triple = "i386-unknown-linux-gnu"

define i32 @f(ptr %p) {
  %a = getelementptr i8, ptr %p, i32 -2147483648
  %q = getelementptr i8, ptr %a, i32 -2147483648
  store i8 1, ptr %q
  store i8 2, ptr %p
  %v = load i8, ptr %q
  %r = zext i8 %v to i32
  ret i32 %r
}

define i32 @main() {
 %buf = alloca i8
  %r = call i32 @f(ptr %buf)
  ret i32 %r
}
```

```console
$ llubi repro.ll; echo $?
2

$ llc -O2 repro.ll -o -
f:                                      # @f
	movl	4(%esp), %eax
	movb	$1, (%eax)
	movb	$2, (%eax)
	movl	$1, %eax
	retl
...

$ llc -O0 repro.ll -o -
f: # @f
	movl	4(%esp), %ecx
	movl	%ecx, %eax
	addl	$-2147483648, %eax              # imm = 0x80000000
	movb	$1, -2147483648(%eax)
	movb	$2, (%ecx)
	movzbl	-2147483648(%eax), %eax
	retl
...
```

### suggested fix
`matchLSNode` (SelectionDAGAddressAnalysis.cpp) adds each constant `ADD`/`PTRADD` operand to an `int64_t Offset` with `getSExtValue()` but never reduces it to the 32-bit address width, so `%q` decomposes to (`%p`, -2^32) while it equals `%p`.
`equalBaseIndex` then returns a 2^32-byte distance and `computeAliasing` reports the two 1-byte accesses as NoAlias, so the load is chained past `store i8 2` and forwarded the value 1.
Reduce the distance in `equalBaseIndex` modulo the address width on every `return true` path, including the GlobalAddress/ConstantPool/FrameIndex adjustments (e.g. `Off = SignExtend64(Off, Base.getValueSizeInBits())`), which covers `computeAliasing`, `contains` and store merging.

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

Reply via email to