PreAllocator.cs 53 KB

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