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 s> x & C
8; Should be transformed into:
9;   x s> C
10; Iff: isPowerOf2(C + 1)
11; C must not be -1, but may be 0.
12
13; NOTE: this pattern is not commutative!
14
15declare i8 @gen8()
16declare <2 x i8> @gen2x8()
17declare <3 x i8> @gen3x8()
18
19; ============================================================================ ;
20; Basic positive tests
21; ============================================================================ ;
22
23define i1 @p0() {
24; CHECK-LABEL: @p0(
25; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
26; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt i8 [[X]], 3
27; CHECK-NEXT:    ret i1 [[TMP1]]
28;
29  %x = call i8 @gen8()
30  %tmp0 = and i8 %x, 3
31  %ret = icmp sgt i8 %x, %tmp0
32  ret i1 %ret
33}
34
35; ============================================================================ ;
36; Vector tests
37; ============================================================================ ;
38
39define <2 x i1> @p1_vec_splat() {
40; CHECK-LABEL: @p1_vec_splat(
41; CHECK-NEXT:    [[X:%.*]] = call <2 x i8> @gen2x8()
42; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt <2 x i8> [[X]], <i8 3, i8 3>
43; CHECK-NEXT:    ret <2 x i1> [[TMP1]]
44;
45  %x = call <2 x i8> @gen2x8()
46  %tmp0 = and <2 x i8> %x, <i8 3, i8 3>
47  %ret = icmp sgt <2 x i8> %x, %tmp0
48  ret <2 x i1> %ret
49}
50
51define <2 x i1> @p2_vec_nonsplat() {
52; CHECK-LABEL: @p2_vec_nonsplat(
53; CHECK-NEXT:    [[X:%.*]] = call <2 x i8> @gen2x8()
54; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt <2 x i8> [[X]], <i8 3, i8 15>
55; CHECK-NEXT:    ret <2 x i1> [[TMP1]]
56;
57  %x = call <2 x i8> @gen2x8()
58  %tmp0 = and <2 x i8> %x, <i8 3, i8 15> ; doesn't have to be splat.
59  %ret = icmp sgt <2 x i8> %x, %tmp0
60  ret <2 x i1> %ret
61}
62
63define <2 x i1> @p2_vec_nonsplat_edgecase() {
64; CHECK-LABEL: @p2_vec_nonsplat_edgecase(
65; CHECK-NEXT:    [[X:%.*]] = call <2 x i8> @gen2x8()
66; CHECK-NEXT:    [[TMP0:%.*]] = and <2 x i8> [[X]], <i8 3, i8 0>
67; CHECK-NEXT:    [[RET:%.*]] = icmp sgt <2 x i8> [[X]], [[TMP0]]
68; CHECK-NEXT:    ret <2 x i1> [[RET]]
69;
70  %x = call <2 x i8> @gen2x8()
71  %tmp0 = and <2 x i8> %x, <i8 3, i8 0>
72  %ret = icmp sgt <2 x i8> %x, %tmp0
73  ret <2 x i1> %ret
74}
75
76define <3 x i1> @p3_vec_splat_undef() {
77; CHECK-LABEL: @p3_vec_splat_undef(
78; CHECK-NEXT:    [[X:%.*]] = call <3 x i8> @gen3x8()
79; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt <3 x i8> [[X]], <i8 3, i8 3, i8 3>
80; CHECK-NEXT:    ret <3 x i1> [[TMP1]]
81;
82  %x = call <3 x i8> @gen3x8()
83  %tmp0 = and <3 x i8> %x, <i8 3, i8 undef, i8 3>
84  %ret = icmp sgt <3 x i8> %x, %tmp0
85  ret <3 x i1> %ret
86}
87
88define <3 x i1> @p3_vec_nonsplat_undef() {
89; CHECK-LABEL: @p3_vec_nonsplat_undef(
90; CHECK-NEXT:    [[X:%.*]] = call <3 x i8> @gen3x8()
91; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt <3 x i8> [[X]], <i8 15, i8 3, i8 15>
92; CHECK-NEXT:    ret <3 x i1> [[TMP1]]
93;
94  %x = call <3 x i8> @gen3x8()
95  %tmp0 = and <3 x i8> %x, <i8 15, i8 3, i8 undef>
96  %ret = icmp sgt <3 x i8> %x, %tmp0
97  ret <3 x i1> %ret
98}
99
100; ============================================================================ ;
101; One-use tests. We don't care about multi-uses here.
102; ============================================================================ ;
103
104declare void @use8(i8)
105
106define i1 @oneuse0() {
107; CHECK-LABEL: @oneuse0(
108; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
109; CHECK-NEXT:    [[TMP0:%.*]] = and i8 [[X]], 3
110; CHECK-NEXT:    call void @use8(i8 [[TMP0]])
111; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt i8 [[X]], 3
112; CHECK-NEXT:    ret i1 [[TMP1]]
113;
114  %x = call i8 @gen8()
115  %tmp0 = and i8 %x, 3
116  call void @use8(i8 %tmp0)
117  %ret = icmp sgt i8 %x, %tmp0
118  ret i1 %ret
119}
120
121; ============================================================================ ;
122; Negative tests
123; ============================================================================ ;
124
125; Commutativity tests.
126
127define i1 @c0(i8 %x) {
128; CHECK-LABEL: @c0(
129; CHECK-NEXT:    [[TMP0:%.*]] = and i8 [[X:%.*]], 3
130; CHECK-NEXT:    [[RET:%.*]] = icmp sgt i8 [[TMP0]], [[X]]
131; CHECK-NEXT:    ret i1 [[RET]]
132;
133  %tmp0 = and i8 %x, 3
134  %ret = icmp sgt i8 %tmp0, %x ; swapped order
135  ret i1 %ret
136}
137
138; ============================================================================ ;
139; Rest of negative tests
140; ============================================================================ ;
141
142define i1 @n0() {
143; CHECK-LABEL: @n0(
144; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
145; CHECK-NEXT:    [[TMP0:%.*]] = and i8 [[X]], 4
146; CHECK-NEXT:    [[RET:%.*]] = icmp sgt i8 [[X]], [[TMP0]]
147; CHECK-NEXT:    ret i1 [[RET]]
148;
149  %x = call i8 @gen8()
150  %tmp0 = and i8 %x, 4 ; power-of-two, but invalid.
151  %ret = icmp sgt i8 %x, %tmp0
152  ret i1 %ret
153}
154
155define i1 @n1(i8 %y, i8 %notx) {
156; CHECK-LABEL: @n1(
157; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
158; CHECK-NEXT:    [[TMP0:%.*]] = and i8 [[X]], 3
159; CHECK-NEXT:    [[RET:%.*]] = icmp sgt i8 [[TMP0]], [[NOTX:%.*]]
160; CHECK-NEXT:    ret i1 [[RET]]
161;
162  %x = call i8 @gen8()
163  %tmp0 = and i8 %x, 3
164  %ret = icmp sgt i8 %tmp0, %notx ; not %x
165  ret i1 %ret
166}
167
168define <2 x i1> @n2() {
169; CHECK-LABEL: @n2(
170; CHECK-NEXT:    [[X:%.*]] = call <2 x i8> @gen2x8()
171; CHECK-NEXT:    [[TMP0:%.*]] = and <2 x i8> [[X]], <i8 3, i8 16>
172; CHECK-NEXT:    [[RET:%.*]] = icmp sgt <2 x i8> [[X]], [[TMP0]]
173; CHECK-NEXT:    ret <2 x i1> [[RET]]
174;
175  %x = call <2 x i8> @gen2x8()
176  %tmp0 = and <2 x i8> %x, <i8 3, i8 16> ; only the first one is valid.
177  %ret = icmp sgt <2 x i8> %x, %tmp0
178  ret <2 x i1> %ret
179}
180
181; ============================================================================ ;
182; Potential miscompiles.
183; ============================================================================ ;
184
185define i1 @pv(i8 %y) {
186; CHECK-LABEL: @pv(
187; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
188; CHECK-NEXT:    [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
189; CHECK-NEXT:    [[TMP1:%.*]] = and i8 [[TMP0]], [[X]]
190; CHECK-NEXT:    [[RET:%.*]] = icmp sgt i8 [[X]], [[TMP1]]
191; CHECK-NEXT:    ret i1 [[RET]]
192;
193  %x = call i8 @gen8()
194  %tmp0 = lshr i8 -1, %y
195  %tmp1 = and i8 %tmp0, %x
196  %ret = icmp sgt i8 %x, %tmp1
197  ret i1 %ret
198}
199
200define <2 x i1> @n3_vec() {
201; CHECK-LABEL: @n3_vec(
202; CHECK-NEXT:    [[X:%.*]] = call <2 x i8> @gen2x8()
203; CHECK-NEXT:    [[TMP0:%.*]] = and <2 x i8> [[X]], <i8 3, i8 -1>
204; CHECK-NEXT:    [[RET:%.*]] = icmp sgt <2 x i8> [[X]], [[TMP0]]
205; CHECK-NEXT:    ret <2 x i1> [[RET]]
206;
207  %x = call <2 x i8> @gen2x8()
208  %tmp0 = and <2 x i8> %x, <i8 3, i8 -1>
209  %ret = icmp sgt <2 x i8> %x, %tmp0
210  ret <2 x i1> %ret
211}
212
213define <3 x i1> @n4_vec() {
214; CHECK-LABEL: @n4_vec(
215; CHECK-NEXT:    [[X:%.*]] = call <3 x i8> @gen3x8()
216; CHECK-NEXT:    [[TMP0:%.*]] = and <3 x i8> [[X]], <i8 3, i8 undef, i8 -1>
217; CHECK-NEXT:    [[RET:%.*]] = icmp sgt <3 x i8> [[X]], [[TMP0]]
218; CHECK-NEXT:    ret <3 x i1> [[RET]]
219;
220  %x = call <3 x i8> @gen3x8()
221  %tmp0 = and <3 x i8> %x, <i8 3, i8 undef, i8 -1>
222  %ret = icmp sgt <3 x i8> %x, %tmp0
223  ret <3 x i1> %ret
224}
225
226; Commutativity tests with variable
227
228; Ok, this one should fold. We only testing commutativity of 'and'.
229define i1 @cv0_GOOD(i8 %y) {
230; CHECK-LABEL: @cv0_GOOD(
231; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
232; CHECK-NEXT:    [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
233; CHECK-NEXT:    [[TMP1:%.*]] = and i8 [[TMP0]], [[X]]
234; CHECK-NEXT:    [[RET:%.*]] = icmp sgt i8 [[X]], [[TMP1]]
235; CHECK-NEXT:    ret i1 [[RET]]
236;
237  %x = call i8 @gen8()
238  %tmp0 = lshr i8 -1, %y
239  %tmp1 = and i8 %tmp0, %x ; swapped order
240  %ret = icmp sgt i8 %x, %tmp1
241  ret i1 %ret
242}
243
244define i1 @cv1(i8 %y) {
245; CHECK-LABEL: @cv1(
246; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
247; CHECK-NEXT:    [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
248; CHECK-NEXT:    [[TMP1:%.*]] = and i8 [[X]], [[TMP0]]
249; CHECK-NEXT:    [[RET:%.*]] = icmp sgt i8 [[TMP1]], [[X]]
250; CHECK-NEXT:    ret i1 [[RET]]
251;
252  %x = call i8 @gen8()
253  %tmp0 = lshr i8 -1, %y
254  %tmp1 = and i8 %x, %tmp0
255  %ret = icmp sgt i8 %tmp1, %x ; swapped order
256  ret i1 %ret
257}
258
259define i1 @cv2(i8 %x, i8 %y) {
260; CHECK-LABEL: @cv2(
261; CHECK-NEXT:    [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
262; CHECK-NEXT:    [[TMP1:%.*]] = and i8 [[TMP0]], [[X:%.*]]
263; CHECK-NEXT:    [[RET:%.*]] = icmp sgt i8 [[TMP1]], [[X]]
264; CHECK-NEXT:    ret i1 [[RET]]
265;
266  %tmp0 = lshr i8 -1, %y
267  %tmp1 = and i8 %tmp0, %x ; swapped order
268  %ret = icmp sgt i8 %tmp1, %x ; swapped order
269  ret i1 %ret
270}
271