AstOptimizer.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using static Ryujinx.Graphics.Shader.StructuredIr.AstHelper;
  5. namespace Ryujinx.Graphics.Shader.StructuredIr
  6. {
  7. static class AstOptimizer
  8. {
  9. public static void Optimize(StructuredProgramContext context)
  10. {
  11. AstBlock mainBlock = context.CurrentFunction.MainBlock;
  12. // When debug mode is enabled, we disable expression propagation
  13. // (this makes comparison with the disassembly easier).
  14. if (!context.DebugMode)
  15. {
  16. AstBlockVisitor visitor = new(mainBlock);
  17. foreach (IAstNode node in visitor.Visit())
  18. {
  19. if (node is AstAssignment assignment && assignment.Destination is AstOperand propVar)
  20. {
  21. bool isWorthPropagating = propVar.Uses.Count == 1 || IsWorthPropagating(assignment.Source);
  22. if (propVar.Defs.Count == 1 && isWorthPropagating)
  23. {
  24. PropagateExpression(propVar, assignment.Source);
  25. }
  26. if (propVar.Type == OperandType.LocalVariable && propVar.Uses.Count == 0)
  27. {
  28. visitor.Block.Remove(assignment);
  29. context.CurrentFunction.Locals.Remove(propVar);
  30. }
  31. }
  32. }
  33. }
  34. RemoveEmptyBlocks(mainBlock);
  35. }
  36. private static bool IsWorthPropagating(IAstNode source)
  37. {
  38. if (source is not AstOperation srcOp)
  39. {
  40. return false;
  41. }
  42. if (!InstructionInfo.IsUnary(srcOp.Inst))
  43. {
  44. return false;
  45. }
  46. return srcOp.GetSource(0) is AstOperand || srcOp.Inst == Instruction.Copy;
  47. }
  48. private static void PropagateExpression(AstOperand propVar, IAstNode source)
  49. {
  50. IAstNode[] uses = propVar.Uses.ToArray();
  51. foreach (IAstNode useNode in uses)
  52. {
  53. if (useNode is AstBlock useBlock)
  54. {
  55. useBlock.Condition = source;
  56. }
  57. else if (useNode is AstOperation useOperation)
  58. {
  59. for (int srcIndex = 0; srcIndex < useOperation.SourcesCount; srcIndex++)
  60. {
  61. if (useOperation.GetSource(srcIndex) == propVar)
  62. {
  63. useOperation.SetSource(srcIndex, source);
  64. }
  65. }
  66. }
  67. else if (useNode is AstAssignment useAssignment)
  68. {
  69. useAssignment.Source = source;
  70. }
  71. }
  72. }
  73. private static void RemoveEmptyBlocks(AstBlock mainBlock)
  74. {
  75. Queue<AstBlock> pending = new();
  76. pending.Enqueue(mainBlock);
  77. while (pending.TryDequeue(out AstBlock block))
  78. {
  79. foreach (IAstNode node in block)
  80. {
  81. if (node is AstBlock childBlock)
  82. {
  83. pending.Enqueue(childBlock);
  84. }
  85. }
  86. AstBlock parent = block.Parent;
  87. if (parent == null)
  88. {
  89. continue;
  90. }
  91. AstBlock nextBlock = Next(block) as AstBlock;
  92. bool hasElse = nextBlock != null && nextBlock.Type == AstBlockType.Else;
  93. bool isIf = block.Type == AstBlockType.If;
  94. if (block.Count == 0)
  95. {
  96. if (isIf)
  97. {
  98. if (hasElse)
  99. {
  100. nextBlock.TurnIntoIf(InverseCond(block.Condition));
  101. }
  102. parent.Remove(block);
  103. }
  104. else if (block.Type == AstBlockType.Else)
  105. {
  106. parent.Remove(block);
  107. }
  108. }
  109. else if (isIf && parent.Type == AstBlockType.Else && parent.Count == (hasElse ? 2 : 1))
  110. {
  111. AstBlock parentOfParent = parent.Parent;
  112. parent.Remove(block);
  113. parentOfParent.AddAfter(parent, block);
  114. if (hasElse)
  115. {
  116. parent.Remove(nextBlock);
  117. parentOfParent.AddAfter(block, nextBlock);
  118. }
  119. parentOfParent.Remove(parent);
  120. block.TurnIntoElseIf();
  121. }
  122. }
  123. }
  124. }
  125. }