fix(peephole): remove dead conditional branch inversion code\n\nThe CondBr+Branch inversion pattern was unreachable because the\nsimple Br fallthrough check runs first and removes the Br. Removed\nthe dead code and the unused NegateCondCode helper.

master
黄熙哲 1 week ago
parent 7490fd3a49
commit e26fd3f520

@ -121,20 +121,6 @@ namespace mir
return true;
}
static CondCode NegateCondCode(CondCode cond)
{
switch (cond)
{
case CondCode::EQ: return CondCode::NE;
case CondCode::NE: return CondCode::EQ;
case CondCode::LT: return CondCode::GE;
case CondCode::LE: return CondCode::GT;
case CondCode::GT: return CondCode::LE;
case CondCode::GE: return CondCode::LT;
default: return CondCode::NE;
}
}
static void RunPeepholeOnBlock(MachineBasicBlock &block,
const MachineFunction &function)
{
@ -248,42 +234,6 @@ namespace mir
}
}
// 条件分支翻转: CondBr cond, L1 + Br L2, 且 L2 是紧邻后继
if (insts.size() >= 2)
{
auto it_last = std::prev(insts.end());
auto it_prev = std::prev(it_last);
if (it_prev->GetOpcode() == Opcode::CondBr &&
it_last->GetOpcode() == Opcode::Br)
{
const auto &cond_ops = it_prev->GetOperands();
const auto &br_ops = it_last->GetOperands();
if (cond_ops.size() >= 2 && br_ops.size() >= 1 &&
cond_ops[0].GetKind() == Operand::Kind::Imm &&
cond_ops[1].GetKind() == Operand::Kind::Label &&
br_ops[0].GetKind() == Operand::Kind::Label)
{
int fallthrough_label = br_ops[0].GetLabel();
const auto &blocks = function.GetBlocks();
for (size_t bi = 0; bi < blocks.size(); ++bi)
{
if (blocks[bi].get() == &block && bi + 1 < blocks.size())
{
if (blocks[bi + 1]->GetLabelId() == fallthrough_label)
{
CondCode old_cond = static_cast<CondCode>(cond_ops[0].GetImm());
CondCode new_cond = NegateCondCode(old_cond);
auto &mutable_ops = const_cast<std::vector<Operand>&>(
it_prev->GetOperands());
mutable_ops[0] = Operand::Imm(static_cast<int>(new_cond));
insts.pop_back();
}
break;
}
}
}
}
}
}
} // namespace

Loading…
Cancel
Save