From patchwork Fri Aug 4 05:44:22 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 9880591 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id DA4E960360 for ; Fri, 4 Aug 2017 05:54:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CFA2B289A6 for ; Fri, 4 Aug 2017 05:54:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C4495289B0; Fri, 4 Aug 2017 05:54:25 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id BC3D7289AB for ; Fri, 4 Aug 2017 05:54:24 +0000 (UTC) Received: from localhost ([::1]:36689 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ddVYY-0002Ou-RQ for patchwork-qemu-devel@patchwork.kernel.org; Fri, 04 Aug 2017 01:53:54 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59952) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ddVPm-00047i-OJ for qemu-devel@nongnu.org; Fri, 04 Aug 2017 01:44:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ddVPk-0003jj-P6 for qemu-devel@nongnu.org; Fri, 04 Aug 2017 01:44:50 -0400 Received: from mail-pg0-x244.google.com ([2607:f8b0:400e:c05::244]:36537) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ddVPk-0003ia-HQ for qemu-devel@nongnu.org; Fri, 04 Aug 2017 01:44:48 -0400 Received: by mail-pg0-x244.google.com with SMTP id y129so823862pgy.3 for ; Thu, 03 Aug 2017 22:44:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:subject:date:message-id:in-reply-to:references; bh=EjXelywn3eVixFOxqlv/9tQ6JkpZ2APpI4N6eCIqiL4=; b=OrxmNPa19iNpBDuHj9cI04oE+AbYPeQ6MA2PY+4XMt2R+EblL5oqhn2Qd6fhRM/2Oi jNsdH1zEkvz6mVF9TReleoMDeaVSxmtHBrPSAY6VQ2/HmjZjfAXR9Y1MUJ7Hmygai++B LxzXFjIyAAbeMljjssNlIleGezLD4VHYiFs7jEhJ3XwEOQzC7LV6wQ+VlifPGSM6pVWE SlTCZgGRrvD64OJKIji5cqCa1erHpse7fI3oPi6JlaB/7/ry5Pof0vABT5KoiRRYo+LK Ln86NaEmOe/YPUF18SfpYyrxYxikuP42I11+S9qOIhCUMqYRc9G3WNOekYTK4MXuw3SN aaeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:subject:date:message-id :in-reply-to:references; bh=EjXelywn3eVixFOxqlv/9tQ6JkpZ2APpI4N6eCIqiL4=; b=OvG1MP1ze+G3tXehRuqH4rA1xzojgowQ9LtUcNxfzSHSBw+Xppvp9wxsiBozs9GoE/ t7k8Aay/420qH6OTKid5rElJk3ybgjMEZgRcM/snWj0ktJvu9NBWzD9dXtr09cLnUiMS 7UxmixlCHPsNpMd3yEj9xsDT9d8KtJocjSWYowZe/Xt9llQ9/XK9JJ5ImTxh1wmBrXPY 4qTm7bmr5l7bTahBMEjEYd2ysw/18X2+1SoO9J0MjL/O1AOpKBVeNq18JgCrJkbc+oGg PlAsIBtG1P6CivkEsEBAJz76nETSLJwSupWdOz3wz0uYKOYAjvM26cAQ6wNB9OlXacGV Ko4Q== X-Gm-Message-State: AIVw112Ds/EsJMoZnntMckfK2sMiaBKWQ+99jSHIgWYOj1tllfmsJJPd NMZfmqHtHWzp/c7aU5Y= X-Received: by 10.84.171.195 with SMTP id l61mr1452675plb.464.1501825487368; Thu, 03 Aug 2017 22:44:47 -0700 (PDT) Received: from bigtime.twiddle.net (97-126-108-236.tukw.qwest.net. [97.126.108.236]) by smtp.gmail.com with ESMTPSA id o14sm1061063pfi.158.2017.08.03.22.44.46 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 03 Aug 2017 22:44:46 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Thu, 3 Aug 2017 22:44:22 -0700 Message-Id: <20170804054426.10590-20-rth@twiddle.net> X-Mailer: git-send-email 2.13.3 In-Reply-To: <20170804054426.10590-1-rth@twiddle.net> References: <20170804054426.10590-1-rth@twiddle.net> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c05::244 Subject: [Qemu-devel] [PATCH for-2.11 19/23] tcg/arm: Use constant pool for movi 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: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Signed-off-by: Richard Henderson --- tcg/arm/tcg-target.h | 1 + tcg/arm/tcg-target.inc.c | 92 ++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 75 insertions(+), 18 deletions(-) diff --git a/tcg/arm/tcg-target.h b/tcg/arm/tcg-target.h index 55de35a691..0f71a85a45 100644 --- a/tcg/arm/tcg-target.h +++ b/tcg/arm/tcg-target.h @@ -141,5 +141,6 @@ void tb_target_set_jmp_target(uintptr_t, uintptr_t, uintptr_t); #ifdef CONFIG_SOFTMMU #define TCG_TARGET_NEED_LDST_LABELS #endif +#define TCG_TARGET_NEED_POOL_LABELS #endif diff --git a/tcg/arm/tcg-target.inc.c b/tcg/arm/tcg-target.inc.c index 78603a19db..2736022d5a 100644 --- a/tcg/arm/tcg-target.inc.c +++ b/tcg/arm/tcg-target.inc.c @@ -23,6 +23,7 @@ */ #include "elf.h" +#include "tcg-pool.inc.c" int arm_arch = __ARM_ARCH; @@ -203,9 +204,39 @@ static inline void reloc_pc24_atomic(tcg_insn_unit *code_ptr, tcg_insn_unit *tar static void patch_reloc(tcg_insn_unit *code_ptr, int type, intptr_t value, intptr_t addend) { - tcg_debug_assert(type == R_ARM_PC24); tcg_debug_assert(addend == 0); - reloc_pc24(code_ptr, (tcg_insn_unit *)value); + + if (type == R_ARM_PC24) { + reloc_pc24(code_ptr, (tcg_insn_unit *)value); + } else if (type == R_ARM_PC13) { + intptr_t diff = value - (uintptr_t)(code_ptr + 2); + tcg_insn_unit insn = *code_ptr; + bool u; + + if (diff >= -0xfff && diff <= 0xfff) { + u = (diff >= 0); + if (!u) { + diff = -diff; + } + } else { + int rd = extract32(insn, 12, 4); + int rt = rd == TCG_REG_PC ? TCG_REG_TMP : rd; + assert(diff >= 0x1000 && diff < 0x100000); + /* add rt, pc, #high */ + *code_ptr++ = ((insn & 0xf0000000) | (1 << 25) | ARITH_ADD + | (TCG_REG_PC << 16) | (rt << 12) + | (20 << 7) | (diff >> 12)); + /* ldr rd, [rt, #low] */ + insn = deposit32(insn, 12, 4, rt); + diff &= 0xfff; + u = 1; + } + insn = deposit32(insn, 23, 1, u); + insn = deposit32(insn, 0, 12, diff); + *code_ptr = insn; + } else { + g_assert_not_reached(); + } } #define TCG_CT_CONST_ARM 0x100 @@ -581,9 +612,20 @@ static inline void tcg_out_ld8s_r(TCGContext *s, int cond, TCGReg rt, tcg_out_memop_r(s, cond, INSN_LDRSB_REG, rt, rn, rm, 1, 1, 0); } +static void tcg_out_movi_pool(TCGContext *s, int cond, int rd, uint32_t arg) +{ + /* The 12-bit range on the ldr insn is sometimes a bit too small. + In order to get around that we require two insns, one of which + will usually be a nop, but may be replaced in patch_reloc. */ + new_pool_label(s, arg, R_ARM_PC13, s->code_ptr, 0); + tcg_out_ld32_12(s, cond, rd, TCG_REG_PC, 0); + tcg_out_nop(s); +} + static void tcg_out_movi32(TCGContext *s, int cond, int rd, uint32_t arg) { - int rot, opc, rn, diff; + int rot, diff, opc, sh1, sh2; + uint32_t tt0, tt1, tt2; /* Check a single MOV/MVN before anything else. */ rot = encode_imm(arg); @@ -631,24 +673,30 @@ static void tcg_out_movi32(TCGContext *s, int cond, int rd, uint32_t arg) return; } - /* TODO: This is very suboptimal, we can easily have a constant - pool somewhere after all the instructions. */ + /* Look for sequences of two insns. If we have lots of 1's, we can + shorten the sequence by beginning with mvn and then clearing + higher bits with eor. */ + tt0 = arg; opc = ARITH_MOV; - rn = 0; - /* If we have lots of leading 1's, we can shorten the sequence by - beginning with mvn and then clearing higher bits with eor. */ - if (clz32(~arg) > clz32(arg)) { - opc = ARITH_MVN, arg = ~arg; + if (ctpop32(arg) > 16) { + tt0 = ~arg; + opc = ARITH_MVN; + } + sh1 = ctz32(tt0) & ~1; + tt1 = tt0 & ~(0xff << sh1); + sh2 = ctz32(tt1) & ~1; + tt2 = tt1 & ~(0xff << sh2); + if (tt2 == 0) { + rot = ((32 - sh1) << 7) & 0xf00; + tcg_out_dat_imm(s, cond, opc, rd, 0, ((tt0 >> sh1) & 0xff) | rot); + rot = ((32 - sh2) << 7) & 0xf00; + tcg_out_dat_imm(s, cond, ARITH_EOR, rd, rd, + ((tt0 >> sh2) & 0xff) | rot); + return; } - do { - int i = ctz32(arg) & ~1; - rot = ((32 - i) << 7) & 0xf00; - tcg_out_dat_imm(s, cond, opc, rd, rn, ((arg >> i) & 0xff) | rot); - arg &= ~(0xff << i); - opc = ARITH_EOR; - rn = rd; - } while (arg); + /* Otherwise, drop it into the constant pool. */ + tcg_out_movi_pool(s, cond, rd, arg); } static inline void tcg_out_dat_rI(TCGContext *s, int cond, int opc, TCGArg dst, @@ -2164,6 +2212,14 @@ static inline void tcg_out_movi(TCGContext *s, TCGType type, tcg_out_movi32(s, COND_AL, ret, arg); } +static void tcg_out_nop_fill(tcg_insn_unit *p, int count) +{ + int i; + for (i = 0; i < count; ++i) { + p[i] = INSN_NOP; + } +} + /* Compute frame size via macros, to share between tcg_target_qemu_prologue and tcg_register_jit. */