[llvm] Matched some basic ISD::AVGFLOORU patterns (PR #84903)

Shourya Goel via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 12 15:17:47 PDT 2024


================
@@ -2821,6 +2821,38 @@ SDValue DAGCombiner::visitADDLike(SDNode *N) {
   return SDValue();
 }
 
+// Attempt to form ext(avgflooru(A, B)) from add(and(A, B), lshr(xor(A, B), 1))
+static SDValue combineFixedwidthToAVG(SDNode *N, SelectionDAG &DAG) {
+  assert(N->getOpcode() == ISD::ADD && "ADD node is required here");
+  SDValue And = N->getOperand(0);
+  SDValue Lshr = N->getOperand(1);
+  if (And.getOpcode() != ISD::AND || Lshr.getOpcode() != ISD::SRL)
+    return SDValue();
+  SDValue Xor = Lshr.getOperand(0);
+  if (Xor.getOpcode() != ISD::XOR)
+    return SDValue();
+  SDValue And1 = And.getOperand(0);
+  SDValue And2 = And.getOperand(1);
+  SDValue Xor1 = Xor.getOperand(0);
+  SDValue Xor2 = Xor.getOperand(1);
+  if (Xor1 != And1 && Xor2 != And2)
+    return SDValue();
+  // Is the right shift using an immediate value of 1?
+  ConstantSDNode *N1C = isConstOrConstSplat(Lshr.getOperand(1));
+  if (!N1C || N1C->getAPIntValue() != 1)
+    return SDValue();
+  EVT VT = And.getValueType();
+  if (VT.isVector())
----------------
Sh0g0-1758 wrote:

Could certainly need a little help with vectors. This is what I have till now : 

```
  EVT VT = And1.getValueType();
  EVT NVT = EVT::getIntegerVT(*DAG.getContext(), VT.getScalarSizeInBits());
  if (VT.isVector())
    NVT = EVT::getVectorVT(*DAG.getContext(), NVT, VT.getVectorElementCount());
  SDLoc DL(N);
  const TargetLowering &TLI = DAG.getTargetLoweringInfo();
  if (!TLI.isOperationLegalOrCustom(ISD::AVGFLOORU, NVT))
    return SDValue();
  return DAG.getNode(ISD::AVGFLOORU, DL, NVT, And1, And2);
```

The test for vector is failing while that for i4 is passing. 

https://github.com/llvm/llvm-project/pull/84903


More information about the llvm-commits mailing list