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