<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">Hi all,<div>  I've run into a case where the optimizer seems to be having trouble doing the "obvious" thing.</div><div><br></div><div>Consider this code:</div><div>```</div><div><div style="color:rgb(0,0,0);background-color:rgb(255,255,254)"><div><span style="color:rgb(0,0,255)">define</span> <span style="color:rgb(0,128,128)">i16</span> <span style="color:rgb(0,17,136)">@foo</span>(<<span style="color:rgb(9,136,90)">8</span> x <span style="color:rgb(0,128,128)">i16</span>>* <span style="color:rgb(0,0,255)">dereferenceable</span>(<span style="color:rgb(9,136,90)">16</span>) <span style="color:rgb(0,17,136)">%egress</span>, <<span style="color:rgb(9,136,90)">16</span> x <span style="color:rgb(0,128,128)">i8</span>> <span style="color:rgb(0,17,136)">%a0</span>) {</div><div>    <span style="color:rgb(0,17,136)">    %a1</span> = icmp slt <<span style="color:rgb(9,136,90)">16</span> x <span style="color:rgb(0,128,128)">i8</span>> <span style="color:rgb(0,17,136)">%a0</span>, <span style="color:rgb(221,0,0)">zeroinitializer</span></div><div>    <span style="color:rgb(0,17,136)">    %a2</span> = bitcast <<span style="color:rgb(9,136,90)">16</span> x <span style="color:rgb(0,128,128)">i1</span>> <span style="color:rgb(0,17,136)">%a1</span> <span style="color:rgb(0,0,255)">to</span> <span style="color:rgb(0,128,128)">i16</span></div><div>    <span style="color:rgb(0,17,136)">    %astore</span> = getelementptr inbounds <<span style="color:rgb(9,136,90)">8</span> x <span style="color:rgb(0,128,128)">i16</span>>, <<span style="color:rgb(9,136,90)">8</span> x <span style="color:rgb(0,128,128)">i16</span>>* <span style="color:rgb(0,17,136)">%egress</span>, <span style="color:rgb(0,128,128)">i64</span> <span style="color:rgb(9,136,90)">0</span>, <span style="color:rgb(0,128,128)">i64</span> <span style="color:rgb(9,136,90)">7</span></div><div>    <span style="color:rgb(0,128,0)">    ;store i16 %a2, i16* %astore</span></div><div>    ret <span style="color:rgb(0,128,128)">i16</span> <span style="color:rgb(0,17,136)">%a2</span></div><div>}</div>```</div><div style="color:rgb(0,0,0);background-color:rgb(255,255,254)">The optimizer recognizes this and llc nicely outputs a vpmovmskb instruction:</div><div style="color:rgb(0,0,0);background-color:rgb(255,255,254)">```</div><div style="color:rgb(0,0,0);background-color:rgb(255,255,254)"><div><div><span style="color:rgb(0,128,128)">foo:</span> <span style="color:rgb(0,128,0)"># @foo</span></div><div>  <span style="color:rgb(0,0,255)">    vpmovmskb</span> <span style="color:rgb(72,100,170)">eax</span>, <span style="color:rgb(72,100,170)">xmm0</span></div><div>  <span style="color:rgb(0,0,255)">    ret</span></div></div><div><span style="color:rgb(0,0,255)">```</span></div></div><div style="color:rgb(0,0,0);background-color:rgb(255,255,254)"><br></div><div style="color:rgb(0,0,0);background-color:rgb(255,255,254)">Writing to the output vector also works well:</div><div style="color:rgb(0,0,0);background-color:rgb(255,255,254)">```</div><div style="color:rgb(0,0,0);background-color:rgb(255,255,254)"><div><span style="color:rgb(0,0,255)">define</span> <span style="color:rgb(0,128,128)">void</span> <span style="color:rgb(0,17,136)">@writing</span>(<<span style="color:rgb(9,136,90)">8</span> x <span style="color:rgb(0,128,128)">i16</span>>* <span style="color:rgb(0,0,255)">dereferenceable</span>(<span style="color:rgb(9,136,90)">16</span>) <span style="color:rgb(0,17,136)">%egress</span>, <<span style="color:rgb(9,136,90)">16</span> x <span style="color:rgb(0,128,128)">i8</span>> <span style="color:rgb(0,17,136)">%a0</span>) {</div><div>    <span style="color:rgb(0,17,136)">    %astore</span> = getelementptr inbounds <<span style="color:rgb(9,136,90)">8</span> x <span style="color:rgb(0,128,128)">i16</span>>, <<span style="color:rgb(9,136,90)">8</span> x <span style="color:rgb(0,128,128)">i16</span>>* <span style="color:rgb(0,17,136)">%egress</span>, <span style="color:rgb(0,128,128)">i64</span> <span style="color:rgb(9,136,90)">0</span>, <span style="color:rgb(0,128,128)">i64</span> <span style="color:rgb(9,136,90)">7</span></div><div>    store <span style="color:rgb(0,128,128)">i16</span> <span style="color:rgb(9,136,90)">123</span>, <span style="color:rgb(0,128,128)">i16*</span> <span style="color:rgb(0,17,136)">%astore</span></div><div>    ret <span style="color:rgb(0,128,128)">void</span></div><div>}</div><div>```</div><div>outputs:</div><div>```</div><div><div><div><span style="color:rgb(0,128,128)">writing:</span> <span style="color:rgb(0,128,0)"># @writing</span></div><div>  <span style="color:rgb(0,0,255)">    mov</span> <span style="color:rgb(0,128,128)">word</span> <span style="color:rgb(0,128,128)">ptr</span> [<span style="color:rgb(72,100,170)">rdi</span> + <span style="color:rgb(9,136,90)">14</span>], <span style="color:rgb(9,136,90)">123</span></div><div>  <span style="color:rgb(0,0,255)">    ret</span></div></div></div><div><span style="color:rgb(0,0,255)">```</span></div><div><br></div><div>Now, combining these two by uncommenting the store in `foo()` suddenly results in a very large function, instead of just:</div><div><div><div><span style="color:rgb(0,0,255)">    vpmovmskb</span> <span style="color:rgb(72,100,170)">eax</span>, <span style="color:rgb(72,100,170)">xmm0</span></div><div><span style="color:rgb(0,0,255)">    mov</span> <span style="color:rgb(0,128,128)">word</span> <span style="color:rgb(0,128,128)">ptr</span> [<span style="color:rgb(72,100,170)">rdi</span> + <span style="color:rgb(9,136,90)">14</span>], <span style="color:rgb(9,136,90)">ax</span></div><div><span style="color:rgb(0,0,255)">    ret</span></div></div><br class="gmail-Apple-interchange-newline"></div><div>Is there something wrong with my IR code, or is the optimizer somehow confused? Can I rewrite the code such that the optimizer does understand?</div><div><br></div><div>Godbolt link: <a href="https://llvm.godbolt.org/z/OgExDk">https://llvm.godbolt.org/z/OgExDk</a></div><div><br></div><div>Thanks a lot for the help.</div><div>Cheers,</div><div>  Johan</div><div><br></div></div></div></div></div></div></div>