<div dir="ltr">Hello,<br><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jul 31, 2017 at 9:56 AM, Anastasiya Ruzhanskaya via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><div><div><div><div><div><div><div>Hello,<br></div>I want to know, if I can always assume that when I do unsigned operations like <br><br></div>udiv, urem<br><br></div>I will get the both operands converted to unsigned values?</div></div></div></div></div></div></blockquote><div><br></div><div>Signed-ness in LLVM is not stored into the integer, it is left to interpretation to the instruction that use them.</div><div><br></div><div>udiv / urem will interpret both their operands as unsigned integer. (and the behavior is straight forward)</div><div>sdiv / srem will interpret both their operands as signed integers stored with 2's complement. (and the behavior match the "crappy" behavior of C)<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><div><div><div><div>with under optimized version of code I sometimes receive these lines:<br>        unsigned a = 123;<br>        int b = -2;<br>        int c = a / b;<br>-> %1 = udiv i32 123, -2<br><br></div>and get the result 0. Will it always be zero?</div></div></div></div></div></blockquote><div><br></div><div>Yes. We decided that LLVM IR, when printed for human consumption, would print literal constants as signed integers.</div><div>Here it is -2, but it actually means 4294967294. This number is indubitably bigger than 123.</div><div><br></div><div>udiv i32 -2, -2</div><div>udiv i32 -1, -2</div><div><br></div><div>Are the only ones that will return a non null result, in both cases the result will be 1.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><div><div><div>or is it undefined? Somewhere I have read that it may produce garbage.<br></div>will it be zero in this case : %1 = udiv i32 -123, 2?<br></div></div></div></div></blockquote><div><br></div><div>No.</div><div><br></div><div>%1 = udiv i32 -123, 2 = udiv i32 4294967173, 2 = 2147483586</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><div><div></div><div>However, when I set only the result as unsigned, then :<br>        int a = 123;<br>        int b = -2;<br>        unsigned int c = a / b;<br>-> %1 = sdiv i32 123, -2 <br></div><div>the result seems to be correct. So one operand (not the result variable) needs to be unsigned in order the result was unsigned too? </div></div></div></div></blockquote><div><br></div><div>That is a C semantic. From what I remember, if one of the operand is unsigned, then the computation will be unsigned.</div><div>The return value type does not influence the type of the operation. But it might introduce a final cast.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><div>What should I expect from sign of the operand if I get the line:<br>%1 = udiv i32 %a.o, 2. Can it in this case be negative. Or this situation is only the result of under optimization and zero value is ok?<br></div></div></div></blockquote><div><br></div><div>The result cannot be "negative". Because negative numbers start with a 1, and a udiv by 2 is equal to a logical shift right by 1 bit, which introduce a 0 as first bit.</div><div>Which means that if a later operand interpret %1 as a signed integer, it will still be a positive value. It can be zero if %a.o is 0 or 1.</div><div><br></div><div>Note that if %a.o was produced by a "signed" operation, and you see the result as a negative number, %a.o can grow in size (because the number will now become positive if interpreted as signed number).</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><div></div>These questions may be related mostly to c, but still, perhaps llvm has some additional rules that I didn't find. The general thing I wanted to know what to expect from sign of value when I get urem/udiv ops in final code? <br></div></div></blockquote><div><br></div><div>It depends what you mean by "sign of value". Again, in llvm, integer data do not have a sign. You will see that there is no sign-cast instruction.</div><div>Some instruction like add/sub/mul/etc... are sign-oblivious and do not need to distinguish between signed/unsigned kind.<br></div><div>While udiv/sdiv, urem/srem, etc... are different depending if you take into account a signed-ness of their operands or not.</div><div><br></div><div>In LLVM, sign of numbers interpreted as signed values follow 2's complement. (0 is positive, and there the number of maximum absolute value is negative and do not have a positive equivalent, which is annoying when considering signed division by -1).</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div></div>Thank you in advance for the answer.<br></div>
<br>______________________________<wbr>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><b>Alexandre Isoard</b><br></div></div>
</div></div>