From patchwork Mon Nov 28 04:46:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tsukasa OI X-Patchwork-Id: 26507 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp5434561wrr; Sun, 27 Nov 2022 20:53:17 -0800 (PST) X-Google-Smtp-Source: AA0mqf4PXgIKVRA1ED8Z+P/7h9nYK/lgtK/nkxzPeK/uoeBCQQuMk6fP1xvleXNFKSEdH5fOk95f X-Received: by 2002:a17:907:989a:b0:7c0:7bd1:6436 with SMTP id ja26-20020a170907989a00b007c07bd16436mr472462ejc.718.1669611197793; Sun, 27 Nov 2022 20:53:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669611197; cv=none; d=google.com; s=arc-20160816; b=MgFD+XW0ocS99iHx8SYSPj/q5dAZt5btWHEW7yEpKQ5t1sr0uWYrPXDCEIIq5hT/tT t0RP0lndERyQzvmO9rnvG32vfpVlrZZj9TtQNROM0riTkgvKvWtiTJcFkvASEpAffXfm 3vNb9XQOU97duhN/IMJfpX6wdibz+Rls+XBSU8hdkPzUfmw3Fl29Fj3zVrZ4lUEsr+EO vQsrj2arhjlf5qFjsh02Y3NKG34zn+XpLld733C47TExXW1g3YeqmfsknUzw7XSZwQY4 8WiGMNAWhWnjqOQQ10+Uvvn5lDjIYMA9zBHFniDR0PgOqyLIjErB9RQ5ncEmDz0Z05BN wmDw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from: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:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=zh9rrwnRKAtXrMQT/3bew/O1ECNls1asgUv3xr5yOXo=; b=fMsVIYcJ4TTZN4WumV7EkrXgGlwGMl5sdr/GN+73pNWU1n2MQPcMuoa+pbNAFHoi63 pH9/EL4AflwWIwNRfsXhmHwWbC2Y557ElHAyzSyU4g0KuaYE6HdpF5OLBz4hMU1fAW3s U/C7LzCBrOQFa0MKp0n8w0zh4oI5dYwnvBI7kmxVzNde9DM2d+qOKPOriFl/pCI1syGd vUvpq/oDs3nVGfS9/DAHYuNtNWdmY1mWd62nZO+RlzJCsyHvQW5sDUm+RiJV9gqt1k8I LpfBrgF4LFvo4mWp6q6X5a9lkstFk50UhZeUOmVdfRzlFpS2Fzc4rfMpRF/23gq/8voO pJoQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=c6enmDg6; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id qk12-20020a1709077f8c00b0078dfe6dc4d2si9436234ejc.33.2022.11.27.20.53.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Nov 2022 20:53:17 -0800 (PST) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=c6enmDg6; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A4294395B075 for ; Mon, 28 Nov 2022 04:48:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A4294395B075 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1669610925; bh=zh9rrwnRKAtXrMQT/3bew/O1ECNls1asgUv3xr5yOXo=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=c6enmDg6EcwdXFHLs1Cm2kanK2YsmMMaW2c0EGHKQglw0sIiT+x/Va88QJ/VOCmFz CMJEFq/DeALsBPZa8wyNB0mUG+fXli2UrZemzcqk5y8VTIQ2I56j2lzhfK2upjo5aS aSVliJ/Lqag2G3T9tLtk0clI1REyddefsMRZmIlI= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-sender-0.a4lg.com (mail-sender.a4lg.com [153.120.152.154]) by sourceware.org (Postfix) with ESMTPS id 223D7384F498 for ; Mon, 28 Nov 2022 04:46:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 223D7384F498 Received: from [127.0.0.1] (localhost [127.0.0.1]) by mail-sender-0.a4lg.com (Postfix) with ESMTPSA id 725F8300089; Mon, 28 Nov 2022 04:46:52 +0000 (UTC) To: Tsukasa OI , Nelson Chu , Kito Cheng , Palmer Dabbelt Cc: binutils@sourceware.org Subject: [PATCH v2 2/3] RISC-V: Fallback on faster hash table Date: Mon, 28 Nov 2022 04:46:21 +0000 Message-Id: <0cd0bbfa70f83db276facb842b9a7bca1aaa77a6.1669610780.git.research_trasio@irq.a4lg.com> In-Reply-To: References: Mime-Version: 1.0 X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tsukasa OI via Binutils From: Tsukasa OI Reply-To: Tsukasa OI Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org Sender: "Binutils" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749975387644190681?= X-GMAIL-MSGID: =?utf-8?q?1750714231443187198?= From: Tsukasa OI Although it does not have a problem on current GNU Binutils implementation, if the custom vendor implements an instruction which spans across multiple major opcodes (e.g. uses both CUSTOM_0 and CUSTOM_1 in a *single* custom instruction), the original assumption of the sorted hash table breaks. In this case, this commit enables the fallback mode to disable all optimizations except filtering macros out. Note that, if a such instruction (that disables this disassembler optimization) is upstreamed to Binutils, a separate solution will be required to avoid major performance degradation when such instruction is not used. The intent of this commit is to make a room for custom vendors to implement such instructions in *their* tree without causing disassembler problems. opcodes/ChangeLog: * riscv-dis.c (is_riscv_hash_fallback) New. (build_riscv_opcodes_hash_table): If an instruction spans across multiple major opcodes, enable fallback mode and disable sorting. (riscv_disassemble_insn): If the fallback mode is enabled, scan through all instructions instead of scanning only instruction entries matching the hash value. --- opcodes/riscv-dis.c | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/opcodes/riscv-dis.c b/opcodes/riscv-dis.c index 4267b3ccf88c..3f6cbf5a3680 100644 --- a/opcodes/riscv-dis.c +++ b/opcodes/riscv-dis.c @@ -838,6 +838,9 @@ print_insn_args (const char *oparg, insn_t l, bfd_vma pc, disassemble_info *info static const struct riscv_opcode **riscv_hash[OP_HASH_LEN + 1]; static const struct riscv_opcode **riscv_opcodes_sorted; +/* Whether the fallback should be used. */ +static bool is_riscv_hash_fallback = false; + /* Compare two riscv_opcode* objects to sort by hash index. */ static int @@ -868,15 +871,25 @@ build_riscv_opcodes_hash_table (void) /* Sort riscv_opcodes entry pointers (except macros). */ for (op = riscv_opcodes; op->name; op++) - if (op->pinfo != INSN_MACRO) + { + if (op->pinfo == INSN_MACRO) + continue; len++; + if (is_riscv_hash_fallback) + continue; + if (OP_HASH_IDX (op->match) < OP_MASK_OP2 + ? (op->mask & OP_MASK_OP2) != OP_MASK_OP2 + : (op->mask & OP_MASK_OP) != OP_MASK_OP) + is_riscv_hash_fallback = true; + } riscv_opcodes_sorted = xcalloc (len, sizeof (struct riscv_opcode *)); pop_end = riscv_opcodes_sorted; for (op = riscv_opcodes; op->name; op++) if (op->pinfo != INSN_MACRO) *pop_end++ = op; - qsort (riscv_opcodes_sorted, len, sizeof (struct riscv_opcode *), - compare_opcodes); + if (!is_riscv_hash_fallback) + qsort (riscv_opcodes_sorted, len, sizeof (struct riscv_opcode *), + compare_opcodes); /* Initialize faster hash table. */ pop = riscv_opcodes_sorted; @@ -922,8 +935,16 @@ riscv_disassemble_insn (bfd_vma memaddr, info->target2 = 0; matched_op = NULL; - pop = riscv_hash[OP_HASH_IDX (word)]; - pop_end = riscv_hash[OP_HASH_IDX (word) + 1]; + if (!is_riscv_hash_fallback) + { + pop = riscv_hash[OP_HASH_IDX (word)]; + pop_end = riscv_hash[OP_HASH_IDX (word) + 1]; + } + else + { + pop = riscv_hash[0]; + pop_end = riscv_hash[OP_HASH_LEN]; + } for (; pop != pop_end; pop++) { op = *pop;