Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disassembler: Core Optimization 1-1 (Hash table and Caching) #88

Open
wants to merge 3 commits into
base: riscv-dis-opt1-req-reorganize
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
RISC-V: Fallback on faster hash table
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.
  • Loading branch information
a4lg committed Oct 19, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 6b3d0679e1ed92dd5302c755cc8905eeff63fad7
31 changes: 26 additions & 5 deletions opcodes/riscv-dis.c
Original file line number Diff line number Diff line change
@@ -912,6 +912,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
@@ -942,15 +945,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;
@@ -997,8 +1010,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;