https://gcc.gnu.org/g:c39f9359a6ee4b76724427d00ac26cb936fda7e4
commit r14-13028-gc39f9359a6ee4b76724427d00ac26cb936fda7e4 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 64-bit TLS local-dynamic base sequence is a const call to __tls_get_addr whose template uses '%&', which needs a local-dynamic TLS symbol from some other insn in the function. The last such reference can be deleted after RA while the call survives: DCE doesn't delete calls in sched2, a dead spill store can still use the call's result, and REG_UNUSED notes may not be recomputed at all when sched2 or DCE doesn't run. The function then has no local-dynamic TLS symbol left, and '%&' 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. Assisted-by: Claude Sonnet 5.5 PR target/128437 gcc/ChangeLog: * 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.C: New test. (cherry picked from commit 45d15ffd7de445fdb993550dee54304d20b053d1) Diff: --- gcc/config/i386/i386.md | 18 +++++++++++++ gcc/testsuite/g++.target/i386/pr128437.C | 44 ++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/gcc/config/i386/i386.md b/gcc/config/i386/i386.md index 2d177d8f6de7..94eda0ab007c 100644 --- a/gcc/config/i386/i386.md +++ b/gcc/config/i386/i386.md @@ -21212,6 +21212,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) @@ -21251,6 +21257,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) @@ -21275,6 +21287,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.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, {}); +}
