]> git.proxmox.com Git - mirror_qemu.git/blame - tcg/tcg.c
tcg: Hoist max_insns computation to tb_gen_code
[mirror_qemu.git] / tcg / tcg.c
CommitLineData
c896fe29
FB
1/*
2 * Tiny Code Generator for QEMU
3 *
4 * Copyright (c) 2008 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
24
c896fe29 25/* define it to use liveness analysis (better code) */
8f2e8c07 26#define USE_TCG_OPTIMIZATIONS
c896fe29 27
757e725b 28#include "qemu/osdep.h"
cca82982 29
813da627
RH
30/* Define to jump the ELF file used to communicate with GDB. */
31#undef DEBUG_JIT
32
72fd2efb 33#include "qemu/error-report.h"
f348b6d1 34#include "qemu/cutils.h"
1de7afc9 35#include "qemu/host-utils.h"
d4c51a0a 36#include "qemu/qemu-print.h"
1de7afc9 37#include "qemu/timer.h"
c896fe29 38
c5d3c498 39/* Note: the long term plan is to reduce the dependencies on the QEMU
c896fe29
FB
40 CPU definitions. Currently they are used for qemu_ld/st
41 instructions */
42#define NO_CPU_IO_DEFS
43#include "cpu.h"
c896fe29 44
63c91552
PB
45#include "exec/cpu-common.h"
46#include "exec/exec-all.h"
47
c896fe29 48#include "tcg-op.h"
813da627 49
edee2579 50#if UINTPTR_MAX == UINT32_MAX
813da627 51# define ELF_CLASS ELFCLASS32
edee2579
RH
52#else
53# define ELF_CLASS ELFCLASS64
813da627
RH
54#endif
55#ifdef HOST_WORDS_BIGENDIAN
56# define ELF_DATA ELFDATA2MSB
57#else
58# define ELF_DATA ELFDATA2LSB
59#endif
60
c896fe29 61#include "elf.h"
508127e2 62#include "exec/log.h"
3468b59e 63#include "sysemu/sysemu.h"
c896fe29 64
ce151109
PM
65/* Forward declarations for functions declared in tcg-target.inc.c and
66 used here. */
e4d58b41 67static void tcg_target_init(TCGContext *s);
f69d277e 68static const TCGTargetOpDef *tcg_target_op_def(TCGOpcode);
e4d58b41 69static void tcg_target_qemu_prologue(TCGContext *s);
6ac17786 70static bool patch_reloc(tcg_insn_unit *code_ptr, int type,
2ba7fae2 71 intptr_t value, intptr_t addend);
c896fe29 72
497a22eb
RH
73/* The CIE and FDE header definitions will be common to all hosts. */
74typedef struct {
75 uint32_t len __attribute__((aligned((sizeof(void *)))));
76 uint32_t id;
77 uint8_t version;
78 char augmentation[1];
79 uint8_t code_align;
80 uint8_t data_align;
81 uint8_t return_column;
82} DebugFrameCIE;
83
84typedef struct QEMU_PACKED {
85 uint32_t len __attribute__((aligned((sizeof(void *)))));
86 uint32_t cie_offset;
edee2579
RH
87 uintptr_t func_start;
88 uintptr_t func_len;
497a22eb
RH
89} DebugFrameFDEHeader;
90
2c90784a
RH
91typedef struct QEMU_PACKED {
92 DebugFrameCIE cie;
93 DebugFrameFDEHeader fde;
94} DebugFrameHeader;
95
813da627 96static void tcg_register_jit_int(void *buf, size_t size,
2c90784a
RH
97 const void *debug_frame,
98 size_t debug_frame_size)
813da627
RH
99 __attribute__((unused));
100
ce151109 101/* Forward declarations for functions declared and used in tcg-target.inc.c. */
069ea736
RH
102static const char *target_parse_constraint(TCGArgConstraint *ct,
103 const char *ct_str, TCGType type);
2a534aff 104static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg1,
a05b5b9b 105 intptr_t arg2);
2a534aff 106static void tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg);
c0ad3001 107static void tcg_out_movi(TCGContext *s, TCGType type,
2a534aff 108 TCGReg ret, tcg_target_long arg);
c0ad3001
SW
109static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args,
110 const int *const_args);
d2fd745f
RH
111#if TCG_TARGET_MAYBE_vec
112static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, unsigned vecl,
113 unsigned vece, const TCGArg *args,
114 const int *const_args);
115#else
116static inline void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, unsigned vecl,
117 unsigned vece, const TCGArg *args,
118 const int *const_args)
119{
120 g_assert_not_reached();
121}
122#endif
2a534aff 123static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg, TCGReg arg1,
a05b5b9b 124 intptr_t arg2);
59d7c14e
RH
125static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
126 TCGReg base, intptr_t ofs);
cf066674 127static void tcg_out_call(TCGContext *s, tcg_insn_unit *target);
f6c6afc1 128static int tcg_target_const_match(tcg_target_long val, TCGType type,
c0ad3001 129 const TCGArgConstraint *arg_ct);
659ef5cb
RH
130#ifdef TCG_TARGET_NEED_LDST_LABELS
131static bool tcg_out_ldst_finalize(TCGContext *s);
132#endif
c896fe29 133
a505785c
EC
134#define TCG_HIGHWATER 1024
135
df2cce29
EC
136static TCGContext **tcg_ctxs;
137static unsigned int n_tcg_ctxs;
1c2adb95 138TCGv_env cpu_env = 0;
df2cce29 139
be2cdc5e
EC
140struct tcg_region_tree {
141 QemuMutex lock;
142 GTree *tree;
143 /* padding to avoid false sharing is computed at run-time */
144};
145
e8feb96f
EC
146/*
147 * We divide code_gen_buffer into equally-sized "regions" that TCG threads
148 * dynamically allocate from as demand dictates. Given appropriate region
149 * sizing, this minimizes flushes even when some TCG threads generate a lot
150 * more code than others.
151 */
152struct tcg_region_state {
153 QemuMutex lock;
154
155 /* fields set at init time */
156 void *start;
157 void *start_aligned;
158 void *end;
159 size_t n;
160 size_t size; /* size of one region */
161 size_t stride; /* .size + guard size */
162
163 /* fields protected by the lock */
164 size_t current; /* current region index */
165 size_t agg_size_full; /* aggregate size of full regions */
166};
167
168static struct tcg_region_state region;
be2cdc5e
EC
169/*
170 * This is an array of struct tcg_region_tree's, with padding.
171 * We use void * to simplify the computation of region_trees[i]; each
172 * struct is found every tree_size bytes.
173 */
174static void *region_trees;
175static size_t tree_size;
d2fd745f 176static TCGRegSet tcg_target_available_regs[TCG_TYPE_COUNT];
b1d8e52e 177static TCGRegSet tcg_target_call_clobber_regs;
c896fe29 178
1813e175 179#if TCG_TARGET_INSN_UNIT_SIZE == 1
4196dca6 180static __attribute__((unused)) inline void tcg_out8(TCGContext *s, uint8_t v)
c896fe29
FB
181{
182 *s->code_ptr++ = v;
183}
184
4196dca6
PM
185static __attribute__((unused)) inline void tcg_patch8(tcg_insn_unit *p,
186 uint8_t v)
5c53bb81 187{
1813e175 188 *p = v;
5c53bb81 189}
1813e175 190#endif
5c53bb81 191
1813e175 192#if TCG_TARGET_INSN_UNIT_SIZE <= 2
4196dca6 193static __attribute__((unused)) inline void tcg_out16(TCGContext *s, uint16_t v)
c896fe29 194{
1813e175
RH
195 if (TCG_TARGET_INSN_UNIT_SIZE == 2) {
196 *s->code_ptr++ = v;
197 } else {
198 tcg_insn_unit *p = s->code_ptr;
199 memcpy(p, &v, sizeof(v));
200 s->code_ptr = p + (2 / TCG_TARGET_INSN_UNIT_SIZE);
201 }
c896fe29
FB
202}
203
4196dca6
PM
204static __attribute__((unused)) inline void tcg_patch16(tcg_insn_unit *p,
205 uint16_t v)
5c53bb81 206{
1813e175
RH
207 if (TCG_TARGET_INSN_UNIT_SIZE == 2) {
208 *p = v;
209 } else {
210 memcpy(p, &v, sizeof(v));
211 }
5c53bb81 212}
1813e175 213#endif
5c53bb81 214
1813e175 215#if TCG_TARGET_INSN_UNIT_SIZE <= 4
4196dca6 216static __attribute__((unused)) inline void tcg_out32(TCGContext *s, uint32_t v)
c896fe29 217{
1813e175
RH
218 if (TCG_TARGET_INSN_UNIT_SIZE == 4) {
219 *s->code_ptr++ = v;
220 } else {
221 tcg_insn_unit *p = s->code_ptr;
222 memcpy(p, &v, sizeof(v));
223 s->code_ptr = p + (4 / TCG_TARGET_INSN_UNIT_SIZE);
224 }
c896fe29
FB
225}
226
4196dca6
PM
227static __attribute__((unused)) inline void tcg_patch32(tcg_insn_unit *p,
228 uint32_t v)
5c53bb81 229{
1813e175
RH
230 if (TCG_TARGET_INSN_UNIT_SIZE == 4) {
231 *p = v;
232 } else {
233 memcpy(p, &v, sizeof(v));
234 }
5c53bb81 235}
1813e175 236#endif
5c53bb81 237
1813e175 238#if TCG_TARGET_INSN_UNIT_SIZE <= 8
4196dca6 239static __attribute__((unused)) inline void tcg_out64(TCGContext *s, uint64_t v)
ac26eb69 240{
1813e175
RH
241 if (TCG_TARGET_INSN_UNIT_SIZE == 8) {
242 *s->code_ptr++ = v;
243 } else {
244 tcg_insn_unit *p = s->code_ptr;
245 memcpy(p, &v, sizeof(v));
246 s->code_ptr = p + (8 / TCG_TARGET_INSN_UNIT_SIZE);
247 }
ac26eb69
RH
248}
249
4196dca6
PM
250static __attribute__((unused)) inline void tcg_patch64(tcg_insn_unit *p,
251 uint64_t v)
5c53bb81 252{
1813e175
RH
253 if (TCG_TARGET_INSN_UNIT_SIZE == 8) {
254 *p = v;
255 } else {
256 memcpy(p, &v, sizeof(v));
257 }
5c53bb81 258}
1813e175 259#endif
5c53bb81 260
c896fe29
FB
261/* label relocation processing */
262
1813e175 263static void tcg_out_reloc(TCGContext *s, tcg_insn_unit *code_ptr, int type,
bec16311 264 TCGLabel *l, intptr_t addend)
c896fe29 265{
c896fe29
FB
266 TCGRelocation *r;
267
c896fe29 268 if (l->has_value) {
623e265c
PB
269 /* FIXME: This may break relocations on RISC targets that
270 modify instruction fields in place. The caller may not have
271 written the initial value. */
6ac17786
RH
272 bool ok = patch_reloc(code_ptr, type, l->u.value, addend);
273 tcg_debug_assert(ok);
c896fe29
FB
274 } else {
275 /* add a new relocation entry */
276 r = tcg_malloc(sizeof(TCGRelocation));
277 r->type = type;
278 r->ptr = code_ptr;
279 r->addend = addend;
280 r->next = l->u.first_reloc;
281 l->u.first_reloc = r;
282 }
283}
284
bec16311 285static void tcg_out_label(TCGContext *s, TCGLabel *l, tcg_insn_unit *ptr)
c896fe29 286{
2ba7fae2 287 intptr_t value = (intptr_t)ptr;
1813e175 288 TCGRelocation *r;
c896fe29 289
eabb7b91 290 tcg_debug_assert(!l->has_value);
1813e175
RH
291
292 for (r = l->u.first_reloc; r != NULL; r = r->next) {
6ac17786
RH
293 bool ok = patch_reloc(r->ptr, r->type, value, r->addend);
294 tcg_debug_assert(ok);
c896fe29 295 }
1813e175 296
c896fe29 297 l->has_value = 1;
1813e175 298 l->u.value_ptr = ptr;
c896fe29
FB
299}
300
42a268c2 301TCGLabel *gen_new_label(void)
c896fe29 302{
b1311c4a 303 TCGContext *s = tcg_ctx;
51e3972c 304 TCGLabel *l = tcg_malloc(sizeof(TCGLabel));
c896fe29 305
51e3972c
RH
306 *l = (TCGLabel){
307 .id = s->nb_labels++
308 };
bef16ab4
RH
309#ifdef CONFIG_DEBUG_TCG
310 QSIMPLEQ_INSERT_TAIL(&s->labels, l, next);
311#endif
42a268c2
RH
312
313 return l;
c896fe29
FB
314}
315
9f754620
RH
316static void set_jmp_reset_offset(TCGContext *s, int which)
317{
318 size_t off = tcg_current_code_size(s);
319 s->tb_jmp_reset_offset[which] = off;
320 /* Make sure that we didn't overflow the stored offset. */
321 assert(s->tb_jmp_reset_offset[which] == off);
322}
323
ce151109 324#include "tcg-target.inc.c"
c896fe29 325
be2cdc5e
EC
326/* compare a pointer @ptr and a tb_tc @s */
327static int ptr_cmp_tb_tc(const void *ptr, const struct tb_tc *s)
328{
329 if (ptr >= s->ptr + s->size) {
330 return 1;
331 } else if (ptr < s->ptr) {
332 return -1;
333 }
334 return 0;
335}
336
337static gint tb_tc_cmp(gconstpointer ap, gconstpointer bp)
338{
339 const struct tb_tc *a = ap;
340 const struct tb_tc *b = bp;
341
342 /*
343 * When both sizes are set, we know this isn't a lookup.
344 * This is the most likely case: every TB must be inserted; lookups
345 * are a lot less frequent.
346 */
347 if (likely(a->size && b->size)) {
348 if (a->ptr > b->ptr) {
349 return 1;
350 } else if (a->ptr < b->ptr) {
351 return -1;
352 }
353 /* a->ptr == b->ptr should happen only on deletions */
354 g_assert(a->size == b->size);
355 return 0;
356 }
357 /*
358 * All lookups have either .size field set to 0.
359 * From the glib sources we see that @ap is always the lookup key. However
360 * the docs provide no guarantee, so we just mark this case as likely.
361 */
362 if (likely(a->size == 0)) {
363 return ptr_cmp_tb_tc(a->ptr, b);
364 }
365 return ptr_cmp_tb_tc(b->ptr, a);
366}
367
368static void tcg_region_trees_init(void)
369{
370 size_t i;
371
372 tree_size = ROUND_UP(sizeof(struct tcg_region_tree), qemu_dcache_linesize);
373 region_trees = qemu_memalign(qemu_dcache_linesize, region.n * tree_size);
374 for (i = 0; i < region.n; i++) {
375 struct tcg_region_tree *rt = region_trees + i * tree_size;
376
377 qemu_mutex_init(&rt->lock);
378 rt->tree = g_tree_new(tb_tc_cmp);
379 }
380}
381
382static struct tcg_region_tree *tc_ptr_to_region_tree(void *p)
383{
384 size_t region_idx;
385
386 if (p < region.start_aligned) {
387 region_idx = 0;
388 } else {
389 ptrdiff_t offset = p - region.start_aligned;
390
391 if (offset > region.stride * (region.n - 1)) {
392 region_idx = region.n - 1;
393 } else {
394 region_idx = offset / region.stride;
395 }
396 }
397 return region_trees + region_idx * tree_size;
398}
399
400void tcg_tb_insert(TranslationBlock *tb)
401{
402 struct tcg_region_tree *rt = tc_ptr_to_region_tree(tb->tc.ptr);
403
404 qemu_mutex_lock(&rt->lock);
405 g_tree_insert(rt->tree, &tb->tc, tb);
406 qemu_mutex_unlock(&rt->lock);
407}
408
409void tcg_tb_remove(TranslationBlock *tb)
410{
411 struct tcg_region_tree *rt = tc_ptr_to_region_tree(tb->tc.ptr);
412
413 qemu_mutex_lock(&rt->lock);
414 g_tree_remove(rt->tree, &tb->tc);
415 qemu_mutex_unlock(&rt->lock);
416}
417
418/*
419 * Find the TB 'tb' such that
420 * tb->tc.ptr <= tc_ptr < tb->tc.ptr + tb->tc.size
421 * Return NULL if not found.
422 */
423TranslationBlock *tcg_tb_lookup(uintptr_t tc_ptr)
424{
425 struct tcg_region_tree *rt = tc_ptr_to_region_tree((void *)tc_ptr);
426 TranslationBlock *tb;
427 struct tb_tc s = { .ptr = (void *)tc_ptr };
428
429 qemu_mutex_lock(&rt->lock);
430 tb = g_tree_lookup(rt->tree, &s);
431 qemu_mutex_unlock(&rt->lock);
432 return tb;
433}
434
435static void tcg_region_tree_lock_all(void)
436{
437 size_t i;
438
439 for (i = 0; i < region.n; i++) {
440 struct tcg_region_tree *rt = region_trees + i * tree_size;
441
442 qemu_mutex_lock(&rt->lock);
443 }
444}
445
446static void tcg_region_tree_unlock_all(void)
447{
448 size_t i;
449
450 for (i = 0; i < region.n; i++) {
451 struct tcg_region_tree *rt = region_trees + i * tree_size;
452
453 qemu_mutex_unlock(&rt->lock);
454 }
455}
456
457void tcg_tb_foreach(GTraverseFunc func, gpointer user_data)
458{
459 size_t i;
460
461 tcg_region_tree_lock_all();
462 for (i = 0; i < region.n; i++) {
463 struct tcg_region_tree *rt = region_trees + i * tree_size;
464
465 g_tree_foreach(rt->tree, func, user_data);
466 }
467 tcg_region_tree_unlock_all();
468}
469
470size_t tcg_nb_tbs(void)
471{
472 size_t nb_tbs = 0;
473 size_t i;
474
475 tcg_region_tree_lock_all();
476 for (i = 0; i < region.n; i++) {
477 struct tcg_region_tree *rt = region_trees + i * tree_size;
478
479 nb_tbs += g_tree_nnodes(rt->tree);
480 }
481 tcg_region_tree_unlock_all();
482 return nb_tbs;
483}
484
485static void tcg_region_tree_reset_all(void)
486{
487 size_t i;
488
489 tcg_region_tree_lock_all();
490 for (i = 0; i < region.n; i++) {
491 struct tcg_region_tree *rt = region_trees + i * tree_size;
492
493 /* Increment the refcount first so that destroy acts as a reset */
494 g_tree_ref(rt->tree);
495 g_tree_destroy(rt->tree);
496 }
497 tcg_region_tree_unlock_all();
498}
499
e8feb96f
EC
500static void tcg_region_bounds(size_t curr_region, void **pstart, void **pend)
501{
502 void *start, *end;
503
504 start = region.start_aligned + curr_region * region.stride;
505 end = start + region.size;
506
507 if (curr_region == 0) {
508 start = region.start;
509 }
510 if (curr_region == region.n - 1) {
511 end = region.end;
512 }
513
514 *pstart = start;
515 *pend = end;
516}
517
518static void tcg_region_assign(TCGContext *s, size_t curr_region)
519{
520 void *start, *end;
521
522 tcg_region_bounds(curr_region, &start, &end);
523
524 s->code_gen_buffer = start;
525 s->code_gen_ptr = start;
526 s->code_gen_buffer_size = end - start;
527 s->code_gen_highwater = end - TCG_HIGHWATER;
528}
529
530static bool tcg_region_alloc__locked(TCGContext *s)
531{
532 if (region.current == region.n) {
533 return true;
534 }
535 tcg_region_assign(s, region.current);
536 region.current++;
537 return false;
538}
539
540/*
541 * Request a new region once the one in use has filled up.
542 * Returns true on error.
543 */
544static bool tcg_region_alloc(TCGContext *s)
545{
546 bool err;
547 /* read the region size now; alloc__locked will overwrite it on success */
548 size_t size_full = s->code_gen_buffer_size;
549
550 qemu_mutex_lock(&region.lock);
551 err = tcg_region_alloc__locked(s);
552 if (!err) {
553 region.agg_size_full += size_full - TCG_HIGHWATER;
554 }
555 qemu_mutex_unlock(&region.lock);
556 return err;
557}
558
559/*
560 * Perform a context's first region allocation.
561 * This function does _not_ increment region.agg_size_full.
562 */
563static inline bool tcg_region_initial_alloc__locked(TCGContext *s)
564{
565 return tcg_region_alloc__locked(s);
566}
567
568/* Call from a safe-work context */
569void tcg_region_reset_all(void)
570{
3468b59e 571 unsigned int n_ctxs = atomic_read(&n_tcg_ctxs);
e8feb96f
EC
572 unsigned int i;
573
574 qemu_mutex_lock(&region.lock);
575 region.current = 0;
576 region.agg_size_full = 0;
577
3468b59e
EC
578 for (i = 0; i < n_ctxs; i++) {
579 TCGContext *s = atomic_read(&tcg_ctxs[i]);
580 bool err = tcg_region_initial_alloc__locked(s);
e8feb96f
EC
581
582 g_assert(!err);
583 }
584 qemu_mutex_unlock(&region.lock);
be2cdc5e
EC
585
586 tcg_region_tree_reset_all();
e8feb96f
EC
587}
588
3468b59e
EC
589#ifdef CONFIG_USER_ONLY
590static size_t tcg_n_regions(void)
591{
592 return 1;
593}
594#else
595/*
596 * It is likely that some vCPUs will translate more code than others, so we
597 * first try to set more regions than max_cpus, with those regions being of
598 * reasonable size. If that's not possible we make do by evenly dividing
599 * the code_gen_buffer among the vCPUs.
600 */
601static size_t tcg_n_regions(void)
602{
603 size_t i;
604
605 /* Use a single region if all we have is one vCPU thread */
606 if (max_cpus == 1 || !qemu_tcg_mttcg_enabled()) {
607 return 1;
608 }
609
610 /* Try to have more regions than max_cpus, with each region being >= 2 MB */
611 for (i = 8; i > 0; i--) {
612 size_t regions_per_thread = i;
613 size_t region_size;
614
615 region_size = tcg_init_ctx.code_gen_buffer_size;
616 region_size /= max_cpus * regions_per_thread;
617
618 if (region_size >= 2 * 1024u * 1024) {
619 return max_cpus * regions_per_thread;
620 }
621 }
622 /* If we can't, then just allocate one region per vCPU thread */
623 return max_cpus;
624}
625#endif
626
e8feb96f
EC
627/*
628 * Initializes region partitioning.
629 *
630 * Called at init time from the parent thread (i.e. the one calling
631 * tcg_context_init), after the target's TCG globals have been set.
3468b59e
EC
632 *
633 * Region partitioning works by splitting code_gen_buffer into separate regions,
634 * and then assigning regions to TCG threads so that the threads can translate
635 * code in parallel without synchronization.
636 *
637 * In softmmu the number of TCG threads is bounded by max_cpus, so we use at
638 * least max_cpus regions in MTTCG. In !MTTCG we use a single region.
639 * Note that the TCG options from the command-line (i.e. -accel accel=tcg,[...])
640 * must have been parsed before calling this function, since it calls
641 * qemu_tcg_mttcg_enabled().
642 *
643 * In user-mode we use a single region. Having multiple regions in user-mode
644 * is not supported, because the number of vCPU threads (recall that each thread
645 * spawned by the guest corresponds to a vCPU thread) is only bounded by the
646 * OS, and usually this number is huge (tens of thousands is not uncommon).
647 * Thus, given this large bound on the number of vCPU threads and the fact
648 * that code_gen_buffer is allocated at compile-time, we cannot guarantee
649 * that the availability of at least one region per vCPU thread.
650 *
651 * However, this user-mode limitation is unlikely to be a significant problem
652 * in practice. Multi-threaded guests share most if not all of their translated
653 * code, which makes parallel code generation less appealing than in softmmu.
e8feb96f
EC
654 */
655void tcg_region_init(void)
656{
657 void *buf = tcg_init_ctx.code_gen_buffer;
658 void *aligned;
659 size_t size = tcg_init_ctx.code_gen_buffer_size;
660 size_t page_size = qemu_real_host_page_size;
661 size_t region_size;
662 size_t n_regions;
663 size_t i;
664
3468b59e 665 n_regions = tcg_n_regions();
e8feb96f
EC
666
667 /* The first region will be 'aligned - buf' bytes larger than the others */
668 aligned = QEMU_ALIGN_PTR_UP(buf, page_size);
669 g_assert(aligned < tcg_init_ctx.code_gen_buffer + size);
670 /*
671 * Make region_size a multiple of page_size, using aligned as the start.
672 * As a result of this we might end up with a few extra pages at the end of
673 * the buffer; we will assign those to the last region.
674 */
675 region_size = (size - (aligned - buf)) / n_regions;
676 region_size = QEMU_ALIGN_DOWN(region_size, page_size);
677
678 /* A region must have at least 2 pages; one code, one guard */
679 g_assert(region_size >= 2 * page_size);
680
681 /* init the region struct */
682 qemu_mutex_init(&region.lock);
683 region.n = n_regions;
684 region.size = region_size - page_size;
685 region.stride = region_size;
686 region.start = buf;
687 region.start_aligned = aligned;
688 /* page-align the end, since its last page will be a guard page */
689 region.end = QEMU_ALIGN_PTR_DOWN(buf + size, page_size);
690 /* account for that last guard page */
691 region.end -= page_size;
692
693 /* set guard pages */
694 for (i = 0; i < region.n; i++) {
695 void *start, *end;
696 int rc;
697
698 tcg_region_bounds(i, &start, &end);
699 rc = qemu_mprotect_none(end, page_size);
700 g_assert(!rc);
701 }
702
be2cdc5e
EC
703 tcg_region_trees_init();
704
3468b59e
EC
705 /* In user-mode we support only one ctx, so do the initial allocation now */
706#ifdef CONFIG_USER_ONLY
e8feb96f
EC
707 {
708 bool err = tcg_region_initial_alloc__locked(tcg_ctx);
709
710 g_assert(!err);
711 }
3468b59e
EC
712#endif
713}
714
715/*
716 * All TCG threads except the parent (i.e. the one that called tcg_context_init
717 * and registered the target's TCG globals) must register with this function
718 * before initiating translation.
719 *
720 * In user-mode we just point tcg_ctx to tcg_init_ctx. See the documentation
721 * of tcg_region_init() for the reasoning behind this.
722 *
723 * In softmmu each caller registers its context in tcg_ctxs[]. Note that in
724 * softmmu tcg_ctxs[] does not track tcg_ctx_init, since the initial context
725 * is not used anymore for translation once this function is called.
726 *
727 * Not tracking tcg_init_ctx in tcg_ctxs[] in softmmu keeps code that iterates
728 * over the array (e.g. tcg_code_size() the same for both softmmu and user-mode.
729 */
730#ifdef CONFIG_USER_ONLY
731void tcg_register_thread(void)
732{
733 tcg_ctx = &tcg_init_ctx;
734}
735#else
736void tcg_register_thread(void)
737{
738 TCGContext *s = g_malloc(sizeof(*s));
739 unsigned int i, n;
740 bool err;
741
742 *s = tcg_init_ctx;
743
744 /* Relink mem_base. */
745 for (i = 0, n = tcg_init_ctx.nb_globals; i < n; ++i) {
746 if (tcg_init_ctx.temps[i].mem_base) {
747 ptrdiff_t b = tcg_init_ctx.temps[i].mem_base - tcg_init_ctx.temps;
748 tcg_debug_assert(b >= 0 && b < n);
749 s->temps[i].mem_base = &s->temps[b];
750 }
751 }
752
753 /* Claim an entry in tcg_ctxs */
754 n = atomic_fetch_inc(&n_tcg_ctxs);
755 g_assert(n < max_cpus);
756 atomic_set(&tcg_ctxs[n], s);
757
758 tcg_ctx = s;
759 qemu_mutex_lock(&region.lock);
760 err = tcg_region_initial_alloc__locked(tcg_ctx);
761 g_assert(!err);
762 qemu_mutex_unlock(&region.lock);
e8feb96f 763}
3468b59e 764#endif /* !CONFIG_USER_ONLY */
e8feb96f
EC
765
766/*
767 * Returns the size (in bytes) of all translated code (i.e. from all regions)
768 * currently in the cache.
769 * See also: tcg_code_capacity()
770 * Do not confuse with tcg_current_code_size(); that one applies to a single
771 * TCG context.
772 */
773size_t tcg_code_size(void)
774{
3468b59e 775 unsigned int n_ctxs = atomic_read(&n_tcg_ctxs);
e8feb96f
EC
776 unsigned int i;
777 size_t total;
778
779 qemu_mutex_lock(&region.lock);
780 total = region.agg_size_full;
3468b59e
EC
781 for (i = 0; i < n_ctxs; i++) {
782 const TCGContext *s = atomic_read(&tcg_ctxs[i]);
e8feb96f
EC
783 size_t size;
784
785 size = atomic_read(&s->code_gen_ptr) - s->code_gen_buffer;
786 g_assert(size <= s->code_gen_buffer_size);
787 total += size;
788 }
789 qemu_mutex_unlock(&region.lock);
790 return total;
791}
792
793/*
794 * Returns the code capacity (in bytes) of the entire cache, i.e. including all
795 * regions.
796 * See also: tcg_code_size()
797 */
798size_t tcg_code_capacity(void)
799{
800 size_t guard_size, capacity;
801
802 /* no need for synchronization; these variables are set at init time */
803 guard_size = region.stride - region.size;
804 capacity = region.end + guard_size - region.start;
805 capacity -= region.n * (guard_size + TCG_HIGHWATER);
806 return capacity;
807}
808
128ed227
EC
809size_t tcg_tb_phys_invalidate_count(void)
810{
811 unsigned int n_ctxs = atomic_read(&n_tcg_ctxs);
812 unsigned int i;
813 size_t total = 0;
814
815 for (i = 0; i < n_ctxs; i++) {
816 const TCGContext *s = atomic_read(&tcg_ctxs[i]);
817
818 total += atomic_read(&s->tb_phys_invalidate_count);
819 }
820 return total;
821}
822
c896fe29
FB
823/* pool based memory allocation */
824void *tcg_malloc_internal(TCGContext *s, int size)
825{
826 TCGPool *p;
827 int pool_size;
828
829 if (size > TCG_POOL_CHUNK_SIZE) {
830 /* big malloc: insert a new pool (XXX: could optimize) */
7267c094 831 p = g_malloc(sizeof(TCGPool) + size);
c896fe29 832 p->size = size;
4055299e
KB
833 p->next = s->pool_first_large;
834 s->pool_first_large = p;
835 return p->data;
c896fe29
FB
836 } else {
837 p = s->pool_current;
838 if (!p) {
839 p = s->pool_first;
840 if (!p)
841 goto new_pool;
842 } else {
843 if (!p->next) {
844 new_pool:
845 pool_size = TCG_POOL_CHUNK_SIZE;
7267c094 846 p = g_malloc(sizeof(TCGPool) + pool_size);
c896fe29
FB
847 p->size = pool_size;
848 p->next = NULL;
849 if (s->pool_current)
850 s->pool_current->next = p;
851 else
852 s->pool_first = p;
853 } else {
854 p = p->next;
855 }
856 }
857 }
858 s->pool_current = p;
859 s->pool_cur = p->data + size;
860 s->pool_end = p->data + p->size;
861 return p->data;
862}
863
864void tcg_pool_reset(TCGContext *s)
865{
4055299e
KB
866 TCGPool *p, *t;
867 for (p = s->pool_first_large; p; p = t) {
868 t = p->next;
869 g_free(p);
870 }
871 s->pool_first_large = NULL;
c896fe29
FB
872 s->pool_cur = s->pool_end = NULL;
873 s->pool_current = NULL;
874}
875
100b5e01
RH
876typedef struct TCGHelperInfo {
877 void *func;
878 const char *name;
afb49896
RH
879 unsigned flags;
880 unsigned sizemask;
100b5e01
RH
881} TCGHelperInfo;
882
2ef6175a
RH
883#include "exec/helper-proto.h"
884
100b5e01 885static const TCGHelperInfo all_helpers[] = {
2ef6175a 886#include "exec/helper-tcg.h"
100b5e01 887};
619205fd 888static GHashTable *helper_table;
100b5e01 889
91478cef 890static int indirect_reg_alloc_order[ARRAY_SIZE(tcg_target_reg_alloc_order)];
f69d277e 891static void process_op_defs(TCGContext *s);
1c2adb95
RH
892static TCGTemp *tcg_global_reg_new_internal(TCGContext *s, TCGType type,
893 TCGReg reg, const char *name);
91478cef 894
c896fe29
FB
895void tcg_context_init(TCGContext *s)
896{
100b5e01 897 int op, total_args, n, i;
c896fe29
FB
898 TCGOpDef *def;
899 TCGArgConstraint *args_ct;
900 int *sorted_args;
1c2adb95 901 TCGTemp *ts;
c896fe29
FB
902
903 memset(s, 0, sizeof(*s));
c896fe29 904 s->nb_globals = 0;
c70fbf0a 905
c896fe29
FB
906 /* Count total number of arguments and allocate the corresponding
907 space */
908 total_args = 0;
909 for(op = 0; op < NB_OPS; op++) {
910 def = &tcg_op_defs[op];
911 n = def->nb_iargs + def->nb_oargs;
912 total_args += n;
913 }
914
7267c094
AL
915 args_ct = g_malloc(sizeof(TCGArgConstraint) * total_args);
916 sorted_args = g_malloc(sizeof(int) * total_args);
c896fe29
FB
917
918 for(op = 0; op < NB_OPS; op++) {
919 def = &tcg_op_defs[op];
920 def->args_ct = args_ct;
921 def->sorted_args = sorted_args;
922 n = def->nb_iargs + def->nb_oargs;
923 sorted_args += n;
924 args_ct += n;
925 }
5cd8f621
RH
926
927 /* Register helpers. */
84fd9dd3 928 /* Use g_direct_hash/equal for direct pointer comparisons on func. */
619205fd 929 helper_table = g_hash_table_new(NULL, NULL);
84fd9dd3 930
100b5e01 931 for (i = 0; i < ARRAY_SIZE(all_helpers); ++i) {
84fd9dd3 932 g_hash_table_insert(helper_table, (gpointer)all_helpers[i].func,
72866e82 933 (gpointer)&all_helpers[i]);
100b5e01 934 }
5cd8f621 935
c896fe29 936 tcg_target_init(s);
f69d277e 937 process_op_defs(s);
91478cef
RH
938
939 /* Reverse the order of the saved registers, assuming they're all at
940 the start of tcg_target_reg_alloc_order. */
941 for (n = 0; n < ARRAY_SIZE(tcg_target_reg_alloc_order); ++n) {
942 int r = tcg_target_reg_alloc_order[n];
943 if (tcg_regset_test_reg(tcg_target_call_clobber_regs, r)) {
944 break;
945 }
946 }
947 for (i = 0; i < n; ++i) {
948 indirect_reg_alloc_order[i] = tcg_target_reg_alloc_order[n - 1 - i];
949 }
950 for (; i < ARRAY_SIZE(tcg_target_reg_alloc_order); ++i) {
951 indirect_reg_alloc_order[i] = tcg_target_reg_alloc_order[i];
952 }
b1311c4a
EC
953
954 tcg_ctx = s;
3468b59e
EC
955 /*
956 * In user-mode we simply share the init context among threads, since we
957 * use a single region. See the documentation tcg_region_init() for the
958 * reasoning behind this.
959 * In softmmu we will have at most max_cpus TCG threads.
960 */
961#ifdef CONFIG_USER_ONLY
df2cce29
EC
962 tcg_ctxs = &tcg_ctx;
963 n_tcg_ctxs = 1;
3468b59e
EC
964#else
965 tcg_ctxs = g_new(TCGContext *, max_cpus);
966#endif
1c2adb95
RH
967
968 tcg_debug_assert(!tcg_regset_test_reg(s->reserved_regs, TCG_AREG0));
969 ts = tcg_global_reg_new_internal(s, TCG_TYPE_PTR, TCG_AREG0, "env");
970 cpu_env = temp_tcgv_ptr(ts);
9002ec79 971}
b03cce8e 972
6e3b2bfd
EC
973/*
974 * Allocate TBs right before their corresponding translated code, making
975 * sure that TBs and code are on different cache lines.
976 */
977TranslationBlock *tcg_tb_alloc(TCGContext *s)
978{
979 uintptr_t align = qemu_icache_linesize;
980 TranslationBlock *tb;
981 void *next;
982
e8feb96f 983 retry:
6e3b2bfd
EC
984 tb = (void *)ROUND_UP((uintptr_t)s->code_gen_ptr, align);
985 next = (void *)ROUND_UP((uintptr_t)(tb + 1), align);
986
987 if (unlikely(next > s->code_gen_highwater)) {
e8feb96f
EC
988 if (tcg_region_alloc(s)) {
989 return NULL;
990 }
991 goto retry;
6e3b2bfd 992 }
e8feb96f 993 atomic_set(&s->code_gen_ptr, next);
57a26946 994 s->data_gen_ptr = NULL;
6e3b2bfd
EC
995 return tb;
996}
997
9002ec79
RH
998void tcg_prologue_init(TCGContext *s)
999{
8163b749
RH
1000 size_t prologue_size, total_size;
1001 void *buf0, *buf1;
1002
1003 /* Put the prologue at the beginning of code_gen_buffer. */
1004 buf0 = s->code_gen_buffer;
5b38ee31 1005 total_size = s->code_gen_buffer_size;
8163b749
RH
1006 s->code_ptr = buf0;
1007 s->code_buf = buf0;
5b38ee31 1008 s->data_gen_ptr = NULL;
8163b749
RH
1009 s->code_gen_prologue = buf0;
1010
5b38ee31
RH
1011 /* Compute a high-water mark, at which we voluntarily flush the buffer
1012 and start over. The size here is arbitrary, significantly larger
1013 than we expect the code generation for any one opcode to require. */
1014 s->code_gen_highwater = s->code_gen_buffer + (total_size - TCG_HIGHWATER);
1015
1016#ifdef TCG_TARGET_NEED_POOL_LABELS
1017 s->pool_labels = NULL;
1018#endif
1019
8163b749 1020 /* Generate the prologue. */
b03cce8e 1021 tcg_target_qemu_prologue(s);
5b38ee31
RH
1022
1023#ifdef TCG_TARGET_NEED_POOL_LABELS
1024 /* Allow the prologue to put e.g. guest_base into a pool entry. */
1025 {
1026 bool ok = tcg_out_pool_finalize(s);
1027 tcg_debug_assert(ok);
1028 }
1029#endif
1030
8163b749
RH
1031 buf1 = s->code_ptr;
1032 flush_icache_range((uintptr_t)buf0, (uintptr_t)buf1);
1033
1034 /* Deduct the prologue from the buffer. */
1035 prologue_size = tcg_current_code_size(s);
1036 s->code_gen_ptr = buf1;
1037 s->code_gen_buffer = buf1;
1038 s->code_buf = buf1;
5b38ee31 1039 total_size -= prologue_size;
8163b749
RH
1040 s->code_gen_buffer_size = total_size;
1041
8163b749 1042 tcg_register_jit(s->code_gen_buffer, total_size);
d6b64b2b
RH
1043
1044#ifdef DEBUG_DISAS
1045 if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) {
1ee73216 1046 qemu_log_lock();
8163b749 1047 qemu_log("PROLOGUE: [size=%zu]\n", prologue_size);
5b38ee31
RH
1048 if (s->data_gen_ptr) {
1049 size_t code_size = s->data_gen_ptr - buf0;
1050 size_t data_size = prologue_size - code_size;
1051 size_t i;
1052
1053 log_disas(buf0, code_size);
1054
1055 for (i = 0; i < data_size; i += sizeof(tcg_target_ulong)) {
1056 if (sizeof(tcg_target_ulong) == 8) {
1057 qemu_log("0x%08" PRIxPTR ": .quad 0x%016" PRIx64 "\n",
1058 (uintptr_t)s->data_gen_ptr + i,
1059 *(uint64_t *)(s->data_gen_ptr + i));
1060 } else {
1061 qemu_log("0x%08" PRIxPTR ": .long 0x%08x\n",
1062 (uintptr_t)s->data_gen_ptr + i,
1063 *(uint32_t *)(s->data_gen_ptr + i));
1064 }
1065 }
1066 } else {
1067 log_disas(buf0, prologue_size);
1068 }
d6b64b2b
RH
1069 qemu_log("\n");
1070 qemu_log_flush();
1ee73216 1071 qemu_log_unlock();
d6b64b2b
RH
1072 }
1073#endif
cedbcb01
EC
1074
1075 /* Assert that goto_ptr is implemented completely. */
1076 if (TCG_TARGET_HAS_goto_ptr) {
1077 tcg_debug_assert(s->code_gen_epilogue != NULL);
1078 }
c896fe29
FB
1079}
1080
c896fe29
FB
1081void tcg_func_start(TCGContext *s)
1082{
1083 tcg_pool_reset(s);
1084 s->nb_temps = s->nb_globals;
0ec9eabc
RH
1085
1086 /* No temps have been previously allocated for size or locality. */
1087 memset(s->free_temps, 0, sizeof(s->free_temps));
1088
abebf925 1089 s->nb_ops = 0;
c896fe29
FB
1090 s->nb_labels = 0;
1091 s->current_frame_offset = s->frame_start;
1092
0a209d4b
RH
1093#ifdef CONFIG_DEBUG_TCG
1094 s->goto_tb_issue_mask = 0;
1095#endif
1096
15fa08f8
RH
1097 QTAILQ_INIT(&s->ops);
1098 QTAILQ_INIT(&s->free_ops);
bef16ab4
RH
1099#ifdef CONFIG_DEBUG_TCG
1100 QSIMPLEQ_INIT(&s->labels);
1101#endif
c896fe29
FB
1102}
1103
7ca4b752
RH
1104static inline TCGTemp *tcg_temp_alloc(TCGContext *s)
1105{
1106 int n = s->nb_temps++;
1107 tcg_debug_assert(n < TCG_MAX_TEMPS);
1108 return memset(&s->temps[n], 0, sizeof(TCGTemp));
1109}
1110
1111static inline TCGTemp *tcg_global_alloc(TCGContext *s)
1112{
fa477d25
RH
1113 TCGTemp *ts;
1114
7ca4b752
RH
1115 tcg_debug_assert(s->nb_globals == s->nb_temps);
1116 s->nb_globals++;
fa477d25
RH
1117 ts = tcg_temp_alloc(s);
1118 ts->temp_global = 1;
1119
1120 return ts;
c896fe29
FB
1121}
1122
085272b3
RH
1123static TCGTemp *tcg_global_reg_new_internal(TCGContext *s, TCGType type,
1124 TCGReg reg, const char *name)
c896fe29 1125{
c896fe29 1126 TCGTemp *ts;
c896fe29 1127
b3a62939 1128 if (TCG_TARGET_REG_BITS == 32 && type != TCG_TYPE_I32) {
c896fe29 1129 tcg_abort();
b3a62939 1130 }
7ca4b752
RH
1131
1132 ts = tcg_global_alloc(s);
c896fe29
FB
1133 ts->base_type = type;
1134 ts->type = type;
1135 ts->fixed_reg = 1;
1136 ts->reg = reg;
c896fe29 1137 ts->name = name;
c896fe29 1138 tcg_regset_set_reg(s->reserved_regs, reg);
7ca4b752 1139
085272b3 1140 return ts;
a7812ae4
PB
1141}
1142
b6638662 1143void tcg_set_frame(TCGContext *s, TCGReg reg, intptr_t start, intptr_t size)
b3a62939 1144{
b3a62939
RH
1145 s->frame_start = start;
1146 s->frame_end = start + size;
085272b3
RH
1147 s->frame_temp
1148 = tcg_global_reg_new_internal(s, TCG_TYPE_PTR, reg, "_frame");
b3a62939
RH
1149}
1150
085272b3
RH
1151TCGTemp *tcg_global_mem_new_internal(TCGType type, TCGv_ptr base,
1152 intptr_t offset, const char *name)
c896fe29 1153{
b1311c4a 1154 TCGContext *s = tcg_ctx;
dc41aa7d 1155 TCGTemp *base_ts = tcgv_ptr_temp(base);
7ca4b752 1156 TCGTemp *ts = tcg_global_alloc(s);
b3915dbb 1157 int indirect_reg = 0, bigendian = 0;
7ca4b752
RH
1158#ifdef HOST_WORDS_BIGENDIAN
1159 bigendian = 1;
1160#endif
c896fe29 1161
b3915dbb 1162 if (!base_ts->fixed_reg) {
5a18407f
RH
1163 /* We do not support double-indirect registers. */
1164 tcg_debug_assert(!base_ts->indirect_reg);
b3915dbb 1165 base_ts->indirect_base = 1;
5a18407f
RH
1166 s->nb_indirects += (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64
1167 ? 2 : 1);
1168 indirect_reg = 1;
b3915dbb
RH
1169 }
1170
7ca4b752
RH
1171 if (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64) {
1172 TCGTemp *ts2 = tcg_global_alloc(s);
c896fe29 1173 char buf[64];
7ca4b752
RH
1174
1175 ts->base_type = TCG_TYPE_I64;
c896fe29 1176 ts->type = TCG_TYPE_I32;
b3915dbb 1177 ts->indirect_reg = indirect_reg;
c896fe29 1178 ts->mem_allocated = 1;
b3a62939 1179 ts->mem_base = base_ts;
7ca4b752 1180 ts->mem_offset = offset + bigendian * 4;
c896fe29
FB
1181 pstrcpy(buf, sizeof(buf), name);
1182 pstrcat(buf, sizeof(buf), "_0");
1183 ts->name = strdup(buf);
c896fe29 1184
7ca4b752
RH
1185 tcg_debug_assert(ts2 == ts + 1);
1186 ts2->base_type = TCG_TYPE_I64;
1187 ts2->type = TCG_TYPE_I32;
b3915dbb 1188 ts2->indirect_reg = indirect_reg;
7ca4b752
RH
1189 ts2->mem_allocated = 1;
1190 ts2->mem_base = base_ts;
1191 ts2->mem_offset = offset + (1 - bigendian) * 4;
c896fe29
FB
1192 pstrcpy(buf, sizeof(buf), name);
1193 pstrcat(buf, sizeof(buf), "_1");
120c1084 1194 ts2->name = strdup(buf);
7ca4b752 1195 } else {
c896fe29
FB
1196 ts->base_type = type;
1197 ts->type = type;
b3915dbb 1198 ts->indirect_reg = indirect_reg;
c896fe29 1199 ts->mem_allocated = 1;
b3a62939 1200 ts->mem_base = base_ts;
c896fe29 1201 ts->mem_offset = offset;
c896fe29 1202 ts->name = name;
c896fe29 1203 }
085272b3 1204 return ts;
a7812ae4
PB
1205}
1206
5bfa8034 1207TCGTemp *tcg_temp_new_internal(TCGType type, bool temp_local)
c896fe29 1208{
b1311c4a 1209 TCGContext *s = tcg_ctx;
c896fe29 1210 TCGTemp *ts;
641d5fbe 1211 int idx, k;
c896fe29 1212
0ec9eabc
RH
1213 k = type + (temp_local ? TCG_TYPE_COUNT : 0);
1214 idx = find_first_bit(s->free_temps[k].l, TCG_MAX_TEMPS);
1215 if (idx < TCG_MAX_TEMPS) {
1216 /* There is already an available temp with the right type. */
1217 clear_bit(idx, s->free_temps[k].l);
1218
e8996ee0 1219 ts = &s->temps[idx];
e8996ee0 1220 ts->temp_allocated = 1;
7ca4b752
RH
1221 tcg_debug_assert(ts->base_type == type);
1222 tcg_debug_assert(ts->temp_local == temp_local);
e8996ee0 1223 } else {
7ca4b752
RH
1224 ts = tcg_temp_alloc(s);
1225 if (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64) {
1226 TCGTemp *ts2 = tcg_temp_alloc(s);
1227
f6aa2f7d 1228 ts->base_type = type;
e8996ee0
FB
1229 ts->type = TCG_TYPE_I32;
1230 ts->temp_allocated = 1;
641d5fbe 1231 ts->temp_local = temp_local;
7ca4b752
RH
1232
1233 tcg_debug_assert(ts2 == ts + 1);
1234 ts2->base_type = TCG_TYPE_I64;
1235 ts2->type = TCG_TYPE_I32;
1236 ts2->temp_allocated = 1;
1237 ts2->temp_local = temp_local;
1238 } else {
e8996ee0
FB
1239 ts->base_type = type;
1240 ts->type = type;
1241 ts->temp_allocated = 1;
641d5fbe 1242 ts->temp_local = temp_local;
e8996ee0 1243 }
c896fe29 1244 }
27bfd83c
PM
1245
1246#if defined(CONFIG_DEBUG_TCG)
1247 s->temps_in_use++;
1248#endif
085272b3 1249 return ts;
c896fe29
FB
1250}
1251
d2fd745f
RH
1252TCGv_vec tcg_temp_new_vec(TCGType type)
1253{
1254 TCGTemp *t;
1255
1256#ifdef CONFIG_DEBUG_TCG
1257 switch (type) {
1258 case TCG_TYPE_V64:
1259 assert(TCG_TARGET_HAS_v64);
1260 break;
1261 case TCG_TYPE_V128:
1262 assert(TCG_TARGET_HAS_v128);
1263 break;
1264 case TCG_TYPE_V256:
1265 assert(TCG_TARGET_HAS_v256);
1266 break;
1267 default:
1268 g_assert_not_reached();
1269 }
1270#endif
1271
1272 t = tcg_temp_new_internal(type, 0);
1273 return temp_tcgv_vec(t);
1274}
1275
1276/* Create a new temp of the same type as an existing temp. */
1277TCGv_vec tcg_temp_new_vec_matching(TCGv_vec match)
1278{
1279 TCGTemp *t = tcgv_vec_temp(match);
1280
1281 tcg_debug_assert(t->temp_allocated != 0);
1282
1283 t = tcg_temp_new_internal(t->base_type, 0);
1284 return temp_tcgv_vec(t);
1285}
1286
5bfa8034 1287void tcg_temp_free_internal(TCGTemp *ts)
c896fe29 1288{
b1311c4a 1289 TCGContext *s = tcg_ctx;
085272b3 1290 int k, idx;
c896fe29 1291
27bfd83c
PM
1292#if defined(CONFIG_DEBUG_TCG)
1293 s->temps_in_use--;
1294 if (s->temps_in_use < 0) {
1295 fprintf(stderr, "More temporaries freed than allocated!\n");
1296 }
1297#endif
1298
085272b3 1299 tcg_debug_assert(ts->temp_global == 0);
eabb7b91 1300 tcg_debug_assert(ts->temp_allocated != 0);
e8996ee0 1301 ts->temp_allocated = 0;
0ec9eabc 1302
085272b3 1303 idx = temp_idx(ts);
18d13fa2 1304 k = ts->base_type + (ts->temp_local ? TCG_TYPE_COUNT : 0);
0ec9eabc 1305 set_bit(idx, s->free_temps[k].l);
c896fe29
FB
1306}
1307
a7812ae4 1308TCGv_i32 tcg_const_i32(int32_t val)
c896fe29 1309{
a7812ae4
PB
1310 TCGv_i32 t0;
1311 t0 = tcg_temp_new_i32();
e8996ee0
FB
1312 tcg_gen_movi_i32(t0, val);
1313 return t0;
1314}
c896fe29 1315
a7812ae4 1316TCGv_i64 tcg_const_i64(int64_t val)
e8996ee0 1317{
a7812ae4
PB
1318 TCGv_i64 t0;
1319 t0 = tcg_temp_new_i64();
e8996ee0
FB
1320 tcg_gen_movi_i64(t0, val);
1321 return t0;
c896fe29
FB
1322}
1323
a7812ae4 1324TCGv_i32 tcg_const_local_i32(int32_t val)
bdffd4a9 1325{
a7812ae4
PB
1326 TCGv_i32 t0;
1327 t0 = tcg_temp_local_new_i32();
bdffd4a9
AJ
1328 tcg_gen_movi_i32(t0, val);
1329 return t0;
1330}
1331
a7812ae4 1332TCGv_i64 tcg_const_local_i64(int64_t val)
bdffd4a9 1333{
a7812ae4
PB
1334 TCGv_i64 t0;
1335 t0 = tcg_temp_local_new_i64();
bdffd4a9
AJ
1336 tcg_gen_movi_i64(t0, val);
1337 return t0;
1338}
1339
27bfd83c
PM
1340#if defined(CONFIG_DEBUG_TCG)
1341void tcg_clear_temp_count(void)
1342{
b1311c4a 1343 TCGContext *s = tcg_ctx;
27bfd83c
PM
1344 s->temps_in_use = 0;
1345}
1346
1347int tcg_check_temp_count(void)
1348{
b1311c4a 1349 TCGContext *s = tcg_ctx;
27bfd83c
PM
1350 if (s->temps_in_use) {
1351 /* Clear the count so that we don't give another
1352 * warning immediately next time around.
1353 */
1354 s->temps_in_use = 0;
1355 return 1;
1356 }
1357 return 0;
1358}
1359#endif
1360
be0f34b5
RH
1361/* Return true if OP may appear in the opcode stream.
1362 Test the runtime variable that controls each opcode. */
1363bool tcg_op_supported(TCGOpcode op)
1364{
d2fd745f
RH
1365 const bool have_vec
1366 = TCG_TARGET_HAS_v64 | TCG_TARGET_HAS_v128 | TCG_TARGET_HAS_v256;
1367
be0f34b5
RH
1368 switch (op) {
1369 case INDEX_op_discard:
1370 case INDEX_op_set_label:
1371 case INDEX_op_call:
1372 case INDEX_op_br:
1373 case INDEX_op_mb:
1374 case INDEX_op_insn_start:
1375 case INDEX_op_exit_tb:
1376 case INDEX_op_goto_tb:
1377 case INDEX_op_qemu_ld_i32:
1378 case INDEX_op_qemu_st_i32:
1379 case INDEX_op_qemu_ld_i64:
1380 case INDEX_op_qemu_st_i64:
1381 return true;
1382
1383 case INDEX_op_goto_ptr:
1384 return TCG_TARGET_HAS_goto_ptr;
1385
1386 case INDEX_op_mov_i32:
1387 case INDEX_op_movi_i32:
1388 case INDEX_op_setcond_i32:
1389 case INDEX_op_brcond_i32:
1390 case INDEX_op_ld8u_i32:
1391 case INDEX_op_ld8s_i32:
1392 case INDEX_op_ld16u_i32:
1393 case INDEX_op_ld16s_i32:
1394 case INDEX_op_ld_i32:
1395 case INDEX_op_st8_i32:
1396 case INDEX_op_st16_i32:
1397 case INDEX_op_st_i32:
1398 case INDEX_op_add_i32:
1399 case INDEX_op_sub_i32:
1400 case INDEX_op_mul_i32:
1401 case INDEX_op_and_i32:
1402 case INDEX_op_or_i32:
1403 case INDEX_op_xor_i32:
1404 case INDEX_op_shl_i32:
1405 case INDEX_op_shr_i32:
1406 case INDEX_op_sar_i32:
1407 return true;
1408
1409 case INDEX_op_movcond_i32:
1410 return TCG_TARGET_HAS_movcond_i32;
1411 case INDEX_op_div_i32:
1412 case INDEX_op_divu_i32:
1413 return TCG_TARGET_HAS_div_i32;
1414 case INDEX_op_rem_i32:
1415 case INDEX_op_remu_i32:
1416 return TCG_TARGET_HAS_rem_i32;
1417 case INDEX_op_div2_i32:
1418 case INDEX_op_divu2_i32:
1419 return TCG_TARGET_HAS_div2_i32;
1420 case INDEX_op_rotl_i32:
1421 case INDEX_op_rotr_i32:
1422 return TCG_TARGET_HAS_rot_i32;
1423 case INDEX_op_deposit_i32:
1424 return TCG_TARGET_HAS_deposit_i32;
1425 case INDEX_op_extract_i32:
1426 return TCG_TARGET_HAS_extract_i32;
1427 case INDEX_op_sextract_i32:
1428 return TCG_TARGET_HAS_sextract_i32;
fce1296f
RH
1429 case INDEX_op_extract2_i32:
1430 return TCG_TARGET_HAS_extract2_i32;
be0f34b5
RH
1431 case INDEX_op_add2_i32:
1432 return TCG_TARGET_HAS_add2_i32;
1433 case INDEX_op_sub2_i32:
1434 return TCG_TARGET_HAS_sub2_i32;
1435 case INDEX_op_mulu2_i32:
1436 return TCG_TARGET_HAS_mulu2_i32;
1437 case INDEX_op_muls2_i32:
1438 return TCG_TARGET_HAS_muls2_i32;
1439 case INDEX_op_muluh_i32:
1440 return TCG_TARGET_HAS_muluh_i32;
1441 case INDEX_op_mulsh_i32:
1442 return TCG_TARGET_HAS_mulsh_i32;
1443 case INDEX_op_ext8s_i32:
1444 return TCG_TARGET_HAS_ext8s_i32;
1445 case INDEX_op_ext16s_i32:
1446 return TCG_TARGET_HAS_ext16s_i32;
1447 case INDEX_op_ext8u_i32:
1448 return TCG_TARGET_HAS_ext8u_i32;
1449 case INDEX_op_ext16u_i32:
1450 return TCG_TARGET_HAS_ext16u_i32;
1451 case INDEX_op_bswap16_i32:
1452 return TCG_TARGET_HAS_bswap16_i32;
1453 case INDEX_op_bswap32_i32:
1454 return TCG_TARGET_HAS_bswap32_i32;
1455 case INDEX_op_not_i32:
1456 return TCG_TARGET_HAS_not_i32;
1457 case INDEX_op_neg_i32:
1458 return TCG_TARGET_HAS_neg_i32;
1459 case INDEX_op_andc_i32:
1460 return TCG_TARGET_HAS_andc_i32;
1461 case INDEX_op_orc_i32:
1462 return TCG_TARGET_HAS_orc_i32;
1463 case INDEX_op_eqv_i32:
1464 return TCG_TARGET_HAS_eqv_i32;
1465 case INDEX_op_nand_i32:
1466 return TCG_TARGET_HAS_nand_i32;
1467 case INDEX_op_nor_i32:
1468 return TCG_TARGET_HAS_nor_i32;
1469 case INDEX_op_clz_i32:
1470 return TCG_TARGET_HAS_clz_i32;
1471 case INDEX_op_ctz_i32:
1472 return TCG_TARGET_HAS_ctz_i32;
1473 case INDEX_op_ctpop_i32:
1474 return TCG_TARGET_HAS_ctpop_i32;
1475
1476 case INDEX_op_brcond2_i32:
1477 case INDEX_op_setcond2_i32:
1478 return TCG_TARGET_REG_BITS == 32;
1479
1480 case INDEX_op_mov_i64:
1481 case INDEX_op_movi_i64:
1482 case INDEX_op_setcond_i64:
1483 case INDEX_op_brcond_i64:
1484 case INDEX_op_ld8u_i64:
1485 case INDEX_op_ld8s_i64:
1486 case INDEX_op_ld16u_i64:
1487 case INDEX_op_ld16s_i64:
1488 case INDEX_op_ld32u_i64:
1489 case INDEX_op_ld32s_i64:
1490 case INDEX_op_ld_i64:
1491 case INDEX_op_st8_i64:
1492 case INDEX_op_st16_i64:
1493 case INDEX_op_st32_i64:
1494 case INDEX_op_st_i64:
1495 case INDEX_op_add_i64:
1496 case INDEX_op_sub_i64:
1497 case INDEX_op_mul_i64:
1498 case INDEX_op_and_i64:
1499 case INDEX_op_or_i64:
1500 case INDEX_op_xor_i64:
1501 case INDEX_op_shl_i64:
1502 case INDEX_op_shr_i64:
1503 case INDEX_op_sar_i64:
1504 case INDEX_op_ext_i32_i64:
1505 case INDEX_op_extu_i32_i64:
1506 return TCG_TARGET_REG_BITS == 64;
1507
1508 case INDEX_op_movcond_i64:
1509 return TCG_TARGET_HAS_movcond_i64;
1510 case INDEX_op_div_i64:
1511 case INDEX_op_divu_i64:
1512 return TCG_TARGET_HAS_div_i64;
1513 case INDEX_op_rem_i64:
1514 case INDEX_op_remu_i64:
1515 return TCG_TARGET_HAS_rem_i64;
1516 case INDEX_op_div2_i64:
1517 case INDEX_op_divu2_i64:
1518 return TCG_TARGET_HAS_div2_i64;
1519 case INDEX_op_rotl_i64:
1520 case INDEX_op_rotr_i64:
1521 return TCG_TARGET_HAS_rot_i64;
1522 case INDEX_op_deposit_i64:
1523 return TCG_TARGET_HAS_deposit_i64;
1524 case INDEX_op_extract_i64:
1525 return TCG_TARGET_HAS_extract_i64;
1526 case INDEX_op_sextract_i64:
1527 return TCG_TARGET_HAS_sextract_i64;
fce1296f
RH
1528 case INDEX_op_extract2_i64:
1529 return TCG_TARGET_HAS_extract2_i64;
be0f34b5
RH
1530 case INDEX_op_extrl_i64_i32:
1531 return TCG_TARGET_HAS_extrl_i64_i32;
1532 case INDEX_op_extrh_i64_i32:
1533 return TCG_TARGET_HAS_extrh_i64_i32;
1534 case INDEX_op_ext8s_i64:
1535 return TCG_TARGET_HAS_ext8s_i64;
1536 case INDEX_op_ext16s_i64:
1537 return TCG_TARGET_HAS_ext16s_i64;
1538 case INDEX_op_ext32s_i64:
1539 return TCG_TARGET_HAS_ext32s_i64;
1540 case INDEX_op_ext8u_i64:
1541 return TCG_TARGET_HAS_ext8u_i64;
1542 case INDEX_op_ext16u_i64:
1543 return TCG_TARGET_HAS_ext16u_i64;
1544 case INDEX_op_ext32u_i64:
1545 return TCG_TARGET_HAS_ext32u_i64;
1546 case INDEX_op_bswap16_i64:
1547 return TCG_TARGET_HAS_bswap16_i64;
1548 case INDEX_op_bswap32_i64:
1549 return TCG_TARGET_HAS_bswap32_i64;
1550 case INDEX_op_bswap64_i64:
1551 return TCG_TARGET_HAS_bswap64_i64;
1552 case INDEX_op_not_i64:
1553 return TCG_TARGET_HAS_not_i64;
1554 case INDEX_op_neg_i64:
1555 return TCG_TARGET_HAS_neg_i64;
1556 case INDEX_op_andc_i64:
1557 return TCG_TARGET_HAS_andc_i64;
1558 case INDEX_op_orc_i64:
1559 return TCG_TARGET_HAS_orc_i64;
1560 case INDEX_op_eqv_i64:
1561 return TCG_TARGET_HAS_eqv_i64;
1562 case INDEX_op_nand_i64:
1563 return TCG_TARGET_HAS_nand_i64;
1564 case INDEX_op_nor_i64:
1565 return TCG_TARGET_HAS_nor_i64;
1566 case INDEX_op_clz_i64:
1567 return TCG_TARGET_HAS_clz_i64;
1568 case INDEX_op_ctz_i64:
1569 return TCG_TARGET_HAS_ctz_i64;
1570 case INDEX_op_ctpop_i64:
1571 return TCG_TARGET_HAS_ctpop_i64;
1572 case INDEX_op_add2_i64:
1573 return TCG_TARGET_HAS_add2_i64;
1574 case INDEX_op_sub2_i64:
1575 return TCG_TARGET_HAS_sub2_i64;
1576 case INDEX_op_mulu2_i64:
1577 return TCG_TARGET_HAS_mulu2_i64;
1578 case INDEX_op_muls2_i64:
1579 return TCG_TARGET_HAS_muls2_i64;
1580 case INDEX_op_muluh_i64:
1581 return TCG_TARGET_HAS_muluh_i64;
1582 case INDEX_op_mulsh_i64:
1583 return TCG_TARGET_HAS_mulsh_i64;
1584
d2fd745f
RH
1585 case INDEX_op_mov_vec:
1586 case INDEX_op_dup_vec:
1587 case INDEX_op_dupi_vec:
1588 case INDEX_op_ld_vec:
1589 case INDEX_op_st_vec:
1590 case INDEX_op_add_vec:
1591 case INDEX_op_sub_vec:
1592 case INDEX_op_and_vec:
1593 case INDEX_op_or_vec:
1594 case INDEX_op_xor_vec:
212be173 1595 case INDEX_op_cmp_vec:
d2fd745f
RH
1596 return have_vec;
1597 case INDEX_op_dup2_vec:
1598 return have_vec && TCG_TARGET_REG_BITS == 32;
1599 case INDEX_op_not_vec:
1600 return have_vec && TCG_TARGET_HAS_not_vec;
1601 case INDEX_op_neg_vec:
1602 return have_vec && TCG_TARGET_HAS_neg_vec;
1603 case INDEX_op_andc_vec:
1604 return have_vec && TCG_TARGET_HAS_andc_vec;
1605 case INDEX_op_orc_vec:
1606 return have_vec && TCG_TARGET_HAS_orc_vec;
3774030a
RH
1607 case INDEX_op_mul_vec:
1608 return have_vec && TCG_TARGET_HAS_mul_vec;
d0ec9796
RH
1609 case INDEX_op_shli_vec:
1610 case INDEX_op_shri_vec:
1611 case INDEX_op_sari_vec:
1612 return have_vec && TCG_TARGET_HAS_shi_vec;
1613 case INDEX_op_shls_vec:
1614 case INDEX_op_shrs_vec:
1615 case INDEX_op_sars_vec:
1616 return have_vec && TCG_TARGET_HAS_shs_vec;
1617 case INDEX_op_shlv_vec:
1618 case INDEX_op_shrv_vec:
1619 case INDEX_op_sarv_vec:
1620 return have_vec && TCG_TARGET_HAS_shv_vec;
8afaf050
RH
1621 case INDEX_op_ssadd_vec:
1622 case INDEX_op_usadd_vec:
1623 case INDEX_op_sssub_vec:
1624 case INDEX_op_ussub_vec:
1625 return have_vec && TCG_TARGET_HAS_sat_vec;
dd0a0fcd
RH
1626 case INDEX_op_smin_vec:
1627 case INDEX_op_umin_vec:
1628 case INDEX_op_smax_vec:
1629 case INDEX_op_umax_vec:
1630 return have_vec && TCG_TARGET_HAS_minmax_vec;
d2fd745f 1631
db432672
RH
1632 default:
1633 tcg_debug_assert(op > INDEX_op_last_generic && op < NB_OPS);
1634 return true;
be0f34b5 1635 }
be0f34b5
RH
1636}
1637
39cf05d3
FB
1638/* Note: we convert the 64 bit args to 32 bit and do some alignment
1639 and endian swap. Maybe it would be better to do the alignment
1640 and endian swap in tcg_reg_alloc_call(). */
ae8b75dc 1641void tcg_gen_callN(void *func, TCGTemp *ret, int nargs, TCGTemp **args)
c896fe29 1642{
75e8b9b7 1643 int i, real_args, nb_rets, pi;
bbb8a1b4 1644 unsigned sizemask, flags;
afb49896 1645 TCGHelperInfo *info;
75e8b9b7 1646 TCGOp *op;
afb49896 1647
619205fd 1648 info = g_hash_table_lookup(helper_table, (gpointer)func);
bbb8a1b4
RH
1649 flags = info->flags;
1650 sizemask = info->sizemask;
2bece2c8 1651
34b1a49c
RH
1652#if defined(__sparc__) && !defined(__arch64__) \
1653 && !defined(CONFIG_TCG_INTERPRETER)
1654 /* We have 64-bit values in one register, but need to pass as two
1655 separate parameters. Split them. */
1656 int orig_sizemask = sizemask;
1657 int orig_nargs = nargs;
1658 TCGv_i64 retl, reth;
ae8b75dc 1659 TCGTemp *split_args[MAX_OPC_PARAM];
34b1a49c 1660
f764718d
RH
1661 retl = NULL;
1662 reth = NULL;
34b1a49c 1663 if (sizemask != 0) {
34b1a49c
RH
1664 for (i = real_args = 0; i < nargs; ++i) {
1665 int is_64bit = sizemask & (1 << (i+1)*2);
1666 if (is_64bit) {
085272b3 1667 TCGv_i64 orig = temp_tcgv_i64(args[i]);
34b1a49c
RH
1668 TCGv_i32 h = tcg_temp_new_i32();
1669 TCGv_i32 l = tcg_temp_new_i32();
1670 tcg_gen_extr_i64_i32(l, h, orig);
ae8b75dc
RH
1671 split_args[real_args++] = tcgv_i32_temp(h);
1672 split_args[real_args++] = tcgv_i32_temp(l);
34b1a49c
RH
1673 } else {
1674 split_args[real_args++] = args[i];
1675 }
1676 }
1677 nargs = real_args;
1678 args = split_args;
1679 sizemask = 0;
1680 }
1681#elif defined(TCG_TARGET_EXTEND_ARGS) && TCG_TARGET_REG_BITS == 64
2bece2c8
RH
1682 for (i = 0; i < nargs; ++i) {
1683 int is_64bit = sizemask & (1 << (i+1)*2);
1684 int is_signed = sizemask & (2 << (i+1)*2);
1685 if (!is_64bit) {
1686 TCGv_i64 temp = tcg_temp_new_i64();
085272b3 1687 TCGv_i64 orig = temp_tcgv_i64(args[i]);
2bece2c8
RH
1688 if (is_signed) {
1689 tcg_gen_ext32s_i64(temp, orig);
1690 } else {
1691 tcg_gen_ext32u_i64(temp, orig);
1692 }
ae8b75dc 1693 args[i] = tcgv_i64_temp(temp);
2bece2c8
RH
1694 }
1695 }
1696#endif /* TCG_TARGET_EXTEND_ARGS */
1697
15fa08f8 1698 op = tcg_emit_op(INDEX_op_call);
75e8b9b7
RH
1699
1700 pi = 0;
ae8b75dc 1701 if (ret != NULL) {
34b1a49c
RH
1702#if defined(__sparc__) && !defined(__arch64__) \
1703 && !defined(CONFIG_TCG_INTERPRETER)
1704 if (orig_sizemask & 1) {
1705 /* The 32-bit ABI is going to return the 64-bit value in
1706 the %o0/%o1 register pair. Prepare for this by using
1707 two return temporaries, and reassemble below. */
1708 retl = tcg_temp_new_i64();
1709 reth = tcg_temp_new_i64();
ae8b75dc
RH
1710 op->args[pi++] = tcgv_i64_arg(reth);
1711 op->args[pi++] = tcgv_i64_arg(retl);
34b1a49c
RH
1712 nb_rets = 2;
1713 } else {
ae8b75dc 1714 op->args[pi++] = temp_arg(ret);
34b1a49c
RH
1715 nb_rets = 1;
1716 }
1717#else
1718 if (TCG_TARGET_REG_BITS < 64 && (sizemask & 1)) {
02eb19d0 1719#ifdef HOST_WORDS_BIGENDIAN
ae8b75dc
RH
1720 op->args[pi++] = temp_arg(ret + 1);
1721 op->args[pi++] = temp_arg(ret);
39cf05d3 1722#else
ae8b75dc
RH
1723 op->args[pi++] = temp_arg(ret);
1724 op->args[pi++] = temp_arg(ret + 1);
39cf05d3 1725#endif
a7812ae4 1726 nb_rets = 2;
34b1a49c 1727 } else {
ae8b75dc 1728 op->args[pi++] = temp_arg(ret);
a7812ae4 1729 nb_rets = 1;
c896fe29 1730 }
34b1a49c 1731#endif
a7812ae4
PB
1732 } else {
1733 nb_rets = 0;
c896fe29 1734 }
cd9090aa 1735 TCGOP_CALLO(op) = nb_rets;
75e8b9b7 1736
a7812ae4
PB
1737 real_args = 0;
1738 for (i = 0; i < nargs; i++) {
2bece2c8 1739 int is_64bit = sizemask & (1 << (i+1)*2);
bbb8a1b4 1740 if (TCG_TARGET_REG_BITS < 64 && is_64bit) {
39cf05d3
FB
1741#ifdef TCG_TARGET_CALL_ALIGN_ARGS
1742 /* some targets want aligned 64 bit args */
ebd486d5 1743 if (real_args & 1) {
75e8b9b7 1744 op->args[pi++] = TCG_CALL_DUMMY_ARG;
ebd486d5 1745 real_args++;
39cf05d3
FB
1746 }
1747#endif
c70fbf0a
RH
1748 /* If stack grows up, then we will be placing successive
1749 arguments at lower addresses, which means we need to
1750 reverse the order compared to how we would normally
1751 treat either big or little-endian. For those arguments
1752 that will wind up in registers, this still works for
1753 HPPA (the only current STACK_GROWSUP target) since the
1754 argument registers are *also* allocated in decreasing
1755 order. If another such target is added, this logic may
1756 have to get more complicated to differentiate between
1757 stack arguments and register arguments. */
02eb19d0 1758#if defined(HOST_WORDS_BIGENDIAN) != defined(TCG_TARGET_STACK_GROWSUP)
ae8b75dc
RH
1759 op->args[pi++] = temp_arg(args[i] + 1);
1760 op->args[pi++] = temp_arg(args[i]);
c896fe29 1761#else
ae8b75dc
RH
1762 op->args[pi++] = temp_arg(args[i]);
1763 op->args[pi++] = temp_arg(args[i] + 1);
c896fe29 1764#endif
a7812ae4 1765 real_args += 2;
2bece2c8 1766 continue;
c896fe29 1767 }
2bece2c8 1768
ae8b75dc 1769 op->args[pi++] = temp_arg(args[i]);
2bece2c8 1770 real_args++;
c896fe29 1771 }
75e8b9b7
RH
1772 op->args[pi++] = (uintptr_t)func;
1773 op->args[pi++] = flags;
cd9090aa 1774 TCGOP_CALLI(op) = real_args;
a7812ae4 1775
75e8b9b7 1776 /* Make sure the fields didn't overflow. */
cd9090aa 1777 tcg_debug_assert(TCGOP_CALLI(op) == real_args);
75e8b9b7 1778 tcg_debug_assert(pi <= ARRAY_SIZE(op->args));
2bece2c8 1779
34b1a49c
RH
1780#if defined(__sparc__) && !defined(__arch64__) \
1781 && !defined(CONFIG_TCG_INTERPRETER)
1782 /* Free all of the parts we allocated above. */
1783 for (i = real_args = 0; i < orig_nargs; ++i) {
1784 int is_64bit = orig_sizemask & (1 << (i+1)*2);
1785 if (is_64bit) {
085272b3
RH
1786 tcg_temp_free_internal(args[real_args++]);
1787 tcg_temp_free_internal(args[real_args++]);
34b1a49c
RH
1788 } else {
1789 real_args++;
1790 }
1791 }
1792 if (orig_sizemask & 1) {
1793 /* The 32-bit ABI returned two 32-bit pieces. Re-assemble them.
1794 Note that describing these as TCGv_i64 eliminates an unnecessary
1795 zero-extension that tcg_gen_concat_i32_i64 would create. */
085272b3 1796 tcg_gen_concat32_i64(temp_tcgv_i64(ret), retl, reth);
34b1a49c
RH
1797 tcg_temp_free_i64(retl);
1798 tcg_temp_free_i64(reth);
1799 }
1800#elif defined(TCG_TARGET_EXTEND_ARGS) && TCG_TARGET_REG_BITS == 64
2bece2c8
RH
1801 for (i = 0; i < nargs; ++i) {
1802 int is_64bit = sizemask & (1 << (i+1)*2);
1803 if (!is_64bit) {
085272b3 1804 tcg_temp_free_internal(args[i]);
2bece2c8
RH
1805 }
1806 }
1807#endif /* TCG_TARGET_EXTEND_ARGS */
c896fe29 1808}
c896fe29 1809
8fcd3692 1810static void tcg_reg_alloc_start(TCGContext *s)
c896fe29 1811{
ac3b8891 1812 int i, n;
c896fe29 1813 TCGTemp *ts;
ac3b8891
RH
1814
1815 for (i = 0, n = s->nb_globals; i < n; i++) {
c896fe29 1816 ts = &s->temps[i];
ac3b8891 1817 ts->val_type = (ts->fixed_reg ? TEMP_VAL_REG : TEMP_VAL_MEM);
c896fe29 1818 }
ac3b8891 1819 for (n = s->nb_temps; i < n; i++) {
e8996ee0 1820 ts = &s->temps[i];
ac3b8891 1821 ts->val_type = (ts->temp_local ? TEMP_VAL_MEM : TEMP_VAL_DEAD);
e8996ee0
FB
1822 ts->mem_allocated = 0;
1823 ts->fixed_reg = 0;
1824 }
f8b2f202
RH
1825
1826 memset(s->reg_to_temp, 0, sizeof(s->reg_to_temp));
c896fe29
FB
1827}
1828
f8b2f202
RH
1829static char *tcg_get_arg_str_ptr(TCGContext *s, char *buf, int buf_size,
1830 TCGTemp *ts)
c896fe29 1831{
1807f4c4 1832 int idx = temp_idx(ts);
ac56dd48 1833
fa477d25 1834 if (ts->temp_global) {
ac56dd48 1835 pstrcpy(buf, buf_size, ts->name);
f8b2f202
RH
1836 } else if (ts->temp_local) {
1837 snprintf(buf, buf_size, "loc%d", idx - s->nb_globals);
c896fe29 1838 } else {
f8b2f202 1839 snprintf(buf, buf_size, "tmp%d", idx - s->nb_globals);
c896fe29
FB
1840 }
1841 return buf;
1842}
1843
43439139
RH
1844static char *tcg_get_arg_str(TCGContext *s, char *buf,
1845 int buf_size, TCGArg arg)
f8b2f202 1846{
43439139 1847 return tcg_get_arg_str_ptr(s, buf, buf_size, arg_temp(arg));
f8b2f202
RH
1848}
1849
6e085f72
RH
1850/* Find helper name. */
1851static inline const char *tcg_find_helper(TCGContext *s, uintptr_t val)
4dc81f28 1852{
6e085f72 1853 const char *ret = NULL;
619205fd
EC
1854 if (helper_table) {
1855 TCGHelperInfo *info = g_hash_table_lookup(helper_table, (gpointer)val);
72866e82
RH
1856 if (info) {
1857 ret = info->name;
1858 }
4dc81f28 1859 }
6e085f72 1860 return ret;
4dc81f28
FB
1861}
1862
f48f3ede
BS
1863static const char * const cond_name[] =
1864{
0aed257f
RH
1865 [TCG_COND_NEVER] = "never",
1866 [TCG_COND_ALWAYS] = "always",
f48f3ede
BS
1867 [TCG_COND_EQ] = "eq",
1868 [TCG_COND_NE] = "ne",
1869 [TCG_COND_LT] = "lt",
1870 [TCG_COND_GE] = "ge",
1871 [TCG_COND_LE] = "le",
1872 [TCG_COND_GT] = "gt",
1873 [TCG_COND_LTU] = "ltu",
1874 [TCG_COND_GEU] = "geu",
1875 [TCG_COND_LEU] = "leu",
1876 [TCG_COND_GTU] = "gtu"
1877};
1878
f713d6ad
RH
1879static const char * const ldst_name[] =
1880{
1881 [MO_UB] = "ub",
1882 [MO_SB] = "sb",
1883 [MO_LEUW] = "leuw",
1884 [MO_LESW] = "lesw",
1885 [MO_LEUL] = "leul",
1886 [MO_LESL] = "lesl",
1887 [MO_LEQ] = "leq",
1888 [MO_BEUW] = "beuw",
1889 [MO_BESW] = "besw",
1890 [MO_BEUL] = "beul",
1891 [MO_BESL] = "besl",
1892 [MO_BEQ] = "beq",
1893};
1894
1f00b27f
SS
1895static const char * const alignment_name[(MO_AMASK >> MO_ASHIFT) + 1] = {
1896#ifdef ALIGNED_ONLY
1897 [MO_UNALN >> MO_ASHIFT] = "un+",
1898 [MO_ALIGN >> MO_ASHIFT] = "",
1899#else
1900 [MO_UNALN >> MO_ASHIFT] = "",
1901 [MO_ALIGN >> MO_ASHIFT] = "al+",
1902#endif
1903 [MO_ALIGN_2 >> MO_ASHIFT] = "al2+",
1904 [MO_ALIGN_4 >> MO_ASHIFT] = "al4+",
1905 [MO_ALIGN_8 >> MO_ASHIFT] = "al8+",
1906 [MO_ALIGN_16 >> MO_ASHIFT] = "al16+",
1907 [MO_ALIGN_32 >> MO_ASHIFT] = "al32+",
1908 [MO_ALIGN_64 >> MO_ASHIFT] = "al64+",
1909};
1910
b016486e
RH
1911static inline bool tcg_regset_single(TCGRegSet d)
1912{
1913 return (d & (d - 1)) == 0;
1914}
1915
1916static inline TCGReg tcg_regset_first(TCGRegSet d)
1917{
1918 if (TCG_TARGET_NB_REGS <= 32) {
1919 return ctz32(d);
1920 } else {
1921 return ctz64(d);
1922 }
1923}
1924
1894f69a 1925static void tcg_dump_ops(TCGContext *s, bool have_prefs)
c896fe29 1926{
c896fe29 1927 char buf[128];
c45cb8bb 1928 TCGOp *op;
c45cb8bb 1929
15fa08f8 1930 QTAILQ_FOREACH(op, &s->ops, link) {
c45cb8bb
RH
1931 int i, k, nb_oargs, nb_iargs, nb_cargs;
1932 const TCGOpDef *def;
c45cb8bb 1933 TCGOpcode c;
bdfb460e 1934 int col = 0;
c896fe29 1935
c45cb8bb 1936 c = op->opc;
c896fe29 1937 def = &tcg_op_defs[c];
c45cb8bb 1938
765b842a 1939 if (c == INDEX_op_insn_start) {
b016486e 1940 nb_oargs = 0;
15fa08f8 1941 col += qemu_log("\n ----");
9aef40ed
RH
1942
1943 for (i = 0; i < TARGET_INSN_START_WORDS; ++i) {
1944 target_ulong a;
7e4597d7 1945#if TARGET_LONG_BITS > TCG_TARGET_REG_BITS
efee3746 1946 a = deposit64(op->args[i * 2], 32, 32, op->args[i * 2 + 1]);
7e4597d7 1947#else
efee3746 1948 a = op->args[i];
7e4597d7 1949#endif
bdfb460e 1950 col += qemu_log(" " TARGET_FMT_lx, a);
eeacee4d 1951 }
7e4597d7 1952 } else if (c == INDEX_op_call) {
c896fe29 1953 /* variable number of arguments */
cd9090aa
RH
1954 nb_oargs = TCGOP_CALLO(op);
1955 nb_iargs = TCGOP_CALLI(op);
c896fe29 1956 nb_cargs = def->nb_cargs;
c896fe29 1957
cf066674 1958 /* function name, flags, out args */
bdfb460e 1959 col += qemu_log(" %s %s,$0x%" TCG_PRIlx ",$%d", def->name,
efee3746
RH
1960 tcg_find_helper(s, op->args[nb_oargs + nb_iargs]),
1961 op->args[nb_oargs + nb_iargs + 1], nb_oargs);
cf066674 1962 for (i = 0; i < nb_oargs; i++) {
43439139
RH
1963 col += qemu_log(",%s", tcg_get_arg_str(s, buf, sizeof(buf),
1964 op->args[i]));
b03cce8e 1965 }
cf066674 1966 for (i = 0; i < nb_iargs; i++) {
efee3746 1967 TCGArg arg = op->args[nb_oargs + i];
cf066674
RH
1968 const char *t = "<dummy>";
1969 if (arg != TCG_CALL_DUMMY_ARG) {
43439139 1970 t = tcg_get_arg_str(s, buf, sizeof(buf), arg);
eeacee4d 1971 }
bdfb460e 1972 col += qemu_log(",%s", t);
e8996ee0 1973 }
b03cce8e 1974 } else {
bdfb460e 1975 col += qemu_log(" %s ", def->name);
c45cb8bb
RH
1976
1977 nb_oargs = def->nb_oargs;
1978 nb_iargs = def->nb_iargs;
1979 nb_cargs = def->nb_cargs;
1980
d2fd745f
RH
1981 if (def->flags & TCG_OPF_VECTOR) {
1982 col += qemu_log("v%d,e%d,", 64 << TCGOP_VECL(op),
1983 8 << TCGOP_VECE(op));
1984 }
1985
b03cce8e 1986 k = 0;
c45cb8bb 1987 for (i = 0; i < nb_oargs; i++) {
eeacee4d 1988 if (k != 0) {
bdfb460e 1989 col += qemu_log(",");
eeacee4d 1990 }
43439139
RH
1991 col += qemu_log("%s", tcg_get_arg_str(s, buf, sizeof(buf),
1992 op->args[k++]));
b03cce8e 1993 }
c45cb8bb 1994 for (i = 0; i < nb_iargs; i++) {
eeacee4d 1995 if (k != 0) {
bdfb460e 1996 col += qemu_log(",");
eeacee4d 1997 }
43439139
RH
1998 col += qemu_log("%s", tcg_get_arg_str(s, buf, sizeof(buf),
1999 op->args[k++]));
b03cce8e 2000 }
be210acb
RH
2001 switch (c) {
2002 case INDEX_op_brcond_i32:
be210acb 2003 case INDEX_op_setcond_i32:
ffc5ea09 2004 case INDEX_op_movcond_i32:
ffc5ea09 2005 case INDEX_op_brcond2_i32:
be210acb 2006 case INDEX_op_setcond2_i32:
ffc5ea09 2007 case INDEX_op_brcond_i64:
be210acb 2008 case INDEX_op_setcond_i64:
ffc5ea09 2009 case INDEX_op_movcond_i64:
212be173 2010 case INDEX_op_cmp_vec:
efee3746
RH
2011 if (op->args[k] < ARRAY_SIZE(cond_name)
2012 && cond_name[op->args[k]]) {
2013 col += qemu_log(",%s", cond_name[op->args[k++]]);
eeacee4d 2014 } else {
efee3746 2015 col += qemu_log(",$0x%" TCG_PRIlx, op->args[k++]);
eeacee4d 2016 }
f48f3ede 2017 i = 1;
be210acb 2018 break;
f713d6ad
RH
2019 case INDEX_op_qemu_ld_i32:
2020 case INDEX_op_qemu_st_i32:
2021 case INDEX_op_qemu_ld_i64:
2022 case INDEX_op_qemu_st_i64:
59227d5d 2023 {
efee3746 2024 TCGMemOpIdx oi = op->args[k++];
59227d5d
RH
2025 TCGMemOp op = get_memop(oi);
2026 unsigned ix = get_mmuidx(oi);
2027
59c4b7e8 2028 if (op & ~(MO_AMASK | MO_BSWAP | MO_SSIZE)) {
bdfb460e 2029 col += qemu_log(",$0x%x,%u", op, ix);
59c4b7e8 2030 } else {
1f00b27f
SS
2031 const char *s_al, *s_op;
2032 s_al = alignment_name[(op & MO_AMASK) >> MO_ASHIFT];
59c4b7e8 2033 s_op = ldst_name[op & (MO_BSWAP | MO_SSIZE)];
bdfb460e 2034 col += qemu_log(",%s%s,%u", s_al, s_op, ix);
59227d5d
RH
2035 }
2036 i = 1;
f713d6ad 2037 }
f713d6ad 2038 break;
be210acb 2039 default:
f48f3ede 2040 i = 0;
be210acb
RH
2041 break;
2042 }
51e3972c
RH
2043 switch (c) {
2044 case INDEX_op_set_label:
2045 case INDEX_op_br:
2046 case INDEX_op_brcond_i32:
2047 case INDEX_op_brcond_i64:
2048 case INDEX_op_brcond2_i32:
efee3746
RH
2049 col += qemu_log("%s$L%d", k ? "," : "",
2050 arg_label(op->args[k])->id);
51e3972c
RH
2051 i++, k++;
2052 break;
2053 default:
2054 break;
2055 }
2056 for (; i < nb_cargs; i++, k++) {
efee3746 2057 col += qemu_log("%s$0x%" TCG_PRIlx, k ? "," : "", op->args[k]);
bdfb460e
RH
2058 }
2059 }
bdfb460e 2060
1894f69a
RH
2061 if (have_prefs || op->life) {
2062 for (; col < 40; ++col) {
bdfb460e
RH
2063 putc(' ', qemu_logfile);
2064 }
1894f69a
RH
2065 }
2066
2067 if (op->life) {
2068 unsigned life = op->life;
bdfb460e
RH
2069
2070 if (life & (SYNC_ARG * 3)) {
2071 qemu_log(" sync:");
2072 for (i = 0; i < 2; ++i) {
2073 if (life & (SYNC_ARG << i)) {
2074 qemu_log(" %d", i);
2075 }
2076 }
2077 }
2078 life /= DEAD_ARG;
2079 if (life) {
2080 qemu_log(" dead:");
2081 for (i = 0; life; ++i, life >>= 1) {
2082 if (life & 1) {
2083 qemu_log(" %d", i);
2084 }
2085 }
b03cce8e 2086 }
c896fe29 2087 }
1894f69a
RH
2088
2089 if (have_prefs) {
2090 for (i = 0; i < nb_oargs; ++i) {
2091 TCGRegSet set = op->output_pref[i];
2092
2093 if (i == 0) {
2094 qemu_log(" pref=");
2095 } else {
2096 qemu_log(",");
2097 }
2098 if (set == 0) {
2099 qemu_log("none");
2100 } else if (set == MAKE_64BIT_MASK(0, TCG_TARGET_NB_REGS)) {
2101 qemu_log("all");
2102#ifdef CONFIG_DEBUG_TCG
2103 } else if (tcg_regset_single(set)) {
2104 TCGReg reg = tcg_regset_first(set);
2105 qemu_log("%s", tcg_target_reg_names[reg]);
2106#endif
2107 } else if (TCG_TARGET_NB_REGS <= 32) {
2108 qemu_log("%#x", (uint32_t)set);
2109 } else {
2110 qemu_log("%#" PRIx64, (uint64_t)set);
2111 }
2112 }
2113 }
2114
eeacee4d 2115 qemu_log("\n");
c896fe29
FB
2116 }
2117}
2118
2119/* we give more priority to constraints with less registers */
2120static int get_constraint_priority(const TCGOpDef *def, int k)
2121{
2122 const TCGArgConstraint *arg_ct;
2123
2124 int i, n;
2125 arg_ct = &def->args_ct[k];
2126 if (arg_ct->ct & TCG_CT_ALIAS) {
2127 /* an alias is equivalent to a single register */
2128 n = 1;
2129 } else {
2130 if (!(arg_ct->ct & TCG_CT_REG))
2131 return 0;
2132 n = 0;
2133 for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
2134 if (tcg_regset_test_reg(arg_ct->u.regs, i))
2135 n++;
2136 }
2137 }
2138 return TCG_TARGET_NB_REGS - n + 1;
2139}
2140
2141/* sort from highest priority to lowest */
2142static void sort_constraints(TCGOpDef *def, int start, int n)
2143{
2144 int i, j, p1, p2, tmp;
2145
2146 for(i = 0; i < n; i++)
2147 def->sorted_args[start + i] = start + i;
2148 if (n <= 1)
2149 return;
2150 for(i = 0; i < n - 1; i++) {
2151 for(j = i + 1; j < n; j++) {
2152 p1 = get_constraint_priority(def, def->sorted_args[start + i]);
2153 p2 = get_constraint_priority(def, def->sorted_args[start + j]);
2154 if (p1 < p2) {
2155 tmp = def->sorted_args[start + i];
2156 def->sorted_args[start + i] = def->sorted_args[start + j];
2157 def->sorted_args[start + j] = tmp;
2158 }
2159 }
2160 }
2161}
2162
f69d277e 2163static void process_op_defs(TCGContext *s)
c896fe29 2164{
a9751609 2165 TCGOpcode op;
c896fe29 2166
f69d277e
RH
2167 for (op = 0; op < NB_OPS; op++) {
2168 TCGOpDef *def = &tcg_op_defs[op];
2169 const TCGTargetOpDef *tdefs;
069ea736
RH
2170 TCGType type;
2171 int i, nb_args;
f69d277e
RH
2172
2173 if (def->flags & TCG_OPF_NOT_PRESENT) {
2174 continue;
2175 }
2176
c896fe29 2177 nb_args = def->nb_iargs + def->nb_oargs;
f69d277e
RH
2178 if (nb_args == 0) {
2179 continue;
2180 }
2181
2182 tdefs = tcg_target_op_def(op);
2183 /* Missing TCGTargetOpDef entry. */
2184 tcg_debug_assert(tdefs != NULL);
2185
069ea736 2186 type = (def->flags & TCG_OPF_64BIT ? TCG_TYPE_I64 : TCG_TYPE_I32);
f69d277e
RH
2187 for (i = 0; i < nb_args; i++) {
2188 const char *ct_str = tdefs->args_ct_str[i];
2189 /* Incomplete TCGTargetOpDef entry. */
eabb7b91 2190 tcg_debug_assert(ct_str != NULL);
f69d277e 2191
ccb1bb66 2192 def->args_ct[i].u.regs = 0;
c896fe29 2193 def->args_ct[i].ct = 0;
17280ff4
RH
2194 while (*ct_str != '\0') {
2195 switch(*ct_str) {
2196 case '0' ... '9':
2197 {
2198 int oarg = *ct_str - '0';
2199 tcg_debug_assert(ct_str == tdefs->args_ct_str[i]);
2200 tcg_debug_assert(oarg < def->nb_oargs);
2201 tcg_debug_assert(def->args_ct[oarg].ct & TCG_CT_REG);
2202 /* TCG_CT_ALIAS is for the output arguments.
2203 The input is tagged with TCG_CT_IALIAS. */
2204 def->args_ct[i] = def->args_ct[oarg];
2205 def->args_ct[oarg].ct |= TCG_CT_ALIAS;
2206 def->args_ct[oarg].alias_index = i;
2207 def->args_ct[i].ct |= TCG_CT_IALIAS;
2208 def->args_ct[i].alias_index = oarg;
c896fe29 2209 }
17280ff4
RH
2210 ct_str++;
2211 break;
2212 case '&':
2213 def->args_ct[i].ct |= TCG_CT_NEWREG;
2214 ct_str++;
2215 break;
2216 case 'i':
2217 def->args_ct[i].ct |= TCG_CT_CONST;
2218 ct_str++;
2219 break;
2220 default:
2221 ct_str = target_parse_constraint(&def->args_ct[i],
2222 ct_str, type);
2223 /* Typo in TCGTargetOpDef constraint. */
2224 tcg_debug_assert(ct_str != NULL);
c896fe29
FB
2225 }
2226 }
2227 }
2228
c68aaa18 2229 /* TCGTargetOpDef entry with too much information? */
eabb7b91 2230 tcg_debug_assert(i == TCG_MAX_OP_ARGS || tdefs->args_ct_str[i] == NULL);
c68aaa18 2231
c896fe29
FB
2232 /* sort the constraints (XXX: this is just an heuristic) */
2233 sort_constraints(def, 0, def->nb_oargs);
2234 sort_constraints(def, def->nb_oargs, def->nb_iargs);
a9751609 2235 }
c896fe29
FB
2236}
2237
0c627cdc
RH
2238void tcg_op_remove(TCGContext *s, TCGOp *op)
2239{
d88a117e
RH
2240 TCGLabel *label;
2241
2242 switch (op->opc) {
2243 case INDEX_op_br:
2244 label = arg_label(op->args[0]);
2245 label->refs--;
2246 break;
2247 case INDEX_op_brcond_i32:
2248 case INDEX_op_brcond_i64:
2249 label = arg_label(op->args[3]);
2250 label->refs--;
2251 break;
2252 case INDEX_op_brcond2_i32:
2253 label = arg_label(op->args[5]);
2254 label->refs--;
2255 break;
2256 default:
2257 break;
2258 }
2259
15fa08f8
RH
2260 QTAILQ_REMOVE(&s->ops, op, link);
2261 QTAILQ_INSERT_TAIL(&s->free_ops, op, link);
abebf925 2262 s->nb_ops--;
0c627cdc
RH
2263
2264#ifdef CONFIG_PROFILER
c3fac113 2265 atomic_set(&s->prof.del_op_count, s->prof.del_op_count + 1);
0c627cdc
RH
2266#endif
2267}
2268
15fa08f8 2269static TCGOp *tcg_op_alloc(TCGOpcode opc)
5a18407f 2270{
15fa08f8
RH
2271 TCGContext *s = tcg_ctx;
2272 TCGOp *op;
5a18407f 2273
15fa08f8
RH
2274 if (likely(QTAILQ_EMPTY(&s->free_ops))) {
2275 op = tcg_malloc(sizeof(TCGOp));
2276 } else {
2277 op = QTAILQ_FIRST(&s->free_ops);
2278 QTAILQ_REMOVE(&s->free_ops, op, link);
2279 }
2280 memset(op, 0, offsetof(TCGOp, link));
2281 op->opc = opc;
abebf925 2282 s->nb_ops++;
5a18407f 2283
15fa08f8
RH
2284 return op;
2285}
2286
2287TCGOp *tcg_emit_op(TCGOpcode opc)
2288{
2289 TCGOp *op = tcg_op_alloc(opc);
2290 QTAILQ_INSERT_TAIL(&tcg_ctx->ops, op, link);
2291 return op;
2292}
5a18407f 2293
ac1043f6 2294TCGOp *tcg_op_insert_before(TCGContext *s, TCGOp *old_op, TCGOpcode opc)
15fa08f8
RH
2295{
2296 TCGOp *new_op = tcg_op_alloc(opc);
2297 QTAILQ_INSERT_BEFORE(old_op, new_op, link);
5a18407f
RH
2298 return new_op;
2299}
2300
ac1043f6 2301TCGOp *tcg_op_insert_after(TCGContext *s, TCGOp *old_op, TCGOpcode opc)
5a18407f 2302{
15fa08f8
RH
2303 TCGOp *new_op = tcg_op_alloc(opc);
2304 QTAILQ_INSERT_AFTER(&s->ops, old_op, new_op, link);
5a18407f
RH
2305 return new_op;
2306}
2307
b4fc67c7
RH
2308/* Reachable analysis : remove unreachable code. */
2309static void reachable_code_pass(TCGContext *s)
2310{
2311 TCGOp *op, *op_next;
2312 bool dead = false;
2313
2314 QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) {
2315 bool remove = dead;
2316 TCGLabel *label;
2317 int call_flags;
2318
2319 switch (op->opc) {
2320 case INDEX_op_set_label:
2321 label = arg_label(op->args[0]);
2322 if (label->refs == 0) {
2323 /*
2324 * While there is an occasional backward branch, virtually
2325 * all branches generated by the translators are forward.
2326 * Which means that generally we will have already removed
2327 * all references to the label that will be, and there is
2328 * little to be gained by iterating.
2329 */
2330 remove = true;
2331 } else {
2332 /* Once we see a label, insns become live again. */
2333 dead = false;
2334 remove = false;
2335
2336 /*
2337 * Optimization can fold conditional branches to unconditional.
2338 * If we find a label with one reference which is preceded by
2339 * an unconditional branch to it, remove both. This needed to
2340 * wait until the dead code in between them was removed.
2341 */
2342 if (label->refs == 1) {
eae3eb3e 2343 TCGOp *op_prev = QTAILQ_PREV(op, link);
b4fc67c7
RH
2344 if (op_prev->opc == INDEX_op_br &&
2345 label == arg_label(op_prev->args[0])) {
2346 tcg_op_remove(s, op_prev);
2347 remove = true;
2348 }
2349 }
2350 }
2351 break;
2352
2353 case INDEX_op_br:
2354 case INDEX_op_exit_tb:
2355 case INDEX_op_goto_ptr:
2356 /* Unconditional branches; everything following is dead. */
2357 dead = true;
2358 break;
2359
2360 case INDEX_op_call:
2361 /* Notice noreturn helper calls, raising exceptions. */
2362 call_flags = op->args[TCGOP_CALLO(op) + TCGOP_CALLI(op) + 1];
2363 if (call_flags & TCG_CALL_NO_RETURN) {
2364 dead = true;
2365 }
2366 break;
2367
2368 case INDEX_op_insn_start:
2369 /* Never remove -- we need to keep these for unwind. */
2370 remove = false;
2371 break;
2372
2373 default:
2374 break;
2375 }
2376
2377 if (remove) {
2378 tcg_op_remove(s, op);
2379 }
2380 }
2381}
2382
c70fbf0a
RH
2383#define TS_DEAD 1
2384#define TS_MEM 2
2385
5a18407f
RH
2386#define IS_DEAD_ARG(n) (arg_life & (DEAD_ARG << (n)))
2387#define NEED_SYNC_ARG(n) (arg_life & (SYNC_ARG << (n)))
2388
25f49c5f
RH
2389/* For liveness_pass_1, the register preferences for a given temp. */
2390static inline TCGRegSet *la_temp_pref(TCGTemp *ts)
2391{
2392 return ts->state_ptr;
2393}
2394
2395/* For liveness_pass_1, reset the preferences for a given temp to the
2396 * maximal regset for its type.
2397 */
2398static inline void la_reset_pref(TCGTemp *ts)
2399{
2400 *la_temp_pref(ts)
2401 = (ts->state == TS_DEAD ? 0 : tcg_target_available_regs[ts->type]);
2402}
2403
9c43b68d
AJ
2404/* liveness analysis: end of function: all temps are dead, and globals
2405 should be in memory. */
2616c808 2406static void la_func_end(TCGContext *s, int ng, int nt)
c896fe29 2407{
b83eabea
RH
2408 int i;
2409
2410 for (i = 0; i < ng; ++i) {
2411 s->temps[i].state = TS_DEAD | TS_MEM;
25f49c5f 2412 la_reset_pref(&s->temps[i]);
b83eabea
RH
2413 }
2414 for (i = ng; i < nt; ++i) {
2415 s->temps[i].state = TS_DEAD;
25f49c5f 2416 la_reset_pref(&s->temps[i]);
b83eabea 2417 }
c896fe29
FB
2418}
2419
9c43b68d
AJ
2420/* liveness analysis: end of basic block: all temps are dead, globals
2421 and local temps should be in memory. */
2616c808 2422static void la_bb_end(TCGContext *s, int ng, int nt)
641d5fbe 2423{
b83eabea 2424 int i;
641d5fbe 2425
b83eabea
RH
2426 for (i = 0; i < ng; ++i) {
2427 s->temps[i].state = TS_DEAD | TS_MEM;
25f49c5f 2428 la_reset_pref(&s->temps[i]);
b83eabea
RH
2429 }
2430 for (i = ng; i < nt; ++i) {
2431 s->temps[i].state = (s->temps[i].temp_local
2432 ? TS_DEAD | TS_MEM
2433 : TS_DEAD);
25f49c5f 2434 la_reset_pref(&s->temps[i]);
641d5fbe
FB
2435 }
2436}
2437
f65a061c
RH
2438/* liveness analysis: sync globals back to memory. */
2439static void la_global_sync(TCGContext *s, int ng)
2440{
2441 int i;
2442
2443 for (i = 0; i < ng; ++i) {
25f49c5f
RH
2444 int state = s->temps[i].state;
2445 s->temps[i].state = state | TS_MEM;
2446 if (state == TS_DEAD) {
2447 /* If the global was previously dead, reset prefs. */
2448 la_reset_pref(&s->temps[i]);
2449 }
f65a061c
RH
2450 }
2451}
2452
2453/* liveness analysis: sync globals back to memory and kill. */
2454static void la_global_kill(TCGContext *s, int ng)
2455{
2456 int i;
2457
2458 for (i = 0; i < ng; i++) {
2459 s->temps[i].state = TS_DEAD | TS_MEM;
25f49c5f
RH
2460 la_reset_pref(&s->temps[i]);
2461 }
2462}
2463
2464/* liveness analysis: note live globals crossing calls. */
2465static void la_cross_call(TCGContext *s, int nt)
2466{
2467 TCGRegSet mask = ~tcg_target_call_clobber_regs;
2468 int i;
2469
2470 for (i = 0; i < nt; i++) {
2471 TCGTemp *ts = &s->temps[i];
2472 if (!(ts->state & TS_DEAD)) {
2473 TCGRegSet *pset = la_temp_pref(ts);
2474 TCGRegSet set = *pset;
2475
2476 set &= mask;
2477 /* If the combination is not possible, restart. */
2478 if (set == 0) {
2479 set = tcg_target_available_regs[ts->type] & mask;
2480 }
2481 *pset = set;
2482 }
f65a061c
RH
2483 }
2484}
2485
a1b3c48d 2486/* Liveness analysis : update the opc_arg_life array to tell if a
c896fe29
FB
2487 given input arguments is dead. Instructions updating dead
2488 temporaries are removed. */
b83eabea 2489static void liveness_pass_1(TCGContext *s)
c896fe29 2490{
c70fbf0a 2491 int nb_globals = s->nb_globals;
2616c808 2492 int nb_temps = s->nb_temps;
15fa08f8 2493 TCGOp *op, *op_prev;
25f49c5f
RH
2494 TCGRegSet *prefs;
2495 int i;
2496
2497 prefs = tcg_malloc(sizeof(TCGRegSet) * nb_temps);
2498 for (i = 0; i < nb_temps; ++i) {
2499 s->temps[i].state_ptr = prefs + i;
2500 }
a1b3c48d 2501
ae36a246 2502 /* ??? Should be redundant with the exit_tb that ends the TB. */
2616c808 2503 la_func_end(s, nb_globals, nb_temps);
c896fe29 2504
eae3eb3e 2505 QTAILQ_FOREACH_REVERSE_SAFE(op, &s->ops, link, op_prev) {
25f49c5f 2506 int nb_iargs, nb_oargs;
c45cb8bb
RH
2507 TCGOpcode opc_new, opc_new2;
2508 bool have_opc_new2;
a1b3c48d 2509 TCGLifeData arg_life = 0;
25f49c5f 2510 TCGTemp *ts;
c45cb8bb
RH
2511 TCGOpcode opc = op->opc;
2512 const TCGOpDef *def = &tcg_op_defs[opc];
2513
c45cb8bb 2514 switch (opc) {
c896fe29 2515 case INDEX_op_call:
c6e113f5
FB
2516 {
2517 int call_flags;
25f49c5f 2518 int nb_call_regs;
c896fe29 2519
cd9090aa
RH
2520 nb_oargs = TCGOP_CALLO(op);
2521 nb_iargs = TCGOP_CALLI(op);
efee3746 2522 call_flags = op->args[nb_oargs + nb_iargs + 1];
c6e113f5 2523
c45cb8bb 2524 /* pure functions can be removed if their result is unused */
78505279 2525 if (call_flags & TCG_CALL_NO_SIDE_EFFECTS) {
cf066674 2526 for (i = 0; i < nb_oargs; i++) {
25f49c5f
RH
2527 ts = arg_temp(op->args[i]);
2528 if (ts->state != TS_DEAD) {
c6e113f5 2529 goto do_not_remove_call;
9c43b68d 2530 }
c6e113f5 2531 }
c45cb8bb 2532 goto do_remove;
152c35aa
RH
2533 }
2534 do_not_remove_call:
c896fe29 2535
25f49c5f 2536 /* Output args are dead. */
152c35aa 2537 for (i = 0; i < nb_oargs; i++) {
25f49c5f
RH
2538 ts = arg_temp(op->args[i]);
2539 if (ts->state & TS_DEAD) {
152c35aa
RH
2540 arg_life |= DEAD_ARG << i;
2541 }
25f49c5f 2542 if (ts->state & TS_MEM) {
152c35aa 2543 arg_life |= SYNC_ARG << i;
c6e113f5 2544 }
25f49c5f
RH
2545 ts->state = TS_DEAD;
2546 la_reset_pref(ts);
2547
2548 /* Not used -- it will be tcg_target_call_oarg_regs[i]. */
2549 op->output_pref[i] = 0;
152c35aa 2550 }
78505279 2551
152c35aa
RH
2552 if (!(call_flags & (TCG_CALL_NO_WRITE_GLOBALS |
2553 TCG_CALL_NO_READ_GLOBALS))) {
f65a061c 2554 la_global_kill(s, nb_globals);
152c35aa 2555 } else if (!(call_flags & TCG_CALL_NO_READ_GLOBALS)) {
f65a061c 2556 la_global_sync(s, nb_globals);
152c35aa 2557 }
b9c18f56 2558
25f49c5f 2559 /* Record arguments that die in this helper. */
152c35aa 2560 for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
25f49c5f
RH
2561 ts = arg_temp(op->args[i]);
2562 if (ts && ts->state & TS_DEAD) {
152c35aa 2563 arg_life |= DEAD_ARG << i;
c6e113f5 2564 }
152c35aa 2565 }
25f49c5f
RH
2566
2567 /* For all live registers, remove call-clobbered prefs. */
2568 la_cross_call(s, nb_temps);
2569
2570 nb_call_regs = ARRAY_SIZE(tcg_target_call_iarg_regs);
2571
2572 /* Input arguments are live for preceding opcodes. */
2573 for (i = 0; i < nb_iargs; i++) {
2574 ts = arg_temp(op->args[i + nb_oargs]);
2575 if (ts && ts->state & TS_DEAD) {
2576 /* For those arguments that die, and will be allocated
2577 * in registers, clear the register set for that arg,
2578 * to be filled in below. For args that will be on
2579 * the stack, reset to any available reg.
2580 */
2581 *la_temp_pref(ts)
2582 = (i < nb_call_regs ? 0 :
2583 tcg_target_available_regs[ts->type]);
2584 ts->state &= ~TS_DEAD;
2585 }
2586 }
2587
2588 /* For each input argument, add its input register to prefs.
2589 If a temp is used once, this produces a single set bit. */
2590 for (i = 0; i < MIN(nb_call_regs, nb_iargs); i++) {
2591 ts = arg_temp(op->args[i + nb_oargs]);
2592 if (ts) {
2593 tcg_regset_set_reg(*la_temp_pref(ts),
2594 tcg_target_call_iarg_regs[i]);
c19f47bf 2595 }
c896fe29 2596 }
c896fe29 2597 }
c896fe29 2598 break;
765b842a 2599 case INDEX_op_insn_start:
c896fe29 2600 break;
5ff9d6a4 2601 case INDEX_op_discard:
5ff9d6a4 2602 /* mark the temporary as dead */
25f49c5f
RH
2603 ts = arg_temp(op->args[0]);
2604 ts->state = TS_DEAD;
2605 la_reset_pref(ts);
5ff9d6a4 2606 break;
1305c451
RH
2607
2608 case INDEX_op_add2_i32:
c45cb8bb 2609 opc_new = INDEX_op_add_i32;
f1fae40c 2610 goto do_addsub2;
1305c451 2611 case INDEX_op_sub2_i32:
c45cb8bb 2612 opc_new = INDEX_op_sub_i32;
f1fae40c
RH
2613 goto do_addsub2;
2614 case INDEX_op_add2_i64:
c45cb8bb 2615 opc_new = INDEX_op_add_i64;
f1fae40c
RH
2616 goto do_addsub2;
2617 case INDEX_op_sub2_i64:
c45cb8bb 2618 opc_new = INDEX_op_sub_i64;
f1fae40c 2619 do_addsub2:
1305c451
RH
2620 nb_iargs = 4;
2621 nb_oargs = 2;
2622 /* Test if the high part of the operation is dead, but not
2623 the low part. The result can be optimized to a simple
2624 add or sub. This happens often for x86_64 guest when the
2625 cpu mode is set to 32 bit. */
b83eabea
RH
2626 if (arg_temp(op->args[1])->state == TS_DEAD) {
2627 if (arg_temp(op->args[0])->state == TS_DEAD) {
1305c451
RH
2628 goto do_remove;
2629 }
c45cb8bb
RH
2630 /* Replace the opcode and adjust the args in place,
2631 leaving 3 unused args at the end. */
2632 op->opc = opc = opc_new;
efee3746
RH
2633 op->args[1] = op->args[2];
2634 op->args[2] = op->args[4];
1305c451
RH
2635 /* Fall through and mark the single-word operation live. */
2636 nb_iargs = 2;
2637 nb_oargs = 1;
2638 }
2639 goto do_not_remove;
2640
1414968a 2641 case INDEX_op_mulu2_i32:
c45cb8bb
RH
2642 opc_new = INDEX_op_mul_i32;
2643 opc_new2 = INDEX_op_muluh_i32;
2644 have_opc_new2 = TCG_TARGET_HAS_muluh_i32;
03271524 2645 goto do_mul2;
f1fae40c 2646 case INDEX_op_muls2_i32:
c45cb8bb
RH
2647 opc_new = INDEX_op_mul_i32;
2648 opc_new2 = INDEX_op_mulsh_i32;
2649 have_opc_new2 = TCG_TARGET_HAS_mulsh_i32;
f1fae40c
RH
2650 goto do_mul2;
2651 case INDEX_op_mulu2_i64:
c45cb8bb
RH
2652 opc_new = INDEX_op_mul_i64;
2653 opc_new2 = INDEX_op_muluh_i64;
2654 have_opc_new2 = TCG_TARGET_HAS_muluh_i64;
03271524 2655 goto do_mul2;
f1fae40c 2656 case INDEX_op_muls2_i64:
c45cb8bb
RH
2657 opc_new = INDEX_op_mul_i64;
2658 opc_new2 = INDEX_op_mulsh_i64;
2659 have_opc_new2 = TCG_TARGET_HAS_mulsh_i64;
03271524 2660 goto do_mul2;
f1fae40c 2661 do_mul2:
1414968a
RH
2662 nb_iargs = 2;
2663 nb_oargs = 2;
b83eabea
RH
2664 if (arg_temp(op->args[1])->state == TS_DEAD) {
2665 if (arg_temp(op->args[0])->state == TS_DEAD) {
03271524 2666 /* Both parts of the operation are dead. */
1414968a
RH
2667 goto do_remove;
2668 }
03271524 2669 /* The high part of the operation is dead; generate the low. */
c45cb8bb 2670 op->opc = opc = opc_new;
efee3746
RH
2671 op->args[1] = op->args[2];
2672 op->args[2] = op->args[3];
b83eabea 2673 } else if (arg_temp(op->args[0])->state == TS_DEAD && have_opc_new2) {
c45cb8bb
RH
2674 /* The low part of the operation is dead; generate the high. */
2675 op->opc = opc = opc_new2;
efee3746
RH
2676 op->args[0] = op->args[1];
2677 op->args[1] = op->args[2];
2678 op->args[2] = op->args[3];
03271524
RH
2679 } else {
2680 goto do_not_remove;
1414968a 2681 }
03271524
RH
2682 /* Mark the single-word operation live. */
2683 nb_oargs = 1;
1414968a
RH
2684 goto do_not_remove;
2685
c896fe29 2686 default:
1305c451 2687 /* XXX: optimize by hardcoding common cases (e.g. triadic ops) */
49516bc0
AJ
2688 nb_iargs = def->nb_iargs;
2689 nb_oargs = def->nb_oargs;
c896fe29 2690
49516bc0
AJ
2691 /* Test if the operation can be removed because all
2692 its outputs are dead. We assume that nb_oargs == 0
2693 implies side effects */
2694 if (!(def->flags & TCG_OPF_SIDE_EFFECTS) && nb_oargs != 0) {
c45cb8bb 2695 for (i = 0; i < nb_oargs; i++) {
b83eabea 2696 if (arg_temp(op->args[i])->state != TS_DEAD) {
49516bc0 2697 goto do_not_remove;
9c43b68d 2698 }
49516bc0 2699 }
152c35aa
RH
2700 goto do_remove;
2701 }
2702 goto do_not_remove;
49516bc0 2703
152c35aa
RH
2704 do_remove:
2705 tcg_op_remove(s, op);
2706 break;
2707
2708 do_not_remove:
152c35aa 2709 for (i = 0; i < nb_oargs; i++) {
25f49c5f
RH
2710 ts = arg_temp(op->args[i]);
2711
2712 /* Remember the preference of the uses that followed. */
2713 op->output_pref[i] = *la_temp_pref(ts);
2714
2715 /* Output args are dead. */
2716 if (ts->state & TS_DEAD) {
152c35aa 2717 arg_life |= DEAD_ARG << i;
49516bc0 2718 }
25f49c5f 2719 if (ts->state & TS_MEM) {
152c35aa
RH
2720 arg_life |= SYNC_ARG << i;
2721 }
25f49c5f
RH
2722 ts->state = TS_DEAD;
2723 la_reset_pref(ts);
152c35aa 2724 }
49516bc0 2725
25f49c5f 2726 /* If end of basic block, update. */
ae36a246
RH
2727 if (def->flags & TCG_OPF_BB_EXIT) {
2728 la_func_end(s, nb_globals, nb_temps);
2729 } else if (def->flags & TCG_OPF_BB_END) {
2616c808 2730 la_bb_end(s, nb_globals, nb_temps);
152c35aa 2731 } else if (def->flags & TCG_OPF_SIDE_EFFECTS) {
f65a061c 2732 la_global_sync(s, nb_globals);
25f49c5f
RH
2733 if (def->flags & TCG_OPF_CALL_CLOBBER) {
2734 la_cross_call(s, nb_temps);
2735 }
152c35aa
RH
2736 }
2737
25f49c5f 2738 /* Record arguments that die in this opcode. */
152c35aa 2739 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
25f49c5f
RH
2740 ts = arg_temp(op->args[i]);
2741 if (ts->state & TS_DEAD) {
152c35aa 2742 arg_life |= DEAD_ARG << i;
c896fe29 2743 }
c896fe29 2744 }
25f49c5f
RH
2745
2746 /* Input arguments are live for preceding opcodes. */
152c35aa 2747 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
25f49c5f
RH
2748 ts = arg_temp(op->args[i]);
2749 if (ts->state & TS_DEAD) {
2750 /* For operands that were dead, initially allow
2751 all regs for the type. */
2752 *la_temp_pref(ts) = tcg_target_available_regs[ts->type];
2753 ts->state &= ~TS_DEAD;
2754 }
2755 }
2756
2757 /* Incorporate constraints for this operand. */
2758 switch (opc) {
2759 case INDEX_op_mov_i32:
2760 case INDEX_op_mov_i64:
2761 /* Note that these are TCG_OPF_NOT_PRESENT and do not
2762 have proper constraints. That said, special case
2763 moves to propagate preferences backward. */
2764 if (IS_DEAD_ARG(1)) {
2765 *la_temp_pref(arg_temp(op->args[0]))
2766 = *la_temp_pref(arg_temp(op->args[1]));
2767 }
2768 break;
2769
2770 default:
2771 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
2772 const TCGArgConstraint *ct = &def->args_ct[i];
2773 TCGRegSet set, *pset;
2774
2775 ts = arg_temp(op->args[i]);
2776 pset = la_temp_pref(ts);
2777 set = *pset;
2778
2779 set &= ct->u.regs;
2780 if (ct->ct & TCG_CT_IALIAS) {
2781 set &= op->output_pref[ct->alias_index];
2782 }
2783 /* If the combination is not possible, restart. */
2784 if (set == 0) {
2785 set = ct->u.regs;
2786 }
2787 *pset = set;
2788 }
2789 break;
152c35aa 2790 }
c896fe29
FB
2791 break;
2792 }
bee158cb 2793 op->life = arg_life;
1ff0a2c5 2794 }
c896fe29 2795}
c896fe29 2796
5a18407f 2797/* Liveness analysis: Convert indirect regs to direct temporaries. */
b83eabea 2798static bool liveness_pass_2(TCGContext *s)
5a18407f
RH
2799{
2800 int nb_globals = s->nb_globals;
15fa08f8 2801 int nb_temps, i;
5a18407f 2802 bool changes = false;
15fa08f8 2803 TCGOp *op, *op_next;
5a18407f 2804
5a18407f
RH
2805 /* Create a temporary for each indirect global. */
2806 for (i = 0; i < nb_globals; ++i) {
2807 TCGTemp *its = &s->temps[i];
2808 if (its->indirect_reg) {
2809 TCGTemp *dts = tcg_temp_alloc(s);
2810 dts->type = its->type;
2811 dts->base_type = its->base_type;
b83eabea
RH
2812 its->state_ptr = dts;
2813 } else {
2814 its->state_ptr = NULL;
5a18407f 2815 }
b83eabea
RH
2816 /* All globals begin dead. */
2817 its->state = TS_DEAD;
2818 }
2819 for (nb_temps = s->nb_temps; i < nb_temps; ++i) {
2820 TCGTemp *its = &s->temps[i];
2821 its->state_ptr = NULL;
2822 its->state = TS_DEAD;
5a18407f 2823 }
5a18407f 2824
15fa08f8 2825 QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) {
5a18407f
RH
2826 TCGOpcode opc = op->opc;
2827 const TCGOpDef *def = &tcg_op_defs[opc];
2828 TCGLifeData arg_life = op->life;
2829 int nb_iargs, nb_oargs, call_flags;
b83eabea 2830 TCGTemp *arg_ts, *dir_ts;
5a18407f 2831
5a18407f 2832 if (opc == INDEX_op_call) {
cd9090aa
RH
2833 nb_oargs = TCGOP_CALLO(op);
2834 nb_iargs = TCGOP_CALLI(op);
efee3746 2835 call_flags = op->args[nb_oargs + nb_iargs + 1];
5a18407f
RH
2836 } else {
2837 nb_iargs = def->nb_iargs;
2838 nb_oargs = def->nb_oargs;
2839
2840 /* Set flags similar to how calls require. */
2841 if (def->flags & TCG_OPF_BB_END) {
2842 /* Like writing globals: save_globals */
2843 call_flags = 0;
2844 } else if (def->flags & TCG_OPF_SIDE_EFFECTS) {
2845 /* Like reading globals: sync_globals */
2846 call_flags = TCG_CALL_NO_WRITE_GLOBALS;
2847 } else {
2848 /* No effect on globals. */
2849 call_flags = (TCG_CALL_NO_READ_GLOBALS |
2850 TCG_CALL_NO_WRITE_GLOBALS);
2851 }
2852 }
2853
2854 /* Make sure that input arguments are available. */
2855 for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
b83eabea
RH
2856 arg_ts = arg_temp(op->args[i]);
2857 if (arg_ts) {
2858 dir_ts = arg_ts->state_ptr;
2859 if (dir_ts && arg_ts->state == TS_DEAD) {
2860 TCGOpcode lopc = (arg_ts->type == TCG_TYPE_I32
5a18407f
RH
2861 ? INDEX_op_ld_i32
2862 : INDEX_op_ld_i64);
ac1043f6 2863 TCGOp *lop = tcg_op_insert_before(s, op, lopc);
5a18407f 2864
b83eabea
RH
2865 lop->args[0] = temp_arg(dir_ts);
2866 lop->args[1] = temp_arg(arg_ts->mem_base);
2867 lop->args[2] = arg_ts->mem_offset;
5a18407f
RH
2868
2869 /* Loaded, but synced with memory. */
b83eabea 2870 arg_ts->state = TS_MEM;
5a18407f
RH
2871 }
2872 }
2873 }
2874
2875 /* Perform input replacement, and mark inputs that became dead.
2876 No action is required except keeping temp_state up to date
2877 so that we reload when needed. */
2878 for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
b83eabea
RH
2879 arg_ts = arg_temp(op->args[i]);
2880 if (arg_ts) {
2881 dir_ts = arg_ts->state_ptr;
2882 if (dir_ts) {
2883 op->args[i] = temp_arg(dir_ts);
5a18407f
RH
2884 changes = true;
2885 if (IS_DEAD_ARG(i)) {
b83eabea 2886 arg_ts->state = TS_DEAD;
5a18407f
RH
2887 }
2888 }
2889 }
2890 }
2891
2892 /* Liveness analysis should ensure that the following are
2893 all correct, for call sites and basic block end points. */
2894 if (call_flags & TCG_CALL_NO_READ_GLOBALS) {
2895 /* Nothing to do */
2896 } else if (call_flags & TCG_CALL_NO_WRITE_GLOBALS) {
2897 for (i = 0; i < nb_globals; ++i) {
2898 /* Liveness should see that globals are synced back,
2899 that is, either TS_DEAD or TS_MEM. */
b83eabea
RH
2900 arg_ts = &s->temps[i];
2901 tcg_debug_assert(arg_ts->state_ptr == 0
2902 || arg_ts->state != 0);
5a18407f
RH
2903 }
2904 } else {
2905 for (i = 0; i < nb_globals; ++i) {
2906 /* Liveness should see that globals are saved back,
2907 that is, TS_DEAD, waiting to be reloaded. */
b83eabea
RH
2908 arg_ts = &s->temps[i];
2909 tcg_debug_assert(arg_ts->state_ptr == 0
2910 || arg_ts->state == TS_DEAD);
5a18407f
RH
2911 }
2912 }
2913
2914 /* Outputs become available. */
2915 for (i = 0; i < nb_oargs; i++) {
b83eabea
RH
2916 arg_ts = arg_temp(op->args[i]);
2917 dir_ts = arg_ts->state_ptr;
2918 if (!dir_ts) {
5a18407f
RH
2919 continue;
2920 }
b83eabea 2921 op->args[i] = temp_arg(dir_ts);
5a18407f
RH
2922 changes = true;
2923
2924 /* The output is now live and modified. */
b83eabea 2925 arg_ts->state = 0;
5a18407f
RH
2926
2927 /* Sync outputs upon their last write. */
2928 if (NEED_SYNC_ARG(i)) {
b83eabea 2929 TCGOpcode sopc = (arg_ts->type == TCG_TYPE_I32
5a18407f
RH
2930 ? INDEX_op_st_i32
2931 : INDEX_op_st_i64);
ac1043f6 2932 TCGOp *sop = tcg_op_insert_after(s, op, sopc);
5a18407f 2933
b83eabea
RH
2934 sop->args[0] = temp_arg(dir_ts);
2935 sop->args[1] = temp_arg(arg_ts->mem_base);
2936 sop->args[2] = arg_ts->mem_offset;
5a18407f 2937
b83eabea 2938 arg_ts->state = TS_MEM;
5a18407f
RH
2939 }
2940 /* Drop outputs that are dead. */
2941 if (IS_DEAD_ARG(i)) {
b83eabea 2942 arg_ts->state = TS_DEAD;
5a18407f
RH
2943 }
2944 }
2945 }
2946
2947 return changes;
2948}
2949
8d8fdbae 2950#ifdef CONFIG_DEBUG_TCG
c896fe29
FB
2951static void dump_regs(TCGContext *s)
2952{
2953 TCGTemp *ts;
2954 int i;
2955 char buf[64];
2956
2957 for(i = 0; i < s->nb_temps; i++) {
2958 ts = &s->temps[i];
43439139 2959 printf(" %10s: ", tcg_get_arg_str_ptr(s, buf, sizeof(buf), ts));
c896fe29
FB
2960 switch(ts->val_type) {
2961 case TEMP_VAL_REG:
2962 printf("%s", tcg_target_reg_names[ts->reg]);
2963 break;
2964 case TEMP_VAL_MEM:
b3a62939
RH
2965 printf("%d(%s)", (int)ts->mem_offset,
2966 tcg_target_reg_names[ts->mem_base->reg]);
c896fe29
FB
2967 break;
2968 case TEMP_VAL_CONST:
2969 printf("$0x%" TCG_PRIlx, ts->val);
2970 break;
2971 case TEMP_VAL_DEAD:
2972 printf("D");
2973 break;
2974 default:
2975 printf("???");
2976 break;
2977 }
2978 printf("\n");
2979 }
2980
2981 for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
f8b2f202 2982 if (s->reg_to_temp[i] != NULL) {
c896fe29
FB
2983 printf("%s: %s\n",
2984 tcg_target_reg_names[i],
f8b2f202 2985 tcg_get_arg_str_ptr(s, buf, sizeof(buf), s->reg_to_temp[i]));
c896fe29
FB
2986 }
2987 }
2988}
2989
2990static void check_regs(TCGContext *s)
2991{
869938ae 2992 int reg;
b6638662 2993 int k;
c896fe29
FB
2994 TCGTemp *ts;
2995 char buf[64];
2996
f8b2f202
RH
2997 for (reg = 0; reg < TCG_TARGET_NB_REGS; reg++) {
2998 ts = s->reg_to_temp[reg];
2999 if (ts != NULL) {
3000 if (ts->val_type != TEMP_VAL_REG || ts->reg != reg) {
c896fe29
FB
3001 printf("Inconsistency for register %s:\n",
3002 tcg_target_reg_names[reg]);
b03cce8e 3003 goto fail;
c896fe29
FB
3004 }
3005 }
3006 }
f8b2f202 3007 for (k = 0; k < s->nb_temps; k++) {
c896fe29 3008 ts = &s->temps[k];
f8b2f202
RH
3009 if (ts->val_type == TEMP_VAL_REG && !ts->fixed_reg
3010 && s->reg_to_temp[ts->reg] != ts) {
3011 printf("Inconsistency for temp %s:\n",
3012 tcg_get_arg_str_ptr(s, buf, sizeof(buf), ts));
b03cce8e 3013 fail:
f8b2f202
RH
3014 printf("reg state:\n");
3015 dump_regs(s);
3016 tcg_abort();
c896fe29
FB
3017 }
3018 }
3019}
3020#endif
3021
2272e4a7 3022static void temp_allocate_frame(TCGContext *s, TCGTemp *ts)
c896fe29 3023{
9b9c37c3
RH
3024#if !(defined(__sparc__) && TCG_TARGET_REG_BITS == 64)
3025 /* Sparc64 stack is accessed with offset of 2047 */
b591dc59
BS
3026 s->current_frame_offset = (s->current_frame_offset +
3027 (tcg_target_long)sizeof(tcg_target_long) - 1) &
3028 ~(sizeof(tcg_target_long) - 1);
f44c9960 3029#endif
b591dc59
BS
3030 if (s->current_frame_offset + (tcg_target_long)sizeof(tcg_target_long) >
3031 s->frame_end) {
5ff9d6a4 3032 tcg_abort();
b591dc59 3033 }
c896fe29 3034 ts->mem_offset = s->current_frame_offset;
b3a62939 3035 ts->mem_base = s->frame_temp;
c896fe29 3036 ts->mem_allocated = 1;
e2c6d1b4 3037 s->current_frame_offset += sizeof(tcg_target_long);
c896fe29
FB
3038}
3039
b722452a 3040static void temp_load(TCGContext *, TCGTemp *, TCGRegSet, TCGRegSet, TCGRegSet);
b3915dbb 3041
59d7c14e
RH
3042/* Mark a temporary as free or dead. If 'free_or_dead' is negative,
3043 mark it free; otherwise mark it dead. */
3044static void temp_free_or_dead(TCGContext *s, TCGTemp *ts, int free_or_dead)
7f6ceedf 3045{
59d7c14e
RH
3046 if (ts->fixed_reg) {
3047 return;
3048 }
3049 if (ts->val_type == TEMP_VAL_REG) {
3050 s->reg_to_temp[ts->reg] = NULL;
3051 }
3052 ts->val_type = (free_or_dead < 0
3053 || ts->temp_local
fa477d25 3054 || ts->temp_global
59d7c14e
RH
3055 ? TEMP_VAL_MEM : TEMP_VAL_DEAD);
3056}
7f6ceedf 3057
59d7c14e
RH
3058/* Mark a temporary as dead. */
3059static inline void temp_dead(TCGContext *s, TCGTemp *ts)
3060{
3061 temp_free_or_dead(s, ts, 1);
3062}
3063
3064/* Sync a temporary to memory. 'allocated_regs' is used in case a temporary
3065 registers needs to be allocated to store a constant. If 'free_or_dead'
3066 is non-zero, subsequently release the temporary; if it is positive, the
3067 temp is dead; if it is negative, the temp is free. */
98b4e186
RH
3068static void temp_sync(TCGContext *s, TCGTemp *ts, TCGRegSet allocated_regs,
3069 TCGRegSet preferred_regs, int free_or_dead)
59d7c14e
RH
3070{
3071 if (ts->fixed_reg) {
3072 return;
3073 }
3074 if (!ts->mem_coherent) {
7f6ceedf 3075 if (!ts->mem_allocated) {
2272e4a7 3076 temp_allocate_frame(s, ts);
59d7c14e 3077 }
59d7c14e
RH
3078 switch (ts->val_type) {
3079 case TEMP_VAL_CONST:
3080 /* If we're going to free the temp immediately, then we won't
3081 require it later in a register, so attempt to store the
3082 constant to memory directly. */
3083 if (free_or_dead
3084 && tcg_out_sti(s, ts->type, ts->val,
3085 ts->mem_base->reg, ts->mem_offset)) {
3086 break;
3087 }
3088 temp_load(s, ts, tcg_target_available_regs[ts->type],
98b4e186 3089 allocated_regs, preferred_regs);
59d7c14e
RH
3090 /* fallthrough */
3091
3092 case TEMP_VAL_REG:
3093 tcg_out_st(s, ts->type, ts->reg,
3094 ts->mem_base->reg, ts->mem_offset);
3095 break;
3096
3097 case TEMP_VAL_MEM:
3098 break;
3099
3100 case TEMP_VAL_DEAD:
3101 default:
3102 tcg_abort();
3103 }
3104 ts->mem_coherent = 1;
3105 }
3106 if (free_or_dead) {
3107 temp_free_or_dead(s, ts, free_or_dead);
7f6ceedf 3108 }
7f6ceedf
AJ
3109}
3110
c896fe29 3111/* free register 'reg' by spilling the corresponding temporary if necessary */
b3915dbb 3112static void tcg_reg_free(TCGContext *s, TCGReg reg, TCGRegSet allocated_regs)
c896fe29 3113{
f8b2f202 3114 TCGTemp *ts = s->reg_to_temp[reg];
f8b2f202 3115 if (ts != NULL) {
98b4e186 3116 temp_sync(s, ts, allocated_regs, 0, -1);
c896fe29
FB
3117 }
3118}
3119
b016486e
RH
3120/**
3121 * tcg_reg_alloc:
3122 * @required_regs: Set of registers in which we must allocate.
3123 * @allocated_regs: Set of registers which must be avoided.
3124 * @preferred_regs: Set of registers we should prefer.
3125 * @rev: True if we search the registers in "indirect" order.
3126 *
3127 * The allocated register must be in @required_regs & ~@allocated_regs,
3128 * but if we can put it in @preferred_regs we may save a move later.
3129 */
3130static TCGReg tcg_reg_alloc(TCGContext *s, TCGRegSet required_regs,
3131 TCGRegSet allocated_regs,
3132 TCGRegSet preferred_regs, bool rev)
c896fe29 3133{
b016486e
RH
3134 int i, j, f, n = ARRAY_SIZE(tcg_target_reg_alloc_order);
3135 TCGRegSet reg_ct[2];
91478cef 3136 const int *order;
c896fe29 3137
b016486e
RH
3138 reg_ct[1] = required_regs & ~allocated_regs;
3139 tcg_debug_assert(reg_ct[1] != 0);
3140 reg_ct[0] = reg_ct[1] & preferred_regs;
3141
3142 /* Skip the preferred_regs option if it cannot be satisfied,
3143 or if the preference made no difference. */
3144 f = reg_ct[0] == 0 || reg_ct[0] == reg_ct[1];
3145
91478cef 3146 order = rev ? indirect_reg_alloc_order : tcg_target_reg_alloc_order;
c896fe29 3147
b016486e
RH
3148 /* Try free registers, preferences first. */
3149 for (j = f; j < 2; j++) {
3150 TCGRegSet set = reg_ct[j];
3151
3152 if (tcg_regset_single(set)) {
3153 /* One register in the set. */
3154 TCGReg reg = tcg_regset_first(set);
3155 if (s->reg_to_temp[reg] == NULL) {
3156 return reg;
3157 }
3158 } else {
3159 for (i = 0; i < n; i++) {
3160 TCGReg reg = order[i];
3161 if (s->reg_to_temp[reg] == NULL &&
3162 tcg_regset_test_reg(set, reg)) {
3163 return reg;
3164 }
3165 }
3166 }
c896fe29
FB
3167 }
3168
b016486e
RH
3169 /* We must spill something. */
3170 for (j = f; j < 2; j++) {
3171 TCGRegSet set = reg_ct[j];
3172
3173 if (tcg_regset_single(set)) {
3174 /* One register in the set. */
3175 TCGReg reg = tcg_regset_first(set);
b3915dbb 3176 tcg_reg_free(s, reg, allocated_regs);
c896fe29 3177 return reg;
b016486e
RH
3178 } else {
3179 for (i = 0; i < n; i++) {
3180 TCGReg reg = order[i];
3181 if (tcg_regset_test_reg(set, reg)) {
3182 tcg_reg_free(s, reg, allocated_regs);
3183 return reg;
3184 }
3185 }
c896fe29
FB
3186 }
3187 }
3188
3189 tcg_abort();
3190}
3191
40ae5c62
RH
3192/* Make sure the temporary is in a register. If needed, allocate the register
3193 from DESIRED while avoiding ALLOCATED. */
3194static void temp_load(TCGContext *s, TCGTemp *ts, TCGRegSet desired_regs,
b722452a 3195 TCGRegSet allocated_regs, TCGRegSet preferred_regs)
40ae5c62
RH
3196{
3197 TCGReg reg;
3198
3199 switch (ts->val_type) {
3200 case TEMP_VAL_REG:
3201 return;
3202 case TEMP_VAL_CONST:
b016486e 3203 reg = tcg_reg_alloc(s, desired_regs, allocated_regs,
b722452a 3204 preferred_regs, ts->indirect_base);
40ae5c62
RH
3205 tcg_out_movi(s, ts->type, reg, ts->val);
3206 ts->mem_coherent = 0;
3207 break;
3208 case TEMP_VAL_MEM:
b016486e 3209 reg = tcg_reg_alloc(s, desired_regs, allocated_regs,
b722452a 3210 preferred_regs, ts->indirect_base);
40ae5c62
RH
3211 tcg_out_ld(s, ts->type, reg, ts->mem_base->reg, ts->mem_offset);
3212 ts->mem_coherent = 1;
3213 break;
3214 case TEMP_VAL_DEAD:
3215 default:
3216 tcg_abort();
3217 }
3218 ts->reg = reg;
3219 ts->val_type = TEMP_VAL_REG;
3220 s->reg_to_temp[reg] = ts;
3221}
3222
59d7c14e
RH
3223/* Save a temporary to memory. 'allocated_regs' is used in case a
3224 temporary registers needs to be allocated to store a constant. */
3225static void temp_save(TCGContext *s, TCGTemp *ts, TCGRegSet allocated_regs)
1ad80729 3226{
5a18407f
RH
3227 /* The liveness analysis already ensures that globals are back
3228 in memory. Keep an tcg_debug_assert for safety. */
3229 tcg_debug_assert(ts->val_type == TEMP_VAL_MEM || ts->fixed_reg);
1ad80729
AJ
3230}
3231
9814dd27 3232/* save globals to their canonical location and assume they can be
e8996ee0
FB
3233 modified be the following code. 'allocated_regs' is used in case a
3234 temporary registers needs to be allocated to store a constant. */
3235static void save_globals(TCGContext *s, TCGRegSet allocated_regs)
c896fe29 3236{
ac3b8891 3237 int i, n;
c896fe29 3238
ac3b8891 3239 for (i = 0, n = s->nb_globals; i < n; i++) {
b13eb728 3240 temp_save(s, &s->temps[i], allocated_regs);
c896fe29 3241 }
e5097dc8
FB
3242}
3243
3d5c5f87
AJ
3244/* sync globals to their canonical location and assume they can be
3245 read by the following code. 'allocated_regs' is used in case a
3246 temporary registers needs to be allocated to store a constant. */
3247static void sync_globals(TCGContext *s, TCGRegSet allocated_regs)
3248{
ac3b8891 3249 int i, n;
3d5c5f87 3250
ac3b8891 3251 for (i = 0, n = s->nb_globals; i < n; i++) {
12b9b11a 3252 TCGTemp *ts = &s->temps[i];
5a18407f
RH
3253 tcg_debug_assert(ts->val_type != TEMP_VAL_REG
3254 || ts->fixed_reg
3255 || ts->mem_coherent);
3d5c5f87
AJ
3256 }
3257}
3258
e5097dc8 3259/* at the end of a basic block, we assume all temporaries are dead and
e8996ee0
FB
3260 all globals are stored at their canonical location. */
3261static void tcg_reg_alloc_bb_end(TCGContext *s, TCGRegSet allocated_regs)
e5097dc8 3262{
e5097dc8
FB
3263 int i;
3264
b13eb728
RH
3265 for (i = s->nb_globals; i < s->nb_temps; i++) {
3266 TCGTemp *ts = &s->temps[i];
641d5fbe 3267 if (ts->temp_local) {
b13eb728 3268 temp_save(s, ts, allocated_regs);
641d5fbe 3269 } else {
5a18407f
RH
3270 /* The liveness analysis already ensures that temps are dead.
3271 Keep an tcg_debug_assert for safety. */
3272 tcg_debug_assert(ts->val_type == TEMP_VAL_DEAD);
c896fe29
FB
3273 }
3274 }
e8996ee0
FB
3275
3276 save_globals(s, allocated_regs);
c896fe29
FB
3277}
3278
0fe4fca4 3279static void tcg_reg_alloc_do_movi(TCGContext *s, TCGTemp *ots,
ba87719c
RH
3280 tcg_target_ulong val, TCGLifeData arg_life,
3281 TCGRegSet preferred_regs)
e8996ee0 3282{
e8996ee0 3283 if (ots->fixed_reg) {
59d7c14e 3284 /* For fixed registers, we do not do any constant propagation. */
e8996ee0 3285 tcg_out_movi(s, ots->type, ots->reg, val);
59d7c14e 3286 return;
e8996ee0 3287 }
59d7c14e
RH
3288
3289 /* The movi is not explicitly generated here. */
3290 if (ots->val_type == TEMP_VAL_REG) {
3291 s->reg_to_temp[ots->reg] = NULL;
ec7a869d 3292 }
59d7c14e
RH
3293 ots->val_type = TEMP_VAL_CONST;
3294 ots->val = val;
3295 ots->mem_coherent = 0;
3296 if (NEED_SYNC_ARG(0)) {
ba87719c 3297 temp_sync(s, ots, s->reserved_regs, preferred_regs, IS_DEAD_ARG(0));
59d7c14e 3298 } else if (IS_DEAD_ARG(0)) {
f8bf00f1 3299 temp_dead(s, ots);
4c4e1ab2 3300 }
e8996ee0
FB
3301}
3302
dd186292 3303static void tcg_reg_alloc_movi(TCGContext *s, const TCGOp *op)
0fe4fca4 3304{
43439139 3305 TCGTemp *ots = arg_temp(op->args[0]);
dd186292 3306 tcg_target_ulong val = op->args[1];
0fe4fca4 3307
69e3706d 3308 tcg_reg_alloc_do_movi(s, ots, val, op->life, op->output_pref[0]);
0fe4fca4
PB
3309}
3310
dd186292 3311static void tcg_reg_alloc_mov(TCGContext *s, const TCGOp *op)
c896fe29 3312{
dd186292 3313 const TCGLifeData arg_life = op->life;
69e3706d 3314 TCGRegSet allocated_regs, preferred_regs;
c896fe29 3315 TCGTemp *ts, *ots;
450445d5 3316 TCGType otype, itype;
c896fe29 3317
d21369f5 3318 allocated_regs = s->reserved_regs;
69e3706d 3319 preferred_regs = op->output_pref[0];
43439139
RH
3320 ots = arg_temp(op->args[0]);
3321 ts = arg_temp(op->args[1]);
450445d5
RH
3322
3323 /* Note that otype != itype for no-op truncation. */
3324 otype = ots->type;
3325 itype = ts->type;
c29c1d7e 3326
0fe4fca4
PB
3327 if (ts->val_type == TEMP_VAL_CONST) {
3328 /* propagate constant or generate sti */
3329 tcg_target_ulong val = ts->val;
3330 if (IS_DEAD_ARG(1)) {
3331 temp_dead(s, ts);
3332 }
69e3706d 3333 tcg_reg_alloc_do_movi(s, ots, val, arg_life, preferred_regs);
0fe4fca4
PB
3334 return;
3335 }
3336
3337 /* If the source value is in memory we're going to be forced
3338 to have it in a register in order to perform the copy. Copy
3339 the SOURCE value into its own register first, that way we
3340 don't have to reload SOURCE the next time it is used. */
3341 if (ts->val_type == TEMP_VAL_MEM) {
69e3706d
RH
3342 temp_load(s, ts, tcg_target_available_regs[itype],
3343 allocated_regs, preferred_regs);
c29c1d7e 3344 }
c896fe29 3345
0fe4fca4 3346 tcg_debug_assert(ts->val_type == TEMP_VAL_REG);
c29c1d7e
AJ
3347 if (IS_DEAD_ARG(0) && !ots->fixed_reg) {
3348 /* mov to a non-saved dead register makes no sense (even with
3349 liveness analysis disabled). */
eabb7b91 3350 tcg_debug_assert(NEED_SYNC_ARG(0));
c29c1d7e 3351 if (!ots->mem_allocated) {
2272e4a7 3352 temp_allocate_frame(s, ots);
c29c1d7e 3353 }
b3a62939 3354 tcg_out_st(s, otype, ts->reg, ots->mem_base->reg, ots->mem_offset);
c29c1d7e 3355 if (IS_DEAD_ARG(1)) {
f8bf00f1 3356 temp_dead(s, ts);
c29c1d7e 3357 }
f8bf00f1 3358 temp_dead(s, ots);
c29c1d7e 3359 } else {
866cb6cb 3360 if (IS_DEAD_ARG(1) && !ts->fixed_reg && !ots->fixed_reg) {
c896fe29 3361 /* the mov can be suppressed */
c29c1d7e 3362 if (ots->val_type == TEMP_VAL_REG) {
f8b2f202 3363 s->reg_to_temp[ots->reg] = NULL;
c29c1d7e
AJ
3364 }
3365 ots->reg = ts->reg;
f8bf00f1 3366 temp_dead(s, ts);
c896fe29 3367 } else {
c29c1d7e
AJ
3368 if (ots->val_type != TEMP_VAL_REG) {
3369 /* When allocating a new register, make sure to not spill the
3370 input one. */
3371 tcg_regset_set_reg(allocated_regs, ts->reg);
450445d5 3372 ots->reg = tcg_reg_alloc(s, tcg_target_available_regs[otype],
69e3706d 3373 allocated_regs, preferred_regs,
b016486e 3374 ots->indirect_base);
c896fe29 3375 }
450445d5 3376 tcg_out_mov(s, otype, ots->reg, ts->reg);
c896fe29 3377 }
c29c1d7e
AJ
3378 ots->val_type = TEMP_VAL_REG;
3379 ots->mem_coherent = 0;
f8b2f202 3380 s->reg_to_temp[ots->reg] = ots;
c29c1d7e 3381 if (NEED_SYNC_ARG(0)) {
98b4e186 3382 temp_sync(s, ots, allocated_regs, 0, 0);
c896fe29 3383 }
ec7a869d 3384 }
c896fe29
FB
3385}
3386
dd186292 3387static void tcg_reg_alloc_op(TCGContext *s, const TCGOp *op)
c896fe29 3388{
dd186292
RH
3389 const TCGLifeData arg_life = op->life;
3390 const TCGOpDef * const def = &tcg_op_defs[op->opc];
82790a87
RH
3391 TCGRegSet i_allocated_regs;
3392 TCGRegSet o_allocated_regs;
b6638662
RH
3393 int i, k, nb_iargs, nb_oargs;
3394 TCGReg reg;
c896fe29
FB
3395 TCGArg arg;
3396 const TCGArgConstraint *arg_ct;
3397 TCGTemp *ts;
3398 TCGArg new_args[TCG_MAX_OP_ARGS];
3399 int const_args[TCG_MAX_OP_ARGS];
3400
3401 nb_oargs = def->nb_oargs;
3402 nb_iargs = def->nb_iargs;
3403
3404 /* copy constants */
3405 memcpy(new_args + nb_oargs + nb_iargs,
dd186292 3406 op->args + nb_oargs + nb_iargs,
c896fe29
FB
3407 sizeof(TCGArg) * def->nb_cargs);
3408
d21369f5
RH
3409 i_allocated_regs = s->reserved_regs;
3410 o_allocated_regs = s->reserved_regs;
82790a87 3411
c896fe29 3412 /* satisfy input constraints */
dd186292 3413 for (k = 0; k < nb_iargs; k++) {
d62816f2
RH
3414 TCGRegSet i_preferred_regs, o_preferred_regs;
3415
c896fe29 3416 i = def->sorted_args[nb_oargs + k];
dd186292 3417 arg = op->args[i];
c896fe29 3418 arg_ct = &def->args_ct[i];
43439139 3419 ts = arg_temp(arg);
40ae5c62
RH
3420
3421 if (ts->val_type == TEMP_VAL_CONST
3422 && tcg_target_const_match(ts->val, ts->type, arg_ct)) {
3423 /* constant is OK for instruction */
3424 const_args[i] = 1;
3425 new_args[i] = ts->val;
d62816f2 3426 continue;
c896fe29 3427 }
40ae5c62 3428
d62816f2 3429 i_preferred_regs = o_preferred_regs = 0;
5ff9d6a4 3430 if (arg_ct->ct & TCG_CT_IALIAS) {
d62816f2 3431 o_preferred_regs = op->output_pref[arg_ct->alias_index];
5ff9d6a4
FB
3432 if (ts->fixed_reg) {
3433 /* if fixed register, we must allocate a new register
3434 if the alias is not the same register */
d62816f2 3435 if (arg != op->args[arg_ct->alias_index]) {
5ff9d6a4 3436 goto allocate_in_reg;
d62816f2 3437 }
5ff9d6a4
FB
3438 } else {
3439 /* if the input is aliased to an output and if it is
3440 not dead after the instruction, we must allocate
3441 a new register and move it */
866cb6cb 3442 if (!IS_DEAD_ARG(i)) {
5ff9d6a4 3443 goto allocate_in_reg;
866cb6cb 3444 }
d62816f2 3445
7e1df267
AJ
3446 /* check if the current register has already been allocated
3447 for another input aliased to an output */
d62816f2
RH
3448 if (ts->val_type == TEMP_VAL_REG) {
3449 int k2, i2;
3450 reg = ts->reg;
3451 for (k2 = 0 ; k2 < k ; k2++) {
3452 i2 = def->sorted_args[nb_oargs + k2];
3453 if ((def->args_ct[i2].ct & TCG_CT_IALIAS) &&
3454 reg == new_args[i2]) {
3455 goto allocate_in_reg;
3456 }
7e1df267
AJ
3457 }
3458 }
d62816f2 3459 i_preferred_regs = o_preferred_regs;
5ff9d6a4 3460 }
c896fe29 3461 }
d62816f2
RH
3462
3463 temp_load(s, ts, arg_ct->u.regs, i_allocated_regs, i_preferred_regs);
c896fe29 3464 reg = ts->reg;
d62816f2 3465
c896fe29
FB
3466 if (tcg_regset_test_reg(arg_ct->u.regs, reg)) {
3467 /* nothing to do : the constraint is satisfied */
3468 } else {
3469 allocate_in_reg:
3470 /* allocate a new register matching the constraint
3471 and move the temporary register into it */
d62816f2
RH
3472 temp_load(s, ts, tcg_target_available_regs[ts->type],
3473 i_allocated_regs, 0);
82790a87 3474 reg = tcg_reg_alloc(s, arg_ct->u.regs, i_allocated_regs,
d62816f2 3475 o_preferred_regs, ts->indirect_base);
3b6dac34 3476 tcg_out_mov(s, ts->type, reg, ts->reg);
c896fe29 3477 }
c896fe29
FB
3478 new_args[i] = reg;
3479 const_args[i] = 0;
82790a87 3480 tcg_regset_set_reg(i_allocated_regs, reg);
c896fe29
FB
3481 }
3482
a52ad07e
AJ
3483 /* mark dead temporaries and free the associated registers */
3484 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
3485 if (IS_DEAD_ARG(i)) {
43439139 3486 temp_dead(s, arg_temp(op->args[i]));
a52ad07e
AJ
3487 }
3488 }
3489
e8996ee0 3490 if (def->flags & TCG_OPF_BB_END) {
82790a87 3491 tcg_reg_alloc_bb_end(s, i_allocated_regs);
e8996ee0 3492 } else {
e8996ee0
FB
3493 if (def->flags & TCG_OPF_CALL_CLOBBER) {
3494 /* XXX: permit generic clobber register list ? */
c8074023
RH
3495 for (i = 0; i < TCG_TARGET_NB_REGS; i++) {
3496 if (tcg_regset_test_reg(tcg_target_call_clobber_regs, i)) {
82790a87 3497 tcg_reg_free(s, i, i_allocated_regs);
e8996ee0 3498 }
c896fe29 3499 }
3d5c5f87
AJ
3500 }
3501 if (def->flags & TCG_OPF_SIDE_EFFECTS) {
3502 /* sync globals if the op has side effects and might trigger
3503 an exception. */
82790a87 3504 sync_globals(s, i_allocated_regs);
c896fe29 3505 }
e8996ee0
FB
3506
3507 /* satisfy the output constraints */
e8996ee0
FB
3508 for(k = 0; k < nb_oargs; k++) {
3509 i = def->sorted_args[k];
dd186292 3510 arg = op->args[i];
e8996ee0 3511 arg_ct = &def->args_ct[i];
43439139 3512 ts = arg_temp(arg);
17280ff4
RH
3513 if ((arg_ct->ct & TCG_CT_ALIAS)
3514 && !const_args[arg_ct->alias_index]) {
e8996ee0 3515 reg = new_args[arg_ct->alias_index];
82790a87
RH
3516 } else if (arg_ct->ct & TCG_CT_NEWREG) {
3517 reg = tcg_reg_alloc(s, arg_ct->u.regs,
3518 i_allocated_regs | o_allocated_regs,
69e3706d 3519 op->output_pref[k], ts->indirect_base);
e8996ee0
FB
3520 } else {
3521 /* if fixed register, we try to use it */
3522 reg = ts->reg;
3523 if (ts->fixed_reg &&
3524 tcg_regset_test_reg(arg_ct->u.regs, reg)) {
3525 goto oarg_end;
3526 }
82790a87 3527 reg = tcg_reg_alloc(s, arg_ct->u.regs, o_allocated_regs,
69e3706d 3528 op->output_pref[k], ts->indirect_base);
c896fe29 3529 }
82790a87 3530 tcg_regset_set_reg(o_allocated_regs, reg);
e8996ee0
FB
3531 /* if a fixed register is used, then a move will be done afterwards */
3532 if (!ts->fixed_reg) {
ec7a869d 3533 if (ts->val_type == TEMP_VAL_REG) {
f8b2f202 3534 s->reg_to_temp[ts->reg] = NULL;
ec7a869d
AJ
3535 }
3536 ts->val_type = TEMP_VAL_REG;
3537 ts->reg = reg;
3538 /* temp value is modified, so the value kept in memory is
3539 potentially not the same */
3540 ts->mem_coherent = 0;
f8b2f202 3541 s->reg_to_temp[reg] = ts;
e8996ee0
FB
3542 }
3543 oarg_end:
3544 new_args[i] = reg;
c896fe29 3545 }
c896fe29
FB
3546 }
3547
c896fe29 3548 /* emit instruction */
d2fd745f
RH
3549 if (def->flags & TCG_OPF_VECTOR) {
3550 tcg_out_vec_op(s, op->opc, TCGOP_VECL(op), TCGOP_VECE(op),
3551 new_args, const_args);
3552 } else {
3553 tcg_out_op(s, op->opc, new_args, const_args);
3554 }
3555
c896fe29
FB
3556 /* move the outputs in the correct register if needed */
3557 for(i = 0; i < nb_oargs; i++) {
43439139 3558 ts = arg_temp(op->args[i]);
c896fe29
FB
3559 reg = new_args[i];
3560 if (ts->fixed_reg && ts->reg != reg) {
3b6dac34 3561 tcg_out_mov(s, ts->type, ts->reg, reg);
c896fe29 3562 }
ec7a869d 3563 if (NEED_SYNC_ARG(i)) {
98b4e186 3564 temp_sync(s, ts, o_allocated_regs, 0, IS_DEAD_ARG(i));
59d7c14e 3565 } else if (IS_DEAD_ARG(i)) {
f8bf00f1 3566 temp_dead(s, ts);
ec7a869d 3567 }
c896fe29
FB
3568 }
3569}
3570
b03cce8e
FB
3571#ifdef TCG_TARGET_STACK_GROWSUP
3572#define STACK_DIR(x) (-(x))
3573#else
3574#define STACK_DIR(x) (x)
3575#endif
3576
dd186292 3577static void tcg_reg_alloc_call(TCGContext *s, TCGOp *op)
c896fe29 3578{
cd9090aa
RH
3579 const int nb_oargs = TCGOP_CALLO(op);
3580 const int nb_iargs = TCGOP_CALLI(op);
dd186292 3581 const TCGLifeData arg_life = op->life;
b6638662
RH
3582 int flags, nb_regs, i;
3583 TCGReg reg;
cf066674 3584 TCGArg arg;
c896fe29 3585 TCGTemp *ts;
d3452f1f
RH
3586 intptr_t stack_offset;
3587 size_t call_stack_size;
cf066674
RH
3588 tcg_insn_unit *func_addr;
3589 int allocate_args;
c896fe29 3590 TCGRegSet allocated_regs;
c896fe29 3591
dd186292
RH
3592 func_addr = (tcg_insn_unit *)(intptr_t)op->args[nb_oargs + nb_iargs];
3593 flags = op->args[nb_oargs + nb_iargs + 1];
c896fe29 3594
6e17d0c5 3595 nb_regs = ARRAY_SIZE(tcg_target_call_iarg_regs);
c45cb8bb
RH
3596 if (nb_regs > nb_iargs) {
3597 nb_regs = nb_iargs;
cf066674 3598 }
c896fe29
FB
3599
3600 /* assign stack slots first */
c45cb8bb 3601 call_stack_size = (nb_iargs - nb_regs) * sizeof(tcg_target_long);
c896fe29
FB
3602 call_stack_size = (call_stack_size + TCG_TARGET_STACK_ALIGN - 1) &
3603 ~(TCG_TARGET_STACK_ALIGN - 1);
b03cce8e
FB
3604 allocate_args = (call_stack_size > TCG_STATIC_CALL_ARGS_SIZE);
3605 if (allocate_args) {
345649c0
BS
3606 /* XXX: if more than TCG_STATIC_CALL_ARGS_SIZE is needed,
3607 preallocate call stack */
3608 tcg_abort();
b03cce8e 3609 }
39cf05d3
FB
3610
3611 stack_offset = TCG_TARGET_CALL_STACK_OFFSET;
dd186292
RH
3612 for (i = nb_regs; i < nb_iargs; i++) {
3613 arg = op->args[nb_oargs + i];
39cf05d3
FB
3614#ifdef TCG_TARGET_STACK_GROWSUP
3615 stack_offset -= sizeof(tcg_target_long);
3616#endif
3617 if (arg != TCG_CALL_DUMMY_ARG) {
43439139 3618 ts = arg_temp(arg);
40ae5c62 3619 temp_load(s, ts, tcg_target_available_regs[ts->type],
b722452a 3620 s->reserved_regs, 0);
40ae5c62 3621 tcg_out_st(s, ts->type, ts->reg, TCG_REG_CALL_STACK, stack_offset);
c896fe29 3622 }
39cf05d3
FB
3623#ifndef TCG_TARGET_STACK_GROWSUP
3624 stack_offset += sizeof(tcg_target_long);
3625#endif
c896fe29
FB
3626 }
3627
3628 /* assign input registers */
d21369f5 3629 allocated_regs = s->reserved_regs;
dd186292
RH
3630 for (i = 0; i < nb_regs; i++) {
3631 arg = op->args[nb_oargs + i];
39cf05d3 3632 if (arg != TCG_CALL_DUMMY_ARG) {
43439139 3633 ts = arg_temp(arg);
39cf05d3 3634 reg = tcg_target_call_iarg_regs[i];
40ae5c62 3635
39cf05d3
FB
3636 if (ts->val_type == TEMP_VAL_REG) {
3637 if (ts->reg != reg) {
4250da10 3638 tcg_reg_free(s, reg, allocated_regs);
3b6dac34 3639 tcg_out_mov(s, ts->type, reg, ts->reg);
39cf05d3 3640 }
39cf05d3 3641 } else {
ccb1bb66 3642 TCGRegSet arg_set = 0;
40ae5c62 3643
4250da10 3644 tcg_reg_free(s, reg, allocated_regs);
40ae5c62 3645 tcg_regset_set_reg(arg_set, reg);
b722452a 3646 temp_load(s, ts, arg_set, allocated_regs, 0);
c896fe29 3647 }
40ae5c62 3648
39cf05d3 3649 tcg_regset_set_reg(allocated_regs, reg);
c896fe29 3650 }
c896fe29
FB
3651 }
3652
c896fe29 3653 /* mark dead temporaries and free the associated registers */
dd186292 3654 for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
866cb6cb 3655 if (IS_DEAD_ARG(i)) {
43439139 3656 temp_dead(s, arg_temp(op->args[i]));
c896fe29
FB
3657 }
3658 }
3659
3660 /* clobber call registers */
c8074023
RH
3661 for (i = 0; i < TCG_TARGET_NB_REGS; i++) {
3662 if (tcg_regset_test_reg(tcg_target_call_clobber_regs, i)) {
b3915dbb 3663 tcg_reg_free(s, i, allocated_regs);
c896fe29
FB
3664 }
3665 }
78505279
AJ
3666
3667 /* Save globals if they might be written by the helper, sync them if
3668 they might be read. */
3669 if (flags & TCG_CALL_NO_READ_GLOBALS) {
3670 /* Nothing to do */
3671 } else if (flags & TCG_CALL_NO_WRITE_GLOBALS) {
3672 sync_globals(s, allocated_regs);
3673 } else {
b9c18f56
AJ
3674 save_globals(s, allocated_regs);
3675 }
c896fe29 3676
cf066674 3677 tcg_out_call(s, func_addr);
c896fe29
FB
3678
3679 /* assign output registers and emit moves if needed */
3680 for(i = 0; i < nb_oargs; i++) {
dd186292 3681 arg = op->args[i];
43439139 3682 ts = arg_temp(arg);
c896fe29 3683 reg = tcg_target_call_oarg_regs[i];
eabb7b91 3684 tcg_debug_assert(s->reg_to_temp[reg] == NULL);
34b1a49c 3685
c896fe29
FB
3686 if (ts->fixed_reg) {
3687 if (ts->reg != reg) {
3b6dac34 3688 tcg_out_mov(s, ts->type, ts->reg, reg);
c896fe29
FB
3689 }
3690 } else {
ec7a869d 3691 if (ts->val_type == TEMP_VAL_REG) {
f8b2f202 3692 s->reg_to_temp[ts->reg] = NULL;
ec7a869d
AJ
3693 }
3694 ts->val_type = TEMP_VAL_REG;
3695 ts->reg = reg;
3696 ts->mem_coherent = 0;
f8b2f202 3697 s->reg_to_temp[reg] = ts;
ec7a869d 3698 if (NEED_SYNC_ARG(i)) {
98b4e186 3699 temp_sync(s, ts, allocated_regs, 0, IS_DEAD_ARG(i));
59d7c14e 3700 } else if (IS_DEAD_ARG(i)) {
f8bf00f1 3701 temp_dead(s, ts);
8c11ad25 3702 }
c896fe29
FB
3703 }
3704 }
c896fe29
FB
3705}
3706
3707#ifdef CONFIG_PROFILER
3708
c3fac113
EC
3709/* avoid copy/paste errors */
3710#define PROF_ADD(to, from, field) \
3711 do { \
3712 (to)->field += atomic_read(&((from)->field)); \
3713 } while (0)
3714
3715#define PROF_MAX(to, from, field) \
3716 do { \
3717 typeof((from)->field) val__ = atomic_read(&((from)->field)); \
3718 if (val__ > (to)->field) { \
3719 (to)->field = val__; \
3720 } \
3721 } while (0)
3722
3723/* Pass in a zero'ed @prof */
3724static inline
3725void tcg_profile_snapshot(TCGProfile *prof, bool counters, bool table)
3726{
3468b59e 3727 unsigned int n_ctxs = atomic_read(&n_tcg_ctxs);
c3fac113
EC
3728 unsigned int i;
3729
3468b59e
EC
3730 for (i = 0; i < n_ctxs; i++) {
3731 TCGContext *s = atomic_read(&tcg_ctxs[i]);
3732 const TCGProfile *orig = &s->prof;
c3fac113
EC
3733
3734 if (counters) {
72fd2efb 3735 PROF_ADD(prof, orig, cpu_exec_time);
c3fac113
EC
3736 PROF_ADD(prof, orig, tb_count1);
3737 PROF_ADD(prof, orig, tb_count);
3738 PROF_ADD(prof, orig, op_count);
3739 PROF_MAX(prof, orig, op_count_max);
3740 PROF_ADD(prof, orig, temp_count);
3741 PROF_MAX(prof, orig, temp_count_max);
3742 PROF_ADD(prof, orig, del_op_count);
3743 PROF_ADD(prof, orig, code_in_len);
3744 PROF_ADD(prof, orig, code_out_len);
3745 PROF_ADD(prof, orig, search_out_len);
3746 PROF_ADD(prof, orig, interm_time);
3747 PROF_ADD(prof, orig, code_time);
3748 PROF_ADD(prof, orig, la_time);
3749 PROF_ADD(prof, orig, opt_time);
3750 PROF_ADD(prof, orig, restore_count);
3751 PROF_ADD(prof, orig, restore_time);
3752 }
3753 if (table) {
3754 int i;
3755
3756 for (i = 0; i < NB_OPS; i++) {
3757 PROF_ADD(prof, orig, table_op_count[i]);
3758 }
3759 }
3760 }
3761}
3762
3763#undef PROF_ADD
3764#undef PROF_MAX
3765
3766static void tcg_profile_snapshot_counters(TCGProfile *prof)
3767{
3768 tcg_profile_snapshot(prof, true, false);
3769}
3770
3771static void tcg_profile_snapshot_table(TCGProfile *prof)
3772{
3773 tcg_profile_snapshot(prof, false, true);
3774}
c896fe29 3775
d4c51a0a 3776void tcg_dump_op_count(void)
c896fe29 3777{
c3fac113 3778 TCGProfile prof = {};
c896fe29 3779 int i;
d70724ce 3780
c3fac113 3781 tcg_profile_snapshot_table(&prof);
15fc7daa 3782 for (i = 0; i < NB_OPS; i++) {
d4c51a0a 3783 qemu_printf("%s %" PRId64 "\n", tcg_op_defs[i].name,
c3fac113 3784 prof.table_op_count[i]);
c896fe29 3785 }
c896fe29 3786}
72fd2efb
EC
3787
3788int64_t tcg_cpu_exec_time(void)
3789{
3790 unsigned int n_ctxs = atomic_read(&n_tcg_ctxs);
3791 unsigned int i;
3792 int64_t ret = 0;
3793
3794 for (i = 0; i < n_ctxs; i++) {
3795 const TCGContext *s = atomic_read(&tcg_ctxs[i]);
3796 const TCGProfile *prof = &s->prof;
3797
3798 ret += atomic_read(&prof->cpu_exec_time);
3799 }
3800 return ret;
3801}
246ae24d 3802#else
d4c51a0a 3803void tcg_dump_op_count(void)
246ae24d 3804{
d4c51a0a 3805 qemu_printf("[TCG profiler not compiled]\n");
246ae24d 3806}
72fd2efb
EC
3807
3808int64_t tcg_cpu_exec_time(void)
3809{
3810 error_report("%s: TCG profiler not compiled", __func__);
3811 exit(EXIT_FAILURE);
3812}
c896fe29
FB
3813#endif
3814
3815
5bd2ec3d 3816int tcg_gen_code(TCGContext *s, TranslationBlock *tb)
c896fe29 3817{
c3fac113
EC
3818#ifdef CONFIG_PROFILER
3819 TCGProfile *prof = &s->prof;
3820#endif
15fa08f8
RH
3821 int i, num_insns;
3822 TCGOp *op;
c896fe29 3823
04fe6400
RH
3824#ifdef CONFIG_PROFILER
3825 {
c1f543b7 3826 int n = 0;
04fe6400 3827
15fa08f8
RH
3828 QTAILQ_FOREACH(op, &s->ops, link) {
3829 n++;
3830 }
c3fac113
EC
3831 atomic_set(&prof->op_count, prof->op_count + n);
3832 if (n > prof->op_count_max) {
3833 atomic_set(&prof->op_count_max, n);
04fe6400
RH
3834 }
3835
3836 n = s->nb_temps;
c3fac113
EC
3837 atomic_set(&prof->temp_count, prof->temp_count + n);
3838 if (n > prof->temp_count_max) {
3839 atomic_set(&prof->temp_count_max, n);
04fe6400
RH
3840 }
3841 }
3842#endif
3843
c896fe29 3844#ifdef DEBUG_DISAS
d977e1c2
AB
3845 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP)
3846 && qemu_log_in_addr_range(tb->pc))) {
1ee73216 3847 qemu_log_lock();
93fcfe39 3848 qemu_log("OP:\n");
1894f69a 3849 tcg_dump_ops(s, false);
93fcfe39 3850 qemu_log("\n");
1ee73216 3851 qemu_log_unlock();
c896fe29
FB
3852 }
3853#endif
3854
bef16ab4
RH
3855#ifdef CONFIG_DEBUG_TCG
3856 /* Ensure all labels referenced have been emitted. */
3857 {
3858 TCGLabel *l;
3859 bool error = false;
3860
3861 QSIMPLEQ_FOREACH(l, &s->labels, next) {
3862 if (unlikely(!l->present) && l->refs) {
3863 qemu_log_mask(CPU_LOG_TB_OP,
3864 "$L%d referenced but not present.\n", l->id);
3865 error = true;
3866 }
3867 }
3868 assert(!error);
3869 }
3870#endif
3871
c5cc28ff 3872#ifdef CONFIG_PROFILER
c3fac113 3873 atomic_set(&prof->opt_time, prof->opt_time - profile_getclock());
c5cc28ff
AJ
3874#endif
3875
8f2e8c07 3876#ifdef USE_TCG_OPTIMIZATIONS
c45cb8bb 3877 tcg_optimize(s);
8f2e8c07
KB
3878#endif
3879
a23a9ec6 3880#ifdef CONFIG_PROFILER
c3fac113
EC
3881 atomic_set(&prof->opt_time, prof->opt_time + profile_getclock());
3882 atomic_set(&prof->la_time, prof->la_time - profile_getclock());
a23a9ec6 3883#endif
c5cc28ff 3884
b4fc67c7 3885 reachable_code_pass(s);
b83eabea 3886 liveness_pass_1(s);
5a18407f 3887
b83eabea 3888 if (s->nb_indirects > 0) {
5a18407f 3889#ifdef DEBUG_DISAS
b83eabea
RH
3890 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_IND)
3891 && qemu_log_in_addr_range(tb->pc))) {
3892 qemu_log_lock();
3893 qemu_log("OP before indirect lowering:\n");
1894f69a 3894 tcg_dump_ops(s, false);
b83eabea
RH
3895 qemu_log("\n");
3896 qemu_log_unlock();
3897 }
5a18407f 3898#endif
b83eabea
RH
3899 /* Replace indirect temps with direct temps. */
3900 if (liveness_pass_2(s)) {
3901 /* If changes were made, re-run liveness. */
3902 liveness_pass_1(s);
5a18407f
RH
3903 }
3904 }
c5cc28ff 3905
a23a9ec6 3906#ifdef CONFIG_PROFILER
c3fac113 3907 atomic_set(&prof->la_time, prof->la_time + profile_getclock());
a23a9ec6 3908#endif
c896fe29
FB
3909
3910#ifdef DEBUG_DISAS
d977e1c2
AB
3911 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_OPT)
3912 && qemu_log_in_addr_range(tb->pc))) {
1ee73216 3913 qemu_log_lock();
c5cc28ff 3914 qemu_log("OP after optimization and liveness analysis:\n");
1894f69a 3915 tcg_dump_ops(s, true);
93fcfe39 3916 qemu_log("\n");
1ee73216 3917 qemu_log_unlock();
c896fe29
FB
3918 }
3919#endif
3920
3921 tcg_reg_alloc_start(s);
3922
e7e168f4
EC
3923 s->code_buf = tb->tc.ptr;
3924 s->code_ptr = tb->tc.ptr;
c896fe29 3925
659ef5cb 3926#ifdef TCG_TARGET_NEED_LDST_LABELS
6001f772 3927 QSIMPLEQ_INIT(&s->ldst_labels);
659ef5cb 3928#endif
57a26946
RH
3929#ifdef TCG_TARGET_NEED_POOL_LABELS
3930 s->pool_labels = NULL;
3931#endif
9ecefc84 3932
fca8a500 3933 num_insns = -1;
15fa08f8 3934 QTAILQ_FOREACH(op, &s->ops, link) {
c45cb8bb 3935 TCGOpcode opc = op->opc;
b3db8758 3936
c896fe29 3937#ifdef CONFIG_PROFILER
c3fac113 3938 atomic_set(&prof->table_op_count[opc], prof->table_op_count[opc] + 1);
c896fe29 3939#endif
c45cb8bb
RH
3940
3941 switch (opc) {
c896fe29 3942 case INDEX_op_mov_i32:
c896fe29 3943 case INDEX_op_mov_i64:
d2fd745f 3944 case INDEX_op_mov_vec:
dd186292 3945 tcg_reg_alloc_mov(s, op);
c896fe29 3946 break;
e8996ee0 3947 case INDEX_op_movi_i32:
e8996ee0 3948 case INDEX_op_movi_i64:
d2fd745f 3949 case INDEX_op_dupi_vec:
dd186292 3950 tcg_reg_alloc_movi(s, op);
e8996ee0 3951 break;
765b842a 3952 case INDEX_op_insn_start:
fca8a500 3953 if (num_insns >= 0) {
9f754620
RH
3954 size_t off = tcg_current_code_size(s);
3955 s->gen_insn_end_off[num_insns] = off;
3956 /* Assert that we do not overflow our stored offset. */
3957 assert(s->gen_insn_end_off[num_insns] == off);
fca8a500
RH
3958 }
3959 num_insns++;
bad729e2
RH
3960 for (i = 0; i < TARGET_INSN_START_WORDS; ++i) {
3961 target_ulong a;
3962#if TARGET_LONG_BITS > TCG_TARGET_REG_BITS
efee3746 3963 a = deposit64(op->args[i * 2], 32, 32, op->args[i * 2 + 1]);
bad729e2 3964#else
efee3746 3965 a = op->args[i];
bad729e2 3966#endif
fca8a500 3967 s->gen_insn_data[num_insns][i] = a;
bad729e2 3968 }
c896fe29 3969 break;
5ff9d6a4 3970 case INDEX_op_discard:
43439139 3971 temp_dead(s, arg_temp(op->args[0]));
5ff9d6a4 3972 break;
c896fe29 3973 case INDEX_op_set_label:
e8996ee0 3974 tcg_reg_alloc_bb_end(s, s->reserved_regs);
efee3746 3975 tcg_out_label(s, arg_label(op->args[0]), s->code_ptr);
c896fe29
FB
3976 break;
3977 case INDEX_op_call:
dd186292 3978 tcg_reg_alloc_call(s, op);
c45cb8bb 3979 break;
c896fe29 3980 default:
25c4d9cc 3981 /* Sanity check that we've not introduced any unhandled opcodes. */
be0f34b5 3982 tcg_debug_assert(tcg_op_supported(opc));
c896fe29
FB
3983 /* Note: in order to speed up the code, it would be much
3984 faster to have specialized register allocator functions for
3985 some common argument patterns */
dd186292 3986 tcg_reg_alloc_op(s, op);
c896fe29
FB
3987 break;
3988 }
8d8fdbae 3989#ifdef CONFIG_DEBUG_TCG
c896fe29
FB
3990 check_regs(s);
3991#endif
b125f9dc
RH
3992 /* Test for (pending) buffer overflow. The assumption is that any
3993 one operation beginning below the high water mark cannot overrun
3994 the buffer completely. Thus we can test for overflow after
3995 generating code without having to check during generation. */
644da9b3 3996 if (unlikely((void *)s->code_ptr > s->code_gen_highwater)) {
b125f9dc
RH
3997 return -1;
3998 }
c896fe29 3999 }
fca8a500
RH
4000 tcg_debug_assert(num_insns >= 0);
4001 s->gen_insn_end_off[num_insns] = tcg_current_code_size(s);
c45cb8bb 4002
b76f0d8c 4003 /* Generate TB finalization at the end of block */
659ef5cb
RH
4004#ifdef TCG_TARGET_NEED_LDST_LABELS
4005 if (!tcg_out_ldst_finalize(s)) {
23dceda6
RH
4006 return -1;
4007 }
659ef5cb 4008#endif
57a26946
RH
4009#ifdef TCG_TARGET_NEED_POOL_LABELS
4010 if (!tcg_out_pool_finalize(s)) {
4011 return -1;
4012 }
4013#endif
c896fe29
FB
4014
4015 /* flush instruction cache */
1813e175 4016 flush_icache_range((uintptr_t)s->code_buf, (uintptr_t)s->code_ptr);
2aeabc08 4017
1813e175 4018 return tcg_current_code_size(s);
c896fe29
FB
4019}
4020
a23a9ec6 4021#ifdef CONFIG_PROFILER
3de2faa9 4022void tcg_dump_info(void)
a23a9ec6 4023{
c3fac113
EC
4024 TCGProfile prof = {};
4025 const TCGProfile *s;
4026 int64_t tb_count;
4027 int64_t tb_div_count;
4028 int64_t tot;
4029
4030 tcg_profile_snapshot_counters(&prof);
4031 s = &prof;
4032 tb_count = s->tb_count;
4033 tb_div_count = tb_count ? tb_count : 1;
4034 tot = s->interm_time + s->code_time;
a23a9ec6 4035
3de2faa9 4036 qemu_printf("JIT cycles %" PRId64 " (%0.3f s at 2.4 GHz)\n",
a23a9ec6 4037 tot, tot / 2.4e9);
3de2faa9
MA
4038 qemu_printf("translated TBs %" PRId64 " (aborted=%" PRId64
4039 " %0.1f%%)\n",
fca8a500
RH
4040 tb_count, s->tb_count1 - tb_count,
4041 (double)(s->tb_count1 - s->tb_count)
4042 / (s->tb_count1 ? s->tb_count1 : 1) * 100.0);
3de2faa9 4043 qemu_printf("avg ops/TB %0.1f max=%d\n",
fca8a500 4044 (double)s->op_count / tb_div_count, s->op_count_max);
3de2faa9 4045 qemu_printf("deleted ops/TB %0.2f\n",
fca8a500 4046 (double)s->del_op_count / tb_div_count);
3de2faa9 4047 qemu_printf("avg temps/TB %0.2f max=%d\n",
fca8a500 4048 (double)s->temp_count / tb_div_count, s->temp_count_max);
3de2faa9 4049 qemu_printf("avg host code/TB %0.1f\n",
fca8a500 4050 (double)s->code_out_len / tb_div_count);
3de2faa9 4051 qemu_printf("avg search data/TB %0.1f\n",
fca8a500 4052 (double)s->search_out_len / tb_div_count);
a23a9ec6 4053
3de2faa9 4054 qemu_printf("cycles/op %0.1f\n",
a23a9ec6 4055 s->op_count ? (double)tot / s->op_count : 0);
3de2faa9 4056 qemu_printf("cycles/in byte %0.1f\n",
a23a9ec6 4057 s->code_in_len ? (double)tot / s->code_in_len : 0);
3de2faa9 4058 qemu_printf("cycles/out byte %0.1f\n",
a23a9ec6 4059 s->code_out_len ? (double)tot / s->code_out_len : 0);
3de2faa9 4060 qemu_printf("cycles/search byte %0.1f\n",
fca8a500
RH
4061 s->search_out_len ? (double)tot / s->search_out_len : 0);
4062 if (tot == 0) {
a23a9ec6 4063 tot = 1;
fca8a500 4064 }
3de2faa9 4065 qemu_printf(" gen_interm time %0.1f%%\n",
a23a9ec6 4066 (double)s->interm_time / tot * 100.0);
3de2faa9 4067 qemu_printf(" gen_code time %0.1f%%\n",
a23a9ec6 4068 (double)s->code_time / tot * 100.0);
3de2faa9 4069 qemu_printf("optim./code time %0.1f%%\n",
c5cc28ff
AJ
4070 (double)s->opt_time / (s->code_time ? s->code_time : 1)
4071 * 100.0);
3de2faa9 4072 qemu_printf("liveness/code time %0.1f%%\n",
a23a9ec6 4073 (double)s->la_time / (s->code_time ? s->code_time : 1) * 100.0);
3de2faa9 4074 qemu_printf("cpu_restore count %" PRId64 "\n",
a23a9ec6 4075 s->restore_count);
3de2faa9 4076 qemu_printf(" avg cycles %0.1f\n",
a23a9ec6 4077 s->restore_count ? (double)s->restore_time / s->restore_count : 0);
a23a9ec6
FB
4078}
4079#else
3de2faa9 4080void tcg_dump_info(void)
a23a9ec6 4081{
3de2faa9 4082 qemu_printf("[TCG profiler not compiled]\n");
a23a9ec6
FB
4083}
4084#endif
813da627
RH
4085
4086#ifdef ELF_HOST_MACHINE
5872bbf2
RH
4087/* In order to use this feature, the backend needs to do three things:
4088
4089 (1) Define ELF_HOST_MACHINE to indicate both what value to
4090 put into the ELF image and to indicate support for the feature.
4091
4092 (2) Define tcg_register_jit. This should create a buffer containing
4093 the contents of a .debug_frame section that describes the post-
4094 prologue unwind info for the tcg machine.
4095
4096 (3) Call tcg_register_jit_int, with the constructed .debug_frame.
4097*/
813da627
RH
4098
4099/* Begin GDB interface. THE FOLLOWING MUST MATCH GDB DOCS. */
4100typedef enum {
4101 JIT_NOACTION = 0,
4102 JIT_REGISTER_FN,
4103 JIT_UNREGISTER_FN
4104} jit_actions_t;
4105
4106struct jit_code_entry {
4107 struct jit_code_entry *next_entry;
4108 struct jit_code_entry *prev_entry;
4109 const void *symfile_addr;
4110 uint64_t symfile_size;
4111};
4112
4113struct jit_descriptor {
4114 uint32_t version;
4115 uint32_t action_flag;
4116 struct jit_code_entry *relevant_entry;
4117 struct jit_code_entry *first_entry;
4118};
4119
4120void __jit_debug_register_code(void) __attribute__((noinline));
4121void __jit_debug_register_code(void)
4122{
4123 asm("");
4124}
4125
4126/* Must statically initialize the version, because GDB may check
4127 the version before we can set it. */
4128struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
4129
4130/* End GDB interface. */
4131
4132static int find_string(const char *strtab, const char *str)
4133{
4134 const char *p = strtab + 1;
4135
4136 while (1) {
4137 if (strcmp(p, str) == 0) {
4138 return p - strtab;
4139 }
4140 p += strlen(p) + 1;
4141 }
4142}
4143
5872bbf2 4144static void tcg_register_jit_int(void *buf_ptr, size_t buf_size,
2c90784a
RH
4145 const void *debug_frame,
4146 size_t debug_frame_size)
813da627 4147{
5872bbf2
RH
4148 struct __attribute__((packed)) DebugInfo {
4149 uint32_t len;
4150 uint16_t version;
4151 uint32_t abbrev;
4152 uint8_t ptr_size;
4153 uint8_t cu_die;
4154 uint16_t cu_lang;
4155 uintptr_t cu_low_pc;
4156 uintptr_t cu_high_pc;
4157 uint8_t fn_die;
4158 char fn_name[16];
4159 uintptr_t fn_low_pc;
4160 uintptr_t fn_high_pc;
4161 uint8_t cu_eoc;
4162 };
813da627
RH
4163
4164 struct ElfImage {
4165 ElfW(Ehdr) ehdr;
4166 ElfW(Phdr) phdr;
5872bbf2
RH
4167 ElfW(Shdr) shdr[7];
4168 ElfW(Sym) sym[2];
4169 struct DebugInfo di;
4170 uint8_t da[24];
4171 char str[80];
4172 };
4173
4174 struct ElfImage *img;
4175
4176 static const struct ElfImage img_template = {
4177 .ehdr = {
4178 .e_ident[EI_MAG0] = ELFMAG0,
4179 .e_ident[EI_MAG1] = ELFMAG1,
4180 .e_ident[EI_MAG2] = ELFMAG2,
4181 .e_ident[EI_MAG3] = ELFMAG3,
4182 .e_ident[EI_CLASS] = ELF_CLASS,
4183 .e_ident[EI_DATA] = ELF_DATA,
4184 .e_ident[EI_VERSION] = EV_CURRENT,
4185 .e_type = ET_EXEC,
4186 .e_machine = ELF_HOST_MACHINE,
4187 .e_version = EV_CURRENT,
4188 .e_phoff = offsetof(struct ElfImage, phdr),
4189 .e_shoff = offsetof(struct ElfImage, shdr),
4190 .e_ehsize = sizeof(ElfW(Shdr)),
4191 .e_phentsize = sizeof(ElfW(Phdr)),
4192 .e_phnum = 1,
4193 .e_shentsize = sizeof(ElfW(Shdr)),
4194 .e_shnum = ARRAY_SIZE(img->shdr),
4195 .e_shstrndx = ARRAY_SIZE(img->shdr) - 1,
abbb3eae
RH
4196#ifdef ELF_HOST_FLAGS
4197 .e_flags = ELF_HOST_FLAGS,
4198#endif
4199#ifdef ELF_OSABI
4200 .e_ident[EI_OSABI] = ELF_OSABI,
4201#endif
5872bbf2
RH
4202 },
4203 .phdr = {
4204 .p_type = PT_LOAD,
4205 .p_flags = PF_X,
4206 },
4207 .shdr = {
4208 [0] = { .sh_type = SHT_NULL },
4209 /* Trick: The contents of code_gen_buffer are not present in
4210 this fake ELF file; that got allocated elsewhere. Therefore
4211 we mark .text as SHT_NOBITS (similar to .bss) so that readers
4212 will not look for contents. We can record any address. */
4213 [1] = { /* .text */
4214 .sh_type = SHT_NOBITS,
4215 .sh_flags = SHF_EXECINSTR | SHF_ALLOC,
4216 },
4217 [2] = { /* .debug_info */
4218 .sh_type = SHT_PROGBITS,
4219 .sh_offset = offsetof(struct ElfImage, di),
4220 .sh_size = sizeof(struct DebugInfo),
4221 },
4222 [3] = { /* .debug_abbrev */
4223 .sh_type = SHT_PROGBITS,
4224 .sh_offset = offsetof(struct ElfImage, da),
4225 .sh_size = sizeof(img->da),
4226 },
4227 [4] = { /* .debug_frame */
4228 .sh_type = SHT_PROGBITS,
4229 .sh_offset = sizeof(struct ElfImage),
4230 },
4231 [5] = { /* .symtab */
4232 .sh_type = SHT_SYMTAB,
4233 .sh_offset = offsetof(struct ElfImage, sym),
4234 .sh_size = sizeof(img->sym),
4235 .sh_info = 1,
4236 .sh_link = ARRAY_SIZE(img->shdr) - 1,
4237 .sh_entsize = sizeof(ElfW(Sym)),
4238 },
4239 [6] = { /* .strtab */
4240 .sh_type = SHT_STRTAB,
4241 .sh_offset = offsetof(struct ElfImage, str),
4242 .sh_size = sizeof(img->str),
4243 }
4244 },
4245 .sym = {
4246 [1] = { /* code_gen_buffer */
4247 .st_info = ELF_ST_INFO(STB_GLOBAL, STT_FUNC),
4248 .st_shndx = 1,
4249 }
4250 },
4251 .di = {
4252 .len = sizeof(struct DebugInfo) - 4,
4253 .version = 2,
4254 .ptr_size = sizeof(void *),
4255 .cu_die = 1,
4256 .cu_lang = 0x8001, /* DW_LANG_Mips_Assembler */
4257 .fn_die = 2,
4258 .fn_name = "code_gen_buffer"
4259 },
4260 .da = {
4261 1, /* abbrev number (the cu) */
4262 0x11, 1, /* DW_TAG_compile_unit, has children */
4263 0x13, 0x5, /* DW_AT_language, DW_FORM_data2 */
4264 0x11, 0x1, /* DW_AT_low_pc, DW_FORM_addr */
4265 0x12, 0x1, /* DW_AT_high_pc, DW_FORM_addr */
4266 0, 0, /* end of abbrev */
4267 2, /* abbrev number (the fn) */
4268 0x2e, 0, /* DW_TAG_subprogram, no children */
4269 0x3, 0x8, /* DW_AT_name, DW_FORM_string */
4270 0x11, 0x1, /* DW_AT_low_pc, DW_FORM_addr */
4271 0x12, 0x1, /* DW_AT_high_pc, DW_FORM_addr */
4272 0, 0, /* end of abbrev */
4273 0 /* no more abbrev */
4274 },
4275 .str = "\0" ".text\0" ".debug_info\0" ".debug_abbrev\0"
4276 ".debug_frame\0" ".symtab\0" ".strtab\0" "code_gen_buffer",
813da627
RH
4277 };
4278
4279 /* We only need a single jit entry; statically allocate it. */
4280 static struct jit_code_entry one_entry;
4281
5872bbf2 4282 uintptr_t buf = (uintptr_t)buf_ptr;
813da627 4283 size_t img_size = sizeof(struct ElfImage) + debug_frame_size;
2c90784a 4284 DebugFrameHeader *dfh;
813da627 4285
5872bbf2
RH
4286 img = g_malloc(img_size);
4287 *img = img_template;
813da627 4288
5872bbf2
RH
4289 img->phdr.p_vaddr = buf;
4290 img->phdr.p_paddr = buf;
4291 img->phdr.p_memsz = buf_size;
813da627 4292
813da627 4293 img->shdr[1].sh_name = find_string(img->str, ".text");
5872bbf2 4294 img->shdr[1].sh_addr = buf;
813da627
RH
4295 img->shdr[1].sh_size = buf_size;
4296
5872bbf2
RH
4297 img->shdr[2].sh_name = find_string(img->str, ".debug_info");
4298 img->shdr[3].sh_name = find_string(img->str, ".debug_abbrev");
4299
4300 img->shdr[4].sh_name = find_string(img->str, ".debug_frame");
4301 img->shdr[4].sh_size = debug_frame_size;
4302
4303 img->shdr[5].sh_name = find_string(img->str, ".symtab");
4304 img->shdr[6].sh_name = find_string(img->str, ".strtab");
4305
4306 img->sym[1].st_name = find_string(img->str, "code_gen_buffer");
4307 img->sym[1].st_value = buf;
4308 img->sym[1].st_size = buf_size;
813da627 4309
5872bbf2 4310 img->di.cu_low_pc = buf;
45aba097 4311 img->di.cu_high_pc = buf + buf_size;
5872bbf2 4312 img->di.fn_low_pc = buf;
45aba097 4313 img->di.fn_high_pc = buf + buf_size;
813da627 4314
2c90784a
RH
4315 dfh = (DebugFrameHeader *)(img + 1);
4316 memcpy(dfh, debug_frame, debug_frame_size);
4317 dfh->fde.func_start = buf;
4318 dfh->fde.func_len = buf_size;
4319
813da627
RH
4320#ifdef DEBUG_JIT
4321 /* Enable this block to be able to debug the ELF image file creation.
4322 One can use readelf, objdump, or other inspection utilities. */
4323 {
4324 FILE *f = fopen("/tmp/qemu.jit", "w+b");
4325 if (f) {
5872bbf2 4326 if (fwrite(img, img_size, 1, f) != img_size) {
813da627
RH
4327 /* Avoid stupid unused return value warning for fwrite. */
4328 }
4329 fclose(f);
4330 }
4331 }
4332#endif
4333
4334 one_entry.symfile_addr = img;
4335 one_entry.symfile_size = img_size;
4336
4337 __jit_debug_descriptor.action_flag = JIT_REGISTER_FN;
4338 __jit_debug_descriptor.relevant_entry = &one_entry;
4339 __jit_debug_descriptor.first_entry = &one_entry;
4340 __jit_debug_register_code();
4341}
4342#else
5872bbf2
RH
4343/* No support for the feature. Provide the entry point expected by exec.c,
4344 and implement the internal function we declared earlier. */
813da627
RH
4345
4346static void tcg_register_jit_int(void *buf, size_t size,
2c90784a
RH
4347 const void *debug_frame,
4348 size_t debug_frame_size)
813da627
RH
4349{
4350}
4351
4352void tcg_register_jit(void *buf, size_t buf_size)
4353{
4354}
4355#endif /* ELF_HOST_MACHINE */
db432672
RH
4356
4357#if !TCG_TARGET_MAYBE_vec
4358void tcg_expand_vec_op(TCGOpcode o, TCGType t, unsigned e, TCGArg a0, ...)
4359{
4360 g_assert_not_reached();
4361}
4362#endif