CopyResolver.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using ARMeilleure.IntermediateRepresentation;
  2. using System;
  3. using System.Collections.Generic;
  4. using static ARMeilleure.IntermediateRepresentation.OperandHelper;
  5. using static ARMeilleure.IntermediateRepresentation.OperationHelper;
  6. namespace ARMeilleure.CodeGen.RegisterAllocators
  7. {
  8. class CopyResolver
  9. {
  10. private class ParallelCopy
  11. {
  12. private 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 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 = new Queue<Operation>();
  119. private Queue<Operation> _spillQueue = new Queue<Operation>();
  120. private ParallelCopy _parallelCopy;
  121. public bool HasCopy { get; private set; }
  122. public CopyResolver()
  123. {
  124. _fillQueue = new Queue<Operation>();
  125. _spillQueue = new Queue<Operation>();
  126. _parallelCopy = new ParallelCopy();
  127. }
  128. public void AddSplit(LiveInterval left, LiveInterval right)
  129. {
  130. if (left.Local != right.Local)
  131. {
  132. throw new ArgumentException("Intervals of different variables are not allowed.");
  133. }
  134. OperandType type = left.Local.Type;
  135. if (left.IsSpilled && !right.IsSpilled)
  136. {
  137. // Move from the stack to a register.
  138. AddSplitFill(left, right, type);
  139. }
  140. else if (!left.IsSpilled && right.IsSpilled)
  141. {
  142. // Move from a register to the stack.
  143. AddSplitSpill(left, right, type);
  144. }
  145. else if (!left.IsSpilled && !right.IsSpilled && left.Register != right.Register)
  146. {
  147. // Move from one register to another.
  148. AddSplitCopy(left, right, type);
  149. }
  150. else if (left.SpillOffset != right.SpillOffset)
  151. {
  152. // This would be the stack-to-stack move case, but this is not supported.
  153. throw new ArgumentException("Both intervals were spilled.");
  154. }
  155. }
  156. private void AddSplitFill(LiveInterval left, LiveInterval right, OperandType type)
  157. {
  158. Operand register = GetRegister(right.Register, type);
  159. Operand offset = Const(left.SpillOffset);
  160. _fillQueue.Enqueue(Operation(Instruction.Fill, register, offset));
  161. HasCopy = true;
  162. }
  163. private void AddSplitSpill(LiveInterval left, LiveInterval right, OperandType type)
  164. {
  165. Operand offset = Const(right.SpillOffset);
  166. Operand register = GetRegister(left.Register, type);
  167. _spillQueue.Enqueue(Operation(Instruction.Spill, null, offset, register));
  168. HasCopy = true;
  169. }
  170. private void AddSplitCopy(LiveInterval left, LiveInterval right, OperandType type)
  171. {
  172. _parallelCopy.AddCopy(right.Register, left.Register, type);
  173. HasCopy = true;
  174. }
  175. public Operation[] Sequence()
  176. {
  177. List<Operation> sequence = new List<Operation>();
  178. while (_spillQueue.TryDequeue(out Operation spillOp))
  179. {
  180. sequence.Add(spillOp);
  181. }
  182. _parallelCopy.Sequence(sequence);
  183. while (_fillQueue.TryDequeue(out Operation fillOp))
  184. {
  185. sequence.Add(fillOp);
  186. }
  187. return sequence.ToArray();
  188. }
  189. private static Operand GetRegister(Register reg, OperandType type)
  190. {
  191. return Register(reg.Index, reg.Type, type);
  192. }
  193. }
  194. }