2
0

LowerMD.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. class Lowerer;
  7. ///---------------------------------------------------------------------------
  8. ///
  9. /// class LowererMD
  10. ///
  11. ///---------------------------------------------------------------------------
  12. #ifdef DBG
  13. #define INSERTDEBUGBREAK(instrInsert)\
  14. {\
  15. IR::Instr *int3 = IR::Instr::New(Js::OpCode::DEBUGBREAK, m_func);\
  16. instrInsert->InsertBefore(int3);\
  17. }
  18. #else
  19. #define INSERTDEBUGBREAK(instrInsert)
  20. #endif
  21. class LowererMD
  22. {
  23. public:
  24. static const int MaxArgumentsToHelper = 16;
  25. LowererMD(Func *func) :
  26. m_func(func),
  27. helperCallArgsCount(0),
  28. helperCallDoubleArgsCount(0)
  29. {
  30. }
  31. static bool IsUnconditionalBranch(const IR::Instr *instr);
  32. static bool IsAssign(const IR::Instr *instr);
  33. static bool IsCall(const IR::Instr *instr);
  34. static bool IsIndirectBranch(const IR::Instr *instr);
  35. static bool IsReturnInstr(const IR::Instr *instr);
  36. static void InvertBranch(IR::BranchInstr *instr);
  37. static Js::OpCode MDBranchOpcode(Js::OpCode opcode);
  38. static Js::OpCode MDUnsignedBranchOpcode(Js::OpCode opcode);
  39. static Js::OpCode MDCompareWithZeroBranchOpcode(Js::OpCode opcode);
  40. static Js::OpCode MDConvertFloat64ToInt32Opcode(const RoundMode roundMode);
  41. static void ChangeToAdd(IR::Instr *const instr, const bool needFlags);
  42. static void ChangeToSub(IR::Instr *const instr, const bool needFlags);
  43. static void ChangeToShift(IR::Instr *const instr, const bool needFlags);
  44. static const uint16 GetFormalParamOffset();
  45. static const Js::OpCode MDUncondBranchOpcode;
  46. static const Js::OpCode MDMultiBranchOpcode;
  47. static const Js::OpCode MDTestOpcode;
  48. static const Js::OpCode MDOrOpcode;
  49. static const Js::OpCode MDXorOpcode;
  50. static const Js::OpCode MDOverflowBranchOpcode;
  51. static const Js::OpCode MDNotOverflowBranchOpcode;
  52. static const Js::OpCode MDConvertFloat32ToFloat64Opcode;
  53. static const Js::OpCode MDConvertFloat64ToFloat32Opcode;
  54. static const Js::OpCode MDCallOpcode;
  55. static const Js::OpCode MDImulOpcode;
  56. static const Js::OpCode MDLea;
  57. public:
  58. void Init(Lowerer *lowerer);
  59. void FinalLower();
  60. bool FinalLowerAssign(IR::Instr* instr);
  61. IR::Opnd * GenerateMemRef(intptr_t addr, IRType type, IR::Instr *instr, bool dontEncode = false);
  62. IR::Instr * ChangeToHelperCall(IR::Instr * instr, IR::JnHelperMethod helperMethod, IR::LabelInstr *labelBailOut = nullptr,
  63. IR::Opnd *opndInstance = nullptr, IR::PropertySymOpnd * propSymOpnd = nullptr, bool isHelperContinuation = false);
  64. IR::Instr * ChangeToHelperCallMem(IR::Instr * instr, IR::JnHelperMethod helperMethod);
  65. static IR::Instr * ChangeToAssign(IR::Instr * instr);
  66. static IR::Instr * ChangeToAssignNoBarrierCheck(IR::Instr * instr);
  67. static IR::Instr * ForceDstToReg(IR::Instr *instr);
  68. static void ImmedSrcToReg(IR::Instr * instr, IR::Opnd * newOpnd, int srcNum);
  69. IR::Instr * LoadArgumentCount(IR::Instr * instr);
  70. IR::Instr * LoadStackArgPtr(IR::Instr * instr);
  71. IR::Instr * LoadHeapArguments(IR::Instr * instrArgs);
  72. IR::Instr * LoadHeapArgsCached(IR::Instr * instr);
  73. IR::Instr * LoadInputParamPtr(IR::Instr * instrInsert, IR::RegOpnd * optionalDstOpnd = nullptr);
  74. IR::Instr * LoadInputParamCount(IR::Instr * instr, int adjust = 0, bool needFlags = false);
  75. IR::Instr * LoadArgumentsFromFrame(IR::Instr * instr);
  76. IR::Instr * LowerRet(IR::Instr * instr);
  77. IR::Instr * LowerCondBranch(IR::Instr * instr);
  78. IR::Instr * LoadFunctionObjectOpnd(IR::Instr *instr, IR::Opnd *&functionObjOpnd);
  79. IR::Instr * GenerateSmIntPairTest(IR::Instr * instrInsert, IR::Opnd * opndSrc1, IR::Opnd * opndSrc2, IR::LabelInstr * labelFail);
  80. #if DBG
  81. static void GenerateDebugBreak(IR::Instr * insertInstr);
  82. #endif
  83. void GenerateTaggedZeroTest( IR::Opnd * opndSrc, IR::Instr * instrInsert, IR::LabelInstr * labelHelper = nullptr);
  84. bool GenerateObjectTest(IR::Opnd * opndSrc, IR::Instr * insertInstr, IR::LabelInstr * labelTarget, bool fContinueLabel = false);
  85. bool GenerateFastCmSrEqConst(IR::Instr *instr);
  86. bool GenerateFastCmXxI4(IR::Instr *instr);
  87. bool GenerateFastCmXxR8(IR::Instr *instr) { Assert(UNREACHED); return nullptr; }
  88. bool GenerateFastCmXxTaggedInt(IR::Instr *instr, bool isInHelper = false);
  89. IR::Instr * GenerateConvBool(IR::Instr *instr);
  90. void GenerateClz(IR::Instr * instr);
  91. void GenerateCtz(IR::Instr * instr) { Assert(UNREACHED); }
  92. void GeneratePopCnt(IR::Instr * instr) { Assert(UNREACHED); }
  93. void GenerateTruncWithCheck(IR::Instr * instr) { Assert(UNREACHED); }
  94. void GenerateFastDivByPow2(IR::Instr *instr);
  95. bool GenerateFastAdd(IR::Instr * instrAdd);
  96. bool GenerateFastSub(IR::Instr * instrSub);
  97. bool GenerateFastMul(IR::Instr * instrMul);
  98. bool GenerateFastAnd(IR::Instr * instrAnd);
  99. bool GenerateFastXor(IR::Instr * instrXor);
  100. bool GenerateFastOr(IR::Instr * instrOr);
  101. bool GenerateFastNot(IR::Instr * instrNot);
  102. bool GenerateFastNeg(IR::Instr * instrNeg);
  103. bool GenerateFastShiftLeft(IR::Instr * instrShift);
  104. bool GenerateFastShiftRight(IR::Instr * instrShift);
  105. void GenerateFastBrS(IR::BranchInstr *brInstr);
  106. void GenerateFastInlineBuiltInCall(IR::Instr* instr, IR::JnHelperMethod helperMethod);
  107. void HelperCallForAsmMathBuiltin(IR::Instr* instr, IR::JnHelperMethod helperMethodFloat, IR::JnHelperMethod helperMethodDouble) { Assert(UNREACHED); } // only for asm.js
  108. IR::Opnd * CreateStackArgumentsSlotOpnd();
  109. void GenerateSmIntTest(IR::Opnd *opndSrc, IR::Instr *insertInstr, IR::LabelInstr *labelHelper, IR::Instr **instrFirst = nullptr, bool fContinueLabel = false);
  110. IR::RegOpnd * LoadNonnegativeIndex(IR::RegOpnd *indexOpnd, const bool skipNegativeCheck, IR::LabelInstr *const notTaggedIntLabel, IR::LabelInstr *const negativeLabel, IR::Instr *const insertBeforeInstr);
  111. IR::RegOpnd * GenerateUntagVar(IR::RegOpnd * opnd, IR::LabelInstr * labelFail, IR::Instr * insertBeforeInstr, bool generateTagCheck = true);
  112. bool GenerateFastLdMethodFromFlags(IR::Instr * instrLdFld);
  113. void GenerateInt32ToVarConversion( IR::Opnd * opndSrc, IR::Instr * insertInstr );
  114. IR::Instr * GenerateFastScopedFld(IR::Instr * instrScopedFld, bool isLoad);
  115. IR::Instr * GenerateFastScopedLdFld(IR::Instr * instrLdFld);
  116. IR::Instr * GenerateFastScopedStFld(IR::Instr * instrStFld);
  117. void GenerateFastAbs(IR::Opnd *dst, IR::Opnd *src, IR::Instr *callInstr, IR::Instr *insertInstr, IR::LabelInstr *labelHelper, IR::LabelInstr *doneLabel);
  118. bool TryGenerateFastMulAdd(IR::Instr * instrAdd, IR::Instr ** pInstrPrev);
  119. void GenerateFloatTest(IR::RegOpnd * opndSrc, IR::Instr * insertInstr, IR::LabelInstr* labelHelper, const bool checkForNullInLoopBody = false);
  120. IR::RegOpnd* CheckFloatAndUntag(IR::RegOpnd * opndSrc, IR::Instr * insertInstr, IR::LabelInstr* labelHelper);
  121. static void EmitInt4Instr(IR::Instr *instr);
  122. void EmitLoadVar(IR::Instr *instr, bool isFromUint32 = false, bool isHelper = false);
  123. bool EmitLoadInt32(IR::Instr *instr, bool conversionFromObjectAllowed, bool bailOutOnHelper = false, IR::LabelInstr * labelBailOut = nullptr);
  124. static void LowerInt4NegWithBailOut(IR::Instr *const instr, const IR::BailOutKind bailOutKind, IR::LabelInstr *const bailOutLabel, IR::LabelInstr *const skipBailOutLabel);
  125. static void LowerInt4AddWithBailOut(IR::Instr *const instr, const IR::BailOutKind bailOutKind, IR::LabelInstr *const bailOutLabel, IR::LabelInstr *const skipBailOutLabel);
  126. static void LowerInt4SubWithBailOut(IR::Instr *const instr, const IR::BailOutKind bailOutKind, IR::LabelInstr *const bailOutLabel, IR::LabelInstr *const skipBailOutLabel);
  127. static void LowerInt4MulWithBailOut(IR::Instr *const instr, const IR::BailOutKind bailOutKind, IR::LabelInstr *const bailOutLabel, IR::LabelInstr *const skipBailOutLabel);
  128. void LowerInt4RemWithBailOut(IR::Instr *const instr, const IR::BailOutKind bailOutKind, IR::LabelInstr *const bailOutLabel, IR::LabelInstr *const skipBailOutLabel) const;
  129. void GenerateFastRecyclerAlloc(size_t allocSize, IR::RegOpnd* newObjDst, IR::Instr* insertionPointInstr, IR::LabelInstr* allocHelperLabel, IR::LabelInstr* allocDoneLabel);
  130. #ifdef _CONTROL_FLOW_GUARD
  131. void GenerateCFGCheck(IR::Opnd * entryPointOpnd, IR::Instr * insertBeforeInstr);
  132. #endif
  133. void SaveDoubleToVar(IR::RegOpnd * dstOpnd, IR::RegOpnd *opndFloat, IR::Instr *instrOrig, IR::Instr *instrInsert, bool isHelper = false);
  134. void EmitLoadFloat(IR::Opnd *dst, IR::Opnd *src, IR::Instr *insertInstr, IR::Instr * instrBailOut = nullptr, IR::LabelInstr * labelBailOut = nullptr);
  135. IR::Instr * LoadCheckedFloat(IR::RegOpnd *opndOrig, IR::RegOpnd *opndFloat, IR::LabelInstr *labelInline, IR::LabelInstr *labelHelper, IR::Instr *instrInsert, const bool checkForNullInLoopBody = false);
  136. IR::Instr * LowerCallHelper(IR::Instr *instrCall);
  137. IR::LabelInstr *GetBailOutStackRestoreLabel(BailOutInfo * bailOutInfo, IR::LabelInstr * exitTargetInstr);
  138. StackSym * GetImplicitParamSlotSym(Js::ArgSlot argSlot);
  139. static StackSym * GetImplicitParamSlotSym(Js::ArgSlot argSlot, Func * func);
  140. IR::Instr * LowerDivI4AndBailOnReminder(IR::Instr * instr, IR::LabelInstr * bailOutLabel);
  141. public:
  142. IR::Instr * LowerCall(IR::Instr * callInstr, Js::ArgSlot argCount);
  143. IR::Instr * LowerCallI(IR::Instr * callInstr, ushort callFlags, bool isHelper = false, IR::Instr* insertBeforeInstrForCFG = nullptr);
  144. int32 LowerCallArgs(IR::Instr * callInstr, IR::Instr * stackParamInsert, ushort callFlags, Js::ArgSlot extraParams = 1 /* for function object */, IR::IntConstOpnd **callInfoOpndRef = nullptr);
  145. int32 LowerCallArgs(IR::Instr * callInstr, ushort callFlags, Js::ArgSlot extraParams = 1 /* for function object */, IR::IntConstOpnd **callInfoOpndRef = nullptr) { return LowerCallArgs(callInstr, callInstr, callFlags, extraParams, callInfoOpndRef); }
  146. IR::Instr * LowerStartCall(IR::Instr * instr);
  147. IR::Instr * LowerAsmJsCallI(IR::Instr * callInstr) { Assert(UNREACHED); return nullptr; }
  148. IR::Instr * LowerAsmJsCallE(IR::Instr * callInstr) { Assert(UNREACHED); return nullptr; }
  149. IR::Instr * LowerWasmMemOp(IR::Instr * instr, IR::Opnd *addrOpnd) { Assert(UNREACHED); return nullptr; }
  150. IR::Instr * LowerAsmJsStElemHelper(IR::Instr * callInstr) { Assert(UNREACHED); return nullptr; }
  151. IR::Instr * LowerAsmJsLdElemHelper(IR::Instr * callInstr) { Assert(UNREACHED); return nullptr; }
  152. IR::Instr * LowerCallIDynamic(IR::Instr *callInstr, IR::Instr*saveThisArgOutInstr, IR::Opnd *argsLength, ushort callFlags, IR::Instr * insertBeforeInstrForCFG = nullptr);
  153. IR::Instr * LoadHelperArgument(IR::Instr * instr, IR::Opnd * opndArg);
  154. IR::Instr * LoadDynamicArgument(IR::Instr * instr, uint argNumber = 1);
  155. IR::Instr * LoadDynamicArgumentUsingLength(IR::Instr *instr);
  156. IR::Instr * LoadDoubleHelperArgument(IR::Instr * instr, IR::Opnd * opndArg);
  157. IR::Instr * LoadFloatHelperArgument(IR::Instr * instr, IR::Opnd * opndArg) { Assert(UNREACHED); return nullptr; } // only used for asm.js right now
  158. IR::Instr * LowerToFloat(IR::Instr *instr);
  159. static IR::BranchInstr * LowerFloatCondBranch(IR::BranchInstr *instrBranch, bool ignoreNaN = false);
  160. void ConvertFloatToInt32(IR::Opnd* intOpnd, IR::Opnd* floatOpnd, IR::LabelInstr * labelHelper, IR::LabelInstr * labelDone, IR::Instr * instInsert);
  161. void CheckOverflowOnFloatToInt32(IR::Instr* instrInsert, IR::Opnd* intOpnd, IR::LabelInstr * labelHelper, IR::LabelInstr * labelDone);
  162. void EmitLoadVarNoCheck(IR::RegOpnd * dst, IR::RegOpnd * src, IR::Instr *instrLoad, bool isFromUint32, bool isHelper);
  163. void EmitIntToFloat(IR::Opnd *dst, IR::Opnd *src, IR::Instr *instrInsert);
  164. void EmitUIntToFloat(IR::Opnd *dst, IR::Opnd *src, IR::Instr *instrInsert);
  165. void EmitFloatToInt(IR::Opnd *dst, IR::Opnd *src, IR::Instr *instrInsert, IR::Instr * instrBailOut = nullptr, IR::LabelInstr * labelBailOut = nullptr);
  166. void EmitFloat32ToFloat64(IR::Opnd *dst, IR::Opnd *src, IR::Instr *instrInsert) { Assert(UNREACHED); }
  167. void EmitInt64toFloat(IR::Opnd *dst, IR::Opnd *src, IR::Instr *instrInsert) { Assert(UNREACHED); }
  168. static IR::Instr * InsertConvertFloat64ToInt32(const RoundMode roundMode, IR::Opnd *const dst, IR::Opnd *const src, IR::Instr *const insertBeforeInstr);
  169. void EmitIntToLong(IR::Opnd *dst, IR::Opnd *src, IR::Instr *instrInsert);
  170. void EmitUIntToLong(IR::Opnd *dst, IR::Opnd *src, IR::Instr *instrInsert);
  171. void EmitLongToInt(IR::Opnd *dst, IR::Opnd *src, IR::Instr *instrInsert);
  172. void EmitSignExtend(IR::Instr * instr) { Assert(UNREACHED); }
  173. void EmitReinterpretPrimitive(IR::Opnd* dst, IR::Opnd* src, IR::Instr* insertBeforeInstr) { Assert(UNREACHED); }
  174. void EmitLoadFloatFromNumber(IR::Opnd *dst, IR::Opnd *src, IR::Instr *insertInstr);
  175. IR::LabelInstr* EmitLoadFloatCommon(IR::Opnd *dst, IR::Opnd *src, IR::Instr *insertInstr, bool needHelperLabel);
  176. static IR::Instr * LoadFloatZero(IR::Opnd * opndDst, IR::Instr * instrInsert);
  177. static IR::Instr * LoadFloatValue(IR::Opnd * opndDst, double value, IR::Instr * instrInsert);
  178. IR::Instr * LowerEntryInstr(IR::EntryInstr * entryInstr);
  179. IR::Instr * LowerExitInstr(IR::ExitInstr * exitInstr);
  180. IR::Instr * LowerExitInstrAsmJs(IR::ExitInstr * exitInstr) { Assert(UNREACHED); return nullptr; }
  181. IR::Instr * LoadNewScObjFirstArg(IR::Instr * instr, IR::Opnd * dst, ushort extraArgs = 0);
  182. IR::Instr * LowerTry(IR::Instr *instr, IR::JnHelperMethod helperMethod);
  183. IR::Instr * LowerLeaveNull(IR::Instr *instr);
  184. IR::LabelInstr * EnsureEHEpilogLabel();
  185. IR::Instr * LowerEHRegionReturn(IR::Instr * insertBeforeInstr, IR::Opnd * targetOpnd);
  186. void FinishArgLowering();
  187. IR::Opnd * GetOpndForArgSlot(Js::ArgSlot argSlot, IR::Opnd * argOpnd = nullptr);
  188. bool GenerateStackAllocation(IR::Instr *instr, uint32 allocSize, uint32 probeSize);
  189. void GenerateStackDeallocation(IR::Instr *instr, uint32 allocSize);
  190. void GenerateStackProbe(IR::Instr *instr, bool afterProlog);
  191. IR::Opnd* GenerateArgOutForStackArgs(IR::Instr* callInstr, IR::Instr* stackArgsInstr);
  192. template <bool verify = false>
  193. static void Legalize(IR::Instr *const instr, bool fPostRegAlloc = false);
  194. void GenerateFastInlineBuiltInMathAbs(IR::Instr *callInstr);
  195. void GenerateFastInlineBuiltInMathRound(IR::Instr *callInstr);
  196. void GenerateFastInlineBuiltInMathFloorCeil(IR::Instr *callInstr);
  197. void GenerateFastInlineBuiltInMathMinMax(IR::Instr *callInstr);
  198. static void GenerateFastInlineMathFround(IR::Instr* instr);
  199. static RegNum GetRegStackPointer() { return RegSP; }
  200. static RegNum GetRegFramePointer() { return RegFP; }
  201. static RegNum GetRegReturn(IRType type) { return IRType_IsFloat(type) ? RegNOREG : RegR0; }
  202. static RegNum GetRegArgI4(int32 argNum) { return RegNOREG; }
  203. static RegNum GetRegArgR8(int32 argNum) { return RegNOREG; }
  204. static Js::OpCode GetLoadOp(IRType type) { return IRType_IsFloat(type) ? Js::OpCode::FLDR : Js::OpCode::LDR; }
  205. static Js::OpCode GetStoreOp(IRType type) { return IRType_IsFloat(type) ? Js::OpCode::FSTR : Js::OpCode::STR; }
  206. static BYTE GetDefaultIndirScale()
  207. {
  208. return IndirScale8;
  209. }
  210. // -16 is to avoid alignment issues popping up, we are conservative here.
  211. static bool IsSmallStack(uint32 size) { return (size < (PAGESIZE - MachStackAlignment)); }
  212. static void GenerateLoadTaggedType(IR::Instr * instrLdSt, IR::RegOpnd * opndType, IR::RegOpnd * opndTaggedType);
  213. static void GenerateLoadPolymorphicInlineCacheSlot(IR::Instr * instrLdSt, IR::RegOpnd * opndInlineCache, IR::RegOpnd * opndType, uint polymorphicInlineCacheSize);
  214. static void GenerateStFldFromLocalInlineCache(IR::Instr * instrStFld, IR::RegOpnd * opndBase, IR::Opnd * opndSrc, IR::RegOpnd * opndInlineCache, IR::LabelInstr * labelFallThru, bool isInlineSlot);
  215. void GenerateFunctionObjectTest(IR::Instr * callInstr, IR::RegOpnd *functionOpnd, bool isHelper, IR::LabelInstr* continueAfterExLabel = nullptr);
  216. static IR::Instr * ChangeToWriteBarrierAssign(IR::Instr * assignInstr, const Func* func);
  217. int GetHelperArgsCount() { return this->helperCallArgsCount; }
  218. void ResetHelperArgsCount() { this->helperCallArgsCount = 0; }
  219. void LowerInlineSpreadArgOutLoop(IR::Instr *callInstr, IR::RegOpnd *indexOpnd, IR::RegOpnd *arrayElementsStartOpnd);
  220. void LowerTypeof(IR::Instr * typeOfInstr);
  221. void GenerateMemInit(IR::RegOpnd * opnd, int32 offset, size_t value, IR::Instr * insertBeforeInstr, bool isZeroed = false);
  222. static void InsertObjectPoison(IR::Opnd* poisonedOpnd, IR::BranchInstr* branchInstr, IR::Instr* insertInstr);
  223. private:
  224. static IR::Instr * ChangeToAssign(IR::Instr * instr, IRType destType);
  225. void GenerateAssignForBuiltinArg(
  226. RegNum dstReg,
  227. IR::Opnd* srcOpnd,
  228. IR::Instr* instr);
  229. IR::Instr* GeneratePreCall(IR::Instr * callInstr, IR::Opnd *functionOpnd, IR::Instr * insertBeforeInstrForCFGCheck);
  230. void SetMaxArgSlots(Js::ArgSlot actualCount /*including this*/);
  231. // Data
  232. protected:
  233. Func * m_func;
  234. Lowerer * m_lowerer;
  235. //
  236. // Support to load helper arguments.
  237. //
  238. static const int MaxDoubleArgumentsToHelper = 8;
  239. // Only 8 double values can be passed through double registers, rest has to go through stack and
  240. // need to following a different calling convention. We should never hit that case as there is no helper call with more
  241. // than 8 double arguments.
  242. uint16 helperCallArgsCount; //consists of both integer & double arguments
  243. uint16 helperCallDoubleArgsCount; //consists of only double arguments
  244. IR::Opnd * helperCallArgs[MaxArgumentsToHelper];
  245. void FlipHelperCallArgsOrder();
  246. };