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