CodeGenerator.cs 67 KB

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