RegisterUsage.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. using ARMeilleure.IntermediateRepresentation;
  2. using ARMeilleure.State;
  3. using System;
  4. using System.Numerics;
  5. using System.Runtime.Intrinsics;
  6. using System.Runtime.Intrinsics.X86;
  7. using static ARMeilleure.IntermediateRepresentation.Operand.Factory;
  8. using static ARMeilleure.IntermediateRepresentation.Operation.Factory;
  9. namespace ARMeilleure.Translation
  10. {
  11. static class RegisterUsage
  12. {
  13. private const int RegsCount = 32;
  14. private const int RegsMask = RegsCount - 1;
  15. private readonly struct RegisterMask : IEquatable<RegisterMask>
  16. {
  17. public long IntMask => Mask.GetElement(0);
  18. public long VecMask => Mask.GetElement(1);
  19. public Vector128<long> Mask { get; }
  20. public RegisterMask(Vector128<long> mask)
  21. {
  22. Mask = mask;
  23. }
  24. public RegisterMask(long intMask, long vecMask)
  25. {
  26. Mask = Vector128.Create(intMask, vecMask);
  27. }
  28. public static RegisterMask operator &(RegisterMask x, RegisterMask y)
  29. {
  30. if (Sse2.IsSupported)
  31. {
  32. return new RegisterMask(Sse2.And(x.Mask, y.Mask));
  33. }
  34. return new RegisterMask(x.IntMask & y.IntMask, x.VecMask & y.VecMask);
  35. }
  36. public static RegisterMask operator |(RegisterMask x, RegisterMask y)
  37. {
  38. if (Sse2.IsSupported)
  39. {
  40. return new RegisterMask(Sse2.Or(x.Mask, y.Mask));
  41. }
  42. return new RegisterMask(x.IntMask | y.IntMask, x.VecMask | y.VecMask);
  43. }
  44. public static RegisterMask operator ~(RegisterMask x)
  45. {
  46. if (Sse2.IsSupported)
  47. {
  48. return new RegisterMask(Sse2.AndNot(x.Mask, Vector128<long>.AllBitsSet));
  49. }
  50. return new RegisterMask(~x.IntMask, ~x.VecMask);
  51. }
  52. public static bool operator ==(RegisterMask x, RegisterMask y)
  53. {
  54. return x.Equals(y);
  55. }
  56. public static bool operator !=(RegisterMask x, RegisterMask y)
  57. {
  58. return !x.Equals(y);
  59. }
  60. public override bool Equals(object obj)
  61. {
  62. return obj is RegisterMask regMask && Equals(regMask);
  63. }
  64. public bool Equals(RegisterMask other)
  65. {
  66. return Mask.Equals(other.Mask);
  67. }
  68. public override int GetHashCode()
  69. {
  70. return Mask.GetHashCode();
  71. }
  72. }
  73. public static void RunPass(ControlFlowGraph cfg, ExecutionMode mode)
  74. {
  75. if (cfg.Entry.Predecessors.Count != 0)
  76. {
  77. // We expect the entry block to have no predecessors.
  78. // This is required because we have a implicit context load at the start of the function,
  79. // but if there is a jump to the start of the function, the context load would trash the modified values.
  80. // Here we insert a new entry block that will jump to the existing entry block.
  81. BasicBlock newEntry = new(cfg.Blocks.Count);
  82. cfg.UpdateEntry(newEntry);
  83. }
  84. // Compute local register inputs and outputs used inside blocks.
  85. RegisterMask[] localInputs = new RegisterMask[cfg.Blocks.Count];
  86. RegisterMask[] localOutputs = new RegisterMask[cfg.Blocks.Count];
  87. for (BasicBlock block = cfg.Blocks.First; block != null; block = block.ListNext)
  88. {
  89. for (Operation node = block.Operations.First; node != default; node = node.ListNext)
  90. {
  91. for (int index = 0; index < node.SourcesCount; index++)
  92. {
  93. Operand source = node.GetSource(index);
  94. if (source.Kind == OperandKind.Register)
  95. {
  96. Register register = source.GetRegister();
  97. localInputs[block.Index] |= GetMask(register) & ~localOutputs[block.Index];
  98. }
  99. }
  100. if (node.Destination != default && node.Destination.Kind == OperandKind.Register)
  101. {
  102. localOutputs[block.Index] |= GetMask(node.Destination.GetRegister());
  103. }
  104. }
  105. }
  106. // Compute global register inputs and outputs used across blocks.
  107. RegisterMask[] globalCmnOutputs = new RegisterMask[cfg.Blocks.Count];
  108. RegisterMask[] globalInputs = new RegisterMask[cfg.Blocks.Count];
  109. RegisterMask[] globalOutputs = new RegisterMask[cfg.Blocks.Count];
  110. bool modified;
  111. bool firstPass = true;
  112. do
  113. {
  114. modified = false;
  115. // Compute register outputs.
  116. for (int index = cfg.PostOrderBlocks.Length - 1; index >= 0; index--)
  117. {
  118. BasicBlock block = cfg.PostOrderBlocks[index];
  119. if (block.Predecessors.Count != 0 && !HasContextLoad(block))
  120. {
  121. BasicBlock predecessor = block.Predecessors[0];
  122. RegisterMask cmnOutputs = localOutputs[predecessor.Index] | globalCmnOutputs[predecessor.Index];
  123. RegisterMask outputs = globalOutputs[predecessor.Index];
  124. for (int pIndex = 1; pIndex < block.Predecessors.Count; pIndex++)
  125. {
  126. predecessor = block.Predecessors[pIndex];
  127. cmnOutputs &= localOutputs[predecessor.Index] | globalCmnOutputs[predecessor.Index];
  128. outputs |= globalOutputs[predecessor.Index];
  129. }
  130. globalInputs[block.Index] |= outputs & ~cmnOutputs;
  131. if (!firstPass)
  132. {
  133. cmnOutputs &= globalCmnOutputs[block.Index];
  134. }
  135. modified |= Exchange(globalCmnOutputs, block.Index, cmnOutputs);
  136. outputs |= localOutputs[block.Index];
  137. modified |= Exchange(globalOutputs, block.Index, globalOutputs[block.Index] | outputs);
  138. }
  139. else
  140. {
  141. modified |= Exchange(globalOutputs, block.Index, localOutputs[block.Index]);
  142. }
  143. }
  144. // Compute register inputs.
  145. for (int index = 0; index < cfg.PostOrderBlocks.Length; index++)
  146. {
  147. BasicBlock block = cfg.PostOrderBlocks[index];
  148. RegisterMask inputs = localInputs[block.Index];
  149. for (int i = 0; i < block.SuccessorsCount; i++)
  150. {
  151. inputs |= globalInputs[block.GetSuccessor(i).Index];
  152. }
  153. inputs &= ~globalCmnOutputs[block.Index];
  154. modified |= Exchange(globalInputs, block.Index, globalInputs[block.Index] | inputs);
  155. }
  156. firstPass = false;
  157. }
  158. while (modified);
  159. // Insert load and store context instructions where needed.
  160. for (BasicBlock block = cfg.Blocks.First; block != null; block = block.ListNext)
  161. {
  162. bool hasContextLoad = HasContextLoad(block);
  163. if (hasContextLoad)
  164. {
  165. block.Operations.Remove(block.Operations.First);
  166. }
  167. Operand arg = default;
  168. // The only block without any predecessor should be the entry block.
  169. // It always needs a context load as it is the first block to run.
  170. if (block == cfg.Entry || hasContextLoad)
  171. {
  172. long vecMask = globalInputs[block.Index].VecMask;
  173. long intMask = globalInputs[block.Index].IntMask;
  174. if (vecMask != 0 || intMask != 0)
  175. {
  176. arg = Local(OperandType.I64);
  177. Operation loadArg = block.Operations.AddFirst(Operation(Instruction.LoadArgument, arg, Const(0)));
  178. LoadLocals(block, vecMask, RegisterType.Vector, mode, loadArg, arg);
  179. LoadLocals(block, intMask, RegisterType.Integer, mode, loadArg, arg);
  180. }
  181. }
  182. bool hasContextStore = HasContextStore(block);
  183. if (hasContextStore)
  184. {
  185. block.Operations.Remove(block.Operations.Last);
  186. }
  187. if (EndsWithReturn(block) || hasContextStore)
  188. {
  189. long vecMask = globalOutputs[block.Index].VecMask;
  190. long intMask = globalOutputs[block.Index].IntMask;
  191. if (vecMask != 0 || intMask != 0)
  192. {
  193. if (arg == default)
  194. {
  195. arg = Local(OperandType.I64);
  196. block.Append(Operation(Instruction.LoadArgument, arg, Const(0)));
  197. }
  198. StoreLocals(block, intMask, RegisterType.Integer, mode, arg);
  199. StoreLocals(block, vecMask, RegisterType.Vector, mode, arg);
  200. }
  201. }
  202. }
  203. }
  204. private static bool HasContextLoad(BasicBlock block)
  205. {
  206. return StartsWith(block, Instruction.LoadFromContext) && block.Operations.First.SourcesCount == 0;
  207. }
  208. private static bool HasContextStore(BasicBlock block)
  209. {
  210. return EndsWith(block, Instruction.StoreToContext) && block.Operations.Last.SourcesCount == 0;
  211. }
  212. private static bool StartsWith(BasicBlock block, Instruction inst)
  213. {
  214. if (block.Operations.Count > 0)
  215. {
  216. Operation first = block.Operations.First;
  217. return first != default && first.Instruction == inst;
  218. }
  219. return false;
  220. }
  221. private static bool EndsWith(BasicBlock block, Instruction inst)
  222. {
  223. if (block.Operations.Count > 0)
  224. {
  225. Operation last = block.Operations.Last;
  226. return last != default && last.Instruction == inst;
  227. }
  228. return false;
  229. }
  230. private static RegisterMask GetMask(Register register)
  231. {
  232. long intMask = 0;
  233. long vecMask = 0;
  234. switch (register.Type)
  235. {
  236. #pragma warning disable IDE0055 // Disable formatting
  237. case RegisterType.Flag: intMask = (1L << RegsCount) << register.Index; break;
  238. case RegisterType.Integer: intMask = 1L << register.Index; break;
  239. case RegisterType.FpFlag: vecMask = (1L << RegsCount) << register.Index; break;
  240. case RegisterType.Vector: vecMask = 1L << register.Index; break;
  241. #pragma warning restore IDE0055
  242. }
  243. return new RegisterMask(intMask, vecMask);
  244. }
  245. private static bool Exchange(RegisterMask[] masks, int blkIndex, RegisterMask value)
  246. {
  247. ref RegisterMask curValue = ref masks[blkIndex];
  248. bool changed = curValue != value;
  249. curValue = value;
  250. return changed;
  251. }
  252. private static void LoadLocals(
  253. BasicBlock block,
  254. long inputs,
  255. RegisterType baseType,
  256. ExecutionMode mode,
  257. Operation loadArg,
  258. Operand arg)
  259. {
  260. while (inputs != 0)
  261. {
  262. int bit = 63 - BitOperations.LeadingZeroCount((ulong)inputs);
  263. Operand dest = GetRegFromBit(bit, baseType, mode);
  264. Operand offset = Const((long)NativeContext.GetRegisterOffset(dest.GetRegister()));
  265. Operand addr = Local(OperandType.I64);
  266. block.Operations.AddAfter(loadArg, Operation(Instruction.Load, dest, addr));
  267. block.Operations.AddAfter(loadArg, Operation(Instruction.Add, addr, arg, offset));
  268. inputs &= ~(1L << bit);
  269. }
  270. }
  271. private static void StoreLocals(
  272. BasicBlock block,
  273. long outputs,
  274. RegisterType baseType,
  275. ExecutionMode mode,
  276. Operand arg)
  277. {
  278. while (outputs != 0)
  279. {
  280. int bit = BitOperations.TrailingZeroCount(outputs);
  281. Operand source = GetRegFromBit(bit, baseType, mode);
  282. Operand offset = Const((long)NativeContext.GetRegisterOffset(source.GetRegister()));
  283. Operand addr = Local(OperandType.I64);
  284. block.Append(Operation(Instruction.Add, addr, arg, offset));
  285. block.Append(Operation(Instruction.Store, default, addr, source));
  286. outputs &= ~(1L << bit);
  287. }
  288. }
  289. private static Operand GetRegFromBit(int bit, RegisterType baseType, ExecutionMode mode)
  290. {
  291. if (bit < RegsCount)
  292. {
  293. return Register(bit, baseType, GetOperandType(baseType, mode));
  294. }
  295. else if (baseType == RegisterType.Integer)
  296. {
  297. return Register(bit & RegsMask, RegisterType.Flag, OperandType.I32);
  298. }
  299. else if (baseType == RegisterType.Vector)
  300. {
  301. return Register(bit & RegsMask, RegisterType.FpFlag, OperandType.I32);
  302. }
  303. else
  304. {
  305. throw new ArgumentOutOfRangeException(nameof(bit));
  306. }
  307. }
  308. private static OperandType GetOperandType(RegisterType type, ExecutionMode mode)
  309. {
  310. return type switch
  311. {
  312. RegisterType.Flag => OperandType.I32,
  313. RegisterType.FpFlag => OperandType.I32,
  314. RegisterType.Integer => (mode == ExecutionMode.Aarch64) ? OperandType.I64 : OperandType.I32,
  315. RegisterType.Vector => OperandType.V128,
  316. _ => throw new ArgumentException($"Invalid register type \"{type}\"."),
  317. };
  318. }
  319. private static bool EndsWithReturn(BasicBlock block)
  320. {
  321. Operation last = block.Operations.Last;
  322. return last != default && last.Instruction == Instruction.Return;
  323. }
  324. }
  325. }