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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
|
; RUN: opt < %s -instsimplify -S | FileCheck %s
target datalayout = "p:32:32"
define i1 @ptrtoint() {
; CHECK: @ptrtoint
%a = alloca i8
%tmp = ptrtoint i8* %a to i32
%r = icmp eq i32 %tmp, 0
ret i1 %r
; CHECK: ret i1 false
}
define i1 @bitcast() {
; CHECK: @bitcast
%a = alloca i32
%b = alloca i64
%x = bitcast i32* %a to i8*
%y = bitcast i64* %b to i8*
%cmp = icmp eq i8* %x, %y
ret i1 %cmp
; CHECK-NEXT: ret i1 false
}
define i1 @gep() {
; CHECK: @gep
%a = alloca [3 x i8], align 8
%x = getelementptr inbounds [3 x i8]* %a, i32 0, i32 0
%cmp = icmp eq i8* %x, null
ret i1 %cmp
; CHECK-NEXT: ret i1 false
}
define i1 @gep2() {
; CHECK: @gep2
%a = alloca [3 x i8], align 8
%x = getelementptr inbounds [3 x i8]* %a, i32 0, i32 0
%y = getelementptr inbounds [3 x i8]* %a, i32 0, i32 0
%cmp = icmp eq i8* %x, %y
ret i1 %cmp
; CHECK-NEXT: ret i1 true
}
; PR11238
%gept = type { i32, i32 }
@gepy = global %gept zeroinitializer, align 8
@gepz = extern_weak global %gept
define i1 @gep3() {
; CHECK: @gep3
%x = alloca %gept, align 8
%a = getelementptr %gept* %x, i64 0, i32 0
%b = getelementptr %gept* %x, i64 0, i32 1
%equal = icmp eq i32* %a, %b
ret i1 %equal
; CHECK-NEXT: ret i1 false
}
define i1 @gep4() {
; CHECK: @gep4
%x = alloca %gept, align 8
%a = getelementptr %gept* @gepy, i64 0, i32 0
%b = getelementptr %gept* @gepy, i64 0, i32 1
%equal = icmp eq i32* %a, %b
ret i1 %equal
; CHECK-NEXT: ret i1 false
}
define i1 @gep5() {
; CHECK: @gep5
%x = alloca %gept, align 8
%a = getelementptr inbounds %gept* %x, i64 0, i32 1
%b = getelementptr %gept* @gepy, i64 0, i32 0
%equal = icmp eq i32* %a, %b
ret i1 %equal
; CHECK-NEXT: ret i1 false
}
define i1 @gep6(%gept* %x) {
; Same as @gep3 but potentially null.
; CHECK: @gep6
%a = getelementptr %gept* %x, i64 0, i32 0
%b = getelementptr %gept* %x, i64 0, i32 1
%equal = icmp eq i32* %a, %b
ret i1 %equal
; CHECK-NEXT: ret i1 false
}
define i1 @gep7(%gept* %x) {
; CHECK: @gep7
%a = getelementptr %gept* %x, i64 0, i32 0
%b = getelementptr %gept* @gepz, i64 0, i32 0
%equal = icmp eq i32* %a, %b
ret i1 %equal
; CHECK: ret i1 %equal
}
define i1 @gep8(%gept* %x) {
; CHECK: @gep8
%a = getelementptr %gept* %x, i32 1
%b = getelementptr %gept* %x, i32 -1
%equal = icmp ugt %gept* %a, %b
ret i1 %equal
; CHECK: ret i1 %equal
}
define i1 @gep9(i8* %ptr) {
; CHECK: @gep9
; CHECK-NOT: ret
; CHECK: ret i1 true
entry:
%first1 = getelementptr inbounds i8* %ptr, i32 0
%first2 = getelementptr inbounds i8* %first1, i32 1
%first3 = getelementptr inbounds i8* %first2, i32 2
%first4 = getelementptr inbounds i8* %first3, i32 4
%last1 = getelementptr inbounds i8* %first2, i32 48
%last2 = getelementptr inbounds i8* %last1, i32 8
%last3 = getelementptr inbounds i8* %last2, i32 -4
%last4 = getelementptr inbounds i8* %last3, i32 -4
%first.int = ptrtoint i8* %first4 to i32
%last.int = ptrtoint i8* %last4 to i32
%cmp = icmp ne i32 %last.int, %first.int
ret i1 %cmp
}
define i1 @gep10(i8* %ptr) {
; CHECK: @gep10
; CHECK-NOT: ret
; CHECK: ret i1 true
entry:
%first1 = getelementptr inbounds i8* %ptr, i32 -2
%first2 = getelementptr inbounds i8* %first1, i32 44
%last1 = getelementptr inbounds i8* %ptr, i32 48
%last2 = getelementptr inbounds i8* %last1, i32 -6
%first.int = ptrtoint i8* %first2 to i32
%last.int = ptrtoint i8* %last2 to i32
%cmp = icmp eq i32 %last.int, %first.int
ret i1 %cmp
}
define i1 @gep11(i8* %ptr) {
; CHECK: @gep11
; CHECK-NOT: ret
; CHECK: ret i1 true
entry:
%first1 = getelementptr inbounds i8* %ptr, i32 -2
%last1 = getelementptr inbounds i8* %ptr, i32 48
%last2 = getelementptr inbounds i8* %last1, i32 -6
%cmp = icmp ult i8* %first1, %last2
ret i1 %cmp
}
define i1 @gep12(i8* %ptr) {
; CHECK: @gep12
; CHECK-NOT: ret
; CHECK: ret i1 %cmp
entry:
%first1 = getelementptr inbounds i8* %ptr, i32 -2
%last1 = getelementptr inbounds i8* %ptr, i32 48
%last2 = getelementptr inbounds i8* %last1, i32 -6
%cmp = icmp slt i8* %first1, %last2
ret i1 %cmp
}
define i1 @gep13(i8* %ptr) {
; CHECK: @gep13
; We can prove this GEP is non-null because it is inbounds.
%x = getelementptr inbounds i8* %ptr, i32 1
%cmp = icmp eq i8* %x, null
ret i1 %cmp
; CHECK-NEXT: ret i1 false
}
define i1 @gep14({ {}, i8 }* %ptr) {
; CHECK: @gep14
; We can't simplify this because the offset of one in the GEP actually doesn't
; move the pointer.
%x = getelementptr inbounds { {}, i8 }* %ptr, i32 0, i32 1
%cmp = icmp eq i8* %x, null
ret i1 %cmp
; CHECK-NOT: ret i1 false
}
define i1 @gep15({ {}, [4 x {i8, i8}]}* %ptr, i32 %y) {
; CHECK: @gep15
; We can prove this GEP is non-null even though there is a user value, as we
; would necessarily violate inbounds on one side or the other.
%x = getelementptr inbounds { {}, [4 x {i8, i8}]}* %ptr, i32 0, i32 1, i32 %y, i32 1
%cmp = icmp eq i8* %x, null
ret i1 %cmp
; CHECK-NEXT: ret i1 false
}
define i1 @gep16(i8* %ptr, i32 %a) {
; CHECK: @gep16
; We can prove this GEP is non-null because it is inbounds and because we know
; %b is non-zero even though we don't know its value.
%b = or i32 %a, 1
%x = getelementptr inbounds i8* %ptr, i32 %b
%cmp = icmp eq i8* %x, null
ret i1 %cmp
; CHECK-NEXT: ret i1 false
}
define i1 @zext(i32 %x) {
; CHECK: @zext
%e1 = zext i32 %x to i64
%e2 = zext i32 %x to i64
%r = icmp eq i64 %e1, %e2
ret i1 %r
; CHECK: ret i1 true
}
define i1 @zext2(i1 %x) {
; CHECK: @zext2
%e = zext i1 %x to i32
%c = icmp ne i32 %e, 0
ret i1 %c
; CHECK: ret i1 %x
}
define i1 @zext3() {
; CHECK: @zext3
%e = zext i1 1 to i32
%c = icmp ne i32 %e, 0
ret i1 %c
; CHECK: ret i1 true
}
define i1 @sext(i32 %x) {
; CHECK: @sext
%e1 = sext i32 %x to i64
%e2 = sext i32 %x to i64
%r = icmp eq i64 %e1, %e2
ret i1 %r
; CHECK: ret i1 true
}
define i1 @sext2(i1 %x) {
; CHECK: @sext2
%e = sext i1 %x to i32
%c = icmp ne i32 %e, 0
ret i1 %c
; CHECK: ret i1 %x
}
define i1 @sext3() {
; CHECK: @sext3
%e = sext i1 1 to i32
%c = icmp ne i32 %e, 0
ret i1 %c
; CHECK: ret i1 true
}
define i1 @add(i32 %x, i32 %y) {
; CHECK: @add
%l = lshr i32 %x, 1
%q = lshr i32 %y, 1
%r = or i32 %q, 1
%s = add i32 %l, %r
%c = icmp eq i32 %s, 0
ret i1 %c
; CHECK: ret i1 false
}
define i1 @add2(i8 %x, i8 %y) {
; CHECK: @add2
%l = or i8 %x, 128
%r = or i8 %y, 129
%s = add i8 %l, %r
%c = icmp eq i8 %s, 0
ret i1 %c
; CHECK: ret i1 false
}
define i1 @add3(i8 %x, i8 %y) {
; CHECK: @add3
%l = zext i8 %x to i32
%r = zext i8 %y to i32
%s = add i32 %l, %r
%c = icmp eq i32 %s, 0
ret i1 %c
; CHECK: ret i1 %c
}
define i1 @add4(i32 %x, i32 %y) {
; CHECK: @add4
%z = add nsw i32 %y, 1
%s1 = add nsw i32 %x, %y
%s2 = add nsw i32 %x, %z
%c = icmp slt i32 %s1, %s2
ret i1 %c
; CHECK: ret i1 true
}
define i1 @add5(i32 %x, i32 %y) {
; CHECK: @add5
%z = add nuw i32 %y, 1
%s1 = add nuw i32 %x, %z
%s2 = add nuw i32 %x, %y
%c = icmp ugt i32 %s1, %s2
ret i1 %c
; CHECK: ret i1 true
}
define i1 @add6(i64 %A, i64 %B) {
; CHECK: @add6
%s1 = add i64 %A, %B
%s2 = add i64 %B, %A
%cmp = icmp eq i64 %s1, %s2
ret i1 %cmp
; CHECK: ret i1 true
}
define i1 @addpowtwo(i32 %x, i32 %y) {
; CHECK: @addpowtwo
%l = lshr i32 %x, 1
%r = shl i32 1, %y
%s = add i32 %l, %r
%c = icmp eq i32 %s, 0
ret i1 %c
; CHECK: ret i1 false
}
define i1 @or(i32 %x) {
; CHECK: @or
%o = or i32 %x, 1
%c = icmp eq i32 %o, 0
ret i1 %c
; CHECK: ret i1 false
}
define i1 @shl(i32 %x) {
; CHECK: @shl
%s = shl i32 1, %x
%c = icmp eq i32 %s, 0
ret i1 %c
; CHECK: ret i1 false
}
define i1 @lshr1(i32 %x) {
; CHECK: @lshr1
%s = lshr i32 -1, %x
%c = icmp eq i32 %s, 0
ret i1 %c
; CHECK: ret i1 false
}
define i1 @lshr2(i32 %x) {
; CHECK: @lshr2
%s = lshr i32 %x, 30
%c = icmp ugt i32 %s, 8
ret i1 %c
; CHECK: ret i1 false
}
define i1 @ashr1(i32 %x) {
; CHECK: @ashr1
%s = ashr i32 -1, %x
%c = icmp eq i32 %s, 0
ret i1 %c
; CHECK: ret i1 false
}
define i1 @ashr2(i32 %x) {
; CHECK: @ashr2
%s = ashr i32 %x, 30
%c = icmp slt i32 %s, -5
ret i1 %c
; CHECK: ret i1 false
}
define i1 @select1(i1 %cond) {
; CHECK: @select1
%s = select i1 %cond, i32 1, i32 0
%c = icmp eq i32 %s, 1
ret i1 %c
; CHECK: ret i1 %cond
}
define i1 @select2(i1 %cond) {
; CHECK: @select2
%x = zext i1 %cond to i32
%s = select i1 %cond, i32 %x, i32 0
%c = icmp ne i32 %s, 0
ret i1 %c
; CHECK: ret i1 %cond
}
define i1 @select3(i1 %cond) {
; CHECK: @select3
%x = zext i1 %cond to i32
%s = select i1 %cond, i32 1, i32 %x
%c = icmp ne i32 %s, 0
ret i1 %c
; CHECK: ret i1 %cond
}
define i1 @select4(i1 %cond) {
; CHECK: @select4
%invert = xor i1 %cond, 1
%s = select i1 %invert, i32 0, i32 1
%c = icmp ne i32 %s, 0
ret i1 %c
; CHECK: ret i1 %cond
}
define i1 @select5(i32 %x) {
; CHECK: @select5
%c = icmp eq i32 %x, 0
%s = select i1 %c, i32 1, i32 %x
%c2 = icmp eq i32 %s, 0
ret i1 %c2
; CHECK: ret i1 false
}
define i1 @select6(i32 %x) {
; CHECK: @select6
%c = icmp sgt i32 %x, 0
%s = select i1 %c, i32 %x, i32 4
%c2 = icmp eq i32 %s, 0
ret i1 %c2
; CHECK: ret i1 %c2
}
define i1 @urem1(i32 %X, i32 %Y) {
; CHECK: @urem1
%A = urem i32 %X, %Y
%B = icmp ult i32 %A, %Y
ret i1 %B
; CHECK: ret i1 true
}
define i1 @urem2(i32 %X, i32 %Y) {
; CHECK: @urem2
%A = urem i32 %X, %Y
%B = icmp eq i32 %A, %Y
ret i1 %B
; CHECK: ret i1 false
}
define i1 @urem3(i32 %X) {
; CHECK: @urem3
%A = urem i32 %X, 10
%B = icmp ult i32 %A, 15
ret i1 %B
; CHECK: ret i1 true
}
define i1 @urem4(i32 %X) {
; CHECK: @urem4
%A = urem i32 %X, 15
%B = icmp ult i32 %A, 10
ret i1 %B
; CHECK: ret i1 %B
}
define i1 @urem5(i16 %X, i32 %Y) {
; CHECK: @urem5
%A = zext i16 %X to i32
%B = urem i32 %A, %Y
%C = icmp slt i32 %B, %Y
ret i1 %C
; CHECK: ret i1 true
}
define i1 @urem6(i32 %X, i32 %Y) {
; CHECK: @urem6
%A = urem i32 %X, %Y
%B = icmp ugt i32 %Y, %A
ret i1 %B
; CHECK: ret i1 true
}
define i1 @srem1(i32 %X) {
; CHECK: @srem1
%A = srem i32 %X, -5
%B = icmp sgt i32 %A, 5
ret i1 %B
; CHECK: ret i1 false
}
; PR9343 #15
; CHECK: @srem2
; CHECK: ret i1 false
define i1 @srem2(i16 %X, i32 %Y) {
%A = zext i16 %X to i32
%B = add nsw i32 %A, 1
%C = srem i32 %B, %Y
%D = icmp slt i32 %C, 0
ret i1 %D
}
; CHECK: @srem3
; CHECK-NEXT: ret i1 false
define i1 @srem3(i16 %X, i32 %Y) {
%A = zext i16 %X to i32
%B = or i32 2147483648, %A
%C = sub nsw i32 1, %B
%D = srem i32 %C, %Y
%E = icmp slt i32 %D, 0
ret i1 %E
}
define i1 @udiv1(i32 %X) {
; CHECK: @udiv1
%A = udiv i32 %X, 1000000
%B = icmp ult i32 %A, 5000
ret i1 %B
; CHECK: ret i1 true
}
define i1 @udiv2(i32 %X, i32 %Y, i32 %Z) {
; CHECK: @udiv2
%A = udiv exact i32 10, %Z
%B = udiv exact i32 20, %Z
%C = icmp ult i32 %A, %B
ret i1 %C
; CHECK: ret i1 true
}
define i1 @udiv3(i32 %X, i32 %Y) {
; CHECK: @udiv3
%A = udiv i32 %X, %Y
%C = icmp ugt i32 %A, %X
ret i1 %C
; CHECK: ret i1 false
}
define i1 @udiv4(i32 %X, i32 %Y) {
; CHECK: @udiv4
%A = udiv i32 %X, %Y
%C = icmp ule i32 %A, %X
ret i1 %C
; CHECK: ret i1 true
}
define i1 @udiv5(i32 %X) {
; CHECK: @udiv5
%A = udiv i32 123, %X
%C = icmp ugt i32 %A, 124
ret i1 %C
; CHECK: ret i1 false
}
; PR11340
define i1 @udiv6(i32 %X) nounwind {
; CHECK: @udiv6
%A = udiv i32 1, %X
%C = icmp eq i32 %A, 0
ret i1 %C
; CHECK: ret i1 %C
}
define i1 @sdiv1(i32 %X) {
; CHECK: @sdiv1
%A = sdiv i32 %X, 1000000
%B = icmp slt i32 %A, 3000
ret i1 %B
; CHECK: ret i1 true
}
define i1 @or1(i32 %X) {
; CHECK: @or1
%A = or i32 %X, 62
%B = icmp ult i32 %A, 50
ret i1 %B
; CHECK: ret i1 false
}
define i1 @and1(i32 %X) {
; CHECK: @and1
%A = and i32 %X, 62
%B = icmp ugt i32 %A, 70
ret i1 %B
; CHECK: ret i1 false
}
define i1 @mul1(i32 %X) {
; CHECK: @mul1
; Square of a non-zero number is non-zero if there is no overflow.
%Y = or i32 %X, 1
%M = mul nuw i32 %Y, %Y
%C = icmp eq i32 %M, 0
ret i1 %C
; CHECK: ret i1 false
}
define i1 @mul2(i32 %X) {
; CHECK: @mul2
; Square of a non-zero number is positive if there is no signed overflow.
%Y = or i32 %X, 1
%M = mul nsw i32 %Y, %Y
%C = icmp sgt i32 %M, 0
ret i1 %C
; CHECK: ret i1 true
}
define i1 @mul3(i32 %X, i32 %Y) {
; CHECK: @mul3
; Product of non-negative numbers is non-negative if there is no signed overflow.
%XX = mul nsw i32 %X, %X
%YY = mul nsw i32 %Y, %Y
%M = mul nsw i32 %XX, %YY
%C = icmp sge i32 %M, 0
ret i1 %C
; CHECK: ret i1 true
}
define <2 x i1> @vectorselect1(<2 x i1> %cond) {
; CHECK: @vectorselect1
%invert = xor <2 x i1> %cond, <i1 1, i1 1>
%s = select <2 x i1> %invert, <2 x i32> <i32 0, i32 0>, <2 x i32> <i32 1, i32 1>
%c = icmp ne <2 x i32> %s, <i32 0, i32 0>
ret <2 x i1> %c
; CHECK: ret <2 x i1> %cond
}
; PR11948
define <2 x i1> @vectorselectcrash(i32 %arg1) {
%tobool40 = icmp ne i32 %arg1, 0
%cond43 = select i1 %tobool40, <2 x i16> <i16 -5, i16 66>, <2 x i16> <i16 46, i16 1>
%cmp45 = icmp ugt <2 x i16> %cond43, <i16 73, i16 21>
ret <2 x i1> %cmp45
}
; PR12013
define i1 @alloca_compare(i64 %idx) {
%sv = alloca { i32, i32, [124 x i32] }
%1 = getelementptr inbounds { i32, i32, [124 x i32] }* %sv, i32 0, i32 2, i64 %idx
%2 = icmp eq i32* %1, null
ret i1 %2
; CHECK: alloca_compare
; CHECK: ret i1 false
}
; PR12075
define i1 @infinite_gep() {
ret i1 1
unreachableblock:
%X = getelementptr i32 *%X, i32 1
%Y = icmp eq i32* %X, null
ret i1 %Y
}
|