<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jul 7, 2015 at 7:33 AM, Robison, Arch <span dir="ltr"><<a href="mailto:arch.robison@intel.com" target="_blank">arch.robison@intel.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div lang="EN-US" link="blue" vlink="purple">
<div>
<p class="MsoNormal"><span style="font-size:10pt;font-family:Verdana,sans-serif">I concur with your analysis.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:Verdana,sans-serif"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:Verdana,sans-serif">I was planning to just extend the applicability of the current transform by just taking on more cases where the inverse image is a single value. Though if someone else wants
to take on the general range case, I’m happy to step aside. :-)</span></p></div></div></blockquote><div><br></div><div>If you have access to the test case for which this optimization was missing cases (at least -0), can you hack in some code in FoldFCmp_IntToFP_Cst to see how many times we are actually bailing out of here in your test case due to not handling the fully general case? If it's a lot, it is probably worth filing a bugzilla (please CC me if you do).</div><div><br></div><div>-- Sean Silva</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div lang="EN-US" link="blue" vlink="purple"><div><p class="MsoNormal"><span style="font-size:10pt;font-family:Verdana,sans-serif"><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:Verdana,sans-serif"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:Verdana,sans-serif">- Arch<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:Verdana,sans-serif"><u></u> <u></u></span></p>
<p class="MsoNormal"><b><span style="font-size:11pt;font-family:Calibri,sans-serif">From:</span></b><span style="font-size:11pt;font-family:Calibri,sans-serif"> Sean Silva [mailto:<a href="mailto:chisophugis@gmail.com" target="_blank">chisophugis@gmail.com</a>]
<br>
<b>Sent:</b> Monday, July 6, 2015 6:10 PM<br>
<b>To:</b> Robison, Arch<span class=""><br>
<b>Cc:</b> <a href="mailto:llvmdev@cs.uiuc.edu" target="_blank">llvmdev@cs.uiuc.edu</a><br>
<b>Subject:</b> Re: [LLVMdev] Why can't comparisons with negative zero be simplified?<u></u><u></u></span></span></p>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal">The way FoldFCmp_IntToFP_Cst works is confusing to me. What it is trying to do basically boils down to taking the set of real numbers where fcmp(?, Cst) is true, then finding the inverse image under the mapping [us]itofp of that set (i.e.
all the integers (of the relevant type) that can map to values in that set under the operation [us]itofp). That inverse image is guaranteed to be contiguous, so at most two integer comparisons suffice. If the inverse image is just a single value, then a single
equality comparison suffices. If the inverse image includes the {maximal,minimal} {signed,unsigned} integer, then a single integer order comparison suffices.<u></u><u></u></p><div><div class="h5">
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">So e.g. consider<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> fcmp oeq double (uitofp x) Cst<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">With Cst = 2^62 + ulp(2^62)), i.e. 2^62*(0b1.0000....1), where the final 1 is in the least significant bit.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Then the set of real numbers for which `fcmp oeq double roundToFloat(realNumber) Cst` yields true is the open interval (a,b) where<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">a = 2^62 + .5*ulp(2^62)<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">b = 2^62 + 1.5*ulp(2^62)<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">i.e. the open interval bracketed by Cst +/- .5*ulp(Cst)<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">(assuming round to nearest ties to even rounding mode)<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">However, since ulp(Cst) = ulp(2^62) = 2^62 * 2^-52 = 2^10, this means that this range covers<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">(2^62 + (1/2)*2^10, 2^62 + (3/2)*2^10) = [2^62 + (1/2)*2^10 + 1, 2^62 + (3/2)*2^10 - 1] which contains 1022 integers, so a range comparison is needed.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">(note: for Cst = 2^62, the interval is only half as large on the low side since Cst lies on the boundary where the exponent changes, i.e. the interval is [Cst - .25*ulp(Cst), Cst + .5*ulp(Cst)]; the closed interval is due to 2^62 being
even)<u></u><u></u></p>
</div>
<div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">This of course requires baking in a rounding mode, but we are already doing that anyway.<u></u><u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">-- Sean Silva<u></u><u></u></p>
</div>
</div>
</div>
</div></div></div><div><div class="h5">
<div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal">On Mon, Jul 6, 2015 at 2:13 PM, Robison, Arch <<a href="mailto:arch.robison@intel.com" target="_blank">arch.robison@intel.com</a>> wrote:<u></u><u></u></p>
<blockquote style="border-style:none none none solid;border-left-color:rgb(204,204,204);border-left-width:1pt;padding:0in 0in 0in 6pt;margin-left:4.8pt;margin-right:0in">
<div>
<div>
<p class="MsoNormal"><span style="font-size:10pt;font-family:Verdana,sans-serif">In InstCombineCompares.cpp, routine InstCombiner::FoldFCmp_IntToFP_Cst, there are these lines:</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:Verdana,sans-serif"> </span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:0.5in">
<span style="font-size:10pt;font-family:Verdana,sans-serif"> // Comparisons with zero are a special case where we know we won't lose</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:0.5in">
<span style="font-size:10pt;font-family:Verdana,sans-serif"> // information.</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:0.5in">
<span style="font-size:10pt;font-family:Verdana,sans-serif"> bool IsCmpZero = RHS.isPosZero();</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:0.5in">
<span style="font-size:10pt;font-family:Verdana,sans-serif"> </span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:0.5in">
<span style="font-size:10pt;font-family:Verdana,sans-serif"> // If the conversion would lose info, don't hack on this.</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:0.5in">
<span style="font-size:10pt;font-family:Verdana,sans-serif"> if ((int)InputSize > MantissaWidth && !IsCmpZero)</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:0.5in">
<span style="font-size:10pt;font-family:Verdana,sans-serif"> return nullptr;</span><u></u><u></u></p>
<p class="MsoNormal"><b><span style="font-size:10pt;font-family:Verdana,sans-serif"> </span></b><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:Verdana,sans-serif">Why check for positive zero instead of checking for any kind of zero? My reading of IEEE 754-2008 is that floating-point
comparison operations cannot distinguish a negative zero from a positive zero. Further supporting this is that fact that
<a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_docs_LangRef.html&d=AwMFAg&c=8hUWFZcy2Z-Za5rBPlktOQ&r=Mfk2qtn1LTDThVkh6-oGglNfMADXfJdty4_bhmuhMHA&m=ZaWNNVon_US-YjLoXztqD5ve4xvIIhIXYdnk3Xqwz8w&s=xonYObEo1TXUyjK3RYC4Pg3kAaSvaYy_Lnvaiq5G1N0&e=" target="_blank">
http://llvm.org/docs/LangRef.html</a> describes the difference between “ordered” and “unordered” as pertaining to QNAN operands,</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:Verdana,sans-serif">with no mention of negative zero.</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:Verdana,sans-serif"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:Verdana,sans-serif">I tried fixing the issue, but then the following test in cast-int-fcmp-eq-0.ll fails:</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:Verdana,sans-serif"> </span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:0.5in">
<span style="font-size:10pt;font-family:Verdana,sans-serif">; CHECK-LABEL: @i32_cast_cmp_oeq_int_n0_uitofp(</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:0.5in">
<span style="font-size:10pt;font-family:Verdana,sans-serif">; CHECK: uitofp</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:0.5in">
<span style="font-size:10pt;font-family:Verdana,sans-serif">; CHECK: fcmp oeq</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:0.5in">
<span style="font-size:10pt;font-family:Verdana,sans-serif">define i1 @i32_cast_cmp_oeq_int_n0_uitofp(i32 %i) {</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:0.5in">
<span style="font-size:10pt;font-family:Verdana,sans-serif"> %f = uitofp i32 %i to float</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:0.5in">
<span style="font-size:10pt;font-family:Verdana,sans-serif"> %cmp = fcmp oeq float %f, -0.0</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:0.5in">
<span style="font-size:10pt;font-family:Verdana,sans-serif"> ret i1 %cmp</span><u></u><u></u></p>
<p class="MsoNormal" style="margin-left:0.5in">
<span style="font-size:10pt;font-family:Verdana,sans-serif">}</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:Verdana,sans-serif"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:Verdana,sans-serif">Is this test really justified, or is it just reinforcing an oversight?</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:Verdana,sans-serif"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:Verdana,sans-serif">- Arch D. Robison</span><u></u><u></u></p>
</div>
</div>
<p class="MsoNormal" style="margin-bottom:12pt"><br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu" target="_blank">LLVMdev@cs.uiuc.edu</a> <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><u></u><u></u></p>
</blockquote>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div></div></div>
</div>
</blockquote></div><br></div></div>