1 ; RUN: opt < %s -instcombine -S | FileCheck %s
3 ; Fold zeroing of inactive lanes into the load's passthrough parameter.
4 define <4 x float> @masked_load_and_zero_inactive_1(<4 x float>* %ptr, <4 x i1> %mask) {
5 ; CHECK-LABEL: @masked_load_and_zero_inactive_1(
6 ; CHECK: %load = call <4 x float> @llvm.masked.load.v4f32.p0v4f32(<4 x float>* %ptr, i32 4, <4 x i1> %mask, <4 x float> zeroinitializer)
7 ; CHECK-NEXT: ret <4 x float> %load
8 %load = call <4 x float> @llvm.masked.load.v4f32.p0v4f32(<4 x float>* %ptr, i32 4, <4 x i1> %mask, <4 x float> undef)
9 %masked = select <4 x i1> %mask, <4 x float> %load, <4 x float> zeroinitializer
10 ret <4 x float> %masked
13 ; As above but reuse the load's existing passthrough.
14 define <4 x i32> @masked_load_and_zero_inactive_2(<4 x i32>* %ptr, <4 x i1> %mask) {
15 ; CHECK-LABEL: @masked_load_and_zero_inactive_2(
16 ; CHECK: %load = call <4 x i32> @llvm.masked.load.v4i32.p0v4i32(<4 x i32>* %ptr, i32 4, <4 x i1> %mask, <4 x i32> zeroinitializer)
17 ; CHECK-NEXT: ret <4 x i32> %load
18 %load = call <4 x i32> @llvm.masked.load.v4i32.p0v4i32(<4 x i32>* %ptr, i32 4, <4 x i1> %mask, <4 x i32> zeroinitializer)
19 %masked = select <4 x i1> %mask, <4 x i32> %load, <4 x i32> zeroinitializer
23 ; No transform when the load's passthrough cannot be reused or altered.
24 define <4 x i32> @masked_load_and_zero_inactive_3(<4 x i32>* %ptr, <4 x i1> %mask, <4 x i32> %passthrough) {
25 ; CHECK-LABEL: @masked_load_and_zero_inactive_3(
26 ; CHECK: %load = call <4 x i32> @llvm.masked.load.v4i32.p0v4i32(<4 x i32>* %ptr, i32 4, <4 x i1> %mask, <4 x i32> %passthrough)
27 ; CHECK-NEXT: %masked = select <4 x i1> %mask, <4 x i32> %load, <4 x i32> zeroinitializer
28 ; CHECK-NEXT: ret <4 x i32> %masked
29 %load = call <4 x i32> @llvm.masked.load.v4i32.p0v4i32(<4 x i32>* %ptr, i32 4, <4 x i1> %mask, <4 x i32> %passthrough)
30 %masked = select <4 x i1> %mask, <4 x i32> %load, <4 x i32> zeroinitializer
34 ; Remove redundant select when its mask doesn't overlap with the load mask.
35 define <4 x i32> @masked_load_and_zero_inactive_4(<4 x i32>* %ptr, <4 x i1> %inv_mask) {
36 ; CHECK-LABEL: @masked_load_and_zero_inactive_4(
37 ; CHECK: %mask = xor <4 x i1> %inv_mask, <i1 true, i1 true, i1 true, i1 true>
38 ; CHECK-NEXT: %load = call <4 x i32> @llvm.masked.load.v4i32.p0v4i32(<4 x i32>* %ptr, i32 4, <4 x i1> %mask, <4 x i32> zeroinitializer)
39 ; CHECK-NEXT: ret <4 x i32> %load
40 %mask = xor <4 x i1> %inv_mask, <i1 true, i1 true, i1 true, i1 true>
41 %load = call <4 x i32> @llvm.masked.load.v4i32.p0v4i32(<4 x i32>* %ptr, i32 4, <4 x i1> %mask, <4 x i32> undef)
42 %masked = select <4 x i1> %inv_mask, <4 x i32> zeroinitializer, <4 x i32> %load
46 ; As above but reuse the load's existing passthrough.
47 define <4 x i32> @masked_load_and_zero_inactive_5(<4 x i32>* %ptr, <4 x i1> %inv_mask) {
48 ; CHECK-LABEL: @masked_load_and_zero_inactive_5(
49 ; CHECK: %mask = xor <4 x i1> %inv_mask, <i1 true, i1 true, i1 true, i1 true>
50 ; CHECK-NEXT: %load = call <4 x i32> @llvm.masked.load.v4i32.p0v4i32(<4 x i32>* %ptr, i32 4, <4 x i1> %mask, <4 x i32> zeroinitializer)
51 ; CHECK-NEXT: ret <4 x i32> %load
52 %mask = xor <4 x i1> %inv_mask, <i1 true, i1 true, i1 true, i1 true>
53 %load = call <4 x i32> @llvm.masked.load.v4i32.p0v4i32(<4 x i32>* %ptr, i32 4, <4 x i1> %mask, <4 x i32> zeroinitializer)
54 %masked = select <4 x i1> %inv_mask, <4 x i32> zeroinitializer, <4 x i32> %load
58 ; No transform when the load's passthrough cannot be reused or altered.
59 define <4 x i32> @masked_load_and_zero_inactive_6(<4 x i32>* %ptr, <4 x i1> %inv_mask, <4 x i32> %passthrough) {
60 ; CHECK-LABEL: @masked_load_and_zero_inactive_6(
61 ; CHECK: %mask = xor <4 x i1> %inv_mask, <i1 true, i1 true, i1 true, i1 true>
62 ; CHECK-NEXT: %load = call <4 x i32> @llvm.masked.load.v4i32.p0v4i32(<4 x i32>* %ptr, i32 4, <4 x i1> %mask, <4 x i32> %passthrough)
63 ; CHECK-NEXT: %masked = select <4 x i1> %inv_mask, <4 x i32> zeroinitializer, <4 x i32> %load
64 ; CHECK-NEXT: ret <4 x i32> %masked
65 %mask = xor <4 x i1> %inv_mask, <i1 true, i1 true, i1 true, i1 true>
66 %load = call <4 x i32> @llvm.masked.load.v4i32.p0v4i32(<4 x i32>* %ptr, i32 4, <4 x i1> %mask, <4 x i32> %passthrough)
67 %masked = select <4 x i1> %inv_mask, <4 x i32> zeroinitializer, <4 x i32> %load
71 ; No transform when select and load masks have no relation.
72 define <4 x i32> @masked_load_and_zero_inactive_7(<4 x i32>* %ptr, <4 x i1> %mask1, <4 x i1> %mask2) {
73 ; CHECK-LABEL: @masked_load_and_zero_inactive_7(
74 ; CHECK: %load = call <4 x i32> @llvm.masked.load.v4i32.p0v4i32(<4 x i32>* %ptr, i32 4, <4 x i1> %mask1, <4 x i32> zeroinitializer)
75 ; CHECK-NEXT: %masked = select <4 x i1> %mask2, <4 x i32> zeroinitializer, <4 x i32> %load
76 ; CHECK-NEXT: ret <4 x i32> %masked
77 %load = call <4 x i32> @llvm.masked.load.v4i32.p0v4i32(<4 x i32>* %ptr, i32 4, <4 x i1> %mask1, <4 x i32> zeroinitializer)
78 %masked = select <4 x i1> %mask2, <4 x i32> zeroinitializer, <4 x i32> %load
82 ; A more complex case where we can prove the select mask is a subset of the
83 ; load's inactive lanes and thus the load's passthrough takes effect.
84 define <4 x float> @masked_load_and_zero_inactive_8(<4 x float>* %ptr, <4 x i1> %inv_mask, <4 x i1> %cond) {
85 ; CHECK-LABEL: @masked_load_and_zero_inactive_8(
86 ; CHECK: %mask = xor <4 x i1> %inv_mask, <i1 true, i1 true, i1 true, i1 true>
87 ; CHECK-NEXT: %pg = and <4 x i1> %mask, %cond
88 ; CHECK-NEXT: %load = call <4 x float> @llvm.masked.load.v4f32.p0v4f32(<4 x float>* %ptr, i32 4, <4 x i1> %pg, <4 x float> zeroinitializer)
89 ; CHECK-NEXT: ret <4 x float> %load
90 %mask = xor <4 x i1> %inv_mask, <i1 true, i1 true, i1 true, i1 true>
91 %pg = and <4 x i1> %mask, %cond
92 %load = call <4 x float> @llvm.masked.load.v4f32.p0v4f32(<4 x float>* %ptr, i32 4, <4 x i1> %pg, <4 x float> undef)
93 %masked = select <4 x i1> %inv_mask, <4 x float> zeroinitializer, <4 x float> %load
94 ret <4 x float> %masked
97 define <8 x float> @masked_load_and_scalar_select_cond(<8 x float>* %ptr, <8 x i1> %mask, i1 %cond) {
98 ; CHECK-LABEL: @masked_load_and_scalar_select_cond(
100 ; CHECK-NEXT: [[TMP0:%.*]] = call <8 x float> @llvm.masked.load.v8f32.p0v8f32(<8 x float>* [[PTR:%.*]], i32 32, <8 x i1> [[MASK:%.*]], <8 x float> undef)
101 ; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[COND:%.*]], <8 x float> zeroinitializer, <8 x float> [[TMP0]]
102 ; CHECK-NEXT: ret <8 x float> [[TMP1]]
104 %0 = call <8 x float> @llvm.masked.load.v8f32.p0v8f32(<8 x float>* %ptr, i32 32, <8 x i1> %mask, <8 x float> undef)
105 %1 = select i1 %cond, <8 x float> zeroinitializer, <8 x float> %0
109 declare <8 x float> @llvm.masked.load.v8f32.p0v8f32(<8 x float>*, i32 immarg, <8 x i1>, <8 x float>)
110 declare <4 x i32> @llvm.masked.load.v4i32.p0v4i32(<4 x i32>*, i32 immarg, <4 x i1>, <4 x i32>)
111 declare <4 x float> @llvm.masked.load.v4f32.p0v4f32(<4 x float>*, i32 immarg, <4 x i1>, <4 x float>)