Skip to content

Commit 9e8ecce

Browse files
authored
[DAGCombine] Transform shl X, cttz(Y) to mul (Y & -Y), X if cttz 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.
1 parent aef0bdd commit 9e8ecce

File tree

2 files changed

+819
-0
lines changed

2 files changed

+819
-0
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10107,6 +10107,18 @@ SDValue DAGCombiner::visitSHL(SDNode *N) {
1010710107
if (SDValue NewSHL = visitShiftByConstant(N))
1010810108
return NewSHL;
1010910109

10110+
// fold (shl X, cttz(Y)) -> (mul (Y & -Y), X) if cttz is unsupported on the
10111+
// target.
10112+
if ((N1.getOpcode() == ISD::CTTZ || N1.getOpcode() == ISD::CTTZ_ZERO_UNDEF) &&
10113+
N1.hasOneUse() && !TLI.isOperationLegalOrCustom(ISD::CTTZ, VT) &&
10114+
TLI.isOperationLegalOrCustom(ISD::MUL, VT)) {
10115+
SDValue Y = N1.getOperand(0);
10116+
SDLoc DL(N);
10117+
SDValue NegY = DAG.getNegative(Y, DL, VT);
10118+
SDValue And = DAG.getNode(ISD::AND, DL, VT, Y, NegY);
10119+
return DAG.getNode(ISD::MUL, DL, VT, And, N0);
10120+
}
10121+
1011010122
if (SimplifyDemandedBits(SDValue(N, 0)))
1011110123
return SDValue(N, 0);
1011210124

0 commit comments

Comments
 (0)