From 7d763c8b0ae6eee5f015fa82c744bff4c7ec8c8b Mon Sep 17 00:00:00 2001 From: Richard-Qin-X Date: Sat, 15 Aug 2026 03:11:22 +0800 Subject: [GlobalISel] Preserve worklists when moving CSE instructions --- backend/include/rcc/CodeGen/MachineBasicBlock.h | 3 +++ backend/lib/CodeGen/GlobalISel/CSEMIRBuilder.cpp | 8 +------ backend/lib/CodeGen/MachineBasicBlock.cpp | 28 ++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 7 deletions(-) (limited to 'backend') diff --git a/backend/include/rcc/CodeGen/MachineBasicBlock.h b/backend/include/rcc/CodeGen/MachineBasicBlock.h index da538db..10cc438 100644 --- a/backend/include/rcc/CodeGen/MachineBasicBlock.h +++ b/backend/include/rcc/CodeGen/MachineBasicBlock.h @@ -99,6 +99,9 @@ public: MachineInstr &Instruction); [[nodiscard]] Expected insertInstructionAfter(MachineInstr &Position, MachineInstr &Instruction); + // Reorder linked instructions without reporting removal or insertion to the delegate. + [[nodiscard]] Expected spliceInstructionBefore(MachineInstr &Position, + MachineInstr &Instruction); [[nodiscard]] Expected removeInstruction(MachineInstr &Instruction); void clearInstructions() noexcept; diff --git a/backend/lib/CodeGen/GlobalISel/CSEMIRBuilder.cpp b/backend/lib/CodeGen/GlobalISel/CSEMIRBuilder.cpp index e1fdaa6..caf9652 100644 --- a/backend/lib/CodeGen/GlobalISel/CSEMIRBuilder.cpp +++ b/backend/lib/CodeGen/GlobalISel/CSEMIRBuilder.cpp @@ -53,13 +53,7 @@ Expected CSEMIRBuilder::makeInstructionDominate(MachineInstr &Instruction) } if (InsertBefore == nullptr || dominatesInsertionPoint(Instruction)) return {}; - auto Removed = Block->removeInstruction(Instruction); - if (!Removed) - return std::unexpected(Removed.error()); - auto Inserted = Block->insertInstructionBefore(*InsertBefore, Instruction); - if (!Inserted) - return std::unexpected(Inserted.error()); - return {}; + return Block->spliceInstructionBefore(*InsertBefore, Instruction); } Expected diff --git a/backend/lib/CodeGen/MachineBasicBlock.cpp b/backend/lib/CodeGen/MachineBasicBlock.cpp index 763a577..2de8f89 100644 --- a/backend/lib/CodeGen/MachineBasicBlock.cpp +++ b/backend/lib/CodeGen/MachineBasicBlock.cpp @@ -99,6 +99,34 @@ Expected MachineBasicBlock::insertInstructionAfter(MachineInstr &Position, return {}; } +Expected MachineBasicBlock::spliceInstructionBefore(MachineInstr &Position, + MachineInstr &Instruction) { + if (Position.parent() != this || Instruction.parent() != this) { + return makeUnexpected(ErrorCode::InvalidArgument, + "instruction splice requires two instructions in this block"); + } + if (&Position == &Instruction || Instruction.NextInstruction == &Position) + return {}; + + if (Instruction.PreviousInstruction != nullptr) + Instruction.PreviousInstruction->NextInstruction = Instruction.NextInstruction; + else + FirstInstruction = Instruction.NextInstruction; + if (Instruction.NextInstruction != nullptr) + Instruction.NextInstruction->PreviousInstruction = Instruction.PreviousInstruction; + else + LastInstruction = Instruction.PreviousInstruction; + + Instruction.PreviousInstruction = Position.PreviousInstruction; + Instruction.NextInstruction = &Position; + if (Position.PreviousInstruction != nullptr) + Position.PreviousInstruction->NextInstruction = &Instruction; + else + FirstInstruction = &Instruction; + Position.PreviousInstruction = &Instruction; + return {}; +} + Expected MachineBasicBlock::removeInstruction(MachineInstr &Instruction) { if (Instruction.parent() != this) { return makeUnexpected(ErrorCode::InvalidArgument, "instruction does not belong to this block"); -- cgit v1.2.3