1 ; REQUIRES: x86-registered-target
3 ; Test that devirtualization option -wholeprogramdevirt-check adds code to check
4 ; that the devirtualization decision was correct and trap or fallback if not.
6 ; The vtables have vcall_visibility metadata with hidden visibility, to enable
9 ; Generate unsplit module with summary for ThinLTO index-based WPD.
10 ; RUN: opt -thinlto-bc -o %t2.o %s
12 ; Check first in trapping mode.
13 ; RUN: llvm-lto2 run %t2.o -save-temps -pass-remarks=. \
14 ; RUN: -wholeprogramdevirt-check=trap \
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
22 ; Check next in fallback mode.
23 ; RUN: llvm-lto2 run %t2.o -save-temps -pass-remarks=. \
24 ; RUN: -wholeprogramdevirt-check=fallback \
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
32 ; REMARK-DAG: single-impl: devirtualized a call to _ZN1A1nEi
34 target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
35 target triple = "x86_64-grtev4-linux-gnu"
37 %struct.A = type { i32 (...)** }
38 %struct.B = type { %struct.A }
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
43 ; CHECK-LABEL: define {{(noundef )?}}i32 @test
44 define i32 @test(%struct.A* %obj, i32 %a) {
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
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.
61 ; TRAP: br i1 %.not, label %1, label %0
63 ; TRAP: tail call void @llvm.debugtrap()
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.
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.
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
89 ; CHECK-LABEL: ret i32
92 declare i1 @llvm.type.test(i8*, metadata)
93 declare void @llvm.assume(i1)
95 define i32 @_ZN1B1fEi(%struct.B* %this, i32 %a) #0 {
99 define i32 @_ZN1A1nEi(%struct.A* %this, i32 %a) #0 {
103 ; Make sure we don't inline or otherwise optimize out the direct calls.
104 attributes #0 = { noinline optnone }
106 !0 = !{i64 16, !"_ZTS1A"}
107 !1 = !{i64 16, !"_ZTS1B"}
111 !6 = !{!"VP", i32 0, i64 1, i64 1621563287929432257, i64 1}
112 !7 = !{i32 (%struct.A*, i32)* @_ZN1A1nEi}