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 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:    [[RET:%.*]] = icmp ugt i8 [[X]], 3
25; CHECK-NEXT:    ret i1 [[RET]]
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:    [[RET:%.*]] = icmp ugt i8 [[X]], [[TMP0]]
38; CHECK-NEXT:    ret i1 [[RET]]
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:    [[RET:%.*]] = icmp ugt <2 x i8> [[X]], splat (i8 3)
55; CHECK-NEXT:    ret <2 x i1> [[RET]]
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:    [[RET:%.*]] = icmp ugt <2 x i8> [[X]], <i8 3, i8 15>
67; CHECK-NEXT:    ret <2 x i1> [[RET]]
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:    [[RET:%.*]] = icmp ugt <2 x i8> [[X]], <i8 3, i8 0>
79; CHECK-NEXT:    ret <2 x i1> [[RET]]
80;
81  %x = call <2 x i8> @gen2x8()
82  %tmp0 = and <2 x i8> %x, <i8 3, i8 0>
83  %ret = icmp ugt <2 x i8> %x, %tmp0
84  ret <2 x i1> %ret
85}
86define <2 x i1> @p2_vec_nonsplat_edgecase1() {
87; CHECK-LABEL: @p2_vec_nonsplat_edgecase1(
88; CHECK-NEXT:    [[X:%.*]] = call <2 x i8> @gen2x8()
89; CHECK-NEXT:    [[RET:%.*]] = icmp ugt <2 x i8> [[X]], <i8 3, i8 -1>
90; CHECK-NEXT:    ret <2 x i1> [[RET]]
91;
92  %x = call <2 x i8> @gen2x8()
93  %tmp0 = and <2 x i8> %x, <i8 3, i8 -1>
94  %ret = icmp ugt <2 x i8> %x, %tmp0
95  ret <2 x i1> %ret
96}
97
98define <3 x i1> @p3_vec_splat_poison() {
99; CHECK-LABEL: @p3_vec_splat_poison(
100; CHECK-NEXT:    [[X:%.*]] = call <3 x i8> @gen3x8()
101; CHECK-NEXT:    [[RET:%.*]] = icmp ugt <3 x i8> [[X]], <i8 3, i8 poison, i8 3>
102; CHECK-NEXT:    ret <3 x i1> [[RET]]
103;
104  %x = call <3 x i8> @gen3x8()
105  %tmp0 = and <3 x i8> %x, <i8 3, i8 poison, i8 3>
106  %ret = icmp ugt <3 x i8> %x, %tmp0
107  ret <3 x i1> %ret
108}
109
110define <3 x i1> @p3_vec_nonsplat_poison() {
111; CHECK-LABEL: @p3_vec_nonsplat_poison(
112; CHECK-NEXT:    [[X:%.*]] = call <3 x i8> @gen3x8()
113; CHECK-NEXT:    [[RET:%.*]] = icmp ugt <3 x i8> [[X]], <i8 3, i8 poison, i8 15>
114; CHECK-NEXT:    ret <3 x i1> [[RET]]
115;
116  %x = call <3 x i8> @gen3x8()
117  %tmp0 = and <3 x i8> %x, <i8 3, i8 poison, i8 15>
118  %ret = icmp ugt <3 x i8> %x, %tmp0
119  ret <3 x i1> %ret
120}
121
122; ============================================================================ ;
123; Commutativity tests.
124; ============================================================================ ;
125
126define i1 @c0(i8 %x) {
127; CHECK-LABEL: @c0(
128; CHECK-NEXT:    ret i1 false
129;
130  %tmp0 = and i8 %x, 3
131  %ret = icmp ugt i8 %tmp0, %x ; swapped order
132  ret i1 %ret
133}
134
135; ============================================================================ ;
136; Commutativity tests with variable
137; ============================================================================ ;
138
139define i1 @cv0(i8 %y) {
140; CHECK-LABEL: @cv0(
141; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
142; CHECK-NEXT:    [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
143; CHECK-NEXT:    [[RET:%.*]] = icmp ugt i8 [[X]], [[TMP0]]
144; CHECK-NEXT:    ret i1 [[RET]]
145;
146  %x = call i8 @gen8()
147  %tmp0 = lshr i8 -1, %y
148  %tmp1 = and i8 %tmp0, %x ; swapped order
149  %ret = icmp ugt i8 %x, %tmp1
150  ret i1 %ret
151}
152
153define i1 @cv1(i8 %y) {
154; CHECK-LABEL: @cv1(
155; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
156; CHECK-NEXT:    ret i1 false
157;
158  %x = call i8 @gen8()
159  %tmp0 = lshr i8 -1, %y
160  %tmp1 = and i8 %x, %tmp0
161  %ret = icmp ugt i8 %tmp1, %x ; swapped order
162  ret i1 %ret
163}
164
165define i1 @cv2(i8 %x, i8 %y) {
166; CHECK-LABEL: @cv2(
167; CHECK-NEXT:    ret i1 false
168;
169  %tmp0 = lshr i8 -1, %y
170  %tmp1 = and i8 %tmp0, %x ; swapped order
171  %ret = icmp ugt i8 %tmp1, %x ; swapped order
172  ret i1 %ret
173}
174
175; ============================================================================ ;
176; One-use tests. We don't care about multi-uses here.
177; ============================================================================ ;
178
179declare void @use8(i8)
180
181define i1 @oneuse0() {
182; CHECK-LABEL: @oneuse0(
183; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
184; CHECK-NEXT:    [[TMP0:%.*]] = and i8 [[X]], 3
185; CHECK-NEXT:    call void @use8(i8 [[TMP0]])
186; CHECK-NEXT:    [[RET:%.*]] = icmp ugt i8 [[X]], 3
187; CHECK-NEXT:    ret i1 [[RET]]
188;
189  %x = call i8 @gen8()
190  %tmp0 = and i8 %x, 3
191  call void @use8(i8 %tmp0)
192  %ret = icmp ugt i8 %x, %tmp0
193  ret i1 %ret
194}
195
196; ============================================================================ ;
197; Negative tests
198; ============================================================================ ;
199
200define i1 @n0() {
201; CHECK-LABEL: @n0(
202; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
203; CHECK-NEXT:    [[TMP1:%.*]] = and i8 [[X]], -5
204; CHECK-NEXT:    [[RET:%.*]] = icmp ne i8 [[TMP1]], 0
205; CHECK-NEXT:    ret i1 [[RET]]
206;
207  %x = call i8 @gen8()
208  %tmp0 = and i8 %x, 4 ; power-of-two, but invalid.
209  %ret = icmp ugt i8 %x, %tmp0
210  ret i1 %ret
211}
212
213define i1 @n1(i8 %y, i8 %notx) {
214; CHECK-LABEL: @n1(
215; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
216; CHECK-NEXT:    [[TMP0:%.*]] = and i8 [[X]], 3
217; CHECK-NEXT:    [[RET:%.*]] = icmp ugt i8 [[TMP0]], [[NOTX:%.*]]
218; CHECK-NEXT:    ret i1 [[RET]]
219;
220  %x = call i8 @gen8()
221  %tmp0 = and i8 %x, 3
222  %ret = icmp ugt i8 %tmp0, %notx ; not %x
223  ret i1 %ret
224}
225
226define <2 x i1> @n2() {
227; CHECK-LABEL: @n2(
228; CHECK-NEXT:    [[X:%.*]] = call <2 x i8> @gen2x8()
229; CHECK-NEXT:    [[TMP1:%.*]] = and <2 x i8> [[X]], <i8 -4, i8 -17>
230; CHECK-NEXT:    [[RET:%.*]] = icmp ne <2 x i8> [[TMP1]], zeroinitializer
231; CHECK-NEXT:    ret <2 x i1> [[RET]]
232;
233  %x = call <2 x i8> @gen2x8()
234  %tmp0 = and <2 x i8> %x, <i8 3, i8 16> ; only the first one is valid.
235  %ret = icmp ugt <2 x i8> %x, %tmp0
236  ret <2 x i1> %ret
237}
238