llvm.org GIT mirror llvm / 47f39c0
[NFC][InstCombine][InstSimplify] PR43251 - and some patterns with offset != 0 https://rise4fun.com/Alive/21b git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@371537 91177308-0d34-0410-b5e6-96231b3b80d8 Roman Lebedev a month ago
2 changed file(s) with 141 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
99
1010 declare {i8, i1} @llvm.usub.with.overflow(i8, i8)
1111 declare void @useagg({i8, i1})
12
13 declare void @llvm.assume(i1)
1214
1315 ; There is a number of base patterns..
1416
370372 ;
371373 %adjusted = sub i8 %base, %offset
372374 call void @use8(i8 %adjusted)
373 %underflow = icmp ugt i8 %adjusted, %base
375 %underflow = icmp ult i8 %base, %offset
374376 call void @use1(i1 %underflow)
375377 %null = icmp eq i8 %adjusted, 0
376378 call void @use1(i1 %null)
420422 %r = and i1 %null, %underflow
421423 ret i1 %r
422424 }
425
426 ;-------------------------------------------------------------------------------
427
428 define i1 @t15(i8 %base, i8 %offset) {
429 ; CHECK-LABEL: @t15(
430 ; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[OFFSET:%.*]], 0
431 ; CHECK-NEXT: call void @llvm.assume(i1 [[CMP]])
432 ; CHECK-NEXT: [[ADJUSTED:%.*]] = sub i8 [[BASE:%.*]], [[OFFSET]]
433 ; CHECK-NEXT: call void @use8(i8 [[ADJUSTED]])
434 ; CHECK-NEXT: [[NO_UNDERFLOW:%.*]] = icmp ult i8 [[ADJUSTED]], [[BASE]]
435 ; CHECK-NEXT: call void @use1(i1 [[NO_UNDERFLOW]])
436 ; CHECK-NEXT: [[NOT_NULL:%.*]] = icmp ne i8 [[ADJUSTED]], 0
437 ; CHECK-NEXT: call void @use1(i1 [[NOT_NULL]])
438 ; CHECK-NEXT: [[R:%.*]] = and i1 [[NOT_NULL]], [[NO_UNDERFLOW]]
439 ; CHECK-NEXT: ret i1 [[R]]
440 ;
441 %cmp = icmp ne i8 %offset, 0
442 call void @llvm.assume(i1 %cmp)
443
444 %adjusted = sub i8 %base, %offset
445 call void @use8(i8 %adjusted)
446 %no_underflow = icmp ult i8 %adjusted, %base
447 call void @use1(i1 %no_underflow)
448 %not_null = icmp ne i8 %adjusted, 0
449 call void @use1(i1 %not_null)
450 %r = and i1 %not_null, %no_underflow
451 ret i1 %r
452 }
453
454 define i1 @t20(i8 %base, i8 %offset) {
455 ; CHECK-LABEL: @t20(
456 ; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[OFFSET:%.*]], 0
457 ; CHECK-NEXT: call void @llvm.assume(i1 [[CMP]])
458 ; CHECK-NEXT: [[ADJUSTED:%.*]] = sub i8 [[BASE:%.*]], [[OFFSET]]
459 ; CHECK-NEXT: call void @use8(i8 [[ADJUSTED]])
460 ; CHECK-NEXT: [[NO_UNDERFLOW:%.*]] = icmp uge i8 [[ADJUSTED]], [[BASE]]
461 ; CHECK-NEXT: call void @use1(i1 [[NO_UNDERFLOW]])
462 ; CHECK-NEXT: [[NOT_NULL:%.*]] = icmp eq i8 [[ADJUSTED]], 0
463 ; CHECK-NEXT: call void @use1(i1 [[NOT_NULL]])
464 ; CHECK-NEXT: [[R:%.*]] = or i1 [[NOT_NULL]], [[NO_UNDERFLOW]]
465 ; CHECK-NEXT: ret i1 [[R]]
466 ;
467 %cmp = icmp ne i8 %offset, 0
468 call void @llvm.assume(i1 %cmp)
469
470 %adjusted = sub i8 %base, %offset
471 call void @use8(i8 %adjusted)
472 %no_underflow = icmp uge i8 %adjusted, %base
473 call void @use1(i1 %no_underflow)
474 %not_null = icmp eq i8 %adjusted, 0
475 call void @use1(i1 %not_null)
476 %r = or i1 %not_null, %no_underflow
477 ret i1 %r
478 }
0 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
1 ; RUN: opt %s -instsimplify -S | FileCheck %s
2
3 ; Here we subtract two values, check that subtraction did not overflow AND
4 ; that the result is non-zero. This can be simplified just to a comparison
5 ; between the base and offset.
6
7 declare void @llvm.assume(i1)
8
9 define i1 @t0(i8 %base, i8 %offset) {
10 ; CHECK-LABEL: @t0(
11 ; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[OFFSET:%.*]], 0
12 ; CHECK-NEXT: call void @llvm.assume(i1 [[CMP]])
13 ; CHECK-NEXT: [[ADJUSTED:%.*]] = sub i8 [[BASE:%.*]], [[OFFSET]]
14 ; CHECK-NEXT: [[NO_UNDERFLOW:%.*]] = icmp uge i8 [[ADJUSTED]], [[BASE]]
15 ; CHECK-NEXT: [[NOT_NULL:%.*]] = icmp ne i8 [[ADJUSTED]], 0
16 ; CHECK-NEXT: [[R:%.*]] = and i1 [[NOT_NULL]], [[NO_UNDERFLOW]]
17 ; CHECK-NEXT: ret i1 [[R]]
18 ;
19 %cmp = icmp ne i8 %offset, 0
20 call void @llvm.assume(i1 %cmp)
21
22 %adjusted = sub i8 %base, %offset
23 %no_underflow = icmp uge i8 %adjusted, %base
24 %not_null = icmp ne i8 %adjusted, 0
25 %r = and i1 %not_null, %no_underflow
26 ret i1 %r
27 }
28
29 define i1 @t1(i8 %base, i8 %offset) {
30 ; CHECK-LABEL: @t1(
31 ; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[OFFSET:%.*]], 0
32 ; CHECK-NEXT: call void @llvm.assume(i1 [[CMP]])
33 ; CHECK-NEXT: [[ADJUSTED:%.*]] = sub i8 [[BASE:%.*]], [[OFFSET]]
34 ; CHECK-NEXT: [[NO_UNDERFLOW:%.*]] = icmp ult i8 [[ADJUSTED]], [[BASE]]
35 ; CHECK-NEXT: [[NOT_NULL:%.*]] = icmp eq i8 [[ADJUSTED]], 0
36 ; CHECK-NEXT: [[R:%.*]] = or i1 [[NOT_NULL]], [[NO_UNDERFLOW]]
37 ; CHECK-NEXT: ret i1 [[R]]
38 ;
39 %cmp = icmp ne i8 %offset, 0
40 call void @llvm.assume(i1 %cmp)
41
42 %adjusted = sub i8 %base, %offset
43 %no_underflow = icmp ult i8 %adjusted, %base
44 %not_null = icmp eq i8 %adjusted, 0
45 %r = or i1 %not_null, %no_underflow
46 ret i1 %r
47 }
48
49 define i1 @t2_commutative(i8 %base, i8 %offset) {
50 ; CHECK-LABEL: @t2_commutative(
51 ; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[OFFSET:%.*]], 0
52 ; CHECK-NEXT: call void @llvm.assume(i1 [[CMP]])
53 ; CHECK-NEXT: [[ADJUSTED:%.*]] = sub i8 [[BASE:%.*]], [[OFFSET]]
54 ; CHECK-NEXT: [[NO_UNDERFLOW:%.*]] = icmp ule i8 [[BASE]], [[ADJUSTED]]
55 ; CHECK-NEXT: [[NOT_NULL:%.*]] = icmp ne i8 [[ADJUSTED]], 0
56 ; CHECK-NEXT: [[R:%.*]] = and i1 [[NOT_NULL]], [[NO_UNDERFLOW]]
57 ; CHECK-NEXT: ret i1 [[R]]
58 ;
59 %cmp = icmp ne i8 %offset, 0
60 call void @llvm.assume(i1 %cmp)
61
62 %adjusted = sub i8 %base, %offset
63 %no_underflow = icmp ule i8 %base, %adjusted
64 %not_null = icmp ne i8 %adjusted, 0
65 %r = and i1 %not_null, %no_underflow
66 ret i1 %r
67 }
68
69 ; We don't know that offset is non-zero, so we can't fold.
70 define i1 @t3_bad(i8 %base, i8 %offset) {
71 ; CHECK-LABEL: @t3_bad(
72 ; CHECK-NEXT: [[ADJUSTED:%.*]] = sub i8 [[BASE:%.*]], [[OFFSET:%.*]]
73 ; CHECK-NEXT: [[NO_UNDERFLOW:%.*]] = icmp uge i8 [[ADJUSTED]], [[BASE]]
74 ; CHECK-NEXT: [[NOT_NULL:%.*]] = icmp ne i8 [[ADJUSTED]], 0
75 ; CHECK-NEXT: [[R:%.*]] = and i1 [[NOT_NULL]], [[NO_UNDERFLOW]]
76 ; CHECK-NEXT: ret i1 [[R]]
77 ;
78 %adjusted = sub i8 %base, %offset
79 %no_underflow = icmp uge i8 %adjusted, %base
80 %not_null = icmp ne i8 %adjusted, 0
81 %r = and i1 %not_null, %no_underflow
82 ret i1 %r
83 }