[llvm-bugs] [Bug 40611] New: Missed optimization opportunity: transform comparisons with constants to a mask test

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Feb 5 08:58:36 PST 2019


https://bugs.llvm.org/show_bug.cgi?id=40611

            Bug ID: 40611
           Summary: Missed optimization opportunity: transform comparisons
                    with constants to a mask test
           Product: clang
           Version: trunk
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: LLVM Codegen
          Assignee: unassignedclangbugs at nondot.org
          Reporter: nok.raven at gmail.com
                CC: llvm-bugs at lists.llvm.org, neeilans at live.com,
                    richard-llvm at metafoo.co.uk

LLVM does not recognize that:

bool zot(unsigned i) {
    return i == 0 || i == 2;
}

Can be rewritten as:

bool zot_handopt(unsigned i) {
    return !(~2u & i);
}

https://rise4fun.com/Alive/PXsF

With more values the optimization even more useful:

bool ztfos(unsigned i) {
    return i == 0 || i == 2 || i == 4 || i == 6;
}
bool ztfos_handopt(unsigned i) {
    return !(~6u & i);
}

More complex example that greatly benefits from such optimization:

bool is_sign(char c) {
    return c == '-' || c == '+';
}
bool is_sign_handopt(char c) {
    unsigned char i = c;
    i -= '+';
    return !(~2u & i);
}

https://rise4fun.com/Alive/nKIp

GCC and MSVC recognize and optimize all the examples above
https://godbolt.org/z/_Pmy0S
The problem is not target specific (checked x86, x86-64, ppc32, ppc64, arm, and
arm64).

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20190205/259a60f8/attachment.html>


More information about the llvm-bugs mailing list