@@ -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
}
@@ -5255,24 +5253,25 @@ SDValue DAGCombiner::visitABD(SDNode *N) {
5255
5253
!DAG.isConstantIntBuildVectorOrConstantInt(N1))
5256
5254
return DAG.getNode(Opcode, DL, N->getVTList(), N1, N0);
5257
5255
5258
- if (VT.isVector()) {
5256
+ if (VT.isVector())
5259
5257
if (SDValue FoldedVOp = SimplifyVBinOp(N, DL))
5260
5258
return FoldedVOp;
5261
5259
5262
- // fold (abds x, 0) -> abs x
5263
- // fold (abdu x, 0) -> x
5264
- if (ISD::isConstantSplatVectorAllZeros(N1.getNode())) {
5265
- if (Opcode == ISD::ABDS)
5266
- return DAG.getNode(ISD::ABS, DL, VT, N0);
5267
- if (Opcode == ISD::ABDU)
5268
- return N0;
5269
- }
5270
- }
5271
-
5272
5260
// fold (abd x, undef) -> 0
5273
5261
if (N0.isUndef() || N1.isUndef())
5274
5262
return DAG.getConstant(0, DL, VT);
5275
5263
5264
+ SDValue X;
5265
+
5266
+ // fold (abds x, 0) -> abs x
5267
+ if (sd_match(N, m_c_BinOp(ISD::ABDS, m_Value(X), m_Zero())) &&
5268
+ (!LegalOperations || hasOperation(ISD::ABS, VT)))
5269
+ return DAG.getNode(ISD::ABS, DL, VT, X);
5270
+
5271
+ // fold (abdu x, 0) -> x
5272
+ if (sd_match(N, m_c_BinOp(ISD::ABDU, m_Value(X), m_Zero())))
5273
+ return X;
5274
+
5276
5275
// fold (abds x, y) -> (abdu x, y) iff both args are known positive
5277
5276
if (Opcode == ISD::ABDS && hasOperation(ISD::ABDU, VT) &&
5278
5277
DAG.SignBitIsZero(N0) && DAG.SignBitIsZero(N1))
0 commit comments