PreAllocator.cs 31 KB

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