<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Dec 18, 2014 at 12:42 PM, Andrea Di Biagio <span dir="ltr"><<a href="mailto:Andrea_DiBiagio@sn.scee.net" target="_blank">Andrea_DiBiagio@sn.scee.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">The constraints are:<br>
a) The 'then' basic block is taken only if the input operand to the cttz/ctlz is different than zero;<br>
b) The phi node propagates the size-of (in bits) of the value %val in input to the cttz/ctlz if %val is zero.<br>
</span>c) The target says that it is "cheap" to speculate cttz/ctlz.<br>
<br>
If all these constraints are met, CodeGenPrepare can hoist the call to cttz/ctlz from the 'then' basic block into the 'entry' basic block. The new cttz/ctlz instruction will also have the 'undef on zero' flag set to 'false'.<br>
<br>
I added two new hooks in TargetLowering.h to let targets customize the behavior (i.e. decide whether it is cheap or not to speculate calls to cttz/ctlz). The two new methods are 'isCheapToSpeculateCtlz' and 'isCheapToSpeculateCttz'.<br>
By default, both methods return 'false'. Which means, CodeGenPrepare doesn't try to speculate calls to cttz/ctlz unless the target says that it is profitable to do it.<br>
<br>
On X86, method 'isCheapToSpeculateCtlz' returns true only if the target has LZCNT. Method 'isCheapToSpeculateCttz' only returns true if the target has BMI.<br>
This may change in future. For now, I avoided to enable the transformation for all x86-64 targets with feature CMOV because I am not 100% it is always a win to speculate bsf/bsr. So, I left a couple of TODO comments in the code.</blockquote></div><br></div><div class="gmail_extra">So, I actually have several piles of code that rely heavily on BSF/BSR lowering performance and I can comment on the effect of CMOV here.</div><div class="gmail_extra"><br></div><div class="gmail_extra">CMOV, at least on a large number of x86 processors, will not actually allow the BSF/BSR to be skipped. Essentially, both inputs to the CMOV have to complete. We actually use to lower the cttz and ctlz intrinsics (a *very* long time ago) using BSF/BSR and a CMOV on x86 and the performance was dramatically improved just by using a conditional branch.</div><div class="gmail_extra"><br></div><div class="gmail_extra">So you can probably nuke the TODO and just comment that we really don't want to speculate this unless it ensures we can directly use LZCNT or TZCNT...</div></div>