BailOut.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. struct GlobalBailOutRecordDataTable;
  7. struct GlobalBailOutRecordDataRow;
  8. class BailOutInfo
  9. {
  10. public:
  11. #ifdef _M_IX86
  12. typedef struct
  13. {
  14. IR::Instr * instr;
  15. uint argCount;
  16. uint argRestoreAdjustCount;
  17. } StartCallInfo;
  18. #else
  19. typedef uint StartCallInfo;
  20. #endif
  21. BailOutInfo(uint32 bailOutOffset, Func* bailOutFunc) :
  22. bailOutOffset(bailOutOffset), bailOutFunc(bailOutFunc),
  23. byteCodeUpwardExposedUsed(nullptr), polymorphicCacheIndex((uint)-1), startCallCount(0), startCallInfo(nullptr), bailOutInstr(nullptr),
  24. totalOutParamCount(0), argOutSyms(nullptr), bailOutRecord(nullptr), wasCloned(false), isInvertedBranch(false), sharedBailOutKind(true), outParamInlinedArgSlot(nullptr),
  25. liveVarSyms(nullptr), liveLosslessInt32Syms(nullptr), liveFloat64Syms(nullptr), liveSimd128F4Syms(nullptr), liveSimd128I4Syms(nullptr), liveSimd128D2Syms(nullptr), branchConditionOpnd(nullptr),
  26. stackLiteralBailOutInfoCount(0), stackLiteralBailOutInfo(nullptr)
  27. {
  28. Assert(bailOutOffset != Js::Constants::NoByteCodeOffset);
  29. #ifdef _M_IX86
  30. outParamFrameAdjustArgSlot = nullptr;
  31. #endif
  32. #if DBG
  33. wasCopied = false;
  34. #endif
  35. this->capturedValues.argObjSyms = nullptr;
  36. this->usedCapturedValues.argObjSyms = nullptr;
  37. }
  38. void Clear(JitArenaAllocator * allocator);
  39. void FinalizeBailOutRecord(Func * func);
  40. #ifdef MD_GROW_LOCALS_AREA_UP
  41. void FinalizeOffsets(__in_ecount(count) int * offsets, uint count, Func *func, BVSparse<JitArenaAllocator> *bvInlinedArgSlot);
  42. #endif
  43. void RecordStartCallInfo(uint i, uint argRestoreAdjustCount, IR::Instr *instr);
  44. uint GetStartCallOutParamCount(uint i) const;
  45. #ifdef _M_IX86
  46. bool NeedsStartCallAdjust(uint i, const IR::Instr * instr) const;
  47. void UnlinkStartCall(const IR::Instr * instr);
  48. #endif
  49. static bool IsBailOutOnImplicitCalls(IR::BailOutKind kind)
  50. {
  51. const IR::BailOutKind kindMinusBits = kind & ~IR::BailOutKindBits;
  52. return kindMinusBits == IR::BailOutOnImplicitCalls ||
  53. kindMinusBits == IR::BailOutOnImplicitCallsPreOp;
  54. }
  55. #if DBG
  56. static bool IsBailOutHelper(IR::JnHelperMethod helper);
  57. #endif
  58. bool wasCloned;
  59. bool isInvertedBranch;
  60. bool sharedBailOutKind;
  61. #if DBG
  62. bool wasCopied;
  63. #endif
  64. uint32 bailOutOffset;
  65. BailOutRecord * bailOutRecord;
  66. CapturedValues capturedValues; // Values we know about after forward pass
  67. CapturedValues usedCapturedValues; // Values that need to be restored in the bail out
  68. BVSparse<JitArenaAllocator> * byteCodeUpwardExposedUsed; // Non-constant stack syms that needs to be restored in the bail out
  69. uint polymorphicCacheIndex;
  70. uint startCallCount;
  71. uint totalOutParamCount;
  72. Func ** startCallFunc;
  73. StartCallInfo * startCallInfo;
  74. StackSym ** argOutSyms;
  75. struct StackLiteralBailOutInfo
  76. {
  77. StackSym * stackSym;
  78. uint initFldCount;
  79. };
  80. uint stackLiteralBailOutInfoCount;
  81. StackLiteralBailOutInfo * stackLiteralBailOutInfo;
  82. BVSparse<JitArenaAllocator> * liveVarSyms;
  83. BVSparse<JitArenaAllocator> * liveLosslessInt32Syms; // These are only the live int32 syms that fully represent the var-equivalent sym's value (see GlobOpt::FillBailOutInfo)
  84. BVSparse<JitArenaAllocator> * liveFloat64Syms;
  85. // SIMD_JS
  86. BVSparse<JitArenaAllocator> * liveSimd128F4Syms;
  87. BVSparse<JitArenaAllocator> * liveSimd128I4Syms;
  88. BVSparse<JitArenaAllocator> * liveSimd128D2Syms;
  89. int * outParamOffsets;
  90. BVSparse<JitArenaAllocator> * outParamInlinedArgSlot;
  91. #ifdef _M_IX86
  92. BVSparse<JitArenaAllocator> * outParamFrameAdjustArgSlot;
  93. BVFixed * inlinedStartCall;
  94. #endif
  95. #ifdef MD_GROW_LOCALS_AREA_UP
  96. // Use a bias to guarantee that all sym offsets are non-zero.
  97. static const int32 StackSymBias = MachStackAlignment;
  98. #endif
  99. // The actual bailout instr, this is normally the instr that has the bailout info.
  100. // 1) If we haven't generated bailout (which happens in lowerer) for this bailout info, this is either of:
  101. // - the instr that has bailout info.
  102. // - in case of shared bailout this will be the BailTarget instr (corresponds to the call to SaveRegistersAndBailOut,
  103. // while other instrs sharing bailout info will just have checks and JMP to BailTarget).
  104. // 2) After we generated bailout, this becomes label instr. In case of shared bailout other instrs JMP to this label.
  105. IR::Instr * bailOutInstr;
  106. #if ENABLE_DEBUG_CONFIG_OPTIONS
  107. Js::OpCode bailOutOpcode;
  108. #endif
  109. Func * bailOutFunc;
  110. IR::Opnd * branchConditionOpnd;
  111. template<class Fn>
  112. void IterateArgOutSyms(Fn callback)
  113. {
  114. uint argOutIndex = 0;
  115. for(uint i = 0; i < this->startCallCount; i++)
  116. {
  117. uint outParamCount = this->GetStartCallOutParamCount(i);
  118. for(uint j = 0; j < outParamCount; j++)
  119. {
  120. StackSym* sym = this->argOutSyms[argOutIndex];
  121. if(sym)
  122. {
  123. callback(argOutIndex, i + sym->GetArgSlotNum(), sym);
  124. }
  125. argOutIndex++;
  126. }
  127. }
  128. }
  129. };
  130. class BailOutRecord
  131. {
  132. public:
  133. BailOutRecord(uint32 bailOutOffset, uint bailOutCacheIndex, IR::BailOutKind kind, Func *bailOutFunc);
  134. static Js::Var BailOut(BailOutRecord const * bailOutRecord);
  135. static Js::Var BailOutFromFunction(Js::JavascriptCallStackLayout * layout, BailOutRecord const * bailOutRecord, void * returnAddress, void * argoutRestoreAddress);
  136. static uint32 BailOutFromLoopBody(Js::JavascriptCallStackLayout * layout, BailOutRecord const * bailOutRecord);
  137. static Js::Var BailOutInlined(Js::JavascriptCallStackLayout * layout, BailOutRecord const * bailOutRecord, void * returnAddress);
  138. static uint32 BailOutFromLoopBodyInlined(Js::JavascriptCallStackLayout * layout, BailOutRecord const * bailOutRecord, void * returnAddress);
  139. static Js::Var BailOutForElidedYield(void * framePointer);
  140. static size_t GetOffsetOfPolymorphicCacheIndex() { return offsetof(BailOutRecord, polymorphicCacheIndex); }
  141. static size_t GetOffsetOfBailOutKind() { return offsetof(BailOutRecord, bailOutKind); }
  142. static bool IsArgumentsObject(uint32 offset);
  143. static uint32 GetArgumentsObjectOffset();
  144. static const uint BailOutRegisterSaveSlotCount = LinearScanMD::RegisterSaveSlotCount;
  145. public:
  146. template <size_t N>
  147. void FillNativeRegToByteCodeRegMap(uint (&nativeRegToByteCodeRegMap)[N]);
  148. void IsOffsetNativeIntOrFloat(uint offsetIndex, int argOutSlotStart, bool * pIsFloat64, bool * pIsInt32, bool * pIsSimd128F4, bool * pIsSImd128I4) const;
  149. template <typename Fn>
  150. void MapStartCallParamCounts(Fn fn);
  151. void SetBailOutKind(IR::BailOutKind bailOutKind) { this->bailOutKind = bailOutKind; }
  152. uint32 GetBailOutOffset() { return bailOutOffset; }
  153. #if ENABLE_DEBUG_CONFIG_OPTIONS
  154. Js::OpCode GetBailOutOpCode() { return bailOutOpcode; }
  155. #endif
  156. template <typename Fn>
  157. void MapArgOutOffsets(Fn fn);
  158. protected:
  159. struct BailOutReturnValue
  160. {
  161. Js::Var returnValue;
  162. Js::RegSlot returnValueRegSlot;
  163. };
  164. static Js::Var BailOutCommonNoCodeGen(Js::JavascriptCallStackLayout * layout, BailOutRecord const * bailOutRecord,
  165. uint32 bailOutOffset, void * returnAddress, IR::BailOutKind bailOutKind, Js::Var branchValue = nullptr, Js::Var * registerSaves = nullptr,
  166. BailOutReturnValue * returnValue = nullptr, void * argoutRestoreAddress = nullptr);
  167. static Js::Var BailOutCommon(Js::JavascriptCallStackLayout * layout, BailOutRecord const * bailOutRecord,
  168. uint32 bailOutOffset, void * returnAddress, IR::BailOutKind bailOutKind, Js::Var branchValue = nullptr, BailOutReturnValue * returnValue = nullptr, void * argoutRestoreAddress = nullptr);
  169. static Js::Var BailOutInlinedCommon(Js::JavascriptCallStackLayout * layout, BailOutRecord const * bailOutRecord,
  170. uint32 bailOutOffset, void * returnAddress, IR::BailOutKind bailOutKind, Js::Var branchValue = nullptr);
  171. static uint32 BailOutFromLoopBodyCommon(Js::JavascriptCallStackLayout * layout, BailOutRecord const * bailOutRecord,
  172. uint32 bailOutOffset, IR::BailOutKind bailOutKind, Js::Var branchValue = nullptr);
  173. static uint32 BailOutFromLoopBodyInlinedCommon(Js::JavascriptCallStackLayout * layout, BailOutRecord const * bailOutRecord,
  174. uint32 bailOutOffset, void * returnAddress, IR::BailOutKind bailOutKind, Js::Var branchValue = nullptr);
  175. static Js::Var BailOutHelper(Js::JavascriptCallStackLayout * layout, Js::ScriptFunction ** functionRef, Js::Arguments& args, const bool boxArgs,
  176. BailOutRecord const * bailOutRecord, uint32 bailOutOffset, void * returnAddress, IR::BailOutKind bailOutKind, Js::Var * registerSaves, BailOutReturnValue * returnValue, Js::Var* pArgumentsObject,
  177. Js::Var branchValue = nullptr, void * argoutRestoreAddress = nullptr);
  178. static void BailOutInlinedHelper(Js::JavascriptCallStackLayout * layout, BailOutRecord const *& bailOutRecord, uint32 bailOutOffset, void * returnAddress,
  179. IR::BailOutKind bailOutKind, Js::Var * registerSaves, BailOutReturnValue * returnValue, Js::ScriptFunction ** innerMostInlinee, bool isInLoopBody, Js::Var branchValue = nullptr);
  180. static uint32 BailOutFromLoopBodyHelper(Js::JavascriptCallStackLayout * layout, BailOutRecord const * bailOutRecord,
  181. uint32 bailOutOffset, IR::BailOutKind bailOutKind, Js::Var branchValue = nullptr, Js::Var * registerSaves = nullptr, BailOutReturnValue * returnValue = nullptr);
  182. static void UpdatePolymorphicFieldAccess(Js::JavascriptFunction * function, BailOutRecord const * bailOutRecord);
  183. static void ScheduleFunctionCodeGen(Js::ScriptFunction * function, Js::ScriptFunction * innerMostInlinee, BailOutRecord const * bailOutRecord, IR::BailOutKind bailOutKind, void * returnAddress);
  184. static void ScheduleLoopBodyCodeGen(Js::ScriptFunction * function, Js::ScriptFunction * innerMostInlinee, BailOutRecord const * bailOutRecord, IR::BailOutKind bailOutKind);
  185. void RestoreValues(IR::BailOutKind bailOutKind, Js::JavascriptCallStackLayout * layout, Js::InterpreterStackFrame * newInstance, Js::ScriptContext * scriptContext,
  186. bool fromLoopBody, Js::Var * registerSaves, BailOutReturnValue * returnValue, Js::Var* pArgumentsObject, Js::Var branchValue = nullptr, void* returnAddress = nullptr, bool useStartCall = true, void * argoutRestoreAddress = nullptr) const;
  187. void RestoreValues(IR::BailOutKind bailOutKind, Js::JavascriptCallStackLayout * layout, uint count, __in_ecount_opt(count) int * offsets, int argOutSlotId,
  188. __out_ecount(count)Js::Var * values, Js::ScriptContext * scriptContext, bool fromLoopBody, Js::Var * registerSaves, Js::InterpreterStackFrame * newInstance, Js::Var* pArgumentsObject,
  189. void * argoutRestoreAddress = nullptr) const;
  190. void RestoreValue(IR::BailOutKind bailOutKind, Js::JavascriptCallStackLayout * layout, Js::Var * values, Js::ScriptContext * scriptContext,
  191. bool fromLoopBody, Js::Var * registerSaves, Js::InterpreterStackFrame * newInstance, Js::Var* pArgumentsObject, void * argoutRestoreAddress,
  192. uint regSlot, int offset, bool isLocal, bool isFloat64, bool isInt32, bool isSimd128F4, bool isSimd128I4) const;
  193. void RestoreInlineFrame(InlinedFrameLayout *inlinedFrame, Js::JavascriptCallStackLayout * layout, Js::Var * registerSaves);
  194. void AdjustOffsetsForDiagMode(Js::JavascriptCallStackLayout * layout, Js::ScriptFunction * function) const;
  195. Js::Var EnsureArguments(Js::InterpreterStackFrame * newInstance, Js::JavascriptCallStackLayout * layout, Js::ScriptContext* scriptContext, Js::Var* pArgumentsObject) const;
  196. Js::JavascriptCallStackLayout *GetStackLayout() const;
  197. struct StackLiteralBailOutRecord
  198. {
  199. Js::RegSlot regSlot;
  200. uint initFldCount;
  201. };
  202. struct ArgOutOffsetInfo
  203. {
  204. BVFixed * argOutFloat64Syms; // Used for float-type-specialized ArgOut symbols. Index = [0 .. BailOutInfo::totalOutParamCount].
  205. BVFixed * argOutLosslessInt32Syms; // Used for int-type-specialized ArgOut symbols (which are native int and for bailout we need tagged ints).
  206. // SIMD_JS
  207. BVFixed * argOutSimd128F4Syms;
  208. BVFixed * argOutSimd128I4Syms;
  209. uint * startCallOutParamCounts;
  210. int * outParamOffsets;
  211. uint startCallCount;
  212. uint argOutSymStart;
  213. };
  214. // The offset to 'globalBailOutRecordTable' is hard-coded in LinearScanMD::SaveAllRegisters, so let this be the first member variable
  215. GlobalBailOutRecordDataTable *globalBailOutRecordTable;
  216. ArgOutOffsetInfo *argOutOffsetInfo;
  217. BailOutRecord * parent;
  218. Js::Var * constants;
  219. Js::EHBailoutData * ehBailoutData;
  220. StackLiteralBailOutRecord * stackLiteralBailOutRecord;
  221. #ifdef _M_IX86
  222. uint * startCallArgRestoreAdjustCounts;
  223. #endif
  224. // Index of start of section of argOuts for current record/current func, used with argOutFloat64Syms and
  225. // argOutLosslessInt32Syms when restoring values, as BailOutInfo uses one argOuts array for all funcs.
  226. // Similar to outParamOffsets which points to current func section for the offsets.
  227. Js::RegSlot localOffsetsCount;
  228. uint32 const bailOutOffset;
  229. uint stackLiteralBailOutRecordCount;
  230. Js::RegSlot branchValueRegSlot;
  231. uint polymorphicCacheIndex;
  232. IR::BailOutKind bailOutKind;
  233. #if DBG
  234. Js::ArgSlot actualCount;
  235. uint constantCount;
  236. int inlineDepth;
  237. void DumpArgOffsets(uint count, int* offsets, int argOutSlotStart);
  238. void DumpLocalOffsets(uint count, int argOutSlotStart);
  239. void DumpValue(int offset, bool isFloat64);
  240. #endif
  241. ushort bailOutCount;
  242. uint32 m_bailOutRecordId;
  243. friend class LinearScan;
  244. friend class BailOutInfo;
  245. friend struct FuncBailOutData;
  246. #if ENABLE_DEBUG_CONFIG_OPTIONS
  247. public:
  248. Js::OpCode bailOutOpcode;
  249. #if DBG
  250. void Dump();
  251. #endif
  252. #endif
  253. };
  254. class BranchBailOutRecord : public BailOutRecord
  255. {
  256. public:
  257. BranchBailOutRecord(uint32 trueBailOutOffset, uint32 falseBailOutOffset, Js::RegSlot resultByteCodeReg, IR::BailOutKind kind, Func *bailOutFunc);
  258. static Js::Var BailOut(BranchBailOutRecord const * bailOutRecord, BOOL cond);
  259. static Js::Var BailOutFromFunction(Js::JavascriptCallStackLayout * layout, BranchBailOutRecord const * bailOutRecord, BOOL cond, void * returnAddress, void * argoutRestoreAddress);
  260. static uint32 BailOutFromLoopBody(Js::JavascriptCallStackLayout * layout, BranchBailOutRecord const * bailOutRecord, BOOL cond);
  261. static Js::Var BailOutInlined(Js::JavascriptCallStackLayout * layout, BranchBailOutRecord const * bailOutRecord, BOOL cond, void * returnAddress);
  262. static uint32 BailOutFromLoopBodyInlined(Js::JavascriptCallStackLayout * layout, BranchBailOutRecord const * bailOutRecord, BOOL cond, void * returnAddress);
  263. private:
  264. uint falseBailOutOffset;
  265. };
  266. class FunctionBailOutRecord
  267. {
  268. public:
  269. FunctionBailOutRecord() : constantCount(0), constants(nullptr) {}
  270. uint constantCount;
  271. Js::Var * constants;
  272. };
  273. template <typename Fn>
  274. inline void BailOutRecord::MapStartCallParamCounts(Fn fn)
  275. {
  276. if (this->argOutOffsetInfo)
  277. {
  278. for (uint i = 0; i < this->argOutOffsetInfo->startCallCount; i++)
  279. {
  280. fn(this->argOutOffsetInfo->startCallOutParamCounts[i]);
  281. }
  282. }
  283. }
  284. template <typename Fn>
  285. inline void BailOutRecord::MapArgOutOffsets(Fn fn)
  286. {
  287. uint outParamSlot = 0;
  288. uint argOutSlotOffset = 0;
  289. if (this->argOutOffsetInfo)
  290. {
  291. for (uint i = 0; i < this->argOutOffsetInfo->startCallCount; i++)
  292. {
  293. uint startCallOutParamCount = this->argOutOffsetInfo->startCallOutParamCounts[i];
  294. argOutSlotOffset += 1; // skip pointer to self which is pushed by OP_StartCall
  295. for (uint j = 0; j < startCallOutParamCount; j++, outParamSlot++, argOutSlotOffset++)
  296. {
  297. if (this->argOutOffsetInfo->outParamOffsets[outParamSlot] != 0)
  298. {
  299. fn(argOutSlotOffset, this->argOutOffsetInfo->outParamOffsets[outParamSlot]);
  300. }
  301. }
  302. }
  303. }
  304. }
  305. struct GlobalBailOutRecordDataRow
  306. {
  307. int32 offset;
  308. uint32 start; // start bailOutId
  309. uint32 end; // end bailOutId
  310. unsigned regSlot : 30;
  311. unsigned isFloat : 1;
  312. unsigned isInt : 1;
  313. // SIMD_JS
  314. unsigned isSimd128F4 : 1;
  315. unsigned isSimd128I4 : 1;
  316. };
  317. struct GlobalBailOutRecordDataTable
  318. {
  319. // The offset to 'registerSaveSpace' is hard-coded in LinearScanMD::SaveAllRegisters, so let this be the first member variable
  320. Js::Var *registerSaveSpace;
  321. GlobalBailOutRecordDataRow *globalBailOutRecordDataRows;
  322. uint32 length;
  323. uint32 size;
  324. int32 firstActualStackOffset;
  325. Js::RegSlot returnValueRegSlot;
  326. bool isInlinedFunction;
  327. bool isInlinedConstructor;
  328. bool isLoopBody;
  329. void Finalize(NativeCodeData::Allocator *allocator, JitArenaAllocator *tempAlloc);
  330. void AddOrUpdateRow(JitArenaAllocator *allocator, uint32 bailOutRecordId, uint32 regSlot, bool isFloat, bool isInt, bool isSimd128F4, bool isSimd128I4, int32 offset, uint *lastUpdatedRowIndex);
  331. template<class Fn>
  332. void IterateGlobalBailOutRecordTableRows(uint32 bailOutRecordId, Fn callback)
  333. {
  334. // Visit all the rows that have this bailout ID in their range.
  335. for (uint i = 0; i < this->length; i++)
  336. {
  337. if (bailOutRecordId > globalBailOutRecordDataRows[i].end)
  338. {
  339. // Not in range.
  340. continue;
  341. }
  342. if (globalBailOutRecordDataRows[i].start > bailOutRecordId)
  343. {
  344. // Not in range, and we know there are no more in range (since the table is sorted by "start").
  345. return;
  346. }
  347. // In range: take action.
  348. callback(&globalBailOutRecordDataRows[i]);
  349. }
  350. }
  351. template<class Fn>
  352. void VisitGlobalBailOutRecordTableRowsAtFirstBailOut(uint32 bailOutRecordId, Fn callback)
  353. {
  354. // Visit all the rows that have this bailout ID as the start of their range.
  355. // (I.e., visit each row once in a walk of the whole function)
  356. for (uint i = 0; i < this->length; i++)
  357. {
  358. if (bailOutRecordId == globalBailOutRecordDataRows[i].start)
  359. {
  360. // Matching start ID: take action.
  361. callback(&globalBailOutRecordDataRows[i]);
  362. }
  363. else if (globalBailOutRecordDataRows[i].start > bailOutRecordId)
  364. {
  365. // We know there are no more in range (since the table is sorted by "start").
  366. return;
  367. }
  368. }
  369. }
  370. };