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