https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98292
Bug ID: 98292 Summary: Optimize away C return; in function returning integral/pointer Product: gcc Version: 11.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: jakub at gcc dot gnu.org Target Milestone: --- This is related to the PR94779, but that one talks just about switches. int foo (int x) { if (x > 20) return x; } int bar (int x) { if (x > 30) return 30; } int baz (int x) { if (x > 40) return x + 1U; } int qux (int x) { if (x > 50) return x | 32; } is optimized to just the return statement in C++ (because we add __builtin_unreachable() in that case), but not in C. I think we should do that also in C, provided that there are no non-debug non-CLOBBER stmts on the branch with GIMPLE_RETURN without argument, and provided that on the other branch there are just very few cheap stmts guaranteed not to invoke UB/trap (appart from debug/CLOBBER stmts) plus the GIMPLE_RETURN with argument. LLVM seems to optimize that (though not sure if it isn't because it incorrectly adds something like __builtin_unreachable () even for C).