tests: add missing genh dependency
[qemu/kevin.git] / tcg / tcg.c
blob284b1c2a156f8e6d5565c5f61dd3d8f0da8d1d97
1 /*
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
22 * THE SOFTWARE.
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. */
31 #undef DEBUG_JIT
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
41 instructions */
42 #define NO_CPU_IO_DEFS
43 #include "cpu.h"
45 #include "exec/exec-all.h"
47 #if !defined(CONFIG_USER_ONLY)
48 #include "hw/boards.h"
49 #endif
51 #include "tcg/tcg-op.h"
53 #if UINTPTR_MAX == UINT32_MAX
54 # define ELF_CLASS ELFCLASS32
55 #else
56 # define ELF_CLASS ELFCLASS64
57 #endif
58 #ifdef HOST_WORDS_BIGENDIAN
59 # define ELF_DATA ELFDATA2MSB
60 #else
61 # define ELF_DATA ELFDATA2LSB
62 #endif
64 #include "elf.h"
65 #include "exec/log.h"
66 #include "sysemu/sysemu.h"
68 /* Forward declarations for functions declared in tcg-target.c.inc and
69 used here. */
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. */
77 typedef struct {
78 uint32_t len __attribute__((aligned((sizeof(void *)))));
79 uint32_t id;
80 uint8_t version;
81 char augmentation[1];
82 uint8_t code_align;
83 uint8_t data_align;
84 uint8_t return_column;
85 } DebugFrameCIE;
87 typedef struct QEMU_PACKED {
88 uint32_t len __attribute__((aligned((sizeof(void *)))));
89 uint32_t cie_offset;
90 uintptr_t func_start;
91 uintptr_t func_len;
92 } DebugFrameFDEHeader;
94 typedef struct QEMU_PACKED {
95 DebugFrameCIE cie;
96 DebugFrameFDEHeader fde;
97 } DebugFrameHeader;
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,
108 intptr_t arg2);
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);
124 #else
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();
146 #endif
147 static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg, TCGReg arg1,
148 intptr_t arg2);
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);
156 #endif
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 {
165 QemuMutex lock;
166 GTree *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 {
177 QemuMutex lock;
179 /* fields set at init time */
180 void *start;
181 void *start_aligned;
182 void *end;
183 size_t n;
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)
206 *s->code_ptr++ = v;
209 static __attribute__((unused)) inline void tcg_patch8(tcg_insn_unit *p,
210 uint8_t v)
212 *p = v;
214 #endif
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) {
220 *s->code_ptr++ = v;
221 } else {
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,
229 uint16_t v)
231 if (TCG_TARGET_INSN_UNIT_SIZE == 2) {
232 *p = v;
233 } else {
234 memcpy(p, &v, sizeof(v));
237 #endif
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) {
243 *s->code_ptr++ = v;
244 } else {
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,
252 uint32_t v)
254 if (TCG_TARGET_INSN_UNIT_SIZE == 4) {
255 *p = v;
256 } else {
257 memcpy(p, &v, sizeof(v));
260 #endif
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) {
266 *s->code_ptr++ = v;
267 } else {
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,
275 uint64_t v)
277 if (TCG_TARGET_INSN_UNIT_SIZE == 8) {
278 *p = v;
279 } else {
280 memcpy(p, &v, sizeof(v));
283 #endif
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));
292 r->type = type;
293 r->ptr = code_ptr;
294 r->addend = addend;
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);
301 l->has_value = 1;
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);
316 return l;
319 static bool tcg_resolve_relocs(TCGContext *s)
321 TCGLabel *l;
323 QSIMPLEQ_FOREACH(l, &s->labels, next) {
324 TCGRelocation *r;
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)) {
329 return false;
333 return true;
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) {
350 return 1;
351 } else if (ptr < s->ptr) {
352 return -1;
354 return 0;
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) {
369 return 1;
370 } else if (a->ptr < b->ptr) {
371 return -1;
373 /* a->ptr == b->ptr should happen only on deletions */
374 g_assert(a->size == b->size);
375 return 0;
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)
390 size_t i;
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)
404 size_t region_idx;
406 if (p < region.start_aligned) {
407 region_idx = 0;
408 } else {
409 ptrdiff_t offset = p - region.start_aligned;
411 if (offset > region.stride * (region.n - 1)) {
412 region_idx = region.n - 1;
413 } else {
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);
452 return tb;
455 static void tcg_region_tree_lock_all(void)
457 size_t i;
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)
468 size_t i;
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)
479 size_t i;
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)
492 size_t nb_tbs = 0;
493 size_t i;
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();
502 return nb_tbs;
505 static gboolean tcg_region_tree_traverse(gpointer k, gpointer v, gpointer data)
507 TranslationBlock *tb = v;
509 tb_destroy(tb);
510 return FALSE;
513 static void tcg_region_tree_reset_all(void)
515 size_t i;
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)
531 void *start, *end;
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) {
540 end = region.end;
543 *pstart = start;
544 *pend = end;
547 static void tcg_region_assign(TCGContext *s, size_t curr_region)
549 void *start, *end;
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) {
562 return true;
564 tcg_region_assign(s, region.current);
565 region.current++;
566 return false;
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)
575 bool err;
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(&region.lock);
580 err = tcg_region_alloc__locked(s);
581 if (!err) {
582 region.agg_size_full += size_full - TCG_HIGHWATER;
584 qemu_mutex_unlock(&region.lock);
585 return err;
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 = qatomic_read(&n_tcg_ctxs);
601 unsigned int i;
603 qemu_mutex_lock(&region.lock);
604 region.current = 0;
605 region.agg_size_full = 0;
607 for (i = 0; i < n_ctxs; i++) {
608 TCGContext *s = qatomic_read(&tcg_ctxs[i]);
609 bool err = tcg_region_initial_alloc__locked(s);
611 g_assert(!err);
613 qemu_mutex_unlock(&region.lock);
615 tcg_region_tree_reset_all();
618 #ifdef CONFIG_USER_ONLY
619 static size_t tcg_n_regions(void)
621 return 1;
623 #else
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)
632 size_t i;
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;
638 #endif
639 if (max_cpus == 1 || !qemu_tcg_mttcg_enabled()) {
640 return 1;
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;
646 size_t region_size;
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 */
656 return max_cpus;
658 #endif
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;
691 void *aligned;
692 size_t size = tcg_init_ctx.code_gen_buffer_size;
693 size_t page_size = qemu_real_host_page_size;
694 size_t region_size;
695 size_t n_regions;
696 size_t i;
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(&region.lock);
716 region.n = n_regions;
717 region.size = region_size - page_size;
718 region.stride = region_size;
719 region.start = buf;
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++) {
728 void *start, *end;
729 int rc;
731 tcg_region_bounds(i, &start, &end);
732 rc = qemu_mprotect_none(end, page_size);
733 g_assert(!rc);
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);
743 g_assert(!err);
745 #endif
748 static void alloc_tcg_plugin_context(TCGContext *s)
750 #ifdef CONFIG_PLUGIN
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);
754 #endif
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;
777 #else
778 void tcg_register_thread(void)
780 MachineState *ms = MACHINE(qdev_get_machine());
781 TCGContext *s = g_malloc(sizeof(*s));
782 unsigned int i, n;
783 bool err;
785 *s = tcg_init_ctx;
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 = qatomic_fetch_inc(&n_tcg_ctxs);
798 g_assert(n < ms->smp.max_cpus);
799 qatomic_set(&tcg_ctxs[n], s);
801 if (n > 0) {
802 alloc_tcg_plugin_context(s);
805 tcg_ctx = s;
806 qemu_mutex_lock(&region.lock);
807 err = tcg_region_initial_alloc__locked(tcg_ctx);
808 g_assert(!err);
809 qemu_mutex_unlock(&region.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
818 * TCG context.
820 size_t tcg_code_size(void)
822 unsigned int n_ctxs = qatomic_read(&n_tcg_ctxs);
823 unsigned int i;
824 size_t total;
826 qemu_mutex_lock(&region.lock);
827 total = region.agg_size_full;
828 for (i = 0; i < n_ctxs; i++) {
829 const TCGContext *s = qatomic_read(&tcg_ctxs[i]);
830 size_t size;
832 size = qatomic_read(&s->code_gen_ptr) - s->code_gen_buffer;
833 g_assert(size <= s->code_gen_buffer_size);
834 total += size;
836 qemu_mutex_unlock(&region.lock);
837 return total;
841 * Returns the code capacity (in bytes) of the entire cache, i.e. including all
842 * regions.
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);
853 return capacity;
856 size_t tcg_tb_phys_invalidate_count(void)
858 unsigned int n_ctxs = qatomic_read(&n_tcg_ctxs);
859 unsigned int i;
860 size_t total = 0;
862 for (i = 0; i < n_ctxs; i++) {
863 const TCGContext *s = qatomic_read(&tcg_ctxs[i]);
865 total += qatomic_read(&s->tb_phys_invalidate_count);
867 return total;
870 /* pool based memory allocation */
871 void *tcg_malloc_internal(TCGContext *s, int size)
873 TCGPool *p;
874 int pool_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);
879 p->size = size;
880 p->next = s->pool_first_large;
881 s->pool_first_large = p;
882 return p->data;
883 } else {
884 p = s->pool_current;
885 if (!p) {
886 p = s->pool_first;
887 if (!p)
888 goto new_pool;
889 } else {
890 if (!p->next) {
891 new_pool:
892 pool_size = TCG_POOL_CHUNK_SIZE;
893 p = g_malloc(sizeof(TCGPool) + pool_size);
894 p->size = pool_size;
895 p->next = NULL;
896 if (s->pool_current)
897 s->pool_current->next = p;
898 else
899 s->pool_first = p;
900 } else {
901 p = p->next;
905 s->pool_current = p;
906 s->pool_cur = p->data + size;
907 s->pool_end = p->data + p->size;
908 return p->data;
911 void tcg_pool_reset(TCGContext *s)
913 TCGPool *p, *t;
914 for (p = s->pool_first_large; p; p = t) {
915 t = p->next;
916 g_free(p);
918 s->pool_first_large = NULL;
919 s->pool_cur = s->pool_end = NULL;
920 s->pool_current = NULL;
923 typedef struct TCGHelperInfo {
924 void *func;
925 const char *name;
926 unsigned flags;
927 unsigned sizemask;
928 } 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;
945 TCGOpDef *def;
946 TCGArgConstraint *args_ct;
947 int *sorted_args;
948 TCGTemp *ts;
950 memset(s, 0, sizeof(*s));
951 s->nb_globals = 0;
953 /* Count total number of arguments and allocate the corresponding
954 space */
955 total_args = 0;
956 for(op = 0; op < NB_OPS; op++) {
957 def = &tcg_op_defs[op];
958 n = def->nb_iargs + def->nb_oargs;
959 total_args += n;
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;
970 sorted_args += n;
971 args_ct += n;
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]);
983 tcg_target_init(s);
984 process_op_defs(s);
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)) {
991 break;
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);
1003 tcg_ctx = 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;
1012 n_tcg_ctxs = 1;
1013 #else
1014 MachineState *ms = MACHINE(qdev_get_machine());
1015 unsigned int max_cpus = ms->smp.max_cpus;
1016 tcg_ctxs = g_new(TCGContext *, max_cpus);
1017 #endif
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;
1032 void *next;
1034 retry:
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)) {
1040 return NULL;
1042 goto retry;
1044 qatomic_set(&s->code_gen_ptr, next);
1045 s->data_gen_ptr = NULL;
1046 return tb;
1049 void tcg_prologue_init(TCGContext *s)
1051 size_t prologue_size, total_size;
1052 void *buf0, *buf1;
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;
1057 s->code_ptr = buf0;
1058 s->code_buf = buf0;
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;
1069 #endif
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);
1080 #endif
1082 buf1 = s->code_ptr;
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;
1089 s->code_buf = buf1;
1090 total_size -= prologue_size;
1091 s->code_gen_buffer_size = total_size;
1093 tcg_register_jit(s->code_gen_buffer, total_size);
1095 #ifdef DEBUG_DISAS
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;
1102 size_t i;
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));
1111 } else {
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));
1117 } else {
1118 log_disas(buf0, prologue_size, NULL);
1120 qemu_log("\n");
1121 qemu_log_flush();
1122 qemu_log_unlock(logfile);
1124 #endif
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)
1134 tcg_pool_reset(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));
1140 s->nb_ops = 0;
1141 s->nb_labels = 0;
1142 s->current_frame_offset = s->frame_start;
1144 #ifdef CONFIG_DEBUG_TCG
1145 s->goto_tb_issue_mask = 0;
1146 #endif
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)
1162 TCGTemp *ts;
1164 tcg_debug_assert(s->nb_globals == s->nb_temps);
1165 s->nb_globals++;
1166 ts = tcg_temp_alloc(s);
1167 ts->temp_global = 1;
1169 return ts;
1172 static TCGTemp *tcg_global_reg_new_internal(TCGContext *s, TCGType type,
1173 TCGReg reg, const char *name)
1175 TCGTemp *ts;
1177 if (TCG_TARGET_REG_BITS == 32 && type != TCG_TYPE_I32) {
1178 tcg_abort();
1181 ts = tcg_global_alloc(s);
1182 ts->base_type = type;
1183 ts->type = type;
1184 ts->fixed_reg = 1;
1185 ts->reg = reg;
1186 ts->name = name;
1187 tcg_regset_set_reg(s->reserved_regs, reg);
1189 return ts;
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;
1196 s->frame_temp
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
1208 bigendian = 1;
1209 #endif
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
1216 ? 2 : 1);
1217 indirect_reg = 1;
1220 if (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64) {
1221 TCGTemp *ts2 = tcg_global_alloc(s);
1222 char buf[64];
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);
1244 } else {
1245 ts->base_type = type;
1246 ts->type = type;
1247 ts->indirect_reg = indirect_reg;
1248 ts->mem_allocated = 1;
1249 ts->mem_base = base_ts;
1250 ts->mem_offset = offset;
1251 ts->name = name;
1253 return ts;
1256 TCGTemp *tcg_temp_new_internal(TCGType type, bool temp_local)
1258 TCGContext *s = tcg_ctx;
1259 TCGTemp *ts;
1260 int idx, k;
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);
1272 } else {
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;
1287 } else {
1288 ts->base_type = type;
1289 ts->type = type;
1290 ts->temp_allocated = 1;
1291 ts->temp_local = temp_local;
1295 #if defined(CONFIG_DEBUG_TCG)
1296 s->temps_in_use++;
1297 #endif
1298 return ts;
1301 TCGv_vec tcg_temp_new_vec(TCGType type)
1303 TCGTemp *t;
1305 #ifdef CONFIG_DEBUG_TCG
1306 switch (type) {
1307 case TCG_TYPE_V64:
1308 assert(TCG_TARGET_HAS_v64);
1309 break;
1310 case TCG_TYPE_V128:
1311 assert(TCG_TARGET_HAS_v128);
1312 break;
1313 case TCG_TYPE_V256:
1314 assert(TCG_TARGET_HAS_v256);
1315 break;
1316 default:
1317 g_assert_not_reached();
1319 #endif
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;
1339 int k, idx;
1341 #if defined(CONFIG_DEBUG_TCG)
1342 s->temps_in_use--;
1343 if (s->temps_in_use < 0) {
1344 fprintf(stderr, "More temporaries freed than allocated!\n");
1346 #endif
1348 tcg_debug_assert(ts->temp_global == 0);
1349 tcg_debug_assert(ts->temp_allocated != 0);
1350 ts->temp_allocated = 0;
1352 idx = temp_idx(ts);
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)
1359 TCGv_i32 t0;
1360 t0 = tcg_temp_new_i32();
1361 tcg_gen_movi_i32(t0, val);
1362 return t0;
1365 TCGv_i64 tcg_const_i64(int64_t val)
1367 TCGv_i64 t0;
1368 t0 = tcg_temp_new_i64();
1369 tcg_gen_movi_i64(t0, val);
1370 return t0;
1373 TCGv_i32 tcg_const_local_i32(int32_t val)
1375 TCGv_i32 t0;
1376 t0 = tcg_temp_local_new_i32();
1377 tcg_gen_movi_i32(t0, val);
1378 return t0;
1381 TCGv_i64 tcg_const_local_i64(int64_t val)
1383 TCGv_i64 t0;
1384 t0 = tcg_temp_local_new_i64();
1385 tcg_gen_movi_i64(t0, val);
1386 return t0;
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;
1404 return 1;
1406 return 0;
1408 #endif
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)
1414 const bool have_vec
1415 = TCG_TARGET_HAS_v64 | TCG_TARGET_HAS_v128 | TCG_TARGET_HAS_v256;
1417 switch (op) {
1418 case INDEX_op_discard:
1419 case INDEX_op_set_label:
1420 case INDEX_op_call:
1421 case INDEX_op_br:
1422 case INDEX_op_mb:
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:
1430 return true;
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:
1456 return true;
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:
1646 return have_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;
1695 default:
1696 tcg_debug_assert(op > INDEX_op_last_generic && op < NB_OPS);
1697 return true;
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;
1709 TCGOp *op;
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;
1720 #endif
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];
1731 retl = NULL;
1732 reth = NULL;
1733 if (sizemask != 0) {
1734 for (i = real_args = 0; i < nargs; ++i) {
1735 int is_64bit = sizemask & (1 << (i+1)*2);
1736 if (is_64bit) {
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);
1743 } else {
1744 split_args[real_args++] = args[i];
1747 nargs = real_args;
1748 args = split_args;
1749 sizemask = 0;
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);
1755 if (!is_64bit) {
1756 TCGv_i64 temp = tcg_temp_new_i64();
1757 TCGv_i64 orig = temp_tcgv_i64(args[i]);
1758 if (is_signed) {
1759 tcg_gen_ext32s_i64(temp, orig);
1760 } else {
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);
1770 pi = 0;
1771 if (ret != NULL) {
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);
1782 nb_rets = 2;
1783 } else {
1784 op->args[pi++] = temp_arg(ret);
1785 nb_rets = 1;
1787 #else
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);
1792 #else
1793 op->args[pi++] = temp_arg(ret);
1794 op->args[pi++] = temp_arg(ret + 1);
1795 #endif
1796 nb_rets = 2;
1797 } else {
1798 op->args[pi++] = temp_arg(ret);
1799 nb_rets = 1;
1801 #endif
1802 } else {
1803 nb_rets = 0;
1805 TCGOP_CALLO(op) = nb_rets;
1807 real_args = 0;
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;
1815 real_args++;
1817 #endif
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]);
1831 #else
1832 op->args[pi++] = temp_arg(args[i]);
1833 op->args[pi++] = temp_arg(args[i] + 1);
1834 #endif
1835 real_args += 2;
1836 continue;
1839 op->args[pi++] = temp_arg(args[i]);
1840 real_args++;
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);
1855 if (is_64bit) {
1856 tcg_temp_free_internal(args[real_args++]);
1857 tcg_temp_free_internal(args[real_args++]);
1858 } else {
1859 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);
1873 if (!is_64bit) {
1874 tcg_temp_free_internal(args[i]);
1877 #endif /* TCG_TARGET_EXTEND_ARGS */
1880 static void tcg_reg_alloc_start(TCGContext *s)
1882 int i, n;
1883 TCGTemp *ts;
1885 for (i = 0, n = s->nb_globals; i < n; i++) {
1886 ts = &s->temps[i];
1887 ts->val_type = (ts->fixed_reg ? TEMP_VAL_REG : TEMP_VAL_MEM);
1889 for (n = s->nb_temps; i < n; i++) {
1890 ts = &s->temps[i];
1891 ts->val_type = (ts->temp_local ? TEMP_VAL_MEM : TEMP_VAL_DEAD);
1892 ts->mem_allocated = 0;
1893 ts->fixed_reg = 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,
1900 TCGTemp *ts)
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);
1908 } else {
1909 snprintf(buf, buf_size, "tmp%d", idx - s->nb_globals);
1911 return buf;
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;
1924 if (helper_table) {
1925 TCGHelperInfo *info = g_hash_table_lookup(helper_table, (gpointer)val);
1926 if (info) {
1927 ret = info->name;
1930 return ret;
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[] =
1951 [MO_UB] = "ub",
1952 [MO_SB] = "sb",
1953 [MO_LEUW] = "leuw",
1954 [MO_LESW] = "lesw",
1955 [MO_LEUL] = "leul",
1956 [MO_LESL] = "lesl",
1957 [MO_LEQ] = "leq",
1958 [MO_BEUW] = "beuw",
1959 [MO_BESW] = "besw",
1960 [MO_BEUL] = "beul",
1961 [MO_BESL] = "besl",
1962 [MO_BEQ] = "beq",
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] = "",
1969 #else
1970 [MO_UNALN >> MO_ASHIFT] = "",
1971 [MO_ALIGN >> MO_ASHIFT] = "al+",
1972 #endif
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) {
1989 return ctz32(d);
1990 } else {
1991 return ctz64(d);
1995 static void tcg_dump_ops(TCGContext *s, bool have_prefs)
1997 char buf[128];
1998 TCGOp *op;
2000 QTAILQ_FOREACH(op, &s->ops, link) {
2001 int i, k, nb_oargs, nb_iargs, nb_cargs;
2002 const TCGOpDef *def;
2003 TCGOpcode c;
2004 int col = 0;
2006 c = op->opc;
2007 def = &tcg_op_defs[c];
2009 if (c == INDEX_op_insn_start) {
2010 nb_oargs = 0;
2011 col += qemu_log("\n ----");
2013 for (i = 0; i < TARGET_INSN_START_WORDS; ++i) {
2014 target_ulong a;
2015 #if TARGET_LONG_BITS > TCG_TARGET_REG_BITS
2016 a = deposit64(op->args[i * 2], 32, 32, op->args[i * 2 + 1]);
2017 #else
2018 a = op->args[i];
2019 #endif
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),
2034 op->args[i]));
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);
2044 } else {
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));
2056 k = 0;
2057 for (i = 0; i < nb_oargs; i++) {
2058 if (k != 0) {
2059 col += qemu_log(",");
2061 col += qemu_log("%s", tcg_get_arg_str(s, buf, sizeof(buf),
2062 op->args[k++]));
2064 for (i = 0; i < nb_iargs; i++) {
2065 if (k != 0) {
2066 col += qemu_log(",");
2068 col += qemu_log("%s", tcg_get_arg_str(s, buf, sizeof(buf),
2069 op->args[k++]));
2071 switch (c) {
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++]]);
2085 } else {
2086 col += qemu_log(",$0x%" TCG_PRIlx, op->args[k++]);
2088 i = 1;
2089 break;
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);
2101 } else {
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);
2107 i = 1;
2109 break;
2110 default:
2111 i = 0;
2112 break;
2114 switch (c) {
2115 case INDEX_op_set_label:
2116 case INDEX_op_br:
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);
2122 i++, k++;
2123 break;
2124 default:
2125 break;
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;
2136 rcu_read_lock();
2137 logfile = qatomic_rcu_read(&qemu_logfile);
2138 if (logfile) {
2139 for (; col < 40; ++col) {
2140 putc(' ', logfile->fd);
2143 rcu_read_unlock();
2146 if (op->life) {
2147 unsigned life = op->life;
2149 if (life & (SYNC_ARG * 3)) {
2150 qemu_log(" sync:");
2151 for (i = 0; i < 2; ++i) {
2152 if (life & (SYNC_ARG << i)) {
2153 qemu_log(" %d", i);
2157 life /= DEAD_ARG;
2158 if (life) {
2159 qemu_log(" dead:");
2160 for (i = 0; life; ++i, life >>= 1) {
2161 if (life & 1) {
2162 qemu_log(" %d", i);
2168 if (have_prefs) {
2169 for (i = 0; i < nb_oargs; ++i) {
2170 TCGRegSet set = op->output_pref[i];
2172 if (i == 0) {
2173 qemu_log(" pref=");
2174 } else {
2175 qemu_log(",");
2177 if (set == 0) {
2178 qemu_log("none");
2179 } else if (set == MAKE_64BIT_MASK(0, TCG_TARGET_NB_REGS)) {
2180 qemu_log("all");
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]);
2185 #endif
2186 } else if (TCG_TARGET_NB_REGS <= 32) {
2187 qemu_log("%#x", (uint32_t)set);
2188 } else {
2189 qemu_log("%#" PRIx64, (uint64_t)set);
2194 qemu_log("\n");
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;
2203 int i, n;
2204 arg_ct = &def->args_ct[k];
2205 if (arg_ct->ct & TCG_CT_ALIAS) {
2206 /* an alias is equivalent to a single register */
2207 n = 1;
2208 } else {
2209 if (!(arg_ct->ct & TCG_CT_REG))
2210 return 0;
2211 n = 0;
2212 for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
2213 if (tcg_regset_test_reg(arg_ct->u.regs, i))
2214 n++;
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;
2227 if (n <= 1)
2228 return;
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]);
2233 if (p1 < p2) {
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)
2244 TCGOpcode op;
2246 for (op = 0; op < NB_OPS; op++) {
2247 TCGOpDef *def = &tcg_op_defs[op];
2248 const TCGTargetOpDef *tdefs;
2249 TCGType type;
2250 int i, nb_args;
2252 if (def->flags & TCG_OPF_NOT_PRESENT) {
2253 continue;
2256 nb_args = def->nb_iargs + def->nb_oargs;
2257 if (nb_args == 0) {
2258 continue;
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') {
2274 switch(*ct_str) {
2275 case '0' ... '9':
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;
2289 ct_str++;
2290 break;
2291 case '&':
2292 def->args_ct[i].ct |= TCG_CT_NEWREG;
2293 ct_str++;
2294 break;
2295 case 'i':
2296 def->args_ct[i].ct |= TCG_CT_CONST;
2297 ct_str++;
2298 break;
2299 default:
2300 ct_str = target_parse_constraint(&def->args_ct[i],
2301 ct_str, type);
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)
2319 TCGLabel *label;
2321 switch (op->opc) {
2322 case INDEX_op_br:
2323 label = arg_label(op->args[0]);
2324 label->refs--;
2325 break;
2326 case INDEX_op_brcond_i32:
2327 case INDEX_op_brcond_i64:
2328 label = arg_label(op->args[3]);
2329 label->refs--;
2330 break;
2331 case INDEX_op_brcond2_i32:
2332 label = arg_label(op->args[5]);
2333 label->refs--;
2334 break;
2335 default:
2336 break;
2339 QTAILQ_REMOVE(&s->ops, op, link);
2340 QTAILQ_INSERT_TAIL(&s->free_ops, op, link);
2341 s->nb_ops--;
2343 #ifdef CONFIG_PROFILER
2344 qatomic_set(&s->prof.del_op_count, s->prof.del_op_count + 1);
2345 #endif
2348 static TCGOp *tcg_op_alloc(TCGOpcode opc)
2350 TCGContext *s = tcg_ctx;
2351 TCGOp *op;
2353 if (likely(QTAILQ_EMPTY(&s->free_ops))) {
2354 op = tcg_malloc(sizeof(TCGOp));
2355 } else {
2356 op = QTAILQ_FIRST(&s->free_ops);
2357 QTAILQ_REMOVE(&s->free_ops, op, link);
2359 memset(op, 0, offsetof(TCGOp, link));
2360 op->opc = opc;
2361 s->nb_ops++;
2363 return op;
2366 TCGOp *tcg_emit_op(TCGOpcode opc)
2368 TCGOp *op = tcg_op_alloc(opc);
2369 QTAILQ_INSERT_TAIL(&tcg_ctx->ops, op, link);
2370 return op;
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);
2377 return new_op;
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);
2384 return new_op;
2387 /* Reachable analysis : remove unreachable code. */
2388 static void reachable_code_pass(TCGContext *s)
2390 TCGOp *op, *op_next;
2391 bool dead = false;
2393 QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) {
2394 bool remove = dead;
2395 TCGLabel *label;
2396 int call_flags;
2398 switch (op->opc) {
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.
2409 remove = true;
2410 } else {
2411 /* Once we see a label, insns become live again. */
2412 dead = false;
2413 remove = false;
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);
2426 remove = true;
2430 break;
2432 case INDEX_op_br:
2433 case INDEX_op_exit_tb:
2434 case INDEX_op_goto_ptr:
2435 /* Unconditional branches; everything following is dead. */
2436 dead = true;
2437 break;
2439 case INDEX_op_call:
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) {
2443 dead = true;
2445 break;
2447 case INDEX_op_insn_start:
2448 /* Never remove -- we need to keep these for unwind. */
2449 remove = false;
2450 break;
2452 default:
2453 break;
2456 if (remove) {
2457 tcg_op_remove(s, op);
2462 #define TS_DEAD 1
2463 #define TS_MEM 2
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)
2479 *la_temp_pref(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)
2487 int i;
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)
2503 int i;
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
2511 ? TS_DEAD | TS_MEM
2512 : TS_DEAD);
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)
2520 int i;
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)
2535 int i;
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;
2547 int i;
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;
2555 set &= mask;
2556 /* If the combination is not possible, restart. */
2557 if (set == 0) {
2558 set = tcg_target_available_regs[ts->type] & mask;
2560 *pset = set;
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;
2573 TCGRegSet *prefs;
2574 int i;
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;
2587 bool have_opc_new2;
2588 TCGLifeData arg_life = 0;
2589 TCGTemp *ts;
2590 TCGOpcode opc = op->opc;
2591 const TCGOpDef *def = &tcg_op_defs[opc];
2593 switch (opc) {
2594 case INDEX_op_call:
2596 int call_flags;
2597 int nb_call_regs;
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;
2611 goto do_remove;
2613 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;
2625 la_reset_pref(ts);
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.
2660 *la_temp_pref(ts)
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]);
2671 if (ts) {
2672 tcg_regset_set_reg(*la_temp_pref(ts),
2673 tcg_target_call_iarg_regs[i]);
2677 break;
2678 case INDEX_op_insn_start:
2679 break;
2680 case INDEX_op_discard:
2681 /* mark the temporary as dead */
2682 ts = arg_temp(op->args[0]);
2683 ts->state = TS_DEAD;
2684 la_reset_pref(ts);
2685 break;
2687 case INDEX_op_add2_i32:
2688 opc_new = INDEX_op_add_i32;
2689 goto do_addsub2;
2690 case INDEX_op_sub2_i32:
2691 opc_new = INDEX_op_sub_i32;
2692 goto do_addsub2;
2693 case INDEX_op_add2_i64:
2694 opc_new = INDEX_op_add_i64;
2695 goto do_addsub2;
2696 case INDEX_op_sub2_i64:
2697 opc_new = INDEX_op_sub_i64;
2698 do_addsub2:
2699 nb_iargs = 4;
2700 nb_oargs = 2;
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) {
2707 goto do_remove;
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. */
2715 nb_iargs = 2;
2716 nb_oargs = 1;
2718 goto do_not_remove;
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;
2724 goto do_mul2;
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;
2729 goto do_mul2;
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;
2734 goto do_mul2;
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;
2739 goto do_mul2;
2740 do_mul2:
2741 nb_iargs = 2;
2742 nb_oargs = 2;
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. */
2746 goto do_remove;
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];
2758 } else {
2759 goto do_not_remove;
2761 /* Mark the single-word operation live. */
2762 nb_oargs = 1;
2763 goto do_not_remove;
2765 default:
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) {
2776 goto do_not_remove;
2779 goto do_remove;
2781 goto do_not_remove;
2783 do_remove:
2784 tcg_op_remove(s, op);
2785 break;
2787 do_not_remove:
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;
2802 la_reset_pref(ts);
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. */
2837 switch (opc) {
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]));
2847 break;
2849 default:
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);
2856 set = *pset;
2858 set &= ct->u.regs;
2859 if (ct->ct & TCG_CT_IALIAS) {
2860 set &= op->output_pref[ct->alias_index];
2862 /* If the combination is not possible, restart. */
2863 if (set == 0) {
2864 set = ct->u.regs;
2866 *pset = set;
2868 break;
2870 break;
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;
2880 int nb_temps, i;
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;
2892 } else {
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];
2915 } else {
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 */
2922 call_flags = 0;
2923 } else if (def->flags & TCG_OPF_SIDE_EFFECTS) {
2924 /* Like reading globals: sync_globals */
2925 call_flags = TCG_CALL_NO_WRITE_GLOBALS;
2926 } else {
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]);
2936 if (arg_ts) {
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
2940 ? INDEX_op_ld_i32
2941 : INDEX_op_ld_i64);
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]);
2959 if (arg_ts) {
2960 dir_ts = arg_ts->state_ptr;
2961 if (dir_ts) {
2962 op->args[i] = temp_arg(dir_ts);
2963 changes = true;
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) {
2974 /* Nothing to do */
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);
2983 } else {
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;
2997 if (dir_ts) {
2998 op->args[0] = temp_arg(dir_ts);
2999 changes = true;
3001 /* The output is now live and modified. */
3002 arg_ts->state = 0;
3004 if (NEED_SYNC_ARG(0)) {
3005 TCGOpcode sopc = (arg_ts->type == TCG_TYPE_I32
3006 ? INDEX_op_st_i32
3007 : INDEX_op_st_i64);
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);
3015 } else {
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;
3022 } else {
3023 tcg_debug_assert(!IS_DEAD_ARG(0));
3026 } else {
3027 for (i = 0; i < nb_oargs; i++) {
3028 arg_ts = arg_temp(op->args[i]);
3029 dir_ts = arg_ts->state_ptr;
3030 if (!dir_ts) {
3031 continue;
3033 op->args[i] = temp_arg(dir_ts);
3034 changes = true;
3036 /* The output is now live and modified. */
3037 arg_ts->state = 0;
3039 /* Sync outputs upon their last write. */
3040 if (NEED_SYNC_ARG(i)) {
3041 TCGOpcode sopc = (arg_ts->type == TCG_TYPE_I32
3042 ? INDEX_op_st_i32
3043 : INDEX_op_st_i64);
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;
3060 return changes;
3063 #ifdef CONFIG_DEBUG_TCG
3064 static void dump_regs(TCGContext *s)
3066 TCGTemp *ts;
3067 int i;
3068 char buf[64];
3070 for(i = 0; i < s->nb_temps; i++) {
3071 ts = &s->temps[i];
3072 printf(" %10s: ", tcg_get_arg_str_ptr(s, buf, sizeof(buf), ts));
3073 switch(ts->val_type) {
3074 case TEMP_VAL_REG:
3075 printf("%s", tcg_target_reg_names[ts->reg]);
3076 break;
3077 case TEMP_VAL_MEM:
3078 printf("%d(%s)", (int)ts->mem_offset,
3079 tcg_target_reg_names[ts->mem_base->reg]);
3080 break;
3081 case TEMP_VAL_CONST:
3082 printf("$0x%" TCG_PRIlx, ts->val);
3083 break;
3084 case TEMP_VAL_DEAD:
3085 printf("D");
3086 break;
3087 default:
3088 printf("???");
3089 break;
3091 printf("\n");
3094 for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
3095 if (s->reg_to_temp[i] != NULL) {
3096 printf("%s: %s\n",
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)
3105 int reg;
3106 int k;
3107 TCGTemp *ts;
3108 char buf[64];
3110 for (reg = 0; reg < TCG_TARGET_NB_REGS; reg++) {
3111 ts = s->reg_to_temp[reg];
3112 if (ts != NULL) {
3113 if (ts->val_type != TEMP_VAL_REG || ts->reg != reg) {
3114 printf("Inconsistency for register %s:\n",
3115 tcg_target_reg_names[reg]);
3116 goto fail;
3120 for (k = 0; k < s->nb_temps; k++) {
3121 ts = &s->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));
3126 fail:
3127 printf("reg state:\n");
3128 dump_regs(s);
3129 tcg_abort();
3133 #endif
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);
3142 #endif
3143 if (s->current_frame_offset + (tcg_target_long)sizeof(tcg_target_long) >
3144 s->frame_end) {
3145 tcg_abort();
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) {
3160 return;
3162 if (ts->val_type == TEMP_VAL_REG) {
3163 s->reg_to_temp[ts->reg] = NULL;
3165 ts->val_type = (free_or_dead < 0
3166 || ts->temp_local
3167 || ts->temp_global
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) {
3185 return;
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. */
3196 if (free_or_dead
3197 && tcg_out_sti(s, ts->type, ts->val,
3198 ts->mem_base->reg, ts->mem_offset)) {
3199 break;
3201 temp_load(s, ts, tcg_target_available_regs[ts->type],
3202 allocated_regs, preferred_regs);
3203 /* fallthrough */
3205 case TEMP_VAL_REG:
3206 tcg_out_st(s, ts->type, ts->reg,
3207 ts->mem_base->reg, ts->mem_offset);
3208 break;
3210 case TEMP_VAL_MEM:
3211 break;
3213 case TEMP_VAL_DEAD:
3214 default:
3215 tcg_abort();
3217 ts->mem_coherent = 1;
3219 if (free_or_dead) {
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];
3228 if (ts != NULL) {
3229 temp_sync(s, ts, allocated_regs, 0, -1);
3234 * tcg_reg_alloc:
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];
3249 const int *order;
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) {
3269 return reg;
3271 } else {
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)) {
3276 return 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);
3290 return reg;
3291 } else {
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);
3296 return reg;
3302 tcg_abort();
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)
3310 TCGReg reg;
3312 switch (ts->val_type) {
3313 case TEMP_VAL_REG:
3314 return;
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;
3320 break;
3321 case TEMP_VAL_MEM:
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;
3326 break;
3327 case TEMP_VAL_DEAD:
3328 default:
3329 tcg_abort();
3331 ts->reg = reg;
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)
3350 int i, n;
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)
3362 int i, n;
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
3367 || ts->fixed_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)
3376 int i;
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);
3382 } else {
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;
3407 ots->val = val;
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)) {
3412 temp_dead(s, ots);
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;
3431 TCGTemp *ts, *ots;
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. */
3443 otype = ots->type;
3444 itype = ts->type;
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)) {
3450 temp_dead(s, ts);
3452 tcg_reg_alloc_do_movi(s, ots, val, arg_life, preferred_regs);
3453 return;
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)) {
3475 temp_dead(s, ts);
3477 temp_dead(s, ots);
3478 } else {
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;
3484 ots->reg = ts->reg;
3485 temp_dead(s, ts);
3486 } else {
3487 if (ots->val_type != TEMP_VAL_REG) {
3488 /* When allocating a new register, make sure to not spill the
3489 input one. */
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);
3509 return;
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;
3528 TCGTemp *its, *ots;
3529 TCGType itype, vtype;
3530 intptr_t endian_fixup;
3531 unsigned vece;
3532 bool ok;
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);
3540 itype = its->type;
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)) {
3548 temp_dead(s, its);
3550 tcg_reg_alloc_do_movi(s, ots, val, arg_life, op->output_pref[0]);
3551 return;
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) {
3573 case TEMP_VAL_REG:
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)) {
3581 goto done;
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)) {
3592 break;
3594 /* Sync the temp back to its slot and load from there. */
3595 temp_sync(s, its, s->reserved_regs, 0, 0);
3597 /* fall through */
3599 case TEMP_VAL_MEM:
3600 #ifdef HOST_WORDS_BIGENDIAN
3601 endian_fixup = itype == TCG_TYPE_I32 ? 4 : 8;
3602 endian_fixup -= 1 << vece;
3603 #else
3604 endian_fixup = 0;
3605 #endif
3606 if (tcg_out_dupm_vec(s, vtype, vece, ots->reg, its->mem_base->reg,
3607 its->mem_offset + endian_fixup)) {
3608 goto done;
3610 tcg_out_ld(s, itype, ots->reg, its->mem_base->reg, its->mem_offset);
3611 break;
3613 default:
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);
3621 done:
3622 if (IS_DEAD_ARG(1)) {
3623 temp_dead(s, its);
3625 if (NEED_SYNC_ARG(0)) {
3626 temp_sync(s, ots, s->reserved_regs, 0, 0);
3628 if (IS_DEAD_ARG(0)) {
3629 temp_dead(s, ots);
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;
3640 TCGReg reg;
3641 TCGArg arg;
3642 const TCGArgConstraint *arg_ct;
3643 TCGTemp *ts;
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];
3663 arg = op->args[i];
3664 arg_ct = &def->args_ct[i];
3665 ts = arg_temp(arg);
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 */
3670 const_args[i] = 1;
3671 new_args[i] = ts->val;
3672 continue;
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;
3684 } else {
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) {
3695 int k2, i2;
3696 reg = ts->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);
3710 reg = ts->reg;
3712 if (tcg_regset_test_reg(arg_ct->u.regs, reg)) {
3713 /* nothing to do : the constraint is satisfied */
3714 } else {
3715 allocate_in_reg:
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);
3732 new_args[i] = reg;
3733 const_args[i] = 0;
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);
3746 } else {
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
3757 an exception. */
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];
3764 arg = op->args[i];
3765 arg_ct = &def->args_ct[i];
3766 ts = arg_temp(arg);
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);
3778 } else {
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;
3787 ts->reg = 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;
3794 new_args[i] = reg;
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);
3802 } else {
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)) {
3816 temp_dead(s, ts);
3821 #ifdef TCG_TARGET_STACK_GROWSUP
3822 #define STACK_DIR(x) (-(x))
3823 #else
3824 #define STACK_DIR(x) (x)
3825 #endif
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;
3833 TCGReg reg;
3834 TCGArg arg;
3835 TCGTemp *ts;
3836 intptr_t stack_offset;
3837 size_t call_stack_size;
3838 tcg_insn_unit *func_addr;
3839 int allocate_args;
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) {
3847 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 */
3858 tcg_abort();
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);
3866 #endif
3867 if (arg != TCG_CALL_DUMMY_ARG) {
3868 ts = arg_temp(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);
3875 #endif
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) {
3883 ts = arg_temp(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);
3899 } else {
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) {
3928 /* Nothing to do */
3929 } else if (flags & TCG_CALL_NO_WRITE_GLOBALS) {
3930 sync_globals(s, allocated_regs);
3931 } else {
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++) {
3939 arg = op->args[i];
3940 ts = arg_temp(arg);
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;
3951 ts->reg = 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)) {
3957 temp_dead(s, ts);
3962 #ifdef CONFIG_PROFILER
3964 /* avoid copy/paste errors */
3965 #define PROF_ADD(to, from, field) \
3966 do { \
3967 (to)->field += qatomic_read(&((from)->field)); \
3968 } while (0)
3970 #define PROF_MAX(to, from, field) \
3971 do { \
3972 typeof((from)->field) val__ = qatomic_read(&((from)->field)); \
3973 if (val__ > (to)->field) { \
3974 (to)->field = val__; \
3976 } while (0)
3978 /* Pass in a zero'ed @prof */
3979 static inline
3980 void tcg_profile_snapshot(TCGProfile *prof, bool counters, bool table)
3982 unsigned int n_ctxs = qatomic_read(&n_tcg_ctxs);
3983 unsigned int i;
3985 for (i = 0; i < n_ctxs; i++) {
3986 TCGContext *s = qatomic_read(&tcg_ctxs[i]);
3987 const TCGProfile *orig = &s->prof;
3989 if (counters) {
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);
4008 if (table) {
4009 int i;
4011 for (i = 0; i < NB_OPS; i++) {
4012 PROF_ADD(prof, orig, table_op_count[i]);
4018 #undef PROF_ADD
4019 #undef PROF_MAX
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 = {};
4034 int i;
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 = qatomic_read(&n_tcg_ctxs);
4046 unsigned int i;
4047 int64_t ret = 0;
4049 for (i = 0; i < n_ctxs; i++) {
4050 const TCGContext *s = qatomic_read(&tcg_ctxs[i]);
4051 const TCGProfile *prof = &s->prof;
4053 ret += qatomic_read(&prof->cpu_exec_time);
4055 return ret;
4057 #else
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__);
4066 exit(EXIT_FAILURE);
4068 #endif
4071 int tcg_gen_code(TCGContext *s, TranslationBlock *tb)
4073 #ifdef CONFIG_PROFILER
4074 TCGProfile *prof = &s->prof;
4075 #endif
4076 int i, num_insns;
4077 TCGOp *op;
4079 #ifdef CONFIG_PROFILER
4081 int n = 0;
4083 QTAILQ_FOREACH(op, &s->ops, link) {
4084 n++;
4086 qatomic_set(&prof->op_count, prof->op_count + n);
4087 if (n > prof->op_count_max) {
4088 qatomic_set(&prof->op_count_max, n);
4091 n = s->nb_temps;
4092 qatomic_set(&prof->temp_count, prof->temp_count + n);
4093 if (n > prof->temp_count_max) {
4094 qatomic_set(&prof->temp_count_max, n);
4097 #endif
4099 #ifdef DEBUG_DISAS
4100 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP)
4101 && qemu_log_in_addr_range(tb->pc))) {
4102 FILE *logfile = qemu_log_lock();
4103 qemu_log("OP:\n");
4104 tcg_dump_ops(s, false);
4105 qemu_log("\n");
4106 qemu_log_unlock(logfile);
4108 #endif
4110 #ifdef CONFIG_DEBUG_TCG
4111 /* Ensure all labels referenced have been emitted. */
4113 TCGLabel *l;
4114 bool error = false;
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);
4120 error = true;
4123 assert(!error);
4125 #endif
4127 #ifdef CONFIG_PROFILER
4128 qatomic_set(&prof->opt_time, prof->opt_time - profile_getclock());
4129 #endif
4131 #ifdef USE_TCG_OPTIMIZATIONS
4132 tcg_optimize(s);
4133 #endif
4135 #ifdef CONFIG_PROFILER
4136 qatomic_set(&prof->opt_time, prof->opt_time + profile_getclock());
4137 qatomic_set(&prof->la_time, prof->la_time - profile_getclock());
4138 #endif
4140 reachable_code_pass(s);
4141 liveness_pass_1(s);
4143 if (s->nb_indirects > 0) {
4144 #ifdef DEBUG_DISAS
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);
4150 qemu_log("\n");
4151 qemu_log_unlock(logfile);
4153 #endif
4154 /* Replace indirect temps with direct temps. */
4155 if (liveness_pass_2(s)) {
4156 /* If changes were made, re-run liveness. */
4157 liveness_pass_1(s);
4161 #ifdef CONFIG_PROFILER
4162 qatomic_set(&prof->la_time, prof->la_time + profile_getclock());
4163 #endif
4165 #ifdef DEBUG_DISAS
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);
4171 qemu_log("\n");
4172 qemu_log_unlock(logfile);
4174 #endif
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);
4183 #endif
4184 #ifdef TCG_TARGET_NEED_POOL_LABELS
4185 s->pool_labels = NULL;
4186 #endif
4188 num_insns = -1;
4189 QTAILQ_FOREACH(op, &s->ops, link) {
4190 TCGOpcode opc = op->opc;
4192 #ifdef CONFIG_PROFILER
4193 qatomic_set(&prof->table_op_count[opc], prof->table_op_count[opc] + 1);
4194 #endif
4196 switch (opc) {
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);
4201 break;
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);
4206 break;
4207 case INDEX_op_dup_vec:
4208 tcg_reg_alloc_dup(s, op);
4209 break;
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);
4217 num_insns++;
4218 for (i = 0; i < TARGET_INSN_START_WORDS; ++i) {
4219 target_ulong a;
4220 #if TARGET_LONG_BITS > TCG_TARGET_REG_BITS
4221 a = deposit64(op->args[i * 2], 32, 32, op->args[i * 2 + 1]);
4222 #else
4223 a = op->args[i];
4224 #endif
4225 s->gen_insn_data[num_insns][i] = a;
4227 break;
4228 case INDEX_op_discard:
4229 temp_dead(s, arg_temp(op->args[0]));
4230 break;
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);
4234 break;
4235 case INDEX_op_call:
4236 tcg_reg_alloc_call(s, op);
4237 break;
4238 default:
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);
4245 break;
4247 #ifdef CONFIG_DEBUG_TCG
4248 check_regs(s);
4249 #endif
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)) {
4255 return -1;
4257 /* Test for TB overflow, as seen by gen_insn_end_off. */
4258 if (unlikely(tcg_current_code_size(s) > UINT16_MAX)) {
4259 return -2;
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);
4268 if (i < 0) {
4269 return i;
4271 #endif
4272 #ifdef TCG_TARGET_NEED_POOL_LABELS
4273 i = tcg_out_pool_finalize(s);
4274 if (i < 0) {
4275 return i;
4277 #endif
4278 if (!tcg_resolve_relocs(s)) {
4279 return -2;
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;
4293 int64_t tb_count;
4294 int64_t tb_div_count;
4295 int64_t tot;
4297 tcg_profile_snapshot_counters(&prof);
4298 s = &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",
4304 tot, tot / 2.4e9);
4305 qemu_printf("translated TBs %" PRId64 " (aborted=%" PRId64
4306 " %0.1f%%)\n",
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);
4329 if (tot == 0) {
4330 tot = 1;
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)
4338 * 100.0);
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",
4342 s->restore_count);
4343 qemu_printf(" avg cycles %0.1f\n",
4344 s->restore_count ? (double)s->restore_time / s->restore_count : 0);
4346 #else
4347 void tcg_dump_info(void)
4349 qemu_printf("[TCG profiler not compiled]\n");
4351 #endif
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. */
4367 typedef enum {
4368 JIT_NOACTION = 0,
4369 JIT_REGISTER_FN,
4370 JIT_UNREGISTER_FN
4371 } jit_actions_t;
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 {
4381 uint32_t version;
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)
4390 asm("");
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;
4403 while (1) {
4404 if (strcmp(p, str) == 0) {
4405 return p - strtab;
4407 p += strlen(p) + 1;
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 {
4416 uint32_t len;
4417 uint16_t version;
4418 uint32_t abbrev;
4419 uint8_t ptr_size;
4420 uint8_t cu_die;
4421 uint16_t cu_lang;
4422 uintptr_t cu_low_pc;
4423 uintptr_t cu_high_pc;
4424 uint8_t fn_die;
4425 char fn_name[16];
4426 uintptr_t fn_low_pc;
4427 uintptr_t fn_high_pc;
4428 uint8_t cu_eoc;
4431 struct ElfImage {
4432 ElfW(Ehdr) ehdr;
4433 ElfW(Phdr) phdr;
4434 ElfW(Shdr) shdr[7];
4435 ElfW(Sym) sym[2];
4436 struct DebugInfo di;
4437 uint8_t da[24];
4438 char str[80];
4441 struct ElfImage *img;
4443 static const struct ElfImage img_template = {
4444 .ehdr = {
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,
4452 .e_type = ET_EXEC,
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)),
4459 .e_phnum = 1,
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,
4465 #endif
4466 #ifdef ELF_OSABI
4467 .e_ident[EI_OSABI] = ELF_OSABI,
4468 #endif
4470 .phdr = {
4471 .p_type = PT_LOAD,
4472 .p_flags = PF_X,
4474 .shdr = {
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. */
4480 [1] = { /* .text */
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),
4502 .sh_info = 1,
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),
4512 .sym = {
4513 [1] = { /* code_gen_buffer */
4514 .st_info = ELF_ST_INFO(STB_GLOBAL, STT_FUNC),
4515 .st_shndx = 1,
4518 .di = {
4519 .len = sizeof(struct DebugInfo) - 4,
4520 .version = 2,
4521 .ptr_size = sizeof(void *),
4522 .cu_die = 1,
4523 .cu_lang = 0x8001, /* DW_LANG_Mips_Assembler */
4524 .fn_die = 2,
4525 .fn_name = "code_gen_buffer"
4527 .da = {
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;
4587 #ifdef DEBUG_JIT
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");
4592 if (f) {
4593 if (fwrite(img, img_size, 1, f) != img_size) {
4594 /* Avoid stupid unused return value warning for fwrite. */
4596 fclose(f);
4599 #endif
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();
4609 #else
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();
4629 #endif