[llvm] [DA] do not handle array accesses of different offsets (PR #123436)
Sebastian Pop via llvm-commits
llvm-commits at lists.llvm.org
Mon May 12 18:58:06 PDT 2025
================
@@ -10971,6 +10971,52 @@ bool ScalarEvolution::isKnownToBeAPowerOfTwo(const SCEV *S, bool OrZero,
return all_of(Mul->operands(), NonRecursive) && (OrZero || isKnownNonZero(S));
}
+bool ScalarEvolution::isKnownMultipleOf(
+ const SCEV *S, uint64_t M,
+ SmallVectorImpl<const SCEVPredicate *> &Assumptions) {
+ if (M == 0)
+ return false;
+ if (M == 1)
+ return true;
+
+ // Recursively check AddRec operands.
+ if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(S))
+ return isKnownMultipleOf(AddRec->getStart(), M, Assumptions) &&
+ isKnownMultipleOf(AddRec->getStepRecurrence(*this), M, Assumptions);
+
+ // For a constant, check that "S % M == 0".
+ if (auto *Cst = dyn_cast<SCEVConstant>(S)) {
+ APInt C = Cst->getAPInt();
+ return C.urem(M) == 0;
+ }
+
+ // Basic tests have failed.
+ // Check "S % M == 0" at compile time and record runtime Assumptions.
+ auto *STy = dyn_cast<IntegerType>(S->getType());
+ const SCEV *SmodM =
+ getURemExpr(S, getConstant(ConstantInt::get(STy, M, false)));
+ const SCEV *Zero = getZero(STy);
+
+ // Check whether "S % M == 0" is known at compile time.
+ if (isKnownPredicate(ICmpInst::ICMP_EQ, SmodM, Zero))
+ return true;
+
+ // Check whether "S % M != 0" is known at compile time.
+ if (isKnownPredicate(ICmpInst::ICMP_NE, SmodM, Zero))
+ return false;
+
+ const SCEVPredicate *P = getComparePredicate(ICmpInst::ICMP_EQ, SmodM, Zero);
+
+ // Detect redundant predicates.
+ for (auto *A : Assumptions)
+ if (A->implies(P, *this))
+ return true;
+
+ // Only record non-redundant predicates.
+ Assumptions.push_back(P);
----------------
sebpop wrote:
I added a TODO comment just before we start building the runtime predicate:
```
+ // TODO: Also check other SCEV expressions, i.e., SCEVAddRecExpr, etc.
+
// Basic tests have failed.
// Check "S % M == 0" at compile time and record runtime Assumptions.
auto *STy = dyn_cast<IntegerType>(S->getType());
```
https://github.com/llvm/llvm-project/pull/123436
More information about the llvm-commits
mailing list