[llvm] 179abb0 - [X86][Disassembler] Replace custom logger with LLVM_DEBUG

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 11 12:17:11 PST 2020


Author: Fangrui Song
Date: 2020-01-11T12:17:05-08:00
New Revision: 179abb091d8a1d67115d21b54001d10250756042

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

LOG: [X86][Disassembler] Replace custom logger with LLVM_DEBUG

llvm-objdump -d on clang is decreased from 7.8s to 7.4s.

The improvement is likely due to the elimination of logger setup and
dbgprintf(), which has a large overhead.

Added: 
    

Modified: 
    llvm/lib/Target/X86/Disassembler/X86Disassembler.cpp
    llvm/lib/Target/X86/Disassembler/X86DisassemblerDecoder.cpp
    llvm/lib/Target/X86/Disassembler/X86DisassemblerDecoder.h

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/X86/Disassembler/X86Disassembler.cpp b/llvm/lib/Target/X86/Disassembler/X86Disassembler.cpp
index ca1232648a79..ad3255b8a28b 100644
--- a/llvm/lib/Target/X86/Disassembler/X86Disassembler.cpp
+++ b/llvm/lib/Target/X86/Disassembler/X86Disassembler.cpp
@@ -169,27 +169,6 @@ X86GenericDisassembler::X86GenericDisassembler(
   llvm_unreachable("Invalid CPU mode");
 }
 
-/// A callback function that wraps the readByte method from Region.
-///
-/// @param Arg      - The generic callback parameter.  In this case, this should
-///                   be a pointer to a Region.
-/// @param Byte     - A pointer to the byte to be read.
-/// @param Address  - The address to be read.
-
-/// logger - a callback function that wraps the operator<< method from
-///   raw_ostream.
-///
-/// @param arg      - The generic callback parameter.  This should be a pointe
-///                   to a raw_ostream.
-/// @param log      - A string to be logged.  logger() adds a newline.
-static void logger(void* arg, const char* log) {
-  if (!arg)
-    return;
-
-  raw_ostream &vStream = *(static_cast<raw_ostream*>(arg));
-  vStream << log << "\n";
-}
-
 //
 // Public interface for the disassembler
 //
@@ -201,14 +180,10 @@ MCDisassembler::DecodeStatus X86GenericDisassembler::getInstruction(
 
   InternalInstruction InternalInstr;
 
-  dlog_t LoggerFn = logger;
-  if (&VStream == &nulls())
-    LoggerFn = nullptr; // Disable logging completely if it's going to nulls().
-
   std::pair<ArrayRef<uint8_t>, uint64_t> R(Bytes, Address);
 
-  int Ret = decodeInstruction(&InternalInstr, &R, LoggerFn, (void *)&VStream,
-                              (const void *)MII.get(), Address, fMode);
+  int Ret = decodeInstruction(&InternalInstr, &R, (const void *)MII.get(),
+                              Address, fMode);
 
   if (Ret) {
     Size = InternalInstr.readerCursor - Address;

diff  --git a/llvm/lib/Target/X86/Disassembler/X86DisassemblerDecoder.cpp b/llvm/lib/Target/X86/Disassembler/X86DisassemblerDecoder.cpp
index f7d7ebf1a6c4..cf0f474eea04 100644
--- a/llvm/lib/Target/X86/Disassembler/X86DisassemblerDecoder.cpp
+++ b/llvm/lib/Target/X86/Disassembler/X86DisassemblerDecoder.cpp
@@ -15,6 +15,8 @@
 #include "X86DisassemblerDecoder.h"
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Debug.h"
+#include "llvm/Support/raw_ostream.h"
 
 #include <cstdarg> /* for va_*()       */
 #include <cstdio>  /* for vsnprintf()  */
@@ -23,6 +25,8 @@
 
 using namespace llvm::X86Disassembler;
 
+#define DEBUG_TYPE "x86-disassembler"
+
 /// Specifies whether a ModR/M byte is needed and (if so) which
 /// instruction each possible value of the ModR/M byte corresponds to.  Once
 /// this information is known, we have narrowed down to a single instruction.
@@ -231,17 +235,14 @@ static bool consume(InternalInstruction *insn, T &ptr) {
 static void dbgprintf(struct InternalInstruction* insn,
                       const char* format,
                       ...) {
-  char buffer[256];
-  va_list ap;
-
-  if (!insn->dlog)
-    return;
-
-  va_start(ap, format);
-  (void)vsnprintf(buffer, sizeof(buffer), format, ap);
-  va_end(ap);
-
-  insn->dlog(insn->dlogArg, buffer);
+  LLVM_DEBUG({
+    char buffer[256];
+    va_list ap;
+    va_start(ap, format);
+    (void)vsnprintf(buffer, sizeof(buffer), format, ap);
+    va_end(ap);
+    llvm::errs() << buffer;
+  });
 }
 
 static bool isREX(struct InternalInstruction *insn, uint8_t prefix) {
@@ -1815,10 +1816,6 @@ static int readOperands(struct InternalInstruction* insn) {
  * @param reader    - The function to be used to read the instruction's bytes.
  * @param readerArg - A generic argument to be passed to the reader to store
  *                    any internal state.
- * @param logger    - If non-NULL, the function to be used to write log messages
- *                    and warnings.
- * @param loggerArg - A generic argument to be passed to the logger to store
- *                    any internal state.
  * @param startLoc  - The address (in the reader's address space) of the first
  *                    byte in the instruction.
  * @param mode      - The mode (real mode, IA-32e, or IA-32e in 64-bit mode) to
@@ -1828,15 +1825,12 @@ static int readOperands(struct InternalInstruction* insn) {
  */
 int llvm::X86Disassembler::decodeInstruction(struct InternalInstruction *insn,
                                              const void *readerArg,
-                                             dlog_t logger, void *loggerArg,
                                              const void *miiArg,
                                              uint64_t startLoc,
                                              DisassemblerMode mode) {
   memset(insn, 0, sizeof(struct InternalInstruction));
 
   insn->readerArg = readerArg;
-  insn->dlog = logger;
-  insn->dlogArg = loggerArg;
   insn->startLocation = startLoc;
   insn->readerCursor = startLoc;
   insn->mode = mode;

diff  --git a/llvm/lib/Target/X86/Disassembler/X86DisassemblerDecoder.h b/llvm/lib/Target/X86/Disassembler/X86DisassemblerDecoder.h
index a79fcec48fc6..a1e036ca7ba1 100644
--- a/llvm/lib/Target/X86/Disassembler/X86DisassemblerDecoder.h
+++ b/llvm/lib/Target/X86/Disassembler/X86DisassemblerDecoder.h
@@ -534,11 +534,6 @@ struct InternalInstruction {
   // The address of the next byte to read via the reader
   uint64_t readerCursor;
 
-  // Logger interface (C)
-  dlog_t dlog;
-  // Opaque value passed to the logger
-  void* dlogArg;
-
   // General instruction information
 
   // The mode to disassemble for (64-bit, protected, real)
@@ -661,18 +656,12 @@ struct InternalInstruction {
 ///                  consumer.
 /// \param readerArg An argument to pass to the reader for storing context
 ///                  specific to the consumer.  May be NULL.
-/// \param logger    The dlog_t to be used in printing status messages from the
-///                  disassembler.  May be NULL.
-/// \param loggerArg An argument to pass to the logger for storing context
-///                  specific to the logger.  May be NULL.
 /// \param startLoc  The address (in the reader's address space) of the first
 ///                  byte in the instruction.
 /// \param mode      The mode (16-bit, 32-bit, 64-bit) to decode in.
 /// \return          Nonzero if there was an error during decode, 0 otherwise.
 int decodeInstruction(InternalInstruction *insn,
                       const void *readerArg,
-                      dlog_t logger,
-                      void *loggerArg,
                       const void *miiArg,
                       uint64_t startLoc,
                       DisassemblerMode mode);


        


More information about the llvm-commits mailing list