PreAllocator.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. using ARMeilleure.CodeGen.RegisterAllocators;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.Translation;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using static ARMeilleure.IntermediateRepresentation.Operand.Factory;
  8. using static ARMeilleure.IntermediateRepresentation.Operation.Factory;
  9. namespace ARMeilleure.CodeGen.Arm64
  10. {
  11. static class PreAllocator
  12. {
  13. private class ConstantDict
  14. {
  15. private readonly Dictionary<(ulong, OperandType), Operand> _constants;
  16. public ConstantDict()
  17. {
  18. _constants = new Dictionary<(ulong, OperandType), Operand>();
  19. }
  20. public void Add(ulong value, OperandType type, Operand local)
  21. {
  22. _constants.Add((value, type), local);
  23. }
  24. public bool TryGetValue(ulong value, OperandType type, out Operand local)
  25. {
  26. return _constants.TryGetValue((value, type), out local);
  27. }
  28. }
  29. public static void RunPass(CompilerContext cctx, StackAllocator stackAlloc, out int maxCallArgs)
  30. {
  31. maxCallArgs = -1;
  32. Span<Operation> buffer = default;
  33. Operand[] preservedArgs = new Operand[CallingConvention.GetArgumentsOnRegsCount()];
  34. for (BasicBlock block = cctx.Cfg.Blocks.First; block != null; block = block.ListNext)
  35. {
  36. ConstantDict constants = new ConstantDict();
  37. Operation nextNode;
  38. for (Operation node = block.Operations.First; node != default; node = nextNode)
  39. {
  40. nextNode = node.ListNext;
  41. if (node.Instruction == Instruction.Phi)
  42. {
  43. continue;
  44. }
  45. InsertConstantRegCopies(constants, block.Operations, node);
  46. InsertDestructiveRegCopies(block.Operations, node);
  47. switch (node.Instruction)
  48. {
  49. case Instruction.Call:
  50. // Get the maximum number of arguments used on a call.
  51. // On windows, when a struct is returned from the call,
  52. // we also need to pass the pointer where the struct
  53. // should be written on the first argument.
  54. int argsCount = node.SourcesCount - 1;
  55. if (node.Destination != default && node.Destination.Type == OperandType.V128)
  56. {
  57. argsCount++;
  58. }
  59. if (maxCallArgs < argsCount)
  60. {
  61. maxCallArgs = argsCount;
  62. }
  63. // Copy values to registers expected by the function
  64. // being called, as mandated by the ABI.
  65. InsertCallCopies(constants, block.Operations, node);
  66. break;
  67. case Instruction.CompareAndSwap:
  68. case Instruction.CompareAndSwap16:
  69. case Instruction.CompareAndSwap8:
  70. nextNode = GenerateCompareAndSwap(block.Operations, node);
  71. break;
  72. case Instruction.LoadArgument:
  73. nextNode = InsertLoadArgumentCopy(cctx, ref buffer, block.Operations, preservedArgs, node);
  74. break;
  75. case Instruction.Return:
  76. InsertReturnCopy(block.Operations, node);
  77. break;
  78. case Instruction.Tailcall:
  79. InsertTailcallCopies(constants, block.Operations, stackAlloc, node, node);
  80. break;
  81. }
  82. }
  83. }
  84. }
  85. private static void InsertConstantRegCopies(ConstantDict constants, IntrusiveList<Operation> nodes, Operation node)
  86. {
  87. if (node.SourcesCount == 0 || IsIntrinsicWithConst(node))
  88. {
  89. return;
  90. }
  91. Instruction inst = node.Instruction;
  92. Operand src1 = node.GetSource(0);
  93. Operand src2;
  94. if (src1.Kind == OperandKind.Constant)
  95. {
  96. if (!src1.Type.IsInteger())
  97. {
  98. // Handle non-integer types (FP32, FP64 and V128).
  99. // For instructions without an immediate operand, we do the following:
  100. // - Insert a copy with the constant value (as integer) to a GPR.
  101. // - Insert a copy from the GPR to a XMM register.
  102. // - Replace the constant use with the XMM register.
  103. src1 = AddFloatConstantCopy(constants, nodes, node, src1);
  104. node.SetSource(0, src1);
  105. }
  106. else if (!HasConstSrc1(node, src1.Value))
  107. {
  108. // Handle integer types.
  109. // Most ALU instructions accepts a 32-bits immediate on the second operand.
  110. // We need to ensure the following:
  111. // - If the constant is on operand 1, we need to move it.
  112. // -- But first, we try to swap operand 1 and 2 if the instruction is commutative.
  113. // -- Doing so may allow us to encode the constant as operand 2 and avoid a copy.
  114. // - If the constant is on operand 2, we check if the instruction supports it,
  115. // if not, we also add a copy. 64-bits constants are usually not supported.
  116. if (IsCommutative(node))
  117. {
  118. src2 = node.GetSource(1);
  119. Operand temp = src1;
  120. src1 = src2;
  121. src2 = temp;
  122. node.SetSource(0, src1);
  123. node.SetSource(1, src2);
  124. }
  125. if (src1.Kind == OperandKind.Constant)
  126. {
  127. src1 = AddIntConstantCopy(constants, nodes, node, src1);
  128. node.SetSource(0, src1);
  129. }
  130. }
  131. }
  132. if (node.SourcesCount < 2)
  133. {
  134. return;
  135. }
  136. src2 = node.GetSource(1);
  137. if (src2.Kind == OperandKind.Constant)
  138. {
  139. if (!src2.Type.IsInteger())
  140. {
  141. src2 = AddFloatConstantCopy(constants, nodes, node, src2);
  142. node.SetSource(1, src2);
  143. }
  144. else if (!HasConstSrc2(inst, src2))
  145. {
  146. src2 = AddIntConstantCopy(constants, nodes, node, src2);
  147. node.SetSource(1, src2);
  148. }
  149. }
  150. if (node.SourcesCount < 3 ||
  151. node.Instruction == Instruction.BranchIf ||
  152. node.Instruction == Instruction.Compare ||
  153. node.Instruction == Instruction.VectorInsert ||
  154. node.Instruction == Instruction.VectorInsert16 ||
  155. node.Instruction == Instruction.VectorInsert8)
  156. {
  157. return;
  158. }
  159. for (int srcIndex = 2; srcIndex < node.SourcesCount; srcIndex++)
  160. {
  161. Operand src = node.GetSource(srcIndex);
  162. if (src.Kind == OperandKind.Constant)
  163. {
  164. if (!src.Type.IsInteger())
  165. {
  166. src = AddFloatConstantCopy(constants, nodes, node, src);
  167. node.SetSource(srcIndex, src);
  168. }
  169. else
  170. {
  171. src = AddIntConstantCopy(constants, nodes, node, src);
  172. node.SetSource(srcIndex, src);
  173. }
  174. }
  175. }
  176. }
  177. private static void InsertDestructiveRegCopies(IntrusiveList<Operation> nodes, Operation node)
  178. {
  179. if (node.Destination == default || node.SourcesCount == 0)
  180. {
  181. return;
  182. }
  183. Operand dest = node.Destination;
  184. Operand src1 = node.GetSource(0);
  185. if (IsSameOperandDestSrc1(node) && src1.Kind == OperandKind.LocalVariable)
  186. {
  187. bool useNewLocal = false;
  188. for (int srcIndex = 1; srcIndex < node.SourcesCount; srcIndex++)
  189. {
  190. if (node.GetSource(srcIndex) == dest)
  191. {
  192. useNewLocal = true;
  193. break;
  194. }
  195. }
  196. if (useNewLocal)
  197. {
  198. // Dest is being used as some source already, we need to use a new
  199. // local to store the temporary value, otherwise the value on dest
  200. // local would be overwritten.
  201. Operand temp = Local(dest.Type);
  202. nodes.AddBefore(node, Operation(Instruction.Copy, temp, src1));
  203. node.SetSource(0, temp);
  204. nodes.AddAfter(node, Operation(Instruction.Copy, dest, temp));
  205. node.Destination = temp;
  206. }
  207. else
  208. {
  209. nodes.AddBefore(node, Operation(Instruction.Copy, dest, src1));
  210. node.SetSource(0, dest);
  211. }
  212. }
  213. }
  214. private static void InsertCallCopies(ConstantDict constants, IntrusiveList<Operation> nodes, Operation node)
  215. {
  216. Operation operation = node;
  217. Operand dest = operation.Destination;
  218. List<Operand> sources = new List<Operand>
  219. {
  220. operation.GetSource(0)
  221. };
  222. int argsCount = operation.SourcesCount - 1;
  223. int intMax = CallingConvention.GetArgumentsOnRegsCount();
  224. int vecMax = CallingConvention.GetArgumentsOnRegsCount();
  225. int intCount = 0;
  226. int vecCount = 0;
  227. int stackOffset = 0;
  228. for (int index = 0; index < argsCount; index++)
  229. {
  230. Operand source = operation.GetSource(index + 1);
  231. bool passOnReg;
  232. if (source.Type.IsInteger())
  233. {
  234. passOnReg = intCount < intMax;
  235. }
  236. else if (source.Type == OperandType.V128)
  237. {
  238. passOnReg = intCount + 1 < intMax;
  239. }
  240. else
  241. {
  242. passOnReg = vecCount < vecMax;
  243. }
  244. if (source.Type == OperandType.V128 && passOnReg)
  245. {
  246. // V128 is a struct, we pass each half on a GPR if possible.
  247. Operand argReg = Gpr(CallingConvention.GetIntArgumentRegister(intCount++), OperandType.I64);
  248. Operand argReg2 = Gpr(CallingConvention.GetIntArgumentRegister(intCount++), OperandType.I64);
  249. nodes.AddBefore(node, Operation(Instruction.VectorExtract, argReg, source, Const(0)));
  250. nodes.AddBefore(node, Operation(Instruction.VectorExtract, argReg2, source, Const(1)));
  251. continue;
  252. }
  253. if (passOnReg)
  254. {
  255. Operand argReg = source.Type.IsInteger()
  256. ? Gpr(CallingConvention.GetIntArgumentRegister(intCount++), source.Type)
  257. : Xmm(CallingConvention.GetVecArgumentRegister(vecCount++), source.Type);
  258. Operation copyOp = Operation(Instruction.Copy, argReg, source);
  259. InsertConstantRegCopies(constants, nodes, nodes.AddBefore(node, copyOp));
  260. sources.Add(argReg);
  261. }
  262. else
  263. {
  264. Operand offset = Const(stackOffset);
  265. Operation spillOp = Operation(Instruction.SpillArg, default, offset, source);
  266. InsertConstantRegCopies(constants, nodes, nodes.AddBefore(node, spillOp));
  267. stackOffset += source.Type.GetSizeInBytes();
  268. }
  269. }
  270. if (dest != default)
  271. {
  272. if (dest.Type == OperandType.V128)
  273. {
  274. Operand retLReg = Gpr(CallingConvention.GetIntReturnRegister(), OperandType.I64);
  275. Operand retHReg = Gpr(CallingConvention.GetIntReturnRegisterHigh(), OperandType.I64);
  276. node = nodes.AddAfter(node, Operation(Instruction.VectorCreateScalar, dest, retLReg));
  277. nodes.AddAfter(node, Operation(Instruction.VectorInsert, dest, dest, retHReg, Const(1)));
  278. operation.Destination = default;
  279. }
  280. else
  281. {
  282. Operand retReg = dest.Type.IsInteger()
  283. ? Gpr(CallingConvention.GetIntReturnRegister(), dest.Type)
  284. : Xmm(CallingConvention.GetVecReturnRegister(), dest.Type);
  285. Operation copyOp = Operation(Instruction.Copy, dest, retReg);
  286. nodes.AddAfter(node, copyOp);
  287. operation.Destination = retReg;
  288. }
  289. }
  290. operation.SetSources(sources.ToArray());
  291. }
  292. private static void InsertTailcallCopies(
  293. ConstantDict constants,
  294. IntrusiveList<Operation> nodes,
  295. StackAllocator stackAlloc,
  296. Operation node,
  297. Operation operation)
  298. {
  299. List<Operand> sources = new List<Operand>
  300. {
  301. operation.GetSource(0)
  302. };
  303. int argsCount = operation.SourcesCount - 1;
  304. int intMax = CallingConvention.GetArgumentsOnRegsCount();
  305. int vecMax = CallingConvention.GetArgumentsOnRegsCount();
  306. int intCount = 0;
  307. int vecCount = 0;
  308. // Handle arguments passed on registers.
  309. for (int index = 0; index < argsCount; index++)
  310. {
  311. Operand source = operation.GetSource(1 + index);
  312. bool passOnReg;
  313. if (source.Type.IsInteger())
  314. {
  315. passOnReg = intCount + 1 < intMax;
  316. }
  317. else
  318. {
  319. passOnReg = vecCount < vecMax;
  320. }
  321. if (source.Type == OperandType.V128 && passOnReg)
  322. {
  323. // V128 is a struct, we pass each half on a GPR if possible.
  324. Operand argReg = Gpr(CallingConvention.GetIntArgumentRegister(intCount++), OperandType.I64);
  325. Operand argReg2 = Gpr(CallingConvention.GetIntArgumentRegister(intCount++), OperandType.I64);
  326. nodes.AddBefore(node, Operation(Instruction.VectorExtract, argReg, source, Const(0)));
  327. nodes.AddBefore(node, Operation(Instruction.VectorExtract, argReg2, source, Const(1)));
  328. continue;
  329. }
  330. if (passOnReg)
  331. {
  332. Operand argReg = source.Type.IsInteger()
  333. ? Gpr(CallingConvention.GetIntArgumentRegister(intCount++), source.Type)
  334. : Xmm(CallingConvention.GetVecArgumentRegister(vecCount++), source.Type);
  335. Operation copyOp = Operation(Instruction.Copy, argReg, source);
  336. InsertConstantRegCopies(constants, nodes, nodes.AddBefore(node, copyOp));
  337. sources.Add(argReg);
  338. }
  339. else
  340. {
  341. throw new NotImplementedException("Spilling is not currently supported for tail calls. (too many arguments)");
  342. }
  343. }
  344. // The target address must be on the return registers, since we
  345. // don't return anything and it is guaranteed to not be a
  346. // callee saved register (which would be trashed on the epilogue).
  347. Operand tcAddress = Gpr(CodeGenCommon.TcAddressRegister, OperandType.I64);
  348. Operation addrCopyOp = Operation(Instruction.Copy, tcAddress, operation.GetSource(0));
  349. nodes.AddBefore(node, addrCopyOp);
  350. sources[0] = tcAddress;
  351. operation.SetSources(sources.ToArray());
  352. }
  353. private static Operation GenerateCompareAndSwap(IntrusiveList<Operation> nodes, Operation node)
  354. {
  355. Operand expected = node.GetSource(1);
  356. if (expected.Type == OperandType.V128)
  357. {
  358. Operand dest = node.Destination;
  359. Operand expectedLow = Local(OperandType.I64);
  360. Operand expectedHigh = Local(OperandType.I64);
  361. Operand desiredLow = Local(OperandType.I64);
  362. Operand desiredHigh = Local(OperandType.I64);
  363. Operand actualLow = Local(OperandType.I64);
  364. Operand actualHigh = Local(OperandType.I64);
  365. Operand address = node.GetSource(0);
  366. Operand desired = node.GetSource(2);
  367. void SplitOperand(Operand source, Operand low, Operand high)
  368. {
  369. nodes.AddBefore(node, Operation(Instruction.VectorExtract, low, source, Const(0)));
  370. nodes.AddBefore(node, Operation(Instruction.VectorExtract, high, source, Const(1)));
  371. }
  372. SplitOperand(expected, expectedLow, expectedHigh);
  373. SplitOperand(desired, desiredLow, desiredHigh);
  374. Operation operation = node;
  375. // Update the sources and destinations with split 64-bit halfs of the whole 128-bit values.
  376. // We also need a additional registers that will be used to store temporary information.
  377. operation.SetDestinations(new[] { actualLow, actualHigh, Local(OperandType.I64), Local(OperandType.I64) });
  378. operation.SetSources(new[] { address, expectedLow, expectedHigh, desiredLow, desiredHigh });
  379. // Add some dummy uses of the input operands, as the CAS operation will be a loop,
  380. // so they can't be used as destination operand.
  381. for (int i = 0; i < operation.SourcesCount; i++)
  382. {
  383. Operand src = operation.GetSource(i);
  384. node = nodes.AddAfter(node, Operation(Instruction.Copy, src, src));
  385. }
  386. // Assemble the vector with the 64-bit values at the given memory location.
  387. node = nodes.AddAfter(node, Operation(Instruction.VectorCreateScalar, dest, actualLow));
  388. node = nodes.AddAfter(node, Operation(Instruction.VectorInsert, dest, dest, actualHigh, Const(1)));
  389. }
  390. else
  391. {
  392. // We need a additional register where the store result will be written to.
  393. node.SetDestinations(new[] { node.Destination, Local(OperandType.I32) });
  394. // Add some dummy uses of the input operands, as the CAS operation will be a loop,
  395. // so they can't be used as destination operand.
  396. Operation operation = node;
  397. for (int i = 0; i < operation.SourcesCount; i++)
  398. {
  399. Operand src = operation.GetSource(i);
  400. node = nodes.AddAfter(node, Operation(Instruction.Copy, src, src));
  401. }
  402. }
  403. return node.ListNext;
  404. }
  405. private static void InsertReturnCopy(IntrusiveList<Operation> nodes, Operation node)
  406. {
  407. if (node.SourcesCount == 0)
  408. {
  409. return;
  410. }
  411. Operand source = node.GetSource(0);
  412. if (source.Type == OperandType.V128)
  413. {
  414. Operand retLReg = Gpr(CallingConvention.GetIntReturnRegister(), OperandType.I64);
  415. Operand retHReg = Gpr(CallingConvention.GetIntReturnRegisterHigh(), OperandType.I64);
  416. nodes.AddBefore(node, Operation(Instruction.VectorExtract, retLReg, source, Const(0)));
  417. nodes.AddBefore(node, Operation(Instruction.VectorExtract, retHReg, source, Const(1)));
  418. }
  419. else
  420. {
  421. Operand retReg = source.Type.IsInteger()
  422. ? Gpr(CallingConvention.GetIntReturnRegister(), source.Type)
  423. : Xmm(CallingConvention.GetVecReturnRegister(), source.Type);
  424. Operation retCopyOp = Operation(Instruction.Copy, retReg, source);
  425. nodes.AddBefore(node, retCopyOp);
  426. }
  427. }
  428. private static Operation InsertLoadArgumentCopy(
  429. CompilerContext cctx,
  430. ref Span<Operation> buffer,
  431. IntrusiveList<Operation> nodes,
  432. Operand[] preservedArgs,
  433. Operation node)
  434. {
  435. Operand source = node.GetSource(0);
  436. Debug.Assert(source.Kind == OperandKind.Constant, "Non-constant LoadArgument source kind.");
  437. int index = source.AsInt32();
  438. int intCount = 0;
  439. int vecCount = 0;
  440. for (int cIndex = 0; cIndex < index; cIndex++)
  441. {
  442. OperandType argType = cctx.FuncArgTypes[cIndex];
  443. if (argType.IsInteger())
  444. {
  445. intCount++;
  446. }
  447. else if (argType == OperandType.V128)
  448. {
  449. intCount += 2;
  450. }
  451. else
  452. {
  453. vecCount++;
  454. }
  455. }
  456. bool passOnReg;
  457. if (source.Type.IsInteger())
  458. {
  459. passOnReg = intCount < CallingConvention.GetArgumentsOnRegsCount();
  460. }
  461. else if (source.Type == OperandType.V128)
  462. {
  463. passOnReg = intCount + 1 < CallingConvention.GetArgumentsOnRegsCount();
  464. }
  465. else
  466. {
  467. passOnReg = vecCount < CallingConvention.GetArgumentsOnRegsCount();
  468. }
  469. if (passOnReg)
  470. {
  471. Operand dest = node.Destination;
  472. if (preservedArgs[index] == default)
  473. {
  474. if (dest.Type == OperandType.V128)
  475. {
  476. // V128 is a struct, we pass each half on a GPR if possible.
  477. Operand pArg = Local(OperandType.V128);
  478. Operand argLReg = Gpr(CallingConvention.GetIntArgumentRegister(intCount), OperandType.I64);
  479. Operand argHReg = Gpr(CallingConvention.GetIntArgumentRegister(intCount + 1), OperandType.I64);
  480. Operation copyL = Operation(Instruction.VectorCreateScalar, pArg, argLReg);
  481. Operation copyH = Operation(Instruction.VectorInsert, pArg, pArg, argHReg, Const(1));
  482. cctx.Cfg.Entry.Operations.AddFirst(copyH);
  483. cctx.Cfg.Entry.Operations.AddFirst(copyL);
  484. preservedArgs[index] = pArg;
  485. }
  486. else
  487. {
  488. Operand pArg = Local(dest.Type);
  489. Operand argReg = dest.Type.IsInteger()
  490. ? Gpr(CallingConvention.GetIntArgumentRegister(intCount), dest.Type)
  491. : Xmm(CallingConvention.GetVecArgumentRegister(vecCount), dest.Type);
  492. Operation copyOp = Operation(Instruction.Copy, pArg, argReg);
  493. cctx.Cfg.Entry.Operations.AddFirst(copyOp);
  494. preservedArgs[index] = pArg;
  495. }
  496. }
  497. Operation nextNode;
  498. if (dest.AssignmentsCount == 1)
  499. {
  500. // Let's propagate the argument if we can to avoid copies.
  501. PreAllocatorCommon.Propagate(ref buffer, dest, preservedArgs[index]);
  502. nextNode = node.ListNext;
  503. }
  504. else
  505. {
  506. Operation argCopyOp = Operation(Instruction.Copy, dest, preservedArgs[index]);
  507. nextNode = nodes.AddBefore(node, argCopyOp);
  508. }
  509. Delete(nodes, node);
  510. return nextNode;
  511. }
  512. else
  513. {
  514. // TODO: Pass on stack.
  515. return node;
  516. }
  517. }
  518. private static Operand AddFloatConstantCopy(
  519. ConstantDict constants,
  520. IntrusiveList<Operation> nodes,
  521. Operation node,
  522. Operand source)
  523. {
  524. Operand temp = Local(source.Type);
  525. Operand intConst = AddIntConstantCopy(constants, nodes, node, GetIntConst(source));
  526. Operation copyOp = Operation(Instruction.VectorCreateScalar, temp, intConst);
  527. nodes.AddBefore(node, copyOp);
  528. return temp;
  529. }
  530. private static Operand AddIntConstantCopy(
  531. ConstantDict constants,
  532. IntrusiveList<Operation> nodes,
  533. Operation node,
  534. Operand source)
  535. {
  536. if (constants.TryGetValue(source.Value, source.Type, out Operand temp))
  537. {
  538. return temp;
  539. }
  540. temp = Local(source.Type);
  541. Operation copyOp = Operation(Instruction.Copy, temp, source);
  542. nodes.AddBefore(node, copyOp);
  543. constants.Add(source.Value, source.Type, temp);
  544. return temp;
  545. }
  546. private static Operand GetIntConst(Operand value)
  547. {
  548. if (value.Type == OperandType.FP32)
  549. {
  550. return Const(value.AsInt32());
  551. }
  552. else if (value.Type == OperandType.FP64)
  553. {
  554. return Const(value.AsInt64());
  555. }
  556. return value;
  557. }
  558. private static void Delete(IntrusiveList<Operation> nodes, Operation node)
  559. {
  560. node.Destination = default;
  561. for (int index = 0; index < node.SourcesCount; index++)
  562. {
  563. node.SetSource(index, default);
  564. }
  565. nodes.Remove(node);
  566. }
  567. private static Operand Gpr(int register, OperandType type)
  568. {
  569. return Register(register, RegisterType.Integer, type);
  570. }
  571. private static Operand Xmm(int register, OperandType type)
  572. {
  573. return Register(register, RegisterType.Vector, type);
  574. }
  575. private static bool IsSameOperandDestSrc1(Operation operation)
  576. {
  577. switch (operation.Instruction)
  578. {
  579. case Instruction.Extended:
  580. return IsSameOperandDestSrc1(operation.Intrinsic);
  581. case Instruction.VectorInsert:
  582. case Instruction.VectorInsert16:
  583. case Instruction.VectorInsert8:
  584. return true;
  585. }
  586. return false;
  587. }
  588. private static bool IsSameOperandDestSrc1(Intrinsic intrinsic)
  589. {
  590. IntrinsicInfo info = IntrinsicTable.GetInfo(intrinsic & ~(Intrinsic.Arm64VTypeMask | Intrinsic.Arm64VSizeMask));
  591. return info.Type == IntrinsicType.ScalarBinaryRd ||
  592. info.Type == IntrinsicType.ScalarTernaryFPRdByElem ||
  593. info.Type == IntrinsicType.ScalarTernaryShlRd ||
  594. info.Type == IntrinsicType.ScalarTernaryShrRd ||
  595. info.Type == IntrinsicType.VectorBinaryRd ||
  596. info.Type == IntrinsicType.VectorInsertByElem ||
  597. info.Type == IntrinsicType.VectorTernaryRd ||
  598. info.Type == IntrinsicType.VectorTernaryRdBitwise ||
  599. info.Type == IntrinsicType.VectorTernaryFPRdByElem ||
  600. info.Type == IntrinsicType.VectorTernaryRdByElem ||
  601. info.Type == IntrinsicType.VectorTernaryShlRd ||
  602. info.Type == IntrinsicType.VectorTernaryShrRd;
  603. }
  604. private static bool HasConstSrc1(Operation node, ulong value)
  605. {
  606. switch (node.Instruction)
  607. {
  608. case Instruction.Add:
  609. case Instruction.BranchIf:
  610. case Instruction.Compare:
  611. case Instruction.Subtract:
  612. // The immediate encoding of those instructions does not allow Rn to be
  613. // XZR (it will be SP instead), so we can't allow a Rn constant in this case.
  614. return value == 0 && NotConstOrConst0(node.GetSource(1));
  615. case Instruction.BitwiseAnd:
  616. case Instruction.BitwiseExclusiveOr:
  617. case Instruction.BitwiseNot:
  618. case Instruction.BitwiseOr:
  619. case Instruction.ByteSwap:
  620. case Instruction.CountLeadingZeros:
  621. case Instruction.Multiply:
  622. case Instruction.Negate:
  623. case Instruction.RotateRight:
  624. case Instruction.ShiftLeft:
  625. case Instruction.ShiftRightSI:
  626. case Instruction.ShiftRightUI:
  627. return value == 0;
  628. case Instruction.Copy:
  629. case Instruction.LoadArgument:
  630. case Instruction.Spill:
  631. case Instruction.SpillArg:
  632. return true;
  633. case Instruction.Extended:
  634. return value == 0;
  635. }
  636. return false;
  637. }
  638. private static bool NotConstOrConst0(Operand operand)
  639. {
  640. return operand.Kind != OperandKind.Constant || operand.Value == 0;
  641. }
  642. private static bool HasConstSrc2(Instruction inst, Operand operand)
  643. {
  644. ulong value = operand.Value;
  645. switch (inst)
  646. {
  647. case Instruction.Add:
  648. case Instruction.BranchIf:
  649. case Instruction.Compare:
  650. case Instruction.Subtract:
  651. return ConstFitsOnUImm12Sh(value);
  652. case Instruction.BitwiseAnd:
  653. case Instruction.BitwiseExclusiveOr:
  654. case Instruction.BitwiseOr:
  655. return value == 0 || CodeGenCommon.TryEncodeBitMask(operand, out _, out _, out _);
  656. case Instruction.Multiply:
  657. case Instruction.Store:
  658. case Instruction.Store16:
  659. case Instruction.Store8:
  660. return value == 0;
  661. case Instruction.RotateRight:
  662. case Instruction.ShiftLeft:
  663. case Instruction.ShiftRightSI:
  664. case Instruction.ShiftRightUI:
  665. case Instruction.VectorExtract:
  666. case Instruction.VectorExtract16:
  667. case Instruction.VectorExtract8:
  668. return true;
  669. case Instruction.Extended:
  670. // TODO: Check if actual intrinsic is supposed to have consts here?
  671. // Right now we only hit this case for fixed-point int <-> FP conversion instructions.
  672. return true;
  673. }
  674. return false;
  675. }
  676. private static bool IsCommutative(Operation operation)
  677. {
  678. switch (operation.Instruction)
  679. {
  680. case Instruction.Add:
  681. case Instruction.BitwiseAnd:
  682. case Instruction.BitwiseExclusiveOr:
  683. case Instruction.BitwiseOr:
  684. case Instruction.Multiply:
  685. return true;
  686. case Instruction.BranchIf:
  687. case Instruction.Compare:
  688. {
  689. Operand comp = operation.GetSource(2);
  690. Debug.Assert(comp.Kind == OperandKind.Constant);
  691. var compType = (Comparison)comp.AsInt32();
  692. return compType == Comparison.Equal || compType == Comparison.NotEqual;
  693. }
  694. }
  695. return false;
  696. }
  697. private static bool ConstFitsOnUImm12Sh(ulong value)
  698. {
  699. return (value & ~0xfffUL) == 0 || (value & ~0xfff000UL) == 0;
  700. }
  701. private static bool IsIntrinsicWithConst(Operation operation)
  702. {
  703. bool isIntrinsic = IsIntrinsic(operation.Instruction);
  704. if (isIntrinsic)
  705. {
  706. Intrinsic intrinsic = operation.Intrinsic;
  707. IntrinsicInfo info = IntrinsicTable.GetInfo(intrinsic & ~(Intrinsic.Arm64VTypeMask | Intrinsic.Arm64VSizeMask));
  708. // Those have integer inputs that don't support consts.
  709. return info.Type != IntrinsicType.ScalarFPConvGpr &&
  710. info.Type != IntrinsicType.ScalarFPConvFixedGpr &&
  711. info.Type != IntrinsicType.SetRegister;
  712. }
  713. return false;
  714. }
  715. private static bool IsIntrinsic(Instruction inst)
  716. {
  717. return inst == Instruction.Extended;
  718. }
  719. }
  720. }