1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -instsimplify -S | FileCheck %s
3; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
4
5declare {i8, i1} @llvm.uadd.with.overflow.i8(i8 %a, i8 %b)
6declare {i8, i1} @llvm.sadd.with.overflow.i8(i8 %a, i8 %b)
7declare {i8, i1} @llvm.usub.with.overflow.i8(i8 %a, i8 %b)
8declare {i8, i1} @llvm.ssub.with.overflow.i8(i8 %a, i8 %b)
9declare {i8, i1} @llvm.umul.with.overflow.i8(i8 %a, i8 %b)
10declare {i8, i1} @llvm.smul.with.overflow.i8(i8 %a, i8 %b)
11
12define i1 @test_uadd1() {
13; CHECK-LABEL: @test_uadd1(
14; CHECK-NEXT:    ret i1 true
15;
16  %x = call {i8, i1} @llvm.uadd.with.overflow.i8(i8 254, i8 3)
17  %overflow = extractvalue {i8, i1} %x, 1
18  ret i1 %overflow
19}
20
21define i8 @test_uadd2() {
22; CHECK-LABEL: @test_uadd2(
23; CHECK-NEXT:    ret i8 42
24;
25  %x = call {i8, i1} @llvm.uadd.with.overflow.i8(i8 254, i8 44)
26  %result = extractvalue {i8, i1} %x, 0
27  ret i8 %result
28}
29
30define {i8, i1} @test_uadd3(i8 %v) {
31; CHECK-LABEL: @test_uadd3(
32; CHECK-NEXT:    ret { i8, i1 } undef
33;
34  %result = call {i8, i1} @llvm.uadd.with.overflow.i8(i8 %v, i8 undef)
35  ret {i8, i1} %result
36}
37
38define {i8, i1} @test_uadd4(i8 %v) {
39; CHECK-LABEL: @test_uadd4(
40; CHECK-NEXT:    ret { i8, i1 } undef
41;
42  %result = call {i8, i1} @llvm.uadd.with.overflow.i8(i8 undef, i8 %v)
43  ret {i8, i1} %result
44}
45
46define i1 @test_sadd1() {
47; CHECK-LABEL: @test_sadd1(
48; CHECK-NEXT:    ret i1 true
49;
50  %x = call {i8, i1} @llvm.sadd.with.overflow.i8(i8 126, i8 3)
51  %overflow = extractvalue {i8, i1} %x, 1
52  ret i1 %overflow
53}
54
55define i8 @test_sadd2() {
56; CHECK-LABEL: @test_sadd2(
57; CHECK-NEXT:    ret i8 -86
58;
59  %x = call {i8, i1} @llvm.sadd.with.overflow.i8(i8 126, i8 44)
60  %result = extractvalue {i8, i1} %x, 0
61  ret i8 %result
62}
63
64define {i8, i1} @test_sadd3(i8 %v) {
65; CHECK-LABEL: @test_sadd3(
66; CHECK-NEXT:    ret { i8, i1 } undef
67;
68  %result = call {i8, i1} @llvm.sadd.with.overflow.i8(i8 %v, i8 undef)
69  ret {i8, i1} %result
70}
71
72define {i8, i1} @test_sadd4(i8 %v) {
73; CHECK-LABEL: @test_sadd4(
74; CHECK-NEXT:    ret { i8, i1 } undef
75;
76  %result = call {i8, i1} @llvm.sadd.with.overflow.i8(i8 undef, i8 %v)
77  ret {i8, i1} %result
78}
79
80define {i8, i1} @test_usub1(i8 %V) {
81; CHECK-LABEL: @test_usub1(
82; CHECK-NEXT:    ret { i8, i1 } zeroinitializer
83;
84  %x = call {i8, i1} @llvm.usub.with.overflow.i8(i8 %V, i8 %V)
85  ret {i8, i1} %x
86}
87
88define {i8, i1} @test_usub2(i8 %V) {
89; CHECK-LABEL: @test_usub2(
90; CHECK-NEXT:    ret { i8, i1 } undef
91;
92  %x = call {i8, i1} @llvm.usub.with.overflow.i8(i8 %V, i8 undef)
93  ret {i8, i1} %x
94}
95
96define {i8, i1} @test_usub3(i8 %V) {
97; CHECK-LABEL: @test_usub3(
98; CHECK-NEXT:    ret { i8, i1 } undef
99;
100  %x = call {i8, i1} @llvm.usub.with.overflow.i8(i8 undef, i8 %V)
101  ret {i8, i1} %x
102}
103
104define {i8, i1} @test_ssub1(i8 %V) {
105; CHECK-LABEL: @test_ssub1(
106; CHECK-NEXT:    ret { i8, i1 } zeroinitializer
107;
108  %x = call {i8, i1} @llvm.ssub.with.overflow.i8(i8 %V, i8 %V)
109  ret {i8, i1} %x
110}
111
112define {i8, i1} @test_ssub2(i8 %V) {
113; CHECK-LABEL: @test_ssub2(
114; CHECK-NEXT:    ret { i8, i1 } undef
115;
116  %x = call {i8, i1} @llvm.ssub.with.overflow.i8(i8 %V, i8 undef)
117  ret {i8, i1} %x
118}
119
120define {i8, i1} @test_ssub3(i8 %V) {
121; CHECK-LABEL: @test_ssub3(
122; CHECK-NEXT:    ret { i8, i1 } undef
123;
124  %x = call {i8, i1} @llvm.ssub.with.overflow.i8(i8 undef, i8 %V)
125  ret {i8, i1} %x
126}
127
128define {i8, i1} @test_umul1(i8 %V) {
129; CHECK-LABEL: @test_umul1(
130; CHECK-NEXT:    ret { i8, i1 } zeroinitializer
131;
132  %x = call {i8, i1} @llvm.umul.with.overflow.i8(i8 %V, i8 0)
133  ret {i8, i1} %x
134}
135
136define {i8, i1} @test_umul2(i8 %V) {
137; CHECK-LABEL: @test_umul2(
138; CHECK-NEXT:    ret { i8, i1 } zeroinitializer
139;
140  %x = call {i8, i1} @llvm.umul.with.overflow.i8(i8 %V, i8 undef)
141  ret {i8, i1} %x
142}
143
144define {i8, i1} @test_umul3(i8 %V) {
145; CHECK-LABEL: @test_umul3(
146; CHECK-NEXT:    ret { i8, i1 } zeroinitializer
147;
148  %x = call {i8, i1} @llvm.umul.with.overflow.i8(i8 0, i8 %V)
149  ret {i8, i1} %x
150}
151
152define {i8, i1} @test_umul4(i8 %V) {
153; CHECK-LABEL: @test_umul4(
154; CHECK-NEXT:    ret { i8, i1 } zeroinitializer
155;
156  %x = call {i8, i1} @llvm.umul.with.overflow.i8(i8 undef, i8 %V)
157  ret {i8, i1} %x
158}
159
160define {i8, i1} @test_smul1(i8 %V) {
161; CHECK-LABEL: @test_smul1(
162; CHECK-NEXT:    ret { i8, i1 } zeroinitializer
163;
164  %x = call {i8, i1} @llvm.smul.with.overflow.i8(i8 %V, i8 0)
165  ret {i8, i1} %x
166}
167
168define {i8, i1} @test_smul2(i8 %V) {
169; CHECK-LABEL: @test_smul2(
170; CHECK-NEXT:    ret { i8, i1 } zeroinitializer
171;
172  %x = call {i8, i1} @llvm.smul.with.overflow.i8(i8 %V, i8 undef)
173  ret {i8, i1} %x
174}
175
176define {i8, i1} @test_smul3(i8 %V) {
177; CHECK-LABEL: @test_smul3(
178; CHECK-NEXT:    ret { i8, i1 } zeroinitializer
179;
180  %x = call {i8, i1} @llvm.smul.with.overflow.i8(i8 0, i8 %V)
181  ret {i8, i1} %x
182}
183
184define {i8, i1} @test_smul4(i8 %V) {
185; CHECK-LABEL: @test_smul4(
186; CHECK-NEXT:    ret { i8, i1 } zeroinitializer
187;
188  %x = call {i8, i1} @llvm.smul.with.overflow.i8(i8 undef, i8 %V)
189  ret {i8, i1} %x
190}
191
192; Test a non-intrinsic that we know about as a library call.
193declare float @fabs(float %x)
194
195define float @test_fabs_libcall() {
196; CHECK-LABEL: @test_fabs_libcall(
197; CHECK-NEXT:    [[X:%.*]] = call float @fabs(float -4.200000e+01)
198; CHECK-NEXT:    ret float 4.200000e+01
199;
200
201  %x = call float @fabs(float -42.0)
202; This is still a real function call, so instsimplify won't nuke it -- other
203; passes have to do that.
204
205  ret float %x
206}
207
208
209declare float @llvm.fabs.f32(float) nounwind readnone
210declare float @llvm.floor.f32(float) nounwind readnone
211declare float @llvm.ceil.f32(float) nounwind readnone
212declare float @llvm.trunc.f32(float) nounwind readnone
213declare float @llvm.rint.f32(float) nounwind readnone
214declare float @llvm.nearbyint.f32(float) nounwind readnone
215declare float @llvm.canonicalize.f32(float) nounwind readnone
216
217; Test idempotent intrinsics
218define float @test_idempotence(float %a) {
219; CHECK-LABEL: @test_idempotence(
220; CHECK-NEXT:    [[A0:%.*]] = call float @llvm.fabs.f32(float [[A:%.*]])
221; CHECK-NEXT:    [[B0:%.*]] = call float @llvm.floor.f32(float [[A]])
222; CHECK-NEXT:    [[C0:%.*]] = call float @llvm.ceil.f32(float [[A]])
223; CHECK-NEXT:    [[D0:%.*]] = call float @llvm.trunc.f32(float [[A]])
224; CHECK-NEXT:    [[E0:%.*]] = call float @llvm.rint.f32(float [[A]])
225; CHECK-NEXT:    [[F0:%.*]] = call float @llvm.nearbyint.f32(float [[A]])
226; CHECK-NEXT:    [[G0:%.*]] = call float @llvm.canonicalize.f32(float [[A]])
227; CHECK-NEXT:    [[R0:%.*]] = fadd float [[A0]], [[B0]]
228; CHECK-NEXT:    [[R1:%.*]] = fadd float [[R0]], [[C0]]
229; CHECK-NEXT:    [[R2:%.*]] = fadd float [[R1]], [[D0]]
230; CHECK-NEXT:    [[R3:%.*]] = fadd float [[R2]], [[E0]]
231; CHECK-NEXT:    [[R4:%.*]] = fadd float [[R3]], [[F0]]
232; CHECK-NEXT:    [[R5:%.*]] = fadd float [[R4]], [[G0]]
233; CHECK-NEXT:    ret float [[R5]]
234;
235
236  %a0 = call float @llvm.fabs.f32(float %a)
237  %a1 = call float @llvm.fabs.f32(float %a0)
238
239  %b0 = call float @llvm.floor.f32(float %a)
240  %b1 = call float @llvm.floor.f32(float %b0)
241
242  %c0 = call float @llvm.ceil.f32(float %a)
243  %c1 = call float @llvm.ceil.f32(float %c0)
244
245  %d0 = call float @llvm.trunc.f32(float %a)
246  %d1 = call float @llvm.trunc.f32(float %d0)
247
248  %e0 = call float @llvm.rint.f32(float %a)
249  %e1 = call float @llvm.rint.f32(float %e0)
250
251  %f0 = call float @llvm.nearbyint.f32(float %a)
252  %f1 = call float @llvm.nearbyint.f32(float %f0)
253
254  %g0 = call float @llvm.canonicalize.f32(float %a)
255  %g1 = call float @llvm.canonicalize.f32(float %g0)
256
257  %r0 = fadd float %a1, %b1
258  %r1 = fadd float %r0, %c1
259  %r2 = fadd float %r1, %d1
260  %r3 = fadd float %r2, %e1
261  %r4 = fadd float %r3, %f1
262  %r5 = fadd float %r4, %g1
263
264  ret float %r5
265}
266
267define i8* @operator_new() {
268; CHECK-LABEL: @operator_new(
269; CHECK-NEXT:  entry:
270; CHECK-NEXT:    [[CALL:%.*]] = tail call noalias i8* @_Znwm(i64 8)
271; CHECK-NEXT:    br i1 false, label [[CAST_END:%.*]], label [[CAST_NOTNULL:%.*]]
272; CHECK:       cast.notnull:
273; CHECK-NEXT:    [[ADD_PTR:%.*]] = getelementptr inbounds i8, i8* [[CALL]], i64 4
274; CHECK-NEXT:    br label [[CAST_END]]
275; CHECK:       cast.end:
276; CHECK-NEXT:    [[CAST_RESULT:%.*]] = phi i8* [ [[ADD_PTR]], [[CAST_NOTNULL]] ], [ null, [[ENTRY:%.*]] ]
277; CHECK-NEXT:    ret i8* [[CAST_RESULT]]
278;
279entry:
280  %call = tail call noalias i8* @_Znwm(i64 8)
281  %cmp = icmp eq i8* %call, null
282  br i1 %cmp, label %cast.end, label %cast.notnull
283
284cast.notnull:                                     ; preds = %entry
285  %add.ptr = getelementptr inbounds i8, i8* %call, i64 4
286  br label %cast.end
287
288cast.end:                                         ; preds = %cast.notnull, %entry
289  %cast.result = phi i8* [ %add.ptr, %cast.notnull ], [ null, %entry ]
290  ret i8* %cast.result
291
292}
293
294declare nonnull noalias i8* @_Znwm(i64)
295
296%"struct.std::nothrow_t" = type { i8 }
297@_ZSt7nothrow = external global %"struct.std::nothrow_t"
298
299define i8* @operator_new_nothrow_t() {
300; CHECK-LABEL: @operator_new_nothrow_t(
301; CHECK-NEXT:  entry:
302; CHECK-NEXT:    [[CALL:%.*]] = tail call noalias i8* @_ZnamRKSt9nothrow_t(i64 8, %"struct.std::nothrow_t"* @_ZSt7nothrow)
303; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8* [[CALL]], null
304; CHECK-NEXT:    br i1 [[CMP]], label [[CAST_END:%.*]], label [[CAST_NOTNULL:%.*]]
305; CHECK:       cast.notnull:
306; CHECK-NEXT:    [[ADD_PTR:%.*]] = getelementptr inbounds i8, i8* [[CALL]], i64 4
307; CHECK-NEXT:    br label [[CAST_END]]
308; CHECK:       cast.end:
309; CHECK-NEXT:    [[CAST_RESULT:%.*]] = phi i8* [ [[ADD_PTR]], [[CAST_NOTNULL]] ], [ null, [[ENTRY:%.*]] ]
310; CHECK-NEXT:    ret i8* [[CAST_RESULT]]
311;
312entry:
313  %call = tail call noalias i8* @_ZnamRKSt9nothrow_t(i64 8, %"struct.std::nothrow_t"* @_ZSt7nothrow)
314  %cmp = icmp eq i8* %call, null
315  br i1 %cmp, label %cast.end, label %cast.notnull
316
317cast.notnull:                                     ; preds = %entry
318  %add.ptr = getelementptr inbounds i8, i8* %call, i64 4
319  br label %cast.end
320
321cast.end:                                         ; preds = %cast.notnull, %entry
322  %cast.result = phi i8* [ %add.ptr, %cast.notnull ], [ null, %entry ]
323  ret i8* %cast.result
324
325}
326
327declare i8* @_ZnamRKSt9nothrow_t(i64, %"struct.std::nothrow_t"*) nounwind
328
329define i8* @malloc_can_return_null() {
330; CHECK-LABEL: @malloc_can_return_null(
331; CHECK-NEXT:  entry:
332; CHECK-NEXT:    [[CALL:%.*]] = tail call noalias i8* @malloc(i64 8)
333; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8* [[CALL]], null
334; CHECK-NEXT:    br i1 [[CMP]], label [[CAST_END:%.*]], label [[CAST_NOTNULL:%.*]]
335; CHECK:       cast.notnull:
336; CHECK-NEXT:    [[ADD_PTR:%.*]] = getelementptr inbounds i8, i8* [[CALL]], i64 4
337; CHECK-NEXT:    br label [[CAST_END]]
338; CHECK:       cast.end:
339; CHECK-NEXT:    [[CAST_RESULT:%.*]] = phi i8* [ [[ADD_PTR]], [[CAST_NOTNULL]] ], [ null, [[ENTRY:%.*]] ]
340; CHECK-NEXT:    ret i8* [[CAST_RESULT]]
341;
342entry:
343  %call = tail call noalias i8* @malloc(i64 8)
344  %cmp = icmp eq i8* %call, null
345  br i1 %cmp, label %cast.end, label %cast.notnull
346
347cast.notnull:                                     ; preds = %entry
348  %add.ptr = getelementptr inbounds i8, i8* %call, i64 4
349  br label %cast.end
350
351cast.end:                                         ; preds = %cast.notnull, %entry
352  %cast.result = phi i8* [ %add.ptr, %cast.notnull ], [ null, %entry ]
353  ret i8* %cast.result
354
355}
356
357define i32 @call_null() {
358; CHECK-LABEL: @call_null(
359; CHECK-NEXT:  entry:
360; CHECK-NEXT:    [[CALL:%.*]] = call i32 null()
361; CHECK-NEXT:    ret i32 undef
362;
363entry:
364  %call = call i32 null()
365  ret i32 %call
366}
367
368define i32 @call_undef() {
369; CHECK-LABEL: @call_undef(
370; CHECK-NEXT:  entry:
371; CHECK-NEXT:    [[CALL:%.*]] = call i32 undef()
372; CHECK-NEXT:    ret i32 undef
373;
374entry:
375  %call = call i32 undef()
376  ret i32 %call
377}
378
379@GV = private constant [8 x i32] [i32 42, i32 43, i32 44, i32 45, i32 46, i32 47, i32 48, i32 49]
380
381define <8 x i32> @partial_masked_load() {
382; CHECK-LABEL: @partial_masked_load(
383; CHECK-NEXT:    ret <8 x i32> <i32 undef, i32 undef, i32 42, i32 43, i32 44, i32 45, i32 46, i32 47>
384;
385  %masked.load = call <8 x i32> @llvm.masked.load.v8i32.p0v8i32(<8 x i32>* bitcast (i32* getelementptr ([8 x i32], [8 x i32]* @GV, i64 0, i64 -2) to <8 x i32>*), i32 4, <8 x i1> <i1 false, i1 false, i1 true, i1 true, i1 true, i1 true, i1 true, i1 true>, <8 x i32> undef)
386  ret <8 x i32> %masked.load
387}
388
389define <8 x i32> @masked_load_undef_mask(<8 x i32>* %V) {
390; CHECK-LABEL: @masked_load_undef_mask(
391; CHECK-NEXT:    ret <8 x i32> <i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0>
392;
393  %masked.load = call <8 x i32> @llvm.masked.load.v8i32.p0v8i32(<8 x i32>* %V, i32 4, <8 x i1> undef, <8 x i32> <i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0>)
394  ret <8 x i32> %masked.load
395}
396
397declare noalias i8* @malloc(i64)
398
399declare <8 x i32> @llvm.masked.load.v8i32.p0v8i32(<8 x i32>*, i32, <8 x i1>, <8 x i32>)
400
401declare double @llvm.powi.f64(double, i32)
402declare <2 x double> @llvm.powi.v2f64(<2 x double>, i32)
403
404define double @constant_fold_powi() {
405; CHECK-LABEL: @constant_fold_powi(
406; CHECK-NEXT:    ret double 9.000000e+00
407;
408  %t0 = call double @llvm.powi.f64(double 3.00000e+00, i32 2)
409  ret double %t0
410}
411
412define <2 x double> @constant_fold_powi_vec() {
413; CHECK-LABEL: @constant_fold_powi_vec(
414; CHECK-NEXT:    ret <2 x double> <double 9.000000e+00, double 2.500000e+01>
415;
416  %t0 = call <2 x double> @llvm.powi.v2f64(<2 x double> <double 3.00000e+00, double 5.00000e+00>, i32 2)
417  ret <2 x double> %t0
418}
419
420declare i8 @llvm.fshl.i8(i8, i8, i8)
421declare i9 @llvm.fshr.i9(i9, i9, i9)
422declare <2 x i7> @llvm.fshl.v2i7(<2 x i7>, <2 x i7>, <2 x i7>)
423declare <2 x i8> @llvm.fshr.v2i8(<2 x i8>, <2 x i8>, <2 x i8>)
424
425define i8 @fshl_no_shift(i8 %x, i8 %y) {
426; CHECK-LABEL: @fshl_no_shift(
427; CHECK-NEXT:    ret i8 [[X:%.*]]
428;
429  %z = call i8 @llvm.fshl.i8(i8 %x, i8 %y, i8 0)
430  ret i8 %z
431}
432
433define i9 @fshr_no_shift(i9 %x, i9 %y) {
434; CHECK-LABEL: @fshr_no_shift(
435; CHECK-NEXT:    ret i9 [[Y:%.*]]
436;
437  %z = call i9 @llvm.fshr.i9(i9 %x, i9 %y, i9 0)
438  ret i9 %z
439}
440
441define i8 @fshl_no_shift_modulo_bitwidth(i8 %x, i8 %y) {
442; CHECK-LABEL: @fshl_no_shift_modulo_bitwidth(
443; CHECK-NEXT:    ret i8 [[X:%.*]]
444;
445  %z = call i8 @llvm.fshl.i8(i8 %x, i8 %y, i8 40)
446  ret i8 %z
447}
448
449define i9 @fshr_no_shift_modulo_bitwidth(i9 %x, i9 %y) {
450; CHECK-LABEL: @fshr_no_shift_modulo_bitwidth(
451; CHECK-NEXT:    ret i9 [[Y:%.*]]
452;
453  %z = call i9 @llvm.fshr.i9(i9 %x, i9 %y, i9 189)
454  ret i9 %z
455}
456
457define <2 x i7> @fshl_no_shift_modulo_bitwidth_splat(<2 x i7> %x, <2 x i7> %y) {
458; CHECK-LABEL: @fshl_no_shift_modulo_bitwidth_splat(
459; CHECK-NEXT:    ret <2 x i7> [[X:%.*]]
460;
461  %z = call <2 x i7> @llvm.fshl.v2i7(<2 x i7> %x, <2 x i7> %y, <2 x i7> <i7 21, i7 21>)
462  ret <2 x i7> %z
463}
464
465define <2 x i8> @fshr_no_shift_modulo_bitwidth_splat(<2 x i8> %x, <2 x i8> %y) {
466; CHECK-LABEL: @fshr_no_shift_modulo_bitwidth_splat(
467; CHECK-NEXT:    ret <2 x i8> [[Y:%.*]]
468;
469  %z = call <2 x i8> @llvm.fshr.v2i8(<2 x i8> %x, <2 x i8> %y, <2 x i8> <i8 72, i8 72>)
470  ret <2 x i8> %z
471}
472
473; When the shift amount is 0, fshl returns its 1st parameter (x), so the guard is not needed.
474
475define i8 @fshl_zero_shift_guard(i8 %x, i8 %y, i8 %sh) {
476; CHECK-LABEL: @fshl_zero_shift_guard(
477; CHECK-NEXT:    [[F:%.*]] = call i8 @llvm.fshl.i8(i8 [[X:%.*]], i8 [[Y:%.*]], i8 [[SH:%.*]])
478; CHECK-NEXT:    ret i8 [[F]]
479;
480  %c = icmp eq i8 %sh, 0
481  %f = call i8 @llvm.fshl.i8(i8 %x, i8 %y, i8 %sh)
482  %s = select i1 %c, i8 %x, i8 %f
483  ret i8 %s
484}
485
486; When the shift amount is 0, fshl returns its 1st parameter (x), so the guard is not needed.
487
488define i8 @fshl_zero_shift_guard_swapped(i8 %x, i8 %y, i8 %sh) {
489; CHECK-LABEL: @fshl_zero_shift_guard_swapped(
490; CHECK-NEXT:    [[F:%.*]] = call i8 @llvm.fshl.i8(i8 [[X:%.*]], i8 [[Y:%.*]], i8 [[SH:%.*]])
491; CHECK-NEXT:    ret i8 [[F]]
492;
493  %c = icmp ne i8 %sh, 0
494  %f = call i8 @llvm.fshl.i8(i8 %x, i8 %y, i8 %sh)
495  %s = select i1 %c, i8 %f, i8 %x
496  ret i8 %s
497}
498
499; When the shift amount is 0, fshl returns its 1st parameter (x), so everything is deleted.
500
501define i8 @fshl_zero_shift_guard_inverted(i8 %x, i8 %y, i8 %sh) {
502; CHECK-LABEL: @fshl_zero_shift_guard_inverted(
503; CHECK-NEXT:    ret i8 [[X:%.*]]
504;
505  %c = icmp eq i8 %sh, 0
506  %f = call i8 @llvm.fshl.i8(i8 %x, i8 %y, i8 %sh)
507  %s = select i1 %c, i8 %f, i8 %x
508  ret i8 %s
509}
510
511; When the shift amount is 0, fshl returns its 1st parameter (x), so everything is deleted.
512
513define i8 @fshl_zero_shift_guard_inverted_swapped(i8 %x, i8 %y, i8 %sh) {
514; CHECK-LABEL: @fshl_zero_shift_guard_inverted_swapped(
515; CHECK-NEXT:    ret i8 [[X:%.*]]
516;
517  %c = icmp ne i8 %sh, 0
518  %f = call i8 @llvm.fshl.i8(i8 %x, i8 %y, i8 %sh)
519  %s = select i1 %c, i8 %x, i8 %f
520  ret i8 %s
521}
522
523; When the shift amount is 0, fshr returns its 2nd parameter (y), so the guard is not needed.
524
525define i9 @fshr_zero_shift_guard(i9 %x, i9 %y, i9 %sh) {
526; CHECK-LABEL: @fshr_zero_shift_guard(
527; CHECK-NEXT:    [[F:%.*]] = call i9 @llvm.fshr.i9(i9 [[X:%.*]], i9 [[Y:%.*]], i9 [[SH:%.*]])
528; CHECK-NEXT:    ret i9 [[F]]
529;
530  %c = icmp eq i9 %sh, 0
531  %f = call i9 @llvm.fshr.i9(i9 %x, i9 %y, i9 %sh)
532  %s = select i1 %c, i9 %y, i9 %f
533  ret i9 %s
534}
535
536; When the shift amount is 0, fshr returns its 2nd parameter (y), so the guard is not needed.
537
538define i9 @fshr_zero_shift_guard_swapped(i9 %x, i9 %y, i9 %sh) {
539; CHECK-LABEL: @fshr_zero_shift_guard_swapped(
540; CHECK-NEXT:    [[F:%.*]] = call i9 @llvm.fshr.i9(i9 [[X:%.*]], i9 [[Y:%.*]], i9 [[SH:%.*]])
541; CHECK-NEXT:    ret i9 [[F]]
542;
543  %c = icmp ne i9 %sh, 0
544  %f = call i9 @llvm.fshr.i9(i9 %x, i9 %y, i9 %sh)
545  %s = select i1 %c, i9 %f, i9 %y
546  ret i9 %s
547}
548
549; When the shift amount is 0, fshr returns its 2nd parameter (y), so everything is deleted.
550
551define i9 @fshr_zero_shift_guard_inverted(i9 %x, i9 %y, i9 %sh) {
552; CHECK-LABEL: @fshr_zero_shift_guard_inverted(
553; CHECK-NEXT:    ret i9 [[Y:%.*]]
554;
555  %c = icmp eq i9 %sh, 0
556  %f = call i9 @llvm.fshr.i9(i9 %x, i9 %y, i9 %sh)
557  %s = select i1 %c, i9 %f, i9 %y
558  ret i9 %s
559}
560
561; When the shift amount is 0, fshr returns its 2nd parameter (y), so everything is deleted.
562
563define i9 @fshr_zero_shift_guard_inverted_swapped(i9 %x, i9 %y, i9 %sh) {
564; CHECK-LABEL: @fshr_zero_shift_guard_inverted_swapped(
565; CHECK-NEXT:    ret i9 [[Y:%.*]]
566;
567  %c = icmp ne i9 %sh, 0
568  %f = call i9 @llvm.fshr.i9(i9 %x, i9 %y, i9 %sh)
569  %s = select i1 %c, i9 %y, i9 %f
570  ret i9 %s
571}
572
573; Negative test - make sure we're matching the correct parameter of fshl.
574
575define i8 @fshl_zero_shift_guard_wrong_select_op(i8 %x, i8 %y, i8 %sh) {
576; CHECK-LABEL: @fshl_zero_shift_guard_wrong_select_op(
577; CHECK-NEXT:    [[C:%.*]] = icmp eq i8 [[SH:%.*]], 0
578; CHECK-NEXT:    [[F:%.*]] = call i8 @llvm.fshl.i8(i8 [[X:%.*]], i8 [[Y:%.*]], i8 [[SH]])
579; CHECK-NEXT:    [[S:%.*]] = select i1 [[C]], i8 [[Y]], i8 [[F]]
580; CHECK-NEXT:    ret i8 [[S]]
581;
582  %c = icmp eq i8 %sh, 0
583  %f = call i8 @llvm.fshl.i8(i8 %x, i8 %y, i8 %sh)
584  %s = select i1 %c, i8 %y, i8 %f
585  ret i8 %s
586}
587
588; Vector types work too.
589
590define <2 x i8> @fshr_zero_shift_guard_splat(<2 x i8> %x, <2 x i8> %y, <2 x i8> %sh) {
591; CHECK-LABEL: @fshr_zero_shift_guard_splat(
592; CHECK-NEXT:    [[F:%.*]] = call <2 x i8> @llvm.fshr.v2i8(<2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]], <2 x i8> [[SH:%.*]])
593; CHECK-NEXT:    ret <2 x i8> [[F]]
594;
595  %c = icmp eq <2 x i8> %sh, zeroinitializer
596  %f = call <2 x i8> @llvm.fshr.v2i8(<2 x i8> %x, <2 x i8> %y, <2 x i8> %sh)
597  %s = select <2 x i1> %c, <2 x i8> %y, <2 x i8> %f
598  ret <2 x i8> %s
599}
600
601; If first two operands of funnel shift are undef, the result is undef
602
603define i8 @fshl_ops_undef(i8 %shamt) {
604; CHECK-LABEL: @fshl_ops_undef(
605; CHECK-NEXT:    ret i8 undef
606;
607  %r = call i8 @llvm.fshl.i8(i8 undef, i8 undef, i8 %shamt)
608  ret i8 %r
609}
610
611define i9 @fshr_ops_undef(i9 %shamt) {
612; CHECK-LABEL: @fshr_ops_undef(
613; CHECK-NEXT:    ret i9 undef
614;
615  %r = call i9 @llvm.fshr.i9(i9 undef, i9 undef, i9 %shamt)
616  ret i9 %r
617}
618
619; If shift amount is undef, treat it as zero, returning operand 0 or 1
620
621define i8 @fshl_shift_undef(i8 %x, i8 %y) {
622; CHECK-LABEL: @fshl_shift_undef(
623; CHECK-NEXT:    ret i8 [[X:%.*]]
624;
625  %r = call i8 @llvm.fshl.i8(i8 %x, i8 %y, i8 undef)
626  ret i8 %r
627}
628
629define i9 @fshr_shift_undef(i9 %x, i9 %y) {
630; CHECK-LABEL: @fshr_shift_undef(
631; CHECK-NEXT:    ret i9 [[Y:%.*]]
632;
633  %r = call i9 @llvm.fshr.i9(i9 %x, i9 %y, i9 undef)
634  ret i9 %r
635}
636
637