Skip to content

Commit ca4c41a

Browse files
alexmalyshevmeta-codesync[bot]
authored andcommitted
Simplify simplifyRedundantCondBranch
Summary: No need to construct a vector of branches to process, they can be processed as they are discovered. Don't abort on unrecognized branches, they should just be skipped instead. Make deleting the old branch a little more exception-resilient by using std::unique_ptr. Reviewed By: mpage Differential Revision: D114321698 fbshipit-source-id: 1032e686dd54f9907bfdc028d58eeecd3d6858b6
1 parent b8fc9ff commit ca4c41a

1 file changed

Lines changed: 15 additions & 18 deletions

File tree

cinderx/Jit/hir/pass.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -856,38 +856,35 @@ bool removeUnreachableInstructions(Function& func) {
856856
}
857857

858858
void simplifyRedundantCondBranches(CFG* cfg) {
859-
std::vector<BasicBlock*> to_simplify;
860-
for (auto& block : cfg->blocks) {
859+
for (BasicBlock& block : cfg->blocks) {
861860
if (block.empty()) {
862861
continue;
863862
}
864-
auto term = block.getTerminator();
865-
std::size_t num_edges = term->numEdges();
866-
if (num_edges < 2) {
863+
864+
// Only optimize branches with two identical successors.
865+
Instr* term = block.getTerminator();
866+
if (term->numEdges() != 2) {
867867
continue;
868868
}
869-
JIT_CHECK(num_edges == 2, "only two edges are supported");
870-
if (term->successor(0) != term->successor(1)) {
869+
BasicBlock* succ0 = term->successor(0);
870+
if (succ0 != term->successor(1)) {
871871
continue;
872872
}
873+
874+
// Verify the instruction is known to be safe to replace.
873875
switch (term->opcode()) {
874876
case Opcode::kCondBranch:
875877
case Opcode::kCondBranchIterNotDone:
876878
case Opcode::kCondBranchCheckType:
877879
break;
878880
default:
879-
// Can't be sure that it's safe to replace the instruction with a branch
880-
JIT_ABORT("Unknown side effects of {} instruction", term->opname());
881+
continue;
881882
}
882-
to_simplify.emplace_back(&block);
883-
}
884-
for (auto& block : to_simplify) {
885-
auto term = block->getTerminator();
886-
term->unlink();
887-
auto branch = block->appendWithOff<Branch>(
888-
term->bytecodeOffset(), term->successor(0));
889-
branch->copyBytecodeOffset(*term);
890-
delete term;
883+
884+
// Replace with an unconditional branch.
885+
block.remove(*term);
886+
std::unique_ptr<Instr> deleter{term};
887+
block.appendWithOff<Branch>(term->bytecodeOffset(), succ0);
891888
}
892889
}
893890

0 commit comments

Comments
 (0)