PreAllocator.cs 47 KB

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