IRBuilder.cpp 279 KB

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