File: index-canonicalize.mlir

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (549 lines) | stat: -rw-r--r-- 13,278 bytes parent folder | download | duplicates (2)
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
// RUN: mlir-opt %s -canonicalize | FileCheck %s

// CHECK-LABEL: @add
func.func @add() -> (index, index) {
  %0 = index.constant 1
  %1 = index.constant 2100
  %2 = index.constant 3000000001
  %3 = index.constant 4000002100
  // Folds normally.
  %4 = index.add %0, %1
  // Folds even though values exceed INT32_MAX.
  %5 = index.add %2, %3

  // CHECK-DAG: %[[A:.*]] = index.constant 2101
  // CHECK-DAG: %[[B:.*]] = index.constant 7000002101
  // CHECK: return %[[A]], %[[B]]
  return %4, %5 : index, index
}

// CHECK-LABEL: @add_overflow
func.func @add_overflow() -> (index, index) {
  %0 = index.constant 2000000000
  %1 = index.constant 8000000000000000000
  // Folds normally.
  %2 = index.add %0, %0
  // Folds and overflows.
  %3 = index.add %1, %1

  // CHECK-DAG: %[[A:.*]] = index.constant 4{{0+}}
  // CHECK-DAG: %[[B:.*]] = index.constant -2446{{[0-9]+}}
  // CHECK: return %[[A]], %[[B]]
  return %2, %3 : index, index
}

// CHECK-LABEL: @sub
func.func @sub() -> index {
  %0 = index.constant -2000000000
  %1 = index.constant 3000000000
  %2 = index.sub %0, %1
  // CHECK: %[[A:.*]] = index.constant -5{{0+}}
  // CHECK: return %[[A]]
  return %2 : index
}

// CHECK-LABEL: @mul
func.func @mul() -> index {
  %0 = index.constant 8000000002000000000
  %1 = index.constant 2
  %2 = index.mul %0, %1
  // CHECK: %[[A:.*]] = index.constant -2446{{[0-9]+}}
  // CHECK: return %[[A]]
  return %2 : index
}

// CHECK-LABEL: @divs
func.func @divs() -> index {
  %0 = index.constant -2
  %1 = index.constant 0x200000000
  %2 = index.divs %1, %0
  // CHECK: %[[A:.*]] = index.constant -429{{[0-9]+}}
  // CHECK: return %[[A]]
  return %2 : index
}

// CHECK-LABEL: @divs_nofold
func.func @divs_nofold() -> (index, index) {
  %0 = index.constant 0
  %1 = index.constant 0x100000000
  %2 = index.constant 2

  // Divide by zero.
  // CHECK: index.divs
  %3 = index.divs %2, %0
  // 32-bit result differs from 64-bit.
  // CHECK: index.divs
  %4 = index.divs %1, %2

  return %3, %4 : index, index
}

// CHECK-LABEL: @divu
func.func @divu() -> index {
  %0 = index.constant -2
  %1 = index.constant 0x200000000
  %2 = index.divu %1, %0
  // CHECK: %[[A:.*]] = index.constant 0
  // CHECK: return %[[A]]
  return %2 : index
}

// CHECK-LABEL: @divu_nofold
func.func @divu_nofold() -> (index, index) {
  %0 = index.constant 0
  %1 = index.constant 0x100000000
  %2 = index.constant 2

  // Divide by zero.
  // CHECK: index.divu
  %3 = index.divu %2, %0
  // 32-bit result differs from 64-bit.
  // CHECK: index.divu
  %4 = index.divu %1, %2

  return %3, %4 : index, index
}

// CHECK-LABEL: @ceildivs
func.func @ceildivs() -> (index, index, index) {
  %c0 = index.constant 0
  %c2 = index.constant 2
  %c5 = index.constant 5

  // CHECK-DAG: %[[A:.*]] = index.constant 0
  %0 = index.ceildivs %c0, %c5

  // CHECK-DAG: %[[B:.*]] = index.constant 1
  %1 = index.ceildivs %c2, %c5

  // CHECK-DAG: %[[C:.*]] = index.constant 3
  %2 = index.ceildivs %c5, %c2

  // CHECK: return %[[A]], %[[B]], %[[C]]
  return %0, %1, %2 : index, index, index
}

// CHECK-LABEL: @ceildivs_neg
func.func @ceildivs_neg() -> index {
  %c5 = index.constant -5
  %c2 = index.constant 2
  // CHECK: %[[A:.*]] = index.constant -2
  %0 = index.ceildivs %c5, %c2
  // CHECK: return %[[A]]
  return %0 : index
}

// CHECK-LABEL: @ceildivs_edge
func.func @ceildivs_edge() -> (index, index) {
  %cn1 = index.constant -1
  %cIntMin = index.constant -2147483648
  %cIntMax = index.constant 2147483647

  // The result is 0 on 32-bit.
  // CHECK-DAG: %[[A:.*]] = index.constant 2147483648
  %0 = index.ceildivs %cIntMin, %cn1

  // CHECK-DAG: %[[B:.*]] = index.constant -2147483647
  %1 = index.ceildivs %cIntMax, %cn1

  // CHECK: return %[[A]], %[[B]]
  return %0, %1 : index, index
}

// CHECK-LABEL: @ceildivu
func.func @ceildivu() -> index {
  %0 = index.constant 0x200000001
  %1 = index.constant 2
  // CHECK: %[[A:.*]] = index.constant 429{{[0-9]+}}7
  %2 = index.ceildivu %0, %1
  // CHECK: return %[[A]]
  return %2 : index
}

// CHECK-LABEL: @floordivs
func.func @floordivs() -> index {
  %0 = index.constant -5
  %1 = index.constant 2
  // CHECK: %[[A:.*]] = index.constant -3
  %2 = index.floordivs %0, %1
  // CHECK: return %[[A]]
  return %2 : index
}

// CHECK-LABEL: @floordivs_edge
func.func @floordivs_edge() -> (index, index) {
  %cIntMin = index.constant -2147483648
  %cIntMax = index.constant 2147483647
  %n1 = index.constant -1
  %p1 = index.constant 1

  // CHECK-DAG: %[[A:.*]] = index.constant -2147483648
  // CHECK-DAG: %[[B:.*]] = index.constant -2147483647
  %0 = index.floordivs %cIntMin, %p1
  %1 = index.floordivs %cIntMax, %n1

  // CHECK: return %[[A]], %[[B]]
  return %0, %1 : index, index
}

// CHECK-LABEL: @floordivs_nofold
func.func @floordivs_nofold() -> index {
  %lhs = index.constant 0x100000000
  %c2 = index.constant 2

  // 32-bit result differs from 64-bit.
  // CHECK: index.floordivs
  %0 = index.floordivs %lhs, %c2

  return %0 : index
}

// CHECK-LABEL: @rems_zerodiv_nofold
func.func @rems_zerodiv_nofold() -> index {
  %lhs = index.constant 2
  %rhs = index.constant 0
  // CHECK: index.rems
  %0 = index.rems %lhs, %rhs
  return %0 : index
}

// CHECK-LABEL: @remu_zerodiv_nofold
func.func @remu_zerodiv_nofold() -> index {
  %lhs = index.constant 2
  %rhs = index.constant 0
  // CHECK: index.remu
  %0 = index.remu %lhs, %rhs
  return %0 : index
}

// CHECK-LABEL: @rems
func.func @rems() -> index {
  %lhs = index.constant -5
  %rhs = index.constant 2
  // CHECK: %[[A:.*]] = index.constant -1
  %0 = index.rems %lhs, %rhs
  // CHECK: return %[[A]]
  return %0 : index
}

// CHECK-LABEL: @rems_nofold
func.func @rems_nofold() -> index {
  %lhs = index.constant 2
  %rhs = index.constant 0x100000001
  // 32-bit result differs from 64-bit.
  // CHECK: index.rems
  %0 = index.rems %lhs, %rhs
  return %0 : index
}

// CHECK-LABEL: @remu
func.func @remu() -> index {
  %lhs = index.constant 2
  %rhs = index.constant -1
  // CHECK: %[[A:.*]] = index.constant 2
  %0 = index.remu %lhs, %rhs
  // CHECK: return %[[A]]
  return %0 : index
}

// CHECK-LABEL: @remu_nofold
func.func @remu_nofold() -> index {
  %lhs = index.constant 2
  %rhs = index.constant 0x100000001
  // 32-bit result differs from 64-bit.
  // CHECK: index.remu
  %0 = index.remu %lhs, %rhs
  return %0 : index
}

// CHECK-LABEL: @maxs
func.func @maxs() -> index {
  %lhs = index.constant -4
  %rhs = index.constant 2
  // CHECK: %[[A:.*]] = index.constant 2
  %0 = index.maxs %lhs, %rhs
  // CHECK: return %[[A]]
  return %0 : index
}

// CHECK-LABEL: @maxs_nofold
func.func @maxs_nofold() -> index {
  %lhs = index.constant 1
  %rhs = index.constant 0x100000000
  // 32-bit result differs from 64-bit.
  // CHECK: index.maxs
  %0 = index.maxs %lhs, %rhs
  return %0 : index
}

// CHECK-LABEL: @maxs_edge
func.func @maxs_edge() -> index {
  %lhs = index.constant 1
  %rhs = index.constant 0x100000001
  // Truncated 64-bit result is the same as 32-bit.
  // CHECK: %[[A:.*]] = index.constant 429{{[0-9]+}}
  %0 = index.maxs %lhs, %rhs
  // CHECK: return %[[A]]
  return %0 : index
}

// CHECK-LABEL: @maxu
func.func @maxu() -> index {
  %lhs = index.constant -1
  %rhs = index.constant 1
  // CHECK: %[[A:.*]] = index.constant -1
  %0 = index.maxu %lhs, %rhs
  // CHECK: return %[[A]]
  return %0 : index
}

// CHECK-LABEL: @mins
func.func @mins() -> index {
  %lhs = index.constant -4
  %rhs = index.constant 2
  // CHECK: %[[A:.*]] = index.constant -4
  %0 = index.mins %lhs, %rhs
  // CHECK: return %[[A]]
  return %0 : index
}

// CHECK-LABEL: @mins_nofold
func.func @mins_nofold() -> index {
  %lhs = index.constant 1
  %rhs = index.constant 0x100000000
  // 32-bit result differs from 64-bit.
  // CHECK: index.mins
  %0 = index.mins %lhs, %rhs
  return %0 : index
}

// CHECK-LABEL: @mins_nofold_2
func.func @mins_nofold_2() -> index {
  %lhs = index.constant 0x7fffffff
  %rhs = index.constant 0x80000000
  // 32-bit result differs from 64-bit.
  // CHECK: index.mins
  %0 = index.mins %lhs, %rhs
  return %0 : index
}

// CHECK-LABEL: @minu
func.func @minu() -> index {
  %lhs = index.constant -1
  %rhs = index.constant 1
  // CHECK: %[[A:.*]] = index.constant 1
  %0 = index.minu %lhs, %rhs
  // CHECK: return %[[A]]
  return %0 : index
}

// CHECK-LABEL: @shl
func.func @shl() -> index {
  %lhs = index.constant 128
  %rhs = index.constant 2
  // CHECK: %[[A:.*]] = index.constant 512
  %0 = index.shl %lhs, %rhs
  // CHECK: return %[[A]]
  return %0 : index
}

// CHECK-LABEL: @shl_32
func.func @shl_32() -> index {
  %lhs = index.constant 1
  %rhs = index.constant 32
  // CHECK: index.shl
  %0 = index.shl %lhs, %rhs
  return %0 : index
}

// CHECK-LABEL: @shl_edge
func.func @shl_edge() -> index {
  %lhs = index.constant 4000000000
  %rhs = index.constant 31
  // CHECK: %[[A:.*]] = index.constant 858{{[0-9]+}}
  %0 = index.shl %lhs, %rhs
  // CHECK: return %[[A]]
  return %0 : index
}

// CHECK-LABEL: @shrs
func.func @shrs() -> index {
  %lhs = index.constant 128
  %rhs = index.constant 2
  // CHECK: %[[A:.*]] = index.constant 32
  %0 = index.shrs %lhs, %rhs
  // CHECK: return %[[A]]
  return %0 : index
}

// CHECK-LABEL: @shrs_32
func.func @shrs_32() -> index {
  %lhs = index.constant 4000000000000
  %rhs = index.constant 32
  // CHECK: index.shrs
  %0 = index.shrs %lhs, %rhs
  return %0 : index
}

// CHECK-LABEL: @shrs_nofold
func.func @shrs_nofold() -> index {
  %lhs = index.constant 0x100000000
  %rhs = index.constant 1
  // CHECK: index.shrs
  %0 = index.shrs %lhs, %rhs
  return %0 : index
}

// CHECK-LABEL: @shrs_edge
func.func @shrs_edge() -> index {
  %lhs = index.constant 0x10000000000
  %rhs = index.constant 3
  // CHECK: %[[A:.*]] = index.constant 137{{[0-9]+}}
  %0 = index.shrs %lhs, %rhs
  // CHECK: return %[[A]]
  return %0 : index
}

// CHECK-LABEL: @shru
func.func @shru() -> index {
  %lhs = index.constant 128
  %rhs = index.constant 2
  // CHECK: %[[A:.*]] = index.constant 32
  %0 = index.shru %lhs, %rhs
  // CHECK: return %[[A]]
  return %0 : index
}

// CHECK-LABEL: @shru_32
func.func @shru_32() -> index {
  %lhs = index.constant 4000000000000
  %rhs = index.constant 32
  // CHECK: index.shru
  %0 = index.shru %lhs, %rhs
  return %0 : index
}

// CHECK-LABEL: @shru_nofold
func.func @shru_nofold() -> index {
  %lhs = index.constant 0x100000000
  %rhs = index.constant 1
  // CHECK: index.shru
  %0 = index.shru %lhs, %rhs
  return %0 : index
}

// CHECK-LABEL: @shru_edge
func.func @shru_edge() -> index {
  %lhs = index.constant 0x10000000000
  %rhs = index.constant 3
  // CHECK: %[[A:.*]] = index.constant 137{{[0-9]+}}
  %0 = index.shru %lhs, %rhs
  // CHECK: return %[[A]]
  return %0 : index
}

// CHECK-LABEL: @and
func.func @and() -> index {
  %lhs = index.constant 5
  %rhs = index.constant 1
  // CHECK: %[[A:.*]] = index.constant 1
  %0 = index.and %lhs, %rhs
  // CHECK: return %[[A]]
  return %0 : index
}

// CHECK-LABEL: @or
func.func @or() -> index {
  %lhs = index.constant 5
  %rhs = index.constant 2
  // CHECK: %[[A:.*]] = index.constant 7
  %0 = index.or %lhs, %rhs
  // CHECK: return %[[A]]
  return %0 : index
}

// CHECK-LABEL: @xor
func.func @xor() -> index {
  %lhs = index.constant 5
  %rhs = index.constant 1
  // CHECK: %[[A:.*]] = index.constant 4
  %0 = index.xor %lhs, %rhs
  // CHECK: return %[[A]]
  return %0 : index
}

// CHECK-LABEL: @cmp
func.func @cmp() -> (i1, i1, i1, i1) {
  %a = index.constant 0
  %b = index.constant -1
  %c = index.constant -2
  %d = index.constant 4

  %0 = index.cmp slt(%a, %b)
  %1 = index.cmp ugt(%b, %a)
  %2 = index.cmp ne(%d, %a)
  %3 = index.cmp sgt(%b, %a)

  // CHECK-DAG: %[[TRUE:.*]] = index.bool.constant true
  // CHECK-DAG: %[[FALSE:.*]] = index.bool.constant false
  // CHECK: return %[[FALSE]], %[[TRUE]], %[[TRUE]], %[[FALSE]]
  return %0, %1, %2, %3 : i1, i1, i1, i1
}

// CHECK-LABEL: @cmp_nofold
func.func @cmp_nofold() -> i1 {
  %lhs = index.constant 1
  %rhs = index.constant 0x100000000
  // 32-bit result differs from 64-bit.
  // CHECK: index.cmp slt
  %0 = index.cmp slt(%lhs, %rhs)
  return %0 : i1
}

// CHECK-LABEL: @cmp_edge
func.func @cmp_edge() -> i1 {
  %lhs = index.constant 1
  %rhs = index.constant 0x100000002
  // 64-bit result is the same as 32-bit.
  // CHECK: %[[TRUE:.*]] = index.bool.constant true
  %0 = index.cmp slt(%lhs, %rhs)
  // CHECK: return %[[TRUE]]
  return %0 : i1
}

// CHECK-LABEL: @cmp_maxs
func.func @cmp_maxs(%arg0: index) -> (i1, i1) {
  %idx0 = index.constant 0
  %idx1 = index.constant 1
  %0 = index.maxs %arg0, %idx1
  %1 = index.cmp sgt(%0, %idx0)
  %2 = index.cmp eq(%0, %idx0)
  // CHECK: return %true, %false
  return %1, %2 : i1, i1
}

// CHECK-LABEL: @mul_identity
func.func @mul_identity(%arg0: index) -> (index, index) {
  %idx0 = index.constant 0
  %idx1 = index.constant 1
  %0 = index.mul %arg0, %idx0
  %1 = index.mul %arg0, %idx1
  // CHECK: return %idx0, %arg0
  return %0, %1 : index, index
}

// CHECK-LABEL: @add_identity
func.func @add_identity(%arg0: index) -> index {
  %idx0 = index.constant 0
  %0 = index.add %arg0, %idx0
  // CHECK-NEXT: return %arg0
  return %0 : index
}

// CHECK-LABEL: @sub_identity
func.func @sub_identity(%arg0: index) -> index {
  %idx0 = index.constant 0
  %0 = index.sub %arg0, %idx0
  // CHECK-NEXT: return %arg0
  return %0 : index
}