[midend]修改了removeinst方法,应对不同的使用情况,增加user析构函数使得user对象销毁顺带销毁其use关系销毁,重构usedelete方法封装指令删除和use关系删除

This commit is contained in:
rain2133
2025-07-31 21:10:20 +08:00
parent aed4577490
commit ef09bc70d4
2 changed files with 86 additions and 14 deletions

View File

@ -599,7 +599,7 @@ public:
prev->addSuccessor(next);
next->addPredecessor(prev);
}
void removeInst(iterator pos) { instructions.erase(pos); }
iterator removeInst(iterator pos) { return instructions.erase(pos); }
void removeInst(Instruction *inst) {
auto pos = std::find_if(instructions.begin(), instructions.end(),
[inst](const std::unique_ptr<Instruction> &i) { return i.get() == inst; });
@ -626,6 +626,21 @@ class User : public Value {
explicit User(Type *type, const std::string &name = "") : Value(type, name) {}
public:
~User() override {
// 当 User 对象被销毁时例如LoadInst 或 StoreInst 被删除时),
// 它必须通知它所使用的所有 Value将对应的 Use 关系从它们的 uses 列表中移除。
// 这样可以防止 Value 的 uses 列表中出现悬空的 Use 对象。
for (const auto &use_ptr : operands) {
// 确保 use_ptr 非空,并且其内部指向的 Value* 也非空
// (虽然通常情况下不会为空,但为了健壮性考虑)
if (use_ptr && use_ptr->getValue()) {
use_ptr->getValue()->removeUse(use_ptr);
}
}
// operands 向量本身是 std::vector<std::shared_ptr<Use>>
// 在此析构函数结束后operands 向量会被销毁,其内部的 shared_ptr 也会被释放,
// 如果 shared_ptr 引用计数降为0Use 对象本身也会被销毁。
}
unsigned getNumOperands() const { return operands.size(); } ///< 获取操作数数量
auto operand_begin() const { return operands.begin(); } ///< 返回操作数列表的开头迭代器
auto operand_end() const { return operands.end(); } ///< 返回操作数列表的结尾迭代器