CodeGenerator.cs 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891
  1. using ARMeilleure.CodeGen.Linking;
  2. using ARMeilleure.CodeGen.Optimizations;
  3. using ARMeilleure.CodeGen.RegisterAllocators;
  4. using ARMeilleure.CodeGen.Unwinding;
  5. using ARMeilleure.Common;
  6. using ARMeilleure.Diagnostics;
  7. using ARMeilleure.IntermediateRepresentation;
  8. using ARMeilleure.Translation;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Diagnostics;
  12. using System.Numerics;
  13. using static ARMeilleure.IntermediateRepresentation.Operand.Factory;
  14. namespace ARMeilleure.CodeGen.X86
  15. {
  16. static class CodeGenerator
  17. {
  18. private const int RegistersCount = 16;
  19. private const int PageSize = 0x1000;
  20. private const int StackGuardSize = 0x2000;
  21. private static readonly Action<CodeGenContext, Operation>[] _instTable;
  22. static CodeGenerator()
  23. {
  24. _instTable = new Action<CodeGenContext, Operation>[EnumUtils.GetCount(typeof(Instruction))];
  25. #pragma warning disable IDE0055 // Disable formatting
  26. Add(Instruction.Add, GenerateAdd);
  27. Add(Instruction.BitwiseAnd, GenerateBitwiseAnd);
  28. Add(Instruction.BitwiseExclusiveOr, GenerateBitwiseExclusiveOr);
  29. Add(Instruction.BitwiseNot, GenerateBitwiseNot);
  30. Add(Instruction.BitwiseOr, GenerateBitwiseOr);
  31. Add(Instruction.BranchIf, GenerateBranchIf);
  32. Add(Instruction.ByteSwap, GenerateByteSwap);
  33. Add(Instruction.Call, GenerateCall);
  34. Add(Instruction.Clobber, GenerateClobber);
  35. Add(Instruction.Compare, GenerateCompare);
  36. Add(Instruction.CompareAndSwap, GenerateCompareAndSwap);
  37. Add(Instruction.CompareAndSwap16, GenerateCompareAndSwap16);
  38. Add(Instruction.CompareAndSwap8, GenerateCompareAndSwap8);
  39. Add(Instruction.ConditionalSelect, GenerateConditionalSelect);
  40. Add(Instruction.ConvertI64ToI32, GenerateConvertI64ToI32);
  41. Add(Instruction.ConvertToFP, GenerateConvertToFP);
  42. Add(Instruction.Copy, GenerateCopy);
  43. Add(Instruction.CountLeadingZeros, GenerateCountLeadingZeros);
  44. Add(Instruction.Divide, GenerateDivide);
  45. Add(Instruction.DivideUI, GenerateDivideUI);
  46. Add(Instruction.Fill, GenerateFill);
  47. Add(Instruction.Load, GenerateLoad);
  48. Add(Instruction.Load16, GenerateLoad16);
  49. Add(Instruction.Load8, GenerateLoad8);
  50. Add(Instruction.MemoryBarrier, GenerateMemoryBarrier);
  51. Add(Instruction.Multiply, GenerateMultiply);
  52. Add(Instruction.Multiply64HighSI, GenerateMultiply64HighSI);
  53. Add(Instruction.Multiply64HighUI, GenerateMultiply64HighUI);
  54. Add(Instruction.Negate, GenerateNegate);
  55. Add(Instruction.Return, GenerateReturn);
  56. Add(Instruction.RotateRight, GenerateRotateRight);
  57. Add(Instruction.ShiftLeft, GenerateShiftLeft);
  58. Add(Instruction.ShiftRightSI, GenerateShiftRightSI);
  59. Add(Instruction.ShiftRightUI, GenerateShiftRightUI);
  60. Add(Instruction.SignExtend16, GenerateSignExtend16);
  61. Add(Instruction.SignExtend32, GenerateSignExtend32);
  62. Add(Instruction.SignExtend8, GenerateSignExtend8);
  63. Add(Instruction.Spill, GenerateSpill);
  64. Add(Instruction.SpillArg, GenerateSpillArg);
  65. Add(Instruction.StackAlloc, GenerateStackAlloc);
  66. Add(Instruction.Store, GenerateStore);
  67. Add(Instruction.Store16, GenerateStore16);
  68. Add(Instruction.Store8, GenerateStore8);
  69. Add(Instruction.Subtract, GenerateSubtract);
  70. Add(Instruction.Tailcall, GenerateTailcall);
  71. Add(Instruction.VectorCreateScalar, GenerateVectorCreateScalar);
  72. Add(Instruction.VectorExtract, GenerateVectorExtract);
  73. Add(Instruction.VectorExtract16, GenerateVectorExtract16);
  74. Add(Instruction.VectorExtract8, GenerateVectorExtract8);
  75. Add(Instruction.VectorInsert, GenerateVectorInsert);
  76. Add(Instruction.VectorInsert16, GenerateVectorInsert16);
  77. Add(Instruction.VectorInsert8, GenerateVectorInsert8);
  78. Add(Instruction.VectorOne, GenerateVectorOne);
  79. Add(Instruction.VectorZero, GenerateVectorZero);
  80. Add(Instruction.VectorZeroUpper64, GenerateVectorZeroUpper64);
  81. Add(Instruction.VectorZeroUpper96, GenerateVectorZeroUpper96);
  82. Add(Instruction.ZeroExtend16, GenerateZeroExtend16);
  83. Add(Instruction.ZeroExtend32, GenerateZeroExtend32);
  84. Add(Instruction.ZeroExtend8, GenerateZeroExtend8);
  85. #pragma warning restore IDE0055
  86. static void Add(Instruction inst, Action<CodeGenContext, Operation> func)
  87. {
  88. _instTable[(int)inst] = func;
  89. }
  90. }
  91. public static CompiledFunction Generate(CompilerContext cctx)
  92. {
  93. ControlFlowGraph cfg = cctx.Cfg;
  94. Logger.StartPass(PassName.Optimization);
  95. if (cctx.Options.HasFlag(CompilerOptions.Optimize))
  96. {
  97. if (cctx.Options.HasFlag(CompilerOptions.SsaForm))
  98. {
  99. Optimizer.RunPass(cfg);
  100. }
  101. BlockPlacement.RunPass(cfg);
  102. }
  103. X86Optimizer.RunPass(cfg);
  104. Logger.EndPass(PassName.Optimization, cfg);
  105. Logger.StartPass(PassName.PreAllocation);
  106. StackAllocator stackAlloc = new();
  107. PreAllocator.RunPass(cctx, stackAlloc, out int maxCallArgs);
  108. Logger.EndPass(PassName.PreAllocation, cfg);
  109. Logger.StartPass(PassName.RegisterAllocation);
  110. if (cctx.Options.HasFlag(CompilerOptions.SsaForm))
  111. {
  112. Ssa.Deconstruct(cfg);
  113. }
  114. IRegisterAllocator regAlloc;
  115. if (cctx.Options.HasFlag(CompilerOptions.Lsra))
  116. {
  117. regAlloc = new LinearScanAllocator();
  118. }
  119. else
  120. {
  121. regAlloc = new HybridAllocator();
  122. }
  123. RegisterMasks regMasks = new(
  124. CallingConvention.GetIntAvailableRegisters(),
  125. CallingConvention.GetVecAvailableRegisters(),
  126. CallingConvention.GetIntCallerSavedRegisters(),
  127. CallingConvention.GetVecCallerSavedRegisters(),
  128. CallingConvention.GetIntCalleeSavedRegisters(),
  129. CallingConvention.GetVecCalleeSavedRegisters(),
  130. RegistersCount);
  131. AllocationResult allocResult = regAlloc.RunPass(cfg, stackAlloc, regMasks);
  132. Logger.EndPass(PassName.RegisterAllocation, cfg);
  133. Logger.StartPass(PassName.CodeGeneration);
  134. bool relocatable = (cctx.Options & CompilerOptions.Relocatable) != 0;
  135. CodeGenContext context = new(allocResult, maxCallArgs, cfg.Blocks.Count, relocatable);
  136. UnwindInfo unwindInfo = WritePrologue(context);
  137. for (BasicBlock block = cfg.Blocks.First; block != null; block = block.ListNext)
  138. {
  139. context.EnterBlock(block);
  140. for (Operation node = block.Operations.First; node != default; node = node.ListNext)
  141. {
  142. GenerateOperation(context, node);
  143. }
  144. if (block.SuccessorsCount == 0)
  145. {
  146. // The only blocks which can have 0 successors are exit blocks.
  147. Operation last = block.Operations.Last;
  148. Debug.Assert(last.Instruction == Instruction.Tailcall ||
  149. last.Instruction == Instruction.Return);
  150. }
  151. else
  152. {
  153. BasicBlock succ = block.GetSuccessor(0);
  154. if (succ != block.ListNext)
  155. {
  156. context.JumpTo(succ);
  157. }
  158. }
  159. }
  160. (byte[] code, RelocInfo relocInfo) = context.Assembler.GetCode();
  161. Logger.EndPass(PassName.CodeGeneration);
  162. return new CompiledFunction(code, unwindInfo, relocInfo);
  163. }
  164. private static void GenerateOperation(CodeGenContext context, Operation operation)
  165. {
  166. if (operation.Instruction == Instruction.Extended)
  167. {
  168. IntrinsicInfo info = IntrinsicTable.GetInfo(operation.Intrinsic);
  169. switch (info.Type)
  170. {
  171. case IntrinsicType.Comis_:
  172. {
  173. Operand dest = operation.Destination;
  174. Operand src1 = operation.GetSource(0);
  175. Operand src2 = operation.GetSource(1);
  176. switch (operation.Intrinsic)
  177. {
  178. case Intrinsic.X86Comisdeq:
  179. context.Assembler.Comisd(src1, src2);
  180. context.Assembler.Setcc(dest, X86Condition.Equal);
  181. break;
  182. case Intrinsic.X86Comisdge:
  183. context.Assembler.Comisd(src1, src2);
  184. context.Assembler.Setcc(dest, X86Condition.AboveOrEqual);
  185. break;
  186. case Intrinsic.X86Comisdlt:
  187. context.Assembler.Comisd(src1, src2);
  188. context.Assembler.Setcc(dest, X86Condition.Below);
  189. break;
  190. case Intrinsic.X86Comisseq:
  191. context.Assembler.Comiss(src1, src2);
  192. context.Assembler.Setcc(dest, X86Condition.Equal);
  193. break;
  194. case Intrinsic.X86Comissge:
  195. context.Assembler.Comiss(src1, src2);
  196. context.Assembler.Setcc(dest, X86Condition.AboveOrEqual);
  197. break;
  198. case Intrinsic.X86Comisslt:
  199. context.Assembler.Comiss(src1, src2);
  200. context.Assembler.Setcc(dest, X86Condition.Below);
  201. break;
  202. }
  203. context.Assembler.Movzx8(dest, dest, OperandType.I32);
  204. break;
  205. }
  206. case IntrinsicType.Mxcsr:
  207. {
  208. Operand offset = operation.GetSource(0);
  209. Debug.Assert(offset.Kind == OperandKind.Constant);
  210. Debug.Assert(offset.Type == OperandType.I32);
  211. int offs = offset.AsInt32() + context.CallArgsRegionSize;
  212. Operand rsp = Register(X86Register.Rsp);
  213. Operand memOp = MemoryOp(OperandType.I32, rsp, default, Multiplier.x1, offs);
  214. Debug.Assert(HardwareCapabilities.SupportsSse || HardwareCapabilities.SupportsVexEncoding);
  215. if (operation.Intrinsic == Intrinsic.X86Ldmxcsr)
  216. {
  217. Operand bits = operation.GetSource(1);
  218. Debug.Assert(bits.Type == OperandType.I32);
  219. context.Assembler.Mov(memOp, bits, OperandType.I32);
  220. context.Assembler.Ldmxcsr(memOp);
  221. }
  222. else if (operation.Intrinsic == Intrinsic.X86Stmxcsr)
  223. {
  224. Operand dest = operation.Destination;
  225. Debug.Assert(dest.Type == OperandType.I32);
  226. context.Assembler.Stmxcsr(memOp);
  227. context.Assembler.Mov(dest, memOp, OperandType.I32);
  228. }
  229. break;
  230. }
  231. case IntrinsicType.PopCount:
  232. {
  233. Operand dest = operation.Destination;
  234. Operand source = operation.GetSource(0);
  235. EnsureSameType(dest, source);
  236. Debug.Assert(dest.Type.IsInteger());
  237. context.Assembler.Popcnt(dest, source, dest.Type);
  238. break;
  239. }
  240. case IntrinsicType.Unary:
  241. {
  242. Operand dest = operation.Destination;
  243. Operand source = operation.GetSource(0);
  244. EnsureSameType(dest, source);
  245. Debug.Assert(!dest.Type.IsInteger());
  246. context.Assembler.WriteInstruction(info.Inst, dest, source);
  247. break;
  248. }
  249. case IntrinsicType.UnaryToGpr:
  250. {
  251. Operand dest = operation.Destination;
  252. Operand source = operation.GetSource(0);
  253. Debug.Assert(dest.Type.IsInteger() && !source.Type.IsInteger());
  254. if (operation.Intrinsic == Intrinsic.X86Cvtsi2si)
  255. {
  256. if (dest.Type == OperandType.I32)
  257. {
  258. context.Assembler.Movd(dest, source); // int _mm_cvtsi128_si32(__m128i a)
  259. }
  260. else /* if (dest.Type == OperandType.I64) */
  261. {
  262. context.Assembler.Movq(dest, source); // __int64 _mm_cvtsi128_si64(__m128i a)
  263. }
  264. }
  265. else
  266. {
  267. context.Assembler.WriteInstruction(info.Inst, dest, source, dest.Type);
  268. }
  269. break;
  270. }
  271. case IntrinsicType.Binary:
  272. {
  273. Operand dest = operation.Destination;
  274. Operand src1 = operation.GetSource(0);
  275. Operand src2 = operation.GetSource(1);
  276. EnsureSameType(dest, src1);
  277. if (!HardwareCapabilities.SupportsVexEncoding)
  278. {
  279. EnsureSameReg(dest, src1);
  280. }
  281. Debug.Assert(!dest.Type.IsInteger());
  282. Debug.Assert(!src2.Type.IsInteger() || src2.Kind == OperandKind.Constant);
  283. context.Assembler.WriteInstruction(info.Inst, dest, src1, src2);
  284. break;
  285. }
  286. case IntrinsicType.BinaryGpr:
  287. {
  288. Operand dest = operation.Destination;
  289. Operand src1 = operation.GetSource(0);
  290. Operand src2 = operation.GetSource(1);
  291. EnsureSameType(dest, src1);
  292. if (!HardwareCapabilities.SupportsVexEncoding)
  293. {
  294. EnsureSameReg(dest, src1);
  295. }
  296. Debug.Assert(!dest.Type.IsInteger() && src2.Type.IsInteger());
  297. context.Assembler.WriteInstruction(info.Inst, dest, src1, src2, src2.Type);
  298. break;
  299. }
  300. case IntrinsicType.Crc32:
  301. {
  302. Operand dest = operation.Destination;
  303. Operand src1 = operation.GetSource(0);
  304. Operand src2 = operation.GetSource(1);
  305. EnsureSameReg(dest, src1);
  306. Debug.Assert(dest.Type.IsInteger() && src1.Type.IsInteger() && src2.Type.IsInteger());
  307. context.Assembler.WriteInstruction(info.Inst, dest, src2, dest.Type);
  308. break;
  309. }
  310. case IntrinsicType.BinaryImm:
  311. {
  312. Operand dest = operation.Destination;
  313. Operand src1 = operation.GetSource(0);
  314. Operand src2 = operation.GetSource(1);
  315. EnsureSameType(dest, src1);
  316. if (!HardwareCapabilities.SupportsVexEncoding)
  317. {
  318. EnsureSameReg(dest, src1);
  319. }
  320. Debug.Assert(!dest.Type.IsInteger() && src2.Kind == OperandKind.Constant);
  321. context.Assembler.WriteInstruction(info.Inst, dest, src1, src2.AsByte());
  322. break;
  323. }
  324. case IntrinsicType.Ternary:
  325. {
  326. Operand dest = operation.Destination;
  327. Operand src1 = operation.GetSource(0);
  328. Operand src2 = operation.GetSource(1);
  329. Operand src3 = operation.GetSource(2);
  330. EnsureSameType(dest, src1, src2, src3);
  331. Debug.Assert(!dest.Type.IsInteger());
  332. if (info.Inst == X86Instruction.Blendvpd && HardwareCapabilities.SupportsVexEncoding)
  333. {
  334. context.Assembler.WriteInstruction(X86Instruction.Vblendvpd, dest, src1, src2, src3);
  335. }
  336. else if (info.Inst == X86Instruction.Blendvps && HardwareCapabilities.SupportsVexEncoding)
  337. {
  338. context.Assembler.WriteInstruction(X86Instruction.Vblendvps, dest, src1, src2, src3);
  339. }
  340. else if (info.Inst == X86Instruction.Pblendvb && HardwareCapabilities.SupportsVexEncoding)
  341. {
  342. context.Assembler.WriteInstruction(X86Instruction.Vpblendvb, dest, src1, src2, src3);
  343. }
  344. else
  345. {
  346. EnsureSameReg(dest, src1);
  347. Debug.Assert(src3.GetRegister().Index == 0);
  348. context.Assembler.WriteInstruction(info.Inst, dest, src1, src2);
  349. }
  350. break;
  351. }
  352. case IntrinsicType.TernaryImm:
  353. {
  354. Operand dest = operation.Destination;
  355. Operand src1 = operation.GetSource(0);
  356. Operand src2 = operation.GetSource(1);
  357. Operand src3 = operation.GetSource(2);
  358. EnsureSameType(dest, src1, src2);
  359. if (!HardwareCapabilities.SupportsVexEncoding)
  360. {
  361. EnsureSameReg(dest, src1);
  362. }
  363. Debug.Assert(!dest.Type.IsInteger() && src3.Kind == OperandKind.Constant);
  364. context.Assembler.WriteInstruction(info.Inst, dest, src1, src2, src3.AsByte());
  365. break;
  366. }
  367. case IntrinsicType.Fma:
  368. {
  369. Operand dest = operation.Destination;
  370. Operand src1 = operation.GetSource(0);
  371. Operand src2 = operation.GetSource(1);
  372. Operand src3 = operation.GetSource(2);
  373. Debug.Assert(HardwareCapabilities.SupportsVexEncoding);
  374. Debug.Assert(dest.Kind == OperandKind.Register && src1.Kind == OperandKind.Register && src2.Kind == OperandKind.Register);
  375. Debug.Assert(src3.Kind == OperandKind.Register || src3.Kind == OperandKind.Memory);
  376. EnsureSameType(dest, src1, src2, src3);
  377. Debug.Assert(dest.Type == OperandType.V128);
  378. Debug.Assert(dest.Value == src1.Value);
  379. context.Assembler.WriteInstruction(info.Inst, dest, src2, src3);
  380. break;
  381. }
  382. }
  383. }
  384. else
  385. {
  386. Action<CodeGenContext, Operation> func = _instTable[(int)operation.Instruction];
  387. if (func != null)
  388. {
  389. func(context, operation);
  390. }
  391. else
  392. {
  393. throw new ArgumentException($"Invalid instruction \"{operation.Instruction}\".");
  394. }
  395. }
  396. }
  397. private static void GenerateAdd(CodeGenContext context, Operation operation)
  398. {
  399. Operand dest = operation.Destination;
  400. Operand src1 = operation.GetSource(0);
  401. Operand src2 = operation.GetSource(1);
  402. if (dest.Type.IsInteger())
  403. {
  404. // If Destination and Source 1 Operands are the same, perform a standard add as there are no benefits to using LEA.
  405. if (dest.Kind == src1.Kind && dest.Value == src1.Value)
  406. {
  407. ValidateBinOp(dest, src1, src2);
  408. context.Assembler.Add(dest, src2, dest.Type);
  409. }
  410. else
  411. {
  412. EnsureSameType(dest, src1, src2);
  413. int offset;
  414. Operand index;
  415. if (src2.Kind == OperandKind.Constant)
  416. {
  417. offset = src2.AsInt32();
  418. index = default;
  419. }
  420. else
  421. {
  422. offset = 0;
  423. index = src2;
  424. }
  425. Operand memOp = MemoryOp(dest.Type, src1, index, Multiplier.x1, offset);
  426. context.Assembler.Lea(dest, memOp, dest.Type);
  427. }
  428. }
  429. else
  430. {
  431. ValidateBinOp(dest, src1, src2);
  432. if (dest.Type == OperandType.FP32)
  433. {
  434. context.Assembler.Addss(dest, src1, src2);
  435. }
  436. else /* if (dest.Type == OperandType.FP64) */
  437. {
  438. context.Assembler.Addsd(dest, src1, src2);
  439. }
  440. }
  441. }
  442. private static void GenerateBitwiseAnd(CodeGenContext context, Operation operation)
  443. {
  444. Operand dest = operation.Destination;
  445. Operand src1 = operation.GetSource(0);
  446. Operand src2 = operation.GetSource(1);
  447. ValidateBinOp(dest, src1, src2);
  448. Debug.Assert(dest.Type.IsInteger());
  449. // Note: GenerateCompareCommon makes the assumption that BitwiseAnd will emit only a single `and`
  450. // instruction.
  451. context.Assembler.And(dest, src2, dest.Type);
  452. }
  453. private static void GenerateBitwiseExclusiveOr(CodeGenContext context, Operation operation)
  454. {
  455. Operand dest = operation.Destination;
  456. Operand src1 = operation.GetSource(0);
  457. Operand src2 = operation.GetSource(1);
  458. ValidateBinOp(dest, src1, src2);
  459. if (dest.Type.IsInteger())
  460. {
  461. context.Assembler.Xor(dest, src2, dest.Type);
  462. }
  463. else
  464. {
  465. context.Assembler.Xorps(dest, src1, src2);
  466. }
  467. }
  468. private static void GenerateBitwiseNot(CodeGenContext context, Operation operation)
  469. {
  470. Operand dest = operation.Destination;
  471. Operand source = operation.GetSource(0);
  472. ValidateUnOp(dest, source);
  473. Debug.Assert(dest.Type.IsInteger());
  474. context.Assembler.Not(dest);
  475. }
  476. private static void GenerateBitwiseOr(CodeGenContext context, Operation operation)
  477. {
  478. Operand dest = operation.Destination;
  479. Operand src1 = operation.GetSource(0);
  480. Operand src2 = operation.GetSource(1);
  481. ValidateBinOp(dest, src1, src2);
  482. Debug.Assert(dest.Type.IsInteger());
  483. context.Assembler.Or(dest, src2, dest.Type);
  484. }
  485. private static void GenerateBranchIf(CodeGenContext context, Operation operation)
  486. {
  487. Operand comp = operation.GetSource(2);
  488. Debug.Assert(comp.Kind == OperandKind.Constant);
  489. var cond = ((Comparison)comp.AsInt32()).ToX86Condition();
  490. GenerateCompareCommon(context, operation);
  491. context.JumpTo(cond, context.CurrBlock.GetSuccessor(1));
  492. }
  493. private static void GenerateByteSwap(CodeGenContext context, Operation operation)
  494. {
  495. Operand dest = operation.Destination;
  496. Operand source = operation.GetSource(0);
  497. ValidateUnOp(dest, source);
  498. Debug.Assert(dest.Type.IsInteger());
  499. context.Assembler.Bswap(dest);
  500. }
  501. private static void GenerateCall(CodeGenContext context, Operation operation)
  502. {
  503. context.Assembler.Call(operation.GetSource(0));
  504. }
  505. private static void GenerateClobber(CodeGenContext context, Operation operation)
  506. {
  507. // This is only used to indicate that a register is clobbered to the
  508. // register allocator, we don't need to produce any code.
  509. }
  510. private static void GenerateCompare(CodeGenContext context, Operation operation)
  511. {
  512. Operand dest = operation.Destination;
  513. Operand comp = operation.GetSource(2);
  514. Debug.Assert(dest.Type == OperandType.I32);
  515. Debug.Assert(comp.Kind == OperandKind.Constant);
  516. var cond = ((Comparison)comp.AsInt32()).ToX86Condition();
  517. GenerateCompareCommon(context, operation);
  518. context.Assembler.Setcc(dest, cond);
  519. context.Assembler.Movzx8(dest, dest, OperandType.I32);
  520. }
  521. private static void GenerateCompareCommon(CodeGenContext context, Operation operation)
  522. {
  523. Operand src1 = operation.GetSource(0);
  524. Operand src2 = operation.GetSource(1);
  525. EnsureSameType(src1, src2);
  526. Debug.Assert(src1.Type.IsInteger());
  527. if (src2.Kind == OperandKind.Constant && src2.Value == 0)
  528. {
  529. if (MatchOperation(operation.ListPrevious, Instruction.BitwiseAnd, src1.Type, src1.GetRegister()))
  530. {
  531. // Since the `test` and `and` instruction set the status flags in the same way, we can omit the
  532. // `test r,r` instruction when it is immediately preceded by an `and r,*` instruction.
  533. //
  534. // For example:
  535. //
  536. // and eax, 0x3
  537. // test eax, eax
  538. // jz .L0
  539. //
  540. // =>
  541. //
  542. // and eax, 0x3
  543. // jz .L0
  544. }
  545. else
  546. {
  547. context.Assembler.Test(src1, src1, src1.Type);
  548. }
  549. }
  550. else
  551. {
  552. context.Assembler.Cmp(src1, src2, src1.Type);
  553. }
  554. }
  555. private static void GenerateCompareAndSwap(CodeGenContext context, Operation operation)
  556. {
  557. Operand src1 = operation.GetSource(0);
  558. if (operation.SourcesCount == 5) // CompareAndSwap128 has 5 sources, compared to CompareAndSwap64/32's 3.
  559. {
  560. Operand memOp = MemoryOp(OperandType.I64, src1);
  561. context.Assembler.Cmpxchg16b(memOp);
  562. }
  563. else
  564. {
  565. Operand src2 = operation.GetSource(1);
  566. Operand src3 = operation.GetSource(2);
  567. EnsureSameType(src2, src3);
  568. Operand memOp = MemoryOp(src3.Type, src1);
  569. context.Assembler.Cmpxchg(memOp, src3);
  570. }
  571. }
  572. private static void GenerateCompareAndSwap16(CodeGenContext context, Operation operation)
  573. {
  574. Operand src1 = operation.GetSource(0);
  575. Operand src2 = operation.GetSource(1);
  576. Operand src3 = operation.GetSource(2);
  577. EnsureSameType(src2, src3);
  578. Operand memOp = MemoryOp(src3.Type, src1);
  579. context.Assembler.Cmpxchg16(memOp, src3);
  580. }
  581. private static void GenerateCompareAndSwap8(CodeGenContext context, Operation operation)
  582. {
  583. Operand src1 = operation.GetSource(0);
  584. Operand src2 = operation.GetSource(1);
  585. Operand src3 = operation.GetSource(2);
  586. EnsureSameType(src2, src3);
  587. Operand memOp = MemoryOp(src3.Type, src1);
  588. context.Assembler.Cmpxchg8(memOp, src3);
  589. }
  590. private static void GenerateConditionalSelect(CodeGenContext context, Operation operation)
  591. {
  592. Operand dest = operation.Destination;
  593. Operand src1 = operation.GetSource(0);
  594. Operand src2 = operation.GetSource(1);
  595. Operand src3 = operation.GetSource(2);
  596. EnsureSameReg(dest, src3);
  597. EnsureSameType(dest, src2, src3);
  598. Debug.Assert(dest.Type.IsInteger());
  599. Debug.Assert(src1.Type == OperandType.I32);
  600. context.Assembler.Test(src1, src1, src1.Type);
  601. context.Assembler.Cmovcc(dest, src2, dest.Type, X86Condition.NotEqual);
  602. }
  603. private static void GenerateConvertI64ToI32(CodeGenContext context, Operation operation)
  604. {
  605. Operand dest = operation.Destination;
  606. Operand source = operation.GetSource(0);
  607. Debug.Assert(dest.Type == OperandType.I32 && source.Type == OperandType.I64);
  608. context.Assembler.Mov(dest, source, OperandType.I32);
  609. }
  610. private static void GenerateConvertToFP(CodeGenContext context, Operation operation)
  611. {
  612. Operand dest = operation.Destination;
  613. Operand source = operation.GetSource(0);
  614. Debug.Assert(dest.Type == OperandType.FP32 || dest.Type == OperandType.FP64);
  615. if (dest.Type == OperandType.FP32)
  616. {
  617. Debug.Assert(source.Type.IsInteger() || source.Type == OperandType.FP64);
  618. if (source.Type.IsInteger())
  619. {
  620. context.Assembler.Xorps(dest, dest, dest);
  621. context.Assembler.Cvtsi2ss(dest, dest, source, source.Type);
  622. }
  623. else /* if (source.Type == OperandType.FP64) */
  624. {
  625. context.Assembler.Cvtsd2ss(dest, dest, source);
  626. GenerateZeroUpper96(context, dest, dest);
  627. }
  628. }
  629. else /* if (dest.Type == OperandType.FP64) */
  630. {
  631. Debug.Assert(source.Type.IsInteger() || source.Type == OperandType.FP32);
  632. if (source.Type.IsInteger())
  633. {
  634. context.Assembler.Xorps(dest, dest, dest);
  635. context.Assembler.Cvtsi2sd(dest, dest, source, source.Type);
  636. }
  637. else /* if (source.Type == OperandType.FP32) */
  638. {
  639. context.Assembler.Cvtss2sd(dest, dest, source);
  640. GenerateZeroUpper64(context, dest, dest);
  641. }
  642. }
  643. }
  644. private static void GenerateCopy(CodeGenContext context, Operation operation)
  645. {
  646. Operand dest = operation.Destination;
  647. Operand source = operation.GetSource(0);
  648. EnsureSameType(dest, source);
  649. Debug.Assert(dest.Type.IsInteger() || source.Kind != OperandKind.Constant);
  650. // Moves to the same register are useless.
  651. if (dest.Kind == source.Kind && dest.Value == source.Value)
  652. {
  653. return;
  654. }
  655. if (dest.Kind == OperandKind.Register &&
  656. source.Kind == OperandKind.Constant && source.Value == 0)
  657. {
  658. // Assemble "mov reg, 0" as "xor reg, reg" as the later is more efficient.
  659. context.Assembler.Xor(dest, dest, OperandType.I32);
  660. }
  661. else if (dest.Type.IsInteger())
  662. {
  663. context.Assembler.Mov(dest, source, dest.Type);
  664. }
  665. else
  666. {
  667. context.Assembler.Movdqu(dest, source);
  668. }
  669. }
  670. private static void GenerateCountLeadingZeros(CodeGenContext context, Operation operation)
  671. {
  672. Operand dest = operation.Destination;
  673. Operand source = operation.GetSource(0);
  674. EnsureSameType(dest, source);
  675. Debug.Assert(dest.Type.IsInteger());
  676. context.Assembler.Bsr(dest, source, dest.Type);
  677. int operandSize = dest.Type == OperandType.I32 ? 32 : 64;
  678. int operandMask = operandSize - 1;
  679. // When the input operand is 0, the result is undefined, however the
  680. // ZF flag is set. We are supposed to return the operand size on that
  681. // case. So, add an additional jump to handle that case, by moving the
  682. // operand size constant to the destination register.
  683. Operand neLabel = Label();
  684. context.Assembler.Jcc(X86Condition.NotEqual, neLabel);
  685. context.Assembler.Mov(dest, Const(operandSize | operandMask), OperandType.I32);
  686. context.Assembler.MarkLabel(neLabel);
  687. // BSR returns the zero based index of the last bit set on the operand,
  688. // starting from the least significant bit. However we are supposed to
  689. // return the number of 0 bits on the high end. So, we invert the result
  690. // of the BSR using XOR to get the correct value.
  691. context.Assembler.Xor(dest, Const(operandMask), OperandType.I32);
  692. }
  693. private static void GenerateDivide(CodeGenContext context, Operation operation)
  694. {
  695. Operand dest = operation.Destination;
  696. Operand dividend = operation.GetSource(0);
  697. Operand divisor = operation.GetSource(1);
  698. if (!dest.Type.IsInteger())
  699. {
  700. ValidateBinOp(dest, dividend, divisor);
  701. }
  702. if (dest.Type.IsInteger())
  703. {
  704. divisor = operation.GetSource(2);
  705. EnsureSameType(dest, divisor);
  706. if (divisor.Type == OperandType.I32)
  707. {
  708. context.Assembler.Cdq();
  709. }
  710. else
  711. {
  712. context.Assembler.Cqo();
  713. }
  714. context.Assembler.Idiv(divisor);
  715. }
  716. else if (dest.Type == OperandType.FP32)
  717. {
  718. context.Assembler.Divss(dest, dividend, divisor);
  719. }
  720. else /* if (dest.Type == OperandType.FP64) */
  721. {
  722. context.Assembler.Divsd(dest, dividend, divisor);
  723. }
  724. }
  725. private static void GenerateDivideUI(CodeGenContext context, Operation operation)
  726. {
  727. Operand divisor = operation.GetSource(2);
  728. Operand rdx = Register(X86Register.Rdx);
  729. Debug.Assert(divisor.Type.IsInteger());
  730. context.Assembler.Xor(rdx, rdx, OperandType.I32);
  731. context.Assembler.Div(divisor);
  732. }
  733. private static void GenerateFill(CodeGenContext context, Operation operation)
  734. {
  735. Operand dest = operation.Destination;
  736. Operand offset = operation.GetSource(0);
  737. Debug.Assert(offset.Kind == OperandKind.Constant);
  738. int offs = offset.AsInt32() + context.CallArgsRegionSize;
  739. Operand rsp = Register(X86Register.Rsp);
  740. Operand memOp = MemoryOp(dest.Type, rsp, default, Multiplier.x1, offs);
  741. GenerateLoad(context, memOp, dest);
  742. }
  743. private static void GenerateLoad(CodeGenContext context, Operation operation)
  744. {
  745. Operand value = operation.Destination;
  746. Operand address = Memory(operation.GetSource(0), value.Type);
  747. GenerateLoad(context, address, value);
  748. }
  749. private static void GenerateLoad16(CodeGenContext context, Operation operation)
  750. {
  751. Operand value = operation.Destination;
  752. Operand address = Memory(operation.GetSource(0), value.Type);
  753. Debug.Assert(value.Type.IsInteger());
  754. context.Assembler.Movzx16(value, address, value.Type);
  755. }
  756. private static void GenerateLoad8(CodeGenContext context, Operation operation)
  757. {
  758. Operand value = operation.Destination;
  759. Operand address = Memory(operation.GetSource(0), value.Type);
  760. Debug.Assert(value.Type.IsInteger());
  761. context.Assembler.Movzx8(value, address, value.Type);
  762. }
  763. private static void GenerateMemoryBarrier(CodeGenContext context, Operation operation)
  764. {
  765. context.Assembler.LockOr(MemoryOp(OperandType.I64, Register(X86Register.Rsp)), Const(0), OperandType.I32);
  766. }
  767. private static void GenerateMultiply(CodeGenContext context, Operation operation)
  768. {
  769. Operand dest = operation.Destination;
  770. Operand src1 = operation.GetSource(0);
  771. Operand src2 = operation.GetSource(1);
  772. if (src2.Kind != OperandKind.Constant)
  773. {
  774. EnsureSameReg(dest, src1);
  775. }
  776. EnsureSameType(dest, src1, src2);
  777. if (dest.Type.IsInteger())
  778. {
  779. if (src2.Kind == OperandKind.Constant)
  780. {
  781. context.Assembler.Imul(dest, src1, src2, dest.Type);
  782. }
  783. else
  784. {
  785. context.Assembler.Imul(dest, src2, dest.Type);
  786. }
  787. }
  788. else if (dest.Type == OperandType.FP32)
  789. {
  790. context.Assembler.Mulss(dest, src1, src2);
  791. }
  792. else /* if (dest.Type == OperandType.FP64) */
  793. {
  794. context.Assembler.Mulsd(dest, src1, src2);
  795. }
  796. }
  797. private static void GenerateMultiply64HighSI(CodeGenContext context, Operation operation)
  798. {
  799. Operand source = operation.GetSource(1);
  800. Debug.Assert(source.Type == OperandType.I64);
  801. context.Assembler.Imul(source);
  802. }
  803. private static void GenerateMultiply64HighUI(CodeGenContext context, Operation operation)
  804. {
  805. Operand source = operation.GetSource(1);
  806. Debug.Assert(source.Type == OperandType.I64);
  807. context.Assembler.Mul(source);
  808. }
  809. private static void GenerateNegate(CodeGenContext context, Operation operation)
  810. {
  811. Operand dest = operation.Destination;
  812. Operand source = operation.GetSource(0);
  813. ValidateUnOp(dest, source);
  814. Debug.Assert(dest.Type.IsInteger());
  815. context.Assembler.Neg(dest);
  816. }
  817. private static void GenerateReturn(CodeGenContext context, Operation operation)
  818. {
  819. WriteEpilogue(context);
  820. context.Assembler.Return();
  821. }
  822. private static void GenerateRotateRight(CodeGenContext context, Operation operation)
  823. {
  824. Operand dest = operation.Destination;
  825. Operand src1 = operation.GetSource(0);
  826. Operand src2 = operation.GetSource(1);
  827. ValidateShift(dest, src1, src2);
  828. context.Assembler.Ror(dest, src2, dest.Type);
  829. }
  830. private static void GenerateShiftLeft(CodeGenContext context, Operation operation)
  831. {
  832. Operand dest = operation.Destination;
  833. Operand src1 = operation.GetSource(0);
  834. Operand src2 = operation.GetSource(1);
  835. ValidateShift(dest, src1, src2);
  836. context.Assembler.Shl(dest, src2, dest.Type);
  837. }
  838. private static void GenerateShiftRightSI(CodeGenContext context, Operation operation)
  839. {
  840. Operand dest = operation.Destination;
  841. Operand src1 = operation.GetSource(0);
  842. Operand src2 = operation.GetSource(1);
  843. ValidateShift(dest, src1, src2);
  844. context.Assembler.Sar(dest, src2, dest.Type);
  845. }
  846. private static void GenerateShiftRightUI(CodeGenContext context, Operation operation)
  847. {
  848. Operand dest = operation.Destination;
  849. Operand src1 = operation.GetSource(0);
  850. Operand src2 = operation.GetSource(1);
  851. ValidateShift(dest, src1, src2);
  852. context.Assembler.Shr(dest, src2, dest.Type);
  853. }
  854. private static void GenerateSignExtend16(CodeGenContext context, Operation operation)
  855. {
  856. Operand dest = operation.Destination;
  857. Operand source = operation.GetSource(0);
  858. Debug.Assert(dest.Type.IsInteger() && source.Type.IsInteger());
  859. context.Assembler.Movsx16(dest, source, dest.Type);
  860. }
  861. private static void GenerateSignExtend32(CodeGenContext context, Operation operation)
  862. {
  863. Operand dest = operation.Destination;
  864. Operand source = operation.GetSource(0);
  865. Debug.Assert(dest.Type.IsInteger() && source.Type.IsInteger());
  866. context.Assembler.Movsx32(dest, source, dest.Type);
  867. }
  868. private static void GenerateSignExtend8(CodeGenContext context, Operation operation)
  869. {
  870. Operand dest = operation.Destination;
  871. Operand source = operation.GetSource(0);
  872. Debug.Assert(dest.Type.IsInteger() && source.Type.IsInteger());
  873. context.Assembler.Movsx8(dest, source, dest.Type);
  874. }
  875. private static void GenerateSpill(CodeGenContext context, Operation operation)
  876. {
  877. GenerateSpill(context, operation, context.CallArgsRegionSize);
  878. }
  879. private static void GenerateSpillArg(CodeGenContext context, Operation operation)
  880. {
  881. GenerateSpill(context, operation, 0);
  882. }
  883. private static void GenerateSpill(CodeGenContext context, Operation operation, int baseOffset)
  884. {
  885. Operand offset = operation.GetSource(0);
  886. Operand source = operation.GetSource(1);
  887. Debug.Assert(offset.Kind == OperandKind.Constant);
  888. int offs = offset.AsInt32() + baseOffset;
  889. Operand rsp = Register(X86Register.Rsp);
  890. Operand memOp = MemoryOp(source.Type, rsp, default, Multiplier.x1, offs);
  891. GenerateStore(context, memOp, source);
  892. }
  893. private static void GenerateStackAlloc(CodeGenContext context, Operation operation)
  894. {
  895. Operand dest = operation.Destination;
  896. Operand offset = operation.GetSource(0);
  897. Debug.Assert(offset.Kind == OperandKind.Constant);
  898. int offs = offset.AsInt32() + context.CallArgsRegionSize;
  899. Operand rsp = Register(X86Register.Rsp);
  900. Operand memOp = MemoryOp(OperandType.I64, rsp, default, Multiplier.x1, offs);
  901. context.Assembler.Lea(dest, memOp, OperandType.I64);
  902. }
  903. private static void GenerateStore(CodeGenContext context, Operation operation)
  904. {
  905. Operand value = operation.GetSource(1);
  906. Operand address = Memory(operation.GetSource(0), value.Type);
  907. GenerateStore(context, address, value);
  908. }
  909. private static void GenerateStore16(CodeGenContext context, Operation operation)
  910. {
  911. Operand value = operation.GetSource(1);
  912. Operand address = Memory(operation.GetSource(0), value.Type);
  913. Debug.Assert(value.Type.IsInteger());
  914. context.Assembler.Mov16(address, value);
  915. }
  916. private static void GenerateStore8(CodeGenContext context, Operation operation)
  917. {
  918. Operand value = operation.GetSource(1);
  919. Operand address = Memory(operation.GetSource(0), value.Type);
  920. Debug.Assert(value.Type.IsInteger());
  921. context.Assembler.Mov8(address, value);
  922. }
  923. private static void GenerateSubtract(CodeGenContext context, Operation operation)
  924. {
  925. Operand dest = operation.Destination;
  926. Operand src1 = operation.GetSource(0);
  927. Operand src2 = operation.GetSource(1);
  928. ValidateBinOp(dest, src1, src2);
  929. if (dest.Type.IsInteger())
  930. {
  931. context.Assembler.Sub(dest, src2, dest.Type);
  932. }
  933. else if (dest.Type == OperandType.FP32)
  934. {
  935. context.Assembler.Subss(dest, src1, src2);
  936. }
  937. else /* if (dest.Type == OperandType.FP64) */
  938. {
  939. context.Assembler.Subsd(dest, src1, src2);
  940. }
  941. }
  942. private static void GenerateTailcall(CodeGenContext context, Operation operation)
  943. {
  944. WriteEpilogue(context);
  945. context.Assembler.Jmp(operation.GetSource(0));
  946. }
  947. private static void GenerateVectorCreateScalar(CodeGenContext context, Operation operation)
  948. {
  949. Operand dest = operation.Destination;
  950. Operand source = operation.GetSource(0);
  951. Debug.Assert(!dest.Type.IsInteger() && source.Type.IsInteger());
  952. if (source.Type == OperandType.I32)
  953. {
  954. context.Assembler.Movd(dest, source); // (__m128i _mm_cvtsi32_si128(int a))
  955. }
  956. else /* if (source.Type == OperandType.I64) */
  957. {
  958. context.Assembler.Movq(dest, source); // (__m128i _mm_cvtsi64_si128(__int64 a))
  959. }
  960. }
  961. private static void GenerateVectorExtract(CodeGenContext context, Operation operation)
  962. {
  963. Operand dest = operation.Destination; //Value
  964. Operand src1 = operation.GetSource(0); //Vector
  965. Operand src2 = operation.GetSource(1); //Index
  966. Debug.Assert(src1.Type == OperandType.V128);
  967. Debug.Assert(src2.Kind == OperandKind.Constant);
  968. byte index = src2.AsByte();
  969. Debug.Assert(index < OperandType.V128.GetSizeInBytes() / dest.Type.GetSizeInBytes());
  970. if (dest.Type == OperandType.I32)
  971. {
  972. if (index == 0)
  973. {
  974. context.Assembler.Movd(dest, src1);
  975. }
  976. else if (HardwareCapabilities.SupportsSse41)
  977. {
  978. context.Assembler.Pextrd(dest, src1, index);
  979. }
  980. else
  981. {
  982. int mask0 = 0b11_10_01_00;
  983. int mask1 = 0b11_10_01_00;
  984. mask0 = BitUtils.RotateRight(mask0, index * 2, 8);
  985. mask1 = BitUtils.RotateRight(mask1, 8 - index * 2, 8);
  986. context.Assembler.Pshufd(src1, src1, (byte)mask0);
  987. context.Assembler.Movd(dest, src1);
  988. context.Assembler.Pshufd(src1, src1, (byte)mask1);
  989. }
  990. }
  991. else if (dest.Type == OperandType.I64)
  992. {
  993. if (index == 0)
  994. {
  995. context.Assembler.Movq(dest, src1);
  996. }
  997. else if (HardwareCapabilities.SupportsSse41)
  998. {
  999. context.Assembler.Pextrq(dest, src1, index);
  1000. }
  1001. else
  1002. {
  1003. const byte Mask = 0b01_00_11_10;
  1004. context.Assembler.Pshufd(src1, src1, Mask);
  1005. context.Assembler.Movq(dest, src1);
  1006. context.Assembler.Pshufd(src1, src1, Mask);
  1007. }
  1008. }
  1009. else
  1010. {
  1011. // Floating-point types.
  1012. if ((index >= 2 && dest.Type == OperandType.FP32) ||
  1013. (index == 1 && dest.Type == OperandType.FP64))
  1014. {
  1015. context.Assembler.Movhlps(dest, dest, src1);
  1016. context.Assembler.Movq(dest, dest);
  1017. }
  1018. else
  1019. {
  1020. context.Assembler.Movq(dest, src1);
  1021. }
  1022. if (dest.Type == OperandType.FP32)
  1023. {
  1024. context.Assembler.Pshufd(dest, dest, (byte)(0xfc | (index & 1)));
  1025. }
  1026. }
  1027. }
  1028. private static void GenerateVectorExtract16(CodeGenContext context, Operation operation)
  1029. {
  1030. Operand dest = operation.Destination; //Value
  1031. Operand src1 = operation.GetSource(0); //Vector
  1032. Operand src2 = operation.GetSource(1); //Index
  1033. Debug.Assert(src1.Type == OperandType.V128);
  1034. Debug.Assert(src2.Kind == OperandKind.Constant);
  1035. byte index = src2.AsByte();
  1036. Debug.Assert(index < 8);
  1037. context.Assembler.Pextrw(dest, src1, index);
  1038. }
  1039. private static void GenerateVectorExtract8(CodeGenContext context, Operation operation)
  1040. {
  1041. Operand dest = operation.Destination; //Value
  1042. Operand src1 = operation.GetSource(0); //Vector
  1043. Operand src2 = operation.GetSource(1); //Index
  1044. Debug.Assert(src1.Type == OperandType.V128);
  1045. Debug.Assert(src2.Kind == OperandKind.Constant);
  1046. byte index = src2.AsByte();
  1047. Debug.Assert(index < 16);
  1048. if (HardwareCapabilities.SupportsSse41)
  1049. {
  1050. context.Assembler.Pextrb(dest, src1, index);
  1051. }
  1052. else
  1053. {
  1054. context.Assembler.Pextrw(dest, src1, (byte)(index >> 1));
  1055. if ((index & 1) != 0)
  1056. {
  1057. context.Assembler.Shr(dest, Const(8), OperandType.I32);
  1058. }
  1059. else
  1060. {
  1061. context.Assembler.Movzx8(dest, dest, OperandType.I32);
  1062. }
  1063. }
  1064. }
  1065. private static void GenerateVectorInsert(CodeGenContext context, Operation operation)
  1066. {
  1067. Operand dest = operation.Destination;
  1068. Operand src1 = operation.GetSource(0); //Vector
  1069. Operand src2 = operation.GetSource(1); //Value
  1070. Operand src3 = operation.GetSource(2); //Index
  1071. if (!HardwareCapabilities.SupportsVexEncoding)
  1072. {
  1073. EnsureSameReg(dest, src1);
  1074. }
  1075. Debug.Assert(src1.Type == OperandType.V128);
  1076. Debug.Assert(src3.Kind == OperandKind.Constant);
  1077. byte index = src3.AsByte();
  1078. void InsertIntSse2(int words)
  1079. {
  1080. if (dest.GetRegister() != src1.GetRegister())
  1081. {
  1082. context.Assembler.Movdqu(dest, src1);
  1083. }
  1084. for (int word = 0; word < words; word++)
  1085. {
  1086. // Insert lower 16-bits.
  1087. context.Assembler.Pinsrw(dest, dest, src2, (byte)(index * words + word));
  1088. // Move next word down.
  1089. context.Assembler.Ror(src2, Const(16), src2.Type);
  1090. }
  1091. }
  1092. if (src2.Type == OperandType.I32)
  1093. {
  1094. Debug.Assert(index < 4);
  1095. if (HardwareCapabilities.SupportsSse41)
  1096. {
  1097. context.Assembler.Pinsrd(dest, src1, src2, index);
  1098. }
  1099. else
  1100. {
  1101. InsertIntSse2(2);
  1102. }
  1103. }
  1104. else if (src2.Type == OperandType.I64)
  1105. {
  1106. Debug.Assert(index < 2);
  1107. if (HardwareCapabilities.SupportsSse41)
  1108. {
  1109. context.Assembler.Pinsrq(dest, src1, src2, index);
  1110. }
  1111. else
  1112. {
  1113. InsertIntSse2(4);
  1114. }
  1115. }
  1116. else if (src2.Type == OperandType.FP32)
  1117. {
  1118. Debug.Assert(index < 4);
  1119. if (index != 0)
  1120. {
  1121. if (HardwareCapabilities.SupportsSse41)
  1122. {
  1123. context.Assembler.Insertps(dest, src1, src2, (byte)(index << 4));
  1124. }
  1125. else
  1126. {
  1127. if (src1.GetRegister() == src2.GetRegister())
  1128. {
  1129. int mask = 0b11_10_01_00;
  1130. mask &= ~(0b11 << index * 2);
  1131. context.Assembler.Pshufd(dest, src1, (byte)mask);
  1132. }
  1133. else
  1134. {
  1135. int mask0 = 0b11_10_01_00;
  1136. int mask1 = 0b11_10_01_00;
  1137. mask0 = BitUtils.RotateRight(mask0, index * 2, 8);
  1138. mask1 = BitUtils.RotateRight(mask1, 8 - index * 2, 8);
  1139. context.Assembler.Pshufd(src1, src1, (byte)mask0); // Lane to be inserted in position 0.
  1140. context.Assembler.Movss(dest, src1, src2); // dest[127:0] = src1[127:32] | src2[31:0]
  1141. context.Assembler.Pshufd(dest, dest, (byte)mask1); // Inserted lane in original position.
  1142. if (dest.GetRegister() != src1.GetRegister())
  1143. {
  1144. context.Assembler.Pshufd(src1, src1, (byte)mask1); // Restore src1.
  1145. }
  1146. }
  1147. }
  1148. }
  1149. else
  1150. {
  1151. context.Assembler.Movss(dest, src1, src2);
  1152. }
  1153. }
  1154. else /* if (src2.Type == OperandType.FP64) */
  1155. {
  1156. Debug.Assert(index < 2);
  1157. if (index != 0)
  1158. {
  1159. context.Assembler.Movlhps(dest, src1, src2);
  1160. }
  1161. else
  1162. {
  1163. context.Assembler.Movsd(dest, src1, src2);
  1164. }
  1165. }
  1166. }
  1167. private static void GenerateVectorInsert16(CodeGenContext context, Operation operation)
  1168. {
  1169. Operand dest = operation.Destination;
  1170. Operand src1 = operation.GetSource(0); //Vector
  1171. Operand src2 = operation.GetSource(1); //Value
  1172. Operand src3 = operation.GetSource(2); //Index
  1173. if (!HardwareCapabilities.SupportsVexEncoding)
  1174. {
  1175. EnsureSameReg(dest, src1);
  1176. }
  1177. Debug.Assert(src1.Type == OperandType.V128);
  1178. Debug.Assert(src3.Kind == OperandKind.Constant);
  1179. byte index = src3.AsByte();
  1180. context.Assembler.Pinsrw(dest, src1, src2, index);
  1181. }
  1182. private static void GenerateVectorInsert8(CodeGenContext context, Operation operation)
  1183. {
  1184. Operand dest = operation.Destination;
  1185. Operand src1 = operation.GetSource(0); //Vector
  1186. Operand src2 = operation.GetSource(1); //Value
  1187. Operand src3 = operation.GetSource(2); //Index
  1188. // It's not possible to emulate this instruction without
  1189. // SSE 4.1 support without the use of a temporary register,
  1190. // so we instead handle that case on the pre-allocator when
  1191. // SSE 4.1 is not supported on the CPU.
  1192. Debug.Assert(HardwareCapabilities.SupportsSse41);
  1193. if (!HardwareCapabilities.SupportsVexEncoding)
  1194. {
  1195. EnsureSameReg(dest, src1);
  1196. }
  1197. Debug.Assert(src1.Type == OperandType.V128);
  1198. Debug.Assert(src3.Kind == OperandKind.Constant);
  1199. byte index = src3.AsByte();
  1200. context.Assembler.Pinsrb(dest, src1, src2, index);
  1201. }
  1202. private static void GenerateVectorOne(CodeGenContext context, Operation operation)
  1203. {
  1204. Operand dest = operation.Destination;
  1205. Debug.Assert(!dest.Type.IsInteger());
  1206. context.Assembler.Pcmpeqw(dest, dest, dest);
  1207. }
  1208. private static void GenerateVectorZero(CodeGenContext context, Operation operation)
  1209. {
  1210. Operand dest = operation.Destination;
  1211. Debug.Assert(!dest.Type.IsInteger());
  1212. context.Assembler.Xorps(dest, dest, dest);
  1213. }
  1214. private static void GenerateVectorZeroUpper64(CodeGenContext context, Operation operation)
  1215. {
  1216. Operand dest = operation.Destination;
  1217. Operand source = operation.GetSource(0);
  1218. Debug.Assert(dest.Type == OperandType.V128 && source.Type == OperandType.V128);
  1219. GenerateZeroUpper64(context, dest, source);
  1220. }
  1221. private static void GenerateVectorZeroUpper96(CodeGenContext context, Operation operation)
  1222. {
  1223. Operand dest = operation.Destination;
  1224. Operand source = operation.GetSource(0);
  1225. Debug.Assert(dest.Type == OperandType.V128 && source.Type == OperandType.V128);
  1226. GenerateZeroUpper96(context, dest, source);
  1227. }
  1228. private static void GenerateZeroExtend16(CodeGenContext context, Operation operation)
  1229. {
  1230. Operand dest = operation.Destination;
  1231. Operand source = operation.GetSource(0);
  1232. Debug.Assert(dest.Type.IsInteger() && source.Type.IsInteger());
  1233. context.Assembler.Movzx16(dest, source, OperandType.I32);
  1234. }
  1235. private static void GenerateZeroExtend32(CodeGenContext context, Operation operation)
  1236. {
  1237. Operand dest = operation.Destination;
  1238. Operand source = operation.GetSource(0);
  1239. Debug.Assert(dest.Type.IsInteger() && source.Type.IsInteger());
  1240. // We can eliminate the move if source is already 32-bit and the registers are the same.
  1241. if (dest.Value == source.Value && source.Type == OperandType.I32)
  1242. {
  1243. return;
  1244. }
  1245. context.Assembler.Mov(dest, source, OperandType.I32);
  1246. }
  1247. private static void GenerateZeroExtend8(CodeGenContext context, Operation operation)
  1248. {
  1249. Operand dest = operation.Destination;
  1250. Operand source = operation.GetSource(0);
  1251. Debug.Assert(dest.Type.IsInteger() && source.Type.IsInteger());
  1252. context.Assembler.Movzx8(dest, source, OperandType.I32);
  1253. }
  1254. private static void GenerateLoad(CodeGenContext context, Operand address, Operand value)
  1255. {
  1256. switch (value.Type)
  1257. {
  1258. case OperandType.I32:
  1259. context.Assembler.Mov(value, address, OperandType.I32);
  1260. break;
  1261. case OperandType.I64:
  1262. context.Assembler.Mov(value, address, OperandType.I64);
  1263. break;
  1264. case OperandType.FP32:
  1265. context.Assembler.Movd(value, address);
  1266. break;
  1267. case OperandType.FP64:
  1268. context.Assembler.Movq(value, address);
  1269. break;
  1270. case OperandType.V128:
  1271. context.Assembler.Movdqu(value, address);
  1272. break;
  1273. default:
  1274. Debug.Assert(false);
  1275. break;
  1276. }
  1277. }
  1278. private static void GenerateStore(CodeGenContext context, Operand address, Operand value)
  1279. {
  1280. switch (value.Type)
  1281. {
  1282. case OperandType.I32:
  1283. context.Assembler.Mov(address, value, OperandType.I32);
  1284. break;
  1285. case OperandType.I64:
  1286. context.Assembler.Mov(address, value, OperandType.I64);
  1287. break;
  1288. case OperandType.FP32:
  1289. context.Assembler.Movd(address, value);
  1290. break;
  1291. case OperandType.FP64:
  1292. context.Assembler.Movq(address, value);
  1293. break;
  1294. case OperandType.V128:
  1295. context.Assembler.Movdqu(address, value);
  1296. break;
  1297. default:
  1298. Debug.Assert(false);
  1299. break;
  1300. }
  1301. }
  1302. private static void GenerateZeroUpper64(CodeGenContext context, Operand dest, Operand source)
  1303. {
  1304. context.Assembler.Movq(dest, source);
  1305. }
  1306. private static void GenerateZeroUpper96(CodeGenContext context, Operand dest, Operand source)
  1307. {
  1308. context.Assembler.Movq(dest, source);
  1309. context.Assembler.Pshufd(dest, dest, 0xfc);
  1310. }
  1311. private static bool MatchOperation(Operation node, Instruction inst, OperandType destType, Register destReg)
  1312. {
  1313. if (node == default || node.DestinationsCount == 0)
  1314. {
  1315. return false;
  1316. }
  1317. if (node.Instruction != inst)
  1318. {
  1319. return false;
  1320. }
  1321. Operand dest = node.Destination;
  1322. return dest.Kind == OperandKind.Register &&
  1323. dest.Type == destType &&
  1324. dest.GetRegister() == destReg;
  1325. }
  1326. [Conditional("DEBUG")]
  1327. private static void ValidateUnOp(Operand dest, Operand source)
  1328. {
  1329. EnsureSameReg(dest, source);
  1330. EnsureSameType(dest, source);
  1331. }
  1332. [Conditional("DEBUG")]
  1333. private static void ValidateBinOp(Operand dest, Operand src1, Operand src2)
  1334. {
  1335. EnsureSameReg(dest, src1);
  1336. EnsureSameType(dest, src1, src2);
  1337. }
  1338. [Conditional("DEBUG")]
  1339. private static void ValidateShift(Operand dest, Operand src1, Operand src2)
  1340. {
  1341. EnsureSameReg(dest, src1);
  1342. EnsureSameType(dest, src1);
  1343. Debug.Assert(dest.Type.IsInteger() && src2.Type == OperandType.I32);
  1344. }
  1345. private static void EnsureSameReg(Operand op1, Operand op2)
  1346. {
  1347. if (!op1.Type.IsInteger() && HardwareCapabilities.SupportsVexEncoding)
  1348. {
  1349. return;
  1350. }
  1351. Debug.Assert(op1.Kind == OperandKind.Register || op1.Kind == OperandKind.Memory);
  1352. Debug.Assert(op1.Kind == op2.Kind);
  1353. Debug.Assert(op1.Value == op2.Value);
  1354. }
  1355. private static void EnsureSameType(Operand op1, Operand op2)
  1356. {
  1357. Debug.Assert(op1.Type == op2.Type);
  1358. }
  1359. private static void EnsureSameType(Operand op1, Operand op2, Operand op3)
  1360. {
  1361. Debug.Assert(op1.Type == op2.Type);
  1362. Debug.Assert(op1.Type == op3.Type);
  1363. }
  1364. private static void EnsureSameType(Operand op1, Operand op2, Operand op3, Operand op4)
  1365. {
  1366. Debug.Assert(op1.Type == op2.Type);
  1367. Debug.Assert(op1.Type == op3.Type);
  1368. Debug.Assert(op1.Type == op4.Type);
  1369. }
  1370. private static UnwindInfo WritePrologue(CodeGenContext context)
  1371. {
  1372. List<UnwindPushEntry> pushEntries = new();
  1373. Operand rsp = Register(X86Register.Rsp);
  1374. int mask = CallingConvention.GetIntCalleeSavedRegisters() & context.AllocResult.IntUsedRegisters;
  1375. while (mask != 0)
  1376. {
  1377. int bit = BitOperations.TrailingZeroCount(mask);
  1378. context.Assembler.Push(Register((X86Register)bit));
  1379. pushEntries.Add(new UnwindPushEntry(UnwindPseudoOp.PushReg, context.StreamOffset, regIndex: bit));
  1380. mask &= ~(1 << bit);
  1381. }
  1382. int reservedStackSize = context.CallArgsRegionSize + context.AllocResult.SpillRegionSize;
  1383. reservedStackSize += context.XmmSaveRegionSize;
  1384. if (reservedStackSize >= StackGuardSize)
  1385. {
  1386. GenerateInlineStackProbe(context, reservedStackSize);
  1387. }
  1388. if (reservedStackSize != 0)
  1389. {
  1390. context.Assembler.Sub(rsp, Const(reservedStackSize), OperandType.I64);
  1391. pushEntries.Add(new UnwindPushEntry(UnwindPseudoOp.AllocStack, context.StreamOffset, stackOffsetOrAllocSize: reservedStackSize));
  1392. }
  1393. int offset = reservedStackSize;
  1394. mask = CallingConvention.GetVecCalleeSavedRegisters() & context.AllocResult.VecUsedRegisters;
  1395. while (mask != 0)
  1396. {
  1397. int bit = BitOperations.TrailingZeroCount(mask);
  1398. offset -= 16;
  1399. Operand memOp = MemoryOp(OperandType.V128, rsp, default, Multiplier.x1, offset);
  1400. context.Assembler.Movdqu(memOp, Xmm((X86Register)bit));
  1401. pushEntries.Add(new UnwindPushEntry(UnwindPseudoOp.SaveXmm128, context.StreamOffset, bit, offset));
  1402. mask &= ~(1 << bit);
  1403. }
  1404. return new UnwindInfo(pushEntries.ToArray(), context.StreamOffset);
  1405. }
  1406. private static void WriteEpilogue(CodeGenContext context)
  1407. {
  1408. Operand rsp = Register(X86Register.Rsp);
  1409. int reservedStackSize = context.CallArgsRegionSize + context.AllocResult.SpillRegionSize;
  1410. reservedStackSize += context.XmmSaveRegionSize;
  1411. int offset = reservedStackSize;
  1412. int mask = CallingConvention.GetVecCalleeSavedRegisters() & context.AllocResult.VecUsedRegisters;
  1413. while (mask != 0)
  1414. {
  1415. int bit = BitOperations.TrailingZeroCount(mask);
  1416. offset -= 16;
  1417. Operand memOp = MemoryOp(OperandType.V128, rsp, default, Multiplier.x1, offset);
  1418. context.Assembler.Movdqu(Xmm((X86Register)bit), memOp);
  1419. mask &= ~(1 << bit);
  1420. }
  1421. if (reservedStackSize != 0)
  1422. {
  1423. context.Assembler.Add(rsp, Const(reservedStackSize), OperandType.I64);
  1424. }
  1425. mask = CallingConvention.GetIntCalleeSavedRegisters() & context.AllocResult.IntUsedRegisters;
  1426. while (mask != 0)
  1427. {
  1428. int bit = BitUtils.HighestBitSet(mask);
  1429. context.Assembler.Pop(Register((X86Register)bit));
  1430. mask &= ~(1 << bit);
  1431. }
  1432. }
  1433. private static void GenerateInlineStackProbe(CodeGenContext context, int size)
  1434. {
  1435. // Windows does lazy stack allocation, and there are just 2
  1436. // guard pages on the end of the stack. So, if the allocation
  1437. // size we make is greater than this guard size, we must ensure
  1438. // that the OS will map all pages that we'll use. We do that by
  1439. // doing a dummy read on those pages, forcing a page fault and
  1440. // the OS to map them. If they are already mapped, nothing happens.
  1441. const int PageMask = PageSize - 1;
  1442. size = (size + PageMask) & ~PageMask;
  1443. Operand rsp = Register(X86Register.Rsp);
  1444. Operand temp = Register(CallingConvention.GetIntReturnRegister());
  1445. for (int offset = PageSize; offset < size; offset += PageSize)
  1446. {
  1447. Operand memOp = MemoryOp(OperandType.I32, rsp, default, Multiplier.x1, -offset);
  1448. context.Assembler.Mov(temp, memOp, OperandType.I32);
  1449. }
  1450. }
  1451. private static Operand Memory(Operand operand, OperandType type)
  1452. {
  1453. if (operand.Kind == OperandKind.Memory)
  1454. {
  1455. return operand;
  1456. }
  1457. return MemoryOp(type, operand);
  1458. }
  1459. private static Operand Register(X86Register register, OperandType type = OperandType.I64)
  1460. {
  1461. return Operand.Factory.Register((int)register, RegisterType.Integer, type);
  1462. }
  1463. private static Operand Xmm(X86Register register)
  1464. {
  1465. return Operand.Factory.Register((int)register, RegisterType.Vector, OperandType.V128);
  1466. }
  1467. }
  1468. }