[llvm] [SCEV] Rewrite more SCEVAddExpr when applying guards. (PR #159942)
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 9 05:29:27 PDT 2025
================
@@ -16079,16 +16079,32 @@ const SCEV *ScalarEvolution::LoopGuards::rewrite(const SCEV *Expr) const {
}
const SCEV *visitAddExpr(const SCEVAddExpr *Expr) {
- // Trip count expressions sometimes consist of adding 3 operands, i.e.
- // (Const + A + B). There may be guard info for A + B, and if so, apply
- // it.
- // TODO: Could more generally apply guards to Add sub-expressions.
- if (isa<SCEVConstant>(Expr->getOperand(0)) &&
- Expr->getNumOperands() == 3) {
- if (const SCEV *S = Map.lookup(
- SE.getAddExpr(Expr->getOperand(1), Expr->getOperand(2))))
- return SE.getAddExpr(Expr->getOperand(0), S);
+ if (const SCEV *S = Map.lookup(Expr))
+ return S;
+ if (isa<SCEVConstant>(Expr->getOperand(0))) {
+ // Trip count expressions sometimes consist of adding 3 operands, i.e.
+ // (Const + A + B). There may be guard info for A + B, and if so, apply
+ // it.
+ // TODO: Could more generally apply guards to Add sub-expressions.
+ if (Expr->getNumOperands() == 3) {
+ if (const SCEV *S = Map.lookup(
+ SE.getAddExpr(Expr->getOperand(1), Expr->getOperand(2))))
+ return SE.getAddExpr(Expr->getOperand(0), S);
+ }
+
+ // For expressions of the form (Const + A), check if we have guard info
+ // for (Const + 1 + A), and rewrite to ((Const + 1 + A) - 1). This makes
+ // sure we don't loose information when rewriting expressions based on
+ // back-edge taken counts in some cases..
+ if (Expr->getNumOperands() == 2) {
+ auto *NewC =
+ SE.getAddExpr(Expr->getOperand(0), SE.getOne(Expr->getType()));
+ if (const SCEV *S =
+ Map.lookup(SE.getAddExpr(NewC, Expr->getOperand(1))))
+ return SE.getMinusSCEV(S, SE.getOne(Expr->getType()));
----------------
fhahn wrote:
Done thanks
https://github.com/llvm/llvm-project/pull/159942
More information about the llvm-commits
mailing list