Skip to content

Commit 1c604a9

Browse files
committed
Revert "[llvm] Teach GlobalDCE about dso_local_equivalent"
This reverts commit 86dbcaf. Reverting since this depends on db28818 which broke our lto builders reported by fxbug.dev/12380.
1 parent 474f5ef commit 1c604a9

File tree

5 files changed

+14
-87
lines changed

5 files changed

+14
-87
lines changed

llvm/include/llvm/Analysis/TypeMetadataUtils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ Constant *getPointerAtOffset(Constant *I, uint64_t Offset, Module &M,
7878
Constant *TopLevelGlobal = nullptr);
7979

8080
/// Finds the same "relative pointer" pattern as described above, where the
81-
/// target is `C`, and replaces the entire pattern with a constant zero.
82-
void replaceRelativePointerUsersWithZero(Constant *C);
81+
/// target is `F`, and replaces the entire pattern with a constant zero.
82+
void replaceRelativePointerUsersWithZero(Function *F);
8383

8484
} // namespace llvm
8585

llvm/lib/Analysis/TypeMetadataUtils.cpp

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -199,26 +199,19 @@ Constant *llvm::getPointerAtOffset(Constant *I, uint64_t Offset, Module &M,
199199
return nullptr;
200200
}
201201

202-
static void replaceRelativePointerUserWithZero(User *U) {
203-
auto *PtrExpr = dyn_cast<ConstantExpr>(U);
204-
if (!PtrExpr || PtrExpr->getOpcode() != Instruction::PtrToInt)
205-
return;
206-
207-
for (auto *PtrToIntUser : PtrExpr->users()) {
208-
auto *SubExpr = dyn_cast<ConstantExpr>(PtrToIntUser);
209-
if (!SubExpr || SubExpr->getOpcode() != Instruction::Sub)
210-
return;
202+
void llvm::replaceRelativePointerUsersWithZero(Function *F) {
203+
for (auto *U : F->users()) {
204+
auto *PtrExpr = dyn_cast<ConstantExpr>(U);
205+
if (!PtrExpr || PtrExpr->getOpcode() != Instruction::PtrToInt)
206+
continue;
211207

212-
SubExpr->replaceNonMetadataUsesWith(
213-
ConstantInt::get(SubExpr->getType(), 0));
214-
}
215-
}
208+
for (auto *PtrToIntUser : PtrExpr->users()) {
209+
auto *SubExpr = dyn_cast<ConstantExpr>(PtrToIntUser);
210+
if (!SubExpr || SubExpr->getOpcode() != Instruction::Sub)
211+
continue;
216212

217-
void llvm::replaceRelativePointerUsersWithZero(Constant *C) {
218-
for (auto *U : C->users()) {
219-
if (auto *Equiv = dyn_cast<DSOLocalEquivalent>(U))
220-
replaceRelativePointerUsersWithZero(Equiv);
221-
else
222-
replaceRelativePointerUserWithZero(U);
213+
SubExpr->replaceNonMetadataUsesWith(
214+
ConstantInt::get(SubExpr->getType(), 0));
215+
}
223216
}
224217
}

llvm/test/Transforms/GlobalDCE/virtual-functions-relative-pointers-bad.ll

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,14 @@ declare { ptr, i1 } @llvm.type.checked.load(ptr, i32, metadata)
1616

1717
; CHECK: @vtable = internal unnamed_addr constant { [3 x i32] } zeroinitializer, align 8, !type !0, !type !1, !vcall_visibility !2
1818

19-
@vtable2 = internal unnamed_addr constant { [3 x i32] } { [3 x i32] [
20-
i32 trunc (i64 sub (i64 ptrtoint (ptr dso_local_equivalent @vfunc3 to i64), i64 ptrtoint (ptr @vtable2 to i64)) to i32),
21-
i32 trunc (i64 sub (i64 ptrtoint (ptr dso_local_equivalent @vfunc4 to i64), i64 ptrtoint (ptr @vtable2 to i64)) to i32),
22-
23-
; a "bad" relative pointer because it's base is not the @vtable symbol
24-
i32 trunc (i64 sub (i64 ptrtoint (ptr @weird_ref_3 to i64), i64 ptrtoint (ptr @weird_ref_4 to i64)) to i32)
25-
]}, align 4, !type !3, !type !4, !vcall_visibility !{i64 2}
26-
!3 = !{i64 0, !"vfunc3.type"}
27-
!4 = !{i64 4, !"vfunc4.type"}
28-
29-
; CHECK: @vtable2 = internal unnamed_addr constant { [3 x i32] } zeroinitializer, align 4, !type !3, !type !4, !vcall_visibility !2
30-
3119
define internal void @vfunc1() { ret void }
3220
define internal void @vfunc2() { ret void }
3321
define internal void @weird_ref_1() { ret void }
3422
define internal void @weird_ref_2() { ret void }
35-
declare void @vfunc3()
36-
declare void @vfunc4()
37-
declare void @weird_ref_3()
38-
declare void @weird_ref_4()
3923

4024
define void @main() {
4125
%1 = ptrtoint ptr @vtable to i64 ; to keep @vtable alive
4226
call void @weird_ref_2()
43-
%2 = ptrtoint ptr @vtable2 to i64 ; to keep @vtable2 alive
44-
call void @weird_ref_4()
4527
ret void
4628
}
4729

llvm/test/Transforms/GlobalDCE/virtual-functions-relative-pointers-gep.ll

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,6 @@ declare { ptr, i1 } @llvm.type.checked.load(ptr, i32, metadata)
1919
; CHECK-SAME: i32 0
2020
; CHECK-SAME: ] }, align 8, !type !0, !type !1, !vcall_visibility !2
2121

22-
@vtable2 = internal unnamed_addr constant { [4 x i32] } { [4 x i32] [
23-
i32 42,
24-
i32 1337,
25-
i32 trunc (i64 sub (i64 ptrtoint (ptr dso_local_equivalent @vfunc3_live_extern to i64), i64 ptrtoint (ptr getelementptr inbounds ({ [4 x i32] }, ptr @vtable2, i32 0, i32 0, i32 2) to i64)) to i32),
26-
i32 trunc (i64 sub (i64 ptrtoint (ptr dso_local_equivalent @vfunc4_dead_extern to i64), i64 ptrtoint (ptr getelementptr inbounds ({ [4 x i32] }, ptr @vtable2, i32 0, i32 0, i32 2) to i64)) to i32)
27-
]}, align 4, !type !3, !type !4, !vcall_visibility !{i64 2}
28-
!3 = !{i64 8, !"vfunc3.type"}
29-
!4 = !{i64 12, !"vfunc4.type"}
30-
31-
; CHECK: @vtable2 = internal unnamed_addr constant { [4 x i32] } { [4 x i32] [
32-
; CHECK-SAME: i32 trunc (i64 sub (i64 ptrtoint (ptr dso_local_equivalent @vfunc3_live_extern to i64), i64 ptrtoint (ptr getelementptr inbounds ({ [4 x i32] }, ptr @vtable2, i32 0, i32 0, i32 2) to i64)) to i32),
33-
; CHECK-SAME: i32 0
34-
; CHECK-SAME: ] }, align 4, !type !3, !type !4, !vcall_visibility !2
35-
3622
; (1) vfunc1_live is referenced from @main, stays alive
3723
define internal void @vfunc1_live() {
3824
; CHECK: define internal void @vfunc1_live(
@@ -45,19 +31,9 @@ define internal void @vfunc2_dead() {
4531
ret void
4632
}
4733

48-
; (3) vfunc3_live_extern is referenced from @main, stays alive
49-
; CHECK: declare void @vfunc3_live_extern
50-
declare void @vfunc3_live_extern()
51-
52-
; (4) vfunc4_dead_extern is never referenced, gets removed and vtable slot is null'd
53-
; CHECK-NOT: declare void @vfunc4_dead_extern
54-
declare void @vfunc4_dead_extern()
55-
5634
define void @main() {
5735
%1 = ptrtoint ptr @vtable to i64 ; to keep @vtable alive
5836
%2 = tail call { ptr, i1 } @llvm.type.checked.load(ptr null, i32 0, metadata !"vfunc1.type")
59-
%3 = ptrtoint ptr @vtable2 to i64 ; to keep @vtable2 alive
60-
%4 = tail call { ptr, i1 } @llvm.type.checked.load(ptr null, i32 0, metadata !"vfunc3.type")
6137
ret void
6238
}
6339

llvm/test/Transforms/GlobalDCE/virtual-functions-relative-pointers.ll

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,6 @@ declare { ptr, i1 } @llvm.type.checked.load(ptr, i32, metadata)
1717
; CHECK-SAME: i32 0
1818
; CHECK-SAME: ] }, align 8, !type !0, !type !1, !vcall_visibility !2
1919

20-
; Similar to above, but the vtable is more aligned to how C++ relative vtables look.
21-
; That is, the functions may not be dso-local.
22-
@vtable2 = internal unnamed_addr constant { [2 x i32] } { [2 x i32] [
23-
i32 trunc (i64 sub (i64 ptrtoint (ptr dso_local_equivalent @vfunc3_live_extern to i64), i64 ptrtoint (ptr @vtable2 to i64)) to i32),
24-
i32 trunc (i64 sub (i64 ptrtoint (ptr dso_local_equivalent @vfunc4_dead_extern to i64), i64 ptrtoint (ptr @vtable2 to i64)) to i32)
25-
]}, align 4, !type !3, !type !4, !vcall_visibility !{i64 2}
26-
!3 = !{i64 0, !"vfunc3.type"}
27-
!4 = !{i64 4, !"vfunc4.type"}
28-
29-
; CHECK: @vtable2 = internal unnamed_addr constant { [2 x i32] } { [2 x i32] [
30-
; CHECK-SAME: i32 trunc (i64 sub (i64 ptrtoint (ptr dso_local_equivalent @vfunc3_live_extern to i64), i64 ptrtoint (ptr @vtable2 to i64)) to i32),
31-
; CHECK-SAME: i32 0
32-
; CHECK-SAME: ] }, align 4, !type !3, !type !4, !vcall_visibility !2
33-
3420
; (1) vfunc1_live is referenced from @main, stays alive
3521
define internal void @vfunc1_live() {
3622
; CHECK: define internal void @vfunc1_live(
@@ -43,19 +29,9 @@ define internal void @vfunc2_dead() {
4329
ret void
4430
}
4531

46-
; (3) vfunc3_live_extern is referenced from @main, stays alive
47-
; CHECK: declare void @vfunc3_live_extern
48-
declare void @vfunc3_live_extern()
49-
50-
; (4) vfunc4_dead_extern is never referenced, gets removed and vtable slot is null'd
51-
; CHECK-NOT: declare void @vfunc4_dead_extern
52-
declare void @vfunc4_dead_extern()
53-
5432
define void @main() {
5533
%1 = ptrtoint ptr @vtable to i64 ; to keep @vtable alive
5634
%2 = tail call { ptr, i1 } @llvm.type.checked.load(ptr null, i32 0, metadata !"vfunc1.type")
57-
%3 = ptrtoint ptr @vtable2 to i64 ; to keep @vtable2 alive
58-
%4 = tail call { ptr, i1 } @llvm.type.checked.load(ptr null, i32 0, metadata !"vfunc3.type")
5935
ret void
6036
}
6137

0 commit comments

Comments
 (0)