SsaConstruction.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. using ARMeilleure.Common;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.State;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using static ARMeilleure.IntermediateRepresentation.OperandHelper;
  8. namespace ARMeilleure.Translation
  9. {
  10. static partial class Ssa
  11. {
  12. private class DefMap
  13. {
  14. private readonly Dictionary<int, Operand> _map;
  15. private readonly BitMap _phiMasks;
  16. public DefMap()
  17. {
  18. _map = new Dictionary<int, Operand>();
  19. _phiMasks = new BitMap(RegisterConsts.TotalCount);
  20. }
  21. public bool TryAddOperand(int key, Operand operand)
  22. {
  23. return _map.TryAdd(key, operand);
  24. }
  25. public bool TryGetOperand(int key, out Operand operand)
  26. {
  27. return _map.TryGetValue(key, out operand);
  28. }
  29. public bool AddPhi(int key)
  30. {
  31. return _phiMasks.Set(key);
  32. }
  33. public bool HasPhi(int key)
  34. {
  35. return _phiMasks.IsSet(key);
  36. }
  37. }
  38. public static void Construct(ControlFlowGraph cfg)
  39. {
  40. var globalDefs = new DefMap[cfg.Blocks.Count];
  41. var localDefs = new Operand[cfg.LocalsCount + RegisterConsts.TotalCount];
  42. var dfPhiBlocks = new Queue<BasicBlock>();
  43. for (BasicBlock block = cfg.Blocks.First; block != null; block = block.ListNext)
  44. {
  45. globalDefs[block.Index] = new DefMap();
  46. }
  47. // First pass, get all defs and locals uses.
  48. for (BasicBlock block = cfg.Blocks.First; block != null; block = block.ListNext)
  49. {
  50. for (Node node = block.Operations.First; node != null; node = node.ListNext)
  51. {
  52. if (node is not Operation operation)
  53. {
  54. continue;
  55. }
  56. for (int index = 0; index < operation.SourcesCount; index++)
  57. {
  58. Operand src = operation.GetSource(index);
  59. if (TryGetId(src, out int srcKey))
  60. {
  61. Operand local = localDefs[srcKey] ?? src;
  62. operation.SetSource(index, local);
  63. }
  64. }
  65. Operand dest = operation.Destination;
  66. if (TryGetId(dest, out int destKey))
  67. {
  68. Operand local = Local(dest.Type);
  69. localDefs[destKey] = local;
  70. operation.Destination = local;
  71. }
  72. }
  73. for (int key = 0; key < localDefs.Length; key++)
  74. {
  75. Operand local = localDefs[key];
  76. if (local is null)
  77. {
  78. continue;
  79. }
  80. globalDefs[block.Index].TryAddOperand(key, local);
  81. dfPhiBlocks.Enqueue(block);
  82. while (dfPhiBlocks.TryDequeue(out BasicBlock dfPhiBlock))
  83. {
  84. foreach (BasicBlock domFrontier in dfPhiBlock.DominanceFrontiers)
  85. {
  86. if (globalDefs[domFrontier.Index].AddPhi(key))
  87. {
  88. dfPhiBlocks.Enqueue(domFrontier);
  89. }
  90. }
  91. }
  92. }
  93. Array.Clear(localDefs, 0, localDefs.Length);
  94. }
  95. // Second pass, rename variables with definitions on different blocks.
  96. for (BasicBlock block = cfg.Blocks.First; block != null; block = block.ListNext)
  97. {
  98. for (Node node = block.Operations.First; node != null; node = node.ListNext)
  99. {
  100. if (node is not Operation operation)
  101. {
  102. continue;
  103. }
  104. for (int index = 0; index < operation.SourcesCount; index++)
  105. {
  106. Operand src = operation.GetSource(index);
  107. if (TryGetId(src, out int key))
  108. {
  109. Operand local = localDefs[key];
  110. if (local is null)
  111. {
  112. local = FindDef(globalDefs, block, src);
  113. localDefs[key] = local;
  114. }
  115. operation.SetSource(index, local);
  116. }
  117. }
  118. }
  119. Array.Clear(localDefs, 0, localDefs.Length);
  120. }
  121. }
  122. private static Operand FindDef(DefMap[] globalDefs, BasicBlock current, Operand operand)
  123. {
  124. if (globalDefs[current.Index].HasPhi(GetId(operand)))
  125. {
  126. return InsertPhi(globalDefs, current, operand);
  127. }
  128. if (current != current.ImmediateDominator)
  129. {
  130. return FindDefOnPred(globalDefs, current.ImmediateDominator, operand);
  131. }
  132. return Undef();
  133. }
  134. private static Operand FindDefOnPred(DefMap[] globalDefs, BasicBlock current, Operand operand)
  135. {
  136. BasicBlock previous;
  137. do
  138. {
  139. DefMap defMap = globalDefs[current.Index];
  140. int key = GetId(operand);
  141. if (defMap.TryGetOperand(key, out Operand lastDef))
  142. {
  143. return lastDef;
  144. }
  145. if (defMap.HasPhi(key))
  146. {
  147. return InsertPhi(globalDefs, current, operand);
  148. }
  149. previous = current;
  150. current = current.ImmediateDominator;
  151. }
  152. while (previous != current);
  153. return Undef();
  154. }
  155. private static Operand InsertPhi(DefMap[] globalDefs, BasicBlock block, Operand operand)
  156. {
  157. // This block has a Phi that has not been materialized yet, but that
  158. // would define a new version of the variable we're looking for. We need
  159. // to materialize the Phi, add all the block/operand pairs into the Phi, and
  160. // then use the definition from that Phi.
  161. Operand local = Local(operand.Type);
  162. PhiNode phi = new PhiNode(local, block.Predecessors.Count);
  163. AddPhi(block, phi);
  164. globalDefs[block.Index].TryAddOperand(GetId(operand), local);
  165. for (int index = 0; index < block.Predecessors.Count; index++)
  166. {
  167. BasicBlock predecessor = block.Predecessors[index];
  168. phi.SetBlock(index, predecessor);
  169. phi.SetSource(index, FindDefOnPred(globalDefs, predecessor, operand));
  170. }
  171. return local;
  172. }
  173. private static void AddPhi(BasicBlock block, PhiNode phi)
  174. {
  175. Node node = block.Operations.First;
  176. if (node != null)
  177. {
  178. while (node.ListNext is PhiNode)
  179. {
  180. node = node.ListNext;
  181. }
  182. }
  183. if (node is PhiNode)
  184. {
  185. block.Operations.AddAfter(node, phi);
  186. }
  187. else
  188. {
  189. block.Operations.AddFirst(phi);
  190. }
  191. }
  192. private static bool TryGetId(Operand operand, out int result)
  193. {
  194. if (operand is { Kind: OperandKind.Register })
  195. {
  196. Register reg = operand.GetRegister();
  197. if (reg.Type == RegisterType.Integer)
  198. {
  199. result = reg.Index;
  200. }
  201. else if (reg.Type == RegisterType.Vector)
  202. {
  203. result = RegisterConsts.IntRegsCount + reg.Index;
  204. }
  205. else if (reg.Type == RegisterType.Flag)
  206. {
  207. result = RegisterConsts.IntAndVecRegsCount + reg.Index;
  208. }
  209. else /* if (reg.Type == RegisterType.FpFlag) */
  210. {
  211. result = RegisterConsts.FpFlagsOffset + reg.Index;
  212. }
  213. return true;
  214. }
  215. else if (operand is { Kind: OperandKind.LocalVariable } && operand.GetLocalNumber() > 0)
  216. {
  217. result = RegisterConsts.TotalCount + operand.GetLocalNumber() - 1;
  218. return true;
  219. }
  220. result = -1;
  221. return false;
  222. }
  223. private static int GetId(Operand operand)
  224. {
  225. if (!TryGetId(operand, out int key))
  226. {
  227. Debug.Fail("OperandKind must be Register or a numbered LocalVariable.");
  228. }
  229. return key;
  230. }
  231. }
  232. }