[llvm] [VPlan] Fold safe divisors into VP intrinsics with EVL (PR #148828)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 4 04:58:10 PDT 2025


================
@@ -2176,6 +2176,52 @@ static VPRecipeBase *optimizeMaskToEVL(VPValue *HeaderMask,
       .Default([&](VPRecipeBase *R) { return nullptr; });
 }
 
+/// Try to optimize safe divisors away by converting their users to VP
+/// intrinsics:
+///
+/// udiv x, (vp.merge allones, y, 1, evl) -> vp.udiv x, y, allones, evl
+///
+/// Note the lanes past EVL will be changed from x to poison. This only works
+/// for the EVL-based IV and not any arbitrary EVL, because we know nothing
+/// will read the lanes past the EVL-based IV.
----------------
fhahn wrote:

Hmm, is one potential issue here that replacing regular `udiv` with `vp.udiv` makes the result more poisonous?  Previously all lanes of the udiv would be non-poison. After the transform, the masked out lanes would be poison.

Some instructions have unconditional UB on poison (like udiv,sdiv I think, as `poison` could be 0). Now if there's such an unpredicated user, we will trigger immediate UB

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


More information about the llvm-commits mailing list