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