PreAllocator.cs 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  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. // BLENDVPD, BLENDVPS, PBLENDVB last operand is always implied to be XMM0 when VEX is not supported.
  240. if ((intrinOp.Intrinsic == Intrinsic.X86Blendvpd ||
  241. intrinOp.Intrinsic == Intrinsic.X86Blendvps ||
  242. intrinOp.Intrinsic == Intrinsic.X86Pblendvb) &&
  243. !HardwareCapabilities.SupportsVexEncoding)
  244. {
  245. Operand xmm0 = Xmm(X86Register.Xmm0, OperandType.V128);
  246. nodes.AddBefore(node, new Operation(Instruction.Copy, xmm0, operation.GetSource(2)));
  247. operation.SetSource(2, xmm0);
  248. }
  249. break;
  250. }
  251. case Instruction.Multiply64HighSI:
  252. case Instruction.Multiply64HighUI:
  253. {
  254. // Handle the many restrictions of the i64 * i64 = i128 multiply instructions:
  255. // - The multiplicand is always in RAX.
  256. // - The lower 64-bits of the result is always in RAX.
  257. // - The higher 64-bits of the result is always in RDX.
  258. Operand src1 = operation.GetSource(0);
  259. Operand rax = Gpr(X86Register.Rax, src1.Type);
  260. Operand rdx = Gpr(X86Register.Rdx, src1.Type);
  261. nodes.AddBefore(node, new Operation(Instruction.Copy, rax, src1));
  262. operation.SetSource(0, rax);
  263. node = nodes.AddAfter(node, new Operation(Instruction.Copy, dest, rdx));
  264. operation.SetDestinations(new Operand[] { rdx, rax });
  265. break;
  266. }
  267. case Instruction.RotateRight:
  268. case Instruction.ShiftLeft:
  269. case Instruction.ShiftRightSI:
  270. case Instruction.ShiftRightUI:
  271. {
  272. // The shift register is always implied to be CL (low 8-bits of RCX or ECX).
  273. if (operation.GetSource(1).Kind == OperandKind.LocalVariable)
  274. {
  275. Operand rcx = Gpr(X86Register.Rcx, OperandType.I32);
  276. nodes.AddBefore(node, new Operation(Instruction.Copy, rcx, operation.GetSource(1)));
  277. operation.SetSource(1, rcx);
  278. }
  279. break;
  280. }
  281. }
  282. return node;
  283. }
  284. private static LLNode HandleSameDestSrc1Copy(LLNode node, Operation operation)
  285. {
  286. if (operation.Destination == null || operation.SourcesCount == 0)
  287. {
  288. return node;
  289. }
  290. Instruction inst = operation.Instruction;
  291. Operand dest = operation.Destination;
  292. Operand src1 = operation.GetSource(0);
  293. LinkedList<Node> nodes = node.List;
  294. // The multiply instruction (that maps to IMUL) is somewhat special, it has
  295. // a three operand form where the second source is a immediate value.
  296. bool threeOperandForm = inst == Instruction.Multiply && operation.GetSource(1).Kind == OperandKind.Constant;
  297. if (IsSameOperandDestSrc1(operation) && src1.Kind == OperandKind.LocalVariable && !threeOperandForm)
  298. {
  299. bool useNewLocal = false;
  300. for (int srcIndex = 1; srcIndex < operation.SourcesCount; srcIndex++)
  301. {
  302. if (operation.GetSource(srcIndex) == dest)
  303. {
  304. useNewLocal = true;
  305. break;
  306. }
  307. }
  308. if (useNewLocal)
  309. {
  310. // Dest is being used as some source already, we need to use a new
  311. // local to store the temporary value, otherwise the value on dest
  312. // local would be overwritten.
  313. Operand temp = Local(dest.Type);
  314. nodes.AddBefore(node, new Operation(Instruction.Copy, temp, src1));
  315. operation.SetSource(0, temp);
  316. node = nodes.AddAfter(node, new Operation(Instruction.Copy, dest, temp));
  317. operation.Destination = temp;
  318. }
  319. else
  320. {
  321. nodes.AddBefore(node, new Operation(Instruction.Copy, dest, src1));
  322. operation.SetSource(0, dest);
  323. }
  324. }
  325. else if (inst == Instruction.ConditionalSelect)
  326. {
  327. Operand src2 = operation.GetSource(1);
  328. Operand src3 = operation.GetSource(2);
  329. if (src1 == dest || src2 == dest)
  330. {
  331. Operand temp = Local(dest.Type);
  332. nodes.AddBefore(node, new Operation(Instruction.Copy, temp, src3));
  333. operation.SetSource(2, temp);
  334. node = nodes.AddAfter(node, new Operation(Instruction.Copy, dest, temp));
  335. operation.Destination = temp;
  336. }
  337. else
  338. {
  339. nodes.AddBefore(node, new Operation(Instruction.Copy, dest, src3));
  340. operation.SetSource(2, dest);
  341. }
  342. }
  343. return node;
  344. }
  345. private static LLNode HandleConvertToFPUI(LLNode node, Operation operation)
  346. {
  347. // Unsigned integer to FP conversions are not supported on X86.
  348. // We need to turn them into signed integer to FP conversions, and
  349. // adjust the final result.
  350. Operand dest = operation.Destination;
  351. Operand source = operation.GetSource(0);
  352. Debug.Assert(source.Type.IsInteger(), $"Invalid source type \"{source.Type}\".");
  353. LinkedList<Node> nodes = node.List;
  354. LLNode currentNode = node;
  355. if (source.Type == OperandType.I32)
  356. {
  357. // For 32-bits integers, we can just zero-extend to 64-bits,
  358. // and then use the 64-bits signed conversion instructions.
  359. Operand zex = Local(OperandType.I64);
  360. node = nodes.AddAfter(node, new Operation(Instruction.ZeroExtend32, zex, source));
  361. node = nodes.AddAfter(node, new Operation(Instruction.ConvertToFP, dest, zex));
  362. }
  363. else /* if (source.Type == OperandType.I64) */
  364. {
  365. // For 64-bits integers, we need to do the following:
  366. // - Ensure that the integer has the most significant bit clear.
  367. // -- This can be done by shifting the value right by 1, that is, dividing by 2.
  368. // -- The least significant bit is lost in this case though.
  369. // - We can then convert the shifted value with a signed integer instruction.
  370. // - The result still needs to be corrected after that.
  371. // -- First, we need to multiply the result by 2, as we divided it by 2 before.
  372. // --- This can be done efficiently by adding the result to itself.
  373. // -- Then, we need to add the least significant bit that was shifted out.
  374. // --- We can convert the least significant bit to float, and add it to the result.
  375. Operand lsb = Local(OperandType.I64);
  376. Operand half = Local(OperandType.I64);
  377. Operand lsbF = Local(dest.Type);
  378. node = nodes.AddAfter(node, new Operation(Instruction.Copy, lsb, source));
  379. node = nodes.AddAfter(node, new Operation(Instruction.Copy, half, source));
  380. node = nodes.AddAfter(node, new Operation(Instruction.BitwiseAnd, lsb, lsb, Const(1L)));
  381. node = nodes.AddAfter(node, new Operation(Instruction.ShiftRightUI, half, half, Const(1)));
  382. node = nodes.AddAfter(node, new Operation(Instruction.ConvertToFP, lsbF, lsb));
  383. node = nodes.AddAfter(node, new Operation(Instruction.ConvertToFP, dest, half));
  384. node = nodes.AddAfter(node, new Operation(Instruction.Add, dest, dest, dest));
  385. node = nodes.AddAfter(node, new Operation(Instruction.Add, dest, dest, lsbF));
  386. }
  387. Delete(currentNode, operation);
  388. return node;
  389. }
  390. private static LLNode HandleNegate(LLNode node, Operation operation)
  391. {
  392. // There's no SSE FP negate instruction, so we need to transform that into
  393. // a XOR of the value to be negated with a mask with the highest bit set.
  394. // This also produces -0 for a negation of the value 0.
  395. Operand dest = operation.Destination;
  396. Operand source = operation.GetSource(0);
  397. Debug.Assert(dest.Type == OperandType.FP32 ||
  398. dest.Type == OperandType.FP64, $"Invalid destination type \"{dest.Type}\".");
  399. LinkedList<Node> nodes = node.List;
  400. LLNode currentNode = node;
  401. Operand res = Local(dest.Type);
  402. node = nodes.AddAfter(node, new Operation(Instruction.VectorOne, res));
  403. if (dest.Type == OperandType.FP32)
  404. {
  405. node = nodes.AddAfter(node, new IntrinsicOperation(Intrinsic.X86Pslld, res, res, Const(31)));
  406. }
  407. else /* if (dest.Type == OperandType.FP64) */
  408. {
  409. node = nodes.AddAfter(node, new IntrinsicOperation(Intrinsic.X86Psllq, res, res, Const(63)));
  410. }
  411. node = nodes.AddAfter(node, new IntrinsicOperation(Intrinsic.X86Xorps, res, res, source));
  412. node = nodes.AddAfter(node, new Operation(Instruction.Copy, dest, res));
  413. Delete(currentNode, operation);
  414. return node;
  415. }
  416. private static LLNode HandleVectorInsert8(LLNode node, Operation operation)
  417. {
  418. // Handle vector insertion, when SSE 4.1 is not supported.
  419. Operand dest = operation.Destination;
  420. Operand src1 = operation.GetSource(0); // Vector
  421. Operand src2 = operation.GetSource(1); // Value
  422. Operand src3 = operation.GetSource(2); // Index
  423. Debug.Assert(src3.Kind == OperandKind.Constant);
  424. byte index = src3.AsByte();
  425. Debug.Assert(index < 16);
  426. LinkedList<Node> nodes = node.List;
  427. LLNode currentNode = node;
  428. Operand temp1 = Local(OperandType.I32);
  429. Operand temp2 = Local(OperandType.I32);
  430. node = nodes.AddAfter(node, new Operation(Instruction.Copy, temp2, src2));
  431. Operation vextOp = new Operation(Instruction.VectorExtract16, temp1, src1, Const(index >> 1));
  432. node = nodes.AddAfter(node, vextOp);
  433. if ((index & 1) != 0)
  434. {
  435. node = nodes.AddAfter(node, new Operation(Instruction.ZeroExtend8, temp1, temp1));
  436. node = nodes.AddAfter(node, new Operation(Instruction.ShiftLeft, temp2, temp2, Const(8)));
  437. node = nodes.AddAfter(node, new Operation(Instruction.BitwiseOr, temp1, temp1, temp2));
  438. }
  439. else
  440. {
  441. node = nodes.AddAfter(node, new Operation(Instruction.ZeroExtend8, temp2, temp2));
  442. node = nodes.AddAfter(node, new Operation(Instruction.BitwiseAnd, temp1, temp1, Const(0xff00)));
  443. node = nodes.AddAfter(node, new Operation(Instruction.BitwiseOr, temp1, temp1, temp2));
  444. }
  445. Operation vinsOp = new Operation(Instruction.VectorInsert16, dest, src1, temp1, Const(index >> 1));
  446. node = nodes.AddAfter(node, vinsOp);
  447. Delete(currentNode, operation);
  448. return node;
  449. }
  450. private static LLNode HandleCallWindowsAbi(StackAllocator stackAlloc, LLNode node, Operation operation)
  451. {
  452. Operand dest = operation.Destination;
  453. LinkedList<Node> nodes = node.List;
  454. // Handle struct arguments.
  455. int retArgs = 0;
  456. int stackAllocOffset = 0;
  457. int AllocateOnStack(int size)
  458. {
  459. // We assume that the stack allocator is initially empty (TotalSize = 0).
  460. // Taking that into account, we can reuse the space allocated for other
  461. // calls by keeping track of our own allocated size (stackAllocOffset).
  462. // If the space allocated is not big enough, then we just expand it.
  463. int offset = stackAllocOffset;
  464. if (stackAllocOffset + size > stackAlloc.TotalSize)
  465. {
  466. stackAlloc.Allocate((stackAllocOffset + size) - stackAlloc.TotalSize);
  467. }
  468. stackAllocOffset += size;
  469. return offset;
  470. }
  471. Operand arg0Reg = null;
  472. if (dest != null && dest.Type == OperandType.V128)
  473. {
  474. int stackOffset = AllocateOnStack(dest.Type.GetSizeInBytes());
  475. arg0Reg = Gpr(CallingConvention.GetIntArgumentRegister(0), OperandType.I64);
  476. Operation allocOp = new Operation(Instruction.StackAlloc, arg0Reg, Const(stackOffset));
  477. nodes.AddBefore(node, allocOp);
  478. retArgs = 1;
  479. }
  480. int argsCount = operation.SourcesCount - 1;
  481. int maxArgs = CallingConvention.GetArgumentsOnRegsCount() - retArgs;
  482. if (argsCount > maxArgs)
  483. {
  484. argsCount = maxArgs;
  485. }
  486. Operand[] sources = new Operand[1 + retArgs + argsCount];
  487. sources[0] = operation.GetSource(0);
  488. if (arg0Reg != null)
  489. {
  490. sources[1] = arg0Reg;
  491. }
  492. for (int index = 1; index < operation.SourcesCount; index++)
  493. {
  494. Operand source = operation.GetSource(index);
  495. if (source.Type == OperandType.V128)
  496. {
  497. Operand stackAddr = Local(OperandType.I64);
  498. int stackOffset = AllocateOnStack(source.Type.GetSizeInBytes());
  499. nodes.AddBefore(node, new Operation(Instruction.StackAlloc, stackAddr, Const(stackOffset)));
  500. Operation storeOp = new Operation(Instruction.Store, null, stackAddr, source);
  501. HandleConstantCopy(nodes.AddBefore(node, storeOp), storeOp);
  502. operation.SetSource(index, stackAddr);
  503. }
  504. }
  505. // Handle arguments passed on registers.
  506. for (int index = 0; index < argsCount; index++)
  507. {
  508. Operand source = operation.GetSource(index + 1);
  509. Operand argReg;
  510. int argIndex = index + retArgs;
  511. if (source.Type.IsInteger())
  512. {
  513. argReg = Gpr(CallingConvention.GetIntArgumentRegister(argIndex), source.Type);
  514. }
  515. else
  516. {
  517. argReg = Xmm(CallingConvention.GetVecArgumentRegister(argIndex), source.Type);
  518. }
  519. Operation copyOp = new Operation(Instruction.Copy, argReg, source);
  520. HandleConstantCopy(nodes.AddBefore(node, copyOp), copyOp);
  521. sources[1 + retArgs + index] = argReg;
  522. }
  523. // The remaining arguments (those that are not passed on registers)
  524. // should be passed on the stack, we write them to the stack with "SpillArg".
  525. for (int index = argsCount; index < operation.SourcesCount - 1; index++)
  526. {
  527. Operand source = operation.GetSource(index + 1);
  528. Operand offset = new Operand((index + retArgs) * 8);
  529. Operation spillOp = new Operation(Instruction.SpillArg, null, offset, source);
  530. HandleConstantCopy(nodes.AddBefore(node, spillOp), spillOp);
  531. }
  532. if (dest != null)
  533. {
  534. if (dest.Type == OperandType.V128)
  535. {
  536. Operand retValueAddr = Local(OperandType.I64);
  537. nodes.AddBefore(node, new Operation(Instruction.Copy, retValueAddr, arg0Reg));
  538. Operation loadOp = new Operation(Instruction.Load, dest, retValueAddr);
  539. node = nodes.AddAfter(node, loadOp);
  540. operation.Destination = null;
  541. }
  542. else
  543. {
  544. Operand retReg = dest.Type.IsInteger()
  545. ? Gpr(CallingConvention.GetIntReturnRegister(), dest.Type)
  546. : Xmm(CallingConvention.GetVecReturnRegister(), dest.Type);
  547. Operation copyOp = new Operation(Instruction.Copy, dest, retReg);
  548. node = nodes.AddAfter(node, copyOp);
  549. operation.Destination = retReg;
  550. }
  551. }
  552. operation.SetSources(sources);
  553. return node;
  554. }
  555. private static LLNode HandleCallSystemVAbi(LLNode node, Operation operation)
  556. {
  557. Operand dest = operation.Destination;
  558. LinkedList<Node> nodes = node.List;
  559. List<Operand> sources = new List<Operand>();
  560. sources.Add(operation.GetSource(0));
  561. int argsCount = operation.SourcesCount - 1;
  562. int intMax = CallingConvention.GetIntArgumentsOnRegsCount();
  563. int vecMax = CallingConvention.GetVecArgumentsOnRegsCount();
  564. int intCount = 0;
  565. int vecCount = 0;
  566. int stackOffset = 0;
  567. for (int index = 0; index < argsCount; index++)
  568. {
  569. Operand source = operation.GetSource(index + 1);
  570. bool passOnReg;
  571. if (source.Type.IsInteger())
  572. {
  573. passOnReg = intCount < intMax;
  574. }
  575. else if (source.Type == OperandType.V128)
  576. {
  577. passOnReg = intCount + 1 < intMax;
  578. }
  579. else
  580. {
  581. passOnReg = vecCount < vecMax;
  582. }
  583. if (source.Type == OperandType.V128 && passOnReg)
  584. {
  585. // V128 is a struct, we pass each half on a GPR if possible.
  586. Operand argReg = Gpr(CallingConvention.GetIntArgumentRegister(intCount++), OperandType.I64);
  587. Operand argReg2 = Gpr(CallingConvention.GetIntArgumentRegister(intCount++), OperandType.I64);
  588. nodes.AddBefore(node, new Operation(Instruction.VectorExtract, argReg, source, Const(0)));
  589. nodes.AddBefore(node, new Operation(Instruction.VectorExtract, argReg2, source, Const(1)));
  590. continue;
  591. }
  592. if (passOnReg)
  593. {
  594. Operand argReg = source.Type.IsInteger()
  595. ? Gpr(CallingConvention.GetIntArgumentRegister(intCount++), source.Type)
  596. : Xmm(CallingConvention.GetVecArgumentRegister(vecCount++), source.Type);
  597. Operation copyOp = new Operation(Instruction.Copy, argReg, source);
  598. HandleConstantCopy(nodes.AddBefore(node, copyOp), copyOp);
  599. sources.Add(argReg);
  600. }
  601. else
  602. {
  603. Operand offset = new Operand(stackOffset);
  604. Operation spillOp = new Operation(Instruction.SpillArg, null, offset, source);
  605. HandleConstantCopy(nodes.AddBefore(node, spillOp), spillOp);
  606. stackOffset += source.Type.GetSizeInBytes();
  607. }
  608. }
  609. if (dest != null)
  610. {
  611. if (dest.Type == OperandType.V128)
  612. {
  613. Operand retLReg = Gpr(CallingConvention.GetIntReturnRegister(), OperandType.I64);
  614. Operand retHReg = Gpr(CallingConvention.GetIntReturnRegisterHigh(), OperandType.I64);
  615. node = nodes.AddAfter(node, new Operation(Instruction.VectorCreateScalar, dest, retLReg));
  616. node = nodes.AddAfter(node, new Operation(Instruction.VectorInsert, dest, dest, retHReg, Const(1)));
  617. operation.Destination = null;
  618. }
  619. else
  620. {
  621. Operand retReg = dest.Type.IsInteger()
  622. ? Gpr(CallingConvention.GetIntReturnRegister(), dest.Type)
  623. : Xmm(CallingConvention.GetVecReturnRegister(), dest.Type);
  624. Operation copyOp = new Operation(Instruction.Copy, dest, retReg);
  625. node = nodes.AddAfter(node, copyOp);
  626. operation.Destination = retReg;
  627. }
  628. }
  629. operation.SetSources(sources.ToArray());
  630. return node;
  631. }
  632. private static void HandleLoadArgumentWindowsAbi(
  633. CompilerContext cctx,
  634. LLNode node,
  635. Operand[] preservedArgs,
  636. Operation operation)
  637. {
  638. Operand source = operation.GetSource(0);
  639. Debug.Assert(source.Kind == OperandKind.Constant, "Non-constant LoadArgument source kind.");
  640. int retArgs = cctx.FuncReturnType == OperandType.V128 ? 1 : 0;
  641. int index = source.AsInt32() + retArgs;
  642. if (index < CallingConvention.GetArgumentsOnRegsCount())
  643. {
  644. Operand dest = operation.Destination;
  645. if (preservedArgs[index] == null)
  646. {
  647. Operand argReg, pArg;
  648. if (dest.Type.IsInteger())
  649. {
  650. argReg = Gpr(CallingConvention.GetIntArgumentRegister(index), dest.Type);
  651. pArg = Local(dest.Type);
  652. }
  653. else if (dest.Type == OperandType.V128)
  654. {
  655. argReg = Gpr(CallingConvention.GetIntArgumentRegister(index), OperandType.I64);
  656. pArg = Local(OperandType.I64);
  657. }
  658. else
  659. {
  660. argReg = Xmm(CallingConvention.GetVecArgumentRegister(index), dest.Type);
  661. pArg = Local(dest.Type);
  662. }
  663. Operation copyOp = new Operation(Instruction.Copy, pArg, argReg);
  664. cctx.Cfg.Entry.Operations.AddFirst(copyOp);
  665. preservedArgs[index] = pArg;
  666. }
  667. Operation argCopyOp = new Operation(dest.Type == OperandType.V128
  668. ? Instruction.Load
  669. : Instruction.Copy, dest, preservedArgs[index]);
  670. node.List.AddBefore(node, argCopyOp);
  671. Delete(node, operation);
  672. }
  673. else
  674. {
  675. // TODO: Pass on stack.
  676. }
  677. }
  678. private static void HandleLoadArgumentSystemVAbi(
  679. CompilerContext cctx,
  680. LLNode node,
  681. Operand[] preservedArgs,
  682. Operation operation)
  683. {
  684. Operand source = operation.GetSource(0);
  685. Debug.Assert(source.Kind == OperandKind.Constant, "Non-constant LoadArgument source kind.");
  686. int index = source.AsInt32();
  687. int intCount = 0;
  688. int vecCount = 0;
  689. for (int cIndex = 0; cIndex < index; cIndex++)
  690. {
  691. OperandType argType = cctx.FuncArgTypes[cIndex];
  692. if (argType.IsInteger())
  693. {
  694. intCount++;
  695. }
  696. else if (argType == OperandType.V128)
  697. {
  698. intCount += 2;
  699. }
  700. else
  701. {
  702. vecCount++;
  703. }
  704. }
  705. bool passOnReg;
  706. if (source.Type.IsInteger())
  707. {
  708. passOnReg = intCount < CallingConvention.GetIntArgumentsOnRegsCount();
  709. }
  710. else if (source.Type == OperandType.V128)
  711. {
  712. passOnReg = intCount + 1 < CallingConvention.GetIntArgumentsOnRegsCount();
  713. }
  714. else
  715. {
  716. passOnReg = vecCount < CallingConvention.GetVecArgumentsOnRegsCount();
  717. }
  718. if (passOnReg)
  719. {
  720. Operand dest = operation.Destination;
  721. if (preservedArgs[index] == null)
  722. {
  723. if (dest.Type == OperandType.V128)
  724. {
  725. // V128 is a struct, we pass each half on a GPR if possible.
  726. Operand pArg = Local(OperandType.V128);
  727. Operand argLReg = Gpr(CallingConvention.GetIntArgumentRegister(intCount), OperandType.I64);
  728. Operand argHReg = Gpr(CallingConvention.GetIntArgumentRegister(intCount + 1), OperandType.I64);
  729. Operation copyL = new Operation(Instruction.VectorCreateScalar, pArg, argLReg);
  730. Operation copyH = new Operation(Instruction.VectorInsert, pArg, pArg, argHReg, Const(1));
  731. cctx.Cfg.Entry.Operations.AddFirst(copyH);
  732. cctx.Cfg.Entry.Operations.AddFirst(copyL);
  733. preservedArgs[index] = pArg;
  734. }
  735. else
  736. {
  737. Operand pArg = Local(dest.Type);
  738. Operand argReg = dest.Type.IsInteger()
  739. ? Gpr(CallingConvention.GetIntArgumentRegister(intCount), dest.Type)
  740. : Xmm(CallingConvention.GetVecArgumentRegister(vecCount), dest.Type);
  741. Operation copyOp = new Operation(Instruction.Copy, pArg, argReg);
  742. cctx.Cfg.Entry.Operations.AddFirst(copyOp);
  743. preservedArgs[index] = pArg;
  744. }
  745. }
  746. Operation argCopyOp = new Operation(Instruction.Copy, dest, preservedArgs[index]);
  747. node.List.AddBefore(node, argCopyOp);
  748. Delete(node, operation);
  749. }
  750. else
  751. {
  752. // TODO: Pass on stack.
  753. }
  754. }
  755. private static void HandleReturnWindowsAbi(
  756. CompilerContext cctx,
  757. LLNode node,
  758. Operand[] preservedArgs,
  759. Operation operation)
  760. {
  761. if (operation.SourcesCount == 0)
  762. {
  763. return;
  764. }
  765. Operand source = operation.GetSource(0);
  766. Operand retReg;
  767. if (source.Type.IsInteger())
  768. {
  769. retReg = Gpr(CallingConvention.GetIntReturnRegister(), source.Type);
  770. }
  771. else if (source.Type == OperandType.V128)
  772. {
  773. if (preservedArgs[0] == null)
  774. {
  775. Operand preservedArg = Local(OperandType.I64);
  776. Operand arg0 = Gpr(CallingConvention.GetIntArgumentRegister(0), OperandType.I64);
  777. Operation copyOp = new Operation(Instruction.Copy, preservedArg, arg0);
  778. cctx.Cfg.Entry.Operations.AddFirst(copyOp);
  779. preservedArgs[0] = preservedArg;
  780. }
  781. retReg = preservedArgs[0];
  782. }
  783. else
  784. {
  785. retReg = Xmm(CallingConvention.GetVecReturnRegister(), source.Type);
  786. }
  787. if (source.Type == OperandType.V128)
  788. {
  789. Operation retStoreOp = new Operation(Instruction.Store, null, retReg, source);
  790. node.List.AddBefore(node, retStoreOp);
  791. }
  792. else
  793. {
  794. Operation retCopyOp = new Operation(Instruction.Copy, retReg, source);
  795. node.List.AddBefore(node, retCopyOp);
  796. }
  797. operation.SetSources(new Operand[0]);
  798. }
  799. private static void HandleReturnSystemVAbi(LLNode node, Operation operation)
  800. {
  801. if (operation.SourcesCount == 0)
  802. {
  803. return;
  804. }
  805. Operand source = operation.GetSource(0);
  806. if (source.Type == OperandType.V128)
  807. {
  808. Operand retLReg = Gpr(CallingConvention.GetIntReturnRegister(), OperandType.I64);
  809. Operand retHReg = Gpr(CallingConvention.GetIntReturnRegisterHigh(), OperandType.I64);
  810. node.List.AddBefore(node, new Operation(Instruction.VectorExtract, retLReg, source, Const(0)));
  811. node.List.AddBefore(node, new Operation(Instruction.VectorExtract, retHReg, source, Const(1)));
  812. }
  813. else
  814. {
  815. Operand retReg = source.Type.IsInteger()
  816. ? Gpr(CallingConvention.GetIntReturnRegister(), source.Type)
  817. : Xmm(CallingConvention.GetVecReturnRegister(), source.Type);
  818. Operation retCopyOp = new Operation(Instruction.Copy, retReg, source);
  819. node.List.AddBefore(node, retCopyOp);
  820. }
  821. }
  822. private static Operand AddXmmCopy(LLNode node, Operand source)
  823. {
  824. Operand temp = Local(source.Type);
  825. Operand intConst = AddCopy(node, GetIntConst(source));
  826. Operation copyOp = new Operation(Instruction.VectorCreateScalar, temp, intConst);
  827. node.List.AddBefore(node, copyOp);
  828. return temp;
  829. }
  830. private static Operand AddCopy(LLNode node, Operand source)
  831. {
  832. Operand temp = Local(source.Type);
  833. Operation copyOp = new Operation(Instruction.Copy, temp, source);
  834. node.List.AddBefore(node, copyOp);
  835. return temp;
  836. }
  837. private static Operand GetIntConst(Operand value)
  838. {
  839. if (value.Type == OperandType.FP32)
  840. {
  841. return Const(value.AsInt32());
  842. }
  843. else if (value.Type == OperandType.FP64)
  844. {
  845. return Const(value.AsInt64());
  846. }
  847. return value;
  848. }
  849. private static bool IsLongConst(Operand operand)
  850. {
  851. long value = operand.Type == OperandType.I32
  852. ? operand.AsInt32()
  853. : operand.AsInt64();
  854. return !ConstFitsOnS32(value);
  855. }
  856. private static bool ConstFitsOnS32(long value)
  857. {
  858. return value == (int)value;
  859. }
  860. private static void Delete(LLNode node, Operation operation)
  861. {
  862. operation.Destination = null;
  863. for (int index = 0; index < operation.SourcesCount; index++)
  864. {
  865. operation.SetSource(index, null);
  866. }
  867. node.List.Remove(node);
  868. }
  869. private static Operand Gpr(X86Register register, OperandType type)
  870. {
  871. return Register((int)register, RegisterType.Integer, type);
  872. }
  873. private static Operand Xmm(X86Register register, OperandType type)
  874. {
  875. return Register((int)register, RegisterType.Vector, type);
  876. }
  877. private static bool IsSameOperandDestSrc1(Operation operation)
  878. {
  879. switch (operation.Instruction)
  880. {
  881. case Instruction.Add:
  882. case Instruction.Multiply:
  883. case Instruction.Subtract:
  884. return !HardwareCapabilities.SupportsVexEncoding || operation.Destination.Type.IsInteger();
  885. case Instruction.BitwiseAnd:
  886. case Instruction.BitwiseExclusiveOr:
  887. case Instruction.BitwiseNot:
  888. case Instruction.BitwiseOr:
  889. case Instruction.ByteSwap:
  890. case Instruction.Negate:
  891. case Instruction.RotateRight:
  892. case Instruction.ShiftLeft:
  893. case Instruction.ShiftRightSI:
  894. case Instruction.ShiftRightUI:
  895. return true;
  896. case Instruction.Divide:
  897. return !HardwareCapabilities.SupportsVexEncoding && !operation.Destination.Type.IsInteger();
  898. case Instruction.VectorInsert:
  899. case Instruction.VectorInsert16:
  900. case Instruction.VectorInsert8:
  901. return !HardwareCapabilities.SupportsVexEncoding;
  902. }
  903. return IsVexSameOperandDestSrc1(operation);
  904. }
  905. private static bool IsVexSameOperandDestSrc1(Operation operation)
  906. {
  907. if (IsIntrinsic(operation.Instruction))
  908. {
  909. bool isUnary = operation.SourcesCount < 2;
  910. bool hasVecDest = operation.Destination != null && operation.Destination.Type == OperandType.V128;
  911. return !HardwareCapabilities.SupportsVexEncoding && !isUnary && hasVecDest;
  912. }
  913. return false;
  914. }
  915. private static bool HasConstSrc1(Instruction inst)
  916. {
  917. switch (inst)
  918. {
  919. case Instruction.Copy:
  920. case Instruction.LoadArgument:
  921. case Instruction.Spill:
  922. case Instruction.SpillArg:
  923. return true;
  924. }
  925. return false;
  926. }
  927. private static bool HasConstSrc2(Instruction inst)
  928. {
  929. switch (inst)
  930. {
  931. case Instruction.Add:
  932. case Instruction.BitwiseAnd:
  933. case Instruction.BitwiseExclusiveOr:
  934. case Instruction.BitwiseOr:
  935. case Instruction.CompareEqual:
  936. case Instruction.CompareGreater:
  937. case Instruction.CompareGreaterOrEqual:
  938. case Instruction.CompareGreaterOrEqualUI:
  939. case Instruction.CompareGreaterUI:
  940. case Instruction.CompareLess:
  941. case Instruction.CompareLessOrEqual:
  942. case Instruction.CompareLessOrEqualUI:
  943. case Instruction.CompareLessUI:
  944. case Instruction.CompareNotEqual:
  945. case Instruction.Multiply:
  946. case Instruction.RotateRight:
  947. case Instruction.ShiftLeft:
  948. case Instruction.ShiftRightSI:
  949. case Instruction.ShiftRightUI:
  950. case Instruction.Subtract:
  951. case Instruction.VectorExtract:
  952. case Instruction.VectorExtract16:
  953. case Instruction.VectorExtract8:
  954. return true;
  955. }
  956. return false;
  957. }
  958. private static bool IsCommutative(Instruction inst)
  959. {
  960. switch (inst)
  961. {
  962. case Instruction.Add:
  963. case Instruction.BitwiseAnd:
  964. case Instruction.BitwiseExclusiveOr:
  965. case Instruction.BitwiseOr:
  966. case Instruction.CompareEqual:
  967. case Instruction.CompareNotEqual:
  968. case Instruction.Multiply:
  969. return true;
  970. }
  971. return false;
  972. }
  973. private static bool IsIntrinsic(Instruction inst)
  974. {
  975. return inst == Instruction.Extended;
  976. }
  977. }
  978. }