HybridAllocator.cs 16 KB

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