Skip to content

Commit

Permalink
[DAGCombine] Transform shl X, cttz(Y) to mul (Y & -Y), X if cttz …
Browse files Browse the repository at this point in the history
…is unsupported (#85066)

This patch fold `shl X, cttz(Y)` to `mul (Y & -Y), X` if cttz is
unsupported by the target.
Alive2: https://alive2.llvm.org/ce/z/AtLN5Y
Fixes #84763.
  • Loading branch information
dtcxzyw authored May 29, 2024
1 parent aef0bdd commit 9e8ecce
Show file tree
Hide file tree
Showing 2 changed files with 819 additions and 0 deletions.
12 changes: 12 additions & 0 deletions llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10107,6 +10107,18 @@ SDValue DAGCombiner::visitSHL(SDNode *N) {
if (SDValue NewSHL = visitShiftByConstant(N))
return NewSHL;

// fold (shl X, cttz(Y)) -> (mul (Y & -Y), X) if cttz is unsupported on the
// target.
if ((N1.getOpcode() == ISD::CTTZ || N1.getOpcode() == ISD::CTTZ_ZERO_UNDEF) &&
N1.hasOneUse() && !TLI.isOperationLegalOrCustom(ISD::CTTZ, VT) &&
TLI.isOperationLegalOrCustom(ISD::MUL, VT)) {
SDValue Y = N1.getOperand(0);
SDLoc DL(N);
SDValue NegY = DAG.getNegative(Y, DL, VT);
SDValue And = DAG.getNode(ISD::AND, DL, VT, Y, NegY);
return DAG.getNode(ISD::MUL, DL, VT, And, N0);
}

if (SimplifyDemandedBits(SDValue(N, 0)))
return SDValue(N, 0);

Expand Down
Loading

0 comments on commit 9e8ecce

Please sign in to comment.