Inline.cpp 263 KB

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