-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[SCEV] Improve code using DenseMap::lookup (NFC) #147507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Member
|
@llvm/pr-subscribers-llvm-analysis Author: Ramkumar Ramachandra (artagnon) ChangesFull diff: https://github.com/llvm/llvm-project/pull/147507.diff 1 Files Affected:
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index dd309bc2c54a8..db0d4824fe421 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -1559,9 +1559,8 @@ ScalarEvolution::getZeroExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth) {
Ty = getEffectiveSCEVType(Ty);
FoldID ID(scZeroExtend, Op, Ty);
- auto Iter = FoldCache.find(ID);
- if (Iter != FoldCache.end())
- return Iter->second;
+ if (const SCEV *S = FoldCache.lookup(ID))
+ return S;
const SCEV *S = getZeroExtendExprImpl(Op, Ty, Depth);
if (!isa<SCEVZeroExtendExpr>(S))
@@ -1894,9 +1893,8 @@ ScalarEvolution::getSignExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth) {
Ty = getEffectiveSCEVType(Ty);
FoldID ID(scSignExtend, Op, Ty);
- auto Iter = FoldCache.find(ID);
- if (Iter != FoldCache.end())
- return Iter->second;
+ if (const SCEV *S = FoldCache.lookup(ID))
+ return S;
const SCEV *S = getSignExtendExprImpl(Op, Ty, Depth);
if (!isa<SCEVSignExtendExpr>(S))
@@ -8494,9 +8492,8 @@ void ScalarEvolution::forgetLoop(const Loop *L) {
++I;
}
- auto LoopUsersItr = LoopUsers.find(CurrL);
- if (LoopUsersItr != LoopUsers.end())
- llvm::append_range(ToForget, LoopUsersItr->second);
+ auto Users = LoopUsers.lookup(CurrL);
+ llvm::append_range(ToForget, Users);
// Drop information about expressions based on loop-header PHIs.
PushLoopPHIs(CurrL, Worklist, Visited);
@@ -8597,11 +8594,10 @@ void ScalarEvolution::forgetBlockAndLoopDispositions(Value *V) {
bool BlockDispoRemoved = BlockDispositions.erase(Curr);
if (!LoopDispoRemoved && !BlockDispoRemoved)
continue;
- auto Users = SCEVUsers.find(Curr);
- if (Users != SCEVUsers.end())
- for (const auto *User : Users->second)
- if (Seen.insert(User).second)
- Worklist.push_back(User);
+ auto Users = SCEVUsers.lookup(Curr);
+ for (const SCEV *User : Users)
+ if (Seen.insert(User).second)
+ Worklist.push_back(User);
}
}
@@ -14496,8 +14492,8 @@ void ScalarEvolution::verify() const {
#endif
// Check that the value is also part of the reverse map.
- auto It = ExprValueMap.find(KV.second);
- if (It == ExprValueMap.end() || !It->second.contains(KV.first)) {
+ auto V = ExprValueMap.lookup(KV.second);
+ if (!V.contains(KV.first)) {
dbgs() << "Value " << *KV.first
<< " is in ValueExprMap but not in ExprValueMap\n";
std::abort();
@@ -14521,15 +14517,15 @@ void ScalarEvolution::verify() const {
for (const auto &KV : ExprValueMap) {
for (Value *V : KV.second) {
- auto It = ValueExprMap.find_as(V);
- if (It == ValueExprMap.end()) {
+ const SCEV *S = ValueExprMap.lookup(V);
+ if (!S) {
dbgs() << "Value " << *V
<< " is in ExprValueMap but not in ValueExprMap\n";
std::abort();
}
- if (It->second != KV.first) {
- dbgs() << "Value " << *V << " mapped to " << *It->second
- << " rather than " << *KV.first << "\n";
+ if (S != KV.first) {
+ dbgs() << "Value " << *V << " mapped to " << *S << " rather than "
+ << *KV.first << "\n";
std::abort();
}
}
@@ -14541,8 +14537,8 @@ void ScalarEvolution::verify() const {
// We do not store dependencies of constants.
if (isa<SCEVConstant>(Op))
continue;
- auto It = SCEVUsers.find(Op);
- if (It != SCEVUsers.end() && It->second.count(&S))
+ auto Users = SCEVUsers.lookup(Op);
+ if (Users.contains(&S))
continue;
dbgs() << "Use of operand " << *Op << " by user " << S
<< " is not being tracked!\n";
@@ -14557,9 +14553,8 @@ void ScalarEvolution::verify() const {
const Loop *L = LoopAndValueAtScope.first;
const SCEV *ValueAtScope = LoopAndValueAtScope.second;
if (!isa<SCEVConstant>(ValueAtScope)) {
- auto It = ValuesAtScopesUsers.find(ValueAtScope);
- if (It != ValuesAtScopesUsers.end() &&
- is_contained(It->second, std::make_pair(L, Value)))
+ auto V = ValuesAtScopesUsers.lookup(ValueAtScope);
+ if (is_contained(V, std::make_pair(L, Value)))
continue;
dbgs() << "Value: " << *Value << ", Loop: " << *L << ", ValueAtScope: "
<< *ValueAtScope << " missing in ValuesAtScopesUsers\n";
@@ -14574,9 +14569,8 @@ void ScalarEvolution::verify() const {
const Loop *L = LoopAndValue.first;
const SCEV *Value = LoopAndValue.second;
assert(!isa<SCEVConstant>(Value));
- auto It = ValuesAtScopes.find(Value);
- if (It != ValuesAtScopes.end() &&
- is_contained(It->second, std::make_pair(L, ValueAtScope)))
+ auto V = ValuesAtScopes.lookup(Value);
+ if (is_contained(V, std::make_pair(L, ValueAtScope)))
continue;
dbgs() << "Value: " << *Value << ", Loop: " << *L << ", ValueAtScope: "
<< *ValueAtScope << " missing in ValuesAtScopes\n";
@@ -14592,9 +14586,8 @@ void ScalarEvolution::verify() const {
for (const ExitNotTakenInfo &ENT : LoopAndBEInfo.second.ExitNotTaken) {
for (const SCEV *S : {ENT.ExactNotTaken, ENT.SymbolicMaxNotTaken}) {
if (!isa<SCEVConstant>(S)) {
- auto UserIt = BECountUsers.find(S);
- if (UserIt != BECountUsers.end() &&
- UserIt->second.contains({ LoopAndBEInfo.first, Predicated }))
+ auto Users = BECountUsers.lookup(S);
+ if (Users.contains({LoopAndBEInfo.first, Predicated}))
continue;
dbgs() << "Value " << *S << " for loop " << *LoopAndBEInfo.first
<< " missing from BECountUsers\n";
@@ -14635,28 +14628,28 @@ void ScalarEvolution::verify() const {
// Verify FoldCache/FoldCacheUser caches.
for (auto [FoldID, Expr] : FoldCache) {
- auto I = FoldCacheUser.find(Expr);
- if (I == FoldCacheUser.end()) {
+ auto F = FoldCacheUser.lookup(Expr);
+ if (F.empty()) {
dbgs() << "Missing entry in FoldCacheUser for cached expression " << *Expr
<< "!\n";
std::abort();
}
- if (!is_contained(I->second, FoldID)) {
+ if (!is_contained(F, FoldID)) {
dbgs() << "Missing FoldID in cached users of " << *Expr << "!\n";
std::abort();
}
}
for (auto [Expr, IDs] : FoldCacheUser) {
for (auto &FoldID : IDs) {
- auto I = FoldCache.find(FoldID);
- if (I == FoldCache.end()) {
+ const SCEV *S = FoldCache.lookup(FoldID);
+ if (!S) {
dbgs() << "Missing entry in FoldCache for expression " << *Expr
<< "!\n";
std::abort();
}
- if (I->second != Expr) {
- dbgs() << "Entry in FoldCache doesn't match FoldCacheUser: "
- << *I->second << " != " << *Expr << "!\n";
+ if (S != Expr) {
+ dbgs() << "Entry in FoldCache doesn't match FoldCacheUser: " << *S
+ << " != " << *Expr << "!\n";
std::abort();
}
}
@@ -15624,8 +15617,7 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
// 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;
+ return RewriteMap.lookup_or(S, S);
};
// Check for the SCEV expression (A /u B) * B while B is a constant, inside
@@ -15928,9 +15920,8 @@ const SCEV *ScalarEvolution::LoopGuards::rewrite(const SCEV *Expr) const {
Bitwidth > Op->getType()->getScalarSizeInBits()) {
Type *NarrowTy = IntegerType::get(SE.getContext(), Bitwidth);
auto *NarrowExt = SE.getZeroExtendExpr(Op, NarrowTy);
- auto I = Map.find(NarrowExt);
- if (I != Map.end())
- return SE.getZeroExtendExpr(I->second, Ty);
+ if (const SCEV *S = Map.lookup(NarrowExt))
+ return SE.getZeroExtendExpr(S, Ty);
Bitwidth = Bitwidth / 2;
}
|
nikic
requested changes
Jul 8, 2025
308cfb8 to
f735f65
Compare
nikic
approved these changes
Jul 8, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.