https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105728
Bug ID: 105728 Summary: dead store to static var not optimized out Product: gcc Version: 11.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: felix-gcc at fefe dot de Target Milestone: --- Consider this piece of test code: int dummy1() { static int removeme = 0; if (removeme) { return 0; } removeme = 1; return 0; } int dummy2() { static int removeme = 0; if (!removeme) removeme = 1; return 0; } int dummy3() { static int removeme = 0; removeme = 1; return 0; } To me, all of these do the same thing and should generate the same code. As nobody else can see removeme, and we aren't leaking its address, shouldn't the compiler be able to deduce that all accesses to removeme are inconsequential and can be removed? My gcc 11.3 generates a condidion and a store and a return 0 for dummy1, the same thing for dummy2, but for dummy3 it understands that it only needs to emit a return 0.