CodeGenerator.cs 66 KB

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