LowerMDShared.cpp 328 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "BackEnd.h"
  6. #include "Language\JavascriptFunctionArgIndex.h"
  7. #include "Types\DynamicObjectEnumerator.h"
  8. #include "Types\DynamicObjectSnapshotEnumerator.h"
  9. #include "Types\DynamicObjectSnapshotEnumeratorWPCache.h"
  10. #include "Library\ForInObjectEnumerator.h"
  11. const Js::OpCode LowererMD::MDUncondBranchOpcode = Js::OpCode::JMP;
  12. const Js::OpCode LowererMD::MDTestOpcode = Js::OpCode::TEST;
  13. const Js::OpCode LowererMD::MDOrOpcode = Js::OpCode::OR;
  14. const Js::OpCode LowererMD::MDXorOpcode = Js::OpCode::XOR;
  15. #if _M_X64
  16. const Js::OpCode LowererMD::MDMovUint64ToFloat64Opcode = Js::OpCode::MOVQ;
  17. #endif
  18. const Js::OpCode LowererMD::MDOverflowBranchOpcode = Js::OpCode::JO;
  19. const Js::OpCode LowererMD::MDNotOverflowBranchOpcode = Js::OpCode::JNO;
  20. const Js::OpCode LowererMD::MDConvertFloat32ToFloat64Opcode = Js::OpCode::CVTSS2SD;
  21. const Js::OpCode LowererMD::MDConvertFloat64ToFloat32Opcode = Js::OpCode::CVTSD2SS;
  22. const Js::OpCode LowererMD::MDCallOpcode = Js::OpCode::CALL;
  23. const Js::OpCode LowererMD::MDImulOpcode = Js::OpCode::IMUL2;
  24. //
  25. // Static utility fn()
  26. //
  27. bool
  28. LowererMD::IsAssign(IR::Instr *instr)
  29. {
  30. return instr->GetDst() && instr->m_opcode == LowererMDArch::GetAssignOp(instr->GetDst()->GetType());
  31. }
  32. ///----------------------------------------------------------------------------
  33. ///
  34. /// LowererMD::IsCall
  35. ///
  36. ///----------------------------------------------------------------------------
  37. bool
  38. LowererMD::IsCall(IR::Instr *instr)
  39. {
  40. return instr->m_opcode == Js::OpCode::CALL;
  41. }
  42. ///----------------------------------------------------------------------------
  43. ///
  44. /// LowererMD::IsUnconditionalBranch
  45. ///
  46. ///----------------------------------------------------------------------------
  47. bool
  48. LowererMD::IsUnconditionalBranch(const IR::Instr *instr)
  49. {
  50. return (instr->m_opcode == Js::OpCode::JMP);
  51. }
  52. // GenerateMemRef: Return an opnd that can be used to access the given address.
  53. IR::Opnd *
  54. LowererMD::GenerateMemRef(void *addr, IRType type, IR::Instr *instr, bool dontEncode)
  55. {
  56. return IR::MemRefOpnd::New(addr, type, this->m_func);
  57. }
  58. ///----------------------------------------------------------------------------
  59. ///
  60. /// LowererMD::InvertBranch
  61. ///
  62. ///----------------------------------------------------------------------------
  63. void
  64. LowererMD::InvertBranch(IR::BranchInstr *branchInstr)
  65. {
  66. switch (branchInstr->m_opcode)
  67. {
  68. case Js::OpCode::JA:
  69. branchInstr->m_opcode = Js::OpCode::JBE;
  70. break;
  71. case Js::OpCode::JAE:
  72. branchInstr->m_opcode = Js::OpCode::JB;
  73. break;
  74. case Js::OpCode::JB:
  75. branchInstr->m_opcode = Js::OpCode::JAE;
  76. break;
  77. case Js::OpCode::JBE:
  78. branchInstr->m_opcode = Js::OpCode::JA;
  79. break;
  80. case Js::OpCode::JEQ:
  81. branchInstr->m_opcode = Js::OpCode::JNE;
  82. break;
  83. case Js::OpCode::JNE:
  84. branchInstr->m_opcode = Js::OpCode::JEQ;
  85. break;
  86. case Js::OpCode::JGE:
  87. branchInstr->m_opcode = Js::OpCode::JLT;
  88. break;
  89. case Js::OpCode::JGT:
  90. branchInstr->m_opcode = Js::OpCode::JLE;
  91. break;
  92. case Js::OpCode::JLT:
  93. branchInstr->m_opcode = Js::OpCode::JGE;
  94. break;
  95. case Js::OpCode::JLE:
  96. branchInstr->m_opcode = Js::OpCode::JGT;
  97. break;
  98. case Js::OpCode::JO:
  99. branchInstr->m_opcode = Js::OpCode::JNO;
  100. break;
  101. case Js::OpCode::JNO:
  102. branchInstr->m_opcode = Js::OpCode::JO;
  103. break;
  104. case Js::OpCode::JP:
  105. branchInstr->m_opcode = Js::OpCode::JNP;
  106. break;
  107. case Js::OpCode::JNP:
  108. branchInstr->m_opcode = Js::OpCode::JP;
  109. break;
  110. case Js::OpCode::JSB:
  111. branchInstr->m_opcode = Js::OpCode::JNSB;
  112. break;
  113. case Js::OpCode::JNSB:
  114. branchInstr->m_opcode = Js::OpCode::JSB;
  115. break;
  116. default:
  117. AssertMsg(UNREACHED, "JCC missing in InvertBranch()");
  118. }
  119. }
  120. void
  121. LowererMD::ReverseBranch(IR::BranchInstr *branchInstr)
  122. {
  123. switch (branchInstr->m_opcode)
  124. {
  125. case Js::OpCode::JA:
  126. branchInstr->m_opcode = Js::OpCode::JB;
  127. break;
  128. case Js::OpCode::JAE:
  129. branchInstr->m_opcode = Js::OpCode::JBE;
  130. break;
  131. case Js::OpCode::JB:
  132. branchInstr->m_opcode = Js::OpCode::JA;
  133. break;
  134. case Js::OpCode::JBE:
  135. branchInstr->m_opcode = Js::OpCode::JAE;
  136. break;
  137. case Js::OpCode::JGE:
  138. branchInstr->m_opcode = Js::OpCode::JLE;
  139. break;
  140. case Js::OpCode::JGT:
  141. branchInstr->m_opcode = Js::OpCode::JLT;
  142. break;
  143. case Js::OpCode::JLT:
  144. branchInstr->m_opcode = Js::OpCode::JGT;
  145. break;
  146. case Js::OpCode::JLE:
  147. branchInstr->m_opcode = Js::OpCode::JGE;
  148. break;
  149. case Js::OpCode::JEQ:
  150. case Js::OpCode::JNE:
  151. case Js::OpCode::JO:
  152. case Js::OpCode::JNO:
  153. case Js::OpCode::JP:
  154. case Js::OpCode::JNP:
  155. case Js::OpCode::JSB:
  156. case Js::OpCode::JNSB:
  157. break;
  158. default:
  159. AssertMsg(UNREACHED, "JCC missing in ReverseBranch()");
  160. }
  161. }
  162. IR::Instr *
  163. LowererMD::LowerCallHelper(IR::Instr *instrCall)
  164. {
  165. IR::Opnd *argOpnd = instrCall->UnlinkSrc2();
  166. IR::Instr *prevInstr = nullptr;
  167. IR::JnHelperMethod helperMethod = instrCall->GetSrc1()->AsHelperCallOpnd()->m_fnHelper;
  168. instrCall->FreeSrc1();
  169. #ifndef _M_X64
  170. prevInstr = ChangeToHelperCall(instrCall, helperMethod);
  171. #endif
  172. while (argOpnd)
  173. {
  174. Assert(argOpnd->IsRegOpnd());
  175. IR::RegOpnd *regArg = argOpnd->AsRegOpnd();
  176. Assert(regArg->m_sym->m_isSingleDef);
  177. IR::Instr *instrArg = regArg->m_sym->m_instrDef;
  178. Assert(instrArg->m_opcode == Js::OpCode::ArgOut_A);
  179. prevInstr = LoadHelperArgument(instrArg, instrArg->UnlinkSrc1());
  180. regArg->Free(this->m_func);
  181. argOpnd = instrArg->GetSrc2();
  182. if (argOpnd)
  183. {
  184. instrArg->UnlinkSrc2();
  185. }
  186. if (prevInstr == instrArg)
  187. {
  188. prevInstr = prevInstr->m_prev;
  189. }
  190. instrArg->Remove();
  191. }
  192. prevInstr = m_lowerer->LoadScriptContext(prevInstr);
  193. #ifdef _M_X64
  194. FlipHelperCallArgsOrder();
  195. ChangeToHelperCall(instrCall, helperMethod);
  196. #else
  197. this->lowererMDArch.ResetHelperArgsCount();
  198. #endif
  199. // There might be ToVar in between the ArgOut, need to continue lower from the call still
  200. return instrCall;
  201. }
  202. //
  203. // forwarding functions
  204. //
  205. IR::Instr *
  206. LowererMD::LowerCall(IR::Instr * callInstr, Js::ArgSlot argCount)
  207. {
  208. return this->lowererMDArch.LowerCall(callInstr, argCount);
  209. }
  210. IR::Instr *
  211. LowererMD::LowerCallI(IR::Instr * callInstr, ushort callFlags, bool isHelper, IR::Instr * insertBeforeInstrForCFG)
  212. {
  213. return this->lowererMDArch.LowerCallI(callInstr, callFlags, isHelper, insertBeforeInstrForCFG);
  214. }
  215. IR::Instr *
  216. LowererMD::LowerAsmJsCallI(IR::Instr * callInstr)
  217. {
  218. return this->lowererMDArch.LowerAsmJsCallI(callInstr);
  219. }
  220. IR::Instr *
  221. LowererMD::LowerAsmJsCallE(IR::Instr * callInstr)
  222. {
  223. return this->lowererMDArch.LowerAsmJsCallE(callInstr);
  224. }
  225. IR::Instr *
  226. LowererMD::LowerAsmJsLdElemHelper(IR::Instr * callInstr)
  227. {
  228. return this->lowererMDArch.LowerAsmJsLdElemHelper(callInstr);
  229. }
  230. IR::Instr *
  231. LowererMD::LowerAsmJsStElemHelper(IR::Instr * callInstr)
  232. {
  233. return this->lowererMDArch.LowerAsmJsStElemHelper(callInstr);
  234. }
  235. IR::Instr *
  236. LowererMD::LowerCallPut(IR::Instr * callInstr)
  237. {
  238. int32 argCount = this->lowererMDArch.LowerCallArgs(callInstr, Js::CallFlags_None, 2);
  239. // load native entry point from script function into eax
  240. IR::Opnd * functionWrapOpnd = callInstr->UnlinkSrc1();
  241. AssertMsg(functionWrapOpnd->IsRegOpnd() && functionWrapOpnd->AsRegOpnd()->m_sym->IsStackSym(),
  242. "Expected call src to be stackSym");
  243. this->LoadHelperArgument(callInstr, functionWrapOpnd);
  244. this->m_lowerer->LoadScriptContext(callInstr);
  245. IR::HelperCallOpnd *helperCallOpnd = IR::HelperCallOpnd::New(IR::HelperOp_InvokePut, this->m_func);
  246. callInstr->SetSrc1(helperCallOpnd);
  247. return this->lowererMDArch.LowerCall(callInstr, argCount);
  248. }
  249. IR::Instr *
  250. LowererMD::LoadHelperArgument(IR::Instr * instr, IR::Opnd * opndArg)
  251. {
  252. return this->lowererMDArch.LoadHelperArgument(instr, opndArg);
  253. }
  254. IR::Instr *
  255. LowererMD::LoadDoubleHelperArgument(IR::Instr * instr, IR::Opnd * opndArg)
  256. {
  257. return this->lowererMDArch.LoadDoubleHelperArgument(instr, opndArg);
  258. }
  259. IR::Instr *
  260. LowererMD::LoadFloatHelperArgument(IR::Instr * instr, IR::Opnd * opndArg)
  261. {
  262. return this->lowererMDArch.LoadFloatHelperArgument(instr, opndArg);
  263. }
  264. IR::Instr *
  265. LowererMD::LowerEntryInstr(IR::EntryInstr * entryInstr)
  266. {
  267. return this->lowererMDArch.LowerEntryInstr(entryInstr);
  268. }
  269. IR::Instr *
  270. LowererMD::LowerExitInstr(IR::ExitInstr * exitInstr)
  271. {
  272. return this->lowererMDArch.LowerExitInstr(exitInstr);
  273. }
  274. IR::Instr *
  275. LowererMD::LowerEntryInstrAsmJs(IR::EntryInstr * entryInstr)
  276. {
  277. return this->lowererMDArch.LowerEntryInstrAsmJs(entryInstr);
  278. }
  279. IR::Instr *
  280. LowererMD::LowerExitInstrAsmJs(IR::ExitInstr * exitInstr)
  281. {
  282. return this->lowererMDArch.LowerExitInstrAsmJs(exitInstr);
  283. }
  284. IR::Instr *
  285. LowererMD::LoadNewScObjFirstArg(IR::Instr * instr, IR::Opnd * dst, ushort extraArgs)
  286. {
  287. return this->lowererMDArch.LoadNewScObjFirstArg(instr, dst, extraArgs);
  288. }
  289. IR::Instr *
  290. LowererMD::LowerTry(IR::Instr *tryInstr, IR::JnHelperMethod helperMethod)
  291. {
  292. // Mark the entry to the try
  293. IR::Instr *instr = tryInstr->GetNextRealInstrOrLabel();
  294. AssertMsg(instr->IsLabelInstr(), "No label at the entry to a try?");
  295. IR::LabelInstr *tryAddr = instr->AsLabelInstr();
  296. // Arg 5: ScriptContext
  297. this->m_lowerer->LoadScriptContext(tryAddr);
  298. if (tryInstr->m_opcode == Js::OpCode::TryCatch)
  299. {
  300. // Arg 4 : hasBailedOutOffset
  301. IR::Opnd * hasBailedOutOffset = IR::IntConstOpnd::New(this->m_func->m_hasBailedOutSym->m_offset, TyInt32, this->m_func);
  302. this->LoadHelperArgument(tryAddr, hasBailedOutOffset);
  303. }
  304. #ifdef _M_X64
  305. // Arg: args size
  306. IR::RegOpnd *argsSizeOpnd = IR::RegOpnd::New(TyMachReg, m_func);
  307. tryAddr->InsertBefore(IR::Instr::New(Js::OpCode::LdArgSize, argsSizeOpnd, this->m_func));
  308. this->LoadHelperArgument(tryAddr, argsSizeOpnd);
  309. // Arg: spill size
  310. IR::RegOpnd *spillSizeOpnd = IR::RegOpnd::New(TyMachReg, m_func);
  311. tryAddr->InsertBefore(IR::Instr::New(Js::OpCode::LdSpillSize, spillSizeOpnd, this->m_func));
  312. this->LoadHelperArgument(tryAddr, spillSizeOpnd);
  313. #endif
  314. // Arg 3: frame pointer
  315. IR::RegOpnd *ebpOpnd = IR::RegOpnd::New(nullptr, lowererMDArch.GetRegBlockPointer(), TyMachReg, this->m_func);
  316. this->LoadHelperArgument(tryAddr, ebpOpnd);
  317. // Arg 2: handler address
  318. IR::LabelInstr *helperAddr = tryInstr->AsBranchInstr()->GetTarget();
  319. this->LoadHelperArgument(tryAddr, IR::LabelOpnd::New(helperAddr, this->m_func));
  320. // Arg 1: try address
  321. this->LoadHelperArgument(tryAddr, IR::LabelOpnd::New(tryAddr, this->m_func));
  322. // Call the helper
  323. IR::RegOpnd *continuationAddr =
  324. IR::RegOpnd::New(StackSym::New(TyMachReg, this->m_func), lowererMDArch.GetRegReturn(TyMachReg), TyMachReg, this->m_func);
  325. IR::Instr *callInstr = IR::Instr::New(
  326. Js::OpCode::Call, continuationAddr, IR::HelperCallOpnd::New(helperMethod, this->m_func), this->m_func);
  327. tryAddr->InsertBefore(callInstr);
  328. this->LowerCall(callInstr, 0);
  329. #ifdef _M_X64
  330. {
  331. // Emit some instruction to separate the CALL from the JMP following it. The OS stack unwinder
  332. // mistakes the JMP for the start of the epilog otherwise.
  333. IR::Instr *nop = IR::Instr::New(Js::OpCode::NOP, m_func);
  334. tryAddr->InsertBefore(nop);
  335. }
  336. #endif
  337. // Jump to the continuation address supplied by the helper
  338. IR::BranchInstr *branchInstr = IR::MultiBranchInstr::New(Js::OpCode::JMP, continuationAddr, this->m_func);
  339. tryAddr->InsertBefore(branchInstr);
  340. return tryInstr->m_prev;
  341. }
  342. IR::Instr *
  343. LowererMD::LowerLeave(IR::Instr *leaveInstr, IR::LabelInstr *targetInstr, bool fromFinalLower, bool isOrphanedLeave)
  344. {
  345. if (isOrphanedLeave)
  346. {
  347. Assert(this->m_func->IsLoopBodyInTry());
  348. leaveInstr->m_opcode = Js::OpCode::JMP;
  349. return leaveInstr->m_prev;
  350. }
  351. IR::Instr *instrPrev = leaveInstr->m_prev;
  352. IR::LabelOpnd *labelOpnd = IR::LabelOpnd::New(targetInstr, this->m_func);
  353. lowererMDArch.LowerEHRegionReturn(leaveInstr, labelOpnd);
  354. if (fromFinalLower)
  355. {
  356. instrPrev = leaveInstr->m_prev; // Need to lower LdArgSize and LdSpillSize
  357. }
  358. leaveInstr->Remove();
  359. return instrPrev;
  360. }
  361. IR::Instr *
  362. LowererMD::LowerEHRegionReturn(IR::Instr * insertBeforeInstr, IR::Opnd * targetOpnd)
  363. {
  364. return lowererMDArch.LowerEHRegionReturn(insertBeforeInstr, targetOpnd);
  365. }
  366. IR::Instr *
  367. LowererMD::LowerLeaveNull(IR::Instr *finallyEndInstr)
  368. {
  369. IR::Instr *instrPrev = finallyEndInstr->m_prev;
  370. IR::Instr *instr = nullptr;
  371. // Return a null continuation address to the helper: execution will resume at the point determined by the try
  372. // or the exception handler.
  373. IR::RegOpnd *retReg = IR::RegOpnd::New(StackSym::New(TyMachReg,this->m_func), lowererMDArch.GetRegReturn(TyMachReg), TyMachReg, this->m_func);
  374. instr = IR::Instr::New(Js::OpCode::XOR, retReg, this->m_func);
  375. IR::RegOpnd *eaxOpnd = IR::RegOpnd::New(nullptr, lowererMDArch.GetRegReturn(TyMachReg), TyMachReg, this->m_func);
  376. instr->SetSrc1(eaxOpnd);
  377. instr->SetSrc2(eaxOpnd);
  378. finallyEndInstr->InsertBefore(instr);
  379. #if _M_X64
  380. {
  381. // amd64_ReturnFromCallWithFakeFrame expects to find the spill size and args size
  382. // in r8 and r9.
  383. // MOV r8, spillSize
  384. IR::Instr *movR8 = IR::Instr::New(Js::OpCode::LdSpillSize,
  385. IR::RegOpnd::New(nullptr, RegR8, TyMachReg, m_func),
  386. m_func);
  387. finallyEndInstr->InsertBefore(movR8);
  388. // MOV r9, argsSize
  389. IR::Instr *movR9 = IR::Instr::New(Js::OpCode::LdArgSize,
  390. IR::RegOpnd::New(nullptr, RegR9, TyMachReg, m_func),
  391. m_func);
  392. finallyEndInstr->InsertBefore(movR9);
  393. IR::Opnd *targetOpnd = IR::RegOpnd::New(nullptr, RegRCX, TyMachReg, m_func);
  394. IR::Instr *movTarget = IR::Instr::New(Js::OpCode::MOV,
  395. targetOpnd,
  396. IR::HelperCallOpnd::New(IR::HelperOp_ReturnFromCallWithFakeFrame, m_func),
  397. m_func);
  398. finallyEndInstr->InsertBefore(movTarget);
  399. IR::Instr *push = IR::Instr::New(Js::OpCode::PUSH, m_func);
  400. push->SetSrc1(targetOpnd);
  401. finallyEndInstr->InsertBefore(push);
  402. }
  403. #endif
  404. IR::IntConstOpnd *intSrc = IR::IntConstOpnd::New(0, TyInt32, this->m_func);
  405. instr = IR::Instr::New(Js::OpCode::RET, this->m_func);
  406. instr->SetSrc1(intSrc);
  407. instr->SetSrc2(retReg);
  408. finallyEndInstr->InsertBefore(instr);
  409. finallyEndInstr->Remove();
  410. return instrPrev;
  411. }
  412. ///----------------------------------------------------------------------------
  413. ///
  414. /// LowererMD::Init
  415. ///
  416. ///----------------------------------------------------------------------------
  417. void
  418. LowererMD::Init(Lowerer *lowerer)
  419. {
  420. m_lowerer = lowerer;
  421. this->lowererMDArch.Init(this);
  422. Simd128InitOpcodeMap();
  423. }
  424. ///----------------------------------------------------------------------------
  425. ///
  426. /// LowererMD::LoadInputParamCount
  427. ///
  428. /// Load the passed-in parameter count from the appropriate EBP slot.
  429. ///
  430. ///----------------------------------------------------------------------------
  431. IR::Instr *
  432. LowererMD::LoadInputParamCount(IR::Instr * instrInsert, int adjust, bool needFlags)
  433. {
  434. IR::Instr * instr;
  435. IR::RegOpnd * dstOpnd;
  436. IR::SymOpnd * srcOpnd;
  437. srcOpnd = Lowerer::LoadCallInfo(instrInsert);
  438. dstOpnd = IR::RegOpnd::New(StackSym::New(TyMachReg, this->m_func), TyMachReg, this->m_func);
  439. instr = IR::Instr::New(Js::OpCode::MOV, dstOpnd, srcOpnd, this->m_func);
  440. instrInsert->InsertBefore(instr);
  441. // Copy the callinfo before masking off the param count
  442. Assert(Js::CallInfo::ksizeofCount == 24);
  443. // Mask off call flags from callinfo
  444. instr = IR::Instr::New(Js::OpCode::AND, dstOpnd, dstOpnd,
  445. IR::IntConstOpnd::New((Js::CallFlags_ExtraArg << Js::CallInfo::ksizeofCount) | 0x00FFFFFF, TyUint32, this->m_func, true), this->m_func);
  446. instrInsert->InsertBefore(instr);
  447. // Shift and mask the "calling eval" bit and subtract it from the incoming count.
  448. // ("Calling eval" means the last param is the frame display, which only the eval built-in should see.)
  449. 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);
  450. instrInsert->InsertBefore(instr);
  451. instr = IR::Instr::New(Js::OpCode::SBB, dstOpnd, dstOpnd, IR::IntConstOpnd::New(-adjust, TyInt32, this->m_func), this->m_func);
  452. instrInsert->InsertBefore(instr);
  453. return instr;
  454. }
  455. IR::Instr *
  456. LowererMD::LoadStackArgPtr(IR::Instr * instr)
  457. {
  458. if (this->m_func->IsLoopBody())
  459. {
  460. // Get the first user param from the interpreter frame instance that was passed in.
  461. // These args don't include the func object and callinfo; we just need to advance past "this".
  462. // t1 = MOV [prm1 + m_inParams]
  463. // dst = LEA &[t1 + sizeof(var)]
  464. Assert(this->m_func->m_loopParamSym);
  465. IR::RegOpnd *baseOpnd = IR::RegOpnd::New(this->m_func->m_loopParamSym, TyMachReg, this->m_func);
  466. size_t offset = Js::InterpreterStackFrame::GetOffsetOfInParams();
  467. IR::IndirOpnd *indirOpnd = IR::IndirOpnd::New(baseOpnd, (int32)offset, TyMachReg, this->m_func);
  468. IR::RegOpnd *tmpOpnd = IR::RegOpnd::New(TyMachReg, this->m_func);
  469. IR::Instr *instrLdParams = IR::Instr::New(Js::OpCode::MOV, tmpOpnd, indirOpnd, this->m_func);
  470. instr->InsertBefore(instrLdParams);
  471. indirOpnd = IR::IndirOpnd::New(tmpOpnd, sizeof(Js::Var), TyMachReg, this->m_func);
  472. instr->SetSrc1(indirOpnd);
  473. instr->m_opcode = Js::OpCode::LEA;
  474. return instr->m_prev;
  475. }
  476. else
  477. {
  478. return this->lowererMDArch.LoadStackArgPtr(instr);
  479. }
  480. }
  481. IR::Instr *
  482. LowererMD::LoadArgumentsFromFrame(IR::Instr * instr)
  483. {
  484. if (this->m_func->IsLoopBody())
  485. {
  486. // Get the arguments ptr from the interpreter frame instance that was passed in.
  487. Assert(this->m_func->m_loopParamSym);
  488. IR::RegOpnd *baseOpnd = IR::RegOpnd::New(this->m_func->m_loopParamSym, TyMachReg, this->m_func);
  489. int32 offset = (int32)Js::InterpreterStackFrame::GetOffsetOfArguments();
  490. instr->SetSrc1(IR::IndirOpnd::New(baseOpnd, offset, TyMachReg, this->m_func));
  491. }
  492. else
  493. {
  494. instr->SetSrc1(this->CreateStackArgumentsSlotOpnd());
  495. }
  496. instr->m_opcode = Js::OpCode::MOV;
  497. return instr->m_prev;
  498. }
  499. // load argument count as I4
  500. IR::Instr *
  501. LowererMD::LoadArgumentCount(IR::Instr * instr)
  502. {
  503. if (this->m_func->IsLoopBody())
  504. {
  505. // Pull the arg count from the interpreter frame instance that was passed in.
  506. // (The callinfo in the loop body's frame just shows the single parameter, the interpreter frame.)
  507. Assert(this->m_func->m_loopParamSym);
  508. IR::RegOpnd *baseOpnd = IR::RegOpnd::New(this->m_func->m_loopParamSym, TyMachReg, this->m_func);
  509. size_t offset = Js::InterpreterStackFrame::GetOffsetOfInSlotsCount();
  510. instr->SetSrc1(IR::IndirOpnd::New(baseOpnd, (int32)offset, TyInt32, this->m_func));
  511. }
  512. else
  513. {
  514. StackSym *sym = StackSym::New(TyVar, this->m_func);
  515. this->m_func->SetArgOffset(sym, (Js::JavascriptFunctionArgIndex_CallInfo - Js::JavascriptFunctionArgIndex_Frame) * sizeof(Js::Var));
  516. instr->SetSrc1(IR::SymOpnd::New(sym, TyMachReg, this->m_func));
  517. }
  518. instr->m_opcode = Js::OpCode::MOV;
  519. return instr->m_prev;
  520. }
  521. IR::Instr *
  522. LowererMD::LoadHeapArguments(IR::Instr * instrArgs, bool force, IR::Opnd* opndInputParamCount)
  523. {
  524. return this->lowererMDArch.LoadHeapArguments(instrArgs, force, opndInputParamCount);
  525. }
  526. IR::Instr *
  527. LowererMD::LoadHeapArgsCached(IR::Instr * instrArgs)
  528. {
  529. return this->lowererMDArch.LoadHeapArgsCached(instrArgs);
  530. }
  531. IR::Instr *
  532. LowererMD::LoadFuncExpression(IR::Instr * instrFuncExpr)
  533. {
  534. return this->lowererMDArch.LoadFuncExpression(instrFuncExpr);
  535. }
  536. ///----------------------------------------------------------------------------
  537. ///
  538. /// LowererMD::ChangeToHelperCall
  539. ///
  540. /// Change the current instruction to a call to the given helper.
  541. ///
  542. ///----------------------------------------------------------------------------
  543. IR::Instr *
  544. LowererMD::ChangeToHelperCall(IR::Instr * callInstr, IR::JnHelperMethod helperMethod, IR::LabelInstr *labelBailOut,
  545. IR::Opnd *opndBailOutArg, IR::PropertySymOpnd *propSymOpnd, bool isHelperContinuation)
  546. {
  547. IR::Instr * bailOutInstr = callInstr;
  548. if (callInstr->HasBailOutInfo())
  549. {
  550. if (callInstr->GetBailOutKind() == IR::BailOutExpectingObject ||
  551. callInstr->GetBailOutKind() == IR::BailOutOnNotPrimitive)
  552. {
  553. callInstr = IR::Instr::New(callInstr->m_opcode, callInstr->m_func);
  554. bailOutInstr->TransferTo(callInstr);
  555. bailOutInstr->InsertBefore(callInstr);
  556. IR::BailOutKind bailOutKind = bailOutInstr->GetBailOutKind();
  557. bailOutInstr->m_opcode = bailOutKind == IR::BailOutExpectingObject ? Js::OpCode::BailOnNotObject : Js::OpCode::BailOnNotPrimitive;
  558. bailOutInstr->SetSrc1(opndBailOutArg);
  559. }
  560. else
  561. {
  562. bailOutInstr = this->m_lowerer->SplitBailOnImplicitCall(callInstr);
  563. }
  564. }
  565. callInstr->m_opcode = Js::OpCode::CALL;
  566. IR::HelperCallOpnd *helperCallOpnd = Lowerer::CreateHelperCallOpnd(helperMethod, this->lowererMDArch.GetHelperArgsCount(), m_func);
  567. if (helperCallOpnd->IsDiagHelperCallOpnd())
  568. {
  569. // Load arguments for the wrapper.
  570. this->LoadHelperArgument(callInstr, IR::AddrOpnd::New((Js::Var)IR::GetMethodOriginalAddress(helperMethod), IR::AddrOpndKindDynamicMisc, m_func));
  571. this->m_lowerer->LoadScriptContext(callInstr);
  572. }
  573. callInstr->SetSrc1(helperCallOpnd);
  574. IR::Instr * instrRet = this->lowererMDArch.LowerCall(callInstr, 0);
  575. if (bailOutInstr != callInstr)
  576. {
  577. // The bailout needs to be lowered after we lower the helper call because the helper argument
  578. // has already been loaded. We need to drain them on AMD64 before starting another helper call
  579. if (bailOutInstr->m_opcode == Js::OpCode::BailOnNotObject)
  580. {
  581. this->m_lowerer->LowerBailOnNotObject(bailOutInstr, nullptr, labelBailOut);
  582. }
  583. else if (bailOutInstr->m_opcode == Js::OpCode::BailOnNotPrimitive)
  584. {
  585. this->m_lowerer->LowerBailOnTrue(bailOutInstr, labelBailOut);
  586. }
  587. else if (bailOutInstr->m_opcode == Js::OpCode::BailOut)
  588. {
  589. this->m_lowerer->GenerateBailOut(bailOutInstr, nullptr, labelBailOut);
  590. }
  591. else
  592. {
  593. this->m_lowerer->LowerBailOnEqualOrNotEqual(bailOutInstr, nullptr, labelBailOut, propSymOpnd, isHelperContinuation);
  594. }
  595. }
  596. return instrRet;
  597. }
  598. IR::Instr* LowererMD::ChangeToHelperCallMem(IR::Instr * instr, IR::JnHelperMethod helperMethod)
  599. {
  600. this->m_lowerer->LoadScriptContext(instr);
  601. return this->ChangeToHelperCall(instr, helperMethod);
  602. }
  603. ///----------------------------------------------------------------------------
  604. ///
  605. /// LowererMD::ChangeToAssign
  606. ///
  607. /// Change to a MOV.
  608. ///
  609. ///----------------------------------------------------------------------------
  610. IR::Instr *
  611. LowererMD::ChangeToAssign(IR::Instr * instr)
  612. {
  613. return ChangeToAssign(instr, instr->GetDst()->GetType());
  614. }
  615. IR::Instr *
  616. LowererMD::ChangeToAssign(IR::Instr * instr, IRType type)
  617. {
  618. Assert(!instr->HasBailOutInfo() || instr->GetBailOutKind() == IR::BailOutExpectingString);
  619. instr->m_opcode = LowererMDArch::GetAssignOp(type);
  620. Legalize(instr);
  621. return instr;
  622. }
  623. ///----------------------------------------------------------------------------
  624. ///
  625. /// LowererMD::ChangeToLea
  626. ///
  627. /// Change to an LEA.
  628. ///
  629. ///----------------------------------------------------------------------------
  630. IR::Instr *
  631. LowererMD::ChangeToLea(IR::Instr * instr)
  632. {
  633. Assert(instr);
  634. Assert(instr->GetDst());
  635. Assert(instr->GetDst()->IsRegOpnd());
  636. Assert(instr->GetSrc1());
  637. Assert(instr->GetSrc1()->IsIndirOpnd() || instr->GetSrc1()->IsSymOpnd());
  638. Assert(!instr->GetSrc2());
  639. instr->m_opcode = Js::OpCode::LEA;
  640. return instr;
  641. }
  642. ///----------------------------------------------------------------------------
  643. ///
  644. /// LowererMD::CreateAssign
  645. ///
  646. /// Create a MOV.
  647. ///
  648. ///----------------------------------------------------------------------------
  649. IR::Instr *
  650. LowererMD::CreateAssign(IR::Opnd *dst, IR::Opnd *src, IR::Instr *instrInsertPt)
  651. {
  652. return Lowerer::InsertMove(dst, src, instrInsertPt);
  653. }
  654. ///----------------------------------------------------------------------------
  655. ///
  656. /// LowererMD::LowerRet
  657. ///
  658. /// Lower Ret to "MOV EAX, src"
  659. /// The real RET is inserted at the exit of the function when emitting the
  660. /// epilog.
  661. ///
  662. ///----------------------------------------------------------------------------
  663. IR::Instr *
  664. LowererMD::LowerRet(IR::Instr * retInstr)
  665. {
  666. IR::RegOpnd * retReg;
  667. if (m_func->GetJnFunction()->GetIsAsmjsMode() && !m_func->IsLoopBody()) // for loop body ret is the bytecodeoffset
  668. {
  669. Js::AsmJsRetType asmType = m_func->GetJnFunction()->GetAsmJsFunctionInfo()->GetReturnType();
  670. IRType regType = TyInt32;
  671. if (asmType.which() == Js::AsmJsRetType::Double)
  672. {
  673. regType = TyFloat64;
  674. }
  675. else if (asmType.which() == Js::AsmJsRetType::Float)
  676. {
  677. regType = TyFloat32;
  678. }
  679. else if (asmType.which() == Js::AsmJsRetType::Signed || asmType.which() == Js::AsmJsRetType::Void)
  680. {
  681. regType = TyInt32;
  682. }
  683. else if (asmType.which() == Js::AsmJsRetType::Float32x4)
  684. {
  685. regType = TySimd128F4;
  686. }
  687. else if (asmType.which() == Js::AsmJsRetType::Int32x4)
  688. {
  689. regType = TySimd128I4;
  690. }
  691. else if (asmType.which() == Js::AsmJsRetType::Float64x2)
  692. {
  693. regType = TySimd128D2;
  694. }
  695. else
  696. {
  697. Assert(UNREACHED);
  698. }
  699. retReg = IR::RegOpnd::New(nullptr, lowererMDArch.GetRegReturnAsmJs(regType), regType, m_func);
  700. }
  701. else
  702. {
  703. retReg = IR::RegOpnd::New(nullptr, lowererMDArch.GetRegReturn(TyMachReg), TyMachReg, m_func);
  704. }
  705. retInstr->SetDst(retReg);
  706. return this->ChangeToAssign(retInstr);
  707. }
  708. ///----------------------------------------------------------------------------
  709. ///
  710. /// LowererMD::LowerUncondBranch
  711. ///
  712. ///----------------------------------------------------------------------------
  713. IR::Instr *
  714. LowererMD::LowerUncondBranch(IR::Instr * instr)
  715. {
  716. instr->m_opcode = Js::OpCode::JMP;
  717. return instr;
  718. }
  719. ///----------------------------------------------------------------------------
  720. ///
  721. /// LowererMD::LowerMultiBranch
  722. ///
  723. ///----------------------------------------------------------------------------
  724. IR::Instr *
  725. LowererMD::LowerMultiBranch(IR::Instr * instr)
  726. {
  727. return LowerUncondBranch(instr);
  728. }
  729. ///----------------------------------------------------------------------------
  730. ///
  731. /// LowererMD::LowerCondBranch
  732. ///
  733. ///----------------------------------------------------------------------------
  734. IR::Instr *
  735. LowererMD::LowerCondBranch(IR::Instr * instr)
  736. {
  737. AssertMsg(instr->GetSrc1() != nullptr, "Expected src opnds on conditional branch");
  738. Assert(!instr->HasBailOutInfo());
  739. IR::Opnd * opndSrc1 = instr->UnlinkSrc1();
  740. IR::Instr * instrPrev = nullptr;
  741. switch (instr->m_opcode)
  742. {
  743. case Js::OpCode::BrTrue_A:
  744. case Js::OpCode::BrFalse_A:
  745. case Js::OpCode::BrNotNull_A:
  746. case Js::OpCode::BrOnObject_A:
  747. case Js::OpCode::BrOnClassConstructor:
  748. Assert(!opndSrc1->IsFloat64());
  749. AssertMsg(instr->GetSrc2() == nullptr, "Expected 1 src on boolean branch");
  750. instrPrev = IR::Instr::New(Js::OpCode::TEST, this->m_func);
  751. instrPrev->SetSrc1(opndSrc1);
  752. instrPrev->SetSrc2(opndSrc1);
  753. instr->InsertBefore(instrPrev);
  754. if (instr->m_opcode != Js::OpCode::BrFalse_A)
  755. {
  756. instr->m_opcode = Js::OpCode::JNE;
  757. }
  758. else
  759. {
  760. instr->m_opcode = Js::OpCode::JEQ;
  761. }
  762. break;
  763. case Js::OpCode::BrOnEmpty:
  764. case Js::OpCode::BrOnNotEmpty:
  765. AssertMsg(0, "BrOnEmpty opcodes should not be passed to MD lowerer");
  766. break;
  767. default:
  768. IR::Opnd * opndSrc2 = instr->UnlinkSrc2();
  769. AssertMsg(opndSrc2 != nullptr, "Expected 2 src's on non-boolean branch");
  770. if (opndSrc1->IsFloat())
  771. {
  772. Assert(opndSrc1->GetType() == opndSrc2->GetType());
  773. instrPrev = IR::Instr::New(opndSrc1->IsFloat64() ? Js::OpCode::COMISD : Js::OpCode::COMISS, m_func);
  774. instrPrev->SetSrc1(opndSrc1);
  775. instrPrev->SetSrc2(opndSrc2);
  776. instr->InsertBefore(instrPrev);
  777. }
  778. else
  779. {
  780. // This check assumes src1 is a variable.
  781. if (opndSrc2->IsIntConstOpnd() && opndSrc2->AsIntConstOpnd()->GetValue() == 0)
  782. {
  783. instrPrev = IR::Instr::New(Js::OpCode::TEST, this->m_func);
  784. instrPrev->SetSrc1(opndSrc1);
  785. instrPrev->SetSrc2(opndSrc1);
  786. instr->InsertBefore(instrPrev);
  787. opndSrc2->Free(this->m_func);
  788. }
  789. else
  790. {
  791. instrPrev = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  792. //
  793. // For 32 bit arithmetic we copy them and set the size of operands to be 32 bits. This is
  794. // relevant only on AMD64.
  795. //
  796. opndSrc1 = instrPrev->SetSrc1(opndSrc1);
  797. opndSrc2 = instrPrev->SetSrc2(opndSrc2);
  798. instr->InsertBefore(instrPrev);
  799. LowererMD::Legalize(instrPrev);
  800. }
  801. }
  802. instr->m_opcode = LowererMD::MDBranchOpcode(instr->m_opcode);
  803. break;
  804. }
  805. return instrPrev;
  806. }
  807. ///----------------------------------------------------------------------------
  808. ///
  809. /// LowererMD::MDBranchOpcode
  810. ///
  811. /// Map HIR branch opcode to machine-dependent equivalent.
  812. ///
  813. ///----------------------------------------------------------------------------
  814. Js::OpCode
  815. LowererMD::MDBranchOpcode(Js::OpCode opcode)
  816. {
  817. switch (opcode)
  818. {
  819. case Js::OpCode::BrSrEq_A:
  820. case Js::OpCode::BrEq_A:
  821. case Js::OpCode::BrSrNotNeq_A:
  822. case Js::OpCode::BrNotNeq_A:
  823. case Js::OpCode::BrAddr_A:
  824. return Js::OpCode::JEQ;
  825. case Js::OpCode::BrSrNeq_A:
  826. case Js::OpCode::BrNeq_A:
  827. case Js::OpCode::BrSrNotEq_A:
  828. case Js::OpCode::BrNotEq_A:
  829. case Js::OpCode::BrNotAddr_A:
  830. return Js::OpCode::JNE;
  831. case Js::OpCode::BrLt_A:
  832. case Js::OpCode::BrNotGe_A:
  833. return Js::OpCode::JLT;
  834. case Js::OpCode::BrLe_A:
  835. case Js::OpCode::BrNotGt_A:
  836. return Js::OpCode::JLE;
  837. case Js::OpCode::BrGt_A:
  838. case Js::OpCode::BrNotLe_A:
  839. return Js::OpCode::JGT;
  840. case Js::OpCode::BrGe_A:
  841. case Js::OpCode::BrNotLt_A:
  842. return Js::OpCode::JGE;
  843. default:
  844. AssertMsg(0, "Branch opcode has no MD mapping");
  845. return opcode;
  846. }
  847. }
  848. Js::OpCode
  849. LowererMD::MDConvertFloat64ToInt32Opcode(const RoundMode roundMode)
  850. {
  851. switch (roundMode)
  852. {
  853. case RoundModeTowardZero:
  854. return Js::OpCode::CVTTSD2SI;
  855. case RoundModeTowardInteger:
  856. return Js::OpCode::Nop;
  857. case RoundModeHalfToEven:
  858. return Js::OpCode::CVTSD2SI;
  859. default:
  860. AssertMsg(0, "RoundMode has no MD mapping.");
  861. return Js::OpCode::Nop;
  862. }
  863. }
  864. Js::OpCode
  865. LowererMD::MDUnsignedBranchOpcode(Js::OpCode opcode)
  866. {
  867. switch (opcode)
  868. {
  869. case Js::OpCode::BrEq_A:
  870. case Js::OpCode::BrSrEq_A:
  871. case Js::OpCode::BrSrNotNeq_A:
  872. case Js::OpCode::BrNotNeq_A:
  873. case Js::OpCode::BrAddr_A:
  874. return Js::OpCode::JEQ;
  875. case Js::OpCode::BrNeq_A:
  876. case Js::OpCode::BrSrNeq_A:
  877. case Js::OpCode::BrSrNotEq_A:
  878. case Js::OpCode::BrNotEq_A:
  879. case Js::OpCode::BrNotAddr_A:
  880. return Js::OpCode::JNE;
  881. case Js::OpCode::BrLt_A:
  882. case Js::OpCode::BrNotGe_A:
  883. return Js::OpCode::JB;
  884. case Js::OpCode::BrLe_A:
  885. case Js::OpCode::BrNotGt_A:
  886. return Js::OpCode::JBE;
  887. case Js::OpCode::BrGt_A:
  888. case Js::OpCode::BrNotLe_A:
  889. return Js::OpCode::JA;
  890. case Js::OpCode::BrGe_A:
  891. case Js::OpCode::BrNotLt_A:
  892. return Js::OpCode::JAE;
  893. default:
  894. AssertMsg(0, "Branch opcode has no MD mapping");
  895. return opcode;
  896. }
  897. }
  898. Js::OpCode LowererMD::MDCompareWithZeroBranchOpcode(Js::OpCode opcode)
  899. {
  900. Assert(opcode == Js::OpCode::BrLt_A || opcode == Js::OpCode::BrGe_A);
  901. return opcode == Js::OpCode::BrLt_A ? Js::OpCode::JSB : Js::OpCode::JNSB;
  902. }
  903. void LowererMD::ChangeToAdd(IR::Instr *const instr, const bool needFlags)
  904. {
  905. Assert(instr);
  906. Assert(instr->GetDst());
  907. Assert(instr->GetSrc1());
  908. Assert(instr->GetSrc2());
  909. if(instr->GetDst()->IsFloat64())
  910. {
  911. Assert(instr->GetSrc1()->IsFloat64());
  912. Assert(instr->GetSrc2()->IsFloat64());
  913. Assert(!needFlags);
  914. instr->m_opcode = Js::OpCode::ADDSD;
  915. return;
  916. }
  917. else if (instr->GetDst()->IsFloat32())
  918. {
  919. Assert(instr->GetSrc1()->IsFloat32());
  920. Assert(instr->GetSrc2()->IsFloat32());
  921. Assert(!needFlags);
  922. instr->m_opcode = Js::OpCode::ADDSS;
  923. return;
  924. }
  925. instr->m_opcode = Js::OpCode::ADD;
  926. MakeDstEquSrc1(instr);
  927. // Prefer INC for add by one
  928. if(instr->GetDst()->IsEqual(instr->GetSrc1()) &&
  929. instr->GetSrc2()->IsIntConstOpnd() &&
  930. instr->GetSrc2()->AsIntConstOpnd()->GetValue() == 1 ||
  931. instr->GetDst()->IsEqual(instr->GetSrc2()) &&
  932. instr->GetSrc1()->IsIntConstOpnd() &&
  933. instr->GetSrc1()->AsIntConstOpnd()->GetValue() == 1)
  934. {
  935. if(instr->GetSrc1()->IsIntConstOpnd())
  936. {
  937. // Swap the operands, such that we would create (dst = INC src2)
  938. instr->SwapOpnds();
  939. }
  940. instr->FreeSrc2();
  941. instr->m_opcode = Js::OpCode::INC;
  942. }
  943. }
  944. void LowererMD::ChangeToSub(IR::Instr *const instr, const bool needFlags)
  945. {
  946. Assert(instr);
  947. Assert(instr->GetDst());
  948. Assert(instr->GetSrc1());
  949. Assert(instr->GetSrc2());
  950. if(instr->GetDst()->IsFloat64())
  951. {
  952. Assert(instr->GetSrc1()->IsFloat64());
  953. Assert(instr->GetSrc2()->IsFloat64());
  954. Assert(!needFlags);
  955. instr->m_opcode = Js::OpCode::SUBSD;
  956. return;
  957. }
  958. // Prefer DEC for sub by one
  959. if(instr->GetDst()->IsEqual(instr->GetSrc1()) &&
  960. instr->GetSrc2()->IsIntConstOpnd() &&
  961. instr->GetSrc2()->AsIntConstOpnd()->GetValue() == 1)
  962. {
  963. instr->FreeSrc2();
  964. instr->m_opcode = Js::OpCode::DEC;
  965. return;
  966. }
  967. instr->m_opcode = Js::OpCode::SUB;
  968. }
  969. void LowererMD::ChangeToShift(IR::Instr *const instr, const bool needFlags)
  970. {
  971. Assert(instr);
  972. Assert(instr->GetDst());
  973. Assert(instr->GetSrc1());
  974. Assert(instr->GetSrc2());
  975. switch(instr->m_opcode)
  976. {
  977. case Js::OpCode::Shl_A:
  978. case Js::OpCode::Shl_I4:
  979. instr->m_opcode = Js::OpCode::SHL;
  980. break;
  981. case Js::OpCode::Shr_A:
  982. case Js::OpCode::Shr_I4:
  983. instr->m_opcode = Js::OpCode::SAR;
  984. break;
  985. case Js::OpCode::ShrU_A:
  986. case Js::OpCode::ShrU_I4:
  987. instr->m_opcode = Js::OpCode::SHR;
  988. break;
  989. default:
  990. Assert(false);
  991. __assume(false);
  992. }
  993. if(instr->GetSrc2()->IsIntConstOpnd())
  994. {
  995. // Only values between 0-31 mean anything
  996. IntConstType value = instr->GetSrc2()->AsIntConstOpnd()->GetValue();
  997. value &= 0x1f;
  998. instr->GetSrc2()->AsIntConstOpnd()->SetValue(value);
  999. }
  1000. }
  1001. void LowererMD::ChangeToMul(IR::Instr *const instr, bool hasOverflowCheck)
  1002. {
  1003. // If non-32 bit overflow check is needed, we have to use the IMUL form.
  1004. if (hasOverflowCheck && !instr->ShouldCheckFor32BitOverflow() && instr->ShouldCheckForNon32BitOverflow())
  1005. {
  1006. IR::RegOpnd *regEAX = IR::RegOpnd::New(TyInt32, instr->m_func);
  1007. IR::Opnd *temp2 = nullptr;
  1008. // MOV eax, src1
  1009. regEAX->SetReg(LowererMDArch::GetRegIMulDestLower());
  1010. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, regEAX, instr->GetSrc1(), instr->m_func));
  1011. if (instr->GetSrc2()->IsImmediateOpnd())
  1012. {
  1013. // MOV reg, imm
  1014. temp2 = IR::RegOpnd::New(TyInt32, instr->m_func);
  1015. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, temp2,
  1016. IR::IntConstOpnd::New((IntConstType)instr->GetSrc2()->GetImmediateValue(), TyInt32, instr->m_func, true),
  1017. instr->m_func));
  1018. }
  1019. // eax = IMUL eax, reg
  1020. instr->m_opcode = Js::OpCode::IMUL;
  1021. instr->ReplaceSrc1(regEAX);
  1022. if (temp2 != nullptr)
  1023. instr->ReplaceSrc2(temp2);
  1024. auto *dst = instr->GetDst()->Copy(instr->m_func);
  1025. instr->ReplaceDst(regEAX);
  1026. // MOV dst, eax
  1027. instr->InsertAfter(IR::Instr::New(Js::OpCode::MOV, dst, regEAX, instr->m_func));
  1028. }
  1029. else
  1030. EmitInt4Instr(instr); // IMUL2
  1031. }
  1032. const uint16
  1033. LowererMD::GetFormalParamOffset()
  1034. {
  1035. //In x86\x64 formal params were offset from EBP by the EBP chain, return address, and the 2 non-user params
  1036. return 4;
  1037. }
  1038. IR::Instr *
  1039. LowererMD::LowerCatch(IR::Instr * instr)
  1040. {
  1041. // t1 = catch => t2(eax) = catch
  1042. // => t1 = t2(eax)
  1043. IR::Opnd *catchObj = instr->UnlinkDst();
  1044. IR::RegOpnd *catchParamReg = IR::RegOpnd::New(TyMachPtr, this->m_func);
  1045. catchParamReg->SetReg(this->lowererMDArch.GetRegReturn(TyMachReg));
  1046. instr->SetDst(catchParamReg);
  1047. instr->InsertAfter(IR::Instr::New(Js::OpCode::MOV, catchObj, catchParamReg, this->m_func));
  1048. return instr->m_prev;
  1049. }
  1050. ///----------------------------------------------------------------------------
  1051. ///
  1052. /// LowererMD::ForceDstToReg
  1053. ///
  1054. ///----------------------------------------------------------------------------
  1055. void
  1056. LowererMD::ForceDstToReg(IR::Instr *instr)
  1057. {
  1058. IR::Opnd * dst = instr->GetDst();
  1059. if (dst->IsRegOpnd())
  1060. {
  1061. return;
  1062. }
  1063. if(dst->IsFloat64())
  1064. {
  1065. instr->SinkDst(Js::OpCode::MOVSD);
  1066. return;
  1067. }
  1068. instr->SinkDst(Js::OpCode::MOV);
  1069. }
  1070. template <bool verify>
  1071. void
  1072. LowererMD::Legalize(IR::Instr *const instr, bool fPostRegAlloc)
  1073. {
  1074. Assert(instr);
  1075. Assert(!instr->isInlineeEntryInstr
  1076. || (instr->m_opcode == Js::OpCode::MOV && instr->GetSrc1()->IsAddrOpnd()));
  1077. switch(instr->m_opcode)
  1078. {
  1079. case Js::OpCode::MOV:
  1080. {
  1081. Assert(instr->GetSrc2() == nullptr);
  1082. IR::Opnd *const dst = instr->GetDst();
  1083. const IRType dstType = dst->GetType();
  1084. IR::Opnd *const src = instr->GetSrc1();
  1085. const IRType srcType = src->GetType();
  1086. if(TySize[dstType] > TySize[srcType])
  1087. {
  1088. if (verify)
  1089. {
  1090. return;
  1091. }
  1092. #if DBG
  1093. switch(dstType)
  1094. {
  1095. case TyInt32:
  1096. case TyUint32:
  1097. #ifdef _M_X64
  1098. case TyInt64:
  1099. case TyUint64:
  1100. #endif
  1101. case TyVar:
  1102. break;
  1103. default:
  1104. Assert(false);
  1105. }
  1106. #endif
  1107. IR::IntConstOpnd *const intConstantSrc = src->IsIntConstOpnd() ? src->AsIntConstOpnd() : nullptr;
  1108. const auto UpdateIntConstantSrc = [&](const size_t extendedValue)
  1109. {
  1110. Assert(intConstantSrc);
  1111. #ifdef _M_X64
  1112. if(TySize[dstType] > sizeof(IntConstType))
  1113. {
  1114. instr->ReplaceSrc1(
  1115. IR::AddrOpnd::New(
  1116. reinterpret_cast<void *>(extendedValue),
  1117. IR::AddrOpndKindConstantVar,
  1118. instr->m_func,
  1119. intConstantSrc->m_dontEncode));
  1120. }
  1121. else
  1122. #endif
  1123. {
  1124. intConstantSrc->SetType(dstType);
  1125. intConstantSrc->SetValue(static_cast<IntConstType>(extendedValue));
  1126. }
  1127. };
  1128. switch(srcType)
  1129. {
  1130. case TyInt8:
  1131. if(intConstantSrc)
  1132. {
  1133. UpdateIntConstantSrc(static_cast<int8>(intConstantSrc->GetValue())); // sign-extend
  1134. break;
  1135. }
  1136. instr->m_opcode = Js::OpCode::MOVSX;
  1137. break;
  1138. case TyUint8:
  1139. if(intConstantSrc)
  1140. {
  1141. UpdateIntConstantSrc(static_cast<uint8>(intConstantSrc->GetValue())); // zero-extend
  1142. break;
  1143. }
  1144. instr->m_opcode = Js::OpCode::MOVZX;
  1145. break;
  1146. case TyInt16:
  1147. if(intConstantSrc)
  1148. {
  1149. UpdateIntConstantSrc(static_cast<int16>(intConstantSrc->GetValue())); // sign-extend
  1150. break;
  1151. }
  1152. instr->m_opcode = Js::OpCode::MOVSXW;
  1153. break;
  1154. case TyUint16:
  1155. if(intConstantSrc)
  1156. {
  1157. UpdateIntConstantSrc(static_cast<uint16>(intConstantSrc->GetValue())); // zero-extend
  1158. break;
  1159. }
  1160. instr->m_opcode = Js::OpCode::MOVZXW;
  1161. break;
  1162. #ifdef _M_X64
  1163. case TyInt32:
  1164. if(intConstantSrc)
  1165. {
  1166. UpdateIntConstantSrc(static_cast<int32>(intConstantSrc->GetValue())); // sign-extend
  1167. break;
  1168. }
  1169. instr->m_opcode = Js::OpCode::MOVSXD;
  1170. break;
  1171. case TyUint32:
  1172. if(intConstantSrc)
  1173. {
  1174. UpdateIntConstantSrc(static_cast<uint32>(intConstantSrc->GetValue())); // zero-extend
  1175. break;
  1176. }
  1177. switch(dst->GetKind())
  1178. {
  1179. case IR::OpndKindReg:
  1180. // (mov r0.u32, r1.u32) clears the upper 32 bits of r0
  1181. dst->SetType(TyUint32);
  1182. instr->m_opcode = Js::OpCode::MOV_TRUNC;
  1183. break;
  1184. case IR::OpndKindSym:
  1185. case IR::OpndKindIndir:
  1186. case IR::OpndKindMemRef:
  1187. // Even if the src is a reg, we don't know if the upper 32 bits are zero. Copy the value to a
  1188. // reg first to zero-extend it to 64 bits, and then copy the 64-bit value to the original dst.
  1189. instr->HoistSrc1(Js::OpCode::MOV_TRUNC);
  1190. instr->GetSrc1()->SetType(dstType);
  1191. break;
  1192. default:
  1193. Assert(false);
  1194. __assume(false);
  1195. }
  1196. break;
  1197. #endif
  1198. default:
  1199. Assert(false);
  1200. __assume(false);
  1201. }
  1202. }
  1203. else if (TySize[dstType] < TySize[srcType])
  1204. {
  1205. instr->GetSrc1()->SetType(dst->GetType());
  1206. }
  1207. if(instr->m_opcode == Js::OpCode::MOV)
  1208. {
  1209. uint src1Forms = L_Reg | L_Mem | L_Ptr; // Allow 64 bit values in x64 as well
  1210. #if _M_X64
  1211. if (dst->IsMemoryOpnd())
  1212. {
  1213. // Only allow <= 32 bit values
  1214. src1Forms = L_Reg | L_Imm32;
  1215. }
  1216. #endif
  1217. LegalizeOpnds<verify>(
  1218. instr,
  1219. L_Reg | L_Mem,
  1220. src1Forms,
  1221. L_None);
  1222. }
  1223. else
  1224. {
  1225. LegalizeOpnds<verify>(
  1226. instr,
  1227. L_Reg,
  1228. L_Reg | L_Mem,
  1229. L_None);
  1230. }
  1231. break;
  1232. }
  1233. case Js::OpCode::MOVSD:
  1234. Assert(AutoSystemInfo::Data.SSE2Available());
  1235. case Js::OpCode::MOVSS:
  1236. {
  1237. Assert(instr->GetDst()->GetType() == (instr->m_opcode == Js::OpCode::MOVSD? TyFloat64 : TyFloat32) || instr->GetDst()->IsSimd128());
  1238. Assert(instr->GetSrc1()->GetType() == (instr->m_opcode == Js::OpCode::MOVSD ? TyFloat64 : TyFloat32) || instr->GetSrc1()->IsSimd128());
  1239. LegalizeOpnds<verify>(
  1240. instr,
  1241. L_Reg | L_Mem,
  1242. instr->GetDst()->IsMemoryOpnd()?
  1243. L_Reg : L_Reg | L_Mem, // LegalizeOpnds doesn't check if dst/src1 are both memopnd, check it here.
  1244. L_None);
  1245. break;
  1246. }
  1247. case Js::OpCode::MOVUPS:
  1248. case Js::OpCode::MOVAPS:
  1249. {
  1250. LegalizeOpnds<verify>(
  1251. instr,
  1252. L_Reg | L_Mem,
  1253. instr->GetDst()->IsMemoryOpnd()?
  1254. L_Reg : L_Reg | L_Mem, // LegalizeOpnds doesn't check if dst/src1 are both memopnd, check it here.
  1255. L_None);
  1256. break;
  1257. }
  1258. case Js::OpCode::CMP:
  1259. LegalizeOpnds<verify>(
  1260. instr,
  1261. L_None,
  1262. L_Reg | L_Mem,
  1263. L_Reg | L_Mem | L_Imm32);
  1264. break;
  1265. case Js::OpCode::TEST:
  1266. if(instr->GetSrc1()->IsImmediateOpnd() && !instr->GetSrc2()->IsImmediateOpnd() ||
  1267. instr->GetSrc2()->IsMemoryOpnd() && !instr->GetSrc1()->IsMemoryOpnd())
  1268. {
  1269. if (verify)
  1270. {
  1271. AssertMsg(false, "Missing legalization");
  1272. return;
  1273. }
  1274. instr->SwapOpnds();
  1275. }
  1276. LegalizeOpnds<verify>(
  1277. instr,
  1278. L_None,
  1279. L_Reg | L_Mem,
  1280. L_Reg | L_Imm32);
  1281. break;
  1282. case Js::OpCode::COMISD:
  1283. case Js::OpCode::UCOMISD:
  1284. Assert(AutoSystemInfo::Data.SSE2Available());
  1285. case Js::OpCode::COMISS:
  1286. case Js::OpCode::UCOMISS:
  1287. LegalizeOpnds<verify>(
  1288. instr,
  1289. L_None,
  1290. L_Reg,
  1291. L_Reg | L_Mem);
  1292. break;
  1293. case Js::OpCode::INC:
  1294. case Js::OpCode::DEC:
  1295. case Js::OpCode::NEG:
  1296. MakeDstEquSrc1<verify>(instr);
  1297. LegalizeOpnds<verify>(
  1298. instr,
  1299. L_Reg | L_Mem,
  1300. L_Reg | L_Mem,
  1301. L_None);
  1302. break;
  1303. case Js::OpCode::ADD:
  1304. case Js::OpCode::SUB:
  1305. case Js::OpCode::AND:
  1306. case Js::OpCode::OR:
  1307. case Js::OpCode::XOR:
  1308. MakeDstEquSrc1<verify>(instr);
  1309. LegalizeOpnds<verify>(
  1310. instr,
  1311. L_Reg | L_Mem,
  1312. L_Reg | L_Mem,
  1313. L_Reg | L_Mem | L_Imm32);
  1314. break;
  1315. case Js::OpCode::ADDSD:
  1316. case Js::OpCode::ADDPD:
  1317. case Js::OpCode::SUBSD:
  1318. case Js::OpCode::ANDPD:
  1319. case Js::OpCode::ANDNPD:
  1320. case Js::OpCode::DIVPD:
  1321. case Js::OpCode::MAXPD:
  1322. case Js::OpCode::MINPD:
  1323. case Js::OpCode::MULPD:
  1324. case Js::OpCode::SUBPD:
  1325. Assert(AutoSystemInfo::Data.SSE2Available());
  1326. case Js::OpCode::ADDPS:
  1327. case Js::OpCode::ADDSS:
  1328. case Js::OpCode::SUBSS:
  1329. case Js::OpCode::ANDPS:
  1330. case Js::OpCode::ANDNPS:
  1331. case Js::OpCode::DIVPS:
  1332. case Js::OpCode::MAXPS:
  1333. case Js::OpCode::MINPS:
  1334. case Js::OpCode::MULPS:
  1335. case Js::OpCode::ORPS:
  1336. case Js::OpCode::PADDD:
  1337. case Js::OpCode::PAND:
  1338. case Js::OpCode::PCMPEQD:
  1339. case Js::OpCode::PCMPGTD:
  1340. case Js::OpCode::PMULUDQ:
  1341. case Js::OpCode::POR:
  1342. case Js::OpCode::PSUBD:
  1343. case Js::OpCode::PXOR:
  1344. case Js::OpCode::SUBPS:
  1345. case Js::OpCode::XORPS:
  1346. case Js::OpCode::CMPLTPS:
  1347. case Js::OpCode::CMPLEPS:
  1348. case Js::OpCode::CMPEQPS:
  1349. case Js::OpCode::CMPNEQPS:
  1350. case Js::OpCode::CMPLTPD:
  1351. case Js::OpCode::CMPLEPD:
  1352. case Js::OpCode::CMPEQPD:
  1353. case Js::OpCode::CMPNEQPD:
  1354. case Js::OpCode::PUNPCKLDQ:
  1355. MakeDstEquSrc1<verify>(instr);
  1356. LegalizeOpnds<verify>(
  1357. instr,
  1358. L_Reg,
  1359. L_Reg,
  1360. L_Reg | L_Mem);
  1361. break;
  1362. case Js::OpCode::SHL:
  1363. case Js::OpCode::SHR:
  1364. case Js::OpCode::SAR:
  1365. if (verify)
  1366. {
  1367. Assert(instr->GetSrc2()->IsIntConstOpnd()
  1368. || instr->GetSrc2()->AsRegOpnd()->GetReg() == LowererMDArch::GetRegShiftCount());
  1369. }
  1370. else
  1371. {
  1372. if(!instr->GetSrc2()->IsIntConstOpnd())
  1373. {
  1374. IR::Instr *const newInstr = instr->HoistSrc2(Js::OpCode::MOV);
  1375. newInstr->GetDst()->AsRegOpnd()->SetReg(LowererMDArch::GetRegShiftCount());
  1376. instr->GetSrc2()->AsRegOpnd()->SetReg(LowererMDArch::GetRegShiftCount());
  1377. }
  1378. instr->GetSrc2()->SetType(TyUint8);
  1379. }
  1380. MakeDstEquSrc1<verify>(instr);
  1381. LegalizeOpnds<verify>(
  1382. instr,
  1383. L_Reg | L_Mem,
  1384. L_Reg | L_Mem,
  1385. L_Reg | L_Imm32);
  1386. break;
  1387. case Js::OpCode::IMUL2:
  1388. MakeDstEquSrc1<verify>(instr); // the encoder does not support IMUL3 r, r/m, imm
  1389. LegalizeOpnds<verify>(
  1390. instr,
  1391. L_Reg,
  1392. L_Reg,
  1393. L_Reg | L_Mem | L_Imm32); // for L_Imm32, the encoder converts it into an IMUL3
  1394. break;
  1395. case Js::OpCode::LZCNT:
  1396. Assert(AutoSystemInfo::Data.LZCntAvailable());
  1397. case Js::OpCode::BSR:
  1398. LegalizeOpnds<verify>(
  1399. instr,
  1400. L_Reg,
  1401. L_Reg | L_Mem,
  1402. L_None);
  1403. break;
  1404. case Js::OpCode::LEA:
  1405. Assert(instr->GetDst()->IsRegOpnd());
  1406. Assert(instr->GetSrc1()->IsIndirOpnd() || instr->GetSrc1()->IsSymOpnd());
  1407. Assert(!instr->GetSrc2());
  1408. break;
  1409. case Js::OpCode::PSRLDQ:
  1410. case Js::OpCode::PSLLDQ:
  1411. Assert(AutoSystemInfo::Data.SSE2Available());
  1412. MakeDstEquSrc1<verify>(instr);
  1413. LegalizeOpnds<verify>(
  1414. instr,
  1415. L_Reg,
  1416. L_Reg,
  1417. L_Imm32);
  1418. break;
  1419. case Js::OpCode::ROUNDSD:
  1420. case Js::OpCode::ROUNDSS:
  1421. Assert(AutoSystemInfo::Data.SSE4_1Available());
  1422. break;
  1423. case Js::OpCode::CVTDQ2PD:
  1424. case Js::OpCode::CVTDQ2PS:
  1425. case Js::OpCode::CVTPD2PS:
  1426. case Js::OpCode::CVTPS2PD:
  1427. case Js::OpCode::CVTSD2SI:
  1428. case Js::OpCode::CVTSD2SS:
  1429. case Js::OpCode::CVTSI2SD:
  1430. case Js::OpCode::CVTSS2SD:
  1431. case Js::OpCode::CVTTPD2DQ:
  1432. case Js::OpCode::CVTTPS2DQ:
  1433. case Js::OpCode::CVTTSD2SI:
  1434. case Js::OpCode::DIVSD:
  1435. case Js::OpCode::SQRTPD:
  1436. case Js::OpCode::SQRTSD:
  1437. case Js::OpCode::SHUFPD:
  1438. Assert(AutoSystemInfo::Data.SSE2Available());
  1439. break;
  1440. }
  1441. #if DBG
  1442. // Asserting general rules
  1443. // There should be at most 1 memory opnd in an instruction
  1444. if (instr->GetDst() && instr->GetDst()->IsMemoryOpnd())
  1445. {
  1446. // All memref address need to fit in a dword
  1447. Assert(!instr->GetDst()->IsMemRefOpnd() || Math::FitsInDWord((size_t)instr->GetDst()->AsMemRefOpnd()->GetMemLoc()));
  1448. if (instr->GetSrc1())
  1449. {
  1450. Assert(instr->GetSrc1()->IsEqual(instr->GetDst()) || !instr->GetSrc1()->IsMemoryOpnd());
  1451. if (instr->GetSrc2())
  1452. {
  1453. Assert(!instr->GetSrc2()->IsMemoryOpnd());
  1454. }
  1455. }
  1456. }
  1457. else if (instr->GetSrc1() && instr->GetSrc1()->IsMemoryOpnd())
  1458. {
  1459. // All memref address need to fit in a dword
  1460. Assert(!instr->GetSrc1()->IsMemRefOpnd() || Math::FitsInDWord((size_t)instr->GetSrc1()->AsMemRefOpnd()->GetMemLoc()));
  1461. Assert(!instr->GetSrc2() || !instr->GetSrc2()->IsMemoryOpnd());
  1462. }
  1463. else if (instr->GetSrc2() && instr->GetSrc2()->IsMemRefOpnd())
  1464. {
  1465. // All memref address need to fit in a dword
  1466. Assert(Math::FitsInDWord((size_t)instr->GetSrc2()->AsMemRefOpnd()->GetMemLoc()));
  1467. }
  1468. // Non-MOV (second operand) immediate need to fit in DWORD for AMD64
  1469. Assert(!instr->GetSrc2() || !instr->GetSrc2()->IsImmediateOpnd()
  1470. || (TySize[instr->GetSrc2()->GetType()] != 8) || Math::FitsInDWord(instr->GetSrc2()->GetImmediateValue()));
  1471. #endif
  1472. }
  1473. template <bool verify>
  1474. void LowererMD::LegalizeOpnds(IR::Instr *const instr, const uint dstForms, const uint src1Forms, uint src2Forms)
  1475. {
  1476. Assert(instr);
  1477. Assert(!instr->GetDst() == !dstForms);
  1478. Assert(!instr->GetSrc1() == !src1Forms);
  1479. Assert(!instr->GetSrc2() == !src2Forms);
  1480. Assert(src1Forms || !src2Forms);
  1481. const auto NormalizeForms = [](uint forms) -> uint
  1482. {
  1483. #ifdef _M_X64
  1484. if(forms & L_Ptr)
  1485. {
  1486. forms |= L_Imm32;
  1487. }
  1488. #else
  1489. if(forms & (L_Imm32 | L_Ptr))
  1490. {
  1491. forms |= L_Imm32 | L_Ptr;
  1492. }
  1493. #endif
  1494. return forms;
  1495. };
  1496. if(dstForms)
  1497. {
  1498. LegalizeDst<verify>(instr, NormalizeForms(dstForms));
  1499. }
  1500. if(!src1Forms)
  1501. {
  1502. return;
  1503. }
  1504. LegalizeSrc<verify>(instr, instr->GetSrc1(), NormalizeForms(src1Forms));
  1505. if(src2Forms & L_Mem && instr->GetSrc1()->IsMemoryOpnd())
  1506. {
  1507. src2Forms ^= L_Mem;
  1508. }
  1509. if(src2Forms)
  1510. {
  1511. LegalizeSrc<verify>(instr, instr->GetSrc2(), NormalizeForms(src2Forms));
  1512. }
  1513. }
  1514. template <bool verify>
  1515. void LowererMD::LegalizeDst(IR::Instr *const instr, const uint forms)
  1516. {
  1517. Assert(instr);
  1518. Assert(forms);
  1519. IR::Opnd *dst = instr->GetDst();
  1520. Assert(dst);
  1521. switch(dst->GetKind())
  1522. {
  1523. case IR::OpndKindReg:
  1524. Assert(forms & L_Reg);
  1525. return;
  1526. case IR::OpndKindMemRef:
  1527. {
  1528. IR::MemRefOpnd *const memRefOpnd = dst->AsMemRefOpnd();
  1529. if(!LowererMDArch::IsLegalMemLoc(memRefOpnd))
  1530. {
  1531. if (verify)
  1532. {
  1533. AssertMsg(false, "Missing legalization");
  1534. return;
  1535. }
  1536. dst = instr->HoistMemRefAddress(memRefOpnd, Js::OpCode::MOV);
  1537. }
  1538. // fall through
  1539. }
  1540. case IR::OpndKindSym:
  1541. case IR::OpndKindIndir:
  1542. if(forms & L_Mem)
  1543. {
  1544. return;
  1545. }
  1546. break;
  1547. default:
  1548. Assert(false);
  1549. __assume(false);
  1550. }
  1551. if (verify)
  1552. {
  1553. AssertMsg(false, "Missing legalization");
  1554. return;
  1555. }
  1556. // Use a reg dst, then store that reg into the original dst
  1557. Assert(forms & L_Reg);
  1558. const IRType irType = dst->GetType();
  1559. IR::RegOpnd *const regOpnd = IR::RegOpnd::New(irType, instr->m_func);
  1560. regOpnd->SetValueType(dst->GetValueType());
  1561. instr->UnlinkDst();
  1562. instr->SetDst(regOpnd);
  1563. instr->InsertAfter(IR::Instr::New(GetStoreOp(irType), dst, regOpnd, instr->m_func));
  1564. // 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
  1565. const bool equalsSrc1 = instr->GetSrc1() && dst->IsEqual(instr->GetSrc1());
  1566. const bool equalsSrc2 = instr->GetSrc2() && dst->IsEqual(instr->GetSrc2());
  1567. if(!(equalsSrc1 || equalsSrc2))
  1568. {
  1569. return;
  1570. }
  1571. const Js::OpCode loadOpCode = GetLoadOp(irType);
  1572. if(equalsSrc1)
  1573. {
  1574. instr->HoistSrc1(loadOpCode, RegNOREG, regOpnd->m_sym);
  1575. if(equalsSrc2)
  1576. {
  1577. instr->ReplaceSrc2(regOpnd);
  1578. }
  1579. }
  1580. else
  1581. {
  1582. instr->HoistSrc2(loadOpCode, RegNOREG, regOpnd->m_sym);
  1583. }
  1584. }
  1585. template <bool verify>
  1586. void LowererMD::LegalizeSrc(IR::Instr *const instr, IR::Opnd *src, const uint forms)
  1587. {
  1588. Assert(instr);
  1589. Assert(src);
  1590. Assert(src == instr->GetSrc1() || src == instr->GetSrc2());
  1591. Assert(forms);
  1592. switch(src->GetKind())
  1593. {
  1594. case IR::OpndKindReg:
  1595. Assert(forms & L_Reg);
  1596. return;
  1597. case IR::OpndKindIntConst:
  1598. Assert(!instr->isInlineeEntryInstr);
  1599. if(forms & L_Imm32)
  1600. {
  1601. return;
  1602. }
  1603. break;
  1604. case IR::OpndKindFloatConst:
  1605. break; // assume for now that it always needs to be hoisted
  1606. case IR::OpndKindAddr:
  1607. if (forms & L_Ptr)
  1608. {
  1609. return;
  1610. }
  1611. #ifdef _M_X64
  1612. {
  1613. IR::AddrOpnd * addrOpnd = src->AsAddrOpnd();
  1614. if ((forms & L_Imm32) && ((TySize[addrOpnd->GetType()] != 8) ||
  1615. (!instr->isInlineeEntryInstr && Math::FitsInDWord((size_t)addrOpnd->m_address))))
  1616. {
  1617. // the address fits in 32-bit, no need to hoist
  1618. return;
  1619. }
  1620. if (verify)
  1621. {
  1622. AssertMsg(false, "Missing legalization");
  1623. return;
  1624. }
  1625. // The actual value for inlinee entry instr isn't determined until encoder
  1626. // So it need to be hoisted conventionally.
  1627. if (!instr->isInlineeEntryInstr)
  1628. {
  1629. Assert(forms & L_Reg);
  1630. IR::IndirOpnd * indirOpnd = instr->m_func->GetTopFunc()->GetConstantAddressIndirOpnd(addrOpnd->m_address, addrOpnd->GetAddrOpndKind(), TyMachPtr, Js::OpCode::MOV);
  1631. if (indirOpnd != nullptr)
  1632. {
  1633. if (indirOpnd->GetOffset() == 0)
  1634. {
  1635. instr->ReplaceSrc(src, indirOpnd->GetBaseOpnd());
  1636. }
  1637. else
  1638. {
  1639. // Hoist the address load as LEA [reg + offset]
  1640. // with the reg = MOV <some address within 32-bit range at the start of the function
  1641. IR::RegOpnd * regOpnd = IR::RegOpnd::New(TyMachPtr, instr->m_func);
  1642. Lowerer::InsertLea(regOpnd, indirOpnd, instr);
  1643. instr->ReplaceSrc(src, regOpnd);
  1644. }
  1645. return;
  1646. }
  1647. }
  1648. }
  1649. #endif
  1650. break;
  1651. case IR::OpndKindMemRef:
  1652. {
  1653. IR::MemRefOpnd *const memRefOpnd = src->AsMemRefOpnd();
  1654. if(!LowererMDArch::IsLegalMemLoc(memRefOpnd))
  1655. {
  1656. if (verify)
  1657. {
  1658. AssertMsg(false, "Missing legalization");
  1659. return;
  1660. }
  1661. src = instr->HoistMemRefAddress(memRefOpnd, Js::OpCode::MOV);
  1662. }
  1663. // fall through
  1664. }
  1665. case IR::OpndKindSym:
  1666. case IR::OpndKindIndir:
  1667. if(forms & L_Mem)
  1668. {
  1669. return;
  1670. }
  1671. break;
  1672. case IR::OpndKindHelperCall:
  1673. case IR::OpndKindLabel:
  1674. Assert(!instr->isInlineeEntryInstr);
  1675. Assert(forms & L_Ptr);
  1676. return;
  1677. default:
  1678. Assert(false);
  1679. __assume(false);
  1680. }
  1681. if (verify)
  1682. {
  1683. AssertMsg(false, "Missing legalization");
  1684. return;
  1685. }
  1686. // Hoist the src into a reg
  1687. Assert(forms & L_Reg);
  1688. Assert(!(instr->GetDst() && instr->GetDst()->IsEqual(src)));
  1689. const Js::OpCode loadOpCode = GetLoadOp(src->GetType());
  1690. if(src == instr->GetSrc2())
  1691. {
  1692. instr->HoistSrc2(loadOpCode);
  1693. return;
  1694. }
  1695. const bool equalsSrc2 = instr->GetSrc2() && src->IsEqual(instr->GetSrc2());
  1696. IR::Instr * hoistInstr = instr->HoistSrc1(loadOpCode);
  1697. if(equalsSrc2)
  1698. {
  1699. instr->ReplaceSrc2(hoistInstr->GetDst());
  1700. }
  1701. hoistInstr->isInlineeEntryInstr = instr->isInlineeEntryInstr;
  1702. instr->isInlineeEntryInstr = false;
  1703. }
  1704. template void LowererMD::Legalize<false>(IR::Instr *const instr, bool fPostRegAlloc);
  1705. template void LowererMD::LegalizeOpnds<false>(IR::Instr *const instr, const uint dstForms, const uint src1Forms, uint src2Forms);
  1706. template void LowererMD::LegalizeDst<false>(IR::Instr *const instr, const uint forms);
  1707. template void LowererMD::LegalizeSrc<false>(IR::Instr *const instr, IR::Opnd *src, const uint forms);
  1708. template void LowererMD::MakeDstEquSrc1<false>(IR::Instr *const instr);
  1709. #if DBG
  1710. template void LowererMD::Legalize<true>(IR::Instr *const instr, bool fPostRegAlloc);
  1711. template void LowererMD::LegalizeOpnds<true>(IR::Instr *const instr, const uint dstForms, const uint src1Forms, uint src2Forms);
  1712. template void LowererMD::LegalizeDst<true>(IR::Instr *const instr, const uint forms);
  1713. template void LowererMD::LegalizeSrc<true>(IR::Instr *const instr, IR::Opnd *src, const uint forms);
  1714. template void LowererMD::MakeDstEquSrc1<true>(IR::Instr *const instr);
  1715. #endif
  1716. IR::Instr *
  1717. LowererMD::LoadFunctionObjectOpnd(IR::Instr *instr, IR::Opnd *&functionObjOpnd)
  1718. {
  1719. IR::Opnd * src1 = instr->GetSrc1();
  1720. IR::Instr * instrPrev = instr->m_prev;
  1721. if (src1 == nullptr)
  1722. {
  1723. IR::RegOpnd * regOpnd = IR::RegOpnd::New(TyMachPtr, m_func);
  1724. StackSym *paramSym = StackSym::New(TyMachPtr, m_func);
  1725. IR::SymOpnd *paramOpnd = IR::SymOpnd::New(paramSym, TyMachPtr, m_func);
  1726. this->m_func->SetArgOffset(paramSym, 2 * MachPtr);
  1727. IR::Instr * mov1 = IR::Instr::New(Js::OpCode::MOV, regOpnd, paramOpnd, m_func);
  1728. instr->InsertBefore(mov1);
  1729. functionObjOpnd = mov1->GetDst()->AsRegOpnd();
  1730. instrPrev = mov1;
  1731. }
  1732. else
  1733. {
  1734. // Inlinee, use the function object opnd on the instruction
  1735. functionObjOpnd = instr->UnlinkSrc1();
  1736. if (!functionObjOpnd->IsRegOpnd())
  1737. {
  1738. Assert(functionObjOpnd->IsAddrOpnd());
  1739. }
  1740. }
  1741. return instrPrev;
  1742. }
  1743. IR::Instr *
  1744. LowererMD::LowerLdSuper(IR::Instr *instr, IR::JnHelperMethod helperOpCode)
  1745. {
  1746. IR::Opnd * functionObjOpnd;
  1747. IR::Instr * instrPrev = LoadFunctionObjectOpnd(instr, functionObjOpnd);
  1748. m_lowerer->LoadScriptContext(instr);
  1749. LoadHelperArgument(instr, functionObjOpnd);
  1750. ChangeToHelperCall(instr, helperOpCode);
  1751. return instrPrev;
  1752. }
  1753. void
  1754. LowererMD::GenerateFastDivByPow2(IR::Instr *instr)
  1755. {
  1756. //
  1757. // Given:
  1758. // dst = Div_A src1, src2
  1759. // where src2 == power of 2
  1760. //
  1761. // Generate:
  1762. // MOV s1, src1
  1763. // AND s1, 0xFFFF000000000000 | (src2Value-1) ----- test for tagged int and divisibility by src2Value [int32]
  1764. // AND s1, 0x00000001 | ((src2Value-1)<<1) [int31]
  1765. // CMP s1, AtomTag_IntPtr
  1766. // JNE $divbyhalf
  1767. // MOV s1, src1
  1768. // SAR s1, log2(src2Value) ------ perform the divide
  1769. // OR s1, 1
  1770. // MOV dst, s1
  1771. // JMP $done
  1772. // $divbyhalf:
  1773. // AND s1, 0xFFFF000000000000 | (src2Value-1>>1) ----- test for tagged int and divisibility by src2Value /2 [int32]
  1774. // AND s1, 0x00000001 | ((src2Value-1)) [int31]
  1775. // CMP s1, AtomTag_IntPtr
  1776. // JNE $helper
  1777. // MOV s1, src1
  1778. // SAR s1, log2(src2Value) [int32]
  1779. // SAR s1, log2(src2Value) + 1 ------ removes the tag and divides [int31]
  1780. // PUSH s1
  1781. // PUSH 0xXXXXXXXX (ScriptContext)
  1782. // CALL Op_FinishOddDivByPow2
  1783. // MOV dst, eax
  1784. // JMP $done
  1785. // $helper:
  1786. // ...
  1787. // $done:
  1788. //
  1789. if (instr->GetSrc1()->IsRegOpnd() && instr->GetSrc1()->AsRegOpnd()->IsNotInt())
  1790. return;
  1791. IR::Opnd *dst = instr->GetDst();
  1792. IR::Opnd *src1 = instr->GetSrc1();
  1793. IR::AddrOpnd *src2 = instr->GetSrc2()->IsAddrOpnd() ? instr->GetSrc2()->AsAddrOpnd() : nullptr;
  1794. IR::LabelInstr *divbyhalf = IR::LabelInstr::New(Js::OpCode::Label, m_func);
  1795. IR::LabelInstr *helper = IR::LabelInstr::New(Js::OpCode::Label, m_func, true);
  1796. IR::LabelInstr *done = IR::LabelInstr::New(Js::OpCode::Label, m_func);
  1797. IR::RegOpnd *s1 = IR::RegOpnd::New(TyVar, m_func);
  1798. AnalysisAssert(src2);
  1799. Assert(src2->IsVar() && Js::TaggedInt::Is(src2->m_address) && (Math::IsPow2(Js::TaggedInt::ToInt32(src2->m_address))));
  1800. int32 src2Value = Js::TaggedInt::ToInt32(src2->m_address);
  1801. // MOV s1, src1
  1802. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, s1, src1, m_func));
  1803. #if INT32VAR
  1804. // dontEncode as src2 is a power of 2.
  1805. IR::Opnd *constant = IR::AddrOpnd::New((Js::Var)(0xFFFF000000000000 | (src2Value - 1)), IR::AddrOpndKindConstantVar, m_func, /* dontEncode = */ true);
  1806. #else
  1807. IR::Opnd *constant = IR::IntConstOpnd::New((0x00000001 | ((src2Value - 1) << 1)), TyInt32, m_func);
  1808. #endif
  1809. // AND s1, constant
  1810. {
  1811. IR::Instr * andInstr = IR::Instr::New(Js::OpCode::AND, s1, s1, constant, m_func);
  1812. instr->InsertBefore(andInstr);
  1813. Legalize(andInstr);
  1814. }
  1815. // CMP s1, AtomTag_IntPtr
  1816. {
  1817. IR::Instr *cmp = IR::Instr::New(Js::OpCode::CMP, m_func);
  1818. cmp->SetSrc1(s1);
  1819. cmp->SetSrc2(IR::AddrOpnd::New((Js::Var)(Js::AtomTag_IntPtr), IR::AddrOpndKindConstantVar, m_func, /* dontEncode = */ true));
  1820. instr->InsertBefore(cmp);
  1821. Legalize(cmp);
  1822. }
  1823. // JNE $divbyhalf
  1824. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JNE, divbyhalf, m_func));
  1825. // MOV s1, src1
  1826. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, s1, src1, m_func));
  1827. s1 = s1->UseWithNewType(TyInt32, m_func)->AsRegOpnd();
  1828. // SAR s1, log2(src2Value)
  1829. instr->InsertBefore(IR::Instr::New(Js::OpCode::SAR, s1, s1, IR::IntConstOpnd::New(Math::Log2(src2Value), TyInt32, m_func), m_func));
  1830. if(s1->GetSize() != MachPtr)
  1831. {
  1832. s1 = s1->UseWithNewType(TyMachPtr, m_func)->AsRegOpnd();
  1833. }
  1834. #if INT32VAR
  1835. GenerateInt32ToVarConversion(s1, instr);
  1836. #else
  1837. // OR s1, 1
  1838. instr->InsertBefore(IR::Instr::New(Js::OpCode::OR, s1, s1, IR::IntConstOpnd::New(1, TyInt32, m_func), m_func));
  1839. #endif
  1840. // MOV dst, s1
  1841. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, dst, s1, m_func));
  1842. // JMP $done
  1843. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JMP, done, m_func));
  1844. // $divbyhalf:
  1845. instr->InsertBefore(divbyhalf);
  1846. #if INT32VAR
  1847. constant = IR::AddrOpnd::New((Js::Var)(0xFFFF000000000000 | ((src2Value-1) >> 1)), IR::AddrOpndKindConstantVar, m_func, /* dontEncode = */ true);
  1848. #else
  1849. constant = IR::IntConstOpnd::New((0x00000001 | (src2Value-1)), TyInt32, m_func);
  1850. #endif
  1851. // AND s1, constant
  1852. {
  1853. IR::Instr * andInstr = IR::Instr::New(Js::OpCode::AND, s1, s1, constant, m_func);
  1854. instr->InsertBefore(andInstr);
  1855. Legalize(andInstr);
  1856. }
  1857. // CMP s1, AtomTag_IntPtr
  1858. {
  1859. IR::Instr *cmp = IR::Instr::New(Js::OpCode::CMP, m_func);
  1860. cmp->SetSrc1(s1);
  1861. cmp->SetSrc2(IR::AddrOpnd::New((Js::Var)(Js::AtomTag_IntPtr), IR::AddrOpndKindConstantVar, m_func, /* dontEncode = */ true));
  1862. instr->InsertBefore(cmp);
  1863. Legalize(cmp);
  1864. }
  1865. // JNE $helper
  1866. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JNE, helper, m_func));
  1867. // MOV s1, src1
  1868. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, s1, src1, m_func));
  1869. s1 = s1->UseWithNewType(TyInt32, this->m_func)->AsRegOpnd();
  1870. #if INT32VAR
  1871. IR::Opnd* shiftOpnd = IR::IntConstOpnd::New(Math::Log2(src2Value), TyInt32, m_func);
  1872. #else
  1873. IR::Opnd* shiftOpnd = IR::IntConstOpnd::New(Math::Log2(src2Value) + 1, TyInt32, m_func);
  1874. #endif
  1875. // SAR s1, shiftOpnd
  1876. instr->InsertBefore(IR::Instr::New(Js::OpCode::SAR, s1, s1, shiftOpnd, m_func));
  1877. // PUSH s1
  1878. // PUSH ScriptContext
  1879. // CALL Op_FinishOddDivByPow2
  1880. {
  1881. IR::JnHelperMethod helperMethod;
  1882. if (instr->dstIsTempNumber)
  1883. {
  1884. IR::Opnd *tempOpnd;
  1885. helperMethod = IR::HelperOp_FinishOddDivByPow2InPlace;
  1886. Assert(dst->IsRegOpnd());
  1887. StackSym * tempNumberSym = this->m_lowerer->GetTempNumberSym(dst, instr->dstIsTempNumberTransferred);
  1888. IR::Instr *load = this->LoadStackAddress(tempNumberSym);
  1889. instr->InsertBefore(load);
  1890. tempOpnd = load->GetDst();
  1891. this->lowererMDArch.LoadHelperArgument(instr, tempOpnd);
  1892. }
  1893. else
  1894. {
  1895. helperMethod = IR::HelperOp_FinishOddDivByPow2;
  1896. }
  1897. m_lowerer->LoadScriptContext(instr);
  1898. lowererMDArch.LoadHelperArgument(instr, s1);
  1899. IR::Instr *call = IR::Instr::New(Js::OpCode::Call, dst, IR::HelperCallOpnd::New(helperMethod, m_func), m_func);
  1900. instr->InsertBefore(call);
  1901. lowererMDArch.LowerCall(call, 0);
  1902. }
  1903. // JMP $done
  1904. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JMP, done, m_func));
  1905. // $helper:
  1906. instr->InsertBefore(helper);
  1907. // $done:
  1908. instr->InsertAfter(done);
  1909. }
  1910. bool
  1911. LowererMD::GenerateFastBrString(IR::BranchInstr *branchInstr)
  1912. {
  1913. Assert(branchInstr->m_opcode == Js::OpCode::BrSrEq_A ||
  1914. branchInstr->m_opcode == Js::OpCode::BrSrNeq_A ||
  1915. branchInstr->m_opcode == Js::OpCode::BrEq_A ||
  1916. branchInstr->m_opcode == Js::OpCode::BrNeq_A ||
  1917. branchInstr->m_opcode == Js::OpCode::BrSrNotEq_A ||
  1918. branchInstr->m_opcode == Js::OpCode::BrSrNotNeq_A ||
  1919. branchInstr->m_opcode == Js::OpCode::BrNotEq_A ||
  1920. branchInstr->m_opcode == Js::OpCode::BrNotNeq_A
  1921. );
  1922. IR::Instr* instrInsert = branchInstr;
  1923. IR::RegOpnd *srcReg1 = branchInstr->GetSrc1()->IsRegOpnd() ? branchInstr->GetSrc1()->AsRegOpnd() : nullptr;
  1924. IR::RegOpnd *srcReg2 = branchInstr->GetSrc2()->IsRegOpnd() ? branchInstr->GetSrc2()->AsRegOpnd() : nullptr;
  1925. if (srcReg1 && srcReg2)
  1926. {
  1927. if (srcReg1->IsTaggedInt() || srcReg2->IsTaggedInt())
  1928. {
  1929. return false;
  1930. }
  1931. bool isSrc1String = srcReg1->GetValueType().IsLikelyString();
  1932. bool isSrc2String = srcReg2->GetValueType().IsLikelyString();
  1933. //Left and right hand are both LikelyString
  1934. if (!isSrc1String || !isSrc2String)
  1935. {
  1936. return false;
  1937. }
  1938. }
  1939. else
  1940. {
  1941. return false;
  1942. }
  1943. // Generates:
  1944. // GenerateObjectTest(src1);
  1945. // MOV s1, [srcReg1 + offset(Type)]
  1946. // CMP type, static_string_type
  1947. // JNE $helper
  1948. // GenerateObjectTest(src2);
  1949. // MOV s2, [srcReg2 + offset(Type)]
  1950. // CMP type, static_string_type
  1951. // JNE $fail ; if src1 is string but not src2, src1 !== src2 if isStrict
  1952. // MOV s3, [srcReg1,offset(m_charLength)]
  1953. // CMP [srcReg2,offset(m_charLength)], s3
  1954. // JNE $fail <--- length check done
  1955. // MOV s4, [srcReg1,offset(m_pszValue)]
  1956. // CMP srcReg1, srcReg2
  1957. // JEQ $success
  1958. // CMP s4, 0
  1959. // JEQ $helper
  1960. // MOV s5, [srcReg2,offset(m_pszValue)]
  1961. // CMP s5, 0
  1962. // JEQ $helper
  1963. // MOV s6,[s4]
  1964. // CMP [s5], s6 -First character comparison
  1965. // JNE $fail
  1966. // SHL length, 1
  1967. // eax = memcmp(src1String, src2String, length*2)
  1968. // TEST eax, eax
  1969. // JEQ $success
  1970. // JMP $fail
  1971. IR::LabelInstr *labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  1972. IR::LabelInstr *labelTarget = branchInstr->GetTarget();
  1973. IR::LabelInstr *labelBranchFail = nullptr;
  1974. IR::LabelInstr *labelBranchSuccess = nullptr;
  1975. bool isEqual = false;
  1976. bool isStrict = false;
  1977. switch (branchInstr->m_opcode)
  1978. {
  1979. case Js::OpCode::BrSrEq_A:
  1980. case Js::OpCode::BrSrNotNeq_A:
  1981. isStrict = true;
  1982. case Js::OpCode::BrEq_A:
  1983. case Js::OpCode::BrNotNeq_A:
  1984. labelBranchFail = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  1985. labelBranchSuccess = labelTarget;
  1986. branchInstr->InsertAfter(labelBranchFail);
  1987. isEqual = true;
  1988. break;
  1989. case Js::OpCode::BrSrNeq_A:
  1990. case Js::OpCode::BrSrNotEq_A:
  1991. isStrict = true;
  1992. case Js::OpCode::BrNeq_A:
  1993. case Js::OpCode::BrNotEq_A:
  1994. labelBranchSuccess = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  1995. labelBranchFail = labelTarget;
  1996. branchInstr->InsertAfter(labelBranchSuccess);
  1997. isEqual = false;
  1998. break;
  1999. default:
  2000. Assert(UNREACHED);
  2001. __assume(0);
  2002. }
  2003. this->m_lowerer->GenerateStringTest(srcReg1, instrInsert, labelHelper);
  2004. if (isStrict)
  2005. {
  2006. this->m_lowerer->GenerateStringTest(srcReg2, instrInsert, labelBranchFail);
  2007. }
  2008. else
  2009. {
  2010. this->m_lowerer->GenerateStringTest(srcReg2, instrInsert, labelHelper);
  2011. }
  2012. // MOV s3, [srcReg1,offset(m_charLength)]
  2013. // CMP [srcReg2,offset(m_charLength)], s3
  2014. // JNE $branchfail
  2015. IR::RegOpnd * src1LengthOpnd = IR::RegOpnd::New(TyUint32, this->m_func);
  2016. IR::Instr * loadSrc1LengthInstr = IR::Instr::New(Js::OpCode::MOV, src1LengthOpnd,
  2017. IR::IndirOpnd::New(srcReg1, Js::JavascriptString::GetOffsetOfcharLength(), TyUint32,
  2018. this->m_func), this->m_func);
  2019. instrInsert->InsertBefore(loadSrc1LengthInstr);
  2020. IR::Instr * checkLengthInstr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  2021. checkLengthInstr->SetSrc1(IR::IndirOpnd::New(srcReg2, Js::JavascriptString::GetOffsetOfcharLength(), TyUint32, this->m_func));
  2022. checkLengthInstr->SetSrc2(src1LengthOpnd);
  2023. instrInsert->InsertBefore(checkLengthInstr);
  2024. instrInsert->InsertBefore(IR::BranchInstr::New(Js::OpCode::JNE, labelBranchFail, this->m_func));
  2025. // MOV s4, [src1,offset(m_pszValue)]
  2026. // CMP s4, 0
  2027. // JEQ $helper
  2028. // MOV s5, [src2,offset(m_pszValue)]
  2029. // CMP s5, 0
  2030. // JEQ $helper
  2031. IR::RegOpnd * src1FlatString = IR::RegOpnd::New(TyMachPtr, this->m_func);
  2032. IR::Instr * loadSrc1StringInstr = IR::Instr::New(Js::OpCode::MOV, src1FlatString,
  2033. IR::IndirOpnd::New(srcReg1, Js::JavascriptString::GetOffsetOfpszValue(), TyMachPtr,
  2034. this->m_func), this->m_func);
  2035. instrInsert->InsertBefore(loadSrc1StringInstr);
  2036. IR::Instr * checkFlatString1Instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  2037. checkFlatString1Instr->SetSrc1(src1FlatString);
  2038. checkFlatString1Instr->SetSrc2(IR::IntConstOpnd::New(0, TyUint32, this->m_func));
  2039. instrInsert->InsertBefore(checkFlatString1Instr);
  2040. instrInsert->InsertBefore(IR::BranchInstr::New(Js::OpCode::JEQ, labelHelper, this->m_func));
  2041. IR::RegOpnd * src2FlatString = IR::RegOpnd::New(TyMachPtr, this->m_func);
  2042. IR::Instr * loadSrc2StringInstr = IR::Instr::New(Js::OpCode::MOV, src2FlatString,
  2043. IR::IndirOpnd::New(srcReg2, Js::JavascriptString::GetOffsetOfpszValue(), TyMachPtr,
  2044. this->m_func), this->m_func);
  2045. instrInsert->InsertBefore(loadSrc2StringInstr);
  2046. // CMP srcReg1, srcReg2 - Ptr comparison
  2047. // JEQ $branchSuccess
  2048. IR::Instr * comparePtrInstr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  2049. comparePtrInstr->SetSrc1(srcReg1);
  2050. comparePtrInstr->SetSrc2(srcReg2);
  2051. instrInsert->InsertBefore(comparePtrInstr);
  2052. instrInsert->InsertBefore(IR::BranchInstr::New(Js::OpCode::JEQ, labelBranchSuccess, this->m_func));
  2053. IR::Instr * checkFlatString2Instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  2054. checkFlatString2Instr->SetSrc1(src2FlatString);
  2055. checkFlatString2Instr->SetSrc2(IR::IntConstOpnd::New(0, TyUint32, this->m_func));
  2056. instrInsert->InsertBefore(checkFlatString2Instr);
  2057. instrInsert->InsertBefore(IR::BranchInstr::New(Js::OpCode::JEQ, labelHelper, this->m_func));
  2058. // MOV s6,[s4]
  2059. // CMP [s5], s6 -First character comparison
  2060. // JNE $branchfail
  2061. IR::RegOpnd * src1FirstChar = IR::RegOpnd::New(TyUint16, this->m_func);
  2062. IR::Instr * loadSrc1CharInstr = IR::Instr::New(Js::OpCode::MOV, src1FirstChar,
  2063. IR::IndirOpnd::New(src1FlatString, 0, TyUint16,
  2064. this->m_func), this->m_func);
  2065. instrInsert->InsertBefore(loadSrc1CharInstr);
  2066. IR::Instr * compareFirstCharInstr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  2067. compareFirstCharInstr->SetSrc1(IR::IndirOpnd::New(src2FlatString, 0, TyUint16, this->m_func));
  2068. compareFirstCharInstr->SetSrc2(src1FirstChar);
  2069. instrInsert->InsertBefore(compareFirstCharInstr);
  2070. instrInsert->InsertBefore(IR::BranchInstr::New(Js::OpCode::JNE, labelBranchFail, this->m_func));
  2071. // SHL length, 1
  2072. instrInsert->InsertBefore(IR::Instr::New(Js::OpCode::SHL, src1LengthOpnd, src1LengthOpnd, IR::IntConstOpnd::New(1, TyUint8, this->m_func), this->m_func));
  2073. // eax = memcmp(src1String, src2String, length*2)
  2074. this->LoadHelperArgument(branchInstr, src1LengthOpnd);
  2075. this->LoadHelperArgument(branchInstr, src1FlatString);
  2076. this->LoadHelperArgument(branchInstr, src2FlatString);
  2077. IR::RegOpnd *dstOpnd = IR::RegOpnd::New(TyInt32, this->m_func);
  2078. IR::Instr *instrCall = IR::Instr::New(Js::OpCode::CALL, dstOpnd, IR::HelperCallOpnd::New(IR::HelperMemCmp, this->m_func), this->m_func);
  2079. branchInstr->InsertBefore(instrCall);
  2080. this->LowerCall(instrCall, 3);
  2081. // TEST eax, eax
  2082. IR::Instr *instrTest = IR::Instr::New(Js::OpCode::TEST, this->m_func);
  2083. instrTest->SetSrc1(instrCall->GetDst());
  2084. instrTest->SetSrc2(instrCall->GetDst());
  2085. instrInsert->InsertBefore(instrTest);
  2086. // JEQ success
  2087. instrInsert->InsertBefore(IR::BranchInstr::New(Js::OpCode::JEQ, labelBranchSuccess, this->m_func));
  2088. // JMP fail
  2089. instrInsert->InsertBefore(IR::BranchInstr::New(Js::OpCode::JMP, labelBranchFail, this->m_func));
  2090. branchInstr->InsertBefore(labelHelper);
  2091. IR::LabelInstr *labelFallthrough = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  2092. branchInstr->InsertAfter(labelFallthrough);
  2093. #if DBG
  2094. // The fast-path for strings assumes the case where 2 strings are equal is rare, and marks that path as 'helper'.
  2095. // This breaks the helper label dbchecks as it can result in non-helper blocks be reachable only from helper blocks.
  2096. // Use m_isHelperToNonHelperBranch and m_noHelperAssert to fix this.
  2097. IR::Instr *blockEndInstr;
  2098. if (isEqual)
  2099. {
  2100. blockEndInstr = labelHelper->GetNextBranchOrLabel();
  2101. }
  2102. else
  2103. {
  2104. blockEndInstr = branchInstr->GetNextBranchOrLabel();
  2105. }
  2106. if (blockEndInstr->IsBranchInstr())
  2107. {
  2108. blockEndInstr->AsBranchInstr()->m_isHelperToNonHelperBranch = true;
  2109. }
  2110. labelFallthrough->m_noHelperAssert = true;
  2111. #endif
  2112. return true;
  2113. }
  2114. ///----------------------------------------------------------------------------
  2115. ///
  2116. /// LowererMD::GenerateFastCmSrEqConst
  2117. ///
  2118. ///----------------------------------------------------------------------------
  2119. bool
  2120. LowererMD::GenerateFastCmSrEqConst(IR::Instr *instr)
  2121. {
  2122. //
  2123. // Given:
  2124. // s1 = CmSrEq_A s2, s3
  2125. // where either s2 or s3 is 'null', 'true' or 'false'
  2126. //
  2127. // Generate:
  2128. //
  2129. // CMP s2, s3
  2130. // JEQ $mov_true
  2131. // MOV s1, Library.GetFalse()
  2132. // JMP $done
  2133. // $mov_true:
  2134. // MOV s1, Library.GetTrue()
  2135. // $done:
  2136. //
  2137. Assert(m_lowerer->IsConstRegOpnd(instr->GetSrc2()->AsRegOpnd()));
  2138. IR::Opnd *opnd = instr->GetSrc1();
  2139. IR::RegOpnd *opndReg = instr->GetSrc2()->AsRegOpnd();
  2140. IR::LabelInstr *labelMovTrue = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  2141. IR::LabelInstr *labelDone = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  2142. if (!opnd->IsRegOpnd())
  2143. {
  2144. IR::RegOpnd *lhsReg = IR::RegOpnd::New(TyVar, m_func);
  2145. IR::Instr *mov = IR::Instr::New(Js::OpCode::MOV, lhsReg, opnd, m_func);
  2146. instr->InsertBefore(mov);
  2147. opnd = lhsReg;
  2148. }
  2149. Assert(opnd->IsRegOpnd());
  2150. // CMP s2, s3
  2151. // JEQ $mov_true
  2152. this->m_lowerer->InsertCompareBranch(opnd, opndReg->m_sym->GetConstOpnd(), Js::OpCode::BrEq_A, labelMovTrue, instr);
  2153. // MOV s1, 'false'
  2154. IR::Instr *instrMov = IR::Instr::New(Js::OpCode::MOV,
  2155. instr->GetDst(),
  2156. m_lowerer->LoadLibraryValueOpnd(instr, LibraryValue::ValueFalse),
  2157. m_func);
  2158. instr->InsertBefore(instrMov);
  2159. // JMP $done
  2160. IR::BranchInstr *jmp = IR::BranchInstr::New(Js::OpCode::JMP, labelDone, this->m_func);
  2161. instr->InsertBefore(jmp);
  2162. // $mov_true:
  2163. instr->InsertBefore(labelMovTrue);
  2164. // MOV s1, 'true'
  2165. instr->m_opcode = Js::OpCode::MOV;
  2166. instr->UnlinkSrc1();
  2167. instr->UnlinkSrc2();
  2168. instr->SetSrc1(m_lowerer->LoadLibraryValueOpnd(instr, LibraryValue::ValueTrue));
  2169. instr->ClearBailOutInfo();
  2170. Legalize(instr);
  2171. // $done:
  2172. instr->InsertAfter(labelDone);
  2173. return true;
  2174. }
  2175. ///----------------------------------------------------------------------------
  2176. ///
  2177. /// LowererMD::GenerateFastCmXxTaggedInt
  2178. ///
  2179. ///----------------------------------------------------------------------------
  2180. bool LowererMD::GenerateFastCmXxTaggedInt(IR::Instr *instr)
  2181. {
  2182. // The idea is to do an inline compare if we can prove that both sources
  2183. // are tagged ints (i.e., are vars with the low bit set).
  2184. //
  2185. // Given:
  2186. //
  2187. // Cmxx_A dst, src1, src2
  2188. //
  2189. // Generate:
  2190. //
  2191. // (If not Int31's, goto $helper)
  2192. // MOV r1, src1
  2193. // if (==, !=, !== or ===)
  2194. // SUB r1, src2
  2195. // NEG r1 // Sets CF if r1 != 0
  2196. // SBB r1, r1 // CF == 1 ? r1 = -1 : r1 = 0
  2197. // else
  2198. // MOV r2, 0
  2199. // CMP r1, src2
  2200. // SETcc r2
  2201. // DEC r2
  2202. // set r1 to r2
  2203. // AND r1, (notEqualResult - equalResult)
  2204. // ADD r1, equalResult
  2205. // MOV dst, r1
  2206. // JMP $fallthru
  2207. // $helper:
  2208. // (caller will generate normal helper call sequence)
  2209. // $fallthru:
  2210. IR::Opnd * src1 = instr->GetSrc1();
  2211. IR::Opnd * src2 = instr->GetSrc2();
  2212. IR::Opnd * dst = instr->GetDst();
  2213. IR::RegOpnd * r1 = IR::RegOpnd::New(TyMachReg, m_func);
  2214. IR::LabelInstr * helper = IR::LabelInstr::New(Js::OpCode::Label, m_func, true);
  2215. IR::LabelInstr * fallthru = IR::LabelInstr::New(Js::OpCode::Label, m_func);
  2216. Assert(src1 && src2 && dst);
  2217. // Not tagged ints?
  2218. if (src1->IsRegOpnd() && src1->AsRegOpnd()->IsNotInt())
  2219. {
  2220. return false;
  2221. }
  2222. if (src2->IsRegOpnd() && src2->AsRegOpnd()->IsNotInt())
  2223. {
  2224. return false;
  2225. }
  2226. bool isNeqOp = instr->m_opcode == Js::OpCode::CmSrNeq_A || instr->m_opcode == Js::OpCode::CmNeq_A;
  2227. Js::Var notEqualResult = m_func->GetScriptContext()->GetLibrary()->GetTrueOrFalse(isNeqOp);
  2228. Js::Var equalResult = m_func->GetScriptContext()->GetLibrary()->GetTrueOrFalse(!isNeqOp);
  2229. // Tagged ints?
  2230. bool isTaggedInts = false;
  2231. if (src1->IsTaggedInt())
  2232. {
  2233. if (src2->IsTaggedInt())
  2234. {
  2235. isTaggedInts = true;
  2236. }
  2237. }
  2238. if (!isTaggedInts)
  2239. {
  2240. this->GenerateSmIntPairTest(instr, src1, src2, helper);
  2241. }
  2242. // MOV r1, src1
  2243. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, r1, src1, m_func));
  2244. Js::OpCode setCC_Opcode = Js::OpCode::Nop;
  2245. switch(instr->m_opcode)
  2246. {
  2247. case Js::OpCode::CmSrEq_A:
  2248. case Js::OpCode::CmEq_A:
  2249. break;
  2250. case Js::OpCode::CmSrNeq_A:
  2251. case Js::OpCode::CmNeq_A:
  2252. break;
  2253. case Js::OpCode::CmGe_A:
  2254. setCC_Opcode = Js::OpCode::SETGE;
  2255. break;
  2256. case Js::OpCode::CmGt_A:
  2257. setCC_Opcode = Js::OpCode::SETG;
  2258. break;
  2259. case Js::OpCode::CmLe_A:
  2260. setCC_Opcode = Js::OpCode::SETLE;
  2261. break;
  2262. case Js::OpCode::CmLt_A:
  2263. setCC_Opcode = Js::OpCode::SETL;
  2264. break;
  2265. default:
  2266. Assume(UNREACHED);
  2267. }
  2268. if (setCC_Opcode == Js::OpCode::Nop)
  2269. {
  2270. // SUB r1, src2
  2271. IR::Instr * subInstr = IR::Instr::New(Js::OpCode::SUB, r1, r1, src2, m_func);
  2272. instr->InsertBefore(subInstr);
  2273. Legalize(subInstr); // src2 may need legalizing
  2274. // NEG r1
  2275. instr->InsertBefore(IR::Instr::New(Js::OpCode::NEG, r1, r1, m_func));
  2276. // SBB r1, r1
  2277. instr->InsertBefore(IR::Instr::New(Js::OpCode::SBB, r1, r1, r1, m_func));
  2278. }
  2279. else
  2280. {
  2281. IR::Instr *instrNew;
  2282. IR::RegOpnd *r2 = IR::RegOpnd::New(TyMachPtr, this->m_func);
  2283. // MOV r2, 0
  2284. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, r2, IR::IntConstOpnd::New(0, TyInt32, this->m_func), m_func));
  2285. // CMP r1, src2
  2286. IR::Opnd *r1_32 = r1->UseWithNewType(TyInt32, this->m_func);
  2287. IR::Opnd *src2_32 =src2->UseWithNewType(TyInt32, this->m_func);
  2288. instrNew = IR::Instr::New(Js::OpCode::CMP, m_func);
  2289. instrNew->SetSrc1(r1_32);
  2290. instrNew->SetSrc2(src2_32);
  2291. instr->InsertBefore(instrNew);
  2292. // SETcc r2
  2293. IR::RegOpnd *r2_i8 = (IR::RegOpnd*) r2->UseWithNewType(TyInt8, this->m_func);
  2294. instrNew = IR::Instr::New(setCC_Opcode, r2_i8, r2_i8, m_func);
  2295. instr->InsertBefore(instrNew);
  2296. // DEC r2
  2297. instr->InsertBefore(IR::Instr::New(Js::OpCode::DEC, r2, r2, m_func));
  2298. // r1 <- r2
  2299. r1 = r2;
  2300. }
  2301. // AND r1, (notEqualResult - equalResult)
  2302. {
  2303. IR::Instr * and = IR::Instr::New(Js::OpCode::AND, r1, r1, m_func);
  2304. and->SetSrc2(IR::AddrOpnd::New((void*)((size_t)notEqualResult - (size_t)equalResult), IR::AddrOpndKind::AddrOpndKindDynamicMisc, this->m_func));
  2305. instr->InsertBefore(and);
  2306. Legalize(and);
  2307. }
  2308. // ADD r1, equalResult
  2309. {
  2310. IR::Instr * add = IR::Instr::New(Js::OpCode::ADD, r1, r1, m_func);
  2311. add->SetSrc2(IR::AddrOpnd::New(equalResult, IR::AddrOpndKind::AddrOpndKindDynamicVar, this->m_func));
  2312. instr->InsertBefore(add);
  2313. Legalize(add);
  2314. }
  2315. // MOV dst, r1
  2316. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, dst, r1, m_func));
  2317. if (isTaggedInts)
  2318. {
  2319. instr->Remove();
  2320. return true;
  2321. }
  2322. // JMP $fallthru
  2323. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JMP, fallthru, m_func));
  2324. instr->InsertBefore(helper);
  2325. instr->InsertAfter(fallthru);
  2326. return false;
  2327. }
  2328. void LowererMD::GenerateFastCmXxR8(IR::Instr *instr)
  2329. {
  2330. GenerateFastCmXx(instr);
  2331. }
  2332. void LowererMD::GenerateFastCmXxI4(IR::Instr *instr)
  2333. {
  2334. GenerateFastCmXx(instr);
  2335. }
  2336. void LowererMD::GenerateFastCmXx(IR::Instr *instr)
  2337. {
  2338. // For float src:
  2339. // dst = MOV 0/1
  2340. // (U)COMISD src1, src2
  2341. // JP $done
  2342. // dst.i8 = SetCC dst.i8
  2343. // $done:
  2344. // for int src:
  2345. // CMP src1, src2
  2346. // dst = MOV 0 / false
  2347. // dst.i8 = SetCC dst.i8 / CMOCcc true
  2348. IR::Opnd * src1 = instr->UnlinkSrc1();
  2349. IR::Opnd * src2 = instr->UnlinkSrc2();
  2350. IR::Opnd * dst = instr->UnlinkDst();
  2351. IR::Opnd * tmp = dst;
  2352. bool isIntDst = dst->AsRegOpnd()->m_sym->IsInt32();
  2353. bool isFloatSrc = src1->IsFloat();
  2354. Assert(!isFloatSrc || src2->IsFloat());
  2355. Assert(!isFloatSrc || isIntDst);
  2356. Assert(!isFloatSrc || AutoSystemInfo::Data.SSE2Available());
  2357. IR::Opnd *opnd;
  2358. IR::Instr *newInstr;
  2359. Assert(src1->IsRegOpnd());
  2360. IR::Instr * done;
  2361. if (isFloatSrc)
  2362. {
  2363. done = IR::LabelInstr::New(Js::OpCode::Label, m_func);
  2364. instr->InsertBefore(done);
  2365. }
  2366. else
  2367. {
  2368. done = instr;
  2369. }
  2370. if (isIntDst)
  2371. {
  2372. // reg = MOV 0 will get peeped to XOR reg, reg which sets the flags.
  2373. // Put the MOV before the CMP, but use a tmp if dst == src1/src2
  2374. if (dst->IsEqual(src1) || dst->IsEqual(src2))
  2375. {
  2376. tmp = IR::RegOpnd::New(dst->GetType(), this->m_func);
  2377. }
  2378. // dst = MOV 0
  2379. if (isFloatSrc && instr->m_opcode == Js::OpCode::CmNeq_A)
  2380. {
  2381. opnd = IR::IntConstOpnd::New(1, TyInt32, this->m_func);
  2382. }
  2383. else
  2384. {
  2385. opnd = IR::IntConstOpnd::New(0, TyInt32, this->m_func);
  2386. }
  2387. newInstr = IR::Instr::New(Js::OpCode::MOV, tmp, opnd, this->m_func);
  2388. done->InsertBefore(newInstr);
  2389. }
  2390. Js::OpCode cmpOp;
  2391. if (isFloatSrc)
  2392. {
  2393. if (instr->m_opcode == Js::OpCode::CmEq_A || instr->m_opcode == Js::OpCode::CmNeq_A)
  2394. {
  2395. cmpOp = src1->IsFloat64() ? Js::OpCode::UCOMISD : Js::OpCode::UCOMISS;
  2396. }
  2397. else
  2398. {
  2399. cmpOp = src1->IsFloat64() ? Js::OpCode::COMISD : Js::OpCode::COMISS;
  2400. }
  2401. }
  2402. else
  2403. {
  2404. cmpOp = Js::OpCode::CMP;
  2405. }
  2406. // CMP src1, src2
  2407. newInstr = IR::Instr::New(cmpOp, this->m_func);
  2408. newInstr->SetSrc1(src1);
  2409. newInstr->SetSrc2(src2);
  2410. done->InsertBefore(newInstr);
  2411. if (isFloatSrc)
  2412. {
  2413. newInstr = IR::BranchInstr::New(Js::OpCode::JP, done->AsLabelInstr(), this->m_func);
  2414. done->InsertBefore(newInstr);
  2415. }
  2416. if (!isIntDst)
  2417. {
  2418. opnd = this->m_lowerer->LoadLibraryValueOpnd(instr, LibraryValue::ValueFalse);
  2419. LowererMD::CreateAssign(tmp, opnd, done);
  2420. }
  2421. Js::OpCode useCC;
  2422. switch(instr->m_opcode)
  2423. {
  2424. case Js::OpCode::CmEq_I4:
  2425. case Js::OpCode::CmEq_A:
  2426. useCC = isIntDst ? Js::OpCode::SETE : Js::OpCode::CMOVE;
  2427. break;
  2428. case Js::OpCode::CmNeq_I4:
  2429. case Js::OpCode::CmNeq_A:
  2430. useCC = isIntDst ? Js::OpCode::SETNE : Js::OpCode::CMOVNE;
  2431. break;
  2432. case Js::OpCode::CmGe_I4:
  2433. useCC = isIntDst ? Js::OpCode::SETGE : Js::OpCode::CMOVGE;
  2434. break;
  2435. case Js::OpCode::CmGt_I4:
  2436. useCC = isIntDst ? Js::OpCode::SETG : Js::OpCode::CMOVG;
  2437. break;
  2438. case Js::OpCode::CmLe_I4:
  2439. useCC = isIntDst ? Js::OpCode::SETLE : Js::OpCode::CMOVLE;
  2440. break;
  2441. case Js::OpCode::CmLt_I4:
  2442. useCC = isIntDst ? Js::OpCode::SETL : Js::OpCode::CMOVL;
  2443. break;
  2444. case Js::OpCode::CmUnGe_I4:
  2445. case Js::OpCode::CmGe_A:
  2446. useCC = isIntDst ? Js::OpCode::SETAE : Js::OpCode::CMOVAE;
  2447. break;
  2448. case Js::OpCode::CmUnGt_I4:
  2449. case Js::OpCode::CmGt_A:
  2450. useCC = isIntDst ? Js::OpCode::SETA : Js::OpCode::CMOVA;
  2451. break;
  2452. case Js::OpCode::CmUnLe_I4:
  2453. case Js::OpCode::CmLe_A:
  2454. useCC = isIntDst ? Js::OpCode::SETBE : Js::OpCode::CMOVBE;
  2455. break;
  2456. case Js::OpCode::CmUnLt_I4:
  2457. case Js::OpCode::CmLt_A:
  2458. useCC = isIntDst ? Js::OpCode::SETB : Js::OpCode::CMOVB;
  2459. break;
  2460. default:
  2461. useCC = Js::OpCode::InvalidOpCode;
  2462. Assume(UNREACHED);
  2463. }
  2464. if (isIntDst)
  2465. {
  2466. // tmp.i8 = SetCC tmp.i8
  2467. IR::Opnd *tmp_i8 = tmp->UseWithNewType(TyInt8, this->m_func);
  2468. newInstr = IR::Instr::New(useCC, tmp_i8, tmp_i8, this->m_func);
  2469. }
  2470. else
  2471. {
  2472. // regTrue = MOV true
  2473. IR::Opnd *regTrue = IR::RegOpnd::New(TyMachPtr, this->m_func);
  2474. Lowerer::InsertMove(regTrue, this->m_lowerer->LoadLibraryValueOpnd(instr, LibraryValue::ValueTrue), done);
  2475. // tmp = CMOVcc tmp, regTrue
  2476. newInstr = IR::Instr::New(useCC, tmp, tmp, regTrue, this->m_func);
  2477. }
  2478. done->InsertBefore(newInstr);
  2479. if (tmp != dst)
  2480. {
  2481. newInstr = IR::Instr::New(Js::OpCode::MOV, dst, tmp, this->m_func);
  2482. instr->InsertBefore(newInstr);
  2483. }
  2484. instr->Remove();
  2485. }
  2486. IR::Instr * LowererMD::GenerateConvBool(IR::Instr *instr)
  2487. {
  2488. // TEST src1, src1
  2489. // dst = MOV true
  2490. // rf = MOV false
  2491. // dst = CMOV dst, rf
  2492. IR::Instr *instrNew, *instrFirst;
  2493. IR::RegOpnd *dst = instr->GetDst()->AsRegOpnd();
  2494. IR::RegOpnd *regFalse;
  2495. // TEST src1, src2
  2496. instrFirst = instrNew = IR::Instr::New(Js::OpCode::TEST, this->m_func);
  2497. instrNew->SetSrc1(instr->GetSrc1());
  2498. instrNew->SetSrc2(instr->GetSrc1());
  2499. instr->InsertBefore(instrNew);
  2500. // dst = MOV true
  2501. Lowerer::InsertMove(dst, this->m_lowerer->LoadLibraryValueOpnd(instr, LibraryValue::ValueTrue), instr);
  2502. // rf = MOV false
  2503. regFalse = IR::RegOpnd::New(TyMachPtr, this->m_func);
  2504. Lowerer::InsertMove(regFalse, this->m_lowerer->LoadLibraryValueOpnd(instr, LibraryValue::ValueFalse), instr);
  2505. // Add dst as src1 of CMOV to create a pseudo use of dst. Otherwise, the register allocator
  2506. // won't know the previous dst is needed. and needed in the same register as the dst of the CMOV.
  2507. // dst = CMOV dst, rf
  2508. instrNew = IR::Instr::New(Js::OpCode::CMOVE, dst, dst, regFalse, this->m_func);
  2509. instr->InsertBefore(instrNew);
  2510. instr->Remove();
  2511. return instrFirst;
  2512. }
  2513. ///----------------------------------------------------------------------------
  2514. ///
  2515. /// LowererMD::GenerateFastAdd
  2516. ///
  2517. /// NOTE: We assume that only the sum of two Int31's will have 0x2 set. This
  2518. /// is only true until we have a var type with tag == 0x2.
  2519. ///
  2520. ///----------------------------------------------------------------------------
  2521. bool
  2522. LowererMD::GenerateFastAdd(IR::Instr * instrAdd)
  2523. {
  2524. // Given:
  2525. //
  2526. // dst = Add src1, src2
  2527. //
  2528. // Generate:
  2529. //
  2530. // (If not 2 Int31's, jump to $helper.)
  2531. // s1 = MOV src1
  2532. // s1 = DEC s1 -- Get rid of one of the tag [Int31 only]
  2533. // s1 = ADD s1, src2 -- try an inline add
  2534. // JO $helper -- bail if the add overflowed
  2535. // s1 = OR s1, AtomTag_IntPtr [Int32 only]
  2536. // dst = MOV s1
  2537. // JMP $fallthru
  2538. // $helper:
  2539. // (caller generates helper call)
  2540. // $fallthru:
  2541. IR::Instr * instr;
  2542. IR::LabelInstr * labelHelper;
  2543. IR::LabelInstr * labelFallThru;
  2544. IR::Opnd * opndReg;
  2545. IR::Opnd * opndSrc1;
  2546. IR::Opnd * opndSrc2;
  2547. opndSrc1 = instrAdd->GetSrc1();
  2548. opndSrc2 = instrAdd->GetSrc2();
  2549. AssertMsg(opndSrc1 && opndSrc2, "Expected 2 src opnd's on Add instruction");
  2550. // Generate fastpath for Incr_A anyway -
  2551. // Incrementing strings representing integers can be inter-mixed with integers e.g. "1"++ -> converts 1 to an int and thereafter, integer increment is expected.
  2552. if (opndSrc1->IsRegOpnd() && (opndSrc1->AsRegOpnd()->IsNotInt() || opndSrc1->GetValueType().IsString()
  2553. || (instrAdd->m_opcode != Js::OpCode::Incr_A && opndSrc1->GetValueType().IsLikelyString())))
  2554. {
  2555. return false;
  2556. }
  2557. if (opndSrc2->IsRegOpnd() && (opndSrc2->AsRegOpnd()->IsNotInt() ||
  2558. opndSrc2->GetValueType().IsLikelyString()))
  2559. {
  2560. return false;
  2561. }
  2562. // Tagged ints?
  2563. bool isTaggedInts = false;
  2564. if (opndSrc1->IsTaggedInt())
  2565. {
  2566. if (opndSrc2->IsTaggedInt())
  2567. {
  2568. isTaggedInts = true;
  2569. }
  2570. }
  2571. labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  2572. if (!isTaggedInts)
  2573. {
  2574. // (If not 2 Int31's, jump to $helper.)
  2575. this->GenerateSmIntPairTest(instrAdd, opndSrc1, opndSrc2, labelHelper);
  2576. }
  2577. if (opndSrc1->IsAddrOpnd())
  2578. {
  2579. // If opnd1 is a constant, just swap them.
  2580. IR::Opnd *opndTmp = opndSrc1;
  2581. opndSrc1 = opndSrc2;
  2582. opndSrc2 = opndTmp;
  2583. }
  2584. //
  2585. // For 32 bit arithmetic we copy them and set the size of operands to be 32 bits. This is
  2586. // relevant only on AMD64.
  2587. //
  2588. opndSrc1 = opndSrc1->UseWithNewType(TyInt32, this->m_func);
  2589. // s1 = MOV src1
  2590. opndReg = IR::RegOpnd::New(TyInt32, this->m_func);
  2591. instr = IR::Instr::New(Js::OpCode::MOV, opndReg, opndSrc1, this->m_func);
  2592. instrAdd->InsertBefore(instr);
  2593. #if !INT32VAR
  2594. // Do the DEC in place
  2595. if (opndSrc2->IsAddrOpnd())
  2596. {
  2597. Assert(opndSrc2->AsAddrOpnd()->GetAddrOpndKind() == IR::AddrOpndKindConstantVar);
  2598. opndSrc2 = IR::IntConstOpnd::New(*((int *)&(opndSrc2->AsAddrOpnd()->m_address)) - 1, TyInt32, this->m_func, opndSrc2->AsAddrOpnd()->m_dontEncode);
  2599. opndSrc2 = opndSrc2->Use(this->m_func);
  2600. }
  2601. else if (opndSrc2->IsIntConstOpnd())
  2602. {
  2603. Assert(opndSrc2->GetType() == TyInt32);
  2604. opndSrc2 = opndSrc2->Use(this->m_func);
  2605. opndSrc2->AsIntConstOpnd()->DecrValue(1);
  2606. }
  2607. else
  2608. {
  2609. // s1 = DEC s1
  2610. opndSrc2 = opndSrc2->UseWithNewType(TyInt32, this->m_func);
  2611. instr = IR::Instr::New(Js::OpCode::DEC, opndReg, opndReg, this->m_func);
  2612. instrAdd->InsertBefore(instr);
  2613. }
  2614. instr = IR::Instr::New(Js::OpCode::ADD, opndReg, opndReg, opndSrc2, this->m_func);
  2615. #else
  2616. if (opndSrc2->IsAddrOpnd())
  2617. {
  2618. // truncate to untag
  2619. int value = ::Math::PointerCastToIntegralTruncate<int>(opndSrc2->AsAddrOpnd()->m_address);
  2620. if (value == 1)
  2621. {
  2622. instr = IR::Instr::New(Js::OpCode::INC, opndReg, opndReg, this->m_func);
  2623. }
  2624. else
  2625. {
  2626. opndSrc2 = IR::IntConstOpnd::New(value, TyInt32, this->m_func);
  2627. instr = IR::Instr::New(Js::OpCode::ADD, opndReg, opndReg, opndSrc2, this->m_func);
  2628. }
  2629. }
  2630. else
  2631. {
  2632. instr = IR::Instr::New(Js::OpCode::ADD, opndReg, opndReg, opndSrc2, this->m_func);
  2633. }
  2634. #endif
  2635. // s1 = ADD s1, src2
  2636. instrAdd->InsertBefore(instr);
  2637. Legalize(instr);
  2638. // JO $helper
  2639. instr = IR::BranchInstr::New(Js::OpCode::JO, labelHelper, this->m_func);
  2640. instrAdd->InsertBefore(instr);
  2641. //
  2642. // Convert TyInt32 operand, back to TyMachPtr type.
  2643. //
  2644. if(TyMachReg != opndReg->GetType())
  2645. {
  2646. opndReg = opndReg->UseWithNewType(TyMachPtr, this->m_func);
  2647. }
  2648. #if INT32VAR
  2649. // s1 = OR s1, AtomTag_IntPtr
  2650. GenerateInt32ToVarConversion(opndReg, instrAdd);
  2651. #endif
  2652. // dst = MOV s1
  2653. instr = IR::Instr::New(Js::OpCode::MOV, instrAdd->GetDst(), opndReg, this->m_func);
  2654. instrAdd->InsertBefore(instr);
  2655. // JMP $fallthru
  2656. labelFallThru = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  2657. instr = IR::BranchInstr::New(Js::OpCode::JMP, labelFallThru, this->m_func);
  2658. instrAdd->InsertBefore(instr);
  2659. // $helper:
  2660. // (caller generates helper call)
  2661. // $fallthru:
  2662. instrAdd->InsertBefore(labelHelper);
  2663. instrAdd->InsertAfter(labelFallThru);
  2664. return true;
  2665. }
  2666. ///----------------------------------------------------------------------------
  2667. ///
  2668. /// LowererMD::GenerateFastSub
  2669. ///
  2670. ///
  2671. ///----------------------------------------------------------------------------
  2672. bool
  2673. LowererMD::GenerateFastSub(IR::Instr * instrSub)
  2674. {
  2675. // Given:
  2676. //
  2677. // dst = Sub src1, src2
  2678. //
  2679. // Generate:
  2680. //
  2681. // (If not 2 Int31's, jump to $helper.)
  2682. // s1 = MOV src1
  2683. // s1 = SUB s1, src2 -- try an inline sub
  2684. // JO $helper -- bail if the subtract overflowed
  2685. // JNE $helper
  2686. // s1 = INC s1 -- restore the var tag on the result [Int31 only]
  2687. // s1 = OR s1, AtomTag_IntPtr [Int32 only]
  2688. // dst = MOV s1
  2689. // JMP $fallthru
  2690. // $helper:
  2691. // (caller generates helper call)
  2692. // $fallthru:
  2693. IR::Instr * instr;
  2694. IR::LabelInstr * labelHelper;
  2695. IR::LabelInstr * labelFallThru;
  2696. IR::Opnd * opndReg;
  2697. IR::Opnd * opndSrc1;
  2698. IR::Opnd * opndSrc2;
  2699. opndSrc1 = instrSub->GetSrc1();
  2700. opndSrc2 = instrSub->GetSrc2();
  2701. AssertMsg(opndSrc1 && opndSrc2, "Expected 2 src opnd's on Sub instruction");
  2702. // Not tagged ints?
  2703. if (opndSrc1->IsRegOpnd() && opndSrc1->AsRegOpnd()->IsNotInt())
  2704. {
  2705. return false;
  2706. }
  2707. if (opndSrc2->IsRegOpnd() && opndSrc2->AsRegOpnd()->IsNotInt())
  2708. {
  2709. return false;
  2710. }
  2711. // Tagged ints?
  2712. bool isTaggedInts = false;
  2713. if (opndSrc1->IsTaggedInt())
  2714. {
  2715. if (opndSrc2->IsTaggedInt())
  2716. {
  2717. isTaggedInts = true;
  2718. }
  2719. }
  2720. labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  2721. if (!isTaggedInts)
  2722. {
  2723. // (If not 2 Int31's, jump to $helper.)
  2724. this->GenerateSmIntPairTest(instrSub, opndSrc1, opndSrc2, labelHelper);
  2725. }
  2726. //
  2727. // For 32 bit arithmetic we copy them and set the size of operands to be 32 bits. This is
  2728. // relevant only on AMD64.
  2729. //
  2730. opndSrc1 = opndSrc1->UseWithNewType(TyInt32, this->m_func);
  2731. opndSrc2 = opndSrc2->UseWithNewType(TyInt32, this->m_func);
  2732. // s1 = MOV src1
  2733. opndReg = IR::RegOpnd::New(TyInt32, this->m_func);
  2734. instr = IR::Instr::New(Js::OpCode::MOV, opndReg, opndSrc1, this->m_func);
  2735. instrSub->InsertBefore(instr);
  2736. // s1 = SUB s1, src2
  2737. instr = IR::Instr::New(Js::OpCode::SUB, opndReg, opndReg, opndSrc2, this->m_func);
  2738. instrSub->InsertBefore(instr);
  2739. // JO $helper
  2740. instr = IR::BranchInstr::New(Js::OpCode::JO, labelHelper, this->m_func);
  2741. instrSub->InsertBefore(instr);
  2742. #if !INT32VAR
  2743. // s1 = INC s1
  2744. instr = IR::Instr::New(Js::OpCode::INC, opndReg, opndReg, this->m_func);
  2745. instrSub->InsertBefore(instr);
  2746. #endif
  2747. //
  2748. // Convert TyInt32 operand, back to TyMachPtr type.
  2749. //
  2750. if(TyMachReg != opndReg->GetType())
  2751. {
  2752. opndReg = opndReg->UseWithNewType(TyMachPtr, this->m_func);
  2753. }
  2754. #if INT32VAR
  2755. // s1 = OR s1, AtomTag_IntPtr
  2756. GenerateInt32ToVarConversion(opndReg, instrSub);
  2757. #endif
  2758. // dst = MOV s1
  2759. instr = IR::Instr::New(Js::OpCode::MOV, instrSub->GetDst(), opndReg, this->m_func);
  2760. instrSub->InsertBefore(instr);
  2761. // JMP $fallthru
  2762. labelFallThru = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  2763. instr = IR::BranchInstr::New(Js::OpCode::JMP, labelFallThru, this->m_func);
  2764. instrSub->InsertBefore(instr);
  2765. // $helper:
  2766. // (caller generates helper call)
  2767. // $fallthru:
  2768. instrSub->InsertBefore(labelHelper);
  2769. instrSub->InsertAfter(labelFallThru);
  2770. return true;
  2771. }
  2772. ///----------------------------------------------------------------------------
  2773. ///
  2774. /// LowererMD::GenerateFastMul
  2775. ///
  2776. ///----------------------------------------------------------------------------
  2777. bool
  2778. LowererMD::GenerateFastMul(IR::Instr * instrMul)
  2779. {
  2780. // Given:
  2781. //
  2782. // dst = Mul src1, src2
  2783. //
  2784. // Generate:
  2785. //
  2786. // (If not 2 Int31's, jump to $helper.)
  2787. // s1 = MOV src1
  2788. // s1 = DEC s1 -- clear the var tag from the value to be multiplied [Int31 only]
  2789. // s2 = MOV src2
  2790. // s2 = SAR s2, Js::VarTag_Shift -- extract the real src2 amount from the var [Int31 only]
  2791. // s1 = IMUL s1, s2 -- do the signed mul
  2792. // JO $helper -- bail if the result overflowed
  2793. // s3 = MOV s1
  2794. // TEST s3, s3 -- Check result is 0. might be -0. Result is -0 when a negative number is multiplied with 0.
  2795. // JEQ $zero
  2796. // JMP $nonzero
  2797. // $zero: -- result of mul was 0. try to check for -0
  2798. // s2 = ADD s2, src1 -- Add src1 to s2
  2799. // JGT $nonzero -- positive 0. [Int31 only]
  2800. // JGE $nonzero -- positive 0. [Int32 only]
  2801. // dst = ToVar(-0.0) -- load negative 0
  2802. // JMP $fallthru
  2803. // $nonzero:
  2804. // s3 = INC s3 -- restore the var tag on the result [Int31 only]
  2805. // s3 = OR s3, AtomTag_IntPtr [Int32 only]
  2806. // dst= MOV s3
  2807. // JMP $fallthru
  2808. // $helper:
  2809. // (caller generates helper call)
  2810. // $fallthru:
  2811. IR::LabelInstr * labelHelper;
  2812. IR::LabelInstr * labelFallThru;
  2813. IR::LabelInstr * labelNonZero;
  2814. IR::Instr * instr;
  2815. IR::RegOpnd * opndReg1;
  2816. IR::RegOpnd * opndReg2;
  2817. IR::RegOpnd * s3;
  2818. IR::Opnd * opndSrc1;
  2819. IR::Opnd * opndSrc2;
  2820. opndSrc1 = instrMul->GetSrc1();
  2821. opndSrc2 = instrMul->GetSrc2();
  2822. AssertMsg(opndSrc1 && opndSrc2, "Expected 2 src opnd's on mul instruction");
  2823. if (opndSrc1->IsRegOpnd() && opndSrc1->AsRegOpnd()->IsNotInt())
  2824. {
  2825. return true;
  2826. }
  2827. if (opndSrc2->IsRegOpnd() && opndSrc2->AsRegOpnd()->IsNotInt())
  2828. {
  2829. return true;
  2830. }
  2831. // (If not 2 Int31's, jump to $helper.)
  2832. labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  2833. labelNonZero = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  2834. labelFallThru = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  2835. this->GenerateSmIntPairTest(instrMul, opndSrc1, opndSrc2, labelHelper);
  2836. //
  2837. // For 32 bit arithmetic we copy them and set the size of operands to be 32 bits. This is
  2838. // relevant only on AMD64.
  2839. //
  2840. opndSrc1 = opndSrc1->UseWithNewType(TyInt32, this->m_func);
  2841. opndSrc2 = opndSrc2->UseWithNewType(TyInt32, this->m_func);
  2842. if (opndSrc1->IsImmediateOpnd())
  2843. {
  2844. IR::Opnd * temp = opndSrc1;
  2845. opndSrc1 = opndSrc2;
  2846. opndSrc2 = temp;
  2847. }
  2848. // s1 = MOV src1
  2849. opndReg1 = IR::RegOpnd::New(TyInt32, this->m_func);
  2850. instr = IR::Instr::New(Js::OpCode::MOV, opndReg1, opndSrc1, this->m_func);
  2851. instrMul->InsertBefore(instr);
  2852. #if !INT32VAR
  2853. // s1 = DEC s1
  2854. instr = IR::Instr::New(Js::OpCode::DEC, opndReg1, opndReg1, this->m_func);
  2855. instrMul->InsertBefore(instr);
  2856. #endif
  2857. if (opndSrc2->IsImmediateOpnd())
  2858. {
  2859. Assert(opndSrc2->IsAddrOpnd() && opndSrc2->AsAddrOpnd()->IsVar());
  2860. IR::Opnd *opnd2 = IR::IntConstOpnd::New(Js::TaggedInt::ToInt32(opndSrc2->AsAddrOpnd()->m_address), TyInt32, this->m_func);
  2861. // s2 = MOV src2
  2862. opndReg2 = IR::RegOpnd::New(TyInt32, this->m_func);
  2863. instr = IR::Instr::New(Js::OpCode::MOV, opndReg2, opnd2, this->m_func);
  2864. instrMul->InsertBefore(instr);
  2865. }
  2866. else
  2867. {
  2868. // s2 = MOV src2
  2869. opndReg2 = IR::RegOpnd::New(TyInt32, this->m_func);
  2870. instr = IR::Instr::New(Js::OpCode::MOV, opndReg2, opndSrc2, this->m_func);
  2871. instrMul->InsertBefore(instr);
  2872. #if !INT32VAR
  2873. // s2 = SAR s2, Js::VarTag_Shift
  2874. instr = IR::Instr::New(
  2875. Js::OpCode::SAR, opndReg2, opndReg2,
  2876. IR::IntConstOpnd::New(Js::VarTag_Shift, TyInt8, this->m_func), this->m_func);
  2877. instrMul->InsertBefore(instr);
  2878. #endif
  2879. }
  2880. // s1 = IMUL s1, s2
  2881. instr = IR::Instr::New(Js::OpCode::IMUL2, opndReg1, opndReg1, opndReg2, this->m_func);
  2882. instrMul->InsertBefore(instr);
  2883. // JO $helper
  2884. instr = IR::BranchInstr::New(Js::OpCode::JO, labelHelper, this->m_func);
  2885. instrMul->InsertBefore(instr);
  2886. // MOV s3, s1
  2887. s3 = IR::RegOpnd::New(TyInt32, this->m_func);
  2888. instr = IR::Instr::New(Js::OpCode::MOV, s3, opndReg1, this->m_func);
  2889. instrMul->InsertBefore(instr);
  2890. // TEST s3, s3
  2891. instr = IR::Instr::New(Js::OpCode::TEST, this->m_func);
  2892. instr->SetSrc1(s3);
  2893. instr->SetSrc2(s3);
  2894. instrMul->InsertBefore(instr);
  2895. // JEQ $zero
  2896. IR::LabelInstr *labelZero = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  2897. instr = IR::BranchInstr::New(Js::OpCode::JEQ, labelZero, this->m_func);
  2898. instrMul->InsertBefore(instr);
  2899. // JMP $nonzero
  2900. instr = IR::BranchInstr::New(Js::OpCode::JMP, labelNonZero, this->m_func);
  2901. instrMul->InsertBefore(instr);
  2902. // $zero:
  2903. instrMul->InsertBefore(labelZero);
  2904. // s2 = ADD s2, src1
  2905. instr = IR::Instr::New(Js::OpCode::ADD, opndReg2, opndReg2, opndSrc1, this->m_func);
  2906. instrMul->InsertBefore(instr);
  2907. Legalize(instr);
  2908. // JGT $nonzero
  2909. #if INT32VAR
  2910. Js::OpCode greaterOpCode = Js::OpCode::JGE;
  2911. #else
  2912. Js::OpCode greaterOpCode = Js::OpCode::JGT;
  2913. #endif
  2914. instr = IR::BranchInstr::New(greaterOpCode, labelNonZero, this->m_func);
  2915. instrMul->InsertBefore(instr);
  2916. // dst = ToVar(-0.0) -- load negative 0
  2917. instr = IR::Instr::New(Js::OpCode::MOV, instrMul->GetDst(), m_lowerer->LoadLibraryValueOpnd(instrMul, LibraryValue::ValueNegativeZero), this->m_func);
  2918. instrMul->InsertBefore(instr);
  2919. // JMP $fallthru
  2920. instr = IR::BranchInstr::New(Js::OpCode::JMP, labelFallThru, this->m_func);
  2921. instrMul->InsertBefore(instr);
  2922. // $nonzero:
  2923. instrMul->InsertBefore(labelNonZero);
  2924. #if !INT32VAR
  2925. // s3 = INC s3
  2926. instr = IR::Instr::New(Js::OpCode::INC, s3, s3, this->m_func);
  2927. instrMul->InsertBefore(instr);
  2928. #endif
  2929. //
  2930. // Convert TyInt32 operand, back to TyMachPtr type.
  2931. // Cast is fine. We know ChangeType returns IR::Opnd * but it
  2932. // preserves the Type.
  2933. //
  2934. if(TyMachReg != s3->GetType())
  2935. {
  2936. s3 = static_cast<IR::RegOpnd *>(s3->UseWithNewType(TyMachPtr, this->m_func));
  2937. }
  2938. #if INT32VAR
  2939. // s3 = OR s3, AtomTag_IntPtr
  2940. GenerateInt32ToVarConversion(s3, instrMul);
  2941. #endif
  2942. // dst = MOV s3
  2943. instr = IR::Instr::New(Js::OpCode::MOV, instrMul->GetDst(), s3, this->m_func);
  2944. instrMul->InsertBefore(instr);
  2945. // JMP $fallthru
  2946. instr = IR::BranchInstr::New(Js::OpCode::JMP, labelFallThru, this->m_func);
  2947. instrMul->InsertBefore(instr);
  2948. // $helper:
  2949. // (caller generates helper call)
  2950. // $fallthru:
  2951. instrMul->InsertBefore(labelHelper);
  2952. instrMul->InsertAfter(labelFallThru);
  2953. return true;
  2954. }
  2955. bool
  2956. LowererMD::GenerateFastNeg(IR::Instr * instrNeg)
  2957. {
  2958. // Given:
  2959. //
  2960. // dst = Not src
  2961. //
  2962. // Generate:
  2963. //
  2964. // if not int, jump $helper
  2965. // if src == 0 -- test for zero (must be handled by the runtime to preserve
  2966. // JEQ $helper difference btw +0 and -0)
  2967. // dst = MOV src
  2968. // dst = NEG dst -- do an inline NEG
  2969. // dst = ADD dst, 2 -- restore the var tag on the result [int31 only]
  2970. // JO $helper
  2971. // dst = OR dst, AtomTag_Ptr [int32 only]
  2972. // JMP $fallthru
  2973. // $helper:
  2974. // (caller generates helper call)
  2975. // $fallthru:
  2976. IR::Instr * instr;
  2977. IR::LabelInstr * labelHelper = nullptr;
  2978. IR::LabelInstr * labelFallThru = nullptr;
  2979. IR::Opnd * opndSrc1;
  2980. IR::Opnd * opndDst;
  2981. bool usingNewDst = false;
  2982. opndSrc1 = instrNeg->GetSrc1();
  2983. AssertMsg(opndSrc1, "Expected src opnd on Neg instruction");
  2984. if(opndSrc1->IsEqual(instrNeg->GetDst()))
  2985. {
  2986. usingNewDst = true;
  2987. opndDst = IR::RegOpnd::New(TyInt32, this->m_func);
  2988. }
  2989. else
  2990. {
  2991. opndDst = instrNeg->GetDst()->UseWithNewType(TyInt32, this->m_func);
  2992. }
  2993. if (opndSrc1->IsRegOpnd() && opndSrc1->AsRegOpnd()->m_sym->IsIntConst())
  2994. {
  2995. IR::Opnd *newOpnd;
  2996. IntConstType value = opndSrc1->AsRegOpnd()->m_sym->GetIntConstValue();
  2997. if (value == 0)
  2998. {
  2999. // If the negate operand is zero, the result is -0.0, which is a Number rather than an Int31.
  3000. newOpnd = m_lowerer->LoadLibraryValueOpnd(instrNeg, LibraryValue::ValueNegativeZero);
  3001. }
  3002. else
  3003. {
  3004. // negation below can overflow because max negative int32 value > max positive value by 1.
  3005. newOpnd = IR::AddrOpnd::NewFromNumber(-(int64)value, m_func);
  3006. }
  3007. instrNeg->ClearBailOutInfo();
  3008. instrNeg->FreeSrc1();
  3009. instrNeg->SetSrc1(newOpnd);
  3010. instrNeg = this->ChangeToAssign(instrNeg);
  3011. // Skip lowering call to helper
  3012. return false;
  3013. }
  3014. bool isInt = (opndSrc1->IsTaggedInt());
  3015. if (opndSrc1->IsRegOpnd() && opndSrc1->AsRegOpnd()->IsNotInt())
  3016. {
  3017. return true;
  3018. }
  3019. labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  3020. if (!isInt)
  3021. {
  3022. GenerateSmIntTest(opndSrc1, instrNeg, labelHelper);
  3023. }
  3024. //
  3025. // For 32 bit arithmetic we copy them and set the size of operands to be 32 bits. This is
  3026. // relevant only on AMD64.
  3027. //
  3028. opndSrc1 = opndSrc1->UseWithNewType(TyInt32, this->m_func);
  3029. GenerateTaggedZeroTest(opndSrc1, instrNeg, labelHelper);
  3030. // dst = MOV src
  3031. instr = IR::Instr::New(Js::OpCode::MOV, opndDst, opndSrc1, this->m_func);
  3032. instrNeg->InsertBefore(instr);
  3033. // dst = NEG dst
  3034. instr = IR::Instr::New(Js::OpCode::NEG, opndDst, opndDst, this->m_func);
  3035. instrNeg->InsertBefore(instr);
  3036. #if !INT32VAR
  3037. // dst = ADD dst, 2
  3038. instr = IR::Instr::New(Js::OpCode::ADD, opndDst, opndDst, IR::IntConstOpnd::New(2, TyInt32, this->m_func), this->m_func);
  3039. instrNeg->InsertBefore(instr);
  3040. #endif
  3041. // JO $helper
  3042. instr = IR::BranchInstr::New(Js::OpCode::JO, labelHelper, this->m_func);
  3043. instrNeg->InsertBefore(instr);
  3044. //
  3045. // Convert TyInt32 operand, back to TyMachPtr type.
  3046. //
  3047. if(TyMachReg != opndDst->GetType())
  3048. {
  3049. opndDst = opndDst->UseWithNewType(TyMachPtr, this->m_func);
  3050. }
  3051. #if INT32VAR
  3052. GenerateInt32ToVarConversion(opndDst, instrNeg);
  3053. #endif
  3054. if(usingNewDst)
  3055. {
  3056. instr = IR::Instr::New(Js::OpCode::MOV, instrNeg->GetDst(), opndDst, this->m_func);
  3057. instrNeg->InsertBefore(instr);
  3058. }
  3059. // JMP $fallthru
  3060. labelFallThru = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  3061. instr = IR::BranchInstr::New(Js::OpCode::JMP, labelFallThru, this->m_func);
  3062. instrNeg->InsertBefore(instr);
  3063. // $helper:
  3064. // (caller generates helper sequence)
  3065. // $fallthru:
  3066. AssertMsg(labelHelper, "Should not be NULL");
  3067. instrNeg->InsertBefore(labelHelper);
  3068. instrNeg->InsertAfter(labelFallThru);
  3069. return true;
  3070. }
  3071. void
  3072. LowererMD::GenerateFastBrS(IR::BranchInstr *brInstr)
  3073. {
  3074. IR::Opnd *src1 = brInstr->UnlinkSrc1();
  3075. Assert(src1->IsIntConstOpnd() || src1->IsAddrOpnd() || src1->IsRegOpnd());
  3076. IR::Instr *cmpInstr = IR::Instr::New(Js::OpCode::TEST, this->m_func);
  3077. cmpInstr->SetSrc1(m_lowerer->LoadOptimizationOverridesValueOpnd(brInstr, OptimizationOverridesValue::OptimizationOverridesSideEffects));
  3078. cmpInstr->SetSrc2(src1);
  3079. brInstr->InsertBefore(cmpInstr);
  3080. Legalize(cmpInstr);
  3081. Js::OpCode opcode;
  3082. switch(brInstr->m_opcode)
  3083. {
  3084. case Js::OpCode::BrHasSideEffects:
  3085. opcode = Js::OpCode::JNE;
  3086. break;
  3087. case Js::OpCode::BrNotHasSideEffects:
  3088. opcode = Js::OpCode::JEQ;
  3089. break;
  3090. default:
  3091. Assert(UNREACHED);
  3092. __assume(false);
  3093. }
  3094. brInstr->m_opcode = opcode;
  3095. }
  3096. ///----------------------------------------------------------------------------
  3097. ///
  3098. /// LowererMD::GenerateSmIntPairTest
  3099. ///
  3100. /// Generate code to test whether the given operands are both Int31 vars
  3101. /// and branch to the given label if not.
  3102. ///
  3103. ///----------------------------------------------------------------------------
  3104. #if !INT32VAR
  3105. IR::Instr *
  3106. LowererMD::GenerateSmIntPairTest(
  3107. IR::Instr * instrInsert,
  3108. IR::Opnd * opndSrc1,
  3109. IR::Opnd * opndSrc2,
  3110. IR::LabelInstr * labelFail)
  3111. {
  3112. IR::Opnd * opndReg;
  3113. IR::Instr * instrPrev = instrInsert->m_prev;
  3114. IR::Instr * instr;
  3115. Assert(opndSrc1->GetType() == TyVar);
  3116. Assert(opndSrc2->GetType() == TyVar);
  3117. if (opndSrc1->IsTaggedInt())
  3118. {
  3119. IR::Opnd *tempOpnd = opndSrc1;
  3120. opndSrc1 = opndSrc2;
  3121. opndSrc2 = tempOpnd;
  3122. }
  3123. if (opndSrc2->IsTaggedInt())
  3124. {
  3125. if (opndSrc1->IsTaggedInt())
  3126. {
  3127. return instrPrev;
  3128. }
  3129. // TEST src1, AtomTag
  3130. // JEQ $fail
  3131. instr = IR::Instr::New(Js::OpCode::TEST, this->m_func);
  3132. instr->SetSrc1(opndSrc1);
  3133. instr->SetSrc2(IR::IntConstOpnd::New(Js::AtomTag, TyInt8, this->m_func));
  3134. instrInsert->InsertBefore(instr);
  3135. }
  3136. else
  3137. {
  3138. // s1 = MOV src1
  3139. // s1 = AND s1, 1
  3140. // TEST s1, src2
  3141. // JEQ $fail
  3142. // s1 = MOV src1
  3143. opndReg = IR::RegOpnd::New(TyMachReg, this->m_func);
  3144. instr = IR::Instr::New(Js::OpCode::MOV, opndReg, opndSrc1, this->m_func);
  3145. instrInsert->InsertBefore(instr);
  3146. // s1 = AND s1, AtomTag
  3147. instr = IR::Instr::New(
  3148. Js::OpCode::AND, opndReg, opndReg, IR::IntConstOpnd::New(Js::AtomTag, TyInt8, this->m_func), this->m_func);
  3149. instrInsert->InsertBefore(instr);
  3150. // TEST s1, src2
  3151. instr = IR::Instr::New(Js::OpCode::TEST, this->m_func);
  3152. instr->SetSrc1(opndReg);
  3153. instr->SetSrc2(opndSrc2);
  3154. instrInsert->InsertBefore(instr);
  3155. }
  3156. // JEQ $fail
  3157. instr = IR::BranchInstr::New(Js::OpCode::JEQ, labelFail, this->m_func);
  3158. instrInsert->InsertBefore(instr);
  3159. return instrPrev;
  3160. }
  3161. #else
  3162. IR::Instr *
  3163. LowererMD::GenerateSmIntPairTest(
  3164. IR::Instr * instrInsert,
  3165. IR::Opnd * opndSrc1,
  3166. IR::Opnd * opndSrc2,
  3167. IR::LabelInstr * labelFail)
  3168. {
  3169. IR::Opnd * opndReg;
  3170. IR::Instr * instrPrev = instrInsert->m_prev;
  3171. IR::Instr * instr;
  3172. Assert(opndSrc1->GetType() == TyVar);
  3173. Assert(opndSrc2->GetType() == TyVar);
  3174. if (opndSrc1->IsTaggedInt())
  3175. {
  3176. IR::Opnd *tempOpnd = opndSrc1;
  3177. opndSrc1 = opndSrc2;
  3178. opndSrc2 = tempOpnd;
  3179. }
  3180. if (opndSrc2->IsTaggedInt())
  3181. {
  3182. if (opndSrc1->IsTaggedInt())
  3183. {
  3184. return instrPrev;
  3185. }
  3186. GenerateSmIntTest(opndSrc1, instrInsert, labelFail);
  3187. return instrPrev;
  3188. }
  3189. else
  3190. {
  3191. opndReg = IR::RegOpnd::New(TyMachReg, this->m_func);
  3192. #ifdef SHIFTLOAD
  3193. instr = IR::Instr::New(Js::OpCode::SHLD, opndReg, opndSrc1, IR::IntConstOpnd::New(16, TyInt8, this->m_func), this->m_func);
  3194. instrInsert->InsertBefore(instr);
  3195. instr = IR::Instr::New(Js::OpCode::SHLD, opndReg, opndSrc2, IR::IntConstOpnd::New(16, TyInt8, this->m_func), this->m_func);
  3196. instrInsert->InsertBefore(instr);
  3197. #else
  3198. IR::Opnd * opndReg1;
  3199. // s1 = MOV src1
  3200. // s1 = SHR s1, VarTag_Shift
  3201. // s2 = MOV src2
  3202. // s2 = SHR s2, 32
  3203. // s1 = OR s1, s2 ------ move both tags to the lower 32 bits
  3204. // CMP s1, AtomTag_Pair ------ compare the tags together to the expected tag pair
  3205. // JNE $fail
  3206. // s1 = MOV src1
  3207. instr = IR::Instr::New(Js::OpCode::MOV, opndReg, opndSrc1, this->m_func);
  3208. instrInsert->InsertBefore(instr);
  3209. // s1 = SHR s1, VarTag_Shift
  3210. instr = IR::Instr::New(Js::OpCode::SHR, opndReg, opndReg, IR::IntConstOpnd::New(Js::VarTag_Shift, TyInt8, this->m_func), this->m_func);
  3211. instrInsert->InsertBefore(instr);
  3212. // s2 = MOV src2
  3213. opndReg1 = IR::RegOpnd::New(TyMachReg, this->m_func);
  3214. instr = IR::Instr::New(Js::OpCode::MOV, opndReg1, opndSrc2, this->m_func);
  3215. instrInsert->InsertBefore(instr);
  3216. // s2 = SHR s2, 32
  3217. instr = IR::Instr::New(Js::OpCode::SHR, opndReg1, opndReg1, IR::IntConstOpnd::New(32, TyInt8, this->m_func), this->m_func);
  3218. instrInsert->InsertBefore(instr);
  3219. // s1 = OR s1, s2
  3220. instr = IR::Instr::New(Js::OpCode::OR, opndReg, opndReg, opndReg1, this->m_func);
  3221. instrInsert->InsertBefore(instr);
  3222. #endif
  3223. opndReg = opndReg->UseWithNewType(TyInt32, this->m_func)->AsRegOpnd();
  3224. // CMP s1, AtomTag_Pair
  3225. instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  3226. instr->SetSrc1(opndReg);
  3227. instr->SetSrc2(IR::IntConstOpnd::New(Js::AtomTag_Pair, TyInt32, this->m_func, true));
  3228. instrInsert->InsertBefore(instr);
  3229. }
  3230. // JNE $fail
  3231. instr = IR::BranchInstr::New(Js::OpCode::JNE, labelFail, this->m_func);
  3232. instrInsert->InsertBefore(instr);
  3233. return instrPrev;
  3234. }
  3235. #endif
  3236. IR::BranchInstr *
  3237. LowererMD::GenerateLocalInlineCacheCheck(
  3238. IR::Instr * instrLdSt,
  3239. IR::RegOpnd * opndType,
  3240. IR::RegOpnd * inlineCache,
  3241. IR::LabelInstr * labelNext,
  3242. bool checkTypeWithoutProperty)
  3243. {
  3244. // Generate:
  3245. //
  3246. // CMP s1, [&(inlineCache->u.local.type/typeWithoutProperty)]
  3247. // JNE $next
  3248. IR::Instr * instr;
  3249. IR::Opnd* typeOpnd;
  3250. if (checkTypeWithoutProperty)
  3251. {
  3252. typeOpnd = IR::IndirOpnd::New(inlineCache, (int32)offsetof(Js::InlineCache, u.local.typeWithoutProperty), TyMachReg, instrLdSt->m_func);
  3253. }
  3254. else
  3255. {
  3256. typeOpnd = IR::IndirOpnd::New(inlineCache, (int32)offsetof(Js::InlineCache, u.local.type), TyMachReg, instrLdSt->m_func);
  3257. }
  3258. // CMP type, [&(inlineCache->u.local.type/typeWithoutProperty)]
  3259. instr = IR::Instr::New(Js::OpCode::CMP, instrLdSt->m_func);
  3260. instr->SetSrc1(opndType);
  3261. instr->SetSrc2(typeOpnd);
  3262. instrLdSt->InsertBefore(instr);
  3263. // JNE $next
  3264. IR::BranchInstr * branchInstr = IR::BranchInstr::New(Js::OpCode::JNE, labelNext, instrLdSt->m_func);
  3265. instrLdSt->InsertBefore(branchInstr);
  3266. return branchInstr;
  3267. }
  3268. IR::BranchInstr *
  3269. LowererMD::GenerateProtoInlineCacheCheck(
  3270. IR::Instr * instrLdSt,
  3271. IR::RegOpnd * opndType,
  3272. IR::RegOpnd * inlineCache,
  3273. IR::LabelInstr * labelNext)
  3274. {
  3275. // Generate:
  3276. //
  3277. // CMP s1, [&(inlineCache->u.proto.type)]
  3278. // JNE $next
  3279. IR::Instr * instr;
  3280. IR::Opnd* typeOpnd = IR::IndirOpnd::New(inlineCache, (int32)offsetof(Js::InlineCache, u.proto.type), TyMachReg, instrLdSt->m_func);
  3281. // CMP s1, [&(inlineCache->u.proto.type)]
  3282. instr = IR::Instr::New(Js::OpCode::CMP, instrLdSt->m_func);
  3283. instr->SetSrc1(opndType);
  3284. instr->SetSrc2(typeOpnd);
  3285. instrLdSt->InsertBefore(instr);
  3286. // JNE $next
  3287. IR::BranchInstr * branchInstr = IR::BranchInstr::New(Js::OpCode::JNE, labelNext, instrLdSt->m_func);
  3288. instrLdSt->InsertBefore(branchInstr);
  3289. return branchInstr;
  3290. }
  3291. IR::BranchInstr *
  3292. LowererMD::GenerateFlagInlineCacheCheck(
  3293. IR::Instr * instrLdSt,
  3294. IR::RegOpnd * opndType,
  3295. IR::RegOpnd * opndInlineCache,
  3296. IR::LabelInstr * labelNext)
  3297. {
  3298. // Generate:
  3299. //
  3300. // CMP s1, [&(inlineCache->u.accessor.type)]
  3301. // JNE $next
  3302. IR::Instr * instr;
  3303. IR::Opnd* typeOpnd;
  3304. typeOpnd = IR::IndirOpnd::New(opndInlineCache, (int32)offsetof(Js::InlineCache, u.accessor.type), TyMachReg, instrLdSt->m_func);
  3305. // CMP s1, [&(inlineCache->u.flag.type)]
  3306. instr = IR::Instr::New(Js::OpCode::CMP, instrLdSt->m_func);
  3307. instr->SetSrc1(opndType);
  3308. instr->SetSrc2(typeOpnd);
  3309. instrLdSt->InsertBefore(instr);
  3310. // JNE $next
  3311. IR::BranchInstr * branchInstr = IR::BranchInstr::New(Js::OpCode::JNE, labelNext, instrLdSt->m_func);
  3312. instrLdSt->InsertBefore(branchInstr);
  3313. return branchInstr;
  3314. }
  3315. IR::BranchInstr *
  3316. LowererMD::GenerateFlagInlineCacheCheckForNoGetterSetter(
  3317. IR::Instr * instrLdSt,
  3318. IR::RegOpnd * opndInlineCache,
  3319. IR::LabelInstr * labelNext)
  3320. {
  3321. // Generate:
  3322. //
  3323. // TEST [&(inlineCache->u.accessor.flags)], (Js::InlineCacheGetterFlag | Js::InlineCacheSetterFlag)
  3324. // JNE $next
  3325. IR::Instr * instr;
  3326. IR::Opnd* flagsOpnd;
  3327. flagsOpnd = IR::IndirOpnd::New(opndInlineCache, 0, TyInt8, instrLdSt->m_func);
  3328. // TEST [&(inlineCache->u.accessor.flags)], (Js::InlineCacheGetterFlag | Js::InlineCacheSetterFlag)
  3329. instr = IR::Instr::New(Js::OpCode::TEST,instrLdSt->m_func);
  3330. instr->SetSrc1(flagsOpnd);
  3331. instr->SetSrc2(IR::IntConstOpnd::New((Js::InlineCacheGetterFlag | Js::InlineCacheSetterFlag) << 1, TyInt8, instrLdSt->m_func));
  3332. instrLdSt->InsertBefore(instr);
  3333. // JNE $next
  3334. IR::BranchInstr * branchInstr = IR::BranchInstr::New(Js::OpCode::JNE, labelNext, instrLdSt->m_func);
  3335. instrLdSt->InsertBefore(branchInstr);
  3336. return branchInstr;
  3337. }
  3338. void
  3339. LowererMD::GenerateFlagInlineCacheCheckForGetterSetter(
  3340. IR::Instr * insertBeforeInstr,
  3341. IR::RegOpnd * opndInlineCache,
  3342. IR::LabelInstr * labelNext)
  3343. {
  3344. uint accessorFlagMask;
  3345. if (PHASE_OFF(Js::InlineGettersPhase, insertBeforeInstr->m_func->GetJnFunction()))
  3346. {
  3347. accessorFlagMask = Js::InlineCache::GetSetterFlagMask();
  3348. }
  3349. else if (PHASE_OFF(Js::InlineSettersPhase, insertBeforeInstr->m_func->GetJnFunction()))
  3350. {
  3351. accessorFlagMask = Js::InlineCache::GetGetterFlagMask();
  3352. }
  3353. else
  3354. {
  3355. accessorFlagMask = Js::InlineCache::GetGetterSetterFlagMask();
  3356. }
  3357. // Generate:
  3358. //
  3359. // TEST [&(inlineCache->u.accessor.flags)], Js::InlineCacheGetterFlag | Js::InlineCacheSetterFlag
  3360. // JEQ $next
  3361. IR::Instr * instr;
  3362. IR::Opnd* flagsOpnd;
  3363. flagsOpnd = IR::IndirOpnd::New(opndInlineCache, (int32)offsetof(Js::InlineCache, u.accessor.rawUInt16), TyInt8, insertBeforeInstr->m_func);
  3364. // TEST [&(inlineCache->u.accessor.flags)], InlineCacheGetterFlag | InlineCacheSetterFlag
  3365. instr = IR::Instr::New(Js::OpCode::TEST,this->m_func);
  3366. instr->SetSrc1(flagsOpnd);
  3367. instr->SetSrc2(IR::IntConstOpnd::New(accessorFlagMask, TyInt8, this->m_func));
  3368. insertBeforeInstr->InsertBefore(instr);
  3369. // JEQ $next
  3370. instr = IR::BranchInstr::New(Js::OpCode::JEQ, labelNext, this->m_func);
  3371. insertBeforeInstr->InsertBefore(instr);
  3372. }
  3373. void
  3374. LowererMD::GenerateLdFldFromLocalInlineCache(
  3375. IR::Instr * instrLdFld,
  3376. IR::RegOpnd * opndBase,
  3377. IR::Opnd * opndDst,
  3378. IR::RegOpnd * inlineCache,
  3379. IR::LabelInstr * labelFallThru,
  3380. bool isInlineSlot)
  3381. {
  3382. // Generate:
  3383. //
  3384. // s1 = MOV base->slots -- load the slot array
  3385. // s2 = MOVZXw [&(inlineCache->u.local.slotIndex)] -- load the cached slot index
  3386. // dst = MOV [s1 + s2* Scale] -- load the value directly from the slot
  3387. // JMP $fallthru
  3388. IR::Instr * instr;
  3389. IR::Opnd* slotIndexOpnd;
  3390. IR::IndirOpnd * opndIndir;
  3391. IR::RegOpnd * opndSlotArray = nullptr;
  3392. if (!isInlineSlot)
  3393. {
  3394. opndSlotArray = IR::RegOpnd::New(TyMachReg, instrLdFld->m_func);
  3395. opndIndir = IR::IndirOpnd::New(opndBase, Js::DynamicObject::GetOffsetOfAuxSlots(), TyMachReg, instrLdFld->m_func);
  3396. instr = IR::Instr::New(Js::OpCode::MOV, opndSlotArray, opndIndir, instrLdFld->m_func);
  3397. instrLdFld->InsertBefore(instr);
  3398. }
  3399. // s2 = MOVZXw [&(inlineCache->u.local.slotIndex)] -- load the cached slot index
  3400. IR::RegOpnd * opndReg2 = IR::RegOpnd::New(TyMachReg, instrLdFld->m_func);
  3401. slotIndexOpnd = IR::IndirOpnd::New(inlineCache, (int32)offsetof(Js::InlineCache, u.local.slotIndex), TyUint16, instrLdFld->m_func);
  3402. instr = IR::Instr::New(Js::OpCode::MOVZXW, opndReg2, slotIndexOpnd, instrLdFld->m_func);
  3403. instrLdFld->InsertBefore(instr);
  3404. if (isInlineSlot)
  3405. {
  3406. // dst = MOV [base + s2* Scale] -- load the value directly from the slot
  3407. opndIndir = IR::IndirOpnd::New(opndBase, opndReg2, LowererMDArch::GetDefaultIndirScale(), TyMachReg, instrLdFld->m_func);
  3408. instr = IR::Instr::New(Js::OpCode::MOV, opndDst, opndIndir, instrLdFld->m_func);
  3409. instrLdFld->InsertBefore(instr);
  3410. }
  3411. else
  3412. {
  3413. // dst = MOV [s1 + s2* Scale] -- load the value directly from the slot
  3414. opndIndir = IR::IndirOpnd::New(opndSlotArray, opndReg2, LowererMDArch::GetDefaultIndirScale(), TyMachReg, instrLdFld->m_func);
  3415. instr = IR::Instr::New(Js::OpCode::MOV, opndDst, opndIndir, instrLdFld->m_func);
  3416. instrLdFld->InsertBefore(instr);
  3417. }
  3418. // JMP $fallthru
  3419. instr = IR::BranchInstr::New(Js::OpCode::JMP, labelFallThru, instrLdFld->m_func);
  3420. instrLdFld->InsertBefore(instr);
  3421. }
  3422. void
  3423. LowererMD::GenerateLdLocalFldFromFlagInlineCache(
  3424. IR::Instr * instrLdFld,
  3425. IR::RegOpnd * opndBase,
  3426. IR::Opnd * opndDst,
  3427. IR::RegOpnd * opndInlineCache,
  3428. IR::LabelInstr * labelFallThru,
  3429. bool isInlineSlot)
  3430. {
  3431. // Generate:
  3432. //
  3433. // s1 = MOV [&base->slots] -- load the slot array
  3434. // s2 = MOVZXW [&(inlineCache->u.accessor.slotIndex)] -- load the cached slot index
  3435. // dst = MOV [s1 + s2*4]
  3436. // JMP $fallthru
  3437. IR::Instr * instr;
  3438. IR::Opnd* slotIndexOpnd;
  3439. IR::IndirOpnd * opndIndir;
  3440. IR::RegOpnd * opndSlotArray = nullptr;
  3441. if (!isInlineSlot)
  3442. {
  3443. opndSlotArray = IR::RegOpnd::New(TyMachReg, instrLdFld->m_func);
  3444. opndIndir = IR::IndirOpnd::New(opndBase, Js::DynamicObject::GetOffsetOfAuxSlots(), TyMachReg, instrLdFld->m_func);
  3445. instr = IR::Instr::New(Js::OpCode::MOV, opndSlotArray, opndIndir, instrLdFld->m_func);
  3446. instrLdFld->InsertBefore(instr);
  3447. }
  3448. // s2 = MOVZXW [&(inlineCache->u.accessor.slotIndex)] -- load the cached slot index
  3449. IR::RegOpnd *opndSlotIndex = IR::RegOpnd::New(TyMachReg, instrLdFld->m_func);
  3450. slotIndexOpnd = IR::IndirOpnd::New(opndInlineCache, (int32)offsetof(Js::InlineCache, u.accessor.slotIndex), TyUint16, instrLdFld->m_func);
  3451. instr = IR::Instr::New(Js::OpCode::MOVZXW, opndSlotIndex, slotIndexOpnd, instrLdFld->m_func);
  3452. instrLdFld->InsertBefore(instr);
  3453. if (isInlineSlot)
  3454. {
  3455. // dst = MOV [s1 + s2*4]
  3456. opndIndir = IR::IndirOpnd::New(opndBase, opndSlotIndex, LowererMDArch::GetDefaultIndirScale(), TyMachReg, instrLdFld->m_func);
  3457. instr = IR::Instr::New(Js::OpCode::MOV, opndDst, opndIndir, instrLdFld->m_func);
  3458. instrLdFld->InsertBefore(instr);
  3459. }
  3460. else
  3461. {
  3462. // dst = MOV [s1 + s2*4]
  3463. opndIndir = IR::IndirOpnd::New(opndSlotArray, opndSlotIndex, LowererMDArch::GetDefaultIndirScale(), TyMachReg, instrLdFld->m_func);
  3464. instr = IR::Instr::New(Js::OpCode::MOV, opndDst, opndIndir, instrLdFld->m_func);
  3465. instrLdFld->InsertBefore(instr);
  3466. }
  3467. // JMP $fallthru
  3468. instr = IR::BranchInstr::New(Js::OpCode::JMP, labelFallThru, instrLdFld->m_func);
  3469. instrLdFld->InsertBefore(instr);
  3470. }
  3471. void
  3472. LowererMD::GenerateLdFldFromFlagInlineCache(
  3473. IR::Instr * insertBeforeInstr,
  3474. IR::RegOpnd * opndBase,
  3475. IR::Opnd * opndDst,
  3476. IR::RegOpnd * opndInlineCache,
  3477. IR::LabelInstr * labelFallThru,
  3478. bool isInlineSlot)
  3479. {
  3480. // Generate:
  3481. //
  3482. // s1 = MOV [&(inlineCache->u.accessor.object)] -- load the cached prototype object
  3483. // s1 = MOV [&s1->slots] -- load the slot array
  3484. // s2 = MOVZXW [&(inlineCache->u.accessor.slotIndex)] -- load the cached slot index
  3485. // dst = MOV [s1 + s2*4]
  3486. // JMP $fallthru
  3487. IR::Instr * instr;
  3488. IR::Opnd* inlineCacheObjOpnd;
  3489. IR::IndirOpnd * opndIndir;
  3490. IR::RegOpnd * opndObjSlots = nullptr;
  3491. inlineCacheObjOpnd = IR::IndirOpnd::New(opndInlineCache, (int32)offsetof(Js::InlineCache, u.accessor.object), TyMachReg, this->m_func);
  3492. // s1 = MOV [&(inlineCache->u.accessor.object)] -- load the cached prototype object
  3493. IR::RegOpnd *opndObject = IR::RegOpnd::New(TyMachReg, this->m_func);
  3494. instr = IR::Instr::New(Js::OpCode::MOV, opndObject, inlineCacheObjOpnd, this->m_func);
  3495. insertBeforeInstr->InsertBefore(instr);
  3496. if (!isInlineSlot)
  3497. {
  3498. // s1 = MOV [&s1->slots] -- load the slot array
  3499. opndObjSlots = IR::RegOpnd::New(TyMachReg, this->m_func);
  3500. opndIndir = IR::IndirOpnd::New(opndObject, Js::DynamicObject::GetOffsetOfAuxSlots(), TyMachReg, this->m_func);
  3501. instr = IR::Instr::New(Js::OpCode::MOV, opndObjSlots, opndIndir, this->m_func);
  3502. insertBeforeInstr->InsertBefore(instr);
  3503. }
  3504. // s2 = MOVZXW [&(inlineCache->u.accessor.slotIndex)] -- load the cached slot index
  3505. IR::RegOpnd *opndSlotIndex = IR::RegOpnd::New(TyMachReg, this->m_func);
  3506. IR::Opnd* slotIndexOpnd = IR::IndirOpnd::New(opndInlineCache, (int32)offsetof(Js::InlineCache, u.accessor.slotIndex), TyUint16, this->m_func);
  3507. instr = IR::Instr::New(Js::OpCode::MOVZXW, opndSlotIndex, slotIndexOpnd, this->m_func);
  3508. insertBeforeInstr->InsertBefore(instr);
  3509. if (isInlineSlot)
  3510. {
  3511. // dst = MOV [s1 + s2*4]
  3512. opndIndir = IR::IndirOpnd::New(opndObject, opndSlotIndex, this->lowererMDArch.GetDefaultIndirScale(), TyMachReg, this->m_func);
  3513. instr = IR::Instr::New(Js::OpCode::MOV, opndDst, opndIndir, this->m_func);
  3514. insertBeforeInstr->InsertBefore(instr);
  3515. }
  3516. else
  3517. {
  3518. // dst = MOV [s1 + s2*4]
  3519. opndIndir = IR::IndirOpnd::New(opndObjSlots, opndSlotIndex, this->lowererMDArch.GetDefaultIndirScale(), TyMachReg, this->m_func);
  3520. instr = IR::Instr::New(Js::OpCode::MOV, opndDst, opndIndir, this->m_func);
  3521. insertBeforeInstr->InsertBefore(instr);
  3522. }
  3523. // JMP $fallthru
  3524. instr = IR::BranchInstr::New(Js::OpCode::JMP, labelFallThru, this->m_func);
  3525. insertBeforeInstr->InsertBefore(instr);
  3526. }
  3527. void
  3528. LowererMD::GenerateLdFldFromProtoInlineCache(
  3529. IR::Instr * instrLdFld,
  3530. IR::RegOpnd * opndBase,
  3531. IR::Opnd * opndDst,
  3532. IR::RegOpnd * inlineCache,
  3533. IR::LabelInstr * labelFallThru,
  3534. bool isInlineSlot)
  3535. {
  3536. // Generate:
  3537. //
  3538. // s1 = MOV [&(inlineCache->u.proto.prototypeObject)] -- load the cached prototype object
  3539. // s1 = MOV [&s1->slots] -- load the slot array
  3540. // s2 = MOVZXW [&(inlineCache->u.proto.slotIndex)] -- load the cached slot index
  3541. // dst = MOV [s1 + s2*4]
  3542. // JMP $fallthru
  3543. IR::Instr * instr;
  3544. IR::Opnd* inlineCacheProtoOpnd;
  3545. IR::IndirOpnd * opndIndir;
  3546. IR::RegOpnd * opndProtoSlots = nullptr;
  3547. inlineCacheProtoOpnd = IR::IndirOpnd::New(inlineCache, (int32)offsetof(Js::InlineCache, u.proto.prototypeObject), TyMachReg, instrLdFld->m_func);
  3548. // s1 = MOV [&(inlineCache->u.proto.prototypeObject)] -- load the cached prototype object
  3549. IR::RegOpnd *opndProto = IR::RegOpnd::New(TyMachReg, instrLdFld->m_func);
  3550. instr = IR::Instr::New(Js::OpCode::MOV, opndProto, inlineCacheProtoOpnd, instrLdFld->m_func);
  3551. instrLdFld->InsertBefore(instr);
  3552. if (!isInlineSlot)
  3553. {
  3554. // s1 = MOV [&s1->slots] -- load the slot array
  3555. opndProtoSlots = IR::RegOpnd::New(TyMachReg, instrLdFld->m_func);
  3556. opndIndir = IR::IndirOpnd::New(opndProto, Js::DynamicObject::GetOffsetOfAuxSlots(), TyMachReg, instrLdFld->m_func);
  3557. instr = IR::Instr::New(Js::OpCode::MOV, opndProtoSlots, opndIndir, instrLdFld->m_func);
  3558. instrLdFld->InsertBefore(instr);
  3559. }
  3560. // s2 = MOVZXW [&(inlineCache->u.proto.slotIndex)] -- load the cached slot index
  3561. IR::RegOpnd *opndSlotIndex = IR::RegOpnd::New(TyMachReg, instrLdFld->m_func);
  3562. IR::Opnd* slotIndexOpnd = IR::IndirOpnd::New(inlineCache, (int32)offsetof(Js::InlineCache, u.proto.slotIndex), TyUint16, instrLdFld->m_func);
  3563. instr = IR::Instr::New(Js::OpCode::MOVZXW, opndSlotIndex, slotIndexOpnd, instrLdFld->m_func);
  3564. instrLdFld->InsertBefore(instr);
  3565. if (isInlineSlot)
  3566. {
  3567. // dst = MOV [s1 + s2*4]
  3568. opndIndir = IR::IndirOpnd::New(opndProto, opndSlotIndex, LowererMDArch::GetDefaultIndirScale(), TyMachReg, instrLdFld->m_func);
  3569. instr = IR::Instr::New(Js::OpCode::MOV, opndDst, opndIndir, instrLdFld->m_func);
  3570. instrLdFld->InsertBefore(instr);
  3571. }
  3572. else
  3573. {
  3574. // dst = MOV [s1 + s2*4]
  3575. opndIndir = IR::IndirOpnd::New(opndProtoSlots, opndSlotIndex, LowererMDArch::GetDefaultIndirScale(), TyMachReg, instrLdFld->m_func);
  3576. instr = IR::Instr::New(Js::OpCode::MOV, opndDst, opndIndir, instrLdFld->m_func);
  3577. instrLdFld->InsertBefore(instr);
  3578. }
  3579. // JMP $fallthru
  3580. instr = IR::BranchInstr::New(Js::OpCode::JMP, labelFallThru, instrLdFld->m_func);
  3581. instrLdFld->InsertBefore(instr);
  3582. }
  3583. void
  3584. LowererMD::GenerateLoadTaggedType(IR::Instr * instrLdSt, IR::RegOpnd * opndType, IR::RegOpnd * opndTaggedType)
  3585. {
  3586. // Generate
  3587. //
  3588. // MOV taggedType, type
  3589. // OR taggedType, InlineCacheAuxSlotTypeTag
  3590. // MOV taggedType, type
  3591. {
  3592. IR::Instr * instrMov = IR::Instr::New(Js::OpCode::MOV, opndTaggedType, opndType, instrLdSt->m_func);
  3593. instrLdSt->InsertBefore(instrMov);
  3594. }
  3595. // OR taggedType, InlineCacheAuxSlotTypeTag
  3596. {
  3597. IR::IntConstOpnd * opndAuxSlotTag = IR::IntConstOpnd::New(InlineCacheAuxSlotTypeTag, TyInt8, instrLdSt->m_func);
  3598. IR::Instr * instrAnd = IR::Instr::New(Js::OpCode::OR, opndTaggedType, opndTaggedType, opndAuxSlotTag, instrLdSt->m_func);
  3599. instrLdSt->InsertBefore(instrAnd);
  3600. }
  3601. }
  3602. ///----------------------------------------------------------------------------
  3603. ///
  3604. /// LowererMD::GenerateFastLdMethodFromFlags
  3605. ///
  3606. /// Make use of the helper to cache the type and slot index used to do a LdFld
  3607. /// and do an inline load from the appropriate slot if the type hasn't changed
  3608. /// since the last time this LdFld was executed.
  3609. ///
  3610. ///----------------------------------------------------------------------------
  3611. bool
  3612. LowererMD::GenerateFastLdMethodFromFlags(IR::Instr * instrLdFld)
  3613. {
  3614. IR::LabelInstr * labelFallThru;
  3615. IR::LabelInstr * bailOutLabel;
  3616. IR::Opnd * opndSrc;
  3617. IR::Opnd * opndDst;
  3618. IR::RegOpnd * opndBase;
  3619. IR::RegOpnd * opndType;
  3620. IR::RegOpnd * opndInlineCache;
  3621. opndSrc = instrLdFld->GetSrc1();
  3622. AssertMsg(opndSrc->IsSymOpnd() && opndSrc->AsSymOpnd()->IsPropertySymOpnd() && opndSrc->AsSymOpnd()->m_sym->IsPropertySym(),
  3623. "Expected property sym operand as src of LdFldFlags");
  3624. IR::PropertySymOpnd * propertySymOpnd = opndSrc->AsPropertySymOpnd();
  3625. Assert(!instrLdFld->DoStackArgsOpt(this->m_func));
  3626. if (propertySymOpnd->IsTypeCheckSeqCandidate())
  3627. {
  3628. AssertMsg(propertySymOpnd->HasObjectTypeSym(), "Type optimized property sym operand without a type sym?");
  3629. StackSym *typeSym = propertySymOpnd->GetObjectTypeSym();
  3630. opndType = IR::RegOpnd::New(typeSym, TyMachReg, this->m_func);
  3631. }
  3632. else
  3633. {
  3634. opndType = IR::RegOpnd::New(TyMachReg, this->m_func);
  3635. }
  3636. opndBase = propertySymOpnd->CreatePropertyOwnerOpnd(m_func);
  3637. opndDst = instrLdFld->GetDst();
  3638. opndInlineCache = IR::RegOpnd::New(TyMachPtr, this->m_func);
  3639. labelFallThru = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  3640. // Label to jump to (or fall through to) when bailing out
  3641. bailOutLabel = IR::LabelInstr::New(Js::OpCode::Label, instrLdFld->m_func, true /* isOpHelper */);
  3642. instrLdFld->InsertBefore(IR::Instr::New(Js::OpCode::MOV, opndInlineCache, m_lowerer->LoadRuntimeInlineCacheOpnd(instrLdFld, propertySymOpnd), this->m_func));
  3643. IR::LabelInstr * labelFlagAux = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  3644. // Check the flag cache with the untagged type
  3645. this->m_lowerer->GenerateObjectTestAndTypeLoad(instrLdFld, opndBase, opndType, bailOutLabel);
  3646. // Blindly do the check for getter flag first and then do the type check
  3647. // We avoid repeated check for getter flag when the function object may be in either
  3648. // inline slots or auxiliary slots
  3649. GenerateFlagInlineCacheCheckForGetterSetter(instrLdFld, opndInlineCache, bailOutLabel);
  3650. GenerateFlagInlineCacheCheck(instrLdFld, opndType, opndInlineCache, labelFlagAux);
  3651. GenerateLdFldFromFlagInlineCache(instrLdFld, opndBase, opndDst, opndInlineCache, labelFallThru, true);
  3652. // Check the flag cache with the tagged type
  3653. instrLdFld->InsertBefore(labelFlagAux);
  3654. IR::RegOpnd * opndTaggedType = IR::RegOpnd::New(TyMachReg, this->m_func);
  3655. GenerateLoadTaggedType(instrLdFld, opndType, opndTaggedType);
  3656. GenerateFlagInlineCacheCheck(instrLdFld, opndTaggedType, opndInlineCache, bailOutLabel);
  3657. GenerateLdFldFromFlagInlineCache(instrLdFld, opndBase, opndDst, opndInlineCache, labelFallThru, false);
  3658. instrLdFld->InsertBefore(bailOutLabel);
  3659. instrLdFld->InsertAfter(labelFallThru);
  3660. // Generate the bailout helper call. 'instr' will be changed to the CALL into the bailout function, so it can't be used for
  3661. // ordering instructions anymore.
  3662. instrLdFld->UnlinkSrc1();
  3663. this->m_lowerer->GenerateBailOut(instrLdFld);
  3664. return true;
  3665. }
  3666. void
  3667. LowererMD::GenerateLoadPolymorphicInlineCacheSlot(IR::Instr * instrLdSt, IR::RegOpnd * opndInlineCache, IR::RegOpnd * opndType, uint polymorphicInlineCacheSize)
  3668. {
  3669. // Generate
  3670. //
  3671. // MOV r1, type
  3672. // SHR r1, PolymorphicInlineCacheShift
  3673. // AND r1, (size - 1)
  3674. // SHL r1, log2(sizeof(Js::InlineCache))
  3675. // LEA inlineCache, [inlineCache + r1]
  3676. // MOV r1, type
  3677. IR::RegOpnd * opndOffset = IR::RegOpnd::New(TyMachPtr, instrLdSt->m_func);
  3678. IR::Instr * instr = IR::Instr::New(Js::OpCode::MOV, opndOffset, opndType, instrLdSt->m_func);
  3679. instrLdSt->InsertBefore(instr);
  3680. IntConstType rightShiftAmount = PolymorphicInlineCacheShift;
  3681. IntConstType leftShiftAmount = Math::Log2(sizeof(Js::InlineCache));
  3682. // instead of generating
  3683. // SHR r1, PolymorphicInlineCacheShift
  3684. // AND r1, (size - 1)
  3685. // SHL r1, log2(sizeof(Js::InlineCache))
  3686. //
  3687. // we can generate:
  3688. // SHR r1, (PolymorphicInlineCacheShift - log2(sizeof(Js::InlineCache))
  3689. // AND r1, (size - 1) << log2(sizeof(Js::InlineCache))
  3690. Assert(rightShiftAmount > leftShiftAmount);
  3691. instr = IR::Instr::New(Js::OpCode::SHR, opndOffset, opndOffset, IR::IntConstOpnd::New(rightShiftAmount - leftShiftAmount, TyUint8, instrLdSt->m_func, true), instrLdSt->m_func);
  3692. instrLdSt->InsertBefore(instr);
  3693. instr = IR::Instr::New(Js::OpCode::AND, opndOffset, opndOffset, IR::AddrOpnd::New((void*)((IntConstType)(polymorphicInlineCacheSize - 1) << leftShiftAmount), IR::AddrOpndKindConstant, instrLdSt->m_func, true), instrLdSt->m_func);
  3694. instrLdSt->InsertBefore(instr);
  3695. // LEA inlineCache, [inlineCache + r1]
  3696. IR::IndirOpnd * indirOpnd = IR::IndirOpnd::New(opndInlineCache, opndOffset, TyMachPtr, instrLdSt->m_func);
  3697. instr = IR::Instr::New(Js::OpCode::LEA, opndInlineCache, indirOpnd, instrLdSt->m_func);
  3698. instrLdSt->InsertBefore(instr);
  3699. }
  3700. void
  3701. LowererMD::ChangeToWriteBarrierAssign(IR::Instr * assignInstr)
  3702. {
  3703. #ifdef RECYCLER_WRITE_BARRIER_JIT
  3704. if (assignInstr->GetSrc1()->IsWriteBarrierTriggerableValue())
  3705. {
  3706. IR::RegOpnd * writeBarrierAddrRegOpnd = IR::RegOpnd::New(TyMachPtr, assignInstr->m_func);
  3707. IR::Instr * leaInstr = IR::Instr::New(Js::OpCode::LEA, writeBarrierAddrRegOpnd, assignInstr->UnlinkDst(), assignInstr->m_func);
  3708. assignInstr->InsertBefore(leaInstr);
  3709. assignInstr->SetDst(IR::IndirOpnd::New(writeBarrierAddrRegOpnd, 0, TyMachReg, assignInstr->m_func));
  3710. GenerateWriteBarrier(writeBarrierAddrRegOpnd, assignInstr->m_next);
  3711. }
  3712. #endif
  3713. ChangeToAssign(assignInstr);
  3714. }
  3715. void
  3716. LowererMD::GenerateWriteBarrierAssign(IR::MemRefOpnd * opndDst, IR::Opnd * opndSrc, IR::Instr * insertBeforeInstr)
  3717. {
  3718. Lowerer::InsertMove(opndDst, opndSrc, insertBeforeInstr);
  3719. #ifdef RECYCLER_WRITE_BARRIER_JIT
  3720. if (opndSrc->IsWriteBarrierTriggerableValue())
  3721. {
  3722. void * address = opndDst->AsMemRefOpnd()->GetMemLoc();
  3723. #ifdef RECYCLER_WRITE_BARRIER_BYTE
  3724. IR::MemRefOpnd * cardTableEntry = IR::MemRefOpnd::New(
  3725. &RecyclerWriteBarrierManager::GetAddressOfCardTable()[RecyclerWriteBarrierManager::GetCardTableIndex(address)], TyInt8, insertBeforeInstr->m_func);
  3726. IR::Instr * movInstr = IR::Instr::New(Js::OpCode::MOV, cardTableEntry, IR::IntConstOpnd::New(1, TyInt8, insertBeforeInstr->m_func), insertBeforeInstr->m_func);
  3727. insertBeforeInstr->InsertBefore(movInstr);
  3728. #else
  3729. IR::MemRefOpnd * cardTableEntry = IR::MemRefOpnd::New(
  3730. &RecyclerWriteBarrierManager::GetAddressOfCardTable()[RecyclerWriteBarrierManager::GetCardTableIndex(address)], TyMachPtr, insertBeforeInstr->m_func);
  3731. IR::Instr * orInstr = IR::Instr::New(Js::OpCode::OR, cardTableEntry,
  3732. IR::IntConstOpnd::New(1 << ((uint)address >> 7), TyInt32, insertBeforeInstr->m_func), insertBeforeInstr->m_func);
  3733. insertBeforeInstr->InsertBefore(orInstr);
  3734. #endif
  3735. }
  3736. #endif
  3737. }
  3738. void
  3739. LowererMD::GenerateWriteBarrierAssign(IR::IndirOpnd * opndDst, IR::Opnd * opndSrc, IR::Instr * insertBeforeInstr)
  3740. {
  3741. #ifdef RECYCLER_WRITE_BARRIER_JIT
  3742. if (opndSrc->IsWriteBarrierTriggerableValue())
  3743. {
  3744. IR::RegOpnd * writeBarrierAddrRegOpnd = IR::RegOpnd::New(TyMachPtr, insertBeforeInstr->m_func);
  3745. insertBeforeInstr->InsertBefore(IR::Instr::New(Js::OpCode::LEA, writeBarrierAddrRegOpnd, opndDst, insertBeforeInstr->m_func));
  3746. insertBeforeInstr->InsertBefore(IR::Instr::New(Js::OpCode::MOV,
  3747. IR::IndirOpnd::New(writeBarrierAddrRegOpnd, 0, TyMachReg, insertBeforeInstr->m_func), opndSrc, insertBeforeInstr->m_func));
  3748. GenerateWriteBarrier(writeBarrierAddrRegOpnd, insertBeforeInstr);
  3749. // The mov happens above, and it's slightly faster doing it that way since we've already calculated the address we're writing to
  3750. return;
  3751. }
  3752. #endif
  3753. Lowerer::InsertMove(opndDst, opndSrc, insertBeforeInstr);
  3754. return;
  3755. }
  3756. #ifdef RECYCLER_WRITE_BARRIER_JIT
  3757. void
  3758. LowererMD::GenerateWriteBarrier(IR::Opnd * writeBarrierAddrRegOpnd, IR::Instr * insertBeforeInstr)
  3759. {
  3760. #if defined(RECYCLER_WRITE_BARRIER_BYTE)
  3761. IR::RegOpnd * indexOpnd = IR::RegOpnd::New(TyMachPtr, insertBeforeInstr->m_func);
  3762. IR::Instr * loadIndexInstr = IR::Instr::New(Js::OpCode::MOV, indexOpnd, writeBarrierAddrRegOpnd, insertBeforeInstr->m_func);
  3763. insertBeforeInstr->InsertBefore(loadIndexInstr);
  3764. IR::Instr * shiftBitInstr = IR::Instr::New(Js::OpCode::SHR, indexOpnd, indexOpnd,
  3765. IR::IntConstOpnd::New(12 /* 1 << 12 = 4096 */, TyInt32, insertBeforeInstr->m_func), insertBeforeInstr->m_func);
  3766. insertBeforeInstr->InsertBefore(shiftBitInstr);
  3767. IR::RegOpnd * cardTableRegOpnd = IR::RegOpnd::New(TyMachReg, insertBeforeInstr->m_func);
  3768. IR::Instr * cardTableAddrInstr = IR::Instr::New(Js::OpCode::MOV, cardTableRegOpnd,
  3769. IR::AddrOpnd::New(RecyclerWriteBarrierManager::GetAddressOfCardTable(), IR::AddrOpndKindDynamicMisc, insertBeforeInstr->m_func),
  3770. insertBeforeInstr->m_func);
  3771. insertBeforeInstr->InsertBefore(cardTableAddrInstr);
  3772. IR::IndirOpnd * cardTableEntryOpnd = IR::IndirOpnd::New(cardTableRegOpnd, indexOpnd,
  3773. TyInt8, insertBeforeInstr->m_func);
  3774. IR::Instr * movInstr = IR::Instr::New(Js::OpCode::MOV, cardTableEntryOpnd, IR::IntConstOpnd::New(1, TyInt8, insertBeforeInstr->m_func), insertBeforeInstr->m_func);
  3775. insertBeforeInstr->InsertBefore(movInstr);
  3776. #else
  3777. Assert(writeBarrierAddrRegOpnd->IsRegOpnd());
  3778. IR::RegOpnd * shiftBitOpnd = IR::RegOpnd::New(TyInt32, insertBeforeInstr->m_func);
  3779. shiftBitOpnd->SetReg(LowererMDArch::GetRegShiftCount());
  3780. IR::Instr * moveShiftBitOpnd = IR::Instr::New(Js::OpCode::MOV, shiftBitOpnd, writeBarrierAddrRegOpnd, insertBeforeInstr->m_func);
  3781. insertBeforeInstr->InsertBefore(moveShiftBitOpnd);
  3782. IR::Instr * shiftBitInstr = IR::Instr::New(Js::OpCode::SHR, shiftBitOpnd, shiftBitOpnd,
  3783. IR::IntConstOpnd::New(7 /* 1 << 7 = 128 */, TyInt32, insertBeforeInstr->m_func), insertBeforeInstr->m_func);
  3784. insertBeforeInstr->InsertBefore(shiftBitInstr);
  3785. IR::RegOpnd * bitOpnd = IR::RegOpnd::New(TyInt32, insertBeforeInstr->m_func);
  3786. IR::Instr * mov1Instr = IR::Instr::New(Js::OpCode::MOV, bitOpnd,
  3787. IR::IntConstOpnd::New(1, TyInt32, insertBeforeInstr->m_func), insertBeforeInstr->m_func);
  3788. insertBeforeInstr->InsertBefore(mov1Instr);
  3789. IR::Instr * bitInstr = IR::Instr::New(Js::OpCode::SHL, bitOpnd, bitOpnd, shiftBitOpnd, insertBeforeInstr->m_func);
  3790. insertBeforeInstr->InsertBefore(bitInstr);
  3791. IR::RegOpnd * indexOpnd = shiftBitOpnd;
  3792. IR::Instr * indexInstr = IR::Instr::New(Js::OpCode::SHR, indexOpnd, indexOpnd,
  3793. IR::IntConstOpnd::New(5 /* 1 << 5 = 32 */, TyInt32, insertBeforeInstr->m_func), insertBeforeInstr->m_func);
  3794. insertBeforeInstr->InsertBefore(indexInstr);
  3795. IR::RegOpnd * cardTableRegOpnd = IR::RegOpnd::New(TyMachReg, insertBeforeInstr->m_func);
  3796. IR::Instr * cardTableAddrInstr = IR::Instr::New(Js::OpCode::MOV, cardTableRegOpnd,
  3797. IR::AddrOpnd::New(RecyclerWriteBarrierManager::GetAddressOfCardTable(), IR::AddrOpndKindDynamicMisc, insertBeforeInstr->m_func),
  3798. insertBeforeInstr->m_func);
  3799. insertBeforeInstr->InsertBefore(cardTableAddrInstr);
  3800. IR::IndirOpnd * cardTableEntryOpnd = IR::IndirOpnd::New(cardTableRegOpnd, indexOpnd, LowererMDArch::GetDefaultIndirScale(),
  3801. TyInt32, insertBeforeInstr->m_func);
  3802. IR::Instr * orInstr = IR::Instr::New(Js::OpCode::OR, cardTableEntryOpnd, cardTableEntryOpnd,
  3803. bitOpnd, insertBeforeInstr->m_func);
  3804. insertBeforeInstr->InsertBefore(orInstr);
  3805. #endif
  3806. }
  3807. #endif
  3808. void
  3809. LowererMD::GenerateStFldFromLocalInlineCache(
  3810. IR::Instr * instrStFld,
  3811. IR::RegOpnd * opndBase,
  3812. IR::Opnd * opndSrc,
  3813. IR::RegOpnd * inlineCache,
  3814. IR::LabelInstr * labelFallThru,
  3815. bool isInlineSlot)
  3816. {
  3817. IR::Instr * instr;
  3818. IR::Opnd* slotIndexOpnd;
  3819. IR::RegOpnd * opndIndirBase = opndBase;
  3820. if (!isInlineSlot)
  3821. {
  3822. // slotArray = MOV base->slots -- load the slot array
  3823. IR::RegOpnd * opndSlotArray = IR::RegOpnd::New(TyMachReg, instrStFld->m_func);
  3824. IR::IndirOpnd * opndIndir = IR::IndirOpnd::New(opndBase, Js::DynamicObject::GetOffsetOfAuxSlots(), TyMachReg, instrStFld->m_func);
  3825. instr = IR::Instr::New(Js::OpCode::MOV, opndSlotArray, opndIndir, instrStFld->m_func);
  3826. instrStFld->InsertBefore(instr);
  3827. opndIndirBase = opndSlotArray;
  3828. }
  3829. // slotIndex = MOV [&inlineCache->u.local.inlineSlotOffsetOrAuxSlotIndex] -- load the cached slot offset or index
  3830. IR::RegOpnd * opndSlotIndex = IR::RegOpnd::New(TyMachReg, instrStFld->m_func);
  3831. slotIndexOpnd = IR::IndirOpnd::New(inlineCache, (int32)offsetof(Js::InlineCache, u.local.slotIndex), TyUint16, instrStFld->m_func);
  3832. instr = IR::Instr::New(Js::OpCode::MOVZXW, opndSlotIndex, slotIndexOpnd, instrStFld->m_func);
  3833. instrStFld->InsertBefore(instr);
  3834. // [base + slotIndex * (1 << indirScale)] = MOV src -- store the value directly to the slot
  3835. // [slotArray + slotIndex * (1 << indirScale)] = MOV src -- store the value directly to the slot
  3836. IR::IndirOpnd * storeLocIndirOpnd = IR::IndirOpnd::New(opndIndirBase, opndSlotIndex,
  3837. LowererMDArch::GetDefaultIndirScale(), TyMachReg, instrStFld->m_func);
  3838. GenerateWriteBarrierAssign(storeLocIndirOpnd, opndSrc, instrStFld);
  3839. // JMP $fallthru
  3840. instr = IR::BranchInstr::New(Js::OpCode::JMP, labelFallThru, instrStFld->m_func);
  3841. instrStFld->InsertBefore(instr);
  3842. }
  3843. void LowererMD::InsertIncUInt8PreventOverflow(
  3844. IR::Opnd *const dst,
  3845. IR::Opnd *const src,
  3846. IR::Instr *const insertBeforeInstr,
  3847. IR::Instr * *const onOverflowInsertBeforeInstrRef)
  3848. {
  3849. Assert(dst);
  3850. Assert(dst->GetType() == TyUint8);
  3851. Assert(src);
  3852. Assert(src->GetType() == TyUint8);
  3853. Assert(insertBeforeInstr);
  3854. Func *const func = insertBeforeInstr->m_func;
  3855. // Generate:
  3856. // cmp src, static_cast<uint8>(-1)
  3857. // jeq $done
  3858. // dst = add src, 1
  3859. // $noOverflow:
  3860. IR::LabelInstr *const noOverflowLabel = Lowerer::InsertLabel(false, insertBeforeInstr);
  3861. Lowerer::InsertCompareBranch(src, IR::IntConstOpnd::New(static_cast<uint8>(-1), TyUint8, func, true),
  3862. Js::OpCode::BrEq_A, noOverflowLabel, noOverflowLabel);
  3863. // inc dst, src
  3864. Lowerer::InsertAdd(true, dst, src, IR::IntConstOpnd::New(1, TyUint8, func, true), noOverflowLabel);
  3865. // $done:
  3866. if(onOverflowInsertBeforeInstrRef)
  3867. {
  3868. *onOverflowInsertBeforeInstrRef = noOverflowLabel;
  3869. }
  3870. }
  3871. void LowererMD::InsertDecUInt8PreventOverflow(
  3872. IR::Opnd *const dst,
  3873. IR::Opnd *const src,
  3874. IR::Instr *const insertBeforeInstr,
  3875. IR::Instr * *const onOverflowInsertBeforeInstrRef)
  3876. {
  3877. Assert(dst);
  3878. Assert(dst->GetType() == TyUint8);
  3879. Assert(src);
  3880. Assert(src->GetType() == TyUint8);
  3881. Assert(insertBeforeInstr);
  3882. Func *const func = insertBeforeInstr->m_func;
  3883. // Generate:
  3884. // sub dst, src, 1
  3885. // jnc $noOverflow
  3886. // mov dst, 0
  3887. // $noOverflow:
  3888. IR::LabelInstr *const noOverflowLabel = Lowerer::InsertLabel(false, insertBeforeInstr);
  3889. // sub dst, src, 1
  3890. IR::Instr *const instr = IR::Instr::New(Js::OpCode::SUB, dst, src, IR::IntConstOpnd::New(1, TyUint8, func, true), func);
  3891. noOverflowLabel->InsertBefore(instr);
  3892. MakeDstEquSrc1(instr);
  3893. // jnc $noOverflow
  3894. Lowerer::InsertBranch(Js::OpCode::BrGe_A, true, noOverflowLabel, noOverflowLabel);
  3895. // mov dst, 0
  3896. Lowerer::InsertMove(dst, IR::IntConstOpnd::New(0, TyUint8, func, true), noOverflowLabel);
  3897. // $noOverflow:
  3898. if(onOverflowInsertBeforeInstrRef)
  3899. {
  3900. *onOverflowInsertBeforeInstrRef = noOverflowLabel;
  3901. }
  3902. }
  3903. //----------------------------------------------------------------------------
  3904. //
  3905. // LowererMD::GenerateFastScopedLdFld
  3906. //
  3907. // Make use of the helper to cache the type and slot index used to do a ScopedLdFld
  3908. // when the scope is an array of length 1.
  3909. // Extract the only element from array and do an inline load from the appropriate slot
  3910. // if the type hasn't changed since the last time this ScopedLdFld was executed.
  3911. //
  3912. //----------------------------------------------------------------------------
  3913. IR::Instr *
  3914. LowererMD::GenerateFastScopedLdFld(IR::Instr * instrLdScopedFld)
  3915. {
  3916. // CMP [base + offset(length)], 1 -- get the length on array and test if it is 1.
  3917. // JNE $helper
  3918. // MOV r1, [base + offset(scopes)] -- load the first scope
  3919. // MOV r2, r1->type
  3920. // CMP r2, [&(inlineCache->u.local.type)] -- check type
  3921. // JNE $helper
  3922. // MOV r1, r1->slots -- load the slots array
  3923. // MOV r2 , [&(inlineCache->u.local.slotIndex)] -- load the cached slot index
  3924. // MOV dst, [r1+r2] -- load the value from the slot
  3925. // JMP $fallthru
  3926. // $helper:
  3927. // dst = CALL PatchGetPropertyScoped(inlineCache, base, field, defaultInstance, scriptContext)
  3928. // $fallthru:
  3929. IR::RegOpnd * opndBase;
  3930. IR::Instr * instr;
  3931. IR::IndirOpnd * indirOpnd;
  3932. IR::LabelInstr * labelHelper;
  3933. IR::Opnd * opndDst;
  3934. IR::RegOpnd * inlineCache;
  3935. IR::RegOpnd *r1;
  3936. IR::LabelInstr * labelFallThru;
  3937. IR::Opnd *propertySrc = instrLdScopedFld->GetSrc1();
  3938. AssertMsg(propertySrc->IsSymOpnd() && propertySrc->AsSymOpnd()->IsPropertySymOpnd() && propertySrc->AsSymOpnd()->m_sym->IsPropertySym(),
  3939. "Expected property sym operand as src of LdScoped");
  3940. IR::PropertySymOpnd * propertySymOpnd = propertySrc->AsPropertySymOpnd();
  3941. opndBase = propertySymOpnd->CreatePropertyOwnerOpnd(m_func);
  3942. IR::Opnd *srcBase = instrLdScopedFld->GetSrc2();
  3943. AssertMsg(srcBase->IsRegOpnd(), "Expected reg opnd as src2");
  3944. //opndBase = srcBase;
  3945. //IR::IndirOpnd * indirOpnd = src->AsIndirOpnd();
  3946. labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  3947. AssertMsg(opndBase->m_sym->m_isSingleDef, "We assume this isn't redefined");
  3948. // CMP [base + offset(length)], 1 -- get the length on array and test if it is 1.
  3949. indirOpnd = IR::IndirOpnd::New(opndBase, Js::FrameDisplay::GetOffsetOfLength(), TyInt16, this->m_func);
  3950. instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  3951. instr->SetSrc1(indirOpnd);
  3952. instr->SetSrc2(IR::IntConstOpnd::New(0x1, TyInt8, this->m_func));
  3953. instrLdScopedFld->InsertBefore(instr);
  3954. // JNE $helper
  3955. instr = IR::BranchInstr::New(Js::OpCode::JNE, labelHelper, this->m_func);
  3956. instrLdScopedFld->InsertBefore(instr);
  3957. // MOV r1, [base + offset(scopes)] -- load the first scope
  3958. indirOpnd = IR::IndirOpnd::New(opndBase, Js::FrameDisplay::GetOffsetOfScopes(), TyMachReg, this->m_func);
  3959. r1 = IR::RegOpnd::New(TyMachReg, this->m_func);
  3960. instr = IR::Instr::New(Js::OpCode::MOV, r1, indirOpnd, this->m_func);
  3961. instrLdScopedFld->InsertBefore(instr);
  3962. //first load the inlineCache type
  3963. inlineCache = IR::RegOpnd::New(TyMachPtr, this->m_func);
  3964. Assert(inlineCache != nullptr);
  3965. IR::RegOpnd * opndType = IR::RegOpnd::New(TyMachReg, this->m_func);
  3966. opndDst = instrLdScopedFld->GetDst();
  3967. labelFallThru = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  3968. r1->m_sym->m_isNotInt = true;
  3969. // Load the type
  3970. this->m_lowerer->GenerateObjectTestAndTypeLoad(instrLdScopedFld, r1, opndType, labelHelper);
  3971. // Check the local cache with the tagged type
  3972. IR::RegOpnd * opndTaggedType = IR::RegOpnd::New(TyMachReg, this->m_func);
  3973. GenerateLoadTaggedType(instrLdScopedFld, opndType, opndTaggedType);
  3974. instrLdScopedFld->InsertBefore(IR::Instr::New(Js::OpCode::MOV, inlineCache, m_lowerer->LoadRuntimeInlineCacheOpnd(instrLdScopedFld, propertySymOpnd), this->m_func));
  3975. GenerateLocalInlineCacheCheck(instrLdScopedFld, opndTaggedType, inlineCache, labelHelper);
  3976. GenerateLdFldFromLocalInlineCache(instrLdScopedFld, r1, opndDst, inlineCache, labelFallThru, false);
  3977. // $helper:
  3978. // dst = CALL PatchGetPropertyScoped(inlineCache, opndBase, propertyId, srcBase, scriptContext)
  3979. // $fallthru:
  3980. instrLdScopedFld->InsertBefore(labelHelper);
  3981. instrLdScopedFld->InsertAfter(labelFallThru);
  3982. return instrLdScopedFld->m_prev;
  3983. }
  3984. //----------------------------------------------------------------------------
  3985. //
  3986. // LowererMD::GenerateFastScopedStFld
  3987. //
  3988. // Make use of the helper to cache the type and slot index used to do a ScopedStFld
  3989. // when the scope is an array of length 1.
  3990. // Extract the only element from array and do an inline load from the appropriate slot
  3991. // if the type hasn't changed since the last time this ScopedStFld was executed.
  3992. //
  3993. //----------------------------------------------------------------------------
  3994. IR::Instr *
  3995. LowererMD::GenerateFastScopedStFld(IR::Instr * instrStScopedFld)
  3996. {
  3997. // CMP [base + offset(length)], 1 -- get the length on array and test if it is 1.
  3998. // JNE $helper
  3999. // MOV r1, [base + offset(scopes)] -- load the first scope
  4000. // MOV r2, r1->type
  4001. // CMP r2, [&(inlineCache->u.local.type)] -- check type
  4002. // JNE $helper
  4003. // MOV r1, r1->slots -- load the slots array
  4004. // MOV r2, [&(inlineCache->u.local.slotIndex)] -- load the cached slot index
  4005. // [r1 + r2*4] = MOV value -- store the value directly to the slot
  4006. // JMP $fallthru
  4007. // $helper:
  4008. // CALL PatchSetPropertyScoped(inlineCache, base, field, value, defaultInstance, scriptContext)
  4009. // $fallthru:
  4010. IR::RegOpnd * opndBase;
  4011. IR::Instr * instr;
  4012. IR::IndirOpnd * indirOpnd;
  4013. IR::LabelInstr * labelHelper;
  4014. IR::Opnd * opndDst;
  4015. IR::RegOpnd * inlineCache;
  4016. IR::RegOpnd *r1;
  4017. IR::LabelInstr * labelFallThru;
  4018. IR::Opnd *newValue = instrStScopedFld->GetSrc1();
  4019. // IR::Opnd *defaultInstance = instrStScopedFld->UnlinkSrc2();
  4020. opndDst = instrStScopedFld->GetDst();
  4021. AssertMsg(opndDst->IsSymOpnd() && opndDst->AsSymOpnd()->IsPropertySymOpnd() && opndDst->AsSymOpnd()->m_sym->IsPropertySym(),
  4022. "Expected property sym operand as dst of StScoped");
  4023. IR::PropertySymOpnd * propertySymOpnd = opndDst->AsPropertySymOpnd();
  4024. opndBase = propertySymOpnd->CreatePropertyOwnerOpnd(m_func);
  4025. labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  4026. AssertMsg(opndBase->m_sym->m_isSingleDef, "We assume this isn't redefined");
  4027. // CMP [base + offset(length)], 1 -- get the length on array and test if it is 1.
  4028. indirOpnd = IR::IndirOpnd::New(opndBase, Js::FrameDisplay::GetOffsetOfLength(), TyInt16, this->m_func);
  4029. instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  4030. instr->SetSrc1(indirOpnd);
  4031. instr->SetSrc2(IR::IntConstOpnd::New(0x1, TyInt8, this->m_func));
  4032. instrStScopedFld->InsertBefore(instr);
  4033. // JNE $helper
  4034. instr = IR::BranchInstr::New(Js::OpCode::JNE, labelHelper, this->m_func);
  4035. instrStScopedFld->InsertBefore(instr);
  4036. // MOV r1, [base + offset(scopes)] -- load the first scope
  4037. indirOpnd = IR::IndirOpnd::New(opndBase, Js::FrameDisplay::GetOffsetOfScopes(), TyMachReg, this->m_func);
  4038. r1 = IR::RegOpnd::New(TyMachReg, this->m_func);
  4039. instr = IR::Instr::New(Js::OpCode::MOV, r1, indirOpnd, this->m_func);
  4040. instrStScopedFld->InsertBefore(instr);
  4041. //first load the inlineCache type
  4042. inlineCache = IR::RegOpnd::New(TyMachPtr, this->m_func);
  4043. Assert(inlineCache != nullptr);
  4044. IR::RegOpnd * opndType = IR::RegOpnd::New(TyMachReg, this->m_func);
  4045. labelFallThru = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  4046. r1->m_sym->m_isNotInt = true;
  4047. // Load the type
  4048. this->m_lowerer->GenerateObjectTestAndTypeLoad(instrStScopedFld, r1, opndType, labelHelper);
  4049. // Check the local cache with the tagged type
  4050. IR::RegOpnd * opndTaggedType = IR::RegOpnd::New(TyMachReg, this->m_func);
  4051. GenerateLoadTaggedType(instrStScopedFld, opndType, opndTaggedType);
  4052. instrStScopedFld->InsertBefore(IR::Instr::New(Js::OpCode::MOV, inlineCache, m_lowerer->LoadRuntimeInlineCacheOpnd(instrStScopedFld, propertySymOpnd), this->m_func));
  4053. GenerateLocalInlineCacheCheck(instrStScopedFld, opndTaggedType, inlineCache, labelHelper);
  4054. GenerateStFldFromLocalInlineCache(instrStScopedFld, r1, newValue, inlineCache, labelFallThru, false);
  4055. // $helper:
  4056. // CALL PatchSetPropertyScoped(inlineCache, opndBase, propertyId, newValue, defaultInstance, scriptContext)
  4057. // $fallthru:
  4058. instrStScopedFld->InsertBefore(labelHelper);
  4059. instrStScopedFld->InsertAfter(labelFallThru);
  4060. return instrStScopedFld->m_prev;
  4061. }
  4062. IR::Opnd *
  4063. LowererMD::CreateStackArgumentsSlotOpnd()
  4064. {
  4065. StackSym *sym = StackSym::New(TyMachReg, this->m_func);
  4066. sym->m_offset = -MachArgsSlotOffset;
  4067. sym->m_allocated = true;
  4068. return IR::SymOpnd::New(sym, TyMachReg, this->m_func);
  4069. }
  4070. IR::RegOpnd *
  4071. LowererMD::GenerateUntagVar(IR::RegOpnd * src, IR::LabelInstr * labelFail, IR::Instr * insertBeforeInstr, bool generateTagCheck)
  4072. {
  4073. Assert(src->IsVar());
  4074. // MOV valueOpnd, index
  4075. IR::RegOpnd *valueOpnd = IR::RegOpnd::New(TyInt32, this->m_func);
  4076. //
  4077. // Convert Index to 32 bits.
  4078. //
  4079. IR::Opnd * opnd = src->UseWithNewType(TyMachReg, this->m_func);
  4080. #if INT32VAR
  4081. if (generateTagCheck)
  4082. {
  4083. Assert(!opnd->IsTaggedInt());
  4084. this->GenerateSmIntTest(opnd, insertBeforeInstr, labelFail);
  4085. }
  4086. // Moving into r2 clears the tag bits on AMD64.
  4087. IR::Instr * instr = IR::Instr::New(Js::OpCode::MOV_TRUNC, valueOpnd, opnd, this->m_func);
  4088. insertBeforeInstr->InsertBefore(instr);
  4089. #else
  4090. IR::Instr * instr = IR::Instr::New(Js::OpCode::MOV, valueOpnd, opnd, this->m_func);
  4091. insertBeforeInstr->InsertBefore(instr);
  4092. // SAR valueOpnd, Js::VarTag_Shift
  4093. instr = IR::Instr::New(Js::OpCode::SAR, valueOpnd, valueOpnd,
  4094. IR::IntConstOpnd::New(Js::VarTag_Shift, TyInt8, this->m_func), this->m_func);
  4095. insertBeforeInstr->InsertBefore(instr);
  4096. if (generateTagCheck)
  4097. {
  4098. Assert(!opnd->IsTaggedInt());
  4099. // SAR set the carry flag (CF) to 1 if the lower bit is 1
  4100. // JAE will jmp if CF = 0
  4101. instr = IR::BranchInstr::New(Js::OpCode::JAE, labelFail, this->m_func);
  4102. insertBeforeInstr->InsertBefore(instr);
  4103. }
  4104. #endif
  4105. return valueOpnd;
  4106. }
  4107. IR::RegOpnd *LowererMD::LoadNonnegativeIndex(
  4108. IR::RegOpnd *indexOpnd,
  4109. const bool skipNegativeCheck,
  4110. IR::LabelInstr *const notTaggedIntLabel,
  4111. IR::LabelInstr *const negativeLabel,
  4112. IR::Instr *const insertBeforeInstr)
  4113. {
  4114. Assert(indexOpnd);
  4115. Assert(indexOpnd->IsVar() || indexOpnd->GetType() == TyInt32 || indexOpnd->GetType() == TyUint32);
  4116. Assert(indexOpnd->GetType() != TyUint32 || skipNegativeCheck);
  4117. Assert(!indexOpnd->IsVar() || notTaggedIntLabel);
  4118. Assert(skipNegativeCheck || negativeLabel);
  4119. Assert(insertBeforeInstr);
  4120. if(indexOpnd->IsVar())
  4121. {
  4122. if (indexOpnd->GetValueType().IsLikelyFloat()
  4123. #ifdef _M_IX86
  4124. && AutoSystemInfo::Data.SSE2Available()
  4125. #endif
  4126. )
  4127. {
  4128. return m_lowerer->LoadIndexFromLikelyFloat(indexOpnd, skipNegativeCheck, notTaggedIntLabel, negativeLabel, insertBeforeInstr);
  4129. }
  4130. // mov intIndex, index
  4131. // sar intIndex, 1
  4132. // jae $notTaggedIntOrNegative
  4133. indexOpnd = GenerateUntagVar(indexOpnd, notTaggedIntLabel, insertBeforeInstr, !indexOpnd->IsTaggedInt());
  4134. }
  4135. if(!skipNegativeCheck)
  4136. {
  4137. // test index, index
  4138. // js $notTaggedIntOrNegative
  4139. Lowerer::InsertTestBranch(indexOpnd, indexOpnd, Js::OpCode::JSB, negativeLabel, insertBeforeInstr);
  4140. }
  4141. return indexOpnd;
  4142. }
  4143. IR::IndirOpnd *
  4144. LowererMD::GenerateFastElemIStringIndexCommon(IR::Instr * instrInsert, bool isStore, IR::IndirOpnd * indirOpnd, IR::LabelInstr * labelHelper)
  4145. {
  4146. IR::RegOpnd *indexOpnd = indirOpnd->GetIndexOpnd();
  4147. IR::RegOpnd *baseOpnd = indirOpnd->GetBaseOpnd();
  4148. Assert(baseOpnd != nullptr);
  4149. Assert(indexOpnd->GetValueType().IsLikelyString());
  4150. // Generates:
  4151. // CMP indexOpnd, PropertyString::`vtable' -- check if index is property string
  4152. // JNE $helper
  4153. // MOV propertyCacheOpnd, index->propCache
  4154. // TEST baseOpnd, AtomTag -- check base not tagged int
  4155. // JNE $helper
  4156. // MOV objectTypeOpnd, baseOpnd->type
  4157. // CMP [propertyCacheOpnd->type], objectTypeOpnd -- check if object type match the cache
  4158. // JNE $helper
  4159. // CMP [propertyCacheOpnd->isInlineSlot,1] -- check if it is inline slots
  4160. // JEQ $inlineSlot
  4161. // MOV slotOpnd, [baseOpnd->slot] -- load the aux slot
  4162. // JMP $afterLabel
  4163. // $inlineSlot:
  4164. // MOV slotOpnd, baseOpnd -- use the object as start of the slot offset
  4165. // $afterLabel:
  4166. // MOVZXW offsetOpnd, [propertyCacheOpnd->dataSlotIndex] -- load the slot index
  4167. // <use [slotOpnd + offsetOpnd * PtrSize]>
  4168. // CMP indexOpnd, PropertyString::`vtable' -- check if index is property string
  4169. // JNE $helper
  4170. this->m_lowerer->InsertCompareBranch(
  4171. IR::IndirOpnd::New(indexOpnd, 0, TyMachPtr, this->m_func),
  4172. m_lowerer->LoadVTableValueOpnd(instrInsert, VTableValue::VtablePropertyString),
  4173. Js::OpCode::BrNeq_A, labelHelper, instrInsert);
  4174. // MOV propertyCacheOpnd, indexOpnd->propCache
  4175. IR::RegOpnd * propertyCacheOpnd = IR::RegOpnd::New(TyMachPtr, this->m_func);
  4176. IR::Instr * loadPropertyCacheInstr = IR::Instr::New(Js::OpCode::MOV, propertyCacheOpnd,
  4177. IR::IndirOpnd::New(indexOpnd, Js::PropertyString::GetOffsetOfPropertyCache(), TyMachPtr,
  4178. this->m_func), this->m_func);
  4179. instrInsert->InsertBefore(loadPropertyCacheInstr);
  4180. // TEST baseOpnd, AtomTag -- check base not tagged int
  4181. // JNE $helper
  4182. if(!baseOpnd->IsNotTaggedValue())
  4183. {
  4184. GenerateObjectTest(baseOpnd, instrInsert, labelHelper);
  4185. }
  4186. // MOV s2, baseOpnd->type
  4187. // CMP [propertyCacheOpnd->type], s2 -- check if object type match the cache
  4188. // JNE $helper
  4189. IR::RegOpnd * objectTypeOpnd = IR::RegOpnd::New(TyMachPtr, this->m_func);
  4190. IR::Instr * loadObjectTypeInstr = IR::Instr::New(Js::OpCode::MOV,
  4191. objectTypeOpnd, IR::IndirOpnd::New(baseOpnd, Js::RecyclableObject::GetOffsetOfType(), TyMachPtr, this->m_func),
  4192. this->m_func);
  4193. instrInsert->InsertBefore(loadObjectTypeInstr);
  4194. IR::Instr * checkTypeInstr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  4195. checkTypeInstr->SetSrc1(IR::IndirOpnd::New(propertyCacheOpnd, (int32)offsetof(Js::PropertyCache, type), TyMachPtr, this->m_func));
  4196. checkTypeInstr->SetSrc2(objectTypeOpnd);
  4197. instrInsert->InsertBefore(checkTypeInstr);
  4198. instrInsert->InsertBefore(IR::BranchInstr::New(Js::OpCode::JNE, labelHelper, this->m_func));
  4199. if (isStore)
  4200. {
  4201. IR::IndirOpnd* isStoreEnabledOpnd = IR::IndirOpnd::New(propertyCacheOpnd, (int32)offsetof(Js::PropertyCache, isStoreFieldEnabled), TyInt8, this->m_func);
  4202. IR::IntConstOpnd* zeroOpnd = IR::IntConstOpnd::New(0, TyInt8, this->m_func, /* dontEncode = */ true);
  4203. this->m_lowerer->InsertCompareBranch(isStoreEnabledOpnd, zeroOpnd, Js::OpCode::BrEq_A, labelHelper, instrInsert);
  4204. }
  4205. // CMP [propertyCacheOpnd->isInlineSlot,1] -- check if it is inline slots
  4206. // JEQ $inlineSlot
  4207. IR::Instr * inlineSlotTestInstr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  4208. inlineSlotTestInstr->SetSrc1(IR::IndirOpnd::New(propertyCacheOpnd, (int32)offsetof(Js::PropertyCache, isInlineSlot), TyInt8, this->m_func));
  4209. inlineSlotTestInstr->SetSrc2(IR::IntConstOpnd::New(1, TyInt8, this->m_func));
  4210. instrInsert->InsertBefore(inlineSlotTestInstr);
  4211. IR::LabelInstr * isInlineSlotLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  4212. instrInsert->InsertBefore(IR::BranchInstr::New(Js::OpCode::JEQ, isInlineSlotLabel, this->m_func));
  4213. // MOV slotOpnd, [baseOpnd->slot] -- load the aux slot
  4214. // JMP $afterLabel
  4215. IR::RegOpnd * slotOpnd = IR::RegOpnd::New(TyMachPtr, this->m_func);
  4216. instrInsert->InsertBefore(IR::Instr::New(Js::OpCode::MOV, slotOpnd,
  4217. IR::IndirOpnd::New(baseOpnd, Js::DynamicObject::GetOffsetOfAuxSlots(), TyMachPtr, this->m_func), this->m_func));
  4218. IR::LabelInstr * afterLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  4219. instrInsert->InsertBefore(IR::BranchInstr::New(Js::OpCode::JMP, afterLabel, this->m_func));
  4220. // $inlineSlot:
  4221. // MOV slotOpnd, baseOpnd -- use the object as start of the slot offset
  4222. instrInsert->InsertBefore(isInlineSlotLabel);
  4223. instrInsert->InsertBefore(IR::Instr::New(Js::OpCode::MOV, slotOpnd, baseOpnd, this->m_func));
  4224. // $afterLabel:
  4225. // MOVZXW offsetOpnd, [propertyCacheOpnd->dataSlotIndex] -- load the slot index
  4226. instrInsert->InsertBefore(afterLabel);
  4227. IR::RegOpnd * offsetOpnd = IR::RegOpnd::New(TyInt32, this->m_func);
  4228. instrInsert->InsertBefore(IR::Instr::New(Js::OpCode::MOVZXW, offsetOpnd,
  4229. IR::IndirOpnd::New(propertyCacheOpnd, (int32)offsetof(Js::PropertyCache, dataSlotIndex), TyUint16, this->m_func), this->m_func));
  4230. // return [slotOpnd + offsetOpnd * PtrSize]
  4231. return IR::IndirOpnd::New(slotOpnd, offsetOpnd, this->GetDefaultIndirScale(), TyVar, this->m_func);
  4232. }
  4233. void
  4234. LowererMD::GenerateFastBrBReturn(IR::Instr *instr)
  4235. {
  4236. Assert(instr->m_opcode == Js::OpCode::BrOnEmpty || instr->m_opcode == Js::OpCode::BrOnNotEmpty);
  4237. AssertMsg(instr->GetSrc1() != nullptr && instr->GetSrc2() == nullptr, "Expected 1 src opnds on BrB");
  4238. Assert(instr->GetSrc1()->IsRegOpnd());
  4239. IR::RegOpnd * forInEnumeratorOpnd = instr->GetSrc1()->AsRegOpnd();
  4240. IR::LabelInstr * labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  4241. // MOV firstPrototypeOpnd, forInEnumerator->firstPrototype
  4242. // TEST firstPrototypeOpnd, firstPrototypeOpnd
  4243. // JNE $helper
  4244. IR::RegOpnd * firstPrototypeOpnd = IR::RegOpnd::New(TyMachPtr, this->m_func);
  4245. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, firstPrototypeOpnd,
  4246. IR::IndirOpnd::New(forInEnumeratorOpnd, Js::ForInObjectEnumerator::GetOffsetOfFirstPrototype(), TyMachPtr, this->m_func), this->m_func));
  4247. IR::Instr * checkFirstPrototypeNullInstr = IR::Instr::New(Js::OpCode::TEST, this->m_func);
  4248. checkFirstPrototypeNullInstr->SetSrc1(firstPrototypeOpnd);
  4249. checkFirstPrototypeNullInstr->SetSrc2(firstPrototypeOpnd);
  4250. instr->InsertBefore(checkFirstPrototypeNullInstr);
  4251. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JNE, labelHelper, this->m_func));
  4252. typedef Js::DynamicObjectSnapshotEnumeratorWPCache<Js::BigPropertyIndex, true, false> SmallDynamicObjectSnapshotEnumeratorWPCache;
  4253. // MOV currentEnumeratorOpnd, forInEnumerator->currentEnumerator
  4254. // CMP currentEnumeratorOpnd, SmallDynamicObjectSnapshotEnumeratorWPCache::`vtable
  4255. // JNE $helper
  4256. IR::RegOpnd * currentEnumeratorOpnd = IR::RegOpnd::New(TyMachPtr, this->m_func);
  4257. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, currentEnumeratorOpnd,
  4258. IR::IndirOpnd::New(forInEnumeratorOpnd, Js::ForInObjectEnumerator::GetOffsetOfCurrentEnumerator(), TyMachPtr, this->m_func), this->m_func));
  4259. this->m_lowerer->InsertCompareBranch(
  4260. IR::IndirOpnd::New(currentEnumeratorOpnd, 0, TyMachPtr, this->m_func),
  4261. m_lowerer->LoadVTableValueOpnd(instr, VTableValue::VtableSmallDynamicObjectSnapshotEnumeratorWPCache),
  4262. Js::OpCode::BrNeq_A, labelHelper, instr);
  4263. // MOV arrayEnumeratorOpnd, currentEnumerator->arrayEnumerator
  4264. // TEST arrayEnumeratorOpnd, arrayEnumeratorOpnd
  4265. // JNE $helper
  4266. IR::RegOpnd * arrayEnumeratorOpnd = IR::RegOpnd::New(TyMachPtr, this->m_func);
  4267. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, arrayEnumeratorOpnd,
  4268. IR::IndirOpnd::New(currentEnumeratorOpnd, SmallDynamicObjectSnapshotEnumeratorWPCache::GetOffsetOfArrayEnumerator(), TyMachPtr, this->m_func), this->m_func));
  4269. IR::Instr * checkArrayEnumeratorNullInstr = IR::Instr::New(Js::OpCode::TEST, this->m_func);
  4270. checkArrayEnumeratorNullInstr->SetSrc1(arrayEnumeratorOpnd);
  4271. checkArrayEnumeratorNullInstr->SetSrc2(arrayEnumeratorOpnd);
  4272. instr->InsertBefore(checkArrayEnumeratorNullInstr);
  4273. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JNE, labelHelper, this->m_func));
  4274. // MOV objectOpnd, currentEnumerator->object
  4275. // MOV initialTypeOpnd, currentEnumerator->initialType
  4276. // CMP initialTypeOpnd, objectOpnd->type
  4277. // JNE $helper
  4278. IR::RegOpnd * objectOpnd = IR::RegOpnd::New(TyMachPtr, this->m_func);
  4279. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, objectOpnd,
  4280. IR::IndirOpnd::New(currentEnumeratorOpnd, SmallDynamicObjectSnapshotEnumeratorWPCache::GetOffsetOfObject(), TyMachPtr, this->m_func), this->m_func));
  4281. IR::RegOpnd * initialTypeOpnd = IR::RegOpnd::New(TyMachPtr, this->m_func);
  4282. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, initialTypeOpnd,
  4283. IR::IndirOpnd::New(currentEnumeratorOpnd, SmallDynamicObjectSnapshotEnumeratorWPCache::GetOffsetOfInitialType(), TyMachPtr, this->m_func), this->m_func));
  4284. IR::Instr * checkTypeInstr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  4285. checkTypeInstr->SetSrc1(initialTypeOpnd);
  4286. checkTypeInstr->SetSrc2(IR::IndirOpnd::New(objectOpnd, Js::DynamicObject::GetOffsetOfType(), TyMachPtr, this->m_func));
  4287. instr->InsertBefore(checkTypeInstr);
  4288. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JNE, labelHelper, this->m_func));
  4289. // MOV enumeratedCountOpnd, currentEnumeratorOpnd->enumeratedCount
  4290. // MOV cachedDataOpnd, currentEnumeratorOpnd->cachedData
  4291. // CMP enumeratedCountOpnd, cachedDataOpnd->cachedCount
  4292. // JGE $helper
  4293. IR::RegOpnd * enumeratedCountOpnd = IR::RegOpnd::New(TyUint32, m_func);
  4294. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, enumeratedCountOpnd,
  4295. IR::IndirOpnd::New(currentEnumeratorOpnd, SmallDynamicObjectSnapshotEnumeratorWPCache::GetOffsetOfEnumeratedCount(), TyUint32, this->m_func), this->m_func));
  4296. IR::RegOpnd * cachedDataOpnd = IR::RegOpnd::New(TyMachPtr, m_func);
  4297. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, cachedDataOpnd,
  4298. IR::IndirOpnd::New(currentEnumeratorOpnd, SmallDynamicObjectSnapshotEnumeratorWPCache::GetOffsetOfCachedData(), TyMachPtr, this->m_func), this->m_func));
  4299. IR::Instr * checkEnumeratedCountInstr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  4300. checkEnumeratedCountInstr->SetSrc1(enumeratedCountOpnd);
  4301. checkEnumeratedCountInstr->SetSrc2(IR::IndirOpnd::New(cachedDataOpnd, SmallDynamicObjectSnapshotEnumeratorWPCache::GetOffsetOfCachedDataCachedCount(), TyUint32, this->m_func));
  4302. instr->InsertBefore(checkEnumeratedCountInstr);
  4303. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JGE, labelHelper, this->m_func));
  4304. // MOV propertyAttributesOpnd, cachedData->attributes
  4305. // MOV objectPropertyAttributesOpnd, propertyAttributesOpnd[enumeratedCount]
  4306. // CMP objectPropertyAttributesOpnd & PropertyEnumerable, PropertyEnumerable
  4307. // JNE $helper
  4308. IR::RegOpnd * propertyAttributesOpnd = IR::RegOpnd::New(TyMachPtr, m_func);
  4309. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, propertyAttributesOpnd,
  4310. IR::IndirOpnd::New(cachedDataOpnd, SmallDynamicObjectSnapshotEnumeratorWPCache::GetOffsetOfCachedDataPropertyAttributes(), TyMachPtr, this->m_func), this->m_func));
  4311. IR::RegOpnd * objectPropertyAttributesOpnd = IR::RegOpnd::New(TyUint8, m_func);
  4312. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, objectPropertyAttributesOpnd,
  4313. IR::IndirOpnd::New(propertyAttributesOpnd, enumeratedCountOpnd, IndirScale1, TyUint8, this->m_func), this->m_func));
  4314. IR::Instr * andPropertyEnumerableInstr = Lowerer::InsertAnd(IR::RegOpnd::New(TyUint8, instr->m_func), objectPropertyAttributesOpnd, IR::IntConstOpnd::New(0x01, TyUint8, this->m_func), instr);
  4315. IR::Instr * checkEnumerableInstr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  4316. checkEnumerableInstr->SetSrc1(andPropertyEnumerableInstr->GetDst());
  4317. checkEnumerableInstr->SetSrc2(IR::IntConstOpnd::New(0x01, TyUint8, this->m_func));
  4318. instr->InsertBefore(checkEnumerableInstr);
  4319. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JNE, labelHelper, this->m_func));
  4320. IR::Opnd * opndDst = instr->GetDst(); // ForIn result propertyString
  4321. Assert(opndDst->IsRegOpnd());
  4322. // MOV stringsOpnd, cachedData->strings
  4323. // MOV opndDst, stringsOpnd[enumeratedCount]
  4324. IR::RegOpnd * stringsOpnd = IR::RegOpnd::New(TyMachPtr, m_func);
  4325. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, stringsOpnd,
  4326. IR::IndirOpnd::New(cachedDataOpnd, SmallDynamicObjectSnapshotEnumeratorWPCache::GetOffsetOfCachedDataStrings(), TyMachPtr, this->m_func), this->m_func));
  4327. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, opndDst,
  4328. IR::IndirOpnd::New(stringsOpnd, enumeratedCountOpnd, this->GetDefaultIndirScale(), TyVar, this->m_func), this->m_func));
  4329. // MOV indexesOpnd, cachedData->indexes
  4330. // MOV objectIndexOpnd, indexesOpnd[enumeratedCount]
  4331. // MOV currentEnumeratorOpnd->objectIndex, objectIndexOpnd
  4332. IR::RegOpnd * indexesOpnd = IR::RegOpnd::New(TyMachPtr, m_func);
  4333. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, indexesOpnd,
  4334. IR::IndirOpnd::New(cachedDataOpnd, SmallDynamicObjectSnapshotEnumeratorWPCache::GetOffsetOfCachedDataIndexes(), TyMachPtr, this->m_func), this->m_func));
  4335. IR::RegOpnd * objectIndexOpnd = IR::RegOpnd::New(TyUint32, m_func);
  4336. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, objectIndexOpnd,
  4337. IR::IndirOpnd::New(indexesOpnd, enumeratedCountOpnd, IndirScale4, TyUint32, this->m_func), this->m_func));
  4338. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV,
  4339. IR::IndirOpnd::New(currentEnumeratorOpnd, SmallDynamicObjectSnapshotEnumeratorWPCache::GetOffsetOfObjectIndex(), TyUint32, this->m_func),
  4340. objectIndexOpnd, this->m_func));
  4341. // INC enumeratedCountOpnd
  4342. // MOV currentEnumeratorOpnd->enumeratedCount, enumeratedCountOpnd
  4343. instr->InsertBefore(IR::Instr::New(Js::OpCode::INC, enumeratedCountOpnd, enumeratedCountOpnd, this->m_func));
  4344. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV,
  4345. IR::IndirOpnd::New(currentEnumeratorOpnd, SmallDynamicObjectSnapshotEnumeratorWPCache::GetOffsetOfEnumeratedCount(), TyUint32, this->m_func),
  4346. enumeratedCountOpnd, this->m_func));
  4347. // We know result propertyString (opndDst) != NULL
  4348. IR::LabelInstr * labelAfter = instr->GetOrCreateContinueLabel();
  4349. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JMP,
  4350. instr->m_opcode == Js::OpCode::BrOnNotEmpty ? instr->AsBranchInstr()->GetTarget() : labelAfter,
  4351. this->m_func));
  4352. // $helper
  4353. instr->InsertBefore(labelHelper);
  4354. // $after
  4355. }
  4356. // Inlines fast-path for int Mul/Add or int Mul/Sub. If not int, call MulAdd/MulSub helper
  4357. bool LowererMD::TryGenerateFastMulAdd(IR::Instr * instrAdd, IR::Instr ** pInstrPrev)
  4358. {
  4359. IR::Instr *instrMul = instrAdd->GetPrevRealInstrOrLabel();
  4360. IR::Opnd *addSrc;
  4361. IR::RegOpnd *addCommonSrcOpnd;
  4362. Assert(instrAdd->m_opcode == Js::OpCode::Add_A || instrAdd->m_opcode == Js::OpCode::Sub_A);
  4363. bool isSub = (instrAdd->m_opcode == Js::OpCode::Sub_A) ? true : false;
  4364. // Mul needs to be a single def reg
  4365. if (instrMul->m_opcode != Js::OpCode::Mul_A || instrMul->GetDst()->IsRegOpnd() == false)
  4366. {
  4367. // Cannot generate MulAdd
  4368. return false;
  4369. }
  4370. if (instrMul->HasBailOutInfo())
  4371. {
  4372. // Bailout will be generated for the Add, but not the Mul.
  4373. // We could handle this, but this path isn't used that much anymore.
  4374. return false;
  4375. }
  4376. IR::RegOpnd *regMulDst = instrMul->GetDst()->AsRegOpnd();
  4377. if (regMulDst->m_sym->m_isSingleDef == false)
  4378. {
  4379. // Cannot generate MulAdd
  4380. return false;
  4381. }
  4382. // Only handle a * b + c, so dst of Mul needs to match left source of Add
  4383. if (instrMul->GetDst()->IsEqual(instrAdd->GetSrc1()))
  4384. {
  4385. addCommonSrcOpnd = instrAdd->GetSrc1()->AsRegOpnd();
  4386. addSrc = instrAdd->GetSrc2();
  4387. }
  4388. else if (instrMul->GetDst()->IsEqual(instrAdd->GetSrc2()))
  4389. {
  4390. addSrc = instrAdd->GetSrc1();
  4391. addCommonSrcOpnd = instrAdd->GetSrc2()->AsRegOpnd();
  4392. }
  4393. else
  4394. {
  4395. return false;
  4396. }
  4397. // Only handle a * b + c where c != a * b
  4398. if (instrAdd->GetSrc1()->IsEqual(instrAdd->GetSrc2()))
  4399. {
  4400. return false;
  4401. }
  4402. if (addCommonSrcOpnd->m_isTempLastUse == false)
  4403. {
  4404. return false;
  4405. }
  4406. IR::Opnd *mulSrc1 = instrMul->GetSrc1();
  4407. IR::Opnd *mulSrc2 = instrMul->GetSrc2();
  4408. if (mulSrc1->IsRegOpnd() && mulSrc1->AsRegOpnd()->IsTaggedInt()
  4409. && mulSrc2->IsRegOpnd() && mulSrc2->AsRegOpnd()->IsTaggedInt())
  4410. {
  4411. return false;
  4412. }
  4413. // Save prevInstr for the main lower loop
  4414. *pInstrPrev = instrMul->m_prev;
  4415. // Generate int31 fast-path for Mul, go to MulAdd helper if it fails, or one of the source is marked notInt
  4416. if (!(addSrc->IsRegOpnd() && addSrc->AsRegOpnd()->IsNotInt())
  4417. && !(mulSrc1->IsRegOpnd() && mulSrc1->AsRegOpnd()->IsNotInt())
  4418. && !(mulSrc2->IsRegOpnd() && mulSrc2->AsRegOpnd()->IsNotInt()))
  4419. {
  4420. this->GenerateFastMul(instrMul);
  4421. IR::LabelInstr *labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  4422. IR::Instr *instr = IR::BranchInstr::New(Js::OpCode::JMP, labelHelper, this->m_func);
  4423. instrMul->InsertBefore(instr);
  4424. // Generate int31 fast-path for Add
  4425. bool success;
  4426. if (isSub)
  4427. {
  4428. success = this->GenerateFastSub(instrAdd);
  4429. }
  4430. else
  4431. {
  4432. success = this->GenerateFastAdd(instrAdd);
  4433. }
  4434. if (!success)
  4435. {
  4436. labelHelper->isOpHelper = false;
  4437. }
  4438. // Generate MulAdd helper call
  4439. instrAdd->InsertBefore(labelHelper);
  4440. }
  4441. if (instrAdd->dstIsTempNumber)
  4442. {
  4443. m_lowerer->LoadHelperTemp(instrAdd, instrAdd);
  4444. }
  4445. else
  4446. {
  4447. IR::Opnd *tempOpnd = IR::IntConstOpnd::New(0, TyInt32, this->m_func);
  4448. this->LoadHelperArgument(instrAdd, tempOpnd);
  4449. }
  4450. this->m_lowerer->LoadScriptContext(instrAdd);
  4451. IR::JnHelperMethod helper;
  4452. if (addSrc == instrAdd->GetSrc2())
  4453. {
  4454. instrAdd->FreeSrc1();
  4455. IR::Opnd *addOpnd = instrAdd->UnlinkSrc2();
  4456. this->LoadHelperArgument(instrAdd, addOpnd);
  4457. helper = isSub ? IR::HelperOp_MulSubRight : IR::HelperOp_MulAddRight;
  4458. }
  4459. else
  4460. {
  4461. instrAdd->FreeSrc2();
  4462. IR::Opnd *addOpnd = instrAdd->UnlinkSrc1();
  4463. this->LoadHelperArgument(instrAdd, addOpnd);
  4464. helper = isSub ? IR::HelperOp_MulSubLeft : IR::HelperOp_MulAddLeft;
  4465. }
  4466. IR::Opnd *src2 = instrMul->UnlinkSrc2();
  4467. this->LoadHelperArgument(instrAdd, src2);
  4468. IR::Opnd *src1 = instrMul->UnlinkSrc1();
  4469. this->LoadHelperArgument(instrAdd, src1);
  4470. this->ChangeToHelperCall(instrAdd, helper);
  4471. instrMul->Remove();
  4472. return true;
  4473. }
  4474. void
  4475. LowererMD::GenerateFastAbs(IR::Opnd *dst, IR::Opnd *src, IR::Instr *callInstr, IR::Instr *insertInstr, IR::LabelInstr *labelHelper, IR::LabelInstr *doneLabel)
  4476. {
  4477. // TEST src1, AtomTag
  4478. // JEQ $float
  4479. // MOV EAX, src
  4480. // SAR EAX, AtomTag_Int32
  4481. // CDQ
  4482. // XOR EAX, EDX
  4483. // SUB EAX, EDX
  4484. // SHL EAX, AtomTag_Int32
  4485. // JO $labelHelper
  4486. // INC EAX
  4487. // MOV dst, EAX
  4488. // JMP $done
  4489. // $float
  4490. // CMP [src], JavascriptNumber.vtable
  4491. // JNE $helper
  4492. // MOVSD r1, [src + offsetof(value)]
  4493. // ANDPD r1, absDoubleCst
  4494. // dst = DoubleToVar(r1)
  4495. IR::Instr *instr = nullptr;
  4496. IR::LabelInstr *labelFloat = nullptr;
  4497. bool isInt = false;
  4498. bool isNotInt = false;
  4499. if (src->IsRegOpnd())
  4500. {
  4501. if (src->AsRegOpnd()->IsTaggedInt())
  4502. {
  4503. isInt = true;
  4504. }
  4505. else if (src->AsRegOpnd()->IsNotInt())
  4506. {
  4507. isNotInt = true;
  4508. }
  4509. }
  4510. else if (src->IsAddrOpnd())
  4511. {
  4512. IR::AddrOpnd *varOpnd = src->AsAddrOpnd();
  4513. Assert(varOpnd->IsVar() && Js::TaggedInt::Is(varOpnd->m_address));
  4514. #ifdef _M_X64
  4515. __int64 absValue = ::_abs64(Js::TaggedInt::ToInt32(varOpnd->m_address));
  4516. #else
  4517. __int32 absValue = ::abs(Js::TaggedInt::ToInt32(varOpnd->m_address));
  4518. #endif
  4519. if (!Js::TaggedInt::IsOverflow(absValue))
  4520. {
  4521. varOpnd->SetAddress(Js::TaggedInt::ToVarUnchecked((__int32)absValue), IR::AddrOpndKindConstantVar);
  4522. instr = IR::Instr::New(Js::OpCode::MOV, dst, varOpnd, this->m_func);
  4523. insertInstr->InsertBefore(instr);
  4524. return;
  4525. }
  4526. }
  4527. if (src->IsRegOpnd() == false)
  4528. {
  4529. IR::RegOpnd *regOpnd = IR::RegOpnd::New(TyVar, this->m_func);
  4530. instr = IR::Instr::New(Js::OpCode::MOV, regOpnd, src, this->m_func);
  4531. insertInstr->InsertBefore(instr);
  4532. src = regOpnd;
  4533. }
  4534. #ifdef _M_IX86
  4535. bool emitFloatAbs = !isInt && AutoSystemInfo::Data.SSE2Available();
  4536. #else
  4537. bool emitFloatAbs = !isInt;
  4538. #endif
  4539. if (!isNotInt)
  4540. {
  4541. if (!isInt)
  4542. {
  4543. IR::LabelInstr *label = labelHelper;
  4544. if (emitFloatAbs)
  4545. {
  4546. label = labelFloat = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  4547. }
  4548. GenerateSmIntTest(src, insertInstr, label);
  4549. }
  4550. // MOV EAX, src
  4551. IR::RegOpnd *regEAX = IR::RegOpnd::New(TyInt32, this->m_func);
  4552. regEAX->SetReg(LowererMDArch::GetRegIMulDestLower());
  4553. instr = IR::Instr::New(Js::OpCode::MOV, regEAX, src, this->m_func);
  4554. insertInstr->InsertBefore(instr);
  4555. #ifdef _M_IX86
  4556. // SAR EAX, AtomTag_Int32
  4557. instr = IR::Instr::New(Js::OpCode::SAR, regEAX, regEAX, IR::IntConstOpnd::New(Js::AtomTag_Int32, TyInt32, this->m_func), this->m_func);
  4558. insertInstr->InsertBefore(instr);
  4559. #endif
  4560. IR::RegOpnd *regEDX = IR::RegOpnd::New(TyInt32, this->m_func);
  4561. regEDX->SetReg(LowererMDArch::GetRegIMulHighDestLower());
  4562. // CDQ
  4563. // Note: put EDX on dst to give of def to the EDX lifetime
  4564. instr = IR::Instr::New(Js::OpCode::CDQ, regEDX, this->m_func);
  4565. insertInstr->InsertBefore(instr);
  4566. // XOR EAX, EDX
  4567. instr = IR::Instr::New(Js::OpCode::XOR, regEAX, regEAX, regEDX, this->m_func);
  4568. insertInstr->InsertBefore(instr);
  4569. // SUB EAX, EDX
  4570. instr = IR::Instr::New(Js::OpCode::SUB, regEAX, regEAX, regEDX, this->m_func);
  4571. insertInstr->InsertBefore(instr);
  4572. #ifdef _M_X64
  4573. // abs(INT_MIN) overflows a 32 bit integer.
  4574. // JO $labelHelper
  4575. instr = IR::BranchInstr::New(Js::OpCode::JO, labelHelper, this->m_func);
  4576. insertInstr->InsertBefore(instr);
  4577. #endif
  4578. #ifdef _M_IX86
  4579. // SHL EAX, AtomTag_Int32
  4580. instr = IR::Instr::New(Js::OpCode::SHL, regEAX, regEAX, IR::IntConstOpnd::New(Js::AtomTag_Int32, TyInt32, this->m_func), this->m_func);
  4581. insertInstr->InsertBefore(instr);
  4582. // JO $labelHelper
  4583. instr = IR::BranchInstr::New(Js::OpCode::JO, labelHelper, this->m_func);
  4584. insertInstr->InsertBefore(instr);
  4585. // INC EAX
  4586. instr = IR::Instr::New(Js::OpCode::INC, regEAX, regEAX, this->m_func);
  4587. insertInstr->InsertBefore(instr);
  4588. #endif
  4589. // MOV dst, EAX
  4590. instr = IR::Instr::New(Js::OpCode::MOV, dst, regEAX, this->m_func);
  4591. insertInstr->InsertBefore(instr);
  4592. #ifdef _M_X64
  4593. GenerateInt32ToVarConversion(dst, insertInstr);
  4594. #endif
  4595. }
  4596. if (labelFloat)
  4597. {
  4598. // JMP $done
  4599. instr = IR::BranchInstr::New(Js::OpCode::JMP, doneLabel, this->m_func);
  4600. insertInstr->InsertBefore(instr);
  4601. // $float
  4602. insertInstr->InsertBefore(labelFloat);
  4603. }
  4604. if (emitFloatAbs)
  4605. {
  4606. #if defined(_M_IX86)
  4607. // CMP [src], JavascriptNumber.vtable
  4608. IR::Opnd *opnd = IR::IndirOpnd::New(src->AsRegOpnd(), (int32)0, TyMachPtr, this->m_func);
  4609. instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  4610. instr->SetSrc1(opnd);
  4611. instr->SetSrc2(m_lowerer->LoadVTableValueOpnd(insertInstr, VTableValue::VtableJavascriptNumber));
  4612. insertInstr->InsertBefore(instr);
  4613. // JNE $helper
  4614. instr = IR::BranchInstr::New(Js::OpCode::JNE, labelHelper, this->m_func);
  4615. insertInstr->InsertBefore(instr);
  4616. // MOVSD r1, [src + offsetof(value)]
  4617. opnd = IR::IndirOpnd::New(src->AsRegOpnd(), Js::JavascriptNumber::GetValueOffset(), TyMachDouble, this->m_func);
  4618. IR::RegOpnd *regOpnd = IR::RegOpnd::New(TyMachDouble, this->m_func);
  4619. instr = IR::Instr::New(Js::OpCode::MOVSD, regOpnd, opnd, this->m_func);
  4620. insertInstr->InsertBefore(instr);
  4621. this->GenerateFloatAbs(regOpnd, insertInstr);
  4622. // dst = DoubleToVar(r1)
  4623. SaveDoubleToVar(callInstr->GetDst()->AsRegOpnd(), regOpnd, callInstr, insertInstr);
  4624. #elif defined(_M_X64)
  4625. // if (typeof(src) == double)
  4626. IR::RegOpnd *src64 = src->AsRegOpnd();
  4627. GenerateFloatTest(src64, insertInstr, labelHelper);
  4628. // dst64 = MOV src64
  4629. insertInstr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, dst, src64, this->m_func));
  4630. // Unconditionally set the sign bit. This will get XORd away when we remove the tag.
  4631. // dst64 = OR 0x8000000000000000
  4632. insertInstr->InsertBefore(IR::Instr::New(Js::OpCode::OR, dst, dst, IR::AddrOpnd::New((void *)MachSignBit, IR::AddrOpndKindConstant, this->m_func), this->m_func));
  4633. #endif
  4634. }
  4635. else if(!isInt)
  4636. {
  4637. // The source is not known to be a tagged int, so either it's definitely not an int (isNotInt), or the int version of
  4638. // abs failed the tag check and jumped here. We can't emit the float version of abs (!emitFloatAbs) due to SSE2 not
  4639. // being available, so jump straight to the helper.
  4640. // JMP $helper
  4641. instr = IR::BranchInstr::New(Js::OpCode::JMP, labelHelper, this->m_func);
  4642. insertInstr->InsertBefore(instr);
  4643. }
  4644. }
  4645. IR::Instr * LowererMD::GenerateFloatAbs(IR::RegOpnd * regOpnd, IR::Instr * insertInstr)
  4646. {
  4647. // ANDPS reg, absDoubleCst
  4648. IR::Opnd * opnd;
  4649. if (regOpnd->IsFloat64())
  4650. {
  4651. opnd = m_lowerer->LoadLibraryValueOpnd(insertInstr, LibraryValue::ValueAbsDoubleCst);
  4652. }
  4653. else
  4654. {
  4655. Assert(regOpnd->IsFloat32());
  4656. opnd = IR::MemRefOpnd::New((void *)&Js::JavascriptNumber::AbsFloatCst, TyFloat32, this->m_func, IR::AddrOpndKindDynamicFloatRef);
  4657. }
  4658. // ANDPS has smaller encoding then ANDPD
  4659. IR::Instr * instr = IR::Instr::New(Js::OpCode::ANDPS, regOpnd, regOpnd, opnd, this->m_func);
  4660. insertInstr->InsertBefore(instr);
  4661. Legalize(instr);
  4662. return instr;
  4663. }
  4664. bool LowererMD::GenerateFastCharAt(Js::BuiltinFunction index, IR::Opnd *dst, IR::Opnd *srcStr, IR::Opnd *srcIndex, IR::Instr *callInstr,
  4665. IR::Instr *insertInstr, IR::LabelInstr *labelHelper, IR::LabelInstr *doneLabel)
  4666. {
  4667. // if regSrcStr is not object, JMP $helper
  4668. // CMP [regSrcStr + offset(type)] , static string type -- check base string type
  4669. // JNE $helper
  4670. // MOV r1, [regSrcStr + offset(m_pszValue)]
  4671. // TEST r1, r1
  4672. // JEQ $helper
  4673. // MOV r2, srcIndex
  4674. // If r2 is not int, JMP $helper
  4675. // Convert r2 to int
  4676. // CMP [regSrcStr + offsetof(length)], r2
  4677. // JBE $helper
  4678. // MOVZX r2, [r1 + r2 * 2]
  4679. // if (charAt)
  4680. // PUSH r1
  4681. // PUSH scriptContext
  4682. // CALL GetStringFromChar
  4683. // MOV dst, EAX
  4684. // else (charCodeAt)
  4685. // if (codePointAt)
  4686. // Lowerer.GenerateFastCodePointAt -- Common inline functions
  4687. // Convert r2 to Var
  4688. // MOV dst, r2
  4689. bool isInt = false;
  4690. bool isNotTaggedValue = false;
  4691. IR::Instr *instr;
  4692. IR::RegOpnd *regSrcStr;
  4693. if (srcStr->IsRegOpnd())
  4694. {
  4695. if (srcStr->AsRegOpnd()->IsTaggedInt())
  4696. {
  4697. isInt = true;
  4698. }
  4699. else if (srcStr->AsRegOpnd()->IsNotTaggedValue())
  4700. {
  4701. isNotTaggedValue = true;
  4702. }
  4703. }
  4704. if (srcStr->IsRegOpnd() == false)
  4705. {
  4706. IR::RegOpnd *regOpnd = IR::RegOpnd::New(TyVar, this->m_func);
  4707. instr = IR::Instr::New(Js::OpCode::MOV, regOpnd, srcStr, this->m_func);
  4708. insertInstr->InsertBefore(instr);
  4709. regSrcStr = regOpnd;
  4710. }
  4711. else
  4712. {
  4713. regSrcStr = srcStr->AsRegOpnd();
  4714. }
  4715. if (!isNotTaggedValue)
  4716. {
  4717. if (!isInt)
  4718. {
  4719. GenerateObjectTest(regSrcStr, insertInstr, labelHelper);
  4720. }
  4721. else
  4722. {
  4723. // Insert delete branch opcode to tell the dbChecks not to assert on this helper label
  4724. IR::Instr *fakeBr = IR::PragmaInstr::New(Js::OpCode::DeletedNonHelperBranch, 0, this->m_func);
  4725. insertInstr->InsertBefore(fakeBr);
  4726. instr = IR::BranchInstr::New(Js::OpCode::JMP, labelHelper, this->m_func);
  4727. insertInstr->InsertBefore(instr);
  4728. }
  4729. }
  4730. // Bail out if index a constant and is less than zero.
  4731. if (srcIndex->IsAddrOpnd() && Js::TaggedInt::ToInt32(srcIndex->AsAddrOpnd()->m_address) < 0)
  4732. {
  4733. labelHelper->isOpHelper = false;
  4734. instr = IR::BranchInstr::New(Js::OpCode::JMP, labelHelper, this->m_func);
  4735. insertInstr->InsertBefore(instr);
  4736. return false;
  4737. }
  4738. this->m_lowerer->GenerateStringTest(regSrcStr, insertInstr, labelHelper, nullptr, false);
  4739. // r1 contains the value of the wchar_t* pointer inside JavascriptString.
  4740. // MOV r1, [regSrcStr + offset(m_pszValue)]
  4741. IR::RegOpnd *r1 = IR::RegOpnd::New(TyMachReg, this->m_func);
  4742. IR::IndirOpnd * indirOpnd = IR::IndirOpnd::New(regSrcStr->AsRegOpnd(), Js::JavascriptString::GetOffsetOfpszValue(), TyMachPtr, this->m_func);
  4743. instr = IR::Instr::New(Js::OpCode::MOV, r1, indirOpnd, this->m_func);
  4744. insertInstr->InsertBefore(instr);
  4745. // TEST r1, r1 -- Null pointer test
  4746. instr = IR::Instr::New(Js::OpCode::TEST, this->m_func);
  4747. instr->SetSrc1(r1);
  4748. instr->SetSrc2(r1);
  4749. insertInstr->InsertBefore(instr);
  4750. // JEQ $helper
  4751. instr = IR::BranchInstr::New(Js::OpCode::JEQ, labelHelper, this->m_func);
  4752. insertInstr->InsertBefore(instr);
  4753. IR::IndirOpnd *strLength = IR::IndirOpnd::New(regSrcStr, offsetof(Js::JavascriptString, m_charLength), TyUint32, this->m_func);
  4754. if (srcIndex->IsAddrOpnd())
  4755. {
  4756. // CMP [regSrcStr + offsetof(length)], index
  4757. instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  4758. instr->SetSrc1(strLength);
  4759. instr->SetSrc2(IR::IntConstOpnd::New(Js::TaggedInt::ToUInt32(srcIndex->AsAddrOpnd()->m_address), TyUint32, this->m_func));
  4760. insertInstr->InsertBefore(instr);
  4761. // Use unsigned compare, this should handle negative indexes as well (they become > INT_MAX)
  4762. // JBE $helper
  4763. instr = IR::BranchInstr::New(Js::OpCode::JBE, labelHelper, this->m_func);
  4764. insertInstr->InsertBefore(instr);
  4765. indirOpnd = IR::IndirOpnd::New(r1, Js::TaggedInt::ToUInt32(srcIndex->AsAddrOpnd()->m_address) * sizeof(wchar_t), TyInt16, this->m_func);
  4766. }
  4767. else
  4768. {
  4769. IR::RegOpnd *r2 = IR::RegOpnd::New(TyVar, this->m_func);
  4770. // MOV r2, srcIndex
  4771. instr = IR::Instr::New(Js::OpCode::MOV, r2, srcIndex, this->m_func);
  4772. insertInstr->InsertBefore(instr);
  4773. if (!srcIndex->IsRegOpnd() || !srcIndex->AsRegOpnd()->IsTaggedInt())
  4774. {
  4775. GenerateSmIntTest(r2, insertInstr, labelHelper);
  4776. }
  4777. #if INT32VAR
  4778. // Remove the tag
  4779. // MOV r2, [32-bit] r2
  4780. IR::Opnd * r2_32 = r2->UseWithNewType(TyInt32, this->m_func);
  4781. instr = IR::Instr::New(Js::OpCode::MOVSXD, r2, r2_32, this->m_func);
  4782. insertInstr->InsertBefore(instr);
  4783. r2 = r2_32->AsRegOpnd();
  4784. #else
  4785. // r2 = SAR r2, VarTag_Shift
  4786. instr = IR::Instr::New(Js::OpCode::SAR, r2, r2, IR::IntConstOpnd::New(Js::VarTag_Shift, TyInt8, this->m_func), this->m_func);
  4787. insertInstr->InsertBefore(instr);
  4788. #endif
  4789. // CMP [regSrcStr + offsetof(length)], r2
  4790. instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  4791. instr->SetSrc1(strLength);
  4792. instr->SetSrc2(r2);
  4793. insertInstr->InsertBefore(instr);
  4794. if (r2->GetSize() != MachPtr)
  4795. {
  4796. r2 = r2->UseWithNewType(TyMachPtr, this->m_func)->AsRegOpnd();
  4797. }
  4798. // Use unsigned compare, this should handle negative indexes as well (they become > INT_MAX)
  4799. // JBE $helper
  4800. instr = IR::BranchInstr::New(Js::OpCode::JBE, labelHelper, this->m_func);
  4801. insertInstr->InsertBefore(instr);
  4802. indirOpnd = IR::IndirOpnd::New(r1, r2, 1, TyInt16, this->m_func);
  4803. }
  4804. // MOVZX charReg, [r1 + r2 * 2] -- this is the value of the char
  4805. IR::RegOpnd *charReg = IR::RegOpnd::New(TyMachReg, this->m_func);
  4806. instr = IR::Instr::New(Js::OpCode::MOVZXW, charReg, indirOpnd, this->m_func);
  4807. insertInstr->InsertBefore(instr);
  4808. if (index == Js::BuiltinFunction::String_CharAt)
  4809. {
  4810. this->m_lowerer->GenerateGetSingleCharString(charReg, dst, labelHelper, doneLabel, insertInstr, false);
  4811. }
  4812. else
  4813. {
  4814. Assert(index == Js::BuiltinFunction::String_CharCodeAt || index == Js::BuiltinFunction::String_CodePointAt);
  4815. if (index == Js::BuiltinFunction::String_CodePointAt)
  4816. {
  4817. this->m_lowerer->GenerateFastInlineStringCodePointAt(insertInstr, this->m_func, strLength, srcIndex, charReg, r1);
  4818. }
  4819. GenerateInt32ToVarConversion(charReg, insertInstr);
  4820. // MOV dst, charReg
  4821. instr = IR::Instr::New(Js::OpCode::MOV, dst, charReg, this->m_func);
  4822. insertInstr->InsertBefore(instr);
  4823. }
  4824. return true;
  4825. }
  4826. void
  4827. LowererMD::GenerateClz(IR::Instr * instr)
  4828. {
  4829. Assert(instr->GetSrc1()->IsInt32() || instr->GetSrc1()->IsUInt32());
  4830. Assert(IRType_IsNativeInt(instr->GetDst()->GetType()));
  4831. if (AutoSystemInfo::Data.LZCntAvailable())
  4832. {
  4833. instr->m_opcode = Js::OpCode::LZCNT;
  4834. Legalize(instr);
  4835. }
  4836. else
  4837. {
  4838. // tmp = BSR src
  4839. // JE $label32
  4840. // dst = SUB 31, tmp
  4841. // JMP $done
  4842. // label32:
  4843. // dst = mov 32;
  4844. // $done
  4845. IR::LabelInstr * doneLabel = Lowerer::InsertLabel(false, instr->m_next);
  4846. IR::Opnd * dst = instr->UnlinkDst();
  4847. IR::Opnd * tmpOpnd = IR::RegOpnd::New(TyInt8, m_func);
  4848. instr->SetDst(tmpOpnd);
  4849. instr->m_opcode = Js::OpCode::BSR;
  4850. Legalize(instr);
  4851. IR::LabelInstr * label32 = Lowerer::InsertLabel(false, doneLabel);
  4852. instr = IR::BranchInstr::New(Js::OpCode::JEQ, label32, m_func);
  4853. label32->InsertBefore(instr);
  4854. Lowerer::InsertSub(false, dst, IR::IntConstOpnd::New(31, TyInt8, m_func), tmpOpnd, label32);
  4855. Lowerer::InsertBranch(Js::OpCode::Br, doneLabel, label32);
  4856. Lowerer::InsertMove(dst, IR::IntConstOpnd::New(32, TyInt8, m_func), doneLabel);
  4857. }
  4858. }
  4859. #if !FLOATVAR
  4860. void
  4861. LowererMD::GenerateNumberAllocation(IR::RegOpnd * opndDst, IR::Instr * instrInsert, bool isHelper)
  4862. {
  4863. Js::RecyclerJavascriptNumberAllocator * allocator = this->m_lowerer->GetScriptContext()->GetNumberAllocator();
  4864. IR::Opnd * endAddressOpnd = m_lowerer->LoadNumberAllocatorValueOpnd(instrInsert, NumberAllocatorValue::NumberAllocatorEndAddress);
  4865. IR::Opnd * freeObjectListOpnd = m_lowerer->LoadNumberAllocatorValueOpnd(instrInsert, NumberAllocatorValue::NumberAllocatorFreeObjectList);
  4866. // MOV dst, allocator->freeObjectList
  4867. IR::Instr * loadMemBlockInstr = IR::Instr::New(Js::OpCode::MOV, opndDst, freeObjectListOpnd, this->m_func);
  4868. instrInsert->InsertBefore(loadMemBlockInstr);
  4869. // LEA nextMemBlock, [dst + allocSize]
  4870. IR::RegOpnd * nextMemBlockOpnd = IR::RegOpnd::New(TyMachPtr, this->m_func);
  4871. IR::Instr * loadNextMemBlockInstr = IR::Instr::New(Js::OpCode::LEA, nextMemBlockOpnd,
  4872. IR::IndirOpnd::New(opndDst, allocator->GetAlignedAllocSize(), TyMachPtr, this->m_func), this->m_func);
  4873. instrInsert->InsertBefore(loadNextMemBlockInstr);
  4874. // CMP nextMemBlock, allocator->endAddress
  4875. IR::Instr * checkInstr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  4876. checkInstr->SetSrc1(nextMemBlockOpnd);
  4877. checkInstr->SetSrc2(endAddressOpnd);
  4878. instrInsert->InsertBefore(checkInstr);
  4879. // JA $helper
  4880. IR::LabelInstr * helperLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  4881. IR::BranchInstr * branchInstr = IR::BranchInstr::New(Js::OpCode::JA, helperLabel, this->m_func);
  4882. instrInsert->InsertBefore(branchInstr);
  4883. // MOV allocator->freeObjectList, nextMemBlock
  4884. IR::Instr * setFreeObjectListInstr = IR::Instr::New(Js::OpCode::MOV, freeObjectListOpnd, nextMemBlockOpnd, this->m_func);
  4885. instrInsert->InsertBefore(setFreeObjectListInstr);
  4886. // JMP $done
  4887. IR::LabelInstr * doneLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, isHelper);
  4888. IR::BranchInstr * branchToDoneInstr = IR::BranchInstr::New(Js::OpCode::JMP, doneLabel, this->m_func);
  4889. instrInsert->InsertBefore(branchToDoneInstr);
  4890. // $helper:
  4891. instrInsert->InsertBefore(helperLabel);
  4892. // PUSH allocator
  4893. this->LoadHelperArgument(instrInsert, m_lowerer->LoadScriptContextValueOpnd(instrInsert, ScriptContextValue::ScriptContextNumberAllocator));
  4894. // dst = Call AllocUninitializedNumber
  4895. IR::Instr * instrCall = IR::Instr::New(Js::OpCode::CALL, opndDst,
  4896. IR::HelperCallOpnd::New(IR::HelperAllocUninitializedNumber, this->m_func), this->m_func);
  4897. instrInsert->InsertBefore(instrCall);
  4898. this->lowererMDArch.LowerCall(instrCall, 0);
  4899. // $done:
  4900. instrInsert->InsertBefore(doneLabel);
  4901. }
  4902. #endif
  4903. #ifdef _CONTROL_FLOW_GUARD
  4904. void
  4905. LowererMD::GenerateCFGCheck(IR::Opnd * entryPointOpnd, IR::Instr * insertBeforeInstr)
  4906. {
  4907. //PreReserve segment at this point, as we will definitely using this segment for JITted code(in almost all cases)
  4908. //This is for CFG check optimization
  4909. IR::LabelInstr * callLabelInstr = nullptr;
  4910. char * preReservedRegionStartAddress = nullptr;
  4911. if (m_func->CanAllocInPreReservedHeapPageSegment())
  4912. {
  4913. PreReservedVirtualAllocWrapper * preReservedVirtualAllocator = m_func->GetScriptContext()->GetThreadContext()->GetPreReservedVirtualAllocator();
  4914. preReservedRegionStartAddress = m_func->GetEmitBufferManager()->EnsurePreReservedPageAllocation(preReservedVirtualAllocator);
  4915. if (preReservedRegionStartAddress != nullptr)
  4916. {
  4917. Assert(preReservedVirtualAllocator);
  4918. char* endAddressOfSegment = (char*)preReservedVirtualAllocator->GetPreReservedEndAddress();
  4919. int32 segmentSize = (int32) (endAddressOfSegment - preReservedRegionStartAddress);
  4920. // Generate instructions for local Pre-Reserved Segment Range check
  4921. IR::AddrOpnd * endAddressOfSegmentConstOpnd = IR::AddrOpnd::New(endAddressOfSegment, IR::AddrOpndKindDynamicMisc, m_func);
  4922. IR::RegOpnd *resultOpnd = IR::RegOpnd::New(TyMachReg, this->m_func);
  4923. #if _M_IX86
  4924. //resultOpnd = endAddressOfSegmentConstOpnd - entryPointOpnd
  4925. IR::Instr* subInstr = IR::Instr::New(Js::OpCode::Sub_I4, resultOpnd, endAddressOfSegmentConstOpnd, entryPointOpnd, m_func);
  4926. insertBeforeInstr->InsertBefore(subInstr);
  4927. this->EmitInt4Instr(subInstr);
  4928. #elif _M_X64
  4929. //MOV resultOpnd, endAddressOfSegment
  4930. //resultOpnd = resultOpnd - entryPointOpnd
  4931. IR::Instr *movInstr = IR::Instr::New(Js::OpCode::MOV, resultOpnd, endAddressOfSegmentConstOpnd, this->m_func);
  4932. insertBeforeInstr->InsertBefore(movInstr);
  4933. IR::Instr* subInstr = IR::Instr::New(Js::OpCode::SUB, resultOpnd, resultOpnd, entryPointOpnd, m_func);
  4934. insertBeforeInstr->InsertBefore(subInstr);
  4935. #endif
  4936. //CMP subResultOpnd, segmentSize
  4937. //JL $callLabelInstr:
  4938. AssertMsg((size_t) segmentSize == (size_t) (endAddressOfSegment - preReservedRegionStartAddress), "Need a bigger datatype for segmentSize?");
  4939. IR::IntConstOpnd * segmentSizeOpnd = IR::IntConstOpnd::New(segmentSize, IRType::TyInt32, m_func, true);
  4940. callLabelInstr = IR::LabelInstr::New(Js::OpCode::Label, m_func);
  4941. this->m_lowerer->InsertCompareBranch(resultOpnd, segmentSizeOpnd, Js::OpCode::JBE, callLabelInstr, insertBeforeInstr);
  4942. }
  4943. }
  4944. //MOV ecx, entryPoint
  4945. IR::RegOpnd * entryPointRegOpnd = IR::RegOpnd::New(TyMachReg, this->m_func);
  4946. #if _M_IX86
  4947. entryPointRegOpnd->SetReg(RegECX);
  4948. #elif _M_X64
  4949. entryPointRegOpnd->SetReg(RegRCX);
  4950. #endif
  4951. entryPointRegOpnd->m_isCallArg = true;
  4952. IR::Instr* movInstrEntryPointToRegister = IR::Instr::New(Js::OpCode::MOV, entryPointRegOpnd, entryPointOpnd, this->m_func);
  4953. insertBeforeInstr->InsertBefore(movInstrEntryPointToRegister);
  4954. //Generate CheckCFG CALL here
  4955. IR::HelperCallOpnd *cfgCallOpnd = IR::HelperCallOpnd::New(IR::HelperGuardCheckCall, this->m_func);
  4956. IR::Instr* cfgCallInstr = IR::Instr::New(Js::OpCode::CALL, this->m_func);
  4957. #if _M_IX86
  4958. //call[__guard_check_icall_fptr]
  4959. cfgCallInstr->SetSrc1(cfgCallOpnd);
  4960. #elif _M_X64
  4961. //mov rax, __guard_check_icall_fptr
  4962. IR::RegOpnd *targetOpnd = IR::RegOpnd::New(StackSym::New(TyMachPtr, m_func), RegRAX, TyMachPtr, this->m_func);
  4963. IR::Instr *movInstr = IR::Instr::New(Js::OpCode::MOV, targetOpnd, cfgCallOpnd, this->m_func);
  4964. insertBeforeInstr->InsertBefore(movInstr);
  4965. //call rax
  4966. cfgCallInstr->SetSrc1(targetOpnd);
  4967. #endif
  4968. //CALL cfg(rax)
  4969. insertBeforeInstr->InsertBefore(cfgCallInstr);
  4970. if (preReservedRegionStartAddress != nullptr)
  4971. {
  4972. Assert(callLabelInstr);
  4973. #if DBG
  4974. //Always generate CFG check in DBG build to make sure that the address is still valid
  4975. movInstrEntryPointToRegister->InsertBefore(callLabelInstr);
  4976. #else
  4977. insertBeforeInstr->InsertBefore(callLabelInstr);
  4978. #endif
  4979. }
  4980. }
  4981. #endif
  4982. void
  4983. LowererMD::GenerateFastRecyclerAlloc(size_t allocSize, IR::RegOpnd* newObjDst, IR::Instr* insertionPointInstr, IR::LabelInstr* allocHelperLabel, IR::LabelInstr* allocDoneLabel)
  4984. {
  4985. IR::Opnd * endAddressOpnd;
  4986. IR::Opnd * freeListOpnd;
  4987. Js::ScriptContext* scriptContext = this->m_func->GetScriptContext();
  4988. Recycler* recycler = scriptContext->GetRecycler();
  4989. void* allocatorAddress;
  4990. uint32 endAddressOffset;
  4991. uint32 freeListOffset;
  4992. size_t alignedSize = HeapInfo::GetAlignedSizeNoCheck(allocSize);
  4993. recycler->GetNormalHeapBlockAllocatorInfoForNativeAllocation(alignedSize, allocatorAddress, endAddressOffset, freeListOffset);
  4994. endAddressOpnd = IR::MemRefOpnd::New((char*)allocatorAddress + endAddressOffset, TyMachPtr, this->m_func, IR::AddrOpndKindDynamicRecyclerAllocatorEndAddressRef);
  4995. freeListOpnd = IR::MemRefOpnd::New((char*)allocatorAddress + freeListOffset, TyMachPtr, this->m_func, IR::AddrOpndKindDynamicRecyclerAllocatorFreeListRef);
  4996. const IR::AutoReuseOpnd autoReuseTempOpnd(freeListOpnd, m_func);
  4997. // MOV newObjDst, allocator->freeObjectList
  4998. Lowerer::InsertMove(newObjDst, freeListOpnd, insertionPointInstr);
  4999. // LEA nextMemBlock, [newObjDst + allocSize]
  5000. IR::RegOpnd * nextMemBlockOpnd = IR::RegOpnd::New(TyMachPtr, this->m_func);
  5001. IR::IndirOpnd* nextMemBlockSrc = IR::IndirOpnd::New(newObjDst, (int32)alignedSize, TyMachPtr, this->m_func);
  5002. IR::Instr * loadNextMemBlockInstr = IR::Instr::New(Js::OpCode::LEA, nextMemBlockOpnd, nextMemBlockSrc, this->m_func);
  5003. insertionPointInstr->InsertBefore(loadNextMemBlockInstr);
  5004. // CMP nextMemBlock, allocator->endAddress
  5005. IR::Instr * checkInstr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  5006. checkInstr->SetSrc1(nextMemBlockOpnd);
  5007. checkInstr->SetSrc2(endAddressOpnd);
  5008. insertionPointInstr->InsertBefore(checkInstr);
  5009. Legalize(checkInstr);
  5010. // JA $allocHelper
  5011. IR::BranchInstr * branchToAllocHelperInstr = IR::BranchInstr::New(Js::OpCode::JA, allocHelperLabel, this->m_func);
  5012. insertionPointInstr->InsertBefore(branchToAllocHelperInstr);
  5013. // MOV allocator->freeObjectList, nextMemBlock
  5014. Lowerer::InsertMove(freeListOpnd, nextMemBlockOpnd, insertionPointInstr);
  5015. // JMP $allocDone
  5016. IR::BranchInstr * branchToAllocDoneInstr = IR::BranchInstr::New(Js::OpCode::JMP, allocDoneLabel, this->m_func);
  5017. insertionPointInstr->InsertBefore(branchToAllocDoneInstr);
  5018. }
  5019. void
  5020. LowererMD::SaveDoubleToVar(IR::RegOpnd * dstOpnd, IR::RegOpnd *opndFloat, IR::Instr *instrOrig, IR::Instr *instrInsert, bool isHelper)
  5021. {
  5022. Assert(opndFloat->GetType() == TyFloat64);
  5023. // Call JSNumber::ToVar to save the float operand to the result of the original (var) instruction
  5024. #if !FLOATVAR
  5025. // We should only generate this if sse2 is available
  5026. Assert(AutoSystemInfo::Data.SSE2Available());
  5027. IR::Opnd * symVTableDst;
  5028. IR::Opnd * symDblDst;
  5029. IR::Opnd * symTypeDst;
  5030. IR::Instr * newInstr;
  5031. IR::Instr * numberInitInsertInstr = nullptr;
  5032. if (instrOrig->dstIsTempNumber)
  5033. {
  5034. // Use the original dst to get the temp number sym
  5035. StackSym * tempNumberSym = this->m_lowerer->GetTempNumberSym(instrOrig->GetDst(), instrOrig->dstIsTempNumberTransferred);
  5036. // LEA dst, &tempSym
  5037. IR::SymOpnd * symTempSrc = IR::SymOpnd::New(tempNumberSym, TyMachPtr, this->m_func);
  5038. IR::Instr * loadTempNumberInstr = IR::Instr::New(Js::OpCode::LEA, dstOpnd, symTempSrc, this->m_func);
  5039. instrInsert->InsertBefore(loadTempNumberInstr);
  5040. symVTableDst = IR::SymOpnd::New(tempNumberSym, TyMachPtr, this->m_func);
  5041. symDblDst = IR::SymOpnd::New(tempNumberSym, (uint32)Js::JavascriptNumber::GetValueOffset(), TyFloat64, this->m_func);
  5042. symTypeDst = IR::SymOpnd::New(tempNumberSym, (uint32)Js::JavascriptNumber::GetOffsetOfType(), TyMachPtr, this->m_func);
  5043. if (this->m_lowerer->outerMostLoopLabel == nullptr)
  5044. {
  5045. // If we are not in loop, just insert in place
  5046. numberInitInsertInstr = instrInsert;
  5047. }
  5048. else
  5049. {
  5050. // Otherwise, initialize in the outer most loop top if we haven't initialized it yet.
  5051. numberInitInsertInstr = this->m_lowerer->initializedTempSym->TestAndSet(tempNumberSym->m_id) ?
  5052. nullptr : this->m_lowerer->outerMostLoopLabel;
  5053. }
  5054. }
  5055. else
  5056. {
  5057. this->GenerateNumberAllocation(dstOpnd, instrInsert, isHelper);
  5058. symVTableDst = IR::IndirOpnd::New(dstOpnd, 0, TyMachPtr, this->m_func);
  5059. symDblDst = IR::IndirOpnd::New(dstOpnd, (uint32)Js::JavascriptNumber::GetValueOffset(), TyFloat64, this->m_func);
  5060. symTypeDst = IR::IndirOpnd::New(dstOpnd, (uint32)Js::JavascriptNumber::GetOffsetOfType(), TyMachPtr, this->m_func);
  5061. numberInitInsertInstr = instrInsert;
  5062. }
  5063. if (numberInitInsertInstr)
  5064. {
  5065. // Inline the case where the dst is marked as temp.
  5066. IR::Opnd *jsNumberVTable = m_lowerer->LoadVTableValueOpnd(numberInitInsertInstr, VTableValue::VtableJavascriptNumber);
  5067. // MOV dst->vtable, JavascriptNumber::vtable
  5068. newInstr = IR::Instr::New(Js::OpCode::MOV, symVTableDst, jsNumberVTable, this->m_func);
  5069. numberInitInsertInstr->InsertBefore(newInstr);
  5070. // MOV dst->type, JavascriptNumber_type
  5071. IR::Opnd *typeOpnd = m_lowerer->LoadLibraryValueOpnd(numberInitInsertInstr, LibraryValue::ValueNumberTypeStatic);
  5072. newInstr = IR::Instr::New(Js::OpCode::MOV, symTypeDst, typeOpnd, this->m_func);
  5073. numberInitInsertInstr->InsertBefore(newInstr);
  5074. }
  5075. // MOVSD dst->value, opndFloat ; copy the float result to the temp JavascriptNumber
  5076. newInstr = IR::Instr::New(Js::OpCode::MOVSD, symDblDst, opndFloat, this->m_func);
  5077. instrInsert->InsertBefore(newInstr);
  5078. #else
  5079. // s1 = MOVD opndFloat
  5080. IR::RegOpnd *s1 = IR::RegOpnd::New(TyMachReg, m_func);
  5081. IR::Instr *movd = IR::Instr::New(Js::OpCode::MOVD, s1, opndFloat, m_func);
  5082. instrInsert->InsertBefore(movd);
  5083. if (m_func->GetJnFunction()->GetIsAsmjsMode())
  5084. {
  5085. // s1 = MOVD src
  5086. // tmp = NOT s1
  5087. // tmp = AND tmp, 0x7FF0000000000000ull
  5088. // test tmp, tmp
  5089. // je helper
  5090. // jmp done
  5091. // helper:
  5092. // tmp2 = AND s1, 0x000FFFFFFFFFFFFFull
  5093. // test tmp2, tmp2
  5094. // je done
  5095. // s1 = JavascriptNumber::k_Nan
  5096. // done:
  5097. IR::RegOpnd *tmp = IR::RegOpnd::New(TyMachReg, m_func);
  5098. IR::Instr * newInstr = IR::Instr::New(Js::OpCode::NOT, tmp, s1, m_func);
  5099. instrInsert->InsertBefore(newInstr);
  5100. LowererMD::MakeDstEquSrc1(newInstr);
  5101. newInstr = IR::Instr::New(Js::OpCode::AND, tmp, tmp, IR::AddrOpnd::New((Js::Var)0x7FF0000000000000, IR::AddrOpndKindConstantVar, m_func, true), m_func);
  5102. instrInsert->InsertBefore(newInstr);
  5103. LowererMD::Legalize(newInstr);
  5104. IR::LabelInstr* helper = Lowerer::InsertLabel(true, instrInsert);
  5105. Lowerer::InsertTestBranch(tmp, tmp, Js::OpCode::BrEq_A, helper, helper);
  5106. IR::LabelInstr* done = Lowerer::InsertLabel(isHelper, instrInsert);
  5107. Lowerer::InsertBranch(Js::OpCode::Br, done, helper);
  5108. IR::RegOpnd *tmp2 = IR::RegOpnd::New(TyMachReg, m_func);
  5109. newInstr = IR::Instr::New(Js::OpCode::AND, tmp2, s1, IR::AddrOpnd::New((Js::Var)0x000FFFFFFFFFFFFFull, IR::AddrOpndKindConstantVar, m_func, true), m_func);
  5110. done->InsertBefore(newInstr);
  5111. LowererMD::Legalize(newInstr);
  5112. Lowerer::InsertTestBranch(tmp2, tmp2, Js::OpCode::BrEq_A, done, done);
  5113. IR::Opnd * opndNaN = IR::AddrOpnd::New((Js::Var)Js::JavascriptNumber::k_Nan, IR::AddrOpndKindConstantVar, m_func, true);
  5114. Lowerer::InsertMove(s1, opndNaN, done);
  5115. }
  5116. // s1 = XOR s1, FloatTag_Value
  5117. // dst = s1
  5118. IR::Instr *setTag = IR::Instr::New(Js::OpCode::XOR,
  5119. s1,
  5120. s1,
  5121. IR::AddrOpnd::New((Js::Var)Js::FloatTag_Value,
  5122. IR::AddrOpndKindConstantVar,
  5123. this->m_func,
  5124. /* dontEncode = */ true),
  5125. this->m_func);
  5126. IR::Instr *movDst = IR::Instr::New(Js::OpCode::MOV, dstOpnd, s1, this->m_func);
  5127. instrInsert->InsertBefore(setTag);
  5128. instrInsert->InsertBefore(movDst);
  5129. LowererMD::Legalize(setTag);
  5130. #endif
  5131. }
  5132. void
  5133. LowererMD::EmitLoadFloatFromNumber(IR::Opnd *dst, IR::Opnd *src, IR::Instr *insertInstr)
  5134. {
  5135. IR::LabelInstr *labelDone;
  5136. IR::Instr *instr;
  5137. labelDone = EmitLoadFloatCommon(dst, src, insertInstr, insertInstr->HasBailOutInfo());
  5138. if (labelDone == nullptr)
  5139. {
  5140. // We're done
  5141. insertInstr->Remove();
  5142. return;
  5143. }
  5144. // $Done note: insertAfter
  5145. insertInstr->InsertAfter(labelDone);
  5146. if (!insertInstr->HasBailOutInfo())
  5147. {
  5148. // $Done
  5149. insertInstr->Remove();
  5150. return;
  5151. }
  5152. Assert(!m_func->GetJnFunction()->GetIsAsmjsMode());
  5153. IR::LabelInstr *labelNoBailOut = nullptr;
  5154. IR::SymOpnd *tempSymOpnd = nullptr;
  5155. if (insertInstr->GetBailOutKind() == IR::BailOutPrimitiveButString)
  5156. {
  5157. if (!this->m_func->tempSymDouble)
  5158. {
  5159. this->m_func->tempSymDouble = StackSym::New(TyFloat64, this->m_func);
  5160. this->m_func->StackAllocate(this->m_func->tempSymDouble, MachDouble);
  5161. }
  5162. // LEA r3, tempSymDouble
  5163. IR::RegOpnd *reg3Opnd = IR::RegOpnd::New(TyMachReg, this->m_func);
  5164. tempSymOpnd = IR::SymOpnd::New(this->m_func->tempSymDouble, TyFloat64, this->m_func);
  5165. instr = IR::Instr::New(Js::OpCode::LEA, reg3Opnd, tempSymOpnd, this->m_func);
  5166. insertInstr->InsertBefore(instr);
  5167. // regBoolResult = to_number_fromPrimitive(value, &dst, allowUndef, scriptContext);
  5168. this->m_lowerer->LoadScriptContext(insertInstr);
  5169. IR::IntConstOpnd *allowUndefOpnd;
  5170. if (insertInstr->GetBailOutKind() == IR::BailOutPrimitiveButString)
  5171. {
  5172. allowUndefOpnd = IR::IntConstOpnd::New(true, TyInt32, this->m_func);
  5173. }
  5174. else
  5175. {
  5176. Assert(insertInstr->GetBailOutKind() == IR::BailOutNumberOnly);
  5177. allowUndefOpnd = IR::IntConstOpnd::New(false, TyInt32, this->m_func);
  5178. }
  5179. this->LoadHelperArgument(insertInstr, allowUndefOpnd);
  5180. this->LoadHelperArgument(insertInstr, reg3Opnd);
  5181. this->LoadHelperArgument(insertInstr, src);
  5182. IR::RegOpnd *regBoolResult = IR::RegOpnd::New(TyInt32, this->m_func);
  5183. instr = IR::Instr::New(Js::OpCode::CALL, regBoolResult, IR::HelperCallOpnd::New(IR::HelperOp_ConvNumber_FromPrimitive, this->m_func), this->m_func);
  5184. insertInstr->InsertBefore(instr);
  5185. this->lowererMDArch.LowerCall(instr, 0);
  5186. // TEST regBoolResult, regBoolResult
  5187. instr = IR::Instr::New(Js::OpCode::TEST, this->m_func);
  5188. instr->SetSrc1(regBoolResult);
  5189. instr->SetSrc2(regBoolResult);
  5190. insertInstr->InsertBefore(instr);
  5191. // JNE $noBailOut
  5192. labelNoBailOut = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  5193. instr = IR::BranchInstr::New(Js::OpCode::JNE, labelNoBailOut, this->m_func);
  5194. insertInstr->InsertBefore(instr);
  5195. }
  5196. // Bailout code
  5197. Assert(insertInstr->m_opcode == Js::OpCode::FromVar);
  5198. insertInstr->UnlinkDst();
  5199. insertInstr->FreeSrc1();
  5200. IR::Instr *bailoutInstr = insertInstr;
  5201. insertInstr = bailoutInstr->m_next;
  5202. this->m_lowerer->GenerateBailOut(bailoutInstr);
  5203. // $noBailOut
  5204. if (labelNoBailOut)
  5205. {
  5206. insertInstr->InsertBefore(labelNoBailOut);
  5207. Assert(dst->IsRegOpnd());
  5208. // MOVSD dst, [pResult].f64
  5209. instr = IR::Instr::New(Js::OpCode::MOVSD, dst, tempSymOpnd, this->m_func);
  5210. insertInstr->InsertBefore(instr);
  5211. }
  5212. }
  5213. IR::LabelInstr*
  5214. LowererMD::EmitLoadFloatCommon(IR::Opnd *dst, IR::Opnd *src, IR::Instr *insertInstr, bool needHelperLabel)
  5215. {
  5216. IR::Instr *instr;
  5217. Assert(src->GetType() == TyVar);
  5218. Assert(dst->IsFloat());
  5219. bool isFloatConst = false;
  5220. IR::RegOpnd *regFloatOpnd = nullptr;
  5221. if (src->IsRegOpnd() && src->AsRegOpnd()->m_sym->m_isFltConst)
  5222. {
  5223. IR::RegOpnd *regOpnd = src->AsRegOpnd();
  5224. Assert(regOpnd->m_sym->m_isSingleDef);
  5225. Js::Var value = regOpnd->m_sym->GetFloatConstValueAsVar_PostGlobOpt();
  5226. #if FLOATVAR
  5227. double *pDouble = NativeCodeDataNew(this->m_func->GetNativeCodeDataAllocator(), double);
  5228. AnalysisAssert(pDouble);
  5229. *pDouble = Js::JavascriptNumber::GetValue(value);
  5230. IR::MemRefOpnd *memRef = IR::MemRefOpnd::New((BYTE*)pDouble, TyFloat64, this->m_func, IR::AddrOpndKindDynamicDoubleRef);
  5231. #else
  5232. IR::MemRefOpnd *memRef = IR::MemRefOpnd::New((BYTE*)value + Js::JavascriptNumber::GetValueOffset(), TyFloat64, this->m_func,
  5233. IR::AddrOpndKindDynamicDoubleRef);
  5234. #endif
  5235. regFloatOpnd = IR::RegOpnd::New(TyFloat64, this->m_func);
  5236. instr = IR::Instr::New(Js::OpCode::MOVSD, regFloatOpnd, memRef, this->m_func);
  5237. insertInstr->InsertBefore(instr);
  5238. Legalize(instr);
  5239. isFloatConst = true;
  5240. }
  5241. // Src is constant?
  5242. if (src->IsImmediateOpnd() || src->IsFloatConstOpnd())
  5243. {
  5244. regFloatOpnd = IR::RegOpnd::New(TyFloat64, this->m_func);
  5245. m_lowerer->LoadFloatFromNonReg(src, regFloatOpnd, insertInstr);
  5246. isFloatConst = true;
  5247. }
  5248. if (isFloatConst)
  5249. {
  5250. if (dst->GetType() == TyFloat32)
  5251. {
  5252. // CVTSD2SS regOpnd32.f32, regOpnd.f64 -- Convert regOpnd from f64 to f32
  5253. IR::RegOpnd *regOpnd32 = regFloatOpnd->UseWithNewType(TyFloat32, this->m_func)->AsRegOpnd();
  5254. instr = IR::Instr::New(Js::OpCode::CVTSD2SS, regOpnd32, regFloatOpnd, this->m_func);
  5255. insertInstr->InsertBefore(instr);
  5256. // MOVSS dst, regOpnd32
  5257. instr = IR::Instr::New(Js::OpCode::MOVSS, dst, regOpnd32, this->m_func);
  5258. insertInstr->InsertBefore(instr);
  5259. }
  5260. else
  5261. {
  5262. // MOVSD dst, regOpnd
  5263. instr = IR::Instr::New(Js::OpCode::MOVSD, dst, regFloatOpnd, this->m_func);
  5264. insertInstr->InsertBefore(instr);
  5265. }
  5266. return nullptr;
  5267. }
  5268. Assert(src->IsRegOpnd());
  5269. IR::LabelInstr *labelStore = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  5270. IR::LabelInstr *labelHelper;
  5271. IR::LabelInstr *labelDone = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  5272. if (needHelperLabel)
  5273. {
  5274. labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  5275. }
  5276. else
  5277. {
  5278. labelHelper = labelDone;
  5279. }
  5280. bool const isFloat32 = dst->GetType() == TyFloat32;
  5281. IR::RegOpnd *reg2 = ((isFloat32 || !dst->IsRegOpnd()) ? IR::RegOpnd::New(TyMachDouble, this->m_func) : dst->AsRegOpnd());
  5282. // Load the float value in reg2
  5283. this->lowererMDArch.LoadCheckedFloat(src->AsRegOpnd(), reg2, labelStore, labelHelper, insertInstr, needHelperLabel);
  5284. // $Store
  5285. insertInstr->InsertBefore(labelStore);
  5286. if (isFloat32)
  5287. {
  5288. IR::RegOpnd *reg2_32 = reg2->UseWithNewType(TyFloat32, this->m_func)->AsRegOpnd();
  5289. // CVTSD2SS r2_32.f32, r2.f64 -- Convert regOpnd from f64 to f32
  5290. instr = IR::Instr::New(Js::OpCode::CVTSD2SS, reg2_32, reg2, this->m_func);
  5291. insertInstr->InsertBefore(instr);
  5292. // MOVSS dst, r2_32
  5293. instr = IR::Instr::New(Js::OpCode::MOVSS, dst, reg2_32, this->m_func);
  5294. insertInstr->InsertBefore(instr);
  5295. }
  5296. else if (reg2 != dst)
  5297. {
  5298. // MOVSD dst, r2
  5299. instr = IR::Instr::New(Js::OpCode::MOVSD, dst, reg2, this->m_func);
  5300. insertInstr->InsertBefore(instr);
  5301. }
  5302. // JMP $Done
  5303. instr = IR::BranchInstr::New(Js::OpCode::JMP, labelDone, this->m_func);
  5304. insertInstr->InsertBefore(instr);
  5305. if (needHelperLabel)
  5306. {
  5307. // $Helper
  5308. insertInstr->InsertBefore(labelHelper);
  5309. }
  5310. return labelDone;
  5311. }
  5312. IR::RegOpnd *
  5313. LowererMD::EmitLoadFloat(IR::Opnd *dst, IR::Opnd *src, IR::Instr *insertInstr)
  5314. {
  5315. IR::LabelInstr *labelDone;
  5316. IR::Instr *instr;
  5317. labelDone = EmitLoadFloatCommon(dst, src, insertInstr, true);
  5318. if (labelDone == nullptr)
  5319. {
  5320. // We're done
  5321. return nullptr;
  5322. }
  5323. IR::Opnd *memAddress = dst;
  5324. if (dst->IsRegOpnd())
  5325. {
  5326. // Create an f64 stack location to store the result of the helper.
  5327. IR::SymOpnd *symOpnd = IR::SymOpnd::New(StackSym::New(dst->GetType(), this->m_func), dst->GetType(), this->m_func);
  5328. this->m_func->StackAllocate(symOpnd->m_sym->AsStackSym(), sizeof(double));
  5329. memAddress = symOpnd;
  5330. }
  5331. // LEA r3, dst
  5332. IR::RegOpnd *reg3Opnd = IR::RegOpnd::New(TyMachReg, this->m_func);
  5333. instr = IR::Instr::New(Js::OpCode::LEA, reg3Opnd, memAddress, this->m_func);
  5334. insertInstr->InsertBefore(instr);
  5335. // to_number_full(value, &dst, scriptContext);
  5336. // Create dummy binary op to convert into helper
  5337. instr = IR::Instr::New(Js::OpCode::Add_A, this->m_func);
  5338. instr->SetSrc1(src);
  5339. instr->SetSrc2(reg3Opnd);
  5340. insertInstr->InsertBefore(instr);
  5341. IR::JnHelperMethod helper;
  5342. if (dst->GetType() == TyFloat32)
  5343. {
  5344. helper = IR::HelperOp_ConvFloat_Helper;
  5345. }
  5346. else
  5347. {
  5348. helper = IR::HelperOp_ConvNumber_Helper;
  5349. }
  5350. this->m_lowerer->LowerBinaryHelperMem(instr, helper);
  5351. if (dst->IsRegOpnd())
  5352. {
  5353. if (dst->GetType() == TyFloat32)
  5354. {
  5355. // MOVSS dst, r32
  5356. instr = IR::Instr::New(Js::OpCode::MOVSS, dst, memAddress, this->m_func);
  5357. insertInstr->InsertBefore(instr);
  5358. }
  5359. else
  5360. {
  5361. // MOVSD dst, [pResult].f64
  5362. instr = IR::Instr::New(Js::OpCode::MOVSD, dst, memAddress, this->m_func);
  5363. insertInstr->InsertBefore(instr);
  5364. }
  5365. }
  5366. // $Done
  5367. insertInstr->InsertBefore(labelDone);
  5368. return nullptr;
  5369. }
  5370. void
  5371. LowererMD::LowerInt4NegWithBailOut(
  5372. IR::Instr *const instr,
  5373. const IR::BailOutKind bailOutKind,
  5374. IR::LabelInstr *const bailOutLabel,
  5375. IR::LabelInstr *const skipBailOutLabel)
  5376. {
  5377. Assert(instr);
  5378. Assert(instr->m_opcode == Js::OpCode::Neg_I4);
  5379. Assert(!instr->HasBailOutInfo());
  5380. Assert(bailOutKind & IR::BailOutOnResultConditions || bailOutKind == IR::BailOutOnFailedHoistedLoopCountBasedBoundCheck);
  5381. Assert(bailOutLabel);
  5382. Assert(instr->m_next == bailOutLabel);
  5383. Assert(skipBailOutLabel);
  5384. instr->ReplaceDst(instr->GetDst()->UseWithNewType(TyInt32, instr->m_func));
  5385. instr->ReplaceSrc1(instr->GetSrc1()->UseWithNewType(TyInt32, instr->m_func));
  5386. // Lower the instruction
  5387. instr->m_opcode = Js::OpCode::NEG;
  5388. Legalize(instr);
  5389. if(bailOutKind & IR::BailOutOnOverflow || bailOutKind == IR::BailOutOnFailedHoistedLoopCountBasedBoundCheck)
  5390. {
  5391. bailOutLabel->InsertBefore(IR::BranchInstr::New(Js::OpCode::JO, bailOutLabel, instr->m_func));
  5392. }
  5393. if(bailOutKind & IR::BailOutOnNegativeZero)
  5394. {
  5395. bailOutLabel->InsertBefore(IR::BranchInstr::New(Js::OpCode::JEQ, bailOutLabel, instr->m_func));
  5396. }
  5397. // Skip bailout
  5398. bailOutLabel->InsertBefore(IR::BranchInstr::New(LowererMD::MDUncondBranchOpcode, skipBailOutLabel, instr->m_func));
  5399. }
  5400. void
  5401. LowererMD::LowerInt4AddWithBailOut(
  5402. IR::Instr *const instr,
  5403. const IR::BailOutKind bailOutKind,
  5404. IR::LabelInstr *const bailOutLabel,
  5405. IR::LabelInstr *const skipBailOutLabel)
  5406. {
  5407. Assert(instr);
  5408. Assert(instr->m_opcode == Js::OpCode::Add_I4);
  5409. Assert(!instr->HasBailOutInfo());
  5410. Assert(
  5411. (bailOutKind & IR::BailOutOnResultConditions) == IR::BailOutOnOverflow ||
  5412. bailOutKind == IR::BailOutOnFailedHoistedLoopCountBasedBoundCheck);
  5413. Assert(bailOutLabel);
  5414. Assert(instr->m_next == bailOutLabel);
  5415. Assert(skipBailOutLabel);
  5416. instr->ReplaceDst(instr->GetDst()->UseWithNewType(TyInt32, instr->m_func));
  5417. instr->ReplaceSrc1(instr->GetSrc1()->UseWithNewType(TyInt32, instr->m_func));
  5418. instr->ReplaceSrc2(instr->GetSrc2()->UseWithNewType(TyInt32, instr->m_func));
  5419. // Restore sources overwritten by the instruction in the bailout path
  5420. const auto dst = instr->GetDst(), src1 = instr->GetSrc1(), src2 = instr->GetSrc2();
  5421. Assert(dst->IsRegOpnd());
  5422. const bool dstEquSrc1 = dst->IsEqual(src1), dstEquSrc2 = dst->IsEqual(src2);
  5423. if(dstEquSrc1 & dstEquSrc2)
  5424. {
  5425. // We have:
  5426. // s1 += s1
  5427. // Which is equivalent to:
  5428. // s1 <<= 1
  5429. //
  5430. // These overflow a signed 32-bit integer when for the initial s1:
  5431. // s1 > 0 && (s1 & 0x40000000) - result is negative after overflow
  5432. // s1 < 0 && !(s1 & 0x40000000) - result is nonnegative after overflow
  5433. //
  5434. // To restore s1 to its value before the operation, we first do an arithmetic right-shift by one bit to undo the
  5435. // left-shift and preserve the sign of the result after overflow. Since the result after overflow always has the
  5436. // opposite sign from the operands (hence the overflow), we just need to invert the sign of the result. The following
  5437. // restores s1 to its value before the instruction:
  5438. // s1 = (s1 >> 1) ^ 0x80000000
  5439. //
  5440. // Generate:
  5441. // sar s1, 1
  5442. // xor s1, 0x80000000
  5443. const auto startBailOutInstr = bailOutLabel->m_next;
  5444. Assert(startBailOutInstr);
  5445. startBailOutInstr->InsertBefore(
  5446. IR::Instr::New(
  5447. Js::OpCode::SAR,
  5448. dst,
  5449. dst,
  5450. IR::IntConstOpnd::New(1, TyInt8, instr->m_func),
  5451. instr->m_func)
  5452. );
  5453. startBailOutInstr->InsertBefore(
  5454. IR::Instr::New(
  5455. Js::OpCode::XOR,
  5456. dst,
  5457. dst,
  5458. IR::IntConstOpnd::New(INT32_MIN, TyInt32, instr->m_func, true /* dontEncode */),
  5459. instr->m_func)
  5460. );
  5461. }
  5462. else if(dstEquSrc1 | dstEquSrc2)
  5463. {
  5464. // We have:
  5465. // s1 += s2
  5466. // Or:
  5467. // s1 = s2 + s1
  5468. //
  5469. // The following restores s1 to its value before the instruction:
  5470. // s1 -= s2
  5471. //
  5472. // Generate:
  5473. // sub s1, s2
  5474. if(dstEquSrc1)
  5475. {
  5476. Assert(src2->IsRegOpnd() || src2->IsIntConstOpnd());
  5477. }
  5478. else
  5479. {
  5480. Assert(src1->IsRegOpnd() || src1->IsIntConstOpnd());
  5481. }
  5482. bailOutLabel->InsertAfter(IR::Instr::New(Js::OpCode::SUB, dst, dst, dstEquSrc1 ? src2 : src1, instr->m_func));
  5483. }
  5484. // Lower the instruction
  5485. ChangeToAdd(instr, true /* needFlags */);
  5486. Legalize(instr);
  5487. // Skip bailout on no overflow
  5488. bailOutLabel->InsertBefore(IR::BranchInstr::New(Js::OpCode::JNO, skipBailOutLabel, instr->m_func));
  5489. // Fall through to bailOutLabel
  5490. }
  5491. void
  5492. LowererMD::LowerInt4SubWithBailOut(
  5493. IR::Instr *const instr,
  5494. const IR::BailOutKind bailOutKind,
  5495. IR::LabelInstr *const bailOutLabel,
  5496. IR::LabelInstr *const skipBailOutLabel)
  5497. {
  5498. Assert(instr);
  5499. Assert(instr->m_opcode == Js::OpCode::Sub_I4);
  5500. Assert(!instr->HasBailOutInfo());
  5501. Assert(
  5502. (bailOutKind & IR::BailOutOnResultConditions) == IR::BailOutOnOverflow ||
  5503. bailOutKind == IR::BailOutOnFailedHoistedLoopCountBasedBoundCheck);
  5504. Assert(bailOutLabel);
  5505. Assert(instr->m_next == bailOutLabel);
  5506. Assert(skipBailOutLabel);
  5507. instr->ReplaceDst(instr->GetDst()->UseWithNewType(TyInt32, instr->m_func));
  5508. instr->ReplaceSrc1(instr->GetSrc1()->UseWithNewType(TyInt32, instr->m_func));
  5509. instr->ReplaceSrc2(instr->GetSrc2()->UseWithNewType(TyInt32, instr->m_func));
  5510. // Restore sources overwritten by the instruction in the bailout path
  5511. const auto dst = instr->GetDst(), src1 = instr->GetSrc1(), src2 = instr->GetSrc2();
  5512. Assert(dst->IsRegOpnd());
  5513. const bool dstEquSrc1 = dst->IsEqual(src1), dstEquSrc2 = dst->IsEqual(src2);
  5514. if(dstEquSrc1 ^ dstEquSrc2)
  5515. {
  5516. // We have:
  5517. // s1 -= s2
  5518. // Or:
  5519. // s1 = s2 - s1
  5520. //
  5521. // The following restores s1 to its value before the instruction:
  5522. // s1 += s2
  5523. // Or:
  5524. // s1 = s2 - s1
  5525. //
  5526. // Generate:
  5527. // neg s1 - only for second case
  5528. // add s1, s2
  5529. if(dstEquSrc1)
  5530. {
  5531. Assert(src2->IsRegOpnd() || src2->IsIntConstOpnd());
  5532. }
  5533. else
  5534. {
  5535. Assert(src1->IsRegOpnd() || src1->IsIntConstOpnd());
  5536. }
  5537. const auto startBailOutInstr = bailOutLabel->m_next;
  5538. Assert(startBailOutInstr);
  5539. if(dstEquSrc2)
  5540. {
  5541. startBailOutInstr->InsertBefore(IR::Instr::New(Js::OpCode::NEG, dst, dst, instr->m_func));
  5542. }
  5543. startBailOutInstr->InsertBefore(IR::Instr::New(Js::OpCode::ADD, dst, dst, dstEquSrc1 ? src2 : src1, instr->m_func));
  5544. }
  5545. // Lower the instruction
  5546. ChangeToSub(instr, true /* needFlags */);
  5547. Legalize(instr);
  5548. // Skip bailout on no overflow
  5549. bailOutLabel->InsertBefore(IR::BranchInstr::New(Js::OpCode::JNO, skipBailOutLabel, instr->m_func));
  5550. // Fall through to bailOutLabel
  5551. }
  5552. bool
  5553. LowererMD::GenerateSimplifiedInt4Mul(
  5554. IR::Instr *const mulInstr,
  5555. const IR::BailOutKind bailOutKind,
  5556. IR::LabelInstr *const bailOutLabel)
  5557. {
  5558. if (AutoSystemInfo::Data.IsAtomPlatform())
  5559. {
  5560. // On Atom, always optimize unless phase is off
  5561. if (PHASE_OFF(Js::AtomPhase, mulInstr->m_func->GetTopFunc()) ||
  5562. PHASE_OFF(Js::MulStrengthReductionPhase, mulInstr->m_func->GetTopFunc()))
  5563. return false;
  5564. }
  5565. else
  5566. {
  5567. // On other platforms, don't optimize unless phase is forced
  5568. if (!PHASE_FORCE(Js::AtomPhase, mulInstr->m_func->GetTopFunc()) &&
  5569. !PHASE_FORCE(Js::MulStrengthReductionPhase, mulInstr->m_func->GetTopFunc()))
  5570. return false;
  5571. }
  5572. Assert(mulInstr);
  5573. Assert(mulInstr->m_opcode == Js::OpCode::Mul_I4);
  5574. IR::Instr *instr = mulInstr, *nextInstr;
  5575. const auto dst = instr->GetDst(), src1 = instr->GetSrc1(), src2 = instr->GetSrc2();
  5576. if (!src1->IsIntConstOpnd() && !src2->IsIntConstOpnd())
  5577. return false;
  5578. // if two const operands, GlobOpt would have folded the computation
  5579. Assert(!(src1->IsIntConstOpnd() && src2->IsIntConstOpnd()));
  5580. Assert(dst->IsRegOpnd());
  5581. const auto constSrc = src1->IsIntConstOpnd() ? src1 : src2;
  5582. const auto nonConstSrc = src1->IsIntConstOpnd() ? src2 : src1;
  5583. const auto constSrcValue = constSrc->AsIntConstOpnd()->AsInt32();
  5584. auto nonConstSrcCopy = nonConstSrc;
  5585. Assert(nonConstSrc->IsRegOpnd());
  5586. bool doOVF = bailOutKind & IR::BailOutOnMulOverflow || bailOutKind == IR::BailOutOnFailedHoistedLoopCountBasedBoundCheck;
  5587. // don't simplify mul by large numbers with OF check
  5588. if (doOVF && (constSrcValue > 3 || constSrcValue < -3))
  5589. return false;
  5590. switch(constSrcValue)
  5591. {
  5592. case -3:
  5593. case 3:
  5594. // if dst = src, we need to have a copy of the src for the ADD/SUB
  5595. if (dst->IsEqual(nonConstSrc))
  5596. {
  5597. nonConstSrcCopy = IR::RegOpnd::New(nonConstSrc->GetType(), instr->m_func);
  5598. // MOV
  5599. Lowerer::InsertMove(nonConstSrcCopy, nonConstSrc, instr);
  5600. }
  5601. instr->UnlinkSrc1();
  5602. instr->UnlinkSrc2();
  5603. // SHL
  5604. instr->m_opcode = Js::OpCode::SHL;
  5605. instr->SetSrc1(nonConstSrc);
  5606. instr->SetSrc2(IR::IntConstOpnd::New((IntConstType) 1, TyInt32, instr->m_func));
  5607. constSrc->Free(instr->m_func);
  5608. Legalize(instr);
  5609. // JO
  5610. if (doOVF)
  5611. {
  5612. nextInstr = IR::BranchInstr::New(Js::OpCode::JO, bailOutLabel, instr->m_func);
  5613. instr->InsertAfter(nextInstr);
  5614. instr = nextInstr;
  5615. }
  5616. // ADD
  5617. nextInstr = IR::Instr::New(Js::OpCode::ADD, dst, dst, nonConstSrcCopy, instr->m_func);
  5618. instr->InsertAfter(nextInstr);
  5619. instr = nextInstr;
  5620. Legalize(instr);
  5621. if (constSrcValue == -3)
  5622. {
  5623. // JO
  5624. if (doOVF)
  5625. {
  5626. nextInstr = IR::BranchInstr::New(Js::OpCode::JO, bailOutLabel, instr->m_func);
  5627. instr->InsertAfter(nextInstr);
  5628. instr = nextInstr;
  5629. }
  5630. // NEG
  5631. nextInstr = IR::Instr::New(Js::OpCode::NEG, dst, dst, instr->m_func);
  5632. instr->InsertAfter(nextInstr);
  5633. instr = nextInstr;
  5634. Legalize(instr);
  5635. }
  5636. // last JO inserted by caller
  5637. return true;
  5638. case -2:
  5639. case 2:
  5640. instr->UnlinkSrc1();
  5641. instr->UnlinkSrc2();
  5642. // SHL
  5643. instr->m_opcode = Js::OpCode::SHL;
  5644. instr->SetSrc1(nonConstSrc);
  5645. instr->SetSrc2(IR::IntConstOpnd::New((IntConstType) 1, TyInt32, instr->m_func));
  5646. constSrc->Free(instr->m_func);
  5647. Legalize(instr);
  5648. if (constSrcValue == -2)
  5649. {
  5650. // JO
  5651. if (doOVF)
  5652. {
  5653. nextInstr = IR::BranchInstr::New(Js::OpCode::JO, bailOutLabel, instr->m_func);
  5654. instr->InsertAfter(nextInstr);
  5655. instr = nextInstr;
  5656. }
  5657. // NEG
  5658. nextInstr = IR::Instr::New(Js::OpCode::NEG, dst, dst, instr->m_func);
  5659. instr->InsertAfter(nextInstr);
  5660. instr = nextInstr;
  5661. Legalize(instr);
  5662. }
  5663. // last JO inserted by caller
  5664. return true;
  5665. case -1:
  5666. instr->UnlinkSrc1();
  5667. instr->UnlinkSrc2();
  5668. // NEG
  5669. instr->m_opcode = Js::OpCode::NEG;
  5670. instr->SetSrc1(nonConstSrc);
  5671. constSrc->Free(instr->m_func);
  5672. Legalize(instr);
  5673. // JO inserted by caller
  5674. return true;
  5675. case 0:
  5676. instr->FreeSrc1();
  5677. instr->FreeSrc2();
  5678. // MOV
  5679. instr->m_opcode = Js::OpCode::MOV;
  5680. instr->SetSrc1(IR::IntConstOpnd::New((IntConstType) 0, TyInt32, instr->m_func));
  5681. Legalize(instr);
  5682. // JO inserted by caller are removed in later phases
  5683. return true;
  5684. case 1:
  5685. instr->UnlinkSrc1();
  5686. instr->UnlinkSrc2();
  5687. // MOV
  5688. instr->m_opcode = Js::OpCode::MOV;
  5689. instr->SetSrc1(nonConstSrc);
  5690. constSrc->Free(instr->m_func);
  5691. Legalize(instr);
  5692. // JO inserted by caller are removed in later phases
  5693. return true;
  5694. default:
  5695. // large numbers with no OF check
  5696. Assert(!doOVF);
  5697. // 2^i
  5698. // -2^i
  5699. if (Math::IsPow2(constSrcValue) || Math::IsPow2(-constSrcValue))
  5700. {
  5701. uint32 shamt = constSrcValue > 0 ? Math::Log2(constSrcValue) : Math::Log2(-constSrcValue);
  5702. instr->UnlinkSrc1();
  5703. instr->UnlinkSrc2();
  5704. // SHL
  5705. instr->m_opcode = Js::OpCode::SHL;
  5706. instr->SetSrc1(nonConstSrc);
  5707. instr->SetSrc2(IR::IntConstOpnd::New((IntConstType) shamt, TyInt32, instr->m_func));
  5708. constSrc->Free(instr->m_func);
  5709. Legalize(instr);
  5710. if (constSrcValue < 0)
  5711. {
  5712. // NEG
  5713. nextInstr = IR::Instr::New(Js::OpCode::NEG, dst, dst, instr->m_func);
  5714. instr->InsertAfter(nextInstr);
  5715. Legalize(instr);
  5716. }
  5717. return true;
  5718. }
  5719. // 2^i + 1
  5720. // 2^i - 1
  5721. if (Math::IsPow2(constSrcValue - 1) || Math::IsPow2(constSrcValue + 1))
  5722. {
  5723. bool plusOne = Math::IsPow2(constSrcValue - 1);
  5724. uint32 shamt = plusOne ? Math::Log2(constSrcValue - 1) : Math::Log2(constSrcValue + 1);
  5725. if (dst->IsEqual(nonConstSrc))
  5726. {
  5727. nonConstSrcCopy = IR::RegOpnd::New(nonConstSrc->GetType(), instr->m_func);
  5728. // MOV
  5729. Lowerer::InsertMove(nonConstSrcCopy, nonConstSrc, instr);
  5730. }
  5731. instr->UnlinkSrc1();
  5732. instr->UnlinkSrc2();
  5733. // SHL
  5734. instr->m_opcode = Js::OpCode::SHL;
  5735. instr->SetSrc1(nonConstSrc);
  5736. instr->SetSrc2(IR::IntConstOpnd::New((IntConstType) shamt, TyInt32, instr->m_func));
  5737. constSrc->Free(instr->m_func);
  5738. Legalize(instr);
  5739. // ADD/SUB
  5740. nextInstr = IR::Instr::New(plusOne ? Js::OpCode::ADD : Js::OpCode::SUB, dst, dst, nonConstSrcCopy, instr->m_func);
  5741. instr->InsertAfter(nextInstr);
  5742. instr = nextInstr;
  5743. Legalize(instr);
  5744. return true;
  5745. }
  5746. return false;
  5747. }
  5748. }
  5749. void
  5750. LowererMD::LowerInt4MulWithBailOut(
  5751. IR::Instr *const instr,
  5752. const IR::BailOutKind bailOutKind,
  5753. IR::LabelInstr *const bailOutLabel,
  5754. IR::LabelInstr *const skipBailOutLabel)
  5755. {
  5756. Assert(instr);
  5757. Assert(instr->m_opcode == Js::OpCode::Mul_I4);
  5758. Assert(!instr->HasBailOutInfo());
  5759. Assert(bailOutKind & IR::BailOutOnResultConditions || bailOutKind == IR::BailOutOnFailedHoistedLoopCountBasedBoundCheck);
  5760. Assert(bailOutLabel);
  5761. Assert(instr->m_next == bailOutLabel);
  5762. Assert(skipBailOutLabel);
  5763. instr->ReplaceDst(instr->GetDst()->UseWithNewType(TyInt32, instr->m_func));
  5764. instr->ReplaceSrc1(instr->GetSrc1()->UseWithNewType(TyInt32, instr->m_func));
  5765. instr->ReplaceSrc2(instr->GetSrc2()->UseWithNewType(TyInt32, instr->m_func));
  5766. IR::LabelInstr *checkForNegativeZeroLabel = nullptr;
  5767. if(bailOutKind & IR::BailOutOnNegativeZero)
  5768. {
  5769. // We have:
  5770. // s3 = s1 * s2
  5771. //
  5772. // If the result is zero, we need to check and only bail out if it would be -0. The following determines this:
  5773. // 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)
  5774. //
  5775. // 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,
  5776. // this can lead to bailout. Will handle that case if ever we decide to ignore mul overflow.
  5777. //
  5778. // Generate:
  5779. // $checkForNegativeZeroLabel:
  5780. // test s1, s1
  5781. // js $bailOutLabel
  5782. // test s2, s2
  5783. // jns $skipBailOutLabel
  5784. // (fall through to bail out)
  5785. const auto dst = instr->GetDst(), src1 = instr->GetSrc1(), src2 = instr->GetSrc2();
  5786. Assert(dst->IsRegOpnd());
  5787. Assert(!src1->IsEqual(src2)); // cannot result in -0 if both operands are the same; GlobOpt should have figured that out
  5788. checkForNegativeZeroLabel = IR::LabelInstr::New(Js::OpCode::Label, instr->m_func, true);
  5789. bailOutLabel->InsertBefore(checkForNegativeZeroLabel);
  5790. if(src1->IsIntConstOpnd() || src2->IsIntConstOpnd())
  5791. {
  5792. Assert(!(src1->IsIntConstOpnd() && src2->IsIntConstOpnd())); // if this results in -0, GlobOpt should have avoided type specialization
  5793. const auto constSrc = src1->IsIntConstOpnd() ? src1 : src2;
  5794. const auto nonConstSrc = src1->IsIntConstOpnd() ? src2 : src1;
  5795. Assert(nonConstSrc->IsRegOpnd());
  5796. const auto newInstr = IR::Instr::New(Js::OpCode::TEST, instr->m_func);
  5797. newInstr->SetSrc1(nonConstSrc);
  5798. newInstr->SetSrc2(nonConstSrc);
  5799. bailOutLabel->InsertBefore(newInstr);
  5800. const auto constSrcValue = constSrc->AsIntConstOpnd()->GetValue();
  5801. if(constSrcValue == 0)
  5802. {
  5803. bailOutLabel->InsertBefore(IR::BranchInstr::New(Js::OpCode::JNSB, skipBailOutLabel, instr->m_func));
  5804. }
  5805. else
  5806. {
  5807. Assert(constSrcValue < 0); // cannot result in -0 if one operand is positive; GlobOpt should have figured that out
  5808. bailOutLabel->InsertBefore(IR::BranchInstr::New(Js::OpCode::JNE, skipBailOutLabel, instr->m_func));
  5809. }
  5810. }
  5811. else
  5812. {
  5813. auto newInstr = IR::Instr::New(Js::OpCode::TEST, instr->m_func);
  5814. newInstr->SetSrc1(src1);
  5815. newInstr->SetSrc2(src1);
  5816. bailOutLabel->InsertBefore(newInstr);
  5817. bailOutLabel->InsertBefore(IR::BranchInstr::New(Js::OpCode::JSB, bailOutLabel, instr->m_func));
  5818. newInstr = IR::Instr::New(Js::OpCode::TEST, instr->m_func);
  5819. newInstr->SetSrc1(src2);
  5820. newInstr->SetSrc2(src2);
  5821. bailOutLabel->InsertBefore(newInstr);
  5822. bailOutLabel->InsertBefore(IR::BranchInstr::New(Js::OpCode::JNSB, skipBailOutLabel, instr->m_func));
  5823. }
  5824. // Fall through to bailOutLabel
  5825. }
  5826. const bool needsOverflowCheck =
  5827. bailOutKind & IR::BailOutOnMulOverflow || bailOutKind == IR::BailOutOnFailedHoistedLoopCountBasedBoundCheck;
  5828. AssertMsg(!instr->ShouldCheckForNon32BitOverflow() || (needsOverflowCheck && instr->ShouldCheckForNon32BitOverflow()), "Non 32-bit overflow check required without bailout info");
  5829. bool simplifiedMul = LowererMD::GenerateSimplifiedInt4Mul(instr, bailOutKind, bailOutLabel);
  5830. // Lower the instruction
  5831. if (!simplifiedMul)
  5832. {
  5833. LowererMD::ChangeToMul(instr, needsOverflowCheck);
  5834. }
  5835. const auto insertBeforeInstr = checkForNegativeZeroLabel ? checkForNegativeZeroLabel : bailOutLabel;
  5836. if(needsOverflowCheck)
  5837. {
  5838. // do we care about int32 or non-int32 overflow ?
  5839. if (!simplifiedMul && !instr->ShouldCheckFor32BitOverflow() && instr->ShouldCheckForNon32BitOverflow())
  5840. LowererMD::EmitNon32BitOvfCheck(instr, insertBeforeInstr, bailOutLabel);
  5841. else
  5842. insertBeforeInstr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JO, bailOutLabel, instr->m_func));
  5843. }
  5844. if(bailOutKind & IR::BailOutOnNegativeZero)
  5845. {
  5846. // On zero, branch to determine whether the result would be -0
  5847. Assert(checkForNegativeZeroLabel);
  5848. const auto newInstr = IR::Instr::New(Js::OpCode::TEST, instr->m_func);
  5849. const auto dst = instr->GetDst();
  5850. newInstr->SetSrc1(dst);
  5851. newInstr->SetSrc2(dst);
  5852. insertBeforeInstr->InsertBefore(newInstr);
  5853. insertBeforeInstr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JEQ, checkForNegativeZeroLabel, instr->m_func));
  5854. }
  5855. // Skip bailout
  5856. insertBeforeInstr->InsertBefore(IR::BranchInstr::New(LowererMD::MDUncondBranchOpcode, skipBailOutLabel, instr->m_func));
  5857. }
  5858. void
  5859. LowererMD::LowerInt4RemWithBailOut(
  5860. IR::Instr *const instr,
  5861. const IR::BailOutKind bailOutKind,
  5862. IR::LabelInstr *const bailOutLabel,
  5863. IR::LabelInstr *const skipBailOutLabel) const
  5864. {
  5865. Assert(instr);
  5866. Assert(instr->m_opcode == Js::OpCode::Rem_I4);
  5867. Assert(!instr->HasBailOutInfo());
  5868. Assert(bailOutKind & IR::BailOutOnNegativeZero);
  5869. Assert(bailOutLabel);
  5870. Assert(instr->m_next == bailOutLabel);
  5871. Assert(skipBailOutLabel);
  5872. instr->ReplaceDst(instr->GetDst()->UseWithNewType(TyInt32, instr->m_func));
  5873. instr->ReplaceSrc1(instr->GetSrc1()->UseWithNewType(TyInt32, instr->m_func));
  5874. instr->ReplaceSrc2(instr->GetSrc2()->UseWithNewType(TyInt32, instr->m_func));
  5875. bool fastPath = m_lowerer->GenerateSimplifiedInt4Rem(instr, skipBailOutLabel);
  5876. // We have:
  5877. // s3 = s1 % s2
  5878. //
  5879. // If the result is zero, we need to check and only bail out if it would be -0. The following determines this:
  5880. // bailOut = (s3 == 0 && s1 < 0)
  5881. //
  5882. // Generate:
  5883. // $checkForNegativeZeroLabel:
  5884. // test s3, s3
  5885. // jne $skipBailOutLabel
  5886. // test s1, s1
  5887. // jns $skipBailOutLabel
  5888. // (fall through to bail out)
  5889. IR::Opnd *dst = instr->GetDst(), *src1 = instr->GetSrc1();
  5890. Assert(dst->IsRegOpnd());
  5891. IR::Instr * newInstr = IR::Instr::New(Js::OpCode::TEST, instr->m_func);
  5892. newInstr->SetSrc1(dst);
  5893. newInstr->SetSrc2(dst);
  5894. bailOutLabel->InsertBefore(newInstr);
  5895. bailOutLabel->InsertBefore(IR::BranchInstr::New(Js::OpCode::JNE, skipBailOutLabel, instr->m_func));
  5896. // Fast path already checks if s1 >= 0
  5897. if (!fastPath)
  5898. {
  5899. newInstr = IR::Instr::New(Js::OpCode::TEST, instr->m_func);
  5900. newInstr->SetSrc1(src1);
  5901. newInstr->SetSrc2(src1);
  5902. bailOutLabel->InsertBefore(newInstr);
  5903. bailOutLabel->InsertBefore(IR::BranchInstr::New(Js::OpCode::JNSB, skipBailOutLabel, instr->m_func));
  5904. }
  5905. // Fall through to bailOutLabel
  5906. // Lower the instruction
  5907. LowererMDArch::EmitInt4Instr(instr);
  5908. }
  5909. IR::Instr *
  5910. LowererMD::LoadFloatZero(IR::Opnd * opndDst, IR::Instr * instrInsert)
  5911. {
  5912. IR::Instr * instr = IR::Instr::New(Js::OpCode::MOVSD_ZERO, opndDst, instrInsert->m_func);
  5913. instrInsert->InsertBefore(instr);
  5914. return instr;
  5915. }
  5916. IR::Instr *
  5917. LowererMD::LoadFloatValue(IR::Opnd * opndDst, double value, IR::Instr * instrInsert)
  5918. {
  5919. if (value == 0.0 && !Js::JavascriptNumber::IsNegZero(value))
  5920. {
  5921. // zero can be loaded with "XORPS xmm, xmm" rather than needing memory load
  5922. return LoadFloatZero(opndDst, instrInsert);
  5923. }
  5924. IR::Opnd * opnd;
  5925. if (opndDst->IsFloat64())
  5926. {
  5927. double *pValue = NativeCodeDataNew(instrInsert->m_func->GetNativeCodeDataAllocator(), double, value);
  5928. opnd = IR::MemRefOpnd::New((void*)pValue, TyMachDouble, instrInsert->m_func, IR::AddrOpndKindDynamicDoubleRef);
  5929. }
  5930. else
  5931. {
  5932. Assert(opndDst->IsFloat32());
  5933. float * pValue = NativeCodeDataNew(instrInsert->m_func->GetNativeCodeDataAllocator(), float, (float)value);
  5934. opnd = IR::MemRefOpnd::New((void *)pValue, TyFloat32, instrInsert->m_func, IR::AddrOpndKindDynamicFloatRef);
  5935. }
  5936. IR::Instr * instr = IR::Instr::New(LowererMDArch::GetAssignOp(opndDst->GetType()), opndDst, opnd, instrInsert->m_func);
  5937. instrInsert->InsertBefore(instr);
  5938. Legalize(instr);
  5939. return instr;
  5940. }
  5941. IR::Instr *
  5942. LowererMD::EnsureAdjacentArgs(IR::Instr * instrArg)
  5943. {
  5944. // Ensure that the arg instructions for a given call site are adjacent.
  5945. // This isn't normally desirable for CQ, but it's required by, for instance, the cloner,
  5946. // which must clone a complete call sequence.
  5947. IR::Opnd * opnd = instrArg->GetSrc2();
  5948. IR::Instr * instrNextArg;
  5949. StackSym * sym;
  5950. AssertMsg(opnd, "opnd");
  5951. while (opnd->IsSymOpnd())
  5952. {
  5953. sym = opnd->AsSymOpnd()->m_sym->AsStackSym();
  5954. IR::Instr * instrNextArg = sym->m_instrDef;
  5955. Assert(instrNextArg);
  5956. instrNextArg->SinkInstrBefore(instrArg);
  5957. instrArg = instrNextArg;
  5958. opnd = instrArg->GetSrc2();
  5959. }
  5960. sym = opnd->AsRegOpnd()->m_sym;
  5961. instrNextArg = sym->m_instrDef;
  5962. Assert(instrNextArg && instrNextArg->m_opcode == Js::OpCode::StartCall);
  5963. // The StartCall can be trivially moved down.
  5964. if (instrNextArg->m_next != instrArg)
  5965. {
  5966. instrNextArg->UnlinkStartCallFromBailOutInfo(instrArg);
  5967. instrNextArg->Unlink();
  5968. instrArg->InsertBefore(instrNextArg);
  5969. }
  5970. return instrNextArg->m_prev;
  5971. }
  5972. #if INT32VAR
  5973. //
  5974. // Convert an int32 to Var representation.
  5975. //
  5976. void LowererMD::GenerateInt32ToVarConversion( IR::Opnd * opndSrc, IR::Instr * insertInstr )
  5977. {
  5978. AssertMsg(TySize[opndSrc->GetType()] == MachPtr, "For this to work it should be a 64-bit register");
  5979. IR::Instr* instr = IR::Instr::New(Js::OpCode::BTS, opndSrc, opndSrc, IR::IntConstOpnd::New(Js::VarTag_Shift, TyInt8, this->m_func), this->m_func);
  5980. insertInstr->InsertBefore(instr);
  5981. }
  5982. //
  5983. // jump to $labelHelper, based on the result of CMP
  5984. //
  5985. void LowererMD::GenerateSmIntTest(IR::Opnd *opndSrc, IR::Instr *insertInstr, IR::LabelInstr *labelHelper, IR::Instr **instrFirst /* = nullptr */, bool fContinueLabel /*= false*/)
  5986. {
  5987. AssertMsg(opndSrc->GetSize() == MachPtr, "64-bit register required");
  5988. IR::Opnd * opndReg = IR::RegOpnd::New(TyMachReg, this->m_func);
  5989. #ifdef SHIFTLOAD
  5990. // s1 = SHLD src1, 16 - Shift top 16-bits of src1 to s1
  5991. IR::Instr* instr = IR::Instr::New(Js::OpCode::SHLD, opndReg, opndSrc, IR::IntConstOpnd::New(16, TyInt8, this->m_func), this->m_func);
  5992. insertInstr->InsertBefore(instr);
  5993. if (instrFirst)
  5994. {
  5995. *instrFirst = instr;
  5996. }
  5997. // CMP s1.i16, AtomTag.i16
  5998. IR::Opnd *opndReg16 = opndReg->Copy(m_func);
  5999. opndReg16->SetType(TyInt16);
  6000. instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  6001. instr->SetSrc1(opndReg16);
  6002. instr->SetSrc2(IR::IntConstOpnd::New(Js::AtomTag, TyInt16, this->m_func, /* dontEncode = */ true));
  6003. insertInstr->InsertBefore(instr);
  6004. #else
  6005. // s1 = MOV src1 - Move to a temporary
  6006. IR::Instr * instr = IR::Instr::New(Js::OpCode::MOV, opndReg, opndSrc, this->m_func);
  6007. insertInstr->InsertBefore(instr);
  6008. if (instrFirst)
  6009. {
  6010. *instrFirst = instr;
  6011. }
  6012. // s1 = SHR s1, VarTag_Shift
  6013. instr = IR::Instr::New(Js::OpCode::SHR, opndReg, opndReg, IR::IntConstOpnd::New(Js::VarTag_Shift, TyInt8, this->m_func), this->m_func);
  6014. insertInstr->InsertBefore(instr);
  6015. // CMP s1, AtomTag
  6016. instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  6017. instr->SetSrc1(opndReg);
  6018. instr->SetSrc2(IR::IntConstOpnd::New(Js::AtomTag, TyInt32, this->m_func, /* dontEncode = */ true));
  6019. insertInstr->InsertBefore(instr);
  6020. #endif
  6021. if(fContinueLabel)
  6022. {
  6023. // JEQ $labelHelper
  6024. instr = IR::BranchInstr::New(Js::OpCode::JEQ, labelHelper, this->m_func);
  6025. }
  6026. else
  6027. {
  6028. // JNE $labelHelper
  6029. instr = IR::BranchInstr::New(Js::OpCode::JNE, labelHelper, this->m_func);
  6030. }
  6031. insertInstr->InsertBefore(instr);
  6032. }
  6033. //
  6034. // If lower 32-bits are zero (value is zero), jump to $helper.
  6035. //
  6036. void LowererMD::GenerateTaggedZeroTest( IR::Opnd * opndSrc, IR::Instr * insertInstr, IR::LabelInstr * labelHelper )
  6037. {
  6038. // Cast the var to 32 bit integer.
  6039. if(opndSrc->GetSize() != 4)
  6040. {
  6041. opndSrc = opndSrc->UseWithNewType(TyUint32, this->m_func);
  6042. }
  6043. AssertMsg(TySize[opndSrc->GetType()] == 4, "This technique works only on the 32-bit version");
  6044. // TEST src1, src1
  6045. IR::Instr* instr = IR::Instr::New(Js::OpCode::TEST, this->m_func);
  6046. instr->SetSrc1(opndSrc);
  6047. instr->SetSrc2(opndSrc);
  6048. insertInstr->InsertBefore(instr);
  6049. if(labelHelper != nullptr)
  6050. {
  6051. // JZ $labelHelper
  6052. instr = IR::BranchInstr::New(Js::OpCode::JEQ, labelHelper, this->m_func);
  6053. insertInstr->InsertBefore(instr);
  6054. }
  6055. }
  6056. //
  6057. // If top 16 bits are not zero i.e. it is NOT object, jump to $helper.
  6058. //
  6059. bool LowererMD::GenerateObjectTest(IR::Opnd * opndSrc, IR::Instr * insertInstr, IR::LabelInstr * labelTarget, bool fContinueLabel)
  6060. {
  6061. AssertMsg(opndSrc->GetSize() == MachPtr, "64-bit register required");
  6062. if (opndSrc->IsTaggedValue() && fContinueLabel)
  6063. {
  6064. // Insert delete branch opcode to tell the dbChecks not to assert on the helper label we may fall through into
  6065. IR::Instr *fakeBr = IR::PragmaInstr::New(Js::OpCode::DeletedNonHelperBranch, 0, this->m_func);
  6066. insertInstr->InsertBefore(fakeBr);
  6067. return false;
  6068. }
  6069. else if (opndSrc->IsNotTaggedValue() && !fContinueLabel)
  6070. {
  6071. return false;
  6072. }
  6073. IR::Opnd * opndReg = IR::RegOpnd::New(TyMachReg, this->m_func);
  6074. // s1 = MOV src1 - Move to a temporary
  6075. IR::Instr * instr = IR::Instr::New(Js::OpCode::MOV, opndReg, opndSrc, this->m_func);
  6076. insertInstr->InsertBefore(instr);
  6077. // s1 = SHR s1, VarTag_Shift
  6078. instr = IR::Instr::New(Js::OpCode::SHR, opndReg, opndReg, IR::IntConstOpnd::New(Js::VarTag_Shift, TyInt8, this->m_func), this->m_func);
  6079. insertInstr->InsertBefore(instr);
  6080. if (fContinueLabel)
  6081. {
  6082. // JEQ $labelHelper
  6083. instr = IR::BranchInstr::New(Js::OpCode::JEQ, labelTarget, this->m_func);
  6084. insertInstr->InsertBefore(instr);
  6085. IR::LabelInstr *labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  6086. insertInstr->InsertBefore(labelHelper);
  6087. }
  6088. else
  6089. {
  6090. // JNZ $labelHelper
  6091. instr = IR::BranchInstr::New(Js::OpCode::JNE, labelTarget, this->m_func);
  6092. insertInstr->InsertBefore(instr);
  6093. }
  6094. return true;
  6095. }
  6096. #else
  6097. //
  6098. // Convert an int32 value to a Var.
  6099. //
  6100. void LowererMD::GenerateInt32ToVarConversion( IR::Opnd * opndSrc, IR::Instr * insertInstr )
  6101. {
  6102. // SHL r1, AtomTag
  6103. IR::Instr * instr = IR::Instr::New(Js::OpCode::SHL, opndSrc, opndSrc, IR::IntConstOpnd::New(Js::AtomTag, TyInt32, this->m_func), this->m_func);
  6104. insertInstr->InsertBefore(instr);
  6105. // INC r1
  6106. instr = IR::Instr::New(Js::OpCode::INC, opndSrc, opndSrc, this->m_func);
  6107. insertInstr->InsertBefore(instr);
  6108. }
  6109. //
  6110. // jump to $labelHelper, based on the result of TEST
  6111. //
  6112. void LowererMD::GenerateSmIntTest(IR::Opnd *opndSrc, IR::Instr *insertInstr, IR::LabelInstr *labelHelper, IR::Instr **instrFirst /* = nullptr */, bool fContinueLabel /*= false*/)
  6113. {
  6114. if (opndSrc->IsTaggedInt() && !fContinueLabel)
  6115. {
  6116. return;
  6117. }
  6118. else if (opndSrc->IsNotTaggedValue() && fContinueLabel)
  6119. {
  6120. return;
  6121. }
  6122. // TEST src1, AtomTag
  6123. IR::Instr* instr = IR::Instr::New(Js::OpCode::TEST, this->m_func);
  6124. instr->SetSrc1(opndSrc);
  6125. instr->SetSrc2(IR::IntConstOpnd::New(Js::AtomTag, TyInt8, this->m_func));
  6126. insertInstr->InsertBefore(instr);
  6127. if (instrFirst)
  6128. {
  6129. *instrFirst = instr;
  6130. }
  6131. if(fContinueLabel)
  6132. {
  6133. // JNE $labelHelper
  6134. instr = IR::BranchInstr::New(Js::OpCode::JNE, labelHelper, this->m_func);
  6135. }
  6136. else
  6137. {
  6138. // JEQ $labelHelper
  6139. instr = IR::BranchInstr::New(Js::OpCode::JEQ, labelHelper, this->m_func);
  6140. }
  6141. insertInstr->InsertBefore(instr);
  6142. }
  6143. //
  6144. // If value is zero in tagged int representation, jump to $labelHelper.
  6145. //
  6146. void LowererMD::GenerateTaggedZeroTest( IR::Opnd * opndSrc, IR::Instr * insertInstr, IR::LabelInstr * labelHelper )
  6147. {
  6148. if (opndSrc->IsNotTaggedValue())
  6149. {
  6150. return;
  6151. }
  6152. // CMP src1, AtomTag
  6153. IR::Instr* instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  6154. instr->SetSrc1(opndSrc);
  6155. instr->SetSrc2(IR::IntConstOpnd::New(Js::AtomTag, TyInt32, this->m_func));
  6156. insertInstr->InsertBefore(instr);
  6157. // JEQ $helper
  6158. if(labelHelper != nullptr)
  6159. {
  6160. // JEQ $labelHelper
  6161. instr = IR::BranchInstr::New(Js::OpCode::JEQ, labelHelper, this->m_func);
  6162. insertInstr->InsertBefore(instr);
  6163. }
  6164. }
  6165. //
  6166. // If not object, jump to $labelHelper.
  6167. //
  6168. bool LowererMD::GenerateObjectTest(IR::Opnd * opndSrc, IR::Instr * insertInstr, IR::LabelInstr * labelTarget, bool fContinueLabel)
  6169. {
  6170. if (opndSrc->IsTaggedInt() && fContinueLabel)
  6171. {
  6172. // Insert delete branch opcode to tell the dbChecks not to assert on this helper label
  6173. IR::Instr *fakeBr = IR::PragmaInstr::New(Js::OpCode::DeletedNonHelperBranch, 0, this->m_func);
  6174. insertInstr->InsertBefore(fakeBr);
  6175. return false;
  6176. }
  6177. else if (opndSrc->IsNotTaggedValue() && !fContinueLabel)
  6178. {
  6179. return false;
  6180. }
  6181. // TEST src1, AtomTag
  6182. IR::Instr* instr = IR::Instr::New(Js::OpCode::TEST, this->m_func);
  6183. instr->SetSrc1(opndSrc);
  6184. instr->SetSrc2(IR::IntConstOpnd::New(Js::AtomTag, TyInt8, this->m_func));
  6185. insertInstr->InsertBefore(instr);
  6186. if (fContinueLabel)
  6187. {
  6188. // JEQ $labelHelper
  6189. instr = IR::BranchInstr::New(Js::OpCode::JEQ, labelTarget, this->m_func);
  6190. insertInstr->InsertBefore(instr);
  6191. IR::LabelInstr *labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  6192. insertInstr->InsertBefore(labelHelper);
  6193. }
  6194. else
  6195. {
  6196. // JNE $labelHelper
  6197. instr = IR::BranchInstr::New(Js::OpCode::JNE, labelTarget, this->m_func);
  6198. insertInstr->InsertBefore(instr);
  6199. }
  6200. return true;
  6201. }
  6202. #endif
  6203. bool LowererMD::GenerateJSBooleanTest(IR::RegOpnd * regSrc, IR::Instr * insertInstr, IR::LabelInstr * labelTarget, bool fContinueLabel)
  6204. {
  6205. IR::Instr* instr;
  6206. if (regSrc->GetValueType().IsBoolean())
  6207. {
  6208. if (fContinueLabel)
  6209. {
  6210. // JMP $labelTarget
  6211. instr = IR::BranchInstr::New(Js::OpCode::JMP, labelTarget, this->m_func);
  6212. insertInstr->InsertBefore(instr);
  6213. #if DBG
  6214. if (labelTarget->isOpHelper)
  6215. {
  6216. labelTarget->m_noHelperAssert = true;
  6217. }
  6218. #endif
  6219. }
  6220. return false;
  6221. }
  6222. // CMP src1, vtable<JavaScriptBoolean>
  6223. instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  6224. IR::IndirOpnd *vtablePtrOpnd = IR::IndirOpnd::New(regSrc, 0, TyMachPtr, this->m_func);
  6225. instr->SetSrc1(vtablePtrOpnd);
  6226. IR::Opnd *jsBooleanVTable = m_lowerer->LoadVTableValueOpnd(insertInstr, VTableValue::VtableJavascriptBoolean);
  6227. instr->SetSrc2(jsBooleanVTable);
  6228. insertInstr->InsertBefore(instr);
  6229. Legalize(instr);
  6230. if (fContinueLabel)
  6231. {
  6232. // JEQ $labelTarget
  6233. instr = IR::BranchInstr::New(Js::OpCode::JEQ, labelTarget, this->m_func);
  6234. insertInstr->InsertBefore(instr);
  6235. IR::LabelInstr *labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  6236. insertInstr->InsertBefore(labelHelper);
  6237. }
  6238. else
  6239. {
  6240. // JNE $labelTarget
  6241. instr = IR::BranchInstr::New(Js::OpCode::JNE, labelTarget, this->m_func);
  6242. insertInstr->InsertBefore(instr);
  6243. }
  6244. return true;
  6245. }
  6246. #if FLOATVAR
  6247. //
  6248. // If any of the top 14 bits are not set, then the var is not a float value and hence, jump to $labelHelper.
  6249. //
  6250. void LowererMD::GenerateFloatTest(IR::RegOpnd * opndSrc, IR::Instr * insertInstr, IR::LabelInstr* labelHelper, const bool checkForNullInLoopBody)
  6251. {
  6252. if (opndSrc->GetValueType().IsFloat())
  6253. {
  6254. return;
  6255. }
  6256. AssertMsg(opndSrc->GetSize() == MachPtr, "64-bit register required");
  6257. // s1 = MOV src1 - Move to a temporary
  6258. IR::Opnd * opndReg = IR::RegOpnd::New(TyMachReg, this->m_func);
  6259. IR::Instr * instr = IR::Instr::New(Js::OpCode::MOV, opndReg, opndSrc, this->m_func);
  6260. insertInstr->InsertBefore(instr);
  6261. // s1 = SHR s1, 50
  6262. instr = IR::Instr::New(Js::OpCode::SHR, opndReg, opndReg, IR::IntConstOpnd::New(50, TyInt8, this->m_func), this->m_func);
  6263. insertInstr->InsertBefore(instr);
  6264. // JZ $helper
  6265. instr = IR::BranchInstr::New(Js::OpCode::JEQ /* JZ */, labelHelper, this->m_func);
  6266. insertInstr->InsertBefore(instr);
  6267. }
  6268. IR::RegOpnd* LowererMD::CheckFloatAndUntag(IR::RegOpnd * opndSrc, IR::Instr * insertInstr, IR::LabelInstr* labelHelper)
  6269. {
  6270. IR::Opnd* floatTag = IR::AddrOpnd::New((Js::Var)Js::FloatTag_Value, IR::AddrOpndKindConstantVar, this->m_func, /* dontEncode = */ true);
  6271. IR::RegOpnd* regOpndFloatTag = IR::RegOpnd::New(TyUint64, this->m_func);
  6272. // MOV floatTagReg, FloatTag_Value
  6273. IR::Instr* instr = IR::Instr::New(Js::OpCode::MOV, regOpndFloatTag, floatTag, this->m_func);
  6274. insertInstr->InsertBefore(instr);
  6275. if (!opndSrc->GetValueType().IsFloat())
  6276. {
  6277. // TEST s1, floatTagReg
  6278. instr = IR::Instr::New(Js::OpCode::TEST, this->m_func);
  6279. instr->SetSrc1(opndSrc);
  6280. instr->SetSrc2(regOpndFloatTag);
  6281. insertInstr->InsertBefore(instr);
  6282. // JZ $helper
  6283. instr = IR::BranchInstr::New(Js::OpCode::JEQ /* JZ */, labelHelper, this->m_func);
  6284. insertInstr->InsertBefore(instr);
  6285. }
  6286. // untaggedFloat = XOR floatTagReg, s1 // where untaggedFloat == floatTagReg; use floatTagReg temporarily for the untagged float
  6287. IR::RegOpnd* untaggedFloat = regOpndFloatTag;
  6288. instr = IR::Instr::New(Js::OpCode::XOR, untaggedFloat, regOpndFloatTag, opndSrc, this->m_func);
  6289. insertInstr->InsertBefore(instr);
  6290. IR::RegOpnd *floatReg = IR::RegOpnd::New(TyMachDouble, this->m_func);
  6291. instr = IR::Instr::New(Js::OpCode::MOVD, floatReg, untaggedFloat, this->m_func);
  6292. insertInstr->InsertBefore(instr);
  6293. return floatReg;
  6294. }
  6295. #else
  6296. void LowererMD::GenerateFloatTest(IR::RegOpnd * opndSrc, IR::Instr * insertInstr, IR::LabelInstr* labelHelper, const bool checkForNullInLoopBody)
  6297. {
  6298. if (opndSrc->GetValueType().IsFloat())
  6299. {
  6300. return;
  6301. }
  6302. AssertMsg(opndSrc->GetSize() == MachPtr, "64-bit register required");
  6303. if(checkForNullInLoopBody && m_func->IsLoopBody())
  6304. {
  6305. // It's possible that the value was determined dead by the jitted function and was not restored. The jitted loop
  6306. // body may not realize that it's dead and may try to use it. Check for null in loop bodies.
  6307. // test src1, src1
  6308. // jz $helper (bail out)
  6309. m_lowerer->InsertCompareBranch(
  6310. opndSrc,
  6311. IR::AddrOpnd::NewNull(m_func),
  6312. Js::OpCode::BrEq_A,
  6313. labelHelper,
  6314. insertInstr);
  6315. }
  6316. IR::Instr* instr = IR::Instr::New(Js::OpCode::CMP, insertInstr->m_func);
  6317. instr->SetSrc1(IR::IndirOpnd::New(opndSrc, 0, TyMachPtr, insertInstr->m_func));
  6318. instr->SetSrc2(m_lowerer->LoadVTableValueOpnd(insertInstr, VTableValue::VtableJavascriptNumber));
  6319. insertInstr->InsertBefore(instr);
  6320. // JNZ $helper
  6321. instr = IR::BranchInstr::New(Js::OpCode::JNE /* JZ */, labelHelper, this->m_func);
  6322. insertInstr->InsertBefore(instr);
  6323. }
  6324. #endif
  6325. #if DBG
  6326. //
  6327. // Helps in debugging of fast paths.
  6328. //
  6329. void LowererMD::GenerateDebugBreak( IR::Instr * insertInstr )
  6330. {
  6331. // int 3
  6332. IR::Instr *int3 = IR::Instr::New(Js::OpCode::INT, insertInstr->m_func);
  6333. int3->SetSrc1(IR::IntConstOpnd::New(3, TyInt32, insertInstr->m_func));
  6334. insertInstr->InsertBefore(int3);
  6335. }
  6336. #endif
  6337. IR::Instr *
  6338. LowererMD::LoadStackAddress(StackSym *sym, IR::RegOpnd *optionalDstOpnd /* = nullptr */)
  6339. {
  6340. IR::RegOpnd * regDst = optionalDstOpnd != nullptr ? optionalDstOpnd : IR::RegOpnd::New(TyMachReg, this->m_func);
  6341. IR::SymOpnd * symSrc = IR::SymOpnd::New(sym, TyMachPtr, this->m_func);
  6342. IR::Instr * lea = IR::Instr::New(Js::OpCode::LEA, regDst, symSrc, this->m_func);
  6343. return lea;
  6344. }
  6345. template <bool verify>
  6346. void
  6347. LowererMD::MakeDstEquSrc1(IR::Instr *const instr)
  6348. {
  6349. Assert(instr);
  6350. Assert(instr->IsLowered());
  6351. Assert(instr->GetDst());
  6352. Assert(instr->GetSrc1());
  6353. if(instr->GetDst()->IsEqual(instr->GetSrc1()))
  6354. {
  6355. return;
  6356. }
  6357. if (verify)
  6358. {
  6359. AssertMsg(false, "Missing legalization");
  6360. return;
  6361. }
  6362. if(instr->GetSrc2() && instr->GetDst()->IsEqual(instr->GetSrc2()))
  6363. {
  6364. switch(instr->m_opcode)
  6365. {
  6366. case Js::OpCode::Add_I4:
  6367. case Js::OpCode::Mul_I4:
  6368. case Js::OpCode::Or_I4:
  6369. case Js::OpCode::Xor_I4:
  6370. case Js::OpCode::And_I4:
  6371. case Js::OpCode::Add_Ptr:
  6372. case Js::OpCode::ADD:
  6373. case Js::OpCode::IMUL2:
  6374. case Js::OpCode::OR:
  6375. case Js::OpCode::XOR:
  6376. case Js::OpCode::AND:
  6377. case Js::OpCode::ADDSD:
  6378. case Js::OpCode::MULSD:
  6379. case Js::OpCode::ADDSS:
  6380. case Js::OpCode::MULSS:
  6381. case Js::OpCode::ADDPS:
  6382. // For (a = b & a), generate (a = a & b)
  6383. instr->SwapOpnds();
  6384. return;
  6385. }
  6386. // For (a = b - a), generate (c = a; a = b - c) and fall through
  6387. ChangeToAssign(instr->HoistSrc2(Js::OpCode::Ld_A));
  6388. }
  6389. // For (a = b - c), generate (a = b; a = a - c)
  6390. IR::Instr *const mov = IR::Instr::New(Js::OpCode::Ld_A, instr->GetDst(), instr->UnlinkSrc1(), instr->m_func);
  6391. instr->InsertBefore(mov);
  6392. ChangeToAssign(mov);
  6393. instr->SetSrc1(instr->GetDst());
  6394. }
  6395. void
  6396. LowererMD::EmitPtrInstr(IR::Instr *instr)
  6397. {
  6398. LowererMDArch::EmitPtrInstr(instr);
  6399. }
  6400. void
  6401. LowererMD::EmitInt4Instr(IR::Instr *instr)
  6402. {
  6403. LowererMDArch::EmitInt4Instr(instr);
  6404. }
  6405. void
  6406. LowererMD::EmitLoadVar(IR::Instr *instrLoad, bool isFromUint32, bool isHelper)
  6407. {
  6408. lowererMDArch.EmitLoadVar(instrLoad, isFromUint32, isHelper);
  6409. }
  6410. bool
  6411. LowererMD::EmitLoadInt32(IR::Instr *instrLoad, bool conversionFromObjectAllowed)
  6412. {
  6413. return lowererMDArch.EmitLoadInt32(instrLoad, conversionFromObjectAllowed);
  6414. }
  6415. void
  6416. LowererMD::EmitIntToFloat(IR::Opnd *dst, IR::Opnd *src, IR::Instr *instrInsert)
  6417. {
  6418. this->lowererMDArch.EmitIntToFloat(dst, src, instrInsert);
  6419. }
  6420. void
  6421. LowererMD::EmitUIntToFloat(IR::Opnd *dst, IR::Opnd *src, IR::Instr *instrInsert)
  6422. {
  6423. this->lowererMDArch.EmitUIntToFloat(dst, src, instrInsert);
  6424. }
  6425. void
  6426. LowererMD::EmitFloat32ToFloat64(IR::Opnd *dst, IR::Opnd *src, IR::Instr *instrInsert)
  6427. {
  6428. // We should only generate this if sse2 is available
  6429. Assert(AutoSystemInfo::Data.SSE2Available());
  6430. Assert(dst->IsRegOpnd() && dst->IsFloat64());
  6431. Assert(src->IsRegOpnd() && src->GetType() == TyFloat32);
  6432. instrInsert->InsertBefore(IR::Instr::New(Js::OpCode::CVTSS2SD, dst, src, this->m_func));
  6433. }
  6434. void
  6435. LowererMD::EmitNon32BitOvfCheck(IR::Instr *instr, IR::Instr *insertInstr, IR::LabelInstr* bailOutLabel)
  6436. {
  6437. AssertMsg(instr->m_opcode == Js::OpCode::IMUL, "IMUL should be used to check for non-32 bit overflow check on x86.");
  6438. IR::RegOpnd *edxSym = IR::RegOpnd::New(TyInt32, instr->m_func);
  6439. #ifdef _M_IX86
  6440. edxSym->SetReg(RegEDX);
  6441. #else
  6442. edxSym->SetReg(RegRDX);
  6443. #endif
  6444. // dummy def for edx to force RegAlloc to generate a lifetime. This is removed later by the Peeps phase.
  6445. IR::Instr *newInstr = IR::Instr::New(Js::OpCode::NOP, edxSym, instr->m_func);
  6446. insertInstr->InsertBefore(newInstr);
  6447. IR::RegOpnd *temp = IR::RegOpnd::New(TyInt32, instr->m_func);
  6448. Assert(instr->ignoreOverflowBitCount > 32);
  6449. uint8 shamt = 64 - instr->ignoreOverflowBitCount;
  6450. // MOV temp, edx
  6451. newInstr = IR::Instr::New(Js::OpCode::MOV, temp, edxSym, instr->m_func);
  6452. insertInstr->InsertBefore(newInstr);
  6453. // SHL temp, shamt
  6454. newInstr = IR::Instr::New(Js::OpCode::SHL, temp, temp, IR::IntConstOpnd::New(shamt, TyInt8, instr->m_func, true), instr->m_func);
  6455. insertInstr->InsertBefore(newInstr);
  6456. // SAR temp, shamt
  6457. newInstr = IR::Instr::New(Js::OpCode::SAR, temp, temp, IR::IntConstOpnd::New(shamt, TyInt8, instr->m_func, true), instr->m_func);
  6458. insertInstr->InsertBefore(newInstr);
  6459. // CMP temp, edx
  6460. newInstr = IR::Instr::New(Js::OpCode::CMP, instr->m_func);
  6461. newInstr->SetSrc1(temp);
  6462. newInstr->SetSrc2(edxSym);
  6463. insertInstr->InsertBefore(newInstr);
  6464. // JNE
  6465. Lowerer::InsertBranch(Js::OpCode::JNE, false, bailOutLabel, insertInstr);
  6466. }
  6467. void LowererMD::ConvertFloatToInt32(IR::Opnd* intOpnd, IR::Opnd* floatOpnd, IR::LabelInstr * labelHelper, IR::LabelInstr * labelDone, IR::Instr * instInsert)
  6468. {
  6469. UNREFERENCED_PARAMETER(labelHelper); // used on ARM
  6470. #if defined(_M_IX86)
  6471. // We should only generate this if sse2 is available
  6472. Assert(AutoSystemInfo::Data.SSE2Available());
  6473. #endif
  6474. Assert((floatOpnd->IsRegOpnd() && floatOpnd->IsFloat()) || (floatOpnd->IsIndirOpnd() && floatOpnd->GetType() == TyMachDouble));
  6475. Assert(intOpnd->GetType() == TyInt32);
  6476. IR::Instr* instr;
  6477. {
  6478. #ifdef _M_X64
  6479. IR::Opnd* dstOpnd = IR::RegOpnd::New(TyInt64, m_func);
  6480. #else
  6481. IR::Opnd* dstOpnd = intOpnd;
  6482. #endif
  6483. // CVTTSD2SI dst, floatOpnd
  6484. IR::Instr* instr = IR::Instr::New(floatOpnd->IsFloat64() ? Js::OpCode::CVTTSD2SI : Js::OpCode::CVTTSS2SI, dstOpnd, floatOpnd, this->m_func);
  6485. instInsert->InsertBefore(instr);
  6486. // CMP dst, 0x80000000 {0x8000000000000000 on x64} -- Check for overflow
  6487. instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  6488. instr->SetSrc1(dstOpnd);
  6489. instr->SetSrc2(IR::AddrOpnd::New((Js::Var)MachSignBit, IR::AddrOpndKindConstant, this->m_func, true));
  6490. instInsert->InsertBefore(instr);
  6491. Legalize(instr);
  6492. #ifdef _M_X64
  6493. // Truncate to int32 for x64. We still need to go to helper though if we have int64 overflow.
  6494. // MOV_TRUNC intOpnd, tmpOpnd
  6495. instr = IR::Instr::New(Js::OpCode::MOV_TRUNC, intOpnd, dstOpnd, this->m_func);
  6496. instInsert->InsertBefore(instr);
  6497. #endif
  6498. }
  6499. // JNE $done
  6500. instr = IR::BranchInstr::New(Js::OpCode::JNE, labelDone, this->m_func);
  6501. instInsert->InsertBefore(instr);
  6502. // It does overflow - Let's try using FISTTP which uses 64 bits and is relevant only for x86
  6503. // but requires going to memory and should only be used in overflow scenarios
  6504. #ifdef _M_IX86
  6505. if (AutoSystemInfo::Data.SSE3Available())
  6506. {
  6507. IR::Opnd* floatStackOpnd;
  6508. StackSym* tempSymDouble = this->m_func->tempSymDouble;
  6509. if (!tempSymDouble)
  6510. {
  6511. this->m_func->tempSymDouble = StackSym::New(TyFloat64, this->m_func);
  6512. this->m_func->StackAllocate(this->m_func->tempSymDouble, MachDouble);
  6513. tempSymDouble = this->m_func->tempSymDouble;
  6514. }
  6515. IR::Opnd * float64Opnd;
  6516. if (floatOpnd->IsFloat32())
  6517. {
  6518. float64Opnd = IR::RegOpnd::New(TyFloat64, m_func);
  6519. IR::Instr* instr = IR::Instr::New(Js::OpCode::CVTSS2SD, float64Opnd, floatOpnd, m_func);
  6520. instInsert->InsertBefore(instr);
  6521. }
  6522. else
  6523. {
  6524. float64Opnd = floatOpnd;
  6525. }
  6526. if (float64Opnd->IsRegOpnd())
  6527. {
  6528. floatStackOpnd = IR::SymOpnd::New(tempSymDouble, TyMachDouble, m_func);
  6529. IR::Instr* instr = IR::Instr::New(Js::OpCode::MOVSD, floatStackOpnd, float64Opnd, m_func);
  6530. instInsert->InsertBefore(instr);
  6531. }
  6532. else
  6533. {
  6534. floatStackOpnd = float64Opnd;
  6535. }
  6536. // FLD [tmpDouble]
  6537. instr = IR::Instr::New(Js::OpCode::FLD, floatStackOpnd, floatStackOpnd, m_func);
  6538. instInsert->InsertBefore(instr);
  6539. if (!float64Opnd->IsRegOpnd())
  6540. {
  6541. floatStackOpnd = IR::SymOpnd::New(tempSymDouble, TyMachDouble, m_func);
  6542. }
  6543. // FISTTP qword ptr [tmpDouble]
  6544. instr = IR::Instr::New(Js::OpCode::FISTTP, floatStackOpnd, m_func);
  6545. instInsert->InsertBefore(instr);
  6546. StackSym *intSym = StackSym::New(TyInt32, m_func);
  6547. intSym->m_offset = tempSymDouble->m_offset;
  6548. intSym->m_allocated = true;
  6549. IR::Opnd* lowerBitsOpnd = IR::SymOpnd::New(intSym, TyInt32, m_func);
  6550. // MOV dst, dword ptr [tmpDouble]
  6551. instr = IR::Instr::New(Js::OpCode::MOV, intOpnd, lowerBitsOpnd, m_func);
  6552. instInsert->InsertBefore(instr);
  6553. // TEST dst, dst -- Check for overflow
  6554. instr = IR::Instr::New(Js::OpCode::TEST, this->m_func);
  6555. instr->SetSrc1(intOpnd);
  6556. instr->SetSrc2(intOpnd);
  6557. instInsert->InsertBefore(instr);
  6558. instr = IR::BranchInstr::New(Js::OpCode::JNE, labelDone, this->m_func);
  6559. instInsert->InsertBefore(instr);
  6560. // CMP [tmpDouble - 4], 0x80000000
  6561. StackSym* higherBitsSym = StackSym::New(TyInt32, m_func);
  6562. higherBitsSym->m_offset = tempSymDouble->m_offset + 4;
  6563. higherBitsSym->m_allocated = true;
  6564. instr = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  6565. instr->SetSrc1(IR::SymOpnd::New(higherBitsSym, TyInt32, m_func));
  6566. instr->SetSrc2(IR::IntConstOpnd::New(0x80000000, TyInt32, this->m_func, true));
  6567. instInsert->InsertBefore(instr);
  6568. instr = IR::BranchInstr::New(Js::OpCode::JNE, labelDone, this->m_func);
  6569. instInsert->InsertBefore(instr);
  6570. }
  6571. #endif
  6572. }
  6573. IR::Instr *
  6574. LowererMD::InsertConvertFloat64ToInt32(const RoundMode roundMode, IR::Opnd *const dst, IR::Opnd *const src, IR::Instr *const insertBeforeInstr)
  6575. {
  6576. Assert(dst);
  6577. Assert(dst->IsInt32());
  6578. Assert(src);
  6579. Assert(src->IsFloat64());
  6580. Assert(insertBeforeInstr);
  6581. // The caller is expected to check for overflow. To have that work be done automatically, use LowererMD::EmitFloatToInt.
  6582. Func *const func = insertBeforeInstr->m_func;
  6583. IR::AutoReuseOpnd autoReuseSrcPlusHalf;
  6584. IR::Instr *instr = nullptr;
  6585. switch (roundMode)
  6586. {
  6587. case RoundModeTowardInteger:
  6588. {
  6589. // Conversion with rounding towards nearest integer is not supported by the architecture. Add 0.5 and do a
  6590. // round-toward-zero conversion instead.
  6591. IR::RegOpnd *const srcPlusHalf = IR::RegOpnd::New(TyFloat64, func);
  6592. autoReuseSrcPlusHalf.Initialize(srcPlusHalf, func);
  6593. Lowerer::InsertAdd(
  6594. false /* needFlags */,
  6595. srcPlusHalf,
  6596. src,
  6597. IR::MemRefOpnd::New((double*)&(Js::JavascriptNumber::k_PointFive), TyFloat64, func,
  6598. IR::AddrOpndKindDynamicDoubleRef),
  6599. insertBeforeInstr);
  6600. instr = IR::Instr::New(LowererMD::MDConvertFloat64ToInt32Opcode(RoundModeTowardZero), dst, srcPlusHalf, func);
  6601. insertBeforeInstr->InsertBefore(instr);
  6602. LowererMD::Legalize(instr);
  6603. return instr;
  6604. }
  6605. case RoundModeHalfToEven:
  6606. {
  6607. instr = IR::Instr::New(LowererMD::MDConvertFloat64ToInt32Opcode(RoundModeHalfToEven), dst, src, func);
  6608. insertBeforeInstr->InsertBefore(instr);
  6609. LowererMD::Legalize(instr);
  6610. return instr;
  6611. }
  6612. default:
  6613. AssertMsg(0, "RoundMode not supported.");
  6614. return nullptr;
  6615. }
  6616. }
  6617. void
  6618. LowererMD::EmitFloatToInt(IR::Opnd *dst, IR::Opnd *src, IR::Instr *instrInsert)
  6619. {
  6620. #ifdef _M_IX86
  6621. // We should only generate this if sse2 is available
  6622. Assert(AutoSystemInfo::Data.SSE2Available());
  6623. #endif
  6624. IR::LabelInstr *labelDone = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  6625. IR::LabelInstr *labelHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  6626. IR::Instr *instr;
  6627. ConvertFloatToInt32(dst, src, labelHelper, labelDone, instrInsert);
  6628. // $Helper
  6629. instrInsert->InsertBefore(labelHelper);
  6630. IR::Opnd * arg = src;
  6631. if (src->IsFloat32())
  6632. {
  6633. arg = IR::RegOpnd::New(TyFloat64, m_func);
  6634. EmitFloat32ToFloat64(arg, src, instrInsert);
  6635. }
  6636. // dst = ToInt32Core(src);
  6637. LoadDoubleHelperArgument(instrInsert, arg);
  6638. instr = IR::Instr::New(Js::OpCode::CALL, dst, this->m_func);
  6639. instrInsert->InsertBefore(instr);
  6640. this->ChangeToHelperCall(instr, IR::HelperConv_ToInt32Core);
  6641. // $Done
  6642. instrInsert->InsertBefore(labelDone);
  6643. }
  6644. void
  6645. LowererMD::EmitLoadVarNoCheck(IR::RegOpnd * dst, IR::RegOpnd * src, IR::Instr *instrLoad, bool isFromUint32, bool isHelper)
  6646. {
  6647. #ifdef _M_IX86
  6648. if (!AutoSystemInfo::Data.SSE2Available())
  6649. {
  6650. IR::JnHelperMethod helperMethod;
  6651. // PUSH &floatTemp
  6652. IR::Opnd *tempOpnd;
  6653. if (instrLoad->dstIsTempNumber)
  6654. {
  6655. helperMethod = isFromUint32 ? IR::HelperOp_UInt32ToAtomInPlace : IR::HelperOp_Int32ToAtomInPlace;
  6656. // Use the original dst to get the temp number sym
  6657. StackSym * tempNumberSym = this->m_lowerer->GetTempNumberSym(instrLoad->GetDst(), instrLoad->dstIsTempNumberTransferred);
  6658. IR::Instr *load = this->LoadStackAddress(tempNumberSym);
  6659. instrLoad->InsertBefore(load);
  6660. tempOpnd = load->GetDst();
  6661. this->LoadHelperArgument(instrLoad, tempOpnd);
  6662. }
  6663. else
  6664. {
  6665. helperMethod = isFromUint32 ? IR::HelperOp_UInt32ToAtom : IR::HelperOp_Int32ToAtom;
  6666. }
  6667. // PUSH memContext
  6668. this->m_lowerer->LoadScriptContext(instrLoad);
  6669. // PUSH s1
  6670. this->LoadHelperArgument(instrLoad, src);
  6671. // dst = ToVar()
  6672. IR::Instr * instr = IR::Instr::New(Js::OpCode::Call, dst,
  6673. IR::HelperCallOpnd::New(helperMethod, this->m_func), this->m_func);
  6674. instrLoad->InsertBefore(instr);
  6675. this->LowerCall(instr, 0);
  6676. return;
  6677. }
  6678. #endif
  6679. IR::RegOpnd * floatReg = IR::RegOpnd::New(TyFloat64, this->m_func);
  6680. if (isFromUint32)
  6681. {
  6682. this->EmitUIntToFloat(floatReg, src, instrLoad);
  6683. }
  6684. else
  6685. {
  6686. this->EmitIntToFloat(floatReg, src, instrLoad);
  6687. }
  6688. this->SaveDoubleToVar(dst, floatReg, instrLoad, instrLoad, isHelper);
  6689. }
  6690. IR::Instr *
  6691. LowererMD::LowerGetCachedFunc(IR::Instr *instr)
  6692. {
  6693. // src1 is an ActivationObjectEx, and we want to get the function object identified by the index (src2)
  6694. // dst = MOV (src1)->GetFuncCacheEntry(src2)->func
  6695. //
  6696. // => [src1 + (offsetof(src1, cache) + (src2 * sizeof(FuncCacheEntry)) + offsetof(FuncCacheEntry, func))]
  6697. IR::IntConstOpnd *src2Opnd = instr->UnlinkSrc2()->AsIntConstOpnd();
  6698. IR::RegOpnd *src1Opnd = instr->UnlinkSrc1()->AsRegOpnd();
  6699. instr->m_opcode = Js::OpCode::MOV;
  6700. IntConstType offset = (src2Opnd->GetValue() * sizeof(Js::FuncCacheEntry)) + Js::ActivationObjectEx::GetOffsetOfCache() + offsetof(Js::FuncCacheEntry, func);
  6701. Assert(Math::FitsInDWord(offset));
  6702. instr->SetSrc1(IR::IndirOpnd::New(src1Opnd, (int32)offset, TyVar, this->m_func));
  6703. src2Opnd->Free(this->m_func);
  6704. return instr->m_prev;
  6705. }
  6706. IR::Instr *
  6707. LowererMD::LowerCommitScope(IR::Instr *instrCommit)
  6708. {
  6709. IR::Instr *instrPrev = instrCommit->m_prev;
  6710. IR::RegOpnd *baseOpnd = instrCommit->UnlinkSrc1()->AsRegOpnd();
  6711. IR::Opnd *opnd;
  6712. IR::Instr * insertInstr = instrCommit->m_next;
  6713. // Write undef to all the local var slots.
  6714. opnd = IR::IndirOpnd::New(baseOpnd, Js::ActivationObjectEx::GetOffsetOfCommitFlag(), TyInt8, this->m_func);
  6715. instrCommit->SetDst(opnd);
  6716. instrCommit->SetSrc1(IR::IntConstOpnd::New(1, TyInt8, this->m_func));
  6717. IR::IntConstOpnd *intConstOpnd = instrCommit->UnlinkSrc2()->AsIntConstOpnd();
  6718. LowererMD::ChangeToAssign(instrCommit);
  6719. const Js::PropertyIdArray *propIds = Js::ByteCodeReader::ReadPropertyIdArray(intConstOpnd->AsUint32(), instrCommit->m_func->GetJnFunction());
  6720. intConstOpnd->Free(this->m_func);
  6721. uint firstVarSlot = (uint)Js::ActivationObjectEx::GetFirstVarSlot(propIds);
  6722. if (firstVarSlot < propIds->count)
  6723. {
  6724. IR::RegOpnd *undefOpnd = IR::RegOpnd::New(TyMachReg, this->m_func);
  6725. LowererMD::CreateAssign(undefOpnd, m_lowerer->LoadLibraryValueOpnd(insertInstr, LibraryValue::ValueUndefined), insertInstr);
  6726. IR::RegOpnd *slotBaseOpnd = IR::RegOpnd::New(TyMachReg, this->m_func);
  6727. // Load a pointer to the aux slots. We assume that all ActivationObject's have only aux slots.
  6728. opnd = IR::IndirOpnd::New(baseOpnd, Js::DynamicObject::GetOffsetOfAuxSlots(), TyMachReg, this->m_func);
  6729. this->CreateAssign(slotBaseOpnd, opnd, insertInstr);
  6730. for (uint i = firstVarSlot; i < propIds->count; i++)
  6731. {
  6732. opnd = IR::IndirOpnd::New(slotBaseOpnd, i << this->GetDefaultIndirScale(), TyMachReg, this->m_func);
  6733. this->CreateAssign(opnd, undefOpnd, insertInstr);
  6734. }
  6735. }
  6736. return instrPrev;
  6737. }
  6738. void
  6739. LowererMD::ImmedSrcToReg(IR::Instr * instr, IR::Opnd * newOpnd, int srcNum)
  6740. {
  6741. if (srcNum == 2)
  6742. {
  6743. instr->SetSrc2(newOpnd);
  6744. }
  6745. else
  6746. {
  6747. Assert(srcNum == 1);
  6748. instr->SetSrc1(newOpnd);
  6749. }
  6750. }
  6751. IR::LabelInstr *
  6752. LowererMD::GetBailOutStackRestoreLabel(BailOutInfo * bailOutInfo, IR::LabelInstr * exitTargetInstr)
  6753. {
  6754. return lowererMDArch.GetBailOutStackRestoreLabel(bailOutInfo, exitTargetInstr);
  6755. }
  6756. StackSym *
  6757. LowererMD::GetImplicitParamSlotSym(Js::ArgSlot argSlot)
  6758. {
  6759. return GetImplicitParamSlotSym(argSlot, this->m_func);
  6760. }
  6761. StackSym *
  6762. LowererMD::GetImplicitParamSlotSym(Js::ArgSlot argSlot, Func * func)
  6763. {
  6764. // Stack looks like (EBP chain)+0, (return addr)+4, (function object)+8, (arg count)+12, (this)+16, actual args
  6765. // Pass in the EBP+8 to start at the function object, the start of the implicit param slots
  6766. StackSym * stackSym = StackSym::NewParamSlotSym(argSlot, func);
  6767. func->SetArgOffset(stackSym, (2 + argSlot) * MachPtr);
  6768. return stackSym;
  6769. }
  6770. bool LowererMD::GenerateFastAnd(IR::Instr * instrAnd)
  6771. {
  6772. return this->lowererMDArch.GenerateFastAnd(instrAnd);
  6773. }
  6774. bool LowererMD::GenerateFastXor(IR::Instr * instrXor)
  6775. {
  6776. return this->lowererMDArch.GenerateFastXor(instrXor);
  6777. }
  6778. bool LowererMD::GenerateFastOr(IR::Instr * instrOr)
  6779. {
  6780. return this->lowererMDArch.GenerateFastOr(instrOr);
  6781. }
  6782. bool LowererMD::GenerateFastNot(IR::Instr * instrNot)
  6783. {
  6784. return this->lowererMDArch.GenerateFastNot(instrNot);
  6785. }
  6786. bool LowererMD::GenerateFastShiftLeft(IR::Instr * instrShift)
  6787. {
  6788. return this->lowererMDArch.GenerateFastShiftLeft(instrShift);
  6789. }
  6790. bool LowererMD::GenerateFastShiftRight(IR::Instr * instrShift)
  6791. {
  6792. return this->lowererMDArch.GenerateFastShiftRight(instrShift);
  6793. }
  6794. void LowererMD::GenerateIsDynamicObject(IR::RegOpnd *regOpnd, IR::Instr *insertInstr, IR::LabelInstr *labelHelper, bool fContinueLabel)
  6795. {
  6796. // CMP [srcReg], Js::DynamicObject::`vtable'
  6797. {
  6798. IR::Instr *cmp = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  6799. cmp->SetSrc1(IR::IndirOpnd::New(regOpnd, 0, TyMachPtr, m_func));
  6800. cmp->SetSrc2(m_lowerer->LoadVTableValueOpnd(insertInstr, VTableValue::VtableDynamicObject));
  6801. insertInstr->InsertBefore(cmp);
  6802. Legalize(cmp);
  6803. }
  6804. if (fContinueLabel)
  6805. {
  6806. // JEQ $fallThough
  6807. IR::Instr * jne = IR::BranchInstr::New(Js::OpCode::JEQ, labelHelper, this->m_func);
  6808. insertInstr->InsertBefore(jne);
  6809. }
  6810. else
  6811. {
  6812. // JNE $helper
  6813. IR::Instr * jne = IR::BranchInstr::New(Js::OpCode::JNE, labelHelper, this->m_func);
  6814. insertInstr->InsertBefore(jne);
  6815. }
  6816. }
  6817. void LowererMD::GenerateIsRecyclableObject(IR::RegOpnd *regOpnd, IR::Instr *insertInstr, IR::LabelInstr *labelHelper, bool checkObjectAndDynamicObject)
  6818. {
  6819. // CMP [srcReg], Js::DynamicObject::`vtable'
  6820. // JEQ $fallThough
  6821. // MOV r1, [src1 + offset(type)] -- get the type id
  6822. // MOV r1, [r1 + offset(typeId)]
  6823. // ADD r1, ~TypeIds_LastJavascriptPrimitiveType -- if (typeId > TypeIds_LastJavascriptPrimitiveType && typeId <= TypeIds_LastTrueJavascriptObjectType)
  6824. // CMP r1, (TypeIds_LastTrueJavascriptObjectType - TypeIds_LastJavascriptPrimitiveType - 1)
  6825. // JA $helper
  6826. //fallThrough:
  6827. IR::LabelInstr *labelFallthrough = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  6828. if (checkObjectAndDynamicObject)
  6829. {
  6830. if (!regOpnd->IsNotTaggedValue())
  6831. {
  6832. GenerateObjectTest(regOpnd, insertInstr, labelHelper);
  6833. }
  6834. this->GenerateIsDynamicObject(regOpnd, insertInstr, labelFallthrough, true);
  6835. }
  6836. IR::RegOpnd * typeRegOpnd = IR::RegOpnd::New(TyMachReg, this->m_func);
  6837. IR::RegOpnd * typeIdRegOpnd = IR::RegOpnd::New(TyInt32, this->m_func);
  6838. // MOV r1, [src1 + offset(type)]
  6839. {
  6840. IR::IndirOpnd * indirOpnd = IR::IndirOpnd::New(regOpnd, Js::RecyclableObject::GetOffsetOfType(), TyMachReg, this->m_func);
  6841. IR::Instr * mov = IR::Instr::New(Js::OpCode::MOV, typeRegOpnd, indirOpnd, this->m_func);
  6842. insertInstr->InsertBefore(mov);
  6843. }
  6844. // MOV r1, [r1 + offset(typeId)]
  6845. {
  6846. IR::IndirOpnd * indirOpnd = IR::IndirOpnd::New(typeRegOpnd, Js::Type::GetOffsetOfTypeId(), TyInt32, this->m_func);
  6847. IR::Instr * mov = IR::Instr::New(Js::OpCode::MOV, typeIdRegOpnd, indirOpnd, this->m_func);
  6848. insertInstr->InsertBefore(mov);
  6849. }
  6850. // ADD r1, ~TypeIds_LastJavascriptPrimitiveType
  6851. {
  6852. 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);
  6853. insertInstr->InsertBefore(add);
  6854. }
  6855. // CMP r1, (TypeIds_LastTrueJavascriptObjectType - TypeIds_LastJavascriptPrimitiveType - 1)
  6856. {
  6857. IR::Instr * cmp = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  6858. cmp->SetSrc1(typeIdRegOpnd);
  6859. cmp->SetSrc2(IR::IntConstOpnd::New(Js::TypeIds_LastTrueJavascriptObjectType - Js::TypeIds_LastJavascriptPrimitiveType - 1, TyInt32, this->m_func));
  6860. insertInstr->InsertBefore(cmp);
  6861. }
  6862. // JA $helper
  6863. {
  6864. IR::Instr * jbe = IR::BranchInstr::New(Js::OpCode::JA, labelHelper, this->m_func);
  6865. insertInstr->InsertBefore(jbe);
  6866. }
  6867. // $fallThrough
  6868. insertInstr->InsertBefore(labelFallthrough);
  6869. }
  6870. bool
  6871. LowererMD::GenerateLdThisCheck(IR::Instr * instr)
  6872. {
  6873. //
  6874. // If not a recyclable object, jump to $helper
  6875. // MOV dst, src1 -- return the object itself
  6876. // JMP $fallthrough
  6877. // $helper:
  6878. // (caller generates helper call)
  6879. // $fallthrough:
  6880. //
  6881. IR::RegOpnd * src1 = instr->GetSrc1()->AsRegOpnd();
  6882. IR::LabelInstr * helper = IR::LabelInstr::New(Js::OpCode::Label, m_func, true);
  6883. IR::LabelInstr * fallthrough = IR::LabelInstr::New(Js::OpCode::Label, m_func);
  6884. this->GenerateIsRecyclableObject(src1, instr, helper);
  6885. // MOV dst, src1
  6886. if (instr->GetDst() && !instr->GetDst()->IsEqual(src1))
  6887. {
  6888. IR::Instr * mov = IR::Instr::New(Js::OpCode::MOV, instr->GetDst(), src1, this->m_func);
  6889. instr->InsertBefore(mov);
  6890. }
  6891. // JMP $fallthrough
  6892. {
  6893. IR::Instr * jmp = IR::BranchInstr::New(Js::OpCode::JMP, fallthrough, this->m_func);
  6894. instr->InsertBefore(jmp);
  6895. }
  6896. // $helper:
  6897. // (caller generates helper call)
  6898. // $fallthrough:
  6899. instr->InsertBefore(helper);
  6900. instr->InsertAfter(fallthrough);
  6901. return true;
  6902. }
  6903. //
  6904. // TEST src, Js::AtomTag
  6905. // JNE $done
  6906. // MOV typeReg, objectSrc + offsetof(RecyclableObject::type)
  6907. // CMP [typeReg + offsetof(Type::typeid)], TypeIds_ActivationObject
  6908. // JEQ $helper
  6909. // $done:
  6910. // MOV dst, src
  6911. // JMP $fallthru
  6912. // helper:
  6913. // MOV dst, undefined
  6914. // $fallthru:
  6915. bool
  6916. LowererMD::GenerateLdThisStrict(IR::Instr* instr)
  6917. {
  6918. IR::RegOpnd * src1 = instr->GetSrc1()->AsRegOpnd();
  6919. IR::RegOpnd * typeReg = IR::RegOpnd::New(TyMachReg, this->m_func);
  6920. IR::LabelInstr * done = IR::LabelInstr::New(Js::OpCode::Label, m_func);
  6921. IR::LabelInstr * fallthru = IR::LabelInstr::New(Js::OpCode::Label, m_func);
  6922. IR::LabelInstr * helper = IR::LabelInstr::New(Js::OpCode::Label, m_func, /*helper*/true);
  6923. bool assign = instr->GetDst() && !instr->GetDst()->IsEqual(src1);
  6924. // TEST src1, Js::AtomTag
  6925. // JNE $done
  6926. if(!src1->IsNotTaggedValue())
  6927. {
  6928. GenerateObjectTest(src1, instr, assign ? done : fallthru);
  6929. }
  6930. // MOV typeReg, objectSrc + offsetof(RecyclableObject::type)
  6931. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, typeReg,
  6932. IR::IndirOpnd::New(src1, Js::RecyclableObject::GetOffsetOfType(), TyMachReg, m_func),
  6933. m_func));
  6934. // CMP [typeReg + offsetof(Type::typeid)], TypeIds_ActivationObject
  6935. {
  6936. IR::Instr * cmp = IR::Instr::New(Js::OpCode::CMP, m_func);
  6937. cmp->SetSrc1(IR::IndirOpnd::New(typeReg, Js::Type::GetOffsetOfTypeId(), TyInt32, m_func));
  6938. cmp->SetSrc2(IR::IntConstOpnd::New(Js::TypeId::TypeIds_ActivationObject, TyInt32, m_func));
  6939. instr->InsertBefore(cmp);
  6940. }
  6941. // JEQ $helper
  6942. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JEQ, helper, m_func));
  6943. if (assign)
  6944. {
  6945. // $done:
  6946. // MOV dst, src
  6947. instr->InsertBefore(done);
  6948. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, instr->GetDst(), src1, m_func));
  6949. }
  6950. // JMP $fallthru
  6951. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JMP, fallthru, m_func));
  6952. instr->InsertBefore(helper);
  6953. if (instr->GetDst())
  6954. {
  6955. // MOV dst, undefined
  6956. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, instr->GetDst(),
  6957. m_lowerer->LoadLibraryValueOpnd(instr, LibraryValue::ValueUndefined), m_func));
  6958. }
  6959. // $fallthru:
  6960. instr->InsertAfter(fallthru);
  6961. return true;
  6962. }
  6963. // given object instanceof function, functionReg is a register with function,
  6964. // objectReg is a register with instance and inlineCache is an InstIsInlineCache.
  6965. // We want to generate:
  6966. //
  6967. // fallback on helper (will patch the inline cache) if function does not match the cache
  6968. // MOV dst, Js::false
  6969. // CMP functionReg, [&(inlineCache->function)]
  6970. // JNE helper
  6971. //
  6972. // fallback if object is a tagged int
  6973. // TEST objectReg, Js::AtomTag
  6974. // JNE done
  6975. //
  6976. // fallback if object's type is not the cached type
  6977. // MOV typeReg, objectSrc + offsetof(RecyclableObject::type)
  6978. // CMP typeReg, [&(inlineCache->type]
  6979. // JNE checkPrimType
  6980. // use the cached result and fallthrough
  6981. // MOV dst, [&(inlineCache->result)]
  6982. // JMP done
  6983. // return false if object is a primitive
  6984. // $checkPrimType
  6985. // CMP [typeReg + offsetof(Type::typeid)], TypeIds_LastJavascriptPrimitiveType
  6986. // JLE done
  6987. //
  6988. //
  6989. // $helper
  6990. // $done
  6991. bool
  6992. LowererMD::GenerateFastIsInst(IR::Instr * instr)
  6993. {
  6994. IR::LabelInstr * helper = IR::LabelInstr::New(Js::OpCode::Label, m_func, true);
  6995. IR::LabelInstr * checkPrimType = IR::LabelInstr::New(Js::OpCode::Label, m_func, true);
  6996. IR::LabelInstr * done = IR::LabelInstr::New(Js::OpCode::Label, m_func);
  6997. IR::RegOpnd * typeReg = IR::RegOpnd::New(TyMachReg, this->m_func);
  6998. IR::Opnd * objectSrc;
  6999. IR::RegOpnd * objectReg;
  7000. IR::Opnd * functionSrc;
  7001. IR::RegOpnd * functionReg;
  7002. Js::IsInstInlineCache * inlineCache;
  7003. IR::Instr * instrArg;
  7004. // We are going to use the extra ArgOut_A instructions to lower the helper call later,
  7005. // so we leave them alone here and clean them up then.
  7006. inlineCache = instr->m_func->GetJnFunction()->GetIsInstInlineCache(instr->GetSrc1()->AsIntConstOpnd()->AsUint32());
  7007. Assert(instr->GetSrc2()->AsRegOpnd()->m_sym->m_isSingleDef);
  7008. instrArg = instr->GetSrc2()->AsRegOpnd()->m_sym->m_instrDef;
  7009. objectSrc = instrArg->GetSrc1();
  7010. Assert(instrArg->GetSrc2()->AsRegOpnd()->m_sym->m_isSingleDef);
  7011. instrArg = instrArg->GetSrc2()->AsRegOpnd()->m_sym->m_instrDef;
  7012. functionSrc = instrArg->GetSrc1();
  7013. Assert(instrArg->GetSrc2() == nullptr);
  7014. // MOV dst, Js::false
  7015. Lowerer::InsertMove(instr->GetDst(), m_lowerer->LoadLibraryValueOpnd(instr, LibraryValue::ValueFalse), instr);
  7016. if (functionSrc->IsRegOpnd())
  7017. {
  7018. functionReg = functionSrc->AsRegOpnd();
  7019. }
  7020. else
  7021. {
  7022. functionReg = IR::RegOpnd::New(TyMachReg, this->m_func);
  7023. // MOV functionReg, functionSrc
  7024. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, functionReg, functionSrc, m_func));
  7025. }
  7026. // CMP functionReg, [&(inlineCache->function)]
  7027. {
  7028. IR::Instr * cmp = IR::Instr::New(Js::OpCode::CMP, m_func);
  7029. cmp->SetSrc1(functionReg);
  7030. cmp->SetSrc2(IR::MemRefOpnd::New((void*)&(inlineCache->function), TyMachReg, m_func,
  7031. IR::AddrOpndKindDynamicIsInstInlineCacheFunctionRef));
  7032. instr->InsertBefore(cmp);
  7033. Legalize(cmp);
  7034. }
  7035. // JNE helper
  7036. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JNE, helper, m_func));
  7037. if (objectSrc->IsRegOpnd())
  7038. {
  7039. objectReg = objectSrc->AsRegOpnd();
  7040. }
  7041. else
  7042. {
  7043. objectReg = IR::RegOpnd::New(TyMachReg, this->m_func);
  7044. // MOV objectReg, objectSrc
  7045. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, objectReg, objectSrc, m_func));
  7046. }
  7047. // TEST objectReg, Js::AtomTag
  7048. // JNE done
  7049. GenerateObjectTest(objectReg, instr, done);
  7050. // MOV typeReg, objectSrc + offsetof(RecyclableObject::type)
  7051. instr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, typeReg,
  7052. IR::IndirOpnd::New(objectReg, Js::RecyclableObject::GetOffsetOfType(), TyMachReg, m_func),
  7053. m_func));
  7054. // CMP typeReg, [&(inlineCache->type]
  7055. {
  7056. IR::Instr * cmp = IR::Instr::New(Js::OpCode::CMP, m_func);
  7057. cmp->SetSrc1(typeReg);
  7058. cmp->SetSrc2(IR::MemRefOpnd::New((void*)&(inlineCache->type), TyMachReg, m_func,
  7059. IR::AddrOpndKindDynamicIsInstInlineCacheTypeRef));
  7060. instr->InsertBefore(cmp);
  7061. Legalize(cmp);
  7062. }
  7063. // JNE checkPrimType
  7064. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JNE, checkPrimType, m_func));
  7065. // MOV dst, [&(inlineCache->result)]
  7066. Lowerer::InsertMove(instr->GetDst(), IR::MemRefOpnd::New((void*)&(inlineCache->result), TyMachReg, m_func,
  7067. IR::AddrOpndKindDynamicIsInstInlineCacheResultRef), instr);
  7068. // JMP done
  7069. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JMP, done, m_func));
  7070. // LABEL checkPrimType
  7071. instr->InsertBefore(checkPrimType);
  7072. // CMP [typeReg + offsetof(Type::typeid)], TypeIds_LastJavascriptPrimitiveType
  7073. {
  7074. IR::Instr * cmp = IR::Instr::New(Js::OpCode::CMP, m_func);
  7075. cmp->SetSrc1(IR::IndirOpnd::New(typeReg, Js::Type::GetOffsetOfTypeId(), TyInt32, m_func));
  7076. cmp->SetSrc2(IR::IntConstOpnd::New(Js::TypeId::TypeIds_LastJavascriptPrimitiveType, TyInt32, m_func));
  7077. instr->InsertBefore(cmp);
  7078. }
  7079. // JLE done
  7080. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JLE, done, m_func));
  7081. // LABEL helper
  7082. instr->InsertBefore(helper);
  7083. instr->InsertAfter(done);
  7084. return true;
  7085. }
  7086. void LowererMD::GenerateIsJsObjectTest(IR::RegOpnd* instanceReg, IR::Instr* insertInstr, IR::LabelInstr* labelHelper)
  7087. {
  7088. // TEST instanceReg, (Js::AtomTag_IntPtr | Js::FloatTag_Value )
  7089. GenerateObjectTest(instanceReg, insertInstr, labelHelper);
  7090. IR::RegOpnd * typeReg = IR::RegOpnd::New(TyMachReg, this->m_func);
  7091. // MOV typeReg, instanceReg + offsetof(RecyclableObject::type)
  7092. insertInstr->InsertBefore(IR::Instr::New(Js::OpCode::MOV, typeReg,
  7093. IR::IndirOpnd::New(instanceReg, Js::RecyclableObject::GetOffsetOfType(), TyMachReg, m_func),
  7094. m_func));
  7095. // CMP [typeReg + offsetof(Type::typeid)], TypeIds_LastJavascriptPrimitiveType
  7096. IR::Instr * cmp = IR::Instr::New(Js::OpCode::CMP, this->m_func);
  7097. cmp->SetSrc1(IR::IndirOpnd::New(typeReg, Js::Type::GetOffsetOfTypeId(), TyInt32, this->m_func));
  7098. cmp->SetSrc2(IR::IntConstOpnd::New(Js::TypeId::TypeIds_LastJavascriptPrimitiveType, TyInt32, this->m_func));
  7099. insertInstr->InsertBefore(cmp);
  7100. // JLE labelHelper
  7101. insertInstr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JLE, labelHelper, this->m_func));
  7102. }
  7103. IR::Instr *
  7104. LowererMD::LowerToFloat(IR::Instr *instr)
  7105. {
  7106. switch (instr->m_opcode)
  7107. {
  7108. case Js::OpCode::Add_A:
  7109. Assert(instr->GetDst()->GetType() == instr->GetSrc1()->GetType());
  7110. Assert(instr->GetSrc1()->GetType() == instr->GetSrc2()->GetType());
  7111. instr->m_opcode = instr->GetSrc1()->IsFloat64() ? Js::OpCode::ADDSD : Js::OpCode::ADDSS;
  7112. break;
  7113. case Js::OpCode::Sub_A:
  7114. Assert(instr->GetDst()->GetType() == instr->GetSrc1()->GetType());
  7115. Assert(instr->GetSrc1()->GetType() == instr->GetSrc2()->GetType());
  7116. instr->m_opcode = instr->GetSrc1()->IsFloat64() ? Js::OpCode::SUBSD : Js::OpCode::SUBSS;
  7117. break;
  7118. case Js::OpCode::Mul_A:
  7119. Assert(instr->GetDst()->GetType() == instr->GetSrc1()->GetType());
  7120. Assert(instr->GetSrc1()->GetType() == instr->GetSrc2()->GetType());
  7121. instr->m_opcode = instr->GetSrc1()->IsFloat64() ? Js::OpCode::MULSD : Js::OpCode::MULSS;
  7122. break;
  7123. case Js::OpCode::Div_A:
  7124. Assert(instr->GetDst()->GetType() == instr->GetSrc1()->GetType());
  7125. Assert(instr->GetSrc1()->GetType() == instr->GetSrc2()->GetType());
  7126. instr->m_opcode = instr->GetSrc1()->IsFloat64() ? Js::OpCode::DIVSD : Js::OpCode::DIVSS;
  7127. break;
  7128. case Js::OpCode::Neg_A:
  7129. {
  7130. IR::Opnd *opnd;
  7131. instr->m_opcode = Js::OpCode::XORPS;
  7132. if (instr->GetDst()->IsFloat32())
  7133. {
  7134. opnd = IR::MemRefOpnd::New((void*)&Js::JavascriptNumber::MaskNegFloat, TyMachDouble, this->m_func, IR::AddrOpndKindDynamicFloatRef);
  7135. }
  7136. else
  7137. {
  7138. Assert(instr->GetDst()->IsFloat64());
  7139. opnd = IR::MemRefOpnd::New((void*)&Js::JavascriptNumber::MaskNegDouble, TyMachDouble, this->m_func, IR::AddrOpndKindDynamicDoubleRef);
  7140. }
  7141. instr->SetSrc2(opnd);
  7142. Legalize(instr);
  7143. break;
  7144. }
  7145. case Js::OpCode::BrEq_A:
  7146. case Js::OpCode::BrNeq_A:
  7147. case Js::OpCode::BrSrEq_A:
  7148. case Js::OpCode::BrSrNeq_A:
  7149. case Js::OpCode::BrGt_A:
  7150. case Js::OpCode::BrGe_A:
  7151. case Js::OpCode::BrLt_A:
  7152. case Js::OpCode::BrLe_A:
  7153. case Js::OpCode::BrNotEq_A:
  7154. case Js::OpCode::BrNotNeq_A:
  7155. case Js::OpCode::BrSrNotEq_A:
  7156. case Js::OpCode::BrSrNotNeq_A:
  7157. case Js::OpCode::BrNotGt_A:
  7158. case Js::OpCode::BrNotGe_A:
  7159. case Js::OpCode::BrNotLt_A:
  7160. case Js::OpCode::BrNotLe_A:
  7161. return this->LowerFloatCondBranch(instr->AsBranchInstr());
  7162. default:
  7163. Assume(UNREACHED);
  7164. }
  7165. this->MakeDstEquSrc1(instr);
  7166. return instr;
  7167. }
  7168. IR::BranchInstr *
  7169. LowererMD::LowerFloatCondBranch(IR::BranchInstr *instrBranch, bool ignoreNan)
  7170. {
  7171. Js::OpCode brOpcode = Js::OpCode::InvalidOpCode;
  7172. Js::OpCode cmpOpcode = Js::OpCode::InvalidOpCode;
  7173. IR::Instr *instr;
  7174. bool swapCmpOpnds = false;
  7175. bool addJP = false;
  7176. IR::LabelInstr *labelNaN = nullptr;
  7177. // Generate float compare that behave correctly for NaN's.
  7178. // These branch on unordered:
  7179. // JB
  7180. // JBE
  7181. // JE
  7182. // These don't branch on unordered:
  7183. // JA
  7184. // JAE
  7185. // JNE
  7186. // Unfortunately, only JA and JAE do what we'd like....
  7187. Func * func = instrBranch->m_func;
  7188. IR::Opnd *src1 = instrBranch->UnlinkSrc1();
  7189. IR::Opnd *src2 = instrBranch->UnlinkSrc2();
  7190. Assert(src1->GetType() == src2->GetType());
  7191. switch (instrBranch->m_opcode)
  7192. {
  7193. case Js::OpCode::BrSrEq_A:
  7194. case Js::OpCode::BrEq_A:
  7195. case Js::OpCode::BrSrNotNeq_A:
  7196. case Js::OpCode::BrNotNeq_A:
  7197. cmpOpcode = src1->IsFloat64() ? Js::OpCode::UCOMISD : Js::OpCode::UCOMISS;
  7198. brOpcode = Js::OpCode::JEQ;
  7199. if (!ignoreNan)
  7200. {
  7201. // Don't jump on NaN's
  7202. labelNaN = instrBranch->GetOrCreateContinueLabel();
  7203. addJP = true;
  7204. }
  7205. break;
  7206. case Js::OpCode::BrNeq_A:
  7207. case Js::OpCode::BrSrNeq_A:
  7208. case Js::OpCode::BrSrNotEq_A:
  7209. case Js::OpCode::BrNotEq_A:
  7210. cmpOpcode = src1->IsFloat64() ? Js::OpCode::UCOMISD : Js::OpCode::UCOMISS;
  7211. brOpcode = Js::OpCode::JNE;
  7212. if (!ignoreNan)
  7213. {
  7214. // Jump on NaN's
  7215. labelNaN = instrBranch->GetTarget();
  7216. addJP = true;
  7217. }
  7218. break;
  7219. case Js::OpCode::BrLe_A:
  7220. swapCmpOpnds = true;
  7221. brOpcode = Js::OpCode::JAE;
  7222. break;
  7223. case Js::OpCode::BrLt_A:
  7224. swapCmpOpnds = true;
  7225. brOpcode = Js::OpCode::JA;
  7226. break;
  7227. case Js::OpCode::BrGe_A:
  7228. brOpcode = Js::OpCode::JAE;
  7229. break;
  7230. case Js::OpCode::BrGt_A:
  7231. brOpcode = Js::OpCode::JA;
  7232. break;
  7233. case Js::OpCode::BrNotLe_A:
  7234. swapCmpOpnds = true;
  7235. brOpcode = Js::OpCode::JB;
  7236. break;
  7237. case Js::OpCode::BrNotLt_A:
  7238. swapCmpOpnds = true;
  7239. brOpcode = Js::OpCode::JBE;
  7240. break;
  7241. case Js::OpCode::BrNotGe_A:
  7242. brOpcode = Js::OpCode::JB;
  7243. break;
  7244. case Js::OpCode::BrNotGt_A:
  7245. brOpcode = Js::OpCode::JBE;
  7246. break;
  7247. default:
  7248. Assume(UNREACHED);
  7249. }
  7250. // if we haven't set cmpOpcode, then we are using COMISD/COMISS
  7251. if (cmpOpcode == Js::OpCode::InvalidOpCode)
  7252. {
  7253. cmpOpcode = src1->IsFloat64() ? Js::OpCode::COMISD : Js::OpCode::COMISS;
  7254. }
  7255. if (swapCmpOpnds)
  7256. {
  7257. IR::Opnd *tmp = src1;
  7258. src1 = src2;
  7259. src2 = tmp;
  7260. }
  7261. // VC generates UCOMISD for BrEq/BrNeq, and COMISD for all others, accordingly to IEEE 754.
  7262. // We'll do the same.
  7263. // COMISD / UCOMISD src1, src2
  7264. IR::Instr *instrCmp = IR::Instr::New(cmpOpcode, func);
  7265. instrCmp->SetSrc1(src1);
  7266. instrCmp->SetSrc2(src2);
  7267. instrBranch->InsertBefore(instrCmp);
  7268. Legalize(instrCmp);
  7269. if (addJP)
  7270. {
  7271. // JP $LabelNaN
  7272. instr = IR::BranchInstr::New(Js::OpCode::JP, labelNaN, func);
  7273. instrBranch->InsertBefore(instr);
  7274. }
  7275. // Jcc $L
  7276. instr = IR::BranchInstr::New(brOpcode, instrBranch->GetTarget(), func);
  7277. instrBranch->InsertBefore(instr);
  7278. instrBranch->Remove();
  7279. return instr->AsBranchInstr();
  7280. }
  7281. void LowererMD::HelperCallForAsmMathBuiltin(IR::Instr* instr, IR::JnHelperMethod helperMethodFloat, IR::JnHelperMethod helperMethodDouble)
  7282. {
  7283. Assert(instr->m_opcode == Js::OpCode::InlineMathFloor || instr->m_opcode == Js::OpCode::InlineMathCeil);
  7284. AssertMsg(instr->GetDst()->IsFloat(), "dst must be float.");
  7285. Assert(instr->GetDst()->GetType() == instr->GetSrc1()->GetType());
  7286. Assert(!instr->GetSrc2());
  7287. IR::Opnd * argOpnd = instr->UnlinkSrc1();
  7288. IR::JnHelperMethod helperMethod;
  7289. if (argOpnd->IsFloat32())
  7290. {
  7291. helperMethod = helperMethodFloat;
  7292. LoadFloatHelperArgument(instr, argOpnd);
  7293. }
  7294. else
  7295. {
  7296. helperMethod = helperMethodDouble;
  7297. LoadDoubleHelperArgument(instr, argOpnd);
  7298. }
  7299. ChangeToHelperCall(instr, helperMethod);
  7300. }
  7301. void LowererMD::GenerateFastInlineBuiltInCall(IR::Instr* instr, IR::JnHelperMethod helperMethod)
  7302. {
  7303. switch (instr->m_opcode)
  7304. {
  7305. case Js::OpCode::InlineMathSqrt:
  7306. // Sqrt maps directly to the SSE2 instruction.
  7307. // src and dst should already be XMM registers, all we need is just change the opcode.
  7308. Assert(helperMethod == (IR::JnHelperMethod)0);
  7309. Assert(instr->GetSrc2() == nullptr);
  7310. instr->m_opcode = instr->GetSrc1()->IsFloat64() ? Js::OpCode::SQRTSD : Js::OpCode::SQRTSS;
  7311. break;
  7312. case Js::OpCode::InlineMathAbs:
  7313. Assert(helperMethod == (IR::JnHelperMethod)0);
  7314. return GenerateFastInlineBuiltInMathAbs(instr);
  7315. case Js::OpCode::InlineMathAcos:
  7316. case Js::OpCode::InlineMathAsin:
  7317. case Js::OpCode::InlineMathAtan:
  7318. case Js::OpCode::InlineMathAtan2:
  7319. case Js::OpCode::InlineMathCos:
  7320. case Js::OpCode::InlineMathExp:
  7321. case Js::OpCode::InlineMathLog:
  7322. case Js::OpCode::InlineMathPow:
  7323. case Js::OpCode::Expo_A: //** operator reuses InlineMathPow fastpath
  7324. case Js::OpCode::InlineMathSin:
  7325. case Js::OpCode::InlineMathTan:
  7326. {
  7327. AssertMsg(instr->GetDst()->IsFloat(), "dst must be float.");
  7328. AssertMsg(instr->GetSrc1()->IsFloat(), "src1 must be float.");
  7329. AssertMsg(!instr->GetSrc2() || instr->GetSrc2()->IsFloat(), "src2 must be float.");
  7330. // Before:
  7331. // dst = <Built-in call> src1, src2
  7332. // After:
  7333. // I386:
  7334. // XMM0 = MOVSD src1
  7335. // CALL helperMethod
  7336. // dst = MOVSD call->dst
  7337. // AMD64:
  7338. // XMM0 = MOVSD src1
  7339. // RAX = MOV helperMethod
  7340. // CALL RAX
  7341. // dst = MOVSD call->dst
  7342. // Src1
  7343. IR::Instr* argOut = IR::Instr::New(Js::OpCode::MOVSD, this->m_func);
  7344. IR::RegOpnd* dst1 = IR::RegOpnd::New(nullptr, (RegNum)FIRST_FLOAT_ARG_REG, TyMachDouble, this->m_func);
  7345. dst1->m_isCallArg = true; // This is to make sure that lifetime of opnd is virtually extended until next CALL instr.
  7346. argOut->SetDst(dst1);
  7347. argOut->SetSrc1(instr->UnlinkSrc1());
  7348. instr->InsertBefore(argOut);
  7349. // Src2
  7350. if (instr->GetSrc2() != nullptr)
  7351. {
  7352. IR::Instr* argOut2 = IR::Instr::New(Js::OpCode::MOVSD, this->m_func);
  7353. IR::RegOpnd* dst2 = IR::RegOpnd::New(nullptr, (RegNum)(FIRST_FLOAT_ARG_REG + 1), TyMachDouble, this->m_func);
  7354. dst2->m_isCallArg = true; // This is to make sure that lifetime of opnd is virtually extended until next CALL instr.
  7355. argOut2->SetDst(dst2);
  7356. argOut2->SetSrc1(instr->UnlinkSrc2());
  7357. instr->InsertBefore(argOut2);
  7358. }
  7359. // Call CRT.
  7360. IR::RegOpnd* floatCallDst = IR::RegOpnd::New(nullptr, (RegNum)(FIRST_FLOAT_REG), TyMachDouble, this->m_func); // Dst in XMM0.
  7361. #ifdef _M_IX86
  7362. IR::Instr* floatCall = IR::Instr::New(Js::OpCode::CALL, floatCallDst, this->m_func);
  7363. floatCall->SetSrc1(IR::HelperCallOpnd::New(helperMethod, this->m_func));
  7364. instr->InsertBefore(floatCall);
  7365. #else
  7366. // s1 = MOV helperAddr
  7367. IR::RegOpnd* s1 = IR::RegOpnd::New(TyMachReg, this->m_func);
  7368. IR::AddrOpnd* helperAddr = IR::AddrOpnd::New((Js::Var)IR::GetMethodOriginalAddress(helperMethod), IR::AddrOpndKind::AddrOpndKindDynamicMisc, this->m_func);
  7369. IR::Instr* mov = IR::Instr::New(Js::OpCode::MOV, s1, helperAddr, this->m_func);
  7370. instr->InsertBefore(mov);
  7371. // dst(XMM0) = CALL s1
  7372. IR::Instr *floatCall = IR::Instr::New(Js::OpCode::CALL, floatCallDst, s1, this->m_func);
  7373. instr->InsertBefore(floatCall);
  7374. #endif
  7375. // Save the result.
  7376. instr->m_opcode = Js::OpCode::MOVSD;
  7377. instr->SetSrc1(floatCall->GetDst());
  7378. break;
  7379. }
  7380. case Js::OpCode::InlineMathFloor:
  7381. case Js::OpCode::InlineMathCeil:
  7382. case Js::OpCode::InlineMathRound:
  7383. {
  7384. Assert(AutoSystemInfo::Data.SSE4_1Available());
  7385. Assert(instr->GetDst()->IsInt32() || instr->GetDst()->IsFloat());
  7386. // MOVSD roundedFloat, src
  7387. //
  7388. // if(round)
  7389. // {
  7390. // /* N.B.: the following CMPs are lowered to COMISDs, whose results can only be >, <, or =.
  7391. // In fact, only ">" can be used if NaN has not been handled.
  7392. // */
  7393. // CMP 0.5, roundedFloat
  7394. // JA $ltHalf
  7395. // CMP TwoToFraction, roundedFloat
  7396. // JA $addHalfToRoundSrcLabel
  7397. // J $skipRoundSd (NaN is also handled here)
  7398. // $ltHalf:
  7399. // CMP roundedFloat, -0.5
  7400. // JL $ltNegHalf
  7401. // if (shouldCheckNegZero) {
  7402. // CMP roundedFloat, 0
  7403. // JA $setZero
  7404. // $negZeroTest [Helper]:
  7405. // JB $bailoutLabel
  7406. // isNegZero(src)
  7407. // JE $bailoutLabel
  7408. // J $skipRoundSd
  7409. // } // else: setZero
  7410. // $setZero:
  7411. // MOV roundedFloat, 0
  7412. // J $skipRoundSd
  7413. // $ltNegHalf:
  7414. // CMP roundedFloat, NegTwoToFraction
  7415. // JA $addHalfToRoundSrc
  7416. // J $skipRoundSd
  7417. // $addHalfToRoundSrc:
  7418. // ADDSD roundedFloat, 0.5
  7419. // $skipAddHalf:
  7420. // }
  7421. //
  7422. // if(isNotCeil)
  7423. // {
  7424. // CMP roundedFloat, 0
  7425. // JGE $skipRoundSd
  7426. // }
  7427. // ROUNDSD roundedFloat, roundedFloat, round_mode
  7428. //
  7429. // $skipRoundSd:
  7430. // if(isNotCeil)
  7431. // MOVSD checkNegZeroOpnd, roundedFloat
  7432. // else if (ceil)
  7433. // MOVSD checkNegZeroOpnd, src
  7434. //
  7435. // CMP checkNegZeroOpnd, 0
  7436. // JNE $convertToInt
  7437. //
  7438. // if(instr->ShouldCheckForNegativeZero())
  7439. // {
  7440. // isNegZero CALL IsNegZero(checkNegZeroOpnd)
  7441. // CMP isNegZero, 0
  7442. // JNE $bailoutLabel
  7443. // }
  7444. //
  7445. // $convertToInt:
  7446. // CVT(T)SD2SI dst, roundedFloat //CVTTSD2SI for floor/round and CVTSD2SI for ceil
  7447. // CMP dst 0x80000000
  7448. // JNE $fallthrough
  7449. //
  7450. // if(!sharedBailout)
  7451. // {
  7452. // $bailoutLabel:
  7453. // }
  7454. // GenerateBailout(instr)
  7455. //
  7456. // $fallthrough:
  7457. bool isNotCeil = instr->m_opcode != Js::OpCode::InlineMathCeil;
  7458. // MOVSD roundedFloat, src
  7459. IR::Opnd * src = instr->UnlinkSrc1();
  7460. IR::RegOpnd* roundedFloat = IR::RegOpnd::New(src->GetType(), this->m_func);
  7461. IR::Instr* argOut = IR::Instr::New(LowererMDArch::GetAssignOp(src->GetType()), roundedFloat, src, this->m_func);
  7462. instr->InsertBefore(argOut);
  7463. bool negZeroCheckDone = false;
  7464. IR::LabelInstr * bailoutLabel = nullptr;
  7465. bool sharedBailout = false;
  7466. if (instr->GetDst()->IsInt32())
  7467. {
  7468. sharedBailout = (instr->GetBailOutInfo()->bailOutInstr != instr) ? true : false;
  7469. if (sharedBailout)
  7470. {
  7471. bailoutLabel = instr->GetBailOutInfo()->bailOutInstr->AsLabelInstr();
  7472. }
  7473. else
  7474. {
  7475. bailoutLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, /*helperLabel*/true);
  7476. }
  7477. }
  7478. IR::Opnd * zero;
  7479. if (src->IsFloat64())
  7480. {
  7481. zero = IR::MemRefOpnd::New((double*)&(Js::JavascriptNumber::k_Zero), TyFloat64, this->m_func, IR::AddrOpndKindDynamicDoubleRef);
  7482. }
  7483. else
  7484. {
  7485. Assert(src->IsFloat32());
  7486. zero = IR::MemRefOpnd::New((float*)&Js::JavascriptNumber::k_Float32Zero, TyFloat32, this->m_func, IR::AddrOpndKindDynamicFloatRef);
  7487. }
  7488. IR::LabelInstr * skipRoundSd = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  7489. if(instr->m_opcode == Js::OpCode::InlineMathRound)
  7490. {
  7491. IR::LabelInstr * addHalfToRoundSrcLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  7492. IR::LabelInstr * ltHalf = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  7493. IR::LabelInstr * setZero = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  7494. IR::LabelInstr * ltNegHalf = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  7495. IR::Opnd * pointFive;
  7496. IR::Opnd * negPointFive;
  7497. if (src->IsFloat64())
  7498. {
  7499. pointFive = IR::MemRefOpnd::New((double*)&(Js::JavascriptNumber::k_PointFive), TyFloat64, this->m_func, IR::AddrOpndKindDynamicDoubleRef);
  7500. negPointFive = IR::MemRefOpnd::New((double*)&Js::JavascriptNumber::k_NegPointFive, TyFloat64, this->m_func, IR::AddrOpndKindDynamicDoubleRef);
  7501. }
  7502. else
  7503. {
  7504. Assert(src->IsFloat32());
  7505. pointFive = IR::MemRefOpnd::New((float*)&Js::JavascriptNumber::k_Float32PointFive, TyFloat32, this->m_func, IR::AddrOpndKindDynamicFloatRef);
  7506. negPointFive = IR::MemRefOpnd::New((float*)&Js::JavascriptNumber::k_Float32NegPointFive, TyFloat32, this->m_func, IR::AddrOpndKindDynamicFloatRef);
  7507. }
  7508. // CMP 0.5, roundedFloat
  7509. // JA $ltHalf
  7510. this->m_lowerer->InsertCompareBranch(pointFive, roundedFloat, Js::OpCode::BrGt_A, ltHalf, instr);
  7511. if (instr->GetDst()->IsInt32())
  7512. {
  7513. // if we are specializing dst to int, we will bailout on overflow so don't need upperbound check
  7514. // Also, we will bailout on NaN, so it doesn't need special handling either
  7515. // J $addHalfToRoundSrcLabel
  7516. this->m_lowerer->InsertBranch(Js::OpCode::Br, addHalfToRoundSrcLabel, instr);
  7517. }
  7518. else
  7519. {
  7520. IR::Opnd * twoToFraction;
  7521. if (src->IsFloat64())
  7522. {
  7523. twoToFraction = IR::MemRefOpnd::New((double*)&Js::JavascriptNumber::k_TwoToFraction, TyFloat64, this->m_func, IR::AddrOpndKindDynamicDoubleRef);
  7524. }
  7525. else
  7526. {
  7527. Assert(src->IsFloat32());
  7528. twoToFraction = IR::MemRefOpnd::New((float*)&Js::JavascriptNumber::k_Float32TwoToFraction, TyFloat32, this->m_func, IR::AddrOpndKindDynamicFloatRef);
  7529. }
  7530. // CMP 2^fraction, roundedFloat
  7531. // JA $addHalfToRoundSrcLabel
  7532. this->m_lowerer->InsertCompareBranch(twoToFraction, roundedFloat, Js::OpCode::BrGt_A, addHalfToRoundSrcLabel, instr);
  7533. // J $skipRoundSd (NaN also handled here)
  7534. this->m_lowerer->InsertBranch(Js::OpCode::Br, skipRoundSd, instr);
  7535. }
  7536. // $ltHalf:
  7537. instr->InsertBefore(ltHalf);
  7538. // CMP roundedFloat, -0.5
  7539. // JL $ltNegHalf
  7540. this->m_lowerer->InsertCompareBranch(roundedFloat, negPointFive, Js::OpCode::BrLt_A, ltNegHalf, instr);
  7541. if (instr->ShouldCheckForNegativeZero())
  7542. {
  7543. // CMP roundedFloat, 0
  7544. // JA $setZero
  7545. this->m_lowerer->InsertCompareBranch(roundedFloat, zero, Js::OpCode::BrGt_A, setZero, instr);
  7546. // $negZeroTest [helper]
  7547. m_lowerer->InsertLabel(true, instr);
  7548. // JB $bailoutLabel
  7549. this->m_lowerer->InsertBranch(Js::OpCode::JB, bailoutLabel, instr);
  7550. IR::Opnd* isNegZero = IsOpndNegZero(src, instr);
  7551. // if isNegZero(src) J $bailoutLabel
  7552. this->m_lowerer->InsertTestBranch(isNegZero, isNegZero, Js::OpCode::BrNeq_A, bailoutLabel, instr);
  7553. // else J $skipRoundSd
  7554. this->m_lowerer->InsertBranch(Js::OpCode::Br, skipRoundSd, instr);
  7555. negZeroCheckDone = true;
  7556. }
  7557. // $setZero:
  7558. instr->InsertBefore(setZero);
  7559. // MOVSD_ZERO roundedFloat
  7560. LoadFloatZero(roundedFloat, instr);
  7561. // J $skipRoundSd
  7562. this->m_lowerer->InsertBranch(Js::OpCode::Br, skipRoundSd, instr);
  7563. // $ltNegHalf:
  7564. instr->InsertBefore(ltNegHalf);
  7565. if (!instr->GetDst()->IsInt32())
  7566. {
  7567. // if we are specializing dst to int, we will bailout on overflow so don't need lowerbound check
  7568. IR::Opnd * negTwoToFraction;
  7569. if (src->IsFloat64())
  7570. {
  7571. negTwoToFraction = IR::MemRefOpnd::New((double*)&Js::JavascriptNumber::k_NegTwoToFraction, TyFloat64, this->m_func, IR::AddrOpndKindDynamicDoubleRef);
  7572. }
  7573. else
  7574. {
  7575. Assert(src->IsFloat32());
  7576. negTwoToFraction = IR::MemRefOpnd::New((float*)&Js::JavascriptNumber::k_Float32NegTwoToFraction, TyFloat32, this->m_func, IR::AddrOpndKindDynamicFloatRef);
  7577. }
  7578. // CMP roundedFloat, negTwoToFraction
  7579. // JA $addHalfToRoundSrcLabel
  7580. this->m_lowerer->InsertCompareBranch(roundedFloat, negTwoToFraction, Js::OpCode::BrGt_A, addHalfToRoundSrcLabel, instr);
  7581. // J $skipRoundSd
  7582. this->m_lowerer->InsertBranch(Js::OpCode::Br, skipRoundSd, instr);
  7583. }
  7584. if (src->IsFloat64())
  7585. {
  7586. pointFive = IR::MemRefOpnd::New((double*)&(Js::JavascriptNumber::k_PointFive), TyFloat64, this->m_func, IR::AddrOpndKindDynamicDoubleRef);
  7587. }
  7588. else
  7589. {
  7590. Assert(src->IsFloat32());
  7591. pointFive = IR::MemRefOpnd::New((float*)&Js::JavascriptNumber::k_Float32PointFive, TyFloat32, this->m_func, IR::AddrOpndKindDynamicFloatRef);
  7592. }
  7593. // $addHalfToRoundSrcLabel
  7594. instr->InsertBefore(addHalfToRoundSrcLabel);
  7595. // ADDSD roundedFloat, 0.5
  7596. IR::Instr * addInstr = IR::Instr::New(src->IsFloat64() ? Js::OpCode::ADDSD : Js::OpCode::ADDSS, roundedFloat, roundedFloat, pointFive, this->m_func);
  7597. instr->InsertBefore(addInstr);
  7598. Legalize(addInstr);
  7599. }
  7600. if (instr->m_opcode == Js::OpCode::InlineMathFloor && instr->GetDst()->IsInt32())
  7601. {
  7602. this->m_lowerer->InsertCompareBranch(roundedFloat, zero, Js::OpCode::BrGe_A, skipRoundSd, instr);
  7603. }
  7604. // ROUNDSD srcCopy, srcCopy, round_mode
  7605. IR::Opnd * roundMode;
  7606. if(isNotCeil)
  7607. {
  7608. roundMode = IR::IntConstOpnd::New(0x01, TyInt32, this->m_func);
  7609. }
  7610. else if (instr->GetDst()->IsInt32() || instr->m_opcode != Js::OpCode::InlineMathFloor)
  7611. {
  7612. roundMode = IR::IntConstOpnd::New(0x02, TyInt32, this->m_func);
  7613. }
  7614. else
  7615. {
  7616. roundMode = IR::IntConstOpnd::New(0x03, TyInt32, this->m_func);
  7617. }
  7618. IR::Instr* roundInstr = IR::Instr::New(src->IsFloat64() ? Js::OpCode::ROUNDSD : Js::OpCode::ROUNDSS, roundedFloat, roundedFloat, roundMode, this->m_func);
  7619. instr->InsertBefore(roundInstr);
  7620. if (instr->m_opcode == Js::OpCode::InlineMathRound)
  7621. {
  7622. instr->InsertBefore(skipRoundSd);
  7623. }
  7624. if (instr->GetDst()->IsInt32())
  7625. {
  7626. if (instr->m_opcode == Js::OpCode::InlineMathFloor)
  7627. {
  7628. instr->InsertBefore(skipRoundSd);
  7629. }
  7630. //negZero bailout
  7631. if(instr->ShouldCheckForNegativeZero() && !negZeroCheckDone)
  7632. {
  7633. IR::LabelInstr * convertToInt = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  7634. IR::Opnd * checkNegZeroOpnd;
  7635. if(isNotCeil)
  7636. {
  7637. checkNegZeroOpnd = src;
  7638. }
  7639. else
  7640. {
  7641. checkNegZeroOpnd = roundedFloat;
  7642. }
  7643. this->m_lowerer->InsertCompareBranch(checkNegZeroOpnd, zero, Js::OpCode::BrNeq_A, convertToInt, instr);
  7644. IR::Opnd* isNegZero = IsOpndNegZero(checkNegZeroOpnd, instr);
  7645. this->m_lowerer->InsertCompareBranch(isNegZero, IR::IntConstOpnd::New(0x00000000, IRType::TyInt32, this->m_func), Js::OpCode::BrNeq_A, bailoutLabel, instr);
  7646. instr->InsertBefore(convertToInt);
  7647. }
  7648. IR::Opnd * originalDst = instr->UnlinkDst();
  7649. // CVT(T)SD2SI dst, srcCopy
  7650. IR::Instr* convertToIntInstr;
  7651. if (isNotCeil)
  7652. {
  7653. convertToIntInstr = IR::Instr::New(src->IsFloat64() ? Js::OpCode::CVTTSD2SI : Js::OpCode::CVTTSS2SI, originalDst, roundedFloat, this->m_func);
  7654. }
  7655. else
  7656. {
  7657. convertToIntInstr = IR::Instr::New(src->IsFloat64() ? Js::OpCode::CVTSD2SI : Js::OpCode::CVTSS2SI, originalDst, roundedFloat, this->m_func);
  7658. }
  7659. instr->InsertBefore(convertToIntInstr);
  7660. IR::LabelInstr * fallthrough = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  7661. IR::Opnd * intOverflowValue = IR::IntConstOpnd::New(INT32_MIN, IRType::TyInt32, this->m_func, true);
  7662. this->m_lowerer->InsertCompareBranch(originalDst, intOverflowValue, Js::OpCode::BrNeq_A, fallthrough, instr);
  7663. instr->InsertAfter(fallthrough);
  7664. if (!sharedBailout)
  7665. {
  7666. instr->InsertBefore(bailoutLabel);
  7667. }
  7668. this->m_lowerer->GenerateBailOut(instr);
  7669. }
  7670. else
  7671. {
  7672. IR::Opnd * originalDst = instr->UnlinkDst();
  7673. Assert(originalDst->IsFloat());
  7674. Assert(originalDst->GetType() == roundedFloat->GetType());
  7675. IR::Instr * movInstr = IR::Instr::New(originalDst->IsFloat64() ? Js::OpCode::MOVSD : Js::OpCode::MOVSS, originalDst, roundedFloat, this->m_func);
  7676. instr->InsertBefore(movInstr);
  7677. instr->Remove();
  7678. }
  7679. break;
  7680. }
  7681. case Js::OpCode::InlineMathMin:
  7682. case Js::OpCode::InlineMathMax:
  7683. {
  7684. IR::Opnd* src1 = instr->GetSrc1();
  7685. IR::Opnd* src2 = instr->GetSrc2();
  7686. IR::Opnd* dst = instr->GetDst();
  7687. IR::LabelInstr* doneLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  7688. IR::LabelInstr* labelNaNHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  7689. IR::LabelInstr* labelNegZeroAndNaNCheckHelper = IR::LabelInstr::New(Js::OpCode::Label, this->m_func, true);
  7690. IR::Instr* branchInstr;
  7691. bool min = instr->m_opcode == Js::OpCode::InlineMathMin ? true : false;
  7692. // CMP src1, src2
  7693. if(dst->IsInt32())
  7694. {
  7695. //MOV dst, src2;
  7696. Assert(!dst->IsEqual(src2));
  7697. this->m_lowerer->InsertMove(dst, src2, instr);
  7698. if(min)
  7699. {
  7700. // JLT $continueLabel
  7701. branchInstr = IR::BranchInstr::New(Js::OpCode::BrGt_I4, doneLabel, src1, src2, instr->m_func);
  7702. instr->InsertBefore(branchInstr);
  7703. LowererMDArch::EmitInt4Instr(branchInstr);
  7704. }
  7705. else
  7706. {
  7707. // JGT $continueLabel
  7708. branchInstr = IR::BranchInstr::New(Js::OpCode::BrLt_I4, doneLabel, src1, src2, instr->m_func);
  7709. instr->InsertBefore(branchInstr);
  7710. LowererMDArch::EmitInt4Instr(branchInstr);
  7711. }
  7712. // MOV dst, src1
  7713. this->m_lowerer->InsertMove(dst, src1, instr);
  7714. }
  7715. else if(dst->IsFloat64())
  7716. {
  7717. // COMISD src1 (src2), src2 (src1)
  7718. // JA $doneLabel
  7719. // JEQ $labelNegZeroAndNaNCheckHelper
  7720. // MOVSD dst, src2
  7721. // JMP $doneLabel
  7722. //
  7723. // $labelNegZeroAndNaNCheckHelper
  7724. // JP $labelNaNHelper
  7725. // if(min)
  7726. // {
  7727. // if(src2 == -0.0)
  7728. // MOVSD dst, src2
  7729. // }
  7730. // else
  7731. // {
  7732. // if(src1 == -0.0)
  7733. // MOVSD dst, src2
  7734. // }
  7735. // JMP $doneLabel
  7736. //
  7737. // $labelNaNHelper
  7738. // MOVSD dst, NaN
  7739. //
  7740. // $doneLabel
  7741. //MOVSD dst, src1;
  7742. Assert(!dst->IsEqual(src1));
  7743. this->m_lowerer->InsertMove(dst, src1, instr);
  7744. if(min)
  7745. {
  7746. 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
  7747. }
  7748. else
  7749. {
  7750. this->m_lowerer->InsertCompareBranch(src1, src2, Js::OpCode::BrGt_A, doneLabel, instr);
  7751. }
  7752. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JEQ, labelNegZeroAndNaNCheckHelper, instr->m_func));
  7753. this->m_lowerer->InsertMove(dst, src2, instr);
  7754. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JMP, doneLabel, instr->m_func));
  7755. instr->InsertBefore(labelNegZeroAndNaNCheckHelper);
  7756. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JP, labelNaNHelper, instr->m_func));
  7757. IR::Opnd* isNegZero;
  7758. if(min)
  7759. {
  7760. isNegZero = IsOpndNegZero(src2, instr);
  7761. }
  7762. else
  7763. {
  7764. isNegZero = IsOpndNegZero(src1, instr);
  7765. }
  7766. this->m_lowerer->InsertCompareBranch(isNegZero, IR::IntConstOpnd::New(0x00000000, IRType::TyInt32, this->m_func), Js::OpCode::BrEq_A, doneLabel, instr);
  7767. this->m_lowerer->InsertMove(dst, src2, instr);
  7768. instr->InsertBefore(IR::BranchInstr::New(Js::OpCode::JMP, doneLabel, instr->m_func));
  7769. instr->InsertBefore(labelNaNHelper);
  7770. IR::Opnd * opndNaN = IR::MemRefOpnd::New((double*)&(Js::JavascriptNumber::k_Nan), IRType::TyFloat64, this->m_func);
  7771. this->m_lowerer->InsertMove(dst, opndNaN, instr);
  7772. }
  7773. instr->InsertBefore(doneLabel);
  7774. instr->Remove();
  7775. break;
  7776. }
  7777. default:
  7778. AssertMsg(FALSE, "Unknown inline built-in opcode");
  7779. break;
  7780. }
  7781. }
  7782. IR::Opnd* LowererMD::IsOpndNegZero(IR::Opnd* opnd, IR::Instr* instr)
  7783. {
  7784. IR::Opnd * isNegZero = IR::RegOpnd::New(TyInt32, this->m_func);
  7785. LoadDoubleHelperArgument(instr, opnd);
  7786. IR::Instr * helperCallInstr = IR::Instr::New(Js::OpCode::CALL, isNegZero, this->m_func);
  7787. instr->InsertBefore(helperCallInstr);
  7788. this->ChangeToHelperCall(helperCallInstr, IR::HelperIsNegZero);
  7789. return isNegZero;
  7790. }
  7791. void LowererMD::GenerateFastInlineBuiltInMathAbs(IR::Instr* inlineInstr)
  7792. {
  7793. IR::Opnd* src = inlineInstr->GetSrc1();
  7794. IR::Opnd* dst = inlineInstr->UnlinkDst();
  7795. Assert(src);
  7796. IR::Instr* tmpInstr;
  7797. IR::Instr* nextInstr = IR::LabelInstr::New(Js::OpCode::Label, m_func);
  7798. IR::Instr* continueInstr = m_lowerer->LowerBailOnIntMin(inlineInstr);
  7799. continueInstr->InsertAfter(nextInstr);
  7800. IRType srcType = src->GetType();
  7801. if (srcType == IRType::TyInt32)
  7802. {
  7803. // Note: if execution gets so far, we always get (untagged) int32 here.
  7804. // Since -x = ~x + 1, abs(x) = x, abs(-x) = -x, sign-extend(x) = 0, sign_extend(-x) = -1, where 0 <= x.
  7805. // Then: abs(x) = sign-extend(x) XOR x - sign-extend(x)
  7806. // Expected input (otherwise bailout):
  7807. // - src1 is (untagged) int, not equal to int_min (abs(int_min) would produce overflow, as there's no corresponding positive int).
  7808. // MOV EAX, src
  7809. IR::RegOpnd *regEAX = IR::RegOpnd::New(TyInt32, this->m_func);
  7810. regEAX->SetReg(LowererMDArch::GetRegIMulDestLower());
  7811. tmpInstr = IR::Instr::New(Js::OpCode::MOV, regEAX, src, this->m_func);
  7812. nextInstr->InsertBefore(tmpInstr);
  7813. IR::RegOpnd *regEDX = IR::RegOpnd::New(TyInt32, this->m_func);
  7814. regEDX->SetReg(LowererMDArch::GetRegIMulHighDestLower());
  7815. // CDQ (sign-extend EAX into EDX, producing 64bit EDX:EAX value)
  7816. // Note: put EDX on dst to give of def to the EDX lifetime
  7817. tmpInstr = IR::Instr::New(Js::OpCode::CDQ, regEDX, this->m_func);
  7818. nextInstr->InsertBefore(tmpInstr);
  7819. // XOR EAX, EDX
  7820. tmpInstr = IR::Instr::New(Js::OpCode::XOR, regEAX, regEAX, regEDX, this->m_func);
  7821. nextInstr->InsertBefore(tmpInstr);
  7822. // SUB EAX, EDX
  7823. tmpInstr = IR::Instr::New(Js::OpCode::SUB, regEAX, regEAX, regEDX, this->m_func);
  7824. nextInstr->InsertBefore(tmpInstr);
  7825. // MOV dst, EAX
  7826. tmpInstr = IR::Instr::New(Js::OpCode::MOV, dst, regEAX, this->m_func);
  7827. nextInstr->InsertBefore(tmpInstr);
  7828. }
  7829. else if (srcType == IRType::TyFloat64)
  7830. {
  7831. if (!dst->IsRegOpnd())
  7832. {
  7833. // MOVSD tempRegOpnd, src
  7834. IR::RegOpnd* tempRegOpnd = IR::RegOpnd::New(nullptr, TyMachDouble, this->m_func);
  7835. tempRegOpnd->m_isCallArg = true; // This is to make sure that lifetime of opnd is virtually extended until next CALL instr.
  7836. tmpInstr = IR::Instr::New(Js::OpCode::MOVSD, tempRegOpnd, src, this->m_func);
  7837. nextInstr->InsertBefore(tmpInstr);
  7838. // This saves the result in the same register.
  7839. this->GenerateFloatAbs(static_cast<IR::RegOpnd*>(tempRegOpnd), nextInstr);
  7840. // MOVSD dst, tempRegOpnd
  7841. tmpInstr = IR::Instr::New(Js::OpCode::MOVSD, dst, tempRegOpnd, this->m_func);
  7842. nextInstr->InsertBefore(tmpInstr);
  7843. }
  7844. else
  7845. {
  7846. // MOVSD dst, src
  7847. tmpInstr = IR::Instr::New(Js::OpCode::MOVSD, dst, src, this->m_func);
  7848. nextInstr->InsertBefore(tmpInstr);
  7849. // This saves the result in the same register.
  7850. this->GenerateFloatAbs(static_cast<IR::RegOpnd*>(dst), nextInstr);
  7851. }
  7852. }
  7853. else if (srcType == IRType::TyFloat32)
  7854. {
  7855. if (!dst->IsRegOpnd())
  7856. {
  7857. // MOVSS tempRegOpnd, src
  7858. IR::RegOpnd* tempRegOpnd = IR::RegOpnd::New(nullptr, TyFloat32, this->m_func);
  7859. tempRegOpnd->m_isCallArg = true; // This is to make sure that lifetime of opnd is virtually extended until next CALL instr.
  7860. tmpInstr = IR::Instr::New(Js::OpCode::MOVSS, tempRegOpnd, src, this->m_func);
  7861. nextInstr->InsertBefore(tmpInstr);
  7862. // This saves the result in the same register.
  7863. this->GenerateFloatAbs(static_cast<IR::RegOpnd*>(tempRegOpnd), nextInstr);
  7864. // MOVSS dst, tempRegOpnd
  7865. tmpInstr = IR::Instr::New(Js::OpCode::MOVSS, dst, tempRegOpnd, this->m_func);
  7866. nextInstr->InsertBefore(tmpInstr);
  7867. }
  7868. else
  7869. {
  7870. // MOVSS dst, src
  7871. tmpInstr = IR::Instr::New(Js::OpCode::MOVSS, dst, src, this->m_func);
  7872. nextInstr->InsertBefore(tmpInstr);
  7873. // This saves the result in the same register.
  7874. this->GenerateFloatAbs(static_cast<IR::RegOpnd*>(dst), nextInstr);
  7875. }
  7876. }
  7877. else
  7878. {
  7879. AssertMsg(FALSE, "GenerateFastInlineBuiltInMathAbs: unexpected type of the src!");
  7880. }
  7881. }
  7882. void
  7883. LowererMD::FinalLower()
  7884. {
  7885. this->lowererMDArch.FinalLower();
  7886. }
  7887. IR::Instr *
  7888. LowererMD::LowerDivI4AndBailOnReminder(IR::Instr * instr, IR::LabelInstr * bailOutLabel)
  7889. {
  7890. // Don't have save the operand for bailout because the lowering of IDIV don't overwrite their values
  7891. // (EDX) = CDQ
  7892. // EAX = numerator
  7893. // (EDX:EAX)= IDIV (EAX), denominator
  7894. // TEST EDX, EDX
  7895. // JNE bailout
  7896. // <Caller insert more checks here>
  7897. // dst = MOV EAX <-- insertBeforeInstr
  7898. Assert(instr);
  7899. Assert(instr->m_opcode == Js::OpCode::Div_I4);
  7900. Assert(!instr->HasBailOutInfo());
  7901. EmitInt4Instr(instr);
  7902. Assert(instr->m_opcode == Js::OpCode::IDIV);
  7903. IR::Instr * prev = instr->m_prev;
  7904. Assert(prev->m_opcode == Js::OpCode::CDQ);
  7905. #ifdef _M_IX86
  7906. Assert(prev->GetDst()->AsRegOpnd()->GetReg() == RegEDX);
  7907. #else
  7908. Assert(prev->GetDst()->AsRegOpnd()->GetReg() == RegRDX);
  7909. #endif
  7910. IR::Opnd * reminderOpnd = prev->GetDst();
  7911. // Insert all check before the assignment to the actual dst.
  7912. IR::Instr * insertBeforeInstr = instr->m_next;
  7913. Assert(insertBeforeInstr->m_opcode == Js::OpCode::MOV);
  7914. #ifdef _M_IX86
  7915. Assert(insertBeforeInstr->GetSrc1()->AsRegOpnd()->GetReg() == RegEAX);
  7916. #else
  7917. Assert(insertBeforeInstr->GetSrc1()->AsRegOpnd()->GetReg() == RegRAX);
  7918. #endif
  7919. // Jump to bailout if the reminder is not 0 (not int result)
  7920. this->m_lowerer->InsertTestBranch(reminderOpnd, reminderOpnd, Js::OpCode::BrNeq_A, bailOutLabel, insertBeforeInstr);
  7921. return insertBeforeInstr;
  7922. }