diff options
| author | Richard-Qin-X <richard.qin.001@gmail.com> | 2026-08-28 13:45:07 +0800 |
|---|---|---|
| committer | Richard-Qin-X <richard.qin.001@gmail.com> | 2026-08-28 13:45:07 +0800 |
| commit | e72ad2cd524ff38f162a23d69da5f78494d337ae (patch) | |
| tree | 2e590806b0f610e4392f25f6e8cdff57d2f5a929 | |
| parent | 3b14bd4d5324b5a280c1ee5567b5526b59e60c5b (diff) | |
| download | rcc-project-e72ad2cd524ff38f162a23d69da5f78494d337ae.tar.gz rcc-project-e72ad2cd524ff38f162a23d69da5f78494d337ae.tar.xz | |
[CodeGen] Centralize machine instruction erasure
30 files changed, 148 insertions, 474 deletions
diff --git a/backend/include/rcc/CodeGen/GlobalISel/LegalizationArtifactCombiner.h b/backend/include/rcc/CodeGen/GlobalISel/LegalizationArtifactCombiner.h index c0c7be0..12225cf 100644 --- a/backend/include/rcc/CodeGen/GlobalISel/LegalizationArtifactCombiner.h +++ b/backend/include/rcc/CodeGen/GlobalISel/LegalizationArtifactCombiner.h @@ -351,7 +351,7 @@ private: for (MachineInstr *Dead : DeadInstructions) { if (Dead == nullptr || Dead->parent() == nullptr) continue; - auto Removed = Dead->parent()->removeInstruction(*Dead); + auto Removed = Dead->eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); } diff --git a/backend/include/rcc/CodeGen/MachineInstr.h b/backend/include/rcc/CodeGen/MachineInstr.h index 6de4a4e..441f80d 100644 --- a/backend/include/rcc/CodeGen/MachineInstr.h +++ b/backend/include/rcc/CodeGen/MachineInstr.h @@ -79,6 +79,8 @@ public: return ParentBlock != nullptr; } + [[nodiscard]] Expected<void> eraseFromParent(); + [[nodiscard]] bool isDebugInstr() const noexcept; [[nodiscard]] std::size_t operandCount() const noexcept { diff --git a/backend/lib/CodeGen/GlobalISel/InstructionSelect.cpp b/backend/lib/CodeGen/GlobalISel/InstructionSelect.cpp index 88bc78e..0814641 100644 --- a/backend/lib/CodeGen/GlobalISel/InstructionSelect.cpp +++ b/backend/lib/CodeGen/GlobalISel/InstructionSelect.cpp @@ -215,10 +215,9 @@ Expected<MachinePassResult> InstructionSelect::run(MachineFunction &Function, return runImplementation(Function, Target, Analyses); } -Expected<MachinePassResult> InstructionSelect::runImplementation(MachineFunction &Function, - const TargetDescription &Target, - MachineFunctionAnalysisManager - &Analyses) { +Expected<MachinePassResult> +InstructionSelect::runImplementation(MachineFunction &Function, const TargetDescription &Target, + MachineFunctionAnalysisManager &Analyses) { MachinePassResult Result; std::vector<MachineInstr *> Worklist; @@ -378,7 +377,7 @@ Expected<MachinePassResult> InstructionSelect::runImplementation(MachineFunction auto Replaced = Function.registerInfo().replaceRegWith(*Destination, *Source); if (!Replaced) return std::unexpected(Replaced.error()); - auto Removed = Block->removeInstruction(*Instruction); + auto Removed = Instruction->eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); Result.Changed = true; diff --git a/backend/lib/CodeGen/GlobalISel/LegalizerHelper.cpp b/backend/lib/CodeGen/GlobalISel/LegalizerHelper.cpp index 2f1d329..06ec01e 100644 --- a/backend/lib/CodeGen/GlobalISel/LegalizerHelper.cpp +++ b/backend/lib/CodeGen/GlobalISel/LegalizerHelper.cpp @@ -650,11 +650,7 @@ Expected<void> LegalizerHelper::widenScalarMergeValues(MachineInstr &Instruction return std::unexpected(Built.error()); } - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "G_MERGE_VALUES became detached during widening"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } auto GCDType = LowLevelType::makeCategoryNeutralScalar(std::gcd(SourceSize, WideSize)); @@ -721,11 +717,7 @@ Expected<void> LegalizerHelper::widenScalarMergeValues(MachineInstr &Instruction return std::unexpected(Built.error()); } - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "G_MERGE_VALUES became detached during widening"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::widenScalarUnmergeValues(MachineInstr &Instruction, @@ -804,11 +796,7 @@ Expected<void> LegalizerHelper::widenScalarUnmergeValues(MachineInstr &Instructi return std::unexpected(Built.error()); } - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "G_UNMERGE_VALUES became detached during widening"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } auto LCMType = getLCMType(*SourceType, WideType); @@ -890,11 +878,7 @@ Expected<void> LegalizerHelper::widenScalarUnmergeValues(MachineInstr &Instructi } } - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "G_UNMERGE_VALUES became detached during widening"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::insertParts(Register Destination, LowLevelType ResultType, @@ -1471,11 +1455,7 @@ Expected<void> LegalizerHelper::widenScalarAddSubOverflow(MachineInstr &Instruct if (auto Built = MIRBuilder.buildTrunc(*Destination, *WideResult); !Built) return std::unexpected(Built.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "add/sub overflow instruction became detached during widening"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::widenScalarMulo(MachineInstr &Instruction, std::size_t TypeIndex, @@ -1588,11 +1568,7 @@ Expected<void> LegalizerHelper::widenScalarMulo(MachineInstr &Instruction, std:: return std::unexpected(Built.error()); } - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "multiply-overflow instruction became detached during widening"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::widenScalar(MachineInstr &Instruction, std::size_t TypeIndex, @@ -1815,11 +1791,7 @@ Expected<void> LegalizerHelper::widenScalar(MachineInstr &Instruction, std::size if (auto Built = MIRBuilder.buildZExtOrTrunc(*Destination, *Result); !Built) return std::unexpected(Built.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "bit-count instruction became detached during widening"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } case GenericOpcode::G_BSWAP: { @@ -2426,11 +2398,7 @@ Expected<void> LegalizerHelper::narrowScalarAddSub(MachineInstr &Instruction, st LeftoverType, LeftoverDestinations); if (!Inserted) return std::unexpected(Inserted.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "add/sub instruction became detached during narrowing"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::narrowScalarMul(MachineInstr &Instruction, std::size_t TypeIndex, @@ -2492,11 +2460,7 @@ Expected<void> LegalizerHelper::narrowScalarMul(MachineInstr &Instruction, std:: TemporaryDestinations.end()); if (auto Built = MIRBuilder.buildMergeLikeInstr(*Destination, ResultParts); !Built) return std::unexpected(Built.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "multiply instruction became detached during narrowing"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::narrowScalarExt(MachineInstr &Instruction, std::size_t TypeIndex, @@ -2528,11 +2492,7 @@ Expected<void> LegalizerHelper::narrowScalarExt(MachineInstr &Instruction, std:: if (!Remerged) return std::unexpected(Remerged.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "extension instruction became detached during narrowing"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::narrowScalarBasic(MachineInstr &Instruction, std::size_t TypeIndex, @@ -2602,11 +2562,7 @@ Expected<void> LegalizerHelper::narrowScalarBasic(MachineInstr &Instruction, std !Inserted) { return std::unexpected(Inserted.error()); } - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "basic scalar instruction became detached during narrowing"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::narrowScalarSelect(MachineInstr &Instruction, std::size_t TypeIndex, @@ -2683,10 +2639,7 @@ Expected<void> LegalizerHelper::narrowScalarSelect(MachineInstr &Instruction, st !Inserted) { return std::unexpected(Inserted.error()); } - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, "G_SELECT became detached during narrowing"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::narrowScalarShiftByConstant(MachineInstr &Instruction, @@ -2711,11 +2664,7 @@ Expected<void> LegalizerHelper::narrowScalarShiftByConstant(MachineInstr &Instru if (Amount.isZero()) { if (auto Built = MIRBuilder.buildMergeLikeInstr(*Destination, {*LowPart, *HighPart}); !Built) return std::unexpected(Built.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "constant shift became detached during narrowing"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } const std::uint64_t HalfBits = HalfType.minimumBitWidth(); @@ -2880,11 +2829,7 @@ Expected<void> LegalizerHelper::narrowScalarShiftByConstant(MachineInstr &Instru if (auto Built = MIRBuilder.buildMergeLikeInstr(*Destination, {LowResult, HighResult}); !Built) return std::unexpected(Built.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "constant shift became detached during narrowing"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::narrowScalarShift(MachineInstr &Instruction, std::size_t TypeIndex, @@ -3057,10 +3002,7 @@ Expected<void> LegalizerHelper::narrowScalarShift(MachineInstr &Instruction, std if (auto Built = MIRBuilder.buildMergeLikeInstr(*Destination, {LowResult, HighResult}); !Built) return std::unexpected(Built.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, "shift became detached during narrowing"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<Register> LegalizerHelper::buildConstantShiftPart(GenericOpcode Generic, @@ -3225,10 +3167,7 @@ Expected<void> LegalizerHelper::narrowScalarShiftByConstantMultiway(MachineInstr if (Amount.isZero()) { if (auto Built = MIRBuilder.buildMergeLikeInstr(*Destination, SourceParts); !Built) return std::unexpected(Built.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, "constant multiway shift became detached"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } const std::uint64_t AmountValue = Amount.word(0); @@ -3279,10 +3218,7 @@ Expected<void> LegalizerHelper::narrowScalarShiftByConstantMultiway(MachineInstr } if (auto Built = MIRBuilder.buildMergeLikeInstr(*Destination, DestinationParts); !Built) return std::unexpected(Built.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, "constant multiway shift became detached"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::narrowScalarShiftMultiway(MachineInstr &Instruction, @@ -3419,10 +3355,7 @@ Expected<void> LegalizerHelper::narrowScalarShiftMultiway(MachineInstr &Instruct if (auto Built = MIRBuilder.buildMergeLikeInstr(*Destination, DestinationParts); !Built) return std::unexpected(Built.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, "multiway shift became detached"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::narrowScalarLoadStore(MachineInstr &Instruction, @@ -3508,12 +3441,7 @@ Expected<void> LegalizerHelper::narrowScalarLoadStore(MachineInstr &Instruction, return std::unexpected(Built.error()); } - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) { - return makeUnexpected(ErrorCode::InternalError, - "memory instruction became detached during scalar narrowing"); - } - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } return reduceLoadStoreWidth(Instruction, TypeIndex, NarrowType); @@ -3667,12 +3595,7 @@ Expected<void> LegalizerHelper::reduceLoadStoreWidth(MachineInstr &Instruction, return std::unexpected(Inserted.error()); } } - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) { - return makeUnexpected(ErrorCode::InternalError, - "memory instruction became detached during width reduction"); - } - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::narrowScalar(MachineInstr &Instruction, std::size_t TypeIndex, @@ -3730,11 +3653,7 @@ Expected<void> LegalizerHelper::narrowScalar(MachineInstr &Instruction, std::siz return std::unexpected(Merged.error()); } - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "G_IMPLICIT_DEF became detached during narrowing"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } case GenericOpcode::G_CONSTANT: { const Register *Destination = Instruction.operandAt(0)->getIf<Register>(); @@ -3803,11 +3722,7 @@ Expected<void> LegalizerHelper::narrowScalar(MachineInstr &Instruction, std::siz !Inserted) { return std::unexpected(Inserted.error()); } - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "G_CONSTANT became detached during narrowing"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } case GenericOpcode::G_ADD: case GenericOpcode::G_SUB: @@ -3935,7 +3850,7 @@ Expected<void> LegalizerHelper::narrowScalar(MachineInstr &Instruction, std::siz if (auto Merged = MIRBuilder.buildMergeLikeInstr(*Destination, Destinations); !Merged) return std::unexpected(Merged.error()); Observer.changedInstr(Instruction); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } case GenericOpcode::G_FCMP: if (TypeIndex != 0) @@ -4055,11 +3970,7 @@ Expected<void> LegalizerHelper::narrowScalar(MachineInstr &Instruction, std::siz if (auto Merged = MIRBuilder.buildMergeLikeInstr(*Destination, DestinationParts); !Merged) return std::unexpected(Merged.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "G_SEXT_INREG became detached during narrowing"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } case GenericOpcode::G_AND: case GenericOpcode::G_OR: @@ -4232,10 +4143,7 @@ Expected<void> LegalizerHelper::narrowScalar(MachineInstr &Instruction, std::siz } } - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, "G_ICMP became detached during narrowing"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } default: return makeUnexpected(ErrorCode::Unsupported, @@ -4356,12 +4264,7 @@ Expected<void> LegalizerHelper::fewerElements(MachineInstr &Instruction, std::si } } - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) { - return makeUnexpected(ErrorCode::InternalError, - "vector instruction became detached during legalization"); - } - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::lowerVAArg(MachineInstr &Instruction) { @@ -4496,10 +4399,7 @@ Expected<void> LegalizerHelper::lowerVAArg(MachineInstr &Instruction) { return std::unexpected(Built.error()); } - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::VerificationError, "G_VAARG is detached during legalization"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::lowerLoad(MachineInstr &Instruction) { @@ -4629,10 +4529,7 @@ Expected<void> LegalizerHelper::lowerLoad(MachineInstr &Instruction) { if (auto Truncated = MIRBuilder.buildTrunc(*Destination, LoadDestination); !Truncated) return std::unexpected(Truncated.error()); } - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, "load became detached during lowering"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } std::uint64_t LargeSplitBits = 0; @@ -4718,10 +4615,7 @@ Expected<void> LegalizerHelper::lowerLoad(MachineInstr &Instruction) { } } - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, "load became detached during lowering"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::lowerStore(MachineInstr &Instruction) { @@ -4821,10 +4715,7 @@ Expected<void> LegalizerHelper::lowerStore(MachineInstr &Instruction) { return std::unexpected(WideMemory.error()); if (auto Built = BuildStore(*ZeroExtended, *Address, **WideMemory); !Built) return std::unexpected(Built.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, "store became detached during lowering"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } std::uint64_t LargeSplitBits = 0; @@ -4900,10 +4791,7 @@ Expected<void> LegalizerHelper::lowerStore(MachineInstr &Instruction) { if (auto Built = BuildStore(*SmallValue, *SmallAddress, **SmallMemory); !Built) return std::unexpected(Built.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, "store became detached during lowering"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::lower(MachineInstr &Instruction, @@ -5045,12 +4933,7 @@ Expected<void> LegalizerHelper::lower(MachineInstr &Instruction, if (auto Built = MIRBuilder.buildAShr(Destination, *Temporary, *ShiftAmount); !Built) return std::unexpected(Built.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) { - return makeUnexpected(ErrorCode::InternalError, - "G_SEXT_INREG became detached during lowering"); - } - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } case GenericOpcode::G_VAARG: return lowerVAArg(Instruction); @@ -5088,12 +4971,7 @@ Expected<void> LegalizerHelper::lower(MachineInstr &Instruction, if (auto Built = MIRBuilder.buildCopy(OldValue, *NewOldValue); !Built) return std::unexpected(Built.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) { - return makeUnexpected(ErrorCode::InternalError, - "G_ATOMIC_CMPXCHG_WITH_SUCCESS became detached during lowering"); - } - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } case GenericOpcode::G_ATOMICRMW_SUB: { const Register Result = *Instruction.operandAt(0)->getIf<Register>(); @@ -5127,11 +5005,7 @@ Expected<void> LegalizerHelper::lower(MachineInstr &Instruction, if (auto Built = AtomicAdd->build(); !Built) return std::unexpected(Built.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "G_ATOMICRMW_SUB became detached during lowering"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } case GenericOpcode::G_UADDO: { const Register Result = *Instruction.operandAt(0)->getIf<Register>(); @@ -5151,10 +5025,7 @@ Expected<void> LegalizerHelper::lower(MachineInstr &Instruction, return std::unexpected(Built.error()); if (auto Built = MIRBuilder.buildCopy(Result, *NewResult); !Built) return std::unexpected(Built.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, "G_UADDO became detached during lowering"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } case GenericOpcode::G_UADDE: { const Register Result = *Instruction.operandAt(0)->getIf<Register>(); @@ -5211,10 +5082,7 @@ Expected<void> LegalizerHelper::lower(MachineInstr &Instruction, return std::unexpected(Built.error()); if (auto Built = MIRBuilder.buildCopy(Result, *NewResult); !Built) return std::unexpected(Built.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, "G_UADDE became detached during lowering"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } case GenericOpcode::G_USUBO: { const Register Result = *Instruction.operandAt(0)->getIf<Register>(); @@ -5229,10 +5097,7 @@ Expected<void> LegalizerHelper::lower(MachineInstr &Instruction, MIRBuilder.buildICmp(IntegerPredicate::UnsignedLessThan, BorrowOut, Left, Right); !Built) return std::unexpected(Built.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, "G_USUBO became detached during lowering"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } case GenericOpcode::G_USUBE: { const Register Result = *Instruction.operandAt(0)->getIf<Register>(); @@ -5284,10 +5149,7 @@ Expected<void> LegalizerHelper::lower(MachineInstr &Instruction, return std::unexpected(Built.error()); if (auto Built = MIRBuilder.buildOr(BorrowOut, *Borrow, *SecondBorrow); !Built) return std::unexpected(Built.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, "G_USUBE became detached during lowering"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } default: { const InstructionDescriptor *Descriptor = @@ -5396,11 +5258,7 @@ Expected<void> LegalizerHelper::lowerDynStackAlloc(MachineInstr &Instruction) { if (auto Built = MIRBuilder.buildCopy(*Destination, *TargetPointer); !Built) return std::unexpected(Built.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "G_DYN_STACKALLOC became detached during lowering"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::lowerStackSave(MachineInstr &Instruction) { @@ -5420,10 +5278,7 @@ Expected<void> LegalizerHelper::lowerStackSave(MachineInstr &Instruction) { !Built) { return std::unexpected(Built.error()); } - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, "G_STACKSAVE became detached during lowering"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::lowerStackRestore(MachineInstr &Instruction) { @@ -5440,12 +5295,7 @@ Expected<void> LegalizerHelper::lowerStackRestore(MachineInstr &Instruction) { return std::unexpected(Point.error()); if (auto Built = MIRBuilder.buildCopy(Register::fromPhysical(StackPointer), *Source); !Built) return std::unexpected(Built.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) { - return makeUnexpected(ErrorCode::InternalError, - "G_STACKRESTORE became detached during lowering"); - } - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::lowerMemcpyInline(MachineInstr &Instruction) { @@ -5485,11 +5335,7 @@ Expected<void> LegalizerHelper::lowerMemcpyInline(MachineInstr &Instruction) { } const std::uint64_t KnownLength = ConstantLength->Value.words().front(); if (KnownLength == 0) { - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "G_MEMCPY_INLINE became detached during legalization"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } auto ByteType = LowLevelType::makeCategoryNeutralScalar(8); @@ -5540,11 +5386,7 @@ Expected<void> LegalizerHelper::lowerMemcpyInline(MachineInstr &Instruction) { return std::unexpected(Built.error()); } - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "G_MEMCPY_INLINE became detached during legalization"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::lowerAbsToAddXor(MachineInstr &Instruction) { @@ -5574,10 +5416,7 @@ Expected<void> LegalizerHelper::lowerAbsToAddXor(MachineInstr &Instruction) { if (auto Built = MIRBuilder.buildXor(Destination, *Add, *Shift); !Built) return std::unexpected(Built.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, "G_ABS became detached during lowering"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::lowerAbsDiffToSelect(MachineInstr &Instruction) { @@ -5618,11 +5457,7 @@ Expected<void> LegalizerHelper::lowerAbsDiffToSelect(MachineInstr &Instruction) return std::unexpected(Built.error()); } - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "G_ABDS/G_ABDU became detached during select lowering"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::lowerAbsDiffToMinMax(MachineInstr &Instruction) { @@ -5652,11 +5487,7 @@ Expected<void> LegalizerHelper::lowerAbsDiffToMinMax(MachineInstr &Instruction) if (auto Built = MIRBuilder.buildSub(Destination, *Maximum, *Minimum); !Built) return std::unexpected(Built.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "G_ABDS/G_ABDU became detached during min/max lowering"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::lowerMinMax(MachineInstr &Instruction) { @@ -5686,11 +5517,7 @@ Expected<void> LegalizerHelper::lowerMinMax(MachineInstr &Instruction) { return std::unexpected(Built.error()); } - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "integer min/max became detached during lowering"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::lowerThreewayCompare(MachineInstr &Instruction) { @@ -5770,11 +5597,7 @@ Expected<void> LegalizerHelper::lowerThreewayCompare(MachineInstr &Instruction) return std::unexpected(Built.error()); } - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "three-way comparison became detached during lowering"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::lowerDivRem(MachineInstr &Instruction) { @@ -5797,10 +5620,7 @@ Expected<void> LegalizerHelper::lowerDivRem(MachineInstr &Instruction) { return std::unexpected(Built.error()); } - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, "DIVREM became detached during lowering"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::lowerHighMultiply(MachineInstr &Instruction) { @@ -5867,11 +5687,7 @@ Expected<void> LegalizerHelper::lowerHighMultiply(MachineInstr &Instruction) { if (auto Built = MIRBuilder.buildTrunc(*Destination, *Shifted); !Built) return std::unexpected(Built.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "high multiply instruction became detached during lowering"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::lowerMulo(MachineInstr &Instruction) { @@ -5974,11 +5790,7 @@ Expected<void> LegalizerHelper::lowerBitCount(MachineInstr &Instruction) { Decision->Action == LegalizationAction::Custom; }; const auto EraseOriginal = [&Instruction]() -> Expected<void> { - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "bit-count instruction became detached during lowering"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); }; switch (Generic) { @@ -6397,10 +6209,7 @@ Expected<void> LegalizerHelper::lowerBswap(MachineInstr &Instruction) { } } - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, "G_BSWAP became detached during lowering"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::lowerFunnelShift(MachineInstr &Instruction) { @@ -6514,11 +6323,7 @@ Expected<void> LegalizerHelper::lowerFunnelShift(MachineInstr &Instruction) { if (auto Built = Result->build(); !Built) return std::unexpected(Built.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) { - return makeUnexpected(ErrorCode::InternalError, "funnel shift became detached during lowering"); - } - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::lowerRotate(MachineInstr &Instruction) { @@ -6618,10 +6423,7 @@ Expected<void> LegalizerHelper::lowerRotate(MachineInstr &Instruction) { if (auto Built = Result->build(); !Built) return std::unexpected(Built.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, "rotate became detached during lowering"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::libcall(MachineInstr &Instruction) { @@ -6810,10 +6612,7 @@ Expected<void> LegalizerHelper::libcall(MachineInstr &Instruction) { return std::unexpected(Merged.error()); } - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, "libcall became detached during legalization"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::lowerSignedAddSubOverflow(MachineInstr &Instruction) { @@ -6909,11 +6708,7 @@ Expected<void> LegalizerHelper::lowerSignedAddSubOverflow(MachineInstr &Instruct return std::unexpected(Built.error()); } - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "signed extended add/sub instruction became detached during lowering"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } const bool IsAdd = Instruction.opcode() == opcode(GenericOpcode::G_SADDO); @@ -6978,11 +6773,7 @@ Expected<void> LegalizerHelper::lowerSignedAddSubOverflow(MachineInstr &Instruct if (auto Built = MIRBuilder.buildCopy(Destination, *Result); !Built) return std::unexpected(Built.error()); - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "signed add/sub overflow instruction became detached during lowering"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::lowerSaturatingAddSubToMinMax(MachineInstr &Instruction) { @@ -7118,11 +6909,7 @@ Expected<void> LegalizerHelper::lowerSaturatingAddSubToMinMax(MachineInstr &Inst } } - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "saturating add/sub became detached during min/max lowering"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::lowerSaturatingAddSubToOverflow(MachineInstr &Instruction) { @@ -7213,11 +7000,7 @@ Expected<void> LegalizerHelper::lowerSaturatingAddSubToOverflow(MachineInstr &In !Built) { return std::unexpected(Built.error()); } - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "saturating add/sub became detached during overflow lowering"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::lowerSaturatingShiftLeft(MachineInstr &Instruction) { @@ -7305,11 +7088,7 @@ Expected<void> LegalizerHelper::lowerSaturatingShiftLeft(MachineInstr &Instructi return std::unexpected(Built.error()); } - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, - "saturating shift-left became detached during lowering"); - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } Expected<void> LegalizerHelper::legalize(MachineInstr &Instruction, diff --git a/backend/lib/CodeGen/GlobalISel/RegBankSelect.cpp b/backend/lib/CodeGen/GlobalISel/RegBankSelect.cpp index 3b7932b..b09fdac 100644 --- a/backend/lib/CodeGen/GlobalISel/RegBankSelect.cpp +++ b/backend/lib/CodeGen/GlobalISel/RegBankSelect.cpp @@ -169,7 +169,7 @@ struct MappingMutation final { auto Replaced = Operand.setRegister(*Fresh); if (!Replaced) { if ((*Copy)->parent() != nullptr) { - auto Removed = (*Copy)->parent()->removeInstruction(**Copy); + auto Removed = (*Copy)->eraseFromParent(); (void)Removed; } auto Erased = Function.registerInfo().eraseVirtualRegister(*Fresh); @@ -188,7 +188,7 @@ struct MappingMutation final { if (!Restored) return std::unexpected(Restored.error()); if (Iterator->Copy != nullptr && Iterator->Copy->parent() != nullptr) { - auto Removed = Iterator->Copy->parent()->removeInstruction(*Iterator->Copy); + auto Removed = Iterator->Copy->eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); } diff --git a/backend/lib/CodeGen/GlobalISel/Utils.cpp b/backend/lib/CodeGen/GlobalISel/Utils.cpp index 7e594ce..0f8d638 100644 --- a/backend/lib/CodeGen/GlobalISel/Utils.cpp +++ b/backend/lib/CodeGen/GlobalISel/Utils.cpp @@ -65,10 +65,7 @@ namespace { if (Definition != nullptr && Definition != &Instruction) DeadChain.push_back(Definition); } - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return {}; - return Block->removeInstruction(Instruction); + return Instruction.eraseFromParent(); } [[nodiscard]] Expected<LowLevelType> makeVectorType(std::uint64_t ElementCount, bool IsScalable, @@ -248,8 +245,9 @@ Expected<void> eraseInstrs(std::span<MachineInstr *const> Instructions, MachineR const TargetDescription &Target) { std::vector<MachineInstr *> DeadChain; for (MachineInstr *Instruction : Instructions) { - if (Instruction == nullptr || Instruction->parent() == nullptr) - continue; + if (Instruction == nullptr) + return makeUnexpected(ErrorCode::InvalidArgument, + "instruction erasure list contains a null instruction"); auto Removed = saveUsesAndErase(*Instruction, MRI, DeadChain); if (!Removed) return std::unexpected(Removed.error()); @@ -258,8 +256,7 @@ Expected<void> eraseInstrs(std::span<MachineInstr *const> Instructions, MachineR while (!DeadChain.empty()) { MachineInstr *Instruction = DeadChain.back(); DeadChain.pop_back(); - if (Instruction == nullptr || Instruction->parent() == nullptr || - !isTriviallyDead(*Instruction, MRI, Target)) + if (Instruction->parent() == nullptr || !isTriviallyDead(*Instruction, MRI, Target)) continue; auto Removed = saveUsesAndErase(*Instruction, MRI, DeadChain); if (!Removed) diff --git a/backend/lib/CodeGen/InlineSpiller.cpp b/backend/lib/CodeGen/InlineSpiller.cpp index 4aa43ce..4243e84 100644 --- a/backend/lib/CodeGen/InlineSpiller.cpp +++ b/backend/lib/CodeGen/InlineSpiller.cpp @@ -80,8 +80,8 @@ Expected<std::optional<std::vector<Register>>> Spiller::tryRematerialize(Registe if (ActionAnalyses != nullptr) { for (const UseSite &Site : Sites) { - auto Accepted = ActionAnalyses->requestAction("rematerialize", *Function, - Site.Instruction->stableID()); + auto Accepted = + ActionAnalyses->requestAction("rematerialize", *Function, Site.Instruction->stableID()); if (!Accepted) return std::unexpected(Accepted.error()); if (!*Accepted) @@ -123,8 +123,7 @@ Expected<std::optional<std::vector<Register>>> Spiller::tryRematerialize(Registe } } if (!HasNonDebugUse) { - MachineBasicBlock *Block = const_cast<MachineBasicBlock *>(DefiningInstruction->parent()); - auto Removed = Block->removeInstruction(*const_cast<MachineInstr *>(DefiningInstruction)); + auto Removed = const_cast<MachineInstr *>(DefiningInstruction)->eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); } @@ -288,9 +287,9 @@ Expected<std::vector<Register>> Spiller::spill(Register VirtualReg) { if (ActionAnalyses != nullptr) { const MachineOperand *Definition = Function->registerInfo().getUniqueVRegDef(VirtualReg); const MachineInstr *Instruction = Definition == nullptr ? nullptr : Definition->parent(); - auto Accepted = ActionAnalyses->requestAction( - "spill", *Function, - Instruction == nullptr ? MachineInstrStableID{} : Instruction->stableID()); + auto Accepted = ActionAnalyses->requestAction("spill", *Function, + Instruction == nullptr ? MachineInstrStableID{} + : Instruction->stableID()); if (!Accepted) return std::unexpected(Accepted.error()); if (!*Accepted) { diff --git a/backend/lib/CodeGen/MachineCFGUpdater.cpp b/backend/lib/CodeGen/MachineCFGUpdater.cpp index 4aacdf5..050071e 100644 --- a/backend/lib/CodeGen/MachineCFGUpdater.cpp +++ b/backend/lib/CodeGen/MachineCFGUpdater.cpp @@ -489,7 +489,7 @@ Expected<MachineBasicBlock *> MachineCFGUpdater::splitBlock(MachineBasicBlock &B MachineInstr *Instruction = &SplitBefore; while (Instruction != nullptr) { MachineInstr *Next = Instruction->next(); - auto Removed = Block.removeInstruction(*Instruction); + auto Removed = Instruction->eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); auto Appended = NewBlock->appendInstruction(*Instruction); diff --git a/backend/lib/CodeGen/MachineCSE.cpp b/backend/lib/CodeGen/MachineCSE.cpp index 7bd5a22..353cbaa 100644 --- a/backend/lib/CodeGen/MachineCSE.cpp +++ b/backend/lib/CodeGen/MachineCSE.cpp @@ -258,15 +258,12 @@ replacementPairs(const MachineInstr &Available, const MachineInstr &Redundant, replaceAndErase(MachineInstr &Redundant, const std::vector<std::pair<Register, Register>> &Replacements, MachineRegisterInfo &Registers) { - MachineBasicBlock *Block = Redundant.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, "machine CSE candidate is detached"); for (const auto &[OldReg, NewReg] : Replacements) { auto Replaced = Registers.replaceRegWith(OldReg, NewReg); if (!Replaced) return std::unexpected(Replaced.error()); } - auto Removed = Block->removeInstruction(Redundant); + auto Removed = Redundant.eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); for (const auto &[OldReg, NewReg] : Replacements) { diff --git a/backend/lib/CodeGen/MachineCombiner.cpp b/backend/lib/CodeGen/MachineCombiner.cpp index 4c2ff88..4341700 100644 --- a/backend/lib/CodeGen/MachineCombiner.cpp +++ b/backend/lib/CodeGen/MachineCombiner.cpp @@ -175,18 +175,11 @@ namespace { if (!Fused) return std::unexpected(Fused.error()); - MachineBasicBlock *Block = Root.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, "machine combiner root is detached"); - auto Removed = Block->removeInstruction(Root); + auto Removed = Root.eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); if (EraseProducer) { - MachineBasicBlock *ProducerBlock = Producer.parent(); - if (ProducerBlock == nullptr) { - return makeUnexpected(ErrorCode::InternalError, "machine combiner producer is detached"); - } - Removed = ProducerBlock->removeInstruction(Producer); + Removed = Producer.eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); } diff --git a/backend/lib/CodeGen/MachineCopyPropagation.cpp b/backend/lib/CodeGen/MachineCopyPropagation.cpp index 9cb64cb..94d83e3 100644 --- a/backend/lib/CodeGen/MachineCopyPropagation.cpp +++ b/backend/lib/CodeGen/MachineCopyPropagation.cpp @@ -232,7 +232,7 @@ struct CopyOperands final { if (*Destination == *Source) { if (!IsGenericCopy) return false; - auto Removed = Block->removeInstruction(Copy); + auto Removed = Copy.eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); return true; @@ -285,7 +285,7 @@ struct CopyOperands final { if (IsGenericCopy && Operands.DestinationOperand->registerFlags().has(RegisterOperandFlag::Renamable) && !valueUsedAfter(Copy, *Destination, Target)) { - auto Removed = Block->removeInstruction(Copy); + auto Removed = Copy.eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); Changed = true; @@ -326,8 +326,7 @@ MachineCopyPropagationPass::runImplementation(MachineFunction &Function, Instruction = Next; continue; } - auto Accepted = - Analyses.requestAction("propagate-copy", Function, Instruction->stableID()); + auto Accepted = Analyses.requestAction("propagate-copy", Function, Instruction->stableID()); if (!Accepted) return std::unexpected(Accepted.error()); if (!*Accepted) { diff --git a/backend/lib/CodeGen/MachineDCE.cpp b/backend/lib/CodeGen/MachineDCE.cpp index 269999b..734509f 100644 --- a/backend/lib/CodeGen/MachineDCE.cpp +++ b/backend/lib/CodeGen/MachineDCE.cpp @@ -140,7 +140,7 @@ Expected<MachinePassResult> MachineDCEPass::runImplementation(MachineFunction &F if (!Cleared) return std::unexpected(Cleared.error()); } - auto Removed = Block->removeInstruction(*Instruction); + auto Removed = Instruction->eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); for (const Register Reg : DefinitionList) { diff --git a/backend/lib/CodeGen/MachineInstr.cpp b/backend/lib/CodeGen/MachineInstr.cpp index 55b4ce9..3b200c5 100644 --- a/backend/lib/CodeGen/MachineInstr.cpp +++ b/backend/lib/CodeGen/MachineInstr.cpp @@ -43,6 +43,12 @@ Expected<void> MachineInstr::setOpcode(Opcode NewOpcode) { return {}; } +Expected<void> MachineInstr::eraseFromParent() { + if (ParentBlock == nullptr) + return makeUnexpected(ErrorCode::InternalError, "cannot erase a detached machine instruction"); + return ParentBlock->removeInstruction(*this); +} + Expected<void> MachineInstr::setDebugInstrNumber(std::uint64_t Number) { if (Number == 0 || Number > std::numeric_limits<std::uint32_t>::max()) { return makeUnexpected(ErrorCode::InvalidArgument, diff --git a/backend/lib/CodeGen/MachineLICM.cpp b/backend/lib/CodeGen/MachineLICM.cpp index 6ab15dc..3863388 100644 --- a/backend/lib/CodeGen/MachineLICM.cpp +++ b/backend/lib/CodeGen/MachineLICM.cpp @@ -303,7 +303,7 @@ operandsAreInvariant(const MachineInstr &Instruction, const MachineLoop &Loop, auto Anchor = firstTerminator(Preheader, Target); if (!Anchor) return std::unexpected(Anchor.error()); - auto Removed = Source->removeInstruction(Instruction); + auto Removed = Instruction.eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); if (*Anchor != nullptr) diff --git a/backend/lib/CodeGen/MachineOutliner.cpp b/backend/lib/CodeGen/MachineOutliner.cpp index bdc4d2b..a36a740 100644 --- a/backend/lib/CodeGen/MachineOutliner.cpp +++ b/backend/lib/CodeGen/MachineOutliner.cpp @@ -473,7 +473,7 @@ buildSequenceGroups(std::vector<InstructionSegment> &Segments, if (!Inserted) return std::unexpected(Inserted.error()); for (MachineInstr *Instruction : Candidate.Instructions) { - auto Removed = Candidate.Block->removeInstruction(*Instruction); + auto Removed = Instruction->eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); } diff --git a/backend/lib/CodeGen/MachineScheduler.cpp b/backend/lib/CodeGen/MachineScheduler.cpp index dfc7b24..65f1e01 100644 --- a/backend/lib/CodeGen/MachineScheduler.cpp +++ b/backend/lib/CodeGen/MachineScheduler.cpp @@ -302,7 +302,7 @@ scheduleRegion(MachineBasicBlock &Block, std::span<MachineInstr *const> Instruct MachineInstr *Anchor = Instructions.back()->next(); for (MachineInstr *Instruction : Instructions) { - auto Removed = Block.removeInstruction(*Instruction); + auto Removed = Instruction->eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); } @@ -323,8 +323,7 @@ runMachineScheduler(MachineFunction &Function, const TargetDescription &Target, const TargetSchedulingModel &Model, ScheduleDAGMode Mode, const LiveVariablesAnalysis::Result &Liveness, const MachineBlockFrequencyAnalysis::Result &Frequencies, - MachineSchedulerOptions Options, - MachineFunctionAnalysisManager &Analyses) { + MachineSchedulerOptions Options, MachineFunctionAnalysisManager &Analyses) { if (Options.MaximumRegionSize == 0) { return makeUnexpected(ErrorCode::InvalidArgument, "machine scheduler region budget must be nonzero"); diff --git a/backend/lib/CodeGen/MachineSink.cpp b/backend/lib/CodeGen/MachineSink.cpp index dda2048..f82f3a7 100644 --- a/backend/lib/CodeGen/MachineSink.cpp +++ b/backend/lib/CodeGen/MachineSink.cpp @@ -327,7 +327,7 @@ findPostRADestination(MachineInstr &Instruction, const MachineFunction &Function MachineBasicBlock *Source = Instruction.parent(); if (Source == nullptr || Destination.Block == nullptr || Destination.FirstUse == nullptr) return makeUnexpected(ErrorCode::InternalError, "machine sink has an invalid move plan"); - auto Removed = Source->removeInstruction(Instruction); + auto Removed = Instruction.eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); return Destination.Block->insertInstructionBefore(*Destination.FirstUse, Instruction); diff --git a/backend/lib/CodeGen/MachineStripDebug.cpp b/backend/lib/CodeGen/MachineStripDebug.cpp index 4699ef0..46d92d1 100644 --- a/backend/lib/CodeGen/MachineStripDebug.cpp +++ b/backend/lib/CodeGen/MachineStripDebug.cpp @@ -14,7 +14,7 @@ Expected<bool> stripDebugMachineFunction(MachineFunction &Function) { for (MachineInstr *Instruction = Block->firstInstruction(); Instruction != nullptr;) { MachineInstr *Next = Instruction->next(); if (Instruction->isDebugInstr() && Instruction->operandCount() > 1) { - auto Removed = Block->removeInstruction(*Instruction); + auto Removed = Instruction->eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); Changed = true; diff --git a/backend/lib/CodeGen/PHIElimination.cpp b/backend/lib/CodeGen/PHIElimination.cpp index 677a04c..8ad785e 100644 --- a/backend/lib/CodeGen/PHIElimination.cpp +++ b/backend/lib/CodeGen/PHIElimination.cpp @@ -479,10 +479,9 @@ Expected<MachinePassResult> PHIEliminationPass::run(MachineFunction &Function, return runImplementation(Function, Target, Analyses); } -Expected<MachinePassResult> PHIEliminationPass::runImplementation(MachineFunction &Function, - const TargetDescription &Target, - MachineFunctionAnalysisManager - &Analyses) { +Expected<MachinePassResult> +PHIEliminationPass::runImplementation(MachineFunction &Function, const TargetDescription &Target, + MachineFunctionAnalysisManager &Analyses) { MachinePassResult Result; std::unordered_map<EdgeKey, MachineBasicBlock *, EdgeKeyHash> SplitEdges; const std::vector<MachineBasicBlock *> Blocks(Function.allBlocks().begin(), @@ -552,7 +551,7 @@ Expected<MachinePassResult> PHIEliminationPass::runImplementation(MachineFunctio } for (PhiPlan &Plan : *Plans) { - auto Removed = Block->removeInstruction(*Plan.Instruction); + auto Removed = Plan.Instruction->eraseFromParent(); if (!Removed) return std::unexpected(Removed.error()); } diff --git a/backend/lib/CodeGen/TwoAddressInstructionPass.cpp b/backend/lib/CodeGen/TwoAddressInstructionPass.cpp index 80da2e8..d7a5ef1 100644 --- a/backend/lib/CodeGen/TwoAddressInstructionPass.cpp +++ b/backend/lib/CodeGen/TwoAddressInstructionPass.cpp @@ -175,13 +175,6 @@ buildImplicitDef(MachineFunction &Function, const TargetDescription &Target, return *Composed; } -[[nodiscard]] Expected<void> removeInstruction(MachineInstr &Instruction) { - MachineBasicBlock *Block = Instruction.parent(); - if (Block == nullptr) - return makeUnexpected(ErrorCode::InternalError, "rewritten instruction is not linked"); - return Block->removeInstruction(Instruction); -} - [[nodiscard]] Expected<void> expandRegSequence(MachineInstr &Instruction, MachineFunction &Function, const TargetDescription &Target, const TargetInstrInfo &TargetInfo) { @@ -240,7 +233,7 @@ buildImplicitDef(MachineFunction &Function, const TargetDescription &Target, if (!Copy) return std::unexpected(Copy.error()); } - return removeInstruction(Instruction); + return Instruction.eraseFromParent(); } [[nodiscard]] Expected<void> expandInsertSubreg(MachineInstr &Instruction, @@ -309,7 +302,7 @@ buildImplicitDef(MachineFunction &Function, const TargetDescription &Target, if (!Copy) return std::unexpected(Copy.error()); } - return removeInstruction(Instruction); + return Instruction.eraseFromParent(); } [[nodiscard]] Expected<void> swapRegisterUses(MachineOperand &Left, MachineOperand &Right) { @@ -450,9 +443,10 @@ TwoAddressInstructionPass::run(MachineFunction &Function, const TargetDescriptio return runImplementation(Function, Target, Analyses); } -Expected<MachinePassResult> TwoAddressInstructionPass::runImplementation( - MachineFunction &Function, const TargetDescription &Target, - MachineFunctionAnalysisManager &Analyses) { +Expected<MachinePassResult> +TwoAddressInstructionPass::runImplementation(MachineFunction &Function, + const TargetDescription &Target, + MachineFunctionAnalysisManager &Analyses) { MachinePassResult Result; const std::vector<MachineInstr *> Instructions(Function.allInstructions().begin(), Function.allInstructions().end()); 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<void> 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<void> 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<MachineInstr *>(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<void> 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<void> RISCVLegalizerInfo::legalizeVAStart(MachineInstr &Instruction, @@ -984,11 +972,7 @@ Expected<void> 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<void> RISCVLegalizerInfo::legalizeCustom(LegalizerHelper &Helper, @@ -1243,11 +1227,7 @@ Expected<void> 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<bool> 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<void> 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<void> 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<void> 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<MachineInstr *> 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<bool> 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<void> 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<std::pair<Register, Register>> 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<Register> 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); |
