CodeGenerator.cs 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865
  1. using ARMeilleure.CodeGen.Linking;
  2. using ARMeilleure.CodeGen.Optimizations;
  3. using ARMeilleure.CodeGen.RegisterAllocators;
  4. using ARMeilleure.CodeGen.Unwinding;
  5. using ARMeilleure.Common;
  6. using ARMeilleure.Diagnostics;
  7. using ARMeilleure.IntermediateRepresentation;
  8. using ARMeilleure.Translation;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Diagnostics;
  12. using System.Numerics;
  13. using static ARMeilleure.IntermediateRepresentation.Operand.Factory;
  14. namespace ARMeilleure.CodeGen.X86
  15. {
  16. static class CodeGenerator
  17. {
  18. private const int RegistersCount = 16;
  19. private const int PageSize = 0x1000;
  20. private const int StackGuardSize = 0x2000;
  21. private static readonly Action<CodeGenContext, Operation>[] _instTable;
  22. static CodeGenerator()
  23. {
  24. _instTable = new Action<CodeGenContext, Operation>[EnumUtils.GetCount(typeof(Instruction))];
  25. Add(Instruction.Add, GenerateAdd);
  26. Add(Instruction.BitwiseAnd, GenerateBitwiseAnd);
  27. Add(Instruction.BitwiseExclusiveOr, GenerateBitwiseExclusiveOr);
  28. Add(Instruction.BitwiseNot, GenerateBitwiseNot);
  29. Add(Instruction.BitwiseOr, GenerateBitwiseOr);
  30. Add(Instruction.BranchIf, GenerateBranchIf);
  31. Add(Instruction.ByteSwap, GenerateByteSwap);
  32. Add(Instruction.Call, GenerateCall);
  33. Add(Instruction.Clobber, GenerateClobber);
  34. Add(Instruction.Compare, GenerateCompare);
  35. Add(Instruction.CompareAndSwap, GenerateCompareAndSwap);
  36. Add(Instruction.CompareAndSwap16, GenerateCompareAndSwap16);
  37. Add(Instruction.CompareAndSwap8, GenerateCompareAndSwap8);
  38. Add(Instruction.ConditionalSelect, GenerateConditionalSelect);
  39. Add(Instruction.ConvertI64ToI32, GenerateConvertI64ToI32);
  40. Add(Instruction.ConvertToFP, GenerateConvertToFP);
  41. Add(Instruction.Copy, GenerateCopy);
  42. Add(Instruction.CountLeadingZeros, GenerateCountLeadingZeros);
  43. Add(Instruction.Divide, GenerateDivide);
  44. Add(Instruction.DivideUI, GenerateDivideUI);
  45. Add(Instruction.Fill, GenerateFill);
  46. Add(Instruction.Load, GenerateLoad);
  47. Add(Instruction.Load16, GenerateLoad16);
  48. Add(Instruction.Load8, GenerateLoad8);
  49. Add(Instruction.MemoryBarrier, GenerateMemoryBarrier);
  50. Add(Instruction.Multiply, GenerateMultiply);
  51. Add(Instruction.Multiply64HighSI, GenerateMultiply64HighSI);
  52. Add(Instruction.Multiply64HighUI, GenerateMultiply64HighUI);
  53. Add(Instruction.Negate, GenerateNegate);
  54. Add(Instruction.Return, GenerateReturn);
  55. Add(Instruction.RotateRight, GenerateRotateRight);
  56. Add(Instruction.ShiftLeft, GenerateShiftLeft);
  57. Add(Instruction.ShiftRightSI, GenerateShiftRightSI);
  58. Add(Instruction.ShiftRightUI, GenerateShiftRightUI);
  59. Add(Instruction.SignExtend16, GenerateSignExtend16);
  60. Add(Instruction.SignExtend32, GenerateSignExtend32);
  61. Add(Instruction.SignExtend8, GenerateSignExtend8);
  62. Add(Instruction.Spill, GenerateSpill);
  63. Add(Instruction.SpillArg, GenerateSpillArg);
  64. Add(Instruction.StackAlloc, GenerateStackAlloc);
  65. Add(Instruction.Store, GenerateStore);
  66. Add(Instruction.Store16, GenerateStore16);
  67. Add(Instruction.Store8, GenerateStore8);
  68. Add(Instruction.Subtract, GenerateSubtract);
  69. Add(Instruction.Tailcall, GenerateTailcall);
  70. Add(Instruction.VectorCreateScalar, GenerateVectorCreateScalar);
  71. Add(Instruction.VectorExtract, GenerateVectorExtract);
  72. Add(Instruction.VectorExtract16, GenerateVectorExtract16);
  73. Add(Instruction.VectorExtract8, GenerateVectorExtract8);
  74. Add(Instruction.VectorInsert, GenerateVectorInsert);
  75. Add(Instruction.VectorInsert16, GenerateVectorInsert16);
  76. Add(Instruction.VectorInsert8, GenerateVectorInsert8);
  77. Add(Instruction.VectorOne, GenerateVectorOne);
  78. Add(Instruction.VectorZero, GenerateVectorZero);
  79. Add(Instruction.VectorZeroUpper64, GenerateVectorZeroUpper64);
  80. Add(Instruction.VectorZeroUpper96, GenerateVectorZeroUpper96);
  81. Add(Instruction.ZeroExtend16, GenerateZeroExtend16);
  82. Add(Instruction.ZeroExtend32, GenerateZeroExtend32);
  83. Add(Instruction.ZeroExtend8, GenerateZeroExtend8);
  84. static void Add(Instruction inst, Action<CodeGenContext, Operation> func)
  85. {
  86. _instTable[(int)inst] = func;
  87. }
  88. }
  89. public static CompiledFunction Generate(CompilerContext cctx)
  90. {
  91. ControlFlowGraph cfg = cctx.Cfg;
  92. Logger.StartPass(PassName.Optimization);
  93. if (cctx.Options.HasFlag(CompilerOptions.Optimize))
  94. {
  95. if (cctx.Options.HasFlag(CompilerOptions.SsaForm))
  96. {
  97. Optimizer.RunPass(cfg);
  98. }
  99. BlockPlacement.RunPass(cfg);
  100. }
  101. X86Optimizer.RunPass(cfg);
  102. Logger.EndPass(PassName.Optimization, cfg);
  103. Logger.StartPass(PassName.PreAllocation);
  104. StackAllocator stackAlloc = new();
  105. PreAllocator.RunPass(cctx, stackAlloc, out int maxCallArgs);
  106. Logger.EndPass(PassName.PreAllocation, cfg);
  107. Logger.StartPass(PassName.RegisterAllocation);
  108. if (cctx.Options.HasFlag(CompilerOptions.SsaForm))
  109. {
  110. Ssa.Deconstruct(cfg);
  111. }
  112. IRegisterAllocator regAlloc;
  113. if (cctx.Options.HasFlag(CompilerOptions.Lsra))
  114. {
  115. regAlloc = new LinearScanAllocator();
  116. }
  117. else
  118. {
  119. regAlloc = new HybridAllocator();
  120. }
  121. RegisterMasks regMasks = new(
  122. CallingConvention.GetIntAvailableRegisters(),
  123. CallingConvention.GetVecAvailableRegisters(),
  124. CallingConvention.GetIntCallerSavedRegisters(),
  125. CallingConvention.GetVecCallerSavedRegisters(),
  126. CallingConvention.GetIntCalleeSavedRegisters(),
  127. CallingConvention.GetVecCalleeSavedRegisters(),
  128. RegistersCount);
  129. AllocationResult allocResult = regAlloc.RunPass(cfg, stackAlloc, regMasks);
  130. Logger.EndPass(PassName.RegisterAllocation, cfg);
  131. Logger.StartPass(PassName.CodeGeneration);
  132. bool relocatable = (cctx.Options & CompilerOptions.Relocatable) != 0;
  133. CodeGenContext context = new(allocResult, maxCallArgs, cfg.Blocks.Count, relocatable);
  134. UnwindInfo unwindInfo = WritePrologue(context);
  135. for (BasicBlock block = cfg.Blocks.First; block != null; block = block.ListNext)
  136. {
  137. context.EnterBlock(block);
  138. for (Operation node = block.Operations.First; node != default; node = node.ListNext)
  139. {
  140. GenerateOperation(context, node);
  141. }
  142. if (block.SuccessorsCount == 0)
  143. {
  144. // The only blocks which can have 0 successors are exit blocks.
  145. Operation last = block.Operations.Last;
  146. Debug.Assert(last.Instruction == Instruction.Tailcall ||
  147. last.Instruction == Instruction.Return);
  148. }
  149. else
  150. {
  151. BasicBlock succ = block.GetSuccessor(0);
  152. if (succ != block.ListNext)
  153. {
  154. context.JumpTo(succ);
  155. }
  156. }
  157. }
  158. (byte[] code, RelocInfo relocInfo) = context.Assembler.GetCode();
  159. Logger.EndPass(PassName.CodeGeneration);
  160. return new CompiledFunction(code, unwindInfo, relocInfo);
  161. }
  162. private static void GenerateOperation(CodeGenContext context, Operation operation)
  163. {
  164. if (operation.Instruction == Instruction.Extended)
  165. {
  166. IntrinsicInfo info = IntrinsicTable.GetInfo(operation.Intrinsic);
  167. switch (info.Type)
  168. {
  169. case IntrinsicType.Comis_:
  170. {
  171. Operand dest = operation.Destination;
  172. Operand src1 = operation.GetSource(0);
  173. Operand src2 = operation.GetSource(1);
  174. switch (operation.Intrinsic)
  175. {
  176. case Intrinsic.X86Comisdeq:
  177. context.Assembler.Comisd(src1, src2);
  178. context.Assembler.Setcc(dest, X86Condition.Equal);
  179. break;
  180. case Intrinsic.X86Comisdge:
  181. context.Assembler.Comisd(src1, src2);
  182. context.Assembler.Setcc(dest, X86Condition.AboveOrEqual);
  183. break;
  184. case Intrinsic.X86Comisdlt:
  185. context.Assembler.Comisd(src1, src2);
  186. context.Assembler.Setcc(dest, X86Condition.Below);
  187. break;
  188. case Intrinsic.X86Comisseq:
  189. context.Assembler.Comiss(src1, src2);
  190. context.Assembler.Setcc(dest, X86Condition.Equal);
  191. break;
  192. case Intrinsic.X86Comissge:
  193. context.Assembler.Comiss(src1, src2);
  194. context.Assembler.Setcc(dest, X86Condition.AboveOrEqual);
  195. break;
  196. case Intrinsic.X86Comisslt:
  197. context.Assembler.Comiss(src1, src2);
  198. context.Assembler.Setcc(dest, X86Condition.Below);
  199. break;
  200. }
  201. context.Assembler.Movzx8(dest, dest, OperandType.I32);
  202. break;
  203. }
  204. case IntrinsicType.Mxcsr:
  205. {
  206. Operand offset = operation.GetSource(0);
  207. Debug.Assert(offset.Kind == OperandKind.Constant);
  208. Debug.Assert(offset.Type == OperandType.I32);
  209. int offs = offset.AsInt32() + context.CallArgsRegionSize;
  210. Operand rsp = Register(X86Register.Rsp);
  211. Operand memOp = MemoryOp(OperandType.I32, rsp, default, Multiplier.x1, offs);
  212. Debug.Assert(HardwareCapabilities.SupportsSse || HardwareCapabilities.SupportsVexEncoding);
  213. if (operation.Intrinsic == Intrinsic.X86Ldmxcsr)
  214. {
  215. Operand bits = operation.GetSource(1);
  216. Debug.Assert(bits.Type == OperandType.I32);
  217. context.Assembler.Mov(memOp, bits, OperandType.I32);
  218. context.Assembler.Ldmxcsr(memOp);
  219. }
  220. else if (operation.Intrinsic == Intrinsic.X86Stmxcsr)
  221. {
  222. Operand dest = operation.Destination;
  223. Debug.Assert(dest.Type == OperandType.I32);
  224. context.Assembler.Stmxcsr(memOp);
  225. context.Assembler.Mov(dest, memOp, OperandType.I32);
  226. }
  227. break;
  228. }
  229. case IntrinsicType.PopCount:
  230. {
  231. Operand dest = operation.Destination;
  232. Operand source = operation.GetSource(0);
  233. EnsureSameType(dest, source);
  234. Debug.Assert(dest.Type.IsInteger());
  235. context.Assembler.Popcnt(dest, source, dest.Type);
  236. break;
  237. }
  238. case IntrinsicType.Unary:
  239. {
  240. Operand dest = operation.Destination;
  241. Operand source = operation.GetSource(0);
  242. EnsureSameType(dest, source);
  243. Debug.Assert(!dest.Type.IsInteger());
  244. context.Assembler.WriteInstruction(info.Inst, dest, source);
  245. break;
  246. }
  247. case IntrinsicType.UnaryToGpr:
  248. {
  249. Operand dest = operation.Destination;
  250. Operand source = operation.GetSource(0);
  251. Debug.Assert(dest.Type.IsInteger() && !source.Type.IsInteger());
  252. if (operation.Intrinsic == Intrinsic.X86Cvtsi2si)
  253. {
  254. if (dest.Type == OperandType.I32)
  255. {
  256. context.Assembler.Movd(dest, source); // int _mm_cvtsi128_si32(__m128i a)
  257. }
  258. else /* if (dest.Type == OperandType.I64) */
  259. {
  260. context.Assembler.Movq(dest, source); // __int64 _mm_cvtsi128_si64(__m128i a)
  261. }
  262. }
  263. else
  264. {
  265. context.Assembler.WriteInstruction(info.Inst, dest, source, dest.Type);
  266. }
  267. break;
  268. }
  269. case IntrinsicType.Binary:
  270. {
  271. Operand dest = operation.Destination;
  272. Operand src1 = operation.GetSource(0);
  273. Operand src2 = operation.GetSource(1);
  274. EnsureSameType(dest, src1);
  275. if (!HardwareCapabilities.SupportsVexEncoding)
  276. {
  277. EnsureSameReg(dest, src1);
  278. }
  279. Debug.Assert(!dest.Type.IsInteger());
  280. Debug.Assert(!src2.Type.IsInteger() || src2.Kind == OperandKind.Constant);
  281. context.Assembler.WriteInstruction(info.Inst, dest, src1, src2);
  282. break;
  283. }
  284. case IntrinsicType.BinaryGpr:
  285. {
  286. Operand dest = operation.Destination;
  287. Operand src1 = operation.GetSource(0);
  288. Operand src2 = operation.GetSource(1);
  289. EnsureSameType(dest, src1);
  290. if (!HardwareCapabilities.SupportsVexEncoding)
  291. {
  292. EnsureSameReg(dest, src1);
  293. }
  294. Debug.Assert(!dest.Type.IsInteger() && src2.Type.IsInteger());
  295. context.Assembler.WriteInstruction(info.Inst, dest, src1, src2, src2.Type);
  296. break;
  297. }
  298. case IntrinsicType.Crc32:
  299. {
  300. Operand dest = operation.Destination;
  301. Operand src1 = operation.GetSource(0);
  302. Operand src2 = operation.GetSource(1);
  303. EnsureSameReg(dest, src1);
  304. Debug.Assert(dest.Type.IsInteger() && src1.Type.IsInteger() && src2.Type.IsInteger());
  305. context.Assembler.WriteInstruction(info.Inst, dest, src2, dest.Type);
  306. break;
  307. }
  308. case IntrinsicType.BinaryImm:
  309. {
  310. Operand dest = operation.Destination;
  311. Operand src1 = operation.GetSource(0);
  312. Operand src2 = operation.GetSource(1);
  313. EnsureSameType(dest, src1);
  314. if (!HardwareCapabilities.SupportsVexEncoding)
  315. {
  316. EnsureSameReg(dest, src1);
  317. }
  318. Debug.Assert(!dest.Type.IsInteger() && src2.Kind == OperandKind.Constant);
  319. context.Assembler.WriteInstruction(info.Inst, dest, src1, src2.AsByte());
  320. break;
  321. }
  322. case IntrinsicType.Ternary:
  323. {
  324. Operand dest = operation.Destination;
  325. Operand src1 = operation.GetSource(0);
  326. Operand src2 = operation.GetSource(1);
  327. Operand src3 = operation.GetSource(2);
  328. EnsureSameType(dest, src1, src2, src3);
  329. Debug.Assert(!dest.Type.IsInteger());
  330. if (info.Inst == X86Instruction.Blendvpd && HardwareCapabilities.SupportsVexEncoding)
  331. {
  332. context.Assembler.WriteInstruction(X86Instruction.Vblendvpd, dest, src1, src2, src3);
  333. }
  334. else if (info.Inst == X86Instruction.Blendvps && HardwareCapabilities.SupportsVexEncoding)
  335. {
  336. context.Assembler.WriteInstruction(X86Instruction.Vblendvps, dest, src1, src2, src3);
  337. }
  338. else if (info.Inst == X86Instruction.Pblendvb && HardwareCapabilities.SupportsVexEncoding)
  339. {
  340. context.Assembler.WriteInstruction(X86Instruction.Vpblendvb, dest, src1, src2, src3);
  341. }
  342. else
  343. {
  344. EnsureSameReg(dest, src1);
  345. Debug.Assert(src3.GetRegister().Index == 0);
  346. context.Assembler.WriteInstruction(info.Inst, dest, src1, src2);
  347. }
  348. break;
  349. }
  350. case IntrinsicType.TernaryImm:
  351. {
  352. Operand dest = operation.Destination;
  353. Operand src1 = operation.GetSource(0);
  354. Operand src2 = operation.GetSource(1);
  355. Operand src3 = operation.GetSource(2);
  356. EnsureSameType(dest, src1, src2);
  357. if (!HardwareCapabilities.SupportsVexEncoding)
  358. {
  359. EnsureSameReg(dest, src1);
  360. }
  361. Debug.Assert(!dest.Type.IsInteger() && src3.Kind == OperandKind.Constant);
  362. context.Assembler.WriteInstruction(info.Inst, dest, src1, src2, src3.AsByte());
  363. break;
  364. }
  365. case IntrinsicType.Fma:
  366. {
  367. Operand dest = operation.Destination;
  368. Operand src1 = operation.GetSource(0);
  369. Operand src2 = operation.GetSource(1);
  370. Operand src3 = operation.GetSource(2);
  371. Debug.Assert(HardwareCapabilities.SupportsVexEncoding);
  372. Debug.Assert(dest.Kind == OperandKind.Register && src1.Kind == OperandKind.Register && src2.Kind == OperandKind.Register);
  373. Debug.Assert(src3.Kind == OperandKind.Register || src3.Kind == OperandKind.Memory);
  374. EnsureSameType(dest, src1, src2, src3);
  375. Debug.Assert(dest.Type == OperandType.V128);
  376. Debug.Assert(dest.Value == src1.Value);
  377. context.Assembler.WriteInstruction(info.Inst, dest, src2, src3);
  378. break;
  379. }
  380. }
  381. }
  382. else
  383. {
  384. Action<CodeGenContext, Operation> func = _instTable[(int)operation.Instruction];
  385. if (func != null)
  386. {
  387. func(context, operation);
  388. }
  389. else
  390. {
  391. throw new ArgumentException($"Invalid instruction \"{operation.Instruction}\".");
  392. }
  393. }
  394. }
  395. private static void GenerateAdd(CodeGenContext context, Operation operation)
  396. {
  397. Operand dest = operation.Destination;
  398. Operand src1 = operation.GetSource(0);
  399. Operand src2 = operation.GetSource(1);
  400. if (dest.Type.IsInteger())
  401. {
  402. // If Destination and Source 1 Operands are the same, perform a standard add as there are no benefits to using LEA.
  403. if (dest.Kind == src1.Kind && dest.Value == src1.Value)
  404. {
  405. ValidateBinOp(dest, src1, src2);
  406. context.Assembler.Add(dest, src2, dest.Type);
  407. }
  408. else
  409. {
  410. EnsureSameType(dest, src1, src2);
  411. int offset;
  412. Operand index;
  413. if (src2.Kind == OperandKind.Constant)
  414. {
  415. offset = src2.AsInt32();
  416. index = default;
  417. }
  418. else
  419. {
  420. offset = 0;
  421. index = src2;
  422. }
  423. Operand memOp = MemoryOp(dest.Type, src1, index, Multiplier.x1, offset);
  424. context.Assembler.Lea(dest, memOp, dest.Type);
  425. }
  426. }
  427. else
  428. {
  429. ValidateBinOp(dest, src1, src2);
  430. if (dest.Type == OperandType.FP32)
  431. {
  432. context.Assembler.Addss(dest, src1, src2);
  433. }
  434. else /* if (dest.Type == OperandType.FP64) */
  435. {
  436. context.Assembler.Addsd(dest, src1, src2);
  437. }
  438. }
  439. }
  440. private static void GenerateBitwiseAnd(CodeGenContext context, Operation operation)
  441. {
  442. Operand dest = operation.Destination;
  443. Operand src1 = operation.GetSource(0);
  444. Operand src2 = operation.GetSource(1);
  445. ValidateBinOp(dest, src1, src2);
  446. Debug.Assert(dest.Type.IsInteger());
  447. // Note: GenerateCompareCommon makes the assumption that BitwiseAnd will emit only a single `and`
  448. // instruction.
  449. context.Assembler.And(dest, src2, dest.Type);
  450. }
  451. private static void GenerateBitwiseExclusiveOr(CodeGenContext context, Operation operation)
  452. {
  453. Operand dest = operation.Destination;
  454. Operand src1 = operation.GetSource(0);
  455. Operand src2 = operation.GetSource(1);
  456. ValidateBinOp(dest, src1, src2);
  457. if (dest.Type.IsInteger())
  458. {
  459. context.Assembler.Xor(dest, src2, dest.Type);
  460. }
  461. else
  462. {
  463. context.Assembler.Xorps(dest, src1, src2);
  464. }
  465. }
  466. private static void GenerateBitwiseNot(CodeGenContext context, Operation operation)
  467. {
  468. Operand dest = operation.Destination;
  469. Operand source = operation.GetSource(0);
  470. ValidateUnOp(dest, source);
  471. Debug.Assert(dest.Type.IsInteger());
  472. context.Assembler.Not(dest);
  473. }
  474. private static void GenerateBitwiseOr(CodeGenContext context, Operation operation)
  475. {
  476. Operand dest = operation.Destination;
  477. Operand src1 = operation.GetSource(0);
  478. Operand src2 = operation.GetSource(1);
  479. ValidateBinOp(dest, src1, src2);
  480. Debug.Assert(dest.Type.IsInteger());
  481. context.Assembler.Or(dest, src2, dest.Type);
  482. }
  483. private static void GenerateBranchIf(CodeGenContext context, Operation operation)
  484. {
  485. Operand comp = operation.GetSource(2);
  486. Debug.Assert(comp.Kind == OperandKind.Constant);
  487. var cond = ((Comparison)comp.AsInt32()).ToX86Condition();
  488. GenerateCompareCommon(context, operation);
  489. context.JumpTo(cond, context.CurrBlock.GetSuccessor(1));
  490. }
  491. private static void GenerateByteSwap(CodeGenContext context, Operation operation)
  492. {
  493. Operand dest = operation.Destination;
  494. Operand source = operation.GetSource(0);
  495. ValidateUnOp(dest, source);
  496. Debug.Assert(dest.Type.IsInteger());
  497. context.Assembler.Bswap(dest);
  498. }
  499. private static void GenerateCall(CodeGenContext context, Operation operation)
  500. {
  501. context.Assembler.Call(operation.GetSource(0));
  502. }
  503. private static void GenerateClobber(CodeGenContext context, Operation operation)
  504. {
  505. // This is only used to indicate that a register is clobbered to the
  506. // register allocator, we don't need to produce any code.
  507. }
  508. private static void GenerateCompare(CodeGenContext context, Operation operation)
  509. {
  510. Operand dest = operation.Destination;
  511. Operand comp = operation.GetSource(2);
  512. Debug.Assert(dest.Type == OperandType.I32);
  513. Debug.Assert(comp.Kind == OperandKind.Constant);
  514. var cond = ((Comparison)comp.AsInt32()).ToX86Condition();
  515. GenerateCompareCommon(context, operation);
  516. context.Assembler.Setcc(dest, cond);
  517. context.Assembler.Movzx8(dest, dest, OperandType.I32);
  518. }
  519. private static void GenerateCompareCommon(CodeGenContext context, Operation operation)
  520. {
  521. Operand src1 = operation.GetSource(0);
  522. Operand src2 = operation.GetSource(1);
  523. EnsureSameType(src1, src2);
  524. Debug.Assert(src1.Type.IsInteger());
  525. if (src2.Kind == OperandKind.Constant && src2.Value == 0)
  526. {
  527. if (MatchOperation(operation.ListPrevious, Instruction.BitwiseAnd, src1.Type, src1.GetRegister()))
  528. {
  529. // Since the `test` and `and` instruction set the status flags in the same way, we can omit the
  530. // `test r,r` instruction when it is immediately preceded by an `and r,*` instruction.
  531. //
  532. // For example:
  533. //
  534. // and eax, 0x3
  535. // test eax, eax
  536. // jz .L0
  537. //
  538. // =>
  539. //
  540. // and eax, 0x3
  541. // jz .L0
  542. }
  543. else
  544. {
  545. context.Assembler.Test(src1, src1, src1.Type);
  546. }
  547. }
  548. else
  549. {
  550. context.Assembler.Cmp(src1, src2, src1.Type);
  551. }
  552. }
  553. private static void GenerateCompareAndSwap(CodeGenContext context, Operation operation)
  554. {
  555. Operand src1 = operation.GetSource(0);
  556. if (operation.SourcesCount == 5) // CompareAndSwap128 has 5 sources, compared to CompareAndSwap64/32's 3.
  557. {
  558. Operand memOp = MemoryOp(OperandType.I64, src1);
  559. context.Assembler.Cmpxchg16b(memOp);
  560. }
  561. else
  562. {
  563. Operand src2 = operation.GetSource(1);
  564. Operand src3 = operation.GetSource(2);
  565. EnsureSameType(src2, src3);
  566. Operand memOp = MemoryOp(src3.Type, src1);
  567. context.Assembler.Cmpxchg(memOp, src3);
  568. }
  569. }
  570. private static void GenerateCompareAndSwap16(CodeGenContext context, Operation operation)
  571. {
  572. Operand src1 = operation.GetSource(0);
  573. Operand src2 = operation.GetSource(1);
  574. Operand src3 = operation.GetSource(2);
  575. EnsureSameType(src2, src3);
  576. Operand memOp = MemoryOp(src3.Type, src1);
  577. context.Assembler.Cmpxchg16(memOp, src3);
  578. }
  579. private static void GenerateCompareAndSwap8(CodeGenContext context, Operation operation)
  580. {
  581. Operand src1 = operation.GetSource(0);
  582. Operand src2 = operation.GetSource(1);
  583. Operand src3 = operation.GetSource(2);
  584. EnsureSameType(src2, src3);
  585. Operand memOp = MemoryOp(src3.Type, src1);
  586. context.Assembler.Cmpxchg8(memOp, src3);
  587. }
  588. private static void GenerateConditionalSelect(CodeGenContext context, Operation operation)
  589. {
  590. Operand dest = operation.Destination;
  591. Operand src1 = operation.GetSource(0);
  592. Operand src2 = operation.GetSource(1);
  593. Operand src3 = operation.GetSource(2);
  594. EnsureSameReg (dest, src3);
  595. EnsureSameType(dest, src2, src3);
  596. Debug.Assert(dest.Type.IsInteger());
  597. Debug.Assert(src1.Type == OperandType.I32);
  598. context.Assembler.Test (src1, src1, src1.Type);
  599. context.Assembler.Cmovcc(dest, src2, dest.Type, X86Condition.NotEqual);
  600. }
  601. private static void GenerateConvertI64ToI32(CodeGenContext context, Operation operation)
  602. {
  603. Operand dest = operation.Destination;
  604. Operand source = operation.GetSource(0);
  605. Debug.Assert(dest.Type == OperandType.I32 && source.Type == OperandType.I64);
  606. context.Assembler.Mov(dest, source, OperandType.I32);
  607. }
  608. private static void GenerateConvertToFP(CodeGenContext context, Operation operation)
  609. {
  610. Operand dest = operation.Destination;
  611. Operand source = operation.GetSource(0);
  612. Debug.Assert(dest.Type == OperandType.FP32 || dest.Type == OperandType.FP64);
  613. if (dest.Type == OperandType.FP32)
  614. {
  615. Debug.Assert(source.Type.IsInteger() || source.Type == OperandType.FP64);
  616. if (source.Type.IsInteger())
  617. {
  618. context.Assembler.Xorps (dest, dest, dest);
  619. context.Assembler.Cvtsi2ss(dest, dest, source, source.Type);
  620. }
  621. else /* if (source.Type == OperandType.FP64) */
  622. {
  623. context.Assembler.Cvtsd2ss(dest, dest, source);
  624. GenerateZeroUpper96(context, dest, dest);
  625. }
  626. }
  627. else /* if (dest.Type == OperandType.FP64) */
  628. {
  629. Debug.Assert(source.Type.IsInteger() || source.Type == OperandType.FP32);
  630. if (source.Type.IsInteger())
  631. {
  632. context.Assembler.Xorps (dest, dest, dest);
  633. context.Assembler.Cvtsi2sd(dest, dest, source, source.Type);
  634. }
  635. else /* if (source.Type == OperandType.FP32) */
  636. {
  637. context.Assembler.Cvtss2sd(dest, dest, source);
  638. GenerateZeroUpper64(context, dest, dest);
  639. }
  640. }
  641. }
  642. private static void GenerateCopy(CodeGenContext context, Operation operation)
  643. {
  644. Operand dest = operation.Destination;
  645. Operand source = operation.GetSource(0);
  646. EnsureSameType(dest, source);
  647. Debug.Assert(dest.Type.IsInteger() || source.Kind != OperandKind.Constant);
  648. // Moves to the same register are useless.
  649. if (dest.Kind == source.Kind && dest.Value == source.Value)
  650. {
  651. return;
  652. }
  653. if (dest.Kind == OperandKind.Register &&
  654. source.Kind == OperandKind.Constant && source.Value == 0)
  655. {
  656. // Assemble "mov reg, 0" as "xor reg, reg" as the later is more efficient.
  657. context.Assembler.Xor(dest, dest, OperandType.I32);
  658. }
  659. else if (dest.Type.IsInteger())
  660. {
  661. context.Assembler.Mov(dest, source, dest.Type);
  662. }
  663. else
  664. {
  665. context.Assembler.Movdqu(dest, source);
  666. }
  667. }
  668. private static void GenerateCountLeadingZeros(CodeGenContext context, Operation operation)
  669. {
  670. Operand dest = operation.Destination;
  671. Operand source = operation.GetSource(0);
  672. EnsureSameType(dest, source);
  673. Debug.Assert(dest.Type.IsInteger());
  674. context.Assembler.Bsr(dest, source, dest.Type);
  675. int operandSize = dest.Type == OperandType.I32 ? 32 : 64;
  676. int operandMask = operandSize - 1;
  677. // When the input operand is 0, the result is undefined, however the
  678. // ZF flag is set. We are supposed to return the operand size on that
  679. // case. So, add an additional jump to handle that case, by moving the
  680. // operand size constant to the destination register.
  681. Operand neLabel = Label();
  682. context.Assembler.Jcc(X86Condition.NotEqual, neLabel);
  683. context.Assembler.Mov(dest, Const(operandSize | operandMask), OperandType.I32);
  684. context.Assembler.MarkLabel(neLabel);
  685. // BSR returns the zero based index of the last bit set on the operand,
  686. // starting from the least significant bit. However we are supposed to
  687. // return the number of 0 bits on the high end. So, we invert the result
  688. // of the BSR using XOR to get the correct value.
  689. context.Assembler.Xor(dest, Const(operandMask), OperandType.I32);
  690. }
  691. private static void GenerateDivide(CodeGenContext context, Operation operation)
  692. {
  693. Operand dest = operation.Destination;
  694. Operand dividend = operation.GetSource(0);
  695. Operand divisor = operation.GetSource(1);
  696. if (!dest.Type.IsInteger())
  697. {
  698. ValidateBinOp(dest, dividend, divisor);
  699. }
  700. if (dest.Type.IsInteger())
  701. {
  702. divisor = operation.GetSource(2);
  703. EnsureSameType(dest, divisor);
  704. if (divisor.Type == OperandType.I32)
  705. {
  706. context.Assembler.Cdq();
  707. }
  708. else
  709. {
  710. context.Assembler.Cqo();
  711. }
  712. context.Assembler.Idiv(divisor);
  713. }
  714. else if (dest.Type == OperandType.FP32)
  715. {
  716. context.Assembler.Divss(dest, dividend, divisor);
  717. }
  718. else /* if (dest.Type == OperandType.FP64) */
  719. {
  720. context.Assembler.Divsd(dest, dividend, divisor);
  721. }
  722. }
  723. private static void GenerateDivideUI(CodeGenContext context, Operation operation)
  724. {
  725. Operand divisor = operation.GetSource(2);
  726. Operand rdx = Register(X86Register.Rdx);
  727. Debug.Assert(divisor.Type.IsInteger());
  728. context.Assembler.Xor(rdx, rdx, OperandType.I32);
  729. context.Assembler.Div(divisor);
  730. }
  731. private static void GenerateFill(CodeGenContext context, Operation operation)
  732. {
  733. Operand dest = operation.Destination;
  734. Operand offset = operation.GetSource(0);
  735. Debug.Assert(offset.Kind == OperandKind.Constant);
  736. int offs = offset.AsInt32() + context.CallArgsRegionSize;
  737. Operand rsp = Register(X86Register.Rsp);
  738. Operand memOp = MemoryOp(dest.Type, rsp, default, Multiplier.x1, offs);
  739. GenerateLoad(context, memOp, dest);
  740. }
  741. private static void GenerateLoad(CodeGenContext context, Operation operation)
  742. {
  743. Operand value = operation.Destination;
  744. Operand address = Memory(operation.GetSource(0), value.Type);
  745. GenerateLoad(context, address, value);
  746. }
  747. private static void GenerateLoad16(CodeGenContext context, Operation operation)
  748. {
  749. Operand value = operation.Destination;
  750. Operand address = Memory(operation.GetSource(0), value.Type);
  751. Debug.Assert(value.Type.IsInteger());
  752. context.Assembler.Movzx16(value, address, value.Type);
  753. }
  754. private static void GenerateLoad8(CodeGenContext context, Operation operation)
  755. {
  756. Operand value = operation.Destination;
  757. Operand address = Memory(operation.GetSource(0), value.Type);
  758. Debug.Assert(value.Type.IsInteger());
  759. context.Assembler.Movzx8(value, address, value.Type);
  760. }
  761. private static void GenerateMemoryBarrier(CodeGenContext context, Operation operation)
  762. {
  763. context.Assembler.LockOr(MemoryOp(OperandType.I64, Register(X86Register.Rsp)), Const(0), OperandType.I32);
  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. Operand memOp = MemoryOp(source.Type, rsp, default, 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. Operand memOp = MemoryOp(OperandType.I64, rsp, default, 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. // We can eliminate the move if source is already 32-bit and the registers are the same.
  1239. if (dest.Value == source.Value && source.Type == OperandType.I32)
  1240. {
  1241. return;
  1242. }
  1243. context.Assembler.Mov(dest, source, OperandType.I32);
  1244. }
  1245. private static void GenerateZeroExtend8(CodeGenContext context, Operation operation)
  1246. {
  1247. Operand dest = operation.Destination;
  1248. Operand source = operation.GetSource(0);
  1249. Debug.Assert(dest.Type.IsInteger() && source.Type.IsInteger());
  1250. context.Assembler.Movzx8(dest, source, OperandType.I32);
  1251. }
  1252. private static void GenerateLoad(CodeGenContext context, Operand address, Operand value)
  1253. {
  1254. switch (value.Type)
  1255. {
  1256. case OperandType.I32: context.Assembler.Mov (value, address, OperandType.I32); break;
  1257. case OperandType.I64: context.Assembler.Mov (value, address, OperandType.I64); break;
  1258. case OperandType.FP32: context.Assembler.Movd (value, address); break;
  1259. case OperandType.FP64: context.Assembler.Movq (value, address); break;
  1260. case OperandType.V128: context.Assembler.Movdqu(value, address); break;
  1261. default: Debug.Assert(false); break;
  1262. }
  1263. }
  1264. private static void GenerateStore(CodeGenContext context, Operand address, Operand value)
  1265. {
  1266. switch (value.Type)
  1267. {
  1268. case OperandType.I32: context.Assembler.Mov (address, value, OperandType.I32); break;
  1269. case OperandType.I64: context.Assembler.Mov (address, value, OperandType.I64); break;
  1270. case OperandType.FP32: context.Assembler.Movd (address, value); break;
  1271. case OperandType.FP64: context.Assembler.Movq (address, value); break;
  1272. case OperandType.V128: context.Assembler.Movdqu(address, value); break;
  1273. default: Debug.Assert(false); break;
  1274. }
  1275. }
  1276. private static void GenerateZeroUpper64(CodeGenContext context, Operand dest, Operand source)
  1277. {
  1278. context.Assembler.Movq(dest, source);
  1279. }
  1280. private static void GenerateZeroUpper96(CodeGenContext context, Operand dest, Operand source)
  1281. {
  1282. context.Assembler.Movq(dest, source);
  1283. context.Assembler.Pshufd(dest, dest, 0xfc);
  1284. }
  1285. private static bool MatchOperation(Operation node, Instruction inst, OperandType destType, Register destReg)
  1286. {
  1287. if (node == default || node.DestinationsCount == 0)
  1288. {
  1289. return false;
  1290. }
  1291. if (node.Instruction != inst)
  1292. {
  1293. return false;
  1294. }
  1295. Operand dest = node.Destination;
  1296. return dest.Kind == OperandKind.Register &&
  1297. dest.Type == destType &&
  1298. dest.GetRegister() == destReg;
  1299. }
  1300. [Conditional("DEBUG")]
  1301. private static void ValidateUnOp(Operand dest, Operand source)
  1302. {
  1303. EnsureSameReg (dest, source);
  1304. EnsureSameType(dest, source);
  1305. }
  1306. [Conditional("DEBUG")]
  1307. private static void ValidateBinOp(Operand dest, Operand src1, Operand src2)
  1308. {
  1309. EnsureSameReg (dest, src1);
  1310. EnsureSameType(dest, src1, src2);
  1311. }
  1312. [Conditional("DEBUG")]
  1313. private static void ValidateShift(Operand dest, Operand src1, Operand src2)
  1314. {
  1315. EnsureSameReg (dest, src1);
  1316. EnsureSameType(dest, src1);
  1317. Debug.Assert(dest.Type.IsInteger() && src2.Type == OperandType.I32);
  1318. }
  1319. private static void EnsureSameReg(Operand op1, Operand op2)
  1320. {
  1321. if (!op1.Type.IsInteger() && HardwareCapabilities.SupportsVexEncoding)
  1322. {
  1323. return;
  1324. }
  1325. Debug.Assert(op1.Kind == OperandKind.Register || op1.Kind == OperandKind.Memory);
  1326. Debug.Assert(op1.Kind == op2.Kind);
  1327. Debug.Assert(op1.Value == op2.Value);
  1328. }
  1329. private static void EnsureSameType(Operand op1, Operand op2)
  1330. {
  1331. Debug.Assert(op1.Type == op2.Type);
  1332. }
  1333. private static void EnsureSameType(Operand op1, Operand op2, Operand op3)
  1334. {
  1335. Debug.Assert(op1.Type == op2.Type);
  1336. Debug.Assert(op1.Type == op3.Type);
  1337. }
  1338. private static void EnsureSameType(Operand op1, Operand op2, Operand op3, Operand op4)
  1339. {
  1340. Debug.Assert(op1.Type == op2.Type);
  1341. Debug.Assert(op1.Type == op3.Type);
  1342. Debug.Assert(op1.Type == op4.Type);
  1343. }
  1344. private static UnwindInfo WritePrologue(CodeGenContext context)
  1345. {
  1346. List<UnwindPushEntry> pushEntries = new List<UnwindPushEntry>();
  1347. Operand rsp = Register(X86Register.Rsp);
  1348. int mask = CallingConvention.GetIntCalleeSavedRegisters() & context.AllocResult.IntUsedRegisters;
  1349. while (mask != 0)
  1350. {
  1351. int bit = BitOperations.TrailingZeroCount(mask);
  1352. context.Assembler.Push(Register((X86Register)bit));
  1353. pushEntries.Add(new UnwindPushEntry(UnwindPseudoOp.PushReg, context.StreamOffset, regIndex: bit));
  1354. mask &= ~(1 << bit);
  1355. }
  1356. int reservedStackSize = context.CallArgsRegionSize + context.AllocResult.SpillRegionSize;
  1357. reservedStackSize += context.XmmSaveRegionSize;
  1358. if (reservedStackSize >= StackGuardSize)
  1359. {
  1360. GenerateInlineStackProbe(context, reservedStackSize);
  1361. }
  1362. if (reservedStackSize != 0)
  1363. {
  1364. context.Assembler.Sub(rsp, Const(reservedStackSize), OperandType.I64);
  1365. pushEntries.Add(new UnwindPushEntry(UnwindPseudoOp.AllocStack, context.StreamOffset, stackOffsetOrAllocSize: reservedStackSize));
  1366. }
  1367. int offset = reservedStackSize;
  1368. mask = CallingConvention.GetVecCalleeSavedRegisters() & context.AllocResult.VecUsedRegisters;
  1369. while (mask != 0)
  1370. {
  1371. int bit = BitOperations.TrailingZeroCount(mask);
  1372. offset -= 16;
  1373. Operand memOp = MemoryOp(OperandType.V128, rsp, default, Multiplier.x1, offset);
  1374. context.Assembler.Movdqu(memOp, Xmm((X86Register)bit));
  1375. pushEntries.Add(new UnwindPushEntry(UnwindPseudoOp.SaveXmm128, context.StreamOffset, bit, offset));
  1376. mask &= ~(1 << bit);
  1377. }
  1378. return new UnwindInfo(pushEntries.ToArray(), context.StreamOffset);
  1379. }
  1380. private static void WriteEpilogue(CodeGenContext context)
  1381. {
  1382. Operand rsp = Register(X86Register.Rsp);
  1383. int reservedStackSize = context.CallArgsRegionSize + context.AllocResult.SpillRegionSize;
  1384. reservedStackSize += context.XmmSaveRegionSize;
  1385. int offset = reservedStackSize;
  1386. int mask = CallingConvention.GetVecCalleeSavedRegisters() & context.AllocResult.VecUsedRegisters;
  1387. while (mask != 0)
  1388. {
  1389. int bit = BitOperations.TrailingZeroCount(mask);
  1390. offset -= 16;
  1391. Operand memOp = MemoryOp(OperandType.V128, rsp, default, Multiplier.x1, offset);
  1392. context.Assembler.Movdqu(Xmm((X86Register)bit), memOp);
  1393. mask &= ~(1 << bit);
  1394. }
  1395. if (reservedStackSize != 0)
  1396. {
  1397. context.Assembler.Add(rsp, Const(reservedStackSize), OperandType.I64);
  1398. }
  1399. mask = CallingConvention.GetIntCalleeSavedRegisters() & context.AllocResult.IntUsedRegisters;
  1400. while (mask != 0)
  1401. {
  1402. int bit = BitUtils.HighestBitSet(mask);
  1403. context.Assembler.Pop(Register((X86Register)bit));
  1404. mask &= ~(1 << bit);
  1405. }
  1406. }
  1407. private static void GenerateInlineStackProbe(CodeGenContext context, int size)
  1408. {
  1409. // Windows does lazy stack allocation, and there are just 2
  1410. // guard pages on the end of the stack. So, if the allocation
  1411. // size we make is greater than this guard size, we must ensure
  1412. // that the OS will map all pages that we'll use. We do that by
  1413. // doing a dummy read on those pages, forcing a page fault and
  1414. // the OS to map them. If they are already mapped, nothing happens.
  1415. const int pageMask = PageSize - 1;
  1416. size = (size + pageMask) & ~pageMask;
  1417. Operand rsp = Register(X86Register.Rsp);
  1418. Operand temp = Register(CallingConvention.GetIntReturnRegister());
  1419. for (int offset = PageSize; offset < size; offset += PageSize)
  1420. {
  1421. Operand memOp = MemoryOp(OperandType.I32, rsp, default, Multiplier.x1, -offset);
  1422. context.Assembler.Mov(temp, memOp, OperandType.I32);
  1423. }
  1424. }
  1425. private static Operand Memory(Operand operand, OperandType type)
  1426. {
  1427. if (operand.Kind == OperandKind.Memory)
  1428. {
  1429. return operand;
  1430. }
  1431. return MemoryOp(type, operand);
  1432. }
  1433. private static Operand Register(X86Register register, OperandType type = OperandType.I64)
  1434. {
  1435. return Operand.Factory.Register((int)register, RegisterType.Integer, type);
  1436. }
  1437. private static Operand Xmm(X86Register register)
  1438. {
  1439. return Operand.Factory.Register((int)register, RegisterType.Vector, OperandType.V128);
  1440. }
  1441. }
  1442. }