[llvm] [X86] Respect code models more when determining if a global reference can fit in 32 bits (PR #75386)

Arthur Eubanks via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 13 20:25:15 PST 2023


================
@@ -96,12 +97,67 @@ define dso_local ptr @lea_static_data() #0 {
 ; LARGE-SMALL-DATA-PIC-NEXT:    leaq .L0$pb(%rip), %rax
 ; LARGE-SMALL-DATA-PIC-NEXT:    movabsq $_GLOBAL_OFFSET_TABLE_-.L0$pb, %rcx
 ; LARGE-SMALL-DATA-PIC-NEXT:    addq %rax, %rcx
-; LARGE-SMALL-DATA-PIC-NEXT:    movabsq $static_data at GOTOFF, %rax
+; LARGE-SMALL-DATA-PIC-NEXT:    movl $static_data at GOTOFF, %eax
 ; LARGE-SMALL-DATA-PIC-NEXT:    addq %rcx, %rax
 ; LARGE-SMALL-DATA-PIC-NEXT:    retq
   ret ptr @static_data
 }
 
+define dso_local ptr @lea_static_data_alias() #0 {
+; SMALL-STATIC-LABEL: lea_static_data_alias:
+; SMALL-STATIC:       # %bb.0:
+; SMALL-STATIC-NEXT:    movl $static_data_alias, %eax
+; SMALL-STATIC-NEXT:    retq
+;
+; MEDIUM-STATIC-LABEL: lea_static_data_alias:
+; MEDIUM-STATIC:       # %bb.0:
+; MEDIUM-STATIC-NEXT:    movabsq $static_data_alias, %rax
+; MEDIUM-STATIC-NEXT:    retq
+;
+; LARGE-STATIC-LABEL: lea_static_data_alias:
+; LARGE-STATIC:       # %bb.0:
+; LARGE-STATIC-NEXT:    movabsq $static_data_alias, %rax
+; LARGE-STATIC-NEXT:    retq
+;
+; SMALL-PIC-LABEL: lea_static_data_alias:
+; SMALL-PIC:       # %bb.0:
+; SMALL-PIC-NEXT:    leaq static_data_alias(%rip), %rax
+; SMALL-PIC-NEXT:    retq
+;
+; MEDIUM-SMALL-DATA-PIC-LABEL: lea_static_data_alias:
+; MEDIUM-SMALL-DATA-PIC:       # %bb.0:
+; MEDIUM-SMALL-DATA-PIC-NEXT:    leaq static_data_alias(%rip), %rax
+; MEDIUM-SMALL-DATA-PIC-NEXT:    retq
+;
+; MEDIUM-PIC-LABEL: lea_static_data_alias:
+; MEDIUM-PIC:       # %bb.0:
+; MEDIUM-PIC-NEXT:    leaq _GLOBAL_OFFSET_TABLE_(%rip), %rcx
+; MEDIUM-PIC-NEXT:    movabsq $static_data_alias at GOTOFF, %rax
+; MEDIUM-PIC-NEXT:    addq %rcx, %rax
+; MEDIUM-PIC-NEXT:    retq
+;
+; LARGE-PIC-LABEL: lea_static_data_alias:
+; LARGE-PIC:       # %bb.0:
+; LARGE-PIC-NEXT:  .L1$pb:
+; LARGE-PIC-NEXT:    leaq .L1$pb(%rip), %rax
+; LARGE-PIC-NEXT:    movabsq $_GLOBAL_OFFSET_TABLE_-.L1$pb, %rcx
----------------
aeubanks wrote:

the problem is relocations only allow one symbol to relocate against, we can't just diff two symbols. so we have to take advantage of the other implicit operands in the relocations, e.g. the PC/GOT/PLT, for PIC. for example, using the GOT as an anchor requires a 64-bit relocation to get the address of the GOT. the only anchor that x86-64 static relocations provide that can be assumed to be close to the instruction is the PC, so we'd probably need to use R_X86_64_PC64 (seems like this is a rare relocation type). perhaps something like the following

```
  leaq .L(%rip), %rax
.L
  movabsq foo-%rip, %rbx
  addq %rbx, %rax
```

but now you need a label for every reference to a global. I'm not sure how much this impacts optimizations. (do optimizations take care to not move around labels if their relative placement with instructions matter?)

maybe this is worth experimenting with at some point

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


More information about the llvm-commits mailing list