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