https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89847
Bug ID: 89847 Summary: Simplify subexpressions of % constant Product: gcc Version: 9.0 Status: UNCONFIRMED Keywords: missed-optimization Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: jakub at gcc dot gnu.org CC: unassigned at gcc dot gnu.org Depends on: 89845 Target Milestone: --- +++ This bug was initially created as a clone of Bug #89845 +++ The https://arxiv.org/pdf/1902.01961.pdf paper also mentions clang is able to optimize: int f1 (int x) { return (31 * x + 27961) & 15; } unsigned f2 (unsigned x) { return (31 * x + 27961) & 15; } into: return (9 - x) & 15; while gcc can't. Of course it should be done only if the operand of %/& (or narrowing cast) isn't used multiple times (to be precise, isn't used outside of the &/&/cast operand) and after giving say SCCVN a chance to merge the same computations from multiple different spots, but then we can really simplify the constants there (modulo the outer constant) and multiplications by constants etc. Referenced Bugs: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89845 [Bug 89845] Consider improving division and modulo by constant if highpart multiply is cheap