From patchwork Wed Nov 28 05:38:34 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 1004224 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=2001:4830:134:3::11; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="HFKocHvP"; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 434V4V26SQz9s3Z for ; Wed, 28 Nov 2018 16:45:44 +1100 (AEDT) Received: from localhost ([::1]:46056 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gRsfM-0001ip-VE for incoming@patchwork.ozlabs.org; Wed, 28 Nov 2018 00:45:41 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48042) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gRsYq-00041E-Lm for qemu-devel@nongnu.org; Wed, 28 Nov 2018 00:38:57 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gRsYm-0004QS-P9 for qemu-devel@nongnu.org; Wed, 28 Nov 2018 00:38:54 -0500 Received: from mail-pf1-x42d.google.com ([2607:f8b0:4864:20::42d]:37519) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gRsYm-0004Pv-GR for qemu-devel@nongnu.org; Wed, 28 Nov 2018 00:38:52 -0500 Received: by mail-pf1-x42d.google.com with SMTP id y126so1726963pfb.4 for ; Tue, 27 Nov 2018 21:38:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=i1xOLMMSZPEBxneoRmjCkNCjZxttxX47mgYHoGxFQFg=; b=HFKocHvPFs7hBDnB6PtIiBpC1GToCPzOuOL1mY0T9n7W0HnUcHSG0e9weQCyxtub5z D6tTXxXr6juiPTMQyfbxv+EsFEgfx2dslSp0fLiAi5Q2bmY30l1lWFVZq9xRNFDlMkul APAzFqb1OvPz7vWX+04JIplKWk1eTNAEBWpKQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=i1xOLMMSZPEBxneoRmjCkNCjZxttxX47mgYHoGxFQFg=; b=bBKZtI/3hceQAjzEwGoFXWMGO2xi99mLqu7Jt5EHMcA8OUe1CN+EQVmfkHVH6JTad8 /uPW5aWEq5qbqvmpmV2bZ2P2LHRhKf93lnrXhIohhsTGrH2N3wHvdB+VjzScynvasRb3 1+37GqByEYlTBz7dBUbJ0McTYQKEpwW5WVqX2FFKY013pXtlJAkoQj7Q91payJoWo2ZZ iB3ZLJByTN3VqjsD0tIH3FFyoT+drtdaJtpUvAYQoL2wmsCmr+YrsQBtY0FZ06vTNeRB axbrUf2xnGuHUeGh7VDlKQ/fKQTu/ieU+IrVuRf/JJ3Vdy63pQeRII7n1oxckwlcJYmr 1x5w== X-Gm-Message-State: AA+aEWb8N22pAMfaF+D3+kzwbIWYGVaJNI+przNYNTInIThDEpT854A/ 3fQgCtZKqc363YL64HNf2Y/be4jo8o0= X-Google-Smtp-Source: AFSGD/Us8eQIYRPovESkncK6m4DgsUwT/fLdJtfew0a9jBcDd0x/P2Xfe+fUp/2lP0SX1cmFdAilqA== X-Received: by 2002:a63:1b48:: with SMTP id b8mr32254069pgm.187.1543383531082; Tue, 27 Nov 2018 21:38:51 -0800 (PST) Received: from cloudburst.twiddle.net (97-113-170-180.tukw.qwest.net. [97.113.170.180]) by smtp.gmail.com with ESMTPSA id 19sm8569336pfs.108.2018.11.27.21.38.49 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 27 Nov 2018 21:38:50 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Date: Tue, 27 Nov 2018 21:38:34 -0800 Message-Id: <20181128053834.10861-13-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20181128053834.10861-1-richard.henderson@linaro.org> References: <20181128053834.10861-1-richard.henderson@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::42d Subject: [Qemu-devel] [PATCH 12/12] tcg: Record register preferences during liveness X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: cota@braap.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" With these preferences, we can arrange for function call arguments to be computed into the proper registers instead of requiring extra moves. Signed-off-by: Richard Henderson --- tcg/tcg.c | 197 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 165 insertions(+), 32 deletions(-) diff --git a/tcg/tcg.c b/tcg/tcg.c index 673aaf52a1..734a453fc8 100644 --- a/tcg/tcg.c +++ b/tcg/tcg.c @@ -2387,6 +2387,21 @@ static void reachable_code_pass(TCGContext *s) #define IS_DEAD_ARG(n) (arg_life & (DEAD_ARG << (n))) #define NEED_SYNC_ARG(n) (arg_life & (SYNC_ARG << (n))) +/* For liveness_pass_1, the register preferences for a given temp. */ +static inline TCGRegSet *la_temp_pref(TCGTemp *ts) +{ + return ts->state_ptr; +} + +/* For liveness_pass_1, reset the preferences for a given temp to the + * maximal regset for its type. + */ +static inline void la_reset_pref(TCGTemp *ts) +{ + *la_temp_pref(ts) + = (ts->state == TS_DEAD ? 0 : tcg_target_available_regs[ts->type]); +} + /* liveness analysis: end of function: all temps are dead, and globals should be in memory. */ static void la_func_end(TCGContext *s, int ng, int nt) @@ -2395,9 +2410,11 @@ static void la_func_end(TCGContext *s, int ng, int nt) for (i = 0; i < ng; ++i) { s->temps[i].state = TS_DEAD | TS_MEM; + la_reset_pref(&s->temps[i]); } for (i = ng; i < nt; ++i) { s->temps[i].state = TS_DEAD; + la_reset_pref(&s->temps[i]); } } @@ -2409,11 +2426,13 @@ static void la_bb_end(TCGContext *s, int ng, int nt) for (i = 0; i < ng; ++i) { s->temps[i].state = TS_DEAD | TS_MEM; + la_reset_pref(&s->temps[i]); } for (i = ng; i < nt; ++i) { s->temps[i].state = (s->temps[i].temp_local ? TS_DEAD | TS_MEM : TS_DEAD); + la_reset_pref(&s->temps[i]); } } @@ -2423,7 +2442,12 @@ static void la_global_sync(TCGContext *s, int ng) int i; for (i = 0; i < ng; ++i) { - s->temps[i].state |= TS_MEM; + int state = s->temps[i].state; + s->temps[i].state = state | TS_MEM; + if (state == TS_DEAD) { + /* If the global was previously dead, reset prefs. */ + la_reset_pref(&s->temps[i]); + } } } @@ -2434,6 +2458,29 @@ static void la_global_kill(TCGContext *s, int ng) for (i = 0; i < ng; i++) { s->temps[i].state = TS_DEAD | TS_MEM; + la_reset_pref(&s->temps[i]); + } +} + +/* liveness analysis: note live globals crossing calls. */ +static void la_cross_call(TCGContext *s, int nt) +{ + TCGRegSet mask = ~tcg_target_call_clobber_regs; + int i; + + for (i = 0; i < nt; i++) { + TCGTemp *ts = &s->temps[i]; + if (!(ts->state & TS_DEAD)) { + TCGRegSet *pset = la_temp_pref(ts); + TCGRegSet set = *pset; + + set &= mask; + /* If the combination is not possible, restart. */ + if (set == 0) { + set = tcg_target_available_regs[ts->type] & mask; + } + *pset = set; + } } } @@ -2445,16 +2492,23 @@ static void liveness_pass_1(TCGContext *s) int nb_globals = s->nb_globals; int nb_temps = s->nb_temps; TCGOp *op, *op_prev; + TCGRegSet *prefs; + int i; + + prefs = tcg_malloc(sizeof(TCGRegSet) * nb_temps); + for (i = 0; i < nb_temps; ++i) { + s->temps[i].state_ptr = prefs + i; + } /* ??? Should be redundant with the exit_tb that ends the TB. */ la_func_end(s, nb_globals, nb_temps); QTAILQ_FOREACH_REVERSE_SAFE(op, &s->ops, TCGOpHead, link, op_prev) { - int i, nb_iargs, nb_oargs; + int nb_iargs, nb_oargs; TCGOpcode opc_new, opc_new2; bool have_opc_new2; TCGLifeData arg_life = 0; - TCGTemp *arg_ts; + TCGTemp *ts; TCGOpcode opc = op->opc; const TCGOpDef *def = &tcg_op_defs[opc]; @@ -2462,6 +2516,7 @@ static void liveness_pass_1(TCGContext *s) case INDEX_op_call: { int call_flags; + int nb_call_regs; nb_oargs = TCGOP_CALLO(op); nb_iargs = TCGOP_CALLI(op); @@ -2470,8 +2525,8 @@ static void liveness_pass_1(TCGContext *s) /* pure functions can be removed if their result is unused */ if (call_flags & TCG_CALL_NO_SIDE_EFFECTS) { for (i = 0; i < nb_oargs; i++) { - arg_ts = arg_temp(op->args[i]); - if (arg_ts->state != TS_DEAD) { + ts = arg_temp(op->args[i]); + if (ts->state != TS_DEAD) { goto do_not_remove_call; } } @@ -2479,16 +2534,20 @@ static void liveness_pass_1(TCGContext *s) } do_not_remove_call: - /* output args are dead */ + /* Output args are dead. */ for (i = 0; i < nb_oargs; i++) { - arg_ts = arg_temp(op->args[i]); - if (arg_ts->state & TS_DEAD) { + ts = arg_temp(op->args[i]); + if (ts->state & TS_DEAD) { arg_life |= DEAD_ARG << i; } - if (arg_ts->state & TS_MEM) { + if (ts->state & TS_MEM) { arg_life |= SYNC_ARG << i; } - arg_ts->state = TS_DEAD; + ts->state = TS_DEAD; + la_reset_pref(ts); + + /* Not used -- it will be tcg_target_call_oarg_regs[i]. */ + op->output_pref[i] = 0; } if (!(call_flags & (TCG_CALL_NO_WRITE_GLOBALS | @@ -2498,18 +2557,42 @@ static void liveness_pass_1(TCGContext *s) la_global_sync(s, nb_globals); } - /* record arguments that die in this helper */ + /* Record arguments that die in this helper. */ for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) { - arg_ts = arg_temp(op->args[i]); - if (arg_ts && arg_ts->state & TS_DEAD) { + ts = arg_temp(op->args[i]); + if (ts && ts->state & TS_DEAD) { arg_life |= DEAD_ARG << i; } } - /* input arguments are live for preceding opcodes */ - for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) { - arg_ts = arg_temp(op->args[i]); - if (arg_ts) { - arg_ts->state &= ~TS_DEAD; + + /* For all live registers, remove call-clobbered prefs. */ + la_cross_call(s, nb_temps); + + nb_call_regs = ARRAY_SIZE(tcg_target_call_iarg_regs); + + /* Input arguments are live for preceding opcodes. */ + for (i = 0; i < nb_iargs; i++) { + ts = arg_temp(op->args[i + nb_oargs]); + if (ts && ts->state & TS_DEAD) { + /* For those arguments that die, and will be allocated + * in registers, clear the register set for that arg, + * to be filled in below. For args that will be on + * the stack, reset to any available reg. + */ + *la_temp_pref(ts) + = (i < nb_call_regs ? 0 : + tcg_target_available_regs[ts->type]); + ts->state &= ~TS_DEAD; + } + } + + /* For each input argument, add its input register to prefs. + If a temp is used once, this produces a single set bit. */ + for (i = 0; i < MIN(nb_call_regs, nb_iargs); i++) { + ts = arg_temp(op->args[i + nb_oargs]); + if (ts) { + tcg_regset_set_reg(*la_temp_pref(ts), + tcg_target_call_iarg_regs[i]); } } } @@ -2518,7 +2601,9 @@ static void liveness_pass_1(TCGContext *s) break; case INDEX_op_discard: /* mark the temporary as dead */ - arg_temp(op->args[0])->state = TS_DEAD; + ts = arg_temp(op->args[0]); + ts->state = TS_DEAD; + la_reset_pref(ts); break; case INDEX_op_add2_i32: @@ -2622,43 +2707,91 @@ static void liveness_pass_1(TCGContext *s) break; do_not_remove: - /* output args are dead */ for (i = 0; i < nb_oargs; i++) { - arg_ts = arg_temp(op->args[i]); - if (arg_ts->state & TS_DEAD) { + ts = arg_temp(op->args[i]); + + /* Remember the preference of the uses that followed. */ + op->output_pref[i] = *la_temp_pref(ts); + + /* Output args are dead. */ + if (ts->state & TS_DEAD) { arg_life |= DEAD_ARG << i; } - if (arg_ts->state & TS_MEM) { + if (ts->state & TS_MEM) { arg_life |= SYNC_ARG << i; } - arg_ts->state = TS_DEAD; + ts->state = TS_DEAD; + la_reset_pref(ts); } - /* if end of basic block, update */ + /* If end of basic block, update. */ if (def->flags & TCG_OPF_BB_EXIT) { la_func_end(s, nb_globals, nb_temps); } else if (def->flags & TCG_OPF_BB_END) { la_bb_end(s, nb_globals, nb_temps); } else if (def->flags & TCG_OPF_SIDE_EFFECTS) { la_global_sync(s, nb_globals); + if (def->flags & TCG_OPF_CALL_CLOBBER) { + la_cross_call(s, nb_temps); + } } - /* record arguments that die in this opcode */ + /* Record arguments that die in this opcode. */ for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) { - arg_ts = arg_temp(op->args[i]); - if (arg_ts->state & TS_DEAD) { + ts = arg_temp(op->args[i]); + if (ts->state & TS_DEAD) { arg_life |= DEAD_ARG << i; } } - /* input arguments are live for preceding opcodes */ + + /* Input arguments are live for preceding opcodes. */ for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) { - arg_temp(op->args[i])->state &= ~TS_DEAD; + ts = arg_temp(op->args[i]); + if (ts->state & TS_DEAD) { + /* For operands that were dead, initially allow + all regs for the type. */ + *la_temp_pref(ts) = tcg_target_available_regs[ts->type]; + ts->state &= ~TS_DEAD; + } + } + + /* Incorporate constraints for this operand. */ + switch (opc) { + case INDEX_op_mov_i32: + case INDEX_op_mov_i64: + /* Note that these are TCG_OPF_NOT_PRESENT and do not + have proper constraints. That said, special case + moves to propagate preferences backward. */ + if (IS_DEAD_ARG(1)) { + *la_temp_pref(arg_temp(op->args[0])) + = *la_temp_pref(arg_temp(op->args[1])); + } + break; + + default: + for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) { + const TCGArgConstraint *ct = &def->args_ct[i]; + TCGRegSet set, *pset; + + ts = arg_temp(op->args[i]); + pset = la_temp_pref(ts); + set = *pset; + + set &= ct->u.regs; + if (ct->ct & TCG_CT_IALIAS) { + set &= op->output_pref[ct->alias_index]; + } + /* If the combination is not possible, restart. */ + if (set == 0) { + set = ct->u.regs; + } + *pset = set; + } + break; } break; } op->life = arg_life; - op->output_pref[0] = 0; - op->output_pref[1] = 0; } }