IRBuilder.cpp 279 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717
  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. case Js::OpCode::InitConst:
  1677. // Don't use InitConst in the JIT, as some dataflow tracking is missing, and we don't currently optimize for it.
  1678. newOpcode = Js::OpCode::Ld_A;
  1679. break;
  1680. }
  1681. IR::RegOpnd * dstOpnd = this->BuildDstOpnd(R0);
  1682. StackSym * dstSym = dstOpnd->m_sym;
  1683. IR::Instr * instr = nullptr;
  1684. switch (newOpcode)
  1685. {
  1686. case Js::OpCode::Ld_A:
  1687. if (symSrc1->m_builtInIndex != Js::BuiltinFunction::None)
  1688. {
  1689. // Note: don't set dstSym->m_builtInIndex to None here (see Win8 399972)
  1690. dstSym->m_builtInIndex = symSrc1->m_builtInIndex;
  1691. }
  1692. break;
  1693. case Js::OpCode::ProfiledStrictLdThis:
  1694. newOpcode = Js::OpCode::StrictLdThis;
  1695. if (m_func->HasProfileInfo())
  1696. {
  1697. dstOpnd->SetValueType(m_func->GetReadOnlyProfileInfo()->GetThisInfo().valueType);
  1698. }
  1699. if (m_func->DoSimpleJitDynamicProfile())
  1700. {
  1701. IR::JitProfilingInstr* newInstr = IR::JitProfilingInstr::New(Js::OpCode::StrictLdThis, dstOpnd, src1Opnd, m_func);
  1702. instr = newInstr;
  1703. }
  1704. break;
  1705. case Js::OpCode::Delete_A:
  1706. dstOpnd->SetValueType(ValueType::Boolean);
  1707. break;
  1708. case Js::OpCode::BeginSwitch:
  1709. m_switchBuilder.BeginSwitch();
  1710. newOpcode = Js::OpCode::Ld_A;
  1711. break;
  1712. case Js::OpCode::LdArrHead:
  1713. src1Opnd->SetValueType(
  1714. ValueType::GetObject(ObjectType::Array).SetHasNoMissingValues(false).SetArrayTypeId(Js::TypeIds_Array));
  1715. src1Opnd->SetValueTypeFixed();
  1716. break;
  1717. case Js::OpCode::LdInnerFrameDisplayNoParent:
  1718. {
  1719. instr = IR::Instr::New(Js::OpCode::LdInnerFrameDisplay, dstOpnd, src1Opnd, m_func);
  1720. this->AddEnvOpndForInnerFrameDisplay(instr, offset);
  1721. if (dstSym->m_isSingleDef)
  1722. {
  1723. dstSym->m_isNotNumber = true;
  1724. }
  1725. this->AddInstr(instr, offset);
  1726. return;
  1727. }
  1728. case Js::OpCode::Conv_Str:
  1729. dstOpnd->SetValueType(ValueType::String);
  1730. break;
  1731. case Js::OpCode::Yield:
  1732. instr = IR::Instr::New(newOpcode, dstOpnd, src1Opnd, m_func);
  1733. this->AddInstr(instr, offset);
  1734. this->m_lastInstr = instr->ConvertToBailOutInstr(instr, IR::BailOutForGeneratorYield);
  1735. IR::LabelInstr* label = IR::LabelInstr::New(Js::OpCode::Label, m_func);
  1736. label->m_hasNonBranchRef = true;
  1737. this->AddInstr(label, Js::Constants::NoByteCodeOffset);
  1738. this->m_func->AddYieldOffsetResumeLabel(nextOffset, label);
  1739. return;
  1740. }
  1741. if (instr == nullptr)
  1742. {
  1743. instr = IR::Instr::New(newOpcode, dstOpnd, src1Opnd, m_func);
  1744. }
  1745. this->AddInstr(instr, offset);
  1746. }
  1747. ///----------------------------------------------------------------------------
  1748. ///
  1749. /// IRBuilder::BuildProfiledReg2
  1750. ///
  1751. /// Build IR instr for a profiled Reg2 instruction.
  1752. ///
  1753. ///----------------------------------------------------------------------------
  1754. template <typename SizePolicy>
  1755. void
  1756. IRBuilder::BuildProfiledReg2(Js::OpCode newOpcode, uint32 offset)
  1757. {
  1758. Assert(OpCodeAttr::IsProfiledOp(newOpcode));
  1759. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  1760. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_Reg2<SizePolicy>>>();
  1761. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  1762. {
  1763. this->DoClosureRegCheck(layout->R0);
  1764. this->DoClosureRegCheck(layout->R1);
  1765. }
  1766. BuildProfiledReg2(newOpcode, offset, layout->R0, layout->R1, layout->profileId);
  1767. }
  1768. void
  1769. IRBuilder::BuildProfiledReg2(Js::OpCode newOpcode, uint32 offset, Js::RegSlot dstRegSlot, Js::RegSlot srcRegSlot, Js::ProfileId profileId)
  1770. {
  1771. bool switchFound = false;
  1772. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(newOpcode);
  1773. Assert(newOpcode == Js::OpCode::BeginSwitch);
  1774. IR::RegOpnd * src1Opnd = this->BuildSrcOpnd(srcRegSlot);
  1775. IR::RegOpnd * dstOpnd;
  1776. if(srcRegSlot == dstRegSlot)
  1777. {
  1778. //if the operands are the same for BeginSwitch, don't build a new operand in IR.
  1779. dstOpnd = src1Opnd;
  1780. }
  1781. else
  1782. {
  1783. dstOpnd = this->BuildDstOpnd(dstRegSlot);
  1784. }
  1785. m_switchBuilder.BeginSwitch();
  1786. switchFound = true;
  1787. newOpcode = Js::OpCode::Ld_A; // BeginSwitch is originally equivalent to Ld_A
  1788. IR::Instr *instr;
  1789. if (m_func->DoSimpleJitDynamicProfile())
  1790. {
  1791. // Since we're in simplejit, we want to keep track of the profileid:
  1792. IR::JitProfilingInstr *profiledInstr = IR::JitProfilingInstr::New(newOpcode, dstOpnd, src1Opnd, m_func);
  1793. profiledInstr->profileId = profileId;
  1794. profiledInstr->isBeginSwitch = newOpcode == Js::OpCode::Ld_A;
  1795. instr = profiledInstr;
  1796. }
  1797. else
  1798. {
  1799. IR::ProfiledInstr *profiledInstr = IR::ProfiledInstr::New(newOpcode, dstOpnd, src1Opnd, m_func);
  1800. instr = profiledInstr;
  1801. profiledInstr->u.FldInfo() = Js::FldInfo();
  1802. }
  1803. this->AddInstr(instr, offset);
  1804. if(switchFound && instr->IsProfiledInstr())
  1805. {
  1806. m_switchBuilder.SetProfiledInstruction(instr, profileId);
  1807. }
  1808. }
  1809. ///----------------------------------------------------------------------------
  1810. ///
  1811. /// IRBuilder::BuildReg3
  1812. ///
  1813. /// Build IR instr for a Reg3 instruction.
  1814. ///
  1815. ///----------------------------------------------------------------------------
  1816. template <typename SizePolicy>
  1817. void
  1818. IRBuilder::BuildReg3(Js::OpCode newOpcode, uint32 offset)
  1819. {
  1820. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  1821. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  1822. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_Reg3<SizePolicy>>();
  1823. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func) && newOpcode != Js::OpCode::NewInnerScopeSlots)
  1824. {
  1825. this->DoClosureRegCheck(layout->R0);
  1826. this->DoClosureRegCheck(layout->R1);
  1827. this->DoClosureRegCheck(layout->R2);
  1828. }
  1829. BuildReg3(newOpcode, offset, layout->R0, layout->R1, layout->R2, Js::Constants::NoProfileId);
  1830. }
  1831. template <typename SizePolicy>
  1832. void
  1833. IRBuilder::BuildProfiledReg3(Js::OpCode newOpcode, uint32 offset)
  1834. {
  1835. Assert(OpCodeAttr::IsProfiledOp(newOpcode));
  1836. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  1837. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_Reg3<SizePolicy>>>();
  1838. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  1839. {
  1840. this->DoClosureRegCheck(layout->R0);
  1841. this->DoClosureRegCheck(layout->R1);
  1842. this->DoClosureRegCheck(layout->R2);
  1843. }
  1844. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(newOpcode);
  1845. BuildReg3(newOpcode, offset, layout->R0, layout->R1, layout->R2, layout->profileId);
  1846. }
  1847. void
  1848. IRBuilder::BuildReg3(Js::OpCode newOpcode, uint32 offset, Js::RegSlot dstRegSlot, Js::RegSlot src1RegSlot,
  1849. Js::RegSlot src2RegSlot, Js::ProfileId profileId)
  1850. {
  1851. IR::Instr * instr;
  1852. if (newOpcode == Js::OpCode::NewInnerScopeSlots)
  1853. {
  1854. if (dstRegSlot >= m_func->GetJITFunctionBody()->GetInnerScopeCount())
  1855. {
  1856. Js::Throw::FatalInternalError();
  1857. }
  1858. newOpcode = Js::OpCode::NewScopeSlotsWithoutPropIds;
  1859. dstRegSlot += m_func->GetJITFunctionBody()->GetFirstInnerScopeReg();
  1860. instr = IR::Instr::New(newOpcode, BuildDstOpnd(dstRegSlot),
  1861. IR::IntConstOpnd::New(src1RegSlot, TyVar, m_func),
  1862. IR::IntConstOpnd::New(src2RegSlot, TyVar, m_func),
  1863. m_func);
  1864. if (instr->GetDst()->AsRegOpnd()->m_sym->m_isSingleDef)
  1865. {
  1866. instr->GetDst()->AsRegOpnd()->m_sym->m_isNotNumber = true;
  1867. }
  1868. this->AddInstr(instr, offset);
  1869. return;
  1870. }
  1871. IR::RegOpnd * src1Opnd = this->BuildSrcOpnd(src1RegSlot);
  1872. IR::RegOpnd * src2Opnd = this->BuildSrcOpnd(src2RegSlot);
  1873. IR::RegOpnd * dstOpnd = this->BuildDstOpnd(dstRegSlot);
  1874. StackSym * dstSym = dstOpnd->m_sym;
  1875. bool isProfiledInstr = (profileId != Js::Constants::NoProfileId);
  1876. bool wasNotProfiled = false;
  1877. const Js::LdElemInfo * ldElemInfo = nullptr;
  1878. if (isProfiledInstr && newOpcode == Js::OpCode::IsIn)
  1879. {
  1880. if (!DoLoadInstructionArrayProfileInfo())
  1881. {
  1882. isProfiledInstr = false;
  1883. }
  1884. else
  1885. {
  1886. ldElemInfo = this->m_func->GetReadOnlyProfileInfo()->GetLdElemInfo(profileId);
  1887. ValueType arrayType = ldElemInfo->GetArrayType();
  1888. wasNotProfiled = !ldElemInfo->WasProfiled();
  1889. if (arrayType.IsLikelyNativeArray() && !AllowNativeArrayProfileInfo())
  1890. {
  1891. arrayType = arrayType.SetArrayTypeId(Js::TypeIds_Array);
  1892. // An opnd's value type will get replaced in the forward phase when it is not fixed. Store the array type in the ProfiledInstr.
  1893. Js::LdElemInfo *const newLdElemInfo = JitAnew(m_func->m_alloc, Js::LdElemInfo, *ldElemInfo);
  1894. newLdElemInfo->arrayType = arrayType;
  1895. ldElemInfo = newLdElemInfo;
  1896. }
  1897. src2Opnd->SetValueType(arrayType);
  1898. if (m_func->GetTopFunc()->HasTry() && !m_func->GetTopFunc()->DoOptimizeTry())
  1899. {
  1900. isProfiledInstr = false;
  1901. }
  1902. }
  1903. }
  1904. if (isProfiledInstr)
  1905. {
  1906. if (m_func->DoSimpleJitDynamicProfile())
  1907. {
  1908. instr = IR::JitProfilingInstr::New(newOpcode, dstOpnd, src1Opnd, src2Opnd, m_func);
  1909. instr->AsJitProfilingInstr()->profileId = profileId;
  1910. }
  1911. else
  1912. {
  1913. instr = IR::ProfiledInstr::New(newOpcode, dstOpnd, src1Opnd, src2Opnd, m_func);
  1914. if (newOpcode == Js::OpCode::IsIn)
  1915. {
  1916. instr->AsProfiledInstr()->u.ldElemInfo = ldElemInfo;
  1917. }
  1918. else
  1919. {
  1920. instr->AsProfiledInstr()->u.profileId = profileId;
  1921. }
  1922. }
  1923. }
  1924. else
  1925. {
  1926. instr = IR::Instr::New(newOpcode, dstOpnd, src1Opnd, src2Opnd, m_func);
  1927. }
  1928. this->AddInstr(instr, offset);
  1929. if (wasNotProfiled && DoBailOnNoProfile())
  1930. {
  1931. InsertBailOnNoProfile(instr);
  1932. }
  1933. switch (newOpcode)
  1934. {
  1935. case Js::OpCode::LdHandlerScope:
  1936. case Js::OpCode::NewScopeSlotsWithoutPropIds:
  1937. if (dstSym->m_isSingleDef)
  1938. {
  1939. dstSym->m_isNotNumber = true;
  1940. }
  1941. break;
  1942. case Js::OpCode::LdInnerFrameDisplay:
  1943. if (dstSym->m_isSingleDef)
  1944. {
  1945. dstSym->m_isNotNumber = true;
  1946. }
  1947. break;
  1948. }
  1949. }
  1950. ///----------------------------------------------------------------------------
  1951. ///
  1952. /// IRBuilder::BuildReg3C
  1953. ///
  1954. /// Build IR instr for a Reg3C instruction.
  1955. ///
  1956. ///----------------------------------------------------------------------------
  1957. template <typename SizePolicy>
  1958. void
  1959. IRBuilder::BuildReg3C(Js::OpCode newOpcode, uint32 offset)
  1960. {
  1961. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  1962. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  1963. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_Reg3C<SizePolicy>>();
  1964. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  1965. {
  1966. this->DoClosureRegCheck(layout->R0);
  1967. this->DoClosureRegCheck(layout->R1);
  1968. this->DoClosureRegCheck(layout->R2);
  1969. }
  1970. BuildReg3C(newOpcode, offset, layout->R0, layout->R1, layout->R2, layout->inlineCacheIndex);
  1971. }
  1972. void
  1973. IRBuilder::BuildReg3C(Js::OpCode newOpCode, uint32 offset, Js::RegSlot dstRegSlot, Js::RegSlot src1RegSlot,
  1974. Js::RegSlot src2RegSlot, Js::CacheId inlineCacheIndex)
  1975. {
  1976. Assert(OpCodeAttr::HasMultiSizeLayout(newOpCode));
  1977. IR::Instr * instr;
  1978. IR::RegOpnd * src1Opnd = this->BuildSrcOpnd(src1RegSlot);
  1979. IR::RegOpnd * src2Opnd = this->BuildSrcOpnd(src2RegSlot);
  1980. IR::RegOpnd * dstOpnd = this->BuildDstOpnd(dstRegSlot);
  1981. instr = IR::Instr::New(Js::OpCode::ArgOut_A, IR::RegOpnd::New(TyVar, m_func), src2Opnd, m_func);
  1982. this->AddInstr(instr, offset);
  1983. instr = IR::Instr::New(Js::OpCode::ArgOut_A, IR::RegOpnd::New(TyVar, m_func), src1Opnd, instr->GetDst(), m_func);
  1984. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  1985. instr = IR::Instr::New(newOpCode, dstOpnd, IR::IntConstOpnd::New(inlineCacheIndex, TyUint32, m_func), instr->GetDst(), m_func);
  1986. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  1987. }
  1988. ///----------------------------------------------------------------------------
  1989. ///
  1990. /// IRBuilder::BuildReg4
  1991. ///
  1992. /// Build IR instr for a Reg4 instruction.
  1993. ///
  1994. ///----------------------------------------------------------------------------
  1995. template <typename SizePolicy>
  1996. void
  1997. IRBuilder::BuildReg4(Js::OpCode newOpcode, uint32 offset)
  1998. {
  1999. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  2000. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2001. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_Reg4<SizePolicy>>();
  2002. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  2003. {
  2004. this->DoClosureRegCheck(layout->R0);
  2005. this->DoClosureRegCheck(layout->R1);
  2006. this->DoClosureRegCheck(layout->R2);
  2007. this->DoClosureRegCheck(layout->R3);
  2008. }
  2009. BuildReg4(newOpcode, offset, layout->R0, layout->R1, layout->R2, layout->R3);
  2010. }
  2011. void
  2012. IRBuilder::BuildReg4(Js::OpCode newOpcode, uint32 offset, Js::RegSlot dstRegSlot, Js::RegSlot src1RegSlot,
  2013. Js::RegSlot src2RegSlot, Js::RegSlot src3RegSlot)
  2014. {
  2015. IR::Instr * instr;
  2016. Assert(newOpcode == Js::OpCode::Concat3);
  2017. IR::RegOpnd * src1Opnd = this->BuildSrcOpnd(src1RegSlot);
  2018. IR::RegOpnd * src2Opnd = this->BuildSrcOpnd(src2RegSlot);
  2019. IR::RegOpnd * src3Opnd = this->BuildSrcOpnd(src3RegSlot);
  2020. IR::RegOpnd * dstOpnd = this->BuildDstOpnd(dstRegSlot);
  2021. IR::RegOpnd * str1Opnd = InsertConvPrimStr(src1Opnd, offset, true);
  2022. IR::RegOpnd * str2Opnd = InsertConvPrimStr(src2Opnd, Js::Constants::NoByteCodeOffset, true);
  2023. IR::RegOpnd * str3Opnd = InsertConvPrimStr(src3Opnd, Js::Constants::NoByteCodeOffset, true);
  2024. // Need to insert a byte code use for src1/src2 that if ConvPrimStr of the src2/src3 bail out
  2025. // we will restore it.
  2026. bool src1HasByteCodeRegSlot = src1Opnd->m_sym->HasByteCodeRegSlot();
  2027. bool src2HasByteCodeRegSlot = src2Opnd->m_sym->HasByteCodeRegSlot();
  2028. if (src1HasByteCodeRegSlot || src2HasByteCodeRegSlot)
  2029. {
  2030. IR::ByteCodeUsesInstr * byteCodeUse = IR::ByteCodeUsesInstr::New(m_func, Js::Constants::NoByteCodeOffset);
  2031. if (src1HasByteCodeRegSlot)
  2032. {
  2033. byteCodeUse->Set(src1Opnd);
  2034. }
  2035. if (src2HasByteCodeRegSlot)
  2036. {
  2037. byteCodeUse->Set(src2Opnd);
  2038. }
  2039. this->AddInstr(byteCodeUse, Js::Constants::NoByteCodeOffset);
  2040. }
  2041. if (!PHASE_OFF(Js::BackendConcatExprOptPhase, this->m_func))
  2042. {
  2043. IR::RegOpnd* tmpDstOpnd1 = IR::RegOpnd::New(StackSym::New(this->m_func), TyVar, this->m_func);
  2044. IR::RegOpnd* tmpDstOpnd2 = IR::RegOpnd::New(StackSym::New(this->m_func), TyVar, this->m_func);
  2045. IR::RegOpnd* tmpDstOpnd3 = IR::RegOpnd::New(StackSym::New(this->m_func), TyVar, this->m_func);
  2046. instr = IR::Instr::New(Js::OpCode::SetConcatStrMultiItemBE, tmpDstOpnd1, str1Opnd, m_func);
  2047. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  2048. instr = IR::Instr::New(Js::OpCode::SetConcatStrMultiItemBE, tmpDstOpnd2, str2Opnd, tmpDstOpnd1, m_func);
  2049. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  2050. instr = IR::Instr::New(Js::OpCode::SetConcatStrMultiItemBE, tmpDstOpnd3, str3Opnd, tmpDstOpnd2, m_func);
  2051. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  2052. IR::IntConstOpnd * countIntConstOpnd = IR::IntConstOpnd::New(3, TyUint32, m_func, true);
  2053. instr = IR::Instr::New(Js::OpCode::NewConcatStrMultiBE, dstOpnd, countIntConstOpnd, tmpDstOpnd3, m_func);
  2054. dstOpnd->SetValueType(ValueType::String);
  2055. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  2056. }
  2057. else
  2058. {
  2059. instr = IR::Instr::New(Js::OpCode::NewConcatStrMulti, dstOpnd, IR::IntConstOpnd::New(3, TyUint32, m_func, true), m_func);
  2060. dstOpnd->SetValueType(ValueType::String);
  2061. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  2062. instr = IR::Instr::New(Js::OpCode::SetConcatStrMultiItem, IR::IndirOpnd::New(dstOpnd, 0, TyVar, m_func), str1Opnd, m_func);
  2063. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  2064. instr = IR::Instr::New(Js::OpCode::SetConcatStrMultiItem, IR::IndirOpnd::New(dstOpnd, 1, TyVar, m_func), str2Opnd, m_func);
  2065. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  2066. instr = IR::Instr::New(Js::OpCode::SetConcatStrMultiItem, IR::IndirOpnd::New(dstOpnd, 2, TyVar, m_func), str3Opnd, m_func);
  2067. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  2068. }
  2069. }
  2070. IR::RegOpnd *
  2071. IRBuilder::InsertConvPrimStr(IR::RegOpnd * srcOpnd, uint offset, bool forcePreOpBailOutIfNeeded)
  2072. {
  2073. IR::RegOpnd * strOpnd = IR::RegOpnd::New(TyVar, this->m_func);
  2074. IR::Instr * instr = IR::Instr::New(Js::OpCode::Conv_PrimStr, strOpnd, srcOpnd, m_func);
  2075. instr->forcePreOpBailOutIfNeeded = forcePreOpBailOutIfNeeded;
  2076. strOpnd->SetValueType(ValueType::String);
  2077. strOpnd->SetValueTypeFixed();
  2078. this->AddInstr(instr, offset);
  2079. return strOpnd;
  2080. }
  2081. template <typename SizePolicy>
  2082. void
  2083. IRBuilder::BuildReg2B1(Js::OpCode newOpcode, uint32 offset)
  2084. {
  2085. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  2086. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2087. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_Reg2B1<SizePolicy>>();
  2088. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  2089. {
  2090. this->DoClosureRegCheck(layout->R0);
  2091. this->DoClosureRegCheck(layout->R1);
  2092. }
  2093. BuildReg2B1(newOpcode, offset, layout->R0, layout->R1, layout->B2);
  2094. }
  2095. void
  2096. IRBuilder::BuildReg2B1(Js::OpCode newOpcode, uint32 offset, Js::RegSlot dstRegSlot, Js::RegSlot srcRegSlot, byte index)
  2097. {
  2098. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2099. Assert(newOpcode == Js::OpCode::SetConcatStrMultiItem);
  2100. IR::Instr * instr;
  2101. IR::RegOpnd * srcOpnd = this->BuildSrcOpnd(srcRegSlot);
  2102. IR::RegOpnd * dstOpnd = this->BuildDstOpnd(dstRegSlot);
  2103. IR::IndirOpnd * indir1Opnd = IR::IndirOpnd::New(dstOpnd, index, TyVar, m_func);
  2104. dstOpnd->SetValueType(ValueType::String);
  2105. instr = IR::Instr::New(Js::OpCode::SetConcatStrMultiItem, indir1Opnd, InsertConvPrimStr(srcOpnd, offset, true), m_func);
  2106. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  2107. }
  2108. template <typename SizePolicy>
  2109. void
  2110. IRBuilder::BuildReg3B1(Js::OpCode newOpcode, uint32 offset)
  2111. {
  2112. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  2113. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2114. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_Reg3B1<SizePolicy>>();
  2115. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  2116. {
  2117. this->DoClosureRegCheck(layout->R0);
  2118. this->DoClosureRegCheck(layout->R1);
  2119. this->DoClosureRegCheck(layout->R2);
  2120. }
  2121. BuildReg3B1(newOpcode, offset, layout->R0, layout->R1, layout->R2, layout->B3);
  2122. }
  2123. void
  2124. IRBuilder::BuildReg3B1(Js::OpCode newOpcode, uint32 offset, Js::RegSlot dstRegSlot, Js::RegSlot src1RegSlot,
  2125. Js::RegSlot src2RegSlot, uint8 index)
  2126. {
  2127. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2128. IR::Instr * instr;
  2129. IR::RegOpnd * src1Opnd = this->BuildSrcOpnd(src1RegSlot);
  2130. IR::RegOpnd * src2Opnd = this->BuildSrcOpnd(src2RegSlot);
  2131. IR::RegOpnd * dstOpnd = this->BuildDstOpnd(dstRegSlot);
  2132. dstOpnd->SetValueType(ValueType::String);
  2133. IR::Instr * newConcatStrMulti = nullptr;
  2134. switch (newOpcode)
  2135. {
  2136. case Js::OpCode::NewConcatStrMulti:
  2137. newConcatStrMulti = IR::Instr::New(Js::OpCode::NewConcatStrMulti, dstOpnd, IR::IntConstOpnd::New(index, TyUint32, m_func), m_func);
  2138. index = 0;
  2139. break;
  2140. case Js::OpCode::SetConcatStrMultiItem2:
  2141. break;
  2142. default:
  2143. Assert(false);
  2144. };
  2145. IR::IndirOpnd * indir1Opnd = IR::IndirOpnd::New(dstOpnd, index, TyVar, m_func);
  2146. IR::IndirOpnd * indir2Opnd = IR::IndirOpnd::New(dstOpnd, index + 1, TyVar, m_func);
  2147. // Need to do the to str first, as they may have side effects.
  2148. IR::RegOpnd * str1Opnd = InsertConvPrimStr(src1Opnd, offset, true);
  2149. IR::RegOpnd * str2Opnd = InsertConvPrimStr(src2Opnd, Js::Constants::NoByteCodeOffset, true);
  2150. // Need to insert a byte code use for src1 so that if ConvPrimStr of the src2 bail out
  2151. // we will restore it.
  2152. if (src1Opnd->m_sym->HasByteCodeRegSlot())
  2153. {
  2154. IR::ByteCodeUsesInstr * byteCodeUse = IR::ByteCodeUsesInstr::New(m_func, Js::Constants::NoByteCodeOffset);
  2155. byteCodeUse->Set(src1Opnd);
  2156. this->AddInstr(byteCodeUse, Js::Constants::NoByteCodeOffset);
  2157. }
  2158. if (newConcatStrMulti)
  2159. {
  2160. // Allocate the concat str after the ConvToStr
  2161. this->AddInstr(newConcatStrMulti, Js::Constants::NoByteCodeOffset);
  2162. }
  2163. instr = IR::Instr::New(Js::OpCode::SetConcatStrMultiItem, indir1Opnd, str1Opnd, m_func);
  2164. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  2165. instr = IR::Instr::New(Js::OpCode::SetConcatStrMultiItem, indir2Opnd, str2Opnd, m_func);
  2166. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  2167. }
  2168. ///----------------------------------------------------------------------------
  2169. ///
  2170. /// IRBuilder::BuildReg5
  2171. ///
  2172. /// Build IR instr for a Reg5 instruction.
  2173. ///
  2174. ///----------------------------------------------------------------------------
  2175. template <typename SizePolicy>
  2176. void
  2177. IRBuilder::BuildReg5(Js::OpCode newOpcode, uint32 offset)
  2178. {
  2179. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  2180. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2181. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_Reg5<SizePolicy>>();
  2182. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  2183. {
  2184. this->DoClosureRegCheck(layout->R0);
  2185. this->DoClosureRegCheck(layout->R1);
  2186. this->DoClosureRegCheck(layout->R2);
  2187. this->DoClosureRegCheck(layout->R3);
  2188. this->DoClosureRegCheck(layout->R4);
  2189. }
  2190. BuildReg5(newOpcode, offset, layout->R0, layout->R1, layout->R2, layout->R3, layout->R4);
  2191. }
  2192. void
  2193. IRBuilder::BuildReg5(Js::OpCode newOpcode, uint32 offset, Js::RegSlot dstRegSlot, Js::RegSlot src1RegSlot,
  2194. Js::RegSlot src2RegSlot, Js::RegSlot src3RegSlot, Js::RegSlot src4RegSlot)
  2195. {
  2196. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2197. IR::Instr * instr;
  2198. IR::RegOpnd * dstOpnd;
  2199. IR::RegOpnd * src1Opnd;
  2200. IR::RegOpnd * src2Opnd;
  2201. IR::RegOpnd * src3Opnd;
  2202. IR::RegOpnd * src4Opnd;
  2203. // We can't support instructions with more than 2 srcs. Instead create a CallHelper instructions,
  2204. // and pass the srcs as ArgOut_A instructions.
  2205. src1Opnd = this->BuildSrcOpnd(src1RegSlot);
  2206. src2Opnd = this->BuildSrcOpnd(src2RegSlot);
  2207. src3Opnd = this->BuildSrcOpnd(src3RegSlot);
  2208. src4Opnd = this->BuildSrcOpnd(src4RegSlot);
  2209. dstOpnd = this->BuildDstOpnd(dstRegSlot);
  2210. instr = IR::Instr::New(Js::OpCode::ArgOut_A, IR::RegOpnd::New(TyVar, m_func), src4Opnd, m_func);
  2211. this->AddInstr(instr, offset);
  2212. instr = IR::Instr::New(Js::OpCode::ArgOut_A, IR::RegOpnd::New(TyVar, m_func), src3Opnd, instr->GetDst(), m_func);
  2213. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  2214. instr = IR::Instr::New(Js::OpCode::ArgOut_A, IR::RegOpnd::New(TyVar, m_func), src2Opnd, instr->GetDst(), m_func);
  2215. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  2216. instr = IR::Instr::New(Js::OpCode::ArgOut_A, IR::RegOpnd::New(TyVar, m_func), src1Opnd, instr->GetDst(), m_func);
  2217. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  2218. IR::HelperCallOpnd *helperOpnd;
  2219. switch (newOpcode) {
  2220. case Js::OpCode::ApplyArgs:
  2221. helperOpnd=IR::HelperCallOpnd::New(IR::HelperOp_OP_ApplyArgs, this->m_func);
  2222. break;
  2223. default:
  2224. AssertMsg(UNREACHED, "Unknown Reg5 opcode");
  2225. Fatal();
  2226. }
  2227. instr = IR::Instr::New(Js::OpCode::CallHelper, dstOpnd, helperOpnd, instr->GetDst(), m_func);
  2228. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  2229. }
  2230. void
  2231. IRBuilder::BuildW1(Js::OpCode newOpcode, uint32 offset)
  2232. {
  2233. Assert(!OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2234. unsigned short C1;
  2235. const unaligned Js::OpLayoutW1 *regLayout = m_jnReader.W1();
  2236. C1 = regLayout->C1;
  2237. IR::Instr * instr;
  2238. IntConstType value = C1;
  2239. IR::IntConstOpnd * srcOpnd;
  2240. srcOpnd = IR::IntConstOpnd::New(value, TyInt32, m_func);
  2241. instr = IR::Instr::New(newOpcode, m_func);
  2242. instr->SetSrc1(srcOpnd);
  2243. this->AddInstr(instr, offset);
  2244. if (newOpcode == Js::OpCode::RuntimeReferenceError || newOpcode == Js::OpCode::RuntimeTypeError)
  2245. {
  2246. if (DoBailOnNoProfile())
  2247. {
  2248. // RuntimeReferenceError are extremely rare as they are guaranteed to throw. Insert BailonNoProfile to optimize this code path.
  2249. // If there are continues bailout bailonnoprofile will be disabled.
  2250. InsertBailOnNoProfile(instr);
  2251. }
  2252. }
  2253. }
  2254. template <typename SizePolicy>
  2255. void
  2256. IRBuilder::BuildUnsigned1(Js::OpCode newOpcode, uint32 offset)
  2257. {
  2258. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  2259. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2260. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_Unsigned1<SizePolicy>>();
  2261. BuildUnsigned1(newOpcode, offset, layout->C1);
  2262. }
  2263. void
  2264. IRBuilder::BuildUnsigned1(Js::OpCode newOpcode, uint32 offset, uint32 num)
  2265. {
  2266. switch (newOpcode)
  2267. {
  2268. case Js::OpCode::EmitTmpRegCount:
  2269. // Note: EmitTmpRegCount is inserted when debugging, not needed for jit.
  2270. // It's only needed by the debugger to see how many tmp regs are active.
  2271. Assert(m_func->IsJitInDebugMode());
  2272. return;
  2273. case Js::OpCode::NewBlockScope:
  2274. case Js::OpCode::NewPseudoScope:
  2275. {
  2276. if (num >= m_func->GetJITFunctionBody()->GetInnerScopeCount())
  2277. {
  2278. Js::Throw::FatalInternalError();
  2279. }
  2280. Js::RegSlot dstRegSlot = num + m_func->GetJITFunctionBody()->GetFirstInnerScopeReg();
  2281. IR::RegOpnd * dstOpnd = BuildDstOpnd(dstRegSlot);
  2282. IR::Instr * instr = IR::Instr::New(newOpcode, dstOpnd, m_func);
  2283. this->AddInstr(instr, offset);
  2284. if (dstOpnd->m_sym->m_isSingleDef)
  2285. {
  2286. dstOpnd->m_sym->m_isNotNumber = true;
  2287. }
  2288. break;
  2289. }
  2290. case Js::OpCode::CloneInnerScopeSlots:
  2291. case Js::OpCode::CloneBlockScope:
  2292. {
  2293. if (num >= m_func->GetJITFunctionBody()->GetInnerScopeCount())
  2294. {
  2295. Js::Throw::FatalInternalError();
  2296. }
  2297. Js::RegSlot srcRegSlot = num + m_func->GetJITFunctionBody()->GetFirstInnerScopeReg();
  2298. IR::RegOpnd * srcOpnd = BuildSrcOpnd(srcRegSlot);
  2299. IR::Instr * instr = IR::Instr::New(newOpcode, m_func);
  2300. instr->SetSrc1(srcOpnd);
  2301. this->AddInstr(instr, offset);
  2302. break;
  2303. }
  2304. case Js::OpCode::ProfiledLoopBodyStart:
  2305. {
  2306. // This opcode is removed from the IR when we aren't doing Profiling SimpleJit or not jitting loop bodies
  2307. if (m_func->DoSimpleJitDynamicProfile() && m_func->GetJITFunctionBody()->DoJITLoopBody())
  2308. {
  2309. // Attach a register to the dest of this instruction to communicate whether we should bail out (the deciding of this is done in lowering)
  2310. IR::Opnd* fullJitExists = IR::RegOpnd::New(TyUint8, m_func);
  2311. auto start = m_lastInstr;
  2312. if (start->m_opcode == Js::OpCode::InitLoopBodyCount)
  2313. {
  2314. Assert(this->IsLoopBody());
  2315. start = start->m_prev;
  2316. }
  2317. Assert(start->m_opcode == Js::OpCode::ProfiledLoopStart && start->GetDst());
  2318. IR::JitProfilingInstr* instr = IR::JitProfilingInstr::New(Js::OpCode::ProfiledLoopBodyStart, fullJitExists, start->GetDst(), m_func);
  2319. // profileId is used here to represent the loop number
  2320. instr->loopNumber = num;
  2321. this->AddInstr(instr, offset);
  2322. // If fullJitExists isn't 0, bail out so that we can get the fulljitted version
  2323. BailOutInfo * bailOutInfo = JitAnew(m_func->m_alloc, BailOutInfo, instr->GetByteCodeOffset(), m_func);
  2324. IR::BailOutInstr * bailInstr = IR::BailOutInstr::New(Js::OpCode::BailOnNotEqual, IR::BailOnSimpleJitToFullJitLoopBody, bailOutInfo, bailOutInfo->bailOutFunc);
  2325. bailInstr->SetSrc1(fullJitExists);
  2326. bailInstr->SetSrc2(IR::IntConstOpnd::New(0, TyUint8, m_func, true));
  2327. this->AddInstr(bailInstr, offset);
  2328. }
  2329. Js::ImplicitCallFlags flags = Js::ImplicitCall_HasNoInfo;
  2330. Js::LoopFlags loopFlags;
  2331. if (this->m_func->HasProfileInfo())
  2332. {
  2333. flags = m_func->GetReadOnlyProfileInfo()->GetLoopImplicitCallFlags(num);
  2334. loopFlags = m_func->GetReadOnlyProfileInfo()->GetLoopFlags(num);
  2335. }
  2336. // Put a label the instruction stream to carry the profile info
  2337. IR::ProfiledLabelInstr * labelInstr = IR::ProfiledLabelInstr::New(Js::OpCode::Label, this->m_func, flags, loopFlags);
  2338. #if DBG
  2339. labelInstr->loopNum = num;
  2340. #endif
  2341. m_lastInstr->InsertAfter(labelInstr);
  2342. m_lastInstr = labelInstr;
  2343. // Set it to the offset to the start of the loop
  2344. labelInstr->SetByteCodeOffset(m_jnReader.GetCurrentOffset());
  2345. break;
  2346. }
  2347. case Js::OpCode::LoopBodyStart:
  2348. break;
  2349. case Js::OpCode::ProfiledLoopStart:
  2350. {
  2351. AssertOrFailFast(num < m_func->GetJITFunctionBody()->GetLoopCount());
  2352. // If we're in profiling SimpleJit and jitting loop bodies, we need to keep this until lowering.
  2353. if (m_func->DoSimpleJitDynamicProfile() && m_func->GetJITFunctionBody()->DoJITLoopBody())
  2354. {
  2355. // In order for the JIT engine to correctly allocate registers we need to have this set up before lowering.
  2356. // There may be 0 to many LoopEnds, but there will only ever be one LoopStart
  2357. Assert(!this->m_saveLoopImplicitCallFlags[num]);
  2358. const auto ty = Lowerer::GetImplicitCallFlagsType();
  2359. auto saveOpnd = IR::RegOpnd::New(ty, m_func);
  2360. this->m_saveLoopImplicitCallFlags[num] = saveOpnd;
  2361. // Note that we insert this instruction /before/ the actual ProfiledLoopStart opcode. This is because Lowering is backwards
  2362. // and this is just a fake instruction which is only used to pass on the saveOpnd; this instruction will eventually be removed.
  2363. auto instr = IR::JitProfilingInstr::New(Js::OpCode::Ld_A, saveOpnd, IR::MemRefOpnd::New((intptr_t)0, ty, m_func), m_func);
  2364. instr->isLoopHelper = true;
  2365. this->AddInstr(instr, offset);
  2366. instr = IR::JitProfilingInstr::New(Js::OpCode::ProfiledLoopStart, IR::RegOpnd::New(TyMachPtr, m_func), nullptr, m_func);
  2367. instr->loopNumber = num;
  2368. this->AddInstr(instr, offset);
  2369. }
  2370. if (this->IsLoopBody() && !m_loopCounterSym)
  2371. {
  2372. InsertInitLoopBodyLoopCounter(num);
  2373. }
  2374. break;
  2375. }
  2376. case Js::OpCode::ProfiledLoopEnd:
  2377. {
  2378. AssertOrFailFast(num < m_func->GetJITFunctionBody()->GetLoopCount());
  2379. // TODO: Decide whether we want the implicit loop call flags to be recorded in simplejitted loop bodies
  2380. if (m_func->DoSimpleJitDynamicProfile() && m_func->GetJITFunctionBody()->DoJITLoopBody())
  2381. {
  2382. Assert(this->m_saveLoopImplicitCallFlags[num]);
  2383. // In profiling simplejit we need this opcode in order to restore the implicit call flags
  2384. auto instr = IR::JitProfilingInstr::New(Js::OpCode::ProfiledLoopEnd, nullptr, this->m_saveLoopImplicitCallFlags[num], m_func);
  2385. this->AddInstr(instr, offset);
  2386. instr->loopNumber = num;
  2387. }
  2388. if (!this->IsLoopBody())
  2389. {
  2390. break;
  2391. }
  2392. // In the early exit case (return), we generated ProfiledLoopEnd for all the outer loop.
  2393. // If we see one of these profile loop, just load the IP of the immediate outer loop of the loop body being JIT'ed
  2394. // and then skip all the other loops using the fact that we have already loaded the return IP
  2395. if (IsLoopBodyReturnIPInstr(m_lastInstr))
  2396. {
  2397. // Already loaded the loop IP sym, skip
  2398. break;
  2399. }
  2400. // See we are ending an outer loop and load the return IP to the ProfiledLoopEnd opcode
  2401. // instead of following the normal branch
  2402. const JITLoopHeaderIDL * loopHeader = m_func->GetJITFunctionBody()->GetLoopHeaderData(num);
  2403. if (m_func->GetJITFunctionBody()->GetLoopHeaderAddr(num) != m_func->m_workItem->GetLoopHeaderAddr() &&
  2404. JITTimeFunctionBody::LoopContains(loopHeader, m_func->m_workItem->GetLoopHeader()))
  2405. {
  2406. this->InsertLoopBodyReturnIPInstr(offset, offset);
  2407. }
  2408. else
  2409. {
  2410. Assert(JITTimeFunctionBody::LoopContains(m_func->m_workItem->GetLoopHeader(), loopHeader));
  2411. }
  2412. break;
  2413. }
  2414. case Js::OpCode::InvalCachedScope:
  2415. {
  2416. // The reg and constant are both src operands.
  2417. IR::Instr* instr = IR::Instr::New(Js::OpCode::InvalCachedScope, m_func);
  2418. IR::RegOpnd *envOpnd = this->BuildSrcOpnd(m_func->GetJITFunctionBody()->GetEnvReg());
  2419. instr->SetSrc1(envOpnd);
  2420. IR::IntConstOpnd *envIndex = IR::IntConstOpnd::New(num, TyInt32, m_func, true);
  2421. instr->SetSrc2(envIndex);
  2422. this->AddInstr(instr, offset);
  2423. return;
  2424. }
  2425. default:
  2426. Assert(false);
  2427. __assume(false);
  2428. }
  2429. }
  2430. template <typename SizePolicy>
  2431. void
  2432. IRBuilder::BuildProfiledReg1Unsigned1(Js::OpCode newOpcode, uint32 offset)
  2433. {
  2434. Assert(OpCodeAttr::IsProfiledOp(newOpcode));
  2435. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2436. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_Reg1Unsigned1<SizePolicy>>>();
  2437. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  2438. {
  2439. this->DoClosureRegCheck(layout->R0);
  2440. }
  2441. BuildProfiledReg1Unsigned1(newOpcode, offset, layout->R0, layout->C1, layout->profileId);
  2442. }
  2443. void
  2444. IRBuilder::BuildProfiledReg1Unsigned1(Js::OpCode newOpcode, uint32 offset, Js::RegSlot R0, int32 C1, Js::ProfileId profileId)
  2445. {
  2446. Assert(newOpcode == Js::OpCode::ProfiledNewScArray || newOpcode == Js::OpCode::ProfiledInitForInEnumerator);
  2447. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(newOpcode);
  2448. if (newOpcode == Js::OpCode::InitForInEnumerator)
  2449. {
  2450. IR::RegOpnd * src1Opnd = this->BuildSrcOpnd(R0);
  2451. IR::Opnd * src2Opnd = this->BuildForInEnumeratorOpnd(C1);
  2452. IR::Instr *instr = IR::ProfiledInstr::New(Js::OpCode::InitForInEnumerator, nullptr, src1Opnd, src2Opnd, m_func);
  2453. instr->AsProfiledInstr()->u.profileId = profileId;
  2454. this->AddInstr(instr, offset);
  2455. return;
  2456. }
  2457. IR::Instr *instr;
  2458. Js::RegSlot dstRegSlot = R0;
  2459. IR::RegOpnd * dstOpnd = this->BuildDstOpnd(dstRegSlot);
  2460. StackSym * dstSym = dstOpnd->m_sym;
  2461. int32 value = C1;
  2462. IR::IntConstOpnd * srcOpnd;
  2463. srcOpnd = IR::IntConstOpnd::New(value, TyInt32, m_func);
  2464. if (m_func->DoSimpleJitDynamicProfile())
  2465. {
  2466. instr = IR::JitProfilingInstr::New(newOpcode, dstOpnd, srcOpnd, m_func);
  2467. instr->AsJitProfilingInstr()->profileId = profileId;
  2468. }
  2469. else
  2470. {
  2471. instr = IR::ProfiledInstr::New(newOpcode, dstOpnd, srcOpnd, m_func);
  2472. instr->AsProfiledInstr()->u.profileId = profileId;
  2473. }
  2474. this->AddInstr(instr, offset);
  2475. if (dstSym->m_isSingleDef)
  2476. {
  2477. dstSym->m_isSafeThis = true;
  2478. dstSym->m_isNotNumber = true;
  2479. }
  2480. // Undefined values in array literals ([0, undefined, 1]) are treated as missing values in some versions
  2481. Js::ArrayCallSiteInfo *arrayInfo = nullptr;
  2482. if (m_func->HasArrayInfo())
  2483. {
  2484. arrayInfo = m_func->GetReadOnlyProfileInfo()->GetArrayCallSiteInfo(profileId);
  2485. }
  2486. Js::TypeId arrayTypeId = Js::TypeIds_Array;
  2487. if (arrayInfo && !m_func->IsJitInDebugMode() && Js::JavascriptArray::HasInlineHeadSegment(value))
  2488. {
  2489. if (arrayInfo->IsNativeIntArray())
  2490. {
  2491. arrayTypeId = Js::TypeIds_NativeIntArray;
  2492. }
  2493. else if (arrayInfo->IsNativeFloatArray())
  2494. {
  2495. arrayTypeId = Js::TypeIds_NativeFloatArray;
  2496. }
  2497. }
  2498. dstOpnd->SetValueType(ValueType::GetObject(ObjectType::Array).SetHasNoMissingValues(true).SetArrayTypeId(arrayTypeId));
  2499. if (dstOpnd->GetValueType().HasVarElements())
  2500. {
  2501. dstOpnd->SetValueTypeFixed();
  2502. }
  2503. else
  2504. {
  2505. dstOpnd->SetValueType(dstOpnd->GetValueType().ToLikely());
  2506. }
  2507. }
  2508. template <typename SizePolicy>
  2509. void
  2510. IRBuilder::BuildReg1Unsigned1(Js::OpCode newOpcode, uint32 offset)
  2511. {
  2512. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  2513. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2514. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_Reg1Unsigned1<SizePolicy>>();
  2515. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  2516. {
  2517. this->DoClosureRegCheck(layout->R0);
  2518. }
  2519. BuildReg1Unsigned1(newOpcode, offset, layout->R0, layout->C1);
  2520. }
  2521. void
  2522. IRBuilder::BuildReg1Unsigned1(Js::OpCode newOpcode, uint offset, Js::RegSlot R0, int32 C1)
  2523. {
  2524. switch (newOpcode)
  2525. {
  2526. case Js::OpCode::NewRegEx:
  2527. this->BuildRegexFromPattern(R0, C1, offset);
  2528. return;
  2529. case Js::OpCode::LdInnerScope:
  2530. {
  2531. IR::RegOpnd * srcOpnd = BuildSrcOpnd(this->InnerScopeIndexToRegSlot(C1));
  2532. IR::RegOpnd * dstOpnd = BuildDstOpnd(R0);
  2533. IR::Instr * instr = IR::Instr::New(Js::OpCode::Ld_A, dstOpnd, srcOpnd, m_func);
  2534. if (dstOpnd->m_sym->m_isSingleDef)
  2535. {
  2536. dstOpnd->m_sym->m_isNotNumber = true;
  2537. }
  2538. this->AddInstr(instr, offset);
  2539. return;
  2540. }
  2541. case Js::OpCode::LdIndexedFrameDisplayNoParent:
  2542. {
  2543. newOpcode = Js::OpCode::LdFrameDisplay;
  2544. IR::RegOpnd *srcOpnd = this->BuildSrcOpnd(this->InnerScopeIndexToRegSlot(C1));
  2545. IR::RegOpnd *dstOpnd = this->BuildDstOpnd(R0);
  2546. IR::Instr *instr = IR::Instr::New(newOpcode, dstOpnd, srcOpnd, m_func);
  2547. this->AddEnvOpndForInnerFrameDisplay(instr, offset);
  2548. if (dstOpnd->m_sym->m_isSingleDef)
  2549. {
  2550. dstOpnd->m_sym->m_isNotNumber = true;
  2551. }
  2552. this->AddInstr(instr, offset);
  2553. return;
  2554. }
  2555. case Js::OpCode::GetCachedFunc:
  2556. {
  2557. IR::RegOpnd *src1Opnd = this->BuildSrcOpnd(m_func->GetJITFunctionBody()->GetLocalClosureReg());
  2558. IR::Opnd *src2Opnd = IR::IntConstOpnd::New(C1, TyUint32, m_func);
  2559. IR::RegOpnd *dstOpnd = this->BuildDstOpnd(R0);
  2560. IR::Instr *instr = IR::Instr::New(newOpcode, dstOpnd, src1Opnd, src2Opnd, m_func);
  2561. if (dstOpnd->m_sym->m_isSingleDef)
  2562. {
  2563. dstOpnd->m_sym->m_isNotNumber = true;
  2564. }
  2565. this->AddInstr(instr, offset);
  2566. return;
  2567. }
  2568. case Js::OpCode::InitForInEnumerator:
  2569. {
  2570. IR::Instr *instr = IR::Instr::New(Js::OpCode::InitForInEnumerator, m_func);
  2571. instr->SetSrc1(this->BuildSrcOpnd(R0));
  2572. instr->SetSrc2(this->BuildForInEnumeratorOpnd(C1));
  2573. this->AddInstr(instr, offset);
  2574. return;
  2575. }
  2576. }
  2577. IR::RegOpnd * dstOpnd = this->BuildDstOpnd(R0);
  2578. StackSym * dstSym = dstOpnd->m_sym;
  2579. IntConstType value = C1;
  2580. IR::IntConstOpnd * srcOpnd = IR::IntConstOpnd::New(value, TyInt32, m_func);
  2581. IR::Instr * instr = IR::Instr::New(newOpcode, dstOpnd, srcOpnd, m_func);
  2582. this->AddInstr(instr, offset);
  2583. if (newOpcode == Js::OpCode::NewScopeSlots)
  2584. {
  2585. this->AddInstr(
  2586. IR::Instr::New(
  2587. Js::OpCode::Ld_A, IR::RegOpnd::New(m_func->GetLocalClosureSym(), TyVar, m_func), dstOpnd, m_func),
  2588. (uint32)-1);
  2589. }
  2590. if (dstSym->m_isSingleDef)
  2591. {
  2592. switch (newOpcode)
  2593. {
  2594. case Js::OpCode::NewScArray:
  2595. case Js::OpCode::NewScArrayWithMissingValues:
  2596. dstSym->m_isSafeThis = true;
  2597. dstSym->m_isNotNumber = true;
  2598. break;
  2599. }
  2600. }
  2601. if (newOpcode == Js::OpCode::NewScArray || newOpcode == Js::OpCode::NewScArrayWithMissingValues)
  2602. {
  2603. // Undefined values in array literals ([0, undefined, 1]) are treated as missing values in some versions
  2604. dstOpnd->SetValueType(
  2605. ValueType::GetObject(ObjectType::Array)
  2606. .SetHasNoMissingValues(newOpcode == Js::OpCode::NewScArray)
  2607. .SetArrayTypeId(Js::TypeIds_Array));
  2608. dstOpnd->SetValueTypeFixed();
  2609. }
  2610. }
  2611. ///----------------------------------------------------------------------------
  2612. ///
  2613. /// IRBuilder::BuildReg2Int1
  2614. ///
  2615. /// Build IR instr for a Reg2I4 instruction.
  2616. ///
  2617. ///----------------------------------------------------------------------------
  2618. template <typename SizePolicy>
  2619. void
  2620. IRBuilder::BuildReg2Int1(Js::OpCode newOpcode, uint32 offset)
  2621. {
  2622. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2623. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_Reg2Int1<SizePolicy>>();
  2624. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  2625. {
  2626. this->DoClosureRegCheck(layout->R0);
  2627. this->DoClosureRegCheck(layout->R1);
  2628. }
  2629. BuildReg2Int1(newOpcode, offset, layout->R0, layout->R1, layout->C1);
  2630. }
  2631. void
  2632. IRBuilder::BuildReg2Int1(Js::OpCode newOpcode, uint32 offset, Js::RegSlot dstRegSlot, Js::RegSlot srcRegSlot, int32 value)
  2633. {
  2634. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2635. IR::Instr * instr;
  2636. if (newOpcode == Js::OpCode::LdIndexedFrameDisplay)
  2637. {
  2638. newOpcode = Js::OpCode::LdFrameDisplay;
  2639. if ((uint)value >= m_func->GetJITFunctionBody()->GetInnerScopeCount())
  2640. {
  2641. Js::Throw::FatalInternalError();
  2642. }
  2643. IR::RegOpnd *src1Opnd = this->BuildSrcOpnd(value + m_func->GetJITFunctionBody()->GetFirstInnerScopeReg());
  2644. IR::RegOpnd *src2Opnd = this->BuildSrcOpnd(srcRegSlot);
  2645. IR::RegOpnd *dstOpnd = this->BuildDstOpnd(dstRegSlot);
  2646. instr = IR::Instr::New(newOpcode, dstOpnd, src1Opnd, src2Opnd, m_func);
  2647. if (dstOpnd->m_sym->m_isSingleDef)
  2648. {
  2649. dstOpnd->m_sym->m_isNotNumber = true;
  2650. }
  2651. this->AddInstr(instr, offset);
  2652. return;
  2653. }
  2654. IR::RegOpnd * src1Opnd = this->BuildSrcOpnd(srcRegSlot);
  2655. IR::IntConstOpnd * src2Opnd = IR::IntConstOpnd::New(value, TyInt32, m_func);
  2656. IR::RegOpnd * dstOpnd = this->BuildDstOpnd(dstRegSlot);
  2657. switch (newOpcode)
  2658. {
  2659. case Js::OpCode::ProfiledLdThis:
  2660. newOpcode = Js::OpCode::LdThis;
  2661. if(m_func->HasProfileInfo())
  2662. {
  2663. dstOpnd->SetValueType(m_func->GetReadOnlyProfileInfo()->GetThisInfo().valueType);
  2664. }
  2665. if(m_func->DoSimpleJitDynamicProfile())
  2666. {
  2667. instr = IR::JitProfilingInstr::New(newOpcode, dstOpnd, src1Opnd, src2Opnd, m_func);
  2668. // Break out since we just made the instr
  2669. break;
  2670. }
  2671. // fall-through
  2672. default:
  2673. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  2674. instr = IR::Instr::New(newOpcode, dstOpnd, src1Opnd, src2Opnd, m_func);
  2675. break;
  2676. }
  2677. this->AddInstr(instr, offset);
  2678. }
  2679. ///----------------------------------------------------------------------------
  2680. ///
  2681. /// IRBuilder::BuildElementC
  2682. ///
  2683. /// Build IR instr for an ElementC instruction.
  2684. ///
  2685. ///----------------------------------------------------------------------------
  2686. template <typename SizePolicy>
  2687. void
  2688. IRBuilder::BuildElementScopedC(Js::OpCode newOpcode, uint32 offset)
  2689. {
  2690. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  2691. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2692. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementScopedC<SizePolicy>>();
  2693. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  2694. {
  2695. this->DoClosureRegCheck(layout->Value);
  2696. }
  2697. BuildElementScopedC(newOpcode, offset, layout->Value, layout->PropertyIdIndex);
  2698. }
  2699. void
  2700. IRBuilder::BuildElementScopedC(Js::OpCode newOpcode, uint32 offset, Js::RegSlot regSlot, Js::PropertyIdIndexType propertyIdIndex)
  2701. {
  2702. IR::Instr * instr;
  2703. Js::PropertyId propertyId = m_func->GetJITFunctionBody()->GetReferencedPropertyId(propertyIdIndex);
  2704. PropertyKind propertyKind = PropertyKindData;
  2705. IR::RegOpnd * regOpnd;
  2706. Js::RegSlot fieldRegSlot = this->GetEnvRegForEvalCode();
  2707. IR::SymOpnd * fieldSymOpnd = this->BuildFieldOpnd(newOpcode, fieldRegSlot, propertyId, propertyIdIndex, propertyKind);
  2708. switch (newOpcode)
  2709. {
  2710. case Js::OpCode::ScopedEnsureNoRedeclFld:
  2711. {
  2712. regOpnd = this->BuildSrcOpnd(regSlot);
  2713. instr = IR::Instr::New(newOpcode, fieldSymOpnd, regOpnd, m_func);
  2714. break;
  2715. }
  2716. case Js::OpCode::ScopedDeleteFld:
  2717. case Js::OpCode::ScopedDeleteFldStrict:
  2718. {
  2719. Assert(this->m_func->GetScriptContextInfo()->GetAddr() == this->m_func->GetTopFunc()->GetScriptContextInfo()->GetAddr());
  2720. regOpnd = this->BuildDstOpnd(regSlot);
  2721. instr = IR::Instr::New(newOpcode, regOpnd, fieldSymOpnd, m_func);
  2722. break;
  2723. }
  2724. case Js::OpCode::ScopedInitFunc:
  2725. {
  2726. // Implicit root object as default instance
  2727. IR::Opnd * instance2Opnd = this->BuildSrcOpnd(Js::FunctionBody::RootObjectRegSlot);
  2728. regOpnd = this->BuildSrcOpnd(regSlot);
  2729. instr = IR::Instr::New(newOpcode, fieldSymOpnd, regOpnd, instance2Opnd, m_func);
  2730. break;
  2731. }
  2732. default:
  2733. AssertMsg(UNREACHED, "Unknown ElementScopedC opcode");
  2734. Fatal();
  2735. }
  2736. this->AddInstr(instr, offset);
  2737. }
  2738. template <typename SizePolicy>
  2739. void
  2740. IRBuilder::BuildElementC(Js::OpCode newOpcode, uint32 offset)
  2741. {
  2742. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  2743. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2744. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementC<SizePolicy>>();
  2745. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  2746. {
  2747. this->DoClosureRegCheck(layout->Value);
  2748. this->DoClosureRegCheck(layout->Instance);
  2749. }
  2750. BuildElementC(newOpcode, offset, layout->Instance, layout->Value, layout->PropertyIdIndex);
  2751. }
  2752. void
  2753. IRBuilder::BuildElementC(Js::OpCode newOpcode, uint32 offset, Js::RegSlot fieldRegSlot, Js::RegSlot regSlot, Js::PropertyIdIndexType propertyIdIndex)
  2754. {
  2755. IR::Instr * instr;
  2756. Js::PropertyId propertyId = m_func->GetJITFunctionBody()->GetReferencedPropertyId(propertyIdIndex);
  2757. PropertyKind propertyKind = PropertyKindData;
  2758. IR::SymOpnd * fieldSymOpnd = this->BuildFieldOpnd(newOpcode, fieldRegSlot, propertyId, propertyIdIndex, propertyKind);
  2759. IR::RegOpnd * regOpnd;
  2760. switch (newOpcode)
  2761. {
  2762. case Js::OpCode::DeleteFld:
  2763. case Js::OpCode::DeleteRootFld:
  2764. case Js::OpCode::DeleteFldStrict:
  2765. case Js::OpCode::DeleteRootFldStrict:
  2766. // Load
  2767. regOpnd = this->BuildDstOpnd(regSlot);
  2768. instr = IR::Instr::New(newOpcode, regOpnd, fieldSymOpnd, m_func);
  2769. break;
  2770. case Js::OpCode::InitSetFld:
  2771. case Js::OpCode::InitGetFld:
  2772. case Js::OpCode::InitClassMemberGet:
  2773. case Js::OpCode::InitClassMemberSet:
  2774. case Js::OpCode::InitProto:
  2775. case Js::OpCode::StFuncExpr:
  2776. // Store
  2777. regOpnd = this->BuildSrcOpnd(regSlot);
  2778. instr = IR::Instr::New(newOpcode, fieldSymOpnd, regOpnd, m_func);
  2779. break;
  2780. default:
  2781. AssertMsg(UNREACHED, "Unknown ElementC opcode");
  2782. Fatal();
  2783. }
  2784. this->AddInstr(instr, offset);
  2785. }
  2786. ///----------------------------------------------------------------------------
  2787. ///
  2788. /// IRBuilder::BuildElementSlot
  2789. ///
  2790. /// Build IR instr for an ElementSlot instruction.
  2791. ///
  2792. ///----------------------------------------------------------------------------
  2793. IR::Instr *
  2794. IRBuilder::BuildProfiledSlotLoad(Js::OpCode loadOp, IR::RegOpnd *dstOpnd, IR::SymOpnd *srcOpnd, Js::ProfileId profileId, bool *pUnprofiled)
  2795. {
  2796. IR::Instr * instr = nullptr;
  2797. if (m_func->DoSimpleJitDynamicProfile())
  2798. {
  2799. instr = IR::JitProfilingInstr::New(loadOp, dstOpnd, srcOpnd, m_func);
  2800. instr->AsJitProfilingInstr()->profileId = profileId;
  2801. }
  2802. else if(this->m_func->HasProfileInfo())
  2803. {
  2804. instr = IR::ProfiledInstr::New(loadOp, dstOpnd, srcOpnd, m_func);
  2805. instr->AsProfiledInstr()->u.FldInfo().valueType =
  2806. this->m_func->GetReadOnlyProfileInfo()->GetSlotLoad(profileId);
  2807. *pUnprofiled = instr->AsProfiledInstr()->u.FldInfo().valueType.IsUninitialized();
  2808. #if ENABLE_DEBUG_CONFIG_OPTIONS
  2809. if(Js::Configuration::Global.flags.TestTrace.IsEnabled(Js::DynamicProfilePhase))
  2810. {
  2811. const ValueType valueType(instr->AsProfiledInstr()->u.FldInfo().valueType);
  2812. char valueTypeStr[VALUE_TYPE_MAX_STRING_SIZE];
  2813. valueType.ToString(valueTypeStr);
  2814. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  2815. Output::Print(_u("TestTrace function %s (#%s) ValueType = %S "), m_func->GetJITFunctionBody()->GetDisplayName(), m_func->GetDebugNumberSet(debugStringBuffer), valueTypeStr);
  2816. instr->DumpTestTrace();
  2817. }
  2818. #endif
  2819. }
  2820. return instr;
  2821. }
  2822. template <typename SizePolicy>
  2823. void
  2824. IRBuilder::BuildElementSlot(Js::OpCode newOpcode, uint32 offset)
  2825. {
  2826. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  2827. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2828. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementSlot<SizePolicy>>();
  2829. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  2830. {
  2831. this->DoClosureRegCheck(layout->Value);
  2832. this->DoClosureRegCheck(layout->Instance);
  2833. }
  2834. BuildElementSlot(newOpcode, offset, layout->Instance, layout->Value, layout->SlotIndex, Js::Constants::NoProfileId);
  2835. }
  2836. template <typename SizePolicy>
  2837. void
  2838. IRBuilder::BuildProfiledElementSlot(Js::OpCode newOpcode, uint32 offset)
  2839. {
  2840. Assert(OpCodeAttr::IsProfiledOp(newOpcode));
  2841. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2842. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_ElementSlot<SizePolicy>>>();
  2843. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  2844. {
  2845. this->DoClosureRegCheck(layout->Value);
  2846. this->DoClosureRegCheck(layout->Instance);
  2847. }
  2848. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(newOpcode);
  2849. BuildElementSlot(newOpcode, offset, layout->Instance, layout->Value, layout->SlotIndex, layout->profileId);
  2850. }
  2851. void
  2852. IRBuilder::BuildElementSlot(Js::OpCode newOpcode, uint32 offset, Js::RegSlot fieldRegSlot, Js::RegSlot regSlot,
  2853. int32 slotId, Js::ProfileId profileId)
  2854. {
  2855. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2856. IR::Instr * instr;
  2857. IR::RegOpnd * regOpnd;
  2858. IR::SymOpnd * fieldSymOpnd;
  2859. PropertyKind propertyKind = PropertyKindSlots;
  2860. PropertySym * fieldSym;
  2861. StackSym * stackFuncPtrSym = nullptr;
  2862. bool isLdSlotThatWasNotProfiled = false;
  2863. switch (newOpcode)
  2864. {
  2865. case Js::OpCode::NewInnerStackScFunc:
  2866. stackFuncPtrSym = this->EnsureStackFuncPtrSym();
  2867. // fall through
  2868. case Js::OpCode::NewInnerScFunc:
  2869. newOpcode = Js::OpCode::NewScFunc;
  2870. goto NewScFuncCommon;
  2871. case Js::OpCode::NewInnerScGenFunc:
  2872. newOpcode = Js::OpCode::NewScGenFunc;
  2873. NewScFuncCommon:
  2874. {
  2875. IR::Opnd * functionBodySlotOpnd = IR::IntConstOpnd::New(slotId, TyInt32, m_func, true);
  2876. IR::Opnd * environmentOpnd = this->BuildSrcOpnd(fieldRegSlot);
  2877. regOpnd = this->BuildDstOpnd(regSlot);
  2878. if (stackFuncPtrSym)
  2879. {
  2880. IR::RegOpnd * dataOpnd = IR::RegOpnd::New(TyVar, m_func);
  2881. instr = IR::Instr::New(Js::OpCode::NewScFuncData, dataOpnd, environmentOpnd, IR::RegOpnd::New(stackFuncPtrSym, TyVar, m_func), m_func);
  2882. this->AddInstr(instr, offset);
  2883. instr = IR::Instr::New(newOpcode, regOpnd, functionBodySlotOpnd, dataOpnd, m_func);
  2884. }
  2885. else
  2886. {
  2887. instr = IR::Instr::New(newOpcode, regOpnd, functionBodySlotOpnd, environmentOpnd, m_func);
  2888. }
  2889. if (regOpnd->m_sym->m_isSingleDef)
  2890. {
  2891. regOpnd->m_sym->m_isSafeThis = true;
  2892. regOpnd->m_sym->m_isNotNumber = true;
  2893. }
  2894. this->AddInstr(instr, offset);
  2895. return;
  2896. }
  2897. case Js::OpCode::NewScFuncHomeObj:
  2898. case Js::OpCode::NewScGenFuncHomeObj:
  2899. {
  2900. Js::FunctionInfoPtrPtr infoRef = m_func->GetJITFunctionBody()->GetNestedFuncRef(slotId);
  2901. IR::AddrOpnd * functionBodySlotOpnd = IR::AddrOpnd::New((Js::Var)infoRef, IR::AddrOpndKindDynamicMisc, m_func);
  2902. IR::Opnd * environmentOpnd = GetEnvironmentOperand(offset);
  2903. IR::Opnd * homeObjOpnd = this->BuildSrcOpnd(fieldRegSlot);
  2904. regOpnd = this->BuildDstOpnd(regSlot);
  2905. instr = IR::Instr::New(Js::OpCode::ExtendArg_A, IR::RegOpnd::New(TyVar, m_func), homeObjOpnd, m_func);
  2906. this->AddInstr(instr, offset);
  2907. instr = IR::Instr::New(Js::OpCode::ExtendArg_A, IR::RegOpnd::New(TyVar, m_func), functionBodySlotOpnd, instr->GetDst(), m_func);
  2908. this->AddInstr(instr, offset);
  2909. instr = IR::Instr::New(Js::OpCode::ExtendArg_A, IR::RegOpnd::New(TyVar, m_func), environmentOpnd, instr->GetDst(), m_func);
  2910. this->AddInstr(instr, offset);
  2911. instr = IR::Instr::New(newOpcode, regOpnd, instr->GetDst(), m_func);
  2912. if (regOpnd->m_sym->m_isSingleDef)
  2913. {
  2914. regOpnd->m_sym->m_isSafeThis = true;
  2915. regOpnd->m_sym->m_isNotNumber = true;
  2916. }
  2917. this->AddInstr(instr, offset);
  2918. return;
  2919. }
  2920. case Js::OpCode::LdObjSlot:
  2921. newOpcode = Js::OpCode::LdSlot;
  2922. goto ObjSlotCommon;
  2923. case Js::OpCode::StObjSlot:
  2924. newOpcode = Js::OpCode::StSlot;
  2925. goto ObjSlotCommon;
  2926. case Js::OpCode::StObjSlotChkUndecl:
  2927. newOpcode = Js::OpCode::StSlotChkUndecl;
  2928. ObjSlotCommon:
  2929. regOpnd = IR::RegOpnd::New(TyVar, m_func);
  2930. fieldSymOpnd = this->BuildFieldOpnd(newOpcode, fieldRegSlot, (Js::DynamicObject::GetOffsetOfAuxSlots())/sizeof(Js::Var), (Js::PropertyIdIndexType)-1, PropertyKindSlotArray);
  2931. instr = IR::Instr::New(Js::OpCode::LdSlotArr, regOpnd, fieldSymOpnd, m_func);
  2932. this->AddInstr(instr, offset);
  2933. fieldSym = PropertySym::New(regOpnd->m_sym, slotId, (uint32)-1, (uint)-1, PropertyKindSlots, m_func);
  2934. fieldSymOpnd = IR::SymOpnd::New(fieldSym, TyVar, m_func);
  2935. if (newOpcode == Js::OpCode::StSlot || newOpcode == Js::OpCode::StSlotChkUndecl)
  2936. {
  2937. goto StSlotCommon;
  2938. }
  2939. goto LdSlotCommon;
  2940. case Js::OpCode::LdSlotArr:
  2941. propertyKind = PropertyKindSlotArray;
  2942. case Js::OpCode::LdSlot:
  2943. // Load
  2944. fieldSymOpnd = this->BuildFieldOpnd(newOpcode, fieldRegSlot, slotId, (Js::PropertyIdIndexType)-1, propertyKind);
  2945. LdSlotCommon:
  2946. regOpnd = this->BuildDstOpnd(regSlot);
  2947. instr = nullptr;
  2948. if (profileId != Js::Constants::NoProfileId)
  2949. {
  2950. instr = this->BuildProfiledSlotLoad(newOpcode, regOpnd, fieldSymOpnd, profileId, &isLdSlotThatWasNotProfiled);
  2951. }
  2952. if (!instr)
  2953. {
  2954. instr = IR::Instr::New(newOpcode, regOpnd, fieldSymOpnd, m_func);
  2955. }
  2956. break;
  2957. case Js::OpCode::StSlot:
  2958. case Js::OpCode::StSlotChkUndecl:
  2959. // Store
  2960. fieldSymOpnd = this->BuildFieldOpnd(newOpcode, fieldRegSlot, slotId, (Js::PropertyIdIndexType)-1, propertyKind);
  2961. StSlotCommon:
  2962. regOpnd = this->BuildSrcOpnd(regSlot);
  2963. instr = IR::Instr::New(newOpcode, fieldSymOpnd, regOpnd, m_func);
  2964. if (newOpcode == Js::OpCode::StSlotChkUndecl)
  2965. {
  2966. // ChkUndecl includes an implicit read of the destination. Communicate the liveness by using the destination in src2.
  2967. instr->SetSrc2(fieldSymOpnd);
  2968. }
  2969. break;
  2970. default:
  2971. AssertMsg(UNREACHED, "Unknown ElementSlot opcode");
  2972. Fatal();
  2973. }
  2974. this->AddInstr(instr, offset);
  2975. if(isLdSlotThatWasNotProfiled && DoBailOnNoProfile())
  2976. {
  2977. InsertBailOnNoProfile(instr);
  2978. }
  2979. }
  2980. template <typename SizePolicy>
  2981. void
  2982. IRBuilder::BuildElementSlotI1(Js::OpCode newOpcode, uint32 offset)
  2983. {
  2984. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  2985. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2986. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementSlotI1<SizePolicy>>();
  2987. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  2988. {
  2989. this->DoClosureRegCheck(layout->Value);
  2990. }
  2991. BuildElementSlotI1(newOpcode, offset, layout->Value, layout->SlotIndex, Js::Constants::NoProfileId);
  2992. }
  2993. template <typename SizePolicy>
  2994. void
  2995. IRBuilder::BuildProfiledElementSlotI1(Js::OpCode newOpcode, uint32 offset)
  2996. {
  2997. Assert(OpCodeAttr::IsProfiledOp(newOpcode));
  2998. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  2999. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_ElementSlotI1<SizePolicy>>>();
  3000. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  3001. {
  3002. this->DoClosureRegCheck(layout->Value);
  3003. }
  3004. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(newOpcode);
  3005. BuildElementSlotI1(newOpcode, offset, layout->Value, layout->SlotIndex, layout->profileId);
  3006. }
  3007. void
  3008. IRBuilder::BuildElementSlotI1(Js::OpCode newOpcode, uint32 offset, Js::RegSlot regSlot,
  3009. int32 slotId, Js::ProfileId profileId)
  3010. {
  3011. IR::RegOpnd *regOpnd;
  3012. IR::SymOpnd *fieldOpnd;
  3013. IR::Instr *instr = nullptr;
  3014. IR::ByteCodeUsesInstr *byteCodeUse;
  3015. PropertySym *fieldSym = nullptr;
  3016. StackSym * stackFuncPtrSym = nullptr;
  3017. SymID symID = m_func->GetJITFunctionBody()->GetLocalClosureReg();
  3018. bool isLdSlotThatWasNotProfiled = false;
  3019. StackSym* closureSym = m_func->GetLocalClosureSym();
  3020. uint scopeSlotSize = this->IsParamScopeDone() ? m_func->GetJITFunctionBody()->GetScopeSlotArraySize() : m_func->GetJITFunctionBody()->GetParamScopeSlotArraySize();
  3021. switch (newOpcode)
  3022. {
  3023. case Js::OpCode::LdParamSlot:
  3024. scopeSlotSize = m_func->GetJITFunctionBody()->GetParamScopeSlotArraySize();
  3025. closureSym = m_func->GetParamClosureSym();
  3026. symID = m_func->GetJITFunctionBody()->GetParamClosureReg();
  3027. // Fall through
  3028. case Js::OpCode::LdLocalSlot:
  3029. if (!PHASE_OFF(Js::ClosureRangeCheckPhase, m_func))
  3030. {
  3031. if ((uint32)slotId >= scopeSlotSize + Js::ScopeSlots::FirstSlotIndex)
  3032. {
  3033. Js::Throw::FatalInternalError();
  3034. }
  3035. }
  3036. if (closureSym->HasByteCodeRegSlot())
  3037. {
  3038. byteCodeUse = IR::ByteCodeUsesInstr::New(m_func, offset);
  3039. byteCodeUse->SetNonOpndSymbol(closureSym->m_id);
  3040. this->AddInstr(byteCodeUse, offset);
  3041. }
  3042. // Read the scope slot pointer back using the stack closure sym.
  3043. newOpcode = Js::OpCode::LdSlot;
  3044. if (m_func->DoStackFrameDisplay())
  3045. {
  3046. // Read the scope slot pointer back using the stack closure sym.
  3047. fieldOpnd = this->BuildFieldOpnd(Js::OpCode::LdSlotArr, closureSym->m_id, 0, (Js::PropertyIdIndexType)-1, PropertyKindSlotArray);
  3048. regOpnd = IR::RegOpnd::New(TyVar, m_func);
  3049. instr = IR::Instr::New(Js::OpCode::LdSlotArr, regOpnd, fieldOpnd, m_func);
  3050. this->AddInstr(instr, offset);
  3051. symID = regOpnd->m_sym->m_id;
  3052. if (IsLoopBody())
  3053. {
  3054. fieldOpnd = this->BuildFieldOpnd(Js::OpCode::LdSlotArr, closureSym->m_id, slotId, (Js::PropertyIdIndexType)-1, PropertyKindSlotArray);
  3055. // Need a dynamic check on the size of the local slot array.
  3056. m_func->GetTopFunc()->AddSlotArrayCheck(fieldOpnd);
  3057. }
  3058. }
  3059. else if (IsLoopBody())
  3060. {
  3061. this->EnsureLoopBodyLoadSlot(symID);
  3062. }
  3063. fieldSym = PropertySym::FindOrCreate(symID, slotId, (uint32)-1, (uint)-1, PropertyKindSlots, m_func);
  3064. fieldOpnd = IR::SymOpnd::New(fieldSym, TyVar, m_func);
  3065. regOpnd = this->BuildDstOpnd(regSlot);
  3066. instr = nullptr;
  3067. if (profileId != Js::Constants::NoProfileId)
  3068. {
  3069. instr = this->BuildProfiledSlotLoad(Js::OpCode::LdSlot, regOpnd, fieldOpnd, profileId, &isLdSlotThatWasNotProfiled);
  3070. }
  3071. if (!instr)
  3072. {
  3073. instr = IR::Instr::New(Js::OpCode::LdSlot, regOpnd, fieldOpnd, m_func);
  3074. }
  3075. this->AddInstr(instr, offset);
  3076. if (!m_func->DoStackFrameDisplay() && IsLoopBody())
  3077. {
  3078. // Need a dynamic check on the size of the local slot array.
  3079. m_func->GetTopFunc()->AddSlotArrayCheck(fieldOpnd);
  3080. }
  3081. break;
  3082. case Js::OpCode::LdParamObjSlot:
  3083. closureSym = m_func->GetParamClosureSym();
  3084. symID = m_func->GetJITFunctionBody()->GetParamClosureReg();
  3085. newOpcode = Js::OpCode::LdLocalObjSlot;
  3086. // Fall through
  3087. case Js::OpCode::LdLocalObjSlot:
  3088. if (closureSym->HasByteCodeRegSlot())
  3089. {
  3090. byteCodeUse = IR::ByteCodeUsesInstr::New(m_func, offset);
  3091. byteCodeUse->SetNonOpndSymbol(closureSym->m_id);
  3092. this->AddInstr(byteCodeUse, offset);
  3093. }
  3094. fieldOpnd = this->BuildFieldOpnd(newOpcode, symID, (Js::DynamicObject::GetOffsetOfAuxSlots()) / sizeof(Js::Var), (Js::PropertyIdIndexType) - 1, PropertyKindSlotArray);
  3095. regOpnd = IR::RegOpnd::New(TyVar, m_func);
  3096. instr = IR::Instr::New(Js::OpCode::LdSlotArr, regOpnd, fieldOpnd, m_func);
  3097. this->AddInstr(instr, offset);
  3098. fieldSym = PropertySym::New(regOpnd->m_sym, slotId, (uint32)-1, (uint)-1, PropertyKindSlots, m_func);
  3099. fieldOpnd = IR::SymOpnd::New(fieldSym, TyVar, m_func);
  3100. regOpnd = this->BuildDstOpnd(regSlot);
  3101. instr = nullptr;
  3102. newOpcode = Js::OpCode::LdSlot;
  3103. if (profileId != Js::Constants::NoProfileId)
  3104. {
  3105. instr = this->BuildProfiledSlotLoad(newOpcode, regOpnd, fieldOpnd, profileId, &isLdSlotThatWasNotProfiled);
  3106. }
  3107. if (!instr)
  3108. {
  3109. instr = IR::Instr::New(newOpcode, regOpnd, fieldOpnd, m_func);
  3110. }
  3111. this->AddInstr(instr, offset);
  3112. break;
  3113. case Js::OpCode::StParamSlot:
  3114. case Js::OpCode::StParamSlotChkUndecl:
  3115. scopeSlotSize = m_func->GetJITFunctionBody()->GetParamScopeSlotArraySize();
  3116. closureSym = m_func->GetParamClosureSym();
  3117. symID = m_func->GetJITFunctionBody()->GetParamClosureReg();
  3118. newOpcode = newOpcode == Js::OpCode::StParamSlot ? Js::OpCode::StLocalSlot : Js::OpCode::StLocalSlotChkUndecl;
  3119. // Fall through
  3120. case Js::OpCode::StLocalSlot:
  3121. case Js::OpCode::StLocalSlotChkUndecl:
  3122. if (!PHASE_OFF(Js::ClosureRangeCheckPhase, m_func))
  3123. {
  3124. if ((uint32)slotId >= scopeSlotSize + Js::ScopeSlots::FirstSlotIndex)
  3125. {
  3126. Js::Throw::FatalInternalError();
  3127. }
  3128. }
  3129. if (closureSym->HasByteCodeRegSlot())
  3130. {
  3131. byteCodeUse = IR::ByteCodeUsesInstr::New(m_func, offset);
  3132. byteCodeUse->SetNonOpndSymbol(closureSym->m_id);
  3133. this->AddInstr(byteCodeUse, offset);
  3134. }
  3135. newOpcode = newOpcode == Js::OpCode::StLocalSlot ? Js::OpCode::StSlot : Js::OpCode::StSlotChkUndecl;
  3136. if (m_func->DoStackFrameDisplay())
  3137. {
  3138. regOpnd = IR::RegOpnd::New(TyVar, m_func);
  3139. // Read the scope slot pointer back using the stack closure sym.
  3140. fieldOpnd = this->BuildFieldOpnd(Js::OpCode::LdSlotArr, closureSym->m_id, 0, (Js::PropertyIdIndexType)-1, PropertyKindSlotArray);
  3141. instr = IR::Instr::New(Js::OpCode::LdSlotArr, regOpnd, fieldOpnd, m_func);
  3142. this->AddInstr(instr, offset);
  3143. symID = regOpnd->m_sym->m_id;
  3144. if (IsLoopBody())
  3145. {
  3146. fieldOpnd = this->BuildFieldOpnd(Js::OpCode::LdSlotArr, closureSym->m_id, slotId, (Js::PropertyIdIndexType)-1, PropertyKindSlotArray);
  3147. // Need a dynamic check on the size of the local slot array.
  3148. m_func->GetTopFunc()->AddSlotArrayCheck(fieldOpnd);
  3149. }
  3150. }
  3151. else
  3152. {
  3153. if (IsLoopBody())
  3154. {
  3155. this->EnsureLoopBodyLoadSlot(symID);
  3156. }
  3157. }
  3158. fieldSym = PropertySym::FindOrCreate(symID, slotId, (uint32)-1, (uint)-1, PropertyKindSlots, m_func);
  3159. fieldOpnd = IR::SymOpnd::New(fieldSym, TyVar, m_func);
  3160. regOpnd = this->BuildSrcOpnd(regSlot);
  3161. instr = IR::Instr::New(newOpcode, fieldOpnd, regOpnd, m_func);
  3162. this->AddInstr(instr, offset);
  3163. if (newOpcode == Js::OpCode::StSlotChkUndecl)
  3164. {
  3165. instr->SetSrc2(fieldOpnd);
  3166. }
  3167. if (!m_func->DoStackFrameDisplay() && IsLoopBody())
  3168. {
  3169. // Need a dynamic check on the size of the local slot array.
  3170. m_func->GetTopFunc()->AddSlotArrayCheck(fieldOpnd);
  3171. }
  3172. break;
  3173. case Js::OpCode::StParamObjSlot:
  3174. case Js::OpCode::StParamObjSlotChkUndecl:
  3175. closureSym = m_func->GetParamClosureSym();
  3176. symID = m_func->GetJITFunctionBody()->GetParamClosureReg();
  3177. newOpcode = newOpcode == Js::OpCode::StParamObjSlot ? Js::OpCode::StLocalObjSlot : Js::OpCode::StLocalObjSlotChkUndecl;
  3178. // Fall through
  3179. case Js::OpCode::StLocalObjSlot:
  3180. case Js::OpCode::StLocalObjSlotChkUndecl:
  3181. if (closureSym->HasByteCodeRegSlot())
  3182. {
  3183. byteCodeUse = IR::ByteCodeUsesInstr::New(m_func, offset);
  3184. byteCodeUse->SetNonOpndSymbol(closureSym->m_id);
  3185. this->AddInstr(byteCodeUse, offset);
  3186. }
  3187. regOpnd = IR::RegOpnd::New(TyVar, m_func);
  3188. fieldOpnd = this->BuildFieldOpnd(Js::OpCode::LdSlotArr, symID, (Js::DynamicObject::GetOffsetOfAuxSlots())/sizeof(Js::Var), (Js::PropertyIdIndexType)-1, PropertyKindSlotArray);
  3189. instr = IR::Instr::New(Js::OpCode::LdSlotArr, regOpnd, fieldOpnd, m_func);
  3190. this->AddInstr(instr, offset);
  3191. newOpcode = newOpcode == Js::OpCode::StLocalObjSlot ? Js::OpCode::StSlot : Js::OpCode::StSlotChkUndecl;
  3192. fieldSym = PropertySym::New(regOpnd->m_sym, slotId, (uint32)-1, (uint)-1, PropertyKindSlots, m_func);
  3193. fieldOpnd = IR::SymOpnd::New(fieldSym, TyVar, m_func);
  3194. regOpnd = this->BuildSrcOpnd(regSlot);
  3195. instr = IR::Instr::New(newOpcode, fieldOpnd, regOpnd, m_func);
  3196. if (newOpcode == Js::OpCode::StSlotChkUndecl)
  3197. {
  3198. // ChkUndecl includes an implicit read of the destination. Communicate the liveness by using the destination in src2.
  3199. instr->SetSrc2(fieldOpnd);
  3200. }
  3201. this->AddInstr(instr, offset);
  3202. break;
  3203. case Js::OpCode::LdEnvObj:
  3204. fieldOpnd = this->BuildFieldOpnd(Js::OpCode::LdSlotArr, this->GetEnvReg(), slotId, (Js::PropertyIdIndexType)-1, PropertyKindSlotArray);
  3205. regOpnd = this->BuildDstOpnd(regSlot);
  3206. instr = IR::Instr::New(Js::OpCode::LdSlotArr, regOpnd, fieldOpnd, m_func);
  3207. this->AddInstr(instr, offset);
  3208. m_func->GetTopFunc()->AddFrameDisplayCheck(fieldOpnd);
  3209. break;
  3210. case Js::OpCode::NewStackScFunc:
  3211. stackFuncPtrSym = this->EnsureStackFuncPtrSym();
  3212. newOpcode = Js::OpCode::NewScFunc;
  3213. // fall through
  3214. case Js::OpCode::NewScFunc:
  3215. goto NewScFuncCommon;
  3216. case Js::OpCode::NewScGenFunc:
  3217. newOpcode = Js::OpCode::NewScGenFunc;
  3218. NewScFuncCommon:
  3219. {
  3220. IR::Opnd * functionBodySlotOpnd = IR::IntConstOpnd::New(slotId, TyInt32, m_func, true);
  3221. IR::Opnd *environmentOpnd = GetEnvironmentOperand(offset);
  3222. regOpnd = this->BuildDstOpnd(regSlot);
  3223. if (stackFuncPtrSym)
  3224. {
  3225. IR::RegOpnd * dataOpnd = IR::RegOpnd::New(TyVar, m_func);
  3226. instr = IR::Instr::New(Js::OpCode::NewScFuncData, dataOpnd, environmentOpnd,
  3227. IR::RegOpnd::New(stackFuncPtrSym, TyVar, m_func), m_func);
  3228. this->AddInstr(instr, offset);
  3229. instr = IR::Instr::New(newOpcode, regOpnd, functionBodySlotOpnd, dataOpnd, m_func);
  3230. }
  3231. else
  3232. {
  3233. instr = IR::Instr::New(newOpcode, regOpnd, functionBodySlotOpnd, environmentOpnd, m_func);
  3234. }
  3235. if (regOpnd->m_sym->m_isSingleDef)
  3236. {
  3237. regOpnd->m_sym->m_isSafeThis = true;
  3238. regOpnd->m_sym->m_isNotNumber = true;
  3239. }
  3240. this->AddInstr(instr, offset);
  3241. return;
  3242. }
  3243. default:
  3244. Assert(0);
  3245. }
  3246. if(isLdSlotThatWasNotProfiled && DoBailOnNoProfile())
  3247. {
  3248. InsertBailOnNoProfile(instr);
  3249. }
  3250. }
  3251. IR::Opnd*
  3252. IRBuilder::GetEnvironmentOperand(uint32 offset)
  3253. {
  3254. StackSym* sym = nullptr;
  3255. // The byte code doesn't refer directly to a closure environment. Get the implicit one
  3256. // that's pointed to by the function body.
  3257. if (m_func->DoStackFrameDisplay() && m_func->GetLocalFrameDisplaySym())
  3258. {
  3259. // Read the scope slot pointer back using the stack closure sym.
  3260. IR::Opnd *fieldOpnd = this->BuildFieldOpnd(Js::OpCode::LdSlotArr, m_func->GetLocalFrameDisplaySym()->m_id, 0, (Js::PropertyIdIndexType) - 1, PropertyKindSlotArray);
  3261. IR::RegOpnd *regOpnd = IR::RegOpnd::New(TyVar, m_func);
  3262. this->AddInstr(
  3263. IR::Instr::New(Js::OpCode::LdSlotArr, regOpnd, fieldOpnd, m_func),
  3264. offset);
  3265. sym = regOpnd->m_sym;
  3266. }
  3267. else
  3268. {
  3269. SymID symID;
  3270. symID = this->GetEnvRegForInnerFrameDisplay();
  3271. Assert(symID != Js::Constants::NoRegister);
  3272. if (IsLoopBody() && !RegIsConstant(symID))
  3273. {
  3274. this->EnsureLoopBodyLoadSlot(symID);
  3275. }
  3276. if (m_func->DoStackNestedFunc() && symID == GetEnvReg())
  3277. {
  3278. // Environment is not guaranteed constant during this function because it could become boxed during execution,
  3279. // so load the environment every time you need it.
  3280. IR::RegOpnd *regOpnd = IR::RegOpnd::New(TyVar, m_func);
  3281. this->AddInstr(
  3282. IR::Instr::New(Js::OpCode::LdEnv, regOpnd, m_func),
  3283. offset);
  3284. sym = regOpnd->m_sym;
  3285. }
  3286. else
  3287. {
  3288. sym = StackSym::FindOrCreate(symID, (Js::RegSlot)symID, m_func);
  3289. }
  3290. }
  3291. return IR::RegOpnd::New(sym, TyVar, m_func);
  3292. }
  3293. template <typename SizePolicy>
  3294. void
  3295. IRBuilder::BuildElementSlotI2(Js::OpCode newOpcode, uint32 offset)
  3296. {
  3297. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  3298. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  3299. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementSlotI2<SizePolicy>>();
  3300. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  3301. {
  3302. this->DoClosureRegCheck(layout->Value);
  3303. }
  3304. BuildElementSlotI2(newOpcode, offset, layout->Value, layout->SlotIndex1, layout->SlotIndex2, Js::Constants::NoProfileId);
  3305. }
  3306. template <typename SizePolicy>
  3307. void
  3308. IRBuilder::BuildProfiledElementSlotI2(Js::OpCode newOpcode, uint32 offset)
  3309. {
  3310. Assert(OpCodeAttr::IsProfiledOp(newOpcode));
  3311. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  3312. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_ElementSlotI2<SizePolicy>>>();
  3313. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  3314. {
  3315. this->DoClosureRegCheck(layout->Value);
  3316. }
  3317. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(newOpcode);
  3318. BuildElementSlotI2(newOpcode, offset, layout->Value, layout->SlotIndex1, layout->SlotIndex2, layout->profileId);
  3319. }
  3320. void
  3321. IRBuilder::BuildElementSlotI2(Js::OpCode newOpcode, uint32 offset, Js::RegSlot regSlot,
  3322. int32 slotId1, int32 slotId2, Js::ProfileId profileId)
  3323. {
  3324. IR::RegOpnd *regOpnd;
  3325. IR::SymOpnd *fieldOpnd;
  3326. IR::Instr *instr;
  3327. PropertySym *fieldSym;
  3328. bool isLdSlotThatWasNotProfiled = false;
  3329. switch (newOpcode)
  3330. {
  3331. case Js::OpCode::LdModuleSlot:
  3332. case Js::OpCode::StModuleSlot:
  3333. {
  3334. Field(Js::Var)* moduleExportVarArrayAddr = Js::JavascriptOperators::OP_GetModuleExportSlotArrayAddress(slotId1, slotId2, m_func->GetScriptContextInfo());
  3335. IR::AddrOpnd* addrOpnd = IR::AddrOpnd::New(moduleExportVarArrayAddr, IR::AddrOpndKindConstantAddress, m_func, true);
  3336. regOpnd = IR::RegOpnd::New(TyVar, m_func);
  3337. instr = IR::Instr::New(Js::OpCode::Ld_A, regOpnd, addrOpnd, m_func);
  3338. this->AddInstr(instr, offset);
  3339. fieldSym = PropertySym::New(regOpnd->m_sym, slotId2, (uint32)-1, (uint)-1, PropertyKindSlots, m_func);
  3340. fieldOpnd = IR::SymOpnd::New(fieldSym, TyVar, m_func);
  3341. if (newOpcode == Js::OpCode::LdModuleSlot)
  3342. {
  3343. newOpcode = Js::OpCode::LdSlot;
  3344. regOpnd = this->BuildDstOpnd(regSlot);
  3345. instr = IR::Instr::New(newOpcode, regOpnd, fieldOpnd, m_func);
  3346. }
  3347. else
  3348. {
  3349. Assert(newOpcode == Js::OpCode::StModuleSlot);
  3350. newOpcode = Js::OpCode::StSlot;
  3351. regOpnd = this->BuildSrcOpnd(regSlot);
  3352. instr = IR::Instr::New(newOpcode, fieldOpnd, regOpnd, m_func);
  3353. }
  3354. this->AddInstr(instr, offset);
  3355. break;
  3356. }
  3357. case Js::OpCode::LdEnvSlot:
  3358. case Js::OpCode::LdEnvObjSlot:
  3359. case Js::OpCode::StEnvSlot:
  3360. case Js::OpCode::StEnvSlotChkUndecl:
  3361. case Js::OpCode::StEnvObjSlot:
  3362. case Js::OpCode::StEnvObjSlotChkUndecl:
  3363. fieldOpnd = this->BuildFieldOpnd(Js::OpCode::LdSlotArr, this->GetEnvReg(), slotId1, (Js::PropertyIdIndexType)-1, PropertyKindSlotArray);
  3364. regOpnd = IR::RegOpnd::New(TyVar, m_func);
  3365. instr = IR::Instr::New(Js::OpCode::LdSlotArr, regOpnd, fieldOpnd, m_func);
  3366. this->AddInstr(instr, offset);
  3367. switch (newOpcode)
  3368. {
  3369. case Js::OpCode::LdEnvObjSlot:
  3370. case Js::OpCode::StEnvObjSlot:
  3371. case Js::OpCode::StEnvObjSlotChkUndecl:
  3372. m_func->GetTopFunc()->AddFrameDisplayCheck(fieldOpnd, (uint32)-1);
  3373. fieldSym = PropertySym::New(regOpnd->m_sym, (Js::DynamicObject::GetOffsetOfAuxSlots())/sizeof(Js::Var),
  3374. (uint32)-1, (uint)-1, PropertyKindSlotArray, m_func);
  3375. fieldOpnd = IR::SymOpnd::New(fieldSym, TyVar, m_func);
  3376. regOpnd = IR::RegOpnd::New(TyVar, m_func);
  3377. instr = IR::Instr::New(Js::OpCode::LdSlotArr, regOpnd, fieldOpnd, m_func);
  3378. this->AddInstr(instr, offset);
  3379. break;
  3380. default:
  3381. m_func->GetTopFunc()->AddFrameDisplayCheck(fieldOpnd, slotId2);
  3382. break;
  3383. }
  3384. fieldSym = PropertySym::New(regOpnd->m_sym, slotId2, (uint32)-1, (uint)-1, PropertyKindSlots, m_func);
  3385. fieldOpnd = IR::SymOpnd::New(fieldSym, TyVar, m_func);
  3386. switch (newOpcode)
  3387. {
  3388. case Js::OpCode::LdEnvSlot:
  3389. case Js::OpCode::LdEnvObjSlot:
  3390. newOpcode = Js::OpCode::LdSlot;
  3391. regOpnd = this->BuildDstOpnd(regSlot);
  3392. instr = nullptr;
  3393. if (profileId != Js::Constants::NoProfileId)
  3394. {
  3395. instr = this->BuildProfiledSlotLoad(newOpcode, regOpnd, fieldOpnd, profileId, &isLdSlotThatWasNotProfiled);
  3396. }
  3397. if (!instr)
  3398. {
  3399. instr = IR::Instr::New(newOpcode, regOpnd, fieldOpnd, m_func);
  3400. }
  3401. break;
  3402. default:
  3403. newOpcode =
  3404. newOpcode == Js::OpCode::StEnvSlot || newOpcode == Js::OpCode::StEnvObjSlot ? Js::OpCode::StSlot : Js::OpCode::StSlotChkUndecl;
  3405. regOpnd = this->BuildSrcOpnd(regSlot);
  3406. instr = IR::Instr::New(newOpcode, fieldOpnd, regOpnd, m_func);
  3407. if (newOpcode == Js::OpCode::StSlotChkUndecl)
  3408. {
  3409. // ChkUndecl includes an implicit read of the destination. Communicate the liveness by using the destination in src2.
  3410. instr->SetSrc2(fieldOpnd);
  3411. }
  3412. break;
  3413. }
  3414. this->AddInstr(instr, offset);
  3415. if(isLdSlotThatWasNotProfiled && DoBailOnNoProfile())
  3416. {
  3417. InsertBailOnNoProfile(instr);
  3418. }
  3419. break;
  3420. case Js::OpCode::StInnerObjSlot:
  3421. case Js::OpCode::StInnerObjSlotChkUndecl:
  3422. case Js::OpCode::StInnerSlot:
  3423. case Js::OpCode::StInnerSlotChkUndecl:
  3424. if ((uint)slotId1 >= m_func->GetJITFunctionBody()->GetInnerScopeCount())
  3425. {
  3426. Js::Throw::FatalInternalError();
  3427. }
  3428. regOpnd = this->BuildSrcOpnd(regSlot);
  3429. slotId1 += this->m_func->GetJITFunctionBody()->GetFirstInnerScopeReg();
  3430. if ((uint)slotId1 >= this->m_func->GetJITFunctionBody()->GetLocalsCount())
  3431. {
  3432. Js::Throw::FatalInternalError();
  3433. }
  3434. if (newOpcode == Js::OpCode::StInnerObjSlot || newOpcode == Js::OpCode::StInnerObjSlotChkUndecl)
  3435. {
  3436. IR::RegOpnd * slotOpnd = IR::RegOpnd::New(TyVar, m_func);
  3437. fieldOpnd = this->BuildFieldOpnd(Js::OpCode::LdSlotArr, slotId1, (Js::DynamicObject::GetOffsetOfAuxSlots())/sizeof(Js::Var), (Js::PropertyIdIndexType)-1, PropertyKindSlotArray);
  3438. instr = IR::Instr::New(Js::OpCode::LdSlotArr, slotOpnd, fieldOpnd, m_func);
  3439. this->AddInstr(instr, offset);
  3440. PropertySym *propertySym = PropertySym::New(slotOpnd->m_sym, slotId2, (uint32)-1, (uint)-1, PropertyKindSlots, m_func);
  3441. fieldOpnd = IR::PropertySymOpnd::New(propertySym, (Js::CacheId)-1, TyVar, m_func);
  3442. }
  3443. else
  3444. {
  3445. fieldOpnd = this->BuildFieldOpnd(Js::OpCode::StSlot, slotId1, slotId2, (Js::PropertyIdIndexType)-1, PropertyKindSlots);
  3446. if (!this->DoSlotArrayCheck(fieldOpnd, IsLoopBody()))
  3447. {
  3448. // Need a dynamic check on the size of the local slot array.
  3449. m_func->GetTopFunc()->AddSlotArrayCheck(fieldOpnd);
  3450. }
  3451. }
  3452. newOpcode =
  3453. newOpcode == Js::OpCode::StInnerObjSlot || newOpcode == Js::OpCode::StInnerSlot ?
  3454. Js::OpCode::StSlot : Js::OpCode::StSlotChkUndecl;
  3455. instr = IR::Instr::New(newOpcode, fieldOpnd, regOpnd, m_func);
  3456. if (newOpcode == Js::OpCode::StSlotChkUndecl)
  3457. {
  3458. // ChkUndecl includes an implicit read of the destination. Communicate the liveness by using the destination in src2.
  3459. instr->SetSrc2(fieldOpnd);
  3460. }
  3461. this->AddInstr(instr, offset);
  3462. break;
  3463. case Js::OpCode::LdInnerSlot:
  3464. case Js::OpCode::LdInnerObjSlot:
  3465. if ((uint)slotId1 >= m_func->GetJITFunctionBody()->GetInnerScopeCount())
  3466. {
  3467. Js::Throw::FatalInternalError();
  3468. }
  3469. slotId1 += this->m_func->GetJITFunctionBody()->GetFirstInnerScopeReg();
  3470. if ((uint)slotId1 >= this->m_func->GetJITFunctionBody()->GetLocalsCount())
  3471. {
  3472. Js::Throw::FatalInternalError();
  3473. }
  3474. if (newOpcode == Js::OpCode::LdInnerObjSlot)
  3475. {
  3476. IR::RegOpnd * slotOpnd = IR::RegOpnd::New(TyVar, m_func);
  3477. fieldOpnd = this->BuildFieldOpnd(Js::OpCode::LdSlotArr, slotId1, (Js::DynamicObject::GetOffsetOfAuxSlots())/sizeof(Js::Var), (Js::PropertyIdIndexType)-1, PropertyKindSlotArray);
  3478. instr = IR::Instr::New(Js::OpCode::LdSlotArr, slotOpnd, fieldOpnd, m_func);
  3479. this->AddInstr(instr, offset);
  3480. PropertySym *propertySym = PropertySym::New(slotOpnd->m_sym, slotId2, (uint32)-1, (uint)-1, PropertyKindSlots, m_func);
  3481. fieldOpnd = IR::PropertySymOpnd::New(propertySym, (Js::CacheId)-1, TyVar, m_func);
  3482. }
  3483. else
  3484. {
  3485. fieldOpnd = this->BuildFieldOpnd(Js::OpCode::LdSlot, slotId1, slotId2, (Js::PropertyIdIndexType)-1, PropertyKindSlots);
  3486. if (!this->DoSlotArrayCheck(fieldOpnd, IsLoopBody()))
  3487. {
  3488. // Need a dynamic check on the size of the local slot array.
  3489. m_func->GetTopFunc()->AddSlotArrayCheck(fieldOpnd);
  3490. }
  3491. }
  3492. regOpnd = this->BuildDstOpnd(regSlot);
  3493. instr = IR::Instr::New(Js::OpCode::LdSlot, regOpnd, fieldOpnd, m_func);
  3494. this->AddInstr(instr, offset);
  3495. break;
  3496. default:
  3497. AssertMsg(false, "Unsupported opcode in BuildElementSlotI2");
  3498. break;
  3499. }
  3500. }
  3501. template <typename SizePolicy>
  3502. void
  3503. IRBuilder::BuildElementSlotI3(Js::OpCode newOpcode, uint32 offset)
  3504. {
  3505. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  3506. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  3507. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementSlotI3<SizePolicy>>();
  3508. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  3509. {
  3510. this->DoClosureRegCheck(layout->Value);
  3511. this->DoClosureRegCheck(layout->Instance);
  3512. this->DoClosureRegCheck(layout->HomeObj);
  3513. }
  3514. BuildElementSlotI3(newOpcode, offset, layout->Instance, layout->Value, layout->SlotIndex, layout->HomeObj, Js::Constants::NoProfileId);
  3515. }
  3516. template <typename SizePolicy>
  3517. void
  3518. IRBuilder::BuildProfiledElementSlotI3(Js::OpCode newOpcode, uint32 offset)
  3519. {
  3520. Assert(OpCodeAttr::IsProfiledOp(newOpcode));
  3521. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  3522. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_ElementSlotI3<SizePolicy>>>();
  3523. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  3524. {
  3525. this->DoClosureRegCheck(layout->Value);
  3526. this->DoClosureRegCheck(layout->Instance);
  3527. this->DoClosureRegCheck(layout->HomeObj);
  3528. }
  3529. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(newOpcode);
  3530. BuildElementSlotI3(newOpcode, offset, layout->Instance, layout->Value, layout->SlotIndex, layout->HomeObj, layout->profileId);
  3531. }
  3532. void
  3533. IRBuilder::BuildElementSlotI3(Js::OpCode newOpcode, uint32 offset, Js::RegSlot fieldRegSlot, Js::RegSlot regSlot,
  3534. int32 slotId, Js::RegSlot homeObj, Js::ProfileId profileId)
  3535. {
  3536. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  3537. IR::Instr * instr;
  3538. IR::RegOpnd * regOpnd;
  3539. switch (newOpcode)
  3540. {
  3541. case Js::OpCode::NewInnerScFuncHomeObj:
  3542. newOpcode = Js::OpCode::NewScFuncHomeObj;
  3543. goto NewScFuncCommon;
  3544. case Js::OpCode::NewInnerScGenFuncHomeObj:
  3545. newOpcode = Js::OpCode::NewScGenFuncHomeObj;
  3546. NewScFuncCommon:
  3547. {
  3548. Js::FunctionInfoPtrPtr infoRef = m_func->GetJITFunctionBody()->GetNestedFuncRef(slotId);
  3549. IR::AddrOpnd * functionBodySlotOpnd = IR::AddrOpnd::New((Js::Var)infoRef, IR::AddrOpndKindDynamicMisc, m_func);
  3550. IR::Opnd * environmentOpnd = this->BuildSrcOpnd(fieldRegSlot);
  3551. IR::Opnd * homeObjOpnd = this->BuildSrcOpnd(homeObj);
  3552. regOpnd = this->BuildDstOpnd(regSlot);
  3553. instr = IR::Instr::New(Js::OpCode::ExtendArg_A, IR::RegOpnd::New(TyVar, m_func), homeObjOpnd, m_func);
  3554. this->AddInstr(instr, offset);
  3555. instr = IR::Instr::New(Js::OpCode::ExtendArg_A, IR::RegOpnd::New(TyVar, m_func), functionBodySlotOpnd, instr->GetDst(), m_func);
  3556. this->AddInstr(instr, offset);
  3557. instr = IR::Instr::New(Js::OpCode::ExtendArg_A, IR::RegOpnd::New(TyVar, m_func), environmentOpnd, instr->GetDst(), m_func);
  3558. this->AddInstr(instr, offset);
  3559. instr = IR::Instr::New(newOpcode, regOpnd, instr->GetDst(), m_func);
  3560. if (regOpnd->m_sym->m_isSingleDef)
  3561. {
  3562. regOpnd->m_sym->m_isSafeThis = true;
  3563. regOpnd->m_sym->m_isNotNumber = true;
  3564. }
  3565. this->AddInstr(instr, offset);
  3566. return;
  3567. }
  3568. default:
  3569. AssertMsg(UNREACHED, "Unknown ElementSlotI3 opcode");
  3570. Fatal();
  3571. }
  3572. }
  3573. IR::SymOpnd *
  3574. IRBuilder::BuildLoopBodySlotOpnd(SymID symId)
  3575. {
  3576. Assert(!this->RegIsConstant((Js::RegSlot)symId));
  3577. // Get the interpreter frame instance that was passed in.
  3578. StackSym *loopParamSym = m_func->EnsureLoopParamSym();
  3579. PropertySym * fieldSym = PropertySym::FindOrCreate(loopParamSym->m_id, (Js::PropertyId)(symId + this->m_loopBodyLocalsStartSlot), (uint32)-1, (uint)-1, PropertyKindLocalSlots, m_func);
  3580. return IR::SymOpnd::New(fieldSym, TyVar, m_func);
  3581. }
  3582. void
  3583. IRBuilder::EnsureLoopBodyLoadSlot(SymID symId, bool isCatchObjectSym)
  3584. {
  3585. // No need to emit LdSlot for a catch object. In fact, if we do, we might be loading an uninitialized value from the slot.
  3586. if (isCatchObjectSym)
  3587. {
  3588. return;
  3589. }
  3590. StackSym * symDst = StackSym::FindOrCreate(symId, (Js::RegSlot)symId, m_func);
  3591. if (symDst->m_isCatchObjectSym)
  3592. {
  3593. return;
  3594. }
  3595. AssertOrFailFast(symId < m_ldSlots->Length());
  3596. if (this->m_ldSlots->TestAndSet(symId))
  3597. {
  3598. return;
  3599. }
  3600. IR::SymOpnd * fieldSymOpnd = this->BuildLoopBodySlotOpnd(symId);
  3601. IR::RegOpnd * dstOpnd = IR::RegOpnd::New(symDst, TyVar, m_func);
  3602. IR::Instr * ldSlotInstr;
  3603. ValueType symValueType;
  3604. if(m_func->GetWorkItem()->HasSymIdToValueTypeMap() && m_func->GetWorkItem()->TryGetValueType(symId, &symValueType))
  3605. {
  3606. ldSlotInstr = IR::ProfiledInstr::New(Js::OpCode::LdSlot, dstOpnd, fieldSymOpnd, m_func);
  3607. ldSlotInstr->AsProfiledInstr()->u.FldInfo().valueType = symValueType;
  3608. }
  3609. else
  3610. {
  3611. ldSlotInstr = IR::Instr::New(Js::OpCode::LdSlot, dstOpnd, fieldSymOpnd, m_func);
  3612. }
  3613. m_func->m_headInstr->InsertAfter(ldSlotInstr);
  3614. if (m_lastInstr == m_func->m_headInstr)
  3615. {
  3616. m_lastInstr = ldSlotInstr;
  3617. }
  3618. }
  3619. void
  3620. IRBuilder::SetLoopBodyStSlot(SymID symID, bool isCatchObjectSym)
  3621. {
  3622. if (this->m_func->HasTry() && !PHASE_OFF(Js::JITLoopBodyInTryCatchPhase, this->m_func))
  3623. {
  3624. // No need to emit StSlot for a catch object. In fact, if we do, we might be storing an uninitialized value to the slot.
  3625. if (isCatchObjectSym)
  3626. {
  3627. return;
  3628. }
  3629. StackSym * dstSym = StackSym::FindOrCreate(symID, (Js::RegSlot)symID, m_func);
  3630. Assert(dstSym);
  3631. if (dstSym->m_isCatchObjectSym)
  3632. {
  3633. return;
  3634. }
  3635. }
  3636. AssertOrFailFast(symID < m_stSlots->Length());
  3637. this->m_stSlots->Set(symID);
  3638. }
  3639. ///----------------------------------------------------------------------------
  3640. ///
  3641. /// IRBuilder::BuildElementCP
  3642. ///
  3643. /// Build IR instr for an ElementCP or ElementRootCP instruction.
  3644. ///
  3645. ///----------------------------------------------------------------------------
  3646. IR::Instr *
  3647. IRBuilder::BuildProfiledFieldLoad(Js::OpCode loadOp, IR::RegOpnd *dstOpnd, IR::SymOpnd *srcOpnd, Js::CacheId inlineCacheIndex, bool *pUnprofiled)
  3648. {
  3649. IR::Instr * instr = nullptr;
  3650. // Prefer JitProfilingInstr if we're in simplejit
  3651. if (m_func->DoSimpleJitDynamicProfile())
  3652. {
  3653. instr = IR::JitProfilingInstr::New(loadOp, dstOpnd, srcOpnd, m_func);
  3654. }
  3655. else if (this->m_func->HasProfileInfo())
  3656. {
  3657. instr = IR::ProfiledInstr::New(loadOp, dstOpnd, srcOpnd, m_func);
  3658. instr->AsProfiledInstr()->u.FldInfo() = *(m_func->GetReadOnlyProfileInfo()->GetFldInfo(inlineCacheIndex));
  3659. *pUnprofiled = !instr->AsProfiledInstr()->u.FldInfo().WasLdFldProfiled();
  3660. dstOpnd->SetValueType(instr->AsProfiledInstr()->u.FldInfo().valueType);
  3661. #if ENABLE_DEBUG_CONFIG_OPTIONS
  3662. if(Js::Configuration::Global.flags.TestTrace.IsEnabled(Js::DynamicProfilePhase))
  3663. {
  3664. const ValueType valueType(instr->AsProfiledInstr()->u.FldInfo().valueType);
  3665. char valueTypeStr[VALUE_TYPE_MAX_STRING_SIZE];
  3666. valueType.ToString(valueTypeStr);
  3667. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  3668. Output::Print(_u("TestTrace function %s (%s) ValueType = %i "), m_func->GetJITFunctionBody()->GetDisplayName(), m_func->GetDebugNumberSet(debugStringBuffer), valueTypeStr);
  3669. instr->DumpTestTrace();
  3670. }
  3671. #endif
  3672. }
  3673. return instr;
  3674. }
  3675. Js::RegSlot IRBuilder::GetEnvRegForEvalCode() const
  3676. {
  3677. if (m_func->GetJITFunctionBody()->IsStrictMode() && m_func->GetJITFunctionBody()->IsGlobalFunc())
  3678. {
  3679. return m_func->GetJITFunctionBody()->GetLocalFrameDisplayReg();
  3680. }
  3681. else
  3682. {
  3683. return GetEnvReg();
  3684. }
  3685. }
  3686. template <typename SizePolicy>
  3687. void
  3688. IRBuilder::BuildElementP(Js::OpCode newOpcode, uint32 offset)
  3689. {
  3690. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  3691. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementP<SizePolicy>>();
  3692. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  3693. {
  3694. this->DoClosureRegCheck(layout->Value);
  3695. }
  3696. BuildElementP(newOpcode, offset, layout->Value, layout->inlineCacheIndex);
  3697. }
  3698. void
  3699. IRBuilder::BuildElementP(Js::OpCode newOpcode, uint32 offset, Js::RegSlot regSlot, Js::CacheId inlineCacheIndex)
  3700. {
  3701. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  3702. IR::Instr * instr;
  3703. IR::RegOpnd * regOpnd;
  3704. IR::Opnd * srcOpnd;
  3705. IR::SymOpnd * fieldSymOpnd;
  3706. Js::PropertyId propertyId;
  3707. bool isProfiled = OpCodeAttr::IsProfiledOp(newOpcode);
  3708. bool isLdFldThatWasNotProfiled = false;
  3709. if (isProfiled)
  3710. {
  3711. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(newOpcode);
  3712. }
  3713. propertyId = this->m_func->GetJITFunctionBody()->GetPropertyIdFromCacheId(inlineCacheIndex);
  3714. Js::RegSlot instance = this->GetEnvRegForEvalCode();
  3715. switch (newOpcode)
  3716. {
  3717. case Js::OpCode::LdLocalFld:
  3718. if (m_func->GetLocalClosureSym()->HasByteCodeRegSlot())
  3719. {
  3720. IR::ByteCodeUsesInstr * byteCodeUse = IR::ByteCodeUsesInstr::New(m_func, offset);
  3721. byteCodeUse->SetNonOpndSymbol(m_func->GetLocalClosureSym()->m_id);
  3722. this->AddInstr(byteCodeUse, offset);
  3723. }
  3724. newOpcode = Js::OpCode::LdFld;
  3725. fieldSymOpnd = this->BuildFieldOpnd(newOpcode, m_func->GetJITFunctionBody()->GetLocalClosureReg(), propertyId, (Js::PropertyIdIndexType)-1, PropertyKindData, inlineCacheIndex);
  3726. if (fieldSymOpnd->IsPropertySymOpnd())
  3727. {
  3728. fieldSymOpnd->AsPropertySymOpnd()->TryDisableRuntimePolymorphicCache();
  3729. }
  3730. regOpnd = this->BuildDstOpnd(regSlot);
  3731. instr = nullptr;
  3732. if (isProfiled)
  3733. {
  3734. instr = this->BuildProfiledFieldLoad(newOpcode, regOpnd, fieldSymOpnd, inlineCacheIndex, &isLdFldThatWasNotProfiled);
  3735. }
  3736. // If it hasn't been set yet
  3737. if (!instr)
  3738. {
  3739. instr = IR::Instr::New(newOpcode, regOpnd, fieldSymOpnd, m_func);
  3740. }
  3741. break;
  3742. case Js::OpCode::StLocalFld:
  3743. if (m_func->GetLocalClosureSym()->HasByteCodeRegSlot())
  3744. {
  3745. IR::ByteCodeUsesInstr * byteCodeUse = IR::ByteCodeUsesInstr::New(m_func, offset);
  3746. byteCodeUse->SetNonOpndSymbol(m_func->GetLocalClosureSym()->m_id);
  3747. this->AddInstr(byteCodeUse, offset);
  3748. }
  3749. fieldSymOpnd = this->BuildFieldOpnd(newOpcode, m_func->GetJITFunctionBody()->GetLocalClosureReg(), propertyId, (Js::PropertyIdIndexType)-1, PropertyKindData, inlineCacheIndex);
  3750. if (fieldSymOpnd->IsPropertySymOpnd())
  3751. {
  3752. fieldSymOpnd->AsPropertySymOpnd()->TryDisableRuntimePolymorphicCache();
  3753. }
  3754. srcOpnd = this->BuildSrcOpnd(regSlot);
  3755. newOpcode = Js::OpCode::StFld;
  3756. goto stCommon;
  3757. case Js::OpCode::InitLocalFld:
  3758. case Js::OpCode::InitLocalLetFld:
  3759. case Js::OpCode::InitUndeclLocalLetFld:
  3760. case Js::OpCode::InitUndeclLocalConstFld:
  3761. {
  3762. if (m_func->GetLocalClosureSym()->HasByteCodeRegSlot())
  3763. {
  3764. IR::ByteCodeUsesInstr * byteCodeUse = IR::ByteCodeUsesInstr::New(m_func, offset);
  3765. byteCodeUse->SetNonOpndSymbol(m_func->GetLocalClosureSym()->m_id);
  3766. this->AddInstr(byteCodeUse, offset);
  3767. }
  3768. fieldSymOpnd = this->BuildFieldOpnd(newOpcode, m_func->GetJITFunctionBody()->GetLocalClosureReg(), propertyId, (Js::PropertyIdIndexType)-1, PropertyKindData, inlineCacheIndex);
  3769. // Store
  3770. if (newOpcode == Js::OpCode::InitUndeclLocalLetFld)
  3771. {
  3772. srcOpnd = IR::AddrOpnd::New(m_func->GetScriptContextInfo()->GetUndeclBlockVarAddr(), IR::AddrOpndKindDynamicVar, this->m_func, true);
  3773. srcOpnd->SetValueType(ValueType::PrimitiveOrObject);
  3774. newOpcode = Js::OpCode::InitLetFld;
  3775. }
  3776. else if (newOpcode == Js::OpCode::InitUndeclLocalConstFld)
  3777. {
  3778. srcOpnd = IR::AddrOpnd::New(m_func->GetScriptContextInfo()->GetUndeclBlockVarAddr(), IR::AddrOpndKindDynamicVar, this->m_func, true);
  3779. srcOpnd->SetValueType(ValueType::PrimitiveOrObject);
  3780. newOpcode = Js::OpCode::InitConstFld;
  3781. }
  3782. else
  3783. {
  3784. srcOpnd = this->BuildSrcOpnd(regSlot);
  3785. newOpcode = newOpcode == Js::OpCode::InitLocalFld ? Js::OpCode::InitFld : Js::OpCode::InitLetFld;
  3786. }
  3787. stCommon:
  3788. instr = nullptr;
  3789. if (isProfiled)
  3790. {
  3791. if (m_func->DoSimpleJitDynamicProfile())
  3792. {
  3793. instr = IR::JitProfilingInstr::New(newOpcode, fieldSymOpnd, srcOpnd, m_func);
  3794. }
  3795. else if (this->m_func->HasProfileInfo())
  3796. {
  3797. instr = IR::ProfiledInstr::New(newOpcode, fieldSymOpnd, srcOpnd, m_func);
  3798. instr->AsProfiledInstr()->u.FldInfo() = *(m_func->GetReadOnlyProfileInfo()->GetFldInfo(inlineCacheIndex));
  3799. }
  3800. }
  3801. // If it hasn't been set yet
  3802. if (!instr)
  3803. {
  3804. instr = IR::Instr::New(newOpcode, fieldSymOpnd, srcOpnd, m_func);
  3805. }
  3806. break;
  3807. }
  3808. case Js::OpCode::ScopedLdFld:
  3809. case Js::OpCode::ScopedLdFldForTypeOf:
  3810. {
  3811. Assert(!isProfiled);
  3812. Assert(this->m_func->GetScriptContextInfo()->GetAddr() == this->m_func->GetTopFunc()->GetScriptContextInfo()->GetAddr());
  3813. fieldSymOpnd = this->BuildFieldOpnd(newOpcode, instance, propertyId, (Js::PropertyIdIndexType)-1, PropertyKindData, inlineCacheIndex);
  3814. regOpnd = this->BuildDstOpnd(regSlot);
  3815. instr = IR::Instr::New(newOpcode, regOpnd, fieldSymOpnd, m_func);
  3816. break;
  3817. }
  3818. case Js::OpCode::ScopedStFld:
  3819. case Js::OpCode::ConsoleScopedStFld:
  3820. case Js::OpCode::ScopedStFldStrict:
  3821. case Js::OpCode::ConsoleScopedStFldStrict:
  3822. {
  3823. Assert(!isProfiled);
  3824. fieldSymOpnd = this->BuildFieldOpnd(newOpcode, instance, propertyId, (Js::PropertyIdIndexType)-1, PropertyKindData, inlineCacheIndex);
  3825. // Implicit root object as default instance
  3826. IR::Opnd * instance2Opnd = this->BuildSrcOpnd(Js::FunctionBody::RootObjectRegSlot);
  3827. regOpnd = this->BuildSrcOpnd(regSlot);
  3828. instr = IR::Instr::New(newOpcode, fieldSymOpnd, regOpnd, instance2Opnd, m_func);
  3829. break;
  3830. }
  3831. default:
  3832. AssertMsg(UNREACHED, "Unknown ElementP opcode");
  3833. Fatal();
  3834. }
  3835. this->AddInstr(instr, offset);
  3836. if(isLdFldThatWasNotProfiled && DoBailOnNoProfile())
  3837. {
  3838. InsertBailOnNoProfile(instr);
  3839. }
  3840. }
  3841. template <typename SizePolicy>
  3842. void
  3843. IRBuilder::BuildElementPIndexed(Js::OpCode newOpcode, uint32 offset)
  3844. {
  3845. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  3846. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementPIndexed<SizePolicy>>();
  3847. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  3848. {
  3849. this->DoClosureRegCheck(layout->Value);
  3850. }
  3851. switch (newOpcode)
  3852. {
  3853. case Js::OpCode::InitInnerFld:
  3854. newOpcode = Js::OpCode::InitFld;
  3855. goto initinnerfldcommon;
  3856. case Js::OpCode::InitInnerLetFld:
  3857. newOpcode = Js::OpCode::InitLetFld;
  3858. // fall through
  3859. initinnerfldcommon:
  3860. case Js::OpCode::InitUndeclLetFld:
  3861. case Js::OpCode::InitUndeclConstFld:
  3862. BuildElementCP(newOpcode, offset, InnerScopeIndexToRegSlot(layout->scopeIndex), layout->Value, layout->inlineCacheIndex);
  3863. break;
  3864. default:
  3865. AssertMsg(false, "Unknown opcode for ElementPIndexed");
  3866. break;
  3867. }
  3868. }
  3869. template <typename SizePolicy>
  3870. void
  3871. IRBuilder::BuildElementCP(Js::OpCode newOpcode, uint32 offset)
  3872. {
  3873. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  3874. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementCP<SizePolicy>>();
  3875. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  3876. {
  3877. this->DoClosureRegCheck(layout->Value);
  3878. this->DoClosureRegCheck(layout->Instance);
  3879. }
  3880. BuildElementCP(newOpcode, offset, layout->Instance, layout->Value, layout->inlineCacheIndex);
  3881. }
  3882. template <typename SizePolicy>
  3883. void
  3884. IRBuilder::BuildElementRootCP(Js::OpCode newOpcode, uint32 offset)
  3885. {
  3886. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  3887. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementRootCP<SizePolicy>>();
  3888. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  3889. {
  3890. this->DoClosureRegCheck(layout->Value);
  3891. }
  3892. BuildElementCP(newOpcode, offset, Js::FunctionBody::RootObjectRegSlot, layout->Value, layout->inlineCacheIndex);
  3893. }
  3894. void
  3895. IRBuilder::BuildElementCP(Js::OpCode newOpcode, uint32 offset, Js::RegSlot instance, Js::RegSlot regSlot, Js::CacheId inlineCacheIndex)
  3896. {
  3897. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  3898. Js::PropertyId propertyId;
  3899. bool isProfiled = OpCodeAttr::IsProfiledOp(newOpcode);
  3900. if (isProfiled)
  3901. {
  3902. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(newOpcode);
  3903. }
  3904. propertyId = m_func->GetJITFunctionBody()->GetPropertyIdFromCacheId(inlineCacheIndex);
  3905. IR::SymOpnd * fieldSymOpnd = this->BuildFieldOpnd(newOpcode, instance, propertyId, (Js::PropertyIdIndexType)-1, PropertyKindData, inlineCacheIndex);
  3906. IR::RegOpnd * regOpnd;
  3907. IR::Instr * instr = nullptr;
  3908. bool isLdFldThatWasNotProfiled = false;
  3909. switch (newOpcode)
  3910. {
  3911. case Js::OpCode::LdFldForTypeOf:
  3912. case Js::OpCode::LdFld:
  3913. case Js::OpCode::LdLen_A:
  3914. if (fieldSymOpnd->IsPropertySymOpnd())
  3915. {
  3916. fieldSymOpnd->AsPropertySymOpnd()->TryDisableRuntimePolymorphicCache();
  3917. }
  3918. case Js::OpCode::LdFldForCallApplyTarget:
  3919. case Js::OpCode::LdRootFldForTypeOf:
  3920. case Js::OpCode::LdRootFld:
  3921. case Js::OpCode::LdMethodFld:
  3922. case Js::OpCode::LdRootMethodFld:
  3923. case Js::OpCode::ScopedLdMethodFld:
  3924. // Load
  3925. // LdMethodFromFlags is backend only. Don't need to be added here.
  3926. regOpnd = this->BuildDstOpnd(regSlot);
  3927. if (isProfiled)
  3928. {
  3929. instr = this->BuildProfiledFieldLoad(newOpcode, regOpnd, fieldSymOpnd, inlineCacheIndex, &isLdFldThatWasNotProfiled);
  3930. }
  3931. // If it hasn't been set yet
  3932. if (!instr)
  3933. {
  3934. instr = IR::Instr::New(newOpcode, regOpnd, fieldSymOpnd, m_func);
  3935. }
  3936. if (newOpcode == Js::OpCode::LdFld ||
  3937. newOpcode == Js::OpCode::LdFldForCallApplyTarget ||
  3938. newOpcode == Js::OpCode::LdMethodFld ||
  3939. newOpcode == Js::OpCode::LdRootMethodFld ||
  3940. newOpcode == Js::OpCode::ScopedLdMethodFld)
  3941. {
  3942. // Check whether we're loading (what appears to be) a built-in method.
  3943. Js::BuiltinFunction builtInIndex = Js::BuiltinFunction::None;
  3944. PropertySym *fieldSym = fieldSymOpnd->m_sym->AsPropertySym();
  3945. this->CheckBuiltIn(fieldSym, &builtInIndex);
  3946. regOpnd->m_sym->m_builtInIndex = builtInIndex;
  3947. }
  3948. break;
  3949. case Js::OpCode::StFld:
  3950. if (fieldSymOpnd->IsPropertySymOpnd())
  3951. {
  3952. fieldSymOpnd->AsPropertySymOpnd()->TryDisableRuntimePolymorphicCache();
  3953. }
  3954. case Js::OpCode::InitFld:
  3955. case Js::OpCode::InitRootFld:
  3956. case Js::OpCode::InitLetFld:
  3957. case Js::OpCode::InitRootLetFld:
  3958. case Js::OpCode::InitConstFld:
  3959. case Js::OpCode::InitRootConstFld:
  3960. case Js::OpCode::InitUndeclLetFld:
  3961. case Js::OpCode::InitUndeclConstFld:
  3962. case Js::OpCode::InitClassMember:
  3963. case Js::OpCode::StRootFld:
  3964. case Js::OpCode::StFldStrict:
  3965. case Js::OpCode::StRootFldStrict:
  3966. {
  3967. IR::Opnd *srcOpnd;
  3968. // Store
  3969. if (newOpcode == Js::OpCode::InitUndeclLetFld)
  3970. {
  3971. srcOpnd = IR::AddrOpnd::New(m_func->GetScriptContextInfo()->GetUndeclBlockVarAddr(), IR::AddrOpndKindDynamicVar, this->m_func, true);
  3972. srcOpnd->SetValueType(ValueType::PrimitiveOrObject);
  3973. newOpcode = Js::OpCode::InitLetFld;
  3974. }
  3975. else if (newOpcode == Js::OpCode::InitUndeclConstFld)
  3976. {
  3977. srcOpnd = IR::AddrOpnd::New(m_func->GetScriptContextInfo()->GetUndeclBlockVarAddr(), IR::AddrOpndKindDynamicVar, this->m_func, true);
  3978. srcOpnd->SetValueType(ValueType::PrimitiveOrObject);
  3979. newOpcode = Js::OpCode::InitConstFld;
  3980. }
  3981. else
  3982. {
  3983. srcOpnd = this->BuildSrcOpnd(regSlot);
  3984. }
  3985. if (isProfiled)
  3986. {
  3987. if (m_func->DoSimpleJitDynamicProfile())
  3988. {
  3989. instr = IR::JitProfilingInstr::New(newOpcode, fieldSymOpnd, srcOpnd, m_func);
  3990. }
  3991. else if (this->m_func->HasProfileInfo())
  3992. {
  3993. instr = IR::ProfiledInstr::New(newOpcode, fieldSymOpnd, srcOpnd, m_func);
  3994. instr->AsProfiledInstr()->u.FldInfo() = *(m_func->GetReadOnlyProfileInfo()->GetFldInfo(inlineCacheIndex));
  3995. }
  3996. }
  3997. // If it hasn't been set yet
  3998. if (!instr)
  3999. {
  4000. instr = IR::Instr::New(newOpcode, fieldSymOpnd, srcOpnd, m_func);
  4001. }
  4002. break;
  4003. }
  4004. default:
  4005. AssertMsg(UNREACHED, "Unknown ElementCP opcode");
  4006. Fatal();
  4007. }
  4008. this->AddInstr(instr, offset);
  4009. if(isLdFldThatWasNotProfiled && DoBailOnNoProfile())
  4010. {
  4011. InsertBailOnNoProfile(instr);
  4012. }
  4013. }
  4014. template <typename SizePolicy>
  4015. void
  4016. IRBuilder::BuildProfiledElementCP(Js::OpCode newOpcode, uint32 offset)
  4017. {
  4018. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  4019. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_ElementCP<SizePolicy>>>();
  4020. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  4021. {
  4022. this->DoClosureRegCheck(layout->Value);
  4023. this->DoClosureRegCheck(layout->Instance);
  4024. }
  4025. BuildProfiledElementCP(newOpcode, offset, layout->Instance, layout->Value, layout->inlineCacheIndex, layout->profileId);
  4026. }
  4027. void
  4028. IRBuilder::BuildProfiledElementCP(Js::OpCode newOpcode, uint32 offset, Js::RegSlot instance, Js::RegSlot regSlot, Js::CacheId inlineCacheIndex, Js::ProfileId profileId)
  4029. {
  4030. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  4031. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(newOpcode);
  4032. Assert(newOpcode == Js::OpCode::LdLen_A);
  4033. Js::PropertyId propertyId = m_func->GetJITFunctionBody()->GetPropertyIdFromCacheId(inlineCacheIndex);
  4034. IR::SymOpnd * fieldSymOpnd = this->BuildFieldOpnd(newOpcode, instance, propertyId, (Js::PropertyIdIndexType) - 1, PropertyKindData, inlineCacheIndex);
  4035. IR::RegOpnd * dstOpnd = this->BuildDstOpnd(regSlot);
  4036. bool isProfiled = (profileId != Js::Constants::NoProfileId);
  4037. ValueType arrayType = ValueType::Uninitialized;
  4038. const Js::LdLenInfo * ldLenInfo = nullptr;
  4039. if (m_func->HasProfileInfo())
  4040. {
  4041. ldLenInfo = m_func->GetReadOnlyProfileInfo()->GetLdLenInfo(profileId);
  4042. arrayType = (ldLenInfo->GetArrayType());
  4043. if (arrayType.IsLikelyNativeArray() && !AllowNativeArrayProfileInfo())
  4044. {
  4045. // An opnd's value type will get replaced in the forward phase when it is not fixed. Store the array type in the ProfiledInstr.
  4046. arrayType = arrayType.SetArrayTypeId(Js::TypeIds_Array);
  4047. }
  4048. fieldSymOpnd->SetValueType(arrayType);
  4049. if (m_func->GetTopFunc()->HasTry() && !m_func->GetTopFunc()->DoOptimizeTry())
  4050. {
  4051. isProfiled = false;
  4052. }
  4053. }
  4054. else
  4055. {
  4056. isProfiled = false;
  4057. }
  4058. bool wasNotProfiled = false;
  4059. IR::Instr *instr = nullptr;
  4060. if (isProfiled)
  4061. {
  4062. instr = this->BuildProfiledFieldLoad(newOpcode, dstOpnd, fieldSymOpnd, inlineCacheIndex, &wasNotProfiled);
  4063. }
  4064. if (instr == nullptr)
  4065. {
  4066. instr = IR::Instr::New(newOpcode, dstOpnd, fieldSymOpnd, m_func);
  4067. }
  4068. else if (instr->IsJitProfilingInstr())
  4069. {
  4070. instr->AsJitProfilingInstr()->profileId = profileId;
  4071. }
  4072. else if (instr->IsProfiledInstr())
  4073. {
  4074. instr->AsProfiledInstr()->u.LdLenInfo() = *ldLenInfo;
  4075. instr->AsProfiledInstr()->u.LdLenInfo().arrayType = arrayType;
  4076. }
  4077. this->AddInstr(instr, offset);
  4078. if (wasNotProfiled && DoBailOnNoProfile())
  4079. {
  4080. InsertBailOnNoProfile(instr);
  4081. }
  4082. }
  4083. ///----------------------------------------------------------------------------
  4084. ///
  4085. /// IRBuilder::BuildElementC2
  4086. ///
  4087. /// Build IR instr for an ElementC2 instruction.
  4088. ///
  4089. ///----------------------------------------------------------------------------
  4090. template <typename SizePolicy>
  4091. void
  4092. IRBuilder::BuildElementScopedC2(Js::OpCode newOpcode, uint32 offset)
  4093. {
  4094. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  4095. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementScopedC2<SizePolicy>>();
  4096. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  4097. {
  4098. this->DoClosureRegCheck(layout->Value);
  4099. this->DoClosureRegCheck(layout->Value2);
  4100. }
  4101. BuildElementScopedC2(newOpcode, offset, layout->Value2, layout->Value, layout->PropertyIdIndex);
  4102. }
  4103. void
  4104. IRBuilder::BuildElementScopedC2(Js::OpCode newOpcode, uint32 offset, Js::RegSlot value2Slot,
  4105. Js::RegSlot regSlot, Js::PropertyIdIndexType propertyIdIndex)
  4106. {
  4107. IR::Instr * instr = nullptr;
  4108. Js::PropertyId propertyId;
  4109. IR::RegOpnd * regOpnd;
  4110. IR::RegOpnd * value2Opnd;
  4111. IR::SymOpnd * fieldSymOpnd;
  4112. Js::RegSlot instanceSlot = this->GetEnvRegForEvalCode();
  4113. switch (newOpcode)
  4114. {
  4115. case Js::OpCode::ScopedLdInst:
  4116. {
  4117. propertyId = m_func->GetJITFunctionBody()->GetReferencedPropertyId(propertyIdIndex);
  4118. fieldSymOpnd = this->BuildFieldOpnd(newOpcode, instanceSlot, propertyId, propertyIdIndex, PropertyKindData);
  4119. regOpnd = this->BuildDstOpnd(regSlot);
  4120. value2Opnd = this->BuildDstOpnd(value2Slot);
  4121. IR::Instr *newInstr = IR::Instr::New(Js::OpCode::Unused, value2Opnd, m_func);
  4122. this->AddInstr(newInstr, offset);
  4123. instr = IR::Instr::New(newOpcode, regOpnd, fieldSymOpnd, newInstr->GetDst(), m_func);
  4124. this->AddInstr(instr, offset);
  4125. }
  4126. break;
  4127. default:
  4128. AssertMsg(UNREACHED, "Unknown ElementC2 opcode");
  4129. Fatal();
  4130. }
  4131. }
  4132. template <typename SizePolicy>
  4133. void
  4134. IRBuilder::BuildElementC2(Js::OpCode newOpcode, uint32 offset)
  4135. {
  4136. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  4137. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementC2<SizePolicy>>();
  4138. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  4139. {
  4140. this->DoClosureRegCheck(layout->Value);
  4141. this->DoClosureRegCheck(layout->Value2);
  4142. this->DoClosureRegCheck(layout->Instance);
  4143. }
  4144. BuildElementC2(newOpcode, offset, layout->Instance, layout->Value2, layout->Value, layout->PropertyIdIndex);
  4145. }
  4146. void
  4147. IRBuilder::BuildElementC2(Js::OpCode newOpcode, uint32 offset, Js::RegSlot instanceSlot, Js::RegSlot value2Slot,
  4148. Js::RegSlot regSlot, Js::PropertyIdIndexType propertyIdIndex)
  4149. {
  4150. IR::Instr * instr = nullptr;
  4151. Js::PropertyId propertyId;
  4152. IR::RegOpnd * regOpnd;
  4153. IR::RegOpnd * value2Opnd;
  4154. IR::SymOpnd * fieldSymOpnd;
  4155. switch (newOpcode)
  4156. {
  4157. case Js::OpCode::ProfiledLdSuperFld:
  4158. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(newOpcode);
  4159. // fall-through
  4160. case Js::OpCode::LdSuperFld:
  4161. {
  4162. propertyId = m_func->GetJITFunctionBody()->GetPropertyIdFromCacheId(propertyIdIndex);
  4163. fieldSymOpnd = this->BuildFieldOpnd(newOpcode, instanceSlot, propertyId, (Js::PropertyIdIndexType) - 1, PropertyKindData, propertyIdIndex);
  4164. if (fieldSymOpnd->IsPropertySymOpnd())
  4165. {
  4166. fieldSymOpnd->AsPropertySymOpnd()->TryDisableRuntimePolymorphicCache();
  4167. }
  4168. value2Opnd = this->BuildSrcOpnd(value2Slot);
  4169. regOpnd = this->BuildDstOpnd(regSlot);
  4170. instr = IR::ProfiledInstr::New(newOpcode, regOpnd, fieldSymOpnd, value2Opnd, m_func);
  4171. this->AddInstr(instr, offset);
  4172. }
  4173. break;
  4174. case Js::OpCode::ProfiledStSuperFld:
  4175. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(newOpcode);
  4176. // fall-through
  4177. case Js::OpCode::StSuperFld:
  4178. {
  4179. propertyId = m_func->GetJITFunctionBody()->GetPropertyIdFromCacheId(propertyIdIndex);
  4180. fieldSymOpnd = this->BuildFieldOpnd(newOpcode, instanceSlot, propertyId, (Js::PropertyIdIndexType) - 1, PropertyKindData, propertyIdIndex);
  4181. if (fieldSymOpnd->IsPropertySymOpnd())
  4182. {
  4183. fieldSymOpnd->AsPropertySymOpnd()->TryDisableRuntimePolymorphicCache();
  4184. }
  4185. regOpnd = this->BuildSrcOpnd(regSlot);
  4186. value2Opnd = this->BuildSrcOpnd(value2Slot);
  4187. instr = IR::ProfiledInstr::New(newOpcode, fieldSymOpnd, regOpnd, value2Opnd, m_func);
  4188. this->AddInstr(instr, offset);
  4189. break;
  4190. }
  4191. default:
  4192. AssertMsg(UNREACHED, "Unknown ElementC2 opcode");
  4193. Fatal();
  4194. }
  4195. }
  4196. ///----------------------------------------------------------------------------
  4197. ///
  4198. /// IRBuilder::BuildElementU
  4199. ///
  4200. /// Build IR instr for an ElementU or ElementRootU instruction.
  4201. ///
  4202. ///----------------------------------------------------------------------------
  4203. template <typename SizePolicy>
  4204. void
  4205. IRBuilder::BuildElementU(Js::OpCode newOpcode, uint32 offset)
  4206. {
  4207. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  4208. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  4209. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementU<SizePolicy>>();
  4210. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  4211. {
  4212. this->DoClosureRegCheck(layout->Instance);
  4213. }
  4214. BuildElementU(newOpcode, offset, layout->Instance, layout->PropertyIdIndex);
  4215. }
  4216. template <typename SizePolicy>
  4217. void
  4218. IRBuilder::BuildElementRootU(Js::OpCode newOpcode, uint32 offset)
  4219. {
  4220. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  4221. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  4222. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementRootU<SizePolicy>>();
  4223. BuildElementU(newOpcode, offset, Js::FunctionBody::RootObjectRegSlot, layout->PropertyIdIndex);
  4224. }
  4225. template <typename SizePolicy>
  4226. void
  4227. IRBuilder::BuildElementScopedU(Js::OpCode newOpcode, uint32 offset)
  4228. {
  4229. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  4230. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  4231. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementScopedU<SizePolicy>>();
  4232. BuildElementU(newOpcode, offset, GetEnvReg(), layout->PropertyIdIndex);
  4233. }
  4234. void
  4235. IRBuilder::BuildElementU(Js::OpCode newOpcode, uint32 offset, Js::RegSlot instance, Js::PropertyIdIndexType propertyIdIndex)
  4236. {
  4237. IR::Instr * instr;
  4238. IR::RegOpnd * regOpnd;
  4239. IR::SymOpnd * fieldSymOpnd;
  4240. Js::PropertyId propertyId = m_func->GetJITFunctionBody()->GetReferencedPropertyId(propertyIdIndex);
  4241. switch (newOpcode)
  4242. {
  4243. case Js::OpCode::LdLocalElemUndef:
  4244. if (m_func->GetLocalClosureSym()->HasByteCodeRegSlot())
  4245. {
  4246. IR::ByteCodeUsesInstr * byteCodeUse = IR::ByteCodeUsesInstr::New(m_func, offset);
  4247. byteCodeUse->SetNonOpndSymbol(m_func->GetLocalClosureSym()->m_id);
  4248. this->AddInstr(byteCodeUse, offset);
  4249. }
  4250. instance = m_func->GetJITFunctionBody()->GetLocalClosureReg();
  4251. newOpcode = Js::OpCode::LdElemUndef;
  4252. fieldSymOpnd = this->BuildFieldOpnd(newOpcode, instance, propertyId, propertyIdIndex, PropertyKindData);
  4253. instr = IR::Instr::New(newOpcode, fieldSymOpnd, m_func);
  4254. break;
  4255. // fall through
  4256. case Js::OpCode::LdElemUndefScoped:
  4257. {
  4258. // Store
  4259. PropertyKind propertyKind = PropertyKindData;
  4260. fieldSymOpnd = this->BuildFieldOpnd(newOpcode, instance, propertyId, propertyIdIndex, propertyKind);
  4261. // Implicit root object as default instance
  4262. regOpnd = this->BuildSrcOpnd(Js::FunctionBody::RootObjectRegSlot);
  4263. instr = IR::Instr::New(newOpcode, fieldSymOpnd, regOpnd, m_func);
  4264. break;
  4265. }
  4266. case Js::OpCode::ClearAttributes:
  4267. {
  4268. instr = IR::Instr::New(newOpcode, m_func);
  4269. IR::RegOpnd * src1Opnd = this->BuildSrcOpnd(instance);
  4270. IR::IntConstOpnd * src2Opnd = IR::IntConstOpnd::New(propertyId, TyInt32, m_func);
  4271. instr->SetSrc1(src1Opnd);
  4272. instr->SetSrc2(src2Opnd);
  4273. break;
  4274. }
  4275. case Js::OpCode::StLocalFuncExpr:
  4276. fieldSymOpnd = this->BuildFieldOpnd(newOpcode, m_func->GetJITFunctionBody()->GetLocalClosureReg(), propertyId, propertyIdIndex, PropertyKindData);
  4277. regOpnd = this->BuildSrcOpnd(instance);
  4278. newOpcode = Js::OpCode::StFuncExpr;
  4279. instr = IR::Instr::New(newOpcode, fieldSymOpnd, regOpnd, m_func);
  4280. break;
  4281. case Js::OpCode::DeleteLocalFld:
  4282. newOpcode = Js::OpCode::DeleteFld;
  4283. fieldSymOpnd = BuildFieldOpnd(newOpcode, m_func->GetJITFunctionBody()->GetLocalClosureReg(), propertyId, propertyIdIndex, PropertyKindData);
  4284. regOpnd = BuildDstOpnd(instance);
  4285. instr = IR::Instr::New(newOpcode, regOpnd, fieldSymOpnd, m_func);
  4286. break;
  4287. default:
  4288. {
  4289. fieldSymOpnd = this->BuildFieldOpnd(newOpcode, instance, propertyId, propertyIdIndex, PropertyKindData);
  4290. instr = IR::Instr::New(newOpcode, fieldSymOpnd, m_func);
  4291. break;
  4292. }
  4293. }
  4294. this->AddInstr(instr, offset);
  4295. }
  4296. ///----------------------------------------------------------------------------
  4297. ///
  4298. /// IRBuilder::BuildAuxiliary
  4299. ///
  4300. /// Build IR instr for an Auxiliary instruction.
  4301. ///
  4302. ///----------------------------------------------------------------------------
  4303. void
  4304. IRBuilder::BuildAuxNoReg(Js::OpCode newOpcode, uint32 offset)
  4305. {
  4306. Assert(!OpCodeAttr::HasMultiSizeLayout(newOpcode));
  4307. IR::Instr * instr;
  4308. const unaligned Js::OpLayoutAuxNoReg *auxInsn = m_jnReader.AuxNoReg();
  4309. switch (newOpcode)
  4310. {
  4311. case Js::OpCode::InitCachedFuncs:
  4312. {
  4313. IR::Opnd *src1Opnd = this->BuildSrcOpnd(m_func->GetJITFunctionBody()->GetLocalClosureReg());
  4314. IR::Opnd *src2Opnd = this->BuildSrcOpnd(m_func->GetJITFunctionBody()->GetLocalFrameDisplayReg());
  4315. IR::Opnd *src3Opnd = this->BuildAuxArrayOpnd(AuxArrayValue::AuxFuncInfoArray, auxInsn->Offset);
  4316. instr = IR::Instr::New(Js::OpCode::ArgOut_A, IR::RegOpnd::New(TyVar, m_func), src3Opnd, m_func);
  4317. this->AddInstr(instr, offset);
  4318. instr = IR::Instr::New(Js::OpCode::ArgOut_A, IR::RegOpnd::New(TyVar, m_func), src2Opnd, instr->GetDst(), m_func);
  4319. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  4320. instr = IR::Instr::New(Js::OpCode::ArgOut_A, IR::RegOpnd::New(TyVar, m_func), src1Opnd, instr->GetDst(), m_func);
  4321. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  4322. IR::HelperCallOpnd *helperOpnd;
  4323. helperOpnd = IR::HelperCallOpnd::New(IR::HelperOP_InitCachedFuncs, this->m_func);
  4324. src2Opnd = instr->GetDst();
  4325. instr = IR::Instr::New(Js::OpCode::CallHelper, m_func);
  4326. instr->SetSrc1(helperOpnd);
  4327. instr->SetSrc2(src2Opnd);
  4328. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  4329. return;
  4330. }
  4331. default:
  4332. {
  4333. AssertMsg(UNREACHED, "Unknown AuxNoReg opcode");
  4334. Fatal();
  4335. break;
  4336. }
  4337. }
  4338. }
  4339. void
  4340. IRBuilder::BuildAuxiliary(Js::OpCode newOpcode, uint32 offset)
  4341. {
  4342. Assert(!OpCodeAttr::HasMultiSizeLayout(newOpcode));
  4343. const unaligned Js::OpLayoutAuxiliary *auxInsn = m_jnReader.Auxiliary();
  4344. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  4345. {
  4346. this->DoClosureRegCheck(auxInsn->R0);
  4347. }
  4348. IR::Instr *instr;
  4349. switch (newOpcode)
  4350. {
  4351. case Js::OpCode::NewScObjectLiteral:
  4352. {
  4353. int literalObjectId = auxInsn->C1;
  4354. IR::RegOpnd * dstOpnd;
  4355. IR::Opnd* srcOpnd;
  4356. Js::RegSlot dstRegSlot = auxInsn->R0;
  4357. // The property ID array needs to be both relocatable and available (so we can
  4358. // get the slot capacity), so we need to just pass the offset to lower and let
  4359. // lower take it from there...
  4360. srcOpnd = IR::IntConstOpnd::New(auxInsn->Offset, TyUint32, m_func);
  4361. dstOpnd = this->BuildDstOpnd(dstRegSlot);
  4362. dstOpnd->SetValueType(ValueType::GetObject(ObjectType::UninitializedObject));
  4363. instr = IR::Instr::New(newOpcode, dstOpnd, srcOpnd, m_func);
  4364. // Because we're going to be making decisions based off the value, we have to defer
  4365. // this until we get to lowering.
  4366. instr->SetSrc2(IR::IntConstOpnd::New(literalObjectId, TyUint32, m_func));
  4367. if (dstOpnd->m_sym->m_isSingleDef)
  4368. {
  4369. dstOpnd->m_sym->m_isSafeThis = true;
  4370. }
  4371. break;
  4372. }
  4373. case Js::OpCode::LdPropIds:
  4374. {
  4375. IR::RegOpnd * dstOpnd;
  4376. IR::Opnd* srcOpnd;
  4377. Js::RegSlot dstRegSlot = auxInsn->R0;
  4378. srcOpnd = this->BuildAuxArrayOpnd(AuxArrayValue::AuxPropertyIdArray, auxInsn->Offset);
  4379. dstOpnd = this->BuildDstOpnd(dstRegSlot);
  4380. instr = IR::Instr::New(newOpcode, dstOpnd, srcOpnd, m_func);
  4381. if (dstOpnd->m_sym->m_isSingleDef)
  4382. {
  4383. dstOpnd->m_sym->m_isNotNumber = true;
  4384. }
  4385. break;
  4386. }
  4387. case Js::OpCode::NewScIntArray:
  4388. {
  4389. IR::RegOpnd* dstOpnd;
  4390. IR::Opnd* src1Opnd;
  4391. src1Opnd = this->BuildAuxArrayOpnd(AuxArrayValue::AuxIntArray, auxInsn->Offset);
  4392. dstOpnd = this->BuildDstOpnd(auxInsn->R0);
  4393. instr = IR::Instr::New(newOpcode, dstOpnd, src1Opnd, m_func);
  4394. const Js::TypeId arrayTypeId = m_func->IsJitInDebugMode() ? Js::TypeIds_Array : Js::TypeIds_NativeIntArray;
  4395. dstOpnd->SetValueType(
  4396. ValueType::GetObject(ObjectType::Array).SetHasNoMissingValues(true).SetArrayTypeId(arrayTypeId));
  4397. dstOpnd->SetValueTypeFixed();
  4398. break;
  4399. }
  4400. case Js::OpCode::NewScFltArray:
  4401. {
  4402. IR::RegOpnd* dstOpnd;
  4403. IR::Opnd* src1Opnd;
  4404. src1Opnd = this->BuildAuxArrayOpnd(AuxArrayValue::AuxFloatArray, auxInsn->Offset);
  4405. dstOpnd = this->BuildDstOpnd(auxInsn->R0);
  4406. instr = IR::Instr::New(newOpcode, dstOpnd, src1Opnd, m_func);
  4407. const Js::TypeId arrayTypeId = m_func->IsJitInDebugMode() ? Js::TypeIds_Array : Js::TypeIds_NativeFloatArray;
  4408. dstOpnd->SetValueType(
  4409. ValueType::GetObject(ObjectType::Array).SetHasNoMissingValues(true).SetArrayTypeId(arrayTypeId));
  4410. dstOpnd->SetValueTypeFixed();
  4411. break;
  4412. }
  4413. case Js::OpCode::StArrSegItem_A:
  4414. {
  4415. IR::RegOpnd* src1Opnd;
  4416. IR::Opnd* src2Opnd;
  4417. src1Opnd = this->BuildSrcOpnd(auxInsn->R0);
  4418. src2Opnd = this->BuildAuxArrayOpnd(AuxArrayValue::AuxVarsArray, auxInsn->Offset);
  4419. instr = IR::Instr::New(newOpcode, m_func);
  4420. instr->SetSrc1(src1Opnd);
  4421. instr->SetSrc2(src2Opnd);
  4422. break;
  4423. }
  4424. case Js::OpCode::NewScObject_A:
  4425. {
  4426. const Js::VarArrayVarCount *vars = (Js::VarArrayVarCount *)m_func->GetJITFunctionBody()->ReadFromAuxContextData(auxInsn->Offset);
  4427. int count = Js::TaggedInt::ToInt32(vars->count);
  4428. StackSym * symDst;
  4429. IR::SymOpnd * dstOpnd;
  4430. IR::Opnd * src1Opnd;
  4431. //
  4432. // PUSH all the parameters on the auxiliary context, to the stack
  4433. //
  4434. for (int i=0;i<count; i++)
  4435. {
  4436. m_argsOnStack++;
  4437. symDst = m_func->m_symTable->GetArgSlotSym((uint16)(i + 2));
  4438. if (symDst == nullptr || (uint16)(i + 2) != (i + 2))
  4439. {
  4440. AssertMsg(UNREACHED, "Arg count too big...");
  4441. Fatal();
  4442. }
  4443. dstOpnd = IR::SymOpnd::New(symDst, TyVar, m_func);
  4444. src1Opnd = IR::AddrOpnd::New(vars->elements[i], IR::AddrOpndKindDynamicVar, this->m_func, true);
  4445. instr = IR::Instr::New(Js::OpCode::ArgOut_A, dstOpnd, src1Opnd, m_func);
  4446. this->AddInstr(instr, offset);
  4447. m_argStack->Push(instr);
  4448. }
  4449. BuildCallI_Helper(Js::OpCode::NewScObject, offset, (Js::RegSlot)auxInsn->R0, (Js::RegSlot)auxInsn->C1, (Js::ArgSlot)count+1, Js::Constants::NoProfileId);
  4450. return;
  4451. }
  4452. default:
  4453. {
  4454. AssertMsg(UNREACHED, "Unknown Auxiliary opcode");
  4455. Fatal();
  4456. break;
  4457. }
  4458. }
  4459. this->AddInstr(instr, offset);
  4460. }
  4461. void
  4462. IRBuilder::BuildProfiledAuxiliary(Js::OpCode newOpcode, uint32 offset)
  4463. {
  4464. Assert(!OpCodeAttr::HasMultiSizeLayout(newOpcode));
  4465. const unaligned Js::OpLayoutDynamicProfile<Js::OpLayoutAuxiliary> *auxInsn = m_jnReader.ProfiledAuxiliary();
  4466. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  4467. {
  4468. this->DoClosureRegCheck(auxInsn->R0);
  4469. }
  4470. switch (newOpcode)
  4471. {
  4472. case Js::OpCode::ProfiledNewScIntArray:
  4473. {
  4474. Js::ProfileId profileId = static_cast<Js::ProfileId>(auxInsn->profileId);
  4475. IR::RegOpnd* dstOpnd;
  4476. IR::Opnd* src1Opnd;
  4477. src1Opnd = this->BuildAuxArrayOpnd(AuxArrayValue::AuxIntArray, auxInsn->Offset);
  4478. dstOpnd = this->BuildDstOpnd(auxInsn->R0);
  4479. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(newOpcode);
  4480. IR::Instr *instr;
  4481. Js::ArrayCallSiteInfo *arrayInfo = nullptr;
  4482. Js::TypeId arrayTypeId = Js::TypeIds_Array;
  4483. if (m_func->DoSimpleJitDynamicProfile())
  4484. {
  4485. instr = IR::JitProfilingInstr::New(newOpcode, dstOpnd, src1Opnd, m_func);
  4486. instr->AsJitProfilingInstr()->profileId = profileId;
  4487. }
  4488. else if (m_func->HasArrayInfo())
  4489. {
  4490. instr = IR::ProfiledInstr::New(newOpcode, dstOpnd, src1Opnd, m_func);
  4491. instr->AsProfiledInstr()->u.profileId = profileId;
  4492. arrayInfo = m_func->GetReadOnlyProfileInfo()->GetArrayCallSiteInfo(profileId);
  4493. if (arrayInfo && !m_func->IsJitInDebugMode())
  4494. {
  4495. if (arrayInfo->IsNativeIntArray())
  4496. {
  4497. arrayTypeId = Js::TypeIds_NativeIntArray;
  4498. }
  4499. else if (arrayInfo->IsNativeFloatArray())
  4500. {
  4501. arrayTypeId = Js::TypeIds_NativeFloatArray;
  4502. }
  4503. }
  4504. }
  4505. else
  4506. {
  4507. instr = IR::Instr::New(newOpcode, dstOpnd, src1Opnd, m_func);
  4508. }
  4509. ValueType dstValueType(
  4510. ValueType::GetObject(ObjectType::Array).SetHasNoMissingValues(true).SetArrayTypeId(arrayTypeId));
  4511. if (dstValueType.IsLikelyNativeArray())
  4512. {
  4513. dstOpnd->SetValueType(dstValueType.ToLikely());
  4514. }
  4515. else
  4516. {
  4517. dstOpnd->SetValueType(dstValueType);
  4518. dstOpnd->SetValueTypeFixed();
  4519. }
  4520. StackSym *dstSym = dstOpnd->AsRegOpnd()->m_sym;
  4521. if (dstSym->m_isSingleDef)
  4522. {
  4523. dstSym->m_isSafeThis = true;
  4524. dstSym->m_isNotNumber = true;
  4525. }
  4526. this->AddInstr(instr, offset);
  4527. break;
  4528. }
  4529. case Js::OpCode::ProfiledNewScFltArray:
  4530. {
  4531. Js::ProfileId profileId = static_cast<Js::ProfileId>(auxInsn->profileId);
  4532. IR::RegOpnd* dstOpnd;
  4533. IR::Opnd* src1Opnd;
  4534. src1Opnd = this->BuildAuxArrayOpnd(AuxArrayValue::AuxFloatArray, auxInsn->Offset);
  4535. dstOpnd = this->BuildDstOpnd(auxInsn->R0);
  4536. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(newOpcode);
  4537. IR::Instr *instr;
  4538. Js::ArrayCallSiteInfo *arrayInfo = nullptr;
  4539. if (m_func->DoSimpleJitDynamicProfile())
  4540. {
  4541. instr = IR::JitProfilingInstr::New(newOpcode, dstOpnd, src1Opnd, m_func);
  4542. instr->AsJitProfilingInstr()->profileId = profileId;
  4543. // Keep arrayInfo null because we aren't using profile data in profiling simplejit
  4544. }
  4545. else
  4546. {
  4547. instr = IR::ProfiledInstr::New(newOpcode, dstOpnd, src1Opnd, m_func);
  4548. instr->AsProfiledInstr()->u.profileId = profileId;
  4549. if (m_func->HasArrayInfo()) {
  4550. arrayInfo = m_func->GetReadOnlyProfileInfo()->GetArrayCallSiteInfo(profileId);
  4551. }
  4552. }
  4553. Js::TypeId arrayTypeId;
  4554. if (arrayInfo && arrayInfo->IsNativeFloatArray())
  4555. {
  4556. arrayTypeId = Js::TypeIds_NativeFloatArray;
  4557. }
  4558. else
  4559. {
  4560. arrayTypeId = Js::TypeIds_Array;
  4561. }
  4562. ValueType dstValueType(
  4563. ValueType::GetObject(ObjectType::Array).SetHasNoMissingValues(true).SetArrayTypeId(arrayTypeId));
  4564. if (dstValueType.IsLikelyNativeArray())
  4565. {
  4566. dstOpnd->SetValueType(dstValueType.ToLikely());
  4567. }
  4568. else
  4569. {
  4570. dstOpnd->SetValueType(dstValueType);
  4571. dstOpnd->SetValueTypeFixed();
  4572. }
  4573. StackSym *dstSym = dstOpnd->AsRegOpnd()->m_sym;
  4574. if (dstSym->m_isSingleDef)
  4575. {
  4576. dstSym->m_isSafeThis = true;
  4577. dstSym->m_isNotNumber = true;
  4578. }
  4579. this->AddInstr(instr, offset);
  4580. break;
  4581. }
  4582. default:
  4583. {
  4584. AssertMsg(UNREACHED, "Unknown Auxiliary opcode");
  4585. Fatal();
  4586. break;
  4587. }
  4588. }
  4589. }
  4590. ///----------------------------------------------------------------------------
  4591. ///
  4592. /// IRBuilder::BuildReg2Aux
  4593. ///
  4594. /// Build IR instr for a Reg2Aux instruction.
  4595. ///
  4596. ///----------------------------------------------------------------------------
  4597. void IRBuilder::BuildInitCachedScope(int auxOffset, int offset)
  4598. {
  4599. IR::Instr * instr;
  4600. IR::RegOpnd * dstOpnd;
  4601. IR::RegOpnd * src1Opnd;
  4602. IR::AddrOpnd * src2Opnd;
  4603. IR::Opnd* src3Opnd;
  4604. IR::Opnd* formalsAreLetDeclOpnd;
  4605. src2Opnd = IR::AddrOpnd::New(m_func->GetJITFunctionBody()->GetFormalsPropIdArrayAddr(), IR::AddrOpndKindDynamicMisc, m_func);
  4606. Js::PropertyIdArray * propIds = m_func->GetJITFunctionBody()->GetFormalsPropIdArray();
  4607. src3Opnd = this->BuildAuxObjectLiteralTypeRefOpnd(Js::ActivationObjectEx::GetLiteralObjectRef(propIds));
  4608. dstOpnd = this->BuildDstOpnd(m_func->GetJITFunctionBody()->GetLocalClosureReg());
  4609. formalsAreLetDeclOpnd = IR::IntConstOpnd::New(propIds->hasNonSimpleParams, TyUint8, m_func);
  4610. instr = IR::Instr::New(Js::OpCode::ExtendArg_A, IR::RegOpnd::New(TyVar, m_func), formalsAreLetDeclOpnd, m_func);
  4611. this->AddInstr(instr, offset);
  4612. instr = IR::Instr::New(Js::OpCode::ExtendArg_A, IR::RegOpnd::New(TyVar, m_func), src3Opnd, instr->GetDst(), m_func);
  4613. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  4614. instr = IR::Instr::New(Js::OpCode::ExtendArg_A, IR::RegOpnd::New(TyVar, m_func), src2Opnd, instr->GetDst(), m_func);
  4615. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  4616. // Disable opt that normally gets disabled when we see LdFuncExpr in the byte code.
  4617. m_func->DisableCanDoInlineArgOpt();
  4618. src1Opnd = IR::RegOpnd::New(TyVar, m_func);
  4619. IR::Instr * instrLdFuncExpr = IR::Instr::New(Js::OpCode::LdFuncExpr, src1Opnd, m_func);
  4620. this->AddInstr(instrLdFuncExpr, Js::Constants::NoByteCodeOffset);
  4621. instr = IR::Instr::New(Js::OpCode::ExtendArg_A, IR::RegOpnd::New(TyVar, m_func), src1Opnd, instr->GetDst(), m_func);
  4622. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  4623. instr = IR::Instr::New(Js::OpCode::InitCachedScope, dstOpnd, instr->GetDst(), m_func);
  4624. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  4625. }
  4626. void
  4627. IRBuilder::BuildReg2Aux(Js::OpCode newOpcode, uint32 offset)
  4628. {
  4629. Assert(!OpCodeAttr::HasMultiSizeLayout(newOpcode));
  4630. const unaligned Js::OpLayoutReg2Aux *auxInsn = m_jnReader.Reg2Aux();
  4631. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  4632. {
  4633. this->DoClosureRegCheck(auxInsn->R0);
  4634. this->DoClosureRegCheck(auxInsn->R1);
  4635. }
  4636. IR::Instr *instr;
  4637. switch (newOpcode)
  4638. {
  4639. case Js::OpCode::SpreadArrayLiteral:
  4640. {
  4641. IR::RegOpnd * dstOpnd;
  4642. IR::RegOpnd * src1Opnd;
  4643. IR::Opnd* src2Opnd;
  4644. Js::RegSlot dstRegSlot = auxInsn->R0;
  4645. Js::RegSlot srcRegSlot = auxInsn->R1;
  4646. src1Opnd = this->BuildSrcOpnd(srcRegSlot);
  4647. src2Opnd = this->BuildAuxArrayOpnd(AuxArrayValue::AuxIntArray, auxInsn->Offset);
  4648. dstOpnd = this->BuildDstOpnd(dstRegSlot);
  4649. instr = IR::Instr::New(Js::OpCode::SpreadArrayLiteral, dstOpnd, src1Opnd, src2Opnd, m_func);
  4650. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  4651. if (dstOpnd->m_sym->m_isSingleDef)
  4652. {
  4653. dstOpnd->m_sym->m_isNotNumber = true;
  4654. }
  4655. break;
  4656. }
  4657. default:
  4658. {
  4659. AssertMsg(UNREACHED, "Unknown Reg2Aux opcode");
  4660. Fatal();
  4661. break;
  4662. }
  4663. }
  4664. }
  4665. ///----------------------------------------------------------------------------
  4666. ///
  4667. /// IRBuilder::BuildElementI
  4668. ///
  4669. /// Build IR instr for an ElementI instruction.
  4670. ///
  4671. ///----------------------------------------------------------------------------
  4672. template <typename SizePolicy>
  4673. void
  4674. IRBuilder::BuildElementI(Js::OpCode newOpcode, uint32 offset)
  4675. {
  4676. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  4677. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  4678. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementI<SizePolicy>>();
  4679. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  4680. {
  4681. this->DoClosureRegCheck(layout->Value);
  4682. this->DoClosureRegCheck(layout->Instance);
  4683. this->DoClosureRegCheck(layout->Element);
  4684. }
  4685. BuildElementI(newOpcode, offset, layout->Instance, layout->Element, layout->Value, Js::Constants::NoProfileId);
  4686. }
  4687. template <typename SizePolicy>
  4688. void
  4689. IRBuilder::BuildProfiledElementI(Js::OpCode newOpcode, uint32 offset)
  4690. {
  4691. Assert(OpCodeAttr::IsProfiledOp(newOpcode));
  4692. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  4693. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_ElementI<SizePolicy>>>();
  4694. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  4695. {
  4696. this->DoClosureRegCheck(layout->Value);
  4697. this->DoClosureRegCheck(layout->Instance);
  4698. this->DoClosureRegCheck(layout->Element);
  4699. }
  4700. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(newOpcode);
  4701. BuildElementI(newOpcode, offset, layout->Instance, layout->Element, layout->Value, layout->profileId);
  4702. }
  4703. void
  4704. IRBuilder::BuildElementI(Js::OpCode newOpcode, uint32 offset, Js::RegSlot baseRegSlot, Js::RegSlot indexRegSlot,
  4705. Js::RegSlot regSlot, Js::ProfileId profileId)
  4706. {
  4707. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  4708. ValueType arrayType;
  4709. const Js::LdElemInfo *ldElemInfo = nullptr;
  4710. const Js::StElemInfo *stElemInfo = nullptr;
  4711. bool isProfiledLoad = false;
  4712. bool isProfiledStore = false;
  4713. bool isProfiledInstr = (profileId != Js::Constants::NoProfileId);
  4714. bool isLdElemOrStElemThatWasNotProfiled = false;
  4715. if (isProfiledInstr)
  4716. {
  4717. switch (newOpcode)
  4718. {
  4719. case Js::OpCode::LdElemI_A:
  4720. if (!DoLoadInstructionArrayProfileInfo())
  4721. {
  4722. break;
  4723. }
  4724. ldElemInfo = this->m_func->GetReadOnlyProfileInfo()->GetLdElemInfo(profileId);
  4725. arrayType = ldElemInfo->GetArrayType();
  4726. isLdElemOrStElemThatWasNotProfiled = !ldElemInfo->WasProfiled();
  4727. isProfiledLoad = true;
  4728. break;
  4729. case Js::OpCode::StElemI_A:
  4730. case Js::OpCode::StElemI_A_Strict:
  4731. if (!DoLoadInstructionArrayProfileInfo())
  4732. {
  4733. break;
  4734. }
  4735. isProfiledStore = true;
  4736. stElemInfo = this->m_func->GetReadOnlyProfileInfo()->GetStElemInfo(profileId);
  4737. arrayType = stElemInfo->GetArrayType();
  4738. isLdElemOrStElemThatWasNotProfiled = !stElemInfo->WasProfiled();
  4739. break;
  4740. }
  4741. }
  4742. IR::Instr * instr;
  4743. IR::RegOpnd * regOpnd;
  4744. IR::IndirOpnd * indirOpnd;
  4745. indirOpnd = this->BuildIndirOpnd(this->BuildSrcOpnd(baseRegSlot), this->BuildSrcOpnd(indexRegSlot));
  4746. if (isProfiledLoad || isProfiledStore)
  4747. {
  4748. if(arrayType.IsLikelyNativeArray() && !AllowNativeArrayProfileInfo())
  4749. {
  4750. arrayType = arrayType.SetArrayTypeId(Js::TypeIds_Array);
  4751. // An opnd's value type will get replaced in the forward phase when it is not fixed. Store the array type in the
  4752. // ProfiledInstr.
  4753. if(isProfiledLoad)
  4754. {
  4755. Js::LdElemInfo *const newLdElemInfo = JitAnew(m_func->m_alloc, Js::LdElemInfo, *ldElemInfo);
  4756. newLdElemInfo->arrayType = arrayType;
  4757. ldElemInfo = newLdElemInfo;
  4758. }
  4759. else
  4760. {
  4761. Js::StElemInfo *const newStElemInfo = JitAnew(m_func->m_alloc, Js::StElemInfo, *stElemInfo);
  4762. newStElemInfo->arrayType = arrayType;
  4763. stElemInfo = newStElemInfo;
  4764. }
  4765. }
  4766. indirOpnd->GetBaseOpnd()->SetValueType(arrayType);
  4767. if (m_func->GetTopFunc()->HasTry() && !m_func->GetTopFunc()->DoOptimizeTry())
  4768. {
  4769. isProfiledLoad = false;
  4770. isProfiledStore = false;
  4771. }
  4772. }
  4773. switch (newOpcode)
  4774. {
  4775. case Js::OpCode::LdMethodElem:
  4776. case Js::OpCode::LdElemI_A:
  4777. case Js::OpCode::DeleteElemI_A:
  4778. case Js::OpCode::DeleteElemIStrict_A:
  4779. case Js::OpCode::TypeofElem:
  4780. {
  4781. // Evaluate to register
  4782. regOpnd = this->BuildDstOpnd(regSlot);
  4783. if (m_func->DoSimpleJitDynamicProfile() && isProfiledInstr)
  4784. {
  4785. instr = IR::JitProfilingInstr::New(newOpcode, regOpnd, indirOpnd, m_func);
  4786. instr->AsJitProfilingInstr()->profileId = profileId;
  4787. }
  4788. else if (isProfiledLoad)
  4789. {
  4790. instr = IR::ProfiledInstr::New(newOpcode, regOpnd, indirOpnd, m_func);
  4791. instr->AsProfiledInstr()->u.ldElemInfo = ldElemInfo;
  4792. }
  4793. else
  4794. {
  4795. instr = IR::Instr::New(newOpcode, regOpnd, indirOpnd, m_func);
  4796. }
  4797. break;
  4798. }
  4799. case Js::OpCode::StElemI_A:
  4800. case Js::OpCode::StElemI_A_Strict:
  4801. {
  4802. // Store
  4803. regOpnd = this->BuildSrcOpnd(regSlot);
  4804. if (m_func->DoSimpleJitDynamicProfile() && isProfiledInstr)
  4805. {
  4806. instr = IR::JitProfilingInstr::New(newOpcode, indirOpnd, regOpnd, m_func);
  4807. instr->AsJitProfilingInstr()->profileId = profileId;
  4808. }
  4809. else if (isProfiledStore)
  4810. {
  4811. instr = IR::ProfiledInstr::New(newOpcode, indirOpnd, regOpnd, m_func);
  4812. instr->AsProfiledInstr()->u.stElemInfo = stElemInfo;
  4813. }
  4814. else
  4815. {
  4816. instr = IR::Instr::New(newOpcode, indirOpnd, regOpnd, m_func);
  4817. }
  4818. break;
  4819. }
  4820. case Js::OpCode::InitSetElemI:
  4821. case Js::OpCode::InitGetElemI:
  4822. case Js::OpCode::InitComputedProperty:
  4823. case Js::OpCode::InitClassMemberComputedName:
  4824. case Js::OpCode::InitClassMemberGetComputedName:
  4825. case Js::OpCode::InitClassMemberSetComputedName:
  4826. {
  4827. regOpnd = this->BuildSrcOpnd(regSlot);
  4828. instr = IR::Instr::New(newOpcode, indirOpnd, regOpnd, m_func);
  4829. break;
  4830. }
  4831. default:
  4832. AssertMsg(false, "Unknown ElementI opcode");
  4833. return;
  4834. }
  4835. this->AddInstr(instr, offset);
  4836. if(isLdElemOrStElemThatWasNotProfiled && DoBailOnNoProfile())
  4837. {
  4838. InsertBailOnNoProfile(instr);
  4839. }
  4840. }
  4841. ///----------------------------------------------------------------------------
  4842. ///
  4843. /// IRBuilder::BuildElementUnsigned1
  4844. ///
  4845. /// Build IR instr for an ElementUnsigned1 instruction.
  4846. ///
  4847. ///----------------------------------------------------------------------------
  4848. template <typename SizePolicy>
  4849. void
  4850. IRBuilder::BuildElementUnsigned1(Js::OpCode newOpcode, uint32 offset)
  4851. {
  4852. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  4853. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  4854. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ElementUnsigned1<SizePolicy>>();
  4855. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  4856. {
  4857. this->DoClosureRegCheck(layout->Value);
  4858. this->DoClosureRegCheck(layout->Instance);
  4859. }
  4860. BuildElementUnsigned1(newOpcode, offset, layout->Instance, layout->Element, layout->Value);
  4861. }
  4862. void
  4863. IRBuilder::BuildElementUnsigned1(Js::OpCode newOpcode, uint32 offset, Js::RegSlot baseRegSlot, uint32 index, Js::RegSlot regSlot)
  4864. {
  4865. // This is an array-style access with a constant (integer) index.
  4866. // Embed the index in the indir opnd as a constant offset.
  4867. IR::Instr * instr;
  4868. const bool simpleJit = m_func->DoSimpleJitDynamicProfile();
  4869. IR::RegOpnd * regOpnd;
  4870. IR::IndirOpnd * indirOpnd;
  4871. IR::RegOpnd * baseOpnd;
  4872. Js::OpCode opcode;
  4873. switch (newOpcode)
  4874. {
  4875. case Js::OpCode::StArrItemI_CI4:
  4876. {
  4877. baseOpnd = this->BuildSrcOpnd(baseRegSlot);
  4878. // This instruction must not create missing values in the array
  4879. baseOpnd->SetValueType(
  4880. ValueType::GetObject(ObjectType::Array).SetHasNoMissingValues(false).SetArrayTypeId(Js::TypeIds_Array));
  4881. baseOpnd->SetValueTypeFixed();
  4882. // In the case of simplejit, we won't know the exact type of array used until run time. Due to this,
  4883. // we must use the specialized version of StElemC in Lowering.
  4884. opcode = simpleJit ? Js::OpCode::StElemC : Js::OpCode::StElemI_A;
  4885. break;
  4886. }
  4887. case Js::OpCode::StArrItemC_CI4:
  4888. {
  4889. baseOpnd = IR::RegOpnd::New(TyVar, m_func);
  4890. // Insert LdArrHead as the next instr and clear the offset to avoid duplication.
  4891. IR::RegOpnd *const arrayOpnd = this->BuildSrcOpnd(baseRegSlot);
  4892. // This instruction must not create missing values in the array
  4893. arrayOpnd->SetValueType(
  4894. ValueType::GetObject(ObjectType::Array).SetHasNoMissingValues(false).SetArrayTypeId(Js::TypeIds_Array));
  4895. arrayOpnd->SetValueTypeFixed();
  4896. this->AddInstr(IR::Instr::New(Js::OpCode::LdArrHead, baseOpnd, arrayOpnd, m_func), offset);
  4897. offset = Js::Constants::NoByteCodeOffset;
  4898. opcode = Js::OpCode::StArrSegElemC;
  4899. break;
  4900. }
  4901. case Js::OpCode::StArrSegItem_CI4:
  4902. {
  4903. baseOpnd = this->BuildSrcOpnd(baseRegSlot, TyVar);
  4904. // This instruction must not create missing values in the array
  4905. opcode = Js::OpCode::StArrSegElemC;
  4906. break;
  4907. }
  4908. case Js::OpCode::StArrInlineItem_CI4:
  4909. {
  4910. baseOpnd = this->BuildSrcOpnd(baseRegSlot);
  4911. IR::Opnd *defOpnd = baseOpnd->m_sym->m_instrDef ? baseOpnd->m_sym->m_instrDef->GetDst() : nullptr;
  4912. if (!defOpnd)
  4913. {
  4914. // The array sym may be multi-def because of oddness in the renumbering of temps -- for instance,
  4915. // if there's a loop increment expression whose result is unused (ExprGen only, probably).
  4916. FOREACH_INSTR_BACKWARD(tmpInstr, m_func->m_exitInstr->m_prev)
  4917. {
  4918. if (tmpInstr->GetDst())
  4919. {
  4920. if (tmpInstr->GetDst()->IsEqual(baseOpnd))
  4921. {
  4922. defOpnd = tmpInstr->GetDst();
  4923. break;
  4924. }
  4925. else if (tmpInstr->m_opcode == Js::OpCode::StElemC &&
  4926. tmpInstr->GetDst()->AsIndirOpnd()->GetBaseOpnd()->IsEqual(baseOpnd))
  4927. {
  4928. defOpnd = tmpInstr->GetDst()->AsIndirOpnd()->GetBaseOpnd();
  4929. break;
  4930. }
  4931. }
  4932. }
  4933. NEXT_INSTR_BACKWARD;
  4934. }
  4935. AnalysisAssert(defOpnd);
  4936. // This instruction must not create missing values in the array
  4937. baseOpnd->SetValueType(defOpnd->GetValueType());
  4938. opcode = Js::OpCode::StElemC;
  4939. break;
  4940. }
  4941. default:
  4942. AssertMsg(false, "Unknown ElementUnsigned1 opcode");
  4943. return;
  4944. }
  4945. indirOpnd = this->BuildIndirOpnd(baseOpnd, index);
  4946. regOpnd = this->BuildSrcOpnd(regSlot);
  4947. if (simpleJit)
  4948. {
  4949. instr = IR::JitProfilingInstr::New(opcode, indirOpnd, regOpnd, m_func);
  4950. }
  4951. else if(opcode == Js::OpCode::StElemC && !baseOpnd->GetValueType().IsUninitialized())
  4952. {
  4953. // An opnd's value type will get replaced in the forward phase when it is not fixed. Store the array type in the
  4954. // ProfiledInstr.
  4955. IR::ProfiledInstr *const profiledInstr = IR::ProfiledInstr::New(opcode, indirOpnd, regOpnd, m_func);
  4956. Js::StElemInfo *const stElemInfo = JitAnew(m_func->m_alloc, Js::StElemInfo);
  4957. stElemInfo->arrayType = baseOpnd->GetValueType();
  4958. profiledInstr->u.stElemInfo = stElemInfo;
  4959. instr = profiledInstr;
  4960. }
  4961. else
  4962. {
  4963. instr = IR::Instr::New(opcode, indirOpnd, regOpnd, m_func);
  4964. }
  4965. this->AddInstr(instr, offset);
  4966. }
  4967. ///----------------------------------------------------------------------------
  4968. ///
  4969. /// IRBuilder::BuildArgIn
  4970. ///
  4971. /// Build IR instr for an ArgIn instruction.
  4972. ///
  4973. ///----------------------------------------------------------------------------
  4974. void
  4975. IRBuilder::BuildArgIn0(uint32 offset, Js::RegSlot dstRegSlot)
  4976. {
  4977. Assert(OpCodeAttr::HasMultiSizeLayout(Js::OpCode::ArgIn0));
  4978. BuildArgIn(offset, dstRegSlot, 0);
  4979. }
  4980. void
  4981. IRBuilder::BuildArgIn(uint32 offset, Js::RegSlot dstRegSlot, uint16 argument)
  4982. {
  4983. IR::Instr * instr;
  4984. IR::SymOpnd * srcOpnd;
  4985. IR::RegOpnd * dstOpnd;
  4986. StackSym * symSrc = StackSym::NewParamSlotSym(argument + 1, m_func);
  4987. this->m_func->SetArgOffset(symSrc, (argument + LowererMD::GetFormalParamOffset()) * MachPtr);
  4988. srcOpnd = IR::SymOpnd::New(symSrc, TyVar, m_func);
  4989. dstOpnd = this->BuildDstOpnd(dstRegSlot);
  4990. if (!this->m_func->IsLoopBody() && this->m_func->HasProfileInfo())
  4991. {
  4992. // Skip "this" pointer; "this" profile data is captured by ProfiledLdThis.
  4993. // Subtract 1 to skip "this" pointer, subtract 1 again to get the index to index into profileData->parameterInfo.
  4994. int paramSlotIndex = symSrc->GetParamSlotNum() - 2;
  4995. if (paramSlotIndex >= 0)
  4996. {
  4997. ValueType profiledValueType;
  4998. profiledValueType = this->m_func->GetReadOnlyProfileInfo()->GetParameterInfo(static_cast<Js::ArgSlot>(paramSlotIndex));
  4999. dstOpnd->SetValueType(profiledValueType);
  5000. }
  5001. }
  5002. instr = IR::Instr::New(Js::OpCode::ArgIn_A, dstOpnd, srcOpnd, m_func);
  5003. this->AddInstr(instr, offset);
  5004. }
  5005. void
  5006. IRBuilder::BuildArgInRest()
  5007. {
  5008. IR::RegOpnd * dstOpnd = this->BuildDstOpnd(m_func->GetJITFunctionBody()->GetRestParamRegSlot());
  5009. IR::Instr *instr = IR::Instr::New(Js::OpCode::ArgIn_Rest, dstOpnd, m_func);
  5010. this->AddInstr(instr, (uint32)-1);
  5011. }
  5012. ///----------------------------------------------------------------------------
  5013. ///
  5014. /// IRBuilder::BuildArg
  5015. ///
  5016. /// Build IR instr for an ArgOut instruction.
  5017. ///
  5018. ///----------------------------------------------------------------------------
  5019. template <typename SizePolicy>
  5020. void
  5021. IRBuilder::BuildArgNoSrc(Js::OpCode newOpcode, uint32 offset)
  5022. {
  5023. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  5024. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5025. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_ArgNoSrc<SizePolicy>>();
  5026. BuildArg(Js::OpCode::ArgOut_A, offset, layout->Arg, this->GetEnvRegForInnerFrameDisplay());
  5027. }
  5028. template <typename SizePolicy>
  5029. void
  5030. IRBuilder::BuildArg(Js::OpCode newOpcode, uint32 offset)
  5031. {
  5032. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  5033. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5034. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_Arg<SizePolicy>>();
  5035. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5036. {
  5037. this->DoClosureRegCheck(layout->Reg);
  5038. }
  5039. BuildArg(newOpcode, offset, layout->Arg, layout->Reg);
  5040. }
  5041. template <typename SizePolicy>
  5042. void
  5043. IRBuilder::BuildProfiledArg(Js::OpCode newOpcode, uint32 offset)
  5044. {
  5045. Assert(OpCodeAttr::IsProfiledOp(newOpcode));
  5046. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5047. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_Arg<SizePolicy>>>();
  5048. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5049. {
  5050. this->DoClosureRegCheck(layout->Reg);
  5051. }
  5052. newOpcode = Js::OpCode::ArgOut_A;
  5053. BuildArg(newOpcode, offset, layout->Arg, layout->Reg);
  5054. }
  5055. void
  5056. IRBuilder::BuildArg(Js::OpCode newOpcode, uint32 offset, Js::ArgSlot argument, Js::RegSlot srcRegSlot)
  5057. {
  5058. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5059. IR::Instr * instr;
  5060. IRType type = TyVar;
  5061. if (newOpcode == Js::OpCode::ArgOut_ANonVar)
  5062. {
  5063. newOpcode = Js::OpCode::ArgOut_A;
  5064. type = TyMachPtr;
  5065. }
  5066. m_argsOnStack++;
  5067. StackSym * symDst;
  5068. Assert(argument < USHRT_MAX);
  5069. symDst = m_func->m_symTable->GetArgSlotSym((uint16)(argument+1));
  5070. if (symDst == nullptr || (uint16)(argument + 1) != (argument + 1))
  5071. {
  5072. AssertMsg(UNREACHED, "Arg count too big...");
  5073. Fatal();
  5074. }
  5075. IR::SymOpnd * dstOpnd = IR::SymOpnd::New(symDst, type, m_func);
  5076. IR::RegOpnd * src1Opnd = this->BuildSrcOpnd(srcRegSlot, type);
  5077. instr = IR::Instr::New(newOpcode, dstOpnd, src1Opnd, m_func);
  5078. this->AddInstr(instr, offset);
  5079. m_argStack->Push(instr);
  5080. }
  5081. ///----------------------------------------------------------------------------
  5082. ///
  5083. /// IRBuilder::BuildStartCall
  5084. ///
  5085. /// Build IR instr for a StartCall instruction.
  5086. ///
  5087. ///----------------------------------------------------------------------------
  5088. void
  5089. IRBuilder::BuildStartCall(Js::OpCode newOpcode, uint32 offset)
  5090. {
  5091. Assert(newOpcode == Js::OpCode::StartCall);
  5092. const unaligned Js::OpLayoutStartCall * regLayout = m_jnReader.StartCall();
  5093. Js::ArgSlot ArgCount = regLayout->ArgCount;
  5094. IR::Instr * instr;
  5095. IR::RegOpnd * dstOpnd;
  5096. // Dst of StartCall is always r0... Let's give it a new dst such that it can
  5097. // be singleDef.
  5098. dstOpnd = IR::RegOpnd::New(TyVar, m_func);
  5099. #if DBG
  5100. m_callsOnStack++;
  5101. #endif
  5102. IntConstType value = ArgCount;
  5103. IR::IntConstOpnd * srcOpnd;
  5104. srcOpnd = IR::IntConstOpnd::New(value, TyInt32, m_func);
  5105. instr = IR::Instr::New(newOpcode, dstOpnd, srcOpnd, m_func);
  5106. this->AddInstr(instr, offset);
  5107. // Keep a stack of arg instructions such that we can link them up once we see
  5108. // the call that consumes them.
  5109. m_argStack->Push(instr);
  5110. }
  5111. ///----------------------------------------------------------------------------
  5112. ///
  5113. /// IRBuilder::BuildCallI
  5114. ///
  5115. /// Build IR instr for a CallI instruction.
  5116. ///
  5117. ///----------------------------------------------------------------------------
  5118. template <typename SizePolicy>
  5119. void
  5120. IRBuilder::BuildCallI(Js::OpCode newOpcode, uint32 offset)
  5121. {
  5122. Assert(Js::OpCodeUtil::IsCallOp(newOpcode) || newOpcode == Js::OpCode::NewScObject || newOpcode == Js::OpCode::NewScObjArray);
  5123. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5124. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_CallI<SizePolicy>>();
  5125. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5126. {
  5127. this->DoClosureRegCheck(layout->Return);
  5128. this->DoClosureRegCheck(layout->Function);
  5129. }
  5130. BuildCallI_Helper(newOpcode, offset, layout->Return, layout->Function, layout->ArgCount, Js::Constants::NoProfileId);
  5131. }
  5132. template <typename SizePolicy>
  5133. void
  5134. IRBuilder::BuildCallIFlags(Js::OpCode newOpcode, uint32 offset)
  5135. {
  5136. Assert(Js::OpCodeUtil::IsCallOp(newOpcode) || newOpcode == Js::OpCode::NewScObject || newOpcode == Js::OpCode::NewScObjArray);
  5137. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5138. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_CallIFlags<SizePolicy>>();
  5139. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5140. {
  5141. this->DoClosureRegCheck(layout->Return);
  5142. this->DoClosureRegCheck(layout->Function);
  5143. }
  5144. IR::Instr* instr = BuildCallI_Helper(newOpcode, offset, layout->Return, layout->Function, layout->ArgCount, Js::Constants::NoProfileId, layout->callFlags);
  5145. Assert(instr->m_opcode == Js::OpCode::CallIFlags);
  5146. if (instr->m_opcode == Js::OpCode::CallIFlags)
  5147. {
  5148. instr->m_opcode =
  5149. layout->callFlags == (Js::CallFlags::CallFlags_NewTarget | Js::CallFlags::CallFlags_New | Js::CallFlags::CallFlags_ExtraArg) ? Js::OpCode::CallINewTargetNew :
  5150. layout->callFlags == Js::CallFlags::CallFlags_New ? Js::OpCode::CallINew :
  5151. instr->m_opcode;
  5152. }
  5153. }
  5154. void IRBuilder::BuildLdSpreadIndices(uint32 offset, uint32 spreadAuxOffset)
  5155. {
  5156. // Link up the LdSpreadIndices instr to be the first in the arg chain. This will allow us to find it in Lowerer easier.
  5157. IR::Opnd *auxArg = this->BuildAuxArrayOpnd(AuxArrayValue::AuxIntArray, spreadAuxOffset);
  5158. IR::Instr *instr = IR::Instr::New(Js::OpCode::LdSpreadIndices, m_func);
  5159. instr->SetSrc1(auxArg);
  5160. // Create the link to the first arg.
  5161. Js::RegSlot lastArg = m_argStack->Head()->GetDst()->AsSymOpnd()->GetStackSym()->GetArgSlotNum();
  5162. instr->SetDst(IR::SymOpnd::New(m_func->m_symTable->GetArgSlotSym((uint16) (lastArg + 1)), TyVar, m_func));
  5163. this->AddInstr(instr, Js::Constants::NoByteCodeOffset);
  5164. m_argStack->Push(instr);
  5165. }
  5166. template <typename SizePolicy>
  5167. void
  5168. IRBuilder::BuildCallIExtended(Js::OpCode newOpcode, uint32 offset)
  5169. {
  5170. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5171. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_CallIExtended<SizePolicy>>();
  5172. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5173. {
  5174. this->DoClosureRegCheck(layout->Return);
  5175. this->DoClosureRegCheck(layout->Function);
  5176. }
  5177. BuildCallIExtended(newOpcode, offset, layout->Return, layout->Function, layout->ArgCount, layout->Options, layout->SpreadAuxOffset);
  5178. }
  5179. IR::Instr*
  5180. IRBuilder::BuildCallIExtended(Js::OpCode newOpcode, uint32 offset, Js::RegSlot returnValue, Js::RegSlot function,
  5181. Js::ArgSlot argCount, Js::CallIExtendedOptions options, uint32 spreadAuxOffset, Js::CallFlags flags)
  5182. {
  5183. if (options & Js::CallIExtended_SpreadArgs)
  5184. {
  5185. BuildLdSpreadIndices(offset, spreadAuxOffset);
  5186. }
  5187. return BuildCallI_Helper(newOpcode, offset, returnValue, function, argCount, Js::Constants::NoProfileId, flags);
  5188. }
  5189. template <typename SizePolicy>
  5190. void
  5191. IRBuilder::BuildCallIWithICIndex(Js::OpCode newOpcode, uint32 offset)
  5192. {
  5193. AssertMsg(false, "NYI");
  5194. }
  5195. template <typename SizePolicy>
  5196. void
  5197. IRBuilder::BuildCallIFlagsWithICIndex(Js::OpCode newOpcode, uint32 offset)
  5198. {
  5199. AssertMsg(false, "NYI");
  5200. }
  5201. template <typename SizePolicy>
  5202. void
  5203. IRBuilder::BuildCallIExtendedFlags(Js::OpCode newOpcode, uint32 offset)
  5204. {
  5205. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5206. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_CallIExtendedFlags<SizePolicy>>();
  5207. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5208. {
  5209. this->DoClosureRegCheck(layout->Return);
  5210. this->DoClosureRegCheck(layout->Function);
  5211. }
  5212. IR::Instr* instr = BuildCallIExtended(newOpcode, offset, layout->Return, layout->Function, layout->ArgCount, layout->Options, layout->SpreadAuxOffset, layout->callFlags);
  5213. Assert(instr->m_opcode == Js::OpCode::CallIExtendedFlags);
  5214. if (instr->m_opcode == Js::OpCode::CallIExtendedFlags)
  5215. {
  5216. instr->m_opcode =
  5217. layout->callFlags == Js::CallFlags::CallFlags_ExtraArg ? Js::OpCode::CallIEval :
  5218. layout->callFlags == Js::CallFlags::CallFlags_New ? Js::OpCode::CallIExtendedNew :
  5219. layout->callFlags == (Js::CallFlags::CallFlags_New | Js::CallFlags::CallFlags_ExtraArg | Js::CallFlags::CallFlags_NewTarget) ? Js::OpCode::CallIExtendedNewTargetNew :
  5220. instr->m_opcode;
  5221. }
  5222. }
  5223. template <typename SizePolicy>
  5224. void
  5225. IRBuilder::BuildCallIExtendedWithICIndex(Js::OpCode newOpcode, uint32 offset)
  5226. {
  5227. AssertMsg(false, "NYI");
  5228. }
  5229. template <typename SizePolicy>
  5230. void
  5231. IRBuilder::BuildCallIExtendedFlagsWithICIndex(Js::OpCode newOpcode, uint32 offset)
  5232. {
  5233. AssertMsg(false, "NYI");
  5234. }
  5235. template <typename SizePolicy>
  5236. void
  5237. IRBuilder::BuildProfiledCallIFlagsWithICIndex(Js::OpCode newOpcode, uint32 offset)
  5238. {
  5239. Assert(OpCodeAttr::IsProfiledOpWithICIndex(newOpcode) || Js::OpCodeUtil::IsProfiledCallOpWithICIndex(newOpcode));
  5240. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5241. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_CallIFlagsWithICIndex<SizePolicy>>>();
  5242. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5243. {
  5244. this->DoClosureRegCheck(layout->Return);
  5245. this->DoClosureRegCheck(layout->Function);
  5246. }
  5247. IR::Instr* instr = BuildProfiledCallIWithICIndex(newOpcode, offset, layout->Return, layout->Function, layout->ArgCount, layout->profileId, layout->inlineCacheIndex);
  5248. Assert(instr->m_opcode == Js::OpCode::CallIFlags);
  5249. if (instr->m_opcode == Js::OpCode::CallIFlags)
  5250. {
  5251. instr->m_opcode =
  5252. layout->callFlags == (Js::CallFlags::CallFlags_NewTarget | Js::CallFlags::CallFlags_New | Js::CallFlags::CallFlags_ExtraArg) ? Js::OpCode::CallINewTargetNew :
  5253. layout->callFlags == Js::CallFlags::CallFlags_New ? Js::OpCode::CallINew :
  5254. instr->m_opcode;
  5255. }
  5256. }
  5257. template <typename SizePolicy>
  5258. void
  5259. IRBuilder::BuildProfiledCallIWithICIndex(Js::OpCode newOpcode, uint32 offset)
  5260. {
  5261. Assert(OpCodeAttr::IsProfiledOpWithICIndex(newOpcode) || Js::OpCodeUtil::IsProfiledCallOpWithICIndex(newOpcode));
  5262. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5263. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_CallIWithICIndex<SizePolicy>>>();
  5264. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5265. {
  5266. this->DoClosureRegCheck(layout->Return);
  5267. this->DoClosureRegCheck(layout->Function);
  5268. }
  5269. BuildProfiledCallIWithICIndex(newOpcode, offset, layout->Return, layout->Function, layout->ArgCount, layout->profileId, layout->inlineCacheIndex);
  5270. }
  5271. IR::Instr*
  5272. IRBuilder::BuildProfiledCallIWithICIndex(Js::OpCode opcode, uint32 offset, Js::RegSlot returnValue, Js::RegSlot function,
  5273. Js::ArgSlot argCount, Js::ProfileId profileId, Js::InlineCacheIndex inlineCacheIndex)
  5274. {
  5275. return BuildProfiledCallI(opcode, offset, returnValue, function, argCount, profileId, Js::CallFlags_None, inlineCacheIndex);
  5276. }
  5277. template <typename SizePolicy>
  5278. void
  5279. IRBuilder::BuildProfiledCallIExtendedFlags(Js::OpCode newOpcode, uint32 offset)
  5280. {
  5281. Assert(OpCodeAttr::IsProfiledOp(newOpcode) || Js::OpCodeUtil::IsProfiledCallOp(newOpcode)
  5282. || Js::OpCodeUtil::IsProfiledReturnTypeCallOp(newOpcode));
  5283. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5284. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_CallIExtendedFlags<SizePolicy>>>();
  5285. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5286. {
  5287. this->DoClosureRegCheck(layout->Return);
  5288. this->DoClosureRegCheck(layout->Function);
  5289. }
  5290. IR::Instr* instr = BuildProfiledCallIExtended(newOpcode, offset, layout->Return, layout->Function, layout->ArgCount, layout->profileId, layout->Options, layout->SpreadAuxOffset, layout->callFlags);
  5291. Assert(instr->m_opcode == Js::OpCode::CallIExtendedFlags);
  5292. if (instr->m_opcode == Js::OpCode::CallIExtendedFlags)
  5293. {
  5294. instr->m_opcode =
  5295. layout->callFlags == Js::CallFlags::CallFlags_ExtraArg ? Js::OpCode::CallIEval :
  5296. layout->callFlags == Js::CallFlags::CallFlags_New ? Js::OpCode::CallIExtendedNew :
  5297. layout->callFlags == (Js::CallFlags::CallFlags_New | Js::CallFlags::CallFlags_ExtraArg | Js::CallFlags::CallFlags_NewTarget) ? Js::OpCode::CallIExtendedNewTargetNew :
  5298. instr->m_opcode;
  5299. }
  5300. }
  5301. template <typename SizePolicy>
  5302. void
  5303. IRBuilder::BuildProfiledCallIExtendedWithICIndex(Js::OpCode newOpcode, uint32 offset)
  5304. {
  5305. Assert(OpCodeAttr::IsProfiledOpWithICIndex(newOpcode) || Js::OpCodeUtil::IsProfiledCallOpWithICIndex(newOpcode));
  5306. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5307. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_CallIExtendedWithICIndex<SizePolicy>>>();
  5308. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5309. {
  5310. this->DoClosureRegCheck(layout->Return);
  5311. this->DoClosureRegCheck(layout->Function);
  5312. }
  5313. BuildProfiledCallIExtendedWithICIndex(newOpcode, offset, layout->Return, layout->Function, layout->ArgCount, layout->profileId, layout->Options, layout->SpreadAuxOffset);
  5314. }
  5315. void
  5316. IRBuilder::BuildProfiledCallIExtendedWithICIndex(Js::OpCode opcode, uint32 offset, Js::RegSlot returnValue, Js::RegSlot function,
  5317. Js::ArgSlot argCount, Js::ProfileId profileId, Js::CallIExtendedOptions options, uint32 spreadAuxOffset)
  5318. {
  5319. BuildProfiledCallIExtended(opcode, offset, returnValue, function, argCount, profileId, options, spreadAuxOffset);
  5320. }
  5321. template <typename SizePolicy>
  5322. void
  5323. IRBuilder::BuildProfiledCallIExtendedFlagsWithICIndex(Js::OpCode newOpcode, uint32 offset)
  5324. {
  5325. Assert(OpCodeAttr::IsProfiledOpWithICIndex(newOpcode) || Js::OpCodeUtil::IsProfiledCallOpWithICIndex(newOpcode));
  5326. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5327. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_CallIExtendedFlagsWithICIndex<SizePolicy>>>();
  5328. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5329. {
  5330. this->DoClosureRegCheck(layout->Return);
  5331. this->DoClosureRegCheck(layout->Function);
  5332. }
  5333. IR::Instr* instr = BuildProfiledCallIExtended(newOpcode, offset, layout->Return, layout->Function, layout->ArgCount, layout->profileId, layout->Options, layout->SpreadAuxOffset);
  5334. Assert(instr->m_opcode == Js::OpCode::CallIExtendedFlags);
  5335. if (instr->m_opcode == Js::OpCode::CallIExtendedFlags)
  5336. {
  5337. instr->m_opcode =
  5338. layout->callFlags == Js::CallFlags::CallFlags_ExtraArg ? Js::OpCode::CallIEval :
  5339. layout->callFlags == Js::CallFlags::CallFlags_New ? Js::OpCode::CallIExtendedNew :
  5340. layout->callFlags == (Js::CallFlags::CallFlags_New | Js::CallFlags::CallFlags_ExtraArg | Js::CallFlags::CallFlags_NewTarget) ? Js::OpCode::CallIExtendedNewTargetNew :
  5341. instr->m_opcode;
  5342. }
  5343. }
  5344. template <typename SizePolicy>
  5345. void
  5346. IRBuilder::BuildProfiledCallI(Js::OpCode newOpcode, uint32 offset)
  5347. {
  5348. Assert(OpCodeAttr::IsProfiledOp(newOpcode) || Js::OpCodeUtil::IsProfiledCallOp(newOpcode)
  5349. || Js::OpCodeUtil::IsProfiledReturnTypeCallOp(newOpcode));
  5350. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5351. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_CallI<SizePolicy>>>();
  5352. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5353. {
  5354. this->DoClosureRegCheck(layout->Return);
  5355. this->DoClosureRegCheck(layout->Function);
  5356. }
  5357. BuildProfiledCallI(newOpcode, offset, layout->Return, layout->Function, layout->ArgCount, layout->profileId);
  5358. }
  5359. template <typename SizePolicy>
  5360. void
  5361. IRBuilder::BuildProfiledCallIFlags(Js::OpCode newOpcode, uint32 offset)
  5362. {
  5363. Assert(OpCodeAttr::IsProfiledOp(newOpcode) || Js::OpCodeUtil::IsProfiledCallOp(newOpcode)
  5364. || Js::OpCodeUtil::IsProfiledReturnTypeCallOp(newOpcode));
  5365. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5366. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_CallIFlags<SizePolicy>>>();
  5367. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5368. {
  5369. this->DoClosureRegCheck(layout->Return);
  5370. this->DoClosureRegCheck(layout->Function);
  5371. }
  5372. IR::Instr* instr = BuildProfiledCallI(newOpcode, offset, layout->Return, layout->Function, layout->ArgCount, layout->profileId, layout->callFlags);
  5373. Assert(instr->m_opcode == Js::OpCode::CallIFlags);
  5374. if (instr->m_opcode == Js::OpCode::CallIFlags)
  5375. {
  5376. instr->m_opcode =
  5377. layout->callFlags == (Js::CallFlags::CallFlags_NewTarget | Js::CallFlags::CallFlags_New | Js::CallFlags::CallFlags_ExtraArg) ? Js::OpCode::CallINewTargetNew :
  5378. layout->callFlags == Js::CallFlags::CallFlags_New ? Js::OpCode::CallINew :
  5379. instr->m_opcode;
  5380. }
  5381. }
  5382. IR::Instr *
  5383. IRBuilder::BuildProfiledCallI(Js::OpCode opcode, uint32 offset, Js::RegSlot returnValue, Js::RegSlot function,
  5384. Js::ArgSlot argCount, Js::ProfileId profileId, Js::CallFlags flags, Js::InlineCacheIndex inlineCacheIndex)
  5385. {
  5386. Js::OpCode newOpcode;
  5387. ValueType returnType;
  5388. bool isProtectedByNoProfileBailout = false;
  5389. if (opcode == Js::OpCode::ProfiledNewScObject || opcode == Js::OpCode::ProfiledNewScObjectWithICIndex
  5390. || opcode == Js::OpCode::ProfiledNewScObjectSpread)
  5391. {
  5392. newOpcode = opcode;
  5393. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(newOpcode);
  5394. Assert(newOpcode == Js::OpCode::NewScObject || newOpcode == Js::OpCode::NewScObjectSpread);
  5395. if (!this->m_func->HasProfileInfo())
  5396. {
  5397. returnType = ValueType::GetObject(ObjectType::UninitializedObject);
  5398. }
  5399. else
  5400. {
  5401. // If we have profile data, make use of it
  5402. returnType = this->m_func->GetReadOnlyProfileInfo()->GetReturnType(opcode, profileId);
  5403. }
  5404. }
  5405. else
  5406. {
  5407. if (this->m_func->HasProfileInfo())
  5408. {
  5409. returnType = this->m_func->GetReadOnlyProfileInfo()->GetReturnType(opcode, profileId);
  5410. }
  5411. if (opcode < Js::OpCode::ProfiledReturnTypeCallI)
  5412. {
  5413. newOpcode = Js::OpCodeUtil::ConvertProfiledCallOpToNonProfiled(opcode);
  5414. if(DoBailOnNoProfile())
  5415. {
  5416. if(this->m_func->GetWorkItem()->GetJITTimeInfo())
  5417. {
  5418. const FunctionJITTimeInfo *inlinerData = this->m_func->GetWorkItem()->GetJITTimeInfo();
  5419. if (!(this->IsLoopBody() && PHASE_OFF(Js::InlineInJitLoopBodyPhase, this->m_func))
  5420. && inlinerData && inlinerData->GetInlineesBV())
  5421. {
  5422. AssertOrFailFast(profileId < inlinerData->GetInlineesBV()->Length());
  5423. if (!inlinerData->GetInlineesBV()->Test(profileId)
  5424. #if DBG
  5425. || (PHASE_STRESS(Js::BailOnNoProfilePhase, this->m_func->GetTopFunc())
  5426. && (CONFIG_FLAG(SkipFuncCountForBailOnNoProfile) < 0
  5427. || this->m_func->m_callSiteCount >= (uint)CONFIG_FLAG(SkipFuncCountForBailOnNoProfile)))
  5428. #endif
  5429. )
  5430. {
  5431. this->InsertBailOnNoProfile(offset);
  5432. isProtectedByNoProfileBailout = true;
  5433. }
  5434. }
  5435. if (!isProtectedByNoProfileBailout)
  5436. {
  5437. this->callTreeHasSomeProfileInfo = true;
  5438. }
  5439. }
  5440. #if DBG
  5441. this->m_func->m_callSiteCount++;
  5442. #endif
  5443. }
  5444. }
  5445. else
  5446. {
  5447. // Changing this opcode into a non ReturnTypeCall* opcode is done in BuildCallI_Helper
  5448. newOpcode = opcode;
  5449. }
  5450. }
  5451. IR::Instr * callInstr = BuildCallI_Helper(newOpcode, offset, returnValue, function, argCount, profileId, flags, inlineCacheIndex);
  5452. callInstr->isCallInstrProtectedByNoProfileBailout = isProtectedByNoProfileBailout;
  5453. if (callInstr->GetDst() && (callInstr->GetDst()->GetValueType().IsUninitialized() || callInstr->GetDst()->GetValueType() == ValueType::UninitializedObject))
  5454. {
  5455. callInstr->GetDst()->SetValueType(returnType);
  5456. }
  5457. return callInstr;
  5458. }
  5459. template <typename SizePolicy>
  5460. void
  5461. IRBuilder::BuildProfiledCallIExtended(Js::OpCode newOpcode, uint32 offset)
  5462. {
  5463. Assert(OpCodeAttr::IsProfiledOp(newOpcode) || Js::OpCodeUtil::IsProfiledCallOp(newOpcode)
  5464. || Js::OpCodeUtil::IsProfiledReturnTypeCallOp(newOpcode));
  5465. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5466. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile<Js::OpLayoutT_CallIExtended<SizePolicy>>>();
  5467. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5468. {
  5469. this->DoClosureRegCheck(layout->Return);
  5470. this->DoClosureRegCheck(layout->Function);
  5471. }
  5472. BuildProfiledCallIExtended(newOpcode, offset, layout->Return, layout->Function, layout->ArgCount, layout->profileId, layout->Options, layout->SpreadAuxOffset);
  5473. }
  5474. IR::Instr *
  5475. IRBuilder::BuildProfiledCallIExtended(Js::OpCode opcode, uint32 offset, Js::RegSlot returnValue, Js::RegSlot function,
  5476. Js::ArgSlot argCount, Js::ProfileId profileId, Js::CallIExtendedOptions options,
  5477. uint32 spreadAuxOffset, Js::CallFlags flags)
  5478. {
  5479. if (options & Js::CallIExtended_SpreadArgs)
  5480. {
  5481. BuildLdSpreadIndices(offset, spreadAuxOffset);
  5482. }
  5483. return BuildProfiledCallI(opcode, offset, returnValue, function, argCount, profileId, flags);
  5484. }
  5485. template <typename SizePolicy>
  5486. void
  5487. IRBuilder::BuildProfiled2CallI(Js::OpCode newOpcode, uint32 offset)
  5488. {
  5489. Assert(OpCodeAttr::IsProfiledOp(newOpcode));
  5490. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5491. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile2<Js::OpLayoutT_CallI<SizePolicy>>>();
  5492. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5493. {
  5494. this->DoClosureRegCheck(layout->Return);
  5495. this->DoClosureRegCheck(layout->Function);
  5496. }
  5497. BuildProfiled2CallI(newOpcode, offset, layout->Return, layout->Function, layout->ArgCount, layout->profileId, layout->profileId2);
  5498. }
  5499. void
  5500. IRBuilder::BuildProfiled2CallI(Js::OpCode opcode, uint32 offset, Js::RegSlot returnValue, Js::RegSlot function,
  5501. Js::ArgSlot argCount, Js::ProfileId profileId, Js::ProfileId profileId2)
  5502. {
  5503. Assert(opcode == Js::OpCode::ProfiledNewScObjArray || opcode == Js::OpCode::ProfiledNewScObjArraySpread);
  5504. Js::OpCodeUtil::ConvertNonCallOpToNonProfiled(opcode);
  5505. Js::OpCode useOpcode = opcode;
  5506. // We either want to provide the array profile id (profileId2) to the native array creation or the call profileid (profileId)
  5507. // to the call to NewScObject
  5508. Js::ProfileId useProfileId = profileId2;
  5509. Js::TypeId arrayTypeId = Js::TypeIds_Array;
  5510. if (returnValue != Js::Constants::NoRegister)
  5511. {
  5512. Js::ArrayCallSiteInfo *arrayCallSiteInfo = nullptr;
  5513. if (m_func->HasArrayInfo())
  5514. {
  5515. arrayCallSiteInfo = m_func->GetReadOnlyProfileInfo()->GetArrayCallSiteInfo(profileId2);
  5516. }
  5517. if (arrayCallSiteInfo && !m_func->IsJitInDebugMode())
  5518. {
  5519. if (arrayCallSiteInfo->IsNativeIntArray())
  5520. {
  5521. arrayTypeId = Js::TypeIds_NativeIntArray;
  5522. }
  5523. else if (arrayCallSiteInfo->IsNativeFloatArray())
  5524. {
  5525. arrayTypeId = Js::TypeIds_NativeFloatArray;
  5526. }
  5527. }
  5528. else
  5529. {
  5530. useOpcode = (opcode == Js::OpCode::NewScObjArraySpread) ? Js::OpCode::NewScObjectSpread : Js::OpCode::NewScObject;
  5531. useProfileId = profileId;
  5532. }
  5533. }
  5534. else
  5535. {
  5536. useOpcode = (opcode == Js::OpCode::NewScObjArraySpread) ? Js::OpCode::NewScObjectSpread : Js::OpCode::NewScObject;
  5537. useProfileId = profileId;
  5538. }
  5539. IR::Instr * callInstr = BuildCallI_Helper(useOpcode, offset, returnValue, function, argCount, useProfileId);
  5540. if (callInstr->GetDst())
  5541. {
  5542. callInstr->GetDst()->SetValueType(
  5543. ValueType::GetObject(ObjectType::Array).ToLikely().SetHasNoMissingValues(true).SetArrayTypeId(arrayTypeId));
  5544. }
  5545. if (callInstr->IsJitProfilingInstr())
  5546. {
  5547. // If we happened to decide in BuildCallI_Helper that this should be a jit profiling instr, then save the fact that it is
  5548. // a "new Array(args, ...)" call and also save the array profile id (profileId2)
  5549. callInstr->AsJitProfilingInstr()->isNewArray = true;
  5550. callInstr->AsJitProfilingInstr()->arrayProfileId = profileId2;
  5551. // Double check that this profileId made it to the JitProfilingInstr like we expect it to.
  5552. Assert(callInstr->AsJitProfilingInstr()->profileId == profileId);
  5553. }
  5554. }
  5555. template <typename SizePolicy>
  5556. void
  5557. IRBuilder::BuildProfiled2CallIExtended(Js::OpCode newOpcode, uint32 offset)
  5558. {
  5559. Assert(OpCodeAttr::IsProfiledOp(newOpcode));
  5560. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5561. auto layout = m_jnReader.GetLayout<Js::OpLayoutDynamicProfile2<Js::OpLayoutT_CallIExtended<SizePolicy>>>();
  5562. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5563. {
  5564. this->DoClosureRegCheck(layout->Return);
  5565. this->DoClosureRegCheck(layout->Function);
  5566. }
  5567. BuildProfiled2CallIExtended(newOpcode, offset, layout->Return, layout->Function, layout->ArgCount, layout->profileId, layout->profileId2, layout->Options, layout->SpreadAuxOffset);
  5568. }
  5569. void
  5570. IRBuilder::BuildProfiled2CallIExtended(Js::OpCode opcode, uint32 offset, Js::RegSlot returnValue, Js::RegSlot function,
  5571. Js::ArgSlot argCount, Js::ProfileId profileId, Js::ProfileId profileId2,
  5572. Js::CallIExtendedOptions options, uint32 spreadAuxOffset)
  5573. {
  5574. if (options & Js::CallIExtended_SpreadArgs)
  5575. {
  5576. BuildLdSpreadIndices(offset, spreadAuxOffset);
  5577. }
  5578. BuildProfiled2CallI(opcode, offset, returnValue, function, argCount, profileId, profileId2);
  5579. }
  5580. IR::Instr *
  5581. 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)
  5582. {
  5583. IR::Instr * instr;
  5584. IR::RegOpnd * dstOpnd;
  5585. IR::RegOpnd * src1Opnd;
  5586. StackSym * symDst;
  5587. src1Opnd = this->BuildSrcOpnd(Src1RegSlot);
  5588. if (dstRegSlot == Js::Constants::NoRegister)
  5589. {
  5590. dstOpnd = nullptr;
  5591. symDst = nullptr;
  5592. }
  5593. else
  5594. {
  5595. dstOpnd = this->BuildDstOpnd(dstRegSlot);
  5596. symDst = dstOpnd->m_sym;
  5597. }
  5598. const bool jitProfiling = m_func->DoSimpleJitDynamicProfile();
  5599. bool profiledReturn = false;
  5600. if (Js::OpCodeUtil::IsProfiledReturnTypeCallOp(newOpcode))
  5601. {
  5602. profiledReturn = true;
  5603. newOpcode = Js::OpCodeUtil::ConvertProfiledReturnTypeCallOpToNonProfiled(newOpcode);
  5604. // If we're profiling in the jitted code we want to propagate the profileId
  5605. // If we're using profile data instead of collecting it, we don't want to
  5606. // use the profile data from a return type call (this was previously done in IRBuilder::BuildProfiledCallI)
  5607. if (!jitProfiling)
  5608. {
  5609. profileId = Js::Constants::NoProfileId;
  5610. }
  5611. }
  5612. if (profileId != Js::Constants::NoProfileId)
  5613. {
  5614. if (jitProfiling)
  5615. {
  5616. // In SimpleJit we want this call to be a profiled call after being jitted
  5617. instr = IR::JitProfilingInstr::New(newOpcode, dstOpnd, src1Opnd, m_func);
  5618. instr->AsJitProfilingInstr()->profileId = profileId;
  5619. instr->AsJitProfilingInstr()->isProfiledReturnCall = profiledReturn;
  5620. instr->AsJitProfilingInstr()->inlineCacheIndex = inlineCacheIndex;
  5621. }
  5622. else
  5623. {
  5624. instr = IR::ProfiledInstr::New(newOpcode, dstOpnd, src1Opnd, m_func);
  5625. instr->AsProfiledInstr()->u.profileId = profileId;
  5626. }
  5627. }
  5628. else
  5629. {
  5630. instr = IR::Instr::New(newOpcode, m_func);
  5631. instr->SetSrc1(src1Opnd);
  5632. if (dstOpnd != nullptr)
  5633. {
  5634. instr->SetDst(dstOpnd);
  5635. }
  5636. }
  5637. if (dstOpnd && newOpcode == Js::OpCode::NewScObject)
  5638. {
  5639. dstOpnd->SetValueType(ValueType::GetObject(ObjectType::UninitializedObject));
  5640. }
  5641. if (symDst && symDst->m_isSingleDef)
  5642. {
  5643. switch (instr->m_opcode)
  5644. {
  5645. case Js::OpCode::NewScObject:
  5646. case Js::OpCode::NewScObjectSpread:
  5647. case Js::OpCode::NewScObjectLiteral:
  5648. case Js::OpCode::NewScObjArray:
  5649. case Js::OpCode::NewScObjArraySpread:
  5650. symDst->m_isSafeThis = true;
  5651. symDst->m_isNotNumber = true;
  5652. break;
  5653. }
  5654. }
  5655. this->AddInstr(instr, offset);
  5656. this->BuildCallCommon(instr, symDst, ArgCount, flags);
  5657. return instr;
  5658. }
  5659. void
  5660. IRBuilder::BuildCallCommon(IR::Instr * instr, StackSym * symDst, Js::ArgSlot argCount, Js::CallFlags flags)
  5661. {
  5662. Js::OpCode newOpcode = instr->m_opcode;
  5663. IR::Instr * argInstr = nullptr;
  5664. IR::Instr * prevInstr = instr;
  5665. #if DBG
  5666. int count = 0;
  5667. #endif
  5668. // Link all the args of this call by creating a def/use chain through the src2.
  5669. AssertOrFailFast(!m_argStack->Empty());
  5670. for (argInstr = m_argStack->Pop();
  5671. argInstr && !m_argStack->Empty() && argInstr->m_opcode != Js::OpCode::StartCall;
  5672. argInstr = m_argStack->Pop())
  5673. {
  5674. prevInstr->SetSrc2(argInstr->GetDst());
  5675. prevInstr = argInstr;
  5676. #if DBG
  5677. count++;
  5678. #endif
  5679. }
  5680. AssertOrFailFast(argInstr == nullptr || argInstr->m_opcode == Js::OpCode::StartCall);
  5681. if (m_argStack->Empty())
  5682. {
  5683. this->callTreeHasSomeProfileInfo = false;
  5684. }
  5685. if (newOpcode == Js::OpCode::NewScObject || newOpcode == Js::OpCode::NewScObjArray
  5686. || newOpcode == Js::OpCode::NewScObjectSpread || newOpcode == Js::OpCode::NewScObjArraySpread)
  5687. {
  5688. #if DBG
  5689. count++;
  5690. #endif
  5691. m_argsOnStack++;
  5692. }
  5693. argCount = Js::CallInfo::GetArgCountWithExtraArgs(flags, argCount);
  5694. if (argInstr)
  5695. {
  5696. prevInstr->SetSrc2(argInstr->GetDst());
  5697. AssertMsg(instr->m_prev->m_opcode == Js::OpCode::LdSpreadIndices
  5698. // All non-spread calls need StartCall to have the same number of args
  5699. || (argInstr->GetSrc1()->IsIntConstOpnd()
  5700. && argInstr->GetSrc1()->AsIntConstOpnd()->GetValue() == count
  5701. && count == argCount), "StartCall has wrong number of arguments...");
  5702. }
  5703. else
  5704. {
  5705. AssertMsg(false, "Expect StartCall on other opcodes...");
  5706. }
  5707. // Update Func if this is the highest amount of stack we've used so far
  5708. // to push args.
  5709. #if DBG
  5710. m_callsOnStack--;
  5711. #endif
  5712. if (m_func->m_argSlotsForFunctionsCalled < m_argsOnStack)
  5713. m_func->m_argSlotsForFunctionsCalled = m_argsOnStack;
  5714. #if DBG
  5715. if (m_callsOnStack == 0)
  5716. Assert(m_argsOnStack == argCount);
  5717. #endif
  5718. m_argsOnStack -= argCount;
  5719. if (m_func->IsJitInDebugMode())
  5720. {
  5721. // Insert bailout after return from a call, script or library function call.
  5722. this->InsertBailOutForDebugger(
  5723. m_jnReader.GetCurrentOffset(), // bailout will resume at the offset of next instr.
  5724. c_debuggerBailOutKindForCall);
  5725. }
  5726. }
  5727. ///----------------------------------------------------------------------------
  5728. ///
  5729. /// IRBuilder::BuildClass
  5730. ///
  5731. /// Build IR instr for an InitClass instruction.
  5732. ///
  5733. ///----------------------------------------------------------------------------
  5734. template <typename SizePolicy>
  5735. void
  5736. IRBuilder::BuildClass(Js::OpCode newOpcode, uint32 offset)
  5737. {
  5738. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  5739. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5740. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_Class<SizePolicy>>();
  5741. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5742. {
  5743. this->DoClosureRegCheck(layout->Constructor);
  5744. this->DoClosureRegCheck(layout->Extends);
  5745. }
  5746. BuildClass(newOpcode, offset, layout->Constructor, layout->Extends);
  5747. }
  5748. void
  5749. IRBuilder::BuildClass(Js::OpCode newOpcode, uint32 offset, Js::RegSlot constructor, Js::RegSlot extends)
  5750. {
  5751. Assert(newOpcode == Js::OpCode::InitClass);
  5752. IR::Instr * insn = IR::Instr::New(newOpcode, m_func);
  5753. insn->SetSrc1(this->BuildSrcOpnd(constructor));
  5754. if (extends != Js::Constants::NoRegister)
  5755. {
  5756. insn->SetSrc2(this->BuildSrcOpnd(extends));
  5757. }
  5758. this->AddInstr(insn, offset);
  5759. }
  5760. ///----------------------------------------------------------------------------
  5761. ///
  5762. /// IRBuilder::BuildBrReg1
  5763. ///
  5764. /// Build IR instr for a BrReg1 instruction.
  5765. /// This is a conditional branch with a single source operand (e.g., "if (x)" ...)
  5766. ///
  5767. ///----------------------------------------------------------------------------
  5768. template <typename SizePolicy>
  5769. void
  5770. IRBuilder::BuildBrReg1(Js::OpCode newOpcode, uint32 offset)
  5771. {
  5772. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  5773. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5774. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_BrReg1<SizePolicy>>();
  5775. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5776. {
  5777. this->DoClosureRegCheck(layout->R1);
  5778. }
  5779. BuildBrReg1(newOpcode, offset, m_jnReader.GetCurrentOffset() + layout->RelativeJumpOffset, layout->R1);
  5780. }
  5781. void
  5782. IRBuilder::BuildBrReg1(Js::OpCode newOpcode, uint32 offset, uint targetOffset, Js::RegSlot srcRegSlot)
  5783. {
  5784. IR::BranchInstr * branchInstr;
  5785. IR::RegOpnd * srcOpnd;
  5786. srcOpnd = this->BuildSrcOpnd(srcRegSlot);
  5787. if (newOpcode == Js::OpCode::BrNotUndecl_A) {
  5788. IR::AddrOpnd *srcOpnd2 = IR::AddrOpnd::New(m_func->GetScriptContextInfo()->GetUndeclBlockVarAddr(),
  5789. IR::AddrOpndKindDynamicVar, this->m_func);
  5790. branchInstr = IR::BranchInstr::New(Js::OpCode::BrNotAddr_A, nullptr, srcOpnd, srcOpnd2, m_func);
  5791. } else {
  5792. branchInstr = IR::BranchInstr::New(newOpcode, nullptr, srcOpnd, m_func);
  5793. }
  5794. this->AddBranchInstr(branchInstr, offset, targetOffset);
  5795. }
  5796. ///----------------------------------------------------------------------------
  5797. ///
  5798. /// IRBuilder::BuildBrReg2
  5799. ///
  5800. /// Build IR instr for a BrReg2 instruction.
  5801. /// This is a conditional branch with a 2 source operands (e.g., "if (x == y)" ...)
  5802. ///
  5803. ///----------------------------------------------------------------------------
  5804. template <typename SizePolicy>
  5805. void
  5806. IRBuilder::BuildBrReg2(Js::OpCode newOpcode, uint32 offset)
  5807. {
  5808. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  5809. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5810. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_BrReg2<SizePolicy>>();
  5811. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5812. {
  5813. this->DoClosureRegCheck(layout->R1);
  5814. this->DoClosureRegCheck(layout->R2);
  5815. }
  5816. BuildBrReg2(newOpcode, offset, m_jnReader.GetCurrentOffset() + layout->RelativeJumpOffset, layout->R1, layout->R2);
  5817. }
  5818. template <typename SizePolicy>
  5819. void
  5820. IRBuilder::BuildBrReg1Unsigned1(Js::OpCode newOpcode, uint32 offset)
  5821. {
  5822. Assert(newOpcode == Js::OpCode::BrOnEmpty
  5823. /* || newOpcode == Js::OpCode::BrOnNotEmpty */ // BrOnNotEmpty not generate by the byte code
  5824. );
  5825. Assert(!OpCodeAttr::IsProfiledOp(newOpcode));
  5826. Assert(OpCodeAttr::HasMultiSizeLayout(newOpcode));
  5827. auto layout = m_jnReader.GetLayout<Js::OpLayoutT_BrReg1Unsigned1<SizePolicy>>();
  5828. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  5829. {
  5830. this->DoClosureRegCheck(layout->R1);
  5831. }
  5832. BuildBrBReturn(newOpcode, offset, layout->R1, layout->C2, m_jnReader.GetCurrentOffset() + layout->RelativeJumpOffset);
  5833. }
  5834. void
  5835. IRBuilder::BuildBrBReturn(Js::OpCode newOpcode, uint32 offset, Js::RegSlot DestRegSlot, uint32 forInLoopLevel, uint32 targetOffset)
  5836. {
  5837. IR::Opnd *srcOpnd = this->BuildForInEnumeratorOpnd(forInLoopLevel);
  5838. IR::RegOpnd * destOpnd = this->BuildDstOpnd(DestRegSlot);
  5839. IR::BranchInstr * branchInstr = IR::BranchInstr::New(newOpcode, destOpnd, nullptr, srcOpnd, m_func);
  5840. this->AddBranchInstr(branchInstr, offset, targetOffset);
  5841. switch (newOpcode)
  5842. {
  5843. case Js::OpCode::BrOnEmpty:
  5844. destOpnd->SetValueType(ValueType::String);
  5845. break;
  5846. default:
  5847. Assert(false);
  5848. break;
  5849. };
  5850. }
  5851. void
  5852. IRBuilder::BuildBrReg2(Js::OpCode newOpcode, uint32 offset, uint targetOffset, Js::RegSlot R1, Js::RegSlot R2)
  5853. {
  5854. IR::BranchInstr * branchInstr;
  5855. if (newOpcode == Js::OpCode::BrOnEmpty
  5856. /* || newOpcode == Js::OpCode::BrOnNotEmpty */ // BrOnNotEmpty not generate by the byte code
  5857. )
  5858. {
  5859. BuildBrBReturn(newOpcode, offset, R1, R2, targetOffset);
  5860. return;
  5861. }
  5862. IR::RegOpnd * src1Opnd;
  5863. IR::RegOpnd * src2Opnd;
  5864. src1Opnd = this->BuildSrcOpnd(R1);
  5865. src2Opnd = this->BuildSrcOpnd(R2);
  5866. if (newOpcode == Js::OpCode::Case)
  5867. {
  5868. // generating branches for Cases is entirely handled
  5869. // by the SwitchIRBuilder
  5870. m_switchBuilder.OnCase(src1Opnd, src2Opnd, offset, targetOffset);
  5871. #ifdef BYTECODE_BRANCH_ISLAND
  5872. // Make sure that if there are branch island between the cases, we consume it first
  5873. EnsureConsumeBranchIsland();
  5874. #endif
  5875. // some instructions can't be optimized past, such as LdFld for objects. In these cases we have
  5876. // to inform the SwitchBuilder to flush any optimized cases that it has stored up to this point
  5877. // peeks the next opcode - to check if it is not a case statement (for example: the next instr can be a LdFld for objects)
  5878. Js::OpCode peekOpcode = m_jnReader.PeekOp();
  5879. if (peekOpcode != Js::OpCode::Case && peekOpcode != Js::OpCode::EndSwitch)
  5880. {
  5881. m_switchBuilder.FlushCases(m_jnReader.GetCurrentOffset());
  5882. }
  5883. }
  5884. else
  5885. {
  5886. branchInstr = IR::BranchInstr::New(newOpcode, nullptr, src1Opnd, src2Opnd, m_func);
  5887. this->AddBranchInstr(branchInstr, offset, targetOffset);
  5888. }
  5889. }
  5890. void
  5891. IRBuilder::BuildEmpty(Js::OpCode newOpcode, uint32 offset)
  5892. {
  5893. IR::Instr *instr;
  5894. m_jnReader.Empty();
  5895. instr = IR::Instr::New(newOpcode, m_func);
  5896. switch (newOpcode)
  5897. {
  5898. case Js::OpCode::CommitScope:
  5899. {
  5900. IR::RegOpnd * src1Opnd;
  5901. src1Opnd = this->BuildSrcOpnd(m_func->GetJITFunctionBody()->GetLocalClosureReg());
  5902. IR::LabelInstr *labelNull = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  5903. IR::RegOpnd * funcExprOpnd = IR::RegOpnd::New(TyVar, m_func);
  5904. instr = IR::Instr::New(Js::OpCode::LdFuncExpr, funcExprOpnd, m_func);
  5905. this->AddInstr(instr, offset);
  5906. IR::BranchInstr *branchInstr = IR::BranchInstr::New(Js::OpCode::BrFncCachedScopeNeq, labelNull,
  5907. funcExprOpnd, src1Opnd, this->m_func);
  5908. this->AddInstr(branchInstr, offset);
  5909. instr = IR::Instr::New(newOpcode, this->m_func);
  5910. instr->SetSrc1(src1Opnd);
  5911. this->AddInstr(instr, offset);
  5912. this->AddInstr(labelNull, Js::Constants::NoByteCodeOffset);
  5913. return;
  5914. }
  5915. case Js::OpCode::Ret:
  5916. {
  5917. IR::RegOpnd *regOpnd = BuildDstOpnd(0);
  5918. instr->SetSrc1(regOpnd);
  5919. this->AddInstr(instr, offset);
  5920. break;
  5921. }
  5922. case Js::OpCode::Leave:
  5923. {
  5924. IR::BranchInstr * branchInstr;
  5925. IR::LabelInstr * labelInstr;
  5926. if (this->handlerOffsetStack && !this->handlerOffsetStack->Empty() && this->handlerOffsetStack->Top().Second())
  5927. {
  5928. // If the try region has a break block, we don't want the Flowgraph to move all of that code out of the loop
  5929. // because an exception will bring the control back into the loop. The branch out of the loop (which is the
  5930. // reason for the code to be a break block) can still be moved out though.
  5931. //
  5932. // "BrOnException $catch" is inserted before Leave's in the try region to instrument flow from the try region
  5933. // to the catch region (which is in the loop).
  5934. IR::BranchInstr * brOnException = IR::BranchInstr::New(Js::OpCode::BrOnException, nullptr, this->m_func);
  5935. this->AddBranchInstr(brOnException, offset, this->handlerOffsetStack->Top().First());
  5936. }
  5937. labelInstr = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  5938. branchInstr = IR::BranchInstr::New(newOpcode, labelInstr, this->m_func);
  5939. this->AddInstr(branchInstr, offset);
  5940. this->AddInstr(labelInstr, Js::Constants::NoByteCodeOffset);
  5941. break;
  5942. }
  5943. case Js::OpCode::LeaveNull:
  5944. finallyBlockLevel--;
  5945. this->AddInstr(instr, offset);
  5946. break;
  5947. case Js::OpCode::Finally:
  5948. if (this->handlerOffsetStack)
  5949. {
  5950. AssertOrFailFast(!this->handlerOffsetStack->Empty());
  5951. AssertOrFailFast(this->handlerOffsetStack->Top().Second() == false);
  5952. this->handlerOffsetStack->Pop();
  5953. }
  5954. finallyBlockLevel++;
  5955. this->AddInstr(IR::Instr::New(Js::OpCode::Finally, this->m_func), offset);
  5956. break;
  5957. case Js::OpCode::Break:
  5958. if (m_func->IsJitInDebugMode())
  5959. {
  5960. // Add explicit bailout.
  5961. this->InsertBailOutForDebugger(offset, IR::BailOutExplicit);
  5962. }
  5963. else
  5964. {
  5965. // Default behavior, let's keep it for now, removed in lowerer.
  5966. this->AddInstr(instr, offset);
  5967. }
  5968. break;
  5969. case Js::OpCode::BeginBodyScope:
  5970. {
  5971. // This marks the end of a param socpe which is not merged with body scope.
  5972. // So we have to first cache the closure so that we can use it to copy the initial values for
  5973. // body syms from corresponding param syms (LdParamSlot). Body should get its own scope slot.
  5974. Assert(!this->IsParamScopeDone());
  5975. this->SetParamScopeDone();
  5976. IR::Opnd * localClosureOpnd;
  5977. if (this->m_func->GetLocalClosureSym() != nullptr)
  5978. {
  5979. localClosureOpnd = IR::RegOpnd::New(this->m_func->GetLocalClosureSym(), TyVar, this->m_func);
  5980. }
  5981. else
  5982. {
  5983. AssertOrFailFast(this->m_func->GetJITFunctionBody()->GetScopeSlotArraySize() == 0 && !this->m_func->GetJITFunctionBody()->HasScopeObject());
  5984. localClosureOpnd = IR::IntConstOpnd::New(0, TyVar, this->m_func);
  5985. }
  5986. this->AddInstr(
  5987. IR::Instr::New(
  5988. Js::OpCode::Ld_A,
  5989. this->BuildDstOpnd(this->m_func->GetJITFunctionBody()->GetParamClosureReg()),
  5990. localClosureOpnd,
  5991. this->m_func),
  5992. offset);
  5993. // Create a new local closure for the body when either body scope has scope slots allocated or
  5994. // eval is present which can leak declarations.
  5995. if (this->m_func->GetJITFunctionBody()->GetScopeSlotArraySize() > 0 || this->m_func->GetJITFunctionBody()->HasScopeObject())
  5996. {
  5997. if (this->m_func->GetJITFunctionBody()->HasScopeObject())
  5998. {
  5999. if (this->m_func->GetJITFunctionBody()->HasCachedScopePropIds())
  6000. {
  6001. this->BuildInitCachedScope(0, Js::Constants::NoByteCodeOffset);
  6002. }
  6003. else
  6004. {
  6005. this->AddInstr(
  6006. IR::Instr::New(
  6007. Js::OpCode::NewScopeObject,
  6008. this->BuildDstOpnd(this->m_func->GetJITFunctionBody()->GetLocalClosureReg()),
  6009. m_func),
  6010. Js::Constants::NoByteCodeOffset);
  6011. }
  6012. }
  6013. else
  6014. {
  6015. this->AddInstr(
  6016. IR::Instr::New(
  6017. Js::OpCode::NewScopeSlots,
  6018. this->BuildDstOpnd(this->m_func->GetJITFunctionBody()->GetLocalClosureReg()),
  6019. IR::IntConstOpnd::New(this->m_func->GetJITFunctionBody()->GetScopeSlotArraySize() + Js::ScopeSlots::FirstSlotIndex, TyUint32, this->m_func),
  6020. m_func),
  6021. Js::Constants::NoByteCodeOffset);
  6022. }
  6023. IR::Instr* lfd = IR::Instr::New(
  6024. Js::OpCode::LdFrameDisplay,
  6025. this->BuildDstOpnd(this->m_func->GetJITFunctionBody()->GetLocalFrameDisplayReg()),
  6026. this->BuildDstOpnd(this->m_func->GetJITFunctionBody()->GetLocalClosureReg()),
  6027. this->BuildDstOpnd(this->m_func->GetJITFunctionBody()->GetLocalFrameDisplayReg()),
  6028. this->m_func);
  6029. this->AddInstr(
  6030. lfd,
  6031. Js::Constants::NoByteCodeOffset);
  6032. lfd->isNonFastPathFrameDisplay = true;
  6033. }
  6034. break;
  6035. }
  6036. default:
  6037. this->AddInstr(instr, offset);
  6038. break;
  6039. }
  6040. }
  6041. #ifdef BYTECODE_BRANCH_ISLAND
  6042. void
  6043. IRBuilder::EnsureConsumeBranchIsland()
  6044. {
  6045. if (m_jnReader.PeekOp() == Js::OpCode::Br)
  6046. {
  6047. // Save the old offset
  6048. uint offset = m_jnReader.GetCurrentOffset();
  6049. // Read the potentially a branch around
  6050. Js::LayoutSize layoutSize;
  6051. Js::OpCode opcode = m_jnReader.ReadOp(layoutSize);
  6052. Assert(opcode == Js::OpCode::Br);
  6053. Assert(layoutSize == Js::SmallLayout);
  6054. const unaligned Js::OpLayoutBr * playout = m_jnReader.Br();
  6055. unsigned int targetOffset = m_jnReader.GetCurrentOffset() + playout->RelativeJumpOffset;
  6056. uint branchIslandOffset = m_jnReader.GetCurrentOffset();
  6057. if (branchIslandOffset == targetOffset)
  6058. {
  6059. // branch to next, there is no long branch
  6060. m_jnReader.SetCurrentOffset(offset);
  6061. return;
  6062. }
  6063. // Ignore all the BrLong
  6064. while (m_jnReader.PeekOp() == Js::OpCode::BrLong)
  6065. {
  6066. opcode = m_jnReader.ReadOp(layoutSize);
  6067. Assert(opcode == Js::OpCode::BrLong);
  6068. Assert(layoutSize == Js::SmallLayout);
  6069. m_jnReader.BrLong();
  6070. }
  6071. // Confirm that is a branch around
  6072. if ((uint)m_jnReader.GetCurrentOffset() == targetOffset)
  6073. {
  6074. // Really consume the branch island
  6075. m_jnReader.SetCurrentOffset(branchIslandOffset);
  6076. ConsumeBranchIsland();
  6077. // Mark the virtual branch around as a redirect long branch as well
  6078. // so that if it is the target of another branch, it will just keep pass
  6079. // the branch island
  6080. Assert(longBranchMap);
  6081. Assert(offset < m_offsetToInstructionCount);
  6082. Assert(m_offsetToInstruction[offset] == nullptr);
  6083. m_offsetToInstruction[offset] = VirtualLongBranchInstr;
  6084. longBranchMap->Add(offset, targetOffset);
  6085. }
  6086. else
  6087. {
  6088. // Reset the offset
  6089. m_jnReader.SetCurrentOffset(offset);
  6090. }
  6091. }
  6092. }
  6093. IR::Instr * const IRBuilder::VirtualLongBranchInstr = (IR::Instr *)-1;
  6094. void
  6095. IRBuilder::ConsumeBranchIsland()
  6096. {
  6097. do
  6098. {
  6099. uint32 offset = m_jnReader.GetCurrentOffset();
  6100. Js::LayoutSize layoutSize;
  6101. Js::OpCode opcode = m_jnReader.ReadOp(layoutSize);
  6102. Assert(opcode == Js::OpCode::BrLong);
  6103. Assert(layoutSize == Js::SmallLayout);
  6104. BuildBrLong(Js::OpCode::BrLong, offset);
  6105. }
  6106. while (m_jnReader.PeekOp() == Js::OpCode::BrLong);
  6107. }
  6108. void
  6109. IRBuilder::BuildBrLong(Js::OpCode newOpcode, uint32 offset)
  6110. {
  6111. Assert(newOpcode == Js::OpCode::BrLong);
  6112. Assert(!OpCodeAttr::HasMultiSizeLayout(newOpcode));
  6113. Assert(offset != Js::Constants::NoByteCodeOffset);
  6114. const unaligned Js::OpLayoutBrLong *branchInsn = m_jnReader.BrLong();
  6115. unsigned int targetOffset = m_jnReader.GetCurrentOffset() + branchInsn->RelativeJumpOffset;
  6116. Assert(offset < m_offsetToInstructionCount);
  6117. Assert(m_offsetToInstruction[offset] == nullptr);
  6118. // BrLong are also just the target of another branch, just set a virtual long branch instr
  6119. // and remap the original branch to the actual destination in ResolveVirtualLongBranch
  6120. m_offsetToInstruction[offset] = VirtualLongBranchInstr;
  6121. longBranchMap->Add(offset, targetOffset);
  6122. }
  6123. uint
  6124. IRBuilder::ResolveVirtualLongBranch(IR::BranchInstr * branchInstr, uint offset)
  6125. {
  6126. Assert(longBranchMap);
  6127. uint32 targetOffset;
  6128. if (!longBranchMap->TryGetValue(offset, &targetOffset))
  6129. {
  6130. // If we see a VirtualLongBranchInstr, we must have a mapping to the real target offset
  6131. Assert(false);
  6132. Fatal();
  6133. }
  6134. // If this is a jump out of the loop body we need to load the return IP and jump to the loop exit instead
  6135. if (!IsLoopBodyOuterOffset(targetOffset))
  6136. {
  6137. return targetOffset;
  6138. }
  6139. // Multi branch shouldn't be exiting a loop
  6140. Assert(branchInstr->m_opcode != Js::OpCode::MultiBr);
  6141. // Don't load the return IP if it is already loaded (for the case of early exit)
  6142. if (!IsLoopBodyReturnIPInstr(branchInstr->m_prev))
  6143. {
  6144. IR::Instr * returnIPInstr = CreateLoopBodyReturnIPInstr(targetOffset, branchInstr->GetByteCodeOffset());
  6145. // Any jump to this branch to jump to the return IP load instr first
  6146. uint32 branchInstrByteCodeOffset = branchInstr->GetByteCodeOffset();
  6147. Assert(this->m_offsetToInstruction[branchInstrByteCodeOffset] == branchInstr ||
  6148. (this->m_offsetToInstruction[branchInstrByteCodeOffset]->HasBailOutInfo() &&
  6149. this->m_offsetToInstruction[branchInstrByteCodeOffset]->GetBailOutKind() == IR::BailOutInjected));
  6150. InsertInstr(returnIPInstr, branchInstr);
  6151. }
  6152. return GetLoopBodyExitInstrOffset();
  6153. }
  6154. #endif
  6155. ///----------------------------------------------------------------------------
  6156. ///
  6157. /// IRBuilder::BuildBr
  6158. ///
  6159. /// Build IR instr for a Br (unconditional branch) instruction.
  6160. /// or TryCatch/TryFinally
  6161. ///
  6162. ///----------------------------------------------------------------------------
  6163. void
  6164. IRBuilder::BuildBr(Js::OpCode newOpcode, uint32 offset)
  6165. {
  6166. Assert(!OpCodeAttr::HasMultiSizeLayout(newOpcode));
  6167. IR::BranchInstr * branchInstr;
  6168. const unaligned Js::OpLayoutBr *branchInsn = m_jnReader.Br();
  6169. unsigned int targetOffset = m_jnReader.GetCurrentOffset() + branchInsn->RelativeJumpOffset;
  6170. #ifdef BYTECODE_BRANCH_ISLAND
  6171. bool isLongBranchIsland = (m_jnReader.PeekOp() == Js::OpCode::BrLong);
  6172. if (isLongBranchIsland)
  6173. {
  6174. ConsumeBranchIsland();
  6175. }
  6176. #endif
  6177. if(newOpcode == Js::OpCode::EndSwitch)
  6178. {
  6179. m_switchBuilder.EndSwitch(offset, targetOffset);
  6180. return;
  6181. }
  6182. #ifdef PERF_HINT
  6183. else if (PHASE_TRACE1(Js::PerfHintPhase) && (newOpcode == Js::OpCode::TryCatch || newOpcode == Js::OpCode::TryFinally) )
  6184. {
  6185. WritePerfHint(PerfHints::HasTryBlock, this->m_func, offset);
  6186. }
  6187. #endif
  6188. #ifdef BYTECODE_BRANCH_ISLAND
  6189. if (isLongBranchIsland && (targetOffset == (uint)m_jnReader.GetCurrentOffset()))
  6190. {
  6191. // Branch to next (probably after consume branch island), try to not emit the branch
  6192. // Mark the jump around instruction as a virtual long branch as well so we can just
  6193. // fall through instead of branch to exit
  6194. Assert(offset < m_offsetToInstructionCount);
  6195. if (m_offsetToInstruction[offset] == nullptr)
  6196. {
  6197. m_offsetToInstruction[offset] = VirtualLongBranchInstr;
  6198. longBranchMap->Add(offset, targetOffset);
  6199. return;
  6200. }
  6201. // We may have already create an instruction on this offset as a statement boundary
  6202. // or in the bailout at every byte code case.
  6203. // The statement boundary case only happens if we have emitted the long branch island
  6204. // after an existing no fall through instruction, but that instruction also happen to be
  6205. // branch to next. We will just generate an actual branch to next instruction.
  6206. Assert(m_offsetToInstruction[offset]->m_opcode == Js::OpCode::StatementBoundary
  6207. || (Js::Configuration::Global.flags.IsEnabled(Js::BailOutAtEveryByteCodeFlag)
  6208. && m_offsetToInstruction[offset]->m_opcode == Js::OpCode::BailOnEqual));
  6209. }
  6210. #endif
  6211. if ((newOpcode == Js::OpCode::TryCatch) && this->handlerOffsetStack)
  6212. {
  6213. this->handlerOffsetStack->Push(Pair<uint, bool>(targetOffset, true));
  6214. }
  6215. else if ((newOpcode == Js::OpCode::TryFinally) && this->handlerOffsetStack)
  6216. {
  6217. this->handlerOffsetStack->Push(Pair<uint, bool>(targetOffset, false));
  6218. }
  6219. branchInstr = IR::BranchInstr::New(newOpcode, nullptr, m_func);
  6220. this->AddBranchInstr(branchInstr, offset, targetOffset);
  6221. }
  6222. void
  6223. IRBuilder::BuildBrS(Js::OpCode newOpcode, uint32 offset)
  6224. {
  6225. Assert(!OpCodeAttr::HasMultiSizeLayout(newOpcode));
  6226. IR::BranchInstr * branchInstr;
  6227. const unaligned Js::OpLayoutBrS *branchInsn = m_jnReader.BrS();
  6228. unsigned int targetOffset = m_jnReader.GetCurrentOffset() + branchInsn->RelativeJumpOffset;
  6229. branchInstr = IR::BranchInstr::New(newOpcode, nullptr,
  6230. IR::IntConstOpnd::New(branchInsn->val,
  6231. TyInt32, m_func),m_func);
  6232. this->AddBranchInstr(branchInstr, offset, targetOffset);
  6233. }
  6234. ///----------------------------------------------------------------------------
  6235. ///
  6236. /// IRBuilder::BuildBrProperty
  6237. ///
  6238. /// Build IR instr for a BrProperty instruction.
  6239. /// This is a conditional branch that tests whether the given property
  6240. /// is present on the given instance.
  6241. ///
  6242. ///----------------------------------------------------------------------------
  6243. void
  6244. IRBuilder::BuildBrProperty(Js::OpCode newOpcode, uint32 offset)
  6245. {
  6246. Assert(!OpCodeAttr::HasMultiSizeLayout(newOpcode));
  6247. const unaligned Js::OpLayoutBrProperty *branchInsn = m_jnReader.BrProperty();
  6248. if (!PHASE_OFF(Js::ClosureRegCheckPhase, m_func))
  6249. {
  6250. this->DoClosureRegCheck(branchInsn->Instance);
  6251. }
  6252. IR::BranchInstr * branchInstr;
  6253. Js::PropertyId propertyId =
  6254. m_func->GetJITFunctionBody()->GetReferencedPropertyId(branchInsn->PropertyIdIndex);
  6255. unsigned int targetOffset = m_jnReader.GetCurrentOffset() + branchInsn->RelativeJumpOffset;
  6256. IR::SymOpnd * fieldSymOpnd = this->BuildFieldOpnd(newOpcode, branchInsn->Instance, propertyId, branchInsn->PropertyIdIndex, PropertyKindData);
  6257. branchInstr = IR::BranchInstr::New(newOpcode, nullptr, fieldSymOpnd, m_func);
  6258. this->AddBranchInstr(branchInstr, offset, targetOffset);
  6259. }
  6260. void
  6261. IRBuilder::BuildBrLocalProperty(Js::OpCode newOpcode, uint32 offset)
  6262. {
  6263. Assert(!OpCodeAttr::HasMultiSizeLayout(newOpcode));
  6264. switch (newOpcode)
  6265. {
  6266. case Js::OpCode::BrOnNoLocalProperty:
  6267. newOpcode = Js::OpCode::BrOnNoProperty;
  6268. break;
  6269. default:
  6270. Assert(0);
  6271. break;
  6272. }
  6273. const unaligned Js::OpLayoutBrLocalProperty *branchInsn = m_jnReader.BrLocalProperty();
  6274. if (m_func->GetLocalClosureSym()->HasByteCodeRegSlot())
  6275. {
  6276. IR::ByteCodeUsesInstr * byteCodeUse = IR::ByteCodeUsesInstr::New(m_func, offset);
  6277. byteCodeUse->SetNonOpndSymbol(m_func->GetLocalClosureSym()->m_id);
  6278. this->AddInstr(byteCodeUse, offset);
  6279. }
  6280. IR::BranchInstr * branchInstr;
  6281. Js::PropertyId propertyId =
  6282. m_func->GetJITFunctionBody()->GetReferencedPropertyId(branchInsn->PropertyIdIndex);
  6283. unsigned int targetOffset = m_jnReader.GetCurrentOffset() + branchInsn->RelativeJumpOffset;
  6284. IR::SymOpnd * fieldSymOpnd = this->BuildFieldOpnd(newOpcode, m_func->GetJITFunctionBody()->GetLocalClosureReg(), propertyId, branchInsn->PropertyIdIndex, PropertyKindData);
  6285. branchInstr = IR::BranchInstr::New(newOpcode, nullptr, fieldSymOpnd, m_func);
  6286. this->AddBranchInstr(branchInstr, offset, targetOffset);
  6287. }
  6288. void
  6289. IRBuilder::BuildBrEnvProperty(Js::OpCode newOpcode, uint32 offset)
  6290. {
  6291. Assert(!OpCodeAttr::HasMultiSizeLayout(newOpcode));
  6292. const unaligned Js::OpLayoutBrEnvProperty *branchInsn = m_jnReader.BrEnvProperty();
  6293. IR::Instr *instr;
  6294. IR::BranchInstr * branchInstr;
  6295. IR::RegOpnd *regOpnd;
  6296. IR::SymOpnd *fieldOpnd;
  6297. PropertySym *fieldSym;
  6298. fieldOpnd = this->BuildFieldOpnd(Js::OpCode::LdSlotArr, this->GetEnvReg(), branchInsn->SlotIndex, (Js::PropertyIdIndexType)-1, PropertyKindSlotArray);
  6299. regOpnd = IR::RegOpnd::New(TyVar, m_func);
  6300. instr = IR::Instr::New(Js::OpCode::LdSlotArr, regOpnd, fieldOpnd, m_func);
  6301. this->AddInstr(instr, offset);
  6302. Js::PropertyId propertyId =
  6303. m_func->GetJITFunctionBody()->GetReferencedPropertyId(branchInsn->PropertyIdIndex);
  6304. unsigned int targetOffset = m_jnReader.GetCurrentOffset() + branchInsn->RelativeJumpOffset;\
  6305. fieldSym = PropertySym::New(regOpnd->m_sym, propertyId, branchInsn->PropertyIdIndex, (uint)-1, PropertyKindData, m_func);
  6306. fieldOpnd = IR::SymOpnd::New(fieldSym, TyVar, m_func);
  6307. branchInstr = IR::BranchInstr::New(Js::OpCode::BrOnNoProperty, nullptr, fieldOpnd, m_func);
  6308. this->AddBranchInstr(branchInstr, offset, targetOffset);
  6309. }
  6310. ///----------------------------------------------------------------------------
  6311. ///
  6312. /// IRBuilder::AddBranchInstr
  6313. ///
  6314. /// Create a branch/offset pair which will be fixed up at the end of the
  6315. /// IRBuilder phase and add the instruction
  6316. ///
  6317. ///----------------------------------------------------------------------------
  6318. BranchReloc *
  6319. IRBuilder::AddBranchInstr(IR::BranchInstr * branchInstr, uint32 offset, uint32 targetOffset)
  6320. {
  6321. AssertOrFailFast(targetOffset <= m_func->GetJITFunctionBody()->GetByteCodeLength());
  6322. //
  6323. // Loop jitting would be done only till the LoopEnd
  6324. // Any branches beyond that offset are for the return stmt
  6325. //
  6326. if (IsLoopBodyOuterOffset(targetOffset))
  6327. {
  6328. // if we have loaded the loop IP sym from the ProfiledLoopEnd then don't add it here
  6329. if (!IsLoopBodyReturnIPInstr(m_lastInstr))
  6330. {
  6331. this->InsertLoopBodyReturnIPInstr(targetOffset, offset);
  6332. }
  6333. // Jump the restore StSlot and Ret instruction
  6334. targetOffset = GetLoopBodyExitInstrOffset();
  6335. }
  6336. BranchReloc * reloc = nullptr;
  6337. reloc = this->CreateRelocRecord(branchInstr, offset, targetOffset);
  6338. this->AddInstr(branchInstr, offset);
  6339. return reloc;
  6340. }
  6341. BranchReloc *
  6342. IRBuilder::CreateRelocRecord(IR::BranchInstr * branchInstr, uint32 offset, uint32 targetOffset)
  6343. {
  6344. BranchReloc * reloc = JitAnew(this->m_tempAlloc, BranchReloc, branchInstr, offset, targetOffset);
  6345. this->branchRelocList->Prepend(reloc);
  6346. return reloc;
  6347. }
  6348. ///----------------------------------------------------------------------------
  6349. ///
  6350. /// IRBuilder::BuildRegexFromPattern
  6351. ///
  6352. /// Build a new RegEx instruction. Simply construct a var to hold the regex
  6353. /// and load it as an immediate into a register.
  6354. ///
  6355. ///----------------------------------------------------------------------------
  6356. void
  6357. IRBuilder::BuildRegexFromPattern(Js::RegSlot dstRegSlot, uint32 patternIndex, uint32 offset)
  6358. {
  6359. IR::Instr * instr;
  6360. IR::RegOpnd* dstOpnd = this->BuildDstOpnd(dstRegSlot);
  6361. dstOpnd->SetValueType(ValueType::GetObject(ObjectType::RegExp));
  6362. IR::Opnd * regexOpnd = IR::AddrOpnd::New(m_func->GetJITFunctionBody()->GetLiteralRegexAddr(patternIndex), IR::AddrOpndKindDynamicMisc, this->m_func);
  6363. instr = IR::Instr::New(Js::OpCode::NewRegEx, dstOpnd, regexOpnd, this->m_func);
  6364. this->AddInstr(instr, offset);
  6365. }
  6366. bool
  6367. IRBuilder::IsFloatFunctionCallsite(Js::BuiltinFunction index, size_t argc)
  6368. {
  6369. return Js::JavascriptLibrary::IsFloatFunctionCallsite(index, argc);
  6370. }
  6371. void
  6372. IRBuilder::CheckBuiltIn(PropertySym * propertySym, Js::BuiltinFunction *puBuiltInIndex)
  6373. {
  6374. Js::BuiltinFunction index = Js::BuiltinFunction::None;
  6375. // Check whether the propertySym appears to be a built-in.
  6376. if (propertySym->m_fieldKind != PropertyKindData)
  6377. {
  6378. return;
  6379. }
  6380. index = Js::JavascriptLibrary::GetBuiltinFunctionForPropId(propertySym->m_propertyId);
  6381. if (index == Js::BuiltinFunction::None)
  6382. {
  6383. return;
  6384. }
  6385. // If the target is one of the Math built-ins, see whether the stack sym is the
  6386. // global "Math".
  6387. if (Js::JavascriptLibrary::IsFltFunc(index))
  6388. {
  6389. if (!propertySym->m_stackSym->m_isSingleDef)
  6390. {
  6391. return;
  6392. }
  6393. IR::Instr *instr = propertySym->m_stackSym->m_instrDef;
  6394. AssertMsg(instr != nullptr, "Single-def stack sym w/o def instr?");
  6395. if (instr->m_opcode != Js::OpCode::LdRootFld && instr->m_opcode != Js::OpCode::LdRootFldForTypeOf)
  6396. {
  6397. return;
  6398. }
  6399. IR::Opnd * opnd = instr->GetSrc1();
  6400. AssertMsg(opnd != nullptr && opnd->IsSymOpnd() && opnd->AsSymOpnd()->m_sym->IsPropertySym(),
  6401. "LdRootFld w/o propertySym src?");
  6402. if (opnd->AsSymOpnd()->m_sym->AsPropertySym()->m_propertyId != Js::PropertyIds::Math)
  6403. {
  6404. return;
  6405. }
  6406. }
  6407. *puBuiltInIndex = index;
  6408. }
  6409. StackSym *
  6410. IRBuilder::EnsureStackFuncPtrSym()
  6411. {
  6412. StackSym * sym = this->m_stackFuncPtrSym;
  6413. if (sym)
  6414. {
  6415. return sym;
  6416. }
  6417. if (m_func->IsLoopBody() && m_func->DoStackNestedFunc())
  6418. {
  6419. Assert(m_func->IsTopFunc());
  6420. sym = StackSym::New(TyVar, m_func);
  6421. this->m_stackFuncPtrSym = sym;
  6422. }
  6423. return sym;
  6424. }
  6425. void
  6426. IRBuilder::GenerateLoopBodySlotAccesses(uint offset)
  6427. {
  6428. //
  6429. // The interpreter instance is passed as 0th argument to the JITted loop body function.
  6430. // Always load the argument, then use it to generate any necessary store-slots.
  6431. //
  6432. uint16 argument = 0;
  6433. StackSym *symSrc = StackSym::NewParamSlotSym(argument + 1, m_func);
  6434. symSrc->m_offset = (argument + LowererMD::GetFormalParamOffset()) * MachPtr;
  6435. symSrc->m_allocated = true;
  6436. m_func->SetHasImplicitParamLoad();
  6437. IR::SymOpnd *srcOpnd = IR::SymOpnd::New(symSrc, TyVar, m_func);
  6438. StackSym *loopParamSym = m_func->EnsureLoopParamSym();
  6439. IR::RegOpnd *loopParamOpnd = IR::RegOpnd::New(loopParamSym, TyMachPtr, m_func);
  6440. IR::Instr *instrArgIn = IR::Instr::New(Js::OpCode::ArgIn_A, loopParamOpnd, srcOpnd, m_func);
  6441. m_func->m_headInstr->InsertAfter(instrArgIn);
  6442. StackSym *stackFuncPtrSym = this->m_stackFuncPtrSym;
  6443. if (stackFuncPtrSym)
  6444. {
  6445. PropertySym * fieldSym = PropertySym::FindOrCreate(loopParamSym->m_id, (Js::PropertyId)(Js::InterpreterStackFrame::GetOffsetOfStackNestedFunctions() / sizeof(Js::Var)), (uint32)-1, (uint)-1, PropertyKindLocalSlots, m_func);
  6446. IR::SymOpnd * opndPtrRef = IR::SymOpnd::New(fieldSym, TyVar, m_func);
  6447. IR::Instr * instrPtrInit = IR::Instr::New(Js::OpCode::LdSlot, IR::RegOpnd::New(stackFuncPtrSym, TyVar, m_func), opndPtrRef, m_func);
  6448. instrArgIn->InsertAfter(instrPtrInit);
  6449. }
  6450. GenerateLoopBodyStSlots(loopParamSym->m_id, offset);
  6451. }
  6452. void
  6453. IRBuilder::GenerateLoopBodyStSlots(SymID loopParamSymId, uint offset)
  6454. {
  6455. if (this->m_stSlots->Count() == 0)
  6456. {
  6457. return;
  6458. }
  6459. FOREACH_BITSET_IN_FIXEDBV(regSlot, this->m_stSlots)
  6460. {
  6461. this->GenerateLoopBodyStSlot(regSlot, offset);
  6462. }
  6463. NEXT_BITSET_IN_FIXEDBV;
  6464. }
  6465. IR::Instr *
  6466. IRBuilder::GenerateLoopBodyStSlot(Js::RegSlot regSlot, uint offset)
  6467. {
  6468. Assert(!this->RegIsConstant((Js::RegSlot)regSlot));
  6469. StackSym *loopParamSym = m_func->EnsureLoopParamSym();
  6470. PropertySym * fieldSym = PropertySym::FindOrCreate(loopParamSym->m_id, (Js::PropertyId)(regSlot + this->m_loopBodyLocalsStartSlot), (uint32)-1, (uint)-1, PropertyKindLocalSlots, m_func);
  6471. IR::SymOpnd * fieldSymOpnd = IR::SymOpnd::New(fieldSym, TyVar, m_func);
  6472. IR::RegOpnd * regOpnd = this->BuildSrcOpnd((Js::RegSlot)regSlot);
  6473. #if !FLOATVAR
  6474. Js::OpCode opcode = Js::OpCode::StSlotBoxTemp;
  6475. #else
  6476. Js::OpCode opcode = Js::OpCode::StSlot;
  6477. #endif
  6478. IR::Instr * stSlotInstr = IR::Instr::New(opcode, fieldSymOpnd, regOpnd, m_func);
  6479. if (offset != Js::Constants::NoByteCodeOffset)
  6480. {
  6481. this->AddInstr(stSlotInstr, offset);
  6482. return nullptr;
  6483. }
  6484. else
  6485. {
  6486. return stSlotInstr;
  6487. }
  6488. }
  6489. IR::Instr *
  6490. IRBuilder::CreateLoopBodyReturnIPInstr(uint targetOffset, uint offset)
  6491. {
  6492. IR::RegOpnd * retOpnd = IR::RegOpnd::New(m_loopBodyRetIPSym, TyMachReg, m_func);
  6493. IR::IntConstOpnd * exitOffsetOpnd = IR::IntConstOpnd::New(targetOffset, TyMachReg, m_func);
  6494. return IR::Instr::New(Js::OpCode::Ld_I4, retOpnd, exitOffsetOpnd, m_func);
  6495. }
  6496. IR::Opnd *
  6497. IRBuilder::InsertLoopBodyReturnIPInstr(uint targetOffset, uint offset)
  6498. {
  6499. IR::Instr * setRetValueInstr = CreateLoopBodyReturnIPInstr(targetOffset, offset);
  6500. this->AddInstr(setRetValueInstr, offset);
  6501. return setRetValueInstr->GetDst();
  6502. }
  6503. void
  6504. IRBuilder::InsertDoneLoopBodyLoopCounter(uint32 lastOffset)
  6505. {
  6506. if (m_loopCounterSym == nullptr)
  6507. {
  6508. return;
  6509. }
  6510. IR::Instr * loopCounterStoreInstr = IR::Instr::New(Js::OpCode::StLoopBodyCount, m_func);
  6511. IR::RegOpnd *countRegOpnd = IR::RegOpnd::New(m_loopCounterSym, TyInt32, this->m_func);
  6512. countRegOpnd->SetIsJITOptimizedReg(true);
  6513. loopCounterStoreInstr->SetSrc1(countRegOpnd);
  6514. this->AddInstr(loopCounterStoreInstr, lastOffset + 1);
  6515. return;
  6516. }
  6517. void
  6518. IRBuilder::InsertIncrLoopBodyLoopCounter(IR::LabelInstr *loopTopLabelInstr)
  6519. {
  6520. Assert(this->IsLoopBody());
  6521. IR::RegOpnd *loopCounterOpnd = IR::RegOpnd::New(m_loopCounterSym, TyInt32, this->m_func);
  6522. IR::Instr * incr = IR::Instr::New(Js::OpCode::IncrLoopBodyCount, loopCounterOpnd, loopCounterOpnd, this->m_func);
  6523. loopCounterOpnd->SetIsJITOptimizedReg(true);
  6524. IR::Instr* nextRealInstr = loopTopLabelInstr->GetNextRealInstr();
  6525. InsertInstr(incr, nextRealInstr);
  6526. }
  6527. void
  6528. IRBuilder::InsertInitLoopBodyLoopCounter(uint loopNum)
  6529. {
  6530. Assert(this->IsLoopBody());
  6531. intptr_t loopHeader = m_func->GetJITFunctionBody()->GetLoopHeaderAddr(loopNum);
  6532. Assert(m_func->GetWorkItem()->GetLoopHeaderAddr() == loopHeader); //Init only once
  6533. m_loopCounterSym = StackSym::New(TyVar, this->m_func);
  6534. IR::RegOpnd* loopCounterOpnd = IR::RegOpnd::New(m_loopCounterSym, TyVar, this->m_func);
  6535. loopCounterOpnd->SetIsJITOptimizedReg(true);
  6536. IR::Instr * initInstr = IR::Instr::New(Js::OpCode::InitLoopBodyCount, loopCounterOpnd, this->m_func);
  6537. m_lastInstr->InsertAfter(initInstr);
  6538. m_lastInstr = initInstr;
  6539. initInstr->SetByteCodeOffset(m_jnReader.GetCurrentOffset());
  6540. }
  6541. IR::AddrOpnd *
  6542. IRBuilder::BuildAuxArrayOpnd(AuxArrayValue auxArrayType, uint32 auxArrayOffset)
  6543. {
  6544. switch (auxArrayType)
  6545. {
  6546. case AuxArrayValue::AuxPropertyIdArray:
  6547. case AuxArrayValue::AuxIntArray:
  6548. case AuxArrayValue::AuxFloatArray:
  6549. case AuxArrayValue::AuxVarsArray:
  6550. case AuxArrayValue::AuxFuncInfoArray:
  6551. case AuxArrayValue::AuxVarArrayVarCount:
  6552. {
  6553. IR::AddrOpnd * opnd = IR::AddrOpnd::New(m_func->GetJITFunctionBody()->GetAuxDataAddr(auxArrayOffset), IR::AddrOpndKindDynamicAuxBufferRef, m_func);
  6554. opnd->m_metadata = m_func->GetJITFunctionBody()->ReadFromAuxData(auxArrayOffset);
  6555. return opnd;
  6556. }
  6557. default:
  6558. Assert(UNREACHED);
  6559. return nullptr;
  6560. }
  6561. }
  6562. IR::Opnd *
  6563. IRBuilder::BuildAuxObjectLiteralTypeRefOpnd(int objectId)
  6564. {
  6565. return IR::AddrOpnd::New(m_func->GetJITFunctionBody()->GetObjectLiteralTypeRef(objectId), IR::AddrOpndKindDynamicMisc, this->m_func);
  6566. }
  6567. void
  6568. IRBuilder::DoClosureRegCheck(Js::RegSlot reg)
  6569. {
  6570. if (reg == Js::Constants::NoRegister)
  6571. {
  6572. return;
  6573. }
  6574. if (reg == m_func->GetJITFunctionBody()->GetEnvReg() ||
  6575. reg == m_func->GetJITFunctionBody()->GetLocalClosureReg() ||
  6576. reg == m_func->GetJITFunctionBody()->GetLocalFrameDisplayReg() ||
  6577. reg == m_func->GetJITFunctionBody()->GetParamClosureReg())
  6578. {
  6579. Js::Throw::FatalInternalError();
  6580. }
  6581. }
  6582. Js::RegSlot
  6583. IRBuilder::InnerScopeIndexToRegSlot(uint32 index) const
  6584. {
  6585. if (index >= m_func->GetJITFunctionBody()->GetInnerScopeCount())
  6586. {
  6587. Js::Throw::FatalInternalError();
  6588. }
  6589. Js::RegSlot reg = m_func->GetJITFunctionBody()->GetFirstInnerScopeReg() + index;
  6590. if (reg >= m_func->GetJITFunctionBody()->GetLocalsCount())
  6591. {
  6592. Js::Throw::FatalInternalError();
  6593. }
  6594. return reg;
  6595. }
  6596. bool
  6597. IRBuilder::DoLoadInstructionArrayProfileInfo()
  6598. {
  6599. return !(!this->m_func->HasProfileInfo() ||
  6600. (
  6601. PHASE_OFF(Js::TypedArrayPhase, this->m_func->GetTopFunc()) &&
  6602. PHASE_OFF(Js::ArrayCheckHoistPhase, this->m_func)
  6603. ));
  6604. }
  6605. bool
  6606. IRBuilder::AllowNativeArrayProfileInfo()
  6607. {
  6608. return !((!(m_func->GetTopFunc()->HasTry() && !m_func->GetTopFunc()->DoOptimizeTry()) && m_func->GetWeakFuncRef() && !m_func->HasArrayInfo()) ||
  6609. m_func->IsJitInDebugMode());
  6610. }