https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104292

            Bug ID: 104292
           Summary: [missed optimization] boolean addition generates
                    suboptimal code
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: avi at scylladb dot com
  Target Milestone: ---

bool bool_or(bool b1, bool b2) {
    return b1 + b2;
}

generates

bool_or(bool, bool):
        movzbl  %dil, %edi
        movzbl  %sil, %esi
        addl    %esi, %edi
        setne   %al
        ret

Whereas it could generate

bool_or(bool, bool):
        or   %edi, %esi
        mov  %esi, %eax
        ret

For a net gain of two instructions (and the final mov can often be elided, so
up to three).

I encountered this while using std::plus<> with boolean types. It could be
optimized as a specialization of std::plus, but I think a magic transformation
in the optimizer would make it apply in more places.

Reply via email to