<div dir="ltr"><div><div><div>Thanks, Ayal. I had not seen that API until now.<br><br></div>So...it's a bit of a mess right now. We have:<br>1. RecurrenceDescriptor::isMinMax<wbr>SelectCmpPattern()<br></div>2. ValueTracking's llvm::matchSelectPattern()<br></div>3. At least 3 places in InstCombine that check for select(icmp(x,y) x, y) OR select(icmp(x, y), y, x); grep for:<br>"instruction is used exclusively by a select as" ...<br>  // part of a minimum or maximum operation. If so, refrain from doing<br>  // any other folding. This helps out other analyses which understand<br>  // non-obfuscated minimum and maximum idioms, such as ScalarEvolution<br>  // and CodeGen.<br><div><div><div><br></div><div>I posted a patch for a first step of improving canonicalization here:<br><a href="https://reviews.llvm.org/D26525" target="_blank">https://reviews.llvm.org/D2652<wbr>5</a><br><br></div><div>And I've enhanced ValueTracking in these commits:<br><a href="https://reviews.llvm.org/rL285499" rel="noreferrer" target="_blank">https://reviews.llvm.org/rL285<wbr>499</a><br>
<a href="https://reviews.llvm.org/rL286318" rel="noreferrer" target="_blank">https://reviews.llvm.org/rL286<wbr>318</a><br></div><div><a href="https://reviews.llvm.org/rL286776" rel="noreferrer" target="_blank">https://reviews.llvm.org/<wbr>rL286776</a><br><br></div><div>Should everyone be using the ValueTracking function as the point-of-truth about whether something is min/max? Even with that, the InstCombine bail-out logic seems shaky to me - we probably need to refine where exactly we want to *not* do a transform in order to prevent breaking the min/max idiom.<br></div><div><br></div><div>Does this change anyone's opinion about whether we need min/max intrinsics?<br></div><div><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Nov 14, 2016 at 4:44 AM, Zaks, Ayal <span dir="ltr"><<a href="mailto:ayal.zaks@intel.com" target="_blank">ayal.zaks@intel.com</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 lang="EN-US">
<div class="gmail-m_4309220106392150094gmail-m_-3927048673516503934gmail-m_2781640894313084497WordSection1">
<p class="MsoNormal"><span style="font-size:11pt;font-family:"calibri",sans-serif;color:rgb(31,73,125)">Hal> Noting that all of the above use the same number of IR instructions, I prefer this third option:<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:"calibri",sans-serif;color:rgb(31,73,125)"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:"calibri",sans-serif;color:rgb(31,73,125)">as does RecurrenceDescriptor::isMinMax<wbr>SelectCmpPattern(), which looks for “Select(ICmp(X, Y), X, Y)”.<u></u><u></u></span></p>
<p class="MsoNormal"><a name="m_4309220106392150094_m_-3927048673516503934_m_2781640894313084497__MailEndCompose"><span style="font-size:11pt;font-family:"calibri",sans-serif;color:rgb(31,73,125)"><u></u> <u></u></span></a></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:"calibri",sans-serif;color:rgb(31,73,125)"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:"calibri",sans-serif;color:rgb(31,73,125)">Sanjay></span>
<span style="font-size:11pt;font-family:"calibri",sans-serif;color:rgb(31,73,125)">Another possible consideration is the structure/accuracy of the cost models used by the vectorizers and other passes. I don't think they ever special-case the cmp+sel pair as a possibly
 unified (and therefore cheaper than the sum of the parts) operation.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:"calibri",sans-serif;color:rgb(31,73,125)"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:"calibri",sans-serif;color:rgb(31,73,125)">They call the above to identify min/max reductions; and use TTI::getCmpSelInstrCost(). Special-casing may be redundant if the costs of scalar-vs-vector min/max
 correspond to the costs of scalar-vs-vector cmp&sel.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:"calibri",sans-serif;color:rgb(31,73,125)"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:"calibri",sans-serif;color:rgb(31,73,125)">Ayal.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:"calibri",sans-serif;color:rgb(31,73,125)"><u></u> <u></u></span></p>
<p class="MsoNormal"><a name="m_4309220106392150094_m_-3927048673516503934_m_2781640894313084497______replyseparator"></a><b><span style="font-size:11pt;font-family:"calibri",sans-serif">From:</span></b><span style="font-size:11pt;font-family:"calibri",sans-serif"> llvm-dev [mailto:<a href="mailto:llvm-dev-bounces@lists.llvm.org" target="_blank">llvm-dev-bounces@lists<wbr>.llvm.org</a>]
<b>On Behalf Of </b>Sanjay Patel via llvm-dev<br>
<b>Sent:</b> Tuesday, November 08, 2016 02:13<br>
<b>To:</b> Hal Finkel <<a href="mailto:hfinkel@anl.gov" target="_blank">hfinkel@anl.gov</a>><br>
<b>Cc:</b> llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>><br>
<b>Subject:</b> Re: [llvm-dev] should we have IR intrinsics for integer min/max?<u></u><u></u></span></p><div><div class="gmail-m_4309220106392150094gmail-m_-3927048673516503934gmail-h5">
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<div>
<div>
<p class="MsoNormal">Thanks, Hal and Matt for the feedback. As usual, my instincts about canonicalization were probably wrong. :)<u></u><u></u></p>
</div>
<p class="MsoNormal" style="margin-bottom:12pt"><br>
I thought that @max1 vs. @max3 would be viewed as an unknowable trade-off between reducing the dependency chain and the pseudo-canonical min/max form, so we'd add intrinsics, and defer that decision to the backend.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">I'll wait to see if there are any other arguments presented.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<p class="MsoNormal">@max2 vs. @max3 is a straightforward commute that we should have been doing anyway, so I can start there. Assuming we go with @max3, we need to add something to DAGCombine to turn that back into @max1 (PPC w/ isel and AArch64 do better
 with @max1; x86 is the same).<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal">On Mon, Nov 7, 2016 at 2:47 PM, Hal Finkel <<a href="mailto:hfinkel@anl.gov" target="_blank">hfinkel@anl.gov</a>> wrote:<u></u><u></u></p>
<blockquote style="border-width:medium medium medium 1pt;border-style:none none none solid;border-color:-moz-use-text-color -moz-use-text-color -moz-use-text-color rgb(204,204,204);padding:0cm 0cm 0cm 6pt;margin-left:4.8pt;margin-right:0cm">
<div>
<div>
<p class="MsoNormal"><span style="font-size:10pt;font-family:"arial",sans-serif;color:black"><u></u> <u></u></span></p>
<div class="MsoNormal" style="text-align:center" align="center"><span style="font-size:10pt;font-family:"arial",sans-serif;color:black">
<hr id="gmail-m_4309220106392150094gmail-m_-3927048673516503934gmail-m_2781640894313084497m_455014662074655325zwchr" width="100%" size="2" align="center">
</span></div>
<blockquote style="border-width:medium medium medium 1.5pt;border-style:none none none solid;border-color:-moz-use-text-color -moz-use-text-color -moz-use-text-color rgb(16,16,255);padding:0cm 0cm 0cm 4pt;margin-left:3.75pt;margin-top:5pt;margin-bottom:5pt">
<p class="MsoNormal" style="margin-bottom:12pt"><b><span style="font-family:"helvetica",sans-serif;color:black">From:
</span></b><span style="font-family:"helvetica",sans-serif;color:black">"Sanjay Patel via llvm-dev" <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>><br>
<b>To: </b>"llvm-dev" <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>><br>
<b>Sent: </b>Monday, November 7, 2016 1:01:27 PM<br>
<b>Subject: </b>[llvm-dev] should we have IR intrinsics for integer min/max?<u></u><u></u></span></p>
<div>
<div>
<div>
<div>
<p class="MsoNormal" style="margin-bottom:12pt"><span style="font-family:"helvetica",sans-serif;color:black">Hi -<u></u><u></u></span></p>
</div>
<p class="MsoNormal" style="margin-bottom:12pt"><span style="font-family:"helvetica",sans-serif;color:black">The answer to this question may help to resolve larger questions about intrinsics and vectorization that were discussed at the dev mtg last week,
 but let's start with the basics:<u></u><u></u></span></p>
</div>
<p class="MsoNormal"><span style="font-family:"helvetica",sans-serif;color:black">Which, if any, of these is the canonical IR?<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"helvetica",sans-serif;color:black"><br>
; ret = x < y ? 0 : x-y<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal" style="margin-bottom:12pt"><span style="font-family:"helvetica",sans-serif;color:black">define i32 @max1(i32 %x, i32 %y) {<br>
  %sub = sub nsw i32 %x, %y<br>
  %cmp = icmp slt i32 %x, %y ; cmp is independent of sub<br>
  %sel = select i1 %cmp, i32 0, i32 %sub<br>
  ret i32 %sel<br>
}<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"helvetica",sans-serif;color:black">; ret = (x-y) < 0 ? 0 : x-y<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal" style="margin-bottom:12pt"><span style="font-family:"helvetica",sans-serif;color:black">define i32 @max2(i32 %x, i32 %y) {<br>
  %sub = sub nsw i32 %x, %y<br>
  %cmp = icmp slt i32 %sub, 0 ; cmp depends on sub, but this looks more like a max?<br>
  %sel = select i1 %cmp, i32 0, i32 %sub<br>
  ret i32 %sel<br>
}<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"helvetica",sans-serif;color:black">; ret = (x-y) > 0 ? x-y : 0<u></u><u></u></span></p>
</div>
<div id="gmail-m_4309220106392150094gmail-m_-3927048673516503934gmail-m_2781640894313084497m_455014662074655325DWT18795">
<p class="MsoNormal"><span style="font-family:"helvetica",sans-serif;color:black">define i32 @max3(i32 %x, i32 %y) {<br>
  %sub = sub nsw i32 %x, %y<br>
  %cmp = icmp sgt i32 %sub, 0 ; canonicalize cmp+sel - looks even more like a max?<br>
  %sel = select i1 %cmp, i32 %sub, i32 0<br>
  ret i32 %sel<br>
}<u></u><u></u></span></p>
</div>
</div>
</blockquote>
<p class="MsoNormal"><span style="font-size:10pt;font-family:"arial",sans-serif;color:black">Noting that all of the above use the same number of IR instructions, I prefer this third option:<br>
<br>
 1. It uses fewer values in the icmp/select, so the live range of the x and y, individually, is shorter. This seems like a reasonable metric for simplicity.<br>
 2. Using a comparison of (x-y) against zero likely makes it easier for computing known bits to simply the answer (you only need to compute the sign bit).<br>
 3. The constant of the select, 0, is the second argument (which seems to reflect our general canonical choice).<u></u><u></u></span></p>
<blockquote style="border-width:medium medium medium 1.5pt;border-style:none none none solid;border-color:-moz-use-text-color -moz-use-text-color -moz-use-text-color rgb(16,16,255);padding:0cm 0cm 0cm 4pt;margin-left:3.75pt;margin-top:5pt;margin-bottom:5pt">
<div>
<div>
<p class="MsoNormal"><span style="font-family:"helvetica",sans-serif;color:black"><br>
define i32 @max4(i32 %x, i32 %y) {<br>
  %sub = sub nsw i32 %x, %y<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"helvetica",sans-serif;color:black">  %max = llvm.smax.i32(i32 %sub, i32 0) ; this intrinsic doesn't exist today<u></u><u></u></span></p>
</div>
<div id="gmail-m_4309220106392150094gmail-m_-3927048673516503934gmail-m_2781640894313084497m_455014662074655325DWT18796">
<p class="MsoNormal" style="margin-bottom:12pt"><span style="font-family:"helvetica",sans-serif;color:black">  ret i32 %max<br>
}<u></u><u></u></span></p>
</div>
</div>
</blockquote>
<p class="MsoNormal"><span style="font-size:10pt;font-family:"arial",sans-serif;color:black">I don't currently see the need for a new intrinsic.<u></u><u></u></span></p>
<blockquote style="border-width:medium medium medium 1.5pt;border-style:none none none solid;border-color:-moz-use-text-color -moz-use-text-color -moz-use-text-color rgb(16,16,255);padding:0cm 0cm 0cm 4pt;margin-left:3.75pt;margin-top:5pt;margin-bottom:5pt">
<div>
<div>
<p class="MsoNormal"><span style="font-family:"helvetica",sans-serif;color:black"><u></u> <u></u></span></p>
</div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"><span style="font-family:"helvetica",sans-serif;color:black">FWIW, InstCombine doesn't canonicalize any of the first 3 options currently. Codegen suffers because of that (depending on the target machine and data types). Regardless of the
 IR choice, some backend fixes are needed.<u></u><u></u></span></p>
</div>
<div id="gmail-m_4309220106392150094gmail-m_-3927048673516503934gmail-m_2781640894313084497m_455014662074655325DWT18794">
<p class="MsoNormal"><span style="font-family:"helvetica",sans-serif;color:black"><br>
Another possible consideration is the structure/accuracy of the cost models used by the vectorizers and other passes. I don't think they ever special-case the cmp+sel pair as a possibly unified (and therefore cheaper than the sum of the parts) operation.<u></u><u></u></span></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
<p class="MsoNormal"><span style="font-size:10pt;font-family:"arial",sans-serif;color:black">We don't have a facility currently for the target to provide a cost for combined operations. We should, but there's design work to be done.<br>
<br>
 -Hal<u></u><u></u></span></p>
<blockquote style="border-width:medium medium medium 1.5pt;border-style:none none none solid;border-color:-moz-use-text-color -moz-use-text-color -moz-use-text-color rgb(16,16,255);padding:0cm 0cm 0cm 4pt;margin-left:3.75pt;margin-top:5pt;margin-bottom:5pt">
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal" style="margin-bottom:12pt"><span style="font-family:"helvetica",sans-serif;color:black"><br>
Note that we added FP variants for min/max ops with:<br>
<a href="https://reviews.llvm.org/rL220341" target="_blank">https://reviews.llvm.org/rL220<wbr>341</a><u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"helvetica",sans-serif;color:black"><u></u> <u></u></span></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<p class="MsoNormal"><span style="font-family:"helvetica",sans-serif;color:black"><br>
______________________________<wbr>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><u></u><u></u></span></p>
</blockquote>
<p class="MsoNormal"><span style="font-size:10pt;font-family:"arial",sans-serif;color:rgb(136,136,136)"><br>
<br>
<br>
<span class="gmail-m_4309220106392150094gmail-m_-3927048673516503934gmail-m_2781640894313084497hoenzb">-- <u></u><u></u></span></span></p>
<div>
<p class="MsoNormal"><span style="font-size:10pt;font-family:"arial",sans-serif;color:rgb(136,136,136)">Hal Finkel<br>
Lead, Compiler Technology and Programming Languages<br>
Leadership Computing Facility<br>
Argonne National Laboratory</span><u></u><u></u></p>
</div>
</div>
</div>
</blockquote>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div></div></div>
<p>------------------------------<wbr>------------------------------<wbr>---------<br>
Intel Israel (74) Limited</p>

<p>This e-mail and any attachments may contain confidential material for<br>
the sole use of the intended recipient(s). Any review or distribution<br>
by others is strictly prohibited. If you are not the intended<br>
recipient, please contact the sender and delete all copies.</p></div>

</blockquote></div><br></div></div>