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

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 15 04:08:49 PDT 2024


================
@@ -676,7 +676,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 &&
+      (!isa<SCEVConstant>(RHSExpr) || SE.isKnownNonZero(RHSExpr))) {
+    if (!isa<SCEVConstant>(S->getRHS()))
----------------
fhahn wrote:

Thanks, updated. This removes the freezes in a few cases, but that should be fine as in those cases the function would always trigger UB, so expanding the UDiv before the loop simply triggers UB earlier (e.g. `multi_exit_4_exit_count_with_udiv_by_value_in_latch`). This should be fine I think (https://alive2.llvm.org/ce/z/anDrW9).

Added additional variants where this is not the case (`multi_exit_4_exit_count_with_udiv_by_value_in_latch_call_before_loop`, `multi_exit_4_exit_count_with_udiv_by_value_in_latch_loop_may_not_execute`) and there's also `multi_exit_4_exit_count_with_udiv_by_value_in_latch_different_bounds`

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


More information about the llvm-commits mailing list