@@ -5213,6 +5213,7 @@ SDValue DAGCombiner::visitAVG(SDNode *N) {
52135213 SDValue N1 = N->getOperand(1);
52145214 EVT VT = N->getValueType(0);
52155215 SDLoc DL(N);
5216+ bool IsSigned = Opcode == ISD::AVGCEILS || Opcode == ISD::AVGFLOORS;
52165217
52175218 // fold (avg c1, c2)
52185219 if (SDValue C = DAG.FoldConstantArithmetic(Opcode, DL, VT, {N0, N1}))
@@ -5248,33 +5249,19 @@ SDValue DAGCombiner::visitAVG(SDNode *N) {
52485249
52495250 // fold avgu(zext(x), zext(y)) -> zext(avgu(x, y))
52505251 // fold avgs(sext(x), sext(y)) -> sext(avgs(x, y))
5251- if (sd_match(
5252- N, m_BinOp(ISD::AVGFLOORU , m_ZExt(m_Value(X)), m_ZExt(m_Value(Y)))) &&
5252+ if (!IsSigned &&
5253+ sd_match( N, m_BinOp(Opcode , m_ZExt(m_Value(X)), m_ZExt(m_Value(Y)))) &&
52535254 X.getValueType() == Y.getValueType() &&
5254- hasOperation(ISD::AVGFLOORU , X.getValueType())) {
5255- SDValue AvgFloorU = DAG.getNode(ISD::AVGFLOORU , DL, X.getValueType(), X, Y);
5256- return DAG.getNode(ISD::ZERO_EXTEND, DL, VT, AvgFloorU );
5255+ hasOperation(Opcode , X.getValueType())) {
5256+ SDValue AvgU = DAG.getNode(Opcode , DL, X.getValueType(), X, Y);
5257+ return DAG.getNode(ISD::ZERO_EXTEND, DL, VT, AvgU );
52575258 }
5258- if (sd_match(
5259- N, m_BinOp(ISD::AVGCEILU, m_ZExt (m_Value(X)), m_ZExt (m_Value(Y)))) &&
5259+ if (IsSigned &&
5260+ sd_match( N, m_BinOp(Opcode, m_SExt (m_Value(X)), m_SExt (m_Value(Y)))) &&
52605261 X.getValueType() == Y.getValueType() &&
5261- hasOperation(ISD::AVGCEILU, X.getValueType())) {
5262- SDValue AvgCeilU = DAG.getNode(ISD::AVGCEILU, DL, X.getValueType(), X, Y);
5263- return DAG.getNode(ISD::ZERO_EXTEND, DL, VT, AvgCeilU);
5264- }
5265- if (sd_match(
5266- N, m_BinOp(ISD::AVGFLOORS, m_SExt(m_Value(X)), m_SExt(m_Value(Y)))) &&
5267- X.getValueType() == Y.getValueType() &&
5268- hasOperation(ISD::AVGFLOORS, X.getValueType())) {
5269- SDValue AvgFloorS = DAG.getNode(ISD::AVGFLOORS, DL, X.getValueType(), X, Y);
5270- return DAG.getNode(ISD::SIGN_EXTEND, DL, VT, AvgFloorS);
5271- }
5272- if (sd_match(
5273- N, m_BinOp(ISD::AVGCEILS, m_SExt(m_Value(X)), m_SExt(m_Value(Y)))) &&
5274- X.getValueType() == Y.getValueType() &&
5275- hasOperation(ISD::AVGCEILS, X.getValueType())) {
5276- SDValue AvgCeilS = DAG.getNode(ISD::AVGCEILS, DL, X.getValueType(), X, Y);
5277- return DAG.getNode(ISD::SIGN_EXTEND, DL, VT, AvgCeilS);
5262+ hasOperation(Opcode, X.getValueType())) {
5263+ SDValue AvgS = DAG.getNode(Opcode, DL, X.getValueType(), X, Y);
5264+ return DAG.getNode(ISD::SIGN_EXTEND, DL, VT, AvgS);
52785265 }
52795266
52805267 // Fold avgflooru(x,y) -> avgceilu(x,y-1) iff y != 0
0 commit comments