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
12; ============================================================================ ;
13; Basic positive tests
14; ============================================================================ ;
15
16define i1 @p0(i8 %x) {
17; CHECK-LABEL: @p0(
18; CHECK-NEXT:    [[TMP1:%.*]] = icmp ugt i8 [[X:%.*]], 3
19; CHECK-NEXT:    ret i1 [[TMP1]]
20;
21  %tmp0 = and i8 %x, 3
22  %ret = icmp ne i8 %tmp0, %x
23  ret i1 %ret
24}
25
26define i1 @pv(i8 %x, i8 %y) {
27; CHECK-LABEL: @pv(
28; CHECK-NEXT:    [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
29; CHECK-NEXT:    [[TMP1:%.*]] = icmp ult i8 [[TMP0]], [[X:%.*]]
30; CHECK-NEXT:    ret i1 [[TMP1]]
31;
32  %tmp0 = lshr i8 -1, %y
33  %tmp1 = and i8 %tmp0, %x
34  %ret = icmp ne i8 %tmp1, %x
35  ret i1 %ret
36}
37
38; ============================================================================ ;
39; Vector tests
40; ============================================================================ ;
41
42define <2 x i1> @p1_vec_splat(<2 x i8> %x) {
43; CHECK-LABEL: @p1_vec_splat(
44; CHECK-NEXT:    [[TMP1:%.*]] = icmp ugt <2 x i8> [[X:%.*]], <i8 3, i8 3>
45; CHECK-NEXT:    ret <2 x i1> [[TMP1]]
46;
47  %tmp0 = and <2 x i8> %x, <i8 3, i8 3>
48  %ret = icmp ne <2 x i8> %tmp0, %x
49  ret <2 x i1> %ret
50}
51
52define <2 x i1> @p2_vec_nonsplat(<2 x i8> %x) {
53; CHECK-LABEL: @p2_vec_nonsplat(
54; CHECK-NEXT:    [[TMP1:%.*]] = icmp ugt <2 x i8> [[X:%.*]], <i8 3, i8 15>
55; CHECK-NEXT:    ret <2 x i1> [[TMP1]]
56;
57  %tmp0 = and <2 x i8> %x, <i8 3, i8 15> ; doesn't have to be splat.
58  %ret = icmp ne <2 x i8> %tmp0, %x
59  ret <2 x i1> %ret
60}
61
62define <3 x i1> @p3_vec_splat_undef(<3 x i8> %x) {
63; CHECK-LABEL: @p3_vec_splat_undef(
64; CHECK-NEXT:    [[TMP1:%.*]] = icmp ugt <3 x i8> [[X:%.*]], <i8 3, i8 undef, i8 3>
65; CHECK-NEXT:    ret <3 x i1> [[TMP1]]
66;
67  %tmp0 = and <3 x i8> %x, <i8 3, i8 undef, i8 3>
68  %ret = icmp ne <3 x i8> %tmp0, %x
69  ret <3 x i1> %ret
70}
71
72; ============================================================================ ;
73; Commutativity tests.
74; ============================================================================ ;
75
76declare i8 @gen8()
77
78define i1 @c0() {
79; CHECK-LABEL: @c0(
80; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
81; CHECK-NEXT:    [[TMP1:%.*]] = icmp ugt i8 [[X]], 3
82; CHECK-NEXT:    ret i1 [[TMP1]]
83;
84  %x = call i8 @gen8()
85  %tmp0 = and i8 %x, 3
86  %ret = icmp ne i8 %x, %tmp0 ; swapped order
87  ret i1 %ret
88}
89
90; ============================================================================ ;
91; Commutativity tests with variable
92; ============================================================================ ;
93
94define i1 @cv0(i8 %y) {
95; CHECK-LABEL: @cv0(
96; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
97; CHECK-NEXT:    [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
98; CHECK-NEXT:    [[TMP1:%.*]] = icmp ugt i8 [[X]], [[TMP0]]
99; CHECK-NEXT:    ret i1 [[TMP1]]
100;
101  %x = call i8 @gen8()
102  %tmp0 = lshr i8 -1, %y
103  %tmp1 = and i8 %x, %tmp0 ; swapped order
104  %ret = icmp ne i8 %tmp1, %x
105  ret i1 %ret
106}
107
108define i1 @cv1(i8 %y) {
109; CHECK-LABEL: @cv1(
110; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
111; CHECK-NEXT:    [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
112; CHECK-NEXT:    [[TMP1:%.*]] = icmp ugt i8 [[X]], [[TMP0]]
113; CHECK-NEXT:    ret i1 [[TMP1]]
114;
115  %x = call i8 @gen8()
116  %tmp0 = lshr i8 -1, %y
117  %tmp1 = and i8 %tmp0, %x
118  %ret = icmp ne i8 %x, %tmp1 ; swapped order
119  ret i1 %ret
120}
121
122define i1 @cv2(i8 %y) {
123; CHECK-LABEL: @cv2(
124; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
125; CHECK-NEXT:    [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
126; CHECK-NEXT:    [[TMP1:%.*]] = icmp ugt i8 [[X]], [[TMP0]]
127; CHECK-NEXT:    ret i1 [[TMP1]]
128;
129  %x = call i8 @gen8()
130  %tmp0 = lshr i8 -1, %y
131  %tmp1 = and i8 %x, %tmp0 ; swapped order
132  %ret = icmp ne i8 %x, %tmp1 ; swapped order
133  ret i1 %ret
134}
135
136; ============================================================================ ;
137; One-use tests. We don't care about multi-uses here.
138; ============================================================================ ;
139
140declare void @use8(i8)
141
142define i1 @oneuse0(i8 %x) {
143; CHECK-LABEL: @oneuse0(
144; CHECK-NEXT:    [[TMP0:%.*]] = and i8 [[X:%.*]], 3
145; CHECK-NEXT:    call void @use8(i8 [[TMP0]])
146; CHECK-NEXT:    [[TMP1:%.*]] = icmp ugt i8 [[X]], 3
147; CHECK-NEXT:    ret i1 [[TMP1]]
148;
149  %tmp0 = and i8 %x, 3
150  call void @use8(i8 %tmp0)
151  %ret = icmp ne i8 %tmp0, %x
152  ret i1 %ret
153}
154
155; ============================================================================ ;
156; Negative tests
157; ============================================================================ ;
158
159define i1 @n0(i8 %x) {
160; CHECK-LABEL: @n0(
161; CHECK-NEXT:    [[TMP0:%.*]] = and i8 [[X:%.*]], 4
162; CHECK-NEXT:    [[RET:%.*]] = icmp ne i8 [[TMP0]], [[X]]
163; CHECK-NEXT:    ret i1 [[RET]]
164;
165  %tmp0 = and i8 %x, 4 ; power-of-two, but invalid.
166  %ret = icmp ne i8 %tmp0, %x
167  ret i1 %ret
168}
169
170define i1 @n1(i8 %x, i8 %y, i8 %notx) {
171; CHECK-LABEL: @n1(
172; CHECK-NEXT:    [[TMP0:%.*]] = and i8 [[X:%.*]], 3
173; CHECK-NEXT:    [[RET:%.*]] = icmp ne i8 [[TMP0]], [[NOTX:%.*]]
174; CHECK-NEXT:    ret i1 [[RET]]
175;
176  %tmp0 = and i8 %x, 3
177  %ret = icmp ne i8 %tmp0, %notx ; not %x
178  ret i1 %ret
179}
180
181define <2 x i1> @n2(<2 x i8> %x) {
182; CHECK-LABEL: @n2(
183; CHECK-NEXT:    [[TMP0:%.*]] = and <2 x i8> [[X:%.*]], <i8 3, i8 16>
184; CHECK-NEXT:    [[RET:%.*]] = icmp ne <2 x i8> [[TMP0]], [[X]]
185; CHECK-NEXT:    ret <2 x i1> [[RET]]
186;
187  %tmp0 = and <2 x i8> %x, <i8 3, i8 16> ; only the first one is valid.
188  %ret = icmp ne <2 x i8> %tmp0, %x
189  ret <2 x i1> %ret
190}
191