Ssa.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. for (int index = 0; index < operation.DestsCount; index++)
  90. {
  91. Operand dest = operation.GetDest(index);
  92. if (dest != null && dest.Type == OperandType.Register)
  93. {
  94. Operand local = Local();
  95. localDefs[GetKeyFromRegister(dest.GetRegister())] = local;
  96. operation.SetDest(index, local);
  97. }
  98. }
  99. }
  100. node = node.Next;
  101. }
  102. for (int index = 0; index < RegisterConsts.TotalCount; index++)
  103. {
  104. Operand local = localDefs[index];
  105. if (local == null)
  106. {
  107. continue;
  108. }
  109. Register reg = GetRegisterFromKey(index);
  110. globalDefs[block.Index].TryAddOperand(reg, local);
  111. dfPhiBlocks.Enqueue(block);
  112. while (dfPhiBlocks.TryDequeue(out BasicBlock dfPhiBlock))
  113. {
  114. foreach (BasicBlock domFrontier in dfPhiBlock.DominanceFrontiers)
  115. {
  116. if (globalDefs[domFrontier.Index].AddPhi(reg))
  117. {
  118. dfPhiBlocks.Enqueue(domFrontier);
  119. }
  120. }
  121. }
  122. }
  123. }
  124. // Second pass, rename variables with definitions on different blocks.
  125. for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
  126. {
  127. Operand[] localDefs = new Operand[RegisterConsts.TotalCount];
  128. BasicBlock block = blocks[blkIndex];
  129. Operand RenameGlobal(Operand operand)
  130. {
  131. if (operand != null && operand.Type == OperandType.Register)
  132. {
  133. int key = GetKeyFromRegister(operand.GetRegister());
  134. Operand local = localDefs[key];
  135. if (local != null)
  136. {
  137. return local;
  138. }
  139. operand = FindDefinitionForCurr(globalDefs, block, operand.GetRegister());
  140. localDefs[key] = operand;
  141. }
  142. return operand;
  143. }
  144. for (LinkedListNode<INode> node = block.Operations.First; node != null; node = node.Next)
  145. {
  146. if (node.Value is Operation operation)
  147. {
  148. for (int index = 0; index < operation.SourcesCount; index++)
  149. {
  150. operation.SetSource(index, RenameGlobal(operation.GetSource(index)));
  151. }
  152. }
  153. }
  154. }
  155. }
  156. private static Operand FindDefinitionForCurr(DefMap[] globalDefs, BasicBlock current, Register reg)
  157. {
  158. if (globalDefs[current.Index].HasPhi(reg))
  159. {
  160. return InsertPhi(globalDefs, current, reg);
  161. }
  162. if (current != current.ImmediateDominator)
  163. {
  164. return FindDefinition(globalDefs, current.ImmediateDominator, reg).Local;
  165. }
  166. return Undef();
  167. }
  168. private static Definition FindDefinition(DefMap[] globalDefs, BasicBlock current, Register reg)
  169. {
  170. foreach (BasicBlock block in SelfAndImmediateDominators(current))
  171. {
  172. DefMap defMap = globalDefs[block.Index];
  173. if (defMap.TryGetOperand(reg, out Operand lastDef))
  174. {
  175. return new Definition(block, lastDef);
  176. }
  177. if (defMap.HasPhi(reg))
  178. {
  179. return new Definition(block, InsertPhi(globalDefs, block, reg));
  180. }
  181. }
  182. return new Definition(current, Undef());
  183. }
  184. private static IEnumerable<BasicBlock> SelfAndImmediateDominators(BasicBlock block)
  185. {
  186. while (block != block.ImmediateDominator)
  187. {
  188. yield return block;
  189. block = block.ImmediateDominator;
  190. }
  191. yield return block;
  192. }
  193. private static Operand InsertPhi(DefMap[] globalDefs, BasicBlock block, Register reg)
  194. {
  195. // This block has a Phi that has not been materialized yet, but that
  196. // would define a new version of the variable we're looking for. We need
  197. // to materialize the Phi, add all the block/operand pairs into the Phi, and
  198. // then use the definition from that Phi.
  199. Operand local = Local();
  200. PhiNode phi = new PhiNode(local);
  201. AddPhi(block, phi);
  202. globalDefs[block.Index].TryAddOperand(reg, local);
  203. foreach (BasicBlock predecessor in block.Predecessors)
  204. {
  205. Definition def = FindDefinition(globalDefs, predecessor, reg);
  206. phi.AddSource(def.Block, def.Local);
  207. }
  208. return local;
  209. }
  210. private static void AddPhi(BasicBlock block, PhiNode phi)
  211. {
  212. LinkedListNode<INode> node = block.Operations.First;
  213. if (node != null)
  214. {
  215. while (node.Next?.Value is PhiNode)
  216. {
  217. node = node.Next;
  218. }
  219. }
  220. if (node?.Value is PhiNode)
  221. {
  222. block.Operations.AddAfter(node, phi);
  223. }
  224. else
  225. {
  226. block.Operations.AddFirst(phi);
  227. }
  228. }
  229. private static int GetKeyFromRegister(Register reg)
  230. {
  231. if (reg.Type == RegisterType.Gpr)
  232. {
  233. return reg.Index;
  234. }
  235. else if (reg.Type == RegisterType.Predicate)
  236. {
  237. return RegisterConsts.GprsCount + reg.Index;
  238. }
  239. else /* if (reg.Type == RegisterType.Flag) */
  240. {
  241. return GprsAndPredsCount + reg.Index;
  242. }
  243. }
  244. private static Register GetRegisterFromKey(int key)
  245. {
  246. if (key < RegisterConsts.GprsCount)
  247. {
  248. return new Register(key, RegisterType.Gpr);
  249. }
  250. else if (key < GprsAndPredsCount)
  251. {
  252. return new Register(key - RegisterConsts.GprsCount, RegisterType.Predicate);
  253. }
  254. else /* if (key < RegisterConsts.TotalCount) */
  255. {
  256. return new Register(key - GprsAndPredsCount, RegisterType.Flag);
  257. }
  258. }
  259. }
  260. }