SsaConstruction.cs 9.1 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.Operand.Factory;
  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(Allocators.Default, 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 (Operation node = block.Operations.First; node != default; node = node.ListNext)
  51. {
  52. for (int index = 0; index < node.SourcesCount; index++)
  53. {
  54. Operand src = node.GetSource(index);
  55. if (TryGetId(src, out int srcKey))
  56. {
  57. Operand local = localDefs[srcKey];
  58. if (local == default)
  59. {
  60. local = src;
  61. }
  62. node.SetSource(index, local);
  63. }
  64. }
  65. Operand dest = node.Destination;
  66. if (TryGetId(dest, out int destKey))
  67. {
  68. Operand local = Local(dest.Type);
  69. localDefs[destKey] = local;
  70. node.Destination = local;
  71. }
  72. }
  73. for (int key = 0; key < localDefs.Length; key++)
  74. {
  75. Operand local = localDefs[key];
  76. if (local == default)
  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);
  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 (Operation node = block.Operations.First; node != default; node = node.ListNext)
  99. {
  100. for (int index = 0; index < node.SourcesCount; index++)
  101. {
  102. Operand src = node.GetSource(index);
  103. if (TryGetId(src, out int key))
  104. {
  105. Operand local = localDefs[key];
  106. if (local == default)
  107. {
  108. local = FindDef(globalDefs, block, src);
  109. localDefs[key] = local;
  110. }
  111. node.SetSource(index, local);
  112. }
  113. }
  114. }
  115. Array.Clear(localDefs, 0, localDefs.Length);
  116. }
  117. }
  118. private static Operand FindDef(DefMap[] globalDefs, BasicBlock current, Operand operand)
  119. {
  120. if (globalDefs[current.Index].HasPhi(GetId(operand)))
  121. {
  122. return InsertPhi(globalDefs, current, operand);
  123. }
  124. if (current != current.ImmediateDominator)
  125. {
  126. return FindDefOnPred(globalDefs, current.ImmediateDominator, operand);
  127. }
  128. return Undef();
  129. }
  130. private static Operand FindDefOnPred(DefMap[] globalDefs, BasicBlock current, Operand operand)
  131. {
  132. BasicBlock previous;
  133. do
  134. {
  135. DefMap defMap = globalDefs[current.Index];
  136. int key = GetId(operand);
  137. if (defMap.TryGetOperand(key, out Operand lastDef))
  138. {
  139. return lastDef;
  140. }
  141. if (defMap.HasPhi(key))
  142. {
  143. return InsertPhi(globalDefs, current, operand);
  144. }
  145. previous = current;
  146. current = current.ImmediateDominator;
  147. }
  148. while (previous != current);
  149. return Undef();
  150. }
  151. private static Operand InsertPhi(DefMap[] globalDefs, BasicBlock block, Operand operand)
  152. {
  153. // This block has a Phi that has not been materialized yet, but that
  154. // would define a new version of the variable we're looking for. We need
  155. // to materialize the Phi, add all the block/operand pairs into the Phi, and
  156. // then use the definition from that Phi.
  157. Operand local = Local(operand.Type);
  158. Operation operation = Operation.Factory.PhiOperation(local, block.Predecessors.Count);
  159. AddPhi(block, operation);
  160. globalDefs[block.Index].TryAddOperand(GetId(operand), local);
  161. PhiOperation phi = operation.AsPhi();
  162. for (int index = 0; index < block.Predecessors.Count; index++)
  163. {
  164. BasicBlock predecessor = block.Predecessors[index];
  165. phi.SetBlock(index, predecessor);
  166. phi.SetSource(index, FindDefOnPred(globalDefs, predecessor, operand));
  167. }
  168. return local;
  169. }
  170. private static void AddPhi(BasicBlock block, Operation phi)
  171. {
  172. Operation node = block.Operations.First;
  173. if (node != default)
  174. {
  175. while (node.ListNext != default && node.ListNext.Instruction == Instruction.Phi)
  176. {
  177. node = node.ListNext;
  178. }
  179. }
  180. if (node != default && node.Instruction == Instruction.Phi)
  181. {
  182. block.Operations.AddAfter(node, phi);
  183. }
  184. else
  185. {
  186. block.Operations.AddFirst(phi);
  187. }
  188. }
  189. private static bool TryGetId(Operand operand, out int result)
  190. {
  191. if (operand != default)
  192. {
  193. if (operand.Kind == OperandKind.Register)
  194. {
  195. Register reg = operand.GetRegister();
  196. if (reg.Type == RegisterType.Integer)
  197. {
  198. result = reg.Index;
  199. }
  200. else if (reg.Type == RegisterType.Vector)
  201. {
  202. result = RegisterConsts.IntRegsCount + reg.Index;
  203. }
  204. else if (reg.Type == RegisterType.Flag)
  205. {
  206. result = RegisterConsts.IntAndVecRegsCount + reg.Index;
  207. }
  208. else /* if (reg.Type == RegisterType.FpFlag) */
  209. {
  210. result = RegisterConsts.FpFlagsOffset + reg.Index;
  211. }
  212. return true;
  213. }
  214. else if (operand.Kind == OperandKind.LocalVariable && operand.GetLocalNumber() > 0)
  215. {
  216. result = RegisterConsts.TotalCount + operand.GetLocalNumber() - 1;
  217. return true;
  218. }
  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. }