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        data = FIELD_DP32(data, VDATA, VMA, s->vma);
1251        tcg_gen_gvec_4_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),
1252                           vreg_ofs(s, a->rs1), vreg_ofs(s, a->rs2),
1253                           cpu_env, s->cfg_ptr->vlen / 8,
1254                           s->cfg_ptr->vlen / 8, data, fn);
1255    }
1256    mark_vs_dirty(s);
1257    gen_set_label(over);
1258    return true;
1259}
1260
1261/* OPIVV with GVEC IR */
1262#define GEN_OPIVV_GVEC_TRANS(NAME, SUF) \
1263static bool trans_##NAME(DisasContext *s, arg_rmrr *a)             \
1264{                                                                  \
1265    static gen_helper_gvec_4_ptr * const fns[4] = {                \
1266        gen_helper_##NAME##_b, gen_helper_##NAME##_h,              \
1267        gen_helper_##NAME##_w, gen_helper_##NAME##_d,              \
1268    };                                                             \
1269    return do_opivv_gvec(s, a, tcg_gen_gvec_##SUF, fns[s->sew]);   \
1270}
1271
1272GEN_OPIVV_GVEC_TRANS(vadd_vv, add)
1273GEN_OPIVV_GVEC_TRANS(vsub_vv, sub)
1274
1275typedef void gen_helper_opivx(TCGv_ptr, TCGv_ptr, TCGv, TCGv_ptr,
1276                              TCGv_env, TCGv_i32);
1277
1278static bool opivx_trans(uint32_t vd, uint32_t rs1, uint32_t vs2, uint32_t vm,
1279                        gen_helper_opivx *fn, DisasContext *s)
1280{
1281    TCGv_ptr dest, src2, mask;
1282    TCGv src1;
1283    TCGv_i32 desc;
1284    uint32_t data = 0;
1285
1286    TCGLabel *over = gen_new_label();
1287    tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_vl, 0, over);
1288    tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);
1289
1290    dest = tcg_temp_new_ptr();
1291    mask = tcg_temp_new_ptr();
1292    src2 = tcg_temp_new_ptr();
1293    src1 = get_gpr(s, rs1, EXT_SIGN);
1294
1295    data = FIELD_DP32(data, VDATA, VM, vm);
1296    data = FIELD_DP32(data, VDATA, LMUL, s->lmul);
1297    data = FIELD_DP32(data, VDATA, VTA, s->vta);
1298    data = FIELD_DP32(data, VDATA, VTA_ALL_1S, s->cfg_vta_all_1s);
1299    desc = tcg_constant_i32(simd_desc(s->cfg_ptr->vlen / 8,
1300                                      s->cfg_ptr->vlen / 8, data));
1301
1302    tcg_gen_addi_ptr(dest, cpu_env, vreg_ofs(s, vd));
1303    tcg_gen_addi_ptr(src2, cpu_env, vreg_ofs(s, vs2));
1304    tcg_gen_addi_ptr(mask, cpu_env, vreg_ofs(s, 0));
1305
1306    fn(dest, mask, src1, src2, cpu_env, desc);
1307
1308    tcg_temp_free_ptr(dest);
1309    tcg_temp_free_ptr(mask);
1310    tcg_temp_free_ptr(src2);
1311    mark_vs_dirty(s);
1312    gen_set_label(over);
1313    return true;
1314}
1315
1316static bool opivx_check(DisasContext *s, arg_rmrr *a)
1317{
1318    return require_rvv(s) &&
1319           vext_check_isa_ill(s) &&
1320           vext_check_ss(s, a->rd, a->rs2, a->vm);
1321}
1322
1323typedef void GVecGen2sFn(unsigned, uint32_t, uint32_t, TCGv_i64,
1324                         uint32_t, uint32_t);
1325
1326static inline bool
1327do_opivx_gvec(DisasContext *s, arg_rmrr *a, GVecGen2sFn *gvec_fn,
1328              gen_helper_opivx *fn)
1329{
1330    if (!opivx_check(s, a)) {
1331        return false;
1332    }
1333
1334    if (a->vm && s->vl_eq_vlmax && !(s->vta && s->lmul < 0)) {
1335        TCGv_i64 src1 = tcg_temp_new_i64();
1336
1337        tcg_gen_ext_tl_i64(src1, get_gpr(s, a->rs1, EXT_SIGN));
1338        gvec_fn(s->sew, vreg_ofs(s, a->rd), vreg_ofs(s, a->rs2),
1339                src1, MAXSZ(s), MAXSZ(s));
1340
1341        tcg_temp_free_i64(src1);
1342        mark_vs_dirty(s);
1343        return true;
1344    }
1345    return opivx_trans(a->rd, a->rs1, a->rs2, a->vm, fn, s);
1346}
1347
1348/* OPIVX with GVEC IR */
1349#define GEN_OPIVX_GVEC_TRANS(NAME, SUF) \
1350static bool trans_##NAME(DisasContext *s, arg_rmrr *a)             \
1351{                                                                  \
1352    static gen_helper_opivx * const fns[4] = {                     \
1353        gen_helper_##NAME##_b, gen_helper_##NAME##_h,              \
1354        gen_helper_##NAME##_w, gen_helper_##NAME##_d,              \
1355    };                                                             \
1356    return do_opivx_gvec(s, a, tcg_gen_gvec_##SUF, fns[s->sew]);   \
1357}
1358
1359GEN_OPIVX_GVEC_TRANS(vadd_vx, adds)
1360GEN_OPIVX_GVEC_TRANS(vsub_vx, subs)
1361
1362static void gen_vec_rsub8_i64(TCGv_i64 d, TCGv_i64 a, TCGv_i64 b)
1363{
1364    tcg_gen_vec_sub8_i64(d, b, a);
1365}
1366
1367static void gen_vec_rsub16_i64(TCGv_i64 d, TCGv_i64 a, TCGv_i64 b)
1368{
1369    tcg_gen_vec_sub16_i64(d, b, a);
1370}
1371
1372static void gen_rsub_i32(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
1373{
1374    tcg_gen_sub_i32(ret, arg2, arg1);
1375}
1376
1377static void gen_rsub_i64(TCGv_i64 ret, TCGv_i64 arg1, TCGv_i64 arg2)
1378{
1379    tcg_gen_sub_i64(ret, arg2, arg1);
1380}
1381
1382static void gen_rsub_vec(unsigned vece, TCGv_vec r, TCGv_vec a, TCGv_vec b)
1383{
1384    tcg_gen_sub_vec(vece, r, b, a);
1385}
1386
1387static void tcg_gen_gvec_rsubs(unsigned vece, uint32_t dofs, uint32_t aofs,
1388                               TCGv_i64 c, uint32_t oprsz, uint32_t maxsz)
1389{
1390    static const TCGOpcode vecop_list[] = { INDEX_op_sub_vec, 0 };
1391    static const GVecGen2s rsub_op[4] = {
1392        { .fni8 = gen_vec_rsub8_i64,
1393          .fniv = gen_rsub_vec,
1394          .fno = gen_helper_vec_rsubs8,
1395          .opt_opc = vecop_list,
1396          .vece = MO_8 },
1397        { .fni8 = gen_vec_rsub16_i64,
1398          .fniv = gen_rsub_vec,
1399          .fno = gen_helper_vec_rsubs16,
1400          .opt_opc = vecop_list,
1401          .vece = MO_16 },
1402        { .fni4 = gen_rsub_i32,
1403          .fniv = gen_rsub_vec,
1404          .fno = gen_helper_vec_rsubs32,
1405          .opt_opc = vecop_list,
1406          .vece = MO_32 },
1407        { .fni8 = gen_rsub_i64,
1408          .fniv = gen_rsub_vec,
1409          .fno = gen_helper_vec_rsubs64,
1410          .opt_opc = vecop_list,
1411          .prefer_i64 = TCG_TARGET_REG_BITS == 64,
1412          .vece = MO_64 },
1413    };
1414
1415    tcg_debug_assert(vece <= MO_64);
1416    tcg_gen_gvec_2s(dofs, aofs, oprsz, maxsz, c, &rsub_op[vece]);
1417}
1418
1419GEN_OPIVX_GVEC_TRANS(vrsub_vx, rsubs)
1420
1421typedef enum {
1422    IMM_ZX,         /* Zero-extended */
1423    IMM_SX,         /* Sign-extended */
1424    IMM_TRUNC_SEW,  /* Truncate to log(SEW) bits */
1425    IMM_TRUNC_2SEW, /* Truncate to log(2*SEW) bits */
1426} imm_mode_t;
1427
1428static int64_t extract_imm(DisasContext *s, uint32_t imm, imm_mode_t imm_mode)
1429{
1430    switch (imm_mode) {
1431    case IMM_ZX:
1432        return extract64(imm, 0, 5);
1433    case IMM_SX:
1434        return sextract64(imm, 0, 5);
1435    case IMM_TRUNC_SEW:
1436        return extract64(imm, 0, s->sew + 3);
1437    case IMM_TRUNC_2SEW:
1438        return extract64(imm, 0, s->sew + 4);
1439    default:
1440        g_assert_not_reached();
1441    }
1442}
1443
1444static bool opivi_trans(uint32_t vd, uint32_t imm, uint32_t vs2, uint32_t vm,
1445                        gen_helper_opivx *fn, DisasContext *s,
1446                        imm_mode_t imm_mode)
1447{
1448    TCGv_ptr dest, src2, mask;
1449    TCGv src1;
1450    TCGv_i32 desc;
1451    uint32_t data = 0;
1452
1453    TCGLabel *over = gen_new_label();
1454    tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_vl, 0, over);
1455    tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);
1456
1457    dest = tcg_temp_new_ptr();
1458    mask = tcg_temp_new_ptr();
1459    src2 = tcg_temp_new_ptr();
1460    src1 = tcg_constant_tl(extract_imm(s, imm, imm_mode));
1461
1462    data = FIELD_DP32(data, VDATA, VM, vm);
1463    data = FIELD_DP32(data, VDATA, LMUL, s->lmul);
1464    data = FIELD_DP32(data, VDATA, VTA, s->vta);
1465    data = FIELD_DP32(data, VDATA, VTA_ALL_1S, s->cfg_vta_all_1s);
1466    desc = tcg_constant_i32(simd_desc(s->cfg_ptr->vlen / 8,
1467                                      s->cfg_ptr->vlen / 8, data));
1468
1469    tcg_gen_addi_ptr(dest, cpu_env, vreg_ofs(s, vd));
1470    tcg_gen_addi_ptr(src2, cpu_env, vreg_ofs(s, vs2));
1471    tcg_gen_addi_ptr(mask, cpu_env, vreg_ofs(s, 0));
1472
1473    fn(dest, mask, src1, src2, cpu_env, desc);
1474
1475    tcg_temp_free_ptr(dest);
1476    tcg_temp_free_ptr(mask);
1477    tcg_temp_free_ptr(src2);
1478    mark_vs_dirty(s);
1479    gen_set_label(over);
1480    return true;
1481}
1482
1483typedef void GVecGen2iFn(unsigned, uint32_t, uint32_t, int64_t,
1484                         uint32_t, uint32_t);
1485
1486static inline bool
1487do_opivi_gvec(DisasContext *s, arg_rmrr *a, GVecGen2iFn *gvec_fn,
1488              gen_helper_opivx *fn, imm_mode_t imm_mode)
1489{
1490    if (!opivx_check(s, a)) {
1491        return false;
1492    }
1493
1494    if (a->vm && s->vl_eq_vlmax && !(s->vta && s->lmul < 0)) {
1495        gvec_fn(s->sew, vreg_ofs(s, a->rd), vreg_ofs(s, a->rs2),
1496                extract_imm(s, a->rs1, imm_mode), MAXSZ(s), MAXSZ(s));
1497        mark_vs_dirty(s);
1498        return true;
1499    }
1500    return opivi_trans(a->rd, a->rs1, a->rs2, a->vm, fn, s, imm_mode);
1501}
1502
1503/* OPIVI with GVEC IR */
1504#define GEN_OPIVI_GVEC_TRANS(NAME, IMM_MODE, OPIVX, SUF) \
1505static bool trans_##NAME(DisasContext *s, arg_rmrr *a)             \
1506{                                                                  \
1507    static gen_helper_opivx * const fns[4] = {                     \
1508        gen_helper_##OPIVX##_b, gen_helper_##OPIVX##_h,            \
1509        gen_helper_##OPIVX##_w, gen_helper_##OPIVX##_d,            \
1510    };                                                             \
1511    return do_opivi_gvec(s, a, tcg_gen_gvec_##SUF,                 \
1512                         fns[s->sew], IMM_MODE);                   \
1513}
1514
1515GEN_OPIVI_GVEC_TRANS(vadd_vi, IMM_SX, vadd_vx, addi)
1516
1517static void tcg_gen_gvec_rsubi(unsigned vece, uint32_t dofs, uint32_t aofs,
1518                               int64_t c, uint32_t oprsz, uint32_t maxsz)
1519{
1520    TCGv_i64 tmp = tcg_constant_i64(c);
1521    tcg_gen_gvec_rsubs(vece, dofs, aofs, tmp, oprsz, maxsz);
1522}
1523
1524GEN_OPIVI_GVEC_TRANS(vrsub_vi, IMM_SX, vrsub_vx, rsubi)
1525
1526/* Vector Widening Integer Add/Subtract */
1527
1528/* OPIVV with WIDEN */
1529static bool opivv_widen_check(DisasContext *s, arg_rmrr *a)
1530{
1531    return require_rvv(s) &&
1532           vext_check_isa_ill(s) &&
1533           vext_check_dss(s, a->rd, a->rs1, a->rs2, a->vm);
1534}
1535
1536static bool do_opivv_widen(DisasContext *s, arg_rmrr *a,
1537                           gen_helper_gvec_4_ptr *fn,
1538                           bool (*checkfn)(DisasContext *, arg_rmrr *))
1539{
1540    if (checkfn(s, a)) {
1541        uint32_t data = 0;
1542        TCGLabel *over = gen_new_label();
1543        tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_vl, 0, over);
1544        tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);
1545
1546        data = FIELD_DP32(data, VDATA, VM, a->vm);
1547        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);
1548        data = FIELD_DP32(data, VDATA, VTA, s->vta);
1549        data = FIELD_DP32(data, VDATA, VMA, s->vma);
1550        tcg_gen_gvec_4_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),
1551                           vreg_ofs(s, a->rs1),
1552                           vreg_ofs(s, a->rs2),
1553                           cpu_env, s->cfg_ptr->vlen / 8,
1554                           s->cfg_ptr->vlen / 8,
1555                           data, fn);
1556        mark_vs_dirty(s);
1557        gen_set_label(over);
1558        return true;
1559    }
1560    return false;
1561}
1562
1563#define GEN_OPIVV_WIDEN_TRANS(NAME, CHECK) \
1564static bool trans_##NAME(DisasContext *s, arg_rmrr *a)       \
1565{                                                            \
1566    static gen_helper_gvec_4_ptr * const fns[3] = {          \
1567        gen_helper_##NAME##_b,                               \
1568        gen_helper_##NAME##_h,                               \
1569        gen_helper_##NAME##_w                                \
1570    };                                                       \
1571    return do_opivv_widen(s, a, fns[s->sew], CHECK);         \
1572}
1573
1574GEN_OPIVV_WIDEN_TRANS(vwaddu_vv, opivv_widen_check)
1575GEN_OPIVV_WIDEN_TRANS(vwadd_vv, opivv_widen_check)
1576GEN_OPIVV_WIDEN_TRANS(vwsubu_vv, opivv_widen_check)
1577GEN_OPIVV_WIDEN_TRANS(vwsub_vv, opivv_widen_check)
1578
1579/* OPIVX with WIDEN */
1580static bool opivx_widen_check(DisasContext *s, arg_rmrr *a)
1581{
1582    return require_rvv(s) &&
1583           vext_check_isa_ill(s) &&
1584           vext_check_ds(s, a->rd, a->rs2, a->vm);
1585}
1586
1587static bool do_opivx_widen(DisasContext *s, arg_rmrr *a,
1588                           gen_helper_opivx *fn)
1589{
1590    if (opivx_widen_check(s, a)) {
1591        return opivx_trans(a->rd, a->rs1, a->rs2, a->vm, fn, s);
1592    }
1593    return false;
1594}
1595
1596#define GEN_OPIVX_WIDEN_TRANS(NAME) \
1597static bool trans_##NAME(DisasContext *s, arg_rmrr *a)       \
1598{                                                            \
1599    static gen_helper_opivx * const fns[3] = {               \
1600        gen_helper_##NAME##_b,                               \
1601        gen_helper_##NAME##_h,                               \
1602        gen_helper_##NAME##_w                                \
1603    };                                                       \
1604    return do_opivx_widen(s, a, fns[s->sew]);                \
1605}
1606
1607GEN_OPIVX_WIDEN_TRANS(vwaddu_vx)
1608GEN_OPIVX_WIDEN_TRANS(vwadd_vx)
1609GEN_OPIVX_WIDEN_TRANS(vwsubu_vx)
1610GEN_OPIVX_WIDEN_TRANS(vwsub_vx)
1611
1612/* WIDEN OPIVV with WIDEN */
1613static bool opiwv_widen_check(DisasContext *s, arg_rmrr *a)
1614{
1615    return require_rvv(s) &&
1616           vext_check_isa_ill(s) &&
1617           vext_check_dds(s, a->rd, a->rs1, a->rs2, a->vm);
1618}
1619
1620static bool do_opiwv_widen(DisasContext *s, arg_rmrr *a,
1621                           gen_helper_gvec_4_ptr *fn)
1622{
1623    if (opiwv_widen_check(s, a)) {
1624        uint32_t data = 0;
1625        TCGLabel *over = gen_new_label();
1626        tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_vl, 0, over);
1627        tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);
1628
1629        data = FIELD_DP32(data, VDATA, VM, a->vm);
1630        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);
1631        data = FIELD_DP32(data, VDATA, VTA, s->vta);
1632        data = FIELD_DP32(data, VDATA, VMA, s->vma);
1633        tcg_gen_gvec_4_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),
1634                           vreg_ofs(s, a->rs1),
1635                           vreg_ofs(s, a->rs2),
1636                           cpu_env, s->cfg_ptr->vlen / 8,
1637                           s->cfg_ptr->vlen / 8, data, fn);
1638        mark_vs_dirty(s);
1639        gen_set_label(over);
1640        return true;
1641    }
1642    return false;
1643}
1644
1645#define GEN_OPIWV_WIDEN_TRANS(NAME) \
1646static bool trans_##NAME(DisasContext *s, arg_rmrr *a)       \
1647{                                                            \
1648    static gen_helper_gvec_4_ptr * const fns[3] = {          \
1649        gen_helper_##NAME##_b,                               \
1650        gen_helper_##NAME##_h,                               \
1651        gen_helper_##NAME##_w                                \
1652    };                                                       \
1653    return do_opiwv_widen(s, a, fns[s->sew]);                \
1654}
1655
1656GEN_OPIWV_WIDEN_TRANS(vwaddu_wv)
1657GEN_OPIWV_WIDEN_TRANS(vwadd_wv)
1658GEN_OPIWV_WIDEN_TRANS(vwsubu_wv)
1659GEN_OPIWV_WIDEN_TRANS(vwsub_wv)
1660
1661/* WIDEN OPIVX with WIDEN */
1662static bool opiwx_widen_check(DisasContext *s, arg_rmrr *a)
1663{
1664    return require_rvv(s) &&
1665           vext_check_isa_ill(s) &&
1666           vext_check_dd(s, a->rd, a->rs2, a->vm);
1667}
1668
1669static bool do_opiwx_widen(DisasContext *s, arg_rmrr *a,
1670                           gen_helper_opivx *fn)
1671{
1672    if (opiwx_widen_check(s, a)) {
1673        return opivx_trans(a->rd, a->rs1, a->rs2, a->vm, fn, s);
1674    }
1675    return false;
1676}
1677
1678#define GEN_OPIWX_WIDEN_TRANS(NAME) \
1679static bool trans_##NAME(DisasContext *s, arg_rmrr *a)       \
1680{                                                            \
1681    static gen_helper_opivx * const fns[3] = {               \
1682        gen_helper_##NAME##_b,                               \
1683        gen_helper_##NAME##_h,                               \
1684        gen_helper_##NAME##_w                                \
1685    };                                                       \
1686    return do_opiwx_widen(s, a, fns[s->sew]);                \
1687}
1688
1689GEN_OPIWX_WIDEN_TRANS(vwaddu_wx)
1690GEN_OPIWX_WIDEN_TRANS(vwadd_wx)
1691GEN_OPIWX_WIDEN_TRANS(vwsubu_wx)
1692GEN_OPIWX_WIDEN_TRANS(vwsub_wx)
1693
1694/* Vector Integer Add-with-Carry / Subtract-with-Borrow Instructions */
1695/* OPIVV without GVEC IR */
1696#define GEN_OPIVV_TRANS(NAME, CHECK)                               \
1697static bool trans_##NAME(DisasContext *s, arg_rmrr *a)             \
1698{                                                                  \
1699    if (CHECK(s, a)) {                                             \
1700        uint32_t data = 0;                                         \
1701        static gen_helper_gvec_4_ptr * const fns[4] = {            \
1702            gen_helper_##NAME##_b, gen_helper_##NAME##_h,          \
1703            gen_helper_##NAME##_w, gen_helper_##NAME##_d,          \
1704        };                                                         \
1705        TCGLabel *over = gen_new_label();                          \
1706        tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_vl, 0, over);          \
1707        tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over); \
1708                                                                   \
1709        data = FIELD_DP32(data, VDATA, VM, a->vm);                 \
1710        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);             \
1711        data = FIELD_DP32(data, VDATA, VTA, s->vta);               \
1712        data =                                                     \
1713            FIELD_DP32(data, VDATA, VTA_ALL_1S, s->cfg_vta_all_1s);\
1714        tcg_gen_gvec_4_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),     \
1715                           vreg_ofs(s, a->rs1),                    \
1716                           vreg_ofs(s, a->rs2), cpu_env,           \
1717                           s->cfg_ptr->vlen / 8,                   \
1718                           s->cfg_ptr->vlen / 8, data,             \
1719                           fns[s->sew]);                           \
1720        mark_vs_dirty(s);                                          \
1721        gen_set_label(over);                                       \
1722        return true;                                               \
1723    }                                                              \
1724    return false;                                                  \
1725}
1726
1727/*
1728 * For vadc and vsbc, an illegal instruction exception is raised if the
1729 * destination vector register is v0 and LMUL > 1. (Section 11.4)
1730 */
1731static bool opivv_vadc_check(DisasContext *s, arg_rmrr *a)
1732{
1733    return require_rvv(s) &&
1734           vext_check_isa_ill(s) &&
1735           (a->rd != 0) &&
1736           vext_check_sss(s, a->rd, a->rs1, a->rs2, a->vm);
1737}
1738
1739GEN_OPIVV_TRANS(vadc_vvm, opivv_vadc_check)
1740GEN_OPIVV_TRANS(vsbc_vvm, opivv_vadc_check)
1741
1742/*
1743 * For vmadc and vmsbc, an illegal instruction exception is raised if the
1744 * destination vector register overlaps a source vector register group.
1745 */
1746static bool opivv_vmadc_check(DisasContext *s, arg_rmrr *a)
1747{
1748    return require_rvv(s) &&
1749           vext_check_isa_ill(s) &&
1750           vext_check_mss(s, a->rd, a->rs1, a->rs2);
1751}
1752
1753GEN_OPIVV_TRANS(vmadc_vvm, opivv_vmadc_check)
1754GEN_OPIVV_TRANS(vmsbc_vvm, opivv_vmadc_check)
1755
1756static bool opivx_vadc_check(DisasContext *s, arg_rmrr *a)
1757{
1758    return require_rvv(s) &&
1759           vext_check_isa_ill(s) &&
1760           (a->rd != 0) &&
1761           vext_check_ss(s, a->rd, a->rs2, a->vm);
1762}
1763
1764/* OPIVX without GVEC IR */
1765#define GEN_OPIVX_TRANS(NAME, CHECK)                                     \
1766static bool trans_##NAME(DisasContext *s, arg_rmrr *a)                   \
1767{                                                                        \
1768    if (CHECK(s, a)) {                                                   \
1769        static gen_helper_opivx * const fns[4] = {                       \
1770            gen_helper_##NAME##_b, gen_helper_##NAME##_h,                \
1771            gen_helper_##NAME##_w, gen_helper_##NAME##_d,                \
1772        };                                                               \
1773                                                                         \
1774        return opivx_trans(a->rd, a->rs1, a->rs2, a->vm, fns[s->sew], s);\
1775    }                                                                    \
1776    return false;                                                        \
1777}
1778
1779GEN_OPIVX_TRANS(vadc_vxm, opivx_vadc_check)
1780GEN_OPIVX_TRANS(vsbc_vxm, opivx_vadc_check)
1781
1782static bool opivx_vmadc_check(DisasContext *s, arg_rmrr *a)
1783{
1784    return require_rvv(s) &&
1785           vext_check_isa_ill(s) &&
1786           vext_check_ms(s, a->rd, a->rs2);
1787}
1788
1789GEN_OPIVX_TRANS(vmadc_vxm, opivx_vmadc_check)
1790GEN_OPIVX_TRANS(vmsbc_vxm, opivx_vmadc_check)
1791
1792/* OPIVI without GVEC IR */
1793#define GEN_OPIVI_TRANS(NAME, IMM_MODE, OPIVX, CHECK)                    \
1794static bool trans_##NAME(DisasContext *s, arg_rmrr *a)                   \
1795{                                                                        \
1796    if (CHECK(s, a)) {                                                   \
1797        static gen_helper_opivx * const fns[4] = {                       \
1798            gen_helper_##OPIVX##_b, gen_helper_##OPIVX##_h,              \
1799            gen_helper_##OPIVX##_w, gen_helper_##OPIVX##_d,              \
1800        };                                                               \
1801        return opivi_trans(a->rd, a->rs1, a->rs2, a->vm,                 \
1802                           fns[s->sew], s, IMM_MODE);                    \
1803    }                                                                    \
1804    return false;                                                        \
1805}
1806
1807GEN_OPIVI_TRANS(vadc_vim, IMM_SX, vadc_vxm, opivx_vadc_check)
1808GEN_OPIVI_TRANS(vmadc_vim, IMM_SX, vmadc_vxm, opivx_vmadc_check)
1809
1810/* Vector Bitwise Logical Instructions */
1811GEN_OPIVV_GVEC_TRANS(vand_vv, and)
1812GEN_OPIVV_GVEC_TRANS(vor_vv,  or)
1813GEN_OPIVV_GVEC_TRANS(vxor_vv, xor)
1814GEN_OPIVX_GVEC_TRANS(vand_vx, ands)
1815GEN_OPIVX_GVEC_TRANS(vor_vx,  ors)
1816GEN_OPIVX_GVEC_TRANS(vxor_vx, xors)
1817GEN_OPIVI_GVEC_TRANS(vand_vi, IMM_SX, vand_vx, andi)
1818GEN_OPIVI_GVEC_TRANS(vor_vi, IMM_SX, vor_vx,  ori)
1819GEN_OPIVI_GVEC_TRANS(vxor_vi, IMM_SX, vxor_vx, xori)
1820
1821/* Vector Single-Width Bit Shift Instructions */
1822GEN_OPIVV_GVEC_TRANS(vsll_vv,  shlv)
1823GEN_OPIVV_GVEC_TRANS(vsrl_vv,  shrv)
1824GEN_OPIVV_GVEC_TRANS(vsra_vv,  sarv)
1825
1826typedef void GVecGen2sFn32(unsigned, uint32_t, uint32_t, TCGv_i32,
1827                           uint32_t, uint32_t);
1828
1829static inline bool
1830do_opivx_gvec_shift(DisasContext *s, arg_rmrr *a, GVecGen2sFn32 *gvec_fn,
1831                    gen_helper_opivx *fn)
1832{
1833    if (!opivx_check(s, a)) {
1834        return false;
1835    }
1836
1837    if (a->vm && s->vl_eq_vlmax && !(s->vta && s->lmul < 0)) {
1838        TCGv_i32 src1 = tcg_temp_new_i32();
1839
1840        tcg_gen_trunc_tl_i32(src1, get_gpr(s, a->rs1, EXT_NONE));
1841        tcg_gen_extract_i32(src1, src1, 0, s->sew + 3);
1842        gvec_fn(s->sew, vreg_ofs(s, a->rd), vreg_ofs(s, a->rs2),
1843                src1, MAXSZ(s), MAXSZ(s));
1844
1845        tcg_temp_free_i32(src1);
1846        mark_vs_dirty(s);
1847        return true;
1848    }
1849    return opivx_trans(a->rd, a->rs1, a->rs2, a->vm, fn, s);
1850}
1851
1852#define GEN_OPIVX_GVEC_SHIFT_TRANS(NAME, SUF) \
1853static bool trans_##NAME(DisasContext *s, arg_rmrr *a)                    \
1854{                                                                         \
1855    static gen_helper_opivx * const fns[4] = {                            \
1856        gen_helper_##NAME##_b, gen_helper_##NAME##_h,                     \
1857        gen_helper_##NAME##_w, gen_helper_##NAME##_d,                     \
1858    };                                                                    \
1859                                                                          \
1860    return do_opivx_gvec_shift(s, a, tcg_gen_gvec_##SUF, fns[s->sew]);    \
1861}
1862
1863GEN_OPIVX_GVEC_SHIFT_TRANS(vsll_vx,  shls)
1864GEN_OPIVX_GVEC_SHIFT_TRANS(vsrl_vx,  shrs)
1865GEN_OPIVX_GVEC_SHIFT_TRANS(vsra_vx,  sars)
1866
1867GEN_OPIVI_GVEC_TRANS(vsll_vi, IMM_TRUNC_SEW, vsll_vx, shli)
1868GEN_OPIVI_GVEC_TRANS(vsrl_vi, IMM_TRUNC_SEW, vsrl_vx, shri)
1869GEN_OPIVI_GVEC_TRANS(vsra_vi, IMM_TRUNC_SEW, vsra_vx, sari)
1870
1871/* Vector Narrowing Integer Right Shift Instructions */
1872static bool opiwv_narrow_check(DisasContext *s, arg_rmrr *a)
1873{
1874    return require_rvv(s) &&
1875           vext_check_isa_ill(s) &&
1876           vext_check_sds(s, a->rd, a->rs1, a->rs2, a->vm);
1877}
1878
1879/* OPIVV with NARROW */
1880#define GEN_OPIWV_NARROW_TRANS(NAME)                               \
1881static bool trans_##NAME(DisasContext *s, arg_rmrr *a)             \
1882{                                                                  \
1883    if (opiwv_narrow_check(s, a)) {                                \
1884        uint32_t data = 0;                                         \
1885        static gen_helper_gvec_4_ptr * const fns[3] = {            \
1886            gen_helper_##NAME##_b,                                 \
1887            gen_helper_##NAME##_h,                                 \
1888            gen_helper_##NAME##_w,                                 \
1889        };                                                         \
1890        TCGLabel *over = gen_new_label();                          \
1891        tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_vl, 0, over);          \
1892        tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over); \
1893                                                                   \
1894        data = FIELD_DP32(data, VDATA, VM, a->vm);                 \
1895        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);             \
1896        data = FIELD_DP32(data, VDATA, VTA, s->vta);               \
1897        tcg_gen_gvec_4_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),     \
1898                           vreg_ofs(s, a->rs1),                    \
1899                           vreg_ofs(s, a->rs2), cpu_env,           \
1900                           s->cfg_ptr->vlen / 8,                   \
1901                           s->cfg_ptr->vlen / 8, data,             \
1902                           fns[s->sew]);                           \
1903        mark_vs_dirty(s);                                          \
1904        gen_set_label(over);                                       \
1905        return true;                                               \
1906    }                                                              \
1907    return false;                                                  \
1908}
1909GEN_OPIWV_NARROW_TRANS(vnsra_wv)
1910GEN_OPIWV_NARROW_TRANS(vnsrl_wv)
1911
1912static bool opiwx_narrow_check(DisasContext *s, arg_rmrr *a)
1913{
1914    return require_rvv(s) &&
1915           vext_check_isa_ill(s) &&
1916           vext_check_sd(s, a->rd, a->rs2, a->vm);
1917}
1918
1919/* OPIVX with NARROW */
1920#define GEN_OPIWX_NARROW_TRANS(NAME)                                     \
1921static bool trans_##NAME(DisasContext *s, arg_rmrr *a)                   \
1922{                                                                        \
1923    if (opiwx_narrow_check(s, a)) {                                      \
1924        static gen_helper_opivx * const fns[3] = {                       \
1925            gen_helper_##NAME##_b,                                       \
1926            gen_helper_##NAME##_h,                                       \
1927            gen_helper_##NAME##_w,                                       \
1928        };                                                               \
1929        return opivx_trans(a->rd, a->rs1, a->rs2, a->vm, fns[s->sew], s);\
1930    }                                                                    \
1931    return false;                                                        \
1932}
1933
1934GEN_OPIWX_NARROW_TRANS(vnsra_wx)
1935GEN_OPIWX_NARROW_TRANS(vnsrl_wx)
1936
1937/* OPIWI with NARROW */
1938#define GEN_OPIWI_NARROW_TRANS(NAME, IMM_MODE, OPIVX)                    \
1939static bool trans_##NAME(DisasContext *s, arg_rmrr *a)                   \
1940{                                                                        \
1941    if (opiwx_narrow_check(s, a)) {                                      \
1942        static gen_helper_opivx * const fns[3] = {                       \
1943            gen_helper_##OPIVX##_b,                                      \
1944            gen_helper_##OPIVX##_h,                                      \
1945            gen_helper_##OPIVX##_w,                                      \
1946        };                                                               \
1947        return opivi_trans(a->rd, a->rs1, a->rs2, a->vm,                 \
1948                           fns[s->sew], s, IMM_MODE);                    \
1949    }                                                                    \
1950    return false;                                                        \
1951}
1952
1953GEN_OPIWI_NARROW_TRANS(vnsra_wi, IMM_ZX, vnsra_wx)
1954GEN_OPIWI_NARROW_TRANS(vnsrl_wi, IMM_ZX, vnsrl_wx)
1955
1956/* Vector Integer Comparison Instructions */
1957/*
1958 * For all comparison instructions, an illegal instruction exception is raised
1959 * if the destination vector register overlaps a source vector register group
1960 * and LMUL > 1.
1961 */
1962static bool opivv_cmp_check(DisasContext *s, arg_rmrr *a)
1963{
1964    return require_rvv(s) &&
1965           vext_check_isa_ill(s) &&
1966           vext_check_mss(s, a->rd, a->rs1, a->rs2);
1967}
1968
1969GEN_OPIVV_TRANS(vmseq_vv, opivv_cmp_check)
1970GEN_OPIVV_TRANS(vmsne_vv, opivv_cmp_check)
1971GEN_OPIVV_TRANS(vmsltu_vv, opivv_cmp_check)
1972GEN_OPIVV_TRANS(vmslt_vv, opivv_cmp_check)
1973GEN_OPIVV_TRANS(vmsleu_vv, opivv_cmp_check)
1974GEN_OPIVV_TRANS(vmsle_vv, opivv_cmp_check)
1975
1976static bool opivx_cmp_check(DisasContext *s, arg_rmrr *a)
1977{
1978    return require_rvv(s) &&
1979           vext_check_isa_ill(s) &&
1980           vext_check_ms(s, a->rd, a->rs2);
1981}
1982
1983GEN_OPIVX_TRANS(vmseq_vx, opivx_cmp_check)
1984GEN_OPIVX_TRANS(vmsne_vx, opivx_cmp_check)
1985GEN_OPIVX_TRANS(vmsltu_vx, opivx_cmp_check)
1986GEN_OPIVX_TRANS(vmslt_vx, opivx_cmp_check)
1987GEN_OPIVX_TRANS(vmsleu_vx, opivx_cmp_check)
1988GEN_OPIVX_TRANS(vmsle_vx, opivx_cmp_check)
1989GEN_OPIVX_TRANS(vmsgtu_vx, opivx_cmp_check)
1990GEN_OPIVX_TRANS(vmsgt_vx, opivx_cmp_check)
1991
1992GEN_OPIVI_TRANS(vmseq_vi, IMM_SX, vmseq_vx, opivx_cmp_check)
1993GEN_OPIVI_TRANS(vmsne_vi, IMM_SX, vmsne_vx, opivx_cmp_check)
1994GEN_OPIVI_TRANS(vmsleu_vi, IMM_SX, vmsleu_vx, opivx_cmp_check)
1995GEN_OPIVI_TRANS(vmsle_vi, IMM_SX, vmsle_vx, opivx_cmp_check)
1996GEN_OPIVI_TRANS(vmsgtu_vi, IMM_SX, vmsgtu_vx, opivx_cmp_check)
1997GEN_OPIVI_TRANS(vmsgt_vi, IMM_SX, vmsgt_vx, opivx_cmp_check)
1998
1999/* Vector Integer Min/Max Instructions */
2000GEN_OPIVV_GVEC_TRANS(vminu_vv, umin)
2001GEN_OPIVV_GVEC_TRANS(vmin_vv,  smin)
2002GEN_OPIVV_GVEC_TRANS(vmaxu_vv, umax)
2003GEN_OPIVV_GVEC_TRANS(vmax_vv,  smax)
2004GEN_OPIVX_TRANS(vminu_vx, opivx_check)
2005GEN_OPIVX_TRANS(vmin_vx,  opivx_check)
2006GEN_OPIVX_TRANS(vmaxu_vx, opivx_check)
2007GEN_OPIVX_TRANS(vmax_vx,  opivx_check)
2008
2009/* Vector Single-Width Integer Multiply Instructions */
2010
2011static bool vmulh_vv_check(DisasContext *s, arg_rmrr *a)
2012{
2013    /*
2014     * All Zve* extensions support all vector integer instructions,
2015     * except that the vmulh integer multiply variants
2016     * that return the high word of the product
2017     * (vmulh.vv, vmulh.vx, vmulhu.vv, vmulhu.vx, vmulhsu.vv, vmulhsu.vx)
2018     * are not included for EEW=64 in Zve64*. (Section 18.2)
2019     */
2020    return opivv_check(s, a) &&
2021           (!has_ext(s, RVV) &&
2022            s->cfg_ptr->ext_zve64f ? s->sew != MO_64 : true);
2023}
2024
2025static bool vmulh_vx_check(DisasContext *s, arg_rmrr *a)
2026{
2027    /*
2028     * All Zve* extensions support all vector integer instructions,
2029     * except that the vmulh integer multiply variants
2030     * that return the high word of the product
2031     * (vmulh.vv, vmulh.vx, vmulhu.vv, vmulhu.vx, vmulhsu.vv, vmulhsu.vx)
2032     * are not included for EEW=64 in Zve64*. (Section 18.2)
2033     */
2034    return opivx_check(s, a) &&
2035           (!has_ext(s, RVV) &&
2036            s->cfg_ptr->ext_zve64f ? s->sew != MO_64 : true);
2037}
2038
2039GEN_OPIVV_GVEC_TRANS(vmul_vv,  mul)
2040GEN_OPIVV_TRANS(vmulh_vv, vmulh_vv_check)
2041GEN_OPIVV_TRANS(vmulhu_vv, vmulh_vv_check)
2042GEN_OPIVV_TRANS(vmulhsu_vv, vmulh_vv_check)
2043GEN_OPIVX_GVEC_TRANS(vmul_vx,  muls)
2044GEN_OPIVX_TRANS(vmulh_vx, vmulh_vx_check)
2045GEN_OPIVX_TRANS(vmulhu_vx, vmulh_vx_check)
2046GEN_OPIVX_TRANS(vmulhsu_vx, vmulh_vx_check)
2047
2048/* Vector Integer Divide Instructions */
2049GEN_OPIVV_TRANS(vdivu_vv, opivv_check)
2050GEN_OPIVV_TRANS(vdiv_vv, opivv_check)
2051GEN_OPIVV_TRANS(vremu_vv, opivv_check)
2052GEN_OPIVV_TRANS(vrem_vv, opivv_check)
2053GEN_OPIVX_TRANS(vdivu_vx, opivx_check)
2054GEN_OPIVX_TRANS(vdiv_vx, opivx_check)
2055GEN_OPIVX_TRANS(vremu_vx, opivx_check)
2056GEN_OPIVX_TRANS(vrem_vx, opivx_check)
2057
2058/* Vector Widening Integer Multiply Instructions */
2059GEN_OPIVV_WIDEN_TRANS(vwmul_vv, opivv_widen_check)
2060GEN_OPIVV_WIDEN_TRANS(vwmulu_vv, opivv_widen_check)
2061GEN_OPIVV_WIDEN_TRANS(vwmulsu_vv, opivv_widen_check)
2062GEN_OPIVX_WIDEN_TRANS(vwmul_vx)
2063GEN_OPIVX_WIDEN_TRANS(vwmulu_vx)
2064GEN_OPIVX_WIDEN_TRANS(vwmulsu_vx)
2065
2066/* Vector Single-Width Integer Multiply-Add Instructions */
2067GEN_OPIVV_TRANS(vmacc_vv, opivv_check)
2068GEN_OPIVV_TRANS(vnmsac_vv, opivv_check)
2069GEN_OPIVV_TRANS(vmadd_vv, opivv_check)
2070GEN_OPIVV_TRANS(vnmsub_vv, opivv_check)
2071GEN_OPIVX_TRANS(vmacc_vx, opivx_check)
2072GEN_OPIVX_TRANS(vnmsac_vx, opivx_check)
2073GEN_OPIVX_TRANS(vmadd_vx, opivx_check)
2074GEN_OPIVX_TRANS(vnmsub_vx, opivx_check)
2075
2076/* Vector Widening Integer Multiply-Add Instructions */
2077GEN_OPIVV_WIDEN_TRANS(vwmaccu_vv, opivv_widen_check)
2078GEN_OPIVV_WIDEN_TRANS(vwmacc_vv, opivv_widen_check)
2079GEN_OPIVV_WIDEN_TRANS(vwmaccsu_vv, opivv_widen_check)
2080GEN_OPIVX_WIDEN_TRANS(vwmaccu_vx)
2081GEN_OPIVX_WIDEN_TRANS(vwmacc_vx)
2082GEN_OPIVX_WIDEN_TRANS(vwmaccsu_vx)
2083GEN_OPIVX_WIDEN_TRANS(vwmaccus_vx)
2084
2085/* Vector Integer Merge and Move Instructions */
2086static bool trans_vmv_v_v(DisasContext *s, arg_vmv_v_v *a)
2087{
2088    if (require_rvv(s) &&
2089        vext_check_isa_ill(s) &&
2090        /* vmv.v.v has rs2 = 0 and vm = 1 */
2091        vext_check_sss(s, a->rd, a->rs1, 0, 1)) {
2092        if (s->vl_eq_vlmax && !(s->vta && s->lmul < 0)) {
2093            tcg_gen_gvec_mov(s->sew, vreg_ofs(s, a->rd),
2094                             vreg_ofs(s, a->rs1),
2095                             MAXSZ(s), MAXSZ(s));
2096        } else {
2097            uint32_t data = FIELD_DP32(0, VDATA, LMUL, s->lmul);
2098            data = FIELD_DP32(data, VDATA, VTA, s->vta);
2099            static gen_helper_gvec_2_ptr * const fns[4] = {
2100                gen_helper_vmv_v_v_b, gen_helper_vmv_v_v_h,
2101                gen_helper_vmv_v_v_w, gen_helper_vmv_v_v_d,
2102            };
2103            TCGLabel *over = gen_new_label();
2104            tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_vl, 0, over);
2105            tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);
2106
2107            tcg_gen_gvec_2_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, a->rs1),
2108                               cpu_env, s->cfg_ptr->vlen / 8,
2109                               s->cfg_ptr->vlen / 8, data,
2110                               fns[s->sew]);
2111            gen_set_label(over);
2112        }
2113        mark_vs_dirty(s);
2114        return true;
2115    }
2116    return false;
2117}
2118
2119typedef void gen_helper_vmv_vx(TCGv_ptr, TCGv_i64, TCGv_env, TCGv_i32);
2120static bool trans_vmv_v_x(DisasContext *s, arg_vmv_v_x *a)
2121{
2122    if (require_rvv(s) &&
2123        vext_check_isa_ill(s) &&
2124        /* vmv.v.x has rs2 = 0 and vm = 1 */
2125        vext_check_ss(s, a->rd, 0, 1)) {
2126        TCGv s1;
2127        TCGLabel *over = gen_new_label();
2128        tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_vl, 0, over);
2129        tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);
2130
2131        s1 = get_gpr(s, a->rs1, EXT_SIGN);
2132
2133        if (s->vl_eq_vlmax && !(s->vta && s->lmul < 0)) {
2134            if (get_xl(s) == MXL_RV32 && s->sew == MO_64) {
2135                TCGv_i64 s1_i64 = tcg_temp_new_i64();
2136                tcg_gen_ext_tl_i64(s1_i64, s1);
2137                tcg_gen_gvec_dup_i64(s->sew, vreg_ofs(s, a->rd),
2138                                     MAXSZ(s), MAXSZ(s), s1_i64);
2139                tcg_temp_free_i64(s1_i64);
2140            } else {
2141                tcg_gen_gvec_dup_tl(s->sew, vreg_ofs(s, a->rd),
2142                                    MAXSZ(s), MAXSZ(s), s1);
2143            }
2144        } else {
2145            TCGv_i32 desc;
2146            TCGv_i64 s1_i64 = tcg_temp_new_i64();
2147            TCGv_ptr dest = tcg_temp_new_ptr();
2148            uint32_t data = FIELD_DP32(0, VDATA, LMUL, s->lmul);
2149            data = FIELD_DP32(data, VDATA, VTA, s->vta);
2150            static gen_helper_vmv_vx * const fns[4] = {
2151                gen_helper_vmv_v_x_b, gen_helper_vmv_v_x_h,
2152                gen_helper_vmv_v_x_w, gen_helper_vmv_v_x_d,
2153            };
2154
2155            tcg_gen_ext_tl_i64(s1_i64, s1);
2156            desc = tcg_constant_i32(simd_desc(s->cfg_ptr->vlen / 8,
2157                                              s->cfg_ptr->vlen / 8, data));
2158            tcg_gen_addi_ptr(dest, cpu_env, vreg_ofs(s, a->rd));
2159            fns[s->sew](dest, s1_i64, cpu_env, desc);
2160
2161            tcg_temp_free_ptr(dest);
2162            tcg_temp_free_i64(s1_i64);
2163        }
2164
2165        mark_vs_dirty(s);
2166        gen_set_label(over);
2167        return true;
2168    }
2169    return false;
2170}
2171
2172static bool trans_vmv_v_i(DisasContext *s, arg_vmv_v_i *a)
2173{
2174    if (require_rvv(s) &&
2175        vext_check_isa_ill(s) &&
2176        /* vmv.v.i has rs2 = 0 and vm = 1 */
2177        vext_check_ss(s, a->rd, 0, 1)) {
2178        int64_t simm = sextract64(a->rs1, 0, 5);
2179        if (s->vl_eq_vlmax && !(s->vta && s->lmul < 0)) {
2180            tcg_gen_gvec_dup_imm(s->sew, vreg_ofs(s, a->rd),
2181                                 MAXSZ(s), MAXSZ(s), simm);
2182            mark_vs_dirty(s);
2183        } else {
2184            TCGv_i32 desc;
2185            TCGv_i64 s1;
2186            TCGv_ptr dest;
2187            uint32_t data = FIELD_DP32(0, VDATA, LMUL, s->lmul);
2188            data = FIELD_DP32(data, VDATA, VTA, s->vta);
2189            static gen_helper_vmv_vx * const fns[4] = {
2190                gen_helper_vmv_v_x_b, gen_helper_vmv_v_x_h,
2191                gen_helper_vmv_v_x_w, gen_helper_vmv_v_x_d,
2192            };
2193            TCGLabel *over = gen_new_label();
2194            tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_vl, 0, over);
2195            tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);
2196
2197            s1 = tcg_constant_i64(simm);
2198            dest = tcg_temp_new_ptr();
2199            desc = tcg_constant_i32(simd_desc(s->cfg_ptr->vlen / 8,
2200                                              s->cfg_ptr->vlen / 8, data));
2201            tcg_gen_addi_ptr(dest, cpu_env, vreg_ofs(s, a->rd));
2202            fns[s->sew](dest, s1, cpu_env, desc);
2203
2204            tcg_temp_free_ptr(dest);
2205            mark_vs_dirty(s);
2206            gen_set_label(over);
2207        }
2208        return true;
2209    }
2210    return false;
2211}
2212
2213GEN_OPIVV_TRANS(vmerge_vvm, opivv_vadc_check)
2214GEN_OPIVX_TRANS(vmerge_vxm, opivx_vadc_check)
2215GEN_OPIVI_TRANS(vmerge_vim, IMM_SX, vmerge_vxm, opivx_vadc_check)
2216
2217/*
2218 *** Vector Fixed-Point Arithmetic Instructions
2219 */
2220
2221/* Vector Single-Width Saturating Add and Subtract */
2222GEN_OPIVV_TRANS(vsaddu_vv, opivv_check)
2223GEN_OPIVV_TRANS(vsadd_vv,  opivv_check)
2224GEN_OPIVV_TRANS(vssubu_vv, opivv_check)
2225GEN_OPIVV_TRANS(vssub_vv,  opivv_check)
2226GEN_OPIVX_TRANS(vsaddu_vx,  opivx_check)
2227GEN_OPIVX_TRANS(vsadd_vx,  opivx_check)
2228GEN_OPIVX_TRANS(vssubu_vx,  opivx_check)
2229GEN_OPIVX_TRANS(vssub_vx,  opivx_check)
2230GEN_OPIVI_TRANS(vsaddu_vi, IMM_SX, vsaddu_vx, opivx_check)
2231GEN_OPIVI_TRANS(vsadd_vi, IMM_SX, vsadd_vx, opivx_check)
2232
2233/* Vector Single-Width Averaging Add and Subtract */
2234GEN_OPIVV_TRANS(vaadd_vv, opivv_check)
2235GEN_OPIVV_TRANS(vaaddu_vv, opivv_check)
2236GEN_OPIVV_TRANS(vasub_vv, opivv_check)
2237GEN_OPIVV_TRANS(vasubu_vv, opivv_check)
2238GEN_OPIVX_TRANS(vaadd_vx,  opivx_check)
2239GEN_OPIVX_TRANS(vaaddu_vx,  opivx_check)
2240GEN_OPIVX_TRANS(vasub_vx,  opivx_check)
2241GEN_OPIVX_TRANS(vasubu_vx,  opivx_check)
2242
2243/* Vector Single-Width Fractional Multiply with Rounding and Saturation */
2244
2245static bool vsmul_vv_check(DisasContext *s, arg_rmrr *a)
2246{
2247    /*
2248     * All Zve* extensions support all vector fixed-point arithmetic
2249     * instructions, except that vsmul.vv and vsmul.vx are not supported
2250     * for EEW=64 in Zve64*. (Section 18.2)
2251     */
2252    return opivv_check(s, a) &&
2253           (!has_ext(s, RVV) &&
2254            s->cfg_ptr->ext_zve64f ? s->sew != MO_64 : true);
2255}
2256
2257static bool vsmul_vx_check(DisasContext *s, arg_rmrr *a)
2258{
2259    /*
2260     * All Zve* extensions support all vector fixed-point arithmetic
2261     * instructions, except that vsmul.vv and vsmul.vx are not supported
2262     * for EEW=64 in Zve64*. (Section 18.2)
2263     */
2264    return opivx_check(s, a) &&
2265           (!has_ext(s, RVV) &&
2266            s->cfg_ptr->ext_zve64f ? s->sew != MO_64 : true);
2267}
2268
2269GEN_OPIVV_TRANS(vsmul_vv, vsmul_vv_check)
2270GEN_OPIVX_TRANS(vsmul_vx,  vsmul_vx_check)
2271
2272/* Vector Single-Width Scaling Shift Instructions */
2273GEN_OPIVV_TRANS(vssrl_vv, opivv_check)
2274GEN_OPIVV_TRANS(vssra_vv, opivv_check)
2275GEN_OPIVX_TRANS(vssrl_vx,  opivx_check)
2276GEN_OPIVX_TRANS(vssra_vx,  opivx_check)
2277GEN_OPIVI_TRANS(vssrl_vi, IMM_TRUNC_SEW, vssrl_vx, opivx_check)
2278GEN_OPIVI_TRANS(vssra_vi, IMM_TRUNC_SEW, vssra_vx, opivx_check)
2279
2280/* Vector Narrowing Fixed-Point Clip Instructions */
2281GEN_OPIWV_NARROW_TRANS(vnclipu_wv)
2282GEN_OPIWV_NARROW_TRANS(vnclip_wv)
2283GEN_OPIWX_NARROW_TRANS(vnclipu_wx)
2284GEN_OPIWX_NARROW_TRANS(vnclip_wx)
2285GEN_OPIWI_NARROW_TRANS(vnclipu_wi, IMM_ZX, vnclipu_wx)
2286GEN_OPIWI_NARROW_TRANS(vnclip_wi, IMM_ZX, vnclip_wx)
2287
2288/*
2289 *** Vector Float Point Arithmetic Instructions
2290 */
2291
2292/*
2293 * As RVF-only cpus always have values NaN-boxed to 64-bits,
2294 * RVF and RVD can be treated equally.
2295 * We don't have to deal with the cases of: SEW > FLEN.
2296 *
2297 * If SEW < FLEN, check whether input fp register is a valid
2298 * NaN-boxed value, in which case the least-significant SEW bits
2299 * of the f regsiter are used, else the canonical NaN value is used.
2300 */
2301static void do_nanbox(DisasContext *s, TCGv_i64 out, TCGv_i64 in)
2302{
2303    switch (s->sew) {
2304    case 1:
2305        gen_check_nanbox_h(out, in);
2306        break;
2307    case 2:
2308        gen_check_nanbox_s(out, in);
2309        break;
2310    case 3:
2311        tcg_gen_mov_i64(out, in);
2312        break;
2313    default:
2314        g_assert_not_reached();
2315    }
2316}
2317
2318/* Vector Single-Width Floating-Point Add/Subtract Instructions */
2319
2320/*
2321 * If the current SEW does not correspond to a supported IEEE floating-point
2322 * type, an illegal instruction exception is raised.
2323 */
2324static bool opfvv_check(DisasContext *s, arg_rmrr *a)
2325{
2326    return require_rvv(s) &&
2327           require_rvf(s) &&
2328           vext_check_isa_ill(s) &&
2329           vext_check_sss(s, a->rd, a->rs1, a->rs2, a->vm) &&
2330           require_zve32f(s) &&
2331           require_zve64f(s);
2332}
2333
2334/* OPFVV without GVEC IR */
2335#define GEN_OPFVV_TRANS(NAME, CHECK)                               \
2336static bool trans_##NAME(DisasContext *s, arg_rmrr *a)             \
2337{                                                                  \
2338    if (CHECK(s, a)) {                                             \
2339        uint32_t data = 0;                                         \
2340        static gen_helper_gvec_4_ptr * const fns[3] = {            \
2341            gen_helper_##NAME##_h,                                 \
2342            gen_helper_##NAME##_w,                                 \
2343            gen_helper_##NAME##_d,                                 \
2344        };                                                         \
2345        TCGLabel *over = gen_new_label();                          \
2346        gen_set_rm(s, RISCV_FRM_DYN);                              \
2347        tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_vl, 0, over);          \
2348        tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over); \
2349                                                                   \
2350        data = FIELD_DP32(data, VDATA, VM, a->vm);                 \
2351        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);             \
2352        data = FIELD_DP32(data, VDATA, VTA, s->vta);               \
2353        data =                                                     \
2354            FIELD_DP32(data, VDATA, VTA_ALL_1S, s->cfg_vta_all_1s);\
2355        tcg_gen_gvec_4_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),     \
2356                           vreg_ofs(s, a->rs1),                    \
2357                           vreg_ofs(s, a->rs2), cpu_env,           \
2358                           s->cfg_ptr->vlen / 8,                   \
2359                           s->cfg_ptr->vlen / 8, data,             \
2360                           fns[s->sew - 1]);                       \
2361        mark_vs_dirty(s);                                          \
2362        gen_set_label(over);                                       \
2363        return true;                                               \
2364    }                                                              \
2365    return false;                                                  \
2366}
2367GEN_OPFVV_TRANS(vfadd_vv, opfvv_check)
2368GEN_OPFVV_TRANS(vfsub_vv, opfvv_check)
2369
2370typedef void gen_helper_opfvf(TCGv_ptr, TCGv_ptr, TCGv_i64, TCGv_ptr,
2371                              TCGv_env, TCGv_i32);
2372
2373static bool opfvf_trans(uint32_t vd, uint32_t rs1, uint32_t vs2,
2374                        uint32_t data, gen_helper_opfvf *fn, DisasContext *s)
2375{
2376    TCGv_ptr dest, src2, mask;
2377    TCGv_i32 desc;
2378    TCGv_i64 t1;
2379
2380    TCGLabel *over = gen_new_label();
2381    tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_vl, 0, over);
2382    tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);
2383
2384    dest = tcg_temp_new_ptr();
2385    mask = tcg_temp_new_ptr();
2386    src2 = tcg_temp_new_ptr();
2387    desc = tcg_constant_i32(simd_desc(s->cfg_ptr->vlen / 8,
2388                                      s->cfg_ptr->vlen / 8, data));
2389
2390    tcg_gen_addi_ptr(dest, cpu_env, vreg_ofs(s, vd));
2391    tcg_gen_addi_ptr(src2, cpu_env, vreg_ofs(s, vs2));
2392    tcg_gen_addi_ptr(mask, cpu_env, vreg_ofs(s, 0));
2393
2394    /* NaN-box f[rs1] */
2395    t1 = tcg_temp_new_i64();
2396    do_nanbox(s, t1, cpu_fpr[rs1]);
2397
2398    fn(dest, mask, t1, src2, cpu_env, desc);
2399
2400    tcg_temp_free_ptr(dest);
2401    tcg_temp_free_ptr(mask);
2402    tcg_temp_free_ptr(src2);
2403    tcg_temp_free_i64(t1);
2404    mark_vs_dirty(s);
2405    gen_set_label(over);
2406    return true;
2407}
2408
2409/*
2410 * If the current SEW does not correspond to a supported IEEE floating-point
2411 * type, an illegal instruction exception is raised
2412 */
2413static bool opfvf_check(DisasContext *s, arg_rmrr *a)
2414{
2415    return require_rvv(s) &&
2416           require_rvf(s) &&
2417           vext_check_isa_ill(s) &&
2418           vext_check_ss(s, a->rd, a->rs2, a->vm) &&
2419           require_zve32f(s) &&
2420           require_zve64f(s);
2421}
2422
2423/* OPFVF without GVEC IR */
2424#define GEN_OPFVF_TRANS(NAME, CHECK)                              \
2425static bool trans_##NAME(DisasContext *s, arg_rmrr *a)            \
2426{                                                                 \
2427    if (CHECK(s, a)) {                                            \
2428        uint32_t data = 0;                                        \
2429        static gen_helper_opfvf *const fns[3] = {                 \
2430            gen_helper_##NAME##_h,                                \
2431            gen_helper_##NAME##_w,                                \
2432            gen_helper_##NAME##_d,                                \
2433        };                                                        \
2434        gen_set_rm(s, RISCV_FRM_DYN);                             \
2435        data = FIELD_DP32(data, VDATA, VM, a->vm);                \
2436        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);            \
2437        data = FIELD_DP32(data, VDATA, VTA, s->vta);              \
2438        data = FIELD_DP32(data, VDATA, VTA_ALL_1S,                \
2439                          s->cfg_vta_all_1s);                     \
2440        return opfvf_trans(a->rd, a->rs1, a->rs2, data,           \
2441                           fns[s->sew - 1], s);                   \
2442    }                                                             \
2443    return false;                                                 \
2444}
2445
2446GEN_OPFVF_TRANS(vfadd_vf,  opfvf_check)
2447GEN_OPFVF_TRANS(vfsub_vf,  opfvf_check)
2448GEN_OPFVF_TRANS(vfrsub_vf,  opfvf_check)
2449
2450/* Vector Widening Floating-Point Add/Subtract Instructions */
2451static bool opfvv_widen_check(DisasContext *s, arg_rmrr *a)
2452{
2453    return require_rvv(s) &&
2454           require_scale_rvf(s) &&
2455           (s->sew != MO_8) &&
2456           vext_check_isa_ill(s) &&
2457           vext_check_dss(s, a->rd, a->rs1, a->rs2, a->vm) &&
2458           require_scale_zve32f(s) &&
2459           require_scale_zve64f(s);
2460}
2461
2462/* OPFVV with WIDEN */
2463#define GEN_OPFVV_WIDEN_TRANS(NAME, CHECK)                       \
2464static bool trans_##NAME(DisasContext *s, arg_rmrr *a)           \
2465{                                                                \
2466    if (CHECK(s, a)) {                                           \
2467        uint32_t data = 0;                                       \
2468        static gen_helper_gvec_4_ptr * const fns[2] = {          \
2469            gen_helper_##NAME##_h, gen_helper_##NAME##_w,        \
2470        };                                                       \
2471        TCGLabel *over = gen_new_label();                        \
2472        gen_set_rm(s, RISCV_FRM_DYN);                            \
2473        tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_vl, 0, over);        \
2474        tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);\
2475                                                                 \
2476        data = FIELD_DP32(data, VDATA, VM, a->vm);               \
2477        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);           \
2478        data = FIELD_DP32(data, VDATA, VTA, s->vta);             \
2479        tcg_gen_gvec_4_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),   \
2480                           vreg_ofs(s, a->rs1),                  \
2481                           vreg_ofs(s, a->rs2), cpu_env,         \
2482                           s->cfg_ptr->vlen / 8,                 \
2483                           s->cfg_ptr->vlen / 8, data,           \
2484                           fns[s->sew - 1]);                     \
2485        mark_vs_dirty(s);                                        \
2486        gen_set_label(over);                                     \
2487        return true;                                             \
2488    }                                                            \
2489    return false;                                                \
2490}
2491
2492GEN_OPFVV_WIDEN_TRANS(vfwadd_vv, opfvv_widen_check)
2493GEN_OPFVV_WIDEN_TRANS(vfwsub_vv, opfvv_widen_check)
2494
2495static bool opfvf_widen_check(DisasContext *s, arg_rmrr *a)
2496{
2497    return require_rvv(s) &&
2498           require_scale_rvf(s) &&
2499           (s->sew != MO_8) &&
2500           vext_check_isa_ill(s) &&
2501           vext_check_ds(s, a->rd, a->rs2, a->vm) &&
2502           require_scale_zve32f(s) &&
2503           require_scale_zve64f(s);
2504}
2505
2506/* OPFVF with WIDEN */
2507#define GEN_OPFVF_WIDEN_TRANS(NAME)                              \
2508static bool trans_##NAME(DisasContext *s, arg_rmrr *a)           \
2509{                                                                \
2510    if (opfvf_widen_check(s, a)) {                               \
2511        uint32_t data = 0;                                       \
2512        static gen_helper_opfvf *const fns[2] = {                \
2513            gen_helper_##NAME##_h, gen_helper_##NAME##_w,        \
2514        };                                                       \
2515        gen_set_rm(s, RISCV_FRM_DYN);                            \
2516        data = FIELD_DP32(data, VDATA, VM, a->vm);               \
2517        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);           \
2518        data = FIELD_DP32(data, VDATA, VTA, s->vta);             \
2519        return opfvf_trans(a->rd, a->rs1, a->rs2, data,          \
2520                           fns[s->sew - 1], s);                  \
2521    }                                                            \
2522    return false;                                                \
2523}
2524
2525GEN_OPFVF_WIDEN_TRANS(vfwadd_vf)
2526GEN_OPFVF_WIDEN_TRANS(vfwsub_vf)
2527
2528static bool opfwv_widen_check(DisasContext *s, arg_rmrr *a)
2529{
2530    return require_rvv(s) &&
2531           require_scale_rvf(s) &&
2532           (s->sew != MO_8) &&
2533           vext_check_isa_ill(s) &&
2534           vext_check_dds(s, a->rd, a->rs1, a->rs2, a->vm) &&
2535           require_scale_zve32f(s) &&
2536           require_scale_zve64f(s);
2537}
2538
2539/* WIDEN OPFVV with WIDEN */
2540#define GEN_OPFWV_WIDEN_TRANS(NAME)                                \
2541static bool trans_##NAME(DisasContext *s, arg_rmrr *a)             \
2542{                                                                  \
2543    if (opfwv_widen_check(s, a)) {                                 \
2544        uint32_t data = 0;                                         \
2545        static gen_helper_gvec_4_ptr * const fns[2] = {            \
2546            gen_helper_##NAME##_h, gen_helper_##NAME##_w,          \
2547        };                                                         \
2548        TCGLabel *over = gen_new_label();                          \
2549        gen_set_rm(s, RISCV_FRM_DYN);                              \
2550        tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_vl, 0, over);          \
2551        tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over); \
2552                                                                   \
2553        data = FIELD_DP32(data, VDATA, VM, a->vm);                 \
2554        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);             \
2555        data = FIELD_DP32(data, VDATA, VTA, s->vta);               \
2556        tcg_gen_gvec_4_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),     \
2557                           vreg_ofs(s, a->rs1),                    \
2558                           vreg_ofs(s, a->rs2), cpu_env,           \
2559                           s->cfg_ptr->vlen / 8,                   \
2560                           s->cfg_ptr->vlen / 8, data,             \
2561                           fns[s->sew - 1]);                       \
2562        mark_vs_dirty(s);                                          \
2563        gen_set_label(over);                                       \
2564        return true;                                               \
2565    }                                                              \
2566    return false;                                                  \
2567}
2568
2569GEN_OPFWV_WIDEN_TRANS(vfwadd_wv)
2570GEN_OPFWV_WIDEN_TRANS(vfwsub_wv)
2571
2572static bool opfwf_widen_check(DisasContext *s, arg_rmrr *a)
2573{
2574    return require_rvv(s) &&
2575           require_scale_rvf(s) &&
2576           (s->sew != MO_8) &&
2577           vext_check_isa_ill(s) &&
2578           vext_check_dd(s, a->rd, a->rs2, a->vm) &&
2579           require_scale_zve32f(s) &&
2580           require_scale_zve64f(s);
2581}
2582
2583/* WIDEN OPFVF with WIDEN */
2584#define GEN_OPFWF_WIDEN_TRANS(NAME)                              \
2585static bool trans_##NAME(DisasContext *s, arg_rmrr *a)           \
2586{                                                                \
2587    if (opfwf_widen_check(s, a)) {                               \
2588        uint32_t data = 0;                                       \
2589        static gen_helper_opfvf *const fns[2] = {                \
2590            gen_helper_##NAME##_h, gen_helper_##NAME##_w,        \
2591        };                                                       \
2592        gen_set_rm(s, RISCV_FRM_DYN);                            \
2593        data = FIELD_DP32(data, VDATA, VM, a->vm);               \
2594        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);           \
2595        data = FIELD_DP32(data, VDATA, VTA, s->vta);             \
2596        return opfvf_trans(a->rd, a->rs1, a->rs2, data,          \
2597                           fns[s->sew - 1], s);                  \
2598    }                                                            \
2599    return false;                                                \
2600}
2601
2602GEN_OPFWF_WIDEN_TRANS(vfwadd_wf)
2603GEN_OPFWF_WIDEN_TRANS(vfwsub_wf)
2604
2605/* Vector Single-Width Floating-Point Multiply/Divide Instructions */
2606GEN_OPFVV_TRANS(vfmul_vv, opfvv_check)
2607GEN_OPFVV_TRANS(vfdiv_vv, opfvv_check)
2608GEN_OPFVF_TRANS(vfmul_vf,  opfvf_check)
2609GEN_OPFVF_TRANS(vfdiv_vf,  opfvf_check)
2610GEN_OPFVF_TRANS(vfrdiv_vf,  opfvf_check)
2611
2612/* Vector Widening Floating-Point Multiply */
2613GEN_OPFVV_WIDEN_TRANS(vfwmul_vv, opfvv_widen_check)
2614GEN_OPFVF_WIDEN_TRANS(vfwmul_vf)
2615
2616/* Vector Single-Width Floating-Point Fused Multiply-Add Instructions */
2617GEN_OPFVV_TRANS(vfmacc_vv, opfvv_check)
2618GEN_OPFVV_TRANS(vfnmacc_vv, opfvv_check)
2619GEN_OPFVV_TRANS(vfmsac_vv, opfvv_check)
2620GEN_OPFVV_TRANS(vfnmsac_vv, opfvv_check)
2621GEN_OPFVV_TRANS(vfmadd_vv, opfvv_check)
2622GEN_OPFVV_TRANS(vfnmadd_vv, opfvv_check)
2623GEN_OPFVV_TRANS(vfmsub_vv, opfvv_check)
2624GEN_OPFVV_TRANS(vfnmsub_vv, opfvv_check)
2625GEN_OPFVF_TRANS(vfmacc_vf, opfvf_check)
2626GEN_OPFVF_TRANS(vfnmacc_vf, opfvf_check)
2627GEN_OPFVF_TRANS(vfmsac_vf, opfvf_check)
2628GEN_OPFVF_TRANS(vfnmsac_vf, opfvf_check)
2629GEN_OPFVF_TRANS(vfmadd_vf, opfvf_check)
2630GEN_OPFVF_TRANS(vfnmadd_vf, opfvf_check)
2631GEN_OPFVF_TRANS(vfmsub_vf, opfvf_check)
2632GEN_OPFVF_TRANS(vfnmsub_vf, opfvf_check)
2633
2634/* Vector Widening Floating-Point Fused Multiply-Add Instructions */
2635GEN_OPFVV_WIDEN_TRANS(vfwmacc_vv, opfvv_widen_check)
2636GEN_OPFVV_WIDEN_TRANS(vfwnmacc_vv, opfvv_widen_check)
2637GEN_OPFVV_WIDEN_TRANS(vfwmsac_vv, opfvv_widen_check)
2638GEN_OPFVV_WIDEN_TRANS(vfwnmsac_vv, opfvv_widen_check)
2639GEN_OPFVF_WIDEN_TRANS(vfwmacc_vf)
2640GEN_OPFVF_WIDEN_TRANS(vfwnmacc_vf)
2641GEN_OPFVF_WIDEN_TRANS(vfwmsac_vf)
2642GEN_OPFVF_WIDEN_TRANS(vfwnmsac_vf)
2643
2644/* Vector Floating-Point Square-Root Instruction */
2645
2646/*
2647 * If the current SEW does not correspond to a supported IEEE floating-point
2648 * type, an illegal instruction exception is raised
2649 */
2650static bool opfv_check(DisasContext *s, arg_rmr *a)
2651{
2652    return require_rvv(s) &&
2653           require_rvf(s) &&
2654           vext_check_isa_ill(s) &&
2655           /* OPFV instructions ignore vs1 check */
2656           vext_check_ss(s, a->rd, a->rs2, a->vm) &&
2657           require_zve32f(s) &&
2658           require_zve64f(s);
2659}
2660
2661static bool do_opfv(DisasContext *s, arg_rmr *a,
2662                    gen_helper_gvec_3_ptr *fn,
2663                    bool (*checkfn)(DisasContext *, arg_rmr *),
2664                    int rm)
2665{
2666    if (checkfn(s, a)) {
2667        if (rm != RISCV_FRM_DYN) {
2668            gen_set_rm(s, RISCV_FRM_DYN);
2669        }
2670
2671        uint32_t data = 0;
2672        TCGLabel *over = gen_new_label();
2673        gen_set_rm(s, rm);
2674        tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_vl, 0, over);
2675        tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);
2676
2677        data = FIELD_DP32(data, VDATA, VM, a->vm);
2678        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);
2679        data = FIELD_DP32(data, VDATA, VTA, s->vta);
2680        tcg_gen_gvec_3_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),
2681                           vreg_ofs(s, a->rs2), cpu_env,
2682                           s->cfg_ptr->vlen / 8,
2683                           s->cfg_ptr->vlen / 8, data, fn);
2684        mark_vs_dirty(s);
2685        gen_set_label(over);
2686        return true;
2687    }
2688    return false;
2689}
2690
2691#define GEN_OPFV_TRANS(NAME, CHECK, FRM)               \
2692static bool trans_##NAME(DisasContext *s, arg_rmr *a)  \
2693{                                                      \
2694    static gen_helper_gvec_3_ptr * const fns[3] = {    \
2695        gen_helper_##NAME##_h,                         \
2696        gen_helper_##NAME##_w,                         \
2697        gen_helper_##NAME##_d                          \
2698    };                                                 \
2699    return do_opfv(s, a, fns[s->sew - 1], CHECK, FRM); \
2700}
2701
2702GEN_OPFV_TRANS(vfsqrt_v, opfv_check, RISCV_FRM_DYN)
2703GEN_OPFV_TRANS(vfrsqrt7_v, opfv_check, RISCV_FRM_DYN)
2704GEN_OPFV_TRANS(vfrec7_v, opfv_check, RISCV_FRM_DYN)
2705
2706/* Vector Floating-Point MIN/MAX Instructions */
2707GEN_OPFVV_TRANS(vfmin_vv, opfvv_check)
2708GEN_OPFVV_TRANS(vfmax_vv, opfvv_check)
2709GEN_OPFVF_TRANS(vfmin_vf, opfvf_check)
2710GEN_OPFVF_TRANS(vfmax_vf, opfvf_check)
2711
2712/* Vector Floating-Point Sign-Injection Instructions */
2713GEN_OPFVV_TRANS(vfsgnj_vv, opfvv_check)
2714GEN_OPFVV_TRANS(vfsgnjn_vv, opfvv_check)
2715GEN_OPFVV_TRANS(vfsgnjx_vv, opfvv_check)
2716GEN_OPFVF_TRANS(vfsgnj_vf, opfvf_check)
2717GEN_OPFVF_TRANS(vfsgnjn_vf, opfvf_check)
2718GEN_OPFVF_TRANS(vfsgnjx_vf, opfvf_check)
2719
2720/* Vector Floating-Point Compare Instructions */
2721static bool opfvv_cmp_check(DisasContext *s, arg_rmrr *a)
2722{
2723    return require_rvv(s) &&
2724           require_rvf(s) &&
2725           vext_check_isa_ill(s) &&
2726           vext_check_mss(s, a->rd, a->rs1, a->rs2) &&
2727           require_zve32f(s) &&
2728           require_zve64f(s);
2729}
2730
2731GEN_OPFVV_TRANS(vmfeq_vv, opfvv_cmp_check)
2732GEN_OPFVV_TRANS(vmfne_vv, opfvv_cmp_check)
2733GEN_OPFVV_TRANS(vmflt_vv, opfvv_cmp_check)
2734GEN_OPFVV_TRANS(vmfle_vv, opfvv_cmp_check)
2735
2736static bool opfvf_cmp_check(DisasContext *s, arg_rmrr *a)
2737{
2738    return require_rvv(s) &&
2739           require_rvf(s) &&
2740           vext_check_isa_ill(s) &&
2741           vext_check_ms(s, a->rd, a->rs2) &&
2742           require_zve32f(s) &&
2743           require_zve64f(s);
2744}
2745
2746GEN_OPFVF_TRANS(vmfeq_vf, opfvf_cmp_check)
2747GEN_OPFVF_TRANS(vmfne_vf, opfvf_cmp_check)
2748GEN_OPFVF_TRANS(vmflt_vf, opfvf_cmp_check)
2749GEN_OPFVF_TRANS(vmfle_vf, opfvf_cmp_check)
2750GEN_OPFVF_TRANS(vmfgt_vf, opfvf_cmp_check)
2751GEN_OPFVF_TRANS(vmfge_vf, opfvf_cmp_check)
2752
2753/* Vector Floating-Point Classify Instruction */
2754GEN_OPFV_TRANS(vfclass_v, opfv_check, RISCV_FRM_DYN)
2755
2756/* Vector Floating-Point Merge Instruction */
2757GEN_OPFVF_TRANS(vfmerge_vfm,  opfvf_check)
2758
2759static bool trans_vfmv_v_f(DisasContext *s, arg_vfmv_v_f *a)
2760{
2761    if (require_rvv(s) &&
2762        require_rvf(s) &&
2763        vext_check_isa_ill(s) &&
2764        require_align(a->rd, s->lmul) &&
2765        require_zve32f(s) &&
2766        require_zve64f(s)) {
2767        gen_set_rm(s, RISCV_FRM_DYN);
2768
2769        TCGv_i64 t1;
2770
2771        if (s->vl_eq_vlmax && !(s->vta && s->lmul < 0)) {
2772            t1 = tcg_temp_new_i64();
2773            /* NaN-box f[rs1] */
2774            do_nanbox(s, t1, cpu_fpr[a->rs1]);
2775
2776            tcg_gen_gvec_dup_i64(s->sew, vreg_ofs(s, a->rd),
2777                                 MAXSZ(s), MAXSZ(s), t1);
2778            mark_vs_dirty(s);
2779        } else {
2780            TCGv_ptr dest;
2781            TCGv_i32 desc;
2782            uint32_t data = FIELD_DP32(0, VDATA, LMUL, s->lmul);
2783            data = FIELD_DP32(data, VDATA, VTA, s->vta);
2784            static gen_helper_vmv_vx * const fns[3] = {
2785                gen_helper_vmv_v_x_h,
2786                gen_helper_vmv_v_x_w,
2787                gen_helper_vmv_v_x_d,
2788            };
2789            TCGLabel *over = gen_new_label();
2790            tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_vl, 0, over);
2791            tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);
2792
2793            t1 = tcg_temp_new_i64();
2794            /* NaN-box f[rs1] */
2795            do_nanbox(s, t1, cpu_fpr[a->rs1]);
2796
2797            dest = tcg_temp_new_ptr();
2798            desc = tcg_constant_i32(simd_desc(s->cfg_ptr->vlen / 8,
2799                                              s->cfg_ptr->vlen / 8, data));
2800            tcg_gen_addi_ptr(dest, cpu_env, vreg_ofs(s, a->rd));
2801
2802            fns[s->sew - 1](dest, t1, cpu_env, desc);
2803
2804            tcg_temp_free_ptr(dest);
2805            mark_vs_dirty(s);
2806            gen_set_label(over);
2807        }
2808        tcg_temp_free_i64(t1);
2809        return true;
2810    }
2811    return false;
2812}
2813
2814/* Single-Width Floating-Point/Integer Type-Convert Instructions */
2815#define GEN_OPFV_CVT_TRANS(NAME, HELPER, FRM)               \
2816static bool trans_##NAME(DisasContext *s, arg_rmr *a)       \
2817{                                                           \
2818    static gen_helper_gvec_3_ptr * const fns[3] = {         \
2819        gen_helper_##HELPER##_h,                            \
2820        gen_helper_##HELPER##_w,                            \
2821        gen_helper_##HELPER##_d                             \
2822    };                                                      \
2823    return do_opfv(s, a, fns[s->sew - 1], opfv_check, FRM); \
2824}
2825
2826GEN_OPFV_CVT_TRANS(vfcvt_xu_f_v, vfcvt_xu_f_v, RISCV_FRM_DYN)
2827GEN_OPFV_CVT_TRANS(vfcvt_x_f_v, vfcvt_x_f_v, RISCV_FRM_DYN)
2828GEN_OPFV_CVT_TRANS(vfcvt_f_xu_v, vfcvt_f_xu_v, RISCV_FRM_DYN)
2829GEN_OPFV_CVT_TRANS(vfcvt_f_x_v, vfcvt_f_x_v, RISCV_FRM_DYN)
2830/* Reuse the helper functions from vfcvt.xu.f.v and vfcvt.x.f.v */
2831GEN_OPFV_CVT_TRANS(vfcvt_rtz_xu_f_v, vfcvt_xu_f_v, RISCV_FRM_RTZ)
2832GEN_OPFV_CVT_TRANS(vfcvt_rtz_x_f_v, vfcvt_x_f_v, RISCV_FRM_RTZ)
2833
2834/* Widening Floating-Point/Integer Type-Convert Instructions */
2835
2836/*
2837 * If the current SEW does not correspond to a supported IEEE floating-point
2838 * type, an illegal instruction exception is raised
2839 */
2840static bool opfv_widen_check(DisasContext *s, arg_rmr *a)
2841{
2842    return require_rvv(s) &&
2843           vext_check_isa_ill(s) &&
2844           vext_check_ds(s, a->rd, a->rs2, a->vm);
2845}
2846
2847static bool opxfv_widen_check(DisasContext *s, arg_rmr *a)
2848{
2849    return opfv_widen_check(s, a) &&
2850           require_rvf(s) &&
2851           require_zve32f(s) &&
2852           require_zve64f(s);
2853}
2854
2855static bool opffv_widen_check(DisasContext *s, arg_rmr *a)
2856{
2857    return opfv_widen_check(s, a) &&
2858           require_scale_rvf(s) &&
2859           (s->sew != MO_8) &&
2860           require_scale_zve32f(s) &&
2861           require_scale_zve64f(s);
2862}
2863
2864#define GEN_OPFV_WIDEN_TRANS(NAME, CHECK, HELPER, FRM)             \
2865static bool trans_##NAME(DisasContext *s, arg_rmr *a)              \
2866{                                                                  \
2867    if (CHECK(s, a)) {                                             \
2868        if (FRM != RISCV_FRM_DYN) {                                \
2869            gen_set_rm(s, RISCV_FRM_DYN);                          \
2870        }                                                          \
2871                                                                   \
2872        uint32_t data = 0;                                         \
2873        static gen_helper_gvec_3_ptr * const fns[2] = {            \
2874            gen_helper_##HELPER##_h,                               \
2875            gen_helper_##HELPER##_w,                               \
2876        };                                                         \
2877        TCGLabel *over = gen_new_label();                          \
2878        gen_set_rm(s, FRM);                                        \
2879        tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_vl, 0, over);          \
2880        tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over); \
2881                                                                   \
2882        data = FIELD_DP32(data, VDATA, VM, a->vm);                 \
2883        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);             \
2884        data = FIELD_DP32(data, VDATA, VTA, s->vta);               \
2885        tcg_gen_gvec_3_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),     \
2886                           vreg_ofs(s, a->rs2), cpu_env,           \
2887                           s->cfg_ptr->vlen / 8,                   \
2888                           s->cfg_ptr->vlen / 8, data,             \
2889                           fns[s->sew - 1]);                       \
2890        mark_vs_dirty(s);                                          \
2891        gen_set_label(over);                                       \
2892        return true;                                               \
2893    }                                                              \
2894    return false;                                                  \
2895}
2896
2897GEN_OPFV_WIDEN_TRANS(vfwcvt_xu_f_v, opxfv_widen_check, vfwcvt_xu_f_v,
2898                     RISCV_FRM_DYN)
2899GEN_OPFV_WIDEN_TRANS(vfwcvt_x_f_v, opxfv_widen_check, vfwcvt_x_f_v,
2900                     RISCV_FRM_DYN)
2901GEN_OPFV_WIDEN_TRANS(vfwcvt_f_f_v, opffv_widen_check, vfwcvt_f_f_v,
2902                     RISCV_FRM_DYN)
2903/* Reuse the helper functions from vfwcvt.xu.f.v and vfwcvt.x.f.v */
2904GEN_OPFV_WIDEN_TRANS(vfwcvt_rtz_xu_f_v, opxfv_widen_check, vfwcvt_xu_f_v,
2905                     RISCV_FRM_RTZ)
2906GEN_OPFV_WIDEN_TRANS(vfwcvt_rtz_x_f_v, opxfv_widen_check, vfwcvt_x_f_v,
2907                     RISCV_FRM_RTZ)
2908
2909static bool opfxv_widen_check(DisasContext *s, arg_rmr *a)
2910{
2911    return require_rvv(s) &&
2912           require_scale_rvf(s) &&
2913           vext_check_isa_ill(s) &&
2914           /* OPFV widening instructions ignore vs1 check */
2915           vext_check_ds(s, a->rd, a->rs2, a->vm) &&
2916           require_scale_zve32f(s) &&
2917           require_scale_zve64f(s);
2918}
2919
2920#define GEN_OPFXV_WIDEN_TRANS(NAME)                                \
2921static bool trans_##NAME(DisasContext *s, arg_rmr *a)              \
2922{                                                                  \
2923    if (opfxv_widen_check(s, a)) {                                 \
2924        uint32_t data = 0;                                         \
2925        static gen_helper_gvec_3_ptr * const fns[3] = {            \
2926            gen_helper_##NAME##_b,                                 \
2927            gen_helper_##NAME##_h,                                 \
2928            gen_helper_##NAME##_w,                                 \
2929        };                                                         \
2930        TCGLabel *over = gen_new_label();                          \
2931        gen_set_rm(s, RISCV_FRM_DYN);                              \
2932        tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_vl, 0, over);          \
2933        tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over); \
2934                                                                   \
2935        data = FIELD_DP32(data, VDATA, VM, a->vm);                 \
2936        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);             \
2937        data = FIELD_DP32(data, VDATA, VTA, s->vta);               \
2938        tcg_gen_gvec_3_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),     \
2939                           vreg_ofs(s, a->rs2), cpu_env,           \
2940                           s->cfg_ptr->vlen / 8,                   \
2941                           s->cfg_ptr->vlen / 8, data,             \
2942                           fns[s->sew]);                           \
2943        mark_vs_dirty(s);                                          \
2944        gen_set_label(over);                                       \
2945        return true;                                               \
2946    }                                                              \
2947    return false;                                                  \
2948}
2949
2950GEN_OPFXV_WIDEN_TRANS(vfwcvt_f_xu_v)
2951GEN_OPFXV_WIDEN_TRANS(vfwcvt_f_x_v)
2952
2953/* Narrowing Floating-Point/Integer Type-Convert Instructions */
2954
2955/*
2956 * If the current SEW does not correspond to a supported IEEE floating-point
2957 * type, an illegal instruction exception is raised
2958 */
2959static bool opfv_narrow_check(DisasContext *s, arg_rmr *a)
2960{
2961    return require_rvv(s) &&
2962           vext_check_isa_ill(s) &&
2963           /* OPFV narrowing instructions ignore vs1 check */
2964           vext_check_sd(s, a->rd, a->rs2, a->vm);
2965}
2966
2967static bool opfxv_narrow_check(DisasContext *s, arg_rmr *a)
2968{
2969    return opfv_narrow_check(s, a) &&
2970           require_rvf(s) &&
2971           (s->sew != MO_64) &&
2972           require_zve32f(s) &&
2973           require_zve64f(s);
2974}
2975
2976static bool opffv_narrow_check(DisasContext *s, arg_rmr *a)
2977{
2978    return opfv_narrow_check(s, a) &&
2979           require_scale_rvf(s) &&
2980           (s->sew != MO_8) &&
2981           require_scale_zve32f(s) &&
2982           require_scale_zve64f(s);
2983}
2984
2985#define GEN_OPFV_NARROW_TRANS(NAME, CHECK, HELPER, FRM)            \
2986static bool trans_##NAME(DisasContext *s, arg_rmr *a)              \
2987{                                                                  \
2988    if (CHECK(s, a)) {                                             \
2989        if (FRM != RISCV_FRM_DYN) {                                \
2990            gen_set_rm(s, RISCV_FRM_DYN);                          \
2991        }                                                          \
2992                                                                   \
2993        uint32_t data = 0;                                         \
2994        static gen_helper_gvec_3_ptr * const fns[2] = {            \
2995            gen_helper_##HELPER##_h,                               \
2996            gen_helper_##HELPER##_w,                               \
2997        };                                                         \
2998        TCGLabel *over = gen_new_label();                          \
2999        gen_set_rm(s, FRM);                                        \
3000        tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_vl, 0, over);          \
3001        tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over); \
3002                                                                   \
3003        data = FIELD_DP32(data, VDATA, VM, a->vm);                 \
3004        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);             \
3005        data = FIELD_DP32(data, VDATA, VTA, s->vta);               \
3006        tcg_gen_gvec_3_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),     \
3007                           vreg_ofs(s, a->rs2), cpu_env,           \
3008                           s->cfg_ptr->vlen / 8,                   \
3009                           s->cfg_ptr->vlen / 8, data,             \
3010                           fns[s->sew - 1]);                       \
3011        mark_vs_dirty(s);                                          \
3012        gen_set_label(over);                                       \
3013        return true;                                               \
3014    }                                                              \
3015    return false;                                                  \
3016}
3017
3018GEN_OPFV_NARROW_TRANS(vfncvt_f_xu_w, opfxv_narrow_check, vfncvt_f_xu_w,
3019                      RISCV_FRM_DYN)
3020GEN_OPFV_NARROW_TRANS(vfncvt_f_x_w, opfxv_narrow_check, vfncvt_f_x_w,
3021                      RISCV_FRM_DYN)
3022GEN_OPFV_NARROW_TRANS(vfncvt_f_f_w, opffv_narrow_check, vfncvt_f_f_w,
3023                      RISCV_FRM_DYN)
3024/* Reuse the helper function from vfncvt.f.f.w */
3025GEN_OPFV_NARROW_TRANS(vfncvt_rod_f_f_w, opffv_narrow_check, vfncvt_f_f_w,
3026                      RISCV_FRM_ROD)
3027
3028static bool opxfv_narrow_check(DisasContext *s, arg_rmr *a)
3029{
3030    return require_rvv(s) &&
3031           require_scale_rvf(s) &&
3032           vext_check_isa_ill(s) &&
3033           /* OPFV narrowing instructions ignore vs1 check */
3034           vext_check_sd(s, a->rd, a->rs2, a->vm) &&
3035           require_scale_zve32f(s) &&
3036           require_scale_zve64f(s);
3037}
3038
3039#define GEN_OPXFV_NARROW_TRANS(NAME, HELPER, FRM)                  \
3040static bool trans_##NAME(DisasContext *s, arg_rmr *a)              \
3041{                                                                  \
3042    if (opxfv_narrow_check(s, a)) {                                \
3043        if (FRM != RISCV_FRM_DYN) {                                \
3044            gen_set_rm(s, RISCV_FRM_DYN);                          \
3045        }                                                          \
3046                                                                   \
3047        uint32_t data = 0;                                         \
3048        static gen_helper_gvec_3_ptr * const fns[3] = {            \
3049            gen_helper_##HELPER##_b,                               \
3050            gen_helper_##HELPER##_h,                               \
3051            gen_helper_##HELPER##_w,                               \
3052        };                                                         \
3053        TCGLabel *over = gen_new_label();                          \
3054        gen_set_rm(s, FRM);                                        \
3055        tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_vl, 0, over);          \
3056        tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over); \
3057                                                                   \
3058        data = FIELD_DP32(data, VDATA, VM, a->vm);                 \
3059        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);             \
3060        data = FIELD_DP32(data, VDATA, VTA, s->vta);               \
3061        tcg_gen_gvec_3_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),     \
3062                           vreg_ofs(s, a->rs2), cpu_env,           \
3063                           s->cfg_ptr->vlen / 8,                   \
3064                           s->cfg_ptr->vlen / 8, data,             \
3065                           fns[s->sew]);                           \
3066        mark_vs_dirty(s);                                          \
3067        gen_set_label(over);                                       \
3068        return true;                                               \
3069    }                                                              \
3070    return false;                                                  \
3071}
3072
3073GEN_OPXFV_NARROW_TRANS(vfncvt_xu_f_w, vfncvt_xu_f_w, RISCV_FRM_DYN)
3074GEN_OPXFV_NARROW_TRANS(vfncvt_x_f_w, vfncvt_x_f_w, RISCV_FRM_DYN)
3075/* Reuse the helper functions from vfncvt.xu.f.w and vfncvt.x.f.w */
3076GEN_OPXFV_NARROW_TRANS(vfncvt_rtz_xu_f_w, vfncvt_xu_f_w, RISCV_FRM_RTZ)
3077GEN_OPXFV_NARROW_TRANS(vfncvt_rtz_x_f_w, vfncvt_x_f_w, RISCV_FRM_RTZ)
3078
3079/*
3080 *** Vector Reduction Operations
3081 */
3082/* Vector Single-Width Integer Reduction Instructions */
3083static bool reduction_check(DisasContext *s, arg_rmrr *a)
3084{
3085    return require_rvv(s) &&
3086           vext_check_isa_ill(s) &&
3087           vext_check_reduction(s, a->rs2);
3088}
3089
3090GEN_OPIVV_TRANS(vredsum_vs, reduction_check)
3091GEN_OPIVV_TRANS(vredmaxu_vs, reduction_check)
3092GEN_OPIVV_TRANS(vredmax_vs, reduction_check)
3093GEN_OPIVV_TRANS(vredminu_vs, reduction_check)
3094GEN_OPIVV_TRANS(vredmin_vs, reduction_check)
3095GEN_OPIVV_TRANS(vredand_vs, reduction_check)
3096GEN_OPIVV_TRANS(vredor_vs, reduction_check)
3097GEN_OPIVV_TRANS(vredxor_vs, reduction_check)
3098
3099/* Vector Widening Integer Reduction Instructions */
3100static bool reduction_widen_check(DisasContext *s, arg_rmrr *a)
3101{
3102    return reduction_check(s, a) && (s->sew < MO_64) &&
3103           ((s->sew + 1) <= (s->cfg_ptr->elen >> 4));
3104}
3105
3106GEN_OPIVV_WIDEN_TRANS(vwredsum_vs, reduction_widen_check)
3107GEN_OPIVV_WIDEN_TRANS(vwredsumu_vs, reduction_widen_check)
3108
3109/* Vector Single-Width Floating-Point Reduction Instructions */
3110static bool freduction_check(DisasContext *s, arg_rmrr *a)
3111{
3112    return reduction_check(s, a) &&
3113           require_rvf(s) &&
3114           require_zve32f(s) &&
3115           require_zve64f(s);
3116}
3117
3118GEN_OPFVV_TRANS(vfredsum_vs, freduction_check)
3119GEN_OPFVV_TRANS(vfredmax_vs, freduction_check)
3120GEN_OPFVV_TRANS(vfredmin_vs, freduction_check)
3121
3122/* Vector Widening Floating-Point Reduction Instructions */
3123static bool freduction_widen_check(DisasContext *s, arg_rmrr *a)
3124{
3125    return reduction_widen_check(s, a) &&
3126           require_scale_rvf(s) &&
3127           (s->sew != MO_8);
3128}
3129
3130GEN_OPFVV_WIDEN_TRANS(vfwredsum_vs, freduction_widen_check)
3131
3132/*
3133 *** Vector Mask Operations
3134 */
3135
3136/* Vector Mask-Register Logical Instructions */
3137#define GEN_MM_TRANS(NAME)                                         \
3138static bool trans_##NAME(DisasContext *s, arg_r *a)                \
3139{                                                                  \
3140    if (require_rvv(s) &&                                          \
3141        vext_check_isa_ill(s)) {                                   \
3142        uint32_t data = 0;                                         \
3143        gen_helper_gvec_4_ptr *fn = gen_helper_##NAME;             \
3144        TCGLabel *over = gen_new_label();                          \
3145        tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_vl, 0, over);          \
3146        tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over); \
3147                                                                   \
3148        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);             \
3149        data =                                                     \
3150            FIELD_DP32(data, VDATA, VTA_ALL_1S, s->cfg_vta_all_1s);\
3151        tcg_gen_gvec_4_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),     \
3152                           vreg_ofs(s, a->rs1),                    \
3153                           vreg_ofs(s, a->rs2), cpu_env,           \
3154                           s->cfg_ptr->vlen / 8,                   \
3155                           s->cfg_ptr->vlen / 8, data, fn);        \
3156        mark_vs_dirty(s);                                          \
3157        gen_set_label(over);                                       \
3158        return true;                                               \
3159    }                                                              \
3160    return false;                                                  \
3161}
3162
3163GEN_MM_TRANS(vmand_mm)
3164GEN_MM_TRANS(vmnand_mm)
3165GEN_MM_TRANS(vmandn_mm)
3166GEN_MM_TRANS(vmxor_mm)
3167GEN_MM_TRANS(vmor_mm)
3168GEN_MM_TRANS(vmnor_mm)
3169GEN_MM_TRANS(vmorn_mm)
3170GEN_MM_TRANS(vmxnor_mm)
3171
3172/* Vector count population in mask vcpop */
3173static bool trans_vcpop_m(DisasContext *s, arg_rmr *a)
3174{
3175    if (require_rvv(s) &&
3176        vext_check_isa_ill(s) &&
3177        s->vstart == 0) {
3178        TCGv_ptr src2, mask;
3179        TCGv dst;
3180        TCGv_i32 desc;
3181        uint32_t data = 0;
3182        data = FIELD_DP32(data, VDATA, VM, a->vm);
3183        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);
3184
3185        mask = tcg_temp_new_ptr();
3186        src2 = tcg_temp_new_ptr();
3187        dst = dest_gpr(s, a->rd);
3188        desc = tcg_constant_i32(simd_desc(s->cfg_ptr->vlen / 8,
3189                                          s->cfg_ptr->vlen / 8, data));
3190
3191        tcg_gen_addi_ptr(src2, cpu_env, vreg_ofs(s, a->rs2));
3192        tcg_gen_addi_ptr(mask, cpu_env, vreg_ofs(s, 0));
3193
3194        gen_helper_vcpop_m(dst, mask, src2, cpu_env, desc);
3195        gen_set_gpr(s, a->rd, dst);
3196
3197        tcg_temp_free_ptr(mask);
3198        tcg_temp_free_ptr(src2);
3199
3200        return true;
3201    }
3202    return false;
3203}
3204
3205/* vmfirst find-first-set mask bit */
3206static bool trans_vfirst_m(DisasContext *s, arg_rmr *a)
3207{
3208    if (require_rvv(s) &&
3209        vext_check_isa_ill(s) &&
3210        s->vstart == 0) {
3211        TCGv_ptr src2, mask;
3212        TCGv dst;
3213        TCGv_i32 desc;
3214        uint32_t data = 0;
3215        data = FIELD_DP32(data, VDATA, VM, a->vm);
3216        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);
3217
3218        mask = tcg_temp_new_ptr();
3219        src2 = tcg_temp_new_ptr();
3220        dst = dest_gpr(s, a->rd);
3221        desc = tcg_constant_i32(simd_desc(s->cfg_ptr->vlen / 8,
3222                                          s->cfg_ptr->vlen / 8, data));
3223
3224        tcg_gen_addi_ptr(src2, cpu_env, vreg_ofs(s, a->rs2));
3225        tcg_gen_addi_ptr(mask, cpu_env, vreg_ofs(s, 0));
3226
3227        gen_helper_vfirst_m(dst, mask, src2, cpu_env, desc);
3228        gen_set_gpr(s, a->rd, dst);
3229
3230        tcg_temp_free_ptr(mask);
3231        tcg_temp_free_ptr(src2);
3232        return true;
3233    }
3234    return false;
3235}
3236
3237/* vmsbf.m set-before-first mask bit */
3238/* vmsif.m set-includ-first mask bit */
3239/* vmsof.m set-only-first mask bit */
3240#define GEN_M_TRANS(NAME)                                          \
3241static bool trans_##NAME(DisasContext *s, arg_rmr *a)              \
3242{                                                                  \
3243    if (require_rvv(s) &&                                          \
3244        vext_check_isa_ill(s) &&                                   \
3245        require_vm(a->vm, a->rd) &&                                \
3246        (a->rd != a->rs2) &&                                       \
3247        (s->vstart == 0)) {                                        \
3248        uint32_t data = 0;                                         \
3249        gen_helper_gvec_3_ptr *fn = gen_helper_##NAME;             \
3250        TCGLabel *over = gen_new_label();                          \
3251        tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_vl, 0, over);          \
3252                                                                   \
3253        data = FIELD_DP32(data, VDATA, VM, a->vm);                 \
3254        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);             \
3255        data =                                                     \
3256            FIELD_DP32(data, VDATA, VTA_ALL_1S, s->cfg_vta_all_1s);\
3257        tcg_gen_gvec_3_ptr(vreg_ofs(s, a->rd),                     \
3258                           vreg_ofs(s, 0), vreg_ofs(s, a->rs2),    \
3259                           cpu_env, s->cfg_ptr->vlen / 8,          \
3260                           s->cfg_ptr->vlen / 8,                   \
3261                           data, fn);                              \
3262        mark_vs_dirty(s);                                          \
3263        gen_set_label(over);                                       \
3264        return true;                                               \
3265    }                                                              \
3266    return false;                                                  \
3267}
3268
3269GEN_M_TRANS(vmsbf_m)
3270GEN_M_TRANS(vmsif_m)
3271GEN_M_TRANS(vmsof_m)
3272
3273/*
3274 * Vector Iota Instruction
3275 *
3276 * 1. The destination register cannot overlap the source register.
3277 * 2. If masked, cannot overlap the mask register ('v0').
3278 * 3. An illegal instruction exception is raised if vstart is non-zero.
3279 */
3280static bool trans_viota_m(DisasContext *s, arg_viota_m *a)
3281{
3282    if (require_rvv(s) &&
3283        vext_check_isa_ill(s) &&
3284        !is_overlapped(a->rd, 1 << MAX(s->lmul, 0), a->rs2, 1) &&
3285        require_vm(a->vm, a->rd) &&
3286        require_align(a->rd, s->lmul) &&
3287        (s->vstart == 0)) {
3288        uint32_t data = 0;
3289        TCGLabel *over = gen_new_label();
3290        tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_vl, 0, over);
3291
3292        data = FIELD_DP32(data, VDATA, VM, a->vm);
3293        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);
3294        data = FIELD_DP32(data, VDATA, VTA, s->vta);
3295        static gen_helper_gvec_3_ptr * const fns[4] = {
3296            gen_helper_viota_m_b, gen_helper_viota_m_h,
3297            gen_helper_viota_m_w, gen_helper_viota_m_d,
3298        };
3299        tcg_gen_gvec_3_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),
3300                           vreg_ofs(s, a->rs2), cpu_env,
3301                           s->cfg_ptr->vlen / 8,
3302                           s->cfg_ptr->vlen / 8, data, fns[s->sew]);
3303        mark_vs_dirty(s);
3304        gen_set_label(over);
3305        return true;
3306    }
3307    return false;
3308}
3309
3310/* Vector Element Index Instruction */
3311static bool trans_vid_v(DisasContext *s, arg_vid_v *a)
3312{
3313    if (require_rvv(s) &&
3314        vext_check_isa_ill(s) &&
3315        require_align(a->rd, s->lmul) &&
3316        require_vm(a->vm, a->rd)) {
3317        uint32_t data = 0;
3318        TCGLabel *over = gen_new_label();
3319        tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_vl, 0, over);
3320        tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);
3321
3322        data = FIELD_DP32(data, VDATA, VM, a->vm);
3323        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);
3324        data = FIELD_DP32(data, VDATA, VTA, s->vta);
3325        static gen_helper_gvec_2_ptr * const fns[4] = {
3326            gen_helper_vid_v_b, gen_helper_vid_v_h,
3327            gen_helper_vid_v_w, gen_helper_vid_v_d,
3328        };
3329        tcg_gen_gvec_2_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),
3330                           cpu_env, s->cfg_ptr->vlen / 8,
3331                           s->cfg_ptr->vlen / 8,
3332                           data, fns[s->sew]);
3333        mark_vs_dirty(s);
3334        gen_set_label(over);
3335        return true;
3336    }
3337    return false;
3338}
3339
3340/*
3341 *** Vector Permutation Instructions
3342 */
3343
3344static void load_element(TCGv_i64 dest, TCGv_ptr base,
3345                         int ofs, int sew, bool sign)
3346{
3347    switch (sew) {
3348    case MO_8:
3349        if (!sign) {
3350            tcg_gen_ld8u_i64(dest, base, ofs);
3351        } else {
3352            tcg_gen_ld8s_i64(dest, base, ofs);
3353        }
3354        break;
3355    case MO_16:
3356        if (!sign) {
3357            tcg_gen_ld16u_i64(dest, base, ofs);
3358        } else {
3359            tcg_gen_ld16s_i64(dest, base, ofs);
3360        }
3361        break;
3362    case MO_32:
3363        if (!sign) {
3364            tcg_gen_ld32u_i64(dest, base, ofs);
3365        } else {
3366            tcg_gen_ld32s_i64(dest, base, ofs);
3367        }
3368        break;
3369    case MO_64:
3370        tcg_gen_ld_i64(dest, base, ofs);
3371        break;
3372    default:
3373        g_assert_not_reached();
3374        break;
3375    }
3376}
3377
3378/* offset of the idx element with base regsiter r */
3379static uint32_t endian_ofs(DisasContext *s, int r, int idx)
3380{
3381#if HOST_BIG_ENDIAN
3382    return vreg_ofs(s, r) + ((idx ^ (7 >> s->sew)) << s->sew);
3383#else
3384    return vreg_ofs(s, r) + (idx << s->sew);
3385#endif
3386}
3387
3388/* adjust the index according to the endian */
3389static void endian_adjust(TCGv_i32 ofs, int sew)
3390{
3391#if HOST_BIG_ENDIAN
3392    tcg_gen_xori_i32(ofs, ofs, 7 >> sew);
3393#endif
3394}
3395
3396/* Load idx >= VLMAX ? 0 : vreg[idx] */
3397static void vec_element_loadx(DisasContext *s, TCGv_i64 dest,
3398                              int vreg, TCGv idx, int vlmax)
3399{
3400    TCGv_i32 ofs = tcg_temp_new_i32();
3401    TCGv_ptr base = tcg_temp_new_ptr();
3402    TCGv_i64 t_idx = tcg_temp_new_i64();
3403    TCGv_i64 t_vlmax, t_zero;
3404
3405    /*
3406     * Mask the index to the length so that we do
3407     * not produce an out-of-range load.
3408     */
3409    tcg_gen_trunc_tl_i32(ofs, idx);
3410    tcg_gen_andi_i32(ofs, ofs, vlmax - 1);
3411
3412    /* Convert the index to an offset. */
3413    endian_adjust(ofs, s->sew);
3414    tcg_gen_shli_i32(ofs, ofs, s->sew);
3415
3416    /* Convert the index to a pointer. */
3417    tcg_gen_ext_i32_ptr(base, ofs);
3418    tcg_gen_add_ptr(base, base, cpu_env);
3419
3420    /* Perform the load. */
3421    load_element(dest, base,
3422                 vreg_ofs(s, vreg), s->sew, false);
3423    tcg_temp_free_ptr(base);
3424    tcg_temp_free_i32(ofs);
3425
3426    /* Flush out-of-range indexing to zero.  */
3427    t_vlmax = tcg_constant_i64(vlmax);
3428    t_zero = tcg_constant_i64(0);
3429    tcg_gen_extu_tl_i64(t_idx, idx);
3430
3431    tcg_gen_movcond_i64(TCG_COND_LTU, dest, t_idx,
3432                        t_vlmax, dest, t_zero);
3433
3434    tcg_temp_free_i64(t_idx);
3435}
3436
3437static void vec_element_loadi(DisasContext *s, TCGv_i64 dest,
3438                              int vreg, int idx, bool sign)
3439{
3440    load_element(dest, cpu_env, endian_ofs(s, vreg, idx), s->sew, sign);
3441}
3442
3443/* Integer Scalar Move Instruction */
3444
3445static void store_element(TCGv_i64 val, TCGv_ptr base,
3446                          int ofs, int sew)
3447{
3448    switch (sew) {
3449    case MO_8:
3450        tcg_gen_st8_i64(val, base, ofs);
3451        break;
3452    case MO_16:
3453        tcg_gen_st16_i64(val, base, ofs);
3454        break;
3455    case MO_32:
3456        tcg_gen_st32_i64(val, base, ofs);
3457        break;
3458    case MO_64:
3459        tcg_gen_st_i64(val, base, ofs);
3460        break;
3461    default:
3462        g_assert_not_reached();
3463        break;
3464    }
3465}
3466
3467/*
3468 * Store vreg[idx] = val.
3469 * The index must be in range of VLMAX.
3470 */
3471static void vec_element_storei(DisasContext *s, int vreg,
3472                               int idx, TCGv_i64 val)
3473{
3474    store_element(val, cpu_env, endian_ofs(s, vreg, idx), s->sew);
3475}
3476
3477/* vmv.x.s rd, vs2 # x[rd] = vs2[0] */
3478static bool trans_vmv_x_s(DisasContext *s, arg_vmv_x_s *a)
3479{
3480    if (require_rvv(s) &&
3481        vext_check_isa_ill(s)) {
3482        TCGv_i64 t1;
3483        TCGv dest;
3484
3485        t1 = tcg_temp_new_i64();
3486        dest = tcg_temp_new();
3487        /*
3488         * load vreg and sign-extend to 64 bits,
3489         * then truncate to XLEN bits before storing to gpr.
3490         */
3491        vec_element_loadi(s, t1, a->rs2, 0, true);
3492        tcg_gen_trunc_i64_tl(dest, t1);
3493        gen_set_gpr(s, a->rd, dest);
3494        tcg_temp_free_i64(t1);
3495        tcg_temp_free(dest);
3496
3497        return true;
3498    }
3499    return false;
3500}
3501
3502/* vmv.s.x vd, rs1 # vd[0] = rs1 */
3503static bool trans_vmv_s_x(DisasContext *s, arg_vmv_s_x *a)
3504{
3505    if (require_rvv(s) &&
3506        vext_check_isa_ill(s)) {
3507        /* This instruction ignores LMUL and vector register groups */
3508        TCGv_i64 t1;
3509        TCGv s1;
3510        TCGLabel *over = gen_new_label();
3511
3512        tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_vl, 0, over);
3513        tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);
3514
3515        t1 = tcg_temp_new_i64();
3516
3517        /*
3518         * load gpr and sign-extend to 64 bits,
3519         * then truncate to SEW bits when storing to vreg.
3520         */
3521        s1 = get_gpr(s, a->rs1, EXT_NONE);
3522        tcg_gen_ext_tl_i64(t1, s1);
3523        vec_element_storei(s, a->rd, 0, t1);
3524        tcg_temp_free_i64(t1);
3525        mark_vs_dirty(s);
3526        gen_set_label(over);
3527        return true;
3528    }
3529    return false;
3530}
3531
3532/* Floating-Point Scalar Move Instructions */
3533static bool trans_vfmv_f_s(DisasContext *s, arg_vfmv_f_s *a)
3534{
3535    if (require_rvv(s) &&
3536        require_rvf(s) &&
3537        vext_check_isa_ill(s) &&
3538        require_zve32f(s) &&
3539        require_zve64f(s)) {
3540        gen_set_rm(s, RISCV_FRM_DYN);
3541
3542        unsigned int ofs = (8 << s->sew);
3543        unsigned int len = 64 - ofs;
3544        TCGv_i64 t_nan;
3545
3546        vec_element_loadi(s, cpu_fpr[a->rd], a->rs2, 0, false);
3547        /* NaN-box f[rd] as necessary for SEW */
3548        if (len) {
3549            t_nan = tcg_constant_i64(UINT64_MAX);
3550            tcg_gen_deposit_i64(cpu_fpr[a->rd], cpu_fpr[a->rd],
3551                                t_nan, ofs, len);
3552        }
3553
3554        mark_fs_dirty(s);
3555        return true;
3556    }
3557    return false;
3558}
3559
3560/* vfmv.s.f vd, rs1 # vd[0] = rs1 (vs2=0) */
3561static bool trans_vfmv_s_f(DisasContext *s, arg_vfmv_s_f *a)
3562{
3563    if (require_rvv(s) &&
3564        require_rvf(s) &&
3565        vext_check_isa_ill(s) &&
3566        require_zve32f(s) &&
3567        require_zve64f(s)) {
3568        gen_set_rm(s, RISCV_FRM_DYN);
3569
3570        /* The instructions ignore LMUL and vector register group. */
3571        TCGv_i64 t1;
3572        TCGLabel *over = gen_new_label();
3573
3574        /* if vl == 0 or vstart >= vl, skip vector register write back */
3575        tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_vl, 0, over);
3576        tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);
3577
3578        /* NaN-box f[rs1] */
3579        t1 = tcg_temp_new_i64();
3580        do_nanbox(s, t1, cpu_fpr[a->rs1]);
3581
3582        vec_element_storei(s, a->rd, 0, t1);
3583        tcg_temp_free_i64(t1);
3584        mark_vs_dirty(s);
3585        gen_set_label(over);
3586        return true;
3587    }
3588    return false;
3589}
3590
3591/* Vector Slide Instructions */
3592static bool slideup_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, true);
3597}
3598
3599GEN_OPIVX_TRANS(vslideup_vx, slideup_check)
3600GEN_OPIVX_TRANS(vslide1up_vx, slideup_check)
3601GEN_OPIVI_TRANS(vslideup_vi, IMM_ZX, vslideup_vx, slideup_check)
3602
3603static bool slidedown_check(DisasContext *s, arg_rmrr *a)
3604{
3605    return require_rvv(s) &&
3606           vext_check_isa_ill(s) &&
3607           vext_check_slide(s, a->rd, a->rs2, a->vm, false);
3608}
3609
3610GEN_OPIVX_TRANS(vslidedown_vx, slidedown_check)
3611GEN_OPIVX_TRANS(vslide1down_vx, slidedown_check)
3612GEN_OPIVI_TRANS(vslidedown_vi, IMM_ZX, vslidedown_vx, slidedown_check)
3613
3614/* Vector Floating-Point Slide Instructions */
3615static bool fslideup_check(DisasContext *s, arg_rmrr *a)
3616{
3617    return slideup_check(s, a) &&
3618           require_rvf(s) &&
3619           require_zve32f(s) &&
3620           require_zve64f(s);
3621}
3622
3623static bool fslidedown_check(DisasContext *s, arg_rmrr *a)
3624{
3625    return slidedown_check(s, a) &&
3626           require_rvf(s) &&
3627           require_zve32f(s) &&
3628           require_zve64f(s);
3629}
3630
3631GEN_OPFVF_TRANS(vfslide1up_vf, fslideup_check)
3632GEN_OPFVF_TRANS(vfslide1down_vf, fslidedown_check)
3633
3634/* Vector Register Gather Instruction */
3635static bool vrgather_vv_check(DisasContext *s, arg_rmrr *a)
3636{
3637    return require_rvv(s) &&
3638           vext_check_isa_ill(s) &&
3639           require_align(a->rd, s->lmul) &&
3640           require_align(a->rs1, s->lmul) &&
3641           require_align(a->rs2, s->lmul) &&
3642           (a->rd != a->rs2 && a->rd != a->rs1) &&
3643           require_vm(a->vm, a->rd);
3644}
3645
3646static bool vrgatherei16_vv_check(DisasContext *s, arg_rmrr *a)
3647{
3648    int8_t emul = MO_16 - s->sew + s->lmul;
3649    return require_rvv(s) &&
3650           vext_check_isa_ill(s) &&
3651           (emul >= -3 && emul <= 3) &&
3652           require_align(a->rd, s->lmul) &&
3653           require_align(a->rs1, emul) &&
3654           require_align(a->rs2, s->lmul) &&
3655           (a->rd != a->rs2 && a->rd != a->rs1) &&
3656           !is_overlapped(a->rd, 1 << MAX(s->lmul, 0),
3657                          a->rs1, 1 << MAX(emul, 0)) &&
3658           !is_overlapped(a->rd, 1 << MAX(s->lmul, 0),
3659                          a->rs2, 1 << MAX(s->lmul, 0)) &&
3660           require_vm(a->vm, a->rd);
3661}
3662
3663GEN_OPIVV_TRANS(vrgather_vv, vrgather_vv_check)
3664GEN_OPIVV_TRANS(vrgatherei16_vv, vrgatherei16_vv_check)
3665
3666static bool vrgather_vx_check(DisasContext *s, arg_rmrr *a)
3667{
3668    return require_rvv(s) &&
3669           vext_check_isa_ill(s) &&
3670           require_align(a->rd, s->lmul) &&
3671           require_align(a->rs2, s->lmul) &&
3672           (a->rd != a->rs2) &&
3673           require_vm(a->vm, a->rd);
3674}
3675
3676/* vrgather.vx vd, vs2, rs1, vm # vd[i] = (x[rs1] >= VLMAX) ? 0 : vs2[rs1] */
3677static bool trans_vrgather_vx(DisasContext *s, arg_rmrr *a)
3678{
3679    if (!vrgather_vx_check(s, a)) {
3680        return false;
3681    }
3682
3683    if (a->vm && s->vl_eq_vlmax && !(s->vta && s->lmul < 0)) {
3684        int scale = s->lmul - (s->sew + 3);
3685        int vlmax = s->cfg_ptr->vlen >> -scale;
3686        TCGv_i64 dest = tcg_temp_new_i64();
3687
3688        if (a->rs1 == 0) {
3689            vec_element_loadi(s, dest, a->rs2, 0, false);
3690        } else {
3691            vec_element_loadx(s, dest, a->rs2, cpu_gpr[a->rs1], vlmax);
3692        }
3693
3694        tcg_gen_gvec_dup_i64(s->sew, vreg_ofs(s, a->rd),
3695                             MAXSZ(s), MAXSZ(s), dest);
3696        tcg_temp_free_i64(dest);
3697        mark_vs_dirty(s);
3698    } else {
3699        static gen_helper_opivx * const fns[4] = {
3700            gen_helper_vrgather_vx_b, gen_helper_vrgather_vx_h,
3701            gen_helper_vrgather_vx_w, gen_helper_vrgather_vx_d
3702        };
3703        return opivx_trans(a->rd, a->rs1, a->rs2, a->vm, fns[s->sew], s);
3704    }
3705    return true;
3706}
3707
3708/* vrgather.vi vd, vs2, imm, vm # vd[i] = (imm >= VLMAX) ? 0 : vs2[imm] */
3709static bool trans_vrgather_vi(DisasContext *s, arg_rmrr *a)
3710{
3711    if (!vrgather_vx_check(s, a)) {
3712        return false;
3713    }
3714
3715    if (a->vm && s->vl_eq_vlmax && !(s->vta && s->lmul < 0)) {
3716        int scale = s->lmul - (s->sew + 3);
3717        int vlmax = s->cfg_ptr->vlen >> -scale;
3718        if (a->rs1 >= vlmax) {
3719            tcg_gen_gvec_dup_imm(MO_64, vreg_ofs(s, a->rd),
3720                                 MAXSZ(s), MAXSZ(s), 0);
3721        } else {
3722            tcg_gen_gvec_dup_mem(s->sew, vreg_ofs(s, a->rd),
3723                                 endian_ofs(s, a->rs2, a->rs1),
3724                                 MAXSZ(s), MAXSZ(s));
3725        }
3726        mark_vs_dirty(s);
3727    } else {
3728        static gen_helper_opivx * const fns[4] = {
3729            gen_helper_vrgather_vx_b, gen_helper_vrgather_vx_h,
3730            gen_helper_vrgather_vx_w, gen_helper_vrgather_vx_d
3731        };
3732        return opivi_trans(a->rd, a->rs1, a->rs2, a->vm, fns[s->sew],
3733                           s, IMM_ZX);
3734    }
3735    return true;
3736}
3737
3738/*
3739 * Vector Compress Instruction
3740 *
3741 * The destination vector register group cannot overlap the
3742 * source vector register group or the source mask register.
3743 */
3744static bool vcompress_vm_check(DisasContext *s, arg_r *a)
3745{
3746    return require_rvv(s) &&
3747           vext_check_isa_ill(s) &&
3748           require_align(a->rd, s->lmul) &&
3749           require_align(a->rs2, s->lmul) &&
3750           (a->rd != a->rs2) &&
3751           !is_overlapped(a->rd, 1 << MAX(s->lmul, 0), a->rs1, 1) &&
3752           (s->vstart == 0);
3753}
3754
3755static bool trans_vcompress_vm(DisasContext *s, arg_r *a)
3756{
3757    if (vcompress_vm_check(s, a)) {
3758        uint32_t data = 0;
3759        static gen_helper_gvec_4_ptr * const fns[4] = {
3760            gen_helper_vcompress_vm_b, gen_helper_vcompress_vm_h,
3761            gen_helper_vcompress_vm_w, gen_helper_vcompress_vm_d,
3762        };
3763        TCGLabel *over = gen_new_label();
3764        tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_vl, 0, over);
3765
3766        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);
3767        data = FIELD_DP32(data, VDATA, VTA, s->vta);
3768        tcg_gen_gvec_4_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),
3769                           vreg_ofs(s, a->rs1), vreg_ofs(s, a->rs2),
3770                           cpu_env, s->cfg_ptr->vlen / 8,
3771                           s->cfg_ptr->vlen / 8, data,
3772                           fns[s->sew]);
3773        mark_vs_dirty(s);
3774        gen_set_label(over);
3775        return true;
3776    }
3777    return false;
3778}
3779
3780/*
3781 * Whole Vector Register Move Instructions ignore vtype and vl setting.
3782 * Thus, we don't need to check vill bit. (Section 16.6)
3783 */
3784#define GEN_VMV_WHOLE_TRANS(NAME, LEN)                             \
3785static bool trans_##NAME(DisasContext *s, arg_##NAME * a)               \
3786{                                                                       \
3787    if (require_rvv(s) &&                                               \
3788        QEMU_IS_ALIGNED(a->rd, LEN) &&                                  \
3789        QEMU_IS_ALIGNED(a->rs2, LEN)) {                                 \
3790        uint32_t maxsz = (s->cfg_ptr->vlen >> 3) * LEN;                 \
3791        if (s->vstart == 0) {                                           \
3792            /* EEW = 8 */                                               \
3793            tcg_gen_gvec_mov(MO_8, vreg_ofs(s, a->rd),                  \
3794                             vreg_ofs(s, a->rs2), maxsz, maxsz);        \
3795            mark_vs_dirty(s);                                           \
3796        } else {                                                        \
3797            TCGLabel *over = gen_new_label();                           \
3798            tcg_gen_brcondi_tl(TCG_COND_GEU, cpu_vstart, maxsz, over);  \
3799            tcg_gen_gvec_2_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, a->rs2), \
3800                               cpu_env, maxsz, maxsz, 0, gen_helper_vmvr_v); \
3801            mark_vs_dirty(s);                                           \
3802            gen_set_label(over);                                        \
3803        }                                                               \
3804        return true;                                                    \
3805    }                                                                   \
3806    return false;                                                       \
3807}
3808
3809GEN_VMV_WHOLE_TRANS(vmv1r_v, 1)
3810GEN_VMV_WHOLE_TRANS(vmv2r_v, 2)
3811GEN_VMV_WHOLE_TRANS(vmv4r_v, 4)
3812GEN_VMV_WHOLE_TRANS(vmv8r_v, 8)
3813
3814static bool int_ext_check(DisasContext *s, arg_rmr *a, uint8_t div)
3815{
3816    uint8_t from = (s->sew + 3) - div;
3817    bool ret = require_rvv(s) &&
3818        (from >= 3 && from <= 8) &&
3819        (a->rd != a->rs2) &&
3820        require_align(a->rd, s->lmul) &&
3821        require_align(a->rs2, s->lmul - div) &&
3822        require_vm(a->vm, a->rd) &&
3823        require_noover(a->rd, s->lmul, a->rs2, s->lmul - div);
3824    return ret;
3825}
3826
3827static bool int_ext_op(DisasContext *s, arg_rmr *a, uint8_t seq)
3828{
3829    uint32_t data = 0;
3830    gen_helper_gvec_3_ptr *fn;
3831    TCGLabel *over = gen_new_label();
3832    tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_vl, 0, over);
3833    tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);
3834
3835    static gen_helper_gvec_3_ptr * const fns[6][4] = {
3836        {
3837            NULL, gen_helper_vzext_vf2_h,
3838            gen_helper_vzext_vf2_w, gen_helper_vzext_vf2_d
3839        },
3840        {
3841            NULL, NULL,
3842            gen_helper_vzext_vf4_w, gen_helper_vzext_vf4_d,
3843        },
3844        {
3845            NULL, NULL,
3846            NULL, gen_helper_vzext_vf8_d
3847        },
3848        {
3849            NULL, gen_helper_vsext_vf2_h,
3850            gen_helper_vsext_vf2_w, gen_helper_vsext_vf2_d
3851        },
3852        {
3853            NULL, NULL,
3854            gen_helper_vsext_vf4_w, gen_helper_vsext_vf4_d,
3855        },
3856        {
3857            NULL, NULL,
3858            NULL, gen_helper_vsext_vf8_d
3859        }
3860    };
3861
3862    fn = fns[seq][s->sew];
3863    if (fn == NULL) {
3864        return false;
3865    }
3866
3867    data = FIELD_DP32(data, VDATA, VM, a->vm);
3868    data = FIELD_DP32(data, VDATA, LMUL, s->lmul);
3869    data = FIELD_DP32(data, VDATA, VTA, s->vta);
3870
3871    tcg_gen_gvec_3_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),
3872                       vreg_ofs(s, a->rs2), cpu_env,
3873                       s->cfg_ptr->vlen / 8,
3874                       s->cfg_ptr->vlen / 8, data, fn);
3875
3876    mark_vs_dirty(s);
3877    gen_set_label(over);
3878    return true;
3879}
3880
3881/* Vector Integer Extension */
3882#define GEN_INT_EXT_TRANS(NAME, DIV, SEQ)             \
3883static bool trans_##NAME(DisasContext *s, arg_rmr *a) \
3884{                                                     \
3885    if (int_ext_check(s, a, DIV)) {                   \
3886        return int_ext_op(s, a, SEQ);                 \
3887    }                                                 \
3888    return false;                                     \
3889}
3890
3891GEN_INT_EXT_TRANS(vzext_vf2, 1, 0)
3892GEN_INT_EXT_TRANS(vzext_vf4, 2, 1)
3893GEN_INT_EXT_TRANS(vzext_vf8, 3, 2)
3894GEN_INT_EXT_TRANS(vsext_vf2, 1, 3)
3895GEN_INT_EXT_TRANS(vsext_vf4, 2, 4)
3896GEN_INT_EXT_TRANS(vsext_vf8, 3, 5)
3897