[llvm] [RISCV] Try to disassemble 48-bit and larger instructions as 32-bit instructions first. (PR #175122)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 8 21:58:19 PST 2026


https://github.com/topperc created https://github.com/llvm/llvm-project/pull/175122

A vendor extension might have used the reserved opcodes, try to disassemble them first.

Should help with #174571.

CC: @abel-bernabeu

>From f9ece797f04ae24e52c57a5fe43ad45e2e46ee36 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Thu, 8 Jan 2026 21:55:08 -0800
Subject: [PATCH] [RISCV] Try to disassemble 48-bit and larger instructions as
 32-bit instructions first.

A vendor extension might have used the reserved opcodes, try to
disassemble them first.

Should help with #174571.
---
 .../Target/RISCV/Disassembler/RISCVDisassembler.cpp | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Target/RISCV/Disassembler/RISCVDisassembler.cpp b/llvm/lib/Target/RISCV/Disassembler/RISCVDisassembler.cpp
index 4bea4c48dddd1..c28e5c6f1e4a9 100644
--- a/llvm/lib/Target/RISCV/Disassembler/RISCVDisassembler.cpp
+++ b/llvm/lib/Target/RISCV/Disassembler/RISCVDisassembler.cpp
@@ -831,15 +831,18 @@ DecodeStatus RISCVDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
   if ((Bytes[0] & 0b11) != 0b11)
     return getInstruction16(MI, Size, Bytes, Address, CS);
 
-  // It's a 32 bit instruction if bit 1:0 are 0b11(checked above) and bits 4:2
-  // are not 0b111.
+  // Try to decode as a 32-bit instruction first.
+  DecodeStatus Result = getInstruction32(MI, Size, Bytes, Address, CS);
+  if (Result != MCDisassembler::Fail)
+    return Result;
+
+  // If bits [4:2] are 0b111 this might be a 48-bit or larger instruction.
   if ((Bytes[0] & 0b1'1100) != 0b1'1100)
-    return getInstruction32(MI, Size, Bytes, Address, CS);
+    return MCDisassembler::Fail;
 
   // 48-bit instructions are encoded as 0bxx011111.
-  if ((Bytes[0] & 0b11'1111) == 0b01'1111) {
+  if ((Bytes[0] & 0b11'1111) == 0b01'1111)
     return getInstruction48(MI, Size, Bytes, Address, CS);
-  }
 
   // 64-bit instructions are encoded as 0x0111111.
   if ((Bytes[0] & 0b111'1111) == 0b011'1111) {



More information about the llvm-commits mailing list