aboutsummaryrefslogtreecommitdiff
path: root/llvm/test/Transforms/AggressiveInstCombine/lower-table-based-cttz-basics.ll
blob: bb3001e909b8ca200978a07df7ce4c9e1554ba66 (plain)
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -passes=aggressive-instcombine -S < %s | FileCheck %s

;; These cases test lowering of various implementations of table-based ctz
;; algorithms to the llvm.cttz instruction.

;; C reproducers:
;; int ctz1 (unsigned x)
;; {
;; static const char table[32] =
;;    {
;;     0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
;;      31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
;;    };
;;   return table[((unsigned)((x & -x) * 0x077CB531U)) >> 27];
;; }

;; int ctz2(unsigned x)
;; {
;; #define u 0
;;  static short table[64] =
;;   {
;;     32, 0, 1, 12, 2, 6, u, 13, 3, u, 7, u, u, u, u, 14,
;;     10, 4, u, u, 8, u, u, 25, u, u, u, u, u, 21, 27, 15,
;;     31, 11, 5, u, u, u, u, u, 9, u, u, 24, u, u, 20, 26,
;;     30, u, u, u, u, 23, u, 19, 29, u, 22, 18, 28, 17, 16, u
;;   };
;; x = (x & -x) * 0x0450FBAF;
;; return table[x >> 26];
;; }

;; int ctz3(unsigned x)
;;{
;; static int table[32] =
;;   {
;;     0, 1, 2, 24, 3, 19, 6, 25, 22, 4, 20, 10, 16, 7, 12, 26,
;;      31, 23, 18, 5, 21, 9, 15, 11, 30, 17, 8, 14, 29, 13, 28, 27
;;    };
;;  if (x == 0) return 32;
;;  x = (x & -x) * 0x04D7651F;
;;  return table[x >> 27];
;; }

;; static const unsigned long long magic = 0x03f08c5392f756cdULL;
;;
;; static const int table[64] = {
;;     0,  1, 12,  2, 13, 22, 17,  3, 14, 33, 23, 36, 18, 58, 28, 4,
;;    62, 15, 34, 26, 24, 48, 50, 37, 19, 55, 59, 52, 29, 44, 39,  5,
;;    63, 11, 21, 16, 32, 35, 57, 27, 61, 25, 47, 49, 54, 51, 43, 38,
;;    10, 20, 31, 56, 60, 46, 53, 42, 9, 30, 45, 41,  8, 40,  7,  6,
;; };
;;
;; int ctz4 (unsigned long long b)
;; {
;;    unsigned long long lsb = b & -b;
;;    return table[(lsb * magic) >> 58];
;; }
;;
;; int ctz5(unsigned x)
;; {
;;  static char table[32] =
;;    {
;;      0, 1, 2, 24, 3, 19, 6, 25, 22, 4, 20, 10, 16, 7, 12, 26,
;;      31, 23, 18, 5, 21, 9, 15, 11, 30, 17, 8, 14, 29, 13, 28, 27
;;    };
;;  x = (x & -x)*0x04D7651F;
;;  return table[x >> 27];
;; }

;; const int indexes[] = {
;;     63, 0, 58, 1, 59, 47, 53, 2,60, 39, 48, 27, 54, 33, 42, 3,
;;     61, 51, 37, 40, 49, 18, 28, 20, 55, 30, 34, 11, 43, 14, 22, 4,
;;     62, 57, 46, 52, 38, 26, 32, 41, 50, 36, 17, 19, 29, 10, 13, 21,
;;     56, 45, 25, 31, 35, 16, 9, 12, 44, 24, 15, 8, 23, 7, 6, 5
;; };
;;
;; int ctz6(unsigned long n)
;; {
;;       return indexes[((n & (~n + 1)) * 0x07EDD5E59A4E28C2ull) >> 58];
;; }
;;
;; int ctz8(unsigned v)
;; {
;; static const int table[] =
;; {
;;  31 ,0 ,1 ,23 ,2 ,18 ,5 ,24 ,21 ,3 ,19 ,9 ,15 ,6 ,11 ,25 ,30 ,22 ,17 ,4 ,20 ;,8 ,14 ,10 ,29 ,16 ,7 ,13 ,28 ,12 ,27 ,26
;; };
;; unsigned x =(-v & v);
;; return table[(unsigned)(x * 0x9AECA3EU) >> 27];
;; }

@ctz7.table = internal unnamed_addr constant [32 x i8] c"\00\01\1C\02\1D\0E\18\03\1E\16\14\0F\19\11\04\08\1F\1B\0D\17\15\13\10\07\1A\0C\12\06\0B\05\0A\09", align 1

define i32 @ctz1(i32 %x) {
; CHECK-LABEL: @ctz1(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[TMP0:%.*]] = call i32 @llvm.cttz.i32(i32 [[X:%.*]], i1 true)
; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq i32 [[X]], 0
; CHECK-NEXT:    [[TMP2:%.*]] = select i1 [[TMP1]], i32 0, i32 [[TMP0]]
; CHECK-NEXT:    [[TMP3:%.*]] = trunc i32 [[TMP2]] to i8
; CHECK-NEXT:    [[CONV:%.*]] = zext i8 [[TMP3]] to i32
; CHECK-NEXT:    ret i32 [[CONV]]
;
entry:
  %sub = sub i32 0, %x
  %and = and i32 %sub, %x
  %mul = mul i32 %and, 125613361
  %shr = lshr i32 %mul, 27
  %idxprom = zext i32 %shr to i64
  %arrayidx = getelementptr inbounds [32 x i8], ptr @ctz7.table, i64 0, i64 %idxprom
  %0 = load i8, ptr %arrayidx, align 1
  %conv = zext i8 %0 to i32
  ret i32 %conv
}

define i32 @ctz1_nusw(i32 %x) {
; CHECK-LABEL: @ctz1_nusw(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[TMP0:%.*]] = call i32 @llvm.cttz.i32(i32 [[X:%.*]], i1 true)
; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq i32 [[X]], 0
; CHECK-NEXT:    [[TMP2:%.*]] = select i1 [[TMP1]], i32 0, i32 [[TMP0]]
; CHECK-NEXT:    [[TMP3:%.*]] = trunc i32 [[TMP2]] to i8
; CHECK-NEXT:    [[CONV:%.*]] = zext i8 [[TMP3]] to i32
; CHECK-NEXT:    ret i32 [[CONV]]
;
entry:
  %sub = sub i32 0, %x
  %and = and i32 %sub, %x
  %mul = mul i32 %and, 125613361
  %shr = lshr i32 %mul, 27
  %idxprom = zext i32 %shr to i64
  %arrayidx = getelementptr nusw [32 x i8], ptr @ctz7.table, i64 0, i64 %idxprom
  %0 = load i8, ptr %arrayidx, align 1
  %conv = zext i8 %0 to i32
  ret i32 %conv
}

@ctz2.table = internal unnamed_addr constant [64 x i16] [i16 32, i16 0, i16 1, i16 12, i16 2, i16 6, i16 0, i16 13, i16 3, i16 0, i16 7, i16 0, i16 0, i16 0, i16 0, i16 14, i16 10, i16 4, i16 0, i16 0, i16 8, i16 0, i16 0, i16 25, i16 0, i16 0, i16 0, i16 0, i16 0, i16 21, i16 27, i16 15, i16 31, i16 11, i16 5, i16 0, i16 0, i16 0, i16 0, i16 0, i16 9, i16 0, i16 0, i16 24, i16 0, i16 0, i16 20, i16 26, i16 30, i16 0, i16 0, i16 0, i16 0, i16 23, i16 0, i16 19, i16 29, i16 0, i16 22, i16 18, i16 28, i16 17, i16 16, i16 0], align 2

define i32 @ctz2(i32 %x) {
; CHECK-LABEL: @ctz2(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[TMP0:%.*]] = call i32 @llvm.cttz.i32(i32 [[X:%.*]], i1 false)
; CHECK-NEXT:    [[TMP1:%.*]] = trunc i32 [[TMP0]] to i16
; CHECK-NEXT:    [[CONV:%.*]] = sext i16 [[TMP1]] to i32
; CHECK-NEXT:    ret i32 [[CONV]]
;
entry:
  %sub = sub i32 0, %x
  %and = and i32 %sub, %x
  %mul = mul i32 %and, 72416175
  %shr = lshr i32 %mul, 26
  %idxprom = zext i32 %shr to i64
  %arrayidx = getelementptr inbounds [64 x i16], ptr @ctz2.table, i64 0, i64 %idxprom
  %0 = load i16, ptr %arrayidx, align 2
  %conv = sext i16 %0 to i32
  ret i32 %conv
}

@ctz3.table = internal unnamed_addr constant [32 x i32] [i32 0, i32 1, i32 2, i32 24, i32 3, i32 19, i32 6, i32 25, i32 22, i32 4, i32 20, i32 10, i32 16, i32 7, i32 12, i32 26, i32 31, i32 23, i32 18, i32 5, i32 21, i32 9, i32 15, i32 11, i32 30, i32 17, i32 8, i32 14, i32 29, i32 13, i32 28, i32 27], align 4

define i32 @ctz3(i32 %x) {
; CHECK-LABEL: @ctz3(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[X:%.*]], 0
; CHECK-NEXT:    br i1 [[CMP]], label [[RETURN:%.*]], label [[IF_END:%.*]]
; CHECK:       if.end:
; CHECK-NEXT:    [[TMP0:%.*]] = call i32 @llvm.cttz.i32(i32 [[X]], i1 true)
; CHECK-NEXT:    br label [[RETURN]]
; CHECK:       return:
; CHECK-NEXT:    [[RETVAL_0:%.*]] = phi i32 [ [[TMP0]], [[IF_END]] ], [ 32, [[ENTRY:%.*]] ]
; CHECK-NEXT:    ret i32 [[RETVAL_0]]
;
entry:
  %cmp = icmp eq i32 %x, 0
  br i1 %cmp, label %return, label %if.end

if.end:                                           ; preds = %entry
  %sub = sub i32 0, %x
  %and = and i32 %sub, %x
  %mul = mul i32 %and, 81224991
  %shr = lshr i32 %mul, 27
  %idxprom = zext i32 %shr to i64
  %arrayidx = getelementptr inbounds [32 x i32], ptr @ctz3.table, i64 0, i64 %idxprom
  %0 = load i32, ptr %arrayidx, align 4
  br label %return

return:                                           ; preds = %entry, %if.end
  %retval.0 = phi i32 [ %0, %if.end ], [ 32, %entry ]
  ret i32 %retval.0
}

define i32 @ctz3_with_i8gep(i32 %x) {
; CHECK-LABEL: @ctz3_with_i8gep(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[X:%.*]], 0
; CHECK-NEXT:    br i1 [[CMP]], label [[RETURN:%.*]], label [[IF_END:%.*]]
; CHECK:       if.end:
; CHECK-NEXT:    [[TMP2:%.*]] = call i32 @llvm.cttz.i32(i32 [[X]], i1 true)
; CHECK-NEXT:    br label [[RETURN]]
; CHECK:       return:
; CHECK-NEXT:    [[RETVAL_0:%.*]] = phi i32 [ [[TMP2]], [[IF_END]] ], [ 32, [[ENTRY:%.*]] ]
; CHECK-NEXT:    ret i32 [[RETVAL_0]]
;
entry:
  %cmp = icmp eq i32 %x, 0
  br i1 %cmp, label %return, label %if.end

if.end:                                           ; preds = %entry
  %sub = sub i32 0, %x
  %and = and i32 %x, %sub
  %mul = mul i32 %and, 81224991
  %0 = lshr i32 %mul, 25
  %1 = and i32 %0, 124
  %arrayidx.idx = zext nneg i32 %1 to i64
  %arrayidx = getelementptr inbounds nuw i8, ptr @ctz3.table, i64 %arrayidx.idx
  %2 = load i32, ptr %arrayidx, align 4
  br label %return

return:                                           ; preds = %if.end, %entry
  %retval.0 = phi i32 [ %2, %if.end ], [ 32, %entry ]
  ret i32 %retval.0
}


@table = internal unnamed_addr constant [64 x i32] [i32 0, i32 1, i32 12, i32 2, i32 13, i32 22, i32 17, i32 3, i32 14, i32 33, i32 23, i32 36, i32 18, i32 58, i32 28, i32 4, i32 62, i32 15, i32 34, i32 26, i32 24, i32 48, i32 50, i32 37, i32 19, i32 55, i32 59, i32 52, i32 29, i32 44, i32 39, i32 5, i32 63, i32 11, i32 21, i32 16, i32 32, i32 35, i32 57, i32 27, i32 61, i32 25, i32 47, i32 49, i32 54, i32 51, i32 43, i32 38, i32 10, i32 20, i32 31, i32 56, i32 60, i32 46, i32 53, i32 42, i32 9, i32 30, i32 45, i32 41, i32 8, i32 40, i32 7, i32 6], align 4

define i32 @ctz4(i64 %b) {
; CHECK-LABEL: @ctz4(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[TMP0:%.*]] = call i64 @llvm.cttz.i64(i64 [[B:%.*]], i1 true)
; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq i64 [[B]], 0
; CHECK-NEXT:    [[TMP2:%.*]] = select i1 [[TMP1]], i64 0, i64 [[TMP0]]
; CHECK-NEXT:    [[TMP3:%.*]] = trunc i64 [[TMP2]] to i32
; CHECK-NEXT:    ret i32 [[TMP3]]
;
entry:
  %sub = sub i64 0, %b
  %and = and i64 %sub, %b
  %mul = mul i64 %and, 283881067100198605
  %shr = lshr i64 %mul, 58
  %arrayidx = getelementptr inbounds [64 x i32], ptr @table, i64 0, i64 %shr
  %0 = load i32, ptr %arrayidx, align 4
  ret i32 %0
}

@ctz5.table = internal unnamed_addr constant [32 x i8] c"\00\01\02\18\03\13\06\19\16\04\14\0A\10\07\0C\1A\1F\17\12\05\15\09\0F\0B\1E\11\08\0E\1D\0D\1C\1B", align 1

define i32 @ctz5(i32 %x) {
; CHECK-LABEL: @ctz5(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[TMP0:%.*]] = call i32 @llvm.cttz.i32(i32 [[X:%.*]], i1 true)
; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq i32 [[X]], 0
; CHECK-NEXT:    [[TMP2:%.*]] = select i1 [[TMP1]], i32 0, i32 [[TMP0]]
; CHECK-NEXT:    [[TMP3:%.*]] = trunc i32 [[TMP2]] to i8
; CHECK-NEXT:    [[CONV:%.*]] = zext i8 [[TMP3]] to i32
; CHECK-NEXT:    ret i32 [[CONV]]
;
entry:
  %sub = sub i32 0, %x
  %and = and i32 %sub, %x
  %mul = mul i32 %and, 81224991
  %shr = lshr i32 %mul, 27
  %idxprom = zext i32 %shr to i64
  %arrayidx = getelementptr inbounds [32 x i8], ptr @ctz5.table, i64 0, i64 %idxprom
  %0 = load i8, ptr %arrayidx, align 1
  %conv = zext i8 %0 to i32
  ret i32 %conv
}

@ctz6.table = constant [64 x i32] [i32 63, i32 0, i32 58, i32 1, i32 59, i32 47, i32 53, i32 2, i32 60, i32 39, i32 48, i32 27, i32 54, i32 33, i32 42, i32 3, i32 61, i32 51, i32 37, i32 40, i32 49, i32 18, i32 28, i32 20, i32 55, i32 30, i32 34, i32 11, i32 43, i32 14, i32 22, i32 4, i32 62, i32 57, i32 46, i32 52, i32 38, i32 26, i32 32, i32 41, i32 50, i32 36, i32 17, i32 19, i32 29, i32 10, i32 13, i32 21, i32 56, i32 45, i32 25, i32 31, i32 35, i32 16, i32 9, i32 12, i32 44, i32 24, i32 15, i32 8, i32 23, i32 7, i32 6, i32 5], align 4

define i32 @ctz6(i64 %n) {
; CHECK-LABEL: @ctz6(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[TMP0:%.*]] = call i64 @llvm.cttz.i64(i64 [[N:%.*]], i1 true)
; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq i64 [[N]], 0
; CHECK-NEXT:    [[TMP2:%.*]] = select i1 [[TMP1]], i64 63, i64 [[TMP0]]
; CHECK-NEXT:    [[TMP3:%.*]] = trunc i64 [[TMP2]] to i32
; CHECK-NEXT:    ret i32 [[TMP3]]
;
entry:
  %add = sub i64 0, %n
  %and = and i64 %add, %n
  %mul = mul i64 %and, 571347909858961602
  %shr = lshr i64 %mul, 58
  %arrayidx = getelementptr inbounds [64 x i32], ptr @ctz6.table, i64 0, i64 %shr
  %0 = load i32, ptr %arrayidx, align 4
  ret i32 %0
}

@ctz8.table = internal unnamed_addr constant [32 x i32] [i32 31, i32 0, i32 1, i32 23, i32 2, i32 18, i32 5, i32 24, i32 21, i32 3, i32 19, i32 9, i32 15, i32 6, i32 11, i32 25, i32 30, i32 22, i32 17, i32 4, i32 20, i32 8, i32 14, i32 10, i32 29, i32 16, i32 7, i32 13, i32 28, i32 12, i32 27, i32 26], align 4

define i32 @ctz8(i32 %v) {
; CHECK-LABEL: @ctz8(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[TMP0:%.*]] = call i32 @llvm.cttz.i32(i32 [[V:%.*]], i1 true)
; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq i32 [[V]], 0
; CHECK-NEXT:    [[TMP2:%.*]] = select i1 [[TMP1]], i32 31, i32 [[TMP0]]
; CHECK-NEXT:    ret i32 [[TMP2]]
;
entry:
  %sub = sub i32 0, %v
  %and = and i32 %sub, %v
  %mul = mul i32 %and, 162449982
  %shr = lshr i32 %mul, 27
  %idxprom = zext i32 %shr to i64
  %arrayidx = getelementptr inbounds [32 x i32], ptr @ctz8.table, i64 0, i64 %idxprom
  %0 = load i32, ptr %arrayidx, align 4
  ret i32 %0
}

;; This has a wrong table size but is otherwise fine.
@ctz9.table = internal unnamed_addr constant [128 x i8] c"\00\01\1C\02\1D\0E\18\03\1E\16\14\0F\19\11\04\08\1F\1B\0D\17\15\13\10\07\1A\0C\12\06\0B\05\0A\09\00\01\1C\02\1D\0E\18\03\1E\16\14\0F\19\11\04\08\1F\1B\0D\17\15\13\10\07\1A\0C\12\06\0B\05\0A\09\00\01\1C\02\1D\0E\18\03\1E\16\14\0F\19\11\04\08\1F\1B\0D\17\15\13\10\07\1A\0C\12\06\0B\05\0A\09\00\01\1C\02\1D\0E\18\03\1E\16\14\0F\19\11\04\08\1F\1B\0D\17\15\13\10\07\1A\0C\12\06\0B\05\0A\09", align 1
define i32 @ctz9(i32 %x) {
; CHECK-LABEL: @ctz9(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[TMP0:%.*]] = call i32 @llvm.cttz.i32(i32 [[X:%.*]], i1 true)
; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq i32 [[X]], 0
; CHECK-NEXT:    [[TMP2:%.*]] = select i1 [[TMP1]], i32 0, i32 [[TMP0]]
; CHECK-NEXT:    [[TMP3:%.*]] = trunc i32 [[TMP2]] to i8
; CHECK-NEXT:    [[CONV:%.*]] = zext i8 [[TMP3]] to i32
; CHECK-NEXT:    ret i32 [[CONV]]
;
entry:
  %sub = sub i32 0, %x
  %and = and i32 %sub, %x
  %mul = mul i32 %and, 125613361
  %shr = lshr i32 %mul, 27
  %idxprom = zext i32 %shr to i64
  %arrayidx = getelementptr inbounds [128 x i8], ptr @ctz9.table, i64 0, i64 %idxprom
  %0 = load i8, ptr %arrayidx, align 1
  %conv = zext i8 %0 to i32
  ret i32 %conv
}

define i32 @ctz1_with_i8_gep(i32 %x) {
; CHECK-LABEL: @ctz1_with_i8_gep(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[TMP0:%.*]] = call i32 @llvm.cttz.i32(i32 [[X:%.*]], i1 true)
; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq i32 [[X]], 0
; CHECK-NEXT:    [[TMP2:%.*]] = select i1 [[TMP1]], i32 0, i32 [[TMP0]]
; CHECK-NEXT:    [[TMP3:%.*]] = trunc i32 [[TMP2]] to i8
; CHECK-NEXT:    [[CONV:%.*]] = zext i8 [[TMP3]] to i32
; CHECK-NEXT:    ret i32 [[CONV]]
;
entry:
  %sub = sub i32 0, %x
  %and = and i32 %sub, %x
  %mul = mul i32 %and, 125613361
  %shr = lshr i32 %mul, 27
  %idxprom = zext i32 %shr to i64
  %arrayidx = getelementptr inbounds i8, ptr @ctz7.table, i64 %idxprom
  %0 = load i8, ptr %arrayidx, align 1
  %conv = zext i8 %0 to i32
  ret i32 %conv
}

; This is the same a ctz2 (i16 table) with an i8 gep making the indices invalid
define i32 @ctz2_with_i8_gep(i32 %x) {
; CHECK-LABEL: @ctz2_with_i8_gep(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[SUB:%.*]] = sub i32 0, [[X:%.*]]
; CHECK-NEXT:    [[AND:%.*]] = and i32 [[SUB]], [[X]]
; CHECK-NEXT:    [[MUL:%.*]] = mul i32 [[AND]], 72416175
; CHECK-NEXT:    [[SHR:%.*]] = lshr i32 [[MUL]], 26
; CHECK-NEXT:    [[IDXPROM:%.*]] = zext i32 [[SHR]] to i64
; CHECK-NEXT:    [[ARRAYIDX:%.*]] = getelementptr inbounds [64 x i8], ptr @ctz2.table, i64 0, i64 [[IDXPROM]]
; CHECK-NEXT:    [[TMP0:%.*]] = load i16, ptr [[ARRAYIDX]], align 1
; CHECK-NEXT:    [[CONV:%.*]] = sext i16 [[TMP0]] to i32
; CHECK-NEXT:    ret i32 [[CONV]]
;
entry:
  %sub = sub i32 0, %x
  %and = and i32 %sub, %x
  %mul = mul i32 %and, 72416175
  %shr = lshr i32 %mul, 26
  %idxprom = zext i32 %shr to i64
  %arrayidx = getelementptr inbounds [64 x i8], ptr @ctz2.table, i64 0, i64 %idxprom
  %0 = load i16, ptr %arrayidx, align 1
  %conv = sext i16 %0 to i32
  ret i32 %conv
}

; This is the same a ctz2_with_i8_gep but with the gep index multiplied by 2.
define i32 @ctz2_with_i8_gep_fixed(i32 %x) {
; CHECK-LABEL: @ctz2_with_i8_gep_fixed(
; CHECK-NEXT:    [[TMP1:%.*]] = call i32 @llvm.cttz.i32(i32 [[X:%.*]], i1 false)
; CHECK-NEXT:    [[TMP2:%.*]] = trunc i32 [[TMP1]] to i16
; CHECK-NEXT:    [[CONV:%.*]] = sext i16 [[TMP2]] to i32
; CHECK-NEXT:    ret i32 [[CONV]]
;
  %sub = sub i32 0, %x
  %and = and i32 %x, %sub
  %mul = mul i32 %and, 72416175
  %shr = lshr i32 %mul, 25
  %shr2 = and i32 %shr, 126
  %1 = zext nneg i32 %shr2 to i64
  %arrayidx = getelementptr inbounds nuw i8, ptr @ctz2.table, i64 %1
  %2 = load i16, ptr %arrayidx, align 2
  %conv = sext i16 %2 to i32
  ret i32 %conv
}

; This is a i16 input with the debruijn table stored in a single i128.
@tablei128 = internal unnamed_addr constant i128 16018378897745984667142067713738932480, align 16
define i32 @cttz_i16_via_i128(i16 noundef %x) {
; CHECK-LABEL: @cttz_i16_via_i128(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[TMP0:%.*]] = call i16 @llvm.cttz.i16(i16 [[X:%.*]], i1 true)
; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i16 [[X]], 0
; CHECK-NEXT:    [[TMP2:%.*]] = select i1 [[TMP3]], i16 0, i16 [[TMP0]]
; CHECK-NEXT:    [[TMP1:%.*]] = trunc i16 [[TMP2]] to i8
; CHECK-NEXT:    [[CONV6:%.*]] = zext i8 [[TMP1]] to i32
; CHECK-NEXT:    ret i32 [[CONV6]]
;
entry:
  %sub = sub i16 0, %x
  %and = and i16 %x, %sub
  %mul = mul i16 %and, 2479
  %0 = lshr i16 %mul, 12
  %idxprom = zext nneg i16 %0 to i64
  %arrayidx = getelementptr inbounds nuw i8, ptr @tablei128, i64 %idxprom
  %1 = load i8, ptr %arrayidx, align 1
  %conv6 = zext i8 %1 to i32
  ret i32 %conv6
}

; Same as above but the table is a little off
@tablei128b = internal unnamed_addr constant i128 16018378897745984667142068813250560256, align 16
define i32 @cttz_i16_via_i128_incorrecttable(i16 noundef %x) {
; CHECK-LABEL: @cttz_i16_via_i128_incorrecttable(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[SUB:%.*]] = sub i16 0, [[X:%.*]]
; CHECK-NEXT:    [[AND:%.*]] = and i16 [[X]], [[SUB]]
; CHECK-NEXT:    [[MUL:%.*]] = mul i16 [[AND]], 2479
; CHECK-NEXT:    [[TMP0:%.*]] = lshr i16 [[MUL]], 12
; CHECK-NEXT:    [[IDXPROM:%.*]] = zext nneg i16 [[TMP0]] to i64
; CHECK-NEXT:    [[ARRAYIDX:%.*]] = getelementptr inbounds nuw i8, ptr @tablei128b, i64 [[IDXPROM]]
; CHECK-NEXT:    [[TMP3:%.*]] = load i8, ptr [[ARRAYIDX]], align 1
; CHECK-NEXT:    [[CONV6:%.*]] = zext i8 [[TMP3]] to i32
; CHECK-NEXT:    ret i32 [[CONV6]]
;
entry:
  %sub = sub i16 0, %x
  %and = and i16 %x, %sub
  %mul = mul i16 %and, 2479
  %0 = lshr i16 %mul, 12
  %idxprom = zext nneg i16 %0 to i64
  %arrayidx = getelementptr inbounds nuw i8, ptr @tablei128b, i64 %idxprom
  %1 = load i8, ptr %arrayidx, align 1
  %conv6 = zext i8 %1 to i32
  ret i32 %conv6
}

; Same as ctz1 but the table and load is very large
@ctz7i128.table = internal unnamed_addr constant [32 x i128] [i128 0, i128 1, i128 28, i128 2, i128 29, i128 14, i128 24, i128 3, i128 30, i128 22, i128 20, i128 15, i128 25, i128 17, i128 4, i128 8, i128 31, i128 27, i128 13, i128 23, i128 21, i128 19, i128 16, i128 7, i128 26, i128 12, i128 18, i128 6, i128 11, i128 5, i128 10, i128 9], align 16
define i128 @ctz1_i128(i32 %x) {
; CHECK-LABEL: @ctz1_i128(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[TMP0:%.*]] = call i32 @llvm.cttz.i32(i32 [[X:%.*]], i1 true)
; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq i32 [[X]], 0
; CHECK-NEXT:    [[TMP2:%.*]] = select i1 [[TMP1]], i32 0, i32 [[TMP0]]
; CHECK-NEXT:    [[TMP3:%.*]] = zext i32 [[TMP2]] to i128
; CHECK-NEXT:    ret i128 [[TMP3]]
;
entry:
  %sub = sub i32 0, %x
  %and = and i32 %sub, %x
  %mul = mul i32 %and, 125613361
  %shr = lshr i32 %mul, 27
  %idxprom = zext i32 %shr to i64
  %arrayidx = getelementptr inbounds [32 x i128], ptr @ctz7i128.table, i64 0, i64 %idxprom
  %l = load i128, ptr %arrayidx, align 1
  ret i128 %l
}

; This is roughly the same as ctz1 but using i128.
@table.i128 = internal unnamed_addr constant [128 x i8] c"\00\01e\02tf<\03|ug^R=!\04}yvWoh_5ZSE>0\22\14\05~rzPwmX.pkiI`K6\1Ab[TBMF?'81*#\1C\15\0E\06\7Fds;{]Q xVn4YD/\13qOl-jHJ\19aAL&7)\1B\0Dc:\\\1FU3C\12N,G\18@%(\0C9\1E2\11+\17$\0B\1D\10\16\0A\0F\09\08\07", align 1
define i32 @src(i128 noundef %x) {
; CHECK-LABEL: @src(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[TMP3:%.*]] = call i128 @llvm.cttz.i128(i128 [[X:%.*]], i1 true)
; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq i128 [[X]], 0
; CHECK-NEXT:    [[TMP2:%.*]] = select i1 [[TMP1]], i128 0, i128 [[TMP3]]
; CHECK-NEXT:    [[TMP0:%.*]] = trunc i128 [[TMP2]] to i8
; CHECK-NEXT:    [[CONV:%.*]] = zext i8 [[TMP0]] to i32
; CHECK-NEXT:    ret i32 [[CONV]]
;
entry:
  %sub = sub i128 0, %x
  %and = and i128 %x, %sub
  %mul = mul i128 %and, 2647824804797170443043024478319300753
  %shr = lshr i128 %mul, 121
  %idxprom = trunc i128 %shr to i64
  %arrayidx = getelementptr inbounds nuw i8, ptr @table.i128, i64 %idxprom
  %0 = load i8, ptr %arrayidx, align 1
  %conv = zext i8 %0 to i32
  ret i32 %conv
}