https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64454
Bug ID: 64454 Summary: optimize (x%5)%5 Product: gcc Version: 5.0 Status: UNCONFIRMED Keywords: missed-optimization Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: glisse at gcc dot gnu.org unsigned f(unsigned x){ return (x%5)%5; } gives: _2 = x_1(D) % 5; _3 = _2 % 5; return _3; It seems we could easily do better in 2 ways: 1) (x%y)%y could be simplified to x%y in match.pd. This would work even when y is not a constant. 2) with VRP, depending on the interval for x, we may be able to simplify x%CST to just x (or sometimes x+CST).