IRBuilder.cpp 272 KB

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