Skip to content

Commit ba1255d

Browse files
committed
[DAG] Use FoldConstantArithmetic to constant fold (and (ext (and V, c1)), c2) -> (and (ext V), (and c1, (ext c2)))
Noticed while triaging the regression from #112710 noticed by @mstorsjo - don't rely on isConstantIntBuildVectorOrConstantInt+getNode to guarantee constant folding (if it fails to constant fold it will infinite loop), use FoldConstantArithmetic instead.
1 parent e6c0143 commit ba1255d

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7159,15 +7159,16 @@ SDValue DAGCombiner::visitAND(SDNode *N) {
71597159
SDValue N0Op0 = N0.getOperand(0);
71607160
if (N0Op0.getOpcode() == ISD::AND &&
71617161
(ExtOpc != ISD::ZERO_EXTEND || !TLI.isZExtFree(N0Op0, VT)) &&
7162-
DAG.isConstantIntBuildVectorOrConstantInt(N1) &&
7163-
DAG.isConstantIntBuildVectorOrConstantInt(N0Op0.getOperand(1)) &&
71647162
N0->hasOneUse() && N0Op0->hasOneUse()) {
7165-
SDValue NewMask =
7166-
DAG.getNode(ISD::AND, DL, VT, N1,
7167-
DAG.getNode(ExtOpc, DL, VT, N0Op0.getOperand(1)));
7168-
return DAG.getNode(ISD::AND, DL, VT,
7169-
DAG.getNode(ExtOpc, DL, VT, N0Op0.getOperand(0)),
7170-
NewMask);
7163+
if (SDValue NewExt = DAG.FoldConstantArithmetic(ExtOpc, DL, VT,
7164+
{N0Op0.getOperand(1)})) {
7165+
if (SDValue NewMask =
7166+
DAG.FoldConstantArithmetic(ISD::AND, DL, VT, {N1, NewExt})) {
7167+
return DAG.getNode(ISD::AND, DL, VT,
7168+
DAG.getNode(ExtOpc, DL, VT, N0Op0.getOperand(0)),
7169+
NewMask);
7170+
}
7171+
}
71717172
}
71727173
}
71737174

0 commit comments

Comments
 (0)