Skip to content

Commit e562837

Browse files
committed
Merge branch 'ast-pointer-map' into ast-dont-merge-memory-locations
2 parents 5f82129 + 9e9ebcd commit e562837

File tree

8 files changed

+192
-167
lines changed

8 files changed

+192
-167
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: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -262,19 +262,23 @@ AliasSet *AliasSetTracker::findAliasSetForUnknownInst(Instruction *Inst) {
262262
}
263263

264264
AliasSet &AliasSetTracker::getAliasSetFor(const MemoryLocation &MemLoc) {
265-
// Check if this MemLoc is already registered
266-
AliasSet *&AS = PointerMap[MemLoc];
267-
if (AS) {
268-
if (AS->Forward) {
269-
// Update PointerMap entry to point to new target
270-
AliasSet *OldAS = AS;
271-
AS = OldAS->getForwardedTarget(*this);
272-
AS->addRef();
273-
OldAS->dropRef(*this);
265+
// The alias sets are indexed with a map from the memory locations' pointer
266+
// values. If the memory location is already registered, we can find it in the
267+
// alias set associated with its pointer.
268+
AliasSet *&MapEntry = PointerMap[MemLoc.Ptr];
269+
if (MapEntry) {
270+
AliasSet *AS = MapEntry->getForwardedTarget(*this);
271+
if (llvm::is_contained(AS->MemoryLocs, MemLoc)) {
272+
if (AS != MapEntry) {
273+
AS->addRef();
274+
MapEntry->dropRef(*this);
275+
MapEntry = AS;
276+
}
277+
return *AS;
274278
}
275-
return *AS;
276279
}
277280

281+
AliasSet *AS;
278282
bool MustAliasAll = false;
279283
if (AliasAnyAS) {
280284
// At this point, the AST is saturated, so we only have one active alias
@@ -287,16 +291,37 @@ AliasSet &AliasSetTracker::getAliasSetFor(const MemoryLocation &MemLoc) {
287291
mergeAliasSetsForPointer(MemLoc, MustAliasAll)) {
288292
// Add it to the alias set it aliases.
289293
AS = AliasAS;
294+
} else if (MapEntry) {
295+
// Although we have an independent memory location, forgo creating a new
296+
// alias set to retain the implementation invariant that all memory
297+
// locations with the same pointer value are in the same alias set.
298+
// (This is only known to occur for undef pointer values, which AA treats as
299+
// noalias.)
300+
AS = MapEntry->getForwardedTarget(*this);
301+
AS->Alias = AliasSet::SetMayAlias;
290302
} else {
291303
// Otherwise create a new alias set to hold the loaded pointer.
292-
AS = new AliasSet();
293-
AliasSets.push_back(AS);
304+
AliasSets.push_back(AS = new AliasSet());
294305
MustAliasAll = true;
295306
}
296-
// Register MemLoc in selected alias set
307+
308+
// Register MemLoc in selected alias set.
297309
AS->addPointer(*this, MemLoc, MustAliasAll);
298-
// PointerMap entry now points to the alias set
299-
AS->addRef();
310+
// Register selected alias set in pointer map (or ensure it is consistent with
311+
// earlier map entry after taking into account new merging).
312+
if (MapEntry) {
313+
if (MapEntry->Forward) {
314+
AliasSet *NewAS = MapEntry->getForwardedTarget(*this);
315+
NewAS->addRef();
316+
MapEntry->dropRef(*this);
317+
MapEntry = NewAS;
318+
}
319+
assert(MapEntry == AS && "Memory locations with same pointer value cannot "
320+
"be in different alias sets");
321+
} else {
322+
AS->addRef();
323+
MapEntry = AS;
324+
}
300325
return *AS;
301326
}
302327

@@ -539,7 +564,7 @@ void AliasSetTracker::print(raw_ostream &OS) const {
539564
OS << "Alias Set Tracker: " << AliasSets.size();
540565
if (AliasAnyAS)
541566
OS << " (Saturated)";
542-
OS << " alias sets for " << PointerMap.size() << " memory locations.\n";
567+
OS << " alias sets for " << PointerMap.size() << " pointer values.\n";
543568
for (const AliasSet &AS : *this)
544569
AS.print(OS);
545570
OS << "\n";

llvm/test/Analysis/AliasSet/argmemonly.ll

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
@d = global i8 2, align 1
55

66
; CHECK: Alias sets for function 'test_alloca_argmemonly':
7-
; CHECK-NEXT: Alias Set Tracker: 2 alias sets for 3 memory locations.
7+
; CHECK-NEXT: Alias Set Tracker: 2 alias sets for 3 pointer values.
88
; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] must alias, Mod Pointers: (ptr %a, LocationSize::precise(1))
99
; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 2] may alias, Mod/Ref Pointers: (ptr %d, unknown before-or-after), (ptr %s, unknown before-or-after)
1010
define void @test_alloca_argmemonly(ptr %s, ptr %d) {
@@ -16,9 +16,9 @@ entry:
1616
}
1717

1818
; CHECK: Alias sets for function 'test_readonly_arg'
19-
; CHECK-NEXT: Alias Set Tracker: 2 alias sets for 3 memory locations.
19+
; CHECK-NEXT: Alias Set Tracker: 2 alias sets for 2 pointer values.
2020
; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] must alias, Mod Pointers: (ptr %d, unknown before-or-after)
21-
; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 2] must alias, Ref Pointers: (ptr %s, unknown before-or-after), (ptr %s, LocationSize::precise(1))
21+
; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] must alias, Ref Pointers: (ptr %s, unknown before-or-after), (ptr %s, LocationSize::precise(1))
2222
define i8 @test_readonly_arg(ptr noalias %s, ptr noalias %d) {
2323
entry:
2424
call void @my_memcpy(ptr %d, ptr %s, i64 1)
@@ -27,7 +27,7 @@ entry:
2727
}
2828

2929
; CHECK: Alias sets for function 'test_noalias_argmemonly':
30-
; CHECK-NEXT: Alias Set Tracker: 2 alias sets for 3 memory locations.
30+
; CHECK-NEXT: Alias Set Tracker: 2 alias sets for 3 pointer values.
3131
; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] must alias, Mod Pointers: (ptr %a, LocationSize::precise(1))
3232
; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 2] may alias, Mod/Ref Pointers: (ptr %d, unknown before-or-after), (ptr %s, unknown before-or-after)
3333
define void @test_noalias_argmemonly(ptr noalias %a, ptr %s, ptr %d) {
@@ -38,9 +38,9 @@ entry:
3838
}
3939

4040
; CHECK: Alias sets for function 'test5':
41-
; CHECK-NEXT: Alias Set Tracker: 2 alias sets for 4 memory locations.
42-
; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 2] must alias, Mod/Ref Pointers: (ptr %a, LocationSize::precise(1)), (ptr %a, unknown before-or-after)
43-
; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 2] must alias, Mod Pointers: (ptr %b, unknown before-or-after), (ptr %b, LocationSize::precise(1))
41+
; CHECK-NEXT: Alias Set Tracker: 2 alias sets for 2 pointer values.
42+
; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] must alias, Mod/Ref Pointers: (ptr %a, LocationSize::precise(1)), (ptr %a, unknown before-or-after)
43+
; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] must alias, Mod Pointers: (ptr %b, unknown before-or-after), (ptr %b, LocationSize::precise(1))
4444
define void @test5(ptr noalias %a, ptr noalias %b) {
4545
entry:
4646
store i8 1, ptr %a, align 1
@@ -50,9 +50,9 @@ entry:
5050
}
5151

5252
; CHECK: Alias sets for function 'test_argcollapse':
53-
; CHECK-NEXT: Alias Set Tracker: 2 alias sets for 4 memory locations.
54-
; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 2] must alias, Mod/Ref Pointers: (ptr %a, LocationSize::precise(1)), (ptr %a, unknown before-or-after)
55-
; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 2] must alias, Mod/Ref Pointers: (ptr %b, unknown before-or-after), (ptr %b, LocationSize::precise(1))
53+
; CHECK-NEXT: Alias Set Tracker: 2 alias sets for 2 pointer values.
54+
; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] must alias, Mod/Ref Pointers: (ptr %a, LocationSize::precise(1)), (ptr %a, unknown before-or-after)
55+
; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] must alias, Mod/Ref Pointers: (ptr %b, unknown before-or-after), (ptr %b, LocationSize::precise(1))
5656
define void @test_argcollapse(ptr noalias %a, ptr noalias %b) {
5757
entry:
5858
store i8 1, ptr %a, align 1
@@ -62,7 +62,7 @@ entry:
6262
}
6363

6464
; CHECK: Alias sets for function 'test_memcpy1':
65-
; CHECK-NEXT: Alias Set Tracker: 2 alias sets for 2 memory locations.
65+
; CHECK-NEXT: Alias Set Tracker: 2 alias sets for 2 pointer values.
6666
; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] must alias, Mod/Ref Pointers: (ptr %b, unknown before-or-after)
6767
; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] must alias, Mod/Ref Pointers: (ptr %a, unknown before-or-after)
6868
define void @test_memcpy1(ptr noalias %a, ptr noalias %b) {
@@ -73,7 +73,7 @@ entry:
7373
}
7474

7575
; CHECK: Alias sets for function 'test_memset1':
76-
; CHECK-NEXT: Alias Set Tracker: 1 alias sets for 1 memory locations.
76+
; CHECK-NEXT: Alias Set Tracker: 1 alias sets for 1 pointer values.
7777
; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] must alias, Mod Pointers: (ptr %a, unknown before-or-after)
7878
define void @test_memset1() {
7979
entry:
@@ -83,7 +83,7 @@ entry:
8383
}
8484

8585
; CHECK: Alias sets for function 'test_memset2':
86-
; CHECK-NEXT: Alias Set Tracker: 1 alias sets for 1 memory locations.
86+
; CHECK-NEXT: Alias Set Tracker: 1 alias sets for 1 pointer values.
8787
; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] must alias, Mod Pointers: (ptr %a, unknown before-or-after)
8888
define void @test_memset2(ptr %a) {
8989
entry:
@@ -92,7 +92,7 @@ entry:
9292
}
9393

9494
; CHECK: Alias sets for function 'test_memset3':
95-
; CHECK-NEXT: Alias Set Tracker: 1 alias sets for 2 memory locations.
95+
; CHECK-NEXT: Alias Set Tracker: 1 alias sets for 2 pointer values.
9696
; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 2] may alias, Mod Pointers: (ptr %a, unknown before-or-after), (ptr %b, unknown before-or-after)
9797
define void @test_memset3(ptr %a, ptr %b) {
9898
entry:
@@ -104,7 +104,7 @@ entry:
104104
;; PICKUP HERE
105105

106106
; CHECK: Alias sets for function 'test_memset4':
107-
; CHECK-NEXT: Alias Set Tracker: 2 alias sets for 2 memory locations.
107+
; CHECK-NEXT: Alias Set Tracker: 2 alias sets for 2 pointer values.
108108
; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] must alias, Mod Pointers: (ptr %a, unknown before-or-after)
109109
; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] must alias, Mod Pointers: (ptr %b, unknown before-or-after)
110110
define void @test_memset4(ptr noalias %a, ptr noalias %b) {
@@ -120,7 +120,7 @@ declare void @my_memmove(ptr nocapture, ptr nocapture readonly, i64) argmemonly
120120

121121

122122
; CHECK: Alias sets for function 'test_attribute_intersect':
123-
; CHECK-NEXT: Alias Set Tracker: 1 alias sets for 1 memory locations.
123+
; CHECK-NEXT: Alias Set Tracker: 1 alias sets for 1 pointer values.
124124
; CHECK-NEXT: AliasSet[0x{{[0-9a-f]+}}, 1] must alias, Ref Pointers: (ptr %a, LocationSize::precise(1))
125125
define i8 @test_attribute_intersect(ptr noalias %a) {
126126
entry:

0 commit comments

Comments
 (0)