Ssa.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. using Ryujinx.Graphics.Shader.Decoders;
  2. using Ryujinx.Graphics.Shader.IntermediateRepresentation;
  3. using System.Collections.Generic;
  4. using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
  5. namespace Ryujinx.Graphics.Shader.Translation
  6. {
  7. static class Ssa
  8. {
  9. private const int GprsAndPredsCount = RegisterConsts.GprsCount + RegisterConsts.PredsCount;
  10. private class DefMap
  11. {
  12. private Dictionary<Register, Operand> _map;
  13. private long[] _phiMasks;
  14. public DefMap()
  15. {
  16. _map = new Dictionary<Register, Operand>();
  17. _phiMasks = new long[(RegisterConsts.TotalCount + 63) / 64];
  18. }
  19. public bool TryAddOperand(Register reg, Operand operand)
  20. {
  21. return _map.TryAdd(reg, operand);
  22. }
  23. public bool TryGetOperand(Register reg, out Operand operand)
  24. {
  25. return _map.TryGetValue(reg, out operand);
  26. }
  27. public bool AddPhi(Register reg)
  28. {
  29. int key = GetKeyFromRegister(reg);
  30. int index = key / 64;
  31. int bit = key & 63;
  32. long mask = 1L << bit;
  33. if ((_phiMasks[index] & mask) != 0)
  34. {
  35. return false;
  36. }
  37. _phiMasks[index] |= mask;
  38. return true;
  39. }
  40. public bool HasPhi(Register reg)
  41. {
  42. int key = GetKeyFromRegister(reg);
  43. int index = key / 64;
  44. int bit = key & 63;
  45. return (_phiMasks[index] & (1L << bit)) != 0;
  46. }
  47. }
  48. private struct Definition
  49. {
  50. public BasicBlock Block { get; }
  51. public Operand Local { get; }
  52. public Definition(BasicBlock block, Operand local)
  53. {
  54. Block = block;
  55. Local = local;
  56. }
  57. }
  58. public static void Rename(BasicBlock[] blocks)
  59. {
  60. DefMap[] globalDefs = new DefMap[blocks.Length];
  61. for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
  62. {
  63. globalDefs[blkIndex] = new DefMap();
  64. }
  65. Queue<BasicBlock> dfPhiBlocks = new Queue<BasicBlock>();
  66. // First pass, get all defs and locals uses.
  67. for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
  68. {
  69. Operand[] localDefs = new Operand[RegisterConsts.TotalCount];
  70. Operand RenameLocal(Operand operand)
  71. {
  72. if (operand != null && operand.Type == OperandType.Register)
  73. {
  74. Operand local = localDefs[GetKeyFromRegister(operand.GetRegister())];
  75. operand = local ?? operand;
  76. }
  77. return operand;
  78. }
  79. BasicBlock block = blocks[blkIndex];
  80. LinkedListNode<INode> node = block.Operations.First;
  81. while (node != null)
  82. {
  83. if (node.Value is Operation operation)
  84. {
  85. for (int index = 0; index < operation.SourcesCount; index++)
  86. {
  87. operation.SetSource(index, RenameLocal(operation.GetSource(index)));
  88. }
  89. if (operation.Dest != null && operation.Dest.Type == OperandType.Register)
  90. {
  91. Operand local = Local();
  92. localDefs[GetKeyFromRegister(operation.Dest.GetRegister())] = local;
  93. operation.Dest = local;
  94. }
  95. }
  96. node = node.Next;
  97. }
  98. for (int index = 0; index < RegisterConsts.TotalCount; index++)
  99. {
  100. Operand local = localDefs[index];
  101. if (local == null)
  102. {
  103. continue;
  104. }
  105. Register reg = GetRegisterFromKey(index);
  106. globalDefs[block.Index].TryAddOperand(reg, local);
  107. dfPhiBlocks.Enqueue(block);
  108. while (dfPhiBlocks.TryDequeue(out BasicBlock dfPhiBlock))
  109. {
  110. foreach (BasicBlock domFrontier in dfPhiBlock.DominanceFrontiers)
  111. {
  112. if (globalDefs[domFrontier.Index].AddPhi(reg))
  113. {
  114. dfPhiBlocks.Enqueue(domFrontier);
  115. }
  116. }
  117. }
  118. }
  119. }
  120. // Second pass, rename variables with definitions on different blocks.
  121. for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
  122. {
  123. Operand[] localDefs = new Operand[RegisterConsts.TotalCount];
  124. BasicBlock block = blocks[blkIndex];
  125. Operand RenameGlobal(Operand operand)
  126. {
  127. if (operand != null && operand.Type == OperandType.Register)
  128. {
  129. int key = GetKeyFromRegister(operand.GetRegister());
  130. Operand local = localDefs[key];
  131. if (local != null)
  132. {
  133. return local;
  134. }
  135. operand = FindDefinitionForCurr(globalDefs, block, operand.GetRegister());
  136. localDefs[key] = operand;
  137. }
  138. return operand;
  139. }
  140. LinkedListNode<INode> node = block.Operations.First;
  141. while (node != null)
  142. {
  143. if (node.Value is Operation operation)
  144. {
  145. for (int index = 0; index < operation.SourcesCount; index++)
  146. {
  147. operation.SetSource(index, RenameGlobal(operation.GetSource(index)));
  148. }
  149. }
  150. node = node.Next;
  151. }
  152. }
  153. }
  154. private static Operand FindDefinitionForCurr(DefMap[] globalDefs, BasicBlock current, Register reg)
  155. {
  156. if (globalDefs[current.Index].HasPhi(reg))
  157. {
  158. return InsertPhi(globalDefs, current, reg);
  159. }
  160. if (current != current.ImmediateDominator)
  161. {
  162. return FindDefinition(globalDefs, current.ImmediateDominator, reg).Local;
  163. }
  164. return Undef();
  165. }
  166. private static Definition FindDefinition(DefMap[] globalDefs, BasicBlock current, Register reg)
  167. {
  168. foreach (BasicBlock block in SelfAndImmediateDominators(current))
  169. {
  170. DefMap defMap = globalDefs[block.Index];
  171. if (defMap.TryGetOperand(reg, out Operand lastDef))
  172. {
  173. return new Definition(block, lastDef);
  174. }
  175. if (defMap.HasPhi(reg))
  176. {
  177. return new Definition(block, InsertPhi(globalDefs, block, reg));
  178. }
  179. }
  180. return new Definition(current, Undef());
  181. }
  182. private static IEnumerable<BasicBlock> SelfAndImmediateDominators(BasicBlock block)
  183. {
  184. while (block != block.ImmediateDominator)
  185. {
  186. yield return block;
  187. block = block.ImmediateDominator;
  188. }
  189. yield return block;
  190. }
  191. private static Operand InsertPhi(DefMap[] globalDefs, BasicBlock block, Register reg)
  192. {
  193. // This block has a Phi that has not been materialized yet, but that
  194. // would define a new version of the variable we're looking for. We need
  195. // to materialize the Phi, add all the block/operand pairs into the Phi, and
  196. // then use the definition from that Phi.
  197. Operand local = Local();
  198. PhiNode phi = new PhiNode(local);
  199. AddPhi(block, phi);
  200. globalDefs[block.Index].TryAddOperand(reg, local);
  201. foreach (BasicBlock predecessor in block.Predecessors)
  202. {
  203. Definition def = FindDefinition(globalDefs, predecessor, reg);
  204. phi.AddSource(def.Block, def.Local);
  205. }
  206. return local;
  207. }
  208. private static void AddPhi(BasicBlock block, PhiNode phi)
  209. {
  210. LinkedListNode<INode> node = block.Operations.First;
  211. if (node != null)
  212. {
  213. while (node.Next?.Value is PhiNode)
  214. {
  215. node = node.Next;
  216. }
  217. }
  218. if (node?.Value is PhiNode)
  219. {
  220. block.Operations.AddAfter(node, phi);
  221. }
  222. else
  223. {
  224. block.Operations.AddFirst(phi);
  225. }
  226. }
  227. private static int GetKeyFromRegister(Register reg)
  228. {
  229. if (reg.Type == RegisterType.Gpr)
  230. {
  231. return reg.Index;
  232. }
  233. else if (reg.Type == RegisterType.Predicate)
  234. {
  235. return RegisterConsts.GprsCount + reg.Index;
  236. }
  237. else /* if (reg.Type == RegisterType.Flag) */
  238. {
  239. return GprsAndPredsCount + reg.Index;
  240. }
  241. }
  242. private static Register GetRegisterFromKey(int key)
  243. {
  244. if (key < RegisterConsts.GprsCount)
  245. {
  246. return new Register(key, RegisterType.Gpr);
  247. }
  248. else if (key < GprsAndPredsCount)
  249. {
  250. return new Register(key - RegisterConsts.GprsCount, RegisterType.Predicate);
  251. }
  252. else /* if (key < RegisterConsts.TotalCount) */
  253. {
  254. return new Register(key - GprsAndPredsCount, RegisterType.Flag);
  255. }
  256. }
  257. }
  258. }