[llvm] [SCEV] Retain SCEVSequentialMinMaxExpr if an operand may trigger UB. (PR #110824)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 7 02:53:21 PDT 2024


================
@@ -4304,6 +4304,16 @@ ScalarEvolution::getSequentialMinMaxExpr(SCEVTypes Kind,
   }
 
   for (unsigned i = 1, e = Ops.size(); i != e; ++i) {
+    bool MayBeUB = SCEVExprContains(Ops[i], [this](const SCEV *S) {
+      auto *UDiv = dyn_cast<SCEVUDivExpr>(S);
+      // The UDiv may be UB if the divisor is poison or zero. Unless the divisor
+      // is a non-zero constant, we have to assume the UDiv may be UB.
+      return UDiv && (!isa<SCEVConstant>(UDiv->getOperand(1)) ||
+                      !isKnownNonZero(UDiv->getOperand(1)));
----------------
nikic wrote:

As it looks like we're going to need it anyway, I think I'd add a isGuaranteedNotToBePoison() method (as a thin wrapper around SCEVPoisonCollector) in this PR and write this as `!isKnownNonZero || !isGuaranteedNotToBePoison`. I find the current way this is checked pretty confusing (at least it should mention that the reason for the SCEVConstant check is poison).

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


More information about the llvm-commits mailing list