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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
| ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -instcombine -S | FileCheck %s
; https://bugs.llvm.org/show_bug.cgi?id=37603
; https://reviews.llvm.org/D46760#1123713
; Pattern:
; x >> y << y
; Should be transformed into:
; x & (-1 << y)
; ============================================================================ ;
; Basic positive tests
; ============================================================================ ;
define i32 @positive_samevar(i32 %x, i32 %y) {
; CHECK-LABEL: @positive_samevar(
; CHECK-NEXT: [[TMP1:%.*]] = shl i32 -1, [[Y:%.*]]
; CHECK-NEXT: [[RET:%.*]] = and i32 [[TMP1]], [[X:%.*]]
; CHECK-NEXT: ret i32 [[RET]]
;
%tmp0 = ashr i32 %x, %y
%ret = shl i32 %tmp0, %y
ret i32 %ret
}
define i32 @positive_sameconst(i32 %x) {
; CHECK-LABEL: @positive_sameconst(
; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[X:%.*]], -32
; CHECK-NEXT: ret i32 [[TMP1]]
;
%tmp0 = ashr i32 %x, 5
%ret = shl i32 %tmp0, 5
ret i32 %ret
}
define i32 @positive_biggerashr(i32 %x) {
; CHECK-LABEL: @positive_biggerashr(
; CHECK-NEXT: [[TMP0:%.*]] = ashr i32 [[X:%.*]], 10
; CHECK-NEXT: [[RET:%.*]] = shl nsw i32 [[TMP0]], 5
; CHECK-NEXT: ret i32 [[RET]]
;
%tmp0 = ashr i32 %x, 10
%ret = shl i32 %tmp0, 5
ret i32 %ret
}
define i32 @positive_biggershl(i32 %x) {
; CHECK-LABEL: @positive_biggershl(
; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[X:%.*]], 5
; CHECK-NEXT: [[RET:%.*]] = shl i32 [[TMP1]], 10
; CHECK-NEXT: ret i32 [[RET]]
;
%tmp0 = ashr i32 %x, 5
%ret = shl i32 %tmp0, 10
ret i32 %ret
}
; ============================================================================ ;
; EXACT on the first shift
; ============================================================================ ;
define i32 @positive_samevar_ashrexact(i32 %x, i32 %y) {
; CHECK-LABEL: @positive_samevar_ashrexact(
; CHECK-NEXT: ret i32 [[X:%.*]]
;
%tmp0 = ashr exact i32 %x, %y
%ret = shl i32 %tmp0, %y ; this one is obviously 'nuw'.
ret i32 %ret
}
define i32 @positive_sameconst_ashrexact(i32 %x) {
; CHECK-LABEL: @positive_sameconst_ashrexact(
; CHECK-NEXT: ret i32 [[X:%.*]]
;
%tmp0 = ashr exact i32 %x, 5
%ret = shl i32 %tmp0, 5 ; this one is obviously 'nuw'.
ret i32 %ret
}
define i32 @positive_biggerashr_ashrexact(i32 %x) {
; CHECK-LABEL: @positive_biggerashr_ashrexact(
; CHECK-NEXT: [[RET:%.*]] = ashr exact i32 [[X:%.*]], 5
; CHECK-NEXT: ret i32 [[RET]]
;
%tmp0 = ashr exact i32 %x, 10
%ret = shl i32 %tmp0, 5 ; this one is obviously 'nuw'.
ret i32 %ret
}
define i32 @positive_biggershl_ashrexact(i32 %x) {
; CHECK-LABEL: @positive_biggershl_ashrexact(
; CHECK-NEXT: [[RET:%.*]] = shl i32 [[X:%.*]], 5
; CHECK-NEXT: ret i32 [[RET]]
;
%tmp0 = ashr exact i32 %x, 5
%ret = shl i32 %tmp0, 10
ret i32 %ret
}
define i32 @positive_biggershl_ashrexact_shlnuw(i32 %x) {
; CHECK-LABEL: @positive_biggershl_ashrexact_shlnuw(
; CHECK-NEXT: [[RET:%.*]] = shl nuw i32 [[X:%.*]], 5
; CHECK-NEXT: ret i32 [[RET]]
;
%tmp0 = ashr exact i32 %x, 5
%ret = shl nuw i32 %tmp0, 10
ret i32 %ret
}
; ============================================================================ ;
; Vector
; ============================================================================ ;
define <2 x i32> @positive_samevar_vec(<2 x i32> %x, <2 x i32> %y) {
; CHECK-LABEL: @positive_samevar_vec(
; CHECK-NEXT: [[TMP1:%.*]] = shl <2 x i32> <i32 -1, i32 -1>, [[Y:%.*]]
; CHECK-NEXT: [[RET:%.*]] = and <2 x i32> [[TMP1]], [[X:%.*]]
; CHECK-NEXT: ret <2 x i32> [[RET]]
;
%tmp0 = ashr <2 x i32> %x, %y
%ret = shl <2 x i32> %tmp0, %y
ret <2 x i32> %ret
}
; ============================================================================ ;
; Constant Vectors
; ============================================================================ ;
define <2 x i32> @positive_sameconst_vec(<2 x i32> %x) {
; CHECK-LABEL: @positive_sameconst_vec(
; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i32> [[X:%.*]], <i32 -32, i32 -32>
; CHECK-NEXT: ret <2 x i32> [[TMP1]]
;
%tmp0 = ashr <2 x i32> %x, <i32 5, i32 5>
%ret = shl <2 x i32> %tmp0, <i32 5, i32 5>
ret <2 x i32> %ret
}
define <3 x i32> @positive_sameconst_vec_undef0(<3 x i32> %x) {
; CHECK-LABEL: @positive_sameconst_vec_undef0(
; CHECK-NEXT: [[TMP0:%.*]] = ashr <3 x i32> [[X:%.*]], <i32 5, i32 undef, i32 5>
; CHECK-NEXT: [[RET:%.*]] = shl <3 x i32> [[TMP0]], <i32 5, i32 5, i32 5>
; CHECK-NEXT: ret <3 x i32> [[RET]]
;
%tmp0 = ashr <3 x i32> %x, <i32 5, i32 undef, i32 5>
%ret = shl <3 x i32> %tmp0, <i32 5, i32 5, i32 5>
ret <3 x i32> %ret
}
define <3 x i32> @positive_sameconst_vec_undef1(<3 x i32> %x) {
; CHECK-LABEL: @positive_sameconst_vec_undef1(
; CHECK-NEXT: [[TMP0:%.*]] = ashr <3 x i32> [[X:%.*]], <i32 5, i32 5, i32 5>
; CHECK-NEXT: [[RET:%.*]] = shl <3 x i32> [[TMP0]], <i32 5, i32 undef, i32 5>
; CHECK-NEXT: ret <3 x i32> [[RET]]
;
%tmp0 = ashr <3 x i32> %x, <i32 5, i32 5, i32 5>
%ret = shl <3 x i32> %tmp0, <i32 5, i32 undef, i32 5>
ret <3 x i32> %ret
}
define <3 x i32> @positive_sameconst_vec_undef2(<3 x i32> %x) {
; CHECK-LABEL: @positive_sameconst_vec_undef2(
; CHECK-NEXT: [[RET:%.*]] = and <3 x i32> [[X:%.*]], <i32 -32, i32 undef, i32 -32>
; CHECK-NEXT: ret <3 x i32> [[RET]]
;
%tmp0 = ashr <3 x i32> %x, <i32 5, i32 undef, i32 5>
%ret = shl <3 x i32> %tmp0, <i32 5, i32 undef, i32 5>
ret <3 x i32> %ret
}
define <2 x i32> @positive_biggerashr_vec(<2 x i32> %x) {
; CHECK-LABEL: @positive_biggerashr_vec(
; CHECK-NEXT: [[TMP0:%.*]] = ashr <2 x i32> [[X:%.*]], <i32 10, i32 10>
; CHECK-NEXT: [[RET:%.*]] = shl nsw <2 x i32> [[TMP0]], <i32 5, i32 5>
; CHECK-NEXT: ret <2 x i32> [[RET]]
;
%tmp0 = ashr <2 x i32> %x, <i32 10, i32 10>
%ret = shl <2 x i32> %tmp0, <i32 5, i32 5>
ret <2 x i32> %ret
}
define <3 x i32> @positive_biggerashr_vec_undef0(<3 x i32> %x) {
; CHECK-LABEL: @positive_biggerashr_vec_undef0(
; CHECK-NEXT: [[TMP0:%.*]] = ashr <3 x i32> [[X:%.*]], <i32 10, i32 undef, i32 10>
; CHECK-NEXT: [[RET:%.*]] = shl <3 x i32> [[TMP0]], <i32 5, i32 5, i32 5>
; CHECK-NEXT: ret <3 x i32> [[RET]]
;
%tmp0 = ashr <3 x i32> %x, <i32 10, i32 undef, i32 10>
%ret = shl <3 x i32> %tmp0, <i32 5, i32 5, i32 5>
ret <3 x i32> %ret
}
define <3 x i32> @positive_biggerashr_vec_undef1(<3 x i32> %x) {
; CHECK-LABEL: @positive_biggerashr_vec_undef1(
; CHECK-NEXT: [[TMP0:%.*]] = ashr <3 x i32> [[X:%.*]], <i32 10, i32 10, i32 10>
; CHECK-NEXT: [[RET:%.*]] = shl <3 x i32> [[TMP0]], <i32 5, i32 undef, i32 5>
; CHECK-NEXT: ret <3 x i32> [[RET]]
;
%tmp0 = ashr <3 x i32> %x, <i32 10, i32 10, i32 10>
%ret = shl <3 x i32> %tmp0, <i32 5, i32 undef, i32 5>
ret <3 x i32> %ret
}
define <3 x i32> @positive_biggerashr_vec_undef2(<3 x i32> %x) {
; CHECK-LABEL: @positive_biggerashr_vec_undef2(
; CHECK-NEXT: [[TMP0:%.*]] = ashr <3 x i32> [[X:%.*]], <i32 10, i32 undef, i32 10>
; CHECK-NEXT: [[RET:%.*]] = shl <3 x i32> [[TMP0]], <i32 5, i32 undef, i32 5>
; CHECK-NEXT: ret <3 x i32> [[RET]]
;
%tmp0 = ashr <3 x i32> %x, <i32 10, i32 undef, i32 10>
%ret = shl <3 x i32> %tmp0, <i32 5, i32 undef, i32 5>
ret <3 x i32> %ret
}
define <2 x i32> @positive_biggershl_vec(<2 x i32> %x) {
; CHECK-LABEL: @positive_biggershl_vec(
; CHECK-NEXT: [[TMP1:%.*]] = lshr <2 x i32> [[X:%.*]], <i32 5, i32 5>
; CHECK-NEXT: [[RET:%.*]] = shl <2 x i32> [[TMP1]], <i32 10, i32 10>
; CHECK-NEXT: ret <2 x i32> [[RET]]
;
%tmp0 = ashr <2 x i32> %x, <i32 5, i32 5>
%ret = shl <2 x i32> %tmp0, <i32 10, i32 10>
ret <2 x i32> %ret
}
define <3 x i32> @positive_biggershl_vec_undef0(<3 x i32> %x) {
; CHECK-LABEL: @positive_biggershl_vec_undef0(
; CHECK-NEXT: [[TMP0:%.*]] = ashr <3 x i32> [[X:%.*]], <i32 5, i32 undef, i32 5>
; CHECK-NEXT: [[RET:%.*]] = shl <3 x i32> [[TMP0]], <i32 10, i32 10, i32 10>
; CHECK-NEXT: ret <3 x i32> [[RET]]
;
%tmp0 = ashr <3 x i32> %x, <i32 5, i32 undef, i32 5>
%ret = shl <3 x i32> %tmp0, <i32 10, i32 10, i32 10>
ret <3 x i32> %ret
}
define <3 x i32> @positive_biggershl_vec_undef1(<3 x i32> %x) {
; CHECK-LABEL: @positive_biggershl_vec_undef1(
; CHECK-NEXT: [[TMP0:%.*]] = ashr <3 x i32> [[X:%.*]], <i32 5, i32 5, i32 5>
; CHECK-NEXT: [[RET:%.*]] = shl <3 x i32> [[TMP0]], <i32 10, i32 undef, i32 10>
; CHECK-NEXT: ret <3 x i32> [[RET]]
;
%tmp0 = ashr <3 x i32> %x, <i32 5, i32 5, i32 5>
%ret = shl <3 x i32> %tmp0, <i32 10, i32 undef, i32 10>
ret <3 x i32> %ret
}
define <3 x i32> @positive_biggershl_vec_undef2(<3 x i32> %x) {
; CHECK-LABEL: @positive_biggershl_vec_undef2(
; CHECK-NEXT: [[TMP0:%.*]] = ashr <3 x i32> [[X:%.*]], <i32 5, i32 undef, i32 5>
; CHECK-NEXT: [[RET:%.*]] = shl <3 x i32> [[TMP0]], <i32 10, i32 undef, i32 10>
; CHECK-NEXT: ret <3 x i32> [[RET]]
;
%tmp0 = ashr <3 x i32> %x, <i32 5, i32 undef, i32 5>
%ret = shl <3 x i32> %tmp0, <i32 10, i32 undef, i32 10>
ret <3 x i32> %ret
}
; ============================================================================ ;
; Positive multi-use tests with constant
; ============================================================================ ;
; FIXME: drop 'exact' once it is no longer needed.
define i32 @positive_sameconst_multiuse(i32 %x) {
; CHECK-LABEL: @positive_sameconst_multiuse(
; CHECK-NEXT: [[TMP0:%.*]] = ashr exact i32 [[X:%.*]], 5
; CHECK-NEXT: call void @use32(i32 [[TMP0]])
; CHECK-NEXT: ret i32 [[X]]
;
%tmp0 = ashr exact i32 %x, 5
call void @use32(i32 %tmp0)
%ret = shl i32 %tmp0, 5
ret i32 %ret
}
define i32 @positive_biggerashr_multiuse(i32 %x) {
; CHECK-LABEL: @positive_biggerashr_multiuse(
; CHECK-NEXT: [[TMP0:%.*]] = ashr exact i32 [[X:%.*]], 10
; CHECK-NEXT: call void @use32(i32 [[TMP0]])
; CHECK-NEXT: [[RET:%.*]] = ashr exact i32 [[X]], 5
; CHECK-NEXT: ret i32 [[RET]]
;
%tmp0 = ashr exact i32 %x, 10
call void @use32(i32 %tmp0)
%ret = shl i32 %tmp0, 5
ret i32 %ret
}
define i32 @positive_biggershl_multiuse(i32 %x) {
; CHECK-LABEL: @positive_biggershl_multiuse(
; CHECK-NEXT: [[TMP0:%.*]] = ashr exact i32 [[X:%.*]], 5
; CHECK-NEXT: call void @use32(i32 [[TMP0]])
; CHECK-NEXT: [[RET:%.*]] = shl i32 [[X]], 5
; CHECK-NEXT: ret i32 [[RET]]
;
%tmp0 = ashr exact i32 %x, 5
call void @use32(i32 %tmp0)
%ret = shl i32 %tmp0, 10
ret i32 %ret
}
; ============================================================================ ;
; Constant Non-Splat Vectors
; ============================================================================ ;
define <2 x i32> @positive_biggerashr_vec_nonsplat(<2 x i32> %x) {
; CHECK-LABEL: @positive_biggerashr_vec_nonsplat(
; CHECK-NEXT: [[TMP0:%.*]] = ashr <2 x i32> [[X:%.*]], <i32 5, i32 5>
; CHECK-NEXT: [[RET:%.*]] = shl <2 x i32> [[TMP0]], <i32 5, i32 10>
; CHECK-NEXT: ret <2 x i32> [[RET]]
;
%tmp0 = ashr <2 x i32> %x, <i32 5, i32 5>
%ret = shl <2 x i32> %tmp0, <i32 5, i32 10>
ret <2 x i32> %ret
}
define <2 x i32> @positive_biggerLashr_vec_nonsplat(<2 x i32> %x) {
; CHECK-LABEL: @positive_biggerLashr_vec_nonsplat(
; CHECK-NEXT: [[TMP0:%.*]] = ashr <2 x i32> [[X:%.*]], <i32 5, i32 10>
; CHECK-NEXT: [[RET:%.*]] = shl <2 x i32> [[TMP0]], <i32 5, i32 5>
; CHECK-NEXT: ret <2 x i32> [[RET]]
;
%tmp0 = ashr <2 x i32> %x, <i32 5, i32 10>
%ret = shl <2 x i32> %tmp0, <i32 5, i32 5>
ret <2 x i32> %ret
}
; ============================================================================ ;
; Negative tests. Should not be folded.
; ============================================================================ ;
define i32 @negative_twovars(i32 %x, i32 %y, i32 %z) {
; CHECK-LABEL: @negative_twovars(
; CHECK-NEXT: [[TMP0:%.*]] = ashr i32 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[RET:%.*]] = shl i32 [[TMP0]], [[Z:%.*]]
; CHECK-NEXT: ret i32 [[RET]]
;
%tmp0 = ashr i32 %x, %y
%ret = shl i32 %tmp0, %z ; $z, not %y
ret i32 %ret
}
declare void @use32(i32)
; One use only.
define i32 @negative_oneuse(i32 %x, i32 %y) {
; CHECK-LABEL: @negative_oneuse(
; CHECK-NEXT: [[TMP0:%.*]] = ashr i32 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: call void @use32(i32 [[TMP0]])
; CHECK-NEXT: [[RET:%.*]] = shl i32 [[TMP0]], [[Y]]
; CHECK-NEXT: ret i32 [[RET]]
;
%tmp0 = ashr i32 %x, %y
call void @use32(i32 %tmp0)
%ret = shl i32 %tmp0, %y
ret i32 %ret
}
|