1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
4 ; https://bugs.llvm.org/show_bug.cgi?id=38123
8 ; Should be transformed into:
10 ; Iff: isPowerOf2(C + 1)
11 ; C must not be -1, but may be 0.
13 ; NOTE: this pattern is not commutative!
16 declare <2 x i8> @gen2x8()
17 declare <3 x i8> @gen3x8()
19 ; ============================================================================ ;
20 ; Basic positive tests
21 ; ============================================================================ ;
25 ; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()
26 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i8 [[X]], 3
27 ; CHECK-NEXT: ret i1 [[TMP1]]
31 %ret = icmp sgt i8 %x, %tmp0
35 ; ============================================================================ ;
37 ; ============================================================================ ;
39 define <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]]
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
51 define <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]]
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
63 define <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]]
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
76 define <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]]
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
88 define <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]]
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
100 ; ============================================================================ ;
101 ; One-use tests. We don't care about multi-uses here.
102 ; ============================================================================ ;
104 declare void @use8(i8)
106 define 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]]
116 call void @use8(i8 %tmp0)
117 %ret = icmp sgt i8 %x, %tmp0
121 ; ============================================================================ ;
123 ; ============================================================================ ;
125 ; Commutativity tests.
127 define i1 @c0(i8 %x) {
129 ; CHECK-NEXT: [[TMP0:%.*]] = and i8 [[X:%.*]], 3
130 ; CHECK-NEXT: [[RET:%.*]] = icmp sgt i8 [[TMP0]], [[X]]
131 ; CHECK-NEXT: ret i1 [[RET]]
134 %ret = icmp sgt i8 %tmp0, %x ; swapped order
138 ; ============================================================================ ;
139 ; Rest of negative tests
140 ; ============================================================================ ;
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]]
150 %tmp0 = and i8 %x, 4 ; power-of-two, but invalid.
151 %ret = icmp sgt i8 %x, %tmp0
155 define i1 @n1(i8 %y, i8 %notx) {
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]]
164 %ret = icmp sgt i8 %tmp0, %notx ; not %x
168 define <2 x i1> @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]]
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
181 ; ============================================================================ ;
182 ; Potential miscompiles.
183 ; ============================================================================ ;
185 define i1 @pv(i8 %y) {
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]]
194 %tmp0 = lshr i8 -1, %y
195 %tmp1 = and i8 %tmp0, %x
196 %ret = icmp sgt i8 %x, %tmp1
200 define <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]]
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
213 define <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]]
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
226 ; Commutativity tests with variable
228 ; Ok, this one should fold. We only testing commutativity of 'and'.
229 define 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]]
238 %tmp0 = lshr i8 -1, %y
239 %tmp1 = and i8 %tmp0, %x ; swapped order
240 %ret = icmp sgt i8 %x, %tmp1
244 define i1 @cv1(i8 %y) {
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]]
253 %tmp0 = lshr i8 -1, %y
254 %tmp1 = and i8 %x, %tmp0
255 %ret = icmp sgt i8 %tmp1, %x ; swapped order
259 define i1 @cv2(i8 %x, i8 %y) {
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]]
266 %tmp0 = lshr i8 -1, %y
267 %tmp1 = and i8 %tmp0, %x ; swapped order
268 %ret = icmp sgt i8 %tmp1, %x ; swapped order