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

Stephen Canon via cfe-dev cfe-dev at lists.llvm.org
Fri Sep 13 13:52:06 PDT 2019


The C and C++ language standards fully specify this behavior. There’s no flexibility here to change it.

– Steve

> On Sep 13, 2019, at 4:49 PM, Alexandre Isoard via cfe-dev <cfe-dev at lists.llvm.org> wrote:
> 
> 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 <mailto: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
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

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


More information about the cfe-dev mailing list