https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101251
Bug ID: 101251
Summary: Optimize i % (b ? C0 : C1) into i & (b ? C0-1 : C1-1)
for power of 2 C0 and C1
Product: gcc
Version: 12.0
Status: UNCONFIRMED
Keywords: missed-optimization
Severity: normal
Priority: P3
Component: middle-end
Assignee: unassigned at gcc dot gnu.org
Reporter: antoshkka at gmail dot com
Target Milestone: ---
Consider the following code
bool test_optim01(bool b, short i) {
return i % (b ? 4 : 16)==0;
}
It could be optimized into
bool test_optim0(bool b, short i) {
return (i & (b ? 15 : 3))==0;
}
Godbolt playground: https://godbolt.org/z/j15br4Kd4
P.S.: Inspired by the manual optimizations in libstdc++
https://github.com/gcc-mirror/gcc/commit/b92d12d3fe3f1aa56d190d960e40c62869a6cfbb