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