[llvm] AArch64/GlobalISel: Mark the LR def of the Mach-O TLS call dead (PR #227234)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 29 02:15:45 PDT 2026


https://github.com/arsenm created https://github.com/llvm/llvm-project/pull/227234

The ordinary call path already does this, and the SelectionDAG emitter
marks unused implicit physreg defs dead for free. Without it the dead
flag needs to be reinferred later.

Co-Authored-By: Claude Opus 5 <noreply at anthropic.com>

>From 40ef409802cbdeb48a5c754222183edf2a6eacc0 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Tue, 29 Sep 2026 06:30:55 +0200
Subject: [PATCH] AArch64/GlobalISel: Mark the LR def of the Mach-O TLS call
 dead

The ordinary call path already does this, and the SelectionDAG emitter
marks unused implicit physreg defs dead for free. Without it the dead
flag needs to be reinferred later.

Co-Authored-By: Claude Opus 5 <noreply at anthropic.com>
---
 .../GISel/AArch64InstructionSelector.cpp      |  1 +
 .../AArch64/GlobalISel/select-tls-macho.ll    | 30 +++++++++++++++++++
 2 files changed, 31 insertions(+)
 create mode 100644 llvm/test/CodeGen/AArch64/GlobalISel/select-tls-macho.ll

diff --git a/llvm/lib/Target/AArch64/GISel/AArch64InstructionSelector.cpp b/llvm/lib/Target/AArch64/GISel/AArch64InstructionSelector.cpp
index 309dbd0def387..22c0e3a3a462d 100644
--- a/llvm/lib/Target/AArch64/GISel/AArch64InstructionSelector.cpp
+++ b/llvm/lib/Target/AArch64/GISel/AArch64InstructionSelector.cpp
@@ -3920,6 +3920,7 @@ bool AArch64InstructionSelector::selectTLSGlobalValueMachO(
   }
 
   MIB.buildInstr(Opcode, {}, {Load})
+      .setOperandDead(1) // implicit-def $lr
       .addUse(AArch64::X0, RegState::Implicit)
       .addDef(AArch64::X0, RegState::Implicit)
       .addRegMask(TRI.getTLSCallPreservedMask());
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/select-tls-macho.ll b/llvm/test/CodeGen/AArch64/GlobalISel/select-tls-macho.ll
new file mode 100644
index 0000000000000..0586ac29f4105
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/select-tls-macho.ll
@@ -0,0 +1,30 @@
+; NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=arm64-apple-macosx -global-isel -O0 -stop-after=instruction-select < %s | FileCheck %s
+
+ at tls_var = external thread_local global i32
+
+define ptr @tls_addr() {
+  ; CHECK-LABEL: name: tls_addr
+  ; CHECK: bb.1 (%ir-block.0):
+  ; CHECK-NEXT:   [[LOADgot:%[0-9]+]]:gpr64common = LOADgot target-flags(aarch64-tls) @tls_var
+  ; CHECK-NEXT:   [[LDRXui:%[0-9]+]]:gpr64common = LDRXui [[LOADgot]], 0
+  ; CHECK-NEXT:   $x0 = COPY [[LOADgot]]
+  ; CHECK-NEXT:   BLR [[LDRXui]], csr_darwin_aarch64_tls, implicit-def dead $lr, implicit $sp, implicit $x0, implicit-def $x0
+  ; CHECK-NEXT:   [[COPY:%[0-9]+]]:gpr64 = COPY $x0
+  ; CHECK-NEXT:   $x0 = COPY [[COPY]]
+  ; CHECK-NEXT:   RET_ReallyLR implicit $x0
+  ret ptr @tls_var
+}
+
+define ptr @tls_addr_ptrauth() "ptrauth-calls" {
+  ; CHECK-LABEL: name: tls_addr_ptrauth
+  ; CHECK: bb.1 (%ir-block.0):
+  ; CHECK-NEXT:   [[LOADgot:%[0-9]+]]:gpr64common = LOADgot target-flags(aarch64-tls) @tls_var
+  ; CHECK-NEXT:   [[LDRXui:%[0-9]+]]:gpr64common = LDRXui [[LOADgot]], 0
+  ; CHECK-NEXT:   $x0 = COPY [[LOADgot]]
+  ; CHECK-NEXT:   BLRAAZ [[LDRXui]], csr_darwin_aarch64_tls, implicit-def dead $lr, implicit $sp, implicit $x0, implicit-def $x0
+  ; CHECK-NEXT:   [[COPY:%[0-9]+]]:gpr64 = COPY $x0
+  ; CHECK-NEXT:   $x0 = COPY [[COPY]]
+  ; CHECK-NEXT:   RET_ReallyLR implicit $x0
+  ret ptr @tls_var
+}



More information about the llvm-commits mailing list