https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111167
Bug ID: 111167 Summary: swapping around duplicated conditionals can produce better code Product: gcc Version: 14.0 Status: UNCONFIRMED Keywords: missed-optimization Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: pinskia at gcc dot gnu.org Target Milestone: --- Take reduced from GCC's sources for lookup_attribute: ``` void f(int); inline void canonicalize_attr_name(const char *&s, int l) { if (l > 4 && s[0] == '_' && s[l - 1] == '_' ) s += 2; } void lookup_attribute(const char *attr_ns, int list, int t) { if (attr_ns && attr_ns[0] != '_') { canonicalize_attr_name(attr_ns, 5); } if (list == 0) { short attr_ns_len = attr_ns ? t : 0; f(attr_ns_len); } } ``` In optimized we have: ``` if (attr_ns_25(D) != 0B) goto <bb 4>; [70.00%] else goto <bb 3>; [30.00%] <bb 3> [local count: 322122543]: if (list_10(D) == 0) goto <bb 6>; [50.00%] else goto <bb 7>; [50.00%] <bb 4> [local count: 751619279]: if (list_10(D) == 0) goto <bb 5>; [50.00%] else goto <bb 7>; [50.00%] ``` But we should really just have: if (list_10(D) == 0) goto bb7; if (attr_ns_25(D) != 0B) goto bb5; else bb6;