http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60320
Bug ID: 60320 Summary: Redundant static initialization check Product: gcc Version: 4.9.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: glisse at gcc dot gnu.org Hello, I think it would be nice if g++ realized that static variables that have been initialized don't later on become uninitialized again. A simple example: extern int e; static int b(){ static const int a = e; return a; } int g(){ return b()-b(); } gets "optimized" to: <bb 2>: _7 = MEM[(char *)&_ZGVZL1bvE1a]; if (_7 == 0) goto <bb 3>; else goto <bb 9>; <bb 3>: _8 = __cxa_guard_acquire (&_ZGVZL1bvE1a); if (_8 != 0) goto <bb 5>; else goto <bb 4>; <bb 4>: pretmp_26 = a; goto <bb 6>; <bb 5>: e.2_9 = e; a = e.2_9; __cxa_guard_release (&_ZGVZL1bvE1a); <bb 6>: # prephitmp_27 = PHI <e.2_9(5), pretmp_26(4)> _11 = MEM[(char *)&_ZGVZL1bvE1a]; if (_11 == 0) goto <bb 7>; else goto <bb 9>; <bb 7>: _12 = __cxa_guard_acquire (&_ZGVZL1bvE1a); if (_12 != 0) goto <bb 8>; else goto <bb 9>; <bb 8>: e.2_13 = e; a = e.2_13; __cxa_guard_release (&_ZGVZL1bvE1a); pretmp_5 = prephitmp_27 - e.2_13; <bb 9>: # prephitmp_3 = PHI <0(6), _12(7), pretmp_5(8), 0(2)> return prephitmp_3; There may be a dup but I couldn't find it. It doesn't seem that easy to teach gcc about it. Maybe LTO-inlining of __cxa_guard_* functions would help (or not). We could emit _ZGVZL1bvE1a=1 after the release call, but that seems ugly. We could have special code that, for a MEM_REF[&var42], looks for a dominating call to __cxa_guard_release(&var42), and in that case asserts it is non-zero (is that, or a slight variant, valid?).