Inline.cpp 248 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358
  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. void
  7. Inline::Optimize()
  8. {
  9. this->Optimize(this->topFunc);
  10. }
  11. void
  12. Inline::Optimize(Func *func, __in_ecount_opt(callerArgOutCount) IR::Instr *callerArgOuts[], Js::ArgSlot callerArgOutCount, uint recursiveInlineDepth)
  13. {
  14. if (!func->DoInline() || !topFunc->DoInline() || func->GetJnFunction()->GetIsAsmjsMode()) // disable inlining for asm
  15. {
  16. return;
  17. }
  18. bool doFixedMethods = !PHASE_OFF(Js::FixedMethodsPhase, func->GetJnFunction());
  19. const auto inlinerData = func->m_workItem->RecyclableData()->JitTimeData();
  20. bool doInline = (inlinerData->InlineeCount() > 0 || inlinerData->IsLdFldInlineePresent());
  21. if (PHASE_OFF(Js::InlinePhase, this->topFunc) ||
  22. PHASE_OFF(Js::InlinePhase, func->GetJnFunction()) ||
  23. func->IsJitInDebugMode())
  24. {
  25. doInline = false;
  26. }
  27. func->actualCount = callerArgOutCount;
  28. // Keep the caller's "this" symbol (if any).
  29. StackSym *symThis = nullptr;
  30. lastStatementBoundary = nullptr;
  31. IR::LabelInstr* loopTop = nullptr;
  32. int32 backEdgeCount = 0;
  33. // Profile data already filter call site outside of loops if the function has loops, so we don't need to detect that here.
  34. FOREACH_INSTR_EDITING(instr, instrNext, func->m_headInstr)
  35. {
  36. bool isInlined = false;
  37. bool isPolymorphic = false;
  38. bool isBuiltIn = false;
  39. bool isCtor = false;
  40. if (doInline)
  41. {
  42. switch (instr->m_opcode)
  43. {
  44. case Js::OpCode::StatementBoundary:
  45. lastStatementBoundary = instr->AsPragmaInstr();
  46. break;
  47. case Js::OpCode::Label:
  48. {
  49. if (!loopTop && instr->AsLabelInstr()->m_isLoopTop)
  50. {
  51. // We only need to know if we are inside loop or not, it doesn't matter how many nested levels we are in.
  52. // This is the cheap way of doing so.
  53. loopTop = instr->AsLabelInstr();
  54. AnalysisAssert(loopTop);
  55. this->isInLoop++;
  56. backEdgeCount = loopTop->labelRefs.Count();
  57. }
  58. }
  59. break;
  60. case Js::OpCode::StFld:
  61. case Js::OpCode::LdFld:
  62. {
  63. // Try inlining of getter setter
  64. if (!inlinerData->IsLdFldInlineePresent())
  65. {
  66. break;
  67. }
  68. if (!instr->IsProfiledInstr())
  69. {
  70. break;
  71. }
  72. if (!(instr->AsProfiledInstr()->u.FldInfo().flags & Js::FldInfoFlags::FldInfo_FromAccessor))
  73. {
  74. break;
  75. }
  76. bool getter = instr->m_opcode == Js::OpCode::LdFld;
  77. IR::Opnd *opnd = getter ? instr->GetSrc1() : instr->GetDst();
  78. if (!(opnd && opnd->IsSymOpnd()))
  79. {
  80. break;
  81. }
  82. IR::SymOpnd* symOpnd = opnd->AsSymOpnd();
  83. if (!symOpnd->m_sym->IsPropertySym())
  84. {
  85. break;
  86. }
  87. Assert(symOpnd->AsSymOpnd()->IsPropertySymOpnd());
  88. const auto inlineCacheIndex = symOpnd->AsPropertySymOpnd()->m_inlineCacheIndex;
  89. const auto inlineeData = inlinerData->GetLdFldInlinee(inlineCacheIndex);
  90. if (!inlineeData)
  91. {
  92. break;
  93. }
  94. Js::FunctionInfo* functionInfo = inlineeData->GetFunctionInfo();
  95. if (!functionInfo->GetFunctionBody())
  96. {
  97. #ifdef ENABLE_DOM_FAST_PATH
  98. Assert(functionInfo->GetLocalFunctionId() == Js::JavascriptBuiltInFunction::DOMFastPathGetter ||
  99. functionInfo->GetLocalFunctionId() == Js::JavascriptBuiltInFunction::DOMFastPathSetter);
  100. if (PHASE_OFF1(Js::InlineHostCandidatePhase))
  101. {
  102. break;
  103. }
  104. this->InlineDOMGetterSetterFunction(instr, inlineeData, inlinerData);
  105. #endif
  106. break;
  107. }
  108. bool isInlinePhaseOff = PHASE_OFF(Js::InlineCandidatePhase, functionInfo->GetFunctionBody()) ||
  109. PHASE_OFF(Js::InlineAccessorsPhase, functionInfo->GetFunctionBody()) ||
  110. (getter && PHASE_OFF(Js::InlineGettersPhase, functionInfo->GetFunctionBody())) ||
  111. (!getter && PHASE_OFF(Js::InlineSettersPhase, functionInfo->GetFunctionBody()));
  112. if (isInlinePhaseOff)
  113. {
  114. break;
  115. }
  116. this->InlineGetterSetterFunction(instr, inlineeData, symThis, inlineCacheIndex, getter /*isGetter*/, recursiveInlineDepth);
  117. break;
  118. }
  119. case Js::OpCode::NewScObjArray:
  120. // We know we're not going to inline these. Just break out and try to do a fixed function check.
  121. isCtor = true;
  122. isBuiltIn = true;
  123. break;
  124. case Js::OpCode::NewScObject:
  125. isCtor = true;
  126. if (PHASE_OFF(Js::InlineConstructorsPhase, this->topFunc))
  127. {
  128. break;
  129. }
  130. // fall-through
  131. case Js::OpCode::CallI:
  132. {
  133. IR::PropertySymOpnd* methodValueOpnd = GetMethodLdOpndForCallInstr(instr);
  134. if (this->inlineesProcessed == inlinerData->InlineeCount())
  135. {
  136. TryResetObjTypeSpecFldInfoOn(methodValueOpnd);
  137. TryDisableRuntimePolymorphicCacheOn(methodValueOpnd);
  138. break;
  139. }
  140. if(!instr->IsProfiledInstr())
  141. {
  142. TryResetObjTypeSpecFldInfoOn(methodValueOpnd);
  143. TryDisableRuntimePolymorphicCacheOn(methodValueOpnd);
  144. break;
  145. }
  146. const auto profileId = static_cast<Js::ProfileId>(instr->AsProfiledInstr()->u.profileId);
  147. if(profileId >= func->GetJnFunction()->GetProfiledCallSiteCount())
  148. {
  149. TryResetObjTypeSpecFldInfoOn(methodValueOpnd);
  150. TryDisableRuntimePolymorphicCacheOn(methodValueOpnd);
  151. break;
  152. }
  153. const auto inlineeData = inlinerData->GetInlinee(profileId);
  154. if(!inlineeData)
  155. {
  156. TryResetObjTypeSpecFldInfoOn(methodValueOpnd);
  157. TryDisableRuntimePolymorphicCacheOn(methodValueOpnd);
  158. break;
  159. }
  160. if(inlinerData->IsPolymorphicCallSite(profileId))
  161. {
  162. isPolymorphic = true;
  163. if (isCtor ||
  164. (PHASE_OFF(Js::PolymorphicInlinePhase, this->topFunc) || PHASE_OFF(Js::PolymorphicInlinePhase, func->GetJnFunction())) ||
  165. (this->IsInliningOutSideLoops() && !PHASE_FORCE(Js::InlinePhase, this->topFunc) && !PHASE_FORCE(Js::InlinePhase, func->GetJnFunction())))
  166. {
  167. #if defined(DBG_DUMP) || defined(ENABLE_DEBUG_CONFIG_OPTIONS)
  168. wchar_t debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  169. #endif
  170. POLYMORPHIC_INLINE_TESTTRACE(L"INLINING (Polymorphic): Skip Inline: Inlining polymorphic call site outside loop\tIsConstructorCall: %s \tisTopFunc: %s\tCaller: %s (%s)\n",
  171. (isCtor? L"true": L"false"), (this->topFunc != func? L"true":L"false"),
  172. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer));
  173. // TODO: Constructor polymorphic inlining
  174. TryResetObjTypeSpecFldInfoOn(methodValueOpnd);
  175. TryDisableRuntimePolymorphicCacheOn(methodValueOpnd);
  176. break;
  177. }
  178. if (!PHASE_OFF(Js::FixedMethodsPhase, this->topFunc) && !PHASE_OFF(Js::PolymorphicInlineFixedMethodsPhase, this->topFunc))
  179. {
  180. instrNext = InlinePolymorphicFunctionUsingFixedMethods(instr, inlinerData, symThis, profileId, methodValueOpnd, &isInlined, recursiveInlineDepth);
  181. }
  182. else
  183. {
  184. TryResetObjTypeSpecFldInfoOn(methodValueOpnd);
  185. TryDisableRuntimePolymorphicCacheOn(methodValueOpnd);
  186. instrNext = InlinePolymorphicFunction(instr, inlinerData, symThis, profileId, &isInlined, recursiveInlineDepth);
  187. }
  188. }
  189. else
  190. {
  191. TryResetObjTypeSpecFldInfoOn(methodValueOpnd);
  192. TryDisableRuntimePolymorphicCacheOn(methodValueOpnd);
  193. Js::FunctionInfo* functionInfo = inlineeData->GetFunctionInfo();
  194. Js::OpCode builtInInlineCandidateOpCode;
  195. ValueType builtInReturnType;
  196. // If the inlinee info is the array constructor, just change the opcode to NewScObjArray
  197. // so that we will inline the array allocation in lower
  198. if (isCtor && functionInfo == &Js::JavascriptArray::EntryInfo::NewInstance)
  199. {
  200. isBuiltIn = true;
  201. instr->m_opcode = Js::OpCode::NewScObjArray;
  202. instr->AsProfiledInstr()->u.profileId = Js::Constants::NoProfileId;
  203. break;
  204. }
  205. isBuiltIn = InliningDecider::GetBuiltInInfo(functionInfo, &builtInInlineCandidateOpCode, &builtInReturnType, func->GetScriptContext());
  206. if(!builtInReturnType.IsUninitialized() && instr->GetDst())
  207. {
  208. Assert(!functionInfo->HasBody());
  209. AssertMsg(instr->m_opcode != Js::OpCode::NewScObjArray, "We should have broken out of the switch statement earlier on this opcode.");
  210. // Value types for the array built-in calls are pulled from the profile; don't change them here.
  211. if ((instr->m_opcode != Js::OpCode::NewScObjArray) ||
  212. !instr->GetDst()->GetValueType().IsLikelyNativeArray())
  213. {
  214. // Assume that this built-in function is not going to be inlined, so the return type cannot be definite
  215. instr->GetDst()->SetValueType(builtInReturnType.ToLikely());
  216. }
  217. }
  218. bool isInlinePhaseOff = functionInfo->HasBody() ?
  219. PHASE_OFF(Js::InlineCandidatePhase, functionInfo->GetFunctionBody()) :
  220. PHASE_OFF1(Js::InlineBuiltInPhase);
  221. if (isInlinePhaseOff)
  222. {
  223. break;
  224. }
  225. if(!functionInfo->HasBody() && builtInInlineCandidateOpCode == 0)
  226. {
  227. // This built-in function is not going to be inlined
  228. break;
  229. }
  230. if(!functionInfo->HasBody())
  231. {
  232. Assert(builtInInlineCandidateOpCode != 0);
  233. if(isCtor)
  234. {
  235. // Inlining a built-in function called as a constructor is currently not supported. Although InliningDecider
  236. // already checks for this, profile data matching with a function does not take into account the difference
  237. // between a constructor call and a regular function call, so need to check it again.
  238. break;
  239. }
  240. // This built-in function is going to be inlined, so reset the destination's value type
  241. if(!builtInReturnType.IsUninitialized())
  242. {
  243. if(instr->GetDst())
  244. {
  245. instr->GetDst()->SetValueType(builtInReturnType);
  246. if(builtInReturnType.IsDefinite())
  247. {
  248. instr->GetDst()->SetValueTypeFixed();
  249. }
  250. }
  251. }
  252. }
  253. else
  254. {
  255. if (!inlineeData->GetFunctionBody()->HasDynamicProfileInfo()) // Don't try to inline a function if it doesn't have profile data
  256. {
  257. break;
  258. }
  259. uint16 constantArguments = 0;
  260. if (!PHASE_OFF(Js::InlineRecursivePhase, func->GetJnFunction()))
  261. {
  262. instr->IterateArgInstrs([&](IR::Instr* argInstr) {
  263. IR::Opnd *src1 = argInstr->GetSrc1();
  264. if (!src1->IsRegOpnd())
  265. {
  266. return false;
  267. }
  268. StackSym *sym = src1->AsRegOpnd()->m_sym;
  269. if (sym->IsIntConst())
  270. {
  271. if (argInstr->GetSrc2() && argInstr->GetSrc2()->IsSymOpnd())
  272. {
  273. StackSym *dstSym = argInstr->GetDst()->AsSymOpnd()->m_sym->AsStackSym();
  274. Assert(dstSym->IsSingleDef());
  275. Assert(dstSym->IsArgSlotSym());
  276. Js::ArgSlot argCount = dstSym->GetArgSlotNum() - 1;
  277. if (argCount == Js::Constants::MaximumArgumentCountForConstantArgumentInlining)
  278. {
  279. return true;
  280. }
  281. constantArguments |= (1 << argCount);
  282. }
  283. }
  284. return false;
  285. });
  286. }
  287. if (!inliningHeuristics.BackendInlineIntoInliner(inlineeData->GetFunctionBody(),
  288. func->GetJnFunction(), this->topFunc, profileId, isCtor, true /*isFixedMethodCall*/,
  289. this->IsInliningOutSideLoops(), this->isInLoop != 0, recursiveInlineDepth, constantArguments))
  290. {
  291. break;
  292. }
  293. }
  294. instrNext = builtInInlineCandidateOpCode != 0 ?
  295. this->InlineBuiltInFunction(instr, functionInfo, builtInInlineCandidateOpCode, inlinerData, symThis, &isInlined, profileId, recursiveInlineDepth) :
  296. this->InlineScriptFunction(instr, inlineeData, symThis, profileId, &isInlined, recursiveInlineDepth);
  297. }
  298. if(++this->inlineesProcessed == inlinerData->InlineeCount())
  299. {
  300. // getterSetter inline caches are shared and we have no way of knowing how many more are present
  301. if (!inlinerData->IsLdFldInlineePresent() && !doFixedMethods)
  302. {
  303. return ;
  304. }
  305. }
  306. break;
  307. }
  308. case Js::OpCode::CallIExtended:
  309. {
  310. if (this->inlineesProcessed == inlinerData->InlineeCount())
  311. {
  312. break;
  313. }
  314. if (!instr->IsProfiledInstr())
  315. {
  316. break;
  317. }
  318. const auto profileId = static_cast<Js::ProfileId>(instr->AsProfiledInstr()->u.profileId);
  319. if (profileId >= func->GetJnFunction()->GetProfiledCallSiteCount())
  320. {
  321. break;
  322. }
  323. const auto inlineeData = inlinerData->GetInlinee(profileId);
  324. if (!inlineeData)
  325. {
  326. break;
  327. }
  328. if (Lowerer::IsSpreadCall(instr))
  329. {
  330. InlineSpread(instr);
  331. }
  332. break;
  333. }
  334. case Js::OpCode::ArgOut_A:
  335. InlConstFoldArg(instr, callerArgOuts, callerArgOutCount);
  336. break;
  337. case Js::OpCode::LdThis:
  338. Assert(instr->GetDst() && instr->GetDst()->IsRegOpnd());
  339. Assert(symThis == nullptr);
  340. symThis = instr->GetDst()->AsRegOpnd()->m_sym;
  341. break;
  342. case Js::OpCode::CheckThis:
  343. // Is this possible? Can we be walking an inlinee here? Doesn't hurt to support this case...
  344. Assert(instr->GetSrc1() && instr->GetSrc1()->IsRegOpnd());
  345. Assert(symThis == nullptr);
  346. symThis = instr->GetSrc1()->AsRegOpnd()->m_sym;
  347. break;
  348. default:
  349. {
  350. if (loopTop && instr->IsBranchInstr())
  351. {
  352. // Look for the back edge to loopTop.
  353. IR::BranchInstr *branch = instr->AsBranchInstr();
  354. IR::LabelInstr *labelDestination = branch->GetTarget();
  355. if (labelDestination == loopTop) // We found the back edge
  356. {
  357. backEdgeCount--;
  358. if (backEdgeCount == 0) // We have seen all the back edges, hence we are outside loop now.
  359. {
  360. Assert(this->isInLoop > 0);
  361. --this->isInLoop;
  362. loopTop = nullptr;
  363. }
  364. }
  365. }
  366. }
  367. }
  368. }
  369. // If we chose not to inline, let's try to optimize this call if it uses a fixed method
  370. if (!isInlined)
  371. {
  372. switch (instr->m_opcode)
  373. {
  374. case Js::OpCode::NewScObject:
  375. case Js::OpCode::NewScObjArray:
  376. isCtor = true;
  377. // intentionally fall through.
  378. case Js::OpCode::CallI:
  379. {
  380. IR::PropertySymOpnd* methodValueOpnd = GetMethodLdOpndForCallInstr(instr);
  381. TryResetObjTypeSpecFldInfoOn(methodValueOpnd);
  382. TryDisableRuntimePolymorphicCacheOn(methodValueOpnd);
  383. StackSym* originalCallTargetStackSym = instr->GetSrc1()->GetStackSym();
  384. bool safeThis = false;
  385. if (TryOptimizeCallInstrWithFixedMethod(instr, nullptr, isPolymorphic /*isPolymorphic*/, isBuiltIn /*isBuiltIn*/, isCtor /*isCtor*/, false /*isInlined*/, safeThis /*unused here*/))
  386. {
  387. Assert(originalCallTargetStackSym != nullptr);
  388. // Insert a ByteCodeUsesInstr to make sure the methodValueDstOpnd's constant value is captured by any
  389. // bailout that occurs between CheckFixedMethodField and CallI.
  390. IR::ByteCodeUsesInstr * useCallTargetInstr = IR::ByteCodeUsesInstr::New(instr, originalCallTargetStackSym->m_id);
  391. instr->InsertBefore(useCallTargetInstr);
  392. // Split NewScObject into NewScObjectNoCtor and CallI, but don't touch NewScObjectArray.
  393. if (instr->m_opcode == Js::OpCode::NewScObject && !PHASE_OFF(Js::SplitNewScObjectPhase, this->topFunc))
  394. {
  395. SplitConstructorCall(instr, false, true);
  396. }
  397. }
  398. else if (instr->m_opcode == Js::OpCode::NewScObjArray)
  399. {
  400. if (instr->GetDst() && instr->GetDst()->GetValueType().IsLikelyNativeArray())
  401. {
  402. // We expect to create a native array here, so we'll insert a check against the
  403. // expected call target, which requires a bailout.
  404. instr = instr->ConvertToBailOutInstr(instr, IR::BailOutOnNotNativeArray);
  405. }
  406. }
  407. }
  408. break;
  409. }
  410. }
  411. } NEXT_INSTR_EDITING;
  412. INLINE_FLUSH();
  413. }
  414. uint Inline::FillInlineesDataArray(
  415. const Js::FunctionCodeGenJitTimeData* inlineeJitTimeData,
  416. const Js::FunctionCodeGenRuntimeData* inlineeRuntimeData,
  417. _Out_writes_to_(inlineesDataArrayLength, (return >= inlineesDataArrayLength ? inlineesDataArrayLength : return)) InlineeData *inlineesDataArray,
  418. uint inlineesDataArrayLength
  419. )
  420. {
  421. uint inlineeCount = 0;
  422. while(inlineeJitTimeData)
  423. {
  424. if (inlineeCount >= inlineesDataArrayLength)
  425. {
  426. // Count the actual number of inlinees for logging.
  427. while (inlineeJitTimeData)
  428. {
  429. inlineeCount++;
  430. inlineeJitTimeData = inlineeJitTimeData->GetNext();
  431. }
  432. return inlineeCount;
  433. }
  434. Js::FunctionBody *inlineeFunctionBody = inlineeJitTimeData->GetFunctionBody();
  435. if (!PHASE_OFF(Js::PolymorphicInlinePhase, inlineeFunctionBody))
  436. {
  437. const Js::FunctionCodeGenJitTimeData* rightInlineeJitTimeData = inlineeJitTimeData->GetJitTimeDataFromFunctionInfo(inlineeFunctionBody);
  438. const Js::FunctionCodeGenRuntimeData* rightInlineeRuntimeData = inlineeRuntimeData->GetRuntimeDataFromFunctionInfo(inlineeFunctionBody);
  439. if (rightInlineeJitTimeData)
  440. {
  441. inlineesDataArray[inlineeCount].inlineeJitTimeData = rightInlineeJitTimeData;
  442. inlineesDataArray[inlineeCount].inlineeRuntimeData = rightInlineeRuntimeData;
  443. inlineesDataArray[inlineeCount].functionBody = inlineeFunctionBody;
  444. Assert(rightInlineeJitTimeData->GetFunctionBody() == inlineeFunctionBody);
  445. #ifdef DBG
  446. for (uint k = 0; k < inlineeCount; k++)
  447. {
  448. if (inlineesDataArray[k].functionBody == inlineeFunctionBody)
  449. {
  450. AssertMsg(false, "We should never see duplicate function body here");
  451. }
  452. }
  453. #endif
  454. inlineeCount++;
  455. }
  456. else
  457. {
  458. #if defined(DBG_DUMP) || defined(ENABLE_DEBUG_CONFIG_OPTIONS)
  459. wchar_t debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  460. #endif
  461. POLYMORPHIC_INLINE_TESTTRACE(L"INLINING (Polymorphic): Missing jit time data skipped inlinee\tInlinee: %s (%s)\n",
  462. inlineeFunctionBody->GetDisplayName(), inlineeFunctionBody->GetDebugNumberSet(debugStringBuffer));
  463. }
  464. }
  465. inlineeJitTimeData = inlineeJitTimeData->GetNext();
  466. }
  467. return inlineeCount;
  468. }
  469. void Inline::FillInlineesDataArrayUsingFixedMethods(
  470. const Js::FunctionCodeGenJitTimeData* inlineeJitTimeData,
  471. const Js::FunctionCodeGenRuntimeData* inlineeRuntimeData,
  472. __inout_ecount(inlineesDataArrayLength) InlineeData *inlineesDataArray,
  473. uint inlineesDataArrayLength,
  474. __inout_ecount(cachedFixedInlineeCount) Js::FixedFieldInfo* fixedFieldInfoArray,
  475. uint16 cachedFixedInlineeCount
  476. )
  477. {
  478. AnalysisAssert(cachedFixedInlineeCount <= inlineesDataArrayLength);
  479. Js::FunctionBody* inlineeFuncBody = nullptr;
  480. while (inlineeJitTimeData)
  481. {
  482. inlineeFuncBody = inlineeJitTimeData->GetFunctionBody();
  483. if (!PHASE_OFF(Js::PolymorphicInlinePhase, inlineeFuncBody) && !PHASE_OFF(Js::PolymorphicInlineFixedMethodsPhase, inlineeFuncBody))
  484. {
  485. const Js::FunctionCodeGenJitTimeData* jitTimeData = inlineeJitTimeData->GetJitTimeDataFromFunctionInfo(inlineeFuncBody);
  486. if (jitTimeData)
  487. {
  488. for (uint16 i = 0; i < cachedFixedInlineeCount; i++)
  489. {
  490. if (inlineeFuncBody == ((Js::JavascriptFunction*)(fixedFieldInfoArray[i].fieldValue))->GetFunctionBody())
  491. {
  492. inlineesDataArray[i].inlineeJitTimeData = inlineeJitTimeData->GetJitTimeDataFromFunctionInfo(inlineeFuncBody);
  493. inlineesDataArray[i].inlineeRuntimeData = inlineeRuntimeData->GetRuntimeDataFromFunctionInfo(inlineeFuncBody);
  494. inlineesDataArray[i].functionBody = inlineeFuncBody;
  495. break;
  496. }
  497. }
  498. }
  499. else
  500. {
  501. #if defined(DBG_DUMP) || defined(ENABLE_DEBUG_CONFIG_OPTIONS)
  502. wchar_t debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  503. #endif
  504. POLYMORPHIC_INLINE_TESTTRACE(L"INLINING (Polymorphic): Missing jit time data skipped inlinee\tInlinee: %s (%s)\n",
  505. inlineeFuncBody->GetDisplayName(), inlineeFuncBody->GetDebugNumberSet(debugStringBuffer));
  506. }
  507. }
  508. inlineeJitTimeData = inlineeJitTimeData->GetNext();
  509. }
  510. }
  511. IR::Instr *
  512. Inline::InlinePolymorphicFunctionUsingFixedMethods(IR::Instr *callInstr, const Js::FunctionCodeGenJitTimeData* inlinerData, const StackSym *symCallerThis, const Js::ProfileId profileId, IR::PropertySymOpnd* methodValueOpnd, bool* pIsInlined, uint recursiveInlineDepth)
  513. {
  514. IR::Instr* instrNext = callInstr->m_next;
  515. *pIsInlined = false;
  516. const Js::FunctionCodeGenJitTimeData* inlineeJitTimeData = inlinerData->GetInlinee(profileId);
  517. AnalysisAssert(inlineeJitTimeData);
  518. #if defined(DBG_DUMP) || defined(ENABLE_DEBUG_CONFIG_OPTIONS)
  519. wchar_t debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  520. wchar_t debugStringBuffer2[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  521. #endif
  522. // Abort conditions:
  523. if(!inlineeJitTimeData->GetNext())
  524. {
  525. POLYMORPHIC_INLINE_TESTTRACE(L"INLINING (Polymorphic): Skip Inline: Missing JitTime data \tInlinee: %s (%s):\tCaller: %s (%s)\n",
  526. inlineeJitTimeData->GetFunctionBody()->GetDisplayName(), inlineeJitTimeData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer),
  527. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer2));
  528. // There are no multiple codegen jit-time data allocated for this call site, not sure how is this possible, abort
  529. TryResetObjTypeSpecFldInfoOn(methodValueOpnd);
  530. TryDisableRuntimePolymorphicCacheOn(methodValueOpnd);
  531. return instrNext;
  532. }
  533. // arguments exceed MaxInlineeArgoutCount
  534. if (callInstr->GetSrc2() &&
  535. callInstr->GetSrc2()->IsSymOpnd() &&
  536. callInstr->GetSrc2()->AsSymOpnd()->m_sym->AsStackSym()->GetArgSlotNum() > Js::InlineeCallInfo::MaxInlineeArgoutCount)
  537. {
  538. // This is a hard limit as we only use 4 bits to encode the actual count in the InlineeCallInfo. Although
  539. // InliningDecider already checks for this, the check is against profile data that may not be accurate since profile
  540. // data matching does not take into account some types of changes to source code. Need to check this again with current
  541. // information.
  542. POLYMORPHIC_INLINE_TESTTRACE(L"INLINING (Polymorphic): Skip Inline: ArgSlot > MaxInlineeArgoutCount\tInlinee: %s (%s)\tArgSlotNum: %d\tMaxInlineeArgoutCount: %d\tCaller: %s (%s)\n",
  543. inlineeJitTimeData->GetFunctionBody()->GetDisplayName(), inlineeJitTimeData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer) , callInstr->GetSrc2()->AsSymOpnd()->m_sym->AsStackSym()->GetArgSlotNum(),
  544. Js::InlineeCallInfo::MaxInlineeArgoutCount, inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer2));
  545. TryResetObjTypeSpecFldInfoOn(methodValueOpnd);
  546. TryDisableRuntimePolymorphicCacheOn(methodValueOpnd);
  547. return instrNext;
  548. }
  549. uint inlineeCount = 0;
  550. const Js::FunctionCodeGenJitTimeData* tmpInlineeJitTimeData = inlineeJitTimeData;
  551. while(tmpInlineeJitTimeData)
  552. {
  553. inlineeCount++;
  554. tmpInlineeJitTimeData = tmpInlineeJitTimeData->GetNext();
  555. }
  556. // Inlinee count too small (<2) or too large (>4)
  557. if (inlineeCount < 2 || inlineeCount > Js::DynamicProfileInfo::maxPolymorphicInliningSize)
  558. {
  559. POLYMORPHIC_INLINE_TESTTRACE(L"INLINING (Polymorphic): Skip Inline: Inlinee count either too small or too large: InlineeCount %d (Max: %d)\tInlinee: %s (%s):\tCaller: %s (%s)\n",
  560. inlineeCount, Js::DynamicProfileInfo::maxPolymorphicInliningSize,
  561. inlineeJitTimeData->GetFunctionBody()->GetDisplayName(), inlineeJitTimeData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer),
  562. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer2));
  563. TryResetObjTypeSpecFldInfoOn(methodValueOpnd);
  564. TryDisableRuntimePolymorphicCacheOn(methodValueOpnd);
  565. return instrNext;
  566. }
  567. *pIsInlined = true;
  568. IR::Instr* tmpInstr = callInstr->m_prev;
  569. while (tmpInstr->m_opcode != Js::OpCode::StartCall)
  570. {
  571. if ((tmpInstr->m_opcode != Js::OpCode::ArgOut_A) && (tmpInstr->m_opcode != Js::OpCode::Ld_A))
  572. {
  573. POLYMORPHIC_INLINE_TESTTRACE(L"INLINING (Polymorphic; Using Fixed Methods): Skip Inline: ArgOuts may have side effects Inlinee: %s (%s):\tCaller: %s (%s)\n",
  574. inlineeJitTimeData->GetFunctionBody()->GetDisplayName(), inlineeJitTimeData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer),
  575. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer2));
  576. return InlinePolymorphicFunction(callInstr, inlinerData, symCallerThis, profileId, pIsInlined, recursiveInlineDepth, true);
  577. }
  578. tmpInstr = tmpInstr->m_prev;
  579. }
  580. StackSym* methodValueSym = callInstr->GetSrc1()->AsRegOpnd()->m_sym->AsStackSym();
  581. if (!methodValueSym->IsSingleDef())
  582. {
  583. return InlinePolymorphicFunction(callInstr, inlinerData, symCallerThis, profileId, pIsInlined, recursiveInlineDepth, true);
  584. }
  585. IR::Instr* ldMethodFldInstr = methodValueSym->GetInstrDef();
  586. if (!(ldMethodFldInstr->GetSrc1()->IsSymOpnd() && ldMethodFldInstr->GetSrc1()->AsSymOpnd()->IsPropertySymOpnd()))
  587. {
  588. POLYMORPHIC_INLINE_TESTTRACE(L"INLINING (Polymorphic; Using Fixed Methods): Skip Inline: Did not find property sym operand for the method load Inlinee: %s (%s):\tCaller: %s (%s)\n",
  589. inlineeJitTimeData->GetFunctionBody()->GetDisplayName(), inlineeJitTimeData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer),
  590. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer2));
  591. return InlinePolymorphicFunction(callInstr, inlinerData, symCallerThis, profileId, pIsInlined, recursiveInlineDepth, true);
  592. }
  593. IR::PropertySymOpnd* methodPropertyOpnd = ldMethodFldInstr->GetSrc1()->AsPropertySymOpnd();
  594. if (!methodPropertyOpnd->HasObjTypeSpecFldInfo())
  595. {
  596. POLYMORPHIC_INLINE_TESTTRACE(L"INLINING (Polymorphic; Using Fixed Methods): Skip Inline: no ObjTypeSpecFldInfo to get Fixed Methods from Inlinee: %s (%s):\tCaller: %s (%s)\n",
  597. inlineeJitTimeData->GetFunctionBody()->GetDisplayName(), inlineeJitTimeData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer),
  598. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer2));
  599. return InlinePolymorphicFunction(callInstr, inlinerData, symCallerThis, profileId, pIsInlined, recursiveInlineDepth, true);
  600. }
  601. if (!methodPropertyOpnd->HasFixedValue())
  602. {
  603. POLYMORPHIC_INLINE_TESTTRACE(L"INLINING (Polymorphic; Using Fixed Methods): Skip Inline: ObjTypeSpecFldInfo doesn't have Fixed Methods for one or some of the inlinees Inlinee: %s (%s):\tCaller: %s (%s)\n",
  604. inlineeJitTimeData->GetFunctionBody()->GetDisplayName(), inlineeJitTimeData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer),
  605. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer2));
  606. return InlinePolymorphicFunction(callInstr, inlinerData, symCallerThis, profileId, pIsInlined, recursiveInlineDepth, true);
  607. }
  608. uint16 cachedFixedInlineeCount = methodPropertyOpnd->GetFixedFieldCount();
  609. if (cachedFixedInlineeCount < 2)
  610. {
  611. POLYMORPHIC_INLINE_TESTTRACE(L"INLINING (Polymorphic; Using Fixed Methods): Skip Inline: fixed function count too less %d (Max: %d)\tInlinee: %s (%s):\tCaller: %s (%s)\n",
  612. cachedFixedInlineeCount, Js::DynamicProfileInfo::maxPolymorphicInliningSize,
  613. inlineeJitTimeData->GetFunctionBody()->GetDisplayName(), inlineeJitTimeData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer),
  614. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer2));
  615. return InlinePolymorphicFunction(callInstr, inlinerData, symCallerThis, profileId, pIsInlined, recursiveInlineDepth, true);
  616. }
  617. Js::FixedFieldInfo* fixedFunctionInfoArray = methodPropertyOpnd->GetFixedFieldInfoArray();
  618. // It might so be the case that two objects of different types call the same function (body), for e.g., if they share the prototype on which the function is defined.
  619. uint uniqueFixedFunctionCount = HandleDifferentTypesSameFunction(fixedFunctionInfoArray, cachedFixedInlineeCount);
  620. if (uniqueFixedFunctionCount != inlineeCount)
  621. {
  622. // inlineeCount obtained from the inlineeJitTimeData is more accurate than cached number of fixed methods for inlinees.
  623. POLYMORPHIC_INLINE_TESTTRACE(L"INLINING (Polymorphic; Using Fixed Methods): Skip Inline: cached fixed function count (%d) doesn't match inlinee count (%d); (Max: %d)\tInlinee: %s (%s):\tCaller: %s (%s)\n",
  624. uniqueFixedFunctionCount, inlineeCount, Js::DynamicProfileInfo::maxPolymorphicInliningSize,
  625. inlineeJitTimeData->GetFunctionBody()->GetDisplayName(), inlineeJitTimeData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer),
  626. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer2));
  627. return InlinePolymorphicFunction(callInstr, inlinerData, symCallerThis, profileId, pIsInlined, recursiveInlineDepth, true);
  628. }
  629. Assert(cachedFixedInlineeCount <= Js::DynamicProfileInfo::maxPolymorphicInliningSize);
  630. InlineeData inlineesDataArray[Js::DynamicProfileInfo::maxPolymorphicInliningSize] = {};
  631. const Js::FunctionCodeGenRuntimeData* inlineeRuntimeData = callInstr->m_func->m_runtimeData ?
  632. callInstr->m_func->m_runtimeData->GetInlinee(profileId) :
  633. this->topFunc->GetJnFunction()->GetInlineeCodeGenRuntimeData(profileId);
  634. FillInlineesDataArrayUsingFixedMethods(inlineeJitTimeData, inlineeRuntimeData, inlineesDataArray, Js::DynamicProfileInfo::maxPolymorphicInliningSize, fixedFunctionInfoArray, cachedFixedInlineeCount);
  635. for (uint i = 0; i < cachedFixedInlineeCount; i++)
  636. {
  637. if(!inlineesDataArray[i].functionBody)
  638. {
  639. POLYMORPHIC_INLINE_TESTTRACE(L"INLINING (Polymorphic; Using Fixed Methods): Skip Inline: One of the inlinees doesn't have the corresponding object/prototype's type cached\tCaller: %s (%s)\n",
  640. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer));
  641. return InlinePolymorphicFunction(callInstr, inlinerData, symCallerThis, profileId, pIsInlined, recursiveInlineDepth, true);
  642. }
  643. #if DBG
  644. if(inlineesDataArray[i].functionBody && inlineesDataArray[i].functionBody != methodPropertyOpnd->GetFieldValueAsFixedFunction(i)->GetFunctionBody())
  645. {
  646. AssertMsg(false, "inlineesDataArray and fixedfunctionInfoArray should be aligned with each other at this point");
  647. }
  648. #endif
  649. while (fixedFunctionInfoArray[i].nextHasSameFixedField)
  650. {
  651. i++;
  652. }
  653. }
  654. bool safeThis = true; // Eliminate CheckThis for inlining.
  655. for (uint i = 0; i < cachedFixedInlineeCount; i++)
  656. {
  657. if (!methodPropertyOpnd->GetFieldValue(i))
  658. {
  659. POLYMORPHIC_INLINE_TESTTRACE(L"INLINING (Polymorphic; Using Fixed Methods): Skip Inline: no fixed method for one of the inlinees; Inlinee: %s (%s):\tCaller: %s (%s)\n",
  660. inlineesDataArray[i].functionBody->GetDisplayName(), inlineesDataArray[i].functionBody->GetDebugNumberSet(debugStringBuffer),
  661. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer2));
  662. return InlinePolymorphicFunction(callInstr, inlinerData, symCallerThis, profileId, pIsInlined, recursiveInlineDepth, true);
  663. }
  664. if (i == 0)
  665. {
  666. // Do all the general, non-function-object-specific checks just once.
  667. if (!TryOptimizeCallInstrWithFixedMethod(callInstr, (Js::FunctionInfo*)(inlineesDataArray[i].functionBody), true, false, false, true /*isInlined*/, safeThis, true /*dontOptimizeJustCheck*/, i))
  668. {
  669. POLYMORPHIC_INLINE_TESTTRACE(L"INLINING (Polymorphic; Using Fixed Methods): Skip Inline: can't optimize using Fixed Methods %d (Max: %d)\tInlinee: %s (%s):\tCaller: %s (%s)\n",
  670. inlineeCount, Js::DynamicProfileInfo::maxPolymorphicInliningSize,
  671. inlineeJitTimeData->GetFunctionBody()->GetDisplayName(), inlineeJitTimeData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer),
  672. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer2));
  673. return InlinePolymorphicFunction(callInstr, inlinerData, symCallerThis, profileId, pIsInlined, recursiveInlineDepth, true);
  674. }
  675. }
  676. else
  677. {
  678. if (methodPropertyOpnd->GetFieldValueAsFixedFunction(i) &&
  679. methodPropertyOpnd->GetFieldValueAsFixedFunction(i)->GetFunctionInfo() != (Js::FunctionInfo*)(inlineesDataArray[i].functionBody))
  680. {
  681. POLYMORPHIC_INLINE_TESTTRACE(L"INLINING (Polymorphic; Using Fixed Methods): Skip Inline: can't optimize using Fixed Methods %d (Max: %d)\tInlinee: %s (%s):\tCaller: %s (%s)\n",
  682. inlineeCount, Js::DynamicProfileInfo::maxPolymorphicInliningSize,
  683. inlineeJitTimeData->GetFunctionBody()->GetDisplayName(), inlineeJitTimeData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer),
  684. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer2));
  685. return InlinePolymorphicFunction(callInstr, inlinerData, symCallerThis, profileId, pIsInlined, recursiveInlineDepth, true);
  686. }
  687. }
  688. Js::TypeId typeId = methodPropertyOpnd->GetTypeId(i);
  689. if(!(typeId > Js::TypeIds_LastJavascriptPrimitiveType && typeId <= Js::TypeIds_LastTrueJavascriptObjectType))
  690. {
  691. // Don't eliminate CheckThis if it cannot be done for any one of the inlinees
  692. safeThis = false;
  693. }
  694. while (fixedFunctionInfoArray[i].nextHasSameFixedField)
  695. {
  696. i++;
  697. }
  698. }
  699. Assert(methodPropertyOpnd->IsPoly());
  700. // emit property guard check for the method load, and load type
  701. IR::RegOpnd *typeOpnd = IR::RegOpnd::New(TyVar, callInstr->m_func);
  702. IR::Instr* propertyGuardCheckInstr = IR::Instr::New(Js::OpCode::CheckPropertyGuardAndLoadType, typeOpnd, ldMethodFldInstr->GetSrc1(), callInstr->m_func);
  703. ldMethodFldInstr->InsertBefore(propertyGuardCheckInstr);
  704. propertyGuardCheckInstr->SetByteCodeOffset(ldMethodFldInstr);
  705. propertyGuardCheckInstr = propertyGuardCheckInstr->ConvertToBailOutInstr(ldMethodFldInstr, IR::BailOutFailedFixedFieldCheck);
  706. POLYMORPHIC_INLINE_TESTTRACE(L"------------------------------------------------\n");
  707. for (uint i = 0; i < cachedFixedInlineeCount; i++)
  708. {
  709. Js::FunctionBody *inlineeFunctionBody = inlineesDataArray[i].functionBody;
  710. POLYMORPHIC_INLINE_TESTTRACE(L"INLINING (Polymorphic; Using Fixed Methods): Start inlining: \tInlinee: %s (%s):\tCaller: %s (%s)\n",
  711. inlineeFunctionBody->GetDisplayName(), inlineeFunctionBody->GetDebugNumberSet(debugStringBuffer),
  712. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer2));
  713. while (fixedFunctionInfoArray[i].nextHasSameFixedField)
  714. {
  715. i++;
  716. }
  717. }
  718. POLYMORPHIC_INLINE_TESTTRACE(L"------------------------------------------------\n");
  719. IR::RegOpnd * returnValueOpnd;
  720. if (callInstr->GetDst())
  721. {
  722. returnValueOpnd = callInstr->UnlinkDst()->AsRegOpnd();
  723. }
  724. else
  725. {
  726. returnValueOpnd = nullptr;
  727. }
  728. callInstr->MoveArgs(/*generateByteCodeCapture*/ true);
  729. callInstr->m_opcode = Js::OpCode::CallIFixed;
  730. // iterate over inlineesDataArray to emit each inlinee
  731. IR::LabelInstr * doneLabel = IR::LabelInstr::New(Js::OpCode::Label, callInstr->m_func, false);
  732. IR::Instr* dispatchStartLabel = IR::LabelInstr::New(Js::OpCode::Label, callInstr->m_func, false);
  733. callInstr->InsertBefore(dispatchStartLabel);
  734. for(uint i=0; i < cachedFixedInlineeCount; i++)
  735. {
  736. IR::LabelInstr* inlineeStartLabel = IR::LabelInstr::New(Js::OpCode::Label, callInstr->m_func);
  737. callInstr->InsertBefore(inlineeStartLabel);
  738. IR::AddrOpnd * constMethodValueOpnd = IR::AddrOpnd::New(methodPropertyOpnd->GetFieldValue(i), IR::AddrOpndKind::AddrOpndKindDynamicVar, callInstr->m_func);
  739. constMethodValueOpnd->m_isFunction = true;
  740. InsertOneInlinee(callInstr, returnValueOpnd, constMethodValueOpnd, inlineesDataArray[i], doneLabel, symCallerThis, safeThis, recursiveInlineDepth);
  741. while (fixedFunctionInfoArray[i].nextHasSameFixedField)
  742. {
  743. dispatchStartLabel->InsertBefore(IR::BranchInstr::New(Js::OpCode::BrAddr_A, inlineeStartLabel, typeOpnd, IR::AddrOpnd::New(methodPropertyOpnd->GetType(i),
  744. IR::AddrOpndKindDynamicType, dispatchStartLabel->m_func), dispatchStartLabel->m_func));
  745. this->topFunc->PinTypeRef(methodPropertyOpnd->GetType(i)); // Keep the types alive as the types may not be equivalent and, hence, won't be kept alive by EquivalentTypeCache
  746. i++;
  747. }
  748. dispatchStartLabel->InsertBefore(IR::BranchInstr::New(Js::OpCode::BrAddr_A, inlineeStartLabel,
  749. typeOpnd, IR::AddrOpnd::New(methodPropertyOpnd->GetType(i), IR::AddrOpndKindDynamicType, dispatchStartLabel->m_func), dispatchStartLabel->m_func));
  750. this->topFunc->PinTypeRef(methodPropertyOpnd->GetType(i)); // Keep the types alive as the types may not be equivalent and, hence, won't be kept alive by EquivalentTypeCache
  751. }
  752. ldMethodFldInstr->Unlink();
  753. ldMethodFldInstr->m_opcode = Js::OpCode::LdMethodFldPolyInlineMiss;
  754. Assert(cachedFixedInlineeCount > 0);
  755. CompletePolymorphicInlining(callInstr, returnValueOpnd, doneLabel, dispatchStartLabel, ldMethodFldInstr, IR::BailOutOnFailedPolymorphicInlineTypeCheck);
  756. this->topFunc->SetHasInlinee();
  757. InsertStatementBoundary(instrNext);
  758. return instrNext;
  759. }
  760. void Inline::CloneCallSequence(IR::Instr* callInstr, IR::Instr* clonedCallInstr)
  761. {
  762. IR::Instr* previousArg = nullptr;
  763. IR::Instr* previousClonedArg = clonedCallInstr;
  764. callInstr->IterateArgInstrs([&](IR::Instr* argInstr){
  765. IR::Instr* cloneArg = IR::Instr::New(argInstr->m_opcode,
  766. IR::SymOpnd::New(callInstr->m_func->m_symTable->GetArgSlotSym(argInstr->GetDst()->GetStackSym()->GetArgSlotNum()), 0, TyMachPtr, callInstr->m_func),
  767. argInstr->GetSrc1(), callInstr->m_func);
  768. cloneArg->SetByteCodeOffset(callInstr);
  769. cloneArg->GetDst()->GetStackSym()->m_isArgCaptured = true;
  770. previousClonedArg->SetSrc2(cloneArg->GetDst());
  771. previousClonedArg->InsertBefore(cloneArg);
  772. previousArg = argInstr;
  773. previousClonedArg = cloneArg;
  774. return false;
  775. });
  776. IR::Instr* startCall = previousArg->GetSrc2()->GetStackSym()->GetInstrDef();
  777. previousClonedArg->SetSrc2(startCall->GetDst());
  778. }
  779. IR::Instr *
  780. Inline::InlinePolymorphicFunction(IR::Instr *callInstr, const Js::FunctionCodeGenJitTimeData* inlinerData, const StackSym *symCallerThis, const Js::ProfileId profileId, bool* pIsInlined, uint recursiveInlineDepth, bool triedUsingFixedMethods)
  781. {
  782. IR::Instr* instrNext = callInstr->m_next;
  783. *pIsInlined = false;
  784. if (triedUsingFixedMethods)
  785. {
  786. if (callInstr->GetSrc1()->AsRegOpnd()->m_sym->AsStackSym()->IsSingleDef())
  787. {
  788. IR::Instr* ldMethodFldInstr = callInstr->GetSrc1()->AsRegOpnd()->m_sym->AsStackSym()->GetInstrDef();
  789. if (ldMethodFldInstr->GetSrc1()->IsSymOpnd() && ldMethodFldInstr->GetSrc1()->AsSymOpnd()->IsPropertySymOpnd())
  790. {
  791. TryResetObjTypeSpecFldInfoOn(ldMethodFldInstr->GetSrc1()->AsPropertySymOpnd());
  792. TryDisableRuntimePolymorphicCacheOn(ldMethodFldInstr->GetSrc1()->AsPropertySymOpnd());
  793. }
  794. }
  795. }
  796. const Js::FunctionCodeGenJitTimeData* inlineeJitTimeData = inlinerData->GetInlinee(profileId);
  797. #if defined(DBG_DUMP) || defined(ENABLE_DEBUG_CONFIG_OPTIONS)
  798. wchar_t debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  799. wchar_t debugStringBuffer2[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  800. #endif
  801. if (!triedUsingFixedMethods) // We would have done the following two checks when we tried to inline using fixed methods
  802. {
  803. if(!inlineeJitTimeData->GetNext())
  804. {
  805. POLYMORPHIC_INLINE_TESTTRACE(L"INLINING (Polymorphic): Skip Inline: Missing JitTime data \tInlinee: %s (%s):\tCaller: %s (%s)\n",
  806. inlineeJitTimeData->GetFunctionBody()->GetDisplayName(), inlineeJitTimeData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer),
  807. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer2));
  808. //There are no multiple codegen jit-time data allocated for this call site, not sure how is this possible, abort
  809. return instrNext;
  810. }
  811. if (callInstr->GetSrc2() &&
  812. callInstr->GetSrc2()->IsSymOpnd() &&
  813. callInstr->GetSrc2()->AsSymOpnd()->m_sym->AsStackSym()->GetArgSlotNum() > Js::InlineeCallInfo::MaxInlineeArgoutCount)
  814. {
  815. // This is a hard limit as we only use 4 bits to encode the actual count in the InlineeCallInfo. Although
  816. // InliningDecider already checks for this, the check is against profile data that may not be accurate since profile
  817. // data matching does not take into account some types of changes to source code. Need to check this again with current
  818. // information.
  819. POLYMORPHIC_INLINE_TESTTRACE(L"INLINING (Polymorphic): Skip Inline: ArgSlot > MaxInlineeArgoutCount\tInlinee: %s (%s)\tArgSlotNum: %d\tMaxInlineeArgoutCount: %d\tCaller: %s (%s)\n",
  820. inlineeJitTimeData->GetFunctionBody()->GetDisplayName(), inlineeJitTimeData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer) , callInstr->GetSrc2()->AsSymOpnd()->m_sym->AsStackSym()->GetArgSlotNum(),
  821. Js::InlineeCallInfo::MaxInlineeArgoutCount, inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer2));
  822. return instrNext;
  823. }
  824. }
  825. InlineeData inlineesDataArray[Js::DynamicProfileInfo::maxPolymorphicInliningSize];
  826. const Js::FunctionCodeGenRuntimeData* inlineeRuntimeData = callInstr->m_func->m_runtimeData ?
  827. callInstr->m_func->m_runtimeData->GetInlinee(profileId) :
  828. this->topFunc->GetJnFunction()->GetInlineeCodeGenRuntimeData(profileId);
  829. uint inlineeCount = FillInlineesDataArray(inlineeJitTimeData, inlineeRuntimeData, inlineesDataArray, Js::DynamicProfileInfo::maxPolymorphicInliningSize);
  830. if (inlineeCount < 2 || inlineeCount > Js::DynamicProfileInfo::maxPolymorphicInliningSize)
  831. {
  832. POLYMORPHIC_INLINE_TESTTRACE(L"INLINING (Polymorphic): Skip Inline: Inlinee count either too small or too large %d (Max: %d)\tInlinee: %s (%s):\tCaller: %s (%s)\n",
  833. inlineeCount, Js::DynamicProfileInfo::maxPolymorphicInliningSize,
  834. inlineeJitTimeData->GetFunctionBody()->GetDisplayName(), inlineeJitTimeData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer),
  835. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer2));
  836. return instrNext;
  837. }
  838. // Begin inlining.
  839. POLYMORPHIC_INLINE_TESTTRACE(L"------------------------------------------------\n");
  840. for (uint i = 0; i < inlineeCount; i++)
  841. {
  842. Js::FunctionBody *inlineeFunctionBody = inlineesDataArray[i].functionBody;
  843. POLYMORPHIC_INLINE_TESTTRACE(L"INLINING (Polymorphic): Start inlining: \tInlinee: %s (%s):\tCaller: %s (%s)\n",
  844. inlineeFunctionBody->GetDisplayName(), inlineeFunctionBody->GetDebugNumberSet(debugStringBuffer),
  845. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer2));
  846. }
  847. POLYMORPHIC_INLINE_TESTTRACE(L"------------------------------------------------\n");
  848. *pIsInlined = true;
  849. // This function is recursive, so when jitting in the foreground, probe the stack
  850. if (!this->topFunc->IsBackgroundJIT())
  851. {
  852. PROBE_STACK(this->topFunc->GetScriptContext(), Js::Constants::MinStackDefault);
  853. }
  854. IR::RegOpnd * returnValueOpnd;
  855. Js::RegSlot returnRegSlot;
  856. if (callInstr->GetDst())
  857. {
  858. returnValueOpnd = callInstr->UnlinkDst()->AsRegOpnd();
  859. returnRegSlot = returnValueOpnd->m_sym->GetByteCodeRegSlot();
  860. }
  861. else
  862. {
  863. returnValueOpnd = nullptr;
  864. returnRegSlot = Js::Constants::NoRegister;
  865. }
  866. Assert(inlineeCount >= 2);
  867. // Shared bailout point for all the guard check bailouts.
  868. InsertJsFunctionCheck(callInstr, callInstr, IR::BailOutOnPolymorphicInlineFunction);
  869. callInstr->MoveArgs(/*generateByteCodeCapture*/ true);
  870. IR::LabelInstr * doneLabel = IR::LabelInstr::New(Js::OpCode::Label, callInstr->m_func, false);
  871. IR::Instr* dispatchStartLabel = IR::LabelInstr::New(Js::OpCode::Label, callInstr->m_func, false);
  872. callInstr->InsertBefore(dispatchStartLabel);
  873. for (uint i = 0; i < inlineeCount; i++)
  874. {
  875. IR::LabelInstr* inlineeStartLabel = IR::LabelInstr::New(Js::OpCode::Label, callInstr->m_func);
  876. callInstr->InsertBefore(inlineeStartLabel);
  877. InsertOneInlinee(callInstr, returnValueOpnd, callInstr->GetSrc1(), inlineesDataArray[i], doneLabel, symCallerThis, /*fixedFunctionSafeThis*/ false, recursiveInlineDepth);
  878. IR::RegOpnd* functionObject = callInstr->GetSrc1()->AsRegOpnd();
  879. dispatchStartLabel->InsertBefore(IR::BranchInstr::New(Js::OpCode::BrAddr_A, inlineeStartLabel,
  880. IR::IndirOpnd::New(functionObject, Js::JavascriptFunction::GetOffsetOfFunctionInfo(), TyMachPtr, dispatchStartLabel->m_func),
  881. IR::AddrOpnd::New(inlineesDataArray[i].functionBody, IR::AddrOpndKindDynamicFunctionBody, dispatchStartLabel->m_func), dispatchStartLabel->m_func));
  882. }
  883. CompletePolymorphicInlining(callInstr, returnValueOpnd, doneLabel, dispatchStartLabel, /*ldMethodFldInstr*/nullptr, IR::BailOutOnPolymorphicInlineFunction);
  884. this->topFunc->SetHasInlinee();
  885. InsertStatementBoundary(instrNext);
  886. return instrNext;
  887. }
  888. void Inline::CompletePolymorphicInlining(IR::Instr* callInstr, IR::RegOpnd* returnValueOpnd, IR::LabelInstr* doneLabel, IR::Instr* dispatchStartLabel, IR::Instr* ldMethodFldInstr, IR::BailOutKind bailoutKind)
  889. {
  890. // Label $bailout:
  891. // LdMethodFldPolyInlineMiss
  892. // BailOnNotPolymorphicInlinee $callOutBytecodeOffset - BailOutOnFailedPolymorphicInlineTypeCheck
  893. // ByteCoudeUses
  894. // BytecodeArgoutUses
  895. // returnValueOpnd = EndCallForPolymorphicInlinee actualsCount
  896. IR::LabelInstr* bailOutLabel = IR::LabelInstr::New(Js::OpCode::Label, callInstr->m_func, /*helperLabel*/ true);
  897. callInstr->InsertBefore(bailOutLabel);
  898. dispatchStartLabel->InsertBefore(IR::BranchInstr::New(Js::OpCode::Br, bailOutLabel, callInstr->m_func));
  899. // Only fixed function inlining requires a ldMethodFldInstr
  900. if (ldMethodFldInstr)
  901. {
  902. callInstr->InsertBefore(ldMethodFldInstr);
  903. }
  904. callInstr->InsertBefore(IR::BailOutInstr::New(Js::OpCode::BailOnNotPolymorphicInlinee, bailoutKind, callInstr, callInstr->m_func));
  905. uint actualsCount = 0;
  906. callInstr->IterateArgInstrs([&](IR::Instr* argInstr) {
  907. IR::Instr* bytecodeArgOutUse = IR::Instr::New(Js::OpCode::BytecodeArgOutUse, callInstr->m_func);
  908. bytecodeArgOutUse->SetByteCodeOffset(callInstr);
  909. bytecodeArgOutUse->SetSrc1(argInstr->GetSrc1());
  910. callInstr->InsertBefore(bytecodeArgOutUse);
  911. actualsCount++;
  912. // Remove the original args
  913. argInstr->Remove();
  914. return false;
  915. });
  916. callInstr->InsertBefore(IR::ByteCodeUsesInstr::New(callInstr, callInstr->GetSrc1()->GetStackSym()->m_id));
  917. IR::Instr* endCallInstr = IR::Instr::New(Js::OpCode::EndCallForPolymorphicInlinee, callInstr->m_func);
  918. endCallInstr->SetSrc1(IR::IntConstOpnd::New(actualsCount + Js::Constants::InlineeMetaArgCount, TyInt32, callInstr->m_func, /*dontEncode*/ true));
  919. if (returnValueOpnd)
  920. {
  921. StackSym* returnValueSym = returnValueOpnd->m_sym->AsStackSym();
  922. IR::Opnd* dstOpnd = IR::RegOpnd::New(returnValueSym, returnValueSym->GetType(), callInstr->m_func);
  923. dstOpnd->SetValueType(returnValueOpnd->GetValueType());
  924. endCallInstr->SetDst(dstOpnd);
  925. }
  926. callInstr->InsertBefore(endCallInstr);
  927. callInstr->InsertBefore(doneLabel);
  928. callInstr->Remove(); // We don't need callInstr anymore.
  929. }
  930. //
  931. // Inlines a function if it is a polymorphic inlining candidate.
  932. // otherwise introduces a call to it.
  933. // The IR for the args & calls is cloned to do this
  934. //
  935. void Inline::InsertOneInlinee(IR::Instr* callInstr, IR::RegOpnd* returnValueOpnd, IR::Opnd* methodOpnd,
  936. const InlineeData& inlineeData, IR::LabelInstr* doneLabel, const StackSym* symCallerThis, bool fixedFunctionSafeThis, uint recursiveInlineDepth)
  937. {
  938. bool isInlined = inlineeData.inlineeJitTimeData->GetIsInlined();
  939. IR::Instr* currentCallInstr;
  940. if (isInlined)
  941. {
  942. currentCallInstr = IR::Instr::New(Js::OpCode::InlineeStart, IR::RegOpnd::New(TyVar, callInstr->m_func), methodOpnd, callInstr->m_func);
  943. }
  944. else
  945. {
  946. currentCallInstr = IR::Instr::New(callInstr->m_opcode, callInstr->m_func);
  947. currentCallInstr->SetSrc1(methodOpnd);
  948. if (returnValueOpnd)
  949. {
  950. currentCallInstr->SetDst(returnValueOpnd);
  951. }
  952. }
  953. currentCallInstr->SetIsCloned(true);
  954. callInstr->InsertBefore(currentCallInstr);
  955. this->CloneCallSequence(callInstr, currentCallInstr);
  956. if (isInlined)
  957. {
  958. Js::FunctionBody *funcBody = inlineeData.functionBody;
  959. Func *inlinee = BuildInlinee(funcBody, inlineeData, returnValueOpnd ? returnValueOpnd->m_sym->GetByteCodeRegSlot() : Js::Constants::NoRegister, callInstr, recursiveInlineDepth);
  960. IR::Instr *argOuts[Js::InlineeCallInfo::MaxInlineeArgoutCount];
  961. #if DBG
  962. memset(argOuts, 0xFE, sizeof(argOuts));
  963. #endif
  964. bool stackArgsArgOutExpanded = false;
  965. Js::ArgSlot actualCount = MapActuals(currentCallInstr, argOuts, Js::InlineeCallInfo::MaxInlineeArgoutCount, inlinee, (Js::ProfileId)callInstr->AsProfiledInstr()->u.profileId, &stackArgsArgOutExpanded);
  966. Assert(actualCount > 0);
  967. MapFormals(inlinee, argOuts, funcBody->GetInParamsCount(), actualCount, returnValueOpnd, currentCallInstr->GetSrc1(), symCallerThis, stackArgsArgOutExpanded, fixedFunctionSafeThis, argOuts);
  968. currentCallInstr->m_func = inlinee;
  969. // Put the meta arguments that the stack walker expects to find on the stack.
  970. // As all the argouts are shared among the inlinees, do this only once.
  971. SetupInlineeFrame(inlinee, currentCallInstr, actualCount, currentCallInstr->GetSrc1());
  972. IR::Instr* inlineeEndInstr = IR::Instr::New(Js::OpCode::InlineeEnd, inlinee);
  973. inlineeEndInstr->SetByteCodeOffset(inlinee->m_tailInstr->GetPrevRealInstr());
  974. inlineeEndInstr->SetSrc1(IR::IntConstOpnd::New(actualCount + Js::Constants::InlineeMetaArgCount, TyInt32, inlinee));
  975. inlineeEndInstr->SetSrc2(currentCallInstr->GetDst());
  976. inlinee->m_tailInstr->InsertBefore(inlineeEndInstr);
  977. // JMP to done at the end
  978. IR::Instr* doneInstr = IR::BranchInstr::New(Js::OpCode::Br, doneLabel, currentCallInstr->m_func);
  979. inlinee->m_tailInstr->InsertBefore(doneInstr);
  980. currentCallInstr->InsertRangeAfter(inlinee->m_headInstr->m_next, inlinee->m_tailInstr->m_prev);
  981. inlinee->m_headInstr->Free();
  982. inlinee->m_tailInstr->Free();
  983. }
  984. else
  985. {
  986. callInstr->InsertBefore(IR::BranchInstr::New(Js::OpCode::Br, doneLabel, callInstr->m_func));
  987. }
  988. }
  989. uint
  990. Inline::HandleDifferentTypesSameFunction(__inout_ecount(cachedFixedInlineeCount) Js::FixedFieldInfo* fixedFunctionInfoArray, uint16 cachedFixedInlineeCount)
  991. {
  992. uint16 uniqueCount = cachedFixedInlineeCount;
  993. uint16 swapIndex;
  994. for (uint16 i = 0; i < cachedFixedInlineeCount; i++)
  995. {
  996. swapIndex = i+1;
  997. for (uint16 j = i+1; j < cachedFixedInlineeCount; j++)
  998. {
  999. if (fixedFunctionInfoArray[i].fieldValue == fixedFunctionInfoArray[j].fieldValue)
  1000. {
  1001. Js::FixedFieldInfo tmpInfo = fixedFunctionInfoArray[j];
  1002. fixedFunctionInfoArray[j] = fixedFunctionInfoArray[swapIndex];
  1003. fixedFunctionInfoArray[swapIndex] = tmpInfo;
  1004. fixedFunctionInfoArray[swapIndex - 1].nextHasSameFixedField = true;
  1005. swapIndex++;
  1006. uniqueCount--;
  1007. }
  1008. }
  1009. i = swapIndex-1;
  1010. }
  1011. return uniqueCount;
  1012. }
  1013. void
  1014. Inline::SetInlineeFrameStartSym(Func *inlinee, uint actualCount)
  1015. {
  1016. StackSym *stackSym = inlinee->m_symTable->GetArgSlotSym((Js::ArgSlot)actualCount + 1);
  1017. stackSym->m_isInlinedArgSlot = true;
  1018. this->topFunc->SetArgOffset(stackSym, (currentInlineeFrameSlot) * MachPtr);
  1019. inlinee->SetInlineeFrameStartSym(stackSym);
  1020. }
  1021. Func *
  1022. Inline::BuildInlinee(Js::FunctionBody* funcBody, const InlineeData& inlineeData, Js::RegSlot returnRegSlot, IR::Instr *callInstr, uint recursiveInlineDepth)
  1023. {
  1024. Assert(callInstr->IsProfiledInstr());
  1025. Js::ProfileId callSiteId = static_cast<Js::ProfileId>(callInstr->AsProfiledInstr()->u.profileId);
  1026. Js::ProxyEntryPointInfo *defaultEntryPointInfo = funcBody->GetDefaultEntryPointInfo();
  1027. Assert(defaultEntryPointInfo->IsFunctionEntryPointInfo());
  1028. Js::FunctionEntryPointInfo *functionEntryPointInfo = static_cast<Js::FunctionEntryPointInfo*>(defaultEntryPointInfo);
  1029. JsFunctionCodeGen *workItem = JitAnew(this->topFunc->m_alloc, JsFunctionCodeGen,
  1030. funcBody->GetScriptContext()->GetNativeCodeGenerator(), funcBody, functionEntryPointInfo, this->topFunc->IsJitInDebugMode());
  1031. workItem->SetRecyclableData(JitAnew(this->topFunc->m_alloc, Js::CodeGenRecyclableData, inlineeData.inlineeJitTimeData));
  1032. workItem->SetJitMode(this->topFunc->m_workItem->GetJitMode());
  1033. const auto profileInfo =
  1034. JitAnew(
  1035. this->topFunc->m_alloc,
  1036. Js::ReadOnlyDynamicProfileInfo,
  1037. funcBody->HasDynamicProfileInfo() ? funcBody->GetAnyDynamicProfileInfo() : nullptr,
  1038. this->topFunc->IsBackgroundJIT() ? this->topFunc->m_alloc : nullptr);
  1039. Js::EntryPointPolymorphicInlineCacheInfo * entryPointPolymorphicInlineCacheInfo = this->topFunc->m_workItem->GetEntryPoint()->GetPolymorphicInlineCacheInfo();
  1040. Func *inlinee = JitAnew(this->topFunc->m_alloc,
  1041. Func,
  1042. this->topFunc->m_alloc,
  1043. workItem,
  1044. inlineeData.inlineeRuntimeData,
  1045. entryPointPolymorphicInlineCacheInfo ? entryPointPolymorphicInlineCacheInfo->GetInlineeInfo(funcBody) : nullptr,
  1046. this->topFunc->GetCodeGenAllocators(),
  1047. this->topFunc->GetNumberAllocator(),
  1048. profileInfo,
  1049. this->topFunc->GetCodeGenProfiler(),
  1050. this->topFunc->IsBackgroundJIT(),
  1051. callInstr->m_func,
  1052. callInstr->m_next->GetByteCodeOffset(),
  1053. returnRegSlot,
  1054. false,
  1055. callSiteId,
  1056. false);
  1057. BuildIRForInlinee(inlinee, funcBody, callInstr, false, recursiveInlineDepth);
  1058. return inlinee;
  1059. }
  1060. void
  1061. Inline::BuildIRForInlinee(Func *inlinee, Js::FunctionBody *funcBody, IR::Instr *callInstr, bool isApplyTarget, uint recursiveInlineDepth)
  1062. {
  1063. Js::ArgSlot actualsCount = 0;
  1064. IR::Instr *argOuts[Js::InlineeCallInfo::MaxInlineeArgoutCount];
  1065. #if DBG
  1066. memset(argOuts, 0xFE, sizeof(argOuts));
  1067. #endif
  1068. callInstr->IterateArgInstrs([&](IR::Instr* argInstr){
  1069. StackSym *argSym = argInstr->GetDst()->AsSymOpnd()->m_sym->AsStackSym();
  1070. argOuts[argSym->GetArgSlotNum() - 1] = argInstr;
  1071. actualsCount++;
  1072. return false;
  1073. });
  1074. inlinee->actualCount = actualsCount;
  1075. inlinee->m_symTable = this->topFunc->m_symTable;
  1076. inlinee->m_symTable->SetIDAdjustment();
  1077. inlinee->m_symTable->IncreaseStartingID(funcBody->GetLocalsCount());
  1078. BEGIN_CODEGEN_PHASE(this->topFunc, Js::IRBuilderPhase);
  1079. IRBuilder irBuilder(inlinee);
  1080. irBuilder.Build();
  1081. END_CODEGEN_PHASE_NO_DUMP(this->topFunc, Js::IRBuilderPhase);
  1082. inlinee->m_symTable->ClearIDAdjustment();
  1083. Inline recursiveInliner(this->topFunc, this->inliningHeuristics, this->isInLoop, currentInlineeFrameSlot + Js::Constants::InlineeMetaArgCount + actualsCount, isApplyTarget);
  1084. recursiveInliner.Optimize(inlinee, argOuts, actualsCount, inlinee->GetJnFunction() == callInstr->m_func->GetJnFunction() ? recursiveInlineDepth + 1 : 0);
  1085. #ifdef DBG
  1086. Js::ArgSlot formalCount = funcBody->GetInParamsCount();
  1087. if (formalCount > Js::InlineeCallInfo::MaxInlineeArgoutCount)
  1088. {
  1089. Fatal();
  1090. }
  1091. #endif
  1092. }
  1093. bool
  1094. Inline::TryOptimizeCallInstrWithFixedMethod(IR::Instr *callInstr, Js::FunctionInfo* functionInfo, bool isPolymorphic, bool isBuiltIn, bool isCtor, bool isInlined, bool &safeThis,
  1095. bool dontOptimizeJustCheck, uint i /*i-th inlinee at a polymorphic call site*/)
  1096. {
  1097. Assert(!callInstr->m_func->GetJnFunction()->GetHasTry());
  1098. if (PHASE_OFF(Js::FixedMethodsPhase, callInstr->m_func->GetJnFunction()))
  1099. {
  1100. return false;
  1101. }
  1102. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  1103. #define TRACE_FIXED_FIELDS 1
  1104. #endif
  1105. #if TRACE_FIXED_FIELDS
  1106. wchar_t debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  1107. wchar_t debugStringBuffer2[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  1108. bool printFixedFieldsTrace =
  1109. ((PHASE_TRACE(Js::FixedMethodsPhase, callInstr->m_func->GetJnFunction()) || PHASE_TESTTRACE(Js::FixedMethodsPhase, callInstr->m_func->GetJnFunction()) ||
  1110. (isCtor && PHASE_TRACE(Js::FixedNewObjPhase, callInstr->m_func->GetJnFunction()) || PHASE_TESTTRACE(Js::FixedNewObjPhase, callInstr->m_func->GetJnFunction()))) && !dontOptimizeJustCheck);
  1111. if (printFixedFieldsTrace)
  1112. {
  1113. Js::FunctionBody* callerFunctionBody = callInstr->m_func->GetJnFunction();
  1114. Js::FunctionBody* calleeFunctionBody = functionInfo != nullptr && functionInfo->HasBody() ? functionInfo->GetFunctionBody() : nullptr;
  1115. const wchar_t* calleeName = calleeFunctionBody != nullptr ? calleeFunctionBody->GetDisplayName() : L"<unknown>";
  1116. Output::Print(L"FixedFields: function %s (%s): considering method <unknown> (%s %s): polymorphic = %d, built-in = %d, ctor = %d, inlined = %d, functionInfo = %p.\n",
  1117. callerFunctionBody->GetDisplayName(), callerFunctionBody->GetDebugNumberSet(debugStringBuffer), calleeName,
  1118. calleeFunctionBody ? calleeFunctionBody->GetDebugNumberSet(debugStringBuffer2) : L"(null)",
  1119. isPolymorphic, isBuiltIn, isCtor, isInlined, functionInfo);
  1120. Output::Flush();
  1121. }
  1122. #endif
  1123. if (isPolymorphic && isInlined)
  1124. {
  1125. Assert(dontOptimizeJustCheck);
  1126. }
  1127. StackSym* methodValueSym = callInstr->GetSrc1()->AsRegOpnd()->m_sym->AsStackSym();
  1128. if (!methodValueSym->IsSingleDef())
  1129. {
  1130. #if TRACE_FIXED_FIELDS
  1131. if (printFixedFieldsTrace)
  1132. {
  1133. Js::FunctionBody* callerFunctionBody = callInstr->m_func->GetJnFunction();
  1134. Js::FunctionBody* calleeFunctionBody = functionInfo != nullptr && functionInfo->HasBody() ? functionInfo->GetFunctionBody() : nullptr;
  1135. const wchar_t* calleeName = calleeFunctionBody != nullptr ? calleeFunctionBody->GetDisplayName() : L"<unknown>";
  1136. Output::Print(L"FixedFields: function %s (%s): %s non-fixed method <unknown> (%s %s), because callee is not single def.\n",
  1137. callerFunctionBody->GetDisplayName(), callerFunctionBody->GetDebugNumberSet(debugStringBuffer),
  1138. functionInfo != nullptr ? L"inlining" : L"calling", calleeName,
  1139. calleeFunctionBody ? calleeFunctionBody->GetDebugNumberSet(debugStringBuffer2) : L"(null)");
  1140. Output::Flush();
  1141. }
  1142. #endif
  1143. return false;
  1144. }
  1145. IR::Instr* ldMethodFldInstr = methodValueSym->GetInstrDef();
  1146. if (ldMethodFldInstr->m_opcode != Js::OpCode::ScopedLdMethodFld
  1147. && ldMethodFldInstr->m_opcode != Js::OpCode::LdRootMethodFld
  1148. && ldMethodFldInstr->m_opcode != Js::OpCode::LdMethodFld
  1149. && ldMethodFldInstr->m_opcode != Js::OpCode::LdRootFld
  1150. && ldMethodFldInstr->m_opcode != Js::OpCode::LdFld
  1151. && ldMethodFldInstr->m_opcode != Js::OpCode::LdFldForCallApplyTarget
  1152. && ldMethodFldInstr->m_opcode != Js::OpCode::LdMethodFromFlags)
  1153. {
  1154. #if TRACE_FIXED_FIELDS
  1155. if (printFixedFieldsTrace)
  1156. {
  1157. Js::FunctionBody* callerFunctionBody = callInstr->m_func->GetJnFunction();
  1158. Js::FunctionBody* calleeFunctionBody = functionInfo != nullptr && functionInfo->HasBody() ? functionInfo->GetFunctionBody() : nullptr;
  1159. const wchar_t* calleeName = calleeFunctionBody != nullptr ? calleeFunctionBody->GetDisplayName() : L"<unknown>";
  1160. Output::Print(L"FixedFields: function %s (%s): %s non-fixed method <unknown> (%s %s), because callee does not come from LdMethodFld.\n",
  1161. callerFunctionBody->GetDisplayName(), callerFunctionBody->GetDebugNumberSet(debugStringBuffer),
  1162. functionInfo != nullptr ? L"inlining" : L"calling", calleeName,
  1163. calleeFunctionBody ? calleeFunctionBody->GetDebugNumberSet(debugStringBuffer2) : L"(null)");
  1164. Output::Flush();
  1165. }
  1166. #endif
  1167. return false;
  1168. }
  1169. IR::PropertySymOpnd* methodPropertyOpnd = ldMethodFldInstr->GetSrc1()->AsPropertySymOpnd();
  1170. if ((isCtor &&
  1171. ((isInlined && PHASE_OFF(Js::FixedCtorInliningPhase, callInstr->m_func->GetJnFunction())) ||
  1172. (!isInlined && PHASE_OFF(Js::FixedCtorCallsPhase, callInstr->m_func->GetJnFunction())) ||
  1173. (methodPropertyOpnd->UsesAccessor()))) ||
  1174. (!isCtor &&
  1175. ((isBuiltIn &&
  1176. ((isInlined && PHASE_OFF(Js::FixedBuiltInMethodInliningPhase, callInstr->m_func->GetJnFunction())) ||
  1177. (!isInlined && PHASE_OFF(Js::FixedBuiltInMethodCallsPhase, callInstr->m_func->GetJnFunction())))) ||
  1178. (!isBuiltIn &&
  1179. ((isInlined && PHASE_OFF(Js::FixedScriptMethodInliningPhase, callInstr->m_func->GetJnFunction())) ||
  1180. (!isInlined && !PHASE_ON(Js::FixedScriptMethodCallsPhase, callInstr->m_func->GetJnFunction()))))))
  1181. )
  1182. {
  1183. #if TRACE_FIXED_FIELDS
  1184. if (printFixedFieldsTrace)
  1185. {
  1186. Js::FunctionBody* callerFunctionBody = callInstr->m_func->GetJnFunction();
  1187. Js::FunctionBody* calleeFunctionBody = functionInfo != nullptr && functionInfo->HasBody() ? functionInfo->GetFunctionBody() : nullptr;
  1188. const wchar_t* calleeName = calleeFunctionBody != nullptr ? calleeFunctionBody->GetDisplayName() : L"<unknown>";
  1189. Js::PropertyId methodPropertyId = callerFunctionBody->GetPropertyIdFromCacheId(methodPropertyOpnd->m_inlineCacheIndex);
  1190. Js::PropertyRecord const * const methodPropertyRecord = callerFunctionBody->GetScriptContext()->GetPropertyNameLocked(methodPropertyId);
  1191. Output::Print(L"FixedFields: function %s (#%u): %s non-fixed method %s (%s #%u) (cache id: %d), because %s fixed %s %s is disabled.\n",
  1192. callerFunctionBody->GetDisplayName(), callerFunctionBody->GetDebugNumberSet(debugStringBuffer),
  1193. functionInfo != nullptr ? L"inlining" : L"calling", methodPropertyRecord->GetBuffer(), calleeName,
  1194. calleeFunctionBody ? calleeFunctionBody->GetDebugNumberSet(debugStringBuffer2) : L"(null)",
  1195. methodPropertyOpnd->m_inlineCacheIndex, isInlined ? L"inlining" : L"calling", isBuiltIn ? L"built-in" : L"script",
  1196. isCtor ? L"ctors" : L"methods");
  1197. Output::Flush();
  1198. }
  1199. #endif
  1200. return false;
  1201. }
  1202. if (!methodPropertyOpnd->IsObjTypeSpecCandidate() && !methodPropertyOpnd->IsRootObjectNonConfigurableFieldLoad())
  1203. {
  1204. #if TRACE_FIXED_FIELDS
  1205. if (printFixedFieldsTrace)
  1206. {
  1207. Js::FunctionBody* callerFunctionBody = callInstr->m_func->GetJnFunction();
  1208. Js::FunctionBody* calleeFunctionBody = functionInfo != nullptr && functionInfo->HasBody() ? functionInfo->GetFunctionBody() : nullptr;
  1209. const wchar_t* calleeName = calleeFunctionBody != nullptr ? calleeFunctionBody->GetDisplayName() : L"<unknown>";
  1210. Js::PropertyId methodPropertyId = callerFunctionBody->GetPropertyIdFromCacheId(methodPropertyOpnd->m_inlineCacheIndex);
  1211. Js::PropertyRecord const * const methodPropertyRecord = callerFunctionBody->GetScriptContext()->GetPropertyNameLocked(methodPropertyId);
  1212. Output::Print(L"FixedFields: function %s (%s): %s non-fixed method %s (%s %s) (cache id: %d), because inline cache has no cached type.\n",
  1213. callerFunctionBody->GetDisplayName(), callerFunctionBody->GetDebugNumberSet(debugStringBuffer),
  1214. functionInfo != nullptr ? L"inlining" : L"calling", methodPropertyRecord->GetBuffer(), calleeName,
  1215. calleeFunctionBody ? calleeFunctionBody->GetDebugNumberSet(debugStringBuffer2) : L"(null)",
  1216. methodPropertyOpnd->m_inlineCacheIndex);
  1217. Output::Flush();
  1218. }
  1219. #endif
  1220. return false;
  1221. }
  1222. Js::JavascriptFunction const * functionObject = nullptr;
  1223. if (!isPolymorphic)
  1224. {
  1225. functionObject = methodPropertyOpnd->HasFixedValue() ? methodPropertyOpnd->GetFieldValueAsFixedFunction() : nullptr;
  1226. }
  1227. else if (isPolymorphic && isInlined)
  1228. {
  1229. functionObject = methodPropertyOpnd->HasFixedValue() ? methodPropertyOpnd->GetFieldValueAsFixedFunction(i) : nullptr;
  1230. }
  1231. if (!functionObject)
  1232. {
  1233. #if TRACE_FIXED_FIELDS
  1234. if (printFixedFieldsTrace)
  1235. {
  1236. Js::FunctionBody* callerFunctionBody = callInstr->m_func->GetJnFunction();
  1237. Js::FunctionBody* calleeFunctionBody = functionInfo != nullptr && functionInfo->HasBody() ? functionInfo->GetFunctionBody() : nullptr;
  1238. const wchar_t* calleeName = calleeFunctionBody != nullptr ? calleeFunctionBody->GetDisplayName() : L"<unknown>";
  1239. Js::PropertyId methodPropertyId = callerFunctionBody->GetPropertyIdFromCacheId(methodPropertyOpnd->m_inlineCacheIndex);
  1240. Js::PropertyRecord const * const methodPropertyRecord = callerFunctionBody->GetScriptContext()->GetPropertyNameLocked(methodPropertyId);
  1241. Output::Print(L"FixedFields: function %s (%s): %s non-fixed method %s (%s %s) (cache id: %d, layout: %s), because inline cache has no fixed function object.\n",
  1242. callerFunctionBody->GetDisplayName(), callerFunctionBody->GetDebugNumberSet(debugStringBuffer),
  1243. functionInfo != nullptr ? L"inlining" : L"calling", methodPropertyRecord->GetBuffer(), calleeName,
  1244. calleeFunctionBody ? calleeFunctionBody->GetDebugNumberSet(debugStringBuffer2) : L"(null)",
  1245. methodPropertyOpnd->m_inlineCacheIndex,
  1246. methodPropertyOpnd->IsLoadedFromProto() ? L"proto" : methodPropertyOpnd->UsesAccessor() ? L"accessor" : L"local");
  1247. Output::Flush();
  1248. }
  1249. #endif
  1250. return false;
  1251. }
  1252. // Certain built-ins that we decide not to inline will get a fast path emitted by the lowerer.
  1253. // The lowering code cannot handle a call with a fixed function target, because it needs access to
  1254. // the original property sym. Turn off fixed method calls for these cases.
  1255. if (functionInfo == nullptr && Func::IsBuiltInInlinedInLowerer(callInstr->GetSrc1()))
  1256. {
  1257. #if TRACE_FIXED_FIELDS
  1258. if (printFixedFieldsTrace)
  1259. {
  1260. Js::FunctionBody* callerFunctionBody = callInstr->m_func->GetJnFunction();
  1261. Js::FunctionBody* calleeFunctionBody = functionInfo != nullptr && functionInfo->HasBody() ? functionInfo->GetFunctionBody() : nullptr;
  1262. const wchar_t* calleeName = calleeFunctionBody != nullptr ? calleeFunctionBody->GetDisplayName() : L"<unknown>";
  1263. Js::PropertyId methodPropertyId = callerFunctionBody->GetPropertyIdFromCacheId(methodPropertyOpnd->m_inlineCacheIndex);
  1264. Js::PropertyRecord const * const methodPropertyRecord = callerFunctionBody->GetScriptContext()->GetPropertyNameLocked(methodPropertyId);
  1265. Output::Print(L"FixedFields: function %s (%s): %s non-fixed method %s (%s %s) (cache id: %d, layout: %s), because callee is a built-in with fast path in lowerer.\n",
  1266. callerFunctionBody->GetDisplayName(), callerFunctionBody->GetDebugNumberSet(debugStringBuffer),
  1267. functionInfo != nullptr ? L"inlining" : L"calling", methodPropertyRecord->GetBuffer(), calleeName,
  1268. calleeFunctionBody ? calleeFunctionBody->GetDebugNumberSet(debugStringBuffer2) : L"(null)",
  1269. methodPropertyOpnd->m_inlineCacheIndex,
  1270. methodPropertyOpnd->IsLoadedFromProto() ? L"proto" : methodPropertyOpnd->UsesAccessor() ? L"accessor" : L"local");
  1271. Output::Flush();
  1272. }
  1273. #endif
  1274. return false;
  1275. }
  1276. if (functionInfo != nullptr && functionObject->GetFunctionInfo() != functionInfo)
  1277. {
  1278. #if TRACE_FIXED_FIELDS
  1279. if (printFixedFieldsTrace)
  1280. {
  1281. wchar_t debugStringBuffer3[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  1282. Js::FunctionBody* callerFunctionBody = callInstr->m_func->GetJnFunction();
  1283. Js::PropertyId methodPropertyId = callerFunctionBody->GetPropertyIdFromCacheId(methodPropertyOpnd->m_inlineCacheIndex);
  1284. Js::PropertyRecord const * const methodPropertyRecord = callerFunctionBody->GetScriptContext()->GetPropertyNameLocked(methodPropertyId);
  1285. bool isProto = methodPropertyOpnd->IsLoadedFromProto();
  1286. bool isAccessor = methodPropertyOpnd->UsesAccessor();
  1287. Js::DynamicObject* protoObject = isProto ? methodPropertyOpnd->GetProtoObject() : nullptr;
  1288. Js::FunctionBody* fixedFunctionBody = functionObject->GetFunctionInfo()->GetFunctionBody();
  1289. const wchar_t* fixedFunctionNumbers = fixedFunctionBody ? fixedFunctionBody->GetDebugNumberSet(debugStringBuffer2) : L"(null)";
  1290. Js::FunctionBody* profileFunctionBody = functionInfo->GetFunctionBody();
  1291. const wchar_t* profileFunctionName = profileFunctionBody != nullptr ? profileFunctionBody->GetDisplayName() : L"<unknown>";
  1292. const wchar_t* profileFunctionNumbers = profileFunctionBody ? profileFunctionBody->GetDebugNumberSet(debugStringBuffer3) : L"(null)";
  1293. if (PHASE_TRACE(Js::FixedMethodsPhase, callInstr->m_func->GetJnFunction()))
  1294. {
  1295. Output::Print(L"FixedFields: function %s (#%s): function body mismatch for inlinee: %s (%s) 0x%p->0x%p != %s (%s) 0x%p (cache id: %d, layout: %s, type: 0x%p, proto: 0x%p, proto type: 0x%p).\n",
  1296. callerFunctionBody->GetDisplayName(), callerFunctionBody->GetDebugNumberSet(debugStringBuffer),
  1297. methodPropertyRecord->GetBuffer(), fixedFunctionNumbers, functionObject, functionObject->GetFunctionInfo(),
  1298. profileFunctionName, profileFunctionNumbers, functionInfo,
  1299. methodPropertyOpnd->m_inlineCacheIndex, isProto ? L"proto" : isAccessor ? L"accessor" : L"local",
  1300. methodPropertyOpnd->GetType(), protoObject, protoObject != nullptr ? protoObject->GetType() : nullptr);
  1301. }
  1302. if (PHASE_TESTTRACE(Js::FixedMethodsPhase, callInstr->m_func->GetJnFunction()))
  1303. {
  1304. Output::Print(L"FixedFields: function %s (%s): function body mismatch for inlinee: %s (%s) != %s (%s) (cache id: %d, layout: %s).\n",
  1305. callerFunctionBody->GetDisplayName(), callerFunctionBody->GetDebugNumberSet(debugStringBuffer),
  1306. methodPropertyRecord->GetBuffer(), fixedFunctionNumbers, profileFunctionName, profileFunctionNumbers,
  1307. methodPropertyOpnd->m_inlineCacheIndex, isProto ? L"proto" : isAccessor ? L"accessor" : L"local");
  1308. }
  1309. Output::Flush();
  1310. }
  1311. #endif
  1312. // It appears that under certain bailout and re-JIT conditions we may end up with an updated
  1313. // inline cache pointing to a new function object, while the call site profile info still
  1314. // holds the old function body. If the two don't match, let's fall back on the regular LdMethodFld.
  1315. return false;
  1316. }
  1317. else
  1318. {
  1319. #if TRACE_FIXED_FIELDS
  1320. if (printFixedFieldsTrace)
  1321. {
  1322. Js::FunctionBody* callerFunctionBody = callInstr->m_func->GetJnFunction();
  1323. Js::FunctionBody* calleeFunctionBody = functionInfo != nullptr && functionInfo->HasBody() ? functionInfo->GetFunctionBody() : nullptr;
  1324. Js::PropertyId methodPropertyId = callerFunctionBody->GetPropertyIdFromCacheId(methodPropertyOpnd->m_inlineCacheIndex);
  1325. Js::PropertyRecord const * const methodPropertyRecord = callerFunctionBody->GetScriptContext()->GetPropertyNameLocked(methodPropertyId);
  1326. const wchar_t* fixedFunctionName = calleeFunctionBody != nullptr ? calleeFunctionBody->GetDisplayName() : L"<unknown>";
  1327. Js::FunctionBody* fixedFunctionBody = functionObject->GetFunctionInfo()->GetFunctionBody();
  1328. const wchar_t* fixedFunctionNumbers = fixedFunctionBody ? fixedFunctionBody->GetDebugNumberSet(debugStringBuffer2) : L"(null)";
  1329. Output::Print(L"FixedFields: function %s (%s): %s fixed method %s (%s %s) (cache id: %d, layout: %s).\n",
  1330. callerFunctionBody->GetDisplayName(), callerFunctionBody->GetDebugNumberSet(debugStringBuffer),
  1331. functionInfo != nullptr ? L"inlining" : L"calling",
  1332. methodPropertyRecord->GetBuffer(), fixedFunctionName, fixedFunctionNumbers,
  1333. methodPropertyOpnd->m_inlineCacheIndex,
  1334. methodPropertyOpnd->IsLoadedFromProto() ? L"proto" : methodPropertyOpnd->UsesAccessor() ? L"accessor" : L"local");
  1335. Output::Flush();
  1336. }
  1337. #endif
  1338. }
  1339. #undef TRACE_FIXED_FIELDS
  1340. if (dontOptimizeJustCheck)
  1341. {
  1342. return true;
  1343. }
  1344. // Change Ld[Root]MethodFld, LdMethodFromFlags to CheckFixedFld, which doesn't need a dst.
  1345. if(ldMethodFldInstr->m_opcode == Js::OpCode::LdMethodFromFlags)
  1346. {
  1347. Assert(ldMethodFldInstr->HasBailOutInfo());
  1348. ldMethodFldInstr->ClearBailOutInfo();
  1349. }
  1350. ldMethodFldInstr->m_opcode = Js::OpCode::CheckFixedFld;
  1351. IR::Opnd * methodValueDstOpnd = ldMethodFldInstr->UnlinkDst();
  1352. IR::Instr * chkMethodFldInstr = ldMethodFldInstr->ConvertToBailOutInstr(ldMethodFldInstr,
  1353. !methodPropertyOpnd->HasEquivalentTypeSet() ? IR::BailOutFailedFixedFieldTypeCheck : IR::BailOutFailedEquivalentFixedFieldTypeCheck);
  1354. chkMethodFldInstr->GetBailOutInfo()->polymorphicCacheIndex = methodPropertyOpnd->m_inlineCacheIndex;
  1355. Assert(chkMethodFldInstr->GetSrc1()->IsSymOpnd());
  1356. if (chkMethodFldInstr->GetSrc1()->AsSymOpnd()->IsPropertySymOpnd())
  1357. {
  1358. Assert(chkMethodFldInstr->m_opcode == Js::OpCode::CheckFixedFld);
  1359. IR::PropertySymOpnd* chkMethodFldOpnd = chkMethodFldInstr->GetSrc1()->AsPropertySymOpnd();
  1360. // For polymorphic field loads we only support fixed functions on prototypes. This helps keep the equivalence check helper simple.
  1361. Assert(chkMethodFldOpnd->IsMono() || chkMethodFldOpnd->IsLoadedFromProto() || chkMethodFldOpnd->UsesAccessor());
  1362. chkMethodFldOpnd->SetUsesFixedValue(true);
  1363. }
  1364. if (isCtor)
  1365. {
  1366. Js::JitTimeConstructorCache* constructorCache = methodPropertyOpnd->GetCtorCache();
  1367. if (constructorCache != nullptr && callInstr->IsProfiledInstr())
  1368. {
  1369. #if ENABLE_DEBUG_CONFIG_OPTIONS
  1370. if (PHASE_TRACE(Js::FixedNewObjPhase, callInstr->m_func->GetJnFunction()) || PHASE_TESTTRACE(Js::FixedNewObjPhase, callInstr->m_func->GetJnFunction()))
  1371. {
  1372. Js::FunctionBody* callerFunctionBody = callInstr->m_func->GetJnFunction();
  1373. Js::FunctionBody* calleeFunctionBody = functionInfo != nullptr && functionInfo->HasBody() ? functionInfo->GetFunctionBody() : nullptr;
  1374. Js::PropertyId methodPropertyId = callerFunctionBody->GetPropertyIdFromCacheId(methodPropertyOpnd->m_inlineCacheIndex);
  1375. Js::PropertyRecord const * const methodPropertyRecord = callerFunctionBody->GetScriptContext()->GetPropertyNameLocked(methodPropertyId);
  1376. const wchar_t* fixedFunctionName = calleeFunctionBody != nullptr ? calleeFunctionBody->GetDisplayName() : L"<unknown>";
  1377. Js::FunctionBody* fixedFunctionBody = functionObject->GetFunctionInfo()->GetFunctionBody();
  1378. const wchar_t* fixedFunctionNumbers = fixedFunctionBody ? fixedFunctionBody->GetDebugNumberSet(debugStringBuffer2) : L"(null)";
  1379. Output::Print(L"FixedNewObj: function %s (%s): fixed new object for %s with %s ctor %s (%s %s)%s\n",
  1380. callerFunctionBody->GetDisplayName(), callerFunctionBody->GetDebugNumberSet(debugStringBuffer), Js::OpCodeUtil::GetOpCodeName(callInstr->m_opcode),
  1381. functionInfo != nullptr ? L"inlined" : L"called",
  1382. methodPropertyRecord->GetBuffer(), fixedFunctionName, fixedFunctionNumbers,
  1383. constructorCache->skipNewScObject ? L" skip default object" : L"");
  1384. Output::Flush();
  1385. }
  1386. #endif
  1387. // The profile ID's hung from array ctor opcodes don't match up with normal profiled call sites.
  1388. if (callInstr->m_opcode != Js::OpCode::NewScObjArray)
  1389. {
  1390. // Because we are storing flow sensitive info in the cache (guarded property operations),
  1391. // we must make sure the same cache cannot be used multiple times in the flow.
  1392. if (constructorCache->isUsed)
  1393. {
  1394. // It's okay to allocate a JitTimeConstructorCache from the func's allocator (rather than recycler),
  1395. // because we only use these during JIT. We use the underlying runtime cache as a guard that must
  1396. // live after JIT, and these are added to the EntryPointInfo during work item creation and thus kept alive.
  1397. constructorCache = constructorCache->Clone(this->topFunc->m_alloc);
  1398. }
  1399. Assert(!constructorCache->isUsed);
  1400. constructorCache->isUsed = true;
  1401. callInstr->m_func->SetConstructorCache(static_cast<Js::ProfileId>(callInstr->AsProfiledInstr()->u.profileId), constructorCache);
  1402. }
  1403. }
  1404. else
  1405. {
  1406. #if ENABLE_DEBUG_CONFIG_OPTIONS
  1407. if (PHASE_TRACE(Js::FixedNewObjPhase, callInstr->m_func->GetJnFunction()) || PHASE_TESTTRACE(Js::FixedNewObjPhase, callInstr->m_func->GetJnFunction()))
  1408. {
  1409. Js::FunctionBody* callerFunctionBody = callInstr->m_func->GetJnFunction();
  1410. Js::FunctionBody* calleeFunctionBody = functionInfo != nullptr && functionInfo->HasBody() ? functionInfo->GetFunctionBody() : nullptr;
  1411. Js::PropertyId methodPropertyId = callerFunctionBody->GetPropertyIdFromCacheId(methodPropertyOpnd->m_inlineCacheIndex);
  1412. Js::PropertyRecord const * const methodPropertyRecord = callerFunctionBody->GetScriptContext()->GetPropertyNameLocked(methodPropertyId);
  1413. const wchar_t* fixedFunctionName = calleeFunctionBody != nullptr ? calleeFunctionBody->GetDisplayName() : L"<unknown>";
  1414. Js::FunctionBody* fixedFunctionBody = functionObject->GetFunctionInfo()->GetFunctionBody();
  1415. const wchar_t* fixedFunctionNumbers = fixedFunctionBody ? fixedFunctionBody->GetDebugNumberSet(debugStringBuffer2) : L"(null)";
  1416. Output::Print(L"FixedNewObj: function %s (%s): non-fixed new object for %s with %s ctor %s (%s %s), because %s.\n",
  1417. callerFunctionBody->GetDisplayName(), callerFunctionBody->GetDebugNumberSet(debugStringBuffer), Js::OpCodeUtil::GetOpCodeName(callInstr->m_opcode),
  1418. functionInfo != nullptr ? L"inlined" : L"called",
  1419. methodPropertyRecord->GetBuffer(), fixedFunctionName, fixedFunctionNumbers,
  1420. constructorCache == nullptr ? L"constructor cache hasn't been cloned" : L"instruction isn't profiled");
  1421. Output::Flush();
  1422. }
  1423. #endif
  1424. }
  1425. }
  1426. // Insert a load instruction to place the constant address in methodOpnd (the Ld[Root]MethodFld's original dst).
  1427. IR::AddrOpnd * constMethodValueOpnd = IR::AddrOpnd::New((Js::Var)functionObject, IR::AddrOpndKind::AddrOpndKindDynamicVar, callInstr->m_func);
  1428. constMethodValueOpnd->m_isFunction = true;
  1429. IR::Instr * ldMethodValueInstr = IR::Instr::New(Js::OpCode::Ld_A, methodValueDstOpnd, constMethodValueOpnd, callInstr->m_func);
  1430. StackSym* methodSym = methodValueDstOpnd->AsRegOpnd()->m_sym;
  1431. if (methodSym->IsSingleDef())
  1432. {
  1433. methodSym->SetIsConst();
  1434. }
  1435. methodValueDstOpnd->SetValueType(ValueType::FromObject((Js::RecyclableObject* const)functionObject));
  1436. chkMethodFldInstr->InsertAfter(ldMethodValueInstr);
  1437. callInstr->ReplaceSrc1(constMethodValueOpnd);
  1438. if (callInstr->m_opcode == Js::OpCode::CallI || callInstr->CallsAccessor(methodPropertyOpnd))
  1439. {
  1440. callInstr->m_opcode = Js::OpCode::CallIFixed;
  1441. }
  1442. else
  1443. {
  1444. // We patch later for constructor inlining.
  1445. Assert(
  1446. callInstr->m_opcode == Js::OpCode::NewScObject ||
  1447. callInstr->m_opcode == Js::OpCode::NewScObjArray);
  1448. }
  1449. if (!isBuiltIn && isInlined)
  1450. {
  1451. // We eliminate CheckThis for fixed method inlining. Assert here that our assumption is true.
  1452. Js::TypeId typeId = methodPropertyOpnd->IsRootObjectNonConfigurableField() ?
  1453. Js::TypeIds_GlobalObject : methodPropertyOpnd->GetTypeId();
  1454. if(typeId > Js::TypeIds_LastJavascriptPrimitiveType && typeId <= Js::TypeIds_LastTrueJavascriptObjectType)
  1455. {
  1456. // Eliminate CheckThis for inlining.
  1457. safeThis = true;
  1458. }
  1459. }
  1460. return true;
  1461. }
  1462. Js::Var
  1463. Inline::TryOptimizeInstrWithFixedDataProperty(IR::Instr *&instr)
  1464. {
  1465. if (PHASE_OFF(Js::UseFixedDataPropsPhase, instr->m_func->GetJnFunction()) ||
  1466. PHASE_OFF(Js::UseFixedDataPropsInInlinerPhase, instr->m_func->GetJnFunction()))
  1467. {
  1468. return nullptr;
  1469. }
  1470. if (!instr->IsProfiledInstr() ||
  1471. !instr->GetSrc1()->IsSymOpnd() || !instr->GetSrc1()->AsSymOpnd()->IsPropertySymOpnd())
  1472. {
  1473. return nullptr;
  1474. }
  1475. if (!OpCodeAttr::CanLoadFixedFields(instr->m_opcode))
  1476. {
  1477. return nullptr;
  1478. }
  1479. return instr->TryOptimizeInstrWithFixedDataProperty(&instr, nullptr);
  1480. }
  1481. // Inline a built-in/math function call, such as Math.sin(x).
  1482. // Main idea on what happens with IR during different stages.
  1483. // 1) Copy args from ArgOuts into inline instr.
  1484. // 2) Change opcode: ArgOut_A -> ArgOut_A_InlineBuiltIn (aka BIA).
  1485. // 3) Notes:
  1486. // - General logic is similar to inlining regular functions, except that:
  1487. // - There are no inner instructions to inline.
  1488. // - We don't need to support arguments object inside the inlinee - don't need inlinee meta frame, etc.
  1489. // - ArgOuts are linked through src2->m_sym->m_instrDef.
  1490. // - ArgOuts are not needed for the inlined call itself, but we can't remove them because they are needed for bailout.
  1491. // We convert them to ArgOut_A_InlineBuiltIn.
  1492. // Example for Math.pow(x, y), x86 case.
  1493. // Original:
  1494. // instrS: dstS = StartCall <N=count>, NULL -- N is actual number of parameters, including "this".
  1495. // instr0: arg0 = ArgOut t, link(->instrS) -- "this" arg
  1496. // instr1: arg1 = ArgOut x, link(->instr0) -- src1
  1497. // instr2: arg2 = ArgOut y, link(->instr1) -- src2
  1498. // instr3: dstC = CallI fn, link(->instr2) -- links to instr2, etc.
  1499. // After Inline:
  1500. // instrS: dstS = StartCall <N=count>, NULL -- N is actual number of parameters, including "this".
  1501. // tmpt = BytecodeArgOutCapture t -- create assigns to temps to snapshot argout values in case they are modified later before the call
  1502. // tmpx = BytecodeArgOutCapture x
  1503. // tmpy = BytecodeArgOutCapture y
  1504. // instr1: arg1 = ArgOut_InlineBuiltIn tmpx, link(->instr0) -- src1
  1505. // instr0: arg0 = ArgOut_InlineBuiltIn tmpt, link(->instrS) -- "this" arg -- Change ArgOut_a to ArgOut_A_InlineBuiltIn
  1506. // instr2: arg2 = ArgOut_InlineBuiltIn tmpy, link(->instr1) -- src2
  1507. // NULL = InlineBuiltInStart fn, link(->instr2)
  1508. // dstC = InlineMathPow, tmpx, tmpy -- actual native math call.
  1509. // NULL = InlineBuiltInEnd <N=count>, link(->instr2)
  1510. // After Globopt:
  1511. // instrS: dstS = StartCall <N=count>, NULL -- N is actual number of parameters, including "this".
  1512. // tmpt = BytecodeArgOutCapture t -- create assigns to temps to snapshot argout values in case they are modified later before the call
  1513. // tmpx = BytecodeArgOutCapture x
  1514. // Bailout 1
  1515. // tmpy = BytecodeArgOutCapture y
  1516. // Bailout 2
  1517. // instr1: arg1 = ArgOut_InlineBuiltIn tmpx, link(->instr0) -- src1
  1518. // instr0: arg0 = ArgOut_InlineBuiltIn tmpt, link(->instrS) -- "this" arg -- Change ArgOut_a to ArgOut_A_InlineBuiltIn
  1519. // instr2: arg2 = ArgOut_InlineBuiltIn tmpy, link(->instr1) -- src2
  1520. // ...
  1521. // NULL = InlineBuiltInStart fn, link(->instr2) -- Note that InlineBuiltInStart is after last bailout.
  1522. // This is important so that fn used for bailout is after last bailout.
  1523. // dstC = InlineMathPow, tmpx, tmpy -- actual native math call.
  1524. // NULL = InlineBuiltInEnd <N=count>, link(->instr2)
  1525. // After Lowerer:
  1526. // ...
  1527. // s1(XMM0) = MOVSD tmpx
  1528. // s2(XMM1) = MOVSD tmpy
  1529. // s1(XMM0) = CALL pow -- actual native math call.
  1530. // dstC = MOVSD s1(XMM0)
  1531. IR::Instr *
  1532. Inline::InlineBuiltInFunction(IR::Instr *callInstr, Js::FunctionInfo *funcInfo, Js::OpCode inlineCallOpCode, const Js::FunctionCodeGenJitTimeData* inlinerData, const StackSym *symCallerThis, bool* pIsInlined, uint profileId, uint recursiveInlineDepth)
  1533. {
  1534. Assert(callInstr);
  1535. Assert(funcInfo);
  1536. Assert(inlinerData);
  1537. Assert(inlineCallOpCode != 0);
  1538. // We may still decide not to inline.
  1539. *pIsInlined = false;
  1540. // Inlining is profile-based, so get the built-in function from profile rather than from the callInstr's opnd.
  1541. Js::BuiltinFunction builtInId = Js::JavascriptLibrary::GetBuiltInForFuncInfo(funcInfo, callInstr->m_func->GetScriptContext());
  1542. #if defined(DBG_DUMP) || defined(ENABLE_DEBUG_CONFIG_OPTIONS)
  1543. wchar_t debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  1544. #endif
  1545. if(inlineCallOpCode == Js::OpCode::InlineMathFloor || inlineCallOpCode == Js::OpCode::InlineMathCeil || inlineCallOpCode == Js::OpCode::InlineMathRound)
  1546. {
  1547. #if defined(_M_IX86) || defined(_M_X64)
  1548. if (!AutoSystemInfo::Data.SSE4_1Available())
  1549. {
  1550. INLINE_TESTTRACE(L"INLINING: Skip Inline: SSE4.1 not available\tInlinee: %s (#%d)\tCaller: %s\n", Js::JavascriptLibrary::GetNameForBuiltIn(builtInId), (int)builtInId, inlinerData->GetFunctionBody()->GetDisplayName());
  1551. return callInstr->m_next;
  1552. }
  1553. #endif
  1554. if(callInstr->m_func->GetTopFunc()->GetProfileInfo()->IsFloorInliningDisabled())
  1555. {
  1556. INLINE_TESTTRACE(L"INLINING: Skip Inline: Floor Inlining Disabled\tInlinee: %s (#%d)\tCaller: %s\n", Js::JavascriptLibrary::GetNameForBuiltIn(builtInId), (int)builtInId, inlinerData->GetFunctionBody()->GetDisplayName());
  1557. return callInstr->m_next;
  1558. }
  1559. }
  1560. if (callInstr->GetSrc2() &&
  1561. callInstr->GetSrc2()->IsSymOpnd() &&
  1562. callInstr->GetSrc2()->AsSymOpnd()->m_sym->AsStackSym()->GetArgSlotNum() > Js::InlineeCallInfo::MaxInlineeArgoutCount)
  1563. {
  1564. // This is a hard limit as we only use 4 bits to encode the actual count in the InlineeCallInfo. Although
  1565. // InliningDecider already checks for this, the check is against profile data that may not be accurate since profile
  1566. // data matching does not take into account some types of changes to source code. Need to check this again with current
  1567. // information.
  1568. INLINE_TESTTRACE(L"INLINING: Skip Inline: ArgSlot > MaxInlineeArgoutCount\tInlinee: %s (#%d)\tArgSlotNum: %d\tMaxInlineeArgoutCount: %d\tCaller: %s (#%d)\n",
  1569. Js::JavascriptLibrary::GetNameForBuiltIn(builtInId), (int)builtInId, callInstr->GetSrc2()->AsSymOpnd()->m_sym->AsStackSym()->GetArgSlotNum(),
  1570. Js::InlineeCallInfo::MaxInlineeArgoutCount, inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer));
  1571. return callInstr->m_next;
  1572. }
  1573. Js::BuiltInFlags builtInFlags = Js::JavascriptLibrary::GetFlagsForBuiltIn(builtInId);
  1574. bool isAnyArgFloat = (builtInFlags & Js::BuiltInFlags::BIF_TypeSpecAllToFloat) != 0;
  1575. if (isAnyArgFloat && !GlobOpt::DoFloatTypeSpec(this->topFunc))
  1576. {
  1577. INLINE_TESTTRACE(L"INLINING: Skip Inline: float type spec is off\tInlinee: %s (#%d)\tCaller: %s (%s)\n",
  1578. Js::JavascriptLibrary::GetNameForBuiltIn(builtInId), (int)builtInId,
  1579. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer));
  1580. return callInstr->m_next;
  1581. }
  1582. bool canDstBeFloat = (builtInFlags & Js::BuiltInFlags::BIF_TypeSpecDstToFloat) != 0;
  1583. if (canDstBeFloat && !Js::JavascriptLibrary::CanFloatPreferenceFunc(builtInId) && inlineCallOpCode != Js::OpCode::InlineArrayPop)
  1584. {
  1585. // Note that for Math.abs that means that even though it can potentially be type-spec'd to int, we won't inline it.
  1586. // Some built-in functions, such as atan2, are disabled for float-pref.
  1587. INLINE_TESTTRACE(L"INLINING: Skip Inline: Cannot float-type-spec the inlinee\tInlinee: %s (#%d)\tCaller: %s (%s)\n",
  1588. Js::JavascriptLibrary::GetNameForBuiltIn(builtInId), (int)builtInId, // Get the _value (cause operator _E) to avoid using struct directly.
  1589. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer));
  1590. return callInstr->m_next;
  1591. }
  1592. bool isAnyArgInt = (builtInFlags & (Js::BuiltInFlags::BIF_TypeSpecDstToInt | Js::BuiltInFlags::BIF_TypeSpecSrc1ToInt | Js::BuiltInFlags::BIF_TypeSpecSrc2ToInt)) != 0;
  1593. if (isAnyArgInt && !GlobOpt::DoAggressiveIntTypeSpec(this->topFunc))
  1594. {
  1595. // Note that for Math.abs that means that even though it can potentially be type-spec'd to float, we won't inline it.
  1596. INLINE_TESTTRACE(L"INLINING: Skip Inline: int type spec is off\tInlinee: %s (#%d)\tCaller: %s (%s)\n",
  1597. Js::JavascriptLibrary::GetNameForBuiltIn(builtInId), (int)builtInId,
  1598. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer));
  1599. return callInstr->m_next;
  1600. }
  1601. if(inlineCallOpCode == Js::OpCode::InlineMathImul && !GlobOpt::DoLossyIntTypeSpec(topFunc))
  1602. {
  1603. INLINE_TESTTRACE(L"INLINING: Skip Inline: lossy int type spec is off, it's required for Math.imul to do | 0 on src opnds\tInlinee: %s (#%d)\tCaller: %s (%s)\n",
  1604. Js::JavascriptLibrary::GetNameForBuiltIn(builtInId), (int)builtInId,
  1605. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer));
  1606. return callInstr->m_next;
  1607. }
  1608. if(inlineCallOpCode == Js::OpCode::InlineMathClz32 && !GlobOpt::DoLossyIntTypeSpec(topFunc))
  1609. {
  1610. INLINE_TESTTRACE(L"INLINING: Skip Inline: lossy int type spec is off, it's required for Math.clz32 to do | 0 on src opnds\tInlinee: %s (#%d)\tCaller: %s (%s)\n",
  1611. Js::JavascriptLibrary::GetNameForBuiltIn(builtInId), (int)builtInId,
  1612. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer));
  1613. return callInstr->m_next;
  1614. }
  1615. if (inlineCallOpCode == Js::OpCode::InlineFunctionApply && (!callInstr->m_func->GetHasStackArgs() || this->topFunc->GetJnFunction()->IsInlineApplyDisabled()))
  1616. {
  1617. INLINE_TESTTRACE(L"INLINING: Skip Inline: stack args of inlining is off\tInlinee: %s (#%d)\tCaller: %s (%s)\n",
  1618. Js::JavascriptLibrary::GetNameForBuiltIn(builtInId), (int)builtInId,
  1619. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer));
  1620. return callInstr->m_next;
  1621. }
  1622. // TODO: when adding support for other type spec args (array, string) do appropriate check as well.
  1623. Assert(callInstr->GetSrc1());
  1624. Assert(callInstr->GetSrc1()->IsRegOpnd());
  1625. Assert(callInstr->GetSrc1()->AsRegOpnd()->m_sym);
  1626. if (!(builtInFlags & Js::BuiltInFlags::BIF_IgnoreDst) && callInstr->GetDst() == nullptr && inlineCallOpCode != Js::OpCode::InlineArrayPop)
  1627. {
  1628. // Is seems that it's not worth optimizing odd cases where the result is unused.
  1629. INLINE_TESTTRACE(L"INLINING: Skip Inline: inlinee's return value is not assigned to anything\tInlinee: %s (#%d)\tCaller: %s (%s)\n",
  1630. Js::JavascriptLibrary::GetNameForBuiltIn(builtInId), (int)builtInId,
  1631. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer));
  1632. return callInstr->m_next;
  1633. }
  1634. // Number of arguments, not including "this".
  1635. IntConstType requiredInlineCallArgCount = (IntConstType)Js::JavascriptLibrary::GetArgCForBuiltIn(builtInId);
  1636. IR::Opnd* linkOpnd = callInstr->GetSrc2();
  1637. Js::ArgSlot actualCount = linkOpnd->AsSymOpnd()->m_sym->AsStackSym()->GetArgSlotNum();
  1638. // Check for missing actuals:
  1639. // if number of passed params to built-in function is not what it needs, don't inline.
  1640. int inlineCallArgCount = (int)((builtInFlags & Js::BuiltInFlags::BIF_UseSrc0) != 0 ? actualCount : actualCount - 1);
  1641. Assert(inlineCallArgCount >= 0);
  1642. if (linkOpnd->IsSymOpnd())
  1643. {
  1644. #if ENABLE_DEBUG_CONFIG_OPTIONS
  1645. wchar_t debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  1646. #endif
  1647. if((builtInFlags & Js::BuiltInFlags::BIF_VariableArgsNumber) != 0)
  1648. {
  1649. if(inlineCallArgCount > requiredInlineCallArgCount)
  1650. {
  1651. INLINE_TESTTRACE(L"INLINING: Skip Inline: parameter count exceeds the maximum number of parameters allowed\tInlinee: %s (#%d)\tCaller: %s (%s)\n",
  1652. Js::JavascriptLibrary::GetNameForBuiltIn(builtInId), (int)builtInId,
  1653. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer));
  1654. return callInstr->m_next;
  1655. }
  1656. }
  1657. else if(inlineCallArgCount != requiredInlineCallArgCount)
  1658. {
  1659. INLINE_TESTTRACE(L"INLINING: Skip Inline: parameter count doesn't match dynamic profile\tInlinee: %s (#%d)\tCaller: %s (%s)\n",
  1660. Js::JavascriptLibrary::GetNameForBuiltIn(builtInId), (int)builtInId,
  1661. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer));
  1662. return callInstr->m_next;
  1663. }
  1664. }
  1665. IR::Instr *inlineBuiltInEndInstr = nullptr;
  1666. if (inlineCallOpCode == Js::OpCode::InlineFunctionApply)
  1667. {
  1668. inlineBuiltInEndInstr = InlineApply(callInstr, funcInfo, inlinerData, symCallerThis, pIsInlined, profileId, recursiveInlineDepth);
  1669. return inlineBuiltInEndInstr->m_next;
  1670. }
  1671. if (inlineCallOpCode == Js::OpCode::InlineFunctionCall)
  1672. {
  1673. inlineBuiltInEndInstr = InlineCall(callInstr, funcInfo, inlinerData, symCallerThis, pIsInlined, profileId, recursiveInlineDepth);
  1674. return inlineBuiltInEndInstr->m_next;
  1675. }
  1676. #if defined(ENABLE_DEBUG_CONFIG_OPTIONS)
  1677. InliningDecider::TraceInlining(inlinerData->GetFunctionBody(), Js::JavascriptLibrary::GetNameForBuiltIn(builtInId),
  1678. nullptr, 0, this->topFunc->m_workItem->GetFunctionBody(), 0, nullptr, profileId, builtInId);
  1679. #endif
  1680. // From now on we are committed to inlining.
  1681. *pIsInlined = true;
  1682. // Save off the call target operand (function object) so we can extend its lifetime as needed, even if
  1683. // the call instruction gets transformed to CallIFixed.
  1684. StackSym* originalCallTargetStackSym = callInstr->GetSrc1()->GetStackSym();
  1685. // We are committed to inlining, optimize the call instruction for fixed fields now and don't attempt it later.
  1686. bool safeThis = false;
  1687. if (TryOptimizeCallInstrWithFixedMethod(callInstr, funcInfo, false /*isPolymorphic*/, true /*isBuiltIn*/, false /*isCtor*/, true /*isInlined*/, safeThis /*unused here*/))
  1688. {
  1689. Assert(callInstr->m_opcode == Js::OpCode::CallIFixed);
  1690. Assert(callInstr->GetFixedFunction()->GetFunctionInfo() == funcInfo);
  1691. }
  1692. else
  1693. {
  1694. // FunctionObject check for built-ins
  1695. IR::BailOutInstr * bailOutInstr = IR::BailOutInstr::New(Js::OpCode::BailOnNotBuiltIn, IR::BailOutOnInlineFunction, callInstr, callInstr->m_func);
  1696. InsertFunctionObjectCheck(callInstr, callInstr, bailOutInstr, funcInfo);
  1697. }
  1698. // To push function object for cases when we have to make calls to helper method to assist in inlining
  1699. if(inlineCallOpCode == Js::OpCode::CallDirect)
  1700. {
  1701. IR::Instr* argoutInstr;
  1702. StackSym *dstSym = callInstr->m_func->m_symTable->GetArgSlotSym((uint16)(1));
  1703. argoutInstr = IR::Instr::New(Js::OpCode::ArgOut_A_InlineSpecialized, IR::SymOpnd::New(dstSym, 0, TyMachPtr, callInstr->m_func), callInstr->UnlinkSrc1(), callInstr->UnlinkSrc2(), callInstr->m_func);
  1704. argoutInstr->SetByteCodeOffset(callInstr);
  1705. callInstr->GetInsertBeforeByteCodeUsesInstr()->InsertBefore(argoutInstr);
  1706. Js::BuiltinFunction builtInId = Js::JavascriptLibrary::GetBuiltInForFuncInfo(funcInfo, callInstr->m_func->GetScriptContext());
  1707. callInstr->m_opcode = inlineCallOpCode;
  1708. SetupInlineInstrForCallDirect(builtInId, callInstr, argoutInstr);
  1709. // Generate ByteCodeArgOutCaptures and move the ArgOut_A/ArgOut_A_Inline close to the call instruction
  1710. callInstr->MoveArgs(/*generateByteCodeCapture*/ true);
  1711. WrapArgsOutWithCoerse(builtInId, callInstr);
  1712. inlineBuiltInEndInstr = callInstr;
  1713. }
  1714. else
  1715. {
  1716. inlineBuiltInEndInstr = InsertInlineeBuiltInStartEndTags(callInstr, actualCount);
  1717. // InlineArrayPop - TrackCalls Need to be done at InlineArrayPop and not at the InlineBuiltInEnd
  1718. // Hence we use a new opcode, to detect that it is an InlineArrayPop and we don't track the call during End of inlineBuiltInCall sequence
  1719. if(inlineCallOpCode == Js::OpCode::InlineArrayPop)
  1720. {
  1721. inlineBuiltInEndInstr->m_opcode = Js::OpCode::InlineNonTrackingBuiltInEnd;
  1722. }
  1723. }
  1724. // Insert a byteCodeUsesInstr to make sure the function object's lifetime is extended beyond the last bailout point
  1725. // at which we may need to call the inlinee again in the interpreter.
  1726. IR::ByteCodeUsesInstr * useCallTargetInstr = IR::ByteCodeUsesInstr::New(callInstr, originalCallTargetStackSym->m_id);
  1727. callInstr->InsertBefore(useCallTargetInstr);
  1728. if(Js::JavascriptLibrary::IsTypeSpecRequired(builtInFlags)
  1729. // SIMD_JS
  1730. || IsSimd128Opcode(inlineCallOpCode)
  1731. //
  1732. )
  1733. {
  1734. // Emit byteCodeUses for function object
  1735. IR::Instr * inlineBuiltInStartInstr = inlineBuiltInEndInstr;
  1736. while(inlineBuiltInStartInstr->m_opcode != Js::OpCode::InlineBuiltInStart)
  1737. {
  1738. inlineBuiltInStartInstr = inlineBuiltInStartInstr->m_prev;
  1739. }
  1740. IR::Opnd * tmpDst = nullptr;
  1741. IR::Opnd * callInstrDst = callInstr->GetDst();
  1742. if(callInstrDst && inlineCallOpCode != Js::OpCode::InlineArrayPop)
  1743. {
  1744. StackSym * tmpSym = StackSym::New(callInstr->GetDst()->GetType(), callInstr->m_func);
  1745. tmpDst = IR::RegOpnd::New(tmpSym, tmpSym->GetType(), callInstr->m_func);
  1746. callInstrDst = callInstr->UnlinkDst();
  1747. callInstr->SetDst(tmpDst);
  1748. }
  1749. else
  1750. {
  1751. AssertMsg(inlineCallOpCode == Js::OpCode::InlineArrayPush || inlineCallOpCode == Js::OpCode::InlineArrayPop || Js::IsSimd128Opcode(inlineCallOpCode),
  1752. "Currently Dst can be null only for InlineArrayPush/InlineArrayPop");
  1753. }
  1754. // Insert a byteCodeUsesInstr to make sure the function object's lifetime is extended beyond the last bailout point
  1755. // at which we may need to call the inlinee again in the interpreter.
  1756. IR::ByteCodeUsesInstr * useCallTargetInstr = IR::ByteCodeUsesInstr::New(callInstr->GetPrevRealInstrOrLabel(), originalCallTargetStackSym->m_id);
  1757. if(inlineCallOpCode == Js::OpCode::InlineArrayPop)
  1758. {
  1759. callInstr->InsertBefore(useCallTargetInstr);
  1760. }
  1761. else
  1762. {
  1763. inlineBuiltInEndInstr->InsertBefore(useCallTargetInstr);
  1764. }
  1765. if(tmpDst)
  1766. {
  1767. IR::Instr * ldInstr = IR::Instr::New(Js::OpCode::Ld_A, callInstrDst, tmpDst, callInstr->m_func);
  1768. inlineBuiltInEndInstr->InsertBefore(ldInstr);
  1769. }
  1770. // Set srcs of the callInstr, and process ArgOuts.
  1771. callInstr->UnlinkSrc1();
  1772. callInstr->UnlinkSrc2();
  1773. callInstr->m_opcode = inlineCallOpCode;
  1774. int argIndex = inlineCallArgCount; // We'll use it to fill call instr srcs from upper to lower.
  1775. IR::ByteCodeUsesInstr * byteCodeUsesInstr = IR::ByteCodeUsesInstr::New(callInstr->m_func);
  1776. byteCodeUsesInstr->SetByteCodeOffset(callInstr);
  1777. byteCodeUsesInstr->byteCodeUpwardExposedUsed = JitAnew(callInstr->m_func->m_alloc, BVSparse<JitArenaAllocator>, callInstr->m_func->m_alloc);
  1778. IR::Instr *argInsertInstr = inlineBuiltInStartInstr;
  1779. // SIMD_JS
  1780. IR::Instr *eaInsertInstr = callInstr;
  1781. IR::Opnd *eaLinkOpnd = nullptr;
  1782. ThreadContext::SimdFuncSignature simdFuncSignature;
  1783. if (IsSimd128Opcode(callInstr->m_opcode))
  1784. {
  1785. callInstr->m_func->GetScriptContext()->GetThreadContext()->GetSimdFuncSignatureFromOpcode(callInstr->m_opcode, simdFuncSignature);
  1786. Assert(simdFuncSignature.valid);
  1787. // if we have decided to inline, then actual arg count == signature arg count == required arg count from inlinee list (LibraryFunction.h)
  1788. Assert(simdFuncSignature.argCount == (uint)inlineCallArgCount);
  1789. Assert(simdFuncSignature.argCount == (uint)requiredInlineCallArgCount);
  1790. }
  1791. //
  1792. inlineBuiltInEndInstr->IterateArgInstrs([&](IR::Instr* argInstr) {
  1793. StackSym *linkSym = linkOpnd->GetStackSym();
  1794. linkSym->m_isInlinedArgSlot = true;
  1795. linkSym->m_allocated = true;
  1796. // We are going to replace the use on the call (below), insert byte code use if necessary
  1797. if (OpCodeAttr::BailOutRec(inlineCallOpCode) || Js::IsSimd128Opcode(inlineCallOpCode))
  1798. {
  1799. StackSym * sym = argInstr->GetSrc1()->GetStackSym();
  1800. if (!sym->m_isSingleDef || !sym->m_instrDef->GetSrc1() || !sym->m_instrDef->GetSrc1()->IsConstOpnd())
  1801. {
  1802. if (!sym->IsFromByteCodeConstantTable())
  1803. {
  1804. byteCodeUsesInstr->byteCodeUpwardExposedUsed->Set(sym->m_id);
  1805. }
  1806. }
  1807. }
  1808. // Convert the arg out to built in arg out, and get the src of the arg out
  1809. IR::Opnd * argOpnd = ConvertToInlineBuiltInArgOut(argInstr);
  1810. // SIMD_JS
  1811. if (inlineCallArgCount > 2 && argIndex != 0 /* don't include 'this' */)
  1812. {
  1813. Assert(IsSimd128Opcode(callInstr->m_opcode));
  1814. // Insert ExtendedArgs
  1815. IR::Instr *eaInstr;
  1816. // inliner sets the dst type of the ExtendedArg to the expected arg type for the operation. The globOpt uses this info to know the type-spec target for each ExtendedArg.
  1817. eaInstr = IR::Instr::New(Js::OpCode::ExtendArg_A, callInstr->m_func);
  1818. eaInstr->SetByteCodeOffset(callInstr);
  1819. if (argIndex == inlineCallArgCount)
  1820. {
  1821. // fix callInstr
  1822. eaLinkOpnd = IR::RegOpnd::New(TyVar, callInstr->m_func);
  1823. eaLinkOpnd->GetStackSym()->m_isInlinedArgSlot = true;
  1824. eaLinkOpnd->GetStackSym()->m_allocated = true;
  1825. Assert(callInstr->GetSrc1() == nullptr && callInstr->GetSrc2() == nullptr);
  1826. callInstr->SetSrc1(eaLinkOpnd);
  1827. }
  1828. Assert(eaLinkOpnd);
  1829. eaInstr->SetDst(eaLinkOpnd);
  1830. eaInstr->SetSrc1(argInstr->GetSrc1());
  1831. // insert link opnd, except for first ExtendedArg
  1832. if (argIndex > 1)
  1833. {
  1834. eaInstr->SetSrc2(IR::RegOpnd::New(TyVar, callInstr->m_func));
  1835. eaLinkOpnd = eaInstr->GetSrc2();
  1836. eaLinkOpnd->GetStackSym()->m_isInlinedArgSlot = true;
  1837. eaLinkOpnd->GetStackSym()->m_allocated = true;
  1838. }
  1839. eaInstr->GetDst()->SetValueType(simdFuncSignature.args[argIndex - 1]);
  1840. eaInsertInstr->InsertBefore(eaInstr);
  1841. eaInsertInstr = eaInstr;
  1842. }
  1843. else
  1844. {
  1845. // Use parameter to the inline call to tempDst.
  1846. if (argIndex == 2)
  1847. {
  1848. callInstr->SetSrc2(argOpnd);
  1849. // Prevent inserting ByteCodeUses instr during globopt, as we already track the src in ArgOut.
  1850. callInstr->GetSrc2()->SetIsJITOptimizedReg(true);
  1851. }
  1852. else if (argIndex == 1)
  1853. {
  1854. callInstr->SetSrc1(argOpnd);
  1855. // Prevent inserting ByteCodeUses instr during globopt, as we already track the src in ArgOut.
  1856. callInstr->GetSrc1()->SetIsJITOptimizedReg(true);
  1857. }
  1858. }
  1859. argIndex--;
  1860. linkOpnd = argInstr->GetSrc2();
  1861. // Move the arguments next to the call.
  1862. argInstr->Move(argInsertInstr);
  1863. argInsertInstr = argInstr;
  1864. return false;
  1865. });
  1866. //SIMD_JS
  1867. Simd128FixLoadStoreInstr(builtInId, callInstr);
  1868. if(inlineCallOpCode == Js::OpCode::InlineMathImul || inlineCallOpCode == Js::OpCode::InlineMathClz32)
  1869. {
  1870. // Convert:
  1871. // s1 = InlineMathImul s2, s3
  1872. // Into:
  1873. // s4 = Or_A s2, 0
  1874. // s5 = Or_A s3, 0
  1875. // s1 = InlineMathImul s4, s5
  1876. Func *const func = callInstr->m_func;
  1877. IR::AddrOpnd *const zeroOpnd = IR::AddrOpnd::NewFromNumber(0, func, true);
  1878. IR::RegOpnd *const s4 = IR::RegOpnd::New(TyVar, func);
  1879. s4->SetIsJITOptimizedReg(true);
  1880. IR::Instr *orInstr = IR::Instr::New(Js::OpCode::Or_A, s4, callInstr->UnlinkSrc1(), zeroOpnd, func);
  1881. orInstr->SetByteCodeOffset(callInstr);
  1882. callInstr->InsertBefore(orInstr);
  1883. callInstr->SetSrc1(s4);
  1884. if (inlineCallOpCode == Js::OpCode::InlineMathImul)
  1885. {
  1886. if (callInstr->GetSrc2()->IsEqual(callInstr->GetSrc1()))
  1887. {
  1888. callInstr->ReplaceSrc2(s4);
  1889. }
  1890. else
  1891. {
  1892. IR::RegOpnd *const s5 = IR::RegOpnd::New(TyVar, func);
  1893. s5->SetIsJITOptimizedReg(true);
  1894. orInstr = IR::Instr::New(Js::OpCode::Or_A, s5, callInstr->UnlinkSrc2(), zeroOpnd, func);
  1895. orInstr->SetByteCodeOffset(callInstr);
  1896. callInstr->InsertBefore(orInstr);
  1897. callInstr->SetSrc2(s5);
  1898. }
  1899. }
  1900. }
  1901. if(OpCodeAttr::BailOutRec(inlineCallOpCode))
  1902. {
  1903. inlineBuiltInEndInstr->InsertBefore(byteCodeUsesInstr);
  1904. }
  1905. Assert(linkOpnd->AsRegOpnd()->m_sym->GetInstrDef()->m_opcode == Js::OpCode::StartCall);
  1906. Assert(linkOpnd->AsRegOpnd()->m_sym->GetInstrDef()->GetArgOutCount(/*getInterpreterArgOutCount*/ false) == actualCount);
  1907. // Mark the StartCall's dst as an inlined arg slot as well so we know this is an inlined start call
  1908. // and not adjust the stack height on x86
  1909. linkOpnd->AsRegOpnd()->m_sym->m_isInlinedArgSlot = true;
  1910. if(OpCodeAttr::BailOutRec(inlineCallOpCode))
  1911. {
  1912. callInstr = callInstr->ConvertToBailOutInstr(callInstr, IR::BailOutOnFloor);
  1913. }
  1914. }
  1915. return inlineBuiltInEndInstr->m_next;
  1916. }
  1917. IR::Instr* Inline::InsertInlineeBuiltInStartEndTags(IR::Instr* callInstr, uint actualCount, IR::Instr** builtinStartInstr)
  1918. {
  1919. IR::Instr* inlineBuiltInStartInstr = IR::Instr::New(Js::OpCode::InlineBuiltInStart, callInstr->m_func);
  1920. inlineBuiltInStartInstr->SetSrc1(callInstr->GetSrc1());
  1921. inlineBuiltInStartInstr->SetSrc2(callInstr->GetSrc2());
  1922. inlineBuiltInStartInstr->SetByteCodeOffset(callInstr);
  1923. callInstr->InsertBefore(inlineBuiltInStartInstr);
  1924. if (builtinStartInstr)
  1925. {
  1926. *builtinStartInstr = inlineBuiltInStartInstr;
  1927. }
  1928. IR::Instr* inlineBuiltInEndInstr = IR::Instr::New(Js::OpCode::InlineBuiltInEnd, callInstr->m_func);
  1929. inlineBuiltInEndInstr->SetSrc1(IR::IntConstOpnd::New(actualCount, TyInt32, callInstr->m_func));
  1930. inlineBuiltInEndInstr->SetSrc2(callInstr->GetSrc2());
  1931. inlineBuiltInEndInstr->SetByteCodeOffset(callInstr->GetNextRealInstrOrLabel());
  1932. callInstr->InsertAfter(inlineBuiltInEndInstr);
  1933. return inlineBuiltInEndInstr;
  1934. }
  1935. IR::Instr* Inline::GetDefInstr(IR::Opnd* linkOpnd)
  1936. {
  1937. StackSym *linkSym = linkOpnd->AsSymOpnd()->m_sym->AsStackSym();
  1938. Assert(linkSym->m_isSingleDef);
  1939. Assert(linkSym->IsArgSlotSym());
  1940. return linkSym->m_instrDef;
  1941. }
  1942. IR::Instr* Inline::InlineApply(IR::Instr *callInstr, Js::FunctionInfo *funcInfo, const Js::FunctionCodeGenJitTimeData* inlinerData, const StackSym *symCallerThis, bool* pIsInlined, uint callSiteId, uint recursiveInlineDepth)
  1943. {
  1944. // We may still decide not to inline.
  1945. *pIsInlined = false;
  1946. Js::BuiltinFunction builtInId = Js::JavascriptLibrary::GetBuiltInForFuncInfo(funcInfo, callInstr->m_func->GetScriptContext());
  1947. const Js::FunctionCodeGenJitTimeData * inlineeData = nullptr;
  1948. IR::SymOpnd* linkOpnd = callInstr->GetSrc2()->AsSymOpnd();
  1949. StackSym *arrayArgsym = linkOpnd->AsSymOpnd()->m_sym->AsStackSym();
  1950. Assert(arrayArgsym->m_isSingleDef);
  1951. Assert(arrayArgsym->IsArgSlotSym());
  1952. IR::Instr* arrayArgInstr = arrayArgsym->m_instrDef;
  1953. IR::Opnd *arrayArgOpnd = arrayArgInstr->GetSrc1();
  1954. // if isArrayOpndArgumentsObject == false, the array opnd can still be the arguments object; we just can't say that for sure
  1955. bool isArrayOpndArgumentsObject = arrayArgOpnd->IsArgumentsObject();
  1956. IR::Instr * returnInstr = nullptr;
  1957. if (!PHASE_OFF(Js::InlineApplyTargetPhase, this->topFunc))
  1958. {
  1959. if (isArrayOpndArgumentsObject && InlineApplyTarget(callInstr, inlinerData, &inlineeData, funcInfo, symCallerThis, &returnInstr, recursiveInlineDepth))
  1960. {
  1961. *pIsInlined = true;
  1962. Assert(returnInstr);
  1963. return returnInstr;
  1964. }
  1965. }
  1966. #if defined(ENABLE_DEBUG_CONFIG_OPTIONS)
  1967. InliningDecider::TraceInlining(inlinerData->GetFunctionBody(), Js::JavascriptLibrary::GetNameForBuiltIn(builtInId),
  1968. nullptr, 0, this->topFunc->m_workItem->GetFunctionBody(), 0, nullptr, callSiteId, builtInId);
  1969. wchar_t debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  1970. #endif
  1971. if (!isArrayOpndArgumentsObject)
  1972. {
  1973. if (inlineeData && inlineeData->GetFunctionBody() == nullptr)
  1974. {
  1975. *pIsInlined = true;
  1976. Assert((inlineeData->GetFunctionInfo()->GetAttributes() & Js::FunctionInfo::Attributes::BuiltInInlinableAsLdFldInlinee) != 0);
  1977. return InlineApplyWithArray(callInstr, funcInfo, Js::JavascriptLibrary::GetBuiltInForFuncInfo(inlineeData->GetFunctionInfo(), callInstr->m_func->GetScriptContext()));
  1978. }
  1979. else
  1980. {
  1981. INLINE_TESTTRACE(L"INLINING: Skip Inline: Supporting inlining func.apply(this, array) or func.apply(this, arguments) with formals in the parent function only when func is a built-in inlinable as apply target \tCaller: %s (%s)\n",
  1982. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer));
  1983. return callInstr;
  1984. }
  1985. }
  1986. *pIsInlined = true;
  1987. return InlineApplyWithArgumentsObject(callInstr, arrayArgInstr, funcInfo);
  1988. }
  1989. IR::Instr * Inline::InlineApplyWithArgumentsObject(IR::Instr * callInstr, IR::Instr * argsObjectArgInstr, Js::FunctionInfo * funcInfo)
  1990. {
  1991. IR::Instr* ldHeapArguments = argsObjectArgInstr->GetSrc1()->GetStackSym()->GetInstrDef();
  1992. IR::RegOpnd* argumentsObj = IR::RegOpnd::New(TyVar, callInstr->m_func);
  1993. IR::Instr *assignInstr = IR::Instr::New(Js::OpCode::LdArgumentsFromStack, argumentsObj, ldHeapArguments->GetDst(), callInstr->m_func);
  1994. assignInstr->SetByteCodeOffset(argsObjectArgInstr);
  1995. argsObjectArgInstr->InsertBefore(assignInstr);
  1996. argsObjectArgInstr->ReplaceSrc1(ldHeapArguments->GetDst());
  1997. IR::Opnd * linkOpnd = callInstr->GetSrc2()->AsSymOpnd();
  1998. IR::Instr * explicitThisArgOut = nullptr;
  1999. IR::Instr * implicitThisArgOut = nullptr;
  2000. callInstr->IterateArgInstrs([&](IR::Instr* argInstr) {
  2001. explicitThisArgOut = implicitThisArgOut;
  2002. implicitThisArgOut = argInstr;
  2003. linkOpnd->AsSymOpnd()->m_sym->AsStackSym()->m_isInlinedArgSlot = true;
  2004. linkOpnd->AsSymOpnd()->m_sym->AsStackSym()->m_allocated = true;
  2005. ConvertToInlineBuiltInArgOut(argInstr);
  2006. linkOpnd = argInstr->GetSrc2();
  2007. return false;
  2008. });
  2009. // BailOnNotEqual s4.var ---------------New additional BAILOUT if not stack args or actuals exceed 16 at runtime.
  2010. // Bailout: #004e (BailOutOnInlineFunction)
  2011. // linkOpnd Argout_FromStackArgs s4.var
  2012. // linkOpnd1 ArgOut_A_Dynamic s3.var, linkOpnd
  2013. // CallI_Dynamic s6.var, linkOpnd1
  2014. IR::Instr* bailOutOnNotStackArgs;
  2015. IR::Instr* bailOutOnNotStackArgsInsertionPoint = callInstr;
  2016. // Save off the call target operand (function object) so we can extend its lifetime as needed, even if
  2017. // the call instruction gets transformed to CallIFixed.
  2018. StackSym* originalCallTargetStackSym = callInstr->GetSrc1()->GetStackSym();
  2019. // If we optimized the call instruction for a fixed function we will have bailed out earlier if the function
  2020. // wasn't what we expected or was not a function at all. However, we must still check and bail out on heap arguments.
  2021. bool safeThis = false;
  2022. if (TryOptimizeCallInstrWithFixedMethod(callInstr, funcInfo, false /*isPolymorphic*/, true /*isBuiltIn*/, false /*isCtor*/, true /*isInlined*/, safeThis /*unused here*/))
  2023. {
  2024. Assert(callInstr->m_opcode == Js::OpCode::CallIFixed);
  2025. bailOutOnNotStackArgs = IR::BailOutInstr::New(Js::OpCode::BailOnNotStackArgs, IR::BailOutOnInlineFunction, callInstr, callInstr->m_func);
  2026. }
  2027. else
  2028. {
  2029. IR::Instr *primaryBailoutInstr = PrepareInsertionPoint(callInstr, funcInfo, callInstr);
  2030. bailOutOnNotStackArgs = IR::BailOutInstr::New(Js::OpCode::BailOnNotStackArgs, IR::BailOutOnInlineFunction, primaryBailoutInstr->GetBailOutInfo(), callInstr->m_func);
  2031. bailOutOnNotStackArgsInsertionPoint = primaryBailoutInstr;
  2032. }
  2033. bailOutOnNotStackArgs->SetSrc1(argumentsObj);
  2034. bailOutOnNotStackArgs->SetSrc2(IR::AddrOpnd::NewNull(callInstr->m_func));
  2035. bailOutOnNotStackArgsInsertionPoint->InsertBefore(bailOutOnNotStackArgs);
  2036. // If we optimized the call instruction for a fixed function, we must extend the function object's lifetime until after
  2037. // the bailout on non-stack arguments.
  2038. if (callInstr->m_opcode == Js::OpCode::CallIFixed)
  2039. {
  2040. IR::ByteCodeUsesInstr * useCallTargetInstr = IR::ByteCodeUsesInstr::New(callInstr, originalCallTargetStackSym->m_id);
  2041. callInstr->InsertBefore(useCallTargetInstr);
  2042. }
  2043. // Optimize .init.apply(this, arguments);
  2044. IR::Instr* builtInStartInstr;
  2045. InsertInlineeBuiltInStartEndTags(callInstr, 3, &builtInStartInstr); //3 args (implicit this + explicit this + arguments = 3)
  2046. // Move argouts close to call
  2047. IR::Instr* argInsertInstr = builtInStartInstr;
  2048. builtInStartInstr->IterateArgInstrs([&](IR::Instr* argInstr) {
  2049. argInstr->Move(argInsertInstr);
  2050. argInsertInstr = argInstr;
  2051. return false;
  2052. });
  2053. IR::Instr *startCall = IR::Instr::New(Js::OpCode::StartCall, callInstr->m_func);
  2054. startCall->SetDst(IR::RegOpnd::New(TyVar, callInstr->m_func));
  2055. startCall->SetSrc1(IR::IntConstOpnd::New(2, TyInt32, callInstr->m_func)); //2 args (this pointer & ArgOut_A_From_StackArgs for this direct call to init
  2056. callInstr->InsertBefore(startCall);
  2057. StackSym *symDst = callInstr->m_func->m_symTable->GetArgSlotSym((uint16)(2));
  2058. IR::SymOpnd* linkOpnd1 = IR::SymOpnd::New(symDst, 0, TyMachPtr, callInstr->m_func);
  2059. symDst = callInstr->m_func->m_symTable->GetArgSlotSym((uint16)(1));
  2060. IR::Opnd *linkOpnd2 = IR::SymOpnd::New(symDst, 0, TyMachPtr, callInstr->m_func);
  2061. // This keeps the stack args alive for bailout to recover
  2062. IR::Instr* argout = IR::Instr::New(Js::OpCode::ArgOut_A_FromStackArgs, linkOpnd1, ldHeapArguments->GetDst(), startCall->GetDst(), callInstr->m_func);
  2063. callInstr->InsertBefore(argout);
  2064. callInstr->ReplaceSrc1(implicitThisArgOut->GetSrc1());
  2065. callInstr->ReplaceSrc2(linkOpnd2);
  2066. callInstr->m_opcode = Js::OpCode::CallIDynamic;
  2067. argout = IR::Instr::New(Js::OpCode::ArgOut_A_Dynamic, linkOpnd2, explicitThisArgOut->GetSrc1(), linkOpnd1, callInstr->m_func); // push explicit this as this pointer
  2068. callInstr->InsertBefore(argout);
  2069. return callInstr;
  2070. }
  2071. IR::Instr * Inline::InlineApplyWithArray(IR::Instr * callInstr, Js::FunctionInfo * funcInfo, Js::BuiltinFunction builtInId)
  2072. {
  2073. IR::Opnd * linkOpnd = callInstr->GetSrc2()->AsSymOpnd();
  2074. IR::Instr * argInsertInstr = callInstr;
  2075. IR::Instr * arrayArgOut = nullptr;
  2076. IR::Instr * explicitThisArgOut = nullptr;
  2077. IR::Instr * implicitThisArgOut = nullptr;
  2078. callInstr->IterateArgInstrs([&](IR::Instr* argInstr) {
  2079. arrayArgOut = explicitThisArgOut;
  2080. explicitThisArgOut = implicitThisArgOut;
  2081. implicitThisArgOut = argInstr;
  2082. linkOpnd->AsSymOpnd()->m_sym->AsStackSym()->m_isInlinedArgSlot = true;
  2083. linkOpnd->AsSymOpnd()->m_sym->AsStackSym()->m_allocated = true;
  2084. ConvertToInlineBuiltInArgOut(argInstr);
  2085. argInstr->Move(argInsertInstr);
  2086. argInsertInstr = argInstr;
  2087. linkOpnd = argInstr->GetSrc2();
  2088. return false;
  2089. });
  2090. StackSym* originalCallTargetStackSym = callInstr->GetSrc1()->GetStackSym();
  2091. // If we optimized the call instruction for a fixed function we will have bailed out earlier if the function
  2092. // wasn't what we expected or was not a function at all. However, we must still check and bail out on heap arguments.
  2093. bool safeThis = false;
  2094. if (TryOptimizeCallInstrWithFixedMethod(callInstr, funcInfo, false /*isPolymorphic*/, true /*isBuiltIn*/, false /*isCtor*/, true /*isInlined*/, safeThis /*unused here*/))
  2095. {
  2096. Assert(callInstr->m_opcode == Js::OpCode::CallIFixed);
  2097. }
  2098. else
  2099. {
  2100. PrepareInsertionPoint(callInstr, funcInfo, callInstr);
  2101. }
  2102. // If we optimized the call instruction for a fixed function, we must extend the function object's lifetime until after the last bailout before the call.
  2103. if (callInstr->m_opcode == Js::OpCode::CallIFixed)
  2104. {
  2105. IR::ByteCodeUsesInstr * useCallTargetInstr = IR::ByteCodeUsesInstr::New(callInstr, originalCallTargetStackSym->m_id);
  2106. callInstr->InsertBefore(useCallTargetInstr);
  2107. }
  2108. IR::Instr* builtInEndInstr = InsertInlineeBuiltInStartEndTags(callInstr, 3); // 3 args (implicit this + explicit this + array = 3)
  2109. builtInEndInstr->m_opcode = Js::OpCode::InlineNonTrackingBuiltInEnd; // We will call EndTrackCall when we see CallDirect for reasons explained in GlobOpt::TrackCalls
  2110. IR::Instr * startCall = IR::Instr::New(Js::OpCode::StartCall,
  2111. IR::RegOpnd::New(TyVar, callInstr->m_func),
  2112. IR::IntConstOpnd::New(2, TyInt32, callInstr->m_func),
  2113. callInstr->m_func);
  2114. callInstr->InsertBefore(startCall);
  2115. StackSym * sym = callInstr->m_func->m_symTable->GetArgSlotSym((uint16)(1));
  2116. linkOpnd = IR::SymOpnd::New(sym, 0, TyMachPtr, callInstr->m_func);
  2117. IR::Instr * argOut = IR::Instr::New(Js::OpCode::ArgOut_A, linkOpnd, explicitThisArgOut->GetSrc1(), startCall->GetDst(), callInstr->m_func);
  2118. callInstr->InsertBefore(argOut);
  2119. sym = callInstr->m_func->m_symTable->GetArgSlotSym((uint16)(2));
  2120. linkOpnd = IR::SymOpnd::New(sym, 0, TyMachPtr, callInstr->m_func);
  2121. argOut = IR::Instr::New(Js::OpCode::ArgOut_A, linkOpnd, arrayArgOut->GetSrc1(), argOut->GetDst(), callInstr->m_func);
  2122. callInstr->InsertBefore(argOut);
  2123. linkOpnd = IR::SymOpnd::New(callInstr->m_func->m_symTable->GetArgSlotSym((uint16)(1)), 0, TyMachPtr, callInstr->m_func);
  2124. argOut = IR::Instr::New(Js::OpCode::ArgOut_A_InlineSpecialized, linkOpnd, implicitThisArgOut->GetSrc1(), argOut->GetDst(), callInstr->m_func);
  2125. callInstr->InsertBefore(argOut);
  2126. IR::HelperCallOpnd * helperCallOpnd = nullptr;
  2127. switch (builtInId)
  2128. {
  2129. case Js::BuiltinFunction::Math_Max:
  2130. helperCallOpnd = IR::HelperCallOpnd::New(IR::HelperOp_MaxInAnArray, callInstr->m_func);
  2131. break;
  2132. case Js::BuiltinFunction::Math_Min:
  2133. helperCallOpnd = IR::HelperCallOpnd::New(IR::HelperOp_MinInAnArray, callInstr->m_func);
  2134. break;
  2135. default:
  2136. Assert(false);
  2137. __assume(UNREACHED);
  2138. }
  2139. callInstr->m_opcode = Js::OpCode::CallDirect;
  2140. callInstr->ReplaceSrc1(helperCallOpnd);
  2141. callInstr->ReplaceSrc2(argOut->GetDst());
  2142. return callInstr;
  2143. }
  2144. bool Inline::InlineApplyTarget(IR::Instr *callInstr, const Js::FunctionCodeGenJitTimeData* inlinerData, const Js::FunctionCodeGenJitTimeData** pInlineeData, Js::FunctionInfo *applyFuncInfo,
  2145. const StackSym *symCallerThis, IR::Instr ** returnInstr, uint recursiveInlineDepth)
  2146. {
  2147. #if ENABLE_DEBUG_CONFIG_OPTIONS
  2148. wchar_t debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  2149. wchar_t debugStringBuffer2[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  2150. #endif
  2151. if (this->isApplyTargetInliningInProgress)
  2152. {
  2153. INLINE_TESTTRACE(L"INLINING: Skip Inline: Skipping apply target inlining, Recursive apply inlining is not supported \tCaller: %s\t(%s) \tTop Func:%s\t(%s)\n", inlinerData->GetFunctionBody()->GetDisplayName(),
  2154. inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer), this->topFunc->GetJnFunction()->GetDisplayName(), this->topFunc->GetJnFunction()->GetDebugNumberSet(debugStringBuffer2));
  2155. return false;
  2156. }
  2157. // Begin inlining apply target
  2158. IR::Opnd* applyOpnd = callInstr->GetSrc1();
  2159. Assert(applyOpnd->IsRegOpnd());
  2160. StackSym* applySym = applyOpnd->AsRegOpnd()->m_sym->AsStackSym();
  2161. if (!applySym->IsSingleDef())
  2162. {
  2163. return false;
  2164. }
  2165. IR::Instr* applyLdInstr = applySym->GetInstrDef();
  2166. IR::Instr* applyTargetLdInstr = applyLdInstr->m_prev;
  2167. if(applyTargetLdInstr->m_opcode != Js::OpCode::LdFldForCallApplyTarget)
  2168. {
  2169. return false;
  2170. }
  2171. IR::Opnd *applyTargetLdOpnd = applyTargetLdInstr->GetSrc1();
  2172. if (!applyTargetLdOpnd->IsSymOpnd() || !applyTargetLdOpnd->AsSymOpnd()->IsPropertySymOpnd())
  2173. {
  2174. return false;
  2175. }
  2176. const auto inlineCacheIndex = applyTargetLdOpnd->AsPropertySymOpnd()->m_inlineCacheIndex;
  2177. const auto inlineeData = inlinerData->GetLdFldInlinee(inlineCacheIndex);
  2178. if (SkipCallApplyTargetInlining_Shared(callInstr, inlinerData, inlineeData, /*isApplyTarget*/ true, /*isCallTarget*/ false))
  2179. {
  2180. *pInlineeData = inlineeData;
  2181. return false;
  2182. }
  2183. if (callInstr->m_func->IsTopFunc())
  2184. {
  2185. INLINE_TESTTRACE(L"INLINING: Skip Inline: Skipping apply target inlining in top func\tCaller: %s\t(%s) \tTop Func:%s\t(%s)\n", inlinerData->GetFunctionBody()->GetDisplayName(),
  2186. inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer), this->topFunc->GetJnFunction()->GetDisplayName(), this->topFunc->GetJnFunction()->GetDebugNumberSet(debugStringBuffer2));
  2187. return false;
  2188. }
  2189. StackSym* originalCallTargetStackSym = callInstr->GetSrc1()->GetStackSym();
  2190. bool safeThis = false;
  2191. if (!TryGetFixedMethodsForBuiltInAndTarget(callInstr, inlinerData, inlineeData, applyFuncInfo, applyLdInstr, applyTargetLdInstr, safeThis, /*isApplyTarget*/ true))
  2192. {
  2193. return false;
  2194. }
  2195. // o.foo.apply(obj, arguments)
  2196. //
  2197. // StartCall
  2198. // ArgOut_A <-- implicit "this" (foo) argout
  2199. // ArgOut_A <-- explicit "this" (obj) argout
  2200. // ArgOut_A <-- arguments object argout
  2201. // CallIFixed
  2202. IR::Instr* implicitThisArgOut = nullptr;
  2203. IR::Instr* explicitThisArgOut = nullptr;
  2204. IR::Instr* argumentsObjArgOut = nullptr;
  2205. callInstr->IterateArgInstrs([&](IR::Instr* argInstr)
  2206. {
  2207. argumentsObjArgOut = explicitThisArgOut;
  2208. explicitThisArgOut = implicitThisArgOut;
  2209. implicitThisArgOut = argInstr;
  2210. argInstr->GenerateBytecodeArgOutCapture(); // Generate BytecodeArgOutCapture here to capture the implicit "this" (to be removed) and arguments object (to be expanded) argouts,
  2211. // so that any bailout in the call sequence restores the argouts stack as the interpreter would expect it to be.
  2212. argInstr->GetDst()->AsSymOpnd()->GetStackSym()->DecrementArgSlotNum(); // We will be removing implicit "this" argout
  2213. return false;
  2214. });
  2215. if (safeThis)
  2216. {
  2217. IR::Instr * byteCodeArgOutCapture = explicitThisArgOut->GetBytecodeArgOutCapture();
  2218. Assert(byteCodeArgOutCapture->GetSrc1()->IsRegOpnd());
  2219. if (byteCodeArgOutCapture->GetSrc1()->AsRegOpnd()->GetStackSym() != symCallerThis)
  2220. {
  2221. safeThis = false;
  2222. }
  2223. }
  2224. IR::Opnd *src1 = argumentsObjArgOut->GetSrc1();
  2225. IR::Instr* ldHeapArguments = src1->AsRegOpnd()->m_sym->m_instrDef;
  2226. IR::RegOpnd* argumentsObj = IR::RegOpnd::New(TyVar, callInstr->m_func);
  2227. IR::Instr *assignInstr = IR::Instr::New(Js::OpCode::LdArgumentsFromStack, argumentsObj, ldHeapArguments->GetDst(), callInstr->m_func);
  2228. assignInstr->SetByteCodeOffset(argumentsObjArgOut);
  2229. argumentsObjArgOut->InsertBefore(assignInstr);
  2230. IR::Instr* argObjByteCodeArgoutCapture = argumentsObjArgOut->GetBytecodeArgOutCapture();
  2231. argObjByteCodeArgoutCapture->GetDst()->GetStackSym()->m_nonEscapingArgObjAlias = true;
  2232. argumentsObjArgOut->m_opcode = Js::OpCode::ArgOut_A_FromStackArgs;
  2233. Assert(implicitThisArgOut->GetSrc2()->IsRegOpnd());
  2234. IR::Instr * startCall = implicitThisArgOut->GetSrc2()->AsRegOpnd()->m_sym->AsStackSym()->GetInstrDef();
  2235. Assert(startCall->m_opcode == Js::OpCode::StartCall);
  2236. IR::Instr * bailOutOnNotStackArgs = IR::BailOutInstr::New(Js::OpCode::BailOnNotStackArgs, IR::BailOutOnInlineFunction,
  2237. callInstr, callInstr->m_func);
  2238. bailOutOnNotStackArgs->SetSrc1(argumentsObj);
  2239. bailOutOnNotStackArgs->SetSrc2(IR::AddrOpnd::NewNull(callInstr->m_func));
  2240. argumentsObjArgOut->InsertBefore(bailOutOnNotStackArgs);
  2241. IR::Instr* byteCodeArgOutUse = IR::Instr::New(Js::OpCode::BytecodeArgOutUse, callInstr->m_func);
  2242. byteCodeArgOutUse->SetSrc1(implicitThisArgOut->GetSrc1());
  2243. byteCodeArgOutUse->SetSrc2(argumentsObjArgOut->GetSrc1());
  2244. callInstr->InsertBefore(byteCodeArgOutUse);
  2245. // don't need the implicit "this" anymore
  2246. explicitThisArgOut->ReplaceSrc2(startCall->GetDst());
  2247. implicitThisArgOut->Remove();
  2248. startCall->SetSrc2(IR::IntConstOpnd::New(startCall->GetArgOutCount(/*getInterpreterArgOutCount*/ false), TyUint32, startCall->m_func));
  2249. startCall->GetSrc1()->AsIntConstOpnd()->IncrValue(-1); // update the count of argouts as seen by JIT, in the start call instruction
  2250. *returnInstr = InlineCallApplyTarget_Shared(callInstr, originalCallTargetStackSym, inlineeData->GetFunctionInfo(), inlineeData, inlineCacheIndex,
  2251. safeThis, /*isApplyTarget*/ true, /*isCallTarget*/ false, recursiveInlineDepth);
  2252. return true;
  2253. }
  2254. IR::Instr *
  2255. Inline::InlineCallApplyTarget_Shared(IR::Instr *callInstr, StackSym* originalCallTargetStackSym, Js::FunctionInfo *funcInfo, const Js::FunctionCodeGenJitTimeData *const inlineeData,
  2256. uint inlineCacheIndex, bool safeThis, bool isApplyTarget, bool isCallTarget, uint recursiveInlineDepth)
  2257. {
  2258. Assert(isApplyTarget ^ isCallTarget);
  2259. // function body
  2260. Js::FunctionBody* funcBody = funcInfo->GetFunctionBody();
  2261. // returnValueOpnd
  2262. IR::RegOpnd * returnValueOpnd;
  2263. Js::RegSlot returnRegSlot;
  2264. if (callInstr->GetDst())
  2265. {
  2266. returnValueOpnd = callInstr->UnlinkDst()->AsRegOpnd();
  2267. returnRegSlot = returnValueOpnd->m_sym->GetByteCodeRegSlot();
  2268. }
  2269. else
  2270. {
  2271. returnValueOpnd = nullptr;
  2272. returnRegSlot = Js::Constants::NoRegister;
  2273. }
  2274. Assert(callInstr->IsProfiledInstr());
  2275. Js::ProfileId callSiteId = static_cast<Js::ProfileId>(callInstr->AsProfiledInstr()->u.profileId);
  2276. // inlinee
  2277. Js::ProxyEntryPointInfo *defaultEntryPointInfo = funcBody->GetDefaultEntryPointInfo();
  2278. Assert(defaultEntryPointInfo->IsFunctionEntryPointInfo());
  2279. Js::FunctionEntryPointInfo *functionEntryPointInfo = static_cast<Js::FunctionEntryPointInfo*>(defaultEntryPointInfo);
  2280. JsFunctionCodeGen *workItem = JitAnew(this->topFunc->m_alloc, JsFunctionCodeGen,
  2281. funcBody->GetScriptContext()->GetNativeCodeGenerator(), funcBody, functionEntryPointInfo, this->topFunc->IsJitInDebugMode());
  2282. workItem->SetRecyclableData(JitAnew(this->topFunc->m_alloc, Js::CodeGenRecyclableData, inlineeData));
  2283. workItem->SetJitMode(this->topFunc->m_workItem->GetJitMode());
  2284. const auto profileInfo =
  2285. JitAnew(
  2286. this->topFunc->m_alloc,
  2287. Js::ReadOnlyDynamicProfileInfo,
  2288. funcBody->HasDynamicProfileInfo() ? funcBody->GetAnyDynamicProfileInfo() : nullptr,
  2289. this->topFunc->IsBackgroundJIT() ? this->topFunc->m_alloc : nullptr);
  2290. Js::EntryPointPolymorphicInlineCacheInfo * entryPointPolymorphicInlineCacheInfo = this->topFunc->m_workItem->GetEntryPoint()->GetPolymorphicInlineCacheInfo();
  2291. Func *inlinee = JitAnew(this->topFunc->m_alloc,
  2292. Func,
  2293. this->topFunc->m_alloc,
  2294. workItem,
  2295. callInstr->m_func->m_runtimeData ?
  2296. callInstr->m_func->m_runtimeData->GetLdFldInlinee(inlineCacheIndex) :
  2297. this->topFunc->GetJnFunction()->GetLdFldInlineeCodeGenRuntimeData(inlineCacheIndex),
  2298. entryPointPolymorphicInlineCacheInfo ? entryPointPolymorphicInlineCacheInfo->GetInlineeInfo(funcBody) : nullptr,
  2299. this->topFunc->GetCodeGenAllocators(),
  2300. this->topFunc->GetNumberAllocator(),
  2301. profileInfo,
  2302. this->topFunc->GetCodeGenProfiler(),
  2303. this->topFunc->IsBackgroundJIT(),
  2304. callInstr->m_func,
  2305. callInstr->m_next->GetByteCodeOffset(),
  2306. returnRegSlot,
  2307. false,
  2308. callSiteId,
  2309. false);
  2310. // instrNext
  2311. IR::Instr* instrNext = callInstr->m_next;
  2312. return InlineFunctionCommon(callInstr, originalCallTargetStackSym, funcBody, inlinee, instrNext, returnValueOpnd, callInstr, nullptr, recursiveInlineDepth, safeThis, isApplyTarget);
  2313. }
  2314. IR::Opnd *
  2315. Inline::ConvertToInlineBuiltInArgOut(IR::Instr * argInstr)
  2316. {
  2317. argInstr->m_opcode = Js::OpCode::ArgOut_A_InlineBuiltIn;
  2318. argInstr->GenerateBytecodeArgOutCapture();
  2319. return argInstr->GetSrc1();
  2320. }
  2321. IR::Instr*
  2322. Inline::InlineCall(IR::Instr *callInstr, Js::FunctionInfo *funcInfo, const Js::FunctionCodeGenJitTimeData* inlinerData, const StackSym *symCallerThis, bool* pIsInlined, uint callSiteId, uint recursiveInlineDepth)
  2323. {
  2324. Js::BuiltinFunction builtInId = Js::JavascriptLibrary::GetBuiltInForFuncInfo(funcInfo, callInstr->m_func->GetScriptContext());
  2325. Func *func = callInstr->m_func;
  2326. *pIsInlined = false;
  2327. if (PHASE_OFF(Js::InlineCallPhase, this->topFunc) || PHASE_OFF(Js::InlineCallPhase, func->GetJnFunction())
  2328. || !this->topFunc->GetJnFunction()->GetInParamsCount())
  2329. {
  2330. return callInstr;
  2331. }
  2332. // Convert all the current ARG_OUT to ArgOut_A_InlineBuiltIn
  2333. IR::Opnd *linkOpnd = callInstr->GetSrc2();
  2334. if (!GetDefInstr(linkOpnd)->GetSrc2()->IsSymOpnd())
  2335. {
  2336. // There is no benefit of inlining.call() with no arguments.
  2337. return callInstr;
  2338. }
  2339. *pIsInlined = true;
  2340. const Js::FunctionCodeGenJitTimeData * inlineeData = nullptr;
  2341. IR::Instr * returnInstr = nullptr;
  2342. if (!PHASE_OFF(Js::InlineCallTargetPhase, this->topFunc))
  2343. {
  2344. if (InlineCallTarget(callInstr, inlinerData, &inlineeData, funcInfo, symCallerThis, &returnInstr, recursiveInlineDepth))
  2345. {
  2346. Assert(returnInstr);
  2347. return returnInstr;
  2348. }
  2349. }
  2350. #if defined(ENABLE_DEBUG_CONFIG_OPTIONS)
  2351. InliningDecider::TraceInlining(inlinerData->GetFunctionBody(), Js::JavascriptLibrary::GetNameForBuiltIn(builtInId),
  2352. nullptr, 0, this->topFunc->m_workItem->GetFunctionBody(), 0, nullptr, callSiteId, builtInId);
  2353. #endif
  2354. uint actualCount = 0;
  2355. Assert(linkOpnd->IsSymOpnd());
  2356. // We are trying to optimize this.superConstructor.call(this, a, b,c);
  2357. // argImplicitInstr represents this.superConstructor which we need to call directly.
  2358. IR::Instr *argImplicitInstr;
  2359. IR::Instr* argInsertInstr = callInstr;
  2360. callInstr->IterateArgInstrs([&](IR::Instr* argInstr) {
  2361. argImplicitInstr = argInstr;
  2362. ++actualCount;
  2363. linkOpnd->AsSymOpnd()->m_sym->AsStackSym()->m_isInlinedArgSlot = true;
  2364. linkOpnd->AsSymOpnd()->m_sym->AsStackSym()->m_allocated = true;
  2365. ConvertToInlineBuiltInArgOut(argInstr);
  2366. // Move the arguments next to the call.
  2367. argInstr->Move(argInsertInstr);
  2368. argInsertInstr = argInstr;
  2369. linkOpnd = argInstr->GetSrc2();
  2370. return false;
  2371. });
  2372. linkOpnd->AsRegOpnd()->m_sym->m_isInlinedArgSlot = true;
  2373. IR::SymOpnd* orgLinkOpnd = callInstr->GetSrc2()->AsSymOpnd();
  2374. // Save off the call target operand (function object) so we can extend its lifetime as needed, even if
  2375. // the call instruction gets transformed to CallIFixed.
  2376. StackSym* originalCallTargetStackSym = callInstr->GetSrc1()->GetStackSym();
  2377. bool safeThis = false;
  2378. if (!TryOptimizeCallInstrWithFixedMethod(callInstr, funcInfo, false /*isPolymorphic*/, true /*isBuiltIn*/, false /*isCtor*/, true /*isInlined*/, safeThis))
  2379. {
  2380. PrepareInsertionPoint(callInstr, funcInfo, callInstr);
  2381. }
  2382. else
  2383. {
  2384. Assert(callInstr->m_opcode == Js::OpCode::CallIFixed);
  2385. // If we optimized the call instruction for a fixed function, we must extend the function object's lifetime until after
  2386. // the bailout on non-stack arguments.
  2387. IR::ByteCodeUsesInstr * useCallTargetInstr = IR::ByteCodeUsesInstr::New(callInstr, originalCallTargetStackSym->m_id);
  2388. callInstr->InsertBefore(useCallTargetInstr);
  2389. }
  2390. InsertInlineeBuiltInStartEndTags(callInstr, actualCount);
  2391. uint actualCountToInlinedCall = actualCount - 1;
  2392. IR::Instr *startCall = IR::Instr::New(Js::OpCode::StartCall, func);
  2393. startCall->SetDst(IR::RegOpnd::New(TyVar, func));
  2394. startCall->SetSrc1(IR::IntConstOpnd::New(actualCountToInlinedCall, TyInt32, func)); // New call will have one less parameter.
  2395. callInstr->InsertBefore(startCall);
  2396. callInstr->ReplaceSrc1(argImplicitInstr->GetSrc1());
  2397. callInstr->UnlinkSrc2();
  2398. callInstr->m_opcode = Js::OpCode::CallI;
  2399. IR::Instr* insertBeforeInstr = callInstr;
  2400. IR::Instr* clonedArgout = nullptr;
  2401. IR::Instr* orgArgout = nullptr;
  2402. for (uint i = actualCountToInlinedCall ; i > 0; i--)
  2403. {
  2404. orgArgout = GetDefInstr(orgLinkOpnd);
  2405. orgLinkOpnd = orgArgout->GetSrc2()->AsSymOpnd();
  2406. IR::Opnd *orgSrc1 = orgArgout->GetSrc1();
  2407. // Change ArgOut to use temp as src1.
  2408. StackSym * stackSym = StackSym::New(orgSrc1->GetStackSym()->GetType(), argImplicitInstr->m_func);
  2409. IR::Opnd* tempDst = IR::RegOpnd::New(stackSym, orgSrc1->GetType(), argImplicitInstr->m_func);
  2410. IR::Instr *assignInstr = IR::Instr::New(Js::OpCode::Ld_A, tempDst, orgSrc1, argImplicitInstr->m_func);
  2411. assignInstr->SetByteCodeOffset(orgArgout);
  2412. tempDst->SetIsJITOptimizedReg(true);
  2413. orgArgout->InsertBefore(assignInstr);
  2414. StackSym *symDst = callInstr->m_func->m_symTable->GetArgSlotSym((uint16)(i));
  2415. IR::SymOpnd* newLinkOpnd = IR::SymOpnd::New(symDst, 0, TyMachPtr, func);
  2416. clonedArgout = IR::Instr::New(Js::OpCode::ArgOut_A, newLinkOpnd, tempDst, func);
  2417. insertBeforeInstr->SetSrc2(newLinkOpnd);
  2418. insertBeforeInstr->InsertBefore(clonedArgout);
  2419. insertBeforeInstr = clonedArgout;
  2420. }
  2421. clonedArgout->SetSrc2(startCall->GetDst());
  2422. Assert(GetDefInstr(orgLinkOpnd) == argImplicitInstr);
  2423. return callInstr;
  2424. }
  2425. bool
  2426. Inline::InlineCallTarget(IR::Instr *callInstr, const Js::FunctionCodeGenJitTimeData* inlinerData, const Js::FunctionCodeGenJitTimeData** pInlineeData, Js::FunctionInfo *callFuncInfo,
  2427. const StackSym *symCallerThis, IR::Instr ** returnInstr, uint recursiveInlineDepth)
  2428. {
  2429. IR::Opnd* src1 = callInstr->GetSrc1();
  2430. Assert(src1->IsRegOpnd());
  2431. StackSym* sym = src1->AsRegOpnd()->GetStackSym();
  2432. if (!sym->IsSingleDef())
  2433. {
  2434. return false;
  2435. }
  2436. IR::Instr* callLdInstr = sym->GetInstrDef();
  2437. Assert(callLdInstr);
  2438. IR::Instr* callTargetLdInstr = callLdInstr->m_prev;
  2439. if (callTargetLdInstr->m_opcode != Js::OpCode::LdFldForCallApplyTarget)
  2440. {
  2441. return false;
  2442. }
  2443. IR::Opnd* callTargetLdOpnd = callTargetLdInstr->GetSrc1();
  2444. if (!callTargetLdOpnd->IsSymOpnd() || !callTargetLdOpnd->AsSymOpnd()->IsPropertySymOpnd())
  2445. {
  2446. return false;
  2447. }
  2448. const auto inlineCacheIndex = callTargetLdOpnd->AsPropertySymOpnd()->m_inlineCacheIndex;
  2449. const auto inlineeData = inlinerData->GetLdFldInlinee(inlineCacheIndex);
  2450. if (SkipCallApplyTargetInlining_Shared(callInstr, inlinerData, inlineeData, /*isApplyTarget*/ false, /*isCallTarget*/ true))
  2451. {
  2452. *pInlineeData = inlineeData;
  2453. return false;
  2454. }
  2455. StackSym* originalCallTargetStackSym = callInstr->GetSrc1()->GetStackSym();
  2456. bool safeThis = false;
  2457. if (!TryGetFixedMethodsForBuiltInAndTarget(callInstr, inlinerData, inlineeData, callFuncInfo, callLdInstr, callTargetLdInstr, safeThis, /*isApplyTarget*/ false))
  2458. {
  2459. return false;
  2460. }
  2461. IR::Instr* implicitThisArgOut = nullptr;
  2462. IR::Instr* explicitThisArgOut = nullptr;
  2463. callInstr->IterateArgInstrs([&] (IR::Instr* argInstr)
  2464. {
  2465. explicitThisArgOut = implicitThisArgOut;
  2466. implicitThisArgOut = argInstr;
  2467. argInstr->GenerateBytecodeArgOutCapture(); // Generate BytecodeArgOutCapture here to capture the implicit "this" argout (which will be removed) as well,
  2468. // so that any bailout in the call sequence restores the argouts stack as the interpreter would expect it to be.
  2469. argInstr->GetDst()->AsSymOpnd()->GetStackSym()->DecrementArgSlotNum(); // We will be removing implicit "this" argout
  2470. return false;
  2471. });
  2472. Assert(explicitThisArgOut);
  2473. Assert(explicitThisArgOut->HasByteCodeArgOutCapture());
  2474. if (safeThis)
  2475. {
  2476. IR::Instr * byteCodeArgOutCapture = explicitThisArgOut->GetBytecodeArgOutCapture();
  2477. Assert(byteCodeArgOutCapture->GetSrc1()->IsRegOpnd());
  2478. if (byteCodeArgOutCapture->GetSrc1()->AsRegOpnd()->GetStackSym() != symCallerThis)
  2479. {
  2480. safeThis = false;
  2481. }
  2482. }
  2483. IR::Opnd* linkOpnd = implicitThisArgOut->GetSrc2();
  2484. Assert(linkOpnd->IsRegOpnd() && linkOpnd->AsRegOpnd()->GetStackSym()->IsSingleDef());
  2485. Assert(linkOpnd->AsRegOpnd()->GetStackSym()->GetInstrDef()->m_opcode == Js::OpCode::StartCall);
  2486. IR::Instr* startCall = linkOpnd->AsRegOpnd()->GetStackSym()->GetInstrDef();
  2487. explicitThisArgOut->ReplaceSrc2(startCall->GetDst());
  2488. IR::Instr * bytecodeArgOutUse = IR::Instr::New(Js::OpCode::BytecodeArgOutUse, callInstr->m_func);
  2489. bytecodeArgOutUse->SetSrc1(implicitThisArgOut->GetSrc1());
  2490. callInstr->InsertBefore(bytecodeArgOutUse); // Need to keep the implicit "this" argout live till the call instruction for it to be captured by any bailout in the call sequence.
  2491. implicitThisArgOut->Remove();
  2492. startCall->SetSrc2(IR::IntConstOpnd::New(startCall->GetArgOutCount(/*getInterpreterArgOutCount*/ false), TyUint32, startCall->m_func));
  2493. startCall->GetSrc1()->AsIntConstOpnd()->SetValue(startCall->GetSrc1()->AsIntConstOpnd()->GetValue() - 1);
  2494. *returnInstr = InlineCallApplyTarget_Shared(callInstr, originalCallTargetStackSym, inlineeData->GetFunctionInfo(), inlineeData, inlineCacheIndex,
  2495. safeThis, /*isApplyTarget*/ false, /*isCallTarget*/ true, recursiveInlineDepth);
  2496. return true;
  2497. }
  2498. bool
  2499. Inline::SkipCallApplyTargetInlining_Shared(IR::Instr *callInstr, const Js::FunctionCodeGenJitTimeData* inlinerData, const Js::FunctionCodeGenJitTimeData* inlineeData, bool isApplyTarget, bool isCallTarget)
  2500. {
  2501. #if ENABLE_DEBUG_CONFIG_OPTIONS
  2502. wchar_t debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  2503. wchar_t debugStringBuffer2[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  2504. wchar_t debugStringBuffer3[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  2505. #endif
  2506. Assert(isApplyTarget ^ isCallTarget);
  2507. if (PHASE_OFF(Js::FixedMethodsPhase, callInstr->m_func->GetJnFunction()))
  2508. {
  2509. INLINE_TESTTRACE(L"INLINING: Skip Inline: Skipping %s target inlining, Fixed Methods turned off\tCaller: %s\t(#%d) \tTop Func:%s\t(#%d)\n", isApplyTarget ? L"apply" : L"call" ,
  2510. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer),
  2511. this->topFunc->GetJnFunction()->GetDisplayName(), this->topFunc->GetJnFunction()->GetDebugNumberSet(debugStringBuffer2));
  2512. return true;
  2513. }
  2514. if (!inlineeData)
  2515. {
  2516. INLINE_TESTTRACE(L"INLINING: Skip Inline: Skipping %s target inlining, inlineeData not present\tCaller: %s\t(#%d) \tTop Func:%s\t(#%d)\n", isApplyTarget ? L"apply" : L"call",
  2517. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer),
  2518. this->topFunc->GetJnFunction()->GetDisplayName(), this->topFunc->GetJnFunction()->GetDebugNumberSet(debugStringBuffer2));
  2519. return true;
  2520. }
  2521. if (!inlineeData->GetFunctionBody())
  2522. {
  2523. if (isCallTarget)
  2524. {
  2525. INLINE_TESTTRACE(L"INLINING: Skip Inline: Skipping .call inlining, target is a built-in\tCaller: %s\t(#%d) \tTop Func:%s\t(#%d)\n",
  2526. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer),
  2527. this->topFunc->GetJnFunction()->GetDisplayName(), this->topFunc->GetJnFunction()->GetDebugNumberSet(debugStringBuffer2));
  2528. }
  2529. return true;
  2530. }
  2531. if (!inlinerData->IsLdFldInlineePresent())
  2532. {
  2533. INLINE_TESTTRACE(L"INLINING: Skip Inline: Skipping %s target inlining, not registered as a LdFld inlinee \tInlinee: %s (#%d)\tCaller: %s\t(#%d) \tTop Func:%s\t(#%d)\n", isApplyTarget ? L"apply" : L"call",
  2534. inlineeData->GetFunctionBody()->GetDisplayName(), inlineeData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer),
  2535. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer2),
  2536. this->topFunc->GetJnFunction()->GetDisplayName(), this->topFunc->GetJnFunction()->GetDebugNumberSet(debugStringBuffer3));
  2537. return true;
  2538. }
  2539. return false;
  2540. }
  2541. bool
  2542. Inline::TryGetFixedMethodsForBuiltInAndTarget(IR::Instr *callInstr, const Js::FunctionCodeGenJitTimeData* inlinerData, const Js::FunctionCodeGenJitTimeData* inlineeData, Js::FunctionInfo *builtInFuncInfo,
  2543. IR::Instr* builtInLdInstr, IR::Instr* targetLdInstr, bool& safeThis, bool isApplyTarget)
  2544. {
  2545. #if ENABLE_DEBUG_CONFIG_OPTIONS
  2546. wchar_t debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  2547. wchar_t debugStringBuffer2[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  2548. wchar_t debugStringBuffer3[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  2549. #endif
  2550. Assert(isApplyTarget || (Js::JavascriptLibrary::GetBuiltInForFuncInfo(builtInFuncInfo, callInstr->m_func->GetScriptContext()) == Js::BuiltinFunction::Function_Call));
  2551. Js::OpCode originalCallOpCode = callInstr->m_opcode;
  2552. StackSym* originalCallTargetStackSym = callInstr->GetSrc1()->GetStackSym();
  2553. IR::ByteCodeUsesInstr * useCallTargetInstr = IR::ByteCodeUsesInstr::New(callInstr->m_func);
  2554. useCallTargetInstr->SetByteCodeOffset(callInstr);
  2555. useCallTargetInstr->byteCodeUpwardExposedUsed = JitAnew(callInstr->m_func->m_alloc, BVSparse<JitArenaAllocator>, callInstr->m_func->m_alloc);
  2556. Js::FunctionInfo* targetFunctionInfo = inlineeData->GetFunctionInfo();
  2557. safeThis = false;
  2558. // Check if we can get fixed method for call
  2559. if (TryOptimizeCallInstrWithFixedMethod(callInstr, builtInFuncInfo/*funcinfo for call*/, false /*isPolymorphic*/, false /*isBuiltIn*/, false /*isCtor*/, true /*isInlined*/,
  2560. safeThis /*unused here*/, true /*dontOptimizeJustCheck*/))
  2561. {
  2562. Assert(callInstr->m_opcode == originalCallOpCode); // check that we didn't change the opcode to CallIFixed.
  2563. callInstr->ReplaceSrc1(targetLdInstr->GetDst());
  2564. safeThis = false;
  2565. // Check if we can get fixed method for call target
  2566. if (!TryOptimizeCallInstrWithFixedMethod(callInstr, targetFunctionInfo, false /*isPolymorphic*/, false /*isBuiltIn*/, false /*isCtor*/, true /*isInlined*/,
  2567. safeThis /*unused here*/, true /*dontOptimizeJustCheck*/))
  2568. {
  2569. callInstr->ReplaceSrc1(builtInLdInstr->GetDst());
  2570. INLINE_TESTTRACE(L"INLINING: Skip Inline: Skipping %s target inlining, did not get fixed method for %s target \tInlinee: %s (#%d)\tCaller: %s\t(#%d) \tTop Func:%s\t(#%d)\n", isApplyTarget ? L"apply" : L"call", isApplyTarget ? L"apply" : L"call",
  2571. inlineeData->GetFunctionBody()->GetDisplayName(), inlineeData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer),
  2572. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer2),
  2573. this->topFunc->GetJnFunction()->GetDisplayName(), this->topFunc->GetJnFunction()->GetDebugNumberSet(debugStringBuffer3));
  2574. return false;
  2575. }
  2576. }
  2577. else
  2578. {
  2579. INLINE_TESTTRACE(L"INLINING: Skip Inline: Skipping %s target inlining, did not get fixed method for %s \tInlinee: %s (#%d)\tCaller: %s\t(#%d) \tTop Func:%s\t(#%d)\n", isApplyTarget ? L"apply" : L"call", isApplyTarget ? L"apply" : L"call",
  2580. inlineeData->GetFunctionBody()->GetDisplayName(), inlineeData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer),
  2581. inlinerData->GetFunctionBody()->GetDisplayName(), inlinerData->GetFunctionBody()->GetDebugNumberSet(debugStringBuffer2),
  2582. this->topFunc->GetJnFunction()->GetDisplayName(), this->topFunc->GetJnFunction()->GetDebugNumberSet(debugStringBuffer3));
  2583. return false;
  2584. }
  2585. if (isApplyTarget)
  2586. {
  2587. callInstr->m_func->SetHasApplyTargetInlining();
  2588. }
  2589. Assert(callInstr->m_opcode == originalCallOpCode);
  2590. callInstr->ReplaceSrc1(builtInLdInstr->GetDst());
  2591. // Emit Fixed Method check for apply/call
  2592. safeThis = false;
  2593. TryOptimizeCallInstrWithFixedMethod(callInstr, builtInFuncInfo/*funcinfo for apply/call */, false /*isPolymorphic*/, false /*isBuiltIn*/, false /*isCtor*/, true /*isInlined*/, safeThis /*unused here*/);
  2594. // If we optimized the call instruction for a fixed function, we must extend the function object's lifetime until after
  2595. // the bailout on non-stack arguments.
  2596. Assert(callInstr->m_opcode == Js::OpCode::CallIFixed);
  2597. useCallTargetInstr->byteCodeUpwardExposedUsed->Set(originalCallTargetStackSym->m_id);
  2598. // Make the target of apply/call as the target of the call instruction
  2599. callInstr->ReplaceSrc1(targetLdInstr->GetDst());
  2600. callInstr->m_opcode = originalCallOpCode;
  2601. //Emit Fixed Method check for apply/call target
  2602. originalCallTargetStackSym = callInstr->GetSrc1()->GetStackSym();
  2603. safeThis = false;
  2604. TryOptimizeCallInstrWithFixedMethod(callInstr, targetFunctionInfo, false /*isPolymorphic*/, false /*isBuiltIn*/, false /*isCtor*/, true /*isInlined*/, safeThis /*unused here*/);
  2605. // If we optimized the call instruction for a fixed function, we must extend the function object's lifetime until after
  2606. // the bailout on non-stack arguments.
  2607. Assert(callInstr->m_opcode == Js::OpCode::CallIFixed);
  2608. useCallTargetInstr->byteCodeUpwardExposedUsed->Set(originalCallTargetStackSym->m_id);
  2609. callInstr->InsertBefore(useCallTargetInstr);
  2610. return true;
  2611. }
  2612. void
  2613. Inline::SetupInlineInstrForCallDirect(Js::BuiltinFunction builtInId, IR::Instr* callInstr, IR::Instr* argoutInstr)
  2614. {
  2615. switch(builtInId)
  2616. {
  2617. case Js::BuiltinFunction::Array_Concat:
  2618. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperArray_Concat, callInstr->m_func));
  2619. break;
  2620. case Js::BuiltinFunction::Array_IndexOf:
  2621. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperArray_IndexOf, callInstr->m_func));
  2622. break;
  2623. case Js::BuiltinFunction::Array_Includes:
  2624. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperArray_Includes, callInstr->m_func));
  2625. break;
  2626. case Js::BuiltinFunction::Array_Join:
  2627. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperArray_Join, callInstr->m_func));
  2628. break;
  2629. case Js::BuiltinFunction::Array_LastIndexOf:
  2630. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperArray_LastIndexOf, callInstr->m_func));
  2631. break;
  2632. case Js::BuiltinFunction::Array_Reverse:
  2633. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperArray_Reverse, callInstr->m_func));
  2634. break;
  2635. case Js::BuiltinFunction::Array_Shift:
  2636. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperArray_Shift, callInstr->m_func));
  2637. break;
  2638. case Js::BuiltinFunction::Array_Slice:
  2639. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperArray_Slice, callInstr->m_func));
  2640. break;
  2641. case Js::BuiltinFunction::Array_Splice:
  2642. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperArray_Splice, callInstr->m_func));
  2643. break;
  2644. case Js::BuiltinFunction::Array_Unshift:
  2645. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperArray_Unshift, callInstr->m_func));
  2646. break;
  2647. case Js::BuiltinFunction::String_Concat:
  2648. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperString_Concat, callInstr->m_func));
  2649. break;
  2650. case Js::BuiltinFunction::String_CharCodeAt:
  2651. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperString_CharCodeAt, callInstr->m_func));
  2652. break;
  2653. case Js::BuiltinFunction::String_CharAt:
  2654. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperString_CharAt, callInstr->m_func));
  2655. break;
  2656. case Js::BuiltinFunction::String_FromCharCode:
  2657. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperString_FromCharCode, callInstr->m_func));
  2658. break;
  2659. case Js::BuiltinFunction::String_FromCodePoint:
  2660. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperString_FromCodePoint, callInstr->m_func));
  2661. break;
  2662. case Js::BuiltinFunction::String_IndexOf:
  2663. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperString_IndexOf, callInstr->m_func));
  2664. break;
  2665. case Js::BuiltinFunction::String_LastIndexOf:
  2666. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperString_LastIndexOf, callInstr->m_func));
  2667. break;
  2668. case Js::BuiltinFunction::String_Link:
  2669. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperString_Link, callInstr->m_func));
  2670. break;
  2671. case Js::BuiltinFunction::String_LocaleCompare:
  2672. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperString_LocaleCompare, callInstr->m_func));
  2673. break;
  2674. case Js::BuiltinFunction::String_Match:
  2675. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperString_Match, callInstr->m_func));
  2676. break;
  2677. case Js::BuiltinFunction::String_Replace:
  2678. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperString_Replace, callInstr->m_func));
  2679. break;
  2680. case Js::BuiltinFunction::String_Search:
  2681. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperString_Search, callInstr->m_func));
  2682. break;
  2683. case Js::BuiltinFunction::String_Slice:
  2684. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperString_Slice, callInstr->m_func));
  2685. break;
  2686. case Js::BuiltinFunction::String_Split:
  2687. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperString_Split, callInstr->m_func));
  2688. break;
  2689. case Js::BuiltinFunction::String_Substr:
  2690. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperString_Substr, callInstr->m_func));
  2691. break;
  2692. case Js::BuiltinFunction::String_Substring:
  2693. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperString_Substring, callInstr->m_func));
  2694. break;
  2695. case Js::BuiltinFunction::String_ToLocaleLowerCase:
  2696. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperString_ToLocaleLowerCase, callInstr->m_func));
  2697. break;
  2698. case Js::BuiltinFunction::String_ToLocaleUpperCase:
  2699. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperString_ToLocaleUpperCase, callInstr->m_func));
  2700. break;
  2701. case Js::BuiltinFunction::String_ToLowerCase:
  2702. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperString_ToLowerCase, callInstr->m_func));
  2703. break;
  2704. case Js::BuiltinFunction::String_ToUpperCase:
  2705. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperString_ToUpperCase, callInstr->m_func));
  2706. break;
  2707. case Js::BuiltinFunction::String_Trim:
  2708. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperString_Trim, callInstr->m_func));
  2709. break;
  2710. case Js::BuiltinFunction::String_TrimLeft:
  2711. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperString_TrimLeft, callInstr->m_func));
  2712. break;
  2713. case Js::BuiltinFunction::String_TrimRight:
  2714. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperString_TrimRight, callInstr->m_func));
  2715. break;
  2716. case Js::BuiltinFunction::String_PadStart:
  2717. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperString_PadStart, callInstr->m_func));
  2718. break;
  2719. case Js::BuiltinFunction::String_PadEnd:
  2720. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperString_PadEnd, callInstr->m_func));
  2721. break;
  2722. case Js::BuiltinFunction::GlobalObject_ParseInt:
  2723. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperGlobalObject_ParseInt, callInstr->m_func));
  2724. break;
  2725. case Js::BuiltinFunction::RegExp_Exec:
  2726. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperRegExp_Exec, callInstr->m_func));
  2727. break;
  2728. case Js::BuiltinFunction::RegExp_SymbolSearch:
  2729. callInstr->SetSrc1(IR::HelperCallOpnd::New(IR::JnHelperMethod::HelperRegExp_SymbolSearch, callInstr->m_func));
  2730. break;
  2731. };
  2732. callInstr->SetSrc2(argoutInstr->GetDst());
  2733. return;
  2734. }
  2735. void
  2736. Inline::WrapArgsOutWithCoerse(Js::BuiltinFunction builtInId, IR::Instr* callInstr)
  2737. {
  2738. switch (builtInId)
  2739. {
  2740. case Js::BuiltinFunction::String_Match:
  2741. callInstr->ForEachCallDirectArgOutInstrBackward([&](IR::Instr *argOutInstr, uint argNum)
  2742. {
  2743. IR::Instr * newInstr = nullptr;
  2744. bool isPreOpBailOutNeeded = false;
  2745. if (argNum == 0)
  2746. {
  2747. newInstr = argOutInstr->HoistSrc1(Js::OpCode::Coerse_Str);
  2748. isPreOpBailOutNeeded = true;
  2749. newInstr->GetDst()->SetValueType(ValueType::String);
  2750. newInstr->SetSrc2(IR::AddrOpnd::New(L"String.prototype.match", IR::AddrOpndKindSz, newInstr->m_func));
  2751. argOutInstr->GetSrc1()->SetValueType(ValueType::String);
  2752. }
  2753. else if (argNum == 1)
  2754. {
  2755. newInstr = argOutInstr->HoistSrc1(Js::OpCode::Coerse_Regex);
  2756. isPreOpBailOutNeeded = true;
  2757. }
  2758. if (isPreOpBailOutNeeded)
  2759. {
  2760. newInstr->SetByteCodeOffset(argOutInstr);
  2761. newInstr->forcePreOpBailOutIfNeeded = true;
  2762. }
  2763. return false;
  2764. }, 2);
  2765. break;
  2766. case Js::BuiltinFunction::String_Replace:
  2767. callInstr->ForEachCallDirectArgOutInstrBackward([&](IR::Instr *argOutInstr, uint argNum)
  2768. {
  2769. IR::Instr * newInstr = nullptr;
  2770. bool isPreOpBailOutNeeded = false;
  2771. if (argNum == 0)
  2772. {
  2773. newInstr = argOutInstr->HoistSrc1(Js::OpCode::Coerse_Str);
  2774. isPreOpBailOutNeeded = true;
  2775. newInstr->GetDst()->SetValueType(ValueType::String);
  2776. newInstr->SetSrc2(IR::AddrOpnd::New(L"String.prototype.replace", IR::AddrOpndKindSz, newInstr->m_func));
  2777. argOutInstr->GetSrc1()->SetValueType(ValueType::String);
  2778. }
  2779. if (argNum == 1)
  2780. {
  2781. newInstr = argOutInstr->HoistSrc1(Js::OpCode::Coerse_StrOrRegex);
  2782. isPreOpBailOutNeeded = true;
  2783. }
  2784. if (isPreOpBailOutNeeded)
  2785. {
  2786. newInstr->SetByteCodeOffset(argOutInstr);
  2787. newInstr->forcePreOpBailOutIfNeeded = true;
  2788. }
  2789. return false;
  2790. }, 3);
  2791. break;
  2792. case Js::BuiltinFunction::RegExp_Exec:
  2793. callInstr->ForEachCallDirectArgOutInstrBackward([&](IR::Instr *argOutInstr, uint argNum)
  2794. {
  2795. IR::Instr * newInstr = nullptr;
  2796. bool isPreOpBailOutNeeded = false;
  2797. if (argNum == 0)
  2798. {
  2799. newInstr = argOutInstr->HoistSrc1(Js::OpCode::Coerse_Regex);
  2800. isPreOpBailOutNeeded = true;
  2801. }
  2802. else if (argNum == 1)
  2803. {
  2804. newInstr = argOutInstr->HoistSrc1(Js::OpCode::Conv_Str);
  2805. newInstr->GetDst()->SetValueType(ValueType::String);
  2806. argOutInstr->GetSrc1()->SetValueType(ValueType::String);
  2807. isPreOpBailOutNeeded = true;
  2808. }
  2809. if (isPreOpBailOutNeeded)
  2810. {
  2811. newInstr->SetByteCodeOffset(argOutInstr);
  2812. newInstr->forcePreOpBailOutIfNeeded = true;
  2813. }
  2814. return false;
  2815. }, 2);
  2816. break;
  2817. }
  2818. }
  2819. IR::Instr *
  2820. Inline::SimulateCallForGetterSetter(IR::Instr *accessorInstr, IR::Instr* insertInstr, IR::PropertySymOpnd* methodOpnd, bool isGetter)
  2821. {
  2822. Assert(methodOpnd->UsesAccessor());
  2823. IntConstType argOutCount = isGetter ? 1 : 2; // A setter would have an additional ArgOut in the form of the value being set.
  2824. IR::Instr *ldMethodFld = IR::Instr::New(Js::OpCode::LdMethodFromFlags, IR::RegOpnd::New(TyVar, accessorInstr->m_func), methodOpnd, accessorInstr->m_func);
  2825. insertInstr->InsertBefore(ldMethodFld);
  2826. ldMethodFld = ldMethodFld->ConvertToBailOutInstr(accessorInstr, IR::BailOutFailedInlineTypeCheck);
  2827. ldMethodFld->SetByteCodeOffset(accessorInstr);
  2828. IR::Instr *startCall = IR::Instr::New(Js::OpCode::StartCall, accessorInstr->m_func);
  2829. startCall->SetDst(IR::RegOpnd::New(TyVar, accessorInstr->m_func));
  2830. startCall->SetSrc1(IR::IntConstOpnd::New(argOutCount, TyInt32, accessorInstr->m_func));
  2831. insertInstr->InsertBefore(startCall);
  2832. startCall->SetByteCodeOffset(accessorInstr);
  2833. PropertySym * fieldSym = methodOpnd->AsSymOpnd()->m_sym->AsPropertySym();
  2834. IR::RegOpnd * instanceOpnd = IR::RegOpnd::New(fieldSym->m_stackSym, TyVar, accessorInstr->m_func);
  2835. IR::Instr *argOutThis = IR::Instr::New(Js::OpCode::ArgOut_A, accessorInstr->m_func);
  2836. StackSym *symDst = accessorInstr->m_func->m_symTable->GetArgSlotSym((uint16)(1));
  2837. argOutThis->SetDst(IR::SymOpnd::New(symDst, 0, TyVar, accessorInstr->m_func));
  2838. argOutThis->SetSrc1(instanceOpnd);
  2839. argOutThis->SetSrc2(startCall->GetDst());
  2840. insertInstr->InsertBefore(argOutThis);
  2841. IR::Instr * argOut = nullptr;
  2842. if(!isGetter)
  2843. {
  2844. // Set the src1 of the StFld to be the second ArgOut.
  2845. argOut = IR::Instr::New(Js::OpCode::ArgOut_A, accessorInstr->m_func);
  2846. symDst = accessorInstr->m_func->m_symTable->GetArgSlotSym((uint16)(2));
  2847. argOut->SetDst(IR::SymOpnd::New(symDst, 0, TyVar, accessorInstr->m_func));
  2848. argOut->SetSrc1(accessorInstr->GetSrc1());
  2849. argOut->SetSrc2(argOutThis->GetDst());
  2850. insertInstr->InsertBefore(argOut);
  2851. }
  2852. accessorInstr->ReplaceSrc1(ldMethodFld->GetDst());
  2853. isGetter ? accessorInstr->SetSrc2(argOutThis->GetDst()) : accessorInstr->SetSrc2(argOut->GetDst());
  2854. if(!isGetter)
  2855. {
  2856. accessorInstr->UnlinkDst();
  2857. }
  2858. return startCall;
  2859. }
  2860. IR::Instr *
  2861. Inline::InlineGetterSetterFunction(IR::Instr *accessorInstr, const Js::FunctionCodeGenJitTimeData *const inlineeData, const StackSym *symCallerThis, const uint inlineCacheIndex, bool isGetter, uint recursiveInlineDepth)
  2862. {
  2863. // This function is recursive, so when jitting in the foreground, probe the stack
  2864. if (!this->topFunc->IsBackgroundJIT())
  2865. {
  2866. PROBE_STACK(this->topFunc->GetScriptContext(), Js::Constants::MinStackDefault);
  2867. }
  2868. IR::Instr *instrNext = accessorInstr->m_next;
  2869. Js::FunctionBody *funcCaller = accessorInstr->m_func->GetJnFunction();
  2870. Js::FunctionBody *funcBody = inlineeData->GetFunctionBody();
  2871. Assert(!accessorInstr->GetSrc2());
  2872. JS_ETW(EventWriteJSCRIPT_BACKEND_INLINE(
  2873. funcCaller->GetFunctionNumber(), funcBody->GetFunctionNumber(),
  2874. funcCaller->GetExternalDisplayName(), funcBody->GetExternalDisplayName()));
  2875. IR::Instr *inlineBailoutChecksBeforeInstr = accessorInstr;
  2876. Js::ProxyEntryPointInfo *defaultEntryPointInfo = funcBody->GetDefaultEntryPointInfo();
  2877. Assert(defaultEntryPointInfo->IsFunctionEntryPointInfo());
  2878. Js::FunctionEntryPointInfo *functionEntryPointInfo = static_cast<Js::FunctionEntryPointInfo*>(defaultEntryPointInfo);
  2879. JsFunctionCodeGen *workItem = JitAnew(this->topFunc->m_alloc, JsFunctionCodeGen,
  2880. funcBody->GetScriptContext()->GetNativeCodeGenerator(), funcBody, functionEntryPointInfo, this->topFunc->IsJitInDebugMode());
  2881. workItem->SetRecyclableData(JitAnew(this->topFunc->m_alloc, Js::CodeGenRecyclableData, inlineeData));
  2882. workItem->SetJitMode(this->topFunc->m_workItem->GetJitMode());
  2883. IR::RegOpnd * returnValueOpnd;
  2884. Js::RegSlot returnRegSlot;
  2885. if (isGetter && accessorInstr->GetDst())
  2886. {
  2887. returnValueOpnd = accessorInstr->UnlinkDst()->AsRegOpnd();
  2888. returnRegSlot = returnValueOpnd->m_sym->GetByteCodeRegSlot();
  2889. }
  2890. else
  2891. {
  2892. returnValueOpnd = nullptr;
  2893. returnRegSlot = Js::Constants::NoRegister;
  2894. }
  2895. const auto profileInfo =
  2896. JitAnew(
  2897. this->topFunc->m_alloc,
  2898. Js::ReadOnlyDynamicProfileInfo,
  2899. funcBody->HasDynamicProfileInfo() ? funcBody->GetAnyDynamicProfileInfo() : nullptr,
  2900. this->topFunc->IsBackgroundJIT() ? this->topFunc->m_alloc : nullptr);
  2901. Js::EntryPointPolymorphicInlineCacheInfo * entryPointPolymorphicInlineCacheInfo = this->topFunc->m_workItem->GetEntryPoint()->GetPolymorphicInlineCacheInfo();
  2902. Func *inlinee = JitAnew(this->topFunc->m_alloc,
  2903. Func,
  2904. this->topFunc->m_alloc,
  2905. workItem,
  2906. accessorInstr->m_func->m_runtimeData ?
  2907. accessorInstr->m_func->m_runtimeData->GetLdFldInlinee(inlineCacheIndex) :
  2908. this->topFunc->GetJnFunction()->GetLdFldInlineeCodeGenRuntimeData(inlineCacheIndex),
  2909. entryPointPolymorphicInlineCacheInfo ? entryPointPolymorphicInlineCacheInfo->GetInlineeInfo(funcBody) : nullptr,
  2910. this->topFunc->GetCodeGenAllocators(),
  2911. this->topFunc->GetNumberAllocator(),
  2912. profileInfo,
  2913. this->topFunc->GetCodeGenProfiler(),
  2914. this->topFunc->IsBackgroundJIT(),
  2915. accessorInstr->m_func,
  2916. accessorInstr->m_next->GetByteCodeOffset(),
  2917. returnRegSlot,
  2918. false,
  2919. UINT16_MAX,
  2920. true);
  2921. // funcBody->GetInParamsCount() can be greater than one even if it is all undefined. Example defineProperty(a,"foo", {get:function(a,b,c){}});
  2922. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  2923. if (Js::Configuration::Global.flags.TestTrace.IsEnabled(Js::InlinePhase) ||
  2924. Js::Configuration::Global.flags.TestTrace.IsEnabled(Js::InlineAccessorsPhase) || Js::Configuration::Global.flags.Trace.IsEnabled(Js::InlineAccessorsPhase))
  2925. {
  2926. wchar_t debugStringBuffer [MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  2927. wchar_t debugStringBuffer2[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  2928. PropertySym *propertySym = isGetter ? accessorInstr->GetSrc1()->AsSymOpnd()->m_sym->AsPropertySym() : accessorInstr->GetDst()->AsSymOpnd()->m_sym->AsPropertySym();
  2929. Js::ScriptContext* scriptContext = propertySym->GetFunc()->GetScriptContext();
  2930. Output::Print(L"INLINING: %s: \tInlinee: %s (%s)\tCaller: %s (%s)\t fieldName: %s\n", isGetter ? L"Getter" : L"Setter",
  2931. funcBody->GetDisplayName(), funcBody->GetDebugNumberSet(debugStringBuffer), funcCaller->GetDisplayName(), funcCaller->GetDebugNumberSet(debugStringBuffer2),
  2932. scriptContext->GetPropertyNameLocked(propertySym->m_propertyId)->GetBuffer());
  2933. Output::Flush();
  2934. }
  2935. #endif
  2936. IR::Opnd * methodOpnd = isGetter ? accessorInstr->GetSrc1() : accessorInstr->GetDst();
  2937. Assert(methodOpnd->IsSymOpnd() && methodOpnd->AsSymOpnd()->IsPropertySymOpnd());
  2938. inlineBailoutChecksBeforeInstr = SimulateCallForGetterSetter(accessorInstr, accessorInstr, methodOpnd->AsPropertySymOpnd(), isGetter);
  2939. bool safeThis = false;
  2940. TryOptimizeCallInstrWithFixedMethod(accessorInstr, inlineeData->GetFunctionInfo(), false, false, false, true, safeThis);
  2941. return InlineFunctionCommon(accessorInstr, nullptr, funcBody, inlinee, instrNext, returnValueOpnd, inlineBailoutChecksBeforeInstr, symCallerThis, recursiveInlineDepth, safeThis);
  2942. }
  2943. IR::Instr *
  2944. Inline::InlineFunctionCommon(IR::Instr *callInstr, StackSym* originalCallTargetStackSym, Js::FunctionBody *funcBody, Func *inlinee, IR::Instr *instrNext,
  2945. IR::RegOpnd * returnValueOpnd, IR::Instr *inlineBailoutChecksBeforeInstr, const StackSym *symCallerThis, uint recursiveInlineDepth, bool safeThis, bool isApplyTarget)
  2946. {
  2947. BuildIRForInlinee(inlinee, funcBody, callInstr, isApplyTarget, recursiveInlineDepth);
  2948. Js::ArgSlot formalCount = funcBody->GetInParamsCount();
  2949. IR::Instr *argOuts[Js::InlineeCallInfo::MaxInlineeArgoutCount];
  2950. #if DBG
  2951. memset(argOuts, 0xFE, sizeof(argOuts));
  2952. #endif
  2953. if (callInstr->m_opcode == Js::OpCode::CallIFixed)
  2954. {
  2955. Assert(callInstr->GetFixedFunction()->GetFunctionInfo() == funcBody);
  2956. }
  2957. else
  2958. {
  2959. PrepareInsertionPoint(callInstr, funcBody, inlineBailoutChecksBeforeInstr);
  2960. }
  2961. Assert(formalCount <= Js::InlineeCallInfo::MaxInlineeArgoutCount);
  2962. __analysis_assume(formalCount <= Js::InlineeCallInfo::MaxInlineeArgoutCount);
  2963. IR::Instr *argOutsExtra[Js::InlineeCallInfo::MaxInlineeArgoutCount];
  2964. #if DBG
  2965. memset(argOutsExtra, 0xFE, sizeof(argOutsExtra));
  2966. #endif
  2967. bool stackArgsArgOutExpanded = false;
  2968. Js::ArgSlot actualCount = MapActuals(callInstr, argOuts, formalCount, inlinee, (Js::ProfileId)callInstr->AsProfiledInstr()->u.profileId, &stackArgsArgOutExpanded, argOutsExtra);
  2969. inlinee->actualCount = actualCount;
  2970. Assert(actualCount > 0);
  2971. #if DBG
  2972. if(safeThis)
  2973. {
  2974. Assert(callInstr->m_opcode == Js::OpCode::CallIFixed);
  2975. }
  2976. #endif
  2977. MapFormals(inlinee, argOuts, formalCount, actualCount, returnValueOpnd, callInstr->GetSrc1(), symCallerThis, stackArgsArgOutExpanded, safeThis, argOutsExtra);
  2978. if (callInstr->m_opcode == Js::OpCode::CallIFixed && !inlinee->isGetterSetter)
  2979. {
  2980. Assert(originalCallTargetStackSym != nullptr);
  2981. // Insert a ByteCodeUsesInstr to make sure the function object's lifetimes is extended beyond the last bailout point
  2982. // at which we may have to call the function again in the interpreter.
  2983. // Don't need to do this for a getter/setter inlinee as, upon bailout, the execution will start in the interpreter at the LdFld/StFld itself.
  2984. callInstr->InsertBefore(IR::ByteCodeUsesInstr::New(callInstr, originalCallTargetStackSym->m_id));
  2985. }
  2986. // InlineeStart indicate the beginning of the inlinee, and we need the stack arg for the inlinee until InlineeEnd
  2987. callInstr->m_opcode = Js::OpCode::InlineeStart;
  2988. // Set it to belong to the inlinee, so that we can use the actual count when lowering InlineeStart
  2989. callInstr->m_func = inlinee;
  2990. callInstr->SetDst(IR::RegOpnd::New(TyVar, inlinee));
  2991. // Put the meta arguments that the stack walker expects to find on the stack.
  2992. SetupInlineeFrame(inlinee, callInstr, actualCount, callInstr->GetSrc1());
  2993. // actualCount + MetaArgCount to include the meta arguments to pop from the inlinee argout stack.
  2994. IR::Instr *inlineeEndInstr = IR::Instr::New(Js::OpCode::InlineeEnd, inlinee);
  2995. inlineeEndInstr->SetByteCodeOffset(inlinee->m_tailInstr->GetPrevRealInstr());
  2996. inlineeEndInstr->SetSrc1(IR::IntConstOpnd::New(actualCount + Js::Constants::InlineeMetaArgCount, TyInt32, callInstr->m_func));
  2997. inlineeEndInstr->SetSrc2(callInstr->GetDst()); // Link the inlinee end to the inlinee Start
  2998. callInstr->InsertAfter(inlineeEndInstr);
  2999. // Move the ArgOut_A_Inlines close to the InlineeStart
  3000. callInstr->MoveArgs();
  3001. inlineeEndInstr->InsertRangeBefore(inlinee->m_headInstr->m_next, inlinee->m_tailInstr->m_prev);
  3002. inlinee->m_headInstr->Free();
  3003. inlinee->m_tailInstr->Free();
  3004. this->topFunc->SetHasInlinee();
  3005. InsertStatementBoundary(instrNext);
  3006. return instrNext;
  3007. }
  3008. #ifdef ENABLE_DOM_FAST_PATH
  3009. // we have LdFld, src1 obj, src2: null; dest: return value
  3010. // We need to convert it to inlined method call.
  3011. // We cannot do CallDirect as it requires ArgOut and that cannot be hoisted/copyprop'd
  3012. // Create a new OpCode, DOMFastPathGetter. The OpCode takes three arguments:
  3013. // The function object, the "this" instance object, and the helper routine as we have one for each index
  3014. // A functionInfo->Index# table is created in scriptContext (and potentially movable to threadContext if WS is not a concern).
  3015. // we use the table to identify the helper that needs to be lowered.
  3016. // At lower time we create the call to helper, which is function entrypoint at this time.
  3017. IR::Instr * Inline::InlineDOMGetterSetterFunction(IR::Instr *ldFldInstr, const Js::FunctionCodeGenJitTimeData *const inlineeData, const Js::FunctionCodeGenJitTimeData *const inlinerData)
  3018. {
  3019. Js::FunctionInfo* functionInfo = inlineeData->GetFunctionInfo();
  3020. Assert(ldFldInstr->GetSrc1()->IsSymOpnd() && ldFldInstr->GetSrc1()->AsSymOpnd()->IsPropertySymOpnd());
  3021. Assert(ldFldInstr->GetSrc1()->AsPropertySymOpnd()->HasObjTypeSpecFldInfo());
  3022. Assert(ldFldInstr->GetSrc1()->AsPropertySymOpnd()->GetObjTypeSpecInfo()->UsesAccessor());
  3023. // Find the helper routine for this functionInfo.
  3024. Js::ScriptContext* scriptContext = this->topFunc->GetScriptContext();
  3025. IR::JnHelperMethod helperMethod;
  3026. bool found = scriptContext->EnsureDOMFastPathIRHelperMap()->TryGetValue(functionInfo, &helperMethod);
  3027. Assert(found);
  3028. // Find the instance object (External object).
  3029. PropertySym * fieldSym = ldFldInstr->GetSrc1()->AsSymOpnd()->m_sym->AsPropertySym();
  3030. IR::RegOpnd * instanceOpnd = IR::RegOpnd::New(fieldSym->m_stackSym, TyMachPtr, ldFldInstr->m_func);
  3031. // Find the function object from getter inline cache. Need bailout to verify.
  3032. IR::Instr *ldMethodFld = IR::Instr::New(Js::OpCode::LdMethodFromFlags, IR::RegOpnd::New(TyVar, ldFldInstr->m_func), ldFldInstr->GetSrc1(), ldFldInstr->m_func);
  3033. ldFldInstr->InsertBefore(ldMethodFld);
  3034. ldMethodFld = ldMethodFld->ConvertToBailOutInstr(ldFldInstr, IR::BailOutFailedInlineTypeCheck);
  3035. ldFldInstr->ReplaceSrc1(ldMethodFld->GetDst());
  3036. ldMethodFld->SetByteCodeOffset(ldFldInstr);
  3037. // generate further object/type bailout
  3038. PrepareInsertionPoint(ldFldInstr, functionInfo, ldFldInstr);
  3039. // We have three arguments to pass to the OpCode. Create a new ExtendArg_A opcode to chain up the argument. It is similar to ArgOut chain
  3040. // except that it is not argout.
  3041. // The Opcode sequence is like:
  3042. // (dst)helpArg1: ExtendArg_A (src1)thisObject (src2)null
  3043. // (dst)helpArg2: ExtendArg_A (src1)funcObject (src2)helpArg1
  3044. // method: DOMFastPathGetter (src1)HelperCall (src2)helpArg2
  3045. IR::Instr* extendArg0 = IR::Instr::New(Js::OpCode::ExtendArg_A, IR::RegOpnd::New(TyVar, ldFldInstr->m_func), instanceOpnd, ldFldInstr->m_func);
  3046. ldFldInstr->InsertBefore(extendArg0);
  3047. IR::Instr* extendArg1 = IR::Instr::New(Js::OpCode::ExtendArg_A, IR::RegOpnd::New(TyVar, ldFldInstr->m_func), ldMethodFld->GetDst(), extendArg0->GetDst(), ldFldInstr->m_func);
  3048. ldFldInstr->InsertBefore(extendArg1);
  3049. ldFldInstr->ReplaceSrc1(IR::HelperCallOpnd::New(helperMethod, ldFldInstr->m_func));
  3050. ldFldInstr->SetSrc2(extendArg1->GetDst());
  3051. ldFldInstr->m_opcode = Js::OpCode::DOMFastPathGetter;
  3052. StackSym * tmpSym = StackSym::New(ldFldInstr->GetDst()->GetType(), ldFldInstr->m_func);
  3053. IR::Opnd * tmpDst = IR::RegOpnd::New(tmpSym, tmpSym->GetType(), ldFldInstr->m_func);
  3054. IR::Opnd * callInstrDst = ldFldInstr->UnlinkDst();
  3055. ldFldInstr->SetDst(tmpDst);
  3056. IR::Instr * ldInstr = IR::Instr::New(Js::OpCode::Ld_A, callInstrDst, tmpDst, ldFldInstr->m_func);
  3057. ldFldInstr->InsertAfter(ldInstr);
  3058. this->topFunc->SetHasInlinee();
  3059. InsertStatementBoundary(ldInstr->m_next);
  3060. return ldInstr->m_next;
  3061. }
  3062. #endif
  3063. void
  3064. Inline::InsertStatementBoundary(IR::Instr * instrNext)
  3065. {
  3066. if (lastStatementBoundary)
  3067. {
  3068. Assert(lastStatementBoundary->m_func == instrNext->m_func);
  3069. IR::PragmaInstr * pragmaInstr = IR::PragmaInstr::New(Js::OpCode::StatementBoundary,
  3070. lastStatementBoundary->m_statementIndex,
  3071. lastStatementBoundary->m_func);
  3072. pragmaInstr->SetByteCodeOffset(instrNext);
  3073. instrNext->InsertBefore(pragmaInstr);
  3074. }
  3075. }
  3076. IR::Instr *
  3077. Inline::InlineScriptFunction(IR::Instr *callInstr, const Js::FunctionCodeGenJitTimeData *const inlineeData, const StackSym *symCallerThis, const Js::ProfileId profileId, bool* pIsInlined, uint recursiveInlineDepth)
  3078. {
  3079. *pIsInlined = false;
  3080. // This function is recursive, so when jitting in the foreground, probe the stack
  3081. if (!this->topFunc->IsBackgroundJIT())
  3082. {
  3083. PROBE_STACK(this->topFunc->GetScriptContext(), Js::Constants::MinStackDefault);
  3084. }
  3085. IR::Instr *instrNext = callInstr->m_next;
  3086. Js::FunctionBody *funcCaller = callInstr->m_func->GetJnFunction();
  3087. Js::FunctionBody *funcBody = inlineeData->GetFunctionBody();
  3088. if (callInstr->GetSrc2() &&
  3089. callInstr->GetSrc2()->IsSymOpnd() &&
  3090. callInstr->GetSrc2()->AsSymOpnd()->m_sym->AsStackSym()->GetArgSlotNum() > Js::InlineeCallInfo::MaxInlineeArgoutCount)
  3091. {
  3092. #if ENABLE_DEBUG_CONFIG_OPTIONS
  3093. wchar_t debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  3094. wchar_t debugStringBuffer2[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  3095. #endif
  3096. // This is a hard limit as we only use 4 bits to encode the actual count in the InlineeCallInfo. Although
  3097. // InliningDecider already checks for this, the check is against profile data that may not be accurate since profile
  3098. // data matching does not take into account some types of changes to source code. Need to check this again with current
  3099. // information.
  3100. INLINE_TESTTRACE(L"INLINING: Skip Inline: ArgSlot > MaxInlineeArgoutCount\tInlinee: %s (%s)\tArgSlotNum: %d\tMaxInlineeArgoutCount: %d\tCaller: %s (%s)\n",
  3101. funcBody->GetDisplayName(), funcBody->GetDebugNumberSet(debugStringBuffer), callInstr->GetSrc2()->AsSymOpnd()->m_sym->AsStackSym()->GetArgSlotNum(),
  3102. Js::InlineeCallInfo::MaxInlineeArgoutCount, funcCaller->GetDisplayName(), funcCaller->GetDebugNumberSet(debugStringBuffer2));
  3103. return instrNext;
  3104. }
  3105. *pIsInlined = true;
  3106. // Save off the call target operand (function object) so we can extend its lifetime as needed, even if
  3107. // the call instruction gets transformed to CallIFixed.
  3108. StackSym* originalCallTargetStackSym = callInstr->GetSrc1()->GetStackSym();
  3109. // We are committed to inlining, optimize the call instruction for fixed fields now and don't attempt it later.
  3110. bool isFixed = false;
  3111. JS_ETW(EventWriteJSCRIPT_BACKEND_INLINE(
  3112. funcCaller->GetFunctionNumber(), funcBody->GetFunctionNumber(),
  3113. funcCaller->GetExternalDisplayName(), funcBody->GetExternalDisplayName()));
  3114. bool isCtor = false;
  3115. bool safeThis = false;
  3116. IR::Instr *inlineBailoutChecksBeforeInstr;
  3117. if (callInstr->m_opcode == Js::OpCode::NewScObject || callInstr->m_opcode == Js::OpCode::NewScObjArray)
  3118. {
  3119. isCtor = true;
  3120. isFixed = TryOptimizeCallInstrWithFixedMethod(callInstr, inlineeData->GetFunctionInfo(),
  3121. false /*isPolymorphic*/, false /*isBuiltIn*/, isCtor /*isCtor*/, true /*isInlined*/, safeThis /*&safeThis*/);
  3122. bool split = SplitConstructorCall(callInstr, true, isFixed, &inlineBailoutChecksBeforeInstr);
  3123. Assert(split && inlineBailoutChecksBeforeInstr != nullptr);
  3124. }
  3125. else
  3126. {
  3127. isFixed = TryOptimizeCallInstrWithFixedMethod(callInstr, inlineeData->GetFunctionInfo(),
  3128. false /*isPolymorphic*/, false /*isBuiltIn*/, isCtor /*isCtor*/, true /*isInlined*/, safeThis /*&safeThis*/);
  3129. inlineBailoutChecksBeforeInstr = callInstr;
  3130. }
  3131. Assert(callInstr->IsProfiledInstr());
  3132. Js::ProfileId callSiteId = static_cast<Js::ProfileId>(callInstr->AsProfiledInstr()->u.profileId);
  3133. Js::ProxyEntryPointInfo *defaultEntryPointInfo = funcBody->GetDefaultEntryPointInfo();
  3134. Assert(defaultEntryPointInfo->IsFunctionEntryPointInfo());
  3135. Js::FunctionEntryPointInfo *functionEntryPointInfo = static_cast<Js::FunctionEntryPointInfo*>(defaultEntryPointInfo);
  3136. JsFunctionCodeGen *workItem = JitAnew(this->topFunc->m_alloc, JsFunctionCodeGen,
  3137. funcBody->GetScriptContext()->GetNativeCodeGenerator(), funcBody, functionEntryPointInfo, this->topFunc->IsJitInDebugMode());
  3138. workItem->SetRecyclableData(JitAnew(this->topFunc->m_alloc, Js::CodeGenRecyclableData, inlineeData));
  3139. workItem->SetJitMode(this->topFunc->m_workItem->GetJitMode());
  3140. IR::RegOpnd * returnValueOpnd;
  3141. Js::RegSlot returnRegSlot;
  3142. if (callInstr->GetDst())
  3143. {
  3144. returnValueOpnd = callInstr->UnlinkDst()->AsRegOpnd();
  3145. returnRegSlot = returnValueOpnd->m_sym->GetByteCodeRegSlot();
  3146. }
  3147. else
  3148. {
  3149. returnValueOpnd = nullptr;
  3150. returnRegSlot = Js::Constants::NoRegister;
  3151. }
  3152. const auto profileInfo =
  3153. JitAnew(
  3154. this->topFunc->m_alloc,
  3155. Js::ReadOnlyDynamicProfileInfo,
  3156. funcBody->HasDynamicProfileInfo() ? funcBody->GetAnyDynamicProfileInfo() : nullptr,
  3157. this->topFunc->IsBackgroundJIT() ? this->topFunc->m_alloc : nullptr);
  3158. Js::EntryPointPolymorphicInlineCacheInfo * entryPointPolymorphicInlineCacheInfo = this->topFunc->m_workItem->GetEntryPoint()->GetPolymorphicInlineCacheInfo();
  3159. Func *inlinee = JitAnew(this->topFunc->m_alloc,
  3160. Func,
  3161. this->topFunc->m_alloc,
  3162. workItem,
  3163. callInstr->m_func->m_runtimeData ?
  3164. callInstr->m_func->m_runtimeData->GetInlineeForTargetInlinee(profileId, funcBody) :
  3165. this->topFunc->GetJnFunction()->GetInlineeCodeGenRuntimeDataForTargetInlinee(profileId, funcBody),
  3166. entryPointPolymorphicInlineCacheInfo ? entryPointPolymorphicInlineCacheInfo->GetInlineeInfo(funcBody) : nullptr,
  3167. this->topFunc->GetCodeGenAllocators(),
  3168. this->topFunc->GetNumberAllocator(),
  3169. profileInfo,
  3170. this->topFunc->GetCodeGenProfiler(),
  3171. this->topFunc->IsBackgroundJIT(),
  3172. callInstr->m_func,
  3173. callInstr->m_next->GetByteCodeOffset(),
  3174. returnRegSlot,
  3175. isCtor,
  3176. callSiteId,
  3177. false);
  3178. return InlineFunctionCommon(callInstr, originalCallTargetStackSym, funcBody, inlinee, instrNext, returnValueOpnd, inlineBailoutChecksBeforeInstr, symCallerThis, recursiveInlineDepth, safeThis);
  3179. }
  3180. bool
  3181. Inline::SplitConstructorCall(IR::Instr *const newObjInstr, const bool isInlined, const bool isFixed, IR::Instr** createObjInstrOut, IR::Instr** callCtorInstrOut) const
  3182. {
  3183. Assert(newObjInstr);
  3184. Assert(newObjInstr->m_opcode == Js::OpCode::NewScObject);
  3185. Assert(newObjInstr->GetSrc1());
  3186. Assert(newObjInstr->GetSrc2());
  3187. this->topFunc->SetHasTempObjectProducingInstr(true);
  3188. return
  3189. SplitConstructorCallCommon(
  3190. newObjInstr,
  3191. newObjInstr->GetSrc2(),
  3192. Js::OpCode::NewScObjectNoCtor,
  3193. isInlined,
  3194. isFixed,
  3195. createObjInstrOut,
  3196. callCtorInstrOut);
  3197. }
  3198. bool
  3199. Inline::SplitConstructorCallCommon(
  3200. IR::Instr *const newObjInstr,
  3201. IR::Opnd *const lastArgOpnd,
  3202. const Js::OpCode newObjOpCode,
  3203. const bool isInlined,
  3204. const bool isFixed,
  3205. IR::Instr** createObjInstrOut,
  3206. IR::Instr** callCtorInstrOut) const
  3207. {
  3208. Assert(newObjInstr);
  3209. Assert(newObjInstr->GetSrc1());
  3210. Assert(lastArgOpnd);
  3211. Assert(isInlined || isFixed);
  3212. const auto callerFunc = newObjInstr->m_func;
  3213. // Call the NoCtor version of NewScObject
  3214. // Use a temporary register for the newly allocated object (before the call to ctor) - even if we know we'll return this
  3215. // object from the whole operation. That's so that we don't trash the bytecode register if we need to bail out at
  3216. // object allocation (bytecode instruction has the form [Profiled]NewScObject R6 = R6).
  3217. IR::RegOpnd* createObjDst = nullptr;
  3218. IR::Instr* createObjInstr = nullptr;
  3219. const Js::JitTimeConstructorCache* constructorCache;
  3220. bool returnCreatedObject = false;
  3221. bool skipNewScObj = false;
  3222. if (newObjInstr->IsProfiledInstr())
  3223. {
  3224. Js::ProfileId profiledCallSiteId = static_cast<Js::ProfileId>(newObjInstr->AsProfiledInstr()->u.profileId);
  3225. constructorCache = newObjInstr->m_func->GetConstructorCache(profiledCallSiteId);
  3226. returnCreatedObject = constructorCache != nullptr && constructorCache->ctorHasNoExplicitReturnValue;
  3227. skipNewScObj = constructorCache != nullptr && constructorCache->skipNewScObject;
  3228. if (!skipNewScObj)
  3229. {
  3230. createObjDst = IR::RegOpnd::New(TyVar, callerFunc);
  3231. createObjInstr = IR::ProfiledInstr::New(newObjOpCode, createObjDst, newObjInstr->GetSrc1(), callerFunc);
  3232. createObjInstr->AsProfiledInstr()->u.profileId = profiledCallSiteId;
  3233. }
  3234. }
  3235. else
  3236. {
  3237. constructorCache = nullptr;
  3238. createObjDst = IR::RegOpnd::New(TyVar, callerFunc);
  3239. createObjInstr = IR::Instr::New(newObjOpCode, createObjDst, newObjInstr->GetSrc1(), callerFunc);
  3240. }
  3241. Assert(!isInlined || !skipNewScObj);
  3242. Assert(isFixed || !skipNewScObj);
  3243. // For new Object() and new Array() we have special fast helpers. We'll let the lowerer convert this instruction directly
  3244. // into a call to one of these helpers.
  3245. if (skipNewScObj)
  3246. {
  3247. Js::JavascriptFunction* ctor = newObjInstr->GetFixedFunction();
  3248. Js::FunctionInfo* ctorInfo = ctor->GetFunctionInfo();
  3249. if ((ctorInfo == &Js::JavascriptObject::EntryInfo::NewInstance || ctorInfo == &Js::JavascriptArray::EntryInfo::NewInstance) &&
  3250. newObjInstr->HasEmptyArgOutChain())
  3251. {
  3252. return false;
  3253. }
  3254. }
  3255. IR::Opnd* thisPtrOpnd;
  3256. if (createObjInstr != nullptr)
  3257. {
  3258. createObjInstr->SetByteCodeOffset(newObjInstr);
  3259. createObjInstr->GetSrc1()->SetIsJITOptimizedReg(true);
  3260. newObjInstr->InsertBefore(createObjInstr);
  3261. createObjDst->SetValueType(ValueType::GetObject(ObjectType::UninitializedObject));
  3262. thisPtrOpnd = createObjDst;
  3263. }
  3264. else
  3265. {
  3266. thisPtrOpnd = IR::AddrOpnd::NewNull(newObjInstr->m_func);
  3267. }
  3268. // Pass the new object to the constructor function with an ArgOut
  3269. const auto thisArgOpnd = IR::SymOpnd::New(callerFunc->m_symTable->GetArgSlotSym(1), TyVar, callerFunc);
  3270. auto instr = IR::Instr::New(Js::OpCode::ArgOut_A, thisArgOpnd, thisPtrOpnd, lastArgOpnd, callerFunc);
  3271. instr->SetByteCodeOffset(newObjInstr);
  3272. instr->GetDst()->SetIsJITOptimizedReg(true);
  3273. instr->GetSrc2()->SetIsJITOptimizedReg(true);
  3274. newObjInstr->InsertBefore(instr);
  3275. // Call the constructor using CallI with isCtorCall set. If we inline the constructor, and the inlined constructor
  3276. // bails out, the interpreter would be entered with CallFlags_Value as well. If the interpreter starts using the
  3277. // call flags, the proper call flags will need to be specified here by using a different op code specific to constructors.
  3278. if (isFixed)
  3279. {
  3280. newObjInstr->m_opcode = Js::OpCode::CallIFixed;
  3281. }
  3282. else
  3283. {
  3284. newObjInstr->m_opcode = Js::OpCode::CallI;
  3285. }
  3286. newObjInstr->isCtorCall = true;
  3287. if(newObjInstr->GetSrc2())
  3288. {
  3289. newObjInstr->FreeSrc2();
  3290. }
  3291. newObjInstr->SetSrc2(thisArgOpnd);
  3292. const auto insertBeforeInstr = newObjInstr->m_next;
  3293. Assert(insertBeforeInstr);
  3294. const auto nextByteCodeOffsetInstr = newObjInstr->GetNextRealInstrOrLabel();
  3295. // Determine which object to use as the final result of NewScObject, the object passed into the constructor as 'this', or
  3296. // the object returned by the constructor. We only need this if we don't have a hard-coded constructor cache, or if the
  3297. // constructor returns something explicitly. Otherwise, we simply return the object we allocated and passed to the constructor.
  3298. if (returnCreatedObject)
  3299. {
  3300. instr = IR::Instr::New(Js::OpCode::Ld_A, newObjInstr->GetDst(), createObjDst, callerFunc);
  3301. instr->SetByteCodeOffset(nextByteCodeOffsetInstr);
  3302. instr->GetDst()->SetIsJITOptimizedReg(true);
  3303. instr->GetSrc1()->SetIsJITOptimizedReg(true);
  3304. insertBeforeInstr->InsertBefore(instr);
  3305. }
  3306. else if (!skipNewScObj)
  3307. {
  3308. Assert(createObjDst != newObjInstr->GetDst());
  3309. // Since we're not returning the default new object, the constructor must be returning something explicitly. We don't
  3310. // know at this point whether it's an object or not. If the constructor is later inlined, the value type will be determined
  3311. // from the flow in glob opt. Otherwise, we'll need to emit an object check.
  3312. newObjInstr->GetDst()->SetValueType(ValueType::Uninitialized);
  3313. instr = IR::Instr::New(Js::OpCode::GetNewScObject, newObjInstr->GetDst(), newObjInstr->GetDst(), createObjDst, callerFunc);
  3314. instr->SetByteCodeOffset(nextByteCodeOffsetInstr);
  3315. instr->GetDst()->SetIsJITOptimizedReg(true);
  3316. instr->GetSrc1()->SetIsJITOptimizedReg(true);
  3317. insertBeforeInstr->InsertBefore(instr);
  3318. }
  3319. // Update the NewScObject cache, but only if we don't have a hard-coded constructor cache. We only clone caches that
  3320. // don't require update, and once updated a cache never requires an update again.
  3321. if (constructorCache == nullptr)
  3322. {
  3323. instr = IR::Instr::New(Js::OpCode::UpdateNewScObjectCache, callerFunc);
  3324. instr->SetSrc1(newObjInstr->GetSrc1()); // constructor function
  3325. instr->SetSrc2(newObjInstr->GetDst()); // the new object
  3326. instr->SetByteCodeOffset(nextByteCodeOffsetInstr);
  3327. instr->GetSrc1()->SetIsJITOptimizedReg(true);
  3328. instr->GetSrc2()->SetIsJITOptimizedReg(true);
  3329. insertBeforeInstr->InsertBefore(instr);
  3330. }
  3331. if (createObjInstrOut != nullptr)
  3332. {
  3333. *createObjInstrOut = createObjInstr;
  3334. }
  3335. if (callCtorInstrOut != nullptr)
  3336. {
  3337. *callCtorInstrOut = newObjInstr;
  3338. }
  3339. return true;
  3340. }
  3341. void
  3342. Inline::InsertObjectCheck(IR::Instr *callInstr, IR::Instr* insertBeforeInstr, IR::Instr*bailOutIfNotObject)
  3343. {
  3344. // Bailout if 'functionRegOpnd' is not an object.
  3345. bailOutIfNotObject->SetSrc1(callInstr->GetSrc1()->AsRegOpnd());
  3346. bailOutIfNotObject->SetByteCodeOffset(insertBeforeInstr);
  3347. insertBeforeInstr->InsertBefore(bailOutIfNotObject);
  3348. }
  3349. void
  3350. Inline::InsertFunctionTypeIdCheck(IR::Instr *callInstr, IR::Instr* insertBeforeInstr, IR::Instr* bailOutIfNotJsFunction)
  3351. {
  3352. // functionTypeRegOpnd = Ld functionRegOpnd->type
  3353. IR::IndirOpnd *functionTypeIndirOpnd = IR::IndirOpnd::New(callInstr->GetSrc1()->AsRegOpnd(), Js::RecyclableObject::GetOffsetOfType(), TyMachPtr, callInstr->m_func);
  3354. IR::RegOpnd *functionTypeRegOpnd = IR::RegOpnd::New(TyVar, this->topFunc);
  3355. IR::Instr *instr = IR::Instr::New(Js::OpCode::Ld_A, functionTypeRegOpnd, functionTypeIndirOpnd, callInstr->m_func);
  3356. if(instr->m_func->HasByteCodeOffset())
  3357. {
  3358. instr->SetByteCodeOffset(insertBeforeInstr);
  3359. }
  3360. insertBeforeInstr->InsertBefore(instr);
  3361. CompileAssert(sizeof(Js::TypeId) == sizeof(int32));
  3362. // if (functionTypeRegOpnd->typeId != TypeIds_Function) goto $noInlineLabel
  3363. // BrNeq_I4 $noInlineLabel, functionTypeRegOpnd->typeId, TypeIds_Function
  3364. IR::IndirOpnd *functionTypeIdIndirOpnd = IR::IndirOpnd::New(functionTypeRegOpnd, Js::Type::GetOffsetOfTypeId(), TyInt32, callInstr->m_func);
  3365. IR::IntConstOpnd *typeIdFunctionConstOpnd = IR::IntConstOpnd::New(Js::TypeIds_Function, TyInt32, callInstr->m_func);
  3366. bailOutIfNotJsFunction->SetSrc1(functionTypeIdIndirOpnd);
  3367. bailOutIfNotJsFunction->SetSrc2(typeIdFunctionConstOpnd);
  3368. insertBeforeInstr->InsertBefore(bailOutIfNotJsFunction);
  3369. }
  3370. void
  3371. Inline::InsertJsFunctionCheck(IR::Instr *callInstr, IR::Instr *insertBeforeInstr, IR::BailOutKind bailOutKind)
  3372. {
  3373. // This function only inserts bailout for tagged int & TypeIds_Function.
  3374. // As of now this is only used for polymorphic inlining.
  3375. Assert(bailOutKind == IR::BailOutOnPolymorphicInlineFunction);
  3376. Assert(insertBeforeInstr);
  3377. Assert(insertBeforeInstr->m_func == callInstr->m_func);
  3378. // bailOutIfNotFunction is primary bailout instruction
  3379. IR::Instr* bailOutIfNotFunction = IR::BailOutInstr::New(Js::OpCode::BailOnNotEqual, bailOutKind, insertBeforeInstr, callInstr->m_func);
  3380. IR::Instr *bailOutIfNotObject = IR::BailOutInstr::New(Js::OpCode::BailOnNotObject, bailOutKind, bailOutIfNotFunction->GetBailOutInfo(),callInstr->m_func);
  3381. InsertObjectCheck(callInstr, insertBeforeInstr, bailOutIfNotObject);
  3382. InsertFunctionTypeIdCheck(callInstr, insertBeforeInstr, bailOutIfNotFunction);
  3383. }
  3384. void
  3385. Inline::InsertFunctionBodyCheck(IR::Instr *callInstr, IR::Instr *insertBeforeInstr, IR::Instr* bailoutInstr, Js::FunctionInfo *funcInfo)
  3386. {
  3387. // if (JavascriptFunction::FromVar(r1)->functionInfo != funcInfo) goto noInlineLabel
  3388. // BrNeq_I4 noInlineLabel, r1->functionInfo, funcInfo
  3389. IR::IndirOpnd* funcBody = IR::IndirOpnd::New(callInstr->GetSrc1()->AsRegOpnd(), Js::JavascriptFunction::GetOffsetOfFunctionInfo(), TyMachPtr, callInstr->m_func);
  3390. IR::AddrOpnd* inlinedFuncBody = IR::AddrOpnd::New(funcInfo, IR::AddrOpndKindDynamicFunctionBody, callInstr->m_func);
  3391. bailoutInstr->SetSrc1(funcBody);
  3392. bailoutInstr->SetSrc2(inlinedFuncBody);
  3393. insertBeforeInstr->InsertBefore(bailoutInstr);
  3394. }
  3395. void
  3396. Inline::InsertFunctionObjectCheck(IR::Instr *callInstr, IR::Instr *insertBeforeInstr, IR::Instr *bailOutInstr, Js::FunctionInfo *funcInfo)
  3397. {
  3398. Js::BuiltinFunction index = Js::JavascriptLibrary::GetBuiltInForFuncInfo(funcInfo, callInstr->m_func->GetScriptContext());
  3399. AssertMsg(index < Js::BuiltinFunction::Count, "Invalid built-in index on a call target marked as built-in");
  3400. bailOutInstr->SetSrc1(callInstr->GetSrc1()->AsRegOpnd());
  3401. bailOutInstr->SetSrc2(IR::IntConstOpnd::New(index, TyInt32, callInstr->m_func));
  3402. insertBeforeInstr->InsertBefore(bailOutInstr);
  3403. }
  3404. IR::Instr *
  3405. Inline::PrepareInsertionPoint(IR::Instr *callInstr, Js::FunctionInfo *funcInfo, IR::Instr *insertBeforeInstr, IR::BailOutKind bailOutKind)
  3406. {
  3407. Assert(insertBeforeInstr);
  3408. Assert(insertBeforeInstr->m_func == callInstr->m_func);
  3409. Assert(bailOutKind == IR::BailOutOnInlineFunction);
  3410. // FunctionBody check is the primary bailout instruction, create it first
  3411. IR::BailOutInstr* primaryBailOutInstr = IR::BailOutInstr::New(Js::OpCode::BailOnNotEqual, bailOutKind, insertBeforeInstr, callInstr->m_func);
  3412. // 1. Bailout if function object is not an object.
  3413. IR::Instr *bailOutIfNotObject = IR::BailOutInstr::New(Js::OpCode::BailOnNotObject,
  3414. bailOutKind,
  3415. primaryBailOutInstr->GetBailOutInfo(),
  3416. callInstr->m_func);
  3417. InsertObjectCheck(callInstr, insertBeforeInstr, bailOutIfNotObject);
  3418. // 2. Bailout if function object is not a TypeId_Function
  3419. IR::Instr* bailOutIfNotJsFunction = IR::BailOutInstr::New(Js::OpCode::BailOnNotEqual, bailOutKind, primaryBailOutInstr->GetBailOutInfo(), callInstr->m_func);
  3420. InsertFunctionTypeIdCheck(callInstr, insertBeforeInstr, bailOutIfNotJsFunction);
  3421. // 3. Bailout if function body doesn't match funcInfo
  3422. InsertFunctionBodyCheck(callInstr, insertBeforeInstr, primaryBailOutInstr, funcInfo);
  3423. return primaryBailOutInstr;
  3424. }
  3425. uint Inline::CountActuals(IR::Instr *callInstr)
  3426. {
  3427. IR::Opnd *linkOpnd = callInstr->GetSrc2();
  3428. uint actualCount = 0;
  3429. if (linkOpnd->IsSymOpnd())
  3430. {
  3431. IR::Instr *argInstr;
  3432. do
  3433. {
  3434. Assert(linkOpnd->IsSymOpnd());
  3435. StackSym *sym = linkOpnd->AsSymOpnd()->m_sym->AsStackSym();
  3436. Assert(sym->m_isSingleDef);
  3437. Assert(sym->IsArgSlotSym());
  3438. argInstr = sym->m_instrDef;
  3439. ++actualCount;
  3440. linkOpnd = argInstr->GetSrc2();
  3441. }
  3442. while (linkOpnd->IsSymOpnd());
  3443. }
  3444. return actualCount;
  3445. }
  3446. bool Inline::InlConstFoldArg(IR::Instr *instr, __in_ecount_opt(callerArgOutCount) IR::Instr *callerArgOuts[], Js::ArgSlot callerArgOutCount)
  3447. {
  3448. Assert(instr->m_opcode == Js::OpCode::ArgOut_A);
  3449. if (PHASE_OFF(Js::InlinerConstFoldPhase, instr->m_func->GetTopFunc()))
  3450. {
  3451. return false;
  3452. }
  3453. IR::Opnd *src1 = instr->GetSrc1();
  3454. IntConstType value;
  3455. if (!src1->IsRegOpnd())
  3456. {
  3457. return false;
  3458. }
  3459. StackSym *sym = instr->GetSrc1()->AsRegOpnd()->m_sym;
  3460. if (!sym->IsSingleDef())
  3461. {
  3462. return false;
  3463. }
  3464. IR::Instr *instrDef = sym->GetInstrDef();
  3465. if (!this->InlConstFold(instrDef, &value, callerArgOuts, callerArgOutCount))
  3466. {
  3467. return false;
  3468. }
  3469. return true;
  3470. }
  3471. bool Inline::InlConstFold(IR::Instr *instr, IntConstType *pValue, __in_ecount_opt(callerArgOutCount) IR::Instr *callerArgOuts[], Js::ArgSlot callerArgOutCount)
  3472. {
  3473. IR::Opnd *src1 = instr->GetSrc1();
  3474. if (!src1)
  3475. {
  3476. return false;
  3477. }
  3478. switch (src1->GetKind())
  3479. {
  3480. case IR::OpndKindReg:
  3481. // Walk the tree below
  3482. break;
  3483. case IR::OpndKindIntConst:
  3484. if (instr->m_opcode == Js::OpCode::LdC_A_I4)
  3485. {
  3486. // Found a constant
  3487. *pValue = src1->AsIntConstOpnd()->GetValue();
  3488. return true;
  3489. }
  3490. return false;
  3491. case IR::OpndKindSym:
  3492. if (callerArgOuts && instr->m_opcode == Js::OpCode::ArgIn_A)
  3493. {
  3494. // We have an ArgIn. Walk the caller's ArgOut tree to see if a constant
  3495. // is passed in to the inlinee.
  3496. Assert(callerArgOuts && callerArgOutCount != (Js::ArgSlot) - 1);
  3497. Assert(src1->AsSymOpnd()->m_sym->AsStackSym()->IsParamSlotSym());
  3498. Js::ArgSlot paramSlot = src1->AsSymOpnd()->m_sym->AsStackSym()->GetParamSlotNum();
  3499. if (paramSlot <= callerArgOutCount)
  3500. {
  3501. IR::Instr *argOut = callerArgOuts[paramSlot - 1];
  3502. IR::Opnd *argOutSrc1 = argOut->GetSrc1();
  3503. if (!argOutSrc1->IsRegOpnd())
  3504. {
  3505. return false;
  3506. }
  3507. StackSym *sym = argOutSrc1->AsRegOpnd()->m_sym;
  3508. if (!sym->IsSingleDef())
  3509. {
  3510. return false;
  3511. }
  3512. IR::Instr *instrDef = sym->GetInstrDef();
  3513. // Walk the caller
  3514. return InlConstFold(instrDef, pValue, nullptr, (Js::ArgSlot) - 1);
  3515. }
  3516. }
  3517. else if (src1->AsSymOpnd()->IsPropertySymOpnd())
  3518. {
  3519. // See if we have a LdFld of a fixed field.
  3520. Js::Var var = TryOptimizeInstrWithFixedDataProperty(instr);
  3521. if (!Js::TaggedInt::Is(var))
  3522. {
  3523. return false;
  3524. }
  3525. else
  3526. {
  3527. *pValue = Js::TaggedInt::ToInt32(var);
  3528. return true;
  3529. }
  3530. }
  3531. return false;
  3532. default:
  3533. return false;
  3534. }
  3535. // All that is left is RegOpnds
  3536. Assert(src1->IsRegOpnd());
  3537. StackSym *sym = instr->GetSrc1()->AsRegOpnd()->m_sym;
  3538. if (!sym->IsSingleDef())
  3539. {
  3540. return false;
  3541. }
  3542. if (!src1 || !src1->IsRegOpnd() || !src1->AsRegOpnd()->m_sym->IsSingleDef())
  3543. {
  3544. return false;
  3545. }
  3546. IR::Opnd *src2 = instr->GetSrc2();
  3547. if (src2)
  3548. {
  3549. if (!src2->IsRegOpnd() || !src2->AsRegOpnd()->m_sym->IsSingleDef())
  3550. {
  3551. return false;
  3552. }
  3553. }
  3554. // See if src1 can be folded to a constant
  3555. if (!InlConstFold(src1->AsRegOpnd()->m_sym->GetInstrDef(), pValue, callerArgOuts, callerArgOutCount))
  3556. {
  3557. return false;
  3558. }
  3559. IntConstType src1Constant = *pValue;
  3560. // See if src2 (unless it is unary) can be folded to a constant
  3561. if (src2 && !InlConstFold(src2->AsRegOpnd()->m_sym->GetInstrDef(), pValue, callerArgOuts, callerArgOutCount))
  3562. {
  3563. return false;
  3564. }
  3565. // Now let's try to constant fold the current instruction
  3566. if (src2)
  3567. {
  3568. IntConstType src2Constant = *pValue;
  3569. if (!instr->BinaryCalculator(src1Constant, src2Constant, pValue)
  3570. || !Math::FitsInDWord(*pValue))
  3571. {
  3572. return false;
  3573. }
  3574. // Success
  3575. IR::ByteCodeUsesInstr * byteCodeInstr = IR::ByteCodeUsesInstr::New(instr->m_func);
  3576. byteCodeInstr->SetByteCodeOffset(instr);
  3577. StackSym *src1Sym = src1->AsRegOpnd()->m_sym;
  3578. StackSym *src2Sym = src2->AsRegOpnd()->m_sym;
  3579. if (src1Sym->HasByteCodeRegSlot() || src2Sym->HasByteCodeRegSlot())
  3580. {
  3581. if (src1Sym->HasByteCodeRegSlot())
  3582. {
  3583. byteCodeInstr->Set(src1Sym->m_id);
  3584. }
  3585. if (src2Sym->HasByteCodeRegSlot())
  3586. {
  3587. byteCodeInstr->Set(src2Sym->m_id);
  3588. }
  3589. instr->InsertBefore(byteCodeInstr);
  3590. }
  3591. #if DBG_DUMP
  3592. if (Js::Configuration::Global.flags.Trace.IsEnabled(Js::InlinerConstFoldPhase, this->topFunc->GetSourceContextId(), this->topFunc->GetLocalFunctionId()))
  3593. {
  3594. Output::Print(L"Constant folding to %d\n", *pValue);
  3595. instr->Dump();
  3596. }
  3597. #endif
  3598. instr->m_opcode = Js::OpCode::LdC_A_I4;
  3599. instr->ReplaceSrc1(IR::IntConstOpnd::New(*pValue, TyInt32, instr->m_func));
  3600. instr->GetDst()->AsRegOpnd()->m_sym->SetIsConst();
  3601. instr->FreeSrc2();
  3602. }
  3603. else
  3604. {
  3605. if (!instr->UnaryCalculator(src1Constant, pValue)
  3606. || !Math::FitsInDWord(*pValue))
  3607. {
  3608. // Skip over BytecodeArgOutCapture
  3609. if (instr->m_opcode == Js::OpCode::BytecodeArgOutCapture)
  3610. {
  3611. return true;
  3612. }
  3613. return false;
  3614. }
  3615. // Success
  3616. StackSym *src1Sym = src1->AsRegOpnd()->m_sym;
  3617. if (src1Sym->HasByteCodeRegSlot())
  3618. {
  3619. IR::ByteCodeUsesInstr * byteCodeInstr = IR::ByteCodeUsesInstr::New(instr->m_func);
  3620. byteCodeInstr->SetByteCodeOffset(instr);
  3621. byteCodeInstr->Set(src1Sym->m_id);
  3622. instr->InsertBefore(byteCodeInstr);
  3623. }
  3624. #if DBG_DUMP
  3625. if (Js::Configuration::Global.flags.Trace.IsEnabled(Js::InlinerConstFoldPhase, this->topFunc->GetSourceContextId(), this->topFunc->GetLocalFunctionId()))
  3626. {
  3627. Output::Print(L"Constant folding to %d\n", *pValue);
  3628. instr->Dump();
  3629. }
  3630. #endif
  3631. instr->m_opcode = Js::OpCode::LdC_A_I4;
  3632. instr->ReplaceSrc1(IR::IntConstOpnd::New(*pValue, TyInt32, instr->m_func));
  3633. instr->GetDst()->AsRegOpnd()->m_sym->SetIsConst();
  3634. }
  3635. return true;
  3636. }
  3637. Js::ArgSlot
  3638. Inline::MapActuals(IR::Instr *callInstr, __out_ecount(maxParamCount) IR::Instr *argOuts[],
  3639. Js::ArgSlot formalCount,
  3640. Func* inlinee,
  3641. Js::ProfileId callSiteId,
  3642. bool *stackArgsArgOutExpanded,
  3643. IR::Instr *argOutsExtra[],
  3644. Js::ArgSlot maxParamCount /* = Js::InlineeCallInfo::MaxInlineeArgoutCount*/)
  3645. {
  3646. AnalysisAssert(formalCount <= maxParamCount);
  3647. IR::Opnd *linkOpnd = callInstr->GetSrc2();
  3648. Js::ArgSlot actualCount = 0;
  3649. *stackArgsArgOutExpanded = false;
  3650. uint inlineeFrameSlot = currentInlineeFrameSlot + (Js::Constants::InlineeMetaArgCount - 1);
  3651. uint fixupArgoutCount = 0;
  3652. if (inlinee)
  3653. {
  3654. bool hasArgumentsAccess = this->GetInlineeHasArgumentObject(inlinee);
  3655. inlinee->SetHasUnoptimizedArgumentsAccess(hasArgumentsAccess);
  3656. }
  3657. if (linkOpnd->IsSymOpnd())
  3658. {
  3659. IR::Instr *argInstr;
  3660. do
  3661. {
  3662. Assert(linkOpnd->IsSymOpnd());
  3663. StackSym *sym = linkOpnd->AsSymOpnd()->m_sym->AsStackSym();
  3664. Assert(sym->m_isSingleDef);
  3665. Assert(sym->IsArgSlotSym());
  3666. sym->m_isInlinedArgSlot = true;
  3667. this->topFunc->SetArgOffset(sym, (inlineeFrameSlot + sym->GetArgSlotNum()) * MachPtr);
  3668. argInstr = sym->m_instrDef;
  3669. if (argInstr->m_opcode == Js::OpCode::ArgOut_A)
  3670. {
  3671. if(inlinee)
  3672. {
  3673. if (!inlinee->GetHasUnoptimizedArgumentsAcccess())
  3674. {
  3675. // This allows us to markTemp the argOut source.
  3676. argInstr->m_opcode = Js::OpCode::ArgOut_A_Inline;
  3677. }
  3678. }
  3679. argInstr->GenerateBytecodeArgOutCapture();
  3680. }
  3681. // Expand
  3682. //
  3683. // s31 ArgOut_A s32
  3684. // s30 ArgOut_A_FromStackArgs s31
  3685. //
  3686. // to
  3687. //
  3688. // s31 ArgOut_A(_Inline) s32
  3689. // sXX ArgOut_A_FixupForStackArgs s31
  3690. // .
  3691. // .
  3692. // s34 ArgOut_A_FixupForStackArgs sXX
  3693. // s30 ArgOut_A_FromStackArgs s34
  3694. if (inlinee && argInstr->m_opcode == Js::OpCode::ArgOut_A_FromStackArgs)
  3695. {
  3696. IR::Instr * argFixupInstr;
  3697. for(uint currentFormal = 1; currentFormal < formalCount; currentFormal++)
  3698. {
  3699. StackSym* newStackSym = StackSym::NewArgSlotSym(sym->GetArgSlotNum(), argInstr->m_func);
  3700. newStackSym->m_isInlinedArgSlot = true;
  3701. IR::SymOpnd * linkOpnd = IR::SymOpnd::New(newStackSym, sym->GetType(), argInstr->m_func);
  3702. IR::Opnd * undefined = IR::AddrOpnd::New(this->topFunc->GetScriptContext()->GetLibrary()->GetUndefined(),
  3703. IR::AddrOpndKindDynamicVar, this->topFunc, true);
  3704. undefined->SetValueType(ValueType::Undefined);
  3705. argFixupInstr = IR::Instr::New(Js::OpCode::ArgOut_A_FixupForStackArgs, linkOpnd, undefined, argInstr->GetSrc2(), argInstr->m_func);
  3706. argInstr->InsertBefore(argFixupInstr);
  3707. argInstr->ReplaceSrc2(argFixupInstr->GetDst());
  3708. sym->IncrementArgSlotNum();
  3709. argInstr->m_func->SetArgOffset(sym, (inlineeFrameSlot + sym->GetArgSlotNum()) * MachPtr);
  3710. argFixupInstr->GenerateArgOutSnapshot();
  3711. fixupArgoutCount++;
  3712. }
  3713. // Now that the arguments object has been expanded, we don't require the sym corresponding to it.
  3714. IR::IntConstOpnd* callSiteIdOpnd = IR::IntConstOpnd::New(callSiteId, TyUint16, argInstr->m_func);
  3715. argInstr->ReplaceSrc1(callSiteIdOpnd);
  3716. // Don't count ArgOut_A_FromStackArgs as an actual, when it has been expanded
  3717. --actualCount;
  3718. *stackArgsArgOutExpanded = true;
  3719. }
  3720. ++actualCount;
  3721. const Js::ArgSlot currentActual = sym->GetArgSlotNum() - 1;
  3722. if (currentActual < formalCount)
  3723. {
  3724. Assert(currentActual < Js::InlineeCallInfo::MaxInlineeArgoutCount);
  3725. argOuts[currentActual] = argInstr;
  3726. }
  3727. // We don't want to treat ArgOut_A_FromStackArgs as an actual arg.
  3728. else if (argInstr->m_opcode != Js::OpCode::ArgOut_A_FromStackArgs)
  3729. {
  3730. Assert(currentActual <= Js::InlineeCallInfo::MaxInlineeArgoutCount);
  3731. if(argOutsExtra)
  3732. {
  3733. argOutsExtra[currentActual] = argInstr;
  3734. if (currentActual < maxParamCount)
  3735. {
  3736. __analysis_assume(currentActual < Js::InlineeCallInfo::MaxInlineeArgoutCount);
  3737. argOuts[currentActual] = nullptr;
  3738. }
  3739. }
  3740. }
  3741. linkOpnd = argInstr->GetSrc2();
  3742. }
  3743. while (linkOpnd->IsSymOpnd());
  3744. #if DBG
  3745. Assert(actualCount <= Js::InlineeCallInfo::MaxInlineeArgoutCount);
  3746. for(Js::ArgSlot i = 0; i < min(actualCount, formalCount); ++i)
  3747. {
  3748. #pragma prefast(suppress:6001)
  3749. Assert(argOuts[i]);
  3750. }
  3751. #endif
  3752. }
  3753. Assert(linkOpnd->IsRegOpnd());
  3754. Assert(linkOpnd->AsRegOpnd()->m_sym->m_isSingleDef);
  3755. Js::OpCode startCallOpCode = linkOpnd->AsRegOpnd()->m_sym->m_instrDef->m_opcode;
  3756. Assert(startCallOpCode == Js::OpCode::StartCall);
  3757. // Update the count in StartCall to reflect
  3758. // 1. ArgOut_A_FromStackArgs is not an actual once it has been expanded.
  3759. // 2. The expanded argouts (from ArgOut_A_FromStackArgs).
  3760. //
  3761. // Note that the StartCall will reflect the formal count only as of now; the actual count would be set during MapFormals
  3762. if(*stackArgsArgOutExpanded)
  3763. {
  3764. // TODO: Is an underflow here intended, it triggers on test\inlining\OS_2733280.js
  3765. IR::IntConstOpnd * countOpnd = linkOpnd->AsRegOpnd()->m_sym->m_instrDef->GetSrc1()->AsIntConstOpnd();
  3766. int32 count = countOpnd->AsInt32();
  3767. count += fixupArgoutCount - 1;
  3768. countOpnd->SetValue(count);
  3769. callInstr->m_func->EnsureCallSiteToArgumentsOffsetFixupMap();
  3770. Assert(!(callInstr->m_func->callSiteToArgumentsOffsetFixupMap->ContainsKey(callSiteId)));
  3771. callInstr->m_func->callSiteToArgumentsOffsetFixupMap->Add(callSiteId, fixupArgoutCount - 1);
  3772. }
  3773. Assert(linkOpnd->AsRegOpnd()->m_sym->m_instrDef->GetArgOutCount(/*getInterpreterArgOutCount*/ false) == actualCount);
  3774. // Mark the StartCall's dst as an inlined arg slot as well so we know this is an inlined start call
  3775. // and not adjust the stack height on x86
  3776. linkOpnd->AsRegOpnd()->m_sym->m_isInlinedArgSlot = true;
  3777. // Missing arguments...
  3778. for (Js::ArgSlot i = actualCount; i < formalCount; i++)
  3779. {
  3780. argOuts[i] = nullptr;
  3781. }
  3782. // We may not know the exact number of actuals that "b" gets in a.b.apply just yet, since we have expanded the ArgOut_A_FromStackArgs based on the number of formals "b" accepts.
  3783. // So, return the actualCount stored on the func if the ArgOut_A_FromStackArgs was expanded (and thus, the expanded argouts were accounted for in calculating the local actualCount)
  3784. return *stackArgsArgOutExpanded ? callInstr->m_func->actualCount : actualCount;
  3785. }
  3786. void
  3787. Inline::MapFormals(Func *inlinee,
  3788. __in_ecount(formalCount) IR::Instr *argOuts[],
  3789. uint formalCount,
  3790. uint actualCount,
  3791. IR::RegOpnd *retOpnd,
  3792. IR::Opnd * funcObjOpnd,
  3793. const StackSym *symCallerThis,
  3794. bool stackArgsArgOutExpanded,
  3795. bool fixedFunctionSafeThis,
  3796. IR::Instr *argOutsExtra[])
  3797. {
  3798. IR::SymOpnd *formalOpnd;
  3799. uint argIndex;
  3800. uint formalCountForInlinee;
  3801. IR::Instr * argInstr;
  3802. IR::Opnd * linkOpnd;
  3803. bool fUsesSafeThis = false;
  3804. bool fUsesConstThis = false;
  3805. StackSym *symThis = nullptr;
  3806. Js::Var thisConstVar = nullptr;
  3807. FOREACH_INSTR_EDITING(instr, instrNext, inlinee->m_headInstr)
  3808. {
  3809. switch (instr->m_opcode)
  3810. {
  3811. case Js::OpCode::ArgIn_Rest:
  3812. {
  3813. // We only currently support a statically known number of actuals.
  3814. if (stackArgsArgOutExpanded)
  3815. {
  3816. break;
  3817. }
  3818. IR::Opnd *restDst = instr->GetDst();
  3819. Assert(actualCount < 1 << 24 && formalCount < 1 << 24); // 24 bits for arg count (see CallInfo.h)
  3820. int excess = actualCount - formalCount;
  3821. if (excess < 0)
  3822. {
  3823. excess = 0;
  3824. }
  3825. // Set the type info about the destination so the array offsets get calculated properly.
  3826. restDst->SetValueType(
  3827. ValueType::GetObject(ObjectType::Array)
  3828. .SetHasNoMissingValues(true)
  3829. .SetArrayTypeId(Js::TypeIds_Array));
  3830. restDst->SetValueTypeFixed();
  3831. // Create the array and assign the elements.
  3832. IR::Instr *newArrInstr = IR::Instr::New(Js::OpCode::NewScArray, restDst, IR::IntConstOpnd::New(excess, TyUint32, inlinee), inlinee);
  3833. instr->InsertBefore(newArrInstr);
  3834. for (uint i = formalCount; i < actualCount; ++i)
  3835. {
  3836. IR::IndirOpnd *arrayLocOpnd = IR::IndirOpnd::New(restDst->AsRegOpnd(), i - formalCount, TyVar, inlinee);
  3837. IR::Instr *stElemInstr = IR::Instr::New(Js::OpCode::StElemC, arrayLocOpnd, argOutsExtra[i]->GetBytecodeArgOutCapture()->GetDst(), inlinee);
  3838. instr->InsertBefore(stElemInstr);
  3839. }
  3840. instr->Remove();
  3841. break;
  3842. }
  3843. case Js::OpCode::ArgIn_A:
  3844. formalOpnd = instr->UnlinkSrc1()->AsSymOpnd();
  3845. argIndex = formalOpnd->m_sym->AsStackSym()->GetParamSlotNum() - 1;
  3846. if (argIndex >= formalCount)
  3847. {
  3848. Fatal();
  3849. }
  3850. formalOpnd->Free(this->topFunc);
  3851. if (argOuts[argIndex])
  3852. {
  3853. IR::Instr *argOut = argOuts[argIndex];
  3854. IR::Instr* instrDef;
  3855. if (argOut->HasByteCodeArgOutCapture())
  3856. {
  3857. instrDef = argOut->GetBytecodeArgOutCapture();
  3858. }
  3859. else
  3860. {
  3861. Assert(argOut->m_opcode == Js::OpCode::ArgOut_A_FixupForStackArgs);
  3862. instrDef = argOut->GetArgOutSnapshot();
  3863. }
  3864. instr->SetSrc1(instrDef->GetDst());
  3865. instr->m_opcode = Js::OpCode::Ld_A;
  3866. IR::Opnd* dst = instr->GetDst();
  3867. IR::Opnd* src = instrDef->GetSrc1();
  3868. if (argIndex == 0)
  3869. {
  3870. // Look at the "this" argument source.
  3871. // If it's known to be a normal object (the caller has already guaranteed that, or
  3872. // it was defined by an instruction that produces normal objects), we'll omit CheckThis.
  3873. // If it's a constant value, we'll do the mapping at jit time and copy the final value.
  3874. if (src->IsRegOpnd())
  3875. {
  3876. symThis = dst->AsRegOpnd()->m_sym;
  3877. StackSym *symSrc = src->AsRegOpnd()->m_sym;
  3878. if (symSrc == symCallerThis ||
  3879. symSrc->m_isSafeThis ||
  3880. inlinee->IsInlinedConstructor())
  3881. {
  3882. fUsesSafeThis = true;
  3883. }
  3884. else if (symSrc->m_isSingleDef && symSrc->IsConst() && !symSrc->IsIntConst() && !symSrc->IsFloatConst())
  3885. {
  3886. thisConstVar = symSrc->GetConstAddress();
  3887. fUsesConstThis = true;
  3888. }
  3889. else if(fixedFunctionSafeThis)
  3890. {
  3891. // Note this need to come after we determined that this pointer is not const (undefined/null)
  3892. fUsesSafeThis = true;
  3893. }
  3894. }
  3895. }
  3896. }
  3897. else
  3898. {
  3899. instr->SetSrc1(IR::AddrOpnd::New(this->topFunc->GetScriptContext()->GetLibrary()->GetUndefined(),
  3900. IR::AddrOpndKindDynamicVar, this->topFunc, true));
  3901. instr->GetSrc1()->SetValueType(ValueType::Undefined);
  3902. instr->m_opcode = Js::OpCode::Ld_A;
  3903. }
  3904. break;
  3905. case Js::OpCode::ArgOut_A_FromStackArgs:
  3906. {
  3907. linkOpnd = instr->GetSrc2();
  3908. if(!linkOpnd->IsSymOpnd())
  3909. {
  3910. break;
  3911. }
  3912. Assert(instr->GetSrc1()->IsIntConstOpnd());
  3913. Js::ProfileId callSiteId = static_cast<Js::ProfileId>(instr->GetSrc1()->AsIntConstOpnd()->GetValue());
  3914. argInstr = linkOpnd->AsSymOpnd()->m_sym->AsStackSym()->GetInstrDef();
  3915. while(linkOpnd->IsSymOpnd())
  3916. {
  3917. argInstr = linkOpnd->AsSymOpnd()->m_sym->AsStackSym()->GetInstrDef();
  3918. linkOpnd = argInstr->GetSrc2();
  3919. }
  3920. Assert(linkOpnd->IsRegOpnd());
  3921. Js::OpCode startCallOpCode = linkOpnd->AsRegOpnd()->m_sym->AsStackSym()->GetInstrDef()->m_opcode;
  3922. Assert(startCallOpCode == Js::OpCode::StartCall);
  3923. IR::Instr* startCallForInlinee = linkOpnd->AsRegOpnd()->m_sym->AsStackSym()->GetInstrDef();
  3924. formalCountForInlinee = startCallForInlinee->GetArgOutCount(false); // As of now, StartCall has the formal count
  3925. if(actualCount < formalCountForInlinee)
  3926. {
  3927. RemoveExtraFixupArgouts(instr, formalCountForInlinee - actualCount, callSiteId);
  3928. startCallForInlinee->GetSrc1()->AsIntConstOpnd()->DecrValue(formalCountForInlinee - actualCount); //account for the extra formals
  3929. }
  3930. linkOpnd = instr->GetSrc2();
  3931. argInstr = linkOpnd->AsSymOpnd()->m_sym->AsStackSym()->GetInstrDef();
  3932. argIndex = ((actualCount < formalCountForInlinee) ? actualCount : formalCountForInlinee) - 1;
  3933. for ( ; argIndex > 0; argIndex--)
  3934. {
  3935. if(argInstr->m_opcode != Js::OpCode::ArgOut_A_FixupForStackArgs)
  3936. {
  3937. break;
  3938. }
  3939. Assert(!argInstr->HasByteCodeArgOutCapture()); // ArgOut_A_FixupForStackArgs should not be restored on bailout, so we don't generate ByteCodeArgOutCapture for these argouts.
  3940. IR::Instr* currentArgOutInstr = nullptr;
  3941. if(argOuts[argIndex])
  3942. {
  3943. currentArgOutInstr = argOuts[argIndex];
  3944. }
  3945. else if(argOutsExtra && argOutsExtra[argIndex])
  3946. {
  3947. currentArgOutInstr = argOutsExtra[argIndex];
  3948. }
  3949. if(currentArgOutInstr)
  3950. {
  3951. Assert(currentArgOutInstr->m_opcode == Js::OpCode::ArgOut_A || currentArgOutInstr->m_opcode == Js::OpCode::ArgOut_A_Inline);
  3952. IR::Instr* bytecodeArgoutCapture = currentArgOutInstr->GetBytecodeArgOutCapture();
  3953. IR::Instr* formalArgOutUse = argInstr->GetArgOutSnapshot();
  3954. Assert(formalArgOutUse->m_opcode == Js::OpCode::Ld_A);
  3955. Assert(formalArgOutUse->GetSrc1()->AsAddrOpnd()->m_address == this->topFunc->GetScriptContext()->GetLibrary()->GetUndefined());
  3956. formalArgOutUse->ReplaceSrc1(bytecodeArgoutCapture->GetDst());
  3957. linkOpnd = argInstr->GetSrc2();
  3958. argInstr = linkOpnd->AsSymOpnd()->m_sym->AsStackSym()->GetInstrDef();
  3959. }
  3960. }
  3961. if (formalCountForInlinee < actualCount)
  3962. {
  3963. FixupExtraActualParams(instr, argOuts, argOutsExtra, formalCountForInlinee, actualCount, callSiteId);
  3964. startCallForInlinee->GetSrc1()->AsIntConstOpnd()->IncrValue(actualCount - formalCountForInlinee); //account for the extra actuals
  3965. }
  3966. break;
  3967. }
  3968. case Js::OpCode::InlineeStart:
  3969. {
  3970. linkOpnd = instr->GetSrc2();
  3971. if(!linkOpnd->IsSymOpnd())
  3972. {
  3973. break;
  3974. }
  3975. IR::Instr* stackArgsInstr = linkOpnd->AsSymOpnd()->m_sym->AsStackSym()->GetInstrDef();
  3976. if (stackArgsInstr->m_opcode == Js::OpCode::ArgOut_A_FromStackArgs)
  3977. {
  3978. linkOpnd = stackArgsInstr->GetSrc2();
  3979. argInstr = linkOpnd->AsSymOpnd()->m_sym->AsStackSym()->GetInstrDef();
  3980. Assert(argInstr->m_opcode == Js::OpCode::ArgOut_A_Inline || argInstr->m_opcode == Js::OpCode::ArgOut_A_FixupForStackArgs || argInstr->m_opcode == Js::OpCode::ArgOut_A);
  3981. stackArgsInstr->Remove();
  3982. Assert(argInstr->GetDst()->IsSymOpnd());
  3983. instr->ReplaceSrc2(argInstr->GetDst());
  3984. }
  3985. break;
  3986. }
  3987. case Js::OpCode::LdEnv:
  3988. if (instr->m_func == inlinee)
  3989. {
  3990. // Need to give the inlinee's function to load the environment
  3991. if (funcObjOpnd->IsAddrOpnd())
  3992. {
  3993. instr->m_opcode = Js::OpCode::Ld_A;
  3994. instr->SetSrc1(IR::AddrOpnd::New(Js::ScriptFunction::FromVar(funcObjOpnd->AsAddrOpnd()->m_address)->GetEnvironment(),
  3995. IR::AddrOpndKindDynamicFrameDisplay, instr->m_func));
  3996. }
  3997. else
  3998. {
  3999. instr->SetSrc1(funcObjOpnd);
  4000. }
  4001. }
  4002. else
  4003. {
  4004. Assert(instr->GetSrc1() != nullptr);
  4005. }
  4006. break;
  4007. case Js::OpCode::LdNewTarget:
  4008. if (instr->m_func == inlinee)
  4009. {
  4010. if (instr->m_func->IsInlinedConstructor())
  4011. {
  4012. instr->SetSrc1(funcObjOpnd);
  4013. }
  4014. else
  4015. {
  4016. instr->SetSrc1(IR::AddrOpnd::New(this->topFunc->GetScriptContext()->GetLibrary()->GetUndefined(),
  4017. IR::AddrOpndKindDynamicVar, this->topFunc, true));
  4018. instr->GetSrc1()->SetValueType(ValueType::Undefined);
  4019. }
  4020. instr->m_opcode = Js::OpCode::Ld_A;
  4021. }
  4022. break;
  4023. case Js::OpCode::ChkNewCallFlag:
  4024. if (instr->m_func == inlinee)
  4025. {
  4026. if (instr->m_func->IsInlinedConstructor())
  4027. {
  4028. instr->Remove();
  4029. }
  4030. else
  4031. {
  4032. // InliningDecider::Inline should have decided not to inline this since we are going to end up throwing anyway
  4033. Assert(false);
  4034. }
  4035. }
  4036. break;
  4037. case Js::OpCode::LdSuper:
  4038. case Js::OpCode::LdSuperCtor:
  4039. if (instr->m_func == inlinee)
  4040. {
  4041. instr->SetSrc1(funcObjOpnd);
  4042. }
  4043. else
  4044. {
  4045. Assert(instr->GetSrc1() != nullptr);
  4046. }
  4047. break;
  4048. case Js::OpCode::LdThis:
  4049. case Js::OpCode::StrictLdThis:
  4050. // Optimization of LdThis may be possible.
  4051. // Verify that this is a use of the "this" passed by the caller (not a nested function).
  4052. if (instr->GetSrc1()->AsRegOpnd()->m_sym == symThis)
  4053. {
  4054. if (fUsesSafeThis)
  4055. {
  4056. // No need for any "this" mapping.
  4057. instrNext = this->RemoveLdThis(instr);
  4058. break;
  4059. }
  4060. else if (fUsesConstThis)
  4061. {
  4062. // "this" is a constant, so map it now.
  4063. // Don't bother mapping if it's not an object, though, since we'd have to create a
  4064. // boxed value at JIT time, and that case doesn't seem worth it.
  4065. Js::TypeId typeId = Js::JavascriptOperators::GetTypeId(thisConstVar);
  4066. if (Js::JavascriptOperators::IsObjectType(typeId) ||
  4067. Js::JavascriptOperators::IsUndefinedOrNullType(typeId))
  4068. {
  4069. Js::ScriptContext *scriptContext = inlinee->GetScriptContext();
  4070. if (instr->m_opcode == Js::OpCode::LdThis)
  4071. {
  4072. thisConstVar = Js::JavascriptOperators::OP_GetThis(
  4073. thisConstVar, instr->GetSrc2()->AsIntConstOpnd()->AsInt32(), scriptContext);
  4074. instr->FreeSrc2();
  4075. }
  4076. else
  4077. {
  4078. thisConstVar = Js::JavascriptOperators::OP_StrictGetThis(thisConstVar, scriptContext);
  4079. }
  4080. IR::Opnd *thisOpnd = IR::AddrOpnd::New(thisConstVar, IR::AddrOpndKindDynamicVar, inlinee, true);
  4081. instr->m_opcode = Js::OpCode::Ld_A;
  4082. instr->ReplaceSrc1(thisOpnd);
  4083. break;
  4084. }
  4085. }
  4086. }
  4087. // Couldn't eliminate the execution-time "this" mapping. Try to change it to a check.
  4088. instrNext = this->DoCheckThisOpt(instr);
  4089. break;
  4090. case Js::OpCode::Throw:
  4091. instr->m_opcode = Js::OpCode::InlineThrow;
  4092. instr->m_func->SetHasImplicitCallsOnSelfAndParents();
  4093. break;
  4094. case Js::OpCode::RuntimeTypeError:
  4095. instr->m_opcode = Js::OpCode::InlineRuntimeTypeError;
  4096. instr->m_func->SetHasImplicitCallsOnSelfAndParents();
  4097. break;
  4098. case Js::OpCode::RuntimeReferenceError:
  4099. instr->m_opcode = Js::OpCode::InlineRuntimeReferenceError;
  4100. instr->m_func->SetHasImplicitCallsOnSelfAndParents();
  4101. break;
  4102. case Js::OpCode::Ret:
  4103. if (!retOpnd)
  4104. {
  4105. instr->Remove();
  4106. }
  4107. else
  4108. {
  4109. instr->m_opcode = Js::OpCode::Ld_A;
  4110. instr->SetDst(retOpnd);
  4111. }
  4112. break;
  4113. }
  4114. } NEXT_INSTR_EDITING;
  4115. }
  4116. void
  4117. Inline::SetupInlineeFrame(Func *inlinee, IR::Instr *inlineeStart, Js::ArgSlot actualCount, IR::Opnd *functionObject)
  4118. {
  4119. Js::ArgSlot argSlots[Js::Constants::InlineeMetaArgCount] = {
  4120. actualCount + 1, /* argc */
  4121. actualCount + 2, /* function object */
  4122. actualCount + 3 /* arguments object slot */
  4123. };
  4124. IR::Opnd *srcs[Js::Constants::InlineeMetaArgCount] = {
  4125. IR::AddrOpnd::New((Js::Var)actualCount, IR::AddrOpndKindConstant, inlinee, true /*dontEncode*/),
  4126. /*
  4127. * Don't initialize this slot with the function object yet. In compat mode we evaluate
  4128. * the target only after evaluating all arguments. Having this SymOpnd here ensures it gets
  4129. * the correct slot in the frame. Lowerer fills this slot with the function object just
  4130. * before entering the inlinee when we're sure we've evaluated the target in all modes.
  4131. */
  4132. nullptr,
  4133. IR::AddrOpnd::NewNull(inlinee)
  4134. };
  4135. const IRType types[Js::Constants::InlineeMetaArgCount] = {
  4136. TyMachReg,
  4137. TyVar,
  4138. TyMachReg
  4139. };
  4140. for (unsigned instrIndex = 0; instrIndex < Js::Constants::InlineeMetaArgCount; instrIndex++)
  4141. {
  4142. StackSym *stackSym = inlinee->m_symTable->GetArgSlotSym(argSlots[instrIndex]);
  4143. stackSym->m_isInlinedArgSlot = true;
  4144. this->topFunc->SetArgOffset(stackSym, (currentInlineeFrameSlot + instrIndex) * MachPtr);
  4145. IR::SymOpnd *symOpnd = IR::SymOpnd::New(stackSym, 0, types[instrIndex], inlinee);
  4146. IR::Instr *instr = IR::Instr::New(Js::OpCode::InlineeMetaArg, inlinee);
  4147. instr->SetDst(symOpnd);
  4148. if (srcs[instrIndex])
  4149. {
  4150. instr->SetSrc1(srcs[instrIndex]);
  4151. }
  4152. inlineeStart->InsertBefore(instr);
  4153. if (instrIndex == 0)
  4154. {
  4155. inlinee->SetInlineeFrameStartSym(stackSym);
  4156. }
  4157. }
  4158. }
  4159. void
  4160. Inline::FixupExtraActualParams(IR::Instr * instr, IR::Instr *argOuts[], IR::Instr *argOutsExtra[], uint index, uint actualCount, Js::ProfileId callSiteId)
  4161. {
  4162. Assert(instr->m_opcode == Js::OpCode::ArgOut_A_FromStackArgs);
  4163. int offsetFixup;
  4164. Assert(instr->m_func->callSiteToArgumentsOffsetFixupMap->ContainsKey(callSiteId));
  4165. instr->m_func->callSiteToArgumentsOffsetFixupMap->TryGetValue(callSiteId, &offsetFixup);
  4166. StackSym *sym = instr->GetDst()->AsSymOpnd()->m_sym->AsStackSym();
  4167. while (index < actualCount)
  4168. {
  4169. IR::Instr* argOutToMapTo = argOuts[index] ? argOuts[index] : argOutsExtra[index];
  4170. StackSym* newStackSym = StackSym::NewArgSlotSym(sym->GetArgSlotNum(), instr->m_func);
  4171. newStackSym->m_isInlinedArgSlot = true;
  4172. this->topFunc->SetArgOffset(newStackSym, sym->m_offset);
  4173. sym->IncrementArgSlotNum();
  4174. this->topFunc->SetArgOffset(sym, sym->m_offset + MachPtr);
  4175. IR::SymOpnd * linkOpnd = IR::SymOpnd::New(newStackSym, sym->GetType(), instr->m_func);
  4176. IR::Instr * extraActualParamInstr = IR::Instr::New(Js::OpCode::ArgOut_A_FixupForStackArgs, linkOpnd, argOutToMapTo->GetSrc1(), instr->GetSrc2(), instr->m_func);
  4177. instr->InsertBefore(extraActualParamInstr);
  4178. extraActualParamInstr->GenerateArgOutSnapshot();
  4179. instr->m_func->callSiteToArgumentsOffsetFixupMap->Item(callSiteId, ++offsetFixup);
  4180. instr->ReplaceSrc2(extraActualParamInstr->GetDst());
  4181. index++;
  4182. }
  4183. }
  4184. void
  4185. Inline::RemoveExtraFixupArgouts(IR::Instr* instr, uint argoutRemoveCount, Js::ProfileId callSiteId)
  4186. {
  4187. Assert(instr->m_opcode == Js::OpCode::ArgOut_A_FromStackArgs);
  4188. int offsetFixup;
  4189. Assert(instr->m_func->callSiteToArgumentsOffsetFixupMap->ContainsKey(callSiteId));
  4190. instr->m_func->callSiteToArgumentsOffsetFixupMap->TryGetValue(callSiteId, &offsetFixup);
  4191. StackSym* argSym = instr->GetDst()->AsSymOpnd()->m_sym->AsStackSym();
  4192. IR::Instr* argInstr = instr->GetSrc2()->AsSymOpnd()->m_sym->AsStackSym()->GetInstrDef();
  4193. for(uint argIndex = 0; argIndex < argoutRemoveCount; argIndex++)
  4194. {
  4195. Assert(argInstr->m_opcode == Js::OpCode::ArgOut_A_FixupForStackArgs);
  4196. Assert(!argInstr->HasByteCodeArgOutCapture()); // ArgOut_A_FixupForStackArgs should not be restored on bailout, so we don't generate ByteCodeArgOutCapture for these argouts.
  4197. instr->ReplaceSrc2(argInstr->GetSrc2());
  4198. argSym->DecrementArgSlotNum();
  4199. argSym->m_offset -= MachPtr;
  4200. argSym->m_allocated = true;
  4201. argInstr->Remove();
  4202. instr->m_func->callSiteToArgumentsOffsetFixupMap->Item(callSiteId, --offsetFixup);
  4203. argInstr = instr->GetSrc2()->AsSymOpnd()->m_sym->AsStackSym()->GetInstrDef();
  4204. }
  4205. }
  4206. IR::Instr *
  4207. Inline::DoCheckThisOpt(IR::Instr * instr)
  4208. {
  4209. IR::Instr * instrNext = instr->m_next;
  4210. if (PHASE_OFF(Js::CheckThisPhase, instr->m_func->GetTopFunc()))
  4211. {
  4212. return instrNext;
  4213. }
  4214. if (!PHASE_FORCE(Js::CheckThisPhase, instr->m_func->GetTopFunc()))
  4215. {
  4216. if (!instr->m_func->HasProfileInfo())
  4217. {
  4218. return instrNext;
  4219. }
  4220. if (instr->m_func->GetProfileInfo()->GetThisInfo().thisType != Js::ThisType_Simple)
  4221. {
  4222. return instrNext;
  4223. }
  4224. if (instr->m_func->GetProfileInfo()->IsCheckThisDisabled())
  4225. {
  4226. return instrNext;
  4227. }
  4228. }
  4229. // If the instr is an inlined LdThis, try to replace it with a CheckThis
  4230. // that will bail out if a helper call is required to get the real "this" pointer.
  4231. Assert(instr->m_opcode == Js::OpCode::LdThis || instr->m_opcode == Js::OpCode::StrictLdThis);
  4232. Assert(instr->IsInlined());
  4233. // Create the CheckThis. The target is the original offset, i.e., the LdThis still has to be executed.
  4234. if(instr->m_opcode == Js::OpCode::LdThis)
  4235. {
  4236. instr->FreeSrc2();
  4237. }
  4238. IR::Instr *newInstr =
  4239. IR::BailOutInstr::New( instr->m_opcode == Js::OpCode::LdThis ? Js::OpCode::CheckThis : Js::OpCode::StrictCheckThis, IR::BailOutCheckThis, instr, instr->m_func);
  4240. // Just re-use the original src1 since the LdThis will usually be deleted.
  4241. newInstr->SetSrc1(instr->GetSrc1());
  4242. newInstr->SetByteCodeOffset(instr);
  4243. instr->InsertBefore(newInstr);
  4244. return this->RemoveLdThis(instr);
  4245. }
  4246. IR::Instr *
  4247. Inline::RemoveLdThis(IR::Instr *instr)
  4248. {
  4249. // Replace the original instr with a copy, if needed.
  4250. if (instr->GetDst()->IsEqual(instr->GetSrc1()))
  4251. {
  4252. // The copy would be a nop, so just delete.
  4253. IR::Instr *instrNext = instr->m_next;
  4254. instr->Remove();
  4255. return instrNext;
  4256. }
  4257. else
  4258. {
  4259. instr->m_opcode = Js::OpCode::Ld_A;
  4260. return instr;
  4261. }
  4262. }
  4263. bool
  4264. Inline::IsArgumentsOpnd(IR::Opnd* opnd, SymID argumentsSymId)
  4265. {
  4266. if (opnd->IsRegOpnd())
  4267. {
  4268. return argumentsSymId == opnd->AsRegOpnd()->m_sym->m_id;
  4269. }
  4270. else if (opnd->IsSymOpnd())
  4271. {
  4272. Sym *sym = opnd->AsSymOpnd()->m_sym;
  4273. if (sym && sym->IsPropertySym())
  4274. {
  4275. PropertySym *propertySym = sym->AsPropertySym();
  4276. return argumentsSymId == propertySym->m_stackSym->m_id;
  4277. }
  4278. return false;
  4279. }
  4280. else if (opnd->IsIndirOpnd())
  4281. {
  4282. IR::RegOpnd *indexOpnd = opnd->AsIndirOpnd()->GetIndexOpnd();
  4283. IR::RegOpnd *baseOpnd = opnd->AsIndirOpnd()->GetBaseOpnd();
  4284. return (argumentsSymId == baseOpnd->m_sym->m_id) || (indexOpnd && indexOpnd->m_sym->m_id == argumentsSymId);
  4285. }
  4286. AssertMsg(false, "Unknown type");
  4287. return false;
  4288. }
  4289. bool
  4290. Inline::HasArgumentsAccess(IR::Opnd *opnd, SymID argumentsSymId)
  4291. {
  4292. // We should look at dst last to correctly handle cases where it's the same as one of the src operands.
  4293. if (opnd)
  4294. {
  4295. if (opnd->IsRegOpnd() || opnd->IsSymOpnd() || opnd->IsIndirOpnd())
  4296. {
  4297. if (IsArgumentsOpnd(opnd, argumentsSymId))
  4298. {
  4299. return true;
  4300. }
  4301. }
  4302. }
  4303. return false;
  4304. }
  4305. bool
  4306. Inline::HasArgumentsAccess(IR::Instr * instr, SymID argumentsSymId)
  4307. {
  4308. IR::Opnd* dst = instr->GetDst();
  4309. IR::Opnd* src1 = instr->GetSrc1();
  4310. IR::Opnd* src2 = instr->GetSrc2();
  4311. // Super conservative here, if we see the arguments or any of its alias being used in any
  4312. // other opcode just don't do this optimization.
  4313. if (HasArgumentsAccess(src1, argumentsSymId) || HasArgumentsAccess(src2, argumentsSymId))
  4314. {
  4315. return true;
  4316. }
  4317. if (dst)
  4318. {
  4319. // For dst no need to check for RegOpnd
  4320. if (dst->IsSymOpnd() || dst->IsIndirOpnd())
  4321. {
  4322. if (IsArgumentsOpnd(dst, argumentsSymId))
  4323. {
  4324. return true;
  4325. }
  4326. }
  4327. }
  4328. return false;
  4329. }
  4330. bool
  4331. Inline::GetInlineeHasArgumentObject(Func * inlinee)
  4332. {
  4333. if (!inlinee->GetHasArgumentObject())
  4334. {
  4335. // If inlinee has no arguments access return false
  4336. return false;
  4337. }
  4338. // Inlinee has arguments access
  4339. if (!inlinee->GetHasApplyTargetInlining())
  4340. {
  4341. // There is no apply target inlining (this.init.apply(this, arguments))
  4342. // So arguments access continues to exist
  4343. return true;
  4344. }
  4345. // Its possible there is no more arguments access after we inline apply target validate the same.
  4346. // This sounds expensive, but we are only walking inlinee which has apply target inlining optimization enabled.
  4347. // Also we walk only instruction in that inlinee and not nested inlinees. So it is not expensive.
  4348. SymID argumentsSymId = 0;
  4349. FOREACH_INSTR_IN_FUNC(instr, inlinee)
  4350. {
  4351. if (instr->m_func != inlinee)
  4352. {
  4353. // Skip nested inlinees
  4354. continue;
  4355. }
  4356. if (instr->m_opcode == Js::OpCode::LdHeapArguments || instr->m_opcode == Js::OpCode::LdLetHeapArguments)
  4357. {
  4358. argumentsSymId = instr->GetDst()->AsRegOpnd()->m_sym->m_id;
  4359. }
  4360. else if (argumentsSymId != 0)
  4361. {
  4362. // Once we find the arguments object i.e. argumentsSymId is set
  4363. // Make sure no one refers to it.
  4364. switch (instr->m_opcode)
  4365. {
  4366. case Js::OpCode::InlineBuiltInStart:
  4367. {
  4368. IR::Opnd* builtInOpnd = instr->GetSrc1();
  4369. if (builtInOpnd->IsAddrOpnd())
  4370. {
  4371. Assert(builtInOpnd->AsAddrOpnd()->m_isFunction);
  4372. Js::BuiltinFunction builtinFunction = Js::JavascriptLibrary::GetBuiltInForFuncInfo(((Js::JavascriptFunction*)builtInOpnd->AsAddrOpnd()->m_address)->GetFunctionInfo(), inlinee->GetScriptContext());
  4373. if (builtinFunction == Js::BuiltinFunction::Function_Apply)
  4374. {
  4375. this->SetIsInInlinedApplyCall(true);
  4376. }
  4377. }
  4378. else if (builtInOpnd->IsRegOpnd())
  4379. {
  4380. if (builtInOpnd->AsRegOpnd()->m_sym->m_builtInIndex == Js::BuiltinFunction::Function_Apply)
  4381. {
  4382. this->SetIsInInlinedApplyCall(true);
  4383. }
  4384. }
  4385. break;
  4386. }
  4387. case Js::OpCode::InlineBuiltInEnd:
  4388. {
  4389. if(this->GetIsInInlinedApplyCall())
  4390. {
  4391. this->SetIsInInlinedApplyCall(false);
  4392. }
  4393. break;
  4394. }
  4395. case Js::OpCode::BailOnNotStackArgs:
  4396. case Js::OpCode::LdArgumentsFromStack:
  4397. case Js::OpCode::ArgOut_A_InlineBuiltIn:
  4398. case Js::OpCode::BytecodeArgOutCapture:
  4399. case Js::OpCode::BytecodeArgOutUse:
  4400. // These are part of arguments optimization and we are fine if they access stack args.
  4401. break;
  4402. case Js::OpCode::ArgOut_A_FromStackArgs:
  4403. {
  4404. // If ArgOut_A_FromStackArgs is part of the call sequence for apply built-in inlining (as opposed to apply target inlining),
  4405. // then arguments access continues to exist.
  4406. if (this->GetIsInInlinedApplyCall() && HasArgumentsAccess(instr, argumentsSymId))
  4407. {
  4408. return true;
  4409. }
  4410. break;
  4411. }
  4412. default:
  4413. {
  4414. if (HasArgumentsAccess(instr, argumentsSymId))
  4415. {
  4416. return true;
  4417. }
  4418. }
  4419. }
  4420. }
  4421. }
  4422. NEXT_INSTR_IN_FUNC;
  4423. return false;
  4424. }
  4425. IR::Instr *
  4426. Inline::InlineSpread(IR::Instr *spreadCall)
  4427. {
  4428. Assert(Lowerer::IsSpreadCall(spreadCall));
  4429. if (spreadCall->m_func->GetJnFunction()->IsInlineSpreadDisabled()
  4430. || this->topFunc->GetJnFunction()->IsInlineSpreadDisabled())
  4431. {
  4432. return spreadCall;
  4433. }
  4434. IR::Instr *spreadIndicesInstr = Lowerer::GetLdSpreadIndicesInstr(spreadCall);
  4435. IR::Opnd *spreadIndicesOpnd = spreadIndicesInstr->GetSrc1();
  4436. Js::AuxArray<uint32>* spreadIndices = static_cast<Js::AuxArray<uint32>*>(spreadIndicesOpnd->AsAddrOpnd()->m_address);
  4437. Assert(spreadIndices->count > 0);
  4438. IR::Instr *argInstr = spreadIndicesInstr;
  4439. IR::SymOpnd *argLinkOpnd = argInstr->GetSrc2()->AsSymOpnd();
  4440. StackSym *argLinkSym = argLinkOpnd->m_sym->AsStackSym();
  4441. argInstr = argLinkSym->m_instrDef;
  4442. // We only support one spread argument for inlining.
  4443. if (argLinkSym->GetArgSlotNum() > 2)
  4444. {
  4445. return spreadCall;
  4446. }
  4447. // We are now committed to inlining spread. Remove the LdSpreadIndices instr
  4448. // and convert the spread and 'this' ArgOuts.
  4449. spreadCall->ReplaceSrc2(argLinkOpnd);
  4450. spreadIndicesInstr->Remove();
  4451. // Insert the bailout before the array ArgOut
  4452. IR::Opnd *arrayOpnd = argInstr->GetSrc1();
  4453. argInstr->m_opcode = Js::OpCode::ArgOut_A_SpreadArg;
  4454. IR::Instr *bailoutInstr = IR::BailOutInstr::New(Js::OpCode::BailOnNotSpreadable, IR::BailOutOnInlineFunction, argInstr, argInstr->m_func);
  4455. bailoutInstr->SetSrc1(arrayOpnd);
  4456. argInstr->InsertBefore(bailoutInstr);
  4457. argLinkOpnd = argInstr->GetSrc2()->AsSymOpnd();
  4458. argLinkSym = argLinkOpnd->m_sym->AsStackSym();
  4459. argInstr = argLinkSym->m_instrDef;
  4460. argInstr->m_opcode = Js::OpCode::ArgOut_A_Dynamic;
  4461. IR::RegOpnd *startCallDstOpnd = argInstr->GetSrc2()->AsRegOpnd();
  4462. argLinkSym = startCallDstOpnd->m_sym->AsStackSym();
  4463. argInstr = argLinkSym->m_instrDef;
  4464. Assert(argInstr->m_opcode == Js::OpCode::StartCall);
  4465. spreadCall->m_opcode = Js::OpCode::CallIDynamicSpread;
  4466. return spreadCall;
  4467. }
  4468. void
  4469. Inline::TryResetObjTypeSpecFldInfoOn(IR::PropertySymOpnd* propertySymOpnd)
  4470. {
  4471. // if an objTypeSpecFldInfo was created just for the purpose of polymorphic inlining but didn't get used for the same (for some reason or the other), and the polymorphic cache it was created from, wasn't equivalent,
  4472. // we should null out this info on the propertySymOpnd so that assumptions downstream around equivalent object type spec still hold.
  4473. if (propertySymOpnd)
  4474. {
  4475. propertySymOpnd->TryResetObjTypeSpecFldInfo();
  4476. }
  4477. }
  4478. void
  4479. Inline::TryDisableRuntimePolymorphicCacheOn(IR::PropertySymOpnd* propertySymOpnd)
  4480. {
  4481. if (propertySymOpnd)
  4482. {
  4483. propertySymOpnd->TryDisableRuntimePolymorphicCache();
  4484. }
  4485. }
  4486. IR::PropertySymOpnd*
  4487. Inline::GetMethodLdOpndForCallInstr(IR::Instr* callInstr)
  4488. {
  4489. IR::Opnd* methodOpnd = callInstr->GetSrc1();
  4490. if (methodOpnd->IsRegOpnd())
  4491. {
  4492. if (methodOpnd->AsRegOpnd()->m_sym->IsStackSym())
  4493. {
  4494. if (methodOpnd->AsRegOpnd()->m_sym->AsStackSym()->IsSingleDef())
  4495. {
  4496. IR::Instr* defInstr = methodOpnd->AsRegOpnd()->m_sym->AsStackSym()->GetInstrDef();
  4497. if (defInstr->GetSrc1() && defInstr->GetSrc1()->IsSymOpnd() && defInstr->GetSrc1()->AsSymOpnd()->IsPropertySymOpnd())
  4498. {
  4499. return defInstr->GetSrc1()->AsSymOpnd()->AsPropertySymOpnd();
  4500. }
  4501. return nullptr;
  4502. }
  4503. return nullptr;
  4504. }
  4505. return nullptr;
  4506. }
  4507. return nullptr;
  4508. }
  4509. // SIMD_JS
  4510. /*
  4511. Fixes the format of a SIMD load/store to match format expected by globOpt. Namely:
  4512. Load:
  4513. dst = Simd128LdArr arr, index
  4514. becomes
  4515. dst = Simd128LdArr [arr, indx]
  4516. Store:
  4517. t3 = EA arr
  4518. t2 = EA index, t3
  4519. t1 = EA value, t2
  4520. Simd128StArr t1
  4521. becomes
  4522. [arr, index] = Simd128StArr value
  4523. It also sets width in bytes of data to be loaded. Needed for bound check generation in GlobOpt.
  4524. */
  4525. void
  4526. Inline::Simd128FixLoadStoreInstr(Js::BuiltinFunction builtInId, IR::Instr * callInstr)
  4527. {
  4528. bool isStore = false;
  4529. callInstr->dataWidth = 0;
  4530. switch (builtInId)
  4531. {
  4532. case Js::BuiltinFunction::SIMD_Float32x4_Store:
  4533. case Js::BuiltinFunction::SIMD_Int32x4_Store:
  4534. isStore = true;
  4535. // fall through
  4536. case Js::BuiltinFunction::SIMD_Float32x4_Load:
  4537. case Js::BuiltinFunction::SIMD_Int32x4_Load:
  4538. callInstr->dataWidth = 16;
  4539. break;
  4540. case Js::BuiltinFunction::SIMD_Float32x4_Store3:
  4541. case Js::BuiltinFunction::SIMD_Int32x4_Store3:
  4542. isStore = true;
  4543. // fall through
  4544. case Js::BuiltinFunction::SIMD_Float32x4_Load3:
  4545. case Js::BuiltinFunction::SIMD_Int32x4_Load3:
  4546. callInstr->dataWidth = 12;
  4547. break;
  4548. case Js::BuiltinFunction::SIMD_Float32x4_Store2:
  4549. case Js::BuiltinFunction::SIMD_Int32x4_Store2:
  4550. isStore = true;
  4551. // fall through
  4552. case Js::BuiltinFunction::SIMD_Float32x4_Load2:
  4553. case Js::BuiltinFunction::SIMD_Int32x4_Load2:
  4554. callInstr->dataWidth = 8;
  4555. break;
  4556. case Js::BuiltinFunction::SIMD_Float32x4_Store1:
  4557. case Js::BuiltinFunction::SIMD_Int32x4_Store1:
  4558. isStore = true;
  4559. // fall through
  4560. case Js::BuiltinFunction::SIMD_Float32x4_Load1:
  4561. case Js::BuiltinFunction::SIMD_Int32x4_Load1:
  4562. callInstr->dataWidth = 4;
  4563. break;
  4564. default:
  4565. // nothing to do
  4566. return;
  4567. }
  4568. IR::IndirOpnd *indirOpnd;
  4569. if (!isStore)
  4570. {
  4571. // load
  4572. indirOpnd = IR::IndirOpnd::New(callInstr->GetSrc1()->AsRegOpnd(), callInstr->GetSrc2()->AsRegOpnd(), TyVar, callInstr->m_func);
  4573. callInstr->ReplaceSrc1(indirOpnd);
  4574. callInstr->FreeSrc2();
  4575. }
  4576. else
  4577. {
  4578. IR::Opnd *linkOpnd = callInstr->GetSrc1();
  4579. IR::Instr *eaInstr1, *eaInstr2, *eaInstr3;
  4580. IR::Opnd *value, *index, *arr;
  4581. eaInstr1 = linkOpnd->GetStackSym()->m_instrDef;
  4582. value = eaInstr1->GetSrc1();
  4583. linkOpnd = eaInstr1->GetSrc2();
  4584. eaInstr2 = linkOpnd->GetStackSym()->m_instrDef;
  4585. index = eaInstr2->GetSrc1();
  4586. linkOpnd = eaInstr2->GetSrc2();
  4587. eaInstr3 = linkOpnd->GetStackSym()->m_instrDef;
  4588. Assert(!eaInstr3->GetSrc2()); // end of args list
  4589. arr = eaInstr3->GetSrc1();
  4590. indirOpnd = IR::IndirOpnd::New(arr->AsRegOpnd(), index->AsRegOpnd(), TyVar, callInstr->m_func);
  4591. callInstr->SetDst(indirOpnd);
  4592. callInstr->ReplaceSrc1(value);
  4593. // remove ea instructions
  4594. eaInstr1->Remove(); eaInstr2->Remove(); eaInstr3->Remove();
  4595. }
  4596. }