Alberto Barbaro via llvm-dev
2019-Mar-22 08:09 UTC
[llvm-dev] Understand if a condition was true or false
Hi all, I have the condition of an ICmpInst in a variable called condition obtained doing GenericValue condition = getOperandValue(&I, SF); 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? I'm not sure because I was expecting something different and I'm a bit lost Should I use another approach? Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190322/2c9287fc/attachment.html>
Tim Northover via llvm-dev
2019-Mar-22 09:32 UTC
[llvm-dev] Understand if a condition was true or false
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 lostI 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.
Alberto Barbaro via llvm-dev
2019-Mar-22 10:06 UTC
[llvm-dev] Understand if a condition was true or false
Thanks Tim, I think I'll go for getZExtValue as you suggested. Anyway saw also getBoolValue that I can use. Thanks again for your help On Fri, Mar 22, 2019, 09:32 Tim Northover <t.p.northover at gmail.com> wrote:> 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. >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190322/c7c542af/attachment-0001.html>