1; REQUIRES: x86-registered-target 2 3; Test devirtualization through the thin link and backend, ensuring that 4; it is only applied when the type test corresponding to a devirtualization 5; dominates an indirect call using the same vtable pointer. Indirect 6; call promotion and inlining may introduce a guarded indirect call 7; that can be promoted, which uses the same vtable address as the fallback 8; indirect call that cannot be devirtualized. 9 10; The code below illustrates the structure when we started with code like: 11; 12; class A { 13; public: 14; virtual int foo() { return 1; } 15; virtual int bar() { return 1; } 16; }; 17; class B : public A { 18; public: 19; virtual int foo(); 20; virtual int bar(); 21; }; 22; 23; int foo(A *a) { 24; return a->foo(); // ICP profile says most calls are to B::foo() 25; } 26; 27; int B::foo() { 28; return bar(); 29; } 30; 31; After the compile step, which will perform ICP and a round of inlining, we 32; have something like: 33; int foo(A *a) { 34; if (&a->foo() == B::foo()) 35; return ((B*)a)->bar(); // Inlined from promoted direct call to B::foo() 36; else 37; return a->foo(); 38; 39; The inlined code seqence will have a type test against "_ZTS1B", 40; which will allow us to devirtualize indirect call ((B*)a)->bar() to B::bar(); 41; Both that type test and the one for the fallback a->foo() indirect call 42; will use the same vtable pointer. Without a dominance check, we could 43; incorrectly devirtualize a->foo() to B::foo(); 44 45; RUN: opt -thinlto-bc -thinlto-split-lto-unit -o %t.o %s 46 47; RUN: llvm-lto2 run %t.o -save-temps -pass-remarks=. \ 48; RUN: -whole-program-visibility \ 49; RUN: -o %t3 \ 50; RUN: -r=%t.o,_Z3bazP1A,px \ 51; RUN: -r=%t.o,_ZN1A3fooEv, \ 52; RUN: -r=%t.o,_ZN1A3barEv, \ 53; RUN: -r=%t.o,_ZN1B3fooEv, \ 54; RUN: -r=%t.o,_ZN1B3barEv, \ 55; RUN: -r=%t.o,_ZTV1A, \ 56; RUN: -r=%t.o,_ZTV1B, \ 57; RUN: -r=%t.o,_ZN1A3fooEv, \ 58; RUN: -r=%t.o,_ZN1A3barEv, \ 59; RUN: -r=%t.o,_ZN1B3fooEv, \ 60; RUN: -r=%t.o,_ZN1B3barEv, \ 61; RUN: -r=%t.o,_ZTV1A,px \ 62; RUN: -r=%t.o,_ZTV1B,px 2>&1 | FileCheck %s --check-prefix=REMARK 63; RUN: llvm-dis %t3.1.4.opt.bc -o - | FileCheck %s --check-prefix=CHECK-IR 64 65; We should only devirtualize the inlined call to bar(). 66; REMARK-NOT: single-impl: devirtualized a call to _ZN1B3fooEv 67; REMARK: single-impl: devirtualized a call to _ZN1B3barEv 68; REMARK-NOT: single-impl: devirtualized a call to _ZN1B3fooEv 69 70target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" 71target triple = "x86_64-grtev4-linux-gnu" 72 73%class.A = type { ptr } 74%class.B = type { %class.A } 75 76@_ZTV1A = linkonce_odr hidden unnamed_addr constant { [4 x ptr] } { [4 x ptr] [ptr null, ptr undef, ptr @_ZN1A3fooEv, ptr @_ZN1A3barEv] }, align 8, !type !0 77@_ZTV1B = hidden unnamed_addr constant { [4 x ptr] } { [4 x ptr] [ptr null, ptr undef, ptr @_ZN1B3fooEv, ptr @_ZN1B3barEv] }, align 8, !type !0, !type !1 78 79define hidden i32 @_Z3bazP1A(ptr %a) local_unnamed_addr { 80entry: 81 %vtable = load ptr, ptr %a, align 8 82 %0 = tail call i1 @llvm.type.test(ptr %vtable, metadata !"_ZTS1A") 83 tail call void @llvm.assume(i1 %0) 84 %1 = load ptr, ptr %vtable, align 8 85 ; This is the compare instruction inserted by ICP 86 %2 = icmp eq ptr %1, @_ZN1B3fooEv 87 br i1 %2, label %if.true.direct_targ, label %if.false.orig_indirect 88 89; This block contains the promoted and inlined call to B::foo(); 90; CHECK-IR: if.true.direct_targ: ; preds = %entry 91if.true.direct_targ: ; preds = %entry 92 %3 = tail call i1 @llvm.type.test(ptr %vtable, metadata !"_ZTS1B") 93 tail call void @llvm.assume(i1 %3) 94 %vfn.i1 = getelementptr inbounds ptr, ptr %vtable, i64 1 95 %4 = load ptr, ptr %vfn.i1, align 8 96; Call to bar() can be devirtualized to call to B::bar(), since it was 97; inlined from B::foo() after ICP introduced the guarded promotion. 98; CHECK-IR: %call.i = tail call i32 @_ZN1B3barEv(ptr nonnull %a) 99 %call.i = tail call i32 %4(ptr %a) 100 br label %if.end.icp 101 102; This block contains the fallback indirect call a->foo() 103; CHECK-IR: if.false.orig_indirect: 104if.false.orig_indirect: ; preds = %entry 105; Fallback indirect call to foo() cannot be devirtualized. 106; CHECK-IR: %call = tail call i32 % 107 %call = tail call i32 %1(ptr nonnull %a) 108 br label %if.end.icp 109 110if.end.icp: ; preds = %if.false.orig_indirect, %if.true.direct_targ 111 %5 = phi i32 [ %call, %if.false.orig_indirect ], [ %call.i, %if.true.direct_targ ] 112 ret i32 %5 113} 114 115declare i1 @llvm.type.test(ptr, metadata) 116 117declare void @llvm.assume(i1) 118 119declare dso_local i32 @_ZN1B3fooEv(ptr %this) unnamed_addr 120declare dso_local i32 @_ZN1B3barEv(ptr) unnamed_addr 121declare dso_local i32 @_ZN1A3barEv(ptr %this) unnamed_addr 122declare dso_local i32 @_ZN1A3fooEv(ptr %this) unnamed_addr 123 124!0 = !{i64 16, !"_ZTS1A"} 125!1 = !{i64 16, !"_ZTS1B"} 126