xref: /llvm-project/lld/test/ELF/lto/devirt_split_unit_localize.ll (revision 0f46e31cfbf415fcd3d3ce121bef94e92c6ccfc8)
1; REQUIRES: x86
2;; Test that that a vtable defined locally in one module but external in another
3;; does not prevent devirtualization.
4
5;; Hybrid WPD
6; RUN: split-file %s %t
7; RUN: opt --thinlto-bc --thinlto-split-lto-unit -o %t/Cat.o %t/Cat.ll
8; RUN: opt --thinlto-bc --thinlto-split-lto-unit -o %t/User.o %t/User.ll
9; RUN: echo '{ global: _Z17useDoThingWithCatv; local: *; };' > %t/version.exp
10
11; RUN: ld.lld %t/Cat.o %t/User.o -shared -o %t/libA.so -save-temps --lto-whole-program-visibility \
12; RUN:   -mllvm -pass-remarks=. --version-script %t/version.exp 2>&1 | \
13; RUN:   FileCheck %s --check-prefix=REMARK
14
15; REMARK-DAG: <unknown>:0:0: single-impl: devirtualized a call to _ZNK3Cat9makeNoiseEv
16; REMARK-DAG: <unknown>:0:0: single-impl: devirtualized a call to _ZNK3Cat9makeNoiseEv
17
18target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
19target triple = "x86_64-unknown-linux-gnu"
20
21;--- Cat.ll
22target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
23target triple = "x86_64-unknown-linux-gnu"
24
25%struct.Cat = type { %struct.Animal }
26%struct.Animal = type { ptr }
27
28$_ZTS6Animal = comdat any
29
30$_ZTI6Animal = comdat any
31
32@.str = private unnamed_addr constant [5 x i8] c"Meow\00", align 1
33@_ZTV3Cat = dso_local unnamed_addr constant { [3 x ptr] } { [3 x ptr] [ptr null, ptr @_ZTI3Cat, ptr @_ZNK3Cat9makeNoiseEv] }, align 8, !type !0, !type !1, !type !2, !type !3
34@_ZTVN10__cxxabiv120__si_class_type_infoE = external dso_local global ptr
35@_ZTS3Cat = dso_local constant [5 x i8] c"3Cat\00", align 1
36@_ZTVN10__cxxabiv117__class_type_infoE = external dso_local global ptr
37@_ZTS6Animal = linkonce_odr dso_local constant [8 x i8] c"6Animal\00", comdat, align 1
38@_ZTI6Animal = linkonce_odr dso_local constant { ptr, ptr } { ptr getelementptr inbounds (ptr, ptr @_ZTVN10__cxxabiv117__class_type_infoE, i64 2), ptr @_ZTS6Animal }, comdat, align 8
39@_ZTI3Cat = dso_local constant { ptr, ptr, ptr } { ptr getelementptr inbounds (ptr, ptr @_ZTVN10__cxxabiv120__si_class_type_infoE, i64 2), ptr @_ZTS3Cat, ptr @_ZTI6Animal }, align 8
40
41define dso_local void @_ZNK3Cat9makeNoiseEv(ptr nocapture nonnull readnone dereferenceable(8) %this) unnamed_addr align 2 {
42entry:
43  %call = tail call i32 @puts(ptr nonnull dereferenceable(1) @.str)
44  ret void
45}
46
47declare dso_local noundef i32 @puts(ptr nocapture noundef readonly) local_unnamed_addr
48
49define dso_local void @_Z14doThingWithCatP6Animal(ptr %a) local_unnamed_addr {
50entry:
51  %tobool.not = icmp eq ptr %a, null
52  br i1 %tobool.not, label %if.end, label %if.then
53
54if.then:                                          ; preds = %entry
55  %vtable = load ptr, ptr %a, align 8, !tbaa !4
56  %0 = tail call i1 @llvm.type.test(ptr %vtable, metadata !"_ZTS3Cat")
57  tail call void @llvm.assume(i1 %0)
58  %1 = load ptr, ptr %vtable, align 8
59  tail call void %1(ptr nonnull dereferenceable(8) %a)
60  br label %if.end
61
62if.end:                                           ; preds = %if.then, %entry
63  ret void
64}
65
66declare i1 @llvm.type.test(ptr, metadata)
67
68declare void @llvm.assume(i1 noundef)
69
70!0 = !{i64 16, !"_ZTS3Cat"}
71!1 = !{i64 16, !"_ZTSM3CatKFvvE.virtual"}
72!2 = !{i64 16, !"_ZTS6Animal"}
73!3 = !{i64 16, !"_ZTSM6AnimalKFvvE.virtual"}
74!4 = !{!5, !5, i64 0}
75!5 = !{!"vtable pointer", !6, i64 0}
76!6 = !{!"Simple C++ TBAA"}
77
78;--- User.ll
79target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
80target triple = "x86_64-unknown-linux-gnu"
81
82%struct.Animal = type { ptr }
83%struct.Cat = type { %struct.Animal }
84
85@_ZTV3Cat = available_externally dso_local unnamed_addr constant { [3 x ptr] } { [3 x ptr] [ptr null, ptr @_ZTI3Cat, ptr @_ZNK3Cat9makeNoiseEv] }, align 8, !type !0, !type !1, !type !2, !type !3
86@_ZTI3Cat = external dso_local constant ptr
87@llvm.compiler.used = appending global [1 x ptr] [ptr @_ZTV3Cat], section "llvm.metadata"
88
89declare dso_local void @_ZNK3Cat9makeNoiseEv(ptr nonnull dereferenceable(8)) unnamed_addr
90
91define dso_local void @_Z17useDoThingWithCatv() local_unnamed_addr {
92entry:
93  %call = tail call noalias nonnull dereferenceable(8) ptr @_Znwm(i64 8)
94  store ptr getelementptr inbounds inrange(-16, 8) ({ [3 x ptr] }, ptr @_ZTV3Cat, i64 0, i32 0, i64 2), ptr %call, align 8, !tbaa !4
95  tail call void @_Z14doThingWithCatP6Animal(ptr nonnull %call)
96  ret void
97}
98
99declare dso_local nonnull ptr @_Znwm(i64) local_unnamed_addr
100
101declare dso_local void @_Z14doThingWithCatP6Animal(ptr) local_unnamed_addr
102
103!0 = !{i64 16, !"_ZTS3Cat"}
104!1 = !{i64 16, !"_ZTSM3CatKFvvE.virtual"}
105!2 = !{i64 16, !"_ZTS6Animal"}
106!3 = !{i64 16, !"_ZTSM6AnimalKFvvE.virtual"}
107!4 = !{!5, !5, i64 0}
108!5 = !{!"vtable pointer", !6, i64 0}
109!6 = !{!"Simple C++ TBAA"}
110