<div dir="ltr"><div>I'd try to change this in 2-3 steps (if necessary):</div><div>1. Change instcombine to use shouldChangeType() - narrowing to a crazy type was too ambitious. The backend isn't likely to produce good code for that anytime soon. That should be enough to solve bug 29009, and maybe the example you've shown here? I don't think there's anything controversial about this change.<br></div><div>2. If #1 is not enough, remove the instcombine transform completely and see if SimplifyCFG or some other pass now picks up those cases.<br></div><div>3. If we show codegen regressions from #2, then try to solve those in the backend. <br></div></div><br><div class="gmail_quote"><div dir="ltr">On Wed, Oct 24, 2018 at 12:20 PM Bakhvalov, Denis <<a href="mailto:denis.bakhvalov@intel.com">denis.bakhvalov@intel.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">





<div lang="PL" link="blue" vlink="purple">
<div class="m_3530358254604491705WordSection1">
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d">Hello Sanjay, Akira,<br>
<br>
>From IR pov this transformation creates additional truncate instruction that didn’t exist in the original IR (as Sanjay wrote).<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d">I went through the links that Sanjay provided and now I’m stuck. :)<br>
Should we now focus on backend (as described in Bug 29009)? <u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d">Or we say that this instcombine transform makes no sense today and we do something about it?<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d">Best regards,<br>
Denis Bakhvalov.<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1f497d"><u></u> <u></u></span></p>
<p class="MsoNormal"><a name="m_3530358254604491705______replyseparator"></a><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif">From:</span></b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif"> Sanjay Patel [mailto:<a href="mailto:spatel@rotateright.com" target="_blank">spatel@rotateright.com</a>]
<br>
<b>Sent:</b> Tuesday, October 23, 2018 11:20 AM<br>
<b>To:</b> Roman Lebedev <<a href="mailto:lebedev.ri@gmail.com" target="_blank">lebedev.ri@gmail.com</a>><br>
<b>Cc:</b> Bakhvalov, Denis <<a href="mailto:denis.bakhvalov@intel.com" target="_blank">denis.bakhvalov@intel.com</a>>; llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>>; Akira Hatanaka <<a href="mailto:ahatanaka@apple.com" target="_blank">ahatanaka@apple.com</a>><br>
<b>Subject:</b> Re: [llvm-dev] InstCombine: Narrow switch instructions using known bits<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p>
<div>
<div>
<div>
<div>
<p class="MsoNormal"><span lang="EN-US">Sorry for not seeing this sooner. I made this transform more aggressive here:<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><a href="https://reviews.llvm.org/D12965" target="_blank"><span lang="EN-US">https://reviews.llvm.org/D12965</span></a><span lang="EN-US"><u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US">But the backend still doesn't deal with strange types optimally:<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><a href="https://bugs.llvm.org/show_bug.cgi?id=29009" target="_blank"><span lang="EN-US">https://bugs.llvm.org/show_bug.cgi?id=29009</span></a><span lang="EN-US"><u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US">So at the least, I think we should limit the transform as it was in an earlier draft of D12965 to not shrink to weird illegal types (the code should use "shouldChangeType()").<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US">I agree with Roman that we should look at the IR diffs if we remove this completely. But this is a potentially unusual instcombine IIRC because it can create an extra instruction (the truncate) that didn't exist in the
 original IR.<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US">Looking at some of the linked patches/bugs should tell us if we are regressing any of the motivating cases. My guess is that improvements to SimplifyCFG since then have made this instcombine transform less likely/necessary.<u></u><u></u></span></p>
</div>
</div>
</div>
</div>
<p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p>
<div>
<div>
<p class="MsoNormal"><span lang="EN-US">On Tue, Oct 23, 2018 at 11:12 AM Roman Lebedev via llvm-dev <</span><a href="mailto:llvm-dev@lists.llvm.org" target="_blank"><span lang="EN-US">llvm-dev@lists.llvm.org</span></a><span lang="EN-US">> wrote:<u></u><u></u></span></p>
</div>
<blockquote style="border:none;border-left:solid #cccccc 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-top:5.0pt;margin-right:0in;margin-bottom:5.0pt">
<p class="MsoNormal"><span lang="EN-US">On Tue, Oct 23, 2018 at 8:02 PM Bakhvalov, Denis via llvm-dev<br>
<</span><a href="mailto:llvm-dev@lists.llvm.org" target="_blank"><span lang="EN-US">llvm-dev@lists.llvm.org</span></a><span lang="EN-US">> wrote:<br>
><br>
> Hi Akira,<br>
><br>
> Can you maybe remember or come up with any example where this transformation still helps today?<br>
><br>
> If no such case and no objections on disabling/removing it, I can start working on that.<br>
><br>
><br>
><br>
> Best regards,<br>
> Denis Bakhvalov.<br>
><br>
><br>
><br>
> From: Akira Hatanaka [mailto:</span><a href="mailto:ahatanak@gmail.com" target="_blank"><span lang="EN-US">ahatanak@gmail.com</span></a><span lang="EN-US">]<br>
> Sent: Wednesday, October 10, 2018 12:46 PM<br>
> To: Bakhvalov, Denis <</span><a href="mailto:denis.bakhvalov@intel.com" target="_blank"><span lang="EN-US">denis.bakhvalov@intel.com</span></a><span lang="EN-US">><br>
> Cc: llvm-dev <</span><a href="mailto:llvm-dev@lists.llvm.org" target="_blank"><span lang="EN-US">llvm-dev@lists.llvm.org</span></a><span lang="EN-US">>; Akira Hatanaka <</span><a href="mailto:ahatanaka@apple.com" target="_blank"><span lang="EN-US">ahatanaka@apple.com</span></a><span lang="EN-US">><br>
> Subject: Re: [llvm-dev] InstCombine: Narrow switch instructions using known bits<br>
><br>
><br>
><br>
> On Fri, Sep 21, 2018 at 7:02 AM Bakhvalov, Denis via llvm-dev <</span><a href="mailto:llvm-dev@lists.llvm.org" target="_blank"><span lang="EN-US">llvm-dev@lists.llvm.org</span></a><span lang="EN-US">> wrote:<br>
><br>
> Hello,<br>
><br>
><br>
><br>
> I found one case where narrowing switch instructions transformation in InstCombine produces worse code.<br>
><br>
> Let's suppose that I have such code:<br>
><br>
><br>
><br>
> $ cat a.c<br>
><br>
> void foo();<br>
><br>
> void bar();<br>
><br>
> void zoo();<br>
><br>
><br>
><br>
> void my_func(unsigned int a) {<br>
><br>
>     unsigned char b = a & 0xF;<br>
><br>
>     switch (b) {<br>
><br>
>         case 0:  foo(); break;<br>
><br>
>         case 1:  bar(); break;<br>
><br>
>         case 2:  foo(); break;<br>
><br>
>         case 3:  foo(); break;<br>
><br>
>         case 4:  foo(); break;<br>
><br>
>         case 5:  bar(); break;<br>
><br>
>         case 6:  foo(); break;<br>
><br>
>         case 7:  foo(); break;<br>
><br>
>         case 8:  bar(); break;<br>
><br>
>         case 9:  foo(); break;<br>
><br>
>         case 10: foo(); break;<br>
><br>
><br>
><br>
>         default: zoo();<br>
><br>
>     }<br>
><br>
> }<br>
><br>
><br>
><br>
> Using recent clang:<br>
><br>
><br>
><br>
> $ clang -O3 -S -c a.c -o a.s<br>
><br>
> I have the following assembly in the beginning of my_func:<br>
><br>
> # bad case<br>
><br>
>         movl    %edi, %eax<br>
><br>
>         andb    $15, %al<br>
><br>
>         cmpb    $10, %al<br>
><br>
>         ja      .LBB0_9                           # jump to the default case<br>
><br>
><br>
><br>
>         andl    $15, %edi<br>
><br>
>         jmpq    *.LJTI0_0(,%rdi,8)      # go to jump table<br>
><br>
><br>
><br>
> I found that if I disable switch shrinking like shown below:<br>
><br>
><br>
><br>
> $ git diff<br>
><br>
> diff --git a/lib/Transforms/InstCombine/InstructionCombining.cpp b/lib/Transforms/InstCombine/InstructionCombining.cpp<br>
><br>
> index 5d5a9b2..3682b88 100644<br>
><br>
> --- a/lib/Transforms/InstCombine/InstructionCombining.cpp<br>
><br>
> +++ b/lib/Transforms/InstCombine/InstructionCombining.cpp<br>
><br>
> @@ -2429,6 +2429,8 @@ Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) {<br>
><br>
>      return &SI;<br>
><br>
>    }<br>
><br>
><br>
><br>
> +  return nullptr;<br>
><br>
> +<br>
><br>
>    KnownBits Known = computeKnownBits(Cond, 0, &SI);<br>
><br>
>    unsigned LeadingKnownZeros = Known.countMinLeadingZeros();<br>
><br>
>    unsigned LeadingKnownOnes = Known.countMinLeadingOnes();<br>
><br>
><br>
><br>
> I get better assembly (there is no additional MOV and AND instructions):<br>
><br>
> # good case<br>
><br>
>         andl    $15, %edi<br>
><br>
>         cmpl    $10, %edi<br>
><br>
>         ja      .LBB0_9<br>
><br>
><br>
><br>
>         jmpq    *.LJTI0_0(,%rdi,8)<br>
It's not always good to compare assembly when talking about middle-end<br>
transforms.<br>
If something is good for middle-end (which is *likely* the case here),<br>
and is bad for back-ends,<br>
then it is usually a back-end problem to deal with it.<br>
In other words, when talking about middle-end, it might be best to<br>
look at the LLVM IR.<br>
<br>
> This transformation was introduced in the commit:<br>
><br>
> commit 4eb03123dfda2de88a84852834845678833c8c36<br>
><br>
> Author: Akira Hatanaka <</span><a href="mailto:ahatanaka@apple.com" target="_blank"><span lang="EN-US">ahatanaka@apple.com</span></a><span lang="EN-US">><br>
><br>
> Date:   Thu Oct 16 06:00:46 2014 +0000<br>
><br>
>     Reapply r219832 - InstCombine: Narrow switch instructions using known bits.<br>
><br>
><br>
><br>
> From IR point of view, after the ‘opt –codegenprepare’ the difference is that in good case we have simple ‘and’ operation (all calculations are made in i32). In the bad case there is 'trunc' to i4 and then ‘zext’ to i8.<br>
><br>
><br>
><br>
> During instruction selection we expand switch into a jump table. In the bad case we use 2 copies of the value that we are switching on. First is in i8 that we use to determine whether we should jump to default case. The second is in i64, which we use for
 calculating address in the jump table. In the good case they were combined.<br>
><br>
><br>
><br>
> But there is still one thing that I don’t understand. What is the bad case that this transformation (narrowing switch instructions) was supposed to fix, i.e. does this transformation still make sense?<br>
><br>
><br>
><br>
><br>
><br>
> I couldn't find the original test case, but the patch was committed to fix a switch over a 128-bit value that was causing llvm to generate suboptimal code in some cases. I'm not sure whether this optimization is still necessary today.<br>
><br>
><br>
><br>
> Best regards,<br>
> Denis Bakhvalov.<br>
Roman.<br>
<br>
> _______________________________________________<br>
> LLVM Developers mailing list<br>
> </span><a href="mailto:llvm-dev@lists.llvm.org" target="_blank"><span lang="EN-US">llvm-dev@lists.llvm.org</span></a><span lang="EN-US"><br>
> </span><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" target="_blank"><span lang="EN-US">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</span></a><span lang="EN-US"><br>
><br>
> _______________________________________________<br>
> LLVM Developers mailing list<br>
> </span><a href="mailto:llvm-dev@lists.llvm.org" target="_blank"><span lang="EN-US">llvm-dev@lists.llvm.org</span></a><span lang="EN-US"><br>
> </span><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" target="_blank"><span lang="EN-US">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</span></a><span lang="EN-US"><br>
_______________________________________________<br>
LLVM Developers mailing list<br>
</span><a href="mailto:llvm-dev@lists.llvm.org" target="_blank"><span lang="EN-US">llvm-dev@lists.llvm.org</span></a><span lang="EN-US"><br>
</span><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" target="_blank"><span lang="EN-US">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</span></a><span lang="EN-US"><u></u><u></u></span></p>
</blockquote>
</div>
</div>
</div>

</blockquote></div>