diff options
Diffstat (limited to 'llvm/test/Transforms/WholeProgramDevirt')
43 files changed, 2488 insertions, 0 deletions
diff --git a/llvm/test/Transforms/WholeProgramDevirt/Inputs/export.yaml b/llvm/test/Transforms/WholeProgramDevirt/Inputs/export.yaml new file mode 100644 index 00000000000..71cf38b216c --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/Inputs/export.yaml @@ -0,0 +1,21 @@ +--- +GlobalValueMap: + 42: + - Live: true + TypeTestAssumeVCalls: + - GUID: 14276520915468743435 # typeid1 + Offset: 0 + TypeCheckedLoadVCalls: + - GUID: 15427464259790519041 # typeid2 + Offset: 0 + TypeTestAssumeConstVCalls: + - VFunc: + GUID: 3515965990081467659 # typeid3 + Offset: 0 + Args: [12, 24] + TypeCheckedLoadConstVCalls: + - VFunc: + GUID: 17525413373118030901 # typeid4 + Offset: 0 + Args: [24, 12] +... diff --git a/llvm/test/Transforms/WholeProgramDevirt/Inputs/import-branch-funnel.yaml b/llvm/test/Transforms/WholeProgramDevirt/Inputs/import-branch-funnel.yaml new file mode 100644 index 00000000000..eaa23b406bf --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/Inputs/import-branch-funnel.yaml @@ -0,0 +1,11 @@ +--- +TypeIdMap: + typeid1: + WPDRes: + 0: + Kind: BranchFunnel + typeid2: + WPDRes: + 8: + Kind: BranchFunnel +... diff --git a/llvm/test/Transforms/WholeProgramDevirt/Inputs/import-indir.yaml b/llvm/test/Transforms/WholeProgramDevirt/Inputs/import-indir.yaml new file mode 100644 index 00000000000..30159c5012b --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/Inputs/import-indir.yaml @@ -0,0 +1,42 @@ +--- +GlobalValueMap: + 42: + - Live: true + TypeTestAssumeVCalls: + - GUID: 123 + Offset: 0 + - GUID: 456 + Offset: 4 + TypeCheckedLoadVCalls: + - GUID: 789 + Offset: 8 + - GUID: 1234 + Offset: 16 + TypeTestAssumeConstVCalls: + - VFunc: + GUID: 123 + Offset: 4 + Args: [12, 24] + TypeCheckedLoadConstVCalls: + - VFunc: + GUID: 456 + Offset: 8 + Args: [24, 12] +TypeIdMap: + typeid1: + WPDRes: + 0: + Kind: Indir + 4: + Kind: Indir + ResByArg: + "": + Kind: UniformRetVal + Info: 12 + 12: + Kind: UniformRetVal + Info: 24 + "12,24": + Kind: UniformRetVal + Info: 48 +... diff --git a/llvm/test/Transforms/WholeProgramDevirt/Inputs/import-single-impl.yaml b/llvm/test/Transforms/WholeProgramDevirt/Inputs/import-single-impl.yaml new file mode 100644 index 00000000000..26764eb3b29 --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/Inputs/import-single-impl.yaml @@ -0,0 +1,13 @@ +--- +TypeIdMap: + typeid1: + WPDRes: + 0: + Kind: SingleImpl + SingleImplName: singleimpl1 + typeid2: + WPDRes: + 8: + Kind: SingleImpl + SingleImplName: singleimpl2 +... diff --git a/llvm/test/Transforms/WholeProgramDevirt/Inputs/import-uniform-ret-val.yaml b/llvm/test/Transforms/WholeProgramDevirt/Inputs/import-uniform-ret-val.yaml new file mode 100644 index 00000000000..f1daae63b67 --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/Inputs/import-uniform-ret-val.yaml @@ -0,0 +1,19 @@ +--- +TypeIdMap: + typeid1: + WPDRes: + 0: + Kind: Indir + ResByArg: + 1: + Kind: UniformRetVal + Info: 42 + typeid2: + WPDRes: + 8: + Kind: Indir + ResByArg: + 1: + Kind: UniformRetVal + Info: 42 +... diff --git a/llvm/test/Transforms/WholeProgramDevirt/Inputs/import-unique-ret-val0.yaml b/llvm/test/Transforms/WholeProgramDevirt/Inputs/import-unique-ret-val0.yaml new file mode 100644 index 00000000000..597b1787776 --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/Inputs/import-unique-ret-val0.yaml @@ -0,0 +1,11 @@ +--- +TypeIdMap: + typeid2: + WPDRes: + 8: + Kind: Indir + ResByArg: + 3: + Kind: UniqueRetVal + Info: 0 +... diff --git a/llvm/test/Transforms/WholeProgramDevirt/Inputs/import-unique-ret-val1.yaml b/llvm/test/Transforms/WholeProgramDevirt/Inputs/import-unique-ret-val1.yaml new file mode 100644 index 00000000000..737ef1173c3 --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/Inputs/import-unique-ret-val1.yaml @@ -0,0 +1,11 @@ +--- +TypeIdMap: + typeid2: + WPDRes: + 8: + Kind: Indir + ResByArg: + 3: + Kind: UniqueRetVal + Info: 1 +... diff --git a/llvm/test/Transforms/WholeProgramDevirt/Inputs/import-vcp-branch-funnel.yaml b/llvm/test/Transforms/WholeProgramDevirt/Inputs/import-vcp-branch-funnel.yaml new file mode 100644 index 00000000000..e1b1bdbb818 --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/Inputs/import-vcp-branch-funnel.yaml @@ -0,0 +1,23 @@ +--- +TypeIdMap: + typeid1: + WPDRes: + 0: + Kind: BranchFunnel + ResByArg: + 1: + Kind: VirtualConstProp + Info: 0 + Byte: 42 + Bit: 0 + typeid2: + WPDRes: + 8: + Kind: BranchFunnel + ResByArg: + 3: + Kind: VirtualConstProp + Info: 0 + Byte: 43 + Bit: 128 +... diff --git a/llvm/test/Transforms/WholeProgramDevirt/Inputs/import-vcp.yaml b/llvm/test/Transforms/WholeProgramDevirt/Inputs/import-vcp.yaml new file mode 100644 index 00000000000..6109e4f765c --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/Inputs/import-vcp.yaml @@ -0,0 +1,23 @@ +--- +TypeIdMap: + typeid1: + WPDRes: + 0: + Kind: Indir + ResByArg: + 1: + Kind: VirtualConstProp + Info: 0 + Byte: 42 + Bit: 0 + typeid2: + WPDRes: + 8: + Kind: Indir + ResByArg: + 3: + Kind: VirtualConstProp + Info: 0 + Byte: 43 + Bit: 128 +... diff --git a/llvm/test/Transforms/WholeProgramDevirt/bad-read-from-vtable.ll b/llvm/test/Transforms/WholeProgramDevirt/bad-read-from-vtable.ll new file mode 100644 index 00000000000..e5d0e74b22e --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/bad-read-from-vtable.ll @@ -0,0 +1,81 @@ +; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s + +target datalayout = "e-p:64:64" +target triple = "x86_64-unknown-linux-gnu" + +@vt1 = constant [2 x i8*] [i8* zeroinitializer, i8* bitcast (void (i8*)* @vf to i8*)], !type !0 +@vt2 = constant i8* bitcast (void (i8*)* @vf to i8*), !type !1 + +define void @vf(i8* %this) { + ret void +} + +; CHECK: define void @unaligned1 +define void @unaligned1(i8* %obj) { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr i8, i8* %vtablei8, i32 1 + %fptrptr_casted = bitcast i8* %fptrptr to i8** + %fptr = load i8*, i8** %fptrptr_casted + %fptr_casted = bitcast i8* %fptr to void (i8*)* + ; CHECK: call void % + call void %fptr_casted(i8* %obj) + ret void +} + +; CHECK: define void @unaligned2 +define void @unaligned2(i8* %obj) { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid2") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr i8, i8* %vtablei8, i32 1 + %fptrptr_casted = bitcast i8* %fptrptr to i8** + %fptr = load i8*, i8** %fptrptr_casted + %fptr_casted = bitcast i8* %fptr to void (i8*)* + ; CHECK: call void % + call void %fptr_casted(i8* %obj) + ret void +} + +; CHECK: define void @outofbounds +define void @outofbounds(i8* %obj) { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr i8, i8* %vtablei8, i32 16 + %fptrptr_casted = bitcast i8* %fptrptr to i8** + %fptr = load i8*, i8** %fptrptr_casted + %fptr_casted = bitcast i8* %fptr to void (i8*)* + ; CHECK: call void % + call void %fptr_casted(i8* %obj) + ret void +} + +; CHECK: define void @nonfunction +define void @nonfunction(i8* %obj) { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr i8, i8* %vtablei8, i32 0 + %fptrptr_casted = bitcast i8* %fptrptr to i8** + %fptr = load i8*, i8** %fptrptr_casted + %fptr_casted = bitcast i8* %fptr to void (i8*)* + ; CHECK: call void % + call void %fptr_casted(i8* %obj) + ret void +} + +declare i1 @llvm.type.test(i8*, metadata) +declare void @llvm.assume(i1) + +!0 = !{i32 0, !"typeid"} +!1 = !{i32 0, !"typeid2"} diff --git a/llvm/test/Transforms/WholeProgramDevirt/branch-funnel-threshold.ll b/llvm/test/Transforms/WholeProgramDevirt/branch-funnel-threshold.ll new file mode 100644 index 00000000000..91cd4e419b6 --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/branch-funnel-threshold.ll @@ -0,0 +1,100 @@ +; RUN: opt -wholeprogramdevirt -wholeprogramdevirt-summary-action=export -wholeprogramdevirt-read-summary=%S/Inputs/export.yaml -wholeprogramdevirt-write-summary=%t -wholeprogramdevirt-branch-funnel-threshold=1 -S -o - %s | not grep @llvm.icall.branch.funnel | count 0 + +; RUN: opt -wholeprogramdevirt -wholeprogramdevirt-summary-action=export -wholeprogramdevirt-read-summary=%S/Inputs/export.yaml -wholeprogramdevirt-write-summary=%t -wholeprogramdevirt-branch-funnel-threshold=10 -S -o - %s | grep @llvm.icall.branch.funnel | count 4 + +; RUN: opt -wholeprogramdevirt -wholeprogramdevirt-summary-action=export -wholeprogramdevirt-read-summary=%S/Inputs/export.yaml -wholeprogramdevirt-write-summary=%t -wholeprogramdevirt-branch-funnel-threshold=100 -S -o - %s | grep @llvm.icall.branch.funnel | count 5 + +target datalayout = "e-p:64:64" +target triple = "x86_64-unknown-linux-gnu" + +@vt1_1 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf1_1 to i8*)], !type !0 +@vt1_2 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf1_2 to i8*)], !type !0 + +declare i32 @vf1_1(i8* %this, i32 %arg) +declare i32 @vf1_2(i8* %this, i32 %arg) + +@vt2_1 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf2_1 to i8*)], !type !1 +@vt2_2 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf2_2 to i8*)], !type !1 +@vt2_3 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf2_3 to i8*)], !type !1 +@vt2_4 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf2_4 to i8*)], !type !1 +@vt2_5 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf2_5 to i8*)], !type !1 +@vt2_6 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf2_6 to i8*)], !type !1 +@vt2_7 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf2_7 to i8*)], !type !1 +@vt2_8 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf2_8 to i8*)], !type !1 +@vt2_9 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf2_9 to i8*)], !type !1 +@vt2_10 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf2_10 to i8*)], !type !1 +@vt2_11 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf2_11 to i8*)], !type !1 + +declare i32 @vf2_1(i8* %this, i32 %arg) +declare i32 @vf2_2(i8* %this, i32 %arg) +declare i32 @vf2_3(i8* %this, i32 %arg) +declare i32 @vf2_4(i8* %this, i32 %arg) +declare i32 @vf2_5(i8* %this, i32 %arg) +declare i32 @vf2_6(i8* %this, i32 %arg) +declare i32 @vf2_7(i8* %this, i32 %arg) +declare i32 @vf2_8(i8* %this, i32 %arg) +declare i32 @vf2_9(i8* %this, i32 %arg) +declare i32 @vf2_10(i8* %this, i32 %arg) +declare i32 @vf2_11(i8* %this, i32 %arg) + +@vt3_1 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf3_1 to i8*)], !type !2 +@vt3_2 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf3_2 to i8*)], !type !2 + +declare i32 @vf3_1(i8* %this, i32 %arg) +declare i32 @vf3_2(i8* %this, i32 %arg) + +@vt4_1 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf4_1 to i8*)], !type !3 +@vt4_2 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf4_2 to i8*)], !type !3 + +declare i32 @vf4_1(i8* %this, i32 %arg) +declare i32 @vf4_2(i8* %this, i32 %arg) + +define i32 @fn1(i8* %obj) #0 { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid1") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to i32 (i8*, i32)* + %result = call i32 %fptr_casted(i8* %obj, i32 1) + ret i32 %result +} + +define i32 @fn2(i8* %obj) #0 { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid2") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to i32 (i8*, i32)* + %result = call i32 %fptr_casted(i8* %obj, i32 1) + ret i32 %result +} + +define i32 @fn3(i8* %obj) #0 { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !4) + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to i32 (i8*, i32)* + %result = call i32 %fptr_casted(i8* %obj, i32 1) + ret i32 %result +} + +declare i1 @llvm.type.test(i8*, metadata) +declare void @llvm.assume(i1) + +!0 = !{i32 0, !"typeid1"} +!1 = !{i32 0, !"typeid2"} +!2 = !{i32 0, !"typeid3"} +!3 = !{i32 0, !4} +!4 = distinct !{} + +attributes #0 = { "target-features"="+retpoline" } diff --git a/llvm/test/Transforms/WholeProgramDevirt/branch-funnel.ll b/llvm/test/Transforms/WholeProgramDevirt/branch-funnel.ll new file mode 100644 index 00000000000..49d9caaf71e --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/branch-funnel.ll @@ -0,0 +1,163 @@ +; RUN: opt -S -wholeprogramdevirt %s | FileCheck --check-prefixes=CHECK,RETP %s +; RUN: sed -e 's,+retpoline,-retpoline,g' %s | opt -S -wholeprogramdevirt | FileCheck --check-prefixes=CHECK,NORETP %s + +; RUN: opt -wholeprogramdevirt -wholeprogramdevirt-summary-action=export -wholeprogramdevirt-read-summary=%S/Inputs/export.yaml -wholeprogramdevirt-write-summary=%t -S -o - %s | FileCheck --check-prefixes=CHECK,RETP %s + +; RUN: opt -wholeprogramdevirt -wholeprogramdevirt-summary-action=export -wholeprogramdevirt-read-summary=%S/Inputs/export.yaml -wholeprogramdevirt-write-summary=%t -O3 -S -o - %s | FileCheck --check-prefixes=CHECK %s + +; RUN: FileCheck --check-prefix=SUMMARY %s < %t + +; SUMMARY: TypeIdMap: +; SUMMARY-NEXT: typeid3: +; SUMMARY-NEXT: TTRes: +; SUMMARY-NEXT: Kind: Unsat +; SUMMARY-NEXT: SizeM1BitWidth: 0 +; SUMMARY-NEXT: AlignLog2: 0 +; SUMMARY-NEXT: SizeM1: 0 +; SUMMARY-NEXT: BitMask: 0 +; SUMMARY-NEXT: InlineBits: 0 +; SUMMARY-NEXT: WPDRes: +; SUMMARY-NEXT: 0: +; SUMMARY-NEXT: Kind: BranchFunnel +; SUMMARY-NEXT: SingleImplName: '' +; SUMMARY-NEXT: ResByArg: +; SUMMARY-NEXT: typeid1: +; SUMMARY-NEXT: TTRes: +; SUMMARY-NEXT: Kind: Unsat +; SUMMARY-NEXT: SizeM1BitWidth: 0 +; SUMMARY-NEXT: AlignLog2: 0 +; SUMMARY-NEXT: SizeM1: 0 +; SUMMARY-NEXT: BitMask: 0 +; SUMMARY-NEXT: InlineBits: 0 +; SUMMARY-NEXT: WPDRes: +; SUMMARY-NEXT: 0: +; SUMMARY-NEXT: Kind: BranchFunnel +; SUMMARY-NEXT: SingleImplName: '' +; SUMMARY-NEXT: ResByArg: +; SUMMARY-NEXT: typeid2: +; SUMMARY-NEXT: TTRes: +; SUMMARY-NEXT: Kind: Unsat +; SUMMARY-NEXT: SizeM1BitWidth: 0 +; SUMMARY-NEXT: AlignLog2: 0 +; SUMMARY-NEXT: SizeM1: 0 +; SUMMARY-NEXT: BitMask: 0 +; SUMMARY-NEXT: InlineBits: 0 +; SUMMARY-NEXT: WPDRes: +; SUMMARY-NEXT: 0: +; SUMMARY-NEXT: Kind: Indir +; SUMMARY-NEXT: SingleImplName: '' +; SUMMARY-NEXT: ResByArg: + +target datalayout = "e-p:64:64" +target triple = "x86_64-unknown-linux-gnu" + +@vt1_1 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf1_1 to i8*)], !type !0 +@vt1_2 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf1_2 to i8*)], !type !0 + +declare i32 @vf1_1(i8* %this, i32 %arg) +declare i32 @vf1_2(i8* %this, i32 %arg) + +@vt2_1 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf2_1 to i8*)], !type !1 +@vt2_2 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf2_2 to i8*)], !type !1 +@vt2_3 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf2_3 to i8*)], !type !1 +@vt2_4 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf2_4 to i8*)], !type !1 +@vt2_5 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf2_5 to i8*)], !type !1 +@vt2_6 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf2_6 to i8*)], !type !1 +@vt2_7 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf2_7 to i8*)], !type !1 +@vt2_8 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf2_8 to i8*)], !type !1 +@vt2_9 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf2_9 to i8*)], !type !1 +@vt2_10 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf2_10 to i8*)], !type !1 +@vt2_11 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf2_11 to i8*)], !type !1 + +declare i32 @vf2_1(i8* %this, i32 %arg) +declare i32 @vf2_2(i8* %this, i32 %arg) +declare i32 @vf2_3(i8* %this, i32 %arg) +declare i32 @vf2_4(i8* %this, i32 %arg) +declare i32 @vf2_5(i8* %this, i32 %arg) +declare i32 @vf2_6(i8* %this, i32 %arg) +declare i32 @vf2_7(i8* %this, i32 %arg) +declare i32 @vf2_8(i8* %this, i32 %arg) +declare i32 @vf2_9(i8* %this, i32 %arg) +declare i32 @vf2_10(i8* %this, i32 %arg) +declare i32 @vf2_11(i8* %this, i32 %arg) + +@vt3_1 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf3_1 to i8*)], !type !2 +@vt3_2 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf3_2 to i8*)], !type !2 + +declare i32 @vf3_1(i8* %this, i32 %arg) +declare i32 @vf3_2(i8* %this, i32 %arg) + +@vt4_1 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf4_1 to i8*)], !type !3 +@vt4_2 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf4_2 to i8*)], !type !3 + +declare i32 @vf4_1(i8* %this, i32 %arg) +declare i32 @vf4_2(i8* %this, i32 %arg) + + + +; CHECK-LABEL: define i32 @fn1 +; CHECK-NOT: call void (...) @llvm.icall.branch.funnel +define i32 @fn1(i8* %obj) #0 { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid1") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to i32 (i8*, i32)* + ; RETP: {{.*}} = bitcast {{.*}} to i8* + ; RETP: [[VT1:%.*]] = bitcast {{.*}} to i8* + ; RETP: call i32 bitcast (void (i8*, ...)* @__typeid_typeid1_0_branch_funnel to i32 (i8*, i8*, i32)*)(i8* nest [[VT1]], i8* %obj, i32 1) + %result = call i32 %fptr_casted(i8* %obj, i32 1) + ; NORETP: call i32 % + ret i32 %result +} + +; CHECK-LABEL: define i32 @fn2 +; CHECK-NOT: call void (...) @llvm.icall.branch.funnel +define i32 @fn2(i8* %obj) #0 { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid2") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to i32 (i8*, i32)* + ; CHECK: call i32 % + %result = call i32 %fptr_casted(i8* %obj, i32 1) + ret i32 %result +} + +; CHECK-LABEL: define i32 @fn3 +; CHECK-NOT: call void (...) @llvm.icall.branch.funnel +define i32 @fn3(i8* %obj) #0 { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !4) + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to i32 (i8*, i32)* + ; RETP: call i32 bitcast (void (i8*, ...)* @branch_funnel to + ; NORETP: call i32 % + %result = call i32 %fptr_casted(i8* %obj, i32 1) + ret i32 %result +} + +; CHECK-LABEL: define internal void @branch_funnel(i8* +; CHECK: define hidden void @__typeid_typeid1_0_branch_funnel(i8* nest, ...) +; CHECK-NEXT: musttail call void (...) @llvm.icall.branch.funnel(i8* %0, i8* bitcast ([1 x i8*]* {{(nonnull )?}}@vt1_1 to i8*), i32 (i8*, i32)* {{(nonnull )?}}@vf1_1, i8* bitcast ([1 x i8*]* {{(nonnull )?}}@vt1_2 to i8*), i32 (i8*, i32)* {{(nonnull )?}}@vf1_2, ...) + +declare i1 @llvm.type.test(i8*, metadata) +declare void @llvm.assume(i1) + +!0 = !{i32 0, !"typeid1"} +!1 = !{i32 0, !"typeid2"} +!2 = !{i32 0, !"typeid3"} +!3 = !{i32 0, !4} +!4 = distinct !{} + +attributes #0 = { "target-features"="+retpoline" } diff --git a/llvm/test/Transforms/WholeProgramDevirt/constant-arg.ll b/llvm/test/Transforms/WholeProgramDevirt/constant-arg.ll new file mode 100644 index 00000000000..f65e4132738 --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/constant-arg.ll @@ -0,0 +1,77 @@ +; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s +; RUN: opt -S -passes=wholeprogramdevirt %s | FileCheck %s + +target datalayout = "e-p:64:64" +target triple = "x86_64-unknown-linux-gnu" + +; CHECK: private constant { [8 x i8], [1 x i8*], [0 x i8] } { [8 x i8] c"\00\00\00\00\00\00\00\01", [1 x i8*] [i8* bitcast (i1 (i8*, i32)* @vf1 to i8*)], [0 x i8] zeroinitializer }, !type [[T8:![0-9]+]] +; CHECK: private constant { [8 x i8], [1 x i8*], [0 x i8] } { [8 x i8] c"\00\00\00\00\00\00\00\02", [1 x i8*] [i8* bitcast (i1 (i8*, i32)* @vf2 to i8*)], [0 x i8] zeroinitializer }, !type [[T8]] +; CHECK: private constant { [8 x i8], [1 x i8*], [0 x i8] } { [8 x i8] c"\00\00\00\00\00\00\00\01", [1 x i8*] [i8* bitcast (i1 (i8*, i32)* @vf4 to i8*)], [0 x i8] zeroinitializer }, !type [[T8]] +; CHECK: private constant { [8 x i8], [1 x i8*], [0 x i8] } { [8 x i8] c"\00\00\00\00\00\00\00\02", [1 x i8*] [i8* bitcast (i1 (i8*, i32)* @vf8 to i8*)], [0 x i8] zeroinitializer }, !type [[T8]] + +@vt1 = constant [1 x i8*] [i8* bitcast (i1 (i8*, i32)* @vf1 to i8*)], !type !0 +@vt2 = constant [1 x i8*] [i8* bitcast (i1 (i8*, i32)* @vf2 to i8*)], !type !0 +@vt4 = constant [1 x i8*] [i8* bitcast (i1 (i8*, i32)* @vf4 to i8*)], !type !0 +@vt8 = constant [1 x i8*] [i8* bitcast (i1 (i8*, i32)* @vf8 to i8*)], !type !0 + +define i1 @vf1(i8* %this, i32 %arg) readnone { + %and = and i32 %arg, 1 + %cmp = icmp ne i32 %and, 0 + ret i1 %cmp +} + +define i1 @vf2(i8* %this, i32 %arg) readnone { + %and = and i32 %arg, 2 + %cmp = icmp ne i32 %and, 0 + ret i1 %cmp +} + +define i1 @vf4(i8* %this, i32 %arg) readnone { + %and = and i32 %arg, 4 + %cmp = icmp ne i32 %and, 0 + ret i1 %cmp +} + +define i1 @vf8(i8* %this, i32 %arg) readnone { + %and = and i32 %arg, 8 + %cmp = icmp ne i32 %and, 0 + ret i1 %cmp +} + +; CHECK: define i1 @call1 +define i1 @call1(i8* %obj) { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to i1 (i8*, i32)* + ; CHECK: getelementptr {{.*}} -1 + ; CHECK: and {{.*}}, 1 + %result = call i1 %fptr_casted(i8* %obj, i32 5) + ret i1 %result +} + +; CHECK: define i1 @call2 +define i1 @call2(i8* %obj) { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to i1 (i8*, i32)* + ; CHECK: getelementptr {{.*}} -1 + ; CHECK: and {{.*}}, 2 + %result = call i1 %fptr_casted(i8* %obj, i32 10) + ret i1 %result +} + +declare i1 @llvm.type.test(i8*, metadata) +declare void @llvm.assume(i1) + +; CHECK: [[T8]] = !{i32 8, !"typeid"} +!0 = !{i32 0, !"typeid"} diff --git a/llvm/test/Transforms/WholeProgramDevirt/devirt-single-impl-check.ll b/llvm/test/Transforms/WholeProgramDevirt/devirt-single-impl-check.ll new file mode 100644 index 00000000000..f4ef8824e2e --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/devirt-single-impl-check.ll @@ -0,0 +1,42 @@ +; RUN: opt -S -wholeprogramdevirt -pass-remarks=wholeprogramdevirt %s 2>&1 | FileCheck %s + +target datalayout = "e-p:64:64" +target triple = "x86_64-unknown-linux-gnu" + +; CHECK: remark: <unknown>:0:0: single-impl: devirtualized a call to vf +; CHECK: remark: <unknown>:0:0: devirtualized vf +; CHECK-NOT: devirtualized + +@vt1 = constant [1 x i8*] [i8* bitcast (void (i8*)* @vf to i8*)], !type !0 +@vt2 = constant [1 x i8*] [i8* bitcast (void (i8*)* @vf to i8*)], !type !0 + +define void @vf(i8* %this) { + ret void +} + +; CHECK: define void @call +define void @call(i8* %obj) { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %pair = call {i8*, i1} @llvm.type.checked.load(i8* %vtablei8, i32 0, metadata !"typeid") + %fptr = extractvalue {i8*, i1} %pair, 0 + %p = extractvalue {i8*, i1} %pair, 1 + ; CHECK: br i1 true, + br i1 %p, label %cont, label %trap + +cont: + %fptr_casted = bitcast i8* %fptr to void (i8*)* + ; CHECK: call void @vf( + call void %fptr_casted(i8* %obj) + ret void + +trap: + call void @llvm.trap() + unreachable +} + +declare {i8*, i1} @llvm.type.checked.load(i8*, i32, metadata) +declare void @llvm.trap() + +!0 = !{i32 0, !"typeid"} diff --git a/llvm/test/Transforms/WholeProgramDevirt/devirt-single-impl.ll b/llvm/test/Transforms/WholeProgramDevirt/devirt-single-impl.ll new file mode 100644 index 00000000000..9f631e94cf5 --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/devirt-single-impl.ll @@ -0,0 +1,47 @@ +; RUN: opt -S -wholeprogramdevirt -pass-remarks=wholeprogramdevirt %s 2>&1 | FileCheck %s + +target datalayout = "e-p:64:64" +target triple = "x86_64-unknown-linux-gnu" + +; CHECK: remark: devirt-single.cc:30:32: single-impl: devirtualized a call to vf +; CHECK: remark: devirt-single.cc:13:0: devirtualized vf +; CHECK-NOT: devirtualized + +@vt1 = constant [1 x i8*] [i8* bitcast (void (i8*)* @vf to i8*)], !type !8 +@vt2 = constant [1 x i8*] [i8* bitcast (void (i8*)* @vf to i8*)], !type !8 + +define void @vf(i8* %this) #0 !dbg !7 { + ret void +} + +; CHECK: define void @call +define void @call(i8* %obj) #1 !dbg !5 { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to void (i8*)* + ; CHECK: call void @vf( + call void %fptr_casted(i8* %obj), !dbg !6 + ret void +} + +declare i1 @llvm.type.test(i8*, metadata) +declare void @llvm.assume(i1) + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!2, !3} +!llvm.ident = !{!4} + +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 4.0.0 (trunk 278098)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug) +!1 = !DIFile(filename: "devirt-single.cc", directory: ".") +!2 = !{i32 2, !"Dwarf Version", i32 4} +!3 = !{i32 2, !"Debug Info Version", i32 3} +!4 = !{!"clang version 4.0.0 (trunk 278098)"} +!5 = distinct !DISubprogram(name: "call", linkageName: "_Z4callPv", scope: !1, file: !1, line: 29, isLocal: false, isDefinition: true, scopeLine: 9, flags: DIFlagPrototyped, isOptimized: false, unit: !0) +!6 = !DILocation(line: 30, column: 32, scope: !5) +!7 = distinct !DISubprogram(name: "vf", linkageName: "_ZN3vt12vfEv", scope: !1, file: !1, line: 13, isLocal: false, isDefinition: true, scopeLine: 13, flags: DIFlagPrototyped, isOptimized: false, unit: !0) +!8 = !{i32 0, !"typeid"} diff --git a/llvm/test/Transforms/WholeProgramDevirt/expand-check.ll b/llvm/test/Transforms/WholeProgramDevirt/expand-check.ll new file mode 100644 index 00000000000..4effaba08b2 --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/expand-check.ll @@ -0,0 +1,63 @@ +; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s + +; Test that we correctly expand the llvm.type.checked.load intrinsic in cases +; where we cannot devirtualize. + +target datalayout = "e-p:64:64" +target triple = "x86_64-unknown-linux-gnu" + +@vt1 = constant [1 x i8*] [i8* bitcast (void (i8*)* @vf1 to i8*)], !type !0 +@vt2 = constant [1 x i8*] [i8* bitcast (void (i8*)* @vf2 to i8*)], !type !0 + +define void @vf1(i8* %this) { + ret void +} + +define void @vf2(i8* %this) { + ret void +} + +; CHECK: define void @call +define void @call(i8* %obj) { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %pair = call {i8*, i1} @llvm.type.checked.load(i8* %vtablei8, i32 0, metadata !"typeid") + %p = extractvalue {i8*, i1} %pair, 1 + ; CHECK: [[TT:%[^ ]*]] = call i1 @llvm.type.test(i8* [[VT:%[^,]*]], metadata !"typeid") + ; CHECK: br i1 [[TT]], + br i1 %p, label %cont, label %trap + +cont: + ; CHECK: [[GEP:%[^ ]*]] = getelementptr i8, i8* [[VT]], i32 0 + ; CHECK: [[BC:%[^ ]*]] = bitcast i8* [[GEP]] to i8** + ; CHECK: [[LOAD:%[^ ]*]] = load i8*, i8** [[BC]] + ; CHECK: [[FPC:%[^ ]*]] = bitcast i8* [[LOAD]] to void (i8*)* + ; CHECK: call void [[FPC]] + %fptr = extractvalue {i8*, i1} %pair, 0 + %fptr_casted = bitcast i8* %fptr to void (i8*)* + call void %fptr_casted(i8* %obj) + ret void + +trap: + call void @llvm.trap() + unreachable +} + +; CHECK: define { i8*, i1 } @ret +define {i8*, i1} @ret(i8* %vtablei8) { + ; CHECK: [[GEP2:%[^ ]*]] = getelementptr i8, i8* [[VT2:%[^,]*]], i32 1 + ; CHECK: [[BC2:%[^ ]*]] = bitcast i8* [[GEP2]] to i8** + ; CHECK: [[LOAD2:%[^ ]*]] = load i8*, i8** [[BC2]] + ; CHECK: [[TT2:%[^ ]*]] = call i1 @llvm.type.test(i8* [[VT2]], metadata !"typeid") + ; CHECK: [[I1:%[^ ]*]] = insertvalue { i8*, i1 } undef, i8* [[LOAD2]], 0 + ; CHECK: [[I2:%[^ ]*]] = insertvalue { i8*, i1 } %5, i1 [[TT2]], 1 + %pair = call {i8*, i1} @llvm.type.checked.load(i8* %vtablei8, i32 1, metadata !"typeid") + ; CHECK: ret { i8*, i1 } [[I2]] + ret {i8*, i1} %pair +} + +declare {i8*, i1} @llvm.type.checked.load(i8*, i32, metadata) +declare void @llvm.trap() + +!0 = !{i32 0, !"typeid"} diff --git a/llvm/test/Transforms/WholeProgramDevirt/export-nothing.ll b/llvm/test/Transforms/WholeProgramDevirt/export-nothing.ll new file mode 100644 index 00000000000..4707eaa17ea --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/export-nothing.ll @@ -0,0 +1,8 @@ +; RUN: opt -wholeprogramdevirt -wholeprogramdevirt-summary-action=export -wholeprogramdevirt-write-summary=%t -o /dev/null %s +; RUN: FileCheck %s < %t + +; CHECK: --- +; CHECK-NEXT: GlobalValueMap: +; CHECK-NEXT: TypeIdMap: +; CHECK-NEXT: WithGlobalValueDeadStripping: false +; CHECK-NEXT: ... diff --git a/llvm/test/Transforms/WholeProgramDevirt/export-single-impl.ll b/llvm/test/Transforms/WholeProgramDevirt/export-single-impl.ll new file mode 100644 index 00000000000..e03933ccb26 --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/export-single-impl.ll @@ -0,0 +1,101 @@ +; RUN: opt -wholeprogramdevirt -wholeprogramdevirt-summary-action=export -wholeprogramdevirt-read-summary=%S/Inputs/export.yaml -wholeprogramdevirt-write-summary=%t -S -o - %s | FileCheck %s +; RUN: FileCheck --check-prefix=SUMMARY %s < %t + +; SUMMARY: TypeIdMap: +; SUMMARY-NEXT: typeid3: +; SUMMARY-NEXT: TTRes: +; SUMMARY-NEXT: Kind: Unsat +; SUMMARY-NEXT: SizeM1BitWidth: 0 +; SUMMARY-NEXT: AlignLog2: 0 +; SUMMARY-NEXT: SizeM1: 0 +; SUMMARY-NEXT: BitMask: 0 +; SUMMARY-NEXT: InlineBits: 0 +; SUMMARY-NEXT: WPDRes: +; SUMMARY-NEXT: 0: +; SUMMARY-NEXT: Kind: SingleImpl +; SUMMARY-NEXT: SingleImplName: 'vf3$merged' +; SUMMARY-NEXT: ResByArg: +; SUMMARY-NEXT: typeid1: +; SUMMARY-NEXT: TTRes: +; SUMMARY-NEXT: Kind: Unsat +; SUMMARY-NEXT: SizeM1BitWidth: 0 +; SUMMARY-NEXT: AlignLog2: 0 +; SUMMARY-NEXT: SizeM1: 0 +; SUMMARY-NEXT: BitMask: 0 +; SUMMARY-NEXT: InlineBits: 0 +; SUMMARY-NEXT: WPDRes: +; SUMMARY-NEXT: 0: +; SUMMARY-NEXT: Kind: SingleImpl +; SUMMARY-NEXT: SingleImplName: vf1 +; SUMMARY-NEXT: ResByArg: +; SUMMARY-NEXT: typeid2: +; SUMMARY-NEXT: TTRes: +; SUMMARY-NEXT: Kind: Unsat +; SUMMARY-NEXT: SizeM1BitWidth: 0 +; SUMMARY-NEXT: AlignLog2: 0 +; SUMMARY-NEXT: SizeM1: 0 +; SUMMARY-NEXT: BitMask: 0 +; SUMMARY-NEXT: InlineBits: 0 +; SUMMARY-NEXT: WPDRes: +; SUMMARY-NEXT: 0: +; SUMMARY-NEXT: Kind: SingleImpl +; SUMMARY-NEXT: SingleImplName: vf2 +; SUMMARY-NEXT: ResByArg: +; SUMMARY-NEXT: typeid4: +; SUMMARY-NEXT: TTRes: +; SUMMARY-NEXT: Kind: Unsat +; SUMMARY-NEXT: SizeM1BitWidth: 0 +; SUMMARY-NEXT: AlignLog2: 0 +; SUMMARY-NEXT: SizeM1: 0 +; SUMMARY-NEXT: BitMask: 0 +; SUMMARY-NEXT: InlineBits: 0 +; SUMMARY-NEXT: WPDRes: +; SUMMARY-NEXT: 0: +; SUMMARY-NEXT: Kind: SingleImpl +; SUMMARY-NEXT: SingleImplName: 'vf4$merged' +; SUMMARY-NEXT: ResByArg: +; SUMMARY-NEXT: WithGlobalValueDeadStripping: false +; SUMMARY-NEXT: ... + +; CHECK: $"vf4$merged" = comdat largest +$vf4 = comdat largest + +; CHECK: @vt1 = constant void (i8*)* @vf1 +@vt1 = constant void (i8*)* @vf1, !type !0 + +; CHECK: @vt2 = constant void (i8*)* @vf2 +@vt2 = constant void (i8*)* @vf2, !type !1 + +@vt3 = constant void (i8*)* @vf3, !type !2 + +; CHECK: @vt4 = constant void (i8*)* @"vf4$merged", comdat($"vf4$merged") +@vt4 = constant void (i8*)* @vf4, comdat($vf4), !type !3 + +@vt5 = constant void (i8*)* @vf5, !type !4 + +; CHECK: declare void @vf1(i8*) +declare void @vf1(i8*) + +; CHECK: define void @vf2(i8*) +define void @vf2(i8*) { + ret void +} + +; CHECK: define hidden void @"vf3$merged"(i8*) { +define internal void @vf3(i8*) { + ret void +} + +; CHECK: define hidden void @"vf4$merged"(i8*) comdat { +define internal void @vf4(i8*) comdat { + ret void +} + +declare void @vf5(i8*) + +!0 = !{i32 0, !"typeid1"} +!1 = !{i32 0, !"typeid2"} +!2 = !{i32 0, !"typeid3"} +!3 = !{i32 0, !"typeid4"} +!4 = !{i32 0, !5} +!5 = distinct !{} diff --git a/llvm/test/Transforms/WholeProgramDevirt/export-uniform-ret-val.ll b/llvm/test/Transforms/WholeProgramDevirt/export-uniform-ret-val.ll new file mode 100644 index 00000000000..43adb90d69f --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/export-uniform-ret-val.ll @@ -0,0 +1,41 @@ +; RUN: opt -wholeprogramdevirt -wholeprogramdevirt-summary-action=export -wholeprogramdevirt-read-summary=%S/Inputs/export.yaml -wholeprogramdevirt-write-summary=%t -S -o - %s | FileCheck %s +; RUN: FileCheck --check-prefix=SUMMARY %s < %t + +; SUMMARY-NOT: TypeTests: + +; SUMMARY: TypeIdMap: +; SUMMARY-NEXT: typeid4: +; SUMMARY-NEXT: TTRes: +; SUMMARY-NEXT: Kind: Unsat +; SUMMARY-NEXT: SizeM1BitWidth: 0 +; SUMMARY-NEXT: AlignLog2: 0 +; SUMMARY-NEXT: SizeM1: 0 +; SUMMARY-NEXT: BitMask: 0 +; SUMMARY-NEXT: InlineBits: 0 +; SUMMARY-NEXT: WPDRes: +; SUMMARY-NEXT: 0: +; SUMMARY-NEXT: Kind: Indir +; SUMMARY-NEXT: SingleImplName: '' +; SUMMARY-NEXT: ResByArg: +; SUMMARY-NEXT: 24,12: +; SUMMARY-NEXT: Kind: UniformRetVal +; SUMMARY-NEXT: Info: 36 +; SUMMARY-NEXT: Byte: 0 +; SUMMARY-NEXT: Bit: 0 + +; CHECK: @vt4a = constant i32 (i8*, i32, i32)* @vf4a +@vt4a = constant i32 (i8*, i32, i32)* @vf4a, !type !0 + +; CHECK: @vt4b = constant i32 (i8*, i32, i32)* @vf4b +@vt4b = constant i32 (i8*, i32, i32)* @vf4b, !type !0 + +define i32 @vf4a(i8*, i32 %x, i32 %y) { + %z = add i32 %x, %y + ret i32 %z +} + +define i32 @vf4b(i8*, i32 %x, i32 %y) { + ret i32 36 +} + +!0 = !{i32 0, !"typeid4"} diff --git a/llvm/test/Transforms/WholeProgramDevirt/export-unique-ret-val.ll b/llvm/test/Transforms/WholeProgramDevirt/export-unique-ret-val.ll new file mode 100644 index 00000000000..4260a2e570d --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/export-unique-ret-val.ll @@ -0,0 +1,90 @@ +; RUN: opt -wholeprogramdevirt -wholeprogramdevirt-summary-action=export -wholeprogramdevirt-read-summary=%S/Inputs/export.yaml -wholeprogramdevirt-write-summary=%t -S -o - %s | FileCheck %s +; RUN: FileCheck --check-prefix=SUMMARY %s < %t + +; SUMMARY-NOT: TypeTests: + +; SUMMARY: TypeIdMap: +; SUMMARY-NEXT: typeid3: +; SUMMARY-NEXT: TTRes: +; SUMMARY-NEXT: Kind: Unsat +; SUMMARY-NEXT: SizeM1BitWidth: 0 +; SUMMARY-NEXT: AlignLog2: 0 +; SUMMARY-NEXT: SizeM1: 0 +; SUMMARY-NEXT: BitMask: 0 +; SUMMARY-NEXT: InlineBits: 0 +; SUMMARY-NEXT: WPDRes: +; SUMMARY-NEXT: 0: +; SUMMARY-NEXT: Kind: Indir +; SUMMARY-NEXT: SingleImplName: '' +; SUMMARY-NEXT: ResByArg: +; SUMMARY-NEXT: 12,24: +; SUMMARY-NEXT: Kind: UniqueRetVal +; SUMMARY-NEXT: Info: 0 +; SUMMARY-NEXT: Byte: 0 +; SUMMARY-NEXT: Bit: 0 +; SUMMARY-NEXT: typeid4: +; SUMMARY-NEXT: TTRes: +; SUMMARY-NEXT: Kind: Unsat +; SUMMARY-NEXT: SizeM1BitWidth: 0 +; SUMMARY-NEXT: AlignLog2: 0 +; SUMMARY-NEXT: SizeM1: 0 +; SUMMARY-NEXT: BitMask: 0 +; SUMMARY-NEXT: InlineBits: 0 +; SUMMARY-NEXT: WPDRes: +; SUMMARY-NEXT: 0: +; SUMMARY-NEXT: Kind: Indir +; SUMMARY-NEXT: SingleImplName: '' +; SUMMARY-NEXT: ResByArg: +; SUMMARY-NEXT: 24,12: +; SUMMARY-NEXT: Kind: UniqueRetVal +; SUMMARY-NEXT: Info: 1 +; SUMMARY-NEXT: Byte: 0 +; SUMMARY-NEXT: Bit: 0 + +; CHECK: @vt3a = constant i1 (i8*, i32, i32)* @vf3a +@vt3a = constant i1 (i8*, i32, i32)* @vf3a, !type !0 + +; CHECK: @vt3b = constant i1 (i8*, i32, i32)* @vf3b +@vt3b = constant i1 (i8*, i32, i32)* @vf3b, !type !0 + +; CHECK: @vt3c = constant i1 (i8*, i32, i32)* @vf3c +@vt3c = constant i1 (i8*, i32, i32)* @vf3c, !type !0 + +; CHECK: @vt4a = constant i1 (i8*, i32, i32)* @vf4a +@vt4a = constant i1 (i8*, i32, i32)* @vf4a, !type !1 + +; CHECK: @vt4b = constant i1 (i8*, i32, i32)* @vf4b +@vt4b = constant i1 (i8*, i32, i32)* @vf4b, !type !1 + +; CHECK: @vt4c = constant i1 (i8*, i32, i32)* @vf4c +@vt4c = constant i1 (i8*, i32, i32)* @vf4c, !type !1 + +; CHECK: @__typeid_typeid3_0_12_24_unique_member = hidden alias i8, bitcast (i1 (i8*, i32, i32)** @vt3b to i8*) +; CHECK: @__typeid_typeid4_0_24_12_unique_member = hidden alias i8, bitcast (i1 (i8*, i32, i32)** @vt4b to i8*) + +define i1 @vf3a(i8*, i32, i32) { + ret i1 true +} + +define i1 @vf3b(i8*, i32, i32) { + ret i1 false +} + +define i1 @vf3c(i8*, i32, i32) { + ret i1 true +} + +define i1 @vf4a(i8*, i32, i32) { + ret i1 false +} + +define i1 @vf4b(i8*, i32, i32) { + ret i1 true +} + +define i1 @vf4c(i8*, i32, i32) { + ret i1 false +} + +!0 = !{i32 0, !"typeid3"} +!1 = !{i32 0, !"typeid4"} diff --git a/llvm/test/Transforms/WholeProgramDevirt/export-unsuccessful-checked.ll b/llvm/test/Transforms/WholeProgramDevirt/export-unsuccessful-checked.ll new file mode 100644 index 00000000000..3132444a9f3 --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/export-unsuccessful-checked.ll @@ -0,0 +1,28 @@ +; RUN: opt -wholeprogramdevirt -wholeprogramdevirt-summary-action=export -wholeprogramdevirt-read-summary=%S/Inputs/export.yaml -wholeprogramdevirt-write-summary=%t -o /dev/null %s +; RUN: FileCheck %s < %t + +; CHECK: TypeTests: [ 15427464259790519041, 17525413373118030901 ] +; CHECK-NEXT: TypeTestAssumeVCalls: + +@vt1a = constant void (i8*)* @vf1a, !type !0 +@vt1b = constant void (i8*)* @vf1b, !type !0 +@vt2a = constant void (i8*)* @vf2a, !type !1 +@vt2b = constant void (i8*)* @vf2b, !type !1 +@vt3a = constant void (i8*)* @vf3a, !type !2 +@vt3b = constant void (i8*)* @vf3b, !type !2 +@vt4a = constant void (i8*)* @vf4a, !type !3 +@vt4b = constant void (i8*)* @vf4b, !type !3 + +declare void @vf1a(i8*) +declare void @vf1b(i8*) +declare void @vf2a(i8*) +declare void @vf2b(i8*) +declare void @vf3a(i8*) +declare void @vf3b(i8*) +declare void @vf4a(i8*) +declare void @vf4b(i8*) + +!0 = !{i32 0, !"typeid1"} +!1 = !{i32 0, !"typeid2"} +!2 = !{i32 0, !"typeid3"} +!3 = !{i32 0, !"typeid4"} diff --git a/llvm/test/Transforms/WholeProgramDevirt/export-vcp.ll b/llvm/test/Transforms/WholeProgramDevirt/export-vcp.ll new file mode 100644 index 00000000000..5982ad4ec58 --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/export-vcp.ll @@ -0,0 +1,102 @@ +; RUN: opt -mtriple=x86_64-unknown-linux-gnu -wholeprogramdevirt -wholeprogramdevirt-summary-action=export -wholeprogramdevirt-read-summary=%S/Inputs/export.yaml -wholeprogramdevirt-write-summary=%t -S -o - %s | FileCheck --check-prefixes=CHECK,X86 %s +; RUN: FileCheck --check-prefixes=SUMMARY,SUMMARY-X86 %s < %t + +; RUN: opt -mtriple=armv7-unknown-linux-gnu -wholeprogramdevirt -wholeprogramdevirt-summary-action=export -wholeprogramdevirt-read-summary=%S/Inputs/export.yaml -wholeprogramdevirt-write-summary=%t -S -o - %s | FileCheck --check-prefixes=CHECK,ARM %s +; RUN: FileCheck --check-prefixes=SUMMARY,SUMMARY-ARM %s < %t + +target datalayout = "e-p:64:64" + +; SUMMARY: TypeIdMap: +; SUMMARY-NEXT: typeid3: +; SUMMARY-NEXT: TTRes: +; SUMMARY-NEXT: Kind: Unsat +; SUMMARY-NEXT: SizeM1BitWidth: 0 +; SUMMARY-NEXT: AlignLog2: 0 +; SUMMARY-NEXT: SizeM1: 0 +; SUMMARY-NEXT: BitMask: 0 +; SUMMARY-NEXT: InlineBits: 0 +; SUMMARY-NEXT: WPDRes: +; SUMMARY-NEXT: 0: +; SUMMARY-NEXT: Kind: Indir +; SUMMARY-NEXT: SingleImplName: '' +; SUMMARY-NEXT: ResByArg: +; SUMMARY-NEXT: 12,24: +; SUMMARY-NEXT: Kind: VirtualConstProp +; SUMMARY-NEXT: Info: 0 +; SUMMARY-X86-NEXT: Byte: 0 +; SUMMARY-X86-NEXT: Bit: 0 +; SUMMARY-ARM-NEXT: Byte: 4294967295 +; SUMMARY-ARM-NEXT: Bit: 1 +; SUMMARY-NEXT: typeid4: +; SUMMARY-NEXT: TTRes: +; SUMMARY-NEXT: Kind: Unsat +; SUMMARY-NEXT: SizeM1BitWidth: 0 +; SUMMARY-NEXT: AlignLog2: 0 +; SUMMARY-NEXT: SizeM1: 0 +; SUMMARY-NEXT: BitMask: 0 +; SUMMARY-NEXT: InlineBits: 0 +; SUMMARY-NEXT: WPDRes: +; SUMMARY-NEXT: 0: +; SUMMARY-NEXT: Kind: Indir +; SUMMARY-NEXT: SingleImplName: '' +; SUMMARY-NEXT: ResByArg: +; SUMMARY-NEXT: 24,12: +; SUMMARY-NEXT: Kind: VirtualConstProp +; SUMMARY-NEXT: Info: 0 +; SUMMARY-X86-NEXT: Byte: 0 +; SUMMARY-X86-NEXT: Bit: 0 +; SUMMARY-ARM-NEXT: Byte: 4294967292 +; SUMMARY-ARM-NEXT: Bit: 1 + +; CHECK: [[CVT3A:.*]] = private constant { [8 x i8], i1 (i8*, i32, i32)*, [0 x i8] } { [8 x i8] zeroinitializer, i1 (i8*, i32, i32)* @vf0i1, [0 x i8] zeroinitializer }, !type !0 +@vt3a = constant i1 (i8*, i32, i32)* @vf0i1, !type !0 + +; CHECK: [[CVT3B:.*]] = private constant { [8 x i8], i1 (i8*, i32, i32)*, [0 x i8] } { [8 x i8] c"\00\00\00\00\00\00\00\01", i1 (i8*, i32, i32)* @vf1i1, [0 x i8] zeroinitializer }, !type !0 +@vt3b = constant i1 (i8*, i32, i32)* @vf1i1, !type !0 + +; CHECK: [[CVT3C:.*]] = private constant { [8 x i8], i1 (i8*, i32, i32)*, [0 x i8] } { [8 x i8] zeroinitializer, i1 (i8*, i32, i32)* @vf0i1, [0 x i8] zeroinitializer }, !type !0 +@vt3c = constant i1 (i8*, i32, i32)* @vf0i1, !type !0 + +; CHECK: [[CVT3D:.*]] = private constant { [8 x i8], i1 (i8*, i32, i32)*, [0 x i8] } { [8 x i8] c"\00\00\00\00\00\00\00\01", i1 (i8*, i32, i32)* @vf1i1, [0 x i8] zeroinitializer }, !type !0 +@vt3d = constant i1 (i8*, i32, i32)* @vf1i1, !type !0 + +; CHECK: [[CVT4A:.*]] = private constant { [8 x i8], i32 (i8*, i32, i32)*, [0 x i8] } { [8 x i8] c"\00\00\00\00\01\00\00\00", i32 (i8*, i32, i32)* @vf1i32, [0 x i8] zeroinitializer }, !type !1 +@vt4a = constant i32 (i8*, i32, i32)* @vf1i32, !type !1 + +; CHECK: [[CVT4B:.*]] = private constant { [8 x i8], i32 (i8*, i32, i32)*, [0 x i8] } { [8 x i8] c"\00\00\00\00\02\00\00\00", i32 (i8*, i32, i32)* @vf2i32, [0 x i8] zeroinitializer }, !type !1 +@vt4b = constant i32 (i8*, i32, i32)* @vf2i32, !type !1 + +; X86: @__typeid_typeid3_0_12_24_byte = hidden alias i8, inttoptr (i32 -1 to i8*) +; X86: @__typeid_typeid3_0_12_24_bit = hidden alias i8, inttoptr (i32 1 to i8*) +; X86: @__typeid_typeid4_0_24_12_byte = hidden alias i8, inttoptr (i32 -4 to i8*) +; X86: @__typeid_typeid4_0_24_12_bit = hidden alias i8, inttoptr (i32 1 to i8*) +; ARM-NOT: alias {{.*}} inttoptr + +; CHECK: @vt3a = alias i1 (i8*, i32, i32)*, getelementptr inbounds ({ [8 x i8], i1 (i8*, i32, i32)*, [0 x i8] }, { [8 x i8], i1 (i8*, i32, i32)*, [0 x i8] }* [[CVT3A]], i32 0, i32 1) +; CHECK: @vt3b = alias i1 (i8*, i32, i32)*, getelementptr inbounds ({ [8 x i8], i1 (i8*, i32, i32)*, [0 x i8] }, { [8 x i8], i1 (i8*, i32, i32)*, [0 x i8] }* [[CVT3B]], i32 0, i32 1) +; CHECK: @vt3c = alias i1 (i8*, i32, i32)*, getelementptr inbounds ({ [8 x i8], i1 (i8*, i32, i32)*, [0 x i8] }, { [8 x i8], i1 (i8*, i32, i32)*, [0 x i8] }* [[CVT3C]], i32 0, i32 1) +; CHECK: @vt3d = alias i1 (i8*, i32, i32)*, getelementptr inbounds ({ [8 x i8], i1 (i8*, i32, i32)*, [0 x i8] }, { [8 x i8], i1 (i8*, i32, i32)*, [0 x i8] }* [[CVT3D]], i32 0, i32 1) +; CHECK: @vt4a = alias i32 (i8*, i32, i32)*, getelementptr inbounds ({ [8 x i8], i32 (i8*, i32, i32)*, [0 x i8] }, { [8 x i8], i32 (i8*, i32, i32)*, [0 x i8] }* [[CVT4A]], i32 0, i32 1) +; CHECK: @vt4b = alias i32 (i8*, i32, i32)*, getelementptr inbounds ({ [8 x i8], i32 (i8*, i32, i32)*, [0 x i8] }, { [8 x i8], i32 (i8*, i32, i32)*, [0 x i8] }* [[CVT4B]], i32 0, i32 1) + +define i1 @vf0i1(i8* %this, i32, i32) readnone { + ret i1 0 +} + +define i1 @vf1i1(i8* %this, i32, i32) readnone { + ret i1 1 +} + +define i32 @vf1i32(i8* %this, i32, i32) readnone { + ret i32 1 +} + +define i32 @vf2i32(i8* %this, i32, i32) readnone { + ret i32 2 +} + +; CHECK: !0 = !{i32 8, !"typeid3"} +; CHECK: !1 = !{i32 8, !"typeid4"} + +!0 = !{i32 0, !"typeid3"} +!1 = !{i32 0, !"typeid4"} diff --git a/llvm/test/Transforms/WholeProgramDevirt/import-indir.ll b/llvm/test/Transforms/WholeProgramDevirt/import-indir.ll new file mode 100644 index 00000000000..927ee16b370 --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/import-indir.ll @@ -0,0 +1,108 @@ +; Test that we correctly import an indir resolution for type identifier "typeid1". +; RUN: opt -S -wholeprogramdevirt -wholeprogramdevirt-summary-action=import -wholeprogramdevirt-read-summary=%S/Inputs/import-indir.yaml -wholeprogramdevirt-write-summary=%t < %s | FileCheck %s +; RUN: FileCheck --check-prefix=SUMMARY %s < %t + +; SUMMARY: GlobalValueMap: +; SUMMARY-NEXT: 42: +; SUMMARY-NEXT: - Linkage: 0 +; SUMMARY-NEXT: NotEligibleToImport: false +; SUMMARY-NEXT: Live: true +; SUMMARY-NEXT: Local: false +; SUMMARY-NEXT: TypeTestAssumeVCalls: +; SUMMARY-NEXT: - GUID: 123 +; SUMMARY-NEXT: Offset: 0 +; SUMMARY-NEXT: - GUID: 456 +; SUMMARY-NEXT: Offset: 4 +; SUMMARY-NEXT: TypeCheckedLoadVCalls: +; SUMMARY-NEXT: - GUID: 789 +; SUMMARY-NEXT: Offset: 8 +; SUMMARY-NEXT: - GUID: 1234 +; SUMMARY-NEXT: Offset: 16 +; SUMMARY-NEXT: TypeTestAssumeConstVCalls: +; SUMMARY-NEXT: - VFunc: +; SUMMARY-NEXT: GUID: 123 +; SUMMARY-NEXT: Offset: 4 +; SUMMARY-NEXT: Args: [ 12, 24 ] +; SUMMARY-NEXT: TypeCheckedLoadConstVCalls: +; SUMMARY-NEXT: - VFunc: +; SUMMARY-NEXT: GUID: 456 +; SUMMARY-NEXT: Offset: 8 +; SUMMARY-NEXT: Args: [ 24, 12 ] +; SUMMARY-NEXT: TypeIdMap: +; SUMMARY-NEXT: typeid1: +; SUMMARY-NEXT: TTRes: +; SUMMARY-NEXT: Kind: Unsat +; SUMMARY-NEXT: SizeM1BitWidth: 0 +; SUMMARY-NEXT: AlignLog2: 0 +; SUMMARY-NEXT: SizeM1: 0 +; SUMMARY-NEXT: BitMask: 0 +; SUMMARY-NEXT: InlineBits: 0 +; SUMMARY-NEXT: WPDRes: +; SUMMARY-NEXT: 0: +; SUMMARY-NEXT: Kind: Indir +; SUMMARY-NEXT: SingleImplName: '' +; SUMMARY-NEXT: ResByArg: +; SUMMARY-NEXT: 4: +; SUMMARY-NEXT: Kind: Indir +; SUMMARY-NEXT: SingleImplName: '' +; SUMMARY-NEXT: ResByArg: +; SUMMARY-NEXT: : +; SUMMARY-NEXT: Kind: UniformRetVal +; SUMMARY-NEXT: Info: 12 +; SUMMARY-NEXT: Byte: 0 +; SUMMARY-NEXT: Bit: 0 +; SUMMARY-NEXT: 12: +; SUMMARY-NEXT: Kind: UniformRetVal +; SUMMARY-NEXT: Info: 24 +; SUMMARY-NEXT: Byte: 0 +; SUMMARY-NEXT: Bit: 0 +; SUMMARY-NEXT: 12,24: +; SUMMARY-NEXT: Kind: UniformRetVal +; SUMMARY-NEXT: Info: 48 +; SUMMARY-NEXT: Byte: 0 +; SUMMARY-NEXT: Bit: 0 + +target datalayout = "e-p:32:32" + +declare void @llvm.assume(i1) +declare void @llvm.trap() +declare {i8*, i1} @llvm.type.checked.load(i8*, i32, metadata) +declare i1 @llvm.type.test(i8*, metadata) + +; CHECK: define i1 @f1 +define i1 @f1(i8* %obj) { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid1") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to i1 (i8*, i32)* + ; CHECK: call i1 % + %result = call i1 %fptr_casted(i8* %obj, i32 5) + ret i1 %result +} + +; CHECK: define i1 @f2 +define i1 @f2(i8* %obj) { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %pair = call {i8*, i1} @llvm.type.checked.load(i8* %vtablei8, i32 4, metadata !"typeid1") + %fptr = extractvalue {i8*, i1} %pair, 0 + %p = extractvalue {i8*, i1} %pair, 1 + ; CHECK: [[P:%.*]] = call i1 @llvm.type.test + ; CHECK: br i1 [[P]] + br i1 %p, label %cont, label %trap + +cont: + %fptr_casted = bitcast i8* %fptr to i1 (i8*, i32)* + ; CHECK: call i1 % + %result = call i1 %fptr_casted(i8* %obj, i32 undef) + ret i1 %result + +trap: + call void @llvm.trap() + unreachable +} diff --git a/llvm/test/Transforms/WholeProgramDevirt/import-no-dominating-assume.ll b/llvm/test/Transforms/WholeProgramDevirt/import-no-dominating-assume.ll new file mode 100644 index 00000000000..b652ea4afe7 --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/import-no-dominating-assume.ll @@ -0,0 +1,37 @@ +; RUN: opt -S -wholeprogramdevirt -wholeprogramdevirt-summary-action=import -wholeprogramdevirt-read-summary=%S/Inputs/import-vcp.yaml < %s | FileCheck %s + +target datalayout = "e-p:64:64" +target triple = "x86_64-unknown-linux-gnu" + +define i32 @call1(i1 %a, i8* %obj) { + %vtableptr = bitcast i8* %obj to [3 x i8*]** + %vtable = load [3 x i8*]*, [3 x i8*]** %vtableptr + br i1 %a, label %bb1, label %bb2 + +bb1: + ; CHECK: {{.*}} = bitcast {{.*}} to i8* + %vtablei8_1 = bitcast [3 x i8*]* %vtable to i8* + %p1 = call i1 @llvm.type.test(i8* %vtablei8_1, metadata !"typeid1") + call void @llvm.assume(i1 %p1) + %fptrptr1 = getelementptr [3 x i8*], [3 x i8*]* %vtable, i32 0, i32 0 + %fptr1 = load i8*, i8** %fptrptr1 + %fptr1_casted = bitcast i8* %fptr1 to i32 (i8*, i32)* + ; CHECK: {{.*}} = bitcast {{.*}} to i8* + %result1 = call i32 %fptr1_casted(i8* %obj, i32 1) + br label %bb2 + + ; CHECK: : +bb2: + %vtablei8_2 = bitcast [3 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8_2, metadata !"typeid1") + call void @llvm.assume(i1 %p) + %fptrptr2 = getelementptr [3 x i8*], [3 x i8*]* %vtable, i32 0, i32 0 + %fptr2 = load i8*, i8** %fptrptr2 + %fptr2_casted = bitcast i8* %fptr2 to i32 (i8*, i32)* + ; CHECK: {{.*}} = bitcast {{.*}} to i8* + %result2 = call i32 %fptr2_casted(i8* %obj, i32 1) + ret i32 %result2 +} + +declare void @llvm.assume(i1) +declare i1 @llvm.type.test(i8*, metadata) diff --git a/llvm/test/Transforms/WholeProgramDevirt/import.ll b/llvm/test/Transforms/WholeProgramDevirt/import.ll new file mode 100644 index 00000000000..525d88cb662 --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/import.ll @@ -0,0 +1,121 @@ +; RUN: opt -S -wholeprogramdevirt -wholeprogramdevirt-summary-action=import -wholeprogramdevirt-read-summary=%S/Inputs/import-single-impl.yaml < %s | FileCheck --check-prefixes=CHECK,SINGLE-IMPL %s +; RUN: opt -S -wholeprogramdevirt -wholeprogramdevirt-summary-action=import -wholeprogramdevirt-read-summary=%S/Inputs/import-uniform-ret-val.yaml < %s | FileCheck --check-prefixes=CHECK,INDIR,UNIFORM-RET-VAL %s +; RUN: opt -S -wholeprogramdevirt -wholeprogramdevirt-summary-action=import -wholeprogramdevirt-read-summary=%S/Inputs/import-unique-ret-val0.yaml < %s | FileCheck --check-prefixes=CHECK,INDIR,UNIQUE-RET-VAL0 %s +; RUN: opt -S -wholeprogramdevirt -wholeprogramdevirt-summary-action=import -wholeprogramdevirt-read-summary=%S/Inputs/import-unique-ret-val1.yaml < %s | FileCheck --check-prefixes=CHECK,INDIR,UNIQUE-RET-VAL1 %s +; RUN: opt -S -wholeprogramdevirt -wholeprogramdevirt-summary-action=import -wholeprogramdevirt-read-summary=%S/Inputs/import-vcp.yaml < %s | FileCheck --check-prefixes=CHECK,VCP,VCP-X86,VCP64,INDIR %s +; RUN: opt -S -wholeprogramdevirt -wholeprogramdevirt-summary-action=import -wholeprogramdevirt-read-summary=%S/Inputs/import-vcp.yaml -mtriple=i686-unknown-linux -data-layout=e-p:32:32 < %s | FileCheck --check-prefixes=CHECK,VCP,VCP-X86,VCP32 %s +; RUN: opt -S -wholeprogramdevirt -wholeprogramdevirt-summary-action=import -wholeprogramdevirt-read-summary=%S/Inputs/import-vcp.yaml -mtriple=armv7-unknown-linux -data-layout=e-p:32:32 < %s | FileCheck --check-prefixes=CHECK,VCP,VCP-ARM %s +; RUN: opt -S -wholeprogramdevirt -wholeprogramdevirt-summary-action=import -wholeprogramdevirt-read-summary=%S/Inputs/import-vcp-branch-funnel.yaml < %s | FileCheck --check-prefixes=CHECK,VCP,VCP-X86,VCP64,BRANCH-FUNNEL %s +; RUN: opt -S -wholeprogramdevirt -wholeprogramdevirt-summary-action=import -wholeprogramdevirt-read-summary=%S/Inputs/import-branch-funnel.yaml < %s | FileCheck --check-prefixes=CHECK,BRANCH-FUNNEL,BRANCH-FUNNEL-NOVCP %s + +target datalayout = "e-p:64:64" +target triple = "x86_64-unknown-linux-gnu" + +; VCP-X86: @__typeid_typeid1_0_1_byte = external hidden global i8, !absolute_symbol !0 +; VCP-X86: @__typeid_typeid1_0_1_bit = external hidden global i8, !absolute_symbol !1 +; VCP-X86: @__typeid_typeid2_8_3_byte = external hidden global i8, !absolute_symbol !0 +; VCP-X86: @__typeid_typeid2_8_3_bit = external hidden global i8, !absolute_symbol !1 + +; Test cases where the argument values are known and we can apply virtual +; constant propagation. + +; CHECK: define i32 @call1 +define i32 @call1(i8* %obj) #0 { + %vtableptr = bitcast i8* %obj to [3 x i8*]** + %vtable = load [3 x i8*]*, [3 x i8*]** %vtableptr + %vtablei8 = bitcast [3 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid1") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [3 x i8*], [3 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to i32 (i8*, i32)* + ; CHECK: {{.*}} = bitcast {{.*}} to i8* + ; VCP: [[VT1:%.*]] = bitcast {{.*}} to i8* + ; SINGLE-IMPL: call i32 bitcast (void ()* @singleimpl1 to i32 (i8*, i32)*) + %result = call i32 %fptr_casted(i8* %obj, i32 1) + ; UNIFORM-RET-VAL: ret i32 42 + ; VCP-X86: [[GEP1:%.*]] = getelementptr i8, i8* [[VT1]], i32 ptrtoint (i8* @__typeid_typeid1_0_1_byte to i32) + ; VCP-ARM: [[GEP1:%.*]] = getelementptr i8, i8* [[VT1]], i32 42 + ; VCP: [[BC1:%.*]] = bitcast i8* [[GEP1]] to i32* + ; VCP: [[LOAD1:%.*]] = load i32, i32* [[BC1]] + ; VCP: ret i32 [[LOAD1]] + ; BRANCH-FUNNEL-NOVCP: [[VT1:%.*]] = bitcast {{.*}} to i8* + ; BRANCH-FUNNEL-NOVCP: call i32 bitcast (void ()* @__typeid_typeid1_0_branch_funnel to i32 (i8*, i8*, i32)*)(i8* nest [[VT1]], i8* %obj, i32 1) + ret i32 %result +} + +; Test cases where the argument values are unknown, so we cannot apply virtual +; constant propagation. + +; CHECK: define i1 @call2 +define i1 @call2(i8* %obj) #0 { + ; BRANCH-FUNNEL: [[VT1:%.*]] = bitcast {{.*}} to i8* + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %pair = call {i8*, i1} @llvm.type.checked.load(i8* %vtablei8, i32 8, metadata !"typeid2") + %fptr = extractvalue {i8*, i1} %pair, 0 + %p = extractvalue {i8*, i1} %pair, 1 + ; SINGLE-IMPL: br i1 true, + br i1 %p, label %cont, label %trap + +cont: + %fptr_casted = bitcast i8* %fptr to i1 (i8*, i32)* + ; SINGLE-IMPL: call i1 bitcast (void ()* @singleimpl2 to i1 (i8*, i32)*) + ; INDIR: call i1 % + ; BRANCH-FUNNEL: call i1 bitcast (void ()* @__typeid_typeid2_8_branch_funnel to i1 (i8*, i8*, i32)*)(i8* nest [[VT1]], i8* %obj, i32 undef) + %result = call i1 %fptr_casted(i8* %obj, i32 undef) + ret i1 %result + +trap: + call void @llvm.trap() + unreachable +} + +; CHECK: define i1 @call3 +define i1 @call3(i8* %obj) #0 { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %pair = call {i8*, i1} @llvm.type.checked.load(i8* %vtablei8, i32 8, metadata !"typeid2") + %fptr = extractvalue {i8*, i1} %pair, 0 + %p = extractvalue {i8*, i1} %pair, 1 + br i1 %p, label %cont, label %trap + +cont: + %fptr_casted = bitcast i8* %fptr to i1 (i8*, i32)* + %result = call i1 %fptr_casted(i8* %obj, i32 3) + ; UNIQUE-RET-VAL0: icmp ne i8* %vtablei8, @__typeid_typeid2_8_3_unique_member + ; UNIQUE-RET-VAL1: icmp eq i8* %vtablei8, @__typeid_typeid2_8_3_unique_member + ; VCP: [[VT2:%.*]] = bitcast {{.*}} to i8* + ; VCP-X86: [[GEP2:%.*]] = getelementptr i8, i8* [[VT2]], i32 ptrtoint (i8* @__typeid_typeid2_8_3_byte to i32) + ; VCP-ARM: [[GEP2:%.*]] = getelementptr i8, i8* [[VT2]], i32 43 + ; VCP: [[LOAD2:%.*]] = load i8, i8* [[GEP2]] + ; VCP-X86: [[AND2:%.*]] = and i8 [[LOAD2]], ptrtoint (i8* @__typeid_typeid2_8_3_bit to i8) + ; VCP-ARM: [[AND2:%.*]] = and i8 [[LOAD2]], -128 + ; VCP: [[ICMP2:%.*]] = icmp ne i8 [[AND2]], 0 + ; VCP: ret i1 [[ICMP2]] + ; BRANCH-FUNNEL-NOVCP: [[VT2:%.*]] = bitcast {{.*}} to i8* + ; BRANCH-FUNNEL-NOVCP: call i1 bitcast (void ()* @__typeid_typeid2_8_branch_funnel to i1 (i8*, i8*, i32)*)(i8* nest [[VT2]], i8* %obj, i32 3) + ret i1 %result + +trap: + call void @llvm.trap() + unreachable +} + +; SINGLE-IMPL-DAG: declare void @singleimpl1() +; SINGLE-IMPL-DAG: declare void @singleimpl2() + +; VCP32: !0 = !{i32 -1, i32 -1} +; VCP64: !0 = !{i64 0, i64 4294967296} + +; VCP32: !1 = !{i32 0, i32 256} +; VCP64: !1 = !{i64 0, i64 256} + +declare void @llvm.assume(i1) +declare void @llvm.trap() +declare {i8*, i1} @llvm.type.checked.load(i8*, i32, metadata) +declare i1 @llvm.type.test(i8*, metadata) + +attributes #0 = { "target-features"="+retpoline" } diff --git a/llvm/test/Transforms/WholeProgramDevirt/non-constant-vtable.ll b/llvm/test/Transforms/WholeProgramDevirt/non-constant-vtable.ll new file mode 100644 index 00000000000..ecc8ad0e7c7 --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/non-constant-vtable.ll @@ -0,0 +1,32 @@ +; RUN: opt -S -wholeprogramdevirt -pass-remarks=wholeprogramdevirt %s 2>&1 | FileCheck %s + +; CHECK-NOT: devirtualized call + +target datalayout = "e-p:64:64" +target triple = "x86_64-unknown-linux-gnu" + +@vt = global [1 x i8*] [i8* bitcast (void (i8*)* @vf to i8*)], !type !0 + +define void @vf(i8* %this) { + ret void +} + +; CHECK: define void @call +define void @call(i8* %obj) { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to void (i8*)* + ; CHECK: call void % + call void %fptr_casted(i8* %obj) + ret void +} + +declare i1 @llvm.type.test(i8*, metadata) +declare void @llvm.assume(i1) + +!0 = !{i32 0, !"typeid"} diff --git a/llvm/test/Transforms/WholeProgramDevirt/pointer-vtable.ll b/llvm/test/Transforms/WholeProgramDevirt/pointer-vtable.ll new file mode 100644 index 00000000000..5e76a5a7dde --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/pointer-vtable.ll @@ -0,0 +1,30 @@ +; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s + +target datalayout = "e-p:64:64" +target triple = "x86_64-unknown-linux-gnu" + +@vt = constant i8* bitcast (void (i8*)* @vf to i8*), !type !0 + +define void @vf(i8* %this) { + ret void +} + +; CHECK: define void @call +define void @call(i8* %obj) { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to void (i8*)* + ; CHECK: call void @vf( + call void %fptr_casted(i8* %obj) + ret void +} + +declare i1 @llvm.type.test(i8*, metadata) +declare void @llvm.assume(i1) + +!0 = !{i32 0, !"typeid"} diff --git a/llvm/test/Transforms/WholeProgramDevirt/soa-vtable.ll b/llvm/test/Transforms/WholeProgramDevirt/soa-vtable.ll new file mode 100644 index 00000000000..3b6afc52e5d --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/soa-vtable.ll @@ -0,0 +1,52 @@ +; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s + +target datalayout = "e-p:64:64" +target triple = "x86_64-unknown-linux-gnu" + +%vtTy = type { [2 x void (i8*)*], [2 x void (i8*)*] } + +@vt = constant %vtTy { [2 x void (i8*)*] [void (i8*)* null, void (i8*)* @vf1], [2 x void (i8*)*] [void (i8*)* null, void (i8*)* @vf2] }, !type !0, !type !1 + +define void @vf1(i8* %this) { + ret void +} + +define void @vf2(i8* %this) { + ret void +} + +; CHECK: define void @call1 +define void @call1(i8* %obj) { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid1") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to void (i8*)* + ; CHECK: call void @vf1( + call void %fptr_casted(i8* %obj) + ret void +} + +; CHECK: define void @call2 +define void @call2(i8* %obj) { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid2") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to void (i8*)* + ; CHECK: call void @vf2( + call void %fptr_casted(i8* %obj) + ret void +} + +declare i1 @llvm.type.test(i8*, metadata) +declare void @llvm.assume(i1) + +!0 = !{i32 8, !"typeid1"} +!1 = !{i32 24, !"typeid2"} diff --git a/llvm/test/Transforms/WholeProgramDevirt/struct-vtable.ll b/llvm/test/Transforms/WholeProgramDevirt/struct-vtable.ll new file mode 100644 index 00000000000..81e41d46694 --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/struct-vtable.ll @@ -0,0 +1,63 @@ +; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s + +target datalayout = "e-p:64:64" +target triple = "x86_64-unknown-linux-gnu" + +%vtTy = type { void (i8*)* } + +@vt = constant %vtTy { void (i8*)* @vf }, !type !0 + +define void @vf(i8* %this) { + ret void +} + +; CHECK: define void @call +define void @call(i8* %obj) { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to void (i8*)* + ; CHECK: call void @vf( + call void %fptr_casted(i8* %obj) + ret void +} + +; CHECK: define void @call_oob +define void @call_oob(i8* %obj) { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 4 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to void (i8*)* + ; CHECK: call void % + call void %fptr_casted(i8* %obj) + ret void +} + +; CHECK: define void @call_unaligned +define void @call_unaligned(i8* %obj) { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr i8, i8* %vtablei8, i32 1 + %fptrptr_casted = bitcast i8* %fptrptr to i8** + %fptr = load i8*, i8** %fptrptr_casted + %fptr_casted = bitcast i8* %fptr to void (i8*)* + ; CHECK: call void % + call void %fptr_casted(i8* %obj) + ret void +} + +declare i1 @llvm.type.test(i8*, metadata) +declare void @llvm.assume(i1) + +!0 = !{i32 0, !"typeid"} diff --git a/llvm/test/Transforms/WholeProgramDevirt/uniform-retval-invoke.ll b/llvm/test/Transforms/WholeProgramDevirt/uniform-retval-invoke.ll new file mode 100644 index 00000000000..8fea9bc7b24 --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/uniform-retval-invoke.ll @@ -0,0 +1,43 @@ +; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s + +target datalayout = "e-p:64:64" +target triple = "x86_64-unknown-linux-gnu" + +@vt1 = constant [1 x i8*] [i8* bitcast (i32 (i8*)* @vf1 to i8*)], !type !0 +@vt2 = constant [1 x i8*] [i8* bitcast (i32 (i8*)* @vf2 to i8*)], !type !0 + +define i32 @vf1(i8* %this) readnone { + ret i32 123 +} + +define i32 @vf2(i8* %this) readnone { + ret i32 123 +} + +; CHECK: define i32 @call +define i32 @call(i8* %obj) personality i8* undef { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to i32 (i8*)* + ; CHECK: br label %[[RET:[0-9A-Za-z]*]] + %result = invoke i32 %fptr_casted(i8* %obj) to label %ret unwind label %unwind + +unwind: + %x = landingpad i32 cleanup + unreachable + +ret: + ; CHECK: [[RET]]: + ; CHECK-NEXT: ret i32 123 + ret i32 %result +} + +declare i1 @llvm.type.test(i8*, metadata) +declare void @llvm.assume(i1) + +!0 = !{i32 0, !"typeid"} diff --git a/llvm/test/Transforms/WholeProgramDevirt/uniform-retval.ll b/llvm/test/Transforms/WholeProgramDevirt/uniform-retval.ll new file mode 100644 index 00000000000..ef3a7e49b52 --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/uniform-retval.ll @@ -0,0 +1,36 @@ +; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s + +target datalayout = "e-p:64:64" +target triple = "x86_64-unknown-linux-gnu" + +@vt1 = constant [1 x i8*] [i8* bitcast (i32 (i8*)* @vf1 to i8*)], !type !0 +@vt2 = constant [1 x i8*] [i8* bitcast (i32 (i8*)* @vf2 to i8*)], !type !0 + +define i32 @vf1(i8* %this) readnone { + ret i32 123 +} + +define i32 @vf2(i8* %this) readnone { + ret i32 123 +} + +; CHECK: define i32 @call +define i32 @call(i8* %obj) { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to i32 (i8*)* + %result = call i32 %fptr_casted(i8* %obj) + ; CHECK-NOT: call + ; CHECK: ret i32 123 + ret i32 %result +} + +declare i1 @llvm.type.test(i8*, metadata) +declare void @llvm.assume(i1) + +!0 = !{i32 0, !"typeid"} diff --git a/llvm/test/Transforms/WholeProgramDevirt/unique-retval.ll b/llvm/test/Transforms/WholeProgramDevirt/unique-retval.ll new file mode 100644 index 00000000000..4452bb75d03 --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/unique-retval.ll @@ -0,0 +1,60 @@ +; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s + +target datalayout = "e-p:64:64" +target triple = "x86_64-unknown-linux-gnu" + +@vt1 = constant [1 x i8*] [i8* bitcast (i1 (i8*)* @vf0 to i8*)], !type !0 +@vt2 = constant [1 x i8*] [i8* bitcast (i1 (i8*)* @vf0 to i8*)], !type !0, !type !1 +@vt3 = constant [1 x i8*] [i8* bitcast (i1 (i8*)* @vf1 to i8*)], !type !0, !type !1 +@vt4 = constant [1 x i8*] [i8* bitcast (i1 (i8*)* @vf1 to i8*)], !type !1 + +define i1 @vf0(i8* %this) readnone { + ret i1 0 +} + +define i1 @vf1(i8* %this) readnone { + ret i1 1 +} + +; CHECK: define i1 @call1 +define i1 @call1(i8* %obj) { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + ; CHECK: {{.*}} = bitcast [1 x i8*]* {{.*}} to i8* + ; CHECK: [[VT1:%[^ ]*]] = bitcast [1 x i8*]* {{.*}} to i8* + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid1") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to i1 (i8*)* + ; CHECK: [[RES1:%[^ ]*]] = icmp eq i8* [[VT1]], bitcast ([1 x i8*]* @vt3 to i8*) + %result = call i1 %fptr_casted(i8* %obj) + ; CHECK: ret i1 [[RES1]] + ret i1 %result +} + +; CHECK: define i32 @call2 +define i32 @call2(i8* %obj) { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + ; CHECK: [[VT2:%[^ ]*]] = bitcast [1 x i8*]* {{.*}} to i8* + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid2") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + ; Intentional type mismatch to test zero extend. + %fptr_casted = bitcast i8* %fptr to i32 (i8*)* + ; CHECK: [[RES2:%[^ ]*]] = icmp ne i8* [[VT1]], bitcast ([1 x i8*]* @vt2 to i8*) + %result = call i32 %fptr_casted(i8* %obj) + ; CHECK: [[ZEXT2:%[^ ]*]] = zext i1 [[RES2]] to i32 + ; CHECK: ret i32 [[ZEXT2:%[^ ]*]] + ret i32 %result +} + +declare i1 @llvm.type.test(i8*, metadata) +declare void @llvm.assume(i1) + +!0 = !{i32 0, !"typeid1"} +!1 = !{i32 0, !"typeid2"} diff --git a/llvm/test/Transforms/WholeProgramDevirt/vcp-accesses-memory.ll b/llvm/test/Transforms/WholeProgramDevirt/vcp-accesses-memory.ll new file mode 100644 index 00000000000..ca76383c494 --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/vcp-accesses-memory.ll @@ -0,0 +1,69 @@ +; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s +; RUN: opt -S -passes=wholeprogramdevirt %s | FileCheck %s + +target datalayout = "e-p:64:64" +target triple = "x86_64-unknown-linux-gnu" + +@vt1 = constant [2 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf1a to i8*), i8* bitcast (i32 (i8*, i32)* @vf1b to i8*)], !type !0 +@vt2 = constant [2 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf2a to i8*), i8* bitcast (i32 (i8*, i32)* @vf2b to i8*)], !type !0 + +@sink = external global i32 + +define i32 @vf1a(i8* %this, i32 %arg) { + store i32 %arg, i32* @sink + ret i32 %arg +} + +define i32 @vf2a(i8* %this, i32 %arg) { + store i32 %arg, i32* @sink + ret i32 %arg +} + +define i32 @vf1b(i8* %this, i32 %arg) { + ret i32 %arg +} + +define i32 @vf2b(i8* %this, i32 %arg) { + ret i32 %arg +} + +; Test that we don't apply VCP if the virtual function body accesses memory, +; even if the function returns a constant. + +; CHECK: define i32 @call1 +define i32 @call1(i8* %obj) { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to i32 (i8*, i32)* + ; CHECK: call i32 % + %result = call i32 %fptr_casted(i8* %obj, i32 1) + ret i32 %result +} + +; Test that we can apply VCP regardless of the function attributes by analyzing +; the function body itself. + +; CHECK: define i32 @call2 +define i32 @call2(i8* %obj) { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 1 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to i32 (i8*, i32)* + %result = call i32 %fptr_casted(i8* %obj, i32 1) + ; CHECK: ret i32 1 + ret i32 %result +} + +declare i1 @llvm.type.test(i8*, metadata) +declare void @llvm.assume(i1) + +!0 = !{i32 0, !"typeid"} diff --git a/llvm/test/Transforms/WholeProgramDevirt/vcp-decl.ll b/llvm/test/Transforms/WholeProgramDevirt/vcp-decl.ll new file mode 100644 index 00000000000..1c4e2fbe97a --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/vcp-decl.ll @@ -0,0 +1,32 @@ +; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s + +target datalayout = "e-p:64:64" +target triple = "x86_64-unknown-linux-gnu" + +@vt1 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf1 to i8*)], !type !0 +@vt2 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf2 to i8*)], !type !0 + +declare i32 @vf1(i8* %this, i32 %arg) readnone + +define i32 @vf2(i8* %this, i32 %arg) readnone { + ret i32 %arg +} + +; CHECK: define i32 @fn +define i32 @fn(i8* %obj) { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to i32 (i8*, i32)* + ; CHECK: call i32 % + %result = call i32 %fptr_casted(i8* %obj, i32 1) + ret i32 %result +} +declare i1 @llvm.type.test(i8*, metadata) +declare void @llvm.assume(i1) + +!0 = !{i32 0, !"typeid"} diff --git a/llvm/test/Transforms/WholeProgramDevirt/vcp-no-this.ll b/llvm/test/Transforms/WholeProgramDevirt/vcp-no-this.ll new file mode 100644 index 00000000000..ce76c8e6797 --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/vcp-no-this.ll @@ -0,0 +1,35 @@ +; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s + +target datalayout = "e-p:64:64" +target triple = "x86_64-unknown-linux-gnu" + +@vt1 = constant [1 x i8*] [i8* bitcast (i32 ()* @vf1 to i8*)], !type !0 +@vt2 = constant [1 x i8*] [i8* bitcast (i32 ()* @vf2 to i8*)], !type !0 + +define i32 @vf1() readnone { + ret i32 1 +} + +define i32 @vf2() readnone { + ret i32 2 +} + +; CHECK: define i32 @call +define i32 @call(i8* %obj) { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to i32 ()* + ; CHECK: call i32 % + %result = call i32 %fptr_casted() + ret i32 %result +} + +declare i1 @llvm.type.test(i8*, metadata) +declare void @llvm.assume(i1) + +!0 = !{i32 0, !"typeid"} diff --git a/llvm/test/Transforms/WholeProgramDevirt/vcp-non-constant-arg.ll b/llvm/test/Transforms/WholeProgramDevirt/vcp-non-constant-arg.ll new file mode 100644 index 00000000000..cc2ff33296a --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/vcp-non-constant-arg.ll @@ -0,0 +1,35 @@ +; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s + +target datalayout = "e-p:64:64" +target triple = "x86_64-unknown-linux-gnu" + +@vt1 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf1 to i8*)], !type !0 +@vt2 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf2 to i8*)], !type !0 + +define i32 @vf1(i8* %this, i32 %arg) readnone { + ret i32 %arg +} + +define i32 @vf2(i8* %this, i32 %arg) readnone { + ret i32 %arg +} + +; CHECK: define void @call +define void @call(i8* %obj, i32 %arg) { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to i32 (i8*, i32)* + ; CHECK: call i32 % + %result = call i32 %fptr_casted(i8* %obj, i32 %arg) + ret void +} + +declare i1 @llvm.type.test(i8*, metadata) +declare void @llvm.assume(i1) + +!0 = !{i32 0, !"typeid"} diff --git a/llvm/test/Transforms/WholeProgramDevirt/vcp-too-wide-ints.ll b/llvm/test/Transforms/WholeProgramDevirt/vcp-too-wide-ints.ll new file mode 100644 index 00000000000..c24c3b4be68 --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/vcp-too-wide-ints.ll @@ -0,0 +1,65 @@ +; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s + +target datalayout = "e-p:64:64" +target triple = "x86_64-unknown-linux-gnu" + +@vt1 = constant [1 x i8*] [i8* bitcast (i64 (i8*, i128)* @vf1 to i8*)], !type !0 +@vt2 = constant [1 x i8*] [i8* bitcast (i64 (i8*, i128)* @vf2 to i8*)], !type !0 +@vt3 = constant [1 x i8*] [i8* bitcast (i128 (i8*, i64)* @vf3 to i8*)], !type !1 +@vt4 = constant [1 x i8*] [i8* bitcast (i128 (i8*, i64)* @vf4 to i8*)], !type !1 + +define i64 @vf1(i8* %this, i128 %arg) readnone { + %argtrunc = trunc i128 %arg to i64 + ret i64 %argtrunc +} + +define i64 @vf2(i8* %this, i128 %arg) readnone { + %argtrunc = trunc i128 %arg to i64 + ret i64 %argtrunc +} + +define i128 @vf3(i8* %this, i64 %arg) readnone { + %argzext = zext i64 %arg to i128 + ret i128 %argzext +} + +define i128 @vf4(i8* %this, i64 %arg) readnone { + %argzext = zext i64 %arg to i128 + ret i128 %argzext +} + +; CHECK: define i64 @call1 +define i64 @call1(i8* %obj) { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid1") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to i64 (i8*, i128)* + ; CHECK: call i64 % + %result = call i64 %fptr_casted(i8* %obj, i128 1) + ret i64 %result +} + +; CHECK: define i128 @call2 +define i128 @call2(i8* %obj) { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid2") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to i128 (i8*, i64)* + ; CHECK: call i128 % + %result = call i128 %fptr_casted(i8* %obj, i64 1) + ret i128 %result +} + +declare i1 @llvm.type.test(i8*, metadata) +declare void @llvm.assume(i1) + +!0 = !{i32 0, !"typeid1"} +!1 = !{i32 0, !"typeid2"} diff --git a/llvm/test/Transforms/WholeProgramDevirt/vcp-type-mismatch.ll b/llvm/test/Transforms/WholeProgramDevirt/vcp-type-mismatch.ll new file mode 100644 index 00000000000..7016263f8f7 --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/vcp-type-mismatch.ll @@ -0,0 +1,71 @@ +; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s + +; Test that we correctly handle function type mismatches in argument counts +; and bitwidths. We handle an argument count mismatch by refusing +; to optimize. For bitwidth mismatches, we allow the optimization in order +; to simplify the implementation. This is legal because the bitwidth mismatch +; gives the call undefined behavior. + +target datalayout = "e-p:64:64" +target triple = "x86_64-unknown-linux-gnu" + +@vt1 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf1 to i8*)], !type !0 +@vt2 = constant [1 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf2 to i8*)], !type !0 + +define i32 @vf1(i8* %this, i32 %arg) readnone { + ret i32 %arg +} + +define i32 @vf2(i8* %this, i32 %arg) readnone { + ret i32 %arg +} + +; CHECK: define i32 @bad_arg_type +define i32 @bad_arg_type(i8* %obj) { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to i32 (i8*, i64)* + %result = call i32 %fptr_casted(i8* %obj, i64 1) + ; CHECK: ret i32 1 + ret i32 %result +} + +; CHECK: define i32 @bad_arg_count +define i32 @bad_arg_count(i8* %obj) { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to i32 (i8*, i64, i64)* + ; CHECK: call i32 % + %result = call i32 %fptr_casted(i8* %obj, i64 1, i64 2) + ret i32 %result +} + +; CHECK: define i64 @bad_return_type +define i64 @bad_return_type(i8* %obj) { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to i64 (i8*, i32)* + %result = call i64 %fptr_casted(i8* %obj, i32 1) + ; CHECK: ret i64 1 + ret i64 %result +} + +declare i1 @llvm.type.test(i8*, metadata) +declare void @llvm.assume(i1) + +!0 = !{i32 0, !"typeid"} diff --git a/llvm/test/Transforms/WholeProgramDevirt/vcp-uses-this.ll b/llvm/test/Transforms/WholeProgramDevirt/vcp-uses-this.ll new file mode 100644 index 00000000000..542402e1657 --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/vcp-uses-this.ll @@ -0,0 +1,37 @@ +; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s + +target datalayout = "e-p:64:64" +target triple = "x86_64-unknown-linux-gnu" + +@vt1 = constant [1 x i8*] [i8* bitcast (i32 (i8*)* @vf1 to i8*)], !type !0 +@vt2 = constant [1 x i8*] [i8* bitcast (i32 (i8*)* @vf2 to i8*)], !type !0 + +define i32 @vf1(i8* %this) readnone { + %this_int = ptrtoint i8* %this to i32 + ret i32 %this_int +} + +define i32 @vf2(i8* %this) readnone { + %this_int = ptrtoint i8* %this to i32 + ret i32 %this_int +} + +; CHECK: define i32 @call +define i32 @call(i8* %obj) { + %vtableptr = bitcast i8* %obj to [1 x i8*]** + %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr + %vtablei8 = bitcast [1 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to i32 (i8*)* + ; CHECK: call i32 % + %result = call i32 %fptr_casted(i8* %obj) + ret i32 %result +} + +declare i1 @llvm.type.test(i8*, metadata) +declare void @llvm.assume(i1) + +!0 = !{i32 0, !"typeid"} diff --git a/llvm/test/Transforms/WholeProgramDevirt/virtual-const-prop-begin.ll b/llvm/test/Transforms/WholeProgramDevirt/virtual-const-prop-begin.ll new file mode 100644 index 00000000000..63549e8540c --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/virtual-const-prop-begin.ll @@ -0,0 +1,139 @@ +; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s + +target datalayout = "e-p:64:64" +target triple = "x86_64-unknown-linux-gnu" + +; CHECK: [[VT1DATA:@[^ ]*]] = private constant { [8 x i8], [3 x i8*], [0 x i8] } { [8 x i8] c"\00\00\00\01\01\00\00\00", [3 x i8*] [i8* bitcast (i1 (i8*)* @vf0i1 to i8*), i8* bitcast (i1 (i8*)* @vf1i1 to i8*), i8* bitcast (i32 (i8*)* @vf1i32 to i8*)], [0 x i8] zeroinitializer }, section "vt1sec", !type [[T8:![0-9]+]] +@vt1 = constant [3 x i8*] [ +i8* bitcast (i1 (i8*)* @vf0i1 to i8*), +i8* bitcast (i1 (i8*)* @vf1i1 to i8*), +i8* bitcast (i32 (i8*)* @vf1i32 to i8*) +], section "vt1sec", !type !0 + +; CHECK: [[VT2DATA:@[^ ]*]] = private constant { [8 x i8], [3 x i8*], [0 x i8] } { [8 x i8] c"\00\00\00\02\02\00\00\00", [3 x i8*] [i8* bitcast (i1 (i8*)* @vf1i1 to i8*), i8* bitcast (i1 (i8*)* @vf0i1 to i8*), i8* bitcast (i32 (i8*)* @vf2i32 to i8*)], [0 x i8] zeroinitializer }, !type [[T8]] +@vt2 = constant [3 x i8*] [ +i8* bitcast (i1 (i8*)* @vf1i1 to i8*), +i8* bitcast (i1 (i8*)* @vf0i1 to i8*), +i8* bitcast (i32 (i8*)* @vf2i32 to i8*) +], !type !0 + +; CHECK: [[VT3DATA:@[^ ]*]] = private constant { [8 x i8], [3 x i8*], [0 x i8] } { [8 x i8] c"\00\00\00\01\03\00\00\00", [3 x i8*] [i8* bitcast (i1 (i8*)* @vf0i1 to i8*), i8* bitcast (i1 (i8*)* @vf1i1 to i8*), i8* bitcast (i32 (i8*)* @vf3i32 to i8*)], [0 x i8] zeroinitializer }, !type [[T8]] +@vt3 = constant [3 x i8*] [ +i8* bitcast (i1 (i8*)* @vf0i1 to i8*), +i8* bitcast (i1 (i8*)* @vf1i1 to i8*), +i8* bitcast (i32 (i8*)* @vf3i32 to i8*) +], !type !0 + +; CHECK: [[VT4DATA:@[^ ]*]] = private constant { [8 x i8], [3 x i8*], [0 x i8] } { [8 x i8] c"\00\00\00\02\04\00\00\00", [3 x i8*] [i8* bitcast (i1 (i8*)* @vf1i1 to i8*), i8* bitcast (i1 (i8*)* @vf0i1 to i8*), i8* bitcast (i32 (i8*)* @vf4i32 to i8*)], [0 x i8] zeroinitializer }, !type [[T8]] +@vt4 = constant [3 x i8*] [ +i8* bitcast (i1 (i8*)* @vf1i1 to i8*), +i8* bitcast (i1 (i8*)* @vf0i1 to i8*), +i8* bitcast (i32 (i8*)* @vf4i32 to i8*) +], !type !0 + +; CHECK: @vt5 = {{.*}}, !type [[T0:![0-9]+]] +@vt5 = constant [3 x i8*] [ +i8* bitcast (void ()* @__cxa_pure_virtual to i8*), +i8* bitcast (void ()* @__cxa_pure_virtual to i8*), +i8* bitcast (void ()* @__cxa_pure_virtual to i8*) +], !type !0 + +; CHECK: @vt1 = alias [3 x i8*], getelementptr inbounds ({ [8 x i8], [3 x i8*], [0 x i8] }, { [8 x i8], [3 x i8*], [0 x i8] }* [[VT1DATA]], i32 0, i32 1) +; CHECK: @vt2 = alias [3 x i8*], getelementptr inbounds ({ [8 x i8], [3 x i8*], [0 x i8] }, { [8 x i8], [3 x i8*], [0 x i8] }* [[VT2DATA]], i32 0, i32 1) +; CHECK: @vt3 = alias [3 x i8*], getelementptr inbounds ({ [8 x i8], [3 x i8*], [0 x i8] }, { [8 x i8], [3 x i8*], [0 x i8] }* [[VT3DATA]], i32 0, i32 1) +; CHECK: @vt4 = alias [3 x i8*], getelementptr inbounds ({ [8 x i8], [3 x i8*], [0 x i8] }, { [8 x i8], [3 x i8*], [0 x i8] }* [[VT4DATA]], i32 0, i32 1) + +define i1 @vf0i1(i8* %this) readnone { + ret i1 0 +} + +define i1 @vf1i1(i8* %this) readnone { + ret i1 1 +} + +define i32 @vf1i32(i8* %this) readnone { + ret i32 1 +} + +define i32 @vf2i32(i8* %this) readnone { + ret i32 2 +} + +define i32 @vf3i32(i8* %this) readnone { + ret i32 3 +} + +define i32 @vf4i32(i8* %this) readnone { + ret i32 4 +} + +; CHECK: define i1 @call1( +define i1 @call1(i8* %obj) { + %vtableptr = bitcast i8* %obj to [3 x i8*]** + %vtable = load [3 x i8*]*, [3 x i8*]** %vtableptr + ; CHECK: {{.*}} = bitcast [3 x i8*]* {{.*}} to i8* + ; CHECK: [[VT1:%[^ ]*]] = bitcast [3 x i8*]* {{.*}} to i8* + %vtablei8 = bitcast [3 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [3 x i8*], [3 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to i1 (i8*)* + ; CHECK: [[VTGEP1:%[^ ]*]] = getelementptr i8, i8* [[VT1]], i32 -5 + ; CHECK: [[VTLOAD1:%[^ ]*]] = load i8, i8* [[VTGEP1]] + ; CHECK: [[VTAND1:%[^ ]*]] = and i8 [[VTLOAD1]], 2 + ; CHECK: [[VTCMP1:%[^ ]*]] = icmp ne i8 [[VTAND1]], 0 + %result = call i1 %fptr_casted(i8* %obj) + ; CHECK: ret i1 [[VTCMP1]] + ret i1 %result +} + +; CHECK: define i1 @call2( +define i1 @call2(i8* %obj) { + %vtableptr = bitcast i8* %obj to [3 x i8*]** + %vtable = load [3 x i8*]*, [3 x i8*]** %vtableptr + ; CHECK: {{.*}} = bitcast [3 x i8*]* {{.*}} to i8* + ; CHECK: [[VT2:%[^ ]*]] = bitcast [3 x i8*]* {{.*}} to i8* + %vtablei8 = bitcast [3 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [3 x i8*], [3 x i8*]* %vtable, i32 0, i32 1 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to i1 (i8*)* + ; CHECK: [[VTGEP2:%[^ ]*]] = getelementptr i8, i8* [[VT2]], i32 -5 + ; CHECK: [[VTLOAD2:%[^ ]*]] = load i8, i8* [[VTGEP2]] + ; CHECK: [[VTAND2:%[^ ]*]] = and i8 [[VTLOAD2]], 1 + ; CHECK: [[VTCMP2:%[^ ]*]] = icmp ne i8 [[VTAND2]], 0 + %result = call i1 %fptr_casted(i8* %obj) + ; CHECK: ret i1 [[VTCMP2]] + ret i1 %result +} + +; CHECK: define i32 @call3( +define i32 @call3(i8* %obj) { + %vtableptr = bitcast i8* %obj to [3 x i8*]** + %vtable = load [3 x i8*]*, [3 x i8*]** %vtableptr + ; CHECK: {{.*}} = bitcast [3 x i8*]* {{.*}} to i8* + ; CHECK: [[VT3:%[^ ]*]] = bitcast [3 x i8*]* {{.*}} to i8* + %vtablei8 = bitcast [3 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [3 x i8*], [3 x i8*]* %vtable, i32 0, i32 2 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to i32 (i8*)* + ; CHECK: [[VTGEP3:%[^ ]*]] = getelementptr i8, i8* [[VT3]], i32 -4 + ; CHECK: [[VTBC3:%[^ ]*]] = bitcast i8* [[VTGEP3]] to i32* + ; CHECK: [[VTLOAD3:%[^ ]*]] = load i32, i32* [[VTBC3]] + %result = call i32 %fptr_casted(i8* %obj) + ; CHECK: ret i32 [[VTLOAD3]] + ret i32 %result +} + +declare i1 @llvm.type.test(i8*, metadata) +declare void @llvm.assume(i1) +declare void @__cxa_pure_virtual() + +; CHECK: [[T8]] = !{i32 8, !"typeid"} +; CHECK: [[T0]] = !{i32 0, !"typeid"} + +!0 = !{i32 0, !"typeid"} diff --git a/llvm/test/Transforms/WholeProgramDevirt/virtual-const-prop-check.ll b/llvm/test/Transforms/WholeProgramDevirt/virtual-const-prop-check.ll new file mode 100644 index 00000000000..3299f7bce65 --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/virtual-const-prop-check.ll @@ -0,0 +1,147 @@ +; RUN: opt -S -wholeprogramdevirt -pass-remarks=wholeprogramdevirt %s 2>&1 | FileCheck %s + +target datalayout = "e-p:64:64" +target triple = "x86_64-unknown-linux-gnu" + +; CHECK: remark: <unknown>:0:0: virtual-const-prop: devirtualized a call to vf1i32 +; CHECK: remark: <unknown>:0:0: virtual-const-prop-1-bit: devirtualized a call to vf1i1 +; CHECK: remark: <unknown>:0:0: virtual-const-prop-1-bit: devirtualized a call to vf0i1 +; CHECK: remark: <unknown>:0:0: devirtualized vf0i1 +; CHECK: remark: <unknown>:0:0: devirtualized vf1i1 +; CHECK: remark: <unknown>:0:0: devirtualized vf1i32 +; CHECK: remark: <unknown>:0:0: devirtualized vf2i32 +; CHECK: remark: <unknown>:0:0: devirtualized vf3i32 +; CHECK: remark: <unknown>:0:0: devirtualized vf4i32 +; CHECK-NOT: devirtualized + +; CHECK: [[VT1DATA:@[^ ]*]] = private constant { [8 x i8], [3 x i8*], [0 x i8] } { [8 x i8] c"\00\00\00\01\01\00\00\00", [3 x i8*] [i8* bitcast (i1 (i8*)* @vf0i1 to i8*), i8* bitcast (i1 (i8*)* @vf1i1 to i8*), i8* bitcast (i32 (i8*)* @vf1i32 to i8*)], [0 x i8] zeroinitializer }, section "vt1sec", !type [[T8:![0-9]+]] +@vt1 = constant [3 x i8*] [ +i8* bitcast (i1 (i8*)* @vf0i1 to i8*), +i8* bitcast (i1 (i8*)* @vf1i1 to i8*), +i8* bitcast (i32 (i8*)* @vf1i32 to i8*) +], section "vt1sec", !type !0 + +; CHECK: [[VT2DATA:@[^ ]*]] = private constant { [8 x i8], [3 x i8*], [0 x i8] } { [8 x i8] c"\00\00\00\02\02\00\00\00", [3 x i8*] [i8* bitcast (i1 (i8*)* @vf1i1 to i8*), i8* bitcast (i1 (i8*)* @vf0i1 to i8*), i8* bitcast (i32 (i8*)* @vf2i32 to i8*)], [0 x i8] zeroinitializer }, !type [[T8]] +@vt2 = constant [3 x i8*] [ +i8* bitcast (i1 (i8*)* @vf1i1 to i8*), +i8* bitcast (i1 (i8*)* @vf0i1 to i8*), +i8* bitcast (i32 (i8*)* @vf2i32 to i8*) +], !type !0 + +; CHECK: [[VT3DATA:@[^ ]*]] = private constant { [8 x i8], [3 x i8*], [0 x i8] } { [8 x i8] c"\00\00\00\01\03\00\00\00", [3 x i8*] [i8* bitcast (i1 (i8*)* @vf0i1 to i8*), i8* bitcast (i1 (i8*)* @vf1i1 to i8*), i8* bitcast (i32 (i8*)* @vf3i32 to i8*)], [0 x i8] zeroinitializer }, !type [[T8]] +@vt3 = constant [3 x i8*] [ +i8* bitcast (i1 (i8*)* @vf0i1 to i8*), +i8* bitcast (i1 (i8*)* @vf1i1 to i8*), +i8* bitcast (i32 (i8*)* @vf3i32 to i8*) +], !type !0 + +; CHECK: [[VT4DATA:@[^ ]*]] = private constant { [8 x i8], [3 x i8*], [0 x i8] } { [8 x i8] c"\00\00\00\02\04\00\00\00", [3 x i8*] [i8* bitcast (i1 (i8*)* @vf1i1 to i8*), i8* bitcast (i1 (i8*)* @vf0i1 to i8*), i8* bitcast (i32 (i8*)* @vf4i32 to i8*)], [0 x i8] zeroinitializer }, !type [[T8]] +@vt4 = constant [3 x i8*] [ +i8* bitcast (i1 (i8*)* @vf1i1 to i8*), +i8* bitcast (i1 (i8*)* @vf0i1 to i8*), +i8* bitcast (i32 (i8*)* @vf4i32 to i8*) +], !type !0 + +; CHECK: @vt5 = {{.*}}, !type [[T0:![0-9]+]] +@vt5 = constant [3 x i8*] [ +i8* bitcast (void ()* @__cxa_pure_virtual to i8*), +i8* bitcast (void ()* @__cxa_pure_virtual to i8*), +i8* bitcast (void ()* @__cxa_pure_virtual to i8*) +], !type !0 + +; CHECK: @vt1 = alias [3 x i8*], getelementptr inbounds ({ [8 x i8], [3 x i8*], [0 x i8] }, { [8 x i8], [3 x i8*], [0 x i8] }* [[VT1DATA]], i32 0, i32 1) +; CHECK: @vt2 = alias [3 x i8*], getelementptr inbounds ({ [8 x i8], [3 x i8*], [0 x i8] }, { [8 x i8], [3 x i8*], [0 x i8] }* [[VT2DATA]], i32 0, i32 1) +; CHECK: @vt3 = alias [3 x i8*], getelementptr inbounds ({ [8 x i8], [3 x i8*], [0 x i8] }, { [8 x i8], [3 x i8*], [0 x i8] }* [[VT3DATA]], i32 0, i32 1) +; CHECK: @vt4 = alias [3 x i8*], getelementptr inbounds ({ [8 x i8], [3 x i8*], [0 x i8] }, { [8 x i8], [3 x i8*], [0 x i8] }* [[VT4DATA]], i32 0, i32 1) + +define i1 @vf0i1(i8* %this) readnone { + ret i1 0 +} + +define i1 @vf1i1(i8* %this) readnone { + ret i1 1 +} + +define i32 @vf1i32(i8* %this) readnone { + ret i32 1 +} + +define i32 @vf2i32(i8* %this) readnone { + ret i32 2 +} + +define i32 @vf3i32(i8* %this) readnone { + ret i32 3 +} + +define i32 @vf4i32(i8* %this) readnone { + ret i32 4 +} + +; CHECK: define i1 @call1( +define i1 @call1(i8* %obj) { + %vtableptr = bitcast i8* %obj to [3 x i8*]** + %vtable = load [3 x i8*]*, [3 x i8*]** %vtableptr + ; CHECK: [[VT1:%[^ ]*]] = bitcast [3 x i8*]* {{.*}} to i8* + %vtablei8 = bitcast [3 x i8*]* %vtable to i8* + %pair = call {i8*, i1} @llvm.type.checked.load(i8* %vtablei8, i32 0, metadata !"typeid") + %fptr = extractvalue {i8*, i1} %pair, 0 + %fptr_casted = bitcast i8* %fptr to i1 (i8*)* + ; CHECK: [[VTGEP1:%[^ ]*]] = getelementptr i8, i8* [[VT1]], i32 -5 + ; CHECK: [[VTLOAD1:%[^ ]*]] = load i8, i8* [[VTGEP1]] + ; CHECK: [[VTAND1:%[^ ]*]] = and i8 [[VTLOAD1]], 2 + ; CHECK: [[VTCMP1:%[^ ]*]] = icmp ne i8 [[VTAND1]], 0 + %result = call i1 %fptr_casted(i8* %obj) + ; CHECK: [[AND1:%[^ ]*]] = and i1 [[VTCMP1]], true + %p = extractvalue {i8*, i1} %pair, 1 + %and = and i1 %result, %p + ; CHECK: ret i1 [[AND1]] + ret i1 %and +} + +; CHECK: define i1 @call2( +define i1 @call2(i8* %obj) { + %vtableptr = bitcast i8* %obj to [3 x i8*]** + %vtable = load [3 x i8*]*, [3 x i8*]** %vtableptr + ; CHECK: [[VT2:%[^ ]*]] = bitcast [3 x i8*]* {{.*}} to i8* + %vtablei8 = bitcast [3 x i8*]* %vtable to i8* + %pair = call {i8*, i1} @llvm.type.checked.load(i8* %vtablei8, i32 8, metadata !"typeid") + %fptr = extractvalue {i8*, i1} %pair, 0 + %fptr_casted = bitcast i8* %fptr to i1 (i8*)* + ; CHECK: [[VTGEP2:%[^ ]*]] = getelementptr i8, i8* [[VT2]], i32 -5 + ; CHECK: [[VTLOAD2:%[^ ]*]] = load i8, i8* [[VTGEP2]] + ; CHECK: [[VTAND2:%[^ ]*]] = and i8 [[VTLOAD2]], 1 + ; CHECK: [[VTCMP2:%[^ ]*]] = icmp ne i8 [[VTAND2]], 0 + %result = call i1 %fptr_casted(i8* %obj) + ; CHECK: [[AND2:%[^ ]*]] = and i1 [[VTCMP2]], true + %p = extractvalue {i8*, i1} %pair, 1 + %and = and i1 %result, %p + ; CHECK: ret i1 [[AND2]] + ret i1 %and +} + +; CHECK: define i32 @call3( +define i32 @call3(i8* %obj) { + %vtableptr = bitcast i8* %obj to [3 x i8*]** + %vtable = load [3 x i8*]*, [3 x i8*]** %vtableptr + ; CHECK: [[VT3:%[^ ]*]] = bitcast [3 x i8*]* {{.*}} to i8* + %vtablei8 = bitcast [3 x i8*]* %vtable to i8* + %pair = call {i8*, i1} @llvm.type.checked.load(i8* %vtablei8, i32 16, metadata !"typeid") + %fptr = extractvalue {i8*, i1} %pair, 0 + %fptr_casted = bitcast i8* %fptr to i32 (i8*)* + ; CHECK: [[VTGEP3:%[^ ]*]] = getelementptr i8, i8* [[VT3]], i32 -4 + ; CHECK: [[VTBC3:%[^ ]*]] = bitcast i8* [[VTGEP3]] to i32* + ; CHECK: [[VTLOAD3:%[^ ]*]] = load i32, i32* [[VTBC3]] + %result = call i32 %fptr_casted(i8* %obj) + ; CHECK: ret i32 [[VTLOAD3]] + ret i32 %result +} + +declare {i8*, i1} @llvm.type.checked.load(i8*, i32, metadata) +declare void @llvm.assume(i1) +declare void @__cxa_pure_virtual() + +; CHECK: [[T8]] = !{i32 8, !"typeid"} +; CHECK: [[T0]] = !{i32 0, !"typeid"} + +!0 = !{i32 0, !"typeid"} diff --git a/llvm/test/Transforms/WholeProgramDevirt/virtual-const-prop-end.ll b/llvm/test/Transforms/WholeProgramDevirt/virtual-const-prop-end.ll new file mode 100644 index 00000000000..3d61e1e2082 --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/virtual-const-prop-end.ll @@ -0,0 +1,134 @@ +; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s + +target datalayout = "e-p:64:64" +target triple = "x86_64-unknown-linux-gnu" + +; CHECK: [[VT1DATA:@[^ ]*]] = private constant { [0 x i8], [4 x i8*], [8 x i8] } { [0 x i8] zeroinitializer, [4 x i8*] [i8* null, i8* bitcast (i1 (i8*)* @vf0i1 to i8*), i8* bitcast (i1 (i8*)* @vf1i1 to i8*), i8* bitcast (i32 (i8*)* @vf1i32 to i8*)], [8 x i8] c"\01\00\00\00\01\00\00\00" }, !type [[T8:![0-9]+]] +@vt1 = constant [4 x i8*] [ +i8* null, +i8* bitcast (i1 (i8*)* @vf0i1 to i8*), +i8* bitcast (i1 (i8*)* @vf1i1 to i8*), +i8* bitcast (i32 (i8*)* @vf1i32 to i8*) +], !type !1 + +; CHECK: [[VT2DATA:@[^ ]*]] = private constant { [0 x i8], [3 x i8*], [8 x i8] } { [0 x i8] zeroinitializer, [3 x i8*] [i8* bitcast (i1 (i8*)* @vf1i1 to i8*), i8* bitcast (i1 (i8*)* @vf0i1 to i8*), i8* bitcast (i32 (i8*)* @vf2i32 to i8*)], [8 x i8] c"\02\00\00\00\02\00\00\00" }, !type [[T0:![0-9]+]] +@vt2 = constant [3 x i8*] [ +i8* bitcast (i1 (i8*)* @vf1i1 to i8*), +i8* bitcast (i1 (i8*)* @vf0i1 to i8*), +i8* bitcast (i32 (i8*)* @vf2i32 to i8*) +], !type !0 + +; CHECK: [[VT3DATA:@[^ ]*]] = private constant { [0 x i8], [4 x i8*], [8 x i8] } { [0 x i8] zeroinitializer, [4 x i8*] [i8* null, i8* bitcast (i1 (i8*)* @vf0i1 to i8*), i8* bitcast (i1 (i8*)* @vf1i1 to i8*), i8* bitcast (i32 (i8*)* @vf3i32 to i8*)], [8 x i8] c"\03\00\00\00\01\00\00\00" }, !type [[T8]] +@vt3 = constant [4 x i8*] [ +i8* null, +i8* bitcast (i1 (i8*)* @vf0i1 to i8*), +i8* bitcast (i1 (i8*)* @vf1i1 to i8*), +i8* bitcast (i32 (i8*)* @vf3i32 to i8*) +], !type !1 + +; CHECK: [[VT4DATA:@[^ ]*]] = private constant { [0 x i8], [3 x i8*], [8 x i8] } { [0 x i8] zeroinitializer, [3 x i8*] [i8* bitcast (i1 (i8*)* @vf1i1 to i8*), i8* bitcast (i1 (i8*)* @vf0i1 to i8*), i8* bitcast (i32 (i8*)* @vf4i32 to i8*)], [8 x i8] c"\04\00\00\00\02\00\00\00" }, !type [[T0]] +@vt4 = constant [3 x i8*] [ +i8* bitcast (i1 (i8*)* @vf1i1 to i8*), +i8* bitcast (i1 (i8*)* @vf0i1 to i8*), +i8* bitcast (i32 (i8*)* @vf4i32 to i8*) +], !type !0 + +; CHECK: @vt1 = alias [4 x i8*], getelementptr inbounds ({ [0 x i8], [4 x i8*], [8 x i8] }, { [0 x i8], [4 x i8*], [8 x i8] }* [[VT1DATA]], i32 0, i32 1) +; CHECK: @vt2 = alias [3 x i8*], getelementptr inbounds ({ [0 x i8], [3 x i8*], [8 x i8] }, { [0 x i8], [3 x i8*], [8 x i8] }* [[VT2DATA]], i32 0, i32 1) +; CHECK: @vt3 = alias [4 x i8*], getelementptr inbounds ({ [0 x i8], [4 x i8*], [8 x i8] }, { [0 x i8], [4 x i8*], [8 x i8] }* [[VT3DATA]], i32 0, i32 1) +; CHECK: @vt4 = alias [3 x i8*], getelementptr inbounds ({ [0 x i8], [3 x i8*], [8 x i8] }, { [0 x i8], [3 x i8*], [8 x i8] }* [[VT4DATA]], i32 0, i32 1) + +define i1 @vf0i1(i8* %this) readnone { + ret i1 0 +} + +define i1 @vf1i1(i8* %this) readnone { + ret i1 1 +} + +define i32 @vf1i32(i8* %this) readnone { + ret i32 1 +} + +define i32 @vf2i32(i8* %this) readnone { + ret i32 2 +} + +define i32 @vf3i32(i8* %this) readnone { + ret i32 3 +} + +define i32 @vf4i32(i8* %this) readnone { + ret i32 4 +} + +; CHECK: define i1 @call1( +define i1 @call1(i8* %obj) { + %vtableptr = bitcast i8* %obj to [3 x i8*]** + %vtable = load [3 x i8*]*, [3 x i8*]** %vtableptr + ; CHECK: {{.*}} = bitcast [3 x i8*]* {{.*}} to i8* + ; CHECK: [[VT1:%[^ ]*]] = bitcast [3 x i8*]* {{.*}} to i8* + %vtablei8 = bitcast [3 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [3 x i8*], [3 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to i1 (i8*)* + ; CHECK: [[VTGEP1:%[^ ]*]] = getelementptr i8, i8* [[VT1]], i32 28 + ; CHECK: [[VTLOAD1:%[^ ]*]] = load i8, i8* [[VTGEP1]] + ; CHECK: [[VTAND1:%[^ ]*]] = and i8 [[VTLOAD1]], 2 + ; CHECK: [[VTCMP1:%[^ ]*]] = icmp ne i8 [[VTAND1]], 0 + %result = call i1 %fptr_casted(i8* %obj) + ; CHECK: ret i1 [[VTCMP1]] + ret i1 %result +} + +; CHECK: define i1 @call2( +define i1 @call2(i8* %obj) { + %vtableptr = bitcast i8* %obj to [3 x i8*]** + %vtable = load [3 x i8*]*, [3 x i8*]** %vtableptr + ; CHECK: {{.*}} = bitcast [3 x i8*]* {{.*}} to i8* + ; CHECK: [[VT2:%[^ ]*]] = bitcast [3 x i8*]* {{.*}} to i8* + %vtablei8 = bitcast [3 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [3 x i8*], [3 x i8*]* %vtable, i32 0, i32 1 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to i1 (i8*)* + ; CHECK: [[VTGEP2:%[^ ]*]] = getelementptr i8, i8* [[VT2]], i32 28 + ; CHECK: [[VTLOAD2:%[^ ]*]] = load i8, i8* [[VTGEP2]] + ; CHECK: [[VTAND2:%[^ ]*]] = and i8 [[VTLOAD2]], 1 + ; CHECK: [[VTCMP2:%[^ ]*]] = icmp ne i8 [[VTAND2]], 0 + %result = call i1 %fptr_casted(i8* %obj) + ; CHECK: ret i1 [[VTCMP2]] + ret i1 %result +} + +; CHECK: define i32 @call3( +define i32 @call3(i8* %obj) { + %vtableptr = bitcast i8* %obj to [3 x i8*]** + %vtable = load [3 x i8*]*, [3 x i8*]** %vtableptr + ; CHECK: {{.*}} = bitcast [3 x i8*]* {{.*}} to i8* + ; CHECK: [[VT3:%[^ ]*]] = bitcast [3 x i8*]* {{.*}} to i8* + %vtablei8 = bitcast [3 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [3 x i8*], [3 x i8*]* %vtable, i32 0, i32 2 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to i32 (i8*)* + ; CHECK: [[VTGEP3:%[^ ]*]] = getelementptr i8, i8* [[VT3]], i32 24 + ; CHECK: [[VTBC3:%[^ ]*]] = bitcast i8* [[VTGEP3]] to i32* + ; CHECK: [[VTLOAD3:%[^ ]*]] = load i32, i32* [[VTBC3]] + %result = call i32 %fptr_casted(i8* %obj) + ; CHECK: ret i32 [[VTLOAD3]] + ret i32 %result +} + +declare i1 @llvm.type.test(i8*, metadata) +declare void @llvm.assume(i1) + +; CHECK: [[T8]] = !{i32 8, !"typeid"} +; CHECK: [[T0]] = !{i32 0, !"typeid"} + +!0 = !{i32 0, !"typeid"} +!1 = !{i32 8, !"typeid"} diff --git a/llvm/test/Transforms/WholeProgramDevirt/vtable-decl.ll b/llvm/test/Transforms/WholeProgramDevirt/vtable-decl.ll new file mode 100644 index 00000000000..e56170a4997 --- /dev/null +++ b/llvm/test/Transforms/WholeProgramDevirt/vtable-decl.ll @@ -0,0 +1,25 @@ +; Check that we don't crash when processing declaration with type metadata +; RUN: opt -S -wholeprogramdevirt %s + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-none-linux-gnu" + +declare i1 @llvm.type.test(i8*, metadata) +declare void @llvm.assume(i1) + +@_ZTVN3foo3barE = external dso_local unnamed_addr constant { [8 x i8*] }, align 8, !type !0 + +define i1 @call1(i8* %obj) { + %vtableptr = bitcast i8* %obj to [3 x i8*]** + %vtable = load [3 x i8*]*, [3 x i8*]** %vtableptr + %vtablei8 = bitcast [3 x i8*]* %vtable to i8* + %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid") + call void @llvm.assume(i1 %p) + %fptrptr = getelementptr [3 x i8*], [3 x i8*]* %vtable, i32 0, i32 0 + %fptr = load i8*, i8** %fptrptr + %fptr_casted = bitcast i8* %fptr to i1 (i8*)* + %result = call i1 %fptr_casted(i8* %obj) + ret i1 %result +} + +!0 = !{i64 16, !"_ZTSN3foo3barE"} |