Skip to content

Commit 6f68d03

Browse files
authored
[MemDepAnalysis] Don't reuse NonLocalPointerDeps cache if memory location size differs (#116936)
As seen in #111585, we can end up using a previous cache entry where the size was too large and was UB. Compile time impact: https://llvm-compile-time-tracker.com/compare.php?from=6a863f7e2679a60f2f38ae6a920d0b6e1a2c1690&to=faccf4e1f47fcd5360a438de2a56d02b770ad498&stat=instructions:u. Fixes #111585.
1 parent 7f19b1e commit 6f68d03

File tree

3 files changed

+27
-47
lines changed

3 files changed

+27
-47
lines changed

llvm/lib/Analysis/MemoryDependenceAnalysis.cpp

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,40 +1066,18 @@ bool MemoryDependenceResults::getNonLocalPointerDepFromBB(
10661066
// Invariant loads don't participate in caching. Thus no need to reconcile.
10671067
if (!isInvariantLoad && !Pair.second) {
10681068
if (CacheInfo->Size != Loc.Size) {
1069-
bool ThrowOutEverything;
1070-
if (CacheInfo->Size.hasValue() && Loc.Size.hasValue()) {
1071-
// FIXME: We may be able to do better in the face of results with mixed
1072-
// precision. We don't appear to get them in practice, though, so just
1073-
// be conservative.
1074-
ThrowOutEverything =
1075-
CacheInfo->Size.isPrecise() != Loc.Size.isPrecise() ||
1076-
!TypeSize::isKnownGE(CacheInfo->Size.getValue(),
1077-
Loc.Size.getValue());
1078-
} else {
1079-
// For our purposes, unknown size > all others.
1080-
ThrowOutEverything = !Loc.Size.hasValue();
1081-
}
1082-
1083-
if (ThrowOutEverything) {
1084-
// The query's Size is greater than the cached one. Throw out the
1085-
// cached data and proceed with the query at the greater size.
1086-
CacheInfo->Pair = BBSkipFirstBlockPair();
1087-
CacheInfo->Size = Loc.Size;
1088-
for (auto &Entry : CacheInfo->NonLocalDeps)
1089-
if (Instruction *Inst = Entry.getResult().getInst())
1090-
RemoveFromReverseMap(ReverseNonLocalPtrDeps, Inst, CacheKey);
1091-
CacheInfo->NonLocalDeps.clear();
1092-
// The cache is cleared (in the above line) so we will have lost
1093-
// information about blocks we have already visited. We therefore must
1094-
// assume that the cache information is incomplete.
1095-
IsIncomplete = true;
1096-
} else {
1097-
// This query's Size is less than the cached one. Conservatively restart
1098-
// the query using the greater size.
1099-
return getNonLocalPointerDepFromBB(
1100-
QueryInst, Pointer, Loc.getWithNewSize(CacheInfo->Size), isLoad,
1101-
StartBB, Result, Visited, SkipFirstBlock, IsIncomplete);
1102-
}
1069+
// The query's Size is not equal to the cached one. Throw out the cached
1070+
// data and proceed with the query with the new size.
1071+
CacheInfo->Pair = BBSkipFirstBlockPair();
1072+
CacheInfo->Size = Loc.Size;
1073+
for (auto &Entry : CacheInfo->NonLocalDeps)
1074+
if (Instruction *Inst = Entry.getResult().getInst())
1075+
RemoveFromReverseMap(ReverseNonLocalPtrDeps, Inst, CacheKey);
1076+
CacheInfo->NonLocalDeps.clear();
1077+
// The cache is cleared (in the above line) so we will have lost
1078+
// information about blocks we have already visited. We therefore must
1079+
// assume that the cache information is incomplete.
1080+
IsIncomplete = true;
11031081
}
11041082

11051083
// If the query's AATags are inconsistent with the cached one,

llvm/test/Analysis/MemoryDependenceAnalysis/load-size-cache.ll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ define i8 @f(i1 %arg0, i1 %arg1, i1 %arg2) {
2222
; CHECK-NEXT: call void @use(i64 undef)
2323
; CHECK-NEXT: br label %[[BB9:.*]]
2424
; CHECK: [[BB7]]:
25+
; CHECK-NEXT: [[LOAD8:%.*]] = load i8, ptr [[CALL]], align 4
2526
; CHECK-NEXT: br label %[[BB9]]
2627
; CHECK: [[BB9]]:
27-
; CHECK-NEXT: ret i8 4
28+
; CHECK-NEXT: [[PHI10:%.*]] = phi i8 [ [[LOAD8]], %[[BB7]] ], [ 4, %[[BB6]] ]
29+
; CHECK-NEXT: ret i8 [[PHI10]]
2830
;
2931
bb:
3032
br i1 %arg2, label %bb2, label %bb11

llvm/test/Transforms/GVN/PRE/rle.ll

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -673,15 +673,15 @@ define i8 @phi_trans4(ptr %p) {
673673
; CHECK-NEXT: [[X3:%.*]] = getelementptr i8, ptr [[P:%.*]], i32 192
674674
; CHECK-NEXT: store i8 -64, ptr [[X3]], align 1
675675
; CHECK-NEXT: [[X:%.*]] = getelementptr i8, ptr [[P]], i32 4
676-
; CHECK-NEXT: [[Y:%.*]] = load i8, ptr [[X]], align 1
676+
; CHECK-NEXT: [[Y2_PRE:%.*]] = load i8, ptr [[X]], align 1
677677
; CHECK-NEXT: br label [[LOOP:%.*]]
678678
; CHECK: loop:
679-
; CHECK-NEXT: [[Y2:%.*]] = phi i8 [ [[Y]], [[ENTRY:%.*]] ], [ 0, [[LOOP]] ]
679+
; CHECK-NEXT: [[Y2:%.*]] = phi i8 [ [[Y2_PRE]], [[ENTRY:%.*]] ], [ 0, [[LOOP]] ]
680680
; CHECK-NEXT: [[COND:%.*]] = call i1 @cond2()
681681
; CHECK-NEXT: store i32 0, ptr [[X3]], align 4
682682
; CHECK-NEXT: br i1 [[COND]], label [[LOOP]], label [[OUT:%.*]]
683683
; CHECK: out:
684-
; CHECK-NEXT: [[R:%.*]] = add i8 [[Y]], [[Y2]]
684+
; CHECK-NEXT: [[R:%.*]] = add i8 [[Y2_PRE]], [[Y2]]
685685
; CHECK-NEXT: ret i8 [[R]]
686686
;
687687
entry:
@@ -772,18 +772,18 @@ define i32 @phi_trans6(ptr noalias nocapture readonly %x, i1 %cond) {
772772
; CHECK-NEXT: call void @use_i32(i32 [[L0]])
773773
; CHECK-NEXT: br label [[HEADER:%.*]]
774774
; CHECK: header:
775-
; CHECK-NEXT: [[L1:%.*]] = phi i32 [ [[L0]], [[ENTRY:%.*]] ], [ [[L1_PRE:%.*]], [[LATCH_HEADER_CRIT_EDGE:%.*]] ]
775+
; CHECK-NEXT: [[L1_PRE:%.*]] = phi i32 [ [[L0]], [[ENTRY:%.*]] ], [ [[L1_PRE1:%.*]], [[LATCH_HEADER_CRIT_EDGE:%.*]] ]
776776
; CHECK-NEXT: [[IV:%.*]] = phi i32 [ 0, [[ENTRY]] ], [ [[IV_NEXT:%.*]], [[LATCH_HEADER_CRIT_EDGE]] ]
777777
; CHECK-NEXT: indirectbr ptr blockaddress(@phi_trans6, [[LATCH:%.*]]), [label %latch]
778778
; CHECK: latch:
779779
; CHECK-NEXT: [[IV_NEXT]] = add i32 [[IV]], 1
780780
; CHECK-NEXT: br i1 [[COND:%.*]], label [[EXIT:%.*]], label [[LATCH_HEADER_CRIT_EDGE]]
781781
; CHECK: latch.header_crit_edge:
782782
; CHECK-NEXT: [[GEP_1_PHI_TRANS_INSERT_PHI_TRANS_INSERT:%.*]] = getelementptr i32, ptr [[X]], i32 [[IV_NEXT]]
783-
; CHECK-NEXT: [[L1_PRE]] = load i32, ptr [[GEP_1_PHI_TRANS_INSERT_PHI_TRANS_INSERT]], align 4
783+
; CHECK-NEXT: [[L1_PRE1]] = load i32, ptr [[GEP_1_PHI_TRANS_INSERT_PHI_TRANS_INSERT]], align 4
784784
; CHECK-NEXT: br label [[HEADER]]
785785
; CHECK: exit:
786-
; CHECK-NEXT: ret i32 [[L1]]
786+
; CHECK-NEXT: ret i32 [[L1_PRE]]
787787
;
788788
entry:
789789
%l0 = load i32, ptr %x
@@ -1057,18 +1057,18 @@ define void @load_load_partial_alias_loop(ptr %P) {
10571057
; LE-NEXT: [[TMP0:%.*]] = trunc i32 [[V_1_32]] to i8
10581058
; LE-NEXT: br label [[LOOP:%.*]]
10591059
; LE: loop:
1060-
; LE-NEXT: [[V_I:%.*]] = phi i8 [ [[TMP0]], [[ENTRY:%.*]] ], [ [[V_I_PRE:%.*]], [[LOOP_LOOP_CRIT_EDGE:%.*]] ]
1060+
; LE-NEXT: [[V_I:%.*]] = phi i8 [ [[TMP0]], [[ENTRY:%.*]] ], [ [[TMP2:%.*]], [[LOOP_LOOP_CRIT_EDGE:%.*]] ]
10611061
; LE-NEXT: [[I:%.*]] = phi i64 [ 1, [[ENTRY]] ], [ [[I_INC:%.*]], [[LOOP_LOOP_CRIT_EDGE]] ]
10621062
; LE-NEXT: [[P_I:%.*]] = getelementptr i8, ptr [[P]], i64 [[I]]
10631063
; LE-NEXT: call void @use.i8(i8 [[V_I]])
10641064
; LE-NEXT: [[V_I_32:%.*]] = load i32, ptr [[P_I]], align 4
10651065
; LE-NEXT: call void @use.i32(i32 [[V_I_32]])
10661066
; LE-NEXT: [[I_INC]] = add i64 [[I]], 1
10671067
; LE-NEXT: [[CMP:%.*]] = icmp ne i64 [[I_INC]], 64
1068+
; LE-NEXT: [[TMP1:%.*]] = lshr i32 [[V_I_32]], 8
1069+
; LE-NEXT: [[TMP2]] = trunc i32 [[TMP1]] to i8
10681070
; LE-NEXT: br i1 [[CMP]], label [[LOOP_LOOP_CRIT_EDGE]], label [[EXIT:%.*]]
10691071
; LE: loop.loop_crit_edge:
1070-
; LE-NEXT: [[P_I_PHI_TRANS_INSERT:%.*]] = getelementptr i8, ptr [[P]], i64 [[I_INC]]
1071-
; LE-NEXT: [[V_I_PRE]] = load i8, ptr [[P_I_PHI_TRANS_INSERT]], align 1
10721072
; LE-NEXT: br label [[LOOP]]
10731073
; LE: exit:
10741074
; LE-NEXT: ret void
@@ -1084,18 +1084,18 @@ define void @load_load_partial_alias_loop(ptr %P) {
10841084
; BE-NEXT: [[TMP1:%.*]] = trunc i32 [[TMP0]] to i8
10851085
; BE-NEXT: br label [[LOOP:%.*]]
10861086
; BE: loop:
1087-
; BE-NEXT: [[V_I:%.*]] = phi i8 [ [[TMP1]], [[ENTRY:%.*]] ], [ [[V_I_PRE:%.*]], [[LOOP_LOOP_CRIT_EDGE:%.*]] ]
1087+
; BE-NEXT: [[V_I:%.*]] = phi i8 [ [[TMP1]], [[ENTRY:%.*]] ], [ [[TMP3:%.*]], [[LOOP_LOOP_CRIT_EDGE:%.*]] ]
10881088
; BE-NEXT: [[I:%.*]] = phi i64 [ 1, [[ENTRY]] ], [ [[I_INC:%.*]], [[LOOP_LOOP_CRIT_EDGE]] ]
10891089
; BE-NEXT: [[P_I:%.*]] = getelementptr i8, ptr [[P]], i64 [[I]]
10901090
; BE-NEXT: call void @use.i8(i8 [[V_I]])
10911091
; BE-NEXT: [[V_I_32:%.*]] = load i32, ptr [[P_I]], align 4
10921092
; BE-NEXT: call void @use.i32(i32 [[V_I_32]])
10931093
; BE-NEXT: [[I_INC]] = add i64 [[I]], 1
10941094
; BE-NEXT: [[CMP:%.*]] = icmp ne i64 [[I_INC]], 64
1095+
; BE-NEXT: [[TMP2:%.*]] = lshr i32 [[V_I_32]], 16
1096+
; BE-NEXT: [[TMP3]] = trunc i32 [[TMP2]] to i8
10951097
; BE-NEXT: br i1 [[CMP]], label [[LOOP_LOOP_CRIT_EDGE]], label [[EXIT:%.*]]
10961098
; BE: loop.loop_crit_edge:
1097-
; BE-NEXT: [[P_I_PHI_TRANS_INSERT:%.*]] = getelementptr i8, ptr [[P]], i64 [[I_INC]]
1098-
; BE-NEXT: [[V_I_PRE]] = load i8, ptr [[P_I_PHI_TRANS_INSERT]], align 1
10991099
; BE-NEXT: br label [[LOOP]]
11001100
; BE: exit:
11011101
; BE-NEXT: ret void

0 commit comments

Comments
 (0)