GlobOptFields.cpp 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962
  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. bool
  7. GlobOpt::DoFieldCopyProp() const
  8. {
  9. BasicBlock *block = this->currentBlock;
  10. Loop *loop = block->loop;
  11. if (this->isRecursiveCallOnLandingPad)
  12. {
  13. // The landing pad at this point only contains load hosted by PRE.
  14. // These need to be copy-prop'd into the loop.
  15. // We want to look at the implicit-call info of the loop, not it's parent.
  16. Assert(block->IsLandingPad());
  17. loop = block->next->loop;
  18. Assert(loop);
  19. }
  20. return DoFieldCopyProp(loop);
  21. }
  22. bool
  23. GlobOpt::DoFunctionFieldCopyProp() const
  24. {
  25. return DoFieldCopyProp(nullptr);
  26. }
  27. bool
  28. GlobOpt::DoFieldCopyProp(Loop * loop) const
  29. {
  30. if (PHASE_OFF(Js::CopyPropPhase, this->func))
  31. {
  32. // Can't do field copy prop without copy prop
  33. return false;
  34. }
  35. if (PHASE_FORCE(Js::FieldCopyPropPhase, this->func))
  36. {
  37. // Force always turns on field copy prop
  38. return true;
  39. }
  40. if (PHASE_OFF(Js::FieldCopyPropPhase, this->func))
  41. {
  42. return false;
  43. }
  44. return this->DoFieldOpts(loop);
  45. }
  46. bool
  47. GlobOpt::DoObjTypeSpec() const
  48. {
  49. return this->DoObjTypeSpec(this->currentBlock->loop);
  50. }
  51. bool
  52. GlobOpt::DoObjTypeSpec(Loop *loop) const
  53. {
  54. if (!this->func->DoFastPaths())
  55. {
  56. return false;
  57. }
  58. if (PHASE_FORCE(Js::ObjTypeSpecPhase, this->func))
  59. {
  60. return true;
  61. }
  62. if (PHASE_OFF(Js::ObjTypeSpecPhase, this->func))
  63. {
  64. return false;
  65. }
  66. if (this->func->IsLoopBody() && this->func->HasProfileInfo() && this->func->GetReadOnlyProfileInfo()->IsObjTypeSpecDisabledInJitLoopBody())
  67. {
  68. return false;
  69. }
  70. if (this->ImplicitCallFlagsAllowOpts(this->func))
  71. {
  72. Assert(loop == nullptr || loop->CanDoFieldCopyProp());
  73. return true;
  74. }
  75. return loop != nullptr && loop->CanDoFieldCopyProp();
  76. }
  77. bool
  78. GlobOpt::DoFieldOpts(Loop * loop) const
  79. {
  80. if (this->ImplicitCallFlagsAllowOpts(this->func))
  81. {
  82. Assert(loop == nullptr || loop->CanDoFieldCopyProp());
  83. return true;
  84. }
  85. return loop != nullptr && loop->CanDoFieldCopyProp();
  86. }
  87. bool GlobOpt::DoFieldPRE() const
  88. {
  89. Loop *loop = this->currentBlock->loop;
  90. return DoFieldPRE(loop);
  91. }
  92. bool
  93. GlobOpt::DoFieldPRE(Loop *loop) const
  94. {
  95. if (PHASE_OFF(Js::FieldPREPhase, this->func))
  96. {
  97. return false;
  98. }
  99. if (PHASE_FORCE(Js::FieldPREPhase, func))
  100. {
  101. // Force always turns on field PRE
  102. return true;
  103. }
  104. if (this->func->HasProfileInfo() && this->func->GetReadOnlyProfileInfo()->IsFieldPREDisabled())
  105. {
  106. return false;
  107. }
  108. return DoFieldOpts(loop);
  109. }
  110. bool GlobOpt::HasMemOp(Loop *loop)
  111. {
  112. #pragma prefast(suppress: 6285, "logical-or of constants is by design")
  113. return (
  114. loop &&
  115. loop->doMemOp &&
  116. (
  117. !PHASE_OFF(Js::MemSetPhase, this->func) ||
  118. !PHASE_OFF(Js::MemCopyPhase, this->func)
  119. ) &&
  120. loop->memOpInfo &&
  121. loop->memOpInfo->candidates &&
  122. !loop->memOpInfo->candidates->Empty()
  123. );
  124. }
  125. void
  126. GlobOpt::KillLiveFields(StackSym * stackSym, BVSparse<JitArenaAllocator> * bv)
  127. {
  128. if (stackSym->IsTypeSpec())
  129. {
  130. stackSym = stackSym->GetVarEquivSym(this->func);
  131. }
  132. Assert(stackSym);
  133. // If the sym has no objectSymInfo, it must not represent an object and, hence, has no type sym or
  134. // property syms to kill.
  135. if (!stackSym->HasObjectInfo() || stackSym->IsSingleDef())
  136. {
  137. return;
  138. }
  139. // Note that the m_writeGuardSym is killed here as well, because it is part of the
  140. // m_propertySymList of the object.
  141. ObjectSymInfo * objectSymInfo = stackSym->GetObjectInfo();
  142. PropertySym * propertySym = objectSymInfo->m_propertySymList;
  143. while (propertySym != nullptr)
  144. {
  145. Assert(propertySym->m_stackSym == stackSym);
  146. bv->Clear(propertySym->m_id);
  147. if (this->IsLoopPrePass())
  148. {
  149. for (Loop * loop = this->rootLoopPrePass; loop != nullptr; loop = loop->parent)
  150. {
  151. loop->fieldKilled->Set(propertySym->m_id);
  152. }
  153. }
  154. else if (bv->IsEmpty())
  155. {
  156. // shortcut
  157. break;
  158. }
  159. propertySym = propertySym->m_nextInStackSymList;
  160. }
  161. this->KillObjectType(stackSym, bv);
  162. }
  163. void
  164. GlobOpt::KillLiveFields(PropertySym * propertySym, BVSparse<JitArenaAllocator> * bv)
  165. {
  166. KillLiveFields(propertySym->m_propertyEquivSet, bv);
  167. }
  168. void GlobOpt::KillLiveFields(BVSparse<JitArenaAllocator> *const fieldsToKill, BVSparse<JitArenaAllocator> *const bv) const
  169. {
  170. Assert(bv);
  171. if (fieldsToKill)
  172. {
  173. bv->Minus(fieldsToKill);
  174. if (this->IsLoopPrePass())
  175. {
  176. for (Loop * loop = this->rootLoopPrePass; loop != nullptr; loop = loop->parent)
  177. {
  178. loop->fieldKilled->Or(fieldsToKill);
  179. }
  180. }
  181. }
  182. }
  183. void
  184. GlobOpt::KillLiveElems(IR::IndirOpnd * indirOpnd, BVSparse<JitArenaAllocator> * bv, bool inGlobOpt, Func *func)
  185. {
  186. IR::RegOpnd *indexOpnd = indirOpnd->GetIndexOpnd();
  187. // obj.x = 10;
  188. // obj["x"] = ...; // This needs to kill obj.x... We need to kill all fields...
  189. //
  190. // Also, 'arguments[i] =' needs to kill all slots even if 'i' is an int.
  191. //
  192. // NOTE: we only need to kill slots here, not all fields. It may be good to separate these one day.
  193. //
  194. // Regarding the check for type specialization:
  195. // - Type specialization does not always update the value to a definite type.
  196. // - The loop prepass is conservative on values even when type specialization occurs.
  197. // - We check the type specialization status for the sym as well. For the purpose of doing kills, we can assume that
  198. // if type specialization happened, that fields don't need to be killed. Note that they may be killed in the next
  199. // pass based on the value.
  200. if (func->GetThisOrParentInlinerHasArguments() ||
  201. (
  202. indexOpnd &&
  203. (
  204. indexOpnd->m_sym->m_isNotNumber ||
  205. (inGlobOpt && !indexOpnd->GetValueType().IsNumber() && !currentBlock->globOptData.IsTypeSpecialized(indexOpnd->m_sym))
  206. )
  207. ))
  208. {
  209. this->KillAllFields(bv); // This also kills all property type values, as the same bit-vector tracks those stack syms
  210. SetAnyPropertyMayBeWrittenTo();
  211. }
  212. }
  213. void
  214. GlobOpt::KillAllFields(BVSparse<JitArenaAllocator> * bv)
  215. {
  216. bv->ClearAll();
  217. if (this->IsLoopPrePass())
  218. {
  219. for (Loop * loop = this->rootLoopPrePass; loop != nullptr; loop = loop->parent)
  220. {
  221. loop->allFieldsKilled = true;
  222. }
  223. }
  224. }
  225. void
  226. GlobOpt::SetAnyPropertyMayBeWrittenTo()
  227. {
  228. this->func->anyPropertyMayBeWrittenTo = true;
  229. }
  230. void
  231. GlobOpt::AddToPropertiesWrittenTo(Js::PropertyId propertyId)
  232. {
  233. this->func->EnsurePropertiesWrittenTo();
  234. this->func->propertiesWrittenTo->Item(propertyId);
  235. }
  236. void
  237. GlobOpt::ProcessFieldKills(IR::Instr *instr, BVSparse<JitArenaAllocator> *bv, bool inGlobOpt)
  238. {
  239. if (bv->IsEmpty() && (!this->IsLoopPrePass() || this->rootLoopPrePass->allFieldsKilled))
  240. {
  241. return;
  242. }
  243. if (instr->m_opcode == Js::OpCode::FromVar || instr->m_opcode == Js::OpCode::Conv_Prim)
  244. {
  245. return;
  246. }
  247. IR::Opnd * dstOpnd = instr->GetDst();
  248. if (dstOpnd)
  249. {
  250. if (dstOpnd->IsRegOpnd())
  251. {
  252. Sym * sym = dstOpnd->AsRegOpnd()->m_sym;
  253. if (sym->IsStackSym())
  254. {
  255. KillLiveFields(sym->AsStackSym(), bv);
  256. }
  257. }
  258. else if (dstOpnd->IsSymOpnd())
  259. {
  260. Sym * sym = dstOpnd->AsSymOpnd()->m_sym;
  261. if (sym->IsStackSym())
  262. {
  263. KillLiveFields(sym->AsStackSym(), bv);
  264. }
  265. else
  266. {
  267. Assert(sym->IsPropertySym());
  268. if (instr->m_opcode == Js::OpCode::InitLetFld || instr->m_opcode == Js::OpCode::InitConstFld || instr->m_opcode == Js::OpCode::InitFld)
  269. {
  270. // These can grow the aux slot of the activation object.
  271. // We need to kill the slot array sym as well.
  272. PropertySym * slotArraySym = PropertySym::Find(sym->AsPropertySym()->m_stackSym->m_id,
  273. (Js::DynamicObject::GetOffsetOfAuxSlots())/sizeof(Js::Var) /*, PropertyKindSlotArray */, instr->m_func);
  274. if (slotArraySym)
  275. {
  276. bv->Clear(slotArraySym->m_id);
  277. }
  278. }
  279. }
  280. }
  281. }
  282. if (bv->IsEmpty() && (!this->IsLoopPrePass() || this->rootLoopPrePass->allFieldsKilled))
  283. {
  284. return;
  285. }
  286. Sym *sym;
  287. IR::JnHelperMethod fnHelper;
  288. switch(instr->m_opcode)
  289. {
  290. case Js::OpCode::StElemI_A:
  291. case Js::OpCode::StElemI_A_Strict:
  292. Assert(dstOpnd != nullptr);
  293. KillLiveFields(this->lengthEquivBv, bv);
  294. KillLiveElems(dstOpnd->AsIndirOpnd(), bv, inGlobOpt, instr->m_func);
  295. if (inGlobOpt)
  296. {
  297. KillObjectHeaderInlinedTypeSyms(this->currentBlock, false);
  298. }
  299. break;
  300. case Js::OpCode::InitComputedProperty:
  301. case Js::OpCode::InitGetElemI:
  302. case Js::OpCode::InitSetElemI:
  303. KillLiveElems(dstOpnd->AsIndirOpnd(), bv, inGlobOpt, instr->m_func);
  304. if (inGlobOpt)
  305. {
  306. KillObjectHeaderInlinedTypeSyms(this->currentBlock, false);
  307. }
  308. break;
  309. case Js::OpCode::DeleteElemI_A:
  310. case Js::OpCode::DeleteElemIStrict_A:
  311. Assert(dstOpnd != nullptr);
  312. KillLiveElems(instr->GetSrc1()->AsIndirOpnd(), bv, inGlobOpt, instr->m_func);
  313. break;
  314. case Js::OpCode::DeleteFld:
  315. case Js::OpCode::DeleteRootFld:
  316. case Js::OpCode::DeleteFldStrict:
  317. case Js::OpCode::DeleteRootFldStrict:
  318. sym = instr->GetSrc1()->AsSymOpnd()->m_sym;
  319. KillLiveFields(sym->AsPropertySym(), bv);
  320. if (inGlobOpt)
  321. {
  322. AddToPropertiesWrittenTo(sym->AsPropertySym()->m_propertyId);
  323. this->KillAllObjectTypes(bv);
  324. }
  325. break;
  326. case Js::OpCode::InitSetFld:
  327. case Js::OpCode::InitGetFld:
  328. case Js::OpCode::InitClassMemberGet:
  329. case Js::OpCode::InitClassMemberSet:
  330. sym = instr->GetDst()->AsSymOpnd()->m_sym;
  331. KillLiveFields(sym->AsPropertySym(), bv);
  332. if (inGlobOpt)
  333. {
  334. AddToPropertiesWrittenTo(sym->AsPropertySym()->m_propertyId);
  335. this->KillAllObjectTypes(bv);
  336. }
  337. break;
  338. case Js::OpCode::InitFld:
  339. case Js::OpCode::StFld:
  340. case Js::OpCode::StRootFld:
  341. case Js::OpCode::StFldStrict:
  342. case Js::OpCode::StRootFldStrict:
  343. case Js::OpCode::StSlot:
  344. case Js::OpCode::StSlotChkUndecl:
  345. Assert(dstOpnd != nullptr);
  346. sym = dstOpnd->AsSymOpnd()->m_sym;
  347. if (inGlobOpt)
  348. {
  349. AddToPropertiesWrittenTo(sym->AsPropertySym()->m_propertyId);
  350. }
  351. if ((inGlobOpt && (sym->AsPropertySym()->m_propertyId == Js::PropertyIds::valueOf || sym->AsPropertySym()->m_propertyId == Js::PropertyIds::toString)) ||
  352. instr->CallsAccessor())
  353. {
  354. // If overriding valueof/tostring, we might have expected a previous LdFld to bailout on implicitCalls but didn't.
  355. // CSE's for example would have expected a bailout. Clear all fields to prevent optimizing across.
  356. this->KillAllFields(bv);
  357. }
  358. else
  359. {
  360. KillLiveFields(sym->AsPropertySym(), bv);
  361. }
  362. break;
  363. case Js::OpCode::InlineArrayPush:
  364. case Js::OpCode::InlineArrayPop:
  365. KillLiveFields(this->lengthEquivBv, bv);
  366. if (inGlobOpt)
  367. {
  368. KillObjectHeaderInlinedTypeSyms(this->currentBlock, false);
  369. }
  370. break;
  371. case Js::OpCode::InlineeStart:
  372. case Js::OpCode::InlineeEnd:
  373. Assert(!instr->UsesAllFields());
  374. // Kill all live 'arguments' and 'caller' fields, as 'inlineeFunction.arguments' and 'inlineeFunction.caller'
  375. // cannot be copy-propped across different instances of the same inlined function.
  376. KillLiveFields(argumentsEquivBv, bv);
  377. KillLiveFields(callerEquivBv, bv);
  378. break;
  379. case Js::OpCode::CallDirect:
  380. fnHelper = instr->GetSrc1()->AsHelperCallOpnd()->m_fnHelper;
  381. // Kill length field for built-ins that can update it.
  382. if(fnHelper == IR::JnHelperMethod::HelperArray_Shift
  383. || fnHelper == IR::JnHelperMethod::HelperArray_Splice
  384. || fnHelper == IR::JnHelperMethod::HelperArray_Unshift)
  385. {
  386. if (nullptr != this->lengthEquivBv)
  387. {
  388. KillLiveFields(this->lengthEquivBv, bv);
  389. }
  390. if (inGlobOpt)
  391. {
  392. KillObjectHeaderInlinedTypeSyms(this->currentBlock, false);
  393. }
  394. }
  395. if ((fnHelper == IR::JnHelperMethod::HelperRegExp_Exec)
  396. || (fnHelper == IR::JnHelperMethod::HelperString_Match)
  397. || (fnHelper == IR::JnHelperMethod::HelperString_Replace))
  398. {
  399. // Consider: We may not need to kill all fields here.
  400. this->KillAllFields(bv);
  401. }
  402. break;
  403. case Js::OpCode::LdHeapArguments:
  404. case Js::OpCode::LdLetHeapArguments:
  405. case Js::OpCode::LdHeapArgsCached:
  406. case Js::OpCode::LdLetHeapArgsCached:
  407. if (inGlobOpt) {
  408. this->KillLiveFields(this->slotSyms, bv);
  409. }
  410. break;
  411. default:
  412. if (instr->UsesAllFields())
  413. {
  414. // This also kills all property type values, as the same bit-vector tracks those stack syms.
  415. this->KillAllFields(bv);
  416. }
  417. break;
  418. }
  419. }
  420. void
  421. GlobOpt::ProcessFieldKills(IR::Instr * instr)
  422. {
  423. if (!this->DoFieldCopyProp() && !this->DoFieldRefOpts() && !DoCSE())
  424. {
  425. Assert(this->currentBlock->globOptData.liveFields->IsEmpty());
  426. return;
  427. }
  428. ProcessFieldKills(instr, this->currentBlock->globOptData.liveFields, true);
  429. }
  430. Value *
  431. GlobOpt::CreateFieldSrcValue(PropertySym * sym, PropertySym * originalSym, IR::Opnd ** ppOpnd, IR::Instr * instr)
  432. {
  433. #if DBG
  434. // If the opcode going to kill all field values immediate anyway, we shouldn't be giving it a value
  435. Assert(!instr->UsesAllFields());
  436. AssertCanCopyPropOrCSEFieldLoad(instr);
  437. Assert(instr->GetSrc1() == *ppOpnd);
  438. #endif
  439. // Only give a value to fields if we are doing field copy prop.
  440. // Consider: We should always copy prop local slots, but the only use right now is LdSlot from jit loop body.
  441. // This should have one onus load, and thus no need for copy prop of field itself. We may want to support
  442. // copy prop LdSlot if there are other uses of local slots
  443. if (!this->DoFieldCopyProp())
  444. {
  445. return nullptr;
  446. }
  447. BOOL wasLive = this->currentBlock->globOptData.liveFields->TestAndSet(sym->m_id);
  448. if (sym != originalSym)
  449. {
  450. this->currentBlock->globOptData.liveFields->TestAndSet(originalSym->m_id);
  451. }
  452. if (!wasLive)
  453. {
  454. // We don't clear the value when we kill the field.
  455. // Clear it to make sure we don't use the old value.
  456. this->currentBlock->globOptData.ClearSymValue(sym);
  457. this->currentBlock->globOptData.ClearSymValue(originalSym);
  458. }
  459. Assert((*ppOpnd)->AsSymOpnd()->m_sym == sym || this->IsLoopPrePass());
  460. // We don't use the sym store to do copy prop on hoisted fields, but create a value
  461. // in case it can be copy prop out of the loop.
  462. return this->NewGenericValue(ValueType::Uninitialized, *ppOpnd);
  463. }
  464. bool
  465. GlobOpt::NeedBailOnImplicitCallWithFieldOpts(Loop *loop, bool hasLiveFields) const
  466. {
  467. if (!(((this->DoFieldRefOpts(loop) ||
  468. this->DoFieldCopyProp(loop)) &&
  469. hasLiveFields)))
  470. {
  471. return false;
  472. }
  473. return true;
  474. }
  475. IR::Instr *
  476. GlobOpt::EnsureDisableImplicitCallRegion(Loop * loop)
  477. {
  478. Assert(loop->bailOutInfo != nullptr);
  479. IR::Instr * endDisableImplicitCall = loop->endDisableImplicitCall;
  480. if (endDisableImplicitCall)
  481. {
  482. return endDisableImplicitCall;
  483. }
  484. IR::Instr * bailOutTarget = EnsureBailTarget(loop);
  485. Func * bailOutFunc = loop->GetFunc();
  486. Assert(loop->bailOutInfo->bailOutFunc == bailOutFunc);
  487. IR::MemRefOpnd * disableImplicitCallAddress = IR::MemRefOpnd::New(this->func->GetThreadContextInfo()->GetDisableImplicitFlagsAddr(), TyInt8, bailOutFunc);
  488. IR::IntConstOpnd * disableImplicitCallAndExceptionValue = IR::IntConstOpnd::New(DisableImplicitCallAndExceptionFlag, TyInt8, bailOutFunc, true);
  489. IR::IntConstOpnd * enableImplicitCallAndExceptionValue = IR::IntConstOpnd::New(DisableImplicitNoFlag, TyInt8, bailOutFunc, true);
  490. IR::Opnd * implicitCallFlags = Lowerer::GetImplicitCallFlagsOpnd(bailOutFunc);
  491. IR::IntConstOpnd * noImplicitCall = IR::IntConstOpnd::New(Js::ImplicitCall_None, TyInt8, bailOutFunc, true);
  492. // Consider: if we are already doing implicit call in the outer loop, we don't need to clear the implicit call bit again
  493. IR::Instr * clearImplicitCall = IR::Instr::New(Js::OpCode::Ld_A, implicitCallFlags, noImplicitCall, bailOutFunc);
  494. bailOutTarget->InsertBefore(clearImplicitCall);
  495. IR::Instr * disableImplicitCall = IR::Instr::New(Js::OpCode::Ld_A, disableImplicitCallAddress, disableImplicitCallAndExceptionValue, bailOutFunc);
  496. bailOutTarget->InsertBefore(disableImplicitCall);
  497. endDisableImplicitCall = IR::Instr::New(Js::OpCode::Ld_A, disableImplicitCallAddress, enableImplicitCallAndExceptionValue, bailOutFunc);
  498. bailOutTarget->InsertBefore(endDisableImplicitCall);
  499. IR::BailOutInstr * bailOutInstr = IR::BailOutInstr::New(Js::OpCode::BailOnNotEqual, IR::BailOutOnImplicitCalls, loop->bailOutInfo, loop->bailOutInfo->bailOutFunc);
  500. bailOutInstr->SetSrc1(implicitCallFlags);
  501. bailOutInstr->SetSrc2(noImplicitCall);
  502. bailOutTarget->InsertBefore(bailOutInstr);
  503. loop->endDisableImplicitCall = endDisableImplicitCall;
  504. return endDisableImplicitCall;
  505. }
  506. #if DBG
  507. bool
  508. GlobOpt::IsPropertySymId(SymID symId) const
  509. {
  510. return this->func->m_symTable->Find(symId)->IsPropertySym();
  511. }
  512. void
  513. GlobOpt::AssertCanCopyPropOrCSEFieldLoad(IR::Instr * instr)
  514. {
  515. // Consider: Hoisting LdRootFld may have complication with exception if the field doesn't exist.
  516. // We need to have another opcode for the hoisted version to avoid the exception and bailout.
  517. Assert(instr->m_opcode == Js::OpCode::LdSlot || instr->m_opcode == Js::OpCode::LdSlotArr
  518. || instr->m_opcode == Js::OpCode::LdFld || instr->m_opcode == Js::OpCode::LdFldForCallApplyTarget
  519. || instr->m_opcode == Js::OpCode::LdLen_A
  520. || instr->m_opcode == Js::OpCode::LdRootFld || instr->m_opcode == Js::OpCode::LdSuperFld
  521. || instr->m_opcode == Js::OpCode::LdFldForTypeOf || instr->m_opcode == Js::OpCode::LdRootFldForTypeOf
  522. || instr->m_opcode == Js::OpCode::LdMethodFld || instr->m_opcode == Js::OpCode::LdMethodFldPolyInlineMiss
  523. || instr->m_opcode == Js::OpCode::LdRootMethodFld
  524. || instr->m_opcode == Js::OpCode::LdMethodFromFlags
  525. || instr->m_opcode == Js::OpCode::ScopedLdMethodFld
  526. || instr->m_opcode == Js::OpCode::CheckFixedFld
  527. || instr->m_opcode == Js::OpCode::CheckPropertyGuardAndLoadType
  528. || instr->m_opcode == Js::OpCode::ScopedLdFld
  529. || instr->m_opcode == Js::OpCode::ScopedLdFldForTypeOf);
  530. Assert(instr->m_opcode == Js::OpCode::CheckFixedFld || instr->GetDst()->GetType() == TyVar || instr->m_func->GetJITFunctionBody()->IsAsmJsMode());
  531. Assert(instr->GetSrc1()->GetType() == TyVar || instr->m_func->GetJITFunctionBody()->IsAsmJsMode());
  532. Assert(instr->GetSrc1()->AsSymOpnd()->m_sym->IsPropertySym());
  533. Assert(instr->GetSrc2() == nullptr);
  534. }
  535. #endif
  536. StackSym *
  537. GlobOpt::EnsureObjectTypeSym(StackSym * objectSym)
  538. {
  539. Assert(!objectSym->IsTypeSpec());
  540. objectSym->EnsureObjectInfo(this->func);
  541. if (objectSym->HasObjectTypeSym())
  542. {
  543. Assert(this->objectTypeSyms);
  544. return objectSym->GetObjectTypeSym();
  545. }
  546. if (this->objectTypeSyms == nullptr)
  547. {
  548. this->objectTypeSyms = JitAnew(this->alloc, BVSparse<JitArenaAllocator>, this->alloc);
  549. }
  550. StackSym * typeSym = StackSym::New(TyVar, this->func);
  551. objectSym->GetObjectInfo()->m_typeSym = typeSym;
  552. this->objectTypeSyms->Set(typeSym->m_id);
  553. return typeSym;
  554. }
  555. PropertySym *
  556. GlobOpt::EnsurePropertyWriteGuardSym(PropertySym * propertySym)
  557. {
  558. // Make sure that the PropertySym has a proto cache sym which is chained into the propertySym list.
  559. if (!propertySym->m_writeGuardSym)
  560. {
  561. propertySym->m_writeGuardSym = PropertySym::New(propertySym->m_stackSym, propertySym->m_propertyId, (uint32)-1, (uint)-1, PropertyKindWriteGuard, this->func);
  562. }
  563. return propertySym->m_writeGuardSym;
  564. }
  565. void
  566. GlobOpt::PreparePropertySymForTypeCheckSeq(PropertySym *propertySym)
  567. {
  568. Assert(!propertySym->m_stackSym->IsTypeSpec());
  569. EnsureObjectTypeSym(propertySym->m_stackSym);
  570. EnsurePropertyWriteGuardSym(propertySym);
  571. }
  572. bool
  573. GlobOpt::IsPropertySymPreparedForTypeCheckSeq(PropertySym *propertySym)
  574. {
  575. Assert(!propertySym->m_stackSym->IsTypeSpec());
  576. // The following doesn't need to be true. We may copy prop a constant into an object sym, which has
  577. // previously been prepared for type check sequence optimization.
  578. // Assert(!propertySym->m_stackSym->m_isIntConst || !propertySym->HasObjectTypeSym());
  579. // The following doesn't need to be true. We may copy prop the object sym into a field load or store
  580. // that doesn't have object type spec info and hence the operand wasn't prepared and doesn't have a write
  581. // guard. The object sym, however, may have other field operations which are object type specialized and
  582. // thus the type sym for it has been created.
  583. // Assert(propertySym->HasObjectTypeSym() == propertySym->HasWriteGuardSym());
  584. return propertySym->HasObjectTypeSym();
  585. }
  586. bool
  587. GlobOpt::PreparePropertySymOpndForTypeCheckSeq(IR::PropertySymOpnd * propertySymOpnd, IR::Instr* instr, Loop * loop)
  588. {
  589. if (!DoFieldRefOpts(loop) || !OpCodeAttr::FastFldInstr(instr->m_opcode) || instr->CallsAccessor())
  590. {
  591. return false;
  592. }
  593. if (!propertySymOpnd->HasObjTypeSpecFldInfo())
  594. {
  595. return false;
  596. }
  597. ObjTypeSpecFldInfo* info = propertySymOpnd->GetObjTypeSpecInfo();
  598. if (info->UsesAccessor() || info->IsRootObjectNonConfigurableFieldLoad())
  599. {
  600. return false;
  601. }
  602. if (info->IsPoly() && !info->GetEquivalentTypeSet())
  603. {
  604. return false;
  605. }
  606. PropertySym * propertySym = propertySymOpnd->m_sym->AsPropertySym();
  607. PreparePropertySymForTypeCheckSeq(propertySym);
  608. propertySymOpnd->SetTypeCheckSeqCandidate(true);
  609. propertySymOpnd->SetIsBeingStored(propertySymOpnd == instr->GetDst());
  610. return true;
  611. }
  612. bool
  613. GlobOpt::CheckIfPropOpEmitsTypeCheck(IR::Instr *instr, IR::PropertySymOpnd *opnd)
  614. {
  615. if (!DoFieldRefOpts() || !OpCodeAttr::FastFldInstr(instr->m_opcode))
  616. {
  617. return false;
  618. }
  619. if (!opnd->IsTypeCheckSeqCandidate())
  620. {
  621. return false;
  622. }
  623. return CheckIfInstrInTypeCheckSeqEmitsTypeCheck(instr, opnd);
  624. }
  625. IR::PropertySymOpnd *
  626. GlobOpt::CreateOpndForTypeCheckOnly(IR::PropertySymOpnd* opnd, Func* func)
  627. {
  628. // Used only for CheckObjType instruction today. Future users should make a call
  629. // whether the new operand is jit optimized in their scenario or not.
  630. Assert(!opnd->IsRootObjectNonConfigurableFieldLoad());
  631. IR::PropertySymOpnd *newOpnd = opnd->CopyCommon(func);
  632. newOpnd->SetObjTypeSpecFldInfo(opnd->GetObjTypeSpecInfo());
  633. newOpnd->SetUsesAuxSlot(opnd->UsesAuxSlot());
  634. newOpnd->SetSlotIndex(opnd->GetSlotIndex());
  635. newOpnd->objTypeSpecFlags = opnd->objTypeSpecFlags;
  636. // If we're turning the instruction owning this operand into a CheckObjType, we will do a type check here
  637. // only for the sake of downstream instructions, so the flags pertaining to this property access are
  638. // irrelevant, because we don't do a property access here.
  639. newOpnd->SetTypeCheckOnly(true);
  640. newOpnd->usesFixedValue = false;
  641. newOpnd->finalType = opnd->finalType;
  642. newOpnd->guardedPropOps = opnd->guardedPropOps != nullptr ? opnd->guardedPropOps->CopyNew() : nullptr;
  643. newOpnd->writeGuards = opnd->writeGuards != nullptr ? opnd->writeGuards->CopyNew() : nullptr;
  644. newOpnd->SetIsJITOptimizedReg(true);
  645. return newOpnd;
  646. }
  647. bool
  648. GlobOpt::FinishOptPropOp(IR::Instr *instr, IR::PropertySymOpnd *opnd, BasicBlock* block, bool updateExistingValue, bool* emitsTypeCheckOut, bool* changesTypeValueOut)
  649. {
  650. if (!DoFieldRefOpts() || !OpCodeAttr::FastFldInstr(instr->m_opcode))
  651. {
  652. return false;
  653. }
  654. bool isTypeCheckSeqCandidate = opnd->IsTypeCheckSeqCandidate();
  655. bool isObjTypeSpecialized = false;
  656. bool isObjTypeChecked = false;
  657. if (isTypeCheckSeqCandidate)
  658. {
  659. isObjTypeSpecialized = ProcessPropOpInTypeCheckSeq<true>(instr, opnd, block, updateExistingValue, emitsTypeCheckOut, changesTypeValueOut, &isObjTypeChecked);
  660. }
  661. if (opnd == instr->GetDst() && this->objectTypeSyms)
  662. {
  663. if (block == nullptr)
  664. {
  665. block = this->currentBlock;
  666. }
  667. // This is a property store that may change the layout of the object that it stores to. This means that
  668. // it may change any aliased object. Do two things to address this:
  669. // - Add all object types in this function to the set that may have had a property added. This will prevent
  670. // final type optimization across this instruction. (Only needed here for non-specialized stores.)
  671. // - Kill all type symbols that currently hold object-header-inlined types. Any of them may have their layout
  672. // changed by the addition of a property.
  673. SymID opndId = opnd->HasObjectTypeSym() ? opnd->GetObjectTypeSym()->m_id : -1;
  674. if (!isObjTypeChecked)
  675. {
  676. if (block->globOptData.maybeWrittenTypeSyms == nullptr)
  677. {
  678. block->globOptData.maybeWrittenTypeSyms = JitAnew(this->alloc, BVSparse<JitArenaAllocator>, this->alloc);
  679. }
  680. if (isObjTypeSpecialized)
  681. {
  682. // The current object will be protected by a type check, unless no further accesses to it are
  683. // protected by this access.
  684. Assert(this->objectTypeSyms->Test(opndId));
  685. this->objectTypeSyms->Clear(opndId);
  686. }
  687. block->globOptData.maybeWrittenTypeSyms->Or(this->objectTypeSyms);
  688. if (isObjTypeSpecialized)
  689. {
  690. this->objectTypeSyms->Set(opndId);
  691. }
  692. }
  693. if (!isObjTypeSpecialized || opnd->ChangesObjectLayout())
  694. {
  695. this->KillObjectHeaderInlinedTypeSyms(block, isObjTypeSpecialized, opndId);
  696. }
  697. else if (!isObjTypeChecked && this->HasLiveObjectHeaderInlinedTypeSym(block, true, opndId))
  698. {
  699. opnd->SetTypeCheckRequired(true);
  700. }
  701. }
  702. return isObjTypeSpecialized;
  703. }
  704. void
  705. GlobOpt::KillObjectHeaderInlinedTypeSyms(BasicBlock *block, bool isObjTypeSpecialized, SymID opndId)
  706. {
  707. this->MapObjectHeaderInlinedTypeSymsUntil(block, isObjTypeSpecialized, opndId, [&](SymID symId)->bool { this->currentBlock->globOptData.liveFields->Clear(symId); return false; });
  708. }
  709. bool
  710. GlobOpt::HasLiveObjectHeaderInlinedTypeSym(BasicBlock *block, bool isObjTypeSpecialized, SymID opndId)
  711. {
  712. return this->MapObjectHeaderInlinedTypeSymsUntil(block, true, opndId, [&](SymID symId)->bool { return this->currentBlock->globOptData.liveFields->Test(symId); });
  713. }
  714. template<class Fn>
  715. bool
  716. GlobOpt::MapObjectHeaderInlinedTypeSymsUntil(BasicBlock *block, bool isObjTypeSpecialized, SymID opndId, Fn fn)
  717. {
  718. if (this->objectTypeSyms == nullptr)
  719. {
  720. return false;
  721. }
  722. FOREACH_BITSET_IN_SPARSEBV(symId, this->objectTypeSyms)
  723. {
  724. if (symId == opndId && isObjTypeSpecialized)
  725. {
  726. // The current object will be protected by a type check, unless no further accesses to it are
  727. // protected by this access.
  728. continue;
  729. }
  730. Value *value = block->globOptData.FindObjectTypeValue(symId);
  731. if (value)
  732. {
  733. JsTypeValueInfo *valueInfo = value->GetValueInfo()->AsJsType();
  734. Assert(valueInfo);
  735. if (valueInfo->GetJsType() != nullptr)
  736. {
  737. JITTypeHolder type(valueInfo->GetJsType());
  738. if (Js::DynamicType::Is(type->GetTypeId()))
  739. {
  740. if (type->GetTypeHandler()->IsObjectHeaderInlinedTypeHandler())
  741. {
  742. if (fn(symId))
  743. {
  744. return true;
  745. }
  746. }
  747. }
  748. }
  749. else if (valueInfo->GetJsTypeSet())
  750. {
  751. Js::EquivalentTypeSet *typeSet = valueInfo->GetJsTypeSet();
  752. for (uint16 i = 0; i < typeSet->GetCount(); i++)
  753. {
  754. JITTypeHolder type = typeSet->GetType(i);
  755. if (type != nullptr && Js::DynamicType::Is(type->GetTypeId()))
  756. {
  757. if (type->GetTypeHandler()->IsObjectHeaderInlinedTypeHandler())
  758. {
  759. if (fn(symId))
  760. {
  761. return true;
  762. }
  763. break;
  764. }
  765. }
  766. }
  767. }
  768. }
  769. }
  770. NEXT_BITSET_IN_SPARSEBV;
  771. return false;
  772. }
  773. bool
  774. GlobOpt::AreTypeSetsIdentical(Js::EquivalentTypeSet * leftTypeSet, Js::EquivalentTypeSet * rightTypeSet)
  775. {
  776. return Js::EquivalentTypeSet::AreIdentical(leftTypeSet, rightTypeSet);
  777. }
  778. bool
  779. GlobOpt::IsSubsetOf(Js::EquivalentTypeSet * leftTypeSet, Js::EquivalentTypeSet * rightTypeSet)
  780. {
  781. return Js::EquivalentTypeSet::IsSubsetOf(leftTypeSet, rightTypeSet);
  782. }
  783. bool
  784. GlobOpt::CompareCurrentTypesWithExpectedTypes(JsTypeValueInfo *valueInfo, IR::PropertySymOpnd * propertySymOpnd)
  785. {
  786. bool isTypeDead = propertySymOpnd->IsTypeDead();
  787. if (valueInfo == nullptr || (valueInfo->GetJsType() == nullptr && valueInfo->GetJsTypeSet() == nullptr))
  788. {
  789. // No upstream types. Do a type check.
  790. return !isTypeDead;
  791. }
  792. if (!propertySymOpnd->HasEquivalentTypeSet() || propertySymOpnd->NeedsMonoCheck())
  793. {
  794. JITTypeHolder opndType = propertySymOpnd->GetType();
  795. if (valueInfo->GetJsType() != nullptr)
  796. {
  797. if (valueInfo->GetJsType() == propertySymOpnd->GetType())
  798. {
  799. return true;
  800. }
  801. if (propertySymOpnd->HasInitialType() && valueInfo->GetJsType() == propertySymOpnd->GetInitialType())
  802. {
  803. return !isTypeDead;
  804. }
  805. return false;
  806. }
  807. else
  808. {
  809. Assert(valueInfo->GetJsTypeSet());
  810. Js::EquivalentTypeSet *valueTypeSet = valueInfo->GetJsTypeSet();
  811. if (valueTypeSet->Contains(opndType))
  812. {
  813. return !isTypeDead;
  814. }
  815. if (propertySymOpnd->HasInitialType() && valueTypeSet->Contains(propertySymOpnd->GetInitialType()))
  816. {
  817. return !isTypeDead;
  818. }
  819. return false;
  820. }
  821. }
  822. else
  823. {
  824. Js::EquivalentTypeSet * opndTypeSet = propertySymOpnd->GetEquivalentTypeSet();
  825. if (valueInfo->GetJsType() != nullptr)
  826. {
  827. uint16 checkedTypeSetIndex;
  828. if (opndTypeSet->Contains(valueInfo->GetJsType(), &checkedTypeSetIndex))
  829. {
  830. return true;
  831. }
  832. return false;
  833. }
  834. else
  835. {
  836. if (IsSubsetOf(valueInfo->GetJsTypeSet(), opndTypeSet))
  837. {
  838. return true;
  839. }
  840. if (propertySymOpnd->IsMono() ?
  841. valueInfo->GetJsTypeSet()->Contains(propertySymOpnd->GetFirstEquivalentType()) :
  842. IsSubsetOf(opndTypeSet, valueInfo->GetJsTypeSet()))
  843. {
  844. return true;
  845. }
  846. return false;
  847. }
  848. }
  849. }
  850. bool
  851. GlobOpt::ProcessPropOpInTypeCheckSeq(IR::Instr* instr, IR::PropertySymOpnd *opnd)
  852. {
  853. return ProcessPropOpInTypeCheckSeq<true>(instr, opnd, this->currentBlock, false);
  854. }
  855. bool GlobOpt::CheckIfInstrInTypeCheckSeqEmitsTypeCheck(IR::Instr* instr, IR::PropertySymOpnd *opnd)
  856. {
  857. bool emitsTypeCheck;
  858. ProcessPropOpInTypeCheckSeq<false>(instr, opnd, this->currentBlock, false, &emitsTypeCheck);
  859. return emitsTypeCheck;
  860. }
  861. template<bool makeChanges>
  862. bool
  863. GlobOpt::ProcessPropOpInTypeCheckSeq(IR::Instr* instr, IR::PropertySymOpnd *opnd, BasicBlock* block, bool updateExistingValue, bool* emitsTypeCheckOut, bool* changesTypeValueOut, bool *isTypeCheckedOut)
  864. {
  865. // We no longer mark types as dead in the backward pass, so we should never see an instr with a dead type here
  866. // during the forward pass. For the time being we've retained the logic below to deal with dead types in case
  867. // we ever wanted to revert back to more aggressive type killing that we had before.
  868. Assert(!opnd->IsTypeDead());
  869. Assert(opnd->IsTypeCheckSeqCandidate());
  870. Assert(opnd->HasObjectTypeSym());
  871. bool isStore = opnd == instr->GetDst();
  872. bool isTypeDead = opnd->IsTypeDead();
  873. bool consumeType = makeChanges && !IsLoopPrePass();
  874. bool produceType = makeChanges && !isTypeDead;
  875. bool isSpecialized = false;
  876. bool emitsTypeCheck = false;
  877. bool addsProperty = false;
  878. if (block == nullptr)
  879. {
  880. block = this->currentBlock;
  881. }
  882. StackSym * typeSym = opnd->GetObjectTypeSym();
  883. #if DBG
  884. uint16 typeCheckSeqFlagsBefore;
  885. Value* valueBefore = nullptr;
  886. JsTypeValueInfo* valueInfoBefore = nullptr;
  887. if (!makeChanges)
  888. {
  889. typeCheckSeqFlagsBefore = opnd->GetTypeCheckSeqFlags();
  890. valueBefore = block->globOptData.FindObjectTypeValue(typeSym);
  891. if (valueBefore != nullptr)
  892. {
  893. Assert(valueBefore->GetValueInfo() != nullptr && valueBefore->GetValueInfo()->IsJsType());
  894. valueInfoBefore = valueBefore->GetValueInfo()->AsJsType();
  895. }
  896. }
  897. #endif
  898. Value *value = block->globOptData.FindObjectTypeValue(typeSym);
  899. JsTypeValueInfo* valueInfo = value != nullptr ? value->GetValueInfo()->AsJsType() : nullptr;
  900. if (consumeType && valueInfo != nullptr)
  901. {
  902. opnd->SetTypeAvailable(true);
  903. }
  904. bool doEquivTypeCheck = opnd->HasEquivalentTypeSet() && !opnd->NeedsMonoCheck();
  905. if (!doEquivTypeCheck)
  906. {
  907. AssertOrFailFast(!opnd->NeedsDepolymorphication());
  908. // We need a monomorphic type check here (e.g., final type opt, fixed field check on non-proto property).
  909. JITTypeHolder opndType = opnd->GetType();
  910. if (valueInfo == nullptr || (valueInfo->GetJsType() == nullptr && valueInfo->GetJsTypeSet() == nullptr))
  911. {
  912. // This is the initial type check.
  913. opnd->SetTypeAvailable(false);
  914. isSpecialized = !isTypeDead;
  915. emitsTypeCheck = isSpecialized;
  916. addsProperty = isStore && isSpecialized && opnd->HasInitialType();
  917. if (produceType)
  918. {
  919. SetObjectTypeFromTypeSym(typeSym, opndType, nullptr, block, updateExistingValue);
  920. }
  921. }
  922. else if (valueInfo->GetJsType() != nullptr)
  923. {
  924. // We have a monomorphic type check upstream. Check against initial/final type.
  925. const JITTypeHolder valueType(valueInfo->GetJsType());
  926. if (valueType == opndType)
  927. {
  928. // The type on this instruction matches the live value in the value table, so there is no need to
  929. // refresh the value table.
  930. isSpecialized = true;
  931. if (isTypeCheckedOut)
  932. {
  933. *isTypeCheckedOut = true;
  934. }
  935. if (consumeType)
  936. {
  937. opnd->SetTypeChecked(true);
  938. }
  939. }
  940. else if (opnd->HasInitialType() && valueType == opnd->GetInitialType())
  941. {
  942. // Checked type matches the initial type at this store.
  943. bool objectMayHaveAcquiredAdditionalProperties =
  944. block->globOptData.maybeWrittenTypeSyms &&
  945. block->globOptData.maybeWrittenTypeSyms->Test(typeSym->m_id);
  946. if (consumeType)
  947. {
  948. opnd->SetTypeChecked(!objectMayHaveAcquiredAdditionalProperties);
  949. opnd->SetInitialTypeChecked(!objectMayHaveAcquiredAdditionalProperties);
  950. }
  951. if (produceType)
  952. {
  953. SetObjectTypeFromTypeSym(typeSym, opndType, nullptr, block, updateExistingValue);
  954. }
  955. isSpecialized = !isTypeDead || !objectMayHaveAcquiredAdditionalProperties;
  956. emitsTypeCheck = isSpecialized && objectMayHaveAcquiredAdditionalProperties;
  957. addsProperty = isSpecialized;
  958. if (isTypeCheckedOut)
  959. {
  960. *isTypeCheckedOut = !objectMayHaveAcquiredAdditionalProperties;
  961. }
  962. }
  963. else
  964. {
  965. // This must be a type mismatch situation, because the value is available, but doesn't match either
  966. // the current type or the initial type. We will not optimize this instruction and we do not produce
  967. // a new type value here.
  968. isSpecialized = false;
  969. if (consumeType)
  970. {
  971. opnd->SetTypeMismatch(true);
  972. }
  973. }
  974. }
  975. else
  976. {
  977. // We have an equivalent type check upstream, but we require a particular type at this point. We
  978. // can't treat it as "checked", but we may benefit from checking for the required type.
  979. Assert(valueInfo->GetJsTypeSet());
  980. Js::EquivalentTypeSet *valueTypeSet = valueInfo->GetJsTypeSet();
  981. if (valueTypeSet->Contains(opndType))
  982. {
  983. // Required type is in the type set we've checked. Check for the required type here, and
  984. // note in the value info that we've narrowed down to this type. (But leave the type set in the
  985. // value info so it can be merged with the same type set on other paths.)
  986. isSpecialized = !isTypeDead;
  987. emitsTypeCheck = isSpecialized;
  988. if (produceType)
  989. {
  990. SetSingleTypeOnObjectTypeValue(value, opndType);
  991. }
  992. }
  993. else if (opnd->HasInitialType() && valueTypeSet->Contains(opnd->GetInitialType()))
  994. {
  995. // Required initial type is in the type set we've checked. Check for the initial type here, and
  996. // note in the value info that we've narrowed down to this type. (But leave the type set in the
  997. // value info so it can be merged with the same type set on other paths.)
  998. isSpecialized = !isTypeDead;
  999. emitsTypeCheck = isSpecialized;
  1000. addsProperty = isSpecialized;
  1001. if (produceType)
  1002. {
  1003. SetSingleTypeOnObjectTypeValue(value, opndType);
  1004. }
  1005. }
  1006. else
  1007. {
  1008. // This must be a type mismatch situation, because the value is available, but doesn't match either
  1009. // the current type or the initial type. We will not optimize this instruction and we do not produce
  1010. // a new type value here.
  1011. isSpecialized = false;
  1012. if (consumeType)
  1013. {
  1014. opnd->SetTypeMismatch(true);
  1015. }
  1016. }
  1017. }
  1018. }
  1019. else
  1020. {
  1021. Assert(!opnd->NeedsMonoCheck());
  1022. Js::EquivalentTypeSet * opndTypeSet = opnd->GetEquivalentTypeSet();
  1023. uint16 checkedTypeSetIndex = (uint16)-1;
  1024. if (opnd->NeedsDepolymorphication())
  1025. {
  1026. // The opnd's type set (opndTypeSet) is non-equivalent. Test all the types coming from the valueInfo.
  1027. // If all of them are contained in opndTypeSet, and all of them have the same slot index in opnd's
  1028. // objtypespecfldinfo, then we can use that slot index and treat the set as equivalent.
  1029. // (Also test whether all types do/don't use aux slots.)
  1030. uint16 slotIndex = Js::Constants::NoSlot;
  1031. bool auxSlot = false;
  1032. // Do this work only if there is an upstream type value. We don't attempt to do a type check based on
  1033. // a non-equivalent set.
  1034. if (valueInfo != nullptr)
  1035. {
  1036. if (valueInfo->GetJsType() != nullptr)
  1037. {
  1038. opnd->TryDepolymorphication(valueInfo->GetJsType(), Js::Constants::NoSlot, false, &slotIndex, &auxSlot, &checkedTypeSetIndex);
  1039. }
  1040. else if (valueInfo->GetJsTypeSet() != nullptr)
  1041. {
  1042. Js::EquivalentTypeSet *typeSet = valueInfo->GetJsTypeSet();
  1043. for (uint16 i = 0; i < typeSet->GetCount(); i++)
  1044. {
  1045. opnd->TryDepolymorphication(typeSet->GetType(i), slotIndex, auxSlot, &slotIndex, &auxSlot);
  1046. if (slotIndex == Js::Constants::NoSlot)
  1047. {
  1048. // Indicates failure/mismatch. We're done.
  1049. break;
  1050. }
  1051. }
  1052. }
  1053. }
  1054. if (slotIndex == Js::Constants::NoSlot)
  1055. {
  1056. // Indicates failure/mismatch
  1057. isSpecialized = false;
  1058. if (consumeType)
  1059. {
  1060. opnd->SetTypeMismatch(true);
  1061. }
  1062. }
  1063. else
  1064. {
  1065. // Indicates we can optimize, as all upstream types are equivalent here.
  1066. opnd->SetSlotIndex(slotIndex);
  1067. opnd->SetUsesAuxSlot(auxSlot);
  1068. opnd->GetObjTypeSpecInfo()->SetSlotIndex(slotIndex);
  1069. opnd->GetObjTypeSpecInfo()->SetUsesAuxSlot(auxSlot);
  1070. isSpecialized = true;
  1071. if (isTypeCheckedOut)
  1072. {
  1073. *isTypeCheckedOut = true;
  1074. }
  1075. if (consumeType)
  1076. {
  1077. opnd->SetTypeChecked(true);
  1078. }
  1079. if (checkedTypeSetIndex != (uint16)-1)
  1080. {
  1081. opnd->SetCheckedTypeSetIndex(checkedTypeSetIndex);
  1082. }
  1083. }
  1084. }
  1085. else if (valueInfo == nullptr || (valueInfo->GetJsType() == nullptr && valueInfo->GetJsTypeSet() == nullptr))
  1086. {
  1087. // If we don't have a value for the type we will have to emit a type check and we produce a new type value here.
  1088. if (produceType)
  1089. {
  1090. if (opnd->IsMono())
  1091. {
  1092. SetObjectTypeFromTypeSym(typeSym, opnd->GetFirstEquivalentType(), nullptr, block, updateExistingValue);
  1093. }
  1094. else
  1095. {
  1096. SetObjectTypeFromTypeSym(typeSym, nullptr, opndTypeSet, block, updateExistingValue);
  1097. }
  1098. }
  1099. isSpecialized = !isTypeDead;
  1100. emitsTypeCheck = isSpecialized;
  1101. }
  1102. else if (valueInfo->GetJsType() != nullptr ?
  1103. opndTypeSet->Contains(valueInfo->GetJsType(), &checkedTypeSetIndex) :
  1104. IsSubsetOf(valueInfo->GetJsTypeSet(), opndTypeSet))
  1105. {
  1106. // All the types in the value info are contained in the set required by this access,
  1107. // meaning that they're equivalent to the opnd's type set.
  1108. // We won't have a type check, and we don't need to touch the type value.
  1109. isSpecialized = true;
  1110. if (isTypeCheckedOut)
  1111. {
  1112. *isTypeCheckedOut = true;
  1113. }
  1114. if (consumeType)
  1115. {
  1116. opnd->SetTypeChecked(true);
  1117. }
  1118. if (checkedTypeSetIndex != (uint16)-1)
  1119. {
  1120. opnd->SetCheckedTypeSetIndex(checkedTypeSetIndex);
  1121. }
  1122. }
  1123. else if (valueInfo->GetJsTypeSet() &&
  1124. (opnd->IsMono() ?
  1125. valueInfo->GetJsTypeSet()->Contains(opnd->GetFirstEquivalentType()) :
  1126. IsSubsetOf(opndTypeSet, valueInfo->GetJsTypeSet())
  1127. )
  1128. )
  1129. {
  1130. // We have an equivalent type check upstream, but we require a tighter type check at this point.
  1131. // We can't treat the operand as "checked", but check for equivalence with the tighter set and update the
  1132. // value info.
  1133. if (produceType)
  1134. {
  1135. if (opnd->IsMono())
  1136. {
  1137. SetObjectTypeFromTypeSym(typeSym, opnd->GetFirstEquivalentType(), nullptr, block, updateExistingValue);
  1138. }
  1139. else
  1140. {
  1141. SetObjectTypeFromTypeSym(typeSym, nullptr, opndTypeSet, block, updateExistingValue);
  1142. }
  1143. }
  1144. isSpecialized = !isTypeDead;
  1145. emitsTypeCheck = isSpecialized;
  1146. }
  1147. else
  1148. {
  1149. // This must be a type mismatch situation, because the value is available, but doesn't match either
  1150. // the current type or the initial type. We will not optimize this instruction and we do not produce
  1151. // a new type value here.
  1152. isSpecialized = false;
  1153. if (consumeType)
  1154. {
  1155. opnd->SetTypeMismatch(true);
  1156. }
  1157. }
  1158. }
  1159. Assert(isSpecialized || (!emitsTypeCheck && !addsProperty));
  1160. if (consumeType && opnd->MayNeedWriteGuardProtection())
  1161. {
  1162. Assert(!isStore);
  1163. PropertySym *propertySym = opnd->m_sym->AsPropertySym();
  1164. Assert(propertySym->m_writeGuardSym);
  1165. opnd->SetWriteGuardChecked(!!block->globOptData.liveFields->Test(propertySym->m_writeGuardSym->m_id));
  1166. }
  1167. // Even specialized property adds must kill all types for other property adds. That's because any other object sym
  1168. // may, in fact, be an alias of the instance whose type is being modified here. (see Windows Blue Bug 541876)
  1169. if (makeChanges && addsProperty)
  1170. {
  1171. Assert(isStore && isSpecialized);
  1172. Assert(this->objectTypeSyms != nullptr);
  1173. Assert(this->objectTypeSyms->Test(typeSym->m_id));
  1174. if (block->globOptData.maybeWrittenTypeSyms == nullptr)
  1175. {
  1176. block->globOptData.maybeWrittenTypeSyms = JitAnew(this->alloc, BVSparse<JitArenaAllocator>, this->alloc);
  1177. }
  1178. this->objectTypeSyms->Clear(typeSym->m_id);
  1179. block->globOptData.maybeWrittenTypeSyms->Or(this->objectTypeSyms);
  1180. this->objectTypeSyms->Set(typeSym->m_id);
  1181. }
  1182. if (produceType && emitsTypeCheck && opnd->IsMono())
  1183. {
  1184. // Consider (ObjTypeSpec): Represent maybeWrittenTypeSyms as a flag on value info of the type sym.
  1185. if (block->globOptData.maybeWrittenTypeSyms != nullptr)
  1186. {
  1187. // We're doing a type check here, so objtypespec of property adds is safe for this type
  1188. // from this point forward.
  1189. block->globOptData.maybeWrittenTypeSyms->Clear(typeSym->m_id);
  1190. }
  1191. }
  1192. // Consider (ObjTypeSpec): Enable setting write guards live on instructions hoisted out of loops. Note that produceType
  1193. // is false if the type values on loop back edges don't match (see earlier comments).
  1194. // This means that hoisted instructions won't set write guards live if the type changes in the loop, even if
  1195. // the corresponding properties have not been written inside the loop. This may result in some unnecessary type
  1196. // checks and bailouts inside the loop. To enable this, we would need to verify the write guards are still live
  1197. // on the back edge (much like we're doing for types above).
  1198. // Consider (ObjTypeSpec): Support polymorphic write guards as well. We can't currently distinguish between mono and
  1199. // poly write guards, and a type check can only protect operations matching with respect to polymorphism (see
  1200. // BackwardPass::TrackObjTypeSpecProperties for details), so for now we only target monomorphic operations.
  1201. if (produceType && emitsTypeCheck && opnd->IsMono())
  1202. {
  1203. // If the type check we'll emit here protects some property operations that require a write guard (i.e.
  1204. // they must do an extra type check and property guard check, if they have been written to in this
  1205. // function), let's mark the write guards as live here, so we can accurately track if their properties
  1206. // have been written to. Make sure we only set those that we'll actually guard, i.e. those that match
  1207. // with respect to polymorphism.
  1208. if (opnd->GetWriteGuards() != nullptr)
  1209. {
  1210. block->globOptData.liveFields->Or(opnd->GetWriteGuards());
  1211. }
  1212. }
  1213. if (makeChanges && isTypeDead)
  1214. {
  1215. this->KillObjectType(opnd->GetObjectSym(), block->globOptData.liveFields);
  1216. }
  1217. #if DBG
  1218. if (!makeChanges)
  1219. {
  1220. uint16 typeCheckSeqFlagsAfter = opnd->GetTypeCheckSeqFlags();
  1221. Assert(typeCheckSeqFlagsBefore == typeCheckSeqFlagsAfter);
  1222. Value* valueAfter = block->globOptData.FindObjectTypeValue(typeSym);
  1223. Assert(valueBefore == valueAfter);
  1224. if (valueAfter != nullptr)
  1225. {
  1226. Assert(valueBefore != nullptr);
  1227. Assert(valueAfter->GetValueInfo() != nullptr && valueAfter->GetValueInfo()->IsJsType());
  1228. JsTypeValueInfo* valueInfoAfter = valueAfter->GetValueInfo()->AsJsType();
  1229. Assert(valueInfoBefore == valueInfoAfter);
  1230. Assert(valueInfoBefore->GetJsType() == valueInfoAfter->GetJsType());
  1231. Assert(valueInfoBefore->GetJsTypeSet() == valueInfoAfter->GetJsTypeSet());
  1232. }
  1233. }
  1234. #endif
  1235. if (emitsTypeCheckOut != nullptr)
  1236. {
  1237. *emitsTypeCheckOut = emitsTypeCheck;
  1238. }
  1239. if (changesTypeValueOut != nullptr)
  1240. {
  1241. *changesTypeValueOut = isSpecialized && (emitsTypeCheck || addsProperty);
  1242. }
  1243. return isSpecialized;
  1244. }
  1245. void
  1246. GlobOpt::OptNewScObject(IR::Instr** instrPtr, Value* srcVal)
  1247. {
  1248. IR::Instr *&instr = *instrPtr;
  1249. if (!instr->IsNewScObjectInstr() || IsLoopPrePass() || !this->DoFieldRefOpts() || PHASE_OFF(Js::ObjTypeSpecNewObjPhase, this->func))
  1250. {
  1251. return;
  1252. }
  1253. bool isCtorInlined = instr->m_opcode == Js::OpCode::NewScObjectNoCtor;
  1254. const JITTimeConstructorCache * ctorCache = instr->IsProfiledInstr() ?
  1255. instr->m_func->GetConstructorCache(static_cast<Js::ProfileId>(instr->AsProfiledInstr()->u.profileId)) : nullptr;
  1256. // TODO: OOP JIT, enable assert
  1257. //Assert(ctorCache == nullptr || srcVal->GetValueInfo()->IsVarConstant() && Js::JavascriptFunction::Is(srcVal->GetValueInfo()->AsVarConstant()->VarValue()));
  1258. Assert(ctorCache == nullptr || !ctorCache->IsTypeFinal() || ctorCache->CtorHasNoExplicitReturnValue());
  1259. if (ctorCache != nullptr && !ctorCache->SkipNewScObject() && (isCtorInlined || ctorCache->IsTypeFinal()))
  1260. {
  1261. GenerateBailAtOperation(instrPtr, IR::BailOutFailedCtorGuardCheck);
  1262. }
  1263. }
  1264. void
  1265. GlobOpt::ValueNumberObjectType(IR::Opnd *dstOpnd, IR::Instr *instr)
  1266. {
  1267. if (!dstOpnd->IsRegOpnd())
  1268. {
  1269. return;
  1270. }
  1271. if (dstOpnd->AsRegOpnd()->m_sym->IsTypeSpec())
  1272. {
  1273. return;
  1274. }
  1275. if (instr->IsNewScObjectInstr())
  1276. {
  1277. // If we have a NewScObj* for which we have a valid constructor cache we know what type the created object will have.
  1278. // Let's produce the type value accordingly so we don't insert a type check and bailout in the constructor and
  1279. // potentially further downstream.
  1280. Assert(!PHASE_OFF(Js::ObjTypeSpecNewObjPhase, this->func) || !instr->HasBailOutInfo());
  1281. if (instr->HasBailOutInfo())
  1282. {
  1283. Assert(instr->IsProfiledInstr());
  1284. Assert(instr->GetBailOutKind() == IR::BailOutFailedCtorGuardCheck);
  1285. bool isCtorInlined = instr->m_opcode == Js::OpCode::NewScObjectNoCtor;
  1286. JITTimeConstructorCache * ctorCache = instr->m_func->GetConstructorCache(static_cast<Js::ProfileId>(instr->AsProfiledInstr()->u.profileId));
  1287. Assert(ctorCache != nullptr && (isCtorInlined || ctorCache->IsTypeFinal()));
  1288. StackSym* objSym = dstOpnd->AsRegOpnd()->m_sym;
  1289. StackSym* dstTypeSym = EnsureObjectTypeSym(objSym);
  1290. Assert(this->currentBlock->globOptData.FindValue(dstTypeSym) == nullptr);
  1291. SetObjectTypeFromTypeSym(dstTypeSym, ctorCache->GetType(), nullptr);
  1292. }
  1293. }
  1294. else
  1295. {
  1296. // If the dst opnd is a reg that has a type sym associated with it, then we are either killing
  1297. // the type's existing value or (in the case of a reg copy) assigning it the value of
  1298. // the src's type sym (if any). If the dst doesn't have a type sym, but the src does, let's
  1299. // give dst a new type sym and transfer the value.
  1300. Value *newValue = nullptr;
  1301. IR::Opnd * srcOpnd = instr->GetSrc1();
  1302. if (instr->m_opcode == Js::OpCode::Ld_A && srcOpnd->IsRegOpnd() &&
  1303. !srcOpnd->AsRegOpnd()->m_sym->IsTypeSpec() && srcOpnd->AsRegOpnd()->m_sym->HasObjectTypeSym())
  1304. {
  1305. StackSym *srcTypeSym = srcOpnd->AsRegOpnd()->m_sym->GetObjectTypeSym();
  1306. newValue = this->currentBlock->globOptData.FindValue(srcTypeSym);
  1307. }
  1308. if (newValue == nullptr)
  1309. {
  1310. if (dstOpnd->AsRegOpnd()->m_sym->HasObjectTypeSym())
  1311. {
  1312. StackSym * typeSym = dstOpnd->AsRegOpnd()->m_sym->GetObjectTypeSym();
  1313. this->currentBlock->globOptData.ClearSymValue(typeSym);
  1314. }
  1315. }
  1316. else
  1317. {
  1318. Assert(newValue->GetValueInfo()->IsJsType());
  1319. StackSym * typeSym;
  1320. if (!dstOpnd->AsRegOpnd()->m_sym->HasObjectTypeSym())
  1321. {
  1322. typeSym = nullptr;
  1323. }
  1324. typeSym = EnsureObjectTypeSym(dstOpnd->AsRegOpnd()->m_sym);
  1325. this->currentBlock->globOptData.SetValue(newValue, typeSym);
  1326. }
  1327. }
  1328. }
  1329. IR::Instr *
  1330. GlobOpt::SetTypeCheckBailOut(IR::Opnd *opnd, IR::Instr *instr, BailOutInfo *bailOutInfo)
  1331. {
  1332. if (this->IsLoopPrePass() || !opnd->IsSymOpnd())
  1333. {
  1334. return instr;
  1335. }
  1336. if (!opnd->AsSymOpnd()->IsPropertySymOpnd())
  1337. {
  1338. return instr;
  1339. }
  1340. IR::PropertySymOpnd * propertySymOpnd = opnd->AsPropertySymOpnd();
  1341. AssertMsg(propertySymOpnd->TypeCheckSeqBitsSetOnlyIfCandidate(), "Property sym operand optimized despite not being a candidate?");
  1342. AssertMsg(bailOutInfo == nullptr || !instr->HasBailOutInfo(), "Why are we adding new bailout info to an instruction that already has it?");
  1343. auto HandleBailout = [&](IR::BailOutKind bailOutKind)->void {
  1344. // At this point, we have a cached type that is live downstream or the type check is required
  1345. // for a fixed field load. If we can't do away with the type check, then we're going to need bailout,
  1346. // so lets add bailout info if we don't already have it.
  1347. if (!instr->HasBailOutInfo())
  1348. {
  1349. if (bailOutInfo)
  1350. {
  1351. instr = instr->ConvertToBailOutInstr(bailOutInfo, bailOutKind);
  1352. }
  1353. else
  1354. {
  1355. GenerateBailAtOperation(&instr, bailOutKind);
  1356. BailOutInfo *bailOutInfo = instr->GetBailOutInfo();
  1357. // Consider (ObjTypeSpec): If we're checking a fixed field here the bailout could be due to polymorphism or
  1358. // due to a fixed field turning non-fixed. Consider distinguishing between the two.
  1359. bailOutInfo->polymorphicCacheIndex = propertySymOpnd->m_inlineCacheIndex;
  1360. }
  1361. }
  1362. else if (instr->GetBailOutKind() == IR::BailOutMarkTempObject)
  1363. {
  1364. Assert(!bailOutInfo);
  1365. Assert(instr->GetBailOutInfo()->polymorphicCacheIndex == -1);
  1366. instr->SetBailOutKind(bailOutKind | IR::BailOutMarkTempObject);
  1367. instr->GetBailOutInfo()->polymorphicCacheIndex = propertySymOpnd->m_inlineCacheIndex;
  1368. }
  1369. else
  1370. {
  1371. Assert(bailOutKind == instr->GetBailOutKind());
  1372. }
  1373. };
  1374. bool isTypeCheckProtected;
  1375. IR::BailOutKind bailOutKind;
  1376. if (GlobOpt::NeedsTypeCheckBailOut(instr, propertySymOpnd, opnd == instr->GetDst(), &isTypeCheckProtected, &bailOutKind))
  1377. {
  1378. HandleBailout(bailOutKind);
  1379. }
  1380. else
  1381. {
  1382. if (instr->m_opcode == Js::OpCode::LdMethodFromFlags)
  1383. {
  1384. // If LdMethodFromFlags is hoisted to the top of the loop, we should share the same bailout Info.
  1385. // We don't need to do anything for LdMethodFromFlags that cannot be field hoisted.
  1386. HandleBailout(IR::BailOutFailedInlineTypeCheck);
  1387. }
  1388. else if (instr->HasBailOutInfo())
  1389. {
  1390. // If we already have a bailout info, but don't actually need it, let's remove it. This can happen if
  1391. // a CheckFixedFld added by the inliner (with bailout info) determined that the object's type has
  1392. // been checked upstream and no bailout is necessary here.
  1393. if (instr->m_opcode == Js::OpCode::CheckFixedFld)
  1394. {
  1395. AssertMsg(!PHASE_OFF(Js::FixedMethodsPhase, instr->m_func) ||
  1396. !PHASE_OFF(Js::UseFixedDataPropsPhase, instr->m_func), "CheckFixedFld with fixed method/data phase disabled?");
  1397. Assert(isTypeCheckProtected);
  1398. AssertMsg(instr->GetBailOutKind() == IR::BailOutFailedFixedFieldTypeCheck || instr->GetBailOutKind() == IR::BailOutFailedEquivalentFixedFieldTypeCheck,
  1399. "Only BailOutFailed[Equivalent]FixedFieldTypeCheck can be safely removed. Why does CheckFixedFld carry a different bailout kind?.");
  1400. instr->ClearBailOutInfo();
  1401. }
  1402. else if (propertySymOpnd->MayNeedTypeCheckProtection() && propertySymOpnd->IsTypeCheckProtected())
  1403. {
  1404. // Both the type and (if necessary) the proto object have been checked.
  1405. // We're doing a direct slot access. No possibility of bailout here (not even implicit call).
  1406. Assert(instr->GetBailOutKind() == IR::BailOutMarkTempObject);
  1407. instr->ClearBailOutInfo();
  1408. }
  1409. }
  1410. }
  1411. return instr;
  1412. }
  1413. void
  1414. GlobOpt::SetSingleTypeOnObjectTypeValue(Value* value, const JITTypeHolder type)
  1415. {
  1416. UpdateObjectTypeValue(value, type, true, nullptr, false);
  1417. }
  1418. void
  1419. GlobOpt::SetTypeSetOnObjectTypeValue(Value* value, Js::EquivalentTypeSet* typeSet)
  1420. {
  1421. UpdateObjectTypeValue(value, nullptr, false, typeSet, true);
  1422. }
  1423. void
  1424. GlobOpt::UpdateObjectTypeValue(Value* value, const JITTypeHolder type, bool setType, Js::EquivalentTypeSet* typeSet, bool setTypeSet)
  1425. {
  1426. Assert(value->GetValueInfo() != nullptr && value->GetValueInfo()->IsJsType());
  1427. JsTypeValueInfo* valueInfo = value->GetValueInfo()->AsJsType();
  1428. if (valueInfo->GetIsShared())
  1429. {
  1430. valueInfo = valueInfo->Copy(this->alloc);
  1431. value->SetValueInfo(valueInfo);
  1432. }
  1433. if (setType)
  1434. {
  1435. valueInfo->SetJsType(type);
  1436. }
  1437. if (setTypeSet)
  1438. {
  1439. valueInfo->SetJsTypeSet(typeSet);
  1440. }
  1441. }
  1442. void
  1443. GlobOpt::SetObjectTypeFromTypeSym(StackSym *typeSym, Value* value, BasicBlock* block)
  1444. {
  1445. Assert(typeSym != nullptr);
  1446. Assert(value != nullptr);
  1447. Assert(value->GetValueInfo() != nullptr && value->GetValueInfo()->IsJsType());
  1448. SymID typeSymId = typeSym->m_id;
  1449. if (block == nullptr)
  1450. {
  1451. block = this->currentBlock;
  1452. }
  1453. block->globOptData.SetValue(value, typeSym);
  1454. block->globOptData.liveFields->Set(typeSymId);
  1455. }
  1456. void
  1457. GlobOpt::SetObjectTypeFromTypeSym(StackSym *typeSym, const JITTypeHolder type, Js::EquivalentTypeSet * typeSet, BasicBlock* block, bool updateExistingValue)
  1458. {
  1459. if (block == nullptr)
  1460. {
  1461. block = this->currentBlock;
  1462. }
  1463. SetObjectTypeFromTypeSym(typeSym, type, typeSet, &block->globOptData, updateExistingValue);
  1464. }
  1465. void
  1466. GlobOpt::SetObjectTypeFromTypeSym(StackSym *typeSym, const JITTypeHolder type, Js::EquivalentTypeSet * typeSet, GlobOptBlockData *blockData, bool updateExistingValue)
  1467. {
  1468. Assert(typeSym != nullptr);
  1469. SymID typeSymId = typeSym->m_id;
  1470. if (blockData == nullptr)
  1471. {
  1472. blockData = &this->currentBlock->globOptData;
  1473. }
  1474. if (updateExistingValue)
  1475. {
  1476. Value* value = blockData->FindValueFromMapDirect(typeSymId);
  1477. // If we're trying to update an existing value, the value better exist. We only do this when updating a generic
  1478. // value created during loop pre-pass for field hoisting, so we expect the value info to still be blank.
  1479. Assert(value != nullptr && value->GetValueInfo() != nullptr && value->GetValueInfo()->IsJsType());
  1480. JsTypeValueInfo* valueInfo = value->GetValueInfo()->AsJsType();
  1481. Assert(valueInfo->GetJsType() == nullptr && valueInfo->GetJsTypeSet() == nullptr);
  1482. UpdateObjectTypeValue(value, type, true, typeSet, true);
  1483. }
  1484. else
  1485. {
  1486. JsTypeValueInfo* valueInfo = JsTypeValueInfo::New(this->alloc, type, typeSet);
  1487. this->SetSymStoreDirect(valueInfo, typeSym);
  1488. Value* value = NewValue(valueInfo);
  1489. blockData->SetValue(value, typeSym);
  1490. }
  1491. blockData->liveFields->Set(typeSymId);
  1492. }
  1493. void
  1494. GlobOpt::KillObjectType(StackSym* objectSym, BVSparse<JitArenaAllocator>* liveFields)
  1495. {
  1496. if (objectSym->IsTypeSpec())
  1497. {
  1498. objectSym = objectSym->GetVarEquivSym(this->func);
  1499. }
  1500. Assert(objectSym);
  1501. // We may be conservatively attempting to kill type syms from object syms that don't actually
  1502. // participate in object type specialization and hence don't actually have type syms (yet).
  1503. if (!objectSym->HasObjectTypeSym())
  1504. {
  1505. return;
  1506. }
  1507. if (liveFields == nullptr)
  1508. {
  1509. liveFields = this->currentBlock->globOptData.liveFields;
  1510. }
  1511. liveFields->Clear(objectSym->GetObjectTypeSym()->m_id);
  1512. }
  1513. void
  1514. GlobOpt::KillAllObjectTypes(BVSparse<JitArenaAllocator>* liveFields)
  1515. {
  1516. if (this->objectTypeSyms && liveFields)
  1517. {
  1518. liveFields->Minus(this->objectTypeSyms);
  1519. }
  1520. }
  1521. void
  1522. GlobOpt::EndFieldLifetime(IR::SymOpnd *symOpnd)
  1523. {
  1524. this->currentBlock->globOptData.liveFields->Clear(symOpnd->m_sym->m_id);
  1525. }
  1526. PropertySym *
  1527. GlobOpt::CopyPropPropertySymObj(IR::SymOpnd *symOpnd, IR::Instr *instr)
  1528. {
  1529. Assert(symOpnd->m_sym->IsPropertySym());
  1530. PropertySym *propertySym = symOpnd->m_sym->AsPropertySym();
  1531. StackSym *objSym = propertySym->m_stackSym;
  1532. Value * val = this->currentBlock->globOptData.FindValue(objSym);
  1533. if (val && !PHASE_OFF(Js::ObjPtrCopyPropPhase, this->func))
  1534. {
  1535. StackSym *copySym = this->currentBlock->globOptData.GetCopyPropSym(objSym, val);
  1536. if (copySym != nullptr)
  1537. {
  1538. PropertySym *newProp = PropertySym::FindOrCreate(
  1539. copySym->m_id, propertySym->m_propertyId, propertySym->GetPropertyIdIndex(), propertySym->GetInlineCacheIndex(), propertySym->m_fieldKind, this->func);
  1540. if (!this->IsLoopPrePass() || SafeToCopyPropInPrepass(objSym, copySym, val))
  1541. {
  1542. #if DBG_DUMP
  1543. if (Js::Configuration::Global.flags.Trace.IsEnabled(Js::GlobOptPhase, this->func->GetSourceContextId(), this->func->GetLocalFunctionId()))
  1544. {
  1545. Output::Print(_u("TRACE: "));
  1546. symOpnd->Dump();
  1547. Output::Print(_u(" : "));
  1548. Output::Print(_u("Copy prop obj ptr s%d, new property: "), copySym->m_id);
  1549. newProp->Dump();
  1550. Output::Print(_u("\n"));
  1551. }
  1552. #endif
  1553. // Copy prop
  1554. this->CaptureByteCodeSymUses(instr);
  1555. // If the old sym was part of an object type spec type check sequence,
  1556. // let's make sure the new one is prepped for it as well.
  1557. if (IsPropertySymPreparedForTypeCheckSeq(propertySym))
  1558. {
  1559. PreparePropertySymForTypeCheckSeq(newProp);
  1560. }
  1561. symOpnd->m_sym = newProp;
  1562. symOpnd->SetIsJITOptimizedReg(true);
  1563. if (symOpnd->IsPropertySymOpnd())
  1564. {
  1565. IR::PropertySymOpnd *propertySymOpnd = symOpnd->AsPropertySymOpnd();
  1566. if (propertySymOpnd->IsTypeCheckSeqCandidate())
  1567. {
  1568. // If the new pointer sym's expected type(s) don't match those in the inline-cache-based data for this access,
  1569. // we probably have a mismatch and can't safely objtypespec. If the saved objtypespecfldinfo isn't right for
  1570. // the new type, then we'll do an incorrect property access.
  1571. StackSym * newTypeSym = copySym->GetObjectTypeSym();
  1572. Value * newValue = currentBlock->globOptData.FindObjectTypeValueNoLivenessCheck(newTypeSym);
  1573. JsTypeValueInfo * newValueInfo = newValue ? newValue->GetValueInfo()->AsJsType() : nullptr;
  1574. bool shouldOptimize = CompareCurrentTypesWithExpectedTypes(newValueInfo, propertySymOpnd);
  1575. if (!shouldOptimize)
  1576. {
  1577. propertySymOpnd->SetTypeCheckSeqCandidate(false);
  1578. }
  1579. }
  1580. // This is no longer strictly necessary, since we don't set the type dead bits in the initial
  1581. // backward pass, but let's keep it around for now in case we choose to revert to the old model.
  1582. propertySymOpnd->SetTypeDeadIfTypeCheckSeqCandidate(false);
  1583. }
  1584. if (this->IsLoopPrePass())
  1585. {
  1586. this->prePassCopyPropSym->Set(copySym->m_id);
  1587. }
  1588. }
  1589. propertySym = newProp;
  1590. if(instr->GetDst() && symOpnd->IsEqual(instr->GetDst()))
  1591. {
  1592. // Make sure any stack sym uses in the new destination property sym are unspecialized
  1593. instr = ToVarUses(instr, symOpnd, true, nullptr);
  1594. }
  1595. }
  1596. }
  1597. return propertySym;
  1598. }
  1599. void
  1600. GlobOpt::UpdateObjPtrValueType(IR::Opnd * opnd, IR::Instr * instr)
  1601. {
  1602. if (!opnd->IsSymOpnd() || !opnd->AsSymOpnd()->IsPropertySymOpnd())
  1603. {
  1604. return;
  1605. }
  1606. if (!instr->HasTypeCheckBailOut())
  1607. {
  1608. // No type check bailout, we didn't check that type of the object pointer.
  1609. return;
  1610. }
  1611. // Only check that fixed field should have type check bailout in loop prepass.
  1612. Assert(instr->m_opcode == Js::OpCode::CheckFixedFld || !this->IsLoopPrePass());
  1613. if (instr->m_opcode != Js::OpCode::CheckFixedFld)
  1614. {
  1615. // DeadStore pass may remove type check bailout, except CheckFixedFld which always needs
  1616. // type check bailout. So we can only change the type for CheckFixedFld.
  1617. // Consider: See if we can expand that in the future.
  1618. return;
  1619. }
  1620. IR::PropertySymOpnd * propertySymOpnd = opnd->AsPropertySymOpnd();
  1621. StackSym * objectSym = propertySymOpnd->GetObjectSym();
  1622. Value * objVal = this->currentBlock->globOptData.FindValue(objectSym);
  1623. if (!objVal)
  1624. {
  1625. return;
  1626. }
  1627. ValueType objValueType = objVal->GetValueInfo()->Type();
  1628. if (objValueType.IsDefinite())
  1629. {
  1630. return;
  1631. }
  1632. ValueInfo *objValueInfo = objVal->GetValueInfo();
  1633. // It is possible for a valueInfo to be not definite and still have a byteCodeConstant as symStore, this is because we conservatively copy valueInfo in prePass
  1634. if (objValueInfo->GetSymStore() && objValueInfo->GetSymStore()->IsStackSym() && objValueInfo->GetSymStore()->AsStackSym()->IsFromByteCodeConstantTable())
  1635. {
  1636. return;
  1637. }
  1638. // Verify that the types we're checking for here have been locked so that the type ID's can't be changed
  1639. // without changing the type.
  1640. if (!propertySymOpnd->HasObjectTypeSym())
  1641. {
  1642. return;
  1643. }
  1644. StackSym * typeSym = propertySymOpnd->GetObjectTypeSym();
  1645. Assert(typeSym);
  1646. Value * typeValue = currentBlock->globOptData.FindObjectTypeValue(typeSym);
  1647. if (!typeValue)
  1648. {
  1649. return;
  1650. }
  1651. JsTypeValueInfo * typeValueInfo = typeValue->GetValueInfo()->AsJsType();
  1652. JITTypeHolder type = typeValueInfo->GetJsType();
  1653. if (type != nullptr)
  1654. {
  1655. if (Js::DynamicType::Is(type->GetTypeId()) &&
  1656. !type->GetTypeHandler()->IsLocked())
  1657. {
  1658. return;
  1659. }
  1660. }
  1661. else
  1662. {
  1663. Js::EquivalentTypeSet * typeSet = typeValueInfo->GetJsTypeSet();
  1664. Assert(typeSet);
  1665. for (uint16 i = 0; i < typeSet->GetCount(); i++)
  1666. {
  1667. type = typeSet->GetType(i);
  1668. if (Js::DynamicType::Is(type->GetTypeId()) &&
  1669. !type->GetTypeHandler()->IsLocked())
  1670. {
  1671. return;
  1672. }
  1673. }
  1674. }
  1675. AnalysisAssert(type != nullptr);
  1676. Js::TypeId typeId = type->GetTypeId();
  1677. // Passing false for useVirtual as we would never have a virtual typed array hitting this code path
  1678. ValueType newValueType = ValueType::FromTypeId(typeId, false);
  1679. if (newValueType == ValueType::Uninitialized)
  1680. {
  1681. switch (typeId)
  1682. {
  1683. default:
  1684. // Can't mark as definite object because it may actually be object-with-array.
  1685. // Consider: a value type that subsumes object, array, and object-with-array.
  1686. break;
  1687. case Js::TypeIds_NativeIntArray:
  1688. case Js::TypeIds_NativeFloatArray:
  1689. // Do not mark these values as definite to protect against array conversion
  1690. break;
  1691. case Js::TypeIds_Array:
  1692. // Because array can change type id, we can only make it definite if we are doing array check hoist
  1693. // so that implicit call will be installed between the array checks.
  1694. if (!DoArrayCheckHoist() ||
  1695. (currentBlock->loop
  1696. ? !this->ImplicitCallFlagsAllowOpts(currentBlock->loop)
  1697. : !this->ImplicitCallFlagsAllowOpts(this->func)))
  1698. {
  1699. break;
  1700. }
  1701. if (objValueType.IsLikelyArrayOrObjectWithArray())
  1702. {
  1703. // If we have likely no missing values before, keep the likely, because, we haven't proven that
  1704. // the array really has no missing values
  1705. if (!objValueType.HasNoMissingValues())
  1706. {
  1707. newValueType = ValueType::GetObject(ObjectType::Array).SetArrayTypeId(typeId);
  1708. }
  1709. }
  1710. else
  1711. {
  1712. newValueType = ValueType::GetObject(ObjectType::Array).SetArrayTypeId(typeId);
  1713. }
  1714. break;
  1715. }
  1716. }
  1717. if (newValueType != ValueType::Uninitialized)
  1718. {
  1719. ChangeValueType(currentBlock, objVal, newValueType, false, true);
  1720. }
  1721. }