<div dir="ltr"><div>Hello,</div><div><br></div><div>I recently found that LLVM generates sub-optimal assembly for a tail call optimization case. Below is an example (<a href="https://godbolt.org/z/ao15xE">https://godbolt.org/z/ao15xE</a>):</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div style="color:rgb(0,0,0);background-color:rgb(255,255,254);font-family:"Consolas, ""><div><span style="color:rgb(0,0,255)">void</span><span style="color:rgb(0,0,0)"> g1();</span></div><div><span style="color:rgb(0,0,255)">void</span><span style="color:rgb(0,0,0)"> g2();</span></div><div><span style="color:rgb(0,0,255)">void</span><span style="color:rgb(0,0,0)"> f(</span><span style="color:rgb(0,0,255)">bool</span><span style="color:rgb(0,0,0)"> v) {</span></div><div><span style="color:rgb(0,0,0)">   </span><span style="color:rgb(0,0,255)">if</span><span style="color:rgb(0,0,0)"> (v) {</span></div><div><span style="color:rgb(0,0,0)">    g1();</span></div><div><span style="color:rgb(0,0,0)">   }</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,0,255)">else</span><span style="color:rgb(0,0,0)"> {</span></div><div><span style="color:rgb(0,0,0)">     g2();</span></div><div><span style="color:rgb(0,0,0)">   }</span></div><div><span style="color:rgb(0,0,0)">}</span></div></div></div></blockquote><div> The assembly generated is as follow:</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div style="color:rgb(0,0,0);background-color:rgb(255,255,254);font-family:"Consolas, ""><div><span style="color:rgb(0,128,128)">f(bool):</span><span style="color:rgb(0,0,0)">                                  </span><span style="color:rgb(0,128,0)"># @f(bool)</span></div><div><span style="color:rgb(0,0,0)">        </span><span style="color:rgb(0,0,255)">testb</span><span style="color:rgb(0,0,0)">   </span><span style="color:rgb(72,100,170)">%dil</span><span style="color:rgb(0,0,0)">, </span><span style="color:rgb(72,100,170)">%dil</span></div><div><span style="color:rgb(0,0,0)">        </span><span style="color:rgb(0,0,255)">je</span><span style="color:rgb(0,0,0)">      </span><span style="color:rgb(0,128,128)">.LBB0_2</span></div><div><span style="color:rgb(0,0,0)">        </span><span style="color:rgb(0,0,255)">jmp</span><span style="color:rgb(0,0,0)">     </span><span style="color:rgb(0,128,128)">g1</span><span style="color:rgb(0,0,0)">()                  </span><span style="color:rgb(0,128,0)"># TAILCALL</span></div><div><span style="color:rgb(0,128,128)">.LBB0_2:</span></div><div><span style="color:rgb(0,0,0)">        </span><span style="color:rgb(0,0,255)">jmp</span><span style="color:rgb(0,0,0)">     </span><span style="color:rgb(0,128,128)">g2</span><span style="color:rgb(0,0,0)">()                  </span><span style="color:rgb(0,128,0)"># TAILCALL</span></div></div></div></blockquote><div>Ho<span style="color:rgb(0,0,0)">wever, in this specific case (where no function epilogue is needed), one can actually change 'je      .LBB0_2' to 'je g2()' directly, thus saving a jump. <br></span></div><div><span style="color:rgb(0,0,0)">Is there any way I could instruct LLVM to do this? For my use case, it is acceptable to do this at any level (C++ level /IR level /MachineInst level is all fine).<br></span></div><div><span style="color:rgb(0,0,0)"><br></span></div><div><span style="color:rgb(0,0,0)">Thanks!</span></div><div><span style="color:rgb(0,0,0)"><br></span></div><div><span style="color:rgb(0,0,0)">Best,</span></div><div><span style="color:rgb(0,0,0)">Haoran<br></span></div><div><span style="color:rgb(0,128,128)"><br></span></div></div>