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