LowerMDShared.cpp 355 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft Corporation and contributors. 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 "Language/JavascriptFunctionArgIndex.h"
  7. const Js::OpCode LowererMD::MDUncondBranchOpcode = Js::OpCode::JMP;
  8. const Js::OpCode LowererMD::MDTestOpcode = Js::OpCode::TEST;
  9. const Js::OpCode LowererMD::MDOrOpcode = Js::OpCode::OR;
  10. const Js::OpCode LowererMD::MDXorOpcode = Js::OpCode::XOR;
  11. #if _M_X64
  12. const Js::OpCode LowererMD::MDMovUint64ToFloat64Opcode = Js::OpCode::MOVQ;
  13. #endif
  14. const Js::OpCode LowererMD::MDOverflowBranchOpcode = Js::OpCode::JO;
  15. const Js::OpCode LowererMD::MDNotOverflowBranchOpcode = Js::OpCode::JNO;
  16. const Js::OpCode LowererMD::MDConvertFloat32ToFloat64Opcode = Js::OpCode::CVTSS2SD;
  17. const Js::OpCode LowererMD::MDConvertFloat64ToFloat32Opcode = Js::OpCode::CVTSD2SS;
  18. const Js::OpCode LowererMD::MDCallOpcode = Js::OpCode::CALL;
  19. const Js::OpCode LowererMD::MDImulOpcode = Js::OpCode::IMUL2;
  20. static const int TWO_31_FLOAT = 0x4f000000;
  21. static const int FLOAT_INT_MIN = 0xcf000000;
  22. //
  23. // Static utility fn()
  24. //
  25. bool
  26. LowererMD::IsAssign(IR::Instr *instr)
  27. {
  28. return instr->GetDst() && instr->m_opcode == LowererMDArch::GetAssignOp(instr->GetDst()->GetType());
  29. }
  30. ///----------------------------------------------------------------------------
  31. ///
  32. /// LowererMD::IsCall
  33. ///
  34. ///----------------------------------------------------------------------------
  35. bool
  36. LowererMD::IsCall(IR::Instr *instr)
  37. {
  38. return instr->m_opcode == Js::OpCode::CALL;
  39. }
  40. ///----------------------------------------------------------------------------
  41. ///
  42. /// LowererMD::IsUnconditionalBranch
  43. ///
  44. ///----------------------------------------------------------------------------
  45. bool
  46. LowererMD::IsUnconditionalBranch(const IR::Instr *instr)
  47. {
  48. return (instr->m_opcode == Js::OpCode::JMP);
  49. }
  50. // GenerateMemRef: Return an opnd that can be used to access the given address.
  51. IR::Opnd *
  52. LowererMD::GenerateMemRef(intptr_t addr, IRType type, IR::Instr *instr, bool dontEncode)
  53. {
  54. return IR::MemRefOpnd::New(addr, type, this->m_func);
  55. }
  56. ///----------------------------------------------------------------------------
  57. ///
  58. /// LowererMD::InvertBranch
  59. ///
  60. ///----------------------------------------------------------------------------
  61. void
  62. LowererMD::InvertBranch(IR::BranchInstr *branchInstr)
  63. {
  64. switch (branchInstr->m_opcode)
  65. {
  66. case Js::OpCode::JA:
  67. branchInstr->m_opcode = Js::OpCode::JBE;
  68. break;
  69. case Js::OpCode::JAE:
  70. branchInstr->m_opcode = Js::OpCode::JB;
  71. break;
  72. case Js::OpCode::JB:
  73. branchInstr->m_opcode = Js::OpCode::JAE;
  74. break;
  75. case Js::OpCode::JBE:
  76. branchInstr->m_opcode = Js::OpCode::JA;
  77. break;
  78. case Js::OpCode::JEQ:
  79. branchInstr->m_opcode = Js::OpCode::JNE;
  80. break;
  81. case Js::OpCode::JNE:
  82. branchInstr->m_opcode = Js::OpCode::JEQ;
  83. break;
  84. case Js::OpCode::JGE:
  85. branchInstr->m_opcode = Js::OpCode::JLT;
  86. break;
  87. case Js::OpCode::JGT:
  88. branchInstr->m_opcode = Js::OpCode::JLE;
  89. break;
  90. case Js::OpCode::JLT:
  91. branchInstr->m_opcode = Js::OpCode::JGE;
  92. break;
  93. case Js::OpCode::JLE:
  94. branchInstr->m_opcode = Js::OpCode::JGT;
  95. break;
  96. case Js::OpCode::JO:
  97. branchInstr->m_opcode = Js::OpCode::JNO;
  98. break;
  99. case Js::OpCode::JNO:
  100. branchInstr->m_opcode = Js::OpCode::JO;
  101. break;
  102. case Js::OpCode::JP:
  103. branchInstr->m_opcode = Js::OpCode::JNP;
  104. break;
  105. case Js::OpCode::JNP:
  106. branchInstr->m_opcode = Js::OpCode::JP;
  107. break;
  108. case Js::OpCode::JSB:
  109. branchInstr->m_opcode = Js::OpCode::JNSB;
  110. break;
  111. case Js::OpCode::JNSB:
  112. branchInstr->m_opcode = Js::OpCode::JSB;
  113. break;
  114. default:
  115. AssertMsg(UNREACHED, "JCC missing in InvertBranch()");
  116. }
  117. }
  118. void
  119. LowererMD::ReverseBranch(IR::BranchInstr *branchInstr)
  120. {
  121. switch (branchInstr->m_opcode)
  122. {
  123. case Js::OpCode::JA:
  124. branchInstr->m_opcode = Js::OpCode::JB;
  125. break;
  126. case Js::OpCode::JAE:
  127. branchInstr->m_opcode = Js::OpCode::JBE;
  128. break;
  129. case Js::OpCode::JB:
  130. branchInstr->m_opcode = Js::OpCode::JA;
  131. break;
  132. case Js::OpCode::JBE:
  133. branchInstr->m_opcode = Js::OpCode::JAE;
  134. break;
  135. case Js::OpCode::JGE:
  136. branchInstr->m_opcode = Js::OpCode::JLE;
  137. break;
  138. case Js::OpCode::JGT:
  139. branchInstr->m_opcode = Js::OpCode::JLT;
  140. break;
  141. case Js::OpCode::JLT:
  142. branchInstr->m_opcode = Js::OpCode::JGT;
  143. break;
  144. case Js::OpCode::JLE:
  145. branchInstr->m_opcode = Js::OpCode::JGE;
  146. break;
  147. case Js::OpCode::JEQ:
  148. case Js::OpCode::JNE:
  149. case Js::OpCode::JO:
  150. case Js::OpCode::JNO:
  151. case Js::OpCode::JP:
  152. case Js::OpCode::JNP:
  153. case Js::OpCode::JSB:
  154. case Js::OpCode::JNSB:
  155. break;
  156. default:
  157. AssertMsg(UNREACHED, "JCC missing in ReverseBranch()");
  158. }
  159. }
  160. IR::Instr *
  161. LowererMD::LowerCallHelper(IR::Instr *instrCall)
  162. {
  163. IR::Opnd *argOpnd = instrCall->UnlinkSrc2();
  164. IR::Instr *prevInstr = nullptr;
  165. IR::JnHelperMethod helperMethod = instrCall->GetSrc1()->AsHelperCallOpnd()->m_fnHelper;
  166. instrCall->FreeSrc1();
  167. #ifndef _M_X64
  168. prevInstr = ChangeToHelperCall(instrCall, helperMethod);
  169. #endif
  170. prevInstr = instrCall;
  171. while (argOpnd)
  172. {
  173. Assert(argOpnd->IsRegOpnd());
  174. IR::RegOpnd *regArg = argOpnd->AsRegOpnd();
  175. Assert(regArg->m_sym->m_isSingleDef);
  176. IR::Instr *instrArg = regArg->m_sym->m_instrDef;
  177. Assert(instrArg->m_opcode == Js::OpCode::ArgOut_A ||
  178. (helperMethod == IR::JnHelperMethod::HelperOP_InitCachedScope && instrArg->m_opcode == Js::OpCode::ExtendArg_A));
  179. prevInstr = LoadHelperArgument(prevInstr, instrArg->GetSrc1());
  180. argOpnd = instrArg->GetSrc2();
  181. if (prevInstr == instrArg)
  182. {
  183. prevInstr = prevInstr->m_prev;
  184. }
  185. if (instrArg->m_opcode == Js::OpCode::ArgOut_A)
  186. {
  187. instrArg->UnlinkSrc1();
  188. if (argOpnd)
  189. {
  190. instrArg->UnlinkSrc2();
  191. }
  192. regArg->Free(this->m_func);
  193. instrArg->Remove();
  194. }
  195. }
  196. prevInstr = m_lowerer->LoadScriptContext(prevInstr);
  197. #ifdef _M_X64
  198. FlipHelperCallArgsOrder();
  199. ChangeToHelperCall(instrCall, helperMethod);
  200. #else
  201. this->lowererMDArch.ResetHelperArgsCount();
  202. #endif
  203. // There might be ToVar in between the ArgOut, need to continue lower from the call still
  204. return instrCall;
  205. }
  206. //
  207. // forwarding functions
  208. //
  209. IR::Instr *
  210. LowererMD::LowerCall(IR::Instr * callInstr, Js::ArgSlot argCount)
  211. {
  212. return this->lowererMDArch.LowerCall(callInstr, argCount);
  213. }
  214. IR::Instr *
  215. LowererMD::LowerCallI(IR::Instr * callInstr, ushort callFlags, bool isHelper, IR::Instr * insertBeforeInstrForCFG)
  216. {
  217. return this->lowererMDArch.LowerCallI(callInstr, callFlags, isHelper, insertBeforeInstrForCFG);
  218. }
  219. IR::Instr *
  220. LowererMD::LowerAsmJsCallI(IR::Instr * callInstr)
  221. {
  222. #if DBG
  223. if (PHASE_ON(Js::AsmjsCallDebugBreakPhase, this->m_func))
  224. {
  225. this->GenerateDebugBreak(callInstr->m_next);
  226. }
  227. #endif
  228. return this->lowererMDArch.LowerAsmJsCallI(callInstr);
  229. }
  230. IR::Instr *
  231. LowererMD::LowerAsmJsCallE(IR::Instr * callInstr)
  232. {
  233. #if DBG
  234. if (PHASE_ON(Js::AsmjsCallDebugBreakPhase, this->m_func))
  235. {
  236. this->GenerateDebugBreak(callInstr->m_next);
  237. }
  238. #endif
  239. return this->lowererMDArch.LowerAsmJsCallE(callInstr);
  240. }
  241. IR::Instr *
  242. LowererMD::LowerWasmMemOp(IR::Instr * instr, IR::Opnd *addrOpnd)
  243. {
  244. return this->lowererMDArch.LowerWasmMemOp(instr, addrOpnd);
  245. }
  246. IR::Instr *
  247. LowererMD::LowerAsmJsLdElemHelper(IR::Instr * callInstr)
  248. {
  249. return this->lowererMDArch.LowerAsmJsLdElemHelper(callInstr);
  250. }
  251. IR::Instr *
  252. LowererMD::LowerAsmJsStElemHelper(IR::Instr * callInstr)
  253. {
  254. return this->lowererMDArch.LowerAsmJsStElemHelper(callInstr);
  255. }
  256. IR::Instr *
  257. LowererMD::LowerCallPut(IR::Instr * callInstr)
  258. {
  259. int32 argCount = this->lowererMDArch.LowerCallArgs(callInstr, Js::CallFlags_None, 2);
  260. // load native entry point from script function into eax
  261. IR::Opnd * functionWrapOpnd = callInstr->UnlinkSrc1();
  262. AssertMsg(functionWrapOpnd->IsRegOpnd() && functionWrapOpnd->AsRegOpnd()->m_sym->IsStackSym(),
  263. "Expected call src to be stackSym");
  264. this->LoadHelperArgument(callInstr, functionWrapOpnd);
  265. this->m_lowerer->LoadScriptContext(callInstr);
  266. IR::HelperCallOpnd *helperCallOpnd = IR::HelperCallOpnd::New(IR::HelperOp_InvokePut, this->m_func);
  267. callInstr->SetSrc1(helperCallOpnd);
  268. return this->lowererMDArch.LowerCall(callInstr, argCount);
  269. }
  270. IR::Instr *
  271. LowererMD::LoadInt64HelperArgument(IR::Instr * instr, IR::Opnd* opnd)
  272. {
  273. return this->lowererMDArch.LoadInt64HelperArgument(instr, opnd);
  274. }
  275. IR::Instr *
  276. LowererMD::LoadHelperArgument(IR::Instr * instr, IR::Opnd * opndArg)
  277. {
  278. return this->lowererMDArch.LoadHelperArgument(instr, opndArg);
  279. }
  280. IR::Instr *
  281. LowererMD::LoadDoubleHelperArgument(IR::Instr * instr, IR::Opnd * opndArg)
  282. {
  283. return this->lowererMDArch.LoadDoubleHelperArgument(instr, opndArg);
  284. }
  285. IR::Instr *
  286. LowererMD::LoadFloatHelperArgument(IR::Instr * instr, IR::Opnd * opndArg)
  287. {
  288. return this->lowererMDArch.LoadFloatHelperArgument(instr, opndArg);
  289. }
  290. IR::Instr *
  291. LowererMD::LowerEntryInstr(IR::EntryInstr * entryInstr)
  292. {
  293. return this->lowererMDArch.LowerEntryInstr(entryInstr);
  294. }
  295. IR::Instr *
  296. LowererMD::LowerExitInstr(IR::ExitInstr * exitInstr)
  297. {
  298. return this->lowererMDArch.LowerExitInstr(exitInstr);
  299. }
  300. IR::Instr *
  301. LowererMD::LowerEntryInstrAsmJs(IR::EntryInstr * entryInstr)
  302. {
  303. return this->lowererMDArch.LowerEntryInstrAsmJs(entryInstr);
  304. }
  305. IR::Instr *
  306. LowererMD::LowerExitInstrAsmJs(IR::ExitInstr * exitInstr)
  307. {
  308. return this->lowererMDArch.LowerExitInstrAsmJs(exitInstr);
  309. }
  310. IR::Instr *
  311. LowererMD::LoadNewScObjFirstArg(IR::Instr * instr, IR::Opnd * dst, ushort extraArgs)
  312. {
  313. return this->lowererMDArch.LoadNewScObjFirstArg(instr, dst, extraArgs);
  314. }
  315. IR::Instr *
  316. LowererMD::LowerTry(IR::Instr *tryInstr, IR::JnHelperMethod helperMethod)
  317. {
  318. // Mark the entry to the try
  319. IR::Instr *instr = tryInstr->GetNextRealInstrOrLabel();
  320. AssertMsg(instr->IsLabelInstr(), "No label at the entry to a try?");
  321. IR::LabelInstr *tryAddr = instr->AsLabelInstr();
  322. // Arg 5: ScriptContext
  323. this->m_lowerer->LoadScriptContext(tryAddr);
  324. if (tryInstr->m_opcode == Js::OpCode::TryCatch)
  325. {
  326. // Arg 4 : hasBailedOutOffset
  327. IR::Opnd * hasBailedOutOffset = IR::IntConstOpnd::New(this->m_func->m_hasBailedOutSym->m_offset, TyInt32, this->m_func);
  328. this->LoadHelperArgument(tryAddr, hasBailedOutOffset);
  329. }
  330. #ifdef _M_X64
  331. // Arg: args size
  332. IR::RegOpnd *argsSizeOpnd = IR::RegOpnd::New(TyMachReg, m_func);
  333. tryAddr->InsertBefore(IR::Instr::New(Js::OpCode::LdArgSize, argsSizeOpnd, this->m_func));
  334. this->LoadHelperArgument(tryAddr, argsSizeOpnd);
  335. // Arg: spill size
  336. IR::RegOpnd *spillSizeOpnd = IR::RegOpnd::New(TyMachReg, m_func);
  337. tryAddr->InsertBefore(IR::Instr::New(Js::OpCode::LdSpillSize, spillSizeOpnd, this->m_func));
  338. this->LoadHelperArgument(tryAddr, spillSizeOpnd);
  339. #endif
  340. // Arg 3: frame pointer
  341. IR::RegOpnd *ebpOpnd = IR::RegOpnd::New(nullptr, lowererMDArch.GetRegBlockPointer(), TyMachReg, this->m_func);
  342. this->LoadHelperArgument(tryAddr, ebpOpnd);
  343. // Arg 2: handler address
  344. IR::LabelInstr *helperAddr = tryInstr->AsBranchInstr()->GetTarget();
  345. this->LoadHelperArgument(tryAddr, IR::LabelOpnd::New(helperAddr, this->m_func));
  346. // Arg 1: try address
  347. this->LoadHelperArgument(tryAddr, IR::LabelOpnd::New(tryAddr, this->m_func));
  348. // Call the helper
  349. IR::RegOpnd *continuationAddr =
  350. IR::RegOpnd::New(StackSym::New(TyMachReg, this->m_func), lowererMDArch.GetRegReturn(TyMachReg), TyMachReg, this->m_func);
  351. IR::Instr *callInstr = IR::Instr::New(
  352. Js::OpCode::Call, continuationAddr, IR::HelperCallOpnd::New(helperMethod, this->m_func), this->m_func);
  353. tryAddr->InsertBefore(callInstr);
  354. this->LowerCall(callInstr, 0);
  355. #ifdef _M_X64
  356. {
  357. // Emit some instruction to separate the CALL from the JMP following it. The OS stack unwinder
  358. // mistakes the JMP for the start of the epilog otherwise.
  359. IR::Instr *nop = IR::Instr::New(Js::OpCode::NOP, m_func);
  360. tryAddr->InsertBefore(nop);
  361. }
  362. #endif
  363. // Jump to the continuation address supplied by the helper
  364. IR::BranchInstr *branchInstr = IR::MultiBranchInstr::New(Js::OpCode::JMP, continuationAddr, this->m_func);
  365. tryAddr->InsertBefore(branchInstr);
  366. return tryInstr->m_prev;
  367. }
  368. IR::Instr *
  369. LowererMD::LowerLeave(IR::Instr *leaveInstr, IR::LabelInstr *targetInstr, bool fromFinalLower, bool isOrphanedLeave)
  370. {
  371. if (isOrphanedLeave)
  372. {
  373. Assert(this->m_func->IsLoopBodyInTry());
  374. leaveInstr->m_opcode = Js::OpCode::JMP;
  375. return leaveInstr->m_prev;
  376. }
  377. IR::Instr *instrPrev = leaveInstr->m_prev;
  378. IR::LabelOpnd *labelOpnd = IR::LabelOpnd::New(targetInstr, this->m_func);
  379. lowererMDArch.LowerEHRegionReturn(leaveInstr, labelOpnd);
  380. if (fromFinalLower)
  381. {
  382. instrPrev = leaveInstr->m_prev; // Need to lower LdArgSize and LdSpillSize
  383. }
  384. leaveInstr->Remove();
  385. return instrPrev;
  386. }
  387. IR::Instr *
  388. LowererMD::LowerEHRegionReturn(IR::Instr * insertBeforeInstr, IR::Opnd * targetOpnd)
  389. {
  390. return lowererMDArch.LowerEHRegionReturn(insertBeforeInstr, targetOpnd);
  391. }
  392. IR::Instr *
  393. LowererMD::LowerLeaveNull(IR::Instr *finallyEndInstr)
  394. {
  395. IR::Instr *instrPrev = finallyEndInstr->m_prev;
  396. IR::Instr *instr = nullptr;
  397. // Return a null continuation address to the helper: execution will resume at the point determined by the try
  398. // or the exception handler.
  399. IR::RegOpnd *retReg = IR::RegOpnd::New(StackSym::New(TyMachReg,this->m_func), lowererMDArch.GetRegReturn(TyMachReg), TyMachReg, this->m_func);
  400. instr = IR::Instr::New(Js::OpCode::XOR, retReg, this->m_func);
  401. IR::RegOpnd *eaxOpnd = IR::RegOpnd::New(nullptr, lowererMDArch.GetRegReturn(TyMachReg), TyMachReg, this->m_func);
  402. instr->SetSrc1(eaxOpnd);
  403. instr->SetSrc2(eaxOpnd);
  404. finallyEndInstr->InsertBefore(instr);
  405. #if _M_X64
  406. {
  407. // amd64_ReturnFromCallWithFakeFrame expects to find the spill size and args size
  408. // in REG_EH_SPILL_SIZE and REG_EH_ARGS_SIZE.
  409. // MOV REG_EH_SPILL_SIZE, spillSize
  410. IR::Instr *movR8 = IR::Instr::New(Js::OpCode::LdSpillSize,
  411. IR::RegOpnd::New(nullptr, REG_EH_SPILL_SIZE, TyMachReg, m_func),
  412. m_func);
  413. finallyEndInstr->InsertBefore(movR8);
  414. // MOV REG_EH_ARGS_SIZE, argsSize
  415. IR::Instr *movR9 = IR::Instr::New(Js::OpCode::LdArgSize,
  416. IR::RegOpnd::New(nullptr, REG_EH_ARGS_SIZE, TyMachReg, m_func),
  417. m_func);
  418. finallyEndInstr->InsertBefore(movR9);
  419. IR::Opnd *targetOpnd = IR::RegOpnd::New(nullptr, REG_EH_TARGET, TyMachReg, m_func);
  420. IR::Instr *movTarget = IR::Instr::New(Js::OpCode::MOV,
  421. targetOpnd,
  422. IR::HelperCallOpnd::New(IR::HelperOp_ReturnFromCallWithFakeFrame, m_func),
  423. m_func);
  424. finallyEndInstr->InsertBefore(movTarget);
  425. IR::Instr *push = IR::Instr::New(Js::OpCode::PUSH, m_func);
  426. push->SetSrc1(targetOpnd);
  427. finallyEndInstr->InsertBefore(push);
  428. }
  429. #endif
  430. IR::IntConstOpnd *intSrc = IR::IntConstOpnd::New(0, TyInt32, this->m_func);
  431. instr = IR::Instr::New(Js::OpCode::RET, this->m_func);
  432. instr->SetSrc1(intSrc);
  433. instr->SetSrc2(retReg);
  434. finallyEndInstr->InsertBefore(instr);
  435. finallyEndInstr->Remove();
  436. return instrPrev;
  437. }
  438. ///----------------------------------------------------------------------------
  439. ///
  440. /// LowererMD::Init
  441. ///
  442. ///----------------------------------------------------------------------------
  443. void
  444. LowererMD::Init(Lowerer *lowerer)
  445. {
  446. m_lowerer = lowerer;
  447. this->lowererMDArch.Init(this);
  448. #ifdef ENABLE_SIMDJS
  449. Simd128InitOpcodeMap();
  450. #endif
  451. }
  452. ///----------------------------------------------------------------------------
  453. ///
  454. /// LowererMD::LoadInputParamCount
  455. ///
  456. /// Load the passed-in parameter count from the appropriate EBP slot.
  457. ///
  458. ///----------------------------------------------------------------------------
  459. IR::Instr *
  460. LowererMD::LoadInputParamCount(IR::Instr * instrInsert, int adjust, bool needFlags)
  461. {
  462. IR::Instr * instr;
  463. IR::RegOpnd * dstOpnd;
  464. IR::SymOpnd * srcOpnd;
  465. srcOpnd = Lowerer::LoadCallInfo(instrInsert);
  466. dstOpnd = IR::RegOpnd::New(StackSym::New(TyMachReg, this->m_func), TyMachReg, this->m_func);
  467. instr = IR::Instr::New(Js::OpCode::MOV, dstOpnd, srcOpnd, this->m_func);
  468. instrInsert->InsertBefore(instr);
  469. // Copy the callinfo before masking off the param count
  470. Assert(Js::CallInfo::ksizeofCount == 24);
  471. // Mask off call flags from callinfo
  472. instr = IR::Instr::New(Js::OpCode::AND, dstOpnd, dstOpnd,
  473. IR::IntConstOpnd::New((Js::CallFlags_ExtraArg << static_cast<unsigned>(Js::CallInfo::ksizeofCount)) | 0x00FFFFFF, TyMachReg, this->m_func, true), this->m_func);
  474. instrInsert->InsertBefore(instr);
  475. // Shift and mask the "calling eval" bit and subtract it from the incoming count.
  476. // ("Calling eval" means the last param is the frame display, which only the eval built-in should see.)
  477. instr = IR::Instr::New(Js::OpCode::BTR, dstOpnd, dstOpnd, IR::IntConstOpnd::New(Math::Log2(Js::CallFlags_ExtraArg) + Js::CallInfo::ksizeofCount, TyInt8, this->m_func), this->m_func);
  478. instrInsert->InsertBefore(instr);
  479. instr = IR::Instr::New(Js::OpCode::SBB, dstOpnd, dstOpnd, IR::IntConstOpnd::New(-adjust, TyMachReg, this->m_func), this->m_func);
  480. instrInsert->InsertBefore(instr);
  481. return instr;
  482. }
  483. IR::Instr *
  484. LowererMD::LoadStackArgPtr(IR::Instr * instr)
  485. {
  486. if (this->m_func->IsLoopBody())
  487. {
  488. // Get the first user param from the interpreter frame instance that was passed in.
  489. // These args don't include the func object and callinfo; we just need to advance past "this".
  490. // t1 = MOV [prm1 + m_inParams]
  491. // dst = LEA &[t1 + sizeof(var)]
  492. Assert(this->m_func->m_loopParamSym);
  493. IR::RegOpnd *baseOpnd = IR::RegOpnd::New(this->m_func->m_loopParamSym, TyMachReg, this->m_func);
  494. size_t offset = Js::InterpreterStackFrame::GetOffsetOfInParams();
  495. IR::IndirOpnd *indirOpnd = IR::IndirOpnd::New(baseOpnd, (int32)offset, TyMachReg, this->m_func);
  496. IR::RegOpnd *tmpOpnd = IR::RegOpnd::New(TyMachReg, this->m_func);
  497. IR::Instr *instrLdParams = IR::Instr::New(Js::OpCode::MOV, tmpOpnd, indirOpnd, this->m_func);
  498. instr->InsertBefore(instrLdParams);
  499. indirOpnd = IR::IndirOpnd::New(tmpOpnd, sizeof(Js::Var), TyMachReg, this->m_func);
  500. instr->SetSrc1(indirOpnd);
  501. instr->m_opcode = Js::OpCode::LEA;
  502. return instr->m_prev;
  503. }
  504. else
  505. {
  506. return this->lowererMDArch.LoadStackArgPtr(instr);
  507. }
  508. }
  509. IR::Instr *
  510. LowererMD::LoadArgumentsFromFrame(IR::Instr * instr)
  511. {
  512. if (this->m_func->IsLoopBody())
  513. {
  514. // Get the arguments ptr from the interpreter frame instance that was passed in.
  515. Assert(this->m_func->m_loopParamSym);
  516. IR::RegOpnd *baseOpnd = IR::RegOpnd::New(this->m_func->m_loopParamSym, TyMachReg, this->m_func);
  517. int32 offset = (int32)Js::InterpreterStackFrame::GetOffsetOfArguments();
  518. instr->SetSrc1(IR::IndirOpnd::New(baseOpnd, offset, TyMachReg, this->m_func));
  519. }
  520. else
  521. {
  522. instr->SetSrc1(this->CreateStackArgumentsSlotOpnd());
  523. }
  524. instr->m_opcode = Js::OpCode::MOV;
  525. return instr->m_prev;
  526. }
  527. // load argument count as I4
  528. IR::Instr *
  529. LowererMD::LoadArgumentCount(IR::Instr * instr)
  530. {
  531. if (this->m_func->IsLoopBody())
  532. {
  533. // Pull the arg count from the interpreter frame instance that was passed in.
  534. // (The callinfo in the loop body's frame just shows the single parameter, the interpreter frame.)
  535. Assert(this->m_func->m_loopParamSym);
  536. IR::RegOpnd *baseOpnd = IR::RegOpnd::New(this->m_func->m_loopParamSym, TyMachReg, this->m_func);
  537. size_t offset = Js::InterpreterStackFrame::GetOffsetOfInSlotsCount();
  538. instr->SetSrc1(IR::IndirOpnd::New(baseOpnd, (int32)offset, TyInt32, this->m_func));
  539. }
  540. else
  541. {
  542. StackSym *sym = StackSym::New(TyVar, this->m_func);
  543. this->m_func->SetArgOffset(sym, (Js::JavascriptFunctionArgIndex_CallInfo - Js::JavascriptFunctionArgIndex_Frame) * sizeof(Js::Var));
  544. instr->SetSrc1(IR::SymOpnd::New(sym, TyMachReg, this->m_func));
  545. }
  546. instr->m_opcode = Js::OpCode::MOV;
  547. return instr->m_prev;
  548. }
  549. IR::Instr *
  550. LowererMD::LoadHeapArguments(IR::Instr * instrArgs)
  551. {
  552. return this->lowererMDArch.LoadHeapArguments(instrArgs);
  553. }
  554. IR::Instr *
  555. LowererMD::LoadHeapArgsCached(IR::Instr * instrArgs)
  556. {
  557. return this->lowererMDArch.LoadHeapArgsCached(instrArgs);
  558. }
  559. IR::Instr *
  560. LowererMD::LoadFuncExpression(IR::Instr * instrFuncExpr)
  561. {
  562. return this->lowererMDArch.LoadFuncExpression(instrFuncExpr);
  563. }
  564. ///----------------------------------------------------------------------------
  565. ///
  566. /// LowererMD::ChangeToHelperCall
  567. ///
  568. /// Change the current instruction to a call to the given helper.
  569. ///
  570. ///----------------------------------------------------------------------------
  571. IR::Instr *
  572. LowererMD::ChangeToHelperCall(IR::Instr * callInstr, IR::JnHelperMethod helperMethod, IR::LabelInstr *labelBailOut,
  573. IR::Opnd *opndBailOutArg, IR::PropertySymOpnd *propSymOpnd, bool isHelperContinuation)
  574. {
  575. IR::Instr * bailOutInstr = callInstr;
  576. if (callInstr->HasBailOutInfo())
  577. {
  578. IR::BailOutKind bailOutKind = callInstr->GetBailOutKind();
  579. if (bailOutKind == IR::BailOutOnNotPrimitive ||
  580. bailOutKind == IR::BailOutOnPowIntIntOverflow)
  581. {
  582. callInstr = IR::Instr::New(callInstr->m_opcode, callInstr->m_func);
  583. bailOutInstr->TransferTo(callInstr);
  584. bailOutInstr->InsertBefore(callInstr);
  585. bailOutInstr->m_opcode = bailOutKind == IR::BailOutOnNotPrimitive
  586. ? Js::OpCode::BailOnNotPrimitive
  587. : Js::OpCode::BailOnPowIntIntOverflow;
  588. bailOutInstr->SetSrc1(opndBailOutArg);
  589. }
  590. else
  591. {
  592. bailOutInstr = this->m_lowerer->SplitBailOnImplicitCall(callInstr);
  593. }
  594. }
  595. callInstr->m_opcode = Js::OpCode::CALL;
  596. IR::HelperCallOpnd *helperCallOpnd = Lowerer::CreateHelperCallOpnd(helperMethod, this->lowererMDArch.GetHelperArgsCount(), m_func);
  597. if (helperCallOpnd->IsDiagHelperCallOpnd())
  598. {
  599. // Load arguments for the wrapper.
  600. this->LoadHelperArgument(callInstr, IR::AddrOpnd::New((Js::Var)IR::GetMethodOriginalAddress(m_func->GetThreadContextInfo(), helperMethod), IR::AddrOpndKindDynamicMisc, m_func));
  601. this->m_lowerer->LoadScriptContext(callInstr);
  602. }
  603. callInstr->SetSrc1(helperCallOpnd);
  604. IR::Instr * instrRet = this->lowererMDArch.LowerCall(callInstr, 0);
  605. if (bailOutInstr != callInstr)
  606. {
  607. // The bailout needs to be lowered after we lower the helper call because the helper argument
  608. // has already been loaded. We need to drain them on AMD64 before starting another helper call
  609. if (bailOutInstr->m_opcode == Js::OpCode::BailOnNotObject)
  610. {
  611. this->m_lowerer->LowerBailOnNotObject(bailOutInstr, nullptr, labelBailOut);
  612. }
  613. else if (bailOutInstr->m_opcode == Js::OpCode::BailOnNotPrimitive ||
  614. bailOutInstr->m_opcode == Js::OpCode::BailOnPowIntIntOverflow)
  615. {
  616. this->m_lowerer->LowerBailOnTrue(bailOutInstr, labelBailOut);
  617. }
  618. else if (bailOutInstr->m_opcode == Js::OpCode::BailOut)
  619. {
  620. this->m_lowerer->GenerateBailOut(bailOutInstr, nullptr, labelBailOut);
  621. }
  622. else
  623. {
  624. this->m_lowerer->LowerBailOnEqualOrNotEqual(bailOutInstr, nullptr, labelBailOut, propSymOpnd, isHelperContinuation);
  625. }
  626. }
  627. #if DBG
  628. if (PHASE_ON(Js::AsmjsCallDebugBreakPhase, this->m_func))
  629. {
  630. this->GenerateDebugBreak(instrRet->m_next);
  631. }
  632. #endif
  633. return instrRet;
  634. }
  635. IR::Instr* LowererMD::ChangeToHelperCallMem(IR::Instr * instr, IR::JnHelperMethod helperMethod)
  636. {
  637. this->m_lowerer->LoadScriptContext(instr);
  638. return this->ChangeToHelperCall(instr, helperMethod);
  639. }
  640. ///----------------------------------------------------------------------------
  641. ///
  642. /// LowererMD::ChangeToAssign
  643. ///
  644. /// Change to a MOV.
  645. ///
  646. ///----------------------------------------------------------------------------
  647. IR::Instr *
  648. LowererMD::ChangeToAssignNoBarrierCheck(IR::Instr * instr)
  649. {
  650. return ChangeToAssign(instr, instr->GetDst()->GetType());
  651. }
  652. IR::Instr *
  653. LowererMD::ChangeToAssign(IR::Instr * instr)
  654. {
  655. return ChangeToWriteBarrierAssign(instr, instr->m_func);
  656. }
  657. IR::Instr *
  658. LowererMD::ChangeToAssign(IR::Instr * instr, IRType type)
  659. {
  660. Assert(!instr->HasBailOutInfo() || instr->GetBailOutKind() == IR::BailOutExpectingString);
  661. instr->m_opcode = LowererMDArch::GetAssignOp(type);
  662. Legalize(instr);
  663. return instr;
  664. }
  665. ///----------------------------------------------------------------------------
  666. ///
  667. /// LowererMD::ChangeToLea
  668. ///
  669. /// Change to an LEA.
  670. ///
  671. ///----------------------------------------------------------------------------
  672. IR::Instr *
  673. LowererMD::ChangeToLea(IR::Instr * instr, bool postRegAlloc)
  674. {
  675. Assert(instr);
  676. Assert(instr->GetDst());
  677. Assert(instr->GetDst()->IsRegOpnd());
  678. Assert(instr->GetSrc1());
  679. Assert(instr->GetSrc1()->IsIndirOpnd() || instr->GetSrc1()->IsSymOpnd());
  680. Assert(!instr->GetSrc2());
  681. instr->m_opcode = Js::OpCode::LEA;
  682. return instr;
  683. }
  684. ///----------------------------------------------------------------------------
  685. ///
  686. /// LowererMD::CreateAssign
  687. ///
  688. /// Create a MOV.
  689. ///
  690. ///----------------------------------------------------------------------------
  691. IR::Instr *
  692. LowererMD::CreateAssign(IR::Opnd *dst, IR::Opnd *src, IR::Instr *instrInsertPt, bool generateWriteBarrier)
  693. {
  694. return Lowerer::InsertMove(dst, src, instrInsertPt, generateWriteBarrier);
  695. }
  696. ///----------------------------------------------------------------------------
  697. ///
  698. /// LowererMD::LowerRet
  699. ///
  700. /// Lower Ret to "MOV EAX, src"
  701. /// The real RET is inserted at the exit of the function when emitting the
  702. /// epilog.
  703. ///
  704. ///----------------------------------------------------------------------------
  705. IR::Instr *
  706. LowererMD::LowerRet(IR::Instr * retInstr)
  707. {
  708. IR::RegOpnd * retReg;
  709. #ifdef ASMJS_PLAT
  710. if (m_func->GetJITFunctionBody()->IsAsmJsMode() && !m_func->IsLoopBody()) // for loop body ret is the bytecodeoffset
  711. {
  712. Js::AsmJsRetType::Which asmType = m_func->GetJITFunctionBody()->GetAsmJsInfo()->GetRetType();
  713. IRType regType = TyInt32;
  714. switch (asmType)
  715. {
  716. case Js::AsmJsRetType::Double:
  717. regType = TyFloat64;
  718. break;
  719. case Js::AsmJsRetType::Float:
  720. regType = TyFloat32;
  721. break;
  722. case Js::AsmJsRetType::Int64:
  723. {
  724. regType = TyInt64;
  725. #ifdef _M_IX86
  726. regType = TyInt32;
  727. Int64RegPair srcPair = m_lowerer->FindOrCreateInt64Pair(retInstr->GetSrc1()->AsRegOpnd());
  728. retInstr->UnlinkSrc1();
  729. retInstr->SetSrc1(srcPair.low);
  730. // Mov high bits to edx
  731. IR::RegOpnd* regEdx = IR::RegOpnd::New(nullptr, RegEDX, TyInt32, this->m_func);
  732. IR::Instr* movHighInstr = IR::Instr::New(Js::OpCode::Ld_I4, regEdx, srcPair.high, this->m_func);
  733. retInstr->InsertBefore(ChangeToAssign(movHighInstr));
  734. #endif
  735. break;
  736. }
  737. case Js::AsmJsRetType::Signed:
  738. case Js::AsmJsRetType::Void:
  739. regType = TyInt32;
  740. break;
  741. case Js::AsmJsRetType::Float32x4:
  742. regType = TySimd128F4;
  743. break;
  744. case Js::AsmJsRetType::Int32x4:
  745. regType = TySimd128I4;
  746. break;
  747. case Js::AsmJsRetType::Float64x2:
  748. regType = TySimd128D2;
  749. break;
  750. case Js::AsmJsRetType::Int16x8:
  751. regType = TySimd128I8;
  752. break;
  753. case Js::AsmJsRetType::Int8x16:
  754. regType = TySimd128I16;
  755. break;
  756. case Js::AsmJsRetType::Uint32x4:
  757. regType = TySimd128U4;
  758. break;
  759. case Js::AsmJsRetType::Uint16x8:
  760. regType = TySimd128U8;
  761. break;
  762. case Js::AsmJsRetType::Uint8x16:
  763. regType = TySimd128U16;
  764. break;
  765. case Js::AsmJsRetType::Bool32x4:
  766. regType = TySimd128B4;
  767. break;
  768. case Js::AsmJsRetType::Bool16x8:
  769. regType = TySimd128B8;
  770. break;
  771. case Js::AsmJsRetType::Bool8x16:
  772. regType = TySimd128B16;
  773. break;
  774. default:
  775. Assert(UNREACHED);
  776. }
  777. retReg = IR::RegOpnd::New(nullptr, lowererMDArch.GetRegReturnAsmJs(regType), regType, m_func);
  778. }
  779. else
  780. #endif
  781. {
  782. retReg = IR::RegOpnd::New(nullptr, lowererMDArch.GetRegReturn(TyMachReg), TyMachReg, m_func);
  783. }
  784. retInstr->SetDst(retReg);
  785. return this->ChangeToAssign(retInstr);
  786. }
  787. ///----------------------------------------------------------------------------
  788. ///
  789. /// LowererMD::LowerUncondBranch
  790. ///
  791. ///----------------------------------------------------------------------------
  792. IR::Instr *
  793. LowererMD::LowerUncondBranch(IR::Instr * instr)
  794. {
  795. instr->m_opcode = Js::OpCode::JMP;
  796. return instr;
  797. }
  798. ///----------------------------------------------------------------------------
  799. ///
  800. /// LowererMD::LowerMultiBranch
  801. ///
  802. ///----------------------------------------------------------------------------
  803. IR::Instr *
  804. LowererMD::LowerMultiBranch(IR::Instr * instr)
  805. {
  806. return LowerUncondBranch(instr);
  807. }
  808. ///----------------------------------------------------------------------------
  809. ///
  810. /// LowererMD::LowerCondBranch
  811. ///
  812. ///----------------------------------------------------------------------------
  813. IR::Instr *
  814. LowererMD::LowerCondBranch(IR::Instr * instr)
  815. {
  816. AssertMsg(instr->GetSrc1() != nullptr, "Expected src opnds on conditional branch");
  817. Assert(!instr->HasBailOutInfo());
  818. IR::Opnd * opndSrc1 = instr->UnlinkSrc1();
  819. IR::Instr * instrPrev = nullptr;
  820. switch (instr->m_opcode)
  821. {
  822. case Js::OpCode::BrTrue_A:
  823. case Js::OpCode::BrFalse_A:
  824. case Js::OpCode::BrNotNull_A:
  825. case Js::OpCode::BrOnObject_A:
  826. case Js::OpCode::BrOnClassConstructor:
  827. case Js::OpCode::BrOnBaseConstructorKind:
  828. Assert(!opndSrc1->IsFloat64());
  829. AssertMsg(instr->GetSrc2() == nullptr, "Expected 1 src on boolean branch");
  830. instrPrev = IR::Instr::New(Js::OpCode::TEST, this->m_func);
  831. instrPrev->SetSrc1(opndSrc1);
  832. instrPrev->SetSrc2(opndSrc1);
  833. instr->InsertBefore(instrPrev);
  834. if (instr->m_opcode != Js::OpCode::BrFalse_A)
  835. {
  836. instr->m_opcode = Js::OpCode::JNE;
  837. }
  838. else
  839. {
  840. instr->m_opcode = Js::OpCode::JEQ;
  841. }
  842. break;
  843. case Js::OpCode::BrOnEmpty:
  844. case Js::OpCode::BrOnNotEmpty:
  845. AssertMsg(0, "BrOnEmpty opcodes should not be passed to MD lowerer");
  846. break;
  847. default:
  848. IR::Opnd * opndSrc2 = instr->UnlinkSrc2();
  849. AssertMsg(opndSrc2 != nullptr, "Expected 2 src's on non-boolean branch");
  850. if (opndSrc1->IsFloat())
  851. {
  852. Assert(opndSrc1->GetType() == opndSrc2->GetType());
  853. instrPrev = IR::Instr::New(opndSrc1->IsFloat64() ? Js::OpCode::COMISD : Js::OpCode::COMISS, m_func);
  854. instrPrev->SetSrc1(opndSrc1);
  855. instrPrev->SetSrc2(opndSrc2);
  856. instr->InsertBefore(instrPrev);
  857. }
  858. else
  859. {
  860. // This check assumes src1 is a variable.
  861. if (opndSrc2->IsIntConstOpnd() && opndSrc2->AsIntConstOpnd()->GetValue() == 0)
  862. {
  863. instrPrev = IR::Instr::New(Js::OpCode::TEST, this->m_func);
  864. instrPrev->SetSrc1(opndSrc1);
  865. instrPrev->SetSrc2(opndSrc1);
  866. instr->InsertBefore(instrPrev);
  867. opndSrc2->Free(this->m_func);
  868. }
  869. else
  870. {
  871. instrPrev = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  872. //
  873. // For 32 bit arithmetic we copy them and set the size of operands to be 32 bits. This is
  874. // relevant only on AMD64.
  875. //
  876. opndSrc1 = instrPrev->SetSrc1(opndSrc1);
  877. opndSrc2 = instrPrev->SetSrc2(opndSrc2);
  878. instr->InsertBefore(instrPrev);
  879. LowererMD::Legalize(instrPrev);
  880. }
  881. }
  882. instr->m_opcode = LowererMD::MDBranchOpcode(instr->m_opcode);
  883. break;
  884. }
  885. return instrPrev;
  886. }
  887. ///----------------------------------------------------------------------------
  888. ///
  889. /// LowererMD::MDBranchOpcode
  890. ///
  891. /// Map HIR branch opcode to machine-dependent equivalent.
  892. ///
  893. ///----------------------------------------------------------------------------
  894. Js::OpCode
  895. LowererMD::MDBranchOpcode(Js::OpCode opcode)
  896. {
  897. switch (opcode)
  898. {
  899. case Js::OpCode::BrSrEq_A:
  900. case Js::OpCode::BrEq_A:
  901. case Js::OpCode::BrSrNotNeq_A:
  902. case Js::OpCode::BrNotNeq_A:
  903. case Js::OpCode::BrAddr_A:
  904. return Js::OpCode::JEQ;
  905. case Js::OpCode::BrSrNeq_A:
  906. case Js::OpCode::BrNeq_A:
  907. case Js::OpCode::BrSrNotEq_A:
  908. case Js::OpCode::BrNotEq_A:
  909. case Js::OpCode::BrNotAddr_A:
  910. return Js::OpCode::JNE;
  911. case Js::OpCode::BrLt_A:
  912. case Js::OpCode::BrNotGe_A:
  913. return Js::OpCode::JLT;
  914. case Js::OpCode::BrLe_A:
  915. case Js::OpCode::BrNotGt_A:
  916. return Js::OpCode::JLE;
  917. case Js::OpCode::BrGt_A:
  918. case Js::OpCode::BrNotLe_A:
  919. return Js::OpCode::JGT;
  920. case Js::OpCode::BrGe_A:
  921. case Js::OpCode::BrNotLt_A:
  922. return Js::OpCode::JGE;
  923. default:
  924. AssertMsg(0, "Branch opcode has no MD mapping");
  925. return opcode;
  926. }
  927. }
  928. Js::OpCode
  929. LowererMD::MDConvertFloat64ToInt32Opcode(const RoundMode roundMode)
  930. {
  931. switch (roundMode)
  932. {
  933. case RoundModeTowardZero:
  934. return Js::OpCode::CVTTSD2SI;
  935. case RoundModeTowardInteger:
  936. return Js::OpCode::Nop;
  937. case RoundModeHalfToEven:
  938. return Js::OpCode::CVTSD2SI;
  939. default:
  940. AssertMsg(0, "RoundMode has no MD mapping.");
  941. return Js::OpCode::Nop;
  942. }
  943. }
  944. Js::OpCode
  945. LowererMD::MDUnsignedBranchOpcode(Js::OpCode opcode)
  946. {
  947. switch (opcode)
  948. {
  949. case Js::OpCode::BrEq_A:
  950. case Js::OpCode::BrSrEq_A:
  951. case Js::OpCode::BrSrNotNeq_A:
  952. case Js::OpCode::BrNotNeq_A:
  953. case Js::OpCode::BrAddr_A:
  954. return Js::OpCode::JEQ;
  955. case Js::OpCode::BrNeq_A:
  956. case Js::OpCode::BrSrNeq_A:
  957. case Js::OpCode::BrSrNotEq_A:
  958. case Js::OpCode::BrNotEq_A:
  959. case Js::OpCode::BrNotAddr_A:
  960. return Js::OpCode::JNE;
  961. case Js::OpCode::BrLt_A:
  962. case Js::OpCode::BrNotGe_A:
  963. return Js::OpCode::JB;
  964. case Js::OpCode::BrLe_A:
  965. case Js::OpCode::BrNotGt_A:
  966. return Js::OpCode::JBE;
  967. case Js::OpCode::BrGt_A:
  968. case Js::OpCode::BrNotLe_A:
  969. return Js::OpCode::JA;
  970. case Js::OpCode::BrGe_A:
  971. case Js::OpCode::BrNotLt_A:
  972. return Js::OpCode::JAE;
  973. default:
  974. AssertMsg(0, "Branch opcode has no MD mapping");
  975. return opcode;
  976. }
  977. }
  978. Js::OpCode LowererMD::MDCompareWithZeroBranchOpcode(Js::OpCode opcode)
  979. {
  980. Assert(opcode == Js::OpCode::BrLt_A || opcode == Js::OpCode::BrGe_A);
  981. return opcode == Js::OpCode::BrLt_A ? Js::OpCode::JSB : Js::OpCode::JNSB;
  982. }
  983. void LowererMD::ChangeToAdd(IR::Instr *const instr, const bool needFlags)
  984. {
  985. Assert(instr);
  986. Assert(instr->GetDst());
  987. Assert(instr->GetSrc1());
  988. Assert(instr->GetSrc2());
  989. if(instr->GetDst()->IsFloat64())
  990. {
  991. Assert(instr->GetSrc1()->IsFloat64());
  992. Assert(instr->GetSrc2()->IsFloat64());
  993. Assert(!needFlags);
  994. instr->m_opcode = Js::OpCode::ADDSD;
  995. return;
  996. }
  997. else if (instr->GetDst()->IsFloat32())
  998. {
  999. Assert(instr->GetSrc1()->IsFloat32());
  1000. Assert(instr->GetSrc2()->IsFloat32());
  1001. Assert(!needFlags);
  1002. instr->m_opcode = Js::OpCode::ADDSS;
  1003. return;
  1004. }
  1005. instr->m_opcode = Js::OpCode::ADD;
  1006. MakeDstEquSrc1(instr);
  1007. // Prefer INC for add by one
  1008. if((instr->GetDst()->IsEqual(instr->GetSrc1()) &&
  1009. instr->GetSrc2()->IsIntConstOpnd() &&
  1010. instr->GetSrc2()->AsIntConstOpnd()->GetValue() == 1) ||
  1011. (instr->GetDst()->IsEqual(instr->GetSrc2()) &&
  1012. instr->GetSrc1()->IsIntConstOpnd() &&
  1013. instr->GetSrc1()->AsIntConstOpnd()->GetValue() == 1))
  1014. {
  1015. if(instr->GetSrc1()->IsIntConstOpnd())
  1016. {
  1017. // Swap the operands, such that we would create (dst = INC src2)
  1018. instr->SwapOpnds();
  1019. }
  1020. instr->FreeSrc2();
  1021. instr->m_opcode = Js::OpCode::INC;
  1022. }
  1023. }
  1024. void LowererMD::ChangeToSub(IR::Instr *const instr, const bool needFlags)
  1025. {
  1026. Assert(instr);
  1027. Assert(instr->GetDst());
  1028. Assert(instr->GetSrc1());
  1029. Assert(instr->GetSrc2());
  1030. if(instr->GetDst()->IsFloat64())
  1031. {
  1032. Assert(instr->GetSrc1()->IsFloat64());
  1033. Assert(instr->GetSrc2()->IsFloat64());
  1034. Assert(!needFlags);
  1035. instr->m_opcode = Js::OpCode::SUBSD;
  1036. return;
  1037. }
  1038. // Prefer DEC for sub by one
  1039. if(instr->GetDst()->IsEqual(instr->GetSrc1()) &&
  1040. instr->GetSrc2()->IsIntConstOpnd() &&
  1041. instr->GetSrc2()->AsIntConstOpnd()->GetValue() == 1)
  1042. {
  1043. instr->FreeSrc2();
  1044. instr->m_opcode = Js::OpCode::DEC;
  1045. return;
  1046. }
  1047. instr->m_opcode = Js::OpCode::SUB;
  1048. }
  1049. void LowererMD::ChangeToShift(IR::Instr *const instr, const bool needFlags)
  1050. {
  1051. Assert(instr);
  1052. Assert(instr->GetDst());
  1053. Assert(instr->GetSrc1());
  1054. Assert(instr->GetSrc2());
  1055. switch(instr->m_opcode)
  1056. {
  1057. case Js::OpCode::Shl_A:
  1058. case Js::OpCode::Shl_I4:
  1059. instr->m_opcode = Js::OpCode::SHL;
  1060. break;
  1061. case Js::OpCode::Shr_A:
  1062. case Js::OpCode::Shr_I4:
  1063. instr->m_opcode = Js::OpCode::SAR;
  1064. break;
  1065. case Js::OpCode::ShrU_A:
  1066. case Js::OpCode::ShrU_I4:
  1067. instr->m_opcode = Js::OpCode::SHR;
  1068. break;
  1069. case Js::OpCode::Rol_I4:
  1070. instr->m_opcode = Js::OpCode::ROL;
  1071. break;
  1072. case Js::OpCode::Ror_I4:
  1073. instr->m_opcode = Js::OpCode::ROR;
  1074. break;
  1075. default:
  1076. Assert(false);
  1077. __assume(false);
  1078. }
  1079. if(instr->GetSrc2()->IsIntConstOpnd())
  1080. {
  1081. // Only values between 0-31 mean anything
  1082. IntConstType value = instr->GetSrc2()->AsIntConstOpnd()->GetValue();
  1083. value &= 0x1f;
  1084. instr->GetSrc2()->AsIntConstOpnd()->SetValue(value);
  1085. }
  1086. }
  1087. void LowererMD::ChangeToIMul(IR::Instr *const instr, bool hasOverflowCheck)
  1088. {
  1089. // If non-32 bit overflow check is needed, we have to use the IMUL form.
  1090. if (hasOverflowCheck && !instr->ShouldCheckFor32BitOverflow() && instr->ShouldCheckForNon32BitOverflow())
  1091. {
  1092. IR::RegOpnd *regEAX = IR::RegOpnd::New(TyInt32, instr->m_func);
  1093. IR::Opnd *temp2 = nullptr;
  1094. // MOV eax, src1
  1095. regEAX->SetReg(LowererMDArch::GetRegIMulDestLower());
  1096. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, regEAX, instr->GetSrc1(), instr->m_func));
  1097. if (instr->GetSrc2()->IsImmediateOpnd())
  1098. {
  1099. // MOV reg, imm
  1100. temp2 = IR::RegOpnd::New(TyInt32, instr->m_func);
  1101. IR::Opnd * src2 = instr->GetSrc2();
  1102. bool dontEncode = false;
  1103. if (src2->IsHelperCallOpnd())
  1104. {
  1105. dontEncode = true;
  1106. }
  1107. else if (src2->IsIntConstOpnd() || src2->IsAddrOpnd())
  1108. {
  1109. dontEncode = src2->IsIntConstOpnd() ? src2->AsIntConstOpnd()->m_dontEncode : src2->AsAddrOpnd()->m_dontEncode;
  1110. }
  1111. else if (src2->IsInt64ConstOpnd())
  1112. {
  1113. dontEncode = false;
  1114. }
  1115. else
  1116. {
  1117. AssertMsg(false, "Unexpected immediate opnd");
  1118. throw Js::OperationAbortedException();
  1119. }
  1120. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, temp2,
  1121. IR::IntConstOpnd::New((IntConstType)instr->GetSrc2()->GetImmediateValue(instr->m_func), TyInt32, instr->m_func, dontEncode),
  1122. instr->m_func));
  1123. }
  1124. // eax = IMUL eax, reg
  1125. instr->m_opcode = Js::OpCode::IMUL;
  1126. instr->ReplaceSrc1(regEAX);
  1127. if (temp2 != nullptr)
  1128. instr->ReplaceSrc2(temp2);
  1129. auto *dst = instr->GetDst()->Copy(instr->m_func);
  1130. instr->ReplaceDst(regEAX);
  1131. // MOV dst, eax
  1132. instr->InsertAfter(IR::Instr::New(Js::OpCode::MOV, dst, regEAX, instr->m_func));
  1133. }
  1134. else
  1135. EmitInt4Instr(instr); // IMUL2
  1136. }
  1137. const uint16
  1138. LowererMD::GetFormalParamOffset()
  1139. {
  1140. //In x86\x64 formal params were offset from EBP by the EBP chain, return address, and the 2 non-user params
  1141. return 4;
  1142. }
  1143. IR::Instr *
  1144. LowererMD::LowerCatch(IR::Instr * instr)
  1145. {
  1146. // t1 = catch => t2(eax) = catch
  1147. // => t1 = t2(eax)
  1148. IR::Opnd *catchObj = instr->UnlinkDst();
  1149. IR::RegOpnd *catchParamReg = IR::RegOpnd::New(TyMachPtr, this->m_func);
  1150. catchParamReg->SetReg(this->lowererMDArch.GetRegReturn(TyMachReg));
  1151. instr->SetDst(catchParamReg);
  1152. instr->InsertAfter(IR::Instr::New(Js::OpCode::MOV, catchObj, catchParamReg, this->m_func));
  1153. return instr->m_prev;
  1154. }
  1155. ///----------------------------------------------------------------------------
  1156. ///
  1157. /// LowererMD::ForceDstToReg
  1158. ///
  1159. ///----------------------------------------------------------------------------
  1160. void
  1161. LowererMD::ForceDstToReg(IR::Instr *instr)
  1162. {
  1163. IR::Opnd * dst = instr->GetDst();
  1164. if (dst->IsRegOpnd())
  1165. {
  1166. return;
  1167. }
  1168. if(dst->IsFloat64())
  1169. {
  1170. instr->SinkDst(Js::OpCode::MOVSD);
  1171. return;
  1172. }
  1173. instr->SinkDst(Js::OpCode::MOV);
  1174. }
  1175. template <bool verify>
  1176. void
  1177. LowererMD::Legalize(IR::Instr *const instr, bool fPostRegAlloc)
  1178. {
  1179. Assert(instr);
  1180. Assert(!instr->isInlineeEntryInstr
  1181. || (instr->m_opcode == Js::OpCode::MOV && instr->GetSrc1()->IsIntConstOpnd()));
  1182. switch(instr->m_opcode)
  1183. {
  1184. case Js::OpCode::MOV:
  1185. {
  1186. Assert(instr->GetSrc2() == nullptr);
  1187. IR::Opnd *const dst = instr->GetDst();
  1188. const IRType dstType = dst->GetType();
  1189. IR::Opnd *const src = instr->GetSrc1();
  1190. const IRType srcType = src->GetType();
  1191. if(TySize[dstType] > TySize[srcType])
  1192. {
  1193. if (verify)
  1194. {
  1195. return;
  1196. }
  1197. #if DBG
  1198. switch(dstType)
  1199. {
  1200. case TyInt32:
  1201. case TyUint32:
  1202. #ifdef _M_X64
  1203. case TyInt64:
  1204. case TyUint64:
  1205. #endif
  1206. case TyVar:
  1207. break;
  1208. default:
  1209. Assert(false);
  1210. }
  1211. #endif
  1212. IR::IntConstOpnd *const intConstantSrc = src->IsIntConstOpnd() ? src->AsIntConstOpnd() : nullptr;
  1213. const auto UpdateIntConstantSrc = [&](const size_t extendedValue)
  1214. {
  1215. Assert(intConstantSrc);
  1216. #ifdef _M_X64
  1217. if(TySize[dstType] > sizeof(IntConstType))
  1218. {
  1219. instr->ReplaceSrc1(
  1220. IR::AddrOpnd::New(
  1221. reinterpret_cast<void *>(extendedValue),
  1222. IR::AddrOpndKindConstantVar,
  1223. instr->m_func,
  1224. intConstantSrc->m_dontEncode));
  1225. }
  1226. else
  1227. #endif
  1228. {
  1229. intConstantSrc->SetType(dstType);
  1230. intConstantSrc->SetValue(static_cast<IntConstType>(extendedValue));
  1231. }
  1232. };
  1233. switch(srcType)
  1234. {
  1235. case TyInt8:
  1236. if(intConstantSrc)
  1237. {
  1238. UpdateIntConstantSrc(static_cast<int8>(intConstantSrc->GetValue())); // sign-extend
  1239. break;
  1240. }
  1241. instr->m_opcode = Js::OpCode::MOVSX;
  1242. break;
  1243. case TyUint8:
  1244. if(intConstantSrc)
  1245. {
  1246. UpdateIntConstantSrc(static_cast<uint8>(intConstantSrc->GetValue())); // zero-extend
  1247. break;
  1248. }
  1249. instr->m_opcode = Js::OpCode::MOVZX;
  1250. break;
  1251. case TyInt16:
  1252. if(intConstantSrc)
  1253. {
  1254. UpdateIntConstantSrc(static_cast<int16>(intConstantSrc->GetValue())); // sign-extend
  1255. break;
  1256. }
  1257. instr->m_opcode = Js::OpCode::MOVSXW;
  1258. break;
  1259. case TyUint16:
  1260. if(intConstantSrc)
  1261. {
  1262. UpdateIntConstantSrc(static_cast<uint16>(intConstantSrc->GetValue())); // zero-extend
  1263. break;
  1264. }
  1265. instr->m_opcode = Js::OpCode::MOVZXW;
  1266. break;
  1267. #ifdef _M_X64
  1268. case TyInt32:
  1269. if(intConstantSrc)
  1270. {
  1271. UpdateIntConstantSrc(static_cast<int32>(intConstantSrc->GetValue())); // sign-extend
  1272. break;
  1273. }
  1274. instr->m_opcode = Js::OpCode::MOVSXD;
  1275. break;
  1276. case TyUint32:
  1277. if(intConstantSrc)
  1278. {
  1279. UpdateIntConstantSrc(static_cast<uint32>(intConstantSrc->GetValue())); // zero-extend
  1280. break;
  1281. }
  1282. switch(dst->GetKind())
  1283. {
  1284. case IR::OpndKindReg:
  1285. // (mov r0.u32, r1.u32) clears the upper 32 bits of r0
  1286. dst->SetType(TyUint32);
  1287. instr->m_opcode = Js::OpCode::MOV_TRUNC;
  1288. break;
  1289. case IR::OpndKindSym:
  1290. case IR::OpndKindIndir:
  1291. case IR::OpndKindMemRef:
  1292. // Even if the src is a reg, we don't know if the upper 32 bits are zero. Copy the value to a
  1293. // reg first to zero-extend it to 64 bits, and then copy the 64-bit value to the original dst.
  1294. instr->HoistSrc1(Js::OpCode::MOV_TRUNC);
  1295. instr->GetSrc1()->SetType(dstType);
  1296. break;
  1297. default:
  1298. Assert(false);
  1299. __assume(false);
  1300. }
  1301. break;
  1302. #endif
  1303. default:
  1304. Assert(false);
  1305. __assume(false);
  1306. }
  1307. }
  1308. else if (TySize[dstType] < TySize[srcType])
  1309. {
  1310. instr->GetSrc1()->SetType(dst->GetType());
  1311. }
  1312. if(instr->m_opcode == Js::OpCode::MOV)
  1313. {
  1314. uint src1Forms = L_Reg | L_Mem | L_Ptr; // Allow 64 bit values in x64 as well
  1315. if (dst->IsMemoryOpnd())
  1316. {
  1317. #if _M_X64
  1318. // Only allow <= 32 bit values
  1319. src1Forms = L_Reg | L_Imm32;
  1320. #else
  1321. src1Forms = L_Reg | L_Ptr;
  1322. #endif
  1323. }
  1324. LegalizeOpnds<verify>(
  1325. instr,
  1326. L_Reg | L_Mem,
  1327. src1Forms,
  1328. L_None);
  1329. }
  1330. else
  1331. {
  1332. LegalizeOpnds<verify>(
  1333. instr,
  1334. L_Reg,
  1335. L_Reg | L_Mem,
  1336. L_None);
  1337. }
  1338. break;
  1339. }
  1340. case Js::OpCode::CMOVA:
  1341. case Js::OpCode::CMOVAE:
  1342. case Js::OpCode::CMOVB:
  1343. case Js::OpCode::CMOVBE:
  1344. case Js::OpCode::CMOVE:
  1345. case Js::OpCode::CMOVG:
  1346. case Js::OpCode::CMOVGE:
  1347. case Js::OpCode::CMOVL:
  1348. case Js::OpCode::CMOVLE:
  1349. case Js::OpCode::CMOVNE:
  1350. case Js::OpCode::CMOVNO:
  1351. case Js::OpCode::CMOVNP:
  1352. case Js::OpCode::CMOVNS:
  1353. case Js::OpCode::CMOVO:
  1354. case Js::OpCode::CMOVP:
  1355. case Js::OpCode::CMOVS:
  1356. if (instr->GetSrc2())
  1357. {
  1358. Assert(instr->GetDst()->GetSize() == instr->GetSrc2()->GetSize());
  1359. Assert(instr->GetDst()->GetSize() == instr->GetSrc1()->GetSize());
  1360. // 0 shouldn't be the src2 of a CMOVcc.
  1361. // CMOVcc doesn't support moving a constant and the legalizer will hoist the load of the constant
  1362. // to a register. If the constant was 0, Peeps will turn it into a XOR which, in turn, may change
  1363. // the zero flags and hence the result of CMOVcc. If you do want to CMOVcc 0, you should load 0
  1364. // into a register before the instruction whose result the CMOVcc depends on.
  1365. Assert(!instr->GetSrc2()->IsIntConstOpnd() || instr->GetSrc2()->AsIntConstOpnd()->GetValue() != 0);
  1366. // sometimes we have fake src1 to help reg alloc
  1367. LegalizeOpnds<verify>(
  1368. instr,
  1369. L_Reg,
  1370. L_Reg,
  1371. L_Reg | L_Mem);
  1372. }
  1373. else
  1374. {
  1375. Assert(instr->GetDst()->GetSize() == instr->GetSrc1()->GetSize());
  1376. LegalizeOpnds<verify>(
  1377. instr,
  1378. L_Reg,
  1379. L_Reg | L_Mem,
  1380. L_None);
  1381. }
  1382. break;
  1383. case Js::OpCode::MOVSD:
  1384. Assert(AutoSystemInfo::Data.SSE2Available());
  1385. case Js::OpCode::MOVSS:
  1386. {
  1387. Assert(instr->GetDst()->GetType() == (instr->m_opcode == Js::OpCode::MOVSD? TyFloat64 : TyFloat32) || instr->GetDst()->IsSimd128());
  1388. Assert(instr->GetSrc1()->GetType() == (instr->m_opcode == Js::OpCode::MOVSD ? TyFloat64 : TyFloat32) || instr->GetSrc1()->IsSimd128());
  1389. LegalizeOpnds<verify>(
  1390. instr,
  1391. L_Reg | L_Mem,
  1392. instr->GetDst()->IsMemoryOpnd()?
  1393. L_Reg : L_Reg | L_Mem, // LegalizeOpnds doesn't check if dst/src1 are both memopnd, check it here.
  1394. L_None);
  1395. break;
  1396. }
  1397. case Js::OpCode::MOVUPS:
  1398. case Js::OpCode::MOVAPS:
  1399. {
  1400. LegalizeOpnds<verify>(
  1401. instr,
  1402. L_Reg | L_Mem,
  1403. instr->GetDst()->IsMemoryOpnd()?
  1404. L_Reg : L_Reg | L_Mem, // LegalizeOpnds doesn't check if dst/src1 are both memopnd, check it here.
  1405. L_None);
  1406. break;
  1407. }
  1408. case Js::OpCode::CMP:
  1409. LegalizeOpnds<verify>(
  1410. instr,
  1411. L_None,
  1412. L_Reg | L_Mem,
  1413. L_Reg | L_Mem | L_Imm32);
  1414. break;
  1415. case Js::OpCode::TEST:
  1416. if((instr->GetSrc1()->IsImmediateOpnd() && !instr->GetSrc2()->IsImmediateOpnd()) ||
  1417. (instr->GetSrc2()->IsMemoryOpnd() && !instr->GetSrc1()->IsMemoryOpnd()))
  1418. {
  1419. if (verify)
  1420. {
  1421. AssertMsg(false, "Missing legalization");
  1422. return;
  1423. }
  1424. instr->SwapOpnds();
  1425. }
  1426. LegalizeOpnds<verify>(
  1427. instr,
  1428. L_None,
  1429. L_Reg | L_Mem,
  1430. L_Reg | L_Imm32);
  1431. break;
  1432. case Js::OpCode::COMISD:
  1433. case Js::OpCode::UCOMISD:
  1434. Assert(AutoSystemInfo::Data.SSE2Available());
  1435. case Js::OpCode::COMISS:
  1436. case Js::OpCode::UCOMISS:
  1437. LegalizeOpnds<verify>(
  1438. instr,
  1439. L_None,
  1440. L_Reg,
  1441. L_Reg | L_Mem);
  1442. break;
  1443. case Js::OpCode::INC:
  1444. case Js::OpCode::DEC:
  1445. case Js::OpCode::NEG:
  1446. MakeDstEquSrc1<verify>(instr);
  1447. LegalizeOpnds<verify>(
  1448. instr,
  1449. L_Reg | L_Mem,
  1450. L_Reg | L_Mem,
  1451. L_None);
  1452. break;
  1453. #ifdef _M_IX86
  1454. case Js::OpCode::ADC:
  1455. #endif
  1456. case Js::OpCode::ADD:
  1457. case Js::OpCode::SUB:
  1458. case Js::OpCode::SBB:
  1459. case Js::OpCode::AND:
  1460. case Js::OpCode::OR:
  1461. case Js::OpCode::XOR:
  1462. MakeDstEquSrc1<verify>(instr);
  1463. LegalizeOpnds<verify>(
  1464. instr,
  1465. L_Reg | L_Mem,
  1466. L_Reg | L_Mem,
  1467. L_Reg | L_Mem | L_Imm32);
  1468. break;
  1469. case Js::OpCode::ADDSD:
  1470. case Js::OpCode::ADDPD:
  1471. case Js::OpCode::SUBSD:
  1472. case Js::OpCode::ANDPD:
  1473. case Js::OpCode::ANDNPD:
  1474. case Js::OpCode::DIVPD:
  1475. case Js::OpCode::MAXPD:
  1476. case Js::OpCode::MINPD:
  1477. case Js::OpCode::MULPD:
  1478. case Js::OpCode::SUBPD:
  1479. Assert(AutoSystemInfo::Data.SSE2Available());
  1480. case Js::OpCode::ADDPS:
  1481. case Js::OpCode::ADDSS:
  1482. case Js::OpCode::SUBSS:
  1483. case Js::OpCode::ANDPS:
  1484. case Js::OpCode::ANDNPS:
  1485. case Js::OpCode::DIVPS:
  1486. case Js::OpCode::MAXPS:
  1487. case Js::OpCode::MINPS:
  1488. case Js::OpCode::MULPS:
  1489. case Js::OpCode::ORPS:
  1490. case Js::OpCode::PADDB:
  1491. case Js::OpCode::PADDSB:
  1492. case Js::OpCode::PADDD:
  1493. case Js::OpCode::PADDW:
  1494. case Js::OpCode::PADDSW:
  1495. case Js::OpCode::PADDUSB:
  1496. case Js::OpCode::PADDUSW:
  1497. case Js::OpCode::PAND:
  1498. case Js::OpCode::PANDN:
  1499. case Js::OpCode::PCMPEQB:
  1500. case Js::OpCode::PCMPEQD:
  1501. case Js::OpCode::PCMPEQW:
  1502. case Js::OpCode::PCMPGTB:
  1503. case Js::OpCode::PCMPGTW:
  1504. case Js::OpCode::PCMPGTD:
  1505. case Js::OpCode::PMAXSW:
  1506. case Js::OpCode::PMAXUB:
  1507. case Js::OpCode::PMINSW:
  1508. case Js::OpCode::PMINUB:
  1509. case Js::OpCode::PMULLW:
  1510. case Js::OpCode::PMULUDQ:
  1511. case Js::OpCode::POR:
  1512. case Js::OpCode::PSUBB:
  1513. case Js::OpCode::PSUBSB:
  1514. case Js::OpCode::PSUBD:
  1515. case Js::OpCode::PSUBW:
  1516. case Js::OpCode::PSUBSW:
  1517. case Js::OpCode::PSUBUSB:
  1518. case Js::OpCode::PSUBUSW:
  1519. case Js::OpCode::PXOR:
  1520. case Js::OpCode::SUBPS:
  1521. case Js::OpCode::XORPS:
  1522. case Js::OpCode::CMPLTPS:
  1523. case Js::OpCode::CMPLEPS:
  1524. case Js::OpCode::CMPEQPS:
  1525. case Js::OpCode::CMPNEQPS:
  1526. case Js::OpCode::CMPLTPD:
  1527. case Js::OpCode::CMPLEPD:
  1528. case Js::OpCode::CMPEQPD:
  1529. case Js::OpCode::CMPNEQPD:
  1530. case Js::OpCode::CMPUNORDPS:
  1531. case Js::OpCode::PUNPCKLBW:
  1532. case Js::OpCode::PUNPCKLDQ:
  1533. case Js::OpCode::PUNPCKLWD:
  1534. MakeDstEquSrc1<verify>(instr);
  1535. LegalizeOpnds<verify>(
  1536. instr,
  1537. L_Reg,
  1538. L_Reg,
  1539. L_Reg | L_Mem);
  1540. break;
  1541. case Js::OpCode::SHL:
  1542. case Js::OpCode::SHR:
  1543. case Js::OpCode::SAR:
  1544. case Js::OpCode::ROL:
  1545. case Js::OpCode::ROR:
  1546. if (verify)
  1547. {
  1548. Assert(instr->GetSrc2()->IsIntConstOpnd()
  1549. || instr->GetSrc2()->AsRegOpnd()->GetReg() == LowererMDArch::GetRegShiftCount());
  1550. }
  1551. else
  1552. {
  1553. if(!instr->GetSrc2()->IsIntConstOpnd())
  1554. {
  1555. IR::Instr *const newInstr = instr->HoistSrc2(Js::OpCode::MOV);
  1556. newInstr->GetDst()->AsRegOpnd()->SetReg(LowererMDArch::GetRegShiftCount());
  1557. instr->GetSrc2()->AsRegOpnd()->SetReg(LowererMDArch::GetRegShiftCount());
  1558. }
  1559. instr->GetSrc2()->SetType(TyUint8);
  1560. }
  1561. MakeDstEquSrc1<verify>(instr);
  1562. LegalizeOpnds<verify>(
  1563. instr,
  1564. L_Reg | L_Mem,
  1565. L_Reg | L_Mem,
  1566. L_Reg | L_Imm32);
  1567. break;
  1568. case Js::OpCode::IMUL2:
  1569. MakeDstEquSrc1<verify>(instr); // the encoder does not support IMUL3 r, r/m, imm
  1570. LegalizeOpnds<verify>(
  1571. instr,
  1572. L_Reg,
  1573. L_Reg,
  1574. L_Reg | L_Mem | L_Imm32); // for L_Imm32, the encoder converts it into an IMUL3
  1575. break;
  1576. case Js::OpCode::TZCNT:
  1577. case Js::OpCode::LZCNT:
  1578. Assert(
  1579. (instr->m_opcode == Js::OpCode::LZCNT && AutoSystemInfo::Data.LZCntAvailable()) ||
  1580. (instr->m_opcode == Js::OpCode::TZCNT && AutoSystemInfo::Data.TZCntAvailable())
  1581. );
  1582. case Js::OpCode::BSF:
  1583. case Js::OpCode::BSR:
  1584. LegalizeOpnds<verify>(
  1585. instr,
  1586. L_Reg,
  1587. L_Reg | L_Mem,
  1588. L_None);
  1589. break;
  1590. case Js::OpCode::LEA:
  1591. Assert(instr->GetDst()->IsRegOpnd());
  1592. Assert(instr->GetSrc1()->IsIndirOpnd() || instr->GetSrc1()->IsSymOpnd()
  1593. || instr->GetSrc1()->IsMemRefOpnd()); // We may convert IndirOpnd to MemRefOpnd
  1594. Assert(!instr->GetSrc2());
  1595. break;
  1596. case Js::OpCode::PSRLDQ:
  1597. case Js::OpCode::PSLLDQ:
  1598. case Js::OpCode::PSRLW:
  1599. case Js::OpCode::PSRLD:
  1600. case Js::OpCode::PSRAW:
  1601. case Js::OpCode::PSRAD:
  1602. case Js::OpCode::PSLLW:
  1603. case Js::OpCode::PSLLD:
  1604. Assert(AutoSystemInfo::Data.SSE2Available());
  1605. MakeDstEquSrc1<verify>(instr);
  1606. LegalizeOpnds<verify>(
  1607. instr,
  1608. L_Reg,
  1609. L_Reg,
  1610. L_Reg | L_Imm32);
  1611. break;
  1612. case Js::OpCode::ROUNDSD:
  1613. case Js::OpCode::ROUNDSS:
  1614. Assert(AutoSystemInfo::Data.SSE4_1Available());
  1615. break;
  1616. case Js::OpCode::CVTDQ2PD:
  1617. case Js::OpCode::CVTDQ2PS:
  1618. case Js::OpCode::CVTPD2PS:
  1619. case Js::OpCode::CVTPS2PD:
  1620. case Js::OpCode::CVTSD2SI:
  1621. case Js::OpCode::CVTSD2SS:
  1622. case Js::OpCode::CVTSI2SD:
  1623. case Js::OpCode::CVTSS2SD:
  1624. case Js::OpCode::CVTTPD2DQ:
  1625. case Js::OpCode::CVTTPS2DQ:
  1626. case Js::OpCode::CVTTSD2SI:
  1627. case Js::OpCode::DIVSD:
  1628. case Js::OpCode::SQRTPD:
  1629. case Js::OpCode::SQRTSD:
  1630. case Js::OpCode::SHUFPD:
  1631. Assert(AutoSystemInfo::Data.SSE2Available());
  1632. break;
  1633. }
  1634. #if DBG
  1635. // Asserting general rules
  1636. // There should be at most 1 memory opnd in an instruction
  1637. if (instr->GetDst() && instr->GetDst()->IsMemoryOpnd())
  1638. {
  1639. // All memref address need to fit in a dword
  1640. Assert(!instr->GetDst()->IsMemRefOpnd() || Math::FitsInDWord((size_t)instr->GetDst()->AsMemRefOpnd()->GetMemLoc()));
  1641. if (instr->GetSrc1())
  1642. {
  1643. Assert(instr->GetSrc1()->IsEqual(instr->GetDst()) || !instr->GetSrc1()->IsMemoryOpnd());
  1644. if (instr->GetSrc2())
  1645. {
  1646. Assert(!instr->GetSrc2()->IsMemoryOpnd());
  1647. }
  1648. }
  1649. }
  1650. else if (instr->GetSrc1() && instr->GetSrc1()->IsMemoryOpnd())
  1651. {
  1652. // All memref address need to fit in a dword
  1653. Assert(!instr->GetSrc1()->IsMemRefOpnd() || Math::FitsInDWord((size_t)instr->GetSrc1()->AsMemRefOpnd()->GetMemLoc()));
  1654. Assert(!instr->GetSrc2() || !instr->GetSrc2()->IsMemoryOpnd());
  1655. }
  1656. else if (instr->GetSrc2() && instr->GetSrc2()->IsMemRefOpnd())
  1657. {
  1658. // All memref address need to fit in a dword
  1659. Assert(Math::FitsInDWord((size_t)instr->GetSrc2()->AsMemRefOpnd()->GetMemLoc()));
  1660. }
  1661. // Non-MOV (second operand) immediate need to fit in DWORD for AMD64
  1662. Assert(!instr->GetSrc2() || !instr->GetSrc2()->IsImmediateOpnd()
  1663. || (TySize[instr->GetSrc2()->GetType()] != 8) || Math::FitsInDWord(instr->GetSrc2()->GetImmediateValue(instr->m_func)));
  1664. #endif
  1665. }
  1666. template <bool verify>
  1667. void LowererMD::LegalizeOpnds(IR::Instr *const instr, const uint dstForms, const uint src1Forms, uint src2Forms)
  1668. {
  1669. Assert(instr);
  1670. Assert(!instr->GetDst() == !dstForms);
  1671. Assert(!instr->GetSrc1() == !src1Forms);
  1672. Assert(!instr->GetSrc2() == !src2Forms);
  1673. Assert(src1Forms || !src2Forms);
  1674. const auto NormalizeForms = [](uint forms) -> uint
  1675. {
  1676. #ifdef _M_X64
  1677. if(forms & L_Ptr)
  1678. {
  1679. forms |= L_Imm32;
  1680. }
  1681. #else
  1682. if(forms & (L_Imm32 | L_Ptr))
  1683. {
  1684. forms |= L_Imm32 | L_Ptr;
  1685. }
  1686. #endif
  1687. return forms;
  1688. };
  1689. if(dstForms)
  1690. {
  1691. LegalizeDst<verify>(instr, NormalizeForms(dstForms));
  1692. }
  1693. if(!src1Forms)
  1694. {
  1695. return;
  1696. }
  1697. LegalizeSrc<verify>(instr, instr->GetSrc1(), NormalizeForms(src1Forms));
  1698. if(src2Forms & L_Mem && instr->GetSrc1()->IsMemoryOpnd())
  1699. {
  1700. src2Forms ^= L_Mem;
  1701. }
  1702. if(src2Forms)
  1703. {
  1704. LegalizeSrc<verify>(instr, instr->GetSrc2(), NormalizeForms(src2Forms));
  1705. }
  1706. }
  1707. template <bool verify>
  1708. void LowererMD::LegalizeDst(IR::Instr *const instr, const uint forms)
  1709. {
  1710. Assert(instr);
  1711. Assert(forms);
  1712. IR::Opnd *dst = instr->GetDst();
  1713. Assert(dst);
  1714. #ifndef _M_X64
  1715. AssertMsg(!dst->IsInt64(), "Int64 supported only on x64");
  1716. #endif
  1717. switch(dst->GetKind())
  1718. {
  1719. case IR::OpndKindReg:
  1720. Assert(forms & L_Reg);
  1721. return;
  1722. case IR::OpndKindMemRef:
  1723. {
  1724. IR::MemRefOpnd *const memRefOpnd = dst->AsMemRefOpnd();
  1725. if(!LowererMDArch::IsLegalMemLoc(memRefOpnd))
  1726. {
  1727. if (verify)
  1728. {
  1729. AssertMsg(false, "Missing legalization");
  1730. return;
  1731. }
  1732. dst = instr->HoistMemRefAddress(memRefOpnd, Js::OpCode::MOV);
  1733. }
  1734. // fall through
  1735. }
  1736. case IR::OpndKindSym:
  1737. case IR::OpndKindIndir:
  1738. if(forms & L_Mem)
  1739. {
  1740. return;
  1741. }
  1742. break;
  1743. default:
  1744. Assert(false);
  1745. __assume(false);
  1746. }
  1747. if (verify)
  1748. {
  1749. AssertMsg(false, "Missing legalization");
  1750. return;
  1751. }
  1752. // Use a reg dst, then store that reg into the original dst
  1753. Assert(forms & L_Reg);
  1754. const IRType irType = dst->GetType();
  1755. IR::RegOpnd *const regOpnd = IR::RegOpnd::New(irType, instr->m_func);
  1756. regOpnd->SetValueType(dst->GetValueType());
  1757. instr->UnlinkDst();
  1758. instr->SetDst(regOpnd);
  1759. instr->InsertAfter(IR::Instr::New(GetStoreOp(irType), dst, regOpnd, instr->m_func));
  1760. // If the original dst is the same as one of the srcs, hoist a src into the same reg and replace the same srcs with the reg
  1761. const bool equalsSrc1 = instr->GetSrc1() && dst->IsEqual(instr->GetSrc1());
  1762. const bool equalsSrc2 = instr->GetSrc2() && dst->IsEqual(instr->GetSrc2());
  1763. if(!(equalsSrc1 || equalsSrc2))
  1764. {
  1765. return;
  1766. }
  1767. const Js::OpCode loadOpCode = GetLoadOp(irType);
  1768. if(equalsSrc1)
  1769. {
  1770. instr->HoistSrc1(loadOpCode, RegNOREG, regOpnd->m_sym);
  1771. if(equalsSrc2)
  1772. {
  1773. instr->ReplaceSrc2(regOpnd);
  1774. }
  1775. }
  1776. else
  1777. {
  1778. instr->HoistSrc2(loadOpCode, RegNOREG, regOpnd->m_sym);
  1779. }
  1780. }
  1781. bool LowererMD::HoistLargeConstant(IR::IndirOpnd *indirOpnd, IR::Opnd *src, IR::Instr *instr) {
  1782. if (indirOpnd != nullptr)
  1783. {
  1784. if (indirOpnd->GetOffset() == 0)
  1785. {
  1786. instr->ReplaceSrc(src, indirOpnd->GetBaseOpnd());
  1787. }
  1788. else
  1789. {
  1790. // Hoist the address load as LEA [reg + offset]
  1791. // with the reg = MOV <some address within 32-bit range at the start of the function
  1792. IR::RegOpnd * regOpnd = IR::RegOpnd::New(TyMachPtr, instr->m_func);
  1793. Lowerer::InsertLea(regOpnd, indirOpnd, instr);
  1794. instr->ReplaceSrc(src, regOpnd);
  1795. }
  1796. return true;
  1797. }
  1798. return false;
  1799. }
  1800. template <bool verify>
  1801. void LowererMD::LegalizeSrc(IR::Instr *const instr, IR::Opnd *src, const uint forms)
  1802. {
  1803. Assert(instr);
  1804. Assert(src);
  1805. Assert(src == instr->GetSrc1() || src == instr->GetSrc2());
  1806. Assert(forms);
  1807. #ifndef _M_X64
  1808. AssertMsg(!src->IsInt64(), "Int64 supported only on x64");
  1809. #endif
  1810. switch(src->GetKind())
  1811. {
  1812. case IR::OpndKindReg:
  1813. Assert(forms & L_Reg);
  1814. return;
  1815. case IR::OpndKindIntConst:
  1816. if(forms & L_Ptr)
  1817. {
  1818. return;
  1819. }
  1820. #ifdef _M_X64
  1821. {
  1822. IR::IntConstOpnd * intOpnd = src->AsIntConstOpnd();
  1823. if ((TySize[intOpnd->GetType()] != 8) ||
  1824. (!instr->isInlineeEntryInstr && Math::FitsInDWord(intOpnd->GetValue())))
  1825. {
  1826. if (forms & L_Imm32)
  1827. {
  1828. // the constant fits in 32-bit, no need to hoist
  1829. return;
  1830. }
  1831. break;
  1832. }
  1833. if (verify)
  1834. {
  1835. AssertMsg(false, "Missing legalization");
  1836. return;
  1837. }
  1838. // The actual value for inlinee entry instr isn't determined until encoder
  1839. // So it need to be hoisted conventionally.
  1840. if (!instr->isInlineeEntryInstr)
  1841. {
  1842. Assert(forms & L_Reg);
  1843. IR::IntConstOpnd * newIntOpnd = intOpnd->Copy(instr->m_func)->AsIntConstOpnd();
  1844. IR::IndirOpnd * indirOpnd = instr->m_func->GetTopFunc()->GetConstantAddressIndirOpnd(intOpnd->GetValue(), newIntOpnd, IR::AddrOpndKindConstantAddress, TyMachPtr, Js::OpCode::MOV);
  1845. if (HoistLargeConstant(indirOpnd, src, instr))
  1846. {
  1847. return;
  1848. }
  1849. }
  1850. }
  1851. #endif
  1852. break;
  1853. case IR::OpndKindFloatConst:
  1854. break; // assume for now that it always needs to be hoisted
  1855. case IR::OpndKindInt64Const:
  1856. if (forms & L_Ptr)
  1857. {
  1858. return;
  1859. }
  1860. #ifdef _M_X64
  1861. {
  1862. IR::Int64ConstOpnd * int64Opnd = src->AsInt64ConstOpnd();
  1863. if ((forms & L_Imm32) && ((TySize[src->GetType()] != 8) ||
  1864. (!instr->isInlineeEntryInstr && Math::FitsInDWord(int64Opnd->GetValue()))))
  1865. {
  1866. // the immediate fits in 32-bit, no need to hoist
  1867. return;
  1868. }
  1869. if (verify)
  1870. {
  1871. AssertMsg(false, "Missing legalization");
  1872. return;
  1873. }
  1874. IR::Opnd* regOpnd = IR::RegOpnd::New(src->GetType(), instr->m_func);
  1875. IR::Instr* moveToReg = IR::Instr::New(Js::OpCode::MOV, regOpnd, src, instr->m_func);
  1876. instr->InsertBefore(moveToReg);
  1877. instr->ReplaceSrc(src, regOpnd);
  1878. return;
  1879. }
  1880. #endif
  1881. break;
  1882. case IR::OpndKindAddr:
  1883. if (forms & L_Ptr)
  1884. {
  1885. return;
  1886. }
  1887. #ifdef _M_X64
  1888. {
  1889. IR::AddrOpnd * addrOpnd = src->AsAddrOpnd();
  1890. if ((forms & L_Imm32) && ((TySize[addrOpnd->GetType()] != 8) ||
  1891. (!instr->isInlineeEntryInstr && Math::FitsInDWord((size_t)addrOpnd->m_address))))
  1892. {
  1893. // the address fits in 32-bit, no need to hoist
  1894. return;
  1895. }
  1896. if (verify)
  1897. {
  1898. AssertMsg(false, "Missing legalization");
  1899. return;
  1900. }
  1901. Assert(!instr->isInlineeEntryInstr);
  1902. Assert(forms & L_Reg);
  1903. // TODO: michhol, remove cast after making m_address intptr
  1904. IR::AddrOpnd * newAddrOpnd = addrOpnd->Copy(instr->m_func)->AsAddrOpnd();
  1905. IR::IndirOpnd * indirOpnd = instr->m_func->GetTopFunc()->GetConstantAddressIndirOpnd((intptr_t)addrOpnd->m_address, newAddrOpnd, addrOpnd->GetAddrOpndKind(), TyMachPtr, Js::OpCode::MOV);
  1906. if (HoistLargeConstant(indirOpnd, src, instr))
  1907. {
  1908. return;
  1909. }
  1910. }
  1911. #endif
  1912. break;
  1913. case IR::OpndKindMemRef:
  1914. {
  1915. IR::MemRefOpnd *const memRefOpnd = src->AsMemRefOpnd();
  1916. if(!LowererMDArch::IsLegalMemLoc(memRefOpnd))
  1917. {
  1918. if (verify)
  1919. {
  1920. AssertMsg(false, "Missing legalization");
  1921. return;
  1922. }
  1923. src = instr->HoistMemRefAddress(memRefOpnd, Js::OpCode::MOV);
  1924. }
  1925. // fall through
  1926. }
  1927. case IR::OpndKindSym:
  1928. case IR::OpndKindIndir:
  1929. if(forms & L_Mem)
  1930. {
  1931. return;
  1932. }
  1933. break;
  1934. case IR::OpndKindHelperCall:
  1935. case IR::OpndKindLabel:
  1936. Assert(!instr->isInlineeEntryInstr);
  1937. Assert(forms & L_Ptr);
  1938. return;
  1939. default:
  1940. Assert(false);
  1941. __assume(false);
  1942. }
  1943. if (verify)
  1944. {
  1945. AssertMsg(false, "Missing legalization");
  1946. return;
  1947. }
  1948. // Hoist the src into a reg
  1949. Assert(forms & L_Reg);
  1950. Assert(!(instr->GetDst() && instr->GetDst()->IsEqual(src)));
  1951. const Js::OpCode loadOpCode = GetLoadOp(src->GetType());
  1952. if(src == instr->GetSrc2())
  1953. {
  1954. instr->HoistSrc2(loadOpCode);
  1955. return;
  1956. }
  1957. const bool equalsSrc2 = instr->GetSrc2() && src->IsEqual(instr->GetSrc2());
  1958. IR::Instr * hoistInstr = instr->HoistSrc1(loadOpCode);
  1959. if(equalsSrc2)
  1960. {
  1961. instr->ReplaceSrc2(hoistInstr->GetDst());
  1962. }
  1963. hoistInstr->isInlineeEntryInstr = instr->isInlineeEntryInstr;
  1964. instr->isInlineeEntryInstr = false;
  1965. }
  1966. template void LowererMD::Legalize<false>(IR::Instr *const instr, bool fPostRegAlloc);
  1967. template void LowererMD::LegalizeOpnds<false>(IR::Instr *const instr, const uint dstForms, const uint src1Forms, uint src2Forms);
  1968. template void LowererMD::LegalizeDst<false>(IR::Instr *const instr, const uint forms);
  1969. template void LowererMD::LegalizeSrc<false>(IR::Instr *const instr, IR::Opnd *src, const uint forms);
  1970. template void LowererMD::MakeDstEquSrc1<false>(IR::Instr *const instr);
  1971. #if DBG
  1972. template void LowererMD::Legalize<true>(IR::Instr *const instr, bool fPostRegAlloc);
  1973. template void LowererMD::LegalizeOpnds<true>(IR::Instr *const instr, const uint dstForms, const uint src1Forms, uint src2Forms);
  1974. template void LowererMD::LegalizeDst<true>(IR::Instr *const instr, const uint forms);
  1975. template void LowererMD::LegalizeSrc<true>(IR::Instr *const instr, IR::Opnd *src, const uint forms);
  1976. template void LowererMD::MakeDstEquSrc1<true>(IR::Instr *const instr);
  1977. #endif
  1978. IR::Instr *
  1979. LowererMD::LoadFunctionObjectOpnd(IR::Instr *instr, IR::Opnd *&functionObjOpnd)
  1980. {
  1981. IR::Opnd * src1 = instr->GetSrc1();
  1982. IR::Instr * instrPrev = instr->m_prev;
  1983. if (src1 == nullptr)
  1984. {
  1985. IR::RegOpnd * regOpnd = IR::RegOpnd::New(TyMachPtr, m_func);
  1986. StackSym *paramSym = StackSym::New(TyMachPtr, m_func);
  1987. IR::SymOpnd *paramOpnd = IR::SymOpnd::New(paramSym, TyMachPtr, m_func);
  1988. this->m_func->SetArgOffset(paramSym, 2 * MachPtr);
  1989. IR::Instr * mov1 = IR::Instr::New(Js::OpCode::MOV, regOpnd, paramOpnd, m_func);
  1990. instr->InsertBefore(mov1);
  1991. functionObjOpnd = mov1->GetDst()->AsRegOpnd();
  1992. instrPrev = mov1;
  1993. instr->m_func->SetHasImplicitParamLoad();
  1994. }
  1995. else
  1996. {
  1997. // Inlinee, use the function object opnd on the instruction
  1998. functionObjOpnd = instr->UnlinkSrc1();
  1999. if (!functionObjOpnd->IsRegOpnd())
  2000. {
  2001. Assert(functionObjOpnd->IsAddrOpnd());
  2002. }
  2003. }
  2004. return instrPrev;
  2005. }
  2006. IR::Instr *
  2007. LowererMD::LowerLdSuper(IR::Instr *instr, IR::JnHelperMethod helperOpCode)
  2008. {
  2009. IR::Opnd * functionObjOpnd;
  2010. IR::Instr * instrPrev = LoadFunctionObjectOpnd(instr, functionObjOpnd);
  2011. m_lowerer->LoadScriptContext(instr);
  2012. LoadHelperArgument(instr, functionObjOpnd);
  2013. ChangeToHelperCall(instr, helperOpCode);
  2014. return instrPrev;
  2015. }
  2016. void
  2017. LowererMD::GenerateFastDivByPow2(IR::Instr *instr)
  2018. {
  2019. //
  2020. // Given:
  2021. // dst = Div_A src1, src2
  2022. // where src2 == power of 2
  2023. //
  2024. // Generate:
  2025. // MOV s1, src1
  2026. // AND s1, 0xFFFF000000000000 | (src2Value-1) ----- test for tagged int and divisibility by src2Value [int32]
  2027. // AND s1, 0x00000001 | ((src2Value-1)<<1) [int31]
  2028. // CMP s1, AtomTag_IntPtr
  2029. // JNE $divbyhalf
  2030. // MOV s1, src1
  2031. // SAR s1, log2(src2Value) ------ perform the divide
  2032. // OR s1, 1
  2033. // MOV dst, s1
  2034. // JMP $done
  2035. // $divbyhalf:
  2036. // AND s1, 0xFFFF000000000000 | (src2Value-1>>1) ----- test for tagged int and divisibility by src2Value /2 [int32]
  2037. // AND s1, 0x00000001 | ((src2Value-1)) [int31]
  2038. // CMP s1, AtomTag_IntPtr
  2039. // JNE $helper
  2040. // MOV s1, src1
  2041. // SAR s1, log2(src2Value) [int32]
  2042. // SAR s1, log2(src2Value) + 1 ------ removes the tag and divides [int31]
  2043. // PUSH s1
  2044. // PUSH 0xXXXXXXXX (ScriptContext)
  2045. // CALL Op_FinishOddDivByPow2
  2046. // MOV dst, eax
  2047. // JMP $done
  2048. // $helper:
  2049. // ...
  2050. // $done:
  2051. //
  2052. if (instr->GetSrc1()->IsRegOpnd() && instr->GetSrc1()->AsRegOpnd()->IsNotInt())
  2053. return;
  2054. IR::Opnd *dst = instr->GetDst();
  2055. IR::Opnd *src1 = instr->GetSrc1();
  2056. IR::AddrOpnd *src2 = instr->GetSrc2()->IsAddrOpnd() ? instr->GetSrc2()->AsAddrOpnd() : nullptr;
  2057. IR::LabelInstr *divbyhalf = IR::LabelInstr::New(Js::OpCode::Label, m_func);
  2058. IR::LabelInstr *helper = IR::LabelInstr::New(Js::OpCode::Label, m_func, true);
  2059. IR::LabelInstr *done = IR::LabelInstr::New(Js::OpCode::Label, m_func);
  2060. IR::RegOpnd *s1 = IR::RegOpnd::New(TyVar, m_func);
  2061. AnalysisAssert(src2);
  2062. Assert(src2->IsVar() && Js::TaggedInt::Is(src2->m_address) && (Math::IsPow2(Js::TaggedInt::ToInt32(src2->m_address))));
  2063. int32 src2Value = Js::TaggedInt::ToInt32(src2->m_address);
  2064. // MOV s1, src1
  2065. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, s1, src1, m_func));
  2066. #if INT32VAR
  2067. // dontEncode as src2 is a power of 2.
  2068. IR::Opnd *constant = IR::AddrOpnd::New((Js::Var)(0xFFFF000000000000 | (src2Value - 1)), IR::AddrOpndKindConstantVar, m_func, /* dontEncode = */ true);
  2069. #else
  2070. IR::Opnd *constant = IR::IntConstOpnd::New((0x00000001 | ((src2Value - 1) << 1)), TyInt32, m_func);
  2071. #endif
  2072. // AND s1, constant
  2073. {
  2074. IR::Instr * andInstr = IR::Instr::New(Js::OpCode::AND, s1, s1, constant, m_func);
  2075. instr->InsertBefore(andInstr);
  2076. Legalize(andInstr);
  2077. }
  2078. // CMP s1, AtomTag_IntPtr
  2079. {
  2080. IR::Instr *cmp = IR::Instr::New(Js::OpCode::CMP, m_func);
  2081. cmp->SetSrc1(s1);
  2082. cmp->SetSrc2(IR::AddrOpnd::New((Js::Var)(Js::AtomTag_IntPtr), IR::AddrOpndKindConstantVar, m_func, /* dontEncode = */ true));
  2083. instr->InsertBefore(cmp);
  2084. Legalize(cmp);
  2085. }
  2086. // JNE $divbyhalf
  2087. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JNE, divbyhalf, m_func));
  2088. // MOV s1, src1
  2089. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, s1, src1, m_func));
  2090. s1 = s1->UseWithNewType(TyInt32, m_func)->AsRegOpnd();
  2091. // SAR s1, log2(src2Value)
  2092. instr->InsertBefore(IR::Instr::New(Js::OpCode::SAR, s1, s1, IR::IntConstOpnd::New(Math::Log2(src2Value), TyInt32, m_func), m_func));
  2093. if(s1->GetSize() != MachPtr)
  2094. {
  2095. s1 = s1->UseWithNewType(TyMachPtr, m_func)->AsRegOpnd();
  2096. }
  2097. #if INT32VAR
  2098. GenerateInt32ToVarConversion(s1, instr);
  2099. #else
  2100. // OR s1, 1
  2101. instr->InsertBefore(IR::Instr::New(Js::OpCode::OR, s1, s1, IR::IntConstOpnd::New(1, TyInt32, m_func), m_func));
  2102. #endif
  2103. // MOV dst, s1
  2104. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, dst, s1, m_func));
  2105. // JMP $done
  2106. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JMP, done, m_func));
  2107. // $divbyhalf:
  2108. instr->InsertBefore(divbyhalf);
  2109. #if INT32VAR
  2110. constant = IR::AddrOpnd::New((Js::Var)(0xFFFF000000000000 | ((src2Value-1) >> 1)), IR::AddrOpndKindConstantVar, m_func, /* dontEncode = */ true);
  2111. #else
  2112. constant = IR::IntConstOpnd::New((0x00000001 | (src2Value-1)), TyInt32, m_func);
  2113. #endif
  2114. // AND s1, constant
  2115. {
  2116. IR::Instr * andInstr = IR::Instr::New(Js::OpCode::AND, s1, s1, constant, m_func);
  2117. instr->InsertBefore(andInstr);
  2118. Legalize(andInstr);
  2119. }
  2120. // CMP s1, AtomTag_IntPtr
  2121. {
  2122. IR::Instr *cmp = IR::Instr::New(Js::OpCode::CMP, m_func);
  2123. cmp->SetSrc1(s1);
  2124. cmp->SetSrc2(IR::AddrOpnd::New((Js::Var)(Js::AtomTag_IntPtr), IR::AddrOpndKindConstantVar, m_func, /* dontEncode = */ true));
  2125. instr->InsertBefore(cmp);
  2126. Legalize(cmp);
  2127. }
  2128. // JNE $helper
  2129. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JNE, helper, m_func));
  2130. // MOV s1, src1
  2131. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, s1, src1, m_func));
  2132. s1 = s1->UseWithNewType(TyInt32, this->m_func)->AsRegOpnd();
  2133. #if INT32VAR
  2134. IR::Opnd* shiftOpnd = IR::IntConstOpnd::New(Math::Log2(src2Value), TyInt32, m_func);
  2135. #else
  2136. IR::Opnd* shiftOpnd = IR::IntConstOpnd::New(Math::Log2(src2Value) + 1, TyInt32, m_func);
  2137. #endif
  2138. // SAR s1, shiftOpnd
  2139. instr->InsertBefore(IR::Instr::New(Js::OpCode::SAR, s1, s1, shiftOpnd, m_func));
  2140. // PUSH s1
  2141. // PUSH ScriptContext
  2142. // CALL Op_FinishOddDivByPow2
  2143. {
  2144. IR::JnHelperMethod helperMethod;
  2145. if (instr->dstIsTempNumber)
  2146. {
  2147. IR::Opnd *tempOpnd;
  2148. helperMethod = IR::HelperOp_FinishOddDivByPow2InPlace;
  2149. Assert(dst->IsRegOpnd());
  2150. StackSym * tempNumberSym = this->m_lowerer->GetTempNumberSym(dst, instr->dstIsTempNumberTransferred);
  2151. IR::Instr *load = this->LoadStackAddress(tempNumberSym);
  2152. instr->InsertBefore(load);
  2153. tempOpnd = load->GetDst();
  2154. this->lowererMDArch.LoadHelperArgument(instr, tempOpnd);
  2155. }
  2156. else
  2157. {
  2158. helperMethod = IR::HelperOp_FinishOddDivByPow2;
  2159. }
  2160. m_lowerer->LoadScriptContext(instr);
  2161. lowererMDArch.LoadHelperArgument(instr, s1);
  2162. IR::Instr *call = IR::Instr::New(Js::OpCode::Call, dst, IR::HelperCallOpnd::New(helperMethod, m_func), m_func);
  2163. instr->InsertBefore(call);
  2164. lowererMDArch.LowerCall(call, 0);
  2165. }
  2166. // JMP $done
  2167. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JMP, done, m_func));
  2168. // $helper:
  2169. instr->InsertBefore(helper);
  2170. // $done:
  2171. instr->InsertAfter(done);
  2172. }
  2173. bool
  2174. LowererMD::GenerateFastBrOrCmString(IR::Instr* instr)
  2175. {
  2176. IR::RegOpnd *srcReg1 = instr->GetSrc1()->IsRegOpnd() ? instr->GetSrc1()->AsRegOpnd() : nullptr;
  2177. IR::RegOpnd *srcReg2 = instr->GetSrc2()->IsRegOpnd() ? instr->GetSrc2()->AsRegOpnd() : nullptr;
  2178. if (!srcReg1 ||
  2179. !srcReg2 ||
  2180. srcReg1->IsTaggedInt() ||
  2181. srcReg2->IsTaggedInt() ||
  2182. !srcReg1->GetValueType().IsLikelyString() ||
  2183. !srcReg2->GetValueType().IsLikelyString())
  2184. {
  2185. return false;
  2186. }
  2187. IR::LabelInstr *labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  2188. IR::LabelInstr *labelBranchFail = nullptr;
  2189. IR::LabelInstr *labelBranchSuccess = nullptr;
  2190. IR::Instr *instrMovSuccess = nullptr;
  2191. IR::Instr *instrMovFailure = nullptr;
  2192. bool isEqual = false;
  2193. bool isStrict = false;
  2194. bool isBranch = true;
  2195. bool isCmNegOp = false;
  2196. switch (instr->m_opcode)
  2197. {
  2198. case Js::OpCode::BrSrEq_A:
  2199. case Js::OpCode::BrSrNotNeq_A:
  2200. isStrict = true;
  2201. case Js::OpCode::BrEq_A:
  2202. case Js::OpCode::BrNotNeq_A:
  2203. labelBranchFail = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  2204. labelBranchSuccess = instr->AsBranchInstr()->GetTarget();
  2205. instr->InsertAfter(labelBranchFail);
  2206. isEqual = true;
  2207. break;
  2208. case Js::OpCode::BrSrNeq_A:
  2209. case Js::OpCode::BrSrNotEq_A:
  2210. isStrict = true;
  2211. case Js::OpCode::BrNeq_A:
  2212. case Js::OpCode::BrNotEq_A:
  2213. labelBranchSuccess = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  2214. labelBranchFail = instr->AsBranchInstr()->GetTarget();
  2215. instr->InsertAfter(labelBranchSuccess);
  2216. isEqual = false;
  2217. break;
  2218. case Js::OpCode::CmSrEq_A:
  2219. isStrict = true;
  2220. case Js::OpCode::CmEq_A:
  2221. isEqual = true;
  2222. isBranch = false;
  2223. break;
  2224. case Js::OpCode::CmSrNeq_A:
  2225. isStrict = true;
  2226. case Js::OpCode::CmNeq_A:
  2227. isEqual = false;
  2228. isBranch = false;
  2229. isCmNegOp = true;
  2230. break;
  2231. default:
  2232. Assert(UNREACHED);
  2233. __assume(0);
  2234. }
  2235. if (!isBranch)
  2236. {
  2237. labelBranchSuccess = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  2238. labelBranchFail = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  2239. LibraryValue successValueType = !isCmNegOp ? LibraryValue::ValueTrue : LibraryValue::ValueFalse;
  2240. LibraryValue failureValueType = !isCmNegOp ? LibraryValue::ValueFalse : LibraryValue::ValueTrue;
  2241. instrMovFailure = IR::Instr::New(Js::OpCode::MOV, instr->GetDst(), this->m_lowerer->LoadLibraryValueOpnd(instr, failureValueType), m_func);
  2242. instrMovSuccess = IR::Instr::New(Js::OpCode::MOV, instr->GetDst(), this->m_lowerer->LoadLibraryValueOpnd(instr, successValueType), m_func);
  2243. }
  2244. this->GenerateFastStringCheck(instr, srcReg1, srcReg2, isEqual, isStrict, labelHelper, labelBranchSuccess, labelBranchFail);
  2245. IR::LabelInstr *labelFallthrough = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  2246. if (!isBranch)
  2247. {
  2248. instr->InsertBefore(labelBranchSuccess);
  2249. instr->InsertBefore(instrMovSuccess);
  2250. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JMP, labelFallthrough, this->m_func));
  2251. instr->InsertBefore(labelBranchFail);
  2252. instr->InsertBefore(instrMovFailure);
  2253. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JMP, labelFallthrough, this->m_func));
  2254. }
  2255. instr->InsertBefore(labelHelper);
  2256. instr->InsertAfter(labelFallthrough);
  2257. #if DBG
  2258. // The fast-path for strings assumes the case where 2 strings are equal is rare, and marks that path as 'helper'.
  2259. // This breaks the helper label dbchecks as it can result in non-helper blocks be reachable only from helper blocks.
  2260. // Use m_isHelperToNonHelperBranch and m_noHelperAssert to fix this.
  2261. IR::Instr *blockEndInstr;
  2262. if (isEqual)
  2263. {
  2264. blockEndInstr = labelHelper->GetNextBranchOrLabel();
  2265. }
  2266. else
  2267. {
  2268. blockEndInstr = instr->GetNextBranchOrLabel();
  2269. }
  2270. if (blockEndInstr->IsBranchInstr())
  2271. {
  2272. blockEndInstr->AsBranchInstr()->m_isHelperToNonHelperBranch = true;
  2273. }
  2274. labelFallthrough->m_noHelperAssert = true;
  2275. #endif
  2276. return true;
  2277. }
  2278. bool
  2279. LowererMD::GenerateFastStringCheck(IR::Instr *instr, IR::RegOpnd *srcReg1, IR::RegOpnd *srcReg2, bool isEqual, bool isStrict, IR::LabelInstr *labelHelper, IR::LabelInstr *labelBranchSuccess, IR::LabelInstr *labelBranchFail)
  2280. {
  2281. Assert(instr->m_opcode == Js::OpCode::BrSrEq_A ||
  2282. instr->m_opcode == Js::OpCode::BrSrNeq_A ||
  2283. instr->m_opcode == Js::OpCode::BrEq_A ||
  2284. instr->m_opcode == Js::OpCode::BrNeq_A ||
  2285. instr->m_opcode == Js::OpCode::BrSrNotEq_A ||
  2286. instr->m_opcode == Js::OpCode::BrSrNotNeq_A ||
  2287. instr->m_opcode == Js::OpCode::BrNotEq_A ||
  2288. instr->m_opcode == Js::OpCode::BrNotNeq_A ||
  2289. instr->m_opcode == Js::OpCode::CmEq_A ||
  2290. instr->m_opcode == Js::OpCode::CmNeq_A ||
  2291. instr->m_opcode == Js::OpCode::CmSrEq_A ||
  2292. instr->m_opcode == Js::OpCode::CmSrNeq_A );
  2293. // if src1 is not string
  2294. // generate object test, if not equal jump to $helper
  2295. // compare type check to string, if not jump to $helper
  2296. //
  2297. // if strict mode generate string test as above for src1 and jump to $failure if failed any time
  2298. // else if not strict generate string test as above for src1 and jump to $helper if failed any time
  2299. //
  2300. // Compare length of src1 and src2 if not equal goto $failure
  2301. //
  2302. // if src1 is not flat string jump to $helper
  2303. //
  2304. // if src1 and src2 m_pszValue pointer match goto $success
  2305. //
  2306. // if src2 is not flat string jump to $helper
  2307. //
  2308. // if first character of src1 and src2 doesn't match goto $failure
  2309. //
  2310. // shift left by 1 length of src1 (length*2)
  2311. //
  2312. // memcmp src1 and src2 flat strings till length * 2
  2313. //
  2314. // test eax (result of memcmp)
  2315. // if equal jump to $success else to $failure
  2316. //
  2317. // $success
  2318. // jmp to $fallthrough
  2319. // $failure
  2320. // jmp to $fallthrough
  2321. // $helper
  2322. //
  2323. // $fallthrough
  2324. // Generates:
  2325. // GenerateObjectTest(src1);
  2326. // MOV s1, [srcReg1 + offset(Type)]
  2327. // CMP type, static_string_type
  2328. // JNE $helper
  2329. // GenerateObjectTest(src2);
  2330. // MOV s2, [srcReg2 + offset(Type)]
  2331. // CMP type, static_string_type
  2332. // JNE $fail ; if src1 is string but not src2, src1 !== src2 if isStrict
  2333. // MOV s3, [srcReg1,offset(m_charLength)]
  2334. // CMP [srcReg2,offset(m_charLength)], s3
  2335. // JNE $fail <--- length check done
  2336. // MOV s4, [srcReg1,offset(m_pszValue)]
  2337. // CMP srcReg1, srcReg2
  2338. // JEQ $success
  2339. // CMP s4, 0
  2340. // JEQ $helper
  2341. // MOV s5, [srcReg2,offset(m_pszValue)]
  2342. // CMP s5, 0
  2343. // JEQ $helper
  2344. // MOV s6,[s4]
  2345. // CMP [s5], s6 -First character comparison
  2346. // JNE $fail
  2347. // SHL length, 1
  2348. // eax = memcmp(src1String, src2String, length*2)
  2349. // TEST eax, eax
  2350. // JEQ $success
  2351. // JMP $fail
  2352. IR::Instr* instrInsert = instr;
  2353. this->m_lowerer->GenerateStringTest(srcReg1, instrInsert, labelHelper);
  2354. if (isStrict)
  2355. {
  2356. this->m_lowerer->GenerateStringTest(srcReg2, instrInsert, labelBranchFail);
  2357. }
  2358. else
  2359. {
  2360. this->m_lowerer->GenerateStringTest(srcReg2, instrInsert, labelHelper);
  2361. }
  2362. // MOV s3, [srcReg1,offset(m_charLength)]
  2363. // CMP [srcReg2,offset(m_charLength)], s3
  2364. // JNE $branchfail
  2365. IR::RegOpnd * src1LengthOpnd = IR::RegOpnd::New(TyUint32, this->m_func);
  2366. IR::Instr * loadSrc1LengthInstr = IR::Instr::New(Js::OpCode::MOV, src1LengthOpnd,
  2367. IR::IndirOpnd::New(srcReg1, Js::JavascriptString::GetOffsetOfcharLength(), TyUint32,
  2368. this->m_func), this->m_func);
  2369. instrInsert->InsertBefore(loadSrc1LengthInstr);
  2370. IR::Instr * checkLengthInstr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  2371. checkLengthInstr->SetSrc1(IR::IndirOpnd::New(srcReg2, Js::JavascriptString::GetOffsetOfcharLength(), TyUint32, this->m_func));
  2372. checkLengthInstr->SetSrc2(src1LengthOpnd);
  2373. instrInsert->InsertBefore(checkLengthInstr);
  2374. instrInsert->InsertBefore(IR::BranchInstr::New(Js::OpCode::JNE, labelBranchFail, this->m_func));
  2375. // MOV s4, [src1,offset(m_pszValue)]
  2376. // CMP s4, 0
  2377. // JEQ $helper
  2378. // MOV s5, [src2,offset(m_pszValue)]
  2379. // CMP s5, 0
  2380. // JEQ $helper
  2381. IR::RegOpnd * src1FlatString = IR::RegOpnd::New(TyMachPtr, this->m_func);
  2382. IR::Instr * loadSrc1StringInstr = IR::Instr::New(Js::OpCode::MOV, src1FlatString,
  2383. IR::IndirOpnd::New(srcReg1, Js::JavascriptString::GetOffsetOfpszValue(), TyMachPtr,
  2384. this->m_func), this->m_func);
  2385. instrInsert->InsertBefore(loadSrc1StringInstr);
  2386. IR::Instr * checkFlatString1Instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  2387. checkFlatString1Instr->SetSrc1(src1FlatString);
  2388. checkFlatString1Instr->SetSrc2(IR::IntConstOpnd::New(0, TyUint32, this->m_func));
  2389. instrInsert->InsertBefore(checkFlatString1Instr);
  2390. instrInsert->InsertBefore(IR::BranchInstr::New(Js::OpCode::JEQ, labelHelper, this->m_func));
  2391. IR::RegOpnd * src2FlatString = IR::RegOpnd::New(TyMachPtr, this->m_func);
  2392. IR::Instr * loadSrc2StringInstr = IR::Instr::New(Js::OpCode::MOV, src2FlatString,
  2393. IR::IndirOpnd::New(srcReg2, Js::JavascriptString::GetOffsetOfpszValue(), TyMachPtr,
  2394. this->m_func), this->m_func);
  2395. instrInsert->InsertBefore(loadSrc2StringInstr);
  2396. // CMP srcReg1, srcReg2 - Ptr comparison
  2397. // JEQ $branchSuccess
  2398. IR::Instr * comparePtrInstr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  2399. comparePtrInstr->SetSrc1(srcReg1);
  2400. comparePtrInstr->SetSrc2(srcReg2);
  2401. instrInsert->InsertBefore(comparePtrInstr);
  2402. instrInsert->InsertBefore(IR::BranchInstr::New(Js::OpCode::JEQ, labelBranchSuccess, this->m_func));
  2403. IR::Instr * checkFlatString2Instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  2404. checkFlatString2Instr->SetSrc1(src2FlatString);
  2405. checkFlatString2Instr->SetSrc2(IR::IntConstOpnd::New(0, TyUint32, this->m_func));
  2406. instrInsert->InsertBefore(checkFlatString2Instr);
  2407. instrInsert->InsertBefore(IR::BranchInstr::New(Js::OpCode::JEQ, labelHelper, this->m_func));
  2408. // MOV s6,[s4]
  2409. // CMP [s5], s6 -First character comparison
  2410. // JNE $branchfail
  2411. IR::RegOpnd * src1FirstChar = IR::RegOpnd::New(TyUint16, this->m_func);
  2412. IR::Instr * loadSrc1CharInstr = IR::Instr::New(Js::OpCode::MOV, src1FirstChar,
  2413. IR::IndirOpnd::New(src1FlatString, 0, TyUint16,
  2414. this->m_func), this->m_func);
  2415. instrInsert->InsertBefore(loadSrc1CharInstr);
  2416. IR::Instr * compareFirstCharInstr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  2417. compareFirstCharInstr->SetSrc1(IR::IndirOpnd::New(src2FlatString, 0, TyUint16, this->m_func));
  2418. compareFirstCharInstr->SetSrc2(src1FirstChar);
  2419. instrInsert->InsertBefore(compareFirstCharInstr);
  2420. instrInsert->InsertBefore(IR::BranchInstr::New(Js::OpCode::JNE, labelBranchFail, this->m_func));
  2421. // SHL length, 1
  2422. instrInsert->InsertBefore(IR::Instr::New(Js::OpCode::SHL, src1LengthOpnd, src1LengthOpnd, IR::IntConstOpnd::New(1, TyUint8, this->m_func), this->m_func));
  2423. // eax = memcmp(src1String, src2String, length*2)
  2424. this->LoadHelperArgument(instr, src1LengthOpnd);
  2425. this->LoadHelperArgument(instr, src1FlatString);
  2426. this->LoadHelperArgument(instr, src2FlatString);
  2427. IR::RegOpnd *dstOpnd = IR::RegOpnd::New(TyInt32, this->m_func);
  2428. IR::Instr *instrCall = IR::Instr::New(Js::OpCode::CALL, dstOpnd, IR::HelperCallOpnd::New(IR::HelperMemCmp, this->m_func), this->m_func);
  2429. instr->InsertBefore(instrCall);
  2430. this->LowerCall(instrCall, 3);
  2431. // TEST eax, eax
  2432. IR::Instr *instrTest = IR::Instr::New(Js::OpCode::TEST, this->m_func);
  2433. instrTest->SetSrc1(instrCall->GetDst());
  2434. instrTest->SetSrc2(instrCall->GetDst());
  2435. instrInsert->InsertBefore(instrTest);
  2436. // JEQ success
  2437. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JEQ, labelBranchSuccess, this->m_func));
  2438. // JMP fail
  2439. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JMP, labelBranchFail, this->m_func));
  2440. return true;
  2441. }
  2442. ///----------------------------------------------------------------------------
  2443. ///
  2444. /// LowererMD::GenerateFastCmSrEqConst
  2445. ///
  2446. ///----------------------------------------------------------------------------
  2447. bool
  2448. LowererMD::GenerateFastCmSrEqConst(IR::Instr *instr)
  2449. {
  2450. //
  2451. // Given:
  2452. // s1 = CmSrEq_A s2, s3
  2453. // where either s2 or s3 is 'null', 'true' or 'false'
  2454. //
  2455. // Generate:
  2456. //
  2457. // CMP s2, s3
  2458. // JEQ $mov_true
  2459. // MOV s1, Library.GetFalse()
  2460. // JMP $done
  2461. // $mov_true:
  2462. // MOV s1, Library.GetTrue()
  2463. // $done:
  2464. //
  2465. Assert(m_lowerer->IsConstRegOpnd(instr->GetSrc2()->AsRegOpnd()));
  2466. IR::Opnd *opnd = instr->GetSrc1();
  2467. IR::RegOpnd *opndReg = instr->GetSrc2()->AsRegOpnd();
  2468. IR::LabelInstr *labelMovTrue = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  2469. IR::LabelInstr *labelDone = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  2470. if (!opnd->IsRegOpnd())
  2471. {
  2472. IR::RegOpnd *lhsReg = IR::RegOpnd::New(TyVar, m_func);
  2473. IR::Instr *mov = IR::Instr::New(Js::OpCode::MOV, lhsReg, opnd, m_func);
  2474. instr->InsertBefore(mov);
  2475. opnd = lhsReg;
  2476. }
  2477. Assert(opnd->IsRegOpnd());
  2478. // CMP s2, s3
  2479. // JEQ $mov_true
  2480. this->m_lowerer->InsertCompareBranch(opnd, opndReg->m_sym->GetConstOpnd(), Js::OpCode::BrEq_A, labelMovTrue, instr);
  2481. // MOV s1, 'false'
  2482. IR::Instr *instrMov = IR::Instr::New(Js::OpCode::MOV,
  2483. instr->GetDst(),
  2484. m_lowerer->LoadLibraryValueOpnd(instr, LibraryValue::ValueFalse),
  2485. m_func);
  2486. instr->InsertBefore(instrMov);
  2487. // JMP $done
  2488. IR::BranchInstr *jmp = IR::BranchInstr::New(Js::OpCode::JMP, labelDone, this->m_func);
  2489. instr->InsertBefore(jmp);
  2490. // $mov_true:
  2491. instr->InsertBefore(labelMovTrue);
  2492. // MOV s1, 'true'
  2493. instr->m_opcode = Js::OpCode::MOV;
  2494. instr->UnlinkSrc1();
  2495. instr->UnlinkSrc2();
  2496. instr->SetSrc1(m_lowerer->LoadLibraryValueOpnd(instr, LibraryValue::ValueTrue));
  2497. instr->ClearBailOutInfo();
  2498. Legalize(instr);
  2499. // $done:
  2500. instr->InsertAfter(labelDone);
  2501. return true;
  2502. }
  2503. ///----------------------------------------------------------------------------
  2504. ///
  2505. /// LowererMD::GenerateFastCmXxTaggedInt
  2506. ///
  2507. ///----------------------------------------------------------------------------
  2508. bool LowererMD::GenerateFastCmXxTaggedInt(IR::Instr *instr)
  2509. {
  2510. // The idea is to do an inline compare if we can prove that both sources
  2511. // are tagged ints (i.e., are vars with the low bit set).
  2512. //
  2513. // Given:
  2514. //
  2515. // Cmxx_A dst, src1, src2
  2516. //
  2517. // Generate:
  2518. //
  2519. // (If not Int31's, goto $helper)
  2520. // MOV r1, src1
  2521. // if (==, !=, !== or ===)
  2522. // SUB r1, src2
  2523. // NEG r1 // Sets CF if r1 != 0
  2524. // SBB r1, r1 // CF == 1 ? r1 = -1 : r1 = 0
  2525. // else
  2526. // MOV r2, 0
  2527. // CMP r1, src2
  2528. // SETcc r2
  2529. // DEC r2
  2530. // set r1 to r2
  2531. // AND r1, (notEqualResult - equalResult)
  2532. // ADD r1, equalResult
  2533. // MOV dst, r1
  2534. // JMP $fallthru
  2535. // $helper:
  2536. // (caller will generate normal helper call sequence)
  2537. // $fallthru:
  2538. IR::Opnd * src1 = instr->GetSrc1();
  2539. IR::Opnd * src2 = instr->GetSrc2();
  2540. IR::Opnd * dst = instr->GetDst();
  2541. IR::RegOpnd * r1 = IR::RegOpnd::New(TyMachReg, m_func);
  2542. IR::LabelInstr * helper = IR::LabelInstr::New(Js::OpCode::Label, m_func, true);
  2543. IR::LabelInstr * fallthru = IR::LabelInstr::New(Js::OpCode::Label, m_func);
  2544. Assert(src1 && src2 && dst);
  2545. // Not tagged ints?
  2546. if (src1->IsRegOpnd() && src1->AsRegOpnd()->IsNotInt())
  2547. {
  2548. return false;
  2549. }
  2550. if (src2->IsRegOpnd() && src2->AsRegOpnd()->IsNotInt())
  2551. {
  2552. return false;
  2553. }
  2554. bool isNeqOp = instr->m_opcode == Js::OpCode::CmSrNeq_A || instr->m_opcode == Js::OpCode::CmNeq_A;
  2555. intptr_t notEqualResult = isNeqOp ? m_func->GetScriptContextInfo()->GetTrueAddr() : m_func->GetScriptContextInfo()->GetFalseAddr();
  2556. intptr_t equalResult = !isNeqOp ? m_func->GetScriptContextInfo()->GetTrueAddr() : m_func->GetScriptContextInfo()->GetFalseAddr();
  2557. // Tagged ints?
  2558. bool isTaggedInts = false;
  2559. if (src1->IsTaggedInt())
  2560. {
  2561. if (src2->IsTaggedInt())
  2562. {
  2563. isTaggedInts = true;
  2564. }
  2565. }
  2566. if (!isTaggedInts)
  2567. {
  2568. this->GenerateSmIntPairTest(instr, src1, src2, helper);
  2569. }
  2570. // MOV r1, src1
  2571. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, r1, src1, m_func));
  2572. Js::OpCode setCC_Opcode = Js::OpCode::Nop;
  2573. switch(instr->m_opcode)
  2574. {
  2575. case Js::OpCode::CmSrEq_A:
  2576. case Js::OpCode::CmEq_A:
  2577. break;
  2578. case Js::OpCode::CmSrNeq_A:
  2579. case Js::OpCode::CmNeq_A:
  2580. break;
  2581. case Js::OpCode::CmGe_A:
  2582. setCC_Opcode = Js::OpCode::SETGE;
  2583. break;
  2584. case Js::OpCode::CmGt_A:
  2585. setCC_Opcode = Js::OpCode::SETG;
  2586. break;
  2587. case Js::OpCode::CmLe_A:
  2588. setCC_Opcode = Js::OpCode::SETLE;
  2589. break;
  2590. case Js::OpCode::CmLt_A:
  2591. setCC_Opcode = Js::OpCode::SETL;
  2592. break;
  2593. default:
  2594. Assume(UNREACHED);
  2595. }
  2596. if (setCC_Opcode == Js::OpCode::Nop)
  2597. {
  2598. // SUB r1, src2
  2599. IR::Instr * subInstr = IR::Instr::New(Js::OpCode::SUB, r1, r1, src2, m_func);
  2600. instr->InsertBefore(subInstr);
  2601. Legalize(subInstr); // src2 may need legalizing
  2602. // NEG r1
  2603. instr->InsertBefore(IR::Instr::New(Js::OpCode::NEG, r1, r1, m_func));
  2604. // SBB r1, r1
  2605. instr->InsertBefore(IR::Instr::New(Js::OpCode::SBB, r1, r1, r1, m_func));
  2606. }
  2607. else
  2608. {
  2609. IR::Instr *instrNew;
  2610. IR::RegOpnd *r2 = IR::RegOpnd::New(TyMachPtr, this->m_func);
  2611. // MOV r2, 0
  2612. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, r2, IR::IntConstOpnd::New(0, TyMachReg, this->m_func), m_func));
  2613. // CMP r1, src2
  2614. IR::Opnd *r1_32 = r1->UseWithNewType(TyInt32, this->m_func);
  2615. IR::Opnd *src2_32 =src2->UseWithNewType(TyInt32, this->m_func);
  2616. instrNew = IR::Instr::New(Js::OpCode::CMP, m_func);
  2617. instrNew->SetSrc1(r1_32);
  2618. instrNew->SetSrc2(src2_32);
  2619. instr->InsertBefore(instrNew);
  2620. // SETcc r2
  2621. IR::RegOpnd *r2_i8 = (IR::RegOpnd*) r2->UseWithNewType(TyInt8, this->m_func);
  2622. instrNew = IR::Instr::New(setCC_Opcode, r2_i8, r2_i8, m_func);
  2623. instr->InsertBefore(instrNew);
  2624. // DEC r2
  2625. instr->InsertBefore(IR::Instr::New(Js::OpCode::DEC, r2, r2, m_func));
  2626. // r1 <- r2
  2627. r1 = r2;
  2628. }
  2629. // AND r1, (notEqualResult - equalResult)
  2630. {
  2631. IR::Instr * andInstr = IR::Instr::New(Js::OpCode::AND, r1, r1, m_func);
  2632. andInstr->SetSrc2(IR::AddrOpnd::New((void*)((size_t)notEqualResult - (size_t)equalResult), IR::AddrOpndKind::AddrOpndKindDynamicMisc, this->m_func));
  2633. instr->InsertBefore(andInstr);
  2634. Legalize(andInstr);
  2635. }
  2636. // ADD r1, equalResult
  2637. {
  2638. IR::Instr * add = IR::Instr::New(Js::OpCode::ADD, r1, r1, m_func);
  2639. add->SetSrc2(IR::AddrOpnd::New(equalResult, IR::AddrOpndKind::AddrOpndKindDynamicVar, this->m_func));
  2640. instr->InsertBefore(add);
  2641. Legalize(add);
  2642. }
  2643. // MOV dst, r1
  2644. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, dst, r1, m_func));
  2645. if (isTaggedInts)
  2646. {
  2647. instr->Remove();
  2648. return true;
  2649. }
  2650. // JMP $fallthru
  2651. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JMP, fallthru, m_func));
  2652. instr->InsertBefore(helper);
  2653. instr->InsertAfter(fallthru);
  2654. return false;
  2655. }
  2656. void LowererMD::GenerateFastCmXxR8(IR::Instr *instr)
  2657. {
  2658. GenerateFastCmXx(instr);
  2659. }
  2660. void LowererMD::GenerateFastCmXxI4(IR::Instr *instr)
  2661. {
  2662. GenerateFastCmXx(instr);
  2663. }
  2664. void LowererMD::GenerateFastCmXx(IR::Instr *instr)
  2665. {
  2666. // For float src:
  2667. // dst = MOV 0/1
  2668. // (U)COMISD src1, src2
  2669. // JP $done
  2670. // dst.i8 = SetCC dst.i8
  2671. // $done:
  2672. // for int src:
  2673. // CMP src1, src2
  2674. // dst = MOV 0 / false
  2675. // dst.i8 = SetCC dst.i8 / CMOCcc true
  2676. IR::Opnd * src1 = instr->UnlinkSrc1();
  2677. IR::Opnd * src2 = instr->UnlinkSrc2();
  2678. IR::Opnd * dst = instr->UnlinkDst();
  2679. IR::Opnd * tmp = dst;
  2680. bool isIntDst = dst->AsRegOpnd()->m_sym->IsInt32();
  2681. bool isFloatSrc = src1->IsFloat();
  2682. bool isInt64Src = src1->IsInt64();
  2683. Assert(!isFloatSrc || src2->IsFloat());
  2684. Assert(!isFloatSrc || isIntDst);
  2685. Assert(!isInt64Src || src2->IsInt64());
  2686. Assert(!isInt64Src || isIntDst);
  2687. Assert(!isFloatSrc || AutoSystemInfo::Data.SSE2Available());
  2688. IR::Opnd *opnd;
  2689. IR::Instr *newInstr;
  2690. Assert(src1->IsRegOpnd());
  2691. #ifndef _M_X64
  2692. Int64RegPair src1Pair, src2Pair;
  2693. if (isInt64Src)
  2694. {
  2695. src1Pair = this->m_lowerer->FindOrCreateInt64Pair(src1);
  2696. src2Pair = this->m_lowerer->FindOrCreateInt64Pair(src2);
  2697. src1 = src1Pair.high;
  2698. src2 = src2Pair.high;
  2699. }
  2700. #endif
  2701. IR::Instr * done;
  2702. if (isFloatSrc)
  2703. {
  2704. done = IR::LabelInstr::New(Js::OpCode::Label, m_func);
  2705. instr->InsertBefore(done);
  2706. }
  2707. else
  2708. {
  2709. done = instr;
  2710. }
  2711. if (isIntDst)
  2712. {
  2713. // reg = MOV 0 will get peeped to XOR reg, reg which sets the flags.
  2714. // Put the MOV before the CMP, but use a tmp if dst == src1/src2
  2715. if (dst->IsEqual(src1) || dst->IsEqual(src2))
  2716. {
  2717. tmp = IR::RegOpnd::New(dst->GetType(), this->m_func);
  2718. }
  2719. // dst = MOV 0
  2720. if (isFloatSrc && instr->m_opcode == Js::OpCode::CmNeq_A)
  2721. {
  2722. opnd = IR::IntConstOpnd::New(1, TyInt32, this->m_func);
  2723. }
  2724. else
  2725. {
  2726. opnd = IR::IntConstOpnd::New(0, TyInt32, this->m_func);
  2727. }
  2728. newInstr = IR::Instr::New(Js::OpCode::MOV, tmp, opnd, this->m_func);
  2729. done->InsertBefore(newInstr);
  2730. }
  2731. Js::OpCode cmpOp;
  2732. if (isFloatSrc)
  2733. {
  2734. if (instr->m_opcode == Js::OpCode::CmEq_A || instr->m_opcode == Js::OpCode::CmNeq_A)
  2735. {
  2736. cmpOp = src1->IsFloat64() ? Js::OpCode::UCOMISD : Js::OpCode::UCOMISS;
  2737. }
  2738. else
  2739. {
  2740. cmpOp = src1->IsFloat64() ? Js::OpCode::COMISD : Js::OpCode::COMISS;
  2741. }
  2742. }
  2743. else
  2744. {
  2745. cmpOp = Js::OpCode::CMP;
  2746. }
  2747. // CMP src1, src2
  2748. newInstr = IR::Instr::New(cmpOp, this->m_func);
  2749. newInstr->SetSrc1(src1);
  2750. newInstr->SetSrc2(src2);
  2751. done->InsertBefore(newInstr);
  2752. if (isFloatSrc)
  2753. {
  2754. newInstr = IR::BranchInstr::New(Js::OpCode::JP, done->AsLabelInstr(), this->m_func);
  2755. done->InsertBefore(newInstr);
  2756. }
  2757. #ifndef _M_X64
  2758. if (isInt64Src)
  2759. {
  2760. IR::LabelInstr* skipLow = IR::LabelInstr::New(Js::OpCode::Label, m_func);
  2761. newInstr = IR::BranchInstr::New(Js::OpCode::JNE, skipLow, this->m_func);
  2762. done->InsertBefore(newInstr);
  2763. newInstr = IR::Instr::New(cmpOp, this->m_func);
  2764. newInstr->SetSrc1(src1Pair.low);
  2765. newInstr->SetSrc2(src2Pair.low);
  2766. done->InsertBefore(newInstr);
  2767. done->InsertBefore(skipLow);
  2768. }
  2769. #endif
  2770. if (!isIntDst)
  2771. {
  2772. opnd = this->m_lowerer->LoadLibraryValueOpnd(instr, LibraryValue::ValueFalse);
  2773. LowererMD::CreateAssign(tmp, opnd, done);
  2774. }
  2775. Js::OpCode useCC;
  2776. switch(instr->m_opcode)
  2777. {
  2778. case Js::OpCode::CmEq_I4:
  2779. case Js::OpCode::CmEq_A:
  2780. useCC = isIntDst ? Js::OpCode::SETE : Js::OpCode::CMOVE;
  2781. break;
  2782. case Js::OpCode::CmNeq_I4:
  2783. case Js::OpCode::CmNeq_A:
  2784. useCC = isIntDst ? Js::OpCode::SETNE : Js::OpCode::CMOVNE;
  2785. break;
  2786. case Js::OpCode::CmGe_I4:
  2787. useCC = isIntDst ? Js::OpCode::SETGE : Js::OpCode::CMOVGE;
  2788. break;
  2789. case Js::OpCode::CmGt_I4:
  2790. useCC = isIntDst ? Js::OpCode::SETG : Js::OpCode::CMOVG;
  2791. break;
  2792. case Js::OpCode::CmLe_I4:
  2793. useCC = isIntDst ? Js::OpCode::SETLE : Js::OpCode::CMOVLE;
  2794. break;
  2795. case Js::OpCode::CmLt_I4:
  2796. useCC = isIntDst ? Js::OpCode::SETL : Js::OpCode::CMOVL;
  2797. break;
  2798. case Js::OpCode::CmUnGe_I4:
  2799. case Js::OpCode::CmGe_A:
  2800. useCC = isIntDst ? Js::OpCode::SETAE : Js::OpCode::CMOVAE;
  2801. break;
  2802. case Js::OpCode::CmUnGt_I4:
  2803. case Js::OpCode::CmGt_A:
  2804. useCC = isIntDst ? Js::OpCode::SETA : Js::OpCode::CMOVA;
  2805. break;
  2806. case Js::OpCode::CmUnLe_I4:
  2807. case Js::OpCode::CmLe_A:
  2808. useCC = isIntDst ? Js::OpCode::SETBE : Js::OpCode::CMOVBE;
  2809. break;
  2810. case Js::OpCode::CmUnLt_I4:
  2811. case Js::OpCode::CmLt_A:
  2812. useCC = isIntDst ? Js::OpCode::SETB : Js::OpCode::CMOVB;
  2813. break;
  2814. default:
  2815. useCC = Js::OpCode::InvalidOpCode;
  2816. Assume(UNREACHED);
  2817. }
  2818. if (isIntDst)
  2819. {
  2820. // tmp.i8 = SetCC tmp.i8
  2821. IR::Opnd *tmp_i8 = tmp->UseWithNewType(TyInt8, this->m_func);
  2822. newInstr = IR::Instr::New(useCC, tmp_i8, tmp_i8, this->m_func);
  2823. }
  2824. else
  2825. {
  2826. // regTrue = MOV true
  2827. IR::Opnd *regTrue = IR::RegOpnd::New(TyMachPtr, this->m_func);
  2828. Lowerer::InsertMove(regTrue, this->m_lowerer->LoadLibraryValueOpnd(instr, LibraryValue::ValueTrue), done);
  2829. // tmp = CMOVcc tmp, regTrue
  2830. newInstr = IR::Instr::New(useCC, tmp, tmp, regTrue, this->m_func);
  2831. }
  2832. done->InsertBefore(newInstr);
  2833. if (tmp != dst)
  2834. {
  2835. newInstr = IR::Instr::New(Js::OpCode::MOV, dst, tmp, this->m_func);
  2836. instr->InsertBefore(newInstr);
  2837. }
  2838. instr->Remove();
  2839. }
  2840. IR::Instr * LowererMD::GenerateConvBool(IR::Instr *instr)
  2841. {
  2842. // TEST src1, src1
  2843. // dst = MOV true
  2844. // rf = MOV false
  2845. // dst = CMOV dst, rf
  2846. IR::Instr *instrNew, *instrFirst;
  2847. IR::RegOpnd *dst = instr->GetDst()->AsRegOpnd();
  2848. IR::RegOpnd *regFalse;
  2849. // TEST src1, src2
  2850. instrFirst = instrNew = IR::Instr::New(Js::OpCode::TEST, this->m_func);
  2851. instrNew->SetSrc1(instr->GetSrc1());
  2852. instrNew->SetSrc2(instr->GetSrc1());
  2853. instr->InsertBefore(instrNew);
  2854. // dst = MOV true
  2855. Lowerer::InsertMove(dst, this->m_lowerer->LoadLibraryValueOpnd(instr, LibraryValue::ValueTrue), instr);
  2856. // rf = MOV false
  2857. regFalse = IR::RegOpnd::New(TyMachPtr, this->m_func);
  2858. Lowerer::InsertMove(regFalse, this->m_lowerer->LoadLibraryValueOpnd(instr, LibraryValue::ValueFalse), instr);
  2859. // Add dst as src1 of CMOV to create a pseudo use of dst. Otherwise, the register allocator
  2860. // won't know the previous dst is needed. and needed in the same register as the dst of the CMOV.
  2861. // dst = CMOV dst, rf
  2862. instrNew = IR::Instr::New(Js::OpCode::CMOVE, dst, dst, regFalse, this->m_func);
  2863. instr->InsertBefore(instrNew);
  2864. instr->Remove();
  2865. return instrFirst;
  2866. }
  2867. ///----------------------------------------------------------------------------
  2868. ///
  2869. /// LowererMD::GenerateFastAdd
  2870. ///
  2871. /// NOTE: We assume that only the sum of two Int31's will have 0x2 set. This
  2872. /// is only true until we have a var type with tag == 0x2.
  2873. ///
  2874. ///----------------------------------------------------------------------------
  2875. bool
  2876. LowererMD::GenerateFastAdd(IR::Instr * instrAdd)
  2877. {
  2878. // Given:
  2879. //
  2880. // dst = Add src1, src2
  2881. //
  2882. // Generate:
  2883. //
  2884. // (If not 2 Int31's, jump to $helper.)
  2885. // s1 = MOV src1
  2886. // s1 = DEC s1 -- Get rid of one of the tag [Int31 only]
  2887. // s1 = ADD s1, src2 -- try an inline add
  2888. // JO $helper -- bail if the add overflowed
  2889. // s1 = OR s1, AtomTag_IntPtr [Int32 only]
  2890. // dst = MOV s1
  2891. // JMP $fallthru
  2892. // $helper:
  2893. // (caller generates helper call)
  2894. // $fallthru:
  2895. IR::Instr * instr;
  2896. IR::LabelInstr * labelHelper;
  2897. IR::LabelInstr * labelFallThru;
  2898. IR::Opnd * opndReg;
  2899. IR::Opnd * opndSrc1;
  2900. IR::Opnd * opndSrc2;
  2901. opndSrc1 = instrAdd->GetSrc1();
  2902. opndSrc2 = instrAdd->GetSrc2();
  2903. AssertMsg(opndSrc1 && opndSrc2, "Expected 2 src opnd's on Add instruction");
  2904. // Generate fastpath for Incr_A anyway -
  2905. // Incrementing strings representing integers can be inter-mixed with integers e.g. "1"++ -> converts 1 to an int and thereafter, integer increment is expected.
  2906. if (opndSrc1->IsRegOpnd() && (opndSrc1->AsRegOpnd()->IsNotInt() || opndSrc1->GetValueType().IsString()
  2907. || (instrAdd->m_opcode != Js::OpCode::Incr_A && opndSrc1->GetValueType().IsLikelyString())))
  2908. {
  2909. return false;
  2910. }
  2911. if (opndSrc2->IsRegOpnd() && (opndSrc2->AsRegOpnd()->IsNotInt() ||
  2912. opndSrc2->GetValueType().IsLikelyString()))
  2913. {
  2914. return false;
  2915. }
  2916. // Tagged ints?
  2917. bool isTaggedInts = false;
  2918. if (opndSrc1->IsTaggedInt())
  2919. {
  2920. if (opndSrc2->IsTaggedInt())
  2921. {
  2922. isTaggedInts = true;
  2923. }
  2924. }
  2925. labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  2926. if (!isTaggedInts)
  2927. {
  2928. // (If not 2 Int31's, jump to $helper.)
  2929. this->GenerateSmIntPairTest(instrAdd, opndSrc1, opndSrc2, labelHelper);
  2930. }
  2931. if (opndSrc1->IsAddrOpnd())
  2932. {
  2933. // If opnd1 is a constant, just swap them.
  2934. IR::Opnd *opndTmp = opndSrc1;
  2935. opndSrc1 = opndSrc2;
  2936. opndSrc2 = opndTmp;
  2937. }
  2938. //
  2939. // For 32 bit arithmetic we copy them and set the size of operands to be 32 bits. This is
  2940. // relevant only on AMD64.
  2941. //
  2942. opndSrc1 = opndSrc1->UseWithNewType(TyInt32, this->m_func);
  2943. // s1 = MOV src1
  2944. opndReg = IR::RegOpnd::New(TyInt32, this->m_func);
  2945. instr = IR::Instr::New(Js::OpCode::MOV, opndReg, opndSrc1, this->m_func);
  2946. instrAdd->InsertBefore(instr);
  2947. #if !INT32VAR
  2948. // Do the DEC in place
  2949. if (opndSrc2->IsAddrOpnd())
  2950. {
  2951. Assert(opndSrc2->AsAddrOpnd()->GetAddrOpndKind() == IR::AddrOpndKindConstantVar);
  2952. opndSrc2 = IR::IntConstOpnd::New(*((int *)&(opndSrc2->AsAddrOpnd()->m_address)) - 1, TyInt32, this->m_func, opndSrc2->AsAddrOpnd()->m_dontEncode);
  2953. opndSrc2 = opndSrc2->Use(this->m_func);
  2954. }
  2955. else if (opndSrc2->IsIntConstOpnd())
  2956. {
  2957. Assert(opndSrc2->GetType() == TyInt32);
  2958. opndSrc2 = opndSrc2->Use(this->m_func);
  2959. opndSrc2->AsIntConstOpnd()->DecrValue(1);
  2960. }
  2961. else
  2962. {
  2963. // s1 = DEC s1
  2964. opndSrc2 = opndSrc2->UseWithNewType(TyInt32, this->m_func);
  2965. instr = IR::Instr::New(Js::OpCode::DEC, opndReg, opndReg, this->m_func);
  2966. instrAdd->InsertBefore(instr);
  2967. }
  2968. instr = IR::Instr::New(Js::OpCode::ADD, opndReg, opndReg, opndSrc2, this->m_func);
  2969. #else
  2970. if (opndSrc2->IsAddrOpnd())
  2971. {
  2972. // truncate to untag
  2973. int value = ::Math::PointerCastToIntegralTruncate<int>(opndSrc2->AsAddrOpnd()->m_address);
  2974. if (value == 1)
  2975. {
  2976. instr = IR::Instr::New(Js::OpCode::INC, opndReg, opndReg, this->m_func);
  2977. }
  2978. else
  2979. {
  2980. opndSrc2 = IR::IntConstOpnd::New(value, TyInt32, this->m_func);
  2981. instr = IR::Instr::New(Js::OpCode::ADD, opndReg, opndReg, opndSrc2, this->m_func);
  2982. }
  2983. }
  2984. else
  2985. {
  2986. instr = IR::Instr::New(Js::OpCode::ADD, opndReg, opndReg, opndSrc2->UseWithNewType(TyInt32, this->m_func), this->m_func);
  2987. }
  2988. #endif
  2989. // s1 = ADD s1, src2
  2990. instrAdd->InsertBefore(instr);
  2991. Legalize(instr);
  2992. // JO $helper
  2993. instr = IR::BranchInstr::New(Js::OpCode::JO, labelHelper, this->m_func);
  2994. instrAdd->InsertBefore(instr);
  2995. //
  2996. // Convert TyInt32 operand, back to TyMachPtr type.
  2997. //
  2998. if(TyMachReg != opndReg->GetType())
  2999. {
  3000. opndReg = opndReg->UseWithNewType(TyMachPtr, this->m_func);
  3001. }
  3002. #if INT32VAR
  3003. // s1 = OR s1, AtomTag_IntPtr
  3004. GenerateInt32ToVarConversion(opndReg, instrAdd);
  3005. #endif
  3006. // dst = MOV s1
  3007. instr = IR::Instr::New(Js::OpCode::MOV, instrAdd->GetDst(), opndReg, this->m_func);
  3008. instrAdd->InsertBefore(instr);
  3009. // JMP $fallthru
  3010. labelFallThru = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  3011. instr = IR::BranchInstr::New(Js::OpCode::JMP, labelFallThru, this->m_func);
  3012. instrAdd->InsertBefore(instr);
  3013. // $helper:
  3014. // (caller generates helper call)
  3015. // $fallthru:
  3016. instrAdd->InsertBefore(labelHelper);
  3017. instrAdd->InsertAfter(labelFallThru);
  3018. return true;
  3019. }
  3020. ///----------------------------------------------------------------------------
  3021. ///
  3022. /// LowererMD::GenerateFastSub
  3023. ///
  3024. ///
  3025. ///----------------------------------------------------------------------------
  3026. bool
  3027. LowererMD::GenerateFastSub(IR::Instr * instrSub)
  3028. {
  3029. // Given:
  3030. //
  3031. // dst = Sub src1, src2
  3032. //
  3033. // Generate:
  3034. //
  3035. // (If not 2 Int31's, jump to $helper.)
  3036. // s1 = MOV src1
  3037. // s1 = SUB s1, src2 -- try an inline sub
  3038. // JO $helper -- bail if the subtract overflowed
  3039. // JNE $helper
  3040. // s1 = INC s1 -- restore the var tag on the result [Int31 only]
  3041. // s1 = OR s1, AtomTag_IntPtr [Int32 only]
  3042. // dst = MOV s1
  3043. // JMP $fallthru
  3044. // $helper:
  3045. // (caller generates helper call)
  3046. // $fallthru:
  3047. IR::Instr * instr;
  3048. IR::LabelInstr * labelHelper;
  3049. IR::LabelInstr * labelFallThru;
  3050. IR::Opnd * opndReg;
  3051. IR::Opnd * opndSrc1;
  3052. IR::Opnd * opndSrc2;
  3053. opndSrc1 = instrSub->GetSrc1();
  3054. opndSrc2 = instrSub->GetSrc2();
  3055. AssertMsg(opndSrc1 && opndSrc2, "Expected 2 src opnd's on Sub instruction");
  3056. // Not tagged ints?
  3057. if (opndSrc1->IsRegOpnd() && opndSrc1->AsRegOpnd()->IsNotInt())
  3058. {
  3059. return false;
  3060. }
  3061. if (opndSrc2->IsRegOpnd() && opndSrc2->AsRegOpnd()->IsNotInt())
  3062. {
  3063. return false;
  3064. }
  3065. // Tagged ints?
  3066. bool isTaggedInts = false;
  3067. if (opndSrc1->IsTaggedInt())
  3068. {
  3069. if (opndSrc2->IsTaggedInt())
  3070. {
  3071. isTaggedInts = true;
  3072. }
  3073. }
  3074. labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  3075. if (!isTaggedInts)
  3076. {
  3077. // (If not 2 Int31's, jump to $helper.)
  3078. this->GenerateSmIntPairTest(instrSub, opndSrc1, opndSrc2, labelHelper);
  3079. }
  3080. //
  3081. // For 32 bit arithmetic we copy them and set the size of operands to be 32 bits. This is
  3082. // relevant only on AMD64.
  3083. //
  3084. opndSrc1 = opndSrc1->UseWithNewType(TyInt32, this->m_func);
  3085. opndSrc2 = opndSrc2->UseWithNewType(TyInt32, this->m_func);
  3086. // s1 = MOV src1
  3087. opndReg = IR::RegOpnd::New(TyInt32, this->m_func);
  3088. instr = IR::Instr::New(Js::OpCode::MOV, opndReg, opndSrc1, this->m_func);
  3089. instrSub->InsertBefore(instr);
  3090. // s1 = SUB s1, src2
  3091. instr = IR::Instr::New(Js::OpCode::SUB, opndReg, opndReg, opndSrc2, this->m_func);
  3092. instrSub->InsertBefore(instr);
  3093. // JO $helper
  3094. instr = IR::BranchInstr::New(Js::OpCode::JO, labelHelper, this->m_func);
  3095. instrSub->InsertBefore(instr);
  3096. #if !INT32VAR
  3097. // s1 = INC s1
  3098. instr = IR::Instr::New(Js::OpCode::INC, opndReg, opndReg, this->m_func);
  3099. instrSub->InsertBefore(instr);
  3100. #endif
  3101. //
  3102. // Convert TyInt32 operand, back to TyMachPtr type.
  3103. //
  3104. if(TyMachReg != opndReg->GetType())
  3105. {
  3106. opndReg = opndReg->UseWithNewType(TyMachPtr, this->m_func);
  3107. }
  3108. #if INT32VAR
  3109. // s1 = OR s1, AtomTag_IntPtr
  3110. GenerateInt32ToVarConversion(opndReg, instrSub);
  3111. #endif
  3112. // dst = MOV s1
  3113. instr = IR::Instr::New(Js::OpCode::MOV, instrSub->GetDst(), opndReg, this->m_func);
  3114. instrSub->InsertBefore(instr);
  3115. // JMP $fallthru
  3116. labelFallThru = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  3117. instr = IR::BranchInstr::New(Js::OpCode::JMP, labelFallThru, this->m_func);
  3118. instrSub->InsertBefore(instr);
  3119. // $helper:
  3120. // (caller generates helper call)
  3121. // $fallthru:
  3122. instrSub->InsertBefore(labelHelper);
  3123. instrSub->InsertAfter(labelFallThru);
  3124. return true;
  3125. }
  3126. ///----------------------------------------------------------------------------
  3127. ///
  3128. /// LowererMD::GenerateFastMul
  3129. ///
  3130. ///----------------------------------------------------------------------------
  3131. bool
  3132. LowererMD::GenerateFastMul(IR::Instr * instrMul)
  3133. {
  3134. // Given:
  3135. //
  3136. // dst = Mul src1, src2
  3137. //
  3138. // Generate:
  3139. //
  3140. // (If not 2 Int31's, jump to $helper.)
  3141. // s1 = MOV src1
  3142. // s1 = DEC s1 -- clear the var tag from the value to be multiplied [Int31 only]
  3143. // s2 = MOV src2
  3144. // s2 = SAR s2, Js::VarTag_Shift -- extract the real src2 amount from the var [Int31 only]
  3145. // s1 = IMUL s1, s2 -- do the signed mul
  3146. // JO $helper -- bail if the result overflowed
  3147. // s3 = MOV s1
  3148. // TEST s3, s3 -- Check result is 0. might be -0. Result is -0 when a negative number is multiplied with 0.
  3149. // JEQ $zero
  3150. // JMP $nonzero
  3151. // $zero: -- result of mul was 0. try to check for -0
  3152. // s2 = ADD s2, src1 -- Add src1 to s2
  3153. // JGT $nonzero -- positive 0. [Int31 only]
  3154. // JGE $nonzero -- positive 0. [Int32 only]
  3155. // dst = ToVar(-0.0) -- load negative 0
  3156. // JMP $fallthru
  3157. // $nonzero:
  3158. // s3 = INC s3 -- restore the var tag on the result [Int31 only]
  3159. // s3 = OR s3, AtomTag_IntPtr [Int32 only]
  3160. // dst= MOV s3
  3161. // JMP $fallthru
  3162. // $helper:
  3163. // (caller generates helper call)
  3164. // $fallthru:
  3165. IR::LabelInstr * labelHelper;
  3166. IR::LabelInstr * labelFallThru;
  3167. IR::LabelInstr * labelNonZero;
  3168. IR::Instr * instr;
  3169. IR::RegOpnd * opndReg1;
  3170. IR::RegOpnd * opndReg2;
  3171. IR::RegOpnd * s3;
  3172. IR::Opnd * opndSrc1;
  3173. IR::Opnd * opndSrc2;
  3174. opndSrc1 = instrMul->GetSrc1();
  3175. opndSrc2 = instrMul->GetSrc2();
  3176. AssertMsg(opndSrc1 && opndSrc2, "Expected 2 src opnd's on mul instruction");
  3177. if (opndSrc1->IsRegOpnd() && opndSrc1->AsRegOpnd()->IsNotInt())
  3178. {
  3179. return true;
  3180. }
  3181. if (opndSrc2->IsRegOpnd() && opndSrc2->AsRegOpnd()->IsNotInt())
  3182. {
  3183. return true;
  3184. }
  3185. // (If not 2 Int31's, jump to $helper.)
  3186. labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  3187. labelNonZero = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  3188. labelFallThru = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  3189. this->GenerateSmIntPairTest(instrMul, opndSrc1, opndSrc2, labelHelper);
  3190. //
  3191. // For 32 bit arithmetic we copy them and set the size of operands to be 32 bits. This is
  3192. // relevant only on AMD64.
  3193. //
  3194. opndSrc1 = opndSrc1->UseWithNewType(TyInt32, this->m_func);
  3195. opndSrc2 = opndSrc2->UseWithNewType(TyInt32, this->m_func);
  3196. if (opndSrc1->IsImmediateOpnd())
  3197. {
  3198. IR::Opnd * temp = opndSrc1;
  3199. opndSrc1 = opndSrc2;
  3200. opndSrc2 = temp;
  3201. }
  3202. // s1 = MOV src1
  3203. opndReg1 = IR::RegOpnd::New(TyInt32, this->m_func);
  3204. instr = IR::Instr::New(Js::OpCode::MOV, opndReg1, opndSrc1, this->m_func);
  3205. instrMul->InsertBefore(instr);
  3206. #if !INT32VAR
  3207. // s1 = DEC s1
  3208. instr = IR::Instr::New(Js::OpCode::DEC, opndReg1, opndReg1, this->m_func);
  3209. instrMul->InsertBefore(instr);
  3210. #endif
  3211. if (opndSrc2->IsImmediateOpnd())
  3212. {
  3213. Assert(opndSrc2->IsAddrOpnd() && opndSrc2->AsAddrOpnd()->IsVar());
  3214. IR::Opnd *opnd2 = IR::IntConstOpnd::New(Js::TaggedInt::ToInt32(opndSrc2->AsAddrOpnd()->m_address), TyInt32, this->m_func);
  3215. // s2 = MOV src2
  3216. opndReg2 = IR::RegOpnd::New(TyInt32, this->m_func);
  3217. instr = IR::Instr::New(Js::OpCode::MOV, opndReg2, opnd2, this->m_func);
  3218. instrMul->InsertBefore(instr);
  3219. }
  3220. else
  3221. {
  3222. // s2 = MOV src2
  3223. opndReg2 = IR::RegOpnd::New(TyInt32, this->m_func);
  3224. instr = IR::Instr::New(Js::OpCode::MOV, opndReg2, opndSrc2, this->m_func);
  3225. instrMul->InsertBefore(instr);
  3226. #if !INT32VAR
  3227. // s2 = SAR s2, Js::VarTag_Shift
  3228. instr = IR::Instr::New(
  3229. Js::OpCode::SAR, opndReg2, opndReg2,
  3230. IR::IntConstOpnd::New(Js::VarTag_Shift, TyInt8, this->m_func), this->m_func);
  3231. instrMul->InsertBefore(instr);
  3232. #endif
  3233. }
  3234. // s1 = IMUL s1, s2
  3235. instr = IR::Instr::New(Js::OpCode::IMUL2, opndReg1, opndReg1, opndReg2, this->m_func);
  3236. instrMul->InsertBefore(instr);
  3237. // JO $helper
  3238. instr = IR::BranchInstr::New(Js::OpCode::JO, labelHelper, this->m_func);
  3239. instrMul->InsertBefore(instr);
  3240. // MOV s3, s1
  3241. s3 = IR::RegOpnd::New(TyInt32, this->m_func);
  3242. instr = IR::Instr::New(Js::OpCode::MOV, s3, opndReg1, this->m_func);
  3243. instrMul->InsertBefore(instr);
  3244. // TEST s3, s3
  3245. instr = IR::Instr::New(Js::OpCode::TEST, this->m_func);
  3246. instr->SetSrc1(s3);
  3247. instr->SetSrc2(s3);
  3248. instrMul->InsertBefore(instr);
  3249. // JEQ $zero
  3250. IR::LabelInstr *labelZero = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  3251. instr = IR::BranchInstr::New(Js::OpCode::JEQ, labelZero, this->m_func);
  3252. instrMul->InsertBefore(instr);
  3253. // JMP $nonzero
  3254. instr = IR::BranchInstr::New(Js::OpCode::JMP, labelNonZero, this->m_func);
  3255. instrMul->InsertBefore(instr);
  3256. // $zero:
  3257. instrMul->InsertBefore(labelZero);
  3258. // s2 = ADD s2, src1
  3259. instr = IR::Instr::New(Js::OpCode::ADD, opndReg2, opndReg2, opndSrc1, this->m_func);
  3260. instrMul->InsertBefore(instr);
  3261. Legalize(instr);
  3262. // JGT $nonzero
  3263. #if INT32VAR
  3264. Js::OpCode greaterOpCode = Js::OpCode::JGE;
  3265. #else
  3266. Js::OpCode greaterOpCode = Js::OpCode::JGT;
  3267. #endif
  3268. instr = IR::BranchInstr::New(greaterOpCode, labelNonZero, this->m_func);
  3269. instrMul->InsertBefore(instr);
  3270. // dst = ToVar(-0.0) -- load negative 0
  3271. instr = IR::Instr::New(Js::OpCode::MOV, instrMul->GetDst(), m_lowerer->LoadLibraryValueOpnd(instrMul, LibraryValue::ValueNegativeZero), this->m_func);
  3272. instrMul->InsertBefore(instr);
  3273. // JMP $fallthru
  3274. instr = IR::BranchInstr::New(Js::OpCode::JMP, labelFallThru, this->m_func);
  3275. instrMul->InsertBefore(instr);
  3276. // $nonzero:
  3277. instrMul->InsertBefore(labelNonZero);
  3278. #if !INT32VAR
  3279. // s3 = INC s3
  3280. instr = IR::Instr::New(Js::OpCode::INC, s3, s3, this->m_func);
  3281. instrMul->InsertBefore(instr);
  3282. #endif
  3283. //
  3284. // Convert TyInt32 operand, back to TyMachPtr type.
  3285. // Cast is fine. We know ChangeType returns IR::Opnd * but it
  3286. // preserves the Type.
  3287. //
  3288. if(TyMachReg != s3->GetType())
  3289. {
  3290. s3 = static_cast<IR::RegOpnd *>(s3->UseWithNewType(TyMachPtr, this->m_func));
  3291. }
  3292. #if INT32VAR
  3293. // s3 = OR s3, AtomTag_IntPtr
  3294. GenerateInt32ToVarConversion(s3, instrMul);
  3295. #endif
  3296. // dst = MOV s3
  3297. instr = IR::Instr::New(Js::OpCode::MOV, instrMul->GetDst(), s3, this->m_func);
  3298. instrMul->InsertBefore(instr);
  3299. // JMP $fallthru
  3300. instr = IR::BranchInstr::New(Js::OpCode::JMP, labelFallThru, this->m_func);
  3301. instrMul->InsertBefore(instr);
  3302. // $helper:
  3303. // (caller generates helper call)
  3304. // $fallthru:
  3305. instrMul->InsertBefore(labelHelper);
  3306. instrMul->InsertAfter(labelFallThru);
  3307. return true;
  3308. }
  3309. bool
  3310. LowererMD::GenerateFastNeg(IR::Instr * instrNeg)
  3311. {
  3312. // Given:
  3313. //
  3314. // dst = Not src
  3315. //
  3316. // Generate:
  3317. //
  3318. // if not int, jump $helper
  3319. // if src == 0 -- test for zero (must be handled by the runtime to preserve
  3320. // JEQ $helper difference btw +0 and -0)
  3321. // dst = MOV src
  3322. // dst = NEG dst -- do an inline NEG
  3323. // dst = ADD dst, 2 -- restore the var tag on the result [int31 only]
  3324. // JO $helper
  3325. // dst = OR dst, AtomTag_Ptr [int32 only]
  3326. // JMP $fallthru
  3327. // $helper:
  3328. // (caller generates helper call)
  3329. // $fallthru:
  3330. IR::Instr * instr;
  3331. IR::LabelInstr * labelHelper = nullptr;
  3332. IR::LabelInstr * labelFallThru = nullptr;
  3333. IR::Opnd * opndSrc1;
  3334. IR::Opnd * opndDst;
  3335. bool usingNewDst = false;
  3336. opndSrc1 = instrNeg->GetSrc1();
  3337. AssertMsg(opndSrc1, "Expected src opnd on Neg instruction");
  3338. if(opndSrc1->IsEqual(instrNeg->GetDst()))
  3339. {
  3340. usingNewDst = true;
  3341. opndDst = IR::RegOpnd::New(TyInt32, this->m_func);
  3342. }
  3343. else
  3344. {
  3345. opndDst = instrNeg->GetDst()->UseWithNewType(TyInt32, this->m_func);
  3346. }
  3347. if (opndSrc1->IsRegOpnd() && opndSrc1->AsRegOpnd()->m_sym->IsIntConst())
  3348. {
  3349. IR::Opnd *newOpnd;
  3350. IntConstType value = opndSrc1->AsRegOpnd()->m_sym->GetIntConstValue();
  3351. if (value == 0)
  3352. {
  3353. // If the negate operand is zero, the result is -0.0, which is a Number rather than an Int31.
  3354. newOpnd = m_lowerer->LoadLibraryValueOpnd(instrNeg, LibraryValue::ValueNegativeZero);
  3355. }
  3356. else
  3357. {
  3358. // negation below can overflow because max negative int32 value > max positive value by 1.
  3359. newOpnd = IR::AddrOpnd::NewFromNumber(-(int64)value, m_func);
  3360. }
  3361. instrNeg->ClearBailOutInfo();
  3362. instrNeg->FreeSrc1();
  3363. instrNeg->SetSrc1(newOpnd);
  3364. instrNeg = this->ChangeToAssign(instrNeg);
  3365. // Skip lowering call to helper
  3366. return false;
  3367. }
  3368. bool isInt = (opndSrc1->IsTaggedInt());
  3369. if (opndSrc1->IsRegOpnd() && opndSrc1->AsRegOpnd()->IsNotInt())
  3370. {
  3371. return true;
  3372. }
  3373. labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  3374. if (!isInt)
  3375. {
  3376. GenerateSmIntTest(opndSrc1, instrNeg, labelHelper);
  3377. }
  3378. //
  3379. // For 32 bit arithmetic we copy them and set the size of operands to be 32 bits. This is
  3380. // relevant only on AMD64.
  3381. //
  3382. opndSrc1 = opndSrc1->UseWithNewType(TyInt32, this->m_func);
  3383. GenerateTaggedZeroTest(opndSrc1, instrNeg, labelHelper);
  3384. // dst = MOV src
  3385. instr = IR::Instr::New(Js::OpCode::MOV, opndDst, opndSrc1, this->m_func);
  3386. instrNeg->InsertBefore(instr);
  3387. // dst = NEG dst
  3388. instr = IR::Instr::New(Js::OpCode::NEG, opndDst, opndDst, this->m_func);
  3389. instrNeg->InsertBefore(instr);
  3390. #if !INT32VAR
  3391. // dst = ADD dst, 2
  3392. instr = IR::Instr::New(Js::OpCode::ADD, opndDst, opndDst, IR::IntConstOpnd::New(2, TyInt32, this->m_func), this->m_func);
  3393. instrNeg->InsertBefore(instr);
  3394. #endif
  3395. // JO $helper
  3396. instr = IR::BranchInstr::New(Js::OpCode::JO, labelHelper, this->m_func);
  3397. instrNeg->InsertBefore(instr);
  3398. //
  3399. // Convert TyInt32 operand, back to TyMachPtr type.
  3400. //
  3401. if(TyMachReg != opndDst->GetType())
  3402. {
  3403. opndDst = opndDst->UseWithNewType(TyMachPtr, this->m_func);
  3404. }
  3405. #if INT32VAR
  3406. GenerateInt32ToVarConversion(opndDst, instrNeg);
  3407. #endif
  3408. if(usingNewDst)
  3409. {
  3410. instr = IR::Instr::New(Js::OpCode::MOV, instrNeg->GetDst(), opndDst, this->m_func);
  3411. instrNeg->InsertBefore(instr);
  3412. }
  3413. // JMP $fallthru
  3414. labelFallThru = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  3415. instr = IR::BranchInstr::New(Js::OpCode::JMP, labelFallThru, this->m_func);
  3416. instrNeg->InsertBefore(instr);
  3417. // $helper:
  3418. // (caller generates helper sequence)
  3419. // $fallthru:
  3420. AssertMsg(labelHelper, "Should not be NULL");
  3421. instrNeg->InsertBefore(labelHelper);
  3422. instrNeg->InsertAfter(labelFallThru);
  3423. return true;
  3424. }
  3425. void
  3426. LowererMD::GenerateFastBrS(IR::BranchInstr *brInstr)
  3427. {
  3428. IR::Opnd *src1 = brInstr->UnlinkSrc1();
  3429. Assert(src1->IsIntConstOpnd() || src1->IsAddrOpnd() || src1->IsRegOpnd());
  3430. IR::Instr *cmpInstr = IR::Instr::New(Js::OpCode::TEST, this->m_func);
  3431. cmpInstr->SetSrc1(m_lowerer->LoadOptimizationOverridesValueOpnd(brInstr, OptimizationOverridesValue::OptimizationOverridesSideEffects));
  3432. cmpInstr->SetSrc2(src1);
  3433. brInstr->InsertBefore(cmpInstr);
  3434. Legalize(cmpInstr);
  3435. Js::OpCode opcode;
  3436. switch(brInstr->m_opcode)
  3437. {
  3438. case Js::OpCode::BrHasSideEffects:
  3439. opcode = Js::OpCode::JNE;
  3440. break;
  3441. case Js::OpCode::BrNotHasSideEffects:
  3442. opcode = Js::OpCode::JEQ;
  3443. break;
  3444. default:
  3445. Assert(UNREACHED);
  3446. __assume(false);
  3447. }
  3448. brInstr->m_opcode = opcode;
  3449. }
  3450. ///----------------------------------------------------------------------------
  3451. ///
  3452. /// LowererMD::GenerateSmIntPairTest
  3453. ///
  3454. /// Generate code to test whether the given operands are both Int31 vars
  3455. /// and branch to the given label if not.
  3456. ///
  3457. ///----------------------------------------------------------------------------
  3458. #if !INT32VAR
  3459. IR::Instr *
  3460. LowererMD::GenerateSmIntPairTest(
  3461. IR::Instr * instrInsert,
  3462. IR::Opnd * opndSrc1,
  3463. IR::Opnd * opndSrc2,
  3464. IR::LabelInstr * labelFail)
  3465. {
  3466. IR::Opnd * opndReg;
  3467. IR::Instr * instrPrev = instrInsert->m_prev;
  3468. IR::Instr * instr;
  3469. Assert(opndSrc1->GetType() == TyVar);
  3470. Assert(opndSrc2->GetType() == TyVar);
  3471. if (opndSrc1->IsTaggedInt())
  3472. {
  3473. IR::Opnd *tempOpnd = opndSrc1;
  3474. opndSrc1 = opndSrc2;
  3475. opndSrc2 = tempOpnd;
  3476. }
  3477. if (opndSrc2->IsTaggedInt())
  3478. {
  3479. if (opndSrc1->IsTaggedInt())
  3480. {
  3481. return instrPrev;
  3482. }
  3483. // TEST src1, AtomTag
  3484. // JEQ $fail
  3485. instr = IR::Instr::New(Js::OpCode::TEST, this->m_func);
  3486. instr->SetSrc1(opndSrc1);
  3487. instr->SetSrc2(IR::IntConstOpnd::New(Js::AtomTag, TyInt8, this->m_func));
  3488. instrInsert->InsertBefore(instr);
  3489. }
  3490. else
  3491. {
  3492. // s1 = MOV src1
  3493. // s1 = AND s1, 1
  3494. // TEST s1, src2
  3495. // JEQ $fail
  3496. // s1 = MOV src1
  3497. opndReg = IR::RegOpnd::New(TyMachReg, this->m_func);
  3498. instr = IR::Instr::New(Js::OpCode::MOV, opndReg, opndSrc1, this->m_func);
  3499. instrInsert->InsertBefore(instr);
  3500. // s1 = AND s1, AtomTag
  3501. instr = IR::Instr::New(
  3502. Js::OpCode::AND, opndReg, opndReg, IR::IntConstOpnd::New(Js::AtomTag, TyMachReg, this->m_func), this->m_func);
  3503. instrInsert->InsertBefore(instr);
  3504. // TEST s1, src2
  3505. instr = IR::Instr::New(Js::OpCode::TEST, this->m_func);
  3506. instr->SetSrc1(opndReg);
  3507. instr->SetSrc2(opndSrc2);
  3508. instrInsert->InsertBefore(instr);
  3509. }
  3510. // JEQ $fail
  3511. instr = IR::BranchInstr::New(Js::OpCode::JEQ, labelFail, this->m_func);
  3512. instrInsert->InsertBefore(instr);
  3513. return instrPrev;
  3514. }
  3515. #else
  3516. IR::Instr *
  3517. LowererMD::GenerateSmIntPairTest(
  3518. IR::Instr * instrInsert,
  3519. IR::Opnd * opndSrc1,
  3520. IR::Opnd * opndSrc2,
  3521. IR::LabelInstr * labelFail)
  3522. {
  3523. IR::Opnd * opndReg;
  3524. IR::Instr * instrPrev = instrInsert->m_prev;
  3525. IR::Instr * instr;
  3526. Assert(opndSrc1->GetType() == TyVar);
  3527. Assert(opndSrc2->GetType() == TyVar);
  3528. if (opndSrc1->IsTaggedInt())
  3529. {
  3530. IR::Opnd *tempOpnd = opndSrc1;
  3531. opndSrc1 = opndSrc2;
  3532. opndSrc2 = tempOpnd;
  3533. }
  3534. if (opndSrc2->IsTaggedInt())
  3535. {
  3536. if (opndSrc1->IsTaggedInt())
  3537. {
  3538. return instrPrev;
  3539. }
  3540. GenerateSmIntTest(opndSrc1, instrInsert, labelFail);
  3541. return instrPrev;
  3542. }
  3543. else
  3544. {
  3545. opndReg = IR::RegOpnd::New(TyMachReg, this->m_func);
  3546. #ifdef SHIFTLOAD
  3547. instr = IR::Instr::New(Js::OpCode::SHLD, opndReg, opndSrc1, IR::IntConstOpnd::New(16, TyInt8, this->m_func), this->m_func);
  3548. instrInsert->InsertBefore(instr);
  3549. instr = IR::Instr::New(Js::OpCode::SHLD, opndReg, opndSrc2, IR::IntConstOpnd::New(16, TyInt8, this->m_func), this->m_func);
  3550. instrInsert->InsertBefore(instr);
  3551. #else
  3552. IR::Opnd * opndReg1;
  3553. // s1 = MOV src1
  3554. // s1 = SHR s1, VarTag_Shift
  3555. // s2 = MOV src2
  3556. // s2 = SHR s2, 32
  3557. // s1 = OR s1, s2 ------ move both tags to the lower 32 bits
  3558. // CMP s1, AtomTag_Pair ------ compare the tags together to the expected tag pair
  3559. // JNE $fail
  3560. // s1 = MOV src1
  3561. instr = IR::Instr::New(Js::OpCode::MOV, opndReg, opndSrc1, this->m_func);
  3562. instrInsert->InsertBefore(instr);
  3563. // s1 = SHR s1, VarTag_Shift
  3564. instr = IR::Instr::New(Js::OpCode::SHR, opndReg, opndReg, IR::IntConstOpnd::New(Js::VarTag_Shift, TyInt8, this->m_func), this->m_func);
  3565. instrInsert->InsertBefore(instr);
  3566. // s2 = MOV src2
  3567. opndReg1 = IR::RegOpnd::New(TyMachReg, this->m_func);
  3568. instr = IR::Instr::New(Js::OpCode::MOV, opndReg1, opndSrc2, this->m_func);
  3569. instrInsert->InsertBefore(instr);
  3570. // s2 = SHR s2, 32
  3571. instr = IR::Instr::New(Js::OpCode::SHR, opndReg1, opndReg1, IR::IntConstOpnd::New(32, TyInt8, this->m_func), this->m_func);
  3572. instrInsert->InsertBefore(instr);
  3573. // s1 = OR s1, s2
  3574. instr = IR::Instr::New(Js::OpCode::OR, opndReg, opndReg, opndReg1, this->m_func);
  3575. instrInsert->InsertBefore(instr);
  3576. #endif
  3577. opndReg = opndReg->UseWithNewType(TyInt32, this->m_func)->AsRegOpnd();
  3578. // CMP s1, AtomTag_Pair
  3579. instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  3580. instr->SetSrc1(opndReg);
  3581. instr->SetSrc2(IR::IntConstOpnd::New(Js::AtomTag_Pair, TyInt32, this->m_func, true));
  3582. instrInsert->InsertBefore(instr);
  3583. }
  3584. // JNE $fail
  3585. instr = IR::BranchInstr::New(Js::OpCode::JNE, labelFail, this->m_func);
  3586. instrInsert->InsertBefore(instr);
  3587. return instrPrev;
  3588. }
  3589. #endif
  3590. IR::BranchInstr *
  3591. LowererMD::GenerateLocalInlineCacheCheck(
  3592. IR::Instr * instrLdSt,
  3593. IR::RegOpnd * opndType,
  3594. IR::RegOpnd * inlineCache,
  3595. IR::LabelInstr * labelNext,
  3596. bool checkTypeWithoutProperty)
  3597. {
  3598. // Generate:
  3599. //
  3600. // CMP s1, [&(inlineCache->u.local.type/typeWithoutProperty)]
  3601. // JNE $next
  3602. IR::Instr * instr;
  3603. IR::Opnd* typeOpnd;
  3604. if (checkTypeWithoutProperty)
  3605. {
  3606. typeOpnd = IR::IndirOpnd::New(inlineCache, (int32)offsetof(Js::InlineCache, u.local.typeWithoutProperty), TyMachReg, instrLdSt->m_func);
  3607. }
  3608. else
  3609. {
  3610. typeOpnd = IR::IndirOpnd::New(inlineCache, (int32)offsetof(Js::InlineCache, u.local.type), TyMachReg, instrLdSt->m_func);
  3611. }
  3612. // CMP type, [&(inlineCache->u.local.type/typeWithoutProperty)]
  3613. instr = IR::Instr::New(Js::OpCode::CMP, instrLdSt->m_func);
  3614. instr->SetSrc1(opndType);
  3615. instr->SetSrc2(typeOpnd);
  3616. instrLdSt->InsertBefore(instr);
  3617. // JNE $next
  3618. IR::BranchInstr * branchInstr = IR::BranchInstr::New(Js::OpCode::JNE, labelNext, instrLdSt->m_func);
  3619. instrLdSt->InsertBefore(branchInstr);
  3620. return branchInstr;
  3621. }
  3622. IR::BranchInstr *
  3623. LowererMD::GenerateProtoInlineCacheCheck(
  3624. IR::Instr * instrLdSt,
  3625. IR::RegOpnd * opndType,
  3626. IR::RegOpnd * inlineCache,
  3627. IR::LabelInstr * labelNext)
  3628. {
  3629. // Generate:
  3630. //
  3631. // CMP s1, [&(inlineCache->u.proto.type)]
  3632. // JNE $next
  3633. IR::Instr * instr;
  3634. IR::Opnd* typeOpnd = IR::IndirOpnd::New(inlineCache, (int32)offsetof(Js::InlineCache, u.proto.type), TyMachReg, instrLdSt->m_func);
  3635. // CMP s1, [&(inlineCache->u.proto.type)]
  3636. instr = IR::Instr::New(Js::OpCode::CMP, instrLdSt->m_func);
  3637. instr->SetSrc1(opndType);
  3638. instr->SetSrc2(typeOpnd);
  3639. instrLdSt->InsertBefore(instr);
  3640. // JNE $next
  3641. IR::BranchInstr * branchInstr = IR::BranchInstr::New(Js::OpCode::JNE, labelNext, instrLdSt->m_func);
  3642. instrLdSt->InsertBefore(branchInstr);
  3643. return branchInstr;
  3644. }
  3645. IR::BranchInstr *
  3646. LowererMD::GenerateFlagInlineCacheCheck(
  3647. IR::Instr * instrLdSt,
  3648. IR::RegOpnd * opndType,
  3649. IR::RegOpnd * opndInlineCache,
  3650. IR::LabelInstr * labelNext)
  3651. {
  3652. // Generate:
  3653. //
  3654. // CMP s1, [&(inlineCache->u.accessor.type)]
  3655. // JNE $next
  3656. IR::Instr * instr;
  3657. IR::Opnd* typeOpnd;
  3658. typeOpnd = IR::IndirOpnd::New(opndInlineCache, (int32)offsetof(Js::InlineCache, u.accessor.type), TyMachReg, instrLdSt->m_func);
  3659. // CMP s1, [&(inlineCache->u.flag.type)]
  3660. instr = IR::Instr::New(Js::OpCode::CMP, instrLdSt->m_func);
  3661. instr->SetSrc1(opndType);
  3662. instr->SetSrc2(typeOpnd);
  3663. instrLdSt->InsertBefore(instr);
  3664. // JNE $next
  3665. IR::BranchInstr * branchInstr = IR::BranchInstr::New(Js::OpCode::JNE, labelNext, instrLdSt->m_func);
  3666. instrLdSt->InsertBefore(branchInstr);
  3667. return branchInstr;
  3668. }
  3669. IR::BranchInstr *
  3670. LowererMD::GenerateFlagInlineCacheCheckForNoGetterSetter(
  3671. IR::Instr * instrLdSt,
  3672. IR::RegOpnd * opndInlineCache,
  3673. IR::LabelInstr * labelNext)
  3674. {
  3675. // Generate:
  3676. //
  3677. // TEST [&(inlineCache->u.accessor.flags)], (Js::InlineCacheGetterFlag | Js::InlineCacheSetterFlag)
  3678. // JNE $next
  3679. IR::Instr * instr;
  3680. IR::Opnd* flagsOpnd;
  3681. flagsOpnd = IR::IndirOpnd::New(opndInlineCache, 0, TyInt8, instrLdSt->m_func);
  3682. // TEST [&(inlineCache->u.accessor.flags)], (Js::InlineCacheGetterFlag | Js::InlineCacheSetterFlag)
  3683. instr = IR::Instr::New(Js::OpCode::TEST,instrLdSt->m_func);
  3684. instr->SetSrc1(flagsOpnd);
  3685. instr->SetSrc2(IR::IntConstOpnd::New((Js::InlineCacheGetterFlag | Js::InlineCacheSetterFlag) << 1, TyInt8, instrLdSt->m_func));
  3686. instrLdSt->InsertBefore(instr);
  3687. // JNE $next
  3688. IR::BranchInstr * branchInstr = IR::BranchInstr::New(Js::OpCode::JNE, labelNext, instrLdSt->m_func);
  3689. instrLdSt->InsertBefore(branchInstr);
  3690. return branchInstr;
  3691. }
  3692. void
  3693. LowererMD::GenerateFlagInlineCacheCheckForGetterSetter(
  3694. IR::Instr * insertBeforeInstr,
  3695. IR::RegOpnd * opndInlineCache,
  3696. IR::LabelInstr * labelNext)
  3697. {
  3698. uint accessorFlagMask;
  3699. if (PHASE_OFF(Js::InlineGettersPhase, insertBeforeInstr->m_func))
  3700. {
  3701. accessorFlagMask = Js::InlineCache::GetSetterFlagMask();
  3702. }
  3703. else if (PHASE_OFF(Js::InlineSettersPhase, insertBeforeInstr->m_func))
  3704. {
  3705. accessorFlagMask = Js::InlineCache::GetGetterFlagMask();
  3706. }
  3707. else
  3708. {
  3709. accessorFlagMask = Js::InlineCache::GetGetterSetterFlagMask();
  3710. }
  3711. // Generate:
  3712. //
  3713. // TEST [&(inlineCache->u.accessor.flags)], Js::InlineCacheGetterFlag | Js::InlineCacheSetterFlag
  3714. // JEQ $next
  3715. IR::Instr * instr;
  3716. IR::Opnd* flagsOpnd;
  3717. flagsOpnd = IR::IndirOpnd::New(opndInlineCache, (int32)offsetof(Js::InlineCache, u.accessor.rawUInt16), TyInt8, insertBeforeInstr->m_func);
  3718. // TEST [&(inlineCache->u.accessor.flags)], InlineCacheGetterFlag | InlineCacheSetterFlag
  3719. instr = IR::Instr::New(Js::OpCode::TEST,this->m_func);
  3720. instr->SetSrc1(flagsOpnd);
  3721. instr->SetSrc2(IR::IntConstOpnd::New(accessorFlagMask, TyInt8, this->m_func));
  3722. insertBeforeInstr->InsertBefore(instr);
  3723. // JEQ $next
  3724. instr = IR::BranchInstr::New(Js::OpCode::JEQ, labelNext, this->m_func);
  3725. insertBeforeInstr->InsertBefore(instr);
  3726. }
  3727. void
  3728. LowererMD::GenerateLdFldFromLocalInlineCache(
  3729. IR::Instr * instrLdFld,
  3730. IR::RegOpnd * opndBase,
  3731. IR::Opnd * opndDst,
  3732. IR::RegOpnd * inlineCache,
  3733. IR::LabelInstr * labelFallThru,
  3734. bool isInlineSlot)
  3735. {
  3736. // Generate:
  3737. //
  3738. // s1 = MOV base->slots -- load the slot array
  3739. // s2 = MOVZXw [&(inlineCache->u.local.slotIndex)] -- load the cached slot index
  3740. // dst = MOV [s1 + s2* Scale] -- load the value directly from the slot
  3741. // JMP $fallthru
  3742. IR::Instr * instr;
  3743. IR::Opnd* slotIndexOpnd;
  3744. IR::IndirOpnd * opndIndir;
  3745. IR::RegOpnd * opndSlotArray = nullptr;
  3746. if (!isInlineSlot)
  3747. {
  3748. opndSlotArray = IR::RegOpnd::New(TyMachReg, instrLdFld->m_func);
  3749. opndIndir = IR::IndirOpnd::New(opndBase, Js::DynamicObject::GetOffsetOfAuxSlots(), TyMachReg, instrLdFld->m_func);
  3750. instr = IR::Instr::New(Js::OpCode::MOV, opndSlotArray, opndIndir, instrLdFld->m_func);
  3751. instrLdFld->InsertBefore(instr);
  3752. }
  3753. // s2 = MOVZXw [&(inlineCache->u.local.slotIndex)] -- load the cached slot index
  3754. IR::RegOpnd * opndReg2 = IR::RegOpnd::New(TyMachReg, instrLdFld->m_func);
  3755. slotIndexOpnd = IR::IndirOpnd::New(inlineCache, (int32)offsetof(Js::InlineCache, u.local.slotIndex), TyUint16, instrLdFld->m_func);
  3756. instr = IR::Instr::New(Js::OpCode::MOVZXW, opndReg2, slotIndexOpnd, instrLdFld->m_func);
  3757. instrLdFld->InsertBefore(instr);
  3758. if (isInlineSlot)
  3759. {
  3760. // dst = MOV [base + s2* Scale] -- load the value directly from the slot
  3761. opndIndir = IR::IndirOpnd::New(opndBase, opndReg2, LowererMDArch::GetDefaultIndirScale(), TyMachReg, instrLdFld->m_func);
  3762. instr = IR::Instr::New(Js::OpCode::MOV, opndDst, opndIndir, instrLdFld->m_func);
  3763. instrLdFld->InsertBefore(instr);
  3764. }
  3765. else
  3766. {
  3767. // dst = MOV [s1 + s2* Scale] -- load the value directly from the slot
  3768. opndIndir = IR::IndirOpnd::New(opndSlotArray, opndReg2, LowererMDArch::GetDefaultIndirScale(), TyMachReg, instrLdFld->m_func);
  3769. instr = IR::Instr::New(Js::OpCode::MOV, opndDst, opndIndir, instrLdFld->m_func);
  3770. instrLdFld->InsertBefore(instr);
  3771. }
  3772. // JMP $fallthru
  3773. instr = IR::BranchInstr::New(Js::OpCode::JMP, labelFallThru, instrLdFld->m_func);
  3774. instrLdFld->InsertBefore(instr);
  3775. }
  3776. void
  3777. LowererMD::GenerateLdLocalFldFromFlagInlineCache(
  3778. IR::Instr * instrLdFld,
  3779. IR::RegOpnd * opndBase,
  3780. IR::Opnd * opndDst,
  3781. IR::RegOpnd * opndInlineCache,
  3782. IR::LabelInstr * labelFallThru,
  3783. bool isInlineSlot)
  3784. {
  3785. // Generate:
  3786. //
  3787. // s1 = MOV [&base->slots] -- load the slot array
  3788. // s2 = MOVZXW [&(inlineCache->u.accessor.slotIndex)] -- load the cached slot index
  3789. // dst = MOV [s1 + s2*4]
  3790. // JMP $fallthru
  3791. IR::Instr * instr;
  3792. IR::Opnd* slotIndexOpnd;
  3793. IR::IndirOpnd * opndIndir;
  3794. IR::RegOpnd * opndSlotArray = nullptr;
  3795. if (!isInlineSlot)
  3796. {
  3797. opndSlotArray = IR::RegOpnd::New(TyMachReg, instrLdFld->m_func);
  3798. opndIndir = IR::IndirOpnd::New(opndBase, Js::DynamicObject::GetOffsetOfAuxSlots(), TyMachReg, instrLdFld->m_func);
  3799. instr = IR::Instr::New(Js::OpCode::MOV, opndSlotArray, opndIndir, instrLdFld->m_func);
  3800. instrLdFld->InsertBefore(instr);
  3801. }
  3802. // s2 = MOVZXW [&(inlineCache->u.accessor.slotIndex)] -- load the cached slot index
  3803. IR::RegOpnd *opndSlotIndex = IR::RegOpnd::New(TyMachReg, instrLdFld->m_func);
  3804. slotIndexOpnd = IR::IndirOpnd::New(opndInlineCache, (int32)offsetof(Js::InlineCache, u.accessor.slotIndex), TyUint16, instrLdFld->m_func);
  3805. instr = IR::Instr::New(Js::OpCode::MOVZXW, opndSlotIndex, slotIndexOpnd, instrLdFld->m_func);
  3806. instrLdFld->InsertBefore(instr);
  3807. if (isInlineSlot)
  3808. {
  3809. // dst = MOV [s1 + s2*4]
  3810. opndIndir = IR::IndirOpnd::New(opndBase, opndSlotIndex, LowererMDArch::GetDefaultIndirScale(), TyMachReg, instrLdFld->m_func);
  3811. instr = IR::Instr::New(Js::OpCode::MOV, opndDst, opndIndir, instrLdFld->m_func);
  3812. instrLdFld->InsertBefore(instr);
  3813. }
  3814. else
  3815. {
  3816. // dst = MOV [s1 + s2*4]
  3817. opndIndir = IR::IndirOpnd::New(opndSlotArray, opndSlotIndex, LowererMDArch::GetDefaultIndirScale(), TyMachReg, instrLdFld->m_func);
  3818. instr = IR::Instr::New(Js::OpCode::MOV, opndDst, opndIndir, instrLdFld->m_func);
  3819. instrLdFld->InsertBefore(instr);
  3820. }
  3821. // JMP $fallthru
  3822. instr = IR::BranchInstr::New(Js::OpCode::JMP, labelFallThru, instrLdFld->m_func);
  3823. instrLdFld->InsertBefore(instr);
  3824. }
  3825. void
  3826. LowererMD::GenerateLdFldFromFlagInlineCache(
  3827. IR::Instr * insertBeforeInstr,
  3828. IR::RegOpnd * opndBase,
  3829. IR::Opnd * opndDst,
  3830. IR::RegOpnd * opndInlineCache,
  3831. IR::LabelInstr * labelFallThru,
  3832. bool isInlineSlot)
  3833. {
  3834. // Generate:
  3835. //
  3836. // s1 = MOV [&(inlineCache->u.accessor.object)] -- load the cached prototype object
  3837. // s1 = MOV [&s1->slots] -- load the slot array
  3838. // s2 = MOVZXW [&(inlineCache->u.accessor.slotIndex)] -- load the cached slot index
  3839. // dst = MOV [s1 + s2*4]
  3840. // JMP $fallthru
  3841. IR::Instr * instr;
  3842. IR::Opnd* inlineCacheObjOpnd;
  3843. IR::IndirOpnd * opndIndir;
  3844. IR::RegOpnd * opndObjSlots = nullptr;
  3845. inlineCacheObjOpnd = IR::IndirOpnd::New(opndInlineCache, (int32)offsetof(Js::InlineCache, u.accessor.object), TyMachReg, this->m_func);
  3846. // s1 = MOV [&(inlineCache->u.accessor.object)] -- load the cached prototype object
  3847. IR::RegOpnd *opndObject = IR::RegOpnd::New(TyMachReg, this->m_func);
  3848. instr = IR::Instr::New(Js::OpCode::MOV, opndObject, inlineCacheObjOpnd, this->m_func);
  3849. insertBeforeInstr->InsertBefore(instr);
  3850. if (!isInlineSlot)
  3851. {
  3852. // s1 = MOV [&s1->slots] -- load the slot array
  3853. opndObjSlots = IR::RegOpnd::New(TyMachReg, this->m_func);
  3854. opndIndir = IR::IndirOpnd::New(opndObject, Js::DynamicObject::GetOffsetOfAuxSlots(), TyMachReg, this->m_func);
  3855. instr = IR::Instr::New(Js::OpCode::MOV, opndObjSlots, opndIndir, this->m_func);
  3856. insertBeforeInstr->InsertBefore(instr);
  3857. }
  3858. // s2 = MOVZXW [&(inlineCache->u.accessor.slotIndex)] -- load the cached slot index
  3859. IR::RegOpnd *opndSlotIndex = IR::RegOpnd::New(TyMachReg, this->m_func);
  3860. IR::Opnd* slotIndexOpnd = IR::IndirOpnd::New(opndInlineCache, (int32)offsetof(Js::InlineCache, u.accessor.slotIndex), TyUint16, this->m_func);
  3861. instr = IR::Instr::New(Js::OpCode::MOVZXW, opndSlotIndex, slotIndexOpnd, this->m_func);
  3862. insertBeforeInstr->InsertBefore(instr);
  3863. if (isInlineSlot)
  3864. {
  3865. // dst = MOV [s1 + s2*4]
  3866. opndIndir = IR::IndirOpnd::New(opndObject, opndSlotIndex, this->lowererMDArch.GetDefaultIndirScale(), TyMachReg, this->m_func);
  3867. instr = IR::Instr::New(Js::OpCode::MOV, opndDst, opndIndir, this->m_func);
  3868. insertBeforeInstr->InsertBefore(instr);
  3869. }
  3870. else
  3871. {
  3872. // dst = MOV [s1 + s2*4]
  3873. opndIndir = IR::IndirOpnd::New(opndObjSlots, opndSlotIndex, this->lowererMDArch.GetDefaultIndirScale(), TyMachReg, this->m_func);
  3874. instr = IR::Instr::New(Js::OpCode::MOV, opndDst, opndIndir, this->m_func);
  3875. insertBeforeInstr->InsertBefore(instr);
  3876. }
  3877. // JMP $fallthru
  3878. instr = IR::BranchInstr::New(Js::OpCode::JMP, labelFallThru, this->m_func);
  3879. insertBeforeInstr->InsertBefore(instr);
  3880. }
  3881. void
  3882. LowererMD::GenerateLdFldFromProtoInlineCache(
  3883. IR::Instr * instrLdFld,
  3884. IR::RegOpnd * opndBase,
  3885. IR::Opnd * opndDst,
  3886. IR::RegOpnd * inlineCache,
  3887. IR::LabelInstr * labelFallThru,
  3888. bool isInlineSlot)
  3889. {
  3890. // Generate:
  3891. //
  3892. // s1 = MOV [&(inlineCache->u.proto.prototypeObject)] -- load the cached prototype object
  3893. // s1 = MOV [&s1->slots] -- load the slot array
  3894. // s2 = MOVZXW [&(inlineCache->u.proto.slotIndex)] -- load the cached slot index
  3895. // dst = MOV [s1 + s2*4]
  3896. // JMP $fallthru
  3897. IR::Instr * instr;
  3898. IR::Opnd* inlineCacheProtoOpnd;
  3899. IR::IndirOpnd * opndIndir;
  3900. IR::RegOpnd * opndProtoSlots = nullptr;
  3901. inlineCacheProtoOpnd = IR::IndirOpnd::New(inlineCache, (int32)offsetof(Js::InlineCache, u.proto.prototypeObject), TyMachReg, instrLdFld->m_func);
  3902. // s1 = MOV [&(inlineCache->u.proto.prototypeObject)] -- load the cached prototype object
  3903. IR::RegOpnd *opndProto = IR::RegOpnd::New(TyMachReg, instrLdFld->m_func);
  3904. instr = IR::Instr::New(Js::OpCode::MOV, opndProto, inlineCacheProtoOpnd, instrLdFld->m_func);
  3905. instrLdFld->InsertBefore(instr);
  3906. if (!isInlineSlot)
  3907. {
  3908. // s1 = MOV [&s1->slots] -- load the slot array
  3909. opndProtoSlots = IR::RegOpnd::New(TyMachReg, instrLdFld->m_func);
  3910. opndIndir = IR::IndirOpnd::New(opndProto, Js::DynamicObject::GetOffsetOfAuxSlots(), TyMachReg, instrLdFld->m_func);
  3911. instr = IR::Instr::New(Js::OpCode::MOV, opndProtoSlots, opndIndir, instrLdFld->m_func);
  3912. instrLdFld->InsertBefore(instr);
  3913. }
  3914. // s2 = MOVZXW [&(inlineCache->u.proto.slotIndex)] -- load the cached slot index
  3915. IR::RegOpnd *opndSlotIndex = IR::RegOpnd::New(TyMachReg, instrLdFld->m_func);
  3916. IR::Opnd* slotIndexOpnd = IR::IndirOpnd::New(inlineCache, (int32)offsetof(Js::InlineCache, u.proto.slotIndex), TyUint16, instrLdFld->m_func);
  3917. instr = IR::Instr::New(Js::OpCode::MOVZXW, opndSlotIndex, slotIndexOpnd, instrLdFld->m_func);
  3918. instrLdFld->InsertBefore(instr);
  3919. if (isInlineSlot)
  3920. {
  3921. // dst = MOV [s1 + s2*4]
  3922. opndIndir = IR::IndirOpnd::New(opndProto, opndSlotIndex, LowererMDArch::GetDefaultIndirScale(), TyMachReg, instrLdFld->m_func);
  3923. instr = IR::Instr::New(Js::OpCode::MOV, opndDst, opndIndir, instrLdFld->m_func);
  3924. instrLdFld->InsertBefore(instr);
  3925. }
  3926. else
  3927. {
  3928. // dst = MOV [s1 + s2*4]
  3929. opndIndir = IR::IndirOpnd::New(opndProtoSlots, opndSlotIndex, LowererMDArch::GetDefaultIndirScale(), TyMachReg, instrLdFld->m_func);
  3930. instr = IR::Instr::New(Js::OpCode::MOV, opndDst, opndIndir, instrLdFld->m_func);
  3931. instrLdFld->InsertBefore(instr);
  3932. }
  3933. // JMP $fallthru
  3934. instr = IR::BranchInstr::New(Js::OpCode::JMP, labelFallThru, instrLdFld->m_func);
  3935. instrLdFld->InsertBefore(instr);
  3936. }
  3937. void
  3938. LowererMD::GenerateLoadTaggedType(IR::Instr * instrLdSt, IR::RegOpnd * opndType, IR::RegOpnd * opndTaggedType)
  3939. {
  3940. // Generate
  3941. //
  3942. // MOV taggedType, type
  3943. // OR taggedType, InlineCacheAuxSlotTypeTag
  3944. // MOV taggedType, type
  3945. {
  3946. IR::Instr * instrMov = IR::Instr::New(Js::OpCode::MOV, opndTaggedType, opndType, instrLdSt->m_func);
  3947. instrLdSt->InsertBefore(instrMov);
  3948. }
  3949. // OR taggedType, InlineCacheAuxSlotTypeTag
  3950. {
  3951. IR::IntConstOpnd * opndAuxSlotTag = IR::IntConstOpnd::New(InlineCacheAuxSlotTypeTag, TyMachPtr, instrLdSt->m_func);
  3952. IR::Instr * instrAnd = IR::Instr::New(Js::OpCode::OR, opndTaggedType, opndTaggedType, opndAuxSlotTag, instrLdSt->m_func);
  3953. instrLdSt->InsertBefore(instrAnd);
  3954. }
  3955. }
  3956. ///----------------------------------------------------------------------------
  3957. ///
  3958. /// LowererMD::GenerateFastLdMethodFromFlags
  3959. ///
  3960. /// Make use of the helper to cache the type and slot index used to do a LdFld
  3961. /// and do an inline load from the appropriate slot if the type hasn't changed
  3962. /// since the last time this LdFld was executed.
  3963. ///
  3964. ///----------------------------------------------------------------------------
  3965. bool
  3966. LowererMD::GenerateFastLdMethodFromFlags(IR::Instr * instrLdFld)
  3967. {
  3968. IR::LabelInstr * labelFallThru;
  3969. IR::LabelInstr * bailOutLabel;
  3970. IR::Opnd * opndSrc;
  3971. IR::Opnd * opndDst;
  3972. IR::RegOpnd * opndBase;
  3973. IR::RegOpnd * opndType;
  3974. IR::RegOpnd * opndInlineCache;
  3975. opndSrc = instrLdFld->GetSrc1();
  3976. AssertMsg(opndSrc->IsSymOpnd() && opndSrc->AsSymOpnd()->IsPropertySymOpnd() && opndSrc->AsSymOpnd()->m_sym->IsPropertySym(),
  3977. "Expected property sym operand as src of LdFldFlags");
  3978. IR::PropertySymOpnd * propertySymOpnd = opndSrc->AsPropertySymOpnd();
  3979. Assert(!instrLdFld->DoStackArgsOpt(this->m_func));
  3980. if (propertySymOpnd->IsTypeCheckSeqCandidate())
  3981. {
  3982. AssertMsg(propertySymOpnd->HasObjectTypeSym(), "Type optimized property sym operand without a type sym?");
  3983. StackSym *typeSym = propertySymOpnd->GetObjectTypeSym();
  3984. opndType = IR::RegOpnd::New(typeSym, TyMachReg, this->m_func);
  3985. }
  3986. else
  3987. {
  3988. opndType = IR::RegOpnd::New(TyMachReg, this->m_func);
  3989. }
  3990. opndBase = propertySymOpnd->CreatePropertyOwnerOpnd(m_func);
  3991. opndDst = instrLdFld->GetDst();
  3992. opndInlineCache = IR::RegOpnd::New(TyMachPtr, this->m_func);
  3993. labelFallThru = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  3994. // Label to jump to (or fall through to) when bailing out
  3995. bailOutLabel = IR::LabelInstr::New(Js::OpCode::Label, instrLdFld->m_func, true /* isOpHelper */);
  3996. instrLdFld->InsertBefore(IR::Instr::New(Js::OpCode::MOV, opndInlineCache, m_lowerer->LoadRuntimeInlineCacheOpnd(instrLdFld, propertySymOpnd), this->m_func));
  3997. IR::LabelInstr * labelFlagAux = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  3998. // Check the flag cache with the untagged type
  3999. this->m_lowerer->GenerateObjectTestAndTypeLoad(instrLdFld, opndBase, opndType, bailOutLabel);
  4000. // Blindly do the check for getter flag first and then do the type check
  4001. // We avoid repeated check for getter flag when the function object may be in either
  4002. // inline slots or auxiliary slots
  4003. GenerateFlagInlineCacheCheckForGetterSetter(instrLdFld, opndInlineCache, bailOutLabel);
  4004. GenerateFlagInlineCacheCheck(instrLdFld, opndType, opndInlineCache, labelFlagAux);
  4005. GenerateLdFldFromFlagInlineCache(instrLdFld, opndBase, opndDst, opndInlineCache, labelFallThru, true);
  4006. // Check the flag cache with the tagged type
  4007. instrLdFld->InsertBefore(labelFlagAux);
  4008. IR::RegOpnd * opndTaggedType = IR::RegOpnd::New(TyMachReg, this->m_func);
  4009. GenerateLoadTaggedType(instrLdFld, opndType, opndTaggedType);
  4010. GenerateFlagInlineCacheCheck(instrLdFld, opndTaggedType, opndInlineCache, bailOutLabel);
  4011. GenerateLdFldFromFlagInlineCache(instrLdFld, opndBase, opndDst, opndInlineCache, labelFallThru, false);
  4012. instrLdFld->InsertBefore(bailOutLabel);
  4013. instrLdFld->InsertAfter(labelFallThru);
  4014. // Generate the bailout helper call. 'instr' will be changed to the CALL into the bailout function, so it can't be used for
  4015. // ordering instructions anymore.
  4016. instrLdFld->UnlinkSrc1();
  4017. this->m_lowerer->GenerateBailOut(instrLdFld);
  4018. return true;
  4019. }
  4020. void
  4021. LowererMD::GenerateLoadPolymorphicInlineCacheSlot(IR::Instr * instrLdSt, IR::RegOpnd * opndInlineCache, IR::RegOpnd * opndType, uint polymorphicInlineCacheSize)
  4022. {
  4023. // Generate
  4024. //
  4025. // MOV r1, type
  4026. // SHR r1, PolymorphicInlineCacheShift
  4027. // AND r1, (size - 1)
  4028. // SHL r1, log2(sizeof(Js::InlineCache))
  4029. // LEA inlineCache, [inlineCache + r1]
  4030. // MOV r1, type
  4031. IR::RegOpnd * opndOffset = IR::RegOpnd::New(TyMachPtr, instrLdSt->m_func);
  4032. IR::Instr * instr = IR::Instr::New(Js::OpCode::MOV, opndOffset, opndType, instrLdSt->m_func);
  4033. instrLdSt->InsertBefore(instr);
  4034. IntConstType rightShiftAmount = PolymorphicInlineCacheShift;
  4035. IntConstType leftShiftAmount = Math::Log2(sizeof(Js::InlineCache));
  4036. // instead of generating
  4037. // SHR r1, PolymorphicInlineCacheShift
  4038. // AND r1, (size - 1)
  4039. // SHL r1, log2(sizeof(Js::InlineCache))
  4040. //
  4041. // we can generate:
  4042. // SHR r1, (PolymorphicInlineCacheShift - log2(sizeof(Js::InlineCache))
  4043. // AND r1, (size - 1) << log2(sizeof(Js::InlineCache))
  4044. Assert(rightShiftAmount > leftShiftAmount);
  4045. instr = IR::Instr::New(Js::OpCode::SHR, opndOffset, opndOffset, IR::IntConstOpnd::New(rightShiftAmount - leftShiftAmount, TyUint8, instrLdSt->m_func, true), instrLdSt->m_func);
  4046. instrLdSt->InsertBefore(instr);
  4047. instr = IR::Instr::New(Js::OpCode::AND, opndOffset, opndOffset, IR::IntConstOpnd::New(((__int64)(polymorphicInlineCacheSize - 1) << leftShiftAmount), TyMachReg, instrLdSt->m_func, true), instrLdSt->m_func);
  4048. instrLdSt->InsertBefore(instr);
  4049. // LEA inlineCache, [inlineCache + r1]
  4050. IR::IndirOpnd * indirOpnd = IR::IndirOpnd::New(opndInlineCache, opndOffset, TyMachPtr, instrLdSt->m_func);
  4051. instr = IR::Instr::New(Js::OpCode::LEA, opndInlineCache, indirOpnd, instrLdSt->m_func);
  4052. instrLdSt->InsertBefore(instr);
  4053. }
  4054. IR::Instr *
  4055. LowererMD::ChangeToWriteBarrierAssign(IR::Instr * assignInstr, const Func* func)
  4056. {
  4057. #ifdef RECYCLER_WRITE_BARRIER_JIT
  4058. IR::Opnd* dest = assignInstr->GetDst();
  4059. auto threadContextInfo = func->GetTopFunc()->GetThreadContextInfo();
  4060. void* destAddr = nullptr;
  4061. bool isPossibleBarrieredDest = false;
  4062. if (TySize[dest->GetType()] == sizeof(void*))
  4063. {
  4064. if (dest->IsIndirOpnd())
  4065. {
  4066. Assert(!dest->AsIndirOpnd()->HasAddrKind());
  4067. isPossibleBarrieredDest = true;
  4068. }
  4069. else if (dest->IsMemRefOpnd())
  4070. {
  4071. // looks all thread context field access are from MemRefOpnd
  4072. destAddr = (void*)dest->AsMemRefOpnd()->GetMemLoc();
  4073. isPossibleBarrieredDest = destAddr != nullptr
  4074. && ((intptr_t)destAddr % sizeof(void*)) == 0
  4075. && destAddr != (void*)threadContextInfo->GetImplicitCallFlagsAddr()
  4076. && destAddr != (void*)threadContextInfo->GetDisableImplicitFlagsAddr()
  4077. && destAddr != (void*)threadContextInfo->GetBailOutRegisterSaveSpaceAddr();
  4078. if (isPossibleBarrieredDest)
  4079. {
  4080. Assert(Recycler::WBCheckIsRecyclerAddress((char*)destAddr));
  4081. }
  4082. }
  4083. }
  4084. #endif
  4085. IR::Instr * instr = ChangeToAssignNoBarrierCheck(assignInstr);
  4086. // Now insert write barrier if necessary
  4087. #ifdef RECYCLER_WRITE_BARRIER_JIT
  4088. if (isPossibleBarrieredDest
  4089. && assignInstr->m_opcode == Js::OpCode::MOV // ignore SSE instructions like MOVSD
  4090. && assignInstr->GetSrc1()->IsWriteBarrierTriggerableValue())
  4091. {
  4092. instr = LowererMD::GenerateWriteBarrier(assignInstr);
  4093. }
  4094. #endif
  4095. return instr;
  4096. }
  4097. void
  4098. LowererMD::GenerateWriteBarrierAssign(IR::MemRefOpnd * opndDst, IR::Opnd * opndSrc, IR::Instr * insertBeforeInstr)
  4099. {
  4100. Lowerer::InsertMove(opndDst, opndSrc, insertBeforeInstr);
  4101. #ifdef RECYCLER_WRITE_BARRIER_JIT
  4102. if (opndSrc->IsWriteBarrierTriggerableValue())
  4103. {
  4104. void * address = (void *)opndDst->AsMemRefOpnd()->GetMemLoc();
  4105. #ifdef RECYCLER_WRITE_BARRIER_BYTE
  4106. // WriteBarrier-TODO: need to pass card table address through RPC
  4107. IR::MemRefOpnd * cardTableEntry = IR::MemRefOpnd::New(
  4108. &RecyclerWriteBarrierManager::GetAddressOfCardTable()[RecyclerWriteBarrierManager::GetCardTableIndex(address)], TyInt8, insertBeforeInstr->m_func);
  4109. IR::Instr * movInstr = IR::Instr::New(Js::OpCode::MOV, cardTableEntry, IR::IntConstOpnd::New(1, TyInt8, insertBeforeInstr->m_func), insertBeforeInstr->m_func);
  4110. insertBeforeInstr->InsertBefore(movInstr);
  4111. #if DBG && GLOBAL_ENABLE_WRITE_BARRIER
  4112. if (CONFIG_FLAG(ForceSoftwareWriteBarrier) && CONFIG_FLAG(RecyclerVerifyMark))
  4113. {
  4114. this->LoadHelperArgument(insertBeforeInstr, opndDst);
  4115. IR::Instr* instrCall = IR::Instr::New(Js::OpCode::Call, m_func);
  4116. insertBeforeInstr->InsertBefore(instrCall);
  4117. this->ChangeToHelperCall(instrCall, IR::HelperWriteBarrierSetVerifyBit);
  4118. }
  4119. #endif
  4120. #else
  4121. IR::MemRefOpnd * cardTableEntry = IR::MemRefOpnd::New(
  4122. &RecyclerWriteBarrierManager::GetAddressOfCardTable()[RecyclerWriteBarrierManager::GetCardTableIndex(address)], TyMachPtr, assignInstr->m_func);
  4123. IR::Instr * orInstr = IR::Instr::New(Js::OpCode::OR, cardTableEntry,
  4124. IR::IntConstOpnd::New(1 << ((uint)address >> 7), TyInt32, assignInstr->m_func), assignInstr->m_func);
  4125. assignInstr->InsertBefore(orInstr);
  4126. #endif
  4127. }
  4128. #endif
  4129. }
  4130. void
  4131. LowererMD::GenerateWriteBarrierAssign(IR::IndirOpnd * opndDst, IR::Opnd * opndSrc, IR::Instr * insertBeforeInstr)
  4132. {
  4133. #ifdef RECYCLER_WRITE_BARRIER_JIT
  4134. if (opndSrc->IsWriteBarrierTriggerableValue())
  4135. {
  4136. IR::RegOpnd * writeBarrierAddrRegOpnd = IR::RegOpnd::New(TyMachPtr, insertBeforeInstr->m_func);
  4137. insertBeforeInstr->InsertBefore(IR::Instr::New(Js::OpCode::LEA, writeBarrierAddrRegOpnd, opndDst, insertBeforeInstr->m_func));
  4138. IR::Instr* movInstr = IR::Instr::New(Js::OpCode::MOV,
  4139. IR::IndirOpnd::New(writeBarrierAddrRegOpnd, 0, TyMachReg, insertBeforeInstr->m_func), opndSrc, insertBeforeInstr->m_func);
  4140. insertBeforeInstr->InsertBefore(movInstr);
  4141. GenerateWriteBarrier(movInstr);
  4142. // The mov happens above, and it's slightly faster doing it that way since we've already calculated the address we're writing to
  4143. return;
  4144. }
  4145. #endif
  4146. Lowerer::InsertMove(opndDst, opndSrc, insertBeforeInstr);
  4147. return;
  4148. }
  4149. #ifdef RECYCLER_WRITE_BARRIER_JIT
  4150. IR::Instr*
  4151. LowererMD::GenerateWriteBarrier(IR::Instr * assignInstr)
  4152. {
  4153. #if defined(RECYCLER_WRITE_BARRIER_BYTE)
  4154. PHASE_PRINT_TRACE(Js::JitWriteBarrierPhase, assignInstr->m_func, _u("Generating write barrier\n"));
  4155. IR::RegOpnd * indexOpnd = IR::RegOpnd::New(TyMachPtr, assignInstr->m_func);
  4156. IR::Instr * loadIndexInstr = IR::Instr::New(Js::OpCode::LEA, indexOpnd, assignInstr->GetDst(), assignInstr->m_func);
  4157. assignInstr->InsertBefore(loadIndexInstr);
  4158. IR::Instr * shiftBitInstr = IR::Instr::New(Js::OpCode::SHR, indexOpnd, indexOpnd,
  4159. IR::IntConstOpnd::New(12 /* 1 << 12 = 4096 */, TyInt8, assignInstr->m_func), assignInstr->m_func);
  4160. assignInstr->InsertAfter(shiftBitInstr);
  4161. // The cardtable address is likely 64 bits already so we have to load it to a register
  4162. // That is, we have to do the following:
  4163. // LEA reg1, targetOfWrite
  4164. // SHR reg1, 12
  4165. // MOV reg2, cardTableAddress
  4166. // MOV [reg1 + reg2], 1
  4167. //
  4168. // Instead of doing this:
  4169. // LEA reg1, targetOfWrite
  4170. // SHR reg1, 12
  4171. // MOV [cardTableAddress + reg2], 1
  4172. //
  4173. //TODO: (leish)(swb) hoist RecyclerWriteBarrierManager::GetAddressOfCardTable()
  4174. IR::RegOpnd * cardTableRegOpnd = IR::RegOpnd::New(TyMachReg, assignInstr->m_func);
  4175. IR::Instr * cardTableAddrInstr = IR::Instr::New(Js::OpCode::MOV, cardTableRegOpnd,
  4176. IR::AddrOpnd::New(RecyclerWriteBarrierManager::GetAddressOfCardTable(), IR::AddrOpndKindWriteBarrierCardTable, assignInstr->m_func),
  4177. assignInstr->m_func);
  4178. shiftBitInstr->InsertAfter(cardTableAddrInstr);
  4179. IR::IndirOpnd * cardTableEntryOpnd = IR::IndirOpnd::New(cardTableRegOpnd, indexOpnd,
  4180. TyInt8, assignInstr->m_func);
  4181. IR::Instr * movInstr = IR::Instr::New(Js::OpCode::MOV, cardTableEntryOpnd, IR::IntConstOpnd::New(1, TyInt8, assignInstr->m_func), assignInstr->m_func);
  4182. cardTableAddrInstr->InsertAfter(movInstr);
  4183. return loadIndexInstr;
  4184. #else
  4185. Assert(writeBarrierAddrRegOpnd->IsRegOpnd());
  4186. IR::RegOpnd * shiftBitOpnd = IR::RegOpnd::New(TyInt32, assignInstr->m_func);
  4187. shiftBitOpnd->SetReg(LowererMDArch::GetRegShiftCount());
  4188. IR::Instr * moveShiftBitOpnd = IR::Instr::New(Js::OpCode::MOV, shiftBitOpnd, writeBarrierAddrRegOpnd, assignInstr->m_func);
  4189. assignInstr->InsertBefore(moveShiftBitOpnd);
  4190. IR::Instr * shiftBitInstr = IR::Instr::New(Js::OpCode::SHR, shiftBitOpnd, shiftBitOpnd,
  4191. IR::IntConstOpnd::New(7 /* 1 << 7 = 128 */, TyInt32, assignInstr->m_func), assignInstr->m_func);
  4192. assignInstr->InsertBefore(shiftBitInstr);
  4193. IR::RegOpnd * bitOpnd = IR::RegOpnd::New(TyInt32, assignInstr->m_func);
  4194. IR::Instr * mov1Instr = IR::Instr::New(Js::OpCode::MOV, bitOpnd,
  4195. IR::IntConstOpnd::New(1, TyInt32, assignInstr->m_func), assignInstr->m_func);
  4196. assignInstr->InsertBefore(mov1Instr);
  4197. IR::Instr * bitInstr = IR::Instr::New(Js::OpCode::SHL, bitOpnd, bitOpnd, shiftBitOpnd, assignInstr->m_func);
  4198. assignInstr->InsertBefore(bitInstr);
  4199. IR::RegOpnd * indexOpnd = shiftBitOpnd;
  4200. IR::Instr * indexInstr = IR::Instr::New(Js::OpCode::SHR, indexOpnd, indexOpnd,
  4201. IR::IntConstOpnd::New(5 /* 1 << 5 = 32 */, TyInt32, assignInstr->m_func), assignInstr->m_func);
  4202. assignInstr->InsertBefore(indexInstr);
  4203. IR::RegOpnd * cardTableRegOpnd = IR::RegOpnd::New(TyMachReg, assignInstr->m_func);
  4204. IR::Instr * cardTableAddrInstr = IR::Instr::New(Js::OpCode::MOV, cardTableRegOpnd,
  4205. IR::AddrOpnd::New(RecyclerWriteBarrierManager::GetAddressOfCardTable(), IR::AddrOpndKindDynamicMisc, assignInstr->m_func),
  4206. assignInstr->m_func);
  4207. assignInstr->InsertBefore(cardTableAddrInstr);
  4208. IR::IndirOpnd * cardTableEntryOpnd = IR::IndirOpnd::New(cardTableRegOpnd, indexOpnd, LowererMDArch::GetDefaultIndirScale(),
  4209. TyInt32, assignInstr->m_func);
  4210. IR::Instr * orInstr = IR::Instr::New(Js::OpCode::OR, cardTableEntryOpnd, cardTableEntryOpnd,
  4211. bitOpnd, assignInstr->m_func);
  4212. assignInstr->InsertBefore(orInstr);
  4213. #endif
  4214. }
  4215. #endif
  4216. void
  4217. LowererMD::GenerateStFldFromLocalInlineCache(
  4218. IR::Instr * instrStFld,
  4219. IR::RegOpnd * opndBase,
  4220. IR::Opnd * opndSrc,
  4221. IR::RegOpnd * inlineCache,
  4222. IR::LabelInstr * labelFallThru,
  4223. bool isInlineSlot)
  4224. {
  4225. IR::Instr * instr;
  4226. IR::Opnd* slotIndexOpnd;
  4227. IR::RegOpnd * opndIndirBase = opndBase;
  4228. if (!isInlineSlot)
  4229. {
  4230. // slotArray = MOV base->slots -- load the slot array
  4231. IR::RegOpnd * opndSlotArray = IR::RegOpnd::New(TyMachReg, instrStFld->m_func);
  4232. IR::IndirOpnd * opndIndir = IR::IndirOpnd::New(opndBase, Js::DynamicObject::GetOffsetOfAuxSlots(), TyMachReg, instrStFld->m_func);
  4233. instr = IR::Instr::New(Js::OpCode::MOV, opndSlotArray, opndIndir, instrStFld->m_func);
  4234. instrStFld->InsertBefore(instr);
  4235. opndIndirBase = opndSlotArray;
  4236. }
  4237. // slotIndex = MOV [&inlineCache->u.local.inlineSlotOffsetOrAuxSlotIndex] -- load the cached slot offset or index
  4238. IR::RegOpnd * opndSlotIndex = IR::RegOpnd::New(TyMachReg, instrStFld->m_func);
  4239. slotIndexOpnd = IR::IndirOpnd::New(inlineCache, (int32)offsetof(Js::InlineCache, u.local.slotIndex), TyUint16, instrStFld->m_func);
  4240. instr = IR::Instr::New(Js::OpCode::MOVZXW, opndSlotIndex, slotIndexOpnd, instrStFld->m_func);
  4241. instrStFld->InsertBefore(instr);
  4242. // [base + slotIndex * (1 << indirScale)] = MOV src -- store the value directly to the slot
  4243. // [slotArray + slotIndex * (1 << indirScale)] = MOV src -- store the value directly to the slot
  4244. IR::IndirOpnd * storeLocIndirOpnd = IR::IndirOpnd::New(opndIndirBase, opndSlotIndex,
  4245. LowererMDArch::GetDefaultIndirScale(), TyMachReg, instrStFld->m_func);
  4246. GenerateWriteBarrierAssign(storeLocIndirOpnd, opndSrc, instrStFld);
  4247. // JMP $fallthru
  4248. instr = IR::BranchInstr::New(Js::OpCode::JMP, labelFallThru, instrStFld->m_func);
  4249. instrStFld->InsertBefore(instr);
  4250. }
  4251. //----------------------------------------------------------------------------
  4252. //
  4253. // LowererMD::GenerateFastScopedLdFld
  4254. //
  4255. // Make use of the helper to cache the type and slot index used to do a ScopedLdFld
  4256. // when the scope is an array of length 1.
  4257. // Extract the only element from array and do an inline load from the appropriate slot
  4258. // if the type hasn't changed since the last time this ScopedLdFld was executed.
  4259. //
  4260. //----------------------------------------------------------------------------
  4261. IR::Instr *
  4262. LowererMD::GenerateFastScopedLdFld(IR::Instr * instrLdScopedFld)
  4263. {
  4264. // CMP [base + offset(length)], 1 -- get the length on array and test if it is 1.
  4265. // JNE $helper
  4266. // MOV r1, [base + offset(scopes)] -- load the first scope
  4267. // MOV r2, r1->type
  4268. // CMP r2, [&(inlineCache->u.local.type)] -- check type
  4269. // JNE $helper
  4270. // MOV r1, r1->slots -- load the slots array
  4271. // MOV r2 , [&(inlineCache->u.local.slotIndex)] -- load the cached slot index
  4272. // MOV dst, [r1+r2] -- load the value from the slot
  4273. // JMP $fallthru
  4274. // $helper:
  4275. // dst = CALL PatchGetPropertyScoped(inlineCache, base, field, defaultInstance, scriptContext)
  4276. // $fallthru:
  4277. IR::RegOpnd * opndBase;
  4278. IR::Instr * instr;
  4279. IR::IndirOpnd * indirOpnd;
  4280. IR::LabelInstr * labelHelper;
  4281. IR::Opnd * opndDst;
  4282. IR::RegOpnd * inlineCache;
  4283. IR::RegOpnd *r1;
  4284. IR::LabelInstr * labelFallThru;
  4285. IR::Opnd *propertySrc = instrLdScopedFld->GetSrc1();
  4286. AssertMsg(propertySrc->IsSymOpnd() && propertySrc->AsSymOpnd()->IsPropertySymOpnd() && propertySrc->AsSymOpnd()->m_sym->IsPropertySym(),
  4287. "Expected property sym operand as src of LdScoped");
  4288. IR::PropertySymOpnd * propertySymOpnd = propertySrc->AsPropertySymOpnd();
  4289. opndBase = propertySymOpnd->CreatePropertyOwnerOpnd(m_func);
  4290. IR::Opnd *srcBase = instrLdScopedFld->GetSrc2();
  4291. AssertMsg(srcBase->IsRegOpnd(), "Expected reg opnd as src2");
  4292. //opndBase = srcBase;
  4293. //IR::IndirOpnd * indirOpnd = src->AsIndirOpnd();
  4294. labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  4295. AssertMsg(opndBase->m_sym->m_isSingleDef, "We assume this isn't redefined");
  4296. // CMP [base + offset(length)], 1 -- get the length on array and test if it is 1.
  4297. indirOpnd = IR::IndirOpnd::New(opndBase, Js::FrameDisplay::GetOffsetOfLength(), TyInt16, this->m_func);
  4298. instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  4299. instr->SetSrc1(indirOpnd);
  4300. instr->SetSrc2(IR::IntConstOpnd::New(0x1, TyInt8, this->m_func));
  4301. instrLdScopedFld->InsertBefore(instr);
  4302. // JNE $helper
  4303. instr = IR::BranchInstr::New(Js::OpCode::JNE, labelHelper, this->m_func);
  4304. instrLdScopedFld->InsertBefore(instr);
  4305. // MOV r1, [base + offset(scopes)] -- load the first scope
  4306. indirOpnd = IR::IndirOpnd::New(opndBase, Js::FrameDisplay::GetOffsetOfScopes(), TyMachReg, this->m_func);
  4307. r1 = IR::RegOpnd::New(TyMachReg, this->m_func);
  4308. instr = IR::Instr::New(Js::OpCode::MOV, r1, indirOpnd, this->m_func);
  4309. instrLdScopedFld->InsertBefore(instr);
  4310. //first load the inlineCache type
  4311. inlineCache = IR::RegOpnd::New(TyMachPtr, this->m_func);
  4312. Assert(inlineCache != nullptr);
  4313. IR::RegOpnd * opndType = IR::RegOpnd::New(TyMachReg, this->m_func);
  4314. opndDst = instrLdScopedFld->GetDst();
  4315. labelFallThru = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  4316. r1->m_sym->m_isNotInt = true;
  4317. // Load the type
  4318. this->m_lowerer->GenerateObjectTestAndTypeLoad(instrLdScopedFld, r1, opndType, labelHelper);
  4319. // Check the local cache with the tagged type
  4320. IR::RegOpnd * opndTaggedType = IR::RegOpnd::New(TyMachReg, this->m_func);
  4321. GenerateLoadTaggedType(instrLdScopedFld, opndType, opndTaggedType);
  4322. instrLdScopedFld->InsertBefore(IR::Instr::New(Js::OpCode::MOV, inlineCache, m_lowerer->LoadRuntimeInlineCacheOpnd(instrLdScopedFld, propertySymOpnd), this->m_func));
  4323. GenerateLocalInlineCacheCheck(instrLdScopedFld, opndTaggedType, inlineCache, labelHelper);
  4324. GenerateLdFldFromLocalInlineCache(instrLdScopedFld, r1, opndDst, inlineCache, labelFallThru, false);
  4325. // $helper:
  4326. // dst = CALL PatchGetPropertyScoped(inlineCache, opndBase, propertyId, srcBase, scriptContext)
  4327. // $fallthru:
  4328. instrLdScopedFld->InsertBefore(labelHelper);
  4329. instrLdScopedFld->InsertAfter(labelFallThru);
  4330. return instrLdScopedFld->m_prev;
  4331. }
  4332. //----------------------------------------------------------------------------
  4333. //
  4334. // LowererMD::GenerateFastScopedStFld
  4335. //
  4336. // Make use of the helper to cache the type and slot index used to do a ScopedStFld
  4337. // when the scope is an array of length 1.
  4338. // Extract the only element from array and do an inline load from the appropriate slot
  4339. // if the type hasn't changed since the last time this ScopedStFld was executed.
  4340. //
  4341. //----------------------------------------------------------------------------
  4342. IR::Instr *
  4343. LowererMD::GenerateFastScopedStFld(IR::Instr * instrStScopedFld)
  4344. {
  4345. // CMP [base + offset(length)], 1 -- get the length on array and test if it is 1.
  4346. // JNE $helper
  4347. // MOV r1, [base + offset(scopes)] -- load the first scope
  4348. // MOV r2, r1->type
  4349. // CMP r2, [&(inlineCache->u.local.type)] -- check type
  4350. // JNE $helper
  4351. // MOV r1, r1->slots -- load the slots array
  4352. // MOV r2, [&(inlineCache->u.local.slotIndex)] -- load the cached slot index
  4353. // [r1 + r2*4] = MOV value -- store the value directly to the slot
  4354. // JMP $fallthru
  4355. // $helper:
  4356. // CALL PatchSetPropertyScoped(inlineCache, base, field, value, defaultInstance, scriptContext)
  4357. // $fallthru:
  4358. IR::RegOpnd * opndBase;
  4359. IR::Instr * instr;
  4360. IR::IndirOpnd * indirOpnd;
  4361. IR::LabelInstr * labelHelper;
  4362. IR::Opnd * opndDst;
  4363. IR::RegOpnd * inlineCache;
  4364. IR::RegOpnd *r1;
  4365. IR::LabelInstr * labelFallThru;
  4366. IR::Opnd *newValue = instrStScopedFld->GetSrc1();
  4367. // IR::Opnd *defaultInstance = instrStScopedFld->UnlinkSrc2();
  4368. opndDst = instrStScopedFld->GetDst();
  4369. AssertMsg(opndDst->IsSymOpnd() && opndDst->AsSymOpnd()->IsPropertySymOpnd() && opndDst->AsSymOpnd()->m_sym->IsPropertySym(),
  4370. "Expected property sym operand as dst of StScoped");
  4371. IR::PropertySymOpnd * propertySymOpnd = opndDst->AsPropertySymOpnd();
  4372. opndBase = propertySymOpnd->CreatePropertyOwnerOpnd(m_func);
  4373. labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  4374. AssertMsg(opndBase->m_sym->m_isSingleDef, "We assume this isn't redefined");
  4375. // CMP [base + offset(length)], 1 -- get the length on array and test if it is 1.
  4376. indirOpnd = IR::IndirOpnd::New(opndBase, Js::FrameDisplay::GetOffsetOfLength(), TyInt16, this->m_func);
  4377. instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  4378. instr->SetSrc1(indirOpnd);
  4379. instr->SetSrc2(IR::IntConstOpnd::New(0x1, TyInt8, this->m_func));
  4380. instrStScopedFld->InsertBefore(instr);
  4381. // JNE $helper
  4382. instr = IR::BranchInstr::New(Js::OpCode::JNE, labelHelper, this->m_func);
  4383. instrStScopedFld->InsertBefore(instr);
  4384. // MOV r1, [base + offset(scopes)] -- load the first scope
  4385. indirOpnd = IR::IndirOpnd::New(opndBase, Js::FrameDisplay::GetOffsetOfScopes(), TyMachReg, this->m_func);
  4386. r1 = IR::RegOpnd::New(TyMachReg, this->m_func);
  4387. instr = IR::Instr::New(Js::OpCode::MOV, r1, indirOpnd, this->m_func);
  4388. instrStScopedFld->InsertBefore(instr);
  4389. //first load the inlineCache type
  4390. inlineCache = IR::RegOpnd::New(TyMachPtr, this->m_func);
  4391. Assert(inlineCache != nullptr);
  4392. IR::RegOpnd * opndType = IR::RegOpnd::New(TyMachReg, this->m_func);
  4393. labelFallThru = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  4394. r1->m_sym->m_isNotInt = true;
  4395. // Load the type
  4396. this->m_lowerer->GenerateObjectTestAndTypeLoad(instrStScopedFld, r1, opndType, labelHelper);
  4397. // Check the local cache with the tagged type
  4398. IR::RegOpnd * opndTaggedType = IR::RegOpnd::New(TyMachReg, this->m_func);
  4399. GenerateLoadTaggedType(instrStScopedFld, opndType, opndTaggedType);
  4400. instrStScopedFld->InsertBefore(IR::Instr::New(Js::OpCode::MOV, inlineCache, m_lowerer->LoadRuntimeInlineCacheOpnd(instrStScopedFld, propertySymOpnd), this->m_func));
  4401. GenerateLocalInlineCacheCheck(instrStScopedFld, opndTaggedType, inlineCache, labelHelper);
  4402. GenerateStFldFromLocalInlineCache(instrStScopedFld, r1, newValue, inlineCache, labelFallThru, false);
  4403. // $helper:
  4404. // CALL PatchSetPropertyScoped(inlineCache, opndBase, propertyId, newValue, defaultInstance, scriptContext)
  4405. // $fallthru:
  4406. instrStScopedFld->InsertBefore(labelHelper);
  4407. instrStScopedFld->InsertAfter(labelFallThru);
  4408. return instrStScopedFld->m_prev;
  4409. }
  4410. IR::Opnd *
  4411. LowererMD::CreateStackArgumentsSlotOpnd()
  4412. {
  4413. StackSym *sym = StackSym::New(TyMachReg, this->m_func);
  4414. sym->m_offset = -MachArgsSlotOffset;
  4415. sym->m_allocated = true;
  4416. return IR::SymOpnd::New(sym, TyMachReg, this->m_func);
  4417. }
  4418. IR::RegOpnd *
  4419. LowererMD::GenerateUntagVar(IR::RegOpnd * src, IR::LabelInstr * labelFail, IR::Instr * assignInstr, bool generateTagCheck)
  4420. {
  4421. Assert(src->IsVar());
  4422. // MOV valueOpnd, index
  4423. IR::RegOpnd *valueOpnd = IR::RegOpnd::New(TyInt32, this->m_func);
  4424. //
  4425. // Convert Index to 32 bits.
  4426. //
  4427. IR::Opnd * opnd = src->UseWithNewType(TyMachReg, this->m_func);
  4428. #if INT32VAR
  4429. if (generateTagCheck)
  4430. {
  4431. Assert(!opnd->IsTaggedInt());
  4432. this->GenerateSmIntTest(opnd, assignInstr, labelFail);
  4433. }
  4434. // Moving into r2 clears the tag bits on AMD64.
  4435. IR::Instr * instr = IR::Instr::New(Js::OpCode::MOV_TRUNC, valueOpnd, opnd, this->m_func);
  4436. assignInstr->InsertBefore(instr);
  4437. #else
  4438. IR::Instr * instr = IR::Instr::New(Js::OpCode::MOV, valueOpnd, opnd, this->m_func);
  4439. assignInstr->InsertBefore(instr);
  4440. // SAR valueOpnd, Js::VarTag_Shift
  4441. instr = IR::Instr::New(Js::OpCode::SAR, valueOpnd, valueOpnd,
  4442. IR::IntConstOpnd::New(Js::VarTag_Shift, TyInt8, this->m_func), this->m_func);
  4443. assignInstr->InsertBefore(instr);
  4444. if (generateTagCheck)
  4445. {
  4446. Assert(!opnd->IsTaggedInt());
  4447. // SAR set the carry flag (CF) to 1 if the lower bit is 1
  4448. // JAE will jmp if CF = 0
  4449. instr = IR::BranchInstr::New(Js::OpCode::JAE, labelFail, this->m_func);
  4450. assignInstr->InsertBefore(instr);
  4451. }
  4452. #endif
  4453. return valueOpnd;
  4454. }
  4455. IR::RegOpnd *LowererMD::LoadNonnegativeIndex(
  4456. IR::RegOpnd *indexOpnd,
  4457. const bool skipNegativeCheck,
  4458. IR::LabelInstr *const notTaggedIntLabel,
  4459. IR::LabelInstr *const negativeLabel,
  4460. IR::Instr *const insertBeforeInstr)
  4461. {
  4462. Assert(indexOpnd);
  4463. Assert(indexOpnd->IsVar() || indexOpnd->GetType() == TyInt32 || indexOpnd->GetType() == TyUint32);
  4464. Assert(indexOpnd->GetType() != TyUint32 || skipNegativeCheck);
  4465. Assert(!indexOpnd->IsVar() || notTaggedIntLabel);
  4466. Assert(skipNegativeCheck || negativeLabel);
  4467. Assert(insertBeforeInstr);
  4468. if(indexOpnd->IsVar())
  4469. {
  4470. if (indexOpnd->GetValueType().IsLikelyFloat()
  4471. #ifdef _M_IX86
  4472. && AutoSystemInfo::Data.SSE2Available()
  4473. #endif
  4474. )
  4475. {
  4476. return m_lowerer->LoadIndexFromLikelyFloat(indexOpnd, skipNegativeCheck, notTaggedIntLabel, negativeLabel, insertBeforeInstr);
  4477. }
  4478. // mov intIndex, index
  4479. // sar intIndex, 1
  4480. // jae $notTaggedIntOrNegative
  4481. indexOpnd = GenerateUntagVar(indexOpnd, notTaggedIntLabel, insertBeforeInstr, !indexOpnd->IsTaggedInt());
  4482. }
  4483. if(!skipNegativeCheck)
  4484. {
  4485. // test index, index
  4486. // js $notTaggedIntOrNegative
  4487. Lowerer::InsertTestBranch(indexOpnd, indexOpnd, Js::OpCode::JSB, negativeLabel, insertBeforeInstr);
  4488. }
  4489. return indexOpnd;
  4490. }
  4491. IR::IndirOpnd *
  4492. LowererMD::GenerateFastElemIStringIndexCommon(IR::Instr * instrInsert, bool isStore, IR::IndirOpnd * indirOpnd, IR::LabelInstr * labelHelper)
  4493. {
  4494. IR::RegOpnd *indexOpnd = indirOpnd->GetIndexOpnd();
  4495. IR::RegOpnd *baseOpnd = indirOpnd->GetBaseOpnd();
  4496. Assert(baseOpnd != nullptr);
  4497. Assert(indexOpnd->GetValueType().IsLikelyString());
  4498. // Generates:
  4499. // CMP indexOpnd, PropertyString::`vtable' -- check if index is property string
  4500. // JNE $helper
  4501. // MOV propertyCacheOpnd, index->propCache
  4502. // TEST baseOpnd, AtomTag -- check base not tagged int
  4503. // JNE $helper
  4504. // MOV objectTypeOpnd, baseOpnd->type
  4505. // CMP [propertyCacheOpnd->type], objectTypeOpnd -- check if object type match the cache
  4506. // JNE $helper
  4507. // CMP [propertyCacheOpnd->isInlineSlot,1] -- check if it is inline slots
  4508. // JEQ $inlineSlot
  4509. // MOV slotOpnd, [baseOpnd->slot] -- load the aux slot
  4510. // JMP $afterLabel
  4511. // $inlineSlot:
  4512. // MOV slotOpnd, baseOpnd -- use the object as start of the slot offset
  4513. // $afterLabel:
  4514. // MOVZXW offsetOpnd, [propertyCacheOpnd->dataSlotIndex] -- load the slot index
  4515. // <use [slotOpnd + offsetOpnd * PtrSize]>
  4516. // CMP indexOpnd, PropertyString::`vtable' -- check if index is property string
  4517. // JNE $helper
  4518. this->m_lowerer->InsertCompareBranch(
  4519. IR::IndirOpnd::New(indexOpnd, 0, TyMachPtr, this->m_func),
  4520. m_lowerer->LoadVTableValueOpnd(instrInsert, VTableValue::VtablePropertyString),
  4521. Js::OpCode::BrNeq_A, labelHelper, instrInsert);
  4522. // MOV propertyCacheOpnd, indexOpnd->propCache
  4523. IR::RegOpnd * propertyCacheOpnd = IR::RegOpnd::New(TyMachPtr, this->m_func);
  4524. IR::Instr * loadPropertyCacheInstr = IR::Instr::New(Js::OpCode::MOV, propertyCacheOpnd,
  4525. IR::IndirOpnd::New(indexOpnd, Js::PropertyString::GetOffsetOfPropertyCache(), TyMachPtr,
  4526. this->m_func), this->m_func);
  4527. instrInsert->InsertBefore(loadPropertyCacheInstr);
  4528. // TEST baseOpnd, AtomTag -- check base not tagged int
  4529. // JNE $helper
  4530. if(!baseOpnd->IsNotTaggedValue())
  4531. {
  4532. GenerateObjectTest(baseOpnd, instrInsert, labelHelper);
  4533. }
  4534. // MOV s2, baseOpnd->type
  4535. // CMP [propertyCacheOpnd->type], s2 -- check if object type match the cache
  4536. // JNE $helper
  4537. IR::RegOpnd * objectTypeOpnd = IR::RegOpnd::New(TyMachPtr, this->m_func);
  4538. IR::Instr * loadObjectTypeInstr = IR::Instr::New(Js::OpCode::MOV,
  4539. objectTypeOpnd, IR::IndirOpnd::New(baseOpnd, Js::RecyclableObject::GetOffsetOfType(), TyMachPtr, this->m_func),
  4540. this->m_func);
  4541. instrInsert->InsertBefore(loadObjectTypeInstr);
  4542. IR::Instr * checkTypeInstr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  4543. checkTypeInstr->SetSrc1(IR::IndirOpnd::New(propertyCacheOpnd, (int32)offsetof(Js::PropertyCache, type), TyMachPtr, this->m_func));
  4544. checkTypeInstr->SetSrc2(objectTypeOpnd);
  4545. instrInsert->InsertBefore(checkTypeInstr);
  4546. instrInsert->InsertBefore(IR::BranchInstr::New(Js::OpCode::JNE, labelHelper, this->m_func));
  4547. if (isStore)
  4548. {
  4549. IR::IndirOpnd* isStoreEnabledOpnd = IR::IndirOpnd::New(propertyCacheOpnd, (int32)offsetof(Js::PropertyCache, isStoreFieldEnabled), TyInt8, this->m_func);
  4550. IR::IntConstOpnd* zeroOpnd = IR::IntConstOpnd::New(0, TyInt8, this->m_func, /* dontEncode = */ true);
  4551. this->m_lowerer->InsertCompareBranch(isStoreEnabledOpnd, zeroOpnd, Js::OpCode::BrEq_A, labelHelper, instrInsert);
  4552. }
  4553. // CMP [propertyCacheOpnd->isInlineSlot,1] -- check if it is inline slots
  4554. // JEQ $inlineSlot
  4555. IR::Instr * inlineSlotTestInstr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  4556. inlineSlotTestInstr->SetSrc1(IR::IndirOpnd::New(propertyCacheOpnd, (int32)offsetof(Js::PropertyCache, isInlineSlot), TyInt8, this->m_func));
  4557. inlineSlotTestInstr->SetSrc2(IR::IntConstOpnd::New(1, TyInt8, this->m_func));
  4558. instrInsert->InsertBefore(inlineSlotTestInstr);
  4559. IR::LabelInstr * isInlineSlotLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  4560. instrInsert->InsertBefore(IR::BranchInstr::New(Js::OpCode::JEQ, isInlineSlotLabel, this->m_func));
  4561. // MOV slotOpnd, [baseOpnd->slot] -- load the aux slot
  4562. // JMP $afterLabel
  4563. IR::RegOpnd * slotOpnd = IR::RegOpnd::New(TyMachPtr, this->m_func);
  4564. instrInsert->InsertBefore(IR::Instr::New(Js::OpCode::MOV, slotOpnd,
  4565. IR::IndirOpnd::New(baseOpnd, Js::DynamicObject::GetOffsetOfAuxSlots(), TyMachPtr, this->m_func), this->m_func));
  4566. IR::LabelInstr * afterLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  4567. instrInsert->InsertBefore(IR::BranchInstr::New(Js::OpCode::JMP, afterLabel, this->m_func));
  4568. // $inlineSlot:
  4569. // MOV slotOpnd, baseOpnd -- use the object as start of the slot offset
  4570. instrInsert->InsertBefore(isInlineSlotLabel);
  4571. instrInsert->InsertBefore(IR::Instr::New(Js::OpCode::MOV, slotOpnd, baseOpnd, this->m_func));
  4572. // $afterLabel:
  4573. // MOVZXW offsetOpnd, [propertyCacheOpnd->dataSlotIndex] -- load the slot index
  4574. instrInsert->InsertBefore(afterLabel);
  4575. IR::RegOpnd * offsetOpnd = IR::RegOpnd::New(TyInt32, this->m_func);
  4576. instrInsert->InsertBefore(IR::Instr::New(Js::OpCode::MOVZXW, offsetOpnd,
  4577. IR::IndirOpnd::New(propertyCacheOpnd, (int32)offsetof(Js::PropertyCache, dataSlotIndex), TyUint16, this->m_func), this->m_func));
  4578. // return [slotOpnd + offsetOpnd * PtrSize]
  4579. return IR::IndirOpnd::New(slotOpnd, offsetOpnd, this->GetDefaultIndirScale(), TyVar, this->m_func);
  4580. }
  4581. // Inlines fast-path for int Mul/Add or int Mul/Sub. If not int, call MulAdd/MulSub helper
  4582. bool LowererMD::TryGenerateFastMulAdd(IR::Instr * instrAdd, IR::Instr ** pInstrPrev)
  4583. {
  4584. IR::Instr *instrMul = instrAdd->GetPrevRealInstrOrLabel();
  4585. IR::Opnd *addSrc;
  4586. IR::RegOpnd *addCommonSrcOpnd;
  4587. Assert(instrAdd->m_opcode == Js::OpCode::Add_A || instrAdd->m_opcode == Js::OpCode::Sub_A);
  4588. bool isSub = (instrAdd->m_opcode == Js::OpCode::Sub_A) ? true : false;
  4589. // Mul needs to be a single def reg
  4590. if (instrMul->m_opcode != Js::OpCode::Mul_A || instrMul->GetDst()->IsRegOpnd() == false)
  4591. {
  4592. // Cannot generate MulAdd
  4593. return false;
  4594. }
  4595. if (instrMul->HasBailOutInfo())
  4596. {
  4597. // Bailout will be generated for the Add, but not the Mul.
  4598. // We could handle this, but this path isn't used that much anymore.
  4599. return false;
  4600. }
  4601. IR::RegOpnd *regMulDst = instrMul->GetDst()->AsRegOpnd();
  4602. if (regMulDst->m_sym->m_isSingleDef == false)
  4603. {
  4604. // Cannot generate MulAdd
  4605. return false;
  4606. }
  4607. // Only handle a * b + c, so dst of Mul needs to match left source of Add
  4608. if (instrMul->GetDst()->IsEqual(instrAdd->GetSrc1()))
  4609. {
  4610. addCommonSrcOpnd = instrAdd->GetSrc1()->AsRegOpnd();
  4611. addSrc = instrAdd->GetSrc2();
  4612. }
  4613. else if (instrMul->GetDst()->IsEqual(instrAdd->GetSrc2()))
  4614. {
  4615. addSrc = instrAdd->GetSrc1();
  4616. addCommonSrcOpnd = instrAdd->GetSrc2()->AsRegOpnd();
  4617. }
  4618. else
  4619. {
  4620. return false;
  4621. }
  4622. // Only handle a * b + c where c != a * b
  4623. if (instrAdd->GetSrc1()->IsEqual(instrAdd->GetSrc2()))
  4624. {
  4625. return false;
  4626. }
  4627. if (addCommonSrcOpnd->m_isTempLastUse == false)
  4628. {
  4629. return false;
  4630. }
  4631. IR::Opnd *mulSrc1 = instrMul->GetSrc1();
  4632. IR::Opnd *mulSrc2 = instrMul->GetSrc2();
  4633. if (mulSrc1->IsRegOpnd() && mulSrc1->AsRegOpnd()->IsTaggedInt()
  4634. && mulSrc2->IsRegOpnd() && mulSrc2->AsRegOpnd()->IsTaggedInt())
  4635. {
  4636. return false;
  4637. }
  4638. // Save prevInstr for the main lower loop
  4639. *pInstrPrev = instrMul->m_prev;
  4640. // Generate int31 fast-path for Mul, go to MulAdd helper if it fails, or one of the source is marked notInt
  4641. if (!(addSrc->IsRegOpnd() && addSrc->AsRegOpnd()->IsNotInt())
  4642. && !(mulSrc1->IsRegOpnd() && mulSrc1->AsRegOpnd()->IsNotInt())
  4643. && !(mulSrc2->IsRegOpnd() && mulSrc2->AsRegOpnd()->IsNotInt()))
  4644. {
  4645. this->GenerateFastMul(instrMul);
  4646. IR::LabelInstr *labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  4647. IR::Instr *instr = IR::BranchInstr::New(Js::OpCode::JMP, labelHelper, this->m_func);
  4648. instrMul->InsertBefore(instr);
  4649. // Generate int31 fast-path for Add
  4650. bool success;
  4651. if (isSub)
  4652. {
  4653. success = this->GenerateFastSub(instrAdd);
  4654. }
  4655. else
  4656. {
  4657. success = this->GenerateFastAdd(instrAdd);
  4658. }
  4659. if (!success)
  4660. {
  4661. labelHelper->isOpHelper = false;
  4662. }
  4663. // Generate MulAdd helper call
  4664. instrAdd->InsertBefore(labelHelper);
  4665. }
  4666. if (instrAdd->dstIsTempNumber)
  4667. {
  4668. m_lowerer->LoadHelperTemp(instrAdd, instrAdd);
  4669. }
  4670. else
  4671. {
  4672. IR::Opnd *tempOpnd = IR::IntConstOpnd::New(0, TyInt32, this->m_func);
  4673. this->LoadHelperArgument(instrAdd, tempOpnd);
  4674. }
  4675. this->m_lowerer->LoadScriptContext(instrAdd);
  4676. IR::JnHelperMethod helper;
  4677. if (addSrc == instrAdd->GetSrc2())
  4678. {
  4679. instrAdd->FreeSrc1();
  4680. IR::Opnd *addOpnd = instrAdd->UnlinkSrc2();
  4681. this->LoadHelperArgument(instrAdd, addOpnd);
  4682. helper = isSub ? IR::HelperOp_MulSubRight : IR::HelperOp_MulAddRight;
  4683. }
  4684. else
  4685. {
  4686. instrAdd->FreeSrc2();
  4687. IR::Opnd *addOpnd = instrAdd->UnlinkSrc1();
  4688. this->LoadHelperArgument(instrAdd, addOpnd);
  4689. helper = isSub ? IR::HelperOp_MulSubLeft : IR::HelperOp_MulAddLeft;
  4690. }
  4691. IR::Opnd *src2 = instrMul->UnlinkSrc2();
  4692. this->LoadHelperArgument(instrAdd, src2);
  4693. IR::Opnd *src1 = instrMul->UnlinkSrc1();
  4694. this->LoadHelperArgument(instrAdd, src1);
  4695. this->ChangeToHelperCall(instrAdd, helper);
  4696. instrMul->Remove();
  4697. return true;
  4698. }
  4699. void
  4700. LowererMD::GenerateFastAbs(IR::Opnd *dst, IR::Opnd *src, IR::Instr *callInstr, IR::Instr *insertInstr, IR::LabelInstr *labelHelper, IR::LabelInstr *doneLabel)
  4701. {
  4702. // TEST src1, AtomTag
  4703. // JEQ $float
  4704. // MOV EAX, src
  4705. // SAR EAX, AtomTag_Int32
  4706. // CDQ
  4707. // XOR EAX, EDX
  4708. // SUB EAX, EDX
  4709. // SHL EAX, AtomTag_Int32
  4710. // JO $labelHelper
  4711. // INC EAX
  4712. // MOV dst, EAX
  4713. // JMP $done
  4714. // $float
  4715. // CMP [src], JavascriptNumber.vtable
  4716. // JNE $helper
  4717. // MOVSD r1, [src + offsetof(value)]
  4718. // ANDPD r1, absDoubleCst
  4719. // dst = DoubleToVar(r1)
  4720. IR::Instr *instr = nullptr;
  4721. IR::LabelInstr *labelFloat = nullptr;
  4722. bool isInt = false;
  4723. bool isNotInt = false;
  4724. if (src->IsRegOpnd())
  4725. {
  4726. if (src->AsRegOpnd()->IsTaggedInt())
  4727. {
  4728. isInt = true;
  4729. }
  4730. else if (src->AsRegOpnd()->IsNotInt())
  4731. {
  4732. isNotInt = true;
  4733. }
  4734. }
  4735. else if (src->IsAddrOpnd())
  4736. {
  4737. IR::AddrOpnd *varOpnd = src->AsAddrOpnd();
  4738. Assert(varOpnd->IsVar() && Js::TaggedInt::Is(varOpnd->m_address));
  4739. #ifdef _M_X64
  4740. __int64 absValue = ::_abs64(Js::TaggedInt::ToInt32(varOpnd->m_address));
  4741. #else
  4742. __int32 absValue = ::abs(Js::TaggedInt::ToInt32(varOpnd->m_address));
  4743. #endif
  4744. if (!Js::TaggedInt::IsOverflow(absValue))
  4745. {
  4746. varOpnd->SetAddress(Js::TaggedInt::ToVarUnchecked((__int32)absValue), IR::AddrOpndKindConstantVar);
  4747. instr = IR::Instr::New(Js::OpCode::MOV, dst, varOpnd, this->m_func);
  4748. insertInstr->InsertBefore(instr);
  4749. return;
  4750. }
  4751. }
  4752. if (src->IsRegOpnd() == false)
  4753. {
  4754. IR::RegOpnd *regOpnd = IR::RegOpnd::New(TyVar, this->m_func);
  4755. instr = IR::Instr::New(Js::OpCode::MOV, regOpnd, src, this->m_func);
  4756. insertInstr->InsertBefore(instr);
  4757. src = regOpnd;
  4758. }
  4759. #ifdef _M_IX86
  4760. bool emitFloatAbs = !isInt && AutoSystemInfo::Data.SSE2Available();
  4761. #else
  4762. bool emitFloatAbs = !isInt;
  4763. #endif
  4764. if (!isNotInt)
  4765. {
  4766. if (!isInt)
  4767. {
  4768. IR::LabelInstr *label = labelHelper;
  4769. if (emitFloatAbs)
  4770. {
  4771. label = labelFloat = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  4772. }
  4773. GenerateSmIntTest(src, insertInstr, label);
  4774. }
  4775. // MOV EAX, src
  4776. IR::RegOpnd *regEAX = IR::RegOpnd::New(TyInt32, this->m_func);
  4777. regEAX->SetReg(LowererMDArch::GetRegIMulDestLower());
  4778. instr = IR::Instr::New(Js::OpCode::MOV, regEAX, src, this->m_func);
  4779. insertInstr->InsertBefore(instr);
  4780. #ifdef _M_IX86
  4781. // SAR EAX, AtomTag_Int32
  4782. instr = IR::Instr::New(Js::OpCode::SAR, regEAX, regEAX, IR::IntConstOpnd::New(Js::AtomTag_Int32, TyInt32, this->m_func), this->m_func);
  4783. insertInstr->InsertBefore(instr);
  4784. #endif
  4785. IR::RegOpnd *regEDX = IR::RegOpnd::New(TyInt32, this->m_func);
  4786. regEDX->SetReg(LowererMDArch::GetRegIMulHighDestLower());
  4787. // CDQ
  4788. // Note: put EDX on dst to give of def to the EDX lifetime
  4789. instr = IR::Instr::New(Js::OpCode::CDQ, regEDX, this->m_func);
  4790. insertInstr->InsertBefore(instr);
  4791. // XOR EAX, EDX
  4792. instr = IR::Instr::New(Js::OpCode::XOR, regEAX, regEAX, regEDX, this->m_func);
  4793. insertInstr->InsertBefore(instr);
  4794. // SUB EAX, EDX
  4795. instr = IR::Instr::New(Js::OpCode::SUB, regEAX, regEAX, regEDX, this->m_func);
  4796. insertInstr->InsertBefore(instr);
  4797. #ifdef _M_X64
  4798. // abs(INT_MIN) overflows a 32 bit integer.
  4799. // JO $labelHelper
  4800. instr = IR::BranchInstr::New(Js::OpCode::JO, labelHelper, this->m_func);
  4801. insertInstr->InsertBefore(instr);
  4802. #endif
  4803. #ifdef _M_IX86
  4804. // SHL EAX, AtomTag_Int32
  4805. instr = IR::Instr::New(Js::OpCode::SHL, regEAX, regEAX, IR::IntConstOpnd::New(Js::AtomTag_Int32, TyInt32, this->m_func), this->m_func);
  4806. insertInstr->InsertBefore(instr);
  4807. // JO $labelHelper
  4808. instr = IR::BranchInstr::New(Js::OpCode::JO, labelHelper, this->m_func);
  4809. insertInstr->InsertBefore(instr);
  4810. // INC EAX
  4811. instr = IR::Instr::New(Js::OpCode::INC, regEAX, regEAX, this->m_func);
  4812. insertInstr->InsertBefore(instr);
  4813. #endif
  4814. // MOV dst, EAX
  4815. instr = IR::Instr::New(Js::OpCode::MOV, dst, regEAX, this->m_func);
  4816. insertInstr->InsertBefore(instr);
  4817. #ifdef _M_X64
  4818. GenerateInt32ToVarConversion(dst, insertInstr);
  4819. #endif
  4820. }
  4821. if (labelFloat)
  4822. {
  4823. // JMP $done
  4824. instr = IR::BranchInstr::New(Js::OpCode::JMP, doneLabel, this->m_func);
  4825. insertInstr->InsertBefore(instr);
  4826. // $float
  4827. insertInstr->InsertBefore(labelFloat);
  4828. }
  4829. if (emitFloatAbs)
  4830. {
  4831. #if defined(_M_IX86)
  4832. // CMP [src], JavascriptNumber.vtable
  4833. IR::Opnd *opnd = IR::IndirOpnd::New(src->AsRegOpnd(), (int32)0, TyMachPtr, this->m_func);
  4834. instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  4835. instr->SetSrc1(opnd);
  4836. instr->SetSrc2(m_lowerer->LoadVTableValueOpnd(insertInstr, VTableValue::VtableJavascriptNumber));
  4837. insertInstr->InsertBefore(instr);
  4838. // JNE $helper
  4839. instr = IR::BranchInstr::New(Js::OpCode::JNE, labelHelper, this->m_func);
  4840. insertInstr->InsertBefore(instr);
  4841. // MOVSD r1, [src + offsetof(value)]
  4842. opnd = IR::IndirOpnd::New(src->AsRegOpnd(), Js::JavascriptNumber::GetValueOffset(), TyMachDouble, this->m_func);
  4843. IR::RegOpnd *regOpnd = IR::RegOpnd::New(TyMachDouble, this->m_func);
  4844. instr = IR::Instr::New(Js::OpCode::MOVSD, regOpnd, opnd, this->m_func);
  4845. insertInstr->InsertBefore(instr);
  4846. this->GenerateFloatAbs(regOpnd, insertInstr);
  4847. // dst = DoubleToVar(r1)
  4848. SaveDoubleToVar(callInstr->GetDst()->AsRegOpnd(), regOpnd, callInstr, insertInstr);
  4849. #elif defined(_M_X64)
  4850. // if (typeof(src) == double)
  4851. IR::RegOpnd *src64 = src->AsRegOpnd();
  4852. GenerateFloatTest(src64, insertInstr, labelHelper);
  4853. // dst64 = MOV src64
  4854. insertInstr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, dst, src64, this->m_func));
  4855. // Unconditionally set the sign bit. This will get XORd away when we remove the tag.
  4856. // dst64 = OR 0x8000000000000000
  4857. insertInstr->InsertBefore(IR::Instr::New(Js::OpCode::OR, dst, dst, IR::IntConstOpnd::New(MachSignBit, TyMachReg, this->m_func), this->m_func));
  4858. #endif
  4859. }
  4860. else if(!isInt)
  4861. {
  4862. // The source is not known to be a tagged int, so either it's definitely not an int (isNotInt), or the int version of
  4863. // abs failed the tag check and jumped here. We can't emit the float version of abs (!emitFloatAbs) due to SSE2 not
  4864. // being available, so jump straight to the helper.
  4865. // JMP $helper
  4866. instr = IR::BranchInstr::New(Js::OpCode::JMP, labelHelper, this->m_func);
  4867. insertInstr->InsertBefore(instr);
  4868. }
  4869. }
  4870. IR::Instr * LowererMD::GenerateFloatAbs(IR::RegOpnd * regOpnd, IR::Instr * insertInstr)
  4871. {
  4872. // ANDPS reg, absDoubleCst
  4873. IR::Opnd * opnd;
  4874. if (regOpnd->IsFloat64())
  4875. {
  4876. opnd = m_lowerer->LoadLibraryValueOpnd(insertInstr, LibraryValue::ValueAbsDoubleCst);
  4877. }
  4878. else
  4879. {
  4880. Assert(regOpnd->IsFloat32());
  4881. opnd = IR::MemRefOpnd::New(m_func->GetThreadContextInfo()->GetAbsFloatCstAddr(), TyFloat32, this->m_func, IR::AddrOpndKindDynamicFloatRef);
  4882. }
  4883. // ANDPS has smaller encoding then ANDPD
  4884. IR::Instr * instr = IR::Instr::New(Js::OpCode::ANDPS, regOpnd, regOpnd, opnd, this->m_func);
  4885. insertInstr->InsertBefore(instr);
  4886. Legalize(instr);
  4887. return instr;
  4888. }
  4889. bool LowererMD::GenerateFastCharAt(Js::BuiltinFunction index, IR::Opnd *dst, IR::Opnd *srcStr, IR::Opnd *srcIndex, IR::Instr *callInstr,
  4890. IR::Instr *insertInstr, IR::LabelInstr *labelHelper, IR::LabelInstr *doneLabel)
  4891. {
  4892. // if regSrcStr is not object, JMP $helper
  4893. // CMP [regSrcStr + offset(type)] , static string type -- check base string type
  4894. // JNE $helper
  4895. // MOV r1, [regSrcStr + offset(m_pszValue)]
  4896. // TEST r1, r1
  4897. // JEQ $helper
  4898. // MOV r2, srcIndex
  4899. // If r2 is not int, JMP $helper
  4900. // Convert r2 to int
  4901. // CMP [regSrcStr + offsetof(length)], r2
  4902. // JBE $helper
  4903. // MOVZX r2, [r1 + r2 * 2]
  4904. // if (charAt)
  4905. // PUSH r1
  4906. // PUSH scriptContext
  4907. // CALL GetStringFromChar
  4908. // MOV dst, EAX
  4909. // else (charCodeAt)
  4910. // if (codePointAt)
  4911. // Lowerer.GenerateFastCodePointAt -- Common inline functions
  4912. // Convert r2 to Var
  4913. // MOV dst, r2
  4914. bool isInt = false;
  4915. bool isNotTaggedValue = false;
  4916. IR::Instr *instr;
  4917. IR::RegOpnd *regSrcStr;
  4918. if (srcStr->IsRegOpnd())
  4919. {
  4920. if (srcStr->AsRegOpnd()->IsTaggedInt())
  4921. {
  4922. isInt = true;
  4923. }
  4924. else if (srcStr->AsRegOpnd()->IsNotTaggedValue())
  4925. {
  4926. isNotTaggedValue = true;
  4927. }
  4928. }
  4929. if (srcStr->IsRegOpnd() == false)
  4930. {
  4931. IR::RegOpnd *regOpnd = IR::RegOpnd::New(TyVar, this->m_func);
  4932. instr = IR::Instr::New(Js::OpCode::MOV, regOpnd, srcStr, this->m_func);
  4933. insertInstr->InsertBefore(instr);
  4934. regSrcStr = regOpnd;
  4935. }
  4936. else
  4937. {
  4938. regSrcStr = srcStr->AsRegOpnd();
  4939. }
  4940. if (!isNotTaggedValue)
  4941. {
  4942. if (!isInt)
  4943. {
  4944. GenerateObjectTest(regSrcStr, insertInstr, labelHelper);
  4945. }
  4946. else
  4947. {
  4948. // Insert delete branch opcode to tell the dbChecks not to assert on this helper label
  4949. IR::Instr *fakeBr = IR::PragmaInstr::New(Js::OpCode::DeletedNonHelperBranch, 0, this->m_func);
  4950. insertInstr->InsertBefore(fakeBr);
  4951. instr = IR::BranchInstr::New(Js::OpCode::JMP, labelHelper, this->m_func);
  4952. insertInstr->InsertBefore(instr);
  4953. }
  4954. }
  4955. // Bail out if index a constant and is less than zero.
  4956. if (srcIndex->IsAddrOpnd() && Js::TaggedInt::ToInt32(srcIndex->AsAddrOpnd()->m_address) < 0)
  4957. {
  4958. labelHelper->isOpHelper = false;
  4959. instr = IR::BranchInstr::New(Js::OpCode::JMP, labelHelper, this->m_func);
  4960. insertInstr->InsertBefore(instr);
  4961. return false;
  4962. }
  4963. this->m_lowerer->GenerateStringTest(regSrcStr, insertInstr, labelHelper, nullptr, false);
  4964. // r1 contains the value of the char16* pointer inside JavascriptString.
  4965. // MOV r1, [regSrcStr + offset(m_pszValue)]
  4966. IR::RegOpnd *r1 = IR::RegOpnd::New(TyMachReg, this->m_func);
  4967. IR::IndirOpnd * indirOpnd = IR::IndirOpnd::New(regSrcStr->AsRegOpnd(), Js::JavascriptString::GetOffsetOfpszValue(), TyMachPtr, this->m_func);
  4968. instr = IR::Instr::New(Js::OpCode::MOV, r1, indirOpnd, this->m_func);
  4969. insertInstr->InsertBefore(instr);
  4970. // TEST r1, r1 -- Null pointer test
  4971. instr = IR::Instr::New(Js::OpCode::TEST, this->m_func);
  4972. instr->SetSrc1(r1);
  4973. instr->SetSrc2(r1);
  4974. insertInstr->InsertBefore(instr);
  4975. // JEQ $helper
  4976. instr = IR::BranchInstr::New(Js::OpCode::JEQ, labelHelper, this->m_func);
  4977. insertInstr->InsertBefore(instr);
  4978. IR::IndirOpnd *strLength = IR::IndirOpnd::New(regSrcStr, offsetof(Js::JavascriptString, m_charLength), TyUint32, this->m_func);
  4979. if (srcIndex->IsAddrOpnd())
  4980. {
  4981. // CMP [regSrcStr + offsetof(length)], index
  4982. instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  4983. instr->SetSrc1(strLength);
  4984. instr->SetSrc2(IR::IntConstOpnd::New(Js::TaggedInt::ToUInt32(srcIndex->AsAddrOpnd()->m_address), TyUint32, this->m_func));
  4985. insertInstr->InsertBefore(instr);
  4986. // Use unsigned compare, this should handle negative indexes as well (they become > INT_MAX)
  4987. // JBE $helper
  4988. instr = IR::BranchInstr::New(Js::OpCode::JBE, labelHelper, this->m_func);
  4989. insertInstr->InsertBefore(instr);
  4990. indirOpnd = IR::IndirOpnd::New(r1, Js::TaggedInt::ToUInt32(srcIndex->AsAddrOpnd()->m_address) * sizeof(char16), TyInt16, this->m_func);
  4991. }
  4992. else
  4993. {
  4994. IR::RegOpnd *r2 = IR::RegOpnd::New(TyVar, this->m_func);
  4995. // MOV r2, srcIndex
  4996. instr = IR::Instr::New(Js::OpCode::MOV, r2, srcIndex, this->m_func);
  4997. insertInstr->InsertBefore(instr);
  4998. if (!srcIndex->IsRegOpnd() || !srcIndex->AsRegOpnd()->IsTaggedInt())
  4999. {
  5000. GenerateSmIntTest(r2, insertInstr, labelHelper);
  5001. }
  5002. #if INT32VAR
  5003. // Remove the tag
  5004. // MOV r2, [32-bit] r2
  5005. IR::Opnd * r2_32 = r2->UseWithNewType(TyInt32, this->m_func);
  5006. instr = IR::Instr::New(Js::OpCode::MOVSXD, r2, r2_32, this->m_func);
  5007. insertInstr->InsertBefore(instr);
  5008. r2 = r2_32->AsRegOpnd();
  5009. #else
  5010. // r2 = SAR r2, VarTag_Shift
  5011. instr = IR::Instr::New(Js::OpCode::SAR, r2, r2, IR::IntConstOpnd::New(Js::VarTag_Shift, TyInt8, this->m_func), this->m_func);
  5012. insertInstr->InsertBefore(instr);
  5013. #endif
  5014. // CMP [regSrcStr + offsetof(length)], r2
  5015. instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  5016. instr->SetSrc1(strLength);
  5017. instr->SetSrc2(r2);
  5018. insertInstr->InsertBefore(instr);
  5019. if (r2->GetSize() != MachPtr)
  5020. {
  5021. r2 = r2->UseWithNewType(TyMachPtr, this->m_func)->AsRegOpnd();
  5022. }
  5023. // Use unsigned compare, this should handle negative indexes as well (they become > INT_MAX)
  5024. // JBE $helper
  5025. instr = IR::BranchInstr::New(Js::OpCode::JBE, labelHelper, this->m_func);
  5026. insertInstr->InsertBefore(instr);
  5027. indirOpnd = IR::IndirOpnd::New(r1, r2, 1, TyInt16, this->m_func);
  5028. }
  5029. // MOVZX charReg, [r1 + r2 * 2] -- this is the value of the char
  5030. IR::RegOpnd *charReg = IR::RegOpnd::New(TyMachReg, this->m_func);
  5031. instr = IR::Instr::New(Js::OpCode::MOVZXW, charReg, indirOpnd, this->m_func);
  5032. insertInstr->InsertBefore(instr);
  5033. if (index == Js::BuiltinFunction::JavascriptString_CharAt)
  5034. {
  5035. IR::Opnd *resultOpnd;
  5036. if (dst->IsEqual(srcStr))
  5037. {
  5038. resultOpnd = IR::RegOpnd::New(TyVar, this->m_func);
  5039. }
  5040. else
  5041. {
  5042. resultOpnd = dst;
  5043. }
  5044. this->m_lowerer->GenerateGetSingleCharString(charReg, resultOpnd, labelHelper, doneLabel, insertInstr, false);
  5045. }
  5046. else
  5047. {
  5048. Assert(index == Js::BuiltinFunction::JavascriptString_CharCodeAt || index == Js::BuiltinFunction::JavascriptString_CodePointAt);
  5049. if (index == Js::BuiltinFunction::JavascriptString_CodePointAt)
  5050. {
  5051. this->m_lowerer->GenerateFastInlineStringCodePointAt(insertInstr, this->m_func, strLength, srcIndex, charReg, r1);
  5052. }
  5053. GenerateInt32ToVarConversion(charReg, insertInstr);
  5054. // MOV dst, charReg
  5055. instr = IR::Instr::New(Js::OpCode::MOV, dst, charReg, this->m_func);
  5056. insertInstr->InsertBefore(instr);
  5057. }
  5058. return true;
  5059. }
  5060. IR::RegOpnd* LowererMD::MaterializeDoubleConstFromInt(intptr_t constAddr, IR::Instr* instr)
  5061. {
  5062. IR::Opnd* constVal = IR::MemRefOpnd::New(constAddr, IRType::TyFloat64, this->m_func);
  5063. IR::RegOpnd * xmmReg = IR::RegOpnd::New(TyFloat64, m_func);
  5064. this->m_lowerer->InsertMove(xmmReg, constVal, instr);
  5065. return xmmReg;
  5066. }
  5067. IR::RegOpnd* LowererMD::MaterializeConstFromBits(int bits, IRType type, IR::Instr* instr)
  5068. {
  5069. IR::Opnd * regBits = IR::RegOpnd::New(TyInt32, m_func);
  5070. this->m_lowerer->InsertMove(regBits, IR::IntConstOpnd::New(bits, TyInt32, m_func), instr);
  5071. IR::RegOpnd * regConst = IR::RegOpnd::New(type, m_func);
  5072. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOVD, regConst, regBits, m_func));
  5073. return regConst;
  5074. }
  5075. IR::Opnd* LowererMD::Subtract2To31(IR::Opnd* src1, IR::Opnd* intMinFP, IRType type, IR::Instr* instr)
  5076. {
  5077. Js::OpCode op = (type == TyFloat32) ? Js::OpCode::SUBSS : Js::OpCode::SUBSD;
  5078. IR::Opnd* adjSrc = IR::RegOpnd::New(type, m_func);
  5079. IR::Instr* sub = IR::Instr::New(op, adjSrc, src1, intMinFP, m_func);
  5080. instr->InsertBefore(sub);
  5081. Legalize(sub);
  5082. return adjSrc;
  5083. }
  5084. IR::Opnd* LowererMD::GenerateTruncChecks(IR::Instr* instr)
  5085. {
  5086. IR::LabelInstr * conversion = IR::LabelInstr::New(Js::OpCode::Label, m_func);
  5087. IR::LabelInstr * throwLabel = IR::LabelInstr::New(Js::OpCode::Label, m_func);
  5088. IR::Opnd* src1 = instr->GetSrc1();
  5089. IR::Opnd * src64 = nullptr;
  5090. if (src1->IsFloat32())
  5091. {
  5092. src64 = IR::RegOpnd::New(TyFloat64, m_func);
  5093. EmitFloat32ToFloat64(src64, src1, instr);
  5094. }
  5095. else
  5096. {
  5097. src64 = src1;
  5098. }
  5099. IR::RegOpnd* limitReg = MaterializeDoubleConstFromInt(instr->GetDst()->IsUInt32() ?
  5100. m_func->GetThreadContextInfo()->GetDoubleNegOneAddr() :
  5101. m_func->GetThreadContextInfo()->GetDoubleIntMinMinusOneAddr(), instr);
  5102. m_lowerer->InsertCompareBranch(src64, limitReg, Js::OpCode::BrLe_A, throwLabel, instr);
  5103. limitReg = MaterializeDoubleConstFromInt(instr->GetDst()->IsUInt32() ?
  5104. m_func->GetThreadContextInfo()->GetDoubleUintMaxPlusOneAddr() :
  5105. m_func->GetThreadContextInfo()->GetDoubleIntMaxPlusOneAddr(), instr);
  5106. m_lowerer->InsertCompareBranch(limitReg, src64, Js::OpCode::BrGt_A, conversion, instr, true /*no NaN check*/);
  5107. instr->InsertBefore(throwLabel);
  5108. this->m_lowerer->GenerateThrow(IR::IntConstOpnd::New(SCODE_CODE(VBSERR_Overflow), TyInt32, m_func), instr);
  5109. //no jump here we aren't coming back
  5110. instr->InsertBefore(conversion);
  5111. return src64;
  5112. }
  5113. void
  5114. LowererMD::GenerateTruncWithCheck(IR::Instr * instr)
  5115. {
  5116. Assert(AutoSystemInfo::Data.SSE2Available());
  5117. IR::Opnd* src64 = GenerateTruncChecks(instr); //converts src to double and checks if MIN <= src <= MAX
  5118. IR::Opnd* dst = instr->GetDst();
  5119. if (dst->IsUnsigned())
  5120. {
  5121. m_lowerer->InsertMove(dst, IR::IntConstOpnd::New(0, TyUint32, m_func), instr);
  5122. IR::LabelInstr * skipUnsignedPart = IR::LabelInstr::New(Js::OpCode::Label, m_func);
  5123. IR::Opnd* twoTo31 = MaterializeDoubleConstFromInt(m_func->GetThreadContextInfo()->GetDoubleTwoTo31Addr(), instr);
  5124. m_lowerer->InsertCompareBranch(src64, twoTo31, Js::OpCode::BrLt_A, skipUnsignedPart, instr);
  5125. instr->InsertBefore(IR::Instr::New(Js::OpCode::SUBPD, src64, src64, twoTo31, m_func));
  5126. m_lowerer->InsertMove(dst, IR::IntConstOpnd::New(0x80000000 /*2^31*/, TyUint32, m_func), instr);
  5127. instr->InsertBefore(skipUnsignedPart);
  5128. IR::Opnd* tmp = IR::RegOpnd::New(TyInt32, m_func);
  5129. instr->InsertBefore(IR::Instr::New(Js::OpCode::CVTTSD2SI, tmp, src64, m_func));
  5130. instr->InsertBefore(IR::Instr::New(Js::OpCode::ADD, dst, dst, tmp, m_func));
  5131. }
  5132. else
  5133. {
  5134. instr->InsertBefore(IR::Instr::New(Js::OpCode::CVTTSD2SI, dst, src64, m_func));
  5135. }
  5136. instr->UnlinkSrc1();
  5137. instr->UnlinkDst();
  5138. instr->Remove();
  5139. }
  5140. void
  5141. LowererMD::GenerateCtz(IR::Instr * instr)
  5142. {
  5143. Assert(instr->GetSrc1()->IsInt32() || instr->GetSrc1()->IsUInt32() || instr->GetSrc1()->IsInt64());
  5144. Assert(IRType_IsNativeInt(instr->GetDst()->GetType()));
  5145. #ifdef _M_IX86
  5146. if (instr->GetSrc1()->IsInt64())
  5147. {
  5148. lowererMDArch.EmitInt64Instr(instr);
  5149. return;
  5150. }
  5151. #endif
  5152. if (AutoSystemInfo::Data.TZCntAvailable())
  5153. {
  5154. instr->m_opcode = Js::OpCode::TZCNT;
  5155. Legalize(instr);
  5156. }
  5157. else
  5158. {
  5159. // dst = BSF src
  5160. // dst = CMOVE dst, 32 // dst is src1 to help reg alloc
  5161. int instrSize = instr->GetSrc1()->GetSize();
  5162. IRType type = instrSize == 8 ? TyInt64 : TyInt32;
  5163. instr->m_opcode = Js::OpCode::BSF;
  5164. Legalize(instr);
  5165. IR::IntConstOpnd * const32 = IR::IntConstOpnd::New(instrSize * 8, type, m_func);
  5166. IR::Instr* cmove = IR::Instr::New(Js::OpCode::CMOVE, instr->GetDst(), instr->GetDst(), const32, this->m_func);
  5167. instr->InsertAfter(cmove);
  5168. Legalize(cmove);
  5169. }
  5170. }
  5171. void
  5172. LowererMD::GeneratePopCnt(IR::Instr * instr)
  5173. {
  5174. Assert(instr->GetSrc1()->IsInt32() || instr->GetSrc1()->IsUInt32() || instr->GetSrc1()->IsInt64());
  5175. Assert(instr->GetDst()->IsInt32() || instr->GetDst()->IsUInt32() || instr->GetDst()->IsInt64());
  5176. #ifdef _M_IX86
  5177. if (instr->GetSrc1()->IsInt64())
  5178. {
  5179. lowererMDArch.EmitInt64Instr(instr);
  5180. return;
  5181. }
  5182. #endif
  5183. if (AutoSystemInfo::Data.PopCntAvailable())
  5184. {
  5185. instr->m_opcode = Js::OpCode::POPCNT;
  5186. Legalize(instr);
  5187. }
  5188. else
  5189. {
  5190. int instrSize = instr->GetSrc1()->GetSize();
  5191. LoadHelperArgument(instr, instr->GetSrc1());
  5192. instr->UnlinkSrc1();
  5193. this->ChangeToHelperCall(instr, instrSize == 8 ? IR::HelperPopCnt64 : IR::HelperPopCnt32);
  5194. }
  5195. }
  5196. void
  5197. LowererMD::GenerateClz(IR::Instr * instr)
  5198. {
  5199. Assert(instr->GetSrc1()->IsInt32() || instr->GetSrc1()->IsUInt32() || instr->GetSrc1()->IsInt64());
  5200. Assert(IRType_IsNativeInt(instr->GetDst()->GetType()));
  5201. #ifdef _M_IX86
  5202. if (instr->GetSrc1()->IsInt64())
  5203. {
  5204. lowererMDArch.EmitInt64Instr(instr);
  5205. return;
  5206. }
  5207. #endif
  5208. if (AutoSystemInfo::Data.LZCntAvailable())
  5209. {
  5210. instr->m_opcode = Js::OpCode::LZCNT;
  5211. Legalize(instr);
  5212. }
  5213. else
  5214. {
  5215. // tmp = BSR src
  5216. // JE $label32
  5217. // dst = SUB 31, tmp
  5218. // dst = SUB 63, tmp; for int64
  5219. // JMP $done
  5220. // label32:
  5221. // dst = mov 32;
  5222. // dst = mov 64; for int64
  5223. // $done
  5224. int instrSize = instr->GetSrc1()->GetSize();
  5225. IRType type = instrSize == 8 ? TyInt64 : TyInt32;
  5226. IR::LabelInstr * doneLabel = Lowerer::InsertLabel(false, instr->m_next);
  5227. IR::Opnd * dst = instr->UnlinkDst();
  5228. IR::Opnd * tmpOpnd = IR::RegOpnd::New(type, m_func);
  5229. instr->SetDst(tmpOpnd);
  5230. instr->m_opcode = Js::OpCode::BSR;
  5231. Legalize(instr);
  5232. IR::LabelInstr * label32 = Lowerer::InsertLabel(false, doneLabel);
  5233. instr = IR::BranchInstr::New(Js::OpCode::JEQ, label32, m_func);
  5234. label32->InsertBefore(instr);
  5235. Lowerer::InsertSub(false, dst, IR::IntConstOpnd::New(instrSize == 8 ? 63 : 31, type, m_func), tmpOpnd, label32);
  5236. Lowerer::InsertBranch(Js::OpCode::Br, doneLabel, label32);
  5237. Lowerer::InsertMove(dst, IR::IntConstOpnd::New(instrSize == 8 ? 64 : 32, type, m_func), doneLabel);
  5238. }
  5239. }
  5240. #if !FLOATVAR
  5241. void
  5242. LowererMD::GenerateNumberAllocation(IR::RegOpnd * opndDst, IR::Instr * instrInsert, bool isHelper)
  5243. {
  5244. size_t alignedAllocSize = Js::RecyclerJavascriptNumberAllocator::GetAlignedAllocSize(
  5245. m_func->GetScriptContextInfo()->IsRecyclerVerifyEnabled(),
  5246. m_func->GetScriptContextInfo()->GetRecyclerVerifyPad());
  5247. IR::Opnd * endAddressOpnd = m_lowerer->LoadNumberAllocatorValueOpnd(instrInsert, NumberAllocatorValue::NumberAllocatorEndAddress);
  5248. IR::Opnd * freeObjectListOpnd = m_lowerer->LoadNumberAllocatorValueOpnd(instrInsert, NumberAllocatorValue::NumberAllocatorFreeObjectList);
  5249. // MOV dst, allocator->freeObjectList
  5250. IR::Instr * loadMemBlockInstr = IR::Instr::New(Js::OpCode::MOV, opndDst, freeObjectListOpnd, this->m_func);
  5251. instrInsert->InsertBefore(loadMemBlockInstr);
  5252. // LEA nextMemBlock, [dst + allocSize]
  5253. IR::RegOpnd * nextMemBlockOpnd = IR::RegOpnd::New(TyMachPtr, this->m_func);
  5254. IR::Instr * loadNextMemBlockInstr = IR::Instr::New(Js::OpCode::LEA, nextMemBlockOpnd,
  5255. IR::IndirOpnd::New(opndDst, alignedAllocSize, TyMachPtr, this->m_func), this->m_func);
  5256. instrInsert->InsertBefore(loadNextMemBlockInstr);
  5257. // CMP nextMemBlock, allocator->endAddress
  5258. IR::Instr * checkInstr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  5259. checkInstr->SetSrc1(nextMemBlockOpnd);
  5260. checkInstr->SetSrc2(endAddressOpnd);
  5261. instrInsert->InsertBefore(checkInstr);
  5262. // JA $helper
  5263. IR::LabelInstr * helperLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  5264. IR::BranchInstr * branchInstr = IR::BranchInstr::New(Js::OpCode::JA, helperLabel, this->m_func);
  5265. instrInsert->InsertBefore(branchInstr);
  5266. // MOV allocator->freeObjectList, nextMemBlock
  5267. IR::Instr * setFreeObjectListInstr = IR::Instr::New(Js::OpCode::MOV, freeObjectListOpnd, nextMemBlockOpnd, this->m_func);
  5268. instrInsert->InsertBefore(setFreeObjectListInstr);
  5269. // JMP $done
  5270. IR::LabelInstr * doneLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, isHelper);
  5271. IR::BranchInstr * branchToDoneInstr = IR::BranchInstr::New(Js::OpCode::JMP, doneLabel, this->m_func);
  5272. instrInsert->InsertBefore(branchToDoneInstr);
  5273. // $helper:
  5274. instrInsert->InsertBefore(helperLabel);
  5275. // PUSH allocator
  5276. this->LoadHelperArgument(instrInsert, m_lowerer->LoadScriptContextValueOpnd(instrInsert, ScriptContextValue::ScriptContextNumberAllocator));
  5277. // dst = Call AllocUninitializedNumber
  5278. IR::Instr * instrCall = IR::Instr::New(Js::OpCode::CALL, opndDst,
  5279. IR::HelperCallOpnd::New(IR::HelperAllocUninitializedNumber, this->m_func), this->m_func);
  5280. instrInsert->InsertBefore(instrCall);
  5281. this->lowererMDArch.LowerCall(instrCall, 0);
  5282. // $done:
  5283. instrInsert->InsertBefore(doneLabel);
  5284. }
  5285. #endif
  5286. #ifdef _CONTROL_FLOW_GUARD
  5287. void
  5288. LowererMD::GenerateCFGCheck(IR::Opnd * entryPointOpnd, IR::Instr * insertBeforeInstr)
  5289. {
  5290. //PreReserve segment at this point, as we will definitely using this segment for JITted code(in almost all cases)
  5291. //This is for CFG check optimization
  5292. IR::LabelInstr * callLabelInstr = nullptr;
  5293. char * preReservedRegionStartAddress = nullptr;
  5294. if (m_func->CanAllocInPreReservedHeapPageSegment())
  5295. {
  5296. char* endAddressOfSegment = nullptr;
  5297. #if ENABLE_OOP_NATIVE_CODEGEN
  5298. if (m_func->IsOOPJIT())
  5299. {
  5300. PreReservedSectionAllocWrapper * preReservedAllocator = m_func->GetOOPThreadContext()->GetPreReservedSectionAllocator();
  5301. preReservedRegionStartAddress = (char *)preReservedAllocator->EnsurePreReservedRegion();
  5302. if (preReservedRegionStartAddress != nullptr)
  5303. {
  5304. endAddressOfSegment = (char*)preReservedAllocator->GetPreReservedEndAddress();
  5305. }
  5306. }
  5307. else
  5308. #endif
  5309. {
  5310. PreReservedVirtualAllocWrapper * preReservedVirtualAllocator = m_func->GetInProcThreadContext()->GetPreReservedVirtualAllocator();
  5311. preReservedRegionStartAddress = (char *)preReservedVirtualAllocator->EnsurePreReservedRegion();
  5312. if (preReservedRegionStartAddress != nullptr)
  5313. {
  5314. endAddressOfSegment = (char*)preReservedVirtualAllocator->GetPreReservedEndAddress();
  5315. }
  5316. }
  5317. if (preReservedRegionStartAddress != nullptr)
  5318. {
  5319. int32 segmentSize = (int32) (endAddressOfSegment - preReservedRegionStartAddress);
  5320. // Generate instructions for local Pre-Reserved Segment Range check
  5321. IR::AddrOpnd * endAddressOfSegmentConstOpnd = IR::AddrOpnd::New(endAddressOfSegment, IR::AddrOpndKindDynamicMisc, m_func);
  5322. IR::RegOpnd *resultOpnd = IR::RegOpnd::New(TyMachReg, this->m_func);
  5323. #if _M_IX86
  5324. //resultOpnd = endAddressOfSegmentConstOpnd - entryPointOpnd
  5325. IR::Instr* subInstr = IR::Instr::New(Js::OpCode::Sub_I4, resultOpnd, endAddressOfSegmentConstOpnd, entryPointOpnd, m_func);
  5326. insertBeforeInstr->InsertBefore(subInstr);
  5327. this->EmitInt4Instr(subInstr);
  5328. #elif _M_X64
  5329. //MOV resultOpnd, endAddressOfSegment
  5330. //resultOpnd = resultOpnd - entryPointOpnd
  5331. IR::Instr *movInstr = IR::Instr::New(Js::OpCode::MOV, resultOpnd, endAddressOfSegmentConstOpnd, this->m_func);
  5332. insertBeforeInstr->InsertBefore(movInstr);
  5333. IR::Instr* subInstr = IR::Instr::New(Js::OpCode::SUB, resultOpnd, resultOpnd, entryPointOpnd, m_func);
  5334. insertBeforeInstr->InsertBefore(subInstr);
  5335. #endif
  5336. //CMP subResultOpnd, segmentSize
  5337. //JL $callLabelInstr:
  5338. AssertMsg((size_t) segmentSize == (size_t) (endAddressOfSegment - preReservedRegionStartAddress), "Need a bigger datatype for segmentSize?");
  5339. IR::IntConstOpnd * segmentSizeOpnd = IR::IntConstOpnd::New(segmentSize, IRType::TyInt32, m_func, true);
  5340. callLabelInstr = IR::LabelInstr::New(Js::OpCode::Label, m_func);
  5341. this->m_lowerer->InsertCompareBranch(resultOpnd, segmentSizeOpnd, Js::OpCode::JBE, callLabelInstr, insertBeforeInstr);
  5342. }
  5343. }
  5344. //MOV ecx, entryPoint
  5345. IR::RegOpnd * entryPointRegOpnd = IR::RegOpnd::New(TyMachReg, this->m_func);
  5346. #if _M_IX86
  5347. entryPointRegOpnd->SetReg(RegECX);
  5348. #elif _M_X64
  5349. entryPointRegOpnd->SetReg(RegRCX);
  5350. #endif
  5351. entryPointRegOpnd->m_isCallArg = true;
  5352. IR::Instr* movInstrEntryPointToRegister = IR::Instr::New(Js::OpCode::MOV, entryPointRegOpnd, entryPointOpnd, this->m_func);
  5353. insertBeforeInstr->InsertBefore(movInstrEntryPointToRegister);
  5354. //Generate CheckCFG CALL here
  5355. IR::HelperCallOpnd *cfgCallOpnd = IR::HelperCallOpnd::New(IR::HelperGuardCheckCall, this->m_func);
  5356. IR::Instr* cfgCallInstr = IR::Instr::New(Js::OpCode::CALL, this->m_func);
  5357. #if _M_IX86
  5358. //call[__guard_check_icall_fptr]
  5359. cfgCallInstr->SetSrc1(cfgCallOpnd);
  5360. #elif _M_X64
  5361. //mov rax, __guard_check_icall_fptr
  5362. IR::RegOpnd *targetOpnd = IR::RegOpnd::New(StackSym::New(TyMachPtr, m_func), RegRAX, TyMachPtr, this->m_func);
  5363. IR::Instr *movInstr = IR::Instr::New(Js::OpCode::MOV, targetOpnd, cfgCallOpnd, this->m_func);
  5364. insertBeforeInstr->InsertBefore(movInstr);
  5365. //call rax
  5366. cfgCallInstr->SetSrc1(targetOpnd);
  5367. #endif
  5368. //CALL cfg(rax)
  5369. insertBeforeInstr->InsertBefore(cfgCallInstr);
  5370. if (preReservedRegionStartAddress != nullptr)
  5371. {
  5372. Assert(callLabelInstr);
  5373. #if DBG
  5374. //Always generate CFG check in DBG build to make sure that the address is still valid
  5375. movInstrEntryPointToRegister->InsertBefore(callLabelInstr);
  5376. #else
  5377. insertBeforeInstr->InsertBefore(callLabelInstr);
  5378. #endif
  5379. }
  5380. }
  5381. #endif
  5382. void
  5383. LowererMD::GenerateFastRecyclerAlloc(size_t allocSize, IR::RegOpnd* newObjDst, IR::Instr* insertionPointInstr, IR::LabelInstr* allocHelperLabel, IR::LabelInstr* allocDoneLabel)
  5384. {
  5385. IR::Opnd * endAddressOpnd;
  5386. IR::Opnd * freeListOpnd;
  5387. ScriptContextInfo* scriptContext = this->m_func->GetScriptContextInfo();
  5388. void* allocatorAddress;
  5389. uint32 endAddressOffset;
  5390. uint32 freeListOffset;
  5391. size_t alignedSize = HeapInfo::GetAlignedSizeNoCheck(allocSize);
  5392. bool allowNativeCodeBumpAllocation = scriptContext->GetRecyclerAllowNativeCodeBumpAllocation();
  5393. Recycler::GetNormalHeapBlockAllocatorInfoForNativeAllocation((void*)scriptContext->GetRecyclerAddr(), alignedSize,
  5394. allocatorAddress, endAddressOffset, freeListOffset,
  5395. allowNativeCodeBumpAllocation, this->m_func->IsOOPJIT());
  5396. endAddressOpnd = IR::MemRefOpnd::New((char*)allocatorAddress + endAddressOffset, TyMachPtr, this->m_func, IR::AddrOpndKindDynamicRecyclerAllocatorEndAddressRef);
  5397. freeListOpnd = IR::MemRefOpnd::New((char*)allocatorAddress + freeListOffset, TyMachPtr, this->m_func, IR::AddrOpndKindDynamicRecyclerAllocatorFreeListRef);
  5398. const IR::AutoReuseOpnd autoReuseTempOpnd(freeListOpnd, m_func);
  5399. // MOV newObjDst, allocator->freeObjectList
  5400. Lowerer::InsertMove(newObjDst, freeListOpnd, insertionPointInstr);
  5401. // LEA nextMemBlock, [newObjDst + allocSize]
  5402. IR::RegOpnd * nextMemBlockOpnd = IR::RegOpnd::New(TyMachPtr, this->m_func);
  5403. IR::IndirOpnd* nextMemBlockSrc = IR::IndirOpnd::New(newObjDst, (int32)alignedSize, TyMachPtr, this->m_func);
  5404. IR::Instr * loadNextMemBlockInstr = IR::Instr::New(Js::OpCode::LEA, nextMemBlockOpnd, nextMemBlockSrc, this->m_func);
  5405. insertionPointInstr->InsertBefore(loadNextMemBlockInstr);
  5406. // CMP nextMemBlock, allocator->endAddress
  5407. IR::Instr * checkInstr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  5408. checkInstr->SetSrc1(nextMemBlockOpnd);
  5409. checkInstr->SetSrc2(endAddressOpnd);
  5410. insertionPointInstr->InsertBefore(checkInstr);
  5411. Legalize(checkInstr);
  5412. // JA $allocHelper
  5413. IR::BranchInstr * branchToAllocHelperInstr = IR::BranchInstr::New(Js::OpCode::JA, allocHelperLabel, this->m_func);
  5414. insertionPointInstr->InsertBefore(branchToAllocHelperInstr);
  5415. // MOV allocator->freeObjectList, nextMemBlock
  5416. Lowerer::InsertMove(freeListOpnd, nextMemBlockOpnd, insertionPointInstr, false);
  5417. // JMP $allocDone
  5418. IR::BranchInstr * branchToAllocDoneInstr = IR::BranchInstr::New(Js::OpCode::JMP, allocDoneLabel, this->m_func);
  5419. insertionPointInstr->InsertBefore(branchToAllocDoneInstr);
  5420. }
  5421. #ifdef ENABLE_WASM
  5422. void
  5423. LowererMD::GenerateCopysign(IR::Instr * instr)
  5424. {
  5425. #if defined(_M_IX86)
  5426. // We should only generate this if sse2 is available
  5427. Assert(AutoSystemInfo::Data.SSE2Available());
  5428. #endif
  5429. // ANDPS reg0, absDoubleCst
  5430. // ANDPS reg1, sgnBitDoubleCst
  5431. // ORPS reg0, reg1
  5432. // Copy sign from src2 to src1
  5433. IR::Opnd* src1 = instr->GetSrc1();
  5434. IR::Opnd* src2 = instr->GetSrc2();
  5435. Assert(src1->IsFloat32() || src1->IsFloat64());
  5436. GenerateFloatAbs(src1->AsRegOpnd(), instr);
  5437. IR::MemRefOpnd *memRef = IR::MemRefOpnd::New(src2->IsFloat32() ? this->m_func->GetThreadContextInfo()->GetSgnFloatBitCst() : this->m_func->GetThreadContextInfo()->GetSgnDoubleBitCst(),
  5438. src2->GetType(), this->m_func, src2->IsFloat32() ? IR::AddrOpndKindDynamicFloatRef : IR::AddrOpndKindDynamicDoubleRef);
  5439. IR::Instr* t2 = IR::Instr::New(Js::OpCode::ANDPS, instr->GetSrc2(), instr->GetSrc2(), memRef, m_func);
  5440. instr->InsertBefore(t2);
  5441. Legalize(t2);
  5442. instr->m_opcode = Js::OpCode::ORPS;
  5443. Legalize(instr);
  5444. };
  5445. #endif //ENABLE_WASM
  5446. void
  5447. LowererMD::SaveDoubleToVar(IR::RegOpnd * dstOpnd, IR::RegOpnd *opndFloat, IR::Instr *instrOrig, IR::Instr *instrInsert, bool isHelper)
  5448. {
  5449. Assert(opndFloat->GetType() == TyFloat64);
  5450. // Call JSNumber::ToVar to save the float operand to the result of the original (var) instruction
  5451. #if !FLOATVAR
  5452. // We should only generate this if sse2 is available
  5453. Assert(AutoSystemInfo::Data.SSE2Available());
  5454. IR::Opnd * symVTableDst;
  5455. IR::Opnd * symDblDst;
  5456. IR::Opnd * symTypeDst;
  5457. IR::Instr * newInstr;
  5458. IR::Instr * numberInitInsertInstr = nullptr;
  5459. if (instrOrig->dstIsTempNumber)
  5460. {
  5461. // Use the original dst to get the temp number sym
  5462. StackSym * tempNumberSym = this->m_lowerer->GetTempNumberSym(instrOrig->GetDst(), instrOrig->dstIsTempNumberTransferred);
  5463. // LEA dst, &tempSym
  5464. IR::SymOpnd * symTempSrc = IR::SymOpnd::New(tempNumberSym, TyMachPtr, this->m_func);
  5465. IR::Instr * loadTempNumberInstr = IR::Instr::New(Js::OpCode::LEA, dstOpnd, symTempSrc, this->m_func);
  5466. instrInsert->InsertBefore(loadTempNumberInstr);
  5467. symVTableDst = IR::SymOpnd::New(tempNumberSym, TyMachPtr, this->m_func);
  5468. symDblDst = IR::SymOpnd::New(tempNumberSym, (uint32)Js::JavascriptNumber::GetValueOffset(), TyFloat64, this->m_func);
  5469. symTypeDst = IR::SymOpnd::New(tempNumberSym, (uint32)Js::JavascriptNumber::GetOffsetOfType(), TyMachPtr, this->m_func);
  5470. if (this->m_lowerer->outerMostLoopLabel == nullptr)
  5471. {
  5472. // If we are not in loop, just insert in place
  5473. numberInitInsertInstr = instrInsert;
  5474. }
  5475. else
  5476. {
  5477. // Otherwise, initialize in the outer most loop top if we haven't initialized it yet.
  5478. numberInitInsertInstr = this->m_lowerer->initializedTempSym->TestAndSet(tempNumberSym->m_id) ?
  5479. nullptr : this->m_lowerer->outerMostLoopLabel;
  5480. }
  5481. }
  5482. else
  5483. {
  5484. this->GenerateNumberAllocation(dstOpnd, instrInsert, isHelper);
  5485. symVTableDst = IR::IndirOpnd::New(dstOpnd, 0, TyMachPtr, this->m_func);
  5486. symDblDst = IR::IndirOpnd::New(dstOpnd, (uint32)Js::JavascriptNumber::GetValueOffset(), TyFloat64, this->m_func);
  5487. symTypeDst = IR::IndirOpnd::New(dstOpnd, (uint32)Js::JavascriptNumber::GetOffsetOfType(), TyMachPtr, this->m_func);
  5488. numberInitInsertInstr = instrInsert;
  5489. }
  5490. if (numberInitInsertInstr)
  5491. {
  5492. // Inline the case where the dst is marked as temp.
  5493. IR::Opnd *jsNumberVTable = m_lowerer->LoadVTableValueOpnd(numberInitInsertInstr, VTableValue::VtableJavascriptNumber);
  5494. // MOV dst->vtable, JavascriptNumber::vtable
  5495. newInstr = IR::Instr::New(Js::OpCode::MOV, symVTableDst, jsNumberVTable, this->m_func);
  5496. numberInitInsertInstr->InsertBefore(newInstr);
  5497. // MOV dst->type, JavascriptNumber_type
  5498. IR::Opnd *typeOpnd = m_lowerer->LoadLibraryValueOpnd(numberInitInsertInstr, LibraryValue::ValueNumberTypeStatic);
  5499. newInstr = IR::Instr::New(Js::OpCode::MOV, symTypeDst, typeOpnd, this->m_func);
  5500. numberInitInsertInstr->InsertBefore(newInstr);
  5501. }
  5502. // MOVSD dst->value, opndFloat ; copy the float result to the temp JavascriptNumber
  5503. newInstr = IR::Instr::New(Js::OpCode::MOVSD, symDblDst, opndFloat, this->m_func);
  5504. instrInsert->InsertBefore(newInstr);
  5505. #else
  5506. // s1 = MOVD opndFloat
  5507. IR::RegOpnd *s1 = IR::RegOpnd::New(TyMachReg, m_func);
  5508. IR::Instr *movd = IR::Instr::New(Js::OpCode::MOVD, s1, opndFloat, m_func);
  5509. instrInsert->InsertBefore(movd);
  5510. if (m_func->GetJITFunctionBody()->IsAsmJsMode())
  5511. {
  5512. // s1 = MOVD src
  5513. // tmp = NOT s1
  5514. // tmp = AND tmp, 0x7FF0000000000000ull
  5515. // test tmp, tmp
  5516. // je helper
  5517. // jmp done
  5518. // helper:
  5519. // tmp2 = AND s1, 0x000FFFFFFFFFFFFFull
  5520. // test tmp2, tmp2
  5521. // je done
  5522. // s1 = JavascriptNumber::k_Nan
  5523. // done:
  5524. IR::RegOpnd *tmp = IR::RegOpnd::New(TyMachReg, m_func);
  5525. IR::Instr * newInstr = IR::Instr::New(Js::OpCode::NOT, tmp, s1, m_func);
  5526. instrInsert->InsertBefore(newInstr);
  5527. LowererMD::MakeDstEquSrc1(newInstr);
  5528. newInstr = IR::Instr::New(Js::OpCode::AND, tmp, tmp, IR::AddrOpnd::New((Js::Var)0x7FF0000000000000, IR::AddrOpndKindConstantVar, m_func, true), m_func);
  5529. instrInsert->InsertBefore(newInstr);
  5530. LowererMD::Legalize(newInstr);
  5531. IR::LabelInstr* helper = Lowerer::InsertLabel(true, instrInsert);
  5532. Lowerer::InsertTestBranch(tmp, tmp, Js::OpCode::BrEq_A, helper, helper);
  5533. IR::LabelInstr* done = Lowerer::InsertLabel(isHelper, instrInsert);
  5534. Lowerer::InsertBranch(Js::OpCode::Br, done, helper);
  5535. IR::RegOpnd *tmp2 = IR::RegOpnd::New(TyMachReg, m_func);
  5536. newInstr = IR::Instr::New(Js::OpCode::AND, tmp2, s1, IR::AddrOpnd::New((Js::Var)0x000FFFFFFFFFFFFFull, IR::AddrOpndKindConstantVar, m_func, true), m_func);
  5537. done->InsertBefore(newInstr);
  5538. LowererMD::Legalize(newInstr);
  5539. Lowerer::InsertTestBranch(tmp2, tmp2, Js::OpCode::BrEq_A, done, done);
  5540. IR::Opnd * opndNaN = IR::AddrOpnd::New((Js::Var)Js::JavascriptNumber::k_Nan, IR::AddrOpndKindConstantVar, m_func, true);
  5541. Lowerer::InsertMove(s1, opndNaN, done);
  5542. }
  5543. // s1 = XOR s1, FloatTag_Value
  5544. // dst = s1
  5545. IR::Instr *setTag = IR::Instr::New(Js::OpCode::XOR,
  5546. s1,
  5547. s1,
  5548. IR::AddrOpnd::New((Js::Var)Js::FloatTag_Value,
  5549. IR::AddrOpndKindConstantVar,
  5550. this->m_func,
  5551. /* dontEncode = */ true),
  5552. this->m_func);
  5553. IR::Instr *movDst = IR::Instr::New(Js::OpCode::MOV, dstOpnd, s1, this->m_func);
  5554. instrInsert->InsertBefore(setTag);
  5555. instrInsert->InsertBefore(movDst);
  5556. LowererMD::Legalize(setTag);
  5557. #endif
  5558. }
  5559. void
  5560. LowererMD::EmitLoadFloatFromNumber(IR::Opnd *dst, IR::Opnd *src, IR::Instr *insertInstr)
  5561. {
  5562. IR::LabelInstr *labelDone;
  5563. IR::Instr *instr;
  5564. labelDone = EmitLoadFloatCommon(dst, src, insertInstr, insertInstr->HasBailOutInfo());
  5565. if (labelDone == nullptr)
  5566. {
  5567. // We're done
  5568. insertInstr->Remove();
  5569. return;
  5570. }
  5571. // $Done note: insertAfter
  5572. insertInstr->InsertAfter(labelDone);
  5573. if (!insertInstr->HasBailOutInfo())
  5574. {
  5575. // $Done
  5576. insertInstr->Remove();
  5577. return;
  5578. }
  5579. Assert(!m_func->GetJITFunctionBody()->IsAsmJsMode());
  5580. IR::LabelInstr *labelNoBailOut = nullptr;
  5581. IR::SymOpnd *tempSymOpnd = nullptr;
  5582. if (insertInstr->GetBailOutKind() == IR::BailOutPrimitiveButString)
  5583. {
  5584. if (!this->m_func->tempSymDouble)
  5585. {
  5586. this->m_func->tempSymDouble = StackSym::New(TyFloat64, this->m_func);
  5587. this->m_func->StackAllocate(this->m_func->tempSymDouble, MachDouble);
  5588. }
  5589. // LEA r3, tempSymDouble
  5590. IR::RegOpnd *reg3Opnd = IR::RegOpnd::New(TyMachReg, this->m_func);
  5591. tempSymOpnd = IR::SymOpnd::New(this->m_func->tempSymDouble, TyFloat64, this->m_func);
  5592. instr = IR::Instr::New(Js::OpCode::LEA, reg3Opnd, tempSymOpnd, this->m_func);
  5593. insertInstr->InsertBefore(instr);
  5594. // regBoolResult = to_number_fromPrimitive(value, &dst, allowUndef, scriptContext);
  5595. this->m_lowerer->LoadScriptContext(insertInstr);
  5596. IR::IntConstOpnd *allowUndefOpnd;
  5597. if (insertInstr->GetBailOutKind() == IR::BailOutPrimitiveButString)
  5598. {
  5599. allowUndefOpnd = IR::IntConstOpnd::New(true, TyInt32, this->m_func);
  5600. }
  5601. else
  5602. {
  5603. Assert(insertInstr->GetBailOutKind() == IR::BailOutNumberOnly);
  5604. allowUndefOpnd = IR::IntConstOpnd::New(false, TyInt32, this->m_func);
  5605. }
  5606. this->LoadHelperArgument(insertInstr, allowUndefOpnd);
  5607. this->LoadHelperArgument(insertInstr, reg3Opnd);
  5608. this->LoadHelperArgument(insertInstr, src);
  5609. IR::RegOpnd *regBoolResult = IR::RegOpnd::New(TyInt32, this->m_func);
  5610. instr = IR::Instr::New(Js::OpCode::CALL, regBoolResult, IR::HelperCallOpnd::New(IR::HelperOp_ConvNumber_FromPrimitive, this->m_func), this->m_func);
  5611. insertInstr->InsertBefore(instr);
  5612. this->lowererMDArch.LowerCall(instr, 0);
  5613. // TEST regBoolResult, regBoolResult
  5614. instr = IR::Instr::New(Js::OpCode::TEST, this->m_func);
  5615. instr->SetSrc1(regBoolResult);
  5616. instr->SetSrc2(regBoolResult);
  5617. insertInstr->InsertBefore(instr);
  5618. // JNE $noBailOut
  5619. labelNoBailOut = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  5620. instr = IR::BranchInstr::New(Js::OpCode::JNE, labelNoBailOut, this->m_func);
  5621. insertInstr->InsertBefore(instr);
  5622. }
  5623. // Bailout code
  5624. Assert(insertInstr->m_opcode == Js::OpCode::FromVar);
  5625. insertInstr->UnlinkDst();
  5626. insertInstr->FreeSrc1();
  5627. IR::Instr *bailoutInstr = insertInstr;
  5628. insertInstr = bailoutInstr->m_next;
  5629. this->m_lowerer->GenerateBailOut(bailoutInstr);
  5630. // $noBailOut
  5631. if (labelNoBailOut)
  5632. {
  5633. insertInstr->InsertBefore(labelNoBailOut);
  5634. Assert(dst->IsRegOpnd());
  5635. // MOVSD dst, [pResult].f64
  5636. instr = IR::Instr::New(Js::OpCode::MOVSD, dst, tempSymOpnd, this->m_func);
  5637. insertInstr->InsertBefore(instr);
  5638. }
  5639. }
  5640. IR::LabelInstr*
  5641. LowererMD::EmitLoadFloatCommon(IR::Opnd *dst, IR::Opnd *src, IR::Instr *insertInstr, bool needHelperLabel)
  5642. {
  5643. IR::Instr *instr;
  5644. Assert(src->GetType() == TyVar);
  5645. Assert(dst->IsFloat());
  5646. bool isFloatConst = false;
  5647. IR::RegOpnd *regFloatOpnd = nullptr;
  5648. if (src->IsRegOpnd() && src->AsRegOpnd()->m_sym->m_isFltConst)
  5649. {
  5650. IR::RegOpnd *regOpnd = src->AsRegOpnd();
  5651. Assert(regOpnd->m_sym->m_isSingleDef);
  5652. Js::Var value = regOpnd->m_sym->GetFloatConstValueAsVar_PostGlobOpt();
  5653. #if FLOATVAR
  5654. void *pDouble = (double*)NativeCodeDataNewNoFixup(this->m_func->GetNativeCodeDataAllocator(), DoubleType<DataDesc_LowererMD_EmitLoadFloatCommon_Double>, Js::JavascriptNumber::GetValue(value));
  5655. IR::Opnd * doubleRef;
  5656. if (!m_func->IsOOPJIT())
  5657. {
  5658. doubleRef = IR::MemRefOpnd::New(pDouble, TyFloat64, this->m_func, IR::AddrOpndKindDynamicDoubleRef);
  5659. }
  5660. else
  5661. {
  5662. int offset = NativeCodeData::GetDataTotalOffset(pDouble);
  5663. doubleRef = IR::IndirOpnd::New(IR::RegOpnd::New(m_func->GetTopFunc()->GetNativeCodeDataSym(), TyVar, m_func), offset, TyMachDouble,
  5664. #if DBG
  5665. NativeCodeData::GetDataDescription(pDouble, m_func->m_alloc),
  5666. #endif
  5667. m_func);
  5668. GetLowerer()->addToLiveOnBackEdgeSyms->Set(m_func->GetTopFunc()->GetNativeCodeDataSym()->m_id);
  5669. }
  5670. #else
  5671. IR::MemRefOpnd *doubleRef = IR::MemRefOpnd::New((BYTE*)value + Js::JavascriptNumber::GetValueOffset(), TyFloat64, this->m_func,
  5672. IR::AddrOpndKindDynamicDoubleRef);
  5673. #endif
  5674. regFloatOpnd = IR::RegOpnd::New(TyFloat64, this->m_func);
  5675. instr = IR::Instr::New(Js::OpCode::MOVSD, regFloatOpnd, doubleRef, this->m_func);
  5676. insertInstr->InsertBefore(instr);
  5677. Legalize(instr);
  5678. isFloatConst = true;
  5679. }
  5680. // Src is constant?
  5681. if (src->IsImmediateOpnd() || src->IsFloatConstOpnd())
  5682. {
  5683. regFloatOpnd = IR::RegOpnd::New(TyFloat64, this->m_func);
  5684. m_lowerer->LoadFloatFromNonReg(src, regFloatOpnd, insertInstr);
  5685. isFloatConst = true;
  5686. }
  5687. if (isFloatConst)
  5688. {
  5689. if (dst->GetType() == TyFloat32)
  5690. {
  5691. // CVTSD2SS regOpnd32.f32, regOpnd.f64 -- Convert regOpnd from f64 to f32
  5692. IR::RegOpnd *regOpnd32 = regFloatOpnd->UseWithNewType(TyFloat32, this->m_func)->AsRegOpnd();
  5693. instr = IR::Instr::New(Js::OpCode::CVTSD2SS, regOpnd32, regFloatOpnd, this->m_func);
  5694. insertInstr->InsertBefore(instr);
  5695. // MOVSS dst, regOpnd32
  5696. instr = IR::Instr::New(Js::OpCode::MOVSS, dst, regOpnd32, this->m_func);
  5697. insertInstr->InsertBefore(instr);
  5698. }
  5699. else
  5700. {
  5701. // MOVSD dst, regOpnd
  5702. instr = IR::Instr::New(Js::OpCode::MOVSD, dst, regFloatOpnd, this->m_func);
  5703. insertInstr->InsertBefore(instr);
  5704. }
  5705. return nullptr;
  5706. }
  5707. Assert(src->IsRegOpnd());
  5708. IR::LabelInstr *labelStore = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  5709. IR::LabelInstr *labelHelper;
  5710. IR::LabelInstr *labelDone = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  5711. if (needHelperLabel)
  5712. {
  5713. labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  5714. }
  5715. else
  5716. {
  5717. labelHelper = labelDone;
  5718. }
  5719. bool const isFloat32 = dst->GetType() == TyFloat32;
  5720. IR::RegOpnd *reg2 = ((isFloat32 || !dst->IsRegOpnd()) ? IR::RegOpnd::New(TyMachDouble, this->m_func) : dst->AsRegOpnd());
  5721. // Load the float value in reg2
  5722. this->lowererMDArch.LoadCheckedFloat(src->AsRegOpnd(), reg2, labelStore, labelHelper, insertInstr, needHelperLabel);
  5723. // $Store
  5724. insertInstr->InsertBefore(labelStore);
  5725. if (isFloat32)
  5726. {
  5727. IR::RegOpnd *reg2_32 = reg2->UseWithNewType(TyFloat32, this->m_func)->AsRegOpnd();
  5728. // CVTSD2SS r2_32.f32, r2.f64 -- Convert regOpnd from f64 to f32
  5729. instr = IR::Instr::New(Js::OpCode::CVTSD2SS, reg2_32, reg2, this->m_func);
  5730. insertInstr->InsertBefore(instr);
  5731. // MOVSS dst, r2_32
  5732. instr = IR::Instr::New(Js::OpCode::MOVSS, dst, reg2_32, this->m_func);
  5733. insertInstr->InsertBefore(instr);
  5734. }
  5735. else if (reg2 != dst)
  5736. {
  5737. // MOVSD dst, r2
  5738. instr = IR::Instr::New(Js::OpCode::MOVSD, dst, reg2, this->m_func);
  5739. insertInstr->InsertBefore(instr);
  5740. }
  5741. // JMP $Done
  5742. instr = IR::BranchInstr::New(Js::OpCode::JMP, labelDone, this->m_func);
  5743. insertInstr->InsertBefore(instr);
  5744. if (needHelperLabel)
  5745. {
  5746. // $Helper
  5747. insertInstr->InsertBefore(labelHelper);
  5748. }
  5749. return labelDone;
  5750. }
  5751. void
  5752. LowererMD::EmitLoadFloat(IR::Opnd *dst, IR::Opnd *src, IR::Instr *insertInstr, IR::Instr * instrBailOut, IR::LabelInstr * labelBailOut)
  5753. {
  5754. IR::LabelInstr *labelDone;
  5755. IR::Instr *instr;
  5756. labelDone = EmitLoadFloatCommon(dst, src, insertInstr, true);
  5757. if (labelDone == nullptr)
  5758. {
  5759. // We're done
  5760. return;
  5761. }
  5762. IR::BailOutKind bailOutKind = instrBailOut && instrBailOut->HasBailOutInfo() ? instrBailOut->GetBailOutKind() : IR::BailOutInvalid;
  5763. if (bailOutKind & IR::BailOutOnArrayAccessHelperCall)
  5764. {
  5765. // Bail out instead of making the helper call.
  5766. Assert(labelBailOut);
  5767. m_lowerer->InsertBranch(Js::OpCode::Br, labelBailOut, insertInstr);
  5768. insertInstr->InsertBefore(labelDone);
  5769. return;
  5770. }
  5771. IR::Opnd *memAddress = dst;
  5772. if (dst->IsRegOpnd())
  5773. {
  5774. // Create an f64 stack location to store the result of the helper.
  5775. IR::SymOpnd *symOpnd = IR::SymOpnd::New(StackSym::New(dst->GetType(), this->m_func), dst->GetType(), this->m_func);
  5776. this->m_func->StackAllocate(symOpnd->m_sym->AsStackSym(), sizeof(double));
  5777. memAddress = symOpnd;
  5778. }
  5779. // LEA r3, dst
  5780. IR::RegOpnd *reg3Opnd = IR::RegOpnd::New(TyMachReg, this->m_func);
  5781. instr = IR::Instr::New(Js::OpCode::LEA, reg3Opnd, memAddress, this->m_func);
  5782. insertInstr->InsertBefore(instr);
  5783. // to_number_full(value, &dst, scriptContext);
  5784. // Create dummy binary op to convert into helper
  5785. instr = IR::Instr::New(Js::OpCode::Add_A, this->m_func);
  5786. instr->SetSrc1(src);
  5787. instr->SetSrc2(reg3Opnd);
  5788. insertInstr->InsertBefore(instr);
  5789. if (BailOutInfo::IsBailOutOnImplicitCalls(bailOutKind))
  5790. {
  5791. _Analysis_assume_(instrBailOut != nullptr);
  5792. instr = instr->ConvertToBailOutInstr(instrBailOut->GetBailOutInfo(), bailOutKind);
  5793. if (instrBailOut->GetBailOutInfo()->bailOutInstr == instrBailOut)
  5794. {
  5795. IR::Instr * instrShare = instrBailOut->ShareBailOut();
  5796. m_lowerer->LowerBailTarget(instrShare);
  5797. }
  5798. }
  5799. IR::JnHelperMethod helper;
  5800. if (dst->GetType() == TyFloat32)
  5801. {
  5802. helper = IR::HelperOp_ConvFloat_Helper;
  5803. }
  5804. else
  5805. {
  5806. helper = IR::HelperOp_ConvNumber_Helper;
  5807. }
  5808. this->m_lowerer->LowerBinaryHelperMem(instr, helper);
  5809. if (dst->IsRegOpnd())
  5810. {
  5811. if (dst->GetType() == TyFloat32)
  5812. {
  5813. // MOVSS dst, r32
  5814. instr = IR::Instr::New(Js::OpCode::MOVSS, dst, memAddress, this->m_func);
  5815. insertInstr->InsertBefore(instr);
  5816. }
  5817. else
  5818. {
  5819. // MOVSD dst, [pResult].f64
  5820. instr = IR::Instr::New(Js::OpCode::MOVSD, dst, memAddress, this->m_func);
  5821. insertInstr->InsertBefore(instr);
  5822. }
  5823. }
  5824. // $Done
  5825. insertInstr->InsertBefore(labelDone);
  5826. }
  5827. void
  5828. LowererMD::LowerInt4NegWithBailOut(
  5829. IR::Instr *const instr,
  5830. const IR::BailOutKind bailOutKind,
  5831. IR::LabelInstr *const bailOutLabel,
  5832. IR::LabelInstr *const skipBailOutLabel)
  5833. {
  5834. Assert(instr);
  5835. Assert(instr->m_opcode == Js::OpCode::Neg_I4);
  5836. Assert(!instr->HasBailOutInfo());
  5837. Assert(bailOutKind & IR::BailOutOnResultConditions || bailOutKind == IR::BailOutOnFailedHoistedLoopCountBasedBoundCheck);
  5838. Assert(bailOutLabel);
  5839. Assert(instr->m_next == bailOutLabel);
  5840. Assert(skipBailOutLabel);
  5841. instr->ReplaceDst(instr->GetDst()->UseWithNewType(TyInt32, instr->m_func));
  5842. instr->ReplaceSrc1(instr->GetSrc1()->UseWithNewType(TyInt32, instr->m_func));
  5843. // Lower the instruction
  5844. instr->m_opcode = Js::OpCode::NEG;
  5845. Legalize(instr);
  5846. if(bailOutKind & IR::BailOutOnOverflow || bailOutKind == IR::BailOutOnFailedHoistedLoopCountBasedBoundCheck)
  5847. {
  5848. bailOutLabel->InsertBefore(IR::BranchInstr::New(Js::OpCode::JO, bailOutLabel, instr->m_func));
  5849. }
  5850. if(bailOutKind & IR::BailOutOnNegativeZero)
  5851. {
  5852. bailOutLabel->InsertBefore(IR::BranchInstr::New(Js::OpCode::JEQ, bailOutLabel, instr->m_func));
  5853. }
  5854. // Skip bailout
  5855. bailOutLabel->InsertBefore(IR::BranchInstr::New(LowererMD::MDUncondBranchOpcode, skipBailOutLabel, instr->m_func));
  5856. }
  5857. void
  5858. LowererMD::LowerInt4AddWithBailOut(
  5859. IR::Instr *const instr,
  5860. const IR::BailOutKind bailOutKind,
  5861. IR::LabelInstr *const bailOutLabel,
  5862. IR::LabelInstr *const skipBailOutLabel)
  5863. {
  5864. Assert(instr);
  5865. Assert(instr->m_opcode == Js::OpCode::Add_I4);
  5866. Assert(!instr->HasBailOutInfo());
  5867. Assert(
  5868. (bailOutKind & IR::BailOutOnResultConditions) == IR::BailOutOnOverflow ||
  5869. bailOutKind == IR::BailOutOnFailedHoistedLoopCountBasedBoundCheck);
  5870. Assert(bailOutLabel);
  5871. Assert(instr->m_next == bailOutLabel);
  5872. Assert(skipBailOutLabel);
  5873. instr->ReplaceDst(instr->GetDst()->UseWithNewType(TyInt32, instr->m_func));
  5874. instr->ReplaceSrc1(instr->GetSrc1()->UseWithNewType(TyInt32, instr->m_func));
  5875. instr->ReplaceSrc2(instr->GetSrc2()->UseWithNewType(TyInt32, instr->m_func));
  5876. // Restore sources overwritten by the instruction in the bailout path
  5877. const auto dst = instr->GetDst(), src1 = instr->GetSrc1(), src2 = instr->GetSrc2();
  5878. Assert(dst->IsRegOpnd());
  5879. const bool dstEquSrc1 = dst->IsEqual(src1), dstEquSrc2 = dst->IsEqual(src2);
  5880. if(dstEquSrc1 & dstEquSrc2)
  5881. {
  5882. // We have:
  5883. // s1 += s1
  5884. // Which is equivalent to:
  5885. // s1 <<= 1
  5886. //
  5887. // These overflow a signed 32-bit integer when for the initial s1:
  5888. // s1 > 0 && (s1 & 0x40000000) - result is negative after overflow
  5889. // s1 < 0 && !(s1 & 0x40000000) - result is nonnegative after overflow
  5890. //
  5891. // To restore s1 to its value before the operation, we first do an arithmetic right-shift by one bit to undo the
  5892. // left-shift and preserve the sign of the result after overflow. Since the result after overflow always has the
  5893. // opposite sign from the operands (hence the overflow), we just need to invert the sign of the result. The following
  5894. // restores s1 to its value before the instruction:
  5895. // s1 = (s1 >> 1) ^ 0x80000000
  5896. //
  5897. // Generate:
  5898. // sar s1, 1
  5899. // xor s1, 0x80000000
  5900. const auto startBailOutInstr = bailOutLabel->m_next;
  5901. Assert(startBailOutInstr);
  5902. startBailOutInstr->InsertBefore(
  5903. IR::Instr::New(
  5904. Js::OpCode::SAR,
  5905. dst,
  5906. dst,
  5907. IR::IntConstOpnd::New(1, TyInt8, instr->m_func),
  5908. instr->m_func)
  5909. );
  5910. startBailOutInstr->InsertBefore(
  5911. IR::Instr::New(
  5912. Js::OpCode::XOR,
  5913. dst,
  5914. dst,
  5915. IR::IntConstOpnd::New(INT32_MIN, TyInt32, instr->m_func, true /* dontEncode */),
  5916. instr->m_func)
  5917. );
  5918. }
  5919. else if(dstEquSrc1 | dstEquSrc2)
  5920. {
  5921. // We have:
  5922. // s1 += s2
  5923. // Or:
  5924. // s1 = s2 + s1
  5925. //
  5926. // The following restores s1 to its value before the instruction:
  5927. // s1 -= s2
  5928. //
  5929. // Generate:
  5930. // sub s1, s2
  5931. if(dstEquSrc1)
  5932. {
  5933. Assert(src2->IsRegOpnd() || src2->IsIntConstOpnd());
  5934. }
  5935. else
  5936. {
  5937. Assert(src1->IsRegOpnd() || src1->IsIntConstOpnd());
  5938. }
  5939. bailOutLabel->InsertAfter(IR::Instr::New(Js::OpCode::SUB, dst, dst, dstEquSrc1 ? src2 : src1, instr->m_func));
  5940. }
  5941. // Lower the instruction
  5942. ChangeToAdd(instr, true /* needFlags */);
  5943. Legalize(instr);
  5944. // Skip bailout on no overflow
  5945. bailOutLabel->InsertBefore(IR::BranchInstr::New(Js::OpCode::JNO, skipBailOutLabel, instr->m_func));
  5946. // Fall through to bailOutLabel
  5947. }
  5948. void
  5949. LowererMD::LowerInt4SubWithBailOut(
  5950. IR::Instr *const instr,
  5951. const IR::BailOutKind bailOutKind,
  5952. IR::LabelInstr *const bailOutLabel,
  5953. IR::LabelInstr *const skipBailOutLabel)
  5954. {
  5955. Assert(instr);
  5956. Assert(instr->m_opcode == Js::OpCode::Sub_I4);
  5957. Assert(!instr->HasBailOutInfo());
  5958. Assert(
  5959. (bailOutKind & IR::BailOutOnResultConditions) == IR::BailOutOnOverflow ||
  5960. bailOutKind == IR::BailOutOnFailedHoistedLoopCountBasedBoundCheck);
  5961. Assert(bailOutLabel);
  5962. Assert(instr->m_next == bailOutLabel);
  5963. Assert(skipBailOutLabel);
  5964. instr->ReplaceDst(instr->GetDst()->UseWithNewType(TyInt32, instr->m_func));
  5965. instr->ReplaceSrc1(instr->GetSrc1()->UseWithNewType(TyInt32, instr->m_func));
  5966. instr->ReplaceSrc2(instr->GetSrc2()->UseWithNewType(TyInt32, instr->m_func));
  5967. // Restore sources overwritten by the instruction in the bailout path
  5968. const auto dst = instr->GetDst(), src1 = instr->GetSrc1(), src2 = instr->GetSrc2();
  5969. Assert(dst->IsRegOpnd());
  5970. const bool dstEquSrc1 = dst->IsEqual(src1), dstEquSrc2 = dst->IsEqual(src2);
  5971. if(dstEquSrc1 ^ dstEquSrc2)
  5972. {
  5973. // We have:
  5974. // s1 -= s2
  5975. // Or:
  5976. // s1 = s2 - s1
  5977. //
  5978. // The following restores s1 to its value before the instruction:
  5979. // s1 += s2
  5980. // Or:
  5981. // s1 = s2 - s1
  5982. //
  5983. // Generate:
  5984. // neg s1 - only for second case
  5985. // add s1, s2
  5986. if(dstEquSrc1)
  5987. {
  5988. Assert(src2->IsRegOpnd() || src2->IsIntConstOpnd());
  5989. }
  5990. else
  5991. {
  5992. Assert(src1->IsRegOpnd() || src1->IsIntConstOpnd());
  5993. }
  5994. const auto startBailOutInstr = bailOutLabel->m_next;
  5995. Assert(startBailOutInstr);
  5996. if(dstEquSrc2)
  5997. {
  5998. startBailOutInstr->InsertBefore(IR::Instr::New(Js::OpCode::NEG, dst, dst, instr->m_func));
  5999. }
  6000. startBailOutInstr->InsertBefore(IR::Instr::New(Js::OpCode::ADD, dst, dst, dstEquSrc1 ? src2 : src1, instr->m_func));
  6001. }
  6002. // Lower the instruction
  6003. ChangeToSub(instr, true /* needFlags */);
  6004. Legalize(instr);
  6005. // Skip bailout on no overflow
  6006. bailOutLabel->InsertBefore(IR::BranchInstr::New(Js::OpCode::JNO, skipBailOutLabel, instr->m_func));
  6007. // Fall through to bailOutLabel
  6008. }
  6009. bool
  6010. LowererMD::GenerateSimplifiedInt4Mul(
  6011. IR::Instr *const mulInstr,
  6012. const IR::BailOutKind bailOutKind,
  6013. IR::LabelInstr *const bailOutLabel)
  6014. {
  6015. if (AutoSystemInfo::Data.IsAtomPlatform())
  6016. {
  6017. // On Atom, always optimize unless phase is off
  6018. if (PHASE_OFF(Js::AtomPhase, mulInstr->m_func->GetTopFunc()) ||
  6019. PHASE_OFF(Js::MulStrengthReductionPhase, mulInstr->m_func->GetTopFunc()))
  6020. return false;
  6021. }
  6022. else
  6023. {
  6024. // On other platforms, don't optimize unless phase is forced
  6025. if (!PHASE_FORCE(Js::AtomPhase, mulInstr->m_func->GetTopFunc()) &&
  6026. !PHASE_FORCE(Js::MulStrengthReductionPhase, mulInstr->m_func->GetTopFunc()))
  6027. return false;
  6028. }
  6029. Assert(mulInstr);
  6030. Assert(mulInstr->m_opcode == Js::OpCode::Mul_I4);
  6031. IR::Instr *instr = mulInstr, *nextInstr;
  6032. const auto dst = instr->GetDst(), src1 = instr->GetSrc1(), src2 = instr->GetSrc2();
  6033. if (!src1->IsIntConstOpnd() && !src2->IsIntConstOpnd())
  6034. return false;
  6035. // if two const operands, GlobOpt would have folded the computation
  6036. Assert(!(src1->IsIntConstOpnd() && src2->IsIntConstOpnd()));
  6037. Assert(dst->IsRegOpnd());
  6038. const auto constSrc = src1->IsIntConstOpnd() ? src1 : src2;
  6039. const auto nonConstSrc = src1->IsIntConstOpnd() ? src2 : src1;
  6040. const auto constSrcValue = constSrc->AsIntConstOpnd()->AsInt32();
  6041. auto nonConstSrcCopy = nonConstSrc;
  6042. Assert(nonConstSrc->IsRegOpnd());
  6043. bool doOVF = bailOutKind & IR::BailOutOnMulOverflow || bailOutKind == IR::BailOutOnFailedHoistedLoopCountBasedBoundCheck;
  6044. // don't simplify mul by large numbers with OF check
  6045. if (doOVF && (constSrcValue > 3 || constSrcValue < -3))
  6046. return false;
  6047. switch(constSrcValue)
  6048. {
  6049. case -3:
  6050. case 3:
  6051. // if dst = src, we need to have a copy of the src for the ADD/SUB
  6052. if (dst->IsEqual(nonConstSrc))
  6053. {
  6054. nonConstSrcCopy = IR::RegOpnd::New(nonConstSrc->GetType(), instr->m_func);
  6055. // MOV
  6056. Lowerer::InsertMove(nonConstSrcCopy, nonConstSrc, instr);
  6057. }
  6058. instr->UnlinkSrc1();
  6059. instr->UnlinkSrc2();
  6060. // SHL
  6061. instr->m_opcode = Js::OpCode::SHL;
  6062. instr->SetSrc1(nonConstSrc);
  6063. instr->SetSrc2(IR::IntConstOpnd::New((IntConstType) 1, TyInt32, instr->m_func));
  6064. constSrc->Free(instr->m_func);
  6065. Legalize(instr);
  6066. // JO
  6067. if (doOVF)
  6068. {
  6069. nextInstr = IR::BranchInstr::New(Js::OpCode::JO, bailOutLabel, instr->m_func);
  6070. instr->InsertAfter(nextInstr);
  6071. instr = nextInstr;
  6072. }
  6073. // ADD
  6074. nextInstr = IR::Instr::New(Js::OpCode::ADD, dst, dst, nonConstSrcCopy, instr->m_func);
  6075. instr->InsertAfter(nextInstr);
  6076. instr = nextInstr;
  6077. Legalize(instr);
  6078. if (constSrcValue == -3)
  6079. {
  6080. // JO
  6081. if (doOVF)
  6082. {
  6083. nextInstr = IR::BranchInstr::New(Js::OpCode::JO, bailOutLabel, instr->m_func);
  6084. instr->InsertAfter(nextInstr);
  6085. instr = nextInstr;
  6086. }
  6087. // NEG
  6088. nextInstr = IR::Instr::New(Js::OpCode::NEG, dst, dst, instr->m_func);
  6089. instr->InsertAfter(nextInstr);
  6090. instr = nextInstr;
  6091. Legalize(instr);
  6092. }
  6093. // last JO inserted by caller
  6094. return true;
  6095. case -2:
  6096. case 2:
  6097. instr->UnlinkSrc1();
  6098. instr->UnlinkSrc2();
  6099. // SHL
  6100. instr->m_opcode = Js::OpCode::SHL;
  6101. instr->SetSrc1(nonConstSrc);
  6102. instr->SetSrc2(IR::IntConstOpnd::New((IntConstType) 1, TyInt32, instr->m_func));
  6103. constSrc->Free(instr->m_func);
  6104. Legalize(instr);
  6105. if (constSrcValue == -2)
  6106. {
  6107. // JO
  6108. if (doOVF)
  6109. {
  6110. nextInstr = IR::BranchInstr::New(Js::OpCode::JO, bailOutLabel, instr->m_func);
  6111. instr->InsertAfter(nextInstr);
  6112. instr = nextInstr;
  6113. }
  6114. // NEG
  6115. nextInstr = IR::Instr::New(Js::OpCode::NEG, dst, dst, instr->m_func);
  6116. instr->InsertAfter(nextInstr);
  6117. instr = nextInstr;
  6118. Legalize(instr);
  6119. }
  6120. // last JO inserted by caller
  6121. return true;
  6122. case -1:
  6123. instr->UnlinkSrc1();
  6124. instr->UnlinkSrc2();
  6125. // NEG
  6126. instr->m_opcode = Js::OpCode::NEG;
  6127. instr->SetSrc1(nonConstSrc);
  6128. constSrc->Free(instr->m_func);
  6129. Legalize(instr);
  6130. // JO inserted by caller
  6131. return true;
  6132. case 0:
  6133. instr->FreeSrc1();
  6134. instr->FreeSrc2();
  6135. // MOV
  6136. instr->m_opcode = Js::OpCode::MOV;
  6137. instr->SetSrc1(IR::IntConstOpnd::New((IntConstType) 0, TyInt32, instr->m_func));
  6138. Legalize(instr);
  6139. // JO inserted by caller are removed in later phases
  6140. return true;
  6141. case 1:
  6142. instr->UnlinkSrc1();
  6143. instr->UnlinkSrc2();
  6144. // MOV
  6145. instr->m_opcode = Js::OpCode::MOV;
  6146. instr->SetSrc1(nonConstSrc);
  6147. constSrc->Free(instr->m_func);
  6148. Legalize(instr);
  6149. // JO inserted by caller are removed in later phases
  6150. return true;
  6151. default:
  6152. // large numbers with no OF check
  6153. Assert(!doOVF);
  6154. // 2^i
  6155. // -2^i
  6156. if (Math::IsPow2(constSrcValue) || Math::IsPow2(-constSrcValue))
  6157. {
  6158. uint32 shamt = constSrcValue > 0 ? Math::Log2(constSrcValue) : Math::Log2(-constSrcValue);
  6159. instr->UnlinkSrc1();
  6160. instr->UnlinkSrc2();
  6161. // SHL
  6162. instr->m_opcode = Js::OpCode::SHL;
  6163. instr->SetSrc1(nonConstSrc);
  6164. instr->SetSrc2(IR::IntConstOpnd::New((IntConstType) shamt, TyInt32, instr->m_func));
  6165. constSrc->Free(instr->m_func);
  6166. Legalize(instr);
  6167. if (constSrcValue < 0)
  6168. {
  6169. // NEG
  6170. nextInstr = IR::Instr::New(Js::OpCode::NEG, dst, dst, instr->m_func);
  6171. instr->InsertAfter(nextInstr);
  6172. Legalize(instr);
  6173. }
  6174. return true;
  6175. }
  6176. // 2^i + 1
  6177. // 2^i - 1
  6178. if (Math::IsPow2(constSrcValue - 1) || Math::IsPow2(constSrcValue + 1))
  6179. {
  6180. bool plusOne = Math::IsPow2(constSrcValue - 1);
  6181. uint32 shamt = plusOne ? Math::Log2(constSrcValue - 1) : Math::Log2(constSrcValue + 1);
  6182. if (dst->IsEqual(nonConstSrc))
  6183. {
  6184. nonConstSrcCopy = IR::RegOpnd::New(nonConstSrc->GetType(), instr->m_func);
  6185. // MOV
  6186. Lowerer::InsertMove(nonConstSrcCopy, nonConstSrc, instr);
  6187. }
  6188. instr->UnlinkSrc1();
  6189. instr->UnlinkSrc2();
  6190. // SHL
  6191. instr->m_opcode = Js::OpCode::SHL;
  6192. instr->SetSrc1(nonConstSrc);
  6193. instr->SetSrc2(IR::IntConstOpnd::New((IntConstType) shamt, TyInt32, instr->m_func));
  6194. constSrc->Free(instr->m_func);
  6195. Legalize(instr);
  6196. // ADD/SUB
  6197. nextInstr = IR::Instr::New(plusOne ? Js::OpCode::ADD : Js::OpCode::SUB, dst, dst, nonConstSrcCopy, instr->m_func);
  6198. instr->InsertAfter(nextInstr);
  6199. instr = nextInstr;
  6200. Legalize(instr);
  6201. return true;
  6202. }
  6203. return false;
  6204. }
  6205. }
  6206. void
  6207. LowererMD::LowerInt4MulWithBailOut(
  6208. IR::Instr *const instr,
  6209. const IR::BailOutKind bailOutKind,
  6210. IR::LabelInstr *const bailOutLabel,
  6211. IR::LabelInstr *const skipBailOutLabel)
  6212. {
  6213. Assert(instr);
  6214. Assert(instr->m_opcode == Js::OpCode::Mul_I4);
  6215. Assert(!instr->HasBailOutInfo());
  6216. Assert(bailOutKind & IR::BailOutOnResultConditions || bailOutKind == IR::BailOutOnFailedHoistedLoopCountBasedBoundCheck);
  6217. Assert(bailOutLabel);
  6218. Assert(instr->m_next == bailOutLabel);
  6219. Assert(skipBailOutLabel);
  6220. instr->ReplaceDst(instr->GetDst()->UseWithNewType(TyInt32, instr->m_func));
  6221. instr->ReplaceSrc1(instr->GetSrc1()->UseWithNewType(TyInt32, instr->m_func));
  6222. instr->ReplaceSrc2(instr->GetSrc2()->UseWithNewType(TyInt32, instr->m_func));
  6223. IR::LabelInstr *checkForNegativeZeroLabel = nullptr;
  6224. if(bailOutKind & IR::BailOutOnNegativeZero)
  6225. {
  6226. // We have:
  6227. // s3 = s1 * s2
  6228. //
  6229. // If the result is zero, we need to check and only bail out if it would be -0. The following determines this:
  6230. // bailOut = (s1 < 0 || s2 < 0) (either s1 or s2 has to be zero for the result to be zero, so we don't emit zero checks)
  6231. //
  6232. // Note, however, that if in future we decide to ignore mul overflow in some cases, and overflow occurs with one of the operands as negative,
  6233. // this can lead to bailout. Will handle that case if ever we decide to ignore mul overflow.
  6234. //
  6235. // Generate:
  6236. // $checkForNegativeZeroLabel:
  6237. // test s1, s1
  6238. // js $bailOutLabel
  6239. // test s2, s2
  6240. // jns $skipBailOutLabel
  6241. // (fall through to bail out)
  6242. const auto dst = instr->GetDst(), src1 = instr->GetSrc1(), src2 = instr->GetSrc2();
  6243. Assert(dst->IsRegOpnd());
  6244. Assert(!src1->IsEqual(src2)); // cannot result in -0 if both operands are the same; GlobOpt should have figured that out
  6245. checkForNegativeZeroLabel = IR::LabelInstr::New(Js::OpCode::Label, instr->m_func, true);
  6246. bailOutLabel->InsertBefore(checkForNegativeZeroLabel);
  6247. if(src1->IsIntConstOpnd() || src2->IsIntConstOpnd())
  6248. {
  6249. Assert(!(src1->IsIntConstOpnd() && src2->IsIntConstOpnd())); // if this results in -0, GlobOpt should have avoided type specialization
  6250. const auto constSrc = src1->IsIntConstOpnd() ? src1 : src2;
  6251. const auto nonConstSrc = src1->IsIntConstOpnd() ? src2 : src1;
  6252. Assert(nonConstSrc->IsRegOpnd());
  6253. const auto newInstr = IR::Instr::New(Js::OpCode::TEST, instr->m_func);
  6254. newInstr->SetSrc1(nonConstSrc);
  6255. newInstr->SetSrc2(nonConstSrc);
  6256. bailOutLabel->InsertBefore(newInstr);
  6257. const auto constSrcValue = constSrc->AsIntConstOpnd()->GetValue();
  6258. if(constSrcValue == 0)
  6259. {
  6260. bailOutLabel->InsertBefore(IR::BranchInstr::New(Js::OpCode::JNSB, skipBailOutLabel, instr->m_func));
  6261. }
  6262. else
  6263. {
  6264. Assert(constSrcValue < 0); // cannot result in -0 if one operand is positive; GlobOpt should have figured that out
  6265. bailOutLabel->InsertBefore(IR::BranchInstr::New(Js::OpCode::JNE, skipBailOutLabel, instr->m_func));
  6266. }
  6267. }
  6268. else
  6269. {
  6270. auto newInstr = IR::Instr::New(Js::OpCode::TEST, instr->m_func);
  6271. newInstr->SetSrc1(src1);
  6272. newInstr->SetSrc2(src1);
  6273. bailOutLabel->InsertBefore(newInstr);
  6274. bailOutLabel->InsertBefore(IR::BranchInstr::New(Js::OpCode::JSB, bailOutLabel, instr->m_func));
  6275. newInstr = IR::Instr::New(Js::OpCode::TEST, instr->m_func);
  6276. newInstr->SetSrc1(src2);
  6277. newInstr->SetSrc2(src2);
  6278. bailOutLabel->InsertBefore(newInstr);
  6279. bailOutLabel->InsertBefore(IR::BranchInstr::New(Js::OpCode::JNSB, skipBailOutLabel, instr->m_func));
  6280. }
  6281. // Fall through to bailOutLabel
  6282. }
  6283. const bool needsOverflowCheck =
  6284. bailOutKind & IR::BailOutOnMulOverflow || bailOutKind == IR::BailOutOnFailedHoistedLoopCountBasedBoundCheck;
  6285. AssertMsg(!instr->ShouldCheckForNon32BitOverflow() || (needsOverflowCheck && instr->ShouldCheckForNon32BitOverflow()), "Non 32-bit overflow check required without bailout info");
  6286. bool simplifiedMul = LowererMD::GenerateSimplifiedInt4Mul(instr, bailOutKind, bailOutLabel);
  6287. // Lower the instruction
  6288. if (!simplifiedMul)
  6289. {
  6290. LowererMD::ChangeToIMul(instr, needsOverflowCheck);
  6291. }
  6292. const auto insertBeforeInstr = checkForNegativeZeroLabel ? checkForNegativeZeroLabel : bailOutLabel;
  6293. if(needsOverflowCheck)
  6294. {
  6295. // do we care about int32 or non-int32 overflow ?
  6296. if (!simplifiedMul && !instr->ShouldCheckFor32BitOverflow() && instr->ShouldCheckForNon32BitOverflow())
  6297. LowererMD::EmitNon32BitOvfCheck(instr, insertBeforeInstr, bailOutLabel);
  6298. else
  6299. insertBeforeInstr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JO, bailOutLabel, instr->m_func));
  6300. }
  6301. if(bailOutKind & IR::BailOutOnNegativeZero)
  6302. {
  6303. // On zero, branch to determine whether the result would be -0
  6304. Assert(checkForNegativeZeroLabel);
  6305. const auto newInstr = IR::Instr::New(Js::OpCode::TEST, instr->m_func);
  6306. const auto dst = instr->GetDst();
  6307. newInstr->SetSrc1(dst);
  6308. newInstr->SetSrc2(dst);
  6309. insertBeforeInstr->InsertBefore(newInstr);
  6310. insertBeforeInstr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JEQ, checkForNegativeZeroLabel, instr->m_func));
  6311. }
  6312. // Skip bailout
  6313. insertBeforeInstr->InsertBefore(IR::BranchInstr::New(LowererMD::MDUncondBranchOpcode, skipBailOutLabel, instr->m_func));
  6314. }
  6315. void
  6316. LowererMD::LowerInt4RemWithBailOut(
  6317. IR::Instr *const instr,
  6318. const IR::BailOutKind bailOutKind,
  6319. IR::LabelInstr *const bailOutLabel,
  6320. IR::LabelInstr *const skipBailOutLabel) const
  6321. {
  6322. Assert(instr);
  6323. Assert(instr->m_opcode == Js::OpCode::Rem_I4);
  6324. Assert(!instr->HasBailOutInfo());
  6325. Assert(bailOutKind & IR::BailOutOnNegativeZero);
  6326. Assert(bailOutLabel);
  6327. Assert(instr->m_next == bailOutLabel);
  6328. Assert(skipBailOutLabel);
  6329. instr->ReplaceDst(instr->GetDst()->UseWithNewType(TyInt32, instr->m_func));
  6330. instr->ReplaceSrc1(instr->GetSrc1()->UseWithNewType(TyInt32, instr->m_func));
  6331. instr->ReplaceSrc2(instr->GetSrc2()->UseWithNewType(TyInt32, instr->m_func));
  6332. bool fastPath = m_lowerer->GenerateSimplifiedInt4Rem(instr, skipBailOutLabel);
  6333. // We have:
  6334. // s3 = s1 % s2
  6335. //
  6336. // If the result is zero, we need to check and only bail out if it would be -0. The following determines this:
  6337. // bailOut = (s3 == 0 && s1 < 0)
  6338. //
  6339. // Generate:
  6340. // $checkForNegativeZeroLabel:
  6341. // test s3, s3
  6342. // jne $skipBailOutLabel
  6343. // test s1, s1
  6344. // jns $skipBailOutLabel
  6345. // (fall through to bail out)
  6346. IR::Opnd *dst = instr->GetDst(), *src1 = instr->GetSrc1();
  6347. Assert(dst->IsRegOpnd());
  6348. IR::Instr * newInstr = IR::Instr::New(Js::OpCode::TEST, instr->m_func);
  6349. newInstr->SetSrc1(dst);
  6350. newInstr->SetSrc2(dst);
  6351. bailOutLabel->InsertBefore(newInstr);
  6352. bailOutLabel->InsertBefore(IR::BranchInstr::New(Js::OpCode::JNE, skipBailOutLabel, instr->m_func));
  6353. // Fast path already checks if s1 >= 0
  6354. if (!fastPath)
  6355. {
  6356. newInstr = IR::Instr::New(Js::OpCode::TEST, instr->m_func);
  6357. newInstr->SetSrc1(src1);
  6358. newInstr->SetSrc2(src1);
  6359. bailOutLabel->InsertBefore(newInstr);
  6360. bailOutLabel->InsertBefore(IR::BranchInstr::New(Js::OpCode::JNSB, skipBailOutLabel, instr->m_func));
  6361. }
  6362. // Fall through to bailOutLabel
  6363. // Lower the instruction
  6364. LowererMDArch::EmitInt4Instr(instr);
  6365. }
  6366. IR::Instr *
  6367. LowererMD::LoadFloatZero(IR::Opnd * opndDst, IR::Instr * instrInsert)
  6368. {
  6369. IR::Instr * instr = IR::Instr::New(Js::OpCode::MOVSD_ZERO, opndDst, instrInsert->m_func);
  6370. instrInsert->InsertBefore(instr);
  6371. return instr;
  6372. }
  6373. IR::Instr *
  6374. LowererMD::LoadFloatValue(IR::Opnd * opndDst, double value, IR::Instr * instrInsert)
  6375. {
  6376. if (value == 0.0 && !Js::JavascriptNumber::IsNegZero(value))
  6377. {
  6378. // zero can be loaded with "XORPS xmm, xmm" rather than needing memory load
  6379. return LoadFloatZero(opndDst, instrInsert);
  6380. }
  6381. IR::Opnd * opnd;
  6382. void* pValue = nullptr;
  6383. bool isFloat64 = opndDst->IsFloat64();
  6384. if (isFloat64)
  6385. {
  6386. pValue = NativeCodeDataNewNoFixup(instrInsert->m_func->GetNativeCodeDataAllocator(), DoubleType<DataDesc_LowererMD_LoadFloatValue_Double>, value);
  6387. }
  6388. else
  6389. {
  6390. Assert(opndDst->IsFloat32());
  6391. pValue = (float*)NativeCodeDataNewNoFixup(instrInsert->m_func->GetNativeCodeDataAllocator(), FloatType<DataDesc_LowererMD_LoadFloatValue_Float>, (float)value);
  6392. }
  6393. if (!instrInsert->m_func->IsOOPJIT())
  6394. {
  6395. opnd = IR::MemRefOpnd::New((void*)pValue, isFloat64 ? TyMachDouble : TyFloat32,
  6396. instrInsert->m_func, isFloat64 ? IR::AddrOpndKindDynamicDoubleRef : IR::AddrOpndKindDynamicFloatRef);
  6397. }
  6398. else // OOP JIT
  6399. {
  6400. int offset = NativeCodeData::GetDataTotalOffset(pValue);
  6401. auto addressRegOpnd = IR::RegOpnd::New(TyMachPtr, instrInsert->m_func);
  6402. Lowerer::InsertMove(
  6403. addressRegOpnd,
  6404. IR::MemRefOpnd::New(instrInsert->m_func->GetWorkItem()->GetWorkItemData()->nativeDataAddr, TyMachPtr, instrInsert->m_func, IR::AddrOpndKindDynamicNativeCodeDataRef),
  6405. instrInsert);
  6406. opnd = IR::IndirOpnd::New(addressRegOpnd, offset, isFloat64 ? TyMachDouble : TyFloat32,
  6407. #if DBG
  6408. NativeCodeData::GetDataDescription(pValue, instrInsert->m_func->m_alloc),
  6409. #endif
  6410. instrInsert->m_func);
  6411. }
  6412. // movsd xmm, [reg+offset]
  6413. IR::Instr * instr = IR::Instr::New(LowererMDArch::GetAssignOp(opndDst->GetType()), opndDst, opnd, instrInsert->m_func);
  6414. instrInsert->InsertBefore(instr);
  6415. Legalize(instr);
  6416. return instr;
  6417. }
  6418. IR::Instr *
  6419. LowererMD::EnsureAdjacentArgs(IR::Instr * instrArg)
  6420. {
  6421. // Ensure that the arg instructions for a given call site are adjacent.
  6422. // This isn't normally desirable for CQ, but it's required by, for instance, the cloner,
  6423. // which must clone a complete call sequence.
  6424. IR::Opnd * opnd = instrArg->GetSrc2();
  6425. IR::Instr * instrNextArg;
  6426. StackSym * sym;
  6427. AssertMsg(opnd, "opnd");
  6428. while (opnd->IsSymOpnd())
  6429. {
  6430. sym = opnd->AsSymOpnd()->m_sym->AsStackSym();
  6431. instrNextArg = sym->m_instrDef;
  6432. Assert(instrNextArg);
  6433. instrNextArg->SinkInstrBefore(instrArg);
  6434. instrArg = instrNextArg;
  6435. opnd = instrArg->GetSrc2();
  6436. }
  6437. sym = opnd->AsRegOpnd()->m_sym;
  6438. instrNextArg = sym->m_instrDef;
  6439. Assert(instrNextArg && instrNextArg->m_opcode == Js::OpCode::StartCall);
  6440. // The StartCall can be trivially moved down.
  6441. if (instrNextArg->m_next != instrArg)
  6442. {
  6443. instrNextArg->UnlinkStartCallFromBailOutInfo(instrArg);
  6444. instrNextArg->Unlink();
  6445. instrArg->InsertBefore(instrNextArg);
  6446. }
  6447. return instrNextArg->m_prev;
  6448. }
  6449. #if INT32VAR
  6450. //
  6451. // Convert an int32 to Var representation.
  6452. //
  6453. void LowererMD::GenerateInt32ToVarConversion( IR::Opnd * opndSrc, IR::Instr * insertInstr )
  6454. {
  6455. AssertMsg(TySize[opndSrc->GetType()] == MachPtr, "For this to work it should be a 64-bit register");
  6456. IR::Instr* instr = IR::Instr::New(Js::OpCode::BTS, opndSrc, opndSrc, IR::IntConstOpnd::New(Js::VarTag_Shift, TyInt8, this->m_func), this->m_func);
  6457. insertInstr->InsertBefore(instr);
  6458. }
  6459. //
  6460. // jump to $labelHelper, based on the result of CMP
  6461. //
  6462. void LowererMD::GenerateSmIntTest(IR::Opnd *opndSrc, IR::Instr *insertInstr, IR::LabelInstr *labelHelper, IR::Instr **instrFirst /* = nullptr */, bool fContinueLabel /*= false*/)
  6463. {
  6464. AssertMsg(opndSrc->GetSize() == MachPtr, "64-bit register required");
  6465. IR::Opnd * opndReg = IR::RegOpnd::New(TyMachReg, this->m_func);
  6466. #ifdef SHIFTLOAD
  6467. // s1 = SHLD src1, 16 - Shift top 16-bits of src1 to s1
  6468. IR::Instr* instr = IR::Instr::New(Js::OpCode::SHLD, opndReg, opndSrc, IR::IntConstOpnd::New(16, TyInt8, this->m_func), this->m_func);
  6469. insertInstr->InsertBefore(instr);
  6470. if (instrFirst)
  6471. {
  6472. *instrFirst = instr;
  6473. }
  6474. // CMP s1.i16, AtomTag.i16
  6475. IR::Opnd *opndReg16 = opndReg->Copy(m_func);
  6476. opndReg16->SetType(TyInt16);
  6477. instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  6478. instr->SetSrc1(opndReg16);
  6479. instr->SetSrc2(IR::IntConstOpnd::New(Js::AtomTag, TyInt16, this->m_func, /* dontEncode = */ true));
  6480. insertInstr->InsertBefore(instr);
  6481. #else
  6482. // s1 = MOV src1 - Move to a temporary
  6483. IR::Instr * instr = IR::Instr::New(Js::OpCode::MOV, opndReg, opndSrc, this->m_func);
  6484. insertInstr->InsertBefore(instr);
  6485. if (instrFirst)
  6486. {
  6487. *instrFirst = instr;
  6488. }
  6489. // s1 = SHR s1, VarTag_Shift
  6490. instr = IR::Instr::New(Js::OpCode::SHR, opndReg, opndReg, IR::IntConstOpnd::New(Js::VarTag_Shift, TyInt8, this->m_func), this->m_func);
  6491. insertInstr->InsertBefore(instr);
  6492. // CMP s1, AtomTag
  6493. instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  6494. instr->SetSrc1(opndReg);
  6495. instr->SetSrc2(IR::IntConstOpnd::New(Js::AtomTag, TyInt32, this->m_func, /* dontEncode = */ true));
  6496. insertInstr->InsertBefore(instr);
  6497. #endif
  6498. if(fContinueLabel)
  6499. {
  6500. // JEQ $labelHelper
  6501. instr = IR::BranchInstr::New(Js::OpCode::JEQ, labelHelper, this->m_func);
  6502. }
  6503. else
  6504. {
  6505. // JNE $labelHelper
  6506. instr = IR::BranchInstr::New(Js::OpCode::JNE, labelHelper, this->m_func);
  6507. }
  6508. insertInstr->InsertBefore(instr);
  6509. }
  6510. //
  6511. // If lower 32-bits are zero (value is zero), jump to $helper.
  6512. //
  6513. void LowererMD::GenerateTaggedZeroTest( IR::Opnd * opndSrc, IR::Instr * insertInstr, IR::LabelInstr * labelHelper )
  6514. {
  6515. // Cast the var to 32 bit integer.
  6516. if(opndSrc->GetSize() != 4)
  6517. {
  6518. opndSrc = opndSrc->UseWithNewType(TyUint32, this->m_func);
  6519. }
  6520. AssertMsg(TySize[opndSrc->GetType()] == 4, "This technique works only on the 32-bit version");
  6521. // TEST src1, src1
  6522. IR::Instr* instr = IR::Instr::New(Js::OpCode::TEST, this->m_func);
  6523. instr->SetSrc1(opndSrc);
  6524. instr->SetSrc2(opndSrc);
  6525. insertInstr->InsertBefore(instr);
  6526. if(labelHelper != nullptr)
  6527. {
  6528. // JZ $labelHelper
  6529. instr = IR::BranchInstr::New(Js::OpCode::JEQ, labelHelper, this->m_func);
  6530. insertInstr->InsertBefore(instr);
  6531. }
  6532. }
  6533. //
  6534. // If top 16 bits are not zero i.e. it is NOT object, jump to $helper.
  6535. //
  6536. bool LowererMD::GenerateObjectTest(IR::Opnd * opndSrc, IR::Instr * insertInstr, IR::LabelInstr * labelTarget, bool fContinueLabel)
  6537. {
  6538. AssertMsg(opndSrc->GetSize() == MachPtr, "64-bit register required");
  6539. if (opndSrc->IsTaggedValue() && fContinueLabel)
  6540. {
  6541. // Insert delete branch opcode to tell the dbChecks not to assert on the helper label we may fall through into
  6542. IR::Instr *fakeBr = IR::PragmaInstr::New(Js::OpCode::DeletedNonHelperBranch, 0, this->m_func);
  6543. insertInstr->InsertBefore(fakeBr);
  6544. return false;
  6545. }
  6546. else if (opndSrc->IsNotTaggedValue() && !fContinueLabel)
  6547. {
  6548. return false;
  6549. }
  6550. IR::Opnd * opndReg = IR::RegOpnd::New(TyMachReg, this->m_func);
  6551. // s1 = MOV src1 - Move to a temporary
  6552. IR::Instr * instr = IR::Instr::New(Js::OpCode::MOV, opndReg, opndSrc, this->m_func);
  6553. insertInstr->InsertBefore(instr);
  6554. // s1 = SHR s1, VarTag_Shift
  6555. instr = IR::Instr::New(Js::OpCode::SHR, opndReg, opndReg, IR::IntConstOpnd::New(Js::VarTag_Shift, TyInt8, this->m_func), this->m_func);
  6556. insertInstr->InsertBefore(instr);
  6557. if (fContinueLabel)
  6558. {
  6559. // JEQ $labelHelper
  6560. instr = IR::BranchInstr::New(Js::OpCode::JEQ, labelTarget, this->m_func);
  6561. insertInstr->InsertBefore(instr);
  6562. IR::LabelInstr *labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  6563. insertInstr->InsertBefore(labelHelper);
  6564. }
  6565. else
  6566. {
  6567. // JNZ $labelHelper
  6568. instr = IR::BranchInstr::New(Js::OpCode::JNE, labelTarget, this->m_func);
  6569. insertInstr->InsertBefore(instr);
  6570. }
  6571. return true;
  6572. }
  6573. #else
  6574. //
  6575. // Convert an int32 value to a Var.
  6576. //
  6577. void LowererMD::GenerateInt32ToVarConversion( IR::Opnd * opndSrc, IR::Instr * insertInstr )
  6578. {
  6579. // SHL r1, AtomTag
  6580. IR::Instr * instr = IR::Instr::New(Js::OpCode::SHL, opndSrc, opndSrc, IR::IntConstOpnd::New(Js::AtomTag, TyInt8, this->m_func), this->m_func);
  6581. insertInstr->InsertBefore(instr);
  6582. // INC r1
  6583. instr = IR::Instr::New(Js::OpCode::INC, opndSrc, opndSrc, this->m_func);
  6584. insertInstr->InsertBefore(instr);
  6585. }
  6586. //
  6587. // jump to $labelHelper, based on the result of TEST
  6588. //
  6589. void LowererMD::GenerateSmIntTest(IR::Opnd *opndSrc, IR::Instr *insertInstr, IR::LabelInstr *labelHelper, IR::Instr **instrFirst /* = nullptr */, bool fContinueLabel /*= false*/)
  6590. {
  6591. if (opndSrc->IsTaggedInt() && !fContinueLabel)
  6592. {
  6593. return;
  6594. }
  6595. else if (opndSrc->IsNotTaggedValue() && fContinueLabel)
  6596. {
  6597. return;
  6598. }
  6599. // TEST src1, AtomTag
  6600. IR::Instr* instr = IR::Instr::New(Js::OpCode::TEST, this->m_func);
  6601. instr->SetSrc1(opndSrc);
  6602. instr->SetSrc2(IR::IntConstOpnd::New(Js::AtomTag, TyInt8, this->m_func));
  6603. insertInstr->InsertBefore(instr);
  6604. if (instrFirst)
  6605. {
  6606. *instrFirst = instr;
  6607. }
  6608. if(fContinueLabel)
  6609. {
  6610. // JNE $labelHelper
  6611. instr = IR::BranchInstr::New(Js::OpCode::JNE, labelHelper, this->m_func);
  6612. }
  6613. else
  6614. {
  6615. // JEQ $labelHelper
  6616. instr = IR::BranchInstr::New(Js::OpCode::JEQ, labelHelper, this->m_func);
  6617. }
  6618. insertInstr->InsertBefore(instr);
  6619. }
  6620. //
  6621. // If value is zero in tagged int representation, jump to $labelHelper.
  6622. //
  6623. void LowererMD::GenerateTaggedZeroTest( IR::Opnd * opndSrc, IR::Instr * insertInstr, IR::LabelInstr * labelHelper )
  6624. {
  6625. if (opndSrc->IsNotTaggedValue())
  6626. {
  6627. return;
  6628. }
  6629. // CMP src1, AtomTag
  6630. IR::Instr* instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  6631. instr->SetSrc1(opndSrc);
  6632. instr->SetSrc2(IR::IntConstOpnd::New(Js::AtomTag, TyInt32, this->m_func));
  6633. insertInstr->InsertBefore(instr);
  6634. // JEQ $helper
  6635. if(labelHelper != nullptr)
  6636. {
  6637. // JEQ $labelHelper
  6638. instr = IR::BranchInstr::New(Js::OpCode::JEQ, labelHelper, this->m_func);
  6639. insertInstr->InsertBefore(instr);
  6640. }
  6641. }
  6642. //
  6643. // If not object, jump to $labelHelper.
  6644. //
  6645. bool LowererMD::GenerateObjectTest(IR::Opnd * opndSrc, IR::Instr * insertInstr, IR::LabelInstr * labelTarget, bool fContinueLabel)
  6646. {
  6647. if (opndSrc->IsTaggedInt() && fContinueLabel)
  6648. {
  6649. // Insert delete branch opcode to tell the dbChecks not to assert on this helper label
  6650. IR::Instr *fakeBr = IR::PragmaInstr::New(Js::OpCode::DeletedNonHelperBranch, 0, this->m_func);
  6651. insertInstr->InsertBefore(fakeBr);
  6652. return false;
  6653. }
  6654. else if (opndSrc->IsNotTaggedValue() && !fContinueLabel)
  6655. {
  6656. return false;
  6657. }
  6658. // TEST src1, AtomTag
  6659. IR::Instr* instr = IR::Instr::New(Js::OpCode::TEST, this->m_func);
  6660. instr->SetSrc1(opndSrc);
  6661. instr->SetSrc2(IR::IntConstOpnd::New(Js::AtomTag, TyInt8, this->m_func));
  6662. insertInstr->InsertBefore(instr);
  6663. if (fContinueLabel)
  6664. {
  6665. // JEQ $labelHelper
  6666. instr = IR::BranchInstr::New(Js::OpCode::JEQ, labelTarget, this->m_func);
  6667. insertInstr->InsertBefore(instr);
  6668. IR::LabelInstr *labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  6669. insertInstr->InsertBefore(labelHelper);
  6670. }
  6671. else
  6672. {
  6673. // JNE $labelHelper
  6674. instr = IR::BranchInstr::New(Js::OpCode::JNE, labelTarget, this->m_func);
  6675. insertInstr->InsertBefore(instr);
  6676. }
  6677. return true;
  6678. }
  6679. #endif
  6680. bool LowererMD::GenerateJSBooleanTest(IR::RegOpnd * regSrc, IR::Instr * insertInstr, IR::LabelInstr * labelTarget, bool fContinueLabel)
  6681. {
  6682. IR::Instr* instr;
  6683. if (regSrc->GetValueType().IsBoolean())
  6684. {
  6685. if (fContinueLabel)
  6686. {
  6687. // JMP $labelTarget
  6688. instr = IR::BranchInstr::New(Js::OpCode::JMP, labelTarget, this->m_func);
  6689. insertInstr->InsertBefore(instr);
  6690. #if DBG
  6691. if (labelTarget->isOpHelper)
  6692. {
  6693. labelTarget->m_noHelperAssert = true;
  6694. }
  6695. #endif
  6696. }
  6697. return false;
  6698. }
  6699. // CMP src1, vtable<JavaScriptBoolean>
  6700. instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  6701. IR::IndirOpnd *vtablePtrOpnd = IR::IndirOpnd::New(regSrc, 0, TyMachPtr, this->m_func);
  6702. instr->SetSrc1(vtablePtrOpnd);
  6703. IR::Opnd *jsBooleanVTable = m_lowerer->LoadVTableValueOpnd(insertInstr, VTableValue::VtableJavascriptBoolean);
  6704. instr->SetSrc2(jsBooleanVTable);
  6705. insertInstr->InsertBefore(instr);
  6706. Legalize(instr);
  6707. if (fContinueLabel)
  6708. {
  6709. // JEQ $labelTarget
  6710. instr = IR::BranchInstr::New(Js::OpCode::JEQ, labelTarget, this->m_func);
  6711. insertInstr->InsertBefore(instr);
  6712. IR::LabelInstr *labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  6713. insertInstr->InsertBefore(labelHelper);
  6714. }
  6715. else
  6716. {
  6717. // JNE $labelTarget
  6718. instr = IR::BranchInstr::New(Js::OpCode::JNE, labelTarget, this->m_func);
  6719. insertInstr->InsertBefore(instr);
  6720. }
  6721. return true;
  6722. }
  6723. #if FLOATVAR
  6724. //
  6725. // If any of the top 14 bits are not set, then the var is not a float value and hence, jump to $labelHelper.
  6726. //
  6727. void LowererMD::GenerateFloatTest(IR::RegOpnd * opndSrc, IR::Instr * insertInstr, IR::LabelInstr* labelHelper, const bool checkForNullInLoopBody)
  6728. {
  6729. if (opndSrc->GetValueType().IsFloat())
  6730. {
  6731. return;
  6732. }
  6733. AssertMsg(opndSrc->GetSize() == MachPtr, "64-bit register required");
  6734. // s1 = MOV src1 - Move to a temporary
  6735. IR::Opnd * opndReg = IR::RegOpnd::New(TyMachReg, this->m_func);
  6736. IR::Instr * instr = IR::Instr::New(Js::OpCode::MOV, opndReg, opndSrc, this->m_func);
  6737. insertInstr->InsertBefore(instr);
  6738. // s1 = SHR s1, 50
  6739. instr = IR::Instr::New(Js::OpCode::SHR, opndReg, opndReg, IR::IntConstOpnd::New(50, TyInt8, this->m_func), this->m_func);
  6740. insertInstr->InsertBefore(instr);
  6741. // JZ $helper
  6742. instr = IR::BranchInstr::New(Js::OpCode::JEQ /* JZ */, labelHelper, this->m_func);
  6743. insertInstr->InsertBefore(instr);
  6744. }
  6745. IR::RegOpnd* LowererMD::CheckFloatAndUntag(IR::RegOpnd * opndSrc, IR::Instr * insertInstr, IR::LabelInstr* labelHelper)
  6746. {
  6747. IR::Opnd* floatTag = IR::AddrOpnd::New((Js::Var)Js::FloatTag_Value, IR::AddrOpndKindConstantVar, this->m_func, /* dontEncode = */ true);
  6748. IR::RegOpnd* regOpndFloatTag = IR::RegOpnd::New(TyUint64, this->m_func);
  6749. // MOV floatTagReg, FloatTag_Value
  6750. IR::Instr* instr = IR::Instr::New(Js::OpCode::MOV, regOpndFloatTag, floatTag, this->m_func);
  6751. insertInstr->InsertBefore(instr);
  6752. if (!opndSrc->GetValueType().IsFloat())
  6753. {
  6754. // TEST s1, floatTagReg
  6755. instr = IR::Instr::New(Js::OpCode::TEST, this->m_func);
  6756. instr->SetSrc1(opndSrc);
  6757. instr->SetSrc2(regOpndFloatTag);
  6758. insertInstr->InsertBefore(instr);
  6759. // JZ $helper
  6760. instr = IR::BranchInstr::New(Js::OpCode::JEQ /* JZ */, labelHelper, this->m_func);
  6761. insertInstr->InsertBefore(instr);
  6762. }
  6763. // untaggedFloat = XOR floatTagReg, s1 // where untaggedFloat == floatTagReg; use floatTagReg temporarily for the untagged float
  6764. IR::RegOpnd* untaggedFloat = regOpndFloatTag;
  6765. instr = IR::Instr::New(Js::OpCode::XOR, untaggedFloat, regOpndFloatTag, opndSrc, this->m_func);
  6766. insertInstr->InsertBefore(instr);
  6767. IR::RegOpnd *floatReg = IR::RegOpnd::New(TyMachDouble, this->m_func);
  6768. instr = IR::Instr::New(Js::OpCode::MOVD, floatReg, untaggedFloat, this->m_func);
  6769. insertInstr->InsertBefore(instr);
  6770. return floatReg;
  6771. }
  6772. #else
  6773. void LowererMD::GenerateFloatTest(IR::RegOpnd * opndSrc, IR::Instr * insertInstr, IR::LabelInstr* labelHelper, const bool checkForNullInLoopBody)
  6774. {
  6775. if (opndSrc->GetValueType().IsFloat())
  6776. {
  6777. return;
  6778. }
  6779. AssertMsg(opndSrc->GetSize() == MachPtr, "64-bit register required");
  6780. if(checkForNullInLoopBody && m_func->IsLoopBody())
  6781. {
  6782. // It's possible that the value was determined dead by the jitted function and was not restored. The jitted loop
  6783. // body may not realize that it's dead and may try to use it. Check for null in loop bodies.
  6784. // test src1, src1
  6785. // jz $helper (bail out)
  6786. m_lowerer->InsertCompareBranch(
  6787. opndSrc,
  6788. IR::AddrOpnd::NewNull(m_func),
  6789. Js::OpCode::BrEq_A,
  6790. labelHelper,
  6791. insertInstr);
  6792. }
  6793. IR::Instr* instr = IR::Instr::New(Js::OpCode::CMP, insertInstr->m_func);
  6794. instr->SetSrc1(IR::IndirOpnd::New(opndSrc, 0, TyMachPtr, insertInstr->m_func));
  6795. instr->SetSrc2(m_lowerer->LoadVTableValueOpnd(insertInstr, VTableValue::VtableJavascriptNumber));
  6796. insertInstr->InsertBefore(instr);
  6797. // JNZ $helper
  6798. instr = IR::BranchInstr::New(Js::OpCode::JNE /* JZ */, labelHelper, this->m_func);
  6799. insertInstr->InsertBefore(instr);
  6800. }
  6801. #endif
  6802. #if DBG
  6803. //
  6804. // Helps in debugging of fast paths.
  6805. //
  6806. void LowererMD::GenerateDebugBreak( IR::Instr * insertInstr )
  6807. {
  6808. // int 3
  6809. IR::Instr *int3 = IR::Instr::New(Js::OpCode::INT, insertInstr->m_func);
  6810. int3->SetSrc1(IR::IntConstOpnd::New(3, TyInt32, insertInstr->m_func));
  6811. insertInstr->InsertBefore(int3);
  6812. }
  6813. #endif
  6814. IR::Instr *
  6815. LowererMD::LoadStackAddress(StackSym *sym, IR::RegOpnd *optionalDstOpnd /* = nullptr */)
  6816. {
  6817. IR::RegOpnd * regDst = optionalDstOpnd != nullptr ? optionalDstOpnd : IR::RegOpnd::New(TyMachReg, this->m_func);
  6818. IR::SymOpnd * symSrc = IR::SymOpnd::New(sym, TyMachPtr, this->m_func);
  6819. IR::Instr * lea = IR::Instr::New(Js::OpCode::LEA, regDst, symSrc, this->m_func);
  6820. return lea;
  6821. }
  6822. template <bool verify>
  6823. void
  6824. LowererMD::MakeDstEquSrc1(IR::Instr *const instr)
  6825. {
  6826. Assert(instr);
  6827. Assert(instr->IsLowered());
  6828. Assert(instr->GetDst());
  6829. Assert(instr->GetSrc1());
  6830. if(instr->GetDst()->IsEqual(instr->GetSrc1()))
  6831. {
  6832. return;
  6833. }
  6834. if (verify)
  6835. {
  6836. AssertMsg(false, "Missing legalization");
  6837. return;
  6838. }
  6839. if(instr->GetSrc2() && instr->GetDst()->IsEqual(instr->GetSrc2()))
  6840. {
  6841. switch(instr->m_opcode)
  6842. {
  6843. #ifdef _M_IX86
  6844. case Js::OpCode::ADC:
  6845. #endif
  6846. case Js::OpCode::Add_I4:
  6847. case Js::OpCode::Mul_I4:
  6848. case Js::OpCode::Or_I4:
  6849. case Js::OpCode::Xor_I4:
  6850. case Js::OpCode::And_I4:
  6851. case Js::OpCode::Add_Ptr:
  6852. case Js::OpCode::ADD:
  6853. case Js::OpCode::IMUL2:
  6854. case Js::OpCode::OR:
  6855. case Js::OpCode::XOR:
  6856. case Js::OpCode::AND:
  6857. case Js::OpCode::ADDSD:
  6858. case Js::OpCode::MULSD:
  6859. case Js::OpCode::ADDSS:
  6860. case Js::OpCode::MULSS:
  6861. case Js::OpCode::ADDPS:
  6862. // For (a = b & a), generate (a = a & b)
  6863. instr->SwapOpnds();
  6864. return;
  6865. }
  6866. // For (a = b - a), generate (c = a; a = b - c) and fall through
  6867. ChangeToAssign(instr->HoistSrc2(Js::OpCode::Ld_A));
  6868. }
  6869. // For (a = b - c), generate (a = b; a = a - c)
  6870. IR::Instr *const mov = IR::Instr::New(Js::OpCode::Ld_A, instr->GetDst(), instr->UnlinkSrc1(), instr->m_func);
  6871. instr->InsertBefore(mov);
  6872. ChangeToAssign(mov);
  6873. instr->SetSrc1(instr->GetDst());
  6874. }
  6875. void
  6876. LowererMD::EmitPtrInstr(IR::Instr *instr)
  6877. {
  6878. LowererMDArch::EmitPtrInstr(instr);
  6879. }
  6880. void
  6881. LowererMD::EmitInt64Instr(IR::Instr * instr)
  6882. {
  6883. #ifdef _M_IX86
  6884. lowererMDArch.EmitInt64Instr(instr);
  6885. #else
  6886. Assert(UNREACHED);
  6887. #endif
  6888. }
  6889. void
  6890. LowererMD::EmitInt4Instr(IR::Instr *instr)
  6891. {
  6892. LowererMDArch::EmitInt4Instr(instr);
  6893. }
  6894. void
  6895. LowererMD::EmitLoadVar(IR::Instr *instrLoad, bool isFromUint32, bool isHelper)
  6896. {
  6897. lowererMDArch.EmitLoadVar(instrLoad, isFromUint32, isHelper);
  6898. }
  6899. bool
  6900. LowererMD::EmitLoadInt32(IR::Instr *instrLoad, bool conversionFromObjectAllowed, bool bailOutOnHelper, IR::LabelInstr * labelBailOut)
  6901. {
  6902. return lowererMDArch.EmitLoadInt32(instrLoad, conversionFromObjectAllowed, bailOutOnHelper, labelBailOut);
  6903. }
  6904. void
  6905. LowererMD::EmitIntToFloat(IR::Opnd *dst, IR::Opnd *src, IR::Instr *instrInsert)
  6906. {
  6907. this->lowererMDArch.EmitIntToFloat(dst, src, instrInsert);
  6908. }
  6909. void
  6910. LowererMD::EmitUIntToFloat(IR::Opnd *dst, IR::Opnd *src, IR::Instr *instrInsert)
  6911. {
  6912. this->lowererMDArch.EmitUIntToFloat(dst, src, instrInsert);
  6913. }
  6914. void
  6915. LowererMD::EmitIntToLong(IR::Opnd *dst, IR::Opnd *src, IR::Instr *instrInsert)
  6916. {
  6917. this->lowererMDArch.EmitIntToLong(dst, src, instrInsert);
  6918. }
  6919. void
  6920. LowererMD::EmitUIntToLong(IR::Opnd *dst, IR::Opnd *src, IR::Instr *instrInsert)
  6921. {
  6922. this->lowererMDArch.EmitUIntToLong(dst, src, instrInsert);
  6923. }
  6924. void
  6925. LowererMD::EmitLongToInt(IR::Opnd *dst, IR::Opnd *src, IR::Instr *instrInsert)
  6926. {
  6927. this->lowererMDArch.EmitLongToInt(dst, src, instrInsert);
  6928. }
  6929. void
  6930. LowererMD::EmitFloat32ToFloat64(IR::Opnd *dst, IR::Opnd *src, IR::Instr *instrInsert)
  6931. {
  6932. // We should only generate this if sse2 is available
  6933. Assert(AutoSystemInfo::Data.SSE2Available());
  6934. Assert(dst->IsRegOpnd() && dst->IsFloat64());
  6935. Assert(src->IsRegOpnd() && src->GetType() == TyFloat32);
  6936. instrInsert->InsertBefore(IR::Instr::New(Js::OpCode::CVTSS2SD, dst, src, this->m_func));
  6937. }
  6938. void
  6939. LowererMD::EmitInt64toFloat(IR::Opnd *dst, IR::Opnd *src, IR::Instr *instr)
  6940. {
  6941. #ifdef _M_IX86
  6942. IR::Opnd *srcOpnd = instr->UnlinkSrc1();
  6943. LoadInt64HelperArgument(instr, srcOpnd);
  6944. IR::Instr* callinstr = IR::Instr::New(Js::OpCode::CALL, dst, this->m_func);
  6945. instr->InsertBefore(callinstr);
  6946. CompileAssert(sizeof(IRType) == 1);
  6947. const uint16 fromToType = dst->GetType() | (srcOpnd->GetType() << 8);
  6948. IR::JnHelperMethod method = IR::HelperOp_Throw;
  6949. switch (fromToType)
  6950. {
  6951. case TyFloat32 | (TyInt64 << 8) : method = IR::HelperI64TOF32; break;
  6952. case TyFloat32 | (TyUint64 << 8) : method = IR::HelperUI64TOF32; break;
  6953. case TyFloat64 | (TyInt64 << 8) : method = IR::HelperI64TOF64; break;
  6954. case TyFloat64 | (TyUint64 << 8) : method = IR::HelperUI64TOF64; break;
  6955. default:
  6956. Assert(UNREACHED);
  6957. }
  6958. this->ChangeToHelperCall(callinstr, method);
  6959. #else
  6960. IR::Opnd* origDst = nullptr;
  6961. if (dst->IsFloat32())
  6962. {
  6963. origDst = dst;
  6964. dst = IR::RegOpnd::New(TyFloat64, this->m_func);
  6965. }
  6966. instr->InsertBefore(IR::Instr::New(Js::OpCode::CVTSI2SD, dst, src, this->m_func));
  6967. if (src->IsUnsigned())
  6968. {
  6969. IR::RegOpnd * highestBitOpnd = IR::RegOpnd::New(TyInt64, this->m_func);
  6970. IR::Instr* instrNew = IR::Instr::New(Js::OpCode::SHR, highestBitOpnd, src,
  6971. IR::IntConstOpnd::New(63, TyInt8, this->m_func, true), this->m_func);
  6972. instr->InsertBefore(instrNew);
  6973. Legalize(instrNew);
  6974. IR::RegOpnd * baseOpnd = IR::RegOpnd::New(TyMachPtr, this->m_func);
  6975. instrNew = IR::Instr::New(Js::OpCode::MOV, baseOpnd, IR::AddrOpnd::New(m_func->GetThreadContextInfo()->GetUInt64ConvertConstAddr(),
  6976. IR::AddrOpndKindDynamicMisc, this->m_func), this->m_func);
  6977. instr->InsertBefore(instrNew);
  6978. instrNew = IR::Instr::New(Js::OpCode::ADDSD, dst, dst, IR::IndirOpnd::New(baseOpnd,
  6979. highestBitOpnd, IndirScale8, TyFloat64, this->m_func), this->m_func);
  6980. instr->InsertBefore(instrNew);
  6981. }
  6982. if (origDst)
  6983. {
  6984. instr->InsertBefore(IR::Instr::New(Js::OpCode::CVTSD2SS, origDst, dst, this->m_func));
  6985. }
  6986. #endif
  6987. }
  6988. void
  6989. LowererMD::EmitNon32BitOvfCheck(IR::Instr *instr, IR::Instr *insertInstr, IR::LabelInstr* bailOutLabel)
  6990. {
  6991. AssertMsg(instr->m_opcode == Js::OpCode::IMUL, "IMUL should be used to check for non-32 bit overflow check on x86.");
  6992. IR::RegOpnd *edxSym = IR::RegOpnd::New(TyInt32, instr->m_func);
  6993. #ifdef _M_IX86
  6994. edxSym->SetReg(RegEDX);
  6995. #else
  6996. edxSym->SetReg(RegRDX);
  6997. #endif
  6998. // dummy def for edx to force RegAlloc to generate a lifetime. This is removed later by the Peeps phase.
  6999. IR::Instr *newInstr = IR::Instr::New(Js::OpCode::NOP, edxSym, instr->m_func);
  7000. insertInstr->InsertBefore(newInstr);
  7001. IR::RegOpnd *temp = IR::RegOpnd::New(TyInt32, instr->m_func);
  7002. Assert(instr->ignoreOverflowBitCount > 32);
  7003. uint8 shamt = 64 - instr->ignoreOverflowBitCount;
  7004. // MOV temp, edx
  7005. newInstr = IR::Instr::New(Js::OpCode::MOV, temp, edxSym, instr->m_func);
  7006. insertInstr->InsertBefore(newInstr);
  7007. // SHL temp, shamt
  7008. newInstr = IR::Instr::New(Js::OpCode::SHL, temp, temp, IR::IntConstOpnd::New(shamt, TyInt8, instr->m_func, true), instr->m_func);
  7009. insertInstr->InsertBefore(newInstr);
  7010. // SAR temp, shamt
  7011. newInstr = IR::Instr::New(Js::OpCode::SAR, temp, temp, IR::IntConstOpnd::New(shamt, TyInt8, instr->m_func, true), instr->m_func);
  7012. insertInstr->InsertBefore(newInstr);
  7013. // CMP temp, edx
  7014. newInstr = IR::Instr::New(Js::OpCode::CMP, instr->m_func);
  7015. newInstr->SetSrc1(temp);
  7016. newInstr->SetSrc2(edxSym);
  7017. insertInstr->InsertBefore(newInstr);
  7018. // JNE
  7019. Lowerer::InsertBranch(Js::OpCode::JNE, false, bailOutLabel, insertInstr);
  7020. }
  7021. void LowererMD::ConvertFloatToInt32(IR::Opnd* intOpnd, IR::Opnd* floatOpnd, IR::LabelInstr * labelHelper, IR::LabelInstr * labelDone, IR::Instr * instInsert)
  7022. {
  7023. UNREFERENCED_PARAMETER(labelHelper); // used on ARM
  7024. #if defined(_M_IX86)
  7025. // We should only generate this if sse2 is available
  7026. Assert(AutoSystemInfo::Data.SSE2Available());
  7027. #endif
  7028. Assert((floatOpnd->IsRegOpnd() && floatOpnd->IsFloat()) || (floatOpnd->IsIndirOpnd() && floatOpnd->GetType() == TyMachDouble));
  7029. Assert(intOpnd->GetType() == TyInt32);
  7030. IR::Instr* instr;
  7031. {
  7032. #ifdef _M_X64
  7033. IR::Opnd* dstOpnd = IR::RegOpnd::New(TyInt64, m_func);
  7034. #else
  7035. IR::Opnd* dstOpnd = intOpnd;
  7036. #endif
  7037. // CVTTSD2SI dst, floatOpnd
  7038. instr = IR::Instr::New(floatOpnd->IsFloat64() ? Js::OpCode::CVTTSD2SI : Js::OpCode::CVTTSS2SI, dstOpnd, floatOpnd, this->m_func);
  7039. instInsert->InsertBefore(instr);
  7040. // CMP dst, 0x80000000 {0x8000000000000000 on x64} -- Check for overflow
  7041. instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  7042. instr->SetSrc1(dstOpnd);
  7043. instr->SetSrc2(IR::IntConstOpnd::New(MachSignBit, TyMachReg, this->m_func, true));
  7044. instInsert->InsertBefore(instr);
  7045. Legalize(instr);
  7046. #ifdef _M_X64
  7047. // Truncate to int32 for x64. We still need to go to helper though if we have int64 overflow.
  7048. // MOV_TRUNC intOpnd, tmpOpnd
  7049. instr = IR::Instr::New(Js::OpCode::MOV_TRUNC, intOpnd, dstOpnd, this->m_func);
  7050. instInsert->InsertBefore(instr);
  7051. #endif
  7052. }
  7053. // JNE $done
  7054. instr = IR::BranchInstr::New(Js::OpCode::JNE, labelDone, this->m_func);
  7055. instInsert->InsertBefore(instr);
  7056. // It does overflow - Let's try using FISTTP which uses 64 bits and is relevant only for x86
  7057. // but requires going to memory and should only be used in overflow scenarios
  7058. #ifdef _M_IX86
  7059. if (AutoSystemInfo::Data.SSE3Available())
  7060. {
  7061. IR::Opnd* floatStackOpnd;
  7062. StackSym* tempSymDouble = this->m_func->tempSymDouble;
  7063. if (!tempSymDouble)
  7064. {
  7065. this->m_func->tempSymDouble = StackSym::New(TyFloat64, this->m_func);
  7066. this->m_func->StackAllocate(this->m_func->tempSymDouble, MachDouble);
  7067. tempSymDouble = this->m_func->tempSymDouble;
  7068. }
  7069. IR::Opnd * float64Opnd;
  7070. if (floatOpnd->IsFloat32())
  7071. {
  7072. float64Opnd = IR::RegOpnd::New(TyFloat64, m_func);
  7073. instr = IR::Instr::New(Js::OpCode::CVTSS2SD, float64Opnd, floatOpnd, m_func);
  7074. instInsert->InsertBefore(instr);
  7075. }
  7076. else
  7077. {
  7078. float64Opnd = floatOpnd;
  7079. }
  7080. if (float64Opnd->IsRegOpnd())
  7081. {
  7082. floatStackOpnd = IR::SymOpnd::New(tempSymDouble, TyMachDouble, m_func);
  7083. instr = IR::Instr::New(Js::OpCode::MOVSD, floatStackOpnd, float64Opnd, m_func);
  7084. instInsert->InsertBefore(instr);
  7085. }
  7086. else
  7087. {
  7088. floatStackOpnd = float64Opnd;
  7089. }
  7090. // FLD [tmpDouble]
  7091. instr = IR::Instr::New(Js::OpCode::FLD, floatStackOpnd, floatStackOpnd, m_func);
  7092. instInsert->InsertBefore(instr);
  7093. if (!float64Opnd->IsRegOpnd())
  7094. {
  7095. floatStackOpnd = IR::SymOpnd::New(tempSymDouble, TyMachDouble, m_func);
  7096. }
  7097. // FISTTP qword ptr [tmpDouble]
  7098. instr = IR::Instr::New(Js::OpCode::FISTTP, floatStackOpnd, m_func);
  7099. instInsert->InsertBefore(instr);
  7100. StackSym *intSym = StackSym::New(TyInt32, m_func);
  7101. intSym->m_offset = tempSymDouble->m_offset;
  7102. intSym->m_allocated = true;
  7103. IR::Opnd* lowerBitsOpnd = IR::SymOpnd::New(intSym, TyInt32, m_func);
  7104. // MOV dst, dword ptr [tmpDouble]
  7105. instr = IR::Instr::New(Js::OpCode::MOV, intOpnd, lowerBitsOpnd, m_func);
  7106. instInsert->InsertBefore(instr);
  7107. // TEST dst, dst -- Check for overflow
  7108. instr = IR::Instr::New(Js::OpCode::TEST, this->m_func);
  7109. instr->SetSrc1(intOpnd);
  7110. instr->SetSrc2(intOpnd);
  7111. instInsert->InsertBefore(instr);
  7112. instr = IR::BranchInstr::New(Js::OpCode::JNE, labelDone, this->m_func);
  7113. instInsert->InsertBefore(instr);
  7114. // CMP [tmpDouble - 4], 0x80000000
  7115. StackSym* higherBitsSym = StackSym::New(TyInt32, m_func);
  7116. higherBitsSym->m_offset = tempSymDouble->m_offset + 4;
  7117. higherBitsSym->m_allocated = true;
  7118. instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  7119. instr->SetSrc1(IR::SymOpnd::New(higherBitsSym, TyInt32, m_func));
  7120. instr->SetSrc2(IR::IntConstOpnd::New(0x80000000, TyInt32, this->m_func, true));
  7121. instInsert->InsertBefore(instr);
  7122. instr = IR::BranchInstr::New(Js::OpCode::JNE, labelDone, this->m_func);
  7123. instInsert->InsertBefore(instr);
  7124. }
  7125. #endif
  7126. }
  7127. IR::Instr *
  7128. LowererMD::InsertConvertFloat64ToInt32(const RoundMode roundMode, IR::Opnd *const dst, IR::Opnd *const src, IR::Instr *const insertBeforeInstr)
  7129. {
  7130. Assert(dst);
  7131. Assert(dst->IsInt32());
  7132. Assert(src);
  7133. Assert(src->IsFloat64());
  7134. Assert(insertBeforeInstr);
  7135. // The caller is expected to check for overflow. To have that work be done automatically, use LowererMD::EmitFloatToInt.
  7136. Func *const func = insertBeforeInstr->m_func;
  7137. IR::AutoReuseOpnd autoReuseSrcPlusHalf;
  7138. IR::Instr *instr = nullptr;
  7139. switch (roundMode)
  7140. {
  7141. case RoundModeTowardInteger:
  7142. {
  7143. // Conversion with rounding towards nearest integer is not supported by the architecture. Add 0.5 and do a
  7144. // round-toward-zero conversion instead.
  7145. IR::RegOpnd *const srcPlusHalf = IR::RegOpnd::New(TyFloat64, func);
  7146. autoReuseSrcPlusHalf.Initialize(srcPlusHalf, func);
  7147. Lowerer::InsertAdd(
  7148. false /* needFlags */,
  7149. srcPlusHalf,
  7150. src,
  7151. IR::MemRefOpnd::New(func->GetThreadContextInfo()->GetDoublePointFiveAddr(), TyFloat64, func,
  7152. IR::AddrOpndKindDynamicDoubleRef),
  7153. insertBeforeInstr);
  7154. instr = IR::Instr::New(LowererMD::MDConvertFloat64ToInt32Opcode(RoundModeTowardZero), dst, srcPlusHalf, func);
  7155. insertBeforeInstr->InsertBefore(instr);
  7156. LowererMD::Legalize(instr);
  7157. return instr;
  7158. }
  7159. case RoundModeHalfToEven:
  7160. {
  7161. instr = IR::Instr::New(LowererMD::MDConvertFloat64ToInt32Opcode(RoundModeHalfToEven), dst, src, func);
  7162. insertBeforeInstr->InsertBefore(instr);
  7163. LowererMD::Legalize(instr);
  7164. return instr;
  7165. }
  7166. default:
  7167. AssertMsg(0, "RoundMode not supported.");
  7168. return nullptr;
  7169. }
  7170. }
  7171. void
  7172. LowererMD::EmitFloatToInt(IR::Opnd *dst, IR::Opnd *src, IR::Instr *instrInsert, IR::Instr *instrBailOut, IR::LabelInstr * labelBailOut)
  7173. {
  7174. #ifdef _M_IX86
  7175. // We should only generate this if sse2 is available
  7176. Assert(AutoSystemInfo::Data.SSE2Available());
  7177. #endif
  7178. IR::BailOutKind bailOutKind = IR::BailOutInvalid;
  7179. if (instrBailOut && instrBailOut->HasBailOutInfo())
  7180. {
  7181. bailOutKind = instrBailOut->GetBailOutKind();
  7182. if (bailOutKind & IR::BailOutOnArrayAccessHelperCall)
  7183. {
  7184. // Bail out instead of calling helper. If this is happening unconditionally, the caller should instead throw a rejit exception.
  7185. Assert(labelBailOut);
  7186. m_lowerer->InsertBranch(Js::OpCode::Br, labelBailOut, instrInsert);
  7187. return;
  7188. }
  7189. }
  7190. IR::LabelInstr *labelDone = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  7191. IR::LabelInstr *labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  7192. IR::Instr *instr;
  7193. ConvertFloatToInt32(dst, src, labelHelper, labelDone, instrInsert);
  7194. // $Helper
  7195. instrInsert->InsertBefore(labelHelper);
  7196. IR::Opnd * arg = src;
  7197. if (src->IsFloat32())
  7198. {
  7199. arg = IR::RegOpnd::New(TyFloat64, m_func);
  7200. EmitFloat32ToFloat64(arg, src, instrInsert);
  7201. }
  7202. instr = IR::Instr::New(Js::OpCode::CALL, dst, this->m_func);
  7203. instrInsert->InsertBefore(instr);
  7204. if (BailOutInfo::IsBailOutOnImplicitCalls(bailOutKind))
  7205. {
  7206. _Analysis_assume_(instrBailOut != nullptr);
  7207. instr = instr->ConvertToBailOutInstr(instrBailOut->GetBailOutInfo(), bailOutKind);
  7208. if (instrBailOut->GetBailOutInfo()->bailOutInstr == instrBailOut)
  7209. {
  7210. IR::Instr * instrShare = instrBailOut->ShareBailOut();
  7211. m_lowerer->LowerBailTarget(instrShare);
  7212. }
  7213. }
  7214. // dst = ToInt32Core(src);
  7215. LoadDoubleHelperArgument(instr, arg);
  7216. this->ChangeToHelperCall(instr, IR::HelperConv_ToInt32Core);
  7217. // $Done
  7218. instrInsert->InsertBefore(labelDone);
  7219. }
  7220. void
  7221. LowererMD::EmitLoadVarNoCheck(IR::RegOpnd * dst, IR::RegOpnd * src, IR::Instr *instrLoad, bool isFromUint32, bool isHelper)
  7222. {
  7223. #ifdef _M_IX86
  7224. if (!AutoSystemInfo::Data.SSE2Available())
  7225. {
  7226. IR::JnHelperMethod helperMethod;
  7227. // PUSH &floatTemp
  7228. IR::Opnd *tempOpnd;
  7229. if (instrLoad->dstIsTempNumber)
  7230. {
  7231. helperMethod = isFromUint32 ? IR::HelperOp_UInt32ToAtomInPlace : IR::HelperOp_Int32ToAtomInPlace;
  7232. // Use the original dst to get the temp number sym
  7233. StackSym * tempNumberSym = this->m_lowerer->GetTempNumberSym(instrLoad->GetDst(), instrLoad->dstIsTempNumberTransferred);
  7234. IR::Instr *load = this->LoadStackAddress(tempNumberSym);
  7235. instrLoad->InsertBefore(load);
  7236. tempOpnd = load->GetDst();
  7237. this->LoadHelperArgument(instrLoad, tempOpnd);
  7238. }
  7239. else
  7240. {
  7241. helperMethod = isFromUint32 ? IR::HelperOp_UInt32ToAtom : IR::HelperOp_Int32ToAtom;
  7242. }
  7243. // PUSH memContext
  7244. this->m_lowerer->LoadScriptContext(instrLoad);
  7245. // PUSH s1
  7246. this->LoadHelperArgument(instrLoad, src);
  7247. // dst = ToVar()
  7248. IR::Instr * instr = IR::Instr::New(Js::OpCode::Call, dst,
  7249. IR::HelperCallOpnd::New(helperMethod, this->m_func), this->m_func);
  7250. instrLoad->InsertBefore(instr);
  7251. this->LowerCall(instr, 0);
  7252. return;
  7253. }
  7254. #endif
  7255. IR::RegOpnd * floatReg = IR::RegOpnd::New(TyFloat64, this->m_func);
  7256. if (isFromUint32)
  7257. {
  7258. this->EmitUIntToFloat(floatReg, src, instrLoad);
  7259. }
  7260. else
  7261. {
  7262. this->EmitIntToFloat(floatReg, src, instrLoad);
  7263. }
  7264. this->SaveDoubleToVar(dst, floatReg, instrLoad, instrLoad, isHelper);
  7265. }
  7266. IR::Instr *
  7267. LowererMD::LowerGetCachedFunc(IR::Instr *instr)
  7268. {
  7269. // src1 is an ActivationObjectEx, and we want to get the function object identified by the index (src2)
  7270. // dst = MOV (src1)->GetFuncCacheEntry(src2)->func
  7271. //
  7272. // => [src1 + (offsetof(src1, cache) + (src2 * sizeof(FuncCacheEntry)) + offsetof(FuncCacheEntry, func))]
  7273. IR::IntConstOpnd *src2Opnd = instr->UnlinkSrc2()->AsIntConstOpnd();
  7274. IR::RegOpnd *src1Opnd = instr->UnlinkSrc1()->AsRegOpnd();
  7275. instr->m_opcode = Js::OpCode::MOV;
  7276. IntConstType offset = (src2Opnd->GetValue() * sizeof(Js::FuncCacheEntry)) + Js::ActivationObjectEx::GetOffsetOfCache() + offsetof(Js::FuncCacheEntry, func);
  7277. Assert(Math::FitsInDWord(offset));
  7278. instr->SetSrc1(IR::IndirOpnd::New(src1Opnd, (int32)offset, TyVar, this->m_func));
  7279. src2Opnd->Free(this->m_func);
  7280. return instr->m_prev;
  7281. }
  7282. IR::Instr *
  7283. LowererMD::LowerCommitScope(IR::Instr *instrCommit)
  7284. {
  7285. IR::Instr *instrPrev = instrCommit->m_prev;
  7286. IR::RegOpnd *baseOpnd = instrCommit->UnlinkSrc1()->AsRegOpnd();
  7287. IR::Opnd *opnd;
  7288. IR::Instr * insertInstr = instrCommit->m_next;
  7289. // Write undef to all the local var slots.
  7290. opnd = IR::IndirOpnd::New(baseOpnd, Js::ActivationObjectEx::GetOffsetOfCommitFlag(), TyInt8, this->m_func);
  7291. instrCommit->SetDst(opnd);
  7292. instrCommit->SetSrc1(IR::IntConstOpnd::New(1, TyInt8, this->m_func));
  7293. LowererMD::ChangeToAssign(instrCommit);
  7294. const Js::PropertyIdArray *propIds = instrCommit->m_func->GetJITFunctionBody()->GetFormalsPropIdArray();
  7295. uint firstVarSlot = (uint)Js::ActivationObjectEx::GetFirstVarSlot(propIds);
  7296. if (firstVarSlot < propIds->count)
  7297. {
  7298. IR::RegOpnd *undefOpnd = IR::RegOpnd::New(TyMachReg, this->m_func);
  7299. LowererMD::CreateAssign(undefOpnd, m_lowerer->LoadLibraryValueOpnd(insertInstr, LibraryValue::ValueUndefined), insertInstr);
  7300. IR::RegOpnd *slotBaseOpnd = IR::RegOpnd::New(TyMachReg, this->m_func);
  7301. // Load a pointer to the aux slots. We assume that all ActivationObject's have only aux slots.
  7302. opnd = IR::IndirOpnd::New(baseOpnd, Js::DynamicObject::GetOffsetOfAuxSlots(), TyMachReg, this->m_func);
  7303. this->CreateAssign(slotBaseOpnd, opnd, insertInstr);
  7304. for (uint i = firstVarSlot; i < propIds->count; i++)
  7305. {
  7306. opnd = IR::IndirOpnd::New(slotBaseOpnd, i << this->GetDefaultIndirScale(), TyMachReg, this->m_func);
  7307. this->CreateAssign(opnd, undefOpnd, insertInstr);
  7308. }
  7309. }
  7310. return instrPrev;
  7311. }
  7312. void
  7313. LowererMD::ImmedSrcToReg(IR::Instr * instr, IR::Opnd * newOpnd, int srcNum)
  7314. {
  7315. if (srcNum == 2)
  7316. {
  7317. instr->SetSrc2(newOpnd);
  7318. }
  7319. else
  7320. {
  7321. Assert(srcNum == 1);
  7322. instr->SetSrc1(newOpnd);
  7323. }
  7324. }
  7325. IR::LabelInstr *
  7326. LowererMD::GetBailOutStackRestoreLabel(BailOutInfo * bailOutInfo, IR::LabelInstr * exitTargetInstr)
  7327. {
  7328. return lowererMDArch.GetBailOutStackRestoreLabel(bailOutInfo, exitTargetInstr);
  7329. }
  7330. StackSym *
  7331. LowererMD::GetImplicitParamSlotSym(Js::ArgSlot argSlot)
  7332. {
  7333. return GetImplicitParamSlotSym(argSlot, this->m_func);
  7334. }
  7335. StackSym *
  7336. LowererMD::GetImplicitParamSlotSym(Js::ArgSlot argSlot, Func * func)
  7337. {
  7338. // Stack looks like (EBP chain)+0, (return addr)+4, (function object)+8, (arg count)+12, (this)+16, actual args
  7339. // Pass in the EBP+8 to start at the function object, the start of the implicit param slots
  7340. StackSym * stackSym = StackSym::NewImplicitParamSym(argSlot, func);
  7341. func->SetArgOffset(stackSym, (2 + argSlot) * MachPtr);
  7342. func->SetHasImplicitParamLoad();
  7343. return stackSym;
  7344. }
  7345. bool LowererMD::GenerateFastAnd(IR::Instr * instrAnd)
  7346. {
  7347. return this->lowererMDArch.GenerateFastAnd(instrAnd);
  7348. }
  7349. bool LowererMD::GenerateFastXor(IR::Instr * instrXor)
  7350. {
  7351. return this->lowererMDArch.GenerateFastXor(instrXor);
  7352. }
  7353. bool LowererMD::GenerateFastOr(IR::Instr * instrOr)
  7354. {
  7355. return this->lowererMDArch.GenerateFastOr(instrOr);
  7356. }
  7357. bool LowererMD::GenerateFastNot(IR::Instr * instrNot)
  7358. {
  7359. return this->lowererMDArch.GenerateFastNot(instrNot);
  7360. }
  7361. bool LowererMD::GenerateFastShiftLeft(IR::Instr * instrShift)
  7362. {
  7363. return this->lowererMDArch.GenerateFastShiftLeft(instrShift);
  7364. }
  7365. bool LowererMD::GenerateFastShiftRight(IR::Instr * instrShift)
  7366. {
  7367. return this->lowererMDArch.GenerateFastShiftRight(instrShift);
  7368. }
  7369. void LowererMD::GenerateIsDynamicObject(IR::RegOpnd *regOpnd, IR::Instr *insertInstr, IR::LabelInstr *labelHelper, bool fContinueLabel)
  7370. {
  7371. // CMP [srcReg], Js::DynamicObject::`vtable'
  7372. {
  7373. IR::Instr *cmp = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  7374. cmp->SetSrc1(IR::IndirOpnd::New(regOpnd, 0, TyMachPtr, m_func));
  7375. cmp->SetSrc2(m_lowerer->LoadVTableValueOpnd(insertInstr, VTableValue::VtableDynamicObject));
  7376. insertInstr->InsertBefore(cmp);
  7377. Legalize(cmp);
  7378. }
  7379. if (fContinueLabel)
  7380. {
  7381. // JEQ $fallThough
  7382. IR::Instr * jne = IR::BranchInstr::New(Js::OpCode::JEQ, labelHelper, this->m_func);
  7383. insertInstr->InsertBefore(jne);
  7384. }
  7385. else
  7386. {
  7387. // JNE $helper
  7388. IR::Instr * jne = IR::BranchInstr::New(Js::OpCode::JNE, labelHelper, this->m_func);
  7389. insertInstr->InsertBefore(jne);
  7390. }
  7391. }
  7392. void LowererMD::GenerateIsRecyclableObject(IR::RegOpnd *regOpnd, IR::Instr *insertInstr, IR::LabelInstr *labelHelper, bool checkObjectAndDynamicObject)
  7393. {
  7394. // CMP [srcReg], Js::DynamicObject::`vtable'
  7395. // JEQ $fallThough
  7396. // MOV r1, [src1 + offset(type)] -- get the type id
  7397. // MOV r1, [r1 + offset(typeId)]
  7398. // ADD r1, ~TypeIds_LastJavascriptPrimitiveType -- if (typeId > TypeIds_LastJavascriptPrimitiveType && typeId <= TypeIds_LastTrueJavascriptObjectType)
  7399. // CMP r1, (TypeIds_LastTrueJavascriptObjectType - TypeIds_LastJavascriptPrimitiveType - 1)
  7400. // JA $helper
  7401. //fallThrough:
  7402. IR::LabelInstr *labelFallthrough = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  7403. if (checkObjectAndDynamicObject)
  7404. {
  7405. if (!regOpnd->IsNotTaggedValue())
  7406. {
  7407. GenerateObjectTest(regOpnd, insertInstr, labelHelper);
  7408. }
  7409. this->GenerateIsDynamicObject(regOpnd, insertInstr, labelFallthrough, true);
  7410. }
  7411. IR::RegOpnd * typeRegOpnd = IR::RegOpnd::New(TyMachReg, this->m_func);
  7412. IR::RegOpnd * typeIdRegOpnd = IR::RegOpnd::New(TyInt32, this->m_func);
  7413. // MOV r1, [src1 + offset(type)]
  7414. {
  7415. IR::IndirOpnd * indirOpnd = IR::IndirOpnd::New(regOpnd, Js::RecyclableObject::GetOffsetOfType(), TyMachReg, this->m_func);
  7416. IR::Instr * mov = IR::Instr::New(Js::OpCode::MOV, typeRegOpnd, indirOpnd, this->m_func);
  7417. insertInstr->InsertBefore(mov);
  7418. }
  7419. // MOV r1, [r1 + offset(typeId)]
  7420. {
  7421. IR::IndirOpnd * indirOpnd = IR::IndirOpnd::New(typeRegOpnd, Js::Type::GetOffsetOfTypeId(), TyInt32, this->m_func);
  7422. IR::Instr * mov = IR::Instr::New(Js::OpCode::MOV, typeIdRegOpnd, indirOpnd, this->m_func);
  7423. insertInstr->InsertBefore(mov);
  7424. }
  7425. // ADD r1, ~TypeIds_LastJavascriptPrimitiveType
  7426. {
  7427. IR::Instr * add = IR::Instr::New(Js::OpCode::ADD, typeIdRegOpnd, typeIdRegOpnd, IR::IntConstOpnd::New(~Js::TypeIds_LastJavascriptPrimitiveType, TyInt32, this->m_func, true), this->m_func);
  7428. insertInstr->InsertBefore(add);
  7429. }
  7430. // CMP r1, (TypeIds_LastTrueJavascriptObjectType - TypeIds_LastJavascriptPrimitiveType - 1)
  7431. {
  7432. IR::Instr * cmp = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  7433. cmp->SetSrc1(typeIdRegOpnd);
  7434. cmp->SetSrc2(IR::IntConstOpnd::New(Js::TypeIds_LastTrueJavascriptObjectType - Js::TypeIds_LastJavascriptPrimitiveType - 1, TyInt32, this->m_func));
  7435. insertInstr->InsertBefore(cmp);
  7436. }
  7437. // JA $helper
  7438. {
  7439. IR::Instr * jbe = IR::BranchInstr::New(Js::OpCode::JA, labelHelper, this->m_func);
  7440. insertInstr->InsertBefore(jbe);
  7441. }
  7442. // $fallThrough
  7443. insertInstr->InsertBefore(labelFallthrough);
  7444. }
  7445. bool
  7446. LowererMD::GenerateLdThisCheck(IR::Instr * instr)
  7447. {
  7448. //
  7449. // If not a recyclable object, jump to $helper
  7450. // MOV dst, src1 -- return the object itself
  7451. // JMP $fallthrough
  7452. // $helper:
  7453. // (caller generates helper call)
  7454. // $fallthrough:
  7455. //
  7456. IR::RegOpnd * src1 = instr->GetSrc1()->AsRegOpnd();
  7457. IR::LabelInstr * helper = IR::LabelInstr::New(Js::OpCode::Label, m_func, true);
  7458. IR::LabelInstr * fallthrough = IR::LabelInstr::New(Js::OpCode::Label, m_func);
  7459. this->GenerateIsRecyclableObject(src1, instr, helper);
  7460. // MOV dst, src1
  7461. if (instr->GetDst() && !instr->GetDst()->IsEqual(src1))
  7462. {
  7463. IR::Instr * mov = IR::Instr::New(Js::OpCode::MOV, instr->GetDst(), src1, this->m_func);
  7464. instr->InsertBefore(mov);
  7465. }
  7466. // JMP $fallthrough
  7467. {
  7468. IR::Instr * jmp = IR::BranchInstr::New(Js::OpCode::JMP, fallthrough, this->m_func);
  7469. instr->InsertBefore(jmp);
  7470. }
  7471. // $helper:
  7472. // (caller generates helper call)
  7473. // $fallthrough:
  7474. instr->InsertBefore(helper);
  7475. instr->InsertAfter(fallthrough);
  7476. return true;
  7477. }
  7478. //
  7479. // TEST src, Js::AtomTag
  7480. // JNE $done
  7481. // MOV typeReg, objectSrc + offsetof(RecyclableObject::type)
  7482. // CMP [typeReg + offsetof(Type::typeid)], TypeIds_ActivationObject
  7483. // JEQ $helper
  7484. // $done:
  7485. // MOV dst, src
  7486. // JMP $fallthru
  7487. // helper:
  7488. // MOV dst, undefined
  7489. // $fallthru:
  7490. bool
  7491. LowererMD::GenerateLdThisStrict(IR::Instr* instr)
  7492. {
  7493. IR::RegOpnd * src1 = instr->GetSrc1()->AsRegOpnd();
  7494. IR::RegOpnd * typeReg = IR::RegOpnd::New(TyMachReg, this->m_func);
  7495. IR::LabelInstr * done = IR::LabelInstr::New(Js::OpCode::Label, m_func);
  7496. IR::LabelInstr * fallthru = IR::LabelInstr::New(Js::OpCode::Label, m_func);
  7497. IR::LabelInstr * helper = IR::LabelInstr::New(Js::OpCode::Label, m_func, /*helper*/true);
  7498. bool assign = instr->GetDst() && !instr->GetDst()->IsEqual(src1);
  7499. // TEST src1, Js::AtomTag
  7500. // JNE $done
  7501. if(!src1->IsNotTaggedValue())
  7502. {
  7503. GenerateObjectTest(src1, instr, assign ? done : fallthru);
  7504. }
  7505. // MOV typeReg, objectSrc + offsetof(RecyclableObject::type)
  7506. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, typeReg,
  7507. IR::IndirOpnd::New(src1, Js::RecyclableObject::GetOffsetOfType(), TyMachReg, m_func),
  7508. m_func));
  7509. // CMP [typeReg + offsetof(Type::typeid)], TypeIds_ActivationObject
  7510. {
  7511. IR::Instr * cmp = IR::Instr::New(Js::OpCode::CMP, m_func);
  7512. cmp->SetSrc1(IR::IndirOpnd::New(typeReg, Js::Type::GetOffsetOfTypeId(), TyInt32, m_func));
  7513. cmp->SetSrc2(IR::IntConstOpnd::New(Js::TypeId::TypeIds_ActivationObject, TyInt32, m_func));
  7514. instr->InsertBefore(cmp);
  7515. }
  7516. // JEQ $helper
  7517. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JEQ, helper, m_func));
  7518. if (assign)
  7519. {
  7520. // $done:
  7521. // MOV dst, src
  7522. instr->InsertBefore(done);
  7523. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, instr->GetDst(), src1, m_func));
  7524. }
  7525. // JMP $fallthru
  7526. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JMP, fallthru, m_func));
  7527. instr->InsertBefore(helper);
  7528. if (instr->GetDst())
  7529. {
  7530. // MOV dst, undefined
  7531. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, instr->GetDst(),
  7532. m_lowerer->LoadLibraryValueOpnd(instr, LibraryValue::ValueUndefined), m_func));
  7533. }
  7534. // $fallthru:
  7535. instr->InsertAfter(fallthru);
  7536. return true;
  7537. }
  7538. // given object instanceof function, functionReg is a register with function,
  7539. // objectReg is a register with instance and inlineCache is an InstIsInlineCache.
  7540. // We want to generate:
  7541. //
  7542. // fallback on helper (will patch the inline cache) if function does not match the cache
  7543. // MOV dst, Js::false
  7544. // CMP functionReg, [&(inlineCache->function)]
  7545. // JNE helper
  7546. //
  7547. // fallback if object is a tagged int
  7548. // TEST objectReg, Js::AtomTag
  7549. // JNE done
  7550. //
  7551. // fallback if object's type is not the cached type
  7552. // MOV typeReg, objectSrc + offsetof(RecyclableObject::type)
  7553. // CMP typeReg, [&(inlineCache->type]
  7554. // JNE checkPrimType
  7555. // use the cached result and fallthrough
  7556. // MOV dst, [&(inlineCache->result)]
  7557. // JMP done
  7558. // return false if object is a primitive
  7559. // $checkPrimType
  7560. // CMP [typeReg + offsetof(Type::typeid)], TypeIds_LastJavascriptPrimitiveType
  7561. // JLE done
  7562. //
  7563. //
  7564. // $helper
  7565. // $done
  7566. bool
  7567. LowererMD::GenerateFastIsInst(IR::Instr * instr)
  7568. {
  7569. IR::LabelInstr * helper = IR::LabelInstr::New(Js::OpCode::Label, m_func, true);
  7570. IR::LabelInstr * checkPrimType = IR::LabelInstr::New(Js::OpCode::Label, m_func, true);
  7571. IR::LabelInstr * done = IR::LabelInstr::New(Js::OpCode::Label, m_func);
  7572. IR::RegOpnd * typeReg = IR::RegOpnd::New(TyMachReg, this->m_func);
  7573. IR::Opnd * objectSrc;
  7574. IR::RegOpnd * objectReg;
  7575. IR::Opnd * functionSrc;
  7576. IR::RegOpnd * functionReg;
  7577. intptr_t inlineCache;
  7578. IR::Instr * instrArg;
  7579. // We are going to use the extra ArgOut_A instructions to lower the helper call later,
  7580. // so we leave them alone here and clean them up then.
  7581. inlineCache = instr->m_func->GetJITFunctionBody()->GetIsInstInlineCache(instr->GetSrc1()->AsIntConstOpnd()->AsUint32());
  7582. Assert(instr->GetSrc2()->AsRegOpnd()->m_sym->m_isSingleDef);
  7583. instrArg = instr->GetSrc2()->AsRegOpnd()->m_sym->m_instrDef;
  7584. objectSrc = instrArg->GetSrc1();
  7585. Assert(instrArg->GetSrc2()->AsRegOpnd()->m_sym->m_isSingleDef);
  7586. instrArg = instrArg->GetSrc2()->AsRegOpnd()->m_sym->m_instrDef;
  7587. functionSrc = instrArg->GetSrc1();
  7588. Assert(instrArg->GetSrc2() == nullptr);
  7589. // MOV dst, Js::false
  7590. Lowerer::InsertMove(instr->GetDst(), m_lowerer->LoadLibraryValueOpnd(instr, LibraryValue::ValueFalse), instr);
  7591. if (functionSrc->IsRegOpnd())
  7592. {
  7593. functionReg = functionSrc->AsRegOpnd();
  7594. }
  7595. else
  7596. {
  7597. functionReg = IR::RegOpnd::New(TyMachReg, this->m_func);
  7598. // MOV functionReg, functionSrc
  7599. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, functionReg, functionSrc, m_func));
  7600. }
  7601. // CMP functionReg, [&(inlineCache->function)]
  7602. {
  7603. IR::Instr * cmp = IR::Instr::New(Js::OpCode::CMP, m_func);
  7604. cmp->SetSrc1(functionReg);
  7605. cmp->SetSrc2(IR::MemRefOpnd::New(inlineCache + Js::IsInstInlineCache::OffsetOfFunction(), TyMachReg, m_func,
  7606. IR::AddrOpndKindDynamicIsInstInlineCacheFunctionRef));
  7607. instr->InsertBefore(cmp);
  7608. Legalize(cmp);
  7609. }
  7610. // JNE helper
  7611. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JNE, helper, m_func));
  7612. if (objectSrc->IsRegOpnd())
  7613. {
  7614. objectReg = objectSrc->AsRegOpnd();
  7615. }
  7616. else
  7617. {
  7618. objectReg = IR::RegOpnd::New(TyMachReg, this->m_func);
  7619. // MOV objectReg, objectSrc
  7620. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, objectReg, objectSrc, m_func));
  7621. }
  7622. // TEST objectReg, Js::AtomTag
  7623. // JNE done
  7624. GenerateObjectTest(objectReg, instr, done);
  7625. // MOV typeReg, objectSrc + offsetof(RecyclableObject::type)
  7626. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, typeReg,
  7627. IR::IndirOpnd::New(objectReg, Js::RecyclableObject::GetOffsetOfType(), TyMachReg, m_func),
  7628. m_func));
  7629. // CMP typeReg, [&(inlineCache->type]
  7630. {
  7631. IR::Instr * cmp = IR::Instr::New(Js::OpCode::CMP, m_func);
  7632. cmp->SetSrc1(typeReg);
  7633. cmp->SetSrc2(IR::MemRefOpnd::New(inlineCache + Js::IsInstInlineCache::OffsetOfType(), TyMachReg, m_func,
  7634. IR::AddrOpndKindDynamicIsInstInlineCacheTypeRef));
  7635. instr->InsertBefore(cmp);
  7636. Legalize(cmp);
  7637. }
  7638. // JNE checkPrimType
  7639. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JNE, checkPrimType, m_func));
  7640. // MOV dst, [&(inlineCache->result)]
  7641. Lowerer::InsertMove(instr->GetDst(), IR::MemRefOpnd::New(inlineCache + Js::IsInstInlineCache::OffsetOfResult(), TyMachReg, m_func,
  7642. IR::AddrOpndKindDynamicIsInstInlineCacheResultRef), instr);
  7643. // JMP done
  7644. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JMP, done, m_func));
  7645. // LABEL checkPrimType
  7646. instr->InsertBefore(checkPrimType);
  7647. // CMP [typeReg + offsetof(Type::typeid)], TypeIds_LastJavascriptPrimitiveType
  7648. {
  7649. IR::Instr * cmp = IR::Instr::New(Js::OpCode::CMP, m_func);
  7650. cmp->SetSrc1(IR::IndirOpnd::New(typeReg, Js::Type::GetOffsetOfTypeId(), TyInt32, m_func));
  7651. cmp->SetSrc2(IR::IntConstOpnd::New(Js::TypeId::TypeIds_LastJavascriptPrimitiveType, TyInt32, m_func));
  7652. instr->InsertBefore(cmp);
  7653. }
  7654. // JLE done
  7655. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JLE, done, m_func));
  7656. // LABEL helper
  7657. instr->InsertBefore(helper);
  7658. instr->InsertAfter(done);
  7659. return true;
  7660. }
  7661. void LowererMD::GenerateIsJsObjectTest(IR::RegOpnd* instanceReg, IR::Instr* insertInstr, IR::LabelInstr* labelHelper)
  7662. {
  7663. // TEST instanceReg, (Js::AtomTag_IntPtr | Js::FloatTag_Value )
  7664. GenerateObjectTest(instanceReg, insertInstr, labelHelper);
  7665. IR::RegOpnd * typeReg = IR::RegOpnd::New(TyMachReg, this->m_func);
  7666. // MOV typeReg, instanceReg + offsetof(RecyclableObject::type)
  7667. insertInstr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, typeReg,
  7668. IR::IndirOpnd::New(instanceReg, Js::RecyclableObject::GetOffsetOfType(), TyMachReg, m_func),
  7669. m_func));
  7670. // CMP [typeReg + offsetof(Type::typeid)], TypeIds_LastJavascriptPrimitiveType
  7671. IR::Instr * cmp = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  7672. cmp->SetSrc1(IR::IndirOpnd::New(typeReg, Js::Type::GetOffsetOfTypeId(), TyInt32, this->m_func));
  7673. cmp->SetSrc2(IR::IntConstOpnd::New(Js::TypeId::TypeIds_LastJavascriptPrimitiveType, TyInt32, this->m_func));
  7674. insertInstr->InsertBefore(cmp);
  7675. // JLE labelHelper
  7676. insertInstr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JLE, labelHelper, this->m_func));
  7677. }
  7678. IR::Instr *
  7679. LowererMD::LowerReinterpretPrimitive(IR::Instr* instr)
  7680. {
  7681. IR::Opnd* dst = instr->GetDst();
  7682. IR::Opnd* src = instr->GetSrc1();
  7683. if ((dst->IsInt64() && src->IsFloat64()) ||
  7684. (dst->IsFloat64() && src->IsInt64()))
  7685. {
  7686. #if _M_AMD64
  7687. instr->m_opcode = Js::OpCode::MOVQ;
  7688. Legalize(instr);
  7689. return instr;
  7690. #elif _M_IX86
  7691. if (dst->IsInt64())
  7692. {
  7693. // movd low_bits, xmm1
  7694. // shufps xmm1, xmm1, 1
  7695. // movd high_bits, xmm1
  7696. Assert(src->IsFloat64());
  7697. Int64RegPair dstPair = m_lowerer->FindOrCreateInt64Pair(dst);
  7698. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOVD, dstPair.low, src, m_func));
  7699. instr->InsertBefore(IR::Instr::New(Js::OpCode::SHUFPS, src, src, IR::IntConstOpnd::New(1, TyInt8, m_func, true), m_func));
  7700. instr->m_opcode = Js::OpCode::MOVD;
  7701. instr->UnlinkDst();
  7702. instr->SetDst(dstPair.high);
  7703. }
  7704. else
  7705. {
  7706. // movd xmm0, lowBits;
  7707. // movd xmm1, highBits;
  7708. // shufps xmm0, xmm1, (0 | 2 << 2 | 0 << 4 | 1 << 6);
  7709. // shufps xmm0, xmm0, (0 | 2 << 2 | 3 << 4 | 3 << 6);
  7710. Assert(src->IsInt64());
  7711. Int64RegPair srcPair = m_lowerer->FindOrCreateInt64Pair(src);
  7712. IR::RegOpnd* tmpDouble = IR::RegOpnd::New(TyFloat64, m_func);
  7713. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOVD, dst, srcPair.low, m_func));
  7714. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOVD, tmpDouble, srcPair.high, m_func));
  7715. instr->InsertBefore(IR::Instr::New(Js::OpCode::SHUFPS, dst, tmpDouble, IR::IntConstOpnd::New((0 | 2 << 2 | 0 << 4 | 1 << 6), TyInt8, m_func, true), m_func));
  7716. instr->m_opcode = Js::OpCode::SHUFPS;
  7717. instr->ReplaceSrc1(dst);
  7718. instr->SetSrc2(IR::IntConstOpnd::New((0 | 2 << 2 | 3 << 4 | 3 << 6), TyInt8, m_func, true));
  7719. }
  7720. return instr;
  7721. #endif
  7722. }
  7723. // 32bit reinterprets
  7724. instr->m_opcode = Js::OpCode::MOVD;
  7725. Legalize(instr);
  7726. return instr;
  7727. }
  7728. IR::Instr *
  7729. LowererMD::LowerInt64Assign(IR::Instr * instr)
  7730. {
  7731. return this->lowererMDArch.LowerInt64Assign(instr);
  7732. }
  7733. IR::Instr *
  7734. LowererMD::LowerToFloat(IR::Instr *instr)
  7735. {
  7736. switch (instr->m_opcode)
  7737. {
  7738. case Js::OpCode::Add_A:
  7739. Assert(instr->GetDst()->GetType() == instr->GetSrc1()->GetType());
  7740. Assert(instr->GetSrc1()->GetType() == instr->GetSrc2()->GetType());
  7741. instr->m_opcode = instr->GetSrc1()->IsFloat64() ? Js::OpCode::ADDSD : Js::OpCode::ADDSS;
  7742. break;
  7743. case Js::OpCode::Sub_A:
  7744. Assert(instr->GetDst()->GetType() == instr->GetSrc1()->GetType());
  7745. Assert(instr->GetSrc1()->GetType() == instr->GetSrc2()->GetType());
  7746. instr->m_opcode = instr->GetSrc1()->IsFloat64() ? Js::OpCode::SUBSD : Js::OpCode::SUBSS;
  7747. break;
  7748. case Js::OpCode::Mul_A:
  7749. Assert(instr->GetDst()->GetType() == instr->GetSrc1()->GetType());
  7750. Assert(instr->GetSrc1()->GetType() == instr->GetSrc2()->GetType());
  7751. instr->m_opcode = instr->GetSrc1()->IsFloat64() ? Js::OpCode::MULSD : Js::OpCode::MULSS;
  7752. break;
  7753. case Js::OpCode::Div_A:
  7754. Assert(instr->GetDst()->GetType() == instr->GetSrc1()->GetType());
  7755. Assert(instr->GetSrc1()->GetType() == instr->GetSrc2()->GetType());
  7756. instr->m_opcode = instr->GetSrc1()->IsFloat64() ? Js::OpCode::DIVSD : Js::OpCode::DIVSS;
  7757. break;
  7758. case Js::OpCode::Neg_A:
  7759. {
  7760. IR::Opnd *opnd;
  7761. instr->m_opcode = Js::OpCode::XORPS;
  7762. if (instr->GetDst()->IsFloat32())
  7763. {
  7764. opnd = IR::MemRefOpnd::New(m_func->GetThreadContextInfo()->GetMaskNegFloatAddr(), TyFloat32, this->m_func, IR::AddrOpndKindDynamicFloatRef);
  7765. }
  7766. else
  7767. {
  7768. Assert(instr->GetDst()->IsFloat64());
  7769. opnd = IR::MemRefOpnd::New(m_func->GetThreadContextInfo()->GetMaskNegDoubleAddr(), TyMachDouble, this->m_func, IR::AddrOpndKindDynamicDoubleRef);
  7770. }
  7771. instr->SetSrc2(opnd);
  7772. Legalize(instr);
  7773. break;
  7774. }
  7775. case Js::OpCode::BrEq_A:
  7776. case Js::OpCode::BrNeq_A:
  7777. case Js::OpCode::BrSrEq_A:
  7778. case Js::OpCode::BrSrNeq_A:
  7779. case Js::OpCode::BrGt_A:
  7780. case Js::OpCode::BrGe_A:
  7781. case Js::OpCode::BrLt_A:
  7782. case Js::OpCode::BrLe_A:
  7783. case Js::OpCode::BrNotEq_A:
  7784. case Js::OpCode::BrNotNeq_A:
  7785. case Js::OpCode::BrSrNotEq_A:
  7786. case Js::OpCode::BrSrNotNeq_A:
  7787. case Js::OpCode::BrNotGt_A:
  7788. case Js::OpCode::BrNotGe_A:
  7789. case Js::OpCode::BrNotLt_A:
  7790. case Js::OpCode::BrNotLe_A:
  7791. return this->LowerFloatCondBranch(instr->AsBranchInstr());
  7792. default:
  7793. Assume(UNREACHED);
  7794. }
  7795. this->MakeDstEquSrc1(instr);
  7796. return instr;
  7797. }
  7798. IR::BranchInstr *
  7799. LowererMD::LowerFloatCondBranch(IR::BranchInstr *instrBranch, bool ignoreNan)
  7800. {
  7801. Js::OpCode brOpcode = Js::OpCode::InvalidOpCode;
  7802. Js::OpCode cmpOpcode = Js::OpCode::InvalidOpCode;
  7803. IR::Instr *instr;
  7804. bool swapCmpOpnds = false;
  7805. bool addJP = false;
  7806. IR::LabelInstr *labelNaN = nullptr;
  7807. // Generate float compare that behave correctly for NaN's.
  7808. // These branch on unordered:
  7809. // JB
  7810. // JBE
  7811. // JE
  7812. // These don't branch on unordered:
  7813. // JA
  7814. // JAE
  7815. // JNE
  7816. // Unfortunately, only JA and JAE do what we'd like....
  7817. Func * func = instrBranch->m_func;
  7818. IR::Opnd *src1 = instrBranch->UnlinkSrc1();
  7819. IR::Opnd *src2 = instrBranch->UnlinkSrc2();
  7820. Assert(src1->GetType() == src2->GetType());
  7821. switch (instrBranch->m_opcode)
  7822. {
  7823. case Js::OpCode::BrSrEq_A:
  7824. case Js::OpCode::BrEq_A:
  7825. case Js::OpCode::BrSrNotNeq_A:
  7826. case Js::OpCode::BrNotNeq_A:
  7827. cmpOpcode = src1->IsFloat64() ? Js::OpCode::UCOMISD : Js::OpCode::UCOMISS;
  7828. brOpcode = Js::OpCode::JEQ;
  7829. if (!ignoreNan)
  7830. {
  7831. // Don't jump on NaN's
  7832. labelNaN = instrBranch->GetOrCreateContinueLabel();
  7833. addJP = true;
  7834. }
  7835. break;
  7836. case Js::OpCode::BrNeq_A:
  7837. case Js::OpCode::BrSrNeq_A:
  7838. case Js::OpCode::BrSrNotEq_A:
  7839. case Js::OpCode::BrNotEq_A:
  7840. cmpOpcode = src1->IsFloat64() ? Js::OpCode::UCOMISD : Js::OpCode::UCOMISS;
  7841. brOpcode = Js::OpCode::JNE;
  7842. if (!ignoreNan)
  7843. {
  7844. // Jump on NaN's
  7845. labelNaN = instrBranch->GetTarget();
  7846. addJP = true;
  7847. }
  7848. break;
  7849. case Js::OpCode::BrLe_A:
  7850. swapCmpOpnds = true;
  7851. brOpcode = Js::OpCode::JAE;
  7852. break;
  7853. case Js::OpCode::BrLt_A:
  7854. swapCmpOpnds = true;
  7855. brOpcode = Js::OpCode::JA;
  7856. break;
  7857. case Js::OpCode::BrGe_A:
  7858. brOpcode = Js::OpCode::JAE;
  7859. break;
  7860. case Js::OpCode::BrGt_A:
  7861. brOpcode = Js::OpCode::JA;
  7862. break;
  7863. case Js::OpCode::BrNotLe_A:
  7864. swapCmpOpnds = true;
  7865. brOpcode = Js::OpCode::JB;
  7866. break;
  7867. case Js::OpCode::BrNotLt_A:
  7868. swapCmpOpnds = true;
  7869. brOpcode = Js::OpCode::JBE;
  7870. break;
  7871. case Js::OpCode::BrNotGe_A:
  7872. brOpcode = Js::OpCode::JB;
  7873. break;
  7874. case Js::OpCode::BrNotGt_A:
  7875. brOpcode = Js::OpCode::JBE;
  7876. break;
  7877. default:
  7878. Assume(UNREACHED);
  7879. }
  7880. // if we haven't set cmpOpcode, then we are using COMISD/COMISS
  7881. if (cmpOpcode == Js::OpCode::InvalidOpCode)
  7882. {
  7883. cmpOpcode = src1->IsFloat64() ? Js::OpCode::COMISD : Js::OpCode::COMISS;
  7884. }
  7885. if (swapCmpOpnds)
  7886. {
  7887. IR::Opnd *tmp = src1;
  7888. src1 = src2;
  7889. src2 = tmp;
  7890. }
  7891. // VC generates UCOMISD for BrEq/BrNeq, and COMISD for all others, accordingly to IEEE 754.
  7892. // We'll do the same.
  7893. // COMISD / UCOMISD src1, src2
  7894. IR::Instr *instrCmp = IR::Instr::New(cmpOpcode, func);
  7895. instrCmp->SetSrc1(src1);
  7896. instrCmp->SetSrc2(src2);
  7897. instrBranch->InsertBefore(instrCmp);
  7898. Legalize(instrCmp);
  7899. if (addJP)
  7900. {
  7901. // JP $LabelNaN
  7902. instr = IR::BranchInstr::New(Js::OpCode::JP, labelNaN, func);
  7903. instrBranch->InsertBefore(instr);
  7904. }
  7905. // Jcc $L
  7906. instr = IR::BranchInstr::New(brOpcode, instrBranch->GetTarget(), func);
  7907. instrBranch->InsertBefore(instr);
  7908. instrBranch->Remove();
  7909. return instr->AsBranchInstr();
  7910. }
  7911. void LowererMD::HelperCallForAsmMathBuiltin(IR::Instr* instr, IR::JnHelperMethod helperMethodFloat, IR::JnHelperMethod helperMethodDouble)
  7912. {
  7913. Assert(instr->m_opcode == Js::OpCode::InlineMathFloor || instr->m_opcode == Js::OpCode::InlineMathCeil || instr->m_opcode == Js::OpCode::Trunc_A || instr->m_opcode == Js::OpCode::Nearest_A);
  7914. AssertMsg(instr->GetDst()->IsFloat(), "dst must be float.");
  7915. Assert(instr->GetDst()->GetType() == instr->GetSrc1()->GetType());
  7916. Assert(!instr->GetSrc2());
  7917. IR::Opnd * argOpnd = instr->UnlinkSrc1();
  7918. IR::JnHelperMethod helperMethod;
  7919. uint dwordCount;
  7920. if (argOpnd->IsFloat32())
  7921. {
  7922. helperMethod = helperMethodFloat;
  7923. LoadFloatHelperArgument(instr, argOpnd);
  7924. dwordCount = 1;
  7925. }
  7926. else
  7927. {
  7928. helperMethod = helperMethodDouble;
  7929. LoadDoubleHelperArgument(instr, argOpnd);
  7930. dwordCount = 2;
  7931. }
  7932. instr->m_opcode = Js::OpCode::CALL;
  7933. IR::HelperCallOpnd *helperCallOpnd = Lowerer::CreateHelperCallOpnd(helperMethod, this->lowererMDArch.GetHelperArgsCount(), m_func);
  7934. instr->SetSrc1(helperCallOpnd);
  7935. this->lowererMDArch.LowerCall(instr, dwordCount);
  7936. }
  7937. void LowererMD::GenerateFastInlineBuiltInCall(IR::Instr* instr, IR::JnHelperMethod helperMethod)
  7938. {
  7939. switch (instr->m_opcode)
  7940. {
  7941. case Js::OpCode::InlineMathSqrt:
  7942. // Sqrt maps directly to the SSE2 instruction.
  7943. // src and dst should already be XMM registers, all we need is just change the opcode.
  7944. Assert(helperMethod == (IR::JnHelperMethod)0);
  7945. Assert(instr->GetSrc2() == nullptr);
  7946. instr->m_opcode = instr->GetSrc1()->IsFloat64() ? Js::OpCode::SQRTSD : Js::OpCode::SQRTSS;
  7947. break;
  7948. case Js::OpCode::InlineMathAbs:
  7949. Assert(helperMethod == (IR::JnHelperMethod)0);
  7950. return GenerateFastInlineBuiltInMathAbs(instr);
  7951. case Js::OpCode::InlineMathPow:
  7952. #ifdef _M_IX86
  7953. if (!instr->GetSrc2()->IsFloat())
  7954. {
  7955. #endif
  7956. this->GenerateFastInlineBuiltInMathPow(instr);
  7957. break;
  7958. #ifdef _M_IX86
  7959. }
  7960. // fallthrough
  7961. #endif
  7962. case Js::OpCode::InlineMathAcos:
  7963. case Js::OpCode::InlineMathAsin:
  7964. case Js::OpCode::InlineMathAtan:
  7965. case Js::OpCode::InlineMathAtan2:
  7966. case Js::OpCode::InlineMathCos:
  7967. case Js::OpCode::InlineMathExp:
  7968. case Js::OpCode::InlineMathLog:
  7969. case Js::OpCode::Expo_A: //** operator reuses InlineMathPow fastpath
  7970. case Js::OpCode::InlineMathSin:
  7971. case Js::OpCode::InlineMathTan:
  7972. {
  7973. AssertMsg(instr->GetDst()->IsFloat(), "dst must be float.");
  7974. AssertMsg(instr->GetSrc1()->IsFloat(), "src1 must be float.");
  7975. AssertMsg(!instr->GetSrc2() || instr->GetSrc2()->IsFloat(), "src2 must be float.");
  7976. // Before:
  7977. // dst = <Built-in call> src1, src2
  7978. // After:
  7979. // I386:
  7980. // XMM0 = MOVSD src1
  7981. // CALL helperMethod
  7982. // dst = MOVSD call->dst
  7983. // AMD64:
  7984. // XMM0 = MOVSD src1
  7985. // RAX = MOV helperMethod
  7986. // CALL RAX
  7987. // dst = MOVSD call->dst
  7988. // Src1
  7989. IR::Instr* argOut = IR::Instr::New(Js::OpCode::MOVSD, this->m_func);
  7990. IR::RegOpnd* dst1 = IR::RegOpnd::New(nullptr, (RegNum)FIRST_FLOAT_ARG_REG, TyMachDouble, this->m_func);
  7991. dst1->m_isCallArg = true; // This is to make sure that lifetime of opnd is virtually extended until next CALL instr.
  7992. argOut->SetDst(dst1);
  7993. argOut->SetSrc1(instr->UnlinkSrc1());
  7994. instr->InsertBefore(argOut);
  7995. // Src2
  7996. if (instr->GetSrc2() != nullptr)
  7997. {
  7998. IR::Instr* argOut2 = IR::Instr::New(Js::OpCode::MOVSD, this->m_func);
  7999. IR::RegOpnd* dst2 = IR::RegOpnd::New(nullptr, (RegNum)(FIRST_FLOAT_ARG_REG + 1), TyMachDouble, this->m_func);
  8000. dst2->m_isCallArg = true; // This is to make sure that lifetime of opnd is virtually extended until next CALL instr.
  8001. argOut2->SetDst(dst2);
  8002. argOut2->SetSrc1(instr->UnlinkSrc2());
  8003. instr->InsertBefore(argOut2);
  8004. }
  8005. // Call CRT.
  8006. IR::RegOpnd* floatCallDst = IR::RegOpnd::New(nullptr, (RegNum)(FIRST_FLOAT_REG), TyMachDouble, this->m_func); // Dst in XMM0.
  8007. #ifdef _M_IX86
  8008. IR::Instr* floatCall = IR::Instr::New(Js::OpCode::CALL, floatCallDst, this->m_func);
  8009. floatCall->SetSrc1(IR::HelperCallOpnd::New(helperMethod, this->m_func));
  8010. instr->InsertBefore(floatCall);
  8011. #else
  8012. // s1 = MOV helperAddr
  8013. IR::RegOpnd* s1 = IR::RegOpnd::New(TyMachReg, this->m_func);
  8014. IR::AddrOpnd* helperAddr = IR::AddrOpnd::New((Js::Var)IR::GetMethodOriginalAddress(m_func->GetThreadContextInfo(), helperMethod), IR::AddrOpndKind::AddrOpndKindDynamicMisc, this->m_func);
  8015. IR::Instr* mov = IR::Instr::New(Js::OpCode::MOV, s1, helperAddr, this->m_func);
  8016. instr->InsertBefore(mov);
  8017. // dst(XMM0) = CALL s1
  8018. IR::Instr *floatCall = IR::Instr::New(Js::OpCode::CALL, floatCallDst, s1, this->m_func);
  8019. instr->InsertBefore(floatCall);
  8020. #endif
  8021. instr->m_func->SetHasCalls();
  8022. // Save the result.
  8023. instr->m_opcode = Js::OpCode::MOVSD;
  8024. instr->SetSrc1(floatCall->GetDst());
  8025. break;
  8026. }
  8027. case Js::OpCode::InlineMathFloor:
  8028. case Js::OpCode::InlineMathCeil:
  8029. case Js::OpCode::InlineMathRound:
  8030. #ifdef ENABLE_WASM
  8031. case Js::OpCode::Trunc_A:
  8032. case Js::OpCode::Nearest_A:
  8033. #endif //ENABLE_WASM
  8034. {
  8035. Assert(AutoSystemInfo::Data.SSE4_1Available());
  8036. Assert(instr->GetDst()->IsInt32() || instr->GetDst()->IsFloat());
  8037. // MOVSD roundedFloat, src
  8038. //
  8039. // if(round)
  8040. // {
  8041. // /* N.B.: the following CMPs are lowered to COMISDs, whose results can only be >, <, or =.
  8042. // In fact, only ">" can be used if NaN has not been handled.
  8043. // */
  8044. // CMP 0.5, roundedFloat
  8045. // JA $ltHalf
  8046. // CMP TwoToFraction, roundedFloat
  8047. // JA $addHalfToRoundSrcLabel
  8048. // J $skipRoundSd (NaN is also handled here)
  8049. // $ltHalf:
  8050. // CMP roundedFloat, -0.5
  8051. // JL $ltNegHalf
  8052. // if (shouldCheckNegZero) {
  8053. // CMP roundedFloat, 0
  8054. // JA $setZero
  8055. // $negZeroTest [Helper]:
  8056. // JB $bailoutLabel
  8057. // isNegZero(src)
  8058. // JE $bailoutLabel
  8059. // J $skipRoundSd
  8060. // } // else: setZero
  8061. // $setZero:
  8062. // MOV roundedFloat, 0
  8063. // J $skipRoundSd
  8064. // $ltNegHalf:
  8065. // CMP roundedFloat, NegTwoToFraction
  8066. // JA $addHalfToRoundSrc
  8067. // J $skipRoundSd
  8068. // $addHalfToRoundSrc:
  8069. // ADDSD roundedFloat, 0.5
  8070. // $skipAddHalf:
  8071. // }
  8072. //
  8073. // if(isNotCeil)
  8074. // {
  8075. // CMP roundedFloat, 0
  8076. // JGE $skipRoundSd
  8077. // }
  8078. // ROUNDSD roundedFloat, roundedFloat, round_mode
  8079. //
  8080. // $skipRoundSd:
  8081. // if(isNotCeil)
  8082. // MOVSD checkNegZeroOpnd, roundedFloat
  8083. // else if (ceil)
  8084. // MOVSD checkNegZeroOpnd, src
  8085. //
  8086. // CMP checkNegZeroOpnd, 0
  8087. // JNE $convertToInt
  8088. //
  8089. // if(instr->ShouldCheckForNegativeZero())
  8090. // {
  8091. // isNegZero CALL IsNegZero(checkNegZeroOpnd)
  8092. // CMP isNegZero, 0
  8093. // JNE $bailoutLabel
  8094. // }
  8095. //
  8096. // $convertToInt:
  8097. // CVT(T)SD2SI dst, roundedFloat //CVTTSD2SI for floor/round and CVTSD2SI for ceil
  8098. // CMP dst 0x80000000
  8099. // JNE $fallthrough
  8100. //
  8101. // if(!sharedBailout)
  8102. // {
  8103. // $bailoutLabel:
  8104. // }
  8105. // GenerateBailout(instr)
  8106. //
  8107. // $fallthrough:
  8108. bool isNotCeil = instr->m_opcode != Js::OpCode::InlineMathCeil;
  8109. // MOVSD roundedFloat, src
  8110. IR::Opnd * src = instr->UnlinkSrc1();
  8111. IR::RegOpnd* roundedFloat = IR::RegOpnd::New(src->GetType(), this->m_func);
  8112. IR::Instr* argOut = IR::Instr::New(LowererMDArch::GetAssignOp(src->GetType()), roundedFloat, src, this->m_func);
  8113. instr->InsertBefore(argOut);
  8114. bool negZeroCheckDone = false;
  8115. IR::LabelInstr * bailoutLabel = nullptr;
  8116. bool sharedBailout = false;
  8117. if (instr->GetDst()->IsInt32())
  8118. {
  8119. sharedBailout = (instr->GetBailOutInfo()->bailOutInstr != instr) ? true : false;
  8120. bailoutLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, /*helperLabel*/true);
  8121. }
  8122. IR::Opnd * zero;
  8123. if (src->IsFloat64())
  8124. {
  8125. zero = IR::MemRefOpnd::New(m_func->GetThreadContextInfo()->GetDoubleZeroAddr(), TyFloat64, this->m_func, IR::AddrOpndKindDynamicDoubleRef);
  8126. }
  8127. else
  8128. {
  8129. Assert(src->IsFloat32());
  8130. zero = IR::MemRefOpnd::New(m_func->GetThreadContextInfo()->GetFloatZeroAddr(), TyFloat32, this->m_func, IR::AddrOpndKindDynamicFloatRef);
  8131. }
  8132. IR::AutoReuseOpnd autoReuseZero(zero, this->m_func);
  8133. IR::LabelInstr * skipRoundSd = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  8134. if(instr->m_opcode == Js::OpCode::InlineMathRound)
  8135. {
  8136. IR::LabelInstr * addHalfToRoundSrcLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  8137. IR::LabelInstr * ltHalf = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  8138. IR::LabelInstr * setZero = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  8139. IR::LabelInstr * ltNegHalf = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  8140. IR::Opnd * pointFive;
  8141. IR::Opnd * negPointFive;
  8142. if (src->IsFloat64())
  8143. {
  8144. pointFive = IR::MemRefOpnd::New(m_func->GetThreadContextInfo()->GetDoublePointFiveAddr(), TyFloat64, this->m_func, IR::AddrOpndKindDynamicDoubleRef);
  8145. negPointFive = IR::MemRefOpnd::New(m_func->GetThreadContextInfo()->GetDoubleNegPointFiveAddr(), TyFloat64, this->m_func, IR::AddrOpndKindDynamicDoubleRef);
  8146. }
  8147. else
  8148. {
  8149. Assert(src->IsFloat32());
  8150. pointFive = IR::MemRefOpnd::New(m_func->GetThreadContextInfo()->GetFloatPointFiveAddr(), TyFloat32, this->m_func, IR::AddrOpndKindDynamicFloatRef);
  8151. negPointFive = IR::MemRefOpnd::New(m_func->GetThreadContextInfo()->GetFloatNegPointFiveAddr(), TyFloat32, this->m_func, IR::AddrOpndKindDynamicFloatRef);
  8152. }
  8153. // CMP 0.5, roundedFloat
  8154. // JA $ltHalf
  8155. this->m_lowerer->InsertCompareBranch(pointFive, roundedFloat, Js::OpCode::BrGt_A, ltHalf, instr);
  8156. if (instr->GetDst()->IsInt32())
  8157. {
  8158. // if we are specializing dst to int, we will bailout on overflow so don't need upperbound check
  8159. // Also, we will bailout on NaN, so it doesn't need special handling either
  8160. // J $addHalfToRoundSrcLabel
  8161. this->m_lowerer->InsertBranch(Js::OpCode::Br, addHalfToRoundSrcLabel, instr);
  8162. }
  8163. else
  8164. {
  8165. IR::Opnd * twoToFraction;
  8166. if (src->IsFloat64())
  8167. {
  8168. twoToFraction = IR::MemRefOpnd::New(m_func->GetThreadContextInfo()->GetDoubleTwoToFractionAddr(), TyFloat64, this->m_func, IR::AddrOpndKindDynamicDoubleRef);
  8169. }
  8170. else
  8171. {
  8172. Assert(src->IsFloat32());
  8173. twoToFraction = IR::MemRefOpnd::New(m_func->GetThreadContextInfo()->GetFloatTwoToFractionAddr(), TyFloat32, this->m_func, IR::AddrOpndKindDynamicFloatRef);
  8174. }
  8175. // CMP 2^fraction, roundedFloat
  8176. // JA $addHalfToRoundSrcLabel
  8177. this->m_lowerer->InsertCompareBranch(twoToFraction, roundedFloat, Js::OpCode::BrGt_A, addHalfToRoundSrcLabel, instr);
  8178. // J $skipRoundSd (NaN also handled here)
  8179. this->m_lowerer->InsertBranch(Js::OpCode::Br, skipRoundSd, instr);
  8180. }
  8181. // $ltHalf:
  8182. instr->InsertBefore(ltHalf);
  8183. // CMP roundedFloat, -0.5
  8184. // JL $ltNegHalf
  8185. this->m_lowerer->InsertCompareBranch(roundedFloat, negPointFive, Js::OpCode::BrLt_A, ltNegHalf, instr);
  8186. if (instr->ShouldCheckForNegativeZero())
  8187. {
  8188. // CMP roundedFloat, 0
  8189. // JA $setZero
  8190. this->m_lowerer->InsertCompareBranch(roundedFloat, zero, Js::OpCode::BrGt_A, setZero, instr);
  8191. // $negZeroTest [helper]
  8192. m_lowerer->InsertLabel(true, instr);
  8193. // JB $bailoutLabel
  8194. this->m_lowerer->InsertBranch(Js::OpCode::JB, bailoutLabel, instr);
  8195. IR::Opnd* isNegZero = IsOpndNegZero(src, instr);
  8196. // if isNegZero(src) J $bailoutLabel
  8197. this->m_lowerer->InsertTestBranch(isNegZero, isNegZero, Js::OpCode::BrNeq_A, bailoutLabel, instr);
  8198. // else J $skipRoundSd
  8199. this->m_lowerer->InsertBranch(Js::OpCode::Br, skipRoundSd, instr);
  8200. negZeroCheckDone = true;
  8201. }
  8202. // $setZero:
  8203. instr->InsertBefore(setZero);
  8204. // MOVSD_ZERO roundedFloat
  8205. LoadFloatZero(roundedFloat, instr);
  8206. // J $skipRoundSd
  8207. this->m_lowerer->InsertBranch(Js::OpCode::Br, skipRoundSd, instr);
  8208. // $ltNegHalf:
  8209. instr->InsertBefore(ltNegHalf);
  8210. if (!instr->GetDst()->IsInt32())
  8211. {
  8212. // if we are specializing dst to int, we will bailout on overflow so don't need lowerbound check
  8213. IR::Opnd * negTwoToFraction;
  8214. if (src->IsFloat64())
  8215. {
  8216. negTwoToFraction = IR::MemRefOpnd::New(m_func->GetThreadContextInfo()->GetDoubleNegTwoToFractionAddr(), TyFloat64, this->m_func, IR::AddrOpndKindDynamicDoubleRef);
  8217. }
  8218. else
  8219. {
  8220. Assert(src->IsFloat32());
  8221. negTwoToFraction = IR::MemRefOpnd::New(m_func->GetThreadContextInfo()->GetFloatNegTwoToFractionAddr(), TyFloat32, this->m_func, IR::AddrOpndKindDynamicFloatRef);
  8222. }
  8223. // CMP roundedFloat, negTwoToFraction
  8224. // JA $addHalfToRoundSrcLabel
  8225. this->m_lowerer->InsertCompareBranch(roundedFloat, negTwoToFraction, Js::OpCode::BrGt_A, addHalfToRoundSrcLabel, instr);
  8226. // J $skipRoundSd
  8227. this->m_lowerer->InsertBranch(Js::OpCode::Br, skipRoundSd, instr);
  8228. }
  8229. if (src->IsFloat64())
  8230. {
  8231. pointFive = IR::MemRefOpnd::New(m_func->GetThreadContextInfo()->GetDoublePointFiveAddr(), TyFloat64, this->m_func, IR::AddrOpndKindDynamicDoubleRef);
  8232. }
  8233. else
  8234. {
  8235. Assert(src->IsFloat32());
  8236. pointFive = IR::MemRefOpnd::New(m_func->GetThreadContextInfo()->GetFloatPointFiveAddr(), TyFloat32, this->m_func, IR::AddrOpndKindDynamicFloatRef);
  8237. }
  8238. // $addHalfToRoundSrcLabel
  8239. instr->InsertBefore(addHalfToRoundSrcLabel);
  8240. // ADDSD roundedFloat, 0.5
  8241. IR::Instr * addInstr = IR::Instr::New(src->IsFloat64() ? Js::OpCode::ADDSD : Js::OpCode::ADDSS, roundedFloat, roundedFloat, pointFive, this->m_func);
  8242. instr->InsertBefore(addInstr);
  8243. Legalize(addInstr);
  8244. }
  8245. if (instr->m_opcode == Js::OpCode::InlineMathFloor && instr->GetDst()->IsInt32())
  8246. {
  8247. this->m_lowerer->InsertCompareBranch(roundedFloat, zero, Js::OpCode::BrGe_A, skipRoundSd, instr);
  8248. }
  8249. // ROUNDSD srcCopy, srcCopy, round_mode
  8250. IR::Opnd * roundMode = nullptr;
  8251. switch (instr->m_opcode)
  8252. {
  8253. #ifdef ENABLE_WASM
  8254. case Js::OpCode::Trunc_A:
  8255. roundMode = IR::IntConstOpnd::New(0x03, TyInt32, this->m_func);
  8256. break;
  8257. case Js::OpCode::Nearest_A:
  8258. roundMode = IR::IntConstOpnd::New(0x00, TyInt32, this->m_func);
  8259. break;
  8260. #endif //ENABLE_WASM
  8261. case Js::OpCode::InlineMathRound:
  8262. case Js::OpCode::InlineMathFloor:
  8263. roundMode = IR::IntConstOpnd::New(0x01, TyInt32, this->m_func);
  8264. break;
  8265. case Js::OpCode::InlineMathCeil:
  8266. roundMode = IR::IntConstOpnd::New(0x02, TyInt32, this->m_func);
  8267. break;
  8268. }
  8269. IR::Instr* roundInstr = IR::Instr::New(src->IsFloat64() ? Js::OpCode::ROUNDSD : Js::OpCode::ROUNDSS, roundedFloat, roundedFloat, roundMode, this->m_func);
  8270. instr->InsertBefore(roundInstr);
  8271. if (instr->m_opcode == Js::OpCode::InlineMathRound)
  8272. {
  8273. instr->InsertBefore(skipRoundSd);
  8274. }
  8275. if (instr->GetDst()->IsInt32())
  8276. {
  8277. if (instr->m_opcode == Js::OpCode::InlineMathFloor)
  8278. {
  8279. instr->InsertBefore(skipRoundSd);
  8280. }
  8281. //negZero bailout
  8282. if(instr->ShouldCheckForNegativeZero() && !negZeroCheckDone)
  8283. {
  8284. IR::LabelInstr * convertToInt = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  8285. IR::Opnd * checkNegZeroOpnd;
  8286. if(isNotCeil)
  8287. {
  8288. checkNegZeroOpnd = src;
  8289. }
  8290. else
  8291. {
  8292. checkNegZeroOpnd = roundedFloat;
  8293. }
  8294. this->m_lowerer->InsertCompareBranch(checkNegZeroOpnd, zero, Js::OpCode::BrNeq_A, convertToInt, instr);
  8295. IR::Opnd* isNegZero = IsOpndNegZero(checkNegZeroOpnd, instr);
  8296. this->m_lowerer->InsertCompareBranch(isNegZero, IR::IntConstOpnd::New(0x00000000, IRType::TyInt32, this->m_func), Js::OpCode::BrNeq_A, bailoutLabel, instr);
  8297. instr->InsertBefore(convertToInt);
  8298. }
  8299. IR::Opnd * originalDst = instr->UnlinkDst();
  8300. // CVT(T)SD2SI dst, srcCopy
  8301. IR::Instr* convertToIntInstr;
  8302. if (isNotCeil)
  8303. {
  8304. convertToIntInstr = IR::Instr::New(src->IsFloat64() ? Js::OpCode::CVTTSD2SI : Js::OpCode::CVTTSS2SI, originalDst, roundedFloat, this->m_func);
  8305. }
  8306. else
  8307. {
  8308. convertToIntInstr = IR::Instr::New(src->IsFloat64() ? Js::OpCode::CVTSD2SI : Js::OpCode::CVTSS2SI, originalDst, roundedFloat, this->m_func);
  8309. }
  8310. instr->InsertBefore(convertToIntInstr);
  8311. IR::LabelInstr * fallthrough = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  8312. IR::Opnd * intOverflowValue = IR::IntConstOpnd::New(INT32_MIN, IRType::TyInt32, this->m_func, true);
  8313. this->m_lowerer->InsertCompareBranch(originalDst, intOverflowValue, Js::OpCode::BrNeq_A, fallthrough, instr);
  8314. instr->InsertAfter(fallthrough);
  8315. if (!sharedBailout)
  8316. {
  8317. instr->InsertBefore(bailoutLabel);
  8318. }
  8319. // In case of a shared bailout, we should jump to the code that sets some data on the bailout record which is specific
  8320. // to this bailout. Pass the bailoutLabel to GenerateFunction so that it may use the label as the collectRuntimeStatsLabel.
  8321. this->m_lowerer->GenerateBailOut(instr, nullptr, nullptr, sharedBailout ? bailoutLabel : nullptr);
  8322. }
  8323. else
  8324. {
  8325. IR::Opnd * originalDst = instr->UnlinkDst();
  8326. Assert(originalDst->IsFloat());
  8327. Assert(originalDst->GetType() == roundedFloat->GetType());
  8328. IR::Instr * movInstr = IR::Instr::New(originalDst->IsFloat64() ? Js::OpCode::MOVSD : Js::OpCode::MOVSS, originalDst, roundedFloat, this->m_func);
  8329. instr->InsertBefore(movInstr);
  8330. instr->Remove();
  8331. }
  8332. break;
  8333. }
  8334. case Js::OpCode::InlineMathMin:
  8335. case Js::OpCode::InlineMathMax:
  8336. {
  8337. IR::Opnd* src1 = instr->GetSrc1();
  8338. IR::Opnd* src2 = instr->GetSrc2();
  8339. IR::Opnd* dst = instr->GetDst();
  8340. IR::LabelInstr* doneLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  8341. IR::LabelInstr* labelNaNHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  8342. IR::LabelInstr* labelNegZeroAndNaNCheckHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  8343. IR::Instr* branchInstr;
  8344. bool min = instr->m_opcode == Js::OpCode::InlineMathMin ? true : false;
  8345. // CMP src1, src2
  8346. if(dst->IsInt32())
  8347. {
  8348. //MOV dst, src2;
  8349. Assert(!dst->IsEqual(src2));
  8350. this->m_lowerer->InsertMove(dst, src2, instr);
  8351. if(min)
  8352. {
  8353. // JLT $continueLabel
  8354. branchInstr = IR::BranchInstr::New(Js::OpCode::BrGt_I4, doneLabel, src1, src2, instr->m_func);
  8355. instr->InsertBefore(branchInstr);
  8356. LowererMDArch::EmitInt4Instr(branchInstr);
  8357. }
  8358. else
  8359. {
  8360. // JGT $continueLabel
  8361. branchInstr = IR::BranchInstr::New(Js::OpCode::BrLt_I4, doneLabel, src1, src2, instr->m_func);
  8362. instr->InsertBefore(branchInstr);
  8363. LowererMDArch::EmitInt4Instr(branchInstr);
  8364. }
  8365. // MOV dst, src1
  8366. this->m_lowerer->InsertMove(dst, src1, instr);
  8367. }
  8368. else if(dst->IsFloat())
  8369. {
  8370. // COMISD/COMISS src1 (src2), src2 (src1)
  8371. // JA $doneLabel
  8372. // JEQ $labelNegZeroAndNaNCheckHelper
  8373. // MOVSD/MOVSS dst, src2
  8374. // JMP $doneLabel
  8375. //
  8376. // $labelNegZeroAndNaNCheckHelper
  8377. // JP $labelNaNHelper
  8378. // if(min)
  8379. // {
  8380. // if(src2 == -0.0)
  8381. // MOVSD/MOVSS dst, src2
  8382. // }
  8383. // else
  8384. // {
  8385. // if(src1 == -0.0)
  8386. // MOVSD/MOVSS dst, src2
  8387. // }
  8388. // JMP $doneLabel
  8389. //
  8390. // $labelNaNHelper
  8391. // MOVSD/MOVSS dst, NaN
  8392. //
  8393. // $doneLabel
  8394. //MOVSD/MOVSS dst, src1;
  8395. Assert(!dst->IsEqual(src1));
  8396. this->m_lowerer->InsertMove(dst, src1, instr);
  8397. if(min)
  8398. {
  8399. this->m_lowerer->InsertCompareBranch(src1, src2, Js::OpCode::BrLt_A, doneLabel, instr); // Lowering of BrLt_A for floats is done to JA with operands swapped
  8400. }
  8401. else
  8402. {
  8403. this->m_lowerer->InsertCompareBranch(src1, src2, Js::OpCode::BrGt_A, doneLabel, instr);
  8404. }
  8405. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JEQ, labelNegZeroAndNaNCheckHelper, instr->m_func));
  8406. this->m_lowerer->InsertMove(dst, src2, instr);
  8407. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JMP, doneLabel, instr->m_func));
  8408. instr->InsertBefore(labelNegZeroAndNaNCheckHelper);
  8409. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JP, labelNaNHelper, instr->m_func));
  8410. IR::Opnd* isNegZero;
  8411. if(min)
  8412. {
  8413. isNegZero = IsOpndNegZero(src2, instr);
  8414. }
  8415. else
  8416. {
  8417. isNegZero = IsOpndNegZero(src1, instr);
  8418. }
  8419. this->m_lowerer->InsertCompareBranch(isNegZero, IR::IntConstOpnd::New(0x00000000, IRType::TyInt32, this->m_func), Js::OpCode::BrEq_A, doneLabel, instr);
  8420. this->m_lowerer->InsertMove(dst, src2, instr);
  8421. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JMP, doneLabel, instr->m_func));
  8422. instr->InsertBefore(labelNaNHelper);
  8423. IR::Opnd * opndNaN = nullptr;
  8424. if (dst->IsFloat32())
  8425. {
  8426. opndNaN = IR::MemRefOpnd::New(m_func->GetThreadContextInfo()->GetFloatNaNAddr(), IRType::TyFloat32, this->m_func);
  8427. }
  8428. else
  8429. {
  8430. opndNaN = IR::MemRefOpnd::New(m_func->GetThreadContextInfo()->GetDoubleNaNAddr(), IRType::TyFloat64, this->m_func);
  8431. }
  8432. this->m_lowerer->InsertMove(dst, opndNaN, instr);
  8433. }
  8434. instr->InsertBefore(doneLabel);
  8435. instr->Remove();
  8436. break;
  8437. }
  8438. default:
  8439. AssertMsg(FALSE, "Unknown inline built-in opcode");
  8440. break;
  8441. }
  8442. }
  8443. IR::Opnd* LowererMD::IsOpndNegZero(IR::Opnd* opnd, IR::Instr* instr)
  8444. {
  8445. IR::Opnd * isNegZero = IR::RegOpnd::New(TyInt32, this->m_func);
  8446. IR::Opnd *src = opnd;
  8447. if (opnd->IsFloat32())
  8448. {
  8449. src = IR::RegOpnd::New(TyFloat64, this->m_func);
  8450. instr->InsertBefore(IR::Instr::New(LowererMD::MDConvertFloat32ToFloat64Opcode, src, opnd, this->m_func));
  8451. }
  8452. Assert(src->IsFloat64());
  8453. LoadDoubleHelperArgument(instr, src);
  8454. IR::Instr * helperCallInstr = IR::Instr::New(Js::OpCode::CALL, isNegZero, this->m_func);
  8455. instr->InsertBefore(helperCallInstr);
  8456. this->ChangeToHelperCall(helperCallInstr, IR::HelperIsNegZero);
  8457. return isNegZero;
  8458. }
  8459. void LowererMD::GenerateFastInlineBuiltInMathAbs(IR::Instr* inlineInstr)
  8460. {
  8461. IR::Opnd* src = inlineInstr->GetSrc1();
  8462. IR::Opnd* dst = inlineInstr->UnlinkDst();
  8463. Assert(src);
  8464. IR::Instr* tmpInstr;
  8465. IR::Instr* nextInstr = IR::LabelInstr::New(Js::OpCode::Label, m_func);
  8466. IR::Instr* continueInstr = m_lowerer->LowerBailOnIntMin(inlineInstr);
  8467. continueInstr->InsertAfter(nextInstr);
  8468. IRType srcType = src->GetType();
  8469. if (srcType == IRType::TyInt32)
  8470. {
  8471. // Note: if execution gets so far, we always get (untagged) int32 here.
  8472. // Since -x = ~x + 1, abs(x) = x, abs(-x) = -x, sign-extend(x) = 0, sign_extend(-x) = -1, where 0 <= x.
  8473. // Then: abs(x) = sign-extend(x) XOR x - sign-extend(x)
  8474. // Expected input (otherwise bailout):
  8475. // - src1 is (untagged) int, not equal to int_min (abs(int_min) would produce overflow, as there's no corresponding positive int).
  8476. // MOV EAX, src
  8477. IR::RegOpnd *regEAX = IR::RegOpnd::New(TyInt32, this->m_func);
  8478. regEAX->SetReg(LowererMDArch::GetRegIMulDestLower());
  8479. tmpInstr = IR::Instr::New(Js::OpCode::MOV, regEAX, src, this->m_func);
  8480. nextInstr->InsertBefore(tmpInstr);
  8481. IR::RegOpnd *regEDX = IR::RegOpnd::New(TyInt32, this->m_func);
  8482. regEDX->SetReg(LowererMDArch::GetRegIMulHighDestLower());
  8483. // CDQ (sign-extend EAX into EDX, producing 64bit EDX:EAX value)
  8484. // Note: put EDX on dst to give of def to the EDX lifetime
  8485. tmpInstr = IR::Instr::New(Js::OpCode::CDQ, regEDX, this->m_func);
  8486. nextInstr->InsertBefore(tmpInstr);
  8487. // XOR EAX, EDX
  8488. tmpInstr = IR::Instr::New(Js::OpCode::XOR, regEAX, regEAX, regEDX, this->m_func);
  8489. nextInstr->InsertBefore(tmpInstr);
  8490. // SUB EAX, EDX
  8491. tmpInstr = IR::Instr::New(Js::OpCode::SUB, regEAX, regEAX, regEDX, this->m_func);
  8492. nextInstr->InsertBefore(tmpInstr);
  8493. // MOV dst, EAX
  8494. tmpInstr = IR::Instr::New(Js::OpCode::MOV, dst, regEAX, this->m_func);
  8495. nextInstr->InsertBefore(tmpInstr);
  8496. }
  8497. else if (srcType == IRType::TyFloat64)
  8498. {
  8499. if (!dst->IsRegOpnd())
  8500. {
  8501. // MOVSD tempRegOpnd, src
  8502. IR::RegOpnd* tempRegOpnd = IR::RegOpnd::New(nullptr, TyMachDouble, this->m_func);
  8503. tempRegOpnd->m_isCallArg = true; // This is to make sure that lifetime of opnd is virtually extended until next CALL instr.
  8504. tmpInstr = IR::Instr::New(Js::OpCode::MOVSD, tempRegOpnd, src, this->m_func);
  8505. nextInstr->InsertBefore(tmpInstr);
  8506. // This saves the result in the same register.
  8507. this->GenerateFloatAbs(static_cast<IR::RegOpnd*>(tempRegOpnd), nextInstr);
  8508. // MOVSD dst, tempRegOpnd
  8509. tmpInstr = IR::Instr::New(Js::OpCode::MOVSD, dst, tempRegOpnd, this->m_func);
  8510. nextInstr->InsertBefore(tmpInstr);
  8511. }
  8512. else
  8513. {
  8514. // MOVSD dst, src
  8515. tmpInstr = IR::Instr::New(Js::OpCode::MOVSD, dst, src, this->m_func);
  8516. nextInstr->InsertBefore(tmpInstr);
  8517. // This saves the result in the same register.
  8518. this->GenerateFloatAbs(static_cast<IR::RegOpnd*>(dst), nextInstr);
  8519. }
  8520. }
  8521. else if (srcType == IRType::TyFloat32)
  8522. {
  8523. if (!dst->IsRegOpnd())
  8524. {
  8525. // MOVSS tempRegOpnd, src
  8526. IR::RegOpnd* tempRegOpnd = IR::RegOpnd::New(nullptr, TyFloat32, this->m_func);
  8527. tempRegOpnd->m_isCallArg = true; // This is to make sure that lifetime of opnd is virtually extended until next CALL instr.
  8528. tmpInstr = IR::Instr::New(Js::OpCode::MOVSS, tempRegOpnd, src, this->m_func);
  8529. nextInstr->InsertBefore(tmpInstr);
  8530. // This saves the result in the same register.
  8531. this->GenerateFloatAbs(static_cast<IR::RegOpnd*>(tempRegOpnd), nextInstr);
  8532. // MOVSS dst, tempRegOpnd
  8533. tmpInstr = IR::Instr::New(Js::OpCode::MOVSS, dst, tempRegOpnd, this->m_func);
  8534. nextInstr->InsertBefore(tmpInstr);
  8535. }
  8536. else
  8537. {
  8538. // MOVSS dst, src
  8539. tmpInstr = IR::Instr::New(Js::OpCode::MOVSS, dst, src, this->m_func);
  8540. nextInstr->InsertBefore(tmpInstr);
  8541. // This saves the result in the same register.
  8542. this->GenerateFloatAbs(static_cast<IR::RegOpnd*>(dst), nextInstr);
  8543. }
  8544. }
  8545. else
  8546. {
  8547. AssertMsg(FALSE, "GenerateFastInlineBuiltInMathAbs: unexpected type of the src!");
  8548. }
  8549. }
  8550. void LowererMD::GenerateFastInlineBuiltInMathPow(IR::Instr* instr)
  8551. {
  8552. #ifdef _M_IX86
  8553. AssertMsg(!instr->GetSrc2()->IsFloat(), "Math.pow(*, double) needs customized lowering!");
  8554. #endif
  8555. IR::JnHelperMethod directPowHelper = (IR::JnHelperMethod)0;
  8556. IR::Opnd* bailoutOpnd = nullptr;
  8557. if (!instr->GetSrc2()->IsFloat())
  8558. {
  8559. LoadHelperArgument(instr, instr->UnlinkSrc2());
  8560. if (instr->GetSrc1()->IsFloat())
  8561. {
  8562. directPowHelper = IR::HelperDirectMath_PowDoubleInt;
  8563. LoadDoubleHelperArgument(instr, instr->UnlinkSrc1());
  8564. }
  8565. else
  8566. {
  8567. directPowHelper = IR::HelperDirectMath_PowIntInt;
  8568. LoadHelperArgument(instr, instr->UnlinkSrc1());
  8569. if (!this->m_func->tempSymBool)
  8570. {
  8571. this->m_func->tempSymBool = StackSym::New(TyUint8, this->m_func);
  8572. this->m_func->StackAllocate(this->m_func->tempSymBool, TySize[TyUint8]);
  8573. }
  8574. IR::SymOpnd* boolOpnd = IR::SymOpnd::New(this->m_func->tempSymBool, TyUint8, this->m_func);
  8575. IR::RegOpnd* boolRefOpnd = IR::RegOpnd::New(TyMachReg, this->m_func);
  8576. this->m_lowerer->InsertLea(boolRefOpnd, boolOpnd, instr);
  8577. LoadHelperArgument(instr, boolRefOpnd);
  8578. bailoutOpnd = boolOpnd;
  8579. }
  8580. }
  8581. #ifndef _M_IX86
  8582. else
  8583. {
  8584. AssertMsg(instr->GetSrc1()->IsFloat(), "Math.Pow(int, double) should not generated by GlobOpt!");
  8585. directPowHelper = IR::HelperDirectMath_Pow;
  8586. LoadDoubleHelperArgument(instr, instr->UnlinkSrc2());
  8587. LoadDoubleHelperArgument(instr, instr->UnlinkSrc1());
  8588. }
  8589. #endif
  8590. ChangeToHelperCall(instr, directPowHelper, nullptr, bailoutOpnd);
  8591. }
  8592. void
  8593. LowererMD::FinalLower()
  8594. {
  8595. this->lowererMDArch.FinalLower();
  8596. }
  8597. IR::Instr *
  8598. LowererMD::LowerDivI4AndBailOnReminder(IR::Instr * instr, IR::LabelInstr * bailOutLabel)
  8599. {
  8600. // Don't have save the operand for bailout because the lowering of IDIV don't overwrite their values
  8601. // (EDX) = CDQ
  8602. // EAX = numerator
  8603. // (EDX:EAX)= IDIV (EAX), denominator
  8604. // TEST EDX, EDX
  8605. // JNE bailout
  8606. // <Caller insert more checks here>
  8607. // dst = MOV EAX <-- assignInstr
  8608. Assert(instr);
  8609. Assert(instr->m_opcode == Js::OpCode::Div_I4);
  8610. Assert(!instr->HasBailOutInfo());
  8611. EmitInt4Instr(instr);
  8612. Assert(instr->m_opcode == Js::OpCode::IDIV);
  8613. IR::Instr * prev = instr->m_prev;
  8614. Assert(prev->m_opcode == Js::OpCode::CDQ);
  8615. #ifdef _M_IX86
  8616. Assert(prev->GetDst()->AsRegOpnd()->GetReg() == RegEDX);
  8617. #else
  8618. Assert(prev->GetDst()->AsRegOpnd()->GetReg() == RegRDX);
  8619. #endif
  8620. IR::Opnd * reminderOpnd = prev->GetDst();
  8621. // Insert all check before the assignment to the actual dst.
  8622. IR::Instr * insertBeforeInstr = instr->m_next;
  8623. Assert(insertBeforeInstr->m_opcode == Js::OpCode::MOV);
  8624. #ifdef _M_IX86
  8625. Assert(insertBeforeInstr->GetSrc1()->AsRegOpnd()->GetReg() == RegEAX);
  8626. #else
  8627. Assert(insertBeforeInstr->GetSrc1()->AsRegOpnd()->GetReg() == RegRAX);
  8628. #endif
  8629. // Jump to bailout if the reminder is not 0 (not int result)
  8630. this->m_lowerer->InsertTestBranch(reminderOpnd, reminderOpnd, Js::OpCode::BrNeq_A, bailOutLabel, insertBeforeInstr);
  8631. return insertBeforeInstr;
  8632. }
  8633. void
  8634. LowererMD::LowerTypeof(IR::Instr * typeOfInstr)
  8635. {
  8636. Func * func = typeOfInstr->m_func;
  8637. IR::Opnd * src1 = typeOfInstr->GetSrc1();
  8638. IR::Opnd * dst = typeOfInstr->GetDst();
  8639. Assert(src1->IsRegOpnd() && dst->IsRegOpnd());
  8640. IR::LabelInstr * helperLabel = IR::LabelInstr::New(Js::OpCode::Label, func, true);
  8641. IR::LabelInstr * taggedIntLabel = IR::LabelInstr::New(Js::OpCode::Label, func);
  8642. IR::LabelInstr * doneLabel = IR::LabelInstr::New(Js::OpCode::Label, func);
  8643. // MOV typeDisplayStringsArray, &javascriptLibrary->typeDisplayStrings
  8644. IR::RegOpnd * typeDisplayStringsArrayOpnd = IR::RegOpnd::New(TyMachPtr, func);
  8645. m_lowerer->InsertMove(typeDisplayStringsArrayOpnd, IR::AddrOpnd::New((BYTE*)m_func->GetScriptContextInfo()->GetLibraryAddr() + Js::JavascriptLibrary::GetTypeDisplayStringsOffset(), IR::AddrOpndKindConstantAddress, this->m_func), typeOfInstr);
  8646. GenerateObjectTest(src1, typeOfInstr, taggedIntLabel);
  8647. // MOV typeId, TypeIds_Object
  8648. // MOV typeRegOpnd, [src1 + offset(Type)]
  8649. // MOV objTypeId, [typeRegOpnd + offsetof(typeId)]
  8650. // CMP objTypeId, TypeIds_Limit /*external object test*/
  8651. // CMOVB typeId, objTypeId
  8652. // TEST [typeRegOpnd + offsetof(flags)], TypeFlagMask_IsFalsy /*test for falsy*/
  8653. // CMOVNE typeId, TypeIds_Undefined
  8654. // MOV dst, typeDisplayStrings[typeId]
  8655. // TEST dst, dst
  8656. // JE $helper
  8657. // JMP $done
  8658. IR::RegOpnd * typeIdOpnd = IR::RegOpnd::New(TyUint32, func);
  8659. m_lowerer->InsertMove(typeIdOpnd, IR::IntConstOpnd::New(Js::TypeIds_Object, TyUint32, func), typeOfInstr);
  8660. IR::RegOpnd * typeRegOpnd = IR::RegOpnd::New(TyMachReg, func);
  8661. m_lowerer->InsertMove(typeRegOpnd,
  8662. IR::IndirOpnd::New(src1->AsRegOpnd(), Js::RecyclableObject::GetOffsetOfType(), TyMachReg, func),
  8663. typeOfInstr);
  8664. IR::RegOpnd * objTypeIdOpnd = IR::RegOpnd::New(TyUint32, func);
  8665. m_lowerer->InsertMove(objTypeIdOpnd, IR::IndirOpnd::New(typeRegOpnd, Js::Type::GetOffsetOfTypeId(), TyInt32, func), typeOfInstr);
  8666. m_lowerer->InsertCompare(objTypeIdOpnd, IR::IntConstOpnd::New(Js::TypeIds_Limit, TyUint32, func), typeOfInstr);
  8667. InsertCmovCC(Js::OpCode::CMOVB, typeIdOpnd, objTypeIdOpnd, typeOfInstr);
  8668. // Insert MOV reg, 0 before the TEST because MOV reg, 0 will be peeped to XOR reg, reg and that may affect the zero flags that CMOVE depends on
  8669. IR::RegOpnd* typeIdUndefinedOpnd = IR::RegOpnd::New(TyUint32, func);
  8670. m_lowerer->InsertMove(typeIdUndefinedOpnd, IR::IntConstOpnd::New(Js::TypeIds_Undefined, TyUint32, func), typeOfInstr);
  8671. IR::Opnd *flagsOpnd = IR::IndirOpnd::New(typeRegOpnd, Js::Type::GetOffsetOfFlags(), TyInt32, this->m_func);
  8672. m_lowerer->InsertTest(flagsOpnd, IR::IntConstOpnd::New(TypeFlagMask_IsFalsy, TyInt32, this->m_func), typeOfInstr);
  8673. InsertCmovCC(Js::OpCode::CMOVNE, typeIdOpnd, typeIdUndefinedOpnd, typeOfInstr);
  8674. if (dst->IsEqual(src1))
  8675. {
  8676. ChangeToAssign(typeOfInstr->HoistSrc1(Js::OpCode::Ld_A));
  8677. }
  8678. m_lowerer->InsertMove(dst, IR::IndirOpnd::New(typeDisplayStringsArrayOpnd, typeIdOpnd, this->GetDefaultIndirScale(), TyMachPtr, func), typeOfInstr);
  8679. m_lowerer->InsertTestBranch(dst, dst, Js::OpCode::BrEq_A, helperLabel, typeOfInstr);
  8680. m_lowerer->InsertBranch(Js::OpCode::Br, doneLabel, typeOfInstr);
  8681. // $taggedInt:
  8682. // MOV dst, typeDisplayStrings[TypeIds_Number]
  8683. // JMP $done
  8684. typeOfInstr->InsertBefore(taggedIntLabel);
  8685. m_lowerer->InsertMove(dst, IR::IndirOpnd::New(typeDisplayStringsArrayOpnd, Js::TypeIds_Number * sizeof(Js::Var), TyMachPtr, func), typeOfInstr);
  8686. m_lowerer->InsertBranch(Js::OpCode::Br, doneLabel, typeOfInstr);
  8687. // $helper
  8688. // CALL OP_TypeOf
  8689. // $done
  8690. typeOfInstr->InsertBefore(helperLabel);
  8691. typeOfInstr->InsertAfter(doneLabel);
  8692. m_lowerer->LowerUnaryHelperMem(typeOfInstr, IR::HelperOp_Typeof);
  8693. }
  8694. IR::Instr*
  8695. LowererMD::InsertCmovCC(const Js::OpCode opCode, IR::Opnd * dst, IR::Opnd* src1, IR::Instr* insertBeforeInstr, bool postRegAlloc)
  8696. {
  8697. Assert(opCode > Js::OpCode::MDStart);
  8698. Func* func = insertBeforeInstr->m_func;
  8699. IR::Opnd* src2 = nullptr;
  8700. if (!postRegAlloc)
  8701. {
  8702. src2 = src1;
  8703. src1 = dst;
  8704. }
  8705. IR::Instr * instr = IR::Instr::New(opCode, dst, src1, src2, func);
  8706. insertBeforeInstr->InsertBefore(instr);
  8707. LowererMD::Legalize(instr);
  8708. return instr;
  8709. }