SsaConstruction.cs 9.5 KB

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