PreAllocator.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. using ARMeilleure.CodeGen.RegisterAllocators;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.Translation;
  4. using System;
  5. using System.Diagnostics;
  6. using static ARMeilleure.IntermediateRepresentation.Operand.Factory;
  7. using static ARMeilleure.IntermediateRepresentation.Operation.Factory;
  8. namespace ARMeilleure.CodeGen.X86
  9. {
  10. class PreAllocator
  11. {
  12. public static void RunPass(CompilerContext cctx, StackAllocator stackAlloc, out int maxCallArgs)
  13. {
  14. maxCallArgs = -1;
  15. Span<Operation> buffer = default;
  16. CallConvName callConv = CallingConvention.GetCurrentCallConv();
  17. Operand[] preservedArgs = new Operand[CallingConvention.GetArgumentsOnRegsCount()];
  18. for (BasicBlock block = cctx.Cfg.Blocks.First; block != null; block = block.ListNext)
  19. {
  20. Operation nextNode;
  21. for (Operation node = block.Operations.First; node != default; node = nextNode)
  22. {
  23. nextNode = node.ListNext;
  24. if (node.Instruction == Instruction.Phi)
  25. {
  26. continue;
  27. }
  28. InsertConstantRegCopies(block.Operations, node);
  29. InsertDestructiveRegCopies(block.Operations, node);
  30. InsertConstrainedRegCopies(block.Operations, node);
  31. switch (node.Instruction)
  32. {
  33. case Instruction.Call:
  34. // Get the maximum number of arguments used on a call.
  35. // On windows, when a struct is returned from the call,
  36. // we also need to pass the pointer where the struct
  37. // should be written on the first argument.
  38. int argsCount = node.SourcesCount - 1;
  39. if (node.Destination != default && node.Destination.Type == OperandType.V128)
  40. {
  41. argsCount++;
  42. }
  43. if (maxCallArgs < argsCount)
  44. {
  45. maxCallArgs = argsCount;
  46. }
  47. // Copy values to registers expected by the function
  48. // being called, as mandated by the ABI.
  49. if (callConv == CallConvName.Windows)
  50. {
  51. PreAllocatorWindows.InsertCallCopies(block.Operations, stackAlloc, node);
  52. }
  53. else /* if (callConv == CallConvName.SystemV) */
  54. {
  55. PreAllocatorSystemV.InsertCallCopies(block.Operations, node);
  56. }
  57. break;
  58. case Instruction.ConvertToFPUI:
  59. GenerateConvertToFPUI(block.Operations, node);
  60. break;
  61. case Instruction.LoadArgument:
  62. if (callConv == CallConvName.Windows)
  63. {
  64. nextNode = PreAllocatorWindows.InsertLoadArgumentCopy(cctx, ref buffer, block.Operations, preservedArgs, node);
  65. }
  66. else /* if (callConv == CallConvName.SystemV) */
  67. {
  68. nextNode = PreAllocatorSystemV.InsertLoadArgumentCopy(cctx, ref buffer, block.Operations, preservedArgs, node);
  69. }
  70. break;
  71. case Instruction.Negate:
  72. if (!node.GetSource(0).Type.IsInteger())
  73. {
  74. GenerateNegate(block.Operations, node);
  75. }
  76. break;
  77. case Instruction.Return:
  78. if (callConv == CallConvName.Windows)
  79. {
  80. PreAllocatorWindows.InsertReturnCopy(cctx, block.Operations, preservedArgs, node);
  81. }
  82. else /* if (callConv == CallConvName.SystemV) */
  83. {
  84. PreAllocatorSystemV.InsertReturnCopy(block.Operations, node);
  85. }
  86. break;
  87. case Instruction.Tailcall:
  88. if (callConv == CallConvName.Windows)
  89. {
  90. PreAllocatorWindows.InsertTailcallCopies(block.Operations, stackAlloc, node);
  91. }
  92. else
  93. {
  94. PreAllocatorSystemV.InsertTailcallCopies(block.Operations, stackAlloc, node);
  95. }
  96. break;
  97. case Instruction.VectorInsert8:
  98. if (!HardwareCapabilities.SupportsSse41)
  99. {
  100. GenerateVectorInsert8(block.Operations, node);
  101. }
  102. break;
  103. case Instruction.Extended:
  104. if (node.Intrinsic == Intrinsic.X86Ldmxcsr)
  105. {
  106. int stackOffset = stackAlloc.Allocate(OperandType.I32);
  107. node.SetSources(new Operand[] { Const(stackOffset), node.GetSource(0) });
  108. }
  109. else if (node.Intrinsic == Intrinsic.X86Stmxcsr)
  110. {
  111. int stackOffset = stackAlloc.Allocate(OperandType.I32);
  112. node.SetSources(new Operand[] { Const(stackOffset) });
  113. }
  114. break;
  115. }
  116. }
  117. }
  118. }
  119. protected static void InsertConstantRegCopies(IntrusiveList<Operation> nodes, Operation node)
  120. {
  121. if (node.SourcesCount == 0 || IsXmmIntrinsic(node))
  122. {
  123. return;
  124. }
  125. Instruction inst = node.Instruction;
  126. Operand src1 = node.GetSource(0);
  127. Operand src2;
  128. if (src1.Kind == OperandKind.Constant)
  129. {
  130. if (!src1.Type.IsInteger())
  131. {
  132. // Handle non-integer types (FP32, FP64 and V128).
  133. // For instructions without an immediate operand, we do the following:
  134. // - Insert a copy with the constant value (as integer) to a GPR.
  135. // - Insert a copy from the GPR to a XMM register.
  136. // - Replace the constant use with the XMM register.
  137. src1 = AddXmmCopy(nodes, node, src1);
  138. node.SetSource(0, src1);
  139. }
  140. else if (!HasConstSrc1(inst))
  141. {
  142. // Handle integer types.
  143. // Most ALU instructions accepts a 32-bits immediate on the second operand.
  144. // We need to ensure the following:
  145. // - If the constant is on operand 1, we need to move it.
  146. // -- But first, we try to swap operand 1 and 2 if the instruction is commutative.
  147. // -- Doing so may allow us to encode the constant as operand 2 and avoid a copy.
  148. // - If the constant is on operand 2, we check if the instruction supports it,
  149. // if not, we also add a copy. 64-bits constants are usually not supported.
  150. if (IsCommutative(node))
  151. {
  152. src2 = node.GetSource(1);
  153. Operand temp = src1;
  154. src1 = src2;
  155. src2 = temp;
  156. node.SetSource(0, src1);
  157. node.SetSource(1, src2);
  158. }
  159. if (src1.Kind == OperandKind.Constant)
  160. {
  161. src1 = AddCopy(nodes, node, src1);
  162. node.SetSource(0, src1);
  163. }
  164. }
  165. }
  166. if (node.SourcesCount < 2)
  167. {
  168. return;
  169. }
  170. src2 = node.GetSource(1);
  171. if (src2.Kind == OperandKind.Constant)
  172. {
  173. if (!src2.Type.IsInteger())
  174. {
  175. src2 = AddXmmCopy(nodes, node, src2);
  176. node.SetSource(1, src2);
  177. }
  178. else if (!HasConstSrc2(inst) || CodeGenCommon.IsLongConst(src2))
  179. {
  180. src2 = AddCopy(nodes, node, src2);
  181. node.SetSource(1, src2);
  182. }
  183. }
  184. }
  185. protected static void InsertConstrainedRegCopies(IntrusiveList<Operation> nodes, Operation node)
  186. {
  187. Operand dest = node.Destination;
  188. switch (node.Instruction)
  189. {
  190. case Instruction.CompareAndSwap:
  191. case Instruction.CompareAndSwap16:
  192. case Instruction.CompareAndSwap8:
  193. {
  194. OperandType type = node.GetSource(1).Type;
  195. if (type == OperandType.V128)
  196. {
  197. // Handle the many restrictions of the compare and exchange (16 bytes) instruction:
  198. // - The expected value should be in RDX:RAX.
  199. // - The new value to be written should be in RCX:RBX.
  200. // - The value at the memory location is loaded to RDX:RAX.
  201. void SplitOperand(Operand source, Operand lr, Operand hr)
  202. {
  203. nodes.AddBefore(node, Operation(Instruction.VectorExtract, lr, source, Const(0)));
  204. nodes.AddBefore(node, Operation(Instruction.VectorExtract, hr, source, Const(1)));
  205. }
  206. Operand rax = Gpr(X86Register.Rax, OperandType.I64);
  207. Operand rbx = Gpr(X86Register.Rbx, OperandType.I64);
  208. Operand rcx = Gpr(X86Register.Rcx, OperandType.I64);
  209. Operand rdx = Gpr(X86Register.Rdx, OperandType.I64);
  210. SplitOperand(node.GetSource(1), rax, rdx);
  211. SplitOperand(node.GetSource(2), rbx, rcx);
  212. Operation operation = node;
  213. node = nodes.AddAfter(node, Operation(Instruction.VectorCreateScalar, dest, rax));
  214. nodes.AddAfter(node, Operation(Instruction.VectorInsert, dest, dest, rdx, Const(1)));
  215. operation.SetDestinations(new Operand[] { rdx, rax });
  216. operation.SetSources(new Operand[] { operation.GetSource(0), rdx, rax, rcx, rbx });
  217. }
  218. else
  219. {
  220. // Handle the many restrictions of the compare and exchange (32/64) instruction:
  221. // - The expected value should be in (E/R)AX.
  222. // - The value at the memory location is loaded to (E/R)AX.
  223. Operand expected = node.GetSource(1);
  224. Operand newValue = node.GetSource(2);
  225. Operand rax = Gpr(X86Register.Rax, expected.Type);
  226. nodes.AddBefore(node, Operation(Instruction.Copy, rax, expected));
  227. // We need to store the new value into a temp, since it may
  228. // be a constant, and this instruction does not support immediate operands.
  229. Operand temp = Local(newValue.Type);
  230. nodes.AddBefore(node, Operation(Instruction.Copy, temp, newValue));
  231. node.SetSources(new Operand[] { node.GetSource(0), rax, temp });
  232. nodes.AddAfter(node, Operation(Instruction.Copy, dest, rax));
  233. node.Destination = rax;
  234. }
  235. break;
  236. }
  237. case Instruction.Divide:
  238. case Instruction.DivideUI:
  239. {
  240. // Handle the many restrictions of the division instructions:
  241. // - The dividend is always in RDX:RAX.
  242. // - The result is always in RAX.
  243. // - Additionally it also writes the remainder in RDX.
  244. if (dest.Type.IsInteger())
  245. {
  246. Operand src1 = node.GetSource(0);
  247. Operand rax = Gpr(X86Register.Rax, src1.Type);
  248. Operand rdx = Gpr(X86Register.Rdx, src1.Type);
  249. nodes.AddBefore(node, Operation(Instruction.Copy, rax, src1));
  250. nodes.AddBefore(node, Operation(Instruction.Clobber, rdx));
  251. nodes.AddAfter(node, Operation(Instruction.Copy, dest, rax));
  252. node.SetSources(new Operand[] { rdx, rax, node.GetSource(1) });
  253. node.Destination = rax;
  254. }
  255. break;
  256. }
  257. case Instruction.Extended:
  258. {
  259. bool isBlend = node.Intrinsic == Intrinsic.X86Blendvpd ||
  260. node.Intrinsic == Intrinsic.X86Blendvps ||
  261. node.Intrinsic == Intrinsic.X86Pblendvb;
  262. // BLENDVPD, BLENDVPS, PBLENDVB last operand is always implied to be XMM0 when VEX is not supported.
  263. // SHA256RNDS2 always has an implied XMM0 as a last operand.
  264. if ((isBlend && !HardwareCapabilities.SupportsVexEncoding) || node.Intrinsic == Intrinsic.X86Sha256Rnds2)
  265. {
  266. Operand xmm0 = Xmm(X86Register.Xmm0, OperandType.V128);
  267. nodes.AddBefore(node, Operation(Instruction.Copy, xmm0, node.GetSource(2)));
  268. node.SetSource(2, xmm0);
  269. }
  270. break;
  271. }
  272. case Instruction.Multiply64HighSI:
  273. case Instruction.Multiply64HighUI:
  274. {
  275. // Handle the many restrictions of the i64 * i64 = i128 multiply instructions:
  276. // - The multiplicand is always in RAX.
  277. // - The lower 64-bits of the result is always in RAX.
  278. // - The higher 64-bits of the result is always in RDX.
  279. Operand src1 = node.GetSource(0);
  280. Operand rax = Gpr(X86Register.Rax, src1.Type);
  281. Operand rdx = Gpr(X86Register.Rdx, src1.Type);
  282. nodes.AddBefore(node, Operation(Instruction.Copy, rax, src1));
  283. node.SetSource(0, rax);
  284. nodes.AddAfter(node, Operation(Instruction.Copy, dest, rdx));
  285. node.SetDestinations(new Operand[] { rdx, rax });
  286. break;
  287. }
  288. case Instruction.RotateRight:
  289. case Instruction.ShiftLeft:
  290. case Instruction.ShiftRightSI:
  291. case Instruction.ShiftRightUI:
  292. {
  293. // The shift register is always implied to be CL (low 8-bits of RCX or ECX).
  294. if (node.GetSource(1).Kind == OperandKind.LocalVariable)
  295. {
  296. Operand rcx = Gpr(X86Register.Rcx, OperandType.I32);
  297. nodes.AddBefore(node, Operation(Instruction.Copy, rcx, node.GetSource(1)));
  298. node.SetSource(1, rcx);
  299. }
  300. break;
  301. }
  302. }
  303. }
  304. protected static void InsertDestructiveRegCopies(IntrusiveList<Operation> nodes, Operation node)
  305. {
  306. if (node.Destination == default || node.SourcesCount == 0)
  307. {
  308. return;
  309. }
  310. Instruction inst = node.Instruction;
  311. Operand dest = node.Destination;
  312. Operand src1 = node.GetSource(0);
  313. // The multiply instruction (that maps to IMUL) is somewhat special, it has
  314. // a three operand form where the second source is a immediate value.
  315. bool threeOperandForm = inst == Instruction.Multiply && node.GetSource(1).Kind == OperandKind.Constant;
  316. if (IsSameOperandDestSrc1(node) && src1.Kind == OperandKind.LocalVariable && !threeOperandForm)
  317. {
  318. bool useNewLocal = false;
  319. for (int srcIndex = 1; srcIndex < node.SourcesCount; srcIndex++)
  320. {
  321. if (node.GetSource(srcIndex) == dest)
  322. {
  323. useNewLocal = true;
  324. break;
  325. }
  326. }
  327. if (useNewLocal)
  328. {
  329. // Dest is being used as some source already, we need to use a new
  330. // local to store the temporary value, otherwise the value on dest
  331. // local would be overwritten.
  332. Operand temp = Local(dest.Type);
  333. nodes.AddBefore(node, Operation(Instruction.Copy, temp, src1));
  334. node.SetSource(0, temp);
  335. nodes.AddAfter(node, Operation(Instruction.Copy, dest, temp));
  336. node.Destination = temp;
  337. }
  338. else
  339. {
  340. nodes.AddBefore(node, Operation(Instruction.Copy, dest, src1));
  341. node.SetSource(0, dest);
  342. }
  343. }
  344. else if (inst == Instruction.ConditionalSelect)
  345. {
  346. Operand src2 = node.GetSource(1);
  347. Operand src3 = node.GetSource(2);
  348. if (src1 == dest || src2 == dest)
  349. {
  350. Operand temp = Local(dest.Type);
  351. nodes.AddBefore(node, Operation(Instruction.Copy, temp, src3));
  352. node.SetSource(2, temp);
  353. nodes.AddAfter(node, Operation(Instruction.Copy, dest, temp));
  354. node.Destination = temp;
  355. }
  356. else
  357. {
  358. nodes.AddBefore(node, Operation(Instruction.Copy, dest, src3));
  359. node.SetSource(2, dest);
  360. }
  361. }
  362. }
  363. private static void GenerateConvertToFPUI(IntrusiveList<Operation> nodes, Operation node)
  364. {
  365. // Unsigned integer to FP conversions are not supported on X86.
  366. // We need to turn them into signed integer to FP conversions, and
  367. // adjust the final result.
  368. Operand dest = node.Destination;
  369. Operand source = node.GetSource(0);
  370. Debug.Assert(source.Type.IsInteger(), $"Invalid source type \"{source.Type}\".");
  371. Operation currentNode = node;
  372. if (source.Type == OperandType.I32)
  373. {
  374. // For 32-bits integers, we can just zero-extend to 64-bits,
  375. // and then use the 64-bits signed conversion instructions.
  376. Operand zex = Local(OperandType.I64);
  377. node = nodes.AddAfter(node, Operation(Instruction.ZeroExtend32, zex, source));
  378. node = nodes.AddAfter(node, Operation(Instruction.ConvertToFP, dest, zex));
  379. }
  380. else /* if (source.Type == OperandType.I64) */
  381. {
  382. // For 64-bits integers, we need to do the following:
  383. // - Ensure that the integer has the most significant bit clear.
  384. // -- This can be done by shifting the value right by 1, that is, dividing by 2.
  385. // -- The least significant bit is lost in this case though.
  386. // - We can then convert the shifted value with a signed integer instruction.
  387. // - The result still needs to be corrected after that.
  388. // -- First, we need to multiply the result by 2, as we divided it by 2 before.
  389. // --- This can be done efficiently by adding the result to itself.
  390. // -- Then, we need to add the least significant bit that was shifted out.
  391. // --- We can convert the least significant bit to float, and add it to the result.
  392. Operand lsb = Local(OperandType.I64);
  393. Operand half = Local(OperandType.I64);
  394. Operand lsbF = Local(dest.Type);
  395. node = nodes.AddAfter(node, Operation(Instruction.Copy, lsb, source));
  396. node = nodes.AddAfter(node, Operation(Instruction.Copy, half, source));
  397. node = nodes.AddAfter(node, Operation(Instruction.BitwiseAnd, lsb, lsb, Const(1L)));
  398. node = nodes.AddAfter(node, Operation(Instruction.ShiftRightUI, half, half, Const(1)));
  399. node = nodes.AddAfter(node, Operation(Instruction.ConvertToFP, lsbF, lsb));
  400. node = nodes.AddAfter(node, Operation(Instruction.ConvertToFP, dest, half));
  401. node = nodes.AddAfter(node, Operation(Instruction.Add, dest, dest, dest));
  402. nodes.AddAfter(node, Operation(Instruction.Add, dest, dest, lsbF));
  403. }
  404. Delete(nodes, currentNode);
  405. }
  406. private static void GenerateNegate(IntrusiveList<Operation> nodes, Operation node)
  407. {
  408. // There's no SSE FP negate instruction, so we need to transform that into
  409. // a XOR of the value to be negated with a mask with the highest bit set.
  410. // This also produces -0 for a negation of the value 0.
  411. Operand dest = node.Destination;
  412. Operand source = node.GetSource(0);
  413. Debug.Assert(dest.Type == OperandType.FP32 ||
  414. dest.Type == OperandType.FP64, $"Invalid destination type \"{dest.Type}\".");
  415. Operation currentNode = node;
  416. Operand res = Local(dest.Type);
  417. node = nodes.AddAfter(node, Operation(Instruction.VectorOne, res));
  418. if (dest.Type == OperandType.FP32)
  419. {
  420. node = nodes.AddAfter(node, Operation(Intrinsic.X86Pslld, res, res, Const(31)));
  421. }
  422. else /* if (dest.Type == OperandType.FP64) */
  423. {
  424. node = nodes.AddAfter(node, Operation(Intrinsic.X86Psllq, res, res, Const(63)));
  425. }
  426. node = nodes.AddAfter(node, Operation(Intrinsic.X86Xorps, res, res, source));
  427. nodes.AddAfter(node, Operation(Instruction.Copy, dest, res));
  428. Delete(nodes, currentNode);
  429. }
  430. private static void GenerateVectorInsert8(IntrusiveList<Operation> nodes, Operation node)
  431. {
  432. // Handle vector insertion, when SSE 4.1 is not supported.
  433. Operand dest = node.Destination;
  434. Operand src1 = node.GetSource(0); // Vector
  435. Operand src2 = node.GetSource(1); // Value
  436. Operand src3 = node.GetSource(2); // Index
  437. Debug.Assert(src3.Kind == OperandKind.Constant);
  438. byte index = src3.AsByte();
  439. Debug.Assert(index < 16);
  440. Operation currentNode = node;
  441. Operand temp1 = Local(OperandType.I32);
  442. Operand temp2 = Local(OperandType.I32);
  443. node = nodes.AddAfter(node, Operation(Instruction.Copy, temp2, src2));
  444. Operation vextOp = Operation(Instruction.VectorExtract16, temp1, src1, Const(index >> 1));
  445. node = nodes.AddAfter(node, vextOp);
  446. if ((index & 1) != 0)
  447. {
  448. node = nodes.AddAfter(node, Operation(Instruction.ZeroExtend8, temp1, temp1));
  449. node = nodes.AddAfter(node, Operation(Instruction.ShiftLeft, temp2, temp2, Const(8)));
  450. node = nodes.AddAfter(node, Operation(Instruction.BitwiseOr, temp1, temp1, temp2));
  451. }
  452. else
  453. {
  454. node = nodes.AddAfter(node, Operation(Instruction.ZeroExtend8, temp2, temp2));
  455. node = nodes.AddAfter(node, Operation(Instruction.BitwiseAnd, temp1, temp1, Const(0xff00)));
  456. node = nodes.AddAfter(node, Operation(Instruction.BitwiseOr, temp1, temp1, temp2));
  457. }
  458. Operation vinsOp = Operation(Instruction.VectorInsert16, dest, src1, temp1, Const(index >> 1));
  459. nodes.AddAfter(node, vinsOp);
  460. Delete(nodes, currentNode);
  461. }
  462. protected static Operand AddXmmCopy(IntrusiveList<Operation> nodes, Operation node, Operand source)
  463. {
  464. Operand temp = Local(source.Type);
  465. Operand intConst = AddCopy(nodes, node, GetIntConst(source));
  466. Operation copyOp = Operation(Instruction.VectorCreateScalar, temp, intConst);
  467. nodes.AddBefore(node, copyOp);
  468. return temp;
  469. }
  470. protected static Operand AddCopy(IntrusiveList<Operation> nodes, Operation node, Operand source)
  471. {
  472. Operand temp = Local(source.Type);
  473. Operation copyOp = Operation(Instruction.Copy, temp, source);
  474. nodes.AddBefore(node, copyOp);
  475. return temp;
  476. }
  477. private static Operand GetIntConst(Operand value)
  478. {
  479. if (value.Type == OperandType.FP32)
  480. {
  481. return Const(value.AsInt32());
  482. }
  483. else if (value.Type == OperandType.FP64)
  484. {
  485. return Const(value.AsInt64());
  486. }
  487. return value;
  488. }
  489. protected static void Delete(IntrusiveList<Operation> nodes, Operation node)
  490. {
  491. node.Destination = default;
  492. for (int index = 0; index < node.SourcesCount; index++)
  493. {
  494. node.SetSource(index, default);
  495. }
  496. nodes.Remove(node);
  497. }
  498. protected static Operand Gpr(X86Register register, OperandType type)
  499. {
  500. return Register((int)register, RegisterType.Integer, type);
  501. }
  502. protected static Operand Xmm(X86Register register, OperandType type)
  503. {
  504. return Register((int)register, RegisterType.Vector, type);
  505. }
  506. private static bool IsSameOperandDestSrc1(Operation operation)
  507. {
  508. switch (operation.Instruction)
  509. {
  510. case Instruction.Add:
  511. return !HardwareCapabilities.SupportsVexEncoding && !operation.Destination.Type.IsInteger();
  512. case Instruction.Multiply:
  513. case Instruction.Subtract:
  514. return !HardwareCapabilities.SupportsVexEncoding || operation.Destination.Type.IsInteger();
  515. case Instruction.BitwiseAnd:
  516. case Instruction.BitwiseExclusiveOr:
  517. case Instruction.BitwiseNot:
  518. case Instruction.BitwiseOr:
  519. case Instruction.ByteSwap:
  520. case Instruction.Negate:
  521. case Instruction.RotateRight:
  522. case Instruction.ShiftLeft:
  523. case Instruction.ShiftRightSI:
  524. case Instruction.ShiftRightUI:
  525. return true;
  526. case Instruction.Divide:
  527. return !HardwareCapabilities.SupportsVexEncoding && !operation.Destination.Type.IsInteger();
  528. case Instruction.VectorInsert:
  529. case Instruction.VectorInsert16:
  530. case Instruction.VectorInsert8:
  531. return !HardwareCapabilities.SupportsVexEncoding;
  532. case Instruction.Extended:
  533. return IsIntrinsicSameOperandDestSrc1(operation);
  534. }
  535. return IsVexSameOperandDestSrc1(operation);
  536. }
  537. private static bool IsIntrinsicSameOperandDestSrc1(Operation operation)
  538. {
  539. IntrinsicInfo info = IntrinsicTable.GetInfo(operation.Intrinsic);
  540. return info.Type == IntrinsicType.Crc32 || info.Type == IntrinsicType.Fma || IsVexSameOperandDestSrc1(operation);
  541. }
  542. private static bool IsVexSameOperandDestSrc1(Operation operation)
  543. {
  544. if (IsIntrinsic(operation.Instruction))
  545. {
  546. IntrinsicInfo info = IntrinsicTable.GetInfo(operation.Intrinsic);
  547. bool hasVex = HardwareCapabilities.SupportsVexEncoding && Assembler.SupportsVexPrefix(info.Inst);
  548. bool isUnary = operation.SourcesCount < 2;
  549. bool hasVecDest = operation.Destination != default && operation.Destination.Type == OperandType.V128;
  550. return !hasVex && !isUnary && hasVecDest;
  551. }
  552. return false;
  553. }
  554. private static bool HasConstSrc1(Instruction inst)
  555. {
  556. switch (inst)
  557. {
  558. case Instruction.Copy:
  559. case Instruction.LoadArgument:
  560. case Instruction.Spill:
  561. case Instruction.SpillArg:
  562. return true;
  563. }
  564. return false;
  565. }
  566. private static bool HasConstSrc2(Instruction inst)
  567. {
  568. switch (inst)
  569. {
  570. case Instruction.Add:
  571. case Instruction.BitwiseAnd:
  572. case Instruction.BitwiseExclusiveOr:
  573. case Instruction.BitwiseOr:
  574. case Instruction.BranchIf:
  575. case Instruction.Compare:
  576. case Instruction.Multiply:
  577. case Instruction.RotateRight:
  578. case Instruction.ShiftLeft:
  579. case Instruction.ShiftRightSI:
  580. case Instruction.ShiftRightUI:
  581. case Instruction.Store:
  582. case Instruction.Store16:
  583. case Instruction.Store8:
  584. case Instruction.Subtract:
  585. case Instruction.VectorExtract:
  586. case Instruction.VectorExtract16:
  587. case Instruction.VectorExtract8:
  588. return true;
  589. }
  590. return false;
  591. }
  592. private static bool IsCommutative(Operation operation)
  593. {
  594. switch (operation.Instruction)
  595. {
  596. case Instruction.Add:
  597. case Instruction.BitwiseAnd:
  598. case Instruction.BitwiseExclusiveOr:
  599. case Instruction.BitwiseOr:
  600. case Instruction.Multiply:
  601. return true;
  602. case Instruction.BranchIf:
  603. case Instruction.Compare:
  604. {
  605. Operand comp = operation.GetSource(2);
  606. Debug.Assert(comp.Kind == OperandKind.Constant);
  607. var compType = (Comparison)comp.AsInt32();
  608. return compType == Comparison.Equal || compType == Comparison.NotEqual;
  609. }
  610. }
  611. return false;
  612. }
  613. private static bool IsIntrinsic(Instruction inst)
  614. {
  615. return inst == Instruction.Extended;
  616. }
  617. private static bool IsXmmIntrinsic(Operation operation)
  618. {
  619. if (operation.Instruction != Instruction.Extended)
  620. {
  621. return false;
  622. }
  623. IntrinsicInfo info = IntrinsicTable.GetInfo(operation.Intrinsic);
  624. return info.Type != IntrinsicType.Crc32;
  625. }
  626. }
  627. }