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(i32 ()** noalias %p, i64* noalias %q) nounwind ssp {
10entry:
11  store i32 ()* @bar, i32 ()** %p
12  store i64 0, i64* %q
13  %tmp3 = load i32 ()*, i32 ()** %p                        ; <i32 ()*> [#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 { i8*, i8* }
37%1 = type { i8*, i8*, i32, i32, i8*, i64, i8*, i64 }
38%2 = type { i8*, i8*, i8* }
39%struct.A = type { i8** }
40%struct.B = type { i8** }
41%struct.C = type { [16 x i8] }
42%struct.D = type { [16 x i8] }
43
44@_ZTV1D = linkonce_odr constant [6 x i8*] [i8* null, i8* bitcast (%2* @_ZTI1D to i8*), i8* bitcast (i32 (%struct.C*)* @_ZN1D1fEv to i8*), i8* inttoptr (i64 -8 to i8*), i8* bitcast (%2* @_ZTI1D to i8*), i8* bitcast (i32 (%struct.C*)* @_ZThn8_N1D1fEv to i8*)] ; <[6 x i8*]*> [#uses=2]
45@_ZTVN10__cxxabiv120__si_class_type_infoE = external global i8* ; <i8**> [#uses=1]
46@_ZTS1D = linkonce_odr constant [3 x i8] c"1D\00"     ; <[3 x i8]*> [#uses=1]
47@_ZTVN10__cxxabiv121__vmi_class_type_infoE = external global i8* ; <i8**> [#uses=1]
48@_ZTS1C = linkonce_odr constant [3 x i8] c"1C\00"     ; <[3 x i8]*> [#uses=1]
49@_ZTVN10__cxxabiv117__class_type_infoE = external global i8* ; <i8**> [#uses=1]
50@_ZTS1A = linkonce_odr constant [3 x i8] c"1A\00"     ; <[3 x i8]*> [#uses=1]
51@_ZTI1A = linkonce_odr constant %0 { i8* bitcast (i8** getelementptr inbounds (i8*, i8** @_ZTVN10__cxxabiv117__class_type_infoE, i64 2) to i8*), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @_ZTS1A, i32 0, i32 0) } ; <%0*> [#uses=1]
52@_ZTS1B = linkonce_odr constant [3 x i8] c"1B\00"     ; <[3 x i8]*> [#uses=1]
53@_ZTI1B = linkonce_odr constant %0 { i8* bitcast (i8** getelementptr inbounds (i8*, i8** @_ZTVN10__cxxabiv117__class_type_infoE, i64 2) to i8*), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @_ZTS1B, i32 0, i32 0) } ; <%0*> [#uses=1]
54@_ZTI1C = linkonce_odr constant %1 { i8* bitcast (i8** getelementptr inbounds (i8*, i8** @_ZTVN10__cxxabiv121__vmi_class_type_infoE, i64 2) to i8*), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @_ZTS1C, i32 0, i32 0), i32 0, i32 2, i8* bitcast (%0* @_ZTI1A to i8*), i64 2, i8* bitcast (%0* @_ZTI1B to i8*), i64 2050 } ; <%1*> [#uses=1]
55@_ZTI1D = linkonce_odr constant %2 { i8* bitcast (i8** getelementptr inbounds (i8*, i8** @_ZTVN10__cxxabiv120__si_class_type_infoE, i64 2) to i8*), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @_ZTS1D, i32 0, i32 0), i8* bitcast (%1* @_ZTI1C to i8*) } ; <%2*> [#uses=1]
56@_ZTV1C = linkonce_odr constant [6 x i8*] [i8* null, i8* bitcast (%1* @_ZTI1C to i8*), i8* bitcast (i32 (%struct.C*)* @_ZN1C1fEv to i8*), i8* inttoptr (i64 -8 to i8*), i8* bitcast (%1* @_ZTI1C to i8*), i8* bitcast (i32 (%struct.C*)* @_ZThn8_N1C1fEv to i8*)] ; <[6 x i8*]*> [#uses=2]
57@_ZTV1B = linkonce_odr constant [3 x i8*] [i8* null, i8* bitcast (%0* @_ZTI1B to i8*), i8* bitcast (i32 (%struct.A*)* @_ZN1B1fEv to i8*)] ; <[3 x i8*]*> [#uses=1]
58@_ZTV1A = linkonce_odr constant [3 x i8*] [i8* null, i8* bitcast (%0* @_ZTI1A to i8*), i8* bitcast (i32 (%struct.A*)* @_ZN1A1fEv to i8*)] ; <[3 x i8*]*> [#uses=1]
59
60define i32 @_Z1gv() ssp {
61entry:
62  %d = alloca %struct.C, align 8                  ; <%struct.C*> [#uses=2]
63  call void @_ZN1DC1Ev(%struct.C* %d)
64  %call = call i32 @_Z1fP1D(%struct.C* %d)        ; <i32> [#uses=1]
65  %X = add i32 %call, 3
66  ret i32 %X
67}
68
69define linkonce_odr void @_ZN1DC1Ev(%struct.C* %this) inlinehint ssp align 2 {
70entry:
71  call void @_ZN1DC2Ev(%struct.C* %this)
72  ret void
73}
74
75define internal i32 @_Z1fP1D(%struct.C* %d) ssp {
76entry:
77  %0 = icmp eq %struct.C* %d, null                ; <i1> [#uses=1]
78  br i1 %0, label %cast.end, label %cast.notnull
79
80cast.notnull:                                     ; preds = %entry
81  %1 = bitcast %struct.C* %d to i8*               ; <i8*> [#uses=1]
82  %add.ptr = getelementptr i8, i8* %1, i64 8          ; <i8*> [#uses=1]
83  %2 = bitcast i8* %add.ptr to %struct.A*         ; <%struct.A*> [#uses=1]
84  br label %cast.end
85
86cast.end:                                         ; preds = %entry, %cast.notnull
87  %3 = phi %struct.A* [ %2, %cast.notnull ], [ null, %entry ] ; <%struct.A*> [#uses=2]
88  %4 = bitcast %struct.A* %3 to i32 (%struct.A*)*** ; <i32 (%struct.A*)***> [#uses=1]
89  %5 = load i32 (%struct.A*)**, i32 (%struct.A*)*** %4                ; <i32 (%struct.A*)**> [#uses=1]
90  %vfn = getelementptr inbounds i32 (%struct.A*)*, i32 (%struct.A*)** %5, i64 0 ; <i32 (%struct.A*)**> [#uses=1]
91  %6 = load i32 (%struct.A*)*, i32 (%struct.A*)** %vfn               ; <i32 (%struct.A*)*> [#uses=1]
92  %call = call i32 %6(%struct.A* %3)              ; <i32> [#uses=1]
93  ret i32 %call
94}
95
96define linkonce_odr i32 @_ZN1D1fEv(%struct.C* %this) ssp align 2 {
97entry:
98  ret i32 4
99}
100
101define linkonce_odr i32 @_ZThn8_N1D1fEv(%struct.C* %this) ssp {
102entry:
103  %0 = bitcast %struct.C* %this to i8*            ; <i8*> [#uses=1]
104  %1 = getelementptr inbounds i8, i8* %0, i64 -8      ; <i8*> [#uses=1]
105  %2 = bitcast i8* %1 to %struct.C*               ; <%struct.C*> [#uses=1]
106  %call = call i32 @_ZN1D1fEv(%struct.C* %2)      ; <i32> [#uses=1]
107  ret i32 %call
108}
109
110define linkonce_odr void @_ZN1DC2Ev(%struct.C* %this) inlinehint ssp align 2 {
111entry:
112  call void @_ZN1CC2Ev(%struct.C* %this)
113  %0 = bitcast %struct.C* %this to i8*            ; <i8*> [#uses=1]
114  %1 = getelementptr inbounds i8, i8* %0, i64 0       ; <i8*> [#uses=1]
115  %2 = bitcast i8* %1 to i8***                    ; <i8***> [#uses=1]
116  store i8** getelementptr inbounds ([6 x i8*], [6 x i8*]* @_ZTV1D, i64 0, i64 2), i8*** %2
117  %3 = bitcast %struct.C* %this to i8*            ; <i8*> [#uses=1]
118  %4 = getelementptr inbounds i8, i8* %3, i64 8       ; <i8*> [#uses=1]
119  %5 = bitcast i8* %4 to i8***                    ; <i8***> [#uses=1]
120  store i8** getelementptr inbounds ([6 x i8*], [6 x i8*]* @_ZTV1D, i64 0, i64 5), i8*** %5
121  ret void
122}
123
124define linkonce_odr void @_ZN1CC2Ev(%struct.C* %this) inlinehint ssp align 2 {
125entry:
126  %0 = bitcast %struct.C* %this to %struct.A*     ; <%struct.A*> [#uses=1]
127  call void @_ZN1AC2Ev(%struct.A* %0)
128  %1 = bitcast %struct.C* %this to i8*            ; <i8*> [#uses=1]
129  %2 = getelementptr inbounds i8, i8* %1, i64 8       ; <i8*> [#uses=1]
130  %3 = bitcast i8* %2 to %struct.A*               ; <%struct.A*> [#uses=1]
131  call void @_ZN1BC2Ev(%struct.A* %3)
132  %4 = bitcast %struct.C* %this to i8*            ; <i8*> [#uses=1]
133  %5 = getelementptr inbounds i8, i8* %4, i64 0       ; <i8*> [#uses=1]
134  %6 = bitcast i8* %5 to i8***                    ; <i8***> [#uses=1]
135  store i8** getelementptr inbounds ([6 x i8*], [6 x i8*]* @_ZTV1C, i64 0, i64 2), i8*** %6
136  %7 = bitcast %struct.C* %this to i8*            ; <i8*> [#uses=1]
137  %8 = getelementptr inbounds i8, i8* %7, i64 8       ; <i8*> [#uses=1]
138  %9 = bitcast i8* %8 to i8***                    ; <i8***> [#uses=1]
139  store i8** getelementptr inbounds ([6 x i8*], [6 x i8*]* @_ZTV1C, i64 0, i64 5), i8*** %9
140  ret void
141}
142
143define linkonce_odr i32 @_ZN1C1fEv(%struct.C* %this) ssp align 2 {
144entry:
145  ret i32 3
146}
147
148define linkonce_odr i32 @_ZThn8_N1C1fEv(%struct.C* %this) {
149entry:
150  %0 = bitcast %struct.C* %this to i8*            ; <i8*> [#uses=1]
151  %1 = getelementptr inbounds i8, i8* %0, i64 -8      ; <i8*> [#uses=1]
152  %2 = bitcast i8* %1 to %struct.C*               ; <%struct.C*> [#uses=1]
153  %call = call i32 @_ZN1C1fEv(%struct.C* %2)      ; <i32> [#uses=1]
154  ret i32 %call
155}
156
157define linkonce_odr void @_ZN1AC2Ev(%struct.A* %this) inlinehint ssp align 2 {
158entry:
159  %0 = bitcast %struct.A* %this to i8*            ; <i8*> [#uses=1]
160  %1 = getelementptr inbounds i8, i8* %0, i64 0       ; <i8*> [#uses=1]
161  %2 = bitcast i8* %1 to i8***                    ; <i8***> [#uses=1]
162  store i8** getelementptr inbounds ([3 x i8*], [3 x i8*]* @_ZTV1A, i64 0, i64 2), i8*** %2
163  ret void
164}
165
166define linkonce_odr void @_ZN1BC2Ev(%struct.A* %this) inlinehint ssp align 2 {
167entry:
168  %0 = bitcast %struct.A* %this to i8*            ; <i8*> [#uses=1]
169  %1 = getelementptr inbounds i8, i8* %0, i64 0       ; <i8*> [#uses=1]
170  %2 = bitcast i8* %1 to i8***                    ; <i8***> [#uses=1]
171  store i8** getelementptr inbounds ([3 x i8*], [3 x i8*]* @_ZTV1B, i64 0, i64 2), i8*** %2
172  ret void
173}
174
175define linkonce_odr i32 @_ZN1B1fEv(%struct.A* %this) ssp align 2 {
176entry:
177  ret i32 2
178}
179
180define linkonce_odr i32 @_ZN1A1fEv(%struct.A* %this) ssp align 2 {
181entry:
182  ret i32 1
183}
184