aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Analysis/ScalarEvolution.cpp
diff options
context:
space:
mode:
authorDmitry Makogon <d.makogon@g.nsu.ru>2023-03-03 18:02:46 +0700
committerDmitry Makogon <d.makogon@g.nsu.ru>2023-03-03 19:22:28 +0700
commit94b35eef4e1837a40643ca64ff8bfd319d166e67 (patch)
treee9a5962a84f3e98e9424b1fe08ea9a904757801f /llvm/lib/Analysis/ScalarEvolution.cpp
parentf5097ed8469416b30d2e9e449a26079f85ccb2f1 (diff)
downloadllvm-94b35eef4e1837a40643ca64ff8bfd319d166e67.zip
llvm-94b35eef4e1837a40643ca64ff8bfd319d166e67.tar.gz
llvm-94b35eef4e1837a40643ca64ff8bfd319d166e67.tar.bz2
[ScalarEvolution] Factor out RewriteMap utilities in applyLoopGuards (NFC)
This factors out two utilities used with RewriteMap in applyLoopGuards: - AddRewrite, which puts a rewrite rule in the map and if needed registers the rewrite in the list of rewritten expressions, - GetMaybeRewritten, which checks whether an expression has already been rewritten, and if so, returns the rewrite. Otherwise, returns the given expression. This may be needed when adding new rewrite rules as not to copy-paste this code.
Diffstat (limited to 'llvm/lib/Analysis/ScalarEvolution.cpp')
-rw-r--r--llvm/lib/Analysis/ScalarEvolution.cpp31
1 files changed, 22 insertions, 9 deletions
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index b074294..ada4f84 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -15052,10 +15052,26 @@ const SCEV *ScalarEvolution::applyLoopGuards(const SCEV *Expr, const Loop *L) {
Predicate = CmpInst::getSwappedPredicate(Predicate);
}
- // Check whether LHS has already been rewritten. In that case we want to
- // chain further rewrites onto the already rewritten value.
- auto I = RewriteMap.find(LHS);
- const SCEV *RewrittenLHS = I != RewriteMap.end() ? I->second : LHS;
+ // Puts rewrite rule \p From -> \p To into the rewrite map. Also if \p From
+ // and \p FromRewritten are the same (i.e. there has been no rewrite
+ // registered for \p From), then puts this value in the list of rewritten
+ // expressions.
+ auto AddRewrite = [&](const SCEV *From, const SCEV *FromRewritten,
+ const SCEV *To) {
+ if (From == FromRewritten)
+ ExprsToRewrite.push_back(From);
+ RewriteMap[From] = To;
+ };
+
+ // Checks whether \p S has already been rewritten. In that case returns the
+ // existing rewrite because we want to chain further rewrites onto the
+ // already rewritten value. Otherwise returns \p S.
+ auto GetMaybeRewritten = [&](const SCEV *S) {
+ auto I = RewriteMap.find(S);
+ return I != RewriteMap.end() ? I->second : S;
+ };
+
+ const SCEV *RewrittenLHS = GetMaybeRewritten(LHS);
const SCEV *RewrittenRHS = nullptr;
switch (Predicate) {
@@ -15104,11 +15120,8 @@ const SCEV *ScalarEvolution::applyLoopGuards(const SCEV *Expr, const Loop *L) {
break;
}
- if (RewrittenRHS) {
- RewriteMap[LHS] = RewrittenRHS;
- if (LHS == RewrittenLHS)
- ExprsToRewrite.push_back(LHS);
- }
+ if (RewrittenRHS)
+ AddRewrite(LHS, RewrittenLHS, RewrittenRHS);
};
BasicBlock *Header = L->getHeader();