From patchwork Tue Jan 17 23:10:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 643417 Delivered-To: patch@linaro.org Received: by 2002:a17:522:b9de:b0:4b9:b062:db3b with SMTP id fj30csp1443127pvb; Tue, 17 Jan 2023 15:12:15 -0800 (PST) X-Google-Smtp-Source: AMrXdXvd22LI/zuGpIVr0X9XlWJsEQ2e/pV9t1sGd8QFkqRHtf6OUP4OYGTAj7Z/3Jri5ZC/52qI X-Received: by 2002:a05:622a:5c8c:b0:3a9:7e3f:2646 with SMTP id ge12-20020a05622a5c8c00b003a97e3f2646mr7264667qtb.11.1673997135312; Tue, 17 Jan 2023 15:12:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673997135; cv=none; d=google.com; s=arc-20160816; b=q7b4Uuh4M96VgajD1l/ZLFF+BjkqiIJ40ogRUo6crP5c+oQfkushwIoNVWTbP/uts4 9yraQCEJYKec9bRlrDyGV8imqQ82MVcZadKzu4H4tbQXAdHMtvSCPwsAtVWj3MK1NErM MY0HRm795YJJjiZ9SzB1/jtZmttBbMWrtJ8f8ShpQVCucuIp5zdKzgYTzcsZC8lcddmD Q7NBpT/yOfddledotDlI8qAS/bI/b7Y8v9BQM1ycOmyHuZ9TncIsHf/9V3JHugezYjF9 61i9HTkz2uLRh4LzZbcKqpC47iKcDzdfgjS2QtIyWWxfqsoJjZTv/P3nx5nIPXIMhz6O UM6Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=ZuZ+Hlic4RgbTEoQoC4Ww4XDg1V1LGDarx8WVkmv+vY=; b=LULsg1fZa6D+TdV1yzxBN9ypg7OKd0Up91kpNlyirPvJZ2WRGgha+lyCMepdFpBrct Q2zYFkdAQX+b2yTNCYq8OvDhImAvAeELhrsMBGL3VwyuslClQJoxUOXPKMIAtoOipN5J v1f1kQr8GmVAeGK0GX4ChBJXz9BQeI39W9tHE5GikQjgHVV0T9ubIj9eqA/wGgDlgiQL 53n0p/NYeNuaWDO9TilqSPL6LZ9oIAscs5MKhSeqonKvRM/qqpFkRHn4BjZPPrwH2zBh ujoJrt6m2DcRSfmhc51AmZBycwYbrvBijLLUj8CTRbg0dWoa42kBP7jEwy0HcGEMmxEs QUkQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=xG07Td2X; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id fb18-20020a05622a481200b003a4f3f7927bsi833567qtb.335.2023.01.17.15.12.15 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 17 Jan 2023 15:12:15 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=xG07Td2X; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pHv79-0003yn-3n; Tue, 17 Jan 2023 18:11:35 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pHv74-0003w1-AU for qemu-devel@nongnu.org; Tue, 17 Jan 2023 18:11:30 -0500 Received: from mail-pg1-x52f.google.com ([2607:f8b0:4864:20::52f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pHv72-0001Tf-LB for qemu-devel@nongnu.org; Tue, 17 Jan 2023 18:11:30 -0500 Received: by mail-pg1-x52f.google.com with SMTP id f3so23199108pgc.2 for ; Tue, 17 Jan 2023 15:11:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ZuZ+Hlic4RgbTEoQoC4Ww4XDg1V1LGDarx8WVkmv+vY=; b=xG07Td2XBomHO6kcHPNpLLrfd4zCdS4qaPIhOeqBRSLWDq8t8jmWuyq0EwRIoHSPxX 1eaWzYB2+iQlZnMzx8K5VTC7IJCI1kYFcXIJZYrwrLbB6YEJUXAUlTBLqU8Uejqf8w2r fo5OoP5MHQZqQvjATzFK03H8xnia0HnzC1YG7pXuRe/YhEqMFLPL/vhB6Gu0a9M+n5PI fP+I716/VoGX2srxsOPo1MBmHl+Bkk0fDUQdqTIlM7HDryBHFeILM9Ta9LfhEhEguRwU 0dDfffrFHiQb5Plm2L8AJCrl8cqcCAT4OdOoZBWKeuE7UzSJ7jqRRFBi0ucXoynLBXO0 cEMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZuZ+Hlic4RgbTEoQoC4Ww4XDg1V1LGDarx8WVkmv+vY=; b=H7nedWwhXNUOyKNWW3psZjBJ9SV51vArjyS7dOW+fjHfd2r4g3KpkZDDd0+QjUY6Kg dq2cIlG+5Ix28EemFTW/ShuIzihuvLmkRfbCfhkPBvHmt0jhmit0vCRPIyc4yh/0ToYh nkKXx2JvAnbyfHmdVcGhCyV39IHxvYMZ8Wl6hsqSCAfNDA0139vOizBvFhPOjVO9f3Ga r6eC/S7w+qzSmFnjSDzGXq07M/9RA/Muyyt+5SLi8EcaFdLnFI2gBNqCoA5buM+vpS56 PpBdcjjgsdfGPK6dT58qClMxWeTUqZ0T1JcRco5VP01F08M9Y5Sw3yQ+f71WnHhCTO2S JN8g== X-Gm-Message-State: AFqh2koCSJZhPVPP/LZssH4E2f9Fy/av8EE/sHqPRRjkJQGorqYj8aS/ HzgfnsBIHzxPcC7/Xoluwg2rlR9D0PJ9ZpiT X-Received: by 2002:a62:4e8e:0:b0:580:fb8e:3044 with SMTP id c136-20020a624e8e000000b00580fb8e3044mr5846864pfb.22.1673997087205; Tue, 17 Jan 2023 15:11:27 -0800 (PST) Received: from stoup.. (rrcs-173-198-77-218.west.biz.rr.com. [173.198.77.218]) by smtp.gmail.com with ESMTPSA id b193-20020a621bca000000b005810c4286d6sm20747070pfb.0.2023.01.17.15.11.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Jan 2023 15:11:26 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, =?utf-8?q?Alex_Benn=C3=A9e?= Subject: [PULL 20/22] tcg/arm: Implement direct branch for goto_tb Date: Tue, 17 Jan 2023 13:10:49 -1000 Message-Id: <20230117231051.354444-21-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230117231051.354444-1-richard.henderson@linaro.org> References: <20230117231051.354444-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::52f; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x52f.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Now that tcg can handle direct and indirect goto_tb simultaneously, we can optimistically leave space for a direct branch and fall back to loading the pointer from the TB for an indirect branch. Reviewed-by: Alex Bennée Signed-off-by: Richard Henderson --- tcg/arm/tcg-target.c.inc | 52 ++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/tcg/arm/tcg-target.c.inc b/tcg/arm/tcg-target.c.inc index e1e1c2620d..6abe94137e 100644 --- a/tcg/arm/tcg-target.c.inc +++ b/tcg/arm/tcg-target.c.inc @@ -135,6 +135,8 @@ typedef enum { ARITH_BIC = 0xe << 21, ARITH_MVN = 0xf << 21, + INSN_B = 0x0a000000, + INSN_CLZ = 0x016f0f10, INSN_RBIT = 0x06ff0f30, @@ -546,7 +548,7 @@ static bool tcg_target_const_match(int64_t val, TCGType type, int ct) static void tcg_out_b_imm(TCGContext *s, ARMCond cond, int32_t offset) { - tcg_out32(s, (cond << 28) | 0x0a000000 | + tcg_out32(s, (cond << 28) | INSN_B | (((offset - 8) >> 2) & 0x00ffffff)); } @@ -1941,32 +1943,52 @@ static void tcg_out_exit_tb(TCGContext *s, uintptr_t arg) static void tcg_out_goto_tb(TCGContext *s, int which) { - /* Indirect jump method */ - intptr_t ptr, dif, dil; - TCGReg base = TCG_REG_PC; + uintptr_t i_addr; + intptr_t i_disp; - ptr = get_jmp_target_addr(s, which); - dif = tcg_pcrel_diff(s, (void *)ptr) - 8; - dil = sextract32(dif, 0, 12); - if (dif != dil) { + /* Direct branch will be patched by tb_target_set_jmp_target. */ + set_jmp_insn_offset(s, which); + tcg_out32(s, INSN_NOP); + + /* When branch is out of range, fall through to indirect. */ + i_addr = get_jmp_target_addr(s, which); + i_disp = tcg_pcrel_diff(s, (void *)i_addr) - 8; + tcg_debug_assert(i_disp < 0); + if (i_disp >= -0xfff) { + tcg_out_ld32_12(s, COND_AL, TCG_REG_PC, TCG_REG_PC, i_disp); + } else { /* * The TB is close, but outside the 12 bits addressable by * the load. We can extend this to 20 bits with a sub of a - * shifted immediate from pc. In the vastly unlikely event - * the code requires more than 1MB, we'll use 2 insns and - * be no worse off. + * shifted immediate from pc. */ - base = TCG_REG_R0; - tcg_out_movi32(s, COND_AL, base, ptr - dil); + int h = -i_disp; + int l = h & 0xfff; + + h = encode_imm_nofail(h - l); + tcg_out_dat_imm(s, COND_AL, ARITH_SUB, TCG_REG_R0, TCG_REG_PC, h); + tcg_out_ld32_12(s, COND_AL, TCG_REG_PC, TCG_REG_R0, l); } - tcg_out_ld32_12(s, COND_AL, TCG_REG_PC, base, dil); set_jmp_reset_offset(s, which); } void tb_target_set_jmp_target(const TranslationBlock *tb, int n, uintptr_t jmp_rx, uintptr_t jmp_rw) { - /* Always indirect, nothing to do */ + uintptr_t addr = tb->jmp_target_addr[n]; + ptrdiff_t offset = addr - (jmp_rx + 8); + tcg_insn_unit insn; + + /* Either directly branch, or fall through to indirect branch. */ + if (offset == sextract64(offset, 0, 26)) { + /* B */ + insn = deposit32((COND_AL << 28) | INSN_B, 0, 24, offset >> 2); + } else { + insn = INSN_NOP; + } + + qatomic_set((uint32_t *)jmp_rw, insn); + flush_idcache_range(jmp_rx, jmp_rw, 4); } static void tcg_out_op(TCGContext *s, TCGOpcode opc,