CodeGenerator.cs 67 KB

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