[llvm] c7449c1 - [RISCV] Refactor RISCVDisassembler::getInstruction to remove repeated code. NFC

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 6 21:24:14 PST 2023


Author: Craig Topper
Date: 2023-02-06T21:23:21-08:00
New Revision: c7449c1770eef0345c32e2b6ad5344e9a06ed2c5

URL: https://github.com/llvm/llvm-project/commit/c7449c1770eef0345c32e2b6ad5344e9a06ed2c5
DIFF: https://github.com/llvm/llvm-project/commit/c7449c1770eef0345c32e2b6ad5344e9a06ed2c5.diff

LOG: [RISCV] Refactor RISCVDisassembler::getInstruction to remove repeated code. NFC

For 4 byte instructions we were always setting size to 4 eventually. Same
for 2 byte instructions. So do it as soon as we know the from the opcode.

Add a return to the end of the 4 byte code so we don't have to have an else
around the 2 byte code.

Differential Revision: https://reviews.llvm.org/D143445

Added: 
    

Modified: 
    llvm/lib/Target/RISCV/Disassembler/RISCVDisassembler.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/RISCV/Disassembler/RISCVDisassembler.cpp b/llvm/lib/Target/RISCV/Disassembler/RISCVDisassembler.cpp
index ae1a90022a1a5..17a8d4806d98a 100644
--- a/llvm/lib/Target/RISCV/Disassembler/RISCVDisassembler.cpp
+++ b/llvm/lib/Target/RISCV/Disassembler/RISCVDisassembler.cpp
@@ -444,74 +444,64 @@ DecodeStatus RISCVDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
       Size = 0;
       return MCDisassembler::Fail;
     }
+    Size = 4;
+
     Insn = support::endian::read32le(Bytes.data());
+
     if (STI.getFeatureBits()[RISCV::FeatureStdExtZdinx] &&
         !STI.getFeatureBits()[RISCV::Feature64Bit]) {
       LLVM_DEBUG(dbgs() << "Trying RV32Zdinx table (Double in Integer and"
                            "rv32)\n");
       Result = decodeInstruction(DecoderTableRV32Zdinx32, MI, Insn, Address,
                                  this, STI);
-      if (Result != MCDisassembler::Fail) {
-        Size = 4;
+      if (Result != MCDisassembler::Fail)
         return Result;
-      }
     }
-
     if (STI.getFeatureBits()[RISCV::FeatureStdExtZfinx]) {
       LLVM_DEBUG(dbgs() << "Trying RVZfinx table (Float in Integer):\n");
       Result = decodeInstruction(DecoderTableRVZfinx32, MI, Insn, Address, this,
                                  STI);
-      if (Result != MCDisassembler::Fail) {
-        Size = 4;
+      if (Result != MCDisassembler::Fail)
         return Result;
-      }
     }
     if (STI.getFeatureBits()[RISCV::FeatureVendorXVentanaCondOps]) {
       LLVM_DEBUG(dbgs() << "Trying Ventana custom opcode table:\n");
       Result = decodeInstruction(DecoderTableVentana32, MI, Insn, Address, this,
                                  STI);
-      if (Result != MCDisassembler::Fail) {
-        Size = 4;
+      if (Result != MCDisassembler::Fail)
         return Result;
-      }
     }
     if (STI.getFeatureBits()[RISCV::FeatureVendorXTHeadVdot]) {
       LLVM_DEBUG(dbgs() << "Trying XTHeadVdot custom opcode table:\n");
       Result =
           decodeInstruction(DecoderTableTHeadV32, MI, Insn, Address, this, STI);
-      if (Result != MCDisassembler::Fail) {
-        Size = 4;
+      if (Result != MCDisassembler::Fail)
         return Result;
-      }
     }
 
     LLVM_DEBUG(dbgs() << "Trying RISCV32 table :\n");
-    Result = decodeInstruction(DecoderTable32, MI, Insn, Address, this, STI);
-    Size = 4;
-  } else {
-    if (Bytes.size() < 2) {
-      Size = 0;
-      return MCDisassembler::Fail;
-    }
-    Insn = support::endian::read16le(Bytes.data());
+    return decodeInstruction(DecoderTable32, MI, Insn, Address, this, STI);
+  }
 
-    if (!STI.getFeatureBits()[RISCV::Feature64Bit]) {
-      LLVM_DEBUG(
-          dbgs() << "Trying RISCV32Only_16 table (16-bit Instruction):\n");
-      // Calling the auto-generated decoder function.
-      Result = decodeInstruction(DecoderTableRISCV32Only_16, MI, Insn, Address,
-                                 this, STI);
-      if (Result != MCDisassembler::Fail) {
-        Size = 2;
-        return Result;
-      }
-    }
+  if (Bytes.size() < 2) {
+    Size = 0;
+    return MCDisassembler::Fail;
+  }
+  Size = 2;
+
+  Insn = support::endian::read16le(Bytes.data());
 
-    LLVM_DEBUG(dbgs() << "Trying RISCV_C table (16-bit Instruction):\n");
+  if (!STI.getFeatureBits()[RISCV::Feature64Bit]) {
+    LLVM_DEBUG(
+        dbgs() << "Trying RISCV32Only_16 table (16-bit Instruction):\n");
     // Calling the auto-generated decoder function.
-    Result = decodeInstruction(DecoderTable16, MI, Insn, Address, this, STI);
-    Size = 2;
+    Result = decodeInstruction(DecoderTableRISCV32Only_16, MI, Insn, Address,
+                               this, STI);
+    if (Result != MCDisassembler::Fail)
+      return Result;
   }
 
-  return Result;
+  LLVM_DEBUG(dbgs() << "Trying RISCV_C table (16-bit Instruction):\n");
+  // Calling the auto-generated decoder function.
+  return decodeInstruction(DecoderTable16, MI, Insn, Address, this, STI);
 }


        


More information about the llvm-commits mailing list