https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92641
Bug ID: 92641 Summary: Function called from dead branch Product: gcc Version: 9.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: middle-end Assignee: unassigned at gcc dot gnu.org Reporter: sagebar at web dot de Target Milestone: --- Created attachment 47339 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=47339&action=edit Listing of problematic code, and similar code that works correctly Given something like this:``` extern unsigned int get_vla_size(void); ``` This is ok:``` if (0) { (void)(int(*)[get_vla_size()])0; } else { (void)get_vla_size(); } ``` asm:``` call _Z12get_vla_sizev ``` However, re-writing this code to use the ?-operator causes `get_vla_size()` to be called twice:``` 0 ? ( (void)(int(*)[get_vla_size()])0 ) : ( (void)get_vla_size() ); ``` asm:``` call _Z12get_vla_sizev call _Z12get_vla_sizev ``` Note that the first call to `get_vla_size()` happens from within a dead branch, meaning that the call should have been fully removed, or at the very least have been skipped by an unconditional jump. When the first branch is wrapped in statement-expressions, code once again function as expected:``` 0 ? ({ (void)(int(*)[get_vla_size()])0; }) : ( (void)get_vla_size() ); ``` asm:``` call _Z12get_vla_sizev ``` This problem consistently appears when compiling for c++, regardless of optimization level, and if I had to guess, it's probably related to the scope in which some given type is declared, where `type-expr` in `{ int x; 0 ? (type-expr)expr : expr; }` is declared in the same scope as `x`, and thereby unconditionally initialized. The problem can be reproduced using:``` g++ -S attached-file.cc && cat attached-file.s ``` and inspecting the produced output Note: I hope that `middle-end` was the correct place to report this, and I'm sorry if it isn't.