Skip to content

Commit 56cc697

Browse files
committed
[clang][dataflow] Merge distinct pointer values in Environment::join
This is part of the implementation of the dataflow analysis framework. See "[RFC] A dataflow analysis framework for Clang AST" on cfe-dev. Reviewed-by: ymandel, xazax.hun Differential Revision: https://reviews.llvm.org/D118480
1 parent d4d0ae6 commit 56cc697

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,12 @@ LatticeJoinEffect Environment::join(const Environment &Other,
8989
if (ExprToLocSizeBefore != ExprToLoc.size())
9090
Effect = LatticeJoinEffect::Changed;
9191

92-
llvm::DenseMap<const StorageLocation *, Value *> MergedLocToVal;
93-
for (auto &Entry : LocToVal) {
92+
// Move `LocToVal` so that `Environment::Merger::merge` can safely assign
93+
// values to storage locations while this code iterates over the current
94+
// assignments.
95+
llvm::DenseMap<const StorageLocation *, Value *> OldLocToVal =
96+
std::move(LocToVal);
97+
for (auto &Entry : OldLocToVal) {
9498
const StorageLocation *Loc = Entry.first;
9599
assert(Loc != nullptr);
96100

@@ -103,19 +107,25 @@ LatticeJoinEffect Environment::join(const Environment &Other,
103107
assert(It->second != nullptr);
104108

105109
if (It->second == Val) {
106-
MergedLocToVal.insert({Loc, Val});
110+
LocToVal.insert({Loc, Val});
107111
continue;
108112
}
109113

114+
if (auto *FirstVal = dyn_cast<PointerValue>(Val)) {
115+
auto *SecondVal = cast<PointerValue>(It->second);
116+
if (&FirstVal->getPointeeLoc() == &SecondVal->getPointeeLoc()) {
117+
LocToVal.insert({Loc, FirstVal});
118+
continue;
119+
}
120+
}
121+
110122
// FIXME: Consider destroying `MergedValue` immediately if `Merger::merge`
111123
// returns false to avoid storing unneeded values in `DACtx`.
112124
if (Value *MergedVal = createValue(Loc->getType()))
113125
if (Merger.merge(Loc->getType(), *Val, *It->second, *MergedVal, *this))
114-
MergedLocToVal.insert({Loc, MergedVal});
126+
LocToVal.insert({Loc, MergedVal});
115127
}
116-
const unsigned LocToValSizeBefore = LocToVal.size();
117-
LocToVal = std::move(MergedLocToVal);
118-
if (LocToValSizeBefore != LocToVal.size())
128+
if (OldLocToVal.size() != LocToVal.size())
119129
Effect = LatticeJoinEffect::Changed;
120130

121131
return Effect;

clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,4 +494,37 @@ TEST_F(WideningTest, JoinDistinctValuesWithSameProperties) {
494494
});
495495
}
496496

497+
TEST_F(WideningTest, DistinctPointersToTheSameLocation) {
498+
std::string Code = R"(
499+
void target(int Foo, bool Cond) {
500+
int *Bar = &Foo;
501+
while (Cond) {
502+
Bar = &Foo;
503+
}
504+
(void)0;
505+
// [[p]]
506+
}
507+
)";
508+
runDataflow(Code,
509+
[](llvm::ArrayRef<
510+
std::pair<std::string, DataflowAnalysisState<NoopLattice>>>
511+
Results,
512+
ASTContext &ASTCtx) {
513+
ASSERT_THAT(Results, ElementsAre(Pair("p", _)));
514+
const Environment &Env = Results[0].second.Env;
515+
516+
const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo");
517+
ASSERT_THAT(FooDecl, NotNull());
518+
519+
const ValueDecl *BarDecl = findValueDecl(ASTCtx, "Bar");
520+
ASSERT_THAT(BarDecl, NotNull());
521+
522+
const auto *FooLoc = cast<ScalarStorageLocation>(
523+
Env.getStorageLocation(*FooDecl, SkipPast::None));
524+
const auto *BarVal =
525+
cast<PointerValue>(Env.getValue(*BarDecl, SkipPast::None));
526+
EXPECT_EQ(&BarVal->getPointeeLoc(), FooLoc);
527+
});
528+
}
529+
497530
} // namespace

0 commit comments

Comments
 (0)