1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt %s -S -riscv-gather-scatter-lowering -mtriple=riscv64 -mattr=+m,+v | FileCheck %s --check-prefixes=CHECK
4 %struct.foo = type { i32, i32, i32, i32 }
6 declare <vscale x 1 x i64> @llvm.experimental.stepvector.nxv1i64()
8 define <vscale x 1 x i64> @gather(ptr %a, i32 %len) {
9 ; CHECK-LABEL: @gather(
10 ; CHECK-NEXT: vector.ph:
11 ; CHECK-NEXT: [[WIDE_TRIP_COUNT:%.*]] = zext i32 [[LEN:%.*]] to i64
12 ; CHECK-NEXT: [[TMP0:%.*]] = tail call i64 @llvm.vscale.i64()
13 ; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
15 ; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH:%.*]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
16 ; CHECK-NEXT: [[VEC_IND_SCALAR:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[VEC_IND_NEXT_SCALAR:%.*]], [[VECTOR_BODY]] ]
17 ; CHECK-NEXT: [[ACCUM:%.*]] = phi <vscale x 1 x i64> [ zeroinitializer, [[VECTOR_PH]] ], [ [[ACCUM_NEXT:%.*]], [[VECTOR_BODY]] ]
18 ; CHECK-NEXT: [[TMP1:%.*]] = getelementptr [[STRUCT_FOO:%.*]], ptr [[A:%.*]], i64 [[VEC_IND_SCALAR]], i32 3
19 ; CHECK-NEXT: [[GATHER:%.*]] = call <vscale x 1 x i64> @llvm.riscv.masked.strided.load.nxv1i64.p0.i64(<vscale x 1 x i64> undef, ptr [[TMP1]], i64 16, <vscale x 1 x i1> shufflevector (<vscale x 1 x i1> insertelement (<vscale x 1 x i1> poison, i1 true, i32 0), <vscale x 1 x i1> poison, <vscale x 1 x i32> zeroinitializer))
20 ; CHECK-NEXT: [[ACCUM_NEXT]] = add <vscale x 1 x i64> [[ACCUM]], [[GATHER]]
21 ; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP0]]
22 ; CHECK-NEXT: [[VEC_IND_NEXT_SCALAR]] = add i64 [[VEC_IND_SCALAR]], [[TMP0]]
23 ; CHECK-NEXT: [[TMP2:%.*]] = icmp ne i64 [[INDEX_NEXT]], [[WIDE_TRIP_COUNT]]
24 ; CHECK-NEXT: br i1 [[TMP2]], label [[FOR_COND_CLEANUP:%.*]], label [[VECTOR_BODY]]
25 ; CHECK: for.cond.cleanup:
26 ; CHECK-NEXT: ret <vscale x 1 x i64> [[ACCUM_NEXT]]
29 %wide.trip.count = zext i32 %len to i64
30 %0 = tail call i64 @llvm.vscale.i64()
31 %1 = tail call <vscale x 1 x i64> @llvm.experimental.stepvector.nxv1i64()
32 %.splatinsert = insertelement <vscale x 1 x i64> poison, i64 %0, i64 0
33 %.splat = shufflevector <vscale x 1 x i64> %.splatinsert, <vscale x 1 x i64> poison, <vscale x 1 x i32> zeroinitializer
36 vector.body: ; preds = %vector.body, %vector.ph
37 %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
38 %vec.ind = phi <vscale x 1 x i64> [ %1, %vector.ph ], [ %vec.ind.next, %vector.body ]
39 %accum = phi <vscale x 1 x i64> [ zeroinitializer, %vector.ph ], [ %accum.next, %vector.body ]
40 %2 = getelementptr inbounds %struct.foo, ptr %a, <vscale x 1 x i64> %vec.ind, i32 3
41 %gather = call <vscale x 1 x i64> @llvm.masked.gather.nxv1i64.nxv1p0(<vscale x 1 x ptr> %2, i32 8, <vscale x 1 x i1> shufflevector (<vscale x 1 x i1> insertelement (<vscale x 1 x i1> poison, i1 true, i32 0), <vscale x 1 x i1> poison, <vscale x 1 x i32> zeroinitializer), <vscale x 1 x i64> undef)
42 %accum.next = add <vscale x 1 x i64> %accum, %gather
43 %index.next = add nuw i64 %index, %0
44 %vec.ind.next = add <vscale x 1 x i64> %vec.ind, %.splat
45 %3 = icmp ne i64 %index.next, %wide.trip.count
46 br i1 %3, label %for.cond.cleanup, label %vector.body
48 for.cond.cleanup: ; preds = %vector.body
49 ret <vscale x 1 x i64> %accum.next
52 define void @scatter(ptr %a, i32 %len) {
53 ; CHECK-LABEL: @scatter(
54 ; CHECK-NEXT: vector.ph:
55 ; CHECK-NEXT: [[WIDE_TRIP_COUNT:%.*]] = zext i32 [[LEN:%.*]] to i64
56 ; CHECK-NEXT: [[TMP0:%.*]] = tail call i64 @llvm.vscale.i64()
57 ; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
59 ; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH:%.*]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
60 ; CHECK-NEXT: [[VEC_IND_SCALAR:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[VEC_IND_NEXT_SCALAR:%.*]], [[VECTOR_BODY]] ]
61 ; CHECK-NEXT: [[TMP1:%.*]] = getelementptr [[STRUCT_FOO:%.*]], ptr [[A:%.*]], i64 [[VEC_IND_SCALAR]], i32 3
62 ; CHECK-NEXT: call void @llvm.riscv.masked.strided.store.nxv1i64.p0.i64(<vscale x 1 x i64> zeroinitializer, ptr [[TMP1]], i64 16, <vscale x 1 x i1> shufflevector (<vscale x 1 x i1> insertelement (<vscale x 1 x i1> poison, i1 true, i32 0), <vscale x 1 x i1> poison, <vscale x 1 x i32> zeroinitializer))
63 ; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP0]]
64 ; CHECK-NEXT: [[VEC_IND_NEXT_SCALAR]] = add i64 [[VEC_IND_SCALAR]], [[TMP0]]
65 ; CHECK-NEXT: [[TMP2:%.*]] = icmp ne i64 [[INDEX_NEXT]], [[WIDE_TRIP_COUNT]]
66 ; CHECK-NEXT: br i1 [[TMP2]], label [[FOR_COND_CLEANUP:%.*]], label [[VECTOR_BODY]]
67 ; CHECK: for.cond.cleanup:
68 ; CHECK-NEXT: ret void
71 %wide.trip.count = zext i32 %len to i64
72 %0 = tail call i64 @llvm.vscale.i64()
73 %1 = tail call <vscale x 1 x i64> @llvm.experimental.stepvector.nxv1i64()
74 %.splatinsert = insertelement <vscale x 1 x i64> poison, i64 %0, i64 0
75 %.splat = shufflevector <vscale x 1 x i64> %.splatinsert, <vscale x 1 x i64> poison, <vscale x 1 x i32> zeroinitializer
78 vector.body: ; preds = %vector.body, %vector.ph
79 %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
80 %vec.ind = phi <vscale x 1 x i64> [ %1, %vector.ph ], [ %vec.ind.next, %vector.body ]
81 %2 = getelementptr inbounds %struct.foo, ptr %a, <vscale x 1 x i64> %vec.ind, i32 3
82 tail call void @llvm.masked.scatter.nxv1i64.nxv1p0(<vscale x 1 x i64> zeroinitializer, <vscale x 1 x ptr> %2, i32 8, <vscale x 1 x i1> shufflevector (<vscale x 1 x i1> insertelement (<vscale x 1 x i1> poison, i1 true, i32 0), <vscale x 1 x i1> poison, <vscale x 1 x i32> zeroinitializer))
83 %index.next = add nuw i64 %index, %0
84 %vec.ind.next = add <vscale x 1 x i64> %vec.ind, %.splat
85 %3 = icmp ne i64 %index.next, %wide.trip.count
86 br i1 %3, label %for.cond.cleanup, label %vector.body
88 for.cond.cleanup: ; preds = %vector.body
92 define <vscale x 1 x i64> @gather_loopless(ptr %p, i64 %stride) {
93 ; CHECK-LABEL: @gather_loopless(
94 ; CHECK-NEXT: [[TMP1:%.*]] = mul i64 [[STRIDE:%.*]], 4
95 ; CHECK-NEXT: [[X:%.*]] = call <vscale x 1 x i64> @llvm.riscv.masked.strided.load.nxv1i64.p0.i64(<vscale x 1 x i64> poison, ptr [[P:%.*]], i64 [[TMP1]], <vscale x 1 x i1> shufflevector (<vscale x 1 x i1> insertelement (<vscale x 1 x i1> poison, i1 true, i64 0), <vscale x 1 x i1> poison, <vscale x 1 x i32> zeroinitializer))
96 ; CHECK-NEXT: ret <vscale x 1 x i64> [[X]]
98 %step = call <vscale x 1 x i64> @llvm.experimental.stepvector.nxv1i64()
99 %splat.insert = insertelement <vscale x 1 x i64> poison, i64 %stride, i64 0
100 %splat = shufflevector <vscale x 1 x i64> %splat.insert, <vscale x 1 x i64> poison, <vscale x 1 x i32> zeroinitializer
101 %offsets = mul <vscale x 1 x i64> %step, %splat
102 %ptrs = getelementptr i32, ptr %p, <vscale x 1 x i64> %offsets
103 %x = call <vscale x 1 x i64> @llvm.masked.gather.nxv1i64.nxv1p0(
104 <vscale x 1 x ptr> %ptrs,
106 <vscale x 1 x i1> shufflevector (<vscale x 1 x i1> insertelement (<vscale x 1 x i1> poison, i1 1, i64 0), <vscale x 1 x i1> poison, <vscale x 1 x i32> zeroinitializer),
107 <vscale x 1 x i64> poison
109 ret <vscale x 1 x i64> %x
112 define <vscale x 1 x i64> @straightline_offset_add(ptr %p, i64 %offset) {
113 ; CHECK-LABEL: @straightline_offset_add(
114 ; CHECK-NEXT: [[TMP1:%.*]] = getelementptr i32, ptr [[P:%.*]], i64 [[OFFSET:%.*]]
115 ; CHECK-NEXT: [[X:%.*]] = call <vscale x 1 x i64> @llvm.riscv.masked.strided.load.nxv1i64.p0.i64(<vscale x 1 x i64> poison, ptr [[TMP1]], i64 4, <vscale x 1 x i1> shufflevector (<vscale x 1 x i1> insertelement (<vscale x 1 x i1> poison, i1 true, i64 0), <vscale x 1 x i1> poison, <vscale x 1 x i32> zeroinitializer))
116 ; CHECK-NEXT: ret <vscale x 1 x i64> [[X]]
118 %step = call <vscale x 1 x i64> @llvm.experimental.stepvector.nxv1i64()
119 %splat.insert = insertelement <vscale x 1 x i64> poison, i64 %offset, i64 0
120 %splat = shufflevector <vscale x 1 x i64> %splat.insert, <vscale x 1 x i64> poison, <vscale x 1 x i32> zeroinitializer
121 %offsetv = add <vscale x 1 x i64> %step, %splat
122 %ptrs = getelementptr i32, ptr %p, <vscale x 1 x i64> %offsetv
123 %x = call <vscale x 1 x i64> @llvm.masked.gather.nxv1i64.nxv1p0(
124 <vscale x 1 x ptr> %ptrs,
126 <vscale x 1 x i1> shufflevector (<vscale x 1 x i1> insertelement (<vscale x 1 x i1> poison, i1 1, i64 0), <vscale x 1 x i1> poison, <vscale x 1 x i32> zeroinitializer),
127 <vscale x 1 x i64> poison
129 ret <vscale x 1 x i64> %x
132 define <vscale x 1 x i64> @straightline_offset_shl(ptr %p) {
133 ; CHECK-LABEL: @straightline_offset_shl(
134 ; CHECK-NEXT: [[X:%.*]] = call <vscale x 1 x i64> @llvm.riscv.masked.strided.load.nxv1i64.p0.i64(<vscale x 1 x i64> poison, ptr [[P:%.*]], i64 32, <vscale x 1 x i1> shufflevector (<vscale x 1 x i1> insertelement (<vscale x 1 x i1> poison, i1 true, i64 0), <vscale x 1 x i1> poison, <vscale x 1 x i32> zeroinitializer))
135 ; CHECK-NEXT: ret <vscale x 1 x i64> [[X]]
137 %step = call <vscale x 1 x i64> @llvm.experimental.stepvector.nxv1i64()
138 %splat.insert = insertelement <vscale x 1 x i64> poison, i64 3, i64 0
139 %splat = shufflevector <vscale x 1 x i64> %splat.insert, <vscale x 1 x i64> poison, <vscale x 1 x i32> zeroinitializer
140 %offset = shl <vscale x 1 x i64> %step, %splat
141 %ptrs = getelementptr i32, ptr %p, <vscale x 1 x i64> %offset
142 %x = call <vscale x 1 x i64> @llvm.masked.gather.nxv1i64.nxv1p0(
143 <vscale x 1 x ptr> %ptrs,
145 <vscale x 1 x i1> shufflevector (<vscale x 1 x i1> insertelement (<vscale x 1 x i1> poison, i1 1, i64 0), <vscale x 1 x i1> poison, <vscale x 1 x i32> zeroinitializer),
146 <vscale x 1 x i64> poison
148 ret <vscale x 1 x i64> %x
151 define <vscale x 1 x i64> @neg_shl_is_not_commutative(ptr %p) {
152 ; CHECK-LABEL: @neg_shl_is_not_commutative(
153 ; CHECK-NEXT: [[STEP:%.*]] = call <vscale x 1 x i64> @llvm.experimental.stepvector.nxv1i64()
154 ; CHECK-NEXT: [[SPLAT_INSERT:%.*]] = insertelement <vscale x 1 x i64> poison, i64 3, i64 0
155 ; CHECK-NEXT: [[SPLAT:%.*]] = shufflevector <vscale x 1 x i64> [[SPLAT_INSERT]], <vscale x 1 x i64> poison, <vscale x 1 x i32> zeroinitializer
156 ; CHECK-NEXT: [[OFFSET:%.*]] = shl <vscale x 1 x i64> [[SPLAT]], [[STEP]]
157 ; CHECK-NEXT: [[PTRS:%.*]] = getelementptr i32, ptr [[P:%.*]], <vscale x 1 x i64> [[OFFSET]]
158 ; CHECK-NEXT: [[X:%.*]] = call <vscale x 1 x i64> @llvm.masked.gather.nxv1i64.nxv1p0(<vscale x 1 x ptr> [[PTRS]], i32 8, <vscale x 1 x i1> shufflevector (<vscale x 1 x i1> insertelement (<vscale x 1 x i1> poison, i1 true, i64 0), <vscale x 1 x i1> poison, <vscale x 1 x i32> zeroinitializer), <vscale x 1 x i64> poison)
159 ; CHECK-NEXT: ret <vscale x 1 x i64> [[X]]
161 %step = call <vscale x 1 x i64> @llvm.experimental.stepvector.nxv1i64()
162 %splat.insert = insertelement <vscale x 1 x i64> poison, i64 3, i64 0
163 %splat = shufflevector <vscale x 1 x i64> %splat.insert, <vscale x 1 x i64> poison, <vscale x 1 x i32> zeroinitializer
164 %offset = shl <vscale x 1 x i64> %splat, %step
165 %ptrs = getelementptr i32, ptr %p, <vscale x 1 x i64> %offset
166 %x = call <vscale x 1 x i64> @llvm.masked.gather.nxv1i64.nxv1p0(
167 <vscale x 1 x ptr> %ptrs,
169 <vscale x 1 x i1> shufflevector (<vscale x 1 x i1> insertelement (<vscale x 1 x i1> poison, i1 1, i64 0), <vscale x 1 x i1> poison, <vscale x 1 x i32> zeroinitializer),
170 <vscale x 1 x i64> poison
172 ret <vscale x 1 x i64> %x
175 define <vscale x 1 x i64> @straightline_offset_shl_nonc(ptr %p, i64 %shift) {
176 ; CHECK-LABEL: @straightline_offset_shl_nonc(
177 ; CHECK-NEXT: [[TMP1:%.*]] = shl i64 1, [[SHIFT:%.*]]
178 ; CHECK-NEXT: [[TMP2:%.*]] = mul i64 [[TMP1]], 4
179 ; CHECK-NEXT: [[X:%.*]] = call <vscale x 1 x i64> @llvm.riscv.masked.strided.load.nxv1i64.p0.i64(<vscale x 1 x i64> poison, ptr [[P:%.*]], i64 [[TMP2]], <vscale x 1 x i1> shufflevector (<vscale x 1 x i1> insertelement (<vscale x 1 x i1> poison, i1 true, i64 0), <vscale x 1 x i1> poison, <vscale x 1 x i32> zeroinitializer))
180 ; CHECK-NEXT: ret <vscale x 1 x i64> [[X]]
182 %step = call <vscale x 1 x i64> @llvm.experimental.stepvector.nxv1i64()
183 %splat.insert = insertelement <vscale x 1 x i64> poison, i64 %shift, i64 0
184 %splat = shufflevector <vscale x 1 x i64> %splat.insert, <vscale x 1 x i64> poison, <vscale x 1 x i32> zeroinitializer
185 %offset = shl <vscale x 1 x i64> %step, %splat
186 %ptrs = getelementptr i32, ptr %p, <vscale x 1 x i64> %offset
187 %x = call <vscale x 1 x i64> @llvm.masked.gather.nxv1i64.nxv1p0(
188 <vscale x 1 x ptr> %ptrs,
190 <vscale x 1 x i1> shufflevector (<vscale x 1 x i1> insertelement (<vscale x 1 x i1> poison, i1 1, i64 0), <vscale x 1 x i1> poison, <vscale x 1 x i32> zeroinitializer),
191 <vscale x 1 x i64> poison
193 ret <vscale x 1 x i64> %x
196 define void @scatter_loopless(<vscale x 1 x i64> %x, ptr %p, i64 %stride) {
197 ; CHECK-LABEL: @scatter_loopless(
198 ; CHECK-NEXT: [[TMP1:%.*]] = mul i64 [[STRIDE:%.*]], 4
199 ; CHECK-NEXT: call void @llvm.riscv.masked.strided.store.nxv1i64.p0.i64(<vscale x 1 x i64> [[X:%.*]], ptr [[P:%.*]], i64 [[TMP1]], <vscale x 1 x i1> shufflevector (<vscale x 1 x i1> insertelement (<vscale x 1 x i1> poison, i1 true, i64 0), <vscale x 1 x i1> poison, <vscale x 1 x i32> zeroinitializer))
200 ; CHECK-NEXT: ret void
202 %step = call <vscale x 1 x i64> @llvm.experimental.stepvector.nxv1i64()
203 %splat.insert = insertelement <vscale x 1 x i64> poison, i64 %stride, i64 0
204 %splat = shufflevector <vscale x 1 x i64> %splat.insert, <vscale x 1 x i64> poison, <vscale x 1 x i32> zeroinitializer
205 %offsets = mul <vscale x 1 x i64> %step, %splat
206 %ptrs = getelementptr i32, ptr %p, <vscale x 1 x i64> %offsets
207 call void @llvm.masked.scatter.nxv1i64.nxv1p0(
208 <vscale x 1 x i64> %x,
209 <vscale x 1 x ptr> %ptrs,
211 <vscale x 1 x i1> shufflevector (<vscale x 1 x i1> insertelement (<vscale x 1 x i1> poison, i1 1, i64 0), <vscale x 1 x i1> poison, <vscale x 1 x i32> zeroinitializer)
216 ; We previously crashed expecting a constant to be fixed length.
217 define void @constant_stride(<vscale x 1 x i64> %x, ptr %p, i64 %stride) {
218 ; CHECK-LABEL: @constant_stride(
219 ; CHECK-NEXT: [[PTRS:%.*]] = getelementptr i32, ptr [[P:%.*]], <vscale x 1 x i64> zeroinitializer
220 ; CHECK-NEXT: call void @llvm.masked.scatter.nxv1i64.nxv1p0(<vscale x 1 x i64> [[X:%.*]], <vscale x 1 x ptr> [[PTRS]], i32 8, <vscale x 1 x i1> shufflevector (<vscale x 1 x i1> insertelement (<vscale x 1 x i1> poison, i1 true, i64 0), <vscale x 1 x i1> poison, <vscale x 1 x i32> zeroinitializer))
221 ; CHECK-NEXT: ret void
223 %ptrs = getelementptr i32, ptr %p, <vscale x 1 x i64> zeroinitializer
224 call void @llvm.masked.scatter.nxv1i64.nxv1p0(
225 <vscale x 1 x i64> %x,
226 <vscale x 1 x ptr> %ptrs,
228 <vscale x 1 x i1> shufflevector (<vscale x 1 x i1> insertelement (<vscale x 1 x i1> poison, i1 1, i64 0), <vscale x 1 x i1> poison, <vscale x 1 x i32> zeroinitializer)
233 declare i64 @llvm.vscale.i64()
234 declare void @llvm.masked.scatter.nxv1i64.nxv1p0(<vscale x 1 x i64>, <vscale x 1 x ptr>, i32, <vscale x 1 x i1>)
235 declare <vscale x 1 x i64> @llvm.masked.gather.nxv1i64.nxv1p0(<vscale x 1 x ptr>, i32, <vscale x 1 x i1>, <vscale x 1 x i64>)