TempTracker.cpp 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728
  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(char16 const * traceName)
  174. {
  175. Output::Print(_u("%s: Non temp syms:"), traceName);
  176. this->nonTempSyms.Dump();
  177. Output::Print(_u("%s: Temp transferred syms:"), traceName);
  178. this->tempTransferredSyms.Dump();
  179. if (this->tempTransferDependencies != nullptr)
  180. {
  181. Output::Print(_u("%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(_u("%s: %8s%4sTemp Use (s%-3d): "), T::GetTraceName(),
  207. backwardPass->IsPrePass() ? _u("Prepass ") : _u(""), isTempUse ? _u("") : _u("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. this->AddTransferDependencies(usedSymID, dstSymID, this->tempTransferDependencies);
  227. #if DBG_DUMP
  228. if (T::DoTrace(backwardPass))
  229. {
  230. Output::Print(_u("%s: %8s s%d -> s%d: "), T::GetTraceName(),
  231. backwardPass->IsPrePass() ? _u("Prepass ") : _u(""), 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. this->PropagateTempPropertyTransferStoreDependencies(usedSymID, propertySym, backwardPass);
  244. #if DBG_DUMP
  245. if (T::DoTrace(backwardPass) && this->tempTransferDependencies)
  246. {
  247. Output::Print(_u("%s: %8s (PropId:%d %s)+[] -> s%d: "), T::GetTraceName(),
  248. backwardPass->IsPrePass() ? _u("Prepass ") : _u(""), propertySym->m_propertyId,
  249. backwardPass->func->GetThreadContextInfo()->GetPropertyRecord(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(_u("[]\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(_u("%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(_u("%s: Loop mark temp check instr:\n"), T::GetTraceName());
  383. instr->DumpSimple();
  384. Output::Print(_u("Transfer dependencies: "));
  385. bvTempTransferDependencies->Dump();
  386. Output::Print(_u("Upward exposed Uses : "));
  387. backwardPass->currentBlock->upwardExposedUses->Dump();
  388. Output::Print(_u("\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(_u("%s: Not temp (s%-03d): "), T::GetTraceName(), sym->m_id);
  400. instr->DumpSimple();
  401. Output::Print(_u(" Transferred exposed uses: "));
  402. JitArenaAllocator tempAllocator(_u("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->GetThreadContextInfo()->GetPropertyRecord(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(_u("%s: %8s s%d -> []: "), NumberTemp::GetTraceName(),
  787. backwardPass->IsPrePass() ? _u("Prepass ") : _u(""), dstSymID);
  788. elemLoadDependencies.Dump();
  789. }
  790. #endif
  791. }
  792. #if DBG_DUMP
  793. if (NumberTemp::DoTrace(backwardPass))
  794. {
  795. Output::Print(_u("%s: %8s%4sTemp Use ([] )"), NumberTemp::GetTraceName(),
  796. backwardPass->IsPrePass() ? _u("Prepass ") : _u(""), isTempUse ? _u("") : _u("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(_u("%s: %8s s%d -> PropId:%d %s: "), NumberTemp::GetTraceName(),
  841. backwardPass->IsPrePass() ? _u("Prepass ") : _u(""), dstSymID, propertySym->m_propertyId,
  842. backwardPass->func->GetThreadContextInfo()->GetPropertyRecord(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(_u("%s: %8s%4sTemp Use (PropId:%d %s)"), NumberTemp::GetTraceName(),
  851. backwardPass->IsPrePass() ? _u("Prepass ") : _u(""), isTempUse ? _u("") : _u("Non "), propertySym->m_propertyId,
  852. backwardPass->func->GetThreadContextInfo()->GetPropertyRecord(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(char16 const * traceName)
  874. {
  875. if (nonTempElemLoad)
  876. {
  877. Output::Print(_u("%s: Has Non Temp Elem Load\n"), traceName);
  878. }
  879. else
  880. {
  881. Output::Print(_u("%s: Non Temp Syms"), traceName);
  882. this->nonTempSyms.Dump();
  883. if (this->propertyIdsTempTransferDependencies != nullptr)
  884. {
  885. Output::Print(_u("%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::LdLen_A:
  919. return instr->GetSrc1()->AsRegOpnd()->GetStackSym() == sym;
  920. case Js::OpCode::ArgOut_A:
  921. return instr->dstIsTempObject;
  922. case Js::OpCode::LdFld:
  923. case Js::OpCode::LdFldForTypeOf:
  924. case Js::OpCode::LdMethodFld:
  925. case Js::OpCode::LdFldForCallApplyTarget:
  926. case Js::OpCode::LdMethodFromFlags:
  927. return instr->GetSrc1()->AsPropertySymOpnd()->GetObjectSym() == sym;
  928. case Js::OpCode::InitFld:
  929. if (Js::PropertyRecord::DefaultAttributesForPropertyId(
  930. instr->GetDst()->AsPropertySymOpnd()->GetPropertySym()->m_propertyId, true) & PropertyDeleted)
  931. {
  932. // If the property record is marked PropertyDeleted, the InitFld will cause a type handler conversion,
  933. // which may result in creation of a weak reference to the object itself.
  934. return false;
  935. }
  936. // Fall through
  937. case Js::OpCode::StFld:
  938. case Js::OpCode::StFldStrict:
  939. return
  940. !(instr->GetSrc1() && instr->GetSrc1()->GetStackSym() == sym) &&
  941. !(instr->GetSrc2() && instr->GetSrc2()->GetStackSym() == sym) &&
  942. instr->GetDst()->AsPropertySymOpnd()->GetObjectSym() == sym;
  943. case Js::OpCode::LdElemI_A:
  944. return instr->GetSrc1()->AsIndirOpnd()->GetBaseOpnd()->m_sym == sym;
  945. case Js::OpCode::StElemI_A:
  946. case Js::OpCode::StElemI_A_Strict:
  947. return instr->GetDst()->AsIndirOpnd()->GetBaseOpnd()->m_sym == sym;
  948. case Js::OpCode::Memset:
  949. return instr->GetDst()->AsIndirOpnd()->GetBaseOpnd()->m_sym == sym || (instr->GetSrc1()->IsRegOpnd() && instr->GetSrc1()->AsRegOpnd()->m_sym == sym);
  950. case Js::OpCode::Memcopy:
  951. return instr->GetDst()->AsIndirOpnd()->GetBaseOpnd()->m_sym == sym || instr->GetSrc1()->AsIndirOpnd()->GetBaseOpnd()->m_sym == sym;
  952. // Special case FromVar for now until we can allow CallsValueOf opcode to be accept temp use
  953. case Js::OpCode::FromVar:
  954. return true;
  955. }
  956. // TODO: Currently, when we disable implicit call, we still don't allow valueOf/toString that has no side effects
  957. // So we shouldn't mark them if we have use of the sym on opcode that does OpndHasImplicitCall yet.
  958. if (OpCodeAttr::OpndHasImplicitCall(opcode))
  959. {
  960. return false;
  961. }
  962. // Mark the symbol as non-tempable if the instruction doesn't allow temp sources,
  963. // or it is transferred to a non-temp dst
  964. return (OpCodeAttr::TempObjectSources(opcode)
  965. && (!OpCodeAttr::TempObjectTransfer(opcode) || instr->dstIsTempObject));
  966. }
  967. bool
  968. ObjectTemp::IsTempTransfer(IR::Instr * instr)
  969. {
  970. return OpCodeAttr::TempObjectTransfer(instr->m_opcode);
  971. }
  972. bool
  973. ObjectTemp::IsTempProducing(IR::Instr * instr)
  974. {
  975. Js::OpCode opcode = instr->m_opcode;
  976. if (OpCodeAttr::TempObjectProducing(opcode))
  977. {
  978. return true;
  979. }
  980. // TODO: Process NewScObject and CallI with isCtorCall when the ctor is fixed
  981. return false;
  982. }
  983. bool
  984. ObjectTemp::CanStoreTemp(IR::Instr * instr)
  985. {
  986. // In order to allow storing temp number on temp objects,
  987. // We have to make sure that if the instr is marked as dstIsTempObject
  988. // we will always generate the code to allocate the object on the stack (so no helper call).
  989. // Currently, we only do this for NewRegEx, NewScObjectSimple, NewScObjectLiteral and
  990. // NewScObjectNoCtor (where the ctor is inlined).
  991. // CONSIDER: review lowering of other TempObjectProducing opcode and see if we can always allocate on the stack
  992. // (for example, NewScArray should be able to, but plain NewScObject can't because the size depends on the
  993. // number inline slots)
  994. Js::OpCode opcode = instr->m_opcode;
  995. if (OpCodeAttr::TempObjectCanStoreTemp(opcode))
  996. {
  997. // Special cases where stack allocation doesn't happen
  998. #if ENABLE_REGEX_CONFIG_OPTIONS
  999. if (opcode == Js::OpCode::NewRegEx && REGEX_CONFIG_FLAG(RegexTracing))
  1000. {
  1001. return false;
  1002. }
  1003. #endif
  1004. if (opcode == Js::OpCode::NewScObjectNoCtor)
  1005. {
  1006. if (PHASE_OFF(Js::FixedNewObjPhase, instr->m_func) && PHASE_OFF(Js::ObjTypeSpecNewObjPhase, instr->m_func->GetTopFunc()))
  1007. {
  1008. return false;
  1009. }
  1010. // Only if we have BailOutFailedCtorGuardCheck would we generate a stack object.
  1011. // Otherwise we will call the helper, which will not generate stack object.
  1012. return instr->HasBailOutInfo();
  1013. }
  1014. return true;
  1015. }
  1016. return false;
  1017. }
  1018. bool
  1019. ObjectTemp::CanMarkTemp(IR::Instr * instr, BackwardPass * backwardPass)
  1020. {
  1021. // We mark the ArgOut with the call in ProcessInstr, no need to do it here
  1022. return IsTempProducing(instr) || IsTempTransfer(instr);
  1023. }
  1024. void
  1025. ObjectTemp::ProcessBailOnNoProfile(IR::Instr * instr)
  1026. {
  1027. Assert(instr->m_opcode == Js::OpCode::BailOnNoProfile);
  1028. // ObjectTemp is done during Backward pass, which hasn't change all succ to BailOnNoProfile
  1029. // to dead yet, so we need to manually clear all the information
  1030. this->nonTempSyms.ClearAll();
  1031. this->tempTransferredSyms.ClearAll();
  1032. if (this->tempTransferDependencies)
  1033. {
  1034. this->tempTransferDependencies->ClearAll();
  1035. }
  1036. }
  1037. void
  1038. ObjectTemp::ProcessInstr(IR::Instr * instr)
  1039. {
  1040. if (instr->m_opcode != Js::OpCode::CallDirect)
  1041. {
  1042. return;
  1043. }
  1044. IR::HelperCallOpnd * helper = instr->GetSrc1()->AsHelperCallOpnd();
  1045. switch (helper->m_fnHelper)
  1046. {
  1047. case IR::JnHelperMethod::HelperString_Match:
  1048. case IR::JnHelperMethod::HelperString_Replace:
  1049. {
  1050. // First (non-this) parameter is either a regexp or search string.
  1051. // It doesn't escape.
  1052. IR::Instr * instrArgDef;
  1053. instr->FindCallArgumentOpnd(2, &instrArgDef);
  1054. instrArgDef->dstIsTempObject = true;
  1055. break;
  1056. }
  1057. case IR::JnHelperMethod::HelperRegExp_Exec:
  1058. {
  1059. IR::Instr * instrArgDef;
  1060. instr->FindCallArgumentOpnd(1, &instrArgDef);
  1061. instrArgDef->dstIsTempObject = true;
  1062. break;
  1063. }
  1064. };
  1065. }
  1066. void
  1067. ObjectTemp::SetDstIsTemp(bool dstIsTemp, bool dstIsTempTransferred, IR::Instr * instr, BackwardPass * backwardPass)
  1068. {
  1069. Assert(dstIsTemp || !dstIsTempTransferred);
  1070. // ArgOut_A are marked by CallDirect and don't need to be set
  1071. if (instr->m_opcode == Js::OpCode::ArgOut_A)
  1072. {
  1073. return;
  1074. }
  1075. instr->dstIsTempObject = dstIsTemp;
  1076. if (!backwardPass->IsPrePass())
  1077. {
  1078. if (OpCodeAttr::TempObjectProducing(instr->m_opcode))
  1079. {
  1080. backwardPass->func->SetHasMarkTempObjects();
  1081. #if DBG_DUMP
  1082. backwardPass->numMarkTempObject += dstIsTemp;
  1083. #endif
  1084. }
  1085. }
  1086. }
  1087. StackSym *
  1088. ObjectTemp::GetStackSym(IR::Opnd * opnd, IR::PropertySymOpnd ** pPropertySymOpnd)
  1089. {
  1090. StackSym * stackSym = nullptr;
  1091. switch (opnd->GetKind())
  1092. {
  1093. case IR::OpndKindReg:
  1094. stackSym = opnd->AsRegOpnd()->m_sym;
  1095. break;
  1096. case IR::OpndKindSym:
  1097. {
  1098. IR::SymOpnd * symOpnd = opnd->AsSymOpnd();
  1099. if (symOpnd->IsPropertySymOpnd())
  1100. {
  1101. IR::PropertySymOpnd * propertySymOpnd = symOpnd->AsPropertySymOpnd();
  1102. *pPropertySymOpnd = propertySymOpnd;
  1103. stackSym = propertySymOpnd->GetObjectSym();
  1104. }
  1105. else if (symOpnd->m_sym->IsPropertySym())
  1106. {
  1107. stackSym = symOpnd->m_sym->AsPropertySym()->m_stackSym;
  1108. }
  1109. break;
  1110. }
  1111. case IR::OpndKindIndir:
  1112. stackSym = opnd->AsIndirOpnd()->GetBaseOpnd()->m_sym;
  1113. break;
  1114. };
  1115. return stackSym;
  1116. }
  1117. #if DBG
  1118. //=================================================================================================
  1119. // ObjectTempVerify
  1120. //=================================================================================================
  1121. ObjectTempVerify::ObjectTempVerify(JitArenaAllocator * alloc, bool inLoop)
  1122. : TempTrackerBase(alloc, inLoop), removedUpwardExposedUse(alloc)
  1123. {
  1124. }
  1125. bool
  1126. ObjectTempVerify::IsTempUse(IR::Instr * instr, Sym * sym, BackwardPass * backwardPass)
  1127. {
  1128. Js::OpCode opcode = instr->m_opcode;
  1129. // If the opcode has implicit call and the profile say we have implicit call, then it is not a temp use.
  1130. // TODO: More precise implicit call tracking
  1131. bool isLandingPad = backwardPass->currentBlock->IsLandingPad();
  1132. if (OpCodeAttr::HasImplicitCall(opcode) && !isLandingPad
  1133. &&
  1134. ((backwardPass->currentBlock->loop != nullptr ?
  1135. !GlobOpt::ImplicitCallFlagsAllowOpts(backwardPass->currentBlock->loop) :
  1136. !GlobOpt::ImplicitCallFlagsAllowOpts(backwardPass->func))
  1137. || instr->CallsAccessor())
  1138. )
  1139. {
  1140. return false;
  1141. }
  1142. if (!ObjectTemp::IsTempUseOpCodeSym(instr, opcode, sym))
  1143. {
  1144. // the opcode and sym is not a temp use, just return
  1145. return false;
  1146. }
  1147. // In the backward pass, this would have been a temp use already. Continue to verify
  1148. // if we have install sufficient bailout on implicit call
  1149. if (isLandingPad || !GlobOpt::MayNeedBailOnImplicitCall(instr, nullptr, nullptr))
  1150. {
  1151. // Implicit call would not happen, or we are in the landing pad where implicit call is disabled.
  1152. return true;
  1153. }
  1154. if (instr->HasBailOutInfo())
  1155. {
  1156. // make sure we have mark the bailout for mark temp object,
  1157. // so that we won't optimize it away in DeadStoreImplicitCalls
  1158. return ((instr->GetBailOutKind() & IR::BailOutMarkTempObject) != 0);
  1159. }
  1160. // Review (ObjTypeSpec): This is a bit conservative now that we don't revert from obj type specialized operations to live cache
  1161. // access even if the operation is isolated. Once we decide a given instruction is an object type spec candidate, we know it
  1162. // will never need an implicit call, so we could basically do opnd->IsObjTypeSpecOptimized() here, instead.
  1163. if (GlobOpt::IsTypeCheckProtected(instr))
  1164. {
  1165. return true;
  1166. }
  1167. return false;
  1168. }
  1169. bool
  1170. ObjectTempVerify::IsTempTransfer(IR::Instr * instr)
  1171. {
  1172. if (ObjectTemp::IsTempTransfer(instr)
  1173. // Add the Ld_I4, and LdC_A_I4 as the forward pass might have changed Ld_A to these
  1174. || instr->m_opcode == Js::OpCode::Ld_I4
  1175. || instr->m_opcode == Js::OpCode::LdC_A_I4)
  1176. {
  1177. if (!instr->dstIsTempObject && instr->GetDst() && instr->GetDst()->IsRegOpnd()
  1178. && instr->GetDst()->AsRegOpnd()->GetValueType().IsNotObject())
  1179. {
  1180. // Globopt has proved that dst is not an object, so this is not really an object transfer.
  1181. // This prevents the case where glob opt turned a Conv_Num to Ld_A and expose additional
  1182. // transfer.
  1183. return false;
  1184. }
  1185. return true;
  1186. }
  1187. return false;
  1188. }
  1189. bool
  1190. ObjectTempVerify::CanMarkTemp(IR::Instr * instr, BackwardPass * backwardPass)
  1191. {
  1192. // We mark the ArgOut with the call in ProcessInstr, no need to do it here
  1193. return ObjectTemp::IsTempProducing(instr)
  1194. || IsTempTransfer(instr);
  1195. }
  1196. void
  1197. ObjectTempVerify::ProcessInstr(IR::Instr * instr, BackwardPass * backwardPass)
  1198. {
  1199. if (instr->m_opcode == Js::OpCode::InlineThrow)
  1200. {
  1201. // We cannot accurately track mark temp for any upward exposed symbol here
  1202. this->removedUpwardExposedUse.Or(backwardPass->currentBlock->byteCodeUpwardExposedUsed);
  1203. return;
  1204. }
  1205. if (instr->m_opcode != Js::OpCode::CallDirect)
  1206. {
  1207. return;
  1208. }
  1209. IR::HelperCallOpnd * helper = instr->GetSrc1()->AsHelperCallOpnd();
  1210. switch (helper->m_fnHelper)
  1211. {
  1212. case IR::JnHelperMethod::HelperString_Match:
  1213. case IR::JnHelperMethod::HelperString_Replace:
  1214. {
  1215. // First (non-this) parameter is either a regexp or search string
  1216. // It doesn't escape
  1217. IR::Instr * instrArgDef;
  1218. instr->FindCallArgumentOpnd(2, &instrArgDef);
  1219. Assert(instrArgDef->dstIsTempObject);
  1220. break;
  1221. }
  1222. case IR::JnHelperMethod::HelperRegExp_Exec:
  1223. {
  1224. IR::Instr * instrArgDef;
  1225. instr->FindCallArgumentOpnd(1, &instrArgDef);
  1226. Assert(instrArgDef->dstIsTempObject);
  1227. break;
  1228. }
  1229. };
  1230. }
  1231. void
  1232. ObjectTempVerify::SetDstIsTemp(bool dstIsTemp, bool dstIsTempTransferred, IR::Instr * instr, BackwardPass * backwardPass)
  1233. {
  1234. Assert(dstIsTemp || !dstIsTempTransferred);
  1235. // ArgOut_A are marked by CallDirect and don't need to be set
  1236. if (instr->m_opcode == Js::OpCode::ArgOut_A)
  1237. {
  1238. return;
  1239. }
  1240. if (OpCodeAttr::TempObjectProducing(instr->m_opcode))
  1241. {
  1242. if (!backwardPass->IsPrePass())
  1243. {
  1244. if (dstIsTemp)
  1245. {
  1246. // Don't assert if we have detected a removed upward exposed use that could
  1247. // expose a new mark temp object. Don't assert if it is set in removedUpwardExposedUse
  1248. bool isBailOnNoProfileUpwardExposedUse =
  1249. !!this->removedUpwardExposedUse.Test(instr->GetDst()->AsRegOpnd()->m_sym->m_id);
  1250. #if DBG
  1251. if (DoTrace(backwardPass) && !instr->dstIsTempObject && !isBailOnNoProfileUpwardExposedUse)
  1252. {
  1253. Output::Print(_u("%s: Missed Mark Temp Object: "), GetTraceName());
  1254. instr->DumpSimple();
  1255. Output::Flush();
  1256. }
  1257. #endif
  1258. // TODO: Unfortunately we still hit this a lot as we are not accounting for some of the globopt changes
  1259. // to the IR. It is just reporting that we have missed mark temp object opportunity, so it doesn't
  1260. // indicate a functional failure. Disable for now.
  1261. // Assert(instr->dstIsTempObject || isBailOnNoProfileUpwardExposedUse);
  1262. }
  1263. else
  1264. {
  1265. // If we have marked the dst is temp in the backward pass, the globopt
  1266. // should have maintained it, and it will be wrong to have detect that it is not
  1267. // temp now in the deadstore pass (whether there is BailOnNoProfile or not)
  1268. #if DBG
  1269. if (DoTrace(backwardPass) && instr->dstIsTempObject)
  1270. {
  1271. Output::Print(_u("%s: Invalid Mark Temp Object: "), GetTraceName());
  1272. instr->DumpSimple();
  1273. Output::Flush();
  1274. }
  1275. #endif
  1276. Assert(!instr->dstIsTempObject);
  1277. }
  1278. }
  1279. }
  1280. else if (IsTempTransfer(instr))
  1281. {
  1282. // Only set the transfer
  1283. instr->dstIsTempObject = dstIsTemp;
  1284. }
  1285. else
  1286. {
  1287. Assert(!dstIsTemp);
  1288. Assert(!instr->dstIsTempObject);
  1289. }
  1290. // clear or transfer the bailOnNoProfile upward exposed use
  1291. if (this->removedUpwardExposedUse.TestAndClear(instr->GetDst()->AsRegOpnd()->m_sym->m_id)
  1292. && IsTempTransfer(instr) && instr->GetSrc1()->IsRegOpnd())
  1293. {
  1294. this->removedUpwardExposedUse.Set(instr->GetSrc1()->AsRegOpnd()->m_sym->m_id);
  1295. }
  1296. }
  1297. void
  1298. ObjectTempVerify::MergeData(ObjectTempVerify * fromData, bool deleteData)
  1299. {
  1300. this->removedUpwardExposedUse.Or(&fromData->removedUpwardExposedUse);
  1301. }
  1302. void
  1303. ObjectTempVerify::MergeDeadData(BasicBlock * block)
  1304. {
  1305. MergeData(block->tempObjectVerifyTracker, false);
  1306. if (!block->isDead)
  1307. {
  1308. // If there was dead flow to a block that is not dead, it might expose
  1309. // new mark temp object, so all its current used (upwardExposedUsed) and optimized
  1310. // use (byteCodeupwardExposedUsed) might not be trace for "missed" mark temp object
  1311. this->removedUpwardExposedUse.Or(block->upwardExposedUses);
  1312. if (block->byteCodeUpwardExposedUsed)
  1313. {
  1314. this->removedUpwardExposedUse.Or(block->byteCodeUpwardExposedUsed);
  1315. }
  1316. }
  1317. }
  1318. void
  1319. ObjectTempVerify::NotifyBailOutRemoval(IR:: Instr * instr, BackwardPass * backwardPass)
  1320. {
  1321. Js::OpCode opcode = instr->m_opcode;
  1322. switch (opcode)
  1323. {
  1324. case Js::OpCode::LdFld:
  1325. case Js::OpCode::LdFldForTypeOf:
  1326. case Js::OpCode::LdMethodFld:
  1327. ((TempTracker<ObjectTempVerify> *)this)->ProcessUse(instr->GetSrc1()->AsPropertySymOpnd()->GetObjectSym(), backwardPass);
  1328. break;
  1329. case Js::OpCode::InitFld:
  1330. case Js::OpCode::StFld:
  1331. case Js::OpCode::StFldStrict:
  1332. ((TempTracker<ObjectTempVerify> *)this)->ProcessUse(instr->GetDst()->AsPropertySymOpnd()->GetObjectSym(), backwardPass);
  1333. break;
  1334. case Js::OpCode::LdElemI_A:
  1335. ((TempTracker<ObjectTempVerify> *)this)->ProcessUse(instr->GetSrc1()->AsIndirOpnd()->GetBaseOpnd()->m_sym, backwardPass);
  1336. break;
  1337. case Js::OpCode::StElemI_A:
  1338. ((TempTracker<ObjectTempVerify> *)this)->ProcessUse(instr->GetDst()->AsIndirOpnd()->GetBaseOpnd()->m_sym, backwardPass);
  1339. break;
  1340. }
  1341. }
  1342. void
  1343. ObjectTempVerify::NotifyReverseCopyProp(IR::Instr * instr)
  1344. {
  1345. Assert(instr->GetDst());
  1346. SymID symId = instr->GetDst()->AsRegOpnd()->m_sym->m_id;
  1347. this->removedUpwardExposedUse.Clear(symId);
  1348. this->nonTempSyms.Clear(symId);
  1349. }
  1350. void
  1351. ObjectTempVerify::NotifyDeadStore(IR::Instr * instr, BackwardPass * backwardPass)
  1352. {
  1353. // Even if we dead store, simulate the uses
  1354. IR::Opnd * src1 = instr->GetSrc1();
  1355. if (src1)
  1356. {
  1357. IR::PropertySymOpnd * propertySymOpnd;
  1358. StackSym * stackSym = ObjectTemp::GetStackSym(src1, &propertySymOpnd);
  1359. if (stackSym)
  1360. {
  1361. ((TempTracker<ObjectTempVerify> *)this)->ProcessUse(stackSym, backwardPass);
  1362. }
  1363. IR::Opnd * src2 = instr->GetSrc2();
  1364. if (src2)
  1365. {
  1366. stackSym = ObjectTemp::GetStackSym(src2, &propertySymOpnd);
  1367. if (stackSym)
  1368. {
  1369. ((TempTracker<ObjectTempVerify> *)this)->ProcessUse(stackSym, backwardPass);
  1370. }
  1371. }
  1372. }
  1373. }
  1374. void
  1375. ObjectTempVerify::NotifyDeadByteCodeUses(IR::Instr * instr)
  1376. {
  1377. if (instr->GetDst())
  1378. {
  1379. SymID symId = instr->GetDst()->AsRegOpnd()->m_sym->m_id;
  1380. this->removedUpwardExposedUse.Clear(symId);
  1381. this->nonTempSyms.Clear(symId);
  1382. }
  1383. IR::ByteCodeUsesInstr *byteCodeUsesInstr = instr->AsByteCodeUsesInstr();
  1384. BVSparse<JitArenaAllocator> * byteCodeUpwardExposedUsed = byteCodeUsesInstr->byteCodeUpwardExposedUsed;
  1385. if (byteCodeUpwardExposedUsed != nullptr)
  1386. {
  1387. this->removedUpwardExposedUse.Or(byteCodeUpwardExposedUsed);
  1388. }
  1389. }
  1390. bool
  1391. ObjectTempVerify::DependencyCheck(IR::Instr * instr, BVSparse<JitArenaAllocator> * bvTempTransferDependencies, BackwardPass * backwardPass)
  1392. {
  1393. if (!instr->dstIsTempObject)
  1394. {
  1395. // The instruction is not marked as temp object anyway, no need to do extra check
  1396. return false;
  1397. }
  1398. // Since our algorithm is conservative, there are cases where even though two defs are unrelated, the use will still
  1399. // seem like overlapping and not mark-temp-able
  1400. // For example:
  1401. // = s6.blah
  1402. // s1 = LdRootFld
  1403. // s6 = s1
  1404. // s1 = NewScObject // s1 is dependent of s6, and s6 is upward exposed.
  1405. // = s6.blah
  1406. // s6 = s1
  1407. // Here, although s1 is mark temp able because the s6.blah use is not related, we only know that s1 is dependent of s6
  1408. // so it looks like s1 may overlap through the iterations. The backward pass will be able to catch that and not mark temp them
  1409. // However, the globopt may create situation like the above while it wasn't there in the backward phase
  1410. // For example:
  1411. // = s6.blah
  1412. // s1 = LdRootFld g
  1413. // s6 = s1
  1414. // s1 = NewScObject
  1415. // s7 = LdRootFld g
  1416. // = s7.blah // Globopt copy prop s7 -> s6, creating the example above.
  1417. // s6 = s1
  1418. // This make it impossible to verify whether we did the right thing using the conservative algorithm.
  1419. // Luckily, this case is very rare (ExprGen didn't hit it with > 100K test cases)
  1420. // So we can use this rather expensive algorithm to find out if any of upward exposed used that we think overlaps
  1421. // really get their value from the marked temp sym or not.
  1422. // See unittest\Object\stackobject_dependency.js (with -maxinterpretcount:1 -off:inline)
  1423. BasicBlock * currentBlock = backwardPass->currentBlock;
  1424. BVSparse<JitArenaAllocator> * upwardExposedUses = currentBlock->upwardExposedUses;
  1425. JitArenaAllocator tempAllocator(_u("temp"), instr->m_func->m_alloc->GetPageAllocator(), Js::Throw::OutOfMemory);
  1426. BVSparse<JitArenaAllocator> * dependentSyms = bvTempTransferDependencies->AndNew(upwardExposedUses, &tempAllocator);
  1427. BVSparse<JitArenaAllocator> * initialDependentSyms = dependentSyms->CopyNew();
  1428. Assert(!dependentSyms->IsEmpty());
  1429. struct BlockRecord
  1430. {
  1431. BasicBlock * block;
  1432. BVSparse<JitArenaAllocator> * dependentSyms;
  1433. };
  1434. SList<BlockRecord> blockStack(&tempAllocator);
  1435. JsUtil::BaseDictionary<BasicBlock *, BVSparse<JitArenaAllocator> *, JitArenaAllocator> processedSyms(&tempAllocator);
  1436. IR::Instr * currentInstr = instr;
  1437. Assert(instr->GetDst()->AsRegOpnd()->m_sym->IsVar());
  1438. SymID markTempSymId = instr->GetDst()->AsRegOpnd()->m_sym->m_id;
  1439. bool initial = true;
  1440. while (true)
  1441. {
  1442. while (currentInstr != currentBlock->GetFirstInstr())
  1443. {
  1444. if (initial)
  1445. {
  1446. initial = false;
  1447. }
  1448. else if (currentInstr == instr)
  1449. {
  1450. if (dependentSyms->Test(markTempSymId))
  1451. {
  1452. // One of the dependent sym from the original set get it's value from the current marked temp dst.
  1453. // The dst definitely cannot be temp because it's lifetime overlaps across iterations.
  1454. return false;
  1455. }
  1456. // If we have already check the same dependent sym, no need to do it again.
  1457. // It will produce the same result anyway.
  1458. dependentSyms->Minus(initialDependentSyms);
  1459. if (dependentSyms->IsEmpty())
  1460. {
  1461. break;
  1462. }
  1463. // Add in newly discovered dependentSym so we won't do it again when it come back here.
  1464. initialDependentSyms->Or(dependentSyms);
  1465. }
  1466. if (currentInstr->GetDst() && currentInstr->GetDst()->IsRegOpnd())
  1467. {
  1468. // Clear the def and mark the src if it is transferred.
  1469. // If the dst sym is a type specialized sym, clear the var sym instead.
  1470. StackSym * dstSym = currentInstr->GetDst()->AsRegOpnd()->m_sym;
  1471. if (!dstSym->IsVar())
  1472. {
  1473. dstSym = dstSym->GetVarEquivSym(nullptr);
  1474. }
  1475. if (dstSym && dependentSyms->TestAndClear(dstSym->m_id) &&
  1476. IsTempTransfer(currentInstr) && currentInstr->GetSrc1()->IsRegOpnd())
  1477. {
  1478. // We only really care about var syms uses for object temp.
  1479. StackSym * srcSym = currentInstr->GetSrc1()->AsRegOpnd()->m_sym;
  1480. if (srcSym->IsVar())
  1481. {
  1482. dependentSyms->Set(srcSym->m_id);
  1483. }
  1484. }
  1485. if (dependentSyms->IsEmpty())
  1486. {
  1487. // No more dependent sym, we found the def of all of them we can move on to the next block.
  1488. break;
  1489. }
  1490. }
  1491. currentInstr = currentInstr->m_prev;
  1492. }
  1493. if (currentBlock->isLoopHeader && !dependentSyms->IsEmpty())
  1494. {
  1495. Assert(currentInstr == currentBlock->GetFirstInstr());
  1496. // If we have try to propagate the symbol through the loop before, we don't need to propagate it again.
  1497. BVSparse<JitArenaAllocator> * currentLoopProcessedSyms = processedSyms.Lookup(currentBlock, nullptr);
  1498. if (currentLoopProcessedSyms == nullptr)
  1499. {
  1500. processedSyms.Add(currentBlock, dependentSyms->CopyNew());
  1501. }
  1502. else
  1503. {
  1504. dependentSyms->Minus(currentLoopProcessedSyms);
  1505. currentLoopProcessedSyms->Or(dependentSyms);
  1506. }
  1507. }
  1508. if (!dependentSyms->IsEmpty())
  1509. {
  1510. Assert(currentInstr == currentBlock->GetFirstInstr());
  1511. FOREACH_PREDECESSOR_BLOCK(predBlock, currentBlock)
  1512. {
  1513. if (predBlock->loop == nullptr)
  1514. {
  1515. // No need to track outside of loops.
  1516. continue;
  1517. }
  1518. BlockRecord record;
  1519. record.block = predBlock;
  1520. record.dependentSyms = dependentSyms->CopyNew();
  1521. blockStack.Prepend(record);
  1522. }
  1523. NEXT_PREDECESSOR_BLOCK;
  1524. }
  1525. JitAdelete(&tempAllocator, dependentSyms);
  1526. if (blockStack.Empty())
  1527. {
  1528. // No more blocks. We are done.
  1529. break;
  1530. }
  1531. currentBlock = blockStack.Head().block;
  1532. dependentSyms = blockStack.Head().dependentSyms;
  1533. blockStack.RemoveHead();
  1534. currentInstr = currentBlock->GetLastInstr();
  1535. }
  1536. // All the dependent sym doesn't get their value from the marked temp def, so it can really be marked temp.
  1537. #if DBG
  1538. if (DoTrace(backwardPass))
  1539. {
  1540. Output::Print(_u("%s: Unrelated overlap mark temp (s%-3d): "), GetTraceName(), markTempSymId);
  1541. instr->DumpSimple();
  1542. Output::Flush();
  1543. }
  1544. #endif
  1545. return true;
  1546. }
  1547. #endif
  1548. #if DBG
  1549. bool
  1550. NumberTemp::DoTrace(BackwardPass * backwardPass)
  1551. {
  1552. return PHASE_TRACE(Js::MarkTempNumberPhase, backwardPass->func);
  1553. }
  1554. bool
  1555. ObjectTemp::DoTrace(BackwardPass * backwardPass)
  1556. {
  1557. return PHASE_TRACE(Js::MarkTempObjectPhase, backwardPass->func);
  1558. }
  1559. bool
  1560. ObjectTempVerify::DoTrace(BackwardPass * backwardPass)
  1561. {
  1562. return PHASE_TRACE(Js::MarkTempObjectPhase, backwardPass->func);
  1563. }
  1564. #endif
  1565. // explicit instantiation
  1566. template class TempTracker<NumberTemp>;
  1567. template class TempTracker<ObjectTemp>;
  1568. #if DBG
  1569. template class TempTracker<ObjectTempVerify>;
  1570. #endif