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;
} }
// Remove the phi node from the block, it will be reinserted
IR::Block::InstructionList& list{block->Instructions()};
list.erase(IR::Block::InstructionList::s_iterator_to(phi));
// Find the first non-phi instruction and use it as an insertion point
IR::Block::iterator reinsert_point{std::ranges::find_if_not(list, IsPhi)};
if (same.IsEmpty()) { if (same.IsEmpty()) {
// The phi is unreachable or in the start block // The phi is unreachable or in the start block
// First remove the phi node from the block, it will be reinserted // Insert an undefined instruction and make it the phi node replacement
IR::Block::InstructionList& list{block->Instructions()}; // The "phi" node reinsertion point is specified after this instruction
list.erase(IR::Block::InstructionList::s_iterator_to(phi)); reinsert_point = block->PrependNewInst(reinsert_point, undef_opcode);
same = IR::Value{&*reinsert_point};
// Insert an undef instruction after all phi nodes (to keep phi instructions on top) ++reinsert_point;
const auto first_not_phi{std::ranges::find_if_not(list, IsPhi)};
same = IR::Value{&*block->PrependNewInst(first_not_phi, undef_opcode)};
// Insert the phi node after the undef opcode, this will be replaced with an identity
list.insert(first_not_phi, phi);
} }
// 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;