1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -S -instcombine < %s | FileCheck %s
4 declare void @usei32(i32)
6 ; If we have a phi of extractvalues, we can sink it,
7 ; Here, we only need a PHI for extracted values.
8 define i32 @test0({ i32, i32 } %agg_left, { i32, i32 } %agg_right, i1 %c) {
11 ; CHECK-NEXT: br i1 [[C:%.*]], label [[LEFT:%.*]], label [[RIGHT:%.*]]
13 ; CHECK-NEXT: br label [[END:%.*]]
15 ; CHECK-NEXT: br label [[END]]
17 ; CHECK-NEXT: [[AGG_LEFT_PN:%.*]] = phi { i32, i32 } [ [[AGG_LEFT:%.*]], [[LEFT]] ], [ [[AGG_RIGHT:%.*]], [[RIGHT]] ]
18 ; CHECK-NEXT: [[R:%.*]] = extractvalue { i32, i32 } [[AGG_LEFT_PN]], 0
19 ; CHECK-NEXT: ret i32 [[R]]
22 br i1 %c, label %left, label %right
25 %i0 = extractvalue { i32, i32 } %agg_left, 0
29 %i1 = extractvalue { i32, i32 } %agg_right, 0
33 %r = phi i32 [ %i0, %left ], [ %i1, %right ]
37 ; But only if the extractvalues have no extra uses
38 define i32 @test1_extrause0({ i32, i32 } %agg_left, { i32, i32 } %agg_right, i1 %c) {
39 ; CHECK-LABEL: @test1_extrause0(
41 ; CHECK-NEXT: br i1 [[C:%.*]], label [[LEFT:%.*]], label [[RIGHT:%.*]]
43 ; CHECK-NEXT: [[I0:%.*]] = extractvalue { i32, i32 } [[AGG_LEFT:%.*]], 0
44 ; CHECK-NEXT: call void @usei32(i32 [[I0]])
45 ; CHECK-NEXT: br label [[END:%.*]]
47 ; CHECK-NEXT: [[I1:%.*]] = extractvalue { i32, i32 } [[AGG_RIGHT:%.*]], 0
48 ; CHECK-NEXT: br label [[END]]
50 ; CHECK-NEXT: [[R:%.*]] = phi i32 [ [[I0]], [[LEFT]] ], [ [[I1]], [[RIGHT]] ]
51 ; CHECK-NEXT: ret i32 [[R]]
54 br i1 %c, label %left, label %right
57 %i0 = extractvalue { i32, i32 } %agg_left, 0
58 call void @usei32(i32 %i0)
62 %i1 = extractvalue { i32, i32 } %agg_right, 0
66 %r = phi i32 [ %i0, %left ], [ %i1, %right ]
69 define i32 @test2_extrause1({ i32, i32 } %agg_left, { i32, i32 } %agg_right, i1 %c) {
70 ; CHECK-LABEL: @test2_extrause1(
72 ; CHECK-NEXT: br i1 [[C:%.*]], label [[LEFT:%.*]], label [[RIGHT:%.*]]
74 ; CHECK-NEXT: [[I0:%.*]] = extractvalue { i32, i32 } [[AGG_LEFT:%.*]], 0
75 ; CHECK-NEXT: br label [[END:%.*]]
77 ; CHECK-NEXT: [[I1:%.*]] = extractvalue { i32, i32 } [[AGG_RIGHT:%.*]], 0
78 ; CHECK-NEXT: call void @usei32(i32 [[I1]])
79 ; CHECK-NEXT: br label [[END]]
81 ; CHECK-NEXT: [[R:%.*]] = phi i32 [ [[I0]], [[LEFT]] ], [ [[I1]], [[RIGHT]] ]
82 ; CHECK-NEXT: ret i32 [[R]]
85 br i1 %c, label %left, label %right
88 %i0 = extractvalue { i32, i32 } %agg_left, 0
92 %i1 = extractvalue { i32, i32 } %agg_right, 0
93 call void @usei32(i32 %i1)
97 %r = phi i32 [ %i0, %left ], [ %i1, %right ]
100 define i32 @test3_extrause2({ i32, i32 } %agg_left, { i32, i32 } %agg_right, i1 %c) {
101 ; CHECK-LABEL: @test3_extrause2(
103 ; CHECK-NEXT: br i1 [[C:%.*]], label [[LEFT:%.*]], label [[RIGHT:%.*]]
105 ; CHECK-NEXT: [[I0:%.*]] = extractvalue { i32, i32 } [[AGG_LEFT:%.*]], 0
106 ; CHECK-NEXT: call void @usei32(i32 [[I0]])
107 ; CHECK-NEXT: br label [[END:%.*]]
109 ; CHECK-NEXT: [[I1:%.*]] = extractvalue { i32, i32 } [[AGG_RIGHT:%.*]], 0
110 ; CHECK-NEXT: call void @usei32(i32 [[I1]])
111 ; CHECK-NEXT: br label [[END]]
113 ; CHECK-NEXT: [[R:%.*]] = phi i32 [ [[I0]], [[LEFT]] ], [ [[I1]], [[RIGHT]] ]
114 ; CHECK-NEXT: ret i32 [[R]]
117 br i1 %c, label %left, label %right
120 %i0 = extractvalue { i32, i32 } %agg_left, 0
121 call void @usei32(i32 %i0)
125 %i1 = extractvalue { i32, i32 } %agg_right, 0
126 call void @usei32(i32 %i1)
130 %r = phi i32 [ %i0, %left ], [ %i1, %right ]
134 ; But the indicies must match
135 define i32 @test4({ i32, i32 } %agg_left, { i32, i32 } %agg_right, i1 %c) {
136 ; CHECK-LABEL: @test4(
138 ; CHECK-NEXT: br i1 [[C:%.*]], label [[LEFT:%.*]], label [[RIGHT:%.*]]
140 ; CHECK-NEXT: [[I0:%.*]] = extractvalue { i32, i32 } [[AGG_LEFT:%.*]], 0
141 ; CHECK-NEXT: br label [[END:%.*]]
143 ; CHECK-NEXT: [[I1:%.*]] = extractvalue { i32, i32 } [[AGG_RIGHT:%.*]], 1
144 ; CHECK-NEXT: br label [[END]]
146 ; CHECK-NEXT: [[R:%.*]] = phi i32 [ [[I0]], [[LEFT]] ], [ [[I1]], [[RIGHT]] ]
147 ; CHECK-NEXT: ret i32 [[R]]
150 br i1 %c, label %left, label %right
153 %i0 = extractvalue { i32, i32 } %agg_left, 0
157 %i1 = extractvalue { i32, i32 } %agg_right, 1
161 %r = phi i32 [ %i0, %left ], [ %i1, %right ]
165 ; More complex aggregates are fine, too, as long as indicies match.
166 define i32 @test5({{ i32, i32 }, { i32, i32 }} %agg_left, {{ i32, i32 }, { i32, i32 }} %agg_right, i1 %c) {
167 ; CHECK-LABEL: @test5(
169 ; CHECK-NEXT: br i1 [[C:%.*]], label [[LEFT:%.*]], label [[RIGHT:%.*]]
171 ; CHECK-NEXT: br label [[END:%.*]]
173 ; CHECK-NEXT: br label [[END]]
175 ; CHECK-NEXT: [[AGG_LEFT_PN:%.*]] = phi { { i32, i32 }, { i32, i32 } } [ [[AGG_LEFT:%.*]], [[LEFT]] ], [ [[AGG_RIGHT:%.*]], [[RIGHT]] ]
176 ; CHECK-NEXT: [[R:%.*]] = extractvalue { { i32, i32 }, { i32, i32 } } [[AGG_LEFT_PN]], 0, 0
177 ; CHECK-NEXT: ret i32 [[R]]
180 br i1 %c, label %left, label %right
183 %i0 = extractvalue {{ i32, i32 }, { i32, i32 }} %agg_left, 0, 0
187 %i1 = extractvalue {{ i32, i32 }, { i32, i32 }} %agg_right, 0, 0
191 %r = phi i32 [ %i0, %left ], [ %i1, %right ]
195 ; The indicies must fully match, on all levels.
196 define i32 @test6({{ i32, i32 }, { i32, i32 }} %agg_left, {{ i32, i32 }, { i32, i32 }} %agg_right, i1 %c) {
197 ; CHECK-LABEL: @test6(
199 ; CHECK-NEXT: br i1 [[C:%.*]], label [[LEFT:%.*]], label [[RIGHT:%.*]]
201 ; CHECK-NEXT: [[I0:%.*]] = extractvalue { { i32, i32 }, { i32, i32 } } [[AGG_LEFT:%.*]], 0, 0
202 ; CHECK-NEXT: br label [[END:%.*]]
204 ; CHECK-NEXT: [[I1:%.*]] = extractvalue { { i32, i32 }, { i32, i32 } } [[AGG_RIGHT:%.*]], 0, 1
205 ; CHECK-NEXT: br label [[END]]
207 ; CHECK-NEXT: [[R:%.*]] = phi i32 [ [[I0]], [[LEFT]] ], [ [[I1]], [[RIGHT]] ]
208 ; CHECK-NEXT: ret i32 [[R]]
211 br i1 %c, label %left, label %right
214 %i0 = extractvalue {{ i32, i32 }, { i32, i32 }} %agg_left, 0, 0
218 %i1 = extractvalue {{ i32, i32 }, { i32, i32 }} %agg_right, 0, 1
222 %r = phi i32 [ %i0, %left ], [ %i1, %right ]
225 define i32 @test7({{ i32, i32 }, { i32, i32 }} %agg_left, {{ i32, i32 }, { i32, i32 }} %agg_right, i1 %c) {
226 ; CHECK-LABEL: @test7(
228 ; CHECK-NEXT: br i1 [[C:%.*]], label [[LEFT:%.*]], label [[RIGHT:%.*]]
230 ; CHECK-NEXT: [[I0:%.*]] = extractvalue { { i32, i32 }, { i32, i32 } } [[AGG_LEFT:%.*]], 0, 0
231 ; CHECK-NEXT: br label [[END:%.*]]
233 ; CHECK-NEXT: [[I1:%.*]] = extractvalue { { i32, i32 }, { i32, i32 } } [[AGG_RIGHT:%.*]], 1, 0
234 ; CHECK-NEXT: br label [[END]]
236 ; CHECK-NEXT: [[R:%.*]] = phi i32 [ [[I0]], [[LEFT]] ], [ [[I1]], [[RIGHT]] ]
237 ; CHECK-NEXT: ret i32 [[R]]
240 br i1 %c, label %left, label %right
243 %i0 = extractvalue {{ i32, i32 }, { i32, i32 }} %agg_left, 0, 0
247 %i1 = extractvalue {{ i32, i32 }, { i32, i32 }} %agg_right, 1, 0
251 %r = phi i32 [ %i0, %left ], [ %i1, %right ]
254 define i32 @test8({{ i32, i32 }, { i32, i32 }} %agg_left, {{ i32, i32 }, { i32, i32 }} %agg_right, i1 %c) {
255 ; CHECK-LABEL: @test8(
257 ; CHECK-NEXT: br i1 [[C:%.*]], label [[LEFT:%.*]], label [[RIGHT:%.*]]
259 ; CHECK-NEXT: [[I0:%.*]] = extractvalue { { i32, i32 }, { i32, i32 } } [[AGG_LEFT:%.*]], 0, 0
260 ; CHECK-NEXT: br label [[END:%.*]]
262 ; CHECK-NEXT: [[I1:%.*]] = extractvalue { { i32, i32 }, { i32, i32 } } [[AGG_RIGHT:%.*]], 1, 1
263 ; CHECK-NEXT: br label [[END]]
265 ; CHECK-NEXT: [[R:%.*]] = phi i32 [ [[I0]], [[LEFT]] ], [ [[I1]], [[RIGHT]] ]
266 ; CHECK-NEXT: ret i32 [[R]]
269 br i1 %c, label %left, label %right
272 %i0 = extractvalue {{ i32, i32 }, { i32, i32 }} %agg_left, 0, 0
276 %i1 = extractvalue {{ i32, i32 }, { i32, i32 }} %agg_right, 1, 1
280 %r = phi i32 [ %i0, %left ], [ %i1, %right ]
284 ; Also, unlike PHI-of-insertvalues, here the base aggregates of extractvalue
285 ; can have different types, and just checking the indicies is not enough.
286 define i32 @test9({ i32, i32 } %agg_left, { i32, { i32, i32 } } %agg_right, i1 %c) {
287 ; CHECK-LABEL: @test9(
289 ; CHECK-NEXT: br i1 [[C:%.*]], label [[LEFT:%.*]], label [[RIGHT:%.*]]
291 ; CHECK-NEXT: [[I0:%.*]] = extractvalue { i32, i32 } [[AGG_LEFT:%.*]], 0
292 ; CHECK-NEXT: br label [[END:%.*]]
294 ; CHECK-NEXT: [[I1:%.*]] = extractvalue { i32, { i32, i32 } } [[AGG_RIGHT:%.*]], 0
295 ; CHECK-NEXT: br label [[END]]
297 ; CHECK-NEXT: [[R:%.*]] = phi i32 [ [[I0]], [[LEFT]] ], [ [[I1]], [[RIGHT]] ]
298 ; CHECK-NEXT: ret i32 [[R]]
301 br i1 %c, label %left, label %right
304 %i0 = extractvalue { i32, i32 } %agg_left, 0
308 %i1 = extractvalue { i32, { i32, i32 } } %agg_right, 0
312 %r = phi i32 [ %i0, %left ], [ %i1, %right ]
316 ; It is fine if there are multiple uses of the PHI's value, as long as they are all in the PHI node itself
317 define i32 @test10({ i32, i32 } %agg_left, { i32, i32 } %agg_right, i1 %c0, i1 %c1) {
318 ; CHECK-LABEL: @test10(
320 ; CHECK-NEXT: br i1 [[C0:%.*]], label [[END:%.*]], label [[DISPATCH:%.*]]
322 ; CHECK-NEXT: br i1 [[C1:%.*]], label [[LEFT:%.*]], label [[RIGHT:%.*]]
324 ; CHECK-NEXT: br label [[END]]
326 ; CHECK-NEXT: br label [[END]]
328 ; CHECK-NEXT: [[AGG_LEFT_PN:%.*]] = phi { i32, i32 } [ [[AGG_LEFT:%.*]], [[ENTRY:%.*]] ], [ [[AGG_LEFT]], [[LEFT]] ], [ [[AGG_RIGHT:%.*]], [[RIGHT]] ]
329 ; CHECK-NEXT: [[R:%.*]] = extractvalue { i32, i32 } [[AGG_LEFT_PN]], 0
330 ; CHECK-NEXT: ret i32 [[R]]
333 %i0 = extractvalue { i32, i32 } %agg_left, 0
334 %i1 = extractvalue { i32, i32 } %agg_right, 0
335 br i1 %c0, label %end, label %dispatch
338 br i1 %c1, label %left, label %right
347 %r = phi i32 [ %i0, %entry ], [ %i0, %left ], [ %i1, %right ]
350 ; Which isn't the case here, there is a legitimate external use.
351 define i32 @test11({ i32, i32 } %agg_left, { i32, i32 } %agg_right, i1 %c0, i1 %c1) {
352 ; CHECK-LABEL: @test11(
354 ; CHECK-NEXT: [[I0:%.*]] = extractvalue { i32, i32 } [[AGG_LEFT:%.*]], 0
355 ; CHECK-NEXT: [[I1:%.*]] = extractvalue { i32, i32 } [[AGG_RIGHT:%.*]], 0
356 ; CHECK-NEXT: call void @usei32(i32 [[I0]])
357 ; CHECK-NEXT: br i1 [[C0:%.*]], label [[END:%.*]], label [[DISPATCH:%.*]]
359 ; CHECK-NEXT: br i1 [[C1:%.*]], label [[LEFT:%.*]], label [[RIGHT:%.*]]
361 ; CHECK-NEXT: br label [[END]]
363 ; CHECK-NEXT: br label [[END]]
365 ; CHECK-NEXT: [[R:%.*]] = phi i32 [ [[I0]], [[ENTRY:%.*]] ], [ [[I0]], [[LEFT]] ], [ [[I1]], [[RIGHT]] ]
366 ; CHECK-NEXT: ret i32 [[R]]
369 %i0 = extractvalue { i32, i32 } %agg_left, 0
370 %i1 = extractvalue { i32, i32 } %agg_right, 0
371 call void @usei32(i32 %i0)
372 br i1 %c0, label %end, label %dispatch
375 br i1 %c1, label %left, label %right
384 %r = phi i32 [ %i0, %entry ], [ %i0, %left ], [ %i1, %right ]