PreAllocator.cs 53 KB

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