TempTracker.cpp 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "BackEnd.h"
  6. /* ===================================================================================
  7. * TempTracker runs the mark temp algorithm. The template parameter provides information
  8. * what are valid temp use, temp transfer, or temp producing operations and what bit to
  9. * set once a symbol def can be marked temp.
  10. *
  11. * NumberTemp mark temp JavascriptNumber creation for math operations, run during deadstore
  12. *
  13. * ObjectTemp mark temp object allocations, run during backward pass so that it can provide
  14. * information to the globopt to install pre op bailout on implicit call while during stack
  15. * allocation objects.
  16. *
  17. * ObjectTempVerify runs a similar mark temp during deadstore in debug mode to assert
  18. * that globopt have install the pre op necessary and a marked temp def is still valid
  19. * as a mark temp
  20. *
  21. * The basic of the mark temp algorithm is very simple: we keep track if we have seen
  22. * any use of a symbol that is not a valid mark temp (the nonTempSyms bitvector)
  23. * and on definition of the symbol, if the all the use allow temp object (not in nonTempSyms
  24. * bitvector) then it is mark them able.
  25. *
  26. * However, the complication comes when the stack object is transferred to another symbol
  27. * and we are in a loop. We need to make sure that the stack object isn't still referred
  28. * by another symbol when we allocate the number/object in the next iteration
  29. *
  30. * For example:
  31. * Loop top:
  32. * s1 = NewScObject
  33. * = s6
  34. * s6 = s1
  35. * Goto Loop top
  36. *
  37. * We cannot mark them this case because when s1 is created, the object might still be
  38. * referred to by s6 from previous iteration, and thus if we mark them we would have
  39. * change the content of s6 as well.
  40. *
  41. * To detect this dependency, we conservatively collect "all" transfers in the pre pass
  42. * of the loop. We have to be conservative to detect reverse dependencies without
  43. * iterating more than 2 times for the loop.
  44. * =================================================================================== */
  45. JitArenaAllocator *
  46. TempTrackerBase::GetAllocator() const
  47. {
  48. return nonTempSyms.GetAllocator();
  49. }
  50. TempTrackerBase::TempTrackerBase(JitArenaAllocator * alloc, bool inLoop)
  51. : nonTempSyms(alloc), tempTransferredSyms(alloc)
  52. {
  53. if (inLoop)
  54. {
  55. tempTransferDependencies = HashTable<BVSparse<JitArenaAllocator> *>::New(alloc, 16);
  56. }
  57. else
  58. {
  59. tempTransferDependencies = nullptr;
  60. }
  61. }
  62. TempTrackerBase::~TempTrackerBase()
  63. {
  64. if (this->tempTransferDependencies != nullptr)
  65. {
  66. JitArenaAllocator * alloc = this->GetAllocator();
  67. FOREACH_HASHTABLE_ENTRY(BVSparse<JitArenaAllocator> *, bucket, this->tempTransferDependencies)
  68. {
  69. JitAdelete(alloc, bucket.element);
  70. }
  71. NEXT_HASHTABLE_ENTRY;
  72. this->tempTransferDependencies->Delete();
  73. }
  74. }
  75. void
  76. TempTrackerBase::MergeData(TempTrackerBase * fromData, bool deleteData)
  77. {
  78. nonTempSyms.Or(&fromData->nonTempSyms);
  79. tempTransferredSyms.Or(&fromData->tempTransferredSyms);
  80. MergeDependencies(tempTransferDependencies, fromData->tempTransferDependencies, deleteData);
  81. }
  82. void
  83. TempTrackerBase::AddTransferDependencies(int sourceId, SymID dstSymID, HashTable<BVSparse<JitArenaAllocator> *> * dependencies)
  84. {
  85. // Add to the transfer dependencies set
  86. BVSparse<JitArenaAllocator> ** pBVSparse = dependencies->FindOrInsertNew(sourceId);
  87. if (*pBVSparse == nullptr)
  88. {
  89. *pBVSparse = JitAnew(this->GetAllocator(), BVSparse<JitArenaAllocator>, this->GetAllocator());
  90. }
  91. AddTransferDependencies(*pBVSparse, dstSymID);
  92. }
  93. void
  94. TempTrackerBase::AddTransferDependencies(BVSparse<JitArenaAllocator> * bv, SymID dstSymID)
  95. {
  96. bv->Set(dstSymID);
  97. // Add the indirect transfers (always from tempTransferDependencies)
  98. BVSparse<JitArenaAllocator> *dstBVSparse = this->tempTransferDependencies->GetAndClear(dstSymID);
  99. if (dstBVSparse != nullptr)
  100. {
  101. bv->Or(dstBVSparse);
  102. JitAdelete(this->GetAllocator(), dstBVSparse);
  103. }
  104. }
  105. template <typename T>
  106. TempTracker<T>::TempTracker(JitArenaAllocator * alloc, bool inLoop):
  107. T(alloc, inLoop)
  108. {
  109. }
  110. template <typename T>
  111. void
  112. TempTracker<T>::MergeData(TempTracker<T> * fromData, bool deleteData)
  113. {
  114. TempTrackerBase::MergeData(fromData, deleteData);
  115. T::MergeData(fromData, deleteData);
  116. if (deleteData)
  117. {
  118. JitAdelete(this->GetAllocator(), fromData);
  119. }
  120. }
  121. void
  122. TempTrackerBase::OrHashTableOfBitVector(HashTable<BVSparse<JitArenaAllocator> *> * toData, HashTable<BVSparse<JitArenaAllocator> *> *& fromData, bool deleteData)
  123. {
  124. Assert(toData != nullptr);
  125. Assert(fromData != nullptr);
  126. toData->Or(fromData,
  127. [=](BVSparse<JitArenaAllocator> * bv1, BVSparse<JitArenaAllocator> * bv2) -> BVSparse<JitArenaAllocator> *
  128. {
  129. if (bv1 == nullptr)
  130. {
  131. if (deleteData)
  132. {
  133. return bv2;
  134. }
  135. return bv2->CopyNew(this->GetAllocator());
  136. }
  137. bv1->Or(bv2);
  138. if (deleteData)
  139. {
  140. JitAdelete(this->GetAllocator(), bv2);
  141. }
  142. return bv1;
  143. });
  144. if (deleteData)
  145. {
  146. fromData->Delete();
  147. fromData = nullptr;
  148. }
  149. }
  150. void
  151. TempTrackerBase::MergeDependencies(HashTable<BVSparse<JitArenaAllocator> *> * toData, HashTable<BVSparse<JitArenaAllocator> *> *& fromData, bool deleteData)
  152. {
  153. if (fromData != nullptr)
  154. {
  155. if (toData != nullptr)
  156. {
  157. OrHashTableOfBitVector(toData, fromData, deleteData);
  158. }
  159. else if (deleteData)
  160. {
  161. FOREACH_HASHTABLE_ENTRY(BVSparse<JitArenaAllocator> *, bucket, fromData)
  162. {
  163. JitAdelete(this->GetAllocator(), bucket.element);
  164. }
  165. NEXT_HASHTABLE_ENTRY;
  166. fromData->Delete();
  167. fromData = nullptr;
  168. }
  169. }
  170. }
  171. #if DBG_DUMP
  172. void
  173. TempTrackerBase::Dump(wchar_t const * traceName)
  174. {
  175. Output::Print(L"%s: Non temp syms:", traceName);
  176. this->nonTempSyms.Dump();
  177. Output::Print(L"%s: Temp transferred syms:", traceName);
  178. this->tempTransferredSyms.Dump();
  179. if (this->tempTransferDependencies != nullptr)
  180. {
  181. Output::Print(L"%s: Temp transfer dependencies:\n", traceName);
  182. this->tempTransferDependencies->Dump();
  183. }
  184. }
  185. #endif
  186. template <typename T>
  187. void
  188. TempTracker<T>::ProcessUse(StackSym * sym, BackwardPass * backwardPass)
  189. {
  190. // Don't care about type specialized syms
  191. if (!sym->IsVar())
  192. {
  193. return;
  194. }
  195. IR::Instr * instr = backwardPass->currentInstr;
  196. SymID usedSymID = sym->m_id;
  197. bool isTempPropertyTransferStore = T::IsTempPropertyTransferStore(instr, backwardPass);
  198. bool isTempUse = isTempPropertyTransferStore || T::IsTempUse(instr, sym, backwardPass);
  199. if (!isTempUse)
  200. {
  201. this->nonTempSyms.Set(usedSymID);
  202. }
  203. #if DBG
  204. if (T::DoTrace(backwardPass))
  205. {
  206. Output::Print(L"%s: %8s%4sTemp Use (s%-3d): ", T::GetTraceName(),
  207. backwardPass->IsPrePass() ? L"Prepass " : L"", isTempUse ? L"" : L"Non ", usedSymID);
  208. instr->DumpSimple();
  209. Output::Flush();
  210. }
  211. #endif
  212. if (T::IsTempTransfer(instr))
  213. {
  214. this->tempTransferredSyms.Set(usedSymID);
  215. // Track dependencies if we are in loop only
  216. if (this->tempTransferDependencies != nullptr)
  217. {
  218. IR::Opnd * dstOpnd = instr->GetDst();
  219. if (dstOpnd->IsRegOpnd())
  220. {
  221. SymID dstSymID = dstOpnd->AsRegOpnd()->m_sym->m_id;
  222. if (dstSymID != usedSymID)
  223. {
  224. // Record that the usedSymID may propagate to dstSymID and all the symbols
  225. // that it may propagate to as well
  226. AddTransferDependencies(usedSymID, dstSymID, this->tempTransferDependencies);
  227. #if DBG_DUMP
  228. if (T::DoTrace(backwardPass))
  229. {
  230. Output::Print(L"%s: %8s s%d -> s%d: ", T::GetTraceName(),
  231. backwardPass->IsPrePass() ? L"Prepass " : L"", dstSymID, usedSymID);
  232. (*this->tempTransferDependencies->Get(usedSymID))->Dump();
  233. }
  234. #endif
  235. }
  236. }
  237. }
  238. }
  239. if (isTempPropertyTransferStore)
  240. {
  241. this->tempTransferredSyms.Set(usedSymID);
  242. PropertySym * propertySym = instr->GetDst()->AsSymOpnd()->m_sym->AsPropertySym();
  243. PropagateTempPropertyTransferStoreDependencies(usedSymID, propertySym, backwardPass);
  244. #if DBG_DUMP
  245. if (T::DoTrace(backwardPass) && this->tempTransferDependencies)
  246. {
  247. Output::Print(L"%s: %8s (PropId:%d %s)+[] -> s%d: ", T::GetTraceName(),
  248. backwardPass->IsPrePass() ? L"Prepass " : L"", propertySym->m_propertyId,
  249. backwardPass->func->GetScriptContext()->GetPropertyNameLocked(propertySym->m_propertyId)->GetBuffer(), usedSymID);
  250. BVSparse<JitArenaAllocator> ** transferDependencies = this->tempTransferDependencies->Get(usedSymID);
  251. if (transferDependencies)
  252. {
  253. (*transferDependencies)->Dump();
  254. }
  255. else
  256. {
  257. Output::Print(L"[]\n");
  258. }
  259. }
  260. #endif
  261. }
  262. };
  263. template <typename T>
  264. void
  265. TempTracker<T>::MarkTemp(StackSym * sym, BackwardPass * backwardPass)
  266. {
  267. // Don't care about type specialized syms
  268. Assert(sym->IsVar());
  269. IR::Instr * instr = backwardPass->currentInstr;
  270. BOOLEAN nonTemp = this->nonTempSyms.TestAndClear(sym->m_id);
  271. BOOLEAN isTempTransferred;
  272. BVSparse<JitArenaAllocator> * bvTempTransferDependencies = nullptr;
  273. bool const isTransferOperation =
  274. T::IsTempTransfer(instr)
  275. || T::IsTempPropertyTransferLoad(instr, backwardPass)
  276. || T::IsTempIndirTransferLoad(instr, backwardPass);
  277. if (this->tempTransferDependencies != nullptr)
  278. {
  279. // Since we don't iterate "while (!changed)" in loops, we don't have complete accurate dataflow
  280. // for loop carried dependencies. So don't clear the dependency transfer info. WOOB:1121525
  281. // Check if this dst is transferred (assigned) to another symbol
  282. if (isTransferOperation)
  283. {
  284. isTempTransferred = this->tempTransferredSyms.Test(sym->m_id);
  285. }
  286. else
  287. {
  288. isTempTransferred = this->tempTransferredSyms.TestAndClear(sym->m_id);
  289. }
  290. // We only need to look at the dependencies if we are in a loop because of the back edge
  291. // Also we don't need to if we are in pre pass
  292. if (isTempTransferred)
  293. {
  294. if (!backwardPass->IsPrePass())
  295. {
  296. if (isTransferOperation)
  297. {
  298. // Transfer operation, load but not clear the information
  299. BVSparse<JitArenaAllocator> **pBv = this->tempTransferDependencies->Get(sym->m_id);
  300. if (pBv)
  301. {
  302. bvTempTransferDependencies = *pBv;
  303. }
  304. }
  305. else
  306. {
  307. // Non transfer operation, load and clear the information and the dst value is replaced
  308. bvTempTransferDependencies = this->tempTransferDependencies->GetAndClear(sym->m_id);
  309. }
  310. }
  311. else if (!isTransferOperation)
  312. {
  313. // In pre pass, and not a transfer operation (just an assign). We can clear the dependency info
  314. // and not look at it.
  315. this->tempTransferDependencies->Clear(sym->m_id);
  316. }
  317. }
  318. }
  319. else
  320. {
  321. isTempTransferred = this->tempTransferredSyms.TestAndClear(sym->m_id);
  322. }
  323. // Reset the dst is temp bit (we set it optimistically on the loop pre pass)
  324. bool dstIsTemp = false;
  325. bool dstIsTempTransferred = false;
  326. if (nonTemp)
  327. {
  328. #if DBG_DUMP
  329. if (T::DoTrace(backwardPass) && !backwardPass->IsPrePass() && T::CanMarkTemp(instr, backwardPass))
  330. {
  331. Output::Print(L"%s: Not temp (s%-03d):", T::GetTraceName(), sym->m_id);
  332. instr->DumpSimple();
  333. }
  334. #endif
  335. }
  336. else if (backwardPass->IsPrePass())
  337. {
  338. // On pre pass, we don't have complete information about whether it is tempable or
  339. // not from the back edge. If we already discovered that it is not a temp (above), then
  340. // we don't mark it, other wise, assume that it is okay to be tempable and have the
  341. // second pass set the bit correctly. The only works on dependency chain that is in order
  342. // e.g.
  343. // s1 = Add
  344. // s2 = s1
  345. // s3 = s2
  346. // The dependencies tracking to catch the case whether the dependency chain is out of order
  347. // e.g
  348. // s1 = Add
  349. // s3 = s2
  350. // s2 = s3
  351. Assert(isTransferOperation == T::IsTempTransfer(instr)
  352. || T::IsTempPropertyTransferLoad(instr, backwardPass)
  353. || T::IsTempIndirTransferLoad(instr, backwardPass));
  354. if (isTransferOperation)
  355. {
  356. dstIsTemp = true;
  357. }
  358. }
  359. else if (T::CanMarkTemp(instr, backwardPass))
  360. {
  361. dstIsTemp = true;
  362. if (isTempTransferred)
  363. {
  364. // Track whether the dst is transferred or not, and allocate separate stack slot for them
  365. // so that another dst will not overrides the value
  366. dstIsTempTransferred = true;
  367. // The temp is aliased, need to trace if there is another use of the set of aliased
  368. // sym that is still live so that we won't mark them this symbol and destroy the value
  369. if (bvTempTransferDependencies != nullptr)
  370. {
  371. // Inside a loop we need to track if any of the reg that we transferred to is still live
  372. // s1 = Add
  373. // = s2
  374. // s2 = s1
  375. // Since s2 is still live on the next iteration when we reassign s1, making s1 a temp
  376. // will cause the value of s2 to change before it's use.
  377. // The upwardExposedUses are the live regs, check if it intersect with the set
  378. // of dependency or not.
  379. #if DBG_DUMP
  380. if (T::DoTrace(backwardPass) && Js::Configuration::Global.flags.Verbose)
  381. {
  382. Output::Print(L"%s: Loop mark temp check instr:\n", T::GetTraceName());
  383. instr->DumpSimple();
  384. Output::Print(L"Transfer dependencies: ");
  385. bvTempTransferDependencies->Dump();
  386. Output::Print(L"Upward exposed Uses : ");
  387. backwardPass->currentBlock->upwardExposedUses->Dump();
  388. Output::Print(L"\n");
  389. }
  390. #endif
  391. BVSparse<JitArenaAllocator> * upwardExposedUses = backwardPass->currentBlock->upwardExposedUses;
  392. bool hasExposedDependencies = bvTempTransferDependencies->Test(upwardExposedUses)
  393. || T::HasExposedFieldDependencies(bvTempTransferDependencies, backwardPass);
  394. if (hasExposedDependencies)
  395. {
  396. #if DBG_DUMP
  397. if (T::DoTrace(backwardPass))
  398. {
  399. Output::Print(L"%s: Not temp (s%-03d): ", T::GetTraceName(), sym->m_id);
  400. instr->DumpSimple();
  401. Output::Print(L" Transferred exposed uses: ");
  402. JitArenaAllocator tempAllocator(L"temp", this->GetAllocator()->GetPageAllocator(), Js::Throw::OutOfMemory);
  403. bvTempTransferDependencies->AndNew(upwardExposedUses, &tempAllocator)->Dump();
  404. }
  405. #endif
  406. dstIsTemp = false;
  407. dstIsTempTransferred = false;
  408. #if DBG
  409. if (IsObjectTempVerify<T>())
  410. {
  411. dstIsTemp = ObjectTempVerify::DependencyCheck(instr, bvTempTransferDependencies, backwardPass);
  412. }
  413. #endif
  414. // Only ObjectTmepVerify would do the do anything here. All other returns false
  415. }
  416. }
  417. }
  418. }
  419. T::SetDstIsTemp(dstIsTemp, dstIsTempTransferred, instr, backwardPass);
  420. }
  421. NumberTemp::NumberTemp(JitArenaAllocator * alloc, bool inLoop)
  422. : TempTrackerBase(alloc, inLoop), elemLoadDependencies(alloc), nonTempElemLoad(false),
  423. upwardExposedMarkTempObjectLiveFields(alloc), upwardExposedMarkTempObjectSymsProperties(nullptr)
  424. {
  425. propertyIdsTempTransferDependencies = inLoop ? HashTable<BVSparse<JitArenaAllocator> *>::New(alloc, 16) : nullptr;
  426. }
  427. void
  428. NumberTemp::MergeData(NumberTemp * fromData, bool deleteData)
  429. {
  430. nonTempElemLoad = nonTempElemLoad || fromData->nonTempElemLoad;
  431. if (!nonTempElemLoad) // Don't bother merging other data if we already have a nonTempElemLoad
  432. {
  433. if (IsInLoop())
  434. {
  435. // in loop
  436. elemLoadDependencies.Or(&fromData->elemLoadDependencies);
  437. }
  438. MergeDependencies(propertyIdsTempTransferDependencies, fromData->propertyIdsTempTransferDependencies, deleteData);
  439. if (fromData->upwardExposedMarkTempObjectSymsProperties)
  440. {
  441. if (upwardExposedMarkTempObjectSymsProperties)
  442. {
  443. OrHashTableOfBitVector(upwardExposedMarkTempObjectSymsProperties, fromData->upwardExposedMarkTempObjectSymsProperties, deleteData);
  444. }
  445. else if (deleteData)
  446. {
  447. upwardExposedMarkTempObjectSymsProperties = fromData->upwardExposedMarkTempObjectSymsProperties;
  448. fromData->upwardExposedMarkTempObjectSymsProperties = nullptr;
  449. }
  450. else
  451. {
  452. upwardExposedMarkTempObjectSymsProperties = HashTable<BVSparse<JitArenaAllocator> *>::New(this->GetAllocator(), 16);
  453. OrHashTableOfBitVector(upwardExposedMarkTempObjectSymsProperties, fromData->upwardExposedMarkTempObjectSymsProperties, deleteData);
  454. }
  455. }
  456. upwardExposedMarkTempObjectLiveFields.Or(&fromData->upwardExposedMarkTempObjectLiveFields);
  457. }
  458. }
  459. bool
  460. NumberTemp::IsTempUse(IR::Instr * instr, Sym * sym, BackwardPass * backwardPass)
  461. {
  462. Js::OpCode opcode = instr->m_opcode;
  463. if (OpCodeAttr::NonTempNumberSources(opcode)
  464. || (OpCodeAttr::TempNumberTransfer(opcode) && !instr->dstIsTempNumber))
  465. {
  466. // For TypedArray stores, we don't store the Var object, so MarkTemp is valid
  467. if (opcode != Js::OpCode::StElemI_A
  468. || !instr->GetDst()->AsIndirOpnd()->GetBaseOpnd()->GetValueType().IsLikelyOptimizedTypedArray())
  469. {
  470. // Mark the symbol as non-tempable if the instruction doesn't allow temp sources,
  471. // or it is transferred to a non-temp dst
  472. return false;
  473. }
  474. }
  475. return true;
  476. }
  477. bool
  478. NumberTemp::IsTempTransfer(IR::Instr * instr)
  479. {
  480. return OpCodeAttr::TempNumberTransfer(instr->m_opcode);
  481. }
  482. bool
  483. NumberTemp::IsTempProducing(IR::Instr * instr)
  484. {
  485. Js::OpCode opcode = instr->m_opcode;
  486. if (OpCodeAttr::TempNumberProducing(opcode))
  487. {
  488. return true;
  489. }
  490. // Loads from float typedArrays usually require a conversion to Var, which we can MarkTemp.
  491. if (opcode == Js::OpCode::LdElemI_A)
  492. {
  493. const ValueType baseValueType(instr->GetSrc1()->AsIndirOpnd()->GetBaseOpnd()->GetValueType());
  494. if (baseValueType.IsLikelyObject()
  495. && (baseValueType.GetObjectType() == ObjectType::Float32Array
  496. || baseValueType.GetObjectType() == ObjectType::Float64Array))
  497. {
  498. return true;
  499. }
  500. }
  501. return false;
  502. }
  503. bool
  504. NumberTemp::CanMarkTemp(IR::Instr * instr, BackwardPass * backwardPass)
  505. {
  506. if (IsTempTransfer(instr) || IsTempProducing(instr))
  507. {
  508. return true;
  509. }
  510. // REVIEW: this is added a long time ago, and I am not sure what this is for any more.
  511. if (OpCodeAttr::InlineCallInstr(instr->m_opcode))
  512. {
  513. return true;
  514. }
  515. if (NumberTemp::IsTempIndirTransferLoad(instr, backwardPass)
  516. || NumberTemp::IsTempPropertyTransferLoad(instr, backwardPass))
  517. {
  518. return true;
  519. }
  520. // the opcode is not temp producing or a transfer, this is not a tmp
  521. // Also mark calls which may get inlined.
  522. return false;
  523. }
  524. void
  525. NumberTemp::ProcessInstr(IR::Instr * instr, BackwardPass * backwardPass)
  526. {
  527. #if DBG
  528. if (instr->m_opcode == Js::OpCode::BailOnNoProfile)
  529. {
  530. // If we see BailOnNoProfile, we shouldn't have any successor to have any non temp syms
  531. Assert(!this->nonTempElemLoad);
  532. Assert(this->nonTempSyms.IsEmpty());
  533. Assert(this->tempTransferredSyms.IsEmpty());
  534. Assert(this->elemLoadDependencies.IsEmpty());
  535. Assert(this->upwardExposedMarkTempObjectLiveFields.IsEmpty());
  536. }
  537. #endif
  538. // We don't get to process all dst in MarkTemp. Do it here for the upwardExposedMarkTempObjectLiveFields
  539. if (!this->DoMarkTempNumbersOnTempObjects(backwardPass))
  540. {
  541. return;
  542. }
  543. IR::Opnd * dst = instr->GetDst();
  544. if (dst == nullptr || !dst->IsRegOpnd())
  545. {
  546. return;
  547. }
  548. StackSym * dstSym = dst->AsRegOpnd()->m_sym;
  549. if (!dstSym->IsVar())
  550. {
  551. dstSym = dstSym->GetVarEquivSym(nullptr);
  552. if (dstSym == nullptr)
  553. {
  554. return;
  555. }
  556. }
  557. SymID dstSymId = dstSym->m_id;
  558. if (this->upwardExposedMarkTempObjectSymsProperties)
  559. {
  560. // We are assigning to dstSym, it no longer has upward exposed use, get the information and clear it from the hash table
  561. BVSparse<JitArenaAllocator> * dstBv = this->upwardExposedMarkTempObjectSymsProperties->GetAndClear(dstSymId);
  562. if (dstBv)
  563. {
  564. // Clear the upward exposed live fields of all the property sym id associated to dstSym
  565. this->upwardExposedMarkTempObjectLiveFields.Minus(dstBv);
  566. if (ObjectTemp::IsTempTransfer(instr) && instr->GetSrc1()->IsRegOpnd())
  567. {
  568. // If it is transfer, copy the dst info to the src
  569. SymID srcStackSymId = instr->GetSrc1()->AsRegOpnd()->m_sym->AsStackSym()->m_id;
  570. SymTable * symTable = backwardPass->func->m_symTable;
  571. FOREACH_BITSET_IN_SPARSEBV(index, dstBv)
  572. {
  573. PropertySym * propertySym = symTable->FindPropertySym(srcStackSymId, index);
  574. if (propertySym)
  575. {
  576. this->upwardExposedMarkTempObjectLiveFields.Set(propertySym->m_id);
  577. }
  578. }
  579. NEXT_BITSET_IN_SPARSEBV;
  580. BVSparse<JitArenaAllocator> ** srcBv = this->upwardExposedMarkTempObjectSymsProperties->FindOrInsert(dstBv, srcStackSymId);
  581. if (srcBv)
  582. {
  583. (*srcBv)->Or(dstBv);
  584. JitAdelete(this->GetAllocator(), dstBv);
  585. }
  586. }
  587. else
  588. {
  589. JitAdelete(this->GetAllocator(), dstBv);
  590. }
  591. }
  592. }
  593. }
  594. void
  595. NumberTemp::SetDstIsTemp(bool dstIsTemp, bool dstIsTempTransferred, IR::Instr * instr, BackwardPass * backwardPass)
  596. {
  597. Assert(dstIsTemp || !dstIsTempTransferred);
  598. Assert(!instr->dstIsTempNumberTransferred);
  599. instr->dstIsTempNumber = dstIsTemp;
  600. instr->dstIsTempNumberTransferred = dstIsTempTransferred;
  601. #if DBG_DUMP
  602. if (!backwardPass->IsPrePass() && IsTempProducing(instr))
  603. {
  604. backwardPass->numMarkTempNumber += dstIsTemp;
  605. backwardPass->numMarkTempNumberTransferred += dstIsTempTransferred;
  606. }
  607. #endif
  608. }
  609. bool
  610. NumberTemp::IsTempPropertyTransferLoad(IR::Instr * instr, BackwardPass * backwardPass)
  611. {
  612. if (DoMarkTempNumbersOnTempObjects(backwardPass))
  613. {
  614. switch (instr->m_opcode)
  615. {
  616. case Js::OpCode::LdFld:
  617. case Js::OpCode::LdFldForTypeOf:
  618. case Js::OpCode::LdMethodFld:
  619. case Js::OpCode::LdFldForCallApplyTarget:
  620. case Js::OpCode::LdMethodFromFlags:
  621. {
  622. // Only care about load from possible stack allocated object.
  623. return instr->GetSrc1()->CanStoreTemp();
  624. }
  625. };
  626. // All other opcode shouldn't have sym opnd that can store temp, See ObjectTemp::IsTempUseOpCodeSym.
  627. Assert(instr->GetSrc1() == nullptr
  628. || instr->GetDst() == nullptr // this isn't a value loading instruction
  629. || instr->GetSrc1()->IsIndirOpnd() // this is detected in IsTempIndirTransferLoad
  630. || !instr->GetSrc1()->CanStoreTemp());
  631. }
  632. return false;
  633. }
  634. bool
  635. NumberTemp::IsTempPropertyTransferStore(IR::Instr * instr, BackwardPass * backwardPass)
  636. {
  637. if (DoMarkTempNumbersOnTempObjects(backwardPass))
  638. {
  639. switch (instr->m_opcode)
  640. {
  641. case Js::OpCode::InitFld:
  642. case Js::OpCode::StFld:
  643. case Js::OpCode::StFldStrict:
  644. {
  645. IR::Opnd * dst = instr->GetDst();
  646. Assert(dst->IsSymOpnd());
  647. if (!dst->CanStoreTemp())
  648. {
  649. return false;
  650. }
  651. // We don't mark temp store of numeric properties (e.g. object literal { 86: foo });
  652. // This should only happen for InitFld, as StFld should have changed to StElem
  653. PropertySym *propertySym = dst->AsSymOpnd()->m_sym->AsPropertySym();
  654. SymID propertySymId = this->GetRepresentativePropertySymId(propertySym, backwardPass);
  655. return !this->nonTempSyms.Test(propertySymId) &&
  656. !instr->m_func->GetScriptContext()->GetPropertyNameLocked(propertySym->m_propertyId)->IsNumeric();
  657. }
  658. };
  659. // All other opcode shouldn't have sym opnd that can store temp, see ObjectTemp::IsTempUseOpCodeSym.
  660. // We also never mark the dst indir as can store temp for StElemI_A because we don't know what property
  661. // it is storing in (or it could be an array index).
  662. Assert(instr->GetDst() == nullptr || !instr->GetDst()->CanStoreTemp());
  663. }
  664. return false;
  665. }
  666. bool
  667. NumberTemp::IsTempIndirTransferLoad(IR::Instr * instr, BackwardPass * backwardPass)
  668. {
  669. if (DoMarkTempNumbersOnTempObjects(backwardPass))
  670. {
  671. if (instr->m_opcode == Js::OpCode::LdElemI_A)
  672. {
  673. // If the index is an int, then we don't care about the non-temp use
  674. IR::Opnd * src1Opnd = instr->GetSrc1();
  675. IR::RegOpnd * indexOpnd = src1Opnd->AsIndirOpnd()->GetIndexOpnd();
  676. if (indexOpnd && (indexOpnd->m_sym->m_isNotInt || !indexOpnd->GetValueType().IsInt()))
  677. {
  678. return src1Opnd->CanStoreTemp();
  679. }
  680. }
  681. else
  682. {
  683. // All other opcode shouldn't have sym opnd that can store temp, See ObjectTemp::IsTempUseOpCodeSym.
  684. Assert(instr->GetSrc1() == nullptr || instr->GetSrc1()->IsSymOpnd()
  685. || !instr->GetSrc1()->CanStoreTemp());
  686. }
  687. }
  688. return false;
  689. }
  690. void
  691. NumberTemp::PropagateTempPropertyTransferStoreDependencies(SymID usedSymID, PropertySym * propertySym, BackwardPass * backwardPass)
  692. {
  693. Assert(!this->nonTempElemLoad);
  694. upwardExposedMarkTempObjectLiveFields.Clear(propertySym->m_id);
  695. if (!this->IsInLoop())
  696. {
  697. // Don't need to track dependencies outside of loop, as we already marked the
  698. // use as temp transfer already and we won't have a case where the "dst" is reused again (outside of loop)
  699. return;
  700. }
  701. Assert(this->tempTransferDependencies != nullptr);
  702. SymID dstSymID = this->GetRepresentativePropertySymId(propertySym, backwardPass);
  703. AddTransferDependencies(usedSymID, dstSymID, this->tempTransferDependencies);
  704. Js::PropertyId storedPropertyId = propertySym->m_propertyId;
  705. // The symbol this properties are transferred to
  706. BVSparse<JitArenaAllocator> ** pPropertyTransferDependencies = this->propertyIdsTempTransferDependencies->Get(storedPropertyId);
  707. BVSparse<JitArenaAllocator> * transferDependencies = nullptr;
  708. if (pPropertyTransferDependencies == nullptr)
  709. {
  710. if (elemLoadDependencies.IsEmpty())
  711. {
  712. // No dependencies to transfer
  713. return;
  714. }
  715. transferDependencies = &elemLoadDependencies;
  716. }
  717. else
  718. {
  719. transferDependencies = *pPropertyTransferDependencies;
  720. }
  721. BVSparse<JitArenaAllocator> ** pBVSparse = this->tempTransferDependencies->FindOrInsertNew(usedSymID);
  722. if (*pBVSparse == nullptr)
  723. {
  724. *pBVSparse = transferDependencies->CopyNew(this->GetAllocator());
  725. }
  726. else
  727. {
  728. (*pBVSparse)->Or(transferDependencies);
  729. }
  730. if (transferDependencies != &elemLoadDependencies)
  731. {
  732. // Always include the element load dependencies as well
  733. (*pBVSparse)->Or(&elemLoadDependencies);
  734. }
  735. // Track the propertySym as well for the case where the dependence is not carried by the use
  736. // Loop1
  737. // o.x = e
  738. // Loop2
  739. // f = o.x
  740. // = f
  741. // e = e + blah
  742. // Here, although we can detect that e and f has dependent relationship, f's life time doesn't cross with e's.
  743. // But o.x will keep the value of e alive, so e can't be mark temp because o.x is still in use (not f)
  744. // We will add the property sym int he dependency set and check with the upward exposed mark temp object live fields
  745. // that we keep track of in NumberTemp
  746. (*pBVSparse)->Set(propertySym->m_id);
  747. }
  748. SymID
  749. NumberTemp::GetRepresentativePropertySymId(PropertySym * propertySym, BackwardPass * backwardPass)
  750. {
  751. // Since we don't track alias with objects, all property accesses are all grouped together.
  752. // Use a single property sym id to represent a propertyId to track dependencies.
  753. SymID symId;
  754. Js::PropertyId propertyId = propertySym->m_propertyId;
  755. if (!backwardPass->numberTempRepresentativePropertySym->TryGetValue(propertyId, &symId))
  756. {
  757. symId = propertySym->m_id;
  758. backwardPass->numberTempRepresentativePropertySym->Add(propertyId, symId);
  759. }
  760. return symId;
  761. }
  762. void
  763. NumberTemp::ProcessIndirUse(IR::IndirOpnd * indirOpnd, IR::Instr * instr, BackwardPass * backwardPass)
  764. {
  765. Assert(backwardPass->DoMarkTempNumbersOnTempObjects());
  766. if (!NumberTemp::IsTempIndirTransferLoad(instr, backwardPass))
  767. {
  768. return;
  769. }
  770. bool isTempUse = instr->dstIsTempNumber;
  771. if (!isTempUse)
  772. {
  773. nonTempElemLoad = true;
  774. }
  775. else if (this->IsInLoop())
  776. {
  777. // We didn't already detect non temp use of this property id. so we should track the dependencies in loops
  778. IR::Opnd * dstOpnd = instr->GetDst();
  779. Assert(dstOpnd->IsRegOpnd());
  780. SymID dstSymID = dstOpnd->AsRegOpnd()->m_sym->m_id;
  781. // Use the no property id as a place holder for elem dependencies
  782. AddTransferDependencies(&elemLoadDependencies, dstSymID);
  783. #if DBG_DUMP
  784. if (NumberTemp::DoTrace(backwardPass))
  785. {
  786. Output::Print(L"%s: %8s s%d -> []: ", NumberTemp::GetTraceName(),
  787. backwardPass->IsPrePass() ? L"Prepass " : L"", dstSymID);
  788. elemLoadDependencies.Dump();
  789. }
  790. #endif
  791. }
  792. #if DBG_DUMP
  793. if (NumberTemp::DoTrace(backwardPass))
  794. {
  795. Output::Print(L"%s: %8s%4sTemp Use ([] )", NumberTemp::GetTraceName(),
  796. backwardPass->IsPrePass() ? L"Prepass " : L"", isTempUse ? L"" : L"Non ");
  797. instr->DumpSimple();
  798. }
  799. #endif
  800. }
  801. void
  802. NumberTemp::ProcessPropertySymUse(IR::SymOpnd * symOpnd, IR::Instr * instr, BackwardPass * backwardPass)
  803. {
  804. Assert(backwardPass->DoMarkTempNumbersOnTempObjects());
  805. // We only care about instruction that may transfer the property value
  806. if (!NumberTemp::IsTempPropertyTransferLoad(instr, backwardPass))
  807. {
  808. return;
  809. }
  810. PropertySym * propertySym = symOpnd->m_sym->AsPropertySym();
  811. upwardExposedMarkTempObjectLiveFields.Set(propertySym->m_id);
  812. if (upwardExposedMarkTempObjectSymsProperties == nullptr)
  813. {
  814. upwardExposedMarkTempObjectSymsProperties = HashTable<BVSparse<JitArenaAllocator> *>::New(this->GetAllocator(), 16);
  815. }
  816. BVSparse<JitArenaAllocator> ** bv = upwardExposedMarkTempObjectSymsProperties->FindOrInsertNew(propertySym->m_stackSym->m_id);
  817. if (*bv == nullptr)
  818. {
  819. *bv = JitAnew(this->GetAllocator(), BVSparse<JitArenaAllocator>, this->GetAllocator());
  820. }
  821. (*bv)->Set(propertySym->m_propertyId);
  822. SymID propertySymId = this->GetRepresentativePropertySymId(propertySym, backwardPass);
  823. bool isTempUse = instr->dstIsTempNumber;
  824. if (!isTempUse)
  825. {
  826. // Use of the value is non temp, track the property ID's property representative sym so we don't mark temp
  827. // assignment to this property on stack objects.
  828. this->nonTempSyms.Set(propertySymId);
  829. }
  830. else if (this->IsInLoop() && !this->nonTempSyms.Test(propertySymId))
  831. {
  832. // We didn't already detect non temp use of this property id. so we should track the dependencies in loops
  833. IR::Opnd * dstOpnd = instr->GetDst();
  834. Assert(dstOpnd->IsRegOpnd());
  835. SymID dstSymID = dstOpnd->AsRegOpnd()->m_sym->m_id;
  836. AddTransferDependencies(propertySym->m_propertyId, dstSymID, this->propertyIdsTempTransferDependencies);
  837. #if DBG_DUMP
  838. if (NumberTemp::DoTrace(backwardPass))
  839. {
  840. Output::Print(L"%s: %8s s%d -> PropId:%d %s: ", NumberTemp::GetTraceName(),
  841. backwardPass->IsPrePass() ? L"Prepass " : L"", dstSymID, propertySym->m_propertyId,
  842. backwardPass->func->GetScriptContext()->GetPropertyNameLocked(propertySym->m_propertyId)->GetBuffer());
  843. (*this->propertyIdsTempTransferDependencies->Get(propertySym->m_propertyId))->Dump();
  844. }
  845. #endif
  846. }
  847. #if DBG_DUMP
  848. if (NumberTemp::DoTrace(backwardPass))
  849. {
  850. Output::Print(L"%s: %8s%4sTemp Use (PropId:%d %s)", NumberTemp::GetTraceName(),
  851. backwardPass->IsPrePass() ? L"Prepass " : L"", isTempUse ? L"" : L"Non ", propertySym->m_propertyId,
  852. backwardPass->func->GetScriptContext()->GetPropertyNameLocked(propertySym->m_propertyId)->GetBuffer());
  853. instr->DumpSimple();
  854. }
  855. #endif
  856. }
  857. bool
  858. NumberTemp::HasExposedFieldDependencies(BVSparse<JitArenaAllocator> * bvTempTransferDependencies, BackwardPass * backwardPass)
  859. {
  860. if (!DoMarkTempNumbersOnTempObjects(backwardPass))
  861. {
  862. return false;
  863. }
  864. return bvTempTransferDependencies->Test(&upwardExposedMarkTempObjectLiveFields);
  865. }
  866. bool
  867. NumberTemp::DoMarkTempNumbersOnTempObjects(BackwardPass * backwardPass) const
  868. {
  869. return backwardPass->DoMarkTempNumbersOnTempObjects() && !this->nonTempElemLoad;
  870. }
  871. #if DBG
  872. void
  873. NumberTemp::Dump(wchar_t const * traceName)
  874. {
  875. if (nonTempElemLoad)
  876. {
  877. Output::Print(L"%s: Has Non Temp Elem Load\n", traceName);
  878. }
  879. else
  880. {
  881. Output::Print(L"%s: Non Temp Syms", traceName);
  882. this->nonTempSyms.Dump();
  883. if (this->propertyIdsTempTransferDependencies != nullptr)
  884. {
  885. Output::Print(L"%s: Temp transfer propertyId dependencies:\n", traceName);
  886. this->propertyIdsTempTransferDependencies->Dump();
  887. }
  888. }
  889. }
  890. #endif
  891. //=================================================================================================
  892. // ObjectTemp
  893. //=================================================================================================
  894. bool
  895. ObjectTemp::IsTempUse(IR::Instr * instr, Sym * sym, BackwardPass * backwardPass)
  896. {
  897. Js::OpCode opcode = instr->m_opcode;
  898. // If the opcode has implicit call and the profile say we have implicit call, then it is not a temp use
  899. // TODO: More precise implicit call tracking
  900. if (instr->HasAnyImplicitCalls()
  901. &&
  902. ((backwardPass->currentBlock->loop != nullptr ?
  903. !GlobOpt::ImplicitCallFlagsAllowOpts(backwardPass->currentBlock->loop) :
  904. !GlobOpt::ImplicitCallFlagsAllowOpts(backwardPass->func))
  905. || instr->CallsAccessor())
  906. )
  907. {
  908. return false;
  909. }
  910. return IsTempUseOpCodeSym(instr, opcode, sym);
  911. }
  912. bool
  913. ObjectTemp::IsTempUseOpCodeSym(IR::Instr * instr, Js::OpCode opcode, Sym * sym)
  914. {
  915. // Special case ArgOut_A which communicate information about CallDirect
  916. switch (opcode)
  917. {
  918. case Js::OpCode::ArgOut_A:
  919. return instr->dstIsTempObject;
  920. case Js::OpCode::LdFld:
  921. case Js::OpCode::LdFldForTypeOf:
  922. case Js::OpCode::LdMethodFld:
  923. case Js::OpCode::LdFldForCallApplyTarget:
  924. case Js::OpCode::LdMethodFromFlags:
  925. return instr->GetSrc1()->AsPropertySymOpnd()->GetObjectSym() == sym;
  926. case Js::OpCode::InitFld:
  927. if (Js::PropertyRecord::DefaultAttributesForPropertyId(
  928. instr->GetDst()->AsPropertySymOpnd()->GetPropertySym()->m_propertyId, true) & PropertyDeleted)
  929. {
  930. // If the property record is marked PropertyDeleted, the InitFld will cause a type handler conversion,
  931. // which may result in creation of a weak reference to the object itself.
  932. return false;
  933. }
  934. // Fall through
  935. case Js::OpCode::StFld:
  936. case Js::OpCode::StFldStrict:
  937. return
  938. !(instr->GetSrc1() && instr->GetSrc1()->GetStackSym() == sym) &&
  939. !(instr->GetSrc2() && instr->GetSrc2()->GetStackSym() == sym) &&
  940. instr->GetDst()->AsPropertySymOpnd()->GetObjectSym() == sym;
  941. case Js::OpCode::LdElemI_A:
  942. return instr->GetSrc1()->AsIndirOpnd()->GetBaseOpnd()->m_sym == sym;
  943. case Js::OpCode::StElemI_A:
  944. case Js::OpCode::StElemI_A_Strict:
  945. return instr->GetDst()->AsIndirOpnd()->GetBaseOpnd()->m_sym == sym;
  946. case Js::OpCode::Memset:
  947. return instr->GetDst()->AsIndirOpnd()->GetBaseOpnd()->m_sym == sym || instr->GetSrc1()->IsRegOpnd() && instr->GetSrc1()->AsRegOpnd()->m_sym == sym;
  948. case Js::OpCode::Memcopy:
  949. return instr->GetDst()->AsIndirOpnd()->GetBaseOpnd()->m_sym == sym || instr->GetSrc1()->AsIndirOpnd()->GetBaseOpnd()->m_sym == sym;
  950. // Special case FromVar for now until we can allow CallsValueOf opcode to be accept temp use
  951. case Js::OpCode::FromVar:
  952. return true;
  953. }
  954. // TODO: Currently, when we disable implicit call, we still don't allow valueOf/toString that has no side effects
  955. // So we shouldn't mark them if we have use of the sym on opcode that does CallsValueOf yet.
  956. if (OpCodeAttr::CallsValueOf(opcode))
  957. {
  958. return false;
  959. }
  960. // Mark the symbol as non-tempable if the instruction doesn't allow temp sources,
  961. // or it is transferred to a non-temp dst
  962. return (OpCodeAttr::TempObjectSources(opcode)
  963. && (!OpCodeAttr::TempObjectTransfer(opcode) || instr->dstIsTempObject));
  964. }
  965. bool
  966. ObjectTemp::IsTempTransfer(IR::Instr * instr)
  967. {
  968. return OpCodeAttr::TempObjectTransfer(instr->m_opcode);
  969. }
  970. bool
  971. ObjectTemp::IsTempProducing(IR::Instr * instr)
  972. {
  973. Js::OpCode opcode = instr->m_opcode;
  974. if (OpCodeAttr::TempObjectProducing(opcode))
  975. {
  976. return true;
  977. }
  978. // TODO: Process NewScObject and CallI with isCtorCall when the ctor is fixed
  979. return false;
  980. }
  981. bool
  982. ObjectTemp::CanStoreTemp(IR::Instr * instr)
  983. {
  984. // In order to allow storing temp number on temp objects,
  985. // We have to make sure that if the instr is marked as dstIsTempObject
  986. // we will always generate the code to allocate the object on the stack (so no helper call).
  987. // Currently, we only do this for NewRegEx, NewScObjectSimple, NewScObjectLiteral and
  988. // NewScObjectNoCtor (where the ctor is inlined).
  989. // CONSIDER: review lowering of other TempObjectProducing opcode and see if we can always allocate on the stack
  990. // (for example, NewScArray should be able to, but plain NewScObject can't because the size depends on the
  991. // number inline slots)
  992. Js::OpCode opcode = instr->m_opcode;
  993. if (OpCodeAttr::TempObjectCanStoreTemp(opcode))
  994. {
  995. // Special cases where stack allocation doesn't happen
  996. #if ENABLE_REGEX_CONFIG_OPTIONS
  997. if (opcode == Js::OpCode::NewRegEx && REGEX_CONFIG_FLAG(RegexTracing))
  998. {
  999. return false;
  1000. }
  1001. #endif
  1002. if (opcode == Js::OpCode::NewScObjectNoCtor)
  1003. {
  1004. if (PHASE_OFF(Js::FixedNewObjPhase, instr->m_func->GetJnFunction()) && PHASE_OFF(Js::ObjTypeSpecNewObjPhase, instr->m_func->GetTopFunc()))
  1005. {
  1006. return false;
  1007. }
  1008. // Only if we have BailOutFailedCtorGuardCheck would we generate a stack object.
  1009. // Otherwise we will call the helper, which will not generate stack object.
  1010. return instr->HasBailOutInfo();
  1011. }
  1012. return true;
  1013. }
  1014. return false;
  1015. }
  1016. bool
  1017. ObjectTemp::CanMarkTemp(IR::Instr * instr, BackwardPass * backwardPass)
  1018. {
  1019. // We mark the ArgOut with the call in ProcessInstr, no need to do it here
  1020. return IsTempProducing(instr) || IsTempTransfer(instr);
  1021. }
  1022. void
  1023. ObjectTemp::ProcessBailOnNoProfile(IR::Instr * instr)
  1024. {
  1025. Assert(instr->m_opcode == Js::OpCode::BailOnNoProfile);
  1026. // ObjectTemp is done during Backward pass, which hasn't change all succ to BailOnNoProfile
  1027. // to dead yet, so we need to manually clear all the information
  1028. this->nonTempSyms.ClearAll();
  1029. this->tempTransferredSyms.ClearAll();
  1030. if (this->tempTransferDependencies)
  1031. {
  1032. this->tempTransferDependencies->ClearAll();
  1033. }
  1034. }
  1035. void
  1036. ObjectTemp::ProcessInstr(IR::Instr * instr)
  1037. {
  1038. if (instr->m_opcode != Js::OpCode::CallDirect)
  1039. {
  1040. return;
  1041. }
  1042. IR::HelperCallOpnd * helper = instr->GetSrc1()->AsHelperCallOpnd();
  1043. switch (helper->m_fnHelper)
  1044. {
  1045. case IR::JnHelperMethod::HelperString_Match:
  1046. case IR::JnHelperMethod::HelperString_Replace:
  1047. {
  1048. // First (non-this) parameter is either a regexp or search string.
  1049. // It doesn't escape.
  1050. IR::Instr * instrArgDef;
  1051. instr->FindCallArgumentOpnd(2, &instrArgDef);
  1052. instrArgDef->dstIsTempObject = true;
  1053. break;
  1054. }
  1055. case IR::JnHelperMethod::HelperRegExp_Exec:
  1056. {
  1057. IR::Instr * instrArgDef;
  1058. instr->FindCallArgumentOpnd(1, &instrArgDef);
  1059. instrArgDef->dstIsTempObject = true;
  1060. break;
  1061. }
  1062. };
  1063. }
  1064. void
  1065. ObjectTemp::SetDstIsTemp(bool dstIsTemp, bool dstIsTempTransferred, IR::Instr * instr, BackwardPass * backwardPass)
  1066. {
  1067. Assert(dstIsTemp || !dstIsTempTransferred);
  1068. // ArgOut_A are marked by CallDirect and don't need to be set
  1069. if (instr->m_opcode == Js::OpCode::ArgOut_A)
  1070. {
  1071. return;
  1072. }
  1073. instr->dstIsTempObject = dstIsTemp;
  1074. if (!backwardPass->IsPrePass())
  1075. {
  1076. if (OpCodeAttr::TempObjectProducing(instr->m_opcode))
  1077. {
  1078. backwardPass->func->SetHasMarkTempObjects();
  1079. #if DBG_DUMP
  1080. backwardPass->numMarkTempObject += dstIsTemp;
  1081. #endif
  1082. }
  1083. }
  1084. }
  1085. StackSym *
  1086. ObjectTemp::GetStackSym(IR::Opnd * opnd, IR::PropertySymOpnd ** pPropertySymOpnd)
  1087. {
  1088. StackSym * stackSym = nullptr;
  1089. switch (opnd->GetKind())
  1090. {
  1091. case IR::OpndKindReg:
  1092. stackSym = opnd->AsRegOpnd()->m_sym;
  1093. break;
  1094. case IR::OpndKindSym:
  1095. {
  1096. IR::SymOpnd * symOpnd = opnd->AsSymOpnd();
  1097. if (symOpnd->IsPropertySymOpnd())
  1098. {
  1099. IR::PropertySymOpnd * propertySymOpnd = symOpnd->AsPropertySymOpnd();
  1100. *pPropertySymOpnd = propertySymOpnd;
  1101. stackSym = propertySymOpnd->GetObjectSym();
  1102. }
  1103. else if (symOpnd->m_sym->IsPropertySym())
  1104. {
  1105. stackSym = symOpnd->m_sym->AsPropertySym()->m_stackSym;
  1106. }
  1107. break;
  1108. }
  1109. case IR::OpndKindIndir:
  1110. stackSym = opnd->AsIndirOpnd()->GetBaseOpnd()->m_sym;
  1111. break;
  1112. };
  1113. return stackSym;
  1114. }
  1115. #if DBG
  1116. //=================================================================================================
  1117. // ObjectTempVerify
  1118. //=================================================================================================
  1119. ObjectTempVerify::ObjectTempVerify(JitArenaAllocator * alloc, bool inLoop)
  1120. : TempTrackerBase(alloc, inLoop), removedUpwardExposedUse(alloc)
  1121. {
  1122. }
  1123. bool
  1124. ObjectTempVerify::IsTempUse(IR::Instr * instr, Sym * sym, BackwardPass * backwardPass)
  1125. {
  1126. Js::OpCode opcode = instr->m_opcode;
  1127. // If the opcode has implicit call and the profile say we have implicit call, then it is not a temp use.
  1128. // TODO: More precise implicit call tracking
  1129. bool isLandingPad = backwardPass->currentBlock->IsLandingPad();
  1130. if (OpCodeAttr::HasImplicitCall(opcode) && !isLandingPad
  1131. &&
  1132. ((backwardPass->currentBlock->loop != nullptr ?
  1133. !GlobOpt::ImplicitCallFlagsAllowOpts(backwardPass->currentBlock->loop) :
  1134. !GlobOpt::ImplicitCallFlagsAllowOpts(backwardPass->func))
  1135. || instr->CallsAccessor())
  1136. )
  1137. {
  1138. return false;
  1139. }
  1140. if (!ObjectTemp::IsTempUseOpCodeSym(instr, opcode, sym))
  1141. {
  1142. // the opcode and sym is not a temp use, just return
  1143. return false;
  1144. }
  1145. // In the backward pass, this would have been a temp use already. Continue to verify
  1146. // if we have install sufficient bailout on implicit call
  1147. if (isLandingPad || !GlobOpt::MayNeedBailOnImplicitCall(instr, nullptr, nullptr))
  1148. {
  1149. // Implicit call would not happen, or we are in the landing pad where implicit call is disabled.
  1150. return true;
  1151. }
  1152. if (instr->HasBailOutInfo())
  1153. {
  1154. // make sure we have mark the bailout for mark temp object,
  1155. // so that we won't optimize it away in DeadStoreImplicitCalls
  1156. return ((instr->GetBailOutKind() & IR::BailOutMarkTempObject) != 0);
  1157. }
  1158. // Review (ObjTypeSpec): This is a bit conservative now that we don't revert from obj type specialized operations to live cache
  1159. // access even if the operation is isolated. Once we decide a given instruction is an object type spec candidate, we know it
  1160. // will never need an implicit call, so we could basically do opnd->IsObjTypeSpecOptimized() here, instead.
  1161. if (GlobOpt::IsTypeCheckProtected(instr))
  1162. {
  1163. return true;
  1164. }
  1165. return false;
  1166. }
  1167. bool
  1168. ObjectTempVerify::IsTempTransfer(IR::Instr * instr)
  1169. {
  1170. if (ObjectTemp::IsTempTransfer(instr)
  1171. // Add the Ld_I4, and LdC_A_I4 as the forward pass might have changed Ld_A to these
  1172. || instr->m_opcode == Js::OpCode::Ld_I4
  1173. || instr->m_opcode == Js::OpCode::LdC_A_I4)
  1174. {
  1175. if (!instr->dstIsTempObject && instr->GetDst() && instr->GetDst()->IsRegOpnd()
  1176. && instr->GetDst()->AsRegOpnd()->GetValueType().IsNotObject())
  1177. {
  1178. // Globopt has proved that dst is not an object, so this is not really an object transfer.
  1179. // This prevents the case where glob opt turned a Conv_Num to Ld_A and expose additional
  1180. // transfer.
  1181. return false;
  1182. }
  1183. return true;
  1184. }
  1185. return false;
  1186. }
  1187. bool
  1188. ObjectTempVerify::CanMarkTemp(IR::Instr * instr, BackwardPass * backwardPass)
  1189. {
  1190. // We mark the ArgOut with the call in ProcessInstr, no need to do it here
  1191. return ObjectTemp::IsTempProducing(instr)
  1192. || IsTempTransfer(instr);
  1193. }
  1194. void
  1195. ObjectTempVerify::ProcessInstr(IR::Instr * instr, BackwardPass * backwardPass)
  1196. {
  1197. if (instr->m_opcode == Js::OpCode::InlineThrow)
  1198. {
  1199. // We cannot accurately track mark temp for any upward exposed symbol here
  1200. this->removedUpwardExposedUse.Or(backwardPass->currentBlock->byteCodeUpwardExposedUsed);
  1201. return;
  1202. }
  1203. if (instr->m_opcode != Js::OpCode::CallDirect)
  1204. {
  1205. return;
  1206. }
  1207. IR::HelperCallOpnd * helper = instr->GetSrc1()->AsHelperCallOpnd();
  1208. switch (helper->m_fnHelper)
  1209. {
  1210. case IR::JnHelperMethod::HelperString_Match:
  1211. case IR::JnHelperMethod::HelperString_Replace:
  1212. {
  1213. // First (non-this) parameter is either a regexp or search string
  1214. // It doesn't escape
  1215. IR::Instr * instrArgDef;
  1216. instr->FindCallArgumentOpnd(2, &instrArgDef);
  1217. Assert(instrArgDef->dstIsTempObject);
  1218. break;
  1219. }
  1220. case IR::JnHelperMethod::HelperRegExp_Exec:
  1221. {
  1222. IR::Instr * instrArgDef;
  1223. instr->FindCallArgumentOpnd(1, &instrArgDef);
  1224. Assert(instrArgDef->dstIsTempObject);
  1225. break;
  1226. }
  1227. };
  1228. }
  1229. void
  1230. ObjectTempVerify::SetDstIsTemp(bool dstIsTemp, bool dstIsTempTransferred, IR::Instr * instr, BackwardPass * backwardPass)
  1231. {
  1232. Assert(dstIsTemp || !dstIsTempTransferred);
  1233. // ArgOut_A are marked by CallDirect and don't need to be set
  1234. if (instr->m_opcode == Js::OpCode::ArgOut_A)
  1235. {
  1236. return;
  1237. }
  1238. if (OpCodeAttr::TempObjectProducing(instr->m_opcode))
  1239. {
  1240. if (!backwardPass->IsPrePass())
  1241. {
  1242. if (dstIsTemp)
  1243. {
  1244. // Don't assert if we have detected a removed upward exposed use that could
  1245. // expose a new mark temp object. Don't assert if it is set in removedUpwardExposedUse
  1246. bool isBailOnNoProfileUpwardExposedUse =
  1247. !!this->removedUpwardExposedUse.Test(instr->GetDst()->AsRegOpnd()->m_sym->m_id);
  1248. #if DBG
  1249. if (DoTrace(backwardPass) && !instr->dstIsTempObject && !isBailOnNoProfileUpwardExposedUse)
  1250. {
  1251. Output::Print(L"%s: Missed Mark Temp Object: ", GetTraceName());
  1252. instr->DumpSimple();
  1253. Output::Flush();
  1254. }
  1255. #endif
  1256. // TODO: Unfortunately we still hit this a lot as we are not accounting for some of the globopt changes
  1257. // to the IR. It is just reporting that we have missed mark temp object opportunity, so it doesn't
  1258. // indicate a functional failure. Disable for now.
  1259. // Assert(instr->dstIsTempObject || isBailOnNoProfileUpwardExposedUse);
  1260. }
  1261. else
  1262. {
  1263. // If we have marked the dst is temp in the backward pass, the globopt
  1264. // should have maintained it, and it will be wrong to have detect that it is not
  1265. // temp now in the deadstore pass (whether there is BailOnNoProfile or not)
  1266. #if DBG
  1267. if (DoTrace(backwardPass) && instr->dstIsTempObject)
  1268. {
  1269. Output::Print(L"%s: Invalid Mark Temp Object: ", GetTraceName());
  1270. instr->DumpSimple();
  1271. Output::Flush();
  1272. }
  1273. #endif
  1274. Assert(!instr->dstIsTempObject);
  1275. }
  1276. }
  1277. }
  1278. else if (IsTempTransfer(instr))
  1279. {
  1280. // Only set the transfer
  1281. instr->dstIsTempObject = dstIsTemp;
  1282. }
  1283. else
  1284. {
  1285. Assert(!dstIsTemp);
  1286. Assert(!instr->dstIsTempObject);
  1287. }
  1288. // clear or transfer the bailOnNoProfile upward exposed use
  1289. if (this->removedUpwardExposedUse.TestAndClear(instr->GetDst()->AsRegOpnd()->m_sym->m_id)
  1290. && IsTempTransfer(instr) && instr->GetSrc1()->IsRegOpnd())
  1291. {
  1292. this->removedUpwardExposedUse.Set(instr->GetSrc1()->AsRegOpnd()->m_sym->m_id);
  1293. }
  1294. }
  1295. void
  1296. ObjectTempVerify::MergeData(ObjectTempVerify * fromData, bool deleteData)
  1297. {
  1298. this->removedUpwardExposedUse.Or(&fromData->removedUpwardExposedUse);
  1299. }
  1300. void
  1301. ObjectTempVerify::MergeDeadData(BasicBlock * block)
  1302. {
  1303. MergeData(block->tempObjectVerifyTracker, false);
  1304. if (!block->isDead)
  1305. {
  1306. // If there was dead flow to a block that is not dead, it might expose
  1307. // new mark temp object, so all its current used (upwardExposedUsed) and optimized
  1308. // use (byteCodeupwardExposedUsed) might not be trace for "missed" mark temp object
  1309. this->removedUpwardExposedUse.Or(block->upwardExposedUses);
  1310. if (block->byteCodeUpwardExposedUsed)
  1311. {
  1312. this->removedUpwardExposedUse.Or(block->byteCodeUpwardExposedUsed);
  1313. }
  1314. }
  1315. }
  1316. void
  1317. ObjectTempVerify::NotifyBailOutRemoval(IR:: Instr * instr, BackwardPass * backwardPass)
  1318. {
  1319. Js::OpCode opcode = instr->m_opcode;
  1320. switch (opcode)
  1321. {
  1322. case Js::OpCode::LdFld:
  1323. case Js::OpCode::LdFldForTypeOf:
  1324. case Js::OpCode::LdMethodFld:
  1325. ((TempTracker<ObjectTempVerify> *)this)->ProcessUse(instr->GetSrc1()->AsPropertySymOpnd()->GetObjectSym(), backwardPass);
  1326. break;
  1327. case Js::OpCode::InitFld:
  1328. case Js::OpCode::StFld:
  1329. case Js::OpCode::StFldStrict:
  1330. ((TempTracker<ObjectTempVerify> *)this)->ProcessUse(instr->GetDst()->AsPropertySymOpnd()->GetObjectSym(), backwardPass);
  1331. break;
  1332. case Js::OpCode::LdElemI_A:
  1333. ((TempTracker<ObjectTempVerify> *)this)->ProcessUse(instr->GetSrc1()->AsIndirOpnd()->GetBaseOpnd()->m_sym, backwardPass);
  1334. break;
  1335. case Js::OpCode::StElemI_A:
  1336. ((TempTracker<ObjectTempVerify> *)this)->ProcessUse(instr->GetDst()->AsIndirOpnd()->GetBaseOpnd()->m_sym, backwardPass);
  1337. break;
  1338. }
  1339. }
  1340. void
  1341. ObjectTempVerify::NotifyReverseCopyProp(IR::Instr * instr)
  1342. {
  1343. Assert(instr->GetDst());
  1344. SymID symId = instr->GetDst()->AsRegOpnd()->m_sym->m_id;
  1345. this->removedUpwardExposedUse.Clear(symId);
  1346. this->nonTempSyms.Clear(symId);
  1347. }
  1348. void
  1349. ObjectTempVerify::NotifyDeadStore(IR::Instr * instr, BackwardPass * backwardPass)
  1350. {
  1351. // Even if we dead store, simulate the uses
  1352. IR::Opnd * src1 = instr->GetSrc1();
  1353. if (src1)
  1354. {
  1355. IR::PropertySymOpnd * propertySymOpnd;
  1356. StackSym * stackSym = ObjectTemp::GetStackSym(src1, &propertySymOpnd);
  1357. if (stackSym)
  1358. {
  1359. ((TempTracker<ObjectTempVerify> *)this)->ProcessUse(stackSym, backwardPass);
  1360. }
  1361. IR::Opnd * src2 = instr->GetSrc2();
  1362. if (src2)
  1363. {
  1364. stackSym = ObjectTemp::GetStackSym(src2, &propertySymOpnd);
  1365. if (stackSym)
  1366. {
  1367. ((TempTracker<ObjectTempVerify> *)this)->ProcessUse(stackSym, backwardPass);
  1368. }
  1369. }
  1370. }
  1371. }
  1372. void
  1373. ObjectTempVerify::NotifyDeadByteCodeUses(IR::Instr * instr)
  1374. {
  1375. if (instr->GetDst())
  1376. {
  1377. SymID symId = instr->GetDst()->AsRegOpnd()->m_sym->m_id;
  1378. this->removedUpwardExposedUse.Clear(symId);
  1379. this->nonTempSyms.Clear(symId);
  1380. }
  1381. IR::ByteCodeUsesInstr *byteCodeUsesInstr = instr->AsByteCodeUsesInstr();
  1382. BVSparse<JitArenaAllocator> * byteCodeUpwardExposedUsed = byteCodeUsesInstr->byteCodeUpwardExposedUsed;
  1383. if (byteCodeUpwardExposedUsed != nullptr)
  1384. {
  1385. this->removedUpwardExposedUse.Or(byteCodeUpwardExposedUsed);
  1386. }
  1387. }
  1388. bool
  1389. ObjectTempVerify::DependencyCheck(IR::Instr * instr, BVSparse<JitArenaAllocator> * bvTempTransferDependencies, BackwardPass * backwardPass)
  1390. {
  1391. if (!instr->dstIsTempObject)
  1392. {
  1393. // The instruction is not marked as temp object anyway, no need to do extra check
  1394. return false;
  1395. }
  1396. // Since our algorithm is conservative, there are cases where even though two defs are unrelated, the use will still
  1397. // seem like overlapping and not mark-temp-able
  1398. // For example:
  1399. // = s6.blah
  1400. // s1 = LdRootFld
  1401. // s6 = s1
  1402. // s1 = NewScObject // s1 is dependent of s6, and s6 is upward exposed.
  1403. // = s6.blah
  1404. // s6 = s1
  1405. // Here, although s1 is mark temp able because the s6.blah use is not related, we only know that s1 is dependent of s6
  1406. // so it looks like s1 may overlap through the iterations. The backward pass will be able to catch that and not mark temp them
  1407. // However, the globopt may create situation like the above while it wasn't there in the backward phase
  1408. // For example:
  1409. // = s6.blah
  1410. // s1 = LdRootFld g
  1411. // s6 = s1
  1412. // s1 = NewScObject
  1413. // s7 = LdRootFld g
  1414. // = s7.blah // Globopt copy prop s7 -> s6, creating the example above.
  1415. // s6 = s1
  1416. // This make it impossible to verify whether we did the right thing using the conservative algorithm.
  1417. // Luckily, this case is very rare (ExprGen didn't hit it with > 100K test cases)
  1418. // So we can use this rather expensive algorithm to find out if any of upward exposed used that we think overlaps
  1419. // really get their value from the marked temp sym or not.
  1420. // See unittest\Object\stackobject_dependency.js (with -maxinterpretcount:1 -off:inline)
  1421. BasicBlock * currentBlock = backwardPass->currentBlock;
  1422. BVSparse<JitArenaAllocator> * upwardExposedUses = currentBlock->upwardExposedUses;
  1423. JitArenaAllocator tempAllocator(L"temp", instr->m_func->m_alloc->GetPageAllocator(), Js::Throw::OutOfMemory);
  1424. BVSparse<JitArenaAllocator> * dependentSyms = bvTempTransferDependencies->AndNew(upwardExposedUses, &tempAllocator);
  1425. BVSparse<JitArenaAllocator> * initialDependentSyms = dependentSyms->CopyNew();
  1426. Assert(!dependentSyms->IsEmpty());
  1427. struct BlockRecord
  1428. {
  1429. BasicBlock * block;
  1430. BVSparse<JitArenaAllocator> * dependentSyms;
  1431. };
  1432. SList<BlockRecord> blockStack(&tempAllocator);
  1433. JsUtil::BaseDictionary<BasicBlock *, BVSparse<JitArenaAllocator> *, JitArenaAllocator> processedSyms(&tempAllocator);
  1434. IR::Instr * currentInstr = instr;
  1435. Assert(instr->GetDst()->AsRegOpnd()->m_sym->IsVar());
  1436. SymID markTempSymId = instr->GetDst()->AsRegOpnd()->m_sym->m_id;
  1437. bool initial = true;
  1438. while (true)
  1439. {
  1440. while (currentInstr != currentBlock->GetFirstInstr())
  1441. {
  1442. if (initial)
  1443. {
  1444. initial = false;
  1445. }
  1446. else if (currentInstr == instr)
  1447. {
  1448. if (dependentSyms->Test(markTempSymId))
  1449. {
  1450. // One of the dependent sym from the original set get it's value from the current marked temp dst.
  1451. // The dst definitely cannot be temp because it's lifetime overlaps across iterations.
  1452. return false;
  1453. }
  1454. // If we have already check the same dependent sym, no need to do it again.
  1455. // It will produce the same result anyway.
  1456. dependentSyms->Minus(initialDependentSyms);
  1457. if (dependentSyms->IsEmpty())
  1458. {
  1459. break;
  1460. }
  1461. // Add in newly discovered dependentSym so we won't do it again when it come back here.
  1462. initialDependentSyms->Or(dependentSyms);
  1463. }
  1464. if (currentInstr->GetDst() && currentInstr->GetDst()->IsRegOpnd())
  1465. {
  1466. // Clear the def and mark the src if it is transferred.
  1467. // If the dst sym is a type specialized sym, clear the var sym instead.
  1468. StackSym * dstSym = currentInstr->GetDst()->AsRegOpnd()->m_sym;
  1469. if (!dstSym->IsVar())
  1470. {
  1471. dstSym = dstSym->GetVarEquivSym(nullptr);
  1472. }
  1473. if (dstSym && dependentSyms->TestAndClear(dstSym->m_id) &&
  1474. IsTempTransfer(currentInstr) && currentInstr->GetSrc1()->IsRegOpnd())
  1475. {
  1476. // We only really care about var syms uses for object temp.
  1477. StackSym * srcSym = currentInstr->GetSrc1()->AsRegOpnd()->m_sym;
  1478. if (srcSym->IsVar())
  1479. {
  1480. dependentSyms->Set(srcSym->m_id);
  1481. }
  1482. }
  1483. if (dependentSyms->IsEmpty())
  1484. {
  1485. // No more dependent sym, we found the def of all of them we can move on to the next block.
  1486. break;
  1487. }
  1488. }
  1489. currentInstr = currentInstr->m_prev;
  1490. }
  1491. if (currentBlock->isLoopHeader && !dependentSyms->IsEmpty())
  1492. {
  1493. Assert(currentInstr == currentBlock->GetFirstInstr());
  1494. // If we have try to propagate the symbol through the loop before, we don't need to propagate it again.
  1495. BVSparse<JitArenaAllocator> * currentLoopProcessedSyms = processedSyms.Lookup(currentBlock, nullptr);
  1496. if (currentLoopProcessedSyms == nullptr)
  1497. {
  1498. processedSyms.Add(currentBlock, dependentSyms->CopyNew());
  1499. }
  1500. else
  1501. {
  1502. dependentSyms->Minus(currentLoopProcessedSyms);
  1503. currentLoopProcessedSyms->Or(dependentSyms);
  1504. }
  1505. }
  1506. if (!dependentSyms->IsEmpty())
  1507. {
  1508. Assert(currentInstr == currentBlock->GetFirstInstr());
  1509. FOREACH_PREDECESSOR_BLOCK(predBlock, currentBlock)
  1510. {
  1511. if (predBlock->loop == nullptr)
  1512. {
  1513. // No need to track outside of loops.
  1514. continue;
  1515. }
  1516. BlockRecord record;
  1517. record.block = predBlock;
  1518. record.dependentSyms = dependentSyms->CopyNew();
  1519. blockStack.Prepend(record);
  1520. }
  1521. NEXT_PREDECESSOR_BLOCK;
  1522. }
  1523. JitAdelete(&tempAllocator, dependentSyms);
  1524. if (blockStack.Empty())
  1525. {
  1526. // No more blocks. We are done.
  1527. break;
  1528. }
  1529. currentBlock = blockStack.Head().block;
  1530. dependentSyms = blockStack.Head().dependentSyms;
  1531. blockStack.RemoveHead();
  1532. currentInstr = currentBlock->GetLastInstr();
  1533. }
  1534. // All the dependent sym doesn't get their value from the marked temp def, so it can really be marked temp.
  1535. #if DBG
  1536. if (DoTrace(backwardPass))
  1537. {
  1538. Output::Print(L"%s: Unrelated overlap mark temp (s%-3d): ", GetTraceName(), markTempSymId);
  1539. instr->DumpSimple();
  1540. Output::Flush();
  1541. }
  1542. #endif
  1543. return true;
  1544. }
  1545. #endif
  1546. #if DBG
  1547. bool
  1548. NumberTemp::DoTrace(BackwardPass * backwardPass)
  1549. {
  1550. return PHASE_TRACE(Js::MarkTempNumberPhase, backwardPass->func);
  1551. }
  1552. bool
  1553. ObjectTemp::DoTrace(BackwardPass * backwardPass)
  1554. {
  1555. return PHASE_TRACE(Js::MarkTempObjectPhase, backwardPass->func);
  1556. }
  1557. bool
  1558. ObjectTempVerify::DoTrace(BackwardPass * backwardPass)
  1559. {
  1560. return PHASE_TRACE(Js::MarkTempObjectPhase, backwardPass->func);
  1561. }
  1562. #endif
  1563. // explicit instantiation
  1564. template class TempTracker<NumberTemp>;
  1565. template class TempTracker<ObjectTemp>;
  1566. #if DBG
  1567. template class TempTracker<ObjectTempVerify>;
  1568. #endif