1;; Represents the possible widths of an element when used in an operation.
2(type VecElementWidth (enum
3  (E8)
4  (E16)
5  (E32)
6  (E64)
7))
8
9;; Vector Register Group Multiplier (LMUL)
10;;
11;; The LMUL setting specifies how we should group registers together. LMUL can
12;; also be a fractional value, reducing the number of bits used in a single
13;; vector register. Fractional LMUL is used to increase the number of effective
14;; usable vector register groups when operating on mixed-width values.
15(type VecLmul (enum
16  (LmulF8)
17  (LmulF4)
18  (LmulF2)
19  (Lmul1)
20  (Lmul2)
21  (Lmul4)
22  (Lmul8)
23))
24
25;; Tail Mode
26;;
27;; The tail mode specifies how the tail elements of a vector register are handled.
28(type VecTailMode (enum
29  ;; Tail Agnostic means that the tail elements are left in an undefined state.
30  (Agnostic)
31  ;; Tail Undisturbed means that the tail elements are left in their original values.
32  (Undisturbed)
33))
34
35;; Mask Mode
36;;
37;; The mask mode specifies how the masked elements of a vector register are handled.
38(type VecMaskMode (enum
39  ;; Mask Agnostic means that the masked out elements are left in an undefined state.
40  (Agnostic)
41  ;; Mask Undisturbed means that the masked out elements are left in their original values.
42  (Undisturbed)
43))
44
45;; Application Vector Length (AVL)
46;;
47;; This setting specifies the number of elements that are going to be processed
48;; in a single instruction. Note: We may end up processing fewer elements than
49;; the AVL setting, if they don't fit in a single register.
50(type VecAvl (enum
51  ;; Static AVL emits a `vsetivli` that uses a constant value
52  (Static (size UImm5))
53  ;; TODO: Add a dynamic, register based AVL mode when we are able to properly test it
54))
55
56(type VType (primitive VType))
57(type VState (primitive VState))
58
59
60;; Vector Opcode Category
61;;
62;; These categories are used to determine the type of operands that are allowed in the
63;; instruction.
64(type VecOpCategory (enum
65  (OPIVV)
66  (OPFVV)
67  (OPMVV)
68  (OPIVI)
69  (OPIVX)
70  (OPFVF)
71  (OPMVX)
72  (OPCFG)
73))
74
75;; Vector Opcode Masking
76;;
77;; When masked, the instruction will only operate on the elements that are dictated by
78;; the mask register. Currently this is always fixed to v0.
79(type VecOpMasking (enum
80  (Enabled (reg Reg))
81  (Disabled)
82))
83
84(decl pure masked (VReg) VecOpMasking)
85(rule (masked reg) (VecOpMasking.Enabled reg))
86
87(decl pure unmasked () VecOpMasking)
88(rule (unmasked) (VecOpMasking.Disabled))
89
90;; Register to Register ALU Ops
91(type VecAluOpRRR (enum
92  ;; Vector-Vector Opcodes
93  (VaddVV)
94  (VsaddVV)
95  (VsadduVV)
96  (VwaddVV)
97  (VwaddWV)
98  (VwadduVV)
99  (VwadduWV)
100  (VsubVV)
101  (VwsubVV)
102  (VwsubWV)
103  (VwsubuVV)
104  (VwsubuWV)
105  (VssubVV)
106  (VssubuVV)
107  (VmulVV)
108  (VmulhVV)
109  (VmulhuVV)
110  (VsmulVV)
111  (VsllVV)
112  (VsrlVV)
113  (VsraVV)
114  (VandVV)
115  (VorVV)
116  (VxorVV)
117  (VmaxVV)
118  (VmaxuVV)
119  (VminVV)
120  (VminuVV)
121  (VfaddVV)
122  (VfsubVV)
123  (VfmulVV)
124  (VfdivVV)
125  (VfminVV)
126  (VfmaxVV)
127  (VfsgnjVV)
128  (VfsgnjnVV)
129  (VfsgnjxVV)
130  (VmergeVVM)
131  (VredmaxuVS)
132  (VredminuVS)
133  (VrgatherVV)
134  (VcompressVM)
135  (VmseqVV)
136  (VmsneVV)
137  (VmsltuVV)
138  (VmsltVV)
139  (VmsleuVV)
140  (VmsleVV)
141  (VmfeqVV)
142  (VmfneVV)
143  (VmfltVV)
144  (VmfleVV)
145  (VmandMM)
146  (VmorMM)
147  (VmnandMM)
148  (VmnorMM)
149
150
151  ;; Vector-Scalar Opcodes
152  (VaddVX)
153  (VsaddVX)
154  (VsadduVX)
155  (VwaddVX)
156  (VwaddWX)
157  (VwadduVX)
158  (VwadduWX)
159  (VsubVX)
160  (VrsubVX)
161  (VwsubVX)
162  (VwsubWX)
163  (VwsubuVX)
164  (VwsubuWX)
165  (VssubVX)
166  (VssubuVX)
167  (VmulVX)
168  (VmulhVX)
169  (VmulhuVX)
170  (VsmulVX)
171  (VsllVX)
172  (VsrlVX)
173  (VsraVX)
174  (VandVX)
175  (VorVX)
176  (VxorVX)
177  (VmaxVX)
178  (VmaxuVX)
179  (VminVX)
180  (VminuVX)
181  (VslidedownVX)
182  (VfaddVF)
183  (VfsubVF)
184  (VfrsubVF)
185  (VfmulVF)
186  (VfdivVF)
187  (VfsgnjVF)
188  (VfrdivVF)
189  (VmergeVXM)
190  (VfmergeVFM)
191  (VrgatherVX)
192  (VmseqVX)
193  (VmsneVX)
194  (VmsltuVX)
195  (VmsltVX)
196  (VmsleuVX)
197  (VmsleVX)
198  (VmsgtuVX)
199  (VmsgtVX)
200  (VmfeqVF)
201  (VmfneVF)
202  (VmfltVF)
203  (VmfleVF)
204  (VmfgtVF)
205  (VmfgeVF)
206))
207
208
209
210;; Register-Imm ALU Ops that modify the destination register
211(type VecAluOpRRRImm5 (enum
212  (VslideupVI)
213))
214
215;; Register-Register ALU Ops that modify the destination register
216(type VecAluOpRRRR (enum
217  ;; Vector-Vector Opcodes
218  (VmaccVV)
219  (VnmsacVV)
220  (VfmaccVV)
221  (VfnmaccVV)
222  (VfmsacVV)
223  (VfnmsacVV)
224
225  ;; Vector-Scalar Opcodes
226  (VmaccVX)
227  (VnmsacVX)
228  (VfmaccVF)
229  (VfnmaccVF)
230  (VfmsacVF)
231  (VfnmsacVF)
232  (Vslide1upVX)
233))
234
235;; Register-Imm ALU Ops
236(type VecAluOpRRImm5 (enum
237  ;; Regular VI Opcodes
238  (VaddVI)
239  (VsaddVI)
240  (VsadduVI)
241  (VrsubVI)
242  (VsllVI)
243  (VsrlVI)
244  (VsraVI)
245  (VandVI)
246  (VorVI)
247  (VxorVI)
248  (VssrlVI)
249  (VslidedownVI)
250  (VmergeVIM)
251  (VrgatherVI)
252  ;; This opcode represents multiple instructions `vmv1r`/`vmv2r`/`vmv4r`/etc...
253  ;; The immediate field specifies how many registers should be copied.
254  (VmvrV)
255  (VnclipWI)
256  (VnclipuWI)
257  (VmseqVI)
258  (VmsneVI)
259  (VmsleuVI)
260  (VmsleVI)
261  (VmsgtuVI)
262  (VmsgtVI)
263))
264
265;; Imm only ALU Ops
266(type VecAluOpRImm5 (enum
267  (VmvVI)
268))
269
270;; These are all of the special cases that have weird encodings. They are all
271;; single source, single destination instructions, and usually use one of
272;; the two source registers as auxiliary encoding space.
273(type VecAluOpRR (enum
274  (VmvSX)
275  (VmvXS)
276  (VfmvSF)
277  (VfmvFS)
278  ;; vmv.v* is special in that vs2 must be v0 (and is ignored) otherwise the instruction is illegal.
279  (VmvVV)
280  (VmvVX)
281  (VfmvVF)
282  (VfsqrtV)
283  (VsextVF2)
284  (VsextVF4)
285  (VsextVF8)
286  (VzextVF2)
287  (VzextVF4)
288  (VzextVF8)
289  (VfcvtxufV)
290  (VfcvtxfV)
291  (VfcvtrtzxufV)
292  (VfcvtrtzxfV)
293  (VfcvtfxuV)
294  (VfcvtfxV)
295  (VfwcvtffV)
296  (VfncvtffW)
297))
298
299;; Returns the canonical destination type for a VecAluOpRRImm5.
300(decl pure vec_alu_rr_dst_type (VecAluOpRR) Type)
301(extern constructor vec_alu_rr_dst_type vec_alu_rr_dst_type)
302
303
304;; Vector Addressing Mode
305(type VecAMode (enum
306  ;; Vector unit-stride operations access elements stored contiguously in memory
307  ;; starting from the base effective address.
308  (UnitStride
309    (base AMode))
310  ;; TODO: Constant Stride
311  ;; TODO: Indexed Operations
312))
313
314
315;; Builds a static VState matching a SIMD type.
316;; The VState is guaranteed to be static with AVL set to the number of lanes.
317;; Element size is set to the size of the type.
318;; LMUL is set to 1.
319;; Tail mode is set to agnostic.
320;; Mask mode is set to agnostic.
321(decl pure vstate_from_type (Type) VState)
322(extern constructor vstate_from_type vstate_from_type)
323(convert Type VState vstate_from_type)
324
325;; Alters the LMUL of a VState to mf2
326(decl pure vstate_mf2 (VState) VState)
327(extern constructor vstate_mf2 vstate_mf2)
328
329;; Extracts an element width from a SIMD type.
330(decl pure element_width_from_type (Type) VecElementWidth)
331(rule (element_width_from_type ty)
332  (if-let $I8 (lane_type ty))
333  (VecElementWidth.E8))
334(rule (element_width_from_type ty)
335  (if-let $I16 (lane_type ty))
336  (VecElementWidth.E16))
337(rule (element_width_from_type ty)
338  (if-let $I32 (lane_type ty))
339  (VecElementWidth.E32))
340(rule (element_width_from_type ty)
341  (if-let $F32 (lane_type ty))
342  (VecElementWidth.E32))
343(rule (element_width_from_type ty)
344  (if-let $I64 (lane_type ty))
345  (VecElementWidth.E64))
346(rule (element_width_from_type ty)
347  (if-let $F64 (lane_type ty))
348  (VecElementWidth.E64))
349
350(decl pure min_vec_reg_size () u64)
351(extern constructor min_vec_reg_size min_vec_reg_size)
352
353;; An extractor that matches any type that is known to fit in a single vector
354;; register.
355(decl ty_vec_fits_in_register (Type) Type)
356(extern extractor ty_vec_fits_in_register ty_vec_fits_in_register)
357
358;;;; Instruction Helpers ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
359
360;; As noted in the RISC-V Vector Extension Specification, rs2 is the first
361;; source register and rs1 is the second source register. This is the opposite
362;; of the usual RISC-V register order.
363;; See Section 10.1 of the RISC-V Vector Extension Specification.
364
365
366;; Helper for emitting `MInst.VecAluRRRR` instructions.
367;; These instructions modify the destination register.
368(decl vec_alu_rrrr (VecAluOpRRRR VReg VReg Reg VecOpMasking  VState) VReg)
369(rule (vec_alu_rrrr op vd_src vs2 vs1 mask vstate)
370      (let ((vd WritableVReg (temp_writable_vreg))
371            (_ Unit (emit (MInst.VecAluRRRR op vd vd_src vs2 vs1 mask vstate))))
372        vd))
373
374;; Helper for emitting `MInst.VecAluRRRImm5` instructions.
375;; These instructions modify the destination register.
376(decl vec_alu_rrr_imm5 (VecAluOpRRRImm5 VReg VReg Imm5 VecOpMasking  VState) VReg)
377(rule (vec_alu_rrr_imm5 op vd_src vs2 imm mask vstate)
378      (let ((vd WritableVReg (temp_writable_vreg))
379            (_ Unit (emit (MInst.VecAluRRRImm5 op vd vd_src vs2 imm mask vstate))))
380        vd))
381
382;; Helper for emitting `MInst.VecAluRRRImm5` instructions where the immediate
383;; is zero extended instead of sign extended.
384(decl vec_alu_rrr_uimm5 (VecAluOpRRRImm5 VReg VReg UImm5 VecOpMasking VState) VReg)
385(rule (vec_alu_rrr_uimm5 op vd_src vs2 imm mask vstate)
386      (vec_alu_rrr_imm5 op vd_src vs2 (uimm5_bitcast_to_imm5 imm) mask vstate))
387
388;; Helper for emitting `MInst.VecAluRRR` instructions.
389(decl vec_alu_rrr (VecAluOpRRR Reg Reg VecOpMasking VState) Reg)
390(rule (vec_alu_rrr op vs2 vs1 mask vstate)
391      (let ((vd WritableVReg (temp_writable_vreg))
392            (_ Unit (emit (MInst.VecAluRRR op vd vs2 vs1 mask vstate))))
393        vd))
394
395;; Helper for emitting `MInst.VecAluRRImm5` instructions.
396(decl vec_alu_rr_imm5 (VecAluOpRRImm5 Reg Imm5 VecOpMasking  VState) Reg)
397(rule (vec_alu_rr_imm5 op vs2 imm mask vstate)
398      (let ((vd WritableVReg (temp_writable_vreg))
399            (_ Unit (emit (MInst.VecAluRRImm5 op vd vs2 imm mask vstate))))
400        vd))
401
402;; Helper for emitting `MInst.VecAluRRImm5` instructions where the immediate
403;; is zero extended instead of sign extended.
404(decl vec_alu_rr_uimm5 (VecAluOpRRImm5 Reg UImm5 VecOpMasking VState) Reg)
405(rule (vec_alu_rr_uimm5 op vs2 imm mask vstate)
406      (vec_alu_rr_imm5 op vs2 (uimm5_bitcast_to_imm5 imm) mask vstate))
407
408;; Helper for emitting `MInst.VecAluRRImm5` instructions that use the Imm5 as
409;; auxiliary encoding space.
410(decl vec_alu_rr (VecAluOpRR Reg VecOpMasking VState) Reg)
411(rule (vec_alu_rr op vs mask vstate)
412      (let ((vd WritableReg (temp_writable_reg (vec_alu_rr_dst_type op)))
413            (_ Unit (emit (MInst.VecAluRR op vd vs mask vstate))))
414        vd))
415
416;; Helper for emitting `MInst.VecAluRImm5` instructions.
417(decl vec_alu_r_imm5 (VecAluOpRImm5 Imm5 VecOpMasking VState) Reg)
418(rule (vec_alu_r_imm5 op imm mask vstate)
419      (let ((vd WritableVReg (temp_writable_vreg))
420            (_ Unit (emit (MInst.VecAluRImm5 op vd imm mask vstate))))
421        vd))
422
423;; Helper for emitting `MInst.VecLoad` instructions.
424(decl vec_load (VecElementWidth VecAMode MemFlags VecOpMasking VState) Reg)
425(rule (vec_load eew from flags mask vstate)
426      (let ((vd WritableVReg (temp_writable_vreg))
427            (_ Unit (emit (MInst.VecLoad eew vd from flags mask vstate))))
428        vd))
429
430;; Helper for emitting `MInst.VecStore` instructions.
431(decl vec_store (VecElementWidth VecAMode VReg MemFlags VecOpMasking VState) InstOutput)
432(rule (vec_store eew to from flags mask vstate)
433      (side_effect
434        (SideEffectNoResult.Inst (MInst.VecStore eew to from flags mask vstate))))
435
436;; Helper for emitting the `vadd.vv` instruction.
437(decl rv_vadd_vv (VReg VReg VecOpMasking VState) VReg)
438(rule (rv_vadd_vv vs2 vs1 mask vstate)
439  (vec_alu_rrr (VecAluOpRRR.VaddVV) vs2 vs1 mask vstate))
440
441;; Helper for emitting the `vadd.vx` instruction.
442(decl rv_vadd_vx (VReg XReg VecOpMasking VState) VReg)
443(rule (rv_vadd_vx vs2 vs1 mask vstate)
444  (vec_alu_rrr (VecAluOpRRR.VaddVX) vs2 vs1 mask vstate))
445
446;; Helper for emitting the `vadd.vi` instruction.
447(decl rv_vadd_vi (VReg Imm5 VecOpMasking VState) VReg)
448(rule (rv_vadd_vi vs2 imm mask vstate)
449  (vec_alu_rr_imm5 (VecAluOpRRImm5.VaddVI) vs2 imm mask vstate))
450
451;; Helper for emitting the `vsadd.vv` instruction.
452(decl rv_vsadd_vv (VReg VReg VecOpMasking VState) VReg)
453(rule (rv_vsadd_vv vs2 vs1 mask vstate)
454  (vec_alu_rrr (VecAluOpRRR.VsaddVV) vs2 vs1 mask vstate))
455
456;; Helper for emitting the `vsadd.vx` instruction.
457(decl rv_vsadd_vx (VReg XReg VecOpMasking VState) VReg)
458(rule (rv_vsadd_vx vs2 vs1 mask vstate)
459  (vec_alu_rrr (VecAluOpRRR.VsaddVX) vs2 vs1 mask vstate))
460
461;; Helper for emitting the `vsadd.vi` instruction.
462(decl rv_vsadd_vi (VReg Imm5 VecOpMasking VState) VReg)
463(rule (rv_vsadd_vi vs2 imm mask vstate)
464  (vec_alu_rr_imm5 (VecAluOpRRImm5.VsaddVI) vs2 imm mask vstate))
465
466;; Helper for emitting the `vsaddu.vv` instruction.
467(decl rv_vsaddu_vv (VReg VReg VecOpMasking VState) VReg)
468(rule (rv_vsaddu_vv vs2 vs1 mask vstate)
469  (vec_alu_rrr (VecAluOpRRR.VsadduVV) vs2 vs1 mask vstate))
470
471;; Helper for emitting the `vsaddu.vx` instruction.
472(decl rv_vsaddu_vx (VReg XReg VecOpMasking VState) VReg)
473(rule (rv_vsaddu_vx vs2 vs1 mask vstate)
474  (vec_alu_rrr (VecAluOpRRR.VsadduVX) vs2 vs1 mask vstate))
475
476;; Helper for emitting the `vsaddu.vi` instruction.
477(decl rv_vsaddu_vi (VReg Imm5 VecOpMasking VState) VReg)
478(rule (rv_vsaddu_vi vs2 imm mask vstate)
479  (vec_alu_rr_imm5 (VecAluOpRRImm5.VsadduVI) vs2 imm mask vstate))
480
481;; Helper for emitting the `vwadd.vv` instruction.
482;;
483;;  Widening integer add, 2*SEW = SEW + SEW
484(decl rv_vwadd_vv (VReg VReg VecOpMasking VState) VReg)
485(rule (rv_vwadd_vv vs2 vs1 mask vstate)
486  (vec_alu_rrr (VecAluOpRRR.VwaddVV) vs2 vs1 mask vstate))
487
488;; Helper for emitting the `vwadd.vx` instruction.
489;;
490;;  Widening integer add, 2*SEW = SEW + SEW
491(decl rv_vwadd_vx (VReg XReg VecOpMasking VState) VReg)
492(rule (rv_vwadd_vx vs2 vs1 mask vstate)
493  (vec_alu_rrr (VecAluOpRRR.VwaddVX) vs2 vs1 mask vstate))
494
495;; Helper for emitting the `vwadd.wv` instruction.
496;;
497;;  Widening integer add, 2*SEW = 2*SEW + SEW
498(decl rv_vwadd_wv (VReg VReg VecOpMasking VState) VReg)
499(rule (rv_vwadd_wv vs2 vs1 mask vstate)
500  (vec_alu_rrr (VecAluOpRRR.VwaddWV) vs2 vs1 mask vstate))
501
502;; Helper for emitting the `vwadd.wx` instruction.
503;;
504;;  Widening integer add, 2*SEW = 2*SEW + SEW
505(decl rv_vwadd_wx (VReg XReg VecOpMasking VState) VReg)
506(rule (rv_vwadd_wx vs2 vs1 mask vstate)
507  (vec_alu_rrr (VecAluOpRRR.VwaddWX) vs2 vs1 mask vstate))
508
509;; Helper for emitting the `vwaddu.vv` instruction.
510;;
511;; Widening unsigned integer add, 2*SEW = SEW + SEW
512(decl rv_vwaddu_vv (VReg VReg VecOpMasking VState) VReg)
513(rule (rv_vwaddu_vv vs2 vs1 mask vstate)
514  (vec_alu_rrr (VecAluOpRRR.VwadduVV) vs2 vs1 mask vstate))
515
516;; Helper for emitting the `vwaddu.vv` instruction.
517;;
518;; Widening unsigned integer add, 2*SEW = SEW + SEW
519(decl rv_vwaddu_vx (VReg XReg VecOpMasking VState) VReg)
520(rule (rv_vwaddu_vx vs2 vs1 mask vstate)
521  (vec_alu_rrr (VecAluOpRRR.VwadduVX) vs2 vs1 mask vstate))
522
523;; Helper for emitting the `vwaddu.wv` instruction.
524;;
525;;  Widening integer add, 2*SEW = 2*SEW + SEW
526(decl rv_vwaddu_wv (VReg VReg VecOpMasking VState) VReg)
527(rule (rv_vwaddu_wv vs2 vs1 mask vstate)
528  (vec_alu_rrr (VecAluOpRRR.VwadduWV) vs2 vs1 mask vstate))
529
530;; Helper for emitting the `vwaddu.wx` instruction.
531;;
532;;  Widening integer add, 2*SEW = 2*SEW + SEW
533(decl rv_vwaddu_wx (VReg XReg VecOpMasking VState) VReg)
534(rule (rv_vwaddu_wx vs2 vs1 mask vstate)
535  (vec_alu_rrr (VecAluOpRRR.VwadduWX) vs2 vs1 mask vstate))
536
537;; Helper for emitting the `vsub.vv` instruction.
538(decl rv_vsub_vv (VReg VReg VecOpMasking VState) VReg)
539(rule (rv_vsub_vv vs2 vs1 mask vstate)
540  (vec_alu_rrr (VecAluOpRRR.VsubVV) vs2 vs1 mask vstate))
541
542;; Helper for emitting the `vsub.vx` instruction.
543(decl rv_vsub_vx (VReg XReg VecOpMasking VState) VReg)
544(rule (rv_vsub_vx vs2 vs1 mask vstate)
545  (vec_alu_rrr (VecAluOpRRR.VsubVX) vs2 vs1 mask vstate))
546
547;; Helper for emitting the `vrsub.vx` instruction.
548(decl rv_vrsub_vx (VReg XReg VecOpMasking VState) VReg)
549(rule (rv_vrsub_vx vs2 vs1 mask vstate)
550  (vec_alu_rrr (VecAluOpRRR.VrsubVX) vs2 vs1 mask vstate))
551
552;; Helper for emitting the `vwsub.vv` instruction.
553;;
554;;  Widening integer sub, 2*SEW = SEW + SEW
555(decl rv_vwsub_vv (VReg VReg VecOpMasking VState) VReg)
556(rule (rv_vwsub_vv vs2 vs1 mask vstate)
557  (vec_alu_rrr (VecAluOpRRR.VwsubVV) vs2 vs1 mask vstate))
558
559;; Helper for emitting the `vwsub.vx` instruction.
560;;
561;;  Widening integer sub, 2*SEW = SEW + SEW
562(decl rv_vwsub_vx (VReg XReg VecOpMasking VState) VReg)
563(rule (rv_vwsub_vx vs2 vs1 mask vstate)
564  (vec_alu_rrr (VecAluOpRRR.VwsubVX) vs2 vs1 mask vstate))
565
566;; Helper for emitting the `vwsub.wv` instruction.
567;;
568;;  Widening integer sub, 2*SEW = 2*SEW + SEW
569(decl rv_vwsub_wv (VReg VReg VecOpMasking VState) VReg)
570(rule (rv_vwsub_wv vs2 vs1 mask vstate)
571  (vec_alu_rrr (VecAluOpRRR.VwsubWV) vs2 vs1 mask vstate))
572
573;; Helper for emitting the `vwsub.wx` instruction.
574;;
575;;  Widening integer sub, 2*SEW = 2*SEW + SEW
576(decl rv_vwsub_wx (VReg XReg VecOpMasking VState) VReg)
577(rule (rv_vwsub_wx vs2 vs1 mask vstate)
578  (vec_alu_rrr (VecAluOpRRR.VwsubWX) vs2 vs1 mask vstate))
579
580;; Helper for emitting the `vwsubu.vv` instruction.
581;;
582;; Widening unsigned integer sub, 2*SEW = SEW + SEW
583(decl rv_vwsubu_vv (VReg VReg VecOpMasking VState) VReg)
584(rule (rv_vwsubu_vv vs2 vs1 mask vstate)
585  (vec_alu_rrr (VecAluOpRRR.VwsubuVV) vs2 vs1 mask vstate))
586
587;; Helper for emitting the `vwsubu.vv` instruction.
588;;
589;; Widening unsigned integer sub, 2*SEW = SEW + SEW
590(decl rv_vwsubu_vx (VReg XReg VecOpMasking VState) VReg)
591(rule (rv_vwsubu_vx vs2 vs1 mask vstate)
592  (vec_alu_rrr (VecAluOpRRR.VwsubuVX) vs2 vs1 mask vstate))
593
594;; Helper for emitting the `vwsubu.wv` instruction.
595;;
596;;  Widening integer sub, 2*SEW = 2*SEW + SEW
597(decl rv_vwsubu_wv (VReg VReg VecOpMasking VState) VReg)
598(rule (rv_vwsubu_wv vs2 vs1 mask vstate)
599  (vec_alu_rrr (VecAluOpRRR.VwsubuWV) vs2 vs1 mask vstate))
600
601;; Helper for emitting the `vwsubu.wx` instruction.
602;;
603;;  Widening integer sub, 2*SEW = 2*SEW + SEW
604(decl rv_vwsubu_wx (VReg XReg VecOpMasking VState) VReg)
605(rule (rv_vwsubu_wx vs2 vs1 mask vstate)
606  (vec_alu_rrr (VecAluOpRRR.VwsubuWX) vs2 vs1 mask vstate))
607
608;; Helper for emitting the `vssub.vv` instruction.
609(decl rv_vssub_vv (VReg VReg VecOpMasking VState) VReg)
610(rule (rv_vssub_vv vs2 vs1 mask vstate)
611  (vec_alu_rrr (VecAluOpRRR.VssubVV) vs2 vs1 mask vstate))
612
613;; Helper for emitting the `vssub.vx` instruction.
614(decl rv_vssub_vx (VReg XReg VecOpMasking VState) VReg)
615(rule (rv_vssub_vx vs2 vs1 mask vstate)
616  (vec_alu_rrr (VecAluOpRRR.VssubVX) vs2 vs1 mask vstate))
617
618;; Helper for emitting the `vssubu.vv` instruction.
619(decl rv_vssubu_vv (VReg VReg VecOpMasking VState) VReg)
620(rule (rv_vssubu_vv vs2 vs1 mask vstate)
621  (vec_alu_rrr (VecAluOpRRR.VssubuVV) vs2 vs1 mask vstate))
622
623;; Helper for emitting the `vssubu.vx` instruction.
624(decl rv_vssubu_vx (VReg XReg VecOpMasking VState) VReg)
625(rule (rv_vssubu_vx vs2 vs1 mask vstate)
626  (vec_alu_rrr (VecAluOpRRR.VssubuVX) vs2 vs1 mask vstate))
627
628;; Helper for emitting the `vneg.v` pseudo-instruction.
629(decl rv_vneg_v (VReg VecOpMasking VState) VReg)
630(rule (rv_vneg_v vs2 mask vstate)
631  (vec_alu_rrr (VecAluOpRRR.VrsubVX) vs2 (zero_reg) mask vstate))
632
633;; Helper for emitting the `vrsub.vi` instruction.
634(decl rv_vrsub_vi (VReg Imm5 VecOpMasking VState) VReg)
635(rule (rv_vrsub_vi vs2 imm mask vstate)
636  (vec_alu_rr_imm5 (VecAluOpRRImm5.VrsubVI) vs2 imm mask vstate))
637
638;; Helper for emitting the `vmul.vv` instruction.
639(decl rv_vmul_vv (VReg VReg VecOpMasking VState) VReg)
640(rule (rv_vmul_vv vs2 vs1 mask vstate)
641  (vec_alu_rrr (VecAluOpRRR.VmulVV) vs2 vs1 mask vstate))
642
643;; Helper for emitting the `vmul.vx` instruction.
644(decl rv_vmul_vx (VReg XReg VecOpMasking VState) VReg)
645(rule (rv_vmul_vx vs2 vs1 mask vstate)
646  (vec_alu_rrr (VecAluOpRRR.VmulVX) vs2 vs1 mask vstate))
647
648;; Helper for emitting the `vmulh.vv` instruction.
649(decl rv_vmulh_vv (VReg VReg VecOpMasking VState) VReg)
650(rule (rv_vmulh_vv vs2 vs1 mask vstate)
651  (vec_alu_rrr (VecAluOpRRR.VmulhVV) vs2 vs1 mask vstate))
652
653;; Helper for emitting the `vmulh.vx` instruction.
654(decl rv_vmulh_vx (VReg XReg VecOpMasking VState) VReg)
655(rule (rv_vmulh_vx vs2 vs1 mask vstate)
656  (vec_alu_rrr (VecAluOpRRR.VmulhVX) vs2 vs1 mask vstate))
657
658;; Helper for emitting the `vmulhu.vv` instruction.
659(decl rv_vmulhu_vv (VReg VReg VecOpMasking VState) VReg)
660(rule (rv_vmulhu_vv vs2 vs1 mask vstate)
661  (vec_alu_rrr (VecAluOpRRR.VmulhuVV) vs2 vs1 mask vstate))
662
663;; Helper for emitting the `vmulhu.vx` instruction.
664(decl rv_vmulhu_vx (VReg XReg VecOpMasking VState) VReg)
665(rule (rv_vmulhu_vx vs2 vs1 mask vstate)
666  (vec_alu_rrr (VecAluOpRRR.VmulhuVX) vs2 vs1 mask vstate))
667
668;; Helper for emitting the `vsmul.vv` instruction.
669;;
670;; Signed saturating and rounding fractional multiply
671;; # vd[i] = clip(roundoff_signed(vs2[i]*vs1[i], SEW-1))
672(decl rv_vsmul_vv (VReg VReg VecOpMasking VState) VReg)
673(rule (rv_vsmul_vv vs2 vs1 mask vstate)
674  (vec_alu_rrr (VecAluOpRRR.VsmulVV) vs2 vs1 mask vstate))
675
676;; Helper for emitting the `vsmul.vx` instruction.
677;;
678;; Signed saturating and rounding fractional multiply
679;; # vd[i] = clip(roundoff_signed(vs2[i]*x[rs1], SEW-1))
680(decl rv_vsmul_vx (VReg XReg VecOpMasking VState) VReg)
681(rule (rv_vsmul_vx vs2 vs1 mask vstate)
682  (vec_alu_rrr (VecAluOpRRR.VsmulVX) vs2 vs1 mask vstate))
683
684;; Helper for emitting the `vmacc.vv` instruction.
685;;
686;; Integer multiply-add, overwrite addend
687;; # vd[i] = +(vs1[i] * vs2[i]) + vd[i]
688(decl rv_vmacc_vv (VReg VReg VReg VecOpMasking VState) VReg)
689(rule (rv_vmacc_vv vd vs2 vs1 mask vstate)
690  (vec_alu_rrrr (VecAluOpRRRR.VmaccVV) vd vs2 vs1 mask vstate))
691
692;; Helper for emitting the `vmacc.vx` instruction.
693;;
694;; Integer multiply-add, overwrite addend
695;; # vd[i] = +(x[rs1] * vs2[i]) + vd[i]
696(decl rv_vmacc_vx (VReg VReg XReg VecOpMasking VState) VReg)
697(rule (rv_vmacc_vx vd vs2 vs1 mask vstate)
698  (vec_alu_rrrr (VecAluOpRRRR.VmaccVX) vd vs2 vs1 mask vstate))
699
700;; Helper for emitting the `vnmsac.vv` instruction.
701;;
702;; Integer multiply-sub, overwrite minuend
703;; # vd[i] = -(vs1[i] * vs2[i]) + vd[i]
704(decl rv_vnmsac_vv (VReg VReg VReg VecOpMasking VState) VReg)
705(rule (rv_vnmsac_vv vd vs2 vs1 mask vstate)
706  (vec_alu_rrrr (VecAluOpRRRR.VnmsacVV) vd vs2 vs1 mask vstate))
707
708;; Helper for emitting the `vnmsac.vx` instruction.
709;;
710;; Integer multiply-sub, overwrite minuend
711;; # vd[i] = -(x[rs1] * vs2[i]) + vd[i]
712(decl rv_vnmsac_vx (VReg VReg XReg VecOpMasking VState) VReg)
713(rule (rv_vnmsac_vx vd vs2 vs1 mask vstate)
714  (vec_alu_rrrr (VecAluOpRRRR.VnmsacVX) vd vs2 vs1 mask vstate))
715
716;; Helper for emitting the `sll.vv` instruction.
717(decl rv_vsll_vv (VReg VReg VecOpMasking VState) VReg)
718(rule (rv_vsll_vv vs2 vs1 mask vstate)
719  (vec_alu_rrr (VecAluOpRRR.VsllVV) vs2 vs1 mask vstate))
720
721;; Helper for emitting the `sll.vx` instruction.
722(decl rv_vsll_vx (VReg XReg VecOpMasking VState) VReg)
723(rule (rv_vsll_vx vs2 vs1 mask vstate)
724  (vec_alu_rrr (VecAluOpRRR.VsllVX) vs2 vs1 mask vstate))
725
726;; Helper for emitting the `vsll.vi` instruction.
727(decl rv_vsll_vi (VReg UImm5 VecOpMasking VState) VReg)
728(rule (rv_vsll_vi vs2 imm mask vstate)
729  (vec_alu_rr_uimm5 (VecAluOpRRImm5.VsllVI) vs2 imm mask vstate))
730
731;; Helper for emitting the `srl.vv` instruction.
732(decl rv_vsrl_vv (VReg VReg VecOpMasking VState) VReg)
733(rule (rv_vsrl_vv vs2 vs1 mask vstate)
734  (vec_alu_rrr (VecAluOpRRR.VsrlVV) vs2 vs1 mask vstate))
735
736;; Helper for emitting the `srl.vx` instruction.
737(decl rv_vsrl_vx (VReg XReg VecOpMasking VState) VReg)
738(rule (rv_vsrl_vx vs2 vs1 mask vstate)
739  (vec_alu_rrr (VecAluOpRRR.VsrlVX) vs2 vs1 mask vstate))
740
741;; Helper for emitting the `vsrl.vi` instruction.
742(decl rv_vsrl_vi (VReg UImm5 VecOpMasking VState) VReg)
743(rule (rv_vsrl_vi vs2 imm mask vstate)
744  (vec_alu_rr_uimm5 (VecAluOpRRImm5.VsrlVI) vs2 imm mask vstate))
745
746;; Helper for emitting the `sra.vv` instruction.
747(decl rv_vsra_vv (VReg VReg VecOpMasking VState) VReg)
748(rule (rv_vsra_vv vs2 vs1 mask vstate)
749  (vec_alu_rrr (VecAluOpRRR.VsraVV) vs2 vs1 mask vstate))
750
751;; Helper for emitting the `sra.vx` instruction.
752(decl rv_vsra_vx (VReg XReg VecOpMasking VState) VReg)
753(rule (rv_vsra_vx vs2 vs1 mask vstate)
754  (vec_alu_rrr (VecAluOpRRR.VsraVX) vs2 vs1 mask vstate))
755
756;; Helper for emitting the `vsra.vi` instruction.
757(decl rv_vsra_vi (VReg UImm5 VecOpMasking VState) VReg)
758(rule (rv_vsra_vi vs2 imm mask vstate)
759  (vec_alu_rr_uimm5 (VecAluOpRRImm5.VsraVI) vs2 imm mask vstate))
760
761;; Helper for emitting the `vand.vv` instruction.
762(decl rv_vand_vv (VReg VReg VecOpMasking VState) VReg)
763(rule (rv_vand_vv vs2 vs1 mask vstate)
764  (vec_alu_rrr (VecAluOpRRR.VandVV) vs2 vs1 mask vstate))
765
766;; Helper for emitting the `vand.vx` instruction.
767(decl rv_vand_vx (VReg XReg VecOpMasking VState) VReg)
768(rule (rv_vand_vx vs2 vs1 mask vstate)
769  (vec_alu_rrr (VecAluOpRRR.VandVX) vs2 vs1 mask vstate))
770
771;; Helper for emitting the `vand.vi` instruction.
772(decl rv_vand_vi (VReg Imm5 VecOpMasking VState) VReg)
773(rule (rv_vand_vi vs2 imm mask vstate)
774  (vec_alu_rr_imm5 (VecAluOpRRImm5.VandVI) vs2 imm mask vstate))
775
776;; Helper for emitting the `vor.vv` instruction.
777(decl rv_vor_vv (VReg VReg VecOpMasking VState) VReg)
778(rule (rv_vor_vv vs2 vs1 mask vstate)
779  (vec_alu_rrr (VecAluOpRRR.VorVV) vs2 vs1 mask vstate))
780
781;; Helper for emitting the `vor.vx` instruction.
782(decl rv_vor_vx (VReg XReg VecOpMasking VState) VReg)
783(rule (rv_vor_vx vs2 vs1 mask vstate)
784  (vec_alu_rrr (VecAluOpRRR.VorVX) vs2 vs1 mask vstate))
785
786;; Helper for emitting the `vor.vi` instruction.
787(decl rv_vor_vi (VReg Imm5 VecOpMasking VState) VReg)
788(rule (rv_vor_vi vs2 imm mask vstate)
789  (vec_alu_rr_imm5 (VecAluOpRRImm5.VorVI) vs2 imm mask vstate))
790
791;; Helper for emitting the `vxor.vv` instruction.
792(decl rv_vxor_vv (VReg VReg VecOpMasking VState) VReg)
793(rule (rv_vxor_vv vs2 vs1 mask vstate)
794  (vec_alu_rrr (VecAluOpRRR.VxorVV) vs2 vs1 mask vstate))
795
796;; Helper for emitting the `vxor.vx` instruction.
797(decl rv_vxor_vx (VReg XReg VecOpMasking VState) VReg)
798(rule (rv_vxor_vx vs2 vs1 mask vstate)
799  (vec_alu_rrr (VecAluOpRRR.VxorVX) vs2 vs1 mask vstate))
800
801;; Helper for emitting the `vxor.vi` instruction.
802(decl rv_vxor_vi (VReg Imm5 VecOpMasking VState) VReg)
803(rule (rv_vxor_vi vs2 imm mask vstate)
804  (vec_alu_rr_imm5 (VecAluOpRRImm5.VxorVI) vs2 imm mask vstate))
805
806;; Helper for emitting the `vssrl.vi` instruction.
807;;
808;; vd[i] = (unsigned(vs2[i]) >> imm) + r
809;;
810;; `r` here is the rounding mode currently selected.
811(decl rv_vssrl_vi (VReg UImm5 VecOpMasking VState) VReg)
812(rule (rv_vssrl_vi vs2 imm mask vstate)
813  (vec_alu_rr_uimm5 (VecAluOpRRImm5.VssrlVI) vs2 imm mask vstate))
814
815;; Helper for emitting the `vnot.v` instruction.
816;; This is just a mnemonic for `vxor.vi vd, vs, -1`
817(decl rv_vnot_v (VReg VecOpMasking VState) VReg)
818(rule (rv_vnot_v vs2 mask vstate)
819  (if-let neg1 (i8_to_imm5 -1))
820  (rv_vxor_vi vs2 neg1 mask vstate))
821
822;; Helper for emitting the `vmax.vv` instruction.
823(decl rv_vmax_vv (VReg VReg VecOpMasking VState) VReg)
824(rule (rv_vmax_vv vs2 vs1 mask vstate)
825  (vec_alu_rrr (VecAluOpRRR.VmaxVV) vs2 vs1 mask vstate))
826
827;; Helper for emitting the `vmax.vx` instruction.
828(decl rv_vmax_vx (VReg XReg VecOpMasking VState) VReg)
829(rule (rv_vmax_vx vs2 vs1 mask vstate)
830  (vec_alu_rrr (VecAluOpRRR.VmaxVX) vs2 vs1 mask vstate))
831
832;; Helper for emitting the `vmin.vv` instruction.
833(decl rv_vmin_vv (VReg VReg VecOpMasking VState) VReg)
834(rule (rv_vmin_vv vs2 vs1 mask vstate)
835  (vec_alu_rrr (VecAluOpRRR.VminVV) vs2 vs1 mask vstate))
836
837;; Helper for emitting the `vmin.vx` instruction.
838(decl rv_vmin_vx (VReg XReg VecOpMasking VState) VReg)
839(rule (rv_vmin_vx vs2 vs1 mask vstate)
840  (vec_alu_rrr (VecAluOpRRR.VminVX) vs2 vs1 mask vstate))
841
842;; Helper for emitting the `vmaxu.vv` instruction.
843(decl rv_vmaxu_vv (VReg VReg VecOpMasking VState) VReg)
844(rule (rv_vmaxu_vv vs2 vs1 mask vstate)
845  (vec_alu_rrr (VecAluOpRRR.VmaxuVV) vs2 vs1 mask vstate))
846
847;; Helper for emitting the `vmaxu.vx` instruction.
848(decl rv_vmaxu_vx (VReg XReg VecOpMasking VState) VReg)
849(rule (rv_vmaxu_vx vs2 vs1 mask vstate)
850  (vec_alu_rrr (VecAluOpRRR.VmaxuVX) vs2 vs1 mask vstate))
851
852;; Helper for emitting the `vminu.vv` instruction.
853(decl rv_vminu_vv (VReg VReg VecOpMasking VState) VReg)
854(rule (rv_vminu_vv vs2 vs1 mask vstate)
855  (vec_alu_rrr (VecAluOpRRR.VminuVV) vs2 vs1 mask vstate))
856
857;; Helper for emitting the `vminu.vx` instruction.
858(decl rv_vminu_vx (VReg XReg VecOpMasking VState) VReg)
859(rule (rv_vminu_vx vs2 vs1 mask vstate)
860  (vec_alu_rrr (VecAluOpRRR.VminuVX) vs2 vs1 mask vstate))
861
862;; Helper for emitting the `vfadd.vv` instruction.
863(decl rv_vfadd_vv (VReg VReg VecOpMasking VState) VReg)
864(rule (rv_vfadd_vv vs2 vs1 mask vstate)
865  (vec_alu_rrr (VecAluOpRRR.VfaddVV) vs2 vs1 mask vstate))
866
867;; Helper for emitting the `vfadd.vf` instruction.
868(decl rv_vfadd_vf (VReg FReg VecOpMasking VState) VReg)
869(rule (rv_vfadd_vf vs2 vs1 mask vstate)
870  (vec_alu_rrr (VecAluOpRRR.VfaddVF) vs2 vs1 mask vstate))
871
872;; Helper for emitting the `vfsub.vv` instruction.
873(decl rv_vfsub_vv (VReg VReg VecOpMasking VState) VReg)
874(rule (rv_vfsub_vv vs2 vs1 mask vstate)
875  (vec_alu_rrr (VecAluOpRRR.VfsubVV) vs2 vs1 mask vstate))
876
877;; Helper for emitting the `vfsub.vf` instruction.
878(decl rv_vfsub_vf (VReg FReg VecOpMasking VState) VReg)
879(rule (rv_vfsub_vf vs2 vs1 mask vstate)
880  (vec_alu_rrr (VecAluOpRRR.VfsubVF) vs2 vs1 mask vstate))
881
882;; Helper for emitting the `vfrsub.vf` instruction.
883(decl rv_vfrsub_vf (VReg FReg VecOpMasking VState) VReg)
884(rule (rv_vfrsub_vf vs2 vs1 mask vstate)
885  (vec_alu_rrr (VecAluOpRRR.VfrsubVF) vs2 vs1 mask vstate))
886
887;; Helper for emitting the `vfmul.vv` instruction.
888(decl rv_vfmul_vv (VReg VReg VecOpMasking VState) VReg)
889(rule (rv_vfmul_vv vs2 vs1 mask vstate)
890  (vec_alu_rrr (VecAluOpRRR.VfmulVV) vs2 vs1 mask vstate))
891
892;; Helper for emitting the `vfmul.vf` instruction.
893(decl rv_vfmul_vf (VReg FReg VecOpMasking VState) VReg)
894(rule (rv_vfmul_vf vs2 vs1 mask vstate)
895  (vec_alu_rrr (VecAluOpRRR.VfmulVF) vs2 vs1 mask vstate))
896
897;; Helper for emitting the `vfmacc.vv` instruction.
898;;
899;; FP multiply-accumulate, overwrites addend
900;; # vd[i] = +(vs1[i] * vs2[i]) + vd[i]
901(decl rv_vfmacc_vv (VReg VReg VReg VecOpMasking VState) VReg)
902(rule (rv_vfmacc_vv vd vs2 vs1 mask vstate)
903  (vec_alu_rrrr (VecAluOpRRRR.VfmaccVV) vd vs2 vs1 mask vstate))
904
905;; Helper for emitting the `vfmacc.vf` instruction.
906;;
907;; FP multiply-accumulate, overwrites addend
908;; # vd[i] = +(f[rs1] * vs2[i]) + vd[i]
909(decl rv_vfmacc_vf (VReg VReg FReg VecOpMasking VState) VReg)
910(rule (rv_vfmacc_vf vd vs2 vs1 mask vstate)
911  (vec_alu_rrrr (VecAluOpRRRR.VfmaccVF) vd vs2 vs1 mask vstate))
912
913;; Helper for emitting the `vfnmacc.vv` instruction.
914;;
915;; FP negate-(multiply-accumulate), overwrites subtrahend
916;; # vd[i] = -(vs1[i] * vs2[i]) - vd[i]
917(decl rv_vfnmacc_vv (VReg VReg VReg VecOpMasking VState) VReg)
918(rule (rv_vfnmacc_vv vd vs2 vs1 mask vstate)
919  (vec_alu_rrrr (VecAluOpRRRR.VfnmaccVV) vd vs2 vs1 mask vstate))
920
921;; Helper for emitting the `vfnmacc.vf` instruction.
922;;
923;; FP negate-(multiply-accumulate), overwrites subtrahend
924;; # vd[i] = -(f[rs1] * vs2[i]) - vd[i]
925(decl rv_vfnmacc_vf (VReg VReg FReg VecOpMasking VState) VReg)
926(rule (rv_vfnmacc_vf vd vs2 vs1 mask vstate)
927  (vec_alu_rrrr (VecAluOpRRRR.VfnmaccVF) vd vs2 vs1 mask vstate))
928
929;; Helper for emitting the `vfmsac.vv` instruction.
930;;
931;; FP multiply-subtract-accumulator, overwrites subtrahend
932;; # vd[i] = +(vs1[i] * vs2[i]) - vd[i]
933(decl rv_vfmsac_vv (VReg VReg VReg VecOpMasking VState) VReg)
934(rule (rv_vfmsac_vv vd vs2 vs1 mask vstate)
935  (vec_alu_rrrr (VecAluOpRRRR.VfmsacVV) vd vs2 vs1 mask vstate))
936
937;; Helper for emitting the `vfmsac.vf` instruction.
938;;
939;; FP multiply-subtract-accumulator, overwrites subtrahend
940;; # vd[i] = +(f[rs1] * vs2[i]) - vd[i]
941(decl rv_vfmsac_vf (VReg VReg FReg VecOpMasking VState) VReg)
942(rule (rv_vfmsac_vf vd vs2 vs1 mask vstate)
943  (vec_alu_rrrr (VecAluOpRRRR.VfmsacVF) vd vs2 vs1 mask vstate))
944
945;; Helper for emitting the `vfnmsac.vv` instruction.
946;;
947;; FP negate-(multiply-subtract-accumulator), overwrites minuend
948;; # vd[i] = -(vs1[i] * vs2[i]) + vd[i]
949(decl rv_vfnmsac_vv (VReg VReg VReg VecOpMasking VState) VReg)
950(rule (rv_vfnmsac_vv vd vs2 vs1 mask vstate)
951  (vec_alu_rrrr (VecAluOpRRRR.VfnmsacVV) vd vs2 vs1 mask vstate))
952
953;; Helper for emitting the `vfnmsac.vf` instruction.
954;;
955;; FP negate-(multiply-subtract-accumulator), overwrites minuend
956;; # vd[i] = -(f[rs1] * vs2[i]) + vd[i]
957(decl rv_vfnmsac_vf (VReg VReg FReg VecOpMasking VState) VReg)
958(rule (rv_vfnmsac_vf vd vs2 vs1 mask vstate)
959  (vec_alu_rrrr (VecAluOpRRRR.VfnmsacVF) vd vs2 vs1 mask vstate))
960
961;; Helper for emitting the `vfdiv.vv` instruction.
962(decl rv_vfdiv_vv (VReg VReg VecOpMasking VState) VReg)
963(rule (rv_vfdiv_vv vs2 vs1 mask vstate)
964  (vec_alu_rrr (VecAluOpRRR.VfdivVV) vs2 vs1 mask vstate))
965
966;; Helper for emitting the `vfdiv.vf` instruction.
967(decl rv_vfdiv_vf (VReg FReg VecOpMasking VState) VReg)
968(rule (rv_vfdiv_vf vs2 vs1 mask vstate)
969  (vec_alu_rrr (VecAluOpRRR.VfdivVF) vs2 vs1 mask vstate))
970
971;; Helper for emitting the `vfrdiv.vf` instruction.
972(decl rv_vfrdiv_vf (VReg FReg VecOpMasking VState) VReg)
973(rule (rv_vfrdiv_vf vs2 vs1 mask vstate)
974  (vec_alu_rrr (VecAluOpRRR.VfrdivVF) vs2 vs1 mask vstate))
975
976;; Helper for emitting the `vfmin.vv` instruction.
977(decl rv_vfmin_vv (VReg VReg VecOpMasking VState) VReg)
978(rule (rv_vfmin_vv vs2 vs1 mask vstate)
979  (vec_alu_rrr (VecAluOpRRR.VfminVV) vs2 vs1 mask vstate))
980
981;; Helper for emitting the `vfmax.vv` instruction.
982(decl rv_vfmax_vv (VReg VReg VecOpMasking VState) VReg)
983(rule (rv_vfmax_vv vs2 vs1 mask vstate)
984  (vec_alu_rrr (VecAluOpRRR.VfmaxVV) vs2 vs1 mask vstate))
985
986;; Helper for emitting the `vfsgnj.vv` ("Floating Point Sign Injection") instruction.
987;; The output of this instruction is `vs2` with the sign bit from `vs1`
988(decl rv_vfsgnj_vv (VReg VReg VecOpMasking VState) VReg)
989(rule (rv_vfsgnj_vv vs2 vs1 mask vstate)
990  (vec_alu_rrr (VecAluOpRRR.VfsgnjVV) vs2 vs1 mask vstate))
991
992;; Helper for emitting the `vfsgnj.vf` ("Floating Point Sign Injection") instruction.
993(decl rv_vfsgnj_vf (VReg FReg VecOpMasking VState) VReg)
994(rule (rv_vfsgnj_vf vs2 vs1 mask vstate)
995  (vec_alu_rrr (VecAluOpRRR.VfsgnjVF) vs2 vs1 mask vstate))
996
997;; Helper for emitting the `vfsgnjn.vv` ("Floating Point Sign Injection Negated") instruction.
998;; The output of this instruction is `vs2` with the negated sign bit from `vs1`
999(decl rv_vfsgnjn_vv (VReg VReg VecOpMasking VState) VReg)
1000(rule (rv_vfsgnjn_vv vs2 vs1 mask vstate)
1001  (vec_alu_rrr (VecAluOpRRR.VfsgnjnVV) vs2 vs1 mask vstate))
1002
1003;; Helper for emitting the `vfneg.v` instruction.
1004;; This instruction is a mnemonic for `vfsgnjn.vv vd, vs, vs`
1005(decl rv_vfneg_v (VReg VecOpMasking VState) VReg)
1006(rule (rv_vfneg_v vs mask vstate) (rv_vfsgnjn_vv vs vs mask vstate))
1007
1008;; Helper for emitting the `vfsgnjx.vv` ("Floating Point Sign Injection Exclusive") instruction.
1009;; The output of this instruction is `vs2` with the XOR of the sign bits from `vs2` and `vs1`.
1010;; When `vs2 == vs1` this implements `fabs`
1011(decl rv_vfsgnjx_vv (VReg VReg VecOpMasking VState) VReg)
1012(rule (rv_vfsgnjx_vv vs2 vs1 mask vstate)
1013  (vec_alu_rrr (VecAluOpRRR.VfsgnjxVV) vs2 vs1 mask vstate))
1014
1015;; Helper for emitting the `vfabs.v` instruction.
1016;; This instruction is a mnemonic for `vfsgnjx.vv vd, vs, vs`
1017(decl rv_vfabs_v (VReg VecOpMasking VState) VReg)
1018(rule (rv_vfabs_v vs mask vstate) (rv_vfsgnjx_vv vs vs mask vstate))
1019
1020;; Helper for emitting the `vfsqrt.v` instruction.
1021;; This instruction splats the F register into all elements of the destination vector.
1022(decl rv_vfsqrt_v (VReg VecOpMasking VState) VReg)
1023(rule (rv_vfsqrt_v vs mask vstate)
1024  (vec_alu_rr (VecAluOpRR.VfsqrtV) vs mask vstate))
1025
1026;; Helper for emitting the `vfcvt.xu.f.v` instruction.
1027;; This instruction converts a float to an unsigned integer.
1028(decl rv_vfcvt_xu_f_v (VReg VecOpMasking VState) VReg)
1029(rule (rv_vfcvt_xu_f_v vs mask vstate)
1030  (vec_alu_rr (VecAluOpRR.VfcvtxufV) vs mask vstate))
1031
1032;; Helper for emitting the `vfcvt.x.f.v` instruction.
1033;; This instruction converts a float to a signed integer.
1034(decl rv_vfcvt_x_f_v (VReg VecOpMasking VState) VReg)
1035(rule (rv_vfcvt_x_f_v vs mask vstate)
1036  (vec_alu_rr (VecAluOpRR.VfcvtxfV) vs mask vstate))
1037
1038;; Helper for emitting the `vfcvt.rtz.xu.f.v` instruction.
1039;; This instruction converts a float to an unsigned integer
1040;; using the Round to Zero (RTZ) rounding mode and ignoring
1041;; the currently set FRM rounding mode.
1042(decl rv_vfcvt_rtz_xu_f_v (VReg VecOpMasking VState) VReg)
1043(rule (rv_vfcvt_rtz_xu_f_v vs mask vstate)
1044  (vec_alu_rr (VecAluOpRR.VfcvtrtzxufV) vs mask vstate))
1045
1046;; Helper for emitting the `vfcvt.rtz.x.f.v` instruction.
1047;; This instruction converts a float to a signed integer.
1048;; using the Round to Zero (RTZ) rounding mode and ignoring
1049;; the currently set FRM rounding mode.
1050(decl rv_vfcvt_rtz_x_f_v (VReg VecOpMasking VState) VReg)
1051(rule (rv_vfcvt_rtz_x_f_v vs mask vstate)
1052  (vec_alu_rr (VecAluOpRR.VfcvtrtzxfV) vs mask vstate))
1053
1054;; Helper for emitting the `vfcvt.f.xu.v` instruction.
1055;; This instruction converts a unsigned integer to a float.
1056(decl rv_vfcvt_f_xu_v (VReg VecOpMasking VState) VReg)
1057(rule (rv_vfcvt_f_xu_v vs mask vstate)
1058  (vec_alu_rr (VecAluOpRR.VfcvtfxuV) vs mask vstate))
1059
1060;; Helper for emitting the `vfcvt.x.f.v` instruction.
1061;; This instruction converts a signed integer to a float.
1062(decl rv_vfcvt_f_x_v (VReg VecOpMasking VState) VReg)
1063(rule (rv_vfcvt_f_x_v vs mask vstate)
1064  (vec_alu_rr (VecAluOpRR.VfcvtfxV) vs mask vstate))
1065
1066  ;; Helper for emitting the `vfwcvt.f.f.v` instruction.
1067;; Convert single-width float to double-width float.
1068(decl rv_vfwcvt_f_f_v (VReg VecOpMasking VState) VReg)
1069(rule (rv_vfwcvt_f_f_v vs mask vstate)
1070  (vec_alu_rr (VecAluOpRR.VfwcvtffV) vs mask vstate))
1071
1072;; Helper for emitting the `vfncvt.f.f.w` instruction.
1073;; Convert double-width float to single-width float.
1074(decl rv_vfncvt_f_f_w (VReg VecOpMasking VState) VReg)
1075(rule (rv_vfncvt_f_f_w vs mask vstate)
1076  (vec_alu_rr (VecAluOpRR.VfncvtffW) vs mask vstate))
1077
1078;; Helper for emitting the `vslidedown.vx` instruction.
1079;; `vslidedown` moves all elements in the vector down by n elements.
1080;; The top most elements are up to the tail policy.
1081(decl rv_vslidedown_vx (VReg XReg VecOpMasking VState) VReg)
1082(rule (rv_vslidedown_vx vs2 vs1 mask vstate)
1083  (vec_alu_rrr (VecAluOpRRR.VslidedownVX) vs2 vs1 mask vstate))
1084
1085;; Helper for emitting the `vslidedown.vi` instruction.
1086;; Unlike other `vi` instructions the immediate is zero extended.
1087(decl rv_vslidedown_vi (VReg UImm5 VecOpMasking VState) VReg)
1088(rule (rv_vslidedown_vi vs2 imm mask vstate)
1089  (vec_alu_rr_uimm5 (VecAluOpRRImm5.VslidedownVI) vs2 imm mask vstate))
1090
1091;; Helper for emitting the `vslideup.vi` instruction.
1092;; Unlike other `vi` instructions the immediate is zero extended.
1093;; This is implemented as a 2 source operand instruction, since it only
1094;; partially modifies the destination register.
1095(decl rv_vslideup_vvi (VReg VReg UImm5 VecOpMasking VState) VReg)
1096(rule (rv_vslideup_vvi vd vs2 imm mask vstate)
1097  (vec_alu_rrr_uimm5 (VecAluOpRRRImm5.VslideupVI) vd vs2 imm mask vstate))
1098
1099;; Helper for emitting the `vslide1up.vx` instruction.
1100;;
1101;; # vd[0]=x[rs1], vd[i+1] = vs2[i]
1102(decl rv_vslide1up_vx (VReg VReg XReg VecOpMasking VState) VReg)
1103(rule (rv_vslide1up_vx vd vs2 rs1 mask vstate)
1104  (vec_alu_rrrr (VecAluOpRRRR.Vslide1upVX) vd vs2 rs1 mask vstate))
1105
1106;; Helper for emitting the `vmv.x.s` instruction.
1107;; This instruction copies the first element of the source vector to the destination X register.
1108;; Masked versions of this instruction are not supported.
1109(decl rv_vmv_xs (VReg VState) XReg)
1110(rule (rv_vmv_xs vs vstate)
1111  (vec_alu_rr (VecAluOpRR.VmvXS) vs (unmasked) vstate))
1112
1113;; Helper for emitting the `vfmv.f.s` instruction.
1114;; This instruction copies the first element of the source vector to the destination F register.
1115;; Masked versions of this instruction are not supported.
1116(decl rv_vfmv_fs (VReg VState) FReg)
1117(rule (rv_vfmv_fs vs vstate)
1118  (vec_alu_rr (VecAluOpRR.VfmvFS) vs (unmasked) vstate))
1119
1120;; Helper for emitting the `vmv.s.x` instruction.
1121;; This instruction copies the source X register into first element of the source vector.
1122;; Masked versions of this instruction are not supported.
1123(decl rv_vmv_sx (XReg VState) VReg)
1124(rule (rv_vmv_sx vs vstate)
1125  (vec_alu_rr (VecAluOpRR.VmvSX) vs (unmasked) vstate))
1126
1127;; Helper for emitting the `vfmv.s.f` instruction.
1128;; This instruction copies the source F register into first element of the source vector.
1129;; Masked versions of this instruction are not supported.
1130(decl rv_vfmv_sf (FReg VState) VReg)
1131(rule (rv_vfmv_sf vs vstate)
1132  (vec_alu_rr (VecAluOpRR.VfmvSF) vs (unmasked) vstate))
1133
1134;; Helper for emitting the `vmv.v.x` instruction.
1135;; This instruction splats the X register into all elements of the destination vector.
1136;; Masked versions of this instruction are called `vmerge`
1137(decl rv_vmv_vx (XReg VState) VReg)
1138(rule (rv_vmv_vx vs vstate)
1139  (vec_alu_rr (VecAluOpRR.VmvVX) vs (unmasked) vstate))
1140
1141;; Helper for emitting the `vfmv.v.f` instruction.
1142;; This instruction splats the F register into all elements of the destination vector.
1143;; Masked versions of this instruction are called `vmerge`
1144(decl rv_vfmv_vf (FReg VState) VReg)
1145(rule (rv_vfmv_vf vs vstate)
1146  (vec_alu_rr (VecAluOpRR.VfmvVF) vs (unmasked) vstate))
1147
1148;; Helper for emitting the `vmv.v.i` instruction.
1149;; This instruction splat's the immediate value into all elements of the destination vector.
1150;; Masked versions of this instruction are called `vmerge`
1151(decl rv_vmv_vi (Imm5 VState) VReg)
1152(rule (rv_vmv_vi imm vstate)
1153  (vec_alu_r_imm5 (VecAluOpRImm5.VmvVI) imm (unmasked) vstate))
1154
1155;; Helper for emitting the `vmerge.vvm` instruction.
1156;; This instruction merges the elements of the two source vectors into the destination vector
1157;; based on a mask. Elements are taken from the first source vector if the mask bit is clear,
1158;; and from the second source vector if the mask bit is set. This instruction is always masked.
1159;;
1160;; vd[i] = v0.mask[i] ? vs1[i] : vs2[i]
1161(decl rv_vmerge_vvm (VReg VReg VReg VState) VReg)
1162(rule (rv_vmerge_vvm vs2 vs1 mask vstate)
1163  (vec_alu_rrr (VecAluOpRRR.VmergeVVM) vs2 vs1 (masked mask) vstate))
1164
1165;; Helper for emitting the `vmerge.vxm` instruction.
1166;; Elements are taken from the first source vector if the mask bit is clear, and from the X
1167;; register if the mask bit is set. This instruction is always masked.
1168;;
1169;; vd[i] = v0.mask[i] ? x[rs1] : vs2[i]
1170(decl rv_vmerge_vxm (VReg XReg VReg VState) VReg)
1171(rule (rv_vmerge_vxm vs2 vs1 mask vstate)
1172  (vec_alu_rrr (VecAluOpRRR.VmergeVXM) vs2 vs1 (masked mask) vstate))
1173
1174;; Helper for emitting the `vfmerge.vfm` instruction.
1175;; Elements are taken from the first source vector if the mask bit is clear, and from the F
1176;; register if the mask bit is set. This instruction is always masked.
1177;;
1178;; vd[i] = v0.mask[i] ? f[rs1] : vs2[i]
1179(decl rv_vfmerge_vfm (VReg FReg VReg VState) VReg)
1180(rule (rv_vfmerge_vfm vs2 vs1 mask vstate)
1181  (vec_alu_rrr (VecAluOpRRR.VfmergeVFM) vs2 vs1 (masked mask) vstate))
1182
1183;; Helper for emitting the `vmerge.vim` instruction.
1184;; Elements are taken from the first source vector if the mask bit is clear, and from the
1185;; immediate value if the mask bit is set. This instruction is always masked.
1186;;
1187;; vd[i] = v0.mask[i] ? imm : vs2[i]
1188(decl rv_vmerge_vim (VReg Imm5 VReg VState) VReg)
1189(rule (rv_vmerge_vim vs2 imm mask vstate)
1190  (vec_alu_rr_imm5 (VecAluOpRRImm5.VmergeVIM) vs2 imm (masked mask) vstate))
1191
1192
1193;; Helper for emitting the `vredminu.vs` instruction.
1194;;
1195;; vd[0] = minu( vs1[0] , vs2[*] )
1196(decl rv_vredminu_vs (VReg VReg VecOpMasking VState) VReg)
1197(rule (rv_vredminu_vs vs2 vs1 mask vstate)
1198  (vec_alu_rrr (VecAluOpRRR.VredminuVS) vs2 vs1 mask vstate))
1199
1200;; Helper for emitting the `vredmaxu.vs` instruction.
1201;;
1202;; vd[0] = maxu( vs1[0] , vs2[*] )
1203(decl rv_vredmaxu_vs (VReg VReg VecOpMasking VState) VReg)
1204(rule (rv_vredmaxu_vs vs2 vs1 mask vstate)
1205  (vec_alu_rrr (VecAluOpRRR.VredmaxuVS) vs2 vs1 mask vstate))
1206
1207;; Helper for emitting the `vrgather.vv` instruction.
1208;;
1209;; vd[i] = (vs1[i] >= VLMAX) ? 0 : vs2[vs1[i]];
1210(decl rv_vrgather_vv (VReg VReg VecOpMasking VState) VReg)
1211(rule (rv_vrgather_vv vs2 vs1 mask vstate)
1212  (vec_alu_rrr (VecAluOpRRR.VrgatherVV) vs2 vs1 mask vstate))
1213
1214;; Helper for emitting the `vrgather.vx` instruction.
1215;;
1216;; vd[i] = (x[rs1] >= VLMAX) ? 0 : vs2[x[rs1]]
1217(decl rv_vrgather_vx (VReg XReg VecOpMasking VState) VReg)
1218(rule (rv_vrgather_vx vs2 vs1 mask vstate)
1219  (vec_alu_rrr (VecAluOpRRR.VrgatherVX) vs2 vs1 mask vstate))
1220
1221;; Helper for emitting the `vrgather.vi` instruction.
1222(decl rv_vrgather_vi (VReg UImm5 VecOpMasking VState) VReg)
1223(rule (rv_vrgather_vi vs2 imm mask vstate)
1224  (vec_alu_rr_uimm5 (VecAluOpRRImm5.VrgatherVI) vs2 imm mask vstate))
1225
1226;; Helper for emitting the `vcompress.vm` instruction.
1227;;
1228;; The vector compress instruction allows elements selected by a vector mask
1229;; register from a source vector register group to be packed into contiguous
1230;; elements at the start of the destination vector register group.
1231;;
1232;; The mask register is specified through vs1
1233(decl rv_vcompress_vm (VReg VReg VState) VReg)
1234(rule (rv_vcompress_vm vs2 vs1 vstate)
1235  (vec_alu_rrr (VecAluOpRRR.VcompressVM) vs2 vs1 (unmasked) vstate))
1236
1237;; Helper for emitting the `vmseq.vv` (Vector Mask Set If Equal) instruction.
1238(decl rv_vmseq_vv (VReg VReg VecOpMasking VState) VReg)
1239(rule (rv_vmseq_vv vs2 vs1 mask vstate)
1240  (vec_alu_rrr (VecAluOpRRR.VmseqVV) vs2 vs1 mask vstate))
1241
1242;; Helper for emitting the `vmseq.vx` (Vector Mask Set If Equal) instruction.
1243(decl rv_vmseq_vx (VReg XReg VecOpMasking VState) VReg)
1244(rule (rv_vmseq_vx vs2 vs1 mask vstate)
1245  (vec_alu_rrr (VecAluOpRRR.VmseqVX) vs2 vs1 mask vstate))
1246
1247;; Helper for emitting the `vmseq.vi` (Vector Mask Set If Equal) instruction.
1248(decl rv_vmseq_vi (VReg Imm5 VecOpMasking VState) VReg)
1249(rule (rv_vmseq_vi vs2 imm mask vstate)
1250  (vec_alu_rr_imm5 (VecAluOpRRImm5.VmseqVI) vs2 imm mask vstate))
1251
1252;; Helper for emitting the `vmsne.vv` (Vector Mask Set If Not Equal) instruction.
1253(decl rv_vmsne_vv (VReg VReg VecOpMasking VState) VReg)
1254(rule (rv_vmsne_vv vs2 vs1 mask vstate)
1255  (vec_alu_rrr (VecAluOpRRR.VmsneVV) vs2 vs1 mask vstate))
1256
1257;; Helper for emitting the `vmsne.vx` (Vector Mask Set If Not Equal) instruction.
1258(decl rv_vmsne_vx (VReg XReg VecOpMasking VState) VReg)
1259(rule (rv_vmsne_vx vs2 vs1 mask vstate)
1260  (vec_alu_rrr (VecAluOpRRR.VmsneVX) vs2 vs1 mask vstate))
1261
1262;; Helper for emitting the `vmsne.vi` (Vector Mask Set If Not Equal) instruction.
1263(decl rv_vmsne_vi (VReg Imm5 VecOpMasking VState) VReg)
1264(rule (rv_vmsne_vi vs2 imm mask vstate)
1265  (vec_alu_rr_imm5 (VecAluOpRRImm5.VmsneVI) vs2 imm mask vstate))
1266
1267;; Helper for emitting the `vmsltu.vv` (Vector Mask Set If Less Than, Unsigned) instruction.
1268(decl rv_vmsltu_vv (VReg VReg VecOpMasking VState) VReg)
1269(rule (rv_vmsltu_vv vs2 vs1 mask vstate)
1270  (vec_alu_rrr (VecAluOpRRR.VmsltuVV) vs2 vs1 mask vstate))
1271
1272;; Helper for emitting the `vmsltu.vx` (Vector Mask Set If Less Than, Unsigned) instruction.
1273(decl rv_vmsltu_vx (VReg XReg VecOpMasking VState) VReg)
1274(rule (rv_vmsltu_vx vs2 vs1 mask vstate)
1275  (vec_alu_rrr (VecAluOpRRR.VmsltuVX) vs2 vs1 mask vstate))
1276
1277;; Helper for emitting the `vmslt.vv` (Vector Mask Set If Less Than) instruction.
1278(decl rv_vmslt_vv (VReg VReg VecOpMasking VState) VReg)
1279(rule (rv_vmslt_vv vs2 vs1 mask vstate)
1280  (vec_alu_rrr (VecAluOpRRR.VmsltVV) vs2 vs1 mask vstate))
1281
1282;; Helper for emitting the `vmslt.vx` (Vector Mask Set If Less Than) instruction.
1283(decl rv_vmslt_vx (VReg XReg VecOpMasking VState) VReg)
1284(rule (rv_vmslt_vx vs2 vs1 mask vstate)
1285  (vec_alu_rrr (VecAluOpRRR.VmsltVX) vs2 vs1 mask vstate))
1286
1287;; Helper for emitting the `vmsleu.vv` (Vector Mask Set If Less Than or Equal, Unsigned) instruction.
1288(decl rv_vmsleu_vv (VReg VReg VecOpMasking VState) VReg)
1289(rule (rv_vmsleu_vv vs2 vs1 mask vstate)
1290  (vec_alu_rrr (VecAluOpRRR.VmsleuVV) vs2 vs1 mask vstate))
1291
1292;; Helper for emitting the `vmsleu.vx` (Vector Mask Set If Less Than or Equal, Unsigned) instruction.
1293(decl rv_vmsleu_vx (VReg XReg VecOpMasking VState) VReg)
1294(rule (rv_vmsleu_vx vs2 vs1 mask vstate)
1295  (vec_alu_rrr (VecAluOpRRR.VmsleuVX) vs2 vs1 mask vstate))
1296
1297;; Helper for emitting the `vmsleu.vi` (Vector Mask Set If Less Than or Equal, Unsigned) instruction.
1298(decl rv_vmsleu_vi (VReg Imm5 VecOpMasking VState) VReg)
1299(rule (rv_vmsleu_vi vs2 imm mask vstate)
1300  (vec_alu_rr_imm5 (VecAluOpRRImm5.VmsleuVI) vs2 imm mask vstate))
1301
1302;; Helper for emitting the `vmsle.vv` (Vector Mask Set If Less Than or Equal) instruction.
1303(decl rv_vmsle_vv (VReg VReg VecOpMasking VState) VReg)
1304(rule (rv_vmsle_vv vs2 vs1 mask vstate)
1305  (vec_alu_rrr (VecAluOpRRR.VmsleVV) vs2 vs1 mask vstate))
1306
1307;; Helper for emitting the `vmsle.vx` (Vector Mask Set If Less Than or Equal) instruction.
1308(decl rv_vmsle_vx (VReg XReg VecOpMasking VState) VReg)
1309(rule (rv_vmsle_vx vs2 vs1 mask vstate)
1310  (vec_alu_rrr (VecAluOpRRR.VmsleVX) vs2 vs1 mask vstate))
1311
1312;; Helper for emitting the `vmsle.vi` (Vector Mask Set If Less Than or Equal) instruction.
1313(decl rv_vmsle_vi (VReg Imm5 VecOpMasking VState) VReg)
1314(rule (rv_vmsle_vi vs2 imm mask vstate)
1315  (vec_alu_rr_imm5 (VecAluOpRRImm5.VmsleVI) vs2 imm mask vstate))
1316
1317;; Helper for emitting the `vmsgt.vv` (Vector Mask Set If Greater Than, Unsigned) instruction.
1318;; This is an alias for `vmsltu.vv` with the operands inverted.
1319(decl rv_vmsgtu_vv (VReg VReg VecOpMasking VState) VReg)
1320(rule (rv_vmsgtu_vv vs2 vs1 mask vstate) (rv_vmsltu_vv vs1 vs2 mask vstate))
1321
1322;; Helper for emitting the `vmsgtu.vx` (Vector Mask Set If Greater Than, Unsigned) instruction.
1323(decl rv_vmsgtu_vx (VReg XReg VecOpMasking VState) VReg)
1324(rule (rv_vmsgtu_vx vs2 vs1 mask vstate)
1325  (vec_alu_rrr (VecAluOpRRR.VmsgtuVX) vs2 vs1 mask vstate))
1326
1327;; Helper for emitting the `vmsgtu.vi` (Vector Mask Set If Greater Than, Unsigned) instruction.
1328(decl rv_vmsgtu_vi (VReg Imm5 VecOpMasking VState) VReg)
1329(rule (rv_vmsgtu_vi vs2 imm mask vstate)
1330  (vec_alu_rr_imm5 (VecAluOpRRImm5.VmsgtuVI) vs2 imm mask vstate))
1331
1332;; Helper for emitting the `vmsgt.vv` (Vector Mask Set If Greater Than) instruction.
1333;; This is an alias for `vmslt.vv` with the operands inverted.
1334(decl rv_vmsgt_vv (VReg VReg VecOpMasking VState) VReg)
1335(rule (rv_vmsgt_vv vs2 vs1 mask vstate) (rv_vmslt_vv vs1 vs2 mask vstate))
1336
1337;; Helper for emitting the `vmsgt.vx` (Vector Mask Set If Greater Than) instruction.
1338(decl rv_vmsgt_vx (VReg XReg VecOpMasking VState) VReg)
1339(rule (rv_vmsgt_vx vs2 vs1 mask vstate)
1340  (vec_alu_rrr (VecAluOpRRR.VmsgtVX) vs2 vs1 mask vstate))
1341
1342;; Helper for emitting the `vmsgt.vi` (Vector Mask Set If Greater Than) instruction.
1343(decl rv_vmsgt_vi (VReg Imm5 VecOpMasking VState) VReg)
1344(rule (rv_vmsgt_vi vs2 imm mask vstate)
1345  (vec_alu_rr_imm5 (VecAluOpRRImm5.VmsgtVI) vs2 imm mask vstate))
1346
1347;; Helper for emitting the `vmsgeu.vv` (Vector Mask Set If Greater Than or Equal, Unsigned) instruction.
1348;; This is an alias for `vmsleu.vv` with the operands inverted.
1349(decl rv_vmsgeu_vv (VReg VReg VecOpMasking VState) VReg)
1350(rule (rv_vmsgeu_vv vs2 vs1 mask vstate) (rv_vmsleu_vv vs1 vs2 mask vstate))
1351
1352;; Helper for emitting the `vmsge.vv` (Vector Mask Set If Greater Than or Equal) instruction.
1353;; This is an alias for `vmsle.vv` with the operands inverted.
1354(decl rv_vmsge_vv (VReg VReg VecOpMasking VState) VReg)
1355(rule (rv_vmsge_vv vs2 vs1 mask vstate) (rv_vmsle_vv vs1 vs2 mask vstate))
1356
1357;; Helper for emitting the `vmfeq.vv` (Vector Mask Set If Float Equal) instruction.
1358(decl rv_vmfeq_vv (VReg VReg VecOpMasking VState) VReg)
1359(rule (rv_vmfeq_vv vs2 vs1 mask vstate)
1360  (vec_alu_rrr (VecAluOpRRR.VmfeqVV) vs2 vs1 mask vstate))
1361
1362;; Helper for emitting the `vmfeq.vf` (Vector Mask Set If Float Equal) instruction.
1363(decl rv_vmfeq_vf (VReg FReg VecOpMasking VState) VReg)
1364(rule (rv_vmfeq_vf vs2 vs1 mask vstate)
1365  (vec_alu_rrr (VecAluOpRRR.VmfeqVF) vs2 vs1 mask vstate))
1366
1367;; Helper for emitting the `vmfne.vv` (Vector Mask Set If Float Not Equal) instruction.
1368(decl rv_vmfne_vv (VReg VReg VecOpMasking VState) VReg)
1369(rule (rv_vmfne_vv vs2 vs1 mask vstate)
1370  (vec_alu_rrr (VecAluOpRRR.VmfneVV) vs2 vs1 mask vstate))
1371
1372;; Helper for emitting the `vmfne.vf` (Vector Mask Set If Float Not Equal) instruction.
1373(decl rv_vmfne_vf (VReg FReg VecOpMasking VState) VReg)
1374(rule (rv_vmfne_vf vs2 vs1 mask vstate)
1375  (vec_alu_rrr (VecAluOpRRR.VmfneVF) vs2 vs1 mask vstate))
1376
1377;; Helper for emitting the `vmflt.vv` (Vector Mask Set If Float Less Than) instruction.
1378(decl rv_vmflt_vv (VReg VReg VecOpMasking VState) VReg)
1379(rule (rv_vmflt_vv vs2 vs1 mask vstate)
1380  (vec_alu_rrr (VecAluOpRRR.VmfltVV) vs2 vs1 mask vstate))
1381
1382;; Helper for emitting the `vmflt.vf` (Vector Mask Set If Float Less Than) instruction.
1383(decl rv_vmflt_vf (VReg FReg VecOpMasking VState) VReg)
1384(rule (rv_vmflt_vf vs2 vs1 mask vstate)
1385  (vec_alu_rrr (VecAluOpRRR.VmfltVF) vs2 vs1 mask vstate))
1386
1387;; Helper for emitting the `vmfle.vv` (Vector Mask Set If Float Less Than Or Equal) instruction.
1388(decl rv_vmfle_vv (VReg VReg VecOpMasking VState) VReg)
1389(rule (rv_vmfle_vv vs2 vs1 mask vstate)
1390  (vec_alu_rrr (VecAluOpRRR.VmfleVV) vs2 vs1 mask vstate))
1391
1392;; Helper for emitting the `vmfle.vf` (Vector Mask Set If Float Less Than Or Equal) instruction.
1393(decl rv_vmfle_vf (VReg FReg VecOpMasking VState) VReg)
1394(rule (rv_vmfle_vf vs2 vs1 mask vstate)
1395  (vec_alu_rrr (VecAluOpRRR.VmfleVF) vs2 vs1 mask vstate))
1396
1397;; Helper for emitting the `vmfgt.vv` (Vector Mask Set If Float Greater Than) instruction.
1398;; This is an alias for `vmflt.vv` with the operands inverted.
1399(decl rv_vmfgt_vv (VReg VReg VecOpMasking VState) VReg)
1400(rule (rv_vmfgt_vv vs2 vs1 mask vstate) (rv_vmflt_vv vs1 vs2 mask vstate))
1401
1402;; Helper for emitting the `vmfgt.vf` (Vector Mask Set If Float Greater Than) instruction.
1403(decl rv_vmfgt_vf (VReg FReg VecOpMasking VState) VReg)
1404(rule (rv_vmfgt_vf vs2 vs1 mask vstate)
1405  (vec_alu_rrr (VecAluOpRRR.VmfgtVF) vs2 vs1 mask vstate))
1406
1407;; Helper for emitting the `vmfge.vv` (Vector Mask Set If Float Greater Than Or Equal) instruction.
1408;; This is an alias for `vmfle.vv` with the operands inverted.
1409(decl rv_vmfge_vv (VReg VReg VecOpMasking VState) VReg)
1410(rule (rv_vmfge_vv vs2 vs1 mask vstate) (rv_vmfle_vv vs1 vs2 mask vstate))
1411
1412;; Helper for emitting the `vmfge.vf` (Vector Mask Set If Float Greater Than Or Equal) instruction.
1413(decl rv_vmfge_vf (VReg FReg VecOpMasking VState) VReg)
1414(rule (rv_vmfge_vf vs2 vs1 mask vstate)
1415  (vec_alu_rrr (VecAluOpRRR.VmfgeVF) vs2 vs1 mask vstate))
1416
1417;; Helper for emitting the `vzext.vf2` instruction.
1418;; Zero-extend SEW/2 source to SEW destination
1419(decl rv_vzext_vf2 (VReg VecOpMasking VState) VReg)
1420(rule (rv_vzext_vf2 vs mask vstate)
1421  (vec_alu_rr (VecAluOpRR.VzextVF2) vs mask vstate))
1422
1423;; Helper for emitting the `vzext.vf4` instruction.
1424;; Zero-extend SEW/4 source to SEW destination
1425(decl rv_vzext_vf4 (VReg VecOpMasking VState) VReg)
1426(rule (rv_vzext_vf4 vs mask vstate)
1427  (vec_alu_rr (VecAluOpRR.VzextVF4) vs mask vstate))
1428
1429;; Helper for emitting the `vzext.vf8` instruction.
1430;; Zero-extend SEW/8 source to SEW destination
1431(decl rv_vzext_vf8 (VReg VecOpMasking VState) VReg)
1432(rule (rv_vzext_vf8 vs mask vstate)
1433  (vec_alu_rr (VecAluOpRR.VzextVF8) vs mask vstate))
1434
1435;; Helper for emitting the `vsext.vf2` instruction.
1436;; Sign-extend SEW/2 source to SEW destination
1437(decl rv_vsext_vf2 (VReg VecOpMasking VState) VReg)
1438(rule (rv_vsext_vf2 vs mask vstate)
1439  (vec_alu_rr (VecAluOpRR.VsextVF2) vs mask vstate))
1440
1441;; Helper for emitting the `vsext.vf4` instruction.
1442;; Sign-extend SEW/4 source to SEW destination
1443(decl rv_vsext_vf4 (VReg VecOpMasking VState) VReg)
1444(rule (rv_vsext_vf4 vs mask vstate)
1445  (vec_alu_rr (VecAluOpRR.VsextVF4) vs mask vstate))
1446
1447;; Helper for emitting the `vsext.vf8` instruction.
1448;; Sign-extend SEW/8 source to SEW destination
1449(decl rv_vsext_vf8 (VReg VecOpMasking VState) VReg)
1450(rule (rv_vsext_vf8 vs mask vstate)
1451  (vec_alu_rr (VecAluOpRR.VsextVF8) vs mask vstate))
1452
1453;; Helper for emitting the `vnclip.wi` instruction.
1454;;
1455;; vd[i] = clip(roundoff_signed(vs2[i], uimm))
1456(decl rv_vnclip_wi (VReg UImm5 VecOpMasking VState) VReg)
1457(rule (rv_vnclip_wi vs2 imm mask vstate)
1458  (vec_alu_rr_uimm5 (VecAluOpRRImm5.VnclipWI) vs2 imm mask vstate))
1459
1460;; Helper for emitting the `vnclipu.wi` instruction.
1461;;
1462;; vd[i] = clip(roundoff_unsigned(vs2[i], uimm))
1463(decl rv_vnclipu_wi (VReg UImm5 VecOpMasking VState) VReg)
1464(rule (rv_vnclipu_wi vs2 imm mask vstate)
1465  (vec_alu_rr_uimm5 (VecAluOpRRImm5.VnclipuWI) vs2 imm mask vstate))
1466
1467;; Helper for emitting the `vmand.mm` (Mask Bitwise AND) instruction.
1468;;
1469;; vd.mask[i] = vs2.mask[i] &&  vs1.mask[i]
1470(decl rv_vmand_mm (VReg VReg VState) VReg)
1471(rule (rv_vmand_mm vs2 vs1 vstate)
1472  (vec_alu_rrr (VecAluOpRRR.VmandMM) vs2 vs1 (unmasked) vstate))
1473
1474;; Helper for emitting the `vmor.mm` (Mask Bitwise OR) instruction.
1475;;
1476;; vd.mask[i] = vs2.mask[i] ||  vs1.mask[i]
1477(decl rv_vmor_mm (VReg VReg VState) VReg)
1478(rule (rv_vmor_mm vs2 vs1 vstate)
1479  (vec_alu_rrr (VecAluOpRRR.VmorMM) vs2 vs1 (unmasked) vstate))
1480
1481;; Helper for emitting the `vmnand.mm` (Mask Bitwise NAND) instruction.
1482;;
1483;; vd.mask[i] = !(vs2.mask[i] &&  vs1.mask[i])
1484(decl rv_vmnand_mm (VReg VReg VState) VReg)
1485(rule (rv_vmnand_mm vs2 vs1 vstate)
1486  (vec_alu_rrr (VecAluOpRRR.VmnandMM) vs2 vs1 (unmasked) vstate))
1487
1488;; Helper for emitting the `vmnot.m` (Mask Bitwise NOT) instruction.
1489;; This is an alias for `vmnand.mm vd, vs, vs`
1490;;
1491;; vd.mask[i] = !vs.mask[i]
1492(decl rv_vmnot_m (VReg VState) VReg)
1493(rule (rv_vmnot_m vs vstate) (rv_vmnand_mm vs vs vstate))
1494
1495;; Helper for emitting the `vmnor.mm` (Mask Bitwise NOR) instruction.
1496;;
1497;; vd.mask[i] = !(vs2.mask[i] ||  vs1.mask[i])
1498(decl rv_vmnor_mm (VReg VReg VState) VReg)
1499(rule (rv_vmnor_mm vs2 vs1 vstate)
1500  (vec_alu_rrr (VecAluOpRRR.VmnorMM) vs2 vs1 (unmasked) vstate))
1501
1502;;;; Multi-Instruction Helpers ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1503
1504(decl gen_extractlane (Type VReg u8) Reg)
1505
1506;; When extracting lane 0 for floats, we can use `vfmv.f.s` directly.
1507(rule 3 (gen_extractlane (ty_vec_fits_in_register ty) src 0)
1508  (if (ty_vector_float ty))
1509  (rv_vfmv_fs src ty))
1510
1511;; When extracting lane 0 for integers, we can use `vmv.x.s` directly.
1512(rule 2 (gen_extractlane (ty_vec_fits_in_register ty) src 0)
1513  (if (ty_vector_not_float ty))
1514  (rv_vmv_xs src ty))
1515
1516;; In the general case, we must first use a `vslidedown` to place the correct lane
1517;; in index 0, and then use the appropriate `vmv` instruction.
1518;; If the index fits into a 5-bit immediate, we can emit a `vslidedown.vi`.
1519(rule 1 (gen_extractlane (ty_vec_fits_in_register ty) src (uimm5_from_u8 idx))
1520  (gen_extractlane ty (rv_vslidedown_vi src idx (unmasked) ty) 0))
1521
1522;; Otherwise lower it into an X register.
1523(rule 0 (gen_extractlane (ty_vec_fits_in_register ty) src idx)
1524  (gen_extractlane ty (rv_vslidedown_vx src (imm $I64 idx) (unmasked) ty) 0))
1525
1526
1527;; Build a vector mask from a u64
1528;; TODO(#6571): We should merge this with the `vconst` rules, and take advantage of
1529;; the other existing `vconst` rules.
1530(decl gen_vec_mask (u64) VReg)
1531
1532;; When the immediate fits in a 5-bit immediate, we can use `vmv.v.i` directly.
1533(rule 1 (gen_vec_mask (imm5_from_u64 imm))
1534  (rv_vmv_vi imm (vstate_from_type $I64X2)))
1535
1536;; Materialize the mask into an X register, and move it into the bottom of
1537;; the vector register.
1538(rule 0 (gen_vec_mask mask)
1539  (rv_vmv_sx (imm $I64 mask) (vstate_from_type $I64X2)))
1540
1541
1542;; Loads a `VCodeConstant` value into a vector register. For some special `VCodeConstant`s
1543;; we can use a dedicated instruction, otherwise we load the value from the pool.
1544;;
1545;; Type is the preferred type to use when loading the constant.
1546(decl gen_constant (Type VCodeConstant) VReg)
1547
1548;; The fallback case is to load the constant from the pool.
1549(rule (gen_constant ty n)
1550  (vec_load
1551    (element_width_from_type ty)
1552    (VecAMode.UnitStride (gen_const_amode n))
1553    (mem_flags_trusted)
1554    (unmasked)
1555    ty))
1556
1557
1558;; Emits a vslidedown instruction that moves half the lanes down.
1559(decl gen_slidedown_half (Type VReg) VReg)
1560
1561;; If the lane count can fit in a 5-bit immediate, we can use `vslidedown.vi`.
1562(rule 1 (gen_slidedown_half (ty_vec_fits_in_register ty) src)
1563  (if-let (uimm5_from_u64 amt) (u64_checked_div (ty_lane_count ty) 2))
1564  (rv_vslidedown_vi src amt (unmasked) ty))
1565
1566;; Otherwise lower it into an X register.
1567(rule 0 (gen_slidedown_half (ty_vec_fits_in_register ty) src)
1568  (if-let amt (u64_checked_div (ty_lane_count ty) 2))
1569  (rv_vslidedown_vx src (imm $I64 amt) (unmasked) ty))
1570
1571
1572;; Expands a mask into SEW wide lanes. Enabled lanes are set to all ones, disabled
1573;; lanes are set to all zeros.
1574(decl gen_expand_mask (Type VReg) VReg)
1575(rule (gen_expand_mask ty mask)
1576  (if-let zero (i8_to_imm5 0))
1577  (if-let neg1 (i8_to_imm5 -1))
1578  (rv_vmerge_vim (rv_vmv_vi zero ty) neg1 mask ty))
1579
1580
1581;; Builds a vector mask corresponding to the IntCC operation.
1582;; TODO: We are still missing some rules here for immediates. See #6623
1583(decl gen_icmp_mask (Type IntCC Value Value) VReg)
1584
1585;; IntCC.Equal
1586
1587(rule 0 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.Equal) x y)
1588  (rv_vmseq_vv x y (unmasked) ty))
1589
1590(rule 1 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.Equal) x (splat y))
1591  (rv_vmseq_vx x y (unmasked) ty))
1592
1593(rule 2 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.Equal) (splat x) y)
1594  (rv_vmseq_vx y x (unmasked) ty))
1595
1596(rule 3 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.Equal) x y)
1597  (if-let y_imm (replicated_imm5 y))
1598  (rv_vmseq_vi x y_imm (unmasked) ty))
1599
1600(rule 4 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.Equal) x y)
1601  (if-let x_imm (replicated_imm5 x))
1602  (rv_vmseq_vi y x_imm (unmasked) ty))
1603
1604;; IntCC.NotEqual
1605
1606(rule 0 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.NotEqual) x y)
1607  (rv_vmsne_vv x y (unmasked) ty))
1608
1609(rule 1 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.NotEqual) x (splat y))
1610  (rv_vmsne_vx x y (unmasked) ty))
1611
1612(rule 2 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.NotEqual) (splat x) y)
1613  (rv_vmsne_vx y x (unmasked) ty))
1614
1615(rule 3 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.NotEqual) x y)
1616  (if-let y_imm (replicated_imm5 y))
1617  (rv_vmsne_vi x y_imm (unmasked) ty))
1618
1619(rule 4 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.NotEqual) x y)
1620  (if-let x_imm (replicated_imm5 x))
1621  (rv_vmsne_vi y x_imm (unmasked) ty))
1622
1623;; IntCC.UnsignedLessThan
1624
1625(rule 0 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.UnsignedLessThan) x y)
1626  (rv_vmsltu_vv x y (unmasked) ty))
1627
1628(rule 1 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.UnsignedLessThan) x (splat y))
1629  (rv_vmsltu_vx x y (unmasked) ty))
1630
1631(rule 2 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.UnsignedLessThan) (splat x) y)
1632  (rv_vmsgtu_vx y x (unmasked) ty))
1633
1634(rule 4 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.UnsignedLessThan) x y)
1635  (if-let x_imm (replicated_imm5 x))
1636  (rv_vmsgtu_vi y x_imm (unmasked) ty))
1637
1638;; IntCC.SignedLessThan
1639
1640(rule 0 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.SignedLessThan) x y)
1641  (rv_vmslt_vv x y (unmasked) ty))
1642
1643(rule 1 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.SignedLessThan) x (splat y))
1644  (rv_vmslt_vx x y (unmasked) ty))
1645
1646(rule 2 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.SignedLessThan) (splat x) y)
1647  (rv_vmsgt_vx y x (unmasked) ty))
1648
1649(rule 4 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.SignedLessThan) x y)
1650  (if-let x_imm (replicated_imm5 x))
1651  (rv_vmsgt_vi y x_imm (unmasked) ty))
1652
1653;; IntCC.UnsignedLessThanOrEqual
1654
1655(rule 0 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.UnsignedLessThanOrEqual) x y)
1656  (rv_vmsleu_vv x y (unmasked) ty))
1657
1658(rule 1 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.UnsignedLessThanOrEqual) x (splat y))
1659  (rv_vmsleu_vx x y (unmasked) ty))
1660
1661(rule 3 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.UnsignedLessThanOrEqual) x y)
1662  (if-let y_imm (replicated_imm5 y))
1663  (rv_vmsleu_vi x y_imm (unmasked) ty))
1664
1665;; IntCC.SignedLessThanOrEqual
1666
1667(rule 0 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.SignedLessThanOrEqual) x y)
1668  (rv_vmsle_vv x y (unmasked) ty))
1669
1670(rule 1 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.SignedLessThanOrEqual) x (splat y))
1671  (rv_vmsle_vx x y (unmasked) ty))
1672
1673(rule 3 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.SignedLessThanOrEqual) x y)
1674  (if-let y_imm (replicated_imm5 y))
1675  (rv_vmsle_vi x y_imm (unmasked) ty))
1676
1677;; IntCC.UnsignedGreaterThan
1678
1679(rule 0 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.UnsignedGreaterThan) x y)
1680  (rv_vmsgtu_vv x y (unmasked) ty))
1681
1682(rule 1 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.UnsignedGreaterThan) x (splat y))
1683  (rv_vmsgtu_vx x y (unmasked) ty))
1684
1685(rule 2 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.UnsignedGreaterThan) (splat x) y)
1686  (rv_vmsltu_vx y x (unmasked) ty))
1687
1688(rule 3 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.UnsignedGreaterThan) x y)
1689  (if-let y_imm (replicated_imm5 y))
1690  (rv_vmsgtu_vi x y_imm (unmasked) ty))
1691
1692;; IntCC.SignedGreaterThan
1693
1694(rule 0 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.SignedGreaterThan) x y)
1695  (rv_vmsgt_vv x y (unmasked) ty))
1696
1697(rule 1 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.SignedGreaterThan) x (splat y))
1698  (rv_vmsgt_vx x y (unmasked) ty))
1699
1700(rule 2 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.SignedGreaterThan) (splat x) y)
1701  (rv_vmslt_vx y x (unmasked) ty))
1702
1703(rule 3 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.SignedGreaterThan) x y)
1704  (if-let y_imm (replicated_imm5 y))
1705  (rv_vmsgt_vi x y_imm (unmasked) ty))
1706
1707;; IntCC.UnsignedGreaterThanOrEqual
1708
1709(rule 0 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.UnsignedGreaterThanOrEqual) x y)
1710  (rv_vmsgeu_vv x y (unmasked) ty))
1711
1712(rule 2 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.UnsignedGreaterThanOrEqual) (splat x) y)
1713  (rv_vmsleu_vx y x (unmasked) ty))
1714
1715(rule 4 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.UnsignedGreaterThanOrEqual) x y)
1716  (if-let x_imm (replicated_imm5 x))
1717  (rv_vmsleu_vi y x_imm (unmasked) ty))
1718
1719;; IntCC.SignedGreaterThanOrEqual
1720
1721(rule 0 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.SignedGreaterThanOrEqual) x y)
1722  (rv_vmsge_vv x y (unmasked) ty))
1723
1724(rule 2 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.SignedGreaterThanOrEqual) (splat x) y)
1725  (rv_vmsle_vx y x (unmasked) ty))
1726
1727(rule 4 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.SignedGreaterThanOrEqual) x y)
1728  (if-let x_imm (replicated_imm5 x))
1729  (rv_vmsle_vi y x_imm (unmasked) ty))
1730
1731
1732
1733;; Builds a vector mask corresponding to the FloatCC operation.
1734(decl gen_fcmp_mask (Type FloatCC Value Value) VReg)
1735
1736;; FloatCC.Equal
1737
1738(rule 0 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.Equal) x y)
1739  (rv_vmfeq_vv x y (unmasked) ty))
1740
1741(rule 1 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.Equal) x (splat y))
1742  (rv_vmfeq_vf x y (unmasked) ty))
1743
1744(rule 2 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.Equal) (splat x) y)
1745  (rv_vmfeq_vf y x (unmasked) ty))
1746
1747;; FloatCC.NotEqual
1748;; Note: This is UnorderedNotEqual. It is the only unordered comparison that is not named as such.
1749
1750(rule 0 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.NotEqual) x y)
1751  (rv_vmfne_vv x y (unmasked) ty))
1752
1753(rule 1 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.NotEqual) x (splat y))
1754  (rv_vmfne_vf x y (unmasked) ty))
1755
1756(rule 2 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.NotEqual) (splat x) y)
1757  (rv_vmfne_vf y x (unmasked) ty))
1758
1759;; FloatCC.LessThan
1760
1761(rule 0 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.LessThan) x y)
1762  (rv_vmflt_vv x y (unmasked) ty))
1763
1764(rule 1 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.LessThan) x (splat y))
1765  (rv_vmflt_vf x y (unmasked) ty))
1766
1767(rule 2 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.LessThan) (splat x) y)
1768  (rv_vmfgt_vf y x (unmasked) ty))
1769
1770;; FloatCC.LessThanOrEqual
1771
1772(rule 0 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.LessThanOrEqual) x y)
1773  (rv_vmfle_vv x y (unmasked) ty))
1774
1775(rule 1 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.LessThanOrEqual) x (splat y))
1776  (rv_vmfle_vf x y (unmasked) ty))
1777
1778(rule 2 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.LessThanOrEqual) (splat x) y)
1779  (rv_vmfge_vf y x (unmasked) ty))
1780
1781;; FloatCC.GreaterThan
1782
1783(rule 0 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.GreaterThan) x y)
1784  (rv_vmfgt_vv x y (unmasked) ty))
1785
1786(rule 1 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.GreaterThan) x (splat y))
1787  (rv_vmfgt_vf x y (unmasked) ty))
1788
1789(rule 2 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.GreaterThan) (splat x) y)
1790  (rv_vmflt_vf y x (unmasked) ty))
1791
1792;; FloatCC.GreaterThanOrEqual
1793
1794(rule 0 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.GreaterThanOrEqual) x y)
1795  (rv_vmfge_vv x y (unmasked) ty))
1796
1797(rule 1 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.GreaterThanOrEqual) x (splat y))
1798  (rv_vmfge_vf x y (unmasked) ty))
1799
1800(rule 2 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.GreaterThanOrEqual) (splat x) y)
1801  (rv_vmfle_vf y x (unmasked) ty))
1802
1803;; FloatCC.Ordered
1804
1805(rule 0 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.Ordered) x y)
1806  (rv_vmand_mm
1807    (gen_fcmp_mask ty (FloatCC.Equal) x x)
1808    (gen_fcmp_mask ty (FloatCC.Equal) y y)
1809    ty))
1810
1811;; FloatCC.Unordered
1812
1813(rule 0 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.Unordered) x y)
1814  (rv_vmor_mm
1815    (gen_fcmp_mask ty (FloatCC.NotEqual) x x)
1816    (gen_fcmp_mask ty (FloatCC.NotEqual) y y)
1817    ty))
1818
1819;; FloatCC.OrderedNotEqual
1820
1821(rule 0 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.OrderedNotEqual) x y)
1822  (rv_vmor_mm
1823    (gen_fcmp_mask ty (FloatCC.LessThan) x y)
1824    (gen_fcmp_mask ty (FloatCC.LessThan) y x)
1825    ty))
1826
1827;; FloatCC.UnorderedOrEqual
1828
1829(rule 0 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.UnorderedOrEqual) x y)
1830  (rv_vmnor_mm
1831    (gen_fcmp_mask ty (FloatCC.LessThan) x y)
1832    (gen_fcmp_mask ty (FloatCC.LessThan) y x)
1833    ty))
1834
1835;; FloatCC.UnorderedOrGreaterThan
1836
1837(rule 0 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.UnorderedOrGreaterThan) x y)
1838  (rv_vmnot_m (gen_fcmp_mask ty (FloatCC.LessThanOrEqual) x y) ty))
1839
1840;; FloatCC.UnorderedOrGreaterThanOrEqual
1841
1842(rule 0 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.UnorderedOrGreaterThanOrEqual) x y)
1843  (rv_vmnot_m (gen_fcmp_mask ty (FloatCC.LessThan) x y) ty))
1844
1845;; FloatCC.UnorderedOrLessThan
1846
1847(rule 0 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.UnorderedOrLessThan) x y)
1848  (rv_vmnot_m (gen_fcmp_mask ty (FloatCC.GreaterThanOrEqual) x y) ty))
1849
1850;; FloatCC.UnorderedOrLessThanOrEqual
1851
1852(rule 0 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.UnorderedOrLessThanOrEqual) x y)
1853  (rv_vmnot_m (gen_fcmp_mask ty (FloatCC.GreaterThan) x y) ty))
1854
1855
1856;; Emits a `vfcvt.x.f.v` instruction with the given rounding mode.
1857(decl gen_vfcvt_x_f (VReg FRM VState) VReg)
1858
1859;; We have a special instruction for RTZ
1860(rule 1 (gen_vfcvt_x_f x (FRM.RTZ) vstate)
1861  (rv_vfcvt_rtz_x_f_v x (unmasked) vstate))
1862
1863;; In the general case we need to first switch into the appropriate rounding mode.
1864(rule 0 (gen_vfcvt_x_f x frm vstate)
1865  (let (
1866        ;; Set the rounding mode and save the current mode
1867        (saved_frm XReg (rv_fsrmi frm))
1868        (res VReg (rv_vfcvt_x_f_v x (unmasked) vstate))
1869        ;; Restore the previous rounding mode
1870        (_ Unit (rv_fsrm saved_frm)))
1871    res))
1872
1873
1874;; Returns the maximum value integer value that can be represented by a float
1875(decl float_int_max (Type) u64)
1876(rule (float_int_max $F32) 0x4B000000)
1877(rule (float_int_max $F64) 0x4330000000000000)
1878
1879;; Builds the instruction sequence to round a vector register to FRM
1880(decl gen_vec_round (VReg FRM Type) VReg)
1881
1882;; For floating-point round operations, if the input is NaN, +/-infinity, or +/-0, the
1883;; same input is returned as the rounded result; this differs from behavior of
1884;; RISCV fcvt instructions (which round out-of-range values to the nearest
1885;; max or min value), therefore special handling is needed for these values.
1886(rule (gen_vec_round x frm (ty_vec_fits_in_register ty))
1887  (let ((scalar_ty Type (lane_type ty))
1888        ;; if x is NaN/+-Infinity/+-Zero or if the exponent is larger than # of bits
1889        ;; in mantissa, the result is the same as src, build a mask for those cases.
1890        ;; (There is an additional fixup for NaN's at the end)
1891        (abs VReg (rv_vfabs_v x (unmasked) ty))
1892        (max FReg (imm scalar_ty (float_int_max scalar_ty)))
1893        (exact VReg (rv_vmflt_vf abs max (unmasked) ty))
1894
1895        ;; The rounding is performed by converting from float to integer, with the
1896        ;; desired rounding mode. And then converting back with the default rounding
1897        ;; mode.
1898        (int VReg (gen_vfcvt_x_f x frm ty))
1899        (cvt VReg (rv_vfcvt_f_x_v int (unmasked) ty))
1900        ;; Copy the sign bit from the original value.
1901        (signed VReg (rv_vfsgnj_vv cvt x (unmasked) ty))
1902
1903        ;; We want to return a arithmetic nan if the input is a canonical nan.
1904        ;; Convert them by adding 0.0 to the input.
1905        (float_zero FReg (gen_bitcast (zero_reg) (float_int_of_same_size scalar_ty) scalar_ty))
1906        (corrected_nan VReg (rv_vfadd_vf x float_zero (unmasked) ty)))
1907    ;; Merge the original value if it does not need rounding, or the rounded value
1908    (rv_vmerge_vvm corrected_nan signed exact ty)))
1909