InstEmitFlow.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using Ryujinx.Graphics.Shader.Decoders;
  2. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  3. using Ryujinx.Graphics.Shader.Translation;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using static Ryujinx.Graphics.Shader.Instructions.InstEmitHelper;
  7. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  8. namespace Ryujinx.Graphics.Shader.Instructions
  9. {
  10. static partial class InstEmit
  11. {
  12. public static void Bra(EmitterContext context)
  13. {
  14. EmitBranch(context, context.CurrBlock.Branch.Address);
  15. }
  16. public static void Brk(EmitterContext context)
  17. {
  18. EmitBrkOrSync(context);
  19. }
  20. public static void Brx(EmitterContext context)
  21. {
  22. OpCodeBranchIndir op = (OpCodeBranchIndir)context.CurrOp;
  23. int offset = (int)op.Address + 8 + op.Offset;
  24. Operand address = context.IAdd(Register(op.Ra), Const(offset));
  25. // Sorting the target addresses in descending order improves the code,
  26. // since it will always check the most distant targets first, then the
  27. // near ones. This can be easily transformed into if/else statements.
  28. IOrderedEnumerable<Block> sortedTargets = op.PossibleTargets.OrderByDescending(x => x.Address);
  29. Block lastTarget = sortedTargets.LastOrDefault();
  30. foreach (Block possibleTarget in sortedTargets)
  31. {
  32. Operand label = context.GetLabel(possibleTarget.Address);
  33. if (possibleTarget != lastTarget)
  34. {
  35. context.BranchIfTrue(label, context.ICompareEqual(address, Const((int)possibleTarget.Address)));
  36. }
  37. else
  38. {
  39. context.Branch(label);
  40. }
  41. }
  42. }
  43. public static void Depbar(EmitterContext context) { }
  44. public static void Exit(EmitterContext context)
  45. {
  46. OpCodeExit op = (OpCodeExit)context.CurrOp;
  47. // TODO: Figure out how this is supposed to work in the
  48. // presence of other condition codes.
  49. if (op.Condition == Condition.Always)
  50. {
  51. context.Return();
  52. }
  53. }
  54. public static void Kil(EmitterContext context)
  55. {
  56. context.Discard();
  57. }
  58. public static void Nop(EmitterContext context) { }
  59. public static void Pbk(EmitterContext context)
  60. {
  61. EmitPbkOrSsy(context);
  62. }
  63. public static void Ssy(EmitterContext context)
  64. {
  65. EmitPbkOrSsy(context);
  66. }
  67. public static void Sync(EmitterContext context)
  68. {
  69. EmitBrkOrSync(context);
  70. }
  71. private static void EmitPbkOrSsy(EmitterContext context)
  72. {
  73. OpCodePush op = (OpCodePush)context.CurrOp;
  74. foreach (KeyValuePair<OpCodeBranchPop, Operand> kv in op.PopOps)
  75. {
  76. OpCodeBranchPop opSync = kv.Key;
  77. Operand local = kv.Value;
  78. int pushOpIndex = opSync.Targets[op];
  79. context.Copy(local, Const(pushOpIndex));
  80. }
  81. }
  82. private static void EmitBrkOrSync(EmitterContext context)
  83. {
  84. OpCodeBranchPop op = (OpCodeBranchPop)context.CurrOp;
  85. if (op.Targets.Count == 1)
  86. {
  87. // If we have only one target, then the SSY/PBK is basically
  88. // a branch, we can produce better codegen for this case.
  89. OpCodePush pushOp = op.Targets.Keys.First();
  90. EmitBranch(context, pushOp.GetAbsoluteAddress());
  91. }
  92. else
  93. {
  94. foreach (KeyValuePair<OpCodePush, int> kv in op.Targets)
  95. {
  96. OpCodePush pushOp = kv.Key;
  97. Operand label = context.GetLabel(pushOp.GetAbsoluteAddress());
  98. Operand local = pushOp.PopOps[op];
  99. int pushOpIndex = kv.Value;
  100. context.BranchIfTrue(label, context.ICompareEqual(local, Const(pushOpIndex)));
  101. }
  102. }
  103. }
  104. private static void EmitBranch(EmitterContext context, ulong address)
  105. {
  106. OpCode op = context.CurrOp;
  107. // If we're branching to the next instruction, then the branch
  108. // is useless and we can ignore it.
  109. if (address == op.Address + 8)
  110. {
  111. return;
  112. }
  113. Operand label = context.GetLabel(address);
  114. Operand pred = Register(op.Predicate);
  115. if (op is OpCodeBranch opBranch && opBranch.Condition != Condition.Always)
  116. {
  117. Operand cond = GetCondition(context, opBranch.Condition);
  118. if (op.Predicate.IsPT)
  119. {
  120. pred = cond;
  121. }
  122. else if (op.InvertPredicate)
  123. {
  124. pred = context.BitwiseAnd(context.BitwiseNot(pred), cond);
  125. }
  126. else
  127. {
  128. pred = context.BitwiseAnd(pred, cond);
  129. }
  130. context.BranchIfTrue(label, pred);
  131. }
  132. else if (op.Predicate.IsPT)
  133. {
  134. context.Branch(label);
  135. }
  136. else if (op.InvertPredicate)
  137. {
  138. context.BranchIfFalse(label, pred);
  139. }
  140. else
  141. {
  142. context.BranchIfTrue(label, pred);
  143. }
  144. }
  145. private static Operand GetCondition(EmitterContext context, Condition cond)
  146. {
  147. // TODO: More condition codes, figure out how they work.
  148. switch (cond)
  149. {
  150. case Condition.Equal:
  151. case Condition.EqualUnordered:
  152. return GetZF();
  153. case Condition.NotEqual:
  154. case Condition.NotEqualUnordered:
  155. return context.BitwiseNot(GetZF());
  156. }
  157. return Const(IrConsts.True);
  158. }
  159. }
  160. }