cranelift-codegen 0.130.0

Low-level code generator library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
;; Represents the possible widths of an element when used in an operation.
(type VecElementWidth (enum
  (E8)
  (E16)
  (E32)
  (E64)
))

;; Vector Register Group Multiplier (LMUL)
;;
;; The LMUL setting specifies how we should group registers together. LMUL can
;; also be a fractional value, reducing the number of bits used in a single
;; vector register. Fractional LMUL is used to increase the number of effective
;; usable vector register groups when operating on mixed-width values.
(type VecLmul (enum
  (LmulF8)
  (LmulF4)
  (LmulF2)
  (Lmul1)
  (Lmul2)
  (Lmul4)
  (Lmul8)
))

;; Tail Mode
;;
;; The tail mode specifies how the tail elements of a vector register are handled.
(type VecTailMode (enum
  ;; Tail Agnostic means that the tail elements are left in an undefined state.
  (Agnostic)
  ;; Tail Undisturbed means that the tail elements are left in their original values.
  (Undisturbed)
))

;; Mask Mode
;;
;; The mask mode specifies how the masked elements of a vector register are handled.
(type VecMaskMode (enum
  ;; Mask Agnostic means that the masked out elements are left in an undefined state.
  (Agnostic)
  ;; Mask Undisturbed means that the masked out elements are left in their original values.
  (Undisturbed)
))

;; Application Vector Length (AVL)
;;
;; This setting specifies the number of elements that are going to be processed
;; in a single instruction. Note: We may end up processing fewer elements than
;; the AVL setting, if they don't fit in a single register.
(type VecAvl (enum
  ;; Static AVL emits a `vsetivli` that uses a constant value
  (Static (size UImm5))
  ;; TODO: Add a dynamic, register based AVL mode when we are able to properly test it
))

(type VType (primitive VType))
(type VState (primitive VState))


;; Vector Opcode Category
;;
;; These categories are used to determine the type of operands that are allowed in the
;; instruction.
(type VecOpCategory (enum
  (OPIVV)
  (OPFVV)
  (OPMVV)
  (OPIVI)
  (OPIVX)
  (OPFVF)
  (OPMVX)
  (OPCFG)
))

;; Vector Opcode Masking
;;
;; When masked, the instruction will only operate on the elements that are dictated by
;; the mask register. Currently this is always fixed to v0.
(type VecOpMasking (enum
  (Enabled (reg Reg))
  (Disabled)
))

(decl pure masked (VReg) VecOpMasking)
(rule (masked reg) (VecOpMasking.Enabled reg))

(decl pure unmasked () VecOpMasking)
(rule (unmasked) (VecOpMasking.Disabled))

;; Register to Register ALU Ops
(type VecAluOpRRR (enum
  ;; Vector-Vector Opcodes
  (VaddVV)
  (VsaddVV)
  (VsadduVV)
  (VwaddVV)
  (VwaddWV)
  (VwadduVV)
  (VwadduWV)
  (VsubVV)
  (VwsubVV)
  (VwsubWV)
  (VwsubuVV)
  (VwsubuWV)
  (VssubVV)
  (VssubuVV)
  (VmulVV)
  (VmulhVV)
  (VmulhuVV)
  (VsmulVV)
  (VsllVV)
  (VsrlVV)
  (VsraVV)
  (VandVV)
  (VorVV)
  (VxorVV)
  (VmaxVV)
  (VmaxuVV)
  (VminVV)
  (VminuVV)
  (VfaddVV)
  (VfsubVV)
  (VfmulVV)
  (VfdivVV)
  (VfminVV)
  (VfmaxVV)
  (VfsgnjVV)
  (VfsgnjnVV)
  (VfsgnjxVV)
  (VmergeVVM)
  (VredmaxuVS)
  (VredminuVS)
  (VrgatherVV)
  (VcompressVM)
  (VmseqVV)
  (VmsneVV)
  (VmsltuVV)
  (VmsltVV)
  (VmsleuVV)
  (VmsleVV)
  (VmfeqVV)
  (VmfneVV)
  (VmfltVV)
  (VmfleVV)
  (VmandMM)
  (VmorMM)
  (VmnandMM)
  (VmnorMM)


  ;; Vector-Scalar Opcodes
  (VaddVX)
  (VsaddVX)
  (VsadduVX)
  (VwaddVX)
  (VwaddWX)
  (VwadduVX)
  (VwadduWX)
  (VsubVX)
  (VrsubVX)
  (VwsubVX)
  (VwsubWX)
  (VwsubuVX)
  (VwsubuWX)
  (VssubVX)
  (VssubuVX)
  (VmulVX)
  (VmulhVX)
  (VmulhuVX)
  (VsmulVX)
  (VsllVX)
  (VsrlVX)
  (VsraVX)
  (VandVX)
  (VorVX)
  (VxorVX)
  (VmaxVX)
  (VmaxuVX)
  (VminVX)
  (VminuVX)
  (VslidedownVX)
  (VfaddVF)
  (VfsubVF)
  (VfrsubVF)
  (VfmulVF)
  (VfdivVF)
  (VfsgnjVF)
  (VfrdivVF)
  (VmergeVXM)
  (VfmergeVFM)
  (VrgatherVX)
  (VmseqVX)
  (VmsneVX)
  (VmsltuVX)
  (VmsltVX)
  (VmsleuVX)
  (VmsleVX)
  (VmsgtuVX)
  (VmsgtVX)
  (VmfeqVF)
  (VmfneVF)
  (VmfltVF)
  (VmfleVF)
  (VmfgtVF)
  (VmfgeVF)
))



;; Register-Imm ALU Ops that modify the destination register
(type VecAluOpRRRImm5 (enum
  (VslideupVI)
))

;; Register-Register ALU Ops that modify the destination register
(type VecAluOpRRRR (enum
  ;; Vector-Vector Opcodes
  (VmaccVV)
  (VnmsacVV)
  (VfmaccVV)
  (VfnmaccVV)
  (VfmsacVV)
  (VfnmsacVV)

  ;; Vector-Scalar Opcodes
  (VmaccVX)
  (VnmsacVX)
  (VfmaccVF)
  (VfnmaccVF)
  (VfmsacVF)
  (VfnmsacVF)
  (Vslide1upVX)
))

;; Register-Imm ALU Ops
(type VecAluOpRRImm5 (enum
  ;; Regular VI Opcodes
  (VaddVI)
  (VsaddVI)
  (VsadduVI)
  (VrsubVI)
  (VsllVI)
  (VsrlVI)
  (VsraVI)
  (VandVI)
  (VorVI)
  (VxorVI)
  (VssrlVI)
  (VslidedownVI)
  (VmergeVIM)
  (VrgatherVI)
  ;; This opcode represents multiple instructions `vmv1r`/`vmv2r`/`vmv4r`/etc...
  ;; The immediate field specifies how many registers should be copied.
  (VmvrV)
  (VnclipWI)
  (VnclipuWI)
  (VmseqVI)
  (VmsneVI)
  (VmsleuVI)
  (VmsleVI)
  (VmsgtuVI)
  (VmsgtVI)
))

;; Imm only ALU Ops
(type VecAluOpRImm5 (enum
  (VmvVI)
))

;; These are all of the special cases that have weird encodings. They are all
;; single source, single destination instructions, and usually use one of
;; the two source registers as auxiliary encoding space.
(type VecAluOpRR (enum
  (VmvSX)
  (VmvXS)
  (VfmvSF)
  (VfmvFS)
  ;; vmv.v* is special in that vs2 must be v0 (and is ignored) otherwise the instruction is illegal.
  (VmvVV)
  (VmvVX)
  (VfmvVF)
  (VfsqrtV)
  (VsextVF2)
  (VsextVF4)
  (VsextVF8)
  (VzextVF2)
  (VzextVF4)
  (VzextVF8)
  (VfcvtxufV)
  (VfcvtxfV)
  (VfcvtrtzxufV)
  (VfcvtrtzxfV)
  (VfcvtfxuV)
  (VfcvtfxV)
  (VfwcvtffV)
  (VfncvtffW)
))

;; Returns the canonical destination type for a VecAluOpRRImm5.
(decl pure vec_alu_rr_dst_type (VecAluOpRR) Type)
(extern constructor vec_alu_rr_dst_type vec_alu_rr_dst_type)


;; Vector Addressing Mode
(type VecAMode (enum
  ;; Vector unit-stride operations access elements stored contiguously in memory
  ;; starting from the base effective address.
  (UnitStride
    (base AMode))
  ;; TODO: Constant Stride
  ;; TODO: Indexed Operations
))


;; Builds a static VState matching a SIMD type.
;; The VState is guaranteed to be static with AVL set to the number of lanes.
;; Element size is set to the size of the type.
;; LMUL is set to 1.
;; Tail mode is set to agnostic.
;; Mask mode is set to agnostic.
(decl pure vstate_from_type (Type) VState)
(extern constructor vstate_from_type vstate_from_type)
(convert Type VState vstate_from_type)

;; Alters the LMUL of a VState to mf2
(decl pure vstate_mf2 (VState) VState)
(extern constructor vstate_mf2 vstate_mf2)

;; Extracts an element width from a SIMD type.
(decl pure element_width_from_type (Type) VecElementWidth)
(rule (element_width_from_type ty)
  (if-let $I8 (lane_type ty))
  (VecElementWidth.E8))
(rule (element_width_from_type ty)
  (if-let $I16 (lane_type ty))
  (VecElementWidth.E16))
(rule (element_width_from_type ty)
  (if-let $I32 (lane_type ty))
  (VecElementWidth.E32))
(rule (element_width_from_type ty)
  (if-let $F32 (lane_type ty))
  (VecElementWidth.E32))
(rule (element_width_from_type ty)
  (if-let $I64 (lane_type ty))
  (VecElementWidth.E64))
(rule (element_width_from_type ty)
  (if-let $F64 (lane_type ty))
  (VecElementWidth.E64))

(decl pure min_vec_reg_size () u64)
(extern constructor min_vec_reg_size min_vec_reg_size)

;; An extractor that matches any type that is known to fit in a single vector
;; register.
(decl ty_vec_fits_in_register (Type) Type)
(extern extractor ty_vec_fits_in_register ty_vec_fits_in_register)

;;;; Instruction Helpers ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; As noted in the RISC-V Vector Extension Specification, rs2 is the first
;; source register and rs1 is the second source register. This is the opposite
;; of the usual RISC-V register order.
;; See Section 10.1 of the RISC-V Vector Extension Specification.


;; Helper for emitting `MInst.VecAluRRRR` instructions.
;; These instructions modify the destination register.
(decl vec_alu_rrrr (VecAluOpRRRR VReg VReg Reg VecOpMasking  VState) VReg)
(rule (vec_alu_rrrr op vd_src vs2 vs1 mask vstate)
      (let ((vd WritableVReg (temp_writable_vreg))
            (_ Unit (emit (MInst.VecAluRRRR op vd vd_src vs2 vs1 mask vstate))))
        vd))

;; Helper for emitting `MInst.VecAluRRRImm5` instructions.
;; These instructions modify the destination register.
(decl vec_alu_rrr_imm5 (VecAluOpRRRImm5 VReg VReg Imm5 VecOpMasking  VState) VReg)
(rule (vec_alu_rrr_imm5 op vd_src vs2 imm mask vstate)
      (let ((vd WritableVReg (temp_writable_vreg))
            (_ Unit (emit (MInst.VecAluRRRImm5 op vd vd_src vs2 imm mask vstate))))
        vd))

;; Helper for emitting `MInst.VecAluRRRImm5` instructions where the immediate
;; is zero extended instead of sign extended.
(decl vec_alu_rrr_uimm5 (VecAluOpRRRImm5 VReg VReg UImm5 VecOpMasking VState) VReg)
(rule (vec_alu_rrr_uimm5 op vd_src vs2 imm mask vstate)
      (vec_alu_rrr_imm5 op vd_src vs2 (uimm5_bitcast_to_imm5 imm) mask vstate))

;; Helper for emitting `MInst.VecAluRRR` instructions.
(decl vec_alu_rrr (VecAluOpRRR Reg Reg VecOpMasking VState) Reg)
(rule (vec_alu_rrr op vs2 vs1 mask vstate)
      (let ((vd WritableVReg (temp_writable_vreg))
            (_ Unit (emit (MInst.VecAluRRR op vd vs2 vs1 mask vstate))))
        vd))

;; Helper for emitting `MInst.VecAluRRImm5` instructions.
(decl vec_alu_rr_imm5 (VecAluOpRRImm5 Reg Imm5 VecOpMasking  VState) Reg)
(rule (vec_alu_rr_imm5 op vs2 imm mask vstate)
      (let ((vd WritableVReg (temp_writable_vreg))
            (_ Unit (emit (MInst.VecAluRRImm5 op vd vs2 imm mask vstate))))
        vd))

;; Helper for emitting `MInst.VecAluRRImm5` instructions where the immediate
;; is zero extended instead of sign extended.
(decl vec_alu_rr_uimm5 (VecAluOpRRImm5 Reg UImm5 VecOpMasking VState) Reg)
(rule (vec_alu_rr_uimm5 op vs2 imm mask vstate)
      (vec_alu_rr_imm5 op vs2 (uimm5_bitcast_to_imm5 imm) mask vstate))

;; Helper for emitting `MInst.VecAluRRImm5` instructions that use the Imm5 as
;; auxiliary encoding space.
(decl vec_alu_rr (VecAluOpRR Reg VecOpMasking VState) Reg)
(rule (vec_alu_rr op vs mask vstate)
      (let ((vd WritableReg (temp_writable_reg (vec_alu_rr_dst_type op)))
            (_ Unit (emit (MInst.VecAluRR op vd vs mask vstate))))
        vd))

;; Helper for emitting `MInst.VecAluRImm5` instructions.
(decl vec_alu_r_imm5 (VecAluOpRImm5 Imm5 VecOpMasking VState) Reg)
(rule (vec_alu_r_imm5 op imm mask vstate)
      (let ((vd WritableVReg (temp_writable_vreg))
            (_ Unit (emit (MInst.VecAluRImm5 op vd imm mask vstate))))
        vd))

;; Helper for emitting `MInst.VecLoad` instructions.
(decl vec_load (VecElementWidth VecAMode MemFlags VecOpMasking VState) Reg)
(rule (vec_load eew from flags mask vstate)
      (let ((vd WritableVReg (temp_writable_vreg))
            (_ Unit (emit (MInst.VecLoad eew vd from flags mask vstate))))
        vd))

;; Helper for emitting `MInst.VecStore` instructions.
(decl vec_store (VecElementWidth VecAMode VReg MemFlags VecOpMasking VState) InstOutput)
(rule (vec_store eew to from flags mask vstate)
      (side_effect
        (SideEffectNoResult.Inst (MInst.VecStore eew to from flags mask vstate))))

;; Helper for emitting the `vadd.vv` instruction.
(decl rv_vadd_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vadd_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VaddVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vadd.vx` instruction.
(decl rv_vadd_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vadd_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VaddVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vadd.vi` instruction.
(decl rv_vadd_vi (VReg Imm5 VecOpMasking VState) VReg)
(rule (rv_vadd_vi vs2 imm mask vstate)
  (vec_alu_rr_imm5 (VecAluOpRRImm5.VaddVI) vs2 imm mask vstate))

;; Helper for emitting the `vsadd.vv` instruction.
(decl rv_vsadd_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vsadd_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VsaddVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vsadd.vx` instruction.
(decl rv_vsadd_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vsadd_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VsaddVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vsadd.vi` instruction.
(decl rv_vsadd_vi (VReg Imm5 VecOpMasking VState) VReg)
(rule (rv_vsadd_vi vs2 imm mask vstate)
  (vec_alu_rr_imm5 (VecAluOpRRImm5.VsaddVI) vs2 imm mask vstate))

;; Helper for emitting the `vsaddu.vv` instruction.
(decl rv_vsaddu_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vsaddu_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VsadduVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vsaddu.vx` instruction.
(decl rv_vsaddu_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vsaddu_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VsadduVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vsaddu.vi` instruction.
(decl rv_vsaddu_vi (VReg Imm5 VecOpMasking VState) VReg)
(rule (rv_vsaddu_vi vs2 imm mask vstate)
  (vec_alu_rr_imm5 (VecAluOpRRImm5.VsadduVI) vs2 imm mask vstate))

;; Helper for emitting the `vwadd.vv` instruction.
;;
;;  Widening integer add, 2*SEW = SEW + SEW
(decl rv_vwadd_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vwadd_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VwaddVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vwadd.vx` instruction.
;;
;;  Widening integer add, 2*SEW = SEW + SEW
(decl rv_vwadd_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vwadd_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VwaddVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vwadd.wv` instruction.
;;
;;  Widening integer add, 2*SEW = 2*SEW + SEW
(decl rv_vwadd_wv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vwadd_wv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VwaddWV) vs2 vs1 mask vstate))

;; Helper for emitting the `vwadd.wx` instruction.
;;
;;  Widening integer add, 2*SEW = 2*SEW + SEW
(decl rv_vwadd_wx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vwadd_wx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VwaddWX) vs2 vs1 mask vstate))

;; Helper for emitting the `vwaddu.vv` instruction.
;;
;; Widening unsigned integer add, 2*SEW = SEW + SEW
(decl rv_vwaddu_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vwaddu_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VwadduVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vwaddu.vv` instruction.
;;
;; Widening unsigned integer add, 2*SEW = SEW + SEW
(decl rv_vwaddu_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vwaddu_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VwadduVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vwaddu.wv` instruction.
;;
;;  Widening integer add, 2*SEW = 2*SEW + SEW
(decl rv_vwaddu_wv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vwaddu_wv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VwadduWV) vs2 vs1 mask vstate))

;; Helper for emitting the `vwaddu.wx` instruction.
;;
;;  Widening integer add, 2*SEW = 2*SEW + SEW
(decl rv_vwaddu_wx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vwaddu_wx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VwadduWX) vs2 vs1 mask vstate))

;; Helper for emitting the `vsub.vv` instruction.
(decl rv_vsub_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vsub_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VsubVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vsub.vx` instruction.
(decl rv_vsub_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vsub_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VsubVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vrsub.vx` instruction.
(decl rv_vrsub_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vrsub_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VrsubVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vwsub.vv` instruction.
;;
;;  Widening integer sub, 2*SEW = SEW + SEW
(decl rv_vwsub_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vwsub_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VwsubVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vwsub.vx` instruction.
;;
;;  Widening integer sub, 2*SEW = SEW + SEW
(decl rv_vwsub_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vwsub_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VwsubVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vwsub.wv` instruction.
;;
;;  Widening integer sub, 2*SEW = 2*SEW + SEW
(decl rv_vwsub_wv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vwsub_wv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VwsubWV) vs2 vs1 mask vstate))

;; Helper for emitting the `vwsub.wx` instruction.
;;
;;  Widening integer sub, 2*SEW = 2*SEW + SEW
(decl rv_vwsub_wx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vwsub_wx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VwsubWX) vs2 vs1 mask vstate))

;; Helper for emitting the `vwsubu.vv` instruction.
;;
;; Widening unsigned integer sub, 2*SEW = SEW + SEW
(decl rv_vwsubu_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vwsubu_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VwsubuVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vwsubu.vv` instruction.
;;
;; Widening unsigned integer sub, 2*SEW = SEW + SEW
(decl rv_vwsubu_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vwsubu_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VwsubuVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vwsubu.wv` instruction.
;;
;;  Widening integer sub, 2*SEW = 2*SEW + SEW
(decl rv_vwsubu_wv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vwsubu_wv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VwsubuWV) vs2 vs1 mask vstate))

;; Helper for emitting the `vwsubu.wx` instruction.
;;
;;  Widening integer sub, 2*SEW = 2*SEW + SEW
(decl rv_vwsubu_wx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vwsubu_wx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VwsubuWX) vs2 vs1 mask vstate))

;; Helper for emitting the `vssub.vv` instruction.
(decl rv_vssub_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vssub_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VssubVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vssub.vx` instruction.
(decl rv_vssub_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vssub_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VssubVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vssubu.vv` instruction.
(decl rv_vssubu_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vssubu_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VssubuVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vssubu.vx` instruction.
(decl rv_vssubu_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vssubu_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VssubuVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vneg.v` pseudo-instruction.
(decl rv_vneg_v (VReg VecOpMasking VState) VReg)
(rule (rv_vneg_v vs2 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VrsubVX) vs2 (zero_reg) mask vstate))

;; Helper for emitting the `vrsub.vi` instruction.
(decl rv_vrsub_vi (VReg Imm5 VecOpMasking VState) VReg)
(rule (rv_vrsub_vi vs2 imm mask vstate)
  (vec_alu_rr_imm5 (VecAluOpRRImm5.VrsubVI) vs2 imm mask vstate))

;; Helper for emitting the `vmul.vv` instruction.
(decl rv_vmul_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vmul_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmulVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vmul.vx` instruction.
(decl rv_vmul_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vmul_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmulVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vmulh.vv` instruction.
(decl rv_vmulh_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vmulh_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmulhVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vmulh.vx` instruction.
(decl rv_vmulh_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vmulh_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmulhVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vmulhu.vv` instruction.
(decl rv_vmulhu_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vmulhu_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmulhuVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vmulhu.vx` instruction.
(decl rv_vmulhu_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vmulhu_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmulhuVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vsmul.vv` instruction.
;;
;; Signed saturating and rounding fractional multiply
;; # vd[i] = clip(roundoff_signed(vs2[i]*vs1[i], SEW-1))
(decl rv_vsmul_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vsmul_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VsmulVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vsmul.vx` instruction.
;;
;; Signed saturating and rounding fractional multiply
;; # vd[i] = clip(roundoff_signed(vs2[i]*x[rs1], SEW-1))
(decl rv_vsmul_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vsmul_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VsmulVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vmacc.vv` instruction.
;;
;; Integer multiply-add, overwrite addend
;; # vd[i] = +(vs1[i] * vs2[i]) + vd[i]
(decl rv_vmacc_vv (VReg VReg VReg VecOpMasking VState) VReg)
(rule (rv_vmacc_vv vd vs2 vs1 mask vstate)
  (vec_alu_rrrr (VecAluOpRRRR.VmaccVV) vd vs2 vs1 mask vstate))

;; Helper for emitting the `vmacc.vx` instruction.
;;
;; Integer multiply-add, overwrite addend
;; # vd[i] = +(x[rs1] * vs2[i]) + vd[i]
(decl rv_vmacc_vx (VReg VReg XReg VecOpMasking VState) VReg)
(rule (rv_vmacc_vx vd vs2 vs1 mask vstate)
  (vec_alu_rrrr (VecAluOpRRRR.VmaccVX) vd vs2 vs1 mask vstate))

;; Helper for emitting the `vnmsac.vv` instruction.
;;
;; Integer multiply-sub, overwrite minuend
;; # vd[i] = -(vs1[i] * vs2[i]) + vd[i]
(decl rv_vnmsac_vv (VReg VReg VReg VecOpMasking VState) VReg)
(rule (rv_vnmsac_vv vd vs2 vs1 mask vstate)
  (vec_alu_rrrr (VecAluOpRRRR.VnmsacVV) vd vs2 vs1 mask vstate))

;; Helper for emitting the `vnmsac.vx` instruction.
;;
;; Integer multiply-sub, overwrite minuend
;; # vd[i] = -(x[rs1] * vs2[i]) + vd[i]
(decl rv_vnmsac_vx (VReg VReg XReg VecOpMasking VState) VReg)
(rule (rv_vnmsac_vx vd vs2 vs1 mask vstate)
  (vec_alu_rrrr (VecAluOpRRRR.VnmsacVX) vd vs2 vs1 mask vstate))

;; Helper for emitting the `sll.vv` instruction.
(decl rv_vsll_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vsll_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VsllVV) vs2 vs1 mask vstate))

;; Helper for emitting the `sll.vx` instruction.
(decl rv_vsll_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vsll_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VsllVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vsll.vi` instruction.
(decl rv_vsll_vi (VReg UImm5 VecOpMasking VState) VReg)
(rule (rv_vsll_vi vs2 imm mask vstate)
  (vec_alu_rr_uimm5 (VecAluOpRRImm5.VsllVI) vs2 imm mask vstate))

;; Helper for emitting the `srl.vv` instruction.
(decl rv_vsrl_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vsrl_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VsrlVV) vs2 vs1 mask vstate))

;; Helper for emitting the `srl.vx` instruction.
(decl rv_vsrl_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vsrl_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VsrlVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vsrl.vi` instruction.
(decl rv_vsrl_vi (VReg UImm5 VecOpMasking VState) VReg)
(rule (rv_vsrl_vi vs2 imm mask vstate)
  (vec_alu_rr_uimm5 (VecAluOpRRImm5.VsrlVI) vs2 imm mask vstate))

;; Helper for emitting the `sra.vv` instruction.
(decl rv_vsra_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vsra_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VsraVV) vs2 vs1 mask vstate))

;; Helper for emitting the `sra.vx` instruction.
(decl rv_vsra_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vsra_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VsraVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vsra.vi` instruction.
(decl rv_vsra_vi (VReg UImm5 VecOpMasking VState) VReg)
(rule (rv_vsra_vi vs2 imm mask vstate)
  (vec_alu_rr_uimm5 (VecAluOpRRImm5.VsraVI) vs2 imm mask vstate))

;; Helper for emitting the `vand.vv` instruction.
(decl rv_vand_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vand_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VandVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vand.vx` instruction.
(decl rv_vand_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vand_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VandVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vand.vi` instruction.
(decl rv_vand_vi (VReg Imm5 VecOpMasking VState) VReg)
(rule (rv_vand_vi vs2 imm mask vstate)
  (vec_alu_rr_imm5 (VecAluOpRRImm5.VandVI) vs2 imm mask vstate))

;; Helper for emitting the `vor.vv` instruction.
(decl rv_vor_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vor_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VorVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vor.vx` instruction.
(decl rv_vor_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vor_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VorVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vor.vi` instruction.
(decl rv_vor_vi (VReg Imm5 VecOpMasking VState) VReg)
(rule (rv_vor_vi vs2 imm mask vstate)
  (vec_alu_rr_imm5 (VecAluOpRRImm5.VorVI) vs2 imm mask vstate))

;; Helper for emitting the `vxor.vv` instruction.
(decl rv_vxor_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vxor_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VxorVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vxor.vx` instruction.
(decl rv_vxor_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vxor_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VxorVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vxor.vi` instruction.
(decl rv_vxor_vi (VReg Imm5 VecOpMasking VState) VReg)
(rule (rv_vxor_vi vs2 imm mask vstate)
  (vec_alu_rr_imm5 (VecAluOpRRImm5.VxorVI) vs2 imm mask vstate))

;; Helper for emitting the `vssrl.vi` instruction.
;;
;; vd[i] = (unsigned(vs2[i]) >> imm) + r
;;
;; `r` here is the rounding mode currently selected.
(decl rv_vssrl_vi (VReg UImm5 VecOpMasking VState) VReg)
(rule (rv_vssrl_vi vs2 imm mask vstate)
  (vec_alu_rr_uimm5 (VecAluOpRRImm5.VssrlVI) vs2 imm mask vstate))

;; Helper for emitting the `vnot.v` instruction.
;; This is just a mnemonic for `vxor.vi vd, vs, -1`
(decl rv_vnot_v (VReg VecOpMasking VState) VReg)
(rule (rv_vnot_v vs2 mask vstate)
  (if-let neg1 (i8_to_imm5 -1))
  (rv_vxor_vi vs2 neg1 mask vstate))

;; Helper for emitting the `vmax.vv` instruction.
(decl rv_vmax_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vmax_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmaxVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vmax.vx` instruction.
(decl rv_vmax_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vmax_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmaxVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vmin.vv` instruction.
(decl rv_vmin_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vmin_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VminVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vmin.vx` instruction.
(decl rv_vmin_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vmin_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VminVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vmaxu.vv` instruction.
(decl rv_vmaxu_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vmaxu_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmaxuVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vmaxu.vx` instruction.
(decl rv_vmaxu_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vmaxu_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmaxuVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vminu.vv` instruction.
(decl rv_vminu_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vminu_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VminuVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vminu.vx` instruction.
(decl rv_vminu_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vminu_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VminuVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vfadd.vv` instruction.
(decl rv_vfadd_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vfadd_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VfaddVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vfadd.vf` instruction.
(decl rv_vfadd_vf (VReg FReg VecOpMasking VState) VReg)
(rule (rv_vfadd_vf vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VfaddVF) vs2 vs1 mask vstate))

;; Helper for emitting the `vfsub.vv` instruction.
(decl rv_vfsub_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vfsub_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VfsubVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vfsub.vf` instruction.
(decl rv_vfsub_vf (VReg FReg VecOpMasking VState) VReg)
(rule (rv_vfsub_vf vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VfsubVF) vs2 vs1 mask vstate))

;; Helper for emitting the `vfrsub.vf` instruction.
(decl rv_vfrsub_vf (VReg FReg VecOpMasking VState) VReg)
(rule (rv_vfrsub_vf vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VfrsubVF) vs2 vs1 mask vstate))

;; Helper for emitting the `vfmul.vv` instruction.
(decl rv_vfmul_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vfmul_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VfmulVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vfmul.vf` instruction.
(decl rv_vfmul_vf (VReg FReg VecOpMasking VState) VReg)
(rule (rv_vfmul_vf vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VfmulVF) vs2 vs1 mask vstate))

;; Helper for emitting the `vfmacc.vv` instruction.
;;
;; FP multiply-accumulate, overwrites addend
;; # vd[i] = +(vs1[i] * vs2[i]) + vd[i]
(decl rv_vfmacc_vv (VReg VReg VReg VecOpMasking VState) VReg)
(rule (rv_vfmacc_vv vd vs2 vs1 mask vstate)
  (vec_alu_rrrr (VecAluOpRRRR.VfmaccVV) vd vs2 vs1 mask vstate))

;; Helper for emitting the `vfmacc.vf` instruction.
;;
;; FP multiply-accumulate, overwrites addend
;; # vd[i] = +(f[rs1] * vs2[i]) + vd[i]
(decl rv_vfmacc_vf (VReg VReg FReg VecOpMasking VState) VReg)
(rule (rv_vfmacc_vf vd vs2 vs1 mask vstate)
  (vec_alu_rrrr (VecAluOpRRRR.VfmaccVF) vd vs2 vs1 mask vstate))

;; Helper for emitting the `vfnmacc.vv` instruction.
;;
;; FP negate-(multiply-accumulate), overwrites subtrahend
;; # vd[i] = -(vs1[i] * vs2[i]) - vd[i]
(decl rv_vfnmacc_vv (VReg VReg VReg VecOpMasking VState) VReg)
(rule (rv_vfnmacc_vv vd vs2 vs1 mask vstate)
  (vec_alu_rrrr (VecAluOpRRRR.VfnmaccVV) vd vs2 vs1 mask vstate))

;; Helper for emitting the `vfnmacc.vf` instruction.
;;
;; FP negate-(multiply-accumulate), overwrites subtrahend
;; # vd[i] = -(f[rs1] * vs2[i]) - vd[i]
(decl rv_vfnmacc_vf (VReg VReg FReg VecOpMasking VState) VReg)
(rule (rv_vfnmacc_vf vd vs2 vs1 mask vstate)
  (vec_alu_rrrr (VecAluOpRRRR.VfnmaccVF) vd vs2 vs1 mask vstate))

;; Helper for emitting the `vfmsac.vv` instruction.
;;
;; FP multiply-subtract-accumulator, overwrites subtrahend
;; # vd[i] = +(vs1[i] * vs2[i]) - vd[i]
(decl rv_vfmsac_vv (VReg VReg VReg VecOpMasking VState) VReg)
(rule (rv_vfmsac_vv vd vs2 vs1 mask vstate)
  (vec_alu_rrrr (VecAluOpRRRR.VfmsacVV) vd vs2 vs1 mask vstate))

;; Helper for emitting the `vfmsac.vf` instruction.
;;
;; FP multiply-subtract-accumulator, overwrites subtrahend
;; # vd[i] = +(f[rs1] * vs2[i]) - vd[i]
(decl rv_vfmsac_vf (VReg VReg FReg VecOpMasking VState) VReg)
(rule (rv_vfmsac_vf vd vs2 vs1 mask vstate)
  (vec_alu_rrrr (VecAluOpRRRR.VfmsacVF) vd vs2 vs1 mask vstate))

;; Helper for emitting the `vfnmsac.vv` instruction.
;;
;; FP negate-(multiply-subtract-accumulator), overwrites minuend
;; # vd[i] = -(vs1[i] * vs2[i]) + vd[i]
(decl rv_vfnmsac_vv (VReg VReg VReg VecOpMasking VState) VReg)
(rule (rv_vfnmsac_vv vd vs2 vs1 mask vstate)
  (vec_alu_rrrr (VecAluOpRRRR.VfnmsacVV) vd vs2 vs1 mask vstate))

;; Helper for emitting the `vfnmsac.vf` instruction.
;;
;; FP negate-(multiply-subtract-accumulator), overwrites minuend
;; # vd[i] = -(f[rs1] * vs2[i]) + vd[i]
(decl rv_vfnmsac_vf (VReg VReg FReg VecOpMasking VState) VReg)
(rule (rv_vfnmsac_vf vd vs2 vs1 mask vstate)
  (vec_alu_rrrr (VecAluOpRRRR.VfnmsacVF) vd vs2 vs1 mask vstate))

;; Helper for emitting the `vfdiv.vv` instruction.
(decl rv_vfdiv_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vfdiv_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VfdivVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vfdiv.vf` instruction.
(decl rv_vfdiv_vf (VReg FReg VecOpMasking VState) VReg)
(rule (rv_vfdiv_vf vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VfdivVF) vs2 vs1 mask vstate))

;; Helper for emitting the `vfrdiv.vf` instruction.
(decl rv_vfrdiv_vf (VReg FReg VecOpMasking VState) VReg)
(rule (rv_vfrdiv_vf vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VfrdivVF) vs2 vs1 mask vstate))

;; Helper for emitting the `vfmin.vv` instruction.
(decl rv_vfmin_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vfmin_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VfminVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vfmax.vv` instruction.
(decl rv_vfmax_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vfmax_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VfmaxVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vfsgnj.vv` ("Floating Point Sign Injection") instruction.
;; The output of this instruction is `vs2` with the sign bit from `vs1`
(decl rv_vfsgnj_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vfsgnj_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VfsgnjVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vfsgnj.vf` ("Floating Point Sign Injection") instruction.
(decl rv_vfsgnj_vf (VReg FReg VecOpMasking VState) VReg)
(rule (rv_vfsgnj_vf vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VfsgnjVF) vs2 vs1 mask vstate))

;; Helper for emitting the `vfsgnjn.vv` ("Floating Point Sign Injection Negated") instruction.
;; The output of this instruction is `vs2` with the negated sign bit from `vs1`
(decl rv_vfsgnjn_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vfsgnjn_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VfsgnjnVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vfneg.v` instruction.
;; This instruction is a mnemonic for `vfsgnjn.vv vd, vs, vs`
(decl rv_vfneg_v (VReg VecOpMasking VState) VReg)
(rule (rv_vfneg_v vs mask vstate) (rv_vfsgnjn_vv vs vs mask vstate))

;; Helper for emitting the `vfsgnjx.vv` ("Floating Point Sign Injection Exclusive") instruction.
;; The output of this instruction is `vs2` with the XOR of the sign bits from `vs2` and `vs1`.
;; When `vs2 == vs1` this implements `fabs`
(decl rv_vfsgnjx_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vfsgnjx_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VfsgnjxVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vfabs.v` instruction.
;; This instruction is a mnemonic for `vfsgnjx.vv vd, vs, vs`
(decl rv_vfabs_v (VReg VecOpMasking VState) VReg)
(rule (rv_vfabs_v vs mask vstate) (rv_vfsgnjx_vv vs vs mask vstate))

;; Helper for emitting the `vfsqrt.v` instruction.
;; This instruction splats the F register into all elements of the destination vector.
(decl rv_vfsqrt_v (VReg VecOpMasking VState) VReg)
(rule (rv_vfsqrt_v vs mask vstate)
  (vec_alu_rr (VecAluOpRR.VfsqrtV) vs mask vstate))

;; Helper for emitting the `vfcvt.xu.f.v` instruction.
;; This instruction converts a float to an unsigned integer.
(decl rv_vfcvt_xu_f_v (VReg VecOpMasking VState) VReg)
(rule (rv_vfcvt_xu_f_v vs mask vstate)
  (vec_alu_rr (VecAluOpRR.VfcvtxufV) vs mask vstate))

;; Helper for emitting the `vfcvt.x.f.v` instruction.
;; This instruction converts a float to a signed integer.
(decl rv_vfcvt_x_f_v (VReg VecOpMasking VState) VReg)
(rule (rv_vfcvt_x_f_v vs mask vstate)
  (vec_alu_rr (VecAluOpRR.VfcvtxfV) vs mask vstate))

;; Helper for emitting the `vfcvt.rtz.xu.f.v` instruction.
;; This instruction converts a float to an unsigned integer
;; using the Round to Zero (RTZ) rounding mode and ignoring
;; the currently set FRM rounding mode.
(decl rv_vfcvt_rtz_xu_f_v (VReg VecOpMasking VState) VReg)
(rule (rv_vfcvt_rtz_xu_f_v vs mask vstate)
  (vec_alu_rr (VecAluOpRR.VfcvtrtzxufV) vs mask vstate))

;; Helper for emitting the `vfcvt.rtz.x.f.v` instruction.
;; This instruction converts a float to a signed integer.
;; using the Round to Zero (RTZ) rounding mode and ignoring
;; the currently set FRM rounding mode.
(decl rv_vfcvt_rtz_x_f_v (VReg VecOpMasking VState) VReg)
(rule (rv_vfcvt_rtz_x_f_v vs mask vstate)
  (vec_alu_rr (VecAluOpRR.VfcvtrtzxfV) vs mask vstate))

;; Helper for emitting the `vfcvt.f.xu.v` instruction.
;; This instruction converts a unsigned integer to a float.
(decl rv_vfcvt_f_xu_v (VReg VecOpMasking VState) VReg)
(rule (rv_vfcvt_f_xu_v vs mask vstate)
  (vec_alu_rr (VecAluOpRR.VfcvtfxuV) vs mask vstate))

;; Helper for emitting the `vfcvt.x.f.v` instruction.
;; This instruction converts a signed integer to a float.
(decl rv_vfcvt_f_x_v (VReg VecOpMasking VState) VReg)
(rule (rv_vfcvt_f_x_v vs mask vstate)
  (vec_alu_rr (VecAluOpRR.VfcvtfxV) vs mask vstate))

  ;; Helper for emitting the `vfwcvt.f.f.v` instruction.
;; Convert single-width float to double-width float.
(decl rv_vfwcvt_f_f_v (VReg VecOpMasking VState) VReg)
(rule (rv_vfwcvt_f_f_v vs mask vstate)
  (vec_alu_rr (VecAluOpRR.VfwcvtffV) vs mask vstate))

;; Helper for emitting the `vfncvt.f.f.w` instruction.
;; Convert double-width float to single-width float.
(decl rv_vfncvt_f_f_w (VReg VecOpMasking VState) VReg)
(rule (rv_vfncvt_f_f_w vs mask vstate)
  (vec_alu_rr (VecAluOpRR.VfncvtffW) vs mask vstate))

;; Helper for emitting the `vslidedown.vx` instruction.
;; `vslidedown` moves all elements in the vector down by n elements.
;; The top most elements are up to the tail policy.
(decl rv_vslidedown_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vslidedown_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VslidedownVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vslidedown.vi` instruction.
;; Unlike other `vi` instructions the immediate is zero extended.
(decl rv_vslidedown_vi (VReg UImm5 VecOpMasking VState) VReg)
(rule (rv_vslidedown_vi vs2 imm mask vstate)
  (vec_alu_rr_uimm5 (VecAluOpRRImm5.VslidedownVI) vs2 imm mask vstate))

;; Helper for emitting the `vslideup.vi` instruction.
;; Unlike other `vi` instructions the immediate is zero extended.
;; This is implemented as a 2 source operand instruction, since it only
;; partially modifies the destination register.
(decl rv_vslideup_vvi (VReg VReg UImm5 VecOpMasking VState) VReg)
(rule (rv_vslideup_vvi vd vs2 imm mask vstate)
  (vec_alu_rrr_uimm5 (VecAluOpRRRImm5.VslideupVI) vd vs2 imm mask vstate))

;; Helper for emitting the `vslide1up.vx` instruction.
;;
;; # vd[0]=x[rs1], vd[i+1] = vs2[i]
(decl rv_vslide1up_vx (VReg VReg XReg VecOpMasking VState) VReg)
(rule (rv_vslide1up_vx vd vs2 rs1 mask vstate)
  (vec_alu_rrrr (VecAluOpRRRR.Vslide1upVX) vd vs2 rs1 mask vstate))

;; Helper for emitting the `vmv.x.s` instruction.
;; This instruction copies the first element of the source vector to the destination X register.
;; Masked versions of this instruction are not supported.
(decl rv_vmv_xs (VReg VState) XReg)
(rule (rv_vmv_xs vs vstate)
  (vec_alu_rr (VecAluOpRR.VmvXS) vs (unmasked) vstate))

;; Helper for emitting the `vfmv.f.s` instruction.
;; This instruction copies the first element of the source vector to the destination F register.
;; Masked versions of this instruction are not supported.
(decl rv_vfmv_fs (VReg VState) FReg)
(rule (rv_vfmv_fs vs vstate)
  (vec_alu_rr (VecAluOpRR.VfmvFS) vs (unmasked) vstate))

;; Helper for emitting the `vmv.s.x` instruction.
;; This instruction copies the source X register into first element of the source vector.
;; Masked versions of this instruction are not supported.
(decl rv_vmv_sx (XReg VState) VReg)
(rule (rv_vmv_sx vs vstate)
  (vec_alu_rr (VecAluOpRR.VmvSX) vs (unmasked) vstate))

;; Helper for emitting the `vfmv.s.f` instruction.
;; This instruction copies the source F register into first element of the source vector.
;; Masked versions of this instruction are not supported.
(decl rv_vfmv_sf (FReg VState) VReg)
(rule (rv_vfmv_sf vs vstate)
  (vec_alu_rr (VecAluOpRR.VfmvSF) vs (unmasked) vstate))

;; Helper for emitting the `vmv.v.x` instruction.
;; This instruction splats the X register into all elements of the destination vector.
;; Masked versions of this instruction are called `vmerge`
(decl rv_vmv_vx (XReg VState) VReg)
(rule (rv_vmv_vx vs vstate)
  (vec_alu_rr (VecAluOpRR.VmvVX) vs (unmasked) vstate))

;; Helper for emitting the `vfmv.v.f` instruction.
;; This instruction splats the F register into all elements of the destination vector.
;; Masked versions of this instruction are called `vmerge`
(decl rv_vfmv_vf (FReg VState) VReg)
(rule (rv_vfmv_vf vs vstate)
  (vec_alu_rr (VecAluOpRR.VfmvVF) vs (unmasked) vstate))

;; Helper for emitting the `vmv.v.i` instruction.
;; This instruction splat's the immediate value into all elements of the destination vector.
;; Masked versions of this instruction are called `vmerge`
(decl rv_vmv_vi (Imm5 VState) VReg)
(rule (rv_vmv_vi imm vstate)
  (vec_alu_r_imm5 (VecAluOpRImm5.VmvVI) imm (unmasked) vstate))

;; Helper for emitting the `vmerge.vvm` instruction.
;; This instruction merges the elements of the two source vectors into the destination vector
;; based on a mask. Elements are taken from the first source vector if the mask bit is clear,
;; and from the second source vector if the mask bit is set. This instruction is always masked.
;;
;; vd[i] = v0.mask[i] ? vs1[i] : vs2[i]
(decl rv_vmerge_vvm (VReg VReg VReg VState) VReg)
(rule (rv_vmerge_vvm vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmergeVVM) vs2 vs1 (masked mask) vstate))

;; Helper for emitting the `vmerge.vxm` instruction.
;; Elements are taken from the first source vector if the mask bit is clear, and from the X
;; register if the mask bit is set. This instruction is always masked.
;;
;; vd[i] = v0.mask[i] ? x[rs1] : vs2[i]
(decl rv_vmerge_vxm (VReg XReg VReg VState) VReg)
(rule (rv_vmerge_vxm vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmergeVXM) vs2 vs1 (masked mask) vstate))

;; Helper for emitting the `vfmerge.vfm` instruction.
;; Elements are taken from the first source vector if the mask bit is clear, and from the F
;; register if the mask bit is set. This instruction is always masked.
;;
;; vd[i] = v0.mask[i] ? f[rs1] : vs2[i]
(decl rv_vfmerge_vfm (VReg FReg VReg VState) VReg)
(rule (rv_vfmerge_vfm vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VfmergeVFM) vs2 vs1 (masked mask) vstate))

;; Helper for emitting the `vmerge.vim` instruction.
;; Elements are taken from the first source vector if the mask bit is clear, and from the
;; immediate value if the mask bit is set. This instruction is always masked.
;;
;; vd[i] = v0.mask[i] ? imm : vs2[i]
(decl rv_vmerge_vim (VReg Imm5 VReg VState) VReg)
(rule (rv_vmerge_vim vs2 imm mask vstate)
  (vec_alu_rr_imm5 (VecAluOpRRImm5.VmergeVIM) vs2 imm (masked mask) vstate))


;; Helper for emitting the `vredminu.vs` instruction.
;;
;; vd[0] = minu( vs1[0] , vs2[*] )
(decl rv_vredminu_vs (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vredminu_vs vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VredminuVS) vs2 vs1 mask vstate))

;; Helper for emitting the `vredmaxu.vs` instruction.
;;
;; vd[0] = maxu( vs1[0] , vs2[*] )
(decl rv_vredmaxu_vs (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vredmaxu_vs vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VredmaxuVS) vs2 vs1 mask vstate))

;; Helper for emitting the `vrgather.vv` instruction.
;;
;; vd[i] = (vs1[i] >= VLMAX) ? 0 : vs2[vs1[i]];
(decl rv_vrgather_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vrgather_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VrgatherVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vrgather.vx` instruction.
;;
;; vd[i] = (x[rs1] >= VLMAX) ? 0 : vs2[x[rs1]]
(decl rv_vrgather_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vrgather_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VrgatherVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vrgather.vi` instruction.
(decl rv_vrgather_vi (VReg UImm5 VecOpMasking VState) VReg)
(rule (rv_vrgather_vi vs2 imm mask vstate)
  (vec_alu_rr_uimm5 (VecAluOpRRImm5.VrgatherVI) vs2 imm mask vstate))

;; Helper for emitting the `vcompress.vm` instruction.
;;
;; The vector compress instruction allows elements selected by a vector mask
;; register from a source vector register group to be packed into contiguous
;; elements at the start of the destination vector register group.
;;
;; The mask register is specified through vs1
(decl rv_vcompress_vm (VReg VReg VState) VReg)
(rule (rv_vcompress_vm vs2 vs1 vstate)
  (vec_alu_rrr (VecAluOpRRR.VcompressVM) vs2 vs1 (unmasked) vstate))

;; Helper for emitting the `vmseq.vv` (Vector Mask Set If Equal) instruction.
(decl rv_vmseq_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vmseq_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmseqVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vmseq.vx` (Vector Mask Set If Equal) instruction.
(decl rv_vmseq_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vmseq_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmseqVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vmseq.vi` (Vector Mask Set If Equal) instruction.
(decl rv_vmseq_vi (VReg Imm5 VecOpMasking VState) VReg)
(rule (rv_vmseq_vi vs2 imm mask vstate)
  (vec_alu_rr_imm5 (VecAluOpRRImm5.VmseqVI) vs2 imm mask vstate))

;; Helper for emitting the `vmsne.vv` (Vector Mask Set If Not Equal) instruction.
(decl rv_vmsne_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vmsne_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmsneVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vmsne.vx` (Vector Mask Set If Not Equal) instruction.
(decl rv_vmsne_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vmsne_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmsneVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vmsne.vi` (Vector Mask Set If Not Equal) instruction.
(decl rv_vmsne_vi (VReg Imm5 VecOpMasking VState) VReg)
(rule (rv_vmsne_vi vs2 imm mask vstate)
  (vec_alu_rr_imm5 (VecAluOpRRImm5.VmsneVI) vs2 imm mask vstate))

;; Helper for emitting the `vmsltu.vv` (Vector Mask Set If Less Than, Unsigned) instruction.
(decl rv_vmsltu_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vmsltu_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmsltuVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vmsltu.vx` (Vector Mask Set If Less Than, Unsigned) instruction.
(decl rv_vmsltu_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vmsltu_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmsltuVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vmslt.vv` (Vector Mask Set If Less Than) instruction.
(decl rv_vmslt_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vmslt_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmsltVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vmslt.vx` (Vector Mask Set If Less Than) instruction.
(decl rv_vmslt_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vmslt_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmsltVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vmsleu.vv` (Vector Mask Set If Less Than or Equal, Unsigned) instruction.
(decl rv_vmsleu_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vmsleu_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmsleuVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vmsleu.vx` (Vector Mask Set If Less Than or Equal, Unsigned) instruction.
(decl rv_vmsleu_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vmsleu_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmsleuVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vmsleu.vi` (Vector Mask Set If Less Than or Equal, Unsigned) instruction.
(decl rv_vmsleu_vi (VReg Imm5 VecOpMasking VState) VReg)
(rule (rv_vmsleu_vi vs2 imm mask vstate)
  (vec_alu_rr_imm5 (VecAluOpRRImm5.VmsleuVI) vs2 imm mask vstate))

;; Helper for emitting the `vmsle.vv` (Vector Mask Set If Less Than or Equal) instruction.
(decl rv_vmsle_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vmsle_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmsleVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vmsle.vx` (Vector Mask Set If Less Than or Equal) instruction.
(decl rv_vmsle_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vmsle_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmsleVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vmsle.vi` (Vector Mask Set If Less Than or Equal) instruction.
(decl rv_vmsle_vi (VReg Imm5 VecOpMasking VState) VReg)
(rule (rv_vmsle_vi vs2 imm mask vstate)
  (vec_alu_rr_imm5 (VecAluOpRRImm5.VmsleVI) vs2 imm mask vstate))

;; Helper for emitting the `vmsgt.vv` (Vector Mask Set If Greater Than, Unsigned) instruction.
;; This is an alias for `vmsltu.vv` with the operands inverted.
(decl rv_vmsgtu_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vmsgtu_vv vs2 vs1 mask vstate) (rv_vmsltu_vv vs1 vs2 mask vstate))

;; Helper for emitting the `vmsgtu.vx` (Vector Mask Set If Greater Than, Unsigned) instruction.
(decl rv_vmsgtu_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vmsgtu_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmsgtuVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vmsgtu.vi` (Vector Mask Set If Greater Than, Unsigned) instruction.
(decl rv_vmsgtu_vi (VReg Imm5 VecOpMasking VState) VReg)
(rule (rv_vmsgtu_vi vs2 imm mask vstate)
  (vec_alu_rr_imm5 (VecAluOpRRImm5.VmsgtuVI) vs2 imm mask vstate))

;; Helper for emitting the `vmsgt.vv` (Vector Mask Set If Greater Than) instruction.
;; This is an alias for `vmslt.vv` with the operands inverted.
(decl rv_vmsgt_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vmsgt_vv vs2 vs1 mask vstate) (rv_vmslt_vv vs1 vs2 mask vstate))

;; Helper for emitting the `vmsgt.vx` (Vector Mask Set If Greater Than) instruction.
(decl rv_vmsgt_vx (VReg XReg VecOpMasking VState) VReg)
(rule (rv_vmsgt_vx vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmsgtVX) vs2 vs1 mask vstate))

;; Helper for emitting the `vmsgt.vi` (Vector Mask Set If Greater Than) instruction.
(decl rv_vmsgt_vi (VReg Imm5 VecOpMasking VState) VReg)
(rule (rv_vmsgt_vi vs2 imm mask vstate)
  (vec_alu_rr_imm5 (VecAluOpRRImm5.VmsgtVI) vs2 imm mask vstate))

;; Helper for emitting the `vmsgeu.vv` (Vector Mask Set If Greater Than or Equal, Unsigned) instruction.
;; This is an alias for `vmsleu.vv` with the operands inverted.
(decl rv_vmsgeu_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vmsgeu_vv vs2 vs1 mask vstate) (rv_vmsleu_vv vs1 vs2 mask vstate))

;; Helper for emitting the `vmsge.vv` (Vector Mask Set If Greater Than or Equal) instruction.
;; This is an alias for `vmsle.vv` with the operands inverted.
(decl rv_vmsge_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vmsge_vv vs2 vs1 mask vstate) (rv_vmsle_vv vs1 vs2 mask vstate))

;; Helper for emitting the `vmfeq.vv` (Vector Mask Set If Float Equal) instruction.
(decl rv_vmfeq_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vmfeq_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmfeqVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vmfeq.vf` (Vector Mask Set If Float Equal) instruction.
(decl rv_vmfeq_vf (VReg FReg VecOpMasking VState) VReg)
(rule (rv_vmfeq_vf vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmfeqVF) vs2 vs1 mask vstate))

;; Helper for emitting the `vmfne.vv` (Vector Mask Set If Float Not Equal) instruction.
(decl rv_vmfne_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vmfne_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmfneVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vmfne.vf` (Vector Mask Set If Float Not Equal) instruction.
(decl rv_vmfne_vf (VReg FReg VecOpMasking VState) VReg)
(rule (rv_vmfne_vf vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmfneVF) vs2 vs1 mask vstate))

;; Helper for emitting the `vmflt.vv` (Vector Mask Set If Float Less Than) instruction.
(decl rv_vmflt_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vmflt_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmfltVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vmflt.vf` (Vector Mask Set If Float Less Than) instruction.
(decl rv_vmflt_vf (VReg FReg VecOpMasking VState) VReg)
(rule (rv_vmflt_vf vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmfltVF) vs2 vs1 mask vstate))

;; Helper for emitting the `vmfle.vv` (Vector Mask Set If Float Less Than Or Equal) instruction.
(decl rv_vmfle_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vmfle_vv vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmfleVV) vs2 vs1 mask vstate))

;; Helper for emitting the `vmfle.vf` (Vector Mask Set If Float Less Than Or Equal) instruction.
(decl rv_vmfle_vf (VReg FReg VecOpMasking VState) VReg)
(rule (rv_vmfle_vf vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmfleVF) vs2 vs1 mask vstate))

;; Helper for emitting the `vmfgt.vv` (Vector Mask Set If Float Greater Than) instruction.
;; This is an alias for `vmflt.vv` with the operands inverted.
(decl rv_vmfgt_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vmfgt_vv vs2 vs1 mask vstate) (rv_vmflt_vv vs1 vs2 mask vstate))

;; Helper for emitting the `vmfgt.vf` (Vector Mask Set If Float Greater Than) instruction.
(decl rv_vmfgt_vf (VReg FReg VecOpMasking VState) VReg)
(rule (rv_vmfgt_vf vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmfgtVF) vs2 vs1 mask vstate))

;; Helper for emitting the `vmfge.vv` (Vector Mask Set If Float Greater Than Or Equal) instruction.
;; This is an alias for `vmfle.vv` with the operands inverted.
(decl rv_vmfge_vv (VReg VReg VecOpMasking VState) VReg)
(rule (rv_vmfge_vv vs2 vs1 mask vstate) (rv_vmfle_vv vs1 vs2 mask vstate))

;; Helper for emitting the `vmfge.vf` (Vector Mask Set If Float Greater Than Or Equal) instruction.
(decl rv_vmfge_vf (VReg FReg VecOpMasking VState) VReg)
(rule (rv_vmfge_vf vs2 vs1 mask vstate)
  (vec_alu_rrr (VecAluOpRRR.VmfgeVF) vs2 vs1 mask vstate))

;; Helper for emitting the `vzext.vf2` instruction.
;; Zero-extend SEW/2 source to SEW destination
(decl rv_vzext_vf2 (VReg VecOpMasking VState) VReg)
(rule (rv_vzext_vf2 vs mask vstate)
  (vec_alu_rr (VecAluOpRR.VzextVF2) vs mask vstate))

;; Helper for emitting the `vzext.vf4` instruction.
;; Zero-extend SEW/4 source to SEW destination
(decl rv_vzext_vf4 (VReg VecOpMasking VState) VReg)
(rule (rv_vzext_vf4 vs mask vstate)
  (vec_alu_rr (VecAluOpRR.VzextVF4) vs mask vstate))

;; Helper for emitting the `vzext.vf8` instruction.
;; Zero-extend SEW/8 source to SEW destination
(decl rv_vzext_vf8 (VReg VecOpMasking VState) VReg)
(rule (rv_vzext_vf8 vs mask vstate)
  (vec_alu_rr (VecAluOpRR.VzextVF8) vs mask vstate))

;; Helper for emitting the `vsext.vf2` instruction.
;; Sign-extend SEW/2 source to SEW destination
(decl rv_vsext_vf2 (VReg VecOpMasking VState) VReg)
(rule (rv_vsext_vf2 vs mask vstate)
  (vec_alu_rr (VecAluOpRR.VsextVF2) vs mask vstate))

;; Helper for emitting the `vsext.vf4` instruction.
;; Sign-extend SEW/4 source to SEW destination
(decl rv_vsext_vf4 (VReg VecOpMasking VState) VReg)
(rule (rv_vsext_vf4 vs mask vstate)
  (vec_alu_rr (VecAluOpRR.VsextVF4) vs mask vstate))

;; Helper for emitting the `vsext.vf8` instruction.
;; Sign-extend SEW/8 source to SEW destination
(decl rv_vsext_vf8 (VReg VecOpMasking VState) VReg)
(rule (rv_vsext_vf8 vs mask vstate)
  (vec_alu_rr (VecAluOpRR.VsextVF8) vs mask vstate))

;; Helper for emitting the `vnclip.wi` instruction.
;;
;; vd[i] = clip(roundoff_signed(vs2[i], uimm))
(decl rv_vnclip_wi (VReg UImm5 VecOpMasking VState) VReg)
(rule (rv_vnclip_wi vs2 imm mask vstate)
  (vec_alu_rr_uimm5 (VecAluOpRRImm5.VnclipWI) vs2 imm mask vstate))

;; Helper for emitting the `vnclipu.wi` instruction.
;;
;; vd[i] = clip(roundoff_unsigned(vs2[i], uimm))
(decl rv_vnclipu_wi (VReg UImm5 VecOpMasking VState) VReg)
(rule (rv_vnclipu_wi vs2 imm mask vstate)
  (vec_alu_rr_uimm5 (VecAluOpRRImm5.VnclipuWI) vs2 imm mask vstate))

;; Helper for emitting the `vmand.mm` (Mask Bitwise AND) instruction.
;;
;; vd.mask[i] = vs2.mask[i] &&  vs1.mask[i]
(decl rv_vmand_mm (VReg VReg VState) VReg)
(rule (rv_vmand_mm vs2 vs1 vstate)
  (vec_alu_rrr (VecAluOpRRR.VmandMM) vs2 vs1 (unmasked) vstate))

;; Helper for emitting the `vmor.mm` (Mask Bitwise OR) instruction.
;;
;; vd.mask[i] = vs2.mask[i] ||  vs1.mask[i]
(decl rv_vmor_mm (VReg VReg VState) VReg)
(rule (rv_vmor_mm vs2 vs1 vstate)
  (vec_alu_rrr (VecAluOpRRR.VmorMM) vs2 vs1 (unmasked) vstate))

;; Helper for emitting the `vmnand.mm` (Mask Bitwise NAND) instruction.
;;
;; vd.mask[i] = !(vs2.mask[i] &&  vs1.mask[i])
(decl rv_vmnand_mm (VReg VReg VState) VReg)
(rule (rv_vmnand_mm vs2 vs1 vstate)
  (vec_alu_rrr (VecAluOpRRR.VmnandMM) vs2 vs1 (unmasked) vstate))

;; Helper for emitting the `vmnot.m` (Mask Bitwise NOT) instruction.
;; This is an alias for `vmnand.mm vd, vs, vs`
;;
;; vd.mask[i] = !vs.mask[i]
(decl rv_vmnot_m (VReg VState) VReg)
(rule (rv_vmnot_m vs vstate) (rv_vmnand_mm vs vs vstate))

;; Helper for emitting the `vmnor.mm` (Mask Bitwise NOR) instruction.
;;
;; vd.mask[i] = !(vs2.mask[i] ||  vs1.mask[i])
(decl rv_vmnor_mm (VReg VReg VState) VReg)
(rule (rv_vmnor_mm vs2 vs1 vstate)
  (vec_alu_rrr (VecAluOpRRR.VmnorMM) vs2 vs1 (unmasked) vstate))

;;;; Multi-Instruction Helpers ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; Recursion: recursive rules reduce to the index zero case, which are handled
; with higher-priority rules.
(decl rec gen_extractlane (Type VReg u8) Reg)

;; When extracting lane 0 for floats, we can use `vfmv.f.s` directly.
(rule 3 (gen_extractlane (ty_vec_fits_in_register ty) src 0)
  (if (ty_vector_float ty))
  (rv_vfmv_fs src ty))

;; When extracting lane 0 for integers, we can use `vmv.x.s` directly.
(rule 2 (gen_extractlane (ty_vec_fits_in_register ty) src 0)
  (if (ty_vector_not_float ty))
  (rv_vmv_xs src ty))

;; In the general case, we must first use a `vslidedown` to place the correct lane
;; in index 0, and then use the appropriate `vmv` instruction.
;; If the index fits into a 5-bit immediate, we can emit a `vslidedown.vi`.
(rule 1 (gen_extractlane (ty_vec_fits_in_register ty) src (uimm5_from_u8 idx))
  (gen_extractlane ty (rv_vslidedown_vi src idx (unmasked) ty) 0))

;; Otherwise lower it into an X register.
(rule 0 (gen_extractlane (ty_vec_fits_in_register ty) src idx)
  (gen_extractlane ty (rv_vslidedown_vx src (imm $I64 idx) (unmasked) ty) 0))


;; Build a vector mask from a u64
;; TODO(#6571): We should merge this with the `vconst` rules, and take advantage of
;; the other existing `vconst` rules.
(decl gen_vec_mask (u64) VReg)

;; When the immediate fits in a 5-bit immediate, we can use `vmv.v.i` directly.
(rule 1 (gen_vec_mask (imm5_from_u64 imm))
  (rv_vmv_vi imm (vstate_from_type $I64X2)))

;; Materialize the mask into an X register, and move it into the bottom of
;; the vector register.
(rule 0 (gen_vec_mask mask)
  (rv_vmv_sx (imm $I64 mask) (vstate_from_type $I64X2)))


;; Loads a `VCodeConstant` value into a vector register. For some special `VCodeConstant`s
;; we can use a dedicated instruction, otherwise we load the value from the pool.
;;
;; Type is the preferred type to use when loading the constant.
(decl gen_constant (Type VCodeConstant) VReg)

;; The fallback case is to load the constant from the pool.
(rule (gen_constant ty n)
  (vec_load
    (element_width_from_type ty)
    (VecAMode.UnitStride (gen_const_amode n))
    (mem_flags_trusted)
    (unmasked)
    ty))


;; Emits a vslidedown instruction that moves half the lanes down.
(decl gen_slidedown_half (Type VReg) VReg)

;; If the lane count can fit in a 5-bit immediate, we can use `vslidedown.vi`.
(rule 1 (gen_slidedown_half (ty_vec_fits_in_register ty) src)
  (if-let (uimm5_from_u64 amt) (u64_checked_div (ty_lane_count ty) 2))
  (rv_vslidedown_vi src amt (unmasked) ty))

;; Otherwise lower it into an X register.
(rule 0 (gen_slidedown_half (ty_vec_fits_in_register ty) src)
  (if-let amt (u64_checked_div (ty_lane_count ty) 2))
  (rv_vslidedown_vx src (imm $I64 amt) (unmasked) ty))


;; Expands a mask into SEW wide lanes. Enabled lanes are set to all ones, disabled
;; lanes are set to all zeros.
(decl gen_expand_mask (Type VReg) VReg)
(rule (gen_expand_mask ty mask)
  (if-let zero (i8_to_imm5 0))
  (if-let neg1 (i8_to_imm5 -1))
  (rv_vmerge_vim (rv_vmv_vi zero ty) neg1 mask ty))


;; Builds a vector mask corresponding to the IntCC operation.
;; TODO: We are still missing some rules here for immediates. See #6623
(decl gen_icmp_mask (Type IntCC Value Value) VReg)

;; IntCC.Equal

(rule 0 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.Equal) x y)
  (rv_vmseq_vv x y (unmasked) ty))

(rule 1 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.Equal) x (splat y))
  (rv_vmseq_vx x y (unmasked) ty))

(rule 2 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.Equal) (splat x) y)
  (rv_vmseq_vx y x (unmasked) ty))

(rule 3 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.Equal) x y)
  (if-let y_imm (replicated_imm5 y))
  (rv_vmseq_vi x y_imm (unmasked) ty))

(rule 4 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.Equal) x y)
  (if-let x_imm (replicated_imm5 x))
  (rv_vmseq_vi y x_imm (unmasked) ty))

;; IntCC.NotEqual

(rule 0 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.NotEqual) x y)
  (rv_vmsne_vv x y (unmasked) ty))

(rule 1 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.NotEqual) x (splat y))
  (rv_vmsne_vx x y (unmasked) ty))

(rule 2 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.NotEqual) (splat x) y)
  (rv_vmsne_vx y x (unmasked) ty))

(rule 3 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.NotEqual) x y)
  (if-let y_imm (replicated_imm5 y))
  (rv_vmsne_vi x y_imm (unmasked) ty))

(rule 4 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.NotEqual) x y)
  (if-let x_imm (replicated_imm5 x))
  (rv_vmsne_vi y x_imm (unmasked) ty))

;; IntCC.UnsignedLessThan

(rule 0 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.UnsignedLessThan) x y)
  (rv_vmsltu_vv x y (unmasked) ty))

(rule 1 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.UnsignedLessThan) x (splat y))
  (rv_vmsltu_vx x y (unmasked) ty))

(rule 2 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.UnsignedLessThan) (splat x) y)
  (rv_vmsgtu_vx y x (unmasked) ty))

(rule 4 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.UnsignedLessThan) x y)
  (if-let x_imm (replicated_imm5 x))
  (rv_vmsgtu_vi y x_imm (unmasked) ty))

;; IntCC.SignedLessThan

(rule 0 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.SignedLessThan) x y)
  (rv_vmslt_vv x y (unmasked) ty))

(rule 1 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.SignedLessThan) x (splat y))
  (rv_vmslt_vx x y (unmasked) ty))

(rule 2 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.SignedLessThan) (splat x) y)
  (rv_vmsgt_vx y x (unmasked) ty))

(rule 4 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.SignedLessThan) x y)
  (if-let x_imm (replicated_imm5 x))
  (rv_vmsgt_vi y x_imm (unmasked) ty))

;; IntCC.UnsignedLessThanOrEqual

(rule 0 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.UnsignedLessThanOrEqual) x y)
  (rv_vmsleu_vv x y (unmasked) ty))

(rule 1 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.UnsignedLessThanOrEqual) x (splat y))
  (rv_vmsleu_vx x y (unmasked) ty))

(rule 3 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.UnsignedLessThanOrEqual) x y)
  (if-let y_imm (replicated_imm5 y))
  (rv_vmsleu_vi x y_imm (unmasked) ty))

;; IntCC.SignedLessThanOrEqual

(rule 0 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.SignedLessThanOrEqual) x y)
  (rv_vmsle_vv x y (unmasked) ty))

(rule 1 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.SignedLessThanOrEqual) x (splat y))
  (rv_vmsle_vx x y (unmasked) ty))

(rule 3 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.SignedLessThanOrEqual) x y)
  (if-let y_imm (replicated_imm5 y))
  (rv_vmsle_vi x y_imm (unmasked) ty))

;; IntCC.UnsignedGreaterThan

(rule 0 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.UnsignedGreaterThan) x y)
  (rv_vmsgtu_vv x y (unmasked) ty))

(rule 1 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.UnsignedGreaterThan) x (splat y))
  (rv_vmsgtu_vx x y (unmasked) ty))

(rule 2 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.UnsignedGreaterThan) (splat x) y)
  (rv_vmsltu_vx y x (unmasked) ty))

(rule 3 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.UnsignedGreaterThan) x y)
  (if-let y_imm (replicated_imm5 y))
  (rv_vmsgtu_vi x y_imm (unmasked) ty))

;; IntCC.SignedGreaterThan

(rule 0 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.SignedGreaterThan) x y)
  (rv_vmsgt_vv x y (unmasked) ty))

(rule 1 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.SignedGreaterThan) x (splat y))
  (rv_vmsgt_vx x y (unmasked) ty))

(rule 2 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.SignedGreaterThan) (splat x) y)
  (rv_vmslt_vx y x (unmasked) ty))

(rule 3 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.SignedGreaterThan) x y)
  (if-let y_imm (replicated_imm5 y))
  (rv_vmsgt_vi x y_imm (unmasked) ty))

;; IntCC.UnsignedGreaterThanOrEqual

(rule 0 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.UnsignedGreaterThanOrEqual) x y)
  (rv_vmsgeu_vv x y (unmasked) ty))

(rule 2 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.UnsignedGreaterThanOrEqual) (splat x) y)
  (rv_vmsleu_vx y x (unmasked) ty))

(rule 4 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.UnsignedGreaterThanOrEqual) x y)
  (if-let x_imm (replicated_imm5 x))
  (rv_vmsleu_vi y x_imm (unmasked) ty))

;; IntCC.SignedGreaterThanOrEqual

(rule 0 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.SignedGreaterThanOrEqual) x y)
  (rv_vmsge_vv x y (unmasked) ty))

(rule 2 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.SignedGreaterThanOrEqual) (splat x) y)
  (rv_vmsle_vx y x (unmasked) ty))

(rule 4 (gen_icmp_mask (ty_vec_fits_in_register ty) (IntCC.SignedGreaterThanOrEqual) x y)
  (if-let x_imm (replicated_imm5 x))
  (rv_vmsle_vi y x_imm (unmasked) ty))



;; Builds a vector mask corresponding to the FloatCC operation.
;;
;; Recursion: recursive rules implement some condition codes in terms of a
;; smaller set of primtives, which recursive rules would not apply to twice.
(decl rec gen_fcmp_mask (Type FloatCC Value Value) VReg)

;; FloatCC.Equal

(rule 0 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.Equal) x y)
  (rv_vmfeq_vv x y (unmasked) ty))

(rule 1 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.Equal) x (splat y))
  (rv_vmfeq_vf x y (unmasked) ty))

(rule 2 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.Equal) (splat x) y)
  (rv_vmfeq_vf y x (unmasked) ty))

;; FloatCC.NotEqual
;; Note: This is UnorderedNotEqual. It is the only unordered comparison that is not named as such.

(rule 0 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.NotEqual) x y)
  (rv_vmfne_vv x y (unmasked) ty))

(rule 1 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.NotEqual) x (splat y))
  (rv_vmfne_vf x y (unmasked) ty))

(rule 2 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.NotEqual) (splat x) y)
  (rv_vmfne_vf y x (unmasked) ty))

;; FloatCC.LessThan

(rule 0 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.LessThan) x y)
  (rv_vmflt_vv x y (unmasked) ty))

(rule 1 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.LessThan) x (splat y))
  (rv_vmflt_vf x y (unmasked) ty))

(rule 2 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.LessThan) (splat x) y)
  (rv_vmfgt_vf y x (unmasked) ty))

;; FloatCC.LessThanOrEqual

(rule 0 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.LessThanOrEqual) x y)
  (rv_vmfle_vv x y (unmasked) ty))

(rule 1 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.LessThanOrEqual) x (splat y))
  (rv_vmfle_vf x y (unmasked) ty))

(rule 2 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.LessThanOrEqual) (splat x) y)
  (rv_vmfge_vf y x (unmasked) ty))

;; FloatCC.GreaterThan

(rule 0 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.GreaterThan) x y)
  (rv_vmfgt_vv x y (unmasked) ty))

(rule 1 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.GreaterThan) x (splat y))
  (rv_vmfgt_vf x y (unmasked) ty))

(rule 2 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.GreaterThan) (splat x) y)
  (rv_vmflt_vf y x (unmasked) ty))

;; FloatCC.GreaterThanOrEqual

(rule 0 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.GreaterThanOrEqual) x y)
  (rv_vmfge_vv x y (unmasked) ty))

(rule 1 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.GreaterThanOrEqual) x (splat y))
  (rv_vmfge_vf x y (unmasked) ty))

(rule 2 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.GreaterThanOrEqual) (splat x) y)
  (rv_vmfle_vf y x (unmasked) ty))

;; FloatCC.Ordered

(rule 0 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.Ordered) x y)
  (rv_vmand_mm
    (gen_fcmp_mask ty (FloatCC.Equal) x x)
    (gen_fcmp_mask ty (FloatCC.Equal) y y)
    ty))

;; FloatCC.Unordered

(rule 0 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.Unordered) x y)
  (rv_vmor_mm
    (gen_fcmp_mask ty (FloatCC.NotEqual) x x)
    (gen_fcmp_mask ty (FloatCC.NotEqual) y y)
    ty))

;; FloatCC.OrderedNotEqual

(rule 0 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.OrderedNotEqual) x y)
  (rv_vmor_mm
    (gen_fcmp_mask ty (FloatCC.LessThan) x y)
    (gen_fcmp_mask ty (FloatCC.LessThan) y x)
    ty))

;; FloatCC.UnorderedOrEqual

(rule 0 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.UnorderedOrEqual) x y)
  (rv_vmnor_mm
    (gen_fcmp_mask ty (FloatCC.LessThan) x y)
    (gen_fcmp_mask ty (FloatCC.LessThan) y x)
    ty))

;; FloatCC.UnorderedOrGreaterThan

(rule 0 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.UnorderedOrGreaterThan) x y)
  (rv_vmnot_m (gen_fcmp_mask ty (FloatCC.LessThanOrEqual) x y) ty))

;; FloatCC.UnorderedOrGreaterThanOrEqual

(rule 0 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.UnorderedOrGreaterThanOrEqual) x y)
  (rv_vmnot_m (gen_fcmp_mask ty (FloatCC.LessThan) x y) ty))

;; FloatCC.UnorderedOrLessThan

(rule 0 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.UnorderedOrLessThan) x y)
  (rv_vmnot_m (gen_fcmp_mask ty (FloatCC.GreaterThanOrEqual) x y) ty))

;; FloatCC.UnorderedOrLessThanOrEqual

(rule 0 (gen_fcmp_mask (ty_vec_fits_in_register ty) (FloatCC.UnorderedOrLessThanOrEqual) x y)
  (rv_vmnot_m (gen_fcmp_mask ty (FloatCC.GreaterThan) x y) ty))


;; Emits a `vfcvt.x.f.v` instruction with the given rounding mode.
(decl gen_vfcvt_x_f (VReg FRM VState) VReg)

;; We have a special instruction for RTZ
(rule 1 (gen_vfcvt_x_f x (FRM.RTZ) vstate)
  (rv_vfcvt_rtz_x_f_v x (unmasked) vstate))

;; In the general case we need to first switch into the appropriate rounding mode.
(rule 0 (gen_vfcvt_x_f x frm vstate)
  (let (
        ;; Set the rounding mode and save the current mode
        (saved_frm XReg (rv_fsrmi frm))
        (res VReg (rv_vfcvt_x_f_v x (unmasked) vstate))
        ;; Restore the previous rounding mode
        (_ Unit (rv_fsrm saved_frm)))
    res))


;; Returns the maximum value integer value that can be represented by a float
(decl float_int_max (Type) u64)
(rule (float_int_max $F32) 0x4B000000)
(rule (float_int_max $F64) 0x4330000000000000)

;; Builds the instruction sequence to round a vector register to FRM
(decl gen_vec_round (VReg FRM Type) VReg)

;; For floating-point round operations, if the input is NaN, +/-infinity, or +/-0, the
;; same input is returned as the rounded result; this differs from behavior of
;; RISCV fcvt instructions (which round out-of-range values to the nearest
;; max or min value), therefore special handling is needed for these values.
(rule (gen_vec_round x frm (ty_vec_fits_in_register ty))
  (let ((scalar_ty Type (lane_type ty))
        ;; if x is NaN/+-Infinity/+-Zero or if the exponent is larger than # of bits
        ;; in mantissa, the result is the same as src, build a mask for those cases.
        ;; (There is an additional fixup for NaN's at the end)
        (abs VReg (rv_vfabs_v x (unmasked) ty))
        (max FReg (imm scalar_ty (float_int_max scalar_ty)))
        (exact VReg (rv_vmflt_vf abs max (unmasked) ty))

        ;; The rounding is performed by converting from float to integer, with the
        ;; desired rounding mode. And then converting back with the default rounding
        ;; mode.
        (int VReg (gen_vfcvt_x_f x frm ty))
        (cvt VReg (rv_vfcvt_f_x_v int (unmasked) ty))
        ;; Copy the sign bit from the original value.
        (signed VReg (rv_vfsgnj_vv cvt x (unmasked) ty))

        ;; We want to return a arithmetic nan if the input is a canonical nan.
        ;; Convert them by adding 0.0 to the input.
        (float_zero FReg (gen_bitcast (zero_reg) (float_int_of_same_size scalar_ty) scalar_ty))
        (corrected_nan VReg (rv_vfadd_vf x float_zero (unmasked) ty)))
    ;; Merge the original value if it does not need rounding, or the rounded value
    (rv_vmerge_vvm corrected_nan signed exact ty)))