aboutsummaryrefslogtreecommitdiff
path: root/llvm
diff options
context:
space:
mode:
authorRamkumar Ramachandra <ramkumar.ramachandra@codasip.com>2025-05-28 20:32:31 +0200
committerGitHub <noreply@github.com>2025-05-28 20:32:31 +0200
commit4bf67cdf026478c4bc3e708153c02f82e2c22524 (patch)
treeee4c92111476fff3565d7efb9bf1834c54922030 /llvm
parent4304d90f0ac171f86c26d7d2da4fd03b336957bb (diff)
downloadllvm-4bf67cdf026478c4bc3e708153c02f82e2c22524.zip
llvm-4bf67cdf026478c4bc3e708153c02f82e2c22524.tar.gz
llvm-4bf67cdf026478c4bc3e708153c02f82e2c22524.tar.bz2
[DenseMap] Fix constness issues with lookup_or (#139247)
Also demonstrate its use in ScalarEvolution.
Diffstat (limited to 'llvm')
-rw-r--r--llvm/include/llvm/ADT/DenseMap.h3
-rw-r--r--llvm/lib/Analysis/ScalarEvolution.cpp5
-rw-r--r--llvm/unittests/ADT/DenseMapTest.cpp7
3 files changed, 9 insertions, 6 deletions
diff --git a/llvm/include/llvm/ADT/DenseMap.h b/llvm/include/llvm/ADT/DenseMap.h
index 4df50e0..de41a21 100644
--- a/llvm/include/llvm/ADT/DenseMap.h
+++ b/llvm/include/llvm/ADT/DenseMap.h
@@ -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;
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 4bd5a4c..56cdfab 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -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) {
diff --git a/llvm/unittests/ADT/DenseMapTest.cpp b/llvm/unittests/ADT/DenseMapTest.cpp
index d002a3c..c95f96c 100644
--- a/llvm/unittests/ADT/DenseMapTest.cpp
+++ b/llvm/unittests/ADT/DenseMapTest.cpp
@@ -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 {