[llvm] [VPlan] Optimize LastActiveLane to EVL - 1 (PR #169766)

Luke Lau via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 26 23:26:56 PST 2025


================
@@ -2787,13 +2788,22 @@ static VPRecipeBase *optimizeMaskToEVL(VPValue *HeaderMask,
             m_Select(m_Specific(HeaderMask), m_VPValue(LHS), m_VPValue(RHS))))
     return new VPWidenIntrinsicRecipe(
         Intrinsic::vp_merge, {Plan->getTrue(), LHS, RHS, &EVL},
-        TypeInfo.inferScalarType(LHS), {}, {}, CurRecipe.getDebugLoc());
+        TypeInfo.inferScalarType(LHS), {}, {}, DL);
 
   if (match(&CurRecipe, m_Select(m_RemoveMask(HeaderMask, Mask), m_VPValue(LHS),
                                  m_VPValue(RHS))))
     return new VPWidenIntrinsicRecipe(
         Intrinsic::vp_merge, {Mask, LHS, RHS, &EVL},
-        TypeInfo.inferScalarType(LHS), {}, {}, CurRecipe.getDebugLoc());
+        TypeInfo.inferScalarType(LHS), {}, {}, DL);
+
+  if (match(&CurRecipe, m_LastActiveLane(m_Specific(HeaderMask)))) {
+    Type *I64Ty = IntegerType::getInt64Ty(Plan->getContext());
+    VPValue *Zext = VPBuilder(&CurRecipe)
+                        .createScalarCast(Instruction::ZExt, &EVL, I64Ty, DL);
+    return new VPInstruction(
+        Instruction::Sub,
+        {Zext, Plan->getOrAddLiveIn(ConstantInt::get(I64Ty, 1))}, {}, {}, DL);
----------------
lukel97 wrote:

Oh is that new? Looks very handy, thanks for mentioning. Done in ce2e450c3c2d

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


More information about the llvm-commits mailing list