CopyResolver.cs 8.5 KB

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