InstEmitFlow.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 Cal(EmitterContext context)
  44. {
  45. OpCodeBranch op = (OpCodeBranch)context.CurrOp;
  46. context.Call(context.GetFunctionId(op.GetAbsoluteAddress()), false);
  47. }
  48. public static void Depbar(EmitterContext context)
  49. {
  50. }
  51. public static void Exit(EmitterContext context)
  52. {
  53. if (context.IsNonMain)
  54. {
  55. context.Config.GpuAccessor.Log("Invalid exit on non-main function.");
  56. return;
  57. }
  58. OpCodeExit op = (OpCodeExit)context.CurrOp;
  59. // TODO: Figure out how this is supposed to work in the
  60. // presence of other condition codes.
  61. if (op.Condition == Condition.Always)
  62. {
  63. context.Return();
  64. }
  65. }
  66. public static void Kil(EmitterContext context)
  67. {
  68. context.Discard();
  69. }
  70. public static void Nop(EmitterContext context)
  71. {
  72. }
  73. public static void Pbk(EmitterContext context)
  74. {
  75. EmitPbkOrSsy(context);
  76. }
  77. public static void Ret(EmitterContext context)
  78. {
  79. if (context.IsNonMain)
  80. {
  81. context.Return();
  82. }
  83. else
  84. {
  85. context.Config.GpuAccessor.Log("Invalid return on main function.");
  86. }
  87. }
  88. public static void Ssy(EmitterContext context)
  89. {
  90. EmitPbkOrSsy(context);
  91. }
  92. public static void Sync(EmitterContext context)
  93. {
  94. EmitBrkOrSync(context);
  95. }
  96. private static void EmitPbkOrSsy(EmitterContext context)
  97. {
  98. OpCodePush op = (OpCodePush)context.CurrOp;
  99. foreach (KeyValuePair<OpCodeBranchPop, Operand> kv in op.PopOps)
  100. {
  101. OpCodeBranchPop opSync = kv.Key;
  102. Operand local = kv.Value;
  103. int pushOpIndex = opSync.Targets[op];
  104. context.Copy(local, Const(pushOpIndex));
  105. }
  106. }
  107. private static void EmitBrkOrSync(EmitterContext context)
  108. {
  109. OpCodeBranchPop op = (OpCodeBranchPop)context.CurrOp;
  110. if (op.Targets.Count == 1)
  111. {
  112. // If we have only one target, then the SSY/PBK is basically
  113. // a branch, we can produce better codegen for this case.
  114. OpCodePush pushOp = op.Targets.Keys.First();
  115. EmitBranch(context, pushOp.GetAbsoluteAddress());
  116. }
  117. else
  118. {
  119. foreach (KeyValuePair<OpCodePush, int> kv in op.Targets)
  120. {
  121. OpCodePush pushOp = kv.Key;
  122. Operand label = context.GetLabel(pushOp.GetAbsoluteAddress());
  123. Operand local = pushOp.PopOps[op];
  124. int pushOpIndex = kv.Value;
  125. context.BranchIfTrue(label, context.ICompareEqual(local, Const(pushOpIndex)));
  126. }
  127. }
  128. }
  129. private static void EmitBranch(EmitterContext context, ulong address)
  130. {
  131. OpCode op = context.CurrOp;
  132. // If we're branching to the next instruction, then the branch
  133. // is useless and we can ignore it.
  134. if (address == op.Address + 8)
  135. {
  136. return;
  137. }
  138. Operand label = context.GetLabel(address);
  139. Operand pred = Register(op.Predicate);
  140. if (op is OpCodeBranch opBranch && opBranch.Condition != Condition.Always)
  141. {
  142. Operand cond = GetCondition(context, opBranch.Condition);
  143. if (op.Predicate.IsPT)
  144. {
  145. pred = cond;
  146. }
  147. else if (op.InvertPredicate)
  148. {
  149. pred = context.BitwiseAnd(context.BitwiseNot(pred), cond);
  150. }
  151. else
  152. {
  153. pred = context.BitwiseAnd(pred, cond);
  154. }
  155. context.BranchIfTrue(label, pred);
  156. }
  157. else if (op.Predicate.IsPT)
  158. {
  159. context.Branch(label);
  160. }
  161. else if (op.InvertPredicate)
  162. {
  163. context.BranchIfFalse(label, pred);
  164. }
  165. else
  166. {
  167. context.BranchIfTrue(label, pred);
  168. }
  169. }
  170. private static Operand GetCondition(EmitterContext context, Condition cond)
  171. {
  172. // TODO: More condition codes, figure out how they work.
  173. switch (cond)
  174. {
  175. case Condition.Equal:
  176. case Condition.EqualUnordered:
  177. return GetZF();
  178. case Condition.NotEqual:
  179. case Condition.NotEqualUnordered:
  180. return context.BitwiseNot(GetZF());
  181. }
  182. return Const(IrConsts.True);
  183. }
  184. }
  185. }