CodeGenerator.cs 67 KB

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