CodeGenerator.cs 67 KB

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