[llvm] [LLVM] Add `llvm.masked.compress` intrinsic (PR #92289)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 19 02:43:05 PDT 2024


================
@@ -12078,6 +12080,68 @@ SDValue DAGCombiner::visitVP_STRIDED_STORE(SDNode *N) {
   return SDValue();
 }
 
+SDValue DAGCombiner::visitMASKED_COMPRESS(SDNode *N) {
+  SDLoc DL(N);
+  SDValue Vec = N->getOperand(0);
+  SDValue Mask = N->getOperand(1);
+  SDValue Passthru = N->getOperand(2);
+  EVT VecVT = Vec.getValueType();
+
+  bool HasPassthru = !Passthru.isUndef();
+
+  APInt SplatVal;
+  if (ISD::isConstantSplatVector(Mask.getNode(), SplatVal)) {
+    bool HasTrueBoolContent = [&] {
+      switch (TLI.getBooleanContents(Mask.getValueType())) {
+      case TargetLoweringBase::UndefinedBooleanContent:
+        return SplatVal.isOne();
+      case TargetLoweringBase::ZeroOrOneBooleanContent:
+        return SplatVal.isOneBitSet(0);
+      case TargetLoweringBase::ZeroOrNegativeOneBooleanContent:
+        return SplatVal.isAllOnes();
+      }
+    }();
+
+    return HasTrueBoolContent ? Vec
+                              : (HasPassthru ? Passthru : DAG.getUNDEF(VecVT));
+  }
+
+  if (Vec.isUndef() || Mask.isUndef())
+    return DAG.getUNDEF(VecVT);
+
+  // No need for potentially expensive compress if the mask is constant.
+  if (ISD::isBuildVectorOfConstantSDNodes(Mask.getNode())) {
+    SmallVector<SDValue, 16> Ops;
+    EVT ScalarVT = VecVT.getVectorElementType();
+    unsigned NumSelected = 0;
+    unsigned NumElmts = VecVT.getVectorNumElements();
+    for (unsigned I = 0; I < NumElmts; ++I) {
+      SDValue MaskI = Mask.getOperand(I);
+      if (MaskI.isUndef())
+        continue;
+
+      ConstantSDNode *CMaskI = cast<ConstantSDNode>(MaskI);
+      if (CMaskI->isAllOnes()) {
----------------
RKSimon wrote:

`TLI.isConstTrueVal(MaskI)` ?

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


More information about the llvm-commits mailing list