shader: Fix forward referencing identity instructions when inserting phi

This commit is contained in:
ReinUsesLisp 2021-04-22 18:33:49 -03:00 committed by ameerj
parent 92a01984e6
commit 25949b864c

View File

@ -278,20 +278,22 @@ private:
} }
same = op; same = op;
} }
if (same.IsEmpty()) { // Remove the phi node from the block, it will be reinserted
// The phi is unreachable or in the start block
// First remove the phi node from the block, it will be reinserted
IR::Block::InstructionList& list{block->Instructions()}; IR::Block::InstructionList& list{block->Instructions()};
list.erase(IR::Block::InstructionList::s_iterator_to(phi)); list.erase(IR::Block::InstructionList::s_iterator_to(phi));
// Insert an undef instruction after all phi nodes (to keep phi instructions on top) // Find the first non-phi instruction and use it as an insertion point
const auto first_not_phi{std::ranges::find_if_not(list, IsPhi)}; IR::Block::iterator reinsert_point{std::ranges::find_if_not(list, IsPhi)};
same = IR::Value{&*block->PrependNewInst(first_not_phi, undef_opcode)}; if (same.IsEmpty()) {
// The phi is unreachable or in the start block
// Insert the phi node after the undef opcode, this will be replaced with an identity // Insert an undefined instruction and make it the phi node replacement
list.insert(first_not_phi, phi); // The "phi" node reinsertion point is specified after this instruction
reinsert_point = block->PrependNewInst(reinsert_point, undef_opcode);
same = IR::Value{&*reinsert_point};
++reinsert_point;
} }
// Reroute all uses of phi to same and remove phi // Reinsert the phi node and reroute all its uses to the "same" value
list.insert(reinsert_point, phi);
phi.ReplaceUsesWith(same); phi.ReplaceUsesWith(same);
// TODO: Try to recursively remove all phi users, which might have become trivial // TODO: Try to recursively remove all phi users, which might have become trivial
return same; return same;