[llvm] [BOLT][NFC] Extract a function for dump MCInst (PR #67225)

via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 21 03:26:46 PST 2023


https://github.com/llongint updated https://github.com/llvm/llvm-project/pull/67225

>From 08ac183ccd27592ebc7bb2f29714232423619163 Mon Sep 17 00:00:00 2001
From: hezuoqiang <hezuoqiang2 at huawei.com>
Date: Sat, 23 Sep 2023 13:23:16 +0800
Subject: [PATCH] [BOLT][NFC] Extract a function for dump MCInst.

Extract a function for printing MCInst, so we can call it in GDB to get
a more intuitive assembly representation.
---
 bolt/include/bolt/Core/BinaryContext.h    |  3 +++
 bolt/lib/Core/BinaryContext.cpp           |  9 +++++++++
 bolt/lib/Passes/ValidateInternalCalls.cpp | 10 ++++------
 3 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/bolt/include/bolt/Core/BinaryContext.h b/bolt/include/bolt/Core/BinaryContext.h
index a4e84cb93c093dc..ad1bf2baaeb5b1e 100644
--- a/bolt/include/bolt/Core/BinaryContext.h
+++ b/bolt/include/bolt/Core/BinaryContext.h
@@ -1290,6 +1290,9 @@ class BinaryContext {
   /// Return true if the function should be emitted to the output file.
   bool shouldEmit(const BinaryFunction &Function) const;
 
+  /// Dump the assembly representation of MCInst to debug output.
+  void dump(const MCInst &Inst) const;
+
   /// Print the string name for a CFI operation.
   static void printCFI(raw_ostream &OS, const MCCFIInstruction &Inst);
 
diff --git a/bolt/lib/Core/BinaryContext.cpp b/bolt/lib/Core/BinaryContext.cpp
index cd70bdb7a4228d0..06b68765909d20e 100644
--- a/bolt/lib/Core/BinaryContext.cpp
+++ b/bolt/lib/Core/BinaryContext.cpp
@@ -1738,6 +1738,15 @@ bool BinaryContext::shouldEmit(const BinaryFunction &Function) const {
   return HasRelocations || Function.isSimple();
 }
 
+void BinaryContext::dump(const MCInst &Inst) const {
+  if (LLVM_UNLIKELY(!InstPrinter)) {
+    dbgs() << "Cannot dump for InstPrinter is not initialized.\n";
+    return;
+  }
+  InstPrinter->printInst(&Inst, 0, "", *STI, dbgs());
+  dbgs() << "\n";
+}
+
 void BinaryContext::printCFI(raw_ostream &OS, const MCCFIInstruction &Inst) {
   uint32_t Operation = Inst.getOperation();
   switch (Operation) {
diff --git a/bolt/lib/Passes/ValidateInternalCalls.cpp b/bolt/lib/Passes/ValidateInternalCalls.cpp
index 22dadf4f6403be3..516f91acb508441 100644
--- a/bolt/lib/Passes/ValidateInternalCalls.cpp
+++ b/bolt/lib/Passes/ValidateInternalCalls.cpp
@@ -281,18 +281,16 @@ bool ValidateInternalCalls::analyzeFunction(BinaryFunction &Function) const {
           LLVM_DEBUG({
             dbgs() << "Detected out-of-range PIC reference in " << Function
                    << "\nReturn address load: ";
-            BC.InstPrinter->printInst(TargetInst, 0, "", *BC.STI, dbgs());
-            dbgs() << "\nUse: ";
-            BC.InstPrinter->printInst(&Use, 0, "", *BC.STI, dbgs());
-            dbgs() << "\n";
+            BC.dump(*TargetInst);
+            dbgs() << "Use: ";
+            BC.dump(Use);
             Function.dump();
           });
           return false;
         }
         LLVM_DEBUG({
           dbgs() << "Validated access: ";
-          BC.InstPrinter->printInst(&Use, 0, "", *BC.STI, dbgs());
-          dbgs() << "\n";
+          BC.dump(Use);
         });
       }
       if (!UseDetected) {



More information about the llvm-commits mailing list