https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96187
Bug ID: 96187 Summary: GCC at -O2 generates branch for code that should be branch-free Product: gcc Version: 10.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: philip.zander at googlemail dot com Target Milestone: --- Created attachment 48870 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=48870&action=edit Test case The attached code contains two related functions, get_length2() and get_length4(). For get_length4(), GCC generates branch-free instructions as expected, but for get_length2() it introduces a branch, which is unfortunate because the code was specifically written to avoid branches. The target is x86_64. You can compare the output here: https://godbolt.org/z/bhEzhY This happens only at -O2, not at -O1 or -O3. It seems to have started with GCC 7, as GCC 6.* is the last version on Compiler Explorer that generates branch-free code for both functions. PS: I hope "tree-optimization" is the right component, I'm not a compiler expert. If the problem lies elsewhere, please edit my report.