[cfe-dev] bitwise ops on booleans
chisophugis at gmail.com
Fri Jun 26 16:21:21 PDT 2015
On Fri, Jun 26, 2015 at 11:51 AM, Sanjay Patel <spatel at rotateright.com>
> 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?
"can be" here just means "this is one of the available conversions".
> 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)?
This all falls under the as-if rule; there is nothing the user can do that
will *require* the compiler to do either thing.
-- Sean Silva
> 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.
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the cfe-dev