PreAllocator.cs 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  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) || 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(new Operand[0]);
  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 bool IsLongConst(Operand operand)
  847. {
  848. long value = operand.Type == OperandType.I32
  849. ? operand.AsInt32()
  850. : operand.AsInt64();
  851. return !ConstFitsOnS32(value);
  852. }
  853. private static bool ConstFitsOnS32(long value)
  854. {
  855. return value == (int)value;
  856. }
  857. private static void Delete(IntrusiveList<Node> nodes, Node node, Operation operation)
  858. {
  859. operation.Destination = null;
  860. for (int index = 0; index < operation.SourcesCount; index++)
  861. {
  862. operation.SetSource(index, null);
  863. }
  864. nodes.Remove(node);
  865. }
  866. private static Operand Gpr(X86Register register, OperandType type)
  867. {
  868. return Register((int)register, RegisterType.Integer, type);
  869. }
  870. private static Operand Xmm(X86Register register, OperandType type)
  871. {
  872. return Register((int)register, RegisterType.Vector, type);
  873. }
  874. private static bool IsSameOperandDestSrc1(Operation operation)
  875. {
  876. switch (operation.Instruction)
  877. {
  878. case Instruction.Add:
  879. case Instruction.Multiply:
  880. case Instruction.Subtract:
  881. return !HardwareCapabilities.SupportsVexEncoding || operation.Destination.Type.IsInteger();
  882. case Instruction.BitwiseAnd:
  883. case Instruction.BitwiseExclusiveOr:
  884. case Instruction.BitwiseNot:
  885. case Instruction.BitwiseOr:
  886. case Instruction.ByteSwap:
  887. case Instruction.Negate:
  888. case Instruction.RotateRight:
  889. case Instruction.ShiftLeft:
  890. case Instruction.ShiftRightSI:
  891. case Instruction.ShiftRightUI:
  892. return true;
  893. case Instruction.Divide:
  894. return !HardwareCapabilities.SupportsVexEncoding && !operation.Destination.Type.IsInteger();
  895. case Instruction.VectorInsert:
  896. case Instruction.VectorInsert16:
  897. case Instruction.VectorInsert8:
  898. return !HardwareCapabilities.SupportsVexEncoding;
  899. }
  900. return IsVexSameOperandDestSrc1(operation);
  901. }
  902. private static bool IsVexSameOperandDestSrc1(Operation operation)
  903. {
  904. if (IsIntrinsic(operation.Instruction))
  905. {
  906. bool isUnary = operation.SourcesCount < 2;
  907. bool hasVecDest = operation.Destination != null && operation.Destination.Type == OperandType.V128;
  908. return !HardwareCapabilities.SupportsVexEncoding && !isUnary && hasVecDest;
  909. }
  910. return false;
  911. }
  912. private static bool HasConstSrc1(Instruction inst)
  913. {
  914. switch (inst)
  915. {
  916. case Instruction.Copy:
  917. case Instruction.LoadArgument:
  918. case Instruction.Spill:
  919. case Instruction.SpillArg:
  920. return true;
  921. }
  922. return false;
  923. }
  924. private static bool HasConstSrc2(Instruction inst)
  925. {
  926. switch (inst)
  927. {
  928. case Instruction.Add:
  929. case Instruction.BitwiseAnd:
  930. case Instruction.BitwiseExclusiveOr:
  931. case Instruction.BitwiseOr:
  932. case Instruction.CompareEqual:
  933. case Instruction.CompareGreater:
  934. case Instruction.CompareGreaterOrEqual:
  935. case Instruction.CompareGreaterOrEqualUI:
  936. case Instruction.CompareGreaterUI:
  937. case Instruction.CompareLess:
  938. case Instruction.CompareLessOrEqual:
  939. case Instruction.CompareLessOrEqualUI:
  940. case Instruction.CompareLessUI:
  941. case Instruction.CompareNotEqual:
  942. case Instruction.Multiply:
  943. case Instruction.RotateRight:
  944. case Instruction.ShiftLeft:
  945. case Instruction.ShiftRightSI:
  946. case Instruction.ShiftRightUI:
  947. case Instruction.Subtract:
  948. case Instruction.VectorExtract:
  949. case Instruction.VectorExtract16:
  950. case Instruction.VectorExtract8:
  951. return true;
  952. }
  953. return false;
  954. }
  955. private static bool IsCommutative(Instruction inst)
  956. {
  957. switch (inst)
  958. {
  959. case Instruction.Add:
  960. case Instruction.BitwiseAnd:
  961. case Instruction.BitwiseExclusiveOr:
  962. case Instruction.BitwiseOr:
  963. case Instruction.CompareEqual:
  964. case Instruction.CompareNotEqual:
  965. case Instruction.Multiply:
  966. return true;
  967. }
  968. return false;
  969. }
  970. private static bool IsIntrinsic(Instruction inst)
  971. {
  972. return inst == Instruction.Extended;
  973. }
  974. }
  975. }