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