Skip to content

Commit 46aac94

Browse files
committed
[GVN] Remove users from ICF when RAUWing loads
When performing store to load forwarding, replacing users of the load may turn an indirect call into one with a known callee, in which case it might become willreturn, invalidating cached ICF information. Avoid this by removing users. This is a bit more aggressive than strictly necessary (e.g. this shouldn't be necessary when doing load-load CSE), but better safe than sorry. Fixes #48805.
1 parent 59e75b7 commit 46aac94

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

llvm/lib/Transforms/Scalar/GVN.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -1472,6 +1472,7 @@ void GVNPass::eliminatePartiallyRedundantLoad(
14721472
// Perform PHI construction.
14731473
Value *V = ConstructSSAForLoadSet(Load, ValuesPerBlock, *this);
14741474
// ConstructSSAForLoadSet is responsible for combining metadata.
1475+
ICF->replaceUsersOf(Load);
14751476
Load->replaceAllUsesWith(V);
14761477
if (isa<PHINode>(V))
14771478
V->takeName(Load);
@@ -1891,6 +1892,7 @@ bool GVNPass::processNonLocalLoad(LoadInst *Load) {
18911892
// Perform PHI construction.
18921893
Value *V = ConstructSSAForLoadSet(Load, ValuesPerBlock, *this);
18931894
// ConstructSSAForLoadSet is responsible for combining metadata.
1895+
ICF->removeUsersOf(Load);
18941896
Load->replaceAllUsesWith(V);
18951897

18961898
if (isa<PHINode>(V))
@@ -2165,6 +2167,7 @@ bool GVNPass::processLoad(LoadInst *L) {
21652167
Value *AvailableValue = AV->MaterializeAdjustedValue(L, L, *this);
21662168

21672169
// MaterializeAdjustedValue is responsible for combining metadata.
2170+
ICF->removeUsersOf(L);
21682171
L->replaceAllUsesWith(AvailableValue);
21692172
markInstructionForDeletion(L);
21702173
if (MSSAU)

llvm/test/Transforms/GVN/pr48805.ll

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 3
2+
; RUN: opt -S -passes=gvn < %s | FileCheck %s
3+
4+
declare void @willreturn() nounwind willreturn
5+
6+
declare void @capture(ptr)
7+
8+
; Make sure ICF is invalidated when the callee becomes known.
9+
10+
define i64 @test(ptr %p) {
11+
; CHECK-LABEL: define i64 @test(
12+
; CHECK-SAME: ptr [[P:%.*]]) {
13+
; CHECK-NEXT: entry:
14+
; CHECK-NEXT: [[A:%.*]] = alloca [2 x ptr], align 8
15+
; CHECK-NEXT: [[A2:%.*]] = getelementptr ptr, ptr [[A]], i64 1
16+
; CHECK-NEXT: call void @capture(ptr [[A]])
17+
; CHECK-NEXT: br i1 false, label [[IF:%.*]], label [[ENTRY_EXIT_CRIT_EDGE:%.*]]
18+
; CHECK: entry.exit_crit_edge:
19+
; CHECK-NEXT: [[RES_PRE:%.*]] = load i64, ptr [[A2]], align 8
20+
; CHECK-NEXT: br label [[EXIT:%.*]]
21+
; CHECK: if:
22+
; CHECK-NEXT: [[P1:%.*]] = load ptr, ptr [[A2]], align 8
23+
; CHECK-NEXT: br label [[EXIT]]
24+
; CHECK: exit:
25+
; CHECK-NEXT: [[RES:%.*]] = phi i64 [ [[RES_PRE]], [[ENTRY_EXIT_CRIT_EDGE]] ], [ undef, [[IF]] ]
26+
; CHECK-NEXT: store ptr @willreturn, ptr [[P]], align 8
27+
; CHECK-NEXT: tail call void @willreturn()
28+
; CHECK-NEXT: ret i64 [[RES]]
29+
;
30+
entry:
31+
%a = alloca [2 x ptr], align 8
32+
%a2 = getelementptr ptr, ptr %a, i64 1
33+
call void @capture(ptr %a)
34+
br i1 false, label %if, label %exit
35+
36+
if:
37+
%p0 = load ptr, ptr %a, align 8
38+
%p1 = load ptr, ptr %a2, align 8
39+
br label %exit
40+
41+
exit:
42+
store ptr @willreturn, ptr %p
43+
%p2 = load ptr, ptr %a, align 8
44+
%pgocount.i = load i64, ptr %p2, align 8
45+
%fn = load ptr, ptr %p
46+
tail call void %fn()
47+
%res = load i64, ptr %a2, align 8
48+
ret i64 %res
49+
}

0 commit comments

Comments
 (0)