[cfe-dev] bitwise ops on booleans

Sanjay Patel spatel at rotateright.com
Fri Jun 26 11:51:38 PDT 2015

Hi Language Lawyers!

In PR23827 ( https://llvm.org/bugs/show_bug.cgi?id=23827 ), a bitwise op on
booleans is considered equivalent to a logical op:

  if ((x < 3) & (y > 10))

effectively becomes:

  if ((x < 3) && (y > 10))

where x and y are of type 'int'. The second statement (&&) requires
short-circuit evaluation to bypass the y comparison when the x comparison
is false (creates an extra branch vs. the original code).

>From N4296 -
4.5 Integral Promotions:
"A prvalue of type bool can be converted to a prvalue of type int, with
false becoming zero and true becoming one."

5.11 Bitwise AND operator:
"The usual arithmetic conversions are performed; the result is the bitwise
AND function of the operands."

Is the type promotion optional ("can be")? Under what conditions would the
promotion be performed?

Assuming the transform is correct, what is the recommended way to write
this in C/C++ to achieve the desired effect: we want both comparisons to be
evaluated (do *not* want short-circuiting)?

FWIW, the IR coming out of clang looks like what I expect: the i1 types are
zexted and fed into an 'and i32'. It's the IR and backend optimizations
that are surprising me.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150626/2fa57b85/attachment.html>

More information about the cfe-dev mailing list