PreAllocator.cs 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  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.OperandHelper;
  8. using static ARMeilleure.IntermediateRepresentation.OperationHelper;
  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. Node nextNode;
  21. for (Node node = block.Operations.First; node != null; node = nextNode)
  22. {
  23. nextNode = node.ListNext;
  24. if (node is not Operation operation)
  25. {
  26. continue;
  27. }
  28. HandleConstantRegCopy(block.Operations, node, operation);
  29. HandleDestructiveRegCopy(block.Operations, node, operation);
  30. HandleConstrainedRegCopy(block.Operations, node, operation);
  31. switch (operation.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 = operation.SourcesCount - 1;
  39. if (operation.Destination != null && operation.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, operation);
  52. }
  53. else /* if (callConv == CallConvName.SystemV) */
  54. {
  55. HandleCallSystemVAbi(block.Operations, node, operation);
  56. }
  57. break;
  58. case Instruction.ConvertToFPUI:
  59. HandleConvertToFPUI(block.Operations, node, operation);
  60. break;
  61. case Instruction.LoadArgument:
  62. if (callConv == CallConvName.Windows)
  63. {
  64. nextNode = HandleLoadArgumentWindowsAbi(cctx, block.Operations, node, preservedArgs, operation);
  65. }
  66. else /* if (callConv == CallConvName.SystemV) */
  67. {
  68. nextNode = HandleLoadArgumentSystemVAbi(cctx, block.Operations, node, preservedArgs, operation);
  69. }
  70. break;
  71. case Instruction.Negate:
  72. if (!operation.GetSource(0).Type.IsInteger())
  73. {
  74. HandleNegate(block.Operations, node, operation);
  75. }
  76. break;
  77. case Instruction.Return:
  78. if (callConv == CallConvName.Windows)
  79. {
  80. HandleReturnWindowsAbi(cctx, block.Operations, node, preservedArgs, operation);
  81. }
  82. else /* if (callConv == CallConvName.SystemV) */
  83. {
  84. HandleReturnSystemVAbi(block.Operations, node, operation);
  85. }
  86. break;
  87. case Instruction.Tailcall:
  88. if (callConv == CallConvName.Windows)
  89. {
  90. HandleTailcallWindowsAbi(block.Operations, stackAlloc, node, operation);
  91. }
  92. else
  93. {
  94. HandleTailcallSystemVAbi(block.Operations, stackAlloc, node, operation);
  95. }
  96. break;
  97. case Instruction.VectorInsert8:
  98. if (!HardwareCapabilities.SupportsSse41)
  99. {
  100. HandleVectorInsert8(block.Operations, node, operation);
  101. }
  102. break;
  103. case Instruction.Extended:
  104. IntrinsicOperation intrinOp = (IntrinsicOperation)operation;
  105. if (intrinOp.Intrinsic == Intrinsic.X86Mxcsrmb || intrinOp.Intrinsic == Intrinsic.X86Mxcsrub)
  106. {
  107. int stackOffset = stackAlloc.Allocate(OperandType.I32);
  108. operation.SetSources(new Operand[] { Const(stackOffset), operation.GetSource(0) });
  109. }
  110. break;
  111. }
  112. }
  113. }
  114. }
  115. private static void HandleConstantRegCopy(IntrusiveList<Node> nodes, Node node, Operation operation)
  116. {
  117. if (operation.SourcesCount == 0 || IsXmmIntrinsic(operation))
  118. {
  119. return;
  120. }
  121. Instruction inst = operation.Instruction;
  122. Operand src1 = operation.GetSource(0);
  123. Operand src2;
  124. if (src1.Kind == OperandKind.Constant)
  125. {
  126. if (!src1.Type.IsInteger())
  127. {
  128. // Handle non-integer types (FP32, FP64 and V128).
  129. // For instructions without an immediate operand, we do the following:
  130. // - Insert a copy with the constant value (as integer) to a GPR.
  131. // - Insert a copy from the GPR to a XMM register.
  132. // - Replace the constant use with the XMM register.
  133. src1 = AddXmmCopy(nodes, node, src1);
  134. operation.SetSource(0, src1);
  135. }
  136. else if (!HasConstSrc1(inst))
  137. {
  138. // Handle integer types.
  139. // Most ALU instructions accepts a 32-bits immediate on the second operand.
  140. // We need to ensure the following:
  141. // - If the constant is on operand 1, we need to move it.
  142. // -- But first, we try to swap operand 1 and 2 if the instruction is commutative.
  143. // -- Doing so may allow us to encode the constant as operand 2 and avoid a copy.
  144. // - If the constant is on operand 2, we check if the instruction supports it,
  145. // if not, we also add a copy. 64-bits constants are usually not supported.
  146. if (IsCommutative(operation))
  147. {
  148. src2 = operation.GetSource(1);
  149. Operand temp = src1;
  150. src1 = src2;
  151. src2 = temp;
  152. operation.SetSource(0, src1);
  153. operation.SetSource(1, src2);
  154. }
  155. if (src1.Kind == OperandKind.Constant)
  156. {
  157. src1 = AddCopy(nodes, node, src1);
  158. operation.SetSource(0, src1);
  159. }
  160. }
  161. }
  162. if (operation.SourcesCount < 2)
  163. {
  164. return;
  165. }
  166. src2 = operation.GetSource(1);
  167. if (src2.Kind == OperandKind.Constant)
  168. {
  169. if (!src2.Type.IsInteger())
  170. {
  171. src2 = AddXmmCopy(nodes, node, src2);
  172. operation.SetSource(1, src2);
  173. }
  174. else if (!HasConstSrc2(inst) || CodeGenCommon.IsLongConst(src2))
  175. {
  176. src2 = AddCopy(nodes, node, src2);
  177. operation.SetSource(1, src2);
  178. }
  179. }
  180. }
  181. private static void HandleConstrainedRegCopy(IntrusiveList<Node> nodes, Node node, Operation operation)
  182. {
  183. Operand dest = operation.Destination;
  184. switch (operation.Instruction)
  185. {
  186. case Instruction.CompareAndSwap:
  187. case Instruction.CompareAndSwap16:
  188. case Instruction.CompareAndSwap8:
  189. {
  190. OperandType type = operation.GetSource(1).Type;
  191. if (type == OperandType.V128)
  192. {
  193. // Handle the many restrictions of the compare and exchange (16 bytes) instruction:
  194. // - The expected value should be in RDX:RAX.
  195. // - The new value to be written should be in RCX:RBX.
  196. // - The value at the memory location is loaded to RDX:RAX.
  197. void SplitOperand(Operand source, Operand lr, Operand hr)
  198. {
  199. nodes.AddBefore(node, Operation(Instruction.VectorExtract, lr, source, Const(0)));
  200. nodes.AddBefore(node, Operation(Instruction.VectorExtract, hr, source, Const(1)));
  201. }
  202. Operand rax = Gpr(X86Register.Rax, OperandType.I64);
  203. Operand rbx = Gpr(X86Register.Rbx, OperandType.I64);
  204. Operand rcx = Gpr(X86Register.Rcx, OperandType.I64);
  205. Operand rdx = Gpr(X86Register.Rdx, OperandType.I64);
  206. SplitOperand(operation.GetSource(1), rax, rdx);
  207. SplitOperand(operation.GetSource(2), rbx, rcx);
  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 = operation.GetSource(1);
  219. Operand newValue = operation.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. operation.SetSources(new Operand[] { operation.GetSource(0), rax, temp });
  227. nodes.AddAfter(node, Operation(Instruction.Copy, dest, rax));
  228. operation.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 = operation.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. operation.SetDestinations(new Operand[] { rdx, rax });
  248. operation.SetSources(new Operand[] { rdx, rax, operation.GetSource(1) });
  249. operation.Destination = rax;
  250. }
  251. break;
  252. }
  253. case Instruction.Extended:
  254. {
  255. IntrinsicOperation intrinOp = (IntrinsicOperation)operation;
  256. // BLENDVPD, BLENDVPS, PBLENDVB last operand is always implied to be XMM0 when VEX is not supported.
  257. if ((intrinOp.Intrinsic == Intrinsic.X86Blendvpd ||
  258. intrinOp.Intrinsic == Intrinsic.X86Blendvps ||
  259. intrinOp.Intrinsic == Intrinsic.X86Pblendvb) &&
  260. !HardwareCapabilities.SupportsVexEncoding)
  261. {
  262. Operand xmm0 = Xmm(X86Register.Xmm0, OperandType.V128);
  263. nodes.AddBefore(node, Operation(Instruction.Copy, xmm0, operation.GetSource(2)));
  264. operation.SetSource(2, xmm0);
  265. }
  266. break;
  267. }
  268. case Instruction.Multiply64HighSI:
  269. case Instruction.Multiply64HighUI:
  270. {
  271. // Handle the many restrictions of the i64 * i64 = i128 multiply instructions:
  272. // - The multiplicand is always in RAX.
  273. // - The lower 64-bits of the result is always in RAX.
  274. // - The higher 64-bits of the result is always in RDX.
  275. Operand src1 = operation.GetSource(0);
  276. Operand rax = Gpr(X86Register.Rax, src1.Type);
  277. Operand rdx = Gpr(X86Register.Rdx, src1.Type);
  278. nodes.AddBefore(node, Operation(Instruction.Copy, rax, src1));
  279. operation.SetSource(0, rax);
  280. nodes.AddAfter(node, Operation(Instruction.Copy, dest, rdx));
  281. operation.SetDestinations(new Operand[] { rdx, rax });
  282. break;
  283. }
  284. case Instruction.RotateRight:
  285. case Instruction.ShiftLeft:
  286. case Instruction.ShiftRightSI:
  287. case Instruction.ShiftRightUI:
  288. {
  289. // The shift register is always implied to be CL (low 8-bits of RCX or ECX).
  290. if (operation.GetSource(1).Kind == OperandKind.LocalVariable)
  291. {
  292. Operand rcx = Gpr(X86Register.Rcx, OperandType.I32);
  293. nodes.AddBefore(node, Operation(Instruction.Copy, rcx, operation.GetSource(1)));
  294. operation.SetSource(1, rcx);
  295. }
  296. break;
  297. }
  298. }
  299. }
  300. private static void HandleDestructiveRegCopy(IntrusiveList<Node> nodes, Node node, Operation operation)
  301. {
  302. if (operation.Destination == null || operation.SourcesCount == 0)
  303. {
  304. return;
  305. }
  306. Instruction inst = operation.Instruction;
  307. Operand dest = operation.Destination;
  308. Operand src1 = operation.GetSource(0);
  309. // The multiply instruction (that maps to IMUL) is somewhat special, it has
  310. // a three operand form where the second source is a immediate value.
  311. bool threeOperandForm = inst == Instruction.Multiply && operation.GetSource(1).Kind == OperandKind.Constant;
  312. if (IsSameOperandDestSrc1(operation) && src1.Kind == OperandKind.LocalVariable && !threeOperandForm)
  313. {
  314. bool useNewLocal = false;
  315. for (int srcIndex = 1; srcIndex < operation.SourcesCount; srcIndex++)
  316. {
  317. if (operation.GetSource(srcIndex) == dest)
  318. {
  319. useNewLocal = true;
  320. break;
  321. }
  322. }
  323. if (useNewLocal)
  324. {
  325. // Dest is being used as some source already, we need to use a new
  326. // local to store the temporary value, otherwise the value on dest
  327. // local would be overwritten.
  328. Operand temp = Local(dest.Type);
  329. nodes.AddBefore(node, Operation(Instruction.Copy, temp, src1));
  330. operation.SetSource(0, temp);
  331. nodes.AddAfter(node, Operation(Instruction.Copy, dest, temp));
  332. operation.Destination = temp;
  333. }
  334. else
  335. {
  336. nodes.AddBefore(node, Operation(Instruction.Copy, dest, src1));
  337. operation.SetSource(0, dest);
  338. }
  339. }
  340. else if (inst == Instruction.ConditionalSelect)
  341. {
  342. Operand src2 = operation.GetSource(1);
  343. Operand src3 = operation.GetSource(2);
  344. if (src1 == dest || src2 == dest)
  345. {
  346. Operand temp = Local(dest.Type);
  347. nodes.AddBefore(node, Operation(Instruction.Copy, temp, src3));
  348. operation.SetSource(2, temp);
  349. nodes.AddAfter(node, Operation(Instruction.Copy, dest, temp));
  350. operation.Destination = temp;
  351. }
  352. else
  353. {
  354. nodes.AddBefore(node, Operation(Instruction.Copy, dest, src3));
  355. operation.SetSource(2, dest);
  356. }
  357. }
  358. }
  359. private static void HandleConvertToFPUI(IntrusiveList<Node> nodes, Node node, Operation operation)
  360. {
  361. // Unsigned integer to FP conversions are not supported on X86.
  362. // We need to turn them into signed integer to FP conversions, and
  363. // adjust the final result.
  364. Operand dest = operation.Destination;
  365. Operand source = operation.GetSource(0);
  366. Debug.Assert(source.Type.IsInteger(), $"Invalid source type \"{source.Type}\".");
  367. Node currentNode = node;
  368. if (source.Type == OperandType.I32)
  369. {
  370. // For 32-bits integers, we can just zero-extend to 64-bits,
  371. // and then use the 64-bits signed conversion instructions.
  372. Operand zex = Local(OperandType.I64);
  373. node = nodes.AddAfter(node, Operation(Instruction.ZeroExtend32, zex, source));
  374. node = nodes.AddAfter(node, Operation(Instruction.ConvertToFP, dest, zex));
  375. }
  376. else /* if (source.Type == OperandType.I64) */
  377. {
  378. // For 64-bits integers, we need to do the following:
  379. // - Ensure that the integer has the most significant bit clear.
  380. // -- This can be done by shifting the value right by 1, that is, dividing by 2.
  381. // -- The least significant bit is lost in this case though.
  382. // - We can then convert the shifted value with a signed integer instruction.
  383. // - The result still needs to be corrected after that.
  384. // -- First, we need to multiply the result by 2, as we divided it by 2 before.
  385. // --- This can be done efficiently by adding the result to itself.
  386. // -- Then, we need to add the least significant bit that was shifted out.
  387. // --- We can convert the least significant bit to float, and add it to the result.
  388. Operand lsb = Local(OperandType.I64);
  389. Operand half = Local(OperandType.I64);
  390. Operand lsbF = Local(dest.Type);
  391. node = nodes.AddAfter(node, Operation(Instruction.Copy, lsb, source));
  392. node = nodes.AddAfter(node, Operation(Instruction.Copy, half, source));
  393. node = nodes.AddAfter(node, Operation(Instruction.BitwiseAnd, lsb, lsb, Const(1L)));
  394. node = nodes.AddAfter(node, Operation(Instruction.ShiftRightUI, half, half, Const(1)));
  395. node = nodes.AddAfter(node, Operation(Instruction.ConvertToFP, lsbF, lsb));
  396. node = nodes.AddAfter(node, Operation(Instruction.ConvertToFP, dest, half));
  397. node = nodes.AddAfter(node, Operation(Instruction.Add, dest, dest, dest));
  398. nodes.AddAfter(node, Operation(Instruction.Add, dest, dest, lsbF));
  399. }
  400. Delete(nodes, currentNode, operation);
  401. }
  402. private static void HandleNegate(IntrusiveList<Node> nodes, Node node, Operation operation)
  403. {
  404. // There's no SSE FP negate instruction, so we need to transform that into
  405. // a XOR of the value to be negated with a mask with the highest bit set.
  406. // This also produces -0 for a negation of the value 0.
  407. Operand dest = operation.Destination;
  408. Operand source = operation.GetSource(0);
  409. Debug.Assert(dest.Type == OperandType.FP32 ||
  410. dest.Type == OperandType.FP64, $"Invalid destination type \"{dest.Type}\".");
  411. Node currentNode = node;
  412. Operand res = Local(dest.Type);
  413. node = nodes.AddAfter(node, Operation(Instruction.VectorOne, res));
  414. if (dest.Type == OperandType.FP32)
  415. {
  416. node = nodes.AddAfter(node, new IntrinsicOperation(Intrinsic.X86Pslld, res, res, Const(31)));
  417. }
  418. else /* if (dest.Type == OperandType.FP64) */
  419. {
  420. node = nodes.AddAfter(node, new IntrinsicOperation(Intrinsic.X86Psllq, res, res, Const(63)));
  421. }
  422. node = nodes.AddAfter(node, new IntrinsicOperation(Intrinsic.X86Xorps, res, res, source));
  423. nodes.AddAfter(node, Operation(Instruction.Copy, dest, res));
  424. Delete(nodes, currentNode, operation);
  425. }
  426. private static void HandleVectorInsert8(IntrusiveList<Node> nodes, Node node, Operation operation)
  427. {
  428. // Handle vector insertion, when SSE 4.1 is not supported.
  429. Operand dest = operation.Destination;
  430. Operand src1 = operation.GetSource(0); // Vector
  431. Operand src2 = operation.GetSource(1); // Value
  432. Operand src3 = operation.GetSource(2); // Index
  433. Debug.Assert(src3.Kind == OperandKind.Constant);
  434. byte index = src3.AsByte();
  435. Debug.Assert(index < 16);
  436. Node currentNode = node;
  437. Operand temp1 = Local(OperandType.I32);
  438. Operand temp2 = Local(OperandType.I32);
  439. node = nodes.AddAfter(node, Operation(Instruction.Copy, temp2, src2));
  440. Operation vextOp = Operation(Instruction.VectorExtract16, temp1, src1, Const(index >> 1));
  441. node = nodes.AddAfter(node, vextOp);
  442. if ((index & 1) != 0)
  443. {
  444. node = nodes.AddAfter(node, Operation(Instruction.ZeroExtend8, temp1, temp1));
  445. node = nodes.AddAfter(node, Operation(Instruction.ShiftLeft, temp2, temp2, Const(8)));
  446. node = nodes.AddAfter(node, Operation(Instruction.BitwiseOr, temp1, temp1, temp2));
  447. }
  448. else
  449. {
  450. node = nodes.AddAfter(node, Operation(Instruction.ZeroExtend8, temp2, temp2));
  451. node = nodes.AddAfter(node, Operation(Instruction.BitwiseAnd, temp1, temp1, Const(0xff00)));
  452. node = nodes.AddAfter(node, Operation(Instruction.BitwiseOr, temp1, temp1, temp2));
  453. }
  454. Operation vinsOp = Operation(Instruction.VectorInsert16, dest, src1, temp1, Const(index >> 1));
  455. nodes.AddAfter(node, vinsOp);
  456. Delete(nodes, currentNode, operation);
  457. }
  458. private static void HandleCallWindowsAbi(IntrusiveList<Node> nodes, StackAllocator stackAlloc, Node node, Operation operation)
  459. {
  460. Operand dest = operation.Destination;
  461. // Handle struct arguments.
  462. int retArgs = 0;
  463. int stackAllocOffset = 0;
  464. int AllocateOnStack(int size)
  465. {
  466. // We assume that the stack allocator is initially empty (TotalSize = 0).
  467. // Taking that into account, we can reuse the space allocated for other
  468. // calls by keeping track of our own allocated size (stackAllocOffset).
  469. // If the space allocated is not big enough, then we just expand it.
  470. int offset = stackAllocOffset;
  471. if (stackAllocOffset + size > stackAlloc.TotalSize)
  472. {
  473. stackAlloc.Allocate((stackAllocOffset + size) - stackAlloc.TotalSize);
  474. }
  475. stackAllocOffset += size;
  476. return offset;
  477. }
  478. Operand arg0Reg = null;
  479. if (dest != null && dest.Type == OperandType.V128)
  480. {
  481. int stackOffset = AllocateOnStack(dest.Type.GetSizeInBytes());
  482. arg0Reg = Gpr(CallingConvention.GetIntArgumentRegister(0), OperandType.I64);
  483. Operation allocOp = Operation(Instruction.StackAlloc, arg0Reg, Const(stackOffset));
  484. nodes.AddBefore(node, allocOp);
  485. retArgs = 1;
  486. }
  487. int argsCount = operation.SourcesCount - 1;
  488. int maxArgs = CallingConvention.GetArgumentsOnRegsCount() - retArgs;
  489. if (argsCount > maxArgs)
  490. {
  491. argsCount = maxArgs;
  492. }
  493. Operand[] sources = new Operand[1 + retArgs + argsCount];
  494. sources[0] = operation.GetSource(0);
  495. if (arg0Reg != null)
  496. {
  497. sources[1] = arg0Reg;
  498. }
  499. for (int index = 1; index < operation.SourcesCount; index++)
  500. {
  501. Operand source = operation.GetSource(index);
  502. if (source.Type == OperandType.V128)
  503. {
  504. Operand stackAddr = Local(OperandType.I64);
  505. int stackOffset = AllocateOnStack(source.Type.GetSizeInBytes());
  506. nodes.AddBefore(node, Operation(Instruction.StackAlloc, stackAddr, Const(stackOffset)));
  507. Operation storeOp = Operation(Instruction.Store, null, stackAddr, source);
  508. HandleConstantRegCopy(nodes, nodes.AddBefore(node, storeOp), storeOp);
  509. operation.SetSource(index, stackAddr);
  510. }
  511. }
  512. // Handle arguments passed on registers.
  513. for (int index = 0; index < argsCount; index++)
  514. {
  515. Operand source = operation.GetSource(index + 1);
  516. Operand argReg;
  517. int argIndex = index + retArgs;
  518. if (source.Type.IsInteger())
  519. {
  520. argReg = Gpr(CallingConvention.GetIntArgumentRegister(argIndex), source.Type);
  521. }
  522. else
  523. {
  524. argReg = Xmm(CallingConvention.GetVecArgumentRegister(argIndex), source.Type);
  525. }
  526. Operation copyOp = Operation(Instruction.Copy, argReg, source);
  527. HandleConstantRegCopy(nodes, nodes.AddBefore(node, copyOp), copyOp);
  528. sources[1 + retArgs + index] = argReg;
  529. }
  530. // The remaining arguments (those that are not passed on registers)
  531. // should be passed on the stack, we write them to the stack with "SpillArg".
  532. for (int index = argsCount; index < operation.SourcesCount - 1; index++)
  533. {
  534. Operand source = operation.GetSource(index + 1);
  535. Operand offset = Const((index + retArgs) * 8);
  536. Operation spillOp = Operation(Instruction.SpillArg, null, offset, source);
  537. HandleConstantRegCopy(nodes, nodes.AddBefore(node, spillOp), spillOp);
  538. }
  539. if (dest != null)
  540. {
  541. if (dest.Type == OperandType.V128)
  542. {
  543. Operand retValueAddr = Local(OperandType.I64);
  544. nodes.AddBefore(node, Operation(Instruction.Copy, retValueAddr, arg0Reg));
  545. Operation loadOp = Operation(Instruction.Load, dest, retValueAddr);
  546. nodes.AddAfter(node, loadOp);
  547. operation.Destination = null;
  548. }
  549. else
  550. {
  551. Operand retReg = dest.Type.IsInteger()
  552. ? Gpr(CallingConvention.GetIntReturnRegister(), dest.Type)
  553. : Xmm(CallingConvention.GetVecReturnRegister(), dest.Type);
  554. Operation copyOp = Operation(Instruction.Copy, dest, retReg);
  555. nodes.AddAfter(node, copyOp);
  556. operation.Destination = retReg;
  557. }
  558. }
  559. operation.SetSources(sources);
  560. }
  561. private static void HandleCallSystemVAbi(IntrusiveList<Node> nodes, Node node, Operation operation)
  562. {
  563. Operand dest = operation.Destination;
  564. List<Operand> sources = new List<Operand>
  565. {
  566. operation.GetSource(0)
  567. };
  568. int argsCount = operation.SourcesCount - 1;
  569. int intMax = CallingConvention.GetIntArgumentsOnRegsCount();
  570. int vecMax = CallingConvention.GetVecArgumentsOnRegsCount();
  571. int intCount = 0;
  572. int vecCount = 0;
  573. int stackOffset = 0;
  574. for (int index = 0; index < argsCount; index++)
  575. {
  576. Operand source = operation.GetSource(index + 1);
  577. bool passOnReg;
  578. if (source.Type.IsInteger())
  579. {
  580. passOnReg = intCount < intMax;
  581. }
  582. else if (source.Type == OperandType.V128)
  583. {
  584. passOnReg = intCount + 1 < intMax;
  585. }
  586. else
  587. {
  588. passOnReg = vecCount < vecMax;
  589. }
  590. if (source.Type == OperandType.V128 && passOnReg)
  591. {
  592. // V128 is a struct, we pass each half on a GPR if possible.
  593. Operand argReg = Gpr(CallingConvention.GetIntArgumentRegister(intCount++), OperandType.I64);
  594. Operand argReg2 = Gpr(CallingConvention.GetIntArgumentRegister(intCount++), OperandType.I64);
  595. nodes.AddBefore(node, Operation(Instruction.VectorExtract, argReg, source, Const(0)));
  596. nodes.AddBefore(node, Operation(Instruction.VectorExtract, argReg2, source, Const(1)));
  597. continue;
  598. }
  599. if (passOnReg)
  600. {
  601. Operand argReg = source.Type.IsInteger()
  602. ? Gpr(CallingConvention.GetIntArgumentRegister(intCount++), source.Type)
  603. : Xmm(CallingConvention.GetVecArgumentRegister(vecCount++), source.Type);
  604. Operation copyOp = Operation(Instruction.Copy, argReg, source);
  605. HandleConstantRegCopy(nodes, nodes.AddBefore(node, copyOp), copyOp);
  606. sources.Add(argReg);
  607. }
  608. else
  609. {
  610. Operand offset = Const(stackOffset);
  611. Operation spillOp = Operation(Instruction.SpillArg, null, offset, source);
  612. HandleConstantRegCopy(nodes, nodes.AddBefore(node, spillOp), spillOp);
  613. stackOffset += source.Type.GetSizeInBytes();
  614. }
  615. }
  616. if (dest != null)
  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. node = nodes.AddAfter(node, Operation(Instruction.VectorCreateScalar, dest, retLReg));
  623. nodes.AddAfter(node, Operation(Instruction.VectorInsert, dest, dest, retHReg, Const(1)));
  624. operation.Destination = null;
  625. }
  626. else
  627. {
  628. Operand retReg = dest.Type.IsInteger()
  629. ? Gpr(CallingConvention.GetIntReturnRegister(), dest.Type)
  630. : Xmm(CallingConvention.GetVecReturnRegister(), dest.Type);
  631. Operation copyOp = Operation(Instruction.Copy, dest, retReg);
  632. nodes.AddAfter(node, copyOp);
  633. operation.Destination = retReg;
  634. }
  635. }
  636. operation.SetSources(sources.ToArray());
  637. }
  638. private static void HandleTailcallSystemVAbi(IntrusiveList<Node> nodes, StackAllocator stackAlloc, Node node, Operation operation)
  639. {
  640. List<Operand> sources = new List<Operand>
  641. {
  642. operation.GetSource(0)
  643. };
  644. int argsCount = operation.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 = operation.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), 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, operation.GetSource(0));
  690. nodes.AddBefore(node, addrCopyOp);
  691. sources[0] = retReg;
  692. operation.SetSources(sources.ToArray());
  693. }
  694. private static void HandleTailcallWindowsAbi(IntrusiveList<Node> nodes, StackAllocator stackAlloc, Node node, Operation operation)
  695. {
  696. int argsCount = operation.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 = operation.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), 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, operation.GetSource(0));
  719. nodes.AddBefore(node, addrCopyOp);
  720. sources[0] = retReg;
  721. operation.SetSources(sources);
  722. }
  723. private static Node HandleLoadArgumentWindowsAbi(
  724. CompilerContext cctx,
  725. IntrusiveList<Node> nodes,
  726. Node node,
  727. Operand[] preservedArgs,
  728. Operation operation)
  729. {
  730. Operand source = operation.GetSource(0);
  731. Debug.Assert(source.Kind == OperandKind.Constant, "Non-constant LoadArgument source kind.");
  732. int retArgs = cctx.FuncReturnType == OperandType.V128 ? 1 : 0;
  733. int index = source.AsInt32() + retArgs;
  734. if (index < CallingConvention.GetArgumentsOnRegsCount())
  735. {
  736. Operand dest = operation.Destination;
  737. if (preservedArgs[index] == null)
  738. {
  739. Operand argReg, pArg;
  740. if (dest.Type.IsInteger())
  741. {
  742. argReg = Gpr(CallingConvention.GetIntArgumentRegister(index), dest.Type);
  743. pArg = Local(dest.Type);
  744. }
  745. else if (dest.Type == OperandType.V128)
  746. {
  747. argReg = Gpr(CallingConvention.GetIntArgumentRegister(index), OperandType.I64);
  748. pArg = Local(OperandType.I64);
  749. }
  750. else
  751. {
  752. argReg = Xmm(CallingConvention.GetVecArgumentRegister(index), dest.Type);
  753. pArg = Local(dest.Type);
  754. }
  755. Operation copyOp = Operation(Instruction.Copy, pArg, argReg);
  756. cctx.Cfg.Entry.Operations.AddFirst(copyOp);
  757. preservedArgs[index] = pArg;
  758. }
  759. Operation argCopyOp = Operation(dest.Type == OperandType.V128
  760. ? Instruction.Load
  761. : Instruction.Copy, dest, preservedArgs[index]);
  762. Node newNode = nodes.AddBefore(node, argCopyOp);
  763. Delete(nodes, node, operation);
  764. return newNode;
  765. }
  766. else
  767. {
  768. // TODO: Pass on stack.
  769. return node;
  770. }
  771. }
  772. private static Node HandleLoadArgumentSystemVAbi(
  773. CompilerContext cctx,
  774. IntrusiveList<Node> nodes,
  775. Node node,
  776. Operand[] preservedArgs,
  777. Operation operation)
  778. {
  779. Operand source = operation.GetSource(0);
  780. Debug.Assert(source.Kind == OperandKind.Constant, "Non-constant LoadArgument source kind.");
  781. int index = source.AsInt32();
  782. int intCount = 0;
  783. int vecCount = 0;
  784. for (int cIndex = 0; cIndex < index; cIndex++)
  785. {
  786. OperandType argType = cctx.FuncArgTypes[cIndex];
  787. if (argType.IsInteger())
  788. {
  789. intCount++;
  790. }
  791. else if (argType == OperandType.V128)
  792. {
  793. intCount += 2;
  794. }
  795. else
  796. {
  797. vecCount++;
  798. }
  799. }
  800. bool passOnReg;
  801. if (source.Type.IsInteger())
  802. {
  803. passOnReg = intCount < CallingConvention.GetIntArgumentsOnRegsCount();
  804. }
  805. else if (source.Type == OperandType.V128)
  806. {
  807. passOnReg = intCount + 1 < CallingConvention.GetIntArgumentsOnRegsCount();
  808. }
  809. else
  810. {
  811. passOnReg = vecCount < CallingConvention.GetVecArgumentsOnRegsCount();
  812. }
  813. if (passOnReg)
  814. {
  815. Operand dest = operation.Destination;
  816. if (preservedArgs[index] == null)
  817. {
  818. if (dest.Type == OperandType.V128)
  819. {
  820. // V128 is a struct, we pass each half on a GPR if possible.
  821. Operand pArg = Local(OperandType.V128);
  822. Operand argLReg = Gpr(CallingConvention.GetIntArgumentRegister(intCount), OperandType.I64);
  823. Operand argHReg = Gpr(CallingConvention.GetIntArgumentRegister(intCount + 1), OperandType.I64);
  824. Operation copyL = Operation(Instruction.VectorCreateScalar, pArg, argLReg);
  825. Operation copyH = Operation(Instruction.VectorInsert, pArg, pArg, argHReg, Const(1));
  826. cctx.Cfg.Entry.Operations.AddFirst(copyH);
  827. cctx.Cfg.Entry.Operations.AddFirst(copyL);
  828. preservedArgs[index] = pArg;
  829. }
  830. else
  831. {
  832. Operand pArg = Local(dest.Type);
  833. Operand argReg = dest.Type.IsInteger()
  834. ? Gpr(CallingConvention.GetIntArgumentRegister(intCount), dest.Type)
  835. : Xmm(CallingConvention.GetVecArgumentRegister(vecCount), dest.Type);
  836. Operation copyOp = Operation(Instruction.Copy, pArg, argReg);
  837. cctx.Cfg.Entry.Operations.AddFirst(copyOp);
  838. preservedArgs[index] = pArg;
  839. }
  840. }
  841. Operation argCopyOp = Operation(Instruction.Copy, dest, preservedArgs[index]);
  842. Node newNode = nodes.AddBefore(node, argCopyOp);
  843. Delete(nodes, node, operation);
  844. return newNode;
  845. }
  846. else
  847. {
  848. // TODO: Pass on stack.
  849. return node;
  850. }
  851. }
  852. private static void HandleReturnWindowsAbi(
  853. CompilerContext cctx,
  854. IntrusiveList<Node> nodes,
  855. Node node,
  856. Operand[] preservedArgs,
  857. Operation operation)
  858. {
  859. if (operation.SourcesCount == 0)
  860. {
  861. return;
  862. }
  863. Operand source = operation.GetSource(0);
  864. Operand retReg;
  865. if (source.Type.IsInteger())
  866. {
  867. retReg = Gpr(CallingConvention.GetIntReturnRegister(), source.Type);
  868. }
  869. else if (source.Type == OperandType.V128)
  870. {
  871. if (preservedArgs[0] == null)
  872. {
  873. Operand preservedArg = Local(OperandType.I64);
  874. Operand arg0 = Gpr(CallingConvention.GetIntArgumentRegister(0), OperandType.I64);
  875. Operation copyOp = Operation(Instruction.Copy, preservedArg, arg0);
  876. cctx.Cfg.Entry.Operations.AddFirst(copyOp);
  877. preservedArgs[0] = preservedArg;
  878. }
  879. retReg = preservedArgs[0];
  880. }
  881. else
  882. {
  883. retReg = Xmm(CallingConvention.GetVecReturnRegister(), source.Type);
  884. }
  885. if (source.Type == OperandType.V128)
  886. {
  887. Operation retStoreOp = Operation(Instruction.Store, null, retReg, source);
  888. nodes.AddBefore(node, retStoreOp);
  889. }
  890. else
  891. {
  892. Operation retCopyOp = Operation(Instruction.Copy, retReg, source);
  893. nodes.AddBefore(node, retCopyOp);
  894. }
  895. operation.SetSources(Array.Empty<Operand>());
  896. }
  897. private static void HandleReturnSystemVAbi(IntrusiveList<Node> nodes, Node node, Operation operation)
  898. {
  899. if (operation.SourcesCount == 0)
  900. {
  901. return;
  902. }
  903. Operand source = operation.GetSource(0);
  904. if (source.Type == OperandType.V128)
  905. {
  906. Operand retLReg = Gpr(CallingConvention.GetIntReturnRegister(), OperandType.I64);
  907. Operand retHReg = Gpr(CallingConvention.GetIntReturnRegisterHigh(), OperandType.I64);
  908. nodes.AddBefore(node, Operation(Instruction.VectorExtract, retLReg, source, Const(0)));
  909. nodes.AddBefore(node, Operation(Instruction.VectorExtract, retHReg, source, Const(1)));
  910. }
  911. else
  912. {
  913. Operand retReg = source.Type.IsInteger()
  914. ? Gpr(CallingConvention.GetIntReturnRegister(), source.Type)
  915. : Xmm(CallingConvention.GetVecReturnRegister(), source.Type);
  916. Operation retCopyOp = Operation(Instruction.Copy, retReg, source);
  917. nodes.AddBefore(node, retCopyOp);
  918. }
  919. }
  920. private static Operand AddXmmCopy(IntrusiveList<Node> nodes, Node node, Operand source)
  921. {
  922. Operand temp = Local(source.Type);
  923. Operand intConst = AddCopy(nodes, node, GetIntConst(source));
  924. Operation copyOp = Operation(Instruction.VectorCreateScalar, temp, intConst);
  925. nodes.AddBefore(node, copyOp);
  926. return temp;
  927. }
  928. private static Operand AddCopy(IntrusiveList<Node> nodes, Node node, Operand source)
  929. {
  930. Operand temp = Local(source.Type);
  931. Operation copyOp = Operation(Instruction.Copy, temp, source);
  932. nodes.AddBefore(node, copyOp);
  933. return temp;
  934. }
  935. private static Operand GetIntConst(Operand value)
  936. {
  937. if (value.Type == OperandType.FP32)
  938. {
  939. return Const(value.AsInt32());
  940. }
  941. else if (value.Type == OperandType.FP64)
  942. {
  943. return Const(value.AsInt64());
  944. }
  945. return value;
  946. }
  947. private static void Delete(IntrusiveList<Node> nodes, Node node, Operation operation)
  948. {
  949. operation.Destination = null;
  950. for (int index = 0; index < operation.SourcesCount; index++)
  951. {
  952. operation.SetSource(index, null);
  953. }
  954. nodes.Remove(node);
  955. }
  956. private static Operand Gpr(X86Register register, OperandType type)
  957. {
  958. return Register((int)register, RegisterType.Integer, type);
  959. }
  960. private static Operand Xmm(X86Register register, OperandType type)
  961. {
  962. return Register((int)register, RegisterType.Vector, type);
  963. }
  964. private static bool IsSameOperandDestSrc1(Operation operation)
  965. {
  966. switch (operation.Instruction)
  967. {
  968. case Instruction.Add:
  969. return !HardwareCapabilities.SupportsVexEncoding && !operation.Destination.Type.IsInteger();
  970. case Instruction.Multiply:
  971. case Instruction.Subtract:
  972. return !HardwareCapabilities.SupportsVexEncoding || operation.Destination.Type.IsInteger();
  973. case Instruction.BitwiseAnd:
  974. case Instruction.BitwiseExclusiveOr:
  975. case Instruction.BitwiseNot:
  976. case Instruction.BitwiseOr:
  977. case Instruction.ByteSwap:
  978. case Instruction.Negate:
  979. case Instruction.RotateRight:
  980. case Instruction.ShiftLeft:
  981. case Instruction.ShiftRightSI:
  982. case Instruction.ShiftRightUI:
  983. return true;
  984. case Instruction.Divide:
  985. return !HardwareCapabilities.SupportsVexEncoding && !operation.Destination.Type.IsInteger();
  986. case Instruction.VectorInsert:
  987. case Instruction.VectorInsert16:
  988. case Instruction.VectorInsert8:
  989. return !HardwareCapabilities.SupportsVexEncoding;
  990. case Instruction.Extended:
  991. return IsIntrinsicSameOperandDestSrc1(operation);
  992. }
  993. return IsVexSameOperandDestSrc1(operation);
  994. }
  995. private static bool IsIntrinsicSameOperandDestSrc1(Operation operation)
  996. {
  997. IntrinsicOperation intrinOp = (IntrinsicOperation)operation;
  998. IntrinsicInfo info = IntrinsicTable.GetInfo(intrinOp.Intrinsic);
  999. return info.Type == IntrinsicType.Crc32 || info.Type == IntrinsicType.Fma || IsVexSameOperandDestSrc1(operation);
  1000. }
  1001. private static bool IsVexSameOperandDestSrc1(Operation operation)
  1002. {
  1003. if (IsIntrinsic(operation.Instruction))
  1004. {
  1005. bool isUnary = operation.SourcesCount < 2;
  1006. bool hasVecDest = operation.Destination != null && operation.Destination.Type == OperandType.V128;
  1007. return !HardwareCapabilities.SupportsVexEncoding && !isUnary && hasVecDest;
  1008. }
  1009. return false;
  1010. }
  1011. private static bool HasConstSrc1(Instruction inst)
  1012. {
  1013. switch (inst)
  1014. {
  1015. case Instruction.Copy:
  1016. case Instruction.LoadArgument:
  1017. case Instruction.Spill:
  1018. case Instruction.SpillArg:
  1019. return true;
  1020. }
  1021. return false;
  1022. }
  1023. private static bool HasConstSrc2(Instruction inst)
  1024. {
  1025. switch (inst)
  1026. {
  1027. case Instruction.Add:
  1028. case Instruction.BitwiseAnd:
  1029. case Instruction.BitwiseExclusiveOr:
  1030. case Instruction.BitwiseOr:
  1031. case Instruction.BranchIf:
  1032. case Instruction.Compare:
  1033. case Instruction.Multiply:
  1034. case Instruction.RotateRight:
  1035. case Instruction.ShiftLeft:
  1036. case Instruction.ShiftRightSI:
  1037. case Instruction.ShiftRightUI:
  1038. case Instruction.Store:
  1039. case Instruction.Store16:
  1040. case Instruction.Store8:
  1041. case Instruction.Subtract:
  1042. case Instruction.VectorExtract:
  1043. case Instruction.VectorExtract16:
  1044. case Instruction.VectorExtract8:
  1045. return true;
  1046. }
  1047. return false;
  1048. }
  1049. private static bool IsCommutative(Operation operation)
  1050. {
  1051. switch (operation.Instruction)
  1052. {
  1053. case Instruction.Add:
  1054. case Instruction.BitwiseAnd:
  1055. case Instruction.BitwiseExclusiveOr:
  1056. case Instruction.BitwiseOr:
  1057. case Instruction.Multiply:
  1058. return true;
  1059. case Instruction.BranchIf:
  1060. case Instruction.Compare:
  1061. {
  1062. Operand comp = operation.GetSource(2);
  1063. Debug.Assert(comp.Kind == OperandKind.Constant);
  1064. var compType = (Comparison)comp.AsInt32();
  1065. return compType == Comparison.Equal || compType == Comparison.NotEqual;
  1066. }
  1067. }
  1068. return false;
  1069. }
  1070. private static bool IsIntrinsic(Instruction inst)
  1071. {
  1072. return inst == Instruction.Extended;
  1073. }
  1074. private static bool IsXmmIntrinsic(Operation operation)
  1075. {
  1076. if (operation.Instruction != Instruction.Extended)
  1077. {
  1078. return false;
  1079. }
  1080. IntrinsicOperation intrinOp = (IntrinsicOperation)operation;
  1081. IntrinsicInfo info = IntrinsicTable.GetInfo(intrinOp.Intrinsic);
  1082. return info.Type != IntrinsicType.Crc32;
  1083. }
  1084. }
  1085. }