[cfe-dev] int-to-bool conversion issues

Alexandre Isoard via cfe-dev cfe-dev at lists.llvm.org
Fri Sep 13 13:49:59 PDT 2019


Sorry for the typo:

false → 0b0000 → 0b1111 → true
true → 0b0001 → 0b1110 → true

On Fri, Sep 13, 2019 at 1:38 PM Alexandre Isoard <alexandre.isoard at gmail.com>
wrote:

> Hello,
>
> I encountered a really annoying issue, I am not sure it is Clang or C++
> language issue.
>
> But basically, the following code:
>
> bool foo(bool a) {
> return ~a;
> }
>
> Collapse into:
>
> bool foo(bool a) {
> return true;
> }
>
> The explanation is simple:
> - a is implicitly converted to int using zext
> - int operator~(int) has the unexpected behavior described below
> - then icmp ne 0 is used to convert to bool and is always true
>
> Here is the truth table of bool-to-int + operator~(int) + int-to-bool:
> false → 0b0000 → 0b0000 → true
> true → 0b0001 → 0b1110 → true
>
> Note that we can solve the issue by using sext for bool-to-int conversions.
> Should we? Is this a known problem?
>
> --
> *Alexandre Isoard*
>


-- 
*Alexandre Isoard*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20190913/c58d09b3/attachment.html>


More information about the cfe-dev mailing list