[llvm] [X86] Add an optimization for a pattern exposed by #214517 (PR #217776)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Aug 22 10:10:47 PDT 2026
================
@@ -50792,6 +50793,26 @@ static SDValue combineMul(SDNode *N, SelectionDAG &DAG,
if (SDValue V = combineMulToPMADD52(N, DL, DAG, Subtarget))
return V;
+ // InstCombine folds shl X, cttz(Y) into mul(and(neg(Y), Y), X), which is
+ // usually profitable but is worse when BMI1 is available: the mul
+ // form lowers to blsi+imul while the shl form uses tzcnt for the shift
+ // amount, which has better latency/throughput despite matching instruction
+ // count (https://github.com/llvm/llvm-project/issues/216550). Reverse the
+ // fold when that feature is available. Only valid when Y is provably nonzero:
+ // CTTZ_ZERO_POISON is poison at Y == 0, while the mul form is well-defined
+ // (0) at Y == 0 - so the rewrite would be a miscompile for Y == 0 without
+ // that guard.
+ if (Subtarget.hasBMI() && VT.isScalarInteger() &&
----------------
zero318 wrote:
Is it viable to apply this to i16 as well? `TZCNT` does support 16 bit regs and that could reasonably output to CL for a shift, but IDK if that causes too many partial register issues to bother with.
https://github.com/llvm/llvm-project/pull/217776
More information about the llvm-commits
mailing list