Ssa.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 class LocalDefMap
  49. {
  50. private Operand[] _map;
  51. private int[] _uses;
  52. public int UseCount { get; private set; }
  53. public LocalDefMap()
  54. {
  55. _map = new Operand[RegisterConsts.TotalCount];
  56. _uses = new int[RegisterConsts.TotalCount];
  57. }
  58. public Operand Get(int key)
  59. {
  60. return _map[key];
  61. }
  62. public void Add(int key, Operand operand)
  63. {
  64. if (_map[key] == null)
  65. {
  66. _uses[UseCount++] = key;
  67. }
  68. _map[key] = operand;
  69. }
  70. public Operand GetUse(int index, out int key)
  71. {
  72. key = _uses[index];
  73. return _map[key];
  74. }
  75. public void Clear()
  76. {
  77. for (int i = 0; i < UseCount; i++)
  78. {
  79. _map[_uses[i]] = null;
  80. }
  81. UseCount = 0;
  82. }
  83. }
  84. private struct Definition
  85. {
  86. public BasicBlock Block { get; }
  87. public Operand Local { get; }
  88. public Definition(BasicBlock block, Operand local)
  89. {
  90. Block = block;
  91. Local = local;
  92. }
  93. }
  94. public static void Rename(BasicBlock[] blocks)
  95. {
  96. DefMap[] globalDefs = new DefMap[blocks.Length];
  97. LocalDefMap localDefs = new LocalDefMap();
  98. for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
  99. {
  100. globalDefs[blkIndex] = new DefMap();
  101. }
  102. Queue<BasicBlock> dfPhiBlocks = new Queue<BasicBlock>();
  103. // First pass, get all defs and locals uses.
  104. for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
  105. {
  106. Operand RenameLocal(Operand operand)
  107. {
  108. if (operand != null && operand.Type == OperandType.Register)
  109. {
  110. Operand local = localDefs.Get(GetKeyFromRegister(operand.GetRegister()));
  111. operand = local ?? operand;
  112. }
  113. return operand;
  114. }
  115. BasicBlock block = blocks[blkIndex];
  116. LinkedListNode<INode> node = block.Operations.First;
  117. while (node != null)
  118. {
  119. if (node.Value is Operation operation)
  120. {
  121. for (int index = 0; index < operation.SourcesCount; index++)
  122. {
  123. operation.SetSource(index, RenameLocal(operation.GetSource(index)));
  124. }
  125. for (int index = 0; index < operation.DestsCount; index++)
  126. {
  127. Operand dest = operation.GetDest(index);
  128. if (dest != null && dest.Type == OperandType.Register)
  129. {
  130. Operand local = Local();
  131. localDefs.Add(GetKeyFromRegister(dest.GetRegister()), local);
  132. operation.SetDest(index, local);
  133. }
  134. }
  135. }
  136. node = node.Next;
  137. }
  138. int localUses = localDefs.UseCount;
  139. for (int index = 0; index < localUses; index++)
  140. {
  141. Operand local = localDefs.GetUse(index, out int key);
  142. Register reg = GetRegisterFromKey(key);
  143. globalDefs[block.Index].TryAddOperand(reg, local);
  144. dfPhiBlocks.Enqueue(block);
  145. while (dfPhiBlocks.TryDequeue(out BasicBlock dfPhiBlock))
  146. {
  147. foreach (BasicBlock domFrontier in dfPhiBlock.DominanceFrontiers)
  148. {
  149. if (globalDefs[domFrontier.Index].AddPhi(reg))
  150. {
  151. dfPhiBlocks.Enqueue(domFrontier);
  152. }
  153. }
  154. }
  155. }
  156. localDefs.Clear();
  157. }
  158. // Second pass, rename variables with definitions on different blocks.
  159. for (int blkIndex = 0; blkIndex < blocks.Length; blkIndex++)
  160. {
  161. BasicBlock block = blocks[blkIndex];
  162. Operand RenameGlobal(Operand operand)
  163. {
  164. if (operand != null && operand.Type == OperandType.Register)
  165. {
  166. int key = GetKeyFromRegister(operand.GetRegister());
  167. Operand local = localDefs.Get(key);
  168. if (local != null)
  169. {
  170. return local;
  171. }
  172. operand = FindDefinitionForCurr(globalDefs, block, operand.GetRegister());
  173. localDefs.Add(key, operand);
  174. }
  175. return operand;
  176. }
  177. for (LinkedListNode<INode> node = block.Operations.First; node != null; node = node.Next)
  178. {
  179. if (node.Value is Operation operation)
  180. {
  181. for (int index = 0; index < operation.SourcesCount; index++)
  182. {
  183. operation.SetSource(index, RenameGlobal(operation.GetSource(index)));
  184. }
  185. }
  186. }
  187. if (blkIndex < blocks.Length - 1)
  188. {
  189. localDefs.Clear();
  190. }
  191. }
  192. }
  193. private static Operand FindDefinitionForCurr(DefMap[] globalDefs, BasicBlock current, Register reg)
  194. {
  195. if (globalDefs[current.Index].HasPhi(reg))
  196. {
  197. return InsertPhi(globalDefs, current, reg);
  198. }
  199. if (current != current.ImmediateDominator)
  200. {
  201. return FindDefinition(globalDefs, current.ImmediateDominator, reg).Local;
  202. }
  203. return Undef();
  204. }
  205. private static Definition FindDefinition(DefMap[] globalDefs, BasicBlock current, Register reg)
  206. {
  207. foreach (BasicBlock block in SelfAndImmediateDominators(current))
  208. {
  209. DefMap defMap = globalDefs[block.Index];
  210. if (defMap.TryGetOperand(reg, out Operand lastDef))
  211. {
  212. return new Definition(block, lastDef);
  213. }
  214. if (defMap.HasPhi(reg))
  215. {
  216. return new Definition(block, InsertPhi(globalDefs, block, reg));
  217. }
  218. }
  219. return new Definition(current, Undef());
  220. }
  221. private static IEnumerable<BasicBlock> SelfAndImmediateDominators(BasicBlock block)
  222. {
  223. while (block != block.ImmediateDominator)
  224. {
  225. yield return block;
  226. block = block.ImmediateDominator;
  227. }
  228. yield return block;
  229. }
  230. private static Operand InsertPhi(DefMap[] globalDefs, BasicBlock block, Register reg)
  231. {
  232. // This block has a Phi that has not been materialized yet, but that
  233. // would define a new version of the variable we're looking for. We need
  234. // to materialize the Phi, add all the block/operand pairs into the Phi, and
  235. // then use the definition from that Phi.
  236. Operand local = Local();
  237. PhiNode phi = new PhiNode(local);
  238. AddPhi(block, phi);
  239. globalDefs[block.Index].TryAddOperand(reg, local);
  240. foreach (BasicBlock predecessor in block.Predecessors)
  241. {
  242. Definition def = FindDefinition(globalDefs, predecessor, reg);
  243. phi.AddSource(def.Block, def.Local);
  244. }
  245. return local;
  246. }
  247. private static void AddPhi(BasicBlock block, PhiNode phi)
  248. {
  249. LinkedListNode<INode> node = block.Operations.First;
  250. if (node != null)
  251. {
  252. while (node.Next?.Value is PhiNode)
  253. {
  254. node = node.Next;
  255. }
  256. }
  257. if (node?.Value is PhiNode)
  258. {
  259. block.Operations.AddAfter(node, phi);
  260. }
  261. else
  262. {
  263. block.Operations.AddFirst(phi);
  264. }
  265. }
  266. private static int GetKeyFromRegister(Register reg)
  267. {
  268. if (reg.Type == RegisterType.Gpr)
  269. {
  270. return reg.Index;
  271. }
  272. else if (reg.Type == RegisterType.Predicate)
  273. {
  274. return RegisterConsts.GprsCount + reg.Index;
  275. }
  276. else /* if (reg.Type == RegisterType.Flag) */
  277. {
  278. return GprsAndPredsCount + reg.Index;
  279. }
  280. }
  281. private static Register GetRegisterFromKey(int key)
  282. {
  283. if (key < RegisterConsts.GprsCount)
  284. {
  285. return new Register(key, RegisterType.Gpr);
  286. }
  287. else if (key < GprsAndPredsCount)
  288. {
  289. return new Register(key - RegisterConsts.GprsCount, RegisterType.Predicate);
  290. }
  291. else /* if (key < RegisterConsts.TotalCount) */
  292. {
  293. return new Register(key - GprsAndPredsCount, RegisterType.Flag);
  294. }
  295. }
  296. }
  297. }