aboutsummaryrefslogtreecommitdiff
path: root/backend/lib/Target/RISCV/GISel
diff options
context:
space:
mode:
authorRichard-Qin-X <richard.qin.001@gmail.com>2026-08-28 13:45:07 +0800
committerRichard-Qin-X <richard.qin.001@gmail.com>2026-08-28 13:45:07 +0800
commite72ad2cd524ff38f162a23d69da5f78494d337ae (patch)
tree2e590806b0f610e4392f25f6e8cdff57d2f5a929 /backend/lib/Target/RISCV/GISel
parent3b14bd4d5324b5a280c1ee5567b5526b59e60c5b (diff)
downloadrcc-project-e72ad2cd524ff38f162a23d69da5f78494d337ae.tar.gz
rcc-project-e72ad2cd524ff38f162a23d69da5f78494d337ae.tar.xz
[CodeGen] Centralize machine instruction erasure
Diffstat (limited to 'backend/lib/Target/RISCV/GISel')
-rw-r--r--backend/lib/Target/RISCV/GISel/RISCVInstructionSelector.cpp18
-rw-r--r--backend/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp36
2 files changed, 9 insertions, 45 deletions
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;
}