From patchwork Tue Dec 25 20:55:17 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 154491 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp5218180ljp; Tue, 25 Dec 2018 13:11:39 -0800 (PST) X-Google-Smtp-Source: ALg8bN5gQn98ZikcKh9smeTMpndTkc0hP3WQshMss5SyKzxDo0/AJubHNfsXK1xx9bs5g3S/4TxV X-Received: by 2002:a37:7845:: with SMTP id t66mr16009728qkc.103.1545772299195; Tue, 25 Dec 2018 13:11:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545772299; cv=none; d=google.com; s=arc-20160816; b=K0xJGnGqV8NOOpA7rlV5kJEwidh/oxAHdtfA4iqfuP6Vd6ZxvF2cQLW1s48nmEwXz2 85jxHSNKqEtyCtV3ZOnl26Y36Wc6nMl/fhEWc2Vthm/nxEU9pjDvx7yN9l3O6vEWj9Dn M7nJL2G0s5EsVbra25CE5294hd87h1ITybCehwc64TI7svRGT758eBOeWEs11c0Z/no1 w1NhYbbTgkNHnJUmSgJqTePdQmCeaK2XZfOy6+qsfjCBqBF2i924YK6uNfxI8MrG7Fd8 qACK+SzH7YB8WDpwfRy8iXJbDHmtbKogK3pu26d7GCYnx2zYpc7rYBPTnWdTw2zXKsm7 WUbw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:references:in-reply-to :message-id:date:to:from:dkim-signature; bh=rFyVT3+Q74xkj2Qe1vgfvSjqk0XbFRCtmL1wHv80Mks=; b=EHBJ0BxZ0Gwame0c+xAI3vqXvJJip64vzXVghjL0awjDsmrS9UVYry549agS4fVt4E z7pKeCj13aon2nIxzkFnhU4addAZFl0ywtZfdNZypYhSw1VTWPtxGz3fx/sZbRKyJ3lq Zr1Lrn73z3B/OuigikdAbbsAma0wa1Q4/yoYv2kDyW3Lv+GT1cWwHSbR+gmIVY2IviLe Y45DrXcN/G0DfgSETEL+x1pxQ0JT6GzJJEzCBMAzb8e9mFwWge3iAc5asxd8Eupc5QTU HM8Vrxm9B5VLSt+2WQlU7PRGlVbuJUWlR8Hfxm/sROoBfKfDzQeg8cLonqUfOX/7bt0y YEPQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=I9Q1OrMS; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [208.118.235.17]) by mx.google.com with ESMTPS id x8si390392qtm.5.2018.12.25.13.11.39 for (version=TLS1 cipher=AES128-SHA bits=128/128); Tue, 25 Dec 2018 13:11:39 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=I9Q1OrMS; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([127.0.0.1]:43150 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gbtzG-0006Ow-IB for patch@linaro.org; Tue, 25 Dec 2018 16:11:38 -0500 Received: from eggs.gnu.org ([208.118.235.92]:50583) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gbtnP-0007WR-NH for qemu-devel@nongnu.org; Tue, 25 Dec 2018 15:59:24 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gbtnN-0001Cd-PJ for qemu-devel@nongnu.org; Tue, 25 Dec 2018 15:59:23 -0500 Received: from mail-pl1-x630.google.com ([2607:f8b0:4864:20::630]:33938) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gbtnL-00019W-Qb for qemu-devel@nongnu.org; Tue, 25 Dec 2018 15:59:21 -0500 Received: by mail-pl1-x630.google.com with SMTP id w4so6820442plz.1 for ; Tue, 25 Dec 2018 12:59:18 -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=rFyVT3+Q74xkj2Qe1vgfvSjqk0XbFRCtmL1wHv80Mks=; b=I9Q1OrMSXm4UVqn7Ig51ErgEaoaNugnsKCYG+37Huz5IcqU1C42vKA3gyXnEmQVK0S m6O20fWRURTlqs0juaQowc7vHILz5LDFuw3IUz180NuXkp+HoxoO3Z+h3yzl4Zv0MLTI Imuyuk8CjLvSTbbZ5Y/S/YQmiggiC1OA69BkI= 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=rFyVT3+Q74xkj2Qe1vgfvSjqk0XbFRCtmL1wHv80Mks=; b=F4IkG2Xf1bkSTFAqIZANHQZ+2wXhMnGe+mZPzRFO74ZpIy8Qozfa3HxlAsELUZAMb6 /JKfTx8J6RjE+ye0k0GsMeWlP6jnUaZJBJpwcnB+DpI/Hdo3bTF69R8f6wgM3r1YQvGZ DtkNIL3C9kNJ/5AG2Wi5hBk/ktsX9aPUadHB49qvp0tRmGjdrpefaDa+pZ1B9cRQyEDP lWn5D6b1+N7vBRepETRlCz5xgD1az2IB89CcHzq4xaozedf2zb9PVn22pM1UJPDcINFr t5+TBrXLZsPyZ47ctYYEhCBThY7zCVTGJ5SE2YfHaPISjLVvbw56f0PkbWG5KFbTms3u DQgw== X-Gm-Message-State: AJcUukdYVmuPiuk0d6/DQlONvG9CdZJADxPNgV/n24wgWzfs/SzChQ1b z+/Sgg1yipXnKP8ms+mlF1VvyvMg7lw= X-Received: by 2002:a17:902:3f81:: with SMTP id a1mr17267385pld.258.1545771557117; Tue, 25 Dec 2018 12:59:17 -0800 (PST) Received: from cloudburst.home (c211-28-135-144.sunsh3.vic.optusnet.com.au. [211.28.135.144]) by smtp.gmail.com with ESMTPSA id t21sm48501628pgg.24.2018.12.25.12.59.11 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 25 Dec 2018 12:59:16 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Date: Wed, 26 Dec 2018 07:55:17 +1100 Message-Id: <20181225205529.10874-31-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20181225205529.10874-1-richard.henderson@linaro.org> References: <20181225205529.10874-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::630 Subject: [Qemu-devel] [PULL 30/42] tcg: Add preferred_reg argument to tcg_reg_alloc 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: peter.maydell@linaro.org Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" This new argument will aid register allocation by indicating how the temporary will be used in future. If the preference cannot be satisfied, fall back to the constraints of the current insn. Short circuit the preference when it cannot be satisfied or if it does not further constrain the operation. With an eye toward optimizing function call sequences, optimize for the preferred_reg set containing a single register. For the moment, all users pass 0 for preference. Reviewed-by: Emilio G. Cota Signed-off-by: Richard Henderson --- tcg/tcg.c | 103 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 81 insertions(+), 22 deletions(-) -- 2.17.2 diff --git a/tcg/tcg.c b/tcg/tcg.c index d2be550ab4..210bd5c6b9 100644 --- a/tcg/tcg.c +++ b/tcg/tcg.c @@ -1887,6 +1887,20 @@ static const char * const alignment_name[(MO_AMASK >> MO_ASHIFT) + 1] = { [MO_ALIGN_64 >> MO_ASHIFT] = "al64+", }; +static inline bool tcg_regset_single(TCGRegSet d) +{ + return (d & (d - 1)) == 0; +} + +static inline TCGReg tcg_regset_first(TCGRegSet d) +{ + if (TCG_TARGET_NB_REGS <= 32) { + return ctz32(d); + } else { + return ctz64(d); + } +} + void tcg_dump_ops(TCGContext *s) { char buf[128]; @@ -1902,6 +1916,7 @@ void tcg_dump_ops(TCGContext *s) def = &tcg_op_defs[c]; if (c == INDEX_op_insn_start) { + nb_oargs = 0; col += qemu_log("\n ----"); for (i = 0; i < TARGET_INSN_START_WORDS; ++i) { @@ -2902,31 +2917,72 @@ static void tcg_reg_free(TCGContext *s, TCGReg reg, TCGRegSet allocated_regs) } } -/* Allocate a register belonging to reg1 & ~reg2 */ -static TCGReg tcg_reg_alloc(TCGContext *s, TCGRegSet desired_regs, - TCGRegSet allocated_regs, bool rev) +/** + * tcg_reg_alloc: + * @required_regs: Set of registers in which we must allocate. + * @allocated_regs: Set of registers which must be avoided. + * @preferred_regs: Set of registers we should prefer. + * @rev: True if we search the registers in "indirect" order. + * + * The allocated register must be in @required_regs & ~@allocated_regs, + * but if we can put it in @preferred_regs we may save a move later. + */ +static TCGReg tcg_reg_alloc(TCGContext *s, TCGRegSet required_regs, + TCGRegSet allocated_regs, + TCGRegSet preferred_regs, bool rev) { - int i, n = ARRAY_SIZE(tcg_target_reg_alloc_order); + int i, j, f, n = ARRAY_SIZE(tcg_target_reg_alloc_order); + TCGRegSet reg_ct[2]; const int *order; - TCGReg reg; - TCGRegSet reg_ct; - reg_ct = desired_regs & ~allocated_regs; + reg_ct[1] = required_regs & ~allocated_regs; + tcg_debug_assert(reg_ct[1] != 0); + reg_ct[0] = reg_ct[1] & preferred_regs; + + /* Skip the preferred_regs option if it cannot be satisfied, + or if the preference made no difference. */ + f = reg_ct[0] == 0 || reg_ct[0] == reg_ct[1]; + order = rev ? indirect_reg_alloc_order : tcg_target_reg_alloc_order; - /* first try free registers */ - for(i = 0; i < n; i++) { - reg = order[i]; - if (tcg_regset_test_reg(reg_ct, reg) && s->reg_to_temp[reg] == NULL) - return reg; + /* Try free registers, preferences first. */ + for (j = f; j < 2; j++) { + TCGRegSet set = reg_ct[j]; + + if (tcg_regset_single(set)) { + /* One register in the set. */ + TCGReg reg = tcg_regset_first(set); + if (s->reg_to_temp[reg] == NULL) { + return reg; + } + } else { + for (i = 0; i < n; i++) { + TCGReg reg = order[i]; + if (s->reg_to_temp[reg] == NULL && + tcg_regset_test_reg(set, reg)) { + return reg; + } + } + } } - /* XXX: do better spill choice */ - for(i = 0; i < n; i++) { - reg = order[i]; - if (tcg_regset_test_reg(reg_ct, reg)) { + /* We must spill something. */ + for (j = f; j < 2; j++) { + TCGRegSet set = reg_ct[j]; + + if (tcg_regset_single(set)) { + /* One register in the set. */ + TCGReg reg = tcg_regset_first(set); tcg_reg_free(s, reg, allocated_regs); return reg; + } else { + for (i = 0; i < n; i++) { + TCGReg reg = order[i]; + if (tcg_regset_test_reg(set, reg)) { + tcg_reg_free(s, reg, allocated_regs); + return reg; + } + } } } @@ -2944,12 +3000,14 @@ static void temp_load(TCGContext *s, TCGTemp *ts, TCGRegSet desired_regs, case TEMP_VAL_REG: return; case TEMP_VAL_CONST: - reg = tcg_reg_alloc(s, desired_regs, allocated_regs, ts->indirect_base); + reg = tcg_reg_alloc(s, desired_regs, allocated_regs, + 0, ts->indirect_base); tcg_out_movi(s, ts->type, reg, ts->val); ts->mem_coherent = 0; break; case TEMP_VAL_MEM: - reg = tcg_reg_alloc(s, desired_regs, allocated_regs, ts->indirect_base); + reg = tcg_reg_alloc(s, desired_regs, allocated_regs, + 0, ts->indirect_base); tcg_out_ld(s, ts->type, reg, ts->mem_base->reg, ts->mem_offset); ts->mem_coherent = 1; break; @@ -3109,7 +3167,8 @@ static void tcg_reg_alloc_mov(TCGContext *s, const TCGOp *op) input one. */ tcg_regset_set_reg(allocated_regs, ts->reg); ots->reg = tcg_reg_alloc(s, tcg_target_available_regs[otype], - allocated_regs, ots->indirect_base); + allocated_regs, 0, + ots->indirect_base); } tcg_out_mov(s, otype, ots->reg, ts->reg); } @@ -3197,7 +3256,7 @@ static void tcg_reg_alloc_op(TCGContext *s, const TCGOp *op) /* allocate a new register matching the constraint and move the temporary register into it */ reg = tcg_reg_alloc(s, arg_ct->u.regs, i_allocated_regs, - ts->indirect_base); + 0, ts->indirect_base); tcg_out_mov(s, ts->type, reg, ts->reg); } new_args[i] = reg; @@ -3242,7 +3301,7 @@ static void tcg_reg_alloc_op(TCGContext *s, const TCGOp *op) } else if (arg_ct->ct & TCG_CT_NEWREG) { reg = tcg_reg_alloc(s, arg_ct->u.regs, i_allocated_regs | o_allocated_regs, - ts->indirect_base); + 0, ts->indirect_base); } else { /* if fixed register, we try to use it */ reg = ts->reg; @@ -3251,7 +3310,7 @@ static void tcg_reg_alloc_op(TCGContext *s, const TCGOp *op) goto oarg_end; } reg = tcg_reg_alloc(s, arg_ct->u.regs, o_allocated_regs, - ts->indirect_base); + 0, ts->indirect_base); } tcg_regset_set_reg(o_allocated_regs, reg); /* if a fixed register is used, then a move will be done afterwards */