[llvm] [SCEV] Check if predicate is known false for predicated AddRecs. (PR #151134)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 6 06:02:18 PDT 2025


================
@@ -14930,6 +14947,35 @@ const SCEVAddRecExpr *ScalarEvolution::convertSCEVToAddRecWithPredicates(
   if (!AddRec)
     return nullptr;
 
+  // Check if any of the transformed predicates is known to be false. In that
+  // case, it doesn't make sense to convert to an predicated AddRec, as the
+  // versioned loop will never execute.
+  for (const SCEVPredicate *Pred : TransformPreds) {
+    auto *WrapPred = dyn_cast<SCEVWrapPredicate>(Pred);
+    if (!WrapPred || WrapPred->getFlags() != SCEVWrapPredicate::IncrementNSSW)
+      continue;
+
+    const SCEVAddRecExpr *AddRecToCheck = WrapPred->getExpr();
+    const SCEV *ExitCount = getBackedgeTakenCount(AddRec->getLoop());
+    if (isa<SCEVCouldNotCompute>(ExitCount))
+      continue;
+
+    const SCEV *Step = AddRecToCheck->getStepRecurrence(*this);
+    if (!Step->isOne())
+      continue;
+
+    ExitCount = getTruncateOrSignExtend(ExitCount, Step->getType());
+    if (checkOverflow(OverflowCheckTy::WillOverflow, this, Instruction::Add,
+                      /*CheckSigned=*/true, AddRecToCheck->getStart(),
+                      ExitCount, nullptr)) {
+      return nullptr;
+    }
+    const SCEV *Add = getAddExpr(AddRecToCheck->getStart(), ExitCount);
+    if (isKnownPredicate(CmpInst::ICMP_SLT, Add, AddRecToCheck->getStart())) {
----------------
nikic wrote:

So I think ultimately only this check is actually used?

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


More information about the llvm-commits mailing list