@@ -280,19 +280,38 @@ AliasSet *AliasSetTracker::findAliasSetForUnknownInst(Instruction *Inst) {
280
280
}
281
281
282
282
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
+ }
292
311
}
293
- return *AS;
294
312
}
295
313
314
+ AliasSet *AS;
296
315
bool MustAliasAll = false ;
297
316
if (AliasAnyAS) {
298
317
// At this point, the AST is saturated, so we only have one active alias
@@ -307,14 +326,30 @@ AliasSet &AliasSetTracker::getAliasSetFor(const MemoryLocation &MemLoc) {
307
326
AS = AliasAS;
308
327
} else {
309
328
// 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 ());
312
330
MustAliasAll = true ;
313
331
}
314
- // Register MemLoc in selected alias set
332
+
333
+ // Register MemLoc in selected alias set.
315
334
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
+ }
318
353
return *AS;
319
354
}
320
355
@@ -557,7 +592,7 @@ void AliasSetTracker::print(raw_ostream &OS) const {
557
592
OS << " Alias Set Tracker: " << AliasSets.size ();
558
593
if (AliasAnyAS)
559
594
OS << " (Saturated)" ;
560
- OS << " alias sets for " << PointerMap.size () << " memory locations .\n " ;
595
+ OS << " alias sets for " << PointerMap.size () << " pointer values .\n " ;
561
596
for (const AliasSet &AS : *this )
562
597
AS.print (OS);
563
598
OS << " \n " ;
0 commit comments