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

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


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*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20190913/b471a2a6/attachment.html>


More information about the cfe-dev mailing list