PreAllocator.cs 53 KB

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