blob: e0c3a7c8d689e50b873046396e9ef2eda15c08c8 [file] [log] [blame]
Roman Lebedevac4dda82019-09-21 22:27:18 +00001; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt %s -instsimplify -S | FileCheck %s
3
4; Here we add unsigned two values, check that addition did not underflow AND
5; that the result is non-zero. This can be simplified just to a comparison
6; between the base and negated offset.
7
8; If we are checking that the result is not null or no underflow happened,
9; it is tautological (always-true).
10define i1 @t1(i8 %x, i8 %y) {
11; CHECK-LABEL: @t1(
12; CHECK-NEXT: ret i1 true
13;
14 %not_null = icmp ne i8 %y, 0
15 %no_underflow = icmp ule i8 %y, %x
16 %r = or i1 %not_null, %no_underflow
17 ret i1 %r
18}
19define i1 @t2_commutative(i8 %x, i8 %y) {
20; CHECK-LABEL: @t2_commutative(
21; CHECK-NEXT: ret i1 true
22;
23 %not_null = icmp ne i8 %y, 0
24 %no_underflow = icmp uge i8 %x, %y ; swapped
25 %r = or i1 %not_null, %no_underflow
26 ret i1 %r
27}
28
29define i1 @t3_commutative(i8 %x, i8 %y) {
30; CHECK-LABEL: @t3_commutative(
31; CHECK-NEXT: ret i1 true
32;
33 %not_null = icmp ne i8 %y, 0
34 %no_underflow = icmp ule i8 %y, %x
35 %r = or i1 %no_underflow, %not_null ; swapped
36 ret i1 %r
37}
38define i1 @t4_commutative(i8 %x, i8 %y) {
39; CHECK-LABEL: @t4_commutative(
40; CHECK-NEXT: ret i1 true
41;
42 %not_null = icmp ne i8 %y, 0
43 %no_underflow = icmp uge i8 %x, %y ; swapped
44 %r = or i1 %no_underflow, %not_null ; swapped
45 ret i1 %r
46}
47
48; If we are checking that the result is null and underflow happened,
49; it is tautological (always-false).
50define i1 @t5(i8 %x, i8 %y) {
51; CHECK-LABEL: @t5(
52; CHECK-NEXT: ret i1 false
53;
54 %not_null = icmp eq i8 %y, 0
55 %no_underflow = icmp ugt i8 %y, %x
56 %r = and i1 %not_null, %no_underflow
57 ret i1 %r
58}
59define i1 @t6_commutative(i8 %x, i8 %y) {
60; CHECK-LABEL: @t6_commutative(
61; CHECK-NEXT: ret i1 false
62;
63 %not_null = icmp eq i8 %y, 0
64 %no_underflow = icmp ult i8 %x, %y ; swapped
65 %r = and i1 %not_null, %no_underflow
66 ret i1 %r
67}
68
69; We only need to know that any of the 'add' operands is non-zero,
70; not necessarily the one used in the comparison.
71define i1 @t7(i8 %x, i8 %y) {
72; CHECK-LABEL: @t7(
73; CHECK-NEXT: ret i1 true
74;
75 %cmp = icmp slt i8 %y, 0
76 %not_null = icmp ne i8 %y, 0
77 %no_underflow = icmp ule i8 %y, %x
78 %r = or i1 %not_null, %no_underflow
79 ret i1 %r
80}
81
82; If we check that no underflow happened and that the result is null,
83; we can just check for null.
84define i1 @t8(i8 %x, i8 %y) {
85; CHECK-LABEL: @t8(
86; CHECK-NEXT: [[NOT_NULL:%.*]] = icmp eq i8 [[Y:%.*]], 0
Roman Lebedevbaf80982019-09-21 22:27:39 +000087; CHECK-NEXT: ret i1 [[NOT_NULL]]
Roman Lebedevac4dda82019-09-21 22:27:18 +000088;
89 %not_null = icmp eq i8 %y, 0
90 %no_underflow = icmp ule i8 %y, %x
91 %r = and i1 %not_null, %no_underflow
92 ret i1 %r
93}
94; Likewise, if we check that result is non-null or underflow happened,
95; we can just check for null.
96define i1 @t9(i8 %x, i8 %y) {
97; CHECK-LABEL: @t9(
98; CHECK-NEXT: [[NOT_NULL:%.*]] = icmp ne i8 [[Y:%.*]], 0
99; CHECK-NEXT: ret i1 [[NOT_NULL]]
100;
101 %not_null = icmp ne i8 %y, 0
102 %no_underflow = icmp ugt i8 %y, %x
103 %r = or i1 %not_null, %no_underflow
104 ret i1 %r
105}
106
107; If we check that no underflow happened or that the result is not null,
108; we can just check for lack of underflow.
109define i1 @t10(i8 %x, i8 %y) {
110; CHECK-LABEL: @t10(
111; CHECK-NEXT: [[NO_UNDERFLOW:%.*]] = icmp ule i8 [[Y:%.*]], [[X:%.*]]
112; CHECK-NEXT: ret i1 [[NO_UNDERFLOW]]
113;
114 %not_null = icmp eq i8 %y, 0
115 %no_underflow = icmp ule i8 %y, %x
116 %r = or i1 %not_null, %no_underflow
117 ret i1 %r
118}
119; Likewise, if we check that underflow happened and that the result is not null,
120; we can just check for lack of underflow.
121define i1 @t11(i8 %x, i8 %y) {
122; CHECK-LABEL: @t11(
123; CHECK-NEXT: [[NO_UNDERFLOW:%.*]] = icmp ugt i8 [[Y:%.*]], [[X:%.*]]
124; CHECK-NEXT: ret i1 [[NO_UNDERFLOW]]
125;
126 %not_null = icmp ne i8 %y, 0
127 %no_underflow = icmp ugt i8 %y, %x
128 %r = and i1 %not_null, %no_underflow
129 ret i1 %r
130}