<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=us-ascii">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";
        mso-fareast-language:EN-US;}
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;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
        {mso-style-priority:34;
        margin-top:0cm;
        margin-right:0cm;
        margin-bottom:0cm;
        margin-left:36.0pt;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";
        mso-fareast-language:EN-US;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri","sans-serif";
        mso-fareast-language:EN-US;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:2.0cm 42.5pt 2.0cm 3.0cm;}
div.WordSection1
        {page:WordSection1;}
/* List Definitions */
@list l0
        {mso-list-id:710149356;
        mso-list-type:hybrid;
        mso-list-template-ids:499557154 68747279 68747289 68747291 68747279 68747289 68747291 68747279 68747289 68747291;}
@list l0:level1
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level2
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level3
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l0:level4
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level5
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level6
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l0:level7
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level8
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-18.0pt;}
@list l0:level9
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
ol
        {margin-bottom:0cm;}
ul
        {margin-bottom:0cm;}
--></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="RU" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span lang="EN-US">Hello everyone,<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">I’m facing a problem with an inefficient vector code generation for X86 with AVX2.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">The problem with the example below is unnecessary conversions of <8 x i32> to <8 x i16> and back when a boolean vector crosses BB boundary. I suppose that is because <8 x i1> is legalized to <8 x 16>. As far as I understand
 there is no such issue if all i1 vectors are computed inside a BB because they are optimized out by Machine Code Optimizations.<br>
<br>
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Here is a short reproducer I’ve written by hand (based on a real LLVM IR sequence produced by a vectorizer I’m working with).<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">bash$ ./llc -mcpu=core-avx2 -filetype=asm -o legalization.overhead.s legalization.overhead.ll -asm-verbose<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New""><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New"">define <8 x i32> @foo(<8 x i32> %arg0, <8 x i32> %arg1,         |foo:                                    # @foo                 <o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New"">                      <8 x i32> %arg2, i1 %switch) {            |        .cfi_startproc                                        
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New"">BB0:                                                            |# BB#0:                                 # %BB0                 <o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New"">  %boolv0 = icmp sgt <8 x i32> %arg0, zeroinitializer           |        pushq   %rbp                                          
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New"">  br i1 %switch, label %BB1, label %BB2                         |.Ltmp2:                                                        <o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New"">BB1:                                                            |        .cfi_def_cfa_offset 16                                
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New"">  %boolv1 = icmp sgt <8 x i32> %arg1, zeroinitializer           |.Ltmp3:                                                        <o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New"">  br label %BB2                                                 |        .cfi_offset %rbp, -16                                 
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New"">BB2:                                                            |        movq    %rsp, %rbp                                     <o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New"">  %boolvx = phi <8 x i1> [ %boolv0, %BB0 ], [ %boolv1, %BB1 ]   |.Ltmp4:                                                       
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New"">  %boolv2 = icmp sgt <8 x i32> %arg2, zeroinitializer           |        .cfi_def_cfa_register %rbp                             <o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New"">  %merge = and <8 x i1> %boolvx, %boolv2                        |        vpxor   %ymm3, %ymm3, %ymm3                           
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New"">  %res = sext <8 x i1> %merge to <8 x i32>                      |        vpcmpgtd        %ymm3, %ymm0, %ymm0                   
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New"">  ret <8 x i32> %res                                            |       
<span style="background:yellow;mso-highlight:yellow">vpshufb .LCPI0_0(%rip), %ymm0, %ymm0</span><br>
}                                                               |        <span style="color:#A6A6A6;mso-style-textfill-fill-color:#A6A6A6;mso-style-textfill-fill-alpha:100.0%;background:yellow;mso-highlight:yellow">
# ymm0 = ymm0[0,1,4,5,8,9,12,13,NULL,NULL,NULL,NULL,16,17,20,21,24,25,28,29,NULL…]</span><span style="color:#A6A6A6;mso-style-textfill-fill-color:#A6A6A6;mso-style-textfill-fill-alpha:100.0%"><o:p></o:p></span></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New"">~                                                               |       
<span style="background:yellow;mso-highlight:yellow">vpermq  $8, %ymm0, %ymm0        # ymm0 = ymm0[0,2,0,0]
</span><o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New"">~                                                               |        testb   $1, %dil                                       <o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New"">~                                                               |        je      .LBB0_2                                       
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New"">~                                                               |# BB#1:                                 # %BB1                 <o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New"">~                                                               |        vpcmpgtd        %ymm3, %ymm1, %ymm0                   
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New"">~                                                               |       
<span style="background:yellow;mso-highlight:yellow">vpshufb .LCPI0_0(%rip), %ymm0, %ymm0                  </span> <o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New"">~                                                               |       
<span style="background:yellow;mso-highlight:yellow">vpermq  $8, %ymm0, %ymm0        # ymm0 = ymm0[0,2,0,0]</span>
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New"">~                                                               |.LBB0_2:                                # %BB2                 <o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New"">~                                                               |        vpcmpgtd        %ymm3, %ymm2, %ymm1                   
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New"">~                                                               |       
<span style="background:yellow;mso-highlight:yellow">vpshufb .LCPI0_0(%rip), %ymm1, %ymm1                  </span> <o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New"">~                                                               |       
<span style="background:yellow;mso-highlight:yellow">vpermq  $8, %ymm1, %ymm1        # ymm1 = ymm1[0,2,0,0]</span>
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New"">~                                                               |        vpand   %xmm1, %xmm0, %xmm0                            <o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New"">~                                                               |       
<span style="background:yellow;mso-highlight:yellow">vpmovzxwd       %xmm0, %ymm0</span>                          
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New"">~                                                               |       
<span style="background:yellow;mso-highlight:yellow">vpslld  $31, %ymm0, %ymm0   </span>
                           <o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New"">~                                                               |       
<span style="background:yellow;mso-highlight:yellow">vpsrad  $31, %ymm0, %ymm0   </span>
                           <o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New"">~                                                               |        popq    %rbp                                           <o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New"">~                                                               |        ret</span><span lang="EN-US"><br>
Instances of the problematic ASM sequences:</span><span lang="EN-US" style="font-size:8.0pt;font-family:"Courier New""><o:p></o:p></span></p>
<p class="MsoListParagraph" style="text-indent:-18.0pt;mso-list:l0 level1 lfo1"><![if !supportLists]><span lang="EN-US"><span style="mso-list:Ignore">1.<span style="font:7.0pt "Times New Roman"">     
</span></span></span><![endif]><b><span lang="EN-US"><8 x i32> to <8 x i16></span></b><span lang="EN-US"><br>
</span><span lang="EN-US" style="font-family:"Courier New"">vpshufb .LCPI0_0(%rip), %ymm0, %ymm0<br>
vpermq  $8, %ymm0, %ymm0        # ymm0 = ymm0[0,2,0,0]</span><span lang="EN-US"><br>
<br>
<o:p></o:p></span></p>
<p class="MsoListParagraph" style="text-indent:-18.0pt;mso-list:l0 level1 lfo1"><![if !supportLists]><span lang="EN-US"><span style="mso-list:Ignore">2.<span style="font:7.0pt "Times New Roman"">     
</span></span></span><![endif]><b><span lang="EN-US"><8 x i16> to <8 x i32></span></b><span lang="EN-US"><br>
</span><span lang="EN-US" style="font-family:"Courier New"">vpmovzxwd       %xmm0, %ymm0<br>
vpslld  $31, %ymm0, %ymm0<br>
vpsrad  $31, %ymm0, %ymm0</span><span lang="EN-US"><o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Currently I’m trying to figure out the best way to completely remove those unnecessary conversions and would appreciate any help from the community.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Thank you!<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Andrey Lizunov<o:p></o:p></span></p>
</div>
<p><br>
--------------------------------------------------------------------<br>
Closed Joint Stock Company Intel A/O<br>
Registered legal address: Krylatsky Hills Business Park, <br>
17 Krylatskaya Str., Bldg 4, Moscow 121614, <br>
Russian Federation</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>