From 864a29abbdce86353ec91c3587757711d3a42f10 Mon Sep 17 00:00:00 2001 From: Richard-Qin-X Date: Sun, 16 Aug 2026 17:28:49 +0800 Subject: [RISCV][GlobalISel] Generate integer comparisons --- .../rcc/CodeGen/GlobalISel/GIMatchTableExecutor.h | 28 ++++++++++++++++++-- .../rcc/CodeGen/GlobalISel/InstructionSelector.h | 6 +++++ backend/include/rcc/TableGen/Record.h | 30 +++++++++++++++++++--- 3 files changed, 58 insertions(+), 6 deletions(-) (limited to 'backend/include') diff --git a/backend/include/rcc/CodeGen/GlobalISel/GIMatchTableExecutor.h b/backend/include/rcc/CodeGen/GlobalISel/GIMatchTableExecutor.h index 7cceca3..afd0d70 100644 --- a/backend/include/rcc/CodeGen/GlobalISel/GIMatchTableExecutor.h +++ b/backend/include/rcc/CodeGen/GlobalISel/GIMatchTableExecutor.h @@ -22,17 +22,27 @@ enum class GIMatchTypeKind : std::uint8_t { Pointer, }; +enum class GIMatchImmediateTransform : std::uint8_t { + Identity, + Negate, + PlusOne, +}; + enum class GIMatchTableOpcode : std::uint8_t { CheckOpcode, CheckOperandType, CheckOperandBank, CheckOperandClass, CheckIntegerPredicate, - CheckOperandDefConstant, + CheckOperandDefConstantRanges, + CreateTemporary, MutateOpcode, + BuildInstruction, RenderOperand, RenderPhysicalRegister, - AppendImmediate, + RenderTemporary, + RenderCapturedImmediate, + RenderImmediate, EraseOperandDefinition, Complete, }; @@ -45,6 +55,11 @@ struct GIMatchTableInstruction final { std::int64_t Immediate = 0; }; +struct GIMatchTableConstantRange final { + std::int64_t Minimum = 0; + std::int64_t Maximum = 0; +}; + struct GIMatchTableRule final { std::string_view Name; std::uint32_t Priority = 0; @@ -55,10 +70,13 @@ struct GIMatchTableRule final { struct GIMatchTable final { std::span Instructions; std::span Rules; + std::span ConstantRanges; }; class GIMatchTableExecutor { public: + virtual ~GIMatchTableExecutor() = default; + [[nodiscard]] Expected match(const GIMatchTable &Table, const MachineInstr &Instruction, const MachineFunction &Function, const TargetDescription &Target) const; @@ -68,6 +86,12 @@ public: [[nodiscard]] Expected select(const GIMatchTable &Table, MachineInstr &Instruction, MachineFunction &Function, const TargetDescription &Target) const; + +protected: + [[nodiscard]] virtual Expected + finalizeGeneratedInstruction(MachineInstr &Output, MachineFunction &Function, + const TargetDescription &Target, MachineInstrFlags InputFlags, + bool InputMayRaiseFPException) const; }; } // namespace rcc diff --git a/backend/include/rcc/CodeGen/GlobalISel/InstructionSelector.h b/backend/include/rcc/CodeGen/GlobalISel/InstructionSelector.h index 36fca8d..f45b24d 100644 --- a/backend/include/rcc/CodeGen/GlobalISel/InstructionSelector.h +++ b/backend/include/rcc/CodeGen/GlobalISel/InstructionSelector.h @@ -38,6 +38,12 @@ public: [[nodiscard]] Expected propagateSelectionFlags(MachineInstr &Output, const MachineInstr &Input, const TargetDescription &Target) const; + +protected: + [[nodiscard]] Expected + finalizeGeneratedInstruction(MachineInstr &Output, MachineFunction &Function, + const TargetDescription &Target, MachineInstrFlags InputFlags, + bool InputMayRaiseFPException) const override; }; } // namespace rcc diff --git a/backend/include/rcc/TableGen/Record.h b/backend/include/rcc/TableGen/Record.h index 4378af8..57e5832 100644 --- a/backend/include/rcc/TableGen/Record.h +++ b/backend/include/rcc/TableGen/Record.h @@ -237,17 +237,40 @@ struct SourceSelectionIntegerPredicate final { struct SourceSelectionOperandDefConstantPredicate final { std::uint32_t OperandIndex = 0; - std::int64_t Value = 0; + struct ConstantRange final { + std::int64_t Minimum = 0; + std::int64_t Maximum = 0; + TextRange SourceRange; + }; + + std::vector Ranges; + TextRange Range; +}; + +struct SourceSelectionTemporary final { + std::string Symbol; + std::uint32_t TypeFromOperand = 0; TextRange Range; }; struct SourceSelectionOperandRenderer final { std::optional OperandIndex; std::string PhysicalRegisterSymbol; + std::string TemporarySymbol; + std::optional CapturedImmediateOperand; + std::optional Immediate; + std::string ImmediateTransform; std::vector Flags; TextRange Range; }; +struct SourceSelectionAction final { + std::string ResultOpcodeSymbol; + bool ReplacesRoot = false; + std::vector OperandRenderers; + TextRange Range; +}; + struct SourceSelectionRule final { std::string Symbol; std::uint32_t Priority = 0; @@ -258,9 +281,8 @@ struct SourceSelectionRule final { std::vector OperandBanks; std::vector OperandClasses; std::vector OperandDefConstants; - std::string ResultOpcodeSymbol; - std::optional> OperandRenderers; - std::vector AppendedImmediates; + std::vector Temporaries; + std::vector Actions; std::vector EraseOperandDefinitions; TextRange Range; }; -- cgit v1.2.3