HybridAllocator.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. BlockInfo[] blockInfo = new BlockInfo[cfg.Blocks.Count];
  68. List<LocalInfo> locInfo = new List<LocalInfo>();
  69. for (int index = cfg.PostOrderBlocks.Length - 1; index >= 0; index--)
  70. {
  71. BasicBlock block = cfg.PostOrderBlocks[index];
  72. int intFixedRegisters = 0;
  73. int vecFixedRegisters = 0;
  74. bool hasCall = false;
  75. for (Node node = block.Operations.First; node != null; node = node.ListNext)
  76. {
  77. if (node is Operation operation && operation.Instruction == Instruction.Call)
  78. {
  79. hasCall = true;
  80. }
  81. for (int srcIndex = 0; srcIndex < node.SourcesCount; srcIndex++)
  82. {
  83. Operand source = node.GetSource(srcIndex);
  84. if (source.Kind == OperandKind.LocalVariable)
  85. {
  86. locInfo[source.AsInt32() - 1].SetBlockIndex(block.Index);
  87. }
  88. else if (source.Kind == OperandKind.Memory)
  89. {
  90. MemoryOperand memOp = (MemoryOperand)source;
  91. if (memOp.BaseAddress != null)
  92. {
  93. locInfo[memOp.BaseAddress.AsInt32() - 1].SetBlockIndex(block.Index);
  94. }
  95. if (memOp.Index != null)
  96. {
  97. locInfo[memOp.Index.AsInt32() - 1].SetBlockIndex(block.Index);
  98. }
  99. }
  100. }
  101. for (int dstIndex = 0; dstIndex < node.DestinationsCount; dstIndex++)
  102. {
  103. Operand dest = node.GetDestination(dstIndex);
  104. if (dest.Kind == OperandKind.LocalVariable)
  105. {
  106. LocalInfo info;
  107. if (dest.Value != 0)
  108. {
  109. info = locInfo[dest.AsInt32() - 1];
  110. }
  111. else
  112. {
  113. dest.NumberLocal(locInfo.Count + 1);
  114. info = new LocalInfo(dest.Type, UsesCount(dest));
  115. locInfo.Add(info);
  116. }
  117. info.SetBlockIndex(block.Index);
  118. }
  119. else if (dest.Kind == OperandKind.Register)
  120. {
  121. if (dest.Type.IsInteger())
  122. {
  123. intFixedRegisters |= 1 << dest.GetRegister().Index;
  124. }
  125. else
  126. {
  127. vecFixedRegisters |= 1 << dest.GetRegister().Index;
  128. }
  129. }
  130. }
  131. }
  132. blockInfo[block.Index] = new BlockInfo(hasCall, intFixedRegisters, vecFixedRegisters);
  133. }
  134. int sequence = 0;
  135. for (int index = cfg.PostOrderBlocks.Length - 1; index >= 0; index--)
  136. {
  137. BasicBlock block = cfg.PostOrderBlocks[index];
  138. BlockInfo blkInfo = blockInfo[block.Index];
  139. int intLocalFreeRegisters = intFreeRegisters & ~blkInfo.IntFixedRegisters;
  140. int vecLocalFreeRegisters = vecFreeRegisters & ~blkInfo.VecFixedRegisters;
  141. int intCallerSavedRegisters = blkInfo.HasCall ? regMasks.IntCallerSavedRegisters : 0;
  142. int vecCallerSavedRegisters = blkInfo.HasCall ? regMasks.VecCallerSavedRegisters : 0;
  143. int intSpillTempRegisters = SelectSpillTemps(
  144. intCallerSavedRegisters & ~blkInfo.IntFixedRegisters,
  145. intLocalFreeRegisters);
  146. int vecSpillTempRegisters = SelectSpillTemps(
  147. vecCallerSavedRegisters & ~blkInfo.VecFixedRegisters,
  148. vecLocalFreeRegisters);
  149. intLocalFreeRegisters &= ~(intSpillTempRegisters | intCallerSavedRegisters);
  150. vecLocalFreeRegisters &= ~(vecSpillTempRegisters | vecCallerSavedRegisters);
  151. for (Node node = block.Operations.First; node != null; node = node.ListNext)
  152. {
  153. int intLocalUse = 0;
  154. int vecLocalUse = 0;
  155. void AllocateRegister(Operand source, MemoryOperand memOp, int srcIndex)
  156. {
  157. LocalInfo info = locInfo[source.AsInt32() - 1];
  158. info.UseCount++;
  159. Debug.Assert(info.UseCount <= info.Uses);
  160. if (info.Register != -1)
  161. {
  162. Operand reg = Register(info.Register, source.Type.ToRegisterType(), source.Type);
  163. if (memOp != null)
  164. {
  165. if (srcIndex == 0)
  166. {
  167. memOp.BaseAddress = reg;
  168. }
  169. else /* if (srcIndex == 1) */
  170. {
  171. memOp.Index = reg;
  172. }
  173. }
  174. else
  175. {
  176. node.SetSource(srcIndex, reg);
  177. }
  178. if (info.UseCount == info.Uses && !info.PreAllocated)
  179. {
  180. if (source.Type.IsInteger())
  181. {
  182. intLocalFreeRegisters |= 1 << info.Register;
  183. }
  184. else
  185. {
  186. vecLocalFreeRegisters |= 1 << info.Register;
  187. }
  188. }
  189. }
  190. else
  191. {
  192. Operand temp = info.Temp;
  193. if (temp == null || info.Sequence != sequence)
  194. {
  195. temp = source.Type.IsInteger()
  196. ? GetSpillTemp(source, intSpillTempRegisters, ref intLocalUse)
  197. : GetSpillTemp(source, vecSpillTempRegisters, ref vecLocalUse);
  198. info.Sequence = sequence;
  199. info.Temp = temp;
  200. }
  201. if (memOp != null)
  202. {
  203. if (srcIndex == 0)
  204. {
  205. memOp.BaseAddress = temp;
  206. }
  207. else /* if (srcIndex == 1) */
  208. {
  209. memOp.Index = temp;
  210. }
  211. }
  212. else
  213. {
  214. node.SetSource(srcIndex, temp);
  215. }
  216. Operation fillOp = Operation(Instruction.Fill, temp, Const(info.SpillOffset));
  217. block.Operations.AddBefore(node, fillOp);
  218. }
  219. }
  220. for (int srcIndex = 0; srcIndex < node.SourcesCount; srcIndex++)
  221. {
  222. Operand source = node.GetSource(srcIndex);
  223. if (source.Kind == OperandKind.LocalVariable)
  224. {
  225. AllocateRegister(source, null, srcIndex);
  226. }
  227. else if (source.Kind == OperandKind.Memory)
  228. {
  229. MemoryOperand memOp = (MemoryOperand)source;
  230. if (memOp.BaseAddress != null)
  231. {
  232. AllocateRegister(memOp.BaseAddress, memOp, 0);
  233. }
  234. if (memOp.Index != null)
  235. {
  236. AllocateRegister(memOp.Index, memOp, 1);
  237. }
  238. }
  239. }
  240. int intLocalAsg = 0;
  241. int vecLocalAsg = 0;
  242. for (int dstIndex = 0; dstIndex < node.DestinationsCount; dstIndex++)
  243. {
  244. Operand dest = node.GetDestination(dstIndex);
  245. if (dest.Kind != OperandKind.LocalVariable)
  246. {
  247. continue;
  248. }
  249. LocalInfo info = locInfo[dest.AsInt32() - 1];
  250. if (info.UseCount == 0 && !info.PreAllocated)
  251. {
  252. int mask = dest.Type.IsInteger()
  253. ? intLocalFreeRegisters
  254. : vecLocalFreeRegisters;
  255. if (info.IsBlockLocal && mask != 0)
  256. {
  257. int selectedReg = BitOperations.TrailingZeroCount(mask);
  258. info.Register = selectedReg;
  259. if (dest.Type.IsInteger())
  260. {
  261. intLocalFreeRegisters &= ~(1 << selectedReg);
  262. intUsedRegisters |= 1 << selectedReg;
  263. }
  264. else
  265. {
  266. vecLocalFreeRegisters &= ~(1 << selectedReg);
  267. vecUsedRegisters |= 1 << selectedReg;
  268. }
  269. }
  270. else
  271. {
  272. info.Register = -1;
  273. info.SpillOffset = stackAlloc.Allocate(dest.Type.GetSizeInBytes());
  274. }
  275. }
  276. info.UseCount++;
  277. Debug.Assert(info.UseCount <= info.Uses);
  278. if (info.Register != -1)
  279. {
  280. node.SetDestination(dstIndex, Register(info.Register, dest.Type.ToRegisterType(), dest.Type));
  281. }
  282. else
  283. {
  284. Operand temp = info.Temp;
  285. if (temp == null || info.Sequence != sequence)
  286. {
  287. temp = dest.Type.IsInteger()
  288. ? GetSpillTemp(dest, intSpillTempRegisters, ref intLocalAsg)
  289. : GetSpillTemp(dest, vecSpillTempRegisters, ref vecLocalAsg);
  290. info.Sequence = sequence;
  291. info.Temp = temp;
  292. }
  293. node.SetDestination(dstIndex, temp);
  294. Operation spillOp = Operation(Instruction.Spill, null, Const(info.SpillOffset), temp);
  295. block.Operations.AddAfter(node, spillOp);
  296. node = spillOp;
  297. }
  298. }
  299. sequence++;
  300. intUsedRegisters |= intLocalAsg | intLocalUse;
  301. vecUsedRegisters |= vecLocalAsg | vecLocalUse;
  302. }
  303. }
  304. return new AllocationResult(intUsedRegisters, vecUsedRegisters, stackAlloc.TotalSize);
  305. }
  306. private static int SelectSpillTemps(int mask0, int mask1)
  307. {
  308. int selection = 0;
  309. int count = 0;
  310. while (count < MaxIROperands && mask0 != 0)
  311. {
  312. int mask = mask0 & -mask0;
  313. selection |= mask;
  314. mask0 &= ~mask;
  315. count++;
  316. }
  317. while (count < MaxIROperands && mask1 != 0)
  318. {
  319. int mask = mask1 & -mask1;
  320. selection |= mask;
  321. mask1 &= ~mask;
  322. count++;
  323. }
  324. Debug.Assert(count == MaxIROperands, "No enough registers for spill temps.");
  325. return selection;
  326. }
  327. private static Operand GetSpillTemp(Operand local, int freeMask, ref int useMask)
  328. {
  329. int selectedReg = BitOperations.TrailingZeroCount(freeMask & ~useMask);
  330. useMask |= 1 << selectedReg;
  331. return Register(selectedReg, local.Type.ToRegisterType(), local.Type);
  332. }
  333. private static int UsesCount(Operand local)
  334. {
  335. return local.Assignments.Count + local.Uses.Count;
  336. }
  337. private static IEnumerable<BasicBlock> Successors(BasicBlock block)
  338. {
  339. if (block.Next != null)
  340. {
  341. yield return block.Next;
  342. }
  343. if (block.Branch != null)
  344. {
  345. yield return block.Branch;
  346. }
  347. }
  348. }
  349. }