HybridAllocator.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. using ARMeilleure.Common;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.Translation;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using static ARMeilleure.IntermediateRepresentation.OperandHelper;
  7. namespace ARMeilleure.CodeGen.RegisterAllocators
  8. {
  9. class HybridAllocator : IRegisterAllocator
  10. {
  11. private const int RegistersCount = 16;
  12. private const int MaxIROperands = 4;
  13. private struct BlockInfo
  14. {
  15. public bool HasCall { get; }
  16. public int IntFixedRegisters { get; }
  17. public int VecFixedRegisters { get; }
  18. public BlockInfo(bool hasCall, int intFixedRegisters, int vecFixedRegisters)
  19. {
  20. HasCall = hasCall;
  21. IntFixedRegisters = intFixedRegisters;
  22. VecFixedRegisters = vecFixedRegisters;
  23. }
  24. }
  25. private class LocalInfo
  26. {
  27. public int Uses { get; set; }
  28. public int UseCount { get; set; }
  29. public bool PreAllocated { get; set; }
  30. public int Register { get; set; }
  31. public int SpillOffset { get; set; }
  32. public int Sequence { get; set; }
  33. public Operand Temp { get; set; }
  34. public OperandType Type { get; }
  35. private int _first;
  36. private int _last;
  37. public bool IsBlockLocal => _first == _last;
  38. public LocalInfo(OperandType type, int uses)
  39. {
  40. Uses = uses;
  41. Type = type;
  42. _first = -1;
  43. _last = -1;
  44. }
  45. public void SetBlockIndex(int blkIndex)
  46. {
  47. if (_first == -1 || blkIndex < _first)
  48. {
  49. _first = blkIndex;
  50. }
  51. if (_last == -1 || blkIndex > _last)
  52. {
  53. _last = blkIndex;
  54. }
  55. }
  56. }
  57. public AllocationResult RunPass(
  58. ControlFlowGraph cfg,
  59. StackAllocator stackAlloc,
  60. RegisterMasks regMasks)
  61. {
  62. int intUsedRegisters = 0;
  63. int vecUsedRegisters = 0;
  64. int intFreeRegisters = regMasks.IntAvailableRegisters;
  65. int vecFreeRegisters = regMasks.VecAvailableRegisters;
  66. BlockInfo[] blockInfo = new BlockInfo[cfg.Blocks.Count];
  67. List<LocalInfo> locInfo = new List<LocalInfo>();
  68. for (int index = cfg.PostOrderBlocks.Length - 1; index >= 0; index--)
  69. {
  70. BasicBlock block = cfg.PostOrderBlocks[index];
  71. int intFixedRegisters = 0;
  72. int vecFixedRegisters = 0;
  73. bool hasCall = false;
  74. foreach (Node node in block.Operations)
  75. {
  76. if (node is Operation operation && operation.Instruction == Instruction.Call)
  77. {
  78. hasCall = true;
  79. }
  80. for (int srcIndex = 0; srcIndex < node.SourcesCount; srcIndex++)
  81. {
  82. Operand source = node.GetSource(srcIndex);
  83. if (source.Kind == OperandKind.LocalVariable)
  84. {
  85. locInfo[source.AsInt32() - 1].SetBlockIndex(block.Index);
  86. }
  87. }
  88. for (int dstIndex = 0; dstIndex < node.DestinationsCount; dstIndex++)
  89. {
  90. Operand dest = node.GetDestination(dstIndex);
  91. if (dest.Kind == OperandKind.LocalVariable)
  92. {
  93. LocalInfo info;
  94. if (dest.Value != 0)
  95. {
  96. info = locInfo[dest.AsInt32() - 1];
  97. }
  98. else
  99. {
  100. dest.NumberLocal(locInfo.Count + 1);
  101. info = new LocalInfo(dest.Type, UsesCount(dest));
  102. locInfo.Add(info);
  103. }
  104. info.SetBlockIndex(block.Index);
  105. }
  106. else if (dest.Kind == OperandKind.Register)
  107. {
  108. if (dest.Type.IsInteger())
  109. {
  110. intFixedRegisters |= 1 << dest.GetRegister().Index;
  111. }
  112. else
  113. {
  114. vecFixedRegisters |= 1 << dest.GetRegister().Index;
  115. }
  116. }
  117. }
  118. }
  119. blockInfo[block.Index] = new BlockInfo(hasCall, intFixedRegisters, vecFixedRegisters);
  120. }
  121. int sequence = 0;
  122. for (int index = cfg.PostOrderBlocks.Length - 1; index >= 0; index--)
  123. {
  124. BasicBlock block = cfg.PostOrderBlocks[index];
  125. BlockInfo blkInfo = blockInfo[block.Index];
  126. int intLocalFreeRegisters = intFreeRegisters & ~blkInfo.IntFixedRegisters;
  127. int vecLocalFreeRegisters = vecFreeRegisters & ~blkInfo.VecFixedRegisters;
  128. int intCallerSavedRegisters = blkInfo.HasCall ? regMasks.IntCallerSavedRegisters : 0;
  129. int vecCallerSavedRegisters = blkInfo.HasCall ? regMasks.VecCallerSavedRegisters : 0;
  130. int intSpillTempRegisters = SelectSpillTemps(
  131. intCallerSavedRegisters & ~blkInfo.IntFixedRegisters,
  132. intLocalFreeRegisters);
  133. int vecSpillTempRegisters = SelectSpillTemps(
  134. vecCallerSavedRegisters & ~blkInfo.VecFixedRegisters,
  135. vecLocalFreeRegisters);
  136. intLocalFreeRegisters &= ~(intSpillTempRegisters | intCallerSavedRegisters);
  137. vecLocalFreeRegisters &= ~(vecSpillTempRegisters | vecCallerSavedRegisters);
  138. for (LinkedListNode<Node> llNode = block.Operations.First; llNode != null; llNode = llNode.Next)
  139. {
  140. Node node = llNode.Value;
  141. int intLocalUse = 0;
  142. int vecLocalUse = 0;
  143. for (int srcIndex = 0; srcIndex < node.SourcesCount; srcIndex++)
  144. {
  145. Operand source = node.GetSource(srcIndex);
  146. if (source.Kind != OperandKind.LocalVariable)
  147. {
  148. continue;
  149. }
  150. LocalInfo info = locInfo[source.AsInt32() - 1];
  151. info.UseCount++;
  152. Debug.Assert(info.UseCount <= info.Uses);
  153. if (info.Register != -1)
  154. {
  155. node.SetSource(srcIndex, Register(info.Register, source.Type.ToRegisterType(), source.Type));
  156. if (info.UseCount == info.Uses && !info.PreAllocated)
  157. {
  158. if (source.Type.IsInteger())
  159. {
  160. intLocalFreeRegisters |= 1 << info.Register;
  161. }
  162. else
  163. {
  164. vecLocalFreeRegisters |= 1 << info.Register;
  165. }
  166. }
  167. }
  168. else
  169. {
  170. Operand temp = info.Temp;
  171. if (temp == null || info.Sequence != sequence)
  172. {
  173. temp = source.Type.IsInteger()
  174. ? GetSpillTemp(source, intSpillTempRegisters, ref intLocalUse)
  175. : GetSpillTemp(source, vecSpillTempRegisters, ref vecLocalUse);
  176. info.Sequence = sequence;
  177. info.Temp = temp;
  178. }
  179. node.SetSource(srcIndex, temp);
  180. Operation fillOp = new Operation(Instruction.Fill, temp, Const(info.SpillOffset));
  181. block.Operations.AddBefore(llNode, fillOp);
  182. }
  183. }
  184. int intLocalAsg = 0;
  185. int vecLocalAsg = 0;
  186. for (int dstIndex = 0; dstIndex < node.DestinationsCount; dstIndex++)
  187. {
  188. Operand dest = node.GetDestination(dstIndex);
  189. if (dest.Kind != OperandKind.LocalVariable)
  190. {
  191. continue;
  192. }
  193. LocalInfo info = locInfo[dest.AsInt32() - 1];
  194. if (info.UseCount == 0 && !info.PreAllocated)
  195. {
  196. int mask = dest.Type.IsInteger()
  197. ? intLocalFreeRegisters
  198. : vecLocalFreeRegisters;
  199. if (info.IsBlockLocal && mask != 0)
  200. {
  201. int selectedReg = BitUtils.LowestBitSet(mask);
  202. info.Register = selectedReg;
  203. if (dest.Type.IsInteger())
  204. {
  205. intLocalFreeRegisters &= ~(1 << selectedReg);
  206. intUsedRegisters |= 1 << selectedReg;
  207. }
  208. else
  209. {
  210. vecLocalFreeRegisters &= ~(1 << selectedReg);
  211. vecUsedRegisters |= 1 << selectedReg;
  212. }
  213. }
  214. else
  215. {
  216. info.Register = -1;
  217. info.SpillOffset = stackAlloc.Allocate(dest.Type.GetSizeInBytes());
  218. }
  219. }
  220. info.UseCount++;
  221. Debug.Assert(info.UseCount <= info.Uses);
  222. if (info.Register != -1)
  223. {
  224. node.SetDestination(dstIndex, Register(info.Register, dest.Type.ToRegisterType(), dest.Type));
  225. }
  226. else
  227. {
  228. Operand temp = info.Temp;
  229. if (temp == null || info.Sequence != sequence)
  230. {
  231. temp = dest.Type.IsInteger()
  232. ? GetSpillTemp(dest, intSpillTempRegisters, ref intLocalAsg)
  233. : GetSpillTemp(dest, vecSpillTempRegisters, ref vecLocalAsg);
  234. info.Sequence = sequence;
  235. info.Temp = temp;
  236. }
  237. node.SetDestination(dstIndex, temp);
  238. Operation spillOp = new Operation(Instruction.Spill, null, Const(info.SpillOffset), temp);
  239. llNode = block.Operations.AddAfter(llNode, spillOp);
  240. }
  241. }
  242. sequence++;
  243. intUsedRegisters |= intLocalAsg | intLocalUse;
  244. vecUsedRegisters |= vecLocalAsg | vecLocalUse;
  245. }
  246. }
  247. return new AllocationResult(intUsedRegisters, vecUsedRegisters, stackAlloc.TotalSize);
  248. }
  249. private static int SelectSpillTemps(int mask0, int mask1)
  250. {
  251. int selection = 0;
  252. int count = 0;
  253. while (count < MaxIROperands && mask0 != 0)
  254. {
  255. int mask = mask0 & -mask0;
  256. selection |= mask;
  257. mask0 &= ~mask;
  258. count++;
  259. }
  260. while (count < MaxIROperands && mask1 != 0)
  261. {
  262. int mask = mask1 & -mask1;
  263. selection |= mask;
  264. mask1 &= ~mask;
  265. count++;
  266. }
  267. Debug.Assert(count == MaxIROperands, "No enough registers for spill temps.");
  268. return selection;
  269. }
  270. private static Operand GetSpillTemp(Operand local, int freeMask, ref int useMask)
  271. {
  272. int selectedReg = BitUtils.LowestBitSet(freeMask & ~useMask);
  273. useMask |= 1 << selectedReg;
  274. return Register(selectedReg, local.Type.ToRegisterType(), local.Type);
  275. }
  276. private static int UsesCount(Operand local)
  277. {
  278. return local.Assignments.Count + local.Uses.Count;
  279. }
  280. private static IEnumerable<BasicBlock> Successors(BasicBlock block)
  281. {
  282. if (block.Next != null)
  283. {
  284. yield return block.Next;
  285. }
  286. if (block.Branch != null)
  287. {
  288. yield return block.Branch;
  289. }
  290. }
  291. }
  292. }