Skip to content

[DenseMap] Fix constness issues with lookup_or #139247

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions llvm/include/llvm/ADT/DenseMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,13 @@ class DenseMapBase : public DebugEpochBase {
// Return the entry with the specified key, or \p Default. This variant is
// useful, because `lookup` cannot be used with non-default-constructible
// values.
ValueT lookup_or(const_arg_type_t<KeyT> Val,
const_arg_type_t<ValueT> Default) const {
ValueT lookup_or(const_arg_type_t<KeyT> Val, const ValueT &Default) const {
if (const BucketT *Bucket = doFind(Val))
return Bucket->getSecond();
return Default;
}

ValueT lookup_or(const_arg_type_t<KeyT> Val, ValueT &&Default) const {
if (const BucketT *Bucket = doFind(Val))
return Bucket->getSecond();
return Default;
Expand Down
5 changes: 1 addition & 4 deletions llvm/lib/Analysis/ScalarEvolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15860,10 +15860,7 @@ const SCEV *ScalarEvolution::LoopGuards::rewrite(const SCEV *Expr) const {
const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) { return Expr; }

const SCEV *visitUnknown(const SCEVUnknown *Expr) {
auto I = Map.find(Expr);
if (I == Map.end())
return Expr;
return I->second;
return Map.lookup_or(Expr, Expr);
}

const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
Expand Down
4 changes: 3 additions & 1 deletion llvm/unittests/ADT/DenseMapTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,9 @@ TEST(DenseMapCustomTest, LookupOr) {

EXPECT_EQ(M.lookup_or(0, 4u), 3u);
EXPECT_EQ(M.lookup_or(1, 4u), 0u);
EXPECT_EQ(M.lookup_or(2, 4u), 4u);

NonDefaultConstructible DefaultV = M.lookup_or(2, 4u);
EXPECT_EQ(DefaultV, 4u);
}

// Key traits that allows lookup with either an unsigned or char* key;
Expand Down
Loading