Skip to content

Commit e74e8b2

Browse files
committed
[NFC][InstCombine] Add tests for PHI merging/aggregate reconstruction (PR47060)
We should be able to see that the new aggregate we have produced is identical to the source aggregate from which we've extracted the elements that we used to form a new aggregate. This happens (a lot) in clang C++ exception code on unwind branch.
1 parent 9b211a5 commit e74e8b2

File tree

2 files changed

+657
-0
lines changed

2 files changed

+657
-0
lines changed
Lines changed: 328 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,328 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt -S -instcombine < %s | FileCheck %s
3+
4+
declare void @foo()
5+
declare void @bar()
6+
declare void @baz()
7+
8+
declare void @usei32(i32)
9+
declare void @usei32i32agg({ i32, i32 })
10+
11+
; Most basic test - we explode the original aggregate into it's elements,
12+
; and then merge them back together exactly the way they were.
13+
; We should just return the source aggregate.
14+
define { i32, i32 } @test0({ i32, i32 } %srcagg) {
15+
; CHECK-LABEL: @test0(
16+
; CHECK-NEXT: [[I0:%.*]] = extractvalue { i32, i32 } [[SRCAGG:%.*]], 0
17+
; CHECK-NEXT: [[I1:%.*]] = extractvalue { i32, i32 } [[SRCAGG]], 1
18+
; CHECK-NEXT: [[I2:%.*]] = insertvalue { i32, i32 } undef, i32 [[I0]], 0
19+
; CHECK-NEXT: [[I3:%.*]] = insertvalue { i32, i32 } [[I2]], i32 [[I1]], 1
20+
; CHECK-NEXT: ret { i32, i32 } [[I3]]
21+
;
22+
%i0 = extractvalue { i32, i32 } %srcagg, 0
23+
%i1 = extractvalue { i32, i32 } %srcagg, 1
24+
%i2 = insertvalue { i32, i32 } undef, i32 %i0, 0
25+
%i3 = insertvalue { i32, i32 } %i2, i32 %i1, 1
26+
ret { i32, i32 } %i3
27+
}
28+
29+
; Arrays are still aggregates
30+
define [2 x i32] @test1([2 x i32] %srcagg) {
31+
; CHECK-LABEL: @test1(
32+
; CHECK-NEXT: [[I0:%.*]] = extractvalue [2 x i32] [[SRCAGG:%.*]], 0
33+
; CHECK-NEXT: [[I1:%.*]] = extractvalue [2 x i32] [[SRCAGG]], 1
34+
; CHECK-NEXT: [[I2:%.*]] = insertvalue [2 x i32] undef, i32 [[I0]], 0
35+
; CHECK-NEXT: [[I3:%.*]] = insertvalue [2 x i32] [[I2]], i32 [[I1]], 1
36+
; CHECK-NEXT: ret [2 x i32] [[I3]]
37+
;
38+
%i0 = extractvalue [2 x i32] %srcagg, 0
39+
%i1 = extractvalue [2 x i32] %srcagg, 1
40+
%i2 = insertvalue [2 x i32] undef, i32 %i0, 0
41+
%i3 = insertvalue [2 x i32] %i2, i32 %i1, 1
42+
ret [2 x i32] %i3
43+
}
44+
45+
; Right now we don't deal with case where there are more than 2 elements.
46+
; FIXME: should we?
47+
define [3 x i32] @test2([3 x i32] %srcagg) {
48+
; CHECK-LABEL: @test2(
49+
; CHECK-NEXT: [[I0:%.*]] = extractvalue [3 x i32] [[SRCAGG:%.*]], 0
50+
; CHECK-NEXT: [[I1:%.*]] = extractvalue [3 x i32] [[SRCAGG]], 1
51+
; CHECK-NEXT: [[I2:%.*]] = extractvalue [3 x i32] [[SRCAGG]], 2
52+
; CHECK-NEXT: [[I3:%.*]] = insertvalue [3 x i32] undef, i32 [[I0]], 0
53+
; CHECK-NEXT: [[I4:%.*]] = insertvalue [3 x i32] [[I3]], i32 [[I1]], 1
54+
; CHECK-NEXT: [[I5:%.*]] = insertvalue [3 x i32] [[I4]], i32 [[I2]], 2
55+
; CHECK-NEXT: ret [3 x i32] [[I5]]
56+
;
57+
%i0 = extractvalue [3 x i32] %srcagg, 0
58+
%i1 = extractvalue [3 x i32] %srcagg, 1
59+
%i2 = extractvalue [3 x i32] %srcagg, 2
60+
%i3 = insertvalue [3 x i32] undef, i32 %i0, 0
61+
%i4 = insertvalue [3 x i32] %i3, i32 %i1, 1
62+
%i5 = insertvalue [3 x i32] %i4, i32 %i2, 2
63+
ret [3 x i32] %i5
64+
}
65+
66+
; Likewise, we only deal with a single-level aggregates.
67+
; FIXME: should we?
68+
define {{ i32, i32 }} @test3({{ i32, i32 }} %srcagg) {
69+
; CHECK-LABEL: @test3(
70+
; CHECK-NEXT: [[I0:%.*]] = extractvalue { { i32, i32 } } [[SRCAGG:%.*]], 0, 0
71+
; CHECK-NEXT: [[I1:%.*]] = extractvalue { { i32, i32 } } [[SRCAGG]], 0, 1
72+
; CHECK-NEXT: [[I2:%.*]] = insertvalue { { i32, i32 } } undef, i32 [[I0]], 0, 0
73+
; CHECK-NEXT: [[I3:%.*]] = insertvalue { { i32, i32 } } [[I2]], i32 [[I1]], 0, 1
74+
; CHECK-NEXT: ret { { i32, i32 } } [[I3]]
75+
;
76+
%i0 = extractvalue {{ i32, i32 }} %srcagg, 0, 0
77+
%i1 = extractvalue {{ i32, i32 }} %srcagg, 0, 1
78+
%i2 = insertvalue {{ i32, i32 }} undef, i32 %i0, 0, 0
79+
%i3 = insertvalue {{ i32, i32 }} %i2, i32 %i1, 0, 1
80+
ret {{ i32, i32 }} %i3
81+
}
82+
83+
; This is fine, however, all elements are on the same level
84+
define { i32, { i32 } } @test4({ i32, { i32 } } %srcagg) {
85+
; CHECK-LABEL: @test4(
86+
; CHECK-NEXT: [[I0:%.*]] = extractvalue { i32, { i32 } } [[SRCAGG:%.*]], 0
87+
; CHECK-NEXT: [[I1:%.*]] = extractvalue { i32, { i32 } } [[SRCAGG]], 1
88+
; CHECK-NEXT: [[I2:%.*]] = insertvalue { i32, { i32 } } undef, i32 [[I0]], 0
89+
; CHECK-NEXT: [[I3:%.*]] = insertvalue { i32, { i32 } } [[I2]], { i32 } [[I1]], 1
90+
; CHECK-NEXT: ret { i32, { i32 } } [[I3]]
91+
;
92+
%i0 = extractvalue { i32, { i32 } } %srcagg, 0
93+
%i1 = extractvalue { i32, { i32 } } %srcagg, 1
94+
%i2 = insertvalue { i32, { i32 } } undef, i32 %i0, 0
95+
%i3 = insertvalue { i32, { i32 } } %i2, { i32 } %i1, 1
96+
ret { i32, { i32 } } %i3
97+
}
98+
99+
; All element of the newly-created aggregate must come from the same base
100+
; aggregate. Here the second element comes from some other origin.
101+
define { i32, i32 } @negative_test5({ i32, i32 } %srcagg, i32 %replacement) {
102+
; CHECK-LABEL: @negative_test5(
103+
; CHECK-NEXT: [[I0:%.*]] = extractvalue { i32, i32 } [[SRCAGG:%.*]], 0
104+
; CHECK-NEXT: [[I2:%.*]] = insertvalue { i32, i32 } undef, i32 [[I0]], 0
105+
; CHECK-NEXT: [[I3:%.*]] = insertvalue { i32, i32 } [[I2]], i32 [[REPLACEMENT:%.*]], 1
106+
; CHECK-NEXT: ret { i32, i32 } [[I3]]
107+
;
108+
%i0 = extractvalue { i32, i32 } %srcagg, 0
109+
; %i1 = extractvalue { i32, i32 } %srcagg, 1
110+
%i2 = insertvalue { i32, i32 } undef, i32 %i0, 0
111+
%i3 = insertvalue { i32, i32 } %i2, i32 %replacement, 1
112+
ret { i32, i32 } %i3
113+
}
114+
115+
; Here we don't know the value of second element of %otheragg,
116+
define { i32, i32 } @negative_test6({ i32, i32 } %srcagg, { i32, i32 } %otheragg) {
117+
; CHECK-LABEL: @negative_test6(
118+
; CHECK-NEXT: [[I0:%.*]] = extractvalue { i32, i32 } [[SRCAGG:%.*]], 0
119+
; CHECK-NEXT: [[I2:%.*]] = insertvalue { i32, i32 } [[OTHERAGG:%.*]], i32 [[I0]], 0
120+
; CHECK-NEXT: ret { i32, i32 } [[I2]]
121+
;
122+
%i0 = extractvalue { i32, i32 } %srcagg, 0
123+
; %i1 = extractvalue { i32, i32 } %srcagg, 1
124+
%i2 = insertvalue { i32, i32 } %otheragg, i32 %i0, 0
125+
ret { i32, i32 } %i2
126+
}
127+
128+
; All element of the newly-created aggregate must come from the same base
129+
; aggregate. Here different elements come from different base aggregates.
130+
define { i32, i32 } @negative_test7({ i32, i32 } %srcagg0, { i32, i32 } %srcagg1) {
131+
; CHECK-LABEL: @negative_test7(
132+
; CHECK-NEXT: [[I0:%.*]] = extractvalue { i32, i32 } [[SRCAGG0:%.*]], 0
133+
; CHECK-NEXT: [[I3:%.*]] = extractvalue { i32, i32 } [[SRCAGG1:%.*]], 1
134+
; CHECK-NEXT: [[I4:%.*]] = insertvalue { i32, i32 } undef, i32 [[I0]], 0
135+
; CHECK-NEXT: [[I5:%.*]] = insertvalue { i32, i32 } [[I4]], i32 [[I3]], 1
136+
; CHECK-NEXT: ret { i32, i32 } [[I5]]
137+
;
138+
%i0 = extractvalue { i32, i32 } %srcagg0, 0
139+
; %i1 = extractvalue { i32, i32 } %srcagg0, 1
140+
141+
; %i2 = extractvalue { i32, i32 } %srcagg1, 0
142+
%i3 = extractvalue { i32, i32 } %srcagg1, 1
143+
144+
%i4 = insertvalue { i32, i32 } undef, i32 %i0, 0
145+
%i5 = insertvalue { i32, i32 } %i4, i32 %i3, 1
146+
ret { i32, i32 } %i5
147+
}
148+
149+
; Here the element order is swapped as compared to the base aggregate.
150+
define { i32, i32 } @negative_test8({ i32, i32 } %srcagg) {
151+
; CHECK-LABEL: @negative_test8(
152+
; CHECK-NEXT: [[I0:%.*]] = extractvalue { i32, i32 } [[SRCAGG:%.*]], 0
153+
; CHECK-NEXT: [[I1:%.*]] = extractvalue { i32, i32 } [[SRCAGG]], 1
154+
; CHECK-NEXT: [[I2:%.*]] = insertvalue { i32, i32 } undef, i32 [[I0]], 1
155+
; CHECK-NEXT: [[I3:%.*]] = insertvalue { i32, i32 } [[I2]], i32 [[I1]], 0
156+
; CHECK-NEXT: ret { i32, i32 } [[I3]]
157+
;
158+
%i0 = extractvalue { i32, i32 } %srcagg, 0
159+
%i1 = extractvalue { i32, i32 } %srcagg, 1
160+
%i2 = insertvalue { i32, i32 } undef, i32 %i0, 1
161+
%i3 = insertvalue { i32, i32 } %i2, i32 %i1, 0
162+
ret { i32, i32 } %i3
163+
}
164+
165+
; Here both elements of the new aggregate come from the same element of the old aggregate.
166+
define { i32, i32 } @negative_test9({ i32, i32 } %srcagg) {
167+
; CHECK-LABEL: @negative_test9(
168+
; CHECK-NEXT: [[I0:%.*]] = extractvalue { i32, i32 } [[SRCAGG:%.*]], 0
169+
; CHECK-NEXT: [[I2:%.*]] = insertvalue { i32, i32 } undef, i32 [[I0]], 0
170+
; CHECK-NEXT: [[I3:%.*]] = insertvalue { i32, i32 } [[I2]], i32 [[I0]], 1
171+
; CHECK-NEXT: ret { i32, i32 } [[I3]]
172+
;
173+
%i0 = extractvalue { i32, i32 } %srcagg, 0
174+
; %i1 = extractvalue { i32, i32 } %srcagg, 1
175+
%i2 = insertvalue { i32, i32 } undef, i32 %i0, 0
176+
%i3 = insertvalue { i32, i32 } %i2, i32 %i0, 1
177+
ret { i32, i32 } %i3
178+
}
179+
180+
; Here the second element of the new aggregate is undef, , so we must keep this as-is, because in %srcagg it might be poison.
181+
; FIXME: defer to noundef attribute on %srcagg
182+
define { i32, i32 } @negative_test10({ i32, i32 } %srcagg) {
183+
; CHECK-LABEL: @negative_test10(
184+
; CHECK-NEXT: [[I0:%.*]] = extractvalue { i32, i32 } [[SRCAGG:%.*]], 0
185+
; CHECK-NEXT: [[I2:%.*]] = insertvalue { i32, i32 } undef, i32 [[I0]], 0
186+
; CHECK-NEXT: ret { i32, i32 } [[I2]]
187+
;
188+
%i0 = extractvalue { i32, i32 } %srcagg, 0
189+
; %i1 = extractvalue { i32, i32 } %srcagg, 1
190+
%i2 = insertvalue { i32, i32 } undef, i32 %i0, 0
191+
ret { i32, i32 } %i2
192+
}
193+
194+
; Here the second element of the new aggregate is undef, so we must keep this as-is, because in %srcagg it might be poison.
195+
; FIXME: defer to noundef attribute on %srcagg
196+
define { i32, i32 } @negative_test11({ i32, i32 } %srcagg) {
197+
; CHECK-LABEL: @negative_test11(
198+
; CHECK-NEXT: [[I0:%.*]] = extractvalue { i32, i32 } [[SRCAGG:%.*]], 0
199+
; CHECK-NEXT: [[I2:%.*]] = insertvalue { i32, i32 } undef, i32 [[I0]], 0
200+
; CHECK-NEXT: [[I3:%.*]] = insertvalue { i32, i32 } [[I2]], i32 undef, 1
201+
; CHECK-NEXT: ret { i32, i32 } [[I3]]
202+
;
203+
%i0 = extractvalue { i32, i32 } %srcagg, 0
204+
; %i1 = extractvalue { i32, i32 } %srcagg, 1
205+
%i2 = insertvalue { i32, i32 } undef, i32 %i0, 0
206+
%i3 = insertvalue { i32, i32 } %i2, i32 undef, 1
207+
ret { i32, i32 } %i3
208+
}
209+
210+
; This fold does not care whether or not intermediate instructions have extra uses.
211+
define { i32, i32 } @test12({ i32, i32 } %srcagg) {
212+
; CHECK-LABEL: @test12(
213+
; CHECK-NEXT: [[I0:%.*]] = extractvalue { i32, i32 } [[SRCAGG:%.*]], 0
214+
; CHECK-NEXT: call void @usei32(i32 [[I0]])
215+
; CHECK-NEXT: [[I1:%.*]] = extractvalue { i32, i32 } [[SRCAGG]], 1
216+
; CHECK-NEXT: call void @usei32(i32 [[I1]])
217+
; CHECK-NEXT: [[I2:%.*]] = insertvalue { i32, i32 } undef, i32 [[I0]], 0
218+
; CHECK-NEXT: call void @usei32i32agg({ i32, i32 } [[I2]])
219+
; CHECK-NEXT: [[I3:%.*]] = insertvalue { i32, i32 } [[I2]], i32 [[I1]], 1
220+
; CHECK-NEXT: ret { i32, i32 } [[I3]]
221+
;
222+
%i0 = extractvalue { i32, i32 } %srcagg, 0
223+
call void @usei32(i32 %i0)
224+
%i1 = extractvalue { i32, i32 } %srcagg, 1
225+
call void @usei32(i32 %i1)
226+
%i2 = insertvalue { i32, i32 } undef, i32 %i0, 0
227+
call void @usei32i32agg({ i32, i32 } %i2)
228+
%i3 = insertvalue { i32, i32 } %i2, i32 %i1, 1
229+
ret { i32, i32 } %i3
230+
}
231+
232+
; Even though we originally store %i1 into first element, it is later
233+
; overwritten with %i0, so all is fine.
234+
define { i32, i32 } @test13({ i32, i32 } %srcagg) {
235+
; CHECK-LABEL: @test13(
236+
; CHECK-NEXT: [[I0:%.*]] = extractvalue { i32, i32 } [[SRCAGG:%.*]], 0
237+
; CHECK-NEXT: [[I1:%.*]] = extractvalue { i32, i32 } [[SRCAGG]], 1
238+
; CHECK-NEXT: [[I3:%.*]] = insertvalue { i32, i32 } undef, i32 [[I0]], 0
239+
; CHECK-NEXT: [[I4:%.*]] = insertvalue { i32, i32 } [[I3]], i32 [[I1]], 1
240+
; CHECK-NEXT: ret { i32, i32 } [[I4]]
241+
;
242+
%i0 = extractvalue { i32, i32 } %srcagg, 0
243+
%i1 = extractvalue { i32, i32 } %srcagg, 1
244+
%i2 = insertvalue { i32, i32 } undef, i32 %i1, 0
245+
%i3 = insertvalue { i32, i32 } %i2, i32 %i0, 0
246+
%i4 = insertvalue { i32, i32 } %i3, i32 %i1, 1
247+
ret { i32, i32 } %i4
248+
}
249+
250+
; The aggregate type must match exactly between the original and recreation.
251+
define { i32, i32 } @negative_test14({ i32, i32, i32 } %srcagg) {
252+
; CHECK-LABEL: @negative_test14(
253+
; CHECK-NEXT: [[I0:%.*]] = extractvalue { i32, i32, i32 } [[SRCAGG:%.*]], 0
254+
; CHECK-NEXT: [[I1:%.*]] = extractvalue { i32, i32, i32 } [[SRCAGG]], 1
255+
; CHECK-NEXT: [[I2:%.*]] = insertvalue { i32, i32 } undef, i32 [[I0]], 0
256+
; CHECK-NEXT: [[I3:%.*]] = insertvalue { i32, i32 } [[I2]], i32 [[I1]], 1
257+
; CHECK-NEXT: ret { i32, i32 } [[I3]]
258+
;
259+
%i0 = extractvalue { i32, i32, i32 } %srcagg, 0
260+
%i1 = extractvalue { i32, i32, i32 } %srcagg, 1
261+
%i2 = insertvalue { i32, i32 } undef, i32 %i0, 0
262+
%i3 = insertvalue { i32, i32 } %i2, i32 %i1, 1
263+
ret { i32, i32 } %i3
264+
}
265+
define { i32, i32 } @negative_test15({ i32, {i32} } %srcagg) {
266+
; CHECK-LABEL: @negative_test15(
267+
; CHECK-NEXT: [[I0:%.*]] = extractvalue { i32, { i32 } } [[SRCAGG:%.*]], 0
268+
; CHECK-NEXT: [[I1:%.*]] = extractvalue { i32, { i32 } } [[SRCAGG]], 1, 0
269+
; CHECK-NEXT: [[I2:%.*]] = insertvalue { i32, i32 } undef, i32 [[I0]], 0
270+
; CHECK-NEXT: [[I3:%.*]] = insertvalue { i32, i32 } [[I2]], i32 [[I1]], 1
271+
; CHECK-NEXT: ret { i32, i32 } [[I3]]
272+
;
273+
%i0 = extractvalue { i32, {i32} } %srcagg, 0
274+
%i1 = extractvalue { i32, {i32} } %srcagg, 1, 0
275+
%i2 = insertvalue { i32, i32 } undef, i32 %i0, 0
276+
%i3 = insertvalue { i32, i32 } %i2, i32 %i1, 1
277+
ret { i32, i32 } %i3
278+
}
279+
280+
; Just because there are predecessors doesn't mean we should look into them.
281+
define { i32, i32 } @test16({ i32, i32 } %srcagg) {
282+
; CHECK-LABEL: @test16(
283+
; CHECK-NEXT: entry:
284+
; CHECK-NEXT: br label [[END:%.*]]
285+
; CHECK: end:
286+
; CHECK-NEXT: [[I0:%.*]] = extractvalue { i32, i32 } [[SRCAGG:%.*]], 0
287+
; CHECK-NEXT: [[I1:%.*]] = extractvalue { i32, i32 } [[SRCAGG]], 1
288+
; CHECK-NEXT: [[I2:%.*]] = insertvalue { i32, i32 } undef, i32 [[I0]], 0
289+
; CHECK-NEXT: [[I3:%.*]] = insertvalue { i32, i32 } [[I2]], i32 [[I1]], 1
290+
; CHECK-NEXT: ret { i32, i32 } [[I3]]
291+
;
292+
entry:
293+
br label %end
294+
end:
295+
%i0 = extractvalue { i32, i32 } %srcagg, 0
296+
%i1 = extractvalue { i32, i32 } %srcagg, 1
297+
%i2 = insertvalue { i32, i32 } undef, i32 %i0, 0
298+
%i3 = insertvalue { i32, i32 } %i2, i32 %i1, 1
299+
ret { i32, i32 } %i3
300+
}
301+
302+
; Again, we should first try to perform local reasoning, without looking to predecessors.
303+
define { i32, i32 } @test17({ i32, i32 } %srcagg0, { i32, i32 } %srcagg1, i1 %c) {
304+
; CHECK-LABEL: @test17(
305+
; CHECK-NEXT: entry:
306+
; CHECK-NEXT: br i1 [[C:%.*]], label [[INTERMEDIATE:%.*]], label [[END:%.*]]
307+
; CHECK: intermediate:
308+
; CHECK-NEXT: br label [[END]]
309+
; CHECK: end:
310+
; CHECK-NEXT: [[SRCAGG_PHI:%.*]] = phi { i32, i32 } [ [[SRCAGG0:%.*]], [[ENTRY:%.*]] ], [ [[SRCAGG1:%.*]], [[INTERMEDIATE]] ]
311+
; CHECK-NEXT: [[I0:%.*]] = extractvalue { i32, i32 } [[SRCAGG_PHI]], 0
312+
; CHECK-NEXT: [[I1:%.*]] = extractvalue { i32, i32 } [[SRCAGG_PHI]], 1
313+
; CHECK-NEXT: [[I2:%.*]] = insertvalue { i32, i32 } undef, i32 [[I0]], 0
314+
; CHECK-NEXT: [[I3:%.*]] = insertvalue { i32, i32 } [[I2]], i32 [[I1]], 1
315+
; CHECK-NEXT: ret { i32, i32 } [[I3]]
316+
;
317+
entry:
318+
br i1 %c, label %intermediate, label %end
319+
intermediate:
320+
br label %end
321+
end:
322+
%srcagg.phi = phi { i32, i32 } [ %srcagg0, %entry ], [ %srcagg1, %intermediate ]
323+
%i0 = extractvalue { i32, i32 } %srcagg.phi, 0
324+
%i1 = extractvalue { i32, i32 } %srcagg.phi, 1
325+
%i2 = insertvalue { i32, i32 } undef, i32 %i0, 0
326+
%i3 = insertvalue { i32, i32 } %i2, i32 %i1, 1
327+
ret { i32, i32 } %i3
328+
}

0 commit comments

Comments
 (0)