CodeGenerator.cs 67 KB

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