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/timer.h"
39 /* Note: the long term plan is to reduce the dependencies on the QEMU
40 CPU definitions. Currently they are used for qemu_ld/st
42 #define NO_CPU_IO_DEFS
45 #include "exec/exec-all.h"
47 #if !defined(CONFIG_USER_ONLY)
48 #include "hw/boards.h"
51 #include "tcg/tcg-op.h"
53 #if UINTPTR_MAX == UINT32_MAX
54 # define ELF_CLASS ELFCLASS32
56 # define ELF_CLASS ELFCLASS64
58 #ifdef HOST_WORDS_BIGENDIAN
59 # define ELF_DATA ELFDATA2MSB
61 # define ELF_DATA ELFDATA2LSB
66 #include "sysemu/sysemu.h"
68 /* Forward declarations for functions declared in tcg-target.c.inc and
70 static void tcg_target_init(TCGContext
*s
);
71 static const TCGTargetOpDef
*tcg_target_op_def(TCGOpcode
);
72 static void tcg_target_qemu_prologue(TCGContext
*s
);
73 static bool patch_reloc(tcg_insn_unit
*code_ptr
, int type
,
74 intptr_t value
, intptr_t addend
);
76 /* The CIE and FDE header definitions will be common to all hosts. */
78 uint32_t len
__attribute__((aligned((sizeof(void *)))));
84 uint8_t return_column
;
87 typedef struct QEMU_PACKED
{
88 uint32_t len
__attribute__((aligned((sizeof(void *)))));
92 } DebugFrameFDEHeader
;
94 typedef struct QEMU_PACKED
{
96 DebugFrameFDEHeader fde
;
99 static void tcg_register_jit_int(void *buf
, size_t size
,
100 const void *debug_frame
,
101 size_t debug_frame_size
)
102 __attribute__((unused
));
104 /* Forward declarations for functions declared and used in tcg-target.c.inc. */
105 static const char *target_parse_constraint(TCGArgConstraint
*ct
,
106 const char *ct_str
, TCGType type
);
107 static void tcg_out_ld(TCGContext
*s
, TCGType type
, TCGReg ret
, TCGReg arg1
,
109 static bool tcg_out_mov(TCGContext
*s
, TCGType type
, TCGReg ret
, TCGReg arg
);
110 static void tcg_out_movi(TCGContext
*s
, TCGType type
,
111 TCGReg ret
, tcg_target_long arg
);
112 static void tcg_out_op(TCGContext
*s
, TCGOpcode opc
, const TCGArg
*args
,
113 const int *const_args
);
114 #if TCG_TARGET_MAYBE_vec
115 static bool tcg_out_dup_vec(TCGContext
*s
, TCGType type
, unsigned vece
,
116 TCGReg dst
, TCGReg src
);
117 static bool tcg_out_dupm_vec(TCGContext
*s
, TCGType type
, unsigned vece
,
118 TCGReg dst
, TCGReg base
, intptr_t offset
);
119 static void tcg_out_dupi_vec(TCGContext
*s
, TCGType type
,
120 TCGReg dst
, tcg_target_long arg
);
121 static void tcg_out_vec_op(TCGContext
*s
, TCGOpcode opc
, unsigned vecl
,
122 unsigned vece
, const TCGArg
*args
,
123 const int *const_args
);
125 static inline bool tcg_out_dup_vec(TCGContext
*s
, TCGType type
, unsigned vece
,
126 TCGReg dst
, TCGReg src
)
128 g_assert_not_reached();
130 static inline bool tcg_out_dupm_vec(TCGContext
*s
, TCGType type
, unsigned vece
,
131 TCGReg dst
, TCGReg base
, intptr_t offset
)
133 g_assert_not_reached();
135 static inline void tcg_out_dupi_vec(TCGContext
*s
, TCGType type
,
136 TCGReg dst
, tcg_target_long arg
)
138 g_assert_not_reached();
140 static inline void tcg_out_vec_op(TCGContext
*s
, TCGOpcode opc
, unsigned vecl
,
141 unsigned vece
, const TCGArg
*args
,
142 const int *const_args
)
144 g_assert_not_reached();
147 static void tcg_out_st(TCGContext
*s
, TCGType type
, TCGReg arg
, TCGReg arg1
,
149 static bool tcg_out_sti(TCGContext
*s
, TCGType type
, TCGArg val
,
150 TCGReg base
, intptr_t ofs
);
151 static void tcg_out_call(TCGContext
*s
, tcg_insn_unit
*target
);
152 static int tcg_target_const_match(tcg_target_long val
, TCGType type
,
153 const TCGArgConstraint
*arg_ct
);
154 #ifdef TCG_TARGET_NEED_LDST_LABELS
155 static int tcg_out_ldst_finalize(TCGContext
*s
);
158 #define TCG_HIGHWATER 1024
160 static TCGContext
**tcg_ctxs
;
161 static unsigned int n_tcg_ctxs
;
162 TCGv_env cpu_env
= 0;
164 struct tcg_region_tree
{
167 /* padding to avoid false sharing is computed at run-time */
171 * We divide code_gen_buffer into equally-sized "regions" that TCG threads
172 * dynamically allocate from as demand dictates. Given appropriate region
173 * sizing, this minimizes flushes even when some TCG threads generate a lot
174 * more code than others.
176 struct tcg_region_state
{
179 /* fields set at init time */
184 size_t size
; /* size of one region */
185 size_t stride
; /* .size + guard size */
187 /* fields protected by the lock */
188 size_t current
; /* current region index */
189 size_t agg_size_full
; /* aggregate size of full regions */
192 static struct tcg_region_state region
;
194 * This is an array of struct tcg_region_tree's, with padding.
195 * We use void * to simplify the computation of region_trees[i]; each
196 * struct is found every tree_size bytes.
198 static void *region_trees
;
199 static size_t tree_size
;
200 static TCGRegSet tcg_target_available_regs
[TCG_TYPE_COUNT
];
201 static TCGRegSet tcg_target_call_clobber_regs
;
203 #if TCG_TARGET_INSN_UNIT_SIZE == 1
204 static __attribute__((unused
)) inline void tcg_out8(TCGContext
*s
, uint8_t v
)
209 static __attribute__((unused
)) inline void tcg_patch8(tcg_insn_unit
*p
,
216 #if TCG_TARGET_INSN_UNIT_SIZE <= 2
217 static __attribute__((unused
)) inline void tcg_out16(TCGContext
*s
, uint16_t v
)
219 if (TCG_TARGET_INSN_UNIT_SIZE
== 2) {
222 tcg_insn_unit
*p
= s
->code_ptr
;
223 memcpy(p
, &v
, sizeof(v
));
224 s
->code_ptr
= p
+ (2 / TCG_TARGET_INSN_UNIT_SIZE
);
228 static __attribute__((unused
)) inline void tcg_patch16(tcg_insn_unit
*p
,
231 if (TCG_TARGET_INSN_UNIT_SIZE
== 2) {
234 memcpy(p
, &v
, sizeof(v
));
239 #if TCG_TARGET_INSN_UNIT_SIZE <= 4
240 static __attribute__((unused
)) inline void tcg_out32(TCGContext
*s
, uint32_t v
)
242 if (TCG_TARGET_INSN_UNIT_SIZE
== 4) {
245 tcg_insn_unit
*p
= s
->code_ptr
;
246 memcpy(p
, &v
, sizeof(v
));
247 s
->code_ptr
= p
+ (4 / TCG_TARGET_INSN_UNIT_SIZE
);
251 static __attribute__((unused
)) inline void tcg_patch32(tcg_insn_unit
*p
,
254 if (TCG_TARGET_INSN_UNIT_SIZE
== 4) {
257 memcpy(p
, &v
, sizeof(v
));
262 #if TCG_TARGET_INSN_UNIT_SIZE <= 8
263 static __attribute__((unused
)) inline void tcg_out64(TCGContext
*s
, uint64_t v
)
265 if (TCG_TARGET_INSN_UNIT_SIZE
== 8) {
268 tcg_insn_unit
*p
= s
->code_ptr
;
269 memcpy(p
, &v
, sizeof(v
));
270 s
->code_ptr
= p
+ (8 / TCG_TARGET_INSN_UNIT_SIZE
);
274 static __attribute__((unused
)) inline void tcg_patch64(tcg_insn_unit
*p
,
277 if (TCG_TARGET_INSN_UNIT_SIZE
== 8) {
280 memcpy(p
, &v
, sizeof(v
));
285 /* label relocation processing */
287 static void tcg_out_reloc(TCGContext
*s
, tcg_insn_unit
*code_ptr
, int type
,
288 TCGLabel
*l
, intptr_t addend
)
290 TCGRelocation
*r
= tcg_malloc(sizeof(TCGRelocation
));
295 QSIMPLEQ_INSERT_TAIL(&l
->relocs
, r
, next
);
298 static void tcg_out_label(TCGContext
*s
, TCGLabel
*l
, tcg_insn_unit
*ptr
)
300 tcg_debug_assert(!l
->has_value
);
302 l
->u
.value_ptr
= ptr
;
305 TCGLabel
*gen_new_label(void)
307 TCGContext
*s
= tcg_ctx
;
308 TCGLabel
*l
= tcg_malloc(sizeof(TCGLabel
));
310 memset(l
, 0, sizeof(TCGLabel
));
311 l
->id
= s
->nb_labels
++;
312 QSIMPLEQ_INIT(&l
->relocs
);
314 QSIMPLEQ_INSERT_TAIL(&s
->labels
, l
, next
);
319 static bool tcg_resolve_relocs(TCGContext
*s
)
323 QSIMPLEQ_FOREACH(l
, &s
->labels
, next
) {
325 uintptr_t value
= l
->u
.value
;
327 QSIMPLEQ_FOREACH(r
, &l
->relocs
, next
) {
328 if (!patch_reloc(r
->ptr
, r
->type
, value
, r
->addend
)) {
336 static void set_jmp_reset_offset(TCGContext
*s
, int which
)
338 size_t off
= tcg_current_code_size(s
);
339 s
->tb_jmp_reset_offset
[which
] = off
;
340 /* Make sure that we didn't overflow the stored offset. */
341 assert(s
->tb_jmp_reset_offset
[which
] == off
);
344 #include "tcg-target.c.inc"
346 /* compare a pointer @ptr and a tb_tc @s */
347 static int ptr_cmp_tb_tc(const void *ptr
, const struct tb_tc
*s
)
349 if (ptr
>= s
->ptr
+ s
->size
) {
351 } else if (ptr
< s
->ptr
) {
357 static gint
tb_tc_cmp(gconstpointer ap
, gconstpointer bp
)
359 const struct tb_tc
*a
= ap
;
360 const struct tb_tc
*b
= bp
;
363 * When both sizes are set, we know this isn't a lookup.
364 * This is the most likely case: every TB must be inserted; lookups
365 * are a lot less frequent.
367 if (likely(a
->size
&& b
->size
)) {
368 if (a
->ptr
> b
->ptr
) {
370 } else if (a
->ptr
< b
->ptr
) {
373 /* a->ptr == b->ptr should happen only on deletions */
374 g_assert(a
->size
== b
->size
);
378 * All lookups have either .size field set to 0.
379 * From the glib sources we see that @ap is always the lookup key. However
380 * the docs provide no guarantee, so we just mark this case as likely.
382 if (likely(a
->size
== 0)) {
383 return ptr_cmp_tb_tc(a
->ptr
, b
);
385 return ptr_cmp_tb_tc(b
->ptr
, a
);
388 static void tcg_region_trees_init(void)
392 tree_size
= ROUND_UP(sizeof(struct tcg_region_tree
), qemu_dcache_linesize
);
393 region_trees
= qemu_memalign(qemu_dcache_linesize
, region
.n
* tree_size
);
394 for (i
= 0; i
< region
.n
; i
++) {
395 struct tcg_region_tree
*rt
= region_trees
+ i
* tree_size
;
397 qemu_mutex_init(&rt
->lock
);
398 rt
->tree
= g_tree_new(tb_tc_cmp
);
402 static struct tcg_region_tree
*tc_ptr_to_region_tree(void *p
)
406 if (p
< region
.start_aligned
) {
409 ptrdiff_t offset
= p
- region
.start_aligned
;
411 if (offset
> region
.stride
* (region
.n
- 1)) {
412 region_idx
= region
.n
- 1;
414 region_idx
= offset
/ region
.stride
;
417 return region_trees
+ region_idx
* tree_size
;
420 void tcg_tb_insert(TranslationBlock
*tb
)
422 struct tcg_region_tree
*rt
= tc_ptr_to_region_tree(tb
->tc
.ptr
);
424 qemu_mutex_lock(&rt
->lock
);
425 g_tree_insert(rt
->tree
, &tb
->tc
, tb
);
426 qemu_mutex_unlock(&rt
->lock
);
429 void tcg_tb_remove(TranslationBlock
*tb
)
431 struct tcg_region_tree
*rt
= tc_ptr_to_region_tree(tb
->tc
.ptr
);
433 qemu_mutex_lock(&rt
->lock
);
434 g_tree_remove(rt
->tree
, &tb
->tc
);
435 qemu_mutex_unlock(&rt
->lock
);
439 * Find the TB 'tb' such that
440 * tb->tc.ptr <= tc_ptr < tb->tc.ptr + tb->tc.size
441 * Return NULL if not found.
443 TranslationBlock
*tcg_tb_lookup(uintptr_t tc_ptr
)
445 struct tcg_region_tree
*rt
= tc_ptr_to_region_tree((void *)tc_ptr
);
446 TranslationBlock
*tb
;
447 struct tb_tc s
= { .ptr
= (void *)tc_ptr
};
449 qemu_mutex_lock(&rt
->lock
);
450 tb
= g_tree_lookup(rt
->tree
, &s
);
451 qemu_mutex_unlock(&rt
->lock
);
455 static void tcg_region_tree_lock_all(void)
459 for (i
= 0; i
< region
.n
; i
++) {
460 struct tcg_region_tree
*rt
= region_trees
+ i
* tree_size
;
462 qemu_mutex_lock(&rt
->lock
);
466 static void tcg_region_tree_unlock_all(void)
470 for (i
= 0; i
< region
.n
; i
++) {
471 struct tcg_region_tree
*rt
= region_trees
+ i
* tree_size
;
473 qemu_mutex_unlock(&rt
->lock
);
477 void tcg_tb_foreach(GTraverseFunc func
, gpointer user_data
)
481 tcg_region_tree_lock_all();
482 for (i
= 0; i
< region
.n
; i
++) {
483 struct tcg_region_tree
*rt
= region_trees
+ i
* tree_size
;
485 g_tree_foreach(rt
->tree
, func
, user_data
);
487 tcg_region_tree_unlock_all();
490 size_t tcg_nb_tbs(void)
495 tcg_region_tree_lock_all();
496 for (i
= 0; i
< region
.n
; i
++) {
497 struct tcg_region_tree
*rt
= region_trees
+ i
* tree_size
;
499 nb_tbs
+= g_tree_nnodes(rt
->tree
);
501 tcg_region_tree_unlock_all();
505 static gboolean
tcg_region_tree_traverse(gpointer k
, gpointer v
, gpointer data
)
507 TranslationBlock
*tb
= v
;
513 static void tcg_region_tree_reset_all(void)
517 tcg_region_tree_lock_all();
518 for (i
= 0; i
< region
.n
; i
++) {
519 struct tcg_region_tree
*rt
= region_trees
+ i
* tree_size
;
521 g_tree_foreach(rt
->tree
, tcg_region_tree_traverse
, NULL
);
522 /* Increment the refcount first so that destroy acts as a reset */
523 g_tree_ref(rt
->tree
);
524 g_tree_destroy(rt
->tree
);
526 tcg_region_tree_unlock_all();
529 static void tcg_region_bounds(size_t curr_region
, void **pstart
, void **pend
)
533 start
= region
.start_aligned
+ curr_region
* region
.stride
;
534 end
= start
+ region
.size
;
536 if (curr_region
== 0) {
537 start
= region
.start
;
539 if (curr_region
== region
.n
- 1) {
547 static void tcg_region_assign(TCGContext
*s
, size_t curr_region
)
551 tcg_region_bounds(curr_region
, &start
, &end
);
553 s
->code_gen_buffer
= start
;
554 s
->code_gen_ptr
= start
;
555 s
->code_gen_buffer_size
= end
- start
;
556 s
->code_gen_highwater
= end
- TCG_HIGHWATER
;
559 static bool tcg_region_alloc__locked(TCGContext
*s
)
561 if (region
.current
== region
.n
) {
564 tcg_region_assign(s
, region
.current
);
570 * Request a new region once the one in use has filled up.
571 * Returns true on error.
573 static bool tcg_region_alloc(TCGContext
*s
)
576 /* read the region size now; alloc__locked will overwrite it on success */
577 size_t size_full
= s
->code_gen_buffer_size
;
579 qemu_mutex_lock(®ion
.lock
);
580 err
= tcg_region_alloc__locked(s
);
582 region
.agg_size_full
+= size_full
- TCG_HIGHWATER
;
584 qemu_mutex_unlock(®ion
.lock
);
589 * Perform a context's first region allocation.
590 * This function does _not_ increment region.agg_size_full.
592 static inline bool tcg_region_initial_alloc__locked(TCGContext
*s
)
594 return tcg_region_alloc__locked(s
);
597 /* Call from a safe-work context */
598 void tcg_region_reset_all(void)
600 unsigned int n_ctxs
= atomic_read(&n_tcg_ctxs
);
603 qemu_mutex_lock(®ion
.lock
);
605 region
.agg_size_full
= 0;
607 for (i
= 0; i
< n_ctxs
; i
++) {
608 TCGContext
*s
= atomic_read(&tcg_ctxs
[i
]);
609 bool err
= tcg_region_initial_alloc__locked(s
);
613 qemu_mutex_unlock(®ion
.lock
);
615 tcg_region_tree_reset_all();
618 #ifdef CONFIG_USER_ONLY
619 static size_t tcg_n_regions(void)
625 * It is likely that some vCPUs will translate more code than others, so we
626 * first try to set more regions than max_cpus, with those regions being of
627 * reasonable size. If that's not possible we make do by evenly dividing
628 * the code_gen_buffer among the vCPUs.
630 static size_t tcg_n_regions(void)
634 /* Use a single region if all we have is one vCPU thread */
635 #if !defined(CONFIG_USER_ONLY)
636 MachineState
*ms
= MACHINE(qdev_get_machine());
637 unsigned int max_cpus
= ms
->smp
.max_cpus
;
639 if (max_cpus
== 1 || !qemu_tcg_mttcg_enabled()) {
643 /* Try to have more regions than max_cpus, with each region being >= 2 MB */
644 for (i
= 8; i
> 0; i
--) {
645 size_t regions_per_thread
= i
;
648 region_size
= tcg_init_ctx
.code_gen_buffer_size
;
649 region_size
/= max_cpus
* regions_per_thread
;
651 if (region_size
>= 2 * 1024u * 1024) {
652 return max_cpus
* regions_per_thread
;
655 /* If we can't, then just allocate one region per vCPU thread */
661 * Initializes region partitioning.
663 * Called at init time from the parent thread (i.e. the one calling
664 * tcg_context_init), after the target's TCG globals have been set.
666 * Region partitioning works by splitting code_gen_buffer into separate regions,
667 * and then assigning regions to TCG threads so that the threads can translate
668 * code in parallel without synchronization.
670 * In softmmu the number of TCG threads is bounded by max_cpus, so we use at
671 * least max_cpus regions in MTTCG. In !MTTCG we use a single region.
672 * Note that the TCG options from the command-line (i.e. -accel accel=tcg,[...])
673 * must have been parsed before calling this function, since it calls
674 * qemu_tcg_mttcg_enabled().
676 * In user-mode we use a single region. Having multiple regions in user-mode
677 * is not supported, because the number of vCPU threads (recall that each thread
678 * spawned by the guest corresponds to a vCPU thread) is only bounded by the
679 * OS, and usually this number is huge (tens of thousands is not uncommon).
680 * Thus, given this large bound on the number of vCPU threads and the fact
681 * that code_gen_buffer is allocated at compile-time, we cannot guarantee
682 * that the availability of at least one region per vCPU thread.
684 * However, this user-mode limitation is unlikely to be a significant problem
685 * in practice. Multi-threaded guests share most if not all of their translated
686 * code, which makes parallel code generation less appealing than in softmmu.
688 void tcg_region_init(void)
690 void *buf
= tcg_init_ctx
.code_gen_buffer
;
692 size_t size
= tcg_init_ctx
.code_gen_buffer_size
;
693 size_t page_size
= qemu_real_host_page_size
;
698 n_regions
= tcg_n_regions();
700 /* The first region will be 'aligned - buf' bytes larger than the others */
701 aligned
= QEMU_ALIGN_PTR_UP(buf
, page_size
);
702 g_assert(aligned
< tcg_init_ctx
.code_gen_buffer
+ size
);
704 * Make region_size a multiple of page_size, using aligned as the start.
705 * As a result of this we might end up with a few extra pages at the end of
706 * the buffer; we will assign those to the last region.
708 region_size
= (size
- (aligned
- buf
)) / n_regions
;
709 region_size
= QEMU_ALIGN_DOWN(region_size
, page_size
);
711 /* A region must have at least 2 pages; one code, one guard */
712 g_assert(region_size
>= 2 * page_size
);
714 /* init the region struct */
715 qemu_mutex_init(®ion
.lock
);
716 region
.n
= n_regions
;
717 region
.size
= region_size
- page_size
;
718 region
.stride
= region_size
;
720 region
.start_aligned
= aligned
;
721 /* page-align the end, since its last page will be a guard page */
722 region
.end
= QEMU_ALIGN_PTR_DOWN(buf
+ size
, page_size
);
723 /* account for that last guard page */
724 region
.end
-= page_size
;
726 /* set guard pages */
727 for (i
= 0; i
< region
.n
; i
++) {
731 tcg_region_bounds(i
, &start
, &end
);
732 rc
= qemu_mprotect_none(end
, page_size
);
736 tcg_region_trees_init();
738 /* In user-mode we support only one ctx, so do the initial allocation now */
739 #ifdef CONFIG_USER_ONLY
741 bool err
= tcg_region_initial_alloc__locked(tcg_ctx
);
748 static void alloc_tcg_plugin_context(TCGContext
*s
)
751 s
->plugin_tb
= g_new0(struct qemu_plugin_tb
, 1);
752 s
->plugin_tb
->insns
=
753 g_ptr_array_new_with_free_func(qemu_plugin_insn_cleanup_fn
);
758 * All TCG threads except the parent (i.e. the one that called tcg_context_init
759 * and registered the target's TCG globals) must register with this function
760 * before initiating translation.
762 * In user-mode we just point tcg_ctx to tcg_init_ctx. See the documentation
763 * of tcg_region_init() for the reasoning behind this.
765 * In softmmu each caller registers its context in tcg_ctxs[]. Note that in
766 * softmmu tcg_ctxs[] does not track tcg_ctx_init, since the initial context
767 * is not used anymore for translation once this function is called.
769 * Not tracking tcg_init_ctx in tcg_ctxs[] in softmmu keeps code that iterates
770 * over the array (e.g. tcg_code_size() the same for both softmmu and user-mode.
772 #ifdef CONFIG_USER_ONLY
773 void tcg_register_thread(void)
775 tcg_ctx
= &tcg_init_ctx
;
778 void tcg_register_thread(void)
780 MachineState
*ms
= MACHINE(qdev_get_machine());
781 TCGContext
*s
= g_malloc(sizeof(*s
));
787 /* Relink mem_base. */
788 for (i
= 0, n
= tcg_init_ctx
.nb_globals
; i
< n
; ++i
) {
789 if (tcg_init_ctx
.temps
[i
].mem_base
) {
790 ptrdiff_t b
= tcg_init_ctx
.temps
[i
].mem_base
- tcg_init_ctx
.temps
;
791 tcg_debug_assert(b
>= 0 && b
< n
);
792 s
->temps
[i
].mem_base
= &s
->temps
[b
];
796 /* Claim an entry in tcg_ctxs */
797 n
= atomic_fetch_inc(&n_tcg_ctxs
);
798 g_assert(n
< ms
->smp
.max_cpus
);
799 atomic_set(&tcg_ctxs
[n
], s
);
802 alloc_tcg_plugin_context(s
);
806 qemu_mutex_lock(®ion
.lock
);
807 err
= tcg_region_initial_alloc__locked(tcg_ctx
);
809 qemu_mutex_unlock(®ion
.lock
);
811 #endif /* !CONFIG_USER_ONLY */
814 * Returns the size (in bytes) of all translated code (i.e. from all regions)
815 * currently in the cache.
816 * See also: tcg_code_capacity()
817 * Do not confuse with tcg_current_code_size(); that one applies to a single
820 size_t tcg_code_size(void)
822 unsigned int n_ctxs
= atomic_read(&n_tcg_ctxs
);
826 qemu_mutex_lock(®ion
.lock
);
827 total
= region
.agg_size_full
;
828 for (i
= 0; i
< n_ctxs
; i
++) {
829 const TCGContext
*s
= atomic_read(&tcg_ctxs
[i
]);
832 size
= atomic_read(&s
->code_gen_ptr
) - s
->code_gen_buffer
;
833 g_assert(size
<= s
->code_gen_buffer_size
);
836 qemu_mutex_unlock(®ion
.lock
);
841 * Returns the code capacity (in bytes) of the entire cache, i.e. including all
843 * See also: tcg_code_size()
845 size_t tcg_code_capacity(void)
847 size_t guard_size
, capacity
;
849 /* no need for synchronization; these variables are set at init time */
850 guard_size
= region
.stride
- region
.size
;
851 capacity
= region
.end
+ guard_size
- region
.start
;
852 capacity
-= region
.n
* (guard_size
+ TCG_HIGHWATER
);
856 size_t tcg_tb_phys_invalidate_count(void)
858 unsigned int n_ctxs
= atomic_read(&n_tcg_ctxs
);
862 for (i
= 0; i
< n_ctxs
; i
++) {
863 const TCGContext
*s
= atomic_read(&tcg_ctxs
[i
]);
865 total
+= atomic_read(&s
->tb_phys_invalidate_count
);
870 /* pool based memory allocation */
871 void *tcg_malloc_internal(TCGContext
*s
, int size
)
876 if (size
> TCG_POOL_CHUNK_SIZE
) {
877 /* big malloc: insert a new pool (XXX: could optimize) */
878 p
= g_malloc(sizeof(TCGPool
) + size
);
880 p
->next
= s
->pool_first_large
;
881 s
->pool_first_large
= p
;
892 pool_size
= TCG_POOL_CHUNK_SIZE
;
893 p
= g_malloc(sizeof(TCGPool
) + pool_size
);
897 s
->pool_current
->next
= p
;
906 s
->pool_cur
= p
->data
+ size
;
907 s
->pool_end
= p
->data
+ p
->size
;
911 void tcg_pool_reset(TCGContext
*s
)
914 for (p
= s
->pool_first_large
; p
; p
= t
) {
918 s
->pool_first_large
= NULL
;
919 s
->pool_cur
= s
->pool_end
= NULL
;
920 s
->pool_current
= NULL
;
923 typedef struct TCGHelperInfo
{
930 #include "exec/helper-proto.h"
932 static const TCGHelperInfo all_helpers
[] = {
933 #include "exec/helper-tcg.h"
935 static GHashTable
*helper_table
;
937 static int indirect_reg_alloc_order
[ARRAY_SIZE(tcg_target_reg_alloc_order
)];
938 static void process_op_defs(TCGContext
*s
);
939 static TCGTemp
*tcg_global_reg_new_internal(TCGContext
*s
, TCGType type
,
940 TCGReg reg
, const char *name
);
942 void tcg_context_init(TCGContext
*s
)
944 int op
, total_args
, n
, i
;
946 TCGArgConstraint
*args_ct
;
950 memset(s
, 0, sizeof(*s
));
953 /* Count total number of arguments and allocate the corresponding
956 for(op
= 0; op
< NB_OPS
; op
++) {
957 def
= &tcg_op_defs
[op
];
958 n
= def
->nb_iargs
+ def
->nb_oargs
;
962 args_ct
= g_malloc(sizeof(TCGArgConstraint
) * total_args
);
963 sorted_args
= g_malloc(sizeof(int) * total_args
);
965 for(op
= 0; op
< NB_OPS
; op
++) {
966 def
= &tcg_op_defs
[op
];
967 def
->args_ct
= args_ct
;
968 def
->sorted_args
= sorted_args
;
969 n
= def
->nb_iargs
+ def
->nb_oargs
;
974 /* Register helpers. */
975 /* Use g_direct_hash/equal for direct pointer comparisons on func. */
976 helper_table
= g_hash_table_new(NULL
, NULL
);
978 for (i
= 0; i
< ARRAY_SIZE(all_helpers
); ++i
) {
979 g_hash_table_insert(helper_table
, (gpointer
)all_helpers
[i
].func
,
980 (gpointer
)&all_helpers
[i
]);
986 /* Reverse the order of the saved registers, assuming they're all at
987 the start of tcg_target_reg_alloc_order. */
988 for (n
= 0; n
< ARRAY_SIZE(tcg_target_reg_alloc_order
); ++n
) {
989 int r
= tcg_target_reg_alloc_order
[n
];
990 if (tcg_regset_test_reg(tcg_target_call_clobber_regs
, r
)) {
994 for (i
= 0; i
< n
; ++i
) {
995 indirect_reg_alloc_order
[i
] = tcg_target_reg_alloc_order
[n
- 1 - i
];
997 for (; i
< ARRAY_SIZE(tcg_target_reg_alloc_order
); ++i
) {
998 indirect_reg_alloc_order
[i
] = tcg_target_reg_alloc_order
[i
];
1001 alloc_tcg_plugin_context(s
);
1005 * In user-mode we simply share the init context among threads, since we
1006 * use a single region. See the documentation tcg_region_init() for the
1007 * reasoning behind this.
1008 * In softmmu we will have at most max_cpus TCG threads.
1010 #ifdef CONFIG_USER_ONLY
1011 tcg_ctxs
= &tcg_ctx
;
1014 MachineState
*ms
= MACHINE(qdev_get_machine());
1015 unsigned int max_cpus
= ms
->smp
.max_cpus
;
1016 tcg_ctxs
= g_new(TCGContext
*, max_cpus
);
1019 tcg_debug_assert(!tcg_regset_test_reg(s
->reserved_regs
, TCG_AREG0
));
1020 ts
= tcg_global_reg_new_internal(s
, TCG_TYPE_PTR
, TCG_AREG0
, "env");
1021 cpu_env
= temp_tcgv_ptr(ts
);
1025 * Allocate TBs right before their corresponding translated code, making
1026 * sure that TBs and code are on different cache lines.
1028 TranslationBlock
*tcg_tb_alloc(TCGContext
*s
)
1030 uintptr_t align
= qemu_icache_linesize
;
1031 TranslationBlock
*tb
;
1035 tb
= (void *)ROUND_UP((uintptr_t)s
->code_gen_ptr
, align
);
1036 next
= (void *)ROUND_UP((uintptr_t)(tb
+ 1), align
);
1038 if (unlikely(next
> s
->code_gen_highwater
)) {
1039 if (tcg_region_alloc(s
)) {
1044 atomic_set(&s
->code_gen_ptr
, next
);
1045 s
->data_gen_ptr
= NULL
;
1049 void tcg_prologue_init(TCGContext
*s
)
1051 size_t prologue_size
, total_size
;
1054 /* Put the prologue at the beginning of code_gen_buffer. */
1055 buf0
= s
->code_gen_buffer
;
1056 total_size
= s
->code_gen_buffer_size
;
1059 s
->data_gen_ptr
= NULL
;
1060 s
->code_gen_prologue
= buf0
;
1062 /* Compute a high-water mark, at which we voluntarily flush the buffer
1063 and start over. The size here is arbitrary, significantly larger
1064 than we expect the code generation for any one opcode to require. */
1065 s
->code_gen_highwater
= s
->code_gen_buffer
+ (total_size
- TCG_HIGHWATER
);
1067 #ifdef TCG_TARGET_NEED_POOL_LABELS
1068 s
->pool_labels
= NULL
;
1071 /* Generate the prologue. */
1072 tcg_target_qemu_prologue(s
);
1074 #ifdef TCG_TARGET_NEED_POOL_LABELS
1075 /* Allow the prologue to put e.g. guest_base into a pool entry. */
1077 int result
= tcg_out_pool_finalize(s
);
1078 tcg_debug_assert(result
== 0);
1083 flush_icache_range((uintptr_t)buf0
, (uintptr_t)buf1
);
1085 /* Deduct the prologue from the buffer. */
1086 prologue_size
= tcg_current_code_size(s
);
1087 s
->code_gen_ptr
= buf1
;
1088 s
->code_gen_buffer
= buf1
;
1090 total_size
-= prologue_size
;
1091 s
->code_gen_buffer_size
= total_size
;
1093 tcg_register_jit(s
->code_gen_buffer
, total_size
);
1096 if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM
)) {
1097 FILE *logfile
= qemu_log_lock();
1098 qemu_log("PROLOGUE: [size=%zu]\n", prologue_size
);
1099 if (s
->data_gen_ptr
) {
1100 size_t code_size
= s
->data_gen_ptr
- buf0
;
1101 size_t data_size
= prologue_size
- code_size
;
1104 log_disas(buf0
, code_size
, NULL
);
1106 for (i
= 0; i
< data_size
; i
+= sizeof(tcg_target_ulong
)) {
1107 if (sizeof(tcg_target_ulong
) == 8) {
1108 qemu_log("0x%08" PRIxPTR
": .quad 0x%016" PRIx64
"\n",
1109 (uintptr_t)s
->data_gen_ptr
+ i
,
1110 *(uint64_t *)(s
->data_gen_ptr
+ i
));
1112 qemu_log("0x%08" PRIxPTR
": .long 0x%08x\n",
1113 (uintptr_t)s
->data_gen_ptr
+ i
,
1114 *(uint32_t *)(s
->data_gen_ptr
+ i
));
1118 log_disas(buf0
, prologue_size
, NULL
);
1122 qemu_log_unlock(logfile
);
1126 /* Assert that goto_ptr is implemented completely. */
1127 if (TCG_TARGET_HAS_goto_ptr
) {
1128 tcg_debug_assert(s
->code_gen_epilogue
!= NULL
);
1132 void tcg_func_start(TCGContext
*s
)
1135 s
->nb_temps
= s
->nb_globals
;
1137 /* No temps have been previously allocated for size or locality. */
1138 memset(s
->free_temps
, 0, sizeof(s
->free_temps
));
1142 s
->current_frame_offset
= s
->frame_start
;
1144 #ifdef CONFIG_DEBUG_TCG
1145 s
->goto_tb_issue_mask
= 0;
1148 QTAILQ_INIT(&s
->ops
);
1149 QTAILQ_INIT(&s
->free_ops
);
1150 QSIMPLEQ_INIT(&s
->labels
);
1153 static inline TCGTemp
*tcg_temp_alloc(TCGContext
*s
)
1155 int n
= s
->nb_temps
++;
1156 tcg_debug_assert(n
< TCG_MAX_TEMPS
);
1157 return memset(&s
->temps
[n
], 0, sizeof(TCGTemp
));
1160 static inline TCGTemp
*tcg_global_alloc(TCGContext
*s
)
1164 tcg_debug_assert(s
->nb_globals
== s
->nb_temps
);
1166 ts
= tcg_temp_alloc(s
);
1167 ts
->temp_global
= 1;
1172 static TCGTemp
*tcg_global_reg_new_internal(TCGContext
*s
, TCGType type
,
1173 TCGReg reg
, const char *name
)
1177 if (TCG_TARGET_REG_BITS
== 32 && type
!= TCG_TYPE_I32
) {
1181 ts
= tcg_global_alloc(s
);
1182 ts
->base_type
= type
;
1187 tcg_regset_set_reg(s
->reserved_regs
, reg
);
1192 void tcg_set_frame(TCGContext
*s
, TCGReg reg
, intptr_t start
, intptr_t size
)
1194 s
->frame_start
= start
;
1195 s
->frame_end
= start
+ size
;
1197 = tcg_global_reg_new_internal(s
, TCG_TYPE_PTR
, reg
, "_frame");
1200 TCGTemp
*tcg_global_mem_new_internal(TCGType type
, TCGv_ptr base
,
1201 intptr_t offset
, const char *name
)
1203 TCGContext
*s
= tcg_ctx
;
1204 TCGTemp
*base_ts
= tcgv_ptr_temp(base
);
1205 TCGTemp
*ts
= tcg_global_alloc(s
);
1206 int indirect_reg
= 0, bigendian
= 0;
1207 #ifdef HOST_WORDS_BIGENDIAN
1211 if (!base_ts
->fixed_reg
) {
1212 /* We do not support double-indirect registers. */
1213 tcg_debug_assert(!base_ts
->indirect_reg
);
1214 base_ts
->indirect_base
= 1;
1215 s
->nb_indirects
+= (TCG_TARGET_REG_BITS
== 32 && type
== TCG_TYPE_I64
1220 if (TCG_TARGET_REG_BITS
== 32 && type
== TCG_TYPE_I64
) {
1221 TCGTemp
*ts2
= tcg_global_alloc(s
);
1224 ts
->base_type
= TCG_TYPE_I64
;
1225 ts
->type
= TCG_TYPE_I32
;
1226 ts
->indirect_reg
= indirect_reg
;
1227 ts
->mem_allocated
= 1;
1228 ts
->mem_base
= base_ts
;
1229 ts
->mem_offset
= offset
+ bigendian
* 4;
1230 pstrcpy(buf
, sizeof(buf
), name
);
1231 pstrcat(buf
, sizeof(buf
), "_0");
1232 ts
->name
= strdup(buf
);
1234 tcg_debug_assert(ts2
== ts
+ 1);
1235 ts2
->base_type
= TCG_TYPE_I64
;
1236 ts2
->type
= TCG_TYPE_I32
;
1237 ts2
->indirect_reg
= indirect_reg
;
1238 ts2
->mem_allocated
= 1;
1239 ts2
->mem_base
= base_ts
;
1240 ts2
->mem_offset
= offset
+ (1 - bigendian
) * 4;
1241 pstrcpy(buf
, sizeof(buf
), name
);
1242 pstrcat(buf
, sizeof(buf
), "_1");
1243 ts2
->name
= strdup(buf
);
1245 ts
->base_type
= type
;
1247 ts
->indirect_reg
= indirect_reg
;
1248 ts
->mem_allocated
= 1;
1249 ts
->mem_base
= base_ts
;
1250 ts
->mem_offset
= offset
;
1256 TCGTemp
*tcg_temp_new_internal(TCGType type
, bool temp_local
)
1258 TCGContext
*s
= tcg_ctx
;
1262 k
= type
+ (temp_local
? TCG_TYPE_COUNT
: 0);
1263 idx
= find_first_bit(s
->free_temps
[k
].l
, TCG_MAX_TEMPS
);
1264 if (idx
< TCG_MAX_TEMPS
) {
1265 /* There is already an available temp with the right type. */
1266 clear_bit(idx
, s
->free_temps
[k
].l
);
1268 ts
= &s
->temps
[idx
];
1269 ts
->temp_allocated
= 1;
1270 tcg_debug_assert(ts
->base_type
== type
);
1271 tcg_debug_assert(ts
->temp_local
== temp_local
);
1273 ts
= tcg_temp_alloc(s
);
1274 if (TCG_TARGET_REG_BITS
== 32 && type
== TCG_TYPE_I64
) {
1275 TCGTemp
*ts2
= tcg_temp_alloc(s
);
1277 ts
->base_type
= type
;
1278 ts
->type
= TCG_TYPE_I32
;
1279 ts
->temp_allocated
= 1;
1280 ts
->temp_local
= temp_local
;
1282 tcg_debug_assert(ts2
== ts
+ 1);
1283 ts2
->base_type
= TCG_TYPE_I64
;
1284 ts2
->type
= TCG_TYPE_I32
;
1285 ts2
->temp_allocated
= 1;
1286 ts2
->temp_local
= temp_local
;
1288 ts
->base_type
= type
;
1290 ts
->temp_allocated
= 1;
1291 ts
->temp_local
= temp_local
;
1295 #if defined(CONFIG_DEBUG_TCG)
1301 TCGv_vec
tcg_temp_new_vec(TCGType type
)
1305 #ifdef CONFIG_DEBUG_TCG
1308 assert(TCG_TARGET_HAS_v64
);
1311 assert(TCG_TARGET_HAS_v128
);
1314 assert(TCG_TARGET_HAS_v256
);
1317 g_assert_not_reached();
1321 t
= tcg_temp_new_internal(type
, 0);
1322 return temp_tcgv_vec(t
);
1325 /* Create a new temp of the same type as an existing temp. */
1326 TCGv_vec
tcg_temp_new_vec_matching(TCGv_vec match
)
1328 TCGTemp
*t
= tcgv_vec_temp(match
);
1330 tcg_debug_assert(t
->temp_allocated
!= 0);
1332 t
= tcg_temp_new_internal(t
->base_type
, 0);
1333 return temp_tcgv_vec(t
);
1336 void tcg_temp_free_internal(TCGTemp
*ts
)
1338 TCGContext
*s
= tcg_ctx
;
1341 #if defined(CONFIG_DEBUG_TCG)
1343 if (s
->temps_in_use
< 0) {
1344 fprintf(stderr
, "More temporaries freed than allocated!\n");
1348 tcg_debug_assert(ts
->temp_global
== 0);
1349 tcg_debug_assert(ts
->temp_allocated
!= 0);
1350 ts
->temp_allocated
= 0;
1353 k
= ts
->base_type
+ (ts
->temp_local
? TCG_TYPE_COUNT
: 0);
1354 set_bit(idx
, s
->free_temps
[k
].l
);
1357 TCGv_i32
tcg_const_i32(int32_t val
)
1360 t0
= tcg_temp_new_i32();
1361 tcg_gen_movi_i32(t0
, val
);
1365 TCGv_i64
tcg_const_i64(int64_t val
)
1368 t0
= tcg_temp_new_i64();
1369 tcg_gen_movi_i64(t0
, val
);
1373 TCGv_i32
tcg_const_local_i32(int32_t val
)
1376 t0
= tcg_temp_local_new_i32();
1377 tcg_gen_movi_i32(t0
, val
);
1381 TCGv_i64
tcg_const_local_i64(int64_t val
)
1384 t0
= tcg_temp_local_new_i64();
1385 tcg_gen_movi_i64(t0
, val
);
1389 #if defined(CONFIG_DEBUG_TCG)
1390 void tcg_clear_temp_count(void)
1392 TCGContext
*s
= tcg_ctx
;
1393 s
->temps_in_use
= 0;
1396 int tcg_check_temp_count(void)
1398 TCGContext
*s
= tcg_ctx
;
1399 if (s
->temps_in_use
) {
1400 /* Clear the count so that we don't give another
1401 * warning immediately next time around.
1403 s
->temps_in_use
= 0;
1410 /* Return true if OP may appear in the opcode stream.
1411 Test the runtime variable that controls each opcode. */
1412 bool tcg_op_supported(TCGOpcode op
)
1415 = TCG_TARGET_HAS_v64
| TCG_TARGET_HAS_v128
| TCG_TARGET_HAS_v256
;
1418 case INDEX_op_discard
:
1419 case INDEX_op_set_label
:
1423 case INDEX_op_insn_start
:
1424 case INDEX_op_exit_tb
:
1425 case INDEX_op_goto_tb
:
1426 case INDEX_op_qemu_ld_i32
:
1427 case INDEX_op_qemu_st_i32
:
1428 case INDEX_op_qemu_ld_i64
:
1429 case INDEX_op_qemu_st_i64
:
1432 case INDEX_op_goto_ptr
:
1433 return TCG_TARGET_HAS_goto_ptr
;
1435 case INDEX_op_mov_i32
:
1436 case INDEX_op_movi_i32
:
1437 case INDEX_op_setcond_i32
:
1438 case INDEX_op_brcond_i32
:
1439 case INDEX_op_ld8u_i32
:
1440 case INDEX_op_ld8s_i32
:
1441 case INDEX_op_ld16u_i32
:
1442 case INDEX_op_ld16s_i32
:
1443 case INDEX_op_ld_i32
:
1444 case INDEX_op_st8_i32
:
1445 case INDEX_op_st16_i32
:
1446 case INDEX_op_st_i32
:
1447 case INDEX_op_add_i32
:
1448 case INDEX_op_sub_i32
:
1449 case INDEX_op_mul_i32
:
1450 case INDEX_op_and_i32
:
1451 case INDEX_op_or_i32
:
1452 case INDEX_op_xor_i32
:
1453 case INDEX_op_shl_i32
:
1454 case INDEX_op_shr_i32
:
1455 case INDEX_op_sar_i32
:
1458 case INDEX_op_movcond_i32
:
1459 return TCG_TARGET_HAS_movcond_i32
;
1460 case INDEX_op_div_i32
:
1461 case INDEX_op_divu_i32
:
1462 return TCG_TARGET_HAS_div_i32
;
1463 case INDEX_op_rem_i32
:
1464 case INDEX_op_remu_i32
:
1465 return TCG_TARGET_HAS_rem_i32
;
1466 case INDEX_op_div2_i32
:
1467 case INDEX_op_divu2_i32
:
1468 return TCG_TARGET_HAS_div2_i32
;
1469 case INDEX_op_rotl_i32
:
1470 case INDEX_op_rotr_i32
:
1471 return TCG_TARGET_HAS_rot_i32
;
1472 case INDEX_op_deposit_i32
:
1473 return TCG_TARGET_HAS_deposit_i32
;
1474 case INDEX_op_extract_i32
:
1475 return TCG_TARGET_HAS_extract_i32
;
1476 case INDEX_op_sextract_i32
:
1477 return TCG_TARGET_HAS_sextract_i32
;
1478 case INDEX_op_extract2_i32
:
1479 return TCG_TARGET_HAS_extract2_i32
;
1480 case INDEX_op_add2_i32
:
1481 return TCG_TARGET_HAS_add2_i32
;
1482 case INDEX_op_sub2_i32
:
1483 return TCG_TARGET_HAS_sub2_i32
;
1484 case INDEX_op_mulu2_i32
:
1485 return TCG_TARGET_HAS_mulu2_i32
;
1486 case INDEX_op_muls2_i32
:
1487 return TCG_TARGET_HAS_muls2_i32
;
1488 case INDEX_op_muluh_i32
:
1489 return TCG_TARGET_HAS_muluh_i32
;
1490 case INDEX_op_mulsh_i32
:
1491 return TCG_TARGET_HAS_mulsh_i32
;
1492 case INDEX_op_ext8s_i32
:
1493 return TCG_TARGET_HAS_ext8s_i32
;
1494 case INDEX_op_ext16s_i32
:
1495 return TCG_TARGET_HAS_ext16s_i32
;
1496 case INDEX_op_ext8u_i32
:
1497 return TCG_TARGET_HAS_ext8u_i32
;
1498 case INDEX_op_ext16u_i32
:
1499 return TCG_TARGET_HAS_ext16u_i32
;
1500 case INDEX_op_bswap16_i32
:
1501 return TCG_TARGET_HAS_bswap16_i32
;
1502 case INDEX_op_bswap32_i32
:
1503 return TCG_TARGET_HAS_bswap32_i32
;
1504 case INDEX_op_not_i32
:
1505 return TCG_TARGET_HAS_not_i32
;
1506 case INDEX_op_neg_i32
:
1507 return TCG_TARGET_HAS_neg_i32
;
1508 case INDEX_op_andc_i32
:
1509 return TCG_TARGET_HAS_andc_i32
;
1510 case INDEX_op_orc_i32
:
1511 return TCG_TARGET_HAS_orc_i32
;
1512 case INDEX_op_eqv_i32
:
1513 return TCG_TARGET_HAS_eqv_i32
;
1514 case INDEX_op_nand_i32
:
1515 return TCG_TARGET_HAS_nand_i32
;
1516 case INDEX_op_nor_i32
:
1517 return TCG_TARGET_HAS_nor_i32
;
1518 case INDEX_op_clz_i32
:
1519 return TCG_TARGET_HAS_clz_i32
;
1520 case INDEX_op_ctz_i32
:
1521 return TCG_TARGET_HAS_ctz_i32
;
1522 case INDEX_op_ctpop_i32
:
1523 return TCG_TARGET_HAS_ctpop_i32
;
1525 case INDEX_op_brcond2_i32
:
1526 case INDEX_op_setcond2_i32
:
1527 return TCG_TARGET_REG_BITS
== 32;
1529 case INDEX_op_mov_i64
:
1530 case INDEX_op_movi_i64
:
1531 case INDEX_op_setcond_i64
:
1532 case INDEX_op_brcond_i64
:
1533 case INDEX_op_ld8u_i64
:
1534 case INDEX_op_ld8s_i64
:
1535 case INDEX_op_ld16u_i64
:
1536 case INDEX_op_ld16s_i64
:
1537 case INDEX_op_ld32u_i64
:
1538 case INDEX_op_ld32s_i64
:
1539 case INDEX_op_ld_i64
:
1540 case INDEX_op_st8_i64
:
1541 case INDEX_op_st16_i64
:
1542 case INDEX_op_st32_i64
:
1543 case INDEX_op_st_i64
:
1544 case INDEX_op_add_i64
:
1545 case INDEX_op_sub_i64
:
1546 case INDEX_op_mul_i64
:
1547 case INDEX_op_and_i64
:
1548 case INDEX_op_or_i64
:
1549 case INDEX_op_xor_i64
:
1550 case INDEX_op_shl_i64
:
1551 case INDEX_op_shr_i64
:
1552 case INDEX_op_sar_i64
:
1553 case INDEX_op_ext_i32_i64
:
1554 case INDEX_op_extu_i32_i64
:
1555 return TCG_TARGET_REG_BITS
== 64;
1557 case INDEX_op_movcond_i64
:
1558 return TCG_TARGET_HAS_movcond_i64
;
1559 case INDEX_op_div_i64
:
1560 case INDEX_op_divu_i64
:
1561 return TCG_TARGET_HAS_div_i64
;
1562 case INDEX_op_rem_i64
:
1563 case INDEX_op_remu_i64
:
1564 return TCG_TARGET_HAS_rem_i64
;
1565 case INDEX_op_div2_i64
:
1566 case INDEX_op_divu2_i64
:
1567 return TCG_TARGET_HAS_div2_i64
;
1568 case INDEX_op_rotl_i64
:
1569 case INDEX_op_rotr_i64
:
1570 return TCG_TARGET_HAS_rot_i64
;
1571 case INDEX_op_deposit_i64
:
1572 return TCG_TARGET_HAS_deposit_i64
;
1573 case INDEX_op_extract_i64
:
1574 return TCG_TARGET_HAS_extract_i64
;
1575 case INDEX_op_sextract_i64
:
1576 return TCG_TARGET_HAS_sextract_i64
;
1577 case INDEX_op_extract2_i64
:
1578 return TCG_TARGET_HAS_extract2_i64
;
1579 case INDEX_op_extrl_i64_i32
:
1580 return TCG_TARGET_HAS_extrl_i64_i32
;
1581 case INDEX_op_extrh_i64_i32
:
1582 return TCG_TARGET_HAS_extrh_i64_i32
;
1583 case INDEX_op_ext8s_i64
:
1584 return TCG_TARGET_HAS_ext8s_i64
;
1585 case INDEX_op_ext16s_i64
:
1586 return TCG_TARGET_HAS_ext16s_i64
;
1587 case INDEX_op_ext32s_i64
:
1588 return TCG_TARGET_HAS_ext32s_i64
;
1589 case INDEX_op_ext8u_i64
:
1590 return TCG_TARGET_HAS_ext8u_i64
;
1591 case INDEX_op_ext16u_i64
:
1592 return TCG_TARGET_HAS_ext16u_i64
;
1593 case INDEX_op_ext32u_i64
:
1594 return TCG_TARGET_HAS_ext32u_i64
;
1595 case INDEX_op_bswap16_i64
:
1596 return TCG_TARGET_HAS_bswap16_i64
;
1597 case INDEX_op_bswap32_i64
:
1598 return TCG_TARGET_HAS_bswap32_i64
;
1599 case INDEX_op_bswap64_i64
:
1600 return TCG_TARGET_HAS_bswap64_i64
;
1601 case INDEX_op_not_i64
:
1602 return TCG_TARGET_HAS_not_i64
;
1603 case INDEX_op_neg_i64
:
1604 return TCG_TARGET_HAS_neg_i64
;
1605 case INDEX_op_andc_i64
:
1606 return TCG_TARGET_HAS_andc_i64
;
1607 case INDEX_op_orc_i64
:
1608 return TCG_TARGET_HAS_orc_i64
;
1609 case INDEX_op_eqv_i64
:
1610 return TCG_TARGET_HAS_eqv_i64
;
1611 case INDEX_op_nand_i64
:
1612 return TCG_TARGET_HAS_nand_i64
;
1613 case INDEX_op_nor_i64
:
1614 return TCG_TARGET_HAS_nor_i64
;
1615 case INDEX_op_clz_i64
:
1616 return TCG_TARGET_HAS_clz_i64
;
1617 case INDEX_op_ctz_i64
:
1618 return TCG_TARGET_HAS_ctz_i64
;
1619 case INDEX_op_ctpop_i64
:
1620 return TCG_TARGET_HAS_ctpop_i64
;
1621 case INDEX_op_add2_i64
:
1622 return TCG_TARGET_HAS_add2_i64
;
1623 case INDEX_op_sub2_i64
:
1624 return TCG_TARGET_HAS_sub2_i64
;
1625 case INDEX_op_mulu2_i64
:
1626 return TCG_TARGET_HAS_mulu2_i64
;
1627 case INDEX_op_muls2_i64
:
1628 return TCG_TARGET_HAS_muls2_i64
;
1629 case INDEX_op_muluh_i64
:
1630 return TCG_TARGET_HAS_muluh_i64
;
1631 case INDEX_op_mulsh_i64
:
1632 return TCG_TARGET_HAS_mulsh_i64
;
1634 case INDEX_op_mov_vec
:
1635 case INDEX_op_dup_vec
:
1636 case INDEX_op_dupi_vec
:
1637 case INDEX_op_dupm_vec
:
1638 case INDEX_op_ld_vec
:
1639 case INDEX_op_st_vec
:
1640 case INDEX_op_add_vec
:
1641 case INDEX_op_sub_vec
:
1642 case INDEX_op_and_vec
:
1643 case INDEX_op_or_vec
:
1644 case INDEX_op_xor_vec
:
1645 case INDEX_op_cmp_vec
:
1647 case INDEX_op_dup2_vec
:
1648 return have_vec
&& TCG_TARGET_REG_BITS
== 32;
1649 case INDEX_op_not_vec
:
1650 return have_vec
&& TCG_TARGET_HAS_not_vec
;
1651 case INDEX_op_neg_vec
:
1652 return have_vec
&& TCG_TARGET_HAS_neg_vec
;
1653 case INDEX_op_abs_vec
:
1654 return have_vec
&& TCG_TARGET_HAS_abs_vec
;
1655 case INDEX_op_andc_vec
:
1656 return have_vec
&& TCG_TARGET_HAS_andc_vec
;
1657 case INDEX_op_orc_vec
:
1658 return have_vec
&& TCG_TARGET_HAS_orc_vec
;
1659 case INDEX_op_mul_vec
:
1660 return have_vec
&& TCG_TARGET_HAS_mul_vec
;
1661 case INDEX_op_shli_vec
:
1662 case INDEX_op_shri_vec
:
1663 case INDEX_op_sari_vec
:
1664 return have_vec
&& TCG_TARGET_HAS_shi_vec
;
1665 case INDEX_op_shls_vec
:
1666 case INDEX_op_shrs_vec
:
1667 case INDEX_op_sars_vec
:
1668 return have_vec
&& TCG_TARGET_HAS_shs_vec
;
1669 case INDEX_op_shlv_vec
:
1670 case INDEX_op_shrv_vec
:
1671 case INDEX_op_sarv_vec
:
1672 return have_vec
&& TCG_TARGET_HAS_shv_vec
;
1673 case INDEX_op_rotli_vec
:
1674 return have_vec
&& TCG_TARGET_HAS_roti_vec
;
1675 case INDEX_op_rotls_vec
:
1676 return have_vec
&& TCG_TARGET_HAS_rots_vec
;
1677 case INDEX_op_rotlv_vec
:
1678 case INDEX_op_rotrv_vec
:
1679 return have_vec
&& TCG_TARGET_HAS_rotv_vec
;
1680 case INDEX_op_ssadd_vec
:
1681 case INDEX_op_usadd_vec
:
1682 case INDEX_op_sssub_vec
:
1683 case INDEX_op_ussub_vec
:
1684 return have_vec
&& TCG_TARGET_HAS_sat_vec
;
1685 case INDEX_op_smin_vec
:
1686 case INDEX_op_umin_vec
:
1687 case INDEX_op_smax_vec
:
1688 case INDEX_op_umax_vec
:
1689 return have_vec
&& TCG_TARGET_HAS_minmax_vec
;
1690 case INDEX_op_bitsel_vec
:
1691 return have_vec
&& TCG_TARGET_HAS_bitsel_vec
;
1692 case INDEX_op_cmpsel_vec
:
1693 return have_vec
&& TCG_TARGET_HAS_cmpsel_vec
;
1696 tcg_debug_assert(op
> INDEX_op_last_generic
&& op
< NB_OPS
);
1701 /* Note: we convert the 64 bit args to 32 bit and do some alignment
1702 and endian swap. Maybe it would be better to do the alignment
1703 and endian swap in tcg_reg_alloc_call(). */
1704 void tcg_gen_callN(void *func
, TCGTemp
*ret
, int nargs
, TCGTemp
**args
)
1706 int i
, real_args
, nb_rets
, pi
;
1707 unsigned sizemask
, flags
;
1708 TCGHelperInfo
*info
;
1711 info
= g_hash_table_lookup(helper_table
, (gpointer
)func
);
1712 flags
= info
->flags
;
1713 sizemask
= info
->sizemask
;
1715 #ifdef CONFIG_PLUGIN
1716 /* detect non-plugin helpers */
1717 if (tcg_ctx
->plugin_insn
&& unlikely(strncmp(info
->name
, "plugin_", 7))) {
1718 tcg_ctx
->plugin_insn
->calls_helpers
= true;
1722 #if defined(__sparc__) && !defined(__arch64__) \
1723 && !defined(CONFIG_TCG_INTERPRETER)
1724 /* We have 64-bit values in one register, but need to pass as two
1725 separate parameters. Split them. */
1726 int orig_sizemask
= sizemask
;
1727 int orig_nargs
= nargs
;
1728 TCGv_i64 retl
, reth
;
1729 TCGTemp
*split_args
[MAX_OPC_PARAM
];
1733 if (sizemask
!= 0) {
1734 for (i
= real_args
= 0; i
< nargs
; ++i
) {
1735 int is_64bit
= sizemask
& (1 << (i
+1)*2);
1737 TCGv_i64 orig
= temp_tcgv_i64(args
[i
]);
1738 TCGv_i32 h
= tcg_temp_new_i32();
1739 TCGv_i32 l
= tcg_temp_new_i32();
1740 tcg_gen_extr_i64_i32(l
, h
, orig
);
1741 split_args
[real_args
++] = tcgv_i32_temp(h
);
1742 split_args
[real_args
++] = tcgv_i32_temp(l
);
1744 split_args
[real_args
++] = args
[i
];
1751 #elif defined(TCG_TARGET_EXTEND_ARGS) && TCG_TARGET_REG_BITS == 64
1752 for (i
= 0; i
< nargs
; ++i
) {
1753 int is_64bit
= sizemask
& (1 << (i
+1)*2);
1754 int is_signed
= sizemask
& (2 << (i
+1)*2);
1756 TCGv_i64 temp
= tcg_temp_new_i64();
1757 TCGv_i64 orig
= temp_tcgv_i64(args
[i
]);
1759 tcg_gen_ext32s_i64(temp
, orig
);
1761 tcg_gen_ext32u_i64(temp
, orig
);
1763 args
[i
] = tcgv_i64_temp(temp
);
1766 #endif /* TCG_TARGET_EXTEND_ARGS */
1768 op
= tcg_emit_op(INDEX_op_call
);
1772 #if defined(__sparc__) && !defined(__arch64__) \
1773 && !defined(CONFIG_TCG_INTERPRETER)
1774 if (orig_sizemask
& 1) {
1775 /* The 32-bit ABI is going to return the 64-bit value in
1776 the %o0/%o1 register pair. Prepare for this by using
1777 two return temporaries, and reassemble below. */
1778 retl
= tcg_temp_new_i64();
1779 reth
= tcg_temp_new_i64();
1780 op
->args
[pi
++] = tcgv_i64_arg(reth
);
1781 op
->args
[pi
++] = tcgv_i64_arg(retl
);
1784 op
->args
[pi
++] = temp_arg(ret
);
1788 if (TCG_TARGET_REG_BITS
< 64 && (sizemask
& 1)) {
1789 #ifdef HOST_WORDS_BIGENDIAN
1790 op
->args
[pi
++] = temp_arg(ret
+ 1);
1791 op
->args
[pi
++] = temp_arg(ret
);
1793 op
->args
[pi
++] = temp_arg(ret
);
1794 op
->args
[pi
++] = temp_arg(ret
+ 1);
1798 op
->args
[pi
++] = temp_arg(ret
);
1805 TCGOP_CALLO(op
) = nb_rets
;
1808 for (i
= 0; i
< nargs
; i
++) {
1809 int is_64bit
= sizemask
& (1 << (i
+1)*2);
1810 if (TCG_TARGET_REG_BITS
< 64 && is_64bit
) {
1811 #ifdef TCG_TARGET_CALL_ALIGN_ARGS
1812 /* some targets want aligned 64 bit args */
1813 if (real_args
& 1) {
1814 op
->args
[pi
++] = TCG_CALL_DUMMY_ARG
;
1818 /* If stack grows up, then we will be placing successive
1819 arguments at lower addresses, which means we need to
1820 reverse the order compared to how we would normally
1821 treat either big or little-endian. For those arguments
1822 that will wind up in registers, this still works for
1823 HPPA (the only current STACK_GROWSUP target) since the
1824 argument registers are *also* allocated in decreasing
1825 order. If another such target is added, this logic may
1826 have to get more complicated to differentiate between
1827 stack arguments and register arguments. */
1828 #if defined(HOST_WORDS_BIGENDIAN) != defined(TCG_TARGET_STACK_GROWSUP)
1829 op
->args
[pi
++] = temp_arg(args
[i
] + 1);
1830 op
->args
[pi
++] = temp_arg(args
[i
]);
1832 op
->args
[pi
++] = temp_arg(args
[i
]);
1833 op
->args
[pi
++] = temp_arg(args
[i
] + 1);
1839 op
->args
[pi
++] = temp_arg(args
[i
]);
1842 op
->args
[pi
++] = (uintptr_t)func
;
1843 op
->args
[pi
++] = flags
;
1844 TCGOP_CALLI(op
) = real_args
;
1846 /* Make sure the fields didn't overflow. */
1847 tcg_debug_assert(TCGOP_CALLI(op
) == real_args
);
1848 tcg_debug_assert(pi
<= ARRAY_SIZE(op
->args
));
1850 #if defined(__sparc__) && !defined(__arch64__) \
1851 && !defined(CONFIG_TCG_INTERPRETER)
1852 /* Free all of the parts we allocated above. */
1853 for (i
= real_args
= 0; i
< orig_nargs
; ++i
) {
1854 int is_64bit
= orig_sizemask
& (1 << (i
+1)*2);
1856 tcg_temp_free_internal(args
[real_args
++]);
1857 tcg_temp_free_internal(args
[real_args
++]);
1862 if (orig_sizemask
& 1) {
1863 /* The 32-bit ABI returned two 32-bit pieces. Re-assemble them.
1864 Note that describing these as TCGv_i64 eliminates an unnecessary
1865 zero-extension that tcg_gen_concat_i32_i64 would create. */
1866 tcg_gen_concat32_i64(temp_tcgv_i64(ret
), retl
, reth
);
1867 tcg_temp_free_i64(retl
);
1868 tcg_temp_free_i64(reth
);
1870 #elif defined(TCG_TARGET_EXTEND_ARGS) && TCG_TARGET_REG_BITS == 64
1871 for (i
= 0; i
< nargs
; ++i
) {
1872 int is_64bit
= sizemask
& (1 << (i
+1)*2);
1874 tcg_temp_free_internal(args
[i
]);
1877 #endif /* TCG_TARGET_EXTEND_ARGS */
1880 static void tcg_reg_alloc_start(TCGContext
*s
)
1885 for (i
= 0, n
= s
->nb_globals
; i
< n
; i
++) {
1887 ts
->val_type
= (ts
->fixed_reg
? TEMP_VAL_REG
: TEMP_VAL_MEM
);
1889 for (n
= s
->nb_temps
; i
< n
; i
++) {
1891 ts
->val_type
= (ts
->temp_local
? TEMP_VAL_MEM
: TEMP_VAL_DEAD
);
1892 ts
->mem_allocated
= 0;
1896 memset(s
->reg_to_temp
, 0, sizeof(s
->reg_to_temp
));
1899 static char *tcg_get_arg_str_ptr(TCGContext
*s
, char *buf
, int buf_size
,
1902 int idx
= temp_idx(ts
);
1904 if (ts
->temp_global
) {
1905 pstrcpy(buf
, buf_size
, ts
->name
);
1906 } else if (ts
->temp_local
) {
1907 snprintf(buf
, buf_size
, "loc%d", idx
- s
->nb_globals
);
1909 snprintf(buf
, buf_size
, "tmp%d", idx
- s
->nb_globals
);
1914 static char *tcg_get_arg_str(TCGContext
*s
, char *buf
,
1915 int buf_size
, TCGArg arg
)
1917 return tcg_get_arg_str_ptr(s
, buf
, buf_size
, arg_temp(arg
));
1920 /* Find helper name. */
1921 static inline const char *tcg_find_helper(TCGContext
*s
, uintptr_t val
)
1923 const char *ret
= NULL
;
1925 TCGHelperInfo
*info
= g_hash_table_lookup(helper_table
, (gpointer
)val
);
1933 static const char * const cond_name
[] =
1935 [TCG_COND_NEVER
] = "never",
1936 [TCG_COND_ALWAYS
] = "always",
1937 [TCG_COND_EQ
] = "eq",
1938 [TCG_COND_NE
] = "ne",
1939 [TCG_COND_LT
] = "lt",
1940 [TCG_COND_GE
] = "ge",
1941 [TCG_COND_LE
] = "le",
1942 [TCG_COND_GT
] = "gt",
1943 [TCG_COND_LTU
] = "ltu",
1944 [TCG_COND_GEU
] = "geu",
1945 [TCG_COND_LEU
] = "leu",
1946 [TCG_COND_GTU
] = "gtu"
1949 static const char * const ldst_name
[] =
1965 static const char * const alignment_name
[(MO_AMASK
>> MO_ASHIFT
) + 1] = {
1966 #ifdef TARGET_ALIGNED_ONLY
1967 [MO_UNALN
>> MO_ASHIFT
] = "un+",
1968 [MO_ALIGN
>> MO_ASHIFT
] = "",
1970 [MO_UNALN
>> MO_ASHIFT
] = "",
1971 [MO_ALIGN
>> MO_ASHIFT
] = "al+",
1973 [MO_ALIGN_2
>> MO_ASHIFT
] = "al2+",
1974 [MO_ALIGN_4
>> MO_ASHIFT
] = "al4+",
1975 [MO_ALIGN_8
>> MO_ASHIFT
] = "al8+",
1976 [MO_ALIGN_16
>> MO_ASHIFT
] = "al16+",
1977 [MO_ALIGN_32
>> MO_ASHIFT
] = "al32+",
1978 [MO_ALIGN_64
>> MO_ASHIFT
] = "al64+",
1981 static inline bool tcg_regset_single(TCGRegSet d
)
1983 return (d
& (d
- 1)) == 0;
1986 static inline TCGReg
tcg_regset_first(TCGRegSet d
)
1988 if (TCG_TARGET_NB_REGS
<= 32) {
1995 static void tcg_dump_ops(TCGContext
*s
, bool have_prefs
)
2000 QTAILQ_FOREACH(op
, &s
->ops
, link
) {
2001 int i
, k
, nb_oargs
, nb_iargs
, nb_cargs
;
2002 const TCGOpDef
*def
;
2007 def
= &tcg_op_defs
[c
];
2009 if (c
== INDEX_op_insn_start
) {
2011 col
+= qemu_log("\n ----");
2013 for (i
= 0; i
< TARGET_INSN_START_WORDS
; ++i
) {
2015 #if TARGET_LONG_BITS > TCG_TARGET_REG_BITS
2016 a
= deposit64(op
->args
[i
* 2], 32, 32, op
->args
[i
* 2 + 1]);
2020 col
+= qemu_log(" " TARGET_FMT_lx
, a
);
2022 } else if (c
== INDEX_op_call
) {
2023 /* variable number of arguments */
2024 nb_oargs
= TCGOP_CALLO(op
);
2025 nb_iargs
= TCGOP_CALLI(op
);
2026 nb_cargs
= def
->nb_cargs
;
2028 /* function name, flags, out args */
2029 col
+= qemu_log(" %s %s,$0x%" TCG_PRIlx
",$%d", def
->name
,
2030 tcg_find_helper(s
, op
->args
[nb_oargs
+ nb_iargs
]),
2031 op
->args
[nb_oargs
+ nb_iargs
+ 1], nb_oargs
);
2032 for (i
= 0; i
< nb_oargs
; i
++) {
2033 col
+= qemu_log(",%s", tcg_get_arg_str(s
, buf
, sizeof(buf
),
2036 for (i
= 0; i
< nb_iargs
; i
++) {
2037 TCGArg arg
= op
->args
[nb_oargs
+ i
];
2038 const char *t
= "<dummy>";
2039 if (arg
!= TCG_CALL_DUMMY_ARG
) {
2040 t
= tcg_get_arg_str(s
, buf
, sizeof(buf
), arg
);
2042 col
+= qemu_log(",%s", t
);
2045 col
+= qemu_log(" %s ", def
->name
);
2047 nb_oargs
= def
->nb_oargs
;
2048 nb_iargs
= def
->nb_iargs
;
2049 nb_cargs
= def
->nb_cargs
;
2051 if (def
->flags
& TCG_OPF_VECTOR
) {
2052 col
+= qemu_log("v%d,e%d,", 64 << TCGOP_VECL(op
),
2053 8 << TCGOP_VECE(op
));
2057 for (i
= 0; i
< nb_oargs
; i
++) {
2059 col
+= qemu_log(",");
2061 col
+= qemu_log("%s", tcg_get_arg_str(s
, buf
, sizeof(buf
),
2064 for (i
= 0; i
< nb_iargs
; i
++) {
2066 col
+= qemu_log(",");
2068 col
+= qemu_log("%s", tcg_get_arg_str(s
, buf
, sizeof(buf
),
2072 case INDEX_op_brcond_i32
:
2073 case INDEX_op_setcond_i32
:
2074 case INDEX_op_movcond_i32
:
2075 case INDEX_op_brcond2_i32
:
2076 case INDEX_op_setcond2_i32
:
2077 case INDEX_op_brcond_i64
:
2078 case INDEX_op_setcond_i64
:
2079 case INDEX_op_movcond_i64
:
2080 case INDEX_op_cmp_vec
:
2081 case INDEX_op_cmpsel_vec
:
2082 if (op
->args
[k
] < ARRAY_SIZE(cond_name
)
2083 && cond_name
[op
->args
[k
]]) {
2084 col
+= qemu_log(",%s", cond_name
[op
->args
[k
++]]);
2086 col
+= qemu_log(",$0x%" TCG_PRIlx
, op
->args
[k
++]);
2090 case INDEX_op_qemu_ld_i32
:
2091 case INDEX_op_qemu_st_i32
:
2092 case INDEX_op_qemu_ld_i64
:
2093 case INDEX_op_qemu_st_i64
:
2095 TCGMemOpIdx oi
= op
->args
[k
++];
2096 MemOp op
= get_memop(oi
);
2097 unsigned ix
= get_mmuidx(oi
);
2099 if (op
& ~(MO_AMASK
| MO_BSWAP
| MO_SSIZE
)) {
2100 col
+= qemu_log(",$0x%x,%u", op
, ix
);
2102 const char *s_al
, *s_op
;
2103 s_al
= alignment_name
[(op
& MO_AMASK
) >> MO_ASHIFT
];
2104 s_op
= ldst_name
[op
& (MO_BSWAP
| MO_SSIZE
)];
2105 col
+= qemu_log(",%s%s,%u", s_al
, s_op
, ix
);
2115 case INDEX_op_set_label
:
2117 case INDEX_op_brcond_i32
:
2118 case INDEX_op_brcond_i64
:
2119 case INDEX_op_brcond2_i32
:
2120 col
+= qemu_log("%s$L%d", k
? "," : "",
2121 arg_label(op
->args
[k
])->id
);
2127 for (; i
< nb_cargs
; i
++, k
++) {
2128 col
+= qemu_log("%s$0x%" TCG_PRIlx
, k
? "," : "", op
->args
[k
]);
2132 if (have_prefs
|| op
->life
) {
2134 QemuLogFile
*logfile
;
2137 logfile
= atomic_rcu_read(&qemu_logfile
);
2139 for (; col
< 40; ++col
) {
2140 putc(' ', logfile
->fd
);
2147 unsigned life
= op
->life
;
2149 if (life
& (SYNC_ARG
* 3)) {
2151 for (i
= 0; i
< 2; ++i
) {
2152 if (life
& (SYNC_ARG
<< i
)) {
2160 for (i
= 0; life
; ++i
, life
>>= 1) {
2169 for (i
= 0; i
< nb_oargs
; ++i
) {
2170 TCGRegSet set
= op
->output_pref
[i
];
2179 } else if (set
== MAKE_64BIT_MASK(0, TCG_TARGET_NB_REGS
)) {
2181 #ifdef CONFIG_DEBUG_TCG
2182 } else if (tcg_regset_single(set
)) {
2183 TCGReg reg
= tcg_regset_first(set
);
2184 qemu_log("%s", tcg_target_reg_names
[reg
]);
2186 } else if (TCG_TARGET_NB_REGS
<= 32) {
2187 qemu_log("%#x", (uint32_t)set
);
2189 qemu_log("%#" PRIx64
, (uint64_t)set
);
2198 /* we give more priority to constraints with less registers */
2199 static int get_constraint_priority(const TCGOpDef
*def
, int k
)
2201 const TCGArgConstraint
*arg_ct
;
2204 arg_ct
= &def
->args_ct
[k
];
2205 if (arg_ct
->ct
& TCG_CT_ALIAS
) {
2206 /* an alias is equivalent to a single register */
2209 if (!(arg_ct
->ct
& TCG_CT_REG
))
2212 for(i
= 0; i
< TCG_TARGET_NB_REGS
; i
++) {
2213 if (tcg_regset_test_reg(arg_ct
->u
.regs
, i
))
2217 return TCG_TARGET_NB_REGS
- n
+ 1;
2220 /* sort from highest priority to lowest */
2221 static void sort_constraints(TCGOpDef
*def
, int start
, int n
)
2223 int i
, j
, p1
, p2
, tmp
;
2225 for(i
= 0; i
< n
; i
++)
2226 def
->sorted_args
[start
+ i
] = start
+ i
;
2229 for(i
= 0; i
< n
- 1; i
++) {
2230 for(j
= i
+ 1; j
< n
; j
++) {
2231 p1
= get_constraint_priority(def
, def
->sorted_args
[start
+ i
]);
2232 p2
= get_constraint_priority(def
, def
->sorted_args
[start
+ j
]);
2234 tmp
= def
->sorted_args
[start
+ i
];
2235 def
->sorted_args
[start
+ i
] = def
->sorted_args
[start
+ j
];
2236 def
->sorted_args
[start
+ j
] = tmp
;
2242 static void process_op_defs(TCGContext
*s
)
2246 for (op
= 0; op
< NB_OPS
; op
++) {
2247 TCGOpDef
*def
= &tcg_op_defs
[op
];
2248 const TCGTargetOpDef
*tdefs
;
2252 if (def
->flags
& TCG_OPF_NOT_PRESENT
) {
2256 nb_args
= def
->nb_iargs
+ def
->nb_oargs
;
2261 tdefs
= tcg_target_op_def(op
);
2262 /* Missing TCGTargetOpDef entry. */
2263 tcg_debug_assert(tdefs
!= NULL
);
2265 type
= (def
->flags
& TCG_OPF_64BIT
? TCG_TYPE_I64
: TCG_TYPE_I32
);
2266 for (i
= 0; i
< nb_args
; i
++) {
2267 const char *ct_str
= tdefs
->args_ct_str
[i
];
2268 /* Incomplete TCGTargetOpDef entry. */
2269 tcg_debug_assert(ct_str
!= NULL
);
2271 def
->args_ct
[i
].u
.regs
= 0;
2272 def
->args_ct
[i
].ct
= 0;
2273 while (*ct_str
!= '\0') {
2277 int oarg
= *ct_str
- '0';
2278 tcg_debug_assert(ct_str
== tdefs
->args_ct_str
[i
]);
2279 tcg_debug_assert(oarg
< def
->nb_oargs
);
2280 tcg_debug_assert(def
->args_ct
[oarg
].ct
& TCG_CT_REG
);
2281 /* TCG_CT_ALIAS is for the output arguments.
2282 The input is tagged with TCG_CT_IALIAS. */
2283 def
->args_ct
[i
] = def
->args_ct
[oarg
];
2284 def
->args_ct
[oarg
].ct
|= TCG_CT_ALIAS
;
2285 def
->args_ct
[oarg
].alias_index
= i
;
2286 def
->args_ct
[i
].ct
|= TCG_CT_IALIAS
;
2287 def
->args_ct
[i
].alias_index
= oarg
;
2292 def
->args_ct
[i
].ct
|= TCG_CT_NEWREG
;
2296 def
->args_ct
[i
].ct
|= TCG_CT_CONST
;
2300 ct_str
= target_parse_constraint(&def
->args_ct
[i
],
2302 /* Typo in TCGTargetOpDef constraint. */
2303 tcg_debug_assert(ct_str
!= NULL
);
2308 /* TCGTargetOpDef entry with too much information? */
2309 tcg_debug_assert(i
== TCG_MAX_OP_ARGS
|| tdefs
->args_ct_str
[i
] == NULL
);
2311 /* sort the constraints (XXX: this is just an heuristic) */
2312 sort_constraints(def
, 0, def
->nb_oargs
);
2313 sort_constraints(def
, def
->nb_oargs
, def
->nb_iargs
);
2317 void tcg_op_remove(TCGContext
*s
, TCGOp
*op
)
2323 label
= arg_label(op
->args
[0]);
2326 case INDEX_op_brcond_i32
:
2327 case INDEX_op_brcond_i64
:
2328 label
= arg_label(op
->args
[3]);
2331 case INDEX_op_brcond2_i32
:
2332 label
= arg_label(op
->args
[5]);
2339 QTAILQ_REMOVE(&s
->ops
, op
, link
);
2340 QTAILQ_INSERT_TAIL(&s
->free_ops
, op
, link
);
2343 #ifdef CONFIG_PROFILER
2344 atomic_set(&s
->prof
.del_op_count
, s
->prof
.del_op_count
+ 1);
2348 static TCGOp
*tcg_op_alloc(TCGOpcode opc
)
2350 TCGContext
*s
= tcg_ctx
;
2353 if (likely(QTAILQ_EMPTY(&s
->free_ops
))) {
2354 op
= tcg_malloc(sizeof(TCGOp
));
2356 op
= QTAILQ_FIRST(&s
->free_ops
);
2357 QTAILQ_REMOVE(&s
->free_ops
, op
, link
);
2359 memset(op
, 0, offsetof(TCGOp
, link
));
2366 TCGOp
*tcg_emit_op(TCGOpcode opc
)
2368 TCGOp
*op
= tcg_op_alloc(opc
);
2369 QTAILQ_INSERT_TAIL(&tcg_ctx
->ops
, op
, link
);
2373 TCGOp
*tcg_op_insert_before(TCGContext
*s
, TCGOp
*old_op
, TCGOpcode opc
)
2375 TCGOp
*new_op
= tcg_op_alloc(opc
);
2376 QTAILQ_INSERT_BEFORE(old_op
, new_op
, link
);
2380 TCGOp
*tcg_op_insert_after(TCGContext
*s
, TCGOp
*old_op
, TCGOpcode opc
)
2382 TCGOp
*new_op
= tcg_op_alloc(opc
);
2383 QTAILQ_INSERT_AFTER(&s
->ops
, old_op
, new_op
, link
);
2387 /* Reachable analysis : remove unreachable code. */
2388 static void reachable_code_pass(TCGContext
*s
)
2390 TCGOp
*op
, *op_next
;
2393 QTAILQ_FOREACH_SAFE(op
, &s
->ops
, link
, op_next
) {
2399 case INDEX_op_set_label
:
2400 label
= arg_label(op
->args
[0]);
2401 if (label
->refs
== 0) {
2403 * While there is an occasional backward branch, virtually
2404 * all branches generated by the translators are forward.
2405 * Which means that generally we will have already removed
2406 * all references to the label that will be, and there is
2407 * little to be gained by iterating.
2411 /* Once we see a label, insns become live again. */
2416 * Optimization can fold conditional branches to unconditional.
2417 * If we find a label with one reference which is preceded by
2418 * an unconditional branch to it, remove both. This needed to
2419 * wait until the dead code in between them was removed.
2421 if (label
->refs
== 1) {
2422 TCGOp
*op_prev
= QTAILQ_PREV(op
, link
);
2423 if (op_prev
->opc
== INDEX_op_br
&&
2424 label
== arg_label(op_prev
->args
[0])) {
2425 tcg_op_remove(s
, op_prev
);
2433 case INDEX_op_exit_tb
:
2434 case INDEX_op_goto_ptr
:
2435 /* Unconditional branches; everything following is dead. */
2440 /* Notice noreturn helper calls, raising exceptions. */
2441 call_flags
= op
->args
[TCGOP_CALLO(op
) + TCGOP_CALLI(op
) + 1];
2442 if (call_flags
& TCG_CALL_NO_RETURN
) {
2447 case INDEX_op_insn_start
:
2448 /* Never remove -- we need to keep these for unwind. */
2457 tcg_op_remove(s
, op
);
2465 #define IS_DEAD_ARG(n) (arg_life & (DEAD_ARG << (n)))
2466 #define NEED_SYNC_ARG(n) (arg_life & (SYNC_ARG << (n)))
2468 /* For liveness_pass_1, the register preferences for a given temp. */
2469 static inline TCGRegSet
*la_temp_pref(TCGTemp
*ts
)
2471 return ts
->state_ptr
;
2474 /* For liveness_pass_1, reset the preferences for a given temp to the
2475 * maximal regset for its type.
2477 static inline void la_reset_pref(TCGTemp
*ts
)
2480 = (ts
->state
== TS_DEAD
? 0 : tcg_target_available_regs
[ts
->type
]);
2483 /* liveness analysis: end of function: all temps are dead, and globals
2484 should be in memory. */
2485 static void la_func_end(TCGContext
*s
, int ng
, int nt
)
2489 for (i
= 0; i
< ng
; ++i
) {
2490 s
->temps
[i
].state
= TS_DEAD
| TS_MEM
;
2491 la_reset_pref(&s
->temps
[i
]);
2493 for (i
= ng
; i
< nt
; ++i
) {
2494 s
->temps
[i
].state
= TS_DEAD
;
2495 la_reset_pref(&s
->temps
[i
]);
2499 /* liveness analysis: end of basic block: all temps are dead, globals
2500 and local temps should be in memory. */
2501 static void la_bb_end(TCGContext
*s
, int ng
, int nt
)
2505 for (i
= 0; i
< ng
; ++i
) {
2506 s
->temps
[i
].state
= TS_DEAD
| TS_MEM
;
2507 la_reset_pref(&s
->temps
[i
]);
2509 for (i
= ng
; i
< nt
; ++i
) {
2510 s
->temps
[i
].state
= (s
->temps
[i
].temp_local
2513 la_reset_pref(&s
->temps
[i
]);
2517 /* liveness analysis: sync globals back to memory. */
2518 static void la_global_sync(TCGContext
*s
, int ng
)
2522 for (i
= 0; i
< ng
; ++i
) {
2523 int state
= s
->temps
[i
].state
;
2524 s
->temps
[i
].state
= state
| TS_MEM
;
2525 if (state
== TS_DEAD
) {
2526 /* If the global was previously dead, reset prefs. */
2527 la_reset_pref(&s
->temps
[i
]);
2532 /* liveness analysis: sync globals back to memory and kill. */
2533 static void la_global_kill(TCGContext
*s
, int ng
)
2537 for (i
= 0; i
< ng
; i
++) {
2538 s
->temps
[i
].state
= TS_DEAD
| TS_MEM
;
2539 la_reset_pref(&s
->temps
[i
]);
2543 /* liveness analysis: note live globals crossing calls. */
2544 static void la_cross_call(TCGContext
*s
, int nt
)
2546 TCGRegSet mask
= ~tcg_target_call_clobber_regs
;
2549 for (i
= 0; i
< nt
; i
++) {
2550 TCGTemp
*ts
= &s
->temps
[i
];
2551 if (!(ts
->state
& TS_DEAD
)) {
2552 TCGRegSet
*pset
= la_temp_pref(ts
);
2553 TCGRegSet set
= *pset
;
2556 /* If the combination is not possible, restart. */
2558 set
= tcg_target_available_regs
[ts
->type
] & mask
;
2565 /* Liveness analysis : update the opc_arg_life array to tell if a
2566 given input arguments is dead. Instructions updating dead
2567 temporaries are removed. */
2568 static void liveness_pass_1(TCGContext
*s
)
2570 int nb_globals
= s
->nb_globals
;
2571 int nb_temps
= s
->nb_temps
;
2572 TCGOp
*op
, *op_prev
;
2576 prefs
= tcg_malloc(sizeof(TCGRegSet
) * nb_temps
);
2577 for (i
= 0; i
< nb_temps
; ++i
) {
2578 s
->temps
[i
].state_ptr
= prefs
+ i
;
2581 /* ??? Should be redundant with the exit_tb that ends the TB. */
2582 la_func_end(s
, nb_globals
, nb_temps
);
2584 QTAILQ_FOREACH_REVERSE_SAFE(op
, &s
->ops
, link
, op_prev
) {
2585 int nb_iargs
, nb_oargs
;
2586 TCGOpcode opc_new
, opc_new2
;
2588 TCGLifeData arg_life
= 0;
2590 TCGOpcode opc
= op
->opc
;
2591 const TCGOpDef
*def
= &tcg_op_defs
[opc
];
2599 nb_oargs
= TCGOP_CALLO(op
);
2600 nb_iargs
= TCGOP_CALLI(op
);
2601 call_flags
= op
->args
[nb_oargs
+ nb_iargs
+ 1];
2603 /* pure functions can be removed if their result is unused */
2604 if (call_flags
& TCG_CALL_NO_SIDE_EFFECTS
) {
2605 for (i
= 0; i
< nb_oargs
; i
++) {
2606 ts
= arg_temp(op
->args
[i
]);
2607 if (ts
->state
!= TS_DEAD
) {
2608 goto do_not_remove_call
;
2615 /* Output args are dead. */
2616 for (i
= 0; i
< nb_oargs
; i
++) {
2617 ts
= arg_temp(op
->args
[i
]);
2618 if (ts
->state
& TS_DEAD
) {
2619 arg_life
|= DEAD_ARG
<< i
;
2621 if (ts
->state
& TS_MEM
) {
2622 arg_life
|= SYNC_ARG
<< i
;
2624 ts
->state
= TS_DEAD
;
2627 /* Not used -- it will be tcg_target_call_oarg_regs[i]. */
2628 op
->output_pref
[i
] = 0;
2631 if (!(call_flags
& (TCG_CALL_NO_WRITE_GLOBALS
|
2632 TCG_CALL_NO_READ_GLOBALS
))) {
2633 la_global_kill(s
, nb_globals
);
2634 } else if (!(call_flags
& TCG_CALL_NO_READ_GLOBALS
)) {
2635 la_global_sync(s
, nb_globals
);
2638 /* Record arguments that die in this helper. */
2639 for (i
= nb_oargs
; i
< nb_iargs
+ nb_oargs
; i
++) {
2640 ts
= arg_temp(op
->args
[i
]);
2641 if (ts
&& ts
->state
& TS_DEAD
) {
2642 arg_life
|= DEAD_ARG
<< i
;
2646 /* For all live registers, remove call-clobbered prefs. */
2647 la_cross_call(s
, nb_temps
);
2649 nb_call_regs
= ARRAY_SIZE(tcg_target_call_iarg_regs
);
2651 /* Input arguments are live for preceding opcodes. */
2652 for (i
= 0; i
< nb_iargs
; i
++) {
2653 ts
= arg_temp(op
->args
[i
+ nb_oargs
]);
2654 if (ts
&& ts
->state
& TS_DEAD
) {
2655 /* For those arguments that die, and will be allocated
2656 * in registers, clear the register set for that arg,
2657 * to be filled in below. For args that will be on
2658 * the stack, reset to any available reg.
2661 = (i
< nb_call_regs
? 0 :
2662 tcg_target_available_regs
[ts
->type
]);
2663 ts
->state
&= ~TS_DEAD
;
2667 /* For each input argument, add its input register to prefs.
2668 If a temp is used once, this produces a single set bit. */
2669 for (i
= 0; i
< MIN(nb_call_regs
, nb_iargs
); i
++) {
2670 ts
= arg_temp(op
->args
[i
+ nb_oargs
]);
2672 tcg_regset_set_reg(*la_temp_pref(ts
),
2673 tcg_target_call_iarg_regs
[i
]);
2678 case INDEX_op_insn_start
:
2680 case INDEX_op_discard
:
2681 /* mark the temporary as dead */
2682 ts
= arg_temp(op
->args
[0]);
2683 ts
->state
= TS_DEAD
;
2687 case INDEX_op_add2_i32
:
2688 opc_new
= INDEX_op_add_i32
;
2690 case INDEX_op_sub2_i32
:
2691 opc_new
= INDEX_op_sub_i32
;
2693 case INDEX_op_add2_i64
:
2694 opc_new
= INDEX_op_add_i64
;
2696 case INDEX_op_sub2_i64
:
2697 opc_new
= INDEX_op_sub_i64
;
2701 /* Test if the high part of the operation is dead, but not
2702 the low part. The result can be optimized to a simple
2703 add or sub. This happens often for x86_64 guest when the
2704 cpu mode is set to 32 bit. */
2705 if (arg_temp(op
->args
[1])->state
== TS_DEAD
) {
2706 if (arg_temp(op
->args
[0])->state
== TS_DEAD
) {
2709 /* Replace the opcode and adjust the args in place,
2710 leaving 3 unused args at the end. */
2711 op
->opc
= opc
= opc_new
;
2712 op
->args
[1] = op
->args
[2];
2713 op
->args
[2] = op
->args
[4];
2714 /* Fall through and mark the single-word operation live. */
2720 case INDEX_op_mulu2_i32
:
2721 opc_new
= INDEX_op_mul_i32
;
2722 opc_new2
= INDEX_op_muluh_i32
;
2723 have_opc_new2
= TCG_TARGET_HAS_muluh_i32
;
2725 case INDEX_op_muls2_i32
:
2726 opc_new
= INDEX_op_mul_i32
;
2727 opc_new2
= INDEX_op_mulsh_i32
;
2728 have_opc_new2
= TCG_TARGET_HAS_mulsh_i32
;
2730 case INDEX_op_mulu2_i64
:
2731 opc_new
= INDEX_op_mul_i64
;
2732 opc_new2
= INDEX_op_muluh_i64
;
2733 have_opc_new2
= TCG_TARGET_HAS_muluh_i64
;
2735 case INDEX_op_muls2_i64
:
2736 opc_new
= INDEX_op_mul_i64
;
2737 opc_new2
= INDEX_op_mulsh_i64
;
2738 have_opc_new2
= TCG_TARGET_HAS_mulsh_i64
;
2743 if (arg_temp(op
->args
[1])->state
== TS_DEAD
) {
2744 if (arg_temp(op
->args
[0])->state
== TS_DEAD
) {
2745 /* Both parts of the operation are dead. */
2748 /* The high part of the operation is dead; generate the low. */
2749 op
->opc
= opc
= opc_new
;
2750 op
->args
[1] = op
->args
[2];
2751 op
->args
[2] = op
->args
[3];
2752 } else if (arg_temp(op
->args
[0])->state
== TS_DEAD
&& have_opc_new2
) {
2753 /* The low part of the operation is dead; generate the high. */
2754 op
->opc
= opc
= opc_new2
;
2755 op
->args
[0] = op
->args
[1];
2756 op
->args
[1] = op
->args
[2];
2757 op
->args
[2] = op
->args
[3];
2761 /* Mark the single-word operation live. */
2766 /* XXX: optimize by hardcoding common cases (e.g. triadic ops) */
2767 nb_iargs
= def
->nb_iargs
;
2768 nb_oargs
= def
->nb_oargs
;
2770 /* Test if the operation can be removed because all
2771 its outputs are dead. We assume that nb_oargs == 0
2772 implies side effects */
2773 if (!(def
->flags
& TCG_OPF_SIDE_EFFECTS
) && nb_oargs
!= 0) {
2774 for (i
= 0; i
< nb_oargs
; i
++) {
2775 if (arg_temp(op
->args
[i
])->state
!= TS_DEAD
) {
2784 tcg_op_remove(s
, op
);
2788 for (i
= 0; i
< nb_oargs
; i
++) {
2789 ts
= arg_temp(op
->args
[i
]);
2791 /* Remember the preference of the uses that followed. */
2792 op
->output_pref
[i
] = *la_temp_pref(ts
);
2794 /* Output args are dead. */
2795 if (ts
->state
& TS_DEAD
) {
2796 arg_life
|= DEAD_ARG
<< i
;
2798 if (ts
->state
& TS_MEM
) {
2799 arg_life
|= SYNC_ARG
<< i
;
2801 ts
->state
= TS_DEAD
;
2805 /* If end of basic block, update. */
2806 if (def
->flags
& TCG_OPF_BB_EXIT
) {
2807 la_func_end(s
, nb_globals
, nb_temps
);
2808 } else if (def
->flags
& TCG_OPF_BB_END
) {
2809 la_bb_end(s
, nb_globals
, nb_temps
);
2810 } else if (def
->flags
& TCG_OPF_SIDE_EFFECTS
) {
2811 la_global_sync(s
, nb_globals
);
2812 if (def
->flags
& TCG_OPF_CALL_CLOBBER
) {
2813 la_cross_call(s
, nb_temps
);
2817 /* Record arguments that die in this opcode. */
2818 for (i
= nb_oargs
; i
< nb_oargs
+ nb_iargs
; i
++) {
2819 ts
= arg_temp(op
->args
[i
]);
2820 if (ts
->state
& TS_DEAD
) {
2821 arg_life
|= DEAD_ARG
<< i
;
2825 /* Input arguments are live for preceding opcodes. */
2826 for (i
= nb_oargs
; i
< nb_oargs
+ nb_iargs
; i
++) {
2827 ts
= arg_temp(op
->args
[i
]);
2828 if (ts
->state
& TS_DEAD
) {
2829 /* For operands that were dead, initially allow
2830 all regs for the type. */
2831 *la_temp_pref(ts
) = tcg_target_available_regs
[ts
->type
];
2832 ts
->state
&= ~TS_DEAD
;
2836 /* Incorporate constraints for this operand. */
2838 case INDEX_op_mov_i32
:
2839 case INDEX_op_mov_i64
:
2840 /* Note that these are TCG_OPF_NOT_PRESENT and do not
2841 have proper constraints. That said, special case
2842 moves to propagate preferences backward. */
2843 if (IS_DEAD_ARG(1)) {
2844 *la_temp_pref(arg_temp(op
->args
[0]))
2845 = *la_temp_pref(arg_temp(op
->args
[1]));
2850 for (i
= nb_oargs
; i
< nb_oargs
+ nb_iargs
; i
++) {
2851 const TCGArgConstraint
*ct
= &def
->args_ct
[i
];
2852 TCGRegSet set
, *pset
;
2854 ts
= arg_temp(op
->args
[i
]);
2855 pset
= la_temp_pref(ts
);
2859 if (ct
->ct
& TCG_CT_IALIAS
) {
2860 set
&= op
->output_pref
[ct
->alias_index
];
2862 /* If the combination is not possible, restart. */
2872 op
->life
= arg_life
;
2876 /* Liveness analysis: Convert indirect regs to direct temporaries. */
2877 static bool liveness_pass_2(TCGContext
*s
)
2879 int nb_globals
= s
->nb_globals
;
2881 bool changes
= false;
2882 TCGOp
*op
, *op_next
;
2884 /* Create a temporary for each indirect global. */
2885 for (i
= 0; i
< nb_globals
; ++i
) {
2886 TCGTemp
*its
= &s
->temps
[i
];
2887 if (its
->indirect_reg
) {
2888 TCGTemp
*dts
= tcg_temp_alloc(s
);
2889 dts
->type
= its
->type
;
2890 dts
->base_type
= its
->base_type
;
2891 its
->state_ptr
= dts
;
2893 its
->state_ptr
= NULL
;
2895 /* All globals begin dead. */
2896 its
->state
= TS_DEAD
;
2898 for (nb_temps
= s
->nb_temps
; i
< nb_temps
; ++i
) {
2899 TCGTemp
*its
= &s
->temps
[i
];
2900 its
->state_ptr
= NULL
;
2901 its
->state
= TS_DEAD
;
2904 QTAILQ_FOREACH_SAFE(op
, &s
->ops
, link
, op_next
) {
2905 TCGOpcode opc
= op
->opc
;
2906 const TCGOpDef
*def
= &tcg_op_defs
[opc
];
2907 TCGLifeData arg_life
= op
->life
;
2908 int nb_iargs
, nb_oargs
, call_flags
;
2909 TCGTemp
*arg_ts
, *dir_ts
;
2911 if (opc
== INDEX_op_call
) {
2912 nb_oargs
= TCGOP_CALLO(op
);
2913 nb_iargs
= TCGOP_CALLI(op
);
2914 call_flags
= op
->args
[nb_oargs
+ nb_iargs
+ 1];
2916 nb_iargs
= def
->nb_iargs
;
2917 nb_oargs
= def
->nb_oargs
;
2919 /* Set flags similar to how calls require. */
2920 if (def
->flags
& TCG_OPF_BB_END
) {
2921 /* Like writing globals: save_globals */
2923 } else if (def
->flags
& TCG_OPF_SIDE_EFFECTS
) {
2924 /* Like reading globals: sync_globals */
2925 call_flags
= TCG_CALL_NO_WRITE_GLOBALS
;
2927 /* No effect on globals. */
2928 call_flags
= (TCG_CALL_NO_READ_GLOBALS
|
2929 TCG_CALL_NO_WRITE_GLOBALS
);
2933 /* Make sure that input arguments are available. */
2934 for (i
= nb_oargs
; i
< nb_iargs
+ nb_oargs
; i
++) {
2935 arg_ts
= arg_temp(op
->args
[i
]);
2937 dir_ts
= arg_ts
->state_ptr
;
2938 if (dir_ts
&& arg_ts
->state
== TS_DEAD
) {
2939 TCGOpcode lopc
= (arg_ts
->type
== TCG_TYPE_I32
2942 TCGOp
*lop
= tcg_op_insert_before(s
, op
, lopc
);
2944 lop
->args
[0] = temp_arg(dir_ts
);
2945 lop
->args
[1] = temp_arg(arg_ts
->mem_base
);
2946 lop
->args
[2] = arg_ts
->mem_offset
;
2948 /* Loaded, but synced with memory. */
2949 arg_ts
->state
= TS_MEM
;
2954 /* Perform input replacement, and mark inputs that became dead.
2955 No action is required except keeping temp_state up to date
2956 so that we reload when needed. */
2957 for (i
= nb_oargs
; i
< nb_iargs
+ nb_oargs
; i
++) {
2958 arg_ts
= arg_temp(op
->args
[i
]);
2960 dir_ts
= arg_ts
->state_ptr
;
2962 op
->args
[i
] = temp_arg(dir_ts
);
2964 if (IS_DEAD_ARG(i
)) {
2965 arg_ts
->state
= TS_DEAD
;
2971 /* Liveness analysis should ensure that the following are
2972 all correct, for call sites and basic block end points. */
2973 if (call_flags
& TCG_CALL_NO_READ_GLOBALS
) {
2975 } else if (call_flags
& TCG_CALL_NO_WRITE_GLOBALS
) {
2976 for (i
= 0; i
< nb_globals
; ++i
) {
2977 /* Liveness should see that globals are synced back,
2978 that is, either TS_DEAD or TS_MEM. */
2979 arg_ts
= &s
->temps
[i
];
2980 tcg_debug_assert(arg_ts
->state_ptr
== 0
2981 || arg_ts
->state
!= 0);
2984 for (i
= 0; i
< nb_globals
; ++i
) {
2985 /* Liveness should see that globals are saved back,
2986 that is, TS_DEAD, waiting to be reloaded. */
2987 arg_ts
= &s
->temps
[i
];
2988 tcg_debug_assert(arg_ts
->state_ptr
== 0
2989 || arg_ts
->state
== TS_DEAD
);
2993 /* Outputs become available. */
2994 if (opc
== INDEX_op_mov_i32
|| opc
== INDEX_op_mov_i64
) {
2995 arg_ts
= arg_temp(op
->args
[0]);
2996 dir_ts
= arg_ts
->state_ptr
;
2998 op
->args
[0] = temp_arg(dir_ts
);
3001 /* The output is now live and modified. */
3004 if (NEED_SYNC_ARG(0)) {
3005 TCGOpcode sopc
= (arg_ts
->type
== TCG_TYPE_I32
3008 TCGOp
*sop
= tcg_op_insert_after(s
, op
, sopc
);
3009 TCGTemp
*out_ts
= dir_ts
;
3011 if (IS_DEAD_ARG(0)) {
3012 out_ts
= arg_temp(op
->args
[1]);
3013 arg_ts
->state
= TS_DEAD
;
3014 tcg_op_remove(s
, op
);
3016 arg_ts
->state
= TS_MEM
;
3019 sop
->args
[0] = temp_arg(out_ts
);
3020 sop
->args
[1] = temp_arg(arg_ts
->mem_base
);
3021 sop
->args
[2] = arg_ts
->mem_offset
;
3023 tcg_debug_assert(!IS_DEAD_ARG(0));
3027 for (i
= 0; i
< nb_oargs
; i
++) {
3028 arg_ts
= arg_temp(op
->args
[i
]);
3029 dir_ts
= arg_ts
->state_ptr
;
3033 op
->args
[i
] = temp_arg(dir_ts
);
3036 /* The output is now live and modified. */
3039 /* Sync outputs upon their last write. */
3040 if (NEED_SYNC_ARG(i
)) {
3041 TCGOpcode sopc
= (arg_ts
->type
== TCG_TYPE_I32
3044 TCGOp
*sop
= tcg_op_insert_after(s
, op
, sopc
);
3046 sop
->args
[0] = temp_arg(dir_ts
);
3047 sop
->args
[1] = temp_arg(arg_ts
->mem_base
);
3048 sop
->args
[2] = arg_ts
->mem_offset
;
3050 arg_ts
->state
= TS_MEM
;
3052 /* Drop outputs that are dead. */
3053 if (IS_DEAD_ARG(i
)) {
3054 arg_ts
->state
= TS_DEAD
;
3063 #ifdef CONFIG_DEBUG_TCG
3064 static void dump_regs(TCGContext
*s
)
3070 for(i
= 0; i
< s
->nb_temps
; i
++) {
3072 printf(" %10s: ", tcg_get_arg_str_ptr(s
, buf
, sizeof(buf
), ts
));
3073 switch(ts
->val_type
) {
3075 printf("%s", tcg_target_reg_names
[ts
->reg
]);
3078 printf("%d(%s)", (int)ts
->mem_offset
,
3079 tcg_target_reg_names
[ts
->mem_base
->reg
]);
3081 case TEMP_VAL_CONST
:
3082 printf("$0x%" TCG_PRIlx
, ts
->val
);
3094 for(i
= 0; i
< TCG_TARGET_NB_REGS
; i
++) {
3095 if (s
->reg_to_temp
[i
] != NULL
) {
3097 tcg_target_reg_names
[i
],
3098 tcg_get_arg_str_ptr(s
, buf
, sizeof(buf
), s
->reg_to_temp
[i
]));
3103 static void check_regs(TCGContext
*s
)
3110 for (reg
= 0; reg
< TCG_TARGET_NB_REGS
; reg
++) {
3111 ts
= s
->reg_to_temp
[reg
];
3113 if (ts
->val_type
!= TEMP_VAL_REG
|| ts
->reg
!= reg
) {
3114 printf("Inconsistency for register %s:\n",
3115 tcg_target_reg_names
[reg
]);
3120 for (k
= 0; k
< s
->nb_temps
; k
++) {
3122 if (ts
->val_type
== TEMP_VAL_REG
&& !ts
->fixed_reg
3123 && s
->reg_to_temp
[ts
->reg
] != ts
) {
3124 printf("Inconsistency for temp %s:\n",
3125 tcg_get_arg_str_ptr(s
, buf
, sizeof(buf
), ts
));
3127 printf("reg state:\n");
3135 static void temp_allocate_frame(TCGContext
*s
, TCGTemp
*ts
)
3137 #if !(defined(__sparc__) && TCG_TARGET_REG_BITS == 64)
3138 /* Sparc64 stack is accessed with offset of 2047 */
3139 s
->current_frame_offset
= (s
->current_frame_offset
+
3140 (tcg_target_long
)sizeof(tcg_target_long
) - 1) &
3141 ~(sizeof(tcg_target_long
) - 1);
3143 if (s
->current_frame_offset
+ (tcg_target_long
)sizeof(tcg_target_long
) >
3147 ts
->mem_offset
= s
->current_frame_offset
;
3148 ts
->mem_base
= s
->frame_temp
;
3149 ts
->mem_allocated
= 1;
3150 s
->current_frame_offset
+= sizeof(tcg_target_long
);
3153 static void temp_load(TCGContext
*, TCGTemp
*, TCGRegSet
, TCGRegSet
, TCGRegSet
);
3155 /* Mark a temporary as free or dead. If 'free_or_dead' is negative,
3156 mark it free; otherwise mark it dead. */
3157 static void temp_free_or_dead(TCGContext
*s
, TCGTemp
*ts
, int free_or_dead
)
3159 if (ts
->fixed_reg
) {
3162 if (ts
->val_type
== TEMP_VAL_REG
) {
3163 s
->reg_to_temp
[ts
->reg
] = NULL
;
3165 ts
->val_type
= (free_or_dead
< 0
3168 ? TEMP_VAL_MEM
: TEMP_VAL_DEAD
);
3171 /* Mark a temporary as dead. */
3172 static inline void temp_dead(TCGContext
*s
, TCGTemp
*ts
)
3174 temp_free_or_dead(s
, ts
, 1);
3177 /* Sync a temporary to memory. 'allocated_regs' is used in case a temporary
3178 registers needs to be allocated to store a constant. If 'free_or_dead'
3179 is non-zero, subsequently release the temporary; if it is positive, the
3180 temp is dead; if it is negative, the temp is free. */
3181 static void temp_sync(TCGContext
*s
, TCGTemp
*ts
, TCGRegSet allocated_regs
,
3182 TCGRegSet preferred_regs
, int free_or_dead
)
3184 if (ts
->fixed_reg
) {
3187 if (!ts
->mem_coherent
) {
3188 if (!ts
->mem_allocated
) {
3189 temp_allocate_frame(s
, ts
);
3191 switch (ts
->val_type
) {
3192 case TEMP_VAL_CONST
:
3193 /* If we're going to free the temp immediately, then we won't
3194 require it later in a register, so attempt to store the
3195 constant to memory directly. */
3197 && tcg_out_sti(s
, ts
->type
, ts
->val
,
3198 ts
->mem_base
->reg
, ts
->mem_offset
)) {
3201 temp_load(s
, ts
, tcg_target_available_regs
[ts
->type
],
3202 allocated_regs
, preferred_regs
);
3206 tcg_out_st(s
, ts
->type
, ts
->reg
,
3207 ts
->mem_base
->reg
, ts
->mem_offset
);
3217 ts
->mem_coherent
= 1;
3220 temp_free_or_dead(s
, ts
, free_or_dead
);
3224 /* free register 'reg' by spilling the corresponding temporary if necessary */
3225 static void tcg_reg_free(TCGContext
*s
, TCGReg reg
, TCGRegSet allocated_regs
)
3227 TCGTemp
*ts
= s
->reg_to_temp
[reg
];
3229 temp_sync(s
, ts
, allocated_regs
, 0, -1);
3235 * @required_regs: Set of registers in which we must allocate.
3236 * @allocated_regs: Set of registers which must be avoided.
3237 * @preferred_regs: Set of registers we should prefer.
3238 * @rev: True if we search the registers in "indirect" order.
3240 * The allocated register must be in @required_regs & ~@allocated_regs,
3241 * but if we can put it in @preferred_regs we may save a move later.
3243 static TCGReg
tcg_reg_alloc(TCGContext
*s
, TCGRegSet required_regs
,
3244 TCGRegSet allocated_regs
,
3245 TCGRegSet preferred_regs
, bool rev
)
3247 int i
, j
, f
, n
= ARRAY_SIZE(tcg_target_reg_alloc_order
);
3248 TCGRegSet reg_ct
[2];
3251 reg_ct
[1] = required_regs
& ~allocated_regs
;
3252 tcg_debug_assert(reg_ct
[1] != 0);
3253 reg_ct
[0] = reg_ct
[1] & preferred_regs
;
3255 /* Skip the preferred_regs option if it cannot be satisfied,
3256 or if the preference made no difference. */
3257 f
= reg_ct
[0] == 0 || reg_ct
[0] == reg_ct
[1];
3259 order
= rev
? indirect_reg_alloc_order
: tcg_target_reg_alloc_order
;
3261 /* Try free registers, preferences first. */
3262 for (j
= f
; j
< 2; j
++) {
3263 TCGRegSet set
= reg_ct
[j
];
3265 if (tcg_regset_single(set
)) {
3266 /* One register in the set. */
3267 TCGReg reg
= tcg_regset_first(set
);
3268 if (s
->reg_to_temp
[reg
] == NULL
) {
3272 for (i
= 0; i
< n
; i
++) {
3273 TCGReg reg
= order
[i
];
3274 if (s
->reg_to_temp
[reg
] == NULL
&&
3275 tcg_regset_test_reg(set
, reg
)) {
3282 /* We must spill something. */
3283 for (j
= f
; j
< 2; j
++) {
3284 TCGRegSet set
= reg_ct
[j
];
3286 if (tcg_regset_single(set
)) {
3287 /* One register in the set. */
3288 TCGReg reg
= tcg_regset_first(set
);
3289 tcg_reg_free(s
, reg
, allocated_regs
);
3292 for (i
= 0; i
< n
; i
++) {
3293 TCGReg reg
= order
[i
];
3294 if (tcg_regset_test_reg(set
, reg
)) {
3295 tcg_reg_free(s
, reg
, allocated_regs
);
3305 /* Make sure the temporary is in a register. If needed, allocate the register
3306 from DESIRED while avoiding ALLOCATED. */
3307 static void temp_load(TCGContext
*s
, TCGTemp
*ts
, TCGRegSet desired_regs
,
3308 TCGRegSet allocated_regs
, TCGRegSet preferred_regs
)
3312 switch (ts
->val_type
) {
3315 case TEMP_VAL_CONST
:
3316 reg
= tcg_reg_alloc(s
, desired_regs
, allocated_regs
,
3317 preferred_regs
, ts
->indirect_base
);
3318 tcg_out_movi(s
, ts
->type
, reg
, ts
->val
);
3319 ts
->mem_coherent
= 0;
3322 reg
= tcg_reg_alloc(s
, desired_regs
, allocated_regs
,
3323 preferred_regs
, ts
->indirect_base
);
3324 tcg_out_ld(s
, ts
->type
, reg
, ts
->mem_base
->reg
, ts
->mem_offset
);
3325 ts
->mem_coherent
= 1;
3332 ts
->val_type
= TEMP_VAL_REG
;
3333 s
->reg_to_temp
[reg
] = ts
;
3336 /* Save a temporary to memory. 'allocated_regs' is used in case a
3337 temporary registers needs to be allocated to store a constant. */
3338 static void temp_save(TCGContext
*s
, TCGTemp
*ts
, TCGRegSet allocated_regs
)
3340 /* The liveness analysis already ensures that globals are back
3341 in memory. Keep an tcg_debug_assert for safety. */
3342 tcg_debug_assert(ts
->val_type
== TEMP_VAL_MEM
|| ts
->fixed_reg
);
3345 /* save globals to their canonical location and assume they can be
3346 modified be the following code. 'allocated_regs' is used in case a
3347 temporary registers needs to be allocated to store a constant. */
3348 static void save_globals(TCGContext
*s
, TCGRegSet allocated_regs
)
3352 for (i
= 0, n
= s
->nb_globals
; i
< n
; i
++) {
3353 temp_save(s
, &s
->temps
[i
], allocated_regs
);
3357 /* sync globals to their canonical location and assume they can be
3358 read by the following code. 'allocated_regs' is used in case a
3359 temporary registers needs to be allocated to store a constant. */
3360 static void sync_globals(TCGContext
*s
, TCGRegSet allocated_regs
)
3364 for (i
= 0, n
= s
->nb_globals
; i
< n
; i
++) {
3365 TCGTemp
*ts
= &s
->temps
[i
];
3366 tcg_debug_assert(ts
->val_type
!= TEMP_VAL_REG
3368 || ts
->mem_coherent
);
3372 /* at the end of a basic block, we assume all temporaries are dead and
3373 all globals are stored at their canonical location. */
3374 static void tcg_reg_alloc_bb_end(TCGContext
*s
, TCGRegSet allocated_regs
)
3378 for (i
= s
->nb_globals
; i
< s
->nb_temps
; i
++) {
3379 TCGTemp
*ts
= &s
->temps
[i
];
3380 if (ts
->temp_local
) {
3381 temp_save(s
, ts
, allocated_regs
);
3383 /* The liveness analysis already ensures that temps are dead.
3384 Keep an tcg_debug_assert for safety. */
3385 tcg_debug_assert(ts
->val_type
== TEMP_VAL_DEAD
);
3389 save_globals(s
, allocated_regs
);
3393 * Specialized code generation for INDEX_op_movi_*.
3395 static void tcg_reg_alloc_do_movi(TCGContext
*s
, TCGTemp
*ots
,
3396 tcg_target_ulong val
, TCGLifeData arg_life
,
3397 TCGRegSet preferred_regs
)
3399 /* ENV should not be modified. */
3400 tcg_debug_assert(!ots
->fixed_reg
);
3402 /* The movi is not explicitly generated here. */
3403 if (ots
->val_type
== TEMP_VAL_REG
) {
3404 s
->reg_to_temp
[ots
->reg
] = NULL
;
3406 ots
->val_type
= TEMP_VAL_CONST
;
3408 ots
->mem_coherent
= 0;
3409 if (NEED_SYNC_ARG(0)) {
3410 temp_sync(s
, ots
, s
->reserved_regs
, preferred_regs
, IS_DEAD_ARG(0));
3411 } else if (IS_DEAD_ARG(0)) {
3416 static void tcg_reg_alloc_movi(TCGContext
*s
, const TCGOp
*op
)
3418 TCGTemp
*ots
= arg_temp(op
->args
[0]);
3419 tcg_target_ulong val
= op
->args
[1];
3421 tcg_reg_alloc_do_movi(s
, ots
, val
, op
->life
, op
->output_pref
[0]);
3425 * Specialized code generation for INDEX_op_mov_*.
3427 static void tcg_reg_alloc_mov(TCGContext
*s
, const TCGOp
*op
)
3429 const TCGLifeData arg_life
= op
->life
;
3430 TCGRegSet allocated_regs
, preferred_regs
;
3432 TCGType otype
, itype
;
3434 allocated_regs
= s
->reserved_regs
;
3435 preferred_regs
= op
->output_pref
[0];
3436 ots
= arg_temp(op
->args
[0]);
3437 ts
= arg_temp(op
->args
[1]);
3439 /* ENV should not be modified. */
3440 tcg_debug_assert(!ots
->fixed_reg
);
3442 /* Note that otype != itype for no-op truncation. */
3446 if (ts
->val_type
== TEMP_VAL_CONST
) {
3447 /* propagate constant or generate sti */
3448 tcg_target_ulong val
= ts
->val
;
3449 if (IS_DEAD_ARG(1)) {
3452 tcg_reg_alloc_do_movi(s
, ots
, val
, arg_life
, preferred_regs
);
3456 /* If the source value is in memory we're going to be forced
3457 to have it in a register in order to perform the copy. Copy
3458 the SOURCE value into its own register first, that way we
3459 don't have to reload SOURCE the next time it is used. */
3460 if (ts
->val_type
== TEMP_VAL_MEM
) {
3461 temp_load(s
, ts
, tcg_target_available_regs
[itype
],
3462 allocated_regs
, preferred_regs
);
3465 tcg_debug_assert(ts
->val_type
== TEMP_VAL_REG
);
3466 if (IS_DEAD_ARG(0)) {
3467 /* mov to a non-saved dead register makes no sense (even with
3468 liveness analysis disabled). */
3469 tcg_debug_assert(NEED_SYNC_ARG(0));
3470 if (!ots
->mem_allocated
) {
3471 temp_allocate_frame(s
, ots
);
3473 tcg_out_st(s
, otype
, ts
->reg
, ots
->mem_base
->reg
, ots
->mem_offset
);
3474 if (IS_DEAD_ARG(1)) {
3479 if (IS_DEAD_ARG(1) && !ts
->fixed_reg
) {
3480 /* the mov can be suppressed */
3481 if (ots
->val_type
== TEMP_VAL_REG
) {
3482 s
->reg_to_temp
[ots
->reg
] = NULL
;
3487 if (ots
->val_type
!= TEMP_VAL_REG
) {
3488 /* When allocating a new register, make sure to not spill the
3490 tcg_regset_set_reg(allocated_regs
, ts
->reg
);
3491 ots
->reg
= tcg_reg_alloc(s
, tcg_target_available_regs
[otype
],
3492 allocated_regs
, preferred_regs
,
3493 ots
->indirect_base
);
3495 if (!tcg_out_mov(s
, otype
, ots
->reg
, ts
->reg
)) {
3497 * Cross register class move not supported.
3498 * Store the source register into the destination slot
3499 * and leave the destination temp as TEMP_VAL_MEM.
3501 assert(!ots
->fixed_reg
);
3502 if (!ts
->mem_allocated
) {
3503 temp_allocate_frame(s
, ots
);
3505 tcg_out_st(s
, ts
->type
, ts
->reg
,
3506 ots
->mem_base
->reg
, ots
->mem_offset
);
3507 ots
->mem_coherent
= 1;
3508 temp_free_or_dead(s
, ots
, -1);
3512 ots
->val_type
= TEMP_VAL_REG
;
3513 ots
->mem_coherent
= 0;
3514 s
->reg_to_temp
[ots
->reg
] = ots
;
3515 if (NEED_SYNC_ARG(0)) {
3516 temp_sync(s
, ots
, allocated_regs
, 0, 0);
3522 * Specialized code generation for INDEX_op_dup_vec.
3524 static void tcg_reg_alloc_dup(TCGContext
*s
, const TCGOp
*op
)
3526 const TCGLifeData arg_life
= op
->life
;
3527 TCGRegSet dup_out_regs
, dup_in_regs
;
3529 TCGType itype
, vtype
;
3530 intptr_t endian_fixup
;
3534 ots
= arg_temp(op
->args
[0]);
3535 its
= arg_temp(op
->args
[1]);
3537 /* ENV should not be modified. */
3538 tcg_debug_assert(!ots
->fixed_reg
);
3541 vece
= TCGOP_VECE(op
);
3542 vtype
= TCGOP_VECL(op
) + TCG_TYPE_V64
;
3544 if (its
->val_type
== TEMP_VAL_CONST
) {
3545 /* Propagate constant via movi -> dupi. */
3546 tcg_target_ulong val
= its
->val
;
3547 if (IS_DEAD_ARG(1)) {
3550 tcg_reg_alloc_do_movi(s
, ots
, val
, arg_life
, op
->output_pref
[0]);
3554 dup_out_regs
= tcg_op_defs
[INDEX_op_dup_vec
].args_ct
[0].u
.regs
;
3555 dup_in_regs
= tcg_op_defs
[INDEX_op_dup_vec
].args_ct
[1].u
.regs
;
3557 /* Allocate the output register now. */
3558 if (ots
->val_type
!= TEMP_VAL_REG
) {
3559 TCGRegSet allocated_regs
= s
->reserved_regs
;
3561 if (!IS_DEAD_ARG(1) && its
->val_type
== TEMP_VAL_REG
) {
3562 /* Make sure to not spill the input register. */
3563 tcg_regset_set_reg(allocated_regs
, its
->reg
);
3565 ots
->reg
= tcg_reg_alloc(s
, dup_out_regs
, allocated_regs
,
3566 op
->output_pref
[0], ots
->indirect_base
);
3567 ots
->val_type
= TEMP_VAL_REG
;
3568 ots
->mem_coherent
= 0;
3569 s
->reg_to_temp
[ots
->reg
] = ots
;
3572 switch (its
->val_type
) {
3575 * The dup constriaints must be broad, covering all possible VECE.
3576 * However, tcg_op_dup_vec() gets to see the VECE and we allow it
3577 * to fail, indicating that extra moves are required for that case.
3579 if (tcg_regset_test_reg(dup_in_regs
, its
->reg
)) {
3580 if (tcg_out_dup_vec(s
, vtype
, vece
, ots
->reg
, its
->reg
)) {
3583 /* Try again from memory or a vector input register. */
3585 if (!its
->mem_coherent
) {
3587 * The input register is not synced, and so an extra store
3588 * would be required to use memory. Attempt an integer-vector
3589 * register move first. We do not have a TCGRegSet for this.
3591 if (tcg_out_mov(s
, itype
, ots
->reg
, its
->reg
)) {
3594 /* Sync the temp back to its slot and load from there. */
3595 temp_sync(s
, its
, s
->reserved_regs
, 0, 0);
3600 #ifdef HOST_WORDS_BIGENDIAN
3601 endian_fixup
= itype
== TCG_TYPE_I32
? 4 : 8;
3602 endian_fixup
-= 1 << vece
;
3606 if (tcg_out_dupm_vec(s
, vtype
, vece
, ots
->reg
, its
->mem_base
->reg
,
3607 its
->mem_offset
+ endian_fixup
)) {
3610 tcg_out_ld(s
, itype
, ots
->reg
, its
->mem_base
->reg
, its
->mem_offset
);
3614 g_assert_not_reached();
3617 /* We now have a vector input register, so dup must succeed. */
3618 ok
= tcg_out_dup_vec(s
, vtype
, vece
, ots
->reg
, ots
->reg
);
3619 tcg_debug_assert(ok
);
3622 if (IS_DEAD_ARG(1)) {
3625 if (NEED_SYNC_ARG(0)) {
3626 temp_sync(s
, ots
, s
->reserved_regs
, 0, 0);
3628 if (IS_DEAD_ARG(0)) {
3633 static void tcg_reg_alloc_op(TCGContext
*s
, const TCGOp
*op
)
3635 const TCGLifeData arg_life
= op
->life
;
3636 const TCGOpDef
* const def
= &tcg_op_defs
[op
->opc
];
3637 TCGRegSet i_allocated_regs
;
3638 TCGRegSet o_allocated_regs
;
3639 int i
, k
, nb_iargs
, nb_oargs
;
3642 const TCGArgConstraint
*arg_ct
;
3644 TCGArg new_args
[TCG_MAX_OP_ARGS
];
3645 int const_args
[TCG_MAX_OP_ARGS
];
3647 nb_oargs
= def
->nb_oargs
;
3648 nb_iargs
= def
->nb_iargs
;
3650 /* copy constants */
3651 memcpy(new_args
+ nb_oargs
+ nb_iargs
,
3652 op
->args
+ nb_oargs
+ nb_iargs
,
3653 sizeof(TCGArg
) * def
->nb_cargs
);
3655 i_allocated_regs
= s
->reserved_regs
;
3656 o_allocated_regs
= s
->reserved_regs
;
3658 /* satisfy input constraints */
3659 for (k
= 0; k
< nb_iargs
; k
++) {
3660 TCGRegSet i_preferred_regs
, o_preferred_regs
;
3662 i
= def
->sorted_args
[nb_oargs
+ k
];
3664 arg_ct
= &def
->args_ct
[i
];
3667 if (ts
->val_type
== TEMP_VAL_CONST
3668 && tcg_target_const_match(ts
->val
, ts
->type
, arg_ct
)) {
3669 /* constant is OK for instruction */
3671 new_args
[i
] = ts
->val
;
3675 i_preferred_regs
= o_preferred_regs
= 0;
3676 if (arg_ct
->ct
& TCG_CT_IALIAS
) {
3677 o_preferred_regs
= op
->output_pref
[arg_ct
->alias_index
];
3678 if (ts
->fixed_reg
) {
3679 /* if fixed register, we must allocate a new register
3680 if the alias is not the same register */
3681 if (arg
!= op
->args
[arg_ct
->alias_index
]) {
3682 goto allocate_in_reg
;
3685 /* if the input is aliased to an output and if it is
3686 not dead after the instruction, we must allocate
3687 a new register and move it */
3688 if (!IS_DEAD_ARG(i
)) {
3689 goto allocate_in_reg
;
3692 /* check if the current register has already been allocated
3693 for another input aliased to an output */
3694 if (ts
->val_type
== TEMP_VAL_REG
) {
3697 for (k2
= 0 ; k2
< k
; k2
++) {
3698 i2
= def
->sorted_args
[nb_oargs
+ k2
];
3699 if ((def
->args_ct
[i2
].ct
& TCG_CT_IALIAS
) &&
3700 reg
== new_args
[i2
]) {
3701 goto allocate_in_reg
;
3705 i_preferred_regs
= o_preferred_regs
;
3709 temp_load(s
, ts
, arg_ct
->u
.regs
, i_allocated_regs
, i_preferred_regs
);
3712 if (tcg_regset_test_reg(arg_ct
->u
.regs
, reg
)) {
3713 /* nothing to do : the constraint is satisfied */
3716 /* allocate a new register matching the constraint
3717 and move the temporary register into it */
3718 temp_load(s
, ts
, tcg_target_available_regs
[ts
->type
],
3719 i_allocated_regs
, 0);
3720 reg
= tcg_reg_alloc(s
, arg_ct
->u
.regs
, i_allocated_regs
,
3721 o_preferred_regs
, ts
->indirect_base
);
3722 if (!tcg_out_mov(s
, ts
->type
, reg
, ts
->reg
)) {
3724 * Cross register class move not supported. Sync the
3725 * temp back to its slot and load from there.
3727 temp_sync(s
, ts
, i_allocated_regs
, 0, 0);
3728 tcg_out_ld(s
, ts
->type
, reg
,
3729 ts
->mem_base
->reg
, ts
->mem_offset
);
3734 tcg_regset_set_reg(i_allocated_regs
, reg
);
3737 /* mark dead temporaries and free the associated registers */
3738 for (i
= nb_oargs
; i
< nb_oargs
+ nb_iargs
; i
++) {
3739 if (IS_DEAD_ARG(i
)) {
3740 temp_dead(s
, arg_temp(op
->args
[i
]));
3744 if (def
->flags
& TCG_OPF_BB_END
) {
3745 tcg_reg_alloc_bb_end(s
, i_allocated_regs
);
3747 if (def
->flags
& TCG_OPF_CALL_CLOBBER
) {
3748 /* XXX: permit generic clobber register list ? */
3749 for (i
= 0; i
< TCG_TARGET_NB_REGS
; i
++) {
3750 if (tcg_regset_test_reg(tcg_target_call_clobber_regs
, i
)) {
3751 tcg_reg_free(s
, i
, i_allocated_regs
);
3755 if (def
->flags
& TCG_OPF_SIDE_EFFECTS
) {
3756 /* sync globals if the op has side effects and might trigger
3758 sync_globals(s
, i_allocated_regs
);
3761 /* satisfy the output constraints */
3762 for(k
= 0; k
< nb_oargs
; k
++) {
3763 i
= def
->sorted_args
[k
];
3765 arg_ct
= &def
->args_ct
[i
];
3768 /* ENV should not be modified. */
3769 tcg_debug_assert(!ts
->fixed_reg
);
3771 if ((arg_ct
->ct
& TCG_CT_ALIAS
)
3772 && !const_args
[arg_ct
->alias_index
]) {
3773 reg
= new_args
[arg_ct
->alias_index
];
3774 } else if (arg_ct
->ct
& TCG_CT_NEWREG
) {
3775 reg
= tcg_reg_alloc(s
, arg_ct
->u
.regs
,
3776 i_allocated_regs
| o_allocated_regs
,
3777 op
->output_pref
[k
], ts
->indirect_base
);
3779 reg
= tcg_reg_alloc(s
, arg_ct
->u
.regs
, o_allocated_regs
,
3780 op
->output_pref
[k
], ts
->indirect_base
);
3782 tcg_regset_set_reg(o_allocated_regs
, reg
);
3783 if (ts
->val_type
== TEMP_VAL_REG
) {
3784 s
->reg_to_temp
[ts
->reg
] = NULL
;
3786 ts
->val_type
= TEMP_VAL_REG
;
3789 * Temp value is modified, so the value kept in memory is
3790 * potentially not the same.
3792 ts
->mem_coherent
= 0;
3793 s
->reg_to_temp
[reg
] = ts
;
3798 /* emit instruction */
3799 if (def
->flags
& TCG_OPF_VECTOR
) {
3800 tcg_out_vec_op(s
, op
->opc
, TCGOP_VECL(op
), TCGOP_VECE(op
),
3801 new_args
, const_args
);
3803 tcg_out_op(s
, op
->opc
, new_args
, const_args
);
3806 /* move the outputs in the correct register if needed */
3807 for(i
= 0; i
< nb_oargs
; i
++) {
3808 ts
= arg_temp(op
->args
[i
]);
3810 /* ENV should not be modified. */
3811 tcg_debug_assert(!ts
->fixed_reg
);
3813 if (NEED_SYNC_ARG(i
)) {
3814 temp_sync(s
, ts
, o_allocated_regs
, 0, IS_DEAD_ARG(i
));
3815 } else if (IS_DEAD_ARG(i
)) {
3821 #ifdef TCG_TARGET_STACK_GROWSUP
3822 #define STACK_DIR(x) (-(x))
3824 #define STACK_DIR(x) (x)
3827 static void tcg_reg_alloc_call(TCGContext
*s
, TCGOp
*op
)
3829 const int nb_oargs
= TCGOP_CALLO(op
);
3830 const int nb_iargs
= TCGOP_CALLI(op
);
3831 const TCGLifeData arg_life
= op
->life
;
3832 int flags
, nb_regs
, i
;
3836 intptr_t stack_offset
;
3837 size_t call_stack_size
;
3838 tcg_insn_unit
*func_addr
;
3840 TCGRegSet allocated_regs
;
3842 func_addr
= (tcg_insn_unit
*)(intptr_t)op
->args
[nb_oargs
+ nb_iargs
];
3843 flags
= op
->args
[nb_oargs
+ nb_iargs
+ 1];
3845 nb_regs
= ARRAY_SIZE(tcg_target_call_iarg_regs
);
3846 if (nb_regs
> nb_iargs
) {
3850 /* assign stack slots first */
3851 call_stack_size
= (nb_iargs
- nb_regs
) * sizeof(tcg_target_long
);
3852 call_stack_size
= (call_stack_size
+ TCG_TARGET_STACK_ALIGN
- 1) &
3853 ~(TCG_TARGET_STACK_ALIGN
- 1);
3854 allocate_args
= (call_stack_size
> TCG_STATIC_CALL_ARGS_SIZE
);
3855 if (allocate_args
) {
3856 /* XXX: if more than TCG_STATIC_CALL_ARGS_SIZE is needed,
3857 preallocate call stack */
3861 stack_offset
= TCG_TARGET_CALL_STACK_OFFSET
;
3862 for (i
= nb_regs
; i
< nb_iargs
; i
++) {
3863 arg
= op
->args
[nb_oargs
+ i
];
3864 #ifdef TCG_TARGET_STACK_GROWSUP
3865 stack_offset
-= sizeof(tcg_target_long
);
3867 if (arg
!= TCG_CALL_DUMMY_ARG
) {
3869 temp_load(s
, ts
, tcg_target_available_regs
[ts
->type
],
3870 s
->reserved_regs
, 0);
3871 tcg_out_st(s
, ts
->type
, ts
->reg
, TCG_REG_CALL_STACK
, stack_offset
);
3873 #ifndef TCG_TARGET_STACK_GROWSUP
3874 stack_offset
+= sizeof(tcg_target_long
);
3878 /* assign input registers */
3879 allocated_regs
= s
->reserved_regs
;
3880 for (i
= 0; i
< nb_regs
; i
++) {
3881 arg
= op
->args
[nb_oargs
+ i
];
3882 if (arg
!= TCG_CALL_DUMMY_ARG
) {
3884 reg
= tcg_target_call_iarg_regs
[i
];
3886 if (ts
->val_type
== TEMP_VAL_REG
) {
3887 if (ts
->reg
!= reg
) {
3888 tcg_reg_free(s
, reg
, allocated_regs
);
3889 if (!tcg_out_mov(s
, ts
->type
, reg
, ts
->reg
)) {
3891 * Cross register class move not supported. Sync the
3892 * temp back to its slot and load from there.
3894 temp_sync(s
, ts
, allocated_regs
, 0, 0);
3895 tcg_out_ld(s
, ts
->type
, reg
,
3896 ts
->mem_base
->reg
, ts
->mem_offset
);
3900 TCGRegSet arg_set
= 0;
3902 tcg_reg_free(s
, reg
, allocated_regs
);
3903 tcg_regset_set_reg(arg_set
, reg
);
3904 temp_load(s
, ts
, arg_set
, allocated_regs
, 0);
3907 tcg_regset_set_reg(allocated_regs
, reg
);
3911 /* mark dead temporaries and free the associated registers */
3912 for (i
= nb_oargs
; i
< nb_iargs
+ nb_oargs
; i
++) {
3913 if (IS_DEAD_ARG(i
)) {
3914 temp_dead(s
, arg_temp(op
->args
[i
]));
3918 /* clobber call registers */
3919 for (i
= 0; i
< TCG_TARGET_NB_REGS
; i
++) {
3920 if (tcg_regset_test_reg(tcg_target_call_clobber_regs
, i
)) {
3921 tcg_reg_free(s
, i
, allocated_regs
);
3925 /* Save globals if they might be written by the helper, sync them if
3926 they might be read. */
3927 if (flags
& TCG_CALL_NO_READ_GLOBALS
) {
3929 } else if (flags
& TCG_CALL_NO_WRITE_GLOBALS
) {
3930 sync_globals(s
, allocated_regs
);
3932 save_globals(s
, allocated_regs
);
3935 tcg_out_call(s
, func_addr
);
3937 /* assign output registers and emit moves if needed */
3938 for(i
= 0; i
< nb_oargs
; i
++) {
3942 /* ENV should not be modified. */
3943 tcg_debug_assert(!ts
->fixed_reg
);
3945 reg
= tcg_target_call_oarg_regs
[i
];
3946 tcg_debug_assert(s
->reg_to_temp
[reg
] == NULL
);
3947 if (ts
->val_type
== TEMP_VAL_REG
) {
3948 s
->reg_to_temp
[ts
->reg
] = NULL
;
3950 ts
->val_type
= TEMP_VAL_REG
;
3952 ts
->mem_coherent
= 0;
3953 s
->reg_to_temp
[reg
] = ts
;
3954 if (NEED_SYNC_ARG(i
)) {
3955 temp_sync(s
, ts
, allocated_regs
, 0, IS_DEAD_ARG(i
));
3956 } else if (IS_DEAD_ARG(i
)) {
3962 #ifdef CONFIG_PROFILER
3964 /* avoid copy/paste errors */
3965 #define PROF_ADD(to, from, field) \
3967 (to)->field += atomic_read(&((from)->field)); \
3970 #define PROF_MAX(to, from, field) \
3972 typeof((from)->field) val__ = atomic_read(&((from)->field)); \
3973 if (val__ > (to)->field) { \
3974 (to)->field = val__; \
3978 /* Pass in a zero'ed @prof */
3980 void tcg_profile_snapshot(TCGProfile
*prof
, bool counters
, bool table
)
3982 unsigned int n_ctxs
= atomic_read(&n_tcg_ctxs
);
3985 for (i
= 0; i
< n_ctxs
; i
++) {
3986 TCGContext
*s
= atomic_read(&tcg_ctxs
[i
]);
3987 const TCGProfile
*orig
= &s
->prof
;
3990 PROF_ADD(prof
, orig
, cpu_exec_time
);
3991 PROF_ADD(prof
, orig
, tb_count1
);
3992 PROF_ADD(prof
, orig
, tb_count
);
3993 PROF_ADD(prof
, orig
, op_count
);
3994 PROF_MAX(prof
, orig
, op_count_max
);
3995 PROF_ADD(prof
, orig
, temp_count
);
3996 PROF_MAX(prof
, orig
, temp_count_max
);
3997 PROF_ADD(prof
, orig
, del_op_count
);
3998 PROF_ADD(prof
, orig
, code_in_len
);
3999 PROF_ADD(prof
, orig
, code_out_len
);
4000 PROF_ADD(prof
, orig
, search_out_len
);
4001 PROF_ADD(prof
, orig
, interm_time
);
4002 PROF_ADD(prof
, orig
, code_time
);
4003 PROF_ADD(prof
, orig
, la_time
);
4004 PROF_ADD(prof
, orig
, opt_time
);
4005 PROF_ADD(prof
, orig
, restore_count
);
4006 PROF_ADD(prof
, orig
, restore_time
);
4011 for (i
= 0; i
< NB_OPS
; i
++) {
4012 PROF_ADD(prof
, orig
, table_op_count
[i
]);
4021 static void tcg_profile_snapshot_counters(TCGProfile
*prof
)
4023 tcg_profile_snapshot(prof
, true, false);
4026 static void tcg_profile_snapshot_table(TCGProfile
*prof
)
4028 tcg_profile_snapshot(prof
, false, true);
4031 void tcg_dump_op_count(void)
4033 TCGProfile prof
= {};
4036 tcg_profile_snapshot_table(&prof
);
4037 for (i
= 0; i
< NB_OPS
; i
++) {
4038 qemu_printf("%s %" PRId64
"\n", tcg_op_defs
[i
].name
,
4039 prof
.table_op_count
[i
]);
4043 int64_t tcg_cpu_exec_time(void)
4045 unsigned int n_ctxs
= atomic_read(&n_tcg_ctxs
);
4049 for (i
= 0; i
< n_ctxs
; i
++) {
4050 const TCGContext
*s
= atomic_read(&tcg_ctxs
[i
]);
4051 const TCGProfile
*prof
= &s
->prof
;
4053 ret
+= atomic_read(&prof
->cpu_exec_time
);
4058 void tcg_dump_op_count(void)
4060 qemu_printf("[TCG profiler not compiled]\n");
4063 int64_t tcg_cpu_exec_time(void)
4065 error_report("%s: TCG profiler not compiled", __func__
);
4071 int tcg_gen_code(TCGContext
*s
, TranslationBlock
*tb
)
4073 #ifdef CONFIG_PROFILER
4074 TCGProfile
*prof
= &s
->prof
;
4079 #ifdef CONFIG_PROFILER
4083 QTAILQ_FOREACH(op
, &s
->ops
, link
) {
4086 atomic_set(&prof
->op_count
, prof
->op_count
+ n
);
4087 if (n
> prof
->op_count_max
) {
4088 atomic_set(&prof
->op_count_max
, n
);
4092 atomic_set(&prof
->temp_count
, prof
->temp_count
+ n
);
4093 if (n
> prof
->temp_count_max
) {
4094 atomic_set(&prof
->temp_count_max
, n
);
4100 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP
)
4101 && qemu_log_in_addr_range(tb
->pc
))) {
4102 FILE *logfile
= qemu_log_lock();
4104 tcg_dump_ops(s
, false);
4106 qemu_log_unlock(logfile
);
4110 #ifdef CONFIG_DEBUG_TCG
4111 /* Ensure all labels referenced have been emitted. */
4116 QSIMPLEQ_FOREACH(l
, &s
->labels
, next
) {
4117 if (unlikely(!l
->present
) && l
->refs
) {
4118 qemu_log_mask(CPU_LOG_TB_OP
,
4119 "$L%d referenced but not present.\n", l
->id
);
4127 #ifdef CONFIG_PROFILER
4128 atomic_set(&prof
->opt_time
, prof
->opt_time
- profile_getclock());
4131 #ifdef USE_TCG_OPTIMIZATIONS
4135 #ifdef CONFIG_PROFILER
4136 atomic_set(&prof
->opt_time
, prof
->opt_time
+ profile_getclock());
4137 atomic_set(&prof
->la_time
, prof
->la_time
- profile_getclock());
4140 reachable_code_pass(s
);
4143 if (s
->nb_indirects
> 0) {
4145 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_IND
)
4146 && qemu_log_in_addr_range(tb
->pc
))) {
4147 FILE *logfile
= qemu_log_lock();
4148 qemu_log("OP before indirect lowering:\n");
4149 tcg_dump_ops(s
, false);
4151 qemu_log_unlock(logfile
);
4154 /* Replace indirect temps with direct temps. */
4155 if (liveness_pass_2(s
)) {
4156 /* If changes were made, re-run liveness. */
4161 #ifdef CONFIG_PROFILER
4162 atomic_set(&prof
->la_time
, prof
->la_time
+ profile_getclock());
4166 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_OPT
)
4167 && qemu_log_in_addr_range(tb
->pc
))) {
4168 FILE *logfile
= qemu_log_lock();
4169 qemu_log("OP after optimization and liveness analysis:\n");
4170 tcg_dump_ops(s
, true);
4172 qemu_log_unlock(logfile
);
4176 tcg_reg_alloc_start(s
);
4178 s
->code_buf
= tb
->tc
.ptr
;
4179 s
->code_ptr
= tb
->tc
.ptr
;
4181 #ifdef TCG_TARGET_NEED_LDST_LABELS
4182 QSIMPLEQ_INIT(&s
->ldst_labels
);
4184 #ifdef TCG_TARGET_NEED_POOL_LABELS
4185 s
->pool_labels
= NULL
;
4189 QTAILQ_FOREACH(op
, &s
->ops
, link
) {
4190 TCGOpcode opc
= op
->opc
;
4192 #ifdef CONFIG_PROFILER
4193 atomic_set(&prof
->table_op_count
[opc
], prof
->table_op_count
[opc
] + 1);
4197 case INDEX_op_mov_i32
:
4198 case INDEX_op_mov_i64
:
4199 case INDEX_op_mov_vec
:
4200 tcg_reg_alloc_mov(s
, op
);
4202 case INDEX_op_movi_i32
:
4203 case INDEX_op_movi_i64
:
4204 case INDEX_op_dupi_vec
:
4205 tcg_reg_alloc_movi(s
, op
);
4207 case INDEX_op_dup_vec
:
4208 tcg_reg_alloc_dup(s
, op
);
4210 case INDEX_op_insn_start
:
4211 if (num_insns
>= 0) {
4212 size_t off
= tcg_current_code_size(s
);
4213 s
->gen_insn_end_off
[num_insns
] = off
;
4214 /* Assert that we do not overflow our stored offset. */
4215 assert(s
->gen_insn_end_off
[num_insns
] == off
);
4218 for (i
= 0; i
< TARGET_INSN_START_WORDS
; ++i
) {
4220 #if TARGET_LONG_BITS > TCG_TARGET_REG_BITS
4221 a
= deposit64(op
->args
[i
* 2], 32, 32, op
->args
[i
* 2 + 1]);
4225 s
->gen_insn_data
[num_insns
][i
] = a
;
4228 case INDEX_op_discard
:
4229 temp_dead(s
, arg_temp(op
->args
[0]));
4231 case INDEX_op_set_label
:
4232 tcg_reg_alloc_bb_end(s
, s
->reserved_regs
);
4233 tcg_out_label(s
, arg_label(op
->args
[0]), s
->code_ptr
);
4236 tcg_reg_alloc_call(s
, op
);
4239 /* Sanity check that we've not introduced any unhandled opcodes. */
4240 tcg_debug_assert(tcg_op_supported(opc
));
4241 /* Note: in order to speed up the code, it would be much
4242 faster to have specialized register allocator functions for
4243 some common argument patterns */
4244 tcg_reg_alloc_op(s
, op
);
4247 #ifdef CONFIG_DEBUG_TCG
4250 /* Test for (pending) buffer overflow. The assumption is that any
4251 one operation beginning below the high water mark cannot overrun
4252 the buffer completely. Thus we can test for overflow after
4253 generating code without having to check during generation. */
4254 if (unlikely((void *)s
->code_ptr
> s
->code_gen_highwater
)) {
4257 /* Test for TB overflow, as seen by gen_insn_end_off. */
4258 if (unlikely(tcg_current_code_size(s
) > UINT16_MAX
)) {
4262 tcg_debug_assert(num_insns
>= 0);
4263 s
->gen_insn_end_off
[num_insns
] = tcg_current_code_size(s
);
4265 /* Generate TB finalization at the end of block */
4266 #ifdef TCG_TARGET_NEED_LDST_LABELS
4267 i
= tcg_out_ldst_finalize(s
);
4272 #ifdef TCG_TARGET_NEED_POOL_LABELS
4273 i
= tcg_out_pool_finalize(s
);
4278 if (!tcg_resolve_relocs(s
)) {
4282 /* flush instruction cache */
4283 flush_icache_range((uintptr_t)s
->code_buf
, (uintptr_t)s
->code_ptr
);
4285 return tcg_current_code_size(s
);
4288 #ifdef CONFIG_PROFILER
4289 void tcg_dump_info(void)
4291 TCGProfile prof
= {};
4292 const TCGProfile
*s
;
4294 int64_t tb_div_count
;
4297 tcg_profile_snapshot_counters(&prof
);
4299 tb_count
= s
->tb_count
;
4300 tb_div_count
= tb_count
? tb_count
: 1;
4301 tot
= s
->interm_time
+ s
->code_time
;
4303 qemu_printf("JIT cycles %" PRId64
" (%0.3f s at 2.4 GHz)\n",
4305 qemu_printf("translated TBs %" PRId64
" (aborted=%" PRId64
4307 tb_count
, s
->tb_count1
- tb_count
,
4308 (double)(s
->tb_count1
- s
->tb_count
)
4309 / (s
->tb_count1
? s
->tb_count1
: 1) * 100.0);
4310 qemu_printf("avg ops/TB %0.1f max=%d\n",
4311 (double)s
->op_count
/ tb_div_count
, s
->op_count_max
);
4312 qemu_printf("deleted ops/TB %0.2f\n",
4313 (double)s
->del_op_count
/ tb_div_count
);
4314 qemu_printf("avg temps/TB %0.2f max=%d\n",
4315 (double)s
->temp_count
/ tb_div_count
, s
->temp_count_max
);
4316 qemu_printf("avg host code/TB %0.1f\n",
4317 (double)s
->code_out_len
/ tb_div_count
);
4318 qemu_printf("avg search data/TB %0.1f\n",
4319 (double)s
->search_out_len
/ tb_div_count
);
4321 qemu_printf("cycles/op %0.1f\n",
4322 s
->op_count
? (double)tot
/ s
->op_count
: 0);
4323 qemu_printf("cycles/in byte %0.1f\n",
4324 s
->code_in_len
? (double)tot
/ s
->code_in_len
: 0);
4325 qemu_printf("cycles/out byte %0.1f\n",
4326 s
->code_out_len
? (double)tot
/ s
->code_out_len
: 0);
4327 qemu_printf("cycles/search byte %0.1f\n",
4328 s
->search_out_len
? (double)tot
/ s
->search_out_len
: 0);
4332 qemu_printf(" gen_interm time %0.1f%%\n",
4333 (double)s
->interm_time
/ tot
* 100.0);
4334 qemu_printf(" gen_code time %0.1f%%\n",
4335 (double)s
->code_time
/ tot
* 100.0);
4336 qemu_printf("optim./code time %0.1f%%\n",
4337 (double)s
->opt_time
/ (s
->code_time
? s
->code_time
: 1)
4339 qemu_printf("liveness/code time %0.1f%%\n",
4340 (double)s
->la_time
/ (s
->code_time
? s
->code_time
: 1) * 100.0);
4341 qemu_printf("cpu_restore count %" PRId64
"\n",
4343 qemu_printf(" avg cycles %0.1f\n",
4344 s
->restore_count
? (double)s
->restore_time
/ s
->restore_count
: 0);
4347 void tcg_dump_info(void)
4349 qemu_printf("[TCG profiler not compiled]\n");
4353 #ifdef ELF_HOST_MACHINE
4354 /* In order to use this feature, the backend needs to do three things:
4356 (1) Define ELF_HOST_MACHINE to indicate both what value to
4357 put into the ELF image and to indicate support for the feature.
4359 (2) Define tcg_register_jit. This should create a buffer containing
4360 the contents of a .debug_frame section that describes the post-
4361 prologue unwind info for the tcg machine.
4363 (3) Call tcg_register_jit_int, with the constructed .debug_frame.
4366 /* Begin GDB interface. THE FOLLOWING MUST MATCH GDB DOCS. */
4373 struct jit_code_entry
{
4374 struct jit_code_entry
*next_entry
;
4375 struct jit_code_entry
*prev_entry
;
4376 const void *symfile_addr
;
4377 uint64_t symfile_size
;
4380 struct jit_descriptor
{
4382 uint32_t action_flag
;
4383 struct jit_code_entry
*relevant_entry
;
4384 struct jit_code_entry
*first_entry
;
4387 void __jit_debug_register_code(void) __attribute__((noinline
));
4388 void __jit_debug_register_code(void)
4393 /* Must statically initialize the version, because GDB may check
4394 the version before we can set it. */
4395 struct jit_descriptor __jit_debug_descriptor
= { 1, 0, 0, 0 };
4397 /* End GDB interface. */
4399 static int find_string(const char *strtab
, const char *str
)
4401 const char *p
= strtab
+ 1;
4404 if (strcmp(p
, str
) == 0) {
4411 static void tcg_register_jit_int(void *buf_ptr
, size_t buf_size
,
4412 const void *debug_frame
,
4413 size_t debug_frame_size
)
4415 struct __attribute__((packed
)) DebugInfo
{
4422 uintptr_t cu_low_pc
;
4423 uintptr_t cu_high_pc
;
4426 uintptr_t fn_low_pc
;
4427 uintptr_t fn_high_pc
;
4436 struct DebugInfo di
;
4441 struct ElfImage
*img
;
4443 static const struct ElfImage img_template
= {
4445 .e_ident
[EI_MAG0
] = ELFMAG0
,
4446 .e_ident
[EI_MAG1
] = ELFMAG1
,
4447 .e_ident
[EI_MAG2
] = ELFMAG2
,
4448 .e_ident
[EI_MAG3
] = ELFMAG3
,
4449 .e_ident
[EI_CLASS
] = ELF_CLASS
,
4450 .e_ident
[EI_DATA
] = ELF_DATA
,
4451 .e_ident
[EI_VERSION
] = EV_CURRENT
,
4453 .e_machine
= ELF_HOST_MACHINE
,
4454 .e_version
= EV_CURRENT
,
4455 .e_phoff
= offsetof(struct ElfImage
, phdr
),
4456 .e_shoff
= offsetof(struct ElfImage
, shdr
),
4457 .e_ehsize
= sizeof(ElfW(Shdr
)),
4458 .e_phentsize
= sizeof(ElfW(Phdr
)),
4460 .e_shentsize
= sizeof(ElfW(Shdr
)),
4461 .e_shnum
= ARRAY_SIZE(img
->shdr
),
4462 .e_shstrndx
= ARRAY_SIZE(img
->shdr
) - 1,
4463 #ifdef ELF_HOST_FLAGS
4464 .e_flags
= ELF_HOST_FLAGS
,
4467 .e_ident
[EI_OSABI
] = ELF_OSABI
,
4475 [0] = { .sh_type
= SHT_NULL
},
4476 /* Trick: The contents of code_gen_buffer are not present in
4477 this fake ELF file; that got allocated elsewhere. Therefore
4478 we mark .text as SHT_NOBITS (similar to .bss) so that readers
4479 will not look for contents. We can record any address. */
4481 .sh_type
= SHT_NOBITS
,
4482 .sh_flags
= SHF_EXECINSTR
| SHF_ALLOC
,
4484 [2] = { /* .debug_info */
4485 .sh_type
= SHT_PROGBITS
,
4486 .sh_offset
= offsetof(struct ElfImage
, di
),
4487 .sh_size
= sizeof(struct DebugInfo
),
4489 [3] = { /* .debug_abbrev */
4490 .sh_type
= SHT_PROGBITS
,
4491 .sh_offset
= offsetof(struct ElfImage
, da
),
4492 .sh_size
= sizeof(img
->da
),
4494 [4] = { /* .debug_frame */
4495 .sh_type
= SHT_PROGBITS
,
4496 .sh_offset
= sizeof(struct ElfImage
),
4498 [5] = { /* .symtab */
4499 .sh_type
= SHT_SYMTAB
,
4500 .sh_offset
= offsetof(struct ElfImage
, sym
),
4501 .sh_size
= sizeof(img
->sym
),
4503 .sh_link
= ARRAY_SIZE(img
->shdr
) - 1,
4504 .sh_entsize
= sizeof(ElfW(Sym
)),
4506 [6] = { /* .strtab */
4507 .sh_type
= SHT_STRTAB
,
4508 .sh_offset
= offsetof(struct ElfImage
, str
),
4509 .sh_size
= sizeof(img
->str
),
4513 [1] = { /* code_gen_buffer */
4514 .st_info
= ELF_ST_INFO(STB_GLOBAL
, STT_FUNC
),
4519 .len
= sizeof(struct DebugInfo
) - 4,
4521 .ptr_size
= sizeof(void *),
4523 .cu_lang
= 0x8001, /* DW_LANG_Mips_Assembler */
4525 .fn_name
= "code_gen_buffer"
4528 1, /* abbrev number (the cu) */
4529 0x11, 1, /* DW_TAG_compile_unit, has children */
4530 0x13, 0x5, /* DW_AT_language, DW_FORM_data2 */
4531 0x11, 0x1, /* DW_AT_low_pc, DW_FORM_addr */
4532 0x12, 0x1, /* DW_AT_high_pc, DW_FORM_addr */
4533 0, 0, /* end of abbrev */
4534 2, /* abbrev number (the fn) */
4535 0x2e, 0, /* DW_TAG_subprogram, no children */
4536 0x3, 0x8, /* DW_AT_name, DW_FORM_string */
4537 0x11, 0x1, /* DW_AT_low_pc, DW_FORM_addr */
4538 0x12, 0x1, /* DW_AT_high_pc, DW_FORM_addr */
4539 0, 0, /* end of abbrev */
4540 0 /* no more abbrev */
4542 .str
= "\0" ".text\0" ".debug_info\0" ".debug_abbrev\0"
4543 ".debug_frame\0" ".symtab\0" ".strtab\0" "code_gen_buffer",
4546 /* We only need a single jit entry; statically allocate it. */
4547 static struct jit_code_entry one_entry
;
4549 uintptr_t buf
= (uintptr_t)buf_ptr
;
4550 size_t img_size
= sizeof(struct ElfImage
) + debug_frame_size
;
4551 DebugFrameHeader
*dfh
;
4553 img
= g_malloc(img_size
);
4554 *img
= img_template
;
4556 img
->phdr
.p_vaddr
= buf
;
4557 img
->phdr
.p_paddr
= buf
;
4558 img
->phdr
.p_memsz
= buf_size
;
4560 img
->shdr
[1].sh_name
= find_string(img
->str
, ".text");
4561 img
->shdr
[1].sh_addr
= buf
;
4562 img
->shdr
[1].sh_size
= buf_size
;
4564 img
->shdr
[2].sh_name
= find_string(img
->str
, ".debug_info");
4565 img
->shdr
[3].sh_name
= find_string(img
->str
, ".debug_abbrev");
4567 img
->shdr
[4].sh_name
= find_string(img
->str
, ".debug_frame");
4568 img
->shdr
[4].sh_size
= debug_frame_size
;
4570 img
->shdr
[5].sh_name
= find_string(img
->str
, ".symtab");
4571 img
->shdr
[6].sh_name
= find_string(img
->str
, ".strtab");
4573 img
->sym
[1].st_name
= find_string(img
->str
, "code_gen_buffer");
4574 img
->sym
[1].st_value
= buf
;
4575 img
->sym
[1].st_size
= buf_size
;
4577 img
->di
.cu_low_pc
= buf
;
4578 img
->di
.cu_high_pc
= buf
+ buf_size
;
4579 img
->di
.fn_low_pc
= buf
;
4580 img
->di
.fn_high_pc
= buf
+ buf_size
;
4582 dfh
= (DebugFrameHeader
*)(img
+ 1);
4583 memcpy(dfh
, debug_frame
, debug_frame_size
);
4584 dfh
->fde
.func_start
= buf
;
4585 dfh
->fde
.func_len
= buf_size
;
4588 /* Enable this block to be able to debug the ELF image file creation.
4589 One can use readelf, objdump, or other inspection utilities. */
4591 FILE *f
= fopen("/tmp/qemu.jit", "w+b");
4593 if (fwrite(img
, img_size
, 1, f
) != img_size
) {
4594 /* Avoid stupid unused return value warning for fwrite. */
4601 one_entry
.symfile_addr
= img
;
4602 one_entry
.symfile_size
= img_size
;
4604 __jit_debug_descriptor
.action_flag
= JIT_REGISTER_FN
;
4605 __jit_debug_descriptor
.relevant_entry
= &one_entry
;
4606 __jit_debug_descriptor
.first_entry
= &one_entry
;
4607 __jit_debug_register_code();
4610 /* No support for the feature. Provide the entry point expected by exec.c,
4611 and implement the internal function we declared earlier. */
4613 static void tcg_register_jit_int(void *buf
, size_t size
,
4614 const void *debug_frame
,
4615 size_t debug_frame_size
)
4619 void tcg_register_jit(void *buf
, size_t buf_size
)
4622 #endif /* ELF_HOST_MACHINE */
4624 #if !TCG_TARGET_MAYBE_vec
4625 void tcg_expand_vec_op(TCGOpcode o
, TCGType t
, unsigned e
, TCGArg a0
, ...)
4627 g_assert_not_reached();