Skip to content

Commit 4bf67cd

Browse files
authored
[DenseMap] Fix constness issues with lookup_or (#139247)
Also demonstrate its use in ScalarEvolution.
1 parent 4304d90 commit 4bf67cd

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

llvm/include/llvm/ADT/DenseMap.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,7 @@ 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, ValueT &&Default) const {
225224
if (const BucketT *Bucket = doFind(Val))
226225
return Bucket->getSecond();
227226
return Default;

llvm/lib/Analysis/ScalarEvolution.cpp

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

1590615906
const SCEV *visitUnknown(const SCEVUnknown *Expr) {
15907-
auto I = Map.find(Expr);
15908-
if (I == Map.end())
15909-
return Expr;
15910-
return I->second;
15907+
return Map.lookup_or(Expr, Expr);
1591115908
}
1591215909

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

llvm/unittests/ADT/DenseMapTest.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,13 @@ TEST(DenseMapCustomTest, LookupOr) {
671671
EXPECT_EQ(M.lookup_or(2, 4u), 4u);
672672
}
673673

674+
TEST(DenseMapCustomTest, LookupOrConstness) {
675+
DenseMap<int, unsigned *> M;
676+
unsigned Default = 3u;
677+
unsigned *Ret = M.lookup_or(0, &Default);
678+
EXPECT_EQ(Ret, &Default);
679+
}
680+
674681
// Key traits that allows lookup with either an unsigned or char* key;
675682
// In the latter case, "a" == 0, "b" == 1 and so on.
676683
struct TestDenseMapInfo {

0 commit comments

Comments
 (0)