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 undef, 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 ; ============================================================================ ;
89 ; One-use tests. We don't care about multi-uses here.
90 ; ============================================================================ ;
92 declare void @use8(i8)
94 define 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]]
104 call void @use8(i8 %tmp0)
105 %ret = icmp sgt i8 %x, %tmp0
109 ; ============================================================================ ;
111 ; ============================================================================ ;
113 ; Commutativity tests.
115 define i1 @c0(i8 %x) {
117 ; CHECK-NEXT: [[TMP0:%.*]] = and i8 [[X:%.*]], 3
118 ; CHECK-NEXT: [[RET:%.*]] = icmp sgt i8 [[TMP0]], [[X]]
119 ; CHECK-NEXT: ret i1 [[RET]]
122 %ret = icmp sgt i8 %tmp0, %x ; swapped order
126 ; ============================================================================ ;
127 ; Rest of negative tests
128 ; ============================================================================ ;
132 ; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()
133 ; CHECK-NEXT: [[TMP0:%.*]] = and i8 [[X]], 4
134 ; CHECK-NEXT: [[RET:%.*]] = icmp sgt i8 [[X]], [[TMP0]]
135 ; CHECK-NEXT: ret i1 [[RET]]
138 %tmp0 = and i8 %x, 4 ; power-of-two, but invalid.
139 %ret = icmp sgt i8 %x, %tmp0
143 define i1 @n1(i8 %y, i8 %notx) {
145 ; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()
146 ; CHECK-NEXT: [[TMP0:%.*]] = and i8 [[X]], 3
147 ; CHECK-NEXT: [[RET:%.*]] = icmp sgt i8 [[TMP0]], [[NOTX:%.*]]
148 ; CHECK-NEXT: ret i1 [[RET]]
152 %ret = icmp sgt i8 %tmp0, %notx ; not %x
156 define <2 x i1> @n2() {
158 ; CHECK-NEXT: [[X:%.*]] = call <2 x i8> @gen2x8()
159 ; CHECK-NEXT: [[TMP0:%.*]] = and <2 x i8> [[X]], <i8 3, i8 16>
160 ; CHECK-NEXT: [[RET:%.*]] = icmp sgt <2 x i8> [[X]], [[TMP0]]
161 ; CHECK-NEXT: ret <2 x i1> [[RET]]
163 %x = call <2 x i8> @gen2x8()
164 %tmp0 = and <2 x i8> %x, <i8 3, i8 16> ; only the first one is valid.
165 %ret = icmp sgt <2 x i8> %x, %tmp0
169 ; ============================================================================ ;
170 ; Potential miscompiles.
171 ; ============================================================================ ;
173 define i1 @pv(i8 %y) {
175 ; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()
176 ; CHECK-NEXT: [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
177 ; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[TMP0]], [[X]]
178 ; CHECK-NEXT: [[RET:%.*]] = icmp sgt i8 [[X]], [[TMP1]]
179 ; CHECK-NEXT: ret i1 [[RET]]
182 %tmp0 = lshr i8 -1, %y
183 %tmp1 = and i8 %tmp0, %x
184 %ret = icmp sgt i8 %x, %tmp1
188 define <2 x i1> @n3_vec() {
189 ; CHECK-LABEL: @n3_vec(
190 ; CHECK-NEXT: [[X:%.*]] = call <2 x i8> @gen2x8()
191 ; CHECK-NEXT: [[TMP0:%.*]] = and <2 x i8> [[X]], <i8 3, i8 -1>
192 ; CHECK-NEXT: [[RET:%.*]] = icmp sgt <2 x i8> [[X]], [[TMP0]]
193 ; CHECK-NEXT: ret <2 x i1> [[RET]]
195 %x = call <2 x i8> @gen2x8()
196 %tmp0 = and <2 x i8> %x, <i8 3, i8 -1>
197 %ret = icmp sgt <2 x i8> %x, %tmp0
201 define <3 x i1> @n4_vec() {
202 ; CHECK-LABEL: @n4_vec(
203 ; CHECK-NEXT: [[X:%.*]] = call <3 x i8> @gen3x8()
204 ; CHECK-NEXT: [[TMP0:%.*]] = and <3 x i8> [[X]], <i8 3, i8 undef, i8 -1>
205 ; CHECK-NEXT: [[RET:%.*]] = icmp sgt <3 x i8> [[X]], [[TMP0]]
206 ; CHECK-NEXT: ret <3 x i1> [[RET]]
208 %x = call <3 x i8> @gen3x8()
209 %tmp0 = and <3 x i8> %x, <i8 3, i8 undef, i8 -1>
210 %ret = icmp sgt <3 x i8> %x, %tmp0
214 ; Commutativity tests with variable
216 ; Ok, this one should fold. We only testing commutativity of 'and'.
217 define i1 @cv0_GOOD(i8 %y) {
218 ; CHECK-LABEL: @cv0_GOOD(
219 ; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()
220 ; CHECK-NEXT: [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
221 ; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[TMP0]], [[X]]
222 ; CHECK-NEXT: [[RET:%.*]] = icmp sgt i8 [[X]], [[TMP1]]
223 ; CHECK-NEXT: ret i1 [[RET]]
226 %tmp0 = lshr i8 -1, %y
227 %tmp1 = and i8 %tmp0, %x ; swapped order
228 %ret = icmp sgt i8 %x, %tmp1
232 define i1 @cv1(i8 %y) {
234 ; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()
235 ; CHECK-NEXT: [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
236 ; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X]], [[TMP0]]
237 ; CHECK-NEXT: [[RET:%.*]] = icmp sgt i8 [[TMP1]], [[X]]
238 ; CHECK-NEXT: ret i1 [[RET]]
241 %tmp0 = lshr i8 -1, %y
242 %tmp1 = and i8 %x, %tmp0
243 %ret = icmp sgt i8 %tmp1, %x ; swapped order
247 define i1 @cv2(i8 %x, i8 %y) {
249 ; CHECK-NEXT: [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
250 ; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[TMP0]], [[X:%.*]]
251 ; CHECK-NEXT: [[RET:%.*]] = icmp sgt i8 [[TMP1]], [[X]]
252 ; CHECK-NEXT: ret i1 [[RET]]
254 %tmp0 = lshr i8 -1, %y
255 %tmp1 = and i8 %tmp0, %x ; swapped order
256 %ret = icmp sgt i8 %tmp1, %x ; swapped order