[llvm] [SCEVExpander] Expand UDiv avoiding UB when in seq_min/max. (PR #92177)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 16 03:04:51 PDT 2024


================
@@ -681,7 +681,15 @@ Value *SCEVExpander::visitUDivExpr(const SCEVUDivExpr *S) {
                          SCEV::FlagAnyWrap, /*IsSafeToHoist*/ true);
   }
 
-  Value *RHS = expand(S->getRHS());
+  const SCEV *RHSExpr = S->getRHS();
+  Value *RHS = expand(RHSExpr);
+  if (SafeUDivMode) {
+    if (!ScalarEvolution::isGuaranteedNotToBePoison(RHSExpr))
+      RHS = Builder.CreateFreeze(RHS);
+    if (!SE.isKnownNonZero(RHSExpr))
+      RHS = Builder.CreateIntrinsic(RHS->getType(), Intrinsic::umax,
+                                    {RHS, ConstantInt::get(RHS->getType(), 1)});
----------------
nikic wrote:

This logic still doesn't look quite right to me. If you have isKnownNonZero but not isGuaranteedNotToBePoison, then you still need the umax, because you no longer know that it's non-zero after the freeze.

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


More information about the llvm-commits mailing list