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
12; NOTE: this pattern is not commutative!
13
14declare i8 @gen8()
15declare <2 x i8> @gen2x8()
16declare <3 x i8> @gen3x8()
17
18; ============================================================================ ;
19; Basic positive tests
20; ============================================================================ ;
21
22define i1 @p0() {
23; CHECK-LABEL: @p0(
24; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
25; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt i8 [[X]], 3
26; CHECK-NEXT:    ret i1 [[TMP1]]
27;
28  %x = call i8 @gen8()
29  %tmp0 = and i8 %x, 3
30  %ret = icmp sgt i8 %x, %tmp0
31  ret i1 %ret
32}
33
34define i1 @pv(i8 %y) {
35; CHECK-LABEL: @pv(
36; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
37; CHECK-NEXT:    [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
38; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt i8 [[X]], [[TMP0]]
39; CHECK-NEXT:    ret i1 [[TMP1]]
40;
41  %x = call i8 @gen8()
42  %tmp0 = lshr i8 -1, %y
43  %tmp1 = and i8 %tmp0, %x
44  %ret = icmp sgt i8 %x, %tmp1
45  ret i1 %ret
46}
47
48; ============================================================================ ;
49; Vector tests
50; ============================================================================ ;
51
52define <2 x i1> @p1_vec_splat() {
53; CHECK-LABEL: @p1_vec_splat(
54; CHECK-NEXT:    [[X:%.*]] = call <2 x i8> @gen2x8()
55; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt <2 x i8> [[X]], <i8 3, i8 3>
56; CHECK-NEXT:    ret <2 x i1> [[TMP1]]
57;
58  %x = call <2 x i8> @gen2x8()
59  %tmp0 = and <2 x i8> %x, <i8 3, i8 3>
60  %ret = icmp sgt <2 x i8> %x, %tmp0
61  ret <2 x i1> %ret
62}
63
64define <2 x i1> @p2_vec_nonsplat() {
65; CHECK-LABEL: @p2_vec_nonsplat(
66; CHECK-NEXT:    [[X:%.*]] = call <2 x i8> @gen2x8()
67; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt <2 x i8> [[X]], <i8 3, i8 15>
68; CHECK-NEXT:    ret <2 x i1> [[TMP1]]
69;
70  %x = call <2 x i8> @gen2x8()
71  %tmp0 = and <2 x i8> %x, <i8 3, i8 15> ; doesn't have to be splat.
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 undef, 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
88; ============================================================================ ;
89; One-use tests. We don't care about multi-uses here.
90; ============================================================================ ;
91
92declare void @use8(i8)
93
94define i1 @oneuse0() {
95; CHECK-LABEL: @oneuse0(
96; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
97; CHECK-NEXT:    [[TMP0:%.*]] = and i8 [[X]], 3
98; CHECK-NEXT:    call void @use8(i8 [[TMP0]])
99; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt i8 [[X]], 3
100; CHECK-NEXT:    ret i1 [[TMP1]]
101;
102  %x = call i8 @gen8()
103  %tmp0 = and i8 %x, 3
104  call void @use8(i8 %tmp0)
105  %ret = icmp sgt i8 %x, %tmp0
106  ret i1 %ret
107}
108
109; ============================================================================ ;
110; Negative tests
111; ============================================================================ ;
112
113; ============================================================================ ;
114; Commutativity tests.
115; ============================================================================ ;
116
117define i1 @c0(i8 %x) {
118; CHECK-LABEL: @c0(
119; CHECK-NEXT:    [[TMP0:%.*]] = and i8 [[X:%.*]], 3
120; CHECK-NEXT:    [[RET:%.*]] = icmp sgt i8 [[TMP0]], [[X]]
121; CHECK-NEXT:    ret i1 [[RET]]
122;
123  %tmp0 = and i8 %x, 3
124  %ret = icmp sgt i8 %tmp0, %x ; swapped order
125  ret i1 %ret
126}
127
128; ============================================================================ ;
129; Commutativity tests with variable
130; ============================================================================ ;
131
132; Ok, this one should fold. We only testing commutativity of 'and'.
133define i1 @cv0_GOOD(i8 %y) {
134; CHECK-LABEL: @cv0_GOOD(
135; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
136; CHECK-NEXT:    [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
137; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt i8 [[X]], [[TMP0]]
138; CHECK-NEXT:    ret i1 [[TMP1]]
139;
140  %x = call i8 @gen8()
141  %tmp0 = lshr i8 -1, %y
142  %tmp1 = and i8 %tmp0, %x ; swapped order
143  %ret = icmp sgt i8 %x, %tmp1
144  ret i1 %ret
145}
146
147define i1 @cv1(i8 %y) {
148; CHECK-LABEL: @cv1(
149; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
150; CHECK-NEXT:    [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
151; CHECK-NEXT:    [[TMP1:%.*]] = and i8 [[X]], [[TMP0]]
152; CHECK-NEXT:    [[RET:%.*]] = icmp sgt i8 [[TMP1]], [[X]]
153; CHECK-NEXT:    ret i1 [[RET]]
154;
155  %x = call i8 @gen8()
156  %tmp0 = lshr i8 -1, %y
157  %tmp1 = and i8 %x, %tmp0
158  %ret = icmp sgt i8 %tmp1, %x ; swapped order
159  ret i1 %ret
160}
161
162define i1 @cv2(i8 %x, i8 %y) {
163; CHECK-LABEL: @cv2(
164; CHECK-NEXT:    [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
165; CHECK-NEXT:    [[TMP1:%.*]] = and i8 [[TMP0]], [[X:%.*]]
166; CHECK-NEXT:    [[RET:%.*]] = icmp sgt i8 [[TMP1]], [[X]]
167; CHECK-NEXT:    ret i1 [[RET]]
168;
169  %tmp0 = lshr i8 -1, %y
170  %tmp1 = and i8 %tmp0, %x ; swapped order
171  %ret = icmp sgt i8 %tmp1, %x ; swapped order
172  ret i1 %ret
173}
174
175; ============================================================================ ;
176; Normal negative tests
177; ============================================================================ ;
178
179define i1 @n0() {
180; CHECK-LABEL: @n0(
181; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
182; CHECK-NEXT:    [[TMP0:%.*]] = and i8 [[X]], 4
183; CHECK-NEXT:    [[RET:%.*]] = icmp sgt i8 [[X]], [[TMP0]]
184; CHECK-NEXT:    ret i1 [[RET]]
185;
186  %x = call i8 @gen8()
187  %tmp0 = and i8 %x, 4 ; power-of-two, but invalid.
188  %ret = icmp sgt i8 %x, %tmp0
189  ret i1 %ret
190}
191
192define i1 @n1(i8 %y, i8 %notx) {
193; CHECK-LABEL: @n1(
194; CHECK-NEXT:    [[X:%.*]] = call i8 @gen8()
195; CHECK-NEXT:    [[TMP0:%.*]] = and i8 [[X]], 3
196; CHECK-NEXT:    [[RET:%.*]] = icmp sgt i8 [[TMP0]], [[NOTX:%.*]]
197; CHECK-NEXT:    ret i1 [[RET]]
198;
199  %x = call i8 @gen8()
200  %tmp0 = and i8 %x, 3
201  %ret = icmp sgt i8 %tmp0, %notx ; not %x
202  ret i1 %ret
203}
204
205define <2 x i1> @n2() {
206; CHECK-LABEL: @n2(
207; CHECK-NEXT:    [[X:%.*]] = call <2 x i8> @gen2x8()
208; CHECK-NEXT:    [[TMP0:%.*]] = and <2 x i8> [[X]], <i8 3, i8 16>
209; CHECK-NEXT:    [[RET:%.*]] = icmp sgt <2 x i8> [[X]], [[TMP0]]
210; CHECK-NEXT:    ret <2 x i1> [[RET]]
211;
212  %x = call <2 x i8> @gen2x8()
213  %tmp0 = and <2 x i8> %x, <i8 3, i8 16> ; only the first one is valid.
214  %ret = icmp sgt <2 x i8> %x, %tmp0
215  ret <2 x i1> %ret
216}
217