Utils.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. namespace Ryujinx.Graphics.Shader.Translation.Optimizations
  3. {
  4. static class Utils
  5. {
  6. private static Operation FindBranchSource(BasicBlock block)
  7. {
  8. foreach (BasicBlock sourceBlock in block.Predecessors)
  9. {
  10. if (sourceBlock.Operations.Count > 0)
  11. {
  12. if (sourceBlock.GetLastOp() is Operation lastOp && IsConditionalBranch(lastOp.Inst) && sourceBlock.Next == block)
  13. {
  14. return lastOp;
  15. }
  16. }
  17. }
  18. return null;
  19. }
  20. private static bool IsConditionalBranch(Instruction inst)
  21. {
  22. return inst == Instruction.BranchIfFalse || inst == Instruction.BranchIfTrue;
  23. }
  24. private static bool BlockConditionsMatch(BasicBlock currentBlock, BasicBlock queryBlock)
  25. {
  26. // Check if all the conditions for the query block are satisfied by the current block.
  27. // Just checks the top-most conditional for now.
  28. Operation currentBranch = FindBranchSource(currentBlock);
  29. Operation queryBranch = FindBranchSource(queryBlock);
  30. Operand currentCondition = currentBranch?.GetSource(0);
  31. Operand queryCondition = queryBranch?.GetSource(0);
  32. // The condition should be the same operand instance.
  33. return currentBranch != null && queryBranch != null &&
  34. currentBranch.Inst == queryBranch.Inst &&
  35. currentCondition == queryCondition;
  36. }
  37. public static Operand FindLastOperation(Operand source, BasicBlock block)
  38. {
  39. if (source.AsgOp is PhiNode phiNode)
  40. {
  41. // This source can have a different value depending on a previous branch.
  42. // Ensure that conditions met for that branch are also met for the current one.
  43. // Prefer the latest sources for the phi node.
  44. for (int i = phiNode.SourcesCount - 1; i >= 0; i--)
  45. {
  46. BasicBlock phiBlock = phiNode.GetBlock(i);
  47. if (BlockConditionsMatch(block, phiBlock))
  48. {
  49. return phiNode.GetSource(i);
  50. }
  51. }
  52. }
  53. return source;
  54. }
  55. }
  56. }