PreAllocator.cs 53 KB

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