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