[llvm-dev] Understand if a condition was true or false

Tim Northover via llvm-dev llvm-dev at lists.llvm.org
Fri Mar 22 02:32:23 PDT 2019


On Fri, 22 Mar 2019 at 08:09, Alberto Barbaro via llvm-dev
<llvm-dev at lists.llvm.org> wrote:
> Now I would like to understand if the comparison was true or false. Is it correct to use condition.IntVal.getSExtValue() and if the value is 0 consider the true and if the value is -1 consider the condition false?

The result of an icmp is a 1-bit integer. As a pure bag-of-bits 0b0 is
false and 0b1 is true. If you interpret those as signed integers then
0b1 is actually -1 though, so the condition is reversed from what you
said above.

It would probably be more natural if you used getZExtValue, which
zero-extends the 1-bit answer and gives the much more natural result
that 0 == false, 1 == true.

> I'm not sure because I was expecting something different and I'm a bit lost

I assume you were expecting 0 or 1 and aren't sure how to interpret
the -1? It's really just a quirk of 2s-complement representation LLVM
uses (together with most CPUs) that a 1-bit signed value doesn't
actually have any positive values and the single non-zero one is -1.

Cheers.

Tim.


More information about the llvm-dev mailing list