1 ; RUN: opt < %s -rewrite-statepoints-for-gc -S | FileCheck %s
2 ; RUN: opt < %s -passes=rewrite-statepoints-for-gc -S | FileCheck %s
5 define i64 addrspace(1)* @test(<2 x i64 addrspace(1)*> %vec, i32 %idx) gc "statepoint-example" {
7 ; CHECK: extractelement
8 ; CHECK: extractelement
11 ; CHECK-DAG: ; (%base_ee, %obj)
13 ; CHECK-DAG: ; (%base_ee, %base_ee)
14 ; Note that the second extractelement is actually redundant here. A correct output would
15 ; be to reuse the existing obj as a base since it is actually a base pointer.
17 %obj = extractelement <2 x i64 addrspace(1)*> %vec, i32 %idx
18 call void @do_safepoint() [ "deopt"() ]
19 ret i64 addrspace(1)* %obj
22 define i64 addrspace(1)* @test2(<2 x i64 addrspace(1)*>* %ptr, i1 %cnd, i32 %idx1, i32 %idx2) gc "statepoint-example" {
25 br i1 %cnd, label %taken, label %untaken
27 taken: ; preds = %entry
28 %obja = load <2 x i64 addrspace(1)*>, <2 x i64 addrspace(1)*>* %ptr
31 untaken: ; preds = %entry
32 %objb = load <2 x i64 addrspace(1)*>, <2 x i64 addrspace(1)*>* %ptr
35 merge: ; preds = %untaken, %taken
36 %vec = phi <2 x i64 addrspace(1)*> [ %obja, %taken ], [ %objb, %untaken ]
37 br i1 %cnd, label %taken2, label %untaken2
39 taken2: ; preds = %merge
40 %obj0 = extractelement <2 x i64 addrspace(1)*> %vec, i32 %idx1
43 untaken2: ; preds = %merge
44 %obj1 = extractelement <2 x i64 addrspace(1)*> %vec, i32 %idx2
47 merge2: ; preds = %untaken2, %taken2
48 ; CHECK-LABEL: merge2:
49 ; CHECK: %obj.base = phi i64 addrspace(1)*
50 ; CHECK: %obj = phi i64 addrspace(1)*
53 ; CHECK-DAG: ; (%obj.base, %obj)
55 ; CHECK-DAG: ; (%obj.base, %obj.base)
56 %obj = phi i64 addrspace(1)* [ %obj0, %taken2 ], [ %obj1, %untaken2 ]
57 call void @do_safepoint() [ "deopt"() ]
58 ret i64 addrspace(1)* %obj
61 define i64 addrspace(1)* @test3(i64 addrspace(1)* %ptr) gc "statepoint-example" {
63 ; CHECK: insertelement
64 ; CHECK: extractelement
67 ; CHECK-DAG: (%obj.base, %obj)
69 %vec = insertelement <2 x i64 addrspace(1)*> undef, i64 addrspace(1)* %ptr, i32 0
70 %obj = extractelement <2 x i64 addrspace(1)*> %vec, i32 0
71 call void @do_safepoint() [ "deopt"() ]
72 ret i64 addrspace(1)* %obj
75 define i64 addrspace(1)* @test4(i64 addrspace(1)* %ptr) gc "statepoint-example" {
79 ; CHECK-DAG: ; (%obj.base, %obj)
80 ; When we can optimize an extractelement from a known
81 ; index and avoid introducing new base pointer instructions
83 %derived = getelementptr i64, i64 addrspace(1)* %ptr, i64 16
84 %veca = insertelement <2 x i64 addrspace(1)*> undef, i64 addrspace(1)* %derived, i32 0
85 %vec = insertelement <2 x i64 addrspace(1)*> %veca, i64 addrspace(1)* %ptr, i32 1
86 %obj = extractelement <2 x i64 addrspace(1)*> %vec, i32 0
87 call void @do_safepoint() [ "deopt"() ]
88 ret i64 addrspace(1)* %obj
91 declare void @use(i64 addrspace(1)*) "gc-leaf-function"
92 declare void @use_vec(<4 x i64 addrspace(1)*>) "gc-leaf-function"
94 define void @test5(i1 %cnd, i64 addrspace(1)* %obj) gc "statepoint-example" {
97 ; CHECK-DAG: (%bdv.base, %bdv)
98 ; When we fundementally have to duplicate
100 %gep = getelementptr i64, i64 addrspace(1)* %obj, i64 1
101 %vec = insertelement <2 x i64 addrspace(1)*> undef, i64 addrspace(1)* %gep, i32 0
102 %bdv = extractelement <2 x i64 addrspace(1)*> %vec, i32 0
103 call void @do_safepoint() [ "deopt"(i32 0, i32 -1, i32 0, i32 0, i32 0) ]
104 call void @use(i64 addrspace(1)* %bdv)
108 define void @test6(i1 %cnd, i64 addrspace(1)* %obj, i64 %idx) gc "statepoint-example" {
109 ; CHECK-LABEL: @test6
110 ; CHECK: %gep = getelementptr i64, i64 addrspace(1)* %obj, i64 1
111 ; CHECK: %vec.base = insertelement <2 x i64 addrspace(1)*> zeroinitializer, i64 addrspace(1)* %obj, i32 0, !is_base_value !0
112 ; CHECK: %vec = insertelement <2 x i64 addrspace(1)*> undef, i64 addrspace(1)* %gep, i32 0
113 ; CHECK: %bdv.base = extractelement <2 x i64 addrspace(1)*> %vec.base, i64 %idx, !is_base_value !0
114 ; CHECK: %bdv = extractelement <2 x i64 addrspace(1)*> %vec, i64 %idx
115 ; CHECK: gc.statepoint
117 ; CHECK-DAG: (%bdv.base, %bdv)
118 ; A more complicated example involving vector and scalar bases.
119 ; This is derived from a failing test case when we didn't have correct
120 ; insertelement handling.
122 %gep = getelementptr i64, i64 addrspace(1)* %obj, i64 1
123 %vec = insertelement <2 x i64 addrspace(1)*> undef, i64 addrspace(1)* %gep, i32 0
124 %bdv = extractelement <2 x i64 addrspace(1)*> %vec, i64 %idx
125 call void @do_safepoint() [ "deopt"(i32 0, i32 -1, i32 0, i32 0, i32 0) ]
126 call void @use(i64 addrspace(1)* %bdv)
130 define i64 addrspace(1)* @test7(i1 %cnd, i64 addrspace(1)* %obj, i64 addrspace(1)* %obj2) gc "statepoint-example" {
131 ; CHECK-LABEL: @test7
133 %vec = insertelement <2 x i64 addrspace(1)*> undef, i64 addrspace(1)* %obj2, i32 0
136 merge1: ; preds = %merge1, %entry
137 ; CHECK-LABEL: merge1:
143 %vec2 = phi <2 x i64 addrspace(1)*> [ %vec, %entry ], [ %vec3, %merge1 ]
144 %gep = getelementptr i64, i64 addrspace(1)* %obj2, i64 1
145 %vec3 = insertelement <2 x i64 addrspace(1)*> undef, i64 addrspace(1)* %gep, i32 0
146 br i1 %cnd, label %merge1, label %next1
148 next1: ; preds = %merge1
149 ; CHECK-LABEL: next1:
152 %bdv = extractelement <2 x i64 addrspace(1)*> %vec2, i32 0
155 merge: ; preds = %merge, %next1
156 ; CHECK-LABEL: merge:
159 ; CHECK: gc.statepoint
161 ; CHECK-DAG: (%objb.base, %objb)
162 %objb = phi i64 addrspace(1)* [ %obj, %next1 ], [ %bdv, %merge ]
163 br i1 %cnd, label %merge, label %next
165 next: ; preds = %merge
166 call void @do_safepoint() [ "deopt"(i32 0, i32 -1, i32 0, i32 0, i32 0) ]
167 ret i64 addrspace(1)* %objb
170 ; identify base for shufflevector
171 define void @test8(i64 addrspace(1)* %obj, i64 %idx) gc "statepoint-example" {
172 ; CHECK-LABEL: @test8
173 ; CHECK: %gep = getelementptr i64, i64 addrspace(1)* %obj, i64 1
174 ; CHECK: %gep2 = getelementptr i64, i64 addrspace(1)* %obj, i64 2
175 ; CHECK: %vec1.base = insertelement <4 x i64 addrspace(1)*> zeroinitializer, i64 addrspace(1)* %obj, i32 0, !is_base_value !0
176 ; CHECK: %vec1 = insertelement <4 x i64 addrspace(1)*> undef, i64 addrspace(1)* %gep, i32 0
177 ; CHECK: %vec2.base = insertelement <4 x i64 addrspace(1)*> zeroinitializer, i64 addrspace(1)* %obj, i32 2, !is_base_value !0
178 ; CHECK: %vec2 = insertelement <4 x i64 addrspace(1)*> undef, i64 addrspace(1)* %gep2, i32 2
179 ; CHECK: %vec.base = shufflevector <4 x i64 addrspace(1)*> %vec1.base, <4 x i64 addrspace(1)*> %vec2.base, <2 x i32> <i32 0, i32 2>, !is_base_value !0
180 ; CHECK: %vec = shufflevector <4 x i64 addrspace(1)*> %vec1, <4 x i64 addrspace(1)*> %vec2, <2 x i32> <i32 0, i32 2>
181 ; CHECK: %bdv.base = extractelement <2 x i64 addrspace(1)*> %vec.base, i64 %idx, !is_base_value !0
182 ; CHECK: %bdv = extractelement <2 x i64 addrspace(1)*> %vec, i64 %idx
183 ; CHECK: gc.statepoint
185 ; CHECK-DAG: (%bdv.base, %bdv)
187 %gep = getelementptr i64, i64 addrspace(1)* %obj, i64 1
188 %gep2 = getelementptr i64, i64 addrspace(1)* %obj, i64 2
189 %vec1 = insertelement <4 x i64 addrspace(1)*> undef, i64 addrspace(1)* %gep, i32 0
190 %vec2 = insertelement <4 x i64 addrspace(1)*> undef, i64 addrspace(1)* %gep2, i32 2
191 %vec = shufflevector <4 x i64 addrspace(1)*> %vec1, <4 x i64 addrspace(1)*> %vec2, <2 x i32> <i32 0, i32 2>
192 %bdv = extractelement <2 x i64 addrspace(1)*> %vec, i64 %idx
193 call void @do_safepoint() [ "deopt"(i32 0, i32 -1, i32 0, i32 0, i32 0) ]
194 call void @use(i64 addrspace(1)* %bdv)
198 ; Since the same 'base' vector is used in the shuffle operands, we do not need
199 ; create a shufflevector base.
200 define void @test9(<4 x i64 addrspace(1)*> %vec1, i64 %idx) gc "statepoint-example" {
201 ; CHECK-LABEL: @test9
202 ; CHECK: %vec = shufflevector <4 x i64 addrspace(1)*> %vec1, <4 x i64 addrspace(1)*> %vec1, <2 x i32> <i32 0, i32 2>
203 ; CHECK: %base_ee = extractelement <4 x i64 addrspace(1)*> %vec1, i64 %idx, !is_base_value !0
204 ; CHECK: %bdv = extractelement <2 x i64 addrspace(1)*> %vec, i64 %idx
205 ; CHECK: gc.statepoint
207 ; CHECK-DAG: (%base_ee, %bdv)
209 ; shrinking vec1 into vec
210 %vec = shufflevector <4 x i64 addrspace(1)*> %vec1, <4 x i64 addrspace(1)*> %vec1, <2 x i32> <i32 0, i32 2>
211 %bdv = extractelement <2 x i64 addrspace(1)*> %vec, i64 %idx
212 call void @do_safepoint() [ "deopt"(i32 0, i32 -1, i32 0, i32 0, i32 0) ]
213 call void @use(i64 addrspace(1)* %bdv)
217 ; vector operand of shufflevector is a phi
218 define i64 addrspace(1)* @test10(i1 %cnd, i64 addrspace(1)* %obj, i64 addrspace(1)* %obj2) gc "statepoint-example" {
219 ; CHECK-LABEL: @test10
221 %vec1 = insertelement <4 x i64 addrspace(1)*> undef, i64 addrspace(1)* %obj, i32 0
222 br i1 %cnd, label %here, label %merge
225 %vec2 = insertelement <4 x i64 addrspace(1)*> undef, i64 addrspace(1)* %obj2, i32 2
228 merge: ; preds = %merge, %entry, %here
229 ; CHECK-LABEL: merge:
230 ; CHECK: %vec.base = phi <4 x i64 addrspace(1)*> [ %vec1.base, %entry ], [ %vec2.base, %here ], [ %vec3.base, %merge ], !is_base_value !0
232 ; CHECK: vec3.base = shufflevector <4 x i64 addrspace(1)*> %vec.base, <4 x i64 addrspace(1)*> %vec.base
236 %vec = phi <4 x i64 addrspace(1)*> [ %vec1, %entry ], [ %vec2, %here], [ %vec3, %merge]
237 %vec3 = shufflevector <4 x i64 addrspace(1)*> %vec, <4 x i64 addrspace(1)*> %vec, <4 x i32> <i32 2, i32 0, i32 1, i32 3>
238 %bdv = extractelement <4 x i64 addrspace(1)*> %vec3, i32 0
239 br i1 %cnd, label %merge, label %next
243 ; CHECK: gc.statepoint
245 ; CHECK-DAG: (%bdv.base, %bdv)
246 call void @do_safepoint() [ "deopt"(i32 0, i32 -1, i32 0, i32 0, i32 0) ]
247 ret i64 addrspace(1)* %bdv
249 declare void @do_safepoint()
251 define void @test11(<4 x i64 addrspace(1)*> %vec1) gc "statepoint-example" {
252 ; CHECK-LABEL: @test11(
253 ; CHECK: @llvm.experimental.gc.statepoint.p0f_isVoidf{{.*}}<4 x i64 addrspace(1)*> %vec1)
254 ; CHECK: %vec1.relocated = call coldcc <4 x i8 addrspace(1)*> @llvm.experimental.gc.relocate.v4p1i8
255 ; CHECK: %vec1.relocated.casted = bitcast <4 x i8 addrspace(1)*> %vec1.relocated to <4 x i64 addrspace(1)*>
256 ; CHECK: %vec2.remat = getelementptr i64, <4 x i64 addrspace(1)*> %vec1.relocated.casted, i32 1024
257 ; CHECK: call void @use_vec(<4 x i64 addrspace(1)*> %vec2.remat)
259 %vec2 = getelementptr i64, <4 x i64 addrspace(1)*> %vec1, i32 1024
260 call void @do_safepoint() [ "deopt"(i32 0, i32 -1, i32 0, i32 0, i32 0) ]
261 call void @use_vec(<4 x i64 addrspace(1) *> %vec2)
265 declare <4 x i64 addrspace(1)*> @def_vec() "gc-leaf-function"
267 define void @test12(<4 x i64 addrspace(1)*> %vec1) gc "statepoint-example" {
268 ; CHECK-LABEL: @test12(
269 ; CHECK: @llvm.experimental.gc.statepoint.p0f_isVoidf{{.*}}<4 x i64 addrspace(1)*> %vec)
270 ; CHECK-NEXT: %vec.relocated = call coldcc <4 x i8 addrspace(1)*> @llvm.experimental.gc.relocate.v4p1i8(
271 ; CHECK-NEXT: %vec.relocated.casted = bitcast <4 x i8 addrspace(1)*> %vec.relocated to <4 x i64 addrspace(1)*>
272 ; CHECK-NEXT: call void @use_vec(<4 x i64 addrspace(1)*> %vec.relocated.casted)
273 ; CHECK-NEXT: ret void
275 %vec = call <4 x i64 addrspace(1)*> @def_vec()
276 call void @do_safepoint() [ "deopt"() ]
277 call void @use_vec(<4 x i64 addrspace(1)*> %vec)