PreAllocator.cs 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  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. // BLENDVPD, BLENDVPS, PBLENDVB last operand is always implied to be XMM0 when VEX is not supported.
  255. if ((node.Intrinsic == Intrinsic.X86Blendvpd ||
  256. node.Intrinsic == Intrinsic.X86Blendvps ||
  257. node.Intrinsic == Intrinsic.X86Pblendvb) &&
  258. !HardwareCapabilities.SupportsVexEncoding)
  259. {
  260. Operand xmm0 = Xmm(X86Register.Xmm0, OperandType.V128);
  261. nodes.AddBefore(node, Operation(Instruction.Copy, xmm0, node.GetSource(2)));
  262. node.SetSource(2, xmm0);
  263. }
  264. break;
  265. }
  266. case Instruction.Multiply64HighSI:
  267. case Instruction.Multiply64HighUI:
  268. {
  269. // Handle the many restrictions of the i64 * i64 = i128 multiply instructions:
  270. // - The multiplicand is always in RAX.
  271. // - The lower 64-bits of the result is always in RAX.
  272. // - The higher 64-bits of the result is always in RDX.
  273. Operand src1 = node.GetSource(0);
  274. Operand rax = Gpr(X86Register.Rax, src1.Type);
  275. Operand rdx = Gpr(X86Register.Rdx, src1.Type);
  276. nodes.AddBefore(node, Operation(Instruction.Copy, rax, src1));
  277. node.SetSource(0, rax);
  278. nodes.AddAfter(node, Operation(Instruction.Copy, dest, rdx));
  279. node.SetDestinations(new Operand[] { rdx, rax });
  280. break;
  281. }
  282. case Instruction.RotateRight:
  283. case Instruction.ShiftLeft:
  284. case Instruction.ShiftRightSI:
  285. case Instruction.ShiftRightUI:
  286. {
  287. // The shift register is always implied to be CL (low 8-bits of RCX or ECX).
  288. if (node.GetSource(1).Kind == OperandKind.LocalVariable)
  289. {
  290. Operand rcx = Gpr(X86Register.Rcx, OperandType.I32);
  291. nodes.AddBefore(node, Operation(Instruction.Copy, rcx, node.GetSource(1)));
  292. node.SetSource(1, rcx);
  293. }
  294. break;
  295. }
  296. }
  297. }
  298. private static void HandleDestructiveRegCopy(IntrusiveList<Operation> nodes, Operation node)
  299. {
  300. if (node.Destination == default || node.SourcesCount == 0)
  301. {
  302. return;
  303. }
  304. Instruction inst = node.Instruction;
  305. Operand dest = node.Destination;
  306. Operand src1 = node.GetSource(0);
  307. // The multiply instruction (that maps to IMUL) is somewhat special, it has
  308. // a three operand form where the second source is a immediate value.
  309. bool threeOperandForm = inst == Instruction.Multiply && node.GetSource(1).Kind == OperandKind.Constant;
  310. if (IsSameOperandDestSrc1(node) && src1.Kind == OperandKind.LocalVariable && !threeOperandForm)
  311. {
  312. bool useNewLocal = false;
  313. for (int srcIndex = 1; srcIndex < node.SourcesCount; srcIndex++)
  314. {
  315. if (node.GetSource(srcIndex) == dest)
  316. {
  317. useNewLocal = true;
  318. break;
  319. }
  320. }
  321. if (useNewLocal)
  322. {
  323. // Dest is being used as some source already, we need to use a new
  324. // local to store the temporary value, otherwise the value on dest
  325. // local would be overwritten.
  326. Operand temp = Local(dest.Type);
  327. nodes.AddBefore(node, Operation(Instruction.Copy, temp, src1));
  328. node.SetSource(0, temp);
  329. nodes.AddAfter(node, Operation(Instruction.Copy, dest, temp));
  330. node.Destination = temp;
  331. }
  332. else
  333. {
  334. nodes.AddBefore(node, Operation(Instruction.Copy, dest, src1));
  335. node.SetSource(0, dest);
  336. }
  337. }
  338. else if (inst == Instruction.ConditionalSelect)
  339. {
  340. Operand src2 = node.GetSource(1);
  341. Operand src3 = node.GetSource(2);
  342. if (src1 == dest || src2 == dest)
  343. {
  344. Operand temp = Local(dest.Type);
  345. nodes.AddBefore(node, Operation(Instruction.Copy, temp, src3));
  346. node.SetSource(2, temp);
  347. nodes.AddAfter(node, Operation(Instruction.Copy, dest, temp));
  348. node.Destination = temp;
  349. }
  350. else
  351. {
  352. nodes.AddBefore(node, Operation(Instruction.Copy, dest, src3));
  353. node.SetSource(2, dest);
  354. }
  355. }
  356. }
  357. private static void HandleConvertToFPUI(IntrusiveList<Operation> nodes, Operation node)
  358. {
  359. // Unsigned integer to FP conversions are not supported on X86.
  360. // We need to turn them into signed integer to FP conversions, and
  361. // adjust the final result.
  362. Operand dest = node.Destination;
  363. Operand source = node.GetSource(0);
  364. Debug.Assert(source.Type.IsInteger(), $"Invalid source type \"{source.Type}\".");
  365. Operation currentNode = node;
  366. if (source.Type == OperandType.I32)
  367. {
  368. // For 32-bits integers, we can just zero-extend to 64-bits,
  369. // and then use the 64-bits signed conversion instructions.
  370. Operand zex = Local(OperandType.I64);
  371. node = nodes.AddAfter(node, Operation(Instruction.ZeroExtend32, zex, source));
  372. node = nodes.AddAfter(node, Operation(Instruction.ConvertToFP, dest, zex));
  373. }
  374. else /* if (source.Type == OperandType.I64) */
  375. {
  376. // For 64-bits integers, we need to do the following:
  377. // - Ensure that the integer has the most significant bit clear.
  378. // -- This can be done by shifting the value right by 1, that is, dividing by 2.
  379. // -- The least significant bit is lost in this case though.
  380. // - We can then convert the shifted value with a signed integer instruction.
  381. // - The result still needs to be corrected after that.
  382. // -- First, we need to multiply the result by 2, as we divided it by 2 before.
  383. // --- This can be done efficiently by adding the result to itself.
  384. // -- Then, we need to add the least significant bit that was shifted out.
  385. // --- We can convert the least significant bit to float, and add it to the result.
  386. Operand lsb = Local(OperandType.I64);
  387. Operand half = Local(OperandType.I64);
  388. Operand lsbF = Local(dest.Type);
  389. node = nodes.AddAfter(node, Operation(Instruction.Copy, lsb, source));
  390. node = nodes.AddAfter(node, Operation(Instruction.Copy, half, source));
  391. node = nodes.AddAfter(node, Operation(Instruction.BitwiseAnd, lsb, lsb, Const(1L)));
  392. node = nodes.AddAfter(node, Operation(Instruction.ShiftRightUI, half, half, Const(1)));
  393. node = nodes.AddAfter(node, Operation(Instruction.ConvertToFP, lsbF, lsb));
  394. node = nodes.AddAfter(node, Operation(Instruction.ConvertToFP, dest, half));
  395. node = nodes.AddAfter(node, Operation(Instruction.Add, dest, dest, dest));
  396. nodes.AddAfter(node, Operation(Instruction.Add, dest, dest, lsbF));
  397. }
  398. Delete(nodes, currentNode);
  399. }
  400. private static void HandleNegate(IntrusiveList<Operation> nodes, Operation node)
  401. {
  402. // There's no SSE FP negate instruction, so we need to transform that into
  403. // a XOR of the value to be negated with a mask with the highest bit set.
  404. // This also produces -0 for a negation of the value 0.
  405. Operand dest = node.Destination;
  406. Operand source = node.GetSource(0);
  407. Debug.Assert(dest.Type == OperandType.FP32 ||
  408. dest.Type == OperandType.FP64, $"Invalid destination type \"{dest.Type}\".");
  409. Operation currentNode = node;
  410. Operand res = Local(dest.Type);
  411. node = nodes.AddAfter(node, Operation(Instruction.VectorOne, res));
  412. if (dest.Type == OperandType.FP32)
  413. {
  414. node = nodes.AddAfter(node, Operation(Intrinsic.X86Pslld, res, res, Const(31)));
  415. }
  416. else /* if (dest.Type == OperandType.FP64) */
  417. {
  418. node = nodes.AddAfter(node, Operation(Intrinsic.X86Psllq, res, res, Const(63)));
  419. }
  420. node = nodes.AddAfter(node, Operation(Intrinsic.X86Xorps, res, res, source));
  421. nodes.AddAfter(node, Operation(Instruction.Copy, dest, res));
  422. Delete(nodes, currentNode);
  423. }
  424. private static void HandleVectorInsert8(IntrusiveList<Operation> nodes, Operation node)
  425. {
  426. // Handle vector insertion, when SSE 4.1 is not supported.
  427. Operand dest = node.Destination;
  428. Operand src1 = node.GetSource(0); // Vector
  429. Operand src2 = node.GetSource(1); // Value
  430. Operand src3 = node.GetSource(2); // Index
  431. Debug.Assert(src3.Kind == OperandKind.Constant);
  432. byte index = src3.AsByte();
  433. Debug.Assert(index < 16);
  434. Operation currentNode = node;
  435. Operand temp1 = Local(OperandType.I32);
  436. Operand temp2 = Local(OperandType.I32);
  437. node = nodes.AddAfter(node, Operation(Instruction.Copy, temp2, src2));
  438. Operation vextOp = Operation(Instruction.VectorExtract16, temp1, src1, Const(index >> 1));
  439. node = nodes.AddAfter(node, vextOp);
  440. if ((index & 1) != 0)
  441. {
  442. node = nodes.AddAfter(node, Operation(Instruction.ZeroExtend8, temp1, temp1));
  443. node = nodes.AddAfter(node, Operation(Instruction.ShiftLeft, temp2, temp2, Const(8)));
  444. node = nodes.AddAfter(node, Operation(Instruction.BitwiseOr, temp1, temp1, temp2));
  445. }
  446. else
  447. {
  448. node = nodes.AddAfter(node, Operation(Instruction.ZeroExtend8, temp2, temp2));
  449. node = nodes.AddAfter(node, Operation(Instruction.BitwiseAnd, temp1, temp1, Const(0xff00)));
  450. node = nodes.AddAfter(node, Operation(Instruction.BitwiseOr, temp1, temp1, temp2));
  451. }
  452. Operation vinsOp = Operation(Instruction.VectorInsert16, dest, src1, temp1, Const(index >> 1));
  453. nodes.AddAfter(node, vinsOp);
  454. Delete(nodes, currentNode);
  455. }
  456. private static void HandleCallWindowsAbi(IntrusiveList<Operation> nodes, StackAllocator stackAlloc, Operation node)
  457. {
  458. Operand dest = node.Destination;
  459. // Handle struct arguments.
  460. int retArgs = 0;
  461. int stackAllocOffset = 0;
  462. int AllocateOnStack(int size)
  463. {
  464. // We assume that the stack allocator is initially empty (TotalSize = 0).
  465. // Taking that into account, we can reuse the space allocated for other
  466. // calls by keeping track of our own allocated size (stackAllocOffset).
  467. // If the space allocated is not big enough, then we just expand it.
  468. int offset = stackAllocOffset;
  469. if (stackAllocOffset + size > stackAlloc.TotalSize)
  470. {
  471. stackAlloc.Allocate((stackAllocOffset + size) - stackAlloc.TotalSize);
  472. }
  473. stackAllocOffset += size;
  474. return offset;
  475. }
  476. Operand arg0Reg = default;
  477. if (dest != default && dest.Type == OperandType.V128)
  478. {
  479. int stackOffset = AllocateOnStack(dest.Type.GetSizeInBytes());
  480. arg0Reg = Gpr(CallingConvention.GetIntArgumentRegister(0), OperandType.I64);
  481. Operation allocOp = Operation(Instruction.StackAlloc, arg0Reg, Const(stackOffset));
  482. nodes.AddBefore(node, allocOp);
  483. retArgs = 1;
  484. }
  485. int argsCount = node.SourcesCount - 1;
  486. int maxArgs = CallingConvention.GetArgumentsOnRegsCount() - retArgs;
  487. if (argsCount > maxArgs)
  488. {
  489. argsCount = maxArgs;
  490. }
  491. Operand[] sources = new Operand[1 + retArgs + argsCount];
  492. sources[0] = node.GetSource(0);
  493. if (arg0Reg != default)
  494. {
  495. sources[1] = arg0Reg;
  496. }
  497. for (int index = 1; index < node.SourcesCount; index++)
  498. {
  499. Operand source = node.GetSource(index);
  500. if (source.Type == OperandType.V128)
  501. {
  502. Operand stackAddr = Local(OperandType.I64);
  503. int stackOffset = AllocateOnStack(source.Type.GetSizeInBytes());
  504. nodes.AddBefore(node, Operation(Instruction.StackAlloc, stackAddr, Const(stackOffset)));
  505. Operation storeOp = Operation(Instruction.Store, default, stackAddr, source);
  506. HandleConstantRegCopy(nodes, nodes.AddBefore(node, storeOp));
  507. node.SetSource(index, stackAddr);
  508. }
  509. }
  510. // Handle arguments passed on registers.
  511. for (int index = 0; index < argsCount; index++)
  512. {
  513. Operand source = node.GetSource(index + 1);
  514. Operand argReg;
  515. int argIndex = index + retArgs;
  516. if (source.Type.IsInteger())
  517. {
  518. argReg = Gpr(CallingConvention.GetIntArgumentRegister(argIndex), source.Type);
  519. }
  520. else
  521. {
  522. argReg = Xmm(CallingConvention.GetVecArgumentRegister(argIndex), source.Type);
  523. }
  524. Operation copyOp = Operation(Instruction.Copy, argReg, source);
  525. HandleConstantRegCopy(nodes, nodes.AddBefore(node, copyOp));
  526. sources[1 + retArgs + index] = argReg;
  527. }
  528. // The remaining arguments (those that are not passed on registers)
  529. // should be passed on the stack, we write them to the stack with "SpillArg".
  530. for (int index = argsCount; index < node.SourcesCount - 1; index++)
  531. {
  532. Operand source = node.GetSource(index + 1);
  533. Operand offset = Const((index + retArgs) * 8);
  534. Operation spillOp = Operation(Instruction.SpillArg, default, offset, source);
  535. HandleConstantRegCopy(nodes, nodes.AddBefore(node, spillOp));
  536. }
  537. if (dest != default)
  538. {
  539. if (dest.Type == OperandType.V128)
  540. {
  541. Operand retValueAddr = Local(OperandType.I64);
  542. nodes.AddBefore(node, Operation(Instruction.Copy, retValueAddr, arg0Reg));
  543. Operation loadOp = Operation(Instruction.Load, dest, retValueAddr);
  544. nodes.AddAfter(node, loadOp);
  545. node.Destination = default;
  546. }
  547. else
  548. {
  549. Operand retReg = dest.Type.IsInteger()
  550. ? Gpr(CallingConvention.GetIntReturnRegister(), dest.Type)
  551. : Xmm(CallingConvention.GetVecReturnRegister(), dest.Type);
  552. Operation copyOp = Operation(Instruction.Copy, dest, retReg);
  553. nodes.AddAfter(node, copyOp);
  554. node.Destination = retReg;
  555. }
  556. }
  557. node.SetSources(sources);
  558. }
  559. private static void HandleCallSystemVAbi(IntrusiveList<Operation> nodes, Operation node)
  560. {
  561. Operand dest = node.Destination;
  562. List<Operand> sources = new List<Operand>
  563. {
  564. node.GetSource(0)
  565. };
  566. int argsCount = node.SourcesCount - 1;
  567. int intMax = CallingConvention.GetIntArgumentsOnRegsCount();
  568. int vecMax = CallingConvention.GetVecArgumentsOnRegsCount();
  569. int intCount = 0;
  570. int vecCount = 0;
  571. int stackOffset = 0;
  572. for (int index = 0; index < argsCount; index++)
  573. {
  574. Operand source = node.GetSource(index + 1);
  575. bool passOnReg;
  576. if (source.Type.IsInteger())
  577. {
  578. passOnReg = intCount < intMax;
  579. }
  580. else if (source.Type == OperandType.V128)
  581. {
  582. passOnReg = intCount + 1 < intMax;
  583. }
  584. else
  585. {
  586. passOnReg = vecCount < vecMax;
  587. }
  588. if (source.Type == OperandType.V128 && passOnReg)
  589. {
  590. // V128 is a struct, we pass each half on a GPR if possible.
  591. Operand argReg = Gpr(CallingConvention.GetIntArgumentRegister(intCount++), OperandType.I64);
  592. Operand argReg2 = Gpr(CallingConvention.GetIntArgumentRegister(intCount++), OperandType.I64);
  593. nodes.AddBefore(node, Operation(Instruction.VectorExtract, argReg, source, Const(0)));
  594. nodes.AddBefore(node, Operation(Instruction.VectorExtract, argReg2, source, Const(1)));
  595. continue;
  596. }
  597. if (passOnReg)
  598. {
  599. Operand argReg = source.Type.IsInteger()
  600. ? Gpr(CallingConvention.GetIntArgumentRegister(intCount++), source.Type)
  601. : Xmm(CallingConvention.GetVecArgumentRegister(vecCount++), source.Type);
  602. Operation copyOp = Operation(Instruction.Copy, argReg, source);
  603. HandleConstantRegCopy(nodes, nodes.AddBefore(node, copyOp));
  604. sources.Add(argReg);
  605. }
  606. else
  607. {
  608. Operand offset = Const(stackOffset);
  609. Operation spillOp = Operation(Instruction.SpillArg, default, offset, source);
  610. HandleConstantRegCopy(nodes, nodes.AddBefore(node, spillOp));
  611. stackOffset += source.Type.GetSizeInBytes();
  612. }
  613. }
  614. if (dest != default)
  615. {
  616. if (dest.Type == OperandType.V128)
  617. {
  618. Operand retLReg = Gpr(CallingConvention.GetIntReturnRegister(), OperandType.I64);
  619. Operand retHReg = Gpr(CallingConvention.GetIntReturnRegisterHigh(), OperandType.I64);
  620. Operation operation = node;
  621. node = nodes.AddAfter(node, Operation(Instruction.VectorCreateScalar, dest, retLReg));
  622. nodes.AddAfter(node, Operation(Instruction.VectorInsert, dest, dest, retHReg, Const(1)));
  623. operation.Destination = default;
  624. }
  625. else
  626. {
  627. Operand retReg = dest.Type.IsInteger()
  628. ? Gpr(CallingConvention.GetIntReturnRegister(), dest.Type)
  629. : Xmm(CallingConvention.GetVecReturnRegister(), dest.Type);
  630. Operation copyOp = Operation(Instruction.Copy, dest, retReg);
  631. nodes.AddAfter(node, copyOp);
  632. node.Destination = retReg;
  633. }
  634. }
  635. node.SetSources(sources.ToArray());
  636. }
  637. private static void HandleTailcallSystemVAbi(IntrusiveList<Operation> nodes, StackAllocator stackAlloc, Operation node)
  638. {
  639. List<Operand> sources = new List<Operand>
  640. {
  641. node.GetSource(0)
  642. };
  643. int argsCount = node.SourcesCount - 1;
  644. int intMax = CallingConvention.GetIntArgumentsOnRegsCount();
  645. int vecMax = CallingConvention.GetVecArgumentsOnRegsCount();
  646. int intCount = 0;
  647. int vecCount = 0;
  648. // Handle arguments passed on registers.
  649. for (int index = 0; index < argsCount; index++)
  650. {
  651. Operand source = node.GetSource(1 + index);
  652. bool passOnReg;
  653. if (source.Type.IsInteger())
  654. {
  655. passOnReg = intCount + 1 < intMax;
  656. }
  657. else
  658. {
  659. passOnReg = vecCount < vecMax;
  660. }
  661. if (source.Type == OperandType.V128 && passOnReg)
  662. {
  663. // V128 is a struct, we pass each half on a GPR if possible.
  664. Operand argReg = Gpr(CallingConvention.GetIntArgumentRegister(intCount++), OperandType.I64);
  665. Operand argReg2 = Gpr(CallingConvention.GetIntArgumentRegister(intCount++), OperandType.I64);
  666. nodes.AddBefore(node, Operation(Instruction.VectorExtract, argReg, source, Const(0)));
  667. nodes.AddBefore(node, Operation(Instruction.VectorExtract, argReg2, source, Const(1)));
  668. continue;
  669. }
  670. if (passOnReg)
  671. {
  672. Operand argReg = source.Type.IsInteger()
  673. ? Gpr(CallingConvention.GetIntArgumentRegister(intCount++), source.Type)
  674. : Xmm(CallingConvention.GetVecArgumentRegister(vecCount++), source.Type);
  675. Operation copyOp = Operation(Instruction.Copy, argReg, source);
  676. HandleConstantRegCopy(nodes, nodes.AddBefore(node, copyOp));
  677. sources.Add(argReg);
  678. }
  679. else
  680. {
  681. throw new NotImplementedException("Spilling is not currently supported for tail calls. (too many arguments)");
  682. }
  683. }
  684. // The target address must be on the return registers, since we
  685. // don't return anything and it is guaranteed to not be a
  686. // callee saved register (which would be trashed on the epilogue).
  687. Operand retReg = Gpr(CallingConvention.GetIntReturnRegister(), OperandType.I64);
  688. Operation addrCopyOp = Operation(Instruction.Copy, retReg, node.GetSource(0));
  689. nodes.AddBefore(node, addrCopyOp);
  690. sources[0] = retReg;
  691. node.SetSources(sources.ToArray());
  692. }
  693. private static void HandleTailcallWindowsAbi(IntrusiveList<Operation> nodes, StackAllocator stackAlloc, Operation node)
  694. {
  695. int argsCount = node.SourcesCount - 1;
  696. int maxArgs = CallingConvention.GetArgumentsOnRegsCount();
  697. if (argsCount > maxArgs)
  698. {
  699. throw new NotImplementedException("Spilling is not currently supported for tail calls. (too many arguments)");
  700. }
  701. Operand[] sources = new Operand[1 + argsCount];
  702. // Handle arguments passed on registers.
  703. for (int index = 0; index < argsCount; index++)
  704. {
  705. Operand source = node.GetSource(1 + index);
  706. Operand argReg = source.Type.IsInteger()
  707. ? Gpr(CallingConvention.GetIntArgumentRegister(index), source.Type)
  708. : Xmm(CallingConvention.GetVecArgumentRegister(index), source.Type);
  709. Operation copyOp = Operation(Instruction.Copy, argReg, source);
  710. HandleConstantRegCopy(nodes, nodes.AddBefore(node, copyOp));
  711. sources[1 + index] = argReg;
  712. }
  713. // The target address must be on the return registers, since we
  714. // don't return anything and it is guaranteed to not be a
  715. // callee saved register (which would be trashed on the epilogue).
  716. Operand retReg = Gpr(CallingConvention.GetIntReturnRegister(), OperandType.I64);
  717. Operation addrCopyOp = Operation(Instruction.Copy, retReg, node.GetSource(0));
  718. nodes.AddBefore(node, addrCopyOp);
  719. sources[0] = retReg;
  720. node.SetSources(sources);
  721. }
  722. private static Operation HandleLoadArgumentWindowsAbi(
  723. CompilerContext cctx,
  724. IntrusiveList<Operation> nodes,
  725. Operand[] preservedArgs,
  726. Operation node)
  727. {
  728. Operand source = node.GetSource(0);
  729. Debug.Assert(source.Kind == OperandKind.Constant, "Non-constant LoadArgument source kind.");
  730. int retArgs = cctx.FuncReturnType == OperandType.V128 ? 1 : 0;
  731. int index = source.AsInt32() + retArgs;
  732. if (index < CallingConvention.GetArgumentsOnRegsCount())
  733. {
  734. Operand dest = node.Destination;
  735. if (preservedArgs[index] == default)
  736. {
  737. Operand argReg, pArg;
  738. if (dest.Type.IsInteger())
  739. {
  740. argReg = Gpr(CallingConvention.GetIntArgumentRegister(index), dest.Type);
  741. pArg = Local(dest.Type);
  742. }
  743. else if (dest.Type == OperandType.V128)
  744. {
  745. argReg = Gpr(CallingConvention.GetIntArgumentRegister(index), OperandType.I64);
  746. pArg = Local(OperandType.I64);
  747. }
  748. else
  749. {
  750. argReg = Xmm(CallingConvention.GetVecArgumentRegister(index), dest.Type);
  751. pArg = Local(dest.Type);
  752. }
  753. Operation copyOp = Operation(Instruction.Copy, pArg, argReg);
  754. cctx.Cfg.Entry.Operations.AddFirst(copyOp);
  755. preservedArgs[index] = pArg;
  756. }
  757. Operation argCopyOp = Operation(dest.Type == OperandType.V128
  758. ? Instruction.Load
  759. : Instruction.Copy, dest, preservedArgs[index]);
  760. Operation newNode = nodes.AddBefore(node, argCopyOp);
  761. Delete(nodes, node);
  762. return newNode;
  763. }
  764. else
  765. {
  766. // TODO: Pass on stack.
  767. return node;
  768. }
  769. }
  770. private static Operation HandleLoadArgumentSystemVAbi(
  771. CompilerContext cctx,
  772. IntrusiveList<Operation> nodes,
  773. Operand[] preservedArgs,
  774. Operation node)
  775. {
  776. Operand source = node.GetSource(0);
  777. Debug.Assert(source.Kind == OperandKind.Constant, "Non-constant LoadArgument source kind.");
  778. int index = source.AsInt32();
  779. int intCount = 0;
  780. int vecCount = 0;
  781. for (int cIndex = 0; cIndex < index; cIndex++)
  782. {
  783. OperandType argType = cctx.FuncArgTypes[cIndex];
  784. if (argType.IsInteger())
  785. {
  786. intCount++;
  787. }
  788. else if (argType == OperandType.V128)
  789. {
  790. intCount += 2;
  791. }
  792. else
  793. {
  794. vecCount++;
  795. }
  796. }
  797. bool passOnReg;
  798. if (source.Type.IsInteger())
  799. {
  800. passOnReg = intCount < CallingConvention.GetIntArgumentsOnRegsCount();
  801. }
  802. else if (source.Type == OperandType.V128)
  803. {
  804. passOnReg = intCount + 1 < CallingConvention.GetIntArgumentsOnRegsCount();
  805. }
  806. else
  807. {
  808. passOnReg = vecCount < CallingConvention.GetVecArgumentsOnRegsCount();
  809. }
  810. if (passOnReg)
  811. {
  812. Operand dest = node.Destination;
  813. if (preservedArgs[index] == default)
  814. {
  815. if (dest.Type == OperandType.V128)
  816. {
  817. // V128 is a struct, we pass each half on a GPR if possible.
  818. Operand pArg = Local(OperandType.V128);
  819. Operand argLReg = Gpr(CallingConvention.GetIntArgumentRegister(intCount), OperandType.I64);
  820. Operand argHReg = Gpr(CallingConvention.GetIntArgumentRegister(intCount + 1), OperandType.I64);
  821. Operation copyL = Operation(Instruction.VectorCreateScalar, pArg, argLReg);
  822. Operation copyH = Operation(Instruction.VectorInsert, pArg, pArg, argHReg, Const(1));
  823. cctx.Cfg.Entry.Operations.AddFirst(copyH);
  824. cctx.Cfg.Entry.Operations.AddFirst(copyL);
  825. preservedArgs[index] = pArg;
  826. }
  827. else
  828. {
  829. Operand pArg = Local(dest.Type);
  830. Operand argReg = dest.Type.IsInteger()
  831. ? Gpr(CallingConvention.GetIntArgumentRegister(intCount), dest.Type)
  832. : Xmm(CallingConvention.GetVecArgumentRegister(vecCount), dest.Type);
  833. Operation copyOp = Operation(Instruction.Copy, pArg, argReg);
  834. cctx.Cfg.Entry.Operations.AddFirst(copyOp);
  835. preservedArgs[index] = pArg;
  836. }
  837. }
  838. Operation argCopyOp = Operation(Instruction.Copy, dest, preservedArgs[index]);
  839. Operation newNode = nodes.AddBefore(node, argCopyOp);
  840. Delete(nodes, node);
  841. return newNode;
  842. }
  843. else
  844. {
  845. // TODO: Pass on stack.
  846. return node;
  847. }
  848. }
  849. private static void HandleReturnWindowsAbi(
  850. CompilerContext cctx,
  851. IntrusiveList<Operation> nodes,
  852. Operand[] preservedArgs,
  853. Operation node)
  854. {
  855. if (node.SourcesCount == 0)
  856. {
  857. return;
  858. }
  859. Operand source = node.GetSource(0);
  860. Operand retReg;
  861. if (source.Type.IsInteger())
  862. {
  863. retReg = Gpr(CallingConvention.GetIntReturnRegister(), source.Type);
  864. }
  865. else if (source.Type == OperandType.V128)
  866. {
  867. if (preservedArgs[0] == default)
  868. {
  869. Operand preservedArg = Local(OperandType.I64);
  870. Operand arg0 = Gpr(CallingConvention.GetIntArgumentRegister(0), OperandType.I64);
  871. Operation copyOp = Operation(Instruction.Copy, preservedArg, arg0);
  872. cctx.Cfg.Entry.Operations.AddFirst(copyOp);
  873. preservedArgs[0] = preservedArg;
  874. }
  875. retReg = preservedArgs[0];
  876. }
  877. else
  878. {
  879. retReg = Xmm(CallingConvention.GetVecReturnRegister(), source.Type);
  880. }
  881. if (source.Type == OperandType.V128)
  882. {
  883. Operation retStoreOp = Operation(Instruction.Store, default, retReg, source);
  884. nodes.AddBefore(node, retStoreOp);
  885. }
  886. else
  887. {
  888. Operation retCopyOp = Operation(Instruction.Copy, retReg, source);
  889. nodes.AddBefore(node, retCopyOp);
  890. }
  891. node.SetSources(Array.Empty<Operand>());
  892. }
  893. private static void HandleReturnSystemVAbi(IntrusiveList<Operation> nodes, Operation node)
  894. {
  895. if (node.SourcesCount == 0)
  896. {
  897. return;
  898. }
  899. Operand source = node.GetSource(0);
  900. if (source.Type == OperandType.V128)
  901. {
  902. Operand retLReg = Gpr(CallingConvention.GetIntReturnRegister(), OperandType.I64);
  903. Operand retHReg = Gpr(CallingConvention.GetIntReturnRegisterHigh(), OperandType.I64);
  904. nodes.AddBefore(node, Operation(Instruction.VectorExtract, retLReg, source, Const(0)));
  905. nodes.AddBefore(node, Operation(Instruction.VectorExtract, retHReg, source, Const(1)));
  906. }
  907. else
  908. {
  909. Operand retReg = source.Type.IsInteger()
  910. ? Gpr(CallingConvention.GetIntReturnRegister(), source.Type)
  911. : Xmm(CallingConvention.GetVecReturnRegister(), source.Type);
  912. Operation retCopyOp = Operation(Instruction.Copy, retReg, source);
  913. nodes.AddBefore(node, retCopyOp);
  914. }
  915. }
  916. private static Operand AddXmmCopy(IntrusiveList<Operation> nodes, Operation node, Operand source)
  917. {
  918. Operand temp = Local(source.Type);
  919. Operand intConst = AddCopy(nodes, node, GetIntConst(source));
  920. Operation copyOp = Operation(Instruction.VectorCreateScalar, temp, intConst);
  921. nodes.AddBefore(node, copyOp);
  922. return temp;
  923. }
  924. private static Operand AddCopy(IntrusiveList<Operation> nodes, Operation node, Operand source)
  925. {
  926. Operand temp = Local(source.Type);
  927. Operation copyOp = Operation(Instruction.Copy, temp, source);
  928. nodes.AddBefore(node, copyOp);
  929. return temp;
  930. }
  931. private static Operand GetIntConst(Operand value)
  932. {
  933. if (value.Type == OperandType.FP32)
  934. {
  935. return Const(value.AsInt32());
  936. }
  937. else if (value.Type == OperandType.FP64)
  938. {
  939. return Const(value.AsInt64());
  940. }
  941. return value;
  942. }
  943. private static void Delete(IntrusiveList<Operation> nodes, Operation node)
  944. {
  945. node.Destination = default;
  946. for (int index = 0; index < node.SourcesCount; index++)
  947. {
  948. node.SetSource(index, default);
  949. }
  950. nodes.Remove(node);
  951. }
  952. private static Operand Gpr(X86Register register, OperandType type)
  953. {
  954. return Register((int)register, RegisterType.Integer, type);
  955. }
  956. private static Operand Xmm(X86Register register, OperandType type)
  957. {
  958. return Register((int)register, RegisterType.Vector, type);
  959. }
  960. private static bool IsSameOperandDestSrc1(Operation operation)
  961. {
  962. switch (operation.Instruction)
  963. {
  964. case Instruction.Add:
  965. return !HardwareCapabilities.SupportsVexEncoding && !operation.Destination.Type.IsInteger();
  966. case Instruction.Multiply:
  967. case Instruction.Subtract:
  968. return !HardwareCapabilities.SupportsVexEncoding || operation.Destination.Type.IsInteger();
  969. case Instruction.BitwiseAnd:
  970. case Instruction.BitwiseExclusiveOr:
  971. case Instruction.BitwiseNot:
  972. case Instruction.BitwiseOr:
  973. case Instruction.ByteSwap:
  974. case Instruction.Negate:
  975. case Instruction.RotateRight:
  976. case Instruction.ShiftLeft:
  977. case Instruction.ShiftRightSI:
  978. case Instruction.ShiftRightUI:
  979. return true;
  980. case Instruction.Divide:
  981. return !HardwareCapabilities.SupportsVexEncoding && !operation.Destination.Type.IsInteger();
  982. case Instruction.VectorInsert:
  983. case Instruction.VectorInsert16:
  984. case Instruction.VectorInsert8:
  985. return !HardwareCapabilities.SupportsVexEncoding;
  986. case Instruction.Extended:
  987. return IsIntrinsicSameOperandDestSrc1(operation);
  988. }
  989. return IsVexSameOperandDestSrc1(operation);
  990. }
  991. private static bool IsIntrinsicSameOperandDestSrc1(Operation operation)
  992. {
  993. IntrinsicInfo info = IntrinsicTable.GetInfo(operation.Intrinsic);
  994. return info.Type == IntrinsicType.Crc32 || info.Type == IntrinsicType.Fma || IsVexSameOperandDestSrc1(operation);
  995. }
  996. private static bool IsVexSameOperandDestSrc1(Operation operation)
  997. {
  998. if (IsIntrinsic(operation.Instruction))
  999. {
  1000. bool isUnary = operation.SourcesCount < 2;
  1001. bool hasVecDest = operation.Destination != default && operation.Destination.Type == OperandType.V128;
  1002. return !HardwareCapabilities.SupportsVexEncoding && !isUnary && hasVecDest;
  1003. }
  1004. return false;
  1005. }
  1006. private static bool HasConstSrc1(Instruction inst)
  1007. {
  1008. switch (inst)
  1009. {
  1010. case Instruction.Copy:
  1011. case Instruction.LoadArgument:
  1012. case Instruction.Spill:
  1013. case Instruction.SpillArg:
  1014. return true;
  1015. }
  1016. return false;
  1017. }
  1018. private static bool HasConstSrc2(Instruction inst)
  1019. {
  1020. switch (inst)
  1021. {
  1022. case Instruction.Add:
  1023. case Instruction.BitwiseAnd:
  1024. case Instruction.BitwiseExclusiveOr:
  1025. case Instruction.BitwiseOr:
  1026. case Instruction.BranchIf:
  1027. case Instruction.Compare:
  1028. case Instruction.Multiply:
  1029. case Instruction.RotateRight:
  1030. case Instruction.ShiftLeft:
  1031. case Instruction.ShiftRightSI:
  1032. case Instruction.ShiftRightUI:
  1033. case Instruction.Store:
  1034. case Instruction.Store16:
  1035. case Instruction.Store8:
  1036. case Instruction.Subtract:
  1037. case Instruction.VectorExtract:
  1038. case Instruction.VectorExtract16:
  1039. case Instruction.VectorExtract8:
  1040. return true;
  1041. }
  1042. return false;
  1043. }
  1044. private static bool IsCommutative(Operation operation)
  1045. {
  1046. switch (operation.Instruction)
  1047. {
  1048. case Instruction.Add:
  1049. case Instruction.BitwiseAnd:
  1050. case Instruction.BitwiseExclusiveOr:
  1051. case Instruction.BitwiseOr:
  1052. case Instruction.Multiply:
  1053. return true;
  1054. case Instruction.BranchIf:
  1055. case Instruction.Compare:
  1056. {
  1057. Operand comp = operation.GetSource(2);
  1058. Debug.Assert(comp.Kind == OperandKind.Constant);
  1059. var compType = (Comparison)comp.AsInt32();
  1060. return compType == Comparison.Equal || compType == Comparison.NotEqual;
  1061. }
  1062. }
  1063. return false;
  1064. }
  1065. private static bool IsIntrinsic(Instruction inst)
  1066. {
  1067. return inst == Instruction.Extended;
  1068. }
  1069. private static bool IsXmmIntrinsic(Operation operation)
  1070. {
  1071. if (operation.Instruction != Instruction.Extended)
  1072. {
  1073. return false;
  1074. }
  1075. IntrinsicInfo info = IntrinsicTable.GetInfo(operation.Intrinsic);
  1076. return info.Type != IntrinsicType.Crc32;
  1077. }
  1078. }
  1079. }