CodeGenerator.cs 67 KB

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