llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.06k stars 11.59k forks source link

[clang] missed optimization: bool addition not converted to boolean or #53502

Open avikivity opened 2 years ago

avikivity commented 2 years ago
bool bool_or(bool b1, bool b2) {
    return b1 + b2;
}

generates (clang 13)

bool_or(bool, bool):                           # @bool_or(bool, bool)
        negl    %esi
        cmpl    %esi, %edi
        setne   %al
        retq

However it can remove an instruction:

bool_or(bool, bool):                           # @bool_or(bool, bool)
        orl     %edi, %esi
        mov     %esi, %ax
        retq

Boolean multiplication generates an andl already.

This can happen when feeding std::plus to things like std::reduce.

RKSimon commented 2 years ago

CC @LebedevRI @rotateright

https://alive2.llvm.org/ce/z/NgxzfL

rotateright commented 2 years ago

There's a question of why we end up with mismatched extends on this pattern, but ultimately, we are missing combines for all IR predicates. A full solution should work out the truth table equivalents, so we don't miss anything.

For example, 'sgt' can also become 'or': https://alive2.llvm.org/ce/z/NkB_eB