https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99619
Bug ID: 99619 Summary: fails to infer local-dynamic TLS model from hidden visibility Product: gcc Version: 11.0 Status: UNCONFIRMED Keywords: missed-optimization Severity: normal Priority: P3 Component: middle-end Assignee: unassigned at gcc dot gnu.org Reporter: amonakov at gcc dot gnu.org Target Milestone: --- Thread-local variables with hidden visibility don't need to use the "general-dynamic" TLS model: they can use "local-dynamic" model, which is more efficient when more than one variable is accessed. This is documented in "ELF handling for thread-local storage". Testcase: __attribute__((visibility("hidden"))) extern __thread int a, b; int f() { return a + b; } clang -O2 -fpic emits: f: .cfi_startproc push rax .cfi_def_cfa_offset 16 lea rdi, [rip + a@TLSLD] call __tls_get_addr@PLT mov rcx, rax mov eax, dword ptr [rax + b@DTPOFF] add eax, dword ptr [rcx + a@DTPOFF] pop rcx .cfi_def_cfa_offset 8 ret gcc -O2 -fpic emits: f: .cfi_startproc push rbx .cfi_def_cfa_offset 16 .cfi_offset 3, -16 data16 lea rdi, a@tlsgd[rip] .value 0x6666 rex64 call __tls_get_addr@PLT mov rbx, rax data16 lea rdi, b@tlsgd[rip] .value 0x6666 rex64 call __tls_get_addr@PLT mov eax, DWORD PTR [rax] add eax, DWORD PTR [rbx] pop rbx .cfi_def_cfa_offset 8 ret