Bug 101252 - Optimize (b ? i % C0 : i % C1) into i & (b ? C0-1 : C1-1) for power of 2 C0 and C1
Summary: Optimize (b ? i % C0 : i % C1) into i & (b ? C0-1 : C1-1) for power of 2 C0 a...
Status: RESOLVED DUPLICATE of bug 101179
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 12.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2021-06-29 08:44 UTC by Antony Polukhin
Modified: 2021-07-01 20:43 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Antony Polukhin 2021-06-29 08:44:50 UTC
Consider the following code

bool test_naive0(bool b, short i) {
    return (b ? i % 4 : i % 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/8vj999M3c

P.S.: Inspired by the manual optimizations in libstdc++ https://github.com/gcc-mirror/gcc/commit/b92d12d3fe3f1aa56d190d960e40c62869a6cfbb
Comment 1 Andrew Pinski 2021-07-01 20:43:07 UTC
Dup of bug 101179.

*** This bug has been marked as a duplicate of bug 101179 ***