DynamicProfileInfo.cpp 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432
  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 "RuntimeLanguagePch.h"
  6. #if ENABLE_NATIVE_CODEGEN
  7. namespace Js
  8. {
  9. #ifdef DYNAMIC_PROFILE_STORAGE
  10. DynamicProfileInfo::DynamicProfileInfo()
  11. {
  12. hasFunctionBody = false;
  13. }
  14. #endif
  15. struct Allocation
  16. {
  17. uint offset;
  18. size_t size;
  19. };
  20. #if DBG_DUMP || defined(DYNAMIC_PROFILE_STORAGE) || defined(RUNTIME_DATA_COLLECTION)
  21. bool DynamicProfileInfo::NeedProfileInfoList()
  22. {
  23. #pragma prefast(suppress: 6235 6286, "(<non-zero constant> || <expression>) is always a non-zero constant. - This is wrong, DBG_DUMP is not set in some build variants")
  24. return DBG_DUMP
  25. #ifdef DYNAMIC_PROFILE_STORAGE
  26. || DynamicProfileStorage::IsEnabled()
  27. #endif
  28. #ifdef RUNTIME_DATA_COLLECTION
  29. || (Configuration::Global.flags.RuntimeDataOutputFile != nullptr)
  30. #endif
  31. ;
  32. }
  33. #endif
  34. void ArrayCallSiteInfo::SetIsNotNativeIntArray()
  35. {
  36. OUTPUT_TRACE_WITH_STACK(Js::NativeArrayConversionPhase, _u("SetIsNotNativeIntArray \n"));
  37. bits |= NotNativeIntBit;
  38. }
  39. void ArrayCallSiteInfo::SetIsNotNativeFloatArray()
  40. {
  41. OUTPUT_TRACE_WITH_STACK(Js::NativeArrayConversionPhase, _u("SetIsNotNativeFloatArray \n"));
  42. bits |= NotNativeFloatBit;
  43. }
  44. void ArrayCallSiteInfo::SetIsNotNativeArray()
  45. {
  46. OUTPUT_TRACE_WITH_STACK(Js::NativeArrayConversionPhase, _u("SetIsNotNativeArray \n"));
  47. bits = NotNativeIntBit | NotNativeFloatBit;
  48. }
  49. DynamicProfileInfo* DynamicProfileInfo::New(Recycler* recycler, FunctionBody* functionBody, bool persistsAcrossScriptContexts)
  50. {
  51. size_t totalAlloc = 0;
  52. Allocation batch[] =
  53. {
  54. { (uint)offsetof(DynamicProfileInfo, callSiteInfo), functionBody->GetProfiledCallSiteCount() * sizeof(CallSiteInfo) },
  55. { (uint)offsetof(DynamicProfileInfo, ldElemInfo), functionBody->GetProfiledLdElemCount() * sizeof(LdElemInfo) },
  56. { (uint)offsetof(DynamicProfileInfo, stElemInfo), functionBody->GetProfiledStElemCount() * sizeof(StElemInfo) },
  57. { (uint)offsetof(DynamicProfileInfo, arrayCallSiteInfo), functionBody->GetProfiledArrayCallSiteCount() * sizeof(ArrayCallSiteInfo) },
  58. { (uint)offsetof(DynamicProfileInfo, fldInfo), functionBody->GetProfiledFldCount() * sizeof(FldInfo) },
  59. { (uint)offsetof(DynamicProfileInfo, divideTypeInfo), functionBody->GetProfiledDivOrRemCount() * sizeof(ValueType) },
  60. { (uint)offsetof(DynamicProfileInfo, switchTypeInfo), functionBody->GetProfiledSwitchCount() * sizeof(ValueType)},
  61. { (uint)offsetof(DynamicProfileInfo, slotInfo), functionBody->GetProfiledSlotCount() * sizeof(ValueType) },
  62. { (uint)offsetof(DynamicProfileInfo, parameterInfo), functionBody->GetProfiledInParamsCount() * sizeof(ValueType) },
  63. { (uint)offsetof(DynamicProfileInfo, returnTypeInfo), functionBody->GetProfiledReturnTypeCount() * sizeof(ValueType) },
  64. { (uint)offsetof(DynamicProfileInfo, loopImplicitCallFlags), (EnableImplicitCallFlags(functionBody) ? (functionBody->GetLoopCount() * sizeof(ImplicitCallFlags)) : 0) },
  65. { (uint)offsetof(DynamicProfileInfo, loopFlags), functionBody->GetLoopCount() ? BVFixed::GetAllocSize(functionBody->GetLoopCount() * LoopFlags::COUNT) : 0 }
  66. };
  67. for (uint i = 0; i < _countof(batch); i++)
  68. {
  69. totalAlloc += batch[i].size;
  70. }
  71. DynamicProfileInfo* info = nullptr;
  72. // In the profile storage case (-only), always allocate a non-leaf profile
  73. // In the regular profile case, we need to allocate it as non-leaf only if it's
  74. // a profile being used in the in-memory cache. This is because in that case, the profile
  75. // also allocates dynamicProfileFunctionInfo, which it uses to match functions across
  76. // script contexts. In the normal case, since we don't allocate that structure, we
  77. // can be a leaf allocation.
  78. if (persistsAcrossScriptContexts)
  79. {
  80. info = RecyclerNewPlusZ(recycler, totalAlloc, DynamicProfileInfo, functionBody);
  81. #if DBG
  82. info->persistsAcrossScriptContexts = true;
  83. #endif
  84. }
  85. else
  86. {
  87. #if DBG_DUMP || defined(DYNAMIC_PROFILE_STORAGE) || defined(RUNTIME_DATA_COLLECTION)
  88. if (DynamicProfileInfo::NeedProfileInfoList())
  89. {
  90. info = RecyclerNewPlusZ(recycler, totalAlloc, DynamicProfileInfo, functionBody);
  91. }
  92. else
  93. #endif
  94. {
  95. info = RecyclerNewPlusLeafZ(recycler, totalAlloc, DynamicProfileInfo, functionBody);
  96. }
  97. }
  98. BYTE* current = (BYTE*)info + sizeof(DynamicProfileInfo);
  99. for (uint i = 0; i < _countof(batch); i++)
  100. {
  101. if (batch[i].size > 0)
  102. {
  103. BYTE** field = (BYTE**)(((BYTE*)info + batch[i].offset));
  104. *field = current;
  105. current += batch[i].size;
  106. }
  107. }
  108. Assert(current - reinterpret_cast<BYTE*>(info) - sizeof(DynamicProfileInfo) == totalAlloc);
  109. info->Initialize(functionBody);
  110. return info;
  111. }
  112. DynamicProfileInfo::DynamicProfileInfo(FunctionBody * functionBody)
  113. #if DBG_DUMP || defined(DYNAMIC_PROFILE_STORAGE) || defined(RUNTIME_DATA_COLLECTION)
  114. : functionBody(DynamicProfileInfo::NeedProfileInfoList() ? functionBody : nullptr)
  115. #endif
  116. {
  117. hasFunctionBody = true;
  118. #if DBG
  119. persistsAcrossScriptContexts = true;
  120. #endif
  121. }
  122. void DynamicProfileInfo::Initialize(FunctionBody *const functionBody)
  123. {
  124. // Need to make value types uninitialized, which is not equivalent to zero
  125. thisInfo.valueType = ValueType::Uninitialized;
  126. const BVIndex loopFlagsCount = functionBody->GetLoopCount() * LoopFlags::COUNT;
  127. if (loopFlagsCount)
  128. {
  129. this->loopFlags->Init(loopFlagsCount);
  130. LoopFlags defaultValues;
  131. for (uint i = 0; i < functionBody->GetLoopCount(); ++i)
  132. {
  133. this->loopFlags->SetRange(&defaultValues, i * LoopFlags::COUNT, LoopFlags::COUNT);
  134. }
  135. }
  136. for (ProfileId i = 0; i < functionBody->GetProfiledCallSiteCount(); ++i)
  137. {
  138. callSiteInfo[i].returnType = ValueType::Uninitialized;
  139. callSiteInfo[i].u.functionData.sourceId = NoSourceId;
  140. }
  141. for (ProfileId i = 0; i < functionBody->GetProfiledLdElemCount(); ++i)
  142. {
  143. ldElemInfo[i].arrayType = ValueType::Uninitialized;
  144. ldElemInfo[i].elemType = ValueType::Uninitialized;
  145. }
  146. for (ProfileId i = 0; i < functionBody->GetProfiledStElemCount(); ++i)
  147. {
  148. stElemInfo[i].arrayType = ValueType::Uninitialized;
  149. }
  150. for (uint i = 0; i < functionBody->GetProfiledFldCount(); ++i)
  151. {
  152. fldInfo[i].flags = FldInfo_NoInfo;
  153. fldInfo[i].valueType = ValueType::Uninitialized;
  154. fldInfo[i].polymorphicInlineCacheUtilization = PolymorphicInlineCacheUtilizationThreshold;
  155. }
  156. for (ProfileId i = 0; i < functionBody->GetProfiledDivOrRemCount(); ++i)
  157. {
  158. divideTypeInfo[i] = ValueType::Uninitialized;
  159. }
  160. for (ProfileId i = 0; i < functionBody->GetProfiledSwitchCount(); ++i)
  161. {
  162. switchTypeInfo[i] = ValueType::Uninitialized;
  163. }
  164. for (ProfileId i = 0; i < functionBody->GetProfiledSlotCount(); ++i)
  165. {
  166. slotInfo[i] = ValueType::Uninitialized;
  167. }
  168. for (ArgSlot i = 0; i < functionBody->GetProfiledInParamsCount(); ++i)
  169. {
  170. parameterInfo[i] = ValueType::Uninitialized;
  171. }
  172. for (ProfileId i = 0; i < functionBody->GetProfiledReturnTypeCount(); ++i)
  173. {
  174. returnTypeInfo[i] = ValueType::Uninitialized;
  175. }
  176. this->rejitCount = 0;
  177. #if DBG
  178. for (ProfileId i = 0; i < functionBody->GetProfiledArrayCallSiteCount(); ++i)
  179. {
  180. arrayCallSiteInfo[i].functionNumber = functionBody->GetFunctionNumber();
  181. arrayCallSiteInfo[i].callSiteNumber = i;
  182. }
  183. #endif
  184. }
  185. bool DynamicProfileInfo::IsEnabledForAtLeastOneFunction(const ScriptContext *const scriptContext)
  186. {
  187. return IsEnabled_OptionalFunctionBody(nullptr, scriptContext);
  188. }
  189. bool DynamicProfileInfo::IsEnabled(const FunctionBody *const functionBody)
  190. {
  191. Assert(functionBody);
  192. return IsEnabled_OptionalFunctionBody(functionBody, functionBody->GetScriptContext());
  193. }
  194. bool DynamicProfileInfo::IsEnabled_OptionalFunctionBody(const FunctionBody *const functionBody, const ScriptContext *const scriptContext)
  195. {
  196. Assert(scriptContext);
  197. return
  198. !PHASE_OFF_OPTFUNC(DynamicProfilePhase, functionBody) &&
  199. (
  200. #if ENABLE_DEBUG_CONFIG_OPTIONS
  201. PHASE_FORCE_OPTFUNC(DynamicProfilePhase, functionBody) ||
  202. #else
  203. Js::Configuration::Global.flags.ForceDynamicProfile ||
  204. #endif
  205. !scriptContext->GetConfig()->IsNoNative() ||
  206. (functionBody && functionBody->IsInDebugMode())
  207. #ifdef DYNAMIC_PROFILE_STORAGE
  208. || DynamicProfileStorage::DoCollectInfo()
  209. #endif
  210. );
  211. }
  212. bool DynamicProfileInfo::IsEnabledForAtLeastOneFunction(const Js::Phase phase, const ScriptContext *const scriptContext)
  213. {
  214. return IsEnabled_OptionalFunctionBody(phase, nullptr, scriptContext);
  215. }
  216. bool DynamicProfileInfo::IsEnabled(const Js::Phase phase, const FunctionBody *const functionBody)
  217. {
  218. Assert(functionBody);
  219. return IsEnabled_OptionalFunctionBody(phase, functionBody, functionBody->GetScriptContext());
  220. }
  221. bool DynamicProfileInfo::IsEnabled_OptionalFunctionBody(
  222. const Js::Phase phase,
  223. const FunctionBody *const functionBody,
  224. const ScriptContext *const scriptContext)
  225. {
  226. if (!DynamicProfileInfo::IsEnabled_OptionalFunctionBody(functionBody, scriptContext))
  227. {
  228. return false;
  229. }
  230. switch (phase)
  231. {
  232. case Phase::TypedArrayPhase:
  233. case Phase::AggressiveIntTypeSpecPhase:
  234. case Phase::CheckThisPhase:
  235. case Phase::ProfileBasedFldFastPathPhase:
  236. case Phase::ObjTypeSpecPhase:
  237. case Phase::ArrayCheckHoistPhase:
  238. case Phase::SwitchOptPhase:
  239. case Phase::FixedNewObjPhase:
  240. return !PHASE_OFF_PROFILED_BYTE_CODE_OPTFUNC(phase, functionBody);
  241. case Phase::NativeArrayPhase:
  242. case Phase::FloatTypeSpecPhase:
  243. return !PHASE_OFF_PROFILED_BYTE_CODE_OPTFUNC(phase, functionBody)
  244. #ifdef _M_IX86
  245. && AutoSystemInfo::Data.SSE2Available()
  246. #endif
  247. ;
  248. case Phase::InlinePhase:
  249. return !PHASE_OFF_PROFILED_BYTE_CODE_OPTFUNC(Phase::InlinePhase, functionBody);
  250. }
  251. return false;
  252. }
  253. bool DynamicProfileInfo::EnableImplicitCallFlags(const FunctionBody *const functionBody)
  254. {
  255. return DynamicProfileInfo::IsEnabled(functionBody);
  256. }
  257. #ifdef _M_IX86
  258. __declspec(naked)
  259. Var
  260. DynamicProfileInfo::EnsureDynamicProfileInfoThunk(RecyclableObject* function, CallInfo callInfo, ...)
  261. {
  262. __asm
  263. {
  264. push ebp
  265. mov ebp, esp
  266. push[esp + 8] // push function object
  267. call DynamicProfileInfo::EnsureDynamicProfileInfo;
  268. #ifdef _CONTROL_FLOW_GUARD
  269. // verify that the call target is valid
  270. mov ecx, eax
  271. call[__guard_check_icall_fptr]
  272. mov eax, ecx
  273. #endif
  274. pop ebp
  275. jmp eax
  276. }
  277. }
  278. #endif
  279. JavascriptMethod DynamicProfileInfo::EnsureDynamicProfileInfo(ScriptFunction * function)
  280. {
  281. // If we're creating a dynamic profile, make sure that the function
  282. // has an entry point and this entry point is the "default" entrypoint
  283. // created when a function body is created.
  284. Assert(function->GetEntryPointInfo() != nullptr);
  285. Assert(function->GetFunctionEntryPointInfo()->entryPointIndex == 0);
  286. FunctionBody * functionBody = function->GetFunctionBody();
  287. // This is used only if the first entry point codegen completes.
  288. // So there is no concurrency concern with background code gen thread modifying the entry point.
  289. EntryPointInfo * entryPoint = functionBody->GetEntryPointInfo(0);
  290. Assert(entryPoint == function->GetEntryPointInfo());
  291. Assert(entryPoint->IsCodeGenDone());
  292. JavascriptMethod directEntryPoint = entryPoint->jsMethod;
  293. // Check if it has changed already
  294. if (directEntryPoint == DynamicProfileInfo::EnsureDynamicProfileInfoThunk)
  295. {
  296. functionBody->EnsureDynamicProfileInfo();
  297. if (functionBody->GetScriptContext()->CurrentThunk == ProfileEntryThunk)
  298. {
  299. directEntryPoint = ProfileEntryThunk;
  300. }
  301. else
  302. {
  303. directEntryPoint = (JavascriptMethod)entryPoint->GetNativeAddress();
  304. }
  305. entryPoint->jsMethod = directEntryPoint;
  306. }
  307. else
  308. {
  309. Assert(directEntryPoint == ProfileEntryThunk || functionBody->GetScriptContext()->IsNativeAddress(directEntryPoint));
  310. Assert(functionBody->HasExecutionDynamicProfileInfo());
  311. }
  312. return function->UpdateThunkEntryPoint(static_cast<FunctionEntryPointInfo*>(entryPoint), directEntryPoint);
  313. }
  314. bool DynamicProfileInfo::HasLdFldCallSiteInfo() const
  315. {
  316. return bits.hasLdFldCallSite;
  317. }
  318. bool DynamicProfileInfo::RecordLdFldCallSiteInfo(FunctionBody* functionBody, RecyclableObject* callee, bool callApplyTarget)
  319. {
  320. auto SetBits = [&]() -> bool
  321. {
  322. this->bits.hasLdFldCallSite = true;
  323. this->currentInlinerVersion++; // we don't mind if this overflows
  324. return true;
  325. };
  326. FunctionInfo* calleeFunctionInfo = callee->GetTypeId() == TypeIds_Function ? JavascriptFunction::FromVar(callee)->GetFunctionInfo() : nullptr;
  327. if (calleeFunctionInfo == nullptr)
  328. {
  329. return false;
  330. }
  331. else if (!calleeFunctionInfo->HasBody())
  332. {
  333. // We can inline fastDOM getter/setter.
  334. // We can directly call Math.max/min as apply targets.
  335. if ((calleeFunctionInfo->GetAttributes() & Js::FunctionInfo::Attributes::NeedCrossSiteSecurityCheck) ||
  336. (callApplyTarget && (calleeFunctionInfo->GetAttributes() & Js::FunctionInfo::Attributes::BuiltInInlinableAsLdFldInlinee)))
  337. {
  338. if (functionBody->GetScriptContext() == callee->GetScriptContext())
  339. {
  340. return SetBits();
  341. }
  342. }
  343. return false;
  344. }
  345. else if (functionBody->CheckCalleeContextForInlining(calleeFunctionInfo->GetFunctionProxy()))
  346. {
  347. // If functionInfo !HasBody(), the previous 'else if' branch is executed; otherwise it has a body and therefore it has a proxy
  348. return SetBits();
  349. }
  350. return false;
  351. }
  352. void DynamicProfileInfo::RecordConstParameterAtCallSite(ProfileId callSiteId, int argNum)
  353. {
  354. Assert(argNum < Js::InlineeCallInfo::MaxInlineeArgoutCount);
  355. Assert(callSiteId < functionBody->GetProfiledCallSiteCount());
  356. callSiteInfo[callSiteId].isArgConstant = callSiteInfo[callSiteId].isArgConstant | (1 << argNum);
  357. }
  358. uint16 DynamicProfileInfo::GetConstantArgInfo(ProfileId callSiteId)
  359. {
  360. return callSiteInfo[callSiteId].isArgConstant;
  361. }
  362. void DynamicProfileInfo::RecordCallSiteInfo(FunctionBody* functionBody, ProfileId callSiteId, FunctionInfo* calleeFunctionInfo, JavascriptFunction* calleeFunction, ArgSlot actualArgCount, bool isConstructorCall, InlineCacheIndex ldFldInlineCacheId)
  363. {
  364. #if DBG_DUMP || defined(DYNAMIC_PROFILE_STORAGE) || defined(RUNTIME_DATA_COLLECTION)
  365. // If we persistsAcrossScriptContext, the dynamic profile info may be referred to by multiple function body from
  366. // different script context
  367. Assert(!DynamicProfileInfo::NeedProfileInfoList() || this->persistsAcrossScriptContexts || this->functionBody == functionBody);
  368. #endif
  369. bool doInline = true;
  370. // This is a hard limit as we only use 4 bits to encode the actual count in the InlineeCallInfo
  371. if (actualArgCount > Js::InlineeCallInfo::MaxInlineeArgoutCount)
  372. {
  373. doInline = false;
  374. }
  375. // Mark the callsite bit where caller and callee is same function
  376. if (functionBody == calleeFunctionInfo && callSiteId < 32)
  377. {
  378. this->m_recursiveInlineInfo = this->m_recursiveInlineInfo | (1 << callSiteId);
  379. }
  380. if (!callSiteInfo[callSiteId].isPolymorphic)
  381. {
  382. Js::SourceId oldSourceId = callSiteInfo[callSiteId].u.functionData.sourceId;
  383. if (oldSourceId == InvalidSourceId)
  384. {
  385. return;
  386. }
  387. Js::LocalFunctionId oldFunctionId = callSiteInfo[callSiteId].u.functionData.functionId;
  388. Js::SourceId sourceId = InvalidSourceId;
  389. Js::LocalFunctionId functionId;
  390. if (calleeFunctionInfo == nullptr)
  391. {
  392. functionId = CallSiteNonFunction;
  393. }
  394. else if (!calleeFunctionInfo->HasBody())
  395. {
  396. Assert(calleeFunction); // calleeFunction can only be passed as null if the calleeFunctionInfo was null (which is checked above)
  397. if (functionBody->GetScriptContext() == calleeFunction->GetScriptContext())
  398. {
  399. sourceId = BuiltInSourceId;
  400. functionId = calleeFunctionInfo->GetLocalFunctionId();
  401. }
  402. else
  403. {
  404. functionId = CallSiteCrossContext;
  405. }
  406. }
  407. else
  408. {
  409. // We can only inline function that are from the same script context. So only record that data
  410. // We're about to call this function so deserialize it right now
  411. FunctionProxy* calleeFunctionProxy = calleeFunctionInfo->GetFunctionProxy();
  412. if (functionBody->GetScriptContext() == calleeFunctionProxy->GetScriptContext())
  413. {
  414. if (functionBody->GetSecondaryHostSourceContext() == calleeFunctionProxy->GetSecondaryHostSourceContext())
  415. {
  416. if (functionBody->GetHostSourceContext() == calleeFunctionProxy->GetHostSourceContext())
  417. {
  418. sourceId = CurrentSourceId; // Caller and callee in same file
  419. }
  420. else
  421. {
  422. sourceId = (Js::SourceId)calleeFunctionProxy->GetHostSourceContext(); // Caller and callee in different files
  423. }
  424. functionId = calleeFunctionProxy->GetLocalFunctionId();
  425. }
  426. else
  427. {
  428. // Pretend that we are cross context when call is crossing script file.
  429. functionId = CallSiteCrossContext;
  430. }
  431. }
  432. else
  433. {
  434. functionId = CallSiteCrossContext;
  435. }
  436. }
  437. if (oldSourceId == NoSourceId)
  438. {
  439. callSiteInfo[callSiteId].u.functionData.sourceId = sourceId;
  440. callSiteInfo[callSiteId].u.functionData.functionId = functionId;
  441. this->currentInlinerVersion++; // we don't mind if this overflows
  442. }
  443. else if (oldSourceId != sourceId || oldFunctionId != functionId)
  444. {
  445. if (oldFunctionId != CallSiteMixed)
  446. {
  447. this->currentInlinerVersion++; // we don't mind if this overflows
  448. }
  449. if (doInline && IsPolymorphicCallSite(functionId, sourceId, oldFunctionId, oldSourceId))
  450. {
  451. CreatePolymorphicDynamicProfileCallSiteInfo(functionBody, callSiteId, functionId, oldFunctionId, sourceId, oldSourceId);
  452. }
  453. else
  454. {
  455. callSiteInfo[callSiteId].u.functionData.functionId = CallSiteMixed;
  456. }
  457. }
  458. callSiteInfo[callSiteId].isConstructorCall = isConstructorCall;
  459. callSiteInfo[callSiteId].dontInline = !doInline;
  460. callSiteInfo[callSiteId].ldFldInlineCacheId = ldFldInlineCacheId;
  461. }
  462. else
  463. {
  464. Assert(doInline);
  465. Assert(callSiteInfo[callSiteId].isConstructorCall == isConstructorCall);
  466. RecordPolymorphicCallSiteInfo(functionBody, callSiteId, calleeFunctionInfo);
  467. }
  468. return;
  469. }
  470. bool DynamicProfileInfo::IsPolymorphicCallSite(Js::LocalFunctionId curFunctionId, Js::SourceId curSourceId, Js::LocalFunctionId oldFunctionId, Js::SourceId oldSourceId)
  471. {
  472. AssertMsg(oldSourceId != NoSourceId, "There is no previous call in this callsite, we shouldn't be checking for polymorphic");
  473. if (oldSourceId == NoSourceId || oldSourceId == InvalidSourceId || oldSourceId == BuiltInSourceId)
  474. {
  475. return false;
  476. }
  477. if (curFunctionId == CallSiteCrossContext || curFunctionId == CallSiteNonFunction || oldFunctionId == CallSiteMixed || oldFunctionId == CallSiteCrossContext)
  478. {
  479. return false;
  480. }
  481. Assert(oldFunctionId != CallSiteNonFunction);
  482. Assert(curFunctionId != oldFunctionId || curSourceId != oldSourceId);
  483. return true;
  484. }
  485. void DynamicProfileInfo::CreatePolymorphicDynamicProfileCallSiteInfo(FunctionBody *funcBody, ProfileId callSiteId, Js::LocalFunctionId functionId, Js::LocalFunctionId oldFunctionId, Js::SourceId sourceId, Js::SourceId oldSourceId)
  486. {
  487. PolymorphicCallSiteInfo *localPolyCallSiteInfo = RecyclerNewStructZ(funcBody->GetScriptContext()->GetRecycler(), PolymorphicCallSiteInfo);
  488. Assert(maxPolymorphicInliningSize >= 2);
  489. localPolyCallSiteInfo->functionIds[0] = oldFunctionId;
  490. localPolyCallSiteInfo->functionIds[1] = functionId;
  491. localPolyCallSiteInfo->sourceIds[0] = oldSourceId;
  492. localPolyCallSiteInfo->sourceIds[1] = sourceId;
  493. localPolyCallSiteInfo->next = funcBody->GetPolymorphicCallSiteInfoHead();
  494. for (int i = 2; i < maxPolymorphicInliningSize; i++)
  495. {
  496. localPolyCallSiteInfo->functionIds[i] = CallSiteNoInfo;
  497. }
  498. callSiteInfo[callSiteId].isPolymorphic = true;
  499. callSiteInfo[callSiteId].u.polymorphicCallSiteInfo = localPolyCallSiteInfo;
  500. funcBody->SetPolymorphicCallSiteInfoHead(localPolyCallSiteInfo);
  501. }
  502. void DynamicProfileInfo::ResetAllPolymorphicCallSiteInfo()
  503. {
  504. if (dynamicProfileFunctionInfo)
  505. {
  506. for (ProfileId i = 0; i < dynamicProfileFunctionInfo->callSiteInfoCount; i++)
  507. {
  508. if (callSiteInfo[i].isPolymorphic)
  509. {
  510. ResetPolymorphicCallSiteInfo(i, CallSiteMixed);
  511. }
  512. }
  513. }
  514. }
  515. void DynamicProfileInfo::ResetPolymorphicCallSiteInfo(ProfileId callSiteId, Js::LocalFunctionId functionId)
  516. {
  517. callSiteInfo[callSiteId].isPolymorphic = false;
  518. callSiteInfo[callSiteId].u.functionData.sourceId = CurrentSourceId;
  519. callSiteInfo[callSiteId].u.functionData.functionId = functionId;
  520. this->currentInlinerVersion++;
  521. }
  522. void DynamicProfileInfo::SetFunctionIdSlotForNewPolymorphicCall(ProfileId callSiteId, Js::LocalFunctionId curFunctionId, Js::SourceId curSourceId, Js::FunctionBody *inliner)
  523. {
  524. for (int i = 0; i < maxPolymorphicInliningSize; i++)
  525. {
  526. if (callSiteInfo[callSiteId].u.polymorphicCallSiteInfo->functionIds[i] == curFunctionId &&
  527. callSiteInfo[callSiteId].u.polymorphicCallSiteInfo->sourceIds[i] == curSourceId)
  528. {
  529. // we have it already
  530. return;
  531. }
  532. else if (callSiteInfo[callSiteId].u.polymorphicCallSiteInfo->functionIds[i] == CallSiteNoInfo)
  533. {
  534. callSiteInfo[callSiteId].u.polymorphicCallSiteInfo->functionIds[i] = curFunctionId;
  535. callSiteInfo[callSiteId].u.polymorphicCallSiteInfo->sourceIds[i] = curSourceId;
  536. this->currentInlinerVersion++;
  537. return;
  538. }
  539. }
  540. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  541. if (Js::Configuration::Global.flags.TestTrace.IsEnabled(Js::PolymorphicInlinePhase))
  542. {
  543. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  544. Output::Print(_u("INLINING (Polymorphic): More than 4 functions at this call site \t callSiteId: %d\t calleeFunctionId: %d TopFunc %s (%s)\n"),
  545. callSiteId,
  546. curFunctionId,
  547. inliner->GetDisplayName(),
  548. inliner->GetDebugNumberSet(debugStringBuffer)
  549. );
  550. Output::Flush();
  551. }
  552. #endif
  553. #ifdef PERF_HINT
  554. if (PHASE_TRACE1(Js::PerfHintPhase))
  555. {
  556. WritePerfHint(PerfHints::PolymorphicInilineCap, inliner);
  557. }
  558. #endif
  559. // We reached the max allowed to inline, no point in continuing collecting the information. Reset and move on.
  560. ResetPolymorphicCallSiteInfo(callSiteId, CallSiteMixed);
  561. }
  562. void DynamicProfileInfo::RecordPolymorphicCallSiteInfo(FunctionBody* functionBody, ProfileId callSiteId, FunctionInfo * calleeFunctionInfo)
  563. {
  564. Js::LocalFunctionId functionId;
  565. if (calleeFunctionInfo == nullptr || !calleeFunctionInfo->HasBody())
  566. {
  567. return ResetPolymorphicCallSiteInfo(callSiteId, CallSiteMixed);
  568. }
  569. // We can only inline function that are from the same script context. So only record that data
  570. // We're about to call this function so deserialize it right now.
  571. FunctionProxy* calleeFunctionProxy = calleeFunctionInfo->GetFunctionProxy();
  572. if (functionBody->GetScriptContext() == calleeFunctionProxy->GetScriptContext())
  573. {
  574. if (functionBody->GetSecondaryHostSourceContext() == calleeFunctionProxy->GetSecondaryHostSourceContext())
  575. {
  576. Js::SourceId sourceId = (Js::SourceId)calleeFunctionProxy->GetHostSourceContext();
  577. if (functionBody->GetHostSourceContext() == sourceId) // if caller and callee in same file
  578. {
  579. sourceId = CurrentSourceId;
  580. }
  581. functionId = calleeFunctionProxy->GetLocalFunctionId();
  582. SetFunctionIdSlotForNewPolymorphicCall(callSiteId, functionId, sourceId, functionBody);
  583. return;
  584. }
  585. }
  586. // Pretend that we are cross context when call is crossing script file.
  587. ResetPolymorphicCallSiteInfo(callSiteId, CallSiteCrossContext);
  588. }
  589. /* static */
  590. bool DynamicProfileInfo::HasCallSiteInfo(FunctionBody* functionBody)
  591. {
  592. SourceContextInfo *sourceContextInfo = functionBody->GetSourceContextInfo();
  593. return !functionBody->GetScriptContext()->IsNoContextSourceContextInfo(sourceContextInfo);
  594. }
  595. bool DynamicProfileInfo::GetPolymorphicCallSiteInfo(FunctionBody* functionBody, ProfileId callSiteId, bool *isConstructorCall, __inout_ecount(functionBodyArrayLength) FunctionBody** functionBodyArray, uint functionBodyArrayLength)
  596. {
  597. Assert(functionBody);
  598. const auto callSiteCount = functionBody->GetProfiledCallSiteCount();
  599. Assert(callSiteId < callSiteCount);
  600. Assert(HasCallSiteInfo(functionBody));
  601. Assert(functionBodyArray);
  602. Assert(functionBodyArrayLength == DynamicProfileInfo::maxPolymorphicInliningSize);
  603. *isConstructorCall = callSiteInfo[callSiteId].isConstructorCall;
  604. if (callSiteInfo[callSiteId].dontInline)
  605. {
  606. return false;
  607. }
  608. if (callSiteInfo[callSiteId].isPolymorphic)
  609. {
  610. PolymorphicCallSiteInfo *polymorphicCallSiteInfo = callSiteInfo[callSiteId].u.polymorphicCallSiteInfo;
  611. for (uint i = 0; i < functionBodyArrayLength; i++)
  612. {
  613. Js::LocalFunctionId localFunctionId;
  614. Js::SourceId localSourceId;
  615. if (!polymorphicCallSiteInfo->GetFunction(i, &localFunctionId, &localSourceId))
  616. {
  617. AssertMsg(i >= 2, "We found at least two function Body");
  618. return true;
  619. }
  620. FunctionBody* matchedFunctionBody;
  621. if (localSourceId == CurrentSourceId) // caller and callee in same file
  622. {
  623. matchedFunctionBody = functionBody->GetUtf8SourceInfo()->FindFunction(localFunctionId);
  624. if (!matchedFunctionBody)
  625. {
  626. return false;
  627. }
  628. functionBodyArray[i] = matchedFunctionBody;
  629. }
  630. else if (localSourceId == NoSourceId || localSourceId == InvalidSourceId)
  631. {
  632. return false;
  633. }
  634. else
  635. {
  636. // For call across files find the function from the right source
  637. typedef JsUtil::List<RecyclerWeakReference<Utf8SourceInfo>*, Recycler, false, Js::FreeListedRemovePolicy> SourceList;
  638. SourceList * sourceList = functionBody->GetScriptContext()->GetSourceList();
  639. bool found = false;
  640. for (int j = 0; j < sourceList->Count() && !found; j++)
  641. {
  642. if (sourceList->IsItemValid(j))
  643. {
  644. Utf8SourceInfo *srcInfo = sourceList->Item(j)->Get();
  645. if (srcInfo && srcInfo->GetHostSourceContext() == localSourceId)
  646. {
  647. matchedFunctionBody = srcInfo->FindFunction(localFunctionId);
  648. if (!matchedFunctionBody)
  649. {
  650. return false;
  651. }
  652. functionBodyArray[i] = matchedFunctionBody;
  653. found = true;
  654. }
  655. }
  656. }
  657. if (!found)
  658. {
  659. return false;
  660. }
  661. }
  662. }
  663. return true;
  664. }
  665. return false;
  666. }
  667. bool DynamicProfileInfo::HasCallSiteInfo(FunctionBody* functionBody, ProfileId callSiteId)
  668. {
  669. Assert(functionBody);
  670. const auto callSiteCount = functionBody->GetProfiledCallSiteCount();
  671. Assert(callSiteId < callSiteCount);
  672. Assert(HasCallSiteInfo(functionBody));
  673. if (callSiteInfo[callSiteId].isPolymorphic)
  674. {
  675. return true;
  676. }
  677. return callSiteInfo[callSiteId].u.functionData.sourceId != NoSourceId;
  678. }
  679. FunctionInfo * DynamicProfileInfo::GetCallSiteInfo(FunctionBody* functionBody, ProfileId callSiteId, bool *isConstructorCall, bool *isPolymorphicCall)
  680. {
  681. Assert(functionBody);
  682. const auto callSiteCount = functionBody->GetProfiledCallSiteCount();
  683. Assert(callSiteId < callSiteCount);
  684. Assert(HasCallSiteInfo(functionBody));
  685. *isConstructorCall = callSiteInfo[callSiteId].isConstructorCall;
  686. if (callSiteInfo[callSiteId].dontInline)
  687. {
  688. return nullptr;
  689. }
  690. if (!callSiteInfo[callSiteId].isPolymorphic)
  691. {
  692. Js::SourceId sourceId = callSiteInfo[callSiteId].u.functionData.sourceId;
  693. Js::LocalFunctionId functionId = callSiteInfo[callSiteId].u.functionData.functionId;
  694. if (sourceId == BuiltInSourceId)
  695. {
  696. return JavascriptBuiltInFunction::GetFunctionInfo(functionId);
  697. }
  698. if (sourceId == CurrentSourceId) // caller and callee in same file
  699. {
  700. return functionBody->GetUtf8SourceInfo()->FindFunction(functionId);
  701. }
  702. if (sourceId != NoSourceId && sourceId != InvalidSourceId)
  703. {
  704. // For call across files find the function from the right source
  705. JsUtil::List<RecyclerWeakReference<Utf8SourceInfo>*, Recycler, false, Js::FreeListedRemovePolicy> * sourceList = functionBody->GetScriptContext()->GetSourceList();
  706. for (int i = 0; i < sourceList->Count(); i++)
  707. {
  708. if (sourceList->IsItemValid(i))
  709. {
  710. Utf8SourceInfo *srcInfo = sourceList->Item(i)->Get();
  711. if (srcInfo && srcInfo->GetHostSourceContext() == sourceId)
  712. {
  713. return srcInfo->FindFunction(functionId);
  714. }
  715. }
  716. }
  717. }
  718. }
  719. else
  720. {
  721. *isPolymorphicCall = true;
  722. }
  723. return nullptr;
  724. }
  725. uint DynamicProfileInfo::GetLdFldCacheIndexFromCallSiteInfo(FunctionBody* functionBody, ProfileId callSiteId)
  726. {
  727. Assert(functionBody);
  728. const auto callSiteCount = functionBody->GetProfiledCallSiteCount();
  729. Assert(callSiteId < callSiteCount);
  730. Assert(HasCallSiteInfo(functionBody));
  731. return callSiteInfo[callSiteId].ldFldInlineCacheId;
  732. }
  733. void DynamicProfileInfo::RecordElementLoad(FunctionBody* functionBody, ProfileId ldElemId, const LdElemInfo& info)
  734. {
  735. Assert(ldElemId < functionBody->GetProfiledLdElemCount());
  736. Assert(info.WasProfiled());
  737. ldElemInfo[ldElemId].Merge(info);
  738. }
  739. void DynamicProfileInfo::RecordElementLoadAsProfiled(FunctionBody *const functionBody, const ProfileId ldElemId)
  740. {
  741. Assert(ldElemId < functionBody->GetProfiledLdElemCount());
  742. ldElemInfo[ldElemId].wasProfiled = true;
  743. }
  744. void DynamicProfileInfo::RecordElementStore(FunctionBody* functionBody, ProfileId stElemId, const StElemInfo& info)
  745. {
  746. Assert(stElemId < functionBody->GetProfiledStElemCount());
  747. Assert(info.WasProfiled());
  748. stElemInfo[stElemId].Merge(info);
  749. }
  750. void DynamicProfileInfo::RecordElementStoreAsProfiled(FunctionBody *const functionBody, const ProfileId stElemId)
  751. {
  752. Assert(stElemId < functionBody->GetProfiledStElemCount());
  753. stElemInfo[stElemId].wasProfiled = true;
  754. }
  755. ArrayCallSiteInfo * DynamicProfileInfo::GetArrayCallSiteInfo(FunctionBody *functionBody, ProfileId index) const
  756. {
  757. Assert(index < functionBody->GetProfiledArrayCallSiteCount());
  758. return &arrayCallSiteInfo[index];
  759. }
  760. inline void DynamicProfileInfo::RecordFieldAccess(FunctionBody* functionBody, uint fieldAccessId, Var object, FldInfoFlags flags)
  761. {
  762. Assert(fieldAccessId < functionBody->GetProfiledFldCount());
  763. FldInfoFlags oldFlags = fldInfo[fieldAccessId].flags;
  764. if (object) // if not provided, the saved value type is not changed
  765. {
  766. fldInfo[fieldAccessId].valueType = fldInfo[fieldAccessId].valueType.Merge(object);
  767. }
  768. const auto mergedFlags = MergeFldInfoFlags(oldFlags, flags);
  769. fldInfo[fieldAccessId].flags = mergedFlags;
  770. if (flags & FldInfo_Polymorphic)
  771. {
  772. bits.hasPolymorphicFldAccess = true;
  773. if (!(oldFlags & FldInfo_Polymorphic))
  774. {
  775. this->SetHasNewPolyFieldAccess(functionBody);
  776. }
  777. if (fldInfo[fieldAccessId].polymorphicInlineCacheUtilization < (PolymorphicInlineCacheUtilizationMaxValue - PolymorphicInlineCacheUtilizationIncrement))
  778. {
  779. fldInfo[fieldAccessId].polymorphicInlineCacheUtilization += PolymorphicInlineCacheUtilizationIncrement;
  780. }
  781. else
  782. {
  783. fldInfo[fieldAccessId].polymorphicInlineCacheUtilization = PolymorphicInlineCacheUtilizationMaxValue;
  784. }
  785. }
  786. else if (flags != FldInfo_NoInfo &&
  787. fldInfo[fieldAccessId].polymorphicInlineCacheUtilization != PolymorphicInlineCacheUtilizationMaxValue)
  788. {
  789. if (fldInfo[fieldAccessId].polymorphicInlineCacheUtilization > (PolymorphicInlineCacheUtilizationMinValue + PolymorphicInlineCacheUtilizationDecrement))
  790. {
  791. fldInfo[fieldAccessId].polymorphicInlineCacheUtilization -= PolymorphicInlineCacheUtilizationDecrement;
  792. }
  793. else
  794. {
  795. fldInfo[fieldAccessId].polymorphicInlineCacheUtilization = PolymorphicInlineCacheUtilizationMinValue;
  796. }
  797. }
  798. }
  799. inline void DynamicProfileInfo::RecordDivideResultType(FunctionBody* body, ProfileId divideId, Var object)
  800. {
  801. Assert(divideId < body->GetProfiledDivOrRemCount());
  802. divideTypeInfo[divideId] = divideTypeInfo[divideId].Merge(object);
  803. }
  804. // We are overloading the value types to store whether it is a mod by power of 2.
  805. // TaggedInt:
  806. inline void DynamicProfileInfo::RecordModulusOpType(FunctionBody* body, ProfileId profileId, bool isModByPowerOf2)
  807. {
  808. Assert(profileId < body->GetProfiledDivOrRemCount());
  809. // allow one op of the modulus to be optimized - anyway
  810. if (divideTypeInfo[profileId].IsUninitialized())
  811. {
  812. divideTypeInfo[profileId] = ValueType::GetInt(true);
  813. }
  814. else
  815. {
  816. if (isModByPowerOf2)
  817. {
  818. divideTypeInfo[profileId] = divideTypeInfo[profileId].Merge(ValueType::GetInt(true));
  819. }
  820. else
  821. {
  822. divideTypeInfo[profileId] = divideTypeInfo[profileId].Merge(ValueType::Float);
  823. }
  824. }
  825. }
  826. bool DynamicProfileInfo::IsModulusOpByPowerOf2(FunctionBody* body, ProfileId profileId) const
  827. {
  828. Assert(profileId < body->GetProfiledDivOrRemCount());
  829. return divideTypeInfo[profileId].IsLikelyTaggedInt();
  830. }
  831. ValueType DynamicProfileInfo::GetDivideResultType(FunctionBody* body, ProfileId divideId) const
  832. {
  833. Assert(divideId < body->GetProfiledDivOrRemCount());
  834. return divideTypeInfo[divideId];
  835. }
  836. inline void DynamicProfileInfo::RecordSwitchType(FunctionBody* body, ProfileId switchId, Var object)
  837. {
  838. Assert(switchId < body->GetProfiledSwitchCount());
  839. switchTypeInfo[switchId] = switchTypeInfo[switchId].Merge(object);
  840. }
  841. ValueType DynamicProfileInfo::GetSwitchType(FunctionBody* body, ProfileId switchId) const
  842. {
  843. Assert(switchId < body->GetProfiledSwitchCount());
  844. return switchTypeInfo[switchId];
  845. }
  846. void DynamicProfileInfo::SetHasNewPolyFieldAccess(FunctionBody *functionBody)
  847. {
  848. this->polymorphicCacheState = functionBody->GetScriptContext()->GetThreadContext()->GetNextPolymorphicCacheState();
  849. PHASE_PRINT_TRACE(
  850. Js::ObjTypeSpecPhase, functionBody,
  851. _u("New profile cache state: %d\n"), this->polymorphicCacheState);
  852. }
  853. inline void DynamicProfileInfo::RecordPolymorphicFieldAccess(FunctionBody* functionBody, uint fieldAccessId)
  854. {
  855. this->RecordFieldAccess(functionBody, fieldAccessId, nullptr, FldInfo_Polymorphic);
  856. }
  857. inline void DynamicProfileInfo::RecordSlotLoad(FunctionBody* functionBody, ProfileId slotLoadId, Var object)
  858. {
  859. Assert(slotLoadId < functionBody->GetProfiledSlotCount());
  860. slotInfo[slotLoadId] = slotInfo[slotLoadId].Merge(object);
  861. }
  862. FldInfoFlags DynamicProfileInfo::MergeFldInfoFlags(FldInfoFlags oldFlags, FldInfoFlags newFlags)
  863. {
  864. return static_cast<FldInfoFlags>(oldFlags | newFlags);
  865. }
  866. inline void DynamicProfileInfo::RecordParameterInfo(FunctionBody *functionBody, ArgSlot index, Var object)
  867. {
  868. Assert(this->parameterInfo != nullptr);
  869. Assert(index < functionBody->GetProfiledInParamsCount());
  870. parameterInfo[index] = parameterInfo[index].Merge(object);
  871. }
  872. ValueType DynamicProfileInfo::GetParameterInfo(FunctionBody* functionBody, ArgSlot index) const
  873. {
  874. Assert(this->parameterInfo != nullptr);
  875. Assert(index < functionBody->GetProfiledInParamsCount());
  876. return parameterInfo[index];
  877. }
  878. inline void DynamicProfileInfo::RecordReturnTypeOnCallSiteInfo(FunctionBody* functionBody, ProfileId callSiteId, Var object)
  879. {
  880. Assert(callSiteId < functionBody->GetProfiledCallSiteCount());
  881. this->callSiteInfo[callSiteId].returnType = this->callSiteInfo[callSiteId].returnType.Merge(object);
  882. }
  883. inline void DynamicProfileInfo::RecordReturnType(FunctionBody* functionBody, ProfileId callSiteId, Var object)
  884. {
  885. Assert(callSiteId < functionBody->GetProfiledReturnTypeCount());
  886. this->returnTypeInfo[callSiteId] = this->returnTypeInfo[callSiteId].Merge(object);
  887. }
  888. ValueType DynamicProfileInfo::GetReturnType(FunctionBody* functionBody, Js::OpCode opcode, ProfileId callSiteId) const
  889. {
  890. if (opcode < Js::OpCode::ProfiledReturnTypeCallI)
  891. {
  892. Assert(IsProfiledCallOp(opcode));
  893. Assert(callSiteId < functionBody->GetProfiledCallSiteCount());
  894. return this->callSiteInfo[callSiteId].returnType;
  895. }
  896. Assert(IsProfiledReturnTypeOp(opcode));
  897. Assert(callSiteId < functionBody->GetProfiledReturnTypeCount());
  898. return this->returnTypeInfo[callSiteId];
  899. }
  900. inline void DynamicProfileInfo::RecordThisInfo(Var object, ThisType thisType)
  901. {
  902. this->thisInfo.valueType = this->thisInfo.valueType.Merge(object);
  903. this->thisInfo.thisType = max(this->thisInfo.thisType, thisType);
  904. }
  905. ThisInfo DynamicProfileInfo::GetThisInfo() const
  906. {
  907. return this->thisInfo;
  908. }
  909. void DynamicProfileInfo::RecordLoopImplicitCallFlags(FunctionBody* functionBody, uint loopNum, ImplicitCallFlags flags)
  910. {
  911. Assert(Js::DynamicProfileInfo::EnableImplicitCallFlags(functionBody));
  912. Assert(loopNum < functionBody->GetLoopCount());
  913. this->loopImplicitCallFlags[loopNum] = (ImplicitCallFlags)(this->loopImplicitCallFlags[loopNum] | flags);
  914. }
  915. ImplicitCallFlags DynamicProfileInfo::GetLoopImplicitCallFlags(FunctionBody* functionBody, uint loopNum) const
  916. {
  917. Assert(Js::DynamicProfileInfo::EnableImplicitCallFlags(functionBody));
  918. Assert(loopNum < functionBody->GetLoopCount());
  919. // Mask out the dispose implicit call. We would bailout on reentrant dispose,
  920. // but it shouldn't affect optimization.
  921. return (ImplicitCallFlags)(this->loopImplicitCallFlags[loopNum] & ImplicitCall_All);
  922. }
  923. void DynamicProfileInfo::RecordImplicitCallFlags(ImplicitCallFlags flags)
  924. {
  925. this->implicitCallFlags = (ImplicitCallFlags)(this->implicitCallFlags | flags);
  926. }
  927. ImplicitCallFlags DynamicProfileInfo::GetImplicitCallFlags() const
  928. {
  929. // Mask out the dispose implicit call. We would bailout on reentrant dispose,
  930. // but it shouldn't affect optimization.
  931. return (ImplicitCallFlags)(this->implicitCallFlags & ImplicitCall_All);
  932. }
  933. void DynamicProfileInfo::UpdateFunctionInfo(FunctionBody* functionBody, Recycler* recycler)
  934. {
  935. Assert(this->persistsAcrossScriptContexts);
  936. if (!this->dynamicProfileFunctionInfo)
  937. {
  938. this->dynamicProfileFunctionInfo = RecyclerNewStructLeaf(recycler, DynamicProfileFunctionInfo);
  939. }
  940. this->dynamicProfileFunctionInfo->callSiteInfoCount = functionBody->GetProfiledCallSiteCount();
  941. this->dynamicProfileFunctionInfo->paramInfoCount = functionBody->GetProfiledInParamsCount();
  942. this->dynamicProfileFunctionInfo->divCount = functionBody->GetProfiledDivOrRemCount();
  943. this->dynamicProfileFunctionInfo->switchCount = functionBody->GetProfiledSwitchCount();
  944. this->dynamicProfileFunctionInfo->returnTypeInfoCount = functionBody->GetProfiledReturnTypeCount();
  945. this->dynamicProfileFunctionInfo->loopCount = functionBody->GetLoopCount();
  946. this->dynamicProfileFunctionInfo->ldElemInfoCount = functionBody->GetProfiledLdElemCount();
  947. this->dynamicProfileFunctionInfo->stElemInfoCount = functionBody->GetProfiledStElemCount();
  948. this->dynamicProfileFunctionInfo->arrayCallSiteCount = functionBody->GetProfiledArrayCallSiteCount();
  949. this->dynamicProfileFunctionInfo->fldInfoCount = functionBody->GetProfiledFldCount();
  950. this->dynamicProfileFunctionInfo->slotInfoCount = functionBody->GetProfiledSlotCount();
  951. }
  952. void DynamicProfileInfo::Save(ScriptContext * scriptContext)
  953. {
  954. // For now, we only support our local storage
  955. #ifdef DYNAMIC_PROFILE_STORAGE
  956. if (!DynamicProfileStorage::IsEnabled())
  957. {
  958. return;
  959. }
  960. if (scriptContext->GetSourceContextInfoMap() == nullptr)
  961. {
  962. // We don't have savable code
  963. Assert(!scriptContext->GetProfileInfoList() || scriptContext->GetProfileInfoList()->Empty() || scriptContext->GetNoContextSourceContextInfo()->nextLocalFunctionId != 0);
  964. return;
  965. }
  966. DynamicProfileInfo::UpdateSourceDynamicProfileManagers(scriptContext);
  967. scriptContext->GetSourceContextInfoMap()->Map([&](DWORD_PTR dwHostSourceContext, SourceContextInfo * sourceContextInfo)
  968. {
  969. if (sourceContextInfo->sourceDynamicProfileManager != nullptr && sourceContextInfo->url != nullptr
  970. && !sourceContextInfo->IsDynamic())
  971. {
  972. sourceContextInfo->sourceDynamicProfileManager->SaveToDynamicProfileStorage(sourceContextInfo->url);
  973. }
  974. });
  975. #endif
  976. }
  977. bool DynamicProfileInfo::MatchFunctionBody(FunctionBody * functionBody)
  978. {
  979. // This function is called to set a function body to the dynamic profile loaded from cache.
  980. // Need to verify that the function body matches with the profile info
  981. Assert(this->dynamicProfileFunctionInfo);
  982. if (this->dynamicProfileFunctionInfo->paramInfoCount != functionBody->GetProfiledInParamsCount()
  983. || this->dynamicProfileFunctionInfo->ldElemInfoCount != functionBody->GetProfiledLdElemCount()
  984. || this->dynamicProfileFunctionInfo->stElemInfoCount != functionBody->GetProfiledStElemCount()
  985. || this->dynamicProfileFunctionInfo->arrayCallSiteCount != functionBody->GetProfiledArrayCallSiteCount()
  986. || this->dynamicProfileFunctionInfo->fldInfoCount != functionBody->GetProfiledFldCount()
  987. || this->dynamicProfileFunctionInfo->slotInfoCount != functionBody->GetProfiledSlotCount()
  988. || this->dynamicProfileFunctionInfo->callSiteInfoCount != functionBody->GetProfiledCallSiteCount()
  989. || this->dynamicProfileFunctionInfo->returnTypeInfoCount != functionBody->GetProfiledReturnTypeCount()
  990. || this->dynamicProfileFunctionInfo->loopCount != functionBody->GetLoopCount()
  991. || this->dynamicProfileFunctionInfo->switchCount != functionBody->GetProfiledSwitchCount()
  992. || this->dynamicProfileFunctionInfo->divCount != functionBody->GetProfiledDivOrRemCount())
  993. {
  994. // Reject, the dynamic profile information doesn't match the function body
  995. return false;
  996. }
  997. #ifdef DYNAMIC_PROFILE_STORAGE
  998. this->functionBody = functionBody;
  999. #endif
  1000. this->hasFunctionBody = true;
  1001. return true;
  1002. }
  1003. FldInfo * DynamicProfileInfo::GetFldInfo(FunctionBody* functionBody, uint fieldAccessId) const
  1004. {
  1005. Assert(fieldAccessId < functionBody->GetProfiledFldCount());
  1006. return &fldInfo[fieldAccessId];
  1007. }
  1008. ValueType DynamicProfileInfo::GetSlotLoad(FunctionBody* functionBody, ProfileId slotLoadId) const
  1009. {
  1010. Assert(slotLoadId < functionBody->GetProfiledSlotCount());
  1011. return slotInfo[slotLoadId];
  1012. }
  1013. FldInfoFlags DynamicProfileInfo::FldInfoFlagsFromCacheType(CacheType cacheType)
  1014. {
  1015. switch (cacheType)
  1016. {
  1017. case CacheType_Local:
  1018. return FldInfo_FromLocal;
  1019. case CacheType_Proto:
  1020. return FldInfo_FromProto;
  1021. case CacheType_LocalWithoutProperty:
  1022. return FldInfo_FromLocalWithoutProperty;
  1023. case CacheType_Getter:
  1024. case CacheType_Setter:
  1025. return FldInfo_FromAccessor;
  1026. default:
  1027. return FldInfo_NoInfo;
  1028. }
  1029. }
  1030. FldInfoFlags DynamicProfileInfo::FldInfoFlagsFromSlotType(SlotType slotType)
  1031. {
  1032. switch (slotType)
  1033. {
  1034. case SlotType_Inline:
  1035. return FldInfo_FromInlineSlots;
  1036. case SlotType_Aux:
  1037. return FldInfo_FromAuxSlots;
  1038. default:
  1039. return FldInfo_NoInfo;
  1040. }
  1041. }
  1042. #if DBG_DUMP
  1043. void DynamicProfileInfo::DumpProfiledValue(char16 const * name, CallSiteInfo * callSiteInfo, uint count)
  1044. {
  1045. if (count != 0)
  1046. {
  1047. Output::Print(_u(" %-16s(%2d):"), name, count);
  1048. for (uint i = 0; i < count; i++)
  1049. {
  1050. Output::Print(i != 0 && (i % 10) == 0 ? _u("\n ") : _u(" "));
  1051. Output::Print(_u("%2d:"), i);
  1052. if (!callSiteInfo[i].isPolymorphic)
  1053. {
  1054. switch (callSiteInfo[i].u.functionData.sourceId)
  1055. {
  1056. case NoSourceId:
  1057. Output::Print(_u(" ????"));
  1058. break;
  1059. case BuiltInSourceId:
  1060. Output::Print(_u(" b%03d"), callSiteInfo[i].u.functionData.functionId);
  1061. break;
  1062. case InvalidSourceId:
  1063. if (callSiteInfo[i].u.functionData.functionId == CallSiteMixed)
  1064. {
  1065. Output::Print(_u(" mix"));
  1066. }
  1067. else if (callSiteInfo[i].u.functionData.functionId == CallSiteCrossContext)
  1068. {
  1069. Output::Print(_u(" x"));
  1070. }
  1071. else if (callSiteInfo[i].u.functionData.functionId == CallSiteNonFunction)
  1072. {
  1073. Output::Print(_u(" !fn"));
  1074. }
  1075. else
  1076. {
  1077. Assert(false);
  1078. }
  1079. break;
  1080. default:
  1081. Output::Print(_u(" %4d:%4d"), callSiteInfo[i].u.functionData.sourceId, callSiteInfo[i].u.functionData.functionId);
  1082. break;
  1083. };
  1084. }
  1085. else
  1086. {
  1087. Output::Print(_u(" poly"));
  1088. for (int j = 0; j < DynamicProfileInfo::maxPolymorphicInliningSize; j++)
  1089. {
  1090. if (callSiteInfo[i].u.polymorphicCallSiteInfo->functionIds[j] != CallSiteNoInfo)
  1091. {
  1092. Output::Print(_u(" %4d:%4d"), callSiteInfo[i].u.polymorphicCallSiteInfo->sourceIds[j], callSiteInfo[i].u.polymorphicCallSiteInfo->functionIds[j]);
  1093. }
  1094. }
  1095. }
  1096. }
  1097. Output::Print(_u("\n"));
  1098. Output::Print(_u(" %-16s(%2d):"), _u("Callsite RetType"), count);
  1099. for (uint i = 0; i < count; i++)
  1100. {
  1101. Output::Print(i != 0 && (i % 10) == 0 ? _u("\n ") : _u(" "));
  1102. Output::Print(_u("%2d:"), i);
  1103. char returnTypeStr[VALUE_TYPE_MAX_STRING_SIZE];
  1104. callSiteInfo[i].returnType.ToString(returnTypeStr);
  1105. Output::Print(_u(" %S"), returnTypeStr);
  1106. }
  1107. Output::Print(_u("\n"));
  1108. }
  1109. }
  1110. void DynamicProfileInfo::DumpProfiledValue(char16 const * name, ArrayCallSiteInfo * arrayCallSiteInfo, uint count)
  1111. {
  1112. if (count != 0)
  1113. {
  1114. Output::Print(_u(" %-16s(%2d):"), name, count);
  1115. Output::Print(_u("\n"));
  1116. for (uint i = 0; i < count; i++)
  1117. {
  1118. Output::Print(i != 0 && (i % 10) == 0 ? _u("\n ") : _u(" "));
  1119. Output::Print(_u("%4d:"), i);
  1120. Output::Print(_u(" Function Number: %2d, CallSite Number: %2d, IsNativeIntArray: %2d, IsNativeFloatArray: %2d"),
  1121. arrayCallSiteInfo[i].functionNumber, arrayCallSiteInfo[i].callSiteNumber, !arrayCallSiteInfo[i].isNotNativeInt, !arrayCallSiteInfo[i].isNotNativeFloat);
  1122. Output::Print(_u("\n"));
  1123. }
  1124. Output::Print(_u("\n"));
  1125. }
  1126. }
  1127. void DynamicProfileInfo::DumpProfiledValue(char16 const * name, ValueType * value, uint count)
  1128. {
  1129. if (count != 0)
  1130. {
  1131. Output::Print(_u(" %-16s(%2d):"), name, count);
  1132. for (uint i = 0; i < count; i++)
  1133. {
  1134. Output::Print(i != 0 && (i % 10) == 0 ? _u("\n ") : _u(" "));
  1135. Output::Print(_u("%2d:"), i);
  1136. char valueStr[VALUE_TYPE_MAX_STRING_SIZE];
  1137. value[i].ToString(valueStr);
  1138. Output::Print(_u(" %S"), valueStr);
  1139. }
  1140. Output::Print(_u("\n"));
  1141. }
  1142. }
  1143. void DynamicProfileInfo::DumpProfiledValue(char16 const * name, uint * value, uint count)
  1144. {
  1145. if (count != 0)
  1146. {
  1147. Output::Print(_u(" %-16s(%2d):"), name, count);
  1148. for (uint i = 0; i < count; i++)
  1149. {
  1150. Output::Print(i != 0 && (i % 10) == 0 ? _u("\n ") : _u(" "));
  1151. Output::Print(_u("%2d:%-4d"), i, value[i]);
  1152. }
  1153. Output::Print(_u("\n"));
  1154. }
  1155. }
  1156. char16 const * DynamicProfileInfo::GetImplicitCallFlagsString(ImplicitCallFlags flags)
  1157. {
  1158. // Mask out the dispose implicit call. We would bailout on reentrant dispose,
  1159. // but it shouldn't affect optimization
  1160. flags = (ImplicitCallFlags)(flags & ImplicitCall_All);
  1161. return flags == ImplicitCall_HasNoInfo ? _u("???") : flags == ImplicitCall_None ? _u("no") : _u("yes");
  1162. }
  1163. void DynamicProfileInfo::DumpProfiledValue(char16 const * name, ImplicitCallFlags * loopImplicitCallFlags, uint count)
  1164. {
  1165. if (count != 0)
  1166. {
  1167. Output::Print(_u(" %-16s(%2d):"), name, count);
  1168. for (uint i = 0; i < count; i++)
  1169. {
  1170. Output::Print(i != 0 && (i % 10) == 0 ? _u("\n ") : _u(" "));
  1171. Output::Print(_u("%2d:%-4s"), i, GetImplicitCallFlagsString(loopImplicitCallFlags[i]));
  1172. }
  1173. Output::Print(_u("\n"));
  1174. }
  1175. }
  1176. bool DynamicProfileInfo::IsProfiledCallOp(OpCode op)
  1177. {
  1178. return Js::OpCodeUtil::IsProfiledCallOp(op) || Js::OpCodeUtil::IsProfiledCallOpWithICIndex(op);
  1179. }
  1180. bool DynamicProfileInfo::IsProfiledReturnTypeOp(OpCode op)
  1181. {
  1182. return Js::OpCodeUtil::IsProfiledReturnTypeCallOp(op);
  1183. }
  1184. template<class TData, class FGetValueType>
  1185. void DynamicProfileInfo::DumpProfiledValuesGroupedByValue(
  1186. const char16 *const name,
  1187. const TData *const data,
  1188. const uint count,
  1189. const FGetValueType GetValueType,
  1190. ArenaAllocator *const dynamicProfileInfoAllocator)
  1191. {
  1192. JsUtil::BaseDictionary<ValueType, bool, ArenaAllocator> uniqueValueTypes(dynamicProfileInfoAllocator);
  1193. for (uint i = 0; i < count; i++)
  1194. {
  1195. const ValueType valueType(GetValueType(data, i));
  1196. if (!valueType.IsUninitialized())
  1197. {
  1198. uniqueValueTypes.Item(valueType, false);
  1199. }
  1200. }
  1201. uniqueValueTypes.Map([&](const ValueType groupValueType, const bool)
  1202. {
  1203. bool header = true;
  1204. uint lastTempFld = (uint)-1;
  1205. for (uint i = 0; i < count; i++)
  1206. {
  1207. const ValueType valueType(GetValueType(data, i));
  1208. if (valueType == groupValueType)
  1209. {
  1210. if (lastTempFld == (uint)-1)
  1211. {
  1212. if (header)
  1213. {
  1214. char valueTypeStr[VALUE_TYPE_MAX_STRING_SIZE];
  1215. valueType.ToString(valueTypeStr);
  1216. Output::Print(_u(" %s %S"), name, valueTypeStr);
  1217. Output::SkipToColumn(24);
  1218. Output::Print(_u(": %d"), i);
  1219. }
  1220. else
  1221. {
  1222. Output::Print(_u(", %d"), i);
  1223. }
  1224. header = false;
  1225. lastTempFld = i;
  1226. }
  1227. }
  1228. else
  1229. {
  1230. if (lastTempFld != (uint)-1)
  1231. {
  1232. if (lastTempFld != i - 1)
  1233. {
  1234. Output::Print(_u("-%d"), i - 1);
  1235. }
  1236. lastTempFld = (uint)-1;
  1237. }
  1238. }
  1239. }
  1240. if (lastTempFld != (uint)-1 && lastTempFld != count - 1)
  1241. {
  1242. Output::Print(_u("-%d\n"), count - 1);
  1243. }
  1244. else if (!header)
  1245. {
  1246. Output::Print(_u("\n"));
  1247. }
  1248. });
  1249. }
  1250. void DynamicProfileInfo::DumpFldInfoFlags(char16 const * name, FldInfo * fldInfo, uint count, FldInfoFlags value, char16 const * valueName)
  1251. {
  1252. bool header = true;
  1253. uint lastTempFld = (uint)-1;
  1254. for (uint i = 0; i < count; i++)
  1255. {
  1256. if (fldInfo[i].flags & value)
  1257. {
  1258. if (lastTempFld == (uint)-1)
  1259. {
  1260. if (header)
  1261. {
  1262. Output::Print(_u(" %s %s"), name, valueName);
  1263. Output::SkipToColumn(24);
  1264. Output::Print(_u(": %d"), i);
  1265. }
  1266. else
  1267. {
  1268. Output::Print(_u(", %d"), i);
  1269. }
  1270. header = false;
  1271. lastTempFld = i;
  1272. }
  1273. }
  1274. else
  1275. {
  1276. if (lastTempFld != (uint)-1)
  1277. {
  1278. if (lastTempFld != i - 1)
  1279. {
  1280. Output::Print(_u("-%d"), i - 1);
  1281. }
  1282. lastTempFld = (uint)-1;
  1283. }
  1284. }
  1285. }
  1286. if (lastTempFld != (uint)-1 && lastTempFld != count - 1)
  1287. {
  1288. Output::Print(_u("-%d\n"), count - 1);
  1289. }
  1290. else if (!header)
  1291. {
  1292. Output::Print(_u("\n"));
  1293. }
  1294. }
  1295. void DynamicProfileInfo::DumpLoopInfo(FunctionBody *fbody)
  1296. {
  1297. if (fbody->DoJITLoopBody())
  1298. {
  1299. uint count = fbody->GetLoopCount();
  1300. Output::Print(_u(" %-16s(%2d):"), _u("Loops"), count);
  1301. for (uint i = 0; i < count; i++)
  1302. {
  1303. Output::Print(i != 0 && (i % 10) == 0 ? _u("\n ") : _u(" "));
  1304. Output::Print(_u("%2d:%-4d"), i, fbody->GetLoopHeader(i)->interpretCount);
  1305. }
  1306. Output::Print(_u("\n"));
  1307. Output::Print(_u(" %-16s(%2d):"), _u("Loops JIT"), count);
  1308. for (uint i = 0; i < count; i++)
  1309. {
  1310. Output::Print(i != 0 && (i % 10) == 0 ? _u("\n ") : _u(" "));
  1311. Output::Print(_u("%2d:%-4d"), i, fbody->GetLoopHeader(i)->nativeCount);
  1312. }
  1313. Output::Print(_u("\n"));
  1314. }
  1315. }
  1316. void DynamicProfileInfo::Dump(FunctionBody* functionBody, ArenaAllocator * dynamicProfileInfoAllocator)
  1317. {
  1318. functionBody->DumpFunctionId(true);
  1319. Js::ArgSlot paramcount = functionBody->GetProfiledInParamsCount();
  1320. Output::Print(_u(": %-20s Interpreted:%6d, Param:%2d, ImpCall:%s, Callsite:%3d, ReturnType:%3d, LdElem:%3d, StElem:%3d, Fld%3d\n"),
  1321. functionBody->GetDisplayName(), functionBody->GetInterpretedCount(), paramcount, DynamicProfileInfo::GetImplicitCallFlagsString(this->GetImplicitCallFlags()),
  1322. functionBody->GetProfiledCallSiteCount(),
  1323. functionBody->GetProfiledReturnTypeCount(),
  1324. functionBody->GetProfiledLdElemCount(),
  1325. functionBody->GetProfiledStElemCount(),
  1326. functionBody->GetProfiledFldCount());
  1327. if (Configuration::Global.flags.Verbose)
  1328. {
  1329. DumpProfiledValue(_u("Div result type"), this->divideTypeInfo, functionBody->GetProfiledDivOrRemCount());
  1330. DumpProfiledValue(_u("Switch opt type"), this->switchTypeInfo, functionBody->GetProfiledSwitchCount());
  1331. DumpProfiledValue(_u("Param type"), this->parameterInfo, paramcount);
  1332. DumpProfiledValue(_u("Callsite"), this->callSiteInfo, functionBody->GetProfiledCallSiteCount());
  1333. DumpProfiledValue(_u("ArrayCallSite"), this->arrayCallSiteInfo, functionBody->GetProfiledArrayCallSiteCount());
  1334. DumpProfiledValue(_u("Return type"), this->returnTypeInfo, functionBody->GetProfiledReturnTypeCount());
  1335. if (dynamicProfileInfoAllocator)
  1336. {
  1337. DumpProfiledValuesGroupedByValue(
  1338. _u("Element load"),
  1339. this->ldElemInfo,
  1340. this->functionBody->GetProfiledLdElemCount(),
  1341. [](const LdElemInfo *const ldElemInfo, const uint i) -> ValueType
  1342. {
  1343. return ldElemInfo[i].GetElementType();
  1344. },
  1345. dynamicProfileInfoAllocator);
  1346. DumpProfiledValuesGroupedByValue(
  1347. _u("Fld"),
  1348. this->fldInfo,
  1349. functionBody->GetProfiledFldCount(),
  1350. [](const FldInfo *const fldInfos, const uint i) -> ValueType
  1351. {
  1352. return fldInfos[i].valueType;
  1353. },
  1354. dynamicProfileInfoAllocator);
  1355. }
  1356. DumpFldInfoFlags(_u("Fld"), this->fldInfo, functionBody->GetProfiledFldCount(), FldInfo_FromLocal, _u("FldInfo_FromLocal"));
  1357. DumpFldInfoFlags(_u("Fld"), this->fldInfo, functionBody->GetProfiledFldCount(), FldInfo_FromProto, _u("FldInfo_FromProto"));
  1358. DumpFldInfoFlags(_u("Fld"), this->fldInfo, functionBody->GetProfiledFldCount(), FldInfo_FromLocalWithoutProperty, _u("FldInfo_FromLocalWithoutProperty"));
  1359. DumpFldInfoFlags(_u("Fld"), this->fldInfo, functionBody->GetProfiledFldCount(), FldInfo_FromAccessor, _u("FldInfo_FromAccessor"));
  1360. DumpFldInfoFlags(_u("Fld"), this->fldInfo, functionBody->GetProfiledFldCount(), FldInfo_Polymorphic, _u("FldInfo_Polymorphic"));
  1361. DumpFldInfoFlags(_u("Fld"), this->fldInfo, functionBody->GetProfiledFldCount(), FldInfo_FromInlineSlots, _u("FldInfo_FromInlineSlots"));
  1362. DumpFldInfoFlags(_u("Fld"), this->fldInfo, functionBody->GetProfiledFldCount(), FldInfo_FromAuxSlots, _u("FldInfo_FromAuxSlots"));
  1363. DumpLoopInfo(functionBody);
  1364. if (DynamicProfileInfo::EnableImplicitCallFlags(functionBody))
  1365. {
  1366. DumpProfiledValue(_u("Loop Imp Call"), this->loopImplicitCallFlags, functionBody->GetLoopCount());
  1367. }
  1368. if (functionBody->GetLoopCount())
  1369. {
  1370. Output::Print(_u(" Loop Flags:\n"));
  1371. for (uint i = 0; i < functionBody->GetLoopCount(); ++i)
  1372. {
  1373. Output::Print(_u(" Loop %d:\n"), i);
  1374. LoopFlags lf = this->GetLoopFlags(i);
  1375. Output::Print(
  1376. _u(" isInterpreted : %s\n")
  1377. _u(" memopMinCountReached : %s\n"),
  1378. IsTrueOrFalse(lf.isInterpreted),
  1379. IsTrueOrFalse(lf.memopMinCountReached)
  1380. );
  1381. }
  1382. }
  1383. Output::Print(
  1384. _u(" Settings:")
  1385. _u(" disableAggressiveIntTypeSpec : %s")
  1386. _u(" disableAggressiveIntTypeSpec_jitLoopBody : %s")
  1387. _u(" disableAggressiveMulIntTypeSpec : %s")
  1388. _u(" disableAggressiveMulIntTypeSpec_jitLoopBody : %s")
  1389. _u(" disableDivIntTypeSpec : %s")
  1390. _u(" disableDivIntTypeSpec_jitLoopBody : %s")
  1391. _u(" disableLossyIntTypeSpec : %s")
  1392. _u(" disableMemOp : %s")
  1393. _u(" disableTrackIntOverflow : %s")
  1394. _u(" disableFloatTypeSpec : %s")
  1395. _u(" disableCheckThis : %s")
  1396. _u(" disableArrayCheckHoist : %s")
  1397. _u(" disableArrayCheckHoist_jitLoopBody : %s")
  1398. _u(" disableArrayMissingValueCheckHoist : %s")
  1399. _u(" disableArrayMissingValueCheckHoist_jitLoopBody : %s")
  1400. _u(" disableJsArraySegmentHoist : %s")
  1401. _u(" disableJsArraySegmentHoist_jitLoopBody : %s")
  1402. _u(" disableArrayLengthHoist : %s")
  1403. _u(" disableArrayLengthHoist_jitLoopBody : %s")
  1404. _u(" disableTypedArrayTypeSpec: %s")
  1405. _u(" disableTypedArrayTypeSpec_jitLoopBody: %s")
  1406. _u(" disableLdLenIntSpec: %s")
  1407. _u(" disableBoundCheckHoist : %s")
  1408. _u(" disableBoundCheckHoist_jitLoopBody : %s")
  1409. _u(" disableLoopCountBasedBoundCheckHoist : %s")
  1410. _u(" disableLoopCountBasedBoundCheckHoist_jitLoopBody : %s")
  1411. _u(" hasPolymorphicFldAccess : %s")
  1412. _u(" hasLdFldCallSite: %s")
  1413. _u(" disableFloorInlining: %s")
  1414. _u(" disableNoProfileBailouts: %s")
  1415. _u(" disableSwitchOpt : %s")
  1416. _u(" disableEquivalentObjTypeSpec : %s\n")
  1417. _u(" disableObjTypeSpec_jitLoopBody : %s\n"),
  1418. _u(" disablePowIntTypeSpec : %s\n"),
  1419. _u(" disableStackArgOpt : %s\n"),
  1420. IsTrueOrFalse(this->bits.disableAggressiveIntTypeSpec),
  1421. IsTrueOrFalse(this->bits.disableAggressiveIntTypeSpec_jitLoopBody),
  1422. IsTrueOrFalse(this->bits.disableAggressiveMulIntTypeSpec),
  1423. IsTrueOrFalse(this->bits.disableAggressiveMulIntTypeSpec_jitLoopBody),
  1424. IsTrueOrFalse(this->bits.disableDivIntTypeSpec),
  1425. IsTrueOrFalse(this->bits.disableDivIntTypeSpec_jitLoopBody),
  1426. IsTrueOrFalse(this->bits.disableLossyIntTypeSpec),
  1427. IsTrueOrFalse(this->bits.disableMemOp),
  1428. IsTrueOrFalse(this->bits.disableTrackCompoundedIntOverflow),
  1429. IsTrueOrFalse(this->bits.disableFloatTypeSpec),
  1430. IsTrueOrFalse(this->bits.disableCheckThis),
  1431. IsTrueOrFalse(this->bits.disableArrayCheckHoist),
  1432. IsTrueOrFalse(this->bits.disableArrayCheckHoist_jitLoopBody),
  1433. IsTrueOrFalse(this->bits.disableArrayMissingValueCheckHoist),
  1434. IsTrueOrFalse(this->bits.disableArrayMissingValueCheckHoist_jitLoopBody),
  1435. IsTrueOrFalse(this->bits.disableJsArraySegmentHoist),
  1436. IsTrueOrFalse(this->bits.disableJsArraySegmentHoist_jitLoopBody),
  1437. IsTrueOrFalse(this->bits.disableArrayLengthHoist),
  1438. IsTrueOrFalse(this->bits.disableArrayLengthHoist_jitLoopBody),
  1439. IsTrueOrFalse(this->bits.disableTypedArrayTypeSpec),
  1440. IsTrueOrFalse(this->bits.disableTypedArrayTypeSpec_jitLoopBody),
  1441. IsTrueOrFalse(this->bits.disableLdLenIntSpec),
  1442. IsTrueOrFalse(this->bits.disableBoundCheckHoist),
  1443. IsTrueOrFalse(this->bits.disableBoundCheckHoist_jitLoopBody),
  1444. IsTrueOrFalse(this->bits.disableLoopCountBasedBoundCheckHoist),
  1445. IsTrueOrFalse(this->bits.disableLoopCountBasedBoundCheckHoist_jitLoopBody),
  1446. IsTrueOrFalse(this->bits.hasPolymorphicFldAccess),
  1447. IsTrueOrFalse(this->bits.hasLdFldCallSite),
  1448. IsTrueOrFalse(this->bits.disableFloorInlining),
  1449. IsTrueOrFalse(this->bits.disableNoProfileBailouts),
  1450. IsTrueOrFalse(this->bits.disableSwitchOpt),
  1451. IsTrueOrFalse(this->bits.disableEquivalentObjTypeSpec),
  1452. IsTrueOrFalse(this->bits.disableObjTypeSpec_jitLoopBody),
  1453. IsTrueOrFalse(this->bits.disablePowIntIntTypeSpec),
  1454. IsTrueOrFalse(this->bits.disableStackArgOpt));
  1455. }
  1456. }
  1457. void DynamicProfileInfo::DumpList(SListBase<DynamicProfileInfo *> * profileInfoList, ArenaAllocator * dynamicProfileInfoAllocator)
  1458. {
  1459. AUTO_NESTED_HANDLED_EXCEPTION_TYPE(ExceptionType_DisableCheck);
  1460. if (Configuration::Global.flags.Dump.IsEnabled(DynamicProfilePhase))
  1461. {
  1462. FOREACH_SLISTBASE_ENTRY(DynamicProfileInfo *, info, profileInfoList)
  1463. {
  1464. if (Configuration::Global.flags.Dump.IsEnabled(DynamicProfilePhase, info->GetFunctionBody()->GetSourceContextId(), info->GetFunctionBody()->GetLocalFunctionId()))
  1465. {
  1466. info->Dump(info->GetFunctionBody(), dynamicProfileInfoAllocator);
  1467. }
  1468. }
  1469. NEXT_SLISTBASE_ENTRY;
  1470. }
  1471. if (Configuration::Global.flags.Dump.IsEnabled(JITLoopBodyPhase) && !Configuration::Global.flags.Dump.IsEnabled(DynamicProfilePhase))
  1472. {
  1473. FOREACH_SLISTBASE_ENTRY(DynamicProfileInfo *, info, profileInfoList)
  1474. {
  1475. if (info->functionBody->GetLoopCount() > 0)
  1476. {
  1477. info->functionBody->DumpFunctionId(true);
  1478. Output::Print(_u(": %-20s\n"), info->functionBody->GetDisplayName());
  1479. DumpLoopInfo(info->functionBody);
  1480. }
  1481. }
  1482. NEXT_SLISTBASE_ENTRY;
  1483. }
  1484. if (PHASE_STATS1(DynamicProfilePhase))
  1485. {
  1486. uint estimatedSavedBytes = sizeof(uint); // count of functions
  1487. uint functionSaved = 0;
  1488. uint loopSaved = 0;
  1489. uint callSiteSaved = 0;
  1490. uint elementAccessSaved = 0;
  1491. uint fldAccessSaved = 0;
  1492. FOREACH_SLISTBASE_ENTRY(DynamicProfileInfo *, info, profileInfoList)
  1493. {
  1494. bool hasHotLoop = false;
  1495. if (info->functionBody->DoJITLoopBody())
  1496. {
  1497. for (uint i = 0; i < info->functionBody->GetLoopCount(); i++)
  1498. {
  1499. if (info->functionBody->GetLoopHeader(i)->interpretCount >= 10)
  1500. {
  1501. hasHotLoop = true;
  1502. break;
  1503. }
  1504. }
  1505. }
  1506. if (hasHotLoop || info->functionBody->GetInterpretedCount() >= 10)
  1507. {
  1508. functionSaved++;
  1509. loopSaved += info->functionBody->GetLoopCount();
  1510. estimatedSavedBytes += sizeof(uint) * 5; // function number, loop count, call site count, local array, temp array
  1511. estimatedSavedBytes += (info->functionBody->GetLoopCount() + 7) / 8; // hot loop bit vector
  1512. estimatedSavedBytes += (info->functionBody->GetProfiledCallSiteCount() + 7) / 8; // call site bit vector
  1513. // call site function number
  1514. for (ProfileId i = 0; i < info->functionBody->GetProfiledCallSiteCount(); i++)
  1515. {
  1516. // TODO poly
  1517. if ((info->callSiteInfo[i].u.functionData.sourceId != NoSourceId) && (info->callSiteInfo[i].u.functionData.sourceId != InvalidSourceId))
  1518. {
  1519. estimatedSavedBytes += sizeof(CallSiteInfo);
  1520. callSiteSaved++;
  1521. }
  1522. }
  1523. elementAccessSaved += info->functionBody->GetProfiledLdElemCount() + info->functionBody->GetProfiledStElemCount();
  1524. fldAccessSaved += info->functionBody->GetProfiledFldCount();
  1525. estimatedSavedBytes += (info->functionBody->GetProfiledLdElemCount() + info->functionBody->GetProfiledStElemCount() + 7) / 8; // temp array access
  1526. }
  1527. }
  1528. NEXT_SLISTBASE_ENTRY;
  1529. if (estimatedSavedBytes != sizeof(uint))
  1530. {
  1531. Output::Print(_u("Estimated save size (Memory used): %6d (%6d): %3d %3d %4d %4d %3d\n"),
  1532. estimatedSavedBytes, dynamicProfileInfoAllocator->Size(), functionSaved, loopSaved, callSiteSaved,
  1533. elementAccessSaved, fldAccessSaved);
  1534. }
  1535. }
  1536. }
  1537. void DynamicProfileInfo::DumpScriptContext(ScriptContext * scriptContext)
  1538. {
  1539. if (Configuration::Global.flags.Dump.IsEnabled(DynamicProfilePhase))
  1540. {
  1541. Output::Print(_u("Sources:\n"));
  1542. if (scriptContext->GetSourceContextInfoMap() != nullptr)
  1543. {
  1544. scriptContext->GetSourceContextInfoMap()->Map([&](DWORD_PTR dwHostSourceContext, SourceContextInfo * sourceContextInfo)
  1545. {
  1546. if (sourceContextInfo->sourceContextId != Js::Constants::NoSourceContext)
  1547. {
  1548. Output::Print(_u("%2d: %s (Function count: %d)\n"), sourceContextInfo->sourceContextId, sourceContextInfo->url, sourceContextInfo->nextLocalFunctionId);
  1549. }
  1550. });
  1551. }
  1552. if (scriptContext->GetDynamicSourceContextInfoMap() != nullptr)
  1553. {
  1554. scriptContext->GetDynamicSourceContextInfoMap()->Map([&](DWORD_PTR dwHostSourceContext, SourceContextInfo * sourceContextInfo)
  1555. {
  1556. Output::Print(_u("%2d: %d (Dynamic) (Function count: %d)\n"), sourceContextInfo->sourceContextId, sourceContextInfo->hash, sourceContextInfo->nextLocalFunctionId);
  1557. });
  1558. }
  1559. }
  1560. DynamicProfileInfo::DumpList(scriptContext->GetProfileInfoList(), scriptContext->DynamicProfileInfoAllocator());
  1561. Output::Flush();
  1562. }
  1563. #endif
  1564. #ifdef DYNAMIC_PROFILE_STORAGE
  1565. #if DBG_DUMP
  1566. void BufferWriter::Log(DynamicProfileInfo* info)
  1567. {
  1568. if (Configuration::Global.flags.Dump.IsEnabled(DynamicProfilePhase, info->GetFunctionBody()->GetSourceContextId(), info->GetFunctionBody()->GetLocalFunctionId()))
  1569. {
  1570. Output::Print(_u("Saving:"));
  1571. info->Dump(info->GetFunctionBody());
  1572. }
  1573. }
  1574. #endif
  1575. template <typename T>
  1576. bool DynamicProfileInfo::Serialize(T * writer)
  1577. {
  1578. #if DBG_DUMP
  1579. writer->Log(this);
  1580. #endif
  1581. FunctionBody * functionBody = this->GetFunctionBody();
  1582. Js::ArgSlot paramInfoCount = functionBody->GetProfiledInParamsCount();
  1583. if (!writer->Write(functionBody->GetLocalFunctionId())
  1584. || !writer->Write(paramInfoCount)
  1585. || !writer->WriteArray(this->parameterInfo, paramInfoCount)
  1586. || !writer->Write(functionBody->GetProfiledLdElemCount())
  1587. || !writer->WriteArray(this->ldElemInfo, functionBody->GetProfiledLdElemCount())
  1588. || !writer->Write(functionBody->GetProfiledStElemCount())
  1589. || !writer->WriteArray(this->stElemInfo, functionBody->GetProfiledStElemCount())
  1590. || !writer->Write(functionBody->GetProfiledArrayCallSiteCount())
  1591. || !writer->WriteArray(this->arrayCallSiteInfo, functionBody->GetProfiledArrayCallSiteCount())
  1592. || !writer->Write(functionBody->GetProfiledFldCount())
  1593. || !writer->WriteArray(this->fldInfo, functionBody->GetProfiledFldCount())
  1594. || !writer->Write(functionBody->GetProfiledSlotCount())
  1595. || !writer->WriteArray(this->slotInfo, functionBody->GetProfiledSlotCount())
  1596. || !writer->Write(functionBody->GetProfiledCallSiteCount())
  1597. || !writer->WriteArray(this->callSiteInfo, functionBody->GetProfiledCallSiteCount())
  1598. || !writer->Write(functionBody->GetProfiledDivOrRemCount())
  1599. || !writer->WriteArray(this->divideTypeInfo, functionBody->GetProfiledDivOrRemCount())
  1600. || !writer->Write(functionBody->GetProfiledSwitchCount())
  1601. || !writer->WriteArray(this->switchTypeInfo, functionBody->GetProfiledSwitchCount())
  1602. || !writer->Write(functionBody->GetProfiledReturnTypeCount())
  1603. || !writer->WriteArray(this->returnTypeInfo, functionBody->GetProfiledReturnTypeCount())
  1604. || !writer->Write(functionBody->GetLoopCount())
  1605. || !writer->WriteArray(this->loopImplicitCallFlags, functionBody->GetLoopCount())
  1606. || !writer->Write(this->implicitCallFlags)
  1607. || !writer->Write(this->thisInfo)
  1608. || !writer->Write(this->bits)
  1609. || !writer->Write(this->m_recursiveInlineInfo)
  1610. || (this->loopFlags && !writer->WriteArray(this->loopFlags->GetData(), this->loopFlags->WordCount())))
  1611. {
  1612. return false;
  1613. }
  1614. return true;
  1615. }
  1616. template <typename T>
  1617. DynamicProfileInfo * DynamicProfileInfo::Deserialize(T * reader, Recycler* recycler, Js::LocalFunctionId * functionId)
  1618. {
  1619. Js::ArgSlot paramInfoCount = 0;
  1620. ProfileId ldElemInfoCount = 0;
  1621. ProfileId stElemInfoCount = 0;
  1622. ProfileId arrayCallSiteCount = 0;
  1623. ProfileId slotInfoCount = 0;
  1624. ProfileId callSiteInfoCount = 0;
  1625. ProfileId returnTypeInfoCount = 0;
  1626. ProfileId divCount = 0;
  1627. ProfileId switchCount = 0;
  1628. uint fldInfoCount = 0;
  1629. uint loopCount = 0;
  1630. ValueType * paramInfo = nullptr;
  1631. LdElemInfo * ldElemInfo = nullptr;
  1632. StElemInfo * stElemInfo = nullptr;
  1633. ArrayCallSiteInfo * arrayCallSiteInfo = nullptr;
  1634. FldInfo * fldInfo = nullptr;
  1635. ValueType * slotInfo = nullptr;
  1636. CallSiteInfo * callSiteInfo = nullptr;
  1637. ValueType * divTypeInfo = nullptr;
  1638. ValueType * switchTypeInfo = nullptr;
  1639. ValueType * returnTypeInfo = nullptr;
  1640. ImplicitCallFlags * loopImplicitCallFlags = nullptr;
  1641. BVFixed * loopFlags = nullptr;
  1642. ImplicitCallFlags implicitCallFlags;
  1643. ThisInfo thisInfo;
  1644. Bits bits;
  1645. uint32 recursiveInlineInfo = 0;
  1646. try
  1647. {
  1648. AUTO_NESTED_HANDLED_EXCEPTION_TYPE(ExceptionType_OutOfMemory);
  1649. if (!reader->Read(functionId))
  1650. {
  1651. return nullptr;
  1652. }
  1653. if (!reader->Read(&paramInfoCount))
  1654. {
  1655. return nullptr;
  1656. }
  1657. if (paramInfoCount != 0)
  1658. {
  1659. paramInfo = RecyclerNewArrayLeaf(recycler, ValueType, paramInfoCount);
  1660. if (!reader->ReadArray(paramInfo, paramInfoCount))
  1661. {
  1662. goto Error;
  1663. }
  1664. }
  1665. if (!reader->Read(&ldElemInfoCount))
  1666. {
  1667. goto Error;
  1668. }
  1669. if (ldElemInfoCount != 0)
  1670. {
  1671. ldElemInfo = RecyclerNewArrayLeaf(recycler, LdElemInfo, ldElemInfoCount);
  1672. if (!reader->ReadArray(ldElemInfo, ldElemInfoCount))
  1673. {
  1674. goto Error;
  1675. }
  1676. }
  1677. if (!reader->Read(&stElemInfoCount))
  1678. {
  1679. goto Error;
  1680. }
  1681. if (stElemInfoCount != 0)
  1682. {
  1683. stElemInfo = RecyclerNewArrayLeaf(recycler, StElemInfo, stElemInfoCount);
  1684. if (!reader->ReadArray(stElemInfo, stElemInfoCount))
  1685. {
  1686. goto Error;
  1687. }
  1688. }
  1689. if (!reader->Read(&arrayCallSiteCount))
  1690. {
  1691. goto Error;
  1692. }
  1693. if (arrayCallSiteCount != 0)
  1694. {
  1695. arrayCallSiteInfo = RecyclerNewArrayLeaf(recycler, ArrayCallSiteInfo, arrayCallSiteCount);
  1696. if (!reader->ReadArray(arrayCallSiteInfo, arrayCallSiteCount))
  1697. {
  1698. goto Error;
  1699. }
  1700. }
  1701. if (!reader->Read(&fldInfoCount))
  1702. {
  1703. goto Error;
  1704. }
  1705. if (fldInfoCount != 0)
  1706. {
  1707. fldInfo = RecyclerNewArrayLeaf(recycler, FldInfo, fldInfoCount);
  1708. if (!reader->ReadArray(fldInfo, fldInfoCount))
  1709. {
  1710. goto Error;
  1711. }
  1712. }
  1713. if (!reader->Read(&slotInfoCount))
  1714. {
  1715. goto Error;
  1716. }
  1717. if (slotInfoCount != 0)
  1718. {
  1719. slotInfo = RecyclerNewArrayLeaf(recycler, ValueType, slotInfoCount);
  1720. if (!reader->ReadArray(slotInfo, slotInfoCount))
  1721. {
  1722. goto Error;
  1723. }
  1724. }
  1725. if (!reader->Read(&callSiteInfoCount))
  1726. {
  1727. goto Error;
  1728. }
  1729. if (callSiteInfoCount != 0)
  1730. {
  1731. callSiteInfo = RecyclerNewArrayLeaf(recycler, CallSiteInfo, callSiteInfoCount);
  1732. if (!reader->ReadArray(callSiteInfo, callSiteInfoCount))
  1733. {
  1734. goto Error;
  1735. }
  1736. }
  1737. if (!reader->Read(&divCount))
  1738. {
  1739. goto Error;
  1740. }
  1741. if (divCount != 0)
  1742. {
  1743. divTypeInfo = RecyclerNewArrayLeaf(recycler, ValueType, divCount);
  1744. if (!reader->ReadArray(divTypeInfo, divCount))
  1745. {
  1746. goto Error;
  1747. }
  1748. }
  1749. if (!reader->Read(&switchCount))
  1750. {
  1751. goto Error;
  1752. }
  1753. if (switchCount != 0)
  1754. {
  1755. switchTypeInfo = RecyclerNewArrayLeaf(recycler, ValueType, switchCount);
  1756. if (!reader->ReadArray(switchTypeInfo, switchCount))
  1757. {
  1758. goto Error;
  1759. }
  1760. }
  1761. if (!reader->Read(&returnTypeInfoCount))
  1762. {
  1763. goto Error;
  1764. }
  1765. if (returnTypeInfoCount != 0)
  1766. {
  1767. returnTypeInfo = RecyclerNewArrayLeaf(recycler, ValueType, returnTypeInfoCount);
  1768. if (!reader->ReadArray(returnTypeInfo, returnTypeInfoCount))
  1769. {
  1770. goto Error;
  1771. }
  1772. }
  1773. if (!reader->Read(&loopCount))
  1774. {
  1775. goto Error;
  1776. }
  1777. if (loopCount != 0)
  1778. {
  1779. loopImplicitCallFlags = RecyclerNewArrayLeaf(recycler, ImplicitCallFlags, loopCount);
  1780. if (!reader->ReadArray(loopImplicitCallFlags, loopCount))
  1781. {
  1782. goto Error;
  1783. }
  1784. }
  1785. if (!reader->Read(&implicitCallFlags) ||
  1786. !reader->Read(&thisInfo) ||
  1787. !reader->Read(&bits) ||
  1788. !reader->Read(&recursiveInlineInfo))
  1789. {
  1790. goto Error;
  1791. }
  1792. if (loopCount != 0)
  1793. {
  1794. loopFlags = BVFixed::New(loopCount * LoopFlags::COUNT, recycler);
  1795. if (!reader->ReadArray(loopFlags->GetData(), loopFlags->WordCount()))
  1796. {
  1797. goto Error;
  1798. }
  1799. }
  1800. DynamicProfileFunctionInfo * dynamicProfileFunctionInfo = RecyclerNewStructLeaf(recycler, DynamicProfileFunctionInfo);
  1801. dynamicProfileFunctionInfo->paramInfoCount = paramInfoCount;
  1802. dynamicProfileFunctionInfo->ldElemInfoCount = ldElemInfoCount;
  1803. dynamicProfileFunctionInfo->stElemInfoCount = stElemInfoCount;
  1804. dynamicProfileFunctionInfo->arrayCallSiteCount = arrayCallSiteCount;
  1805. dynamicProfileFunctionInfo->fldInfoCount = fldInfoCount;
  1806. dynamicProfileFunctionInfo->slotInfoCount = slotInfoCount;
  1807. dynamicProfileFunctionInfo->callSiteInfoCount = callSiteInfoCount;
  1808. dynamicProfileFunctionInfo->divCount = divCount;
  1809. dynamicProfileFunctionInfo->switchCount = switchCount;
  1810. dynamicProfileFunctionInfo->returnTypeInfoCount = returnTypeInfoCount;
  1811. dynamicProfileFunctionInfo->loopCount = loopCount;
  1812. DynamicProfileInfo * dynamicProfileInfo = RecyclerNew(recycler, DynamicProfileInfo);
  1813. dynamicProfileInfo->dynamicProfileFunctionInfo = dynamicProfileFunctionInfo;
  1814. dynamicProfileInfo->parameterInfo = paramInfo;
  1815. dynamicProfileInfo->ldElemInfo = ldElemInfo;
  1816. dynamicProfileInfo->stElemInfo = stElemInfo;
  1817. dynamicProfileInfo->arrayCallSiteInfo = arrayCallSiteInfo;
  1818. dynamicProfileInfo->fldInfo = fldInfo;
  1819. dynamicProfileInfo->slotInfo = slotInfo;
  1820. dynamicProfileInfo->callSiteInfo = callSiteInfo;
  1821. dynamicProfileInfo->divideTypeInfo = divTypeInfo;
  1822. dynamicProfileInfo->switchTypeInfo = switchTypeInfo;
  1823. dynamicProfileInfo->returnTypeInfo = returnTypeInfo;
  1824. dynamicProfileInfo->loopImplicitCallFlags = loopImplicitCallFlags;
  1825. dynamicProfileInfo->implicitCallFlags = implicitCallFlags;
  1826. dynamicProfileInfo->loopFlags = loopFlags;
  1827. dynamicProfileInfo->thisInfo = thisInfo;
  1828. dynamicProfileInfo->bits = bits;
  1829. dynamicProfileInfo->m_recursiveInlineInfo = recursiveInlineInfo;
  1830. // Fixed functions and object type data is not serialized. There is no point in trying to serialize polymorphic call site info.
  1831. dynamicProfileInfo->ResetAllPolymorphicCallSiteInfo();
  1832. return dynamicProfileInfo;
  1833. }
  1834. catch (OutOfMemoryException)
  1835. {
  1836. }
  1837. Error:
  1838. return nullptr;
  1839. }
  1840. // Explicit instantiations - to force the compiler to generate these - so they can be referenced from other compilation units.
  1841. template DynamicProfileInfo * DynamicProfileInfo::Deserialize<BufferReader>(BufferReader*, Recycler*, Js::LocalFunctionId *);
  1842. template bool DynamicProfileInfo::Serialize<BufferSizeCounter>(BufferSizeCounter*);
  1843. template bool DynamicProfileInfo::Serialize<BufferWriter>(BufferWriter*);
  1844. void DynamicProfileInfo::UpdateSourceDynamicProfileManagers(ScriptContext * scriptContext)
  1845. {
  1846. // We don't clear old dynamic data here, because if a function is inlined, it will never go through the
  1847. // EnsureDynamicProfileThunk and thus not appear in the list. We would want to keep those data as well.
  1848. // Just save/update the data from function that has execute.
  1849. // That means that the data will never go away, probably not a good policy if this is cached for web page in WININET.
  1850. SListBase<DynamicProfileInfo *> * profileInfoList = scriptContext->GetProfileInfoList();
  1851. FOREACH_SLISTBASE_ENTRY(DynamicProfileInfo *, info, profileInfoList)
  1852. {
  1853. FunctionBody * functionBody = info->GetFunctionBody();
  1854. SourceDynamicProfileManager * sourceDynamicProfileManager = functionBody->GetSourceContextInfo()->sourceDynamicProfileManager;
  1855. sourceDynamicProfileManager->SaveDynamicProfileInfo(functionBody->GetLocalFunctionId(), info);
  1856. }
  1857. NEXT_SLISTBASE_ENTRY
  1858. }
  1859. #endif
  1860. #ifdef RUNTIME_DATA_COLLECTION
  1861. CriticalSection DynamicProfileInfo::s_csOutput;
  1862. template <typename T>
  1863. void DynamicProfileInfo::WriteData(T data, FILE * file)
  1864. {
  1865. fwrite(&data, sizeof(T), 1, file);
  1866. }
  1867. template <>
  1868. void DynamicProfileInfo::WriteData<char16 const *>(char16 const * sz, FILE * file)
  1869. {
  1870. if (sz)
  1871. {
  1872. charcount_t len = static_cast<charcount_t>(wcslen(sz));
  1873. utf8char_t * tempBuffer = HeapNewArray(utf8char_t, len * 3);
  1874. size_t cbNeeded = utf8::EncodeInto(tempBuffer, sz, len);
  1875. fwrite(&cbNeeded, sizeof(cbNeeded), 1, file);
  1876. fwrite(tempBuffer, sizeof(utf8char_t), cbNeeded, file);
  1877. HeapDeleteArray(len * 3, tempBuffer);
  1878. }
  1879. else
  1880. {
  1881. charcount_t len = 0;
  1882. fwrite(&len, sizeof(len), 1, file);
  1883. }
  1884. }
  1885. template <typename T>
  1886. void DynamicProfileInfo::WriteArray(uint count, T * arr, FILE * file)
  1887. {
  1888. WriteData(count, file);
  1889. for (uint i = 0; i < count; i++)
  1890. {
  1891. WriteData(arr[i], file);
  1892. }
  1893. }
  1894. template <>
  1895. void DynamicProfileInfo::WriteData<FunctionBody *>(FunctionBody * functionBody, FILE * file)
  1896. {
  1897. WriteData(functionBody->GetSourceContextInfo()->sourceContextId, file);
  1898. WriteData(functionBody->GetLocalFunctionId(), file);
  1899. }
  1900. void DynamicProfileInfo::DumpScriptContextToFile(ScriptContext * scriptContext)
  1901. {
  1902. if (Configuration::Global.flags.RuntimeDataOutputFile == nullptr)
  1903. {
  1904. return;
  1905. }
  1906. AutoCriticalSection autocs(&s_csOutput);
  1907. FILE * file;
  1908. if (_wfopen_s(&file, Configuration::Global.flags.RuntimeDataOutputFile, _u("ab+")) != 0 || file == nullptr)
  1909. {
  1910. return;
  1911. }
  1912. WriteData(scriptContext->GetAllocId(), file);
  1913. WriteData(scriptContext->GetCreateTime(), file);
  1914. WriteData(scriptContext->GetUrl(), file);
  1915. WriteData(scriptContext->GetSourceContextInfoMap() != nullptr ? scriptContext->GetSourceContextInfoMap()->Count() : 0, file);
  1916. if (scriptContext->GetSourceContextInfoMap())
  1917. {
  1918. scriptContext->GetSourceContextInfoMap()->Map([&](DWORD_PTR dwHostSourceContext, SourceContextInfo * sourceContextInfo)
  1919. {
  1920. WriteData(sourceContextInfo->sourceContextId, file);
  1921. WriteData(sourceContextInfo->nextLocalFunctionId, file);
  1922. WriteData(sourceContextInfo->url, file);
  1923. });
  1924. }
  1925. FOREACH_SLISTBASE_ENTRY(DynamicProfileInfo *, info, scriptContext->GetProfileInfoList())
  1926. {
  1927. WriteData((byte)1, file);
  1928. WriteData(info->functionBody, file);
  1929. WriteData(info->functionBody->GetDisplayName(), file);
  1930. WriteData(info->functionBody->GetInterpretedCount(), file);
  1931. uint loopCount = info->functionBody->GetLoopCount();
  1932. WriteData(loopCount, file);
  1933. for (uint i = 0; i < loopCount; i++)
  1934. {
  1935. if (info->functionBody->DoJITLoopBody())
  1936. {
  1937. WriteData(info->functionBody->GetLoopHeader(i)->interpretCount, file);
  1938. }
  1939. else
  1940. {
  1941. WriteData(-1, file);
  1942. }
  1943. }
  1944. WriteArray(info->functionBody->GetProfiledLdElemCount(), info->ldElemInfo, file);
  1945. WriteArray(info->functionBody->GetProfiledStElemCount(), info->stElemInfo, file);
  1946. WriteArray(info->functionBody->GetProfiledArrayCallSiteCount(), info->arrayCallSiteInfo, file);
  1947. WriteArray(info->functionBody->GetProfiledCallSiteCount(), info->callSiteInfo, file);
  1948. }
  1949. NEXT_SLISTBASE_ENTRY;
  1950. WriteData((byte)0, file);
  1951. fflush(file);
  1952. fclose(file);
  1953. }
  1954. #endif
  1955. void DynamicProfileInfo::InstantiateForceInlinedMembers()
  1956. {
  1957. // Force-inlined functions defined in a translation unit need a reference from an extern non-force-inlined function in the
  1958. // same translation unit to force an instantiation of the force-inlined function. Otherwise, if the force-inlined function
  1959. // is not referenced in the same translation unit, it will not be generated and the linker is not able to find the
  1960. // definition to inline the function in other translation units.
  1961. Assert(false);
  1962. FunctionBody *const functionBody = nullptr;
  1963. const Js::Var var = nullptr;
  1964. DynamicProfileInfo *const p = nullptr;
  1965. p->RecordFieldAccess(functionBody, 0, var, FldInfo_NoInfo);
  1966. p->RecordDivideResultType(functionBody, 0, var);
  1967. p->RecordModulusOpType(functionBody, 0, false);
  1968. p->RecordSwitchType(functionBody, 0, var);
  1969. p->RecordPolymorphicFieldAccess(functionBody, 0);
  1970. p->RecordSlotLoad(functionBody, 0, var);
  1971. p->RecordParameterInfo(functionBody, 0, var);
  1972. p->RecordReturnTypeOnCallSiteInfo(functionBody, 0, var);
  1973. p->RecordReturnType(functionBody, 0, var);
  1974. p->RecordThisInfo(var, ThisType_Unknown);
  1975. }
  1976. };
  1977. bool IR::IsTypeCheckBailOutKind(IR::BailOutKind kind)
  1978. {
  1979. IR::BailOutKind kindWithoutBits = kind & ~IR::BailOutKindBits;
  1980. return
  1981. kindWithoutBits == IR::BailOutFailedTypeCheck ||
  1982. kindWithoutBits == IR::BailOutFailedFixedFieldTypeCheck ||
  1983. kindWithoutBits == IR::BailOutFailedEquivalentTypeCheck ||
  1984. kindWithoutBits == IR::BailOutFailedEquivalentFixedFieldTypeCheck;
  1985. }
  1986. bool IR::IsEquivalentTypeCheckBailOutKind(IR::BailOutKind kind)
  1987. {
  1988. IR::BailOutKind kindWithoutBits = kind & ~IR::BailOutKindBits;
  1989. return
  1990. kindWithoutBits == IR::BailOutFailedEquivalentTypeCheck ||
  1991. kindWithoutBits == IR::BailOutFailedEquivalentFixedFieldTypeCheck;
  1992. }
  1993. IR::BailOutKind IR::EquivalentToMonoTypeCheckBailOutKind(IR::BailOutKind kind)
  1994. {
  1995. switch (kind & ~IR::BailOutKindBits)
  1996. {
  1997. case IR::BailOutFailedEquivalentTypeCheck:
  1998. return IR::BailOutFailedTypeCheck | (kind & IR::BailOutKindBits);
  1999. case IR::BailOutFailedEquivalentFixedFieldTypeCheck:
  2000. return IR::BailOutFailedFixedFieldTypeCheck | (kind & IR::BailOutKindBits);
  2001. default:
  2002. Assert(0);
  2003. return IR::BailOutInvalid;
  2004. }
  2005. }
  2006. #if ENABLE_DEBUG_CONFIG_OPTIONS
  2007. const char *const BailOutKindNames[] =
  2008. {
  2009. #define BAIL_OUT_KIND_LAST(n) "" STRINGIZE(n) ""
  2010. #define BAIL_OUT_KIND(n, ...) BAIL_OUT_KIND_LAST(n),
  2011. #define BAIL_OUT_KIND_VALUE_LAST(n, v) BAIL_OUT_KIND_LAST(n)
  2012. #define BAIL_OUT_KIND_VALUE(n, v) BAIL_OUT_KIND(n)
  2013. #include "BailOutKind.h"
  2014. };
  2015. IR::BailOutKind const BailOutKindValidBits[] =
  2016. {
  2017. #define BAIL_OUT_KIND(n, bits) (IR::BailOutKind)bits,
  2018. #define BAIL_OUT_KIND_VALUE_LAST(n, v)
  2019. #define BAIL_OUT_KIND_VALUE(n, v)
  2020. #define BAIL_OUT_KIND_LAST(n)
  2021. #include "BailOutKind.h"
  2022. };
  2023. bool IsValidBailOutKindAndBits(IR::BailOutKind bailOutKind)
  2024. {
  2025. IR::BailOutKind kindNoBits = bailOutKind & ~IR::BailOutKindBits;
  2026. if (kindNoBits >= IR::BailOutKindBitsStart)
  2027. {
  2028. return false;
  2029. }
  2030. return ((bailOutKind & IR::BailOutKindBits) & ~BailOutKindValidBits[kindNoBits]) == 0;
  2031. }
  2032. // Concats into the buffer, specified by the name parameter, the name of 'bit' bailout kind, specified by the enumEntryOffsetFromBitsStart parameter.
  2033. // Returns the number of bytes printed to the buffer.
  2034. size_t ConcatBailOutKindBits(_Out_writes_bytes_(dstSizeBytes) char* dst, _In_ size_t dstSizeBytes, _In_ size_t position, _In_ uint enumEntryOffsetFromBitsStart)
  2035. {
  2036. const char* kindName = BailOutKindNames[IR::BailOutKindBitsStart + static_cast<IR::BailOutKind>(enumEntryOffsetFromBitsStart)];
  2037. int printedBytes =
  2038. sprintf_s(
  2039. &dst[position],
  2040. dstSizeBytes - position * sizeof(dst[0]),
  2041. position == 0 ? "%s" : " | %s",
  2042. kindName);
  2043. return printedBytes;
  2044. }
  2045. const char* GetBailOutKindName(IR::BailOutKind kind)
  2046. {
  2047. using namespace IR;
  2048. if (!(kind & BailOutKindBits))
  2049. {
  2050. return BailOutKindNames[kind];
  2051. }
  2052. static char name[512];
  2053. size_t position = 0;
  2054. const auto normalKind = kind & ~BailOutKindBits;
  2055. if (normalKind != 0)
  2056. {
  2057. kind -= normalKind;
  2058. position +=
  2059. sprintf_s(
  2060. &name[position],
  2061. sizeof(name) / sizeof(name[0]) - position * sizeof(name[0]),
  2062. position == 0 ? "%s" : " | %s",
  2063. BailOutKindNames[normalKind]);
  2064. }
  2065. uint offset = 1;
  2066. if (kind & BailOutOnOverflow)
  2067. {
  2068. kind ^= BailOutOnOverflow;
  2069. position += ConcatBailOutKindBits(name, sizeof(name), position, offset);
  2070. }
  2071. ++offset;
  2072. if (kind & BailOutOnMulOverflow)
  2073. {
  2074. kind ^= BailOutOnMulOverflow;
  2075. position += ConcatBailOutKindBits(name, sizeof(name), position, offset);
  2076. }
  2077. ++offset;
  2078. if (kind & BailOutOnNegativeZero)
  2079. {
  2080. kind ^= BailOutOnNegativeZero;
  2081. position += ConcatBailOutKindBits(name, sizeof(name), position, offset);
  2082. }
  2083. ++offset;
  2084. if (kind & BailOutOnPowIntIntOverflow)
  2085. {
  2086. kind ^= BailOutOnPowIntIntOverflow;
  2087. position += ConcatBailOutKindBits(name, sizeof(name), position, offset);
  2088. }
  2089. ++offset;
  2090. // BailOutOnResultConditions
  2091. ++offset;
  2092. if (kind & BailOutOnMissingValue)
  2093. {
  2094. kind ^= BailOutOnMissingValue;
  2095. position += ConcatBailOutKindBits(name, sizeof(name), position, offset);
  2096. }
  2097. ++offset;
  2098. if (kind & BailOutConventionalNativeArrayAccessOnly)
  2099. {
  2100. kind ^= BailOutConventionalNativeArrayAccessOnly;
  2101. position += ConcatBailOutKindBits(name, sizeof(name), position, offset);
  2102. }
  2103. ++offset;
  2104. if (kind & BailOutConvertedNativeArray)
  2105. {
  2106. kind ^= BailOutConvertedNativeArray;
  2107. position += ConcatBailOutKindBits(name, sizeof(name), position, offset);
  2108. }
  2109. ++offset;
  2110. if (kind & BailOutOnArrayAccessHelperCall)
  2111. {
  2112. kind ^= BailOutOnArrayAccessHelperCall;
  2113. position += ConcatBailOutKindBits(name, sizeof(name), position, offset);
  2114. }
  2115. ++offset;
  2116. if (kind & BailOutOnInvalidatedArrayHeadSegment)
  2117. {
  2118. kind ^= BailOutOnInvalidatedArrayHeadSegment;
  2119. position += ConcatBailOutKindBits(name, sizeof(name), position, offset);
  2120. }
  2121. ++offset;
  2122. if (kind & BailOutOnInvalidatedArrayLength)
  2123. {
  2124. kind ^= BailOutOnInvalidatedArrayLength;
  2125. position += ConcatBailOutKindBits(name, sizeof(name), position, offset);
  2126. }
  2127. ++offset;
  2128. if (kind & BailOnStackArgsOutOfActualsRange)
  2129. {
  2130. kind ^= BailOnStackArgsOutOfActualsRange;
  2131. position += ConcatBailOutKindBits(name, sizeof(name), position, offset);
  2132. }
  2133. ++offset;
  2134. // BailOutForArrayBits
  2135. ++offset;
  2136. if (kind & BailOutForceByFlag)
  2137. {
  2138. kind ^= BailOutForceByFlag;
  2139. position += ConcatBailOutKindBits(name, sizeof(name), position, offset);
  2140. }
  2141. ++offset;
  2142. if (kind & BailOutBreakPointInFunction)
  2143. {
  2144. kind ^= BailOutBreakPointInFunction;
  2145. position += ConcatBailOutKindBits(name, sizeof(name), position, offset);
  2146. }
  2147. ++offset;
  2148. if (kind & BailOutStackFrameBase)
  2149. {
  2150. kind ^= BailOutStackFrameBase;
  2151. position += ConcatBailOutKindBits(name, sizeof(name), position, offset);
  2152. }
  2153. ++offset;
  2154. if (kind & BailOutLocalValueChanged)
  2155. {
  2156. kind ^= BailOutLocalValueChanged;
  2157. position += ConcatBailOutKindBits(name, sizeof(name), position, offset);
  2158. }
  2159. ++offset;
  2160. if (kind & BailOutExplicit)
  2161. {
  2162. kind ^= BailOutExplicit;
  2163. position += ConcatBailOutKindBits(name, sizeof(name), position, offset);
  2164. }
  2165. ++offset;
  2166. if (kind & BailOutStep)
  2167. {
  2168. kind ^= BailOutStep;
  2169. position += ConcatBailOutKindBits(name, sizeof(name), position, offset);
  2170. }
  2171. ++offset;
  2172. if (kind & BailOutIgnoreException)
  2173. {
  2174. kind ^= BailOutIgnoreException;
  2175. position += ConcatBailOutKindBits(name, sizeof(name), position, offset);
  2176. }
  2177. ++offset;
  2178. // BailOutForDebuggerBits
  2179. ++offset;
  2180. if (kind & BailOutOnDivByZero)
  2181. {
  2182. kind ^= BailOutOnDivByZero;
  2183. position += ConcatBailOutKindBits(name, sizeof(name), position, offset);
  2184. }
  2185. ++offset;
  2186. if (kind & BailOutOnDivOfMinInt)
  2187. {
  2188. kind ^= BailOutOnDivOfMinInt;
  2189. position += ConcatBailOutKindBits(name, sizeof(name), position, offset);
  2190. }
  2191. ++offset;
  2192. // BailOutOnDivSrcConditions
  2193. ++offset;
  2194. if (kind & BailOutMarkTempObject)
  2195. {
  2196. kind ^= BailOutMarkTempObject;
  2197. position += ConcatBailOutKindBits(name, sizeof(name), position, offset);
  2198. }
  2199. ++offset;
  2200. // BailOutKindBits
  2201. Assert(position != 0);
  2202. Assert(!kind);
  2203. return name;
  2204. }
  2205. #endif
  2206. #endif