Skip to content

Commit 8e8f059

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

File tree

3 files changed

+21
-24
lines changed

3 files changed

+21
-24
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: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9571,15 +9571,14 @@ getConstantEvolvingPHIOperands(Instruction *UseInst, const Loop *L,
95719571
if (!OpInst || !canConstantEvolve(OpInst, L)) return nullptr;
95729572

95739573
PHINode *P = dyn_cast<PHINode>(OpInst);
9574-
if (!P)
9574+
if (!P) {
95759575
// If this operand is already visited, reuse the prior result.
95769576
// We may have P != PHI if this is the deepest point at which the
95779577
// inconsistent paths meet.
9578-
P = PHIMap.lookup(OpInst);
9579-
if (!P) {
95809578
// Recurse and memoize the results, whether a phi is found or not.
95819579
// This recursive call invalidates pointers into PHIMap.
9582-
P = getConstantEvolvingPHIOperands(OpInst, L, PHIMap, Depth + 1);
9580+
P = PHIMap.lookup_or(
9581+
OpInst, getConstantEvolvingPHIOperands(OpInst, L, PHIMap, Depth + 1));
95839582
PHIMap[OpInst] = P;
95849583
}
95859584
if (!P)
@@ -15860,10 +15859,7 @@ const SCEV *ScalarEvolution::LoopGuards::rewrite(const SCEV *Expr) const {
1586015859
const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) { return Expr; }
1586115860

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

1586915865
const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
@@ -15891,25 +15887,19 @@ const SCEV *ScalarEvolution::LoopGuards::rewrite(const SCEV *Expr) const {
1589115887
}
1589215888

1589315889
const SCEV *visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
15894-
auto I = Map.find(Expr);
15895-
if (I == Map.end())
15896-
return SCEVRewriteVisitor<SCEVLoopGuardRewriter>::visitSignExtendExpr(
15897-
Expr);
15898-
return I->second;
15890+
return Map.lookup_or(
15891+
Expr,
15892+
SCEVRewriteVisitor<SCEVLoopGuardRewriter>::visitSignExtendExpr(Expr));
1589915893
}
1590015894

1590115895
const SCEV *visitUMinExpr(const SCEVUMinExpr *Expr) {
15902-
auto I = Map.find(Expr);
15903-
if (I == Map.end())
15904-
return SCEVRewriteVisitor<SCEVLoopGuardRewriter>::visitUMinExpr(Expr);
15905-
return I->second;
15896+
return Map.lookup_or(
15897+
Expr, SCEVRewriteVisitor<SCEVLoopGuardRewriter>::visitUMinExpr(Expr));
1590615898
}
1590715899

1590815900
const SCEV *visitSMinExpr(const SCEVSMinExpr *Expr) {
15909-
auto I = Map.find(Expr);
15910-
if (I == Map.end())
15911-
return SCEVRewriteVisitor<SCEVLoopGuardRewriter>::visitSMinExpr(Expr);
15912-
return I->second;
15901+
return Map.lookup_or(
15902+
Expr, SCEVRewriteVisitor<SCEVLoopGuardRewriter>::visitSMinExpr(Expr));
1591315903
}
1591415904

1591515905
const SCEV *visitAddExpr(const SCEVAddExpr *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)