From f3e54f2f97daa135b99a47277625cc6e8391753e Mon Sep 17 00:00:00 2001 From: llongint Date: Tue, 21 Nov 2023 20:30:44 +0800 Subject: [PATCH] [BOLT][NFC] Extract a function for dump MCInst (#67225) In GDB debugging, obtaining the assembly representation of MCInst is more intuitive. --- 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 a4e84cb93c093d..ad1bf2baaeb5b1 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 cd70bdb7a4228d..06b68765909d20 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 22dadf4f6403be..516f91acb50844 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) {