From patchwork Sat Oct 22 09:58:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 617498 Delivered-To: patch@linaro.org Received: by 2002:a17:522:c983:b0:460:3032:e3c4 with SMTP id kr3csp1530725pvb; Sat, 22 Oct 2022 03:18:04 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7pFX3L/cjjIZwvz/7avdcXCKFfdKZQ74OWZkPTnXslNrRz9NCx8djfCqMFy0WnwQUigNet X-Received: by 2002:ac8:7f4c:0:b0:39d:98e:c177 with SMTP id g12-20020ac87f4c000000b0039d098ec177mr12470682qtk.210.1666433884710; Sat, 22 Oct 2022 03:18:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666433884; cv=none; d=google.com; s=arc-20160816; b=ZdsR9IU+dnUhaAJA+8FOr7N6pp3JJnQ0PQq+3iANWbV4UlYPc9X6mwFUG4WHyfGCMS 712rJgPQzSzW47MSETIIdhYWlUttCByJOnDsMq3/PMTty75kSJd8XKyECMw0yibJWLD4 F70wGAWaoQETaCB6du2P2lnEE3WrXHm3ZAVWe2OcRwRc+2BkHKVBv5sQrSLRF/jk/U0O yTyfSnOnYfvCjGf6u2+MjUNSMS71KUG+ya3EgsPVUcVq95okB3dDW1IcemNajSwFyb9C Pv691s2/YefFzHEpNL3d8a8YKDnYMB2wt+S4SjeTPAMf6H1hPq5C+tZm9IC55SRcyrO3 k6vQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:sender:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature; bh=RU8/5HTo8+baKMeHrUEQiBWbmsr/936MisDwlU3iBGk=; b=t5pC4eaeJDPJvUMrlkRE/cRPBE4btyplmkXWVE+4yd1buQqVDe9rdgxQiy4Dnpww5I idcwLNmdOj4ZMQt5yq/E7OnPVh7thsL9qUKibIEH0Y3mdWpyq92lhbLdKV2VtVw1zRvj K4SR85tX61aWvtGhLNCG7waG4v3r3uzIFbFvwUp3CL6K0UONBydxBvCOcyQuTHsIHeci 27wvuX0P89BxvUMJy8+rbwL7mjHmeN/0E7QPDNvSDtya/KZjvZuo9oBlhg0tyIZZBpI4 mMHVQzs78KJokvVXeT9yIJHXCl92L8NaZO6yGT7jfEqbuRjsA8Hw5G7iGsTYZgmuXTRb fqwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=m+HclE7B; 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 e20-20020ac84914000000b0039abfb4cabdsi13274816qtq.748.2022.10.22.03.18.04 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sat, 22 Oct 2022 03:18:04 -0700 (PDT) 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=m+HclE7B; 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 1omBHj-0004aU-Qy; Sat, 22 Oct 2022 05:59:19 -0400 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 1omBHi-0004aM-F7 for qemu-devel@nongnu.org; Sat, 22 Oct 2022 05:59:18 -0400 Received: from mail-pg1-x533.google.com ([2607:f8b0:4864:20::533]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1omBHT-0008FD-7t for qemu-devel@nongnu.org; Sat, 22 Oct 2022 05:59:18 -0400 Received: by mail-pg1-x533.google.com with SMTP id f9so246483pgj.2 for ; Sat, 22 Oct 2022 02:59:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=RU8/5HTo8+baKMeHrUEQiBWbmsr/936MisDwlU3iBGk=; b=m+HclE7BndO+171z9rIM5f+KdAsuQQwcekZv5h0GAIUIYxV1BvJndK+zpQZPe4T7sa o4LdYXvgfBlEduVXcPXw8z0G7MAkDMTXkonGjlM1uL5YC1CmJ9GBnfx4Jsc8wjpCFeKh 5rZiy9WR0936WanKNzwHeudWvmwBdswwKVxnJhdB/HoNqIxjgfmP24lDPgrZxEIdmpM0 rvx7ZM32MWjhn/TgBFOEXr1BQZGYWPJ7/8WvX8AdWplTzcjPbRXllTATwrzhPAEEgvMa xeUomqcp10JpVUIXfygszLOXgpQWOFb1EoGEGhuSosK1+vQ0FUO68gtcCI+OVwc40TiH glIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=RU8/5HTo8+baKMeHrUEQiBWbmsr/936MisDwlU3iBGk=; b=m7QC7RQVdiCjum4nj/13IsaZQ4J1cmGFfbkfXvdECPJkqwedVuoKYIsgxeDTjOuYii ImChr1gdzAzljDZRV/AXj7iizUWreXTfsaRrtmK6tgAvP3JQ5nG/TAwuJV01p+fUbjTA kYgdcfJGi0fyVttD+G8lxo7qXCGigLcKgcnzhmFcvPNan38x/hw2BixQg5QTwHN2xd9W bl8Je9rmAjQHfFYHA1jo1VQ5P1fUy7xc/bcasnaro4T+DNJO+HtSeky++X+PKaWekN8Z eIzu3kDT/8ADUVRa3byOzH6ov1zk24LbRa+glpwDoSv0TBgnIydM+hGMyvOuKPnJaSbT tLeQ== X-Gm-Message-State: ACrzQf2ZrtTTsVslf9Gd1sPTKa/SSNSggB0uDQlssY8Yxg0SHMuxBS7u lLMHICmwXXkzpYuTX+TBbE7DgAI+uSOOJQo0 X-Received: by 2002:a05:6a00:174c:b0:565:c73a:9117 with SMTP id j12-20020a056a00174c00b00565c73a9117mr23288425pfc.23.1666432741344; Sat, 22 Oct 2022 02:59:01 -0700 (PDT) Received: from localhost.localdomain ([149.135.10.35]) by smtp.gmail.com with ESMTPSA id 129-20020a621987000000b0056b3c863950sm2021564pfz.8.2022.10.22.02.58.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 Oct 2022 02:59:00 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: Alistair.Francis@wdc.com, qemu-riscv@nongnu.org, palmer@dabbelt.com, LIU Zhiwei Subject: [PATCH] tcg/riscv: Fix range matched by TCG_CT_CONST_M12 Date: Sat, 22 Oct 2022 19:58:21 +1000 Message-Id: <20221022095821.2441874-1-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::533; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x533.google.com X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no 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: , Sender: "Qemu-devel" Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org We were matching a signed 13-bit range, not a 12-bit range. Expand the commentary within the function and be explicit about all of the ranges. Reported-by: LIU Zhiwei Signed-off-by: Richard Henderson Reviewed-by: LIU Zhiwei Reviewed-by: Alistair Francis --- tcg/riscv/tcg-target.c.inc | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/tcg/riscv/tcg-target.c.inc b/tcg/riscv/tcg-target.c.inc index 1cdaf7b57b..2a84c57bec 100644 --- a/tcg/riscv/tcg-target.c.inc +++ b/tcg/riscv/tcg-target.c.inc @@ -154,13 +154,26 @@ static bool tcg_target_const_match(int64_t val, TCGType type, int ct) if ((ct & TCG_CT_CONST_ZERO) && val == 0) { return 1; } - if ((ct & TCG_CT_CONST_S12) && val == sextreg(val, 0, 12)) { + /* + * Sign extended from 12 bits: [-0x800, 0x7ff]. + * Used for most arithmetic, as this is the isa field. + */ + if ((ct & TCG_CT_CONST_S12) && val >= -0x800 && val <= 0x7ff) { return 1; } - if ((ct & TCG_CT_CONST_N12) && -val == sextreg(-val, 0, 12)) { + /* + * Sign extended from 12 bits, negated: [-0x7ff, 0x800]. + * Used for subtraction, where a constant must be handled by ADDI. + */ + if ((ct & TCG_CT_CONST_N12) && val >= -0x7ff && val <= 0x800) { return 1; } - if ((ct & TCG_CT_CONST_M12) && val >= -0xfff && val <= 0xfff) { + /* + * Sign extended from 12 bits, +/- matching: [-0x7ff, 0x7ff]. + * Used by addsub2, which may need the negative operation, + * and requires the modified constant to be representable. + */ + if ((ct & TCG_CT_CONST_M12) && val >= -0x7ff && val <= 0x7ff) { return 1; } return 0;