xref: /llvm-project/clang/test/CodeGen/thinlto-distributed-cfi-devirt.ll (revision d33937b6236767137a1ec3393d0933f10eed4ffe)
1; REQUIRES: x86-registered-target
2
3; Backend test for distribute ThinLTO with CFI.
4; It additionally enables -fwhole-program-vtables to get more information in
5; TYPE_IDs of GLOBALVAL_SUMMARY_BLOCK.
6
7; RUN: opt -thinlto-bc -thinlto-split-lto-unit -o %t.o %s
8
9; RUN: llvm-lto2 run -thinlto-distributed-indexes -disable-thinlto-funcattrs=0 %t.o \
10; RUN:   -whole-program-visibility \
11; RUN:   -o %t2.index \
12; RUN:   -r=%t.o,test,px \
13; RUN:   -r=%t.o,_ZN1A1nEi,p \
14; RUN:   -r=%t.o,_ZN1B1fEi,p \
15; RUN:   -r=%t.o,_ZN1C1fEi,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
23
24; Ensure that typeids are in the index.
25; RUN: llvm-bcanalyzer -dump %t.o.thinlto.bc | FileCheck %s
26; CHECK-LABEL: <GLOBALVAL_SUMMARY_BLOCK
27; CHECK: <TYPE_ID op0=0 op1=6 op2=4 op3=7 op4=0 op5=0 op6=0 op7=0 op8=0 op9=2 op10=6 op11=0 op12=0 op13=8 op14=1 op15=6 op16=9 op17=0/>
28; CHECK-LABEL: </GLOBALVAL_SUMMARY_BLOCK
29; CHECK-LABEL: <STRTAB_BLOCK
30; CHECK: blob data = '_ZTS1A_ZN1A1nEi'
31; CHECK-LABEL: </STRTAB_BLOCK
32
33; RUN: llvm-dis %t.o.thinlto.bc -o - | FileCheck %s --check-prefix=CHECK-DIS
34; Round trip it through llvm-as
35; RUN: llvm-dis %t.o.thinlto.bc -o - | llvm-as -o - | llvm-dis -o - | FileCheck %s --check-prefix=CHECK-DIS
36; CHECK-DIS: ^0 = module: (path: "{{.*}}thinlto-distributed-cfi-devirt.ll.tmp.o", hash: ({{.*}}, {{.*}}, {{.*}}, {{.*}}, {{.*}}))
37; CHECK-DIS: ^1 = gv: (guid: 8346051122425466633, summaries: (function: (module: ^0, flags: (linkage: external, visibility: default, notEligibleToImport: 0, live: 1, dsoLocal: 0, canAutoHide: 0, importType: definition), insts: 18, funcFlags: (readNone: 0, readOnly: 0, noRecurse: 0, returnDoesNotAlias: 0, noInline: 0, alwaysInline: 0, noUnwind: 0, mayThrow: 0, hasUnknownCall: 1, mustBeUnreachable: 0), typeIdInfo: (typeTests: (^2), typeCheckedLoadVCalls: (vFuncId: (^2, offset: 8), vFuncId: (^2, offset: 0))))))
38; CHECK-DIS: ^2 = typeid: (name: "_ZTS1A", summary: (typeTestRes: (kind: allOnes, sizeM1BitWidth: 7), wpdResolutions: ((offset: 0, wpdRes: (kind: branchFunnel)), (offset: 8, wpdRes: (kind: singleImpl, singleImplName: "_ZN1A1nEi"))))) ; guid = 7004155349499253778
39
40; RUN: %clang_cc1 -triple x86_64-grtev4-linux-gnu \
41; RUN:   -fthinlto-index=%t.o.thinlto.bc -O2 -Rpass=wholeprogramdevirt \
42; RUN:   -emit-llvm -o - -x ir %t.o 2>&1 | FileCheck %s --check-prefixes=CHECK-IR --check-prefixes=REMARKS
43
44; Check that the devirtualization is suppressed via -wholeprogramdevirt-skip
45; RUN: %clang_cc1 -triple x86_64-grtev4-linux-gnu -mllvm -wholeprogramdevirt-skip=_ZN1A1nEi \
46; RUN:   -fthinlto-index=%t.o.thinlto.bc -O2 -Rpass=wholeprogramdevirt \
47; RUN:   -emit-llvm -o - -x ir %t.o 2>&1 | FileCheck %s --check-prefixes=SKIP-IR --check-prefixes=SKIP-REMARKS
48
49; REMARKS: single-impl: devirtualized a call to _ZN1A1nEi
50; SKIP-REMARKS-NOT: single-impl
51
52; Check that backend does not fail generating native code.
53; RUN: %clang_cc1 -triple x86_64-grtev4-linux-gnu \
54; RUN:   -emit-obj -fthinlto-index=%t.o.thinlto.bc -O2 \
55; RUN:   -o %t.native.o -x ir %t.o
56
57target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
58target triple = "x86_64-grtev4-linux-gnu"
59
60%struct.A = type { i32 (...)** }
61%struct.B = type { %struct.A }
62%struct.C = type { %struct.A }
63
64@_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
65@_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
66
67; CHECK-IR-LABEL: define i32 @test
68define i32 @test(%struct.A* %obj, i32 %a) {
69entry:
70  %0 = bitcast %struct.A* %obj to i8**
71  %vtable5 = load i8*, i8** %0
72
73  %1 = tail call { i8*, i1 } @llvm.type.checked.load(i8* %vtable5, i32 8, metadata !"_ZTS1A")
74  %2 = extractvalue { i8*, i1 } %1, 1
75  br i1 %2, label %cont, label %trap
76
77trap:
78  tail call void @llvm.trap()
79  unreachable
80
81cont:
82  %3 = extractvalue { i8*, i1 } %1, 0
83  %4 = bitcast i8* %3 to i32 (%struct.A*, i32)*
84
85  ; Check that the call was devirtualized.
86  ; CHECK-IR: %call = tail call i32 @_ZN1A1nEi
87  ; SKIP-IR-NOT: %call = tail call i32 @_ZN1A1nEi
88  %call = tail call i32 %4(%struct.A* nonnull %obj, i32 %a)
89  %vtable16 = load i8*, i8** %0
90  %5 = tail call { i8*, i1 } @llvm.type.checked.load(i8* %vtable16, i32 0, metadata !"_ZTS1A")
91  %6 = extractvalue { i8*, i1 } %5, 1
92  br i1 %6, label %cont2, label %trap
93
94cont2:
95  %7 = extractvalue { i8*, i1 } %5, 0
96  %8 = bitcast i8* %7 to i32 (%struct.A*, i32)*
97
98  ; Check that traps are conditional. Invalid TYPE_ID can cause
99  ; unconditional traps.
100  ; CHECK-IR: br i1 {{.*}}, label %trap
101
102  ; We still have to call it as virtual.
103  ; CHECK-IR: %call3 = tail call i32 {{%[0-9]+}}
104  %call3 = tail call i32 %8(%struct.A* nonnull %obj, i32 %call)
105  ret i32 %call3
106}
107; CHECK-IR-LABEL: ret i32
108; CHECK-IR-LABEL: }
109
110declare { i8*, i1 } @llvm.type.checked.load(i8*, i32, metadata)
111declare void @llvm.trap()
112
113declare i32 @_ZN1B1fEi(%struct.B* %this, i32 %a)
114declare i32 @_ZN1A1nEi(%struct.A* %this, i32 %a)
115declare i32 @_ZN1C1fEi(%struct.C* %this, i32 %a)
116
117!0 = !{i64 16, !"_ZTS1A"}
118!1 = !{i64 16, !"_ZTS1B"}
119!2 = !{i64 16, !"_ZTS1C"}
120