Skip to content

Commit 23443f3

Browse files
committed
Switch back to map of pointer values
1 parent 4ee2cef commit 23443f3

File tree

2 files changed

+52
-17
lines changed

2 files changed

+52
-17
lines changed

llvm/include/llvm/Analysis/AliasSetTracker.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ class AliasSetTracker {
179179
BatchAAResults &AA;
180180
ilist<AliasSet> AliasSets;
181181

182-
using PointerMapType = DenseMap<MemoryLocation, AliasSet *>;
182+
using PointerMapType = DenseMap<AssertingVH<const Value>, AliasSet *>;
183183

184184
// Map from pointers to their node
185185
PointerMapType PointerMap;

llvm/lib/Analysis/AliasSetTracker.cpp

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -280,19 +280,38 @@ AliasSet *AliasSetTracker::findAliasSetForUnknownInst(Instruction *Inst) {
280280
}
281281

282282
AliasSet &AliasSetTracker::getAliasSetFor(const MemoryLocation &MemLoc) {
283-
// Check if this MemLoc is already registered
284-
AliasSet *&AS = PointerMap[MemLoc];
285-
if (AS) {
286-
if (AS->Forward) {
287-
// Update PointerMap entry to point to new target
288-
AliasSet *OldAS = AS;
289-
AS = OldAS->getForwardedTarget(*this);
290-
AS->addRef();
291-
OldAS->dropRef(*this);
283+
// The alias sets are indexed with a map from the memory locations' pointer
284+
// values. If the memory location is already registered, we can find it in the
285+
// alias set associated with its pointer.
286+
//
287+
// The PointerMap structure requires that all memory locations for the same
288+
// value will be in the same alias set, which does not hold for undef values.
289+
// So we keep UndefValue pointers associated with the nullptr in the pointer
290+
// map, and resort to a linear scan of all alias sets in that case.
291+
auto [It, Inserted] = PointerMap.insert({MemLoc.Ptr, nullptr});
292+
AliasSet *&MapEntry = It->second;
293+
if (!Inserted) {
294+
if (!isa<UndefValue>(MemLoc.Ptr)) {
295+
AliasSet *AS = MapEntry->getForwardedTarget(*this);
296+
if (llvm::find(AS->MemoryLocs, MemLoc) != AS->MemoryLocs.end()) {
297+
if (AS != MapEntry) {
298+
AS->addRef();
299+
MapEntry->dropRef(*this);
300+
MapEntry = AS;
301+
}
302+
return *AS;
303+
}
304+
} else {
305+
for (auto &AS : *this) {
306+
if (AS.isForwardingAliasSet())
307+
continue;
308+
if (llvm::find(AS.MemoryLocs, MemLoc) != AS.MemoryLocs.end())
309+
return AS;
310+
}
292311
}
293-
return *AS;
294312
}
295313

314+
AliasSet *AS;
296315
bool MustAliasAll = false;
297316
if (AliasAnyAS) {
298317
// At this point, the AST is saturated, so we only have one active alias
@@ -307,14 +326,30 @@ AliasSet &AliasSetTracker::getAliasSetFor(const MemoryLocation &MemLoc) {
307326
AS = AliasAS;
308327
} else {
309328
// Otherwise create a new alias set to hold the loaded pointer.
310-
AS = new AliasSet();
311-
AliasSets.push_back(AS);
329+
AliasSets.push_back(AS = new AliasSet());
312330
MustAliasAll = true;
313331
}
314-
// Register MemLoc in selected alias set
332+
333+
// Register MemLoc in selected alias set.
315334
AS->addPointer(*this, MemLoc, MustAliasAll);
316-
// PointerMap entry now points to the alias set
317-
AS->addRef();
335+
// Register selected alias set in pointer map (or ensure it is consistent with
336+
// earlier map entry after taking into account new merging).
337+
if (!isa<UndefValue>(MemLoc.Ptr)) {
338+
if (MapEntry) {
339+
if (MapEntry->Forward) {
340+
AliasSet *NewAS = MapEntry->getForwardedTarget(*this);
341+
NewAS->addRef();
342+
MapEntry->dropRef(*this);
343+
MapEntry = NewAS;
344+
}
345+
assert(MapEntry == AS &&
346+
"Memory locations with same pointer value cannot "
347+
"be in different alias sets");
348+
} else {
349+
AS->addRef();
350+
MapEntry = AS;
351+
}
352+
}
318353
return *AS;
319354
}
320355

@@ -557,7 +592,7 @@ void AliasSetTracker::print(raw_ostream &OS) const {
557592
OS << "Alias Set Tracker: " << AliasSets.size();
558593
if (AliasAnyAS)
559594
OS << " (Saturated)";
560-
OS << " alias sets for " << PointerMap.size() << " memory locations.\n";
595+
OS << " alias sets for " << PointerMap.size() << " pointer values.\n";
561596
for (const AliasSet &AS : *this)
562597
AS.print(OS);
563598
OS << "\n";

0 commit comments

Comments
 (0)