xref: /llvm-project/llvm/test/ThinLTO/X86/devirt_check.ll (revision e1aa1e43decf9275175845bea970ef6d7c2b1af6)
1; REQUIRES: x86-registered-target
2
3; Test that devirtualization option -wholeprogramdevirt-check adds code to check
4; that the devirtualization decision was correct and trap or fallback if not.
5
6; The vtables have vcall_visibility metadata with hidden visibility, to enable
7; devirtualization.
8
9; Generate unsplit module with summary for ThinLTO index-based WPD.
10; RUN: opt -thinlto-bc -o %t2.o %s
11
12; Check first in trapping mode.
13; RUN: llvm-lto2 run %t2.o -save-temps -pass-remarks=. \
14; RUN:	 -wholeprogramdevirt-check=trap \
15; RUN:   -o %t3 \
16; RUN:   -r=%t2.o,test,px \
17; RUN:   -r=%t2.o,_ZN1A1nEi,p \
18; RUN:   -r=%t2.o,_ZN1B1fEi,p \
19; RUN:   -r=%t2.o,_ZTV1B,px 2>&1 | FileCheck %s --check-prefix=REMARK
20; RUN: llvm-dis %t3.1.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK --check-prefix=TRAP
21
22; Check next in fallback mode.
23; RUN: llvm-lto2 run %t2.o -save-temps -pass-remarks=. \
24; RUN:	 -wholeprogramdevirt-check=fallback \
25; RUN:   -o %t3 \
26; RUN:   -r=%t2.o,test,px \
27; RUN:   -r=%t2.o,_ZN1A1nEi,p \
28; RUN:   -r=%t2.o,_ZN1B1fEi,p \
29; RUN:   -r=%t2.o,_ZTV1B,px 2>&1 | FileCheck %s --check-prefix=REMARK
30; RUN: llvm-dis %t3.1.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK --check-prefix=FALLBACK
31
32; REMARK-DAG: single-impl: devirtualized a call to _ZN1A1nEi
33
34target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
35target triple = "x86_64-grtev4-linux-gnu"
36
37%struct.A = type { i32 (...)** }
38%struct.B = type { %struct.A }
39
40@_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, !vcall_visibility !5
41
42
43; CHECK-LABEL: define {{(noundef )?}}i32 @test
44define i32 @test(%struct.A* %obj, i32 %a) {
45entry:
46  %0 = bitcast %struct.A* %obj to i8***
47  %vtable = load i8**, i8*** %0
48  %1 = bitcast i8** %vtable to i8*
49  %p = call i1 @llvm.type.test(i8* %1, metadata !"_ZTS1A")
50  call void @llvm.assume(i1 %p)
51  %fptrptr = getelementptr i8*, i8** %vtable, i32 1
52  %2 = bitcast i8** %fptrptr to i32 (%struct.A*, i32)**
53  %fptr1 = load i32 (%struct.A*, i32)*, i32 (%struct.A*, i32)** %2, align 8
54
55  ; Check that the call was devirtualized, but preceeded by a check guarding
56  ; a trap if the function pointer doesn't match.
57  ; TRAP:   %.not = icmp eq ptr %fptr1, @_ZN1A1nEi
58  ; Ensure !prof and !callees metadata for indirect call promotion removed.
59  ; TRAP-NOT: prof
60  ; TRAP-NOT: callees
61  ; TRAP:   br i1 %.not, label %1, label %0, !prof ![[PROF:[0-9]+]]
62  ; TRAP: 0:
63  ; TRAP:   tail call void @llvm.debugtrap()
64  ; TRAP:   br label %1
65  ; TRAP: 1:
66  ; TRAP:   tail call i32 @_ZN1A1nEi
67  ; Check that the call was devirtualized, but preceeded by a check guarding
68  ; a fallback if the function pointer doesn't match.
69  ; FALLBACK:   %0 = icmp eq ptr %fptr1, @_ZN1A1nEi
70  ; FALLBACK:   br i1 %0, label %if.true.direct_targ, label %if.false.orig_indirect
71  ; FALLBACK: if.true.direct_targ:
72  ; FALLBACK:   tail call i32 @_ZN1A1nEi
73  ; Ensure !prof and !callees metadata for indirect call promotion removed.
74  ; FALLBACK-NOT: prof
75  ; FALLBACK-NOT: callees
76  ; FALLBACK:   br label %if.end.icp
77  ; FALLBACK: if.false.orig_indirect:
78  ; FALLBACK:   tail call i32 %fptr1
79  ; Ensure !prof and !callees metadata for indirect call promotion removed.
80  ; In particular, if left on the fallback indirect call ICP may perform an
81  ; additional round of promotion.
82  ; FALLBACK-NOT: prof
83  ; FALLBACK-NOT: callees
84  ; FALLBACK:   br label %if.end.icp
85  %call = tail call i32 %fptr1(%struct.A* nonnull %obj, i32 %a), !prof !6, !callees !7
86
87  ret i32 %call
88}
89; CHECK-LABEL:   ret i32
90; CHECK-LABEL: }
91
92; TRAP: ![[PROF]] = !{!"branch_weights", i32 1048575, i32 1}
93
94declare i1 @llvm.type.test(i8*, metadata)
95declare void @llvm.assume(i1)
96
97define i32 @_ZN1B1fEi(%struct.B* %this, i32 %a) #0 {
98   ret i32 0;
99}
100
101define i32 @_ZN1A1nEi(%struct.A* %this, i32 %a) #0 {
102   ret i32 0;
103}
104
105; Make sure we don't inline or otherwise optimize out the direct calls.
106attributes #0 = { noinline optnone }
107
108!0 = !{i64 16, !"_ZTS1A"}
109!1 = !{i64 16, !"_ZTS1B"}
110!3 = !{i64 16, !4}
111!4 = distinct !{}
112!5 = !{i64 1}
113!6 = !{!"VP", i32 0, i64 1, i64 1621563287929432257, i64 1}
114!7 = !{i32 (%struct.A*, i32)* @_ZN1A1nEi}
115