aboutsummaryrefslogtreecommitdiff
path: root/target/riscv/insn_trans/trans_rvvk.c.inc
blob: 3801c16829d4fb9efbe6993d814bb5eca3f801ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/*
 * RISC-V translation routines for the vector crypto extension.
 *
 * Copyright (C) 2023 SiFive, Inc.
 * Written by Codethink Ltd and SiFive.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2 or later, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * Zvbc
 */

#define GEN_VV_MASKED_TRANS(NAME, CHECK)                     \
    static bool trans_##NAME(DisasContext *s, arg_rmrr *a)   \
    {                                                        \
        if (CHECK(s, a)) {                                   \
            return opivv_trans(a->rd, a->rs1, a->rs2, a->vm, \
                               gen_helper_##NAME, s);        \
        }                                                    \
        return false;                                        \
    }

static bool vclmul_vv_check(DisasContext *s, arg_rmrr *a)
{
    return opivv_check(s, a) &&
           s->cfg_ptr->ext_zvbc == true &&
           s->sew == MO_64;
}

GEN_VV_MASKED_TRANS(vclmul_vv, vclmul_vv_check)
GEN_VV_MASKED_TRANS(vclmulh_vv, vclmul_vv_check)

#define GEN_VX_MASKED_TRANS(NAME, CHECK)                     \
    static bool trans_##NAME(DisasContext *s, arg_rmrr *a)   \
    {                                                        \
        if (CHECK(s, a)) {                                   \
            return opivx_trans(a->rd, a->rs1, a->rs2, a->vm, \
                               gen_helper_##NAME, s);        \
        }                                                    \
        return false;                                        \
    }

static bool vclmul_vx_check(DisasContext *s, arg_rmrr *a)
{
    return opivx_check(s, a) &&
           s->cfg_ptr->ext_zvbc == true &&
           s->sew == MO_64;
}

GEN_VX_MASKED_TRANS(vclmul_vx, vclmul_vx_check)
GEN_VX_MASKED_TRANS(vclmulh_vx, vclmul_vx_check)

/*
 * Zvbb
 */

#define GEN_OPIVI_GVEC_TRANS_CHECK(NAME, IMM_MODE, OPIVX, SUF, CHECK)   \
    static bool trans_##NAME(DisasContext *s, arg_rmrr *a)              \
    {                                                                   \
        if (CHECK(s, a)) {                                              \
            static gen_helper_opivx *const fns[4] = {                   \
                gen_helper_##OPIVX##_b,                                 \
                gen_helper_##OPIVX##_h,                                 \
                gen_helper_##OPIVX##_w,                                 \
                gen_helper_##OPIVX##_d,                                 \
            };                                                          \
            return do_opivi_gvec(s, a, tcg_gen_gvec_##SUF, fns[s->sew], \
                                 IMM_MODE);                             \
        }                                                               \
        return false;                                                   \
    }

#define GEN_OPIVV_GVEC_TRANS_CHECK(NAME, SUF, CHECK)                     \
    static bool trans_##NAME(DisasContext *s, arg_rmrr *a)               \
    {                                                                    \
        if (CHECK(s, a)) {                                               \
            static gen_helper_gvec_4_ptr *const fns[4] = {               \
                gen_helper_##NAME##_b,                                   \
                gen_helper_##NAME##_h,                                   \
                gen_helper_##NAME##_w,                                   \
                gen_helper_##NAME##_d,                                   \
            };                                                           \
            return do_opivv_gvec(s, a, tcg_gen_gvec_##SUF, fns[s->sew]); \
        }                                                                \
        return false;                                                    \
    }

#define GEN_OPIVX_GVEC_SHIFT_TRANS_CHECK(NAME, SUF, CHECK)       \
    static bool trans_##NAME(DisasContext *s, arg_rmrr *a)       \
    {                                                            \
        if (CHECK(s, a)) {                                       \
            static gen_helper_opivx *const fns[4] = {            \
                gen_helper_##NAME##_b,                           \
                gen_helper_##NAME##_h,                           \
                gen_helper_##NAME##_w,                           \
                gen_helper_##NAME##_d,                           \
            };                                                   \
            return do_opivx_gvec_shift(s, a, tcg_gen_gvec_##SUF, \
                                       fns[s->sew]);             \
        }                                                        \
        return false;                                            \
    }

static bool zvkb_vv_check(DisasContext *s, arg_rmrr *a)
{
    return opivv_check(s, a) &&
           (s->cfg_ptr->ext_zvbb == true || s->cfg_ptr->ext_zvkb == true);
}

static bool zvkb_vx_check(DisasContext *s, arg_rmrr *a)
{
    return opivx_check(s, a) &&
           (s->cfg_ptr->ext_zvbb == true || s->cfg_ptr->ext_zvkb == true);
}

/* vrol.v[vx] */
GEN_OPIVV_GVEC_TRANS_CHECK(vrol_vv, rotlv, zvkb_vv_check)
GEN_OPIVX_GVEC_SHIFT_TRANS_CHECK(vrol_vx, rotls, zvkb_vx_check)

/* vror.v[vxi] */
GEN_OPIVV_GVEC_TRANS_CHECK(vror_vv, rotrv, zvkb_vv_check)
GEN_OPIVX_GVEC_SHIFT_TRANS_CHECK(vror_vx, rotrs, zvkb_vx_check)
GEN_OPIVI_GVEC_TRANS_CHECK(vror_vi, IMM_TRUNC_SEW, vror_vx, rotri,
                           zvkb_vx_check)

#define GEN_OPIVX_GVEC_TRANS_CHECK(NAME, SUF, CHECK)                     \
    static bool trans_##NAME(DisasContext *s, arg_rmrr *a)               \
    {                                                                    \
        if (CHECK(s, a)) {                                               \
            static gen_helper_opivx *const fns[4] = {                    \
                gen_helper_##NAME##_b,                                   \
                gen_helper_##NAME##_h,                                   \
                gen_helper_##NAME##_w,                                   \
                gen_helper_##NAME##_d,                                   \
            };                                                           \
            return do_opivx_gvec(s, a, tcg_gen_gvec_##SUF, fns[s->sew]); \
        }                                                                \
        return false;                                                    \
    }

/* vandn.v[vx] */
GEN_OPIVV_GVEC_TRANS_CHECK(vandn_vv, andc, zvkb_vv_check)
GEN_OPIVX_GVEC_TRANS_CHECK(vandn_vx, andcs, zvkb_vx_check)

#define GEN_OPIV_TRANS(NAME, CHECK)                                        \
    static bool trans_##NAME(DisasContext *s, arg_rmr *a)                  \
    {                                                                      \
        if (CHECK(s, a)) {                                                 \
            uint32_t data = 0;                                             \
            static gen_helper_gvec_3_ptr *const fns[4] = {                 \
                gen_helper_##NAME##_b,                                     \
                gen_helper_##NAME##_h,                                     \
                gen_helper_##NAME##_w,                                     \
                gen_helper_##NAME##_d,                                     \
            };                                                             \
            TCGLabel *over = gen_new_label();                              \
            tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);     \
                                                                           \
            data = FIELD_DP32(data, VDATA, VM, a->vm);                     \
            data = FIELD_DP32(data, VDATA, LMUL, s->lmul);                 \
            data = FIELD_DP32(data, VDATA, VTA, s->vta);                   \
            data = FIELD_DP32(data, VDATA, VTA_ALL_1S, s->cfg_vta_all_1s); \
            data = FIELD_DP32(data, VDATA, VMA, s->vma);                   \
            tcg_gen_gvec_3_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, 0),         \
                               vreg_ofs(s, a->rs2), tcg_env,               \
                               s->cfg_ptr->vlen / 8, s->cfg_ptr->vlen / 8, \
                               data, fns[s->sew]);                         \
            mark_vs_dirty(s);                                              \
            gen_set_label(over);                                           \
            return true;                                                   \
        }                                                                  \
        return false;                                                      \
    }

static bool zvbb_opiv_check(DisasContext *s, arg_rmr *a)
{
    return s->cfg_ptr->ext_zvbb == true &&
           require_rvv(s) &&
           vext_check_isa_ill(s) &&
           vext_check_ss(s, a->rd, a->rs2, a->vm);
}

static bool zvkb_opiv_check(DisasContext *s, arg_rmr *a)
{
    return (s->cfg_ptr->ext_zvbb == true || s->cfg_ptr->ext_zvkb == true) &&
           require_rvv(s) &&
           vext_check_isa_ill(s) &&
           vext_check_ss(s, a->rd, a->rs2, a->vm);
}

GEN_OPIV_TRANS(vbrev8_v, zvkb_opiv_check)
GEN_OPIV_TRANS(vrev8_v, zvkb_opiv_check)
GEN_OPIV_TRANS(vbrev_v, zvbb_opiv_check)
GEN_OPIV_TRANS(vclz_v, zvbb_opiv_check)
GEN_OPIV_TRANS(vctz_v, zvbb_opiv_check)
GEN_OPIV_TRANS(vcpop_v, zvbb_opiv_check)

static bool vwsll_vv_check(DisasContext *s, arg_rmrr *a)
{
    return s->cfg_ptr->ext_zvbb && opivv_widen_check(s, a);
}

static bool vwsll_vx_check(DisasContext *s, arg_rmrr *a)
{
    return s->cfg_ptr->ext_zvbb && opivx_widen_check(s, a);
}

/* OPIVI without GVEC IR */
#define GEN_OPIVI_WIDEN_TRANS(NAME, IMM_MODE, OPIVX, CHECK)                  \
    static bool trans_##NAME(DisasContext *s, arg_rmrr *a)                   \
    {                                                                        \
        if (CHECK(s, a)) {                                                   \
            static gen_helper_opivx *const fns[3] = {                        \
                gen_helper_##OPIVX##_b,                                      \
                gen_helper_##OPIVX##_h,                                      \
                gen_helper_##OPIVX##_w,                                      \
            };                                                               \
            return opivi_trans(a->rd, a->rs1, a->rs2, a->vm, fns[s->sew], s, \
                               IMM_MODE);                                    \
        }                                                                    \
        return false;                                                        \
    }

GEN_OPIVV_WIDEN_TRANS(vwsll_vv, vwsll_vv_check)
GEN_OPIVX_WIDEN_TRANS(vwsll_vx, vwsll_vx_check)
GEN_OPIVI_WIDEN_TRANS(vwsll_vi, IMM_ZX, vwsll_vx, vwsll_vx_check)

/*
 * Zvkned
 */

#define ZVKNED_EGS 4

#define GEN_V_UNMASKED_TRANS(NAME, CHECK, EGS)                                \
    static bool trans_##NAME(DisasContext *s, arg_##NAME *a)                  \
    {                                                                         \
        if (CHECK(s, a)) {                                                    \
            TCGv_ptr rd_v, rs2_v;                                             \
            TCGv_i32 desc, egs;                                               \
            uint32_t data = 0;                                                \
            TCGLabel *over = gen_new_label();                                 \
                                                                              \
            if (!s->vstart_eq_zero || !s->vl_eq_vlmax) {                      \
                /* save opcode for unwinding in case we throw an exception */ \
                decode_save_opc(s);                                           \
                egs = tcg_constant_i32(EGS);                                  \
                gen_helper_egs_check(egs, tcg_env);                           \
                tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);    \
            }                                                                 \
                                                                              \
            data = FIELD_DP32(data, VDATA, VM, a->vm);                        \
            data = FIELD_DP32(data, VDATA, LMUL, s->lmul);                    \
            data = FIELD_DP32(data, VDATA, VTA, s->vta);                      \
            data = FIELD_DP32(data, VDATA, VTA_ALL_1S, s->cfg_vta_all_1s);    \
            data = FIELD_DP32(data, VDATA, VMA, s->vma);                      \
            rd_v = tcg_temp_new_ptr();                                        \
            rs2_v = tcg_temp_new_ptr();                                       \
            desc = tcg_constant_i32(                                          \
                simd_desc(s->cfg_ptr->vlen / 8, s->cfg_ptr->vlen / 8, data)); \
            tcg_gen_addi_ptr(rd_v, tcg_env, vreg_ofs(s, a->rd));              \
            tcg_gen_addi_ptr(rs2_v, tcg_env, vreg_ofs(s, a->rs2));            \
            gen_helper_##NAME(rd_v, rs2_v, tcg_env, desc);                    \
            mark_vs_dirty(s);                                                 \
            gen_set_label(over);                                              \
            return true;                                                      \
        }                                                                     \
        return false;                                                         \
    }

static bool vaes_check_vv(DisasContext *s, arg_rmr *a)
{
    int egw_bytes = ZVKNED_EGS << s->sew;
    return s->cfg_ptr->ext_zvkned == true &&
           require_rvv(s) &&
           vext_check_isa_ill(s) &&
           MAXSZ(s) >= egw_bytes &&
           require_align(a->rd, s->lmul) &&
           require_align(a->rs2, s->lmul) &&
           s->sew == MO_32;
}

static bool vaes_check_overlap(DisasContext *s, int vd, int vs2)
{
    int8_t op_size = s->lmul <= 0 ? 1 : 1 << s->lmul;
    return !is_overlapped(vd, op_size, vs2, 1);
}

static bool vaes_check_vs(DisasContext *s, arg_rmr *a)
{
    int egw_bytes = ZVKNED_EGS << s->sew;
    return vaes_check_overlap(s, a->rd, a->rs2) &&
           MAXSZ(s) >= egw_bytes &&
           s->cfg_ptr->ext_zvkned == true &&
           require_rvv(s) &&
           vext_check_isa_ill(s) &&
           require_align(a->rd, s->lmul) &&
           s->sew == MO_32;
}

GEN_V_UNMASKED_TRANS(vaesef_vv, vaes_check_vv, ZVKNED_EGS)
GEN_V_UNMASKED_TRANS(vaesef_vs, vaes_check_vs, ZVKNED_EGS)
GEN_V_UNMASKED_TRANS(vaesdf_vv, vaes_check_vv, ZVKNED_EGS)
GEN_V_UNMASKED_TRANS(vaesdf_vs, vaes_check_vs, ZVKNED_EGS)
GEN_V_UNMASKED_TRANS(vaesdm_vv, vaes_check_vv, ZVKNED_EGS)
GEN_V_UNMASKED_TRANS(vaesdm_vs, vaes_check_vs, ZVKNED_EGS)
GEN_V_UNMASKED_TRANS(vaesz_vs, vaes_check_vs, ZVKNED_EGS)
GEN_V_UNMASKED_TRANS(vaesem_vv, vaes_check_vv, ZVKNED_EGS)
GEN_V_UNMASKED_TRANS(vaesem_vs, vaes_check_vs, ZVKNED_EGS)

#define GEN_VI_UNMASKED_TRANS(NAME, CHECK, EGS)                               \
    static bool trans_##NAME(DisasContext *s, arg_##NAME *a)                  \
    {                                                                         \
        if (CHECK(s, a)) {                                                    \
            TCGv_ptr rd_v, rs2_v;                                             \
            TCGv_i32 uimm_v, desc, egs;                                       \
            uint32_t data = 0;                                                \
            TCGLabel *over = gen_new_label();                                 \
                                                                              \
            if (!s->vstart_eq_zero || !s->vl_eq_vlmax) {                      \
                /* save opcode for unwinding in case we throw an exception */ \
                decode_save_opc(s);                                           \
                egs = tcg_constant_i32(EGS);                                  \
                gen_helper_egs_check(egs, tcg_env);                           \
                tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);    \
            }                                                                 \
                                                                              \
            data = FIELD_DP32(data, VDATA, VM, a->vm);                        \
            data = FIELD_DP32(data, VDATA, LMUL, s->lmul);                    \
            data = FIELD_DP32(data, VDATA, VTA, s->vta);                      \
            data = FIELD_DP32(data, VDATA, VTA_ALL_1S, s->cfg_vta_all_1s);    \
            data = FIELD_DP32(data, VDATA, VMA, s->vma);                      \
                                                                              \
            rd_v = tcg_temp_new_ptr();                                        \
            rs2_v = tcg_temp_new_ptr();                                       \
            uimm_v = tcg_constant_i32(a->rs1);                                \
            desc = tcg_constant_i32(                                          \
                simd_desc(s->cfg_ptr->vlen / 8, s->cfg_ptr->vlen / 8, data)); \
            tcg_gen_addi_ptr(rd_v, tcg_env, vreg_ofs(s, a->rd));              \
            tcg_gen_addi_ptr(rs2_v, tcg_env, vreg_ofs(s, a->rs2));            \
            gen_helper_##NAME(rd_v, rs2_v, uimm_v, tcg_env, desc);            \
            mark_vs_dirty(s);                                                 \
            gen_set_label(over);                                              \
            return true;                                                      \
        }                                                                     \
        return false;                                                         \
    }

static bool vaeskf1_check(DisasContext *s, arg_vaeskf1_vi *a)
{
    int egw_bytes = ZVKNED_EGS << s->sew;
    return s->cfg_ptr->ext_zvkned == true &&
           require_rvv(s) &&
           vext_check_isa_ill(s) &&
           MAXSZ(s) >= egw_bytes &&
           s->sew == MO_32 &&
           require_align(a->rd, s->lmul) &&
           require_align(a->rs2, s->lmul);
}

static bool vaeskf2_check(DisasContext *s, arg_vaeskf2_vi *a)
{
    int egw_bytes = ZVKNED_EGS << s->sew;
    return s->cfg_ptr->ext_zvkned == true &&
           require_rvv(s) &&
           vext_check_isa_ill(s) &&
           MAXSZ(s) >= egw_bytes &&
           s->sew == MO_32 &&
           require_align(a->rd, s->lmul) &&
           require_align(a->rs2, s->lmul);
}

GEN_VI_UNMASKED_TRANS(vaeskf1_vi, vaeskf1_check, ZVKNED_EGS)
GEN_VI_UNMASKED_TRANS(vaeskf2_vi, vaeskf2_check, ZVKNED_EGS)

/*
 * Zvknh
 */

#define ZVKNH_EGS 4

#define GEN_VV_UNMASKED_TRANS(NAME, CHECK, EGS)                               \
    static bool trans_##NAME(DisasContext *s, arg_rmrr *a)                    \
    {                                                                         \
        if (CHECK(s, a)) {                                                    \
            uint32_t data = 0;                                                \
            TCGLabel *over = gen_new_label();                                 \
            TCGv_i32 egs;                                                     \
                                                                              \
            if (!s->vstart_eq_zero || !s->vl_eq_vlmax) {                      \
                /* save opcode for unwinding in case we throw an exception */ \
                decode_save_opc(s);                                           \
                egs = tcg_constant_i32(EGS);                                  \
                gen_helper_egs_check(egs, tcg_env);                           \
                tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);    \
            }                                                                 \
                                                                              \
            data = FIELD_DP32(data, VDATA, VM, a->vm);                        \
            data = FIELD_DP32(data, VDATA, LMUL, s->lmul);                    \
            data = FIELD_DP32(data, VDATA, VTA, s->vta);                      \
            data = FIELD_DP32(data, VDATA, VTA_ALL_1S, s->cfg_vta_all_1s);    \
            data = FIELD_DP32(data, VDATA, VMA, s->vma);                      \
                                                                              \
            tcg_gen_gvec_3_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, a->rs1),       \
                               vreg_ofs(s, a->rs2), tcg_env,                  \
                               s->cfg_ptr->vlen / 8, s->cfg_ptr->vlen / 8,    \
                               data, gen_helper_##NAME);                      \
                                                                              \
            mark_vs_dirty(s);                                                 \
            gen_set_label(over);                                              \
            return true;                                                      \
        }                                                                     \
        return false;                                                         \
    }

static bool vsha_check_sew(DisasContext *s)
{
    return (s->cfg_ptr->ext_zvknha == true && s->sew == MO_32) ||
           (s->cfg_ptr->ext_zvknhb == true &&
            (s->sew == MO_32 || s->sew == MO_64));
}

static bool vsha_check(DisasContext *s, arg_rmrr *a)
{
    int egw_bytes = ZVKNH_EGS << s->sew;
    int mult = 1 << MAX(s->lmul, 0);
    return opivv_check(s, a) &&
           vsha_check_sew(s) &&
           MAXSZ(s) >= egw_bytes &&
           !is_overlapped(a->rd, mult, a->rs1, mult) &&
           !is_overlapped(a->rd, mult, a->rs2, mult) &&
           s->lmul >= 0;
}

GEN_VV_UNMASKED_TRANS(vsha2ms_vv, vsha_check, ZVKNH_EGS)

static bool trans_vsha2cl_vv(DisasContext *s, arg_rmrr *a)
{
    if (vsha_check(s, a)) {
        uint32_t data = 0;
        TCGLabel *over = gen_new_label();
        TCGv_i32 egs;

        if (!s->vstart_eq_zero || !s->vl_eq_vlmax) {
            /* save opcode for unwinding in case we throw an exception */
            decode_save_opc(s);
            egs = tcg_constant_i32(ZVKNH_EGS);
            gen_helper_egs_check(egs, tcg_env);
            tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);
        }

        data = FIELD_DP32(data, VDATA, VM, a->vm);
        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);
        data = FIELD_DP32(data, VDATA, VTA, s->vta);
        data = FIELD_DP32(data, VDATA, VTA_ALL_1S, s->cfg_vta_all_1s);
        data = FIELD_DP32(data, VDATA, VMA, s->vma);

        tcg_gen_gvec_3_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, a->rs1),
            vreg_ofs(s, a->rs2), tcg_env, s->cfg_ptr->vlen / 8,
            s->cfg_ptr->vlen / 8, data,
            s->sew == MO_32 ?
                gen_helper_vsha2cl32_vv : gen_helper_vsha2cl64_vv);

        mark_vs_dirty(s);
        gen_set_label(over);
        return true;
    }
    return false;
}

static bool trans_vsha2ch_vv(DisasContext *s, arg_rmrr *a)
{
    if (vsha_check(s, a)) {
        uint32_t data = 0;
        TCGLabel *over = gen_new_label();
        TCGv_i32 egs;

        if (!s->vstart_eq_zero || !s->vl_eq_vlmax) {
            /* save opcode for unwinding in case we throw an exception */
            decode_save_opc(s);
            egs = tcg_constant_i32(ZVKNH_EGS);
            gen_helper_egs_check(egs, tcg_env);
            tcg_gen_brcond_tl(TCG_COND_GEU, cpu_vstart, cpu_vl, over);
        }

        data = FIELD_DP32(data, VDATA, VM, a->vm);
        data = FIELD_DP32(data, VDATA, LMUL, s->lmul);
        data = FIELD_DP32(data, VDATA, VTA, s->vta);
        data = FIELD_DP32(data, VDATA, VTA_ALL_1S, s->cfg_vta_all_1s);
        data = FIELD_DP32(data, VDATA, VMA, s->vma);

        tcg_gen_gvec_3_ptr(vreg_ofs(s, a->rd), vreg_ofs(s, a->rs1),
            vreg_ofs(s, a->rs2), tcg_env, s->cfg_ptr->vlen / 8,
            s->cfg_ptr->vlen / 8, data,
            s->sew == MO_32 ?
                gen_helper_vsha2ch32_vv : gen_helper_vsha2ch64_vv);

        mark_vs_dirty(s);
        gen_set_label(over);
        return true;
    }
    return false;
}

/*
 * Zvksh
 */

#define ZVKSH_EGS 8

static inline bool vsm3_check(DisasContext *s, arg_rmrr *a)
{
    int egw_bytes = ZVKSH_EGS << s->sew;
    int mult = 1 << MAX(s->lmul, 0);
    return s->cfg_ptr->ext_zvksh == true &&
           require_rvv(s) &&
           vext_check_isa_ill(s) &&
           !is_overlapped(a->rd, mult, a->rs2, mult) &&
           MAXSZ(s) >= egw_bytes &&
           s->sew == MO_32;
}

static inline bool vsm3me_check(DisasContext *s, arg_rmrr *a)
{
    return vsm3_check(s, a) && vext_check_sss(s, a->rd, a->rs1, a->rs2, a->vm);
}

static inline bool vsm3c_check(DisasContext *s, arg_rmrr *a)
{
    return vsm3_check(s, a) && vext_check_ss(s, a->rd, a->rs2, a->vm);
}

GEN_VV_UNMASKED_TRANS(vsm3me_vv, vsm3me_check, ZVKSH_EGS)
GEN_VI_UNMASKED_TRANS(vsm3c_vi, vsm3c_check, ZVKSH_EGS)

/*
 * Zvkg
 */

#define ZVKG_EGS 4

static bool vgmul_check(DisasContext *s, arg_rmr *a)
{
    int egw_bytes = ZVKG_EGS << s->sew;
    return s->cfg_ptr->ext_zvkg == true &&
           vext_check_isa_ill(s) &&
           require_rvv(s) &&
           MAXSZ(s) >= egw_bytes &&
           vext_check_ss(s, a->rd, a->rs2, a->vm) &&
           s->sew == MO_32;
}

GEN_V_UNMASKED_TRANS(vgmul_vv, vgmul_check, ZVKG_EGS)

static bool vghsh_check(DisasContext *s, arg_rmrr *a)
{
    int egw_bytes = ZVKG_EGS << s->sew;
    return s->cfg_ptr->ext_zvkg == true &&
           opivv_check(s, a) &&
           MAXSZ(s) >= egw_bytes &&
           s->sew == MO_32;
}

GEN_VV_UNMASKED_TRANS(vghsh_vv, vghsh_check, ZVKG_EGS)

/*
 * Zvksed
 */

#define ZVKSED_EGS 4

static bool zvksed_check(DisasContext *s)
{
    int egw_bytes = ZVKSED_EGS << s->sew;
    return s->cfg_ptr->ext_zvksed == true &&
           require_rvv(s) &&
           vext_check_isa_ill(s) &&
           MAXSZ(s) >= egw_bytes &&
           s->sew == MO_32;
}

static bool vsm4k_vi_check(DisasContext *s, arg_rmrr *a)
{
    return zvksed_check(s) &&
           require_align(a->rd, s->lmul) &&
           require_align(a->rs2, s->lmul);
}

GEN_VI_UNMASKED_TRANS(vsm4k_vi, vsm4k_vi_check, ZVKSED_EGS)

static bool vsm4r_vv_check(DisasContext *s, arg_rmr *a)
{
    return zvksed_check(s) &&
           require_align(a->rd, s->lmul) &&
           require_align(a->rs2, s->lmul);
}

GEN_V_UNMASKED_TRANS(vsm4r_vv, vsm4r_vv_check, ZVKSED_EGS)

static bool vsm4r_vs_check(DisasContext *s, arg_rmr *a)
{
    return zvksed_check(s) &&
           !is_overlapped(a->rd, 1 << MAX(s->lmul, 0), a->rs2, 1) &&
           require_align(a->rd, s->lmul);
}

GEN_V_UNMASKED_TRANS(vsm4r_vs, vsm4r_vs_check, ZVKSED_EGS)