PreAllocator.cs 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  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.X86
  10. {
  11. static class PreAllocator
  12. {
  13. public static void RunPass(CompilerContext cctx, StackAllocator stackAlloc, out int maxCallArgs)
  14. {
  15. maxCallArgs = -1;
  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. HandleConstantRegCopy(block.Operations, node);
  29. HandleDestructiveRegCopy(block.Operations, node);
  30. HandleConstrainedRegCopy(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. HandleCallWindowsAbi(block.Operations, stackAlloc, node);
  52. }
  53. else /* if (callConv == CallConvName.SystemV) */
  54. {
  55. HandleCallSystemVAbi(block.Operations, node);
  56. }
  57. break;
  58. case Instruction.ConvertToFPUI:
  59. HandleConvertToFPUI(block.Operations, node);
  60. break;
  61. case Instruction.LoadArgument:
  62. if (callConv == CallConvName.Windows)
  63. {
  64. nextNode = HandleLoadArgumentWindowsAbi(cctx, block.Operations, preservedArgs, node);
  65. }
  66. else /* if (callConv == CallConvName.SystemV) */
  67. {
  68. nextNode = HandleLoadArgumentSystemVAbi(cctx, block.Operations, preservedArgs, node);
  69. }
  70. break;
  71. case Instruction.Negate:
  72. if (!node.GetSource(0).Type.IsInteger())
  73. {
  74. HandleNegate(block.Operations, node);
  75. }
  76. break;
  77. case Instruction.Return:
  78. if (callConv == CallConvName.Windows)
  79. {
  80. HandleReturnWindowsAbi(cctx, block.Operations, preservedArgs, node);
  81. }
  82. else /* if (callConv == CallConvName.SystemV) */
  83. {
  84. HandleReturnSystemVAbi(block.Operations, node);
  85. }
  86. break;
  87. case Instruction.Tailcall:
  88. if (callConv == CallConvName.Windows)
  89. {
  90. HandleTailcallWindowsAbi(block.Operations, stackAlloc, node);
  91. }
  92. else
  93. {
  94. HandleTailcallSystemVAbi(block.Operations, stackAlloc, node);
  95. }
  96. break;
  97. case Instruction.VectorInsert8:
  98. if (!HardwareCapabilities.SupportsSse41)
  99. {
  100. HandleVectorInsert8(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. private static void HandleConstantRegCopy(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. private static void HandleConstrainedRegCopy(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. private static void HandleDestructiveRegCopy(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 HandleConvertToFPUI(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 HandleNegate(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 HandleVectorInsert8(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. private static void HandleCallWindowsAbi(IntrusiveList<Operation> nodes, StackAllocator stackAlloc, Operation node)
  458. {
  459. Operand dest = node.Destination;
  460. // Handle struct arguments.
  461. int retArgs = 0;
  462. int stackAllocOffset = 0;
  463. int AllocateOnStack(int size)
  464. {
  465. // We assume that the stack allocator is initially empty (TotalSize = 0).
  466. // Taking that into account, we can reuse the space allocated for other
  467. // calls by keeping track of our own allocated size (stackAllocOffset).
  468. // If the space allocated is not big enough, then we just expand it.
  469. int offset = stackAllocOffset;
  470. if (stackAllocOffset + size > stackAlloc.TotalSize)
  471. {
  472. stackAlloc.Allocate((stackAllocOffset + size) - stackAlloc.TotalSize);
  473. }
  474. stackAllocOffset += size;
  475. return offset;
  476. }
  477. Operand arg0Reg = default;
  478. if (dest != default && dest.Type == OperandType.V128)
  479. {
  480. int stackOffset = AllocateOnStack(dest.Type.GetSizeInBytes());
  481. arg0Reg = Gpr(CallingConvention.GetIntArgumentRegister(0), OperandType.I64);
  482. Operation allocOp = Operation(Instruction.StackAlloc, arg0Reg, Const(stackOffset));
  483. nodes.AddBefore(node, allocOp);
  484. retArgs = 1;
  485. }
  486. int argsCount = node.SourcesCount - 1;
  487. int maxArgs = CallingConvention.GetArgumentsOnRegsCount() - retArgs;
  488. if (argsCount > maxArgs)
  489. {
  490. argsCount = maxArgs;
  491. }
  492. Operand[] sources = new Operand[1 + retArgs + argsCount];
  493. sources[0] = node.GetSource(0);
  494. if (arg0Reg != default)
  495. {
  496. sources[1] = arg0Reg;
  497. }
  498. for (int index = 1; index < node.SourcesCount; index++)
  499. {
  500. Operand source = node.GetSource(index);
  501. if (source.Type == OperandType.V128)
  502. {
  503. Operand stackAddr = Local(OperandType.I64);
  504. int stackOffset = AllocateOnStack(source.Type.GetSizeInBytes());
  505. nodes.AddBefore(node, Operation(Instruction.StackAlloc, stackAddr, Const(stackOffset)));
  506. Operation storeOp = Operation(Instruction.Store, default, stackAddr, source);
  507. HandleConstantRegCopy(nodes, nodes.AddBefore(node, storeOp));
  508. node.SetSource(index, stackAddr);
  509. }
  510. }
  511. // Handle arguments passed on registers.
  512. for (int index = 0; index < argsCount; index++)
  513. {
  514. Operand source = node.GetSource(index + 1);
  515. Operand argReg;
  516. int argIndex = index + retArgs;
  517. if (source.Type.IsInteger())
  518. {
  519. argReg = Gpr(CallingConvention.GetIntArgumentRegister(argIndex), source.Type);
  520. }
  521. else
  522. {
  523. argReg = Xmm(CallingConvention.GetVecArgumentRegister(argIndex), source.Type);
  524. }
  525. Operation copyOp = Operation(Instruction.Copy, argReg, source);
  526. HandleConstantRegCopy(nodes, nodes.AddBefore(node, copyOp));
  527. sources[1 + retArgs + index] = argReg;
  528. }
  529. // The remaining arguments (those that are not passed on registers)
  530. // should be passed on the stack, we write them to the stack with "SpillArg".
  531. for (int index = argsCount; index < node.SourcesCount - 1; index++)
  532. {
  533. Operand source = node.GetSource(index + 1);
  534. Operand offset = Const((index + retArgs) * 8);
  535. Operation spillOp = Operation(Instruction.SpillArg, default, offset, source);
  536. HandleConstantRegCopy(nodes, nodes.AddBefore(node, spillOp));
  537. }
  538. if (dest != default)
  539. {
  540. if (dest.Type == OperandType.V128)
  541. {
  542. Operand retValueAddr = Local(OperandType.I64);
  543. nodes.AddBefore(node, Operation(Instruction.Copy, retValueAddr, arg0Reg));
  544. Operation loadOp = Operation(Instruction.Load, dest, retValueAddr);
  545. nodes.AddAfter(node, loadOp);
  546. node.Destination = default;
  547. }
  548. else
  549. {
  550. Operand retReg = dest.Type.IsInteger()
  551. ? Gpr(CallingConvention.GetIntReturnRegister(), dest.Type)
  552. : Xmm(CallingConvention.GetVecReturnRegister(), dest.Type);
  553. Operation copyOp = Operation(Instruction.Copy, dest, retReg);
  554. nodes.AddAfter(node, copyOp);
  555. node.Destination = retReg;
  556. }
  557. }
  558. node.SetSources(sources);
  559. }
  560. private static void HandleCallSystemVAbi(IntrusiveList<Operation> nodes, Operation node)
  561. {
  562. Operand dest = node.Destination;
  563. List<Operand> sources = new List<Operand>
  564. {
  565. node.GetSource(0)
  566. };
  567. int argsCount = node.SourcesCount - 1;
  568. int intMax = CallingConvention.GetIntArgumentsOnRegsCount();
  569. int vecMax = CallingConvention.GetVecArgumentsOnRegsCount();
  570. int intCount = 0;
  571. int vecCount = 0;
  572. int stackOffset = 0;
  573. for (int index = 0; index < argsCount; index++)
  574. {
  575. Operand source = node.GetSource(index + 1);
  576. bool passOnReg;
  577. if (source.Type.IsInteger())
  578. {
  579. passOnReg = intCount < intMax;
  580. }
  581. else if (source.Type == OperandType.V128)
  582. {
  583. passOnReg = intCount + 1 < intMax;
  584. }
  585. else
  586. {
  587. passOnReg = vecCount < vecMax;
  588. }
  589. if (source.Type == OperandType.V128 && passOnReg)
  590. {
  591. // V128 is a struct, we pass each half on a GPR if possible.
  592. Operand argReg = Gpr(CallingConvention.GetIntArgumentRegister(intCount++), OperandType.I64);
  593. Operand argReg2 = Gpr(CallingConvention.GetIntArgumentRegister(intCount++), OperandType.I64);
  594. nodes.AddBefore(node, Operation(Instruction.VectorExtract, argReg, source, Const(0)));
  595. nodes.AddBefore(node, Operation(Instruction.VectorExtract, argReg2, source, Const(1)));
  596. continue;
  597. }
  598. if (passOnReg)
  599. {
  600. Operand argReg = source.Type.IsInteger()
  601. ? Gpr(CallingConvention.GetIntArgumentRegister(intCount++), source.Type)
  602. : Xmm(CallingConvention.GetVecArgumentRegister(vecCount++), source.Type);
  603. Operation copyOp = Operation(Instruction.Copy, argReg, source);
  604. HandleConstantRegCopy(nodes, nodes.AddBefore(node, copyOp));
  605. sources.Add(argReg);
  606. }
  607. else
  608. {
  609. Operand offset = Const(stackOffset);
  610. Operation spillOp = Operation(Instruction.SpillArg, default, offset, source);
  611. HandleConstantRegCopy(nodes, nodes.AddBefore(node, spillOp));
  612. stackOffset += source.Type.GetSizeInBytes();
  613. }
  614. }
  615. node.SetSources(sources.ToArray());
  616. if (dest != default)
  617. {
  618. if (dest.Type == OperandType.V128)
  619. {
  620. Operand retLReg = Gpr(CallingConvention.GetIntReturnRegister(), OperandType.I64);
  621. Operand retHReg = Gpr(CallingConvention.GetIntReturnRegisterHigh(), OperandType.I64);
  622. Operation operation = node;
  623. node = nodes.AddAfter(node, Operation(Instruction.VectorCreateScalar, dest, retLReg));
  624. nodes.AddAfter(node, Operation(Instruction.VectorInsert, dest, dest, retHReg, Const(1)));
  625. operation.Destination = default;
  626. }
  627. else
  628. {
  629. Operand retReg = dest.Type.IsInteger()
  630. ? Gpr(CallingConvention.GetIntReturnRegister(), dest.Type)
  631. : Xmm(CallingConvention.GetVecReturnRegister(), dest.Type);
  632. Operation copyOp = Operation(Instruction.Copy, dest, retReg);
  633. nodes.AddAfter(node, copyOp);
  634. node.Destination = retReg;
  635. }
  636. }
  637. }
  638. private static void HandleTailcallSystemVAbi(IntrusiveList<Operation> nodes, StackAllocator stackAlloc, Operation node)
  639. {
  640. List<Operand> sources = new List<Operand>
  641. {
  642. node.GetSource(0)
  643. };
  644. int argsCount = node.SourcesCount - 1;
  645. int intMax = CallingConvention.GetIntArgumentsOnRegsCount();
  646. int vecMax = CallingConvention.GetVecArgumentsOnRegsCount();
  647. int intCount = 0;
  648. int vecCount = 0;
  649. // Handle arguments passed on registers.
  650. for (int index = 0; index < argsCount; index++)
  651. {
  652. Operand source = node.GetSource(1 + index);
  653. bool passOnReg;
  654. if (source.Type.IsInteger())
  655. {
  656. passOnReg = intCount + 1 < intMax;
  657. }
  658. else
  659. {
  660. passOnReg = vecCount < vecMax;
  661. }
  662. if (source.Type == OperandType.V128 && passOnReg)
  663. {
  664. // V128 is a struct, we pass each half on a GPR if possible.
  665. Operand argReg = Gpr(CallingConvention.GetIntArgumentRegister(intCount++), OperandType.I64);
  666. Operand argReg2 = Gpr(CallingConvention.GetIntArgumentRegister(intCount++), OperandType.I64);
  667. nodes.AddBefore(node, Operation(Instruction.VectorExtract, argReg, source, Const(0)));
  668. nodes.AddBefore(node, Operation(Instruction.VectorExtract, argReg2, source, Const(1)));
  669. continue;
  670. }
  671. if (passOnReg)
  672. {
  673. Operand argReg = source.Type.IsInteger()
  674. ? Gpr(CallingConvention.GetIntArgumentRegister(intCount++), source.Type)
  675. : Xmm(CallingConvention.GetVecArgumentRegister(vecCount++), source.Type);
  676. Operation copyOp = Operation(Instruction.Copy, argReg, source);
  677. HandleConstantRegCopy(nodes, nodes.AddBefore(node, copyOp));
  678. sources.Add(argReg);
  679. }
  680. else
  681. {
  682. throw new NotImplementedException("Spilling is not currently supported for tail calls. (too many arguments)");
  683. }
  684. }
  685. // The target address must be on the return registers, since we
  686. // don't return anything and it is guaranteed to not be a
  687. // callee saved register (which would be trashed on the epilogue).
  688. Operand retReg = Gpr(CallingConvention.GetIntReturnRegister(), OperandType.I64);
  689. Operation addrCopyOp = Operation(Instruction.Copy, retReg, node.GetSource(0));
  690. nodes.AddBefore(node, addrCopyOp);
  691. sources[0] = retReg;
  692. node.SetSources(sources.ToArray());
  693. }
  694. private static void HandleTailcallWindowsAbi(IntrusiveList<Operation> nodes, StackAllocator stackAlloc, Operation node)
  695. {
  696. int argsCount = node.SourcesCount - 1;
  697. int maxArgs = CallingConvention.GetArgumentsOnRegsCount();
  698. if (argsCount > maxArgs)
  699. {
  700. throw new NotImplementedException("Spilling is not currently supported for tail calls. (too many arguments)");
  701. }
  702. Operand[] sources = new Operand[1 + argsCount];
  703. // Handle arguments passed on registers.
  704. for (int index = 0; index < argsCount; index++)
  705. {
  706. Operand source = node.GetSource(1 + index);
  707. Operand argReg = source.Type.IsInteger()
  708. ? Gpr(CallingConvention.GetIntArgumentRegister(index), source.Type)
  709. : Xmm(CallingConvention.GetVecArgumentRegister(index), source.Type);
  710. Operation copyOp = Operation(Instruction.Copy, argReg, source);
  711. HandleConstantRegCopy(nodes, nodes.AddBefore(node, copyOp));
  712. sources[1 + index] = argReg;
  713. }
  714. // The target address must be on the return registers, since we
  715. // don't return anything and it is guaranteed to not be a
  716. // callee saved register (which would be trashed on the epilogue).
  717. Operand retReg = Gpr(CallingConvention.GetIntReturnRegister(), OperandType.I64);
  718. Operation addrCopyOp = Operation(Instruction.Copy, retReg, node.GetSource(0));
  719. nodes.AddBefore(node, addrCopyOp);
  720. sources[0] = retReg;
  721. node.SetSources(sources);
  722. }
  723. private static Operation HandleLoadArgumentWindowsAbi(
  724. CompilerContext cctx,
  725. IntrusiveList<Operation> nodes,
  726. Operand[] preservedArgs,
  727. Operation node)
  728. {
  729. Operand source = node.GetSource(0);
  730. Debug.Assert(source.Kind == OperandKind.Constant, "Non-constant LoadArgument source kind.");
  731. int retArgs = cctx.FuncReturnType == OperandType.V128 ? 1 : 0;
  732. int index = source.AsInt32() + retArgs;
  733. if (index < CallingConvention.GetArgumentsOnRegsCount())
  734. {
  735. Operand dest = node.Destination;
  736. if (preservedArgs[index] == default)
  737. {
  738. Operand argReg, pArg;
  739. if (dest.Type.IsInteger())
  740. {
  741. argReg = Gpr(CallingConvention.GetIntArgumentRegister(index), dest.Type);
  742. pArg = Local(dest.Type);
  743. }
  744. else if (dest.Type == OperandType.V128)
  745. {
  746. argReg = Gpr(CallingConvention.GetIntArgumentRegister(index), OperandType.I64);
  747. pArg = Local(OperandType.I64);
  748. }
  749. else
  750. {
  751. argReg = Xmm(CallingConvention.GetVecArgumentRegister(index), dest.Type);
  752. pArg = Local(dest.Type);
  753. }
  754. Operation copyOp = Operation(Instruction.Copy, pArg, argReg);
  755. cctx.Cfg.Entry.Operations.AddFirst(copyOp);
  756. preservedArgs[index] = pArg;
  757. }
  758. Operation argCopyOp = Operation(dest.Type == OperandType.V128
  759. ? Instruction.Load
  760. : Instruction.Copy, dest, preservedArgs[index]);
  761. Operation newNode = nodes.AddBefore(node, argCopyOp);
  762. Delete(nodes, node);
  763. return newNode;
  764. }
  765. else
  766. {
  767. // TODO: Pass on stack.
  768. return node;
  769. }
  770. }
  771. private static Operation HandleLoadArgumentSystemVAbi(
  772. CompilerContext cctx,
  773. IntrusiveList<Operation> nodes,
  774. Operand[] preservedArgs,
  775. Operation node)
  776. {
  777. Operand source = node.GetSource(0);
  778. Debug.Assert(source.Kind == OperandKind.Constant, "Non-constant LoadArgument source kind.");
  779. int index = source.AsInt32();
  780. int intCount = 0;
  781. int vecCount = 0;
  782. for (int cIndex = 0; cIndex < index; cIndex++)
  783. {
  784. OperandType argType = cctx.FuncArgTypes[cIndex];
  785. if (argType.IsInteger())
  786. {
  787. intCount++;
  788. }
  789. else if (argType == OperandType.V128)
  790. {
  791. intCount += 2;
  792. }
  793. else
  794. {
  795. vecCount++;
  796. }
  797. }
  798. bool passOnReg;
  799. if (source.Type.IsInteger())
  800. {
  801. passOnReg = intCount < CallingConvention.GetIntArgumentsOnRegsCount();
  802. }
  803. else if (source.Type == OperandType.V128)
  804. {
  805. passOnReg = intCount + 1 < CallingConvention.GetIntArgumentsOnRegsCount();
  806. }
  807. else
  808. {
  809. passOnReg = vecCount < CallingConvention.GetVecArgumentsOnRegsCount();
  810. }
  811. if (passOnReg)
  812. {
  813. Operand dest = node.Destination;
  814. if (preservedArgs[index] == default)
  815. {
  816. if (dest.Type == OperandType.V128)
  817. {
  818. // V128 is a struct, we pass each half on a GPR if possible.
  819. Operand pArg = Local(OperandType.V128);
  820. Operand argLReg = Gpr(CallingConvention.GetIntArgumentRegister(intCount), OperandType.I64);
  821. Operand argHReg = Gpr(CallingConvention.GetIntArgumentRegister(intCount + 1), OperandType.I64);
  822. Operation copyL = Operation(Instruction.VectorCreateScalar, pArg, argLReg);
  823. Operation copyH = Operation(Instruction.VectorInsert, pArg, pArg, argHReg, Const(1));
  824. cctx.Cfg.Entry.Operations.AddFirst(copyH);
  825. cctx.Cfg.Entry.Operations.AddFirst(copyL);
  826. preservedArgs[index] = pArg;
  827. }
  828. else
  829. {
  830. Operand pArg = Local(dest.Type);
  831. Operand argReg = dest.Type.IsInteger()
  832. ? Gpr(CallingConvention.GetIntArgumentRegister(intCount), dest.Type)
  833. : Xmm(CallingConvention.GetVecArgumentRegister(vecCount), dest.Type);
  834. Operation copyOp = Operation(Instruction.Copy, pArg, argReg);
  835. cctx.Cfg.Entry.Operations.AddFirst(copyOp);
  836. preservedArgs[index] = pArg;
  837. }
  838. }
  839. Operation argCopyOp = Operation(Instruction.Copy, dest, preservedArgs[index]);
  840. Operation newNode = nodes.AddBefore(node, argCopyOp);
  841. Delete(nodes, node);
  842. return newNode;
  843. }
  844. else
  845. {
  846. // TODO: Pass on stack.
  847. return node;
  848. }
  849. }
  850. private static void HandleReturnWindowsAbi(
  851. CompilerContext cctx,
  852. IntrusiveList<Operation> nodes,
  853. Operand[] preservedArgs,
  854. Operation node)
  855. {
  856. if (node.SourcesCount == 0)
  857. {
  858. return;
  859. }
  860. Operand source = node.GetSource(0);
  861. Operand retReg;
  862. if (source.Type.IsInteger())
  863. {
  864. retReg = Gpr(CallingConvention.GetIntReturnRegister(), source.Type);
  865. }
  866. else if (source.Type == OperandType.V128)
  867. {
  868. if (preservedArgs[0] == default)
  869. {
  870. Operand preservedArg = Local(OperandType.I64);
  871. Operand arg0 = Gpr(CallingConvention.GetIntArgumentRegister(0), OperandType.I64);
  872. Operation copyOp = Operation(Instruction.Copy, preservedArg, arg0);
  873. cctx.Cfg.Entry.Operations.AddFirst(copyOp);
  874. preservedArgs[0] = preservedArg;
  875. }
  876. retReg = preservedArgs[0];
  877. }
  878. else
  879. {
  880. retReg = Xmm(CallingConvention.GetVecReturnRegister(), source.Type);
  881. }
  882. if (source.Type == OperandType.V128)
  883. {
  884. Operation retStoreOp = Operation(Instruction.Store, default, retReg, source);
  885. nodes.AddBefore(node, retStoreOp);
  886. }
  887. else
  888. {
  889. Operation retCopyOp = Operation(Instruction.Copy, retReg, source);
  890. nodes.AddBefore(node, retCopyOp);
  891. }
  892. node.SetSources(Array.Empty<Operand>());
  893. }
  894. private static void HandleReturnSystemVAbi(IntrusiveList<Operation> nodes, Operation node)
  895. {
  896. if (node.SourcesCount == 0)
  897. {
  898. return;
  899. }
  900. Operand source = node.GetSource(0);
  901. if (source.Type == OperandType.V128)
  902. {
  903. Operand retLReg = Gpr(CallingConvention.GetIntReturnRegister(), OperandType.I64);
  904. Operand retHReg = Gpr(CallingConvention.GetIntReturnRegisterHigh(), OperandType.I64);
  905. nodes.AddBefore(node, Operation(Instruction.VectorExtract, retLReg, source, Const(0)));
  906. nodes.AddBefore(node, Operation(Instruction.VectorExtract, retHReg, source, Const(1)));
  907. }
  908. else
  909. {
  910. Operand retReg = source.Type.IsInteger()
  911. ? Gpr(CallingConvention.GetIntReturnRegister(), source.Type)
  912. : Xmm(CallingConvention.GetVecReturnRegister(), source.Type);
  913. Operation retCopyOp = Operation(Instruction.Copy, retReg, source);
  914. nodes.AddBefore(node, retCopyOp);
  915. }
  916. }
  917. private static Operand AddXmmCopy(IntrusiveList<Operation> nodes, Operation node, Operand source)
  918. {
  919. Operand temp = Local(source.Type);
  920. Operand intConst = AddCopy(nodes, node, GetIntConst(source));
  921. Operation copyOp = Operation(Instruction.VectorCreateScalar, temp, intConst);
  922. nodes.AddBefore(node, copyOp);
  923. return temp;
  924. }
  925. private static Operand AddCopy(IntrusiveList<Operation> nodes, Operation node, Operand source)
  926. {
  927. Operand temp = Local(source.Type);
  928. Operation copyOp = Operation(Instruction.Copy, temp, source);
  929. nodes.AddBefore(node, copyOp);
  930. return temp;
  931. }
  932. private static Operand GetIntConst(Operand value)
  933. {
  934. if (value.Type == OperandType.FP32)
  935. {
  936. return Const(value.AsInt32());
  937. }
  938. else if (value.Type == OperandType.FP64)
  939. {
  940. return Const(value.AsInt64());
  941. }
  942. return value;
  943. }
  944. private static void Delete(IntrusiveList<Operation> nodes, Operation node)
  945. {
  946. node.Destination = default;
  947. for (int index = 0; index < node.SourcesCount; index++)
  948. {
  949. node.SetSource(index, default);
  950. }
  951. nodes.Remove(node);
  952. }
  953. private static Operand Gpr(X86Register register, OperandType type)
  954. {
  955. return Register((int)register, RegisterType.Integer, type);
  956. }
  957. private static Operand Xmm(X86Register register, OperandType type)
  958. {
  959. return Register((int)register, RegisterType.Vector, type);
  960. }
  961. private static bool IsSameOperandDestSrc1(Operation operation)
  962. {
  963. switch (operation.Instruction)
  964. {
  965. case Instruction.Add:
  966. return !HardwareCapabilities.SupportsVexEncoding && !operation.Destination.Type.IsInteger();
  967. case Instruction.Multiply:
  968. case Instruction.Subtract:
  969. return !HardwareCapabilities.SupportsVexEncoding || operation.Destination.Type.IsInteger();
  970. case Instruction.BitwiseAnd:
  971. case Instruction.BitwiseExclusiveOr:
  972. case Instruction.BitwiseNot:
  973. case Instruction.BitwiseOr:
  974. case Instruction.ByteSwap:
  975. case Instruction.Negate:
  976. case Instruction.RotateRight:
  977. case Instruction.ShiftLeft:
  978. case Instruction.ShiftRightSI:
  979. case Instruction.ShiftRightUI:
  980. return true;
  981. case Instruction.Divide:
  982. return !HardwareCapabilities.SupportsVexEncoding && !operation.Destination.Type.IsInteger();
  983. case Instruction.VectorInsert:
  984. case Instruction.VectorInsert16:
  985. case Instruction.VectorInsert8:
  986. return !HardwareCapabilities.SupportsVexEncoding;
  987. case Instruction.Extended:
  988. return IsIntrinsicSameOperandDestSrc1(operation);
  989. }
  990. return IsVexSameOperandDestSrc1(operation);
  991. }
  992. private static bool IsIntrinsicSameOperandDestSrc1(Operation operation)
  993. {
  994. IntrinsicInfo info = IntrinsicTable.GetInfo(operation.Intrinsic);
  995. return info.Type == IntrinsicType.Crc32 || info.Type == IntrinsicType.Fma || IsVexSameOperandDestSrc1(operation);
  996. }
  997. private static bool IsVexSameOperandDestSrc1(Operation operation)
  998. {
  999. if (IsIntrinsic(operation.Instruction))
  1000. {
  1001. IntrinsicInfo info = IntrinsicTable.GetInfo(operation.Intrinsic);
  1002. bool hasVex = HardwareCapabilities.SupportsVexEncoding && Assembler.SupportsVexPrefix(info.Inst);
  1003. bool isUnary = operation.SourcesCount < 2;
  1004. bool hasVecDest = operation.Destination != default && operation.Destination.Type == OperandType.V128;
  1005. return !hasVex && !isUnary && hasVecDest;
  1006. }
  1007. return false;
  1008. }
  1009. private static bool HasConstSrc1(Instruction inst)
  1010. {
  1011. switch (inst)
  1012. {
  1013. case Instruction.Copy:
  1014. case Instruction.LoadArgument:
  1015. case Instruction.Spill:
  1016. case Instruction.SpillArg:
  1017. return true;
  1018. }
  1019. return false;
  1020. }
  1021. private static bool HasConstSrc2(Instruction inst)
  1022. {
  1023. switch (inst)
  1024. {
  1025. case Instruction.Add:
  1026. case Instruction.BitwiseAnd:
  1027. case Instruction.BitwiseExclusiveOr:
  1028. case Instruction.BitwiseOr:
  1029. case Instruction.BranchIf:
  1030. case Instruction.Compare:
  1031. case Instruction.Multiply:
  1032. case Instruction.RotateRight:
  1033. case Instruction.ShiftLeft:
  1034. case Instruction.ShiftRightSI:
  1035. case Instruction.ShiftRightUI:
  1036. case Instruction.Store:
  1037. case Instruction.Store16:
  1038. case Instruction.Store8:
  1039. case Instruction.Subtract:
  1040. case Instruction.VectorExtract:
  1041. case Instruction.VectorExtract16:
  1042. case Instruction.VectorExtract8:
  1043. return true;
  1044. }
  1045. return false;
  1046. }
  1047. private static bool IsCommutative(Operation operation)
  1048. {
  1049. switch (operation.Instruction)
  1050. {
  1051. case Instruction.Add:
  1052. case Instruction.BitwiseAnd:
  1053. case Instruction.BitwiseExclusiveOr:
  1054. case Instruction.BitwiseOr:
  1055. case Instruction.Multiply:
  1056. return true;
  1057. case Instruction.BranchIf:
  1058. case Instruction.Compare:
  1059. {
  1060. Operand comp = operation.GetSource(2);
  1061. Debug.Assert(comp.Kind == OperandKind.Constant);
  1062. var compType = (Comparison)comp.AsInt32();
  1063. return compType == Comparison.Equal || compType == Comparison.NotEqual;
  1064. }
  1065. }
  1066. return false;
  1067. }
  1068. private static bool IsIntrinsic(Instruction inst)
  1069. {
  1070. return inst == Instruction.Extended;
  1071. }
  1072. private static bool IsXmmIntrinsic(Operation operation)
  1073. {
  1074. if (operation.Instruction != Instruction.Extended)
  1075. {
  1076. return false;
  1077. }
  1078. IntrinsicInfo info = IntrinsicTable.GetInfo(operation.Intrinsic);
  1079. return info.Type != IntrinsicType.Crc32;
  1080. }
  1081. }
  1082. }