1; REQUIRES: x86-registered-target
2
3; Test devirtualization requiring promotion of local targets.
4
5; Generate split module with summary for hybrid Thin/Regular LTO WPD.
6; RUN: opt -thinlto-bc -thinlto-split-lto-unit -o %t1.o %s
7; RUN: opt -thinlto-bc -thinlto-split-lto-unit -o %t2.o %p/Inputs/devirt2.ll
8
9; Check that we have module flag showing splitting enabled, and that we don't
10; generate summary information needed for index-based WPD.
11; RUN: llvm-modextract -b -n=0 %t2.o -o %t2.o.0
12; RUN: llvm-dis -o - %t2.o.0 | FileCheck %s --check-prefix=ENABLESPLITFLAG --implicit-check-not=vTableFuncs --implicit-check-not=typeidCompatibleVTable
13; RUN: llvm-modextract -b -n=1 %t2.o -o %t2.o.1
14; RUN: llvm-dis -o - %t2.o.1 | FileCheck %s --check-prefix=ENABLESPLITFLAG --implicit-check-not=vTableFuncs --implicit-check-not=typeidCompatibleVTable
15; ENABLESPLITFLAG: !{i32 1, !"EnableSplitLTOUnit", i32 1}
16
17; Generate unsplit module with summary for ThinLTO index-based WPD.
18; Force generation of the bitcode index so that we also test lazy metadata
19; loader handling of the type metadata.
20; RUN: opt -bitcode-mdindex-threshold=0 -thinlto-bc -o %t3.o %s
21; RUN: opt -bitcode-mdindex-threshold=0 -thinlto-bc -o %t4.o %p/Inputs/devirt2.ll
22
23; Check that we don't have module flag when splitting not enabled for ThinLTO,
24; and that we generate summary information needed for index-based WPD.
25; RUN: llvm-dis -o - %t4.o | FileCheck %s --check-prefix=NOENABLESPLITFLAG
26; NOENABLESPLITFLAG-DAG: !{i32 1, !"EnableSplitLTOUnit", i32 0}
27; NOENABLESPLITFLAG-DAG: [[An:\^[0-9]+]] = gv: (name: "_ZN1A1nEi"
28; NOENABLESPLITFLAG-DAG: [[Bf:\^[0-9]+]] = gv: (name: "_ZN1B1fEi"
29; NOENABLESPLITFLAG-DAG: [[Cf:\^[0-9]+]] = gv: (name: "_ZN1C1fEi"
30; NOENABLESPLITFLAG-DAG: [[Dm:\^[0-9]+]] = gv: (name: "_ZN1D1mEi"
31; NOENABLESPLITFLAG-DAG: [[B:\^[0-9]+]] = gv: (name: "_ZTV1B", {{.*}} vTableFuncs: ((virtFunc: [[Bf]], offset: 16), (virtFunc: [[An]], offset: 24)), refs: ([[Bf]], [[An]])
32; NOENABLESPLITFLAG-DAG: [[C:\^[0-9]+]] = gv: (name: "_ZTV1C", {{.*}} vTableFuncs: ((virtFunc: [[Cf]], offset: 16), (virtFunc: [[An]], offset: 24)), refs: ([[An]], [[Cf]])
33; NOENABLESPLITFLAG-DAG: [[D:\^[0-9]+]] = gv: (name: "_ZTV1D", {{.*}} vTableFuncs: ((virtFunc: [[Dm]], offset: 16)), refs: ([[Dm]])
34; NOENABLESPLITFLAG-DAG: typeidCompatibleVTable: (name: "_ZTS1A", summary: ((offset: 16, [[B]]), (offset: 16, [[C]])))
35; NOENABLESPLITFLAG-DAG: typeidCompatibleVTable: (name: "_ZTS1B", summary: ((offset: 16, [[B]])))
36; NOENABLESPLITFLAG-DAG: typeidCompatibleVTable: (name: "_ZTS1C", summary: ((offset: 16, [[C]])))
37; NOENABLESPLITFLAG-DAG: typeidCompatibleVTable: (name: "_ZTS1D", summary: ((offset: 16, [[D]])))
38
39; Index based WPD
40; RUN: llvm-lto2 run %t3.o %t4.o -save-temps -pass-remarks=. \
41; RUN:   -whole-program-visibility \
42; RUN:   -wholeprogramdevirt-print-index-based \
43; RUN:   -o %t5 \
44; RUN:   -r=%t3.o,test,px \
45; RUN:   -r=%t3.o,_ZTV1B, \
46; RUN:   -r=%t3.o,_ZTV1C, \
47; RUN:   -r=%t3.o,_ZTV1D, \
48; RUN:   -r=%t3.o,_ZN1D1mEi, \
49; RUN:   -r=%t3.o,test2, \
50; RUN:   -r=%t4.o,_ZN1B1fEi,p \
51; RUN:   -r=%t4.o,_ZN1C1fEi,p \
52; RUN:   -r=%t4.o,_ZN1D1mEi,p \
53; RUN:   -r=%t4.o,test2,px \
54; RUN:   -r=%t4.o,_ZTV1B,px \
55; RUN:   -r=%t4.o,_ZTV1C,px \
56; RUN:   -r=%t4.o,_ZTV1D,px \
57; RUN:   -r=%t4.o,_ZTV1E,px 2>&1 | FileCheck %s --check-prefix=REMARK --check-prefix=PRINT
58; RUN: llvm-dis %t5.1.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-IR1
59; RUN: llvm-dis %t5.2.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-IR2
60; RUN: llvm-nm %t5.1 | FileCheck %s --check-prefix=NM-INDEX1
61; RUN: llvm-nm %t5.2 | FileCheck %s --check-prefix=NM-INDEX2
62
63; NM-INDEX1-DAG: U _ZN1A1nEi.llvm.
64; NM-INDEX1-DAG: U _ZN1E1mEi.llvm.
65; NM-INDEX1-DAG: U _ZN1D1mEi
66
67; NM-INDEX2-DAG: T _ZN1A1nEi.llvm.
68; NM-INDEX2-DAG: T _ZN1E1mEi.llvm.
69; NM-INDEX2-DAG: W _ZN1D1mEi
70; NM-INDEX2-DAG: t _ZN1B1fEi
71; NM-INDEX2-DAG: t _ZN1C1fEi
72
73; Index based WPD, distributed backends
74; RUN: llvm-lto2 run %t3.o %t4.o -save-temps \
75; RUN:   -whole-program-visibility \
76; RUN:   -thinlto-distributed-indexes -wholeprogramdevirt-print-index-based \
77; RUN:   -o %t5 \
78; RUN:   -r=%t3.o,test,px \
79; RUN:   -r=%t3.o,_ZTV1B, \
80; RUN:   -r=%t3.o,_ZTV1C, \
81; RUN:   -r=%t3.o,_ZTV1D, \
82; RUN:   -r=%t3.o,_ZN1D1mEi, \
83; RUN:   -r=%t3.o,test2, \
84; RUN:   -r=%t4.o,_ZN1B1fEi,p \
85; RUN:   -r=%t4.o,_ZN1C1fEi,p \
86; RUN:   -r=%t4.o,_ZN1D1mEi,p \
87; RUN:   -r=%t4.o,test2,px \
88; RUN:   -r=%t4.o,_ZTV1B,px \
89; RUN:   -r=%t4.o,_ZTV1C,px \
90; RUN:   -r=%t4.o,_ZTV1D,px \
91; RUN:   -r=%t4.o,_ZTV1E,px 2>&1 | FileCheck %s --check-prefix=PRINT
92
93; PRINT-DAG: Devirtualized call to {{.*}} (_ZN1A1nEi)
94; PRINT-DAG: Devirtualized call to {{.*}} (_ZN1E1mEi)
95; PRINT-DAG: Devirtualized call to {{.*}} (_ZN1D1mEi)
96
97; New PM
98; RUN: llvm-lto2 run %t1.o %t2.o -save-temps -pass-remarks=. \
99; RUN:   -whole-program-visibility \
100; RUN:   -o %t5 \
101; RUN:   -r=%t1.o,test,px \
102; RUN:   -r=%t1.o,_ZTV1B, \
103; RUN:   -r=%t1.o,_ZTV1C, \
104; RUN:   -r=%t1.o,_ZTV1D, \
105; RUN:   -r=%t1.o,_ZTV1D, \
106; RUN:   -r=%t1.o,_ZN1D1mEi, \
107; RUN:   -r=%t1.o,_ZN1D1mEi, \
108; RUN:   -r=%t1.o,test2, \
109; RUN:   -r=%t2.o,_ZN1A1nEi,p \
110; RUN:   -r=%t2.o,_ZN1B1fEi,p \
111; RUN:   -r=%t2.o,_ZN1C1fEi,p \
112; RUN:   -r=%t2.o,_ZN1D1mEi,p \
113; RUN:   -r=%t2.o,_ZN1E1mEi,p \
114; RUN:   -r=%t2.o,_ZTV1B, \
115; RUN:   -r=%t2.o,_ZTV1C, \
116; RUN:   -r=%t2.o,_ZTV1D, \
117; RUN:   -r=%t2.o,_ZTV1E, \
118; RUN:   -r=%t2.o,test2,px \
119; RUN:   -r=%t2.o,_ZN1A1nEi, \
120; RUN:   -r=%t2.o,_ZN1B1fEi, \
121; RUN:   -r=%t2.o,_ZN1C1fEi, \
122; RUN:   -r=%t2.o,_ZN1D1mEi, \
123; RUN:   -r=%t2.o,_ZN1E1mEi, \
124; RUN:   -r=%t2.o,_ZTV1B,px \
125; RUN:   -r=%t2.o,_ZTV1C,px \
126; RUN:   -r=%t2.o,_ZTV1D,px \
127; RUN:   -r=%t2.o,_ZTV1E,px 2>&1 | FileCheck %s --check-prefix=REMARK
128; RUN: llvm-dis %t5.1.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-IR1
129; RUN: llvm-dis %t5.2.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-IR2
130; RUN: llvm-nm %t5.1 | FileCheck %s --check-prefix=NM-HYBRID1
131; RUN: llvm-nm %t5.2 | FileCheck %s --check-prefix=NM-HYBRID2
132
133; NM-HYBRID1-DAG: U _ZN1A1nEi.{{[0-9a-f]*}}
134; NM-HYBRID1-DAG: U _ZN1E1mEi.{{[0-9a-f]*}}
135; NM-HYBRID1-DAG: U _ZN1D1mEi
136
137; NM-HYBRID2-DAG: T _ZN1A1nEi.{{[0-9a-f]*}}
138; NM-HYBRID2-DAG: T _ZN1E1mEi.{{[0-9a-f]*}}
139; NM-HYBRID2-DAG: W _ZN1D1mEi
140; NM-HYBRID2-DAG: T _ZN1B1fEi
141; NM-HYBRID2-DAG: T _ZN1C1fEi
142
143; REMARK-DAG: single-impl: devirtualized a call to _ZN1A1nEi
144; REMARK-DAG: single-impl: devirtualized a call to _ZN1D1mEi
145; We should devirt call to _ZN1E1mEi once in importing module and once
146; in original (exporting) module.
147; REMARK-DAG: single-impl: devirtualized a call to _ZN1E1mEi
148; REMARK-DAG: single-impl: devirtualized a call to _ZN1E1mEi
149
150target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
151target triple = "x86_64-grtev4-linux-gnu"
152
153%struct.A = type { i32 (...)** }
154%struct.B = type { %struct.A }
155%struct.C = type { %struct.A }
156%struct.D = type { i32 (...)** }
157%struct.E = type { i32 (...)** }
158
159@_ZTV1B = external constant [4 x i8*]
160@_ZTV1C = external constant [4 x i8*]
161;@_ZTV1D = external constant [3 x i8*]
162@_ZTV1D = linkonce_odr constant { [3 x i8*] } { [3 x i8*] [i8* null, i8* undef, i8* bitcast (i32 (%struct.D*, i32)* @_ZN1D1mEi to i8*)] }, !type !3
163
164define linkonce_odr i32 @_ZN1D1mEi(%struct.D* %this, i32 %a) #0 {
165   ret i32 0
166}
167
168; CHECK-IR1-LABEL: define i32 @test
169define i32 @test(%struct.A* %obj, %struct.D* %obj2, %struct.E* %obj3, i32 %a) {
170entry:
171  %0 = bitcast %struct.A* %obj to i8***
172  %vtable = load i8**, i8*** %0
173  %1 = bitcast i8** %vtable to i8*
174  %p = call i1 @llvm.type.test(i8* %1, metadata !"_ZTS1A")
175  call void @llvm.assume(i1 %p)
176  %fptrptr = getelementptr i8*, i8** %vtable, i32 1
177  %2 = bitcast i8** %fptrptr to i32 (%struct.A*, i32)**
178  %fptr1 = load i32 (%struct.A*, i32)*, i32 (%struct.A*, i32)** %2, align 8
179
180  ; Check that the call was devirtualized. Ignore extra character before
181  ; symbol name which would happen if it was promoted during module
182  ; splitting for hybrid WPD.
183  ; CHECK-IR1: %call = tail call i32 @_ZN1A1nEi
184  %call = tail call i32 %fptr1(%struct.A* nonnull %obj, i32 %a)
185
186  %3 = bitcast i8** %vtable to i32 (%struct.A*, i32)**
187  %fptr22 = load i32 (%struct.A*, i32)*, i32 (%struct.A*, i32)** %3, align 8
188
189  ; We still have to call it as virtual.
190  ; CHECK-IR1: %call3 = tail call i32 %fptr22
191  %call3 = tail call i32 %fptr22(%struct.A* nonnull %obj, i32 %call)
192
193  %4 = bitcast %struct.D* %obj2 to i8***
194  %vtable2 = load i8**, i8*** %4
195  %5 = bitcast i8** %vtable2 to i8*
196  %p2 = call i1 @llvm.type.test(i8* %5, metadata !"_ZTS1D")
197  call void @llvm.assume(i1 %p2)
198
199  %6 = bitcast i8** %vtable2 to i32 (%struct.D*, i32)**
200  %fptr33 = load i32 (%struct.D*, i32)*, i32 (%struct.D*, i32)** %6, align 8
201
202  ; Check that the call was devirtualized.
203  ; CHECK-IR1: %call4 = tail call i32 @_ZN1D1mEi
204  %call4 = tail call i32 %fptr33(%struct.D* nonnull %obj2, i32 %call3)
205
206  %call5 = tail call i32 @test2(%struct.E* nonnull %obj3, i32 %call4)
207  ret i32 %call5
208}
209; CHECK-IR1-LABEL: ret i32
210; CHECK-IR1-LABEL: }
211
212; CHECK-IR2: define i32 @test2
213; CHECK-IR2-NEXT: entry:
214; Check that the call was devirtualized. Ignore extra character before
215; symbol name which would happen if it was promoted during module
216; splitting for hybrid WPD.
217; CHECK-IR2-NEXT:   %call4 = tail call i32 @{{.*}}_ZN1E1mEi
218
219declare i1 @llvm.type.test(i8*, metadata)
220declare void @llvm.assume(i1)
221declare i32 @test2(%struct.E* %obj, i32 %a)
222
223attributes #0 = { noinline optnone }
224
225!3 = !{i64 16, !"_ZTS1D"}
226