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 ; ============================================================================ ;
14 ; Basic positive tests
15 ; ============================================================================ ;
17 define i1 @p0(i8 %x) {
19 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i8 [[X:%.*]], 3
20 ; CHECK-NEXT: ret i1 [[TMP1]]
23 %ret = icmp slt i8 %tmp0, %x
27 ; ============================================================================ ;
29 ; ============================================================================ ;
31 define <2 x i1> @p1_vec_splat(<2 x i8> %x) {
32 ; CHECK-LABEL: @p1_vec_splat(
33 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt <2 x i8> [[X:%.*]], <i8 3, i8 3>
34 ; CHECK-NEXT: ret <2 x i1> [[TMP1]]
36 %tmp0 = and <2 x i8> %x, <i8 3, i8 3>
37 %ret = icmp slt <2 x i8> %tmp0, %x
41 define <2 x i1> @p2_vec_nonsplat(<2 x i8> %x) {
42 ; CHECK-LABEL: @p2_vec_nonsplat(
43 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt <2 x i8> [[X:%.*]], <i8 3, i8 15>
44 ; CHECK-NEXT: ret <2 x i1> [[TMP1]]
46 %tmp0 = and <2 x i8> %x, <i8 3, i8 15> ; doesn't have to be splat.
47 %ret = icmp slt <2 x i8> %tmp0, %x
51 define <2 x i1> @p2_vec_nonsplat_edgecase(<2 x i8> %x) {
52 ; CHECK-LABEL: @p2_vec_nonsplat_edgecase(
53 ; CHECK-NEXT: [[TMP0:%.*]] = and <2 x i8> [[X:%.*]], <i8 3, i8 0>
54 ; CHECK-NEXT: [[RET:%.*]] = icmp slt <2 x i8> [[TMP0]], [[X]]
55 ; CHECK-NEXT: ret <2 x i1> [[RET]]
57 %tmp0 = and <2 x i8> %x, <i8 3, i8 0>
58 %ret = icmp slt <2 x i8> %tmp0, %x
62 define <3 x i1> @p3_vec_splat_undef(<3 x i8> %x) {
63 ; CHECK-LABEL: @p3_vec_splat_undef(
64 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt <3 x i8> [[X:%.*]], <i8 3, i8 3, i8 3>
65 ; CHECK-NEXT: ret <3 x i1> [[TMP1]]
67 %tmp0 = and <3 x i8> %x, <i8 3, i8 undef, i8 3>
68 %ret = icmp slt <3 x i8> %tmp0, %x
72 define <3 x i1> @p3_vec_nonsplat_undef(<3 x i8> %x) {
73 ; CHECK-LABEL: @p3_vec_nonsplat_undef(
74 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt <3 x i8> [[X:%.*]], <i8 15, i8 15, i8 3>
75 ; CHECK-NEXT: ret <3 x i1> [[TMP1]]
77 %tmp0 = and <3 x i8> %x, <i8 undef, i8 15, i8 3>
78 %ret = icmp slt <3 x i8> %tmp0, %x
82 ; ============================================================================ ;
83 ; One-use tests. We don't care about multi-uses here.
84 ; ============================================================================ ;
86 declare void @use8(i8)
88 define i1 @oneuse0(i8 %x) {
89 ; CHECK-LABEL: @oneuse0(
90 ; CHECK-NEXT: [[TMP0:%.*]] = and i8 [[X:%.*]], 3
91 ; CHECK-NEXT: call void @use8(i8 [[TMP0]])
92 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i8 [[X]], 3
93 ; CHECK-NEXT: ret i1 [[TMP1]]
96 call void @use8(i8 %tmp0)
97 %ret = icmp slt i8 %tmp0, %x
101 ; ============================================================================ ;
103 ; ============================================================================ ;
105 ; Commutativity tests.
111 ; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()
112 ; CHECK-NEXT: [[TMP0:%.*]] = and i8 [[X]], 3
113 ; CHECK-NEXT: [[RET:%.*]] = icmp slt i8 [[X]], [[TMP0]]
114 ; CHECK-NEXT: ret i1 [[RET]]
118 %ret = icmp slt i8 %x, %tmp0 ; swapped order
122 ; ============================================================================ ;
123 ; Rest of negative tests
124 ; ============================================================================ ;
126 define i1 @n0(i8 %x) {
128 ; CHECK-NEXT: [[TMP0:%.*]] = and i8 [[X:%.*]], 4
129 ; CHECK-NEXT: [[RET:%.*]] = icmp slt i8 [[TMP0]], [[X]]
130 ; CHECK-NEXT: ret i1 [[RET]]
132 %tmp0 = and i8 %x, 4 ; power-of-two, but invalid.
133 %ret = icmp slt i8 %tmp0, %x
137 define i1 @n1(i8 %x, i8 %y, i8 %notx) {
139 ; CHECK-NEXT: [[TMP0:%.*]] = and i8 [[X:%.*]], 3
140 ; CHECK-NEXT: [[RET:%.*]] = icmp slt i8 [[TMP0]], [[NOTX:%.*]]
141 ; CHECK-NEXT: ret i1 [[RET]]
144 %ret = icmp slt i8 %tmp0, %notx ; not %x
148 define <2 x i1> @n2(<2 x i8> %x) {
150 ; CHECK-NEXT: [[TMP0:%.*]] = and <2 x i8> [[X:%.*]], <i8 3, i8 16>
151 ; CHECK-NEXT: [[RET:%.*]] = icmp slt <2 x i8> [[TMP0]], [[X]]
152 ; CHECK-NEXT: ret <2 x i1> [[RET]]
154 %tmp0 = and <2 x i8> %x, <i8 3, i8 16> ; only the first one is valid.
155 %ret = icmp slt <2 x i8> %tmp0, %x
159 ; ============================================================================ ;
160 ; Potential miscompiles.
161 ; ============================================================================ ;
163 define i1 @nv(i8 %x, i8 %y) {
165 ; CHECK-NEXT: [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
166 ; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[TMP0]], [[X:%.*]]
167 ; CHECK-NEXT: [[RET:%.*]] = icmp slt i8 [[TMP1]], [[X]]
168 ; CHECK-NEXT: ret i1 [[RET]]
170 %tmp0 = lshr i8 -1, %y
171 %tmp1 = and i8 %tmp0, %x
172 %ret = icmp slt i8 %tmp1, %x
176 define <2 x i1> @n3(<2 x i8> %x) {
178 ; CHECK-NEXT: [[TMP0:%.*]] = and <2 x i8> [[X:%.*]], <i8 3, i8 -1>
179 ; CHECK-NEXT: [[RET:%.*]] = icmp slt <2 x i8> [[TMP0]], [[X]]
180 ; CHECK-NEXT: ret <2 x i1> [[RET]]
182 %tmp0 = and <2 x i8> %x, <i8 3, i8 -1>
183 %ret = icmp slt <2 x i8> %tmp0, %x
187 define <3 x i1> @n4(<3 x i8> %x) {
189 ; CHECK-NEXT: [[TMP0:%.*]] = and <3 x i8> [[X:%.*]], <i8 3, i8 undef, i8 -1>
190 ; CHECK-NEXT: [[RET:%.*]] = icmp slt <3 x i8> [[TMP0]], [[X]]
191 ; CHECK-NEXT: ret <3 x i1> [[RET]]
193 %tmp0 = and <3 x i8> %x, <i8 3, i8 undef, i8 -1>
194 %ret = icmp slt <3 x i8> %tmp0, %x
198 ; Commutativity tests with variable
200 define i1 @cv0(i8 %y) {
202 ; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()
203 ; CHECK-NEXT: [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
204 ; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X]], [[TMP0]]
205 ; CHECK-NEXT: [[RET:%.*]] = icmp slt i8 [[TMP1]], [[X]]
206 ; CHECK-NEXT: ret i1 [[RET]]
209 %tmp0 = lshr i8 -1, %y
210 %tmp1 = and i8 %x, %tmp0 ; swapped order
211 %ret = icmp slt i8 %tmp1, %x
215 define i1 @cv1(i8 %y) {
217 ; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()
218 ; CHECK-NEXT: [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
219 ; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[TMP0]], [[X]]
220 ; CHECK-NEXT: [[RET:%.*]] = icmp slt i8 [[X]], [[TMP1]]
221 ; CHECK-NEXT: ret i1 [[RET]]
224 %tmp0 = lshr i8 -1, %y
225 %tmp1 = and i8 %tmp0, %x
226 %ret = icmp slt i8 %x, %tmp1 ; swapped order
230 define i1 @cv2(i8 %y) {
232 ; CHECK-NEXT: [[X:%.*]] = call i8 @gen8()
233 ; CHECK-NEXT: [[TMP0:%.*]] = lshr i8 -1, [[Y:%.*]]
234 ; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X]], [[TMP0]]
235 ; CHECK-NEXT: [[RET:%.*]] = icmp slt i8 [[X]], [[TMP1]]
236 ; CHECK-NEXT: ret i1 [[RET]]
239 %tmp0 = lshr i8 -1, %y
240 %tmp1 = and i8 %x, %tmp0 ; swapped order
241 %ret = icmp slt i8 %x, %tmp1 ; swapped order