blob: ca76383c49434c361a40da35c6011951d7d93d65 [file] [log] [blame]
Eric Christophercee313d2019-04-17 04:52:47 +00001; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s
2; RUN: opt -S -passes=wholeprogramdevirt %s | FileCheck %s
3
4target datalayout = "e-p:64:64"
5target triple = "x86_64-unknown-linux-gnu"
6
7@vt1 = constant [2 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf1a to i8*), i8* bitcast (i32 (i8*, i32)* @vf1b to i8*)], !type !0
8@vt2 = constant [2 x i8*] [i8* bitcast (i32 (i8*, i32)* @vf2a to i8*), i8* bitcast (i32 (i8*, i32)* @vf2b to i8*)], !type !0
9
10@sink = external global i32
11
12define i32 @vf1a(i8* %this, i32 %arg) {
13 store i32 %arg, i32* @sink
14 ret i32 %arg
15}
16
17define i32 @vf2a(i8* %this, i32 %arg) {
18 store i32 %arg, i32* @sink
19 ret i32 %arg
20}
21
22define i32 @vf1b(i8* %this, i32 %arg) {
23 ret i32 %arg
24}
25
26define i32 @vf2b(i8* %this, i32 %arg) {
27 ret i32 %arg
28}
29
30; Test that we don't apply VCP if the virtual function body accesses memory,
31; even if the function returns a constant.
32
33; CHECK: define i32 @call1
34define i32 @call1(i8* %obj) {
35 %vtableptr = bitcast i8* %obj to [1 x i8*]**
36 %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr
37 %vtablei8 = bitcast [1 x i8*]* %vtable to i8*
38 %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid")
39 call void @llvm.assume(i1 %p)
40 %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 0
41 %fptr = load i8*, i8** %fptrptr
42 %fptr_casted = bitcast i8* %fptr to i32 (i8*, i32)*
43 ; CHECK: call i32 %
44 %result = call i32 %fptr_casted(i8* %obj, i32 1)
45 ret i32 %result
46}
47
48; Test that we can apply VCP regardless of the function attributes by analyzing
49; the function body itself.
50
51; CHECK: define i32 @call2
52define i32 @call2(i8* %obj) {
53 %vtableptr = bitcast i8* %obj to [1 x i8*]**
54 %vtable = load [1 x i8*]*, [1 x i8*]** %vtableptr
55 %vtablei8 = bitcast [1 x i8*]* %vtable to i8*
56 %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid")
57 call void @llvm.assume(i1 %p)
58 %fptrptr = getelementptr [1 x i8*], [1 x i8*]* %vtable, i32 0, i32 1
59 %fptr = load i8*, i8** %fptrptr
60 %fptr_casted = bitcast i8* %fptr to i32 (i8*, i32)*
61 %result = call i32 %fptr_casted(i8* %obj, i32 1)
62 ; CHECK: ret i32 1
63 ret i32 %result
64}
65
66declare i1 @llvm.type.test(i8*, metadata)
67declare void @llvm.assume(i1)
68
69!0 = !{i32 0, !"typeid"}