https://gcc.gnu.org/g:45d15ffd7de445fdb993550dee54304d20b053d1

commit r17-6061-g45d15ffd7de445fdb993550dee54304d20b053d1
Author: Uros Bizjak <[email protected]>
Date:   Sat Nov 7 11:34:49 2026 +0000

    i386: Don't output dead TLS LD base sequences [PR128437]
    
    The fix for PR128437 removed dead 64-bit TLS local-dynamic base calls
    in ix86_reorg, but it relies on REG_UNUSED notes on the call, and these
    are often missing.  The call result can still be used by a dead spill
    store whose reload was deleted by DCE in sched2, DCE never deletes
    non-register stores, and the notes may not be recomputed at all when
    sched2 or DCE doesn't run.  The call is then left without any
    local-dynamic TLS symbol in the function, and '%&' in its template ICEs.
    
    Fix this by making the output independent of what other passes deleted.
    The only users of the local-dynamic base are the "base + sym@dtpoff"
    forms, and all of them contain the TLS symbol.  So if
    get_some_local_dynamic_name finds no symbol, nothing can use the base,
    and the insn only has to define its result register.  Output "xor"
    instead of the lea and call in this case, in both 64-bit patterns and in
    the 32-bit pattern, which can be kept alive by a dead spill store in the
    same way.  FLAGS_REG is clobbered by the call patterns, and explicitly by
    the 32-bit pattern.
    
    Remove ix86_remove_dead_tls_ld_base, which is not needed anymore.
    
    Assisted-by: Claude Sonnet 5.5
    
            PR target/128437
    
    gcc/ChangeLog:
    
            * config/i386/i386.cc (ix86_remove_dead_tls_ld_base): Remove.
            (ix86_reorg): Do not call it.
            * config/i386/i386.md (*tls_local_dynamic_base_32_gnu): Output
            xor of the result register when no local-dynamic TLS symbol
            is left.
            (*tls_local_dynamic_base_64_<mode>): Ditto.
            (*tls_local_dynamic_base_64_largepic): Ditto.
    
    gcc/testsuite/ChangeLog:
    
            * g++.target/i386/pr128437-1.C: New test.

Diff:
---
 gcc/config/i386/i386.cc                    | 38 ----------------------
 gcc/config/i386/i386.md                    | 18 +++++++++++
 gcc/testsuite/g++.target/i386/pr128437-1.C | 51 ++++++++++++++++++++++++++++++
 3 files changed, 69 insertions(+), 38 deletions(-)

diff --git a/gcc/config/i386/i386.cc b/gcc/config/i386/i386.cc
index d1e16e6ecafc..5a08490abc70 100644
--- a/gcc/config/i386/i386.cc
+++ b/gcc/config/i386/i386.cc
@@ -25472,40 +25472,6 @@ ix86_split_stlf_stall_load ()
     }
 }
 
-/* Recursive DCE run from sched2 doesn't delete dead const calls, so the
-   call of a 64-bit TLS local-dynamic base sequence can be left behind with
-   an unused result after its last @dtpoff user got deleted.  Remove such
-   calls, since print_operand's '%&' needs some remaining local-dynamic TLS
-   reference.  */
-
-static void
-ix86_remove_dead_tls_ld_base (void)
-{
-  rtx_insn *insn, *next;
-
-  for (insn = get_insns (); insn; insn = next)
-    {
-      next = NEXT_INSN (insn);
-
-      if (!CALL_P (insn)
-         || !RTL_CONST_CALL_P (insn)
-         || !insn_nothrow_p (insn))
-       continue;
-
-      if (!find_regno_note (insn, REG_UNUSED, AX_REG))
-       continue;
-
-      subrtx_iterator::array_type array;
-      FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
-       if (GET_CODE (*iter) == UNSPEC
-           && XINT (*iter, 1) == UNSPEC_TLS_LD_BASE)
-         {
-           delete_insn (insn);
-           break;
-         }
-    }
-}
-
 /* Implement machine specific optimizations.  We implement padding of returns
    for K8 CPUs and pass to avoid 4 jumps in the single 16 byte window.  */
 static void
@@ -25518,10 +25484,6 @@ ix86_reorg (void)
   if (TARGET_SEH && current_function_has_exception_handlers ())
     ix86_seh_fixup_eh_fallthru ();
 
-  if (optimize && TARGET_64BIT && TARGET_GNU_TLS
-      && ix86_tls_descriptor_calls_expanded_in_cfun)
-    ix86_remove_dead_tls_ld_base ();
-
   if (optimize && optimize_function_for_speed_p (cfun))
     {
       if (TARGET_SSE2)
diff --git a/gcc/config/i386/i386.md b/gcc/config/i386/i386.md
index 3bd20837e710..71408b720d30 100644
--- a/gcc/config/i386/i386.md
+++ b/gcc/config/i386/i386.md
@@ -24370,6 +24370,12 @@
    (clobber (reg:CC FLAGS_REG))]
   "!TARGET_64BIT && TARGET_GNU_TLS"
 {
+  /* The last local-dynamic TLS reference can be deleted after RA while
+     this insn survives (a dead spill store may still use the result).
+     Then '%&' has no symbol to print and nobody uses the base; only
+     define the result register.  */
+  if (!get_some_local_dynamic_name ())
+    return "xor{l}\t%0, %0";
   output_asm_insn
     ("lea{l}\t{%&@tlsldm(%1), %0|%0, %&@tlsldm[%1]}", operands);
   if (TARGET_SUN_TLS)
@@ -24409,6 +24415,12 @@
    (clobber (match_operand:P 3 "register_operand" "=D"))]
   "TARGET_64BIT"
 {
+  /* The last local-dynamic TLS reference can be deleted after RA while
+     this insn survives (DCE doesn't delete calls in sched2, and a dead
+     spill store may still use the result).  Then '%&' has no symbol to
+     print and nobody uses the base; only define the result register.  */
+  if (!get_some_local_dynamic_name ())
+    return "xor{l}\t%k0, %k0";
   output_asm_insn
     ("lea{q}\t{%&@tlsld(%%rip), %q3|%q3, %&@tlsld[rip]}", operands);
   if (TARGET_SUN_TLS)
@@ -24434,6 +24446,12 @@
    && GET_CODE (XEXP (operands[2], 0)) == UNSPEC
    && XINT (XEXP (operands[2], 0), 1) == UNSPEC_PLTOFF"
 {
+  /* The last local-dynamic TLS reference can be deleted after RA while
+     this insn survives (DCE doesn't delete calls in sched2, and a dead
+     spill store may still use the result).  Then '%&' has no symbol to
+     print and nobody uses the base; only define the result register.  */
+  if (!get_some_local_dynamic_name ())
+    return "xor{l}\t%k0, %k0";
   output_asm_insn
     ("lea{q}\t{%&@tlsld(%%rip), %4|%4, %&@tlsld[rip]}", operands);
   output_asm_insn ("movabs{q}\t{%2, %%rax|rax, %2}", operands);
diff --git a/gcc/testsuite/g++.target/i386/pr128437-1.C 
b/gcc/testsuite/g++.target/i386/pr128437-1.C
new file mode 100644
index 000000000000..78c5ac46e6f1
--- /dev/null
+++ b/gcc/testsuite/g++.target/i386/pr128437-1.C
@@ -0,0 +1,51 @@
+// PR target/128437
+// { dg-do compile { target { *-*-linux* && { ! ia32 } } } }
+// { dg-options "-O2 -fPIC -fvisibility=hidden -mtls-dialect=gnu" }
+// { dg-require-effective-target c++20 }
+
+template <typename _Tp> constexpr bool is_pointer_v = __is_pointer(_Tp);
+template <typename _Tp> constexpr bool is_function_v = __is_function(_Tp);
+void allocation_prefix();
+int is_pointer___trans_tmp_3;
+long bits_;
+struct {
+  static void *slot_load(void *) {
+    return is_pointer___trans_tmp_3 ? reinterpret_cast<void *>(bits_) : 
nullptr;
+  }
+} *entries_, source;
+void erase(void *slot) {
+  while (entries_)
+    if (entries_ == slot) {
+      entries_ = {};
+      return;
+    }
+}
+int thread_local current;
+void released(void *slot) {
+  if (current)
+    erase(slot);
+}
+template <class T>
+concept SlotValue = !is_function_v<T>;
+template <SlotValue T> void *allocation(T value) {
+  if constexpr (is_pointer_v<T>)
+    return value;
+  else
+    return T::slot_load(&value);
+}
+struct Reference {
+  int value_;
+  Reference() { released(&value_); }
+  void detach() { released(&value_); }
+};
+template <SlotValue T> void replace(T slot, Reference next) {
+  next.detach();
+  void *__trans_tmp_2 = allocation(slot);
+  if (__trans_tmp_2)
+    allocation_prefix();
+}
+int *views;
+void retire_owned() {
+  replace(source, {});
+  replace(views, {});
+}

Reply via email to