https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82803
Bug ID: 82803 Summary: Wildly excessive calls to __tls_get_addr with optimizations enabled. Product: gcc Version: 7.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: amohr at amohr dot org Target Milestone: --- gcc emits a call to __tls_get_addr for every iteration of the while loop below at -O1 or above. It does not happen at -O0. static thread_local int x; int g(); int f(int c) { int *px = &x; while (c--) *px =+ g(); return *px; } Compile with: -O2 -std=c++11 -fPIC -Wall -Wextra I tried it with several versions back to 4.8.2 with the same results. At -O0 exactly one call is emitted. Seems like the optimizer is doing something silly with thread_locals. https://godbolt.org/g/S9hvHQ