From patchwork Wed Nov 28 05:38:23 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 152181 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp686405ljp; Tue, 27 Nov 2018 21:39:20 -0800 (PST) X-Google-Smtp-Source: AFSGD/XcEEGSFmamxojR8/5CjzPVxWNF7oonyeNYRJKykBujpmphpTPnt5edfCDHiaT8mi03CMKe X-Received: by 2002:a81:78cc:: with SMTP id t195mr23279566ywc.387.1543383560596; Tue, 27 Nov 2018 21:39:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543383560; cv=none; d=google.com; s=arc-20160816; b=w6LrxqZJPm5JX/TWmfYEpWaSEUlzoD4S4ckpwmE80MCF8dIrVqgF/0lGAK9yPvDVfV bXEg0CR16InKMAvH0ozntFl5YI5DNtDLPR66LtzkvnPFgl0aFRVZ2kv218kkSGzBdQv/ tMOd0sVgXtewF2t3p3qruhXCZsBUvnpelOWXqmFVenebcL6HqSd+X1nqODMHc6kUGgE7 0pgbRaRHsichVFRVTZFoE0E0mQISjkKp6FroscT172qzzyBKnGjZN+YZGKq87Jkxeb3N GKH2EzxzZT481Wram5ihIzaH0U6AxQnf9n+7BCjPGVSJ1skZxN1Qwzpv+ZENUBuhAn11 CniQ== 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=xjZ2g0RlKwtygAl8ZD7KX634Dk8TC9PWLwdw3xE4psE=; b=Zx0VHomPambpIXZNOf/Q1oyS2arOVObHFzmaF31rqFjAJ3OxQmTq2p0BfO7uYkacbe GPUSQoNZ5acA7AM9Ln31z8GM/+N6icL4VI5N863zYKThu4nCauZCo55KrVOflk9jONBw pqHpAfK5K3xwKs9BB5a6URsQ0a5K6R+9AJ6EVzTMCdPrfy4ObBtIN1AeIO+N8iO2Lohy 4KLO8aCGOoCnsMfWXJ5Cu8PtFkdmp0qRV5l2qrDLiNwD2tiIhqCODRb43Nb0Dixumx8a 7fnc2ZIxyNp/F+w5k6Vq7b3X4QGTqjaGJ+PPFnR8Slpq5Exc0ujEDAbjyjGV2I8zg2eu UW1A== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=jWtTukTk; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 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. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id 200si4356754ywr.313.2018.11.27.21.39.20 for (version=TLS1 cipher=AES128-SHA bits=128/128); Tue, 27 Nov 2018 21:39:20 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=jWtTukTk; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 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 ([::1]:46028 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gRsZD-0003oH-SM for patch@linaro.org; Wed, 28 Nov 2018 00:39:19 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47819) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gRsYd-0003kh-94 for qemu-devel@nongnu.org; Wed, 28 Nov 2018 00:38:44 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gRsYZ-0004IU-Co for qemu-devel@nongnu.org; Wed, 28 Nov 2018 00:38:42 -0500 Received: from mail-pf1-x434.google.com ([2607:f8b0:4864:20::434]:47085) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gRsYZ-0004He-2r for qemu-devel@nongnu.org; Wed, 28 Nov 2018 00:38:39 -0500 Received: by mail-pf1-x434.google.com with SMTP id c73so9591137pfe.13 for ; Tue, 27 Nov 2018 21:38:39 -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=xjZ2g0RlKwtygAl8ZD7KX634Dk8TC9PWLwdw3xE4psE=; b=jWtTukTkyirF/XbueAf983CBMS2W3sbKxEcxaOUzGhaRfkbZHccrKW0OW7ZDFq7K+8 MzDW/aYQa/55YgDnhCC7jcC+MrihiKEtAbidIiPrUGnh5UYjlXqBViiuMKCCz9q2vEe4 oAbGTI35pJSPueJapmf42pp81pSbZlCfeYHCI= 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=xjZ2g0RlKwtygAl8ZD7KX634Dk8TC9PWLwdw3xE4psE=; b=WgCCc20l6cpW+BbuYHlPBA20RaRyAgofhkQcG4mYHWuphHw9D+oEfHo7OHH+kk3fZt BiGYn0q9v4F5sJrves6v9jy5zF5mKV3rbSdryS7tbcj20Y7I289i0M6SBGjkasK5TfaR nh8HHjY5mdfsf1SDATlyL549bLtzfoAvFFg9QJsxYWWKtb5y4ICKEeP+Ycd5rGUbMYSq gSSWZNCXR0TGNbo8iKwBH93lEy0rlTDupZpKH0iJtS3S5Wmp2Kvw0r6b4XavjutWA4y+ FjoGjk7HTuDDW20APn1gCC8pnAublbLBEQ4T0T0loipA07PpaFN0NFEUovEz9kl4pLBx Pd9g== X-Gm-Message-State: AA+aEWZgPAxFO6Org5FCfSZgYqNy8tAHO7pbpjKJILqSFB61xuOtaC/k E6LGZk7NcauiwdFbRws7qRkKJJ5mOaI= X-Received: by 2002:a63:2bc4:: with SMTP id r187mr13226325pgr.306.1543383517679; Tue, 27 Nov 2018 21:38:37 -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.36 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 27 Nov 2018 21:38:36 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Date: Tue, 27 Nov 2018 21:38:23 -0800 Message-Id: <20181128053834.10861-2-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::434 Subject: [Qemu-devel] [PATCH 01/12] 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: cota@braap.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. 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 8734389ba9..c596277fd0 100644 --- a/tcg/tcg.c +++ b/tcg/tcg.c @@ -1907,6 +1907,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]; @@ -1922,6 +1936,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) { @@ -2924,31 +2939,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; + } + } } } @@ -2966,12 +3022,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; @@ -3131,7 +3189,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); } @@ -3219,7 +3278,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; @@ -3264,7 +3323,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; @@ -3273,7 +3332,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 */