PreAllocator.cs 52 KB

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