Skip to content

Commit 9f5c8de

Browse files
committed
[DAG] visitAVG - rewrite "fold (avgfloor x, 0) -> x >> 1" to use SDPatternMatch
No need for this to be vector specific, and its more likely that scalar cases will appear after #92096
1 parent e0217ee commit 9f5c8de

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5211,30 +5211,28 @@ SDValue DAGCombiner::visitAVG(SDNode *N) {
52115211
!DAG.isConstantIntBuildVectorOrConstantInt(N1))
52125212
return DAG.getNode(Opcode, DL, N->getVTList(), N1, N0);
52135213

5214-
if (VT.isVector()) {
5214+
if (VT.isVector())
52155215
if (SDValue FoldedVOp = SimplifyVBinOp(N, DL))
52165216
return FoldedVOp;
52175217

5218-
// fold (avgfloor x, 0) -> x >> 1
5219-
if (ISD::isConstantSplatVectorAllZeros(N1.getNode())) {
5220-
if (Opcode == ISD::AVGFLOORS)
5221-
return DAG.getNode(ISD::SRA, DL, VT, N0, DAG.getConstant(1, DL, VT));
5222-
if (Opcode == ISD::AVGFLOORU)
5223-
return DAG.getNode(ISD::SRL, DL, VT, N0, DAG.getConstant(1, DL, VT));
5224-
}
5225-
}
5226-
52275218
// fold (avg x, undef) -> x
52285219
if (N0.isUndef())
52295220
return N1;
52305221
if (N1.isUndef())
52315222
return N0;
52325223

5233-
// Fold (avg x, x) --> x
5224+
// fold (avg x, x) --> x
52345225
if (N0 == N1 && Level >= AfterLegalizeTypes)
52355226
return N0;
52365227

5237-
// TODO If we use avg for scalars anywhere, we can add (avgfl x, 0) -> x >> 1
5228+
// fold (avgfloor x, 0) -> x >> 1
5229+
SDValue X;
5230+
if (sd_match(N, m_c_BinOp(ISD::AVGFLOORS, m_Value(X), m_Zero())))
5231+
return DAG.getNode(ISD::SRA, DL, VT, X,
5232+
DAG.getShiftAmountConstant(1, VT, DL));
5233+
if (sd_match(N, m_c_BinOp(ISD::AVGFLOORU, m_Value(X), m_Zero())))
5234+
return DAG.getNode(ISD::SRL, DL, VT, X,
5235+
DAG.getShiftAmountConstant(1, VT, DL));
52385236

52395237
return SDValue();
52405238
}

0 commit comments

Comments
 (0)