[X86] Don't convert local function foo in the same section to foo(%rip) when the offset is near INT32_MIN by MaskRay · Pull Request #98438 · llvm/llvm-project (original) (raw)

@llvm/pr-subscribers-backend-x86

Author: Fangrui Song (MaskRay)

Changes

When Sym is a local function symbol, leaq Sym+Offset(%rip), %rax in
non-large code models generates a relocation that references the section
symbol. An offset that is too closer to INT32_MIN could cause an
underflow addend (https://discourse.llvm.org/t/arithmetic-referencing-dso-local-function-causes-compilation-error-on-linux-x64/80033):

leaq Sym-0x80000000(%rip), %rax
=>
leaq .text-0x80000001(%rip), %rax  # if Sym=.text+1

Don't fold such offsets. For simplicity, we don't check whether Sym is a
function symbol.


Full diff: https://github.com/llvm/llvm-project/pull/98438.diff

2 Files Affected:

diff --git a/llvm/lib/Target/X86/X86ISelDAGToDAG.cpp b/llvm/lib/Target/X86/X86ISelDAGToDAG.cpp index c91bd576dc9f6..e06de1c4276a3 100644 --- a/llvm/lib/Target/X86/X86ISelDAGToDAG.cpp +++ b/llvm/lib/Target/X86/X86ISelDAGToDAG.cpp @@ -1795,6 +1795,13 @@ bool X86DAGToDAGISel::foldOffsetIntoAddress(uint64_t Offset, !X86::isOffsetSuitableForCodeModel(Val, M, AM.hasSymbolicDisplacement())) return true;

+define dso_local i64 @neg_0x70000000() #0 { +; STATIC-LABEL: neg_0x70000000: +; STATIC: # %bb.0: # %entry +; STATIC-NEXT: leaq foo-1879048192(%rip), %rax +; STATIC-NEXT: retq +; +; PIC-LABEL: neg_0x70000000: +; PIC: # %bb.0: # %entry +; PIC-NEXT: leaq foo-1879048192(%rip), %rax +; PIC-NEXT: retq +; +; MSTATIC-LABEL: neg_0x70000000: +; MSTATIC: # %bb.0: # %entry +; MSTATIC-NEXT: movabsq $foo, %rax +; MSTATIC-NEXT: addq $-1879048192, %rax # imm = 0x90000000 +; MSTATIC-NEXT: retq +; +; MPIC-LABEL: neg_0x70000000: +; MPIC: # %bb.0: # %entry +; MPIC-NEXT: leaq GLOBAL_OFFSET_TABLE(%rip), %rax +; MPIC-NEXT: movabsq $foo@GOTOFF, %rcx +; MPIC-NEXT: leaq -1879048192(%rax,%rcx), %rax +; MPIC-NEXT: retq +entry:

+define internal void @bar() #0 { +; STATIC-LABEL: bar: +; STATIC: # %bb.0: +; STATIC-NEXT: retq +; +; PIC-LABEL: bar: +; PIC: # %bb.0: +; PIC-NEXT: retq +; +; MSTATIC-LABEL: bar: +; MSTATIC: # %bb.0: +; MSTATIC-NEXT: retq +; +; MPIC-LABEL: bar: +; MPIC: # %bb.0: +; MPIC-NEXT: retq