1
2; RUN: opt < %s -globaldce -S | FileCheck %s
3
4target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
5
6; struct A {
7;   A();
8;   virtual int foo(int);
9;   virtual int bar(float);
10; };
11;
12; struct B : A {
13;   B();
14;   virtual int foo(int);
15;   virtual int bar(float);
16; };
17;
18; A::A() {}
19; B::B() {}
20; int A::foo(int)   { return 1; }
21; int A::bar(float) { return 2; }
22; int B::foo(int)   { return 3; }
23; int B::bar(float) { return 4; }
24;
25; extern "C" int test(B *p, int (B::*q)(int)) { return (p->*q)(42); }
26
27; Member function pointers are tracked by the combination of their object type
28; and function type, which must both be compatible. Here, the call is through a
29; pointer of type "int (B::*q)(int)", so the call could only be dispatched to
30; B::foo. It can't be dispatched to A::bar or B::bar as the function pointer
31; does not match, and it can't be dispatched to A::foo as the object type
32; doesn't match, so those can be removed.
33
34%struct.A = type { i32 (...)** }
35%struct.B = type { %struct.A }
36
37; CHECK: @_ZTV1A = internal unnamed_addr constant { [4 x i8*] } zeroinitializer
38@_ZTV1A = internal unnamed_addr constant { [4 x i8*] } { [4 x i8*] [i8* null, i8* null, i8* bitcast (i32 (%struct.A*, i32)* @_ZN1A3fooEi to i8*), i8* bitcast (i32 (%struct.A*, float)* @_ZN1A3barEf to i8*)] }, align 8, !type !0, !type !1, !type !2, !vcall_visibility !3
39; CHECK: @_ZTV1B = internal unnamed_addr constant { [4 x i8*] } { [4 x i8*] [i8* null, i8* null, i8* bitcast (i32 (%struct.B*, i32)* @_ZN1B3fooEi to i8*), i8* null] }
40@_ZTV1B = internal unnamed_addr constant { [4 x i8*] } { [4 x i8*] [i8* null, i8* null, i8* bitcast (i32 (%struct.B*, i32)* @_ZN1B3fooEi to i8*), i8* bitcast (i32 (%struct.B*, float)* @_ZN1B3barEf to i8*)] }, align 8, !type !0, !type !1, !type !2, !type !4, !type !5, !type !6, !vcall_visibility !3
41
42
43; CHECK-NOT: define internal i32 @_ZN1A3fooEi(
44define internal i32 @_ZN1A3fooEi(%struct.A* nocapture readnone %this, i32) unnamed_addr #1 align 2 {
45entry:
46  ret i32 1
47}
48
49; CHECK-NOT: define internal i32 @_ZN1A3barEf(
50define internal i32 @_ZN1A3barEf(%struct.A* nocapture readnone %this, float) unnamed_addr #1 align 2 {
51entry:
52  ret i32 2
53}
54
55; CHECK: define internal i32 @_ZN1B3fooEi(
56define internal i32 @_ZN1B3fooEi(%struct.B* nocapture readnone %this, i32) unnamed_addr #1 align 2 {
57entry:
58  ret i32 3
59}
60
61; CHECK-NOT: define internal i32 @_ZN1B3barEf(
62define internal i32 @_ZN1B3barEf(%struct.B* nocapture readnone %this, float) unnamed_addr #1 align 2 {
63entry:
64  ret i32 4
65}
66
67
68define hidden void @_ZN1AC2Ev(%struct.A* nocapture %this) {
69entry:
70  %0 = getelementptr inbounds %struct.A, %struct.A* %this, i64 0, i32 0
71  store i32 (...)** bitcast (i8** getelementptr inbounds ({ [4 x i8*] }, { [4 x i8*] }* @_ZTV1A, i64 0, inrange i32 0, i64 2) to i32 (...)**), i32 (...)*** %0, align 8
72  ret void
73}
74
75define hidden void @_ZN1BC2Ev(%struct.B* nocapture %this) {
76entry:
77  %0 = getelementptr inbounds %struct.B, %struct.B* %this, i64 0, i32 0, i32 0
78  store i32 (...)** bitcast (i8** getelementptr inbounds ({ [4 x i8*] }, { [4 x i8*] }* @_ZTV1B, i64 0, inrange i32 0, i64 2) to i32 (...)**), i32 (...)*** %0, align 8
79  ret void
80}
81
82define hidden i32 @test(%struct.B* %p, i64 %q.coerce0, i64 %q.coerce1) {
83entry:
84  %0 = bitcast %struct.B* %p to i8*
85  %1 = getelementptr inbounds i8, i8* %0, i64 %q.coerce1
86  %this.adjusted = bitcast i8* %1 to %struct.B*
87  %2 = and i64 %q.coerce0, 1
88  %memptr.isvirtual = icmp eq i64 %2, 0
89  br i1 %memptr.isvirtual, label %memptr.nonvirtual, label %memptr.virtual
90
91memptr.virtual:                                   ; preds = %entry
92  %3 = bitcast i8* %1 to i8**
93  %vtable = load i8*, i8** %3, align 8
94  %4 = add i64 %q.coerce0, -1
95  %5 = getelementptr i8, i8* %vtable, i64 %4, !nosanitize !12
96  %6 = tail call { i8*, i1 } @llvm.type.checked.load(i8* %5, i32 0, metadata !"_ZTSM1BFiiE.virtual"), !nosanitize !12
97  %7 = extractvalue { i8*, i1 } %6, 0, !nosanitize !12
98  %memptr.virtualfn = bitcast i8* %7 to i32 (%struct.B*, i32)*, !nosanitize !12
99  br label %memptr.end
100
101memptr.nonvirtual:                                ; preds = %entry
102  %memptr.nonvirtualfn = inttoptr i64 %q.coerce0 to i32 (%struct.B*, i32)*
103  br label %memptr.end
104
105memptr.end:                                       ; preds = %memptr.nonvirtual, %memptr.virtual
106  %8 = phi i32 (%struct.B*, i32)* [ %memptr.virtualfn, %memptr.virtual ], [ %memptr.nonvirtualfn, %memptr.nonvirtual ]
107  %call = tail call i32 %8(%struct.B* %this.adjusted, i32 42)
108  ret i32 %call
109}
110
111declare { i8*, i1 } @llvm.type.checked.load(i8*, i32, metadata)
112
113!llvm.module.flags = !{!7}
114
115!0 = !{i64 16, !"_ZTS1A"}
116!1 = !{i64 16, !"_ZTSM1AFiiE.virtual"}
117!2 = !{i64 24, !"_ZTSM1AFifE.virtual"}
118!3 = !{i64 2}
119!4 = !{i64 16, !"_ZTS1B"}
120!5 = !{i64 16, !"_ZTSM1BFiiE.virtual"}
121!6 = !{i64 24, !"_ZTSM1BFifE.virtual"}
122!7 = !{i32 1, !"Virtual Function Elim", i32 1}
123!12 = !{}
124