https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111560
Richard Biener <rguenth at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |rguenth at gcc dot gnu.org Status|UNCONFIRMED |NEW Ever confirmed|0 |1 Last reconfirmed| |2023-09-24 --- Comment #6 from Richard Biener <rguenth at gcc dot gnu.org> --- Confirmed. Simpler testcase: int e,f; void test(int a, int b, int c, int d) { e=a+b+c; //line 5 f=d+b+c; //"b+c" can be replaced with the value at line 5 } it's as Andrew says plus re-associating for global optimal CSE is difficult, imagine adding g = a + b + d; to the above. There's either a + b in common with e but then that breaks commoning b + c in e and f or there is b + d in common with f also breaking the other CSE. Our re-association only produces a canonical order within a single expression. That doesn't by design allow CSE of common subexpressions in other expressions. I don't know of any work (paper) describing how to attack this global optimization problem.