1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
4 define i47 @shl_by_0(i47 %A) {
5 ; CHECK-LABEL: @shl_by_0(
6 ; CHECK-NEXT: ret i47 [[A:%.*]]
12 define i41 @shl_0(i41 %X) {
13 ; CHECK-LABEL: @shl_0(
14 ; CHECK-NEXT: ret i41 0
20 define <2 x i41> @shl_0_vec_undef_elt(<2 x i41> %X) {
21 ; CHECK-LABEL: @shl_0_vec_undef_elt(
22 ; CHECK-NEXT: ret <2 x i41> zeroinitializer
24 %B = shl <2 x i41> <i41 0, i41 undef>, %X
28 define i41 @ashr_by_0(i41 %A) {
29 ; CHECK-LABEL: @ashr_by_0(
30 ; CHECK-NEXT: ret i41 [[A:%.*]]
36 define i39 @ashr_0(i39 %X) {
37 ; CHECK-LABEL: @ashr_0(
38 ; CHECK-NEXT: ret i39 0
44 define <2 x i141> @ashr_0_vec_undef_elt(<2 x i141> %X) {
45 ; CHECK-LABEL: @ashr_0_vec_undef_elt(
46 ; CHECK-NEXT: ret <2 x i141> zeroinitializer
48 %B = shl <2 x i141> <i141 undef, i141 0>, %X
52 define i55 @lshr_by_bitwidth(i55 %A) {
53 ; CHECK-LABEL: @lshr_by_bitwidth(
54 ; CHECK-NEXT: ret i55 poison
60 define i32 @shl_by_bitwidth(i32 %A) {
61 ; CHECK-LABEL: @shl_by_bitwidth(
62 ; CHECK-NEXT: ret i32 poison
68 define <4 x i32> @lshr_by_bitwidth_splat(<4 x i32> %A) {
69 ; CHECK-LABEL: @lshr_by_bitwidth_splat(
70 ; CHECK-NEXT: ret <4 x i32> poison
72 %B = lshr <4 x i32> %A, <i32 32, i32 32, i32 32, i32 32> ;; shift all bits out
76 define <4 x i32> @lshr_by_0_splat(<4 x i32> %A) {
77 ; CHECK-LABEL: @lshr_by_0_splat(
78 ; CHECK-NEXT: ret <4 x i32> [[A:%.*]]
80 %B = lshr <4 x i32> %A, zeroinitializer
84 define <4 x i32> @shl_by_bitwidth_splat(<4 x i32> %A) {
85 ; CHECK-LABEL: @shl_by_bitwidth_splat(
86 ; CHECK-NEXT: ret <4 x i32> poison
88 %B = shl <4 x i32> %A, <i32 32, i32 32, i32 32, i32 32> ;; shift all bits out
92 define i32 @ashr_undef() {
93 ; CHECK-LABEL: @ashr_undef(
94 ; CHECK-NEXT: ret i32 0
96 %B = ashr i32 undef, 2 ;; top two bits must be equal, so not undef
100 define i32 @ashr_undef_variable_shift_amount(i32 %A) {
101 ; CHECK-LABEL: @ashr_undef_variable_shift_amount(
102 ; CHECK-NEXT: ret i32 0
104 %B = ashr i32 undef, %A ;; top %A bits must be equal, so not undef
108 define i32 @ashr_all_ones(i32 %A) {
109 ; CHECK-LABEL: @ashr_all_ones(
110 ; CHECK-NEXT: ret i32 -1
116 define <3 x i8> @ashr_all_ones_vec_with_undef_elts(<3 x i8> %x, <3 x i8> %y) {
117 ; CHECK-LABEL: @ashr_all_ones_vec_with_undef_elts(
118 ; CHECK-NEXT: ret <3 x i8> <i8 -1, i8 -1, i8 -1>
120 %sh = ashr <3 x i8> <i8 undef, i8 -1, i8 undef>, %y
124 define i8 @lshr_by_sext_bool(i1 %x, i8 %y) {
125 ; CHECK-LABEL: @lshr_by_sext_bool(
126 ; CHECK-NEXT: ret i8 [[Y:%.*]]
128 %s = sext i1 %x to i8
133 define <2 x i8> @lshr_by_sext_bool_vec(<2 x i1> %x, <2 x i8> %y) {
134 ; CHECK-LABEL: @lshr_by_sext_bool_vec(
135 ; CHECK-NEXT: ret <2 x i8> [[Y:%.*]]
137 %s = sext <2 x i1> %x to <2 x i8>
138 %r = lshr <2 x i8> %y, %s
142 define i8 @ashr_by_sext_bool(i1 %x, i8 %y) {
143 ; CHECK-LABEL: @ashr_by_sext_bool(
144 ; CHECK-NEXT: ret i8 [[Y:%.*]]
146 %s = sext i1 %x to i8
151 define <2 x i8> @ashr_by_sext_bool_vec(<2 x i1> %x, <2 x i8> %y) {
152 ; CHECK-LABEL: @ashr_by_sext_bool_vec(
153 ; CHECK-NEXT: ret <2 x i8> [[Y:%.*]]
155 %s = sext <2 x i1> %x to <2 x i8>
156 %r = ashr <2 x i8> %y, %s
160 define i8 @shl_by_sext_bool(i1 %x, i8 %y) {
161 ; CHECK-LABEL: @shl_by_sext_bool(
162 ; CHECK-NEXT: ret i8 [[Y:%.*]]
164 %s = sext i1 %x to i8
169 define <2 x i8> @shl_by_sext_bool_vec(<2 x i1> %x, <2 x i8> %y) {
170 ; CHECK-LABEL: @shl_by_sext_bool_vec(
171 ; CHECK-NEXT: ret <2 x i8> [[Y:%.*]]
173 %s = sext <2 x i1> %x to <2 x i8>
174 %r = shl <2 x i8> %y, %s
178 define i64 @shl_or_shr(i32 %a, i32 %b) {
179 ; CHECK-LABEL: @shl_or_shr(
180 ; CHECK-NEXT: [[T1:%.*]] = zext i32 [[A:%.*]] to i64
181 ; CHECK-NEXT: ret i64 [[T1]]
183 %t1 = zext i32 %a to i64
184 %t2 = zext i32 %b to i64
185 %t3 = shl nuw i64 %t1, 32
186 %t4 = or i64 %t2, %t3
187 %t5 = lshr i64 %t4, 32
191 ; Since shift count of shl is smaller than the size of %b, OR cannot be eliminated.
192 define i64 @shl_or_shr2(i32 %a, i32 %b) {
193 ; CHECK-LABEL: @shl_or_shr2(
194 ; CHECK-NEXT: [[T1:%.*]] = zext i32 [[A:%.*]] to i64
195 ; CHECK-NEXT: [[T2:%.*]] = zext i32 [[B:%.*]] to i64
196 ; CHECK-NEXT: [[T3:%.*]] = shl nuw i64 [[T1]], 31
197 ; CHECK-NEXT: [[T4:%.*]] = or i64 [[T2]], [[T3]]
198 ; CHECK-NEXT: [[T5:%.*]] = lshr i64 [[T4]], 31
199 ; CHECK-NEXT: ret i64 [[T5]]
201 %t1 = zext i32 %a to i64
202 %t2 = zext i32 %b to i64
203 %t3 = shl nuw i64 %t1, 31
204 %t4 = or i64 %t2, %t3
205 %t5 = lshr i64 %t4, 31
209 ; Unit test for vector integer
210 define <2 x i64> @shl_or_shr1v(<2 x i32> %a, <2 x i32> %b) {
211 ; CHECK-LABEL: @shl_or_shr1v(
212 ; CHECK-NEXT: [[T1:%.*]] = zext <2 x i32> [[A:%.*]] to <2 x i64>
213 ; CHECK-NEXT: ret <2 x i64> [[T1]]
215 %t1 = zext <2 x i32> %a to <2 x i64>
216 %t2 = zext <2 x i32> %b to <2 x i64>
217 %t3 = shl nuw <2 x i64> %t1, <i64 32, i64 32>
218 %t4 = or <2 x i64> %t3, %t2
219 %t5 = lshr <2 x i64> %t4, <i64 32, i64 32>
223 ; Negative unit test for vector integer
224 define <2 x i64> @shl_or_shr2v(<2 x i32> %a, <2 x i32> %b) {
225 ; CHECK-LABEL: @shl_or_shr2v(
226 ; CHECK-NEXT: [[T1:%.*]] = zext <2 x i32> [[A:%.*]] to <2 x i64>
227 ; CHECK-NEXT: [[T2:%.*]] = zext <2 x i32> [[B:%.*]] to <2 x i64>
228 ; CHECK-NEXT: [[T3:%.*]] = shl nuw <2 x i64> [[T1]], <i64 31, i64 31>
229 ; CHECK-NEXT: [[T4:%.*]] = or <2 x i64> [[T2]], [[T3]]
230 ; CHECK-NEXT: [[T5:%.*]] = lshr <2 x i64> [[T4]], <i64 31, i64 31>
231 ; CHECK-NEXT: ret <2 x i64> [[T5]]
233 %t1 = zext <2 x i32> %a to <2 x i64>
234 %t2 = zext <2 x i32> %b to <2 x i64>
235 %t3 = shl nuw <2 x i64> %t1, <i64 31, i64 31>
236 %t4 = or <2 x i64> %t2, %t3
237 %t5 = lshr <2 x i64> %t4, <i64 31, i64 31>
241 define i32 @poison(i32 %x) {
242 ; CHECK-LABEL: @poison(
243 ; CHECK-NEXT: ret i32 poison
245 %v = lshr i32 %x, poison
249 define i32 @poison2(i32 %x) {
250 ; CHECK-LABEL: @poison2(
251 ; CHECK-NEXT: ret i32 poison
253 %v = ashr i32 %x, poison
257 define i32 @poison3(i32 %x) {
258 ; CHECK-LABEL: @poison3(
259 ; CHECK-NEXT: ret i32 poison
261 %v = shl i32 %x, poison
265 define i32 @poison4(i32 %x) {
266 ; CHECK-LABEL: @poison4(
267 ; CHECK-NEXT: ret i32 poison
269 %v = lshr i32 poison, %x
273 define i32 @poison5(i32 %x) {
274 ; CHECK-LABEL: @poison5(
275 ; CHECK-NEXT: ret i32 poison
277 %v = ashr i32 poison, %x
281 define i32 @poison6(i32 %x) {
282 ; CHECK-LABEL: @poison6(
283 ; CHECK-NEXT: ret i32 poison
285 %v = shl i32 poison, %x
289 define i32 @all_ones_left_right(i32 %x) {
290 ; CHECK-LABEL: @all_ones_left_right(
291 ; CHECK-NEXT: ret i32 -1
293 %left = shl i32 -1, %x
294 %right = ashr i32 %left, %x
298 define <2 x i7> @all_ones_left_right_splat(<2 x i7> %x) {
299 ; CHECK-LABEL: @all_ones_left_right_splat(
300 ; CHECK-NEXT: ret <2 x i7> <i7 -1, i7 -1>
302 %left = shl <2 x i7> <i7 -1, i7 -1>, %x
303 %right = ashr <2 x i7> %left, %x
307 ; Poison could propagate, but undef must not.
309 define <3 x i7> @all_ones_left_right_splat_poison_undef_elt(<3 x i7> %x) {
310 ; CHECK-LABEL: @all_ones_left_right_splat_poison_undef_elt(
311 ; CHECK-NEXT: ret <3 x i7> <i7 -1, i7 -1, i7 -1>
313 %left = shl <3 x i7> <i7 poison, i7 -1, i7 undef>, %x
314 %right = ashr <3 x i7> %left, %x
318 ; negative test - must have -1
320 define i32 @almost_all_ones_left_right(i32 %x) {
321 ; CHECK-LABEL: @almost_all_ones_left_right(
322 ; CHECK-NEXT: [[LEFT:%.*]] = shl i32 -2, [[X:%.*]]
323 ; CHECK-NEXT: [[RIGHT:%.*]] = ashr i32 [[LEFT]], [[X]]
324 ; CHECK-NEXT: ret i32 [[RIGHT]]
326 %left = shl i32 -2, %x
327 %right = ashr i32 %left, %x
331 ; negative test - must have same shift amount
333 define i32 @all_ones_left_right_not_same_shift(i32 %x, i32 %y) {
334 ; CHECK-LABEL: @all_ones_left_right_not_same_shift(
335 ; CHECK-NEXT: [[LEFT:%.*]] = shl i32 -1, [[X:%.*]]
336 ; CHECK-NEXT: [[RIGHT:%.*]] = ashr i32 [[LEFT]], [[Y:%.*]]
337 ; CHECK-NEXT: ret i32 [[RIGHT]]
339 %left = shl i32 -1, %x
340 %right = ashr i32 %left, %y
344 ; shift by splat of bitwidth or more is poison
346 define <vscale x 4 x i16> @lshr_scalable_overshift(<vscale x 4 x i16> %va) {
347 ; CHECK-LABEL: @lshr_scalable_overshift(
348 ; CHECK-NEXT: ret <vscale x 4 x i16> poison
350 %vc = lshr <vscale x 4 x i16> %va, shufflevector (<vscale x 4 x i16> insertelement (<vscale x 4 x i16> poison, i16 16, i32 0), <vscale x 4 x i16> poison, <vscale x 4 x i32> zeroinitializer)
351 ret <vscale x 4 x i16> %vc
355 define i8 @shl_nsw_nuw_7_eq_0(i8 %x) {
356 ; CHECK-LABEL: @shl_nsw_nuw_7_eq_0(
357 ; CHECK-NEXT: ret i8 0
359 %y = shl nsw nuw i8 %x, 7
363 ; Make sure we match the element width
364 define <2 x i8> @shl_vec_nsw_nuw_7_eq_0(<2 x i8> %x) {
365 ; CHECK-LABEL: @shl_vec_nsw_nuw_7_eq_0(
366 ; CHECK-NEXT: ret <2 x i8> zeroinitializer
368 %y = shl nsw nuw <2 x i8> %x, <i8 7, i8 7>
372 ; negative test (missing nuw)
373 define i8 @shl_nsw_7_fail_missing_nuw(i8 %x) {
374 ; CHECK-LABEL: @shl_nsw_7_fail_missing_nuw(
375 ; CHECK-NEXT: [[Y:%.*]] = shl nsw i8 [[X:%.*]], 7
376 ; CHECK-NEXT: ret i8 [[Y]]
378 %y = shl nsw i8 %x, 7
382 ; negative test (missing nsw)
383 define i8 @shl_nuw_7_fail_missing_nsw(i8 %x) {
384 ; CHECK-LABEL: @shl_nuw_7_fail_missing_nsw(
385 ; CHECK-NEXT: [[Y:%.*]] = shl nuw i8 [[X:%.*]], 7
386 ; CHECK-NEXT: ret i8 [[Y]]
388 %y = shl nuw i8 %x, 7
392 ; negative test (shift value != bitwdth - 1)
393 define i8 @shl_nsw_nuw_6_do_nothing(i8 %x) {
394 ; CHECK-LABEL: @shl_nsw_nuw_6_do_nothing(
395 ; CHECK-NEXT: [[Y:%.*]] = shl nuw nsw i8 [[X:%.*]], 6
396 ; CHECK-NEXT: ret i8 [[Y]]
398 %y = shl nsw nuw i8 %x, 6