2 * Tiny Code Generator for QEMU
4 * Copyright (c) 2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 /* define it to use liveness analysis (better code) */
26 #define USE_TCG_OPTIMIZATIONS
28 #include "qemu/osdep.h"
30 /* Define to jump the ELF file used to communicate with GDB. */
33 #include "qemu/error-report.h"
34 #include "qemu/cutils.h"
35 #include "qemu/host-utils.h"
36 #include "qemu/qemu-print.h"
37 #include "qemu/cacheflush.h"
38 #include "qemu/cacheinfo.h"
39 #include "qemu/timer.h"
41 /* Note: the long term plan is to reduce the dependencies on the QEMU
42 CPU definitions. Currently they are used for qemu_ld/st
44 #define NO_CPU_IO_DEFS
46 #include "exec/exec-all.h"
47 #include "tcg/tcg-op.h"
49 #if UINTPTR_MAX == UINT32_MAX
50 # define ELF_CLASS ELFCLASS32
52 # define ELF_CLASS ELFCLASS64
55 # define ELF_DATA ELFDATA2MSB
57 # define ELF_DATA ELFDATA2LSB
62 #include "tcg/tcg-ldst.h"
63 #include "tcg/tcg-temp-internal.h"
64 #include "tcg-internal.h"
65 #include "accel/tcg/perf.h"
67 /* Forward declarations for functions declared in tcg-target.c.inc and
69 static void tcg_target_init(TCGContext
*s
);
70 static void tcg_target_qemu_prologue(TCGContext
*s
);
71 static bool patch_reloc(tcg_insn_unit
*code_ptr
, int type
,
72 intptr_t value
, intptr_t addend
);
74 /* The CIE and FDE header definitions will be common to all hosts. */
76 uint32_t len
__attribute__((aligned((sizeof(void *)))));
82 uint8_t return_column
;
85 typedef struct QEMU_PACKED
{
86 uint32_t len
__attribute__((aligned((sizeof(void *)))));
90 } DebugFrameFDEHeader
;
92 typedef struct QEMU_PACKED
{
94 DebugFrameFDEHeader fde
;
97 static void tcg_register_jit_int(const void *buf
, size_t size
,
98 const void *debug_frame
,
99 size_t debug_frame_size
)
100 __attribute__((unused
));
102 /* Forward declarations for functions declared and used in tcg-target.c.inc. */
103 static void tcg_out_ld(TCGContext
*s
, TCGType type
, TCGReg ret
, TCGReg arg1
,
105 static bool tcg_out_mov(TCGContext
*s
, TCGType type
, TCGReg ret
, TCGReg arg
);
106 static void tcg_out_movi(TCGContext
*s
, TCGType type
,
107 TCGReg ret
, tcg_target_long arg
);
108 static void tcg_out_ext8s(TCGContext
*s
, TCGType type
, TCGReg ret
, TCGReg arg
);
109 static void tcg_out_ext16s(TCGContext
*s
, TCGType type
, TCGReg ret
, TCGReg arg
);
110 static void tcg_out_ext8u(TCGContext
*s
, TCGReg ret
, TCGReg arg
);
111 static void tcg_out_ext16u(TCGContext
*s
, TCGReg ret
, TCGReg arg
);
112 static void tcg_out_ext32s(TCGContext
*s
, TCGReg ret
, TCGReg arg
);
113 static void tcg_out_ext32u(TCGContext
*s
, TCGReg ret
, TCGReg arg
);
114 static void tcg_out_exts_i32_i64(TCGContext
*s
, TCGReg ret
, TCGReg arg
);
115 static void tcg_out_extu_i32_i64(TCGContext
*s
, TCGReg ret
, TCGReg arg
);
116 static void tcg_out_addi_ptr(TCGContext
*s
, TCGReg
, TCGReg
, tcg_target_long
);
117 static void tcg_out_exit_tb(TCGContext
*s
, uintptr_t arg
);
118 static void tcg_out_goto_tb(TCGContext
*s
, int which
);
119 static void tcg_out_op(TCGContext
*s
, TCGOpcode opc
,
120 const TCGArg args
[TCG_MAX_OP_ARGS
],
121 const int const_args
[TCG_MAX_OP_ARGS
]);
122 #if TCG_TARGET_MAYBE_vec
123 static bool tcg_out_dup_vec(TCGContext
*s
, TCGType type
, unsigned vece
,
124 TCGReg dst
, TCGReg src
);
125 static bool tcg_out_dupm_vec(TCGContext
*s
, TCGType type
, unsigned vece
,
126 TCGReg dst
, TCGReg base
, intptr_t offset
);
127 static void tcg_out_dupi_vec(TCGContext
*s
, TCGType type
, unsigned vece
,
128 TCGReg dst
, int64_t arg
);
129 static void tcg_out_vec_op(TCGContext
*s
, TCGOpcode opc
,
130 unsigned vecl
, unsigned vece
,
131 const TCGArg args
[TCG_MAX_OP_ARGS
],
132 const int const_args
[TCG_MAX_OP_ARGS
]);
134 static inline bool tcg_out_dup_vec(TCGContext
*s
, TCGType type
, unsigned vece
,
135 TCGReg dst
, TCGReg src
)
137 g_assert_not_reached();
139 static inline bool tcg_out_dupm_vec(TCGContext
*s
, TCGType type
, unsigned vece
,
140 TCGReg dst
, TCGReg base
, intptr_t offset
)
142 g_assert_not_reached();
144 static inline void tcg_out_dupi_vec(TCGContext
*s
, TCGType type
, unsigned vece
,
145 TCGReg dst
, int64_t arg
)
147 g_assert_not_reached();
149 static inline void tcg_out_vec_op(TCGContext
*s
, TCGOpcode opc
,
150 unsigned vecl
, unsigned vece
,
151 const TCGArg args
[TCG_MAX_OP_ARGS
],
152 const int const_args
[TCG_MAX_OP_ARGS
])
154 g_assert_not_reached();
157 static void tcg_out_st(TCGContext
*s
, TCGType type
, TCGReg arg
, TCGReg arg1
,
159 static bool tcg_out_sti(TCGContext
*s
, TCGType type
, TCGArg val
,
160 TCGReg base
, intptr_t ofs
);
161 static void tcg_out_call(TCGContext
*s
, const tcg_insn_unit
*target
,
162 const TCGHelperInfo
*info
);
163 static TCGReg
tcg_target_call_oarg_reg(TCGCallReturnKind kind
, int slot
);
164 static bool tcg_target_const_match(int64_t val
, TCGType type
, int ct
);
165 #ifdef TCG_TARGET_NEED_LDST_LABELS
166 static int tcg_out_ldst_finalize(TCGContext
*s
);
169 TCGContext tcg_init_ctx
;
170 __thread TCGContext
*tcg_ctx
;
172 TCGContext
**tcg_ctxs
;
173 unsigned int tcg_cur_ctxs
;
174 unsigned int tcg_max_ctxs
;
175 TCGv_env cpu_env
= 0;
176 const void *tcg_code_gen_epilogue
;
177 uintptr_t tcg_splitwx_diff
;
179 #ifndef CONFIG_TCG_INTERPRETER
180 tcg_prologue_fn
*tcg_qemu_tb_exec
;
183 static TCGRegSet tcg_target_available_regs
[TCG_TYPE_COUNT
];
184 static TCGRegSet tcg_target_call_clobber_regs
;
186 #if TCG_TARGET_INSN_UNIT_SIZE == 1
187 static __attribute__((unused
)) inline void tcg_out8(TCGContext
*s
, uint8_t v
)
192 static __attribute__((unused
)) inline void tcg_patch8(tcg_insn_unit
*p
,
199 #if TCG_TARGET_INSN_UNIT_SIZE <= 2
200 static __attribute__((unused
)) inline void tcg_out16(TCGContext
*s
, uint16_t v
)
202 if (TCG_TARGET_INSN_UNIT_SIZE
== 2) {
205 tcg_insn_unit
*p
= s
->code_ptr
;
206 memcpy(p
, &v
, sizeof(v
));
207 s
->code_ptr
= p
+ (2 / TCG_TARGET_INSN_UNIT_SIZE
);
211 static __attribute__((unused
)) inline void tcg_patch16(tcg_insn_unit
*p
,
214 if (TCG_TARGET_INSN_UNIT_SIZE
== 2) {
217 memcpy(p
, &v
, sizeof(v
));
222 #if TCG_TARGET_INSN_UNIT_SIZE <= 4
223 static __attribute__((unused
)) inline void tcg_out32(TCGContext
*s
, uint32_t v
)
225 if (TCG_TARGET_INSN_UNIT_SIZE
== 4) {
228 tcg_insn_unit
*p
= s
->code_ptr
;
229 memcpy(p
, &v
, sizeof(v
));
230 s
->code_ptr
= p
+ (4 / TCG_TARGET_INSN_UNIT_SIZE
);
234 static __attribute__((unused
)) inline void tcg_patch32(tcg_insn_unit
*p
,
237 if (TCG_TARGET_INSN_UNIT_SIZE
== 4) {
240 memcpy(p
, &v
, sizeof(v
));
245 #if TCG_TARGET_INSN_UNIT_SIZE <= 8
246 static __attribute__((unused
)) inline void tcg_out64(TCGContext
*s
, uint64_t v
)
248 if (TCG_TARGET_INSN_UNIT_SIZE
== 8) {
251 tcg_insn_unit
*p
= s
->code_ptr
;
252 memcpy(p
, &v
, sizeof(v
));
253 s
->code_ptr
= p
+ (8 / TCG_TARGET_INSN_UNIT_SIZE
);
257 static __attribute__((unused
)) inline void tcg_patch64(tcg_insn_unit
*p
,
260 if (TCG_TARGET_INSN_UNIT_SIZE
== 8) {
263 memcpy(p
, &v
, sizeof(v
));
268 /* label relocation processing */
270 static void tcg_out_reloc(TCGContext
*s
, tcg_insn_unit
*code_ptr
, int type
,
271 TCGLabel
*l
, intptr_t addend
)
273 TCGRelocation
*r
= tcg_malloc(sizeof(TCGRelocation
));
278 QSIMPLEQ_INSERT_TAIL(&l
->relocs
, r
, next
);
281 static void tcg_out_label(TCGContext
*s
, TCGLabel
*l
)
283 tcg_debug_assert(!l
->has_value
);
285 l
->u
.value_ptr
= tcg_splitwx_to_rx(s
->code_ptr
);
288 TCGLabel
*gen_new_label(void)
290 TCGContext
*s
= tcg_ctx
;
291 TCGLabel
*l
= tcg_malloc(sizeof(TCGLabel
));
293 memset(l
, 0, sizeof(TCGLabel
));
294 l
->id
= s
->nb_labels
++;
295 QSIMPLEQ_INIT(&l
->branches
);
296 QSIMPLEQ_INIT(&l
->relocs
);
298 QSIMPLEQ_INSERT_TAIL(&s
->labels
, l
, next
);
303 static bool tcg_resolve_relocs(TCGContext
*s
)
307 QSIMPLEQ_FOREACH(l
, &s
->labels
, next
) {
309 uintptr_t value
= l
->u
.value
;
311 QSIMPLEQ_FOREACH(r
, &l
->relocs
, next
) {
312 if (!patch_reloc(r
->ptr
, r
->type
, value
, r
->addend
)) {
320 static void set_jmp_reset_offset(TCGContext
*s
, int which
)
323 * We will check for overflow at the end of the opcode loop in
324 * tcg_gen_code, where we bound tcg_current_code_size to UINT16_MAX.
326 s
->gen_tb
->jmp_reset_offset
[which
] = tcg_current_code_size(s
);
329 static void G_GNUC_UNUSED
set_jmp_insn_offset(TCGContext
*s
, int which
)
332 * We will check for overflow at the end of the opcode loop in
333 * tcg_gen_code, where we bound tcg_current_code_size to UINT16_MAX.
335 s
->gen_tb
->jmp_insn_offset
[which
] = tcg_current_code_size(s
);
338 static uintptr_t G_GNUC_UNUSED
get_jmp_target_addr(TCGContext
*s
, int which
)
341 * Return the read-execute version of the pointer, for the benefit
342 * of any pc-relative addressing mode.
344 return (uintptr_t)tcg_splitwx_to_rx(&s
->gen_tb
->jmp_target_addr
[which
]);
347 /* Signal overflow, starting over with fewer guest insns. */
349 void tcg_raise_tb_overflow(TCGContext
*s
)
351 siglongjmp(s
->jmp_trans
, -2);
354 #define C_PFX1(P, A) P##A
355 #define C_PFX2(P, A, B) P##A##_##B
356 #define C_PFX3(P, A, B, C) P##A##_##B##_##C
357 #define C_PFX4(P, A, B, C, D) P##A##_##B##_##C##_##D
358 #define C_PFX5(P, A, B, C, D, E) P##A##_##B##_##C##_##D##_##E
359 #define C_PFX6(P, A, B, C, D, E, F) P##A##_##B##_##C##_##D##_##E##_##F
361 /* Define an enumeration for the various combinations. */
363 #define C_O0_I1(I1) C_PFX1(c_o0_i1_, I1),
364 #define C_O0_I2(I1, I2) C_PFX2(c_o0_i2_, I1, I2),
365 #define C_O0_I3(I1, I2, I3) C_PFX3(c_o0_i3_, I1, I2, I3),
366 #define C_O0_I4(I1, I2, I3, I4) C_PFX4(c_o0_i4_, I1, I2, I3, I4),
368 #define C_O1_I1(O1, I1) C_PFX2(c_o1_i1_, O1, I1),
369 #define C_O1_I2(O1, I1, I2) C_PFX3(c_o1_i2_, O1, I1, I2),
370 #define C_O1_I3(O1, I1, I2, I3) C_PFX4(c_o1_i3_, O1, I1, I2, I3),
371 #define C_O1_I4(O1, I1, I2, I3, I4) C_PFX5(c_o1_i4_, O1, I1, I2, I3, I4),
373 #define C_N1_I2(O1, I1, I2) C_PFX3(c_n1_i2_, O1, I1, I2),
375 #define C_O2_I1(O1, O2, I1) C_PFX3(c_o2_i1_, O1, O2, I1),
376 #define C_O2_I2(O1, O2, I1, I2) C_PFX4(c_o2_i2_, O1, O2, I1, I2),
377 #define C_O2_I3(O1, O2, I1, I2, I3) C_PFX5(c_o2_i3_, O1, O2, I1, I2, I3),
378 #define C_O2_I4(O1, O2, I1, I2, I3, I4) C_PFX6(c_o2_i4_, O1, O2, I1, I2, I3, I4),
381 #include "tcg-target-con-set.h"
382 } TCGConstraintSetIndex
;
384 static TCGConstraintSetIndex
tcg_target_op_def(TCGOpcode
);
400 /* Put all of the constraint sets into an array, indexed by the enum. */
402 #define C_O0_I1(I1) { .args_ct_str = { #I1 } },
403 #define C_O0_I2(I1, I2) { .args_ct_str = { #I1, #I2 } },
404 #define C_O0_I3(I1, I2, I3) { .args_ct_str = { #I1, #I2, #I3 } },
405 #define C_O0_I4(I1, I2, I3, I4) { .args_ct_str = { #I1, #I2, #I3, #I4 } },
407 #define C_O1_I1(O1, I1) { .args_ct_str = { #O1, #I1 } },
408 #define C_O1_I2(O1, I1, I2) { .args_ct_str = { #O1, #I1, #I2 } },
409 #define C_O1_I3(O1, I1, I2, I3) { .args_ct_str = { #O1, #I1, #I2, #I3 } },
410 #define C_O1_I4(O1, I1, I2, I3, I4) { .args_ct_str = { #O1, #I1, #I2, #I3, #I4 } },
412 #define C_N1_I2(O1, I1, I2) { .args_ct_str = { "&" #O1, #I1, #I2 } },
414 #define C_O2_I1(O1, O2, I1) { .args_ct_str = { #O1, #O2, #I1 } },
415 #define C_O2_I2(O1, O2, I1, I2) { .args_ct_str = { #O1, #O2, #I1, #I2 } },
416 #define C_O2_I3(O1, O2, I1, I2, I3) { .args_ct_str = { #O1, #O2, #I1, #I2, #I3 } },
417 #define C_O2_I4(O1, O2, I1, I2, I3, I4) { .args_ct_str = { #O1, #O2, #I1, #I2, #I3, #I4 } },
419 static const TCGTargetOpDef constraint_sets
[] = {
420 #include "tcg-target-con-set.h"
438 /* Expand the enumerator to be returned from tcg_target_op_def(). */
440 #define C_O0_I1(I1) C_PFX1(c_o0_i1_, I1)
441 #define C_O0_I2(I1, I2) C_PFX2(c_o0_i2_, I1, I2)
442 #define C_O0_I3(I1, I2, I3) C_PFX3(c_o0_i3_, I1, I2, I3)
443 #define C_O0_I4(I1, I2, I3, I4) C_PFX4(c_o0_i4_, I1, I2, I3, I4)
445 #define C_O1_I1(O1, I1) C_PFX2(c_o1_i1_, O1, I1)
446 #define C_O1_I2(O1, I1, I2) C_PFX3(c_o1_i2_, O1, I1, I2)
447 #define C_O1_I3(O1, I1, I2, I3) C_PFX4(c_o1_i3_, O1, I1, I2, I3)
448 #define C_O1_I4(O1, I1, I2, I3, I4) C_PFX5(c_o1_i4_, O1, I1, I2, I3, I4)
450 #define C_N1_I2(O1, I1, I2) C_PFX3(c_n1_i2_, O1, I1, I2)
452 #define C_O2_I1(O1, O2, I1) C_PFX3(c_o2_i1_, O1, O2, I1)
453 #define C_O2_I2(O1, O2, I1, I2) C_PFX4(c_o2_i2_, O1, O2, I1, I2)
454 #define C_O2_I3(O1, O2, I1, I2, I3) C_PFX5(c_o2_i3_, O1, O2, I1, I2, I3)
455 #define C_O2_I4(O1, O2, I1, I2, I3, I4) C_PFX6(c_o2_i4_, O1, O2, I1, I2, I3, I4)
457 #include "tcg-target.c.inc"
459 static void alloc_tcg_plugin_context(TCGContext
*s
)
462 s
->plugin_tb
= g_new0(struct qemu_plugin_tb
, 1);
463 s
->plugin_tb
->insns
=
464 g_ptr_array_new_with_free_func(qemu_plugin_insn_cleanup_fn
);
469 * All TCG threads except the parent (i.e. the one that called tcg_context_init
470 * and registered the target's TCG globals) must register with this function
471 * before initiating translation.
473 * In user-mode we just point tcg_ctx to tcg_init_ctx. See the documentation
474 * of tcg_region_init() for the reasoning behind this.
476 * In softmmu each caller registers its context in tcg_ctxs[]. Note that in
477 * softmmu tcg_ctxs[] does not track tcg_ctx_init, since the initial context
478 * is not used anymore for translation once this function is called.
480 * Not tracking tcg_init_ctx in tcg_ctxs[] in softmmu keeps code that iterates
481 * over the array (e.g. tcg_code_size() the same for both softmmu and user-mode.
483 #ifdef CONFIG_USER_ONLY
484 void tcg_register_thread(void)
486 tcg_ctx
= &tcg_init_ctx
;
489 void tcg_register_thread(void)
491 TCGContext
*s
= g_malloc(sizeof(*s
));
496 /* Relink mem_base. */
497 for (i
= 0, n
= tcg_init_ctx
.nb_globals
; i
< n
; ++i
) {
498 if (tcg_init_ctx
.temps
[i
].mem_base
) {
499 ptrdiff_t b
= tcg_init_ctx
.temps
[i
].mem_base
- tcg_init_ctx
.temps
;
500 tcg_debug_assert(b
>= 0 && b
< n
);
501 s
->temps
[i
].mem_base
= &s
->temps
[b
];
505 /* Claim an entry in tcg_ctxs */
506 n
= qatomic_fetch_inc(&tcg_cur_ctxs
);
507 g_assert(n
< tcg_max_ctxs
);
508 qatomic_set(&tcg_ctxs
[n
], s
);
511 alloc_tcg_plugin_context(s
);
512 tcg_region_initial_alloc(s
);
517 #endif /* !CONFIG_USER_ONLY */
519 /* pool based memory allocation */
520 void *tcg_malloc_internal(TCGContext
*s
, int size
)
525 if (size
> TCG_POOL_CHUNK_SIZE
) {
526 /* big malloc: insert a new pool (XXX: could optimize) */
527 p
= g_malloc(sizeof(TCGPool
) + size
);
529 p
->next
= s
->pool_first_large
;
530 s
->pool_first_large
= p
;
541 pool_size
= TCG_POOL_CHUNK_SIZE
;
542 p
= g_malloc(sizeof(TCGPool
) + pool_size
);
545 if (s
->pool_current
) {
546 s
->pool_current
->next
= p
;
556 s
->pool_cur
= p
->data
+ size
;
557 s
->pool_end
= p
->data
+ p
->size
;
561 void tcg_pool_reset(TCGContext
*s
)
564 for (p
= s
->pool_first_large
; p
; p
= t
) {
568 s
->pool_first_large
= NULL
;
569 s
->pool_cur
= s
->pool_end
= NULL
;
570 s
->pool_current
= NULL
;
573 #include "exec/helper-proto.h"
575 static TCGHelperInfo all_helpers
[] = {
576 #include "exec/helper-tcg.h"
578 static GHashTable
*helper_table
;
580 #ifdef CONFIG_TCG_INTERPRETER
581 static ffi_type
*typecode_to_ffi(int argmask
)
584 * libffi does not support __int128_t, so we have forced Int128
585 * to use the structure definition instead of the builtin type.
587 static ffi_type
*ffi_type_i128_elements
[3] = {
592 static ffi_type ffi_type_i128
= {
594 .alignment
= __alignof__(Int128
),
595 .type
= FFI_TYPE_STRUCT
,
596 .elements
= ffi_type_i128_elements
,
600 case dh_typecode_void
:
601 return &ffi_type_void
;
602 case dh_typecode_i32
:
603 return &ffi_type_uint32
;
604 case dh_typecode_s32
:
605 return &ffi_type_sint32
;
606 case dh_typecode_i64
:
607 return &ffi_type_uint64
;
608 case dh_typecode_s64
:
609 return &ffi_type_sint64
;
610 case dh_typecode_ptr
:
611 return &ffi_type_pointer
;
612 case dh_typecode_i128
:
613 return &ffi_type_i128
;
615 g_assert_not_reached();
618 static void init_ffi_layouts(void)
620 /* g_direct_hash/equal for direct comparisons on uint32_t. */
621 GHashTable
*ffi_table
= g_hash_table_new(NULL
, NULL
);
623 for (int i
= 0; i
< ARRAY_SIZE(all_helpers
); ++i
) {
624 TCGHelperInfo
*info
= &all_helpers
[i
];
625 unsigned typemask
= info
->typemask
;
626 gpointer hash
= (gpointer
)(uintptr_t)typemask
;
635 cif
= g_hash_table_lookup(ffi_table
, hash
);
641 /* Ignoring the return type, find the last non-zero field. */
642 nargs
= 32 - clz32(typemask
>> 3);
643 nargs
= DIV_ROUND_UP(nargs
, 3);
644 assert(nargs
<= MAX_CALL_IARGS
);
646 ca
= g_malloc0(sizeof(*ca
) + nargs
* sizeof(ffi_type
*));
647 ca
->cif
.rtype
= typecode_to_ffi(typemask
& 7);
648 ca
->cif
.nargs
= nargs
;
651 ca
->cif
.arg_types
= ca
->args
;
652 for (int j
= 0; j
< nargs
; ++j
) {
653 int typecode
= extract32(typemask
, (j
+ 1) * 3, 3);
654 ca
->args
[j
] = typecode_to_ffi(typecode
);
658 status
= ffi_prep_cif(&ca
->cif
, FFI_DEFAULT_ABI
, nargs
,
659 ca
->cif
.rtype
, ca
->cif
.arg_types
);
660 assert(status
== FFI_OK
);
664 g_hash_table_insert(ffi_table
, hash
, (gpointer
)cif
);
667 g_hash_table_destroy(ffi_table
);
669 #endif /* CONFIG_TCG_INTERPRETER */
671 typedef struct TCGCumulativeArgs
{
672 int arg_idx
; /* tcg_gen_callN args[] */
673 int info_in_idx
; /* TCGHelperInfo in[] */
674 int arg_slot
; /* regs+stack slot */
675 int ref_slot
; /* stack slots for references */
678 static void layout_arg_even(TCGCumulativeArgs
*cum
)
680 cum
->arg_slot
+= cum
->arg_slot
& 1;
683 static void layout_arg_1(TCGCumulativeArgs
*cum
, TCGHelperInfo
*info
,
684 TCGCallArgumentKind kind
)
686 TCGCallArgumentLoc
*loc
= &info
->in
[cum
->info_in_idx
];
688 *loc
= (TCGCallArgumentLoc
){
690 .arg_idx
= cum
->arg_idx
,
691 .arg_slot
= cum
->arg_slot
,
697 static void layout_arg_normal_n(TCGCumulativeArgs
*cum
,
698 TCGHelperInfo
*info
, int n
)
700 TCGCallArgumentLoc
*loc
= &info
->in
[cum
->info_in_idx
];
702 for (int i
= 0; i
< n
; ++i
) {
703 /* Layout all using the same arg_idx, adjusting the subindex. */
704 loc
[i
] = (TCGCallArgumentLoc
){
705 .kind
= TCG_CALL_ARG_NORMAL
,
706 .arg_idx
= cum
->arg_idx
,
708 .arg_slot
= cum
->arg_slot
+ i
,
711 cum
->info_in_idx
+= n
;
715 static void layout_arg_by_ref(TCGCumulativeArgs
*cum
, TCGHelperInfo
*info
)
717 TCGCallArgumentLoc
*loc
= &info
->in
[cum
->info_in_idx
];
718 int n
= 128 / TCG_TARGET_REG_BITS
;
720 /* The first subindex carries the pointer. */
721 layout_arg_1(cum
, info
, TCG_CALL_ARG_BY_REF
);
724 * The callee is allowed to clobber memory associated with
725 * structure pass by-reference. Therefore we must make copies.
726 * Allocate space from "ref_slot", which will be adjusted to
727 * follow the parameters on the stack.
729 loc
[0].ref_slot
= cum
->ref_slot
;
732 * Subsequent words also go into the reference slot, but
733 * do not accumulate into the regular arguments.
735 for (int i
= 1; i
< n
; ++i
) {
736 loc
[i
] = (TCGCallArgumentLoc
){
737 .kind
= TCG_CALL_ARG_BY_REF_N
,
738 .arg_idx
= cum
->arg_idx
,
740 .ref_slot
= cum
->ref_slot
+ i
,
743 cum
->info_in_idx
+= n
;
747 static void init_call_layout(TCGHelperInfo
*info
)
749 int max_reg_slots
= ARRAY_SIZE(tcg_target_call_iarg_regs
);
750 int max_stk_slots
= TCG_STATIC_CALL_ARGS_SIZE
/ sizeof(tcg_target_long
);
751 unsigned typemask
= info
->typemask
;
753 TCGCumulativeArgs cum
= { };
756 * Parse and place any function return value.
758 typecode
= typemask
& 7;
760 case dh_typecode_void
:
763 case dh_typecode_i32
:
764 case dh_typecode_s32
:
765 case dh_typecode_ptr
:
767 info
->out_kind
= TCG_CALL_RET_NORMAL
;
769 case dh_typecode_i64
:
770 case dh_typecode_s64
:
771 info
->nr_out
= 64 / TCG_TARGET_REG_BITS
;
772 info
->out_kind
= TCG_CALL_RET_NORMAL
;
773 /* Query the last register now to trigger any assert early. */
774 tcg_target_call_oarg_reg(info
->out_kind
, info
->nr_out
- 1);
776 case dh_typecode_i128
:
777 info
->nr_out
= 128 / TCG_TARGET_REG_BITS
;
778 info
->out_kind
= TCG_TARGET_CALL_RET_I128
;
779 switch (TCG_TARGET_CALL_RET_I128
) {
780 case TCG_CALL_RET_NORMAL
:
781 /* Query the last register now to trigger any assert early. */
782 tcg_target_call_oarg_reg(info
->out_kind
, info
->nr_out
- 1);
784 case TCG_CALL_RET_BY_VEC
:
785 /* Query the single register now to trigger any assert early. */
786 tcg_target_call_oarg_reg(TCG_CALL_RET_BY_VEC
, 0);
788 case TCG_CALL_RET_BY_REF
:
790 * Allocate the first argument to the output.
791 * We don't need to store this anywhere, just make it
792 * unavailable for use in the input loop below.
797 qemu_build_not_reached();
801 g_assert_not_reached();
805 * Parse and place function arguments.
807 for (typemask
>>= 3; typemask
; typemask
>>= 3, cum
.arg_idx
++) {
808 TCGCallArgumentKind kind
;
811 typecode
= typemask
& 7;
813 case dh_typecode_i32
:
814 case dh_typecode_s32
:
817 case dh_typecode_i64
:
818 case dh_typecode_s64
:
821 case dh_typecode_ptr
:
824 case dh_typecode_i128
:
825 type
= TCG_TYPE_I128
;
828 g_assert_not_reached();
833 switch (TCG_TARGET_CALL_ARG_I32
) {
834 case TCG_CALL_ARG_EVEN
:
835 layout_arg_even(&cum
);
837 case TCG_CALL_ARG_NORMAL
:
838 layout_arg_1(&cum
, info
, TCG_CALL_ARG_NORMAL
);
840 case TCG_CALL_ARG_EXTEND
:
841 kind
= TCG_CALL_ARG_EXTEND_U
+ (typecode
& 1);
842 layout_arg_1(&cum
, info
, kind
);
845 qemu_build_not_reached();
850 switch (TCG_TARGET_CALL_ARG_I64
) {
851 case TCG_CALL_ARG_EVEN
:
852 layout_arg_even(&cum
);
854 case TCG_CALL_ARG_NORMAL
:
855 if (TCG_TARGET_REG_BITS
== 32) {
856 layout_arg_normal_n(&cum
, info
, 2);
858 layout_arg_1(&cum
, info
, TCG_CALL_ARG_NORMAL
);
862 qemu_build_not_reached();
867 switch (TCG_TARGET_CALL_ARG_I128
) {
868 case TCG_CALL_ARG_EVEN
:
869 layout_arg_even(&cum
);
871 case TCG_CALL_ARG_NORMAL
:
872 layout_arg_normal_n(&cum
, info
, 128 / TCG_TARGET_REG_BITS
);
874 case TCG_CALL_ARG_BY_REF
:
875 layout_arg_by_ref(&cum
, info
);
878 qemu_build_not_reached();
883 g_assert_not_reached();
886 info
->nr_in
= cum
.info_in_idx
;
888 /* Validate that we didn't overrun the input array. */
889 assert(cum
.info_in_idx
<= ARRAY_SIZE(info
->in
));
890 /* Validate the backend has enough argument space. */
891 assert(cum
.arg_slot
<= max_reg_slots
+ max_stk_slots
);
894 * Relocate the "ref_slot" area to the end of the parameters.
895 * Minimizing this stack offset helps code size for x86,
896 * which has a signed 8-bit offset encoding.
898 if (cum
.ref_slot
!= 0) {
901 if (cum
.arg_slot
> max_reg_slots
) {
902 int align
= __alignof(Int128
) / sizeof(tcg_target_long
);
904 ref_base
= cum
.arg_slot
- max_reg_slots
;
906 ref_base
= ROUND_UP(ref_base
, align
);
909 assert(ref_base
+ cum
.ref_slot
<= max_stk_slots
);
912 for (int i
= cum
.info_in_idx
- 1; i
>= 0; --i
) {
913 TCGCallArgumentLoc
*loc
= &info
->in
[i
];
915 case TCG_CALL_ARG_BY_REF
:
916 case TCG_CALL_ARG_BY_REF_N
:
917 loc
->ref_slot
+= ref_base
;
927 static int indirect_reg_alloc_order
[ARRAY_SIZE(tcg_target_reg_alloc_order
)];
928 static void process_op_defs(TCGContext
*s
);
929 static TCGTemp
*tcg_global_reg_new_internal(TCGContext
*s
, TCGType type
,
930 TCGReg reg
, const char *name
);
932 static void tcg_context_init(unsigned max_cpus
)
934 TCGContext
*s
= &tcg_init_ctx
;
935 int op
, total_args
, n
, i
;
937 TCGArgConstraint
*args_ct
;
940 memset(s
, 0, sizeof(*s
));
943 /* Count total number of arguments and allocate the corresponding
946 for(op
= 0; op
< NB_OPS
; op
++) {
947 def
= &tcg_op_defs
[op
];
948 n
= def
->nb_iargs
+ def
->nb_oargs
;
952 args_ct
= g_new0(TCGArgConstraint
, total_args
);
954 for(op
= 0; op
< NB_OPS
; op
++) {
955 def
= &tcg_op_defs
[op
];
956 def
->args_ct
= args_ct
;
957 n
= def
->nb_iargs
+ def
->nb_oargs
;
961 /* Register helpers. */
962 /* Use g_direct_hash/equal for direct pointer comparisons on func. */
963 helper_table
= g_hash_table_new(NULL
, NULL
);
965 for (i
= 0; i
< ARRAY_SIZE(all_helpers
); ++i
) {
966 init_call_layout(&all_helpers
[i
]);
967 g_hash_table_insert(helper_table
, (gpointer
)all_helpers
[i
].func
,
968 (gpointer
)&all_helpers
[i
]);
971 #ifdef CONFIG_TCG_INTERPRETER
978 /* Reverse the order of the saved registers, assuming they're all at
979 the start of tcg_target_reg_alloc_order. */
980 for (n
= 0; n
< ARRAY_SIZE(tcg_target_reg_alloc_order
); ++n
) {
981 int r
= tcg_target_reg_alloc_order
[n
];
982 if (tcg_regset_test_reg(tcg_target_call_clobber_regs
, r
)) {
986 for (i
= 0; i
< n
; ++i
) {
987 indirect_reg_alloc_order
[i
] = tcg_target_reg_alloc_order
[n
- 1 - i
];
989 for (; i
< ARRAY_SIZE(tcg_target_reg_alloc_order
); ++i
) {
990 indirect_reg_alloc_order
[i
] = tcg_target_reg_alloc_order
[i
];
993 alloc_tcg_plugin_context(s
);
997 * In user-mode we simply share the init context among threads, since we
998 * use a single region. See the documentation tcg_region_init() for the
999 * reasoning behind this.
1000 * In softmmu we will have at most max_cpus TCG threads.
1002 #ifdef CONFIG_USER_ONLY
1003 tcg_ctxs
= &tcg_ctx
;
1007 tcg_max_ctxs
= max_cpus
;
1008 tcg_ctxs
= g_new0(TCGContext
*, max_cpus
);
1011 tcg_debug_assert(!tcg_regset_test_reg(s
->reserved_regs
, TCG_AREG0
));
1012 ts
= tcg_global_reg_new_internal(s
, TCG_TYPE_PTR
, TCG_AREG0
, "env");
1013 cpu_env
= temp_tcgv_ptr(ts
);
1016 void tcg_init(size_t tb_size
, int splitwx
, unsigned max_cpus
)
1018 tcg_context_init(max_cpus
);
1019 tcg_region_init(tb_size
, splitwx
, max_cpus
);
1023 * Allocate TBs right before their corresponding translated code, making
1024 * sure that TBs and code are on different cache lines.
1026 TranslationBlock
*tcg_tb_alloc(TCGContext
*s
)
1028 uintptr_t align
= qemu_icache_linesize
;
1029 TranslationBlock
*tb
;
1033 tb
= (void *)ROUND_UP((uintptr_t)s
->code_gen_ptr
, align
);
1034 next
= (void *)ROUND_UP((uintptr_t)(tb
+ 1), align
);
1036 if (unlikely(next
> s
->code_gen_highwater
)) {
1037 if (tcg_region_alloc(s
)) {
1042 qatomic_set(&s
->code_gen_ptr
, next
);
1043 s
->data_gen_ptr
= NULL
;
1047 void tcg_prologue_init(TCGContext
*s
)
1049 size_t prologue_size
;
1051 s
->code_ptr
= s
->code_gen_ptr
;
1052 s
->code_buf
= s
->code_gen_ptr
;
1053 s
->data_gen_ptr
= NULL
;
1055 #ifndef CONFIG_TCG_INTERPRETER
1056 tcg_qemu_tb_exec
= (tcg_prologue_fn
*)tcg_splitwx_to_rx(s
->code_ptr
);
1059 #ifdef TCG_TARGET_NEED_POOL_LABELS
1060 s
->pool_labels
= NULL
;
1063 qemu_thread_jit_write();
1064 /* Generate the prologue. */
1065 tcg_target_qemu_prologue(s
);
1067 #ifdef TCG_TARGET_NEED_POOL_LABELS
1068 /* Allow the prologue to put e.g. guest_base into a pool entry. */
1070 int result
= tcg_out_pool_finalize(s
);
1071 tcg_debug_assert(result
== 0);
1075 prologue_size
= tcg_current_code_size(s
);
1076 perf_report_prologue(s
->code_gen_ptr
, prologue_size
);
1078 #ifndef CONFIG_TCG_INTERPRETER
1079 flush_idcache_range((uintptr_t)tcg_splitwx_to_rx(s
->code_buf
),
1080 (uintptr_t)s
->code_buf
, prologue_size
);
1084 if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM
)) {
1085 FILE *logfile
= qemu_log_trylock();
1087 fprintf(logfile
, "PROLOGUE: [size=%zu]\n", prologue_size
);
1088 if (s
->data_gen_ptr
) {
1089 size_t code_size
= s
->data_gen_ptr
- s
->code_gen_ptr
;
1090 size_t data_size
= prologue_size
- code_size
;
1093 disas(logfile
, s
->code_gen_ptr
, code_size
);
1095 for (i
= 0; i
< data_size
; i
+= sizeof(tcg_target_ulong
)) {
1096 if (sizeof(tcg_target_ulong
) == 8) {
1098 "0x%08" PRIxPTR
": .quad 0x%016" PRIx64
"\n",
1099 (uintptr_t)s
->data_gen_ptr
+ i
,
1100 *(uint64_t *)(s
->data_gen_ptr
+ i
));
1103 "0x%08" PRIxPTR
": .long 0x%08x\n",
1104 (uintptr_t)s
->data_gen_ptr
+ i
,
1105 *(uint32_t *)(s
->data_gen_ptr
+ i
));
1109 disas(logfile
, s
->code_gen_ptr
, prologue_size
);
1111 fprintf(logfile
, "\n");
1112 qemu_log_unlock(logfile
);
1117 #ifndef CONFIG_TCG_INTERPRETER
1119 * Assert that goto_ptr is implemented completely, setting an epilogue.
1120 * For tci, we use NULL as the signal to return from the interpreter,
1121 * so skip this check.
1123 tcg_debug_assert(tcg_code_gen_epilogue
!= NULL
);
1126 tcg_region_prologue_set(s
);
1129 void tcg_func_start(TCGContext
*s
)
1132 s
->nb_temps
= s
->nb_globals
;
1134 /* No temps have been previously allocated for size or locality. */
1135 memset(s
->free_temps
, 0, sizeof(s
->free_temps
));
1137 /* No constant temps have been previously allocated. */
1138 for (int i
= 0; i
< TCG_TYPE_COUNT
; ++i
) {
1139 if (s
->const_table
[i
]) {
1140 g_hash_table_remove_all(s
->const_table
[i
]);
1146 s
->current_frame_offset
= s
->frame_start
;
1148 #ifdef CONFIG_DEBUG_TCG
1149 s
->goto_tb_issue_mask
= 0;
1152 QTAILQ_INIT(&s
->ops
);
1153 QTAILQ_INIT(&s
->free_ops
);
1154 QSIMPLEQ_INIT(&s
->labels
);
1157 static TCGTemp
*tcg_temp_alloc(TCGContext
*s
)
1159 int n
= s
->nb_temps
++;
1161 if (n
>= TCG_MAX_TEMPS
) {
1162 tcg_raise_tb_overflow(s
);
1164 return memset(&s
->temps
[n
], 0, sizeof(TCGTemp
));
1167 static TCGTemp
*tcg_global_alloc(TCGContext
*s
)
1171 tcg_debug_assert(s
->nb_globals
== s
->nb_temps
);
1172 tcg_debug_assert(s
->nb_globals
< TCG_MAX_TEMPS
);
1174 ts
= tcg_temp_alloc(s
);
1175 ts
->kind
= TEMP_GLOBAL
;
1180 static TCGTemp
*tcg_global_reg_new_internal(TCGContext
*s
, TCGType type
,
1181 TCGReg reg
, const char *name
)
1185 tcg_debug_assert(TCG_TARGET_REG_BITS
== 64 || type
== TCG_TYPE_I32
);
1187 ts
= tcg_global_alloc(s
);
1188 ts
->base_type
= type
;
1190 ts
->kind
= TEMP_FIXED
;
1193 tcg_regset_set_reg(s
->reserved_regs
, reg
);
1198 void tcg_set_frame(TCGContext
*s
, TCGReg reg
, intptr_t start
, intptr_t size
)
1200 s
->frame_start
= start
;
1201 s
->frame_end
= start
+ size
;
1203 = tcg_global_reg_new_internal(s
, TCG_TYPE_PTR
, reg
, "_frame");
1206 TCGTemp
*tcg_global_mem_new_internal(TCGType type
, TCGv_ptr base
,
1207 intptr_t offset
, const char *name
)
1209 TCGContext
*s
= tcg_ctx
;
1210 TCGTemp
*base_ts
= tcgv_ptr_temp(base
);
1211 TCGTemp
*ts
= tcg_global_alloc(s
);
1212 int indirect_reg
= 0;
1214 switch (base_ts
->kind
) {
1218 /* We do not support double-indirect registers. */
1219 tcg_debug_assert(!base_ts
->indirect_reg
);
1220 base_ts
->indirect_base
= 1;
1221 s
->nb_indirects
+= (TCG_TARGET_REG_BITS
== 32 && type
== TCG_TYPE_I64
1226 g_assert_not_reached();
1229 if (TCG_TARGET_REG_BITS
== 32 && type
== TCG_TYPE_I64
) {
1230 TCGTemp
*ts2
= tcg_global_alloc(s
);
1233 ts
->base_type
= TCG_TYPE_I64
;
1234 ts
->type
= TCG_TYPE_I32
;
1235 ts
->indirect_reg
= indirect_reg
;
1236 ts
->mem_allocated
= 1;
1237 ts
->mem_base
= base_ts
;
1238 ts
->mem_offset
= offset
;
1239 pstrcpy(buf
, sizeof(buf
), name
);
1240 pstrcat(buf
, sizeof(buf
), "_0");
1241 ts
->name
= strdup(buf
);
1243 tcg_debug_assert(ts2
== ts
+ 1);
1244 ts2
->base_type
= TCG_TYPE_I64
;
1245 ts2
->type
= TCG_TYPE_I32
;
1246 ts2
->indirect_reg
= indirect_reg
;
1247 ts2
->mem_allocated
= 1;
1248 ts2
->mem_base
= base_ts
;
1249 ts2
->mem_offset
= offset
+ 4;
1250 ts2
->temp_subindex
= 1;
1251 pstrcpy(buf
, sizeof(buf
), name
);
1252 pstrcat(buf
, sizeof(buf
), "_1");
1253 ts2
->name
= strdup(buf
);
1255 ts
->base_type
= type
;
1257 ts
->indirect_reg
= indirect_reg
;
1258 ts
->mem_allocated
= 1;
1259 ts
->mem_base
= base_ts
;
1260 ts
->mem_offset
= offset
;
1266 TCGTemp
*tcg_temp_new_internal(TCGType type
, TCGTempKind kind
)
1268 TCGContext
*s
= tcg_ctx
;
1272 if (kind
== TEMP_EBB
) {
1273 int idx
= find_first_bit(s
->free_temps
[type
].l
, TCG_MAX_TEMPS
);
1275 if (idx
< TCG_MAX_TEMPS
) {
1276 /* There is already an available temp with the right type. */
1277 clear_bit(idx
, s
->free_temps
[type
].l
);
1279 ts
= &s
->temps
[idx
];
1280 ts
->temp_allocated
= 1;
1281 tcg_debug_assert(ts
->base_type
== type
);
1282 tcg_debug_assert(ts
->kind
== kind
);
1286 tcg_debug_assert(kind
== TEMP_TB
);
1297 n
= 64 / TCG_TARGET_REG_BITS
;
1300 n
= 128 / TCG_TARGET_REG_BITS
;
1303 g_assert_not_reached();
1306 ts
= tcg_temp_alloc(s
);
1307 ts
->base_type
= type
;
1308 ts
->temp_allocated
= 1;
1314 ts
->type
= TCG_TYPE_REG
;
1316 for (int i
= 1; i
< n
; ++i
) {
1317 TCGTemp
*ts2
= tcg_temp_alloc(s
);
1319 tcg_debug_assert(ts2
== ts
+ i
);
1320 ts2
->base_type
= type
;
1321 ts2
->type
= TCG_TYPE_REG
;
1322 ts2
->temp_allocated
= 1;
1323 ts2
->temp_subindex
= i
;
1330 TCGv_vec
tcg_temp_new_vec(TCGType type
)
1334 #ifdef CONFIG_DEBUG_TCG
1337 assert(TCG_TARGET_HAS_v64
);
1340 assert(TCG_TARGET_HAS_v128
);
1343 assert(TCG_TARGET_HAS_v256
);
1346 g_assert_not_reached();
1350 t
= tcg_temp_new_internal(type
, TEMP_EBB
);
1351 return temp_tcgv_vec(t
);
1354 /* Create a new temp of the same type as an existing temp. */
1355 TCGv_vec
tcg_temp_new_vec_matching(TCGv_vec match
)
1357 TCGTemp
*t
= tcgv_vec_temp(match
);
1359 tcg_debug_assert(t
->temp_allocated
!= 0);
1361 t
= tcg_temp_new_internal(t
->base_type
, TEMP_EBB
);
1362 return temp_tcgv_vec(t
);
1365 void tcg_temp_free_internal(TCGTemp
*ts
)
1367 TCGContext
*s
= tcg_ctx
;
1372 /* Silently ignore free. */
1375 tcg_debug_assert(ts
->temp_allocated
!= 0);
1376 ts
->temp_allocated
= 0;
1377 set_bit(temp_idx(ts
), s
->free_temps
[ts
->base_type
].l
);
1380 /* It never made sense to free TEMP_FIXED or TEMP_GLOBAL. */
1381 g_assert_not_reached();
1385 TCGTemp
*tcg_constant_internal(TCGType type
, int64_t val
)
1387 TCGContext
*s
= tcg_ctx
;
1388 GHashTable
*h
= s
->const_table
[type
];
1392 h
= g_hash_table_new(g_int64_hash
, g_int64_equal
);
1393 s
->const_table
[type
] = h
;
1396 ts
= g_hash_table_lookup(h
, &val
);
1400 ts
= tcg_temp_alloc(s
);
1402 if (TCG_TARGET_REG_BITS
== 32 && type
== TCG_TYPE_I64
) {
1403 TCGTemp
*ts2
= tcg_temp_alloc(s
);
1405 tcg_debug_assert(ts2
== ts
+ 1);
1407 ts
->base_type
= TCG_TYPE_I64
;
1408 ts
->type
= TCG_TYPE_I32
;
1409 ts
->kind
= TEMP_CONST
;
1410 ts
->temp_allocated
= 1;
1412 ts2
->base_type
= TCG_TYPE_I64
;
1413 ts2
->type
= TCG_TYPE_I32
;
1414 ts2
->kind
= TEMP_CONST
;
1415 ts2
->temp_allocated
= 1;
1416 ts2
->temp_subindex
= 1;
1419 * Retain the full value of the 64-bit constant in the low
1420 * part, so that the hash table works. Actual uses will
1421 * truncate the value to the low part.
1423 ts
[HOST_BIG_ENDIAN
].val
= val
;
1424 ts
[!HOST_BIG_ENDIAN
].val
= val
>> 32;
1425 val_ptr
= &ts
[HOST_BIG_ENDIAN
].val
;
1427 ts
->base_type
= type
;
1429 ts
->kind
= TEMP_CONST
;
1430 ts
->temp_allocated
= 1;
1434 g_hash_table_insert(h
, val_ptr
, ts
);
1440 TCGv_vec
tcg_constant_vec(TCGType type
, unsigned vece
, int64_t val
)
1442 val
= dup_const(vece
, val
);
1443 return temp_tcgv_vec(tcg_constant_internal(type
, val
));
1446 TCGv_vec
tcg_constant_vec_matching(TCGv_vec match
, unsigned vece
, int64_t val
)
1448 TCGTemp
*t
= tcgv_vec_temp(match
);
1450 tcg_debug_assert(t
->temp_allocated
!= 0);
1451 return tcg_constant_vec(t
->base_type
, vece
, val
);
1454 /* Return true if OP may appear in the opcode stream.
1455 Test the runtime variable that controls each opcode. */
1456 bool tcg_op_supported(TCGOpcode op
)
1459 = TCG_TARGET_HAS_v64
| TCG_TARGET_HAS_v128
| TCG_TARGET_HAS_v256
;
1462 case INDEX_op_discard
:
1463 case INDEX_op_set_label
:
1467 case INDEX_op_insn_start
:
1468 case INDEX_op_exit_tb
:
1469 case INDEX_op_goto_tb
:
1470 case INDEX_op_goto_ptr
:
1471 case INDEX_op_qemu_ld_i32
:
1472 case INDEX_op_qemu_st_i32
:
1473 case INDEX_op_qemu_ld_i64
:
1474 case INDEX_op_qemu_st_i64
:
1477 case INDEX_op_qemu_st8_i32
:
1478 return TCG_TARGET_HAS_qemu_st8_i32
;
1480 case INDEX_op_mov_i32
:
1481 case INDEX_op_setcond_i32
:
1482 case INDEX_op_brcond_i32
:
1483 case INDEX_op_ld8u_i32
:
1484 case INDEX_op_ld8s_i32
:
1485 case INDEX_op_ld16u_i32
:
1486 case INDEX_op_ld16s_i32
:
1487 case INDEX_op_ld_i32
:
1488 case INDEX_op_st8_i32
:
1489 case INDEX_op_st16_i32
:
1490 case INDEX_op_st_i32
:
1491 case INDEX_op_add_i32
:
1492 case INDEX_op_sub_i32
:
1493 case INDEX_op_mul_i32
:
1494 case INDEX_op_and_i32
:
1495 case INDEX_op_or_i32
:
1496 case INDEX_op_xor_i32
:
1497 case INDEX_op_shl_i32
:
1498 case INDEX_op_shr_i32
:
1499 case INDEX_op_sar_i32
:
1502 case INDEX_op_movcond_i32
:
1503 return TCG_TARGET_HAS_movcond_i32
;
1504 case INDEX_op_div_i32
:
1505 case INDEX_op_divu_i32
:
1506 return TCG_TARGET_HAS_div_i32
;
1507 case INDEX_op_rem_i32
:
1508 case INDEX_op_remu_i32
:
1509 return TCG_TARGET_HAS_rem_i32
;
1510 case INDEX_op_div2_i32
:
1511 case INDEX_op_divu2_i32
:
1512 return TCG_TARGET_HAS_div2_i32
;
1513 case INDEX_op_rotl_i32
:
1514 case INDEX_op_rotr_i32
:
1515 return TCG_TARGET_HAS_rot_i32
;
1516 case INDEX_op_deposit_i32
:
1517 return TCG_TARGET_HAS_deposit_i32
;
1518 case INDEX_op_extract_i32
:
1519 return TCG_TARGET_HAS_extract_i32
;
1520 case INDEX_op_sextract_i32
:
1521 return TCG_TARGET_HAS_sextract_i32
;
1522 case INDEX_op_extract2_i32
:
1523 return TCG_TARGET_HAS_extract2_i32
;
1524 case INDEX_op_add2_i32
:
1525 return TCG_TARGET_HAS_add2_i32
;
1526 case INDEX_op_sub2_i32
:
1527 return TCG_TARGET_HAS_sub2_i32
;
1528 case INDEX_op_mulu2_i32
:
1529 return TCG_TARGET_HAS_mulu2_i32
;
1530 case INDEX_op_muls2_i32
:
1531 return TCG_TARGET_HAS_muls2_i32
;
1532 case INDEX_op_muluh_i32
:
1533 return TCG_TARGET_HAS_muluh_i32
;
1534 case INDEX_op_mulsh_i32
:
1535 return TCG_TARGET_HAS_mulsh_i32
;
1536 case INDEX_op_ext8s_i32
:
1537 return TCG_TARGET_HAS_ext8s_i32
;
1538 case INDEX_op_ext16s_i32
:
1539 return TCG_TARGET_HAS_ext16s_i32
;
1540 case INDEX_op_ext8u_i32
:
1541 return TCG_TARGET_HAS_ext8u_i32
;
1542 case INDEX_op_ext16u_i32
:
1543 return TCG_TARGET_HAS_ext16u_i32
;
1544 case INDEX_op_bswap16_i32
:
1545 return TCG_TARGET_HAS_bswap16_i32
;
1546 case INDEX_op_bswap32_i32
:
1547 return TCG_TARGET_HAS_bswap32_i32
;
1548 case INDEX_op_not_i32
:
1549 return TCG_TARGET_HAS_not_i32
;
1550 case INDEX_op_neg_i32
:
1551 return TCG_TARGET_HAS_neg_i32
;
1552 case INDEX_op_andc_i32
:
1553 return TCG_TARGET_HAS_andc_i32
;
1554 case INDEX_op_orc_i32
:
1555 return TCG_TARGET_HAS_orc_i32
;
1556 case INDEX_op_eqv_i32
:
1557 return TCG_TARGET_HAS_eqv_i32
;
1558 case INDEX_op_nand_i32
:
1559 return TCG_TARGET_HAS_nand_i32
;
1560 case INDEX_op_nor_i32
:
1561 return TCG_TARGET_HAS_nor_i32
;
1562 case INDEX_op_clz_i32
:
1563 return TCG_TARGET_HAS_clz_i32
;
1564 case INDEX_op_ctz_i32
:
1565 return TCG_TARGET_HAS_ctz_i32
;
1566 case INDEX_op_ctpop_i32
:
1567 return TCG_TARGET_HAS_ctpop_i32
;
1569 case INDEX_op_brcond2_i32
:
1570 case INDEX_op_setcond2_i32
:
1571 return TCG_TARGET_REG_BITS
== 32;
1573 case INDEX_op_mov_i64
:
1574 case INDEX_op_setcond_i64
:
1575 case INDEX_op_brcond_i64
:
1576 case INDEX_op_ld8u_i64
:
1577 case INDEX_op_ld8s_i64
:
1578 case INDEX_op_ld16u_i64
:
1579 case INDEX_op_ld16s_i64
:
1580 case INDEX_op_ld32u_i64
:
1581 case INDEX_op_ld32s_i64
:
1582 case INDEX_op_ld_i64
:
1583 case INDEX_op_st8_i64
:
1584 case INDEX_op_st16_i64
:
1585 case INDEX_op_st32_i64
:
1586 case INDEX_op_st_i64
:
1587 case INDEX_op_add_i64
:
1588 case INDEX_op_sub_i64
:
1589 case INDEX_op_mul_i64
:
1590 case INDEX_op_and_i64
:
1591 case INDEX_op_or_i64
:
1592 case INDEX_op_xor_i64
:
1593 case INDEX_op_shl_i64
:
1594 case INDEX_op_shr_i64
:
1595 case INDEX_op_sar_i64
:
1596 case INDEX_op_ext_i32_i64
:
1597 case INDEX_op_extu_i32_i64
:
1598 return TCG_TARGET_REG_BITS
== 64;
1600 case INDEX_op_movcond_i64
:
1601 return TCG_TARGET_HAS_movcond_i64
;
1602 case INDEX_op_div_i64
:
1603 case INDEX_op_divu_i64
:
1604 return TCG_TARGET_HAS_div_i64
;
1605 case INDEX_op_rem_i64
:
1606 case INDEX_op_remu_i64
:
1607 return TCG_TARGET_HAS_rem_i64
;
1608 case INDEX_op_div2_i64
:
1609 case INDEX_op_divu2_i64
:
1610 return TCG_TARGET_HAS_div2_i64
;
1611 case INDEX_op_rotl_i64
:
1612 case INDEX_op_rotr_i64
:
1613 return TCG_TARGET_HAS_rot_i64
;
1614 case INDEX_op_deposit_i64
:
1615 return TCG_TARGET_HAS_deposit_i64
;
1616 case INDEX_op_extract_i64
:
1617 return TCG_TARGET_HAS_extract_i64
;
1618 case INDEX_op_sextract_i64
:
1619 return TCG_TARGET_HAS_sextract_i64
;
1620 case INDEX_op_extract2_i64
:
1621 return TCG_TARGET_HAS_extract2_i64
;
1622 case INDEX_op_extrl_i64_i32
:
1623 return TCG_TARGET_HAS_extrl_i64_i32
;
1624 case INDEX_op_extrh_i64_i32
:
1625 return TCG_TARGET_HAS_extrh_i64_i32
;
1626 case INDEX_op_ext8s_i64
:
1627 return TCG_TARGET_HAS_ext8s_i64
;
1628 case INDEX_op_ext16s_i64
:
1629 return TCG_TARGET_HAS_ext16s_i64
;
1630 case INDEX_op_ext32s_i64
:
1631 return TCG_TARGET_HAS_ext32s_i64
;
1632 case INDEX_op_ext8u_i64
:
1633 return TCG_TARGET_HAS_ext8u_i64
;
1634 case INDEX_op_ext16u_i64
:
1635 return TCG_TARGET_HAS_ext16u_i64
;
1636 case INDEX_op_ext32u_i64
:
1637 return TCG_TARGET_HAS_ext32u_i64
;
1638 case INDEX_op_bswap16_i64
:
1639 return TCG_TARGET_HAS_bswap16_i64
;
1640 case INDEX_op_bswap32_i64
:
1641 return TCG_TARGET_HAS_bswap32_i64
;
1642 case INDEX_op_bswap64_i64
:
1643 return TCG_TARGET_HAS_bswap64_i64
;
1644 case INDEX_op_not_i64
:
1645 return TCG_TARGET_HAS_not_i64
;
1646 case INDEX_op_neg_i64
:
1647 return TCG_TARGET_HAS_neg_i64
;
1648 case INDEX_op_andc_i64
:
1649 return TCG_TARGET_HAS_andc_i64
;
1650 case INDEX_op_orc_i64
:
1651 return TCG_TARGET_HAS_orc_i64
;
1652 case INDEX_op_eqv_i64
:
1653 return TCG_TARGET_HAS_eqv_i64
;
1654 case INDEX_op_nand_i64
:
1655 return TCG_TARGET_HAS_nand_i64
;
1656 case INDEX_op_nor_i64
:
1657 return TCG_TARGET_HAS_nor_i64
;
1658 case INDEX_op_clz_i64
:
1659 return TCG_TARGET_HAS_clz_i64
;
1660 case INDEX_op_ctz_i64
:
1661 return TCG_TARGET_HAS_ctz_i64
;
1662 case INDEX_op_ctpop_i64
:
1663 return TCG_TARGET_HAS_ctpop_i64
;
1664 case INDEX_op_add2_i64
:
1665 return TCG_TARGET_HAS_add2_i64
;
1666 case INDEX_op_sub2_i64
:
1667 return TCG_TARGET_HAS_sub2_i64
;
1668 case INDEX_op_mulu2_i64
:
1669 return TCG_TARGET_HAS_mulu2_i64
;
1670 case INDEX_op_muls2_i64
:
1671 return TCG_TARGET_HAS_muls2_i64
;
1672 case INDEX_op_muluh_i64
:
1673 return TCG_TARGET_HAS_muluh_i64
;
1674 case INDEX_op_mulsh_i64
:
1675 return TCG_TARGET_HAS_mulsh_i64
;
1677 case INDEX_op_mov_vec
:
1678 case INDEX_op_dup_vec
:
1679 case INDEX_op_dupm_vec
:
1680 case INDEX_op_ld_vec
:
1681 case INDEX_op_st_vec
:
1682 case INDEX_op_add_vec
:
1683 case INDEX_op_sub_vec
:
1684 case INDEX_op_and_vec
:
1685 case INDEX_op_or_vec
:
1686 case INDEX_op_xor_vec
:
1687 case INDEX_op_cmp_vec
:
1689 case INDEX_op_dup2_vec
:
1690 return have_vec
&& TCG_TARGET_REG_BITS
== 32;
1691 case INDEX_op_not_vec
:
1692 return have_vec
&& TCG_TARGET_HAS_not_vec
;
1693 case INDEX_op_neg_vec
:
1694 return have_vec
&& TCG_TARGET_HAS_neg_vec
;
1695 case INDEX_op_abs_vec
:
1696 return have_vec
&& TCG_TARGET_HAS_abs_vec
;
1697 case INDEX_op_andc_vec
:
1698 return have_vec
&& TCG_TARGET_HAS_andc_vec
;
1699 case INDEX_op_orc_vec
:
1700 return have_vec
&& TCG_TARGET_HAS_orc_vec
;
1701 case INDEX_op_nand_vec
:
1702 return have_vec
&& TCG_TARGET_HAS_nand_vec
;
1703 case INDEX_op_nor_vec
:
1704 return have_vec
&& TCG_TARGET_HAS_nor_vec
;
1705 case INDEX_op_eqv_vec
:
1706 return have_vec
&& TCG_TARGET_HAS_eqv_vec
;
1707 case INDEX_op_mul_vec
:
1708 return have_vec
&& TCG_TARGET_HAS_mul_vec
;
1709 case INDEX_op_shli_vec
:
1710 case INDEX_op_shri_vec
:
1711 case INDEX_op_sari_vec
:
1712 return have_vec
&& TCG_TARGET_HAS_shi_vec
;
1713 case INDEX_op_shls_vec
:
1714 case INDEX_op_shrs_vec
:
1715 case INDEX_op_sars_vec
:
1716 return have_vec
&& TCG_TARGET_HAS_shs_vec
;
1717 case INDEX_op_shlv_vec
:
1718 case INDEX_op_shrv_vec
:
1719 case INDEX_op_sarv_vec
:
1720 return have_vec
&& TCG_TARGET_HAS_shv_vec
;
1721 case INDEX_op_rotli_vec
:
1722 return have_vec
&& TCG_TARGET_HAS_roti_vec
;
1723 case INDEX_op_rotls_vec
:
1724 return have_vec
&& TCG_TARGET_HAS_rots_vec
;
1725 case INDEX_op_rotlv_vec
:
1726 case INDEX_op_rotrv_vec
:
1727 return have_vec
&& TCG_TARGET_HAS_rotv_vec
;
1728 case INDEX_op_ssadd_vec
:
1729 case INDEX_op_usadd_vec
:
1730 case INDEX_op_sssub_vec
:
1731 case INDEX_op_ussub_vec
:
1732 return have_vec
&& TCG_TARGET_HAS_sat_vec
;
1733 case INDEX_op_smin_vec
:
1734 case INDEX_op_umin_vec
:
1735 case INDEX_op_smax_vec
:
1736 case INDEX_op_umax_vec
:
1737 return have_vec
&& TCG_TARGET_HAS_minmax_vec
;
1738 case INDEX_op_bitsel_vec
:
1739 return have_vec
&& TCG_TARGET_HAS_bitsel_vec
;
1740 case INDEX_op_cmpsel_vec
:
1741 return have_vec
&& TCG_TARGET_HAS_cmpsel_vec
;
1744 tcg_debug_assert(op
> INDEX_op_last_generic
&& op
< NB_OPS
);
1749 static TCGOp
*tcg_op_alloc(TCGOpcode opc
, unsigned nargs
);
1751 void tcg_gen_callN(void *func
, TCGTemp
*ret
, int nargs
, TCGTemp
**args
)
1753 const TCGHelperInfo
*info
;
1754 TCGv_i64 extend_free
[MAX_CALL_IARGS
];
1757 int i
, n
, pi
= 0, total_args
;
1759 info
= g_hash_table_lookup(helper_table
, (gpointer
)func
);
1760 total_args
= info
->nr_out
+ info
->nr_in
+ 2;
1761 op
= tcg_op_alloc(INDEX_op_call
, total_args
);
1763 #ifdef CONFIG_PLUGIN
1764 /* Flag helpers that may affect guest state */
1765 if (tcg_ctx
->plugin_insn
&&
1766 !(info
->flags
& TCG_CALL_PLUGIN
) &&
1767 !(info
->flags
& TCG_CALL_NO_SIDE_EFFECTS
)) {
1768 tcg_ctx
->plugin_insn
->calls_helpers
= true;
1772 TCGOP_CALLO(op
) = n
= info
->nr_out
;
1775 tcg_debug_assert(ret
== NULL
);
1778 tcg_debug_assert(ret
!= NULL
);
1779 op
->args
[pi
++] = temp_arg(ret
);
1783 tcg_debug_assert(ret
!= NULL
);
1784 tcg_debug_assert(ret
->base_type
== ret
->type
+ ctz32(n
));
1785 tcg_debug_assert(ret
->temp_subindex
== 0);
1786 for (i
= 0; i
< n
; ++i
) {
1787 op
->args
[pi
++] = temp_arg(ret
+ i
);
1791 g_assert_not_reached();
1794 TCGOP_CALLI(op
) = n
= info
->nr_in
;
1795 for (i
= 0; i
< n
; i
++) {
1796 const TCGCallArgumentLoc
*loc
= &info
->in
[i
];
1797 TCGTemp
*ts
= args
[loc
->arg_idx
] + loc
->tmp_subindex
;
1799 switch (loc
->kind
) {
1800 case TCG_CALL_ARG_NORMAL
:
1801 case TCG_CALL_ARG_BY_REF
:
1802 case TCG_CALL_ARG_BY_REF_N
:
1803 op
->args
[pi
++] = temp_arg(ts
);
1806 case TCG_CALL_ARG_EXTEND_U
:
1807 case TCG_CALL_ARG_EXTEND_S
:
1809 TCGv_i64 temp
= tcg_temp_ebb_new_i64();
1810 TCGv_i32 orig
= temp_tcgv_i32(ts
);
1812 if (loc
->kind
== TCG_CALL_ARG_EXTEND_S
) {
1813 tcg_gen_ext_i32_i64(temp
, orig
);
1815 tcg_gen_extu_i32_i64(temp
, orig
);
1817 op
->args
[pi
++] = tcgv_i64_arg(temp
);
1818 extend_free
[n_extend
++] = temp
;
1823 g_assert_not_reached();
1826 op
->args
[pi
++] = (uintptr_t)func
;
1827 op
->args
[pi
++] = (uintptr_t)info
;
1828 tcg_debug_assert(pi
== total_args
);
1830 QTAILQ_INSERT_TAIL(&tcg_ctx
->ops
, op
, link
);
1832 tcg_debug_assert(n_extend
< ARRAY_SIZE(extend_free
));
1833 for (i
= 0; i
< n_extend
; ++i
) {
1834 tcg_temp_free_i64(extend_free
[i
]);
1838 static void tcg_reg_alloc_start(TCGContext
*s
)
1842 for (i
= 0, n
= s
->nb_temps
; i
< n
; i
++) {
1843 TCGTemp
*ts
= &s
->temps
[i
];
1844 TCGTempVal val
= TEMP_VAL_MEM
;
1848 val
= TEMP_VAL_CONST
;
1856 val
= TEMP_VAL_DEAD
;
1859 ts
->mem_allocated
= 0;
1862 g_assert_not_reached();
1867 memset(s
->reg_to_temp
, 0, sizeof(s
->reg_to_temp
));
1870 static char *tcg_get_arg_str_ptr(TCGContext
*s
, char *buf
, int buf_size
,
1873 int idx
= temp_idx(ts
);
1878 pstrcpy(buf
, buf_size
, ts
->name
);
1881 snprintf(buf
, buf_size
, "loc%d", idx
- s
->nb_globals
);
1884 snprintf(buf
, buf_size
, "tmp%d", idx
- s
->nb_globals
);
1889 snprintf(buf
, buf_size
, "$0x%x", (int32_t)ts
->val
);
1891 #if TCG_TARGET_REG_BITS > 32
1893 snprintf(buf
, buf_size
, "$0x%" PRIx64
, ts
->val
);
1899 snprintf(buf
, buf_size
, "v%d$0x%" PRIx64
,
1900 64 << (ts
->type
- TCG_TYPE_V64
), ts
->val
);
1903 g_assert_not_reached();
1910 static char *tcg_get_arg_str(TCGContext
*s
, char *buf
,
1911 int buf_size
, TCGArg arg
)
1913 return tcg_get_arg_str_ptr(s
, buf
, buf_size
, arg_temp(arg
));
1916 static const char * const cond_name
[] =
1918 [TCG_COND_NEVER
] = "never",
1919 [TCG_COND_ALWAYS
] = "always",
1920 [TCG_COND_EQ
] = "eq",
1921 [TCG_COND_NE
] = "ne",
1922 [TCG_COND_LT
] = "lt",
1923 [TCG_COND_GE
] = "ge",
1924 [TCG_COND_LE
] = "le",
1925 [TCG_COND_GT
] = "gt",
1926 [TCG_COND_LTU
] = "ltu",
1927 [TCG_COND_GEU
] = "geu",
1928 [TCG_COND_LEU
] = "leu",
1929 [TCG_COND_GTU
] = "gtu"
1932 static const char * const ldst_name
[] =
1948 static const char * const alignment_name
[(MO_AMASK
>> MO_ASHIFT
) + 1] = {
1949 #ifdef TARGET_ALIGNED_ONLY
1950 [MO_UNALN
>> MO_ASHIFT
] = "un+",
1951 [MO_ALIGN
>> MO_ASHIFT
] = "",
1953 [MO_UNALN
>> MO_ASHIFT
] = "",
1954 [MO_ALIGN
>> MO_ASHIFT
] = "al+",
1956 [MO_ALIGN_2
>> MO_ASHIFT
] = "al2+",
1957 [MO_ALIGN_4
>> MO_ASHIFT
] = "al4+",
1958 [MO_ALIGN_8
>> MO_ASHIFT
] = "al8+",
1959 [MO_ALIGN_16
>> MO_ASHIFT
] = "al16+",
1960 [MO_ALIGN_32
>> MO_ASHIFT
] = "al32+",
1961 [MO_ALIGN_64
>> MO_ASHIFT
] = "al64+",
1964 static const char bswap_flag_name
[][6] = {
1965 [TCG_BSWAP_IZ
] = "iz",
1966 [TCG_BSWAP_OZ
] = "oz",
1967 [TCG_BSWAP_OS
] = "os",
1968 [TCG_BSWAP_IZ
| TCG_BSWAP_OZ
] = "iz,oz",
1969 [TCG_BSWAP_IZ
| TCG_BSWAP_OS
] = "iz,os",
1972 static inline bool tcg_regset_single(TCGRegSet d
)
1974 return (d
& (d
- 1)) == 0;
1977 static inline TCGReg
tcg_regset_first(TCGRegSet d
)
1979 if (TCG_TARGET_NB_REGS
<= 32) {
1986 /* Return only the number of characters output -- no error return. */
1987 #define ne_fprintf(...) \
1988 ({ int ret_ = fprintf(__VA_ARGS__); ret_ >= 0 ? ret_ : 0; })
1990 static void tcg_dump_ops(TCGContext
*s
, FILE *f
, bool have_prefs
)
1995 QTAILQ_FOREACH(op
, &s
->ops
, link
) {
1996 int i
, k
, nb_oargs
, nb_iargs
, nb_cargs
;
1997 const TCGOpDef
*def
;
2002 def
= &tcg_op_defs
[c
];
2004 if (c
== INDEX_op_insn_start
) {
2006 col
+= ne_fprintf(f
, "\n ----");
2008 for (i
= 0; i
< TARGET_INSN_START_WORDS
; ++i
) {
2010 #if TARGET_LONG_BITS > TCG_TARGET_REG_BITS
2011 a
= deposit64(op
->args
[i
* 2], 32, 32, op
->args
[i
* 2 + 1]);
2015 col
+= ne_fprintf(f
, " " TARGET_FMT_lx
, a
);
2017 } else if (c
== INDEX_op_call
) {
2018 const TCGHelperInfo
*info
= tcg_call_info(op
);
2019 void *func
= tcg_call_func(op
);
2021 /* variable number of arguments */
2022 nb_oargs
= TCGOP_CALLO(op
);
2023 nb_iargs
= TCGOP_CALLI(op
);
2024 nb_cargs
= def
->nb_cargs
;
2026 col
+= ne_fprintf(f
, " %s ", def
->name
);
2029 * Print the function name from TCGHelperInfo, if available.
2030 * Note that plugins have a template function for the info,
2031 * but the actual function pointer comes from the plugin.
2033 if (func
== info
->func
) {
2034 col
+= ne_fprintf(f
, "%s", info
->name
);
2036 col
+= ne_fprintf(f
, "plugin(%p)", func
);
2039 col
+= ne_fprintf(f
, ",$0x%x,$%d", info
->flags
, nb_oargs
);
2040 for (i
= 0; i
< nb_oargs
; i
++) {
2041 col
+= ne_fprintf(f
, ",%s", tcg_get_arg_str(s
, buf
, sizeof(buf
),
2044 for (i
= 0; i
< nb_iargs
; i
++) {
2045 TCGArg arg
= op
->args
[nb_oargs
+ i
];
2046 const char *t
= tcg_get_arg_str(s
, buf
, sizeof(buf
), arg
);
2047 col
+= ne_fprintf(f
, ",%s", t
);
2050 col
+= ne_fprintf(f
, " %s ", def
->name
);
2052 nb_oargs
= def
->nb_oargs
;
2053 nb_iargs
= def
->nb_iargs
;
2054 nb_cargs
= def
->nb_cargs
;
2056 if (def
->flags
& TCG_OPF_VECTOR
) {
2057 col
+= ne_fprintf(f
, "v%d,e%d,", 64 << TCGOP_VECL(op
),
2058 8 << TCGOP_VECE(op
));
2062 for (i
= 0; i
< nb_oargs
; i
++) {
2063 const char *sep
= k
? "," : "";
2064 col
+= ne_fprintf(f
, "%s%s", sep
,
2065 tcg_get_arg_str(s
, buf
, sizeof(buf
),
2068 for (i
= 0; i
< nb_iargs
; i
++) {
2069 const char *sep
= k
? "," : "";
2070 col
+= ne_fprintf(f
, "%s%s", sep
,
2071 tcg_get_arg_str(s
, buf
, sizeof(buf
),
2075 case INDEX_op_brcond_i32
:
2076 case INDEX_op_setcond_i32
:
2077 case INDEX_op_movcond_i32
:
2078 case INDEX_op_brcond2_i32
:
2079 case INDEX_op_setcond2_i32
:
2080 case INDEX_op_brcond_i64
:
2081 case INDEX_op_setcond_i64
:
2082 case INDEX_op_movcond_i64
:
2083 case INDEX_op_cmp_vec
:
2084 case INDEX_op_cmpsel_vec
:
2085 if (op
->args
[k
] < ARRAY_SIZE(cond_name
)
2086 && cond_name
[op
->args
[k
]]) {
2087 col
+= ne_fprintf(f
, ",%s", cond_name
[op
->args
[k
++]]);
2089 col
+= ne_fprintf(f
, ",$0x%" TCG_PRIlx
, op
->args
[k
++]);
2093 case INDEX_op_qemu_ld_i32
:
2094 case INDEX_op_qemu_st_i32
:
2095 case INDEX_op_qemu_st8_i32
:
2096 case INDEX_op_qemu_ld_i64
:
2097 case INDEX_op_qemu_st_i64
:
2099 MemOpIdx oi
= op
->args
[k
++];
2100 MemOp op
= get_memop(oi
);
2101 unsigned ix
= get_mmuidx(oi
);
2103 if (op
& ~(MO_AMASK
| MO_BSWAP
| MO_SSIZE
)) {
2104 col
+= ne_fprintf(f
, ",$0x%x,%u", op
, ix
);
2106 const char *s_al
, *s_op
;
2107 s_al
= alignment_name
[(op
& MO_AMASK
) >> MO_ASHIFT
];
2108 s_op
= ldst_name
[op
& (MO_BSWAP
| MO_SSIZE
)];
2109 col
+= ne_fprintf(f
, ",%s%s,%u", s_al
, s_op
, ix
);
2114 case INDEX_op_bswap16_i32
:
2115 case INDEX_op_bswap16_i64
:
2116 case INDEX_op_bswap32_i32
:
2117 case INDEX_op_bswap32_i64
:
2118 case INDEX_op_bswap64_i64
:
2120 TCGArg flags
= op
->args
[k
];
2121 const char *name
= NULL
;
2123 if (flags
< ARRAY_SIZE(bswap_flag_name
)) {
2124 name
= bswap_flag_name
[flags
];
2127 col
+= ne_fprintf(f
, ",%s", name
);
2129 col
+= ne_fprintf(f
, ",$0x%" TCG_PRIlx
, flags
);
2139 case INDEX_op_set_label
:
2141 case INDEX_op_brcond_i32
:
2142 case INDEX_op_brcond_i64
:
2143 case INDEX_op_brcond2_i32
:
2144 col
+= ne_fprintf(f
, "%s$L%d", k
? "," : "",
2145 arg_label(op
->args
[k
])->id
);
2150 TCGBar membar
= op
->args
[k
];
2151 const char *b_op
, *m_op
;
2153 switch (membar
& TCG_BAR_SC
) {
2167 g_assert_not_reached();
2170 switch (membar
& TCG_MO_ALL
) {
2186 case TCG_MO_LD_LD
| TCG_MO_LD_ST
:
2189 case TCG_MO_LD_LD
| TCG_MO_ST_LD
:
2192 case TCG_MO_LD_LD
| TCG_MO_ST_ST
:
2195 case TCG_MO_LD_ST
| TCG_MO_ST_LD
:
2198 case TCG_MO_LD_ST
| TCG_MO_ST_ST
:
2201 case TCG_MO_ST_LD
| TCG_MO_ST_ST
:
2204 case TCG_MO_LD_LD
| TCG_MO_LD_ST
| TCG_MO_ST_LD
:
2207 case TCG_MO_LD_LD
| TCG_MO_LD_ST
| TCG_MO_ST_ST
:
2210 case TCG_MO_LD_LD
| TCG_MO_ST_LD
| TCG_MO_ST_ST
:
2213 case TCG_MO_LD_ST
| TCG_MO_ST_LD
| TCG_MO_ST_ST
:
2220 g_assert_not_reached();
2223 col
+= ne_fprintf(f
, "%s%s:%s", (k
? "," : ""), b_op
, m_op
);
2230 for (; i
< nb_cargs
; i
++, k
++) {
2231 col
+= ne_fprintf(f
, "%s$0x%" TCG_PRIlx
, k
? "," : "",
2236 if (have_prefs
|| op
->life
) {
2237 for (; col
< 40; ++col
) {
2243 unsigned life
= op
->life
;
2245 if (life
& (SYNC_ARG
* 3)) {
2246 ne_fprintf(f
, " sync:");
2247 for (i
= 0; i
< 2; ++i
) {
2248 if (life
& (SYNC_ARG
<< i
)) {
2249 ne_fprintf(f
, " %d", i
);
2255 ne_fprintf(f
, " dead:");
2256 for (i
= 0; life
; ++i
, life
>>= 1) {
2258 ne_fprintf(f
, " %d", i
);
2265 for (i
= 0; i
< nb_oargs
; ++i
) {
2266 TCGRegSet set
= output_pref(op
, i
);
2269 ne_fprintf(f
, " pref=");
2274 ne_fprintf(f
, "none");
2275 } else if (set
== MAKE_64BIT_MASK(0, TCG_TARGET_NB_REGS
)) {
2276 ne_fprintf(f
, "all");
2277 #ifdef CONFIG_DEBUG_TCG
2278 } else if (tcg_regset_single(set
)) {
2279 TCGReg reg
= tcg_regset_first(set
);
2280 ne_fprintf(f
, "%s", tcg_target_reg_names
[reg
]);
2282 } else if (TCG_TARGET_NB_REGS
<= 32) {
2283 ne_fprintf(f
, "0x%x", (uint32_t)set
);
2285 ne_fprintf(f
, "0x%" PRIx64
, (uint64_t)set
);
2294 /* we give more priority to constraints with less registers */
2295 static int get_constraint_priority(const TCGOpDef
*def
, int k
)
2297 const TCGArgConstraint
*arg_ct
= &def
->args_ct
[k
];
2298 int n
= ctpop64(arg_ct
->regs
);
2301 * Sort constraints of a single register first, which includes output
2302 * aliases (which must exactly match the input already allocated).
2304 if (n
== 1 || arg_ct
->oalias
) {
2309 * Sort register pairs next, first then second immediately after.
2310 * Arbitrarily sort multiple pairs by the index of the first reg;
2311 * there shouldn't be many pairs.
2313 switch (arg_ct
->pair
) {
2318 return (arg_ct
->pair_index
+ 1) * 2 - 1;
2321 /* Finally, sort by decreasing register count. */
2326 /* sort from highest priority to lowest */
2327 static void sort_constraints(TCGOpDef
*def
, int start
, int n
)
2330 TCGArgConstraint
*a
= def
->args_ct
;
2332 for (i
= 0; i
< n
; i
++) {
2333 a
[start
+ i
].sort_index
= start
+ i
;
2338 for (i
= 0; i
< n
- 1; i
++) {
2339 for (j
= i
+ 1; j
< n
; j
++) {
2340 int p1
= get_constraint_priority(def
, a
[start
+ i
].sort_index
);
2341 int p2
= get_constraint_priority(def
, a
[start
+ j
].sort_index
);
2343 int tmp
= a
[start
+ i
].sort_index
;
2344 a
[start
+ i
].sort_index
= a
[start
+ j
].sort_index
;
2345 a
[start
+ j
].sort_index
= tmp
;
2351 static void process_op_defs(TCGContext
*s
)
2355 for (op
= 0; op
< NB_OPS
; op
++) {
2356 TCGOpDef
*def
= &tcg_op_defs
[op
];
2357 const TCGTargetOpDef
*tdefs
;
2358 bool saw_alias_pair
= false;
2359 int i
, o
, i2
, o2
, nb_args
;
2361 if (def
->flags
& TCG_OPF_NOT_PRESENT
) {
2365 nb_args
= def
->nb_iargs
+ def
->nb_oargs
;
2371 * Macro magic should make it impossible, but double-check that
2372 * the array index is in range. Since the signness of an enum
2373 * is implementation defined, force the result to unsigned.
2375 unsigned con_set
= tcg_target_op_def(op
);
2376 tcg_debug_assert(con_set
< ARRAY_SIZE(constraint_sets
));
2377 tdefs
= &constraint_sets
[con_set
];
2379 for (i
= 0; i
< nb_args
; i
++) {
2380 const char *ct_str
= tdefs
->args_ct_str
[i
];
2381 bool input_p
= i
>= def
->nb_oargs
;
2383 /* Incomplete TCGTargetOpDef entry. */
2384 tcg_debug_assert(ct_str
!= NULL
);
2389 tcg_debug_assert(input_p
);
2390 tcg_debug_assert(o
< def
->nb_oargs
);
2391 tcg_debug_assert(def
->args_ct
[o
].regs
!= 0);
2392 tcg_debug_assert(!def
->args_ct
[o
].oalias
);
2393 def
->args_ct
[i
] = def
->args_ct
[o
];
2394 /* The output sets oalias. */
2395 def
->args_ct
[o
].oalias
= 1;
2396 def
->args_ct
[o
].alias_index
= i
;
2397 /* The input sets ialias. */
2398 def
->args_ct
[i
].ialias
= 1;
2399 def
->args_ct
[i
].alias_index
= o
;
2400 if (def
->args_ct
[i
].pair
) {
2401 saw_alias_pair
= true;
2403 tcg_debug_assert(ct_str
[1] == '\0');
2407 tcg_debug_assert(!input_p
);
2408 def
->args_ct
[i
].newreg
= true;
2412 case 'p': /* plus */
2413 /* Allocate to the register after the previous. */
2414 tcg_debug_assert(i
> (input_p
? def
->nb_oargs
: 0));
2416 tcg_debug_assert(!def
->args_ct
[o
].pair
);
2417 tcg_debug_assert(!def
->args_ct
[o
].ct
);
2418 def
->args_ct
[i
] = (TCGArgConstraint
){
2421 .regs
= def
->args_ct
[o
].regs
<< 1,
2423 def
->args_ct
[o
].pair
= 1;
2424 def
->args_ct
[o
].pair_index
= i
;
2425 tcg_debug_assert(ct_str
[1] == '\0');
2428 case 'm': /* minus */
2429 /* Allocate to the register before the previous. */
2430 tcg_debug_assert(i
> (input_p
? def
->nb_oargs
: 0));
2432 tcg_debug_assert(!def
->args_ct
[o
].pair
);
2433 tcg_debug_assert(!def
->args_ct
[o
].ct
);
2434 def
->args_ct
[i
] = (TCGArgConstraint
){
2437 .regs
= def
->args_ct
[o
].regs
>> 1,
2439 def
->args_ct
[o
].pair
= 2;
2440 def
->args_ct
[o
].pair_index
= i
;
2441 tcg_debug_assert(ct_str
[1] == '\0');
2448 def
->args_ct
[i
].ct
|= TCG_CT_CONST
;
2451 /* Include all of the target-specific constraints. */
2454 #define CONST(CASE, MASK) \
2455 case CASE: def->args_ct[i].ct |= MASK; break;
2456 #define REGS(CASE, MASK) \
2457 case CASE: def->args_ct[i].regs |= MASK; break;
2459 #include "tcg-target-con-str.h"
2468 /* Typo in TCGTargetOpDef constraint. */
2469 g_assert_not_reached();
2471 } while (*++ct_str
!= '\0');
2474 /* TCGTargetOpDef entry with too much information? */
2475 tcg_debug_assert(i
== TCG_MAX_OP_ARGS
|| tdefs
->args_ct_str
[i
] == NULL
);
2478 * Fix up output pairs that are aliased with inputs.
2479 * When we created the alias, we copied pair from the output.
2480 * There are three cases:
2481 * (1a) Pairs of inputs alias pairs of outputs.
2482 * (1b) One input aliases the first of a pair of outputs.
2483 * (2) One input aliases the second of a pair of outputs.
2485 * Case 1a is handled by making sure that the pair_index'es are
2486 * properly updated so that they appear the same as a pair of inputs.
2488 * Case 1b is handled by setting the pair_index of the input to
2489 * itself, simply so it doesn't point to an unrelated argument.
2490 * Since we don't encounter the "second" during the input allocation
2491 * phase, nothing happens with the second half of the input pair.
2493 * Case 2 is handled by setting the second input to pair=3, the
2494 * first output to pair=3, and the pair_index'es to match.
2496 if (saw_alias_pair
) {
2497 for (i
= def
->nb_oargs
; i
< nb_args
; i
++) {
2499 * Since [0-9pm] must be alone in the constraint string,
2500 * the only way they can both be set is if the pair comes
2501 * from the output alias.
2503 if (!def
->args_ct
[i
].ialias
) {
2506 switch (def
->args_ct
[i
].pair
) {
2510 o
= def
->args_ct
[i
].alias_index
;
2511 o2
= def
->args_ct
[o
].pair_index
;
2512 tcg_debug_assert(def
->args_ct
[o
].pair
== 1);
2513 tcg_debug_assert(def
->args_ct
[o2
].pair
== 2);
2514 if (def
->args_ct
[o2
].oalias
) {
2516 i2
= def
->args_ct
[o2
].alias_index
;
2517 tcg_debug_assert(def
->args_ct
[i2
].pair
== 2);
2518 def
->args_ct
[i2
].pair_index
= i
;
2519 def
->args_ct
[i
].pair_index
= i2
;
2522 def
->args_ct
[i
].pair_index
= i
;
2526 o
= def
->args_ct
[i
].alias_index
;
2527 o2
= def
->args_ct
[o
].pair_index
;
2528 tcg_debug_assert(def
->args_ct
[o
].pair
== 2);
2529 tcg_debug_assert(def
->args_ct
[o2
].pair
== 1);
2530 if (def
->args_ct
[o2
].oalias
) {
2532 i2
= def
->args_ct
[o2
].alias_index
;
2533 tcg_debug_assert(def
->args_ct
[i2
].pair
== 1);
2534 def
->args_ct
[i2
].pair_index
= i
;
2535 def
->args_ct
[i
].pair_index
= i2
;
2538 def
->args_ct
[i
].pair
= 3;
2539 def
->args_ct
[o2
].pair
= 3;
2540 def
->args_ct
[i
].pair_index
= o2
;
2541 def
->args_ct
[o2
].pair_index
= i
;
2545 g_assert_not_reached();
2550 /* sort the constraints (XXX: this is just an heuristic) */
2551 sort_constraints(def
, 0, def
->nb_oargs
);
2552 sort_constraints(def
, def
->nb_oargs
, def
->nb_iargs
);
2556 static void remove_label_use(TCGOp
*op
, int idx
)
2558 TCGLabel
*label
= arg_label(op
->args
[idx
]);
2561 QSIMPLEQ_FOREACH(use
, &label
->branches
, next
) {
2562 if (use
->op
== op
) {
2563 QSIMPLEQ_REMOVE(&label
->branches
, use
, TCGLabelUse
, next
);
2567 g_assert_not_reached();
2570 void tcg_op_remove(TCGContext
*s
, TCGOp
*op
)
2574 remove_label_use(op
, 0);
2576 case INDEX_op_brcond_i32
:
2577 case INDEX_op_brcond_i64
:
2578 remove_label_use(op
, 3);
2580 case INDEX_op_brcond2_i32
:
2581 remove_label_use(op
, 5);
2587 QTAILQ_REMOVE(&s
->ops
, op
, link
);
2588 QTAILQ_INSERT_TAIL(&s
->free_ops
, op
, link
);
2591 #ifdef CONFIG_PROFILER
2592 qatomic_set(&s
->prof
.del_op_count
, s
->prof
.del_op_count
+ 1);
2596 void tcg_remove_ops_after(TCGOp
*op
)
2598 TCGContext
*s
= tcg_ctx
;
2601 TCGOp
*last
= tcg_last_op();
2605 tcg_op_remove(s
, last
);
2609 static TCGOp
*tcg_op_alloc(TCGOpcode opc
, unsigned nargs
)
2611 TCGContext
*s
= tcg_ctx
;
2614 if (unlikely(!QTAILQ_EMPTY(&s
->free_ops
))) {
2615 QTAILQ_FOREACH(op
, &s
->free_ops
, link
) {
2616 if (nargs
<= op
->nargs
) {
2617 QTAILQ_REMOVE(&s
->free_ops
, op
, link
);
2624 /* Most opcodes have 3 or 4 operands: reduce fragmentation. */
2625 nargs
= MAX(4, nargs
);
2626 op
= tcg_malloc(sizeof(TCGOp
) + sizeof(TCGArg
) * nargs
);
2629 memset(op
, 0, offsetof(TCGOp
, link
));
2633 /* Check for bitfield overflow. */
2634 tcg_debug_assert(op
->nargs
== nargs
);
2640 TCGOp
*tcg_emit_op(TCGOpcode opc
, unsigned nargs
)
2642 TCGOp
*op
= tcg_op_alloc(opc
, nargs
);
2643 QTAILQ_INSERT_TAIL(&tcg_ctx
->ops
, op
, link
);
2647 TCGOp
*tcg_op_insert_before(TCGContext
*s
, TCGOp
*old_op
,
2648 TCGOpcode opc
, unsigned nargs
)
2650 TCGOp
*new_op
= tcg_op_alloc(opc
, nargs
);
2651 QTAILQ_INSERT_BEFORE(old_op
, new_op
, link
);
2655 TCGOp
*tcg_op_insert_after(TCGContext
*s
, TCGOp
*old_op
,
2656 TCGOpcode opc
, unsigned nargs
)
2658 TCGOp
*new_op
= tcg_op_alloc(opc
, nargs
);
2659 QTAILQ_INSERT_AFTER(&s
->ops
, old_op
, new_op
, link
);
2663 static void move_label_uses(TCGLabel
*to
, TCGLabel
*from
)
2667 QSIMPLEQ_FOREACH(u
, &from
->branches
, next
) {
2671 op
->args
[0] = label_arg(to
);
2673 case INDEX_op_brcond_i32
:
2674 case INDEX_op_brcond_i64
:
2675 op
->args
[3] = label_arg(to
);
2677 case INDEX_op_brcond2_i32
:
2678 op
->args
[5] = label_arg(to
);
2681 g_assert_not_reached();
2685 QSIMPLEQ_CONCAT(&to
->branches
, &from
->branches
);
2688 /* Reachable analysis : remove unreachable code. */
2689 static void __attribute__((noinline
))
2690 reachable_code_pass(TCGContext
*s
)
2692 TCGOp
*op
, *op_next
, *op_prev
;
2695 QTAILQ_FOREACH_SAFE(op
, &s
->ops
, link
, op_next
) {
2700 case INDEX_op_set_label
:
2701 label
= arg_label(op
->args
[0]);
2704 * Note that the first op in the TB is always a load,
2705 * so there is always something before a label.
2707 op_prev
= QTAILQ_PREV(op
, link
);
2710 * If we find two sequential labels, move all branches to
2711 * reference the second label and remove the first label.
2712 * Do this before branch to next optimization, so that the
2713 * middle label is out of the way.
2715 if (op_prev
->opc
== INDEX_op_set_label
) {
2716 move_label_uses(label
, arg_label(op_prev
->args
[0]));
2717 tcg_op_remove(s
, op_prev
);
2718 op_prev
= QTAILQ_PREV(op
, link
);
2722 * Optimization can fold conditional branches to unconditional.
2723 * If we find a label which is preceded by an unconditional
2724 * branch to next, remove the branch. We couldn't do this when
2725 * processing the branch because any dead code between the branch
2726 * and label had not yet been removed.
2728 if (op_prev
->opc
== INDEX_op_br
&&
2729 label
== arg_label(op_prev
->args
[0])) {
2730 tcg_op_remove(s
, op_prev
);
2731 /* Fall through means insns become live again. */
2735 if (QSIMPLEQ_EMPTY(&label
->branches
)) {
2737 * While there is an occasional backward branch, virtually
2738 * all branches generated by the translators are forward.
2739 * Which means that generally we will have already removed
2740 * all references to the label that will be, and there is
2741 * little to be gained by iterating.
2745 /* Once we see a label, insns become live again. */
2752 case INDEX_op_exit_tb
:
2753 case INDEX_op_goto_ptr
:
2754 /* Unconditional branches; everything following is dead. */
2759 /* Notice noreturn helper calls, raising exceptions. */
2760 if (tcg_call_flags(op
) & TCG_CALL_NO_RETURN
) {
2765 case INDEX_op_insn_start
:
2766 /* Never remove -- we need to keep these for unwind. */
2775 tcg_op_remove(s
, op
);
2783 #define IS_DEAD_ARG(n) (arg_life & (DEAD_ARG << (n)))
2784 #define NEED_SYNC_ARG(n) (arg_life & (SYNC_ARG << (n)))
2786 /* For liveness_pass_1, the register preferences for a given temp. */
2787 static inline TCGRegSet
*la_temp_pref(TCGTemp
*ts
)
2789 return ts
->state_ptr
;
2792 /* For liveness_pass_1, reset the preferences for a given temp to the
2793 * maximal regset for its type.
2795 static inline void la_reset_pref(TCGTemp
*ts
)
2798 = (ts
->state
== TS_DEAD
? 0 : tcg_target_available_regs
[ts
->type
]);
2801 /* liveness analysis: end of function: all temps are dead, and globals
2802 should be in memory. */
2803 static void la_func_end(TCGContext
*s
, int ng
, int nt
)
2807 for (i
= 0; i
< ng
; ++i
) {
2808 s
->temps
[i
].state
= TS_DEAD
| TS_MEM
;
2809 la_reset_pref(&s
->temps
[i
]);
2811 for (i
= ng
; i
< nt
; ++i
) {
2812 s
->temps
[i
].state
= TS_DEAD
;
2813 la_reset_pref(&s
->temps
[i
]);
2817 /* liveness analysis: end of basic block: all temps are dead, globals
2818 and local temps should be in memory. */
2819 static void la_bb_end(TCGContext
*s
, int ng
, int nt
)
2823 for (i
= 0; i
< nt
; ++i
) {
2824 TCGTemp
*ts
= &s
->temps
[i
];
2831 state
= TS_DEAD
| TS_MEM
;
2838 g_assert_not_reached();
2845 /* liveness analysis: sync globals back to memory. */
2846 static void la_global_sync(TCGContext
*s
, int ng
)
2850 for (i
= 0; i
< ng
; ++i
) {
2851 int state
= s
->temps
[i
].state
;
2852 s
->temps
[i
].state
= state
| TS_MEM
;
2853 if (state
== TS_DEAD
) {
2854 /* If the global was previously dead, reset prefs. */
2855 la_reset_pref(&s
->temps
[i
]);
2861 * liveness analysis: conditional branch: all temps are dead unless
2862 * explicitly live-across-conditional-branch, globals and local temps
2865 static void la_bb_sync(TCGContext
*s
, int ng
, int nt
)
2867 la_global_sync(s
, ng
);
2869 for (int i
= ng
; i
< nt
; ++i
) {
2870 TCGTemp
*ts
= &s
->temps
[i
];
2876 ts
->state
= state
| TS_MEM
;
2877 if (state
!= TS_DEAD
) {
2885 g_assert_not_reached();
2887 la_reset_pref(&s
->temps
[i
]);
2891 /* liveness analysis: sync globals back to memory and kill. */
2892 static void la_global_kill(TCGContext
*s
, int ng
)
2896 for (i
= 0; i
< ng
; i
++) {
2897 s
->temps
[i
].state
= TS_DEAD
| TS_MEM
;
2898 la_reset_pref(&s
->temps
[i
]);
2902 /* liveness analysis: note live globals crossing calls. */
2903 static void la_cross_call(TCGContext
*s
, int nt
)
2905 TCGRegSet mask
= ~tcg_target_call_clobber_regs
;
2908 for (i
= 0; i
< nt
; i
++) {
2909 TCGTemp
*ts
= &s
->temps
[i
];
2910 if (!(ts
->state
& TS_DEAD
)) {
2911 TCGRegSet
*pset
= la_temp_pref(ts
);
2912 TCGRegSet set
= *pset
;
2915 /* If the combination is not possible, restart. */
2917 set
= tcg_target_available_regs
[ts
->type
] & mask
;
2925 * Liveness analysis: Verify the lifetime of TEMP_TB, and reduce
2926 * to TEMP_EBB, if possible.
2928 static void __attribute__((noinline
))
2929 liveness_pass_0(TCGContext
*s
)
2931 void * const multiple_ebb
= (void *)(uintptr_t)-1;
2932 int nb_temps
= s
->nb_temps
;
2935 for (int i
= s
->nb_globals
; i
< nb_temps
; ++i
) {
2936 s
->temps
[i
].state_ptr
= NULL
;
2940 * Represent each EBB by the op at which it begins. In the case of
2941 * the first EBB, this is the first op, otherwise it is a label.
2942 * Collect the uses of each TEMP_TB: NULL for unused, EBB for use
2943 * within a single EBB, else MULTIPLE_EBB.
2945 ebb
= QTAILQ_FIRST(&s
->ops
);
2946 QTAILQ_FOREACH(op
, &s
->ops
, link
) {
2947 const TCGOpDef
*def
;
2948 int nb_oargs
, nb_iargs
;
2951 case INDEX_op_set_label
:
2954 case INDEX_op_discard
:
2957 nb_oargs
= TCGOP_CALLO(op
);
2958 nb_iargs
= TCGOP_CALLI(op
);
2961 def
= &tcg_op_defs
[op
->opc
];
2962 nb_oargs
= def
->nb_oargs
;
2963 nb_iargs
= def
->nb_iargs
;
2967 for (int i
= 0; i
< nb_oargs
+ nb_iargs
; ++i
) {
2968 TCGTemp
*ts
= arg_temp(op
->args
[i
]);
2970 if (ts
->kind
!= TEMP_TB
) {
2973 if (ts
->state_ptr
== NULL
) {
2974 ts
->state_ptr
= ebb
;
2975 } else if (ts
->state_ptr
!= ebb
) {
2976 ts
->state_ptr
= multiple_ebb
;
2982 * For TEMP_TB that turned out not to be used beyond one EBB,
2983 * reduce the liveness to TEMP_EBB.
2985 for (int i
= s
->nb_globals
; i
< nb_temps
; ++i
) {
2986 TCGTemp
*ts
= &s
->temps
[i
];
2987 if (ts
->kind
== TEMP_TB
&& ts
->state_ptr
!= multiple_ebb
) {
2988 ts
->kind
= TEMP_EBB
;
2993 /* Liveness analysis : update the opc_arg_life array to tell if a
2994 given input arguments is dead. Instructions updating dead
2995 temporaries are removed. */
2996 static void __attribute__((noinline
))
2997 liveness_pass_1(TCGContext
*s
)
2999 int nb_globals
= s
->nb_globals
;
3000 int nb_temps
= s
->nb_temps
;
3001 TCGOp
*op
, *op_prev
;
3005 prefs
= tcg_malloc(sizeof(TCGRegSet
) * nb_temps
);
3006 for (i
= 0; i
< nb_temps
; ++i
) {
3007 s
->temps
[i
].state_ptr
= prefs
+ i
;
3010 /* ??? Should be redundant with the exit_tb that ends the TB. */
3011 la_func_end(s
, nb_globals
, nb_temps
);
3013 QTAILQ_FOREACH_REVERSE_SAFE(op
, &s
->ops
, link
, op_prev
) {
3014 int nb_iargs
, nb_oargs
;
3015 TCGOpcode opc_new
, opc_new2
;
3017 TCGLifeData arg_life
= 0;
3019 TCGOpcode opc
= op
->opc
;
3020 const TCGOpDef
*def
= &tcg_op_defs
[opc
];
3025 const TCGHelperInfo
*info
= tcg_call_info(op
);
3026 int call_flags
= tcg_call_flags(op
);
3028 nb_oargs
= TCGOP_CALLO(op
);
3029 nb_iargs
= TCGOP_CALLI(op
);
3031 /* pure functions can be removed if their result is unused */
3032 if (call_flags
& TCG_CALL_NO_SIDE_EFFECTS
) {
3033 for (i
= 0; i
< nb_oargs
; i
++) {
3034 ts
= arg_temp(op
->args
[i
]);
3035 if (ts
->state
!= TS_DEAD
) {
3036 goto do_not_remove_call
;
3043 /* Output args are dead. */
3044 for (i
= 0; i
< nb_oargs
; i
++) {
3045 ts
= arg_temp(op
->args
[i
]);
3046 if (ts
->state
& TS_DEAD
) {
3047 arg_life
|= DEAD_ARG
<< i
;
3049 if (ts
->state
& TS_MEM
) {
3050 arg_life
|= SYNC_ARG
<< i
;
3052 ts
->state
= TS_DEAD
;
3056 /* Not used -- it will be tcg_target_call_oarg_reg(). */
3057 memset(op
->output_pref
, 0, sizeof(op
->output_pref
));
3059 if (!(call_flags
& (TCG_CALL_NO_WRITE_GLOBALS
|
3060 TCG_CALL_NO_READ_GLOBALS
))) {
3061 la_global_kill(s
, nb_globals
);
3062 } else if (!(call_flags
& TCG_CALL_NO_READ_GLOBALS
)) {
3063 la_global_sync(s
, nb_globals
);
3066 /* Record arguments that die in this helper. */
3067 for (i
= nb_oargs
; i
< nb_iargs
+ nb_oargs
; i
++) {
3068 ts
= arg_temp(op
->args
[i
]);
3069 if (ts
->state
& TS_DEAD
) {
3070 arg_life
|= DEAD_ARG
<< i
;
3074 /* For all live registers, remove call-clobbered prefs. */
3075 la_cross_call(s
, nb_temps
);
3078 * Input arguments are live for preceding opcodes.
3080 * For those arguments that die, and will be allocated in
3081 * registers, clear the register set for that arg, to be
3082 * filled in below. For args that will be on the stack,
3083 * reset to any available reg. Process arguments in reverse
3084 * order so that if a temp is used more than once, the stack
3085 * reset to max happens before the register reset to 0.
3087 for (i
= nb_iargs
- 1; i
>= 0; i
--) {
3088 const TCGCallArgumentLoc
*loc
= &info
->in
[i
];
3089 ts
= arg_temp(op
->args
[nb_oargs
+ i
]);
3091 if (ts
->state
& TS_DEAD
) {
3092 switch (loc
->kind
) {
3093 case TCG_CALL_ARG_NORMAL
:
3094 case TCG_CALL_ARG_EXTEND_U
:
3095 case TCG_CALL_ARG_EXTEND_S
:
3097 *la_temp_pref(ts
) = 0;
3103 tcg_target_available_regs
[ts
->type
];
3106 ts
->state
&= ~TS_DEAD
;
3111 * For each input argument, add its input register to prefs.
3112 * If a temp is used once, this produces a single set bit;
3113 * if a temp is used multiple times, this produces a set.
3115 for (i
= 0; i
< nb_iargs
; i
++) {
3116 const TCGCallArgumentLoc
*loc
= &info
->in
[i
];
3117 ts
= arg_temp(op
->args
[nb_oargs
+ i
]);
3119 switch (loc
->kind
) {
3120 case TCG_CALL_ARG_NORMAL
:
3121 case TCG_CALL_ARG_EXTEND_U
:
3122 case TCG_CALL_ARG_EXTEND_S
:
3124 tcg_regset_set_reg(*la_temp_pref(ts
),
3125 tcg_target_call_iarg_regs
[loc
->arg_slot
]);
3134 case INDEX_op_insn_start
:
3136 case INDEX_op_discard
:
3137 /* mark the temporary as dead */
3138 ts
= arg_temp(op
->args
[0]);
3139 ts
->state
= TS_DEAD
;
3143 case INDEX_op_add2_i32
:
3144 opc_new
= INDEX_op_add_i32
;
3146 case INDEX_op_sub2_i32
:
3147 opc_new
= INDEX_op_sub_i32
;
3149 case INDEX_op_add2_i64
:
3150 opc_new
= INDEX_op_add_i64
;
3152 case INDEX_op_sub2_i64
:
3153 opc_new
= INDEX_op_sub_i64
;
3157 /* Test if the high part of the operation is dead, but not
3158 the low part. The result can be optimized to a simple
3159 add or sub. This happens often for x86_64 guest when the
3160 cpu mode is set to 32 bit. */
3161 if (arg_temp(op
->args
[1])->state
== TS_DEAD
) {
3162 if (arg_temp(op
->args
[0])->state
== TS_DEAD
) {
3165 /* Replace the opcode and adjust the args in place,
3166 leaving 3 unused args at the end. */
3167 op
->opc
= opc
= opc_new
;
3168 op
->args
[1] = op
->args
[2];
3169 op
->args
[2] = op
->args
[4];
3170 /* Fall through and mark the single-word operation live. */
3176 case INDEX_op_mulu2_i32
:
3177 opc_new
= INDEX_op_mul_i32
;
3178 opc_new2
= INDEX_op_muluh_i32
;
3179 have_opc_new2
= TCG_TARGET_HAS_muluh_i32
;
3181 case INDEX_op_muls2_i32
:
3182 opc_new
= INDEX_op_mul_i32
;
3183 opc_new2
= INDEX_op_mulsh_i32
;
3184 have_opc_new2
= TCG_TARGET_HAS_mulsh_i32
;
3186 case INDEX_op_mulu2_i64
:
3187 opc_new
= INDEX_op_mul_i64
;
3188 opc_new2
= INDEX_op_muluh_i64
;
3189 have_opc_new2
= TCG_TARGET_HAS_muluh_i64
;
3191 case INDEX_op_muls2_i64
:
3192 opc_new
= INDEX_op_mul_i64
;
3193 opc_new2
= INDEX_op_mulsh_i64
;
3194 have_opc_new2
= TCG_TARGET_HAS_mulsh_i64
;
3199 if (arg_temp(op
->args
[1])->state
== TS_DEAD
) {
3200 if (arg_temp(op
->args
[0])->state
== TS_DEAD
) {
3201 /* Both parts of the operation are dead. */
3204 /* The high part of the operation is dead; generate the low. */
3205 op
->opc
= opc
= opc_new
;
3206 op
->args
[1] = op
->args
[2];
3207 op
->args
[2] = op
->args
[3];
3208 } else if (arg_temp(op
->args
[0])->state
== TS_DEAD
&& have_opc_new2
) {
3209 /* The low part of the operation is dead; generate the high. */
3210 op
->opc
= opc
= opc_new2
;
3211 op
->args
[0] = op
->args
[1];
3212 op
->args
[1] = op
->args
[2];
3213 op
->args
[2] = op
->args
[3];
3217 /* Mark the single-word operation live. */
3222 /* XXX: optimize by hardcoding common cases (e.g. triadic ops) */
3223 nb_iargs
= def
->nb_iargs
;
3224 nb_oargs
= def
->nb_oargs
;
3226 /* Test if the operation can be removed because all
3227 its outputs are dead. We assume that nb_oargs == 0
3228 implies side effects */
3229 if (!(def
->flags
& TCG_OPF_SIDE_EFFECTS
) && nb_oargs
!= 0) {
3230 for (i
= 0; i
< nb_oargs
; i
++) {
3231 if (arg_temp(op
->args
[i
])->state
!= TS_DEAD
) {
3240 tcg_op_remove(s
, op
);
3244 for (i
= 0; i
< nb_oargs
; i
++) {
3245 ts
= arg_temp(op
->args
[i
]);
3247 /* Remember the preference of the uses that followed. */
3248 if (i
< ARRAY_SIZE(op
->output_pref
)) {
3249 op
->output_pref
[i
] = *la_temp_pref(ts
);
3252 /* Output args are dead. */
3253 if (ts
->state
& TS_DEAD
) {
3254 arg_life
|= DEAD_ARG
<< i
;
3256 if (ts
->state
& TS_MEM
) {
3257 arg_life
|= SYNC_ARG
<< i
;
3259 ts
->state
= TS_DEAD
;
3263 /* If end of basic block, update. */
3264 if (def
->flags
& TCG_OPF_BB_EXIT
) {
3265 la_func_end(s
, nb_globals
, nb_temps
);
3266 } else if (def
->flags
& TCG_OPF_COND_BRANCH
) {
3267 la_bb_sync(s
, nb_globals
, nb_temps
);
3268 } else if (def
->flags
& TCG_OPF_BB_END
) {
3269 la_bb_end(s
, nb_globals
, nb_temps
);
3270 } else if (def
->flags
& TCG_OPF_SIDE_EFFECTS
) {
3271 la_global_sync(s
, nb_globals
);
3272 if (def
->flags
& TCG_OPF_CALL_CLOBBER
) {
3273 la_cross_call(s
, nb_temps
);
3277 /* Record arguments that die in this opcode. */
3278 for (i
= nb_oargs
; i
< nb_oargs
+ nb_iargs
; i
++) {
3279 ts
= arg_temp(op
->args
[i
]);
3280 if (ts
->state
& TS_DEAD
) {
3281 arg_life
|= DEAD_ARG
<< i
;
3285 /* Input arguments are live for preceding opcodes. */
3286 for (i
= nb_oargs
; i
< nb_oargs
+ nb_iargs
; i
++) {
3287 ts
= arg_temp(op
->args
[i
]);
3288 if (ts
->state
& TS_DEAD
) {
3289 /* For operands that were dead, initially allow
3290 all regs for the type. */
3291 *la_temp_pref(ts
) = tcg_target_available_regs
[ts
->type
];
3292 ts
->state
&= ~TS_DEAD
;
3296 /* Incorporate constraints for this operand. */
3298 case INDEX_op_mov_i32
:
3299 case INDEX_op_mov_i64
:
3300 /* Note that these are TCG_OPF_NOT_PRESENT and do not
3301 have proper constraints. That said, special case
3302 moves to propagate preferences backward. */
3303 if (IS_DEAD_ARG(1)) {
3304 *la_temp_pref(arg_temp(op
->args
[0]))
3305 = *la_temp_pref(arg_temp(op
->args
[1]));
3310 for (i
= nb_oargs
; i
< nb_oargs
+ nb_iargs
; i
++) {
3311 const TCGArgConstraint
*ct
= &def
->args_ct
[i
];
3312 TCGRegSet set
, *pset
;
3314 ts
= arg_temp(op
->args
[i
]);
3315 pset
= la_temp_pref(ts
);
3320 set
&= output_pref(op
, ct
->alias_index
);
3322 /* If the combination is not possible, restart. */
3332 op
->life
= arg_life
;
3336 /* Liveness analysis: Convert indirect regs to direct temporaries. */
3337 static bool __attribute__((noinline
))
3338 liveness_pass_2(TCGContext
*s
)
3340 int nb_globals
= s
->nb_globals
;
3342 bool changes
= false;
3343 TCGOp
*op
, *op_next
;
3345 /* Create a temporary for each indirect global. */
3346 for (i
= 0; i
< nb_globals
; ++i
) {
3347 TCGTemp
*its
= &s
->temps
[i
];
3348 if (its
->indirect_reg
) {
3349 TCGTemp
*dts
= tcg_temp_alloc(s
);
3350 dts
->type
= its
->type
;
3351 dts
->base_type
= its
->base_type
;
3352 dts
->temp_subindex
= its
->temp_subindex
;
3353 dts
->kind
= TEMP_EBB
;
3354 its
->state_ptr
= dts
;
3356 its
->state_ptr
= NULL
;
3358 /* All globals begin dead. */
3359 its
->state
= TS_DEAD
;
3361 for (nb_temps
= s
->nb_temps
; i
< nb_temps
; ++i
) {
3362 TCGTemp
*its
= &s
->temps
[i
];
3363 its
->state_ptr
= NULL
;
3364 its
->state
= TS_DEAD
;
3367 QTAILQ_FOREACH_SAFE(op
, &s
->ops
, link
, op_next
) {
3368 TCGOpcode opc
= op
->opc
;
3369 const TCGOpDef
*def
= &tcg_op_defs
[opc
];
3370 TCGLifeData arg_life
= op
->life
;
3371 int nb_iargs
, nb_oargs
, call_flags
;
3372 TCGTemp
*arg_ts
, *dir_ts
;
3374 if (opc
== INDEX_op_call
) {
3375 nb_oargs
= TCGOP_CALLO(op
);
3376 nb_iargs
= TCGOP_CALLI(op
);
3377 call_flags
= tcg_call_flags(op
);
3379 nb_iargs
= def
->nb_iargs
;
3380 nb_oargs
= def
->nb_oargs
;
3382 /* Set flags similar to how calls require. */
3383 if (def
->flags
& TCG_OPF_COND_BRANCH
) {
3384 /* Like reading globals: sync_globals */
3385 call_flags
= TCG_CALL_NO_WRITE_GLOBALS
;
3386 } else if (def
->flags
& TCG_OPF_BB_END
) {
3387 /* Like writing globals: save_globals */
3389 } else if (def
->flags
& TCG_OPF_SIDE_EFFECTS
) {
3390 /* Like reading globals: sync_globals */
3391 call_flags
= TCG_CALL_NO_WRITE_GLOBALS
;
3393 /* No effect on globals. */
3394 call_flags
= (TCG_CALL_NO_READ_GLOBALS
|
3395 TCG_CALL_NO_WRITE_GLOBALS
);
3399 /* Make sure that input arguments are available. */
3400 for (i
= nb_oargs
; i
< nb_iargs
+ nb_oargs
; i
++) {
3401 arg_ts
= arg_temp(op
->args
[i
]);
3402 dir_ts
= arg_ts
->state_ptr
;
3403 if (dir_ts
&& arg_ts
->state
== TS_DEAD
) {
3404 TCGOpcode lopc
= (arg_ts
->type
== TCG_TYPE_I32
3407 TCGOp
*lop
= tcg_op_insert_before(s
, op
, lopc
, 3);
3409 lop
->args
[0] = temp_arg(dir_ts
);
3410 lop
->args
[1] = temp_arg(arg_ts
->mem_base
);
3411 lop
->args
[2] = arg_ts
->mem_offset
;
3413 /* Loaded, but synced with memory. */
3414 arg_ts
->state
= TS_MEM
;
3418 /* Perform input replacement, and mark inputs that became dead.
3419 No action is required except keeping temp_state up to date
3420 so that we reload when needed. */
3421 for (i
= nb_oargs
; i
< nb_iargs
+ nb_oargs
; i
++) {
3422 arg_ts
= arg_temp(op
->args
[i
]);
3423 dir_ts
= arg_ts
->state_ptr
;
3425 op
->args
[i
] = temp_arg(dir_ts
);
3427 if (IS_DEAD_ARG(i
)) {
3428 arg_ts
->state
= TS_DEAD
;
3433 /* Liveness analysis should ensure that the following are
3434 all correct, for call sites and basic block end points. */
3435 if (call_flags
& TCG_CALL_NO_READ_GLOBALS
) {
3437 } else if (call_flags
& TCG_CALL_NO_WRITE_GLOBALS
) {
3438 for (i
= 0; i
< nb_globals
; ++i
) {
3439 /* Liveness should see that globals are synced back,
3440 that is, either TS_DEAD or TS_MEM. */
3441 arg_ts
= &s
->temps
[i
];
3442 tcg_debug_assert(arg_ts
->state_ptr
== 0
3443 || arg_ts
->state
!= 0);
3446 for (i
= 0; i
< nb_globals
; ++i
) {
3447 /* Liveness should see that globals are saved back,
3448 that is, TS_DEAD, waiting to be reloaded. */
3449 arg_ts
= &s
->temps
[i
];
3450 tcg_debug_assert(arg_ts
->state_ptr
== 0
3451 || arg_ts
->state
== TS_DEAD
);
3455 /* Outputs become available. */
3456 if (opc
== INDEX_op_mov_i32
|| opc
== INDEX_op_mov_i64
) {
3457 arg_ts
= arg_temp(op
->args
[0]);
3458 dir_ts
= arg_ts
->state_ptr
;
3460 op
->args
[0] = temp_arg(dir_ts
);
3463 /* The output is now live and modified. */
3466 if (NEED_SYNC_ARG(0)) {
3467 TCGOpcode sopc
= (arg_ts
->type
== TCG_TYPE_I32
3470 TCGOp
*sop
= tcg_op_insert_after(s
, op
, sopc
, 3);
3471 TCGTemp
*out_ts
= dir_ts
;
3473 if (IS_DEAD_ARG(0)) {
3474 out_ts
= arg_temp(op
->args
[1]);
3475 arg_ts
->state
= TS_DEAD
;
3476 tcg_op_remove(s
, op
);
3478 arg_ts
->state
= TS_MEM
;
3481 sop
->args
[0] = temp_arg(out_ts
);
3482 sop
->args
[1] = temp_arg(arg_ts
->mem_base
);
3483 sop
->args
[2] = arg_ts
->mem_offset
;
3485 tcg_debug_assert(!IS_DEAD_ARG(0));
3489 for (i
= 0; i
< nb_oargs
; i
++) {
3490 arg_ts
= arg_temp(op
->args
[i
]);
3491 dir_ts
= arg_ts
->state_ptr
;
3495 op
->args
[i
] = temp_arg(dir_ts
);
3498 /* The output is now live and modified. */
3501 /* Sync outputs upon their last write. */
3502 if (NEED_SYNC_ARG(i
)) {
3503 TCGOpcode sopc
= (arg_ts
->type
== TCG_TYPE_I32
3506 TCGOp
*sop
= tcg_op_insert_after(s
, op
, sopc
, 3);
3508 sop
->args
[0] = temp_arg(dir_ts
);
3509 sop
->args
[1] = temp_arg(arg_ts
->mem_base
);
3510 sop
->args
[2] = arg_ts
->mem_offset
;
3512 arg_ts
->state
= TS_MEM
;
3514 /* Drop outputs that are dead. */
3515 if (IS_DEAD_ARG(i
)) {
3516 arg_ts
->state
= TS_DEAD
;
3525 static void temp_allocate_frame(TCGContext
*s
, TCGTemp
*ts
)
3530 /* When allocating an object, look at the full type. */
3531 size
= tcg_type_size(ts
->base_type
);
3532 switch (ts
->base_type
) {
3544 * Note that we do not require aligned storage for V256,
3545 * and that we provide alignment for I128 to match V128,
3546 * even if that's above what the host ABI requires.
3551 g_assert_not_reached();
3555 * Assume the stack is sufficiently aligned.
3556 * This affects e.g. ARM NEON, where we have 8 byte stack alignment
3557 * and do not require 16 byte vector alignment. This seems slightly
3558 * easier than fully parameterizing the above switch statement.
3560 align
= MIN(TCG_TARGET_STACK_ALIGN
, align
);
3561 off
= ROUND_UP(s
->current_frame_offset
, align
);
3563 /* If we've exhausted the stack frame, restart with a smaller TB. */
3564 if (off
+ size
> s
->frame_end
) {
3565 tcg_raise_tb_overflow(s
);
3567 s
->current_frame_offset
= off
+ size
;
3568 #if defined(__sparc__)
3569 off
+= TCG_TARGET_STACK_BIAS
;
3572 /* If the object was subdivided, assign memory to all the parts. */
3573 if (ts
->base_type
!= ts
->type
) {
3574 int part_size
= tcg_type_size(ts
->type
);
3575 int part_count
= size
/ part_size
;
3578 * Each part is allocated sequentially in tcg_temp_new_internal.
3579 * Jump back to the first part by subtracting the current index.
3581 ts
-= ts
->temp_subindex
;
3582 for (int i
= 0; i
< part_count
; ++i
) {
3583 ts
[i
].mem_offset
= off
+ i
* part_size
;
3584 ts
[i
].mem_base
= s
->frame_temp
;
3585 ts
[i
].mem_allocated
= 1;
3588 ts
->mem_offset
= off
;
3589 ts
->mem_base
= s
->frame_temp
;
3590 ts
->mem_allocated
= 1;
3594 /* Assign @reg to @ts, and update reg_to_temp[]. */
3595 static void set_temp_val_reg(TCGContext
*s
, TCGTemp
*ts
, TCGReg reg
)
3597 if (ts
->val_type
== TEMP_VAL_REG
) {
3598 TCGReg old
= ts
->reg
;
3599 tcg_debug_assert(s
->reg_to_temp
[old
] == ts
);
3603 s
->reg_to_temp
[old
] = NULL
;
3605 tcg_debug_assert(s
->reg_to_temp
[reg
] == NULL
);
3606 s
->reg_to_temp
[reg
] = ts
;
3607 ts
->val_type
= TEMP_VAL_REG
;
3611 /* Assign a non-register value type to @ts, and update reg_to_temp[]. */
3612 static void set_temp_val_nonreg(TCGContext
*s
, TCGTemp
*ts
, TCGTempVal type
)
3614 tcg_debug_assert(type
!= TEMP_VAL_REG
);
3615 if (ts
->val_type
== TEMP_VAL_REG
) {
3616 TCGReg reg
= ts
->reg
;
3617 tcg_debug_assert(s
->reg_to_temp
[reg
] == ts
);
3618 s
->reg_to_temp
[reg
] = NULL
;
3620 ts
->val_type
= type
;
3623 static void temp_load(TCGContext
*, TCGTemp
*, TCGRegSet
, TCGRegSet
, TCGRegSet
);
3625 /* Mark a temporary as free or dead. If 'free_or_dead' is negative,
3626 mark it free; otherwise mark it dead. */
3627 static void temp_free_or_dead(TCGContext
*s
, TCGTemp
*ts
, int free_or_dead
)
3629 TCGTempVal new_type
;
3636 new_type
= TEMP_VAL_MEM
;
3639 new_type
= free_or_dead
< 0 ? TEMP_VAL_MEM
: TEMP_VAL_DEAD
;
3642 new_type
= TEMP_VAL_CONST
;
3645 g_assert_not_reached();
3647 set_temp_val_nonreg(s
, ts
, new_type
);
3650 /* Mark a temporary as dead. */
3651 static inline void temp_dead(TCGContext
*s
, TCGTemp
*ts
)
3653 temp_free_or_dead(s
, ts
, 1);
3656 /* Sync a temporary to memory. 'allocated_regs' is used in case a temporary
3657 registers needs to be allocated to store a constant. If 'free_or_dead'
3658 is non-zero, subsequently release the temporary; if it is positive, the
3659 temp is dead; if it is negative, the temp is free. */
3660 static void temp_sync(TCGContext
*s
, TCGTemp
*ts
, TCGRegSet allocated_regs
,
3661 TCGRegSet preferred_regs
, int free_or_dead
)
3663 if (!temp_readonly(ts
) && !ts
->mem_coherent
) {
3664 if (!ts
->mem_allocated
) {
3665 temp_allocate_frame(s
, ts
);
3667 switch (ts
->val_type
) {
3668 case TEMP_VAL_CONST
:
3669 /* If we're going to free the temp immediately, then we won't
3670 require it later in a register, so attempt to store the
3671 constant to memory directly. */
3673 && tcg_out_sti(s
, ts
->type
, ts
->val
,
3674 ts
->mem_base
->reg
, ts
->mem_offset
)) {
3677 temp_load(s
, ts
, tcg_target_available_regs
[ts
->type
],
3678 allocated_regs
, preferred_regs
);
3682 tcg_out_st(s
, ts
->type
, ts
->reg
,
3683 ts
->mem_base
->reg
, ts
->mem_offset
);
3691 g_assert_not_reached();
3693 ts
->mem_coherent
= 1;
3696 temp_free_or_dead(s
, ts
, free_or_dead
);
3700 /* free register 'reg' by spilling the corresponding temporary if necessary */
3701 static void tcg_reg_free(TCGContext
*s
, TCGReg reg
, TCGRegSet allocated_regs
)
3703 TCGTemp
*ts
= s
->reg_to_temp
[reg
];
3705 temp_sync(s
, ts
, allocated_regs
, 0, -1);
3711 * @required_regs: Set of registers in which we must allocate.
3712 * @allocated_regs: Set of registers which must be avoided.
3713 * @preferred_regs: Set of registers we should prefer.
3714 * @rev: True if we search the registers in "indirect" order.
3716 * The allocated register must be in @required_regs & ~@allocated_regs,
3717 * but if we can put it in @preferred_regs we may save a move later.
3719 static TCGReg
tcg_reg_alloc(TCGContext
*s
, TCGRegSet required_regs
,
3720 TCGRegSet allocated_regs
,
3721 TCGRegSet preferred_regs
, bool rev
)
3723 int i
, j
, f
, n
= ARRAY_SIZE(tcg_target_reg_alloc_order
);
3724 TCGRegSet reg_ct
[2];
3727 reg_ct
[1] = required_regs
& ~allocated_regs
;
3728 tcg_debug_assert(reg_ct
[1] != 0);
3729 reg_ct
[0] = reg_ct
[1] & preferred_regs
;
3731 /* Skip the preferred_regs option if it cannot be satisfied,
3732 or if the preference made no difference. */
3733 f
= reg_ct
[0] == 0 || reg_ct
[0] == reg_ct
[1];
3735 order
= rev
? indirect_reg_alloc_order
: tcg_target_reg_alloc_order
;
3737 /* Try free registers, preferences first. */
3738 for (j
= f
; j
< 2; j
++) {
3739 TCGRegSet set
= reg_ct
[j
];
3741 if (tcg_regset_single(set
)) {
3742 /* One register in the set. */
3743 TCGReg reg
= tcg_regset_first(set
);
3744 if (s
->reg_to_temp
[reg
] == NULL
) {
3748 for (i
= 0; i
< n
; i
++) {
3749 TCGReg reg
= order
[i
];
3750 if (s
->reg_to_temp
[reg
] == NULL
&&
3751 tcg_regset_test_reg(set
, reg
)) {
3758 /* We must spill something. */
3759 for (j
= f
; j
< 2; j
++) {
3760 TCGRegSet set
= reg_ct
[j
];
3762 if (tcg_regset_single(set
)) {
3763 /* One register in the set. */
3764 TCGReg reg
= tcg_regset_first(set
);
3765 tcg_reg_free(s
, reg
, allocated_regs
);
3768 for (i
= 0; i
< n
; i
++) {
3769 TCGReg reg
= order
[i
];
3770 if (tcg_regset_test_reg(set
, reg
)) {
3771 tcg_reg_free(s
, reg
, allocated_regs
);
3778 g_assert_not_reached();
3781 static TCGReg
tcg_reg_alloc_pair(TCGContext
*s
, TCGRegSet required_regs
,
3782 TCGRegSet allocated_regs
,
3783 TCGRegSet preferred_regs
, bool rev
)
3785 int i
, j
, k
, fmin
, n
= ARRAY_SIZE(tcg_target_reg_alloc_order
);
3786 TCGRegSet reg_ct
[2];
3789 /* Ensure that if I is not in allocated_regs, I+1 is not either. */
3790 reg_ct
[1] = required_regs
& ~(allocated_regs
| (allocated_regs
>> 1));
3791 tcg_debug_assert(reg_ct
[1] != 0);
3792 reg_ct
[0] = reg_ct
[1] & preferred_regs
;
3794 order
= rev
? indirect_reg_alloc_order
: tcg_target_reg_alloc_order
;
3797 * Skip the preferred_regs option if it cannot be satisfied,
3798 * or if the preference made no difference.
3800 k
= reg_ct
[0] == 0 || reg_ct
[0] == reg_ct
[1];
3803 * Minimize the number of flushes by looking for 2 free registers first,
3804 * then a single flush, then two flushes.
3806 for (fmin
= 2; fmin
>= 0; fmin
--) {
3807 for (j
= k
; j
< 2; j
++) {
3808 TCGRegSet set
= reg_ct
[j
];
3810 for (i
= 0; i
< n
; i
++) {
3811 TCGReg reg
= order
[i
];
3813 if (tcg_regset_test_reg(set
, reg
)) {
3814 int f
= !s
->reg_to_temp
[reg
] + !s
->reg_to_temp
[reg
+ 1];
3816 tcg_reg_free(s
, reg
, allocated_regs
);
3817 tcg_reg_free(s
, reg
+ 1, allocated_regs
);
3824 g_assert_not_reached();
3827 /* Make sure the temporary is in a register. If needed, allocate the register
3828 from DESIRED while avoiding ALLOCATED. */
3829 static void temp_load(TCGContext
*s
, TCGTemp
*ts
, TCGRegSet desired_regs
,
3830 TCGRegSet allocated_regs
, TCGRegSet preferred_regs
)
3834 switch (ts
->val_type
) {
3837 case TEMP_VAL_CONST
:
3838 reg
= tcg_reg_alloc(s
, desired_regs
, allocated_regs
,
3839 preferred_regs
, ts
->indirect_base
);
3840 if (ts
->type
<= TCG_TYPE_I64
) {
3841 tcg_out_movi(s
, ts
->type
, reg
, ts
->val
);
3843 uint64_t val
= ts
->val
;
3847 * Find the minimal vector element that matches the constant.
3848 * The targets will, in general, have to do this search anyway,
3849 * do this generically.
3851 if (val
== dup_const(MO_8
, val
)) {
3853 } else if (val
== dup_const(MO_16
, val
)) {
3855 } else if (val
== dup_const(MO_32
, val
)) {
3859 tcg_out_dupi_vec(s
, ts
->type
, vece
, reg
, ts
->val
);
3861 ts
->mem_coherent
= 0;
3864 reg
= tcg_reg_alloc(s
, desired_regs
, allocated_regs
,
3865 preferred_regs
, ts
->indirect_base
);
3866 tcg_out_ld(s
, ts
->type
, reg
, ts
->mem_base
->reg
, ts
->mem_offset
);
3867 ts
->mem_coherent
= 1;
3871 g_assert_not_reached();
3873 set_temp_val_reg(s
, ts
, reg
);
3876 /* Save a temporary to memory. 'allocated_regs' is used in case a
3877 temporary registers needs to be allocated to store a constant. */
3878 static void temp_save(TCGContext
*s
, TCGTemp
*ts
, TCGRegSet allocated_regs
)
3880 /* The liveness analysis already ensures that globals are back
3881 in memory. Keep an tcg_debug_assert for safety. */
3882 tcg_debug_assert(ts
->val_type
== TEMP_VAL_MEM
|| temp_readonly(ts
));
3885 /* save globals to their canonical location and assume they can be
3886 modified be the following code. 'allocated_regs' is used in case a
3887 temporary registers needs to be allocated to store a constant. */
3888 static void save_globals(TCGContext
*s
, TCGRegSet allocated_regs
)
3892 for (i
= 0, n
= s
->nb_globals
; i
< n
; i
++) {
3893 temp_save(s
, &s
->temps
[i
], allocated_regs
);
3897 /* sync globals to their canonical location and assume they can be
3898 read by the following code. 'allocated_regs' is used in case a
3899 temporary registers needs to be allocated to store a constant. */
3900 static void sync_globals(TCGContext
*s
, TCGRegSet allocated_regs
)
3904 for (i
= 0, n
= s
->nb_globals
; i
< n
; i
++) {
3905 TCGTemp
*ts
= &s
->temps
[i
];
3906 tcg_debug_assert(ts
->val_type
!= TEMP_VAL_REG
3907 || ts
->kind
== TEMP_FIXED
3908 || ts
->mem_coherent
);
3912 /* at the end of a basic block, we assume all temporaries are dead and
3913 all globals are stored at their canonical location. */
3914 static void tcg_reg_alloc_bb_end(TCGContext
*s
, TCGRegSet allocated_regs
)
3918 for (i
= s
->nb_globals
; i
< s
->nb_temps
; i
++) {
3919 TCGTemp
*ts
= &s
->temps
[i
];
3923 temp_save(s
, ts
, allocated_regs
);
3926 /* The liveness analysis already ensures that temps are dead.
3927 Keep an tcg_debug_assert for safety. */
3928 tcg_debug_assert(ts
->val_type
== TEMP_VAL_DEAD
);
3931 /* Similarly, we should have freed any allocated register. */
3932 tcg_debug_assert(ts
->val_type
== TEMP_VAL_CONST
);
3935 g_assert_not_reached();
3939 save_globals(s
, allocated_regs
);
3943 * At a conditional branch, we assume all temporaries are dead unless
3944 * explicitly live-across-conditional-branch; all globals and local
3945 * temps are synced to their location.
3947 static void tcg_reg_alloc_cbranch(TCGContext
*s
, TCGRegSet allocated_regs
)
3949 sync_globals(s
, allocated_regs
);
3951 for (int i
= s
->nb_globals
; i
< s
->nb_temps
; i
++) {
3952 TCGTemp
*ts
= &s
->temps
[i
];
3954 * The liveness analysis already ensures that temps are dead.
3955 * Keep tcg_debug_asserts for safety.
3959 tcg_debug_assert(ts
->val_type
!= TEMP_VAL_REG
|| ts
->mem_coherent
);
3965 g_assert_not_reached();
3971 * Specialized code generation for INDEX_op_mov_* with a constant.
3973 static void tcg_reg_alloc_do_movi(TCGContext
*s
, TCGTemp
*ots
,
3974 tcg_target_ulong val
, TCGLifeData arg_life
,
3975 TCGRegSet preferred_regs
)
3977 /* ENV should not be modified. */
3978 tcg_debug_assert(!temp_readonly(ots
));
3980 /* The movi is not explicitly generated here. */
3981 set_temp_val_nonreg(s
, ots
, TEMP_VAL_CONST
);
3983 ots
->mem_coherent
= 0;
3984 if (NEED_SYNC_ARG(0)) {
3985 temp_sync(s
, ots
, s
->reserved_regs
, preferred_regs
, IS_DEAD_ARG(0));
3986 } else if (IS_DEAD_ARG(0)) {
3992 * Specialized code generation for INDEX_op_mov_*.
3994 static void tcg_reg_alloc_mov(TCGContext
*s
, const TCGOp
*op
)
3996 const TCGLifeData arg_life
= op
->life
;
3997 TCGRegSet allocated_regs
, preferred_regs
;
3999 TCGType otype
, itype
;
4002 allocated_regs
= s
->reserved_regs
;
4003 preferred_regs
= output_pref(op
, 0);
4004 ots
= arg_temp(op
->args
[0]);
4005 ts
= arg_temp(op
->args
[1]);
4007 /* ENV should not be modified. */
4008 tcg_debug_assert(!temp_readonly(ots
));
4010 /* Note that otype != itype for no-op truncation. */
4014 if (ts
->val_type
== TEMP_VAL_CONST
) {
4015 /* propagate constant or generate sti */
4016 tcg_target_ulong val
= ts
->val
;
4017 if (IS_DEAD_ARG(1)) {
4020 tcg_reg_alloc_do_movi(s
, ots
, val
, arg_life
, preferred_regs
);
4024 /* If the source value is in memory we're going to be forced
4025 to have it in a register in order to perform the copy. Copy
4026 the SOURCE value into its own register first, that way we
4027 don't have to reload SOURCE the next time it is used. */
4028 if (ts
->val_type
== TEMP_VAL_MEM
) {
4029 temp_load(s
, ts
, tcg_target_available_regs
[itype
],
4030 allocated_regs
, preferred_regs
);
4032 tcg_debug_assert(ts
->val_type
== TEMP_VAL_REG
);
4035 if (IS_DEAD_ARG(0)) {
4036 /* mov to a non-saved dead register makes no sense (even with
4037 liveness analysis disabled). */
4038 tcg_debug_assert(NEED_SYNC_ARG(0));
4039 if (!ots
->mem_allocated
) {
4040 temp_allocate_frame(s
, ots
);
4042 tcg_out_st(s
, otype
, ireg
, ots
->mem_base
->reg
, ots
->mem_offset
);
4043 if (IS_DEAD_ARG(1)) {
4050 if (IS_DEAD_ARG(1) && ts
->kind
!= TEMP_FIXED
) {
4052 * The mov can be suppressed. Kill input first, so that it
4053 * is unlinked from reg_to_temp, then set the output to the
4054 * reg that we saved from the input.
4059 if (ots
->val_type
== TEMP_VAL_REG
) {
4062 /* Make sure to not spill the input register during allocation. */
4063 oreg
= tcg_reg_alloc(s
, tcg_target_available_regs
[otype
],
4064 allocated_regs
| ((TCGRegSet
)1 << ireg
),
4065 preferred_regs
, ots
->indirect_base
);
4067 if (!tcg_out_mov(s
, otype
, oreg
, ireg
)) {
4069 * Cross register class move not supported.
4070 * Store the source register into the destination slot
4071 * and leave the destination temp as TEMP_VAL_MEM.
4073 assert(!temp_readonly(ots
));
4074 if (!ts
->mem_allocated
) {
4075 temp_allocate_frame(s
, ots
);
4077 tcg_out_st(s
, ts
->type
, ireg
, ots
->mem_base
->reg
, ots
->mem_offset
);
4078 set_temp_val_nonreg(s
, ts
, TEMP_VAL_MEM
);
4079 ots
->mem_coherent
= 1;
4083 set_temp_val_reg(s
, ots
, oreg
);
4084 ots
->mem_coherent
= 0;
4086 if (NEED_SYNC_ARG(0)) {
4087 temp_sync(s
, ots
, allocated_regs
, 0, 0);
4092 * Specialized code generation for INDEX_op_dup_vec.
4094 static void tcg_reg_alloc_dup(TCGContext
*s
, const TCGOp
*op
)
4096 const TCGLifeData arg_life
= op
->life
;
4097 TCGRegSet dup_out_regs
, dup_in_regs
;
4099 TCGType itype
, vtype
;
4104 ots
= arg_temp(op
->args
[0]);
4105 its
= arg_temp(op
->args
[1]);
4107 /* ENV should not be modified. */
4108 tcg_debug_assert(!temp_readonly(ots
));
4111 vece
= TCGOP_VECE(op
);
4112 vtype
= TCGOP_VECL(op
) + TCG_TYPE_V64
;
4114 if (its
->val_type
== TEMP_VAL_CONST
) {
4115 /* Propagate constant via movi -> dupi. */
4116 tcg_target_ulong val
= its
->val
;
4117 if (IS_DEAD_ARG(1)) {
4120 tcg_reg_alloc_do_movi(s
, ots
, val
, arg_life
, output_pref(op
, 0));
4124 dup_out_regs
= tcg_op_defs
[INDEX_op_dup_vec
].args_ct
[0].regs
;
4125 dup_in_regs
= tcg_op_defs
[INDEX_op_dup_vec
].args_ct
[1].regs
;
4127 /* Allocate the output register now. */
4128 if (ots
->val_type
!= TEMP_VAL_REG
) {
4129 TCGRegSet allocated_regs
= s
->reserved_regs
;
4132 if (!IS_DEAD_ARG(1) && its
->val_type
== TEMP_VAL_REG
) {
4133 /* Make sure to not spill the input register. */
4134 tcg_regset_set_reg(allocated_regs
, its
->reg
);
4136 oreg
= tcg_reg_alloc(s
, dup_out_regs
, allocated_regs
,
4137 output_pref(op
, 0), ots
->indirect_base
);
4138 set_temp_val_reg(s
, ots
, oreg
);
4141 switch (its
->val_type
) {
4144 * The dup constriaints must be broad, covering all possible VECE.
4145 * However, tcg_op_dup_vec() gets to see the VECE and we allow it
4146 * to fail, indicating that extra moves are required for that case.
4148 if (tcg_regset_test_reg(dup_in_regs
, its
->reg
)) {
4149 if (tcg_out_dup_vec(s
, vtype
, vece
, ots
->reg
, its
->reg
)) {
4152 /* Try again from memory or a vector input register. */
4154 if (!its
->mem_coherent
) {
4156 * The input register is not synced, and so an extra store
4157 * would be required to use memory. Attempt an integer-vector
4158 * register move first. We do not have a TCGRegSet for this.
4160 if (tcg_out_mov(s
, itype
, ots
->reg
, its
->reg
)) {
4163 /* Sync the temp back to its slot and load from there. */
4164 temp_sync(s
, its
, s
->reserved_regs
, 0, 0);
4170 if (HOST_BIG_ENDIAN
) {
4171 lowpart_ofs
= tcg_type_size(itype
) - (1 << vece
);
4173 if (tcg_out_dupm_vec(s
, vtype
, vece
, ots
->reg
, its
->mem_base
->reg
,
4174 its
->mem_offset
+ lowpart_ofs
)) {
4177 /* Load the input into the destination vector register. */
4178 tcg_out_ld(s
, itype
, ots
->reg
, its
->mem_base
->reg
, its
->mem_offset
);
4182 g_assert_not_reached();
4185 /* We now have a vector input register, so dup must succeed. */
4186 ok
= tcg_out_dup_vec(s
, vtype
, vece
, ots
->reg
, ots
->reg
);
4187 tcg_debug_assert(ok
);
4190 ots
->mem_coherent
= 0;
4191 if (IS_DEAD_ARG(1)) {
4194 if (NEED_SYNC_ARG(0)) {
4195 temp_sync(s
, ots
, s
->reserved_regs
, 0, 0);
4197 if (IS_DEAD_ARG(0)) {
4202 static void tcg_reg_alloc_op(TCGContext
*s
, const TCGOp
*op
)
4204 const TCGLifeData arg_life
= op
->life
;
4205 const TCGOpDef
* const def
= &tcg_op_defs
[op
->opc
];
4206 TCGRegSet i_allocated_regs
;
4207 TCGRegSet o_allocated_regs
;
4208 int i
, k
, nb_iargs
, nb_oargs
;
4211 const TCGArgConstraint
*arg_ct
;
4213 TCGArg new_args
[TCG_MAX_OP_ARGS
];
4214 int const_args
[TCG_MAX_OP_ARGS
];
4216 nb_oargs
= def
->nb_oargs
;
4217 nb_iargs
= def
->nb_iargs
;
4219 /* copy constants */
4220 memcpy(new_args
+ nb_oargs
+ nb_iargs
,
4221 op
->args
+ nb_oargs
+ nb_iargs
,
4222 sizeof(TCGArg
) * def
->nb_cargs
);
4224 i_allocated_regs
= s
->reserved_regs
;
4225 o_allocated_regs
= s
->reserved_regs
;
4227 /* satisfy input constraints */
4228 for (k
= 0; k
< nb_iargs
; k
++) {
4229 TCGRegSet i_preferred_regs
, i_required_regs
;
4230 bool allocate_new_reg
, copyto_new_reg
;
4234 i
= def
->args_ct
[nb_oargs
+ k
].sort_index
;
4236 arg_ct
= &def
->args_ct
[i
];
4239 if (ts
->val_type
== TEMP_VAL_CONST
4240 && tcg_target_const_match(ts
->val
, ts
->type
, arg_ct
->ct
)) {
4241 /* constant is OK for instruction */
4243 new_args
[i
] = ts
->val
;
4248 i_preferred_regs
= 0;
4249 i_required_regs
= arg_ct
->regs
;
4250 allocate_new_reg
= false;
4251 copyto_new_reg
= false;
4253 switch (arg_ct
->pair
) {
4254 case 0: /* not paired */
4255 if (arg_ct
->ialias
) {
4256 i_preferred_regs
= output_pref(op
, arg_ct
->alias_index
);
4259 * If the input is readonly, then it cannot also be an
4260 * output and aliased to itself. If the input is not
4261 * dead after the instruction, we must allocate a new
4262 * register and move it.
4264 if (temp_readonly(ts
) || !IS_DEAD_ARG(i
)) {
4265 allocate_new_reg
= true;
4266 } else if (ts
->val_type
== TEMP_VAL_REG
) {
4268 * Check if the current register has already been
4269 * allocated for another input.
4272 tcg_regset_test_reg(i_allocated_regs
, reg
);
4275 if (!allocate_new_reg
) {
4276 temp_load(s
, ts
, i_required_regs
, i_allocated_regs
,
4279 allocate_new_reg
= !tcg_regset_test_reg(i_required_regs
, reg
);
4281 if (allocate_new_reg
) {
4283 * Allocate a new register matching the constraint
4284 * and move the temporary register into it.
4286 temp_load(s
, ts
, tcg_target_available_regs
[ts
->type
],
4287 i_allocated_regs
, 0);
4288 reg
= tcg_reg_alloc(s
, i_required_regs
, i_allocated_regs
,
4289 i_preferred_regs
, ts
->indirect_base
);
4290 copyto_new_reg
= true;
4295 /* First of an input pair; if i1 == i2, the second is an output. */
4297 i2
= arg_ct
->pair_index
;
4298 ts2
= i1
!= i2
? arg_temp(op
->args
[i2
]) : NULL
;
4301 * It is easier to default to allocating a new pair
4302 * and to identify a few cases where it's not required.
4304 if (arg_ct
->ialias
) {
4305 i_preferred_regs
= output_pref(op
, arg_ct
->alias_index
);
4306 if (IS_DEAD_ARG(i1
) &&
4308 !temp_readonly(ts
) &&
4309 ts
->val_type
== TEMP_VAL_REG
&&
4310 ts
->reg
< TCG_TARGET_NB_REGS
- 1 &&
4311 tcg_regset_test_reg(i_required_regs
, reg
) &&
4312 !tcg_regset_test_reg(i_allocated_regs
, reg
) &&
4313 !tcg_regset_test_reg(i_allocated_regs
, reg
+ 1) &&
4315 ? ts2
->val_type
== TEMP_VAL_REG
&&
4316 ts2
->reg
== reg
+ 1 &&
4318 : s
->reg_to_temp
[reg
+ 1] == NULL
)) {
4322 /* Without aliasing, the pair must also be an input. */
4323 tcg_debug_assert(ts2
);
4324 if (ts
->val_type
== TEMP_VAL_REG
&&
4325 ts2
->val_type
== TEMP_VAL_REG
&&
4326 ts2
->reg
== reg
+ 1 &&
4327 tcg_regset_test_reg(i_required_regs
, reg
)) {
4331 reg
= tcg_reg_alloc_pair(s
, i_required_regs
, i_allocated_regs
,
4332 0, ts
->indirect_base
);
4335 case 2: /* pair second */
4336 reg
= new_args
[arg_ct
->pair_index
] + 1;
4339 case 3: /* ialias with second output, no first input */
4340 tcg_debug_assert(arg_ct
->ialias
);
4341 i_preferred_regs
= output_pref(op
, arg_ct
->alias_index
);
4343 if (IS_DEAD_ARG(i
) &&
4344 !temp_readonly(ts
) &&
4345 ts
->val_type
== TEMP_VAL_REG
&&
4347 s
->reg_to_temp
[reg
- 1] == NULL
&&
4348 tcg_regset_test_reg(i_required_regs
, reg
) &&
4349 !tcg_regset_test_reg(i_allocated_regs
, reg
) &&
4350 !tcg_regset_test_reg(i_allocated_regs
, reg
- 1)) {
4351 tcg_regset_set_reg(i_allocated_regs
, reg
- 1);
4354 reg
= tcg_reg_alloc_pair(s
, i_required_regs
>> 1,
4355 i_allocated_regs
, 0,
4357 tcg_regset_set_reg(i_allocated_regs
, reg
);
4363 * If an aliased input is not dead after the instruction,
4364 * we must allocate a new register and move it.
4366 if (arg_ct
->ialias
&& (!IS_DEAD_ARG(i
) || temp_readonly(ts
))) {
4367 TCGRegSet t_allocated_regs
= i_allocated_regs
;
4370 * Because of the alias, and the continued life, make sure
4371 * that the temp is somewhere *other* than the reg pair,
4372 * and we get a copy in reg.
4374 tcg_regset_set_reg(t_allocated_regs
, reg
);
4375 tcg_regset_set_reg(t_allocated_regs
, reg
+ 1);
4376 if (ts
->val_type
== TEMP_VAL_REG
&& ts
->reg
== reg
) {
4377 /* If ts was already in reg, copy it somewhere else. */
4381 tcg_debug_assert(ts
->kind
!= TEMP_FIXED
);
4382 nr
= tcg_reg_alloc(s
, tcg_target_available_regs
[ts
->type
],
4383 t_allocated_regs
, 0, ts
->indirect_base
);
4384 ok
= tcg_out_mov(s
, ts
->type
, nr
, reg
);
4385 tcg_debug_assert(ok
);
4387 set_temp_val_reg(s
, ts
, nr
);
4389 temp_load(s
, ts
, tcg_target_available_regs
[ts
->type
],
4390 t_allocated_regs
, 0);
4391 copyto_new_reg
= true;
4394 /* Preferably allocate to reg, otherwise copy. */
4395 i_required_regs
= (TCGRegSet
)1 << reg
;
4396 temp_load(s
, ts
, i_required_regs
, i_allocated_regs
,
4398 copyto_new_reg
= ts
->reg
!= reg
;
4403 g_assert_not_reached();
4406 if (copyto_new_reg
) {
4407 if (!tcg_out_mov(s
, ts
->type
, reg
, ts
->reg
)) {
4409 * Cross register class move not supported. Sync the
4410 * temp back to its slot and load from there.
4412 temp_sync(s
, ts
, i_allocated_regs
, 0, 0);
4413 tcg_out_ld(s
, ts
->type
, reg
,
4414 ts
->mem_base
->reg
, ts
->mem_offset
);
4419 tcg_regset_set_reg(i_allocated_regs
, reg
);
4422 /* mark dead temporaries and free the associated registers */
4423 for (i
= nb_oargs
; i
< nb_oargs
+ nb_iargs
; i
++) {
4424 if (IS_DEAD_ARG(i
)) {
4425 temp_dead(s
, arg_temp(op
->args
[i
]));
4429 if (def
->flags
& TCG_OPF_COND_BRANCH
) {
4430 tcg_reg_alloc_cbranch(s
, i_allocated_regs
);
4431 } else if (def
->flags
& TCG_OPF_BB_END
) {
4432 tcg_reg_alloc_bb_end(s
, i_allocated_regs
);
4434 if (def
->flags
& TCG_OPF_CALL_CLOBBER
) {
4435 /* XXX: permit generic clobber register list ? */
4436 for (i
= 0; i
< TCG_TARGET_NB_REGS
; i
++) {
4437 if (tcg_regset_test_reg(tcg_target_call_clobber_regs
, i
)) {
4438 tcg_reg_free(s
, i
, i_allocated_regs
);
4442 if (def
->flags
& TCG_OPF_SIDE_EFFECTS
) {
4443 /* sync globals if the op has side effects and might trigger
4445 sync_globals(s
, i_allocated_regs
);
4448 /* satisfy the output constraints */
4449 for(k
= 0; k
< nb_oargs
; k
++) {
4450 i
= def
->args_ct
[k
].sort_index
;
4452 arg_ct
= &def
->args_ct
[i
];
4455 /* ENV should not be modified. */
4456 tcg_debug_assert(!temp_readonly(ts
));
4458 switch (arg_ct
->pair
) {
4459 case 0: /* not paired */
4460 if (arg_ct
->oalias
&& !const_args
[arg_ct
->alias_index
]) {
4461 reg
= new_args
[arg_ct
->alias_index
];
4462 } else if (arg_ct
->newreg
) {
4463 reg
= tcg_reg_alloc(s
, arg_ct
->regs
,
4464 i_allocated_regs
| o_allocated_regs
,
4465 output_pref(op
, k
), ts
->indirect_base
);
4467 reg
= tcg_reg_alloc(s
, arg_ct
->regs
, o_allocated_regs
,
4468 output_pref(op
, k
), ts
->indirect_base
);
4472 case 1: /* first of pair */
4473 tcg_debug_assert(!arg_ct
->newreg
);
4474 if (arg_ct
->oalias
) {
4475 reg
= new_args
[arg_ct
->alias_index
];
4478 reg
= tcg_reg_alloc_pair(s
, arg_ct
->regs
, o_allocated_regs
,
4479 output_pref(op
, k
), ts
->indirect_base
);
4482 case 2: /* second of pair */
4483 tcg_debug_assert(!arg_ct
->newreg
);
4484 if (arg_ct
->oalias
) {
4485 reg
= new_args
[arg_ct
->alias_index
];
4487 reg
= new_args
[arg_ct
->pair_index
] + 1;
4491 case 3: /* first of pair, aliasing with a second input */
4492 tcg_debug_assert(!arg_ct
->newreg
);
4493 reg
= new_args
[arg_ct
->pair_index
] - 1;
4497 g_assert_not_reached();
4499 tcg_regset_set_reg(o_allocated_regs
, reg
);
4500 set_temp_val_reg(s
, ts
, reg
);
4501 ts
->mem_coherent
= 0;
4506 /* emit instruction */
4508 case INDEX_op_ext8s_i32
:
4509 tcg_out_ext8s(s
, TCG_TYPE_I32
, new_args
[0], new_args
[1]);
4511 case INDEX_op_ext8s_i64
:
4512 tcg_out_ext8s(s
, TCG_TYPE_I64
, new_args
[0], new_args
[1]);
4514 case INDEX_op_ext8u_i32
:
4515 case INDEX_op_ext8u_i64
:
4516 tcg_out_ext8u(s
, new_args
[0], new_args
[1]);
4518 case INDEX_op_ext16s_i32
:
4519 tcg_out_ext16s(s
, TCG_TYPE_I32
, new_args
[0], new_args
[1]);
4521 case INDEX_op_ext16s_i64
:
4522 tcg_out_ext16s(s
, TCG_TYPE_I64
, new_args
[0], new_args
[1]);
4524 case INDEX_op_ext16u_i32
:
4525 case INDEX_op_ext16u_i64
:
4526 tcg_out_ext16u(s
, new_args
[0], new_args
[1]);
4528 case INDEX_op_ext32s_i64
:
4529 tcg_out_ext32s(s
, new_args
[0], new_args
[1]);
4531 case INDEX_op_ext32u_i64
:
4532 tcg_out_ext32u(s
, new_args
[0], new_args
[1]);
4534 case INDEX_op_ext_i32_i64
:
4535 tcg_out_exts_i32_i64(s
, new_args
[0], new_args
[1]);
4537 case INDEX_op_extu_i32_i64
:
4538 tcg_out_extu_i32_i64(s
, new_args
[0], new_args
[1]);
4541 if (def
->flags
& TCG_OPF_VECTOR
) {
4542 tcg_out_vec_op(s
, op
->opc
, TCGOP_VECL(op
), TCGOP_VECE(op
),
4543 new_args
, const_args
);
4545 tcg_out_op(s
, op
->opc
, new_args
, const_args
);
4550 /* move the outputs in the correct register if needed */
4551 for(i
= 0; i
< nb_oargs
; i
++) {
4552 ts
= arg_temp(op
->args
[i
]);
4554 /* ENV should not be modified. */
4555 tcg_debug_assert(!temp_readonly(ts
));
4557 if (NEED_SYNC_ARG(i
)) {
4558 temp_sync(s
, ts
, o_allocated_regs
, 0, IS_DEAD_ARG(i
));
4559 } else if (IS_DEAD_ARG(i
)) {
4565 static bool tcg_reg_alloc_dup2(TCGContext
*s
, const TCGOp
*op
)
4567 const TCGLifeData arg_life
= op
->life
;
4568 TCGTemp
*ots
, *itsl
, *itsh
;
4569 TCGType vtype
= TCGOP_VECL(op
) + TCG_TYPE_V64
;
4571 /* This opcode is only valid for 32-bit hosts, for 64-bit elements. */
4572 tcg_debug_assert(TCG_TARGET_REG_BITS
== 32);
4573 tcg_debug_assert(TCGOP_VECE(op
) == MO_64
);
4575 ots
= arg_temp(op
->args
[0]);
4576 itsl
= arg_temp(op
->args
[1]);
4577 itsh
= arg_temp(op
->args
[2]);
4579 /* ENV should not be modified. */
4580 tcg_debug_assert(!temp_readonly(ots
));
4582 /* Allocate the output register now. */
4583 if (ots
->val_type
!= TEMP_VAL_REG
) {
4584 TCGRegSet allocated_regs
= s
->reserved_regs
;
4585 TCGRegSet dup_out_regs
=
4586 tcg_op_defs
[INDEX_op_dup_vec
].args_ct
[0].regs
;
4589 /* Make sure to not spill the input registers. */
4590 if (!IS_DEAD_ARG(1) && itsl
->val_type
== TEMP_VAL_REG
) {
4591 tcg_regset_set_reg(allocated_regs
, itsl
->reg
);
4593 if (!IS_DEAD_ARG(2) && itsh
->val_type
== TEMP_VAL_REG
) {
4594 tcg_regset_set_reg(allocated_regs
, itsh
->reg
);
4597 oreg
= tcg_reg_alloc(s
, dup_out_regs
, allocated_regs
,
4598 output_pref(op
, 0), ots
->indirect_base
);
4599 set_temp_val_reg(s
, ots
, oreg
);
4602 /* Promote dup2 of immediates to dupi_vec. */
4603 if (itsl
->val_type
== TEMP_VAL_CONST
&& itsh
->val_type
== TEMP_VAL_CONST
) {
4604 uint64_t val
= deposit64(itsl
->val
, 32, 32, itsh
->val
);
4607 if (val
== dup_const(MO_8
, val
)) {
4609 } else if (val
== dup_const(MO_16
, val
)) {
4611 } else if (val
== dup_const(MO_32
, val
)) {
4615 tcg_out_dupi_vec(s
, vtype
, vece
, ots
->reg
, val
);
4619 /* If the two inputs form one 64-bit value, try dupm_vec. */
4620 if (itsl
->temp_subindex
== HOST_BIG_ENDIAN
&&
4621 itsh
->temp_subindex
== !HOST_BIG_ENDIAN
&&
4622 itsl
== itsh
+ (HOST_BIG_ENDIAN
? 1 : -1)) {
4623 TCGTemp
*its
= itsl
- HOST_BIG_ENDIAN
;
4625 temp_sync(s
, its
+ 0, s
->reserved_regs
, 0, 0);
4626 temp_sync(s
, its
+ 1, s
->reserved_regs
, 0, 0);
4628 if (tcg_out_dupm_vec(s
, vtype
, MO_64
, ots
->reg
,
4629 its
->mem_base
->reg
, its
->mem_offset
)) {
4634 /* Fall back to generic expansion. */
4638 ots
->mem_coherent
= 0;
4639 if (IS_DEAD_ARG(1)) {
4642 if (IS_DEAD_ARG(2)) {
4645 if (NEED_SYNC_ARG(0)) {
4646 temp_sync(s
, ots
, s
->reserved_regs
, 0, IS_DEAD_ARG(0));
4647 } else if (IS_DEAD_ARG(0)) {
4653 static void load_arg_reg(TCGContext
*s
, TCGReg reg
, TCGTemp
*ts
,
4654 TCGRegSet allocated_regs
)
4656 if (ts
->val_type
== TEMP_VAL_REG
) {
4657 if (ts
->reg
!= reg
) {
4658 tcg_reg_free(s
, reg
, allocated_regs
);
4659 if (!tcg_out_mov(s
, ts
->type
, reg
, ts
->reg
)) {
4661 * Cross register class move not supported. Sync the
4662 * temp back to its slot and load from there.
4664 temp_sync(s
, ts
, allocated_regs
, 0, 0);
4665 tcg_out_ld(s
, ts
->type
, reg
,
4666 ts
->mem_base
->reg
, ts
->mem_offset
);
4670 TCGRegSet arg_set
= 0;
4672 tcg_reg_free(s
, reg
, allocated_regs
);
4673 tcg_regset_set_reg(arg_set
, reg
);
4674 temp_load(s
, ts
, arg_set
, allocated_regs
, 0);
4678 static void load_arg_stk(TCGContext
*s
, int stk_slot
, TCGTemp
*ts
,
4679 TCGRegSet allocated_regs
)
4682 * When the destination is on the stack, load up the temp and store.
4683 * If there are many call-saved registers, the temp might live to
4684 * see another use; otherwise it'll be discarded.
4686 temp_load(s
, ts
, tcg_target_available_regs
[ts
->type
], allocated_regs
, 0);
4687 tcg_out_st(s
, ts
->type
, ts
->reg
, TCG_REG_CALL_STACK
,
4688 TCG_TARGET_CALL_STACK_OFFSET
+
4689 stk_slot
* sizeof(tcg_target_long
));
4692 static void load_arg_normal(TCGContext
*s
, const TCGCallArgumentLoc
*l
,
4693 TCGTemp
*ts
, TCGRegSet
*allocated_regs
)
4696 TCGReg reg
= tcg_target_call_iarg_regs
[l
->arg_slot
];
4697 load_arg_reg(s
, reg
, ts
, *allocated_regs
);
4698 tcg_regset_set_reg(*allocated_regs
, reg
);
4700 load_arg_stk(s
, l
->arg_slot
- ARRAY_SIZE(tcg_target_call_iarg_regs
),
4701 ts
, *allocated_regs
);
4705 static void load_arg_ref(TCGContext
*s
, int arg_slot
, TCGReg ref_base
,
4706 intptr_t ref_off
, TCGRegSet
*allocated_regs
)
4709 int stk_slot
= arg_slot
- ARRAY_SIZE(tcg_target_call_iarg_regs
);
4712 reg
= tcg_target_call_iarg_regs
[arg_slot
];
4713 tcg_reg_free(s
, reg
, *allocated_regs
);
4714 tcg_out_addi_ptr(s
, reg
, ref_base
, ref_off
);
4715 tcg_regset_set_reg(*allocated_regs
, reg
);
4717 reg
= tcg_reg_alloc(s
, tcg_target_available_regs
[TCG_TYPE_PTR
],
4718 *allocated_regs
, 0, false);
4719 tcg_out_addi_ptr(s
, reg
, ref_base
, ref_off
);
4720 tcg_out_st(s
, TCG_TYPE_PTR
, reg
, TCG_REG_CALL_STACK
,
4721 TCG_TARGET_CALL_STACK_OFFSET
4722 + stk_slot
* sizeof(tcg_target_long
));
4726 static void tcg_reg_alloc_call(TCGContext
*s
, TCGOp
*op
)
4728 const int nb_oargs
= TCGOP_CALLO(op
);
4729 const int nb_iargs
= TCGOP_CALLI(op
);
4730 const TCGLifeData arg_life
= op
->life
;
4731 const TCGHelperInfo
*info
= tcg_call_info(op
);
4732 TCGRegSet allocated_regs
= s
->reserved_regs
;
4736 * Move inputs into place in reverse order,
4737 * so that we place stacked arguments first.
4739 for (i
= nb_iargs
- 1; i
>= 0; --i
) {
4740 const TCGCallArgumentLoc
*loc
= &info
->in
[i
];
4741 TCGTemp
*ts
= arg_temp(op
->args
[nb_oargs
+ i
]);
4743 switch (loc
->kind
) {
4744 case TCG_CALL_ARG_NORMAL
:
4745 case TCG_CALL_ARG_EXTEND_U
:
4746 case TCG_CALL_ARG_EXTEND_S
:
4747 load_arg_normal(s
, loc
, ts
, &allocated_regs
);
4749 case TCG_CALL_ARG_BY_REF
:
4750 load_arg_stk(s
, loc
->ref_slot
, ts
, allocated_regs
);
4751 load_arg_ref(s
, loc
->arg_slot
, TCG_REG_CALL_STACK
,
4752 TCG_TARGET_CALL_STACK_OFFSET
4753 + loc
->ref_slot
* sizeof(tcg_target_long
),
4756 case TCG_CALL_ARG_BY_REF_N
:
4757 load_arg_stk(s
, loc
->ref_slot
, ts
, allocated_regs
);
4760 g_assert_not_reached();
4764 /* Mark dead temporaries and free the associated registers. */
4765 for (i
= nb_oargs
; i
< nb_iargs
+ nb_oargs
; i
++) {
4766 if (IS_DEAD_ARG(i
)) {
4767 temp_dead(s
, arg_temp(op
->args
[i
]));
4771 /* Clobber call registers. */
4772 for (i
= 0; i
< TCG_TARGET_NB_REGS
; i
++) {
4773 if (tcg_regset_test_reg(tcg_target_call_clobber_regs
, i
)) {
4774 tcg_reg_free(s
, i
, allocated_regs
);
4779 * Save globals if they might be written by the helper,
4780 * sync them if they might be read.
4782 if (info
->flags
& TCG_CALL_NO_READ_GLOBALS
) {
4784 } else if (info
->flags
& TCG_CALL_NO_WRITE_GLOBALS
) {
4785 sync_globals(s
, allocated_regs
);
4787 save_globals(s
, allocated_regs
);
4791 * If the ABI passes a pointer to the returned struct as the first
4792 * argument, load that now. Pass a pointer to the output home slot.
4794 if (info
->out_kind
== TCG_CALL_RET_BY_REF
) {
4795 TCGTemp
*ts
= arg_temp(op
->args
[0]);
4797 if (!ts
->mem_allocated
) {
4798 temp_allocate_frame(s
, ts
);
4800 load_arg_ref(s
, 0, ts
->mem_base
->reg
, ts
->mem_offset
, &allocated_regs
);
4803 tcg_out_call(s
, tcg_call_func(op
), info
);
4805 /* Assign output registers and emit moves if needed. */
4806 switch (info
->out_kind
) {
4807 case TCG_CALL_RET_NORMAL
:
4808 for (i
= 0; i
< nb_oargs
; i
++) {
4809 TCGTemp
*ts
= arg_temp(op
->args
[i
]);
4810 TCGReg reg
= tcg_target_call_oarg_reg(TCG_CALL_RET_NORMAL
, i
);
4812 /* ENV should not be modified. */
4813 tcg_debug_assert(!temp_readonly(ts
));
4815 set_temp_val_reg(s
, ts
, reg
);
4816 ts
->mem_coherent
= 0;
4820 case TCG_CALL_RET_BY_VEC
:
4822 TCGTemp
*ts
= arg_temp(op
->args
[0]);
4824 tcg_debug_assert(ts
->base_type
== TCG_TYPE_I128
);
4825 tcg_debug_assert(ts
->temp_subindex
== 0);
4826 if (!ts
->mem_allocated
) {
4827 temp_allocate_frame(s
, ts
);
4829 tcg_out_st(s
, TCG_TYPE_V128
,
4830 tcg_target_call_oarg_reg(TCG_CALL_RET_BY_VEC
, 0),
4831 ts
->mem_base
->reg
, ts
->mem_offset
);
4833 /* fall through to mark all parts in memory */
4835 case TCG_CALL_RET_BY_REF
:
4836 /* The callee has performed a write through the reference. */
4837 for (i
= 0; i
< nb_oargs
; i
++) {
4838 TCGTemp
*ts
= arg_temp(op
->args
[i
]);
4839 ts
->val_type
= TEMP_VAL_MEM
;
4844 g_assert_not_reached();
4847 /* Flush or discard output registers as needed. */
4848 for (i
= 0; i
< nb_oargs
; i
++) {
4849 TCGTemp
*ts
= arg_temp(op
->args
[i
]);
4850 if (NEED_SYNC_ARG(i
)) {
4851 temp_sync(s
, ts
, s
->reserved_regs
, 0, IS_DEAD_ARG(i
));
4852 } else if (IS_DEAD_ARG(i
)) {
4858 #ifdef CONFIG_PROFILER
4860 /* avoid copy/paste errors */
4861 #define PROF_ADD(to, from, field) \
4863 (to)->field += qatomic_read(&((from)->field)); \
4866 #define PROF_MAX(to, from, field) \
4868 typeof((from)->field) val__ = qatomic_read(&((from)->field)); \
4869 if (val__ > (to)->field) { \
4870 (to)->field = val__; \
4874 /* Pass in a zero'ed @prof */
4876 void tcg_profile_snapshot(TCGProfile
*prof
, bool counters
, bool table
)
4878 unsigned int n_ctxs
= qatomic_read(&tcg_cur_ctxs
);
4881 for (i
= 0; i
< n_ctxs
; i
++) {
4882 TCGContext
*s
= qatomic_read(&tcg_ctxs
[i
]);
4883 const TCGProfile
*orig
= &s
->prof
;
4886 PROF_ADD(prof
, orig
, cpu_exec_time
);
4887 PROF_ADD(prof
, orig
, tb_count1
);
4888 PROF_ADD(prof
, orig
, tb_count
);
4889 PROF_ADD(prof
, orig
, op_count
);
4890 PROF_MAX(prof
, orig
, op_count_max
);
4891 PROF_ADD(prof
, orig
, temp_count
);
4892 PROF_MAX(prof
, orig
, temp_count_max
);
4893 PROF_ADD(prof
, orig
, del_op_count
);
4894 PROF_ADD(prof
, orig
, code_in_len
);
4895 PROF_ADD(prof
, orig
, code_out_len
);
4896 PROF_ADD(prof
, orig
, search_out_len
);
4897 PROF_ADD(prof
, orig
, interm_time
);
4898 PROF_ADD(prof
, orig
, code_time
);
4899 PROF_ADD(prof
, orig
, la_time
);
4900 PROF_ADD(prof
, orig
, opt_time
);
4901 PROF_ADD(prof
, orig
, restore_count
);
4902 PROF_ADD(prof
, orig
, restore_time
);
4907 for (i
= 0; i
< NB_OPS
; i
++) {
4908 PROF_ADD(prof
, orig
, table_op_count
[i
]);
4917 static void tcg_profile_snapshot_counters(TCGProfile
*prof
)
4919 tcg_profile_snapshot(prof
, true, false);
4922 static void tcg_profile_snapshot_table(TCGProfile
*prof
)
4924 tcg_profile_snapshot(prof
, false, true);
4927 void tcg_dump_op_count(GString
*buf
)
4929 TCGProfile prof
= {};
4932 tcg_profile_snapshot_table(&prof
);
4933 for (i
= 0; i
< NB_OPS
; i
++) {
4934 g_string_append_printf(buf
, "%s %" PRId64
"\n", tcg_op_defs
[i
].name
,
4935 prof
.table_op_count
[i
]);
4939 int64_t tcg_cpu_exec_time(void)
4941 unsigned int n_ctxs
= qatomic_read(&tcg_cur_ctxs
);
4945 for (i
= 0; i
< n_ctxs
; i
++) {
4946 const TCGContext
*s
= qatomic_read(&tcg_ctxs
[i
]);
4947 const TCGProfile
*prof
= &s
->prof
;
4949 ret
+= qatomic_read(&prof
->cpu_exec_time
);
4954 void tcg_dump_op_count(GString
*buf
)
4956 g_string_append_printf(buf
, "[TCG profiler not compiled]\n");
4959 int64_t tcg_cpu_exec_time(void)
4961 error_report("%s: TCG profiler not compiled", __func__
);
4967 int tcg_gen_code(TCGContext
*s
, TranslationBlock
*tb
, target_ulong pc_start
)
4969 #ifdef CONFIG_PROFILER
4970 TCGProfile
*prof
= &s
->prof
;
4975 #ifdef CONFIG_PROFILER
4979 QTAILQ_FOREACH(op
, &s
->ops
, link
) {
4982 qatomic_set(&prof
->op_count
, prof
->op_count
+ n
);
4983 if (n
> prof
->op_count_max
) {
4984 qatomic_set(&prof
->op_count_max
, n
);
4988 qatomic_set(&prof
->temp_count
, prof
->temp_count
+ n
);
4989 if (n
> prof
->temp_count_max
) {
4990 qatomic_set(&prof
->temp_count_max
, n
);
4996 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP
)
4997 && qemu_log_in_addr_range(pc_start
))) {
4998 FILE *logfile
= qemu_log_trylock();
5000 fprintf(logfile
, "OP:\n");
5001 tcg_dump_ops(s
, logfile
, false);
5002 fprintf(logfile
, "\n");
5003 qemu_log_unlock(logfile
);
5008 #ifdef CONFIG_DEBUG_TCG
5009 /* Ensure all labels referenced have been emitted. */
5014 QSIMPLEQ_FOREACH(l
, &s
->labels
, next
) {
5015 if (unlikely(!l
->present
) && !QSIMPLEQ_EMPTY(&l
->branches
)) {
5016 qemu_log_mask(CPU_LOG_TB_OP
,
5017 "$L%d referenced but not present.\n", l
->id
);
5025 #ifdef CONFIG_PROFILER
5026 qatomic_set(&prof
->opt_time
, prof
->opt_time
- profile_getclock());
5029 #ifdef USE_TCG_OPTIMIZATIONS
5033 #ifdef CONFIG_PROFILER
5034 qatomic_set(&prof
->opt_time
, prof
->opt_time
+ profile_getclock());
5035 qatomic_set(&prof
->la_time
, prof
->la_time
- profile_getclock());
5038 reachable_code_pass(s
);
5042 if (s
->nb_indirects
> 0) {
5044 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_IND
)
5045 && qemu_log_in_addr_range(pc_start
))) {
5046 FILE *logfile
= qemu_log_trylock();
5048 fprintf(logfile
, "OP before indirect lowering:\n");
5049 tcg_dump_ops(s
, logfile
, false);
5050 fprintf(logfile
, "\n");
5051 qemu_log_unlock(logfile
);
5055 /* Replace indirect temps with direct temps. */
5056 if (liveness_pass_2(s
)) {
5057 /* If changes were made, re-run liveness. */
5062 #ifdef CONFIG_PROFILER
5063 qatomic_set(&prof
->la_time
, prof
->la_time
+ profile_getclock());
5067 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_OPT
)
5068 && qemu_log_in_addr_range(pc_start
))) {
5069 FILE *logfile
= qemu_log_trylock();
5071 fprintf(logfile
, "OP after optimization and liveness analysis:\n");
5072 tcg_dump_ops(s
, logfile
, true);
5073 fprintf(logfile
, "\n");
5074 qemu_log_unlock(logfile
);
5079 /* Initialize goto_tb jump offsets. */
5080 tb
->jmp_reset_offset
[0] = TB_JMP_OFFSET_INVALID
;
5081 tb
->jmp_reset_offset
[1] = TB_JMP_OFFSET_INVALID
;
5082 tb
->jmp_insn_offset
[0] = TB_JMP_OFFSET_INVALID
;
5083 tb
->jmp_insn_offset
[1] = TB_JMP_OFFSET_INVALID
;
5085 tcg_reg_alloc_start(s
);
5088 * Reset the buffer pointers when restarting after overflow.
5089 * TODO: Move this into translate-all.c with the rest of the
5090 * buffer management. Having only this done here is confusing.
5092 s
->code_buf
= tcg_splitwx_to_rw(tb
->tc
.ptr
);
5093 s
->code_ptr
= s
->code_buf
;
5095 #ifdef TCG_TARGET_NEED_LDST_LABELS
5096 QSIMPLEQ_INIT(&s
->ldst_labels
);
5098 #ifdef TCG_TARGET_NEED_POOL_LABELS
5099 s
->pool_labels
= NULL
;
5103 QTAILQ_FOREACH(op
, &s
->ops
, link
) {
5104 TCGOpcode opc
= op
->opc
;
5106 #ifdef CONFIG_PROFILER
5107 qatomic_set(&prof
->table_op_count
[opc
], prof
->table_op_count
[opc
] + 1);
5111 case INDEX_op_mov_i32
:
5112 case INDEX_op_mov_i64
:
5113 case INDEX_op_mov_vec
:
5114 tcg_reg_alloc_mov(s
, op
);
5116 case INDEX_op_dup_vec
:
5117 tcg_reg_alloc_dup(s
, op
);
5119 case INDEX_op_insn_start
:
5120 if (num_insns
>= 0) {
5121 size_t off
= tcg_current_code_size(s
);
5122 s
->gen_insn_end_off
[num_insns
] = off
;
5123 /* Assert that we do not overflow our stored offset. */
5124 assert(s
->gen_insn_end_off
[num_insns
] == off
);
5127 for (i
= 0; i
< TARGET_INSN_START_WORDS
; ++i
) {
5129 #if TARGET_LONG_BITS > TCG_TARGET_REG_BITS
5130 a
= deposit64(op
->args
[i
* 2], 32, 32, op
->args
[i
* 2 + 1]);
5134 s
->gen_insn_data
[num_insns
][i
] = a
;
5137 case INDEX_op_discard
:
5138 temp_dead(s
, arg_temp(op
->args
[0]));
5140 case INDEX_op_set_label
:
5141 tcg_reg_alloc_bb_end(s
, s
->reserved_regs
);
5142 tcg_out_label(s
, arg_label(op
->args
[0]));
5145 tcg_reg_alloc_call(s
, op
);
5147 case INDEX_op_exit_tb
:
5148 tcg_out_exit_tb(s
, op
->args
[0]);
5150 case INDEX_op_goto_tb
:
5151 tcg_out_goto_tb(s
, op
->args
[0]);
5153 case INDEX_op_dup2_vec
:
5154 if (tcg_reg_alloc_dup2(s
, op
)) {
5159 /* Sanity check that we've not introduced any unhandled opcodes. */
5160 tcg_debug_assert(tcg_op_supported(opc
));
5161 /* Note: in order to speed up the code, it would be much
5162 faster to have specialized register allocator functions for
5163 some common argument patterns */
5164 tcg_reg_alloc_op(s
, op
);
5167 /* Test for (pending) buffer overflow. The assumption is that any
5168 one operation beginning below the high water mark cannot overrun
5169 the buffer completely. Thus we can test for overflow after
5170 generating code without having to check during generation. */
5171 if (unlikely((void *)s
->code_ptr
> s
->code_gen_highwater
)) {
5174 /* Test for TB overflow, as seen by gen_insn_end_off. */
5175 if (unlikely(tcg_current_code_size(s
) > UINT16_MAX
)) {
5179 tcg_debug_assert(num_insns
>= 0);
5180 s
->gen_insn_end_off
[num_insns
] = tcg_current_code_size(s
);
5182 /* Generate TB finalization at the end of block */
5183 #ifdef TCG_TARGET_NEED_LDST_LABELS
5184 i
= tcg_out_ldst_finalize(s
);
5189 #ifdef TCG_TARGET_NEED_POOL_LABELS
5190 i
= tcg_out_pool_finalize(s
);
5195 if (!tcg_resolve_relocs(s
)) {
5199 #ifndef CONFIG_TCG_INTERPRETER
5200 /* flush instruction cache */
5201 flush_idcache_range((uintptr_t)tcg_splitwx_to_rx(s
->code_buf
),
5202 (uintptr_t)s
->code_buf
,
5203 tcg_ptr_byte_diff(s
->code_ptr
, s
->code_buf
));
5206 return tcg_current_code_size(s
);
5209 #ifdef CONFIG_PROFILER
5210 void tcg_dump_info(GString
*buf
)
5212 TCGProfile prof
= {};
5213 const TCGProfile
*s
;
5215 int64_t tb_div_count
;
5218 tcg_profile_snapshot_counters(&prof
);
5220 tb_count
= s
->tb_count
;
5221 tb_div_count
= tb_count
? tb_count
: 1;
5222 tot
= s
->interm_time
+ s
->code_time
;
5224 g_string_append_printf(buf
, "JIT cycles %" PRId64
5225 " (%0.3f s at 2.4 GHz)\n",
5227 g_string_append_printf(buf
, "translated TBs %" PRId64
5228 " (aborted=%" PRId64
" %0.1f%%)\n",
5229 tb_count
, s
->tb_count1
- tb_count
,
5230 (double)(s
->tb_count1
- s
->tb_count
)
5231 / (s
->tb_count1
? s
->tb_count1
: 1) * 100.0);
5232 g_string_append_printf(buf
, "avg ops/TB %0.1f max=%d\n",
5233 (double)s
->op_count
/ tb_div_count
, s
->op_count_max
);
5234 g_string_append_printf(buf
, "deleted ops/TB %0.2f\n",
5235 (double)s
->del_op_count
/ tb_div_count
);
5236 g_string_append_printf(buf
, "avg temps/TB %0.2f max=%d\n",
5237 (double)s
->temp_count
/ tb_div_count
,
5239 g_string_append_printf(buf
, "avg host code/TB %0.1f\n",
5240 (double)s
->code_out_len
/ tb_div_count
);
5241 g_string_append_printf(buf
, "avg search data/TB %0.1f\n",
5242 (double)s
->search_out_len
/ tb_div_count
);
5244 g_string_append_printf(buf
, "cycles/op %0.1f\n",
5245 s
->op_count
? (double)tot
/ s
->op_count
: 0);
5246 g_string_append_printf(buf
, "cycles/in byte %0.1f\n",
5247 s
->code_in_len
? (double)tot
/ s
->code_in_len
: 0);
5248 g_string_append_printf(buf
, "cycles/out byte %0.1f\n",
5249 s
->code_out_len
? (double)tot
/ s
->code_out_len
: 0);
5250 g_string_append_printf(buf
, "cycles/search byte %0.1f\n",
5252 (double)tot
/ s
->search_out_len
: 0);
5256 g_string_append_printf(buf
, " gen_interm time %0.1f%%\n",
5257 (double)s
->interm_time
/ tot
* 100.0);
5258 g_string_append_printf(buf
, " gen_code time %0.1f%%\n",
5259 (double)s
->code_time
/ tot
* 100.0);
5260 g_string_append_printf(buf
, "optim./code time %0.1f%%\n",
5261 (double)s
->opt_time
/ (s
->code_time
?
5264 g_string_append_printf(buf
, "liveness/code time %0.1f%%\n",
5265 (double)s
->la_time
/ (s
->code_time
?
5266 s
->code_time
: 1) * 100.0);
5267 g_string_append_printf(buf
, "cpu_restore count %" PRId64
"\n",
5269 g_string_append_printf(buf
, " avg cycles %0.1f\n",
5271 (double)s
->restore_time
/ s
->restore_count
: 0);
5274 void tcg_dump_info(GString
*buf
)
5276 g_string_append_printf(buf
, "[TCG profiler not compiled]\n");
5280 #ifdef ELF_HOST_MACHINE
5281 /* In order to use this feature, the backend needs to do three things:
5283 (1) Define ELF_HOST_MACHINE to indicate both what value to
5284 put into the ELF image and to indicate support for the feature.
5286 (2) Define tcg_register_jit. This should create a buffer containing
5287 the contents of a .debug_frame section that describes the post-
5288 prologue unwind info for the tcg machine.
5290 (3) Call tcg_register_jit_int, with the constructed .debug_frame.
5293 /* Begin GDB interface. THE FOLLOWING MUST MATCH GDB DOCS. */
5300 struct jit_code_entry
{
5301 struct jit_code_entry
*next_entry
;
5302 struct jit_code_entry
*prev_entry
;
5303 const void *symfile_addr
;
5304 uint64_t symfile_size
;
5307 struct jit_descriptor
{
5309 uint32_t action_flag
;
5310 struct jit_code_entry
*relevant_entry
;
5311 struct jit_code_entry
*first_entry
;
5314 void __jit_debug_register_code(void) __attribute__((noinline
));
5315 void __jit_debug_register_code(void)
5320 /* Must statically initialize the version, because GDB may check
5321 the version before we can set it. */
5322 struct jit_descriptor __jit_debug_descriptor
= { 1, 0, 0, 0 };
5324 /* End GDB interface. */
5326 static int find_string(const char *strtab
, const char *str
)
5328 const char *p
= strtab
+ 1;
5331 if (strcmp(p
, str
) == 0) {
5338 static void tcg_register_jit_int(const void *buf_ptr
, size_t buf_size
,
5339 const void *debug_frame
,
5340 size_t debug_frame_size
)
5342 struct __attribute__((packed
)) DebugInfo
{
5349 uintptr_t cu_low_pc
;
5350 uintptr_t cu_high_pc
;
5353 uintptr_t fn_low_pc
;
5354 uintptr_t fn_high_pc
;
5363 struct DebugInfo di
;
5368 struct ElfImage
*img
;
5370 static const struct ElfImage img_template
= {
5372 .e_ident
[EI_MAG0
] = ELFMAG0
,
5373 .e_ident
[EI_MAG1
] = ELFMAG1
,
5374 .e_ident
[EI_MAG2
] = ELFMAG2
,
5375 .e_ident
[EI_MAG3
] = ELFMAG3
,
5376 .e_ident
[EI_CLASS
] = ELF_CLASS
,
5377 .e_ident
[EI_DATA
] = ELF_DATA
,
5378 .e_ident
[EI_VERSION
] = EV_CURRENT
,
5380 .e_machine
= ELF_HOST_MACHINE
,
5381 .e_version
= EV_CURRENT
,
5382 .e_phoff
= offsetof(struct ElfImage
, phdr
),
5383 .e_shoff
= offsetof(struct ElfImage
, shdr
),
5384 .e_ehsize
= sizeof(ElfW(Shdr
)),
5385 .e_phentsize
= sizeof(ElfW(Phdr
)),
5387 .e_shentsize
= sizeof(ElfW(Shdr
)),
5388 .e_shnum
= ARRAY_SIZE(img
->shdr
),
5389 .e_shstrndx
= ARRAY_SIZE(img
->shdr
) - 1,
5390 #ifdef ELF_HOST_FLAGS
5391 .e_flags
= ELF_HOST_FLAGS
,
5394 .e_ident
[EI_OSABI
] = ELF_OSABI
,
5402 [0] = { .sh_type
= SHT_NULL
},
5403 /* Trick: The contents of code_gen_buffer are not present in
5404 this fake ELF file; that got allocated elsewhere. Therefore
5405 we mark .text as SHT_NOBITS (similar to .bss) so that readers
5406 will not look for contents. We can record any address. */
5408 .sh_type
= SHT_NOBITS
,
5409 .sh_flags
= SHF_EXECINSTR
| SHF_ALLOC
,
5411 [2] = { /* .debug_info */
5412 .sh_type
= SHT_PROGBITS
,
5413 .sh_offset
= offsetof(struct ElfImage
, di
),
5414 .sh_size
= sizeof(struct DebugInfo
),
5416 [3] = { /* .debug_abbrev */
5417 .sh_type
= SHT_PROGBITS
,
5418 .sh_offset
= offsetof(struct ElfImage
, da
),
5419 .sh_size
= sizeof(img
->da
),
5421 [4] = { /* .debug_frame */
5422 .sh_type
= SHT_PROGBITS
,
5423 .sh_offset
= sizeof(struct ElfImage
),
5425 [5] = { /* .symtab */
5426 .sh_type
= SHT_SYMTAB
,
5427 .sh_offset
= offsetof(struct ElfImage
, sym
),
5428 .sh_size
= sizeof(img
->sym
),
5430 .sh_link
= ARRAY_SIZE(img
->shdr
) - 1,
5431 .sh_entsize
= sizeof(ElfW(Sym
)),
5433 [6] = { /* .strtab */
5434 .sh_type
= SHT_STRTAB
,
5435 .sh_offset
= offsetof(struct ElfImage
, str
),
5436 .sh_size
= sizeof(img
->str
),
5440 [1] = { /* code_gen_buffer */
5441 .st_info
= ELF_ST_INFO(STB_GLOBAL
, STT_FUNC
),
5446 .len
= sizeof(struct DebugInfo
) - 4,
5448 .ptr_size
= sizeof(void *),
5450 .cu_lang
= 0x8001, /* DW_LANG_Mips_Assembler */
5452 .fn_name
= "code_gen_buffer"
5455 1, /* abbrev number (the cu) */
5456 0x11, 1, /* DW_TAG_compile_unit, has children */
5457 0x13, 0x5, /* DW_AT_language, DW_FORM_data2 */
5458 0x11, 0x1, /* DW_AT_low_pc, DW_FORM_addr */
5459 0x12, 0x1, /* DW_AT_high_pc, DW_FORM_addr */
5460 0, 0, /* end of abbrev */
5461 2, /* abbrev number (the fn) */
5462 0x2e, 0, /* DW_TAG_subprogram, no children */
5463 0x3, 0x8, /* DW_AT_name, DW_FORM_string */
5464 0x11, 0x1, /* DW_AT_low_pc, DW_FORM_addr */
5465 0x12, 0x1, /* DW_AT_high_pc, DW_FORM_addr */
5466 0, 0, /* end of abbrev */
5467 0 /* no more abbrev */
5469 .str
= "\0" ".text\0" ".debug_info\0" ".debug_abbrev\0"
5470 ".debug_frame\0" ".symtab\0" ".strtab\0" "code_gen_buffer",
5473 /* We only need a single jit entry; statically allocate it. */
5474 static struct jit_code_entry one_entry
;
5476 uintptr_t buf
= (uintptr_t)buf_ptr
;
5477 size_t img_size
= sizeof(struct ElfImage
) + debug_frame_size
;
5478 DebugFrameHeader
*dfh
;
5480 img
= g_malloc(img_size
);
5481 *img
= img_template
;
5483 img
->phdr
.p_vaddr
= buf
;
5484 img
->phdr
.p_paddr
= buf
;
5485 img
->phdr
.p_memsz
= buf_size
;
5487 img
->shdr
[1].sh_name
= find_string(img
->str
, ".text");
5488 img
->shdr
[1].sh_addr
= buf
;
5489 img
->shdr
[1].sh_size
= buf_size
;
5491 img
->shdr
[2].sh_name
= find_string(img
->str
, ".debug_info");
5492 img
->shdr
[3].sh_name
= find_string(img
->str
, ".debug_abbrev");
5494 img
->shdr
[4].sh_name
= find_string(img
->str
, ".debug_frame");
5495 img
->shdr
[4].sh_size
= debug_frame_size
;
5497 img
->shdr
[5].sh_name
= find_string(img
->str
, ".symtab");
5498 img
->shdr
[6].sh_name
= find_string(img
->str
, ".strtab");
5500 img
->sym
[1].st_name
= find_string(img
->str
, "code_gen_buffer");
5501 img
->sym
[1].st_value
= buf
;
5502 img
->sym
[1].st_size
= buf_size
;
5504 img
->di
.cu_low_pc
= buf
;
5505 img
->di
.cu_high_pc
= buf
+ buf_size
;
5506 img
->di
.fn_low_pc
= buf
;
5507 img
->di
.fn_high_pc
= buf
+ buf_size
;
5509 dfh
= (DebugFrameHeader
*)(img
+ 1);
5510 memcpy(dfh
, debug_frame
, debug_frame_size
);
5511 dfh
->fde
.func_start
= buf
;
5512 dfh
->fde
.func_len
= buf_size
;
5515 /* Enable this block to be able to debug the ELF image file creation.
5516 One can use readelf, objdump, or other inspection utilities. */
5518 g_autofree
char *jit
= g_strdup_printf("%s/qemu.jit", g_get_tmp_dir());
5519 FILE *f
= fopen(jit
, "w+b");
5521 if (fwrite(img
, img_size
, 1, f
) != img_size
) {
5522 /* Avoid stupid unused return value warning for fwrite. */
5529 one_entry
.symfile_addr
= img
;
5530 one_entry
.symfile_size
= img_size
;
5532 __jit_debug_descriptor
.action_flag
= JIT_REGISTER_FN
;
5533 __jit_debug_descriptor
.relevant_entry
= &one_entry
;
5534 __jit_debug_descriptor
.first_entry
= &one_entry
;
5535 __jit_debug_register_code();
5538 /* No support for the feature. Provide the entry point expected by exec.c,
5539 and implement the internal function we declared earlier. */
5541 static void tcg_register_jit_int(const void *buf
, size_t size
,
5542 const void *debug_frame
,
5543 size_t debug_frame_size
)
5547 void tcg_register_jit(const void *buf
, size_t buf_size
)
5550 #endif /* ELF_HOST_MACHINE */
5552 #if !TCG_TARGET_MAYBE_vec
5553 void tcg_expand_vec_op(TCGOpcode o
, TCGType t
, unsigned e
, TCGArg a0
, ...)
5555 g_assert_not_reached();