Skip to content

Commit 9c22c18

Browse files
committed
[DenseMap] Fix constness issues with lookup_or
Also demonstrate its use in ScalarEvolution.
1 parent 68dccb9 commit 9c22c18

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

llvm/include/llvm/ADT/DenseMap.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,13 @@ class DenseMapBase : public DebugEpochBase {
220220
// Return the entry with the specified key, or \p Default. This variant is
221221
// useful, because `lookup` cannot be used with non-default-constructible
222222
// values.
223-
ValueT lookup_or(const_arg_type_t<KeyT> Val,
224-
const_arg_type_t<ValueT> Default) const {
223+
ValueT lookup_or(const_arg_type_t<KeyT> Val, const ValueT &Default) const {
224+
if (const BucketT *Bucket = doFind(Val))
225+
return Bucket->getSecond();
226+
return Default;
227+
}
228+
229+
ValueT lookup_or(const_arg_type_t<KeyT> Val, ValueT &&Default) const {
225230
if (const BucketT *Bucket = doFind(Val))
226231
return Bucket->getSecond();
227232
return Default;

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15860,10 +15860,7 @@ const SCEV *ScalarEvolution::LoopGuards::rewrite(const SCEV *Expr) const {
1586015860
const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) { return Expr; }
1586115861

1586215862
const SCEV *visitUnknown(const SCEVUnknown *Expr) {
15863-
auto I = Map.find(Expr);
15864-
if (I == Map.end())
15865-
return Expr;
15866-
return I->second;
15863+
return Map.lookup_or(Expr, Expr);
1586715864
}
1586815865

1586915866
const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {

llvm/unittests/ADT/DenseMapTest.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,9 @@ TEST(DenseMapCustomTest, LookupOr) {
668668

669669
EXPECT_EQ(M.lookup_or(0, 4u), 3u);
670670
EXPECT_EQ(M.lookup_or(1, 4u), 0u);
671-
EXPECT_EQ(M.lookup_or(2, 4u), 4u);
671+
672+
NonDefaultConstructible DefaultV = M.lookup_or(2, 4u);
673+
EXPECT_EQ(DefaultV, 4u);
672674
}
673675

674676
// Key traits that allows lookup with either an unsigned or char* key;

0 commit comments

Comments
 (0)