[llvm] [VPlan] Simplify (sdiv/srem lhs, (select header, rhs, 1)) -> (sdiv/srem lhs, rhs) (PR #174547)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 15 10:43:46 PST 2026


================
@@ -1444,6 +1444,49 @@ static void simplifyRecipe(VPSingleDefRecipe *Def, VPTypeAnalysis &TypeInfo) {
     return;
   }
 
+  // Drop header mask if LHS/RHS range is safe.
+  // (sdiv lhs, (select header, rhs, 1)) -> (sdiv lhs, rhs)
+  // (srem lhs, (select header, rhs, 1)) -> (srem lhs, rhs)
+  VPValue *LHS, *WSel, *RHS, *Mask;
+  if (match(Def, m_CombineOr(m_VPWidenRecipe<Instruction::SDiv>(
+                                 m_VPValue(LHS), m_VPValue(WSel)),
+                             m_VPWidenRecipe<Instruction::SRem>(
+                                 m_VPValue(LHS), m_VPValue(WSel)))) &&
+      match(WSel, m_Select(m_VPValue(Mask), m_VPValue(RHS), m_One())) &&
+      Plan->getVectorLoopRegion() && vputils::isHeaderMask(Mask, *Plan)) {
+    auto InductionWithoutN = [](VPValue *Recipe, APInt N) {
+      auto *IV = dyn_cast<VPWidenIntOrFpInductionRecipe>(Recipe);
+      if (!IV)
+        return false;
+      auto *Start = dyn_cast_or_null<ConstantInt>(
+          IV->getStartValue()->getUnderlyingValue());
+      auto *Increment = dyn_cast_or_null<ConstantInt>(
+          IV->getStepValue()->getUnderlyingValue());
+      if (!Start || !Increment)
+        return false;
+      APInt Range = N - Start->getValue();
----------------
fhahn wrote:

could you add some documentation here, explaining what N is etc to make it easier to follow the logic

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


More information about the llvm-commits mailing list