From d54646c69a5117cbeef35f5c3f0d546f4c6bf0e7 Mon Sep 17 00:00:00 2001 From: Richard-Qin-X Date: Wed, 26 Aug 2026 00:50:21 +0800 Subject: [CodeGen] Implement reaching-def analysis printing --- backend/include/rcc/CodeGen/MIRPrinter.h | 13 ++++++ backend/include/rcc/CodeGen/ReachingDefAnalysis.h | 52 +++++++++++++++++++++++ backend/include/rcc/MIR/ReachingDefPrinter.h | 26 ++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 backend/include/rcc/CodeGen/ReachingDefAnalysis.h create mode 100644 backend/include/rcc/MIR/ReachingDefPrinter.h (limited to 'backend/include') diff --git a/backend/include/rcc/CodeGen/MIRPrinter.h b/backend/include/rcc/CodeGen/MIRPrinter.h index 4c20bed..ea58434 100644 --- a/backend/include/rcc/CodeGen/MIRPrinter.h +++ b/backend/include/rcc/CodeGen/MIRPrinter.h @@ -13,6 +13,8 @@ namespace rcc { class MachineFunction; class MachineBasicBlock; +class MachineInstr; +class MachineOperand; class TargetDescription; namespace mir { @@ -36,6 +38,17 @@ struct MIPrinterOptions final { const MIParserContext &Context = {}, MIPrinterOptions Options = {}); +[[nodiscard]] Expected +createMachineFunctionPrintContext(const MachineFunction &Function); +[[nodiscard]] Expected printMachineInstruction(const MachineInstr &Instruction, + const TargetDescription &Target, + const MIParserContext &Context, + MIPrinterOptions Options = {}); +[[nodiscard]] Expected printMachineOperand(const MachineOperand &Operand, + const TargetDescription &Target, + const MIParserContext &Context, + MIPrinterOptions Options = {}); + struct MIRDocumentPrinterOptions final { MIPrintMode Mode = MIPrintMode::Complete; bool EmitEmbeddedIR = true; diff --git a/backend/include/rcc/CodeGen/ReachingDefAnalysis.h b/backend/include/rcc/CodeGen/ReachingDefAnalysis.h new file mode 100644 index 0000000..64eb462 --- /dev/null +++ b/backend/include/rcc/CodeGen/ReachingDefAnalysis.h @@ -0,0 +1,52 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef RCC_CODEGEN_REACHINGDEFANALYSIS_H +#define RCC_CODEGEN_REACHINGDEFANALYSIS_H + +#include "rcc/CodeGen/MachineFunctionAnalysisManager.h" +#include "rcc/Support/Error.h" + +#include +#include +#include +#include +#include + +namespace rcc { + +class MachineFunction; +class MachineInstr; +class MachineOperand; +class TargetDescription; + +class ReachingDefAnalysis final { +public: + using DefinitionSet = std::vector; + + class Result final { + public: + [[nodiscard]] std::span + reachingDefinitions(const MachineOperand &Operand) const noexcept; + [[nodiscard]] std::size_t instructionNumber(const MachineInstr &Instruction) const noexcept; + [[nodiscard]] bool invalidate(const MachineFunction &, + const PreservedAnalyses &Preserved) const noexcept; + + private: + std::unordered_map OperandDefinitions; + std::unordered_map InstructionNumbers; + + friend class ReachingDefAnalysis; + }; + + [[nodiscard]] static constexpr std::string_view name() noexcept { + return "ReachingDefAnalysis"; + } + + [[nodiscard]] static Expected run(MachineFunction &Function, + const TargetDescription &Target, + MachineFunctionAnalysisManager &Analyses); +}; + +} // namespace rcc + +#endif // RCC_CODEGEN_REACHINGDEFANALYSIS_H diff --git a/backend/include/rcc/MIR/ReachingDefPrinter.h b/backend/include/rcc/MIR/ReachingDefPrinter.h new file mode 100644 index 0000000..8b4d1e0 --- /dev/null +++ b/backend/include/rcc/MIR/ReachingDefPrinter.h @@ -0,0 +1,26 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef RCC_MIR_REACHINGDEFPRINTER_H +#define RCC_MIR_REACHINGDEFPRINTER_H + +#include "rcc/CodeGen/MachinePassManager.h" + +#include + +namespace rcc::mir { + +class ReachingDefPrinterPass final : public MachineFunctionPass { +public: + [[nodiscard]] static constexpr std::string_view name() noexcept { + return "print"; + } + + [[nodiscard]] const MachinePassContract &contract() const noexcept override; + [[nodiscard]] Expected run(MachineFunction &Function, + const TargetDescription &Target, + MachineFunctionAnalysisManager &Analyses) override; +}; + +} // namespace rcc::mir + +#endif // RCC_MIR_REACHINGDEFPRINTER_H -- cgit v1.2.3