Skip to content

Commit d926ec3

Browse files
authored
[X86] Asm modifier %a: add (%rip) for 64-bit static relocation model
In GCC, ``` static int a; int foo() { asm("# %a0" : : "i"(&a)); } ``` lowers to `# a(%rip)` regardless of the PIC mode. This PR follow suits for ELF -fno-pic, matching ELF -fpic (asm-modifier-pic.ll) and Mach-O (which defaults to PIC). Close #139001 Pull Request: #139040
1 parent d915355 commit d926ec3

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

llvm/docs/LangRef.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5776,6 +5776,7 @@ and GCC likely indicates a bug in LLVM.
57765776

57775777
Target-independent:
57785778

5779+
- ``a``: Print a memory reference. Targets might customize the output.
57795780
- ``c``: Print an immediate integer constant unadorned, without
57805781
the target-specific immediate punctuation (e.g. no ``$`` prefix).
57815782
- ``n``: Negate and print immediate integer constant unadorned, without the
@@ -5913,6 +5914,8 @@ target-independent modifiers.
59135914

59145915
X86:
59155916

5917+
- ``a``: Print a memory reference. This displays as ``sym(%rip)`` for x86-64.
5918+
i386 should only use this with the static relocation model.
59165919
- ``c``: Print an unadorned integer or symbol name. (The latter is
59175920
target-specific behavior for this typically target-independent modifier).
59185921
- ``A``: Print a register name with a '``*``' before it.

llvm/lib/Target/X86/X86AsmPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ bool X86AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
744744
llvm_unreachable("unexpected operand type!");
745745
case MachineOperand::MO_GlobalAddress:
746746
PrintSymbolOperand(MO, O);
747-
if (Subtarget->isPICStyleRIPRel())
747+
if (Subtarget->is64Bit())
748748
O << "(%rip)";
749749
return false;
750750
case MachineOperand::MO_Register:

llvm/test/CodeGen/X86/asm-modifier.ll

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@
66
@var = internal global i32 0, align 4
77

88
define dso_local void @test_a() nounwind {
9-
; CHECK-LABEL: test_a:
10-
; CHECK: # %bb.0:
11-
; CHECK-NEXT: #APP
12-
; CHECK-NEXT: #TEST 42 var#
13-
; CHECK-NEXT: #NO_APP
14-
; CHECK-NEXT: ret{{[l|q]}}
9+
; X86-LABEL: test_a:
10+
; X86: # %bb.0:
11+
; X86-NEXT: #APP
12+
; X86-NEXT: #TEST 42 var#
13+
; X86-NEXT: #NO_APP
14+
; X86-NEXT: retl
15+
;
16+
; X64-LABEL: test_a:
17+
; X64: # %bb.0:
18+
; X64-NEXT: #APP
19+
; X64-NEXT: #TEST 42 var(%rip)#
20+
; X64-NEXT: #NO_APP
21+
; X64-NEXT: retq
1522
tail call void asm sideeffect "#TEST ${0:a} ${1:a}#", "i,i"(i32 42, ptr @var)
1623
ret void
1724
}

0 commit comments

Comments
 (0)