CopyResolver.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using ARMeilleure.IntermediateRepresentation;
  2. using System;
  3. using System.Collections.Generic;
  4. using static ARMeilleure.IntermediateRepresentation.Operand.Factory;
  5. using static ARMeilleure.IntermediateRepresentation.Operation.Factory;
  6. namespace ARMeilleure.CodeGen.RegisterAllocators
  7. {
  8. class CopyResolver
  9. {
  10. private class ParallelCopy
  11. {
  12. private readonly struct Copy
  13. {
  14. public Register Dest { get; }
  15. public Register Source { get; }
  16. public OperandType Type { get; }
  17. public Copy(Register dest, Register source, OperandType type)
  18. {
  19. Dest = dest;
  20. Source = source;
  21. Type = type;
  22. }
  23. }
  24. private readonly List<Copy> _copies;
  25. public int Count => _copies.Count;
  26. public ParallelCopy()
  27. {
  28. _copies = new List<Copy>();
  29. }
  30. public void AddCopy(Register dest, Register source, OperandType type)
  31. {
  32. _copies.Add(new Copy(dest, source, type));
  33. }
  34. public void Sequence(List<Operation> sequence)
  35. {
  36. Dictionary<Register, Register> locations = new Dictionary<Register, Register>();
  37. Dictionary<Register, Register> sources = new Dictionary<Register, Register>();
  38. Dictionary<Register, OperandType> types = new Dictionary<Register, OperandType>();
  39. Queue<Register> pendingQueue = new Queue<Register>();
  40. Queue<Register> readyQueue = new Queue<Register>();
  41. foreach (Copy copy in _copies)
  42. {
  43. locations[copy.Source] = copy.Source;
  44. sources[copy.Dest] = copy.Source;
  45. types[copy.Dest] = copy.Type;
  46. pendingQueue.Enqueue(copy.Dest);
  47. }
  48. foreach (Copy copy in _copies)
  49. {
  50. // If the destination is not used anywhere, we can assign it immediately.
  51. if (!locations.ContainsKey(copy.Dest))
  52. {
  53. readyQueue.Enqueue(copy.Dest);
  54. }
  55. }
  56. while (pendingQueue.TryDequeue(out Register current))
  57. {
  58. Register copyDest;
  59. Register origSource;
  60. Register copySource;
  61. while (readyQueue.TryDequeue(out copyDest))
  62. {
  63. origSource = sources[copyDest];
  64. copySource = locations[origSource];
  65. OperandType type = types[copyDest];
  66. EmitCopy(sequence, GetRegister(copyDest, type), GetRegister(copySource, type));
  67. locations[origSource] = copyDest;
  68. if (origSource == copySource && sources.ContainsKey(origSource))
  69. {
  70. readyQueue.Enqueue(origSource);
  71. }
  72. }
  73. copyDest = current;
  74. origSource = sources[copyDest];
  75. copySource = locations[origSource];
  76. if (copyDest != copySource)
  77. {
  78. OperandType type = types[copyDest];
  79. type = type.IsInteger() ? OperandType.I64 : OperandType.V128;
  80. EmitXorSwap(sequence, GetRegister(copyDest, type), GetRegister(copySource, type));
  81. locations[origSource] = copyDest;
  82. Register swapOther = copySource;
  83. if (copyDest != locations[sources[copySource]])
  84. {
  85. // Find the other swap destination register.
  86. // To do that, we search all the pending registers, and pick
  87. // the one where the copy source register is equal to the
  88. // current destination register being processed (copyDest).
  89. foreach (Register pending in pendingQueue)
  90. {
  91. // Is this a copy of pending <- copyDest?
  92. if (copyDest == locations[sources[pending]])
  93. {
  94. swapOther = pending;
  95. break;
  96. }
  97. }
  98. }
  99. // The value that was previously at "copyDest" now lives on
  100. // "copySource" thanks to the swap, now we need to update the
  101. // location for the next copy that is supposed to copy the value
  102. // that used to live on "copyDest".
  103. locations[sources[swapOther]] = copySource;
  104. }
  105. }
  106. }
  107. private static void EmitCopy(List<Operation> sequence, Operand x, Operand y)
  108. {
  109. sequence.Add(Operation(Instruction.Copy, x, y));
  110. }
  111. private static void EmitXorSwap(List<Operation> sequence, Operand x, Operand y)
  112. {
  113. sequence.Add(Operation(Instruction.BitwiseExclusiveOr, x, x, y));
  114. sequence.Add(Operation(Instruction.BitwiseExclusiveOr, y, y, x));
  115. sequence.Add(Operation(Instruction.BitwiseExclusiveOr, x, x, y));
  116. }
  117. }
  118. private Queue<Operation> _fillQueue = null;
  119. private Queue<Operation> _spillQueue = null;
  120. private ParallelCopy _parallelCopy = null;
  121. public bool HasCopy { get; private set; }
  122. public void AddSplit(LiveInterval left, LiveInterval right)
  123. {
  124. if (left.Local != right.Local)
  125. {
  126. throw new ArgumentException("Intervals of different variables are not allowed.");
  127. }
  128. OperandType type = left.Local.Type;
  129. if (left.IsSpilled && !right.IsSpilled)
  130. {
  131. // Move from the stack to a register.
  132. AddSplitFill(left, right, type);
  133. }
  134. else if (!left.IsSpilled && right.IsSpilled)
  135. {
  136. // Move from a register to the stack.
  137. AddSplitSpill(left, right, type);
  138. }
  139. else if (!left.IsSpilled && !right.IsSpilled && left.Register != right.Register)
  140. {
  141. // Move from one register to another.
  142. AddSplitCopy(left, right, type);
  143. }
  144. else if (left.SpillOffset != right.SpillOffset)
  145. {
  146. // This would be the stack-to-stack move case, but this is not supported.
  147. throw new ArgumentException("Both intervals were spilled.");
  148. }
  149. }
  150. private void AddSplitFill(LiveInterval left, LiveInterval right, OperandType type)
  151. {
  152. if (_fillQueue == null)
  153. {
  154. _fillQueue = new Queue<Operation>();
  155. }
  156. Operand register = GetRegister(right.Register, type);
  157. Operand offset = Const(left.SpillOffset);
  158. _fillQueue.Enqueue(Operation(Instruction.Fill, register, offset));
  159. HasCopy = true;
  160. }
  161. private void AddSplitSpill(LiveInterval left, LiveInterval right, OperandType type)
  162. {
  163. if (_spillQueue == null)
  164. {
  165. _spillQueue = new Queue<Operation>();
  166. }
  167. Operand offset = Const(right.SpillOffset);
  168. Operand register = GetRegister(left.Register, type);
  169. _spillQueue.Enqueue(Operation(Instruction.Spill, default, offset, register));
  170. HasCopy = true;
  171. }
  172. private void AddSplitCopy(LiveInterval left, LiveInterval right, OperandType type)
  173. {
  174. if (_parallelCopy == null)
  175. {
  176. _parallelCopy = new ParallelCopy();
  177. }
  178. _parallelCopy.AddCopy(right.Register, left.Register, type);
  179. HasCopy = true;
  180. }
  181. public Operation[] Sequence()
  182. {
  183. List<Operation> sequence = new List<Operation>();
  184. if (_spillQueue != null)
  185. {
  186. while (_spillQueue.TryDequeue(out Operation spillOp))
  187. {
  188. sequence.Add(spillOp);
  189. }
  190. }
  191. _parallelCopy?.Sequence(sequence);
  192. if (_fillQueue != null)
  193. {
  194. while (_fillQueue.TryDequeue(out Operation fillOp))
  195. {
  196. sequence.Add(fillOp);
  197. }
  198. }
  199. return sequence.ToArray();
  200. }
  201. private static Operand GetRegister(Register reg, OperandType type)
  202. {
  203. return Register(reg.Index, reg.Type, type);
  204. }
  205. }
  206. }