xref: /llvm-project/llvm/test/Transforms/Inline/devirtualize.ll (revision 151602c7a9935558ca671b35359989b261045db0)
1; RUN: opt -S -Os < %s | FileCheck %s
2; RUN: opt -S -aa-pipeline=basic-aa -passes='default<Os>' < %s | FileCheck %s
3target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
4target triple = "x86_64-apple-darwin10.0.0"
5
6
7; Simple devirt testcase, requires iteration between inliner and GVN.
8;  rdar://6295824
9define i32 @foo(ptr noalias %p, ptr noalias %q) nounwind ssp {
10entry:
11  store ptr @bar, ptr %p
12  store i64 0, ptr %q
13  %tmp3 = load ptr, ptr %p                        ; <ptr> [#uses=1]
14  %call = call i32 %tmp3()                        ; <i32> [#uses=1]
15  %X = add i32 %call, 4
16  ret i32 %X
17
18; CHECK-LABEL: @foo(
19; CHECK-NEXT: entry:
20; CHECK-NEXT: store
21; CHECK-NEXT: store
22; CHECK-NEXT: ret i32 11
23}
24
25define internal i32 @bar() nounwind ssp {
26entry:
27  ret i32 7
28}
29
30
31;; More complex devirt case, from PR6724
32; CHECK: @_Z1gv()
33; CHECK-NEXT: entry:
34; CHECK-NEXT: ret i32 7
35
36%0 = type { ptr, ptr }
37%1 = type { ptr, ptr, i32, i32, ptr, i64, ptr, i64 }
38%2 = type { ptr, ptr, ptr }
39%struct.A = type { ptr }
40%struct.B = type { ptr }
41%struct.C = type { [16 x i8] }
42%struct.D = type { [16 x i8] }
43
44@_ZTV1D = linkonce_odr constant [6 x ptr] [ptr null, ptr @_ZTI1D, ptr @_ZN1D1fEv, ptr inttoptr (i64 -8 to ptr), ptr @_ZTI1D, ptr @_ZThn8_N1D1fEv] ; <ptr> [#uses=2]
45@_ZTVN10__cxxabiv120__si_class_type_infoE = external global ptr ; <ptr> [#uses=1]
46@_ZTS1D = linkonce_odr constant [3 x i8] c"1D\00"     ; <ptr> [#uses=1]
47@_ZTVN10__cxxabiv121__vmi_class_type_infoE = external global ptr ; <ptr> [#uses=1]
48@_ZTS1C = linkonce_odr constant [3 x i8] c"1C\00"     ; <ptr> [#uses=1]
49@_ZTVN10__cxxabiv117__class_type_infoE = external global ptr ; <ptr> [#uses=1]
50@_ZTS1A = linkonce_odr constant [3 x i8] c"1A\00"     ; <ptr> [#uses=1]
51@_ZTI1A = linkonce_odr constant %0 { ptr getelementptr inbounds (ptr, ptr @_ZTVN10__cxxabiv117__class_type_infoE, i64 2), ptr @_ZTS1A } ; <ptr> [#uses=1]
52@_ZTS1B = linkonce_odr constant [3 x i8] c"1B\00"     ; <ptr> [#uses=1]
53@_ZTI1B = linkonce_odr constant %0 { ptr getelementptr inbounds (ptr, ptr @_ZTVN10__cxxabiv117__class_type_infoE, i64 2), ptr @_ZTS1B } ; <ptr> [#uses=1]
54@_ZTI1C = linkonce_odr constant %1 { ptr getelementptr inbounds (ptr, ptr @_ZTVN10__cxxabiv121__vmi_class_type_infoE, i64 2), ptr @_ZTS1C, i32 0, i32 2, ptr @_ZTI1A, i64 2, ptr @_ZTI1B, i64 2050 } ; <ptr> [#uses=1]
55@_ZTI1D = linkonce_odr constant %2 { ptr getelementptr inbounds (ptr, ptr @_ZTVN10__cxxabiv120__si_class_type_infoE, i64 2), ptr @_ZTS1D, ptr @_ZTI1C } ; <ptr> [#uses=1]
56@_ZTV1C = linkonce_odr constant [6 x ptr] [ptr null, ptr @_ZTI1C, ptr @_ZN1C1fEv, ptr inttoptr (i64 -8 to ptr), ptr @_ZTI1C, ptr @_ZThn8_N1C1fEv] ; <ptr> [#uses=2]
57@_ZTV1B = linkonce_odr constant [3 x ptr] [ptr null, ptr @_ZTI1B, ptr @_ZN1B1fEv] ; <ptr> [#uses=1]
58@_ZTV1A = linkonce_odr constant [3 x ptr] [ptr null, ptr @_ZTI1A, ptr @_ZN1A1fEv] ; <ptr> [#uses=1]
59
60define i32 @_Z1gv() ssp {
61entry:
62  %d = alloca %struct.C, align 8                  ; <ptr> [#uses=2]
63  call void @_ZN1DC1Ev(ptr %d)
64  %call = call i32 @_Z1fP1D(ptr %d)        ; <i32> [#uses=1]
65  %X = add i32 %call, 3
66  ret i32 %X
67}
68
69define linkonce_odr void @_ZN1DC1Ev(ptr %this) inlinehint ssp align 2 {
70entry:
71  call void @_ZN1DC2Ev(ptr %this)
72  ret void
73}
74
75define internal i32 @_Z1fP1D(ptr %d) ssp {
76entry:
77  %0 = icmp eq ptr %d, null                ; <i1> [#uses=1]
78  br i1 %0, label %cast.end, label %cast.notnull
79
80cast.notnull:                                     ; preds = %entry
81  %add.ptr = getelementptr i8, ptr %d, i64 8          ; <ptr> [#uses=1]
82  br label %cast.end
83
84cast.end:                                         ; preds = %entry, %cast.notnull
85  %1 = phi ptr [ %add.ptr, %cast.notnull ], [ null, %entry ] ; <ptr> [#uses=2]
86  %2 = load ptr, ptr %1                ; <ptr> [#uses=1]
87  %vfn = getelementptr inbounds ptr, ptr %2, i64 0 ; <ptr> [#uses=1]
88  %3 = load ptr, ptr %vfn               ; <ptr> [#uses=1]
89  %call = call i32 %3(ptr %1)              ; <i32> [#uses=1]
90  ret i32 %call
91}
92
93define linkonce_odr i32 @_ZN1D1fEv(ptr %this) ssp align 2 {
94entry:
95  ret i32 4
96}
97
98define linkonce_odr i32 @_ZThn8_N1D1fEv(ptr %this) ssp {
99entry:
100  %0 = getelementptr inbounds i8, ptr %this, i64 -8      ; <ptr> [#uses=1]
101  %call = call i32 @_ZN1D1fEv(ptr %0)      ; <i32> [#uses=1]
102  ret i32 %call
103}
104
105define linkonce_odr void @_ZN1DC2Ev(ptr %this) inlinehint ssp align 2 {
106entry:
107  call void @_ZN1CC2Ev(ptr %this)
108  %0 = getelementptr inbounds i8, ptr %this, i64 0       ; <ptr> [#uses=1]
109  store ptr getelementptr inbounds ([6 x ptr], ptr @_ZTV1D, i64 0, i64 2), ptr %0
110  %1 = getelementptr inbounds i8, ptr %this, i64 8       ; <ptr> [#uses=1]
111  store ptr getelementptr inbounds ([6 x ptr], ptr @_ZTV1D, i64 0, i64 5), ptr %1
112  ret void
113}
114
115define linkonce_odr void @_ZN1CC2Ev(ptr %this) inlinehint ssp align 2 {
116entry:
117  call void @_ZN1AC2Ev(ptr %this)
118  %0 = getelementptr inbounds i8, ptr %this, i64 8       ; <ptr> [#uses=1]
119  call void @_ZN1BC2Ev(ptr %0)
120  %1 = getelementptr inbounds i8, ptr %this, i64 0       ; <ptr> [#uses=1]
121  store ptr getelementptr inbounds ([6 x ptr], ptr @_ZTV1C, i64 0, i64 2), ptr %1
122  %2 = getelementptr inbounds i8, ptr %this, i64 8       ; <ptr> [#uses=1]
123  store ptr getelementptr inbounds ([6 x ptr], ptr @_ZTV1C, i64 0, i64 5), ptr %2
124  ret void
125}
126
127define linkonce_odr i32 @_ZN1C1fEv(ptr %this) ssp align 2 {
128entry:
129  ret i32 3
130}
131
132define linkonce_odr i32 @_ZThn8_N1C1fEv(ptr %this) {
133entry:
134  %0 = getelementptr inbounds i8, ptr %this, i64 -8      ; <ptr> [#uses=1]
135  %call = call i32 @_ZN1C1fEv(ptr %0)      ; <i32> [#uses=1]
136  ret i32 %call
137}
138
139define linkonce_odr void @_ZN1AC2Ev(ptr %this) inlinehint ssp align 2 {
140entry:
141  %0 = getelementptr inbounds i8, ptr %this, i64 0       ; <ptr> [#uses=1]
142  store ptr getelementptr inbounds ([3 x ptr], ptr @_ZTV1A, i64 0, i64 2), ptr %0
143  ret void
144}
145
146define linkonce_odr void @_ZN1BC2Ev(ptr %this) inlinehint ssp align 2 {
147entry:
148  %0 = getelementptr inbounds i8, ptr %this, i64 0       ; <ptr> [#uses=1]
149  store ptr getelementptr inbounds ([3 x ptr], ptr @_ZTV1B, i64 0, i64 2), ptr %0
150  ret void
151}
152
153define linkonce_odr i32 @_ZN1B1fEv(ptr %this) ssp align 2 {
154entry:
155  ret i32 2
156}
157
158define linkonce_odr i32 @_ZN1A1fEv(ptr %this) ssp align 2 {
159entry:
160  ret i32 1
161}
162