Func.cpp 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172
  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. #include "Backend.h"
  6. #include "Base/EtwTrace.h"
  7. #include "Base/ScriptContextProfiler.h"
  8. #ifdef VTUNE_PROFILING
  9. #include "Base/VTuneChakraProfile.h"
  10. #endif
  11. #include "Library/ForInObjectEnumerator.h"
  12. Func::Func(JitArenaAllocator *alloc, JITTimeWorkItem * workItem,
  13. ThreadContextInfo * threadContextInfo,
  14. ScriptContextInfo * scriptContextInfo,
  15. JITOutputIDL * outputData,
  16. Js::EntryPointInfo* epInfo,
  17. const FunctionJITRuntimeInfo *const runtimeInfo,
  18. JITTimePolymorphicInlineCacheInfo * const polymorphicInlineCacheInfo, void * const codeGenAllocators,
  19. #if !FLOATVAR
  20. CodeGenNumberAllocator * numberAllocator,
  21. #endif
  22. Js::ScriptContextProfiler *const codeGenProfiler, const bool isBackgroundJIT, Func * parentFunc,
  23. uint postCallByteCodeOffset, Js::RegSlot returnValueRegSlot, const bool isInlinedConstructor,
  24. Js::ProfileId callSiteIdInParentFunc, bool isGetterSetter) :
  25. m_alloc(alloc),
  26. m_workItem(workItem),
  27. m_output(outputData),
  28. m_entryPointInfo(epInfo),
  29. m_threadContextInfo(threadContextInfo),
  30. m_scriptContextInfo(scriptContextInfo),
  31. m_runtimeInfo(runtimeInfo),
  32. m_polymorphicInlineCacheInfo(polymorphicInlineCacheInfo),
  33. m_codeGenAllocators(codeGenAllocators),
  34. m_inlineeId(0),
  35. pinnedTypeRefs(nullptr),
  36. singleTypeGuards(nullptr),
  37. equivalentTypeGuards(nullptr),
  38. propertyGuardsByPropertyId(nullptr),
  39. ctorCachesByPropertyId(nullptr),
  40. callSiteToArgumentsOffsetFixupMap(nullptr),
  41. indexedPropertyGuardCount(0),
  42. propertiesWrittenTo(nullptr),
  43. lazyBailoutProperties(alloc),
  44. anyPropertyMayBeWrittenTo(false),
  45. #ifdef PROFILE_EXEC
  46. m_codeGenProfiler(codeGenProfiler),
  47. #endif
  48. m_isBackgroundJIT(isBackgroundJIT),
  49. m_cloner(nullptr),
  50. m_cloneMap(nullptr),
  51. m_loopParamSym(nullptr),
  52. m_funcObjSym(nullptr),
  53. m_localClosureSym(nullptr),
  54. m_paramClosureSym(nullptr),
  55. m_localFrameDisplaySym(nullptr),
  56. m_bailoutReturnValueSym(nullptr),
  57. m_hasBailedOutSym(nullptr),
  58. m_inlineeFrameStartSym(nullptr),
  59. m_regsUsed(0),
  60. m_fg(nullptr),
  61. m_labelCount(0),
  62. m_argSlotsForFunctionsCalled(0),
  63. m_hasCalls(false),
  64. m_hasInlineArgsOpt(false),
  65. m_canDoInlineArgsOpt(true),
  66. m_doFastPaths(false),
  67. hasBailout(false),
  68. firstIRTemp(0),
  69. hasBailoutInEHRegion(false),
  70. hasInstrNumber(false),
  71. maintainByteCodeOffset(true),
  72. frameSize(0),
  73. topFunc(parentFunc ? parentFunc->topFunc : this),
  74. parentFunc(parentFunc),
  75. argObjSyms(nullptr),
  76. m_nonTempLocalVars(nullptr),
  77. hasAnyStackNestedFunc(false),
  78. hasMarkTempObjects(false),
  79. postCallByteCodeOffset(postCallByteCodeOffset),
  80. maxInlineeArgOutSize(0),
  81. returnValueRegSlot(returnValueRegSlot),
  82. firstActualStackOffset(-1),
  83. m_localVarSlotsOffset(Js::Constants::InvalidOffset),
  84. m_hasLocalVarChangedOffset(Js::Constants::InvalidOffset),
  85. actualCount((Js::ArgSlot) - 1),
  86. tryCatchNestingLevel(0),
  87. m_localStackHeight(0),
  88. tempSymDouble(nullptr),
  89. tempSymBool(nullptr),
  90. hasInlinee(false),
  91. thisOrParentInlinerHasArguments(false),
  92. hasStackArgs(false),
  93. hasImplicitParamLoad(false),
  94. hasThrow(false),
  95. hasNonSimpleParams(false),
  96. hasUnoptimizedArgumentsAccess(false),
  97. applyTargetInliningRemovedArgumentsAccess(false),
  98. hasImplicitCalls(false),
  99. hasTempObjectProducingInstr(false),
  100. isInlinedConstructor(isInlinedConstructor),
  101. #if !FLOATVAR
  102. numberAllocator(numberAllocator),
  103. #endif
  104. loopCount(0),
  105. callSiteIdInParentFunc(callSiteIdInParentFunc),
  106. isGetterSetter(isGetterSetter),
  107. frameInfo(nullptr),
  108. isTJLoopBody(false),
  109. m_nativeCodeDataSym(nullptr),
  110. isFlowGraphValid(false),
  111. legalizePostRegAlloc(false),
  112. #if DBG
  113. m_callSiteCount(0),
  114. #endif
  115. stackNestedFunc(false),
  116. stackClosure(false)
  117. #if defined(_M_ARM32_OR_ARM64)
  118. , m_ArgumentsOffset(0)
  119. , m_epilogLabel(nullptr)
  120. #endif
  121. , m_funcStartLabel(nullptr)
  122. , m_funcEndLabel(nullptr)
  123. #if DBG
  124. , hasCalledSetDoFastPaths(false)
  125. , allowRemoveBailOutArgInstr(false)
  126. , currentPhases(alloc)
  127. , isPostLower(false)
  128. , isPostRegAlloc(false)
  129. , isPostPeeps(false)
  130. , isPostLayout(false)
  131. , isPostFinalLower(false)
  132. , vtableMap(nullptr)
  133. #endif
  134. , m_yieldOffsetResumeLabelList(nullptr)
  135. , m_bailOutNoSaveLabel(nullptr)
  136. , constantAddressRegOpnd(alloc)
  137. , lastConstantAddressRegLoadInstr(nullptr)
  138. , m_totalJumpTableSizeInBytesForSwitchStatements(0)
  139. , slotArrayCheckTable(nullptr)
  140. , frameDisplayCheckTable(nullptr)
  141. , stackArgWithFormalsTracker(nullptr)
  142. , m_forInLoopBaseDepth(0)
  143. , m_forInEnumeratorArrayOffset(-1)
  144. , argInsCount(0)
  145. , m_globalObjTypeSpecFldInfoArray(nullptr)
  146. #if LOWER_SPLIT_INT64
  147. , m_int64SymPairMap(nullptr)
  148. #endif
  149. #ifdef RECYCLER_WRITE_BARRIER_JIT
  150. , m_lowerer(nullptr)
  151. #endif
  152. {
  153. Assert(this->IsInlined() == !!runtimeInfo);
  154. AssertOrFailFast(!HasProfileInfo() || GetReadOnlyProfileInfo()->GetLoopCount() == GetJITFunctionBody()->GetLoopCount());
  155. Js::RegSlot tmpResult;
  156. AssertOrFailFast(!UInt32Math::Add(GetJITFunctionBody()->GetConstCount(), GetJITFunctionBody()->GetVarCount(), &tmpResult));
  157. AssertOrFailFast(GetJITFunctionBody()->IsAsmJsMode() || GetJITFunctionBody()->GetFirstTmpReg() <= GetJITFunctionBody()->GetLocalsCount());
  158. AssertOrFailFast(!IsLoopBody() || m_workItem->GetLoopNumber() < GetJITFunctionBody()->GetLoopCount());
  159. AssertOrFailFast(CONFIG_FLAG(Prejit) || CONFIG_ISENABLED(Js::ForceNativeFlag) || GetJITFunctionBody()->GetByteCodeLength() < (uint)CONFIG_FLAG(MaxJITFunctionBytecodeByteLength));
  160. GetJITFunctionBody()->EnsureConsistentConstCount();
  161. if (this->IsTopFunc())
  162. {
  163. outputData->hasJittedStackClosure = false;
  164. outputData->localVarSlotsOffset = m_localVarSlotsOffset;
  165. outputData->localVarChangedOffset = m_hasLocalVarChangedOffset;
  166. }
  167. if (this->IsInlined())
  168. {
  169. m_inlineeId = ++(GetTopFunc()->m_inlineeId);
  170. }
  171. bool doStackNestedFunc = GetJITFunctionBody()->DoStackNestedFunc();
  172. bool doStackClosure = GetJITFunctionBody()->DoStackClosure() && !PHASE_OFF(Js::FrameDisplayFastPathPhase, this) && !PHASE_OFF(Js::StackClosurePhase, this);
  173. Assert(!doStackClosure || doStackNestedFunc);
  174. this->stackClosure = doStackClosure && this->IsTopFunc();
  175. if (this->stackClosure)
  176. {
  177. // TODO: calculate on runtime side?
  178. m_output.SetHasJITStackClosure();
  179. }
  180. if (m_workItem->Type() == JsFunctionType &&
  181. GetJITFunctionBody()->DoBackendArgumentsOptimization() &&
  182. (!GetJITFunctionBody()->HasTry() || this->DoOptimizeTry()))
  183. {
  184. // doBackendArgumentsOptimization bit is set when there is no eval inside a function
  185. // as determined by the bytecode generator.
  186. SetHasStackArgs(true);
  187. }
  188. if (doStackNestedFunc && GetJITFunctionBody()->GetNestedCount() != 0 &&
  189. (this->IsTopFunc() || this->GetTopFunc()->m_workItem->Type() != JsLoopBodyWorkItemType)) // make sure none of the functions inlined in a jitted loop body allocate nested functions on the stack
  190. {
  191. Assert(!(this->IsJitInDebugMode() && !GetJITFunctionBody()->IsLibraryCode()));
  192. stackNestedFunc = true;
  193. this->GetTopFunc()->hasAnyStackNestedFunc = true;
  194. }
  195. if (GetJITFunctionBody()->HasOrParentHasArguments() || (parentFunc && parentFunc->thisOrParentInlinerHasArguments))
  196. {
  197. thisOrParentInlinerHasArguments = true;
  198. }
  199. if (parentFunc == nullptr)
  200. {
  201. inlineDepth = 0;
  202. m_symTable = JitAnew(alloc, SymTable);
  203. m_symTable->Init(this);
  204. m_symTable->SetStartingID(static_cast<SymID>(workItem->GetJITFunctionBody()->GetLocalsCount() + 1));
  205. Assert(Js::Constants::NoByteCodeOffset == postCallByteCodeOffset);
  206. Assert(Js::Constants::NoRegister == returnValueRegSlot);
  207. #if defined(_M_IX86) || defined(_M_X64)
  208. if (HasArgumentSlot())
  209. {
  210. // Pre-allocate the single argument slot we'll reserve for the arguments object.
  211. // For ARM, the argument slot is not part of the local but part of the register saves
  212. m_localStackHeight = MachArgsSlotOffset;
  213. }
  214. #endif
  215. }
  216. else
  217. {
  218. inlineDepth = parentFunc->inlineDepth + 1;
  219. Assert(Js::Constants::NoByteCodeOffset != postCallByteCodeOffset);
  220. }
  221. this->constructorCacheCount = 0;
  222. this->constructorCaches = AnewArrayZ(this->m_alloc, JITTimeConstructorCache*, GetJITFunctionBody()->GetProfiledCallSiteCount());
  223. #if DBG_DUMP
  224. m_codeSize = -1;
  225. #endif
  226. #if defined(_M_X64)
  227. m_spillSize = -1;
  228. m_argsSize = -1;
  229. m_savedRegSize = -1;
  230. #endif
  231. if (this->IsJitInDebugMode())
  232. {
  233. m_nonTempLocalVars = Anew(this->m_alloc, BVSparse<JitArenaAllocator>, this->m_alloc);
  234. }
  235. if (GetJITFunctionBody()->IsCoroutine())
  236. {
  237. m_yieldOffsetResumeLabelList = YieldOffsetResumeLabelList::New(this->m_alloc);
  238. }
  239. if (this->IsTopFunc())
  240. {
  241. m_globalObjTypeSpecFldInfoArray = JitAnewArrayZ(this->m_alloc, ObjTypeSpecFldInfo*, GetWorkItem()->GetJITTimeInfo()->GetGlobalObjTypeSpecFldInfoCount());
  242. }
  243. for (uint i = 0; i < GetJITFunctionBody()->GetInlineCacheCount(); ++i)
  244. {
  245. ObjTypeSpecFldInfo * info = GetWorkItem()->GetJITTimeInfo()->GetObjTypeSpecFldInfo(i);
  246. if (info != nullptr)
  247. {
  248. AssertOrFailFast(info->GetObjTypeSpecFldId() < GetTopFunc()->GetWorkItem()->GetJITTimeInfo()->GetGlobalObjTypeSpecFldInfoCount());
  249. GetTopFunc()->m_globalObjTypeSpecFldInfoArray[info->GetObjTypeSpecFldId()] = info;
  250. }
  251. }
  252. canHoistConstantAddressLoad = !PHASE_OFF(Js::HoistConstAddrPhase, this);
  253. m_forInLoopMaxDepth = this->GetJITFunctionBody()->GetForInLoopDepth();
  254. }
  255. bool
  256. Func::IsLoopBodyInTry() const
  257. {
  258. return IsLoopBody() && m_workItem->GetLoopHeader()->isInTry;
  259. }
  260. bool
  261. Func::IsLoopBodyInTryFinally() const
  262. {
  263. return IsLoopBody() && m_workItem->GetLoopHeader()->isInTryFinally;
  264. }
  265. /* static */
  266. void
  267. Func::Codegen(JitArenaAllocator *alloc, JITTimeWorkItem * workItem,
  268. ThreadContextInfo * threadContextInfo,
  269. ScriptContextInfo * scriptContextInfo,
  270. JITOutputIDL * outputData,
  271. Js::EntryPointInfo* epInfo, // for in-proc jit only
  272. const FunctionJITRuntimeInfo *const runtimeInfo,
  273. JITTimePolymorphicInlineCacheInfo * const polymorphicInlineCacheInfo, void * const codeGenAllocators,
  274. #if !FLOATVAR
  275. CodeGenNumberAllocator * numberAllocator,
  276. #endif
  277. Js::ScriptContextProfiler *const codeGenProfiler, const bool isBackgroundJIT)
  278. {
  279. bool rejit;
  280. do
  281. {
  282. Func func(alloc, workItem, threadContextInfo,
  283. scriptContextInfo, outputData, epInfo, runtimeInfo,
  284. polymorphicInlineCacheInfo, codeGenAllocators,
  285. #if !FLOATVAR
  286. numberAllocator,
  287. #endif
  288. codeGenProfiler, isBackgroundJIT);
  289. try
  290. {
  291. func.TryCodegen();
  292. rejit = false;
  293. }
  294. catch (Js::RejitException ex)
  295. {
  296. // The work item needs to be rejitted, likely due to some optimization that was too aggressive
  297. switch (ex.Reason())
  298. {
  299. case RejitReason::AggressiveIntTypeSpecDisabled:
  300. outputData->disableAggressiveIntTypeSpec = TRUE;
  301. break;
  302. case RejitReason::InlineApplyDisabled:
  303. workItem->GetJITFunctionBody()->DisableInlineApply();
  304. outputData->disableInlineApply = TRUE;
  305. break;
  306. case RejitReason::InlineSpreadDisabled:
  307. workItem->GetJITFunctionBody()->DisableInlineSpread();
  308. outputData->disableInlineSpread = TRUE;
  309. break;
  310. case RejitReason::DisableStackArgOpt:
  311. outputData->disableStackArgOpt = TRUE;
  312. break;
  313. case RejitReason::DisableSwitchOptExpectingInteger:
  314. case RejitReason::DisableSwitchOptExpectingString:
  315. outputData->disableSwitchOpt = TRUE;
  316. break;
  317. case RejitReason::ArrayCheckHoistDisabled:
  318. case RejitReason::ArrayAccessHelperCallEliminationDisabled:
  319. outputData->disableArrayCheckHoist = TRUE;
  320. break;
  321. case RejitReason::TrackIntOverflowDisabled:
  322. outputData->disableTrackCompoundedIntOverflow = TRUE;
  323. break;
  324. case RejitReason::MemOpDisabled:
  325. outputData->disableMemOp = TRUE;
  326. break;
  327. default:
  328. Assume(UNREACHED);
  329. }
  330. if (PHASE_TRACE(Js::ReJITPhase, &func))
  331. {
  332. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  333. Output::Print(
  334. _u("Rejit (compile-time): function: %s (%s) reason: %S\n"),
  335. workItem->GetJITFunctionBody()->GetDisplayName(),
  336. workItem->GetJITTimeInfo()->GetDebugNumberSet(debugStringBuffer),
  337. ex.ReasonName());
  338. }
  339. rejit = true;
  340. }
  341. // Either the entry point has a reference to the number now, or we failed to code gen and we
  342. // don't need to numbers, we can flush the completed page now.
  343. //
  344. // If the number allocator is NULL then we are shutting down the thread context and so too the
  345. // code generator. The number allocator must be freed before the recycler (and thus before the
  346. // code generator) so we can't and don't need to flush it.
  347. // TODO: OOP JIT, allocator cleanup
  348. } while (rejit);
  349. }
  350. ///----------------------------------------------------------------------------
  351. ///
  352. /// Func::TryCodegen
  353. ///
  354. /// Attempt to Codegen this function.
  355. ///
  356. ///----------------------------------------------------------------------------
  357. void
  358. Func::TryCodegen()
  359. {
  360. Assert(!IsJitInDebugMode() || !GetJITFunctionBody()->HasTry());
  361. BEGIN_CODEGEN_PHASE(this, Js::BackEndPhase);
  362. {
  363. // IRBuilder
  364. BEGIN_CODEGEN_PHASE(this, Js::IRBuilderPhase);
  365. #ifdef ASMJS_PLAT
  366. if (GetJITFunctionBody()->IsAsmJsMode())
  367. {
  368. IRBuilderAsmJs asmIrBuilder(this);
  369. asmIrBuilder.Build();
  370. }
  371. else
  372. #endif
  373. {
  374. IRBuilder irBuilder(this);
  375. irBuilder.Build();
  376. }
  377. END_CODEGEN_PHASE(this, Js::IRBuilderPhase);
  378. #ifdef IR_VIEWER
  379. IRtoJSObjectBuilder::DumpIRtoGlobalObject(this, Js::IRBuilderPhase);
  380. #endif /* IR_VIEWER */
  381. BEGIN_CODEGEN_PHASE(this, Js::InlinePhase);
  382. InliningHeuristics heuristics(GetWorkItem()->GetJITTimeInfo(), this->IsLoopBody());
  383. Inline inliner(this, heuristics);
  384. inliner.Optimize();
  385. END_CODEGEN_PHASE(this, Js::InlinePhase);
  386. ThrowIfScriptClosed();
  387. // FlowGraph
  388. {
  389. // Scope for FlowGraph arena
  390. NoRecoverMemoryJitArenaAllocator fgAlloc(_u("BE-FlowGraph"), m_alloc->GetPageAllocator(), Js::Throw::OutOfMemory);
  391. BEGIN_CODEGEN_PHASE(this, Js::FGBuildPhase);
  392. this->m_fg = FlowGraph::New(this, &fgAlloc);
  393. this->m_fg->Build();
  394. END_CODEGEN_PHASE(this, Js::FGBuildPhase);
  395. // Global Optimization and Type Specialization
  396. BEGIN_CODEGEN_PHASE(this, Js::GlobOptPhase);
  397. GlobOpt globOpt(this);
  398. globOpt.Optimize();
  399. END_CODEGEN_PHASE(this, Js::GlobOptPhase);
  400. // Delete flowGraph now
  401. this->m_fg->Destroy();
  402. this->m_fg = nullptr;
  403. }
  404. #ifdef IR_VIEWER
  405. IRtoJSObjectBuilder::DumpIRtoGlobalObject(this, Js::GlobOptPhase);
  406. #endif /* IR_VIEWER */
  407. ThrowIfScriptClosed();
  408. // Lowering
  409. Lowerer lowerer(this);
  410. BEGIN_CODEGEN_PHASE(this, Js::LowererPhase);
  411. lowerer.Lower();
  412. END_CODEGEN_PHASE(this, Js::LowererPhase);
  413. #ifdef IR_VIEWER
  414. IRtoJSObjectBuilder::DumpIRtoGlobalObject(this, Js::LowererPhase);
  415. #endif /* IR_VIEWER */
  416. // Encode constants
  417. Security security(this);
  418. BEGIN_CODEGEN_PHASE(this, Js::EncodeConstantsPhase)
  419. security.EncodeLargeConstants();
  420. END_CODEGEN_PHASE(this, Js::EncodeConstantsPhase);
  421. if (GetJITFunctionBody()->DoInterruptProbe())
  422. {
  423. BEGIN_CODEGEN_PHASE(this, Js::InterruptProbePhase)
  424. lowerer.DoInterruptProbes();
  425. END_CODEGEN_PHASE(this, Js::InterruptProbePhase)
  426. }
  427. // Register Allocation
  428. BEGIN_CODEGEN_PHASE(this, Js::RegAllocPhase);
  429. LinearScan linearScan(this);
  430. linearScan.RegAlloc();
  431. END_CODEGEN_PHASE(this, Js::RegAllocPhase);
  432. #ifdef IR_VIEWER
  433. IRtoJSObjectBuilder::DumpIRtoGlobalObject(this, Js::RegAllocPhase);
  434. #endif /* IR_VIEWER */
  435. ThrowIfScriptClosed();
  436. // Peephole optimizations
  437. BEGIN_CODEGEN_PHASE(this, Js::PeepsPhase);
  438. Peeps peeps(this);
  439. peeps.PeepFunc();
  440. END_CODEGEN_PHASE(this, Js::PeepsPhase);
  441. // Layout
  442. BEGIN_CODEGEN_PHASE(this, Js::LayoutPhase);
  443. SimpleLayout layout(this);
  444. layout.Layout();
  445. END_CODEGEN_PHASE(this, Js::LayoutPhase);
  446. if (this->HasTry() && this->hasBailoutInEHRegion)
  447. {
  448. BEGIN_CODEGEN_PHASE(this, Js::EHBailoutPatchUpPhase);
  449. lowerer.EHBailoutPatchUp();
  450. END_CODEGEN_PHASE(this, Js::EHBailoutPatchUpPhase);
  451. }
  452. // Insert NOPs (moving this before prolog/epilog for AMD64 and possibly ARM).
  453. BEGIN_CODEGEN_PHASE(this, Js::InsertNOPsPhase);
  454. security.InsertNOPs();
  455. END_CODEGEN_PHASE(this, Js::InsertNOPsPhase);
  456. // Prolog/Epilog
  457. BEGIN_CODEGEN_PHASE(this, Js::PrologEpilogPhase);
  458. if (GetJITFunctionBody()->IsAsmJsMode())
  459. {
  460. lowerer.LowerPrologEpilogAsmJs();
  461. }
  462. else
  463. {
  464. lowerer.LowerPrologEpilog();
  465. }
  466. END_CODEGEN_PHASE(this, Js::PrologEpilogPhase);
  467. BEGIN_CODEGEN_PHASE(this, Js::FinalLowerPhase);
  468. lowerer.FinalLower();
  469. END_CODEGEN_PHASE(this, Js::FinalLowerPhase);
  470. // Encoder
  471. BEGIN_CODEGEN_PHASE(this, Js::EncoderPhase);
  472. Encoder encoder(this);
  473. encoder.Encode();
  474. END_CODEGEN_PHASE_NO_DUMP(this, Js::EncoderPhase);
  475. #ifdef IR_VIEWER
  476. IRtoJSObjectBuilder::DumpIRtoGlobalObject(this, Js::EncoderPhase);
  477. #endif /* IR_VIEWER */
  478. }
  479. #if DBG_DUMP
  480. if (Js::Configuration::Global.flags.IsEnabled(Js::AsmDumpModeFlag))
  481. {
  482. FILE * oldFile = 0;
  483. FILE * asmFile = GetScriptContext()->GetNativeCodeGenerator()->asmFile;
  484. if (asmFile)
  485. {
  486. oldFile = Output::SetFile(asmFile);
  487. }
  488. this->Dump(IRDumpFlags_AsmDumpMode);
  489. Output::Flush();
  490. if (asmFile)
  491. {
  492. FILE *openedFile = Output::SetFile(oldFile);
  493. Assert(openedFile == asmFile);
  494. }
  495. }
  496. #endif
  497. if (this->IsOOPJIT())
  498. {
  499. BEGIN_CODEGEN_PHASE(this, Js::NativeCodeDataPhase);
  500. auto dataAllocator = this->GetNativeCodeDataAllocator();
  501. if (dataAllocator->allocCount > 0)
  502. {
  503. NativeCodeData::DataChunk *chunk = (NativeCodeData::DataChunk*)dataAllocator->chunkList;
  504. NativeCodeData::DataChunk *next1 = chunk;
  505. while (next1)
  506. {
  507. if (next1->fixupFunc)
  508. {
  509. next1->fixupFunc(next1->data, chunk);
  510. }
  511. #if DBG
  512. if (CONFIG_FLAG(OOPJITFixupValidate))
  513. {
  514. // Scan memory to see if there's missing pointer needs to be fixed up
  515. // This can hit false positive if some data field happens to have value
  516. // falls into the NativeCodeData memory range.
  517. NativeCodeData::DataChunk *next2 = chunk;
  518. while (next2)
  519. {
  520. for (unsigned int i = 0; i < next1->len / sizeof(void*); i++)
  521. {
  522. if (((void**)next1->data)[i] == (void*)next2->data)
  523. {
  524. NativeCodeData::VerifyExistFixupEntry((void*)next2->data, &((void**)next1->data)[i], next1->data);
  525. }
  526. }
  527. next2 = next2->next;
  528. }
  529. }
  530. #endif
  531. next1 = next1->next;
  532. }
  533. JITOutputIDL* jitOutputData = m_output.GetOutputData();
  534. size_t allocSize = offsetof(NativeDataFixupTable, fixupRecords) + sizeof(NativeDataFixupRecord)* (dataAllocator->allocCount);
  535. jitOutputData->nativeDataFixupTable = (NativeDataFixupTable*)midl_user_allocate(allocSize);
  536. if (!jitOutputData->nativeDataFixupTable)
  537. {
  538. Js::Throw::OutOfMemory();
  539. }
  540. __analysis_assume(jitOutputData->nativeDataFixupTable);
  541. jitOutputData->nativeDataFixupTable->count = dataAllocator->allocCount;
  542. jitOutputData->buffer = (NativeDataBuffer*)midl_user_allocate(offsetof(NativeDataBuffer, data) + dataAllocator->totalSize);
  543. if (!jitOutputData->buffer)
  544. {
  545. Js::Throw::OutOfMemory();
  546. }
  547. __analysis_assume(jitOutputData->buffer);
  548. jitOutputData->buffer->len = dataAllocator->totalSize;
  549. unsigned int len = 0;
  550. unsigned int count = 0;
  551. next1 = chunk;
  552. while (next1)
  553. {
  554. memcpy(jitOutputData->buffer->data + len, next1->data, next1->len);
  555. len += next1->len;
  556. jitOutputData->nativeDataFixupTable->fixupRecords[count].index = next1->allocIndex;
  557. jitOutputData->nativeDataFixupTable->fixupRecords[count].length = next1->len;
  558. jitOutputData->nativeDataFixupTable->fixupRecords[count].startOffset = next1->offset;
  559. jitOutputData->nativeDataFixupTable->fixupRecords[count].updateList = next1->fixupList;
  560. count++;
  561. next1 = next1->next;
  562. }
  563. #if DBG
  564. if (PHASE_TRACE1(Js::NativeCodeDataPhase))
  565. {
  566. Output::Print(_u("NativeCodeData Server Buffer: %p, len: %x, chunk head: %p\n"), jitOutputData->buffer->data, jitOutputData->buffer->len, chunk);
  567. }
  568. #endif
  569. }
  570. END_CODEGEN_PHASE(this, Js::NativeCodeDataPhase);
  571. }
  572. END_CODEGEN_PHASE(this, Js::BackEndPhase);
  573. }
  574. ///----------------------------------------------------------------------------
  575. /// Func::StackAllocate
  576. /// Allocate stack space of given size.
  577. ///----------------------------------------------------------------------------
  578. int32
  579. Func::StackAllocate(int size)
  580. {
  581. Assert(this->IsTopFunc());
  582. int32 offset;
  583. #ifdef MD_GROW_LOCALS_AREA_UP
  584. // Locals have positive offsets and are allocated from bottom to top.
  585. m_localStackHeight = Math::Align(m_localStackHeight, min(size, MachStackAlignment));
  586. offset = m_localStackHeight;
  587. m_localStackHeight += size;
  588. #else
  589. // Locals have negative offsets and are allocated from top to bottom.
  590. m_localStackHeight += size;
  591. m_localStackHeight = Math::Align(m_localStackHeight, min(size, MachStackAlignment));
  592. offset = -m_localStackHeight;
  593. #endif
  594. return offset;
  595. }
  596. ///----------------------------------------------------------------------------
  597. ///
  598. /// Func::StackAllocate
  599. ///
  600. /// Allocate stack space for this symbol.
  601. ///
  602. ///----------------------------------------------------------------------------
  603. int32
  604. Func::StackAllocate(StackSym *stackSym, int size)
  605. {
  606. Assert(size > 0);
  607. if (stackSym->IsArgSlotSym() || stackSym->IsParamSlotSym() || stackSym->IsAllocated())
  608. {
  609. return stackSym->m_offset;
  610. }
  611. Assert(stackSym->m_offset == 0);
  612. stackSym->m_allocated = true;
  613. stackSym->m_offset = StackAllocate(size);
  614. return stackSym->m_offset;
  615. }
  616. void
  617. Func::SetArgOffset(StackSym *stackSym, int32 offset)
  618. {
  619. AssertMsg(offset >= 0, "Why is the offset, negative?");
  620. stackSym->m_offset = offset;
  621. stackSym->m_allocated = true;
  622. }
  623. ///
  624. /// Ensures that local var slots are created, if the function has locals.
  625. /// Allocate stack space for locals used for debugging
  626. /// (for local non-temp vars we write-through memory so that locals inspection can make use of that.).
  627. // On stack, after local slots we allocate space for metadata (in particular, whether any the locals was changed in debugger).
  628. ///
  629. void
  630. Func::EnsureLocalVarSlots()
  631. {
  632. Assert(IsJitInDebugMode());
  633. if (!this->HasLocalVarSlotCreated())
  634. {
  635. uint32 localSlotCount = GetJITFunctionBody()->GetNonTempLocalVarCount();
  636. if (localSlotCount && m_localVarSlotsOffset == Js::Constants::InvalidOffset)
  637. {
  638. // Allocate the slots.
  639. int32 size = localSlotCount * GetDiagLocalSlotSize();
  640. m_localVarSlotsOffset = StackAllocate(size);
  641. m_hasLocalVarChangedOffset = StackAllocate(max(1, MachStackAlignment)); // Can't alloc less than StackAlignment bytes.
  642. Assert(m_workItem->Type() == JsFunctionType);
  643. m_output.SetVarSlotsOffset(AdjustOffsetValue(m_localVarSlotsOffset));
  644. m_output.SetVarChangedOffset(AdjustOffsetValue(m_hasLocalVarChangedOffset));
  645. }
  646. }
  647. }
  648. void Func::SetFirstArgOffset(IR::Instr* inlineeStart)
  649. {
  650. Assert(inlineeStart->m_func == this);
  651. Assert(!IsTopFunc());
  652. int32 lastOffset;
  653. IR::Instr* arg = inlineeStart->GetNextArg();
  654. if (arg)
  655. {
  656. const auto lastArgOutStackSym = arg->GetDst()->AsSymOpnd()->m_sym->AsStackSym();
  657. lastOffset = lastArgOutStackSym->m_offset;
  658. Assert(lastArgOutStackSym->m_isSingleDef);
  659. const auto secondLastArgOutOpnd = lastArgOutStackSym->m_instrDef->GetSrc2();
  660. if (secondLastArgOutOpnd->IsSymOpnd())
  661. {
  662. const auto secondLastOffset = secondLastArgOutOpnd->AsSymOpnd()->m_sym->AsStackSym()->m_offset;
  663. if (secondLastOffset > lastOffset)
  664. {
  665. lastOffset = secondLastOffset;
  666. }
  667. }
  668. lastOffset += MachPtr;
  669. }
  670. else
  671. {
  672. Assert(this->GetTopFunc()->GetJITFunctionBody()->IsAsmJsMode());
  673. lastOffset = MachPtr;
  674. }
  675. int32 firstActualStackOffset = lastOffset - ((this->actualCount + Js::Constants::InlineeMetaArgCount) * MachPtr);
  676. Assert((this->firstActualStackOffset == -1) || (this->firstActualStackOffset == firstActualStackOffset));
  677. this->firstActualStackOffset = firstActualStackOffset;
  678. }
  679. int32
  680. Func::GetLocalVarSlotOffset(int32 slotId)
  681. {
  682. this->EnsureLocalVarSlots();
  683. Assert(m_localVarSlotsOffset != Js::Constants::InvalidOffset);
  684. int32 slotOffset = slotId * GetDiagLocalSlotSize();
  685. return m_localVarSlotsOffset + slotOffset;
  686. }
  687. void Func::OnAddSym(Sym* sym)
  688. {
  689. Assert(sym);
  690. if (this->IsJitInDebugMode() && this->IsNonTempLocalVar(sym->m_id))
  691. {
  692. Assert(m_nonTempLocalVars);
  693. m_nonTempLocalVars->Set(sym->m_id);
  694. }
  695. }
  696. ///
  697. /// Returns offset of the flag (1 byte) whether any local was changed (in debugger).
  698. /// If the function does not have any locals, returns -1.
  699. ///
  700. int32
  701. Func::GetHasLocalVarChangedOffset()
  702. {
  703. this->EnsureLocalVarSlots();
  704. return m_hasLocalVarChangedOffset;
  705. }
  706. bool
  707. Func::IsJitInDebugMode() const
  708. {
  709. return m_workItem->IsJitInDebugMode();
  710. }
  711. bool
  712. Func::IsNonTempLocalVar(uint32 slotIndex)
  713. {
  714. return GetJITFunctionBody()->IsNonTempLocalVar(slotIndex);
  715. }
  716. int32
  717. Func::AdjustOffsetValue(int32 offset)
  718. {
  719. #ifdef MD_GROW_LOCALS_AREA_UP
  720. return -(offset + BailOutInfo::StackSymBias);
  721. #else
  722. // Stack offset are negative, includes the PUSH EBP and return address
  723. return offset - (2 * MachPtr);
  724. #endif
  725. }
  726. #ifdef MD_GROW_LOCALS_AREA_UP
  727. // Note: this is called during jit-compile when we finalize bail out record.
  728. void
  729. Func::AjustLocalVarSlotOffset()
  730. {
  731. if (GetJITFunctionBody()->GetNonTempLocalVarCount())
  732. {
  733. // Turn positive SP-relative base locals offset into negative frame-pointer-relative offset
  734. // This is changing value for restoring the locals when read due to locals inspection.
  735. int localsOffset = m_localVarSlotsOffset - (m_localStackHeight + m_ArgumentsOffset);
  736. int valueChangeOffset = m_hasLocalVarChangedOffset - (m_localStackHeight + m_ArgumentsOffset);
  737. m_output.SetVarSlotsOffset(localsOffset);
  738. m_output.SetVarChangedOffset(valueChangeOffset);
  739. }
  740. }
  741. #endif
  742. bool
  743. Func::DoGlobOptsForGeneratorFunc() const
  744. {
  745. // Disable GlobOpt optimizations for generators initially. Will visit and enable each one by one.
  746. return !GetJITFunctionBody()->IsCoroutine();
  747. }
  748. bool
  749. Func::DoSimpleJitDynamicProfile() const
  750. {
  751. return IsSimpleJit() && !PHASE_OFF(Js::SimpleJitDynamicProfilePhase, GetTopFunc()) && !CONFIG_FLAG(NewSimpleJit);
  752. }
  753. void
  754. Func::SetDoFastPaths()
  755. {
  756. // Make sure we only call this once!
  757. Assert(!this->hasCalledSetDoFastPaths);
  758. bool doFastPaths = false;
  759. if(!PHASE_OFF(Js::FastPathPhase, this) && (!IsSimpleJit() || CONFIG_FLAG(NewSimpleJit)))
  760. {
  761. doFastPaths = true;
  762. }
  763. this->m_doFastPaths = doFastPaths;
  764. #ifdef DBG
  765. this->hasCalledSetDoFastPaths = true;
  766. #endif
  767. }
  768. #if LOWER_SPLIT_INT64
  769. Int64RegPair Func::FindOrCreateInt64Pair(IR::Opnd* opnd)
  770. {
  771. if (!this->IsTopFunc())
  772. {
  773. return GetTopFunc()->FindOrCreateInt64Pair(opnd);
  774. }
  775. AssertMsg(currentPhases.Top() == Js::LowererPhase, "New Int64 sym map is only allowed during lower");
  776. Int64RegPair pair;
  777. IRType pairType = opnd->GetType();
  778. if (opnd->IsInt64())
  779. {
  780. pairType = IRType_IsSignedInt(pairType) ? TyInt32 : TyUint32;
  781. }
  782. if (opnd->IsIndirOpnd())
  783. {
  784. IR::IndirOpnd* indir = opnd->AsIndirOpnd();
  785. indir->SetType(pairType);
  786. pair.low = indir;
  787. pair.high = indir->Copy(this)->AsIndirOpnd();
  788. pair.high->AsIndirOpnd()->SetOffset(indir->GetOffset() + 4);
  789. return pair;
  790. }
  791. // Only indir opnd can have a type other than int64
  792. Assert(opnd->IsInt64());
  793. if (opnd->IsImmediateOpnd())
  794. {
  795. int64 value = opnd->GetImmediateValue(this);
  796. pair.low = IR::IntConstOpnd::New((int32)value, pairType, this);
  797. pair.high = IR::IntConstOpnd::New((int32)(value >> 32), pairType, this);
  798. return pair;
  799. }
  800. Int64SymPair symPair;
  801. if (!m_int64SymPairMap)
  802. {
  803. m_int64SymPairMap = Anew(m_alloc, Int64SymPairMap, m_alloc);
  804. }
  805. StackSym* stackSym = opnd->GetStackSym();
  806. AssertOrFailFastMsg(stackSym, "Invalid int64 operand type");
  807. SymID symId = stackSym->m_id;
  808. if (!m_int64SymPairMap->TryGetValue(symId, &symPair))
  809. {
  810. if (stackSym->IsArgSlotSym() || stackSym->IsParamSlotSym())
  811. {
  812. const bool isArg = stackSym->IsArgSlotSym();
  813. if (isArg)
  814. {
  815. Js::ArgSlot slotNumber = stackSym->GetArgSlotNum();
  816. symPair.low = StackSym::NewArgSlotSym(slotNumber, this, pairType);
  817. symPair.high = StackSym::NewArgSlotSym(slotNumber, this, pairType);
  818. }
  819. else
  820. {
  821. Js::ArgSlot slotNumber = stackSym->GetParamSlotNum();
  822. symPair.low = StackSym::NewParamSlotSym(slotNumber, this, pairType);
  823. symPair.high = StackSym::NewParamSlotSym(slotNumber + 1, this, pairType);
  824. }
  825. symPair.low->m_allocated = true;
  826. symPair.low->m_offset = stackSym->m_offset;
  827. symPair.high->m_allocated = true;
  828. symPair.high->m_offset = stackSym->m_offset + 4;
  829. }
  830. else
  831. {
  832. symPair.low = StackSym::New(pairType, this);
  833. symPair.high = StackSym::New(pairType, this);
  834. }
  835. m_int64SymPairMap->Add(symId, symPair);
  836. }
  837. if (opnd->IsSymOpnd())
  838. {
  839. pair.low = IR::SymOpnd::New(symPair.low, opnd->AsSymOpnd()->m_offset, pairType, this);
  840. pair.high = IR::SymOpnd::New(symPair.high, opnd->AsSymOpnd()->m_offset, pairType, this);
  841. }
  842. else
  843. {
  844. pair.low = IR::RegOpnd::New(symPair.low, pairType, this);
  845. pair.high = IR::RegOpnd::New(symPair.high, pairType, this);
  846. }
  847. return pair;
  848. }
  849. void Func::Int64SplitExtendLoopLifetime(Loop* loop)
  850. {
  851. if (!this->IsTopFunc())
  852. {
  853. GetTopFunc()->Int64SplitExtendLoopLifetime(loop);
  854. return;
  855. }
  856. if (m_int64SymPairMap)
  857. {
  858. BVSparse<JitArenaAllocator> *liveOnBackEdgeSyms = loop->regAlloc.liveOnBackEdgeSyms;
  859. FOREACH_BITSET_IN_SPARSEBV(symId, liveOnBackEdgeSyms)
  860. {
  861. Int64SymPair pair;
  862. if (m_int64SymPairMap->TryGetValue(symId, &pair))
  863. {
  864. // If we have replaced any sym that was live on the back edge for 2 other syms
  865. // these 2 syms needs to be live on back edge as well.
  866. liveOnBackEdgeSyms->Set(pair.low->m_id);
  867. liveOnBackEdgeSyms->Set(pair.high->m_id);
  868. }
  869. } NEXT_BITSET_IN_SPARSEBV;
  870. }
  871. }
  872. #endif
  873. #if defined(_M_ARM32_OR_ARM64)
  874. RegNum
  875. Func::GetLocalsPointer() const
  876. {
  877. #ifdef DBG
  878. if (Js::Configuration::Global.flags.IsEnabled(Js::ForceLocalsPtrFlag))
  879. {
  880. return ALT_LOCALS_PTR;
  881. }
  882. #endif
  883. if (GetJITFunctionBody()->HasTry())
  884. {
  885. return ALT_LOCALS_PTR;
  886. }
  887. return RegSP;
  888. }
  889. #endif
  890. void Func::AddSlotArrayCheck(IR::SymOpnd *fieldOpnd)
  891. {
  892. if (PHASE_OFF(Js::ClosureRangeCheckPhase, this))
  893. {
  894. return;
  895. }
  896. Assert(IsTopFunc());
  897. if (this->slotArrayCheckTable == nullptr)
  898. {
  899. this->slotArrayCheckTable = SlotArrayCheckTable::New(m_alloc, 4);
  900. }
  901. PropertySym *propertySym = fieldOpnd->m_sym->AsPropertySym();
  902. uint32 slot = propertySym->m_propertyId;
  903. uint32 *pSlotId = this->slotArrayCheckTable->FindOrInsert(slot, propertySym->m_stackSym->m_id);
  904. if (pSlotId && (*pSlotId == (uint32)-1 || *pSlotId < slot))
  905. {
  906. *pSlotId = propertySym->m_propertyId;
  907. }
  908. }
  909. void Func::AddFrameDisplayCheck(IR::SymOpnd *fieldOpnd, uint32 slotId)
  910. {
  911. if (PHASE_OFF(Js::ClosureRangeCheckPhase, this))
  912. {
  913. return;
  914. }
  915. Assert(IsTopFunc());
  916. if (this->frameDisplayCheckTable == nullptr)
  917. {
  918. this->frameDisplayCheckTable = FrameDisplayCheckTable::New(m_alloc, 4);
  919. }
  920. PropertySym *propertySym = fieldOpnd->m_sym->AsPropertySym();
  921. FrameDisplayCheckRecord **record = this->frameDisplayCheckTable->FindOrInsertNew(propertySym->m_stackSym->m_id);
  922. if (*record == nullptr)
  923. {
  924. *record = JitAnew(m_alloc, FrameDisplayCheckRecord);
  925. }
  926. uint32 frameDisplaySlot = propertySym->m_propertyId;
  927. if ((*record)->table == nullptr || (*record)->slotId < frameDisplaySlot)
  928. {
  929. (*record)->slotId = frameDisplaySlot;
  930. }
  931. if (slotId != (uint32)-1)
  932. {
  933. if ((*record)->table == nullptr)
  934. {
  935. (*record)->table = SlotArrayCheckTable::New(m_alloc, 4);
  936. }
  937. uint32 *pSlotId = (*record)->table->FindOrInsert(slotId, frameDisplaySlot);
  938. if (pSlotId && *pSlotId < slotId)
  939. {
  940. *pSlotId = slotId;
  941. }
  942. }
  943. }
  944. void Func::InitLocalClosureSyms()
  945. {
  946. Assert(this->m_localClosureSym == nullptr);
  947. // Allocate stack space for closure pointers. Do this only if we're jitting for stack closures, and
  948. // tell bailout that these are not byte code symbols so that we don't try to encode them in the bailout record,
  949. // as they don't have normal lifetimes.
  950. Js::RegSlot regSlot = GetJITFunctionBody()->GetLocalClosureReg();
  951. if (regSlot != Js::Constants::NoRegister)
  952. {
  953. this->m_localClosureSym =
  954. StackSym::FindOrCreate(static_cast<SymID>(regSlot),
  955. this->DoStackFrameDisplay() ? (Js::RegSlot)-1 : regSlot,
  956. this);
  957. }
  958. regSlot = this->GetJITFunctionBody()->GetParamClosureReg();
  959. if (regSlot != Js::Constants::NoRegister)
  960. {
  961. Assert(this->GetParamClosureSym() == nullptr && !this->GetJITFunctionBody()->IsParamAndBodyScopeMerged());
  962. this->m_paramClosureSym =
  963. StackSym::FindOrCreate(static_cast<SymID>(regSlot),
  964. this->DoStackFrameDisplay() ? (Js::RegSlot) - 1 : regSlot,
  965. this);
  966. }
  967. regSlot = GetJITFunctionBody()->GetLocalFrameDisplayReg();
  968. if (regSlot != Js::Constants::NoRegister)
  969. {
  970. this->m_localFrameDisplaySym =
  971. StackSym::FindOrCreate(static_cast<SymID>(regSlot),
  972. this->DoStackFrameDisplay() ? (Js::RegSlot)-1 : regSlot,
  973. this);
  974. }
  975. }
  976. bool
  977. Func::IsTrackCompoundedIntOverflowDisabled() const
  978. {
  979. return (HasProfileInfo() && GetReadOnlyProfileInfo()->IsTrackCompoundedIntOverflowDisabled()) || m_output.IsTrackCompoundedIntOverflowDisabled();
  980. }
  981. bool
  982. Func::IsMemOpDisabled() const
  983. {
  984. return (HasProfileInfo() && GetReadOnlyProfileInfo()->IsMemOpDisabled()) || m_output.IsMemOpDisabled();
  985. }
  986. bool
  987. Func::IsArrayCheckHoistDisabled() const
  988. {
  989. return (HasProfileInfo() && GetReadOnlyProfileInfo()->IsArrayCheckHoistDisabled(IsLoopBody())) || m_output.IsArrayCheckHoistDisabled();
  990. }
  991. bool
  992. Func::IsStackArgOptDisabled() const
  993. {
  994. return (HasProfileInfo() && GetReadOnlyProfileInfo()->IsStackArgOptDisabled()) || m_output.IsStackArgOptDisabled();
  995. }
  996. bool
  997. Func::IsSwitchOptDisabled() const
  998. {
  999. return (HasProfileInfo() && GetReadOnlyProfileInfo()->IsSwitchOptDisabled()) || m_output.IsSwitchOptDisabled();
  1000. }
  1001. bool
  1002. Func::IsAggressiveIntTypeSpecDisabled() const
  1003. {
  1004. return (HasProfileInfo() && GetReadOnlyProfileInfo()->IsAggressiveIntTypeSpecDisabled(IsLoopBody())) || m_output.IsAggressiveIntTypeSpecDisabled();
  1005. }
  1006. bool Func::CanAllocInPreReservedHeapPageSegment ()
  1007. {
  1008. #ifdef _CONTROL_FLOW_GUARD
  1009. return PHASE_FORCE1(Js::PreReservedHeapAllocPhase) || (!PHASE_OFF1(Js::PreReservedHeapAllocPhase) &&
  1010. !IsJitInDebugMode()
  1011. #if _M_IX86
  1012. && m_workItem->GetJitMode() == ExecutionMode::FullJit
  1013. #if ENABLE_OOP_NATIVE_CODEGEN
  1014. && (JITManager::GetJITManager()->IsJITServer()
  1015. ? GetOOPCodeGenAllocators()->canCreatePreReservedSegment
  1016. : GetInProcCodeGenAllocators()->canCreatePreReservedSegment)
  1017. #else
  1018. && GetInProcCodeGenAllocators()->canCreatePreReservedSegment
  1019. #endif
  1020. );
  1021. #elif TARGET_64
  1022. && true);
  1023. #else
  1024. && false); //Not yet implemented for architectures other than x86 and amd64.
  1025. #endif //_M_ARCH
  1026. #else
  1027. return false;
  1028. #endif//_CONTROL_FLOW_GUARD
  1029. }
  1030. ///----------------------------------------------------------------------------
  1031. ///
  1032. /// Func::GetInstrCount
  1033. ///
  1034. /// Returns the number of instrs.
  1035. /// Note: It counts all instrs for now, including labels, etc.
  1036. ///
  1037. ///----------------------------------------------------------------------------
  1038. uint32
  1039. Func::GetInstrCount()
  1040. {
  1041. uint instrCount = 0;
  1042. FOREACH_INSTR_IN_FUNC(instr, this)
  1043. {
  1044. instrCount++;
  1045. }NEXT_INSTR_IN_FUNC;
  1046. return instrCount;
  1047. }
  1048. ///----------------------------------------------------------------------------
  1049. ///
  1050. /// Func::NumberInstrs
  1051. ///
  1052. /// Number each instruction in order of appearance in the function.
  1053. ///
  1054. ///----------------------------------------------------------------------------
  1055. void
  1056. Func::NumberInstrs()
  1057. {
  1058. #if DBG_DUMP
  1059. Assert(this->IsTopFunc());
  1060. Assert(!this->hasInstrNumber);
  1061. this->hasInstrNumber = true;
  1062. #endif
  1063. uint instrCount = 1;
  1064. FOREACH_INSTR_IN_FUNC(instr, this)
  1065. {
  1066. instr->SetNumber(instrCount++);
  1067. }
  1068. NEXT_INSTR_IN_FUNC;
  1069. }
  1070. #if DBG
  1071. BVSparse<JitArenaAllocator>* Func::GetByteCodeOffsetUses(uint offset) const
  1072. {
  1073. InstrByteCodeRegisterUses uses;
  1074. if (byteCodeRegisterUses->TryGetValue(offset, &uses))
  1075. {
  1076. return uses.bv;
  1077. }
  1078. return nullptr;
  1079. }
  1080. ///----------------------------------------------------------------------------
  1081. ///
  1082. /// Func::IsInPhase
  1083. ///
  1084. /// Determines whether the function is currently in the provided phase
  1085. ///
  1086. ///----------------------------------------------------------------------------
  1087. bool
  1088. Func::IsInPhase(Js::Phase tag)
  1089. {
  1090. return this->GetTopFunc()->currentPhases.Contains(tag);
  1091. }
  1092. #endif
  1093. ///----------------------------------------------------------------------------
  1094. ///
  1095. /// Func::BeginPhase
  1096. ///
  1097. /// Takes care of the profiler
  1098. ///
  1099. ///----------------------------------------------------------------------------
  1100. void
  1101. Func::BeginPhase(Js::Phase tag)
  1102. {
  1103. #ifdef DBG
  1104. this->GetTopFunc()->currentPhases.Push(tag);
  1105. if (PHASE_DEBUGBREAK_ON_PHASE_BEGIN(tag, this))
  1106. {
  1107. __debugbreak();
  1108. }
  1109. #endif
  1110. #ifdef PROFILE_EXEC
  1111. AssertMsg((this->m_codeGenProfiler != nullptr) == Js::Configuration::Global.flags.IsEnabled(Js::ProfileFlag),
  1112. "Profiler tag is supplied but the profiler pointer is NULL");
  1113. if (this->m_codeGenProfiler)
  1114. {
  1115. this->m_codeGenProfiler->ProfileBegin(tag);
  1116. }
  1117. #endif
  1118. }
  1119. ///----------------------------------------------------------------------------
  1120. ///
  1121. /// Func::EndPhase
  1122. ///
  1123. /// Takes care of the profiler and dumper
  1124. ///
  1125. ///----------------------------------------------------------------------------
  1126. void
  1127. Func::EndProfiler(Js::Phase tag)
  1128. {
  1129. #ifdef DBG
  1130. Assert(this->GetTopFunc()->currentPhases.Count() > 0);
  1131. Js::Phase popped = this->GetTopFunc()->currentPhases.Pop();
  1132. Assert(tag == popped);
  1133. #endif
  1134. #ifdef PROFILE_EXEC
  1135. AssertMsg((this->m_codeGenProfiler != nullptr) == Js::Configuration::Global.flags.IsEnabled(Js::ProfileFlag),
  1136. "Profiler tag is supplied but the profiler pointer is NULL");
  1137. if (this->m_codeGenProfiler)
  1138. {
  1139. this->m_codeGenProfiler->ProfileEnd(tag);
  1140. }
  1141. #endif
  1142. }
  1143. void
  1144. Func::EndPhase(Js::Phase tag, bool dump)
  1145. {
  1146. this->EndProfiler(tag);
  1147. #if DBG_DUMP
  1148. if(dump && (PHASE_DUMP(tag, this)
  1149. || PHASE_DUMP(Js::BackEndPhase, this)))
  1150. {
  1151. Output::Print(_u("-----------------------------------------------------------------------------\n"));
  1152. if (IsLoopBody())
  1153. {
  1154. Output::Print(_u("************ IR after %s (%S) Loop %d ************\n"),
  1155. Js::PhaseNames[tag],
  1156. ExecutionModeName(m_workItem->GetJitMode()),
  1157. m_workItem->GetLoopNumber());
  1158. }
  1159. else
  1160. {
  1161. Output::Print(_u("************ IR after %s (%S) ************\n"),
  1162. Js::PhaseNames[tag],
  1163. ExecutionModeName(m_workItem->GetJitMode()));
  1164. }
  1165. this->Dump(Js::Configuration::Global.flags.AsmDiff? IRDumpFlags_AsmDumpMode : IRDumpFlags_None);
  1166. }
  1167. #endif
  1168. if (tag == Js::RegAllocPhase)
  1169. {
  1170. this->legalizePostRegAlloc = true;
  1171. }
  1172. #if DBG
  1173. if (tag == Js::LowererPhase)
  1174. {
  1175. Assert(!this->isPostLower);
  1176. this->isPostLower = true;
  1177. }
  1178. else if (tag == Js::RegAllocPhase)
  1179. {
  1180. Assert(!this->isPostRegAlloc);
  1181. this->isPostRegAlloc = true;
  1182. }
  1183. else if (tag == Js::PeepsPhase)
  1184. {
  1185. Assert(this->isPostLower && !this->isPostLayout);
  1186. this->isPostPeeps = true;
  1187. }
  1188. else if (tag == Js::LayoutPhase)
  1189. {
  1190. Assert(this->isPostPeeps && !this->isPostLayout);
  1191. this->isPostLayout = true;
  1192. }
  1193. else if (tag == Js::FinalLowerPhase)
  1194. {
  1195. Assert(this->isPostLayout && !this->isPostFinalLower);
  1196. this->isPostFinalLower = true;
  1197. }
  1198. if (this->isPostLower)
  1199. {
  1200. #ifndef _M_ARM // Need to verify ARM is clean.
  1201. DbCheckPostLower dbCheck(this);
  1202. dbCheck.Check();
  1203. #endif
  1204. }
  1205. this->m_alloc->MergeDelayFreeList();
  1206. #endif
  1207. }
  1208. StackSym *
  1209. Func::EnsureLoopParamSym()
  1210. {
  1211. if (this->m_loopParamSym == nullptr)
  1212. {
  1213. this->m_loopParamSym = StackSym::New(TyMachPtr, this);
  1214. }
  1215. return this->m_loopParamSym;
  1216. }
  1217. void
  1218. Func::UpdateMaxInlineeArgOutSize(uint inlineeArgOutSize)
  1219. {
  1220. if (this->maxInlineeArgOutSize < inlineeArgOutSize)
  1221. {
  1222. this->maxInlineeArgOutSize = inlineeArgOutSize;
  1223. }
  1224. }
  1225. void
  1226. Func::BeginClone(Lowerer * lowerer, JitArenaAllocator *alloc)
  1227. {
  1228. Assert(this->IsTopFunc());
  1229. AssertMsg(m_cloner == nullptr, "Starting new clone while one is in progress");
  1230. m_cloner = JitAnew(alloc, Cloner, lowerer, alloc);
  1231. if (m_cloneMap == nullptr)
  1232. {
  1233. m_cloneMap = JitAnew(alloc, InstrMap, alloc, 7);
  1234. }
  1235. }
  1236. void
  1237. Func::EndClone()
  1238. {
  1239. Assert(this->IsTopFunc());
  1240. if (m_cloner)
  1241. {
  1242. m_cloner->Finish();
  1243. JitAdelete(m_cloner->alloc, m_cloner);
  1244. m_cloner = nullptr;
  1245. }
  1246. }
  1247. IR::SymOpnd *
  1248. Func::GetInlineeOpndAtOffset(int32 offset)
  1249. {
  1250. Assert(IsInlinee());
  1251. StackSym *stackSym = CreateInlineeStackSym();
  1252. this->SetArgOffset(stackSym, stackSym->m_offset + offset);
  1253. Assert(stackSym->m_offset >= 0);
  1254. return IR::SymOpnd::New(stackSym, 0, TyMachReg, this);
  1255. }
  1256. StackSym *
  1257. Func::CreateInlineeStackSym()
  1258. {
  1259. // Make sure this is an inlinee and that GlobOpt has initialized the offset
  1260. // in the inlinee's frame.
  1261. Assert(IsInlinee());
  1262. Assert(m_inlineeFrameStartSym->m_offset != -1);
  1263. StackSym *stackSym = m_symTable->GetArgSlotSym((Js::ArgSlot)-1);
  1264. stackSym->m_isInlinedArgSlot = true;
  1265. stackSym->m_offset = m_inlineeFrameStartSym->m_offset;
  1266. stackSym->m_allocated = true;
  1267. return stackSym;
  1268. }
  1269. uint16
  1270. Func::GetArgUsedForBranch() const
  1271. {
  1272. // this value can change while JITing, so or these together
  1273. return GetJITFunctionBody()->GetArgUsedForBranch() | GetJITOutput()->GetArgUsedForBranch();
  1274. }
  1275. intptr_t
  1276. Func::GetJittedLoopIterationsSinceLastBailoutAddress() const
  1277. {
  1278. Assert(this->m_workItem->Type() == JsLoopBodyWorkItemType);
  1279. return m_workItem->GetJittedLoopIterationsSinceLastBailoutAddr();
  1280. }
  1281. intptr_t
  1282. Func::GetWeakFuncRef() const
  1283. {
  1284. // TODO: OOP JIT figure out if this can be null
  1285. return m_workItem->GetJITTimeInfo()->GetWeakFuncRef();
  1286. }
  1287. intptr_t
  1288. Func::GetRuntimeInlineCache(const uint index) const
  1289. {
  1290. if(m_runtimeInfo != nullptr && m_runtimeInfo->HasClonedInlineCaches())
  1291. {
  1292. intptr_t inlineCache = m_runtimeInfo->GetClonedInlineCache(index);
  1293. if(inlineCache)
  1294. {
  1295. return inlineCache;
  1296. }
  1297. }
  1298. return GetJITFunctionBody()->GetInlineCache(index);
  1299. }
  1300. JITTimePolymorphicInlineCache *
  1301. Func::GetRuntimePolymorphicInlineCache(const uint index) const
  1302. {
  1303. if (this->m_polymorphicInlineCacheInfo && this->m_polymorphicInlineCacheInfo->HasInlineCaches())
  1304. {
  1305. return this->m_polymorphicInlineCacheInfo->GetInlineCache(index);
  1306. }
  1307. return nullptr;
  1308. }
  1309. byte
  1310. Func::GetPolyCacheUtilToInitialize(const uint index) const
  1311. {
  1312. return this->GetRuntimePolymorphicInlineCache(index) ? this->GetPolyCacheUtil(index) : PolymorphicInlineCacheUtilizationMinValue;
  1313. }
  1314. byte
  1315. Func::GetPolyCacheUtil(const uint index) const
  1316. {
  1317. return this->m_polymorphicInlineCacheInfo->GetUtil(index);
  1318. }
  1319. ObjTypeSpecFldInfo*
  1320. Func::GetObjTypeSpecFldInfo(const uint index) const
  1321. {
  1322. if (GetJITFunctionBody()->GetInlineCacheCount() == 0)
  1323. {
  1324. Assert(UNREACHED);
  1325. return nullptr;
  1326. }
  1327. return GetWorkItem()->GetJITTimeInfo()->GetObjTypeSpecFldInfo(index);
  1328. }
  1329. ObjTypeSpecFldInfo*
  1330. Func::GetGlobalObjTypeSpecFldInfo(uint propertyInfoId) const
  1331. {
  1332. Assert(propertyInfoId < GetTopFunc()->GetWorkItem()->GetJITTimeInfo()->GetGlobalObjTypeSpecFldInfoCount());
  1333. return GetTopFunc()->m_globalObjTypeSpecFldInfoArray[propertyInfoId];
  1334. }
  1335. void
  1336. Func::EnsurePinnedTypeRefs()
  1337. {
  1338. if (this->pinnedTypeRefs == nullptr)
  1339. {
  1340. this->pinnedTypeRefs = JitAnew(this->m_alloc, TypeRefSet, this->m_alloc);
  1341. }
  1342. }
  1343. void
  1344. Func::PinTypeRef(void* typeRef)
  1345. {
  1346. EnsurePinnedTypeRefs();
  1347. this->pinnedTypeRefs->AddNew(typeRef);
  1348. }
  1349. void
  1350. Func::EnsureSingleTypeGuards()
  1351. {
  1352. if (this->singleTypeGuards == nullptr)
  1353. {
  1354. this->singleTypeGuards = JitAnew(this->m_alloc, TypePropertyGuardDictionary, this->m_alloc);
  1355. }
  1356. }
  1357. Js::JitTypePropertyGuard*
  1358. Func::GetOrCreateSingleTypeGuard(intptr_t typeAddr)
  1359. {
  1360. EnsureSingleTypeGuards();
  1361. Js::JitTypePropertyGuard* guard = nullptr;
  1362. if (!this->singleTypeGuards->TryGetValue(typeAddr, &guard))
  1363. {
  1364. // Property guards are allocated by NativeCodeData::Allocator so that their lifetime extends as long as the EntryPointInfo is alive.
  1365. guard = NativeCodeDataNewNoFixup(GetNativeCodeDataAllocator(), Js::JitTypePropertyGuard, typeAddr, this->indexedPropertyGuardCount++);
  1366. this->singleTypeGuards->Add(typeAddr, guard);
  1367. }
  1368. else
  1369. {
  1370. Assert(guard->GetTypeAddr() == typeAddr);
  1371. }
  1372. return guard;
  1373. }
  1374. void
  1375. Func::EnsureEquivalentTypeGuards()
  1376. {
  1377. AssertMsg(!PHASE_OFF(Js::EquivObjTypeSpecPhase, this), "Why do we have equivalent type guards if we don't do equivalent object type spec?");
  1378. if (this->equivalentTypeGuards == nullptr)
  1379. {
  1380. this->equivalentTypeGuards = JitAnew(this->m_alloc, EquivalentTypeGuardList, this->m_alloc);
  1381. }
  1382. }
  1383. Js::JitEquivalentTypeGuard*
  1384. Func::CreateEquivalentTypeGuard(JITTypeHolder type, uint32 objTypeSpecFldId)
  1385. {
  1386. EnsureEquivalentTypeGuards();
  1387. Js::JitEquivalentTypeGuard* guard = NativeCodeDataNewNoFixup(GetNativeCodeDataAllocator(), Js::JitEquivalentTypeGuard, type->GetAddr(), this->indexedPropertyGuardCount++, objTypeSpecFldId);
  1388. this->InitializeEquivalentTypeGuard(guard);
  1389. return guard;
  1390. }
  1391. Js::JitPolyEquivalentTypeGuard*
  1392. Func::CreatePolyEquivalentTypeGuard(uint32 objTypeSpecFldId)
  1393. {
  1394. EnsureEquivalentTypeGuards();
  1395. Js::JitPolyEquivalentTypeGuard* guard = NativeCodeDataNewNoFixup(GetNativeCodeDataAllocator(), Js::JitPolyEquivalentTypeGuard, this->indexedPropertyGuardCount++, objTypeSpecFldId);
  1396. this->InitializeEquivalentTypeGuard(guard);
  1397. return guard;
  1398. }
  1399. void
  1400. Func::InitializeEquivalentTypeGuard(Js::JitEquivalentTypeGuard * guard)
  1401. {
  1402. // If we want to hard code the address of the cache, we will need to go back to allocating it from the native code data allocator.
  1403. // We would then need to maintain consistency (double write) to both the recycler allocated cache and the one on the heap.
  1404. Js::EquivalentTypeCache* cache = nullptr;
  1405. if (this->IsOOPJIT())
  1406. {
  1407. cache = JitAnewZ(this->m_alloc, Js::EquivalentTypeCache);
  1408. }
  1409. else
  1410. {
  1411. cache = NativeCodeDataNewZNoFixup(GetTransferDataAllocator(), Js::EquivalentTypeCache);
  1412. }
  1413. guard->SetCache(cache);
  1414. // Give the cache a back-pointer to the guard so that the guard can be cleared at runtime if necessary.
  1415. cache->SetGuard(guard);
  1416. this->equivalentTypeGuards->Prepend(guard);
  1417. }
  1418. void
  1419. Func::EnsurePropertyGuardsByPropertyId()
  1420. {
  1421. if (this->propertyGuardsByPropertyId == nullptr)
  1422. {
  1423. this->propertyGuardsByPropertyId = JitAnew(this->m_alloc, PropertyGuardByPropertyIdMap, this->m_alloc);
  1424. }
  1425. }
  1426. void
  1427. Func::EnsureCtorCachesByPropertyId()
  1428. {
  1429. if (this->ctorCachesByPropertyId == nullptr)
  1430. {
  1431. this->ctorCachesByPropertyId = JitAnew(this->m_alloc, CtorCachesByPropertyIdMap, this->m_alloc);
  1432. }
  1433. }
  1434. void
  1435. Func::LinkGuardToPropertyId(Js::PropertyId propertyId, Js::JitIndexedPropertyGuard* guard)
  1436. {
  1437. Assert(guard != nullptr);
  1438. Assert(guard->GetValue() != NULL);
  1439. Assert(this->propertyGuardsByPropertyId != nullptr);
  1440. IndexedPropertyGuardSet* set;
  1441. if (!this->propertyGuardsByPropertyId->TryGetValue(propertyId, &set))
  1442. {
  1443. set = JitAnew(this->m_alloc, IndexedPropertyGuardSet, this->m_alloc);
  1444. this->propertyGuardsByPropertyId->Add(propertyId, set);
  1445. }
  1446. set->Item(guard);
  1447. }
  1448. void
  1449. Func::LinkCtorCacheToPropertyId(Js::PropertyId propertyId, JITTimeConstructorCache* cache)
  1450. {
  1451. Assert(cache != nullptr);
  1452. Assert(this->ctorCachesByPropertyId != nullptr);
  1453. CtorCacheSet* set;
  1454. if (!this->ctorCachesByPropertyId->TryGetValue(propertyId, &set))
  1455. {
  1456. set = JitAnew(this->m_alloc, CtorCacheSet, this->m_alloc);
  1457. this->ctorCachesByPropertyId->Add(propertyId, set);
  1458. }
  1459. set->Item(cache->GetRuntimeCacheAddr());
  1460. }
  1461. JITTimeConstructorCache* Func::GetConstructorCache(const Js::ProfileId profiledCallSiteId)
  1462. {
  1463. AssertOrFailFast(profiledCallSiteId < GetJITFunctionBody()->GetProfiledCallSiteCount());
  1464. Assert(this->constructorCaches != nullptr);
  1465. return this->constructorCaches[profiledCallSiteId];
  1466. }
  1467. void Func::SetConstructorCache(const Js::ProfileId profiledCallSiteId, JITTimeConstructorCache* constructorCache)
  1468. {
  1469. AssertOrFailFast(profiledCallSiteId < GetJITFunctionBody()->GetProfiledCallSiteCount());
  1470. Assert(constructorCache != nullptr);
  1471. Assert(this->constructorCaches != nullptr);
  1472. Assert(this->constructorCaches[profiledCallSiteId] == nullptr);
  1473. this->constructorCacheCount++;
  1474. this->constructorCaches[profiledCallSiteId] = constructorCache;
  1475. }
  1476. void Func::EnsurePropertiesWrittenTo()
  1477. {
  1478. if (this->propertiesWrittenTo == nullptr)
  1479. {
  1480. this->propertiesWrittenTo = JitAnew(this->m_alloc, PropertyIdSet, this->m_alloc);
  1481. }
  1482. }
  1483. void Func::EnsureCallSiteToArgumentsOffsetFixupMap()
  1484. {
  1485. if (this->callSiteToArgumentsOffsetFixupMap == nullptr)
  1486. {
  1487. this->callSiteToArgumentsOffsetFixupMap = JitAnew(this->m_alloc, CallSiteToArgumentsOffsetFixupMap, this->m_alloc);
  1488. }
  1489. }
  1490. IR::LabelInstr *
  1491. Func::GetFuncStartLabel()
  1492. {
  1493. return m_funcStartLabel;
  1494. }
  1495. IR::LabelInstr *
  1496. Func::EnsureFuncStartLabel()
  1497. {
  1498. if(m_funcStartLabel == nullptr)
  1499. {
  1500. m_funcStartLabel = IR::LabelInstr::New( Js::OpCode::Label, this );
  1501. m_funcStartLabel->m_isDataLabel = true;
  1502. }
  1503. return m_funcStartLabel;
  1504. }
  1505. IR::LabelInstr *
  1506. Func::GetFuncEndLabel()
  1507. {
  1508. return m_funcEndLabel;
  1509. }
  1510. IR::LabelInstr *
  1511. Func::EnsureFuncEndLabel()
  1512. {
  1513. if(m_funcEndLabel == nullptr)
  1514. {
  1515. m_funcEndLabel = IR::LabelInstr::New( Js::OpCode::Label, this );
  1516. m_funcEndLabel->m_isDataLabel = true;
  1517. }
  1518. return m_funcEndLabel;
  1519. }
  1520. void
  1521. Func::EnsureStackArgWithFormalsTracker()
  1522. {
  1523. if (stackArgWithFormalsTracker == nullptr)
  1524. {
  1525. stackArgWithFormalsTracker = JitAnew(m_alloc, StackArgWithFormalsTracker, m_alloc);
  1526. }
  1527. }
  1528. BOOL
  1529. Func::IsFormalsArraySym(SymID symId)
  1530. {
  1531. if (stackArgWithFormalsTracker == nullptr || stackArgWithFormalsTracker->GetFormalsArraySyms() == nullptr)
  1532. {
  1533. return false;
  1534. }
  1535. return stackArgWithFormalsTracker->GetFormalsArraySyms()->Test(symId);
  1536. }
  1537. void
  1538. Func::TrackFormalsArraySym(SymID symId)
  1539. {
  1540. EnsureStackArgWithFormalsTracker();
  1541. stackArgWithFormalsTracker->SetFormalsArraySyms(symId);
  1542. }
  1543. void
  1544. Func::TrackStackSymForFormalIndex(Js::ArgSlot formalsIndex, StackSym * sym)
  1545. {
  1546. EnsureStackArgWithFormalsTracker();
  1547. Js::ArgSlot formalsCount = GetJITFunctionBody()->GetInParamsCount() - 1;
  1548. stackArgWithFormalsTracker->SetStackSymInFormalsIndexMap(sym, formalsIndex, formalsCount);
  1549. }
  1550. StackSym *
  1551. Func::GetStackSymForFormal(Js::ArgSlot formalsIndex)
  1552. {
  1553. if (stackArgWithFormalsTracker == nullptr || stackArgWithFormalsTracker->GetFormalsIndexToStackSymMap() == nullptr)
  1554. {
  1555. return nullptr;
  1556. }
  1557. Js::ArgSlot formalsCount = GetJITFunctionBody()->GetInParamsCount() - 1;
  1558. StackSym ** formalsIndexToStackSymMap = stackArgWithFormalsTracker->GetFormalsIndexToStackSymMap();
  1559. AssertMsg(formalsIndex < formalsCount, "OutOfRange ? ");
  1560. return formalsIndexToStackSymMap[formalsIndex];
  1561. }
  1562. bool
  1563. Func::HasStackSymForFormal(Js::ArgSlot formalsIndex)
  1564. {
  1565. if (stackArgWithFormalsTracker == nullptr || stackArgWithFormalsTracker->GetFormalsIndexToStackSymMap() == nullptr)
  1566. {
  1567. return false;
  1568. }
  1569. return GetStackSymForFormal(formalsIndex) != nullptr;
  1570. }
  1571. void
  1572. Func::SetScopeObjSym(StackSym * sym)
  1573. {
  1574. EnsureStackArgWithFormalsTracker();
  1575. stackArgWithFormalsTracker->SetScopeObjSym(sym);
  1576. }
  1577. StackSym *
  1578. Func::GetNativeCodeDataSym() const
  1579. {
  1580. Assert(IsOOPJIT());
  1581. return m_nativeCodeDataSym;
  1582. }
  1583. void
  1584. Func::SetNativeCodeDataSym(StackSym * opnd)
  1585. {
  1586. Assert(IsOOPJIT());
  1587. m_nativeCodeDataSym = opnd;
  1588. }
  1589. StackSym*
  1590. Func::GetScopeObjSym()
  1591. {
  1592. if (stackArgWithFormalsTracker == nullptr)
  1593. {
  1594. return nullptr;
  1595. }
  1596. return stackArgWithFormalsTracker->GetScopeObjSym();
  1597. }
  1598. BVSparse<JitArenaAllocator> *
  1599. StackArgWithFormalsTracker::GetFormalsArraySyms()
  1600. {
  1601. return formalsArraySyms;
  1602. }
  1603. void
  1604. StackArgWithFormalsTracker::SetFormalsArraySyms(SymID symId)
  1605. {
  1606. if (formalsArraySyms == nullptr)
  1607. {
  1608. formalsArraySyms = JitAnew(alloc, BVSparse<JitArenaAllocator>, alloc);
  1609. }
  1610. formalsArraySyms->Set(symId);
  1611. }
  1612. StackSym **
  1613. StackArgWithFormalsTracker::GetFormalsIndexToStackSymMap()
  1614. {
  1615. return formalsIndexToStackSymMap;
  1616. }
  1617. void
  1618. StackArgWithFormalsTracker::SetStackSymInFormalsIndexMap(StackSym * sym, Js::ArgSlot formalsIndex, Js::ArgSlot formalsCount)
  1619. {
  1620. if(formalsIndexToStackSymMap == nullptr)
  1621. {
  1622. formalsIndexToStackSymMap = JitAnewArrayZ(alloc, StackSym*, formalsCount);
  1623. }
  1624. AssertMsg(formalsIndex < formalsCount, "Out of range ?");
  1625. formalsIndexToStackSymMap[formalsIndex] = sym;
  1626. }
  1627. void
  1628. StackArgWithFormalsTracker::SetScopeObjSym(StackSym * sym)
  1629. {
  1630. m_scopeObjSym = sym;
  1631. }
  1632. StackSym *
  1633. StackArgWithFormalsTracker::GetScopeObjSym()
  1634. {
  1635. return m_scopeObjSym;
  1636. }
  1637. void
  1638. Cloner::AddInstr(IR::Instr * instrOrig, IR::Instr * instrClone)
  1639. {
  1640. if (!this->instrFirst)
  1641. {
  1642. this->instrFirst = instrClone;
  1643. }
  1644. this->instrLast = instrClone;
  1645. }
  1646. void
  1647. Cloner::Finish()
  1648. {
  1649. this->RetargetClonedBranches();
  1650. if (this->lowerer)
  1651. {
  1652. lowerer->LowerRange(this->instrFirst, this->instrLast, false, false);
  1653. }
  1654. }
  1655. void
  1656. Cloner::RetargetClonedBranches()
  1657. {
  1658. if (!this->fRetargetClonedBranch)
  1659. {
  1660. return;
  1661. }
  1662. FOREACH_INSTR_IN_RANGE(instr, this->instrFirst, this->instrLast)
  1663. {
  1664. if (instr->IsBranchInstr())
  1665. {
  1666. instr->AsBranchInstr()->RetargetClonedBranch();
  1667. }
  1668. }
  1669. NEXT_INSTR_IN_RANGE;
  1670. }
  1671. void Func::ThrowIfScriptClosed()
  1672. {
  1673. if (GetScriptContextInfo()->IsClosed())
  1674. {
  1675. // Should not be jitting something in the foreground when the script context is actually closed
  1676. Assert(IsBackgroundJIT() || !GetScriptContext()->IsActuallyClosed());
  1677. throw Js::OperationAbortedException();
  1678. }
  1679. }
  1680. IR::IndirOpnd * Func::GetConstantAddressIndirOpnd(intptr_t address, IR::Opnd * largeConstOpnd, IR::AddrOpndKind kind, IRType type, Js::OpCode loadOpCode)
  1681. {
  1682. Assert(this->GetTopFunc() == this);
  1683. if (!canHoistConstantAddressLoad)
  1684. {
  1685. // We can't hoist constant address load after lower, as we can't mark the sym as
  1686. // live on back edge
  1687. return nullptr;
  1688. }
  1689. int offset = 0;
  1690. IR::RegOpnd ** foundRegOpnd = this->constantAddressRegOpnd.Find([address, &offset](IR::RegOpnd * regOpnd)
  1691. {
  1692. Assert(regOpnd->m_sym->IsSingleDef());
  1693. Assert(regOpnd->m_sym->m_instrDef->GetSrc1()->IsAddrOpnd() || regOpnd->m_sym->m_instrDef->GetSrc1()->IsIntConstOpnd());
  1694. void * curr = regOpnd->m_sym->m_instrDef->GetSrc1()->IsAddrOpnd() ?
  1695. regOpnd->m_sym->m_instrDef->GetSrc1()->AsAddrOpnd()->m_address :
  1696. (void *)regOpnd->m_sym->m_instrDef->GetSrc1()->AsIntConstOpnd()->GetValue();
  1697. ptrdiff_t diff = (uintptr_t)address - (uintptr_t)curr;
  1698. if (!Math::FitsInDWord(diff))
  1699. {
  1700. return false;
  1701. }
  1702. offset = (int)diff;
  1703. return true;
  1704. });
  1705. IR::RegOpnd * addressRegOpnd;
  1706. if (foundRegOpnd != nullptr)
  1707. {
  1708. addressRegOpnd = *foundRegOpnd;
  1709. }
  1710. else
  1711. {
  1712. Assert(offset == 0);
  1713. addressRegOpnd = IR::RegOpnd::New(TyMachPtr, this);
  1714. IR::Instr *const newInstr =
  1715. IR::Instr::New(
  1716. loadOpCode,
  1717. addressRegOpnd,
  1718. largeConstOpnd,
  1719. this);
  1720. this->constantAddressRegOpnd.Prepend(addressRegOpnd);
  1721. IR::Instr * insertBeforeInstr = this->lastConstantAddressRegLoadInstr;
  1722. if (insertBeforeInstr == nullptr)
  1723. {
  1724. insertBeforeInstr = this->GetFunctionEntryInsertionPoint();
  1725. this->lastConstantAddressRegLoadInstr = newInstr;
  1726. }
  1727. insertBeforeInstr->InsertBefore(newInstr);
  1728. }
  1729. IR::IndirOpnd * indirOpnd = IR::IndirOpnd::New(addressRegOpnd, offset, type, this, true);
  1730. #if DBG_DUMP
  1731. // TODO: michhol make intptr_t
  1732. indirOpnd->SetAddrKind(kind, (void*)address);
  1733. #endif
  1734. return indirOpnd;
  1735. }
  1736. void Func::MarkConstantAddressSyms(BVSparse<JitArenaAllocator> * bv)
  1737. {
  1738. Assert(this->GetTopFunc() == this);
  1739. this->constantAddressRegOpnd.Iterate([bv](IR::RegOpnd * regOpnd)
  1740. {
  1741. bv->Set(regOpnd->m_sym->m_id);
  1742. });
  1743. }
  1744. IR::Instr *
  1745. Func::GetFunctionEntryInsertionPoint()
  1746. {
  1747. Assert(this->GetTopFunc() == this);
  1748. IR::Instr * insertInsert = this->lastConstantAddressRegLoadInstr;
  1749. if (insertInsert != nullptr)
  1750. {
  1751. return insertInsert->m_next;
  1752. }
  1753. insertInsert = this->m_headInstr;
  1754. if (this->HasTry())
  1755. {
  1756. // Insert it inside the root region
  1757. insertInsert = insertInsert->m_next;
  1758. Assert(insertInsert->IsLabelInstr() && insertInsert->AsLabelInstr()->GetRegion()->GetType() == RegionTypeRoot);
  1759. }
  1760. return insertInsert->m_next;
  1761. }
  1762. Js::Var
  1763. Func::AllocateNumber(double value)
  1764. {
  1765. Js::Var number = nullptr;
  1766. #if FLOATVAR
  1767. number = Js::JavascriptNumber::NewCodeGenInstance((double)value, nullptr);
  1768. #else
  1769. if (!IsOOPJIT()) // in-proc jit
  1770. {
  1771. number = Js::JavascriptNumber::NewCodeGenInstance(GetNumberAllocator(), (double)value, GetScriptContext());
  1772. }
  1773. else // OOP JIT
  1774. {
  1775. number = GetXProcNumberAllocator()->AllocateNumber(this, value);
  1776. }
  1777. #endif
  1778. return number;
  1779. }
  1780. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  1781. void
  1782. Func::DumpFullFunctionName()
  1783. {
  1784. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  1785. Output::Print(_u("Function %s (%s)"), GetJITFunctionBody()->GetDisplayName(), GetDebugNumberSet(debugStringBuffer));
  1786. }
  1787. #endif
  1788. void
  1789. Func::UpdateForInLoopMaxDepth(uint forInLoopMaxDepth)
  1790. {
  1791. Assert(this->IsTopFunc());
  1792. this->m_forInLoopMaxDepth = max(this->m_forInLoopMaxDepth, forInLoopMaxDepth);
  1793. }
  1794. int
  1795. Func::GetForInEnumeratorArrayOffset() const
  1796. {
  1797. Func const* topFunc = this->GetTopFunc();
  1798. Assert(this->m_forInLoopBaseDepth + this->GetJITFunctionBody()->GetForInLoopDepth() <= topFunc->m_forInLoopMaxDepth);
  1799. return topFunc->m_forInEnumeratorArrayOffset
  1800. + this->m_forInLoopBaseDepth * sizeof(Js::ForInObjectEnumerator);
  1801. }
  1802. #if DBG_DUMP
  1803. ///----------------------------------------------------------------------------
  1804. ///
  1805. /// Func::DumpHeader
  1806. ///
  1807. ///----------------------------------------------------------------------------
  1808. void
  1809. Func::DumpHeader()
  1810. {
  1811. Output::Print(_u("-----------------------------------------------------------------------------\n"));
  1812. DumpFullFunctionName();
  1813. Output::SkipToColumn(50);
  1814. Output::Print(_u("Instr Count:%d"), GetInstrCount());
  1815. if(m_codeSize > 0)
  1816. {
  1817. Output::Print(_u("\t\tSize:%d\n\n"), m_codeSize);
  1818. }
  1819. else
  1820. {
  1821. Output::Print(_u("\n\n"));
  1822. }
  1823. }
  1824. ///----------------------------------------------------------------------------
  1825. ///
  1826. /// Func::Dump
  1827. ///
  1828. ///----------------------------------------------------------------------------
  1829. void
  1830. Func::Dump(IRDumpFlags flags)
  1831. {
  1832. this->DumpHeader();
  1833. FOREACH_INSTR_IN_FUNC(instr, this)
  1834. {
  1835. instr->DumpGlobOptInstrString();
  1836. instr->Dump(flags);
  1837. }NEXT_INSTR_IN_FUNC;
  1838. Output::Flush();
  1839. }
  1840. void
  1841. Func::Dump()
  1842. {
  1843. this->Dump(IRDumpFlags_None);
  1844. }
  1845. #endif
  1846. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  1847. LPCSTR
  1848. Func::GetVtableName(INT_PTR address)
  1849. {
  1850. #if DBG
  1851. if (vtableMap == nullptr)
  1852. {
  1853. vtableMap = VirtualTableRegistry::CreateVtableHashMap(this->m_alloc);
  1854. };
  1855. LPCSTR name = vtableMap->Lookup(address, nullptr);
  1856. if (name)
  1857. {
  1858. if (strncmp(name, "class ", _countof("class ") - 1) == 0)
  1859. {
  1860. name += _countof("class ") - 1;
  1861. }
  1862. }
  1863. return name;
  1864. #else
  1865. return "";
  1866. #endif
  1867. }
  1868. #endif
  1869. #if DBG_DUMP | defined(VTUNE_PROFILING)
  1870. bool Func::DoRecordNativeMap() const
  1871. {
  1872. #if defined(VTUNE_PROFILING)
  1873. if (VTuneChakraProfile::isJitProfilingActive)
  1874. {
  1875. return true;
  1876. }
  1877. #endif
  1878. #if DBG_DUMP
  1879. return PHASE_DUMP(Js::EncoderPhase, this) && Js::Configuration::Global.flags.Verbose;
  1880. #else
  1881. return false;
  1882. #endif
  1883. }
  1884. #endif
  1885. #ifdef PERF_HINT
  1886. void WritePerfHint(PerfHints hint, Func* func, uint byteCodeOffset /*= Js::Constants::NoByteCodeOffset*/)
  1887. {
  1888. if (!func->IsOOPJIT())
  1889. {
  1890. WritePerfHint(hint, (Js::FunctionBody*)func->GetJITFunctionBody()->GetAddr(), byteCodeOffset);
  1891. }
  1892. }
  1893. #endif