PreAllocator.cs 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  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. using LLNode = LinkedListNode<Node>;
  10. static class PreAllocator
  11. {
  12. public static void RunPass(CompilerContext cctx, StackAllocator stackAlloc, out int maxCallArgs)
  13. {
  14. maxCallArgs = -1;
  15. CallConvName callConv = CallingConvention.GetCurrentCallConv();
  16. Operand[] preservedArgs = new Operand[CallingConvention.GetArgumentsOnRegsCount()];
  17. foreach (BasicBlock block in cctx.Cfg.Blocks)
  18. {
  19. LLNode nextNode;
  20. for (LLNode node = block.Operations.First; node != null; node = nextNode)
  21. {
  22. nextNode = node.Next;
  23. if (!(node.Value is Operation operation))
  24. {
  25. continue;
  26. }
  27. HandleConstantCopy(node, operation);
  28. HandleSameDestSrc1Copy(node, operation);
  29. HandleFixedRegisterCopy(node, operation);
  30. switch (operation.Instruction)
  31. {
  32. case Instruction.Call:
  33. // Get the maximum number of arguments used on a call.
  34. // On windows, when a struct is returned from the call,
  35. // we also need to pass the pointer where the struct
  36. // should be written on the first argument.
  37. int argsCount = operation.SourcesCount - 1;
  38. if (operation.Destination != null && operation.Destination.Type == OperandType.V128)
  39. {
  40. argsCount++;
  41. }
  42. if (maxCallArgs < argsCount)
  43. {
  44. maxCallArgs = argsCount;
  45. }
  46. // Copy values to registers expected by the function
  47. // being called, as mandated by the ABI.
  48. if (callConv == CallConvName.Windows)
  49. {
  50. node = HandleCallWindowsAbi(stackAlloc, node, operation);
  51. }
  52. else /* if (callConv == CallConvName.SystemV) */
  53. {
  54. node = HandleCallSystemVAbi(node, operation);
  55. }
  56. break;
  57. case Instruction.ConvertToFPUI:
  58. HandleConvertToFPUI(node, operation);
  59. break;
  60. case Instruction.LoadArgument:
  61. if (callConv == CallConvName.Windows)
  62. {
  63. HandleLoadArgumentWindowsAbi(cctx, node, preservedArgs, operation);
  64. }
  65. else /* if (callConv == CallConvName.SystemV) */
  66. {
  67. HandleLoadArgumentSystemVAbi(cctx, node, preservedArgs, operation);
  68. }
  69. break;
  70. case Instruction.Negate:
  71. if (!operation.GetSource(0).Type.IsInteger())
  72. {
  73. node = HandleNegate(node, operation);
  74. }
  75. break;
  76. case Instruction.Return:
  77. if (callConv == CallConvName.Windows)
  78. {
  79. HandleReturnWindowsAbi(cctx, node, preservedArgs, operation);
  80. }
  81. else /* if (callConv == CallConvName.SystemV) */
  82. {
  83. HandleReturnSystemVAbi(node, operation);
  84. }
  85. break;
  86. case Instruction.VectorInsert8:
  87. if (!HardwareCapabilities.SupportsSse41)
  88. {
  89. node = HandleVectorInsert8(node, operation);
  90. }
  91. break;
  92. }
  93. }
  94. }
  95. }
  96. private static void HandleConstantCopy(LLNode node, Operation operation)
  97. {
  98. if (operation.SourcesCount == 0 || IsIntrinsic(operation.Instruction))
  99. {
  100. return;
  101. }
  102. Instruction inst = operation.Instruction;
  103. Operand src1 = operation.GetSource(0);
  104. Operand src2;
  105. if (src1.Kind == OperandKind.Constant)
  106. {
  107. if (!src1.Type.IsInteger())
  108. {
  109. // Handle non-integer types (FP32, FP64 and V128).
  110. // For instructions without an immediate operand, we do the following:
  111. // - Insert a copy with the constant value (as integer) to a GPR.
  112. // - Insert a copy from the GPR to a XMM register.
  113. // - Replace the constant use with the XMM register.
  114. src1 = AddXmmCopy(node, src1);
  115. operation.SetSource(0, src1);
  116. }
  117. else if (!HasConstSrc1(inst))
  118. {
  119. // Handle integer types.
  120. // Most ALU instructions accepts a 32-bits immediate on the second operand.
  121. // We need to ensure the following:
  122. // - If the constant is on operand 1, we need to move it.
  123. // -- But first, we try to swap operand 1 and 2 if the instruction is commutative.
  124. // -- Doing so may allow us to encode the constant as operand 2 and avoid a copy.
  125. // - If the constant is on operand 2, we check if the instruction supports it,
  126. // if not, we also add a copy. 64-bits constants are usually not supported.
  127. if (IsCommutative(inst))
  128. {
  129. src2 = operation.GetSource(1);
  130. Operand temp = src1;
  131. src1 = src2;
  132. src2 = temp;
  133. operation.SetSource(0, src1);
  134. operation.SetSource(1, src2);
  135. }
  136. if (src1.Kind == OperandKind.Constant)
  137. {
  138. src1 = AddCopy(node, src1);
  139. operation.SetSource(0, src1);
  140. }
  141. }
  142. }
  143. if (operation.SourcesCount < 2)
  144. {
  145. return;
  146. }
  147. src2 = operation.GetSource(1);
  148. if (src2.Kind == OperandKind.Constant)
  149. {
  150. if (!src2.Type.IsInteger())
  151. {
  152. src2 = AddXmmCopy(node, src2);
  153. operation.SetSource(1, src2);
  154. }
  155. else if (!HasConstSrc2(inst) || IsLongConst(src2))
  156. {
  157. src2 = AddCopy(node, src2);
  158. operation.SetSource(1, src2);
  159. }
  160. }
  161. }
  162. private static LLNode HandleFixedRegisterCopy(LLNode node, Operation operation)
  163. {
  164. Operand dest = operation.Destination;
  165. LinkedList<Node> nodes = node.List;
  166. switch (operation.Instruction)
  167. {
  168. case Instruction.CompareAndSwap128:
  169. {
  170. // Handle the many restrictions of the compare and exchange (16 bytes) instruction:
  171. // - The expected value should be in RDX:RAX.
  172. // - The new value to be written should be in RCX:RBX.
  173. // - The value at the memory location is loaded to RDX:RAX.
  174. void SplitOperand(Operand source, Operand lr, Operand hr)
  175. {
  176. nodes.AddBefore(node, new Operation(Instruction.VectorExtract, lr, source, Const(0)));
  177. nodes.AddBefore(node, new Operation(Instruction.VectorExtract, hr, source, Const(1)));
  178. }
  179. Operand rax = Gpr(X86Register.Rax, OperandType.I64);
  180. Operand rbx = Gpr(X86Register.Rbx, OperandType.I64);
  181. Operand rcx = Gpr(X86Register.Rcx, OperandType.I64);
  182. Operand rdx = Gpr(X86Register.Rdx, OperandType.I64);
  183. SplitOperand(operation.GetSource(1), rax, rdx);
  184. SplitOperand(operation.GetSource(2), rbx, rcx);
  185. node = nodes.AddAfter(node, new Operation(Instruction.VectorCreateScalar, dest, rax));
  186. node = nodes.AddAfter(node, new Operation(Instruction.VectorInsert, dest, dest, rdx, Const(1)));
  187. operation.SetDestinations(new Operand[] { rdx, rax });
  188. operation.SetSources(new Operand[] { operation.GetSource(0), rdx, rax, rcx, rbx });
  189. break;
  190. }
  191. case Instruction.CpuId:
  192. {
  193. // Handle the many restrictions of the CPU Id instruction:
  194. // - EAX controls the information returned by this instruction.
  195. // - When EAX is 1, feature information is returned.
  196. // - The information is written to registers EAX, EBX, ECX and EDX.
  197. Debug.Assert(dest.Type == OperandType.I64);
  198. Operand eax = Gpr(X86Register.Rax, OperandType.I32);
  199. Operand ebx = Gpr(X86Register.Rbx, OperandType.I32);
  200. Operand ecx = Gpr(X86Register.Rcx, OperandType.I32);
  201. Operand edx = Gpr(X86Register.Rdx, OperandType.I32);
  202. // Value 0x01 = Version, family and feature information.
  203. nodes.AddBefore(node, new Operation(Instruction.Copy, eax, Const(1)));
  204. // Copy results to the destination register.
  205. // The values are split into 2 32-bits registers, we merge them
  206. // into a single 64-bits register.
  207. Operand rcx = Gpr(X86Register.Rcx, OperandType.I64);
  208. node = nodes.AddAfter(node, new Operation(Instruction.ZeroExtend32, dest, edx));
  209. node = nodes.AddAfter(node, new Operation(Instruction.ShiftLeft, dest, dest, Const(32)));
  210. node = nodes.AddAfter(node, new Operation(Instruction.BitwiseOr, dest, dest, rcx));
  211. operation.SetDestinations(new Operand[] { eax, ebx, ecx, edx });
  212. operation.SetSources(new Operand[] { eax });
  213. break;
  214. }
  215. case Instruction.Divide:
  216. case Instruction.DivideUI:
  217. {
  218. // Handle the many restrictions of the division instructions:
  219. // - The dividend is always in RDX:RAX.
  220. // - The result is always in RAX.
  221. // - Additionally it also writes the remainder in RDX.
  222. if (dest.Type.IsInteger())
  223. {
  224. Operand src1 = operation.GetSource(0);
  225. Operand rax = Gpr(X86Register.Rax, src1.Type);
  226. Operand rdx = Gpr(X86Register.Rdx, src1.Type);
  227. nodes.AddBefore(node, new Operation(Instruction.Copy, rax, src1));
  228. nodes.AddBefore(node, new Operation(Instruction.Clobber, rdx));
  229. node = nodes.AddAfter(node, new Operation(Instruction.Copy, dest, rax));
  230. operation.SetDestinations(new Operand[] { rdx, rax });
  231. operation.SetSources(new Operand[] { rdx, rax, operation.GetSource(1) });
  232. operation.Destination = rax;
  233. }
  234. break;
  235. }
  236. case Instruction.Extended:
  237. {
  238. IntrinsicOperation intrinOp = (IntrinsicOperation)operation;
  239. // PBLENDVB last operand is always implied to be XMM0 when VEX is not supported.
  240. if (intrinOp.Intrinsic == Intrinsic.X86Pblendvb && !HardwareCapabilities.SupportsVexEncoding)
  241. {
  242. Operand xmm0 = Xmm(X86Register.Xmm0, OperandType.V128);
  243. nodes.AddBefore(node, new Operation(Instruction.Copy, xmm0, operation.GetSource(2)));
  244. operation.SetSource(2, xmm0);
  245. }
  246. break;
  247. }
  248. case Instruction.Multiply64HighSI:
  249. case Instruction.Multiply64HighUI:
  250. {
  251. // Handle the many restrictions of the i64 * i64 = i128 multiply instructions:
  252. // - The multiplicand is always in RAX.
  253. // - The lower 64-bits of the result is always in RAX.
  254. // - The higher 64-bits of the result is always in RDX.
  255. Operand src1 = operation.GetSource(0);
  256. Operand rax = Gpr(X86Register.Rax, src1.Type);
  257. Operand rdx = Gpr(X86Register.Rdx, src1.Type);
  258. nodes.AddBefore(node, new Operation(Instruction.Copy, rax, src1));
  259. operation.SetSource(0, rax);
  260. node = nodes.AddAfter(node, new Operation(Instruction.Copy, dest, rdx));
  261. operation.SetDestinations(new Operand[] { rdx, rax });
  262. break;
  263. }
  264. case Instruction.RotateRight:
  265. case Instruction.ShiftLeft:
  266. case Instruction.ShiftRightSI:
  267. case Instruction.ShiftRightUI:
  268. {
  269. // The shift register is always implied to be CL (low 8-bits of RCX or ECX).
  270. if (operation.GetSource(1).Kind == OperandKind.LocalVariable)
  271. {
  272. Operand rcx = Gpr(X86Register.Rcx, OperandType.I32);
  273. nodes.AddBefore(node, new Operation(Instruction.Copy, rcx, operation.GetSource(1)));
  274. operation.SetSource(1, rcx);
  275. }
  276. break;
  277. }
  278. }
  279. return node;
  280. }
  281. private static LLNode HandleSameDestSrc1Copy(LLNode node, Operation operation)
  282. {
  283. if (operation.Destination == null || operation.SourcesCount == 0)
  284. {
  285. return node;
  286. }
  287. Instruction inst = operation.Instruction;
  288. Operand dest = operation.Destination;
  289. Operand src1 = operation.GetSource(0);
  290. LinkedList<Node> nodes = node.List;
  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 LLNode HandleConvertToFPUI(LLNode 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. LinkedList<Node> nodes = node.List;
  351. LLNode currentNode = node;
  352. if (source.Type == OperandType.I32)
  353. {
  354. // For 32-bits integers, we can just zero-extend to 64-bits,
  355. // and then use the 64-bits signed conversion instructions.
  356. Operand zex = Local(OperandType.I64);
  357. node = nodes.AddAfter(node, new Operation(Instruction.ZeroExtend32, zex, source));
  358. node = nodes.AddAfter(node, new Operation(Instruction.ConvertToFP, dest, zex));
  359. }
  360. else /* if (source.Type == OperandType.I64) */
  361. {
  362. // For 64-bits integers, we need to do the following:
  363. // - Ensure that the integer has the most significant bit clear.
  364. // -- This can be done by shifting the value right by 1, that is, dividing by 2.
  365. // -- The least significant bit is lost in this case though.
  366. // - We can then convert the shifted value with a signed integer instruction.
  367. // - The result still needs to be corrected after that.
  368. // -- First, we need to multiply the result by 2, as we divided it by 2 before.
  369. // --- This can be done efficiently by adding the result to itself.
  370. // -- Then, we need to add the least significant bit that was shifted out.
  371. // --- We can convert the least significant bit to float, and add it to the result.
  372. Operand lsb = Local(OperandType.I64);
  373. Operand half = Local(OperandType.I64);
  374. Operand lsbF = Local(dest.Type);
  375. node = nodes.AddAfter(node, new Operation(Instruction.Copy, lsb, source));
  376. node = nodes.AddAfter(node, new Operation(Instruction.Copy, half, source));
  377. node = nodes.AddAfter(node, new Operation(Instruction.BitwiseAnd, lsb, lsb, Const(1L)));
  378. node = nodes.AddAfter(node, new Operation(Instruction.ShiftRightUI, half, half, Const(1)));
  379. node = nodes.AddAfter(node, new Operation(Instruction.ConvertToFP, lsbF, lsb));
  380. node = nodes.AddAfter(node, new Operation(Instruction.ConvertToFP, dest, half));
  381. node = nodes.AddAfter(node, new Operation(Instruction.Add, dest, dest, dest));
  382. node = nodes.AddAfter(node, new Operation(Instruction.Add, dest, dest, lsbF));
  383. }
  384. Delete(currentNode, operation);
  385. return node;
  386. }
  387. private static LLNode HandleNegate(LLNode node, Operation operation)
  388. {
  389. // There's no SSE FP negate instruction, so we need to transform that into
  390. // a XOR of the value to be negated with a mask with the highest bit set.
  391. // This also produces -0 for a negation of the value 0.
  392. Operand dest = operation.Destination;
  393. Operand source = operation.GetSource(0);
  394. Debug.Assert(dest.Type == OperandType.FP32 ||
  395. dest.Type == OperandType.FP64, $"Invalid destination type \"{dest.Type}\".");
  396. LinkedList<Node> nodes = node.List;
  397. LLNode currentNode = node;
  398. Operand res = Local(dest.Type);
  399. node = nodes.AddAfter(node, new Operation(Instruction.VectorOne, res));
  400. if (dest.Type == OperandType.FP32)
  401. {
  402. node = nodes.AddAfter(node, new IntrinsicOperation(Intrinsic.X86Pslld, res, res, Const(31)));
  403. }
  404. else /* if (dest.Type == OperandType.FP64) */
  405. {
  406. node = nodes.AddAfter(node, new IntrinsicOperation(Intrinsic.X86Psllq, res, res, Const(63)));
  407. }
  408. node = nodes.AddAfter(node, new IntrinsicOperation(Intrinsic.X86Xorps, res, res, source));
  409. node = nodes.AddAfter(node, new Operation(Instruction.Copy, dest, res));
  410. Delete(currentNode, operation);
  411. return node;
  412. }
  413. private static LLNode HandleVectorInsert8(LLNode node, Operation operation)
  414. {
  415. // Handle vector insertion, when SSE 4.1 is not supported.
  416. Operand dest = operation.Destination;
  417. Operand src1 = operation.GetSource(0); // Vector
  418. Operand src2 = operation.GetSource(1); // Value
  419. Operand src3 = operation.GetSource(2); // Index
  420. Debug.Assert(src3.Kind == OperandKind.Constant);
  421. byte index = src3.AsByte();
  422. Debug.Assert(index < 16);
  423. LinkedList<Node> nodes = node.List;
  424. LLNode currentNode = node;
  425. Operand temp1 = Local(OperandType.I32);
  426. Operand temp2 = Local(OperandType.I32);
  427. node = nodes.AddAfter(node, new Operation(Instruction.Copy, temp2, src2));
  428. Operation vextOp = new Operation(Instruction.VectorExtract16, temp1, src1, Const(index >> 1));
  429. node = nodes.AddAfter(node, vextOp);
  430. if ((index & 1) != 0)
  431. {
  432. node = nodes.AddAfter(node, new Operation(Instruction.ZeroExtend8, temp1, temp1));
  433. node = nodes.AddAfter(node, new Operation(Instruction.ShiftLeft, temp2, temp2, Const(8)));
  434. node = nodes.AddAfter(node, new Operation(Instruction.BitwiseOr, temp1, temp1, temp2));
  435. }
  436. else
  437. {
  438. node = nodes.AddAfter(node, new Operation(Instruction.ZeroExtend8, temp2, temp2));
  439. node = nodes.AddAfter(node, new Operation(Instruction.BitwiseAnd, temp1, temp1, Const(0xff00)));
  440. node = nodes.AddAfter(node, new Operation(Instruction.BitwiseOr, temp1, temp1, temp2));
  441. }
  442. Operation vinsOp = new Operation(Instruction.VectorInsert16, dest, src1, temp1, Const(index >> 1));
  443. node = nodes.AddAfter(node, vinsOp);
  444. Delete(currentNode, operation);
  445. return node;
  446. }
  447. private static LLNode HandleCallWindowsAbi(StackAllocator stackAlloc, LLNode node, Operation operation)
  448. {
  449. Operand dest = operation.Destination;
  450. LinkedList<Node> nodes = node.List;
  451. // Handle struct arguments.
  452. int retArgs = 0;
  453. int stackAllocOffset = 0;
  454. int AllocateOnStack(int size)
  455. {
  456. // We assume that the stack allocator is initially empty (TotalSize = 0).
  457. // Taking that into account, we can reuse the space allocated for other
  458. // calls by keeping track of our own allocated size (stackAllocOffset).
  459. // If the space allocated is not big enough, then we just expand it.
  460. int offset = stackAllocOffset;
  461. if (stackAllocOffset + size > stackAlloc.TotalSize)
  462. {
  463. stackAlloc.Allocate((stackAllocOffset + size) - stackAlloc.TotalSize);
  464. }
  465. stackAllocOffset += size;
  466. return offset;
  467. }
  468. Operand arg0Reg = null;
  469. if (dest != null && dest.Type == OperandType.V128)
  470. {
  471. int stackOffset = AllocateOnStack(dest.Type.GetSizeInBytes());
  472. arg0Reg = Gpr(CallingConvention.GetIntArgumentRegister(0), OperandType.I64);
  473. Operation allocOp = new Operation(Instruction.StackAlloc, arg0Reg, Const(stackOffset));
  474. nodes.AddBefore(node, allocOp);
  475. retArgs = 1;
  476. }
  477. int argsCount = operation.SourcesCount - 1;
  478. int maxArgs = CallingConvention.GetArgumentsOnRegsCount() - retArgs;
  479. if (argsCount > maxArgs)
  480. {
  481. argsCount = maxArgs;
  482. }
  483. Operand[] sources = new Operand[1 + retArgs + argsCount];
  484. sources[0] = operation.GetSource(0);
  485. if (arg0Reg != null)
  486. {
  487. sources[1] = arg0Reg;
  488. }
  489. for (int index = 1; index < operation.SourcesCount; index++)
  490. {
  491. Operand source = operation.GetSource(index);
  492. if (source.Type == OperandType.V128)
  493. {
  494. Operand stackAddr = Local(OperandType.I64);
  495. int stackOffset = AllocateOnStack(source.Type.GetSizeInBytes());
  496. nodes.AddBefore(node, new Operation(Instruction.StackAlloc, stackAddr, Const(stackOffset)));
  497. Operation storeOp = new Operation(Instruction.Store, null, stackAddr, source);
  498. HandleConstantCopy(nodes.AddBefore(node, storeOp), storeOp);
  499. operation.SetSource(index, stackAddr);
  500. }
  501. }
  502. // Handle arguments passed on registers.
  503. for (int index = 0; index < argsCount; index++)
  504. {
  505. Operand source = operation.GetSource(index + 1);
  506. Operand argReg;
  507. int argIndex = index + retArgs;
  508. if (source.Type.IsInteger())
  509. {
  510. argReg = Gpr(CallingConvention.GetIntArgumentRegister(argIndex), source.Type);
  511. }
  512. else
  513. {
  514. argReg = Xmm(CallingConvention.GetVecArgumentRegister(argIndex), source.Type);
  515. }
  516. Operation copyOp = new Operation(Instruction.Copy, argReg, source);
  517. HandleConstantCopy(nodes.AddBefore(node, copyOp), copyOp);
  518. sources[1 + retArgs + index] = argReg;
  519. }
  520. // The remaining arguments (those that are not passed on registers)
  521. // should be passed on the stack, we write them to the stack with "SpillArg".
  522. for (int index = argsCount; index < operation.SourcesCount - 1; index++)
  523. {
  524. Operand source = operation.GetSource(index + 1);
  525. Operand offset = new Operand((index + retArgs) * 8);
  526. Operation spillOp = new Operation(Instruction.SpillArg, null, offset, source);
  527. HandleConstantCopy(nodes.AddBefore(node, spillOp), spillOp);
  528. }
  529. if (dest != null)
  530. {
  531. if (dest.Type == OperandType.V128)
  532. {
  533. Operand retValueAddr = Local(OperandType.I64);
  534. nodes.AddBefore(node, new Operation(Instruction.Copy, retValueAddr, arg0Reg));
  535. Operation loadOp = new Operation(Instruction.Load, dest, retValueAddr);
  536. node = nodes.AddAfter(node, loadOp);
  537. operation.Destination = null;
  538. }
  539. else
  540. {
  541. Operand retReg = dest.Type.IsInteger()
  542. ? Gpr(CallingConvention.GetIntReturnRegister(), dest.Type)
  543. : Xmm(CallingConvention.GetVecReturnRegister(), dest.Type);
  544. Operation copyOp = new Operation(Instruction.Copy, dest, retReg);
  545. node = nodes.AddAfter(node, copyOp);
  546. operation.Destination = retReg;
  547. }
  548. }
  549. operation.SetSources(sources);
  550. return node;
  551. }
  552. private static LLNode HandleCallSystemVAbi(LLNode node, Operation operation)
  553. {
  554. Operand dest = operation.Destination;
  555. LinkedList<Node> nodes = node.List;
  556. List<Operand> sources = new List<Operand>();
  557. sources.Add(operation.GetSource(0));
  558. int argsCount = operation.SourcesCount - 1;
  559. int intMax = CallingConvention.GetIntArgumentsOnRegsCount();
  560. int vecMax = CallingConvention.GetVecArgumentsOnRegsCount();
  561. int intCount = 0;
  562. int vecCount = 0;
  563. int stackOffset = 0;
  564. for (int index = 0; index < argsCount; index++)
  565. {
  566. Operand source = operation.GetSource(index + 1);
  567. bool passOnReg;
  568. if (source.Type.IsInteger())
  569. {
  570. passOnReg = intCount < intMax;
  571. }
  572. else if (source.Type == OperandType.V128)
  573. {
  574. passOnReg = intCount + 1 < intMax;
  575. }
  576. else
  577. {
  578. passOnReg = vecCount < vecMax;
  579. }
  580. if (source.Type == OperandType.V128 && passOnReg)
  581. {
  582. // V128 is a struct, we pass each half on a GPR if possible.
  583. Operand argReg = Gpr(CallingConvention.GetIntArgumentRegister(intCount++), OperandType.I64);
  584. Operand argReg2 = Gpr(CallingConvention.GetIntArgumentRegister(intCount++), OperandType.I64);
  585. nodes.AddBefore(node, new Operation(Instruction.VectorExtract, argReg, source, Const(0)));
  586. nodes.AddBefore(node, new Operation(Instruction.VectorExtract, argReg2, source, Const(1)));
  587. continue;
  588. }
  589. if (passOnReg)
  590. {
  591. Operand argReg = source.Type.IsInteger()
  592. ? Gpr(CallingConvention.GetIntArgumentRegister(intCount++), source.Type)
  593. : Xmm(CallingConvention.GetVecArgumentRegister(vecCount++), source.Type);
  594. Operation copyOp = new Operation(Instruction.Copy, argReg, source);
  595. HandleConstantCopy(nodes.AddBefore(node, copyOp), copyOp);
  596. sources.Add(argReg);
  597. }
  598. else
  599. {
  600. Operand offset = new Operand(stackOffset);
  601. Operation spillOp = new Operation(Instruction.SpillArg, null, offset, source);
  602. HandleConstantCopy(nodes.AddBefore(node, spillOp), spillOp);
  603. stackOffset += source.Type.GetSizeInBytes();
  604. }
  605. }
  606. if (dest != null)
  607. {
  608. if (dest.Type == OperandType.V128)
  609. {
  610. Operand retLReg = Gpr(CallingConvention.GetIntReturnRegister(), OperandType.I64);
  611. Operand retHReg = Gpr(CallingConvention.GetIntReturnRegisterHigh(), OperandType.I64);
  612. node = nodes.AddAfter(node, new Operation(Instruction.VectorCreateScalar, dest, retLReg));
  613. node = nodes.AddAfter(node, new Operation(Instruction.VectorInsert, dest, dest, retHReg, Const(1)));
  614. operation.Destination = null;
  615. }
  616. else
  617. {
  618. Operand retReg = dest.Type.IsInteger()
  619. ? Gpr(CallingConvention.GetIntReturnRegister(), dest.Type)
  620. : Xmm(CallingConvention.GetVecReturnRegister(), dest.Type);
  621. Operation copyOp = new Operation(Instruction.Copy, dest, retReg);
  622. node = nodes.AddAfter(node, copyOp);
  623. operation.Destination = retReg;
  624. }
  625. }
  626. operation.SetSources(sources.ToArray());
  627. return node;
  628. }
  629. private static void HandleLoadArgumentWindowsAbi(
  630. CompilerContext cctx,
  631. LLNode node,
  632. Operand[] preservedArgs,
  633. Operation operation)
  634. {
  635. Operand source = operation.GetSource(0);
  636. Debug.Assert(source.Kind == OperandKind.Constant, "Non-constant LoadArgument source kind.");
  637. int retArgs = cctx.FuncReturnType == OperandType.V128 ? 1 : 0;
  638. int index = source.AsInt32() + retArgs;
  639. if (index < CallingConvention.GetArgumentsOnRegsCount())
  640. {
  641. Operand dest = operation.Destination;
  642. if (preservedArgs[index] == null)
  643. {
  644. Operand argReg, pArg;
  645. if (dest.Type.IsInteger())
  646. {
  647. argReg = Gpr(CallingConvention.GetIntArgumentRegister(index), dest.Type);
  648. pArg = Local(dest.Type);
  649. }
  650. else if (dest.Type == OperandType.V128)
  651. {
  652. argReg = Gpr(CallingConvention.GetIntArgumentRegister(index), OperandType.I64);
  653. pArg = Local(OperandType.I64);
  654. }
  655. else
  656. {
  657. argReg = Xmm(CallingConvention.GetVecArgumentRegister(index), dest.Type);
  658. pArg = Local(dest.Type);
  659. }
  660. Operation copyOp = new Operation(Instruction.Copy, pArg, argReg);
  661. cctx.Cfg.Entry.Operations.AddFirst(copyOp);
  662. preservedArgs[index] = pArg;
  663. }
  664. Operation argCopyOp = new Operation(dest.Type == OperandType.V128
  665. ? Instruction.Load
  666. : Instruction.Copy, dest, preservedArgs[index]);
  667. node.List.AddBefore(node, argCopyOp);
  668. Delete(node, operation);
  669. }
  670. else
  671. {
  672. // TODO: Pass on stack.
  673. }
  674. }
  675. private static void HandleLoadArgumentSystemVAbi(
  676. CompilerContext cctx,
  677. LLNode node,
  678. Operand[] preservedArgs,
  679. Operation operation)
  680. {
  681. Operand source = operation.GetSource(0);
  682. Debug.Assert(source.Kind == OperandKind.Constant, "Non-constant LoadArgument source kind.");
  683. int index = source.AsInt32();
  684. int intCount = 0;
  685. int vecCount = 0;
  686. for (int cIndex = 0; cIndex < index; cIndex++)
  687. {
  688. OperandType argType = cctx.FuncArgTypes[cIndex];
  689. if (argType.IsInteger())
  690. {
  691. intCount++;
  692. }
  693. else if (argType == OperandType.V128)
  694. {
  695. intCount += 2;
  696. }
  697. else
  698. {
  699. vecCount++;
  700. }
  701. }
  702. bool passOnReg;
  703. if (source.Type.IsInteger())
  704. {
  705. passOnReg = intCount < CallingConvention.GetIntArgumentsOnRegsCount();
  706. }
  707. else if (source.Type == OperandType.V128)
  708. {
  709. passOnReg = intCount + 1 < CallingConvention.GetIntArgumentsOnRegsCount();
  710. }
  711. else
  712. {
  713. passOnReg = vecCount < CallingConvention.GetVecArgumentsOnRegsCount();
  714. }
  715. if (passOnReg)
  716. {
  717. Operand dest = operation.Destination;
  718. if (preservedArgs[index] == null)
  719. {
  720. if (dest.Type == OperandType.V128)
  721. {
  722. // V128 is a struct, we pass each half on a GPR if possible.
  723. Operand pArg = Local(OperandType.V128);
  724. Operand argLReg = Gpr(CallingConvention.GetIntArgumentRegister(intCount), OperandType.I64);
  725. Operand argHReg = Gpr(CallingConvention.GetIntArgumentRegister(intCount + 1), OperandType.I64);
  726. Operation copyL = new Operation(Instruction.VectorCreateScalar, pArg, argLReg);
  727. Operation copyH = new Operation(Instruction.VectorInsert, pArg, pArg, argHReg, Const(1));
  728. cctx.Cfg.Entry.Operations.AddFirst(copyH);
  729. cctx.Cfg.Entry.Operations.AddFirst(copyL);
  730. preservedArgs[index] = pArg;
  731. }
  732. else
  733. {
  734. Operand pArg = Local(dest.Type);
  735. Operand argReg = dest.Type.IsInteger()
  736. ? Gpr(CallingConvention.GetIntArgumentRegister(intCount), dest.Type)
  737. : Xmm(CallingConvention.GetVecArgumentRegister(vecCount), dest.Type);
  738. Operation copyOp = new Operation(Instruction.Copy, pArg, argReg);
  739. cctx.Cfg.Entry.Operations.AddFirst(copyOp);
  740. preservedArgs[index] = pArg;
  741. }
  742. }
  743. Operation argCopyOp = new Operation(Instruction.Copy, dest, preservedArgs[index]);
  744. node.List.AddBefore(node, argCopyOp);
  745. Delete(node, operation);
  746. }
  747. else
  748. {
  749. // TODO: Pass on stack.
  750. }
  751. }
  752. private static void HandleReturnWindowsAbi(
  753. CompilerContext cctx,
  754. LLNode 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. node.List.AddBefore(node, retStoreOp);
  788. }
  789. else
  790. {
  791. Operation retCopyOp = new Operation(Instruction.Copy, retReg, source);
  792. node.List.AddBefore(node, retCopyOp);
  793. }
  794. operation.SetSources(new Operand[0]);
  795. }
  796. private static void HandleReturnSystemVAbi(LLNode 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. node.List.AddBefore(node, new Operation(Instruction.VectorExtract, retLReg, source, Const(0)));
  808. node.List.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. node.List.AddBefore(node, retCopyOp);
  817. }
  818. }
  819. private static Operand AddXmmCopy(LLNode node, Operand source)
  820. {
  821. Operand temp = Local(source.Type);
  822. Operand intConst = AddCopy(node, GetIntConst(source));
  823. Operation copyOp = new Operation(Instruction.VectorCreateScalar, temp, intConst);
  824. node.List.AddBefore(node, copyOp);
  825. return temp;
  826. }
  827. private static Operand AddCopy(LLNode node, Operand source)
  828. {
  829. Operand temp = Local(source.Type);
  830. Operation copyOp = new Operation(Instruction.Copy, temp, source);
  831. node.List.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(LLNode 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. node.List.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. }