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