From e72ad2cd524ff38f162a23d69da5f78494d337ae Mon Sep 17 00:00:00 2001 From: Richard-Qin-X Date: Fri, 28 Aug 2026 13:45:07 +0800 Subject: [CodeGen] Centralize machine instruction erasure --- .../RISCV/GISel/RISCVInstructionSelector.cpp | 18 ++-------- .../lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp | 36 ++++---------------- backend/lib/Target/RISCV/RISCVFinalizeISel.cpp | 18 +++------- backend/lib/Target/RISCV/RISCVFrameLowering.cpp | 4 +-- backend/lib/Target/RISCV/RISCVInstrInfo.cpp | 19 +++-------- backend/lib/Target/RISCV/RISCVMergeBaseOffset.cpp | 39 +++++----------------- backend/lib/Target/RISCV/RISCVMoveMerger.cpp | 4 +-- backend/lib/Target/RISCV/RISCVOptWInstrs.cpp | 2 +- backend/lib/Target/RISCV/RISCVPseudoExpansion.cpp | 15 +++------ backend/lib/Target/RISCV/RISCVPushPopOptimizer.cpp | 10 ++---- 10 files changed, 38 insertions(+), 127 deletions(-) (limited to 'backend/lib/Target/RISCV') diff --git a/backend/lib/Target/RISCV/GISel/RISCVInstructionSelector.cpp b/backend/lib/Target/RISCV/GISel/RISCVInstructionSelector.cpp index f87bb78..8361de2 100644 --- a/backend/lib/Target/RISCV/GISel/RISCVInstructionSelector.cpp +++ b/backend/lib/Target/RISCV/GISel/RISCVInstructionSelector.cpp @@ -707,11 +707,7 @@ private: auto Constraint = constrainSelectedInstruction(**Result, Function, Target); if (!Constraint) return std::unexpected(Constraint.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "RISC-V global address is detached from its block"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); }; if (UseGOT) { @@ -781,11 +777,7 @@ private: auto LowConstraint = constrainSelectedInstruction(**Low, Function, Target); if (!LowConstraint) return std::unexpected(LowConstraint.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "RISC-V global address is detached from its block"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } [[nodiscard]] Expected selectCopy(MachineInstr &Instruction, MachineFunction &Function, @@ -1107,11 +1099,7 @@ private: if (!Inverted) return std::unexpected(Inverted.error()); } - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "RISC-V floating comparison is detached from its block"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } [[nodiscard]] Expected selectFence(MachineInstr &Instruction) const { diff --git a/backend/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp b/backend/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp index 5d0be47..3c0ec35 100644 --- a/backend/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp +++ b/backend/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp @@ -154,11 +154,7 @@ foldConstantExtension(MachineInstr &Instruction, MachineFunction &Function, Gene auto Added = Def->addOperand(MachineOperand::integerImmediate(std::move(*Extended))); if (!Added) return std::unexpected(Added.error()); - MachineBasicBlock *Parent = Instruction.parent(); - if (Parent == nullptr) - return makeUnexpected(ErrorCode::VerificationError, - "RISC-V constant extension is detached during folding"); - return Parent->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } auto SetOpcode = Instruction.setOpcode(opcode(GenericOpcode::G_CONSTANT)); @@ -175,11 +171,7 @@ foldConstantExtension(MachineInstr &Instruction, MachineFunction &Function, Gene if (Function.registerInfo().useEmpty(SourceRegister)) { MachineInstr *Def = const_cast(SourceDef); - MachineBasicBlock *Parent = Def->parent(); - if (Parent == nullptr) - return makeUnexpected(ErrorCode::VerificationError, - "RISC-V constant extension source definition is detached"); - auto Removed = Parent->removeInstruction(*Def); + auto Removed = Def->eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); // Keep the source register record as a tombstone-free historical vreg until @@ -903,11 +895,7 @@ Expected RISCVLegalizerInfo::legalizeBRJT(MachineInstr &Instruction, if (auto Built = BranchBuilder->build(); !Built) return std::unexpected(Built.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "RISC-V G_BRJT became detached during legalization"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected RISCVLegalizerInfo::legalizeVAStart(MachineInstr &Instruction, @@ -984,11 +972,7 @@ Expected RISCVLegalizerInfo::legalizeVAStart(MachineInstr &Instruction, if (auto Built = StoreBuilder->build(); !Built) return std::unexpected(Built.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "RISC-V G_VASTART became detached during legalization"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected RISCVLegalizerInfo::legalizeCustom(LegalizerHelper &Helper, @@ -1243,11 +1227,7 @@ Expected RISCVLegalizerInfo::legalizeCustom(LegalizerHelper &Helper, !Built) { return std::unexpected(Built.error()); } - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "RISC-V G_IS_FPCLASS became detached during lowering"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } if (Instruction.opcode() == opcode(GenericOpcode::G_ANYEXT) || Instruction.opcode() == opcode(GenericOpcode::G_SEXT) || @@ -1388,11 +1368,7 @@ Expected RISCVLegalizerInfo::legalizeIntrinsic(LegalizerHelper &Helper, if (auto Built = StoreBuilder->build(); !Built) return std::unexpected(Built.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "llvm.va_copy intrinsic became detached during lowering"); - if (auto Removed = Block->removeInstruction(Instruction); !Removed) + if (auto Removed = Instruction.eraseFromParent(); !Removed) return std::unexpected(Removed.error()); return true; } diff --git a/backend/lib/Target/RISCV/RISCVFinalizeISel.cpp b/backend/lib/Target/RISCV/RISCVFinalizeISel.cpp index 784490d..e2d99b3 100644 --- a/backend/lib/Target/RISCV/RISCVFinalizeISel.cpp +++ b/backend/lib/Target/RISCV/RISCVFinalizeISel.cpp @@ -254,10 +254,7 @@ buildPhi(MachineFunction &Function, const TargetDescription &Target, MachineBasi [[nodiscard]] Expected moveInstructionToEnd(MachineInstr &Instruction, MachineBasicBlock &Destination) { - MachineBasicBlock *Source = Instruction.parent(); - if (Source == nullptr) - return makeUnexpected(ErrorCode::InternalError, "moved instruction is detached"); - if (auto Removed = Source->removeInstruction(Instruction); !Removed) + if (auto Removed = Instruction.eraseFromParent(); !Removed) return std::unexpected(Removed.error()); return Destination.appendInstruction(Instruction); } @@ -326,11 +323,7 @@ buildPhi(MachineFunction &Function, const TargetDescription &Target, MachineBasi return std::unexpected(Phi.error()); } for (MachineInstr *Select : Sequence->Selects) { - MachineBasicBlock *Parent = Select->parent(); - if (Parent == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "RISC-V custom select became detached during expansion"); - if (auto Removed = Parent->removeInstruction(*Select); !Removed) + if (auto Removed = Select->eraseFromParent(); !Removed) return std::unexpected(Removed.error()); } Function.properties().clear(MachineFunctionProperty::NoPHIs); @@ -468,12 +461,9 @@ emitCascadedSelect(MachineInstr &First, MachineInstr &Second, MachineBasicBlock if (!Phi) return std::unexpected(Phi.error()); - if (auto Removed = Head.removeInstruction(First); !Removed) + if (auto Removed = First.eraseFromParent(); !Removed) return std::unexpected(Removed.error()); - MachineBasicBlock *SecondParent = Second.parent(); - if (SecondParent == nullptr) - return makeUnexpected(ErrorCode::InternalError, "cascaded select became detached"); - if (auto Removed = SecondParent->removeInstruction(Second); !Removed) + if (auto Removed = Second.eraseFromParent(); !Removed) return std::unexpected(Removed.error()); Function.properties().clear(MachineFunctionProperty::NoPHIs); return Sink; diff --git a/backend/lib/Target/RISCV/RISCVFrameLowering.cpp b/backend/lib/Target/RISCV/RISCVFrameLowering.cpp index 8a40bc1..1c9ec62 100644 --- a/backend/lib/Target/RISCV/RISCVFrameLowering.cpp +++ b/backend/lib/Target/RISCV/RISCVFrameLowering.cpp @@ -1714,7 +1714,7 @@ public: if (!Adjusted) return std::unexpected(Adjusted.error()); } - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } [[nodiscard]] Expected emitPrologue(MachineFunction &Function, @@ -2291,7 +2291,7 @@ public: auto Built = Pending->build(); if (!Built) return std::unexpected(Built.error()); - auto Removed = Block.removeInstruction(Terminator); + auto Removed = Terminator.eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); } diff --git a/backend/lib/Target/RISCV/RISCVInstrInfo.cpp b/backend/lib/Target/RISCV/RISCVInstrInfo.cpp index 11387c1..6e356a6 100644 --- a/backend/lib/Target/RISCV/RISCVInstrInfo.cpp +++ b/backend/lib/Target/RISCV/RISCVInstrInfo.cpp @@ -218,15 +218,6 @@ constexpr std::uint32_t PCRelativeLowFlag = 4; return Operand->setBasicBlock(TargetBlock); } -[[nodiscard]] Expected removeInstruction(MachineInstr &Instruction) { - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) { - return makeUnexpected(ErrorCode::InvalidArgument, - "RISC-V branch instruction is not linked to a block"); - } - return Block->removeInstruction(Instruction); -} - [[nodiscard]] Expected buildJAL(MachineFunction &Function, const TargetDescription &Target, MachineBasicBlock &Block, MachineInstr *Before, PhysicalRegister Destination, MachineBasicBlock &TargetBlock) { @@ -380,7 +371,7 @@ selectLongBranchScratch(const MachineInstr &Instruction, const MachineBasicBlock auto Annotations = copySingleInstructionAnnotations(Instruction, **Replacement); if (!Annotations) return std::unexpected(Annotations.error()); - auto Removed = removeInstruction(Instruction); + auto Removed = Instruction.eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); return true; @@ -415,7 +406,7 @@ selectLongBranchScratch(const MachineInstr &Instruction, const MachineBasicBlock auto Annotations = copySingleInstructionAnnotations(Instruction, **Replacement); if (!Annotations) return std::unexpected(Annotations.error()); - auto Removed = removeInstruction(Instruction); + auto Removed = Instruction.eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); return true; @@ -436,7 +427,7 @@ selectLongBranchScratch(const MachineInstr &Instruction, const MachineBasicBlock if (!FalseTarget) return std::unexpected(FalseTarget.error()); if (*FalseTarget == *TrueTarget) { - auto Removed = removeInstruction(Instruction); + auto Removed = Instruction.eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); return true; @@ -589,7 +580,7 @@ selectLongBranchScratch(const MachineInstr &Instruction, const MachineBasicBlock LowFlags.clear(MachineInstrFlag::BundledWithPred); LowFlags.set(MachineInstrFlag::BundledWithPred); (*Low)->setFlags(LowFlags); - auto Removed = removeInstruction(Instruction); + auto Removed = Instruction.eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); return true; @@ -798,7 +789,7 @@ public: if (&Branch.function() != &Function) return makeUnexpected(ErrorCode::InvalidArgument, "RISC-V branch removal references another function"); - return removeInstruction(Branch); + return Branch.eraseFromParent(); } [[nodiscard]] Expected rewriteBranchTarget(MachineBasicBlock &Block, diff --git a/backend/lib/Target/RISCV/RISCVMergeBaseOffset.cpp b/backend/lib/Target/RISCV/RISCVMergeBaseOffset.cpp index d9b2f4b..8ef05aa 100644 --- a/backend/lib/Target/RISCV/RISCVMergeBaseOffset.cpp +++ b/backend/lib/Target/RISCV/RISCVMergeBaseOffset.cpp @@ -176,10 +176,7 @@ struct AddressPair final { auto ReplacedRegisters = Registers.replaceRegWith(*TailRegister, *LowRegister); if (!ReplacedRegisters) return std::unexpected(ReplacedRegisters.error()); - MachineBasicBlock *Block = Tail.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, "RISC-V offset tail is not linked"); - auto Removed = Block->removeInstruction(Tail); + auto Removed = Tail.eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); return true; @@ -215,10 +212,7 @@ struct AddressPair final { auto Folded = foldOffset(Pair, Tail, *Low, Registers); if (!Folded || !*Folded) return Folded; - MachineBasicBlock *Block = OffsetTail->parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, "RISC-V offset instruction is not linked"); - auto Removed = Block->removeInstruction(*OffsetTail); + auto Removed = OffsetTail->eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); return true; @@ -239,14 +233,10 @@ struct AddressPair final { auto Folded = foldOffset(Pair, Tail, Offset, Registers); if (!Folded || !*Folded) return Folded; - MachineBasicBlock *Block = OffsetTail->parent(); - MachineBasicBlock *HighBlock = OffsetHigh->parent(); - if (Block == nullptr || HighBlock == nullptr) - return makeUnexpected(ErrorCode::InternalError, "RISC-V offset instructions are not linked"); - auto Removed = Block->removeInstruction(*OffsetTail); + auto Removed = OffsetTail->eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); - Removed = HighBlock->removeInstruction(*OffsetHigh); + Removed = OffsetHigh->eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); return true; @@ -263,10 +253,7 @@ struct AddressPair final { auto Folded = foldOffset(Pair, Tail, Offset, Registers); if (!Folded || !*Folded) return Folded; - MachineBasicBlock *Block = OffsetTail->parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, "RISC-V offset instruction is not linked"); - auto Removed = Block->removeInstruction(*OffsetTail); + auto Removed = OffsetTail->eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); return true; @@ -306,10 +293,7 @@ struct AddressPair final { auto Folded = foldOffset(Pair, Tail, Offset, Registers); if (!Folded || !*Folded) return Folded; - MachineBasicBlock *Block = OffsetTail->parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, "RISC-V offset instruction is not linked"); - auto Removed = Block->removeInstruction(*OffsetTail); + auto Removed = OffsetTail->eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); return true; @@ -343,11 +327,7 @@ struct AddressPair final { auto Folded = foldOffset(Pair, *SecondTail, *Immediate + *SecondImmediate, Registers); if (!Folded || !*Folded) return Folded; - MachineBasicBlock *Block = Tail->parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "RISC-V offset instruction is not linked"); - auto Removed = Block->removeInstruction(*Tail); + auto Removed = Tail->eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); return true; @@ -444,10 +424,7 @@ struct AddressPair final { if (!ReplacedOffset) return std::unexpected(ReplacedOffset.error()); } - MachineBasicBlock *Block = Pair.Low->parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, "RISC-V low address instruction is not linked"); - auto Removed = Block->removeInstruction(*Pair.Low); + auto Removed = Pair.Low->eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); return true; diff --git a/backend/lib/Target/RISCV/RISCVMoveMerger.cpp b/backend/lib/Target/RISCV/RISCVMoveMerger.cpp index fa1b1ee..1accb2e 100644 --- a/backend/lib/Target/RISCV/RISCVMoveMerger.cpp +++ b/backend/lib/Target/RISCV/RISCVMoveMerger.cpp @@ -241,10 +241,10 @@ struct CopyOperands final { auto Merged = mergeCopies(Function, *Instruction, *Paired, MoveFromSavedToArgument, Target); if (!Merged) return std::unexpected(Merged.error()); - auto RemovedFirst = Block.removeInstruction(*Instruction); + auto RemovedFirst = Instruction->eraseFromParent(); if (!RemovedFirst) return std::unexpected(RemovedFirst.error()); - auto RemovedPaired = Block.removeInstruction(*Paired); + auto RemovedPaired = Paired->eraseFromParent(); if (!RemovedPaired) return std::unexpected(RemovedPaired.error()); Changed = true; diff --git a/backend/lib/Target/RISCV/RISCVOptWInstrs.cpp b/backend/lib/Target/RISCV/RISCVOptWInstrs.cpp index 124a4f0..5448ae1 100644 --- a/backend/lib/Target/RISCV/RISCVOptWInstrs.cpp +++ b/backend/lib/Target/RISCV/RISCVOptWInstrs.cpp @@ -475,7 +475,7 @@ void clearWordInvalidFlags(MachineInstr &Instruction) { auto Cleared = clearKillFlags(Function, *Source); if (!Cleared) return std::unexpected(Cleared.error()); - auto Removed = Block->removeInstruction(*Instruction); + auto Removed = Instruction->eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); Changed = true; diff --git a/backend/lib/Target/RISCV/RISCVPseudoExpansion.cpp b/backend/lib/Target/RISCV/RISCVPseudoExpansion.cpp index 6945f68..3b3a205 100644 --- a/backend/lib/Target/RISCV/RISCVPseudoExpansion.cpp +++ b/backend/lib/Target/RISCV/RISCVPseudoExpansion.cpp @@ -121,13 +121,6 @@ registerBank(Register Reg, const MachineFunction &Function, const TargetDescript return {}; } -[[nodiscard]] Expected removeOldInstruction(MachineInstr &Instruction) { - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InvalidArgument, "pseudo instruction is not linked"); - return Block->removeInstruction(Instruction); -} - [[nodiscard]] Expected> copyRegisters(const MachineInstr &Instruction) { const MachineOperand *DestinationOperand = Instruction.operandAt(0); @@ -190,7 +183,7 @@ copyRegisters(const MachineInstr &Instruction) { "RISC-V post-RA COPY retains a virtual register"); } if (Destination == Source) { - auto Removed = removeOldInstruction(Instruction); + auto Removed = Instruction.eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); return true; @@ -270,7 +263,7 @@ copyRegisters(const MachineInstr &Instruction) { auto Decoration = copyCommonDecoration(Instruction, **Replacement, true, true, true); if (!Decoration) return std::unexpected(Decoration.error()); - auto Removed = removeOldInstruction(Instruction); + auto Removed = Instruction.eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); return true; @@ -418,7 +411,7 @@ copyRegisters(const MachineInstr &Instruction) { Function.records().replaceInstructionReference(Instruction.stableID(), (*Low)->stableID()); if (!Records) return std::unexpected(Records.error()); - auto Removed = removeOldInstruction(Instruction); + auto Removed = Instruction.eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); return true; @@ -560,7 +553,7 @@ copyRegisters(const MachineInstr &Instruction) { Function.records().replaceInstructionReference(Instruction.stableID(), (*Low)->stableID()); if (!Records) return std::unexpected(Records.error()); - auto Removed = removeOldInstruction(Instruction); + auto Removed = Instruction.eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); return true; diff --git a/backend/lib/Target/RISCV/RISCVPushPopOptimizer.cpp b/backend/lib/Target/RISCV/RISCVPushPopOptimizer.cpp index 16928fb..dc33ad1 100644 --- a/backend/lib/Target/RISCV/RISCVPushPopOptimizer.cpp +++ b/backend/lib/Target/RISCV/RISCVPushPopOptimizer.cpp @@ -127,11 +127,7 @@ struct PushPopOpcodes final { const std::optional Source = Copy->Source->registerValue(); if (Destination && Source && Destination->physicalRegister() == ReturnRegister && Source->physicalRegister() == ZeroRegister) { - MachineBasicBlock *Parent = Instruction->parent(); - if (Parent == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "RISC-V return-value assignment became detached"); - if (auto Removed = Parent->removeInstruction(*Instruction); !Removed) + if (auto Removed = Instruction->eraseFromParent(); !Removed) return std::unexpected(Removed.error()); return true; } @@ -244,9 +240,9 @@ public: buildPopReturn(*PopInstruction, *Return, *ReturnZero, Opcodes, Function, Target); if (!NewReturn) return std::unexpected(NewReturn.error()); - if (auto Removed = Block->removeInstruction(*PopInstruction); !Removed) + if (auto Removed = PopInstruction->eraseFromParent(); !Removed) return std::unexpected(Removed.error()); - if (auto Removed = Block->removeInstruction(*Return); !Removed) + if (auto Removed = Return->eraseFromParent(); !Removed) return std::unexpected(Removed.error()); Result.Changes.add(MachinePassChange::Instructions); Result.Changes.add(MachinePassChange::Operands); -- cgit v1.2.3