[llvm] [LV] Don't vectorize if trip count expansion may introduce UB. (PR #92177)

Eli Friedman via llvm-commits llvm-commits at lists.llvm.org
Wed May 15 10:50:25 PDT 2024


================
@@ -161,6 +161,11 @@ SCEVExpander::findInsertPointAfter(Instruction *I,
   return IP;
 }
 
+bool SCEVExpander::expansionMayIntroduceUB(const SCEV *Expr) {
+  return SCEVExprContains(Expr,
+                          [](const SCEV *Op) { return isa<SCEVUDivExpr>(Op); });
----------------
efriedma-quic wrote:

> e.g. we have two exit counts 1) `c` and 2) `a - (42/b)`, with the BTC being `umin(c, a - (42/b))`. If `b == 0` then the BTC should be `c`, but using 1 as divisor may result in `a - (42/(umax 1, b)) < c` and we would use an incorrect BTC?

In the given example, `c` has to be zero, I think?  If `c` isn't zero, and `b` is zero, we divide by zero even before any optimizations run.

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


More information about the llvm-commits mailing list