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: unassignedclangb...@nondot.org
          Reporter: nok.ra...@gmail.com
                CC: llvm-bugs@lists.llvm.org, neeil...@live.com,
                    richard-l...@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.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to