HybridAllocator.cs 16 KB

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