From 36b0be1ec1558f88d4f031a1817c3a4c2cba07a6 Mon Sep 17 00:00:00 2001 From: Richard-Qin-X Date: Sat, 22 Aug 2026 18:59:22 +0800 Subject: [CodeGen] Match LLVM machine debug stripping --- backend/lib/CodeGen/CMakeLists.txt | 2 ++ backend/lib/CodeGen/MIRPrinter.cpp | 3 ++- backend/lib/CodeGen/MachineInstr.cpp | 16 +++++++++++++++ backend/lib/CodeGen/MachineStripDebug.cpp | 34 +++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 backend/lib/CodeGen/MachineStripDebug.cpp (limited to 'backend/lib/CodeGen') diff --git a/backend/lib/CodeGen/CMakeLists.txt b/backend/lib/CodeGen/CMakeLists.txt index 2fc7254..b95fe42 100644 --- a/backend/lib/CodeGen/CMakeLists.txt +++ b/backend/lib/CodeGen/CMakeLists.txt @@ -23,6 +23,7 @@ set( MachineCopyPropagation.cpp MachineCSE.cpp MachineDebugInfo.cpp + MachineStripDebug.cpp MachineDCE.cpp MachineFrameInfo.cpp MachineFinalization.cpp @@ -111,6 +112,7 @@ set( "${RCC_BACKEND_SOURCE_DIR}/include/rcc/CodeGen/MachineCopyPropagation.h" "${RCC_BACKEND_SOURCE_DIR}/include/rcc/CodeGen/MachineCSE.h" "${RCC_BACKEND_SOURCE_DIR}/include/rcc/CodeGen/MachineDebugInfo.h" + "${RCC_BACKEND_SOURCE_DIR}/include/rcc/CodeGen/MachineStripDebug.h" "${RCC_BACKEND_SOURCE_DIR}/include/rcc/CodeGen/MachineDCE.h" "${RCC_BACKEND_SOURCE_DIR}/include/rcc/CodeGen/MachineFrameInfo.h" "${RCC_BACKEND_SOURCE_DIR}/include/rcc/CodeGen/MachineFinalization.h" diff --git a/backend/lib/CodeGen/MIRPrinter.cpp b/backend/lib/CodeGen/MIRPrinter.cpp index 5eb244a..c684368 100644 --- a/backend/lib/CodeGen/MIRPrinter.cpp +++ b/backend/lib/CodeGen/MIRPrinter.cpp @@ -1935,7 +1935,8 @@ Expected BodyPrinter::printRegisterOperand(const MachineOperand &Operand, Output.append("internal "); if (Flags.has(RegisterOperandFlag::EarlyClobber)) Output.append("early-clobber "); - if (Flags.has(RegisterOperandFlag::Debug)) + if (Flags.has(RegisterOperandFlag::Debug) && + (Operand.parent() == nullptr || !Operand.parent()->isDebugInstr())) Output.append("debug-use "); if (Flags.has(RegisterOperandFlag::Renamable)) Output.append("renamable "); diff --git a/backend/lib/CodeGen/MachineInstr.cpp b/backend/lib/CodeGen/MachineInstr.cpp index f4602e0..55b4ce9 100644 --- a/backend/lib/CodeGen/MachineInstr.cpp +++ b/backend/lib/CodeGen/MachineInstr.cpp @@ -2,6 +2,7 @@ #include "rcc/CodeGen/MachineInstr.h" +#include "rcc/CodeGen/GenericOpcodes.h" #include "rcc/CodeGen/MachineBasicBlock.h" #include "rcc/CodeGen/MachineFunction.h" #include "rcc/CodeGen/MachineMemOperand.h" @@ -18,6 +19,14 @@ namespace rcc { +bool MachineInstr::isDebugInstr() const noexcept { + return InstructionOpcode == rcc::opcode(GenericOpcode::DBG_VALUE) || + InstructionOpcode == rcc::opcode(GenericOpcode::DBG_VALUE_LIST) || + InstructionOpcode == rcc::opcode(GenericOpcode::DBG_LABEL) || + InstructionOpcode == rcc::opcode(GenericOpcode::DBG_INSTR_REF) || + InstructionOpcode == rcc::opcode(GenericOpcode::DBG_PHI); +} + MachineInstr::MachineInstr(MachineFunction &OwnerFunction, Opcode InstructionOpcode, MachineInstrStableID StableID) : OwnerFunction(&OwnerFunction), InstructionOpcode(InstructionOpcode), StableID(StableID) {} @@ -97,6 +106,13 @@ Expected MachineInstr::insertOperand(std::size_t Index, MachineOperand &&O return makeUnexpected(ErrorCode::InvalidArgument, "register operand references an unknown virtual register"); } + if (Operand.isUse() && isDebugInstr()) { + RegisterOperandFlags RegisterFlags = Operand.registerFlags(); + RegisterFlags.set(RegisterOperandFlag::Debug); + auto Updated = Operand.setRegisterFlags(RegisterFlags); + if (!Updated) + return std::unexpected(Updated.error()); + } Operands.insert(Operands.begin() + static_cast(Index), std::move(Operand)); adjustTiesForInsertion(Index); diff --git a/backend/lib/CodeGen/MachineStripDebug.cpp b/backend/lib/CodeGen/MachineStripDebug.cpp new file mode 100644 index 0000000..4699ef0 --- /dev/null +++ b/backend/lib/CodeGen/MachineStripDebug.cpp @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "rcc/CodeGen/MachineStripDebug.h" + +#include "rcc/CodeGen/MachineBasicBlock.h" +#include "rcc/CodeGen/MachineFunction.h" +#include "rcc/CodeGen/MachineInstr.h" + +namespace rcc { + +Expected stripDebugMachineFunction(MachineFunction &Function) { + bool Changed = false; + for (MachineBasicBlock *Block = Function.firstBlock(); Block != nullptr; Block = Block->next()) { + for (MachineInstr *Instruction = Block->firstInstruction(); Instruction != nullptr;) { + MachineInstr *Next = Instruction->next(); + if (Instruction->isDebugInstr() && Instruction->operandCount() > 1) { + auto Removed = Block->removeInstruction(*Instruction); + if (!Removed) + return std::unexpected(Removed.error()); + Changed = true; + Instruction = Next; + continue; + } + if (Instruction->debugLocation()) { + Instruction->clearDebugLocation(); + Changed = true; + } + Instruction = Next; + } + } + return Changed; +} + +} // namespace rcc -- cgit v1.2.3