xref: /openbmc/qemu/target/i386/tcg/emit.c.inc (revision 5b5968c4)
1/*
2 * New-style TCG opcode generator for i386 instructions
3 *
4 *  Copyright (c) 2022 Red Hat, Inc.
5 *
6 * Author: Paolo Bonzini <pbonzini@redhat.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 */
21
22#define ZMM_OFFSET(reg) offsetof(CPUX86State, xmm_regs[reg])
23
24typedef void (*SSEFunc_i_ep)(TCGv_i32 val, TCGv_ptr env, TCGv_ptr reg);
25typedef void (*SSEFunc_l_ep)(TCGv_i64 val, TCGv_ptr env, TCGv_ptr reg);
26typedef void (*SSEFunc_0_epp)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b);
27typedef void (*SSEFunc_0_eppp)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
28                               TCGv_ptr reg_c);
29typedef void (*SSEFunc_0_epppp)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
30                                TCGv_ptr reg_c, TCGv_ptr reg_d);
31typedef void (*SSEFunc_0_eppi)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
32                               TCGv_i32 val);
33typedef void (*SSEFunc_0_epppi)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
34                                TCGv_ptr reg_c, TCGv_i32 val);
35typedef void (*SSEFunc_0_ppi)(TCGv_ptr reg_a, TCGv_ptr reg_b, TCGv_i32 val);
36typedef void (*SSEFunc_0_pppi)(TCGv_ptr reg_a, TCGv_ptr reg_b, TCGv_ptr reg_c,
37                               TCGv_i32 val);
38typedef void (*SSEFunc_0_eppt)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
39                               TCGv val);
40typedef void (*SSEFunc_0_epppti)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
41                                 TCGv_ptr reg_c, TCGv a0, TCGv_i32 scale);
42typedef void (*SSEFunc_0_eppppi)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
43                                  TCGv_ptr reg_c, TCGv_ptr reg_d, TCGv_i32 flags);
44typedef void (*SSEFunc_0_eppppii)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
45                                  TCGv_ptr reg_c, TCGv_ptr reg_d, TCGv_i32 even,
46                                  TCGv_i32 odd);
47
48static inline TCGv_i32 tcg_constant8u_i32(uint8_t val)
49{
50    return tcg_constant_i32(val);
51}
52
53static void gen_NM_exception(DisasContext *s)
54{
55    gen_exception(s, EXCP07_PREX);
56}
57
58static void gen_illegal(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
59{
60    gen_illegal_opcode(s);
61}
62
63static void gen_load_ea(DisasContext *s, AddressParts *mem, bool is_vsib)
64{
65    TCGv ea = gen_lea_modrm_1(s, *mem, is_vsib);
66    gen_lea_v_seg(s, s->aflag, ea, mem->def_seg, s->override);
67}
68
69static inline int mmx_offset(MemOp ot)
70{
71    switch (ot) {
72    case MO_8:
73        return offsetof(MMXReg, MMX_B(0));
74    case MO_16:
75        return offsetof(MMXReg, MMX_W(0));
76    case MO_32:
77        return offsetof(MMXReg, MMX_L(0));
78    case MO_64:
79        return offsetof(MMXReg, MMX_Q(0));
80    default:
81        g_assert_not_reached();
82    }
83}
84
85static inline int xmm_offset(MemOp ot)
86{
87    switch (ot) {
88    case MO_8:
89        return offsetof(ZMMReg, ZMM_B(0));
90    case MO_16:
91        return offsetof(ZMMReg, ZMM_W(0));
92    case MO_32:
93        return offsetof(ZMMReg, ZMM_L(0));
94    case MO_64:
95        return offsetof(ZMMReg, ZMM_Q(0));
96    case MO_128:
97        return offsetof(ZMMReg, ZMM_X(0));
98    case MO_256:
99        return offsetof(ZMMReg, ZMM_Y(0));
100    default:
101        g_assert_not_reached();
102    }
103}
104
105static int vector_reg_offset(X86DecodedOp *op)
106{
107    assert(op->unit == X86_OP_MMX || op->unit == X86_OP_SSE);
108
109    if (op->unit == X86_OP_MMX) {
110        return op->offset - mmx_offset(op->ot);
111    } else {
112        return op->offset - xmm_offset(op->ot);
113    }
114}
115
116static int vector_elem_offset(X86DecodedOp *op, MemOp ot, int n)
117{
118    int base_ofs = vector_reg_offset(op);
119    switch(ot) {
120    case MO_8:
121        if (op->unit == X86_OP_MMX) {
122            return base_ofs + offsetof(MMXReg, MMX_B(n));
123        } else {
124            return base_ofs + offsetof(ZMMReg, ZMM_B(n));
125        }
126    case MO_16:
127        if (op->unit == X86_OP_MMX) {
128            return base_ofs + offsetof(MMXReg, MMX_W(n));
129        } else {
130            return base_ofs + offsetof(ZMMReg, ZMM_W(n));
131        }
132    case MO_32:
133        if (op->unit == X86_OP_MMX) {
134            return base_ofs + offsetof(MMXReg, MMX_L(n));
135        } else {
136            return base_ofs + offsetof(ZMMReg, ZMM_L(n));
137        }
138    case MO_64:
139        if (op->unit == X86_OP_MMX) {
140            return base_ofs;
141        } else {
142            return base_ofs + offsetof(ZMMReg, ZMM_Q(n));
143        }
144    case MO_128:
145        assert(op->unit == X86_OP_SSE);
146        return base_ofs + offsetof(ZMMReg, ZMM_X(n));
147    case MO_256:
148        assert(op->unit == X86_OP_SSE);
149        return base_ofs + offsetof(ZMMReg, ZMM_Y(n));
150    default:
151        g_assert_not_reached();
152    }
153}
154
155static void compute_mmx_offset(X86DecodedOp *op)
156{
157    if (!op->has_ea) {
158        op->offset = offsetof(CPUX86State, fpregs[op->n].mmx) + mmx_offset(op->ot);
159    } else {
160        op->offset = offsetof(CPUX86State, mmx_t0) + mmx_offset(op->ot);
161    }
162}
163
164static void compute_xmm_offset(X86DecodedOp *op)
165{
166    if (!op->has_ea) {
167        op->offset = ZMM_OFFSET(op->n) + xmm_offset(op->ot);
168    } else {
169        op->offset = offsetof(CPUX86State, xmm_t0) + xmm_offset(op->ot);
170    }
171}
172
173static void gen_load_sse(DisasContext *s, TCGv temp, MemOp ot, int dest_ofs, bool aligned)
174{
175    switch(ot) {
176    case MO_8:
177        gen_op_ld_v(s, MO_8, temp, s->A0);
178        tcg_gen_st8_tl(temp, cpu_env, dest_ofs);
179        break;
180    case MO_16:
181        gen_op_ld_v(s, MO_16, temp, s->A0);
182        tcg_gen_st16_tl(temp, cpu_env, dest_ofs);
183        break;
184    case MO_32:
185        gen_op_ld_v(s, MO_32, temp, s->A0);
186        tcg_gen_st32_tl(temp, cpu_env, dest_ofs);
187        break;
188    case MO_64:
189        gen_ldq_env_A0(s, dest_ofs);
190        break;
191    case MO_128:
192        gen_ldo_env_A0(s, dest_ofs, aligned);
193        break;
194    case MO_256:
195        gen_ldy_env_A0(s, dest_ofs, aligned);
196        break;
197    default:
198        g_assert_not_reached();
199    }
200}
201
202static bool sse_needs_alignment(DisasContext *s, X86DecodedInsn *decode, MemOp ot)
203{
204    switch (decode->e.vex_class) {
205    case 2:
206    case 4:
207        if ((s->prefix & PREFIX_VEX) ||
208            decode->e.vex_special == X86_VEX_SSEUnaligned) {
209            /* MOST legacy SSE instructions require aligned memory operands, but not all.  */
210            return false;
211        }
212        /* fall through */
213    case 1:
214        return ot >= MO_128;
215
216    default:
217        return false;
218    }
219}
220
221static void gen_load(DisasContext *s, X86DecodedInsn *decode, int opn, TCGv v)
222{
223    X86DecodedOp *op = &decode->op[opn];
224
225    switch (op->unit) {
226    case X86_OP_SKIP:
227        return;
228    case X86_OP_SEG:
229        tcg_gen_ld32u_tl(v, cpu_env,
230                         offsetof(CPUX86State,segs[op->n].selector));
231        break;
232    case X86_OP_CR:
233        tcg_gen_ld_tl(v, cpu_env, offsetof(CPUX86State, cr[op->n]));
234        break;
235    case X86_OP_DR:
236        tcg_gen_ld_tl(v, cpu_env, offsetof(CPUX86State, dr[op->n]));
237        break;
238    case X86_OP_INT:
239        if (op->has_ea) {
240            gen_op_ld_v(s, op->ot, v, s->A0);
241        } else {
242            gen_op_mov_v_reg(s, op->ot, v, op->n);
243        }
244        break;
245    case X86_OP_IMM:
246        tcg_gen_movi_tl(v, decode->immediate);
247        break;
248
249    case X86_OP_MMX:
250        compute_mmx_offset(op);
251        goto load_vector;
252
253    case X86_OP_SSE:
254        compute_xmm_offset(op);
255    load_vector:
256        if (op->has_ea) {
257            bool aligned = sse_needs_alignment(s, decode, op->ot);
258            gen_load_sse(s, v, op->ot, op->offset, aligned);
259        }
260        break;
261
262    default:
263        g_assert_not_reached();
264    }
265}
266
267static TCGv_ptr op_ptr(X86DecodedInsn *decode, int opn)
268{
269    X86DecodedOp *op = &decode->op[opn];
270    if (op->v_ptr) {
271        return op->v_ptr;
272    }
273    op->v_ptr = tcg_temp_new_ptr();
274
275    /* The temporary points to the MMXReg or ZMMReg.  */
276    tcg_gen_addi_ptr(op->v_ptr, cpu_env, vector_reg_offset(op));
277    return op->v_ptr;
278}
279
280#define OP_PTR0 op_ptr(decode, 0)
281#define OP_PTR1 op_ptr(decode, 1)
282#define OP_PTR2 op_ptr(decode, 2)
283
284static void gen_writeback(DisasContext *s, X86DecodedInsn *decode, int opn, TCGv v)
285{
286    X86DecodedOp *op = &decode->op[opn];
287    switch (op->unit) {
288    case X86_OP_SKIP:
289        break;
290    case X86_OP_SEG:
291        /* Note that gen_movl_seg_T0 takes care of interrupt shadow and TF.  */
292        gen_movl_seg_T0(s, op->n);
293        break;
294    case X86_OP_INT:
295        if (op->has_ea) {
296            gen_op_st_v(s, op->ot, v, s->A0);
297        } else {
298            gen_op_mov_reg_v(s, op->ot, op->n, v);
299        }
300        break;
301    case X86_OP_MMX:
302        break;
303    case X86_OP_SSE:
304        if (!op->has_ea && (s->prefix & PREFIX_VEX) && op->ot <= MO_128) {
305            tcg_gen_gvec_dup_imm(MO_64,
306                                 offsetof(CPUX86State, xmm_regs[op->n].ZMM_X(1)),
307                                 16, 16, 0);
308        }
309        break;
310    case X86_OP_CR:
311    case X86_OP_DR:
312    default:
313        g_assert_not_reached();
314    }
315}
316
317static inline int vector_len(DisasContext *s, X86DecodedInsn *decode)
318{
319    if (decode->e.special == X86_SPECIAL_MMX &&
320        !(s->prefix & (PREFIX_DATA | PREFIX_REPZ | PREFIX_REPNZ))) {
321        return 8;
322    }
323    return s->vex_l ? 32 : 16;
324}
325
326static void gen_store_sse(DisasContext *s, X86DecodedInsn *decode, int src_ofs)
327{
328    MemOp ot = decode->op[0].ot;
329    int vec_len = vector_len(s, decode);
330    bool aligned = sse_needs_alignment(s, decode, ot);
331
332    if (!decode->op[0].has_ea) {
333        tcg_gen_gvec_mov(MO_64, decode->op[0].offset, src_ofs, vec_len, vec_len);
334        return;
335    }
336
337    switch (ot) {
338    case MO_64:
339        gen_stq_env_A0(s, src_ofs);
340        break;
341    case MO_128:
342        gen_sto_env_A0(s, src_ofs, aligned);
343        break;
344    case MO_256:
345        gen_sty_env_A0(s, src_ofs, aligned);
346        break;
347    default:
348        g_assert_not_reached();
349    }
350}
351
352static void gen_helper_pavgusb(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b)
353{
354    gen_helper_pavgb_mmx(env, reg_a, reg_a, reg_b);
355}
356
357#define FN_3DNOW_MOVE ((SSEFunc_0_epp) (uintptr_t) 1)
358static const SSEFunc_0_epp fns_3dnow[] = {
359    [0x0c] = gen_helper_pi2fw,
360    [0x0d] = gen_helper_pi2fd,
361    [0x1c] = gen_helper_pf2iw,
362    [0x1d] = gen_helper_pf2id,
363    [0x8a] = gen_helper_pfnacc,
364    [0x8e] = gen_helper_pfpnacc,
365    [0x90] = gen_helper_pfcmpge,
366    [0x94] = gen_helper_pfmin,
367    [0x96] = gen_helper_pfrcp,
368    [0x97] = gen_helper_pfrsqrt,
369    [0x9a] = gen_helper_pfsub,
370    [0x9e] = gen_helper_pfadd,
371    [0xa0] = gen_helper_pfcmpgt,
372    [0xa4] = gen_helper_pfmax,
373    [0xa6] = FN_3DNOW_MOVE, /* PFRCPIT1; no need to actually increase precision */
374    [0xa7] = FN_3DNOW_MOVE, /* PFRSQIT1 */
375    [0xb6] = FN_3DNOW_MOVE, /* PFRCPIT2 */
376    [0xaa] = gen_helper_pfsubr,
377    [0xae] = gen_helper_pfacc,
378    [0xb0] = gen_helper_pfcmpeq,
379    [0xb4] = gen_helper_pfmul,
380    [0xb7] = gen_helper_pmulhrw_mmx,
381    [0xbb] = gen_helper_pswapd,
382    [0xbf] = gen_helper_pavgusb,
383};
384
385static void gen_3dnow(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
386{
387    uint8_t b = decode->immediate;
388    SSEFunc_0_epp fn = b < ARRAY_SIZE(fns_3dnow) ? fns_3dnow[b] : NULL;
389
390    if (!fn) {
391        gen_illegal_opcode(s);
392        return;
393    }
394    if (s->flags & HF_TS_MASK) {
395        gen_NM_exception(s);
396        return;
397    }
398    if (s->flags & HF_EM_MASK) {
399        gen_illegal_opcode(s);
400        return;
401    }
402
403    gen_helper_enter_mmx(cpu_env);
404    if (fn == FN_3DNOW_MOVE) {
405       tcg_gen_ld_i64(s->tmp1_i64, cpu_env, decode->op[1].offset);
406       tcg_gen_st_i64(s->tmp1_i64, cpu_env, decode->op[0].offset);
407    } else {
408       fn(cpu_env, OP_PTR0, OP_PTR1);
409    }
410}
411
412/*
413 * 00 = v*ps Vps, Hps, Wpd
414 * 66 = v*pd Vpd, Hpd, Wps
415 * f3 = v*ss Vss, Hss, Wps
416 * f2 = v*sd Vsd, Hsd, Wps
417 */
418static inline void gen_unary_fp_sse(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode,
419                              SSEFunc_0_epp pd_xmm, SSEFunc_0_epp ps_xmm,
420                              SSEFunc_0_epp pd_ymm, SSEFunc_0_epp ps_ymm,
421                              SSEFunc_0_eppp sd, SSEFunc_0_eppp ss)
422{
423    if ((s->prefix & (PREFIX_REPZ | PREFIX_REPNZ)) != 0) {
424        SSEFunc_0_eppp fn = s->prefix & PREFIX_REPZ ? ss : sd;
425        if (!fn) {
426            gen_illegal_opcode(s);
427            return;
428        }
429        fn(cpu_env, OP_PTR0, OP_PTR1, OP_PTR2);
430    } else {
431        SSEFunc_0_epp ps, pd, fn;
432        ps = s->vex_l ? ps_ymm : ps_xmm;
433        pd = s->vex_l ? pd_ymm : pd_xmm;
434        fn = s->prefix & PREFIX_DATA ? pd : ps;
435        if (!fn) {
436            gen_illegal_opcode(s);
437            return;
438        }
439        fn(cpu_env, OP_PTR0, OP_PTR2);
440    }
441}
442#define UNARY_FP_SSE(uname, lname)                                                 \
443static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
444{                                                                                  \
445    gen_unary_fp_sse(s, env, decode,                                               \
446                     gen_helper_##lname##pd_xmm,                                   \
447                     gen_helper_##lname##ps_xmm,                                   \
448                     gen_helper_##lname##pd_ymm,                                   \
449                     gen_helper_##lname##ps_ymm,                                   \
450                     gen_helper_##lname##sd,                                       \
451                     gen_helper_##lname##ss);                                      \
452}
453UNARY_FP_SSE(VSQRT, sqrt)
454
455/*
456 * 00 = v*ps Vps, Hps, Wpd
457 * 66 = v*pd Vpd, Hpd, Wps
458 * f3 = v*ss Vss, Hss, Wps
459 * f2 = v*sd Vsd, Hsd, Wps
460 */
461static inline void gen_fp_sse(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode,
462                              SSEFunc_0_eppp pd_xmm, SSEFunc_0_eppp ps_xmm,
463                              SSEFunc_0_eppp pd_ymm, SSEFunc_0_eppp ps_ymm,
464                              SSEFunc_0_eppp sd, SSEFunc_0_eppp ss)
465{
466    SSEFunc_0_eppp ps, pd, fn;
467    if ((s->prefix & (PREFIX_REPZ | PREFIX_REPNZ)) != 0) {
468        fn = s->prefix & PREFIX_REPZ ? ss : sd;
469    } else {
470        ps = s->vex_l ? ps_ymm : ps_xmm;
471        pd = s->vex_l ? pd_ymm : pd_xmm;
472        fn = s->prefix & PREFIX_DATA ? pd : ps;
473    }
474    if (fn) {
475        fn(cpu_env, OP_PTR0, OP_PTR1, OP_PTR2);
476    } else {
477        gen_illegal_opcode(s);
478    }
479}
480
481#define FP_SSE(uname, lname)                                                       \
482static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
483{                                                                                  \
484    gen_fp_sse(s, env, decode,                                                     \
485               gen_helper_##lname##pd_xmm,                                         \
486               gen_helper_##lname##ps_xmm,                                         \
487               gen_helper_##lname##pd_ymm,                                         \
488               gen_helper_##lname##ps_ymm,                                         \
489               gen_helper_##lname##sd,                                             \
490               gen_helper_##lname##ss);                                            \
491}
492FP_SSE(VADD, add)
493FP_SSE(VMUL, mul)
494FP_SSE(VSUB, sub)
495FP_SSE(VMIN, min)
496FP_SSE(VDIV, div)
497FP_SSE(VMAX, max)
498
499#define FMA_SSE_PACKED(uname, ptr0, ptr1, ptr2, even, odd)                         \
500static void gen_##uname##Px(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
501{                                                                                  \
502    SSEFunc_0_eppppii xmm = s->vex_w ? gen_helper_fma4pd_xmm : gen_helper_fma4ps_xmm; \
503    SSEFunc_0_eppppii ymm = s->vex_w ? gen_helper_fma4pd_ymm : gen_helper_fma4ps_ymm; \
504    SSEFunc_0_eppppii fn = s->vex_l ? ymm : xmm;                                   \
505                                                                                   \
506    fn(cpu_env, OP_PTR0, ptr0, ptr1, ptr2,                                         \
507       tcg_constant_i32(even),                                                     \
508       tcg_constant_i32((even) ^ (odd)));                                          \
509}
510
511#define FMA_SSE(uname, ptr0, ptr1, ptr2, flags)                                    \
512FMA_SSE_PACKED(uname, ptr0, ptr1, ptr2, flags, flags)                              \
513static void gen_##uname##Sx(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
514{                                                                                  \
515    SSEFunc_0_eppppi fn = s->vex_w ? gen_helper_fma4sd : gen_helper_fma4ss;        \
516                                                                                   \
517    fn(cpu_env, OP_PTR0, ptr0, ptr1, ptr2,                                         \
518       tcg_constant_i32(flags));                                                   \
519}                                                                                  \
520
521FMA_SSE(VFMADD231,  OP_PTR1, OP_PTR2, OP_PTR0, 0)
522FMA_SSE(VFMADD213,  OP_PTR1, OP_PTR0, OP_PTR2, 0)
523FMA_SSE(VFMADD132,  OP_PTR0, OP_PTR2, OP_PTR1, 0)
524
525FMA_SSE(VFNMADD231, OP_PTR1, OP_PTR2, OP_PTR0, float_muladd_negate_product)
526FMA_SSE(VFNMADD213, OP_PTR1, OP_PTR0, OP_PTR2, float_muladd_negate_product)
527FMA_SSE(VFNMADD132, OP_PTR0, OP_PTR2, OP_PTR1, float_muladd_negate_product)
528
529FMA_SSE(VFMSUB231,  OP_PTR1, OP_PTR2, OP_PTR0, float_muladd_negate_c)
530FMA_SSE(VFMSUB213,  OP_PTR1, OP_PTR0, OP_PTR2, float_muladd_negate_c)
531FMA_SSE(VFMSUB132,  OP_PTR0, OP_PTR2, OP_PTR1, float_muladd_negate_c)
532
533FMA_SSE(VFNMSUB231, OP_PTR1, OP_PTR2, OP_PTR0, float_muladd_negate_c|float_muladd_negate_product)
534FMA_SSE(VFNMSUB213, OP_PTR1, OP_PTR0, OP_PTR2, float_muladd_negate_c|float_muladd_negate_product)
535FMA_SSE(VFNMSUB132, OP_PTR0, OP_PTR2, OP_PTR1, float_muladd_negate_c|float_muladd_negate_product)
536
537FMA_SSE_PACKED(VFMADDSUB231, OP_PTR1, OP_PTR2, OP_PTR0, float_muladd_negate_c, 0)
538FMA_SSE_PACKED(VFMADDSUB213, OP_PTR1, OP_PTR0, OP_PTR2, float_muladd_negate_c, 0)
539FMA_SSE_PACKED(VFMADDSUB132, OP_PTR0, OP_PTR2, OP_PTR1, float_muladd_negate_c, 0)
540
541FMA_SSE_PACKED(VFMSUBADD231, OP_PTR1, OP_PTR2, OP_PTR0, 0, float_muladd_negate_c)
542FMA_SSE_PACKED(VFMSUBADD213, OP_PTR1, OP_PTR0, OP_PTR2, 0, float_muladd_negate_c)
543FMA_SSE_PACKED(VFMSUBADD132, OP_PTR0, OP_PTR2, OP_PTR1, 0, float_muladd_negate_c)
544
545#define FP_UNPACK_SSE(uname, lname)                                                \
546static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
547{                                                                                  \
548    /* PS maps to the DQ integer instruction, PD maps to QDQ.  */                  \
549    gen_fp_sse(s, env, decode,                                                     \
550               gen_helper_##lname##qdq_xmm,                                        \
551               gen_helper_##lname##dq_xmm,                                         \
552               gen_helper_##lname##qdq_ymm,                                        \
553               gen_helper_##lname##dq_ymm,                                         \
554               NULL, NULL);                                                        \
555}
556FP_UNPACK_SSE(VUNPCKLPx, punpckl)
557FP_UNPACK_SSE(VUNPCKHPx, punpckh)
558
559/*
560 * 00 = v*ps Vps, Wpd
561 * f3 = v*ss Vss, Wps
562 */
563static inline void gen_unary_fp32_sse(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode,
564                                      SSEFunc_0_epp ps_xmm,
565                                      SSEFunc_0_epp ps_ymm,
566                                      SSEFunc_0_eppp ss)
567{
568    if ((s->prefix & (PREFIX_DATA | PREFIX_REPNZ)) != 0) {
569        goto illegal_op;
570    } else if (s->prefix & PREFIX_REPZ) {
571        if (!ss) {
572            goto illegal_op;
573        }
574        ss(cpu_env, OP_PTR0, OP_PTR1, OP_PTR2);
575    } else {
576        SSEFunc_0_epp fn = s->vex_l ? ps_ymm : ps_xmm;
577        if (!fn) {
578            goto illegal_op;
579        }
580        fn(cpu_env, OP_PTR0, OP_PTR2);
581    }
582    return;
583
584illegal_op:
585    gen_illegal_opcode(s);
586}
587#define UNARY_FP32_SSE(uname, lname)                                               \
588static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
589{                                                                                  \
590    gen_unary_fp32_sse(s, env, decode,                                             \
591                       gen_helper_##lname##ps_xmm,                                 \
592                       gen_helper_##lname##ps_ymm,                                 \
593                       gen_helper_##lname##ss);                                    \
594}
595UNARY_FP32_SSE(VRSQRT, rsqrt)
596UNARY_FP32_SSE(VRCP, rcp)
597
598/*
599 * 66 = v*pd Vpd, Hpd, Wpd
600 * f2 = v*ps Vps, Hps, Wps
601 */
602static inline void gen_horizontal_fp_sse(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode,
603                                         SSEFunc_0_eppp pd_xmm, SSEFunc_0_eppp ps_xmm,
604                                         SSEFunc_0_eppp pd_ymm, SSEFunc_0_eppp ps_ymm)
605{
606    SSEFunc_0_eppp ps, pd, fn;
607    ps = s->vex_l ? ps_ymm : ps_xmm;
608    pd = s->vex_l ? pd_ymm : pd_xmm;
609    fn = s->prefix & PREFIX_DATA ? pd : ps;
610    fn(cpu_env, OP_PTR0, OP_PTR1, OP_PTR2);
611}
612#define HORIZONTAL_FP_SSE(uname, lname)                                            \
613static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
614{                                                                                  \
615    gen_horizontal_fp_sse(s, env, decode,                                          \
616                          gen_helper_##lname##pd_xmm, gen_helper_##lname##ps_xmm,  \
617                          gen_helper_##lname##pd_ymm, gen_helper_##lname##ps_ymm); \
618}
619HORIZONTAL_FP_SSE(VHADD, hadd)
620HORIZONTAL_FP_SSE(VHSUB, hsub)
621HORIZONTAL_FP_SSE(VADDSUB, addsub)
622
623static inline void gen_ternary_sse(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode,
624                                   int op3, SSEFunc_0_epppp xmm, SSEFunc_0_epppp ymm)
625{
626    SSEFunc_0_epppp fn = s->vex_l ? ymm : xmm;
627    TCGv_ptr ptr3 = tcg_temp_new_ptr();
628
629    /* The format of the fourth input is Lx */
630    tcg_gen_addi_ptr(ptr3, cpu_env, ZMM_OFFSET(op3));
631    fn(cpu_env, OP_PTR0, OP_PTR1, OP_PTR2, ptr3);
632    tcg_temp_free_ptr(ptr3);
633}
634#define TERNARY_SSE(uname, uvname, lname)                                          \
635static void gen_##uvname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
636{                                                                                  \
637    gen_ternary_sse(s, env, decode, (uint8_t)decode->immediate >> 4,               \
638                    gen_helper_##lname##_xmm, gen_helper_##lname##_ymm);           \
639}                                                                                  \
640static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
641{                                                                                  \
642    gen_ternary_sse(s, env, decode, 0,                                             \
643                  gen_helper_##lname##_xmm, gen_helper_##lname##_ymm);             \
644}
645TERNARY_SSE(BLENDVPS, VBLENDVPS, blendvps)
646TERNARY_SSE(BLENDVPD, VBLENDVPD, blendvpd)
647TERNARY_SSE(PBLENDVB, VPBLENDVB, pblendvb)
648
649static inline void gen_binary_imm_sse(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode,
650                                      SSEFunc_0_epppi xmm, SSEFunc_0_epppi ymm)
651{
652    TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
653    if (!s->vex_l) {
654        xmm(cpu_env, OP_PTR0, OP_PTR1, OP_PTR2, imm);
655    } else {
656        ymm(cpu_env, OP_PTR0, OP_PTR1, OP_PTR2, imm);
657    }
658}
659
660#define BINARY_IMM_SSE(uname, lname)                                               \
661static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
662{                                                                                  \
663    gen_binary_imm_sse(s, env, decode,                                             \
664                       gen_helper_##lname##_xmm,                                   \
665                       gen_helper_##lname##_ymm);                                  \
666}
667
668BINARY_IMM_SSE(VBLENDPD,   blendpd)
669BINARY_IMM_SSE(VBLENDPS,   blendps)
670BINARY_IMM_SSE(VPBLENDW,   pblendw)
671BINARY_IMM_SSE(VDDPS,      dpps)
672#define gen_helper_dppd_ymm NULL
673BINARY_IMM_SSE(VDDPD,      dppd)
674BINARY_IMM_SSE(VMPSADBW,   mpsadbw)
675BINARY_IMM_SSE(PCLMULQDQ,  pclmulqdq)
676
677
678#define UNARY_INT_GVEC(uname, func, ...)                                           \
679static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
680{                                                                                  \
681    int vec_len = vector_len(s, decode);                                          \
682                                                                                   \
683    func(__VA_ARGS__, decode->op[0].offset,                                        \
684         decode->op[2].offset, vec_len, vec_len);                                  \
685}
686UNARY_INT_GVEC(PABSB,          tcg_gen_gvec_abs, MO_8)
687UNARY_INT_GVEC(PABSW,          tcg_gen_gvec_abs, MO_16)
688UNARY_INT_GVEC(PABSD,          tcg_gen_gvec_abs, MO_32)
689UNARY_INT_GVEC(VBROADCASTx128, tcg_gen_gvec_dup_mem, MO_128)
690UNARY_INT_GVEC(VPBROADCASTB,   tcg_gen_gvec_dup_mem, MO_8)
691UNARY_INT_GVEC(VPBROADCASTW,   tcg_gen_gvec_dup_mem, MO_16)
692UNARY_INT_GVEC(VPBROADCASTD,   tcg_gen_gvec_dup_mem, MO_32)
693UNARY_INT_GVEC(VPBROADCASTQ,   tcg_gen_gvec_dup_mem, MO_64)
694
695
696#define BINARY_INT_GVEC(uname, func, ...)                                          \
697static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
698{                                                                                  \
699    int vec_len = vector_len(s, decode);                                          \
700                                                                                   \
701    func(__VA_ARGS__,                                                              \
702         decode->op[0].offset, decode->op[1].offset,                               \
703         decode->op[2].offset, vec_len, vec_len);                                  \
704}
705
706BINARY_INT_GVEC(PADDB,   tcg_gen_gvec_add, MO_8)
707BINARY_INT_GVEC(PADDW,   tcg_gen_gvec_add, MO_16)
708BINARY_INT_GVEC(PADDD,   tcg_gen_gvec_add, MO_32)
709BINARY_INT_GVEC(PADDQ,   tcg_gen_gvec_add, MO_64)
710BINARY_INT_GVEC(PADDSB,  tcg_gen_gvec_ssadd, MO_8)
711BINARY_INT_GVEC(PADDSW,  tcg_gen_gvec_ssadd, MO_16)
712BINARY_INT_GVEC(PADDUSB, tcg_gen_gvec_usadd, MO_8)
713BINARY_INT_GVEC(PADDUSW, tcg_gen_gvec_usadd, MO_16)
714BINARY_INT_GVEC(PAND,    tcg_gen_gvec_and, MO_64)
715BINARY_INT_GVEC(PCMPEQB, tcg_gen_gvec_cmp, TCG_COND_EQ, MO_8)
716BINARY_INT_GVEC(PCMPEQD, tcg_gen_gvec_cmp, TCG_COND_EQ, MO_32)
717BINARY_INT_GVEC(PCMPEQW, tcg_gen_gvec_cmp, TCG_COND_EQ, MO_16)
718BINARY_INT_GVEC(PCMPEQQ, tcg_gen_gvec_cmp, TCG_COND_EQ, MO_64)
719BINARY_INT_GVEC(PCMPGTB, tcg_gen_gvec_cmp, TCG_COND_GT, MO_8)
720BINARY_INT_GVEC(PCMPGTW, tcg_gen_gvec_cmp, TCG_COND_GT, MO_16)
721BINARY_INT_GVEC(PCMPGTD, tcg_gen_gvec_cmp, TCG_COND_GT, MO_32)
722BINARY_INT_GVEC(PCMPGTQ, tcg_gen_gvec_cmp, TCG_COND_GT, MO_64)
723BINARY_INT_GVEC(PMAXSB,  tcg_gen_gvec_smax, MO_8)
724BINARY_INT_GVEC(PMAXSW,  tcg_gen_gvec_smax, MO_16)
725BINARY_INT_GVEC(PMAXSD,  tcg_gen_gvec_smax, MO_32)
726BINARY_INT_GVEC(PMAXUB,  tcg_gen_gvec_umax, MO_8)
727BINARY_INT_GVEC(PMAXUW,  tcg_gen_gvec_umax, MO_16)
728BINARY_INT_GVEC(PMAXUD,  tcg_gen_gvec_umax, MO_32)
729BINARY_INT_GVEC(PMINSB,  tcg_gen_gvec_smin, MO_8)
730BINARY_INT_GVEC(PMINSW,  tcg_gen_gvec_smin, MO_16)
731BINARY_INT_GVEC(PMINSD,  tcg_gen_gvec_smin, MO_32)
732BINARY_INT_GVEC(PMINUB,  tcg_gen_gvec_umin, MO_8)
733BINARY_INT_GVEC(PMINUW,  tcg_gen_gvec_umin, MO_16)
734BINARY_INT_GVEC(PMINUD,  tcg_gen_gvec_umin, MO_32)
735BINARY_INT_GVEC(PMULLW,  tcg_gen_gvec_mul, MO_16)
736BINARY_INT_GVEC(PMULLD,  tcg_gen_gvec_mul, MO_32)
737BINARY_INT_GVEC(POR,     tcg_gen_gvec_or, MO_64)
738BINARY_INT_GVEC(PSUBB,   tcg_gen_gvec_sub, MO_8)
739BINARY_INT_GVEC(PSUBW,   tcg_gen_gvec_sub, MO_16)
740BINARY_INT_GVEC(PSUBD,   tcg_gen_gvec_sub, MO_32)
741BINARY_INT_GVEC(PSUBQ,   tcg_gen_gvec_sub, MO_64)
742BINARY_INT_GVEC(PSUBSB,  tcg_gen_gvec_sssub, MO_8)
743BINARY_INT_GVEC(PSUBSW,  tcg_gen_gvec_sssub, MO_16)
744BINARY_INT_GVEC(PSUBUSB, tcg_gen_gvec_ussub, MO_8)
745BINARY_INT_GVEC(PSUBUSW, tcg_gen_gvec_ussub, MO_16)
746BINARY_INT_GVEC(PXOR,    tcg_gen_gvec_xor, MO_64)
747
748
749/*
750 * 00 = p*  Pq, Qq (if mmx not NULL; no VEX)
751 * 66 = vp* Vx, Hx, Wx
752 *
753 * These are really the same encoding, because 1) V is the same as P when VEX.V
754 * is not present 2) P and Q are the same as H and W apart from MM/XMM
755 */
756static inline void gen_binary_int_sse(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode,
757                                      SSEFunc_0_eppp mmx, SSEFunc_0_eppp xmm, SSEFunc_0_eppp ymm)
758{
759    assert(!!mmx == !!(decode->e.special == X86_SPECIAL_MMX));
760
761    if (mmx && (s->prefix & PREFIX_VEX) && !(s->prefix & PREFIX_DATA)) {
762        /* VEX encoding is not applicable to MMX instructions.  */
763        gen_illegal_opcode(s);
764        return;
765    }
766    if (!(s->prefix & PREFIX_DATA)) {
767        mmx(cpu_env, OP_PTR0, OP_PTR1, OP_PTR2);
768    } else if (!s->vex_l) {
769        xmm(cpu_env, OP_PTR0, OP_PTR1, OP_PTR2);
770    } else {
771        ymm(cpu_env, OP_PTR0, OP_PTR1, OP_PTR2);
772    }
773}
774
775
776#define BINARY_INT_MMX(uname, lname)                                               \
777static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
778{                                                                                  \
779    gen_binary_int_sse(s, env, decode,                                             \
780                          gen_helper_##lname##_mmx,                                \
781                          gen_helper_##lname##_xmm,                                \
782                          gen_helper_##lname##_ymm);                               \
783}
784BINARY_INT_MMX(PUNPCKLBW,  punpcklbw)
785BINARY_INT_MMX(PUNPCKLWD,  punpcklwd)
786BINARY_INT_MMX(PUNPCKLDQ,  punpckldq)
787BINARY_INT_MMX(PACKSSWB,   packsswb)
788BINARY_INT_MMX(PACKUSWB,   packuswb)
789BINARY_INT_MMX(PUNPCKHBW,  punpckhbw)
790BINARY_INT_MMX(PUNPCKHWD,  punpckhwd)
791BINARY_INT_MMX(PUNPCKHDQ,  punpckhdq)
792BINARY_INT_MMX(PACKSSDW,   packssdw)
793
794BINARY_INT_MMX(PAVGB,   pavgb)
795BINARY_INT_MMX(PAVGW,   pavgw)
796BINARY_INT_MMX(PMADDWD, pmaddwd)
797BINARY_INT_MMX(PMULHUW, pmulhuw)
798BINARY_INT_MMX(PMULHW,  pmulhw)
799BINARY_INT_MMX(PMULUDQ, pmuludq)
800BINARY_INT_MMX(PSADBW,  psadbw)
801
802BINARY_INT_MMX(PSLLW_r, psllw)
803BINARY_INT_MMX(PSLLD_r, pslld)
804BINARY_INT_MMX(PSLLQ_r, psllq)
805BINARY_INT_MMX(PSRLW_r, psrlw)
806BINARY_INT_MMX(PSRLD_r, psrld)
807BINARY_INT_MMX(PSRLQ_r, psrlq)
808BINARY_INT_MMX(PSRAW_r, psraw)
809BINARY_INT_MMX(PSRAD_r, psrad)
810
811BINARY_INT_MMX(PHADDW,    phaddw)
812BINARY_INT_MMX(PHADDSW,   phaddsw)
813BINARY_INT_MMX(PHADDD,    phaddd)
814BINARY_INT_MMX(PHSUBW,    phsubw)
815BINARY_INT_MMX(PHSUBSW,   phsubsw)
816BINARY_INT_MMX(PHSUBD,    phsubd)
817BINARY_INT_MMX(PMADDUBSW, pmaddubsw)
818BINARY_INT_MMX(PSHUFB,    pshufb)
819BINARY_INT_MMX(PSIGNB,    psignb)
820BINARY_INT_MMX(PSIGNW,    psignw)
821BINARY_INT_MMX(PSIGND,    psignd)
822BINARY_INT_MMX(PMULHRSW,  pmulhrsw)
823
824/* Instructions with no MMX equivalent.  */
825#define BINARY_INT_SSE(uname, lname)                                               \
826static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
827{                                                                                  \
828    gen_binary_int_sse(s, env, decode,                                             \
829                          NULL,                                                    \
830                          gen_helper_##lname##_xmm,                                \
831                          gen_helper_##lname##_ymm);                               \
832}
833
834/* Instructions with no MMX equivalent.  */
835BINARY_INT_SSE(PUNPCKLQDQ, punpcklqdq)
836BINARY_INT_SSE(PUNPCKHQDQ, punpckhqdq)
837BINARY_INT_SSE(VPACKUSDW,  packusdw)
838BINARY_INT_SSE(VPERMILPS,  vpermilps)
839BINARY_INT_SSE(VPERMILPD,  vpermilpd)
840BINARY_INT_SSE(VMASKMOVPS, vpmaskmovd)
841BINARY_INT_SSE(VMASKMOVPD, vpmaskmovq)
842
843BINARY_INT_SSE(PMULDQ,    pmuldq)
844
845BINARY_INT_SSE(VAESDEC, aesdec)
846BINARY_INT_SSE(VAESDECLAST, aesdeclast)
847BINARY_INT_SSE(VAESENC, aesenc)
848BINARY_INT_SSE(VAESENCLAST, aesenclast)
849
850#define UNARY_CMP_SSE(uname, lname)                                                \
851static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
852{                                                                                  \
853    if (!s->vex_l) {                                                               \
854        gen_helper_##lname##_xmm(cpu_env, OP_PTR1, OP_PTR2);                       \
855    } else {                                                                       \
856        gen_helper_##lname##_ymm(cpu_env, OP_PTR1, OP_PTR2);                       \
857    }                                                                              \
858    set_cc_op(s, CC_OP_EFLAGS);                                                    \
859}
860UNARY_CMP_SSE(VPTEST,     ptest)
861UNARY_CMP_SSE(VTESTPS,    vtestps)
862UNARY_CMP_SSE(VTESTPD,    vtestpd)
863
864static inline void gen_unary_int_sse(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode,
865                                     SSEFunc_0_epp xmm, SSEFunc_0_epp ymm)
866{
867    if (!s->vex_l) {
868        xmm(cpu_env, OP_PTR0, OP_PTR2);
869    } else {
870        ymm(cpu_env, OP_PTR0, OP_PTR2);
871    }
872}
873
874#define UNARY_INT_SSE(uname, lname)                                                \
875static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
876{                                                                                  \
877    gen_unary_int_sse(s, env, decode,                                              \
878                      gen_helper_##lname##_xmm,                                    \
879                      gen_helper_##lname##_ymm);                                   \
880}
881
882UNARY_INT_SSE(VPMOVSXBW,    pmovsxbw)
883UNARY_INT_SSE(VPMOVSXBD,    pmovsxbd)
884UNARY_INT_SSE(VPMOVSXBQ,    pmovsxbq)
885UNARY_INT_SSE(VPMOVSXWD,    pmovsxwd)
886UNARY_INT_SSE(VPMOVSXWQ,    pmovsxwq)
887UNARY_INT_SSE(VPMOVSXDQ,    pmovsxdq)
888
889UNARY_INT_SSE(VPMOVZXBW,    pmovzxbw)
890UNARY_INT_SSE(VPMOVZXBD,    pmovzxbd)
891UNARY_INT_SSE(VPMOVZXBQ,    pmovzxbq)
892UNARY_INT_SSE(VPMOVZXWD,    pmovzxwd)
893UNARY_INT_SSE(VPMOVZXWQ,    pmovzxwq)
894UNARY_INT_SSE(VPMOVZXDQ,    pmovzxdq)
895
896UNARY_INT_SSE(VMOVSLDUP,    pmovsldup)
897UNARY_INT_SSE(VMOVSHDUP,    pmovshdup)
898UNARY_INT_SSE(VMOVDDUP,     pmovdldup)
899
900UNARY_INT_SSE(VCVTDQ2PD, cvtdq2pd)
901UNARY_INT_SSE(VCVTPD2DQ, cvtpd2dq)
902UNARY_INT_SSE(VCVTTPD2DQ, cvttpd2dq)
903UNARY_INT_SSE(VCVTDQ2PS, cvtdq2ps)
904UNARY_INT_SSE(VCVTPS2DQ, cvtps2dq)
905UNARY_INT_SSE(VCVTTPS2DQ, cvttps2dq)
906UNARY_INT_SSE(VCVTPH2PS, cvtph2ps)
907
908
909static inline void gen_unary_imm_sse(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode,
910                                     SSEFunc_0_ppi xmm, SSEFunc_0_ppi ymm)
911{
912    TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
913    if (!s->vex_l) {
914        xmm(OP_PTR0, OP_PTR1, imm);
915    } else {
916        ymm(OP_PTR0, OP_PTR1, imm);
917    }
918}
919
920#define UNARY_IMM_SSE(uname, lname)                                                \
921static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
922{                                                                                  \
923    gen_unary_imm_sse(s, env, decode,                                              \
924                      gen_helper_##lname##_xmm,                                    \
925                      gen_helper_##lname##_ymm);                                   \
926}
927
928UNARY_IMM_SSE(PSHUFD,     pshufd)
929UNARY_IMM_SSE(PSHUFHW,    pshufhw)
930UNARY_IMM_SSE(PSHUFLW,    pshuflw)
931#define gen_helper_vpermq_xmm NULL
932UNARY_IMM_SSE(VPERMQ,      vpermq)
933UNARY_IMM_SSE(VPERMILPS_i, vpermilps_imm)
934UNARY_IMM_SSE(VPERMILPD_i, vpermilpd_imm)
935
936static inline void gen_unary_imm_fp_sse(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode,
937                                        SSEFunc_0_eppi xmm, SSEFunc_0_eppi ymm)
938{
939    TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
940    if (!s->vex_l) {
941        xmm(cpu_env, OP_PTR0, OP_PTR1, imm);
942    } else {
943        ymm(cpu_env, OP_PTR0, OP_PTR1, imm);
944    }
945}
946
947#define UNARY_IMM_FP_SSE(uname, lname)                                             \
948static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
949{                                                                                  \
950    gen_unary_imm_fp_sse(s, env, decode,                                           \
951                      gen_helper_##lname##_xmm,                                    \
952                      gen_helper_##lname##_ymm);                                   \
953}
954
955UNARY_IMM_FP_SSE(VROUNDPS,    roundps)
956UNARY_IMM_FP_SSE(VROUNDPD,    roundpd)
957
958static inline void gen_vexw_avx(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode,
959                                SSEFunc_0_eppp d_xmm, SSEFunc_0_eppp q_xmm,
960                                SSEFunc_0_eppp d_ymm, SSEFunc_0_eppp q_ymm)
961{
962    SSEFunc_0_eppp d = s->vex_l ? d_ymm : d_xmm;
963    SSEFunc_0_eppp q = s->vex_l ? q_ymm : q_xmm;
964    SSEFunc_0_eppp fn = s->vex_w ? q : d;
965    fn(cpu_env, OP_PTR0, OP_PTR1, OP_PTR2);
966}
967
968/* VEX.W affects whether to operate on 32- or 64-bit elements.  */
969#define VEXW_AVX(uname, lname)                                                     \
970static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
971{                                                                                  \
972    gen_vexw_avx(s, env, decode,                                                   \
973                 gen_helper_##lname##d_xmm, gen_helper_##lname##q_xmm,             \
974                 gen_helper_##lname##d_ymm, gen_helper_##lname##q_ymm);            \
975}
976VEXW_AVX(VPSLLV,    vpsllv)
977VEXW_AVX(VPSRLV,    vpsrlv)
978VEXW_AVX(VPSRAV,    vpsrav)
979VEXW_AVX(VPMASKMOV, vpmaskmov)
980
981/* Same as above, but with extra arguments to the helper.  */
982static inline void gen_vsib_avx(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode,
983                                SSEFunc_0_epppti d_xmm, SSEFunc_0_epppti q_xmm,
984                                SSEFunc_0_epppti d_ymm, SSEFunc_0_epppti q_ymm)
985{
986    SSEFunc_0_epppti d = s->vex_l ? d_ymm : d_xmm;
987    SSEFunc_0_epppti q = s->vex_l ? q_ymm : q_xmm;
988    SSEFunc_0_epppti fn = s->vex_w ? q : d;
989    TCGv_i32 scale = tcg_constant_i32(decode->mem.scale);
990    TCGv_ptr index = tcg_temp_new_ptr();
991
992    /* Pass third input as (index, base, scale) */
993    tcg_gen_addi_ptr(index, cpu_env, ZMM_OFFSET(decode->mem.index));
994    fn(cpu_env, OP_PTR0, OP_PTR1, index, s->A0, scale);
995
996    /*
997     * There are two output operands, so zero OP1's high 128 bits
998     * in the VEX.128 case.
999     */
1000    if (!s->vex_l) {
1001        int ymmh_ofs = vector_elem_offset(&decode->op[1], MO_128, 1);
1002        tcg_gen_gvec_dup_imm(MO_64, ymmh_ofs, 16, 16, 0);
1003    }
1004    tcg_temp_free_ptr(index);
1005}
1006#define VSIB_AVX(uname, lname)                                                     \
1007static void gen_##uname(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode) \
1008{                                                                                  \
1009    gen_vsib_avx(s, env, decode,                                                   \
1010                 gen_helper_##lname##d_xmm, gen_helper_##lname##q_xmm,             \
1011                 gen_helper_##lname##d_ymm, gen_helper_##lname##q_ymm);            \
1012}
1013VSIB_AVX(VPGATHERD, vpgatherd)
1014VSIB_AVX(VPGATHERQ, vpgatherq)
1015
1016static void gen_ADCOX(DisasContext *s, CPUX86State *env, MemOp ot, int cc_op)
1017{
1018    int opposite_cc_op;
1019    TCGv carry_in = NULL;
1020    TCGv carry_out = (cc_op == CC_OP_ADCX ? cpu_cc_dst : cpu_cc_src2);
1021    TCGv zero;
1022
1023    if (cc_op == s->cc_op || s->cc_op == CC_OP_ADCOX) {
1024        /* Re-use the carry-out from a previous round.  */
1025        carry_in = carry_out;
1026    } else {
1027        /* We don't have a carry-in, get it out of EFLAGS.  */
1028        if (s->cc_op != CC_OP_ADCX && s->cc_op != CC_OP_ADOX) {
1029            gen_compute_eflags(s);
1030        }
1031        carry_in = s->tmp0;
1032        tcg_gen_extract_tl(carry_in, cpu_cc_src,
1033            ctz32(cc_op == CC_OP_ADCX ? CC_C : CC_O), 1);
1034    }
1035
1036    switch (ot) {
1037#ifdef TARGET_X86_64
1038    case MO_32:
1039        /* If TL is 64-bit just do everything in 64-bit arithmetic.  */
1040        tcg_gen_ext32u_tl(s->T0, s->T0);
1041        tcg_gen_ext32u_tl(s->T1, s->T1);
1042        tcg_gen_add_i64(s->T0, s->T0, s->T1);
1043        tcg_gen_add_i64(s->T0, s->T0, carry_in);
1044        tcg_gen_shri_i64(carry_out, s->T0, 32);
1045        break;
1046#endif
1047    default:
1048        zero = tcg_constant_tl(0);
1049        tcg_gen_add2_tl(s->T0, carry_out, s->T0, zero, carry_in, zero);
1050        tcg_gen_add2_tl(s->T0, carry_out, s->T0, carry_out, s->T1, zero);
1051        break;
1052    }
1053
1054    opposite_cc_op = cc_op == CC_OP_ADCX ? CC_OP_ADOX : CC_OP_ADCX;
1055    if (s->cc_op == CC_OP_ADCOX || s->cc_op == opposite_cc_op) {
1056        /* Merge with the carry-out from the opposite instruction.  */
1057        set_cc_op(s, CC_OP_ADCOX);
1058    } else {
1059        set_cc_op(s, cc_op);
1060    }
1061}
1062
1063static void gen_ADCX(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1064{
1065    gen_ADCOX(s, env, decode->op[0].ot, CC_OP_ADCX);
1066}
1067
1068static void gen_ADOX(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1069{
1070    gen_ADCOX(s, env, decode->op[0].ot, CC_OP_ADOX);
1071}
1072
1073static void gen_ANDN(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1074{
1075    MemOp ot = decode->op[0].ot;
1076
1077    tcg_gen_andc_tl(s->T0, s->T1, s->T0);
1078    gen_op_update1_cc(s);
1079    set_cc_op(s, CC_OP_LOGICB + ot);
1080}
1081
1082static void gen_BEXTR(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1083{
1084    MemOp ot = decode->op[0].ot;
1085    TCGv bound = tcg_constant_tl(ot == MO_64 ? 63 : 31);
1086    TCGv zero = tcg_constant_tl(0);
1087    TCGv mone = tcg_constant_tl(-1);
1088
1089    /*
1090     * Extract START, and shift the operand.
1091     * Shifts larger than operand size get zeros.
1092     */
1093    tcg_gen_ext8u_tl(s->A0, s->T1);
1094    if (TARGET_LONG_BITS == 64 && ot == MO_32) {
1095        tcg_gen_ext32u_tl(s->T0, s->T0);
1096    }
1097    tcg_gen_shr_tl(s->T0, s->T0, s->A0);
1098
1099    tcg_gen_movcond_tl(TCG_COND_LEU, s->T0, s->A0, bound, s->T0, zero);
1100
1101    /*
1102     * Extract the LEN into an inverse mask.  Lengths larger than
1103     * operand size get all zeros, length 0 gets all ones.
1104     */
1105    tcg_gen_extract_tl(s->A0, s->T1, 8, 8);
1106    tcg_gen_shl_tl(s->T1, mone, s->A0);
1107    tcg_gen_movcond_tl(TCG_COND_LEU, s->T1, s->A0, bound, s->T1, zero);
1108    tcg_gen_andc_tl(s->T0, s->T0, s->T1);
1109
1110    gen_op_update1_cc(s);
1111    set_cc_op(s, CC_OP_LOGICB + ot);
1112}
1113
1114static void gen_BLSI(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1115{
1116    MemOp ot = decode->op[0].ot;
1117
1118    tcg_gen_mov_tl(cpu_cc_src, s->T0);
1119    tcg_gen_neg_tl(s->T1, s->T0);
1120    tcg_gen_and_tl(s->T0, s->T0, s->T1);
1121    tcg_gen_mov_tl(cpu_cc_dst, s->T0);
1122    set_cc_op(s, CC_OP_BMILGB + ot);
1123}
1124
1125static void gen_BLSMSK(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1126{
1127    MemOp ot = decode->op[0].ot;
1128
1129    tcg_gen_mov_tl(cpu_cc_src, s->T0);
1130    tcg_gen_subi_tl(s->T1, s->T0, 1);
1131    tcg_gen_xor_tl(s->T0, s->T0, s->T1);
1132    tcg_gen_mov_tl(cpu_cc_dst, s->T0);
1133    set_cc_op(s, CC_OP_BMILGB + ot);
1134}
1135
1136static void gen_BLSR(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1137{
1138    MemOp ot = decode->op[0].ot;
1139
1140    tcg_gen_mov_tl(cpu_cc_src, s->T0);
1141    tcg_gen_subi_tl(s->T1, s->T0, 1);
1142    tcg_gen_and_tl(s->T0, s->T0, s->T1);
1143    tcg_gen_mov_tl(cpu_cc_dst, s->T0);
1144    set_cc_op(s, CC_OP_BMILGB + ot);
1145}
1146
1147static void gen_BZHI(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1148{
1149    MemOp ot = decode->op[0].ot;
1150    TCGv bound;
1151
1152    tcg_gen_ext8u_tl(s->T1, cpu_regs[s->vex_v]);
1153    bound = tcg_constant_tl(ot == MO_64 ? 63 : 31);
1154
1155    /*
1156     * Note that since we're using BMILG (in order to get O
1157     * cleared) we need to store the inverse into C.
1158     */
1159    tcg_gen_setcond_tl(TCG_COND_LT, cpu_cc_src, s->T1, bound);
1160    tcg_gen_movcond_tl(TCG_COND_GT, s->T1, s->T1, bound, bound, s->T1);
1161
1162    tcg_gen_movi_tl(s->A0, -1);
1163    tcg_gen_shl_tl(s->A0, s->A0, s->T1);
1164    tcg_gen_andc_tl(s->T0, s->T0, s->A0);
1165
1166    gen_op_update1_cc(s);
1167    set_cc_op(s, CC_OP_BMILGB + ot);
1168}
1169
1170static void gen_CRC32(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1171{
1172    MemOp ot = decode->op[2].ot;
1173
1174    tcg_gen_trunc_tl_i32(s->tmp2_i32, s->T0);
1175    gen_helper_crc32(s->T0, s->tmp2_i32, s->T1, tcg_constant_i32(8 << ot));
1176}
1177
1178static void gen_CVTPI2Px(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1179{
1180    gen_helper_enter_mmx(cpu_env);
1181    if (s->prefix & PREFIX_DATA) {
1182        gen_helper_cvtpi2pd(cpu_env, OP_PTR0, OP_PTR2);
1183    } else {
1184        gen_helper_cvtpi2ps(cpu_env, OP_PTR0, OP_PTR2);
1185    }
1186}
1187
1188static void gen_CVTPx2PI(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1189{
1190    gen_helper_enter_mmx(cpu_env);
1191    if (s->prefix & PREFIX_DATA) {
1192        gen_helper_cvtpd2pi(cpu_env, OP_PTR0, OP_PTR2);
1193    } else {
1194        gen_helper_cvtps2pi(cpu_env, OP_PTR0, OP_PTR2);
1195    }
1196}
1197
1198static void gen_CVTTPx2PI(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1199{
1200    gen_helper_enter_mmx(cpu_env);
1201    if (s->prefix & PREFIX_DATA) {
1202        gen_helper_cvttpd2pi(cpu_env, OP_PTR0, OP_PTR2);
1203    } else {
1204        gen_helper_cvttps2pi(cpu_env, OP_PTR0, OP_PTR2);
1205    }
1206}
1207
1208static void gen_EMMS(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1209{
1210    gen_helper_emms(cpu_env);
1211}
1212
1213static void gen_EXTRQ_i(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1214{
1215    TCGv_i32 length = tcg_constant_i32(decode->immediate & 63);
1216    TCGv_i32 index = tcg_constant_i32((decode->immediate >> 8) & 63);
1217
1218    gen_helper_extrq_i(cpu_env, OP_PTR0, index, length);
1219}
1220
1221static void gen_EXTRQ_r(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1222{
1223    gen_helper_extrq_r(cpu_env, OP_PTR0, OP_PTR2);
1224}
1225
1226static void gen_INSERTQ_i(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1227{
1228    TCGv_i32 length = tcg_constant_i32(decode->immediate & 63);
1229    TCGv_i32 index = tcg_constant_i32((decode->immediate >> 8) & 63);
1230
1231    gen_helper_insertq_i(cpu_env, OP_PTR0, OP_PTR1, index, length);
1232}
1233
1234static void gen_INSERTQ_r(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1235{
1236    gen_helper_insertq_r(cpu_env, OP_PTR0, OP_PTR2);
1237}
1238
1239static void gen_LDMXCSR(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1240{
1241    if (s->vex_l) {
1242        gen_illegal_opcode(s);
1243        return;
1244    }
1245    tcg_gen_trunc_tl_i32(s->tmp2_i32, s->T1);
1246    gen_helper_ldmxcsr(cpu_env, s->tmp2_i32);
1247}
1248
1249static void gen_MASKMOV(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1250{
1251    tcg_gen_mov_tl(s->A0, cpu_regs[R_EDI]);
1252    gen_extu(s->aflag, s->A0);
1253    gen_add_A0_ds_seg(s);
1254
1255    if (s->prefix & PREFIX_DATA) {
1256        gen_helper_maskmov_xmm(cpu_env, OP_PTR1, OP_PTR2, s->A0);
1257    } else {
1258        gen_helper_maskmov_mmx(cpu_env, OP_PTR1, OP_PTR2, s->A0);
1259    }
1260}
1261
1262static void gen_MOVBE(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1263{
1264    MemOp ot = decode->op[0].ot;
1265
1266    /* M operand type does not load/store */
1267    if (decode->e.op0 == X86_TYPE_M) {
1268        tcg_gen_qemu_st_tl(s->T0, s->A0, s->mem_index, ot | MO_BE);
1269    } else {
1270        tcg_gen_qemu_ld_tl(s->T0, s->A0, s->mem_index, ot | MO_BE);
1271    }
1272}
1273
1274static void gen_MOVD_from(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1275{
1276    MemOp ot = decode->op[2].ot;
1277
1278    switch (ot) {
1279    case MO_32:
1280#ifdef TARGET_X86_64
1281        tcg_gen_ld32u_tl(s->T0, cpu_env, decode->op[2].offset);
1282        break;
1283    case MO_64:
1284#endif
1285        tcg_gen_ld_tl(s->T0, cpu_env, decode->op[2].offset);
1286        break;
1287    default:
1288        abort();
1289    }
1290}
1291
1292static void gen_MOVD_to(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1293{
1294    MemOp ot = decode->op[2].ot;
1295    int vec_len = vector_len(s, decode);
1296    int lo_ofs = vector_elem_offset(&decode->op[0], ot, 0);
1297
1298    tcg_gen_gvec_dup_imm(MO_64, decode->op[0].offset, vec_len, vec_len, 0);
1299
1300    switch (ot) {
1301    case MO_32:
1302#ifdef TARGET_X86_64
1303        tcg_gen_st32_tl(s->T1, cpu_env, lo_ofs);
1304        break;
1305    case MO_64:
1306#endif
1307        tcg_gen_st_tl(s->T1, cpu_env, lo_ofs);
1308        break;
1309    default:
1310        g_assert_not_reached();
1311    }
1312}
1313
1314static void gen_MOVDQ(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1315{
1316    gen_store_sse(s, decode, decode->op[2].offset);
1317}
1318
1319static void gen_MOVMSK(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1320{
1321    typeof(gen_helper_movmskps_ymm) *ps, *pd, *fn;
1322    ps = s->vex_l ? gen_helper_movmskps_ymm : gen_helper_movmskps_xmm;
1323    pd = s->vex_l ? gen_helper_movmskpd_ymm : gen_helper_movmskpd_xmm;
1324    fn = s->prefix & PREFIX_DATA ? pd : ps;
1325    fn(s->tmp2_i32, cpu_env, OP_PTR2);
1326    tcg_gen_extu_i32_tl(s->T0, s->tmp2_i32);
1327}
1328
1329static void gen_MOVQ(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1330{
1331    int vec_len = vector_len(s, decode);
1332    int lo_ofs = vector_elem_offset(&decode->op[0], MO_64, 0);
1333
1334    tcg_gen_ld_i64(s->tmp1_i64, cpu_env, decode->op[2].offset);
1335    if (decode->op[0].has_ea) {
1336        tcg_gen_qemu_st_i64(s->tmp1_i64, s->A0, s->mem_index, MO_LEUQ);
1337    } else {
1338        /*
1339         * tcg_gen_gvec_dup_i64(MO_64, op0.offset, 8, vec_len, s->tmp1_64) would
1340         * seem to work, but it does not on big-endian platforms; the cleared parts
1341         * are always at higher addresses, but cross-endian emulation inverts the
1342         * byte order so that the cleared parts need to be at *lower* addresses.
1343         * Because oprsz is 8, we see this here even for SSE; but more in general,
1344         * it disqualifies using oprsz < maxsz to emulate VEX128.
1345         */
1346        tcg_gen_gvec_dup_imm(MO_64, decode->op[0].offset, vec_len, vec_len, 0);
1347        tcg_gen_st_i64(s->tmp1_i64, cpu_env, lo_ofs);
1348    }
1349}
1350
1351static void gen_MOVq_dq(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1352{
1353    gen_helper_enter_mmx(cpu_env);
1354    /* Otherwise the same as any other movq.  */
1355    return gen_MOVQ(s, env, decode);
1356}
1357
1358static void gen_MULX(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1359{
1360    MemOp ot = decode->op[0].ot;
1361
1362    /* low part of result in VEX.vvvv, high in MODRM */
1363    switch (ot) {
1364    default:
1365        tcg_gen_trunc_tl_i32(s->tmp2_i32, s->T0);
1366        tcg_gen_trunc_tl_i32(s->tmp3_i32, s->T1);
1367        tcg_gen_mulu2_i32(s->tmp2_i32, s->tmp3_i32,
1368                          s->tmp2_i32, s->tmp3_i32);
1369        tcg_gen_extu_i32_tl(cpu_regs[s->vex_v], s->tmp2_i32);
1370        tcg_gen_extu_i32_tl(s->T0, s->tmp3_i32);
1371        break;
1372#ifdef TARGET_X86_64
1373    case MO_64:
1374        tcg_gen_mulu2_i64(cpu_regs[s->vex_v], s->T0, s->T0, s->T1);
1375        break;
1376#endif
1377    }
1378
1379}
1380
1381static void gen_PALIGNR(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1382{
1383    TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
1384    if (!(s->prefix & PREFIX_DATA)) {
1385        gen_helper_palignr_mmx(cpu_env, OP_PTR0, OP_PTR1, OP_PTR2, imm);
1386    } else if (!s->vex_l) {
1387        gen_helper_palignr_xmm(cpu_env, OP_PTR0, OP_PTR1, OP_PTR2, imm);
1388    } else {
1389        gen_helper_palignr_ymm(cpu_env, OP_PTR0, OP_PTR1, OP_PTR2, imm);
1390    }
1391}
1392
1393static void gen_PANDN(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1394{
1395    int vec_len = vector_len(s, decode);
1396
1397    /* Careful, operand order is reversed!  */
1398    tcg_gen_gvec_andc(MO_64,
1399                      decode->op[0].offset, decode->op[2].offset,
1400                      decode->op[1].offset, vec_len, vec_len);
1401}
1402
1403static void gen_PCMPESTRI(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1404{
1405    TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
1406    gen_helper_pcmpestri_xmm(cpu_env, OP_PTR1, OP_PTR2, imm);
1407    set_cc_op(s, CC_OP_EFLAGS);
1408}
1409
1410static void gen_PCMPESTRM(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1411{
1412    TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
1413    gen_helper_pcmpestrm_xmm(cpu_env, OP_PTR1, OP_PTR2, imm);
1414    set_cc_op(s, CC_OP_EFLAGS);
1415    if ((s->prefix & PREFIX_VEX) && !s->vex_l) {
1416        tcg_gen_gvec_dup_imm(MO_64, offsetof(CPUX86State, xmm_regs[0].ZMM_X(1)),
1417                             16, 16, 0);
1418    }
1419}
1420
1421static void gen_PCMPISTRI(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1422{
1423    TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
1424    gen_helper_pcmpistri_xmm(cpu_env, OP_PTR1, OP_PTR2, imm);
1425    set_cc_op(s, CC_OP_EFLAGS);
1426}
1427
1428static void gen_PCMPISTRM(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1429{
1430    TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
1431    gen_helper_pcmpistrm_xmm(cpu_env, OP_PTR1, OP_PTR2, imm);
1432    set_cc_op(s, CC_OP_EFLAGS);
1433    if ((s->prefix & PREFIX_VEX) && !s->vex_l) {
1434        tcg_gen_gvec_dup_imm(MO_64, offsetof(CPUX86State, xmm_regs[0].ZMM_X(1)),
1435                             16, 16, 0);
1436    }
1437}
1438
1439static void gen_PDEP(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1440{
1441    MemOp ot = decode->op[1].ot;
1442    if (ot < MO_64) {
1443        tcg_gen_ext32u_tl(s->T0, s->T0);
1444    }
1445    gen_helper_pdep(s->T0, s->T0, s->T1);
1446}
1447
1448static void gen_PEXT(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1449{
1450    MemOp ot = decode->op[1].ot;
1451    if (ot < MO_64) {
1452        tcg_gen_ext32u_tl(s->T0, s->T0);
1453    }
1454    gen_helper_pext(s->T0, s->T0, s->T1);
1455}
1456
1457static inline void gen_pextr(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode, MemOp ot)
1458{
1459    int vec_len = vector_len(s, decode);
1460    int mask = (vec_len >> ot) - 1;
1461    int val = decode->immediate & mask;
1462
1463    switch (ot) {
1464    case MO_8:
1465        tcg_gen_ld8u_tl(s->T0, cpu_env, vector_elem_offset(&decode->op[1], ot, val));
1466        break;
1467    case MO_16:
1468        tcg_gen_ld16u_tl(s->T0, cpu_env, vector_elem_offset(&decode->op[1], ot, val));
1469        break;
1470    case MO_32:
1471#ifdef TARGET_X86_64
1472        tcg_gen_ld32u_tl(s->T0, cpu_env, vector_elem_offset(&decode->op[1], ot, val));
1473        break;
1474    case MO_64:
1475#endif
1476        tcg_gen_ld_tl(s->T0, cpu_env, vector_elem_offset(&decode->op[1], ot, val));
1477        break;
1478    default:
1479        abort();
1480    }
1481}
1482
1483static void gen_PEXTRB(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1484{
1485    gen_pextr(s, env, decode, MO_8);
1486}
1487
1488static void gen_PEXTRW(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1489{
1490    gen_pextr(s, env, decode, MO_16);
1491}
1492
1493static void gen_PEXTR(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1494{
1495    MemOp ot = decode->op[0].ot;
1496    gen_pextr(s, env, decode, ot);
1497}
1498
1499static inline void gen_pinsr(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode, MemOp ot)
1500{
1501    int vec_len = vector_len(s, decode);
1502    int mask = (vec_len >> ot) - 1;
1503    int val = decode->immediate & mask;
1504
1505    if (decode->op[1].offset != decode->op[0].offset) {
1506        assert(vec_len == 16);
1507        gen_store_sse(s, decode, decode->op[1].offset);
1508    }
1509
1510    switch (ot) {
1511    case MO_8:
1512        tcg_gen_st8_tl(s->T1, cpu_env, vector_elem_offset(&decode->op[0], ot, val));
1513        break;
1514    case MO_16:
1515        tcg_gen_st16_tl(s->T1, cpu_env, vector_elem_offset(&decode->op[0], ot, val));
1516        break;
1517    case MO_32:
1518#ifdef TARGET_X86_64
1519        tcg_gen_st32_tl(s->T1, cpu_env, vector_elem_offset(&decode->op[0], ot, val));
1520        break;
1521    case MO_64:
1522#endif
1523        tcg_gen_st_tl(s->T1, cpu_env, vector_elem_offset(&decode->op[0], ot, val));
1524        break;
1525    default:
1526        abort();
1527    }
1528}
1529
1530static void gen_PINSRB(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1531{
1532    gen_pinsr(s, env, decode, MO_8);
1533}
1534
1535static void gen_PINSRW(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1536{
1537    gen_pinsr(s, env, decode, MO_16);
1538}
1539
1540static void gen_PINSR(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1541{
1542    gen_pinsr(s, env, decode, decode->op[2].ot);
1543}
1544
1545static void gen_pmovmskb_i64(TCGv_i64 d, TCGv_i64 s)
1546{
1547    TCGv_i64 t = tcg_temp_new_i64();
1548
1549    tcg_gen_andi_i64(d, s, 0x8080808080808080ull);
1550
1551    /*
1552     * After each shift+or pair:
1553     * 0:  a.......b.......c.......d.......e.......f.......g.......h.......
1554     * 7:  ab......bc......cd......de......ef......fg......gh......h.......
1555     * 14: abcd....bcde....cdef....defg....efgh....fgh.....gh......h.......
1556     * 28: abcdefghbcdefgh.cdefgh..defgh...efgh....fgh.....gh......h.......
1557     * The result is left in the high bits of the word.
1558     */
1559    tcg_gen_shli_i64(t, d, 7);
1560    tcg_gen_or_i64(d, d, t);
1561    tcg_gen_shli_i64(t, d, 14);
1562    tcg_gen_or_i64(d, d, t);
1563    tcg_gen_shli_i64(t, d, 28);
1564    tcg_gen_or_i64(d, d, t);
1565}
1566
1567static void gen_pmovmskb_vec(unsigned vece, TCGv_vec d, TCGv_vec s)
1568{
1569    TCGv_vec t = tcg_temp_new_vec_matching(d);
1570    TCGv_vec m = tcg_constant_vec_matching(d, MO_8, 0x80);
1571
1572    /* See above */
1573    tcg_gen_and_vec(vece, d, s, m);
1574    tcg_gen_shli_vec(vece, t, d, 7);
1575    tcg_gen_or_vec(vece, d, d, t);
1576    tcg_gen_shli_vec(vece, t, d, 14);
1577    tcg_gen_or_vec(vece, d, d, t);
1578    tcg_gen_shli_vec(vece, t, d, 28);
1579    tcg_gen_or_vec(vece, d, d, t);
1580}
1581
1582#ifdef TARGET_X86_64
1583#define TCG_TARGET_HAS_extract2_tl TCG_TARGET_HAS_extract2_i64
1584#else
1585#define TCG_TARGET_HAS_extract2_tl TCG_TARGET_HAS_extract2_i32
1586#endif
1587
1588static void gen_PMOVMSKB(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1589{
1590    static const TCGOpcode vecop_list[] = { INDEX_op_shli_vec, 0 };
1591    static const GVecGen2 g = {
1592        .fni8 = gen_pmovmskb_i64,
1593        .fniv = gen_pmovmskb_vec,
1594        .opt_opc = vecop_list,
1595        .vece = MO_64,
1596        .prefer_i64 = TCG_TARGET_REG_BITS == 64
1597    };
1598    MemOp ot = decode->op[2].ot;
1599    int vec_len = vector_len(s, decode);
1600    TCGv t = tcg_temp_new();
1601
1602    tcg_gen_gvec_2(offsetof(CPUX86State, xmm_t0) + xmm_offset(ot), decode->op[2].offset,
1603                   vec_len, vec_len, &g);
1604    tcg_gen_ld8u_tl(s->T0, cpu_env, offsetof(CPUX86State, xmm_t0.ZMM_B(vec_len - 1)));
1605    while (vec_len > 8) {
1606        vec_len -= 8;
1607        if (TCG_TARGET_HAS_extract2_tl) {
1608            /*
1609             * Load the next byte of the result into the high byte of T.
1610             * TCG does a similar expansion of deposit to shl+extract2; by
1611             * loading the whole word, the shift left is avoided.
1612             */
1613#ifdef TARGET_X86_64
1614            tcg_gen_ld_tl(t, cpu_env, offsetof(CPUX86State, xmm_t0.ZMM_Q((vec_len - 1) / 8)));
1615#else
1616            tcg_gen_ld_tl(t, cpu_env, offsetof(CPUX86State, xmm_t0.ZMM_L((vec_len - 1) / 4)));
1617#endif
1618
1619            tcg_gen_extract2_tl(s->T0, t, s->T0, TARGET_LONG_BITS - 8);
1620        } else {
1621            /*
1622             * The _previous_ value is deposited into bits 8 and higher of t.  Because
1623             * those bits are known to be zero after ld8u, this becomes a shift+or
1624             * if deposit is not available.
1625             */
1626            tcg_gen_ld8u_tl(t, cpu_env, offsetof(CPUX86State, xmm_t0.ZMM_B(vec_len - 1)));
1627            tcg_gen_deposit_tl(s->T0, t, s->T0, 8, TARGET_LONG_BITS - 8);
1628        }
1629    }
1630    tcg_temp_free(t);
1631}
1632
1633static void gen_PSHUFW(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1634{
1635    TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
1636    gen_helper_pshufw_mmx(OP_PTR0, OP_PTR1, imm);
1637}
1638
1639static void gen_PSRLW_i(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1640{
1641    int vec_len = vector_len(s, decode);
1642
1643    if (decode->immediate >= 16) {
1644        tcg_gen_gvec_dup_imm(MO_64, decode->op[0].offset, vec_len, vec_len, 0);
1645    } else {
1646        tcg_gen_gvec_shri(MO_16,
1647                          decode->op[0].offset, decode->op[1].offset,
1648                          decode->immediate, vec_len, vec_len);
1649    }
1650}
1651
1652static void gen_PSLLW_i(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1653{
1654    int vec_len = vector_len(s, decode);
1655
1656    if (decode->immediate >= 16) {
1657        tcg_gen_gvec_dup_imm(MO_64, decode->op[0].offset, vec_len, vec_len, 0);
1658    } else {
1659        tcg_gen_gvec_shli(MO_16,
1660                          decode->op[0].offset, decode->op[1].offset,
1661                          decode->immediate, vec_len, vec_len);
1662    }
1663}
1664
1665static void gen_PSRAW_i(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1666{
1667    int vec_len = vector_len(s, decode);
1668
1669    if (decode->immediate >= 16) {
1670        decode->immediate = 15;
1671    }
1672    tcg_gen_gvec_sari(MO_16,
1673                      decode->op[0].offset, decode->op[1].offset,
1674                      decode->immediate, vec_len, vec_len);
1675}
1676
1677static void gen_PSRLD_i(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1678{
1679    int vec_len = vector_len(s, decode);
1680
1681    if (decode->immediate >= 32) {
1682        tcg_gen_gvec_dup_imm(MO_64, decode->op[0].offset, vec_len, vec_len, 0);
1683    } else {
1684        tcg_gen_gvec_shri(MO_32,
1685                          decode->op[0].offset, decode->op[1].offset,
1686                          decode->immediate, vec_len, vec_len);
1687    }
1688}
1689
1690static void gen_PSLLD_i(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1691{
1692    int vec_len = vector_len(s, decode);
1693
1694    if (decode->immediate >= 32) {
1695        tcg_gen_gvec_dup_imm(MO_64, decode->op[0].offset, vec_len, vec_len, 0);
1696    } else {
1697        tcg_gen_gvec_shli(MO_32,
1698                          decode->op[0].offset, decode->op[1].offset,
1699                          decode->immediate, vec_len, vec_len);
1700    }
1701}
1702
1703static void gen_PSRAD_i(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1704{
1705    int vec_len = vector_len(s, decode);
1706
1707    if (decode->immediate >= 32) {
1708        decode->immediate = 31;
1709    }
1710    tcg_gen_gvec_sari(MO_32,
1711                      decode->op[0].offset, decode->op[1].offset,
1712                      decode->immediate, vec_len, vec_len);
1713}
1714
1715static void gen_PSRLQ_i(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1716{
1717    int vec_len = vector_len(s, decode);
1718
1719    if (decode->immediate >= 64) {
1720        tcg_gen_gvec_dup_imm(MO_64, decode->op[0].offset, vec_len, vec_len, 0);
1721    } else {
1722        tcg_gen_gvec_shri(MO_64,
1723                          decode->op[0].offset, decode->op[1].offset,
1724                          decode->immediate, vec_len, vec_len);
1725    }
1726}
1727
1728static void gen_PSLLQ_i(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1729{
1730    int vec_len = vector_len(s, decode);
1731
1732    if (decode->immediate >= 64) {
1733        tcg_gen_gvec_dup_imm(MO_64, decode->op[0].offset, vec_len, vec_len, 0);
1734    } else {
1735        tcg_gen_gvec_shli(MO_64,
1736                          decode->op[0].offset, decode->op[1].offset,
1737                          decode->immediate, vec_len, vec_len);
1738    }
1739}
1740
1741static TCGv_ptr make_imm8u_xmm_vec(uint8_t imm, int vec_len)
1742{
1743    MemOp ot = vec_len == 16 ? MO_128 : MO_256;
1744    TCGv_i32 imm_v = tcg_constant8u_i32(imm);
1745    TCGv_ptr ptr = tcg_temp_new_ptr();
1746
1747    tcg_gen_gvec_dup_imm(MO_64, offsetof(CPUX86State, xmm_t0) + xmm_offset(ot),
1748                         vec_len, vec_len, 0);
1749
1750    tcg_gen_addi_ptr(ptr, cpu_env, offsetof(CPUX86State, xmm_t0));
1751    tcg_gen_st_i32(imm_v, cpu_env, offsetof(CPUX86State, xmm_t0.ZMM_L(0)));
1752    return ptr;
1753}
1754
1755static void gen_PSRLDQ_i(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1756{
1757    int vec_len = vector_len(s, decode);
1758    TCGv_ptr imm_vec = make_imm8u_xmm_vec(decode->immediate, vec_len);
1759
1760    if (s->vex_l) {
1761        gen_helper_psrldq_ymm(cpu_env, OP_PTR0, OP_PTR1, imm_vec);
1762    } else {
1763        gen_helper_psrldq_xmm(cpu_env, OP_PTR0, OP_PTR1, imm_vec);
1764    }
1765    tcg_temp_free_ptr(imm_vec);
1766}
1767
1768static void gen_PSLLDQ_i(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1769{
1770    int vec_len = vector_len(s, decode);
1771    TCGv_ptr imm_vec = make_imm8u_xmm_vec(decode->immediate, vec_len);
1772
1773    if (s->vex_l) {
1774        gen_helper_pslldq_ymm(cpu_env, OP_PTR0, OP_PTR1, imm_vec);
1775    } else {
1776        gen_helper_pslldq_xmm(cpu_env, OP_PTR0, OP_PTR1, imm_vec);
1777    }
1778    tcg_temp_free_ptr(imm_vec);
1779}
1780
1781static void gen_RORX(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1782{
1783    MemOp ot = decode->op[0].ot;
1784    int b = decode->immediate;
1785
1786    if (ot == MO_64) {
1787        tcg_gen_rotri_tl(s->T0, s->T0, b & 63);
1788    } else {
1789        tcg_gen_trunc_tl_i32(s->tmp2_i32, s->T0);
1790        tcg_gen_rotri_i32(s->tmp2_i32, s->tmp2_i32, b & 31);
1791        tcg_gen_extu_i32_tl(s->T0, s->tmp2_i32);
1792    }
1793}
1794
1795static void gen_SARX(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1796{
1797    MemOp ot = decode->op[0].ot;
1798    int mask;
1799
1800    mask = ot == MO_64 ? 63 : 31;
1801    tcg_gen_andi_tl(s->T1, s->T1, mask);
1802    if (ot != MO_64) {
1803        tcg_gen_ext32s_tl(s->T0, s->T0);
1804    }
1805    tcg_gen_sar_tl(s->T0, s->T0, s->T1);
1806}
1807
1808static void gen_SHLX(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1809{
1810    MemOp ot = decode->op[0].ot;
1811    int mask;
1812
1813    mask = ot == MO_64 ? 63 : 31;
1814    tcg_gen_andi_tl(s->T1, s->T1, mask);
1815    tcg_gen_shl_tl(s->T0, s->T0, s->T1);
1816}
1817
1818static void gen_SHRX(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1819{
1820    MemOp ot = decode->op[0].ot;
1821    int mask;
1822
1823    mask = ot == MO_64 ? 63 : 31;
1824    tcg_gen_andi_tl(s->T1, s->T1, mask);
1825    if (ot != MO_64) {
1826        tcg_gen_ext32u_tl(s->T0, s->T0);
1827    }
1828    tcg_gen_shr_tl(s->T0, s->T0, s->T1);
1829}
1830
1831static void gen_VAESKEYGEN(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1832{
1833    TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
1834    assert(!s->vex_l);
1835    gen_helper_aeskeygenassist_xmm(cpu_env, OP_PTR0, OP_PTR1, imm);
1836}
1837
1838static void gen_STMXCSR(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1839{
1840    if (s->vex_l) {
1841        gen_illegal_opcode(s);
1842        return;
1843    }
1844    gen_helper_update_mxcsr(cpu_env);
1845    tcg_gen_ld32u_tl(s->T0, cpu_env, offsetof(CPUX86State, mxcsr));
1846}
1847
1848static void gen_VAESIMC(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1849{
1850    assert(!s->vex_l);
1851    gen_helper_aesimc_xmm(cpu_env, OP_PTR0, OP_PTR2);
1852}
1853
1854/*
1855 * 00 = v*ps Vps, Hps, Wpd
1856 * 66 = v*pd Vpd, Hpd, Wps
1857 * f3 = v*ss Vss, Hss, Wps
1858 * f2 = v*sd Vsd, Hsd, Wps
1859 */
1860#define SSE_CMP(x) { \
1861    gen_helper_ ## x ## ps ## _xmm, gen_helper_ ## x ## pd ## _xmm, \
1862    gen_helper_ ## x ## ss, gen_helper_ ## x ## sd, \
1863    gen_helper_ ## x ## ps ## _ymm, gen_helper_ ## x ## pd ## _ymm}
1864static const SSEFunc_0_eppp gen_helper_cmp_funcs[32][6] = {
1865    SSE_CMP(cmpeq),
1866    SSE_CMP(cmplt),
1867    SSE_CMP(cmple),
1868    SSE_CMP(cmpunord),
1869    SSE_CMP(cmpneq),
1870    SSE_CMP(cmpnlt),
1871    SSE_CMP(cmpnle),
1872    SSE_CMP(cmpord),
1873
1874    SSE_CMP(cmpequ),
1875    SSE_CMP(cmpnge),
1876    SSE_CMP(cmpngt),
1877    SSE_CMP(cmpfalse),
1878    SSE_CMP(cmpnequ),
1879    SSE_CMP(cmpge),
1880    SSE_CMP(cmpgt),
1881    SSE_CMP(cmptrue),
1882
1883    SSE_CMP(cmpeqs),
1884    SSE_CMP(cmpltq),
1885    SSE_CMP(cmpleq),
1886    SSE_CMP(cmpunords),
1887    SSE_CMP(cmpneqq),
1888    SSE_CMP(cmpnltq),
1889    SSE_CMP(cmpnleq),
1890    SSE_CMP(cmpords),
1891
1892    SSE_CMP(cmpequs),
1893    SSE_CMP(cmpngeq),
1894    SSE_CMP(cmpngtq),
1895    SSE_CMP(cmpfalses),
1896    SSE_CMP(cmpnequs),
1897    SSE_CMP(cmpgeq),
1898    SSE_CMP(cmpgtq),
1899    SSE_CMP(cmptrues),
1900};
1901#undef SSE_CMP
1902
1903static void gen_VCMP(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1904{
1905    int index = decode->immediate & (s->prefix & PREFIX_VEX ? 31 : 7);
1906    int b =
1907        s->prefix & PREFIX_REPZ  ? 2 /* ss */ :
1908        s->prefix & PREFIX_REPNZ ? 3 /* sd */ :
1909        !!(s->prefix & PREFIX_DATA) /* pd */ + (s->vex_l << 2);
1910
1911    gen_helper_cmp_funcs[index][b](cpu_env, OP_PTR0, OP_PTR1, OP_PTR2);
1912}
1913
1914static void gen_VCOMI(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1915{
1916    SSEFunc_0_epp fn;
1917    fn = s->prefix & PREFIX_DATA ? gen_helper_comisd : gen_helper_comiss;
1918    fn(cpu_env, OP_PTR1, OP_PTR2);
1919    set_cc_op(s, CC_OP_EFLAGS);
1920}
1921
1922static void gen_VCVTfp2fp(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1923{
1924    gen_unary_fp_sse(s, env, decode,
1925                     gen_helper_cvtpd2ps_xmm, gen_helper_cvtps2pd_xmm,
1926                     gen_helper_cvtpd2ps_ymm, gen_helper_cvtps2pd_ymm,
1927                     gen_helper_cvtsd2ss, gen_helper_cvtss2sd);
1928}
1929
1930static void gen_VCVTPS2PH(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1931{
1932    gen_unary_imm_fp_sse(s, env, decode,
1933                      gen_helper_cvtps2ph_xmm,
1934                      gen_helper_cvtps2ph_ymm);
1935    /*
1936     * VCVTPS2PH is the only instruction that performs an operation on a
1937     * register source and then *stores* into memory.
1938     */
1939    if (decode->op[0].has_ea) {
1940        gen_store_sse(s, decode, decode->op[0].offset);
1941    }
1942}
1943
1944static void gen_VCVTSI2Sx(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
1945{
1946    int vec_len = vector_len(s, decode);
1947    TCGv_i32 in;
1948
1949    tcg_gen_gvec_mov(MO_64, decode->op[0].offset, decode->op[1].offset, vec_len, vec_len);
1950
1951#ifdef TARGET_X86_64
1952    MemOp ot = decode->op[2].ot;
1953    if (ot == MO_64) {
1954        if (s->prefix & PREFIX_REPNZ) {
1955            gen_helper_cvtsq2sd(cpu_env, OP_PTR0, s->T1);
1956        } else {
1957            gen_helper_cvtsq2ss(cpu_env, OP_PTR0, s->T1);
1958        }
1959        return;
1960    }
1961    in = s->tmp2_i32;
1962    tcg_gen_trunc_tl_i32(in, s->T1);
1963#else
1964    in = s->T1;
1965#endif
1966
1967    if (s->prefix & PREFIX_REPNZ) {
1968        gen_helper_cvtsi2sd(cpu_env, OP_PTR0, in);
1969    } else {
1970        gen_helper_cvtsi2ss(cpu_env, OP_PTR0, in);
1971    }
1972}
1973
1974static inline void gen_VCVTtSx2SI(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode,
1975                                  SSEFunc_i_ep ss2si, SSEFunc_l_ep ss2sq,
1976                                  SSEFunc_i_ep sd2si, SSEFunc_l_ep sd2sq)
1977{
1978    TCGv_i32 out;
1979
1980#ifdef TARGET_X86_64
1981    MemOp ot = decode->op[0].ot;
1982    if (ot == MO_64) {
1983        if (s->prefix & PREFIX_REPNZ) {
1984            sd2sq(s->T0, cpu_env, OP_PTR2);
1985        } else {
1986            ss2sq(s->T0, cpu_env, OP_PTR2);
1987        }
1988        return;
1989    }
1990
1991    out = s->tmp2_i32;
1992#else
1993    out = s->T0;
1994#endif
1995    if (s->prefix & PREFIX_REPNZ) {
1996        sd2si(out, cpu_env, OP_PTR2);
1997    } else {
1998        ss2si(out, cpu_env, OP_PTR2);
1999    }
2000#ifdef TARGET_X86_64
2001    tcg_gen_extu_i32_tl(s->T0, out);
2002#endif
2003}
2004
2005#ifndef TARGET_X86_64
2006#define gen_helper_cvtss2sq NULL
2007#define gen_helper_cvtsd2sq NULL
2008#define gen_helper_cvttss2sq NULL
2009#define gen_helper_cvttsd2sq NULL
2010#endif
2011
2012static void gen_VCVTSx2SI(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2013{
2014    gen_VCVTtSx2SI(s, env, decode,
2015                   gen_helper_cvtss2si, gen_helper_cvtss2sq,
2016                   gen_helper_cvtsd2si, gen_helper_cvtsd2sq);
2017}
2018
2019static void gen_VCVTTSx2SI(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2020{
2021    gen_VCVTtSx2SI(s, env, decode,
2022                   gen_helper_cvttss2si, gen_helper_cvttss2sq,
2023                   gen_helper_cvttsd2si, gen_helper_cvttsd2sq);
2024}
2025
2026static void gen_VEXTRACTx128(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2027{
2028    int mask = decode->immediate & 1;
2029    int src_ofs = vector_elem_offset(&decode->op[1], MO_128, mask);
2030    if (decode->op[0].has_ea) {
2031        /* VEX-only instruction, no alignment requirements.  */
2032        gen_sto_env_A0(s, src_ofs, false);
2033    } else {
2034        tcg_gen_gvec_mov(MO_64, decode->op[0].offset, src_ofs, 16, 16);
2035    }
2036}
2037
2038static void gen_VEXTRACTPS(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2039{
2040    gen_pextr(s, env, decode, MO_32);
2041}
2042
2043static void gen_vinsertps(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2044{
2045    int val = decode->immediate;
2046    int dest_word = (val >> 4) & 3;
2047    int new_mask = (val & 15) | (1 << dest_word);
2048    int vec_len = 16;
2049
2050    assert(!s->vex_l);
2051
2052    if (new_mask == 15) {
2053        /* All zeroes except possibly for the inserted element */
2054        tcg_gen_gvec_dup_imm(MO_64, decode->op[0].offset, vec_len, vec_len, 0);
2055    } else if (decode->op[1].offset != decode->op[0].offset) {
2056        gen_store_sse(s, decode, decode->op[1].offset);
2057    }
2058
2059    if (new_mask != (val & 15)) {
2060        tcg_gen_st_i32(s->tmp2_i32, cpu_env,
2061                       vector_elem_offset(&decode->op[0], MO_32, dest_word));
2062    }
2063
2064    if (new_mask != 15) {
2065        TCGv_i32 zero = tcg_constant_i32(0); /* float32_zero */
2066        int i;
2067        for (i = 0; i < 4; i++) {
2068            if ((val >> i) & 1) {
2069                tcg_gen_st_i32(zero, cpu_env,
2070                               vector_elem_offset(&decode->op[0], MO_32, i));
2071            }
2072        }
2073    }
2074}
2075
2076static void gen_VINSERTPS_r(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2077{
2078    int val = decode->immediate;
2079    tcg_gen_ld_i32(s->tmp2_i32, cpu_env,
2080                   vector_elem_offset(&decode->op[2], MO_32, (val >> 6) & 3));
2081    gen_vinsertps(s, env, decode);
2082}
2083
2084static void gen_VINSERTPS_m(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2085{
2086    tcg_gen_qemu_ld_i32(s->tmp2_i32, s->A0, s->mem_index, MO_LEUL);
2087    gen_vinsertps(s, env, decode);
2088}
2089
2090static void gen_VINSERTx128(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2091{
2092    int mask = decode->immediate & 1;
2093    tcg_gen_gvec_mov(MO_64,
2094                     decode->op[0].offset + offsetof(YMMReg, YMM_X(mask)),
2095                     decode->op[2].offset + offsetof(YMMReg, YMM_X(0)), 16, 16);
2096    tcg_gen_gvec_mov(MO_64,
2097                     decode->op[0].offset + offsetof(YMMReg, YMM_X(!mask)),
2098                     decode->op[1].offset + offsetof(YMMReg, YMM_X(!mask)), 16, 16);
2099}
2100
2101static inline void gen_maskmov(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode,
2102                               SSEFunc_0_eppt xmm, SSEFunc_0_eppt ymm)
2103{
2104    if (!s->vex_l) {
2105        xmm(cpu_env, OP_PTR2, OP_PTR1, s->A0);
2106    } else {
2107        ymm(cpu_env, OP_PTR2, OP_PTR1, s->A0);
2108    }
2109}
2110
2111static void gen_VMASKMOVPD_st(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2112{
2113    gen_maskmov(s, env, decode, gen_helper_vpmaskmovq_st_xmm, gen_helper_vpmaskmovq_st_ymm);
2114}
2115
2116static void gen_VMASKMOVPS_st(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2117{
2118    gen_maskmov(s, env, decode, gen_helper_vpmaskmovd_st_xmm, gen_helper_vpmaskmovd_st_ymm);
2119}
2120
2121static void gen_VMOVHPx_ld(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2122{
2123    gen_ldq_env_A0(s, decode->op[0].offset + offsetof(XMMReg, XMM_Q(1)));
2124    if (decode->op[0].offset != decode->op[1].offset) {
2125        tcg_gen_ld_i64(s->tmp1_i64, cpu_env, decode->op[1].offset + offsetof(XMMReg, XMM_Q(0)));
2126        tcg_gen_st_i64(s->tmp1_i64, cpu_env, decode->op[0].offset + offsetof(XMMReg, XMM_Q(0)));
2127    }
2128}
2129
2130static void gen_VMOVHPx_st(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2131{
2132    gen_stq_env_A0(s, decode->op[2].offset + offsetof(XMMReg, XMM_Q(1)));
2133}
2134
2135static void gen_VMOVHPx(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2136{
2137    if (decode->op[0].offset != decode->op[2].offset) {
2138        tcg_gen_ld_i64(s->tmp1_i64, cpu_env, decode->op[2].offset + offsetof(XMMReg, XMM_Q(1)));
2139        tcg_gen_st_i64(s->tmp1_i64, cpu_env, decode->op[0].offset + offsetof(XMMReg, XMM_Q(1)));
2140    }
2141    if (decode->op[0].offset != decode->op[1].offset) {
2142        tcg_gen_ld_i64(s->tmp1_i64, cpu_env, decode->op[1].offset + offsetof(XMMReg, XMM_Q(0)));
2143        tcg_gen_st_i64(s->tmp1_i64, cpu_env, decode->op[0].offset + offsetof(XMMReg, XMM_Q(0)));
2144    }
2145}
2146
2147static void gen_VMOVHLPS(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2148{
2149    tcg_gen_ld_i64(s->tmp1_i64, cpu_env, decode->op[2].offset + offsetof(XMMReg, XMM_Q(1)));
2150    tcg_gen_st_i64(s->tmp1_i64, cpu_env, decode->op[0].offset + offsetof(XMMReg, XMM_Q(0)));
2151    if (decode->op[0].offset != decode->op[1].offset) {
2152        tcg_gen_ld_i64(s->tmp1_i64, cpu_env, decode->op[1].offset + offsetof(XMMReg, XMM_Q(1)));
2153        tcg_gen_st_i64(s->tmp1_i64, cpu_env, decode->op[0].offset + offsetof(XMMReg, XMM_Q(1)));
2154    }
2155}
2156
2157static void gen_VMOVLHPS(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2158{
2159    tcg_gen_ld_i64(s->tmp1_i64, cpu_env, decode->op[2].offset);
2160    tcg_gen_st_i64(s->tmp1_i64, cpu_env, decode->op[0].offset + offsetof(XMMReg, XMM_Q(1)));
2161    if (decode->op[0].offset != decode->op[1].offset) {
2162        tcg_gen_ld_i64(s->tmp1_i64, cpu_env, decode->op[1].offset + offsetof(XMMReg, XMM_Q(0)));
2163        tcg_gen_st_i64(s->tmp1_i64, cpu_env, decode->op[0].offset + offsetof(XMMReg, XMM_Q(0)));
2164    }
2165}
2166
2167/*
2168 * Note that MOVLPx supports 256-bit operation unlike MOVHLPx, MOVLHPx, MOXHPx.
2169 * Use a gvec move to move everything above the bottom 64 bits.
2170 */
2171
2172static void gen_VMOVLPx(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2173{
2174    int vec_len = vector_len(s, decode);
2175
2176    tcg_gen_ld_i64(s->tmp1_i64, cpu_env, decode->op[2].offset + offsetof(XMMReg, XMM_Q(0)));
2177    tcg_gen_gvec_mov(MO_64, decode->op[0].offset, decode->op[1].offset, vec_len, vec_len);
2178    tcg_gen_st_i64(s->tmp1_i64, cpu_env, decode->op[0].offset + offsetof(XMMReg, XMM_Q(0)));
2179}
2180
2181static void gen_VMOVLPx_ld(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2182{
2183    int vec_len = vector_len(s, decode);
2184
2185    tcg_gen_qemu_ld_i64(s->tmp1_i64, s->A0, s->mem_index, MO_LEUQ);
2186    tcg_gen_gvec_mov(MO_64, decode->op[0].offset, decode->op[1].offset, vec_len, vec_len);
2187    tcg_gen_st_i64(s->tmp1_i64, OP_PTR0, offsetof(ZMMReg, ZMM_Q(0)));
2188}
2189
2190static void gen_VMOVLPx_st(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2191{
2192    tcg_gen_ld_i64(s->tmp1_i64, OP_PTR2, offsetof(ZMMReg, ZMM_Q(0)));
2193    tcg_gen_qemu_st_i64(s->tmp1_i64, s->A0, s->mem_index, MO_LEUQ);
2194}
2195
2196static void gen_VMOVSD_ld(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2197{
2198    TCGv_i64 zero = tcg_constant_i64(0);
2199
2200    tcg_gen_qemu_ld_i64(s->tmp1_i64, s->A0, s->mem_index, MO_LEUQ);
2201    tcg_gen_st_i64(zero, OP_PTR0, offsetof(ZMMReg, ZMM_Q(1)));
2202    tcg_gen_st_i64(s->tmp1_i64, OP_PTR0, offsetof(ZMMReg, ZMM_Q(0)));
2203}
2204
2205static void gen_VMOVSS(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2206{
2207    int vec_len = vector_len(s, decode);
2208
2209    tcg_gen_ld_i32(s->tmp2_i32, OP_PTR2, offsetof(ZMMReg, ZMM_L(0)));
2210    tcg_gen_gvec_mov(MO_64, decode->op[0].offset, decode->op[1].offset, vec_len, vec_len);
2211    tcg_gen_st_i32(s->tmp2_i32, OP_PTR0, offsetof(ZMMReg, ZMM_L(0)));
2212}
2213
2214static void gen_VMOVSS_ld(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2215{
2216    int vec_len = vector_len(s, decode);
2217
2218    tcg_gen_qemu_ld_i32(s->tmp2_i32, s->A0, s->mem_index, MO_LEUL);
2219    tcg_gen_gvec_dup_imm(MO_64, decode->op[0].offset, vec_len, vec_len, 0);
2220    tcg_gen_st_i32(s->tmp2_i32, OP_PTR0, offsetof(ZMMReg, ZMM_L(0)));
2221}
2222
2223static void gen_VMOVSS_st(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2224{
2225    tcg_gen_ld_i32(s->tmp2_i32, OP_PTR2, offsetof(ZMMReg, ZMM_L(0)));
2226    tcg_gen_qemu_st_i32(s->tmp2_i32, s->A0, s->mem_index, MO_LEUL);
2227}
2228
2229static void gen_VPMASKMOV_st(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2230{
2231    if (s->vex_w) {
2232        gen_VMASKMOVPD_st(s, env, decode);
2233    } else {
2234        gen_VMASKMOVPS_st(s, env, decode);
2235    }
2236}
2237
2238static void gen_VPERMD(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2239{
2240    assert(s->vex_l);
2241    gen_helper_vpermd_ymm(OP_PTR0, OP_PTR1, OP_PTR2);
2242}
2243
2244static void gen_VPERM2x128(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2245{
2246    TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
2247    assert(s->vex_l);
2248    gen_helper_vpermdq_ymm(OP_PTR0, OP_PTR1, OP_PTR2, imm);
2249}
2250
2251static void gen_VPHMINPOSUW(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2252{
2253    assert(!s->vex_l);
2254    gen_helper_phminposuw_xmm(cpu_env, OP_PTR0, OP_PTR2);
2255}
2256
2257static void gen_VROUNDSD(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2258{
2259    TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
2260    assert(!s->vex_l);
2261    gen_helper_roundsd_xmm(cpu_env, OP_PTR0, OP_PTR1, OP_PTR2, imm);
2262}
2263
2264static void gen_VROUNDSS(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2265{
2266    TCGv_i32 imm = tcg_constant8u_i32(decode->immediate);
2267    assert(!s->vex_l);
2268    gen_helper_roundss_xmm(cpu_env, OP_PTR0, OP_PTR1, OP_PTR2, imm);
2269}
2270
2271static void gen_VSHUF(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2272{
2273    TCGv_i32 imm = tcg_constant_i32(decode->immediate);
2274    SSEFunc_0_pppi ps, pd, fn;
2275    ps = s->vex_l ? gen_helper_shufps_ymm : gen_helper_shufps_xmm;
2276    pd = s->vex_l ? gen_helper_shufpd_ymm : gen_helper_shufpd_xmm;
2277    fn = s->prefix & PREFIX_DATA ? pd : ps;
2278    fn(OP_PTR0, OP_PTR1, OP_PTR2, imm);
2279}
2280
2281static void gen_VUCOMI(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2282{
2283    SSEFunc_0_epp fn;
2284    fn = s->prefix & PREFIX_DATA ? gen_helper_ucomisd : gen_helper_ucomiss;
2285    fn(cpu_env, OP_PTR1, OP_PTR2);
2286    set_cc_op(s, CC_OP_EFLAGS);
2287}
2288
2289static void gen_VZEROALL(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2290{
2291    TCGv_ptr ptr = tcg_temp_new_ptr();
2292
2293    tcg_gen_addi_ptr(ptr, cpu_env, offsetof(CPUX86State, xmm_t0));
2294    gen_helper_memset(ptr, ptr, tcg_constant_i32(0),
2295                      tcg_constant_ptr(CPU_NB_REGS * sizeof(ZMMReg)));
2296    tcg_temp_free_ptr(ptr);
2297}
2298
2299static void gen_VZEROUPPER(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
2300{
2301    int i;
2302
2303    for (i = 0; i < CPU_NB_REGS; i++) {
2304        int offset = offsetof(CPUX86State, xmm_regs[i].ZMM_X(1));
2305        tcg_gen_gvec_dup_imm(MO_64, offset, 16, 16, 0);
2306    }
2307}
2308