[llvm] [ConstraintElim] Don't request SCEV if there are no loops. (NFC) (PR #226736)
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Sun Sep 27 02:24:07 PDT 2026
https://github.com/fhahn updated https://github.com/llvm/llvm-project/pull/226736
>From c4453240417d8705b0752c3feda8d02365f8175c Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Sat, 26 Sep 2026 20:16:05 +0100
Subject: [PATCH] [ConstraintElim] Don't request SCEV if there are no loops.
(NFC)
---
.../Scalar/ConstraintElimination.cpp | 24 ++++++++++---------
.../analysis-invalidation.ll | 9 +++----
2 files changed, 16 insertions(+), 17 deletions(-)
diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index 0d1dae4e667c0..ae5f85374ad39 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -217,11 +217,12 @@ struct MonotonicInfo {
struct State {
DominatorTree &DT;
LoopInfo &LI;
- ScalarEvolution &SE;
+ /// Only available for functions with loops.
+ ScalarEvolution *SE;
TargetLibraryInfo &TLI;
SmallVector<FactOrCheck, 64> WorkList;
- State(DominatorTree &DT, LoopInfo &LI, ScalarEvolution &SE,
+ State(DominatorTree &DT, LoopInfo &LI, ScalarEvolution *SE,
TargetLibraryInfo &TLI)
: DT(DT), LI(LI), SE(SE), TLI(TLI) {}
@@ -1150,14 +1151,14 @@ MonotonicInfo State::getMonotonicityInfo(PHINode &PN, Value *Step) {
if (Info.Unsigned || Info.Signed || !StepOffset)
return Info;
- const auto *AR = dyn_cast<SCEVAddRecExpr>(SE.getSCEV(&PN));
+ const auto *AR = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(&PN));
if (!AR)
return Info;
ScalarEvolution::MonotonicPredicateType Expected =
Info.Decreasing ? ScalarEvolution::MonotonicallyDecreasing
: ScalarEvolution::MonotonicallyIncreasing;
auto IsMonotonic = [&](CmpInst::Predicate Pred) {
- return SE.getMonotonicPredicateType(AR, Pred) == Expected;
+ return SE->getMonotonicPredicateType(AR, Pred) == Expected;
};
Info.Signed = IsMonotonic(CmpInst::ICMP_SGT);
Info.Unsigned = !Info.Decreasing && IsMonotonic(CmpInst::ICMP_UGT);
@@ -1238,7 +1239,7 @@ void State::addInfoForInductions(BasicBlock &BB) {
}
if (PN->getParent() != Header || PN->getNumIncomingValues() != 2 ||
- !SE.isSCEVable(PN->getType()))
+ !SE->isSCEVable(PN->getType()))
return;
// For latch conditions, we need to inject the condition that holds for the
@@ -1307,7 +1308,7 @@ void State::addInfoForInductions(BasicBlock &BB) {
if (StepOffset->isZero())
return;
} else {
- const SCEV *Expr = SE.getSCEV(PN);
+ const SCEV *Expr = SE->getSCEV(PN);
if (!match(Expr,
m_scev_AffineAddRec(m_SCEV(StartSCEV), m_scev_APInt(StepOffset),
m_SpecificLoop(L))))
@@ -1362,10 +1363,10 @@ void State::addInfoForInductions(BasicBlock &BB) {
if (!StepOffset->isOne()) {
// Check whether B-Start is known to be a multiple of StepOffset.
if (!StartSCEV)
- StartSCEV = SE.getSCEV(StartValue);
- const SCEV *BMinusStart = SE.getMinusSCEV(SE.getSCEV(B), StartSCEV);
+ StartSCEV = SE->getSCEV(StartValue);
+ const SCEV *BMinusStart = SE->getMinusSCEV(SE->getSCEV(B), StartSCEV);
if (isa<SCEVCouldNotCompute>(BMinusStart) ||
- !SE.getConstantMultiple(BMinusStart).urem(*StepOffset).isZero())
+ !SE->getConstantMultiple(BMinusStart).urem(*StepOffset).isZero())
return;
}
@@ -2359,7 +2360,7 @@ tryToSimplifyOverflowMath(WithOverflowInst *II, ConstraintInfo &Info,
}
static bool eliminateConstraints(Function &F, DominatorTree &DT, LoopInfo &LI,
- ScalarEvolution &SE,
+ ScalarEvolution *SE,
OptimizationRemarkEmitter &ORE,
TargetLibraryInfo &TLI) {
bool Changed = false;
@@ -2698,7 +2699,8 @@ PreservedAnalyses ConstraintEliminationPass::run(Function &F,
FunctionAnalysisManager &AM) {
auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
auto &LI = AM.getResult<LoopAnalysis>(F);
- auto &SE = AM.getResult<ScalarEvolutionAnalysis>(F);
+ // SCEV is only used for loops, only construct it if there are some.
+ auto *SE = LI.empty() ? nullptr : &AM.getResult<ScalarEvolutionAnalysis>(F);
auto &ORE = AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
if (!eliminateConstraints(F, DT, LI, SE, ORE, TLI))
diff --git a/llvm/test/Transforms/ConstraintElimination/analysis-invalidation.ll b/llvm/test/Transforms/ConstraintElimination/analysis-invalidation.ll
index cb51f8c852503..85d310798fa52 100644
--- a/llvm/test/Transforms/ConstraintElimination/analysis-invalidation.ll
+++ b/llvm/test/Transforms/ConstraintElimination/analysis-invalidation.ll
@@ -12,9 +12,8 @@
; CHECK-NEXT: Running analysis: DominatorTreeAnalysis on ssub_no_overflow_due_to_or_conds
; CHECK-NEXT: Running pass: ConstraintEliminationPass on ssub_no_overflow_due_to_or_conds
; CHECK-NEXT: Running analysis: LoopAnalysis on ssub_no_overflow_due_to_or_conds
-; CHECK-NEXT: Running analysis: ScalarEvolutionAnalysis on ssub_no_overflow_due_to_or_conds
-; CHECK-NEXT: Running analysis: TargetLibraryAnalysis on ssub_no_overflow_due_to_or_conds
; CHECK-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis on ssub_no_overflow_due_to_or_conds
+; CHECK-NEXT: Running analysis: TargetLibraryAnalysis on ssub_no_overflow_due_to_or_conds
; CHECK-NEXT: Invalidating analysis: DemandedBitsAnalysis on ssub_no_overflow_due_to_or_conds
; CHECK-NEXT: Running pass: RequireAnalysisPass
; CHECK-NEXT: Running analysis: DemandedBitsAnalysis on ssub_no_overflow_due_to_or_conds
@@ -26,9 +25,8 @@
; CHECK-NEXT: Running analysis: DominatorTreeAnalysis on uge_zext
; CHECK-NEXT: Running pass: ConstraintEliminationPass on uge_zext
; CHECK-NEXT: Running analysis: LoopAnalysis on uge_zext
-; CHECK-NEXT: Running analysis: ScalarEvolutionAnalysis on uge_zext
-; CHECK-NEXT: Running analysis: TargetLibraryAnalysis on uge_zext
; CHECK-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis on uge_zext
+; CHECK-NEXT: Running analysis: TargetLibraryAnalysis on uge_zext
; CHECK-NEXT: Invalidating analysis: DemandedBitsAnalysis on uge_zext
; CHECK-NEXT: Running pass: RequireAnalysisPass
; CHECK-NEXT: Running analysis: DemandedBitsAnalysis on uge_zext
@@ -40,9 +38,8 @@
; CHECK-NEXT: Running analysis: DominatorTreeAnalysis on test_mul_const_nuw_unsigned_14
; CHECK-NEXT: Running pass: ConstraintEliminationPass on test_mul_const_nuw_unsigned_14
; CHECK-NEXT: Running analysis: LoopAnalysis on test_mul_const_nuw_unsigned_14
-; CHECK-NEXT: Running analysis: ScalarEvolutionAnalysis on test_mul_const_nuw_unsigned_14
-; CHECK-NEXT: Running analysis: TargetLibraryAnalysis on test_mul_const_nuw_unsigned_14
; CHECK-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis on test_mul_const_nuw_unsigned_14
+; CHECK-NEXT: Running analysis: TargetLibraryAnalysis on test_mul_const_nuw_unsigned_14
declare { i8, i1 } @llvm.ssub.with.overflow.i8(i8, i8)
More information about the llvm-commits
mailing list