Fangrui Song 570871eab5
[X86] Don't convert local function foo in the same section to foo(%rip) when the offset is near INT32_MIN
```
define internal void @foo() {
  ret void
}
define i64 @main() {
  ret i64 add (i64 ptrtoint (ptr @foo to i64), i64 -2147483626)
}
```

When `foo` is a local symbol, `foo` and `main` are in the same section,
and `offset` is near INT32_MIN, referencing `foo+offset` in `main` with
RIP-relative addressing needs `leaq .text+offset1(%rip), %rax` where
`offset1 < offset`, and `offset1` might underflow.
(https://discourse.llvm.org/t/arithmetic-referencing-dso-local-function-causes-compilation-error-on-linux-x64/80033):

Don't use RIP-relative addressing if the negative offset is near
INT32_MIN. Arbitrarily reuse the magic number in isOffsetSuitableForCodeModel to
guard against the edge case when `address(current_instruction)-foo < 4GiB-16MiB`.
If the difference is larger than 4GiB-16MiB, `ret i64 add (i64 ptrtoint
(ptr @foo to i64), i64 -2**32+256MiB)` would still cause the assembly
issue, such cases are unrealistic.

Pull Request: https://github.com/llvm/llvm-project/pull/98438
2024-09-30 22:46:08 -07:00
..
2024-04-29 09:01:57 +08:00
2024-05-15 13:10:16 +01:00