1; REQUIRES: x86-registered-target
2
3; Test CFI devirtualization through the thin link and backend.
4
5; RUN: opt -thinlto-bc -thinlto-split-lto-unit -o %t.o %s
6
7; Legacy PM
8; RUN: llvm-lto2 run %t.o -save-temps -pass-remarks=. \
9; RUN:   -whole-program-visibility \
10; RUN:   -o %t3 \
11; RUN:   -r=%t.o,test,px \
12; RUN:   -r=%t.o,_ZN1A1nEi,p \
13; RUN:   -r=%t.o,_ZN1B1fEi,p \
14; RUN:   -r=%t.o,_ZN1C1fEi,p \
15; RUN:   -r=%t.o,empty,p \
16; RUN:   -r=%t.o,_ZTV1B, \
17; RUN:   -r=%t.o,_ZTV1C, \
18; RUN:   -r=%t.o,_ZN1A1nEi, \
19; RUN:   -r=%t.o,_ZN1B1fEi, \
20; RUN:   -r=%t.o,_ZN1C1fEi, \
21; RUN:   -r=%t.o,_ZTV1B,px \
22; RUN:   -r=%t.o,_ZTV1C,px 2>&1 | FileCheck %s --check-prefix=REMARK
23; RUN: llvm-dis %t3.1.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-IR
24
25; New PM
26; RUN: llvm-lto2 run %t.o -save-temps -use-new-pm -pass-remarks=. \
27; RUN:   -whole-program-visibility \
28; RUN:   -o %t3 \
29; RUN:   -r=%t.o,test,px \
30; RUN:   -r=%t.o,_ZN1A1nEi,p \
31; RUN:   -r=%t.o,_ZN1B1fEi,p \
32; RUN:   -r=%t.o,_ZN1C1fEi,p \
33; RUN:   -r=%t.o,empty,p \
34; RUN:   -r=%t.o,_ZTV1B, \
35; RUN:   -r=%t.o,_ZTV1C, \
36; RUN:   -r=%t.o,_ZN1A1nEi, \
37; RUN:   -r=%t.o,_ZN1B1fEi, \
38; RUN:   -r=%t.o,_ZN1C1fEi, \
39; RUN:   -r=%t.o,_ZTV1B,px \
40; RUN:   -r=%t.o,_ZTV1C,px 2>&1 | FileCheck %s --check-prefix=REMARK
41; RUN: llvm-dis %t3.1.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-IR
42
43; REMARK: single-impl: devirtualized a call to _ZN1A1nEi
44
45; Next check that we emit an error when trying to LTO link this module
46; containing an llvm.type.checked.load (with a split LTO Unit) with one
47; that does not have a split LTO Unit. Use -thinlto-distributed-indexes
48; to ensure it is being caught in the thin link.
49; RUN: opt -thinlto-bc -o %t2.o %S/Inputs/empty.ll
50; RUN: not llvm-lto2 run %t.o %t2.o -thinlto-distributed-indexes \
51; RUN:   -whole-program-visibility \
52; RUN:   -o %t3 \
53; RUN:   -r=%t.o,test,px \
54; RUN:   -r=%t.o,_ZN1A1nEi,p \
55; RUN:   -r=%t.o,_ZN1B1fEi,p \
56; RUN:   -r=%t.o,_ZN1C1fEi,p \
57; RUN:   -r=%t.o,empty,p \
58; RUN:   -r=%t.o,_ZTV1B, \
59; RUN:   -r=%t.o,_ZTV1C, \
60; RUN:   -r=%t.o,_ZN1A1nEi, \
61; RUN:   -r=%t.o,_ZN1B1fEi, \
62; RUN:   -r=%t.o,_ZN1C1fEi, \
63; RUN:   -r=%t.o,_ZTV1B,px \
64; RUN:   -r=%t.o,_ZTV1C,px 2>&1 | FileCheck %s --check-prefix=ERROR
65; ERROR: failed: inconsistent LTO Unit splitting (recompile with -fsplit-lto-unit)
66
67target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
68target triple = "x86_64-grtev4-linux-gnu"
69
70%struct.A = type { i32 (...)** }
71%struct.B = type { %struct.A }
72%struct.C = type { %struct.A }
73
74@_ZTV1B = constant { [4 x i8*] } { [4 x i8*] [i8* null, i8* undef, i8* bitcast (i32 (%struct.B*, i32)* @_ZN1B1fEi to i8*), i8* bitcast (i32 (%struct.A*, i32)* @_ZN1A1nEi to i8*)] }, !type !0, !type !1
75@_ZTV1C = constant { [4 x i8*] } { [4 x i8*] [i8* null, i8* undef, i8* bitcast (i32 (%struct.C*, i32)* @_ZN1C1fEi to i8*), i8* bitcast (i32 (%struct.A*, i32)* @_ZN1A1nEi to i8*)] }, !type !0, !type !2
76
77; Put declaration first to test handling of remarks when the first
78; function has no basic blocks.
79declare void @empty()
80
81; CHECK-IR-LABEL: define i32 @test
82define i32 @test(%struct.A* %obj, i32 %a) {
83entry:
84  %0 = bitcast %struct.A* %obj to i8**
85  %vtable5 = load i8*, i8** %0
86
87  %1 = tail call { i8*, i1 } @llvm.type.checked.load(i8* %vtable5, i32 8, metadata !"_ZTS1A")
88  %2 = extractvalue { i8*, i1 } %1, 1
89  br i1 %2, label %cont, label %trap
90
91trap:
92  tail call void @llvm.trap()
93  unreachable
94
95cont:
96  %3 = extractvalue { i8*, i1 } %1, 0
97  %4 = bitcast i8* %3 to i32 (%struct.A*, i32)*
98
99  ; Check that the call was devirtualized.
100  ; CHECK-IR: %call = tail call i32 @_ZN1A1nEi
101  %call = tail call i32 %4(%struct.A* nonnull %obj, i32 %a)
102  %vtable16 = load i8*, i8** %0
103  %5 = tail call { i8*, i1 } @llvm.type.checked.load(i8* %vtable16, i32 0, metadata !"_ZTS1A")
104  %6 = extractvalue { i8*, i1 } %5, 1
105  br i1 %6, label %cont2, label %trap
106
107cont2:
108  %7 = extractvalue { i8*, i1 } %5, 0
109  %8 = bitcast i8* %7 to i32 (%struct.A*, i32)*
110
111  ; Check that traps are conditional. Invalid TYPE_ID can cause
112  ; unconditional traps.
113  ; CHECK-IR: br i1 {{.*}}, label %trap, label %cont2
114
115  ; We still have to call it as virtual.
116  ; CHECK-IR: %call3 = tail call i32 %7
117  %call3 = tail call i32 %8(%struct.A* nonnull %obj, i32 %call)
118  ret i32 %call3
119}
120; CHECK-IR-LABEL: ret i32
121; CHECK-IR-LABEL: }
122
123declare { i8*, i1 } @llvm.type.checked.load(i8*, i32, metadata)
124declare void @llvm.trap()
125
126declare i32 @_ZN1B1fEi(%struct.B* %this, i32 %a)
127declare i32 @_ZN1A1nEi(%struct.A* %this, i32 %a)
128declare i32 @_ZN1C1fEi(%struct.C* %this, i32 %a)
129
130!0 = !{i64 16, !"_ZTS1A"}
131!1 = !{i64 16, !"_ZTS1B"}
132!2 = !{i64 16, !"_ZTS1C"}
133