[DenseMap] Fix constness issues with lookup_or (#139247)

Also demonstrate its use in ScalarEvolution.
This commit is contained in:
Ramkumar Ramachandra
2025-05-28 20:32:31 +02:00
committed by GitHub
parent 4304d90f0a
commit 4bf67cdf02
3 changed files with 9 additions and 6 deletions

View File

@@ -220,8 +220,7 @@ public:
// 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, ValueT &&Default) const {
if (const BucketT *Bucket = doFind(Val))
return Bucket->getSecond();
return Default;

View File

@@ -15904,10 +15904,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) {

View File

@@ -671,6 +671,13 @@ TEST(DenseMapCustomTest, LookupOr) {
EXPECT_EQ(M.lookup_or(2, 4u), 4u);
}
TEST(DenseMapCustomTest, LookupOrConstness) {
DenseMap<int, unsigned *> M;
unsigned Default = 3u;
unsigned *Ret = M.lookup_or(0, &Default);
EXPECT_EQ(Ret, &Default);
}
// Key traits that allows lookup with either an unsigned or char* key;
// In the latter case, "a" == 0, "b" == 1 and so on.
struct TestDenseMapInfo {