xref: /openbmc/qemu/target/riscv/insn_trans/trans_rvv.c.inc (revision 43a4f232fa8d8cab2c748112bba363e92da1ea37)
1/*
2 *
3 * Copyright (c) 2020 T-Head Semiconductor Co., Ltd. All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2 or later, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17#include "tcg/tcg-op-gvec.h"
18#include "tcg/tcg-gvec-desc.h"
19#include "internals.h"
20
21static inline bool is_overlapped(const int8_t astart, int8_t asize,
22                                 const int8_t bstart, int8_t bsize)
23{
24    const int8_t aend = astart + asize;
25    const int8_t bend = bstart + bsize;
26
27    return MAX(aend, bend) - MIN(astart, bstart) < asize + bsize;
28}
29
30static bool require_rvv(DisasContext *s)
31{
32    return s->mstatus_vs != EXT_STATUS_DISABLED;
33}
34
35static bool require_rvf(DisasContext *s)
36{
37    if (s->mstatus_fs == EXT_STATUS_DISABLED) {
38        return false;
39    }
40
41    switch (s->sew) {
42    case MO_16:
43        return s->cfg_ptr->ext_zvfh;
44    case MO_32:
45        return s->cfg_ptr->ext_zve32f;
46    case MO_64:
47        return s->cfg_ptr->ext_zve64d;
48    default:
49        return false;
50    }
51}
52
53static bool require_rvfmin(DisasContext *s)
54{
55    if (s->mstatus_fs == EXT_STATUS_DISABLED) {
56        return false;
57    }
58
59    switch (s->sew) {
60    case MO_16:
61        return s->cfg_ptr->ext_zvfhmin;
62    case MO_32:
63        return s->cfg_ptr->ext_zve32f;
64    default:
65        return false;
66    }
67}
68
69static bool require_scale_rvf(DisasContext *s)
70{
71    if (s->mstatus_fs == EXT_STATUS_DISABLED) {
72        return false;
73    }
74
75    switch (s->sew) {
76    case MO_8:
77        return s->cfg_ptr->ext_zvfh;
78    case MO_16:
79        return s->cfg_ptr->ext_zve32f;
80    case MO_32:
81        return s->cfg_ptr->ext_zve64d;
82    default:
83        return false;
84    }
85}
86
87static bool require_scale_rvfmin(DisasContext *s)
88{
89    if (s->mstatus_fs == EXT_STATUS_DISABLED) {
90        return false;
91    }
92
93    switch (s->sew) {
94    case MO_16:
95        return s->cfg_ptr->ext_zve32f;
96    case MO_32:
97        return s->cfg_ptr->ext_zve64d;
98    default:
99        return false;
100    }
101}
102
103/*
104 * Source and destination vector register groups cannot overlap source mask
105 * register:
106 *
107 * A vector register cannot be used to provide source operands with more than
108 * one EEW for a single instruction. A mask register source is considered to
109 * have EEW=1 for this constraint. An encoding that would result in the same
110 * vector register being read with two or more different EEWs, including when
111 * the vector register appears at different positions within two or more vector
112 * register groups, is reserved.
113 * (Section 5.2)
114 *
115 * A destination vector register group can overlap a source vector
116 * register group only if one of the following holds:
117 *  1. The destination EEW equals the source EEW.
118 *  2. The destination EEW is smaller than the source EEW and the overlap
119 *     is in the lowest-numbered part of the source register group.
120 *  3. The destination EEW is greater than the source EEW, the source EMUL
121 *     is at least 1, and the overlap is in the highest-numbered part of
122 *     the destination register group.
123 * For the purpose of determining register group overlap constraints, mask
124 * elements have EEW=1.
125 * (Section 5.2)
126 */
127static bool require_vm(int vm, int v)
128{
129    return (vm != 0 || v != 0);
130}
131
132static bool require_nf(int vd, int nf, int lmul)
133{
134    int size = nf << MAX(lmul, 0);
135    return size <= 8 && vd + size <= 32;
136}
137
138/*
139 * Vector register should aligned with the passed-in LMUL (EMUL).
140 * If LMUL < 0, i.e. fractional LMUL, any vector register is allowed.
141 */
142static bool require_align(const int8_t val, const int8_t lmul)
143{
144    return lmul <= 0 || extract32(val, 0, lmul) == 0;
145}
146
147/*
148 * A destination vector register group can overlap a source vector
149 * register group only if one of the following holds:
150 *  1. The destination EEW equals the source EEW.
151 *  2. The destination EEW is smaller than the source EEW and the overlap
152 *     is in the lowest-numbered part of the source register group.
153 *  3. The destination EEW is greater than the source EEW, the source EMUL
154 *     is at least 1, and the overlap is in the highest-numbered part of
155 *     the destination register group.
156 * (Section 5.2)
157 *
158 * This function returns true if one of the following holds:
159 *  * Destination vector register group does not overlap a source vector
160 *    register group.
161 *  * Rule 3 met.
162 * For rule 1, overlap is allowed so this function doesn't need to be called.
163 * For rule 2, (vd == vs). Caller has to check whether: (vd != vs) before
164 * calling this function.
165 */
166static bool require_noover(const int8_t dst, const int8_t dst_lmul,
167                           const int8_t src, const int8_t src_lmul)
168{
169    int8_t dst_size = dst_lmul <= 0 ? 1 : 1 << dst_lmul;
170    int8_t src_size = src_lmul <= 0 ? 1 : 1 << src_lmul;
171
172    /* Destination EEW is greater than the source EEW, check rule 3. */
173    if (dst_size > src_size) {
174        if (dst < src &&
175            src_lmul >= 0 &&
176            is_overlapped(dst, dst_size, src, src_size) &&
177            !is_overlapped(dst, dst_size, src + src_size, src_size)) {
178            return true;
179        }
180    }
181
182    return !is_overlapped(dst, dst_size, src, src_size);
183}
184
185static bool do_vsetvl(DisasContext *s, int rd, int rs1, TCGv s2)
186{
187    TCGv s1, dst;
188
189    if (!require_rvv(s) || !s->cfg_ptr->ext_zve32x) {
190        return false;
191    }
192
193    dst = dest_gpr(s, rd);
194
195    if (rd == 0 && rs1 == 0) {
196        s1 = tcg_temp_new();
197        tcg_gen_mov_tl(s1, cpu_vl);
198    } else if (rs1 == 0) {
199        /* As the mask is at least one bit, RV_VLEN_MAX is >= VLMAX */
200        s1 = tcg_constant_tl(RV_VLEN_MAX);
201    } else {
202        s1 = get_gpr(s, rs1, EXT_ZERO);
203    }
204
205    gen_helper_vsetvl(dst, tcg_env, s1, s2);
206    gen_set_gpr(s, rd, dst);
207    finalize_rvv_inst(s);
208
209    gen_update_pc(s, s->cur_insn_len);
210    lookup_and_goto_ptr(s);
211    s->base.is_jmp = DISAS_NORETURN;
212    return true;
213}
214
215static bool do_vsetivli(DisasContext *s, int rd, TCGv s1, TCGv s2)
216{
217    TCGv dst;
218
219    if (!require_rvv(s) || !s->cfg_ptr->ext_zve32x) {
220        return false;
221    }
222
223    dst = dest_gpr(s, rd);
224
225    gen_helper_vsetvl(dst, tcg_env, s1, s2);
226    gen_set_gpr(s, rd, dst);
227    finalize_rvv_inst(s);
228    gen_update_pc(s, s->cur_insn_len);
229    lookup_and_goto_ptr(s);
230    s->base.is_jmp = DISAS_NORETURN;
231
232    return true;
233}
234
235static bool trans_vsetvl(DisasContext *s, arg_vsetvl *a)
236{
237    TCGv s2 = get_gpr(s, a->rs2, EXT_ZERO);
238    return do_vsetvl(s, a->rd, a->rs1, s2);
239}
240
241static bool trans_vsetvli(DisasContext *s, arg_vsetvli *a)
242{
243    TCGv s2 = tcg_constant_tl(a->zimm);
244    return do_vsetvl(s, a->rd, a->rs1, s2);
245}
246
247static bool trans_vsetivli(DisasContext *s, arg_vsetivli *a)
248{
249    TCGv s1 = tcg_constant_tl(a->rs1);
250    TCGv s2 = tcg_constant_tl(a->zimm);
251    return do_vsetivli(s, a->rd, s1, s2);
252}
253
254/* vector register offset from env */
255static uint32_t vreg_ofs(DisasContext *s, int reg)
256{
257    return offsetof(CPURISCVState, vreg) + reg * s->cfg_ptr->vlenb;
258}
259
260/* check functions */
261
262/*
263 * Vector unit-stride, strided, unit-stride segment, strided segment
264 * store check function.
265 *
266 * Rules to be checked here:
267 *   1. EMUL must within the range: 1/8 <= EMUL <= 8. (Section 7.3)
268 *   2. Destination vector register number is multiples of EMUL.
269 *      (Section 3.4.2, 7.3)
270 *   3. The EMUL setting must be such that EMUL * NFIELDS ≤ 8. (Section 7.8)
271 *   4. Vector register numbers accessed by the segment load or store
272 *      cannot increment past 31. (Section 7.8)
273 */
274static bool vext_check_store(DisasContext *s, int vd, int nf, uint8_t eew)
275{
276    int8_t emul = eew - s->sew + s->lmul;
277    return (emul >= -3 && emul <= 3) &&
278           require_align(vd, emul) &&
279           require_nf(vd, nf, emul);
280}
281
282/*
283 * Vector unit-stride, strided, unit-stride segment, strided segment
284 * load check function.
285 *
286 * Rules to be checked here:
287 *   1. All rules applies to store instructions are applies
288 *      to load instructions.
289 *   2. Destination vector register group for a masked vector
290 *      instruction cannot overlap the source mask register (v0).
291 *      (Section 5.3)
292 */
293static bool vext_check_load(DisasContext *s, int vd, int nf, int vm,
294                            uint8_t eew)
295{
296    return vext_check_store(s, vd, nf, eew) && require_vm(vm, vd);
297}
298
299/*
300 * Vector indexed, indexed segment store check function.
301 *
302 * Rules to be checked here:
303 *   1. EMUL must within the range: 1/8 <= EMUL <= 8. (Section 7.3)
304 *   2. Index vector register number is multiples of EMUL.
305 *      (Section 3.4.2, 7.3)
306 *   3. Destination vector register number is multiples of LMUL.
307 *      (Section 3.4.2, 7.3)
308 *   4. The EMUL setting must be such that EMUL * NFIELDS ≤ 8. (Section 7.8)
309 *   5. Vector register numbers accessed by the segment load or store
310 *      cannot increment past 31. (Section 7.8)
311 */
312static bool vext_check_st_index(DisasContext *s, int vd, int vs2, int nf,
313                                uint8_t eew)
314{
315    int8_t emul = eew - s->sew + s->lmul;
316    bool ret = (emul >= -3 && emul <= 3) &&
317               require_align(vs2, emul) &&
318               require_align(vd, s->lmul) &&
319               require_nf(vd, nf, s->lmul);
320
321    /*
322     * V extension supports all vector load and store instructions,
323     * except V extension does not support EEW=64 for index values
324     * when XLEN=32. (Section 18.3)
325     */
326    if (get_xl(s) == MXL_RV32) {
327        ret &= (eew != MO_64);
328    }
329
330    return ret;
331}
332
333/*
334 * Vector indexed, indexed segment load check function.
335 *
336 * Rules to be checked here:
337 *   1. All rules applies to store instructions are applies
338 *      to load instructions.
339 *   2. Destination vector register group for a masked vector
340 *      instruction cannot overlap the source mask register (v0).
341 *      (Section 5.3)
342 *   3. Destination vector register cannot overlap a source vector
343 *      register (vs2) group.
344 *      (Section 5.2)
345 *   4. Destination vector register groups cannot overlap
346 *      the source vector register (vs2) group for
347 *      indexed segment load instructions. (Section 7.8.3)
348 */
349static bool vext_check_ld_index(DisasContext *s, int vd, int vs2,
350                                int nf, int vm, uint8_t eew)
351{
352    int8_t seg_vd;
353    int8_t emul = eew - s->sew + s->lmul;
354    bool ret = vext_check_st_index(s, vd, vs2, nf, eew) &&
355               require_vm(vm, vd);
356
357    /* Each segment register group has to follow overlap rules. */
358    for (int i = 0; i < nf; ++i) {
359        seg_vd = vd + (1 << MAX(s->lmul, 0)) * i;
360
361        if (eew > s->sew) {
362            if (seg_vd != vs2) {
363                ret &= require_noover(seg_vd, s->lmul, vs2, emul);
364            }
365        } else if (eew < s->sew) {
366            ret &= require_noover(seg_vd, s->lmul, vs2, emul);
367        }
368
369        /*
370         * Destination vector register groups cannot overlap
371         * the source vector register (vs2) group for
372         * indexed segment load instructions.
373         */
374        if (nf > 1) {
375            ret &= !is_overlapped(seg_vd, 1 << MAX(s->lmul, 0),
376                                  vs2, 1 << MAX(emul, 0));
377        }
378    }
379    return ret;
380}
381
382/*
383 * Check whether a vector register is used to provide source operands with
384 * more than one EEW for the vector instruction.
385 * Returns true if the instruction has valid encoding
386 * Returns false if encoding violates the mismatched input EEWs constraint
387 */
388static bool vext_check_input_eew(DisasContext *s, int vs1, uint8_t eew_vs1,
389                                 int vs2, uint8_t eew_vs2, int vm)
390{
391    bool is_valid = true;
392    int8_t emul_vs1 = eew_vs1 - s->sew + s->lmul;
393    int8_t emul_vs2 = eew_vs2 - s->sew + s->lmul;
394
395    /* When vm is 0, vs1 & vs2(EEW!=1) group can't overlap v0 (EEW=1) */
396    if ((vs1 != -1 && !require_vm(vm, vs1)) ||
397        (vs2 != -1 && !require_vm(vm, vs2))) {
398        is_valid = false;
399    }
400
401    /* When eew_vs1 != eew_vs2, check whether vs1 and vs2 are overlapped */
402    if ((vs1 != -1 && vs2 != -1) && (eew_vs1 != eew_vs2) &&
403        is_overlapped(vs1, 1 << MAX(emul_vs1, 0),
404                      vs2, 1 << MAX(emul_vs2, 0))) {
405        is_valid = false;
406    }
407
408    return is_valid;
409}
410
411static bool vext_check_ss(DisasContext *s, int vd, int vs, int vm)
412{
413    return require_vm(vm, vd) &&
414           require_align(vd, s->lmul) &&
415           require_align(vs, s->lmul) &&
416           vext_check_input_eew(s, vs, s->sew, -1, s->sew, vm);
417}
418
419/*
420 * Check function for vector instruction with format:
421 * single-width result and single-width sources (SEW = SEW op SEW)
422 *
423 * Rules to be checked here:
424 *   1. Destination vector register group for a masked vector
425 *      instruction cannot overlap the source mask register (v0).
426 *      (Section 5.3)
427 *   2. Destination vector register number is multiples of LMUL.
428 *      (Section 3.4.2)
429 *   3. Source (vs2, vs1) vector register number are multiples of LMUL.
430 *      (Section 3.4.2)
431 */
432static bool vext_check_sss(DisasContext *s, int vd, int vs1, int vs2, int vm)
433{
434    return vext_check_ss(s, vd, vs2, vm) &&
435           vext_check_input_eew(s, vs1, s->sew, vs2, s->sew, vm) &&
436           require_align(vs1, s->lmul);
437}
438
439static bool vext_check_ms(DisasContext *s, int vd, int vs)
440{
441    bool ret = require_align(vs, s->lmul);
442    if (vd != vs) {
443        ret &= require_noover(vd, 0, vs, s->lmul);
444    }
445    return ret;
446}
447
448/*
449 * Check function for maskable vector instruction with format:
450 * single-width result and single-width sources (SEW = SEW op SEW)
451 *
452 * Rules to be checked here:
453 *   1. Source (vs2, vs1) vector register number are multiples of LMUL.
454 *      (Section 3.4.2)
455 *   2. Destination vector register cannot overlap a source vector
456 *      register (vs2, vs1) group.
457 *      (Section 5.2)
458 *   3. The destination vector register group for a masked vector
459 *      instruction cannot overlap the source mask register (v0),
460 *      unless the destination vector register is being written
461 *      with a mask value (e.g., comparisons) or the scalar result
462 *      of a reduction. (Section 5.3)
463 */
464static bool vext_check_mss(DisasContext *s, int vd, int vs1, int vs2)
465{
466    bool ret = vext_check_ms(s, vd, vs2) &&
467               require_align(vs1, s->lmul);
468    if (vd != vs1) {
469        ret &= require_noover(vd, 0, vs1, s->lmul);
470    }
471    return ret;
472}
473
474/*
475 * Common check function for vector widening instructions
476 * of double-width result (2*SEW).
477 *
478 * Rules to be checked here:
479 *   1. The largest vector register group used by an instruction
480 *      can not be greater than 8 vector registers (Section 5.2):
481 *      => LMUL < 8.
482 *      => SEW < 64.
483 *   2. Double-width SEW cannot greater than ELEN.
484 *   3. Destination vector register number is multiples of 2 * LMUL.
485 *      (Section 3.4.2)
486 *   4. Destination vector register group for a masked vector
487 *      instruction cannot overlap the source mask register (v0).
488 *      (Section 5.3)
489 */
490static bool vext_wide_check_common(DisasContext *s, int vd, int vm)
491{
492    return (s->lmul <= 2) &&
493           (s->sew < MO_64) &&
494           ((s->sew + 1) <= (s->cfg_ptr->elen >> 4)) &&
495           require_align(vd, s->lmul + 1) &&
496           require_vm(vm, vd);
497}
498
499/*
500 * Common check function for vector narrowing instructions
501 * of single-width result (SEW) and double-width source (2*SEW).
502 *
503 * Rules to be checked here:
504 *   1. The largest vector register group used by an instruction
505 *      can not be greater than 8 vector registers (Section 5.2):
506 *      => LMUL < 8.
507 *      => SEW < 64.
508 *   2. Double-width SEW cannot greater than ELEN.
509 *   3. Source vector register number is multiples of 2 * LMUL.
510 *      (Section 3.4.2)
511 *   4. Destination vector register number is multiples of LMUL.
512 *      (Section 3.4.2)
513 *   5. Destination vector register group for a masked vector
514 *      instruction cannot overlap the source mask register (v0).
515 *      (Section 5.3)
516 */
517static bool vext_narrow_check_common(DisasContext *s, int vd, int vs2,
518                                     int vm)
519{
520    return (s->lmul <= 2) &&
521           (s->sew < MO_64) &&
522           ((s->sew + 1) <= (s->cfg_ptr->elen >> 4)) &&
523           require_align(vs2, s->lmul + 1) &&
524           require_align(vd, s->lmul) &&
525           require_vm(vm, vd);
526}
527
528static bool vext_check_ds(DisasContext *s, int vd, int vs, int vm)
529{
530    return vext_wide_check_common(s, vd, vm) &&
531           vext_check_input_eew(s, vs, s->sew, -1, 0, vm) &&
532           require_align(vs, s->lmul) &&
533           require_noover(vd, s->lmul + 1, vs, s->lmul);
534}
535
536static bool vext_check_dd(DisasContext *s, int vd, int vs, int vm)
537{
538    return vext_wide_check_common(s, vd, vm) &&
539           vext_check_input_eew(s, vs, s->sew + 1, -1, 0, vm) &&
540           require_align(vs, s->lmul + 1);
541}
542
543/*
544 * Check function for vector instruction with format:
545 * double-width result and single-width sources (2*SEW = SEW op SEW)
546 *
547 * Rules to be checked here:
548 *   1. All rules in defined in widen common rules are applied.
549 *   2. Source (vs2, vs1) vector register number are multiples of LMUL.
550 *      (Section 3.4.2)
551 *   3. Destination vector register cannot overlap a source vector
552 *      register (vs2, vs1) group.
553 *      (Section 5.2)
554 */
555static bool vext_check_dss(DisasContext *s, int vd, int vs1, int vs2, int vm)
556{
557    return vext_check_ds(s, vd, vs2, vm) &&
558           vext_check_input_eew(s, vs1, s->sew, vs2, s->sew, vm) &&
559           require_align(vs1, s->lmul) &&
560           require_noover(vd, s->lmul + 1, vs1, s->lmul);
561}
562
563/*
564 * Check function for vector instruction with format:
565 * double-width result and double-width source1 and single-width
566 * source2 (2*SEW = 2*SEW op SEW)
567 *
568 * Rules to be checked here:
569 *   1. All rules in defined in widen common rules are applied.
570 *   2. Source 1 (vs2) vector register number is multiples of 2 * LMUL.
571 *      (Section 3.4.2)
572 *   3. Source 2 (vs1) vector register number is multiples of LMUL.
573 *      (Section 3.4.2)
574 *   4. Destination vector register cannot overlap a source vector
575 *      register (vs1) group.
576 *      (Section 5.2)
577 */
578static bool vext_check_dds(DisasContext *s, int vd, int vs1, int vs2, int vm)
579{
580    return vext_check_ds(s, vd, vs1, vm) &&
581           vext_check_input_eew(s, vs1, s->sew, vs2, s->sew + 1, vm) &&
582           require_align(vs2, s->lmul + 1);
583}
584
585static bool vext_check_sd(DisasContext *s, int vd, int vs, int vm)
586{
587    bool ret = vext_narrow_check_common(s, vd, vs, vm) &&
588               vext_check_input_eew(s, vs, s->sew + 1, -1, 0, vm);
589    if (vd != vs) {
590        ret &= require_noover(vd, s->lmul, vs, s->lmul + 1);
591    }
592    return ret;
593}
594
595/*
596 * Check function for vector instruction with format:
597 * single-width result and double-width source 1 and single-width
598 * source 2 (SEW = 2*SEW op SEW)
599 *
600 * Rules to be checked here:
601 *   1. All rules in defined in narrow common rules are applied.
602 *   2. Destination vector register cannot overlap a source vector
603 *      register (vs2) group.
604 *      (Section 5.2)
605 *   3. Source 2 (vs1) vector register number is multiples of LMUL.
606 *      (Section 3.4.2)
607 */
608static bool vext_check_sds(DisasContext *s, int vd, int vs1, int vs2, int vm)
609{
610    return vext_check_sd(s, vd, vs2, vm) &&
611           vext_check_input_eew(s, vs1, s->sew, vs2, s->sew + 1, vm) &&
612           require_align(vs1, s->lmul);
613}
614
615/*
616 * Check function for vector reduction instructions.
617 *
618 * Rules to be checked here:
619 *   1. Source 1 (vs2) vector register number is multiples of LMUL.
620 *      (Section 3.4.2)
621 */
622static bool vext_check_reduction(DisasContext *s, int vs2)
623{
624    return require_align(vs2, s->lmul) && s->vstart_eq_zero;
625}
626
627/*
628 * Check function for vector slide instructions.
629 *
630 * Rules to be checked here:
631 *   1. Source 1 (vs2) vector register number is multiples of LMUL.
632 *      (Section 3.4.2)
633 *   2. Destination vector register number is multiples of LMUL.
634 *      (Section 3.4.2)
635 *   3. Destination vector register group for a masked vector
636 *      instruction cannot overlap the source mask register (v0).
637 *      (Section 5.3)
638 *   4. The destination vector register group for vslideup, vslide1up,
639 *      vfslide1up, cannot overlap the source vector register (vs2) group.
640 *      (Section 5.2, 16.3.1, 16.3.3)
641 */
642static bool vext_check_slide(DisasContext *s, int vd, int vs2,
643                             int vm, bool is_over)
644{
645    bool ret = require_align(vs2, s->lmul) &&
646               require_align(vd, s->lmul) &&
647               require_vm(vm, vd) &&
648               vext_check_input_eew(s, -1, 0, vs2, s->sew, vm);
649
650    if (is_over) {
651        ret &= (vd != vs2);
652    }
653    return ret;
654}
655
656/*
657 * In cpu_get_tb_cpu_state(), set VILL if RVV was not present.
658 * So RVV is also be checked in this function.
659 */
660static bool vext_check_isa_ill(DisasContext *s)
661{
662    return !s->vill;
663}
664
665/* common translation macro */
666#define GEN_VEXT_TRANS(NAME, EEW, ARGTYPE, OP, CHECK)        \
667static bool trans_##NAME(DisasContext *s, arg_##ARGTYPE * a) \
668{                                                            \
669    if (CHECK(s, a, EEW)) {                                  \
670        return OP(s, a, EEW);                                \
671    }                                                        \
672    return false;                                            \
673}
674
675static uint8_t vext_get_emul(DisasContext *s, uint8_t eew)
676{
677    int8_t emul = eew - s->sew + s->lmul;
678    return emul < 0 ? 0 : emul;
679}
680
681/*
682 *** unit stride load and store
683 */
684typedef void gen_helper_ldst_us(TCGv_ptr, TCGv_ptr, TCGv,
685                                TCGv_env, TCGv_i32);
686
687static bool ldst_us_trans(uint32_t vd, uint32_t rs1, uint32_t data,
688                          gen_helper_ldst_us *fn, DisasContext *s,
689                          bool is_store)
690{
691    TCGv_ptr dest, mask;
692    TCGv base;
693    TCGv_i32 desc;
694
695    dest = tcg_temp_new_ptr();
696    mask = tcg_temp_new_ptr();
697    base = get_gpr(s, rs1, EXT_NONE);
698
699    /*
700     * As simd_desc supports at most 2048 bytes, and in this implementation,
701     * the max vector group length is 4096 bytes. So split it into two parts.
702     *
703     * The first part is vlen in bytes (vlenb), encoded in maxsz of simd_desc.
704     * The second part is lmul, encoded in data of simd_desc.
705     */
706    desc = tcg_constant_i32(simd_desc(s->cfg_ptr->vlenb,
707                                      s->cfg_ptr->vlenb, data));
708
709    tcg_gen_addi_ptr(dest, tcg_env, vreg_ofs(s, vd));
710    tcg_gen_addi_ptr(mask, tcg_env, vreg_ofs(s, 0));
711
712    /*
713     * According to the specification
714     *
715     *   Additionally, if the Ztso extension is implemented, then vector memory
716     *   instructions in the V extension and Zve family of extensions follow
717     *   RVTSO at the instruction level.  The Ztso extension does not
718     *   strengthen the ordering of intra-instruction element accesses.
719     *
720     * as a result neither ordered nor unordered accesses from the V
721     * instructions need ordering within the loop but we do still need barriers
722     * around the loop.
723     */
724    if (is_store && s->ztso) {
725        tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
726    }
727
728    mark_vs_dirty(s);
729
730    fn(dest, mask, base, tcg_env, desc);
731
732    if (!is_store && s->ztso) {
733        tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
734    }
735
736    finalize_rvv_inst(s);
737    return true;
738}
739
740static bool ld_us_op(DisasContext *s, arg_r2nfvm *a, uint8_t eew)
741{
742    uint32_t data = 0;
743    gen_helper_ldst_us *fn;
744    static gen_helper_ldst_us * const fns[2][4] = {
745        /* masked unit stride load */
746        { gen_helper_vle8_v_mask, gen_helper_vle16_v_mask,
747          gen_helper_vle32_v_mask, gen_helper_vle64_v_mask },
748        /* unmasked unit stride load */
749        { gen_helper_vle8_v, gen_helper_vle16_v,
750          gen_helper_vle32_v, gen_helper_vle64_v }
751    };
752
753    fn =  fns[a->vm][eew];
754    if (fn == NULL) {
755        return false;
756    }
757
758    /*
759     * Vector load/store instructions have the EEW encoded
760     * directly in the instructions. The maximum vector size is
761     * calculated with EMUL rather than LMUL.
762     */
763    uint8_t emul = vext_get_emul(s, eew);
764    data = FIELD_DP32(data, VDATA, VM, a->vm);
765    data = FIELD_DP32(data, VDATA, LMUL, emul);
766    data = FIELD_DP32(data, VDATA, NF, a->nf);
767    data = FIELD_DP32(data, VDATA, VTA, s->vta);
768    data = FIELD_DP32(data, VDATA, VMA, s->vma);
769    return ldst_us_trans(a->rd, a->rs1, data, fn, s, false);
770}
771
772static bool ld_us_check(DisasContext *s, arg_r2nfvm* a, uint8_t eew)
773{
774    return require_rvv(s) &&
775           vext_check_isa_ill(s) &&
776           vext_check_load(s, a->rd, a->nf, a->vm, eew);
777}
778
779GEN_VEXT_TRANS(vle8_v,  MO_8,  r2nfvm, ld_us_op, ld_us_check)
780GEN_VEXT_TRANS(vle16_v, MO_16, r2nfvm, ld_us_op, ld_us_check)
781GEN_VEXT_TRANS(vle32_v, MO_32, r2nfvm, ld_us_op, ld_us_check)
782GEN_VEXT_TRANS(vle64_v, MO_64, r2nfvm, ld_us_op, ld_us_check)
783
784static bool st_us_op(DisasContext *s, arg_r2nfvm *a, uint8_t eew)
785{
786    uint32_t data = 0;
787    gen_helper_ldst_us *fn;
788    static gen_helper_ldst_us * const fns[2][4] = {
789        /* masked unit stride store */
790        { gen_helper_vse8_v_mask, gen_helper_vse16_v_mask,
791          gen_helper_vse32_v_mask, gen_helper_vse64_v_mask },
792        /* unmasked unit stride store */
793        { gen_helper_vse8_v, gen_helper_vse16_v,
794          gen_helper_vse32_v, gen_helper_vse64_v }
795    };
796
797    fn =  fns[a->vm][eew];
798    if (fn == NULL) {
799        return false;
800    }
801
802    uint8_t emul = vext_get_emul(s, eew);
803    data = FIELD_DP32(data, VDATA, VM, a->vm);
804    data = FIELD_DP32(data, VDATA, LMUL, emul);
805    data = FIELD_DP32(data, VDATA, NF, a->nf);
806    return ldst_us_trans(a->rd, a->rs1, data, fn, s, true);
807}
808
809static bool st_us_check(DisasContext *s, arg_r2nfvm* a, uint8_t eew)
810{
811    return require_rvv(s) &&
812           vext_check_isa_ill(s) &&
813           vext_check_store(s, a->rd, a->nf, eew);
814}
815
816GEN_VEXT_TRANS(vse8_v,  MO_8,  r2nfvm, st_us_op, st_us_check)
817GEN_VEXT_TRANS(vse16_v, MO_16, r2nfvm, st_us_op, st_us_check)
818GEN_VEXT_TRANS(vse32_v, MO_32, r2nfvm, st_us_op, st_us_check)
819GEN_VEXT_TRANS(vse64_v, MO_64, r2nfvm, st_us_op, st_us_check)
820
821/*
822 *** unit stride mask load and store
823 */
824static bool ld_us_mask_op(DisasContext *s, arg_vlm_v *a, uint8_t eew)
825{
826    uint32_t data = 0;
827    gen_helper_ldst_us *fn = gen_helper_vlm_v;
828
829    /* EMUL = 1, NFIELDS = 1 */
830    data = FIELD_DP32(data, VDATA, LMUL, 0);
831    data = FIELD_DP32(data, VDATA, NF, 1);
832    /* Mask destination register are always tail-agnostic */
833    data = FIELD_DP32(data, VDATA, VTA, s->cfg_vta_all_1s);
834    data = FIELD_DP32(data, VDATA, VMA, s->vma);
835    data = FIELD_DP32(data, VDATA, VM, 1);
836    return ldst_us_trans(a->rd, a->rs1, data, fn, s, false);
837}
838
839static bool ld_us_mask_check(DisasContext *s, arg_vlm_v *a, uint8_t eew)
840{
841    /* EMUL = 1, NFIELDS = 1 */
842    return require_rvv(s) && vext_check_isa_ill(s);
843}
844
845static bool st_us_mask_op(DisasContext *s, arg_vsm_v *a, uint8_t eew)
846{
847    uint32_t data = 0;
848    gen_helper_ldst_us *fn = gen_helper_vsm_v;
849
850    /* EMUL = 1, NFIELDS = 1 */
851    data = FIELD_DP32(data, VDATA, LMUL, 0);
852    data = FIELD_DP32(data, VDATA, NF, 1);
853    data = FIELD_DP32(data, VDATA, VM, 1);
854    return ldst_us_trans(a->rd, a->rs1, data, fn, s, true);
855}
856
857static bool st_us_mask_check(DisasContext *s, arg_vsm_v *a, uint8_t eew)
858{
859    /* EMUL = 1, NFIELDS = 1 */
860    return require_rvv(s) && vext_check_isa_ill(s);
861}
862
863GEN_VEXT_TRANS(vlm_v, MO_8, vlm_v, ld_us_mask_op, ld_us_mask_check)
864GEN_VEXT_TRANS(vsm_v, MO_8, vsm_v, st_us_mask_op, st_us_mask_check)
865
866/*
867 *** stride load and store
868 */
869typedef void gen_helper_ldst_stride(TCGv_ptr, TCGv_ptr, TCGv,
870                                    TCGv, TCGv_env, TCGv_i32);
871
872static bool ldst_stride_trans(uint32_t vd, uint32_t rs1, uint32_t rs2,
873                              uint32_t data, gen_helper_ldst_stride *fn,
874                              DisasContext *s)
875{
876    TCGv_ptr dest, mask;
877    TCGv base, stride;
878    TCGv_i32 desc;
879
880    dest = tcg_temp_new_ptr();
881    mask = tcg_temp_new_ptr();
882    base = get_gpr(s, rs1, EXT_NONE);
883    stride = get_gpr(s, rs2, EXT_NONE);
884    desc = tcg_constant_i32(simd_desc(s->cfg_ptr->vlenb,
885                                      s->cfg_ptr->vlenb, data));
886
887    tcg_gen_addi_ptr(dest, tcg_env, vreg_ofs(s, vd));
888    tcg_gen_addi_ptr(mask, tcg_env, vreg_ofs(s, 0));
889
890    mark_vs_dirty(s);
891
892    fn(dest, mask, base, stride, tcg_env, desc);
893
894    finalize_rvv_inst(s);
895    return true;
896}
897
898static bool ld_stride_op(DisasContext *s, arg_rnfvm *a, uint8_t eew)
899{
900    uint32_t data = 0;
901    gen_helper_ldst_stride *fn;
902    static gen_helper_ldst_stride * const fns[4] = {
903        gen_helper_vlse8_v, gen_helper_vlse16_v,
904        gen_helper_vlse32_v, gen_helper_vlse64_v
905    };
906
907    fn = fns[eew];
908    if (fn == NULL) {
909        return false;
910    }
911
912    uint8_t emul = vext_get_emul(s, eew);
913    data = FIELD_DP32(data, VDATA, VM, a->vm);
914    data = FIELD_DP32(data, VDATA, LMUL, emul);
915    data = FIELD_DP32(data, VDATA, NF, a->nf);
916    data = FIELD_DP32(data, VDATA, VTA, s->vta);
917    data = FIELD_DP32(data, VDATA, VMA, s->vma);
918    return ldst_stride_trans(a->rd, a->rs1, a->rs2, data, fn, s);
919}
920
921static bool ld_stride_check(DisasContext *s, arg_rnfvm* a, uint8_t eew)
922{
923    return require_rvv(s) &&
924           vext_check_isa_ill(s) &&
925           vext_check_load(s, a->rd, a->nf, a->vm, eew);
926}
927
928GEN_VEXT_TRANS(vlse8_v,  MO_8,  rnfvm, ld_stride_op, ld_stride_check)
929GEN_VEXT_TRANS(vlse16_v, MO_16, rnfvm, ld_stride_op, ld_stride_check)
930GEN_VEXT_TRANS(vlse32_v, MO_32, rnfvm, ld_stride_op, ld_stride_check)
931GEN_VEXT_TRANS(vlse64_v, MO_64, rnfvm, ld_stride_op, ld_stride_check)
932
933static bool st_stride_op(DisasContext *s, arg_rnfvm *a, uint8_t eew)
934{
935    uint32_t data = 0;
936    gen_helper_ldst_stride *fn;
937    static gen_helper_ldst_stride * const fns[4] = {
938        /* masked stride store */
939        gen_helper_vsse8_v,  gen_helper_vsse16_v,
940        gen_helper_vsse32_v,  gen_helper_vsse64_v
941    };
942
943    uint8_t emul = vext_get_emul(s, eew);
944    data = FIELD_DP32(data, VDATA, VM, a->vm);
945    data = FIELD_DP32(data, VDATA, LMUL, emul);
946    data = FIELD_DP32(data, VDATA, NF, a->nf);
947    fn = fns[eew];
948    if (fn == NULL) {
949        return false;
950    }
951
952    return ldst_stride_trans(a->rd, a->rs1, a->rs2, data, fn, s);
953}
954
955static bool st_stride_check(DisasContext *s, arg_rnfvm* a, uint8_t eew)
956{
957    return require_rvv(s) &&
958           vext_check_isa_ill(s) &&
959           vext_check_store(s, a->rd, a->nf, eew);
960}
961
962GEN_VEXT_TRANS(vsse8_v,  MO_8,  rnfvm, st_stride_op, st_stride_check)
963GEN_VEXT_TRANS(vsse16_v, MO_16, rnfvm, st_stride_op, st_stride_check)
964GEN_VEXT_TRANS(vsse32_v, MO_32, rnfvm, st_stride_op, st_stride_check)
965GEN_VEXT_TRANS(vsse64_v, MO_64, rnfvm, st_stride_op, st_stride_check)
966
967/*
968 *** index load and store
969 */
970typedef void gen_helper_ldst_index(TCGv_ptr, TCGv_ptr, TCGv,
971                                   TCGv_ptr, TCGv_env, TCGv_i32);
972
973static bool ldst_index_trans(uint32_t vd, uint32_t rs1, uint32_t vs2,
974                             uint32_t data, gen_helper_ldst_index *fn,
975                             DisasContext *s)
976{
977    TCGv_ptr dest, mask, index;
978    TCGv base;
979    TCGv_i32 desc;
980
981    dest = tcg_temp_new_ptr();
982    mask = tcg_temp_new_ptr();
983    index = tcg_temp_new_ptr();
984    base = get_gpr(s, rs1, EXT_NONE);
985    desc = tcg_constant_i32(simd_desc(s->cfg_ptr->vlenb,
986                                      s->cfg_ptr->vlenb, data));
987
988    tcg_gen_addi_ptr(dest, tcg_env, vreg_ofs(s, vd));
989    tcg_gen_addi_ptr(index, tcg_env, vreg_ofs(s, vs2));
990    tcg_gen_addi_ptr(mask, tcg_env, vreg_ofs(s, 0));
991
992    mark_vs_dirty(s);
993
994    fn(dest, mask, base, index, tcg_env, desc);
995
996    finalize_rvv_inst(s);
997    return true;
998}
999
1000static bool ld_index_op(DisasContext *s, arg_rnfvm *a, uint8_t eew)
1001{
1002    uint32_t data = 0;
1003    gen_helper_ldst_index *fn;
1004    static gen_helper_ldst_index * const fns[4][4] = {
1005        /*
1006         * offset vector register group EEW = 8,
1007         * data vector register group EEW = SEW
1008         */
1009        { gen_helper_vlxei8_8_v,  gen_helper_vlxei8_16_v,
1010          gen_helper_vlxei8_32_v, gen_helper_vlxei8_64_v },
1011        /*
1012         * offset vector register group EEW = 16,
1013         * data vector register group EEW = SEW
1014         */
1015        { gen_helper_vlxei16_8_v, gen_helper_vlxei16_16_v,
1016          gen_helper_vlxei16_32_v, gen_helper_vlxei16_64_v },
1017        /*
1018         * offset vector register group EEW = 32,
1019         * data vector register group EEW = SEW
1020         */
1021        { gen_helper_vlxei32_8_v, gen_helper_vlxei32_16_v,
1022          gen_helper_vlxei32_32_v, gen_helper_vlxei32_64_v },
1023        /*
1024         * offset vector register group EEW = 64,
1025         * data vector register group EEW = SEW
1026         */
1027        { gen_helper_vlxei64_8_v, gen_helper_vlxei64_16_v,
1028          gen_helper_vlxei64_32_v, gen_helper_vlxei64_64_v }
1029    };
1030
1031    fn = fns[eew][s->sew];
1032
1033    uint8_t emul = vext_get_emul(s, s->sew);
1034    data = FIELD_DP32(data, VDATA, VM, a->vm);
1035    data = FIELD_DP32(data, VDATA, LMUL, emul);
1036    data = FIELD_DP32(data, VDATA, NF, a->nf);
1037    data = FIELD_DP32(data, VDATA, VTA, s->vta);
1038    data = FIELD_DP32(data, VDATA, VMA, s->vma);
1039    return ldst_index_trans(a->rd, a->rs1, a->rs2, data, fn, s);
1040}
1041
1042static bool ld_index_check(DisasContext *s, arg_rnfvm* a, uint8_t eew)
1043{
1044    return require_rvv(s) &&
1045           vext_check_isa_ill(s) &&
1046           vext_check_ld_index(s, a->rd, a->rs2, a->nf, a->vm, eew) &&
1047           vext_check_input_eew(s, -1, 0, a->rs2, eew, a->vm);
1048}
1049
1050GEN_VEXT_TRANS(vlxei8_v,  MO_8,  rnfvm, ld_index_op, ld_index_check)
1051GEN_VEXT_TRANS(vlxei16_v, MO_16, rnfvm, ld_index_op, ld_index_check)
1052GEN_VEXT_TRANS(vlxei32_v, MO_32, rnfvm, ld_index_op, ld_index_check)
1053GEN_VEXT_TRANS(vlxei64_v, MO_64, rnfvm, ld_index_op, ld_index_check)
1054
1055static bool st_index_op(DisasContext *s, arg_rnfvm *a, uint8_t eew)
1056{
1057    uint32_t data = 0;
1058    gen_helper_ldst_index *fn;
1059    static gen_helper_ldst_index * const fns[4][4] = {
1060        /*
1061         * offset vector register group EEW = 8,
1062         * data vector register group EEW = SEW
1063         */
1064        { gen_helper_vsxei8_8_v,  gen_helper_vsxei8_16_v,
1065          gen_helper_vsxei8_32_v, gen_helper_vsxei8_64_v },
1066        /*
1067         * offset vector register group EEW = 16,
1068         * data vector register group EEW = SEW
1069         */
1070        { gen_helper_vsxei16_8_v, gen_helper_vsxei16_16_v,
1071          gen_helper_vsxei16_32_v, gen_helper_vsxei16_64_v },
1072        /*
1073         * offset vector register group EEW = 32,
1074         * data vector register group EEW = SEW
1075         */
1076        { gen_helper_vsxei32_8_v, gen_helper_vsxei32_16_v,
1077          gen_helper_vsxei32_32_v, gen_helper_vsxei32_64_v },
1078        /*
1079         * offset vector register group EEW = 64,
1080         * data vector register group EEW = SEW
1081         */
1082        { gen_helper_vsxei64_8_v, gen_helper_vsxei64_16_v,
1083          gen_helper_vsxei64_32_v, gen_helper_vsxei64_64_v }
1084    };
1085
1086    fn = fns[eew][s->sew];
1087
1088    uint8_t emul = vext_get_emul(s, s->sew);
1089    data = FIELD_DP32(data, VDATA, VM, a->vm);
1090    data = FIELD_DP32(data, VDATA, LMUL, emul);
1091    data = FIELD_DP32(data, VDATA, NF, a->nf);
1092    return ldst_index_trans(a->rd, a->rs1, a->rs2, data, fn, s);
1093}
1094
1095static bool st_index_check(DisasContext *s, arg_rnfvm* a, uint8_t eew)
1096{
1097    return require_rvv(s) &&
1098           vext_check_isa_ill(s) &&
1099           vext_check_st_index(s, a->rd, a->rs2, a->nf, eew) &&
1100           vext_check_input_eew(s, a->rd, s->sew, a->rs2, eew, a->vm);
1101}
1102
1103GEN_VEXT_TRANS(vsxei8_v,  MO_8,  rnfvm, st_index_op, st_index_check)
1104GEN_VEXT_TRANS(vsxei16_v, MO_16, rnfvm, st_index_op, st_index_check)
1105GEN_VEXT_TRANS(vsxei32_v, MO_32, rnfvm, st_index_op, st_index_check)
1106GEN_VEXT_TRANS(vsxei64_v, MO_64, rnfvm, st_index_op, st_index_check)
1107
1108/*
1109 *** unit stride fault-only-first load
1110 */
1111static bool ldff_trans(uint32_t vd, uint32_t rs1, uint32_t data,
1112                       gen_helper_ldst_us *fn, DisasContext *s)
1113{
1114    TCGv_ptr dest, mask;
1115    TCGv base;
1116    TCGv_i32 desc;
1117
1118    dest = tcg_temp_new_ptr();
1119    mask = tcg_temp_new_ptr();
1120    base = get_gpr(s, rs1, EXT_NONE);
1121    desc = tcg_constant_i32(simd_desc(s->cfg_ptr->vlenb,
1122                                      s->cfg_ptr->vlenb, data));
1123
1124    tcg_gen_addi_ptr(dest, tcg_env, vreg_ofs(s, vd));
1125    tcg_gen_addi_ptr(mask, tcg_env, vreg_ofs(s, 0));
1126
1127    fn(dest, mask, base, tcg_env, desc);
1128
1129    finalize_rvv_inst(s);
1130    return true;
1131}
1132
1133static bool ldff_op(DisasContext *s, arg_r2nfvm *a, uint8_t eew)
1134{
1135    uint32_t data = 0;
1136    gen_helper_ldst_us *fn;
1137    static gen_helper_ldst_us * const fns[4] = {
1138        gen_helper_vle8ff_v, gen_helper_vle16ff_v,
1139        gen_helper_vle32ff_v, gen_helper_vle64ff_v
1140    };
1141
1142    fn = fns[eew];
1143    if (fn == NULL) {
1144        return false;
1145    }
1146
1147    uint8_t emul = vext_get_emul(s, eew);
1148    data = FIELD_DP32(data, VDATA, VM, a->vm);
1149    data = FIELD_DP32(data, VDATA, LMUL, emul);
1150    data = FIELD_DP32(data, VDATA, NF, a->nf);
1151    data = FIELD_DP32(data, VDATA, VTA, s->vta);
1152    data = FIELD_DP32(data, VDATA, VMA, s->vma);
1153    return ldff_trans(a->rd, a->rs1, data, fn, s);
1154}
1155
1156GEN_VEXT_TRANS(vle8ff_v,  MO_8,  r2nfvm, ldff_op, ld_us_check)
1157GEN_VEXT_TRANS(vle16ff_v, MO_16, r2nfvm, ldff_op, ld_us_check)
1158GEN_VEXT_TRANS(vle32ff_v, MO_32, r2nfvm, ldff_op, ld_us_check)
1159GEN_VEXT_TRANS(vle64ff_v, MO_64, r2nfvm, ldff_op, ld_us_check)
1160
1161/*
1162 * load and store whole register instructions
1163 */
1164typedef void gen_helper_ldst_whole(TCGv_ptr, TCGv, TCGv_env, TCGv_i32);
1165
1166static bool ldst_whole_trans(uint32_t vd, uint32_t rs1, uint32_t nf,
1167                             gen_helper_ldst_whole *fn,
1168                             DisasContext *s)
1169{
1170    TCGv_ptr dest;
1171    TCGv base;
1172    TCGv_i32 desc;
1173
1174    uint32_t data = FIELD_DP32(0, VDATA, NF, nf);
1175    data = FIELD_DP32(data, VDATA, VM, 1);
1176    dest = tcg_temp_new_ptr();
1177    desc = tcg_constant_i32(simd_desc(s->cfg_ptr->vlenb,
1178                                      s->cfg_ptr->vlenb, data));
1179
1180    base = get_gpr(s, rs1, EXT_NONE);
1181    tcg_gen_addi_ptr(dest, tcg_env, vreg_ofs(s, vd));
1182
1183    mark_vs_dirty(s);
1184
1185    fn(dest, base, tcg_env, desc);
1186
1187    finalize_rvv_inst(s);
1188    return true;
1189}
1190
1191/*
1192 * load and store whole register instructions ignore vtype and vl setting.
1193 * Thus, we don't need to check vill bit. (Section 7.9)
1194 */
1195#define GEN_LDST_WHOLE_TRANS(NAME, ARG_NF)                                \
1196static bool trans_##NAME(DisasContext *s, arg_##NAME * a)                 \
1197{                                                                         \
1198    if (require_rvv(s) &&                                                 \
1199        QEMU_IS_ALIGNED(a->rd, ARG_NF)) {                                 \
1200        return ldst_whole_trans(a->rd, a->rs1, ARG_NF,                    \
1201                                gen_helper_##NAME, s);                    \
1202    }                                                                     \
1203    return false;                                                         \
1204}
1205
1206GEN_LDST_WHOLE_TRANS(vl1re8_v,  1)
1207GEN_LDST_WHOLE_TRANS(vl1re16_v, 1)
1208GEN_LDST_WHOLE_TRANS(vl1re32_v, 1)
1209GEN_LDST_WHOLE_TRANS(vl1re64_v, 1)
1210GEN_LDST_WHOLE_TRANS(vl2re8_v,  2)
1211GEN_LDST_WHOLE_TRANS(vl2re16_v, 2)
1212GEN_LDST_WHOLE_TRANS(vl2re32_v, 2)
1213GEN_LDST_WHOLE_TRANS(vl2re64_v, 2)
1214GEN_LDST_WHOLE_TRANS(vl4re8_v,  4)
1215GEN_LDST_WHOLE_TRANS(vl4re16_v, 4)
1216GEN_LDST_WHOLE_TRANS(vl4re32_v, 4)
1217GEN_LDST_WHOLE_TRANS(vl4re64_v, 4)
1218GEN_LDST_WHOLE_TRANS(vl8re8_v,  8)
1219GEN_LDST_WHOLE_TRANS(vl8re16_v, 8)
1220GEN_LDST_WHOLE_TRANS(vl8re32_v, 8)
1221GEN_LDST_WHOLE_TRANS(vl8re64_v, 8)
1222
1223/*
1224 * The vector whole register store instructions are encoded similar to
1225 * unmasked unit-stride store of elements with EEW=8.
1226 */
1227GEN_LDST_WHOLE_TRANS(vs1r_v, 1)
1228GEN_LDST_WHOLE_TRANS(vs2r_v, 2)
1229GEN_LDST_WHOLE_TRANS(vs4r_v, 4)
1230GEN_LDST_WHOLE_TRANS(vs8r_v, 8)
1231
1232/*
1233 *** Vector Integer Arithmetic Instructions
1234 */
1235
1236/*
1237 * MAXSZ returns the maximum vector size can be operated in bytes,
1238 * which is used in GVEC IR when vl_eq_vlmax flag is set to true
1239 * to accelerate vector operation.
1240 */
1241static inline uint32_t MAXSZ(DisasContext *s)
1242{
1243    int max_sz = s->cfg_ptr->vlenb * 8;
1244    return max_sz >> (3 - s->lmul);
1245}
1246
1247static bool opivv_check(DisasContext *s, arg_rmrr *a)
1248{
1249    return require_rvv(s) &&
1250           vext_check_isa_ill(s) &&
1251           vext_check_sss(s, a->rd, a->rs1, a->rs2, a->vm);
1252}
1253
1254typedef void GVecGen3Fn(unsigned, uint32_t, uint32_t,
1255                        uint32_t, uint32_t, uint32_t);
1256
1257static inline bool
1258do_opivv_gvec(DisasContext *s, arg_rmrr *a, GVecGen3Fn *gvec_fn,
1259              gen_helper_gvec_4_ptr *fn)
1260{
1261    if (a->vm && s->vl_eq_vlmax && !(s->vta && s->lmul < 0)) {
1262        gvec_fn(s->sew, vreg_ofs(s, a->rd),
1263                vreg_ofs(s, a->rs2), vreg_ofs(s, a->rs1),
1264                MAXSZ(s), MAXSZ(s));
1265    } else {
1266        uint32_t data = 0;
1267
1268        data = FIELD_DP32(data, VDATA, VM, a->vm);
1269        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);
1270        data = FIELD_DP32(data, VDATA, VTA, s->vta);
1271        data = FIELD_DP32(data, VDATA, VMA, s->vma);
1272        tcg_gen_gvec_4_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),
1273                           vreg_ofs(s, a->rs1), vreg_ofs(s, a->rs2),
1274                           tcg_env, s->cfg_ptr->vlenb,
1275                           s->cfg_ptr->vlenb, data, fn);
1276    }
1277    finalize_rvv_inst(s);
1278    return true;
1279}
1280
1281/* OPIVV with GVEC IR */
1282#define GEN_OPIVV_GVEC_TRANS(NAME, SUF) \
1283static bool trans_##NAME(DisasContext *s, arg_rmrr *a)             \
1284{                                                                  \
1285    static gen_helper_gvec_4_ptr * const fns[4] = {                \
1286        gen_helper_##NAME##_b, gen_helper_##NAME##_h,              \
1287        gen_helper_##NAME##_w, gen_helper_##NAME##_d,              \
1288    };                                                             \
1289    if (!opivv_check(s, a)) {                                      \
1290        return false;                                              \
1291    }                                                              \
1292    return do_opivv_gvec(s, a, tcg_gen_gvec_##SUF, fns[s->sew]);   \
1293}
1294
1295GEN_OPIVV_GVEC_TRANS(vadd_vv, add)
1296GEN_OPIVV_GVEC_TRANS(vsub_vv, sub)
1297
1298typedef void gen_helper_opivx(TCGv_ptr, TCGv_ptr, TCGv, TCGv_ptr,
1299                              TCGv_env, TCGv_i32);
1300
1301static bool opivx_trans(uint32_t vd, uint32_t rs1, uint32_t vs2, uint32_t vm,
1302                        gen_helper_opivx *fn, DisasContext *s)
1303{
1304    TCGv_ptr dest, src2, mask;
1305    TCGv src1;
1306    TCGv_i32 desc;
1307    uint32_t data = 0;
1308
1309    dest = tcg_temp_new_ptr();
1310    mask = tcg_temp_new_ptr();
1311    src2 = tcg_temp_new_ptr();
1312    src1 = get_gpr(s, rs1, EXT_SIGN);
1313
1314    data = FIELD_DP32(data, VDATA, VM, vm);
1315    data = FIELD_DP32(data, VDATA, LMUL, s->lmul);
1316    data = FIELD_DP32(data, VDATA, VTA, s->vta);
1317    data = FIELD_DP32(data, VDATA, VTA_ALL_1S, s->cfg_vta_all_1s);
1318    data = FIELD_DP32(data, VDATA, VMA, s->vma);
1319    desc = tcg_constant_i32(simd_desc(s->cfg_ptr->vlenb,
1320                                      s->cfg_ptr->vlenb, data));
1321
1322    tcg_gen_addi_ptr(dest, tcg_env, vreg_ofs(s, vd));
1323    tcg_gen_addi_ptr(src2, tcg_env, vreg_ofs(s, vs2));
1324    tcg_gen_addi_ptr(mask, tcg_env, vreg_ofs(s, 0));
1325
1326    fn(dest, mask, src1, src2, tcg_env, desc);
1327
1328    finalize_rvv_inst(s);
1329    return true;
1330}
1331
1332static bool opivx_check(DisasContext *s, arg_rmrr *a)
1333{
1334    return require_rvv(s) &&
1335           vext_check_isa_ill(s) &&
1336           vext_check_ss(s, a->rd, a->rs2, a->vm);
1337}
1338
1339typedef void GVecGen2sFn(unsigned, uint32_t, uint32_t, TCGv_i64,
1340                         uint32_t, uint32_t);
1341
1342static inline bool
1343do_opivx_gvec(DisasContext *s, arg_rmrr *a, GVecGen2sFn *gvec_fn,
1344              gen_helper_opivx *fn)
1345{
1346    if (a->vm && s->vl_eq_vlmax && !(s->vta && s->lmul < 0)) {
1347        TCGv_i64 src1 = tcg_temp_new_i64();
1348
1349        tcg_gen_ext_tl_i64(src1, get_gpr(s, a->rs1, EXT_SIGN));
1350        gvec_fn(s->sew, vreg_ofs(s, a->rd), vreg_ofs(s, a->rs2),
1351                src1, MAXSZ(s), MAXSZ(s));
1352
1353        finalize_rvv_inst(s);
1354        return true;
1355    }
1356    return opivx_trans(a->rd, a->rs1, a->rs2, a->vm, fn, s);
1357}
1358
1359/* OPIVX with GVEC IR */
1360#define GEN_OPIVX_GVEC_TRANS(NAME, SUF) \
1361static bool trans_##NAME(DisasContext *s, arg_rmrr *a)             \
1362{                                                                  \
1363    static gen_helper_opivx * const fns[4] = {                     \
1364        gen_helper_##NAME##_b, gen_helper_##NAME##_h,              \
1365        gen_helper_##NAME##_w, gen_helper_##NAME##_d,              \
1366    };                                                             \
1367    if (!opivx_check(s, a)) {                                      \
1368        return false;                                              \
1369    }                                                              \
1370    return do_opivx_gvec(s, a, tcg_gen_gvec_##SUF, fns[s->sew]);   \
1371}
1372
1373GEN_OPIVX_GVEC_TRANS(vadd_vx, adds)
1374GEN_OPIVX_GVEC_TRANS(vsub_vx, subs)
1375
1376static void gen_vec_rsub8_i64(TCGv_i64 d, TCGv_i64 a, TCGv_i64 b)
1377{
1378    tcg_gen_vec_sub8_i64(d, b, a);
1379}
1380
1381static void gen_vec_rsub16_i64(TCGv_i64 d, TCGv_i64 a, TCGv_i64 b)
1382{
1383    tcg_gen_vec_sub16_i64(d, b, a);
1384}
1385
1386static void gen_rsub_i32(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
1387{
1388    tcg_gen_sub_i32(ret, arg2, arg1);
1389}
1390
1391static void gen_rsub_i64(TCGv_i64 ret, TCGv_i64 arg1, TCGv_i64 arg2)
1392{
1393    tcg_gen_sub_i64(ret, arg2, arg1);
1394}
1395
1396static void gen_rsub_vec(unsigned vece, TCGv_vec r, TCGv_vec a, TCGv_vec b)
1397{
1398    tcg_gen_sub_vec(vece, r, b, a);
1399}
1400
1401static void tcg_gen_gvec_rsubs(unsigned vece, uint32_t dofs, uint32_t aofs,
1402                               TCGv_i64 c, uint32_t oprsz, uint32_t maxsz)
1403{
1404    static const TCGOpcode vecop_list[] = { INDEX_op_sub_vec, 0 };
1405    static const GVecGen2s rsub_op[4] = {
1406        { .fni8 = gen_vec_rsub8_i64,
1407          .fniv = gen_rsub_vec,
1408          .fno = gen_helper_vec_rsubs8,
1409          .opt_opc = vecop_list,
1410          .vece = MO_8 },
1411        { .fni8 = gen_vec_rsub16_i64,
1412          .fniv = gen_rsub_vec,
1413          .fno = gen_helper_vec_rsubs16,
1414          .opt_opc = vecop_list,
1415          .vece = MO_16 },
1416        { .fni4 = gen_rsub_i32,
1417          .fniv = gen_rsub_vec,
1418          .fno = gen_helper_vec_rsubs32,
1419          .opt_opc = vecop_list,
1420          .vece = MO_32 },
1421        { .fni8 = gen_rsub_i64,
1422          .fniv = gen_rsub_vec,
1423          .fno = gen_helper_vec_rsubs64,
1424          .opt_opc = vecop_list,
1425          .prefer_i64 = TCG_TARGET_REG_BITS == 64,
1426          .vece = MO_64 },
1427    };
1428
1429    tcg_debug_assert(vece <= MO_64);
1430    tcg_gen_gvec_2s(dofs, aofs, oprsz, maxsz, c, &rsub_op[vece]);
1431}
1432
1433GEN_OPIVX_GVEC_TRANS(vrsub_vx, rsubs)
1434
1435typedef enum {
1436    IMM_ZX,         /* Zero-extended */
1437    IMM_SX,         /* Sign-extended */
1438    IMM_TRUNC_SEW,  /* Truncate to log(SEW) bits */
1439    IMM_TRUNC_2SEW, /* Truncate to log(2*SEW) bits */
1440} imm_mode_t;
1441
1442static int64_t extract_imm(DisasContext *s, uint32_t imm, imm_mode_t imm_mode)
1443{
1444    switch (imm_mode) {
1445    case IMM_ZX:
1446        return extract64(imm, 0, 5);
1447    case IMM_SX:
1448        return sextract64(imm, 0, 5);
1449    case IMM_TRUNC_SEW:
1450        return extract64(imm, 0, s->sew + 3);
1451    case IMM_TRUNC_2SEW:
1452        return extract64(imm, 0, s->sew + 4);
1453    default:
1454        g_assert_not_reached();
1455    }
1456}
1457
1458static bool opivi_trans(uint32_t vd, uint32_t imm, uint32_t vs2, uint32_t vm,
1459                        gen_helper_opivx *fn, DisasContext *s,
1460                        imm_mode_t imm_mode)
1461{
1462    TCGv_ptr dest, src2, mask;
1463    TCGv src1;
1464    TCGv_i32 desc;
1465    uint32_t data = 0;
1466
1467    dest = tcg_temp_new_ptr();
1468    mask = tcg_temp_new_ptr();
1469    src2 = tcg_temp_new_ptr();
1470    src1 = tcg_constant_tl(extract_imm(s, imm, imm_mode));
1471
1472    data = FIELD_DP32(data, VDATA, VM, vm);
1473    data = FIELD_DP32(data, VDATA, LMUL, s->lmul);
1474    data = FIELD_DP32(data, VDATA, VTA, s->vta);
1475    data = FIELD_DP32(data, VDATA, VTA_ALL_1S, s->cfg_vta_all_1s);
1476    data = FIELD_DP32(data, VDATA, VMA, s->vma);
1477    desc = tcg_constant_i32(simd_desc(s->cfg_ptr->vlenb,
1478                                      s->cfg_ptr->vlenb, data));
1479
1480    tcg_gen_addi_ptr(dest, tcg_env, vreg_ofs(s, vd));
1481    tcg_gen_addi_ptr(src2, tcg_env, vreg_ofs(s, vs2));
1482    tcg_gen_addi_ptr(mask, tcg_env, vreg_ofs(s, 0));
1483
1484    fn(dest, mask, src1, src2, tcg_env, desc);
1485
1486    finalize_rvv_inst(s);
1487    return true;
1488}
1489
1490typedef void GVecGen2iFn(unsigned, uint32_t, uint32_t, int64_t,
1491                         uint32_t, uint32_t);
1492
1493static inline bool
1494do_opivi_gvec(DisasContext *s, arg_rmrr *a, GVecGen2iFn *gvec_fn,
1495              gen_helper_opivx *fn, imm_mode_t imm_mode)
1496{
1497    if (a->vm && s->vl_eq_vlmax && !(s->vta && s->lmul < 0)) {
1498        gvec_fn(s->sew, vreg_ofs(s, a->rd), vreg_ofs(s, a->rs2),
1499                extract_imm(s, a->rs1, imm_mode), MAXSZ(s), MAXSZ(s));
1500        finalize_rvv_inst(s);
1501        return true;
1502    }
1503    return opivi_trans(a->rd, a->rs1, a->rs2, a->vm, fn, s, imm_mode);
1504}
1505
1506/* OPIVI with GVEC IR */
1507#define GEN_OPIVI_GVEC_TRANS(NAME, IMM_MODE, OPIVX, SUF) \
1508static bool trans_##NAME(DisasContext *s, arg_rmrr *a)             \
1509{                                                                  \
1510    static gen_helper_opivx * const fns[4] = {                     \
1511        gen_helper_##OPIVX##_b, gen_helper_##OPIVX##_h,            \
1512        gen_helper_##OPIVX##_w, gen_helper_##OPIVX##_d,            \
1513    };                                                             \
1514    if (!opivx_check(s, a)) {                                      \
1515        return false;                                              \
1516    }                                                              \
1517    return do_opivi_gvec(s, a, tcg_gen_gvec_##SUF,                 \
1518                         fns[s->sew], IMM_MODE);                   \
1519}
1520
1521GEN_OPIVI_GVEC_TRANS(vadd_vi, IMM_SX, vadd_vx, addi)
1522
1523static void tcg_gen_gvec_rsubi(unsigned vece, uint32_t dofs, uint32_t aofs,
1524                               int64_t c, uint32_t oprsz, uint32_t maxsz)
1525{
1526    TCGv_i64 tmp = tcg_constant_i64(c);
1527    tcg_gen_gvec_rsubs(vece, dofs, aofs, tmp, oprsz, maxsz);
1528}
1529
1530GEN_OPIVI_GVEC_TRANS(vrsub_vi, IMM_SX, vrsub_vx, rsubi)
1531
1532/* Vector Widening Integer Add/Subtract */
1533
1534/* OPIVV with WIDEN */
1535static bool opivv_widen_check(DisasContext *s, arg_rmrr *a)
1536{
1537    return require_rvv(s) &&
1538           vext_check_isa_ill(s) &&
1539           vext_check_dss(s, a->rd, a->rs1, a->rs2, a->vm);
1540}
1541
1542/* OPIVV with overwrite and WIDEN */
1543static bool opivv_overwrite_widen_check(DisasContext *s, arg_rmrr *a)
1544{
1545    return require_rvv(s) &&
1546           vext_check_isa_ill(s) &&
1547           vext_check_dss(s, a->rd, a->rs1, a->rs2, a->vm) &&
1548           vext_check_input_eew(s, a->rd, s->sew + 1, a->rs1, s->sew, a->vm) &&
1549           vext_check_input_eew(s, a->rd, s->sew + 1, a->rs2, s->sew, a->vm);
1550}
1551
1552static bool do_opivv_widen(DisasContext *s, arg_rmrr *a,
1553                           gen_helper_gvec_4_ptr *fn,
1554                           bool (*checkfn)(DisasContext *, arg_rmrr *))
1555{
1556    if (checkfn(s, a)) {
1557        uint32_t data = 0;
1558
1559        data = FIELD_DP32(data, VDATA, VM, a->vm);
1560        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);
1561        data = FIELD_DP32(data, VDATA, VTA, s->vta);
1562        data = FIELD_DP32(data, VDATA, VMA, s->vma);
1563        tcg_gen_gvec_4_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),
1564                           vreg_ofs(s, a->rs1),
1565                           vreg_ofs(s, a->rs2),
1566                           tcg_env, s->cfg_ptr->vlenb,
1567                           s->cfg_ptr->vlenb,
1568                           data, fn);
1569        finalize_rvv_inst(s);
1570        return true;
1571    }
1572    return false;
1573}
1574
1575#define GEN_OPIVV_WIDEN_TRANS(NAME, CHECK) \
1576static bool trans_##NAME(DisasContext *s, arg_rmrr *a)       \
1577{                                                            \
1578    static gen_helper_gvec_4_ptr * const fns[3] = {          \
1579        gen_helper_##NAME##_b,                               \
1580        gen_helper_##NAME##_h,                               \
1581        gen_helper_##NAME##_w                                \
1582    };                                                       \
1583    return do_opivv_widen(s, a, fns[s->sew], CHECK);         \
1584}
1585
1586GEN_OPIVV_WIDEN_TRANS(vwaddu_vv, opivv_widen_check)
1587GEN_OPIVV_WIDEN_TRANS(vwadd_vv, opivv_widen_check)
1588GEN_OPIVV_WIDEN_TRANS(vwsubu_vv, opivv_widen_check)
1589GEN_OPIVV_WIDEN_TRANS(vwsub_vv, opivv_widen_check)
1590
1591/* OPIVX with WIDEN */
1592static bool opivx_widen_check(DisasContext *s, arg_rmrr *a)
1593{
1594    return require_rvv(s) &&
1595           vext_check_isa_ill(s) &&
1596           vext_check_ds(s, a->rd, a->rs2, a->vm);
1597}
1598
1599static bool opivx_overwrite_widen_check(DisasContext *s, arg_rmrr *a)
1600{
1601    return require_rvv(s) &&
1602           vext_check_isa_ill(s) &&
1603           vext_check_ds(s, a->rd, a->rs2, a->vm) &&
1604           vext_check_input_eew(s, a->rd, s->sew + 1, a->rs2, s->sew, a->vm);
1605}
1606
1607#define GEN_OPIVX_WIDEN_TRANS(NAME, CHECK) \
1608static bool trans_##NAME(DisasContext *s, arg_rmrr *a)                    \
1609{                                                                         \
1610    if (CHECK(s, a)) {                                                    \
1611        static gen_helper_opivx * const fns[3] = {                        \
1612            gen_helper_##NAME##_b,                                        \
1613            gen_helper_##NAME##_h,                                        \
1614            gen_helper_##NAME##_w                                         \
1615        };                                                                \
1616        return opivx_trans(a->rd, a->rs1, a->rs2, a->vm, fns[s->sew], s); \
1617    }                                                                     \
1618    return false;                                                         \
1619}
1620
1621GEN_OPIVX_WIDEN_TRANS(vwaddu_vx, opivx_widen_check)
1622GEN_OPIVX_WIDEN_TRANS(vwadd_vx, opivx_widen_check)
1623GEN_OPIVX_WIDEN_TRANS(vwsubu_vx, opivx_widen_check)
1624GEN_OPIVX_WIDEN_TRANS(vwsub_vx, opivx_widen_check)
1625
1626/* WIDEN OPIVV with WIDEN */
1627static bool opiwv_widen_check(DisasContext *s, arg_rmrr *a)
1628{
1629    return require_rvv(s) &&
1630           vext_check_isa_ill(s) &&
1631           vext_check_dds(s, a->rd, a->rs1, a->rs2, a->vm);
1632}
1633
1634static bool do_opiwv_widen(DisasContext *s, arg_rmrr *a,
1635                           gen_helper_gvec_4_ptr *fn)
1636{
1637    if (opiwv_widen_check(s, a)) {
1638        uint32_t data = 0;
1639
1640        data = FIELD_DP32(data, VDATA, VM, a->vm);
1641        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);
1642        data = FIELD_DP32(data, VDATA, VTA, s->vta);
1643        data = FIELD_DP32(data, VDATA, VMA, s->vma);
1644        tcg_gen_gvec_4_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),
1645                           vreg_ofs(s, a->rs1),
1646                           vreg_ofs(s, a->rs2),
1647                           tcg_env, s->cfg_ptr->vlenb,
1648                           s->cfg_ptr->vlenb, data, fn);
1649        finalize_rvv_inst(s);
1650        return true;
1651    }
1652    return false;
1653}
1654
1655#define GEN_OPIWV_WIDEN_TRANS(NAME) \
1656static bool trans_##NAME(DisasContext *s, arg_rmrr *a)       \
1657{                                                            \
1658    static gen_helper_gvec_4_ptr * const fns[3] = {          \
1659        gen_helper_##NAME##_b,                               \
1660        gen_helper_##NAME##_h,                               \
1661        gen_helper_##NAME##_w                                \
1662    };                                                       \
1663    return do_opiwv_widen(s, a, fns[s->sew]);                \
1664}
1665
1666GEN_OPIWV_WIDEN_TRANS(vwaddu_wv)
1667GEN_OPIWV_WIDEN_TRANS(vwadd_wv)
1668GEN_OPIWV_WIDEN_TRANS(vwsubu_wv)
1669GEN_OPIWV_WIDEN_TRANS(vwsub_wv)
1670
1671/* WIDEN OPIVX with WIDEN */
1672static bool opiwx_widen_check(DisasContext *s, arg_rmrr *a)
1673{
1674    return require_rvv(s) &&
1675           vext_check_isa_ill(s) &&
1676           vext_check_dd(s, a->rd, a->rs2, a->vm);
1677}
1678
1679static bool do_opiwx_widen(DisasContext *s, arg_rmrr *a,
1680                           gen_helper_opivx *fn)
1681{
1682    if (opiwx_widen_check(s, a)) {
1683        return opivx_trans(a->rd, a->rs1, a->rs2, a->vm, fn, s);
1684    }
1685    return false;
1686}
1687
1688#define GEN_OPIWX_WIDEN_TRANS(NAME) \
1689static bool trans_##NAME(DisasContext *s, arg_rmrr *a)       \
1690{                                                            \
1691    static gen_helper_opivx * const fns[3] = {               \
1692        gen_helper_##NAME##_b,                               \
1693        gen_helper_##NAME##_h,                               \
1694        gen_helper_##NAME##_w                                \
1695    };                                                       \
1696    return do_opiwx_widen(s, a, fns[s->sew]);                \
1697}
1698
1699GEN_OPIWX_WIDEN_TRANS(vwaddu_wx)
1700GEN_OPIWX_WIDEN_TRANS(vwadd_wx)
1701GEN_OPIWX_WIDEN_TRANS(vwsubu_wx)
1702GEN_OPIWX_WIDEN_TRANS(vwsub_wx)
1703
1704static bool opivv_trans(uint32_t vd, uint32_t vs1, uint32_t vs2, uint32_t vm,
1705                        gen_helper_gvec_4_ptr *fn, DisasContext *s)
1706{
1707    uint32_t data = 0;
1708
1709    data = FIELD_DP32(data, VDATA, VM, vm);
1710    data = FIELD_DP32(data, VDATA, LMUL, s->lmul);
1711    data = FIELD_DP32(data, VDATA, VTA, s->vta);
1712    data = FIELD_DP32(data, VDATA, VTA_ALL_1S, s->cfg_vta_all_1s);
1713    data = FIELD_DP32(data, VDATA, VMA, s->vma);
1714    tcg_gen_gvec_4_ptr(vreg_ofs(s, vd), vreg_ofs(s, 0), vreg_ofs(s, vs1),
1715                       vreg_ofs(s, vs2), tcg_env, s->cfg_ptr->vlenb,
1716                       s->cfg_ptr->vlenb, data, fn);
1717    finalize_rvv_inst(s);
1718    return true;
1719}
1720
1721/* Vector Integer Add-with-Carry / Subtract-with-Borrow Instructions */
1722/* OPIVV without GVEC IR */
1723#define GEN_OPIVV_TRANS(NAME, CHECK)                                     \
1724static bool trans_##NAME(DisasContext *s, arg_rmrr *a)                   \
1725{                                                                        \
1726    if (CHECK(s, a)) {                                                   \
1727        static gen_helper_gvec_4_ptr * const fns[4] = {                  \
1728            gen_helper_##NAME##_b, gen_helper_##NAME##_h,                \
1729            gen_helper_##NAME##_w, gen_helper_##NAME##_d,                \
1730        };                                                               \
1731        return opivv_trans(a->rd, a->rs1, a->rs2, a->vm, fns[s->sew], s);\
1732    }                                                                    \
1733    return false;                                                        \
1734}
1735
1736/*
1737 * For vadc and vsbc, an illegal instruction exception is raised if the
1738 * destination vector register is v0 and LMUL > 1. (Section 11.4)
1739 */
1740static bool opivv_vadc_check(DisasContext *s, arg_rmrr *a)
1741{
1742    return require_rvv(s) &&
1743           vext_check_isa_ill(s) &&
1744           (a->rd != 0) &&
1745           vext_check_sss(s, a->rd, a->rs1, a->rs2, a->vm);
1746}
1747
1748GEN_OPIVV_TRANS(vadc_vvm, opivv_vadc_check)
1749GEN_OPIVV_TRANS(vsbc_vvm, opivv_vadc_check)
1750
1751/*
1752 * For vmadc and vmsbc, an illegal instruction exception is raised if the
1753 * destination vector register overlaps a source vector register group.
1754 */
1755static bool opivv_vmadc_check(DisasContext *s, arg_rmrr *a)
1756{
1757    return require_rvv(s) &&
1758           vext_check_isa_ill(s) &&
1759           vext_check_mss(s, a->rd, a->rs1, a->rs2);
1760}
1761
1762GEN_OPIVV_TRANS(vmadc_vvm, opivv_vmadc_check)
1763GEN_OPIVV_TRANS(vmsbc_vvm, opivv_vmadc_check)
1764
1765static bool opivx_vadc_check(DisasContext *s, arg_rmrr *a)
1766{
1767    return require_rvv(s) &&
1768           vext_check_isa_ill(s) &&
1769           (a->rd != 0) &&
1770           vext_check_ss(s, a->rd, a->rs2, a->vm);
1771}
1772
1773/* OPIVX without GVEC IR */
1774#define GEN_OPIVX_TRANS(NAME, CHECK)                                     \
1775static bool trans_##NAME(DisasContext *s, arg_rmrr *a)                   \
1776{                                                                        \
1777    if (CHECK(s, a)) {                                                   \
1778        static gen_helper_opivx * const fns[4] = {                       \
1779            gen_helper_##NAME##_b, gen_helper_##NAME##_h,                \
1780            gen_helper_##NAME##_w, gen_helper_##NAME##_d,                \
1781        };                                                               \
1782                                                                         \
1783        return opivx_trans(a->rd, a->rs1, a->rs2, a->vm, fns[s->sew], s);\
1784    }                                                                    \
1785    return false;                                                        \
1786}
1787
1788GEN_OPIVX_TRANS(vadc_vxm, opivx_vadc_check)
1789GEN_OPIVX_TRANS(vsbc_vxm, opivx_vadc_check)
1790
1791static bool opivx_vmadc_check(DisasContext *s, arg_rmrr *a)
1792{
1793    return require_rvv(s) &&
1794           vext_check_isa_ill(s) &&
1795           vext_check_ms(s, a->rd, a->rs2);
1796}
1797
1798GEN_OPIVX_TRANS(vmadc_vxm, opivx_vmadc_check)
1799GEN_OPIVX_TRANS(vmsbc_vxm, opivx_vmadc_check)
1800
1801/* OPIVI without GVEC IR */
1802#define GEN_OPIVI_TRANS(NAME, IMM_MODE, OPIVX, CHECK)                    \
1803static bool trans_##NAME(DisasContext *s, arg_rmrr *a)                   \
1804{                                                                        \
1805    if (CHECK(s, a)) {                                                   \
1806        static gen_helper_opivx * const fns[4] = {                       \
1807            gen_helper_##OPIVX##_b, gen_helper_##OPIVX##_h,              \
1808            gen_helper_##OPIVX##_w, gen_helper_##OPIVX##_d,              \
1809        };                                                               \
1810        return opivi_trans(a->rd, a->rs1, a->rs2, a->vm,                 \
1811                           fns[s->sew], s, IMM_MODE);                    \
1812    }                                                                    \
1813    return false;                                                        \
1814}
1815
1816GEN_OPIVI_TRANS(vadc_vim, IMM_SX, vadc_vxm, opivx_vadc_check)
1817GEN_OPIVI_TRANS(vmadc_vim, IMM_SX, vmadc_vxm, opivx_vmadc_check)
1818
1819/* Vector Bitwise Logical Instructions */
1820GEN_OPIVV_GVEC_TRANS(vand_vv, and)
1821GEN_OPIVV_GVEC_TRANS(vor_vv,  or)
1822GEN_OPIVV_GVEC_TRANS(vxor_vv, xor)
1823GEN_OPIVX_GVEC_TRANS(vand_vx, ands)
1824GEN_OPIVX_GVEC_TRANS(vor_vx,  ors)
1825GEN_OPIVX_GVEC_TRANS(vxor_vx, xors)
1826GEN_OPIVI_GVEC_TRANS(vand_vi, IMM_SX, vand_vx, andi)
1827GEN_OPIVI_GVEC_TRANS(vor_vi, IMM_SX, vor_vx,  ori)
1828GEN_OPIVI_GVEC_TRANS(vxor_vi, IMM_SX, vxor_vx, xori)
1829
1830/* Vector Single-Width Bit Shift Instructions */
1831GEN_OPIVV_GVEC_TRANS(vsll_vv,  shlv)
1832GEN_OPIVV_GVEC_TRANS(vsrl_vv,  shrv)
1833GEN_OPIVV_GVEC_TRANS(vsra_vv,  sarv)
1834
1835typedef void GVecGen2sFn32(unsigned, uint32_t, uint32_t, TCGv_i32,
1836                           uint32_t, uint32_t);
1837
1838static inline bool
1839do_opivx_gvec_shift(DisasContext *s, arg_rmrr *a, GVecGen2sFn32 *gvec_fn,
1840                    gen_helper_opivx *fn)
1841{
1842    if (a->vm && s->vl_eq_vlmax && !(s->vta && s->lmul < 0)) {
1843        TCGv_i32 src1 = tcg_temp_new_i32();
1844
1845        tcg_gen_trunc_tl_i32(src1, get_gpr(s, a->rs1, EXT_NONE));
1846        tcg_gen_extract_i32(src1, src1, 0, s->sew + 3);
1847        gvec_fn(s->sew, vreg_ofs(s, a->rd), vreg_ofs(s, a->rs2),
1848                src1, MAXSZ(s), MAXSZ(s));
1849
1850        finalize_rvv_inst(s);
1851        return true;
1852    }
1853    return opivx_trans(a->rd, a->rs1, a->rs2, a->vm, fn, s);
1854}
1855
1856#define GEN_OPIVX_GVEC_SHIFT_TRANS(NAME, SUF) \
1857static bool trans_##NAME(DisasContext *s, arg_rmrr *a)                    \
1858{                                                                         \
1859    static gen_helper_opivx * const fns[4] = {                            \
1860        gen_helper_##NAME##_b, gen_helper_##NAME##_h,                     \
1861        gen_helper_##NAME##_w, gen_helper_##NAME##_d,                     \
1862    };                                                                    \
1863    if (!opivx_check(s, a)) {                                             \
1864        return false;                                                     \
1865    }                                                                     \
1866    return do_opivx_gvec_shift(s, a, tcg_gen_gvec_##SUF, fns[s->sew]);    \
1867}
1868
1869GEN_OPIVX_GVEC_SHIFT_TRANS(vsll_vx,  shls)
1870GEN_OPIVX_GVEC_SHIFT_TRANS(vsrl_vx,  shrs)
1871GEN_OPIVX_GVEC_SHIFT_TRANS(vsra_vx,  sars)
1872
1873GEN_OPIVI_GVEC_TRANS(vsll_vi, IMM_TRUNC_SEW, vsll_vx, shli)
1874GEN_OPIVI_GVEC_TRANS(vsrl_vi, IMM_TRUNC_SEW, vsrl_vx, shri)
1875GEN_OPIVI_GVEC_TRANS(vsra_vi, IMM_TRUNC_SEW, vsra_vx, sari)
1876
1877/* Vector Narrowing Integer Right Shift Instructions */
1878static bool opiwv_narrow_check(DisasContext *s, arg_rmrr *a)
1879{
1880    return require_rvv(s) &&
1881           vext_check_isa_ill(s) &&
1882           vext_check_sds(s, a->rd, a->rs1, a->rs2, a->vm);
1883}
1884
1885/* OPIVV with NARROW */
1886#define GEN_OPIWV_NARROW_TRANS(NAME)                               \
1887static bool trans_##NAME(DisasContext *s, arg_rmrr *a)             \
1888{                                                                  \
1889    if (opiwv_narrow_check(s, a)) {                                \
1890        uint32_t data = 0;                                         \
1891        static gen_helper_gvec_4_ptr * const fns[3] = {            \
1892            gen_helper_##NAME##_b,                                 \
1893            gen_helper_##NAME##_h,                                 \
1894            gen_helper_##NAME##_w,                                 \
1895        };                                                         \
1896                                                                   \
1897        data = FIELD_DP32(data, VDATA, VM, a->vm);                 \
1898        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);             \
1899        data = FIELD_DP32(data, VDATA, VTA, s->vta);               \
1900        data = FIELD_DP32(data, VDATA, VMA, s->vma);               \
1901        tcg_gen_gvec_4_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),     \
1902                           vreg_ofs(s, a->rs1),                    \
1903                           vreg_ofs(s, a->rs2), tcg_env,           \
1904                           s->cfg_ptr->vlenb,                      \
1905                           s->cfg_ptr->vlenb, data,                \
1906                           fns[s->sew]);                           \
1907        finalize_rvv_inst(s);                                      \
1908        return true;                                               \
1909    }                                                              \
1910    return false;                                                  \
1911}
1912GEN_OPIWV_NARROW_TRANS(vnsra_wv)
1913GEN_OPIWV_NARROW_TRANS(vnsrl_wv)
1914
1915static bool opiwx_narrow_check(DisasContext *s, arg_rmrr *a)
1916{
1917    return require_rvv(s) &&
1918           vext_check_isa_ill(s) &&
1919           vext_check_sd(s, a->rd, a->rs2, a->vm);
1920}
1921
1922/* OPIVX with NARROW */
1923#define GEN_OPIWX_NARROW_TRANS(NAME)                                     \
1924static bool trans_##NAME(DisasContext *s, arg_rmrr *a)                   \
1925{                                                                        \
1926    if (opiwx_narrow_check(s, a)) {                                      \
1927        static gen_helper_opivx * const fns[3] = {                       \
1928            gen_helper_##NAME##_b,                                       \
1929            gen_helper_##NAME##_h,                                       \
1930            gen_helper_##NAME##_w,                                       \
1931        };                                                               \
1932        return opivx_trans(a->rd, a->rs1, a->rs2, a->vm, fns[s->sew], s);\
1933    }                                                                    \
1934    return false;                                                        \
1935}
1936
1937GEN_OPIWX_NARROW_TRANS(vnsra_wx)
1938GEN_OPIWX_NARROW_TRANS(vnsrl_wx)
1939
1940/* OPIWI with NARROW */
1941#define GEN_OPIWI_NARROW_TRANS(NAME, IMM_MODE, OPIVX)                    \
1942static bool trans_##NAME(DisasContext *s, arg_rmrr *a)                   \
1943{                                                                        \
1944    if (opiwx_narrow_check(s, a)) {                                      \
1945        static gen_helper_opivx * const fns[3] = {                       \
1946            gen_helper_##OPIVX##_b,                                      \
1947            gen_helper_##OPIVX##_h,                                      \
1948            gen_helper_##OPIVX##_w,                                      \
1949        };                                                               \
1950        return opivi_trans(a->rd, a->rs1, a->rs2, a->vm,                 \
1951                           fns[s->sew], s, IMM_MODE);                    \
1952    }                                                                    \
1953    return false;                                                        \
1954}
1955
1956GEN_OPIWI_NARROW_TRANS(vnsra_wi, IMM_ZX, vnsra_wx)
1957GEN_OPIWI_NARROW_TRANS(vnsrl_wi, IMM_ZX, vnsrl_wx)
1958
1959/* Vector Integer Comparison Instructions */
1960/*
1961 * For all comparison instructions, an illegal instruction exception is raised
1962 * if the destination vector register overlaps a source vector register group
1963 * and LMUL > 1.
1964 */
1965static bool opivv_cmp_check(DisasContext *s, arg_rmrr *a)
1966{
1967    return require_rvv(s) &&
1968           vext_check_isa_ill(s) &&
1969           vext_check_mss(s, a->rd, a->rs1, a->rs2);
1970}
1971
1972GEN_OPIVV_TRANS(vmseq_vv, opivv_cmp_check)
1973GEN_OPIVV_TRANS(vmsne_vv, opivv_cmp_check)
1974GEN_OPIVV_TRANS(vmsltu_vv, opivv_cmp_check)
1975GEN_OPIVV_TRANS(vmslt_vv, opivv_cmp_check)
1976GEN_OPIVV_TRANS(vmsleu_vv, opivv_cmp_check)
1977GEN_OPIVV_TRANS(vmsle_vv, opivv_cmp_check)
1978
1979static bool opivx_cmp_check(DisasContext *s, arg_rmrr *a)
1980{
1981    return require_rvv(s) &&
1982           vext_check_isa_ill(s) &&
1983           vext_check_ms(s, a->rd, a->rs2);
1984}
1985
1986GEN_OPIVX_TRANS(vmseq_vx, opivx_cmp_check)
1987GEN_OPIVX_TRANS(vmsne_vx, opivx_cmp_check)
1988GEN_OPIVX_TRANS(vmsltu_vx, opivx_cmp_check)
1989GEN_OPIVX_TRANS(vmslt_vx, opivx_cmp_check)
1990GEN_OPIVX_TRANS(vmsleu_vx, opivx_cmp_check)
1991GEN_OPIVX_TRANS(vmsle_vx, opivx_cmp_check)
1992GEN_OPIVX_TRANS(vmsgtu_vx, opivx_cmp_check)
1993GEN_OPIVX_TRANS(vmsgt_vx, opivx_cmp_check)
1994
1995GEN_OPIVI_TRANS(vmseq_vi, IMM_SX, vmseq_vx, opivx_cmp_check)
1996GEN_OPIVI_TRANS(vmsne_vi, IMM_SX, vmsne_vx, opivx_cmp_check)
1997GEN_OPIVI_TRANS(vmsleu_vi, IMM_SX, vmsleu_vx, opivx_cmp_check)
1998GEN_OPIVI_TRANS(vmsle_vi, IMM_SX, vmsle_vx, opivx_cmp_check)
1999GEN_OPIVI_TRANS(vmsgtu_vi, IMM_SX, vmsgtu_vx, opivx_cmp_check)
2000GEN_OPIVI_TRANS(vmsgt_vi, IMM_SX, vmsgt_vx, opivx_cmp_check)
2001
2002/* Vector Integer Min/Max Instructions */
2003GEN_OPIVV_GVEC_TRANS(vminu_vv, umin)
2004GEN_OPIVV_GVEC_TRANS(vmin_vv,  smin)
2005GEN_OPIVV_GVEC_TRANS(vmaxu_vv, umax)
2006GEN_OPIVV_GVEC_TRANS(vmax_vv,  smax)
2007GEN_OPIVX_TRANS(vminu_vx, opivx_check)
2008GEN_OPIVX_TRANS(vmin_vx,  opivx_check)
2009GEN_OPIVX_TRANS(vmaxu_vx, opivx_check)
2010GEN_OPIVX_TRANS(vmax_vx,  opivx_check)
2011
2012/* Vector Single-Width Integer Multiply Instructions */
2013
2014static bool vmulh_vv_check(DisasContext *s, arg_rmrr *a)
2015{
2016    /*
2017     * All Zve* extensions support all vector integer instructions,
2018     * except that the vmulh integer multiply variants
2019     * that return the high word of the product
2020     * (vmulh.vv, vmulh.vx, vmulhu.vv, vmulhu.vx, vmulhsu.vv, vmulhsu.vx)
2021     * are not included for EEW=64 in Zve64*. (Section 18.2)
2022     */
2023    return opivv_check(s, a) &&
2024           (!has_ext(s, RVV) ? s->sew != MO_64 : true);
2025}
2026
2027static bool vmulh_vx_check(DisasContext *s, arg_rmrr *a)
2028{
2029    /*
2030     * All Zve* extensions support all vector integer instructions,
2031     * except that the vmulh integer multiply variants
2032     * that return the high word of the product
2033     * (vmulh.vv, vmulh.vx, vmulhu.vv, vmulhu.vx, vmulhsu.vv, vmulhsu.vx)
2034     * are not included for EEW=64 in Zve64*. (Section 18.2)
2035     */
2036    return opivx_check(s, a) &&
2037           (!has_ext(s, RVV) ? s->sew != MO_64 : true);
2038}
2039
2040GEN_OPIVV_GVEC_TRANS(vmul_vv,  mul)
2041GEN_OPIVV_TRANS(vmulh_vv, vmulh_vv_check)
2042GEN_OPIVV_TRANS(vmulhu_vv, vmulh_vv_check)
2043GEN_OPIVV_TRANS(vmulhsu_vv, vmulh_vv_check)
2044GEN_OPIVX_GVEC_TRANS(vmul_vx,  muls)
2045GEN_OPIVX_TRANS(vmulh_vx, vmulh_vx_check)
2046GEN_OPIVX_TRANS(vmulhu_vx, vmulh_vx_check)
2047GEN_OPIVX_TRANS(vmulhsu_vx, vmulh_vx_check)
2048
2049/* Vector Integer Divide Instructions */
2050GEN_OPIVV_TRANS(vdivu_vv, opivv_check)
2051GEN_OPIVV_TRANS(vdiv_vv, opivv_check)
2052GEN_OPIVV_TRANS(vremu_vv, opivv_check)
2053GEN_OPIVV_TRANS(vrem_vv, opivv_check)
2054GEN_OPIVX_TRANS(vdivu_vx, opivx_check)
2055GEN_OPIVX_TRANS(vdiv_vx, opivx_check)
2056GEN_OPIVX_TRANS(vremu_vx, opivx_check)
2057GEN_OPIVX_TRANS(vrem_vx, opivx_check)
2058
2059/* Vector Widening Integer Multiply Instructions */
2060GEN_OPIVV_WIDEN_TRANS(vwmul_vv, opivv_widen_check)
2061GEN_OPIVV_WIDEN_TRANS(vwmulu_vv, opivv_widen_check)
2062GEN_OPIVV_WIDEN_TRANS(vwmulsu_vv, opivv_widen_check)
2063GEN_OPIVX_WIDEN_TRANS(vwmul_vx, opivx_widen_check)
2064GEN_OPIVX_WIDEN_TRANS(vwmulu_vx, opivx_widen_check)
2065GEN_OPIVX_WIDEN_TRANS(vwmulsu_vx, opivx_widen_check)
2066
2067/* Vector Single-Width Integer Multiply-Add Instructions */
2068GEN_OPIVV_TRANS(vmacc_vv, opivv_check)
2069GEN_OPIVV_TRANS(vnmsac_vv, opivv_check)
2070GEN_OPIVV_TRANS(vmadd_vv, opivv_check)
2071GEN_OPIVV_TRANS(vnmsub_vv, opivv_check)
2072GEN_OPIVX_TRANS(vmacc_vx, opivx_check)
2073GEN_OPIVX_TRANS(vnmsac_vx, opivx_check)
2074GEN_OPIVX_TRANS(vmadd_vx, opivx_check)
2075GEN_OPIVX_TRANS(vnmsub_vx, opivx_check)
2076
2077/* Vector Widening Integer Multiply-Add Instructions */
2078GEN_OPIVV_WIDEN_TRANS(vwmaccu_vv, opivv_overwrite_widen_check)
2079GEN_OPIVV_WIDEN_TRANS(vwmacc_vv, opivv_overwrite_widen_check)
2080GEN_OPIVV_WIDEN_TRANS(vwmaccsu_vv, opivv_overwrite_widen_check)
2081GEN_OPIVX_WIDEN_TRANS(vwmaccu_vx, opivx_overwrite_widen_check)
2082GEN_OPIVX_WIDEN_TRANS(vwmacc_vx, opivx_overwrite_widen_check)
2083GEN_OPIVX_WIDEN_TRANS(vwmaccsu_vx, opivx_overwrite_widen_check)
2084GEN_OPIVX_WIDEN_TRANS(vwmaccus_vx, opivx_overwrite_widen_check)
2085
2086/* Vector Integer Merge and Move Instructions */
2087static bool trans_vmv_v_v(DisasContext *s, arg_vmv_v_v *a)
2088{
2089    if (require_rvv(s) &&
2090        vext_check_isa_ill(s) &&
2091        /* vmv.v.v has rs2 = 0 and vm = 1 */
2092        vext_check_sss(s, a->rd, a->rs1, 0, 1)) {
2093        if (s->vl_eq_vlmax && !(s->vta && s->lmul < 0)) {
2094            tcg_gen_gvec_mov(s->sew, vreg_ofs(s, a->rd),
2095                             vreg_ofs(s, a->rs1),
2096                             MAXSZ(s), MAXSZ(s));
2097        } else {
2098            uint32_t data = FIELD_DP32(0, VDATA, LMUL, s->lmul);
2099            data = FIELD_DP32(data, VDATA, VTA, s->vta);
2100            static gen_helper_gvec_2_ptr * const fns[4] = {
2101                gen_helper_vmv_v_v_b, gen_helper_vmv_v_v_h,
2102                gen_helper_vmv_v_v_w, gen_helper_vmv_v_v_d,
2103            };
2104
2105            tcg_gen_gvec_2_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, a->rs1),
2106                               tcg_env, s->cfg_ptr->vlenb,
2107                               s->cfg_ptr->vlenb, data,
2108                               fns[s->sew]);
2109        }
2110        finalize_rvv_inst(s);
2111        return true;
2112    }
2113    return false;
2114}
2115
2116typedef void gen_helper_vmv_vx(TCGv_ptr, TCGv_i64, TCGv_env, TCGv_i32);
2117static bool trans_vmv_v_x(DisasContext *s, arg_vmv_v_x *a)
2118{
2119    if (require_rvv(s) &&
2120        vext_check_isa_ill(s) &&
2121        /* vmv.v.x has rs2 = 0 and vm = 1 */
2122        vext_check_ss(s, a->rd, 0, 1)) {
2123        TCGv s1;
2124
2125        s1 = get_gpr(s, a->rs1, EXT_SIGN);
2126
2127        if (s->vl_eq_vlmax && !(s->vta && s->lmul < 0)) {
2128            if (get_xl(s) == MXL_RV32 && s->sew == MO_64) {
2129                TCGv_i64 s1_i64 = tcg_temp_new_i64();
2130                tcg_gen_ext_tl_i64(s1_i64, s1);
2131                tcg_gen_gvec_dup_i64(s->sew, vreg_ofs(s, a->rd),
2132                                     MAXSZ(s), MAXSZ(s), s1_i64);
2133            } else {
2134                tcg_gen_gvec_dup_tl(s->sew, vreg_ofs(s, a->rd),
2135                                    MAXSZ(s), MAXSZ(s), s1);
2136            }
2137        } else {
2138            TCGv_i32 desc;
2139            TCGv_i64 s1_i64 = tcg_temp_new_i64();
2140            TCGv_ptr dest = tcg_temp_new_ptr();
2141            uint32_t data = FIELD_DP32(0, VDATA, LMUL, s->lmul);
2142            data = FIELD_DP32(data, VDATA, VTA, s->vta);
2143            static gen_helper_vmv_vx * const fns[4] = {
2144                gen_helper_vmv_v_x_b, gen_helper_vmv_v_x_h,
2145                gen_helper_vmv_v_x_w, gen_helper_vmv_v_x_d,
2146            };
2147
2148            tcg_gen_ext_tl_i64(s1_i64, s1);
2149            desc = tcg_constant_i32(simd_desc(s->cfg_ptr->vlenb,
2150                                              s->cfg_ptr->vlenb, data));
2151            tcg_gen_addi_ptr(dest, tcg_env, vreg_ofs(s, a->rd));
2152            fns[s->sew](dest, s1_i64, tcg_env, desc);
2153        }
2154
2155        finalize_rvv_inst(s);
2156        return true;
2157    }
2158    return false;
2159}
2160
2161static bool trans_vmv_v_i(DisasContext *s, arg_vmv_v_i *a)
2162{
2163    if (require_rvv(s) &&
2164        vext_check_isa_ill(s) &&
2165        /* vmv.v.i has rs2 = 0 and vm = 1 */
2166        vext_check_ss(s, a->rd, 0, 1)) {
2167        int64_t simm = sextract64(a->rs1, 0, 5);
2168        if (s->vl_eq_vlmax && !(s->vta && s->lmul < 0)) {
2169            tcg_gen_gvec_dup_imm(s->sew, vreg_ofs(s, a->rd),
2170                                 MAXSZ(s), MAXSZ(s), simm);
2171        } else {
2172            TCGv_i32 desc;
2173            TCGv_i64 s1;
2174            TCGv_ptr dest;
2175            uint32_t data = FIELD_DP32(0, VDATA, LMUL, s->lmul);
2176            data = FIELD_DP32(data, VDATA, VTA, s->vta);
2177            static gen_helper_vmv_vx * const fns[4] = {
2178                gen_helper_vmv_v_x_b, gen_helper_vmv_v_x_h,
2179                gen_helper_vmv_v_x_w, gen_helper_vmv_v_x_d,
2180            };
2181
2182            s1 = tcg_constant_i64(simm);
2183            dest = tcg_temp_new_ptr();
2184            desc = tcg_constant_i32(simd_desc(s->cfg_ptr->vlenb,
2185                                              s->cfg_ptr->vlenb, data));
2186            tcg_gen_addi_ptr(dest, tcg_env, vreg_ofs(s, a->rd));
2187            fns[s->sew](dest, s1, tcg_env, desc);
2188        }
2189        finalize_rvv_inst(s);
2190        return true;
2191    }
2192    return false;
2193}
2194
2195GEN_OPIVV_TRANS(vmerge_vvm, opivv_vadc_check)
2196GEN_OPIVX_TRANS(vmerge_vxm, opivx_vadc_check)
2197GEN_OPIVI_TRANS(vmerge_vim, IMM_SX, vmerge_vxm, opivx_vadc_check)
2198
2199/*
2200 *** Vector Fixed-Point Arithmetic Instructions
2201 */
2202
2203/* Vector Single-Width Saturating Add and Subtract */
2204GEN_OPIVV_TRANS(vsaddu_vv, opivv_check)
2205GEN_OPIVV_TRANS(vsadd_vv,  opivv_check)
2206GEN_OPIVV_TRANS(vssubu_vv, opivv_check)
2207GEN_OPIVV_TRANS(vssub_vv,  opivv_check)
2208GEN_OPIVX_TRANS(vsaddu_vx,  opivx_check)
2209GEN_OPIVX_TRANS(vsadd_vx,  opivx_check)
2210GEN_OPIVX_TRANS(vssubu_vx,  opivx_check)
2211GEN_OPIVX_TRANS(vssub_vx,  opivx_check)
2212GEN_OPIVI_TRANS(vsaddu_vi, IMM_SX, vsaddu_vx, opivx_check)
2213GEN_OPIVI_TRANS(vsadd_vi, IMM_SX, vsadd_vx, opivx_check)
2214
2215/* Vector Single-Width Averaging Add and Subtract */
2216GEN_OPIVV_TRANS(vaadd_vv, opivv_check)
2217GEN_OPIVV_TRANS(vaaddu_vv, opivv_check)
2218GEN_OPIVV_TRANS(vasub_vv, opivv_check)
2219GEN_OPIVV_TRANS(vasubu_vv, opivv_check)
2220GEN_OPIVX_TRANS(vaadd_vx,  opivx_check)
2221GEN_OPIVX_TRANS(vaaddu_vx,  opivx_check)
2222GEN_OPIVX_TRANS(vasub_vx,  opivx_check)
2223GEN_OPIVX_TRANS(vasubu_vx,  opivx_check)
2224
2225/* Vector Single-Width Fractional Multiply with Rounding and Saturation */
2226
2227static bool vsmul_vv_check(DisasContext *s, arg_rmrr *a)
2228{
2229    /*
2230     * All Zve* extensions support all vector fixed-point arithmetic
2231     * instructions, except that vsmul.vv and vsmul.vx are not supported
2232     * for EEW=64 in Zve64*. (Section 18.2)
2233     */
2234    return opivv_check(s, a) &&
2235           (!has_ext(s, RVV) ? s->sew != MO_64 : true);
2236}
2237
2238static bool vsmul_vx_check(DisasContext *s, arg_rmrr *a)
2239{
2240    /*
2241     * All Zve* extensions support all vector fixed-point arithmetic
2242     * instructions, except that vsmul.vv and vsmul.vx are not supported
2243     * for EEW=64 in Zve64*. (Section 18.2)
2244     */
2245    return opivx_check(s, a) &&
2246           (!has_ext(s, RVV) ? s->sew != MO_64 : true);
2247}
2248
2249GEN_OPIVV_TRANS(vsmul_vv, vsmul_vv_check)
2250GEN_OPIVX_TRANS(vsmul_vx,  vsmul_vx_check)
2251
2252/* Vector Single-Width Scaling Shift Instructions */
2253GEN_OPIVV_TRANS(vssrl_vv, opivv_check)
2254GEN_OPIVV_TRANS(vssra_vv, opivv_check)
2255GEN_OPIVX_TRANS(vssrl_vx,  opivx_check)
2256GEN_OPIVX_TRANS(vssra_vx,  opivx_check)
2257GEN_OPIVI_TRANS(vssrl_vi, IMM_TRUNC_SEW, vssrl_vx, opivx_check)
2258GEN_OPIVI_TRANS(vssra_vi, IMM_TRUNC_SEW, vssra_vx, opivx_check)
2259
2260/* Vector Narrowing Fixed-Point Clip Instructions */
2261GEN_OPIWV_NARROW_TRANS(vnclipu_wv)
2262GEN_OPIWV_NARROW_TRANS(vnclip_wv)
2263GEN_OPIWX_NARROW_TRANS(vnclipu_wx)
2264GEN_OPIWX_NARROW_TRANS(vnclip_wx)
2265GEN_OPIWI_NARROW_TRANS(vnclipu_wi, IMM_ZX, vnclipu_wx)
2266GEN_OPIWI_NARROW_TRANS(vnclip_wi, IMM_ZX, vnclip_wx)
2267
2268/*
2269 *** Vector Float Point Arithmetic Instructions
2270 */
2271
2272/*
2273 * As RVF-only cpus always have values NaN-boxed to 64-bits,
2274 * RVF and RVD can be treated equally.
2275 * We don't have to deal with the cases of: SEW > FLEN.
2276 *
2277 * If SEW < FLEN, check whether input fp register is a valid
2278 * NaN-boxed value, in which case the least-significant SEW bits
2279 * of the f register are used, else the canonical NaN value is used.
2280 */
2281static void do_nanbox(DisasContext *s, TCGv_i64 out, TCGv_i64 in)
2282{
2283    switch (s->sew) {
2284    case 1:
2285        gen_check_nanbox_h(out, in);
2286        break;
2287    case 2:
2288        gen_check_nanbox_s(out, in);
2289        break;
2290    case 3:
2291        tcg_gen_mov_i64(out, in);
2292        break;
2293    default:
2294        g_assert_not_reached();
2295    }
2296}
2297
2298/* Vector Single-Width Floating-Point Add/Subtract Instructions */
2299
2300/*
2301 * If the current SEW does not correspond to a supported IEEE floating-point
2302 * type, an illegal instruction exception is raised.
2303 */
2304static bool opfvv_check(DisasContext *s, arg_rmrr *a)
2305{
2306    return require_rvv(s) &&
2307           require_rvf(s) &&
2308           vext_check_isa_ill(s) &&
2309           vext_check_sss(s, a->rd, a->rs1, a->rs2, a->vm);
2310}
2311
2312/* OPFVV without GVEC IR */
2313#define GEN_OPFVV_TRANS(NAME, CHECK)                               \
2314static bool trans_##NAME(DisasContext *s, arg_rmrr *a)             \
2315{                                                                  \
2316    if (CHECK(s, a)) {                                             \
2317        uint32_t data = 0;                                         \
2318        static gen_helper_gvec_4_ptr * const fns[3] = {            \
2319            gen_helper_##NAME##_h,                                 \
2320            gen_helper_##NAME##_w,                                 \
2321            gen_helper_##NAME##_d,                                 \
2322        };                                                         \
2323        gen_set_rm(s, RISCV_FRM_DYN);                              \
2324                                                                   \
2325        data = FIELD_DP32(data, VDATA, VM, a->vm);                 \
2326        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);             \
2327        data = FIELD_DP32(data, VDATA, VTA, s->vta);               \
2328        data =                                                     \
2329            FIELD_DP32(data, VDATA, VTA_ALL_1S, s->cfg_vta_all_1s);\
2330        data = FIELD_DP32(data, VDATA, VMA, s->vma);               \
2331        tcg_gen_gvec_4_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),     \
2332                           vreg_ofs(s, a->rs1),                    \
2333                           vreg_ofs(s, a->rs2), tcg_env,           \
2334                           s->cfg_ptr->vlenb,                      \
2335                           s->cfg_ptr->vlenb, data,                \
2336                           fns[s->sew - 1]);                       \
2337        finalize_rvv_inst(s);                                      \
2338        return true;                                               \
2339    }                                                              \
2340    return false;                                                  \
2341}
2342GEN_OPFVV_TRANS(vfadd_vv, opfvv_check)
2343GEN_OPFVV_TRANS(vfsub_vv, opfvv_check)
2344
2345typedef void gen_helper_opfvf(TCGv_ptr, TCGv_ptr, TCGv_i64, TCGv_ptr,
2346                              TCGv_env, TCGv_i32);
2347
2348static bool opfvf_trans(uint32_t vd, uint32_t rs1, uint32_t vs2,
2349                        uint32_t data, gen_helper_opfvf *fn, DisasContext *s)
2350{
2351    TCGv_ptr dest, src2, mask;
2352    TCGv_i32 desc;
2353    TCGv_i64 t1;
2354
2355    dest = tcg_temp_new_ptr();
2356    mask = tcg_temp_new_ptr();
2357    src2 = tcg_temp_new_ptr();
2358    desc = tcg_constant_i32(simd_desc(s->cfg_ptr->vlenb,
2359                                      s->cfg_ptr->vlenb, data));
2360
2361    tcg_gen_addi_ptr(dest, tcg_env, vreg_ofs(s, vd));
2362    tcg_gen_addi_ptr(src2, tcg_env, vreg_ofs(s, vs2));
2363    tcg_gen_addi_ptr(mask, tcg_env, vreg_ofs(s, 0));
2364
2365    /* NaN-box f[rs1] */
2366    t1 = tcg_temp_new_i64();
2367    do_nanbox(s, t1, cpu_fpr[rs1]);
2368
2369    fn(dest, mask, t1, src2, tcg_env, desc);
2370
2371    finalize_rvv_inst(s);
2372    return true;
2373}
2374
2375/*
2376 * If the current SEW does not correspond to a supported IEEE floating-point
2377 * type, an illegal instruction exception is raised
2378 */
2379static bool opfvf_check(DisasContext *s, arg_rmrr *a)
2380{
2381    return require_rvv(s) &&
2382           require_rvf(s) &&
2383           vext_check_isa_ill(s) &&
2384           vext_check_ss(s, a->rd, a->rs2, a->vm);
2385}
2386
2387/* OPFVF without GVEC IR */
2388#define GEN_OPFVF_TRANS(NAME, CHECK)                              \
2389static bool trans_##NAME(DisasContext *s, arg_rmrr *a)            \
2390{                                                                 \
2391    if (CHECK(s, a)) {                                            \
2392        uint32_t data = 0;                                        \
2393        static gen_helper_opfvf *const fns[3] = {                 \
2394            gen_helper_##NAME##_h,                                \
2395            gen_helper_##NAME##_w,                                \
2396            gen_helper_##NAME##_d,                                \
2397        };                                                        \
2398        gen_set_rm(s, RISCV_FRM_DYN);                             \
2399        data = FIELD_DP32(data, VDATA, VM, a->vm);                \
2400        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);            \
2401        data = FIELD_DP32(data, VDATA, VTA, s->vta);              \
2402        data = FIELD_DP32(data, VDATA, VTA_ALL_1S,                \
2403                          s->cfg_vta_all_1s);                     \
2404        data = FIELD_DP32(data, VDATA, VMA, s->vma);              \
2405        return opfvf_trans(a->rd, a->rs1, a->rs2, data,           \
2406                           fns[s->sew - 1], s);                   \
2407    }                                                             \
2408    return false;                                                 \
2409}
2410
2411GEN_OPFVF_TRANS(vfadd_vf,  opfvf_check)
2412GEN_OPFVF_TRANS(vfsub_vf,  opfvf_check)
2413GEN_OPFVF_TRANS(vfrsub_vf,  opfvf_check)
2414
2415/* Vector Widening Floating-Point Add/Subtract Instructions */
2416static bool opfvv_widen_check(DisasContext *s, arg_rmrr *a)
2417{
2418    return require_rvv(s) &&
2419           require_rvf(s) &&
2420           require_scale_rvf(s) &&
2421           vext_check_isa_ill(s) &&
2422           vext_check_dss(s, a->rd, a->rs1, a->rs2, a->vm);
2423}
2424
2425static bool opfvv_overwrite_widen_check(DisasContext *s, arg_rmrr *a)
2426{
2427    return require_rvv(s) &&
2428           require_rvf(s) &&
2429           require_scale_rvf(s) &&
2430           vext_check_isa_ill(s) &&
2431           vext_check_dss(s, a->rd, a->rs1, a->rs2, a->vm) &&
2432           vext_check_input_eew(s, a->rd, s->sew + 1, a->rs1, s->sew, a->vm) &&
2433           vext_check_input_eew(s, a->rd, s->sew + 1, a->rs2, s->sew, a->vm);
2434}
2435
2436/* OPFVV with WIDEN */
2437#define GEN_OPFVV_WIDEN_TRANS(NAME, CHECK)                       \
2438static bool trans_##NAME(DisasContext *s, arg_rmrr *a)           \
2439{                                                                \
2440    if (CHECK(s, a)) {                                           \
2441        uint32_t data = 0;                                       \
2442        static gen_helper_gvec_4_ptr * const fns[2] = {          \
2443            gen_helper_##NAME##_h, gen_helper_##NAME##_w,        \
2444        };                                                       \
2445        gen_set_rm(s, RISCV_FRM_DYN);                            \
2446                                                                 \
2447        data = FIELD_DP32(data, VDATA, VM, a->vm);               \
2448        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);           \
2449        data = FIELD_DP32(data, VDATA, VTA, s->vta);             \
2450        data = FIELD_DP32(data, VDATA, VMA, s->vma);             \
2451        tcg_gen_gvec_4_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),   \
2452                           vreg_ofs(s, a->rs1),                  \
2453                           vreg_ofs(s, a->rs2), tcg_env,         \
2454                           s->cfg_ptr->vlenb,                    \
2455                           s->cfg_ptr->vlenb, data,              \
2456                           fns[s->sew - 1]);                     \
2457        finalize_rvv_inst(s);                                    \
2458        return true;                                             \
2459    }                                                            \
2460    return false;                                                \
2461}
2462
2463GEN_OPFVV_WIDEN_TRANS(vfwadd_vv, opfvv_widen_check)
2464GEN_OPFVV_WIDEN_TRANS(vfwsub_vv, opfvv_widen_check)
2465
2466static bool opfvf_widen_check(DisasContext *s, arg_rmrr *a)
2467{
2468    return require_rvv(s) &&
2469           require_rvf(s) &&
2470           require_scale_rvf(s) &&
2471           vext_check_isa_ill(s) &&
2472           vext_check_ds(s, a->rd, a->rs2, a->vm);
2473}
2474
2475static bool opfvf_overwrite_widen_check(DisasContext *s, arg_rmrr *a)
2476{
2477    return require_rvv(s) &&
2478           require_rvf(s) &&
2479           require_scale_rvf(s) &&
2480           vext_check_isa_ill(s) &&
2481           vext_check_ds(s, a->rd, a->rs2, a->vm) &&
2482           vext_check_input_eew(s, a->rd, s->sew + 1, a->rs2, s->sew, a->vm);
2483}
2484
2485/* OPFVF with WIDEN */
2486#define GEN_OPFVF_WIDEN_TRANS(NAME, CHECK)                       \
2487static bool trans_##NAME(DisasContext *s, arg_rmrr *a)           \
2488{                                                                \
2489    if (CHECK(s, a)) {                                           \
2490        uint32_t data = 0;                                       \
2491        static gen_helper_opfvf *const fns[2] = {                \
2492            gen_helper_##NAME##_h, gen_helper_##NAME##_w,        \
2493        };                                                       \
2494        gen_set_rm(s, RISCV_FRM_DYN);                            \
2495        data = FIELD_DP32(data, VDATA, VM, a->vm);               \
2496        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);           \
2497        data = FIELD_DP32(data, VDATA, VTA, s->vta);             \
2498        data = FIELD_DP32(data, VDATA, VMA, s->vma);             \
2499        return opfvf_trans(a->rd, a->rs1, a->rs2, data,          \
2500                           fns[s->sew - 1], s);                  \
2501    }                                                            \
2502    return false;                                                \
2503}
2504
2505GEN_OPFVF_WIDEN_TRANS(vfwadd_vf, opfvf_widen_check)
2506GEN_OPFVF_WIDEN_TRANS(vfwsub_vf, opfvf_widen_check)
2507
2508static bool opfwv_widen_check(DisasContext *s, arg_rmrr *a)
2509{
2510    return require_rvv(s) &&
2511           require_rvf(s) &&
2512           require_scale_rvf(s) &&
2513           vext_check_isa_ill(s) &&
2514           vext_check_dds(s, a->rd, a->rs1, a->rs2, a->vm);
2515}
2516
2517/* WIDEN OPFVV with WIDEN */
2518#define GEN_OPFWV_WIDEN_TRANS(NAME)                                \
2519static bool trans_##NAME(DisasContext *s, arg_rmrr *a)             \
2520{                                                                  \
2521    if (opfwv_widen_check(s, a)) {                                 \
2522        uint32_t data = 0;                                         \
2523        static gen_helper_gvec_4_ptr * const fns[2] = {            \
2524            gen_helper_##NAME##_h, gen_helper_##NAME##_w,          \
2525        };                                                         \
2526        gen_set_rm(s, RISCV_FRM_DYN);                              \
2527                                                                   \
2528        data = FIELD_DP32(data, VDATA, VM, a->vm);                 \
2529        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);             \
2530        data = FIELD_DP32(data, VDATA, VTA, s->vta);               \
2531        data = FIELD_DP32(data, VDATA, VMA, s->vma);               \
2532        tcg_gen_gvec_4_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),     \
2533                           vreg_ofs(s, a->rs1),                    \
2534                           vreg_ofs(s, a->rs2), tcg_env,           \
2535                           s->cfg_ptr->vlenb,                      \
2536                           s->cfg_ptr->vlenb, data,                \
2537                           fns[s->sew - 1]);                       \
2538        finalize_rvv_inst(s);                                      \
2539        return true;                                               \
2540    }                                                              \
2541    return false;                                                  \
2542}
2543
2544GEN_OPFWV_WIDEN_TRANS(vfwadd_wv)
2545GEN_OPFWV_WIDEN_TRANS(vfwsub_wv)
2546
2547static bool opfwf_widen_check(DisasContext *s, arg_rmrr *a)
2548{
2549    return require_rvv(s) &&
2550           require_rvf(s) &&
2551           require_scale_rvf(s) &&
2552           vext_check_isa_ill(s) &&
2553           vext_check_dd(s, a->rd, a->rs2, a->vm);
2554}
2555
2556/* WIDEN OPFVF with WIDEN */
2557#define GEN_OPFWF_WIDEN_TRANS(NAME)                              \
2558static bool trans_##NAME(DisasContext *s, arg_rmrr *a)           \
2559{                                                                \
2560    if (opfwf_widen_check(s, a)) {                               \
2561        uint32_t data = 0;                                       \
2562        static gen_helper_opfvf *const fns[2] = {                \
2563            gen_helper_##NAME##_h, gen_helper_##NAME##_w,        \
2564        };                                                       \
2565        gen_set_rm(s, RISCV_FRM_DYN);                            \
2566        data = FIELD_DP32(data, VDATA, VM, a->vm);               \
2567        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);           \
2568        data = FIELD_DP32(data, VDATA, VTA, s->vta);             \
2569        data = FIELD_DP32(data, VDATA, VMA, s->vma);             \
2570        return opfvf_trans(a->rd, a->rs1, a->rs2, data,          \
2571                           fns[s->sew - 1], s);                  \
2572    }                                                            \
2573    return false;                                                \
2574}
2575
2576GEN_OPFWF_WIDEN_TRANS(vfwadd_wf)
2577GEN_OPFWF_WIDEN_TRANS(vfwsub_wf)
2578
2579/* Vector Single-Width Floating-Point Multiply/Divide Instructions */
2580GEN_OPFVV_TRANS(vfmul_vv, opfvv_check)
2581GEN_OPFVV_TRANS(vfdiv_vv, opfvv_check)
2582GEN_OPFVF_TRANS(vfmul_vf,  opfvf_check)
2583GEN_OPFVF_TRANS(vfdiv_vf,  opfvf_check)
2584GEN_OPFVF_TRANS(vfrdiv_vf,  opfvf_check)
2585
2586/* Vector Widening Floating-Point Multiply */
2587GEN_OPFVV_WIDEN_TRANS(vfwmul_vv, opfvv_widen_check)
2588GEN_OPFVF_WIDEN_TRANS(vfwmul_vf, opfvf_widen_check)
2589
2590/* Vector Single-Width Floating-Point Fused Multiply-Add Instructions */
2591GEN_OPFVV_TRANS(vfmacc_vv, opfvv_check)
2592GEN_OPFVV_TRANS(vfnmacc_vv, opfvv_check)
2593GEN_OPFVV_TRANS(vfmsac_vv, opfvv_check)
2594GEN_OPFVV_TRANS(vfnmsac_vv, opfvv_check)
2595GEN_OPFVV_TRANS(vfmadd_vv, opfvv_check)
2596GEN_OPFVV_TRANS(vfnmadd_vv, opfvv_check)
2597GEN_OPFVV_TRANS(vfmsub_vv, opfvv_check)
2598GEN_OPFVV_TRANS(vfnmsub_vv, opfvv_check)
2599GEN_OPFVF_TRANS(vfmacc_vf, opfvf_check)
2600GEN_OPFVF_TRANS(vfnmacc_vf, opfvf_check)
2601GEN_OPFVF_TRANS(vfmsac_vf, opfvf_check)
2602GEN_OPFVF_TRANS(vfnmsac_vf, opfvf_check)
2603GEN_OPFVF_TRANS(vfmadd_vf, opfvf_check)
2604GEN_OPFVF_TRANS(vfnmadd_vf, opfvf_check)
2605GEN_OPFVF_TRANS(vfmsub_vf, opfvf_check)
2606GEN_OPFVF_TRANS(vfnmsub_vf, opfvf_check)
2607
2608/* Vector Widening Floating-Point Fused Multiply-Add Instructions */
2609GEN_OPFVV_WIDEN_TRANS(vfwmacc_vv, opfvv_overwrite_widen_check)
2610GEN_OPFVV_WIDEN_TRANS(vfwnmacc_vv, opfvv_overwrite_widen_check)
2611GEN_OPFVV_WIDEN_TRANS(vfwmsac_vv, opfvv_overwrite_widen_check)
2612GEN_OPFVV_WIDEN_TRANS(vfwnmsac_vv, opfvv_overwrite_widen_check)
2613GEN_OPFVF_WIDEN_TRANS(vfwmacc_vf, opfvf_overwrite_widen_check)
2614GEN_OPFVF_WIDEN_TRANS(vfwnmacc_vf, opfvf_overwrite_widen_check)
2615GEN_OPFVF_WIDEN_TRANS(vfwmsac_vf, opfvf_overwrite_widen_check)
2616GEN_OPFVF_WIDEN_TRANS(vfwnmsac_vf, opfvf_overwrite_widen_check)
2617
2618/* Vector Floating-Point Square-Root Instruction */
2619
2620/*
2621 * If the current SEW does not correspond to a supported IEEE floating-point
2622 * type, an illegal instruction exception is raised
2623 */
2624static bool opfv_check(DisasContext *s, arg_rmr *a)
2625{
2626    return require_rvv(s) &&
2627           require_rvf(s) &&
2628           vext_check_isa_ill(s) &&
2629           /* OPFV instructions ignore vs1 check */
2630           vext_check_ss(s, a->rd, a->rs2, a->vm);
2631}
2632
2633static bool do_opfv(DisasContext *s, arg_rmr *a,
2634                    gen_helper_gvec_3_ptr *fn,
2635                    bool (*checkfn)(DisasContext *, arg_rmr *),
2636                    int rm)
2637{
2638    if (checkfn(s, a)) {
2639        uint32_t data = 0;
2640        gen_set_rm_chkfrm(s, rm);
2641
2642        data = FIELD_DP32(data, VDATA, VM, a->vm);
2643        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);
2644        data = FIELD_DP32(data, VDATA, VTA, s->vta);
2645        data = FIELD_DP32(data, VDATA, VMA, s->vma);
2646        tcg_gen_gvec_3_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),
2647                           vreg_ofs(s, a->rs2), tcg_env,
2648                           s->cfg_ptr->vlenb,
2649                           s->cfg_ptr->vlenb, data, fn);
2650        finalize_rvv_inst(s);
2651        return true;
2652    }
2653    return false;
2654}
2655
2656#define GEN_OPFV_TRANS(NAME, CHECK, FRM)               \
2657static bool trans_##NAME(DisasContext *s, arg_rmr *a)  \
2658{                                                      \
2659    static gen_helper_gvec_3_ptr * const fns[3] = {    \
2660        gen_helper_##NAME##_h,                         \
2661        gen_helper_##NAME##_w,                         \
2662        gen_helper_##NAME##_d                          \
2663    };                                                 \
2664    return do_opfv(s, a, fns[s->sew - 1], CHECK, FRM); \
2665}
2666
2667GEN_OPFV_TRANS(vfsqrt_v, opfv_check, RISCV_FRM_DYN)
2668GEN_OPFV_TRANS(vfrsqrt7_v, opfv_check, RISCV_FRM_DYN)
2669GEN_OPFV_TRANS(vfrec7_v, opfv_check, RISCV_FRM_DYN)
2670
2671/* Vector Floating-Point MIN/MAX Instructions */
2672GEN_OPFVV_TRANS(vfmin_vv, opfvv_check)
2673GEN_OPFVV_TRANS(vfmax_vv, opfvv_check)
2674GEN_OPFVF_TRANS(vfmin_vf, opfvf_check)
2675GEN_OPFVF_TRANS(vfmax_vf, opfvf_check)
2676
2677/* Vector Floating-Point Sign-Injection Instructions */
2678GEN_OPFVV_TRANS(vfsgnj_vv, opfvv_check)
2679GEN_OPFVV_TRANS(vfsgnjn_vv, opfvv_check)
2680GEN_OPFVV_TRANS(vfsgnjx_vv, opfvv_check)
2681GEN_OPFVF_TRANS(vfsgnj_vf, opfvf_check)
2682GEN_OPFVF_TRANS(vfsgnjn_vf, opfvf_check)
2683GEN_OPFVF_TRANS(vfsgnjx_vf, opfvf_check)
2684
2685/* Vector Floating-Point Compare Instructions */
2686static bool opfvv_cmp_check(DisasContext *s, arg_rmrr *a)
2687{
2688    return require_rvv(s) &&
2689           require_rvf(s) &&
2690           vext_check_isa_ill(s) &&
2691           vext_check_mss(s, a->rd, a->rs1, a->rs2);
2692}
2693
2694GEN_OPFVV_TRANS(vmfeq_vv, opfvv_cmp_check)
2695GEN_OPFVV_TRANS(vmfne_vv, opfvv_cmp_check)
2696GEN_OPFVV_TRANS(vmflt_vv, opfvv_cmp_check)
2697GEN_OPFVV_TRANS(vmfle_vv, opfvv_cmp_check)
2698
2699static bool opfvf_cmp_check(DisasContext *s, arg_rmrr *a)
2700{
2701    return require_rvv(s) &&
2702           require_rvf(s) &&
2703           vext_check_isa_ill(s) &&
2704           vext_check_ms(s, a->rd, a->rs2);
2705}
2706
2707GEN_OPFVF_TRANS(vmfeq_vf, opfvf_cmp_check)
2708GEN_OPFVF_TRANS(vmfne_vf, opfvf_cmp_check)
2709GEN_OPFVF_TRANS(vmflt_vf, opfvf_cmp_check)
2710GEN_OPFVF_TRANS(vmfle_vf, opfvf_cmp_check)
2711GEN_OPFVF_TRANS(vmfgt_vf, opfvf_cmp_check)
2712GEN_OPFVF_TRANS(vmfge_vf, opfvf_cmp_check)
2713
2714/* Vector Floating-Point Classify Instruction */
2715GEN_OPFV_TRANS(vfclass_v, opfv_check, RISCV_FRM_DYN)
2716
2717/* Vector Floating-Point Merge Instruction */
2718GEN_OPFVF_TRANS(vfmerge_vfm,  opfvf_check)
2719
2720static bool trans_vfmv_v_f(DisasContext *s, arg_vfmv_v_f *a)
2721{
2722    if (require_rvv(s) &&
2723        require_rvf(s) &&
2724        vext_check_isa_ill(s) &&
2725        require_align(a->rd, s->lmul)) {
2726        gen_set_rm(s, RISCV_FRM_DYN);
2727
2728        TCGv_i64 t1;
2729
2730        if (s->vl_eq_vlmax && !(s->vta && s->lmul < 0)) {
2731            t1 = tcg_temp_new_i64();
2732            /* NaN-box f[rs1] */
2733            do_nanbox(s, t1, cpu_fpr[a->rs1]);
2734
2735            tcg_gen_gvec_dup_i64(s->sew, vreg_ofs(s, a->rd),
2736                                 MAXSZ(s), MAXSZ(s), t1);
2737        } else {
2738            TCGv_ptr dest;
2739            TCGv_i32 desc;
2740            uint32_t data = FIELD_DP32(0, VDATA, LMUL, s->lmul);
2741            data = FIELD_DP32(data, VDATA, VTA, s->vta);
2742            data = FIELD_DP32(data, VDATA, VMA, s->vma);
2743            static gen_helper_vmv_vx * const fns[3] = {
2744                gen_helper_vmv_v_x_h,
2745                gen_helper_vmv_v_x_w,
2746                gen_helper_vmv_v_x_d,
2747            };
2748
2749            t1 = tcg_temp_new_i64();
2750            /* NaN-box f[rs1] */
2751            do_nanbox(s, t1, cpu_fpr[a->rs1]);
2752
2753            dest = tcg_temp_new_ptr();
2754            desc = tcg_constant_i32(simd_desc(s->cfg_ptr->vlenb,
2755                                              s->cfg_ptr->vlenb, data));
2756            tcg_gen_addi_ptr(dest, tcg_env, vreg_ofs(s, a->rd));
2757
2758            fns[s->sew - 1](dest, t1, tcg_env, desc);
2759        }
2760        finalize_rvv_inst(s);
2761        return true;
2762    }
2763    return false;
2764}
2765
2766/* Single-Width Floating-Point/Integer Type-Convert Instructions */
2767#define GEN_OPFV_CVT_TRANS(NAME, HELPER, FRM)               \
2768static bool trans_##NAME(DisasContext *s, arg_rmr *a)       \
2769{                                                           \
2770    static gen_helper_gvec_3_ptr * const fns[3] = {         \
2771        gen_helper_##HELPER##_h,                            \
2772        gen_helper_##HELPER##_w,                            \
2773        gen_helper_##HELPER##_d                             \
2774    };                                                      \
2775    return do_opfv(s, a, fns[s->sew - 1], opfv_check, FRM); \
2776}
2777
2778GEN_OPFV_CVT_TRANS(vfcvt_xu_f_v, vfcvt_xu_f_v, RISCV_FRM_DYN)
2779GEN_OPFV_CVT_TRANS(vfcvt_x_f_v, vfcvt_x_f_v, RISCV_FRM_DYN)
2780GEN_OPFV_CVT_TRANS(vfcvt_f_xu_v, vfcvt_f_xu_v, RISCV_FRM_DYN)
2781GEN_OPFV_CVT_TRANS(vfcvt_f_x_v, vfcvt_f_x_v, RISCV_FRM_DYN)
2782/* Reuse the helper functions from vfcvt.xu.f.v and vfcvt.x.f.v */
2783GEN_OPFV_CVT_TRANS(vfcvt_rtz_xu_f_v, vfcvt_xu_f_v, RISCV_FRM_RTZ)
2784GEN_OPFV_CVT_TRANS(vfcvt_rtz_x_f_v, vfcvt_x_f_v, RISCV_FRM_RTZ)
2785
2786/* Widening Floating-Point/Integer Type-Convert Instructions */
2787
2788/*
2789 * If the current SEW does not correspond to a supported IEEE floating-point
2790 * type, an illegal instruction exception is raised
2791 */
2792static bool opfv_widen_check(DisasContext *s, arg_rmr *a)
2793{
2794    return require_rvv(s) &&
2795           vext_check_isa_ill(s) &&
2796           vext_check_ds(s, a->rd, a->rs2, a->vm);
2797}
2798
2799static bool opxfv_widen_check(DisasContext *s, arg_rmr *a)
2800{
2801    return opfv_widen_check(s, a) &&
2802           require_rvf(s);
2803}
2804
2805static bool opffv_widen_check(DisasContext *s, arg_rmr *a)
2806{
2807    return opfv_widen_check(s, a) &&
2808           require_rvfmin(s) &&
2809           require_scale_rvfmin(s);
2810}
2811
2812#define GEN_OPFV_WIDEN_TRANS(NAME, CHECK, HELPER, FRM)             \
2813static bool trans_##NAME(DisasContext *s, arg_rmr *a)              \
2814{                                                                  \
2815    if (CHECK(s, a)) {                                             \
2816        uint32_t data = 0;                                         \
2817        static gen_helper_gvec_3_ptr * const fns[2] = {            \
2818            gen_helper_##HELPER##_h,                               \
2819            gen_helper_##HELPER##_w,                               \
2820        };                                                         \
2821        gen_set_rm_chkfrm(s, FRM);                                 \
2822                                                                   \
2823        data = FIELD_DP32(data, VDATA, VM, a->vm);                 \
2824        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);             \
2825        data = FIELD_DP32(data, VDATA, VTA, s->vta);               \
2826        data = FIELD_DP32(data, VDATA, VMA, s->vma);               \
2827        tcg_gen_gvec_3_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),     \
2828                           vreg_ofs(s, a->rs2), tcg_env,           \
2829                           s->cfg_ptr->vlenb,                      \
2830                           s->cfg_ptr->vlenb, data,                \
2831                           fns[s->sew - 1]);                       \
2832        finalize_rvv_inst(s);                                      \
2833        return true;                                               \
2834    }                                                              \
2835    return false;                                                  \
2836}
2837
2838GEN_OPFV_WIDEN_TRANS(vfwcvt_xu_f_v, opxfv_widen_check, vfwcvt_xu_f_v,
2839                     RISCV_FRM_DYN)
2840GEN_OPFV_WIDEN_TRANS(vfwcvt_x_f_v, opxfv_widen_check, vfwcvt_x_f_v,
2841                     RISCV_FRM_DYN)
2842GEN_OPFV_WIDEN_TRANS(vfwcvt_f_f_v, opffv_widen_check, vfwcvt_f_f_v,
2843                     RISCV_FRM_DYN)
2844/* Reuse the helper functions from vfwcvt.xu.f.v and vfwcvt.x.f.v */
2845GEN_OPFV_WIDEN_TRANS(vfwcvt_rtz_xu_f_v, opxfv_widen_check, vfwcvt_xu_f_v,
2846                     RISCV_FRM_RTZ)
2847GEN_OPFV_WIDEN_TRANS(vfwcvt_rtz_x_f_v, opxfv_widen_check, vfwcvt_x_f_v,
2848                     RISCV_FRM_RTZ)
2849
2850static bool opfxv_widen_check(DisasContext *s, arg_rmr *a)
2851{
2852    return require_rvv(s) &&
2853           require_scale_rvf(s) &&
2854           vext_check_isa_ill(s) &&
2855           /* OPFV widening instructions ignore vs1 check */
2856           vext_check_ds(s, a->rd, a->rs2, a->vm);
2857}
2858
2859#define GEN_OPFXV_WIDEN_TRANS(NAME)                                \
2860static bool trans_##NAME(DisasContext *s, arg_rmr *a)              \
2861{                                                                  \
2862    if (opfxv_widen_check(s, a)) {                                 \
2863        uint32_t data = 0;                                         \
2864        static gen_helper_gvec_3_ptr * const fns[3] = {            \
2865            gen_helper_##NAME##_b,                                 \
2866            gen_helper_##NAME##_h,                                 \
2867            gen_helper_##NAME##_w,                                 \
2868        };                                                         \
2869        gen_set_rm(s, RISCV_FRM_DYN);                              \
2870                                                                   \
2871        data = FIELD_DP32(data, VDATA, VM, a->vm);                 \
2872        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);             \
2873        data = FIELD_DP32(data, VDATA, VTA, s->vta);               \
2874        data = FIELD_DP32(data, VDATA, VMA, s->vma);               \
2875        tcg_gen_gvec_3_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),     \
2876                           vreg_ofs(s, a->rs2), tcg_env,           \
2877                           s->cfg_ptr->vlenb,                      \
2878                           s->cfg_ptr->vlenb, data,                \
2879                           fns[s->sew]);                           \
2880        finalize_rvv_inst(s);                                      \
2881        return true;                                               \
2882    }                                                              \
2883    return false;                                                  \
2884}
2885
2886GEN_OPFXV_WIDEN_TRANS(vfwcvt_f_xu_v)
2887GEN_OPFXV_WIDEN_TRANS(vfwcvt_f_x_v)
2888
2889/* Narrowing Floating-Point/Integer Type-Convert Instructions */
2890
2891/*
2892 * If the current SEW does not correspond to a supported IEEE floating-point
2893 * type, an illegal instruction exception is raised
2894 */
2895static bool opfv_narrow_check(DisasContext *s, arg_rmr *a)
2896{
2897    return require_rvv(s) &&
2898           vext_check_isa_ill(s) &&
2899           /* OPFV narrowing instructions ignore vs1 check */
2900           vext_check_sd(s, a->rd, a->rs2, a->vm);
2901}
2902
2903static bool opfxv_narrow_check(DisasContext *s, arg_rmr *a)
2904{
2905    return opfv_narrow_check(s, a) &&
2906           require_rvf(s) &&
2907           (s->sew != MO_64);
2908}
2909
2910static bool opffv_narrow_check(DisasContext *s, arg_rmr *a)
2911{
2912    return opfv_narrow_check(s, a) &&
2913           require_rvfmin(s) &&
2914           require_scale_rvfmin(s);
2915}
2916
2917static bool opffv_rod_narrow_check(DisasContext *s, arg_rmr *a)
2918{
2919    return opfv_narrow_check(s, a) &&
2920           require_rvf(s) &&
2921           require_scale_rvf(s);
2922}
2923
2924#define GEN_OPFV_NARROW_TRANS(NAME, CHECK, HELPER, FRM)            \
2925static bool trans_##NAME(DisasContext *s, arg_rmr *a)              \
2926{                                                                  \
2927    if (CHECK(s, a)) {                                             \
2928        uint32_t data = 0;                                         \
2929        static gen_helper_gvec_3_ptr * const fns[2] = {            \
2930            gen_helper_##HELPER##_h,                               \
2931            gen_helper_##HELPER##_w,                               \
2932        };                                                         \
2933        gen_set_rm_chkfrm(s, FRM);                                 \
2934                                                                   \
2935        data = FIELD_DP32(data, VDATA, VM, a->vm);                 \
2936        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);             \
2937        data = FIELD_DP32(data, VDATA, VTA, s->vta);               \
2938        data = FIELD_DP32(data, VDATA, VMA, s->vma);               \
2939        tcg_gen_gvec_3_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),     \
2940                           vreg_ofs(s, a->rs2), tcg_env,           \
2941                           s->cfg_ptr->vlenb,                      \
2942                           s->cfg_ptr->vlenb, data,                \
2943                           fns[s->sew - 1]);                       \
2944        finalize_rvv_inst(s);                                      \
2945        return true;                                               \
2946    }                                                              \
2947    return false;                                                  \
2948}
2949
2950GEN_OPFV_NARROW_TRANS(vfncvt_f_xu_w, opfxv_narrow_check, vfncvt_f_xu_w,
2951                      RISCV_FRM_DYN)
2952GEN_OPFV_NARROW_TRANS(vfncvt_f_x_w, opfxv_narrow_check, vfncvt_f_x_w,
2953                      RISCV_FRM_DYN)
2954GEN_OPFV_NARROW_TRANS(vfncvt_f_f_w, opffv_narrow_check, vfncvt_f_f_w,
2955                      RISCV_FRM_DYN)
2956/* Reuse the helper function from vfncvt.f.f.w */
2957GEN_OPFV_NARROW_TRANS(vfncvt_rod_f_f_w, opffv_rod_narrow_check, vfncvt_f_f_w,
2958                      RISCV_FRM_ROD)
2959
2960static bool opxfv_narrow_check(DisasContext *s, arg_rmr *a)
2961{
2962    return require_rvv(s) &&
2963           require_scale_rvf(s) &&
2964           vext_check_isa_ill(s) &&
2965           /* OPFV narrowing instructions ignore vs1 check */
2966           vext_check_sd(s, a->rd, a->rs2, a->vm);
2967}
2968
2969#define GEN_OPXFV_NARROW_TRANS(NAME, HELPER, FRM)                  \
2970static bool trans_##NAME(DisasContext *s, arg_rmr *a)              \
2971{                                                                  \
2972    if (opxfv_narrow_check(s, a)) {                                \
2973        uint32_t data = 0;                                         \
2974        static gen_helper_gvec_3_ptr * const fns[3] = {            \
2975            gen_helper_##HELPER##_b,                               \
2976            gen_helper_##HELPER##_h,                               \
2977            gen_helper_##HELPER##_w,                               \
2978        };                                                         \
2979        gen_set_rm_chkfrm(s, FRM);                                 \
2980                                                                   \
2981        data = FIELD_DP32(data, VDATA, VM, a->vm);                 \
2982        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);             \
2983        data = FIELD_DP32(data, VDATA, VTA, s->vta);               \
2984        data = FIELD_DP32(data, VDATA, VMA, s->vma);               \
2985        tcg_gen_gvec_3_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),     \
2986                           vreg_ofs(s, a->rs2), tcg_env,           \
2987                           s->cfg_ptr->vlenb,                      \
2988                           s->cfg_ptr->vlenb, data,                \
2989                           fns[s->sew]);                           \
2990        finalize_rvv_inst(s);                                      \
2991        return true;                                               \
2992    }                                                              \
2993    return false;                                                  \
2994}
2995
2996GEN_OPXFV_NARROW_TRANS(vfncvt_xu_f_w, vfncvt_xu_f_w, RISCV_FRM_DYN)
2997GEN_OPXFV_NARROW_TRANS(vfncvt_x_f_w, vfncvt_x_f_w, RISCV_FRM_DYN)
2998/* Reuse the helper functions from vfncvt.xu.f.w and vfncvt.x.f.w */
2999GEN_OPXFV_NARROW_TRANS(vfncvt_rtz_xu_f_w, vfncvt_xu_f_w, RISCV_FRM_RTZ)
3000GEN_OPXFV_NARROW_TRANS(vfncvt_rtz_x_f_w, vfncvt_x_f_w, RISCV_FRM_RTZ)
3001
3002/*
3003 *** Vector Reduction Operations
3004 */
3005/* Vector Single-Width Integer Reduction Instructions */
3006static bool reduction_check(DisasContext *s, arg_rmrr *a)
3007{
3008    return require_rvv(s) &&
3009           vext_check_isa_ill(s) &&
3010           vext_check_reduction(s, a->rs2);
3011}
3012
3013GEN_OPIVV_TRANS(vredsum_vs, reduction_check)
3014GEN_OPIVV_TRANS(vredmaxu_vs, reduction_check)
3015GEN_OPIVV_TRANS(vredmax_vs, reduction_check)
3016GEN_OPIVV_TRANS(vredminu_vs, reduction_check)
3017GEN_OPIVV_TRANS(vredmin_vs, reduction_check)
3018GEN_OPIVV_TRANS(vredand_vs, reduction_check)
3019GEN_OPIVV_TRANS(vredor_vs, reduction_check)
3020GEN_OPIVV_TRANS(vredxor_vs, reduction_check)
3021
3022/* Vector Widening Integer Reduction Instructions */
3023static bool reduction_widen_check(DisasContext *s, arg_rmrr *a)
3024{
3025    return reduction_check(s, a) && (s->sew < MO_64) &&
3026           ((s->sew + 1) <= (s->cfg_ptr->elen >> 4));
3027}
3028
3029GEN_OPIVV_WIDEN_TRANS(vwredsum_vs, reduction_widen_check)
3030GEN_OPIVV_WIDEN_TRANS(vwredsumu_vs, reduction_widen_check)
3031
3032/* Vector Single-Width Floating-Point Reduction Instructions */
3033static bool freduction_check(DisasContext *s, arg_rmrr *a)
3034{
3035    return reduction_check(s, a) &&
3036           require_rvf(s);
3037}
3038
3039GEN_OPFVV_TRANS(vfredusum_vs, freduction_check)
3040GEN_OPFVV_TRANS(vfredosum_vs, freduction_check)
3041GEN_OPFVV_TRANS(vfredmax_vs, freduction_check)
3042GEN_OPFVV_TRANS(vfredmin_vs, freduction_check)
3043
3044/* Vector Widening Floating-Point Reduction Instructions */
3045static bool freduction_widen_check(DisasContext *s, arg_rmrr *a)
3046{
3047    return reduction_widen_check(s, a) &&
3048           require_rvf(s) &&
3049           require_scale_rvf(s);
3050}
3051
3052GEN_OPFVV_WIDEN_TRANS(vfwredusum_vs, freduction_widen_check)
3053GEN_OPFVV_WIDEN_TRANS(vfwredosum_vs, freduction_widen_check)
3054
3055/*
3056 *** Vector Mask Operations
3057 */
3058
3059/* Vector Mask-Register Logical Instructions */
3060#define GEN_MM_TRANS(NAME)                                         \
3061static bool trans_##NAME(DisasContext *s, arg_r *a)                \
3062{                                                                  \
3063    if (require_rvv(s) &&                                          \
3064        vext_check_isa_ill(s)) {                                   \
3065        uint32_t data = 0;                                         \
3066        gen_helper_gvec_4_ptr *fn = gen_helper_##NAME;             \
3067                                                                   \
3068        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);             \
3069        data =                                                     \
3070            FIELD_DP32(data, VDATA, VTA_ALL_1S, s->cfg_vta_all_1s);\
3071        tcg_gen_gvec_4_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),     \
3072                           vreg_ofs(s, a->rs1),                    \
3073                           vreg_ofs(s, a->rs2), tcg_env,           \
3074                           s->cfg_ptr->vlenb,                      \
3075                           s->cfg_ptr->vlenb, data, fn);           \
3076        finalize_rvv_inst(s);                                      \
3077        return true;                                               \
3078    }                                                              \
3079    return false;                                                  \
3080}
3081
3082GEN_MM_TRANS(vmand_mm)
3083GEN_MM_TRANS(vmnand_mm)
3084GEN_MM_TRANS(vmandn_mm)
3085GEN_MM_TRANS(vmxor_mm)
3086GEN_MM_TRANS(vmor_mm)
3087GEN_MM_TRANS(vmnor_mm)
3088GEN_MM_TRANS(vmorn_mm)
3089GEN_MM_TRANS(vmxnor_mm)
3090
3091/* Vector count population in mask vcpop */
3092static bool trans_vcpop_m(DisasContext *s, arg_rmr *a)
3093{
3094    if (require_rvv(s) &&
3095        vext_check_isa_ill(s) &&
3096        s->vstart_eq_zero) {
3097        TCGv_ptr src2, mask;
3098        TCGv dst;
3099        TCGv_i32 desc;
3100        uint32_t data = 0;
3101        data = FIELD_DP32(data, VDATA, VM, a->vm);
3102        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);
3103
3104        mask = tcg_temp_new_ptr();
3105        src2 = tcg_temp_new_ptr();
3106        dst = dest_gpr(s, a->rd);
3107        desc = tcg_constant_i32(simd_desc(s->cfg_ptr->vlenb,
3108                                          s->cfg_ptr->vlenb, data));
3109
3110        tcg_gen_addi_ptr(src2, tcg_env, vreg_ofs(s, a->rs2));
3111        tcg_gen_addi_ptr(mask, tcg_env, vreg_ofs(s, 0));
3112
3113        gen_helper_vcpop_m(dst, mask, src2, tcg_env, desc);
3114        gen_set_gpr(s, a->rd, dst);
3115        return true;
3116    }
3117    return false;
3118}
3119
3120/* vmfirst find-first-set mask bit */
3121static bool trans_vfirst_m(DisasContext *s, arg_rmr *a)
3122{
3123    if (require_rvv(s) &&
3124        vext_check_isa_ill(s) &&
3125        s->vstart_eq_zero) {
3126        TCGv_ptr src2, mask;
3127        TCGv dst;
3128        TCGv_i32 desc;
3129        uint32_t data = 0;
3130        data = FIELD_DP32(data, VDATA, VM, a->vm);
3131        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);
3132
3133        mask = tcg_temp_new_ptr();
3134        src2 = tcg_temp_new_ptr();
3135        dst = dest_gpr(s, a->rd);
3136        desc = tcg_constant_i32(simd_desc(s->cfg_ptr->vlenb,
3137                                          s->cfg_ptr->vlenb, data));
3138
3139        tcg_gen_addi_ptr(src2, tcg_env, vreg_ofs(s, a->rs2));
3140        tcg_gen_addi_ptr(mask, tcg_env, vreg_ofs(s, 0));
3141
3142        gen_helper_vfirst_m(dst, mask, src2, tcg_env, desc);
3143        gen_set_gpr(s, a->rd, dst);
3144        return true;
3145    }
3146    return false;
3147}
3148
3149/*
3150 * vmsbf.m set-before-first mask bit
3151 * vmsif.m set-including-first mask bit
3152 * vmsof.m set-only-first mask bit
3153 */
3154#define GEN_M_TRANS(NAME)                                          \
3155static bool trans_##NAME(DisasContext *s, arg_rmr *a)              \
3156{                                                                  \
3157    if (require_rvv(s) &&                                          \
3158        vext_check_isa_ill(s) &&                                   \
3159        require_vm(a->vm, a->rd) &&                                \
3160        (a->rd != a->rs2) &&                                       \
3161        s->vstart_eq_zero) {                                       \
3162        uint32_t data = 0;                                         \
3163        gen_helper_gvec_3_ptr *fn = gen_helper_##NAME;             \
3164                                                                   \
3165        data = FIELD_DP32(data, VDATA, VM, a->vm);                 \
3166        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);             \
3167        data =                                                     \
3168            FIELD_DP32(data, VDATA, VTA_ALL_1S, s->cfg_vta_all_1s);\
3169        data = FIELD_DP32(data, VDATA, VMA, s->vma);               \
3170        tcg_gen_gvec_3_ptr(vreg_ofs(s, a->rd),                     \
3171                           vreg_ofs(s, 0), vreg_ofs(s, a->rs2),    \
3172                           tcg_env, s->cfg_ptr->vlenb,             \
3173                           s->cfg_ptr->vlenb,                      \
3174                           data, fn);                              \
3175        finalize_rvv_inst(s);                                      \
3176        return true;                                               \
3177    }                                                              \
3178    return false;                                                  \
3179}
3180
3181GEN_M_TRANS(vmsbf_m)
3182GEN_M_TRANS(vmsif_m)
3183GEN_M_TRANS(vmsof_m)
3184
3185/*
3186 * Vector Iota Instruction
3187 *
3188 * 1. The destination register cannot overlap the source register.
3189 * 2. If masked, cannot overlap the mask register ('v0').
3190 * 3. An illegal instruction exception is raised if vstart is non-zero.
3191 */
3192static bool trans_viota_m(DisasContext *s, arg_viota_m *a)
3193{
3194    if (require_rvv(s) &&
3195        vext_check_isa_ill(s) &&
3196        !is_overlapped(a->rd, 1 << MAX(s->lmul, 0), a->rs2, 1) &&
3197        require_vm(a->vm, a->rd) &&
3198        require_align(a->rd, s->lmul) &&
3199        s->vstart_eq_zero) {
3200        uint32_t data = 0;
3201
3202        data = FIELD_DP32(data, VDATA, VM, a->vm);
3203        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);
3204        data = FIELD_DP32(data, VDATA, VTA, s->vta);
3205        data = FIELD_DP32(data, VDATA, VMA, s->vma);
3206        static gen_helper_gvec_3_ptr * const fns[4] = {
3207            gen_helper_viota_m_b, gen_helper_viota_m_h,
3208            gen_helper_viota_m_w, gen_helper_viota_m_d,
3209        };
3210        tcg_gen_gvec_3_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),
3211                           vreg_ofs(s, a->rs2), tcg_env,
3212                           s->cfg_ptr->vlenb,
3213                           s->cfg_ptr->vlenb, data, fns[s->sew]);
3214        finalize_rvv_inst(s);
3215        return true;
3216    }
3217    return false;
3218}
3219
3220/* Vector Element Index Instruction */
3221static bool trans_vid_v(DisasContext *s, arg_vid_v *a)
3222{
3223    if (require_rvv(s) &&
3224        vext_check_isa_ill(s) &&
3225        require_align(a->rd, s->lmul) &&
3226        require_vm(a->vm, a->rd)) {
3227        uint32_t data = 0;
3228
3229        data = FIELD_DP32(data, VDATA, VM, a->vm);
3230        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);
3231        data = FIELD_DP32(data, VDATA, VTA, s->vta);
3232        data = FIELD_DP32(data, VDATA, VMA, s->vma);
3233        static gen_helper_gvec_2_ptr * const fns[4] = {
3234            gen_helper_vid_v_b, gen_helper_vid_v_h,
3235            gen_helper_vid_v_w, gen_helper_vid_v_d,
3236        };
3237        tcg_gen_gvec_2_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),
3238                           tcg_env, s->cfg_ptr->vlenb,
3239                           s->cfg_ptr->vlenb,
3240                           data, fns[s->sew]);
3241        finalize_rvv_inst(s);
3242        return true;
3243    }
3244    return false;
3245}
3246
3247/*
3248 *** Vector Permutation Instructions
3249 */
3250
3251static void load_element(TCGv_i64 dest, TCGv_ptr base,
3252                         int ofs, int sew, bool sign)
3253{
3254    switch (sew) {
3255    case MO_8:
3256        if (!sign) {
3257            tcg_gen_ld8u_i64(dest, base, ofs);
3258        } else {
3259            tcg_gen_ld8s_i64(dest, base, ofs);
3260        }
3261        break;
3262    case MO_16:
3263        if (!sign) {
3264            tcg_gen_ld16u_i64(dest, base, ofs);
3265        } else {
3266            tcg_gen_ld16s_i64(dest, base, ofs);
3267        }
3268        break;
3269    case MO_32:
3270        if (!sign) {
3271            tcg_gen_ld32u_i64(dest, base, ofs);
3272        } else {
3273            tcg_gen_ld32s_i64(dest, base, ofs);
3274        }
3275        break;
3276    case MO_64:
3277        tcg_gen_ld_i64(dest, base, ofs);
3278        break;
3279    default:
3280        g_assert_not_reached();
3281    }
3282}
3283
3284/* offset of the idx element with base register r */
3285static uint32_t endian_ofs(DisasContext *s, int r, int idx)
3286{
3287#if HOST_BIG_ENDIAN
3288    return vreg_ofs(s, r) + ((idx ^ (7 >> s->sew)) << s->sew);
3289#else
3290    return vreg_ofs(s, r) + (idx << s->sew);
3291#endif
3292}
3293
3294/* adjust the index according to the endian */
3295static void endian_adjust(TCGv_i32 ofs, int sew)
3296{
3297#if HOST_BIG_ENDIAN
3298    tcg_gen_xori_i32(ofs, ofs, 7 >> sew);
3299#endif
3300}
3301
3302/* Load idx >= VLMAX ? 0 : vreg[idx] */
3303static void vec_element_loadx(DisasContext *s, TCGv_i64 dest,
3304                              int vreg, TCGv idx, int vlmax)
3305{
3306    TCGv_i32 ofs = tcg_temp_new_i32();
3307    TCGv_ptr base = tcg_temp_new_ptr();
3308    TCGv_i64 t_idx = tcg_temp_new_i64();
3309    TCGv_i64 t_vlmax, t_zero;
3310
3311    /*
3312     * Mask the index to the length so that we do
3313     * not produce an out-of-range load.
3314     */
3315    tcg_gen_trunc_tl_i32(ofs, idx);
3316    tcg_gen_andi_i32(ofs, ofs, vlmax - 1);
3317
3318    /* Convert the index to an offset. */
3319    endian_adjust(ofs, s->sew);
3320    tcg_gen_shli_i32(ofs, ofs, s->sew);
3321
3322    /* Convert the index to a pointer. */
3323    tcg_gen_ext_i32_ptr(base, ofs);
3324    tcg_gen_add_ptr(base, base, tcg_env);
3325
3326    /* Perform the load. */
3327    load_element(dest, base,
3328                 vreg_ofs(s, vreg), s->sew, false);
3329
3330    /* Flush out-of-range indexing to zero.  */
3331    t_vlmax = tcg_constant_i64(vlmax);
3332    t_zero = tcg_constant_i64(0);
3333    tcg_gen_extu_tl_i64(t_idx, idx);
3334
3335    tcg_gen_movcond_i64(TCG_COND_LTU, dest, t_idx,
3336                        t_vlmax, dest, t_zero);
3337}
3338
3339static void vec_element_loadi(DisasContext *s, TCGv_i64 dest,
3340                              int vreg, int idx, bool sign)
3341{
3342    load_element(dest, tcg_env, endian_ofs(s, vreg, idx), s->sew, sign);
3343}
3344
3345/* Integer Scalar Move Instruction */
3346
3347static void store_element(TCGv_i64 val, TCGv_ptr base,
3348                          int ofs, int sew)
3349{
3350    switch (sew) {
3351    case MO_8:
3352        tcg_gen_st8_i64(val, base, ofs);
3353        break;
3354    case MO_16:
3355        tcg_gen_st16_i64(val, base, ofs);
3356        break;
3357    case MO_32:
3358        tcg_gen_st32_i64(val, base, ofs);
3359        break;
3360    case MO_64:
3361        tcg_gen_st_i64(val, base, ofs);
3362        break;
3363    default:
3364        g_assert_not_reached();
3365    }
3366}
3367
3368/*
3369 * Store vreg[idx] = val.
3370 * The index must be in range of VLMAX.
3371 */
3372static void vec_element_storei(DisasContext *s, int vreg,
3373                               int idx, TCGv_i64 val)
3374{
3375    store_element(val, tcg_env, endian_ofs(s, vreg, idx), s->sew);
3376}
3377
3378/* vmv.x.s rd, vs2 # x[rd] = vs2[0] */
3379static bool trans_vmv_x_s(DisasContext *s, arg_vmv_x_s *a)
3380{
3381    if (require_rvv(s) &&
3382        vext_check_isa_ill(s)) {
3383        TCGv_i64 t1;
3384        TCGv dest;
3385
3386        t1 = tcg_temp_new_i64();
3387        dest = tcg_temp_new();
3388        /*
3389         * load vreg and sign-extend to 64 bits,
3390         * then truncate to XLEN bits before storing to gpr.
3391         */
3392        vec_element_loadi(s, t1, a->rs2, 0, true);
3393        tcg_gen_trunc_i64_tl(dest, t1);
3394        gen_set_gpr(s, a->rd, dest);
3395        tcg_gen_movi_tl(cpu_vstart, 0);
3396        finalize_rvv_inst(s);
3397        return true;
3398    }
3399    return false;
3400}
3401
3402/* vmv.s.x vd, rs1 # vd[0] = rs1 */
3403static bool trans_vmv_s_x(DisasContext *s, arg_vmv_s_x *a)
3404{
3405    if (require_rvv(s) &&
3406        vext_check_isa_ill(s)) {
3407        /* This instruction ignores LMUL and vector register groups */
3408        TCGv_i64 t1;
3409        TCGv s1;
3410        TCGLabel *over = gen_new_label();
3411
3412        tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);
3413
3414        t1 = tcg_temp_new_i64();
3415
3416        /*
3417         * load gpr and sign-extend to 64 bits,
3418         * then truncate to SEW bits when storing to vreg.
3419         */
3420        s1 = get_gpr(s, a->rs1, EXT_NONE);
3421        tcg_gen_ext_tl_i64(t1, s1);
3422        vec_element_storei(s, a->rd, 0, t1);
3423        gen_set_label(over);
3424        tcg_gen_movi_tl(cpu_vstart, 0);
3425        finalize_rvv_inst(s);
3426        return true;
3427    }
3428    return false;
3429}
3430
3431/* Floating-Point Scalar Move Instructions */
3432static bool trans_vfmv_f_s(DisasContext *s, arg_vfmv_f_s *a)
3433{
3434    if (require_rvv(s) &&
3435        require_rvf(s) &&
3436        vext_check_isa_ill(s)) {
3437        gen_set_rm(s, RISCV_FRM_DYN);
3438
3439        unsigned int ofs = (8 << s->sew);
3440        unsigned int len = 64 - ofs;
3441        TCGv_i64 t_nan;
3442
3443        vec_element_loadi(s, cpu_fpr[a->rd], a->rs2, 0, false);
3444        /* NaN-box f[rd] as necessary for SEW */
3445        if (len) {
3446            t_nan = tcg_constant_i64(UINT64_MAX);
3447            tcg_gen_deposit_i64(cpu_fpr[a->rd], cpu_fpr[a->rd],
3448                                t_nan, ofs, len);
3449        }
3450
3451        mark_fs_dirty(s);
3452        tcg_gen_movi_tl(cpu_vstart, 0);
3453        finalize_rvv_inst(s);
3454        return true;
3455    }
3456    return false;
3457}
3458
3459/* vfmv.s.f vd, rs1 # vd[0] = rs1 (vs2=0) */
3460static bool trans_vfmv_s_f(DisasContext *s, arg_vfmv_s_f *a)
3461{
3462    if (require_rvv(s) &&
3463        require_rvf(s) &&
3464        vext_check_isa_ill(s)) {
3465        gen_set_rm(s, RISCV_FRM_DYN);
3466
3467        /* The instructions ignore LMUL and vector register group. */
3468        TCGv_i64 t1;
3469        TCGLabel *over = gen_new_label();
3470
3471        /* if vstart >= vl, skip vector register write back */
3472        tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);
3473
3474        /* NaN-box f[rs1] */
3475        t1 = tcg_temp_new_i64();
3476        do_nanbox(s, t1, cpu_fpr[a->rs1]);
3477
3478        vec_element_storei(s, a->rd, 0, t1);
3479
3480        gen_set_label(over);
3481        tcg_gen_movi_tl(cpu_vstart, 0);
3482        finalize_rvv_inst(s);
3483        return true;
3484    }
3485    return false;
3486}
3487
3488/* Vector Slide Instructions */
3489static bool slideup_check(DisasContext *s, arg_rmrr *a)
3490{
3491    return require_rvv(s) &&
3492           vext_check_isa_ill(s) &&
3493           vext_check_slide(s, a->rd, a->rs2, a->vm, true);
3494}
3495
3496GEN_OPIVX_TRANS(vslideup_vx, slideup_check)
3497GEN_OPIVX_TRANS(vslide1up_vx, slideup_check)
3498GEN_OPIVI_TRANS(vslideup_vi, IMM_ZX, vslideup_vx, slideup_check)
3499
3500static bool slidedown_check(DisasContext *s, arg_rmrr *a)
3501{
3502    return require_rvv(s) &&
3503           vext_check_isa_ill(s) &&
3504           vext_check_slide(s, a->rd, a->rs2, a->vm, false);
3505}
3506
3507GEN_OPIVX_TRANS(vslidedown_vx, slidedown_check)
3508GEN_OPIVX_TRANS(vslide1down_vx, slidedown_check)
3509GEN_OPIVI_TRANS(vslidedown_vi, IMM_ZX, vslidedown_vx, slidedown_check)
3510
3511/* Vector Floating-Point Slide Instructions */
3512static bool fslideup_check(DisasContext *s, arg_rmrr *a)
3513{
3514    return slideup_check(s, a) &&
3515           require_rvf(s);
3516}
3517
3518static bool fslidedown_check(DisasContext *s, arg_rmrr *a)
3519{
3520    return slidedown_check(s, a) &&
3521           require_rvf(s);
3522}
3523
3524GEN_OPFVF_TRANS(vfslide1up_vf, fslideup_check)
3525GEN_OPFVF_TRANS(vfslide1down_vf, fslidedown_check)
3526
3527/* Vector Register Gather Instruction */
3528static bool vrgather_vv_check(DisasContext *s, arg_rmrr *a)
3529{
3530    return require_rvv(s) &&
3531           vext_check_isa_ill(s) &&
3532           vext_check_input_eew(s, a->rs1, s->sew, a->rs2, s->sew, a->vm) &&
3533           require_align(a->rd, s->lmul) &&
3534           require_align(a->rs1, s->lmul) &&
3535           require_align(a->rs2, s->lmul) &&
3536           (a->rd != a->rs2 && a->rd != a->rs1) &&
3537           require_vm(a->vm, a->rd);
3538}
3539
3540static bool vrgatherei16_vv_check(DisasContext *s, arg_rmrr *a)
3541{
3542    int8_t emul = MO_16 - s->sew + s->lmul;
3543    return require_rvv(s) &&
3544           vext_check_isa_ill(s) &&
3545           vext_check_input_eew(s, a->rs1, MO_16, a->rs2, s->sew, a->vm) &&
3546           (emul >= -3 && emul <= 3) &&
3547           require_align(a->rd, s->lmul) &&
3548           require_align(a->rs1, emul) &&
3549           require_align(a->rs2, s->lmul) &&
3550           (a->rd != a->rs2 && a->rd != a->rs1) &&
3551           !is_overlapped(a->rd, 1 << MAX(s->lmul, 0),
3552                          a->rs1, 1 << MAX(emul, 0)) &&
3553           !is_overlapped(a->rd, 1 << MAX(s->lmul, 0),
3554                          a->rs2, 1 << MAX(s->lmul, 0)) &&
3555           require_vm(a->vm, a->rd);
3556}
3557
3558GEN_OPIVV_TRANS(vrgather_vv, vrgather_vv_check)
3559GEN_OPIVV_TRANS(vrgatherei16_vv, vrgatherei16_vv_check)
3560
3561static bool vrgather_vx_check(DisasContext *s, arg_rmrr *a)
3562{
3563    return require_rvv(s) &&
3564           vext_check_isa_ill(s) &&
3565           vext_check_input_eew(s, -1, MO_64, a->rs2, s->sew, a->vm) &&
3566           require_align(a->rd, s->lmul) &&
3567           require_align(a->rs2, s->lmul) &&
3568           (a->rd != a->rs2) &&
3569           require_vm(a->vm, a->rd);
3570}
3571
3572/* vrgather.vx vd, vs2, rs1, vm # vd[i] = (x[rs1] >= VLMAX) ? 0 : vs2[rs1] */
3573static bool trans_vrgather_vx(DisasContext *s, arg_rmrr *a)
3574{
3575    if (!vrgather_vx_check(s, a)) {
3576        return false;
3577    }
3578
3579    if (a->vm && s->vl_eq_vlmax && !(s->vta && s->lmul < 0)) {
3580        int vlmax = vext_get_vlmax(s->cfg_ptr->vlenb, s->sew, s->lmul);
3581        TCGv_i64 dest = tcg_temp_new_i64();
3582
3583        if (a->rs1 == 0) {
3584            vec_element_loadi(s, dest, a->rs2, 0, false);
3585        } else {
3586            vec_element_loadx(s, dest, a->rs2, cpu_gpr[a->rs1], vlmax);
3587        }
3588
3589        tcg_gen_gvec_dup_i64(s->sew, vreg_ofs(s, a->rd),
3590                             MAXSZ(s), MAXSZ(s), dest);
3591        finalize_rvv_inst(s);
3592    } else {
3593        static gen_helper_opivx * const fns[4] = {
3594            gen_helper_vrgather_vx_b, gen_helper_vrgather_vx_h,
3595            gen_helper_vrgather_vx_w, gen_helper_vrgather_vx_d
3596        };
3597        return opivx_trans(a->rd, a->rs1, a->rs2, a->vm, fns[s->sew], s);
3598    }
3599    return true;
3600}
3601
3602/* vrgather.vi vd, vs2, imm, vm # vd[i] = (imm >= VLMAX) ? 0 : vs2[imm] */
3603static bool trans_vrgather_vi(DisasContext *s, arg_rmrr *a)
3604{
3605    if (!vrgather_vx_check(s, a)) {
3606        return false;
3607    }
3608
3609    if (a->vm && s->vl_eq_vlmax && !(s->vta && s->lmul < 0)) {
3610        int vlmax = vext_get_vlmax(s->cfg_ptr->vlenb, s->sew, s->lmul);
3611        if (a->rs1 >= vlmax) {
3612            tcg_gen_gvec_dup_imm(MO_64, vreg_ofs(s, a->rd),
3613                                 MAXSZ(s), MAXSZ(s), 0);
3614        } else {
3615            tcg_gen_gvec_dup_mem(s->sew, vreg_ofs(s, a->rd),
3616                                 endian_ofs(s, a->rs2, a->rs1),
3617                                 MAXSZ(s), MAXSZ(s));
3618        }
3619        finalize_rvv_inst(s);
3620    } else {
3621        static gen_helper_opivx * const fns[4] = {
3622            gen_helper_vrgather_vx_b, gen_helper_vrgather_vx_h,
3623            gen_helper_vrgather_vx_w, gen_helper_vrgather_vx_d
3624        };
3625        return opivi_trans(a->rd, a->rs1, a->rs2, a->vm, fns[s->sew],
3626                           s, IMM_ZX);
3627    }
3628    return true;
3629}
3630
3631/*
3632 * Vector Compress Instruction
3633 *
3634 * The destination vector register group cannot overlap the
3635 * source vector register group or the source mask register.
3636 */
3637static bool vcompress_vm_check(DisasContext *s, arg_r *a)
3638{
3639    return require_rvv(s) &&
3640           vext_check_isa_ill(s) &&
3641           require_align(a->rd, s->lmul) &&
3642           require_align(a->rs2, s->lmul) &&
3643           (a->rd != a->rs2) &&
3644           !is_overlapped(a->rd, 1 << MAX(s->lmul, 0), a->rs1, 1) &&
3645           s->vstart_eq_zero;
3646}
3647
3648static bool trans_vcompress_vm(DisasContext *s, arg_r *a)
3649{
3650    if (vcompress_vm_check(s, a)) {
3651        uint32_t data = 0;
3652        static gen_helper_gvec_4_ptr * const fns[4] = {
3653            gen_helper_vcompress_vm_b, gen_helper_vcompress_vm_h,
3654            gen_helper_vcompress_vm_w, gen_helper_vcompress_vm_d,
3655        };
3656
3657        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);
3658        data = FIELD_DP32(data, VDATA, VTA, s->vta);
3659        tcg_gen_gvec_4_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),
3660                           vreg_ofs(s, a->rs1), vreg_ofs(s, a->rs2),
3661                           tcg_env, s->cfg_ptr->vlenb,
3662                           s->cfg_ptr->vlenb, data,
3663                           fns[s->sew]);
3664        finalize_rvv_inst(s);
3665        return true;
3666    }
3667    return false;
3668}
3669
3670/*
3671 * Whole Vector Register Move Instructions depend on vtype register(vsew).
3672 * Thus, we need to check vill bit. (Section 16.6)
3673 */
3674#define GEN_VMV_WHOLE_TRANS(NAME, LEN)                             \
3675static bool trans_##NAME(DisasContext *s, arg_##NAME * a)               \
3676{                                                                       \
3677    if (require_rvv(s) &&                                               \
3678        vext_check_isa_ill(s) &&                                        \
3679        QEMU_IS_ALIGNED(a->rd, LEN) &&                                  \
3680        QEMU_IS_ALIGNED(a->rs2, LEN)) {                                 \
3681        uint32_t maxsz = s->cfg_ptr->vlenb * LEN;                       \
3682        if (s->vstart_eq_zero) {                                        \
3683            tcg_gen_gvec_mov(s->sew, vreg_ofs(s, a->rd),                \
3684                             vreg_ofs(s, a->rs2), maxsz, maxsz);        \
3685        } else {                                                        \
3686            tcg_gen_gvec_2_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, a->rs2), \
3687                               tcg_env, maxsz, maxsz, 0, gen_helper_vmvr_v); \
3688        }                                                               \
3689        finalize_rvv_inst(s);                                           \
3690        return true;                                                    \
3691    }                                                                   \
3692    return false;                                                       \
3693}
3694
3695GEN_VMV_WHOLE_TRANS(vmv1r_v, 1)
3696GEN_VMV_WHOLE_TRANS(vmv2r_v, 2)
3697GEN_VMV_WHOLE_TRANS(vmv4r_v, 4)
3698GEN_VMV_WHOLE_TRANS(vmv8r_v, 8)
3699
3700static bool int_ext_check(DisasContext *s, arg_rmr *a, uint8_t div)
3701{
3702    uint8_t from = (s->sew + 3) - div;
3703    bool ret = require_rvv(s) &&
3704        (from >= 3 && from <= 8) &&
3705        (a->rd != a->rs2) &&
3706        require_align(a->rd, s->lmul) &&
3707        require_align(a->rs2, s->lmul - div) &&
3708        require_vm(a->vm, a->rd) &&
3709        require_noover(a->rd, s->lmul, a->rs2, s->lmul - div) &&
3710        vext_check_input_eew(s, -1, 0, a->rs2, s->sew, a->vm);
3711
3712    return ret;
3713}
3714
3715static bool int_ext_op(DisasContext *s, arg_rmr *a, uint8_t seq)
3716{
3717    uint32_t data = 0;
3718    gen_helper_gvec_3_ptr *fn;
3719
3720    static gen_helper_gvec_3_ptr * const fns[6][4] = {
3721        {
3722            NULL, gen_helper_vzext_vf2_h,
3723            gen_helper_vzext_vf2_w, gen_helper_vzext_vf2_d
3724        },
3725        {
3726            NULL, NULL,
3727            gen_helper_vzext_vf4_w, gen_helper_vzext_vf4_d,
3728        },
3729        {
3730            NULL, NULL,
3731            NULL, gen_helper_vzext_vf8_d
3732        },
3733        {
3734            NULL, gen_helper_vsext_vf2_h,
3735            gen_helper_vsext_vf2_w, gen_helper_vsext_vf2_d
3736        },
3737        {
3738            NULL, NULL,
3739            gen_helper_vsext_vf4_w, gen_helper_vsext_vf4_d,
3740        },
3741        {
3742            NULL, NULL,
3743            NULL, gen_helper_vsext_vf8_d
3744        }
3745    };
3746
3747    fn = fns[seq][s->sew];
3748    if (fn == NULL) {
3749        return false;
3750    }
3751
3752    data = FIELD_DP32(data, VDATA, VM, a->vm);
3753    data = FIELD_DP32(data, VDATA, LMUL, s->lmul);
3754    data = FIELD_DP32(data, VDATA, VTA, s->vta);
3755    data = FIELD_DP32(data, VDATA, VMA, s->vma);
3756
3757    tcg_gen_gvec_3_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),
3758                       vreg_ofs(s, a->rs2), tcg_env,
3759                       s->cfg_ptr->vlenb,
3760                       s->cfg_ptr->vlenb, data, fn);
3761
3762    finalize_rvv_inst(s);
3763    return true;
3764}
3765
3766/* Vector Integer Extension */
3767#define GEN_INT_EXT_TRANS(NAME, DIV, SEQ)             \
3768static bool trans_##NAME(DisasContext *s, arg_rmr *a) \
3769{                                                     \
3770    if (int_ext_check(s, a, DIV)) {                   \
3771        return int_ext_op(s, a, SEQ);                 \
3772    }                                                 \
3773    return false;                                     \
3774}
3775
3776GEN_INT_EXT_TRANS(vzext_vf2, 1, 0)
3777GEN_INT_EXT_TRANS(vzext_vf4, 2, 1)
3778GEN_INT_EXT_TRANS(vzext_vf8, 3, 2)
3779GEN_INT_EXT_TRANS(vsext_vf2, 1, 3)
3780GEN_INT_EXT_TRANS(vsext_vf4, 2, 4)
3781GEN_INT_EXT_TRANS(vsext_vf8, 3, 5)
3782