1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -passes=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:    [[RET:%.*]] = icmp ult i8 [[X:%.*]], 4
20; CHECK-NEXT:    ret i1 [[RET]]
21;
22  %tmp0 = and i8 %x, 3
23  %ret = icmp eq 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:    [[RET:%.*]] = icmp ule i8 [[X:%.*]], [[TMP0]]
31; CHECK-NEXT:    ret i1 [[RET]]
32;
33  %tmp0 = lshr i8 -1, %y
34  %tmp1 = and i8 %tmp0, %x
35  %ret = icmp eq 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:    [[RET:%.*]] = icmp ult <2 x i8> [[X:%.*]], splat (i8 4)
46; CHECK-NEXT:    ret <2 x i1> [[RET]]
47;
48  %tmp0 = and <2 x i8> %x, <i8 3, i8 3>
49  %ret = icmp eq <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:    [[RET:%.*]] = icmp ult <2 x i8> [[X:%.*]], <i8 4, i8 16>
56; CHECK-NEXT:    ret <2 x i1> [[RET]]
57;
58  %tmp0 = and <2 x i8> %x, <i8 3, i8 15> ; doesn't have to be splat.
59  %ret = icmp eq <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:    [[RET:%.*]] = icmp ult <2 x i8> [[X:%.*]], <i8 4, i8 1>
66; CHECK-NEXT:    ret <2 x i1> [[RET]]
67;
68  %tmp0 = and <2 x i8> %x, <i8 3, i8 0>
69  %ret = icmp eq <2 x i8> %tmp0, %x
70  ret <2 x i1> %ret
71}
72define <2 x i1> @p2_vec_nonsplat_edgecase1(<2 x i8> %x) {
73; CHECK-LABEL: @p2_vec_nonsplat_edgecase1(
74; CHECK-NEXT:    [[RET:%.*]] = icmp ule <2 x i8> [[X:%.*]], <i8 3, i8 -1>
75; CHECK-NEXT:    ret <2 x i1> [[RET]]
76;
77  %tmp0 = and <2 x i8> %x, <i8 3, i8 -1>
78  %ret = icmp eq <2 x i8> %tmp0, %x
79  ret <2 x i1> %ret
80}
81
82define <3 x i1> @p3_vec_splat_poison(<3 x i8> %x) {
83; CHECK-LABEL: @p3_vec_splat_poison(
84; CHECK-NEXT:    [[RET:%.*]] = icmp ult <3 x i8> [[X:%.*]], splat (i8 4)
85; CHECK-NEXT:    ret <3 x i1> [[RET]]
86;
87  %tmp0 = and <3 x i8> %x, <i8 3, i8 poison, i8 3>
88  %ret = icmp eq <3 x i8> %tmp0, %x
89  ret <3 x i1> %ret
90}
91
92; ============================================================================ ;
93; Commutativity tests.
94; ============================================================================ ;
95
96declare i8 @gen8()
97
98define i1 @c0() {
99; CHECK-LABEL: @c0(
100; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
101; CHECK-NEXT:    [[RET:%.*]] = icmp ult i8 [[X]], 4
102; CHECK-NEXT:    ret i1 [[RET]]
103;
104  %x = call i8 @gen8()
105  %tmp0 = and i8 %x, 3
106  %ret = icmp eq i8 %x, %tmp0 ; swapped order
107  ret i1 %ret
108}
109
110; ============================================================================ ;
111; Commutativity tests with variable
112; ============================================================================ ;
113
114define i1 @cv0(i8 %y) {
115; CHECK-LABEL: @cv0(
116; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
117; CHECK-NEXT:    [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
118; CHECK-NEXT:    [[RET:%.*]] = icmp ule i8 [[X]], [[TMP0]]
119; CHECK-NEXT:    ret i1 [[RET]]
120;
121  %x = call i8 @gen8()
122  %tmp0 = lshr i8 -1, %y
123  %tmp1 = and i8 %x, %tmp0 ; swapped order
124  %ret = icmp eq i8 %tmp1, %x
125  ret i1 %ret
126}
127
128define i1 @cv1(i8 %y) {
129; CHECK-LABEL: @cv1(
130; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
131; CHECK-NEXT:    [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
132; CHECK-NEXT:    [[RET:%.*]] = icmp ule i8 [[X]], [[TMP0]]
133; CHECK-NEXT:    ret i1 [[RET]]
134;
135  %x = call i8 @gen8()
136  %tmp0 = lshr i8 -1, %y
137  %tmp1 = and i8 %tmp0, %x
138  %ret = icmp eq i8 %x, %tmp1 ; swapped order
139  ret i1 %ret
140}
141
142define i1 @cv2(i8 %y) {
143; CHECK-LABEL: @cv2(
144; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
145; CHECK-NEXT:    [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
146; CHECK-NEXT:    [[RET:%.*]] = icmp ule i8 [[X]], [[TMP0]]
147; CHECK-NEXT:    ret i1 [[RET]]
148;
149  %x = call i8 @gen8()
150  %tmp0 = lshr i8 -1, %y
151  %tmp1 = and i8 %x, %tmp0 ; swapped order
152  %ret = icmp eq i8 %x, %tmp1 ; swapped order
153  ret i1 %ret
154}
155
156; ============================================================================ ;
157; One-use tests. We don't care about multi-uses here.
158; ============================================================================ ;
159
160declare void @use8(i8)
161
162define i1 @oneuse0(i8 %x) {
163; CHECK-LABEL: @oneuse0(
164; CHECK-NEXT:    [[TMP0:%.*]] = and i8 [[X:%.*]], 3
165; CHECK-NEXT:    call void @use8(i8 [[TMP0]])
166; CHECK-NEXT:    [[RET:%.*]] = icmp ult i8 [[X]], 4
167; CHECK-NEXT:    ret i1 [[RET]]
168;
169  %tmp0 = and i8 %x, 3
170  call void @use8(i8 %tmp0)
171  %ret = icmp eq i8 %tmp0, %x
172  ret i1 %ret
173}
174
175; ============================================================================ ;
176; Negative tests
177; ============================================================================ ;
178
179define i1 @n0(i8 %x) {
180; CHECK-LABEL: @n0(
181; CHECK-NEXT:    [[TMP1:%.*]] = and i8 [[X:%.*]], -5
182; CHECK-NEXT:    [[RET:%.*]] = icmp eq i8 [[TMP1]], 0
183; CHECK-NEXT:    ret i1 [[RET]]
184;
185  %tmp0 = and i8 %x, 4 ; power-of-two, but invalid.
186  %ret = icmp eq i8 %tmp0, %x
187  ret i1 %ret
188}
189
190define i1 @n1(i8 %x, i8 %y, i8 %notx) {
191; CHECK-LABEL: @n1(
192; CHECK-NEXT:    [[TMP0:%.*]] = and i8 [[X:%.*]], 3
193; CHECK-NEXT:    [[RET:%.*]] = icmp eq i8 [[TMP0]], [[NOTX:%.*]]
194; CHECK-NEXT:    ret i1 [[RET]]
195;
196  %tmp0 = and i8 %x, 3
197  %ret = icmp eq i8 %tmp0, %notx ; not %x
198  ret i1 %ret
199}
200
201define <2 x i1> @n2(<2 x i8> %x) {
202; CHECK-LABEL: @n2(
203; CHECK-NEXT:    [[TMP1:%.*]] = and <2 x i8> [[X:%.*]], <i8 -4, i8 -17>
204; CHECK-NEXT:    [[RET:%.*]] = icmp eq <2 x i8> [[TMP1]], zeroinitializer
205; CHECK-NEXT:    ret <2 x i1> [[RET]]
206;
207  %tmp0 = and <2 x i8> %x, <i8 3, i8 16> ; only the first one is valid.
208  %ret = icmp eq <2 x i8> %tmp0, %x
209  ret <2 x i1> %ret
210}
211