@@ -5211,30 +5211,28 @@ SDValue DAGCombiner::visitAVG(SDNode *N) {
5211
5211
!DAG.isConstantIntBuildVectorOrConstantInt(N1))
5212
5212
return DAG.getNode(Opcode, DL, N->getVTList(), N1, N0);
5213
5213
5214
- if (VT.isVector()) {
5214
+ if (VT.isVector())
5215
5215
if (SDValue FoldedVOp = SimplifyVBinOp(N, DL))
5216
5216
return FoldedVOp;
5217
5217
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
-
5227
5218
// fold (avg x, undef) -> x
5228
5219
if (N0.isUndef())
5229
5220
return N1;
5230
5221
if (N1.isUndef())
5231
5222
return N0;
5232
5223
5233
- // Fold (avg x, x) --> x
5224
+ // fold (avg x, x) --> x
5234
5225
if (N0 == N1 && Level >= AfterLegalizeTypes)
5235
5226
return N0;
5236
5227
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));
5238
5236
5239
5237
return SDValue();
5240
5238
}
0 commit comments