blob: ca76383c49434c361a40da35c6011951d7d93d65 [file] [log] [blame]
Peter Collingbournedf49d1b2016-02-09 22:50:34 +00001; RUN: opt -S -wholeprogramdevirt %s | FileCheck %s
Peter Collingbourne37317f12017-02-17 18:17:04 +00002; RUN: opt -S -passes=wholeprogramdevirt %s | FileCheck %s
Peter Collingbournedf49d1b2016-02-09 22:50:34 +00003
4target datalayout = "e-p:64:64"
5target triple = "x86_64-unknown-linux-gnu"
6
Peter Collingbourne37317f12017-02-17 18:17:04 +00007@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
Peter Collingbournedf49d1b2016-02-09 22:50:34 +00009
Peter Collingbourne37317f12017-02-17 18:17:04 +000010@sink = external global i32
11
12define i32 @vf1a(i8* %this, i32 %arg) {
13 store i32 %arg, i32* @sink
Peter Collingbournedf49d1b2016-02-09 22:50:34 +000014 ret i32 %arg
15}
16
Peter Collingbourne37317f12017-02-17 18:17:04 +000017define i32 @vf2a(i8* %this, i32 %arg) {
18 store i32 %arg, i32* @sink
Peter Collingbournedf49d1b2016-02-09 22:50:34 +000019 ret i32 %arg
20}
21
Peter Collingbourne37317f12017-02-17 18:17:04 +000022define 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) {
Peter Collingbournedf49d1b2016-02-09 22:50:34 +000035 %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*
Peter Collingbourne7efd7502016-06-24 21:21:32 +000038 %p = call i1 @llvm.type.test(i8* %vtablei8, metadata !"typeid")
Peter Collingbournedf49d1b2016-02-09 22:50:34 +000039 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
Peter Collingbourne37317f12017-02-17 18:17:04 +000048; 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
Peter Collingbourne7efd7502016-06-24 21:21:32 +000066declare i1 @llvm.type.test(i8*, metadata)
Peter Collingbournedf49d1b2016-02-09 22:50:34 +000067declare void @llvm.assume(i1)
68
Peter Collingbourne7efd7502016-06-24 21:21:32 +000069!0 = !{i32 0, !"typeid"}