DynamicProfileInfo.cpp 114 KB

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