<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
pre
        {mso-style-priority:99;
        mso-style-link:"HTML Preformatted Char";
        margin:0cm;
        margin-bottom:.0001pt;
        font-size:10.0pt;
        font-family:"Courier New";}
span.HTMLPreformattedChar
        {mso-style-name:"HTML Preformatted Char";
        mso-style-priority:99;
        mso-style-link:"HTML Preformatted";
        font-family:"Consolas",serif;}
span.EmailStyle19
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri",sans-serif;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">Hi Andrea,<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">Thank you very much for pointing out this bug. I reverted this commit in r299368 and will give it another try with the correct transformation.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">I have a patch with a similar simplification in InstSimplify under review,
<a href="https://reviews.llvm.org/D31527">D31527</a>. Will make sure it is not suffering from the same issue and at the least add a negative test to cover the problem you showed here.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">Thanks, Zvi<o:p></o:p></span></p>
<p class="MsoNormal"><a name="_MailEndCompose"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></a></p>
<p class="MsoNormal"><a name="_____replyseparator"></a><b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">From:</span></b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"> Andrea Di Biagio [mailto:andrea.dibiagio@gmail.com]
<br>
<b>Sent:</b> Monday, April 03, 2017 18:55<br>
<b>To:</b> Rackover, Zvi <zvi.rackover@intel.com><br>
<b>Cc:</b> llvm-commits <llvm-commits@lists.llvm.org><br>
<b>Subject:</b> Re: [llvm] r299047 - [DAGCombine] A shuffle of a splat is always the splat itself<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt">Hi Zvi (and Sanjay),<o:p></o:p></p>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt">There is a problem with this patch.
<br>
Checking for `isSplat()` is not enough, we also need to check if the splat mask contains undef indices. Otherwise, we risk to incorrectly propagate undef to users of the folded splat.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">Consider the following example:<br>
<br>
<br>
<o:p></o:p></p>
<pre>t49: v4i32 = vector_shuffle<0,2,u,u> t58, undef:v4i32<o:p></o:p></pre>
<pre> t58: v4i32 = vector_shuffle<2,u,2,u> t13, undef:v4i32<o:p></o:p></pre>
<pre><br><span style="font-family:"Arial",sans-serif">A shuffle with interleaved `undef` indices is often introduced by the type legalizer when promoting from an illegal vector type to another types with same number of elements but wider element type.<br><br>In this case, the lower half of t49 is defined, while the upper half of t49 is undef.<br>This patch incorrectly simplifies that shuffle as follows:<br></span><br>t58: v4i32 = vector_shuffle<2,u,2,u> t13, undef:v4i32<o:p></o:p></pre>
<pre><span style="font-family:"Arial",sans-serif"><br>This is problematic in the following case:<br></span><br>t51: i64 = extract_vector_elt t50, Constant:i64<0><br> t50: v2i64 = bitcast t49<br>  t49: v4i32 = vector_shuffle<0,2,u,u> t58, undef:v4i32<o:p></o:p></pre>
<pre>   t58: v4i32 = vector_shuffle<2,u,2,u> t13, undef:v4i32<span style="font-family:"Arial",sans-serif"><o:p></o:p></span></pre>
<pre><o:p> </o:p></pre>
<pre id="gmail-comment_text_3"><span style="font-family:"Arial",sans-serif">This patch makes the upper half of t51 undefined. However, in the original code, the entire t51 was defined.<br>Here is another interesting case:<br></span><br>t51: i32 = extract_vector_elt t49, Constant:i64<0><br>t50: i32 = extract_vector_elt t49, Constant:i64<1><br> t49: v4i32 = vector_shuffle<0,2,u,u> t58, undef:v4i32<o:p></o:p></pre>
<pre>  t58: v4i32 = vector_shuffle<2,u,2,u> t13, undef:v4i32<span style="font-family:"Arial",sans-serif"><o:p></o:p></span></pre>
<pre id="gmail-comment_text_3"><o:p> </o:p></pre>
<pre id="gmail-comment_text_3"><span style="font-family:"Arial",sans-serif">Before this patch, both t50 and t51 were defined, and the extracted value was the same. <br>With this patch, we now have this:<br></span><br>t51: i32 = extract_vector_elt t58, Constant:i64<0><br>t50: i32 = extract_vector_elt t58, Constant:i64<1><br> t58: v4i32 = vector_shuffle<2,u,2,u> t13, undef:v4i32<span style="font-family:"Arial",sans-serif"><o:p></o:p></span></pre>
<pre style="margin-bottom:12.0pt"><span style="font-family:"Arial",sans-serif"><br>Here, t50 is undef and can be folded away.</span><o:p></o:p></pre>
<pre id="gmail-comment_text_3"><span style="font-family:"Arial",sans-serif">Here is a small reproducible:<br><br>// ===========================================================================<br>extern "C" int printf(const char*, ...);<br><br>using v2 = unsigned int __attribute__((__vector_size__(8)));<br>using v4 = unsigned int __attribute__((__vector_size__(16)));<br><br>int main() {<br>  v4 alpha = (v4){1, 2, 3, 4};<br>  v2 bravo = __builtin_shufflevector(alpha, alpha, 2, 2);<br>  printf("<%x %x>\n", bravo[0], bravo[1]);<br>}<br>// ===========================================================================</span><o:p></o:p></pre>
<pre id="gmail-comment_text_3"><span style="font-family:"Arial",sans-serif">At runtime, the executable built at -O0 (-target x86_64-unknown-linux-gnu) prints out: <3 4></span><o:p></o:p></pre>
<pre style="margin-bottom:12.0pt" id="gmail-comment_text_3"><span style="font-family:"Arial",sans-serif">At  -O2, it would print <3 3> (this is correct).<br><br></span><o:p></o:p></pre>
<pre id="gmail-comment_text_3"><span style="font-family:"Arial",sans-serif">-Andrea</span><o:p></o:p></pre>
</div>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal">On Thu, Mar 30, 2017 at 2:42 AM, Zvi Rackover via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>> wrote:<o:p></o:p></p>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm">
<p class="MsoNormal">Author: zvi<br>
Date: Wed Mar 29 20:42:57 2017<br>
New Revision: 299047<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=299047&view=rev" target="_blank">
http://llvm.org/viewvc/llvm-project?rev=299047&view=rev</a><br>
Log:<br>
[DAGCombine]  A shuffle of a splat is always the splat itself<br>
<br>
Summary:<br>
Add a simplification:<br>
shuffle (splat-shuffle), undef, M --> splat-shuffle<br>
<br>
Fixes pr32449<br>
<br>
Patch by Sanjay Patel<br>
<br>
Reviewers: eli.friedman, RKSimon, spatel<br>
<br>
Reviewed By: spatel<br>
<br>
Subscribers: llvm-commits<br>
<br>
Differential Revision: <a href="https://reviews.llvm.org/D31426" target="_blank">
https://reviews.llvm.org/D31426</a><br>
<br>
Modified:<br>
    llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp<br>
    llvm/trunk/test/CodeGen/X86/shuffle-of-splat-multiuses.ll<br>
<br>
Modified: llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp?rev=299047&r1=299046&r2=299047&view=diff" target="_blank">
http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp?rev=299047&r1=299046&r2=299047&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp (original)<br>
+++ llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp Wed Mar 29 20:42:57 2017<br>
@@ -14643,6 +14643,12 @@ SDValue DAGCombiner::visitVECTOR_SHUFFLE<br>
       return DAG.getVectorShuffle(VT, SDLoc(N), N0, N1, NewMask);<br>
   }<br>
<br>
+  // A shuffle of a splat is always the splat itself:<br>
+  // shuffle (splat-shuffle), undef, M --> splat-shuffle<br>
+  if (auto *N0Shuf = dyn_cast<ShuffleVectorSDNode>(N0))<br>
+    if (N1.isUndef() && N0Shuf->isSplat())<br>
+      return N0;<br>
+<br>
   // If it is a splat, check if the argument vector is another splat or a<br>
   // build_vector.<br>
   if (SVN->isSplat() && SVN->getSplatIndex() < (int)NumElts) {<br>
<br>
Modified: llvm/trunk/test/CodeGen/X86/shuffle-of-splat-multiuses.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/shuffle-of-splat-multiuses.ll?rev=299047&r1=299046&r2=299047&view=diff" target="_blank">
http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/shuffle-of-splat-multiuses.ll?rev=299047&r1=299046&r2=299047&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/CodeGen/X86/shuffle-of-splat-multiuses.ll (original)<br>
+++ llvm/trunk/test/CodeGen/X86/shuffle-of-splat-multiuses.ll Wed Mar 29 20:42:57 2017<br>
@@ -5,9 +5,8 @@<br>
 define <2 x double> @foo2(<2 x double> %v, <2 x double> *%p) nounwind {<br>
 ; AVX2-LABEL: foo2:<br>
 ; AVX2:       # BB#0:<br>
-; AVX2-NEXT:    vpermilpd {{.*#+}} xmm1 = xmm0[1,1]<br>
-; AVX2-NEXT:    vpermilpd {{.*#+}} xmm0 = xmm1[1,0]<br>
-; AVX2-NEXT:    vmovapd %xmm1, (%rdi)<br>
+; AVX2-NEXT:    vpermilpd {{.*#+}} xmm0 = xmm0[1,1]<br>
+; AVX2-NEXT:    vmovapd %xmm0, (%rdi)<br>
 ; AVX2-NEXT:    retq<br>
   %res = shufflevector <2 x double> %v, <2 x double> undef, <2 x i32> <i32 1, i32 1><br>
   %res1 = shufflevector<2 x double> %res, <2 x double> undef, <2 x i32> <i32 1, i32 undef><br>
@@ -18,9 +17,8 @@ define <2 x double> @foo2(<2 x double> %<br>
 define <4 x double> @foo4(<4 x double> %v, <4 x double> *%p) nounwind {<br>
 ; AVX2-LABEL: foo4:<br>
 ; AVX2:       # BB#0:<br>
-; AVX2-NEXT:    vpermpd {{.*#+}} ymm1 = ymm0[2,2,2,2]<br>
-; AVX2-NEXT:    vpermpd {{.*#+}} ymm0 = ymm1[2,0,2,3]<br>
-; AVX2-NEXT:    vmovapd %ymm1, (%rdi)<br>
+; AVX2-NEXT:    vpermpd {{.*#+}} ymm0 = ymm0[2,2,2,2]<br>
+; AVX2-NEXT:    vmovapd %ymm0, (%rdi)<br>
 ; AVX2-NEXT:    retq<br>
   %res = shufflevector <4 x double> %v, <4 x double> undef, <4 x i32> <i32 2, i32 2, i32 2, i32 2><br>
   %res1 = shufflevector<4 x double> %res, <4 x double> undef, <4 x i32> <i32 2, i32 0, i32 undef, i32 undef><br>
@@ -32,10 +30,8 @@ define <8 x float> @foo8(<8 x float> %v,<br>
 ; AVX2-LABEL: foo8:<br>
 ; AVX2:       # BB#0:<br>
 ; AVX2-NEXT:    vmovshdup {{.*#+}} ymm0 = ymm0[1,1,3,3,5,5,7,7]<br>
-; AVX2-NEXT:    vpermpd {{.*#+}} ymm1 = ymm0[2,2,2,2]<br>
-; AVX2-NEXT:    vmovaps {{.*#+}} ymm0 = <2,0,u,u,5,1,3,7><br>
-; AVX2-NEXT:    vpermps %ymm1, %ymm0, %ymm0<br>
-; AVX2-NEXT:    vmovapd %ymm1, (%rdi)<br>
+; AVX2-NEXT:    vpermpd {{.*#+}} ymm0 = ymm0[2,2,2,2]<br>
+; AVX2-NEXT:    vmovapd %ymm0, (%rdi)<br>
 ; AVX2-NEXT:    retq<br>
   %res = shufflevector <8 x float> %v, <8 x float> undef, <8 x i32> <i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5, i32 5><br>
   %res1 = shufflevector<8 x float> %res, <8 x float> undef, <8 x i32> <i32 2, i32 0, i32 undef, i32 undef, i32 5, i32 1, i32 3, i32 7><br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><o:p></o:p></p>
</blockquote>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
<p>---------------------------------------------------------------------<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></body>
</html>