IRBuilder.cpp 277 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647
  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. #include "Library/ForInObjectEnumerator.h"
  7. #pragma prefast(push)
  8. #pragma prefast(disable:28652, "Prefast complains that the OR are causing the compiler to emit dynamic initializers and the variable to be allocated in read/write mem...")
  9. static const IR::BailOutKind c_debuggerBailOutKindForCall =
  10. IR::BailOutForceByFlag | IR::BailOutStackFrameBase | IR::BailOutBreakPointInFunction | IR::BailOutLocalValueChanged | IR::BailOutIgnoreException | IR::BailOutStep;
  11. static const IR::BailOutKind c_debuggerBaseBailOutKindForHelper = IR::BailOutIgnoreException | IR::BailOutForceByFlag;
  12. #pragma prefast(pop)
  13. uint
  14. IRBuilder::AddStatementBoundary(uint statementIndex, uint offset)
  15. {
  16. // Under debugger we use full stmt map, so that we know exactly start and end of each user stmt.
  17. // We insert additional instrs in between statements, such as ProfiledLoopStart, for these bytecode reader acts as
  18. // there is "unknown" stmt boundary with statementIndex == -1. Don't add stmt boundary for that as later
  19. // it may cause issues, e.g. see WinBlue 218307.
  20. if (!(statementIndex == Js::Constants::NoStatementIndex && this->m_func->IsJitInDebugMode()))
  21. {
  22. IR::PragmaInstr* pragmaInstr = IR::PragmaInstr::New(Js::OpCode::StatementBoundary, statementIndex, m_func);
  23. this->AddInstr(pragmaInstr, offset);
  24. }
  25. #ifdef BAILOUT_INJECTION
  26. if (!this->m_func->IsOOPJIT())
  27. {
  28. // Don't inject bailout if the function have trys
  29. if (!this->m_func->GetTopFunc()->HasTry() && (statementIndex != Js::Constants::NoStatementIndex))
  30. {
  31. if (Js::Configuration::Global.flags.IsEnabled(Js::BailOutFlag) && !this->m_func->GetJITFunctionBody()->IsLibraryCode())
  32. {
  33. ULONG line;
  34. LONG col;
  35. // Since we're on a separate thread, don't allow the line cache to be allocated in the Recycler.
  36. if (((Js::FunctionBody*)m_func->GetJITFunctionBody()->GetAddr())->GetLineCharOffset(this->m_jnReader.GetCurrentOffset(), &line, &col, false /*canAllocateLineCache*/))
  37. {
  38. line++;
  39. if (Js::Configuration::Global.flags.BailOut.Contains(line, (uint32)col) || Js::Configuration::Global.flags.BailOut.Contains(line, (uint32)-1))
  40. {
  41. this->InjectBailOut(offset);
  42. }
  43. }
  44. }
  45. else if (Js::Configuration::Global.flags.IsEnabled(Js::BailOutAtEveryLineFlag))
  46. {
  47. this->InjectBailOut(offset);
  48. }
  49. }
  50. }
  51. #endif
  52. return m_statementReader.MoveNextStatementBoundary();
  53. }
  54. // Add conditional bailout for breaking into interpreter debug thunk - for fast F12.
  55. void
  56. IRBuilder::InsertBailOutForDebugger(uint byteCodeOffset, IR::BailOutKind kind, IR::Instr* insertBeforeInstr /* default nullptr */)
  57. {
  58. Assert(m_func->IsJitInDebugMode());
  59. Assert(byteCodeOffset != Js::Constants::NoByteCodeOffset);
  60. BailOutInfo * bailOutInfo = JitAnew(m_func->m_alloc, BailOutInfo, byteCodeOffset, m_func);
  61. IR::BailOutInstr * instr = IR::BailOutInstr::New(Js::OpCode::BailForDebugger, kind, bailOutInfo, bailOutInfo->bailOutFunc);
  62. if (insertBeforeInstr)
  63. {
  64. InsertInstr(instr, insertBeforeInstr);
  65. }
  66. else
  67. {
  68. this->AddInstr(instr, m_lastInstr->GetByteCodeOffset());
  69. }
  70. }
  71. bool
  72. IRBuilder::DoBailOnNoProfile()
  73. {
  74. if (PHASE_OFF(Js::BailOnNoProfilePhase, this->m_func->GetTopFunc()))
  75. {
  76. return false;
  77. }
  78. Func *const topFunc = m_func->GetTopFunc();
  79. if(topFunc->GetWorkItem()->GetProfiledIterations() == 0)
  80. {
  81. // The top function has not been profiled yet. Some switch must have been used to force jitting. This is not a
  82. // real-world case, but for the purpose of testing the JIT, it's beneficial to generate code in unprofiled paths.
  83. return false;
  84. }
  85. if (m_func->HasProfileInfo() && m_func->GetReadOnlyProfileInfo()->IsNoProfileBailoutsDisabled())
  86. {
  87. return false;
  88. }
  89. if (!m_func->DoGlobOpt())
  90. {
  91. return false;
  92. }
  93. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  94. if (this->m_func->GetTopFunc() != this->m_func && Js::Configuration::Global.flags.IsEnabled(Js::ForceJITLoopBodyFlag))
  95. {
  96. // No profile data for loop bodies with -force...
  97. return false;
  98. }
  99. #endif
  100. if (!this->m_func->HasProfileInfo())
  101. {
  102. return false;
  103. }
  104. return true;
  105. }
  106. void
  107. IRBuilder::InsertBailOnNoProfile(uint offset)
  108. {
  109. Assert(DoBailOnNoProfile());
  110. if (this->callTreeHasSomeProfileInfo)
  111. {
  112. return;
  113. }
  114. IR::Instr *startCall = nullptr;
  115. int count = 0;
  116. FOREACH_SLIST_ENTRY(IR::Instr *, argInstr, this->m_argStack)
  117. {
  118. if (argInstr->m_opcode == Js::OpCode::StartCall)
  119. {
  120. startCall = argInstr;
  121. count++;
  122. if (count > 1)
  123. {
  124. return;
  125. }
  126. }
  127. } NEXT_SLIST_ENTRY;
  128. AnalysisAssert(startCall);
  129. if (startCall->m_prev->m_opcode != Js::OpCode::BailOnNoProfile)
  130. {
  131. InsertBailOnNoProfile(startCall);
  132. }
  133. }
  134. void IRBuilder::InsertBailOnNoProfile(IR::Instr *const insertBeforeInstr)
  135. {
  136. Assert(DoBailOnNoProfile());
  137. IR::Instr *const bailOnNoProfileInstr = IR::Instr::New(Js::OpCode::BailOnNoProfile, m_func);
  138. InsertInstr(bailOnNoProfileInstr, insertBeforeInstr);
  139. }
  140. #ifdef BAILOUT_INJECTION
  141. void
  142. IRBuilder::InjectBailOut(uint offset)
  143. {
  144. if(m_func->IsSimpleJit())
  145. {
  146. return; // bailout injection is only applicable to full JIT
  147. }
  148. IR::IntConstOpnd * opnd = IR::IntConstOpnd::New(0, TyInt32, m_func);
  149. uint bailOutOffset = offset;
  150. if (bailOutOffset == Js::Constants::NoByteCodeOffset)
  151. {
  152. bailOutOffset = m_lastInstr->GetByteCodeOffset();
  153. }
  154. BailOutInfo * bailOutInfo = JitAnew(m_func->m_alloc, BailOutInfo, bailOutOffset, m_func);
  155. IR::BailOutInstr * instr = IR::BailOutInstr::New(Js::OpCode::BailOnEqual, IR::BailOutInjected, bailOutInfo, bailOutInfo->bailOutFunc);
  156. instr->SetSrc1(opnd);
  157. instr->SetSrc2(opnd);
  158. this->AddInstr(instr, offset);
  159. }
  160. void
  161. IRBuilder::CheckBailOutInjection(Js::OpCode opcode)
  162. {
  163. // Detect these sequence and disable Bailout injection between them:
  164. // LdStackArgPtr
  165. // LdArgCnt
  166. // ApplyArgs
  167. // This assumes that LdStackArgPtr, LdArgCnt and ApplyArgs can
  168. // only be emitted in these sequence. This will need to be modified if it changes.
  169. //
  170. // Also insert a single bailout before the beginning of a switch case block for all the case labels.
  171. switch(opcode)
  172. {
  173. case Js::OpCode::LdStackArgPtr:
  174. Assert(!seenLdStackArgPtr);
  175. Assert(!expectApplyArg);
  176. seenLdStackArgPtr = true;
  177. break;
  178. case Js::OpCode::LdArgCnt:
  179. Assert(seenLdStackArgPtr);
  180. Assert(!expectApplyArg);
  181. expectApplyArg = true;
  182. break;
  183. case Js::OpCode::ApplyArgs:
  184. Assert(seenLdStackArgPtr);
  185. Assert(expectApplyArg);
  186. seenLdStackArgPtr = false;
  187. expectApplyArg = false;
  188. break;
  189. case Js::OpCode::BeginSwitch:
  190. case Js::OpCode::ProfiledBeginSwitch:
  191. Assert(!seenProfiledBeginSwitch);
  192. seenProfiledBeginSwitch = true;
  193. break;
  194. case Js::OpCode::EndSwitch:
  195. Assert(seenProfiledBeginSwitch);
  196. seenProfiledBeginSwitch = false;
  197. break;
  198. default:
  199. Assert(!seenLdStackArgPtr);
  200. Assert(!expectApplyArg);
  201. break;
  202. }
  203. }
  204. #endif
  205. bool
  206. IRBuilder::IsLoopBody() const
  207. {
  208. return m_func->IsLoopBody();
  209. }
  210. bool
  211. IRBuilder::IsLoopBodyInTry() const
  212. {
  213. return m_func->IsLoopBodyInTry();
  214. }
  215. bool
  216. IRBuilder::IsLoopBodyReturnIPInstr(IR::Instr * instr) const
  217. {
  218. IR::Opnd * dst = instr->GetDst();
  219. return (dst && dst->IsRegOpnd() && dst->AsRegOpnd()->m_sym == m_loopBodyRetIPSym);
  220. }
  221. bool
  222. IRBuilder::IsLoopBodyOuterOffset(uint offset) const
  223. {
  224. if (!IsLoopBody())
  225. {
  226. return false;
  227. }
  228. return (offset >= m_func->m_workItem->GetLoopHeader()->endOffset || offset < m_func->m_workItem->GetLoopHeader()->startOffset);
  229. }
  230. uint
  231. IRBuilder::GetLoopBodyExitInstrOffset() const
  232. {
  233. // End of loop body, start of StSlot and Ret instruction at endOffset + 1
  234. return m_func->m_workItem->GetLoopHeader()->endOffset + 1;
  235. }
  236. Js::RegSlot
  237. IRBuilder::GetEnvReg() const
  238. {
  239. return m_func->GetJITFunctionBody()->GetEnvReg();
  240. }
  241. Js::RegSlot
  242. IRBuilder::GetEnvRegForInnerFrameDisplay() const
  243. {
  244. Js::RegSlot envReg = m_func->GetJITFunctionBody()->GetLocalFrameDisplayReg();
  245. if (envReg == Js::Constants::NoRegister)
  246. {
  247. envReg = this->GetEnvReg();
  248. }
  249. return envReg;
  250. }
  251. void
  252. IRBuilder::AddEnvOpndForInnerFrameDisplay(IR::Instr *instr, uint offset)
  253. {
  254. Js::RegSlot envReg = this->GetEnvRegForInnerFrameDisplay();
  255. if (envReg != Js::Constants::NoRegister)
  256. {
  257. IR::RegOpnd *src2Opnd;
  258. if (envReg == m_func->GetJITFunctionBody()->GetLocalFrameDisplayReg() &&
  259. m_func->DoStackFrameDisplay() &&
  260. m_func->IsTopFunc())
  261. {
  262. src2Opnd = IR::RegOpnd::New(TyVar, m_func);
  263. this->AddInstr(
  264. IR::Instr::New(
  265. Js::OpCode::LdSlot, src2Opnd,
  266. this->BuildFieldOpnd(Js::OpCode::LdSlot, m_func->GetLocalFrameDisplaySym()->m_id, 0, (Js::PropertyIdIndexType)-1, PropertyKindSlots),
  267. m_func),
  268. offset);
  269. }
  270. else
  271. {
  272. src2Opnd = this->BuildSrcOpnd(envReg);
  273. }
  274. instr->SetSrc2(src2Opnd);
  275. }
  276. }
  277. ///----------------------------------------------------------------------------
  278. ///
  279. /// IRBuilder::Build
  280. ///
  281. /// IRBuilder main entry point. Read the bytecode for this function and
  282. /// generate IR.
  283. ///
  284. ///----------------------------------------------------------------------------
  285. void
  286. IRBuilder::Build()
  287. {
  288. m_funcAlloc = m_func->m_alloc;
  289. NoRecoverMemoryJitArenaAllocator localAlloc(_u("BE-IRBuilder"), m_funcAlloc->GetPageAllocator(), Js::Throw::OutOfMemory);
  290. m_tempAlloc = &localAlloc;
  291. uint32 offset;
  292. uint32 statementIndex = m_statementReader.GetStatementIndex();
  293. m_argStack = JitAnew(m_tempAlloc, SList<IR::Instr *>, m_tempAlloc);
  294. this->branchRelocList = JitAnew(m_tempAlloc, SList<BranchReloc *>, m_tempAlloc);
  295. Func * topFunc = this->m_func->GetTopFunc();
  296. if (topFunc->HasTry() &&
  297. ((!topFunc->IsLoopBody() && !PHASE_OFF(Js::OptimizeTryCatchPhase, topFunc)) ||
  298. (topFunc->HasFinally() && !topFunc->IsLoopBody() && !PHASE_OFF(Js::OptimizeTryFinallyPhase, topFunc)) ||
  299. (topFunc->IsSimpleJit() && topFunc->GetJITFunctionBody()->DoJITLoopBody()) || // should be relaxed as more bailouts are added in Simple Jit
  300. topFunc->IsLoopBodyInTryFinally())) // We need accurate flow when we are full jitting loop bodies which have try finally
  301. {
  302. this->handlerOffsetStack = JitAnew(m_tempAlloc, SList<handlerStackElementType>, m_tempAlloc);
  303. }
  304. this->firstTemp = m_func->GetJITFunctionBody()->GetFirstTmpReg();
  305. Js::RegSlot tempCount = m_func->GetJITFunctionBody()->GetTempCount();
  306. if (tempCount > 0)
  307. {
  308. this->tempMap = AnewArrayZ(m_tempAlloc, SymID, tempCount);
  309. this->fbvTempUsed = BVFixed::New<JitArenaAllocator>(tempCount, m_tempAlloc);
  310. }
  311. else
  312. {
  313. this->tempMap = nullptr;
  314. this->fbvTempUsed = nullptr;
  315. }
  316. m_func->m_headInstr = IR::EntryInstr::New(Js::OpCode::FunctionEntry, m_func);
  317. m_func->m_exitInstr = IR::ExitInstr::New(Js::OpCode::FunctionExit, m_func);
  318. m_func->m_tailInstr = m_func->m_exitInstr;
  319. m_func->m_headInstr->InsertAfter(m_func->m_tailInstr);
  320. if (m_func->GetJITFunctionBody()->IsParamAndBodyScopeMerged() || this->IsLoopBody())
  321. {
  322. this->SetParamScopeDone();
  323. }
  324. if (m_func->GetJITFunctionBody()->GetLocalClosureReg() != Js::Constants::NoRegister)
  325. {
  326. m_func->InitLocalClosureSyms();
  327. }
  328. m_functionStartOffset = m_jnReader.GetCurrentOffset();
  329. m_lastInstr = m_func->m_headInstr;
  330. AssertMsg(sizeof(SymID) >= sizeof(Js::RegSlot), "sizeof(SymID) != sizeof(Js::RegSlot)!!");
  331. // Skip the last EndOfBlock opcode
  332. Assert(!OpCodeAttr::HasMultiSizeLayout(Js::OpCode::EndOfBlock));
  333. uint32 lastOffset = m_func->GetJITFunctionBody()->GetByteCodeLength() - Js::OpCodeUtil::EncodedSize(Js::OpCode::EndOfBlock, Js::SmallLayout);
  334. uint32 offsetToInstructionCount = lastOffset;
  335. if (this->IsLoopBody())
  336. {
  337. // LdSlot needs to cover all the register, including the temps, because we might treat
  338. // those as if they are local for the value of the with statement
  339. this->m_ldSlots = BVFixed::New<JitArenaAllocator>(m_func->GetJITFunctionBody()->GetLocalsCount(), m_tempAlloc);
  340. this->m_stSlots = BVFixed::New<JitArenaAllocator>(m_func->GetJITFunctionBody()->GetFirstTmpReg(), m_tempAlloc);
  341. this->m_loopBodyRetIPSym = StackSym::New(TyMachReg, this->m_func);
  342. #if DBG
  343. if (m_func->GetJITFunctionBody()->GetTempCount() != 0)
  344. {
  345. this->m_usedAsTemp = BVFixed::New<JitArenaAllocator>(m_func->GetJITFunctionBody()->GetTempCount(), m_tempAlloc);
  346. }
  347. #endif
  348. lastOffset = m_func->m_workItem->GetLoopHeader()->endOffset;
  349. AssertOrFailFast(lastOffset < m_func->GetJITFunctionBody()->GetByteCodeLength());
  350. // Ret is created at lastOffset + 1, so we need lastOffset + 2 entries
  351. offsetToInstructionCount = lastOffset + 2;
  352. // Compute the offset of the start of the locals array as a Var index.
  353. size_t localsOffset = Js::InterpreterStackFrame::GetOffsetOfLocals();
  354. Assert(localsOffset % sizeof(Js::Var) == 0);
  355. this->m_loopBodyLocalsStartSlot = (Js::PropertyId)(localsOffset / sizeof(Js::Var));
  356. }
  357. m_offsetToInstructionCount = offsetToInstructionCount;
  358. m_offsetToInstruction = JitAnewArrayZ(m_tempAlloc, IR::Instr *, offsetToInstructionCount);
  359. #ifdef BYTECODE_BRANCH_ISLAND
  360. longBranchMap = JitAnew(m_tempAlloc, LongBranchMap, m_tempAlloc);
  361. #endif
  362. m_switchBuilder.Init(m_func, m_tempAlloc, false);
  363. this->LoadNativeCodeData();
  364. this->BuildConstantLoads();
  365. this->BuildGeneratorPreamble();
  366. if (!this->IsLoopBody() && m_func->GetJITFunctionBody()->HasImplicitArgIns())
  367. {
  368. this->BuildImplicitArgIns();
  369. }
  370. if (!this->IsLoopBody() && m_func->GetJITFunctionBody()->HasRestParameter())
  371. {
  372. this->BuildArgInRest();
  373. }
  374. if (m_func->IsJitInDebugMode())
  375. {
  376. // This is first bailout in the function, the locals at stack have not initialized to undefined, so do not restore them.
  377. this->InsertBailOutForDebugger(m_functionStartOffset, IR::BailOutForceByFlag | IR::BailOutBreakPointInFunction | IR::BailOutStep, nullptr);
  378. }
  379. #ifdef BAILOUT_INJECTION
  380. // Start bailout inject after the constant and arg load. We don't bailout before that
  381. IR::Instr * lastInstr = m_lastInstr;
  382. #endif
  383. offset = Js::Constants::NoByteCodeOffset;
  384. if (!this->IsLoopBody())
  385. {
  386. IR::Instr *instr;
  387. // Do the implicit operations LdEnv, NewScopeSlots, LdFrameDisplay, as indicated by function body attributes.
  388. Js::RegSlot envReg = m_func->GetJITFunctionBody()->GetEnvReg();
  389. if (envReg != Js::Constants::NoRegister && !this->RegIsConstant(envReg))
  390. {
  391. Js::OpCode newOpcode;
  392. Js::RegSlot thisReg = m_func->GetJITFunctionBody()->GetThisRegForEventHandler();
  393. IR::RegOpnd *srcOpnd = nullptr;
  394. IR::RegOpnd *dstOpnd = nullptr;
  395. if (thisReg != Js::Constants::NoRegister)
  396. {
  397. this->BuildArgIn0(offset, thisReg);
  398. srcOpnd = BuildSrcOpnd(thisReg);
  399. newOpcode = Js::OpCode::LdHandlerScope;
  400. }
  401. else
  402. {
  403. newOpcode = Js::OpCode::LdEnv;
  404. }
  405. dstOpnd = BuildDstOpnd(envReg);
  406. instr = IR::Instr::New(newOpcode, dstOpnd, m_func);
  407. if (srcOpnd)
  408. {
  409. instr->SetSrc1(srcOpnd);
  410. }
  411. if (dstOpnd->m_sym->m_isSingleDef)
  412. {
  413. dstOpnd->m_sym->m_isNotNumber = true;
  414. }
  415. this->AddInstr(instr, offset);
  416. }
  417. Js::RegSlot funcExprScopeReg = m_func->GetJITFunctionBody()->GetFuncExprScopeReg();
  418. IR::RegOpnd *frameDisplayOpnd = nullptr;
  419. if (funcExprScopeReg != Js::Constants::NoRegister)
  420. {
  421. IR::RegOpnd *funcExprScopeOpnd = BuildDstOpnd(funcExprScopeReg);
  422. instr = IR::Instr::New(Js::OpCode::NewPseudoScope, funcExprScopeOpnd, m_func);
  423. this->AddInstr(instr, offset);
  424. }
  425. Js::RegSlot closureReg = m_func->GetJITFunctionBody()->GetLocalClosureReg();
  426. IR::RegOpnd *closureOpnd = nullptr;
  427. if (closureReg != Js::Constants::NoRegister)
  428. {
  429. Assert(!this->RegIsConstant(closureReg));
  430. if (m_func->DoStackScopeSlots())
  431. {
  432. closureOpnd = IR::RegOpnd::New(TyVar, m_func);
  433. }
  434. else
  435. {
  436. closureOpnd = this->BuildDstOpnd(closureReg);
  437. }
  438. if (m_func->GetJITFunctionBody()->HasScopeObject())
  439. {
  440. if (m_func->GetJITFunctionBody()->HasCachedScopePropIds())
  441. {
  442. this->BuildInitCachedScope(0, offset);
  443. }
  444. else
  445. {
  446. instr = IR::Instr::New(Js::OpCode::NewScopeObject, closureOpnd, m_func);
  447. this->AddInstr(instr, offset);
  448. }
  449. }
  450. else
  451. {
  452. Js::OpCode op =
  453. m_func->DoStackScopeSlots() ? Js::OpCode::NewStackScopeSlots : Js::OpCode::NewScopeSlots;
  454. uint size = m_func->GetJITFunctionBody()->IsParamAndBodyScopeMerged() ? m_func->GetJITFunctionBody()->GetScopeSlotArraySize() : m_func->GetJITFunctionBody()->GetParamScopeSlotArraySize();
  455. IR::Opnd * srcOpnd = IR::IntConstOpnd::New(size + Js::ScopeSlots::FirstSlotIndex, TyUint32, m_func);
  456. instr = IR::Instr::New(op, closureOpnd, srcOpnd, m_func);
  457. this->AddInstr(instr, offset);
  458. }
  459. if (closureOpnd->m_sym->m_isSingleDef)
  460. {
  461. closureOpnd->m_sym->m_isNotNumber = true;
  462. }
  463. if (m_func->DoStackScopeSlots())
  464. {
  465. // Init the stack closure sym and use it to save the scope slot pointer.
  466. this->AddInstr(
  467. IR::Instr::New(
  468. Js::OpCode::InitLocalClosure, this->BuildDstOpnd(m_func->GetLocalClosureSym()->m_id), m_func),
  469. offset);
  470. this->AddInstr(
  471. IR::Instr::New(
  472. Js::OpCode::StSlot,
  473. this->BuildFieldOpnd(
  474. Js::OpCode::StSlot, m_func->GetLocalClosureSym()->m_id, 0, (Js::PropertyIdIndexType)-1, PropertyKindSlots),
  475. closureOpnd, m_func),
  476. offset);
  477. }
  478. }
  479. Js::RegSlot frameDisplayReg = m_func->GetJITFunctionBody()->GetLocalFrameDisplayReg();
  480. if (frameDisplayReg != Js::Constants::NoRegister)
  481. {
  482. Assert(!this->RegIsConstant(frameDisplayReg));
  483. Js::OpCode op = m_func->DoStackScopeSlots() ? Js::OpCode::NewStackFrameDisplay : Js::OpCode::LdFrameDisplay;
  484. if (funcExprScopeReg != Js::Constants::NoRegister)
  485. {
  486. // Insert the function expression scope ahead of any enclosing scopes.
  487. IR::RegOpnd * funcExprScopeOpnd = BuildSrcOpnd(funcExprScopeReg);
  488. frameDisplayOpnd = closureReg != Js::Constants::NoRegister ? IR::RegOpnd::New(TyVar, m_func) : BuildDstOpnd(frameDisplayReg);
  489. instr = IR::Instr::New(Js::OpCode::LdFrameDisplay, frameDisplayOpnd, funcExprScopeOpnd, m_func);
  490. if (envReg != Js::Constants::NoRegister)
  491. {
  492. instr->SetSrc2(this->BuildSrcOpnd(envReg));
  493. }
  494. this->AddInstr(instr, (uint)-1);
  495. }
  496. if (closureReg != Js::Constants::NoRegister)
  497. {
  498. IR::RegOpnd *dstOpnd;
  499. if (m_func->DoStackScopeSlots() && m_func->IsTopFunc())
  500. {
  501. dstOpnd = IR::RegOpnd::New(TyVar, m_func);
  502. }
  503. else
  504. {
  505. dstOpnd = this->BuildDstOpnd(frameDisplayReg);
  506. }
  507. instr = IR::Instr::New(op, dstOpnd, closureOpnd, m_func);
  508. if (frameDisplayOpnd != nullptr)
  509. {
  510. // We're building on an intermediate LdFrameDisplay result.
  511. instr->SetSrc2(frameDisplayOpnd);
  512. }
  513. else if (envReg != Js::Constants::NoRegister)
  514. {
  515. // We're building on the environment created by the enclosing function.
  516. instr->SetSrc2(this->BuildSrcOpnd(envReg));
  517. }
  518. this->AddInstr(instr, offset);
  519. if (dstOpnd->m_sym->m_isSingleDef)
  520. {
  521. dstOpnd->m_sym->m_isNotNumber = true;
  522. }
  523. if (m_func->DoStackFrameDisplay())
  524. {
  525. // Use the stack closure sym to save the frame display pointer.
  526. this->AddInstr(
  527. IR::Instr::New(
  528. Js::OpCode::InitLocalClosure, this->BuildDstOpnd(m_func->GetLocalFrameDisplaySym()->m_id), m_func),
  529. offset);
  530. this->AddInstr(
  531. IR::Instr::New(
  532. Js::OpCode::StSlot,
  533. this->BuildFieldOpnd(Js::OpCode::StSlot, m_func->GetLocalFrameDisplaySym()->m_id, 0, (Js::PropertyIdIndexType)-1, PropertyKindSlots),
  534. dstOpnd, m_func),
  535. offset);
  536. }
  537. }
  538. }
  539. }
  540. offset = m_functionStartOffset;
  541. if (m_statementReader.AtStatementBoundary(&m_jnReader))
  542. {
  543. statementIndex = this->AddStatementBoundary(statementIndex, offset);
  544. }
  545. // For label instr we can add bailout only after all labels were finalized. Put to list/add in the end.
  546. JsUtil::BaseDictionary<IR::Instr*, int, JitArenaAllocator> ignoreExBranchInstrToOffsetMap(m_tempAlloc);
  547. Js::LayoutSize layoutSize;
  548. IR::Instr* lastProcessedInstrForJITLoopBody = m_func->m_headInstr;
  549. for (Js::OpCode newOpcode = m_jnReader.ReadOp(layoutSize); (uint)m_jnReader.GetCurrentOffset() <= lastOffset; newOpcode = m_jnReader.ReadOp(layoutSize))
  550. {
  551. Assert(newOpcode != Js::OpCode::EndOfBlock);
  552. #ifdef BAILOUT_INJECTION
  553. if (!this->m_func->GetTopFunc()->HasTry()
  554. #ifdef BYTECODE_BRANCH_ISLAND
  555. && newOpcode != Js::OpCode::BrLong // Don't inject bailout on BrLong as they are just redirecting to a different offset anyways
  556. #endif
  557. )
  558. {
  559. if (!this->m_func->IsOOPJIT())
  560. {
  561. if (!seenLdStackArgPtr && !seenProfiledBeginSwitch)
  562. {
  563. if (Js::Configuration::Global.flags.IsEnabled(Js::BailOutByteCodeFlag))
  564. {
  565. ThreadContext * threadContext = this->m_func->GetScriptContext()->GetThreadContext();
  566. if (Js::Configuration::Global.flags.BailOutByteCode.Contains(threadContext->bailOutByteCodeLocationCount))
  567. {
  568. this->InjectBailOut(offset);
  569. }
  570. }
  571. else if (Js::Configuration::Global.flags.IsEnabled(Js::BailOutAtEveryByteCodeFlag))
  572. {
  573. this->InjectBailOut(offset);
  574. }
  575. }
  576. CheckBailOutInjection(newOpcode);
  577. }
  578. }
  579. #endif
  580. AssertOrFailFastMsg(Js::OpCodeUtil::IsValidByteCodeOpcode(newOpcode), "Error getting opcode from m_jnReader.Op()");
  581. uint layoutAndSize = layoutSize * Js::OpLayoutType::Count + Js::OpCodeUtil::GetOpCodeLayout(newOpcode);
  582. switch(layoutAndSize)
  583. {
  584. #define LAYOUT_TYPE(layout) \
  585. case Js::OpLayoutType::layout: \
  586. Assert(layoutSize == Js::SmallLayout); \
  587. this->Build##layout(newOpcode, offset); \
  588. break;
  589. #define LAYOUT_TYPE_WMS(layout) \
  590. case Js::SmallLayout * Js::OpLayoutType::Count + Js::OpLayoutType::layout: \
  591. this->Build##layout<Js::SmallLayoutSizePolicy>(newOpcode, offset); \
  592. break; \
  593. case Js::MediumLayout * Js::OpLayoutType::Count + Js::OpLayoutType::layout: \
  594. this->Build##layout<Js::MediumLayoutSizePolicy>(newOpcode, offset); \
  595. break; \
  596. case Js::LargeLayout * Js::OpLayoutType::Count + Js::OpLayoutType::layout: \
  597. this->Build##layout<Js::LargeLayoutSizePolicy>(newOpcode, offset); \
  598. break;
  599. #include "ByteCode/LayoutTypes.h"
  600. default:
  601. AssertMsg(0, "Unimplemented layout");
  602. break;
  603. }
  604. #ifdef BAILOUT_INJECTION
  605. if (!this->m_func->IsOOPJIT())
  606. {
  607. if (!this->m_func->GetTopFunc()->HasTry() && Js::Configuration::Global.flags.IsEnabled(Js::BailOutByteCodeFlag))
  608. {
  609. ThreadContext * threadContext = this->m_func->GetScriptContext()->GetThreadContext();
  610. if (lastInstr != m_lastInstr)
  611. {
  612. lastInstr = lastInstr->GetNextRealInstr();
  613. if (lastInstr->HasBailOutInfo())
  614. {
  615. lastInstr = lastInstr->m_next;
  616. }
  617. lastInstr->bailOutByteCodeLocation = threadContext->bailOutByteCodeLocationCount;
  618. lastInstr = m_lastInstr;
  619. }
  620. threadContext->bailOutByteCodeLocationCount++;
  621. }
  622. }
  623. #endif
  624. if (IsLoopBodyInTry() && lastProcessedInstrForJITLoopBody != m_lastInstr)
  625. {
  626. // traverse in backward so we get new/later value of given symId for storing instead of the earlier/stale
  627. // symId value. m_stSlots is used to prevent multiple stores to the same symId.
  628. FOREACH_INSTR_BACKWARD_EDITING_IN_RANGE(
  629. instr,
  630. instrPrev,
  631. m_lastInstr,
  632. lastProcessedInstrForJITLoopBody->m_next)
  633. {
  634. if (instr->GetDst() && instr->GetDst()->IsRegOpnd() && instr->GetDst()->GetStackSym()->HasByteCodeRegSlot())
  635. {
  636. StackSym * dstSym = instr->GetDst()->GetStackSym();
  637. Js::RegSlot dstRegSlot = dstSym->GetByteCodeRegSlot();
  638. if (!this->RegIsTemp(dstRegSlot) && !this->RegIsConstant(dstRegSlot))
  639. {
  640. SymID symId = dstSym->m_id;
  641. if (this->m_stSlots->Test(symId))
  642. {
  643. // For jitted loop bodies that are in a try block, we consider any symbol that has a
  644. // non-temp bytecode reg slot, to be write-through. Hence, generating StSlots at all
  645. // defs for such symbols
  646. IR::Instr * stSlot = this->GenerateLoopBodyStSlot(dstRegSlot);
  647. AddInstr(stSlot, Js::Constants::NoByteCodeOffset);
  648. this->m_stSlots->Clear(symId);
  649. }
  650. else
  651. {
  652. Assert(dstSym->m_isCatchObjectSym);
  653. }
  654. }
  655. }
  656. } NEXT_INSTR_BACKWARD_EDITING_IN_RANGE;
  657. lastProcessedInstrForJITLoopBody = m_lastInstr;
  658. }
  659. offset = m_jnReader.GetCurrentOffset();
  660. if (m_func->IsJitInDebugMode())
  661. {
  662. bool needBailoutForHelper = CONFIG_FLAG(EnableContinueAfterExceptionWrappersForHelpers) &&
  663. (OpCodeAttr::NeedsPostOpDbgBailOut(newOpcode) ||
  664. (m_lastInstr->m_opcode == Js::OpCode::CallHelper && m_lastInstr->GetSrc1() &&
  665. HelperMethodAttributes::CanThrow(m_lastInstr->GetSrc1()->AsHelperCallOpnd()->m_fnHelper)));
  666. if (needBailoutForHelper)
  667. {
  668. // Insert bailout after return from a helper call.
  669. // For now use offset of next instr, when we get & ignore exception, we replace this with next statement offset.
  670. if (m_lastInstr->IsBranchInstr())
  671. {
  672. // Debugger bailout on branches goes to different block which can become dead. Keep bailout with real instr.
  673. // Can't convert to bailout at this time, can do that only after branches are finalized, remember for later.
  674. ignoreExBranchInstrToOffsetMap.Add(m_lastInstr, offset);
  675. }
  676. else if (
  677. m_lastInstr->m_opcode == Js::OpCode::Throw ||
  678. m_lastInstr->m_opcode == Js::OpCode::RuntimeReferenceError ||
  679. m_lastInstr->m_opcode == Js::OpCode::RuntimeTypeError)
  680. {
  681. uint32 lastInstrOffset = m_lastInstr->GetByteCodeOffset();
  682. AssertOrFailFast(lastInstrOffset < m_offsetToInstructionCount);
  683. #if DBG
  684. __analysis_assume(lastInstrOffset < this->m_offsetToInstructionCount);
  685. #endif
  686. bool isLastInstrUpdateNeeded = m_offsetToInstruction[lastInstrOffset] == m_lastInstr;
  687. BailOutInfo * bailOutInfo = JitAnew(this->m_func->m_alloc, BailOutInfo, offset, this->m_func);
  688. m_lastInstr = m_lastInstr->ConvertToBailOutInstr(bailOutInfo, c_debuggerBaseBailOutKindForHelper, true);
  689. if (isLastInstrUpdateNeeded)
  690. {
  691. m_offsetToInstruction[lastInstrOffset] = m_lastInstr;
  692. }
  693. }
  694. else
  695. {
  696. IR::BailOutKind bailOutKind = c_debuggerBaseBailOutKindForHelper;
  697. if (OpCodeAttr::HasImplicitCall(newOpcode) || OpCodeAttr::OpndHasImplicitCall(newOpcode))
  698. {
  699. // When we get out of e.g. valueOf called by a helper (e.g. Add_A) during stepping,
  700. // we need to bail out to continue debugging calling function in interpreter,
  701. // essentially this is similar to bail out on return from a method.
  702. bailOutKind |= c_debuggerBailOutKindForCall;
  703. }
  704. this->InsertBailOutForDebugger(offset, bailOutKind);
  705. }
  706. }
  707. }
  708. while (m_statementReader.AtStatementBoundary(&m_jnReader))
  709. {
  710. statementIndex = this->AddStatementBoundary(statementIndex, offset);
  711. }
  712. }
  713. if (Js::Constants::NoStatementIndex != statementIndex)
  714. {
  715. // If we are inside a user statement then create a trailing line pragma instruction
  716. statementIndex = this->AddStatementBoundary(statementIndex, Js::Constants::NoByteCodeOffset);
  717. }
  718. if (IsLoopBody())
  719. {
  720. // Insert the LdSlot/StSlot and Ret
  721. IR::Opnd * retOpnd = this->InsertLoopBodyReturnIPInstr(offset, offset);
  722. // Restore and Ret are at the last offset + 1
  723. GenerateLoopBodySlotAccesses(lastOffset + 1);
  724. InsertDoneLoopBodyLoopCounter(lastOffset);
  725. IR::Instr * retInstr = IR::Instr::New(Js::OpCode::Ret, m_func);
  726. retInstr->SetSrc1(retOpnd);
  727. this->AddInstr(retInstr, lastOffset + 1);
  728. }
  729. // Now fix up the targets for all the branches we've introduced.
  730. InsertLabels();
  731. Assert(!this->handlerOffsetStack || this->handlerOffsetStack->Empty());
  732. // Insert bailout for ignore exception for labels, after all labels were finalized.
  733. ignoreExBranchInstrToOffsetMap.Map([this](IR::Instr* instr, int byteCodeOffset) {
  734. BailOutInfo * bailOutInfo = JitAnew(this->m_func->m_alloc, BailOutInfo, byteCodeOffset, this->m_func);
  735. instr->ConvertToBailOutInstr(bailOutInfo, c_debuggerBaseBailOutKindForHelper, true);
  736. });
  737. // Now that we know whether the func is a leaf or not, decide whether we'll emit fast paths.
  738. // Do this once and for all, per-func, since the source size on the ThreadContext will be
  739. // changing while we JIT.
  740. if (this->m_func->IsTopFunc())
  741. {
  742. this->m_func->SetDoFastPaths();
  743. this->EmitClosureRangeChecks();
  744. }
  745. }
  746. void
  747. IRBuilder::EmitClosureRangeChecks()
  748. {
  749. if (m_func->frameDisplayCheckTable)
  750. {
  751. // Frame display checks. Again, chain to the instruction (LdEnv/LdSlot).
  752. FOREACH_HASHTABLE_ENTRY(FrameDisplayCheckRecord*, bucket, m_func->frameDisplayCheckTable)
  753. {
  754. StackSym *stackSym = m_func->m_symTable->FindStackSym(bucket.value);
  755. Assert(stackSym && stackSym->m_instrDef);
  756. IR::Instr *instrDef = stackSym->m_instrDef;
  757. IR::Instr *insertInstr = instrDef->m_next;
  758. IR::RegOpnd *dstOpnd = instrDef->UnlinkDst()->AsRegOpnd();
  759. IR::Instr *instr = IR::Instr::New(Js::OpCode::FrameDisplayCheck, dstOpnd, m_func);
  760. dstOpnd = IR::RegOpnd::New(TyVar, m_func);
  761. instrDef->SetDst(dstOpnd);
  762. instr->SetSrc1(dstOpnd);
  763. // Attach the two-dimensional check info.
  764. IR::AddrOpnd *recordOpnd = IR::AddrOpnd::New(bucket.element, IR::AddrOpndKindDynamicMisc, m_func, true);
  765. instr->SetSrc2(recordOpnd);
  766. insertInstr->InsertBefore(instr);
  767. }
  768. NEXT_HASHTABLE_ENTRY;
  769. }
  770. // If not a loop, but there are loops and trys, restore scope slot pointer and FD
  771. if (!m_func->IsLoopBody() && m_func->HasTry() && m_func->GetJITFunctionBody()->GetByteCodeInLoopCount() != 0)
  772. {
  773. BVSparse<JitArenaAllocator> * bv = nullptr;
  774. if (m_func->GetLocalClosureSym() && m_func->GetLocalClosureSym()->HasByteCodeRegSlot())
  775. {
  776. bv = JitAnew(m_func->m_alloc, BVSparse<JitArenaAllocator>, m_func->m_alloc);
  777. bv->Set(m_func->GetLocalClosureSym()->m_id);
  778. }
  779. if (m_func->GetLocalFrameDisplaySym() && m_func->GetLocalFrameDisplaySym()->HasByteCodeRegSlot())
  780. {
  781. if (!bv)
  782. {
  783. bv = JitAnew(m_func->m_alloc, BVSparse<JitArenaAllocator>, m_func->m_alloc);
  784. }
  785. bv->Set(m_func->GetLocalFrameDisplaySym()->m_id);
  786. }
  787. if (bv)
  788. {
  789. FOREACH_INSTR_IN_FUNC_BACKWARD(instr, m_func)
  790. {
  791. if (instr->m_opcode == Js::OpCode::Ret)
  792. {
  793. IR::ByteCodeUsesInstr * byteCodeUse = IR::ByteCodeUsesInstr::New(instr);
  794. byteCodeUse->SetBV(bv);
  795. instr->InsertBefore(byteCodeUse);
  796. break;
  797. }
  798. }
  799. NEXT_INSTR_IN_FUNC_BACKWARD;
  800. }
  801. }
  802. }
  803. ///----------------------------------------------------------------------------
  804. ///
  805. /// IRBuilder::InsertLabels
  806. ///
  807. /// Insert label instructions at the offsets recorded in the branch reloc list.
  808. ///
  809. ///----------------------------------------------------------------------------
  810. void
  811. IRBuilder::InsertLabels()
  812. {
  813. AssertMsg(this->branchRelocList, "Malformed branch reloc list");
  814. SList<BranchReloc *>::Iterator iter(this->branchRelocList);
  815. while (iter.Next())
  816. {
  817. IR::LabelInstr * labelInstr = nullptr;
  818. BranchReloc * reloc = iter.Data();
  819. IR::BranchInstr * branchInstr = reloc->GetBranchInstr();
  820. uint offset = reloc->GetOffset();
  821. uint const branchOffset = reloc->GetBranchOffset();
  822. Assert(!IsLoopBody() || offset <= GetLoopBodyExitInstrOffset());
  823. if(branchInstr->m_opcode == Js::OpCode::MultiBr)
  824. {
  825. IR::MultiBranchInstr * multiBranchInstr = branchInstr->AsBranchInstr()->AsMultiBrInstr();
  826. multiBranchInstr->UpdateMultiBrTargetOffsets([&](uint32 offset) -> IR::LabelInstr *
  827. {
  828. labelInstr = this->CreateLabel(branchInstr, offset);
  829. multiBranchInstr->ChangeLabelRef(nullptr, labelInstr);
  830. return labelInstr;
  831. });
  832. }
  833. else
  834. {
  835. labelInstr = this->CreateLabel(branchInstr, offset);
  836. branchInstr->SetTarget(labelInstr);
  837. }
  838. if (!reloc->IsNotBackEdge() && branchOffset >= offset)
  839. {
  840. bool wasLoopTop = labelInstr->m_isLoopTop;
  841. labelInstr->m_isLoopTop = true;
  842. if (m_func->IsJitInDebugMode())
  843. {
  844. // Add bailout for Async Break.
  845. IR::BranchInstr* backEdgeBranchInstr = reloc->GetBranchInstr();
  846. this->InsertBailOutForDebugger(backEdgeBranchInstr->GetByteCodeOffset(), IR::BailOutForceByFlag | IR::BailOutBreakPointInFunction, backEdgeBranchInstr);
  847. }
  848. if (!wasLoopTop && m_loopCounterSym)
  849. {
  850. this->InsertIncrLoopBodyLoopCounter(labelInstr);
  851. }
  852. }
  853. }
  854. }
  855. IR::LabelInstr *
  856. IRBuilder::CreateLabel(IR::BranchInstr * branchInstr, uint& offset)
  857. {
  858. IR::LabelInstr * labelInstr;
  859. IR::Instr * targetInstr;
  860. for (;;)
  861. {
  862. AssertOrFailFast(offset < m_offsetToInstructionCount);
  863. targetInstr = this->m_offsetToInstruction[offset];
  864. if (targetInstr != nullptr)
  865. {
  866. #ifdef BYTECODE_BRANCH_ISLAND
  867. // If we have a long branch, remap it to the target offset
  868. if (targetInstr == VirtualLongBranchInstr)
  869. {
  870. offset = ResolveVirtualLongBranch(branchInstr, offset);
  871. continue;
  872. }
  873. #endif
  874. break;
  875. }
  876. offset++;
  877. }
  878. IR::Instr *instrPrev = targetInstr->m_prev;
  879. if (instrPrev)
  880. {
  881. instrPrev = targetInstr->GetPrevRealInstrOrLabel();
  882. }
  883. if (instrPrev && instrPrev->IsLabelInstr() && instrPrev->GetByteCodeOffset() == offset)
  884. {
  885. // Found an existing label at the right offset. Just reuse it.
  886. labelInstr = instrPrev->AsLabelInstr();
  887. }
  888. else
  889. {
  890. // No label at the desired offset. Create one.
  891. labelInstr = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  892. labelInstr->SetByteCodeOffset(offset);
  893. if (instrPrev)
  894. {
  895. instrPrev->InsertAfter(labelInstr);
  896. }
  897. else
  898. {
  899. targetInstr->InsertBefore(labelInstr);
  900. }
  901. }
  902. return labelInstr;
  903. }
  904. void IRBuilder::InsertInstr(IR::Instr *instr, IR::Instr* insertBeforeInstr)
  905. {
  906. AssertOrFailFast(insertBeforeInstr->GetByteCodeOffset() < m_offsetToInstructionCount);
  907. instr->SetByteCodeOffset(insertBeforeInstr);
  908. uint32 offset = insertBeforeInstr->GetByteCodeOffset();
  909. if (m_offsetToInstruction[offset] == insertBeforeInstr)
  910. {
  911. m_offsetToInstruction[offset] = instr;
  912. }
  913. insertBeforeInstr->InsertBefore(instr);
  914. #if DBG_DUMP
  915. if (PHASE_TRACE(Js::IRBuilderPhase, m_func->GetTopFunc()))
  916. {
  917. instr->Dump();
  918. }
  919. #endif
  920. }
  921. ///----------------------------------------------------------------------------
  922. ///
  923. /// IRBuilder::AddInstr
  924. ///
  925. /// Add an instruction to the current instr list. Also add this instr to
  926. /// offsetToinstruction table to patch branches/labels afterwards.
  927. ///
  928. ///----------------------------------------------------------------------------
  929. void
  930. IRBuilder::AddInstr(IR::Instr *instr, uint32 offset)
  931. {
  932. m_lastInstr->InsertAfter(instr);
  933. if (offset != Js::Constants::NoByteCodeOffset)
  934. {
  935. AssertOrFailFast(offset < m_offsetToInstructionCount);
  936. if (m_offsetToInstruction[offset] == nullptr)
  937. {
  938. m_offsetToInstruction[offset] = instr;
  939. }
  940. else
  941. {
  942. Assert(m_lastInstr->GetByteCodeOffset() == offset);
  943. }
  944. if (instr->GetByteCodeOffset() == Js::Constants::NoByteCodeOffset)
  945. {
  946. instr->SetByteCodeOffset(offset);
  947. }
  948. }
  949. else
  950. {
  951. instr->SetByteCodeOffset(m_lastInstr->GetByteCodeOffset());
  952. }
  953. m_lastInstr = instr;
  954. Func *topFunc = this->m_func->GetTopFunc();
  955. if (!topFunc->GetHasTempObjectProducingInstr())
  956. {
  957. if (OpCodeAttr::TempObjectProducing(instr->m_opcode))
  958. {
  959. topFunc->SetHasTempObjectProducingInstr(true);
  960. }
  961. }
  962. #if DBG_DUMP
  963. if (Js::Configuration::Global.flags.Trace.IsEnabled(Js::IRBuilderPhase, this->m_func->GetTopFunc()->GetSourceContextId(), this->m_func->GetTopFunc()->GetLocalFunctionId()))
  964. {
  965. instr->Dump();
  966. }
  967. #endif
  968. }
  969. IR::IndirOpnd *
  970. IRBuilder::BuildIndirOpnd(IR::RegOpnd *baseReg, IR::RegOpnd *indexReg)
  971. {
  972. IR::IndirOpnd *indirOpnd = IR::IndirOpnd::New(baseReg, indexReg, TyVar, m_func);
  973. return indirOpnd;
  974. }
  975. IR::IndirOpnd *
  976. IRBuilder::BuildIndirOpnd(IR::RegOpnd *baseReg, uint32 offset)
  977. {
  978. IR::IndirOpnd *indirOpnd = IR::IndirOpnd::New(baseReg, offset, TyVar, m_func);
  979. return indirOpnd;
  980. }
  981. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  982. IR::IndirOpnd *
  983. IRBuilder::BuildIndirOpnd(IR::RegOpnd *baseReg, uint32 offset, const char16 *desc)
  984. {
  985. IR::IndirOpnd *indirOpnd = IR::IndirOpnd::New(baseReg, offset, TyVar, desc, m_func);
  986. return indirOpnd;
  987. }
  988. #endif
  989. IR::SymOpnd *
  990. IRBuilder::BuildFieldOpnd(Js::OpCode newOpcode, Js::RegSlot reg, Js::PropertyId propertyId, Js::PropertyIdIndexType propertyIdIndex, PropertyKind propertyKind, uint inlineCacheIndex)
  991. {
  992. AssertOrFailFast(inlineCacheIndex < m_func->GetJITFunctionBody()->GetInlineCacheCount() || inlineCacheIndex == Js::Constants::NoInlineCacheIndex);
  993. PropertySym * propertySym = BuildFieldSym(reg, propertyId, propertyIdIndex, inlineCacheIndex, propertyKind);
  994. IR::SymOpnd * symOpnd;
  995. // If we plan to apply object type optimization to this instruction or if we intend to emit a fast path using an inline
  996. // cache, we will need a property sym operand.
  997. if (OpCodeAttr::FastFldInstr(newOpcode) || inlineCacheIndex != (uint)-1)
  998. {
  999. Assert(propertyKind == PropertyKindData);
  1000. symOpnd = IR::PropertySymOpnd::New(propertySym, inlineCacheIndex, TyVar, this->m_func);
  1001. if (inlineCacheIndex != (uint)-1 && propertySym->m_loadInlineCacheIndex == (uint)-1)
  1002. {
  1003. if (GlobOpt::IsPREInstrCandidateLoad(newOpcode))
  1004. {
  1005. propertySym->m_loadInlineCacheIndex = inlineCacheIndex;
  1006. propertySym->m_loadInlineCacheFunc = this->m_func;
  1007. }
  1008. }
  1009. }
  1010. else
  1011. {
  1012. symOpnd = IR::SymOpnd::New(propertySym, TyVar, this->m_func);
  1013. }
  1014. return symOpnd;
  1015. }
  1016. PropertySym *
  1017. IRBuilder::BuildFieldSym(Js::RegSlot reg, Js::PropertyId propertyId, Js::PropertyIdIndexType propertyIdIndex, uint inlineCacheIndex, PropertyKind propertyKind)
  1018. {
  1019. PropertySym * propertySym;
  1020. SymID symId = this->BuildSrcStackSymID(reg);
  1021. AssertMsg(m_func->m_symTable->FindStackSym(symId), "Tried to use an undefined stacksym?");
  1022. propertySym = PropertySym::FindOrCreate(symId, propertyId, propertyIdIndex, inlineCacheIndex, propertyKind, m_func);
  1023. return propertySym;
  1024. }
  1025. SymID
  1026. IRBuilder::BuildSrcStackSymID(Js::RegSlot regSlot)
  1027. {
  1028. SymID symID;
  1029. if (this->RegIsTemp(regSlot))
  1030. {
  1031. // This is a use of a temp. Map the reg slot to its sym ID.
  1032. // !!!NOTE: always process an instruction's temp uses before its temp defs!!!
  1033. symID = this->GetMappedTemp(regSlot);
  1034. if (symID == 0)
  1035. {
  1036. // We might have temps that are live through the loop body via "with" statement
  1037. // We need to treat those as if they are locals and don't remap them
  1038. Assert(this->IsLoopBody());
  1039. Assert(!this->m_usedAsTemp->Test(regSlot - m_func->GetJITFunctionBody()->GetFirstTmpReg()));
  1040. symID = static_cast<SymID>(regSlot);
  1041. this->SetMappedTemp(regSlot, symID);
  1042. this->EnsureLoopBodyLoadSlot(symID);
  1043. }
  1044. this->SetTempUsed(regSlot, TRUE);
  1045. }
  1046. else
  1047. {
  1048. symID = static_cast<SymID>(regSlot);
  1049. if (IsLoopBody() && !this->RegIsConstant(regSlot))
  1050. {
  1051. this->EnsureLoopBodyLoadSlot(symID);
  1052. }
  1053. }
  1054. return symID;
  1055. }
  1056. IR::RegOpnd *
  1057. IRBuilder::EnsureLoopBodyForInEnumeratorArrayOpnd()
  1058. {
  1059. Assert(this->IsLoopBody());
  1060. IR::RegOpnd * loopBodyForInEnumeratorArrayOpnd = this->m_loopBodyForInEnumeratorArrayOpnd;
  1061. if (loopBodyForInEnumeratorArrayOpnd == nullptr)
  1062. {
  1063. loopBodyForInEnumeratorArrayOpnd = IR::RegOpnd::New(TyMachPtr, this->m_func);
  1064. this->m_loopBodyForInEnumeratorArrayOpnd = loopBodyForInEnumeratorArrayOpnd;
  1065. StackSym *loopParamSym = m_func->EnsureLoopParamSym();
  1066. IR::RegOpnd *loopParamOpnd = IR::RegOpnd::New(loopParamSym, TyMachPtr, m_func);
  1067. IR::Instr * ldInstr = IR::Instr::New(Js::OpCode::Ld_A, loopBodyForInEnumeratorArrayOpnd,
  1068. IR::IndirOpnd::New(loopParamOpnd, Js::InterpreterStackFrame::GetOffsetOfForInEnumerators(), TyMachPtr, this->m_func),
  1069. this->m_func);
  1070. m_func->m_headInstr->InsertAfter(ldInstr);
  1071. }
  1072. return loopBodyForInEnumeratorArrayOpnd;
  1073. }
  1074. IR::Opnd *
  1075. IRBuilder::BuildForInEnumeratorOpnd(uint forInLoopLevel)
  1076. {
  1077. Assert(forInLoopLevel < this->m_func->GetJITFunctionBody()->GetForInLoopDepth());
  1078. if (!this->IsLoopBody())
  1079. {
  1080. StackSym *stackSym = StackSym::New(TyMisc, this->m_func);
  1081. stackSym->m_offset = forInLoopLevel;
  1082. return IR::SymOpnd::New(stackSym, TyMachPtr, this->m_func);
  1083. }
  1084. return IR::IndirOpnd::New(
  1085. EnsureLoopBodyForInEnumeratorArrayOpnd(), forInLoopLevel * sizeof(Js::ForInObjectEnumerator), TyMachPtr, this->m_func);
  1086. }
  1087. ///----------------------------------------------------------------------------
  1088. ///
  1089. /// IRBuilder::BuildSrcOpnd
  1090. ///
  1091. /// Create a StackSym and return a RegOpnd for this RegSlot.
  1092. ///
  1093. ///----------------------------------------------------------------------------
  1094. IR::RegOpnd *
  1095. IRBuilder::BuildSrcOpnd(Js::RegSlot srcRegSlot, IRType type)
  1096. {
  1097. StackSym * symSrc = m_func->m_symTable->FindStackSym(BuildSrcStackSymID(srcRegSlot));
  1098. AssertMsg(symSrc, "Tried to use an undefined stack slot?");
  1099. IR::RegOpnd *regOpnd = IR::RegOpnd::New(symSrc, type, m_func);
  1100. return regOpnd;
  1101. }
  1102. ///----------------------------------------------------------------------------
  1103. ///
  1104. /// IRBuilder::BuildDstOpnd
  1105. ///
  1106. /// Create a StackSym and return a RegOpnd for this RegSlot.
  1107. /// If the RegSlot is '0', it may have multiple defs, so use FindOrCreate.
  1108. ///
  1109. ///----------------------------------------------------------------------------
  1110. IR::RegOpnd *
  1111. IRBuilder::BuildDstOpnd(Js::RegSlot dstRegSlot, IRType type, bool isCatchObjectSym)
  1112. {
  1113. StackSym * symDst;
  1114. SymID symID;
  1115. if (this->RegIsTemp(dstRegSlot))
  1116. {
  1117. #if DBG
  1118. if (this->IsLoopBody())
  1119. {
  1120. // If we are doing loop body, and a temp reg slot is loaded via LdSlot
  1121. // That means that we have detected that the slot is live coming in to the loop.
  1122. // This would only happen for the value of a "with" statement, so there shouldn't
  1123. // be any def for those
  1124. Assert(!this->m_ldSlots->Test(dstRegSlot));
  1125. this->m_usedAsTemp->Set(dstRegSlot - m_func->GetJITFunctionBody()->GetFirstTmpReg());
  1126. }
  1127. #endif
  1128. // This is a def of a temp. Create a new sym ID for it if it's been used since its last def.
  1129. // !!!NOTE: always process an instruction's temp uses before its temp defs!!!
  1130. if (this->GetTempUsed(dstRegSlot))
  1131. {
  1132. symID = m_func->m_symTable->NewID();
  1133. this->SetTempUsed(dstRegSlot, FALSE);
  1134. this->SetMappedTemp(dstRegSlot, symID);
  1135. }
  1136. else
  1137. {
  1138. symID = this->GetMappedTemp(dstRegSlot);
  1139. // The temp hasn't been used since its last def. There are 2 possibilities:
  1140. if (symID == 0)
  1141. {
  1142. // First time we've seen the temp. Just use the number that the front end gave it.
  1143. symID = static_cast<SymID>(dstRegSlot);
  1144. this->SetMappedTemp(dstRegSlot, symID);
  1145. }
  1146. }
  1147. }
  1148. else
  1149. {
  1150. symID = static_cast<SymID>(dstRegSlot);
  1151. if (this->RegIsConstant(dstRegSlot))
  1152. {
  1153. // Don't need to track constant registers for bailout. Don't set the byte code register for constant.
  1154. dstRegSlot = Js::Constants::NoRegister;
  1155. }
  1156. else if (IsLoopBody())
  1157. {
  1158. // Loop body and not constants
  1159. this->SetLoopBodyStSlot(symID, isCatchObjectSym);
  1160. // We need to make sure that the symbols is loaded as well
  1161. // so that the sym will be defined on all path.
  1162. this->EnsureLoopBodyLoadSlot(symID, isCatchObjectSym);
  1163. }
  1164. }
  1165. symDst = StackSym::FindOrCreate(symID, dstRegSlot, m_func);
  1166. // Always reset isSafeThis to false. We'll set it to true for singleDef cases,
  1167. // but want to reset it to false if it is multi-def.
  1168. // NOTE: We could handle the multiDef if they are all safe, but it probably isn't very common.
  1169. symDst->m_isSafeThis = false;
  1170. IR::RegOpnd *regOpnd = IR::RegOpnd::New(symDst, type, m_func);
  1171. return regOpnd;
  1172. }
  1173. void
  1174. IRBuilder::BuildImplicitArgIns()
  1175. {
  1176. Js::RegSlot startReg = m_func->GetJITFunctionBody()->GetConstCount() - 1;
  1177. for (Js::ArgSlot i = 1; i < m_func->GetJITFunctionBody()->GetInParamsCount(); i++)
  1178. {
  1179. this->BuildArgIn((uint32)-1, startReg + i, i);
  1180. }
  1181. }
  1182. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  1183. #define POINTER_OFFSET(opnd, c, field) \
  1184. BuildIndirOpnd((opnd), c::Get##field##Offset(), _u(#c) _u(".") _u(#field))
  1185. #else
  1186. #define POINTER_OFFSET(opnd, c, field) \
  1187. BuildIndirOpnd((opnd), c::Get##field##Offset())
  1188. #endif
  1189. void
  1190. IRBuilder::BuildGeneratorPreamble()
  1191. {
  1192. if (!this->m_func->GetJITFunctionBody()->IsCoroutine())
  1193. {
  1194. return;
  1195. }
  1196. // Build code to check if the generator already has state and if it does then jump to the corresponding resume point.
  1197. // Otherwise jump to the start of the function. The generator object is the first argument by convention established
  1198. // in JavascriptGenerator::EntryNext/EntryReturn/EntryThrow.
  1199. //
  1200. // s1 = Ld_A prm1
  1201. // s2 = Ld_A s1[offset of JavascriptGenerator::frame]
  1202. // BrAddr_A s2 nullptr $startOfFunc
  1203. // s3 = Ld_A s2[offset of InterpreterStackFrame::m_reader.m_currentLocation]
  1204. // s4 = Ld_A s2[offset of InterpreterStackFrame::m_reader.m_startLocation]
  1205. // s5 = Sub_I4 s3 s4
  1206. // GeneratorResumeJumpTable s5
  1207. // $startOfFunc:
  1208. //
  1209. StackSym *genParamSym = StackSym::NewParamSlotSym(1, this->m_func);
  1210. this->m_func->SetArgOffset(genParamSym, LowererMD::GetFormalParamOffset() * MachPtr);
  1211. IR::SymOpnd *genParamOpnd = IR::SymOpnd::New(genParamSym, TyMachPtr, this->m_func);
  1212. IR::RegOpnd *genRegOpnd = IR::RegOpnd::New(TyMachPtr, this->m_func);
  1213. IR::Instr *instr = IR::Instr::New(Js::OpCode::Ld_A, genRegOpnd, genParamOpnd, this->m_func);
  1214. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  1215. IR::RegOpnd *genFrameOpnd = IR::RegOpnd::New(TyMachPtr, this->m_func);
  1216. instr = IR::Instr::New(Js::OpCode::Ld_A, genFrameOpnd, POINTER_OFFSET(genRegOpnd, Js::JavascriptGenerator, Frame), this->m_func);
  1217. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  1218. IR::LabelInstr *labelInstr = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  1219. IR::BranchInstr *branchInstr = IR::BranchInstr::New(Js::OpCode::BrAddr_A, labelInstr, genFrameOpnd, IR::AddrOpnd::NewNull(this->m_func), this->m_func);
  1220. this->AddInstr(branchInstr, Js::Constants::NoByteCodeOffset);
  1221. IR::RegOpnd *curLocOpnd = IR::RegOpnd::New(TyMachPtr, this->m_func);
  1222. instr = IR::Instr::New(Js::OpCode::Ld_A, curLocOpnd, POINTER_OFFSET(genFrameOpnd, Js::InterpreterStackFrame, CurrentLocation), this->m_func);
  1223. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  1224. IR::RegOpnd *startLocOpnd = IR::RegOpnd::New(TyMachPtr, this->m_func);
  1225. instr = IR::Instr::New(Js::OpCode::Ld_A, startLocOpnd, POINTER_OFFSET(genFrameOpnd, Js::InterpreterStackFrame, StartLocation), this->m_func);
  1226. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  1227. IR::RegOpnd *curOffsetOpnd = IR::RegOpnd::New(TyUint32, this->m_func);
  1228. instr = IR::Instr::New(Js::OpCode::Sub_I4, curOffsetOpnd, curLocOpnd, startLocOpnd, this->m_func);
  1229. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  1230. instr = IR::Instr::New(Js::OpCode::GeneratorResumeJumpTable, this->m_func);
  1231. instr->SetSrc1(curOffsetOpnd);
  1232. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  1233. this->AddInstr(labelInstr, Js::Constants::NoByteCodeOffset);
  1234. }
  1235. void
  1236. IRBuilder::LoadNativeCodeData()
  1237. {
  1238. if (m_func->IsOOPJIT() && m_func->IsTopFunc())
  1239. {
  1240. IR::RegOpnd * nativeDataOpnd = IR::RegOpnd::New(TyVar, m_func);
  1241. IR::Instr * instr = IR::Instr::New(Js::OpCode::LdNativeCodeData, nativeDataOpnd, m_func);
  1242. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  1243. m_func->SetNativeCodeDataSym(nativeDataOpnd->GetStackSym());
  1244. }
  1245. }
  1246. void
  1247. IRBuilder::BuildConstantLoads()
  1248. {
  1249. Js::RegSlot count = m_func->GetJITFunctionBody()->GetConstCount();
  1250. for (Js::RegSlot reg = Js::FunctionBody::FirstRegSlot; reg < count; reg++)
  1251. {
  1252. intptr_t varConst = m_func->GetJITFunctionBody()->GetConstantVar(reg);
  1253. Assert(varConst != 0);
  1254. Js::TypeId type = m_func->GetJITFunctionBody()->GetConstantType(reg);
  1255. IR::RegOpnd *dstOpnd = this->BuildDstOpnd(reg);
  1256. Assert(this->RegIsConstant(reg));
  1257. dstOpnd->m_sym->SetIsFromByteCodeConstantTable();
  1258. // TODO: be more precise about this
  1259. ValueType valueType;
  1260. IR::Instr *instr = nullptr;
  1261. switch (type)
  1262. {
  1263. case Js::TypeIds_Number:
  1264. valueType = ValueType::Number;
  1265. instr = IR::Instr::NewConstantLoad(dstOpnd, varConst, valueType, m_func
  1266. #if !FLOATVAR
  1267. , m_func->IsOOPJIT() ? m_func->GetJITFunctionBody()->GetConstAsT<Js::JavascriptNumber>(reg) : nullptr
  1268. #endif
  1269. );
  1270. break;
  1271. case Js::TypeIds_String:
  1272. {
  1273. valueType = ValueType::String;
  1274. if (m_func->IsOOPJIT())
  1275. {
  1276. // must be either PropertyString or LiteralString
  1277. JITRecyclableObject * jitObj = m_func->GetJITFunctionBody()->GetConstantContent(reg);
  1278. JITJavascriptString * constStr = JITJavascriptString::FromVar(jitObj);
  1279. instr = IR::Instr::NewConstantLoad(dstOpnd, varConst, valueType, m_func, constStr);
  1280. }
  1281. else
  1282. {
  1283. instr = IR::Instr::NewConstantLoad(dstOpnd, varConst, valueType, m_func);
  1284. }
  1285. break;
  1286. }
  1287. case Js::TypeIds_Limit:
  1288. valueType = ValueType::FromTypeId(type, false);
  1289. instr = IR::Instr::NewConstantLoad(dstOpnd, varConst, valueType, m_func);
  1290. break;
  1291. default:
  1292. valueType = ValueType::FromTypeId(type, false);
  1293. instr = IR::Instr::NewConstantLoad(dstOpnd, varConst, valueType, m_func,
  1294. m_func->IsOOPJIT() ? m_func->GetJITFunctionBody()->GetConstAsT<Js::RecyclableObject>(reg) : nullptr);
  1295. break;
  1296. }
  1297. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  1298. }
  1299. }
  1300. ///----------------------------------------------------------------------------
  1301. ///
  1302. /// IRBuilder::BuildReg1
  1303. ///
  1304. /// Build IR instr for a Reg1 instruction.
  1305. ///
  1306. ///----------------------------------------------------------------------------
  1307. template <typename SizePolicy>
  1308. void
  1309. IRBuilder::BuildReg1(Js::OpCode newOpcode, uint32 offset)
  1310. {
  1311. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  1312. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  1313. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_Reg1<SizePolicy>>();
  1314. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  1315. {
  1316. this->DoClosureRegCheck(layout->R0);
  1317. }
  1318. BuildReg1(newOpcode, offset, layout->R0);
  1319. }
  1320. void
  1321. IRBuilder::BuildReg1(Js::OpCode newOpcode, uint32 offset, Js::RegSlot R0)
  1322. {
  1323. if (newOpcode == Js::OpCode::ArgIn0)
  1324. {
  1325. this->BuildArgIn0(offset, R0);
  1326. return;
  1327. }
  1328. IR::Instr * instr;
  1329. Js::RegSlot srcRegOpnd, dstRegSlot;
  1330. srcRegOpnd = dstRegSlot = R0;
  1331. IR::Opnd * srcOpnd = nullptr;
  1332. bool isNotInt = false;
  1333. bool dstIsCatchObject = false;
  1334. ValueType dstValueType;
  1335. switch (newOpcode)
  1336. {
  1337. case Js::OpCode::LdLetHeapArguments:
  1338. {
  1339. this->m_func->SetHasNonSimpleParams();
  1340. //FallThrough to next case block!
  1341. }
  1342. case Js::OpCode::LdHeapArguments:
  1343. {
  1344. if (this->m_func->GetJITFunctionBody()->NeedScopeObjectForArguments(m_func->GetHasNonSimpleParams()))
  1345. {
  1346. Js::RegSlot regFrameObj = m_func->GetJITFunctionBody()->GetLocalClosureReg();
  1347. Assert(regFrameObj != Js::Constants::NoRegister);
  1348. srcOpnd = BuildSrcOpnd(regFrameObj);
  1349. if (m_func->GetJITFunctionBody()->GetInParamsCount() > 1)
  1350. {
  1351. m_func->SetScopeObjSym(srcOpnd->GetStackSym());
  1352. }
  1353. }
  1354. else
  1355. {
  1356. srcOpnd = IR::AddrOpnd::New(
  1357. m_func->GetScriptContextInfo()->GetNullAddr(), IR::AddrOpndKindDynamicVar, m_func, true);
  1358. }
  1359. IR::RegOpnd * dstOpnd = BuildDstOpnd(R0);
  1360. instr = IR::Instr::New(newOpcode, dstOpnd, srcOpnd, m_func);
  1361. this->AddInstr(instr, offset);
  1362. StackSym * dstSym = dstOpnd->m_sym;
  1363. if (dstSym->m_isSingleDef)
  1364. {
  1365. dstSym->m_isSafeThis = true;
  1366. dstSym->m_isNotNumber = true;
  1367. }
  1368. return;
  1369. }
  1370. case Js::OpCode::LdLetHeapArgsCached:
  1371. {
  1372. this->m_func->SetHasNonSimpleParams();
  1373. //Fallthrough to next case block!
  1374. }
  1375. case Js::OpCode::LdHeapArgsCached:
  1376. if (!m_func->GetJITFunctionBody()->HasScopeObject())
  1377. {
  1378. Js::Throw::FatalInternalError();
  1379. }
  1380. srcOpnd = BuildSrcOpnd(m_func->GetJITFunctionBody()->GetLocalClosureReg());
  1381. if (m_func->GetJITFunctionBody()->GetInParamsCount() > 1)
  1382. {
  1383. m_func->SetScopeObjSym(srcOpnd->GetStackSym());
  1384. }
  1385. isNotInt = true;
  1386. break;
  1387. case Js::OpCode::LdLocalObj:
  1388. if (!m_func->GetJITFunctionBody()->HasScopeObject())
  1389. {
  1390. Js::Throw::FatalInternalError();
  1391. }
  1392. srcOpnd = BuildSrcOpnd(m_func->GetJITFunctionBody()->GetLocalClosureReg());
  1393. isNotInt = true;
  1394. newOpcode = Js::OpCode::Ld_A;
  1395. break;
  1396. case Js::OpCode::LdParamObj:
  1397. if (!m_func->GetJITFunctionBody()->HasScopeObject())
  1398. {
  1399. Js::Throw::FatalInternalError();
  1400. }
  1401. srcOpnd = BuildSrcOpnd(m_func->GetJITFunctionBody()->GetParamClosureReg());
  1402. isNotInt = true;
  1403. newOpcode = Js::OpCode::Ld_A;
  1404. break;
  1405. case Js::OpCode::Throw:
  1406. {
  1407. srcOpnd = this->BuildSrcOpnd(srcRegOpnd);
  1408. if ((this->handlerOffsetStack && !this->handlerOffsetStack->Empty()) ||
  1409. finallyBlockLevel > 0)
  1410. {
  1411. newOpcode = Js::OpCode::EHThrow;
  1412. }
  1413. instr = IR::Instr::New(newOpcode, m_func);
  1414. instr->SetSrc1(srcOpnd);
  1415. this->AddInstr(instr, offset);
  1416. if(DoBailOnNoProfile())
  1417. {
  1418. //So optimistically assume it doesn't throw and introduce bailonnoprofile here.
  1419. //If there are continuous bailout bailonnoprofile will be disabled.
  1420. InsertBailOnNoProfile(instr);
  1421. }
  1422. return;
  1423. }
  1424. case Js::OpCode::LdC_A_Null:
  1425. {
  1426. const auto addrOpnd = IR::AddrOpnd::New(m_func->GetScriptContextInfo()->GetNullAddr(), IR::AddrOpndKindDynamicVar, m_func, true);
  1427. addrOpnd->SetValueType(ValueType::Null);
  1428. srcOpnd = addrOpnd;
  1429. newOpcode = Js::OpCode::Ld_A;
  1430. break;
  1431. }
  1432. case Js::OpCode::LdUndef:
  1433. {
  1434. const auto addrOpnd = IR::AddrOpnd::New(m_func->GetScriptContextInfo()->GetUndefinedAddr(), IR::AddrOpndKindDynamicVar, m_func, true);
  1435. addrOpnd->SetValueType(ValueType::Undefined);
  1436. srcOpnd = addrOpnd;
  1437. newOpcode = Js::OpCode::Ld_A;
  1438. break;
  1439. }
  1440. case Js::OpCode::LdInfinity:
  1441. {
  1442. const auto floatConstOpnd = IR::FloatConstOpnd::New(Js::JavascriptNumber::POSITIVE_INFINITY, TyFloat64, m_func);
  1443. srcOpnd = floatConstOpnd;
  1444. newOpcode = Js::OpCode::LdC_A_R8;
  1445. break;
  1446. }
  1447. case Js::OpCode::LdNaN:
  1448. {
  1449. const auto floatConstOpnd = IR::FloatConstOpnd::New(Js::JavascriptNumber::NaN, TyFloat64, m_func);
  1450. srcOpnd = floatConstOpnd;
  1451. newOpcode = Js::OpCode::LdC_A_R8;
  1452. break;
  1453. }
  1454. case Js::OpCode::LdFalse:
  1455. {
  1456. const auto addrOpnd = IR::AddrOpnd::New(m_func->GetScriptContextInfo()->GetFalseAddr(), IR::AddrOpndKindDynamicVar, m_func, true);
  1457. addrOpnd->SetValueType(ValueType::Boolean);
  1458. srcOpnd = addrOpnd;
  1459. newOpcode = Js::OpCode::Ld_A;
  1460. break;
  1461. }
  1462. case Js::OpCode::LdTrue:
  1463. {
  1464. const auto addrOpnd = IR::AddrOpnd::New(m_func->GetScriptContextInfo()->GetTrueAddr(), IR::AddrOpndKindDynamicVar, m_func, true);
  1465. addrOpnd->SetValueType(ValueType::Boolean);
  1466. srcOpnd = addrOpnd;
  1467. newOpcode = Js::OpCode::Ld_A;
  1468. break;
  1469. }
  1470. case Js::OpCode::NewScObjectSimple:
  1471. dstValueType = ValueType::GetObject(ObjectType::UninitializedObject);
  1472. // fall-through
  1473. case Js::OpCode::LdFuncExpr:
  1474. m_func->DisableCanDoInlineArgOpt();
  1475. break;
  1476. case Js::OpCode::LdEnv:
  1477. case Js::OpCode::LdHomeObj:
  1478. case Js::OpCode::LdFuncObj:
  1479. isNotInt = TRUE;
  1480. break;
  1481. case Js::OpCode::Unused:
  1482. // Don't generate anything. Just indicate that the temp reg is used.
  1483. Assert(this->RegIsTemp(dstRegSlot));
  1484. this->SetTempUsed(dstRegSlot, TRUE);
  1485. return;
  1486. case Js::OpCode::InitUndecl:
  1487. srcOpnd = IR::AddrOpnd::New(m_func->GetScriptContextInfo()->GetUndeclBlockVarAddr(), IR::AddrOpndKindDynamicVar, m_func, true);
  1488. srcOpnd->SetValueType(ValueType::PrimitiveOrObject);
  1489. newOpcode = Js::OpCode::Ld_A;
  1490. break;
  1491. case Js::OpCode::ChkUndecl:
  1492. srcOpnd = BuildSrcOpnd(srcRegOpnd);
  1493. instr = IR::Instr::New(Js::OpCode::ChkUndecl, m_func);
  1494. instr->SetSrc1(srcOpnd);
  1495. this->AddInstr(instr, offset);
  1496. return;
  1497. case Js::OpCode::Catch:
  1498. if (this->handlerOffsetStack)
  1499. {
  1500. AssertOrFailFast(!this->handlerOffsetStack->Empty());
  1501. AssertOrFailFast(this->handlerOffsetStack->Top().Second() == true);
  1502. this->handlerOffsetStack->Pop();
  1503. }
  1504. dstIsCatchObject = true;
  1505. break;
  1506. case Js::OpCode::LdChakraLib:
  1507. {
  1508. const auto addrOpnd = IR::AddrOpnd::New(m_func->GetScriptContextInfo()->GetChakraLibAddr(), IR::AddrOpndKindDynamicVar, m_func, true);
  1509. addrOpnd->SetValueType(ValueType::PrimitiveOrObject);
  1510. srcOpnd = addrOpnd;
  1511. newOpcode = Js::OpCode::Ld_A;
  1512. break;
  1513. }
  1514. }
  1515. IR::RegOpnd * dstOpnd = this->BuildDstOpnd(dstRegSlot, TyVar, dstIsCatchObject);
  1516. dstOpnd->SetValueType(dstValueType);
  1517. StackSym * dstSym = dstOpnd->m_sym;
  1518. dstSym->m_isCatchObjectSym = dstIsCatchObject;
  1519. instr = IR::Instr::New(newOpcode, dstOpnd, m_func);
  1520. if (srcOpnd)
  1521. {
  1522. instr->SetSrc1(srcOpnd);
  1523. if (dstSym->m_isSingleDef)
  1524. {
  1525. if (srcOpnd->IsHelperCallOpnd())
  1526. {
  1527. // Don't do anything
  1528. }
  1529. else if (srcOpnd->IsIntConstOpnd())
  1530. {
  1531. dstSym->SetIsIntConst(srcOpnd->AsIntConstOpnd()->GetValue());
  1532. }
  1533. else if (srcOpnd->IsFloatConstOpnd())
  1534. {
  1535. dstSym->SetIsFloatConst();
  1536. }
  1537. else if (srcOpnd->IsAddrOpnd())
  1538. {
  1539. dstSym->m_isConst = true;
  1540. dstSym->m_isNotNumber = true;
  1541. }
  1542. }
  1543. }
  1544. if (isNotInt && dstSym->m_isSingleDef)
  1545. {
  1546. dstSym->m_isNotNumber = true;
  1547. }
  1548. this->AddInstr(instr, offset);
  1549. }
  1550. ///----------------------------------------------------------------------------
  1551. ///
  1552. /// IRBuilder::BuildReg2
  1553. ///
  1554. /// Build IR instr for a Reg2 instruction.
  1555. ///
  1556. ///----------------------------------------------------------------------------
  1557. template <typename SizePolicy>
  1558. void
  1559. IRBuilder::BuildReg2(Js::OpCode newOpcode, uint32 offset)
  1560. {
  1561. Assert(!OpCodeAttr::IsProfiledOp(newOpcode) || newOpcode == Js::OpCode::ProfiledStrictLdThis);
  1562. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  1563. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_Reg2<SizePolicy>>();
  1564. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  1565. {
  1566. this->DoClosureRegCheck(layout->R0);
  1567. this->DoClosureRegCheck(layout->R1);
  1568. }
  1569. BuildReg2(newOpcode, offset, layout->R0, layout->R1, m_jnReader.GetCurrentOffset());
  1570. }
  1571. void
  1572. IRBuilder::BuildReg2(Js::OpCode newOpcode, uint32 offset, Js::RegSlot R0, Js::RegSlot R1, uint32 nextOffset)
  1573. {
  1574. IR::RegOpnd * src1Opnd = this->BuildSrcOpnd(R1);
  1575. StackSym * symSrc1 = src1Opnd->m_sym;
  1576. switch (newOpcode)
  1577. {
  1578. case Js::OpCode::SpreadObjectLiteral:
  1579. // fall through
  1580. case Js::OpCode::SetComputedNameVar:
  1581. {
  1582. IR::Instr *instr = IR::Instr::New(newOpcode, m_func);
  1583. instr->SetSrc1(this->BuildSrcOpnd(R0));
  1584. instr->SetSrc2(src1Opnd);
  1585. this->AddInstr(instr, offset);
  1586. return;
  1587. }
  1588. case Js::OpCode::LdFuncExprFrameDisplay:
  1589. {
  1590. IR::RegOpnd *dstOpnd = IR::RegOpnd::New(TyVar, m_func);
  1591. IR::Instr *instr = IR::Instr::New(Js::OpCode::LdFrameDisplay, dstOpnd, src1Opnd, m_func);
  1592. Js::RegSlot envReg = this->GetEnvReg();
  1593. if (envReg != Js::Constants::NoRegister)
  1594. {
  1595. instr->SetSrc2(BuildSrcOpnd(envReg));
  1596. }
  1597. this->AddInstr(instr, offset);
  1598. IR::RegOpnd *src2Opnd = dstOpnd;
  1599. src1Opnd = BuildSrcOpnd(R0);
  1600. dstOpnd = BuildDstOpnd(m_func->GetJITFunctionBody()->GetLocalFrameDisplayReg());
  1601. instr = IR::Instr::New(Js::OpCode::LdFrameDisplay, dstOpnd, src1Opnd, src2Opnd, m_func);
  1602. dstOpnd->m_sym->m_isNotNumber = true;
  1603. this->AddInstr(instr, offset);
  1604. return;
  1605. }
  1606. }
  1607. IR::RegOpnd * dstOpnd = this->BuildDstOpnd(R0);
  1608. StackSym * dstSym = dstOpnd->m_sym;
  1609. IR::Instr * instr = nullptr;
  1610. switch (newOpcode)
  1611. {
  1612. case Js::OpCode::Ld_A:
  1613. if (symSrc1->m_builtInIndex != Js::BuiltinFunction::None)
  1614. {
  1615. // Note: don't set dstSym->m_builtInIndex to None here (see Win8 399972)
  1616. dstSym->m_builtInIndex = symSrc1->m_builtInIndex;
  1617. }
  1618. break;
  1619. case Js::OpCode::ProfiledStrictLdThis:
  1620. newOpcode = Js::OpCode::StrictLdThis;
  1621. if (m_func->HasProfileInfo())
  1622. {
  1623. dstOpnd->SetValueType(m_func->GetReadOnlyProfileInfo()->GetThisInfo().valueType);
  1624. }
  1625. if (m_func->DoSimpleJitDynamicProfile())
  1626. {
  1627. IR::JitProfilingInstr* newInstr = IR::JitProfilingInstr::New(Js::OpCode::StrictLdThis, dstOpnd, src1Opnd, m_func);
  1628. instr = newInstr;
  1629. }
  1630. break;
  1631. case Js::OpCode::Delete_A:
  1632. dstOpnd->SetValueType(ValueType::Boolean);
  1633. break;
  1634. case Js::OpCode::BeginSwitch:
  1635. m_switchBuilder.BeginSwitch();
  1636. newOpcode = Js::OpCode::Ld_A;
  1637. break;
  1638. case Js::OpCode::LdArrHead:
  1639. src1Opnd->SetValueType(
  1640. ValueType::GetObject(ObjectType::Array).SetHasNoMissingValues(false).SetArrayTypeId(Js::TypeIds_Array));
  1641. src1Opnd->SetValueTypeFixed();
  1642. break;
  1643. case Js::OpCode::LdInnerFrameDisplayNoParent:
  1644. {
  1645. instr = IR::Instr::New(Js::OpCode::LdInnerFrameDisplay, dstOpnd, src1Opnd, m_func);
  1646. this->AddEnvOpndForInnerFrameDisplay(instr, offset);
  1647. if (dstSym->m_isSingleDef)
  1648. {
  1649. dstSym->m_isNotNumber = true;
  1650. }
  1651. this->AddInstr(instr, offset);
  1652. return;
  1653. }
  1654. case Js::OpCode::Conv_Str:
  1655. dstOpnd->SetValueType(ValueType::String);
  1656. break;
  1657. case Js::OpCode::Yield:
  1658. instr = IR::Instr::New(newOpcode, dstOpnd, src1Opnd, m_func);
  1659. this->AddInstr(instr, offset);
  1660. this->m_lastInstr = instr->ConvertToBailOutInstr(instr, IR::BailOutForGeneratorYield);
  1661. IR::LabelInstr* label = IR::LabelInstr::New(Js::OpCode::Label, m_func);
  1662. label->m_hasNonBranchRef = true;
  1663. this->AddInstr(label, Js::Constants::NoByteCodeOffset);
  1664. this->m_func->AddYieldOffsetResumeLabel(nextOffset, label);
  1665. return;
  1666. }
  1667. if (instr == nullptr)
  1668. {
  1669. instr = IR::Instr::New(newOpcode, dstOpnd, src1Opnd, m_func);
  1670. }
  1671. this->AddInstr(instr, offset);
  1672. }
  1673. ///----------------------------------------------------------------------------
  1674. ///
  1675. /// IRBuilder::BuildProfiledReg2
  1676. ///
  1677. /// Build IR instr for a profiled Reg2 instruction.
  1678. ///
  1679. ///----------------------------------------------------------------------------
  1680. template <typename SizePolicy>
  1681. void
  1682. IRBuilder::BuildProfiledReg2(Js::OpCode newOpcode, uint32 offset)
  1683. {
  1684. Assert(OpCodeAttr::IsProfiledOp(newOpcode));
  1685. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  1686. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_Reg2<SizePolicy>>>();
  1687. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  1688. {
  1689. this->DoClosureRegCheck(layout->R0);
  1690. this->DoClosureRegCheck(layout->R1);
  1691. }
  1692. BuildProfiledReg2(newOpcode, offset, layout->R0, layout->R1, layout->profileId);
  1693. }
  1694. void
  1695. IRBuilder::BuildProfiledReg2(Js::OpCode newOpcode, uint32 offset, Js::RegSlot dstRegSlot, Js::RegSlot srcRegSlot, Js::ProfileId profileId)
  1696. {
  1697. bool switchFound = false;
  1698. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(newOpcode);
  1699. Assert(newOpcode == Js::OpCode::BeginSwitch);
  1700. IR::RegOpnd * src1Opnd = this->BuildSrcOpnd(srcRegSlot);
  1701. IR::RegOpnd * dstOpnd;
  1702. if(srcRegSlot == dstRegSlot)
  1703. {
  1704. //if the operands are the same for BeginSwitch, don't build a new operand in IR.
  1705. dstOpnd = src1Opnd;
  1706. }
  1707. else
  1708. {
  1709. dstOpnd = this->BuildDstOpnd(dstRegSlot);
  1710. }
  1711. m_switchBuilder.BeginSwitch();
  1712. switchFound = true;
  1713. newOpcode = Js::OpCode::Ld_A; // BeginSwitch is originally equivalent to Ld_A
  1714. IR::Instr *instr;
  1715. if (m_func->DoSimpleJitDynamicProfile())
  1716. {
  1717. // Since we're in simplejit, we want to keep track of the profileid:
  1718. IR::JitProfilingInstr *profiledInstr = IR::JitProfilingInstr::New(newOpcode, dstOpnd, src1Opnd, m_func);
  1719. profiledInstr->profileId = profileId;
  1720. profiledInstr->isBeginSwitch = newOpcode == Js::OpCode::Ld_A;
  1721. instr = profiledInstr;
  1722. }
  1723. else
  1724. {
  1725. IR::ProfiledInstr *profiledInstr = IR::ProfiledInstr::New(newOpcode, dstOpnd, src1Opnd, m_func);
  1726. instr = profiledInstr;
  1727. profiledInstr->u.FldInfo() = Js::FldInfo();
  1728. }
  1729. this->AddInstr(instr, offset);
  1730. if(switchFound && instr->IsProfiledInstr())
  1731. {
  1732. m_switchBuilder.SetProfiledInstruction(instr, profileId);
  1733. }
  1734. }
  1735. ///----------------------------------------------------------------------------
  1736. ///
  1737. /// IRBuilder::BuildReg3
  1738. ///
  1739. /// Build IR instr for a Reg3 instruction.
  1740. ///
  1741. ///----------------------------------------------------------------------------
  1742. template <typename SizePolicy>
  1743. void
  1744. IRBuilder::BuildReg3(Js::OpCode newOpcode, uint32 offset)
  1745. {
  1746. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  1747. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  1748. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_Reg3<SizePolicy>>();
  1749. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func) && newOpcode != Js::OpCode::NewInnerScopeSlots)
  1750. {
  1751. this->DoClosureRegCheck(layout->R0);
  1752. this->DoClosureRegCheck(layout->R1);
  1753. this->DoClosureRegCheck(layout->R2);
  1754. }
  1755. BuildReg3(newOpcode, offset, layout->R0, layout->R1, layout->R2, Js::Constants::NoProfileId);
  1756. }
  1757. template <typename SizePolicy>
  1758. void
  1759. IRBuilder::BuildProfiledReg3(Js::OpCode newOpcode, uint32 offset)
  1760. {
  1761. Assert(OpCodeAttr::IsProfiledOp(newOpcode));
  1762. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  1763. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_Reg3<SizePolicy>>>();
  1764. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  1765. {
  1766. this->DoClosureRegCheck(layout->R0);
  1767. this->DoClosureRegCheck(layout->R1);
  1768. this->DoClosureRegCheck(layout->R2);
  1769. }
  1770. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(newOpcode);
  1771. BuildReg3(newOpcode, offset, layout->R0, layout->R1, layout->R2, layout->profileId);
  1772. }
  1773. void
  1774. IRBuilder::BuildReg3(Js::OpCode newOpcode, uint32 offset, Js::RegSlot dstRegSlot, Js::RegSlot src1RegSlot,
  1775. Js::RegSlot src2RegSlot, Js::ProfileId profileId)
  1776. {
  1777. IR::Instr * instr;
  1778. if (newOpcode == Js::OpCode::NewInnerScopeSlots)
  1779. {
  1780. if (dstRegSlot >= m_func->GetJITFunctionBody()->GetInnerScopeCount())
  1781. {
  1782. Js::Throw::FatalInternalError();
  1783. }
  1784. newOpcode = Js::OpCode::NewScopeSlotsWithoutPropIds;
  1785. dstRegSlot += m_func->GetJITFunctionBody()->GetFirstInnerScopeReg();
  1786. instr = IR::Instr::New(newOpcode, BuildDstOpnd(dstRegSlot),
  1787. IR::IntConstOpnd::New(src1RegSlot, TyVar, m_func),
  1788. IR::IntConstOpnd::New(src2RegSlot, TyVar, m_func),
  1789. m_func);
  1790. if (instr->GetDst()->AsRegOpnd()->m_sym->m_isSingleDef)
  1791. {
  1792. instr->GetDst()->AsRegOpnd()->m_sym->m_isNotNumber = true;
  1793. }
  1794. this->AddInstr(instr, offset);
  1795. return;
  1796. }
  1797. IR::RegOpnd * src1Opnd = this->BuildSrcOpnd(src1RegSlot);
  1798. IR::RegOpnd * src2Opnd = this->BuildSrcOpnd(src2RegSlot);
  1799. IR::RegOpnd * dstOpnd = this->BuildDstOpnd(dstRegSlot);
  1800. StackSym * dstSym = dstOpnd->m_sym;
  1801. bool isProfiledInstr = (profileId != Js::Constants::NoProfileId);
  1802. bool wasNotProfiled = false;
  1803. const Js::LdElemInfo * ldElemInfo = nullptr;
  1804. if (isProfiledInstr && newOpcode == Js::OpCode::IsIn)
  1805. {
  1806. if (!DoLoadInstructionArrayProfileInfo())
  1807. {
  1808. isProfiledInstr = false;
  1809. }
  1810. else
  1811. {
  1812. ldElemInfo = this->m_func->GetReadOnlyProfileInfo()->GetLdElemInfo(profileId);
  1813. ValueType arrayType = ldElemInfo->GetArrayType();
  1814. wasNotProfiled = !ldElemInfo->WasProfiled();
  1815. if (arrayType.IsLikelyNativeArray() && !AllowNativeArrayProfileInfo())
  1816. {
  1817. arrayType = arrayType.SetArrayTypeId(Js::TypeIds_Array);
  1818. // An opnd's value type will get replaced in the forward phase when it is not fixed. Store the array type in the ProfiledInstr.
  1819. Js::LdElemInfo *const newLdElemInfo = JitAnew(m_func->m_alloc, Js::LdElemInfo, *ldElemInfo);
  1820. newLdElemInfo->arrayType = arrayType;
  1821. ldElemInfo = newLdElemInfo;
  1822. }
  1823. src2Opnd->SetValueType(arrayType);
  1824. if (m_func->GetTopFunc()->HasTry() && !m_func->GetTopFunc()->DoOptimizeTry())
  1825. {
  1826. isProfiledInstr = false;
  1827. }
  1828. }
  1829. }
  1830. if (isProfiledInstr)
  1831. {
  1832. if (m_func->DoSimpleJitDynamicProfile())
  1833. {
  1834. instr = IR::JitProfilingInstr::New(newOpcode, dstOpnd, src1Opnd, src2Opnd, m_func);
  1835. instr->AsJitProfilingInstr()->profileId = profileId;
  1836. }
  1837. else
  1838. {
  1839. instr = IR::ProfiledInstr::New(newOpcode, dstOpnd, src1Opnd, src2Opnd, m_func);
  1840. if (newOpcode == Js::OpCode::IsIn)
  1841. {
  1842. instr->AsProfiledInstr()->u.ldElemInfo = ldElemInfo;
  1843. }
  1844. else
  1845. {
  1846. instr->AsProfiledInstr()->u.profileId = profileId;
  1847. }
  1848. }
  1849. }
  1850. else
  1851. {
  1852. instr = IR::Instr::New(newOpcode, dstOpnd, src1Opnd, src2Opnd, m_func);
  1853. }
  1854. this->AddInstr(instr, offset);
  1855. if (wasNotProfiled && DoBailOnNoProfile())
  1856. {
  1857. InsertBailOnNoProfile(instr);
  1858. }
  1859. switch (newOpcode)
  1860. {
  1861. case Js::OpCode::LdHandlerScope:
  1862. case Js::OpCode::NewScopeSlotsWithoutPropIds:
  1863. if (dstSym->m_isSingleDef)
  1864. {
  1865. dstSym->m_isNotNumber = true;
  1866. }
  1867. break;
  1868. case Js::OpCode::LdInnerFrameDisplay:
  1869. if (dstSym->m_isSingleDef)
  1870. {
  1871. dstSym->m_isNotNumber = true;
  1872. }
  1873. break;
  1874. }
  1875. }
  1876. ///----------------------------------------------------------------------------
  1877. ///
  1878. /// IRBuilder::BuildReg3C
  1879. ///
  1880. /// Build IR instr for a Reg3C instruction.
  1881. ///
  1882. ///----------------------------------------------------------------------------
  1883. template <typename SizePolicy>
  1884. void
  1885. IRBuilder::BuildReg3C(Js::OpCode newOpcode, uint32 offset)
  1886. {
  1887. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  1888. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  1889. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_Reg3C<SizePolicy>>();
  1890. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  1891. {
  1892. this->DoClosureRegCheck(layout->R0);
  1893. this->DoClosureRegCheck(layout->R1);
  1894. this->DoClosureRegCheck(layout->R2);
  1895. }
  1896. BuildReg3C(newOpcode, offset, layout->R0, layout->R1, layout->R2, layout->inlineCacheIndex);
  1897. }
  1898. void
  1899. IRBuilder::BuildReg3C(Js::OpCode newOpCode, uint32 offset, Js::RegSlot dstRegSlot, Js::RegSlot src1RegSlot,
  1900. Js::RegSlot src2RegSlot, Js::CacheId inlineCacheIndex)
  1901. {
  1902. Assert(OpCodeAttr::HasMultiSizeLayout(newOpCode));
  1903. IR::Instr * instr;
  1904. IR::RegOpnd * src1Opnd = this->BuildSrcOpnd(src1RegSlot);
  1905. IR::RegOpnd * src2Opnd = this->BuildSrcOpnd(src2RegSlot);
  1906. IR::RegOpnd * dstOpnd = this->BuildDstOpnd(dstRegSlot);
  1907. instr = IR::Instr::New(Js::OpCode::ArgOut_A, IR::RegOpnd::New(TyVar, m_func), src2Opnd, m_func);
  1908. this->AddInstr(instr, offset);
  1909. instr = IR::Instr::New(Js::OpCode::ArgOut_A, IR::RegOpnd::New(TyVar, m_func), src1Opnd, instr->GetDst(), m_func);
  1910. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  1911. instr = IR::Instr::New(newOpCode, dstOpnd, IR::IntConstOpnd::New(inlineCacheIndex, TyUint32, m_func), instr->GetDst(), m_func);
  1912. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  1913. }
  1914. ///----------------------------------------------------------------------------
  1915. ///
  1916. /// IRBuilder::BuildReg4
  1917. ///
  1918. /// Build IR instr for a Reg4 instruction.
  1919. ///
  1920. ///----------------------------------------------------------------------------
  1921. template <typename SizePolicy>
  1922. void
  1923. IRBuilder::BuildReg4(Js::OpCode newOpcode, uint32 offset)
  1924. {
  1925. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  1926. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  1927. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_Reg4<SizePolicy>>();
  1928. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  1929. {
  1930. this->DoClosureRegCheck(layout->R0);
  1931. this->DoClosureRegCheck(layout->R1);
  1932. this->DoClosureRegCheck(layout->R2);
  1933. this->DoClosureRegCheck(layout->R3);
  1934. }
  1935. BuildReg4(newOpcode, offset, layout->R0, layout->R1, layout->R2, layout->R3);
  1936. }
  1937. void
  1938. IRBuilder::BuildReg4(Js::OpCode newOpcode, uint32 offset, Js::RegSlot dstRegSlot, Js::RegSlot src1RegSlot,
  1939. Js::RegSlot src2RegSlot, Js::RegSlot src3RegSlot)
  1940. {
  1941. IR::Instr * instr = nullptr;
  1942. Assert(newOpcode == Js::OpCode::Concat3 || newOpcode == Js::OpCode::Restify);
  1943. IR::RegOpnd * src1Opnd = this->BuildSrcOpnd(src1RegSlot);
  1944. IR::RegOpnd * src2Opnd = this->BuildSrcOpnd(src2RegSlot);
  1945. IR::RegOpnd * src3Opnd = this->BuildSrcOpnd(src3RegSlot);
  1946. if (newOpcode == Js::OpCode::Restify)
  1947. {
  1948. IR::RegOpnd * src0Opnd = this->BuildSrcOpnd(dstRegSlot);
  1949. instr = IR::Instr::New(Js::OpCode::ExtendArg_A, IR::RegOpnd::New(TyVar, m_func), src3Opnd, m_func);
  1950. this->AddInstr(instr, offset);
  1951. instr = IR::Instr::New(Js::OpCode::ExtendArg_A, IR::RegOpnd::New(TyVar, m_func), src2Opnd, instr->GetDst(), m_func);
  1952. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  1953. instr = IR::Instr::New(Js::OpCode::ExtendArg_A, IR::RegOpnd::New(TyVar, m_func), src1Opnd, instr->GetDst(), m_func);
  1954. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  1955. instr = IR::Instr::New(Js::OpCode::ExtendArg_A, IR::RegOpnd::New(TyVar, m_func), src0Opnd, instr->GetDst(), m_func);
  1956. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  1957. IR::Opnd *firstArg = instr->GetDst();
  1958. instr = IR::Instr::New(newOpcode, m_func);
  1959. instr->SetSrc1(firstArg);
  1960. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  1961. return;
  1962. }
  1963. IR::RegOpnd * dstOpnd = this->BuildDstOpnd(dstRegSlot);
  1964. IR::RegOpnd * str1Opnd = InsertConvPrimStr(src1Opnd, offset, true);
  1965. IR::RegOpnd * str2Opnd = InsertConvPrimStr(src2Opnd, Js::Constants::NoByteCodeOffset, true);
  1966. IR::RegOpnd * str3Opnd = InsertConvPrimStr(src3Opnd, Js::Constants::NoByteCodeOffset, true);
  1967. // Need to insert a byte code use for src1/src2 that if ConvPrimStr of the src2/src3 bail out
  1968. // we will restore it.
  1969. bool src1HasByteCodeRegSlot = src1Opnd->m_sym->HasByteCodeRegSlot();
  1970. bool src2HasByteCodeRegSlot = src2Opnd->m_sym->HasByteCodeRegSlot();
  1971. if (src1HasByteCodeRegSlot || src2HasByteCodeRegSlot)
  1972. {
  1973. IR::ByteCodeUsesInstr * byteCodeUse = IR::ByteCodeUsesInstr::New(m_func, Js::Constants::NoByteCodeOffset);
  1974. if (src1HasByteCodeRegSlot)
  1975. {
  1976. byteCodeUse->Set(src1Opnd);
  1977. }
  1978. if (src2HasByteCodeRegSlot)
  1979. {
  1980. byteCodeUse->Set(src2Opnd);
  1981. }
  1982. this->AddInstr(byteCodeUse, Js::Constants::NoByteCodeOffset);
  1983. }
  1984. if (!PHASE_OFF(Js::BackendConcatExprOptPhase, this->m_func))
  1985. {
  1986. IR::RegOpnd* tmpDstOpnd1 = IR::RegOpnd::New(StackSym::New(this->m_func), TyVar, this->m_func);
  1987. IR::RegOpnd* tmpDstOpnd2 = IR::RegOpnd::New(StackSym::New(this->m_func), TyVar, this->m_func);
  1988. IR::RegOpnd* tmpDstOpnd3 = IR::RegOpnd::New(StackSym::New(this->m_func), TyVar, this->m_func);
  1989. instr = IR::Instr::New(Js::OpCode::SetConcatStrMultiItemBE, tmpDstOpnd1, str1Opnd, m_func);
  1990. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  1991. instr = IR::Instr::New(Js::OpCode::SetConcatStrMultiItemBE, tmpDstOpnd2, str2Opnd, tmpDstOpnd1, m_func);
  1992. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  1993. instr = IR::Instr::New(Js::OpCode::SetConcatStrMultiItemBE, tmpDstOpnd3, str3Opnd, tmpDstOpnd2, m_func);
  1994. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  1995. IR::IntConstOpnd * countIntConstOpnd = IR::IntConstOpnd::New(3, TyUint32, m_func, true);
  1996. instr = IR::Instr::New(Js::OpCode::NewConcatStrMultiBE, dstOpnd, countIntConstOpnd, tmpDstOpnd3, m_func);
  1997. dstOpnd->SetValueType(ValueType::String);
  1998. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  1999. }
  2000. else
  2001. {
  2002. instr = IR::Instr::New(Js::OpCode::NewConcatStrMulti, dstOpnd, IR::IntConstOpnd::New(3, TyUint32, m_func, true), m_func);
  2003. dstOpnd->SetValueType(ValueType::String);
  2004. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  2005. instr = IR::Instr::New(Js::OpCode::SetConcatStrMultiItem, IR::IndirOpnd::New(dstOpnd, 0, TyVar, m_func), str1Opnd, m_func);
  2006. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  2007. instr = IR::Instr::New(Js::OpCode::SetConcatStrMultiItem, IR::IndirOpnd::New(dstOpnd, 1, TyVar, m_func), str2Opnd, m_func);
  2008. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  2009. instr = IR::Instr::New(Js::OpCode::SetConcatStrMultiItem, IR::IndirOpnd::New(dstOpnd, 2, TyVar, m_func), str3Opnd, m_func);
  2010. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  2011. }
  2012. }
  2013. IR::RegOpnd *
  2014. IRBuilder::InsertConvPrimStr(IR::RegOpnd * srcOpnd, uint offset, bool forcePreOpBailOutIfNeeded)
  2015. {
  2016. IR::RegOpnd * strOpnd = IR::RegOpnd::New(TyVar, this->m_func);
  2017. IR::Instr * instr = IR::Instr::New(Js::OpCode::Conv_PrimStr, strOpnd, srcOpnd, m_func);
  2018. instr->forcePreOpBailOutIfNeeded = forcePreOpBailOutIfNeeded;
  2019. strOpnd->SetValueType(ValueType::String);
  2020. strOpnd->SetValueTypeFixed();
  2021. this->AddInstr(instr, offset);
  2022. return strOpnd;
  2023. }
  2024. template <typename SizePolicy>
  2025. void
  2026. IRBuilder::BuildReg2B1(Js::OpCode newOpcode, uint32 offset)
  2027. {
  2028. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  2029. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2030. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_Reg2B1<SizePolicy>>();
  2031. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  2032. {
  2033. this->DoClosureRegCheck(layout->R0);
  2034. this->DoClosureRegCheck(layout->R1);
  2035. }
  2036. BuildReg2B1(newOpcode, offset, layout->R0, layout->R1, layout->B2);
  2037. }
  2038. void
  2039. IRBuilder::BuildReg2B1(Js::OpCode newOpcode, uint32 offset, Js::RegSlot dstRegSlot, Js::RegSlot srcRegSlot, byte index)
  2040. {
  2041. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2042. Assert(newOpcode == Js::OpCode::SetConcatStrMultiItem);
  2043. IR::Instr * instr;
  2044. IR::RegOpnd * srcOpnd = this->BuildSrcOpnd(srcRegSlot);
  2045. IR::RegOpnd * dstOpnd = this->BuildDstOpnd(dstRegSlot);
  2046. IR::IndirOpnd * indir1Opnd = IR::IndirOpnd::New(dstOpnd, index, TyVar, m_func);
  2047. dstOpnd->SetValueType(ValueType::String);
  2048. instr = IR::Instr::New(Js::OpCode::SetConcatStrMultiItem, indir1Opnd, InsertConvPrimStr(srcOpnd, offset, true), m_func);
  2049. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  2050. }
  2051. template <typename SizePolicy>
  2052. void
  2053. IRBuilder::BuildReg3B1(Js::OpCode newOpcode, uint32 offset)
  2054. {
  2055. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  2056. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2057. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_Reg3B1<SizePolicy>>();
  2058. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  2059. {
  2060. this->DoClosureRegCheck(layout->R0);
  2061. this->DoClosureRegCheck(layout->R1);
  2062. this->DoClosureRegCheck(layout->R2);
  2063. }
  2064. BuildReg3B1(newOpcode, offset, layout->R0, layout->R1, layout->R2, layout->B3);
  2065. }
  2066. void
  2067. IRBuilder::BuildReg3B1(Js::OpCode newOpcode, uint32 offset, Js::RegSlot dstRegSlot, Js::RegSlot src1RegSlot,
  2068. Js::RegSlot src2RegSlot, uint8 index)
  2069. {
  2070. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2071. IR::Instr * instr;
  2072. IR::RegOpnd * src1Opnd = this->BuildSrcOpnd(src1RegSlot);
  2073. IR::RegOpnd * src2Opnd = this->BuildSrcOpnd(src2RegSlot);
  2074. IR::RegOpnd * dstOpnd = this->BuildDstOpnd(dstRegSlot);
  2075. dstOpnd->SetValueType(ValueType::String);
  2076. IR::Instr * newConcatStrMulti = nullptr;
  2077. switch (newOpcode)
  2078. {
  2079. case Js::OpCode::NewConcatStrMulti:
  2080. newConcatStrMulti = IR::Instr::New(Js::OpCode::NewConcatStrMulti, dstOpnd, IR::IntConstOpnd::New(index, TyUint32, m_func), m_func);
  2081. index = 0;
  2082. break;
  2083. case Js::OpCode::SetConcatStrMultiItem2:
  2084. break;
  2085. default:
  2086. Assert(false);
  2087. };
  2088. IR::IndirOpnd * indir1Opnd = IR::IndirOpnd::New(dstOpnd, index, TyVar, m_func);
  2089. IR::IndirOpnd * indir2Opnd = IR::IndirOpnd::New(dstOpnd, index + 1, TyVar, m_func);
  2090. // Need to do the to str first, as they may have side effects.
  2091. IR::RegOpnd * str1Opnd = InsertConvPrimStr(src1Opnd, offset, true);
  2092. IR::RegOpnd * str2Opnd = InsertConvPrimStr(src2Opnd, Js::Constants::NoByteCodeOffset, true);
  2093. // Need to insert a byte code use for src1 so that if ConvPrimStr of the src2 bail out
  2094. // we will restore it.
  2095. if (src1Opnd->m_sym->HasByteCodeRegSlot())
  2096. {
  2097. IR::ByteCodeUsesInstr * byteCodeUse = IR::ByteCodeUsesInstr::New(m_func, Js::Constants::NoByteCodeOffset);
  2098. byteCodeUse->Set(src1Opnd);
  2099. this->AddInstr(byteCodeUse, Js::Constants::NoByteCodeOffset);
  2100. }
  2101. if (newConcatStrMulti)
  2102. {
  2103. // Allocate the concat str after the ConvToStr
  2104. this->AddInstr(newConcatStrMulti, Js::Constants::NoByteCodeOffset);
  2105. }
  2106. instr = IR::Instr::New(Js::OpCode::SetConcatStrMultiItem, indir1Opnd, str1Opnd, m_func);
  2107. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  2108. instr = IR::Instr::New(Js::OpCode::SetConcatStrMultiItem, indir2Opnd, str2Opnd, m_func);
  2109. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  2110. }
  2111. ///----------------------------------------------------------------------------
  2112. ///
  2113. /// IRBuilder::BuildReg5
  2114. ///
  2115. /// Build IR instr for a Reg5 instruction.
  2116. ///
  2117. ///----------------------------------------------------------------------------
  2118. template <typename SizePolicy>
  2119. void
  2120. IRBuilder::BuildReg5(Js::OpCode newOpcode, uint32 offset)
  2121. {
  2122. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  2123. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2124. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_Reg5<SizePolicy>>();
  2125. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  2126. {
  2127. this->DoClosureRegCheck(layout->R0);
  2128. this->DoClosureRegCheck(layout->R1);
  2129. this->DoClosureRegCheck(layout->R2);
  2130. this->DoClosureRegCheck(layout->R3);
  2131. this->DoClosureRegCheck(layout->R4);
  2132. }
  2133. BuildReg5(newOpcode, offset, layout->R0, layout->R1, layout->R2, layout->R3, layout->R4);
  2134. }
  2135. void
  2136. IRBuilder::BuildReg5(Js::OpCode newOpcode, uint32 offset, Js::RegSlot dstRegSlot, Js::RegSlot src1RegSlot,
  2137. Js::RegSlot src2RegSlot, Js::RegSlot src3RegSlot, Js::RegSlot src4RegSlot)
  2138. {
  2139. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2140. IR::Instr * instr;
  2141. IR::RegOpnd * dstOpnd;
  2142. IR::RegOpnd * src1Opnd;
  2143. IR::RegOpnd * src2Opnd;
  2144. IR::RegOpnd * src3Opnd;
  2145. IR::RegOpnd * src4Opnd;
  2146. // We can't support instructions with more than 2 srcs. Instead create a CallHelper instructions,
  2147. // and pass the srcs as ArgOut_A instructions.
  2148. src1Opnd = this->BuildSrcOpnd(src1RegSlot);
  2149. src2Opnd = this->BuildSrcOpnd(src2RegSlot);
  2150. src3Opnd = this->BuildSrcOpnd(src3RegSlot);
  2151. src4Opnd = this->BuildSrcOpnd(src4RegSlot);
  2152. dstOpnd = this->BuildDstOpnd(dstRegSlot);
  2153. instr = IR::Instr::New(Js::OpCode::ArgOut_A, IR::RegOpnd::New(TyVar, m_func), src4Opnd, m_func);
  2154. this->AddInstr(instr, offset);
  2155. instr = IR::Instr::New(Js::OpCode::ArgOut_A, IR::RegOpnd::New(TyVar, m_func), src3Opnd, instr->GetDst(), m_func);
  2156. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  2157. instr = IR::Instr::New(Js::OpCode::ArgOut_A, IR::RegOpnd::New(TyVar, m_func), src2Opnd, instr->GetDst(), m_func);
  2158. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  2159. instr = IR::Instr::New(Js::OpCode::ArgOut_A, IR::RegOpnd::New(TyVar, m_func), src1Opnd, instr->GetDst(), m_func);
  2160. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  2161. IR::HelperCallOpnd *helperOpnd;
  2162. switch (newOpcode) {
  2163. case Js::OpCode::ApplyArgs:
  2164. helperOpnd=IR::HelperCallOpnd::New(IR::HelperOp_OP_ApplyArgs, this->m_func);
  2165. break;
  2166. default:
  2167. AssertMsg(UNREACHED, "Unknown Reg5 opcode");
  2168. Fatal();
  2169. }
  2170. instr = IR::Instr::New(Js::OpCode::CallHelper, dstOpnd, helperOpnd, instr->GetDst(), m_func);
  2171. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  2172. }
  2173. void
  2174. IRBuilder::BuildW1(Js::OpCode newOpcode, uint32 offset)
  2175. {
  2176. Assert(!OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2177. unsigned short C1;
  2178. const unaligned Js::OpLayoutW1 *regLayout = m_jnReader.W1();
  2179. C1 = regLayout->C1;
  2180. IR::Instr * instr;
  2181. IntConstType value = C1;
  2182. IR::IntConstOpnd * srcOpnd;
  2183. srcOpnd = IR::IntConstOpnd::New(value, TyInt32, m_func);
  2184. instr = IR::Instr::New(newOpcode, m_func);
  2185. instr->SetSrc1(srcOpnd);
  2186. this->AddInstr(instr, offset);
  2187. if (newOpcode == Js::OpCode::RuntimeReferenceError || newOpcode == Js::OpCode::RuntimeTypeError)
  2188. {
  2189. if (DoBailOnNoProfile())
  2190. {
  2191. // RuntimeReferenceError are extremely rare as they are guaranteed to throw. Insert BailonNoProfile to optimize this code path.
  2192. // If there are continues bailout bailonnoprofile will be disabled.
  2193. InsertBailOnNoProfile(instr);
  2194. }
  2195. }
  2196. }
  2197. template <typename SizePolicy>
  2198. void
  2199. IRBuilder::BuildUnsigned1(Js::OpCode newOpcode, uint32 offset)
  2200. {
  2201. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  2202. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2203. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_Unsigned1<SizePolicy>>();
  2204. BuildUnsigned1(newOpcode, offset, layout->C1);
  2205. }
  2206. void
  2207. IRBuilder::BuildUnsigned1(Js::OpCode newOpcode, uint32 offset, uint32 num)
  2208. {
  2209. switch (newOpcode)
  2210. {
  2211. case Js::OpCode::EmitTmpRegCount:
  2212. // Note: EmitTmpRegCount is inserted when debugging, not needed for jit.
  2213. // It's only needed by the debugger to see how many tmp regs are active.
  2214. Assert(m_func->IsJitInDebugMode());
  2215. return;
  2216. case Js::OpCode::NewBlockScope:
  2217. case Js::OpCode::NewPseudoScope:
  2218. {
  2219. if (num >= m_func->GetJITFunctionBody()->GetInnerScopeCount())
  2220. {
  2221. Js::Throw::FatalInternalError();
  2222. }
  2223. Js::RegSlot dstRegSlot = num + m_func->GetJITFunctionBody()->GetFirstInnerScopeReg();
  2224. IR::RegOpnd * dstOpnd = BuildDstOpnd(dstRegSlot);
  2225. IR::Instr * instr = IR::Instr::New(newOpcode, dstOpnd, m_func);
  2226. this->AddInstr(instr, offset);
  2227. if (dstOpnd->m_sym->m_isSingleDef)
  2228. {
  2229. dstOpnd->m_sym->m_isNotNumber = true;
  2230. }
  2231. break;
  2232. }
  2233. case Js::OpCode::CloneInnerScopeSlots:
  2234. case Js::OpCode::CloneBlockScope:
  2235. {
  2236. if (num >= m_func->GetJITFunctionBody()->GetInnerScopeCount())
  2237. {
  2238. Js::Throw::FatalInternalError();
  2239. }
  2240. Js::RegSlot srcRegSlot = num + m_func->GetJITFunctionBody()->GetFirstInnerScopeReg();
  2241. IR::RegOpnd * srcOpnd = BuildSrcOpnd(srcRegSlot);
  2242. IR::Instr * instr = IR::Instr::New(newOpcode, m_func);
  2243. instr->SetSrc1(srcOpnd);
  2244. this->AddInstr(instr, offset);
  2245. break;
  2246. }
  2247. case Js::OpCode::ProfiledLoopBodyStart:
  2248. {
  2249. // This opcode is removed from the IR when we aren't doing Profiling SimpleJit or not jitting loop bodies
  2250. if (m_func->DoSimpleJitDynamicProfile() && m_func->GetJITFunctionBody()->DoJITLoopBody())
  2251. {
  2252. // Attach a register to the dest of this instruction to communicate whether we should bail out (the deciding of this is done in lowering)
  2253. IR::Opnd* fullJitExists = IR::RegOpnd::New(TyUint8, m_func);
  2254. auto start = m_lastInstr;
  2255. if (start->m_opcode == Js::OpCode::InitLoopBodyCount)
  2256. {
  2257. Assert(this->IsLoopBody());
  2258. start = start->m_prev;
  2259. }
  2260. Assert(start->m_opcode == Js::OpCode::ProfiledLoopStart && start->GetDst());
  2261. IR::JitProfilingInstr* instr = IR::JitProfilingInstr::New(Js::OpCode::ProfiledLoopBodyStart, fullJitExists, start->GetDst(), m_func);
  2262. // profileId is used here to represent the loop number
  2263. instr->loopNumber = num;
  2264. this->AddInstr(instr, offset);
  2265. // If fullJitExists isn't 0, bail out so that we can get the fulljitted version
  2266. BailOutInfo * bailOutInfo = JitAnew(m_func->m_alloc, BailOutInfo, instr->GetByteCodeOffset(), m_func);
  2267. IR::BailOutInstr * bailInstr = IR::BailOutInstr::New(Js::OpCode::BailOnNotEqual, IR::BailOnSimpleJitToFullJitLoopBody, bailOutInfo, bailOutInfo->bailOutFunc);
  2268. bailInstr->SetSrc1(fullJitExists);
  2269. bailInstr->SetSrc2(IR::IntConstOpnd::New(0, TyUint8, m_func, true));
  2270. this->AddInstr(bailInstr, offset);
  2271. }
  2272. Js::ImplicitCallFlags flags = Js::ImplicitCall_HasNoInfo;
  2273. Js::LoopFlags loopFlags;
  2274. if (this->m_func->HasProfileInfo())
  2275. {
  2276. flags = m_func->GetReadOnlyProfileInfo()->GetLoopImplicitCallFlags(num);
  2277. loopFlags = m_func->GetReadOnlyProfileInfo()->GetLoopFlags(num);
  2278. }
  2279. // Put a label the instruction stream to carry the profile info
  2280. IR::ProfiledLabelInstr * labelInstr = IR::ProfiledLabelInstr::New(Js::OpCode::Label, this->m_func, flags, loopFlags);
  2281. #if DBG
  2282. labelInstr->loopNum = num;
  2283. #endif
  2284. m_lastInstr->InsertAfter(labelInstr);
  2285. m_lastInstr = labelInstr;
  2286. // Set it to the offset to the start of the loop
  2287. labelInstr->SetByteCodeOffset(m_jnReader.GetCurrentOffset());
  2288. break;
  2289. }
  2290. case Js::OpCode::LoopBodyStart:
  2291. break;
  2292. case Js::OpCode::ProfiledLoopStart:
  2293. {
  2294. AssertOrFailFast(num < m_func->GetJITFunctionBody()->GetLoopCount());
  2295. // If we're in profiling SimpleJit and jitting loop bodies, we need to keep this until lowering.
  2296. if (m_func->DoSimpleJitDynamicProfile() && m_func->GetJITFunctionBody()->DoJITLoopBody())
  2297. {
  2298. // In order for the JIT engine to correctly allocate registers we need to have this set up before lowering.
  2299. // There may be 0 to many LoopEnds, but there will only ever be one LoopStart
  2300. Assert(!this->m_saveLoopImplicitCallFlags[num]);
  2301. const auto ty = Lowerer::GetImplicitCallFlagsType();
  2302. auto saveOpnd = IR::RegOpnd::New(ty, m_func);
  2303. this->m_saveLoopImplicitCallFlags[num] = saveOpnd;
  2304. // Note that we insert this instruction /before/ the actual ProfiledLoopStart opcode. This is because Lowering is backwards
  2305. // and this is just a fake instruction which is only used to pass on the saveOpnd; this instruction will eventually be removed.
  2306. auto instr = IR::JitProfilingInstr::New(Js::OpCode::Ld_A, saveOpnd, IR::MemRefOpnd::New((intptr_t)0, ty, m_func), m_func);
  2307. instr->isLoopHelper = true;
  2308. this->AddInstr(instr, offset);
  2309. instr = IR::JitProfilingInstr::New(Js::OpCode::ProfiledLoopStart, IR::RegOpnd::New(TyMachPtr, m_func), nullptr, m_func);
  2310. instr->loopNumber = num;
  2311. this->AddInstr(instr, offset);
  2312. }
  2313. if (this->IsLoopBody() && !m_loopCounterSym)
  2314. {
  2315. InsertInitLoopBodyLoopCounter(num);
  2316. }
  2317. break;
  2318. }
  2319. case Js::OpCode::ProfiledLoopEnd:
  2320. {
  2321. AssertOrFailFast(num < m_func->GetJITFunctionBody()->GetLoopCount());
  2322. // TODO: Decide whether we want the implicit loop call flags to be recorded in simplejitted loop bodies
  2323. if (m_func->DoSimpleJitDynamicProfile() && m_func->GetJITFunctionBody()->DoJITLoopBody())
  2324. {
  2325. Assert(this->m_saveLoopImplicitCallFlags[num]);
  2326. // In profiling simplejit we need this opcode in order to restore the implicit call flags
  2327. auto instr = IR::JitProfilingInstr::New(Js::OpCode::ProfiledLoopEnd, nullptr, this->m_saveLoopImplicitCallFlags[num], m_func);
  2328. this->AddInstr(instr, offset);
  2329. instr->loopNumber = num;
  2330. }
  2331. if (!this->IsLoopBody())
  2332. {
  2333. break;
  2334. }
  2335. // In the early exit case (return), we generated ProfiledLoopEnd for all the outer loop.
  2336. // If we see one of these profile loop, just load the IP of the immediate outer loop of the loop body being JIT'ed
  2337. // and then skip all the other loops using the fact that we have already loaded the return IP
  2338. if (IsLoopBodyReturnIPInstr(m_lastInstr))
  2339. {
  2340. // Already loaded the loop IP sym, skip
  2341. break;
  2342. }
  2343. // See we are ending an outer loop and load the return IP to the ProfiledLoopEnd opcode
  2344. // instead of following the normal branch
  2345. const JITLoopHeaderIDL * loopHeader = m_func->GetJITFunctionBody()->GetLoopHeaderData(num);
  2346. if (m_func->GetJITFunctionBody()->GetLoopHeaderAddr(num) != m_func->m_workItem->GetLoopHeaderAddr() &&
  2347. JITTimeFunctionBody::LoopContains(loopHeader, m_func->m_workItem->GetLoopHeader()))
  2348. {
  2349. this->InsertLoopBodyReturnIPInstr(offset, offset);
  2350. }
  2351. else
  2352. {
  2353. Assert(JITTimeFunctionBody::LoopContains(m_func->m_workItem->GetLoopHeader(), loopHeader));
  2354. }
  2355. break;
  2356. }
  2357. case Js::OpCode::InvalCachedScope:
  2358. {
  2359. // The reg and constant are both src operands.
  2360. IR::Instr* instr = IR::Instr::New(Js::OpCode::InvalCachedScope, m_func);
  2361. IR::RegOpnd *envOpnd = this->BuildSrcOpnd(m_func->GetJITFunctionBody()->GetEnvReg());
  2362. instr->SetSrc1(envOpnd);
  2363. IR::IntConstOpnd *envIndex = IR::IntConstOpnd::New(num, TyInt32, m_func, true);
  2364. instr->SetSrc2(envIndex);
  2365. this->AddInstr(instr, offset);
  2366. return;
  2367. }
  2368. default:
  2369. Assert(false);
  2370. __assume(false);
  2371. }
  2372. }
  2373. template <typename SizePolicy>
  2374. void
  2375. IRBuilder::BuildProfiledReg1Unsigned1(Js::OpCode newOpcode, uint32 offset)
  2376. {
  2377. Assert(OpCodeAttr::IsProfiledOp(newOpcode));
  2378. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2379. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_Reg1Unsigned1<SizePolicy>>>();
  2380. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  2381. {
  2382. this->DoClosureRegCheck(layout->R0);
  2383. }
  2384. BuildProfiledReg1Unsigned1(newOpcode, offset, layout->R0, layout->C1, layout->profileId);
  2385. }
  2386. void
  2387. IRBuilder::BuildProfiledReg1Unsigned1(Js::OpCode newOpcode, uint32 offset, Js::RegSlot R0, int32 C1, Js::ProfileId profileId)
  2388. {
  2389. Assert(newOpcode == Js::OpCode::ProfiledNewScArray || newOpcode == Js::OpCode::ProfiledInitForInEnumerator);
  2390. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(newOpcode);
  2391. if (newOpcode == Js::OpCode::InitForInEnumerator)
  2392. {
  2393. IR::RegOpnd * src1Opnd = this->BuildSrcOpnd(R0);
  2394. IR::Opnd * src2Opnd = this->BuildForInEnumeratorOpnd(C1);
  2395. IR::Instr *instr = IR::ProfiledInstr::New(Js::OpCode::InitForInEnumerator, nullptr, src1Opnd, src2Opnd, m_func);
  2396. instr->AsProfiledInstr()->u.profileId = profileId;
  2397. this->AddInstr(instr, offset);
  2398. return;
  2399. }
  2400. IR::Instr *instr;
  2401. Js::RegSlot dstRegSlot = R0;
  2402. IR::RegOpnd * dstOpnd = this->BuildDstOpnd(dstRegSlot);
  2403. StackSym * dstSym = dstOpnd->m_sym;
  2404. int32 value = C1;
  2405. IR::IntConstOpnd * srcOpnd;
  2406. srcOpnd = IR::IntConstOpnd::New(value, TyInt32, m_func);
  2407. if (m_func->DoSimpleJitDynamicProfile())
  2408. {
  2409. instr = IR::JitProfilingInstr::New(newOpcode, dstOpnd, srcOpnd, m_func);
  2410. instr->AsJitProfilingInstr()->profileId = profileId;
  2411. }
  2412. else
  2413. {
  2414. instr = IR::ProfiledInstr::New(newOpcode, dstOpnd, srcOpnd, m_func);
  2415. instr->AsProfiledInstr()->u.profileId = profileId;
  2416. }
  2417. this->AddInstr(instr, offset);
  2418. if (dstSym->m_isSingleDef)
  2419. {
  2420. dstSym->m_isSafeThis = true;
  2421. dstSym->m_isNotNumber = true;
  2422. }
  2423. // Undefined values in array literals ([0, undefined, 1]) are treated as missing values in some versions
  2424. Js::ArrayCallSiteInfo *arrayInfo = nullptr;
  2425. if (m_func->HasArrayInfo())
  2426. {
  2427. arrayInfo = m_func->GetReadOnlyProfileInfo()->GetArrayCallSiteInfo(profileId);
  2428. }
  2429. Js::TypeId arrayTypeId = Js::TypeIds_Array;
  2430. if (arrayInfo && !m_func->IsJitInDebugMode() && Js::JavascriptArray::HasInlineHeadSegment(value))
  2431. {
  2432. if (arrayInfo->IsNativeIntArray())
  2433. {
  2434. arrayTypeId = Js::TypeIds_NativeIntArray;
  2435. }
  2436. else if (arrayInfo->IsNativeFloatArray())
  2437. {
  2438. arrayTypeId = Js::TypeIds_NativeFloatArray;
  2439. }
  2440. }
  2441. dstOpnd->SetValueType(ValueType::GetObject(ObjectType::Array).SetHasNoMissingValues(true).SetArrayTypeId(arrayTypeId));
  2442. if (dstOpnd->GetValueType().HasVarElements())
  2443. {
  2444. dstOpnd->SetValueTypeFixed();
  2445. }
  2446. else
  2447. {
  2448. dstOpnd->SetValueType(dstOpnd->GetValueType().ToLikely());
  2449. }
  2450. }
  2451. template <typename SizePolicy>
  2452. void
  2453. IRBuilder::BuildReg1Unsigned1(Js::OpCode newOpcode, uint32 offset)
  2454. {
  2455. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  2456. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2457. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_Reg1Unsigned1<SizePolicy>>();
  2458. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  2459. {
  2460. this->DoClosureRegCheck(layout->R0);
  2461. }
  2462. BuildReg1Unsigned1(newOpcode, offset, layout->R0, layout->C1);
  2463. }
  2464. void
  2465. IRBuilder::BuildReg1Unsigned1(Js::OpCode newOpcode, uint offset, Js::RegSlot R0, int32 C1)
  2466. {
  2467. switch (newOpcode)
  2468. {
  2469. case Js::OpCode::NewRegEx:
  2470. this->BuildRegexFromPattern(R0, C1, offset);
  2471. return;
  2472. case Js::OpCode::LdInnerScope:
  2473. {
  2474. IR::RegOpnd * srcOpnd = BuildSrcOpnd(this->InnerScopeIndexToRegSlot(C1));
  2475. IR::RegOpnd * dstOpnd = BuildDstOpnd(R0);
  2476. IR::Instr * instr = IR::Instr::New(Js::OpCode::Ld_A, dstOpnd, srcOpnd, m_func);
  2477. if (dstOpnd->m_sym->m_isSingleDef)
  2478. {
  2479. dstOpnd->m_sym->m_isNotNumber = true;
  2480. }
  2481. this->AddInstr(instr, offset);
  2482. return;
  2483. }
  2484. case Js::OpCode::LdIndexedFrameDisplayNoParent:
  2485. {
  2486. newOpcode = Js::OpCode::LdFrameDisplay;
  2487. IR::RegOpnd *srcOpnd = this->BuildSrcOpnd(this->InnerScopeIndexToRegSlot(C1));
  2488. IR::RegOpnd *dstOpnd = this->BuildDstOpnd(R0);
  2489. IR::Instr *instr = IR::Instr::New(newOpcode, dstOpnd, srcOpnd, m_func);
  2490. this->AddEnvOpndForInnerFrameDisplay(instr, offset);
  2491. if (dstOpnd->m_sym->m_isSingleDef)
  2492. {
  2493. dstOpnd->m_sym->m_isNotNumber = true;
  2494. }
  2495. this->AddInstr(instr, offset);
  2496. return;
  2497. }
  2498. case Js::OpCode::GetCachedFunc:
  2499. {
  2500. IR::RegOpnd *src1Opnd = this->BuildSrcOpnd(m_func->GetJITFunctionBody()->GetLocalClosureReg());
  2501. IR::Opnd *src2Opnd = IR::IntConstOpnd::New(C1, TyUint32, m_func);
  2502. IR::RegOpnd *dstOpnd = this->BuildDstOpnd(R0);
  2503. IR::Instr *instr = IR::Instr::New(newOpcode, dstOpnd, src1Opnd, src2Opnd, m_func);
  2504. if (dstOpnd->m_sym->m_isSingleDef)
  2505. {
  2506. dstOpnd->m_sym->m_isNotNumber = true;
  2507. }
  2508. this->AddInstr(instr, offset);
  2509. return;
  2510. }
  2511. case Js::OpCode::InitForInEnumerator:
  2512. {
  2513. IR::Instr *instr = IR::Instr::New(Js::OpCode::InitForInEnumerator, m_func);
  2514. instr->SetSrc1(this->BuildSrcOpnd(R0));
  2515. instr->SetSrc2(this->BuildForInEnumeratorOpnd(C1));
  2516. this->AddInstr(instr, offset);
  2517. return;
  2518. }
  2519. }
  2520. IR::RegOpnd * dstOpnd = this->BuildDstOpnd(R0);
  2521. StackSym * dstSym = dstOpnd->m_sym;
  2522. IntConstType value = C1;
  2523. IR::IntConstOpnd * srcOpnd = IR::IntConstOpnd::New(value, TyInt32, m_func);
  2524. IR::Instr * instr = IR::Instr::New(newOpcode, dstOpnd, srcOpnd, m_func);
  2525. this->AddInstr(instr, offset);
  2526. if (newOpcode == Js::OpCode::NewScopeSlots)
  2527. {
  2528. this->AddInstr(
  2529. IR::Instr::New(
  2530. Js::OpCode::Ld_A, IR::RegOpnd::New(m_func->GetLocalClosureSym(), TyVar, m_func), dstOpnd, m_func),
  2531. (uint32)-1);
  2532. }
  2533. if (dstSym->m_isSingleDef)
  2534. {
  2535. switch (newOpcode)
  2536. {
  2537. case Js::OpCode::NewScArray:
  2538. case Js::OpCode::NewScArrayWithMissingValues:
  2539. dstSym->m_isSafeThis = true;
  2540. dstSym->m_isNotNumber = true;
  2541. break;
  2542. }
  2543. }
  2544. if (newOpcode == Js::OpCode::NewScArray || newOpcode == Js::OpCode::NewScArrayWithMissingValues)
  2545. {
  2546. // Undefined values in array literals ([0, undefined, 1]) are treated as missing values in some versions
  2547. dstOpnd->SetValueType(
  2548. ValueType::GetObject(ObjectType::Array)
  2549. .SetHasNoMissingValues(newOpcode == Js::OpCode::NewScArray)
  2550. .SetArrayTypeId(Js::TypeIds_Array));
  2551. dstOpnd->SetValueTypeFixed();
  2552. }
  2553. }
  2554. ///----------------------------------------------------------------------------
  2555. ///
  2556. /// IRBuilder::BuildReg2Int1
  2557. ///
  2558. /// Build IR instr for a Reg2I4 instruction.
  2559. ///
  2560. ///----------------------------------------------------------------------------
  2561. template <typename SizePolicy>
  2562. void
  2563. IRBuilder::BuildReg2Int1(Js::OpCode newOpcode, uint32 offset)
  2564. {
  2565. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2566. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_Reg2Int1<SizePolicy>>();
  2567. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  2568. {
  2569. this->DoClosureRegCheck(layout->R0);
  2570. this->DoClosureRegCheck(layout->R1);
  2571. }
  2572. BuildReg2Int1(newOpcode, offset, layout->R0, layout->R1, layout->C1);
  2573. }
  2574. void
  2575. IRBuilder::BuildReg2Int1(Js::OpCode newOpcode, uint32 offset, Js::RegSlot dstRegSlot, Js::RegSlot srcRegSlot, int32 value)
  2576. {
  2577. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2578. IR::Instr * instr;
  2579. if (newOpcode == Js::OpCode::LdIndexedFrameDisplay)
  2580. {
  2581. newOpcode = Js::OpCode::LdFrameDisplay;
  2582. if ((uint)value >= m_func->GetJITFunctionBody()->GetInnerScopeCount())
  2583. {
  2584. Js::Throw::FatalInternalError();
  2585. }
  2586. IR::RegOpnd *src1Opnd = this->BuildSrcOpnd(value + m_func->GetJITFunctionBody()->GetFirstInnerScopeReg());
  2587. IR::RegOpnd *src2Opnd = this->BuildSrcOpnd(srcRegSlot);
  2588. IR::RegOpnd *dstOpnd = this->BuildDstOpnd(dstRegSlot);
  2589. instr = IR::Instr::New(newOpcode, dstOpnd, src1Opnd, src2Opnd, m_func);
  2590. if (dstOpnd->m_sym->m_isSingleDef)
  2591. {
  2592. dstOpnd->m_sym->m_isNotNumber = true;
  2593. }
  2594. this->AddInstr(instr, offset);
  2595. return;
  2596. }
  2597. IR::RegOpnd * src1Opnd = this->BuildSrcOpnd(srcRegSlot);
  2598. IR::IntConstOpnd * src2Opnd = IR::IntConstOpnd::New(value, TyInt32, m_func);
  2599. IR::RegOpnd * dstOpnd = this->BuildDstOpnd(dstRegSlot);
  2600. switch (newOpcode)
  2601. {
  2602. case Js::OpCode::ProfiledLdThis:
  2603. newOpcode = Js::OpCode::LdThis;
  2604. if(m_func->HasProfileInfo())
  2605. {
  2606. dstOpnd->SetValueType(m_func->GetReadOnlyProfileInfo()->GetThisInfo().valueType);
  2607. }
  2608. if(m_func->DoSimpleJitDynamicProfile())
  2609. {
  2610. instr = IR::JitProfilingInstr::New(newOpcode, dstOpnd, src1Opnd, src2Opnd, m_func);
  2611. // Break out since we just made the instr
  2612. break;
  2613. }
  2614. // fall-through
  2615. default:
  2616. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  2617. instr = IR::Instr::New(newOpcode, dstOpnd, src1Opnd, src2Opnd, m_func);
  2618. break;
  2619. }
  2620. this->AddInstr(instr, offset);
  2621. }
  2622. ///----------------------------------------------------------------------------
  2623. ///
  2624. /// IRBuilder::BuildElementC
  2625. ///
  2626. /// Build IR instr for an ElementC instruction.
  2627. ///
  2628. ///----------------------------------------------------------------------------
  2629. template <typename SizePolicy>
  2630. void
  2631. IRBuilder::BuildElementScopedC(Js::OpCode newOpcode, uint32 offset)
  2632. {
  2633. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  2634. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2635. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementScopedC<SizePolicy>>();
  2636. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  2637. {
  2638. this->DoClosureRegCheck(layout->Value);
  2639. }
  2640. BuildElementScopedC(newOpcode, offset, layout->Value, layout->PropertyIdIndex);
  2641. }
  2642. void
  2643. IRBuilder::BuildElementScopedC(Js::OpCode newOpcode, uint32 offset, Js::RegSlot regSlot, Js::PropertyIdIndexType propertyIdIndex)
  2644. {
  2645. IR::Instr * instr;
  2646. Js::PropertyId propertyId = m_func->GetJITFunctionBody()->GetReferencedPropertyId(propertyIdIndex);
  2647. PropertyKind propertyKind = PropertyKindData;
  2648. IR::RegOpnd * regOpnd;
  2649. Js::RegSlot fieldRegSlot = this->GetEnvRegForEvalCode();
  2650. IR::SymOpnd * fieldSymOpnd = this->BuildFieldOpnd(newOpcode, fieldRegSlot, propertyId, propertyIdIndex, propertyKind);
  2651. switch (newOpcode)
  2652. {
  2653. case Js::OpCode::ScopedEnsureNoRedeclFld:
  2654. {
  2655. regOpnd = this->BuildSrcOpnd(regSlot);
  2656. instr = IR::Instr::New(newOpcode, fieldSymOpnd, regOpnd, m_func);
  2657. break;
  2658. }
  2659. case Js::OpCode::ScopedDeleteFld:
  2660. case Js::OpCode::ScopedDeleteFldStrict:
  2661. {
  2662. Assert(this->m_func->GetScriptContextInfo()->GetAddr() == this->m_func->GetTopFunc()->GetScriptContextInfo()->GetAddr());
  2663. regOpnd = this->BuildDstOpnd(regSlot);
  2664. instr = IR::Instr::New(newOpcode, regOpnd, fieldSymOpnd, m_func);
  2665. break;
  2666. }
  2667. case Js::OpCode::ScopedInitFunc:
  2668. {
  2669. // Implicit root object as default instance
  2670. IR::Opnd * instance2Opnd = this->BuildSrcOpnd(Js::FunctionBody::RootObjectRegSlot);
  2671. regOpnd = this->BuildSrcOpnd(regSlot);
  2672. instr = IR::Instr::New(newOpcode, fieldSymOpnd, regOpnd, instance2Opnd, m_func);
  2673. break;
  2674. }
  2675. default:
  2676. AssertMsg(UNREACHED, "Unknown ElementScopedC opcode");
  2677. Fatal();
  2678. }
  2679. this->AddInstr(instr, offset);
  2680. }
  2681. template <typename SizePolicy>
  2682. void
  2683. IRBuilder::BuildElementC(Js::OpCode newOpcode, uint32 offset)
  2684. {
  2685. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  2686. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2687. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementC<SizePolicy>>();
  2688. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  2689. {
  2690. this->DoClosureRegCheck(layout->Value);
  2691. this->DoClosureRegCheck(layout->Instance);
  2692. }
  2693. BuildElementC(newOpcode, offset, layout->Instance, layout->Value, layout->PropertyIdIndex);
  2694. }
  2695. void
  2696. IRBuilder::BuildElementC(Js::OpCode newOpcode, uint32 offset, Js::RegSlot fieldRegSlot, Js::RegSlot regSlot, Js::PropertyIdIndexType propertyIdIndex)
  2697. {
  2698. IR::Instr * instr;
  2699. Js::PropertyId propertyId = m_func->GetJITFunctionBody()->GetReferencedPropertyId(propertyIdIndex);
  2700. PropertyKind propertyKind = PropertyKindData;
  2701. IR::SymOpnd * fieldSymOpnd = this->BuildFieldOpnd(newOpcode, fieldRegSlot, propertyId, propertyIdIndex, propertyKind);
  2702. IR::RegOpnd * regOpnd;
  2703. switch (newOpcode)
  2704. {
  2705. case Js::OpCode::DeleteFld:
  2706. case Js::OpCode::DeleteRootFld:
  2707. case Js::OpCode::DeleteFldStrict:
  2708. case Js::OpCode::DeleteRootFldStrict:
  2709. // Load
  2710. regOpnd = this->BuildDstOpnd(regSlot);
  2711. instr = IR::Instr::New(newOpcode, regOpnd, fieldSymOpnd, m_func);
  2712. break;
  2713. case Js::OpCode::InitSetFld:
  2714. case Js::OpCode::InitGetFld:
  2715. case Js::OpCode::InitClassMemberGet:
  2716. case Js::OpCode::InitClassMemberSet:
  2717. case Js::OpCode::InitProto:
  2718. case Js::OpCode::StFuncExpr:
  2719. // Store
  2720. regOpnd = this->BuildSrcOpnd(regSlot);
  2721. instr = IR::Instr::New(newOpcode, fieldSymOpnd, regOpnd, m_func);
  2722. break;
  2723. default:
  2724. AssertMsg(UNREACHED, "Unknown ElementC opcode");
  2725. Fatal();
  2726. }
  2727. this->AddInstr(instr, offset);
  2728. }
  2729. ///----------------------------------------------------------------------------
  2730. ///
  2731. /// IRBuilder::BuildElementSlot
  2732. ///
  2733. /// Build IR instr for an ElementSlot instruction.
  2734. ///
  2735. ///----------------------------------------------------------------------------
  2736. IR::Instr *
  2737. IRBuilder::BuildProfiledSlotLoad(Js::OpCode loadOp, IR::RegOpnd *dstOpnd, IR::SymOpnd *srcOpnd, Js::ProfileId profileId, bool *pUnprofiled)
  2738. {
  2739. IR::Instr * instr = nullptr;
  2740. if (m_func->DoSimpleJitDynamicProfile())
  2741. {
  2742. instr = IR::JitProfilingInstr::New(loadOp, dstOpnd, srcOpnd, m_func);
  2743. instr->AsJitProfilingInstr()->profileId = profileId;
  2744. }
  2745. else if(this->m_func->HasProfileInfo())
  2746. {
  2747. instr = IR::ProfiledInstr::New(loadOp, dstOpnd, srcOpnd, m_func);
  2748. instr->AsProfiledInstr()->u.FldInfo().valueType =
  2749. this->m_func->GetReadOnlyProfileInfo()->GetSlotLoad(profileId);
  2750. *pUnprofiled = instr->AsProfiledInstr()->u.FldInfo().valueType.IsUninitialized();
  2751. #if ENABLE_DEBUG_CONFIG_OPTIONS
  2752. if(Js::Configuration::Global.flags.TestTrace.IsEnabled(Js::DynamicProfilePhase))
  2753. {
  2754. const ValueType valueType(instr->AsProfiledInstr()->u.FldInfo().valueType);
  2755. char valueTypeStr[VALUE_TYPE_MAX_STRING_SIZE];
  2756. valueType.ToString(valueTypeStr);
  2757. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  2758. Output::Print(_u("TestTrace function %s (#%s) ValueType = %S "), m_func->GetJITFunctionBody()->GetDisplayName(), m_func->GetDebugNumberSet(debugStringBuffer), valueTypeStr);
  2759. instr->DumpTestTrace();
  2760. }
  2761. #endif
  2762. }
  2763. return instr;
  2764. }
  2765. template <typename SizePolicy>
  2766. void
  2767. IRBuilder::BuildElementSlot(Js::OpCode newOpcode, uint32 offset)
  2768. {
  2769. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  2770. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2771. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementSlot<SizePolicy>>();
  2772. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  2773. {
  2774. this->DoClosureRegCheck(layout->Value);
  2775. this->DoClosureRegCheck(layout->Instance);
  2776. }
  2777. BuildElementSlot(newOpcode, offset, layout->Instance, layout->Value, layout->SlotIndex, Js::Constants::NoProfileId);
  2778. }
  2779. template <typename SizePolicy>
  2780. void
  2781. IRBuilder::BuildProfiledElementSlot(Js::OpCode newOpcode, uint32 offset)
  2782. {
  2783. Assert(OpCodeAttr::IsProfiledOp(newOpcode));
  2784. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2785. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_ElementSlot<SizePolicy>>>();
  2786. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  2787. {
  2788. this->DoClosureRegCheck(layout->Value);
  2789. this->DoClosureRegCheck(layout->Instance);
  2790. }
  2791. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(newOpcode);
  2792. BuildElementSlot(newOpcode, offset, layout->Instance, layout->Value, layout->SlotIndex, layout->profileId);
  2793. }
  2794. void
  2795. IRBuilder::BuildElementSlot(Js::OpCode newOpcode, uint32 offset, Js::RegSlot fieldRegSlot, Js::RegSlot regSlot,
  2796. int32 slotId, Js::ProfileId profileId)
  2797. {
  2798. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2799. IR::Instr * instr;
  2800. IR::RegOpnd * regOpnd;
  2801. IR::SymOpnd * fieldSymOpnd;
  2802. PropertyKind propertyKind = PropertyKindSlots;
  2803. PropertySym * fieldSym;
  2804. StackSym * stackFuncPtrSym = nullptr;
  2805. bool isLdSlotThatWasNotProfiled = false;
  2806. switch (newOpcode)
  2807. {
  2808. case Js::OpCode::NewInnerStackScFunc:
  2809. stackFuncPtrSym = this->EnsureStackFuncPtrSym();
  2810. // fall through
  2811. case Js::OpCode::NewInnerScFunc:
  2812. newOpcode = Js::OpCode::NewScFunc;
  2813. goto NewScFuncCommon;
  2814. case Js::OpCode::NewInnerScGenFunc:
  2815. newOpcode = Js::OpCode::NewScGenFunc;
  2816. NewScFuncCommon:
  2817. {
  2818. IR::Opnd * functionBodySlotOpnd = IR::IntConstOpnd::New(slotId, TyInt32, m_func, true);
  2819. IR::Opnd * environmentOpnd = this->BuildSrcOpnd(fieldRegSlot);
  2820. regOpnd = this->BuildDstOpnd(regSlot);
  2821. if (stackFuncPtrSym)
  2822. {
  2823. IR::RegOpnd * dataOpnd = IR::RegOpnd::New(TyVar, m_func);
  2824. instr = IR::Instr::New(Js::OpCode::NewScFuncData, dataOpnd, environmentOpnd, IR::RegOpnd::New(stackFuncPtrSym, TyVar, m_func), m_func);
  2825. this->AddInstr(instr, offset);
  2826. instr = IR::Instr::New(newOpcode, regOpnd, functionBodySlotOpnd, dataOpnd, m_func);
  2827. }
  2828. else
  2829. {
  2830. instr = IR::Instr::New(newOpcode, regOpnd, functionBodySlotOpnd, environmentOpnd, m_func);
  2831. }
  2832. if (regOpnd->m_sym->m_isSingleDef)
  2833. {
  2834. regOpnd->m_sym->m_isSafeThis = true;
  2835. regOpnd->m_sym->m_isNotNumber = true;
  2836. }
  2837. this->AddInstr(instr, offset);
  2838. return;
  2839. }
  2840. case Js::OpCode::NewScFuncHomeObj:
  2841. case Js::OpCode::NewScGenFuncHomeObj:
  2842. {
  2843. Js::FunctionInfoPtrPtr infoRef = m_func->GetJITFunctionBody()->GetNestedFuncRef(slotId);
  2844. IR::AddrOpnd * functionBodySlotOpnd = IR::AddrOpnd::New((Js::Var)infoRef, IR::AddrOpndKindDynamicMisc, m_func);
  2845. IR::Opnd * environmentOpnd = GetEnvironmentOperand(offset);
  2846. IR::Opnd * homeObjOpnd = this->BuildSrcOpnd(fieldRegSlot);
  2847. regOpnd = this->BuildDstOpnd(regSlot);
  2848. instr = IR::Instr::New(Js::OpCode::ExtendArg_A, IR::RegOpnd::New(TyVar, m_func), homeObjOpnd, m_func);
  2849. this->AddInstr(instr, offset);
  2850. instr = IR::Instr::New(Js::OpCode::ExtendArg_A, IR::RegOpnd::New(TyVar, m_func), functionBodySlotOpnd, instr->GetDst(), m_func);
  2851. this->AddInstr(instr, offset);
  2852. instr = IR::Instr::New(Js::OpCode::ExtendArg_A, IR::RegOpnd::New(TyVar, m_func), environmentOpnd, instr->GetDst(), m_func);
  2853. this->AddInstr(instr, offset);
  2854. instr = IR::Instr::New(newOpcode, regOpnd, instr->GetDst(), m_func);
  2855. if (regOpnd->m_sym->m_isSingleDef)
  2856. {
  2857. regOpnd->m_sym->m_isSafeThis = true;
  2858. regOpnd->m_sym->m_isNotNumber = true;
  2859. }
  2860. this->AddInstr(instr, offset);
  2861. return;
  2862. }
  2863. case Js::OpCode::LdObjSlot:
  2864. newOpcode = Js::OpCode::LdSlot;
  2865. goto ObjSlotCommon;
  2866. case Js::OpCode::StObjSlot:
  2867. newOpcode = Js::OpCode::StSlot;
  2868. goto ObjSlotCommon;
  2869. case Js::OpCode::StObjSlotChkUndecl:
  2870. newOpcode = Js::OpCode::StSlotChkUndecl;
  2871. ObjSlotCommon:
  2872. regOpnd = IR::RegOpnd::New(TyVar, m_func);
  2873. fieldSymOpnd = this->BuildFieldOpnd(newOpcode, fieldRegSlot, (Js::DynamicObject::GetOffsetOfAuxSlots())/sizeof(Js::Var), (Js::PropertyIdIndexType)-1, PropertyKindSlotArray);
  2874. instr = IR::Instr::New(Js::OpCode::LdSlotArr, regOpnd, fieldSymOpnd, m_func);
  2875. this->AddInstr(instr, offset);
  2876. fieldSym = PropertySym::New(regOpnd->m_sym, slotId, (uint32)-1, (uint)-1, PropertyKindSlots, m_func);
  2877. fieldSymOpnd = IR::SymOpnd::New(fieldSym, TyVar, m_func);
  2878. if (newOpcode == Js::OpCode::StSlot || newOpcode == Js::OpCode::StSlotChkUndecl)
  2879. {
  2880. goto StSlotCommon;
  2881. }
  2882. goto LdSlotCommon;
  2883. case Js::OpCode::LdSlotArr:
  2884. propertyKind = PropertyKindSlotArray;
  2885. case Js::OpCode::LdSlot:
  2886. // Load
  2887. fieldSymOpnd = this->BuildFieldOpnd(newOpcode, fieldRegSlot, slotId, (Js::PropertyIdIndexType)-1, propertyKind);
  2888. LdSlotCommon:
  2889. regOpnd = this->BuildDstOpnd(regSlot);
  2890. instr = nullptr;
  2891. if (profileId != Js::Constants::NoProfileId)
  2892. {
  2893. instr = this->BuildProfiledSlotLoad(newOpcode, regOpnd, fieldSymOpnd, profileId, &isLdSlotThatWasNotProfiled);
  2894. }
  2895. if (!instr)
  2896. {
  2897. instr = IR::Instr::New(newOpcode, regOpnd, fieldSymOpnd, m_func);
  2898. }
  2899. break;
  2900. case Js::OpCode::StSlot:
  2901. case Js::OpCode::StSlotChkUndecl:
  2902. // Store
  2903. fieldSymOpnd = this->BuildFieldOpnd(newOpcode, fieldRegSlot, slotId, (Js::PropertyIdIndexType)-1, propertyKind);
  2904. StSlotCommon:
  2905. regOpnd = this->BuildSrcOpnd(regSlot);
  2906. instr = IR::Instr::New(newOpcode, fieldSymOpnd, regOpnd, m_func);
  2907. if (newOpcode == Js::OpCode::StSlotChkUndecl)
  2908. {
  2909. // ChkUndecl includes an implicit read of the destination. Communicate the liveness by using the destination in src2.
  2910. instr->SetSrc2(fieldSymOpnd);
  2911. }
  2912. break;
  2913. case Js::OpCode::StPropIdArrFromVar:
  2914. {
  2915. IR::RegOpnd * src0Opnd = this->BuildSrcOpnd(fieldRegSlot);
  2916. IR::RegOpnd * src1Opnd = this->BuildSrcOpnd(regSlot);
  2917. IntConstType value = slotId;
  2918. IR::IntConstOpnd * valOpnd = IR::IntConstOpnd::New(value, TyInt32, m_func);
  2919. instr = IR::Instr::New(Js::OpCode::ExtendArg_A, IR::RegOpnd::New(TyVar, m_func), src1Opnd, m_func);
  2920. this->AddInstr(instr, offset);
  2921. offset = Js::Constants::NoByteCodeOffset;
  2922. instr = IR::Instr::New(Js::OpCode::ExtendArg_A, IR::RegOpnd::New(TyVar, m_func), valOpnd, instr->GetDst(), m_func);
  2923. this->AddInstr(instr, offset);
  2924. instr = IR::Instr::New(Js::OpCode::ExtendArg_A, IR::RegOpnd::New(TyVar, m_func), src0Opnd, instr->GetDst(), m_func);
  2925. this->AddInstr(instr, offset);
  2926. IR::Opnd * firstArg = instr->GetDst();
  2927. instr = IR::Instr::New(newOpcode, m_func);
  2928. instr->SetSrc1(firstArg);
  2929. break;
  2930. }
  2931. default:
  2932. AssertMsg(UNREACHED, "Unknown ElementSlot opcode");
  2933. Fatal();
  2934. }
  2935. this->AddInstr(instr, offset);
  2936. if(isLdSlotThatWasNotProfiled && DoBailOnNoProfile())
  2937. {
  2938. InsertBailOnNoProfile(instr);
  2939. }
  2940. }
  2941. template <typename SizePolicy>
  2942. void
  2943. IRBuilder::BuildElementSlotI1(Js::OpCode newOpcode, uint32 offset)
  2944. {
  2945. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  2946. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2947. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementSlotI1<SizePolicy>>();
  2948. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  2949. {
  2950. this->DoClosureRegCheck(layout->Value);
  2951. }
  2952. BuildElementSlotI1(newOpcode, offset, layout->Value, layout->SlotIndex, Js::Constants::NoProfileId);
  2953. }
  2954. template <typename SizePolicy>
  2955. void
  2956. IRBuilder::BuildProfiledElementSlotI1(Js::OpCode newOpcode, uint32 offset)
  2957. {
  2958. Assert(OpCodeAttr::IsProfiledOp(newOpcode));
  2959. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2960. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_ElementSlotI1<SizePolicy>>>();
  2961. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  2962. {
  2963. this->DoClosureRegCheck(layout->Value);
  2964. }
  2965. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(newOpcode);
  2966. BuildElementSlotI1(newOpcode, offset, layout->Value, layout->SlotIndex, layout->profileId);
  2967. }
  2968. void
  2969. IRBuilder::BuildElementSlotI1(Js::OpCode newOpcode, uint32 offset, Js::RegSlot regSlot,
  2970. int32 slotId, Js::ProfileId profileId)
  2971. {
  2972. IR::RegOpnd *regOpnd;
  2973. IR::SymOpnd *fieldOpnd;
  2974. IR::Instr *instr = nullptr;
  2975. IR::ByteCodeUsesInstr *byteCodeUse;
  2976. PropertySym *fieldSym = nullptr;
  2977. StackSym * stackFuncPtrSym = nullptr;
  2978. SymID symID = m_func->GetJITFunctionBody()->GetLocalClosureReg();
  2979. bool isLdSlotThatWasNotProfiled = false;
  2980. StackSym* closureSym = m_func->GetLocalClosureSym();
  2981. uint scopeSlotSize = this->IsParamScopeDone() ? m_func->GetJITFunctionBody()->GetScopeSlotArraySize() : m_func->GetJITFunctionBody()->GetParamScopeSlotArraySize();
  2982. switch (newOpcode)
  2983. {
  2984. case Js::OpCode::LdParamSlot:
  2985. scopeSlotSize = m_func->GetJITFunctionBody()->GetParamScopeSlotArraySize();
  2986. closureSym = m_func->GetParamClosureSym();
  2987. symID = m_func->GetJITFunctionBody()->GetParamClosureReg();
  2988. // Fall through
  2989. case Js::OpCode::LdLocalSlot:
  2990. if (!PHASE_OFF(Js::ClosureRangeCheckPhase, m_func))
  2991. {
  2992. if ((uint32)slotId >= scopeSlotSize + Js::ScopeSlots::FirstSlotIndex)
  2993. {
  2994. Js::Throw::FatalInternalError();
  2995. }
  2996. }
  2997. if (closureSym->HasByteCodeRegSlot())
  2998. {
  2999. byteCodeUse = IR::ByteCodeUsesInstr::New(m_func, offset);
  3000. byteCodeUse->SetNonOpndSymbol(closureSym->m_id);
  3001. this->AddInstr(byteCodeUse, offset);
  3002. }
  3003. // Read the scope slot pointer back using the stack closure sym.
  3004. newOpcode = Js::OpCode::LdSlot;
  3005. if (m_func->DoStackFrameDisplay())
  3006. {
  3007. // Read the scope slot pointer back using the stack closure sym.
  3008. fieldOpnd = this->BuildFieldOpnd(Js::OpCode::LdSlotArr, closureSym->m_id, 0, (Js::PropertyIdIndexType)-1, PropertyKindSlotArray);
  3009. regOpnd = IR::RegOpnd::New(TyVar, m_func);
  3010. instr = IR::Instr::New(Js::OpCode::LdSlotArr, regOpnd, fieldOpnd, m_func);
  3011. this->AddInstr(instr, offset);
  3012. symID = regOpnd->m_sym->m_id;
  3013. if (IsLoopBody())
  3014. {
  3015. fieldOpnd = this->BuildFieldOpnd(Js::OpCode::LdSlotArr, closureSym->m_id, slotId, (Js::PropertyIdIndexType)-1, PropertyKindSlotArray);
  3016. }
  3017. }
  3018. else if (IsLoopBody())
  3019. {
  3020. this->EnsureLoopBodyLoadSlot(symID);
  3021. }
  3022. fieldSym = PropertySym::FindOrCreate(symID, slotId, (uint32)-1, (uint)-1, PropertyKindSlots, m_func);
  3023. fieldOpnd = IR::SymOpnd::New(fieldSym, TyVar, m_func);
  3024. regOpnd = this->BuildDstOpnd(regSlot);
  3025. instr = nullptr;
  3026. if (profileId != Js::Constants::NoProfileId)
  3027. {
  3028. instr = this->BuildProfiledSlotLoad(Js::OpCode::LdSlot, regOpnd, fieldOpnd, profileId, &isLdSlotThatWasNotProfiled);
  3029. }
  3030. if (!instr)
  3031. {
  3032. instr = IR::Instr::New(Js::OpCode::LdSlot, regOpnd, fieldOpnd, m_func);
  3033. }
  3034. this->AddInstr(instr, offset);
  3035. break;
  3036. case Js::OpCode::LdParamObjSlot:
  3037. closureSym = m_func->GetParamClosureSym();
  3038. symID = m_func->GetJITFunctionBody()->GetParamClosureReg();
  3039. newOpcode = Js::OpCode::LdLocalObjSlot;
  3040. // Fall through
  3041. case Js::OpCode::LdLocalObjSlot:
  3042. if (closureSym->HasByteCodeRegSlot())
  3043. {
  3044. byteCodeUse = IR::ByteCodeUsesInstr::New(m_func, offset);
  3045. byteCodeUse->SetNonOpndSymbol(closureSym->m_id);
  3046. this->AddInstr(byteCodeUse, offset);
  3047. }
  3048. fieldOpnd = this->BuildFieldOpnd(newOpcode, symID, (Js::DynamicObject::GetOffsetOfAuxSlots()) / sizeof(Js::Var), (Js::PropertyIdIndexType) - 1, PropertyKindSlotArray);
  3049. regOpnd = IR::RegOpnd::New(TyVar, m_func);
  3050. instr = IR::Instr::New(Js::OpCode::LdSlotArr, regOpnd, fieldOpnd, m_func);
  3051. this->AddInstr(instr, offset);
  3052. fieldSym = PropertySym::New(regOpnd->m_sym, slotId, (uint32)-1, (uint)-1, PropertyKindSlots, m_func);
  3053. fieldOpnd = IR::SymOpnd::New(fieldSym, TyVar, m_func);
  3054. regOpnd = this->BuildDstOpnd(regSlot);
  3055. instr = nullptr;
  3056. newOpcode = Js::OpCode::LdSlot;
  3057. if (profileId != Js::Constants::NoProfileId)
  3058. {
  3059. instr = this->BuildProfiledSlotLoad(newOpcode, regOpnd, fieldOpnd, profileId, &isLdSlotThatWasNotProfiled);
  3060. }
  3061. if (!instr)
  3062. {
  3063. instr = IR::Instr::New(newOpcode, regOpnd, fieldOpnd, m_func);
  3064. }
  3065. this->AddInstr(instr, offset);
  3066. break;
  3067. case Js::OpCode::StParamSlot:
  3068. case Js::OpCode::StParamSlotChkUndecl:
  3069. scopeSlotSize = m_func->GetJITFunctionBody()->GetParamScopeSlotArraySize();
  3070. closureSym = m_func->GetParamClosureSym();
  3071. symID = m_func->GetJITFunctionBody()->GetParamClosureReg();
  3072. newOpcode = newOpcode == Js::OpCode::StParamSlot ? Js::OpCode::StLocalSlot : Js::OpCode::StLocalSlotChkUndecl;
  3073. // Fall through
  3074. case Js::OpCode::StLocalSlot:
  3075. case Js::OpCode::StLocalSlotChkUndecl:
  3076. if (!PHASE_OFF(Js::ClosureRangeCheckPhase, m_func))
  3077. {
  3078. if ((uint32)slotId >= scopeSlotSize + Js::ScopeSlots::FirstSlotIndex)
  3079. {
  3080. Js::Throw::FatalInternalError();
  3081. }
  3082. }
  3083. if (closureSym->HasByteCodeRegSlot())
  3084. {
  3085. byteCodeUse = IR::ByteCodeUsesInstr::New(m_func, offset);
  3086. byteCodeUse->SetNonOpndSymbol(closureSym->m_id);
  3087. this->AddInstr(byteCodeUse, offset);
  3088. }
  3089. newOpcode = newOpcode == Js::OpCode::StLocalSlot ? Js::OpCode::StSlot : Js::OpCode::StSlotChkUndecl;
  3090. if (m_func->DoStackFrameDisplay())
  3091. {
  3092. regOpnd = IR::RegOpnd::New(TyVar, m_func);
  3093. // Read the scope slot pointer back using the stack closure sym.
  3094. fieldOpnd = this->BuildFieldOpnd(Js::OpCode::LdSlotArr, closureSym->m_id, 0, (Js::PropertyIdIndexType)-1, PropertyKindSlotArray);
  3095. instr = IR::Instr::New(Js::OpCode::LdSlotArr, regOpnd, fieldOpnd, m_func);
  3096. this->AddInstr(instr, offset);
  3097. symID = regOpnd->m_sym->m_id;
  3098. if (IsLoopBody())
  3099. {
  3100. fieldOpnd = this->BuildFieldOpnd(Js::OpCode::LdSlotArr, closureSym->m_id, slotId, (Js::PropertyIdIndexType)-1, PropertyKindSlotArray);
  3101. }
  3102. }
  3103. else
  3104. {
  3105. if (IsLoopBody())
  3106. {
  3107. this->EnsureLoopBodyLoadSlot(symID);
  3108. }
  3109. }
  3110. fieldSym = PropertySym::FindOrCreate(symID, slotId, (uint32)-1, (uint)-1, PropertyKindSlots, m_func);
  3111. fieldOpnd = IR::SymOpnd::New(fieldSym, TyVar, m_func);
  3112. regOpnd = this->BuildSrcOpnd(regSlot);
  3113. instr = IR::Instr::New(newOpcode, fieldOpnd, regOpnd, m_func);
  3114. this->AddInstr(instr, offset);
  3115. if (newOpcode == Js::OpCode::StSlotChkUndecl)
  3116. {
  3117. instr->SetSrc2(fieldOpnd);
  3118. }
  3119. break;
  3120. case Js::OpCode::StParamObjSlot:
  3121. case Js::OpCode::StParamObjSlotChkUndecl:
  3122. closureSym = m_func->GetParamClosureSym();
  3123. symID = m_func->GetJITFunctionBody()->GetParamClosureReg();
  3124. newOpcode = newOpcode == Js::OpCode::StParamObjSlot ? Js::OpCode::StLocalObjSlot : Js::OpCode::StLocalObjSlotChkUndecl;
  3125. // Fall through
  3126. case Js::OpCode::StLocalObjSlot:
  3127. case Js::OpCode::StLocalObjSlotChkUndecl:
  3128. if (closureSym->HasByteCodeRegSlot())
  3129. {
  3130. byteCodeUse = IR::ByteCodeUsesInstr::New(m_func, offset);
  3131. byteCodeUse->SetNonOpndSymbol(closureSym->m_id);
  3132. this->AddInstr(byteCodeUse, offset);
  3133. }
  3134. regOpnd = IR::RegOpnd::New(TyVar, m_func);
  3135. fieldOpnd = this->BuildFieldOpnd(Js::OpCode::LdSlotArr, symID, (Js::DynamicObject::GetOffsetOfAuxSlots())/sizeof(Js::Var), (Js::PropertyIdIndexType)-1, PropertyKindSlotArray);
  3136. instr = IR::Instr::New(Js::OpCode::LdSlotArr, regOpnd, fieldOpnd, m_func);
  3137. this->AddInstr(instr, offset);
  3138. newOpcode = newOpcode == Js::OpCode::StLocalObjSlot ? Js::OpCode::StSlot : Js::OpCode::StSlotChkUndecl;
  3139. fieldSym = PropertySym::New(regOpnd->m_sym, slotId, (uint32)-1, (uint)-1, PropertyKindSlots, m_func);
  3140. fieldOpnd = IR::SymOpnd::New(fieldSym, TyVar, m_func);
  3141. regOpnd = this->BuildSrcOpnd(regSlot);
  3142. instr = IR::Instr::New(newOpcode, fieldOpnd, regOpnd, m_func);
  3143. if (newOpcode == Js::OpCode::StSlotChkUndecl)
  3144. {
  3145. // ChkUndecl includes an implicit read of the destination. Communicate the liveness by using the destination in src2.
  3146. instr->SetSrc2(fieldOpnd);
  3147. }
  3148. this->AddInstr(instr, offset);
  3149. break;
  3150. case Js::OpCode::LdEnvObj:
  3151. fieldOpnd = this->BuildFieldOpnd(Js::OpCode::LdSlotArr, this->GetEnvReg(), slotId, (Js::PropertyIdIndexType)-1, PropertyKindSlotArray);
  3152. regOpnd = this->BuildDstOpnd(regSlot);
  3153. instr = IR::Instr::New(Js::OpCode::LdSlotArr, regOpnd, fieldOpnd, m_func);
  3154. this->AddInstr(instr, offset);
  3155. m_func->GetTopFunc()->AddFrameDisplayCheck(fieldOpnd);
  3156. break;
  3157. case Js::OpCode::NewStackScFunc:
  3158. stackFuncPtrSym = this->EnsureStackFuncPtrSym();
  3159. newOpcode = Js::OpCode::NewScFunc;
  3160. // fall through
  3161. case Js::OpCode::NewScFunc:
  3162. goto NewScFuncCommon;
  3163. case Js::OpCode::NewScGenFunc:
  3164. newOpcode = Js::OpCode::NewScGenFunc;
  3165. NewScFuncCommon:
  3166. {
  3167. IR::Opnd * functionBodySlotOpnd = IR::IntConstOpnd::New(slotId, TyInt32, m_func, true);
  3168. IR::Opnd *environmentOpnd = GetEnvironmentOperand(offset);
  3169. regOpnd = this->BuildDstOpnd(regSlot);
  3170. if (stackFuncPtrSym)
  3171. {
  3172. IR::RegOpnd * dataOpnd = IR::RegOpnd::New(TyVar, m_func);
  3173. instr = IR::Instr::New(Js::OpCode::NewScFuncData, dataOpnd, environmentOpnd,
  3174. IR::RegOpnd::New(stackFuncPtrSym, TyVar, m_func), m_func);
  3175. this->AddInstr(instr, offset);
  3176. instr = IR::Instr::New(newOpcode, regOpnd, functionBodySlotOpnd, dataOpnd, m_func);
  3177. }
  3178. else
  3179. {
  3180. instr = IR::Instr::New(newOpcode, regOpnd, functionBodySlotOpnd, environmentOpnd, m_func);
  3181. }
  3182. if (regOpnd->m_sym->m_isSingleDef)
  3183. {
  3184. regOpnd->m_sym->m_isSafeThis = true;
  3185. regOpnd->m_sym->m_isNotNumber = true;
  3186. }
  3187. this->AddInstr(instr, offset);
  3188. return;
  3189. }
  3190. default:
  3191. Assert(0);
  3192. }
  3193. if(isLdSlotThatWasNotProfiled && DoBailOnNoProfile())
  3194. {
  3195. InsertBailOnNoProfile(instr);
  3196. }
  3197. }
  3198. IR::Opnd*
  3199. IRBuilder::GetEnvironmentOperand(uint32 offset)
  3200. {
  3201. StackSym* sym = nullptr;
  3202. // The byte code doesn't refer directly to a closure environment. Get the implicit one
  3203. // that's pointed to by the function body.
  3204. if (m_func->DoStackFrameDisplay() && m_func->GetLocalFrameDisplaySym())
  3205. {
  3206. // Read the scope slot pointer back using the stack closure sym.
  3207. IR::Opnd *fieldOpnd = this->BuildFieldOpnd(Js::OpCode::LdSlotArr, m_func->GetLocalFrameDisplaySym()->m_id, 0, (Js::PropertyIdIndexType) - 1, PropertyKindSlotArray);
  3208. IR::RegOpnd *regOpnd = IR::RegOpnd::New(TyVar, m_func);
  3209. this->AddInstr(
  3210. IR::Instr::New(Js::OpCode::LdSlotArr, regOpnd, fieldOpnd, m_func),
  3211. offset);
  3212. sym = regOpnd->m_sym;
  3213. }
  3214. else
  3215. {
  3216. SymID symID;
  3217. symID = this->GetEnvRegForInnerFrameDisplay();
  3218. Assert(symID != Js::Constants::NoRegister);
  3219. if (IsLoopBody() && !RegIsConstant(symID))
  3220. {
  3221. this->EnsureLoopBodyLoadSlot(symID);
  3222. }
  3223. if (m_func->DoStackNestedFunc() && symID == GetEnvReg())
  3224. {
  3225. // Environment is not guaranteed constant during this function because it could become boxed during execution,
  3226. // so load the environment every time you need it.
  3227. IR::RegOpnd *regOpnd = IR::RegOpnd::New(TyVar, m_func);
  3228. this->AddInstr(
  3229. IR::Instr::New(Js::OpCode::LdEnv, regOpnd, m_func),
  3230. offset);
  3231. sym = regOpnd->m_sym;
  3232. }
  3233. else
  3234. {
  3235. sym = StackSym::FindOrCreate(symID, (Js::RegSlot)symID, m_func);
  3236. }
  3237. }
  3238. return IR::RegOpnd::New(sym, TyVar, m_func);
  3239. }
  3240. template <typename SizePolicy>
  3241. void
  3242. IRBuilder::BuildElementSlotI2(Js::OpCode newOpcode, uint32 offset)
  3243. {
  3244. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  3245. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  3246. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementSlotI2<SizePolicy>>();
  3247. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  3248. {
  3249. this->DoClosureRegCheck(layout->Value);
  3250. }
  3251. BuildElementSlotI2(newOpcode, offset, layout->Value, layout->SlotIndex1, layout->SlotIndex2, Js::Constants::NoProfileId);
  3252. }
  3253. template <typename SizePolicy>
  3254. void
  3255. IRBuilder::BuildProfiledElementSlotI2(Js::OpCode newOpcode, uint32 offset)
  3256. {
  3257. Assert(OpCodeAttr::IsProfiledOp(newOpcode));
  3258. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  3259. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_ElementSlotI2<SizePolicy>>>();
  3260. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  3261. {
  3262. this->DoClosureRegCheck(layout->Value);
  3263. }
  3264. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(newOpcode);
  3265. BuildElementSlotI2(newOpcode, offset, layout->Value, layout->SlotIndex1, layout->SlotIndex2, layout->profileId);
  3266. }
  3267. void
  3268. IRBuilder::BuildElementSlotI2(Js::OpCode newOpcode, uint32 offset, Js::RegSlot regSlot,
  3269. int32 slotId1, int32 slotId2, Js::ProfileId profileId)
  3270. {
  3271. IR::RegOpnd *regOpnd;
  3272. IR::SymOpnd *fieldOpnd;
  3273. IR::Instr *instr;
  3274. PropertySym *fieldSym;
  3275. bool isLdSlotThatWasNotProfiled = false;
  3276. switch (newOpcode)
  3277. {
  3278. case Js::OpCode::LdModuleSlot:
  3279. case Js::OpCode::StModuleSlot:
  3280. {
  3281. Field(Js::Var)* moduleExportVarArrayAddr = Js::JavascriptOperators::OP_GetModuleExportSlotArrayAddress(slotId1, slotId2, m_func->GetScriptContextInfo());
  3282. IR::AddrOpnd* addrOpnd = IR::AddrOpnd::New(moduleExportVarArrayAddr, IR::AddrOpndKindConstantAddress, m_func, true);
  3283. regOpnd = IR::RegOpnd::New(TyVar, m_func);
  3284. instr = IR::Instr::New(Js::OpCode::Ld_A, regOpnd, addrOpnd, m_func);
  3285. this->AddInstr(instr, offset);
  3286. fieldSym = PropertySym::New(regOpnd->m_sym, slotId2, (uint32)-1, (uint)-1, PropertyKindSlots, m_func);
  3287. fieldOpnd = IR::SymOpnd::New(fieldSym, TyVar, m_func);
  3288. if (newOpcode == Js::OpCode::LdModuleSlot)
  3289. {
  3290. newOpcode = Js::OpCode::LdSlot;
  3291. regOpnd = this->BuildDstOpnd(regSlot);
  3292. instr = IR::Instr::New(newOpcode, regOpnd, fieldOpnd, m_func);
  3293. }
  3294. else
  3295. {
  3296. Assert(newOpcode == Js::OpCode::StModuleSlot);
  3297. newOpcode = Js::OpCode::StSlot;
  3298. regOpnd = this->BuildSrcOpnd(regSlot);
  3299. instr = IR::Instr::New(newOpcode, fieldOpnd, regOpnd, m_func);
  3300. }
  3301. this->AddInstr(instr, offset);
  3302. break;
  3303. }
  3304. case Js::OpCode::LdEnvSlot:
  3305. case Js::OpCode::LdEnvObjSlot:
  3306. case Js::OpCode::StEnvSlot:
  3307. case Js::OpCode::StEnvSlotChkUndecl:
  3308. case Js::OpCode::StEnvObjSlot:
  3309. case Js::OpCode::StEnvObjSlotChkUndecl:
  3310. fieldOpnd = this->BuildFieldOpnd(Js::OpCode::LdSlotArr, this->GetEnvReg(), slotId1, (Js::PropertyIdIndexType)-1, PropertyKindSlotArray);
  3311. regOpnd = IR::RegOpnd::New(TyVar, m_func);
  3312. instr = IR::Instr::New(Js::OpCode::LdSlotArr, regOpnd, fieldOpnd, m_func);
  3313. this->AddInstr(instr, offset);
  3314. switch (newOpcode)
  3315. {
  3316. case Js::OpCode::LdEnvObjSlot:
  3317. case Js::OpCode::StEnvObjSlot:
  3318. case Js::OpCode::StEnvObjSlotChkUndecl:
  3319. m_func->GetTopFunc()->AddFrameDisplayCheck(fieldOpnd, (uint32)-1);
  3320. fieldSym = PropertySym::New(regOpnd->m_sym, (Js::DynamicObject::GetOffsetOfAuxSlots())/sizeof(Js::Var),
  3321. (uint32)-1, (uint)-1, PropertyKindSlotArray, m_func);
  3322. fieldOpnd = IR::SymOpnd::New(fieldSym, TyVar, m_func);
  3323. regOpnd = IR::RegOpnd::New(TyVar, m_func);
  3324. instr = IR::Instr::New(Js::OpCode::LdSlotArr, regOpnd, fieldOpnd, m_func);
  3325. this->AddInstr(instr, offset);
  3326. break;
  3327. default:
  3328. m_func->GetTopFunc()->AddFrameDisplayCheck(fieldOpnd, slotId2);
  3329. break;
  3330. }
  3331. fieldSym = PropertySym::New(regOpnd->m_sym, slotId2, (uint32)-1, (uint)-1, PropertyKindSlots, m_func);
  3332. fieldOpnd = IR::SymOpnd::New(fieldSym, TyVar, m_func);
  3333. switch (newOpcode)
  3334. {
  3335. case Js::OpCode::LdEnvSlot:
  3336. case Js::OpCode::LdEnvObjSlot:
  3337. newOpcode = Js::OpCode::LdSlot;
  3338. regOpnd = this->BuildDstOpnd(regSlot);
  3339. instr = nullptr;
  3340. if (profileId != Js::Constants::NoProfileId)
  3341. {
  3342. instr = this->BuildProfiledSlotLoad(newOpcode, regOpnd, fieldOpnd, profileId, &isLdSlotThatWasNotProfiled);
  3343. }
  3344. if (!instr)
  3345. {
  3346. instr = IR::Instr::New(newOpcode, regOpnd, fieldOpnd, m_func);
  3347. }
  3348. break;
  3349. default:
  3350. newOpcode =
  3351. newOpcode == Js::OpCode::StEnvSlot || newOpcode == Js::OpCode::StEnvObjSlot ? Js::OpCode::StSlot : Js::OpCode::StSlotChkUndecl;
  3352. regOpnd = this->BuildSrcOpnd(regSlot);
  3353. instr = IR::Instr::New(newOpcode, fieldOpnd, regOpnd, m_func);
  3354. if (newOpcode == Js::OpCode::StSlotChkUndecl)
  3355. {
  3356. // ChkUndecl includes an implicit read of the destination. Communicate the liveness by using the destination in src2.
  3357. instr->SetSrc2(fieldOpnd);
  3358. }
  3359. break;
  3360. }
  3361. this->AddInstr(instr, offset);
  3362. if(isLdSlotThatWasNotProfiled && DoBailOnNoProfile())
  3363. {
  3364. InsertBailOnNoProfile(instr);
  3365. }
  3366. break;
  3367. case Js::OpCode::StInnerObjSlot:
  3368. case Js::OpCode::StInnerObjSlotChkUndecl:
  3369. case Js::OpCode::StInnerSlot:
  3370. case Js::OpCode::StInnerSlotChkUndecl:
  3371. if ((uint)slotId1 >= m_func->GetJITFunctionBody()->GetInnerScopeCount())
  3372. {
  3373. Js::Throw::FatalInternalError();
  3374. }
  3375. regOpnd = this->BuildSrcOpnd(regSlot);
  3376. slotId1 += this->m_func->GetJITFunctionBody()->GetFirstInnerScopeReg();
  3377. if ((uint)slotId1 >= this->m_func->GetJITFunctionBody()->GetLocalsCount())
  3378. {
  3379. Js::Throw::FatalInternalError();
  3380. }
  3381. if (newOpcode == Js::OpCode::StInnerObjSlot || newOpcode == Js::OpCode::StInnerObjSlotChkUndecl)
  3382. {
  3383. IR::RegOpnd * slotOpnd = IR::RegOpnd::New(TyVar, m_func);
  3384. fieldOpnd = this->BuildFieldOpnd(Js::OpCode::LdSlotArr, slotId1, (Js::DynamicObject::GetOffsetOfAuxSlots())/sizeof(Js::Var), (Js::PropertyIdIndexType)-1, PropertyKindSlotArray);
  3385. instr = IR::Instr::New(Js::OpCode::LdSlotArr, slotOpnd, fieldOpnd, m_func);
  3386. this->AddInstr(instr, offset);
  3387. PropertySym *propertySym = PropertySym::New(slotOpnd->m_sym, slotId2, (uint32)-1, (uint)-1, PropertyKindSlots, m_func);
  3388. fieldOpnd = IR::PropertySymOpnd::New(propertySym, (Js::CacheId)-1, TyVar, m_func);
  3389. }
  3390. else
  3391. {
  3392. fieldOpnd = this->BuildFieldOpnd(Js::OpCode::StSlot, slotId1, slotId2, (Js::PropertyIdIndexType)-1, PropertyKindSlots);
  3393. }
  3394. newOpcode =
  3395. newOpcode == Js::OpCode::StInnerObjSlot || newOpcode == Js::OpCode::StInnerSlot ?
  3396. Js::OpCode::StSlot : Js::OpCode::StSlotChkUndecl;
  3397. instr = IR::Instr::New(newOpcode, fieldOpnd, regOpnd, m_func);
  3398. if (newOpcode == Js::OpCode::StSlotChkUndecl)
  3399. {
  3400. // ChkUndecl includes an implicit read of the destination. Communicate the liveness by using the destination in src2.
  3401. instr->SetSrc2(fieldOpnd);
  3402. }
  3403. this->AddInstr(instr, offset);
  3404. break;
  3405. case Js::OpCode::LdInnerSlot:
  3406. case Js::OpCode::LdInnerObjSlot:
  3407. if ((uint)slotId1 >= m_func->GetJITFunctionBody()->GetInnerScopeCount())
  3408. {
  3409. Js::Throw::FatalInternalError();
  3410. }
  3411. slotId1 += this->m_func->GetJITFunctionBody()->GetFirstInnerScopeReg();
  3412. if ((uint)slotId1 >= this->m_func->GetJITFunctionBody()->GetLocalsCount())
  3413. {
  3414. Js::Throw::FatalInternalError();
  3415. }
  3416. if (newOpcode == Js::OpCode::LdInnerObjSlot)
  3417. {
  3418. IR::RegOpnd * slotOpnd = IR::RegOpnd::New(TyVar, m_func);
  3419. fieldOpnd = this->BuildFieldOpnd(Js::OpCode::LdSlotArr, slotId1, (Js::DynamicObject::GetOffsetOfAuxSlots())/sizeof(Js::Var), (Js::PropertyIdIndexType)-1, PropertyKindSlotArray);
  3420. instr = IR::Instr::New(Js::OpCode::LdSlotArr, slotOpnd, fieldOpnd, m_func);
  3421. this->AddInstr(instr, offset);
  3422. PropertySym *propertySym = PropertySym::New(slotOpnd->m_sym, slotId2, (uint32)-1, (uint)-1, PropertyKindSlots, m_func);
  3423. fieldOpnd = IR::PropertySymOpnd::New(propertySym, (Js::CacheId)-1, TyVar, m_func);
  3424. }
  3425. else
  3426. {
  3427. fieldOpnd = this->BuildFieldOpnd(Js::OpCode::LdSlot, slotId1, slotId2, (Js::PropertyIdIndexType)-1, PropertyKindSlots);
  3428. }
  3429. regOpnd = this->BuildDstOpnd(regSlot);
  3430. instr = IR::Instr::New(Js::OpCode::LdSlot, regOpnd, fieldOpnd, m_func);
  3431. this->AddInstr(instr, offset);
  3432. break;
  3433. default:
  3434. AssertMsg(false, "Unsupported opcode in BuildElementSlotI2");
  3435. break;
  3436. }
  3437. }
  3438. template <typename SizePolicy>
  3439. void
  3440. IRBuilder::BuildElementSlotI3(Js::OpCode newOpcode, uint32 offset)
  3441. {
  3442. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  3443. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  3444. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementSlotI3<SizePolicy>>();
  3445. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  3446. {
  3447. this->DoClosureRegCheck(layout->Value);
  3448. this->DoClosureRegCheck(layout->Instance);
  3449. this->DoClosureRegCheck(layout->HomeObj);
  3450. }
  3451. BuildElementSlotI3(newOpcode, offset, layout->Instance, layout->Value, layout->SlotIndex, layout->HomeObj, Js::Constants::NoProfileId);
  3452. }
  3453. template <typename SizePolicy>
  3454. void
  3455. IRBuilder::BuildProfiledElementSlotI3(Js::OpCode newOpcode, uint32 offset)
  3456. {
  3457. Assert(OpCodeAttr::IsProfiledOp(newOpcode));
  3458. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  3459. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_ElementSlotI3<SizePolicy>>>();
  3460. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  3461. {
  3462. this->DoClosureRegCheck(layout->Value);
  3463. this->DoClosureRegCheck(layout->Instance);
  3464. this->DoClosureRegCheck(layout->HomeObj);
  3465. }
  3466. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(newOpcode);
  3467. BuildElementSlotI3(newOpcode, offset, layout->Instance, layout->Value, layout->SlotIndex, layout->HomeObj, layout->profileId);
  3468. }
  3469. void
  3470. IRBuilder::BuildElementSlotI3(Js::OpCode newOpcode, uint32 offset, Js::RegSlot fieldRegSlot, Js::RegSlot regSlot,
  3471. int32 slotId, Js::RegSlot homeObj, Js::ProfileId profileId)
  3472. {
  3473. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  3474. IR::Instr * instr;
  3475. IR::RegOpnd * regOpnd;
  3476. switch (newOpcode)
  3477. {
  3478. case Js::OpCode::NewInnerScFuncHomeObj:
  3479. newOpcode = Js::OpCode::NewScFuncHomeObj;
  3480. goto NewScFuncCommon;
  3481. case Js::OpCode::NewInnerScGenFuncHomeObj:
  3482. newOpcode = Js::OpCode::NewScGenFuncHomeObj;
  3483. NewScFuncCommon:
  3484. {
  3485. Js::FunctionInfoPtrPtr infoRef = m_func->GetJITFunctionBody()->GetNestedFuncRef(slotId);
  3486. IR::AddrOpnd * functionBodySlotOpnd = IR::AddrOpnd::New((Js::Var)infoRef, IR::AddrOpndKindDynamicMisc, m_func);
  3487. IR::Opnd * environmentOpnd = this->BuildSrcOpnd(fieldRegSlot);
  3488. IR::Opnd * homeObjOpnd = this->BuildSrcOpnd(homeObj);
  3489. regOpnd = this->BuildDstOpnd(regSlot);
  3490. instr = IR::Instr::New(Js::OpCode::ExtendArg_A, IR::RegOpnd::New(TyVar, m_func), homeObjOpnd, m_func);
  3491. this->AddInstr(instr, offset);
  3492. instr = IR::Instr::New(Js::OpCode::ExtendArg_A, IR::RegOpnd::New(TyVar, m_func), functionBodySlotOpnd, instr->GetDst(), m_func);
  3493. this->AddInstr(instr, offset);
  3494. instr = IR::Instr::New(Js::OpCode::ExtendArg_A, IR::RegOpnd::New(TyVar, m_func), environmentOpnd, instr->GetDst(), m_func);
  3495. this->AddInstr(instr, offset);
  3496. instr = IR::Instr::New(newOpcode, regOpnd, instr->GetDst(), m_func);
  3497. if (regOpnd->m_sym->m_isSingleDef)
  3498. {
  3499. regOpnd->m_sym->m_isSafeThis = true;
  3500. regOpnd->m_sym->m_isNotNumber = true;
  3501. }
  3502. this->AddInstr(instr, offset);
  3503. return;
  3504. }
  3505. default:
  3506. AssertMsg(UNREACHED, "Unknown ElementSlotI3 opcode");
  3507. Fatal();
  3508. }
  3509. }
  3510. IR::SymOpnd *
  3511. IRBuilder::BuildLoopBodySlotOpnd(SymID symId)
  3512. {
  3513. Assert(!this->RegIsConstant((Js::RegSlot)symId));
  3514. // Get the interpreter frame instance that was passed in.
  3515. StackSym *loopParamSym = m_func->EnsureLoopParamSym();
  3516. PropertySym * fieldSym = PropertySym::FindOrCreate(loopParamSym->m_id, (Js::PropertyId)(symId + this->m_loopBodyLocalsStartSlot), (uint32)-1, (uint)-1, PropertyKindLocalSlots, m_func);
  3517. return IR::SymOpnd::New(fieldSym, TyVar, m_func);
  3518. }
  3519. void
  3520. IRBuilder::EnsureLoopBodyLoadSlot(SymID symId, bool isCatchObjectSym)
  3521. {
  3522. // No need to emit LdSlot for a catch object. In fact, if we do, we might be loading an uninitialized value from the slot.
  3523. if (isCatchObjectSym)
  3524. {
  3525. return;
  3526. }
  3527. StackSym * symDst = StackSym::FindOrCreate(symId, (Js::RegSlot)symId, m_func);
  3528. if (symDst->m_isCatchObjectSym || this->m_ldSlots->TestAndSet(symId))
  3529. {
  3530. return;
  3531. }
  3532. IR::SymOpnd * fieldSymOpnd = this->BuildLoopBodySlotOpnd(symId);
  3533. IR::RegOpnd * dstOpnd = IR::RegOpnd::New(symDst, TyVar, m_func);
  3534. IR::Instr * ldSlotInstr;
  3535. ValueType symValueType;
  3536. if(m_func->GetWorkItem()->HasSymIdToValueTypeMap() && m_func->GetWorkItem()->TryGetValueType(symId, &symValueType))
  3537. {
  3538. ldSlotInstr = IR::ProfiledInstr::New(Js::OpCode::LdSlot, dstOpnd, fieldSymOpnd, m_func);
  3539. ldSlotInstr->AsProfiledInstr()->u.FldInfo().valueType = symValueType;
  3540. }
  3541. else
  3542. {
  3543. ldSlotInstr = IR::Instr::New(Js::OpCode::LdSlot, dstOpnd, fieldSymOpnd, m_func);
  3544. }
  3545. m_func->m_headInstr->InsertAfter(ldSlotInstr);
  3546. if (m_lastInstr == m_func->m_headInstr)
  3547. {
  3548. m_lastInstr = ldSlotInstr;
  3549. }
  3550. }
  3551. void
  3552. IRBuilder::SetLoopBodyStSlot(SymID symID, bool isCatchObjectSym)
  3553. {
  3554. if (this->m_func->HasTry() && !PHASE_OFF(Js::JITLoopBodyInTryCatchPhase, this->m_func))
  3555. {
  3556. // No need to emit StSlot for a catch object. In fact, if we do, we might be storing an uninitialized value to the slot.
  3557. if (isCatchObjectSym)
  3558. {
  3559. return;
  3560. }
  3561. StackSym * dstSym = StackSym::FindOrCreate(symID, (Js::RegSlot)symID, m_func);
  3562. Assert(dstSym);
  3563. if (dstSym->m_isCatchObjectSym)
  3564. {
  3565. return;
  3566. }
  3567. }
  3568. this->m_stSlots->Set(symID);
  3569. }
  3570. ///----------------------------------------------------------------------------
  3571. ///
  3572. /// IRBuilder::BuildElementCP
  3573. ///
  3574. /// Build IR instr for an ElementCP or ElementRootCP instruction.
  3575. ///
  3576. ///----------------------------------------------------------------------------
  3577. IR::Instr *
  3578. IRBuilder::BuildProfiledFieldLoad(Js::OpCode loadOp, IR::RegOpnd *dstOpnd, IR::SymOpnd *srcOpnd, Js::CacheId inlineCacheIndex, bool *pUnprofiled)
  3579. {
  3580. IR::Instr * instr = nullptr;
  3581. // Prefer JitProfilingInstr if we're in simplejit
  3582. if (m_func->DoSimpleJitDynamicProfile())
  3583. {
  3584. instr = IR::JitProfilingInstr::New(loadOp, dstOpnd, srcOpnd, m_func);
  3585. }
  3586. else if (this->m_func->HasProfileInfo())
  3587. {
  3588. instr = IR::ProfiledInstr::New(loadOp, dstOpnd, srcOpnd, m_func);
  3589. instr->AsProfiledInstr()->u.FldInfo() = *(m_func->GetReadOnlyProfileInfo()->GetFldInfo(inlineCacheIndex));
  3590. *pUnprofiled = !instr->AsProfiledInstr()->u.FldInfo().WasLdFldProfiled();
  3591. dstOpnd->SetValueType(instr->AsProfiledInstr()->u.FldInfo().valueType);
  3592. #if ENABLE_DEBUG_CONFIG_OPTIONS
  3593. if(Js::Configuration::Global.flags.TestTrace.IsEnabled(Js::DynamicProfilePhase))
  3594. {
  3595. const ValueType valueType(instr->AsProfiledInstr()->u.FldInfo().valueType);
  3596. char valueTypeStr[VALUE_TYPE_MAX_STRING_SIZE];
  3597. valueType.ToString(valueTypeStr);
  3598. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  3599. Output::Print(_u("TestTrace function %s (%s) ValueType = %i "), m_func->GetJITFunctionBody()->GetDisplayName(), m_func->GetDebugNumberSet(debugStringBuffer), valueTypeStr);
  3600. instr->DumpTestTrace();
  3601. }
  3602. #endif
  3603. }
  3604. return instr;
  3605. }
  3606. Js::RegSlot IRBuilder::GetEnvRegForEvalCode() const
  3607. {
  3608. if (m_func->GetJITFunctionBody()->IsStrictMode() && m_func->GetJITFunctionBody()->IsGlobalFunc())
  3609. {
  3610. return m_func->GetJITFunctionBody()->GetLocalFrameDisplayReg();
  3611. }
  3612. else
  3613. {
  3614. return GetEnvReg();
  3615. }
  3616. }
  3617. template <typename SizePolicy>
  3618. void
  3619. IRBuilder::BuildElementP(Js::OpCode newOpcode, uint32 offset)
  3620. {
  3621. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  3622. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementP<SizePolicy>>();
  3623. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  3624. {
  3625. this->DoClosureRegCheck(layout->Value);
  3626. }
  3627. BuildElementP(newOpcode, offset, layout->Value, layout->inlineCacheIndex);
  3628. }
  3629. void
  3630. IRBuilder::BuildElementP(Js::OpCode newOpcode, uint32 offset, Js::RegSlot regSlot, Js::CacheId inlineCacheIndex)
  3631. {
  3632. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  3633. IR::Instr * instr;
  3634. IR::RegOpnd * regOpnd;
  3635. IR::Opnd * srcOpnd;
  3636. IR::SymOpnd * fieldSymOpnd;
  3637. Js::PropertyId propertyId;
  3638. bool isProfiled = OpCodeAttr::IsProfiledOp(newOpcode);
  3639. bool isLdFldThatWasNotProfiled = false;
  3640. if (isProfiled)
  3641. {
  3642. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(newOpcode);
  3643. }
  3644. propertyId = this->m_func->GetJITFunctionBody()->GetPropertyIdFromCacheId(inlineCacheIndex);
  3645. Js::RegSlot instance = this->GetEnvRegForEvalCode();
  3646. switch (newOpcode)
  3647. {
  3648. case Js::OpCode::LdLocalFld:
  3649. if (m_func->GetLocalClosureSym()->HasByteCodeRegSlot())
  3650. {
  3651. IR::ByteCodeUsesInstr * byteCodeUse = IR::ByteCodeUsesInstr::New(m_func, offset);
  3652. byteCodeUse->SetNonOpndSymbol(m_func->GetLocalClosureSym()->m_id);
  3653. this->AddInstr(byteCodeUse, offset);
  3654. }
  3655. newOpcode = Js::OpCode::LdFld;
  3656. fieldSymOpnd = this->BuildFieldOpnd(newOpcode, m_func->GetJITFunctionBody()->GetLocalClosureReg(), propertyId, (Js::PropertyIdIndexType)-1, PropertyKindData, inlineCacheIndex);
  3657. if (fieldSymOpnd->IsPropertySymOpnd())
  3658. {
  3659. fieldSymOpnd->AsPropertySymOpnd()->TryDisableRuntimePolymorphicCache();
  3660. }
  3661. regOpnd = this->BuildDstOpnd(regSlot);
  3662. instr = nullptr;
  3663. if (isProfiled)
  3664. {
  3665. instr = this->BuildProfiledFieldLoad(newOpcode, regOpnd, fieldSymOpnd, inlineCacheIndex, &isLdFldThatWasNotProfiled);
  3666. }
  3667. // If it hasn't been set yet
  3668. if (!instr)
  3669. {
  3670. instr = IR::Instr::New(newOpcode, regOpnd, fieldSymOpnd, m_func);
  3671. }
  3672. break;
  3673. case Js::OpCode::StLocalFld:
  3674. if (m_func->GetLocalClosureSym()->HasByteCodeRegSlot())
  3675. {
  3676. IR::ByteCodeUsesInstr * byteCodeUse = IR::ByteCodeUsesInstr::New(m_func, offset);
  3677. byteCodeUse->SetNonOpndSymbol(m_func->GetLocalClosureSym()->m_id);
  3678. this->AddInstr(byteCodeUse, offset);
  3679. }
  3680. fieldSymOpnd = this->BuildFieldOpnd(newOpcode, m_func->GetJITFunctionBody()->GetLocalClosureReg(), propertyId, (Js::PropertyIdIndexType)-1, PropertyKindData, inlineCacheIndex);
  3681. if (fieldSymOpnd->IsPropertySymOpnd())
  3682. {
  3683. fieldSymOpnd->AsPropertySymOpnd()->TryDisableRuntimePolymorphicCache();
  3684. }
  3685. srcOpnd = this->BuildSrcOpnd(regSlot);
  3686. newOpcode = Js::OpCode::StFld;
  3687. goto stCommon;
  3688. case Js::OpCode::InitLocalFld:
  3689. case Js::OpCode::InitLocalLetFld:
  3690. case Js::OpCode::InitUndeclLocalLetFld:
  3691. case Js::OpCode::InitUndeclLocalConstFld:
  3692. {
  3693. if (m_func->GetLocalClosureSym()->HasByteCodeRegSlot())
  3694. {
  3695. IR::ByteCodeUsesInstr * byteCodeUse = IR::ByteCodeUsesInstr::New(m_func, offset);
  3696. byteCodeUse->SetNonOpndSymbol(m_func->GetLocalClosureSym()->m_id);
  3697. this->AddInstr(byteCodeUse, offset);
  3698. }
  3699. fieldSymOpnd = this->BuildFieldOpnd(newOpcode, m_func->GetJITFunctionBody()->GetLocalClosureReg(), propertyId, (Js::PropertyIdIndexType)-1, PropertyKindData, inlineCacheIndex);
  3700. // Store
  3701. if (newOpcode == Js::OpCode::InitUndeclLocalLetFld)
  3702. {
  3703. srcOpnd = IR::AddrOpnd::New(m_func->GetScriptContextInfo()->GetUndeclBlockVarAddr(), IR::AddrOpndKindDynamicVar, this->m_func, true);
  3704. srcOpnd->SetValueType(ValueType::PrimitiveOrObject);
  3705. newOpcode = Js::OpCode::InitLetFld;
  3706. }
  3707. else if (newOpcode == Js::OpCode::InitUndeclLocalConstFld)
  3708. {
  3709. srcOpnd = IR::AddrOpnd::New(m_func->GetScriptContextInfo()->GetUndeclBlockVarAddr(), IR::AddrOpndKindDynamicVar, this->m_func, true);
  3710. srcOpnd->SetValueType(ValueType::PrimitiveOrObject);
  3711. newOpcode = Js::OpCode::InitConstFld;
  3712. }
  3713. else
  3714. {
  3715. srcOpnd = this->BuildSrcOpnd(regSlot);
  3716. newOpcode = newOpcode == Js::OpCode::InitLocalFld ? Js::OpCode::InitFld : Js::OpCode::InitLetFld;
  3717. }
  3718. stCommon:
  3719. instr = nullptr;
  3720. if (isProfiled)
  3721. {
  3722. if (m_func->DoSimpleJitDynamicProfile())
  3723. {
  3724. instr = IR::JitProfilingInstr::New(newOpcode, fieldSymOpnd, srcOpnd, m_func);
  3725. }
  3726. else if (this->m_func->HasProfileInfo())
  3727. {
  3728. instr = IR::ProfiledInstr::New(newOpcode, fieldSymOpnd, srcOpnd, m_func);
  3729. instr->AsProfiledInstr()->u.FldInfo() = *(m_func->GetReadOnlyProfileInfo()->GetFldInfo(inlineCacheIndex));
  3730. }
  3731. }
  3732. // If it hasn't been set yet
  3733. if (!instr)
  3734. {
  3735. instr = IR::Instr::New(newOpcode, fieldSymOpnd, srcOpnd, m_func);
  3736. }
  3737. break;
  3738. }
  3739. case Js::OpCode::ScopedLdFld:
  3740. case Js::OpCode::ScopedLdFldForTypeOf:
  3741. {
  3742. Assert(!isProfiled);
  3743. Assert(this->m_func->GetScriptContextInfo()->GetAddr() == this->m_func->GetTopFunc()->GetScriptContextInfo()->GetAddr());
  3744. fieldSymOpnd = this->BuildFieldOpnd(newOpcode, instance, propertyId, (Js::PropertyIdIndexType)-1, PropertyKindData, inlineCacheIndex);
  3745. regOpnd = this->BuildDstOpnd(regSlot);
  3746. instr = IR::Instr::New(newOpcode, regOpnd, fieldSymOpnd, m_func);
  3747. break;
  3748. }
  3749. case Js::OpCode::ScopedStFld:
  3750. case Js::OpCode::ConsoleScopedStFld:
  3751. case Js::OpCode::ScopedStFldStrict:
  3752. case Js::OpCode::ConsoleScopedStFldStrict:
  3753. {
  3754. Assert(!isProfiled);
  3755. fieldSymOpnd = this->BuildFieldOpnd(newOpcode, instance, propertyId, (Js::PropertyIdIndexType)-1, PropertyKindData, inlineCacheIndex);
  3756. // Implicit root object as default instance
  3757. IR::Opnd * instance2Opnd = this->BuildSrcOpnd(Js::FunctionBody::RootObjectRegSlot);
  3758. regOpnd = this->BuildSrcOpnd(regSlot);
  3759. instr = IR::Instr::New(newOpcode, fieldSymOpnd, regOpnd, instance2Opnd, m_func);
  3760. break;
  3761. }
  3762. default:
  3763. AssertMsg(UNREACHED, "Unknown ElementP opcode");
  3764. Fatal();
  3765. }
  3766. this->AddInstr(instr, offset);
  3767. if(isLdFldThatWasNotProfiled && DoBailOnNoProfile())
  3768. {
  3769. InsertBailOnNoProfile(instr);
  3770. }
  3771. }
  3772. template <typename SizePolicy>
  3773. void
  3774. IRBuilder::BuildElementPIndexed(Js::OpCode newOpcode, uint32 offset)
  3775. {
  3776. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  3777. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementPIndexed<SizePolicy>>();
  3778. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  3779. {
  3780. this->DoClosureRegCheck(layout->Value);
  3781. }
  3782. switch (newOpcode)
  3783. {
  3784. case Js::OpCode::InitInnerFld:
  3785. newOpcode = Js::OpCode::InitFld;
  3786. goto initinnerfldcommon;
  3787. case Js::OpCode::InitInnerLetFld:
  3788. newOpcode = Js::OpCode::InitLetFld;
  3789. // fall through
  3790. initinnerfldcommon:
  3791. case Js::OpCode::InitUndeclLetFld:
  3792. case Js::OpCode::InitUndeclConstFld:
  3793. BuildElementCP(newOpcode, offset, InnerScopeIndexToRegSlot(layout->scopeIndex), layout->Value, layout->inlineCacheIndex);
  3794. break;
  3795. default:
  3796. AssertMsg(false, "Unknown opcode for ElementPIndexed");
  3797. break;
  3798. }
  3799. }
  3800. template <typename SizePolicy>
  3801. void
  3802. IRBuilder::BuildElementCP(Js::OpCode newOpcode, uint32 offset)
  3803. {
  3804. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  3805. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementCP<SizePolicy>>();
  3806. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  3807. {
  3808. this->DoClosureRegCheck(layout->Value);
  3809. this->DoClosureRegCheck(layout->Instance);
  3810. }
  3811. BuildElementCP(newOpcode, offset, layout->Instance, layout->Value, layout->inlineCacheIndex);
  3812. }
  3813. template <typename SizePolicy>
  3814. void
  3815. IRBuilder::BuildElementRootCP(Js::OpCode newOpcode, uint32 offset)
  3816. {
  3817. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  3818. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementRootCP<SizePolicy>>();
  3819. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  3820. {
  3821. this->DoClosureRegCheck(layout->Value);
  3822. }
  3823. BuildElementCP(newOpcode, offset, Js::FunctionBody::RootObjectRegSlot, layout->Value, layout->inlineCacheIndex);
  3824. }
  3825. void
  3826. IRBuilder::BuildElementCP(Js::OpCode newOpcode, uint32 offset, Js::RegSlot instance, Js::RegSlot regSlot, Js::CacheId inlineCacheIndex)
  3827. {
  3828. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  3829. Js::PropertyId propertyId;
  3830. bool isProfiled = OpCodeAttr::IsProfiledOp(newOpcode);
  3831. if (isProfiled)
  3832. {
  3833. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(newOpcode);
  3834. }
  3835. propertyId = m_func->GetJITFunctionBody()->GetPropertyIdFromCacheId(inlineCacheIndex);
  3836. IR::SymOpnd * fieldSymOpnd = this->BuildFieldOpnd(newOpcode, instance, propertyId, (Js::PropertyIdIndexType)-1, PropertyKindData, inlineCacheIndex);
  3837. IR::RegOpnd * regOpnd;
  3838. IR::Instr * instr = nullptr;
  3839. bool isLdFldThatWasNotProfiled = false;
  3840. switch (newOpcode)
  3841. {
  3842. case Js::OpCode::LdFldForTypeOf:
  3843. case Js::OpCode::LdFld:
  3844. case Js::OpCode::LdLen_A:
  3845. if (fieldSymOpnd->IsPropertySymOpnd())
  3846. {
  3847. fieldSymOpnd->AsPropertySymOpnd()->TryDisableRuntimePolymorphicCache();
  3848. }
  3849. case Js::OpCode::LdFldForCallApplyTarget:
  3850. case Js::OpCode::LdRootFldForTypeOf:
  3851. case Js::OpCode::LdRootFld:
  3852. case Js::OpCode::LdMethodFld:
  3853. case Js::OpCode::LdRootMethodFld:
  3854. case Js::OpCode::ScopedLdMethodFld:
  3855. // Load
  3856. // LdMethodFromFlags is backend only. Don't need to be added here.
  3857. regOpnd = this->BuildDstOpnd(regSlot);
  3858. if (isProfiled)
  3859. {
  3860. instr = this->BuildProfiledFieldLoad(newOpcode, regOpnd, fieldSymOpnd, inlineCacheIndex, &isLdFldThatWasNotProfiled);
  3861. }
  3862. // If it hasn't been set yet
  3863. if (!instr)
  3864. {
  3865. instr = IR::Instr::New(newOpcode, regOpnd, fieldSymOpnd, m_func);
  3866. }
  3867. if (newOpcode == Js::OpCode::LdFld ||
  3868. newOpcode == Js::OpCode::LdFldForCallApplyTarget ||
  3869. newOpcode == Js::OpCode::LdMethodFld ||
  3870. newOpcode == Js::OpCode::LdRootMethodFld ||
  3871. newOpcode == Js::OpCode::ScopedLdMethodFld)
  3872. {
  3873. // Check whether we're loading (what appears to be) a built-in method.
  3874. Js::BuiltinFunction builtInIndex = Js::BuiltinFunction::None;
  3875. PropertySym *fieldSym = fieldSymOpnd->m_sym->AsPropertySym();
  3876. this->CheckBuiltIn(fieldSym, &builtInIndex);
  3877. regOpnd->m_sym->m_builtInIndex = builtInIndex;
  3878. }
  3879. break;
  3880. case Js::OpCode::StFld:
  3881. if (fieldSymOpnd->IsPropertySymOpnd())
  3882. {
  3883. fieldSymOpnd->AsPropertySymOpnd()->TryDisableRuntimePolymorphicCache();
  3884. }
  3885. case Js::OpCode::InitFld:
  3886. case Js::OpCode::InitRootFld:
  3887. case Js::OpCode::InitLetFld:
  3888. case Js::OpCode::InitRootLetFld:
  3889. case Js::OpCode::InitConstFld:
  3890. case Js::OpCode::InitRootConstFld:
  3891. case Js::OpCode::InitUndeclLetFld:
  3892. case Js::OpCode::InitUndeclConstFld:
  3893. case Js::OpCode::InitClassMember:
  3894. case Js::OpCode::StRootFld:
  3895. case Js::OpCode::StFldStrict:
  3896. case Js::OpCode::StRootFldStrict:
  3897. {
  3898. IR::Opnd *srcOpnd;
  3899. // Store
  3900. if (newOpcode == Js::OpCode::InitUndeclLetFld)
  3901. {
  3902. srcOpnd = IR::AddrOpnd::New(m_func->GetScriptContextInfo()->GetUndeclBlockVarAddr(), IR::AddrOpndKindDynamicVar, this->m_func, true);
  3903. srcOpnd->SetValueType(ValueType::PrimitiveOrObject);
  3904. newOpcode = Js::OpCode::InitLetFld;
  3905. }
  3906. else if (newOpcode == Js::OpCode::InitUndeclConstFld)
  3907. {
  3908. srcOpnd = IR::AddrOpnd::New(m_func->GetScriptContextInfo()->GetUndeclBlockVarAddr(), IR::AddrOpndKindDynamicVar, this->m_func, true);
  3909. srcOpnd->SetValueType(ValueType::PrimitiveOrObject);
  3910. newOpcode = Js::OpCode::InitConstFld;
  3911. }
  3912. else
  3913. {
  3914. srcOpnd = this->BuildSrcOpnd(regSlot);
  3915. }
  3916. if (isProfiled)
  3917. {
  3918. if (m_func->DoSimpleJitDynamicProfile())
  3919. {
  3920. instr = IR::JitProfilingInstr::New(newOpcode, fieldSymOpnd, srcOpnd, m_func);
  3921. }
  3922. else if (this->m_func->HasProfileInfo())
  3923. {
  3924. instr = IR::ProfiledInstr::New(newOpcode, fieldSymOpnd, srcOpnd, m_func);
  3925. instr->AsProfiledInstr()->u.FldInfo() = *(m_func->GetReadOnlyProfileInfo()->GetFldInfo(inlineCacheIndex));
  3926. }
  3927. }
  3928. // If it hasn't been set yet
  3929. if (!instr)
  3930. {
  3931. instr = IR::Instr::New(newOpcode, fieldSymOpnd, srcOpnd, m_func);
  3932. }
  3933. break;
  3934. }
  3935. default:
  3936. AssertMsg(UNREACHED, "Unknown ElementCP opcode");
  3937. Fatal();
  3938. }
  3939. this->AddInstr(instr, offset);
  3940. if(isLdFldThatWasNotProfiled && DoBailOnNoProfile())
  3941. {
  3942. InsertBailOnNoProfile(instr);
  3943. }
  3944. }
  3945. template <typename SizePolicy>
  3946. void
  3947. IRBuilder::BuildProfiledElementCP(Js::OpCode newOpcode, uint32 offset)
  3948. {
  3949. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  3950. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_ElementCP<SizePolicy>>>();
  3951. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  3952. {
  3953. this->DoClosureRegCheck(layout->Value);
  3954. this->DoClosureRegCheck(layout->Instance);
  3955. }
  3956. BuildProfiledElementCP(newOpcode, offset, layout->Instance, layout->Value, layout->inlineCacheIndex, layout->profileId);
  3957. }
  3958. void
  3959. IRBuilder::BuildProfiledElementCP(Js::OpCode newOpcode, uint32 offset, Js::RegSlot instance, Js::RegSlot regSlot, Js::CacheId inlineCacheIndex, Js::ProfileId profileId)
  3960. {
  3961. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  3962. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(newOpcode);
  3963. Assert(newOpcode == Js::OpCode::LdLen_A);
  3964. Js::PropertyId propertyId = m_func->GetJITFunctionBody()->GetPropertyIdFromCacheId(inlineCacheIndex);
  3965. IR::SymOpnd * fieldSymOpnd = this->BuildFieldOpnd(newOpcode, instance, propertyId, (Js::PropertyIdIndexType) - 1, PropertyKindData, inlineCacheIndex);
  3966. IR::RegOpnd * dstOpnd = this->BuildDstOpnd(regSlot);
  3967. bool isProfiled = (profileId != Js::Constants::NoProfileId);
  3968. ValueType arrayType = ValueType::Uninitialized;
  3969. const Js::LdLenInfo * ldLenInfo = nullptr;
  3970. if (m_func->HasProfileInfo())
  3971. {
  3972. ldLenInfo = m_func->GetReadOnlyProfileInfo()->GetLdLenInfo(profileId);
  3973. arrayType = (ldLenInfo->GetArrayType());
  3974. if (arrayType.IsLikelyNativeArray() && !AllowNativeArrayProfileInfo())
  3975. {
  3976. // An opnd's value type will get replaced in the forward phase when it is not fixed. Store the array type in the ProfiledInstr.
  3977. arrayType = arrayType.SetArrayTypeId(Js::TypeIds_Array);
  3978. }
  3979. fieldSymOpnd->SetValueType(arrayType);
  3980. if (m_func->GetTopFunc()->HasTry() && !m_func->GetTopFunc()->DoOptimizeTry())
  3981. {
  3982. isProfiled = false;
  3983. }
  3984. }
  3985. else
  3986. {
  3987. isProfiled = false;
  3988. }
  3989. bool wasNotProfiled = false;
  3990. IR::Instr *instr = nullptr;
  3991. if (isProfiled)
  3992. {
  3993. instr = this->BuildProfiledFieldLoad(newOpcode, dstOpnd, fieldSymOpnd, inlineCacheIndex, &wasNotProfiled);
  3994. }
  3995. if (instr == nullptr)
  3996. {
  3997. instr = IR::Instr::New(newOpcode, dstOpnd, fieldSymOpnd, m_func);
  3998. }
  3999. else if (instr->IsJitProfilingInstr())
  4000. {
  4001. instr->AsJitProfilingInstr()->profileId = profileId;
  4002. }
  4003. else if (instr->IsProfiledInstr())
  4004. {
  4005. instr->AsProfiledInstr()->u.LdLenInfo() = *ldLenInfo;
  4006. instr->AsProfiledInstr()->u.LdLenInfo().arrayType = arrayType;
  4007. }
  4008. this->AddInstr(instr, offset);
  4009. if (wasNotProfiled && DoBailOnNoProfile())
  4010. {
  4011. InsertBailOnNoProfile(instr);
  4012. }
  4013. }
  4014. ///----------------------------------------------------------------------------
  4015. ///
  4016. /// IRBuilder::BuildElementC2
  4017. ///
  4018. /// Build IR instr for an ElementC2 instruction.
  4019. ///
  4020. ///----------------------------------------------------------------------------
  4021. template <typename SizePolicy>
  4022. void
  4023. IRBuilder::BuildElementScopedC2(Js::OpCode newOpcode, uint32 offset)
  4024. {
  4025. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  4026. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementScopedC2<SizePolicy>>();
  4027. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  4028. {
  4029. this->DoClosureRegCheck(layout->Value);
  4030. this->DoClosureRegCheck(layout->Value2);
  4031. }
  4032. BuildElementScopedC2(newOpcode, offset, layout->Value2, layout->Value, layout->PropertyIdIndex);
  4033. }
  4034. void
  4035. IRBuilder::BuildElementScopedC2(Js::OpCode newOpcode, uint32 offset, Js::RegSlot value2Slot,
  4036. Js::RegSlot regSlot, Js::PropertyIdIndexType propertyIdIndex)
  4037. {
  4038. IR::Instr * instr = nullptr;
  4039. Js::PropertyId propertyId;
  4040. IR::RegOpnd * regOpnd;
  4041. IR::RegOpnd * value2Opnd;
  4042. IR::SymOpnd * fieldSymOpnd;
  4043. Js::RegSlot instanceSlot = this->GetEnvRegForEvalCode();
  4044. switch (newOpcode)
  4045. {
  4046. case Js::OpCode::ScopedLdInst:
  4047. {
  4048. propertyId = m_func->GetJITFunctionBody()->GetReferencedPropertyId(propertyIdIndex);
  4049. fieldSymOpnd = this->BuildFieldOpnd(newOpcode, instanceSlot, propertyId, propertyIdIndex, PropertyKindData);
  4050. regOpnd = this->BuildDstOpnd(regSlot);
  4051. value2Opnd = this->BuildDstOpnd(value2Slot);
  4052. IR::Instr *newInstr = IR::Instr::New(Js::OpCode::Unused, value2Opnd, m_func);
  4053. this->AddInstr(newInstr, offset);
  4054. instr = IR::Instr::New(newOpcode, regOpnd, fieldSymOpnd, newInstr->GetDst(), m_func);
  4055. this->AddInstr(instr, offset);
  4056. }
  4057. break;
  4058. default:
  4059. AssertMsg(UNREACHED, "Unknown ElementC2 opcode");
  4060. Fatal();
  4061. }
  4062. }
  4063. template <typename SizePolicy>
  4064. void
  4065. IRBuilder::BuildElementC2(Js::OpCode newOpcode, uint32 offset)
  4066. {
  4067. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  4068. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementC2<SizePolicy>>();
  4069. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  4070. {
  4071. this->DoClosureRegCheck(layout->Value);
  4072. this->DoClosureRegCheck(layout->Value2);
  4073. this->DoClosureRegCheck(layout->Instance);
  4074. }
  4075. BuildElementC2(newOpcode, offset, layout->Instance, layout->Value2, layout->Value, layout->PropertyIdIndex);
  4076. }
  4077. void
  4078. IRBuilder::BuildElementC2(Js::OpCode newOpcode, uint32 offset, Js::RegSlot instanceSlot, Js::RegSlot value2Slot,
  4079. Js::RegSlot regSlot, Js::PropertyIdIndexType propertyIdIndex)
  4080. {
  4081. IR::Instr * instr = nullptr;
  4082. Js::PropertyId propertyId;
  4083. IR::RegOpnd * regOpnd;
  4084. IR::RegOpnd * value2Opnd;
  4085. IR::SymOpnd * fieldSymOpnd;
  4086. switch (newOpcode)
  4087. {
  4088. case Js::OpCode::ProfiledLdSuperFld:
  4089. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(newOpcode);
  4090. // fall-through
  4091. case Js::OpCode::LdSuperFld:
  4092. {
  4093. propertyId = m_func->GetJITFunctionBody()->GetPropertyIdFromCacheId(propertyIdIndex);
  4094. fieldSymOpnd = this->BuildFieldOpnd(newOpcode, instanceSlot, propertyId, (Js::PropertyIdIndexType) - 1, PropertyKindData, propertyIdIndex);
  4095. if (fieldSymOpnd->IsPropertySymOpnd())
  4096. {
  4097. fieldSymOpnd->AsPropertySymOpnd()->TryDisableRuntimePolymorphicCache();
  4098. }
  4099. value2Opnd = this->BuildSrcOpnd(value2Slot);
  4100. regOpnd = this->BuildDstOpnd(regSlot);
  4101. instr = IR::ProfiledInstr::New(newOpcode, regOpnd, fieldSymOpnd, value2Opnd, m_func);
  4102. instr->AsProfiledInstr()->u.FldInfo() = *(m_func->GetReadOnlyProfileInfo()->GetFldInfo(propertyIdIndex));
  4103. this->AddInstr(instr, offset);
  4104. }
  4105. break;
  4106. case Js::OpCode::ProfiledStSuperFld:
  4107. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(newOpcode);
  4108. // fall-through
  4109. case Js::OpCode::StSuperFld:
  4110. {
  4111. propertyId = m_func->GetJITFunctionBody()->GetPropertyIdFromCacheId(propertyIdIndex);
  4112. fieldSymOpnd = this->BuildFieldOpnd(newOpcode, instanceSlot, propertyId, (Js::PropertyIdIndexType) - 1, PropertyKindData, propertyIdIndex);
  4113. if (fieldSymOpnd->IsPropertySymOpnd())
  4114. {
  4115. fieldSymOpnd->AsPropertySymOpnd()->TryDisableRuntimePolymorphicCache();
  4116. }
  4117. regOpnd = this->BuildSrcOpnd(regSlot);
  4118. value2Opnd = this->BuildSrcOpnd(value2Slot);
  4119. instr = IR::ProfiledInstr::New(newOpcode, fieldSymOpnd, regOpnd, value2Opnd, m_func);
  4120. instr->AsProfiledInstr()->u.FldInfo() = *(m_func->GetReadOnlyProfileInfo()->GetFldInfo(propertyIdIndex));
  4121. this->AddInstr(instr, offset);
  4122. break;
  4123. }
  4124. default:
  4125. AssertMsg(UNREACHED, "Unknown ElementC2 opcode");
  4126. Fatal();
  4127. }
  4128. }
  4129. ///----------------------------------------------------------------------------
  4130. ///
  4131. /// IRBuilder::BuildElementU
  4132. ///
  4133. /// Build IR instr for an ElementU or ElementRootU instruction.
  4134. ///
  4135. ///----------------------------------------------------------------------------
  4136. template <typename SizePolicy>
  4137. void
  4138. IRBuilder::BuildElementU(Js::OpCode newOpcode, uint32 offset)
  4139. {
  4140. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  4141. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  4142. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementU<SizePolicy>>();
  4143. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  4144. {
  4145. this->DoClosureRegCheck(layout->Instance);
  4146. }
  4147. BuildElementU(newOpcode, offset, layout->Instance, layout->PropertyIdIndex);
  4148. }
  4149. template <typename SizePolicy>
  4150. void
  4151. IRBuilder::BuildElementRootU(Js::OpCode newOpcode, uint32 offset)
  4152. {
  4153. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  4154. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  4155. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementRootU<SizePolicy>>();
  4156. BuildElementU(newOpcode, offset, Js::FunctionBody::RootObjectRegSlot, layout->PropertyIdIndex);
  4157. }
  4158. template <typename SizePolicy>
  4159. void
  4160. IRBuilder::BuildElementScopedU(Js::OpCode newOpcode, uint32 offset)
  4161. {
  4162. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  4163. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  4164. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementScopedU<SizePolicy>>();
  4165. BuildElementU(newOpcode, offset, GetEnvReg(), layout->PropertyIdIndex);
  4166. }
  4167. void
  4168. IRBuilder::BuildElementU(Js::OpCode newOpcode, uint32 offset, Js::RegSlot instance, Js::PropertyIdIndexType propertyIdIndex)
  4169. {
  4170. IR::Instr * instr;
  4171. IR::RegOpnd * regOpnd;
  4172. IR::SymOpnd * fieldSymOpnd;
  4173. Js::PropertyId propertyId = m_func->GetJITFunctionBody()->GetReferencedPropertyId(propertyIdIndex);
  4174. switch (newOpcode)
  4175. {
  4176. case Js::OpCode::LdLocalElemUndef:
  4177. if (m_func->GetLocalClosureSym()->HasByteCodeRegSlot())
  4178. {
  4179. IR::ByteCodeUsesInstr * byteCodeUse = IR::ByteCodeUsesInstr::New(m_func, offset);
  4180. byteCodeUse->SetNonOpndSymbol(m_func->GetLocalClosureSym()->m_id);
  4181. this->AddInstr(byteCodeUse, offset);
  4182. }
  4183. instance = m_func->GetJITFunctionBody()->GetLocalClosureReg();
  4184. newOpcode = Js::OpCode::LdElemUndef;
  4185. fieldSymOpnd = this->BuildFieldOpnd(newOpcode, instance, propertyId, propertyIdIndex, PropertyKindData);
  4186. instr = IR::Instr::New(newOpcode, fieldSymOpnd, m_func);
  4187. break;
  4188. // fall through
  4189. case Js::OpCode::LdElemUndefScoped:
  4190. {
  4191. // Store
  4192. PropertyKind propertyKind = PropertyKindData;
  4193. fieldSymOpnd = this->BuildFieldOpnd(newOpcode, instance, propertyId, propertyIdIndex, propertyKind);
  4194. // Implicit root object as default instance
  4195. regOpnd = this->BuildSrcOpnd(Js::FunctionBody::RootObjectRegSlot);
  4196. instr = IR::Instr::New(newOpcode, fieldSymOpnd, regOpnd, m_func);
  4197. break;
  4198. }
  4199. case Js::OpCode::ClearAttributes:
  4200. {
  4201. instr = IR::Instr::New(newOpcode, m_func);
  4202. IR::RegOpnd * src1Opnd = this->BuildSrcOpnd(instance);
  4203. IR::IntConstOpnd * src2Opnd = IR::IntConstOpnd::New(propertyId, TyInt32, m_func);
  4204. instr->SetSrc1(src1Opnd);
  4205. instr->SetSrc2(src2Opnd);
  4206. break;
  4207. }
  4208. case Js::OpCode::StLocalFuncExpr:
  4209. fieldSymOpnd = this->BuildFieldOpnd(newOpcode, m_func->GetJITFunctionBody()->GetLocalClosureReg(), propertyId, propertyIdIndex, PropertyKindData);
  4210. regOpnd = this->BuildSrcOpnd(instance);
  4211. newOpcode = Js::OpCode::StFuncExpr;
  4212. instr = IR::Instr::New(newOpcode, fieldSymOpnd, regOpnd, m_func);
  4213. break;
  4214. case Js::OpCode::DeleteLocalFld:
  4215. newOpcode = Js::OpCode::DeleteFld;
  4216. fieldSymOpnd = BuildFieldOpnd(newOpcode, m_func->GetJITFunctionBody()->GetLocalClosureReg(), propertyId, propertyIdIndex, PropertyKindData);
  4217. regOpnd = BuildDstOpnd(instance);
  4218. instr = IR::Instr::New(newOpcode, regOpnd, fieldSymOpnd, m_func);
  4219. break;
  4220. default:
  4221. {
  4222. fieldSymOpnd = this->BuildFieldOpnd(newOpcode, instance, propertyId, propertyIdIndex, PropertyKindData);
  4223. instr = IR::Instr::New(newOpcode, fieldSymOpnd, m_func);
  4224. break;
  4225. }
  4226. }
  4227. this->AddInstr(instr, offset);
  4228. }
  4229. ///----------------------------------------------------------------------------
  4230. ///
  4231. /// IRBuilder::BuildAuxiliary
  4232. ///
  4233. /// Build IR instr for an Auxiliary instruction.
  4234. ///
  4235. ///----------------------------------------------------------------------------
  4236. void
  4237. IRBuilder::BuildAuxNoReg(Js::OpCode newOpcode, uint32 offset)
  4238. {
  4239. Assert(!OpCodeAttr::HasMultiSizeLayout(newOpcode));
  4240. IR::Instr * instr;
  4241. const unaligned Js::OpLayoutAuxNoReg *auxInsn = m_jnReader.AuxNoReg();
  4242. switch (newOpcode)
  4243. {
  4244. case Js::OpCode::InitCachedFuncs:
  4245. {
  4246. IR::Opnd *src1Opnd = this->BuildSrcOpnd(m_func->GetJITFunctionBody()->GetLocalClosureReg());
  4247. IR::Opnd *src2Opnd = this->BuildSrcOpnd(m_func->GetJITFunctionBody()->GetLocalFrameDisplayReg());
  4248. IR::Opnd *src3Opnd = this->BuildAuxArrayOpnd(AuxArrayValue::AuxFuncInfoArray, auxInsn->Offset);
  4249. instr = IR::Instr::New(Js::OpCode::ArgOut_A, IR::RegOpnd::New(TyVar, m_func), src3Opnd, m_func);
  4250. this->AddInstr(instr, offset);
  4251. instr = IR::Instr::New(Js::OpCode::ArgOut_A, IR::RegOpnd::New(TyVar, m_func), src2Opnd, instr->GetDst(), m_func);
  4252. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  4253. instr = IR::Instr::New(Js::OpCode::ArgOut_A, IR::RegOpnd::New(TyVar, m_func), src1Opnd, instr->GetDst(), m_func);
  4254. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  4255. IR::HelperCallOpnd *helperOpnd;
  4256. helperOpnd = IR::HelperCallOpnd::New(IR::HelperOP_InitCachedFuncs, this->m_func);
  4257. src2Opnd = instr->GetDst();
  4258. instr = IR::Instr::New(Js::OpCode::CallHelper, m_func);
  4259. instr->SetSrc1(helperOpnd);
  4260. instr->SetSrc2(src2Opnd);
  4261. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  4262. return;
  4263. }
  4264. default:
  4265. {
  4266. AssertMsg(UNREACHED, "Unknown AuxNoReg opcode");
  4267. Fatal();
  4268. break;
  4269. }
  4270. }
  4271. }
  4272. void
  4273. IRBuilder::BuildAuxiliary(Js::OpCode newOpcode, uint32 offset)
  4274. {
  4275. Assert(!OpCodeAttr::HasMultiSizeLayout(newOpcode));
  4276. const unaligned Js::OpLayoutAuxiliary *auxInsn = m_jnReader.Auxiliary();
  4277. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  4278. {
  4279. this->DoClosureRegCheck(auxInsn->R0);
  4280. }
  4281. IR::Instr *instr;
  4282. switch (newOpcode)
  4283. {
  4284. case Js::OpCode::NewScObjectLiteral:
  4285. {
  4286. int literalObjectId = auxInsn->C1;
  4287. IR::RegOpnd * dstOpnd;
  4288. IR::Opnd* srcOpnd;
  4289. Js::RegSlot dstRegSlot = auxInsn->R0;
  4290. // The property ID array needs to be both relocatable and available (so we can
  4291. // get the slot capacity), so we need to just pass the offset to lower and let
  4292. // lower take it from there...
  4293. srcOpnd = IR::IntConstOpnd::New(auxInsn->Offset, TyUint32, m_func);
  4294. dstOpnd = this->BuildDstOpnd(dstRegSlot);
  4295. dstOpnd->SetValueType(ValueType::GetObject(ObjectType::UninitializedObject));
  4296. instr = IR::Instr::New(newOpcode, dstOpnd, srcOpnd, m_func);
  4297. // Because we're going to be making decisions based off the value, we have to defer
  4298. // this until we get to lowering.
  4299. instr->SetSrc2(IR::IntConstOpnd::New(literalObjectId, TyUint32, m_func));
  4300. if (dstOpnd->m_sym->m_isSingleDef)
  4301. {
  4302. dstOpnd->m_sym->m_isSafeThis = true;
  4303. }
  4304. break;
  4305. }
  4306. case Js::OpCode::LdPropIds:
  4307. {
  4308. IR::RegOpnd * dstOpnd;
  4309. IR::Opnd* srcOpnd;
  4310. Js::RegSlot dstRegSlot = auxInsn->R0;
  4311. srcOpnd = this->BuildAuxArrayOpnd(AuxArrayValue::AuxPropertyIdArray, auxInsn->Offset);
  4312. dstOpnd = this->BuildDstOpnd(dstRegSlot);
  4313. instr = IR::Instr::New(newOpcode, dstOpnd, srcOpnd, m_func);
  4314. if (dstOpnd->m_sym->m_isSingleDef)
  4315. {
  4316. dstOpnd->m_sym->m_isNotNumber = true;
  4317. }
  4318. break;
  4319. }
  4320. case Js::OpCode::NewScIntArray:
  4321. {
  4322. IR::RegOpnd* dstOpnd;
  4323. IR::Opnd* src1Opnd;
  4324. src1Opnd = this->BuildAuxArrayOpnd(AuxArrayValue::AuxIntArray, auxInsn->Offset);
  4325. dstOpnd = this->BuildDstOpnd(auxInsn->R0);
  4326. instr = IR::Instr::New(newOpcode, dstOpnd, src1Opnd, m_func);
  4327. const Js::TypeId arrayTypeId = m_func->IsJitInDebugMode() ? Js::TypeIds_Array : Js::TypeIds_NativeIntArray;
  4328. dstOpnd->SetValueType(
  4329. ValueType::GetObject(ObjectType::Array).SetHasNoMissingValues(true).SetArrayTypeId(arrayTypeId));
  4330. dstOpnd->SetValueTypeFixed();
  4331. break;
  4332. }
  4333. case Js::OpCode::NewScFltArray:
  4334. {
  4335. IR::RegOpnd* dstOpnd;
  4336. IR::Opnd* src1Opnd;
  4337. src1Opnd = this->BuildAuxArrayOpnd(AuxArrayValue::AuxFloatArray, auxInsn->Offset);
  4338. dstOpnd = this->BuildDstOpnd(auxInsn->R0);
  4339. instr = IR::Instr::New(newOpcode, dstOpnd, src1Opnd, m_func);
  4340. const Js::TypeId arrayTypeId = m_func->IsJitInDebugMode() ? Js::TypeIds_Array : Js::TypeIds_NativeFloatArray;
  4341. dstOpnd->SetValueType(
  4342. ValueType::GetObject(ObjectType::Array).SetHasNoMissingValues(true).SetArrayTypeId(arrayTypeId));
  4343. dstOpnd->SetValueTypeFixed();
  4344. break;
  4345. }
  4346. case Js::OpCode::StArrSegItem_A:
  4347. {
  4348. IR::RegOpnd* src1Opnd;
  4349. IR::Opnd* src2Opnd;
  4350. src1Opnd = this->BuildSrcOpnd(auxInsn->R0);
  4351. src2Opnd = this->BuildAuxArrayOpnd(AuxArrayValue::AuxVarsArray, auxInsn->Offset);
  4352. instr = IR::Instr::New(newOpcode, m_func);
  4353. instr->SetSrc1(src1Opnd);
  4354. instr->SetSrc2(src2Opnd);
  4355. break;
  4356. }
  4357. case Js::OpCode::NewScObject_A:
  4358. {
  4359. const Js::VarArrayVarCount *vars = (Js::VarArrayVarCount *)m_func->GetJITFunctionBody()->ReadFromAuxContextData(auxInsn->Offset);
  4360. int count = Js::TaggedInt::ToInt32(vars->count);
  4361. StackSym * symDst;
  4362. IR::SymOpnd * dstOpnd;
  4363. IR::Opnd * src1Opnd;
  4364. //
  4365. // PUSH all the parameters on the auxiliary context, to the stack
  4366. //
  4367. for (int i=0;i<count; i++)
  4368. {
  4369. m_argsOnStack++;
  4370. symDst = m_func->m_symTable->GetArgSlotSym((uint16)(i + 2));
  4371. if (symDst == nullptr || (uint16)(i + 2) != (i + 2))
  4372. {
  4373. AssertMsg(UNREACHED, "Arg count too big...");
  4374. Fatal();
  4375. }
  4376. dstOpnd = IR::SymOpnd::New(symDst, TyVar, m_func);
  4377. src1Opnd = IR::AddrOpnd::New(vars->elements[i], IR::AddrOpndKindDynamicVar, this->m_func, true);
  4378. instr = IR::Instr::New(Js::OpCode::ArgOut_A, dstOpnd, src1Opnd, m_func);
  4379. this->AddInstr(instr, offset);
  4380. m_argStack->Push(instr);
  4381. }
  4382. BuildCallI_Helper(Js::OpCode::NewScObject, offset, (Js::RegSlot)auxInsn->R0, (Js::RegSlot)auxInsn->C1, (Js::ArgSlot)count+1, Js::Constants::NoProfileId);
  4383. return;
  4384. }
  4385. default:
  4386. {
  4387. AssertMsg(UNREACHED, "Unknown Auxiliary opcode");
  4388. Fatal();
  4389. break;
  4390. }
  4391. }
  4392. this->AddInstr(instr, offset);
  4393. }
  4394. void
  4395. IRBuilder::BuildProfiledAuxiliary(Js::OpCode newOpcode, uint32 offset)
  4396. {
  4397. Assert(!OpCodeAttr::HasMultiSizeLayout(newOpcode));
  4398. const unaligned Js::OpLayoutDynamicProfile<Js::OpLayoutAuxiliary> *auxInsn = m_jnReader.ProfiledAuxiliary();
  4399. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  4400. {
  4401. this->DoClosureRegCheck(auxInsn->R0);
  4402. }
  4403. switch (newOpcode)
  4404. {
  4405. case Js::OpCode::ProfiledNewScIntArray:
  4406. {
  4407. Js::ProfileId profileId = static_cast<Js::ProfileId>(auxInsn->profileId);
  4408. IR::RegOpnd* dstOpnd;
  4409. IR::Opnd* src1Opnd;
  4410. src1Opnd = this->BuildAuxArrayOpnd(AuxArrayValue::AuxIntArray, auxInsn->Offset);
  4411. dstOpnd = this->BuildDstOpnd(auxInsn->R0);
  4412. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(newOpcode);
  4413. IR::Instr *instr;
  4414. Js::ArrayCallSiteInfo *arrayInfo = nullptr;
  4415. Js::TypeId arrayTypeId = Js::TypeIds_Array;
  4416. if (m_func->DoSimpleJitDynamicProfile())
  4417. {
  4418. instr = IR::JitProfilingInstr::New(newOpcode, dstOpnd, src1Opnd, m_func);
  4419. instr->AsJitProfilingInstr()->profileId = profileId;
  4420. }
  4421. else if (m_func->HasArrayInfo())
  4422. {
  4423. instr = IR::ProfiledInstr::New(newOpcode, dstOpnd, src1Opnd, m_func);
  4424. instr->AsProfiledInstr()->u.profileId = profileId;
  4425. arrayInfo = m_func->GetReadOnlyProfileInfo()->GetArrayCallSiteInfo(profileId);
  4426. if (arrayInfo && !m_func->IsJitInDebugMode())
  4427. {
  4428. if (arrayInfo->IsNativeIntArray())
  4429. {
  4430. arrayTypeId = Js::TypeIds_NativeIntArray;
  4431. }
  4432. else if (arrayInfo->IsNativeFloatArray())
  4433. {
  4434. arrayTypeId = Js::TypeIds_NativeFloatArray;
  4435. }
  4436. }
  4437. }
  4438. else
  4439. {
  4440. instr = IR::Instr::New(newOpcode, dstOpnd, src1Opnd, m_func);
  4441. }
  4442. ValueType dstValueType(
  4443. ValueType::GetObject(ObjectType::Array).SetHasNoMissingValues(true).SetArrayTypeId(arrayTypeId));
  4444. if (dstValueType.IsLikelyNativeArray())
  4445. {
  4446. dstOpnd->SetValueType(dstValueType.ToLikely());
  4447. }
  4448. else
  4449. {
  4450. dstOpnd->SetValueType(dstValueType);
  4451. dstOpnd->SetValueTypeFixed();
  4452. }
  4453. StackSym *dstSym = dstOpnd->AsRegOpnd()->m_sym;
  4454. if (dstSym->m_isSingleDef)
  4455. {
  4456. dstSym->m_isSafeThis = true;
  4457. dstSym->m_isNotNumber = true;
  4458. }
  4459. this->AddInstr(instr, offset);
  4460. break;
  4461. }
  4462. case Js::OpCode::ProfiledNewScFltArray:
  4463. {
  4464. Js::ProfileId profileId = static_cast<Js::ProfileId>(auxInsn->profileId);
  4465. IR::RegOpnd* dstOpnd;
  4466. IR::Opnd* src1Opnd;
  4467. src1Opnd = this->BuildAuxArrayOpnd(AuxArrayValue::AuxFloatArray, auxInsn->Offset);
  4468. dstOpnd = this->BuildDstOpnd(auxInsn->R0);
  4469. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(newOpcode);
  4470. IR::Instr *instr;
  4471. Js::ArrayCallSiteInfo *arrayInfo = nullptr;
  4472. if (m_func->DoSimpleJitDynamicProfile())
  4473. {
  4474. instr = IR::JitProfilingInstr::New(newOpcode, dstOpnd, src1Opnd, m_func);
  4475. instr->AsJitProfilingInstr()->profileId = profileId;
  4476. // Keep arrayInfo null because we aren't using profile data in profiling simplejit
  4477. }
  4478. else
  4479. {
  4480. instr = IR::ProfiledInstr::New(newOpcode, dstOpnd, src1Opnd, m_func);
  4481. instr->AsProfiledInstr()->u.profileId = profileId;
  4482. if (m_func->HasArrayInfo()) {
  4483. arrayInfo = m_func->GetReadOnlyProfileInfo()->GetArrayCallSiteInfo(profileId);
  4484. }
  4485. }
  4486. Js::TypeId arrayTypeId;
  4487. if (arrayInfo && arrayInfo->IsNativeFloatArray())
  4488. {
  4489. arrayTypeId = Js::TypeIds_NativeFloatArray;
  4490. }
  4491. else
  4492. {
  4493. arrayTypeId = Js::TypeIds_Array;
  4494. }
  4495. ValueType dstValueType(
  4496. ValueType::GetObject(ObjectType::Array).SetHasNoMissingValues(true).SetArrayTypeId(arrayTypeId));
  4497. if (dstValueType.IsLikelyNativeArray())
  4498. {
  4499. dstOpnd->SetValueType(dstValueType.ToLikely());
  4500. }
  4501. else
  4502. {
  4503. dstOpnd->SetValueType(dstValueType);
  4504. dstOpnd->SetValueTypeFixed();
  4505. }
  4506. StackSym *dstSym = dstOpnd->AsRegOpnd()->m_sym;
  4507. if (dstSym->m_isSingleDef)
  4508. {
  4509. dstSym->m_isSafeThis = true;
  4510. dstSym->m_isNotNumber = true;
  4511. }
  4512. this->AddInstr(instr, offset);
  4513. break;
  4514. }
  4515. default:
  4516. {
  4517. AssertMsg(UNREACHED, "Unknown Auxiliary opcode");
  4518. Fatal();
  4519. break;
  4520. }
  4521. }
  4522. }
  4523. ///----------------------------------------------------------------------------
  4524. ///
  4525. /// IRBuilder::BuildReg2Aux
  4526. ///
  4527. /// Build IR instr for a Reg2Aux instruction.
  4528. ///
  4529. ///----------------------------------------------------------------------------
  4530. void IRBuilder::BuildInitCachedScope(int auxOffset, int offset)
  4531. {
  4532. IR::Instr * instr;
  4533. IR::RegOpnd * dstOpnd;
  4534. IR::RegOpnd * src1Opnd;
  4535. IR::AddrOpnd * src2Opnd;
  4536. IR::Opnd* src3Opnd;
  4537. IR::Opnd* formalsAreLetDeclOpnd;
  4538. src2Opnd = IR::AddrOpnd::New(m_func->GetJITFunctionBody()->GetFormalsPropIdArrayAddr(), IR::AddrOpndKindDynamicMisc, m_func);
  4539. Js::PropertyIdArray * propIds = m_func->GetJITFunctionBody()->GetFormalsPropIdArray();
  4540. src3Opnd = this->BuildAuxObjectLiteralTypeRefOpnd(Js::ActivationObjectEx::GetLiteralObjectRef(propIds));
  4541. dstOpnd = this->BuildDstOpnd(m_func->GetJITFunctionBody()->GetLocalClosureReg());
  4542. formalsAreLetDeclOpnd = IR::IntConstOpnd::New(propIds->hasNonSimpleParams, TyUint8, m_func);
  4543. instr = IR::Instr::New(Js::OpCode::ExtendArg_A, IR::RegOpnd::New(TyVar, m_func), formalsAreLetDeclOpnd, m_func);
  4544. this->AddInstr(instr, offset);
  4545. instr = IR::Instr::New(Js::OpCode::ExtendArg_A, IR::RegOpnd::New(TyVar, m_func), src3Opnd, instr->GetDst(), m_func);
  4546. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  4547. instr = IR::Instr::New(Js::OpCode::ExtendArg_A, IR::RegOpnd::New(TyVar, m_func), src2Opnd, instr->GetDst(), m_func);
  4548. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  4549. // Disable opt that normally gets disabled when we see LdFuncExpr in the byte code.
  4550. m_func->DisableCanDoInlineArgOpt();
  4551. src1Opnd = IR::RegOpnd::New(TyVar, m_func);
  4552. IR::Instr * instrLdFuncExpr = IR::Instr::New(Js::OpCode::LdFuncExpr, src1Opnd, m_func);
  4553. this->AddInstr(instrLdFuncExpr, Js::Constants::NoByteCodeOffset);
  4554. instr = IR::Instr::New(Js::OpCode::ExtendArg_A, IR::RegOpnd::New(TyVar, m_func), src1Opnd, instr->GetDst(), m_func);
  4555. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  4556. instr = IR::Instr::New(Js::OpCode::InitCachedScope, dstOpnd, instr->GetDst(), m_func);
  4557. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  4558. }
  4559. void
  4560. IRBuilder::BuildReg2Aux(Js::OpCode newOpcode, uint32 offset)
  4561. {
  4562. Assert(!OpCodeAttr::HasMultiSizeLayout(newOpcode));
  4563. const unaligned Js::OpLayoutReg2Aux *auxInsn = m_jnReader.Reg2Aux();
  4564. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  4565. {
  4566. this->DoClosureRegCheck(auxInsn->R0);
  4567. this->DoClosureRegCheck(auxInsn->R1);
  4568. }
  4569. IR::Instr *instr;
  4570. switch (newOpcode)
  4571. {
  4572. case Js::OpCode::SpreadArrayLiteral:
  4573. {
  4574. IR::RegOpnd * dstOpnd;
  4575. IR::RegOpnd * src1Opnd;
  4576. IR::Opnd* src2Opnd;
  4577. Js::RegSlot dstRegSlot = auxInsn->R0;
  4578. Js::RegSlot srcRegSlot = auxInsn->R1;
  4579. src1Opnd = this->BuildSrcOpnd(srcRegSlot);
  4580. src2Opnd = this->BuildAuxArrayOpnd(AuxArrayValue::AuxIntArray, auxInsn->Offset);
  4581. dstOpnd = this->BuildDstOpnd(dstRegSlot);
  4582. instr = IR::Instr::New(Js::OpCode::SpreadArrayLiteral, dstOpnd, src1Opnd, src2Opnd, m_func);
  4583. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  4584. if (dstOpnd->m_sym->m_isSingleDef)
  4585. {
  4586. dstOpnd->m_sym->m_isNotNumber = true;
  4587. }
  4588. break;
  4589. }
  4590. default:
  4591. {
  4592. AssertMsg(UNREACHED, "Unknown Reg2Aux opcode");
  4593. Fatal();
  4594. break;
  4595. }
  4596. }
  4597. }
  4598. ///----------------------------------------------------------------------------
  4599. ///
  4600. /// IRBuilder::BuildElementI
  4601. ///
  4602. /// Build IR instr for an ElementI instruction.
  4603. ///
  4604. ///----------------------------------------------------------------------------
  4605. template <typename SizePolicy>
  4606. void
  4607. IRBuilder::BuildElementI(Js::OpCode newOpcode, uint32 offset)
  4608. {
  4609. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  4610. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  4611. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementI<SizePolicy>>();
  4612. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  4613. {
  4614. this->DoClosureRegCheck(layout->Value);
  4615. this->DoClosureRegCheck(layout->Instance);
  4616. this->DoClosureRegCheck(layout->Element);
  4617. }
  4618. BuildElementI(newOpcode, offset, layout->Instance, layout->Element, layout->Value, Js::Constants::NoProfileId);
  4619. }
  4620. template <typename SizePolicy>
  4621. void
  4622. IRBuilder::BuildProfiledElementI(Js::OpCode newOpcode, uint32 offset)
  4623. {
  4624. Assert(OpCodeAttr::IsProfiledOp(newOpcode));
  4625. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  4626. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_ElementI<SizePolicy>>>();
  4627. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  4628. {
  4629. this->DoClosureRegCheck(layout->Value);
  4630. this->DoClosureRegCheck(layout->Instance);
  4631. this->DoClosureRegCheck(layout->Element);
  4632. }
  4633. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(newOpcode);
  4634. BuildElementI(newOpcode, offset, layout->Instance, layout->Element, layout->Value, layout->profileId);
  4635. }
  4636. void
  4637. IRBuilder::BuildElementI(Js::OpCode newOpcode, uint32 offset, Js::RegSlot baseRegSlot, Js::RegSlot indexRegSlot,
  4638. Js::RegSlot regSlot, Js::ProfileId profileId)
  4639. {
  4640. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  4641. ValueType arrayType;
  4642. const Js::LdElemInfo *ldElemInfo = nullptr;
  4643. const Js::StElemInfo *stElemInfo = nullptr;
  4644. bool isProfiledLoad = false;
  4645. bool isProfiledStore = false;
  4646. bool isProfiledInstr = (profileId != Js::Constants::NoProfileId);
  4647. bool isLdElemOrStElemThatWasNotProfiled = false;
  4648. if (isProfiledInstr)
  4649. {
  4650. switch (newOpcode)
  4651. {
  4652. case Js::OpCode::LdElemI_A:
  4653. if (!DoLoadInstructionArrayProfileInfo())
  4654. {
  4655. break;
  4656. }
  4657. ldElemInfo = this->m_func->GetReadOnlyProfileInfo()->GetLdElemInfo(profileId);
  4658. arrayType = ldElemInfo->GetArrayType();
  4659. isLdElemOrStElemThatWasNotProfiled = !ldElemInfo->WasProfiled();
  4660. isProfiledLoad = true;
  4661. break;
  4662. case Js::OpCode::StElemI_A:
  4663. case Js::OpCode::StElemI_A_Strict:
  4664. if (!DoLoadInstructionArrayProfileInfo())
  4665. {
  4666. break;
  4667. }
  4668. isProfiledStore = true;
  4669. stElemInfo = this->m_func->GetReadOnlyProfileInfo()->GetStElemInfo(profileId);
  4670. arrayType = stElemInfo->GetArrayType();
  4671. isLdElemOrStElemThatWasNotProfiled = !stElemInfo->WasProfiled();
  4672. break;
  4673. }
  4674. }
  4675. IR::Instr * instr;
  4676. IR::RegOpnd * regOpnd;
  4677. IR::IndirOpnd * indirOpnd;
  4678. indirOpnd = this->BuildIndirOpnd(this->BuildSrcOpnd(baseRegSlot), this->BuildSrcOpnd(indexRegSlot));
  4679. if (isProfiledLoad || isProfiledStore)
  4680. {
  4681. if(arrayType.IsLikelyNativeArray() && !AllowNativeArrayProfileInfo())
  4682. {
  4683. arrayType = arrayType.SetArrayTypeId(Js::TypeIds_Array);
  4684. // An opnd's value type will get replaced in the forward phase when it is not fixed. Store the array type in the
  4685. // ProfiledInstr.
  4686. if(isProfiledLoad)
  4687. {
  4688. Js::LdElemInfo *const newLdElemInfo = JitAnew(m_func->m_alloc, Js::LdElemInfo, *ldElemInfo);
  4689. newLdElemInfo->arrayType = arrayType;
  4690. ldElemInfo = newLdElemInfo;
  4691. }
  4692. else
  4693. {
  4694. Js::StElemInfo *const newStElemInfo = JitAnew(m_func->m_alloc, Js::StElemInfo, *stElemInfo);
  4695. newStElemInfo->arrayType = arrayType;
  4696. stElemInfo = newStElemInfo;
  4697. }
  4698. }
  4699. indirOpnd->GetBaseOpnd()->SetValueType(arrayType);
  4700. if (m_func->GetTopFunc()->HasTry() && !m_func->GetTopFunc()->DoOptimizeTry())
  4701. {
  4702. isProfiledLoad = false;
  4703. isProfiledStore = false;
  4704. }
  4705. }
  4706. switch (newOpcode)
  4707. {
  4708. case Js::OpCode::LdMethodElem:
  4709. case Js::OpCode::LdElemI_A:
  4710. case Js::OpCode::DeleteElemI_A:
  4711. case Js::OpCode::DeleteElemIStrict_A:
  4712. case Js::OpCode::TypeofElem:
  4713. {
  4714. // Evaluate to register
  4715. regOpnd = this->BuildDstOpnd(regSlot);
  4716. if (m_func->DoSimpleJitDynamicProfile() && isProfiledInstr)
  4717. {
  4718. instr = IR::JitProfilingInstr::New(newOpcode, regOpnd, indirOpnd, m_func);
  4719. instr->AsJitProfilingInstr()->profileId = profileId;
  4720. }
  4721. else if (isProfiledLoad)
  4722. {
  4723. instr = IR::ProfiledInstr::New(newOpcode, regOpnd, indirOpnd, m_func);
  4724. instr->AsProfiledInstr()->u.ldElemInfo = ldElemInfo;
  4725. }
  4726. else
  4727. {
  4728. instr = IR::Instr::New(newOpcode, regOpnd, indirOpnd, m_func);
  4729. }
  4730. break;
  4731. }
  4732. case Js::OpCode::StElemI_A:
  4733. case Js::OpCode::StElemI_A_Strict:
  4734. {
  4735. // Store
  4736. regOpnd = this->BuildSrcOpnd(regSlot);
  4737. if (m_func->DoSimpleJitDynamicProfile() && isProfiledInstr)
  4738. {
  4739. instr = IR::JitProfilingInstr::New(newOpcode, indirOpnd, regOpnd, m_func);
  4740. instr->AsJitProfilingInstr()->profileId = profileId;
  4741. }
  4742. else if (isProfiledStore)
  4743. {
  4744. instr = IR::ProfiledInstr::New(newOpcode, indirOpnd, regOpnd, m_func);
  4745. instr->AsProfiledInstr()->u.stElemInfo = stElemInfo;
  4746. }
  4747. else
  4748. {
  4749. instr = IR::Instr::New(newOpcode, indirOpnd, regOpnd, m_func);
  4750. }
  4751. break;
  4752. }
  4753. case Js::OpCode::InitSetElemI:
  4754. case Js::OpCode::InitGetElemI:
  4755. case Js::OpCode::InitComputedProperty:
  4756. case Js::OpCode::InitClassMemberComputedName:
  4757. case Js::OpCode::InitClassMemberGetComputedName:
  4758. case Js::OpCode::InitClassMemberSetComputedName:
  4759. {
  4760. regOpnd = this->BuildSrcOpnd(regSlot);
  4761. instr = IR::Instr::New(newOpcode, indirOpnd, regOpnd, m_func);
  4762. break;
  4763. }
  4764. default:
  4765. AssertMsg(false, "Unknown ElementI opcode");
  4766. return;
  4767. }
  4768. this->AddInstr(instr, offset);
  4769. if(isLdElemOrStElemThatWasNotProfiled && DoBailOnNoProfile())
  4770. {
  4771. InsertBailOnNoProfile(instr);
  4772. }
  4773. }
  4774. ///----------------------------------------------------------------------------
  4775. ///
  4776. /// IRBuilder::BuildElementUnsigned1
  4777. ///
  4778. /// Build IR instr for an ElementUnsigned1 instruction.
  4779. ///
  4780. ///----------------------------------------------------------------------------
  4781. template <typename SizePolicy>
  4782. void
  4783. IRBuilder::BuildElementUnsigned1(Js::OpCode newOpcode, uint32 offset)
  4784. {
  4785. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  4786. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  4787. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementUnsigned1<SizePolicy>>();
  4788. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  4789. {
  4790. this->DoClosureRegCheck(layout->Value);
  4791. this->DoClosureRegCheck(layout->Instance);
  4792. }
  4793. BuildElementUnsigned1(newOpcode, offset, layout->Instance, layout->Element, layout->Value);
  4794. }
  4795. void
  4796. IRBuilder::BuildElementUnsigned1(Js::OpCode newOpcode, uint32 offset, Js::RegSlot baseRegSlot, uint32 index, Js::RegSlot regSlot)
  4797. {
  4798. // This is an array-style access with a constant (integer) index.
  4799. // Embed the index in the indir opnd as a constant offset.
  4800. IR::Instr * instr;
  4801. const bool simpleJit = m_func->DoSimpleJitDynamicProfile();
  4802. IR::RegOpnd * regOpnd;
  4803. IR::IndirOpnd * indirOpnd;
  4804. IR::RegOpnd * baseOpnd;
  4805. Js::OpCode opcode;
  4806. switch (newOpcode)
  4807. {
  4808. case Js::OpCode::StArrItemI_CI4:
  4809. {
  4810. baseOpnd = this->BuildSrcOpnd(baseRegSlot);
  4811. // This instruction must not create missing values in the array
  4812. baseOpnd->SetValueType(
  4813. ValueType::GetObject(ObjectType::Array).SetHasNoMissingValues(false).SetArrayTypeId(Js::TypeIds_Array));
  4814. baseOpnd->SetValueTypeFixed();
  4815. // In the case of simplejit, we won't know the exact type of array used until run time. Due to this,
  4816. // we must use the specialized version of StElemC in Lowering.
  4817. opcode = simpleJit ? Js::OpCode::StElemC : Js::OpCode::StElemI_A;
  4818. break;
  4819. }
  4820. case Js::OpCode::StArrItemC_CI4:
  4821. {
  4822. baseOpnd = IR::RegOpnd::New(TyVar, m_func);
  4823. // Insert LdArrHead as the next instr and clear the offset to avoid duplication.
  4824. IR::RegOpnd *const arrayOpnd = this->BuildSrcOpnd(baseRegSlot);
  4825. // This instruction must not create missing values in the array
  4826. arrayOpnd->SetValueType(
  4827. ValueType::GetObject(ObjectType::Array).SetHasNoMissingValues(false).SetArrayTypeId(Js::TypeIds_Array));
  4828. arrayOpnd->SetValueTypeFixed();
  4829. this->AddInstr(IR::Instr::New(Js::OpCode::LdArrHead, baseOpnd, arrayOpnd, m_func), offset);
  4830. offset = Js::Constants::NoByteCodeOffset;
  4831. opcode = Js::OpCode::StArrSegElemC;
  4832. break;
  4833. }
  4834. case Js::OpCode::StArrSegItem_CI4:
  4835. {
  4836. baseOpnd = this->BuildSrcOpnd(baseRegSlot, TyVar);
  4837. // This instruction must not create missing values in the array
  4838. opcode = Js::OpCode::StArrSegElemC;
  4839. break;
  4840. }
  4841. case Js::OpCode::StArrInlineItem_CI4:
  4842. {
  4843. baseOpnd = this->BuildSrcOpnd(baseRegSlot);
  4844. IR::Opnd *defOpnd = baseOpnd->m_sym->m_instrDef ? baseOpnd->m_sym->m_instrDef->GetDst() : nullptr;
  4845. if (!defOpnd)
  4846. {
  4847. // The array sym may be multi-def because of oddness in the renumbering of temps -- for instance,
  4848. // if there's a loop increment expression whose result is unused (ExprGen only, probably).
  4849. FOREACH_INSTR_BACKWARD(tmpInstr, m_func->m_exitInstr->m_prev)
  4850. {
  4851. if (tmpInstr->GetDst())
  4852. {
  4853. if (tmpInstr->GetDst()->IsEqual(baseOpnd))
  4854. {
  4855. defOpnd = tmpInstr->GetDst();
  4856. break;
  4857. }
  4858. else if (tmpInstr->m_opcode == Js::OpCode::StElemC &&
  4859. tmpInstr->GetDst()->AsIndirOpnd()->GetBaseOpnd()->IsEqual(baseOpnd))
  4860. {
  4861. defOpnd = tmpInstr->GetDst()->AsIndirOpnd()->GetBaseOpnd();
  4862. break;
  4863. }
  4864. }
  4865. }
  4866. NEXT_INSTR_BACKWARD;
  4867. }
  4868. AnalysisAssert(defOpnd);
  4869. // This instruction must not create missing values in the array
  4870. baseOpnd->SetValueType(defOpnd->GetValueType());
  4871. opcode = Js::OpCode::StElemC;
  4872. break;
  4873. }
  4874. default:
  4875. AssertMsg(false, "Unknown ElementUnsigned1 opcode");
  4876. return;
  4877. }
  4878. indirOpnd = this->BuildIndirOpnd(baseOpnd, index);
  4879. regOpnd = this->BuildSrcOpnd(regSlot);
  4880. if (simpleJit)
  4881. {
  4882. instr = IR::JitProfilingInstr::New(opcode, indirOpnd, regOpnd, m_func);
  4883. }
  4884. else if(opcode == Js::OpCode::StElemC && !baseOpnd->GetValueType().IsUninitialized())
  4885. {
  4886. // An opnd's value type will get replaced in the forward phase when it is not fixed. Store the array type in the
  4887. // ProfiledInstr.
  4888. IR::ProfiledInstr *const profiledInstr = IR::ProfiledInstr::New(opcode, indirOpnd, regOpnd, m_func);
  4889. Js::StElemInfo *const stElemInfo = JitAnew(m_func->m_alloc, Js::StElemInfo);
  4890. stElemInfo->arrayType = baseOpnd->GetValueType();
  4891. profiledInstr->u.stElemInfo = stElemInfo;
  4892. instr = profiledInstr;
  4893. }
  4894. else
  4895. {
  4896. instr = IR::Instr::New(opcode, indirOpnd, regOpnd, m_func);
  4897. }
  4898. this->AddInstr(instr, offset);
  4899. }
  4900. ///----------------------------------------------------------------------------
  4901. ///
  4902. /// IRBuilder::BuildArgIn
  4903. ///
  4904. /// Build IR instr for an ArgIn instruction.
  4905. ///
  4906. ///----------------------------------------------------------------------------
  4907. void
  4908. IRBuilder::BuildArgIn0(uint32 offset, Js::RegSlot dstRegSlot)
  4909. {
  4910. Assert(OpCodeAttr::HasMultiSizeLayout(Js::OpCode::ArgIn0));
  4911. BuildArgIn(offset, dstRegSlot, 0);
  4912. }
  4913. void
  4914. IRBuilder::BuildArgIn(uint32 offset, Js::RegSlot dstRegSlot, uint16 argument)
  4915. {
  4916. IR::Instr * instr;
  4917. IR::SymOpnd * srcOpnd;
  4918. IR::RegOpnd * dstOpnd;
  4919. StackSym * symSrc = StackSym::NewParamSlotSym(argument + 1, m_func);
  4920. this->m_func->SetArgOffset(symSrc, (argument + LowererMD::GetFormalParamOffset()) * MachPtr);
  4921. srcOpnd = IR::SymOpnd::New(symSrc, TyVar, m_func);
  4922. dstOpnd = this->BuildDstOpnd(dstRegSlot);
  4923. if (!this->m_func->IsLoopBody() && this->m_func->HasProfileInfo())
  4924. {
  4925. // Skip "this" pointer; "this" profile data is captured by ProfiledLdThis.
  4926. // Subtract 1 to skip "this" pointer, subtract 1 again to get the index to index into profileData->parameterInfo.
  4927. int paramSlotIndex = symSrc->GetParamSlotNum() - 2;
  4928. if (paramSlotIndex >= 0)
  4929. {
  4930. ValueType profiledValueType;
  4931. profiledValueType = this->m_func->GetReadOnlyProfileInfo()->GetParameterInfo(static_cast<Js::ArgSlot>(paramSlotIndex));
  4932. dstOpnd->SetValueType(profiledValueType);
  4933. }
  4934. }
  4935. instr = IR::Instr::New(Js::OpCode::ArgIn_A, dstOpnd, srcOpnd, m_func);
  4936. this->AddInstr(instr, offset);
  4937. }
  4938. void
  4939. IRBuilder::BuildArgInRest()
  4940. {
  4941. IR::RegOpnd * dstOpnd = this->BuildDstOpnd(m_func->GetJITFunctionBody()->GetRestParamRegSlot());
  4942. IR::Instr *instr = IR::Instr::New(Js::OpCode::ArgIn_Rest, dstOpnd, m_func);
  4943. this->AddInstr(instr, (uint32)-1);
  4944. }
  4945. ///----------------------------------------------------------------------------
  4946. ///
  4947. /// IRBuilder::BuildArg
  4948. ///
  4949. /// Build IR instr for an ArgOut instruction.
  4950. ///
  4951. ///----------------------------------------------------------------------------
  4952. template <typename SizePolicy>
  4953. void
  4954. IRBuilder::BuildArgNoSrc(Js::OpCode newOpcode, uint32 offset)
  4955. {
  4956. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  4957. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  4958. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ArgNoSrc<SizePolicy>>();
  4959. BuildArg(Js::OpCode::ArgOut_A, offset, layout->Arg, this->GetEnvRegForInnerFrameDisplay());
  4960. }
  4961. template <typename SizePolicy>
  4962. void
  4963. IRBuilder::BuildArg(Js::OpCode newOpcode, uint32 offset)
  4964. {
  4965. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  4966. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  4967. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_Arg<SizePolicy>>();
  4968. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  4969. {
  4970. this->DoClosureRegCheck(layout->Reg);
  4971. }
  4972. BuildArg(newOpcode, offset, layout->Arg, layout->Reg);
  4973. }
  4974. template <typename SizePolicy>
  4975. void
  4976. IRBuilder::BuildProfiledArg(Js::OpCode newOpcode, uint32 offset)
  4977. {
  4978. Assert(OpCodeAttr::IsProfiledOp(newOpcode));
  4979. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  4980. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_Arg<SizePolicy>>>();
  4981. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  4982. {
  4983. this->DoClosureRegCheck(layout->Reg);
  4984. }
  4985. newOpcode = Js::OpCode::ArgOut_A;
  4986. BuildArg(newOpcode, offset, layout->Arg, layout->Reg);
  4987. }
  4988. void
  4989. IRBuilder::BuildArg(Js::OpCode newOpcode, uint32 offset, Js::ArgSlot argument, Js::RegSlot srcRegSlot)
  4990. {
  4991. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  4992. IR::Instr * instr;
  4993. IRType type = TyVar;
  4994. if (newOpcode == Js::OpCode::ArgOut_ANonVar)
  4995. {
  4996. newOpcode = Js::OpCode::ArgOut_A;
  4997. type = TyMachPtr;
  4998. }
  4999. m_argsOnStack++;
  5000. StackSym * symDst;
  5001. Assert(argument < USHRT_MAX);
  5002. symDst = m_func->m_symTable->GetArgSlotSym((uint16)(argument+1));
  5003. if (symDst == nullptr || (uint16)(argument + 1) != (argument + 1))
  5004. {
  5005. AssertMsg(UNREACHED, "Arg count too big...");
  5006. Fatal();
  5007. }
  5008. IR::SymOpnd * dstOpnd = IR::SymOpnd::New(symDst, type, m_func);
  5009. IR::RegOpnd * src1Opnd = this->BuildSrcOpnd(srcRegSlot, type);
  5010. instr = IR::Instr::New(newOpcode, dstOpnd, src1Opnd, m_func);
  5011. this->AddInstr(instr, offset);
  5012. m_argStack->Push(instr);
  5013. }
  5014. ///----------------------------------------------------------------------------
  5015. ///
  5016. /// IRBuilder::BuildStartCall
  5017. ///
  5018. /// Build IR instr for a StartCall instruction.
  5019. ///
  5020. ///----------------------------------------------------------------------------
  5021. void
  5022. IRBuilder::BuildStartCall(Js::OpCode newOpcode, uint32 offset)
  5023. {
  5024. Assert(newOpcode == Js::OpCode::StartCall);
  5025. const unaligned Js::OpLayoutStartCall * regLayout = m_jnReader.StartCall();
  5026. Js::ArgSlot ArgCount = regLayout->ArgCount;
  5027. IR::Instr * instr;
  5028. IR::RegOpnd * dstOpnd;
  5029. // Dst of StartCall is always r0... Let's give it a new dst such that it can
  5030. // be singleDef.
  5031. dstOpnd = IR::RegOpnd::New(TyVar, m_func);
  5032. #if DBG
  5033. m_callsOnStack++;
  5034. #endif
  5035. IntConstType value = ArgCount;
  5036. IR::IntConstOpnd * srcOpnd;
  5037. srcOpnd = IR::IntConstOpnd::New(value, TyInt32, m_func);
  5038. instr = IR::Instr::New(newOpcode, dstOpnd, srcOpnd, m_func);
  5039. this->AddInstr(instr, offset);
  5040. // Keep a stack of arg instructions such that we can link them up once we see
  5041. // the call that consumes them.
  5042. m_argStack->Push(instr);
  5043. }
  5044. ///----------------------------------------------------------------------------
  5045. ///
  5046. /// IRBuilder::BuildCallI
  5047. ///
  5048. /// Build IR instr for a CallI instruction.
  5049. ///
  5050. ///----------------------------------------------------------------------------
  5051. template <typename SizePolicy>
  5052. void
  5053. IRBuilder::BuildCallI(Js::OpCode newOpcode, uint32 offset)
  5054. {
  5055. Assert(Js::OpCodeUtil::IsCallOp(newOpcode) || newOpcode == Js::OpCode::NewScObject || newOpcode == Js::OpCode::NewScObjArray);
  5056. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5057. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_CallI<SizePolicy>>();
  5058. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5059. {
  5060. this->DoClosureRegCheck(layout->Return);
  5061. this->DoClosureRegCheck(layout->Function);
  5062. }
  5063. BuildCallI_Helper(newOpcode, offset, layout->Return, layout->Function, layout->ArgCount, Js::Constants::NoProfileId);
  5064. }
  5065. template <typename SizePolicy>
  5066. void
  5067. IRBuilder::BuildCallIFlags(Js::OpCode newOpcode, uint32 offset)
  5068. {
  5069. Assert(Js::OpCodeUtil::IsCallOp(newOpcode) || newOpcode == Js::OpCode::NewScObject || newOpcode == Js::OpCode::NewScObjArray);
  5070. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5071. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_CallIFlags<SizePolicy>>();
  5072. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5073. {
  5074. this->DoClosureRegCheck(layout->Return);
  5075. this->DoClosureRegCheck(layout->Function);
  5076. }
  5077. IR::Instr* instr = BuildCallI_Helper(newOpcode, offset, layout->Return, layout->Function, layout->ArgCount, Js::Constants::NoProfileId, layout->callFlags);
  5078. Assert(instr->m_opcode == Js::OpCode::CallIFlags);
  5079. if (instr->m_opcode == Js::OpCode::CallIFlags)
  5080. {
  5081. instr->m_opcode =
  5082. layout->callFlags == (Js::CallFlags::CallFlags_NewTarget | Js::CallFlags::CallFlags_New | Js::CallFlags::CallFlags_ExtraArg) ? Js::OpCode::CallINewTargetNew :
  5083. layout->callFlags == Js::CallFlags::CallFlags_New ? Js::OpCode::CallINew :
  5084. instr->m_opcode;
  5085. }
  5086. }
  5087. void IRBuilder::BuildLdSpreadIndices(uint32 offset, uint32 spreadAuxOffset)
  5088. {
  5089. // Link up the LdSpreadIndices instr to be the first in the arg chain. This will allow us to find it in Lowerer easier.
  5090. IR::Opnd *auxArg = this->BuildAuxArrayOpnd(AuxArrayValue::AuxIntArray, spreadAuxOffset);
  5091. IR::Instr *instr = IR::Instr::New(Js::OpCode::LdSpreadIndices, m_func);
  5092. instr->SetSrc1(auxArg);
  5093. // Create the link to the first arg.
  5094. Js::RegSlot lastArg = m_argStack->Head()->GetDst()->AsSymOpnd()->GetStackSym()->GetArgSlotNum();
  5095. instr->SetDst(IR::SymOpnd::New(m_func->m_symTable->GetArgSlotSym((uint16) (lastArg + 1)), TyVar, m_func));
  5096. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  5097. m_argStack->Push(instr);
  5098. }
  5099. template <typename SizePolicy>
  5100. void
  5101. IRBuilder::BuildCallIExtended(Js::OpCode newOpcode, uint32 offset)
  5102. {
  5103. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5104. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_CallIExtended<SizePolicy>>();
  5105. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5106. {
  5107. this->DoClosureRegCheck(layout->Return);
  5108. this->DoClosureRegCheck(layout->Function);
  5109. }
  5110. BuildCallIExtended(newOpcode, offset, layout->Return, layout->Function, layout->ArgCount, layout->Options, layout->SpreadAuxOffset);
  5111. }
  5112. IR::Instr*
  5113. IRBuilder::BuildCallIExtended(Js::OpCode newOpcode, uint32 offset, Js::RegSlot returnValue, Js::RegSlot function,
  5114. Js::ArgSlot argCount, Js::CallIExtendedOptions options, uint32 spreadAuxOffset, Js::CallFlags flags)
  5115. {
  5116. if (options & Js::CallIExtended_SpreadArgs)
  5117. {
  5118. BuildLdSpreadIndices(offset, spreadAuxOffset);
  5119. }
  5120. return BuildCallI_Helper(newOpcode, offset, returnValue, function, argCount, Js::Constants::NoProfileId, flags);
  5121. }
  5122. template <typename SizePolicy>
  5123. void
  5124. IRBuilder::BuildCallIWithICIndex(Js::OpCode newOpcode, uint32 offset)
  5125. {
  5126. AssertMsg(false, "NYI");
  5127. }
  5128. template <typename SizePolicy>
  5129. void
  5130. IRBuilder::BuildCallIFlagsWithICIndex(Js::OpCode newOpcode, uint32 offset)
  5131. {
  5132. AssertMsg(false, "NYI");
  5133. }
  5134. template <typename SizePolicy>
  5135. void
  5136. IRBuilder::BuildCallIExtendedFlags(Js::OpCode newOpcode, uint32 offset)
  5137. {
  5138. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5139. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_CallIExtendedFlags<SizePolicy>>();
  5140. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5141. {
  5142. this->DoClosureRegCheck(layout->Return);
  5143. this->DoClosureRegCheck(layout->Function);
  5144. }
  5145. IR::Instr* instr = BuildCallIExtended(newOpcode, offset, layout->Return, layout->Function, layout->ArgCount, layout->Options, layout->SpreadAuxOffset, layout->callFlags);
  5146. Assert(instr->m_opcode == Js::OpCode::CallIExtendedFlags);
  5147. if (instr->m_opcode == Js::OpCode::CallIExtendedFlags)
  5148. {
  5149. instr->m_opcode =
  5150. layout->callFlags == Js::CallFlags::CallFlags_ExtraArg ? Js::OpCode::CallIEval :
  5151. layout->callFlags == Js::CallFlags::CallFlags_New ? Js::OpCode::CallIExtendedNew :
  5152. layout->callFlags == (Js::CallFlags::CallFlags_New | Js::CallFlags::CallFlags_ExtraArg | Js::CallFlags::CallFlags_NewTarget) ? Js::OpCode::CallIExtendedNewTargetNew :
  5153. instr->m_opcode;
  5154. }
  5155. }
  5156. template <typename SizePolicy>
  5157. void
  5158. IRBuilder::BuildCallIExtendedWithICIndex(Js::OpCode newOpcode, uint32 offset)
  5159. {
  5160. AssertMsg(false, "NYI");
  5161. }
  5162. template <typename SizePolicy>
  5163. void
  5164. IRBuilder::BuildCallIExtendedFlagsWithICIndex(Js::OpCode newOpcode, uint32 offset)
  5165. {
  5166. AssertMsg(false, "NYI");
  5167. }
  5168. template <typename SizePolicy>
  5169. void
  5170. IRBuilder::BuildProfiledCallIFlagsWithICIndex(Js::OpCode newOpcode, uint32 offset)
  5171. {
  5172. Assert(OpCodeAttr::IsProfiledOpWithICIndex(newOpcode) || Js::OpCodeUtil::IsProfiledCallOpWithICIndex(newOpcode));
  5173. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5174. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_CallIFlagsWithICIndex<SizePolicy>>>();
  5175. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5176. {
  5177. this->DoClosureRegCheck(layout->Return);
  5178. this->DoClosureRegCheck(layout->Function);
  5179. }
  5180. IR::Instr* instr = BuildProfiledCallIWithICIndex(newOpcode, offset, layout->Return, layout->Function, layout->ArgCount, layout->profileId, layout->inlineCacheIndex);
  5181. Assert(instr->m_opcode == Js::OpCode::CallIFlags);
  5182. if (instr->m_opcode == Js::OpCode::CallIFlags)
  5183. {
  5184. instr->m_opcode =
  5185. layout->callFlags == (Js::CallFlags::CallFlags_NewTarget | Js::CallFlags::CallFlags_New | Js::CallFlags::CallFlags_ExtraArg) ? Js::OpCode::CallINewTargetNew :
  5186. layout->callFlags == Js::CallFlags::CallFlags_New ? Js::OpCode::CallINew :
  5187. instr->m_opcode;
  5188. }
  5189. }
  5190. template <typename SizePolicy>
  5191. void
  5192. IRBuilder::BuildProfiledCallIWithICIndex(Js::OpCode newOpcode, uint32 offset)
  5193. {
  5194. Assert(OpCodeAttr::IsProfiledOpWithICIndex(newOpcode) || Js::OpCodeUtil::IsProfiledCallOpWithICIndex(newOpcode));
  5195. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5196. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_CallIWithICIndex<SizePolicy>>>();
  5197. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5198. {
  5199. this->DoClosureRegCheck(layout->Return);
  5200. this->DoClosureRegCheck(layout->Function);
  5201. }
  5202. BuildProfiledCallIWithICIndex(newOpcode, offset, layout->Return, layout->Function, layout->ArgCount, layout->profileId, layout->inlineCacheIndex);
  5203. }
  5204. IR::Instr*
  5205. IRBuilder::BuildProfiledCallIWithICIndex(Js::OpCode opcode, uint32 offset, Js::RegSlot returnValue, Js::RegSlot function,
  5206. Js::ArgSlot argCount, Js::ProfileId profileId, Js::InlineCacheIndex inlineCacheIndex)
  5207. {
  5208. return BuildProfiledCallI(opcode, offset, returnValue, function, argCount, profileId, Js::CallFlags_None, inlineCacheIndex);
  5209. }
  5210. template <typename SizePolicy>
  5211. void
  5212. IRBuilder::BuildProfiledCallIExtendedFlags(Js::OpCode newOpcode, uint32 offset)
  5213. {
  5214. Assert(OpCodeAttr::IsProfiledOp(newOpcode) || Js::OpCodeUtil::IsProfiledCallOp(newOpcode)
  5215. || Js::OpCodeUtil::IsProfiledReturnTypeCallOp(newOpcode));
  5216. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5217. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_CallIExtendedFlags<SizePolicy>>>();
  5218. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5219. {
  5220. this->DoClosureRegCheck(layout->Return);
  5221. this->DoClosureRegCheck(layout->Function);
  5222. }
  5223. IR::Instr* instr = BuildProfiledCallIExtended(newOpcode, offset, layout->Return, layout->Function, layout->ArgCount, layout->profileId, layout->Options, layout->SpreadAuxOffset, layout->callFlags);
  5224. Assert(instr->m_opcode == Js::OpCode::CallIExtendedFlags);
  5225. if (instr->m_opcode == Js::OpCode::CallIExtendedFlags)
  5226. {
  5227. instr->m_opcode =
  5228. layout->callFlags == Js::CallFlags::CallFlags_ExtraArg ? Js::OpCode::CallIEval :
  5229. layout->callFlags == Js::CallFlags::CallFlags_New ? Js::OpCode::CallIExtendedNew :
  5230. layout->callFlags == (Js::CallFlags::CallFlags_New | Js::CallFlags::CallFlags_ExtraArg | Js::CallFlags::CallFlags_NewTarget) ? Js::OpCode::CallIExtendedNewTargetNew :
  5231. instr->m_opcode;
  5232. }
  5233. }
  5234. template <typename SizePolicy>
  5235. void
  5236. IRBuilder::BuildProfiledCallIExtendedWithICIndex(Js::OpCode newOpcode, uint32 offset)
  5237. {
  5238. Assert(OpCodeAttr::IsProfiledOpWithICIndex(newOpcode) || Js::OpCodeUtil::IsProfiledCallOpWithICIndex(newOpcode));
  5239. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5240. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_CallIExtendedWithICIndex<SizePolicy>>>();
  5241. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5242. {
  5243. this->DoClosureRegCheck(layout->Return);
  5244. this->DoClosureRegCheck(layout->Function);
  5245. }
  5246. BuildProfiledCallIExtendedWithICIndex(newOpcode, offset, layout->Return, layout->Function, layout->ArgCount, layout->profileId, layout->Options, layout->SpreadAuxOffset);
  5247. }
  5248. void
  5249. IRBuilder::BuildProfiledCallIExtendedWithICIndex(Js::OpCode opcode, uint32 offset, Js::RegSlot returnValue, Js::RegSlot function,
  5250. Js::ArgSlot argCount, Js::ProfileId profileId, Js::CallIExtendedOptions options, uint32 spreadAuxOffset)
  5251. {
  5252. BuildProfiledCallIExtended(opcode, offset, returnValue, function, argCount, profileId, options, spreadAuxOffset);
  5253. }
  5254. template <typename SizePolicy>
  5255. void
  5256. IRBuilder::BuildProfiledCallIExtendedFlagsWithICIndex(Js::OpCode newOpcode, uint32 offset)
  5257. {
  5258. Assert(OpCodeAttr::IsProfiledOpWithICIndex(newOpcode) || Js::OpCodeUtil::IsProfiledCallOpWithICIndex(newOpcode));
  5259. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5260. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_CallIExtendedFlagsWithICIndex<SizePolicy>>>();
  5261. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5262. {
  5263. this->DoClosureRegCheck(layout->Return);
  5264. this->DoClosureRegCheck(layout->Function);
  5265. }
  5266. IR::Instr* instr = BuildProfiledCallIExtended(newOpcode, offset, layout->Return, layout->Function, layout->ArgCount, layout->profileId, layout->Options, layout->SpreadAuxOffset);
  5267. Assert(instr->m_opcode == Js::OpCode::CallIExtendedFlags);
  5268. if (instr->m_opcode == Js::OpCode::CallIExtendedFlags)
  5269. {
  5270. instr->m_opcode =
  5271. layout->callFlags == Js::CallFlags::CallFlags_ExtraArg ? Js::OpCode::CallIEval :
  5272. layout->callFlags == Js::CallFlags::CallFlags_New ? Js::OpCode::CallIExtendedNew :
  5273. layout->callFlags == (Js::CallFlags::CallFlags_New | Js::CallFlags::CallFlags_ExtraArg | Js::CallFlags::CallFlags_NewTarget) ? Js::OpCode::CallIExtendedNewTargetNew :
  5274. instr->m_opcode;
  5275. }
  5276. }
  5277. template <typename SizePolicy>
  5278. void
  5279. IRBuilder::BuildProfiledCallI(Js::OpCode newOpcode, uint32 offset)
  5280. {
  5281. Assert(OpCodeAttr::IsProfiledOp(newOpcode) || Js::OpCodeUtil::IsProfiledCallOp(newOpcode)
  5282. || Js::OpCodeUtil::IsProfiledReturnTypeCallOp(newOpcode));
  5283. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5284. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_CallI<SizePolicy>>>();
  5285. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5286. {
  5287. this->DoClosureRegCheck(layout->Return);
  5288. this->DoClosureRegCheck(layout->Function);
  5289. }
  5290. BuildProfiledCallI(newOpcode, offset, layout->Return, layout->Function, layout->ArgCount, layout->profileId);
  5291. }
  5292. template <typename SizePolicy>
  5293. void
  5294. IRBuilder::BuildProfiledCallIFlags(Js::OpCode newOpcode, uint32 offset)
  5295. {
  5296. Assert(OpCodeAttr::IsProfiledOp(newOpcode) || Js::OpCodeUtil::IsProfiledCallOp(newOpcode)
  5297. || Js::OpCodeUtil::IsProfiledReturnTypeCallOp(newOpcode));
  5298. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5299. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_CallIFlags<SizePolicy>>>();
  5300. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5301. {
  5302. this->DoClosureRegCheck(layout->Return);
  5303. this->DoClosureRegCheck(layout->Function);
  5304. }
  5305. IR::Instr* instr = BuildProfiledCallI(newOpcode, offset, layout->Return, layout->Function, layout->ArgCount, layout->profileId, layout->callFlags);
  5306. Assert(instr->m_opcode == Js::OpCode::CallIFlags);
  5307. if (instr->m_opcode == Js::OpCode::CallIFlags)
  5308. {
  5309. instr->m_opcode =
  5310. layout->callFlags == (Js::CallFlags::CallFlags_NewTarget | Js::CallFlags::CallFlags_New | Js::CallFlags::CallFlags_ExtraArg) ? Js::OpCode::CallINewTargetNew :
  5311. layout->callFlags == Js::CallFlags::CallFlags_New ? Js::OpCode::CallINew :
  5312. instr->m_opcode;
  5313. }
  5314. }
  5315. IR::Instr *
  5316. IRBuilder::BuildProfiledCallI(Js::OpCode opcode, uint32 offset, Js::RegSlot returnValue, Js::RegSlot function,
  5317. Js::ArgSlot argCount, Js::ProfileId profileId, Js::CallFlags flags, Js::InlineCacheIndex inlineCacheIndex)
  5318. {
  5319. Js::OpCode newOpcode;
  5320. ValueType returnType;
  5321. bool isProtectedByNoProfileBailout = false;
  5322. if (opcode == Js::OpCode::ProfiledNewScObject || opcode == Js::OpCode::ProfiledNewScObjectWithICIndex
  5323. || opcode == Js::OpCode::ProfiledNewScObjectSpread)
  5324. {
  5325. newOpcode = opcode;
  5326. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(newOpcode);
  5327. Assert(newOpcode == Js::OpCode::NewScObject || newOpcode == Js::OpCode::NewScObjectSpread);
  5328. if (!this->m_func->HasProfileInfo())
  5329. {
  5330. returnType = ValueType::GetObject(ObjectType::UninitializedObject);
  5331. }
  5332. else
  5333. {
  5334. // If we have profile data, make use of it
  5335. returnType = this->m_func->GetReadOnlyProfileInfo()->GetReturnType(opcode, profileId);
  5336. }
  5337. }
  5338. else
  5339. {
  5340. if (this->m_func->HasProfileInfo())
  5341. {
  5342. returnType = this->m_func->GetReadOnlyProfileInfo()->GetReturnType(opcode, profileId);
  5343. }
  5344. if (opcode < Js::OpCode::ProfiledReturnTypeCallI)
  5345. {
  5346. newOpcode = Js::OpCodeUtil::ConvertProfiledCallOpToNonProfiled(opcode);
  5347. if(DoBailOnNoProfile())
  5348. {
  5349. if(this->m_func->GetWorkItem()->GetJITTimeInfo())
  5350. {
  5351. const FunctionJITTimeInfo *inlinerData = this->m_func->GetWorkItem()->GetJITTimeInfo();
  5352. if (!(this->IsLoopBody() && PHASE_OFF(Js::InlineInJitLoopBodyPhase, this->m_func))
  5353. && inlinerData && inlinerData->GetInlineesBV())
  5354. {
  5355. AssertOrFailFast(profileId < inlinerData->GetInlineesBV()->Length());
  5356. if (!inlinerData->GetInlineesBV()->Test(profileId)
  5357. #if DBG
  5358. || (PHASE_STRESS(Js::BailOnNoProfilePhase, this->m_func->GetTopFunc())
  5359. && (CONFIG_FLAG(SkipFuncCountForBailOnNoProfile) < 0
  5360. || this->m_func->m_callSiteCount >= (uint)CONFIG_FLAG(SkipFuncCountForBailOnNoProfile)))
  5361. #endif
  5362. )
  5363. {
  5364. this->InsertBailOnNoProfile(offset);
  5365. isProtectedByNoProfileBailout = true;
  5366. }
  5367. }
  5368. if (!isProtectedByNoProfileBailout)
  5369. {
  5370. this->callTreeHasSomeProfileInfo = true;
  5371. }
  5372. }
  5373. #if DBG
  5374. this->m_func->m_callSiteCount++;
  5375. #endif
  5376. }
  5377. }
  5378. else
  5379. {
  5380. // Changing this opcode into a non ReturnTypeCall* opcode is done in BuildCallI_Helper
  5381. newOpcode = opcode;
  5382. }
  5383. }
  5384. IR::Instr * callInstr = BuildCallI_Helper(newOpcode, offset, returnValue, function, argCount, profileId, flags, inlineCacheIndex);
  5385. callInstr->isCallInstrProtectedByNoProfileBailout = isProtectedByNoProfileBailout;
  5386. if (callInstr->GetDst() && (callInstr->GetDst()->GetValueType().IsUninitialized() || callInstr->GetDst()->GetValueType() == ValueType::UninitializedObject))
  5387. {
  5388. callInstr->GetDst()->SetValueType(returnType);
  5389. }
  5390. return callInstr;
  5391. }
  5392. template <typename SizePolicy>
  5393. void
  5394. IRBuilder::BuildProfiledCallIExtended(Js::OpCode newOpcode, uint32 offset)
  5395. {
  5396. Assert(OpCodeAttr::IsProfiledOp(newOpcode) || Js::OpCodeUtil::IsProfiledCallOp(newOpcode)
  5397. || Js::OpCodeUtil::IsProfiledReturnTypeCallOp(newOpcode));
  5398. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5399. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_CallIExtended<SizePolicy>>>();
  5400. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5401. {
  5402. this->DoClosureRegCheck(layout->Return);
  5403. this->DoClosureRegCheck(layout->Function);
  5404. }
  5405. BuildProfiledCallIExtended(newOpcode, offset, layout->Return, layout->Function, layout->ArgCount, layout->profileId, layout->Options, layout->SpreadAuxOffset);
  5406. }
  5407. IR::Instr *
  5408. IRBuilder::BuildProfiledCallIExtended(Js::OpCode opcode, uint32 offset, Js::RegSlot returnValue, Js::RegSlot function,
  5409. Js::ArgSlot argCount, Js::ProfileId profileId, Js::CallIExtendedOptions options,
  5410. uint32 spreadAuxOffset, Js::CallFlags flags)
  5411. {
  5412. if (options & Js::CallIExtended_SpreadArgs)
  5413. {
  5414. BuildLdSpreadIndices(offset, spreadAuxOffset);
  5415. }
  5416. return BuildProfiledCallI(opcode, offset, returnValue, function, argCount, profileId, flags);
  5417. }
  5418. template <typename SizePolicy>
  5419. void
  5420. IRBuilder::BuildProfiled2CallI(Js::OpCode newOpcode, uint32 offset)
  5421. {
  5422. Assert(OpCodeAttr::IsProfiledOp(newOpcode));
  5423. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5424. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile2<Js::OpLayoutT_CallI<SizePolicy>>>();
  5425. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5426. {
  5427. this->DoClosureRegCheck(layout->Return);
  5428. this->DoClosureRegCheck(layout->Function);
  5429. }
  5430. BuildProfiled2CallI(newOpcode, offset, layout->Return, layout->Function, layout->ArgCount, layout->profileId, layout->profileId2);
  5431. }
  5432. void
  5433. IRBuilder::BuildProfiled2CallI(Js::OpCode opcode, uint32 offset, Js::RegSlot returnValue, Js::RegSlot function,
  5434. Js::ArgSlot argCount, Js::ProfileId profileId, Js::ProfileId profileId2)
  5435. {
  5436. Assert(opcode == Js::OpCode::ProfiledNewScObjArray || opcode == Js::OpCode::ProfiledNewScObjArraySpread);
  5437. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(opcode);
  5438. Js::OpCode useOpcode = opcode;
  5439. // We either want to provide the array profile id (profileId2) to the native array creation or the call profileid (profileId)
  5440. // to the call to NewScObject
  5441. Js::ProfileId useProfileId = profileId2;
  5442. Js::TypeId arrayTypeId = Js::TypeIds_Array;
  5443. if (returnValue != Js::Constants::NoRegister)
  5444. {
  5445. Js::ArrayCallSiteInfo *arrayCallSiteInfo = nullptr;
  5446. if (m_func->HasArrayInfo())
  5447. {
  5448. arrayCallSiteInfo = m_func->GetReadOnlyProfileInfo()->GetArrayCallSiteInfo(profileId2);
  5449. }
  5450. if (arrayCallSiteInfo && !m_func->IsJitInDebugMode())
  5451. {
  5452. if (arrayCallSiteInfo->IsNativeIntArray())
  5453. {
  5454. arrayTypeId = Js::TypeIds_NativeIntArray;
  5455. }
  5456. else if (arrayCallSiteInfo->IsNativeFloatArray())
  5457. {
  5458. arrayTypeId = Js::TypeIds_NativeFloatArray;
  5459. }
  5460. }
  5461. else
  5462. {
  5463. useOpcode = (opcode == Js::OpCode::NewScObjArraySpread) ? Js::OpCode::NewScObjectSpread : Js::OpCode::NewScObject;
  5464. useProfileId = profileId;
  5465. }
  5466. }
  5467. else
  5468. {
  5469. useOpcode = (opcode == Js::OpCode::NewScObjArraySpread) ? Js::OpCode::NewScObjectSpread : Js::OpCode::NewScObject;
  5470. useProfileId = profileId;
  5471. }
  5472. IR::Instr * callInstr = BuildCallI_Helper(useOpcode, offset, returnValue, function, argCount, useProfileId);
  5473. if (callInstr->GetDst())
  5474. {
  5475. callInstr->GetDst()->SetValueType(
  5476. ValueType::GetObject(ObjectType::Array).ToLikely().SetHasNoMissingValues(true).SetArrayTypeId(arrayTypeId));
  5477. }
  5478. if (callInstr->IsJitProfilingInstr())
  5479. {
  5480. // If we happened to decide in BuildCallI_Helper that this should be a jit profiling instr, then save the fact that it is
  5481. // a "new Array(args, ...)" call and also save the array profile id (profileId2)
  5482. callInstr->AsJitProfilingInstr()->isNewArray = true;
  5483. callInstr->AsJitProfilingInstr()->arrayProfileId = profileId2;
  5484. // Double check that this profileId made it to the JitProfilingInstr like we expect it to.
  5485. Assert(callInstr->AsJitProfilingInstr()->profileId == profileId);
  5486. }
  5487. }
  5488. template <typename SizePolicy>
  5489. void
  5490. IRBuilder::BuildProfiled2CallIExtended(Js::OpCode newOpcode, uint32 offset)
  5491. {
  5492. Assert(OpCodeAttr::IsProfiledOp(newOpcode));
  5493. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5494. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile2<Js::OpLayoutT_CallIExtended<SizePolicy>>>();
  5495. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5496. {
  5497. this->DoClosureRegCheck(layout->Return);
  5498. this->DoClosureRegCheck(layout->Function);
  5499. }
  5500. BuildProfiled2CallIExtended(newOpcode, offset, layout->Return, layout->Function, layout->ArgCount, layout->profileId, layout->profileId2, layout->Options, layout->SpreadAuxOffset);
  5501. }
  5502. void
  5503. IRBuilder::BuildProfiled2CallIExtended(Js::OpCode opcode, uint32 offset, Js::RegSlot returnValue, Js::RegSlot function,
  5504. Js::ArgSlot argCount, Js::ProfileId profileId, Js::ProfileId profileId2,
  5505. Js::CallIExtendedOptions options, uint32 spreadAuxOffset)
  5506. {
  5507. if (options & Js::CallIExtended_SpreadArgs)
  5508. {
  5509. BuildLdSpreadIndices(offset, spreadAuxOffset);
  5510. }
  5511. BuildProfiled2CallI(opcode, offset, returnValue, function, argCount, profileId, profileId2);
  5512. }
  5513. IR::Instr *
  5514. IRBuilder::BuildCallI_Helper(Js::OpCode newOpcode, uint32 offset, Js::RegSlot dstRegSlot, Js::RegSlot Src1RegSlot, Js::ArgSlot ArgCount, Js::ProfileId profileId, Js::CallFlags flags, Js::InlineCacheIndex inlineCacheIndex)
  5515. {
  5516. IR::Instr * instr;
  5517. IR::RegOpnd * dstOpnd;
  5518. IR::RegOpnd * src1Opnd;
  5519. StackSym * symDst;
  5520. src1Opnd = this->BuildSrcOpnd(Src1RegSlot);
  5521. if (dstRegSlot == Js::Constants::NoRegister)
  5522. {
  5523. dstOpnd = nullptr;
  5524. symDst = nullptr;
  5525. }
  5526. else
  5527. {
  5528. dstOpnd = this->BuildDstOpnd(dstRegSlot);
  5529. symDst = dstOpnd->m_sym;
  5530. }
  5531. const bool jitProfiling = m_func->DoSimpleJitDynamicProfile();
  5532. bool profiledReturn = false;
  5533. if (Js::OpCodeUtil::IsProfiledReturnTypeCallOp(newOpcode))
  5534. {
  5535. profiledReturn = true;
  5536. newOpcode = Js::OpCodeUtil::ConvertProfiledReturnTypeCallOpToNonProfiled(newOpcode);
  5537. // If we're profiling in the jitted code we want to propagate the profileId
  5538. // If we're using profile data instead of collecting it, we don't want to
  5539. // use the profile data from a return type call (this was previously done in IRBuilder::BuildProfiledCallI)
  5540. if (!jitProfiling)
  5541. {
  5542. profileId = Js::Constants::NoProfileId;
  5543. }
  5544. }
  5545. if (profileId != Js::Constants::NoProfileId)
  5546. {
  5547. if (jitProfiling)
  5548. {
  5549. // In SimpleJit we want this call to be a profiled call after being jitted
  5550. instr = IR::JitProfilingInstr::New(newOpcode, dstOpnd, src1Opnd, m_func);
  5551. instr->AsJitProfilingInstr()->profileId = profileId;
  5552. instr->AsJitProfilingInstr()->isProfiledReturnCall = profiledReturn;
  5553. instr->AsJitProfilingInstr()->inlineCacheIndex = inlineCacheIndex;
  5554. }
  5555. else
  5556. {
  5557. instr = IR::ProfiledInstr::New(newOpcode, dstOpnd, src1Opnd, m_func);
  5558. instr->AsProfiledInstr()->u.profileId = profileId;
  5559. }
  5560. }
  5561. else
  5562. {
  5563. instr = IR::Instr::New(newOpcode, m_func);
  5564. instr->SetSrc1(src1Opnd);
  5565. if (dstOpnd != nullptr)
  5566. {
  5567. instr->SetDst(dstOpnd);
  5568. }
  5569. }
  5570. if (dstOpnd && newOpcode == Js::OpCode::NewScObject)
  5571. {
  5572. dstOpnd->SetValueType(ValueType::GetObject(ObjectType::UninitializedObject));
  5573. }
  5574. if (symDst && symDst->m_isSingleDef)
  5575. {
  5576. switch (instr->m_opcode)
  5577. {
  5578. case Js::OpCode::NewScObject:
  5579. case Js::OpCode::NewScObjectSpread:
  5580. case Js::OpCode::NewScObjectLiteral:
  5581. case Js::OpCode::NewScObjArray:
  5582. case Js::OpCode::NewScObjArraySpread:
  5583. symDst->m_isSafeThis = true;
  5584. symDst->m_isNotNumber = true;
  5585. break;
  5586. }
  5587. }
  5588. this->AddInstr(instr, offset);
  5589. this->BuildCallCommon(instr, symDst, ArgCount, flags);
  5590. return instr;
  5591. }
  5592. void
  5593. IRBuilder::BuildCallCommon(IR::Instr * instr, StackSym * symDst, Js::ArgSlot argCount, Js::CallFlags flags)
  5594. {
  5595. Js::OpCode newOpcode = instr->m_opcode;
  5596. IR::Instr * argInstr = nullptr;
  5597. IR::Instr * prevInstr = instr;
  5598. #if DBG
  5599. int count = 0;
  5600. #endif
  5601. // Link all the args of this call by creating a def/use chain through the src2.
  5602. AssertOrFailFast(!m_argStack->Empty());
  5603. for (argInstr = m_argStack->Pop();
  5604. argInstr && !m_argStack->Empty() && argInstr->m_opcode != Js::OpCode::StartCall;
  5605. argInstr = m_argStack->Pop())
  5606. {
  5607. prevInstr->SetSrc2(argInstr->GetDst());
  5608. prevInstr = argInstr;
  5609. #if DBG
  5610. count++;
  5611. #endif
  5612. }
  5613. AssertOrFailFast(argInstr == nullptr || argInstr->m_opcode == Js::OpCode::StartCall);
  5614. if (m_argStack->Empty())
  5615. {
  5616. this->callTreeHasSomeProfileInfo = false;
  5617. }
  5618. if (newOpcode == Js::OpCode::NewScObject || newOpcode == Js::OpCode::NewScObjArray
  5619. || newOpcode == Js::OpCode::NewScObjectSpread || newOpcode == Js::OpCode::NewScObjArraySpread)
  5620. {
  5621. #if DBG
  5622. count++;
  5623. #endif
  5624. m_argsOnStack++;
  5625. }
  5626. argCount = Js::CallInfo::GetArgCountWithExtraArgs(flags, argCount);
  5627. if (argInstr)
  5628. {
  5629. prevInstr->SetSrc2(argInstr->GetDst());
  5630. AssertMsg(instr->m_prev->m_opcode == Js::OpCode::LdSpreadIndices
  5631. // All non-spread calls need StartCall to have the same number of args
  5632. || (argInstr->GetSrc1()->IsIntConstOpnd()
  5633. && argInstr->GetSrc1()->AsIntConstOpnd()->GetValue() == count
  5634. && count == argCount), "StartCall has wrong number of arguments...");
  5635. }
  5636. else
  5637. {
  5638. AssertMsg(false, "Expect StartCall on other opcodes...");
  5639. }
  5640. // Update Func if this is the highest amount of stack we've used so far
  5641. // to push args.
  5642. #if DBG
  5643. m_callsOnStack--;
  5644. #endif
  5645. if (m_func->m_argSlotsForFunctionsCalled < m_argsOnStack)
  5646. m_func->m_argSlotsForFunctionsCalled = m_argsOnStack;
  5647. #if DBG
  5648. if (m_callsOnStack == 0)
  5649. Assert(m_argsOnStack == argCount);
  5650. #endif
  5651. m_argsOnStack -= argCount;
  5652. if (m_func->IsJitInDebugMode())
  5653. {
  5654. // Insert bailout after return from a call, script or library function call.
  5655. this->InsertBailOutForDebugger(
  5656. m_jnReader.GetCurrentOffset(), // bailout will resume at the offset of next instr.
  5657. c_debuggerBailOutKindForCall);
  5658. }
  5659. }
  5660. ///----------------------------------------------------------------------------
  5661. ///
  5662. /// IRBuilder::BuildClass
  5663. ///
  5664. /// Build IR instr for an InitClass instruction.
  5665. ///
  5666. ///----------------------------------------------------------------------------
  5667. template <typename SizePolicy>
  5668. void
  5669. IRBuilder::BuildClass(Js::OpCode newOpcode, uint32 offset)
  5670. {
  5671. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  5672. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5673. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_Class<SizePolicy>>();
  5674. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5675. {
  5676. this->DoClosureRegCheck(layout->Constructor);
  5677. this->DoClosureRegCheck(layout->Extends);
  5678. }
  5679. BuildClass(newOpcode, offset, layout->Constructor, layout->Extends);
  5680. }
  5681. void
  5682. IRBuilder::BuildClass(Js::OpCode newOpcode, uint32 offset, Js::RegSlot constructor, Js::RegSlot extends)
  5683. {
  5684. Assert(newOpcode == Js::OpCode::InitClass);
  5685. IR::Instr * insn = IR::Instr::New(newOpcode, m_func);
  5686. insn->SetSrc1(this->BuildSrcOpnd(constructor));
  5687. if (extends != Js::Constants::NoRegister)
  5688. {
  5689. insn->SetSrc2(this->BuildSrcOpnd(extends));
  5690. }
  5691. this->AddInstr(insn, offset);
  5692. }
  5693. ///----------------------------------------------------------------------------
  5694. ///
  5695. /// IRBuilder::BuildBrReg1
  5696. ///
  5697. /// Build IR instr for a BrReg1 instruction.
  5698. /// This is a conditional branch with a single source operand (e.g., "if (x)" ...)
  5699. ///
  5700. ///----------------------------------------------------------------------------
  5701. template <typename SizePolicy>
  5702. void
  5703. IRBuilder::BuildBrReg1(Js::OpCode newOpcode, uint32 offset)
  5704. {
  5705. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  5706. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5707. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_BrReg1<SizePolicy>>();
  5708. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5709. {
  5710. this->DoClosureRegCheck(layout->R1);
  5711. }
  5712. BuildBrReg1(newOpcode, offset, m_jnReader.GetCurrentOffset() + layout->RelativeJumpOffset, layout->R1);
  5713. }
  5714. void
  5715. IRBuilder::BuildBrReg1(Js::OpCode newOpcode, uint32 offset, uint targetOffset, Js::RegSlot srcRegSlot)
  5716. {
  5717. IR::BranchInstr * branchInstr;
  5718. IR::RegOpnd * srcOpnd;
  5719. srcOpnd = this->BuildSrcOpnd(srcRegSlot);
  5720. if (newOpcode == Js::OpCode::BrNotUndecl_A) {
  5721. IR::AddrOpnd *srcOpnd2 = IR::AddrOpnd::New(m_func->GetScriptContextInfo()->GetUndeclBlockVarAddr(),
  5722. IR::AddrOpndKindDynamicVar, this->m_func);
  5723. branchInstr = IR::BranchInstr::New(Js::OpCode::BrNotAddr_A, nullptr, srcOpnd, srcOpnd2, m_func);
  5724. } else {
  5725. branchInstr = IR::BranchInstr::New(newOpcode, nullptr, srcOpnd, m_func);
  5726. }
  5727. this->AddBranchInstr(branchInstr, offset, targetOffset);
  5728. }
  5729. ///----------------------------------------------------------------------------
  5730. ///
  5731. /// IRBuilder::BuildBrReg2
  5732. ///
  5733. /// Build IR instr for a BrReg2 instruction.
  5734. /// This is a conditional branch with a 2 source operands (e.g., "if (x == y)" ...)
  5735. ///
  5736. ///----------------------------------------------------------------------------
  5737. template <typename SizePolicy>
  5738. void
  5739. IRBuilder::BuildBrReg2(Js::OpCode newOpcode, uint32 offset)
  5740. {
  5741. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  5742. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5743. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_BrReg2<SizePolicy>>();
  5744. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5745. {
  5746. this->DoClosureRegCheck(layout->R1);
  5747. this->DoClosureRegCheck(layout->R2);
  5748. }
  5749. BuildBrReg2(newOpcode, offset, m_jnReader.GetCurrentOffset() + layout->RelativeJumpOffset, layout->R1, layout->R2);
  5750. }
  5751. template <typename SizePolicy>
  5752. void
  5753. IRBuilder::BuildBrReg1Unsigned1(Js::OpCode newOpcode, uint32 offset)
  5754. {
  5755. Assert(newOpcode == Js::OpCode::BrOnEmpty
  5756. /* || newOpcode == Js::OpCode::BrOnNotEmpty */ // BrOnNotEmpty not generate by the byte code
  5757. );
  5758. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  5759. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5760. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_BrReg1Unsigned1<SizePolicy>>();
  5761. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5762. {
  5763. this->DoClosureRegCheck(layout->R1);
  5764. }
  5765. BuildBrBReturn(newOpcode, offset, layout->R1, layout->C2, m_jnReader.GetCurrentOffset() + layout->RelativeJumpOffset);
  5766. }
  5767. void
  5768. IRBuilder::BuildBrBReturn(Js::OpCode newOpcode, uint32 offset, Js::RegSlot DestRegSlot, uint32 forInLoopLevel, uint32 targetOffset)
  5769. {
  5770. IR::Opnd *srcOpnd = this->BuildForInEnumeratorOpnd(forInLoopLevel);
  5771. IR::RegOpnd * destOpnd = this->BuildDstOpnd(DestRegSlot);
  5772. IR::BranchInstr * branchInstr = IR::BranchInstr::New(newOpcode, destOpnd, nullptr, srcOpnd, m_func);
  5773. this->AddBranchInstr(branchInstr, offset, targetOffset);
  5774. switch (newOpcode)
  5775. {
  5776. case Js::OpCode::BrOnEmpty:
  5777. destOpnd->SetValueType(ValueType::String);
  5778. break;
  5779. default:
  5780. Assert(false);
  5781. break;
  5782. };
  5783. }
  5784. void
  5785. IRBuilder::BuildBrReg2(Js::OpCode newOpcode, uint32 offset, uint targetOffset, Js::RegSlot R1, Js::RegSlot R2)
  5786. {
  5787. IR::BranchInstr * branchInstr;
  5788. if (newOpcode == Js::OpCode::BrOnEmpty
  5789. /* || newOpcode == Js::OpCode::BrOnNotEmpty */ // BrOnNotEmpty not generate by the byte code
  5790. )
  5791. {
  5792. BuildBrBReturn(newOpcode, offset, R1, R2, targetOffset);
  5793. return;
  5794. }
  5795. IR::RegOpnd * src1Opnd;
  5796. IR::RegOpnd * src2Opnd;
  5797. src1Opnd = this->BuildSrcOpnd(R1);
  5798. src2Opnd = this->BuildSrcOpnd(R2);
  5799. if (newOpcode == Js::OpCode::Case)
  5800. {
  5801. // generating branches for Cases is entirely handled
  5802. // by the SwitchIRBuilder
  5803. m_switchBuilder.OnCase(src1Opnd, src2Opnd, offset, targetOffset);
  5804. #ifdef BYTECODE_BRANCH_ISLAND
  5805. // Make sure that if there are branch island between the cases, we consume it first
  5806. EnsureConsumeBranchIsland();
  5807. #endif
  5808. // some instructions can't be optimized past, such as LdFld for objects. In these cases we have
  5809. // to inform the SwitchBuilder to flush any optimized cases that it has stored up to this point
  5810. // peeks the next opcode - to check if it is not a case statement (for example: the next instr can be a LdFld for objects)
  5811. Js::OpCode peekOpcode = m_jnReader.PeekOp();
  5812. if (peekOpcode != Js::OpCode::Case && peekOpcode != Js::OpCode::EndSwitch)
  5813. {
  5814. m_switchBuilder.FlushCases(m_jnReader.GetCurrentOffset());
  5815. }
  5816. }
  5817. else
  5818. {
  5819. branchInstr = IR::BranchInstr::New(newOpcode, nullptr, src1Opnd, src2Opnd, m_func);
  5820. this->AddBranchInstr(branchInstr, offset, targetOffset);
  5821. }
  5822. }
  5823. void
  5824. IRBuilder::BuildEmpty(Js::OpCode newOpcode, uint32 offset)
  5825. {
  5826. IR::Instr *instr;
  5827. m_jnReader.Empty();
  5828. instr = IR::Instr::New(newOpcode, m_func);
  5829. switch (newOpcode)
  5830. {
  5831. case Js::OpCode::CommitScope:
  5832. {
  5833. IR::RegOpnd * src1Opnd;
  5834. src1Opnd = this->BuildSrcOpnd(m_func->GetJITFunctionBody()->GetLocalClosureReg());
  5835. IR::LabelInstr *labelNull = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  5836. IR::RegOpnd * funcExprOpnd = IR::RegOpnd::New(TyVar, m_func);
  5837. instr = IR::Instr::New(Js::OpCode::LdFuncExpr, funcExprOpnd, m_func);
  5838. this->AddInstr(instr, offset);
  5839. IR::BranchInstr *branchInstr = IR::BranchInstr::New(Js::OpCode::BrFncCachedScopeNeq, labelNull,
  5840. funcExprOpnd, src1Opnd, this->m_func);
  5841. this->AddInstr(branchInstr, offset);
  5842. instr = IR::Instr::New(newOpcode, this->m_func);
  5843. instr->SetSrc1(src1Opnd);
  5844. this->AddInstr(instr, offset);
  5845. this->AddInstr(labelNull, Js::Constants::NoByteCodeOffset);
  5846. return;
  5847. }
  5848. case Js::OpCode::Ret:
  5849. {
  5850. IR::RegOpnd *regOpnd = BuildDstOpnd(0);
  5851. instr->SetSrc1(regOpnd);
  5852. this->AddInstr(instr, offset);
  5853. break;
  5854. }
  5855. case Js::OpCode::Leave:
  5856. {
  5857. IR::BranchInstr * branchInstr;
  5858. IR::LabelInstr * labelInstr;
  5859. if (this->handlerOffsetStack && !this->handlerOffsetStack->Empty() && this->handlerOffsetStack->Top().Second())
  5860. {
  5861. // If the try region has a break block, we don't want the Flowgraph to move all of that code out of the loop
  5862. // because an exception will bring the control back into the loop. The branch out of the loop (which is the
  5863. // reason for the code to be a break block) can still be moved out though.
  5864. //
  5865. // "BrOnException $catch" is inserted before Leave's in the try region to instrument flow from the try region
  5866. // to the catch region (which is in the loop).
  5867. IR::BranchInstr * brOnException = IR::BranchInstr::New(Js::OpCode::BrOnException, nullptr, this->m_func);
  5868. this->AddBranchInstr(brOnException, offset, this->handlerOffsetStack->Top().First());
  5869. }
  5870. labelInstr = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  5871. branchInstr = IR::BranchInstr::New(newOpcode, labelInstr, this->m_func);
  5872. this->AddInstr(branchInstr, offset);
  5873. this->AddInstr(labelInstr, Js::Constants::NoByteCodeOffset);
  5874. break;
  5875. }
  5876. case Js::OpCode::LeaveNull:
  5877. finallyBlockLevel--;
  5878. this->AddInstr(instr, offset);
  5879. break;
  5880. case Js::OpCode::Finally:
  5881. if (this->handlerOffsetStack)
  5882. {
  5883. AssertOrFailFast(!this->handlerOffsetStack->Empty());
  5884. AssertOrFailFast(this->handlerOffsetStack->Top().Second() == false);
  5885. this->handlerOffsetStack->Pop();
  5886. }
  5887. finallyBlockLevel++;
  5888. this->AddInstr(IR::Instr::New(Js::OpCode::Finally, this->m_func), offset);
  5889. break;
  5890. case Js::OpCode::Break:
  5891. if (m_func->IsJitInDebugMode())
  5892. {
  5893. // Add explicit bailout.
  5894. this->InsertBailOutForDebugger(offset, IR::BailOutExplicit);
  5895. }
  5896. else
  5897. {
  5898. // Default behavior, let's keep it for now, removed in lowerer.
  5899. this->AddInstr(instr, offset);
  5900. }
  5901. break;
  5902. case Js::OpCode::BeginBodyScope:
  5903. {
  5904. // This marks the end of a param socpe which is not merged with body scope.
  5905. // So we have to first cache the closure so that we can use it to copy the initial values for
  5906. // body syms from corresponding param syms (LdParamSlot). Body should get its own scope slot.
  5907. Assert(!this->IsParamScopeDone());
  5908. this->SetParamScopeDone();
  5909. IR::Opnd * localClosureOpnd;
  5910. if (this->m_func->GetLocalClosureSym() != nullptr)
  5911. {
  5912. localClosureOpnd = IR::RegOpnd::New(this->m_func->GetLocalClosureSym(), TyVar, this->m_func);
  5913. }
  5914. else
  5915. {
  5916. AssertOrFailFast(this->m_func->GetJITFunctionBody()->GetScopeSlotArraySize() == 0 && !this->m_func->GetJITFunctionBody()->HasScopeObject());
  5917. localClosureOpnd = IR::IntConstOpnd::New(0, TyVar, this->m_func);
  5918. }
  5919. this->AddInstr(
  5920. IR::Instr::New(
  5921. Js::OpCode::Ld_A,
  5922. this->BuildDstOpnd(this->m_func->GetJITFunctionBody()->GetParamClosureReg()),
  5923. localClosureOpnd,
  5924. this->m_func),
  5925. offset);
  5926. // Create a new local closure for the body when either body scope has scope slots allocated or
  5927. // eval is present which can leak declarations.
  5928. if (this->m_func->GetJITFunctionBody()->GetScopeSlotArraySize() > 0 || this->m_func->GetJITFunctionBody()->HasScopeObject())
  5929. {
  5930. if (this->m_func->GetJITFunctionBody()->HasScopeObject())
  5931. {
  5932. if (this->m_func->GetJITFunctionBody()->HasCachedScopePropIds())
  5933. {
  5934. this->BuildInitCachedScope(0, Js::Constants::NoByteCodeOffset);
  5935. }
  5936. else
  5937. {
  5938. this->AddInstr(
  5939. IR::Instr::New(
  5940. Js::OpCode::NewScopeObject,
  5941. this->BuildDstOpnd(this->m_func->GetJITFunctionBody()->GetLocalClosureReg()),
  5942. m_func),
  5943. Js::Constants::NoByteCodeOffset);
  5944. }
  5945. }
  5946. else
  5947. {
  5948. this->AddInstr(
  5949. IR::Instr::New(
  5950. Js::OpCode::NewScopeSlots,
  5951. this->BuildDstOpnd(this->m_func->GetJITFunctionBody()->GetLocalClosureReg()),
  5952. IR::IntConstOpnd::New(this->m_func->GetJITFunctionBody()->GetScopeSlotArraySize() + Js::ScopeSlots::FirstSlotIndex, TyUint32, this->m_func),
  5953. m_func),
  5954. Js::Constants::NoByteCodeOffset);
  5955. }
  5956. IR::Instr* lfd = IR::Instr::New(
  5957. Js::OpCode::LdFrameDisplay,
  5958. this->BuildDstOpnd(this->m_func->GetJITFunctionBody()->GetLocalFrameDisplayReg()),
  5959. this->BuildDstOpnd(this->m_func->GetJITFunctionBody()->GetLocalClosureReg()),
  5960. this->BuildDstOpnd(this->m_func->GetJITFunctionBody()->GetLocalFrameDisplayReg()),
  5961. this->m_func);
  5962. this->AddInstr(
  5963. lfd,
  5964. Js::Constants::NoByteCodeOffset);
  5965. lfd->isNonFastPathFrameDisplay = true;
  5966. }
  5967. break;
  5968. }
  5969. default:
  5970. this->AddInstr(instr, offset);
  5971. break;
  5972. }
  5973. }
  5974. #ifdef BYTECODE_BRANCH_ISLAND
  5975. void
  5976. IRBuilder::EnsureConsumeBranchIsland()
  5977. {
  5978. if (m_jnReader.PeekOp() == Js::OpCode::Br)
  5979. {
  5980. // Save the old offset
  5981. uint offset = m_jnReader.GetCurrentOffset();
  5982. // Read the potentially a branch around
  5983. Js::LayoutSize layoutSize;
  5984. Js::OpCode opcode = m_jnReader.ReadOp(layoutSize);
  5985. Assert(opcode == Js::OpCode::Br);
  5986. Assert(layoutSize == Js::SmallLayout);
  5987. const unaligned Js::OpLayoutBr * playout = m_jnReader.Br();
  5988. unsigned int targetOffset = m_jnReader.GetCurrentOffset() + playout->RelativeJumpOffset;
  5989. uint branchIslandOffset = m_jnReader.GetCurrentOffset();
  5990. if (branchIslandOffset == targetOffset)
  5991. {
  5992. // branch to next, there is no long branch
  5993. m_jnReader.SetCurrentOffset(offset);
  5994. return;
  5995. }
  5996. // Ignore all the BrLong
  5997. while (m_jnReader.PeekOp() == Js::OpCode::BrLong)
  5998. {
  5999. opcode = m_jnReader.ReadOp(layoutSize);
  6000. Assert(opcode == Js::OpCode::BrLong);
  6001. Assert(layoutSize == Js::SmallLayout);
  6002. m_jnReader.BrLong();
  6003. }
  6004. // Confirm that is a branch around
  6005. if ((uint)m_jnReader.GetCurrentOffset() == targetOffset)
  6006. {
  6007. // Really consume the branch island
  6008. m_jnReader.SetCurrentOffset(branchIslandOffset);
  6009. ConsumeBranchIsland();
  6010. // Mark the virtual branch around as a redirect long branch as well
  6011. // so that if it is the target of another branch, it will just keep pass
  6012. // the branch island
  6013. Assert(longBranchMap);
  6014. Assert(offset < m_offsetToInstructionCount);
  6015. Assert(m_offsetToInstruction[offset] == nullptr);
  6016. m_offsetToInstruction[offset] = VirtualLongBranchInstr;
  6017. longBranchMap->Add(offset, targetOffset);
  6018. }
  6019. else
  6020. {
  6021. // Reset the offset
  6022. m_jnReader.SetCurrentOffset(offset);
  6023. }
  6024. }
  6025. }
  6026. IR::Instr * const IRBuilder::VirtualLongBranchInstr = (IR::Instr *)-1;
  6027. void
  6028. IRBuilder::ConsumeBranchIsland()
  6029. {
  6030. do
  6031. {
  6032. uint32 offset = m_jnReader.GetCurrentOffset();
  6033. Js::LayoutSize layoutSize;
  6034. Js::OpCode opcode = m_jnReader.ReadOp(layoutSize);
  6035. Assert(opcode == Js::OpCode::BrLong);
  6036. Assert(layoutSize == Js::SmallLayout);
  6037. BuildBrLong(Js::OpCode::BrLong, offset);
  6038. }
  6039. while (m_jnReader.PeekOp() == Js::OpCode::BrLong);
  6040. }
  6041. void
  6042. IRBuilder::BuildBrLong(Js::OpCode newOpcode, uint32 offset)
  6043. {
  6044. Assert(newOpcode == Js::OpCode::BrLong);
  6045. Assert(!OpCodeAttr::HasMultiSizeLayout(newOpcode));
  6046. Assert(offset != Js::Constants::NoByteCodeOffset);
  6047. const unaligned Js::OpLayoutBrLong *branchInsn = m_jnReader.BrLong();
  6048. unsigned int targetOffset = m_jnReader.GetCurrentOffset() + branchInsn->RelativeJumpOffset;
  6049. Assert(offset < m_offsetToInstructionCount);
  6050. Assert(m_offsetToInstruction[offset] == nullptr);
  6051. // BrLong are also just the target of another branch, just set a virtual long branch instr
  6052. // and remap the original branch to the actual destination in ResolveVirtualLongBranch
  6053. m_offsetToInstruction[offset] = VirtualLongBranchInstr;
  6054. longBranchMap->Add(offset, targetOffset);
  6055. }
  6056. uint
  6057. IRBuilder::ResolveVirtualLongBranch(IR::BranchInstr * branchInstr, uint offset)
  6058. {
  6059. Assert(longBranchMap);
  6060. uint32 targetOffset;
  6061. if (!longBranchMap->TryGetValue(offset, &targetOffset))
  6062. {
  6063. // If we see a VirtualLongBranchInstr, we must have a mapping to the real target offset
  6064. Assert(false);
  6065. Fatal();
  6066. }
  6067. // If this is a jump out of the loop body we need to load the return IP and jump to the loop exit instead
  6068. if (!IsLoopBodyOuterOffset(targetOffset))
  6069. {
  6070. return targetOffset;
  6071. }
  6072. // Multi branch shouldn't be exiting a loop
  6073. Assert(branchInstr->m_opcode != Js::OpCode::MultiBr);
  6074. // Don't load the return IP if it is already loaded (for the case of early exit)
  6075. if (!IsLoopBodyReturnIPInstr(branchInstr->m_prev))
  6076. {
  6077. IR::Instr * returnIPInstr = CreateLoopBodyReturnIPInstr(targetOffset, branchInstr->GetByteCodeOffset());
  6078. // Any jump to this branch to jump to the return IP load instr first
  6079. uint32 branchInstrByteCodeOffset = branchInstr->GetByteCodeOffset();
  6080. Assert(this->m_offsetToInstruction[branchInstrByteCodeOffset] == branchInstr ||
  6081. (this->m_offsetToInstruction[branchInstrByteCodeOffset]->HasBailOutInfo() &&
  6082. this->m_offsetToInstruction[branchInstrByteCodeOffset]->GetBailOutKind() == IR::BailOutInjected));
  6083. InsertInstr(returnIPInstr, branchInstr);
  6084. }
  6085. return GetLoopBodyExitInstrOffset();
  6086. }
  6087. #endif
  6088. ///----------------------------------------------------------------------------
  6089. ///
  6090. /// IRBuilder::BuildBr
  6091. ///
  6092. /// Build IR instr for a Br (unconditional branch) instruction.
  6093. /// or TryCatch/TryFinally
  6094. ///
  6095. ///----------------------------------------------------------------------------
  6096. void
  6097. IRBuilder::BuildBr(Js::OpCode newOpcode, uint32 offset)
  6098. {
  6099. Assert(!OpCodeAttr::HasMultiSizeLayout(newOpcode));
  6100. IR::BranchInstr * branchInstr;
  6101. const unaligned Js::OpLayoutBr *branchInsn = m_jnReader.Br();
  6102. unsigned int targetOffset = m_jnReader.GetCurrentOffset() + branchInsn->RelativeJumpOffset;
  6103. #ifdef BYTECODE_BRANCH_ISLAND
  6104. bool isLongBranchIsland = (m_jnReader.PeekOp() == Js::OpCode::BrLong);
  6105. if (isLongBranchIsland)
  6106. {
  6107. ConsumeBranchIsland();
  6108. }
  6109. #endif
  6110. if(newOpcode == Js::OpCode::EndSwitch)
  6111. {
  6112. m_switchBuilder.EndSwitch(offset, targetOffset);
  6113. return;
  6114. }
  6115. #ifdef PERF_HINT
  6116. else if (PHASE_TRACE1(Js::PerfHintPhase) && (newOpcode == Js::OpCode::TryCatch || newOpcode == Js::OpCode::TryFinally) )
  6117. {
  6118. WritePerfHint(PerfHints::HasTryBlock, this->m_func, offset);
  6119. }
  6120. #endif
  6121. #ifdef BYTECODE_BRANCH_ISLAND
  6122. if (isLongBranchIsland && (targetOffset == (uint)m_jnReader.GetCurrentOffset()))
  6123. {
  6124. // Branch to next (probably after consume branch island), try to not emit the branch
  6125. // Mark the jump around instruction as a virtual long branch as well so we can just
  6126. // fall through instead of branch to exit
  6127. Assert(offset < m_offsetToInstructionCount);
  6128. if (m_offsetToInstruction[offset] == nullptr)
  6129. {
  6130. m_offsetToInstruction[offset] = VirtualLongBranchInstr;
  6131. longBranchMap->Add(offset, targetOffset);
  6132. return;
  6133. }
  6134. // We may have already create an instruction on this offset as a statement boundary
  6135. // or in the bailout at every byte code case.
  6136. // The statement boundary case only happens if we have emitted the long branch island
  6137. // after an existing no fall through instruction, but that instruction also happen to be
  6138. // branch to next. We will just generate an actual branch to next instruction.
  6139. Assert(m_offsetToInstruction[offset]->m_opcode == Js::OpCode::StatementBoundary
  6140. || (Js::Configuration::Global.flags.IsEnabled(Js::BailOutAtEveryByteCodeFlag)
  6141. && m_offsetToInstruction[offset]->m_opcode == Js::OpCode::BailOnEqual));
  6142. }
  6143. #endif
  6144. if ((newOpcode == Js::OpCode::TryCatch) && this->handlerOffsetStack)
  6145. {
  6146. this->handlerOffsetStack->Push(Pair<uint, bool>(targetOffset, true));
  6147. }
  6148. else if ((newOpcode == Js::OpCode::TryFinally) && this->handlerOffsetStack)
  6149. {
  6150. this->handlerOffsetStack->Push(Pair<uint, bool>(targetOffset, false));
  6151. }
  6152. branchInstr = IR::BranchInstr::New(newOpcode, nullptr, m_func);
  6153. this->AddBranchInstr(branchInstr, offset, targetOffset);
  6154. }
  6155. void
  6156. IRBuilder::BuildBrS(Js::OpCode newOpcode, uint32 offset)
  6157. {
  6158. Assert(!OpCodeAttr::HasMultiSizeLayout(newOpcode));
  6159. IR::BranchInstr * branchInstr;
  6160. const unaligned Js::OpLayoutBrS *branchInsn = m_jnReader.BrS();
  6161. unsigned int targetOffset = m_jnReader.GetCurrentOffset() + branchInsn->RelativeJumpOffset;
  6162. branchInstr = IR::BranchInstr::New(newOpcode, nullptr,
  6163. IR::IntConstOpnd::New(branchInsn->val,
  6164. TyInt32, m_func),m_func);
  6165. this->AddBranchInstr(branchInstr, offset, targetOffset);
  6166. }
  6167. ///----------------------------------------------------------------------------
  6168. ///
  6169. /// IRBuilder::BuildBrProperty
  6170. ///
  6171. /// Build IR instr for a BrProperty instruction.
  6172. /// This is a conditional branch that tests whether the given property
  6173. /// is present on the given instance.
  6174. ///
  6175. ///----------------------------------------------------------------------------
  6176. void
  6177. IRBuilder::BuildBrProperty(Js::OpCode newOpcode, uint32 offset)
  6178. {
  6179. Assert(!OpCodeAttr::HasMultiSizeLayout(newOpcode));
  6180. const unaligned Js::OpLayoutBrProperty *branchInsn = m_jnReader.BrProperty();
  6181. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  6182. {
  6183. this->DoClosureRegCheck(branchInsn->Instance);
  6184. }
  6185. IR::BranchInstr * branchInstr;
  6186. Js::PropertyId propertyId =
  6187. m_func->GetJITFunctionBody()->GetReferencedPropertyId(branchInsn->PropertyIdIndex);
  6188. unsigned int targetOffset = m_jnReader.GetCurrentOffset() + branchInsn->RelativeJumpOffset;
  6189. IR::SymOpnd * fieldSymOpnd = this->BuildFieldOpnd(newOpcode, branchInsn->Instance, propertyId, branchInsn->PropertyIdIndex, PropertyKindData);
  6190. branchInstr = IR::BranchInstr::New(newOpcode, nullptr, fieldSymOpnd, m_func);
  6191. this->AddBranchInstr(branchInstr, offset, targetOffset);
  6192. }
  6193. void
  6194. IRBuilder::BuildBrLocalProperty(Js::OpCode newOpcode, uint32 offset)
  6195. {
  6196. Assert(!OpCodeAttr::HasMultiSizeLayout(newOpcode));
  6197. switch (newOpcode)
  6198. {
  6199. case Js::OpCode::BrOnNoLocalProperty:
  6200. newOpcode = Js::OpCode::BrOnNoProperty;
  6201. break;
  6202. default:
  6203. Assert(0);
  6204. break;
  6205. }
  6206. const unaligned Js::OpLayoutBrLocalProperty *branchInsn = m_jnReader.BrLocalProperty();
  6207. if (m_func->GetLocalClosureSym()->HasByteCodeRegSlot())
  6208. {
  6209. IR::ByteCodeUsesInstr * byteCodeUse = IR::ByteCodeUsesInstr::New(m_func, offset);
  6210. byteCodeUse->SetNonOpndSymbol(m_func->GetLocalClosureSym()->m_id);
  6211. this->AddInstr(byteCodeUse, offset);
  6212. }
  6213. IR::BranchInstr * branchInstr;
  6214. Js::PropertyId propertyId =
  6215. m_func->GetJITFunctionBody()->GetReferencedPropertyId(branchInsn->PropertyIdIndex);
  6216. unsigned int targetOffset = m_jnReader.GetCurrentOffset() + branchInsn->RelativeJumpOffset;
  6217. IR::SymOpnd * fieldSymOpnd = this->BuildFieldOpnd(newOpcode, m_func->GetJITFunctionBody()->GetLocalClosureReg(), propertyId, branchInsn->PropertyIdIndex, PropertyKindData);
  6218. branchInstr = IR::BranchInstr::New(newOpcode, nullptr, fieldSymOpnd, m_func);
  6219. this->AddBranchInstr(branchInstr, offset, targetOffset);
  6220. }
  6221. void
  6222. IRBuilder::BuildBrEnvProperty(Js::OpCode newOpcode, uint32 offset)
  6223. {
  6224. Assert(!OpCodeAttr::HasMultiSizeLayout(newOpcode));
  6225. const unaligned Js::OpLayoutBrEnvProperty *branchInsn = m_jnReader.BrEnvProperty();
  6226. IR::Instr *instr;
  6227. IR::BranchInstr * branchInstr;
  6228. IR::RegOpnd *regOpnd;
  6229. IR::SymOpnd *fieldOpnd;
  6230. PropertySym *fieldSym;
  6231. fieldOpnd = this->BuildFieldOpnd(Js::OpCode::LdSlotArr, this->GetEnvReg(), branchInsn->SlotIndex, (Js::PropertyIdIndexType)-1, PropertyKindSlotArray);
  6232. regOpnd = IR::RegOpnd::New(TyVar, m_func);
  6233. instr = IR::Instr::New(Js::OpCode::LdSlotArr, regOpnd, fieldOpnd, m_func);
  6234. this->AddInstr(instr, offset);
  6235. Js::PropertyId propertyId =
  6236. m_func->GetJITFunctionBody()->GetReferencedPropertyId(branchInsn->PropertyIdIndex);
  6237. unsigned int targetOffset = m_jnReader.GetCurrentOffset() + branchInsn->RelativeJumpOffset;\
  6238. fieldSym = PropertySym::New(regOpnd->m_sym, propertyId, branchInsn->PropertyIdIndex, (uint)-1, PropertyKindData, m_func);
  6239. fieldOpnd = IR::SymOpnd::New(fieldSym, TyVar, m_func);
  6240. branchInstr = IR::BranchInstr::New(Js::OpCode::BrOnNoProperty, nullptr, fieldOpnd, m_func);
  6241. this->AddBranchInstr(branchInstr, offset, targetOffset);
  6242. }
  6243. ///----------------------------------------------------------------------------
  6244. ///
  6245. /// IRBuilder::AddBranchInstr
  6246. ///
  6247. /// Create a branch/offset pair which will be fixed up at the end of the
  6248. /// IRBuilder phase and add the instruction
  6249. ///
  6250. ///----------------------------------------------------------------------------
  6251. BranchReloc *
  6252. IRBuilder::AddBranchInstr(IR::BranchInstr * branchInstr, uint32 offset, uint32 targetOffset)
  6253. {
  6254. AssertOrFailFast(targetOffset <= m_func->GetJITFunctionBody()->GetByteCodeLength());
  6255. //
  6256. // Loop jitting would be done only till the LoopEnd
  6257. // Any branches beyond that offset are for the return stmt
  6258. //
  6259. if (IsLoopBodyOuterOffset(targetOffset))
  6260. {
  6261. // if we have loaded the loop IP sym from the ProfiledLoopEnd then don't add it here
  6262. if (!IsLoopBodyReturnIPInstr(m_lastInstr))
  6263. {
  6264. this->InsertLoopBodyReturnIPInstr(targetOffset, offset);
  6265. }
  6266. // Jump the restore StSlot and Ret instruction
  6267. targetOffset = GetLoopBodyExitInstrOffset();
  6268. }
  6269. BranchReloc * reloc = nullptr;
  6270. reloc = this->CreateRelocRecord(branchInstr, offset, targetOffset);
  6271. this->AddInstr(branchInstr, offset);
  6272. return reloc;
  6273. }
  6274. BranchReloc *
  6275. IRBuilder::CreateRelocRecord(IR::BranchInstr * branchInstr, uint32 offset, uint32 targetOffset)
  6276. {
  6277. BranchReloc * reloc = JitAnew(this->m_tempAlloc, BranchReloc, branchInstr, offset, targetOffset);
  6278. this->branchRelocList->Prepend(reloc);
  6279. return reloc;
  6280. }
  6281. ///----------------------------------------------------------------------------
  6282. ///
  6283. /// IRBuilder::BuildRegexFromPattern
  6284. ///
  6285. /// Build a new RegEx instruction. Simply construct a var to hold the regex
  6286. /// and load it as an immediate into a register.
  6287. ///
  6288. ///----------------------------------------------------------------------------
  6289. void
  6290. IRBuilder::BuildRegexFromPattern(Js::RegSlot dstRegSlot, uint32 patternIndex, uint32 offset)
  6291. {
  6292. IR::Instr * instr;
  6293. IR::RegOpnd* dstOpnd = this->BuildDstOpnd(dstRegSlot);
  6294. dstOpnd->SetValueType(ValueType::GetObject(ObjectType::RegExp));
  6295. IR::Opnd * regexOpnd = IR::AddrOpnd::New(m_func->GetJITFunctionBody()->GetLiteralRegexAddr(patternIndex), IR::AddrOpndKindDynamicMisc, this->m_func);
  6296. instr = IR::Instr::New(Js::OpCode::NewRegEx, dstOpnd, regexOpnd, this->m_func);
  6297. this->AddInstr(instr, offset);
  6298. }
  6299. bool
  6300. IRBuilder::IsFloatFunctionCallsite(Js::BuiltinFunction index, size_t argc)
  6301. {
  6302. return Js::JavascriptLibrary::IsFloatFunctionCallsite(index, argc);
  6303. }
  6304. void
  6305. IRBuilder::CheckBuiltIn(PropertySym * propertySym, Js::BuiltinFunction *puBuiltInIndex)
  6306. {
  6307. Js::BuiltinFunction index = Js::BuiltinFunction::None;
  6308. // Check whether the propertySym appears to be a built-in.
  6309. if (propertySym->m_fieldKind != PropertyKindData)
  6310. {
  6311. return;
  6312. }
  6313. index = Js::JavascriptLibrary::GetBuiltinFunctionForPropId(propertySym->m_propertyId);
  6314. if (index == Js::BuiltinFunction::None)
  6315. {
  6316. return;
  6317. }
  6318. // If the target is one of the Math built-ins, see whether the stack sym is the
  6319. // global "Math".
  6320. if (Js::JavascriptLibrary::IsFltFunc(index))
  6321. {
  6322. if (!propertySym->m_stackSym->m_isSingleDef)
  6323. {
  6324. return;
  6325. }
  6326. IR::Instr *instr = propertySym->m_stackSym->m_instrDef;
  6327. AssertMsg(instr != nullptr, "Single-def stack sym w/o def instr?");
  6328. if (instr->m_opcode != Js::OpCode::LdRootFld && instr->m_opcode != Js::OpCode::LdRootFldForTypeOf)
  6329. {
  6330. return;
  6331. }
  6332. IR::Opnd * opnd = instr->GetSrc1();
  6333. AssertMsg(opnd != nullptr && opnd->IsSymOpnd() && opnd->AsSymOpnd()->m_sym->IsPropertySym(),
  6334. "LdRootFld w/o propertySym src?");
  6335. if (opnd->AsSymOpnd()->m_sym->AsPropertySym()->m_propertyId != Js::PropertyIds::Math)
  6336. {
  6337. return;
  6338. }
  6339. }
  6340. *puBuiltInIndex = index;
  6341. }
  6342. StackSym *
  6343. IRBuilder::EnsureStackFuncPtrSym()
  6344. {
  6345. StackSym * sym = this->m_stackFuncPtrSym;
  6346. if (sym)
  6347. {
  6348. return sym;
  6349. }
  6350. if (m_func->IsLoopBody() && m_func->DoStackNestedFunc())
  6351. {
  6352. Assert(m_func->IsTopFunc());
  6353. sym = StackSym::New(TyVar, m_func);
  6354. this->m_stackFuncPtrSym = sym;
  6355. }
  6356. return sym;
  6357. }
  6358. void
  6359. IRBuilder::GenerateLoopBodySlotAccesses(uint offset)
  6360. {
  6361. //
  6362. // The interpreter instance is passed as 0th argument to the JITted loop body function.
  6363. // Always load the argument, then use it to generate any necessary store-slots.
  6364. //
  6365. uint16 argument = 0;
  6366. StackSym *symSrc = StackSym::NewParamSlotSym(argument + 1, m_func);
  6367. symSrc->m_offset = (argument + LowererMD::GetFormalParamOffset()) * MachPtr;
  6368. symSrc->m_allocated = true;
  6369. m_func->SetHasImplicitParamLoad();
  6370. IR::SymOpnd *srcOpnd = IR::SymOpnd::New(symSrc, TyVar, m_func);
  6371. StackSym *loopParamSym = m_func->EnsureLoopParamSym();
  6372. IR::RegOpnd *loopParamOpnd = IR::RegOpnd::New(loopParamSym, TyMachPtr, m_func);
  6373. IR::Instr *instrArgIn = IR::Instr::New(Js::OpCode::ArgIn_A, loopParamOpnd, srcOpnd, m_func);
  6374. m_func->m_headInstr->InsertAfter(instrArgIn);
  6375. StackSym *stackFuncPtrSym = this->m_stackFuncPtrSym;
  6376. if (stackFuncPtrSym)
  6377. {
  6378. PropertySym * fieldSym = PropertySym::FindOrCreate(loopParamSym->m_id, (Js::PropertyId)(Js::InterpreterStackFrame::GetOffsetOfStackNestedFunctions() / sizeof(Js::Var)), (uint32)-1, (uint)-1, PropertyKindLocalSlots, m_func);
  6379. IR::SymOpnd * opndPtrRef = IR::SymOpnd::New(fieldSym, TyVar, m_func);
  6380. IR::Instr * instrPtrInit = IR::Instr::New(Js::OpCode::LdSlot, IR::RegOpnd::New(stackFuncPtrSym, TyVar, m_func), opndPtrRef, m_func);
  6381. instrArgIn->InsertAfter(instrPtrInit);
  6382. }
  6383. GenerateLoopBodyStSlots(loopParamSym->m_id, offset);
  6384. }
  6385. void
  6386. IRBuilder::GenerateLoopBodyStSlots(SymID loopParamSymId, uint offset)
  6387. {
  6388. if (this->m_stSlots->Count() == 0)
  6389. {
  6390. return;
  6391. }
  6392. FOREACH_BITSET_IN_FIXEDBV(regSlot, this->m_stSlots)
  6393. {
  6394. this->GenerateLoopBodyStSlot(regSlot, offset);
  6395. }
  6396. NEXT_BITSET_IN_FIXEDBV;
  6397. }
  6398. IR::Instr *
  6399. IRBuilder::GenerateLoopBodyStSlot(Js::RegSlot regSlot, uint offset)
  6400. {
  6401. Assert(!this->RegIsConstant((Js::RegSlot)regSlot));
  6402. StackSym *loopParamSym = m_func->EnsureLoopParamSym();
  6403. PropertySym * fieldSym = PropertySym::FindOrCreate(loopParamSym->m_id, (Js::PropertyId)(regSlot + this->m_loopBodyLocalsStartSlot), (uint32)-1, (uint)-1, PropertyKindLocalSlots, m_func);
  6404. IR::SymOpnd * fieldSymOpnd = IR::SymOpnd::New(fieldSym, TyVar, m_func);
  6405. IR::RegOpnd * regOpnd = this->BuildSrcOpnd((Js::RegSlot)regSlot);
  6406. #if !FLOATVAR
  6407. Js::OpCode opcode = Js::OpCode::StSlotBoxTemp;
  6408. #else
  6409. Js::OpCode opcode = Js::OpCode::StSlot;
  6410. #endif
  6411. IR::Instr * stSlotInstr = IR::Instr::New(opcode, fieldSymOpnd, regOpnd, m_func);
  6412. if (offset != Js::Constants::NoByteCodeOffset)
  6413. {
  6414. this->AddInstr(stSlotInstr, offset);
  6415. return nullptr;
  6416. }
  6417. else
  6418. {
  6419. return stSlotInstr;
  6420. }
  6421. }
  6422. IR::Instr *
  6423. IRBuilder::CreateLoopBodyReturnIPInstr(uint targetOffset, uint offset)
  6424. {
  6425. IR::RegOpnd * retOpnd = IR::RegOpnd::New(m_loopBodyRetIPSym, TyMachReg, m_func);
  6426. IR::IntConstOpnd * exitOffsetOpnd = IR::IntConstOpnd::New(targetOffset, TyMachReg, m_func);
  6427. return IR::Instr::New(Js::OpCode::Ld_I4, retOpnd, exitOffsetOpnd, m_func);
  6428. }
  6429. IR::Opnd *
  6430. IRBuilder::InsertLoopBodyReturnIPInstr(uint targetOffset, uint offset)
  6431. {
  6432. IR::Instr * setRetValueInstr = CreateLoopBodyReturnIPInstr(targetOffset, offset);
  6433. this->AddInstr(setRetValueInstr, offset);
  6434. return setRetValueInstr->GetDst();
  6435. }
  6436. void
  6437. IRBuilder::InsertDoneLoopBodyLoopCounter(uint32 lastOffset)
  6438. {
  6439. if (m_loopCounterSym == nullptr)
  6440. {
  6441. return;
  6442. }
  6443. IR::Instr * loopCounterStoreInstr = IR::Instr::New(Js::OpCode::StLoopBodyCount, m_func);
  6444. IR::RegOpnd *countRegOpnd = IR::RegOpnd::New(m_loopCounterSym, TyInt32, this->m_func);
  6445. countRegOpnd->SetIsJITOptimizedReg(true);
  6446. loopCounterStoreInstr->SetSrc1(countRegOpnd);
  6447. this->AddInstr(loopCounterStoreInstr, lastOffset + 1);
  6448. return;
  6449. }
  6450. void
  6451. IRBuilder::InsertIncrLoopBodyLoopCounter(IR::LabelInstr *loopTopLabelInstr)
  6452. {
  6453. Assert(this->IsLoopBody());
  6454. IR::RegOpnd *loopCounterOpnd = IR::RegOpnd::New(m_loopCounterSym, TyInt32, this->m_func);
  6455. IR::Instr * incr = IR::Instr::New(Js::OpCode::IncrLoopBodyCount, loopCounterOpnd, loopCounterOpnd, this->m_func);
  6456. loopCounterOpnd->SetIsJITOptimizedReg(true);
  6457. IR::Instr* nextRealInstr = loopTopLabelInstr->GetNextRealInstr();
  6458. InsertInstr(incr, nextRealInstr);
  6459. }
  6460. void
  6461. IRBuilder::InsertInitLoopBodyLoopCounter(uint loopNum)
  6462. {
  6463. Assert(this->IsLoopBody());
  6464. intptr_t loopHeader = m_func->GetJITFunctionBody()->GetLoopHeaderAddr(loopNum);
  6465. Assert(m_func->GetWorkItem()->GetLoopHeaderAddr() == loopHeader); //Init only once
  6466. m_loopCounterSym = StackSym::New(TyVar, this->m_func);
  6467. IR::RegOpnd* loopCounterOpnd = IR::RegOpnd::New(m_loopCounterSym, TyVar, this->m_func);
  6468. loopCounterOpnd->SetIsJITOptimizedReg(true);
  6469. IR::Instr * initInstr = IR::Instr::New(Js::OpCode::InitLoopBodyCount, loopCounterOpnd, this->m_func);
  6470. m_lastInstr->InsertAfter(initInstr);
  6471. m_lastInstr = initInstr;
  6472. initInstr->SetByteCodeOffset(m_jnReader.GetCurrentOffset());
  6473. }
  6474. IR::AddrOpnd *
  6475. IRBuilder::BuildAuxArrayOpnd(AuxArrayValue auxArrayType, uint32 auxArrayOffset)
  6476. {
  6477. switch (auxArrayType)
  6478. {
  6479. case AuxArrayValue::AuxPropertyIdArray:
  6480. case AuxArrayValue::AuxIntArray:
  6481. case AuxArrayValue::AuxFloatArray:
  6482. case AuxArrayValue::AuxVarsArray:
  6483. case AuxArrayValue::AuxFuncInfoArray:
  6484. case AuxArrayValue::AuxVarArrayVarCount:
  6485. {
  6486. IR::AddrOpnd * opnd = IR::AddrOpnd::New(m_func->GetJITFunctionBody()->GetAuxDataAddr(auxArrayOffset), IR::AddrOpndKindDynamicAuxBufferRef, m_func);
  6487. opnd->m_metadata = m_func->GetJITFunctionBody()->ReadFromAuxData(auxArrayOffset);
  6488. return opnd;
  6489. }
  6490. default:
  6491. Assert(UNREACHED);
  6492. return nullptr;
  6493. }
  6494. }
  6495. IR::Opnd *
  6496. IRBuilder::BuildAuxObjectLiteralTypeRefOpnd(int objectId)
  6497. {
  6498. return IR::AddrOpnd::New(m_func->GetJITFunctionBody()->GetObjectLiteralTypeRef(objectId), IR::AddrOpndKindDynamicMisc, this->m_func);
  6499. }
  6500. void
  6501. IRBuilder::DoClosureRegCheck(Js::RegSlot reg)
  6502. {
  6503. if (reg == Js::Constants::NoRegister)
  6504. {
  6505. return;
  6506. }
  6507. if (reg == m_func->GetJITFunctionBody()->GetEnvReg() ||
  6508. reg == m_func->GetJITFunctionBody()->GetLocalClosureReg() ||
  6509. reg == m_func->GetJITFunctionBody()->GetLocalFrameDisplayReg() ||
  6510. reg == m_func->GetJITFunctionBody()->GetParamClosureReg())
  6511. {
  6512. Js::Throw::FatalInternalError();
  6513. }
  6514. }
  6515. Js::RegSlot
  6516. IRBuilder::InnerScopeIndexToRegSlot(uint32 index) const
  6517. {
  6518. if (index >= m_func->GetJITFunctionBody()->GetInnerScopeCount())
  6519. {
  6520. Js::Throw::FatalInternalError();
  6521. }
  6522. Js::RegSlot reg = m_func->GetJITFunctionBody()->GetFirstInnerScopeReg() + index;
  6523. if (reg >= m_func->GetJITFunctionBody()->GetLocalsCount())
  6524. {
  6525. Js::Throw::FatalInternalError();
  6526. }
  6527. return reg;
  6528. }
  6529. bool
  6530. IRBuilder::DoLoadInstructionArrayProfileInfo()
  6531. {
  6532. return !(!this->m_func->HasProfileInfo() ||
  6533. (
  6534. PHASE_OFF(Js::TypedArrayPhase, this->m_func->GetTopFunc()) &&
  6535. PHASE_OFF(Js::ArrayCheckHoistPhase, this->m_func)
  6536. ));
  6537. }
  6538. bool
  6539. IRBuilder::AllowNativeArrayProfileInfo()
  6540. {
  6541. return !((!(m_func->GetTopFunc()->HasTry() && !m_func->GetTopFunc()->DoOptimizeTry()) && m_func->GetWeakFuncRef() && !m_func->HasArrayInfo()) ||
  6542. m_func->IsJitInDebugMode());
  6543. }