https://gcc.gnu.org/g:c147997f4f3adbaa91f4f8772dbe0ed32144076f
commit r17-6003-gc147997f4f3adbaa91f4f8772dbe0ed32144076f Author: Uros Bizjak <[email protected]> Date: Thu Nov 5 14:35:53 2026 +0100 i386: Remove dead TLS local-dynamic base calls in ix86_reorg [PR128437] The 64-bit TLS local-dynamic base sequence is a const call to __tls_get_addr. When its last @dtpoff user is dead, the fast DCE run from sched2 deletes that user but keeps the call; DCE doesn't delete calls while df is in progress, and no DCE pass runs after sched2. The remaining call's output template uses '%&', which scans the insn stream for a local-dynamic TLS symbol, finds none and ICEs. Remove such calls in ix86_reorg, which runs after sched2, when their result is unused. Calls whose result is still used are left alone, so '%&' keeps finding the symbol of the remaining references. The 32-bit sequences are not calls and are deleted by DCE together with their users. Assisted-by: Claude Sonnet 5.5 PR target/128437 gcc/ChangeLog: * config/i386/i386.cc (ix86_remove_dead_tls_ld_base): New function. (ix86_reorg): Call it for 64-bit targets with GNU TLS when optimizing and TLS calls were expanded. gcc/testsuite/ChangeLog: * g++.target/i386/pr128437.C: New test. Diff: --- gcc/config/i386/i386.cc | 38 +++++++++++++++++++++++++++ gcc/testsuite/g++.target/i386/pr128437.C | 44 ++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/gcc/config/i386/i386.cc b/gcc/config/i386/i386.cc index 5a08490abc70..d1e16e6ecafc 100644 --- a/gcc/config/i386/i386.cc +++ b/gcc/config/i386/i386.cc @@ -25472,6 +25472,40 @@ 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 @@ -25484,6 +25518,10 @@ 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/testsuite/g++.target/i386/pr128437.C b/gcc/testsuite/g++.target/i386/pr128437.C new file mode 100644 index 000000000000..9a45d862c2ce --- /dev/null +++ b/gcc/testsuite/g++.target/i386/pr128437.C @@ -0,0 +1,44 @@ +// PR target/128437 +// { dg-do compile { target { *-*-linux* && { ! ia32 } } } } +// { dg-options "-O2 -fPIC -fvisibility=hidden -mtls-dialect=gnu" } +// { dg-require-effective-target c++11 } + +void f (); +void *p; +struct { void *slot; } *entries; +long capacity; + +void erase () +{ + if (capacity) + return; + for (int i = 0; entries[i].slot; ++i) + if (!entries) + entries = {}; +} + +thread_local int current, x, y; + +void released (void *) +{ + if (current) + erase (); +} + +struct Ref +{ + int v; + ~Ref () { released (&v); } +}; + +void replace (int, Ref) +{ + if (p) + f (); +} + +void g () +{ + replace (x, {}); + replace (y, {}); +}
