PreAllocator.cs 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  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. node.SetSources(sources.ToArray());
  615. if (dest != default)
  616. {
  617. if (dest.Type == OperandType.V128)
  618. {
  619. Operand retLReg = Gpr(CallingConvention.GetIntReturnRegister(), OperandType.I64);
  620. Operand retHReg = Gpr(CallingConvention.GetIntReturnRegisterHigh(), OperandType.I64);
  621. Operation operation = node;
  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 = default;
  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. node.Destination = retReg;
  634. }
  635. }
  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. IntrinsicInfo info = IntrinsicTable.GetInfo(operation.Intrinsic);
  1001. bool hasVex = HardwareCapabilities.SupportsVexEncoding && Assembler.SupportsVexPrefix(info.Inst);
  1002. bool isUnary = operation.SourcesCount < 2;
  1003. bool hasVecDest = operation.Destination != default && operation.Destination.Type == OperandType.V128;
  1004. return !hasVex && !isUnary && hasVecDest;
  1005. }
  1006. return false;
  1007. }
  1008. private static bool HasConstSrc1(Instruction inst)
  1009. {
  1010. switch (inst)
  1011. {
  1012. case Instruction.Copy:
  1013. case Instruction.LoadArgument:
  1014. case Instruction.Spill:
  1015. case Instruction.SpillArg:
  1016. return true;
  1017. }
  1018. return false;
  1019. }
  1020. private static bool HasConstSrc2(Instruction inst)
  1021. {
  1022. switch (inst)
  1023. {
  1024. case Instruction.Add:
  1025. case Instruction.BitwiseAnd:
  1026. case Instruction.BitwiseExclusiveOr:
  1027. case Instruction.BitwiseOr:
  1028. case Instruction.BranchIf:
  1029. case Instruction.Compare:
  1030. case Instruction.Multiply:
  1031. case Instruction.RotateRight:
  1032. case Instruction.ShiftLeft:
  1033. case Instruction.ShiftRightSI:
  1034. case Instruction.ShiftRightUI:
  1035. case Instruction.Store:
  1036. case Instruction.Store16:
  1037. case Instruction.Store8:
  1038. case Instruction.Subtract:
  1039. case Instruction.VectorExtract:
  1040. case Instruction.VectorExtract16:
  1041. case Instruction.VectorExtract8:
  1042. return true;
  1043. }
  1044. return false;
  1045. }
  1046. private static bool IsCommutative(Operation operation)
  1047. {
  1048. switch (operation.Instruction)
  1049. {
  1050. case Instruction.Add:
  1051. case Instruction.BitwiseAnd:
  1052. case Instruction.BitwiseExclusiveOr:
  1053. case Instruction.BitwiseOr:
  1054. case Instruction.Multiply:
  1055. return true;
  1056. case Instruction.BranchIf:
  1057. case Instruction.Compare:
  1058. {
  1059. Operand comp = operation.GetSource(2);
  1060. Debug.Assert(comp.Kind == OperandKind.Constant);
  1061. var compType = (Comparison)comp.AsInt32();
  1062. return compType == Comparison.Equal || compType == Comparison.NotEqual;
  1063. }
  1064. }
  1065. return false;
  1066. }
  1067. private static bool IsIntrinsic(Instruction inst)
  1068. {
  1069. return inst == Instruction.Extended;
  1070. }
  1071. private static bool IsXmmIntrinsic(Operation operation)
  1072. {
  1073. if (operation.Instruction != Instruction.Extended)
  1074. {
  1075. return false;
  1076. }
  1077. IntrinsicInfo info = IntrinsicTable.GetInfo(operation.Intrinsic);
  1078. return info.Type != IntrinsicType.Crc32;
  1079. }
  1080. }
  1081. }