[llvm] [DAG] Expand mask_beforefirst during promotion (PR #230163)

Luke Lau via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 8 08:49:01 PDT 2026


https://github.com/lukel97 created https://github.com/llvm/llvm-project/pull/230163

Fixed length mask vector types (v16i1 etc.) are promoted on NEON, so mask_beforefirst @ v16i1 becomes mask_beforefirst @ v16i8, which in turn gets expanded to get_active_lane_mask + cttz_elts @ v16i8, and finally undergoes generic expansion.

If we expand it earlier to get_active_lane_mask + cttz_elts @ v16i1, then AArch64 can custom lower it to AArch64ISD::CTTZ_ELTS and we get the good optimizeBrk lowering.


>From 519cc5a2d785b34116168dc4469cf4d5850f881f Mon Sep 17 00:00:00 2001
From: Luke Lau <luke at igalia.com>
Date: Thu, 8 Oct 2026 13:35:40 +0100
Subject: [PATCH] [DAG] Expand mask_beforefirst during promotion

Fixed length mask vector types (v16i1 etc.) are promoted on NEON, so mask_beforefirst @ v16i1 becomes mask_beforefirst @ v16i8, which in turn gets expanded to get_active_lane_mask + cttz_elts @ v16i8, and finally undergoes generic expansion.

If we expand it earlier to get_active_lane_mask + cttz_elts @ v16i1, then AArch64 can custom lower it to AArch64ISD::CTTZ_ELTS and we get the good optimizeBrk lowering.
---
 llvm/include/llvm/CodeGen/TargetLowering.h    |  5 ++
 .../SelectionDAG/LegalizeIntegerTypes.cpp     |  7 +++
 .../SelectionDAG/LegalizeVectorOps.cpp        |  9 +--
 .../CodeGen/SelectionDAG/TargetLowering.cpp   | 12 ++++
 .../CodeGen/AArch64/mask-beforefirst-sve.ll   | 55 ++++++-------------
 5 files changed, 41 insertions(+), 47 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/TargetLowering.h b/llvm/include/llvm/CodeGen/TargetLowering.h
index 9762184ca825f..bb549118ec0b8 100644
--- a/llvm/include/llvm/CodeGen/TargetLowering.h
+++ b/llvm/include/llvm/CodeGen/TargetLowering.h
@@ -5897,6 +5897,11 @@ class LLVM_ABI TargetLowering : public TargetLoweringBase {
   /// \returns The expansion result or SDValue() if it fails.
   SDValue expandLoopDependenceMask(SDNode *N, SelectionDAG &DAG) const;
 
+  /// Expand MASK_BEFOREFIRST nodes
+  /// \param N Node to expand
+  /// \returns The expansion result or SDValue() if it fails.
+  SDValue expandMaskBeforeFirst(SDNode *N, SelectionDAG &DAG) const;
+
   /// Expand ABS nodes. Expands vector/scalar ABS nodes,
   /// vector nodes can only succeed if all operations are legal/custom.
   /// (ABS x) -> (XOR (ADD x, (SRA x, type_size)), (SRA x, type_size))
diff --git a/llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp b/llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp
index 080cedce8a7df..51a9f2695483c 100644
--- a/llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp
@@ -6465,6 +6465,13 @@ SDValue DAGTypeLegalizer::PromoteIntRes_GET_ACTIVE_LANE_MASK(SDNode *N) {
 SDValue DAGTypeLegalizer::PromoteIntRes_MASK_BEFOREFIRST(SDNode *N) {
   EVT VT = N->getValueType(0);
   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
+
+  // If the promoted MASK_BEFOREFIRST isn't supported by the target, try to
+  // expand now so the target can lower cttz_elts on the original mask type.
+  if (!TLI.isOperationLegalOrCustom(ISD::MASK_BEFOREFIRST, NVT))
+    return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), NVT,
+                       TLI.expandMaskBeforeFirst(N, DAG));
+
   SDValue Op = PromoteTargetBoolean(N->getOperand(0), NVT);
   return DAG.getNode(ISD::MASK_BEFOREFIRST, SDLoc(N), NVT, Op);
 }
diff --git a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorOps.cpp b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorOps.cpp
index 55d445632a174..1a6b8158fd25d 100644
--- a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorOps.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorOps.cpp
@@ -1795,14 +1795,7 @@ SDValue VectorLegalizer::ExpandLOOP_DEPENDENCE_MASK(SDNode *N) {
 }
 
 SDValue VectorLegalizer::ExpandMASK_BEFOREFIRST(SDNode *N) {
-  // Expand to (get_active_lane_mask 0, (cttz_elts x))
-  SDLoc DL(N);
-  EVT VT = N->getValueType(0);
-  EVT VecIdxVT = TLI.getVectorIdxTy(DAG.getDataLayout());
-  SDValue CttzElts =
-      DAG.getNode(ISD::CTTZ_ELTS, DL, VecIdxVT, N->getOperand(0));
-  return DAG.getNode(ISD::GET_ACTIVE_LANE_MASK, DL, VT,
-                     DAG.getConstant(0, DL, VecIdxVT), CttzElts);
+  return TLI.expandMaskBeforeFirst(N, DAG);
 }
 
 SDValue VectorLegalizer::ExpandMaskedBinOp(SDNode *N) {
diff --git a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
index fcf989694c3c5..c9894dfc3384f 100644
--- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
@@ -11478,6 +11478,18 @@ SDValue TargetLowering::expandLoopDependenceMask(SDNode *N,
   return DAG.getNode(ISD::GET_ACTIVE_LANE_MASK, DL, VT, LaneOffset, MaskN);
 }
 
+SDValue TargetLowering::expandMaskBeforeFirst(SDNode *N,
+                                              SelectionDAG &DAG) const {
+  // Expand to (get_active_lane_mask 0, (cttz_elts x))
+  SDLoc DL(N);
+  EVT VT = N->getValueType(0);
+  EVT VecIdxVT = getVectorIdxTy(DAG.getDataLayout());
+  SDValue CttzElts =
+      DAG.getNode(ISD::CTTZ_ELTS, DL, VecIdxVT, N->getOperand(0));
+  return DAG.getNode(ISD::GET_ACTIVE_LANE_MASK, DL, VT,
+                     DAG.getConstant(0, DL, VecIdxVT), CttzElts);
+}
+
 SDValue TargetLowering::expandABS(SDNode *N, SelectionDAG &DAG,
                                   bool IsNegative) const {
   SDLoc dl(N);
diff --git a/llvm/test/CodeGen/AArch64/mask-beforefirst-sve.ll b/llvm/test/CodeGen/AArch64/mask-beforefirst-sve.ll
index 793a7a7303594..9f9d9029ee8df 100644
--- a/llvm/test/CodeGen/AArch64/mask-beforefirst-sve.ll
+++ b/llvm/test/CodeGen/AArch64/mask-beforefirst-sve.ll
@@ -41,22 +41,14 @@ define <vscale x 2 x i1> @nxv2i1(<vscale x 2 x i1> %m) {
   ret <vscale x 2 x i1> %x
 }
 
-; TODO: Use brkb for fixed length vectors.
-
 define <16 x i1> @v16i1(<16 x i1> %m) {
 ; CHECK-LABEL: v16i1:
 ; CHECK:       // %bb.0:
 ; CHECK-NEXT:    shl v0.16b, v0.16b, #7
-; CHECK-NEXT:    adrp x8, .LCPI4_0
-; CHECK-NEXT:    mov w9, #16 // =0x10
-; CHECK-NEXT:    ldr q1, [x8, :lo12:.LCPI4_0]
-; CHECK-NEXT:    cmlt v0.16b, v0.16b, #0
-; CHECK-NEXT:    and v0.16b, v0.16b, v1.16b
-; CHECK-NEXT:    umaxv b0, v0.16b
-; CHECK-NEXT:    fmov w8, s0
-; CHECK-NEXT:    sub w8, w9, w8, uxtb
-; CHECK-NEXT:    whilelo p0.b, xzr, x8
-; CHECK-NEXT:    mov z0.b, p0/z, #-1 // =0xffffffffffffffff
+; CHECK-NEXT:    ptrue p0.b, vl16
+; CHECK-NEXT:    cmpne p1.b, p0/z, z0.b, #0
+; CHECK-NEXT:    brkb p1.b, p0/z, p1.b
+; CHECK-NEXT:    mov z0.b, p1/z, #-1 // =0xffffffffffffffff
 ; CHECK-NEXT:    // kill: def $q0 killed $q0 killed $z0
 ; CHECK-NEXT:    ret
   %x = call <16 x i1> @llvm.mask.beforefirst(<16 x i1> %m)
@@ -67,15 +59,10 @@ define <8 x i1> @v8i1(<8 x i1> %m) {
 ; CHECK-LABEL: v8i1:
 ; CHECK:       // %bb.0:
 ; CHECK-NEXT:    shl v0.8b, v0.8b, #7
-; CHECK-NEXT:    index z1.b, #8, #-1
-; CHECK-NEXT:    mov w9, #8 // =0x8
-; CHECK-NEXT:    cmlt v0.8b, v0.8b, #0
-; CHECK-NEXT:    and v0.8b, v0.8b, v1.8b
-; CHECK-NEXT:    umaxv b0, v0.8b
-; CHECK-NEXT:    fmov w8, s0
-; CHECK-NEXT:    sub w8, w9, w8, uxtb
-; CHECK-NEXT:    whilelo p0.b, xzr, x8
-; CHECK-NEXT:    mov z0.b, p0/z, #-1 // =0xffffffffffffffff
+; CHECK-NEXT:    ptrue p0.b, vl8
+; CHECK-NEXT:    cmpne p1.b, p0/z, z0.b, #0
+; CHECK-NEXT:    brkb p1.b, p0/z, p1.b
+; CHECK-NEXT:    mov z0.b, p1/z, #-1 // =0xffffffffffffffff
 ; CHECK-NEXT:    // kill: def $d0 killed $d0 killed $z0
 ; CHECK-NEXT:    ret
   %x = call <8 x i1> @llvm.mask.beforefirst(<8 x i1> %m)
@@ -86,15 +73,10 @@ define <4 x i1> @v4i1(<4 x i1> %m) {
 ; CHECK-LABEL: v4i1:
 ; CHECK:       // %bb.0:
 ; CHECK-NEXT:    shl v0.4h, v0.4h, #15
-; CHECK-NEXT:    index z1.h, #4, #-1
-; CHECK-NEXT:    mov w9, #4 // =0x4
-; CHECK-NEXT:    cmlt v0.4h, v0.4h, #0
-; CHECK-NEXT:    and v0.8b, v0.8b, v1.8b
-; CHECK-NEXT:    umaxv h0, v0.4h
-; CHECK-NEXT:    fmov w8, s0
-; CHECK-NEXT:    sub w8, w9, w8, uxth
-; CHECK-NEXT:    whilelo p0.h, xzr, x8
-; CHECK-NEXT:    mov z0.h, p0/z, #-1 // =0xffffffffffffffff
+; CHECK-NEXT:    ptrue p0.h, vl4
+; CHECK-NEXT:    cmpne p1.h, p0/z, z0.h, #0
+; CHECK-NEXT:    brkb p1.b, p0/z, p1.b
+; CHECK-NEXT:    mov z0.h, p1/z, #-1 // =0xffffffffffffffff
 ; CHECK-NEXT:    // kill: def $d0 killed $d0 killed $z0
 ; CHECK-NEXT:    ret
   %x = call <4 x i1> @llvm.mask.beforefirst(<4 x i1> %m)
@@ -105,15 +87,10 @@ define <2 x i1> @v2i1(<2 x i1> %m) {
 ; CHECK-LABEL: v2i1:
 ; CHECK:       // %bb.0:
 ; CHECK-NEXT:    shl v0.2s, v0.2s, #31
-; CHECK-NEXT:    index z1.s, #2, #-1
-; CHECK-NEXT:    mov w9, #2 // =0x2
-; CHECK-NEXT:    cmlt v0.2s, v0.2s, #0
-; CHECK-NEXT:    and v0.8b, v0.8b, v1.8b
-; CHECK-NEXT:    umaxp v0.2s, v0.2s, v0.2s
-; CHECK-NEXT:    fmov w8, s0
-; CHECK-NEXT:    sub w8, w9, w8
-; CHECK-NEXT:    whilelo p0.s, xzr, x8
-; CHECK-NEXT:    mov z0.s, p0/z, #-1 // =0xffffffffffffffff
+; CHECK-NEXT:    ptrue p0.s, vl2
+; CHECK-NEXT:    cmpne p1.s, p0/z, z0.s, #0
+; CHECK-NEXT:    brkb p1.b, p0/z, p1.b
+; CHECK-NEXT:    mov z0.s, p1/z, #-1 // =0xffffffffffffffff
 ; CHECK-NEXT:    // kill: def $d0 killed $d0 killed $z0
 ; CHECK-NEXT:    ret
   %x = call <2 x i1> @llvm.mask.beforefirst(<2 x i1> %m)



More information about the llvm-commits mailing list