1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -instcombine -S | FileCheck %s
3
4; https://bugs.llvm.org/show_bug.cgi?id=38123
5
6; Pattern:
7;   x & C != x
8; Should be transformed into:
9;   x u> C
10; Iff: isPowerOf2(C + 1)
11; C can be 0 and -1.
12
13; ============================================================================ ;
14; Basic positive tests
15; ============================================================================ ;
16
17define i1 @p0(i8 %x) {
18; CHECK-LABEL: @p0(
19; CHECK-NEXT:    [[TMP1:%.*]] = icmp ugt i8 [[X:%.*]], 3
20; CHECK-NEXT:    ret i1 [[TMP1]]
21;
22  %tmp0 = and i8 %x, 3
23  %ret = icmp ne i8 %tmp0, %x
24  ret i1 %ret
25}
26
27define i1 @pv(i8 %x, i8 %y) {
28; CHECK-LABEL: @pv(
29; CHECK-NEXT:    [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
30; CHECK-NEXT:    [[TMP1:%.*]] = icmp ult i8 [[TMP0]], [[X:%.*]]
31; CHECK-NEXT:    ret i1 [[TMP1]]
32;
33  %tmp0 = lshr i8 -1, %y
34  %tmp1 = and i8 %tmp0, %x
35  %ret = icmp ne i8 %tmp1, %x
36  ret i1 %ret
37}
38
39; ============================================================================ ;
40; Vector tests
41; ============================================================================ ;
42
43define <2 x i1> @p1_vec_splat(<2 x i8> %x) {
44; CHECK-LABEL: @p1_vec_splat(
45; CHECK-NEXT:    [[TMP1:%.*]] = icmp ugt <2 x i8> [[X:%.*]], <i8 3, i8 3>
46; CHECK-NEXT:    ret <2 x i1> [[TMP1]]
47;
48  %tmp0 = and <2 x i8> %x, <i8 3, i8 3>
49  %ret = icmp ne <2 x i8> %tmp0, %x
50  ret <2 x i1> %ret
51}
52
53define <2 x i1> @p2_vec_nonsplat(<2 x i8> %x) {
54; CHECK-LABEL: @p2_vec_nonsplat(
55; CHECK-NEXT:    [[TMP1:%.*]] = icmp ugt <2 x i8> [[X:%.*]], <i8 3, i8 15>
56; CHECK-NEXT:    ret <2 x i1> [[TMP1]]
57;
58  %tmp0 = and <2 x i8> %x, <i8 3, i8 15> ; doesn't have to be splat.
59  %ret = icmp ne <2 x i8> %tmp0, %x
60  ret <2 x i1> %ret
61}
62
63define <2 x i1> @p2_vec_nonsplat_edgecase0(<2 x i8> %x) {
64; CHECK-LABEL: @p2_vec_nonsplat_edgecase0(
65; CHECK-NEXT:    [[TMP0:%.*]] = and <2 x i8> [[X:%.*]], <i8 3, i8 0>
66; CHECK-NEXT:    [[RET:%.*]] = icmp ne <2 x i8> [[TMP0]], [[X]]
67; CHECK-NEXT:    ret <2 x i1> [[RET]]
68;
69  %tmp0 = and <2 x i8> %x, <i8 3, i8 0>
70  %ret = icmp ne <2 x i8> %tmp0, %x
71  ret <2 x i1> %ret
72}
73define <2 x i1> @p2_vec_nonsplat_edgecase1(<2 x i8> %x) {
74; CHECK-LABEL: @p2_vec_nonsplat_edgecase1(
75; CHECK-NEXT:    [[TMP1:%.*]] = icmp ugt <2 x i8> [[X:%.*]], <i8 3, i8 -1>
76; CHECK-NEXT:    ret <2 x i1> [[TMP1]]
77;
78  %tmp0 = and <2 x i8> %x, <i8 3, i8 -1>
79  %ret = icmp ne <2 x i8> %tmp0, %x
80  ret <2 x i1> %ret
81}
82
83define <3 x i1> @p3_vec_splat_undef(<3 x i8> %x) {
84; CHECK-LABEL: @p3_vec_splat_undef(
85; CHECK-NEXT:    [[TMP1:%.*]] = icmp ugt <3 x i8> [[X:%.*]], <i8 3, i8 3, i8 3>
86; CHECK-NEXT:    ret <3 x i1> [[TMP1]]
87;
88  %tmp0 = and <3 x i8> %x, <i8 3, i8 undef, i8 3>
89  %ret = icmp ne <3 x i8> %tmp0, %x
90  ret <3 x i1> %ret
91}
92
93define <3 x i1> @p3_vec_nonsplat_undef(<3 x i8> %x) {
94; CHECK-LABEL: @p3_vec_nonsplat_undef(
95; CHECK-NEXT:    [[TMP1:%.*]] = icmp ugt <3 x i8> [[X:%.*]], <i8 -1, i8 -1, i8 3>
96; CHECK-NEXT:    ret <3 x i1> [[TMP1]]
97;
98  %tmp0 = and <3 x i8> %x, <i8 -1, i8 undef, i8 3>
99  %ret = icmp ne <3 x i8> %tmp0, %x
100  ret <3 x i1> %ret
101}
102
103; ============================================================================ ;
104; Commutativity tests.
105; ============================================================================ ;
106
107declare i8 @gen8()
108
109define i1 @c0() {
110; CHECK-LABEL: @c0(
111; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
112; CHECK-NEXT:    [[TMP1:%.*]] = icmp ugt i8 [[X]], 3
113; CHECK-NEXT:    ret i1 [[TMP1]]
114;
115  %x = call i8 @gen8()
116  %tmp0 = and i8 %x, 3
117  %ret = icmp ne i8 %x, %tmp0 ; swapped order
118  ret i1 %ret
119}
120
121; ============================================================================ ;
122; Commutativity tests with variable
123; ============================================================================ ;
124
125define i1 @cv0(i8 %y) {
126; CHECK-LABEL: @cv0(
127; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
128; CHECK-NEXT:    [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
129; CHECK-NEXT:    [[TMP1:%.*]] = icmp ugt i8 [[X]], [[TMP0]]
130; CHECK-NEXT:    ret i1 [[TMP1]]
131;
132  %x = call i8 @gen8()
133  %tmp0 = lshr i8 -1, %y
134  %tmp1 = and i8 %x, %tmp0 ; swapped order
135  %ret = icmp ne i8 %tmp1, %x
136  ret i1 %ret
137}
138
139define i1 @cv1(i8 %y) {
140; CHECK-LABEL: @cv1(
141; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
142; CHECK-NEXT:    [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
143; CHECK-NEXT:    [[TMP1:%.*]] = icmp ugt i8 [[X]], [[TMP0]]
144; CHECK-NEXT:    ret i1 [[TMP1]]
145;
146  %x = call i8 @gen8()
147  %tmp0 = lshr i8 -1, %y
148  %tmp1 = and i8 %tmp0, %x
149  %ret = icmp ne i8 %x, %tmp1 ; swapped order
150  ret i1 %ret
151}
152
153define i1 @cv2(i8 %y) {
154; CHECK-LABEL: @cv2(
155; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
156; CHECK-NEXT:    [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
157; CHECK-NEXT:    [[TMP1:%.*]] = icmp ugt i8 [[X]], [[TMP0]]
158; CHECK-NEXT:    ret i1 [[TMP1]]
159;
160  %x = call i8 @gen8()
161  %tmp0 = lshr i8 -1, %y
162  %tmp1 = and i8 %x, %tmp0 ; swapped order
163  %ret = icmp ne i8 %x, %tmp1 ; swapped order
164  ret i1 %ret
165}
166
167; ============================================================================ ;
168; One-use tests. We don't care about multi-uses here.
169; ============================================================================ ;
170
171declare void @use8(i8)
172
173define i1 @oneuse0(i8 %x) {
174; CHECK-LABEL: @oneuse0(
175; CHECK-NEXT:    [[TMP0:%.*]] = and i8 [[X:%.*]], 3
176; CHECK-NEXT:    call void @use8(i8 [[TMP0]])
177; CHECK-NEXT:    [[TMP1:%.*]] = icmp ugt i8 [[X]], 3
178; CHECK-NEXT:    ret i1 [[TMP1]]
179;
180  %tmp0 = and i8 %x, 3
181  call void @use8(i8 %tmp0)
182  %ret = icmp ne i8 %tmp0, %x
183  ret i1 %ret
184}
185
186; ============================================================================ ;
187; Negative tests
188; ============================================================================ ;
189
190define i1 @n0(i8 %x) {
191; CHECK-LABEL: @n0(
192; CHECK-NEXT:    [[TMP0:%.*]] = and i8 [[X:%.*]], 4
193; CHECK-NEXT:    [[RET:%.*]] = icmp ne i8 [[TMP0]], [[X]]
194; CHECK-NEXT:    ret i1 [[RET]]
195;
196  %tmp0 = and i8 %x, 4 ; power-of-two, but invalid.
197  %ret = icmp ne i8 %tmp0, %x
198  ret i1 %ret
199}
200
201define i1 @n1(i8 %x, i8 %y, i8 %notx) {
202; CHECK-LABEL: @n1(
203; CHECK-NEXT:    [[TMP0:%.*]] = and i8 [[X:%.*]], 3
204; CHECK-NEXT:    [[RET:%.*]] = icmp ne i8 [[TMP0]], [[NOTX:%.*]]
205; CHECK-NEXT:    ret i1 [[RET]]
206;
207  %tmp0 = and i8 %x, 3
208  %ret = icmp ne i8 %tmp0, %notx ; not %x
209  ret i1 %ret
210}
211
212define <2 x i1> @n2(<2 x i8> %x) {
213; CHECK-LABEL: @n2(
214; CHECK-NEXT:    [[TMP0:%.*]] = and <2 x i8> [[X:%.*]], <i8 3, i8 16>
215; CHECK-NEXT:    [[RET:%.*]] = icmp ne <2 x i8> [[TMP0]], [[X]]
216; CHECK-NEXT:    ret <2 x i1> [[RET]]
217;
218  %tmp0 = and <2 x i8> %x, <i8 3, i8 16> ; only the first one is valid.
219  %ret = icmp ne <2 x i8> %tmp0, %x
220  ret <2 x i1> %ret
221}
222