/
sext.ll
398 lines (363 loc) · 11.2 KB
/
sext.ll
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -instcombine -S | FileCheck %s
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128"
declare i32 @llvm.ctpop.i32(i32)
declare i32 @llvm.ctlz.i32(i32, i1)
declare i32 @llvm.cttz.i32(i32, i1)
declare void @use(i8)
declare void @use_vec(<2 x i5>)
define i64 @test1(i32 %x) {
; CHECK-LABEL: @test1(
; CHECK-NEXT: [[T:%.*]] = call i32 @llvm.ctpop.i32(i32 [[X:%.*]]), !range [[RNG0:![0-9]+]]
; CHECK-NEXT: [[S:%.*]] = zext i32 [[T]] to i64
; CHECK-NEXT: ret i64 [[S]]
;
%t = call i32 @llvm.ctpop.i32(i32 %x)
%s = sext i32 %t to i64
ret i64 %s
}
define i64 @test2(i32 %x) {
; CHECK-LABEL: @test2(
; CHECK-NEXT: [[T:%.*]] = call i32 @llvm.ctlz.i32(i32 [[X:%.*]], i1 true), !range [[RNG0]]
; CHECK-NEXT: [[S:%.*]] = zext i32 [[T]] to i64
; CHECK-NEXT: ret i64 [[S]]
;
%t = call i32 @llvm.ctlz.i32(i32 %x, i1 true)
%s = sext i32 %t to i64
ret i64 %s
}
define i64 @test3(i32 %x) {
; CHECK-LABEL: @test3(
; CHECK-NEXT: [[T:%.*]] = call i32 @llvm.cttz.i32(i32 [[X:%.*]], i1 true), !range [[RNG0]]
; CHECK-NEXT: [[S:%.*]] = zext i32 [[T]] to i64
; CHECK-NEXT: ret i64 [[S]]
;
%t = call i32 @llvm.cttz.i32(i32 %x, i1 true)
%s = sext i32 %t to i64
ret i64 %s
}
define i64 @test4(i32 %x) {
; CHECK-LABEL: @test4(
; CHECK-NEXT: [[T:%.*]] = udiv i32 [[X:%.*]], 3
; CHECK-NEXT: [[S:%.*]] = zext i32 [[T]] to i64
; CHECK-NEXT: ret i64 [[S]]
;
%t = udiv i32 %x, 3
%s = sext i32 %t to i64
ret i64 %s
}
define i64 @test5(i32 %x) {
; CHECK-LABEL: @test5(
; CHECK-NEXT: [[T:%.*]] = urem i32 [[X:%.*]], 30000
; CHECK-NEXT: [[S:%.*]] = zext i32 [[T]] to i64
; CHECK-NEXT: ret i64 [[S]]
;
%t = urem i32 %x, 30000
%s = sext i32 %t to i64
ret i64 %s
}
define i64 @test6(i32 %x) {
; CHECK-LABEL: @test6(
; CHECK-NEXT: [[U:%.*]] = lshr i32 [[X:%.*]], 3
; CHECK-NEXT: [[T:%.*]] = mul nuw nsw i32 [[U]], 3
; CHECK-NEXT: [[S:%.*]] = zext i32 [[T]] to i64
; CHECK-NEXT: ret i64 [[S]]
;
%u = lshr i32 %x, 3
%t = mul i32 %u, 3
%s = sext i32 %t to i64
ret i64 %s
}
define i64 @test7(i32 %x) {
; CHECK-LABEL: @test7(
; CHECK-NEXT: [[T:%.*]] = and i32 [[X:%.*]], 511
; CHECK-NEXT: [[U:%.*]] = sub nuw nsw i32 20000, [[T]]
; CHECK-NEXT: [[S:%.*]] = zext i32 [[U]] to i64
; CHECK-NEXT: ret i64 [[S]]
;
%t = and i32 %x, 511
%u = sub i32 20000, %t
%s = sext i32 %u to i64
ret i64 %s
}
define i32 @test8(i8 %a, i32 %f, i1 %p, i32* %z) {
; CHECK-LABEL: @test8(
; CHECK-NEXT: [[D:%.*]] = lshr i32 [[F:%.*]], 24
; CHECK-NEXT: [[N:%.*]] = select i1 [[P:%.*]], i32 [[D]], i32 0
; CHECK-NEXT: ret i32 [[N]]
;
%d = lshr i32 %f, 24
%e = select i1 %p, i32 %d, i32 0
%s = trunc i32 %e to i16
%n = sext i16 %s to i32
ret i32 %n
}
; rdar://6013816
define i16 @test9(i16 %t, i1 %cond) {
; CHECK-LABEL: @test9(
; CHECK-NEXT: entry:
; CHECK-NEXT: br i1 [[COND:%.*]], label [[TBB:%.*]], label [[FBB:%.*]]
; CHECK: TBB:
; CHECK-NEXT: br label [[FBB]]
; CHECK: FBB:
; CHECK-NEXT: [[V_OFF0:%.*]] = phi i16 [ [[T:%.*]], [[TBB]] ], [ 42, [[ENTRY:%.*]] ]
; CHECK-NEXT: ret i16 [[V_OFF0]]
;
entry:
br i1 %cond, label %TBB, label %FBB
TBB:
%t2 = sext i16 %t to i32
br label %FBB
FBB:
%V = phi i32 [%t2, %TBB], [42, %entry]
%W = trunc i32 %V to i16
ret i16 %W
}
; PR2638
define i32 @test10(i32 %i) {
; CHECK-LABEL: @test10(
; CHECK-NEXT: [[D1:%.*]] = shl i32 [[I:%.*]], 30
; CHECK-NEXT: [[D:%.*]] = ashr exact i32 [[D1]], 30
; CHECK-NEXT: ret i32 [[D]]
;
%A = trunc i32 %i to i8
%B = shl i8 %A, 6
%C = ashr i8 %B, 6
%D = sext i8 %C to i32
ret i32 %D
}
define <2 x i32> @test10_vec(<2 x i32> %i) {
; CHECK-LABEL: @test10_vec(
; CHECK-NEXT: [[D1:%.*]] = shl <2 x i32> [[I:%.*]], <i32 30, i32 30>
; CHECK-NEXT: [[D:%.*]] = ashr exact <2 x i32> [[D1]], <i32 30, i32 30>
; CHECK-NEXT: ret <2 x i32> [[D]]
;
%A = trunc <2 x i32> %i to <2 x i8>
%B = shl <2 x i8> %A, <i8 6, i8 6>
%C = ashr <2 x i8> %B, <i8 6, i8 6>
%D = sext <2 x i8> %C to <2 x i32>
ret <2 x i32> %D
}
define <2 x i32> @test10_vec_nonuniform(<2 x i32> %i) {
; CHECK-LABEL: @test10_vec_nonuniform(
; CHECK-NEXT: [[D1:%.*]] = shl <2 x i32> [[I:%.*]], <i32 30, i32 27>
; CHECK-NEXT: [[D:%.*]] = ashr <2 x i32> [[D1]], <i32 30, i32 27>
; CHECK-NEXT: ret <2 x i32> [[D]]
;
%A = trunc <2 x i32> %i to <2 x i8>
%B = shl <2 x i8> %A, <i8 6, i8 3>
%C = ashr <2 x i8> %B, <i8 6, i8 3>
%D = sext <2 x i8> %C to <2 x i32>
ret <2 x i32> %D
}
define <2 x i32> @test10_vec_undef0(<2 x i32> %i) {
; CHECK-LABEL: @test10_vec_undef0(
; CHECK-NEXT: [[D1:%.*]] = shl <2 x i32> [[I:%.*]], <i32 30, i32 undef>
; CHECK-NEXT: [[D:%.*]] = ashr <2 x i32> [[D1]], <i32 30, i32 undef>
; CHECK-NEXT: ret <2 x i32> [[D]]
;
%A = trunc <2 x i32> %i to <2 x i8>
%B = shl <2 x i8> %A, <i8 6, i8 0>
%C = ashr <2 x i8> %B, <i8 6, i8 undef>
%D = sext <2 x i8> %C to <2 x i32>
ret <2 x i32> %D
}
define <2 x i32> @test10_vec_undef1(<2 x i32> %i) {
; CHECK-LABEL: @test10_vec_undef1(
; CHECK-NEXT: [[D1:%.*]] = shl <2 x i32> [[I:%.*]], <i32 30, i32 undef>
; CHECK-NEXT: [[D:%.*]] = ashr <2 x i32> [[D1]], <i32 30, i32 undef>
; CHECK-NEXT: ret <2 x i32> [[D]]
;
%A = trunc <2 x i32> %i to <2 x i8>
%B = shl <2 x i8> %A, <i8 6, i8 undef>
%C = ashr <2 x i8> %B, <i8 6, i8 0>
%D = sext <2 x i8> %C to <2 x i32>
ret <2 x i32> %D
}
define <2 x i32> @test10_vec_undef2(<2 x i32> %i) {
; CHECK-LABEL: @test10_vec_undef2(
; CHECK-NEXT: [[D1:%.*]] = shl <2 x i32> [[I:%.*]], <i32 30, i32 undef>
; CHECK-NEXT: [[D:%.*]] = ashr <2 x i32> [[D1]], <i32 30, i32 undef>
; CHECK-NEXT: ret <2 x i32> [[D]]
;
%A = trunc <2 x i32> %i to <2 x i8>
%B = shl <2 x i8> %A, <i8 6, i8 undef>
%C = ashr <2 x i8> %B, <i8 6, i8 undef>
%D = sext <2 x i8> %C to <2 x i32>
ret <2 x i32> %D
}
define void @test11(<2 x i16> %srcA, <2 x i16> %srcB, <2 x i16>* %dst) {
; CHECK-LABEL: @test11(
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <2 x i16> [[SRCB:%.*]], [[SRCA:%.*]]
; CHECK-NEXT: [[SEXT:%.*]] = sext <2 x i1> [[CMP]] to <2 x i16>
; CHECK-NEXT: store <2 x i16> [[SEXT]], <2 x i16>* [[DST:%.*]], align 4
; CHECK-NEXT: ret void
;
%cmp = icmp eq <2 x i16> %srcB, %srcA
%sext = sext <2 x i1> %cmp to <2 x i16>
%tmask = ashr <2 x i16> %sext, <i16 15, i16 15>
store <2 x i16> %tmask, <2 x i16>* %dst
ret void
}
define i64 @test12(i32 %x) {
; CHECK-LABEL: @test12(
; CHECK-NEXT: [[SHR:%.*]] = lshr i32 [[X:%.*]], 1
; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 0, [[SHR]]
; CHECK-NEXT: [[CONV:%.*]] = sext i32 [[SUB]] to i64
; CHECK-NEXT: ret i64 [[CONV]]
;
%shr = lshr i32 %x, 1
%sub = sub nsw i32 0, %shr
%conv = sext i32 %sub to i64
ret i64 %conv
}
define i32 @test13(i32 %x) {
; CHECK-LABEL: @test13(
; CHECK-NEXT: [[AND:%.*]] = lshr i32 [[X:%.*]], 3
; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[AND]], 1
; CHECK-NEXT: [[SEXT:%.*]] = add nsw i32 [[TMP1]], -1
; CHECK-NEXT: ret i32 [[SEXT]]
;
%and = and i32 %x, 8
%cmp = icmp eq i32 %and, 0
%ext = sext i1 %cmp to i32
ret i32 %ext
}
define i32 @test14(i16 %x) {
; CHECK-LABEL: @test14(
; CHECK-NEXT: [[AND:%.*]] = lshr i16 [[X:%.*]], 4
; CHECK-NEXT: [[TMP1:%.*]] = and i16 [[AND]], 1
; CHECK-NEXT: [[SEXT:%.*]] = add nsw i16 [[TMP1]], -1
; CHECK-NEXT: [[EXT:%.*]] = sext i16 [[SEXT]] to i32
; CHECK-NEXT: ret i32 [[EXT]]
;
%and = and i16 %x, 16
%cmp = icmp ne i16 %and, 16
%ext = sext i1 %cmp to i32
ret i32 %ext
}
define i32 @test15(i32 %x) {
; CHECK-LABEL: @test15(
; CHECK-NEXT: [[AND:%.*]] = shl i32 [[X:%.*]], 27
; CHECK-NEXT: [[SEXT:%.*]] = ashr i32 [[AND]], 31
; CHECK-NEXT: ret i32 [[SEXT]]
;
%and = and i32 %x, 16
%cmp = icmp ne i32 %and, 0
%ext = sext i1 %cmp to i32
ret i32 %ext
}
define i32 @test16(i16 %x) {
; CHECK-LABEL: @test16(
; CHECK-NEXT: [[AND:%.*]] = shl i16 [[X:%.*]], 12
; CHECK-NEXT: [[SEXT:%.*]] = ashr i16 [[AND]], 15
; CHECK-NEXT: [[EXT:%.*]] = sext i16 [[SEXT]] to i32
; CHECK-NEXT: ret i32 [[EXT]]
;
%and = and i16 %x, 8
%cmp = icmp eq i16 %and, 8
%ext = sext i1 %cmp to i32
ret i32 %ext
}
define i32 @test17(i1 %x) {
; CHECK-LABEL: @test17(
; CHECK-NEXT: [[C1_NEG:%.*]] = zext i1 [[X:%.*]] to i32
; CHECK-NEXT: ret i32 [[C1_NEG]]
;
%c1 = sext i1 %x to i32
%c2 = sub i32 0, %c1
ret i32 %c2
}
define i32 @test18(i16 %x) {
; CHECK-LABEL: @test18(
; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i16 [[X:%.*]], 0
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[TMP1]], i16 [[X]], i16 0
; CHECK-NEXT: [[EXT:%.*]] = zext i16 [[SEL]] to i32
; CHECK-NEXT: ret i32 [[EXT]]
;
%cmp = icmp slt i16 %x, 0
%sel = select i1 %cmp, i16 0, i16 %x
%ext = sext i16 %sel to i32
ret i32 %ext
}
define i10 @test19(i10 %i) {
; CHECK-LABEL: @test19(
; CHECK-NEXT: [[A:%.*]] = trunc i10 [[I:%.*]] to i3
; CHECK-NEXT: [[TMP1:%.*]] = and i3 [[A]], 1
; CHECK-NEXT: [[C:%.*]] = sub nsw i3 0, [[TMP1]]
; CHECK-NEXT: [[D:%.*]] = sext i3 [[C]] to i10
; CHECK-NEXT: ret i10 [[D]]
;
%a = trunc i10 %i to i3
%b = shl i3 %a, 2
%c = ashr i3 %b, 2
%d = sext i3 %c to i10
ret i10 %d
}
define i32 @smear_set_bit(i32 %x) {
; CHECK-LABEL: @smear_set_bit(
; CHECK-NEXT: [[TMP1:%.*]] = shl i32 [[X:%.*]], 24
; CHECK-NEXT: [[S:%.*]] = ashr i32 [[TMP1]], 31
; CHECK-NEXT: ret i32 [[S]]
;
%t = trunc i32 %x to i8
%a = ashr i8 %t, 7
%s = sext i8 %a to i32
ret i32 %s
}
; extra use of trunc is ok because we still shorten the use chain
define <2 x i32> @smear_set_bit_vec_use1(<2 x i32> %x) {
; CHECK-LABEL: @smear_set_bit_vec_use1(
; CHECK-NEXT: [[T:%.*]] = trunc <2 x i32> [[X:%.*]] to <2 x i5>
; CHECK-NEXT: call void @use_vec(<2 x i5> [[T]])
; CHECK-NEXT: [[TMP1:%.*]] = shl <2 x i32> [[X]], <i32 27, i32 27>
; CHECK-NEXT: [[S:%.*]] = ashr <2 x i32> [[TMP1]], <i32 31, i32 31>
; CHECK-NEXT: ret <2 x i32> [[S]]
;
%t = trunc <2 x i32> %x to <2 x i5>
call void @use_vec(<2 x i5> %t)
%a = ashr <2 x i5> %t, <i5 4, i5 4>
%s = sext <2 x i5> %a to <2 x i32>
ret <2 x i32> %s
}
; negative test - extra use
define i32 @smear_set_bit_use2(i32 %x) {
; CHECK-LABEL: @smear_set_bit_use2(
; CHECK-NEXT: [[T:%.*]] = trunc i32 [[X:%.*]] to i8
; CHECK-NEXT: [[A:%.*]] = ashr i8 [[T]], 7
; CHECK-NEXT: call void @use(i8 [[A]])
; CHECK-NEXT: [[S:%.*]] = sext i8 [[A]] to i32
; CHECK-NEXT: ret i32 [[S]]
;
%t = trunc i32 %x to i8
%a = ashr i8 %t, 7
call void @use(i8 %a)
%s = sext i8 %a to i32
ret i32 %s
}
; negative test - must shift all the way across
define i32 @smear_set_bit_wrong_shift_amount(i32 %x) {
; CHECK-LABEL: @smear_set_bit_wrong_shift_amount(
; CHECK-NEXT: [[T:%.*]] = trunc i32 [[X:%.*]] to i8
; CHECK-NEXT: [[A:%.*]] = ashr i8 [[T]], 6
; CHECK-NEXT: [[S:%.*]] = sext i8 [[A]] to i32
; CHECK-NEXT: ret i32 [[S]]
;
%t = trunc i32 %x to i8
%a = ashr i8 %t, 6
%s = sext i8 %a to i32
ret i32 %s
}
; TODO: this could be mask+compare+sext or shifts+trunc
define i16 @smear_set_bit_different_dest_type(i32 %x) {
; CHECK-LABEL: @smear_set_bit_different_dest_type(
; CHECK-NEXT: [[T:%.*]] = trunc i32 [[X:%.*]] to i8
; CHECK-NEXT: [[A:%.*]] = ashr i8 [[T]], 7
; CHECK-NEXT: [[S:%.*]] = sext i8 [[A]] to i16
; CHECK-NEXT: ret i16 [[S]]
;
%t = trunc i32 %x to i8
%a = ashr i8 %t, 7
%s = sext i8 %a to i16
ret i16 %s
}