IR.cpp 138 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880
  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 "RuntimeMathPch.h"
  7. namespace IR
  8. {
  9. void
  10. Instr::Init(Js::OpCode opcode, IRKind kind, Func * func)
  11. {
  12. Assert(!OpCodeAttr::ByteCodeOnly(opcode));
  13. this->m_opcode = opcode;
  14. this->m_kind = kind;
  15. this->m_func = func;
  16. #ifdef BAILOUT_INJECTION
  17. this->bailOutByteCodeLocation = (uint)-1;
  18. #endif
  19. }
  20. uint32
  21. Instr::GetByteCodeOffset() const
  22. {
  23. Assert(m_func->HasByteCodeOffset());
  24. return m_number;
  25. }
  26. void
  27. Instr::SetByteCodeOffset(uint32 offset)
  28. {
  29. Assert(m_func->HasByteCodeOffset());
  30. Assert(m_number == Js::Constants::NoByteCodeOffset);
  31. m_number = offset;
  32. }
  33. void
  34. Instr::SetByteCodeOffset(IR::Instr * instr)
  35. {
  36. SetByteCodeOffset(instr->GetByteCodeOffset());
  37. }
  38. void
  39. Instr::ClearByteCodeOffset()
  40. {
  41. Assert(m_func->HasByteCodeOffset());
  42. m_number = Js::Constants::NoByteCodeOffset;
  43. }
  44. uint32
  45. Instr::GetNumber() const
  46. {
  47. Assert(m_func->HasInstrNumber());
  48. return m_number;
  49. }
  50. void
  51. Instr::SetNumber(uint32 number)
  52. {
  53. Assert(m_func->HasInstrNumber());
  54. m_number = number;
  55. }
  56. bool
  57. Instr::IsPlainInstr() const
  58. {
  59. return this->GetKind() == IR::InstrKindInstr;
  60. }
  61. bool
  62. Instr::DoStackArgsOpt() const
  63. {
  64. return this->usesStackArgumentsObject && m_func->IsStackArgsEnabled();
  65. }
  66. bool
  67. Instr::HasTypeCheckBailOut() const
  68. {
  69. return this->HasBailOutInfo() && IR::IsTypeCheckBailOutKind(this->GetBailOutKind());
  70. }
  71. bool
  72. Instr::HasEquivalentTypeCheckBailOut() const
  73. {
  74. return this->HasBailOutInfo() && IR::IsEquivalentTypeCheckBailOutKind(this->GetBailOutKind());
  75. }
  76. bool
  77. Instr::HasBailOnNoProfile() const
  78. {
  79. return this->HasBailOutInfo() && this->GetBailOutKind() == IR::BailOutOnNoProfile;
  80. }
  81. void
  82. Instr::ChangeEquivalentToMonoTypeCheckBailOut()
  83. {
  84. Assert(this->HasEquivalentTypeCheckBailOut());
  85. this->SetBailOutKind(IR::EquivalentToMonoTypeCheckBailOutKind(this->GetBailOutKind()));
  86. }
  87. intptr_t
  88. Instr::TryOptimizeInstrWithFixedDataProperty(IR::Instr **pInstr, GlobOpt * globopt)
  89. {
  90. IR::Instr *&instr = *pInstr;
  91. Assert(OpCodeAttr::CanLoadFixedFields(instr->m_opcode));
  92. IR::Opnd * src1 = instr->GetSrc1();
  93. Assert(src1 && src1->IsSymOpnd() && src1->AsSymOpnd()->IsPropertySymOpnd());
  94. IR::PropertySymOpnd * propSymOpnd = src1->AsSymOpnd()->AsPropertySymOpnd();
  95. if (propSymOpnd->HasFixedValue() && !propSymOpnd->IsPoly())
  96. {
  97. intptr_t fixedValue = propSymOpnd->GetFieldValueAsFixedData();
  98. Assert(instr->IsProfiledInstr());
  99. ValueType valType = instr->AsProfiledInstr()->u.FldInfo().valueType;
  100. if (fixedValue && ((Js::TaggedInt::Is(fixedValue) && (valType.IsUninitialized() || valType.IsLikelyInt())) || PHASE_ON1(Js::FixDataVarPropsPhase)))
  101. {
  102. // Change Ld[Root]Fld to CheckFixedFld, which doesn't need a dst.
  103. instr->m_opcode = Js::OpCode::CheckFixedFld;
  104. IR::RegOpnd* dataValueDstOpnd = instr->UnlinkDst()->AsRegOpnd();
  105. if (globopt)
  106. {
  107. globopt->GenerateBailAtOperation(&instr, !propSymOpnd->HasEquivalentTypeSet() ? IR::BailOutFailedFixedFieldTypeCheck : IR::BailOutFailedEquivalentFixedFieldTypeCheck);
  108. }
  109. else
  110. {
  111. instr = instr->ConvertToBailOutInstr(instr, !propSymOpnd->HasEquivalentTypeSet() ? IR::BailOutFailedFixedFieldTypeCheck : IR::BailOutFailedEquivalentFixedFieldTypeCheck);
  112. }
  113. IR::Instr* loadInstr = IR::Instr::NewConstantLoad(dataValueDstOpnd, (intptr_t)fixedValue, valType, instr->m_func);
  114. OUTPUT_VERBOSE_TRACE(Js::UseFixedDataPropsPhase,
  115. _u("FixedFields: Replacing the source (fixed Data prop) with property id %u with 0x%x .\n"),
  116. propSymOpnd->GetPropertyId(), fixedValue);
  117. instr->InsertAfter(loadInstr);
  118. propSymOpnd->SetUsesFixedValue(true);
  119. return fixedValue;
  120. }
  121. }
  122. return 0;
  123. }
  124. ///----------------------------------------------------------------------------
  125. ///
  126. /// Instr::IsEqual
  127. /// Check if this instruction is the same instruction as compareInstr. Two
  128. /// instructions are equal if kind, opcode, dst, src1 and src2 from both instrs
  129. /// are the same.
  130. ///
  131. ///----------------------------------------------------------------------------
  132. bool
  133. Instr::IsEqual(IR::Instr *compareInstr) const
  134. {
  135. Assert(compareInstr);
  136. if (this->GetKind() == compareInstr->GetKind()
  137. && this->m_opcode == compareInstr->m_opcode)
  138. {
  139. IR::Opnd *dst = this->GetDst();
  140. IR::Opnd *src1 = this->GetSrc1();
  141. IR::Opnd *src2 = this->GetSrc2();
  142. IR::Opnd *compareDst = compareInstr->GetDst();
  143. IR::Opnd *compareSrc1 = compareInstr->GetSrc1();
  144. IR::Opnd *compareSrc2 = compareInstr->GetSrc2();
  145. // when both dst and compareDst are null, they are equal, same applies to src1, src2
  146. if ((dst != compareDst) && (!dst || !compareDst || !dst->IsEqual(compareDst)))
  147. {
  148. return false;
  149. }
  150. if ((src1 != compareSrc1) && (!src1 || !compareSrc1 || !src1->IsEqual(compareSrc1)))
  151. {
  152. return false;
  153. }
  154. if ((src2 != compareSrc2) && (!src2 || !compareSrc2 || !src2->IsEqual(compareSrc2)))
  155. {
  156. return false;
  157. }
  158. return true;
  159. }
  160. else
  161. {
  162. return false;
  163. }
  164. }
  165. ///----------------------------------------------------------------------------
  166. ///
  167. /// Instr::InsertBefore
  168. ///
  169. /// Insert 'instr' before 'this' instruction.
  170. ///
  171. ///----------------------------------------------------------------------------
  172. void
  173. Instr::InsertBefore(Instr *instr)
  174. {
  175. Assert(!instr->IsLinked());
  176. Instr * prevInstr = this->m_prev;
  177. instr->m_prev = prevInstr;
  178. this->m_prev = instr;
  179. if (prevInstr)
  180. {
  181. prevInstr->m_next = instr;
  182. }
  183. instr->m_next = this;
  184. }
  185. ///----------------------------------------------------------------------------
  186. ///
  187. /// Instr::InsertAfter
  188. ///
  189. /// Insert 'instr' after 'this' instruction.
  190. ///
  191. ///----------------------------------------------------------------------------
  192. void
  193. Instr::InsertAfter(Instr *instr)
  194. {
  195. Assert(!instr->IsLinked());
  196. Instr * nextInstr = this->m_next;
  197. instr->m_next = nextInstr;
  198. this->m_next = instr;
  199. if (nextInstr)
  200. {
  201. nextInstr->m_prev = instr;
  202. }
  203. instr->m_prev = this;
  204. }
  205. ///----------------------------------------------------------------------------
  206. ///
  207. /// Instr::InsertRangeBefore
  208. ///
  209. ///----------------------------------------------------------------------------
  210. void
  211. Instr::InsertRangeBefore(Instr *startInstr, Instr *endInstr)
  212. {
  213. Instr * prevInstr = this->m_prev;
  214. startInstr->m_prev = prevInstr;
  215. this->m_prev = endInstr;
  216. if (prevInstr)
  217. {
  218. prevInstr->m_next = startInstr;
  219. }
  220. endInstr->m_next = this;
  221. }
  222. ///----------------------------------------------------------------------------
  223. ///
  224. /// Instr::InsertMultipleBefore - Inserts multiple instr
  225. ///
  226. ///----------------------------------------------------------------------------
  227. void
  228. Instr::InsertMultipleBefore(Instr *endInstr)
  229. {
  230. Instr *startInstr = endInstr->m_prev;
  231. if (startInstr) // more than one instruction to insert
  232. {
  233. while (startInstr->m_prev)
  234. {
  235. startInstr = startInstr->m_prev;
  236. }
  237. return this->InsertRangeBefore(startInstr, endInstr);
  238. }
  239. return this->InsertBefore(endInstr);
  240. }
  241. ///----------------------------------------------------------------------------
  242. ///
  243. /// Instr::InsertRangeAfter
  244. ///
  245. ///----------------------------------------------------------------------------
  246. void
  247. Instr::InsertRangeAfter(Instr *startInstr, Instr *endInstr)
  248. {
  249. Instr * nextInstr = this->m_next;
  250. endInstr->m_next = nextInstr;
  251. this->m_next = startInstr;
  252. if (nextInstr)
  253. {
  254. nextInstr->m_prev = endInstr;
  255. }
  256. startInstr->m_prev = this;
  257. }
  258. ///----------------------------------------------------------------------------
  259. ///
  260. /// Instr::InsertMultipleAfter - Inserts multiple instr
  261. ///
  262. ///----------------------------------------------------------------------------
  263. void
  264. Instr::InsertMultipleAfter(Instr *endInstr)
  265. {
  266. Instr *startInstr = endInstr->m_prev;
  267. if (startInstr) //more than one instruction to insert
  268. {
  269. while (startInstr->m_prev)
  270. {
  271. startInstr = startInstr->m_prev;
  272. }
  273. return this->InsertRangeAfter(startInstr, endInstr);
  274. }
  275. return this->InsertAfter(endInstr);
  276. }
  277. ///----------------------------------------------------------------------------
  278. ///
  279. /// Instr::Free
  280. ///
  281. /// Free this instruction by putting it on a free list.
  282. ///
  283. ///----------------------------------------------------------------------------
  284. void
  285. Instr::Free()
  286. {
  287. AssertMsg(!this->IsLabelInstr() || !this->AsLabelInstr()->m_hasNonBranchRef,
  288. "Cannot free label with non-branch reference");
  289. switch (this->GetKind())
  290. {
  291. case InstrKindBranch:
  292. {
  293. IR::BranchInstr *branchInstr = this->AsBranchInstr();
  294. branchInstr->ClearTarget();
  295. break;
  296. }
  297. }
  298. IR::Opnd * dstOpnd = this->GetDst();
  299. if (dstOpnd)
  300. {
  301. StackSym * stackSym = dstOpnd->GetStackSym();
  302. if (stackSym)
  303. {
  304. if (stackSym->m_isSingleDef)
  305. {
  306. Assert(!stackSym->m_isEncodedConstant);
  307. if (stackSym->m_instrDef == this)
  308. {
  309. Assert(!dstOpnd->isFakeDst);
  310. if (stackSym->IsConst())
  311. {
  312. // keep the instruction around so we can get the constant value
  313. // from the symbol
  314. return;
  315. }
  316. Assert(this->m_func->GetTopFunc()->allowRemoveBailOutArgInstr || !stackSym->m_isBailOutReferenced);
  317. }
  318. else
  319. {
  320. Assert(dstOpnd->isFakeDst);
  321. }
  322. }
  323. else
  324. {
  325. // Encoded constants are not single-defs anymore, and therefore not isConst.
  326. Assert((!stackSym->m_isConst && stackSym->constantValue == 0)
  327. || (stackSym->m_isEncodedConstant && stackSym->constantValue != 0));
  328. }
  329. }
  330. this->FreeDst();
  331. }
  332. if (this->GetSrc1())
  333. {
  334. this->FreeSrc1();
  335. }
  336. if (this->GetSrc2())
  337. {
  338. // This pattern isn't so unusual:
  339. // src = instr->UnlinkSrc1();
  340. // instr->Remove();
  341. this->FreeSrc2();
  342. }
  343. ClearBailOutInfo();
  344. JitAdelete(this->m_func->m_alloc, this);
  345. }
  346. ///----------------------------------------------------------------------------
  347. ///
  348. /// Instr::Unlink
  349. ///
  350. /// Unlink this instr from the instr list.
  351. ///
  352. ///----------------------------------------------------------------------------
  353. void
  354. Instr::Unlink()
  355. {
  356. m_prev->m_next = m_next;
  357. if (m_next)
  358. {
  359. m_next->m_prev = m_prev;
  360. }
  361. else
  362. {
  363. Assert(this == this->m_func->m_tailInstr);
  364. }
  365. #if DBG_DUMP
  366. // Transferring the globOptInstrString to the next non-Label Instruction
  367. if(this->globOptInstrString != nullptr && m_next && m_next->globOptInstrString == nullptr && !m_next->IsLabelInstr())
  368. {
  369. m_next->globOptInstrString = this->globOptInstrString;
  370. }
  371. #endif
  372. #if DBG
  373. m_prev = nullptr;
  374. m_next = nullptr;
  375. #endif
  376. }
  377. ///----------------------------------------------------------------------------
  378. ///
  379. /// Instr::Remove
  380. ///
  381. /// Unlink and free this instr.
  382. ///
  383. ///----------------------------------------------------------------------------
  384. void
  385. Instr::Remove()
  386. {
  387. this->Unlink();
  388. this->Free();
  389. }
  390. void
  391. Instr::SwapOpnds()
  392. {
  393. IR::Opnd *opndTemp = m_src1;
  394. m_src1 = m_src2;
  395. m_src2 = opndTemp;
  396. }
  397. Instr *
  398. Instr::CopyWithoutDst()
  399. {
  400. return Copy(false /*copyDst*/);
  401. }
  402. // Copy a vanilla instruction.
  403. Instr *
  404. Instr::Copy(bool copyDst)
  405. {
  406. Instr * instrCopy;
  407. if (this->HasBailOutInfo() || this->HasAuxBailOut())
  408. {
  409. instrCopy = BailOutInstr::New(this->m_opcode, this->GetBailOutKind(), this->GetBailOutInfo(), this->m_func);
  410. instrCopy->SetByteCodeOffset(this->GetByteCodeOffset());
  411. if (this->HasAuxBailOut())
  412. {
  413. instrCopy->hasAuxBailOut = true;
  414. instrCopy->SetAuxBailOutKind(this->GetAuxBailOutKind());
  415. }
  416. }
  417. else
  418. {
  419. switch (this->GetKind())
  420. {
  421. case InstrKindInstr:
  422. instrCopy = Instr::New(this->m_opcode, this->m_func);
  423. break;
  424. case InstrKindProfiled:
  425. instrCopy = this->AsProfiledInstr()->CopyProfiledInstr();
  426. break;
  427. case InstrKindJitProfiling:
  428. instrCopy = this->AsJitProfilingInstr()->CopyJitProfiling();
  429. break;
  430. case InstrKindPragma:
  431. instrCopy = this->AsPragmaInstr()->CopyPragma();
  432. break;
  433. default:
  434. instrCopy = nullptr;
  435. AnalysisAssertMsg(UNREACHED, "Copy of other instr kinds NYI");
  436. }
  437. }
  438. Opnd * opnd = this->GetDst();
  439. if (copyDst && opnd)
  440. {
  441. instrCopy->SetDst(opnd->Copy(this->m_func));
  442. }
  443. opnd = this->GetSrc1();
  444. if (opnd)
  445. {
  446. instrCopy->SetSrc1(opnd->Copy(this->m_func));
  447. opnd = this->GetSrc2();
  448. if (opnd)
  449. {
  450. instrCopy->SetSrc2(opnd->Copy(this->m_func));
  451. }
  452. }
  453. instrCopy->isInlineeEntryInstr = this->isInlineeEntryInstr;
  454. if (this->m_func->DoMaintainByteCodeOffset())
  455. {
  456. instrCopy->SetByteCodeOffset(this->GetByteCodeOffset());
  457. }
  458. instrCopy->usesStackArgumentsObject = this->usesStackArgumentsObject;
  459. return instrCopy;
  460. }
  461. LabelInstr *
  462. LabelInstr::CloneLabel(BOOL fCreate)
  463. {
  464. Func * func = this->m_func;
  465. Cloner * cloner = func->GetCloner();
  466. IR::LabelInstr * instrLabel = nullptr;
  467. AssertMsg(cloner, "Use Func::BeginClone to initialize cloner");
  468. if (cloner->labelMap == nullptr)
  469. {
  470. if (!fCreate)
  471. {
  472. return nullptr;
  473. }
  474. cloner->labelMap = HashTable<LabelInstr*>::New(cloner->alloc, 7);
  475. }
  476. else
  477. {
  478. IR::LabelInstr ** map = cloner->labelMap->Get(this->m_id);
  479. if (map)
  480. {
  481. instrLabel = *map;
  482. }
  483. }
  484. if (instrLabel == nullptr)
  485. {
  486. if (!fCreate)
  487. {
  488. return nullptr;
  489. }
  490. if (this->IsProfiledLabelInstr())
  491. {
  492. instrLabel = IR::ProfiledLabelInstr::New(this->m_opcode, func, this->AsProfiledLabelInstr()->loopImplicitCallFlags, this->AsProfiledLabelInstr()->loopFlags);
  493. #if DBG
  494. instrLabel->AsProfiledLabelInstr()->loopNum = this->AsProfiledLabelInstr()->loopNum;
  495. #endif
  496. }
  497. else
  498. {
  499. instrLabel = IR::LabelInstr::New(this->m_opcode, func, this->isOpHelper);
  500. }
  501. instrLabel->m_isLoopTop = this->m_isLoopTop;
  502. cloner->labelMap->FindOrInsert(instrLabel, this->m_id);
  503. }
  504. return instrLabel;
  505. }
  506. ProfiledLabelInstr::ProfiledLabelInstr(JitArenaAllocator * allocator)
  507. : LabelInstr(allocator)
  508. {
  509. }
  510. ProfiledLabelInstr *
  511. ProfiledLabelInstr::New(Js::OpCode opcode, Func *func, Js::ImplicitCallFlags flags, Js::LoopFlags loopFlags)
  512. {
  513. ProfiledLabelInstr * profiledLabelInstr = JitAnew(func->m_alloc, ProfiledLabelInstr, func->m_alloc);
  514. profiledLabelInstr->Init(opcode, InstrKindProfiledLabel, func, false);
  515. profiledLabelInstr->loopImplicitCallFlags = flags;
  516. profiledLabelInstr->loopFlags = loopFlags;
  517. return profiledLabelInstr;
  518. }
  519. void
  520. BranchInstr::RetargetClonedBranch()
  521. {
  522. IR::LabelInstr * instrLabel = this->m_branchTarget->CloneLabel(false);
  523. if (instrLabel == nullptr)
  524. {
  525. // Jumping outside the cloned range. No retarget.
  526. return;
  527. }
  528. this->SetTarget(instrLabel);
  529. }
  530. PragmaInstr *
  531. PragmaInstr::ClonePragma()
  532. {
  533. return this->CopyPragma();
  534. }
  535. PragmaInstr *
  536. PragmaInstr::CopyPragma()
  537. {
  538. IR::PragmaInstr * instrPragma = IR::PragmaInstr::New(this->m_opcode, 0, this->m_func);
  539. return instrPragma;
  540. }
  541. Instr *
  542. Instr::CloneInstr() const
  543. {
  544. if (this->HasBailOutInfo() || this->HasAuxBailOut())
  545. {
  546. return ((BailOutInstr *)this)->CloneBailOut();
  547. }
  548. IR::Instr *clone = IR::Instr::New(this->m_opcode, this->m_func);
  549. clone->isInlineeEntryInstr = this->isInlineeEntryInstr;
  550. return clone;
  551. }
  552. // Clone a vanilla instruction, replacing single-def StackSym's with new syms where appropriate.
  553. Instr *
  554. Instr::Clone()
  555. {
  556. Func * func = this->m_func;
  557. Cloner *cloner = func->GetCloner();
  558. IR::Instr * instrClone;
  559. IR::Opnd * opnd;
  560. switch (this->GetKind())
  561. {
  562. case InstrKindInstr:
  563. instrClone = this->CloneInstr();
  564. break;
  565. case InstrKindBranch:
  566. instrClone = this->AsBranchInstr()->CloneBranchInstr();
  567. break;
  568. case InstrKindProfiled:
  569. instrClone = this->AsProfiledInstr()->CloneProfiledInstr();
  570. break;
  571. case InstrKindLabel:
  572. case InstrKindProfiledLabel:
  573. instrClone = this->AsLabelInstr()->CloneLabel(true);
  574. break;
  575. case InstrKindPragma:
  576. instrClone = this->AsPragmaInstr()->ClonePragma();
  577. break;
  578. case InstrKindJitProfiling:
  579. instrClone = this->AsJitProfilingInstr()->CloneJitProfiling();
  580. break;
  581. default:
  582. AssertMsg(0, "Clone of this instr kind NYI");
  583. return nullptr;
  584. }
  585. opnd = this->GetDst();
  586. if (opnd)
  587. {
  588. instrClone->SetDst(opnd->CloneDef(func));
  589. }
  590. opnd = this->GetSrc1();
  591. if (opnd)
  592. {
  593. instrClone->SetSrc1(opnd->CloneUse(func));
  594. opnd = this->GetSrc2();
  595. if (opnd)
  596. {
  597. instrClone->SetSrc2(opnd->CloneUse(func));
  598. }
  599. }
  600. if (this->m_func->DoMaintainByteCodeOffset())
  601. {
  602. instrClone->SetByteCodeOffset(this->GetByteCodeOffset());
  603. }
  604. instrClone->usesStackArgumentsObject = this->usesStackArgumentsObject;
  605. cloner->AddInstr(this, instrClone);
  606. return instrClone;
  607. }
  608. // Clone a range of instructions.
  609. Instr *
  610. Instr::CloneRange(
  611. Instr * instrStart, Instr * instrLast, Instr * instrAfter, Lowerer *lowerer, JitArenaAllocator * alloc, bool (*fMapTest)(IR::Instr*), bool clonedInstrGetOrigArgSlotSym)
  612. {
  613. IR::Instr * instrReturn = instrAfter;
  614. Func * topFunc = instrStart->m_func->GetTopFunc();
  615. topFunc->BeginClone(lowerer, alloc);
  616. topFunc->GetCloner()->clonedInstrGetOrigArgSlotSym = clonedInstrGetOrigArgSlotSym;
  617. FOREACH_INSTR_IN_RANGE(instr, instrStart, instrLast)
  618. {
  619. Instr * instrClone = instr->Clone();
  620. instrAfter->InsertAfter(instrClone);
  621. instrAfter = instrClone;
  622. instr->isCloned = true;
  623. if (fMapTest(instrClone))
  624. {
  625. IR::LabelInstr *instrLabel = IR::LabelInstr::New(Js::OpCode::Label, instr->m_func);
  626. instrClone->InsertBefore(instrLabel);
  627. topFunc->GetCloneMap()->Item(instr, instrLabel);
  628. }
  629. }
  630. NEXT_INSTR_IN_RANGE;
  631. topFunc->EndClone();
  632. return instrReturn;
  633. }
  634. ///----------------------------------------------------------------------------
  635. ///
  636. /// Instr::MoveRangeAfter
  637. ///
  638. /// Move a range of instruction after another instruction
  639. ///
  640. ///----------------------------------------------------------------------------
  641. void
  642. Instr::MoveRangeAfter(Instr * instrStart, Instr * instrLast, Instr * instrAfter)
  643. {
  644. if (instrLast->m_next != nullptr)
  645. {
  646. instrLast->m_next->m_prev = instrStart->m_prev;
  647. }
  648. else
  649. {
  650. instrLast->m_func->m_tailInstr = instrStart->m_prev;
  651. }
  652. if (instrStart->m_prev != nullptr)
  653. {
  654. instrStart->m_prev->m_next = instrLast->m_next;
  655. }
  656. else
  657. {
  658. instrStart->m_func->m_headInstr = instrLast->m_next;
  659. }
  660. instrStart->m_prev = instrAfter;
  661. instrLast->m_next = instrAfter->m_next;
  662. if (instrAfter->m_next != nullptr)
  663. {
  664. instrAfter->m_next->m_prev = instrLast;
  665. }
  666. else
  667. {
  668. instrAfter->m_func->m_tailInstr = instrLast;
  669. }
  670. instrAfter->m_next = instrStart;
  671. }
  672. JitProfilingInstr *
  673. JitProfilingInstr::New(Js::OpCode opcode, Opnd *dstOpnd, Opnd *src1Opnd, Opnd *src2Opnd, Func * func)
  674. {
  675. JitProfilingInstr * profiledInstr = JitProfilingInstr::New(opcode, dstOpnd, src1Opnd, func);
  676. profiledInstr->SetSrc2(src2Opnd);
  677. return profiledInstr;
  678. }
  679. JitProfilingInstr *
  680. JitProfilingInstr::New(Js::OpCode opcode, Opnd *dstOpnd, Opnd *src1Opnd, Func * func)
  681. {
  682. Assert(func->DoSimpleJitDynamicProfile());
  683. JitProfilingInstr * profiledInstr = JitAnew(func->m_alloc, IR::JitProfilingInstr);
  684. profiledInstr->Init(opcode, InstrKindJitProfiling, func);
  685. if (dstOpnd)
  686. {
  687. profiledInstr->SetDst(dstOpnd);
  688. }
  689. if (src1Opnd)
  690. {
  691. profiledInstr->SetSrc1(src1Opnd);
  692. }
  693. #if DBG
  694. profiledInstr->profileId = Js::Constants::NoProfileId;
  695. profiledInstr->arrayProfileId = Js::Constants::NoProfileId;
  696. profiledInstr->inlineCacheIndex = Js::Constants::NoInlineCacheIndex;
  697. Assert(profiledInstr->loopNumber == 0u - 1);
  698. #endif
  699. // default these to false.
  700. profiledInstr->isProfiledReturnCall = false;
  701. profiledInstr->isBeginSwitch = false;
  702. profiledInstr->isNewArray = false;
  703. profiledInstr->isLoopHelper = false;
  704. return profiledInstr;
  705. }
  706. JitProfilingInstr*
  707. JitProfilingInstr::CloneJitProfiling() const
  708. {
  709. // Adapted from Profiled::CloneProfiledInstr. Note that the dst and srcs are not set.
  710. Assert(!(this->HasBailOutInfo() || this->HasAuxBailOut())); // Shouldn't have bailout info in a jitprofiling instr
  711. return this->CopyJitProfiling();
  712. }
  713. JitProfilingInstr*
  714. JitProfilingInstr::CopyJitProfiling() const
  715. {
  716. // Adapted from Profiled::CopyProfiledInstr. Note that the dst and srcs are not set.
  717. IR::JitProfilingInstr * jitProfInstr;
  718. jitProfInstr = JitAnew(this->m_func->m_alloc, IR::JitProfilingInstr);
  719. jitProfInstr->Init(this->m_opcode, InstrKindProfiled, this->m_func);
  720. jitProfInstr->isProfiledReturnCall = this->isProfiledReturnCall;
  721. jitProfInstr->isBeginSwitch = this->isBeginSwitch;
  722. jitProfInstr->isNewArray = this->isNewArray;
  723. jitProfInstr->isLoopHelper = this->isLoopHelper;
  724. jitProfInstr->profileId = this->profileId;
  725. jitProfInstr->arrayProfileId = this->arrayProfileId;
  726. jitProfInstr->inlineCacheIndex = this->inlineCacheIndex;
  727. Assert(jitProfInstr->loopNumber == this->loopNumber);
  728. return jitProfInstr;
  729. }
  730. ProfiledInstr *
  731. ProfiledInstr::New(Js::OpCode opcode, Opnd *dstOpnd, Opnd *src1Opnd, Opnd *src2Opnd, Func * func)
  732. {
  733. ProfiledInstr * profiledInstr = ProfiledInstr::New(opcode, dstOpnd, src1Opnd, func);
  734. profiledInstr->SetSrc2(src2Opnd);
  735. return profiledInstr;
  736. }
  737. ProfiledInstr *
  738. ProfiledInstr::New(Js::OpCode opcode, Opnd *dstOpnd, Opnd *src1Opnd, Func * func)
  739. {
  740. ProfiledInstr * profiledInstr = JitAnew(func->m_alloc, IR::ProfiledInstr);
  741. profiledInstr->Init(opcode, InstrKindProfiled, func);
  742. if (dstOpnd)
  743. {
  744. profiledInstr->SetDst(dstOpnd);
  745. }
  746. if (src1Opnd)
  747. {
  748. profiledInstr->SetSrc1(src1Opnd);
  749. }
  750. profiledInstr->u.ldElemInfo = nullptr;
  751. return profiledInstr;
  752. }
  753. ProfiledInstr *
  754. ProfiledInstr::CloneProfiledInstr() const
  755. {
  756. IR::ProfiledInstr * profiledInstr;
  757. if (this->HasBailOutInfo() || this->HasAuxBailOut())
  758. {
  759. profiledInstr = ((ProfiledBailOutInstr *)this)->CloneBailOut();
  760. profiledInstr->u = this->u;
  761. }
  762. else
  763. {
  764. profiledInstr = this->CopyProfiledInstr();
  765. }
  766. return profiledInstr;
  767. }
  768. ProfiledInstr *
  769. ProfiledInstr::CopyProfiledInstr() const
  770. {
  771. IR::ProfiledInstr * profiledInstr;
  772. profiledInstr = JitAnew(this->m_func->m_alloc, IR::ProfiledInstr);
  773. profiledInstr->Init(this->m_opcode, InstrKindProfiled, this->m_func);
  774. profiledInstr->u = this->u;
  775. return profiledInstr;
  776. }
  777. ByteCodeUsesInstr *
  778. ByteCodeUsesInstr::New(IR::Instr * originalBytecodeInstr)
  779. {
  780. Func* func = originalBytecodeInstr->m_func;
  781. ByteCodeUsesInstr * byteCodeUses = JitAnew(func->m_alloc, IR::ByteCodeUsesInstr);
  782. byteCodeUses->Init(Js::OpCode::ByteCodeUses, InstrKindByteCodeUses, func);
  783. byteCodeUses->byteCodeUpwardExposedUsed = nullptr;
  784. byteCodeUses->propertySymUse = nullptr;
  785. byteCodeUses->SetByteCodeOffset(originalBytecodeInstr);
  786. return byteCodeUses;
  787. }
  788. ByteCodeUsesInstr *
  789. ByteCodeUsesInstr::New(Func * func, uint32 offset)
  790. {
  791. ByteCodeUsesInstr * byteCodeUses = JitAnew(func->m_alloc, IR::ByteCodeUsesInstr);
  792. byteCodeUses->Init(Js::OpCode::ByteCodeUses, InstrKindByteCodeUses, func);
  793. byteCodeUses->byteCodeUpwardExposedUsed = nullptr;
  794. byteCodeUses->propertySymUse = nullptr;
  795. byteCodeUses->SetByteCodeOffset(offset);
  796. return byteCodeUses;
  797. }
  798. const BVSparse<JitArenaAllocator> * ByteCodeUsesInstr::GetByteCodeUpwardExposedUsed() const
  799. {
  800. return this->byteCodeUpwardExposedUsed;
  801. }
  802. // In the case of instances where you would like to add a ByteCodeUses to some sym,
  803. // which doesn't have an operand associated with it (like a block closure sym), use
  804. // this to set it without needing to pass the check for JIT-Optimized registers.
  805. void ByteCodeUsesInstr::SetNonOpndSymbol(uint symId)
  806. {
  807. if (!this->byteCodeUpwardExposedUsed)
  808. {
  809. this->byteCodeUpwardExposedUsed = JitAnew(m_func->m_alloc, BVSparse<JitArenaAllocator>, m_func->m_alloc);
  810. }
  811. this->byteCodeUpwardExposedUsed->Set(symId);
  812. }
  813. // In cases where the operand you're working on may be changed between when you get
  814. // access to it and when you determine that you can set it in the ByteCodeUsesInstr
  815. // set method, cache the values and use this caller.
  816. void ByteCodeUsesInstr::SetRemovedOpndSymbol(bool isJITOptimizedReg, uint symId)
  817. {
  818. if (isJITOptimizedReg)
  819. {
  820. AssertMsg(false, "Tried to add a jit-optimized register to a ByteCodeUses instruction!");
  821. // Although we assert on debug builds, we should actually be ok with release builds
  822. // if we ignore the operand; not ignoring it, however, can cause us to introduce an
  823. // inconsistency in bytecode register lifetimes.
  824. return;
  825. }
  826. if(!this->byteCodeUpwardExposedUsed)
  827. {
  828. this->byteCodeUpwardExposedUsed = JitAnew(m_func->m_alloc, BVSparse<JitArenaAllocator>, m_func->m_alloc);
  829. }
  830. this->byteCodeUpwardExposedUsed->Set(symId);
  831. }
  832. void ByteCodeUsesInstr::Set(IR::Opnd * originalOperand)
  833. {
  834. Assert(originalOperand && originalOperand->GetStackSym());
  835. bool isJITOptimizedReg = originalOperand->GetIsJITOptimizedReg();
  836. SymID symId = originalOperand->GetStackSym()->m_id;
  837. if (isJITOptimizedReg)
  838. {
  839. AssertMsg(false, "Tried to add a jit-optimized register to a ByteCodeUses instruction!");
  840. // Although we assert on debug builds, we should actually be ok with release builds
  841. // if we ignore the operand; not ignoring it, however, can cause us to introduce an
  842. // inconsistency in bytecode register lifetimes.
  843. return;
  844. }
  845. if (!this->byteCodeUpwardExposedUsed)
  846. {
  847. this->byteCodeUpwardExposedUsed = JitAnew(m_func->m_alloc, BVSparse<JitArenaAllocator>, m_func->m_alloc);
  848. }
  849. this->byteCodeUpwardExposedUsed->Set(symId);
  850. }
  851. void ByteCodeUsesInstr::Clear(uint symId)
  852. {
  853. Assert(byteCodeUpwardExposedUsed != nullptr);
  854. this->byteCodeUpwardExposedUsed->Clear(symId);
  855. }
  856. void ByteCodeUsesInstr::SetBV(BVSparse<JitArenaAllocator>* newbv)
  857. {
  858. Assert(byteCodeUpwardExposedUsed == nullptr && newbv != nullptr);
  859. byteCodeUpwardExposedUsed = newbv;
  860. }
  861. // If possible, we want to aggregate with subsequent ByteCodeUses Instructions, so
  862. // that we can do some optimizations in other places where we can simplify args in
  863. // a compare, but still need to generate them for bailouts. Without this, we cause
  864. // problems because we end up with an instruction losing atomicity in terms of its
  865. // bytecode use and generation lifetimes.
  866. void ByteCodeUsesInstr::AggregateFollowingByteCodeUses()
  867. {
  868. IR::Instr* scanner = this->m_next;
  869. while (scanner && scanner->m_opcode == Js::OpCode::ByteCodeUses && scanner->GetByteCodeOffset() == this->GetByteCodeOffset() && scanner->GetDst() == nullptr)
  870. {
  871. IR::ByteCodeUsesInstr* target = scanner->AsByteCodeUsesInstr();
  872. this->Aggregate(target);
  873. scanner = scanner->m_next;
  874. }
  875. }
  876. void ByteCodeUsesInstr::Aggregate(ByteCodeUsesInstr * byteCodeUsesInstr)
  877. {
  878. Assert(this->m_func == byteCodeUsesInstr->m_func);
  879. if (byteCodeUsesInstr->byteCodeUpwardExposedUsed)
  880. {
  881. Assert(byteCodeUsesInstr->GetDst() == nullptr);
  882. if (this->byteCodeUpwardExposedUsed)
  883. {
  884. this->byteCodeUpwardExposedUsed->Or(byteCodeUsesInstr->byteCodeUpwardExposedUsed);
  885. JitAdelete(byteCodeUsesInstr->byteCodeUpwardExposedUsed->GetAllocator(), byteCodeUsesInstr->byteCodeUpwardExposedUsed);
  886. byteCodeUsesInstr->byteCodeUpwardExposedUsed = nullptr;
  887. }
  888. else
  889. {
  890. this->byteCodeUpwardExposedUsed = byteCodeUsesInstr->byteCodeUpwardExposedUsed;
  891. byteCodeUsesInstr->byteCodeUpwardExposedUsed = nullptr;
  892. }
  893. }
  894. }
  895. bool Instr::CanAggregateByteCodeUsesAcrossInstr(Instr * instr)
  896. {
  897. return !instr->StartsBasicBlock() &&
  898. instr->m_func == this->m_func &&
  899. ((instr->GetByteCodeOffset() == Js::Constants::NoByteCodeOffset) ||
  900. (instr->GetByteCodeOffset() == this->GetByteCodeOffset()));
  901. }
  902. bool IR::Instr::IsStFldVariant() const
  903. {
  904. return this->m_opcode == Js::OpCode::StFld ||
  905. this->m_opcode == Js::OpCode::StFldStrict ||
  906. this->m_opcode == Js::OpCode::StLocalFld ||
  907. this->m_opcode == Js::OpCode::StRootFld ||
  908. this->m_opcode == Js::OpCode::StRootFldStrict ||
  909. this->m_opcode == Js::OpCode::StSuperFld;
  910. }
  911. bool IR::Instr::IsStElemVariant() const
  912. {
  913. return this->m_opcode == Js::OpCode::StElemI_A ||
  914. this->m_opcode == Js::OpCode::StElemI_A_Strict ||
  915. this->m_opcode == Js::OpCode::StElemC;
  916. }
  917. bool IR::Instr::CanChangeFieldValueWithoutImplicitCall() const
  918. {
  919. // TODO: Why is InitFld necessary?
  920. return this->IsStFldVariant() || this->IsStElemVariant();
  921. }
  922. // If LazyBailOut is the only BailOutKind on the instruction, the BailOutInfo is cleared.
  923. // Otherwise, we remove the LazyBailOut kind from the instruction and still keep the BailOutInfo.
  924. void IR::Instr::ClearLazyBailOut()
  925. {
  926. if (!this->HasBailOutInfo())
  927. {
  928. return;
  929. }
  930. if (this->OnlyHasLazyBailOut())
  931. {
  932. this->ClearBailOutInfo();
  933. }
  934. else
  935. {
  936. this->GetBailOutInfo()->RestoreUseOfDst();
  937. this->SetBailOutKind(BailOutInfo::WithoutLazyBailOut(this->GetBailOutKind()));
  938. }
  939. Assert(!this->HasLazyBailOut());
  940. }
  941. int IR::Instr::GetOpndCount() const
  942. {
  943. return (this->m_src1 ? 1 : 0) + (this->m_src2 ? 1 : 0) + (this->m_dst ? 1 : 0);
  944. }
  945. bool IR::Instr::AreAllOpndsTypeSpecialized() const
  946. {
  947. bool src1TypeSpec = !this->m_src1 || (this->m_src1->GetStackSym() && this->m_src1->GetStackSym()->IsTypeSpec());
  948. bool src2TypeSpec = !this->m_src2 || (this->m_src2->GetStackSym() && this->m_src2->GetStackSym()->IsTypeSpec());
  949. bool dstTypeSpec = !this->m_dst || (this->m_dst->GetStackSym() && this->m_dst->GetStackSym()->IsTypeSpec());
  950. return src1TypeSpec && src2TypeSpec && dstTypeSpec && this->GetOpndCount() > 0;
  951. }
  952. bool IR::Instr::OnlyHasLazyBailOut() const
  953. {
  954. return this->HasBailOutInfo() && BailOutInfo::OnlyHasLazyBailOut(this->GetBailOutKind());
  955. }
  956. bool IR::Instr::HasLazyBailOut() const
  957. {
  958. return this->HasBailOutInfo() && BailOutInfo::HasLazyBailOut(this->GetBailOutKind());
  959. }
  960. bool IR::Instr::HasPreOpBailOut() const
  961. {
  962. return this->HasBailOutInfo() && this->GetBailOutInfo()->bailOutOffset == this->GetByteCodeOffset();
  963. }
  964. bool IR::Instr::HasPostOpBailOut() const
  965. {
  966. return this->HasBailOutInfo() && this->GetBailOutInfo()->bailOutOffset > this->GetByteCodeOffset();
  967. }
  968. BailOutInfo *
  969. Instr::GetBailOutInfo() const
  970. {
  971. Assert(this->HasBailOutInfo() || this->HasAuxBailOut());
  972. switch (this->m_kind)
  973. {
  974. case InstrKindInstr:
  975. return ((BailOutInstr const *)this)->bailOutInfo;
  976. case InstrKindProfiled:
  977. return ((ProfiledBailOutInstr const *)this)->bailOutInfo;
  978. case InstrKindBranch:
  979. return ((BranchBailOutInstr const *)this)->bailOutInfo;
  980. default:
  981. Assert(false);
  982. __assume(false);
  983. }
  984. }
  985. BailOutKind
  986. Instr::GetBailOutKind() const
  987. {
  988. Assert(this->HasBailOutInfo());
  989. switch (this->m_kind)
  990. {
  991. case InstrKindInstr:
  992. return ((BailOutInstr const *)this)->bailOutKind;
  993. case InstrKindProfiled:
  994. return ((ProfiledBailOutInstr const *)this)->bailOutKind;
  995. case InstrKindBranch:
  996. return ((BranchBailOutInstr const *)this)->bailOutKind;
  997. default:
  998. Assert(false);
  999. return BailOutInvalid;
  1000. }
  1001. }
  1002. BailOutKind
  1003. Instr::GetBailOutKindNoBits() const
  1004. {
  1005. return GetBailOutKind() & ~IR::BailOutKindBits;
  1006. }
  1007. BailOutKind
  1008. Instr::GetAuxBailOutKind() const
  1009. {
  1010. Assert(this->HasAuxBailOut());
  1011. switch (this->m_kind)
  1012. {
  1013. case InstrKindInstr:
  1014. return ((BailOutInstr const *)this)->auxBailOutKind;
  1015. case InstrKindProfiled:
  1016. return ((ProfiledBailOutInstr const *)this)->auxBailOutKind;
  1017. case InstrKindBranch:
  1018. return ((BranchBailOutInstr const *)this)->auxBailOutKind;
  1019. default:
  1020. Assert(false);
  1021. return BailOutInvalid;
  1022. }
  1023. }
  1024. void Instr::SetBailOutKind(const IR::BailOutKind bailOutKind)
  1025. {
  1026. Assert(this->HasBailOutInfo());
  1027. Assert(bailOutKind != IR::BailOutInvalid);
  1028. this->SetBailOutKind_NoAssert(bailOutKind);
  1029. }
  1030. // Helper to set bail out kind, doesn't assert.
  1031. void Instr::SetBailOutKind_NoAssert(const IR::BailOutKind bailOutKind)
  1032. {
  1033. Assert(IsValidBailOutKindAndBits(bailOutKind));
  1034. switch (this->m_kind)
  1035. {
  1036. case InstrKindInstr:
  1037. ((BailOutInstr *)this)->bailOutKind = bailOutKind;
  1038. break;
  1039. case InstrKindProfiled:
  1040. ((ProfiledBailOutInstr *)this)->bailOutKind = bailOutKind;
  1041. break;
  1042. case InstrKindBranch:
  1043. ((BranchBailOutInstr *)this)->bailOutKind = bailOutKind;
  1044. break;
  1045. default:
  1046. Assert(false);
  1047. __assume(false);
  1048. }
  1049. }
  1050. void Instr::SetAuxBailOutKind(const IR::BailOutKind bailOutKind)
  1051. {
  1052. switch (this->m_kind)
  1053. {
  1054. case InstrKindInstr:
  1055. ((BailOutInstr *)this)->auxBailOutKind = bailOutKind;
  1056. break;
  1057. case InstrKindProfiled:
  1058. ((ProfiledBailOutInstr *)this)->auxBailOutKind = bailOutKind;
  1059. break;
  1060. case InstrKindBranch:
  1061. ((BranchBailOutInstr *)this)->auxBailOutKind = bailOutKind;
  1062. break;
  1063. default:
  1064. Assert(false);
  1065. __assume(false);
  1066. }
  1067. }
  1068. BailOutInfo *
  1069. Instr::UnlinkBailOutInfo()
  1070. {
  1071. BailOutInfo *bailOutInfo;
  1072. Assert(this->HasBailOutInfo() || this->HasAuxBailOut());
  1073. switch (this->m_kind)
  1074. {
  1075. case InstrKindInstr:
  1076. bailOutInfo = ((BailOutInstr const *)this)->bailOutInfo;
  1077. ((BailOutInstr *)this)->bailOutInfo = nullptr;
  1078. break;
  1079. case InstrKindProfiled:
  1080. bailOutInfo = ((ProfiledBailOutInstr const *)this)->bailOutInfo;
  1081. ((ProfiledBailOutInstr *)this)->bailOutInfo = nullptr;
  1082. break;
  1083. case InstrKindBranch:
  1084. bailOutInfo = ((BranchBailOutInstr const *)this)->bailOutInfo;
  1085. ((BranchBailOutInstr *)this)->bailOutInfo = nullptr;
  1086. break;
  1087. default:
  1088. Assert(false);
  1089. return nullptr;
  1090. }
  1091. Assert(bailOutInfo);
  1092. #if 0
  1093. if (bailOutInfo->bailOutInstr == this)
  1094. {
  1095. bailOutInfo->bailOutInstr = nullptr;
  1096. }
  1097. #endif
  1098. this->hasBailOutInfo = false;
  1099. this->hasAuxBailOut = false;
  1100. return bailOutInfo;
  1101. }
  1102. void
  1103. Instr::ReplaceBailOutInfo(BailOutInfo *newBailOutInfo)
  1104. {
  1105. BailOutInfo *oldBailOutInfo = nullptr;
  1106. #if DBG
  1107. newBailOutInfo->wasCopied = true;
  1108. #endif
  1109. Assert(this->HasBailOutInfo() || this->HasAuxBailOut());
  1110. switch (this->m_kind)
  1111. {
  1112. case InstrKindInstr:
  1113. oldBailOutInfo = ((BailOutInstr *)this)->bailOutInfo;
  1114. ((BailOutInstr *)this)->bailOutInfo = newBailOutInfo;
  1115. break;
  1116. case InstrKindProfiled:
  1117. oldBailOutInfo = ((ProfiledBailOutInstr *)this)->bailOutInfo;
  1118. ((ProfiledBailOutInstr *)this)->bailOutInfo = newBailOutInfo;
  1119. break;
  1120. case InstrKindBranch:
  1121. AssertMsg(!this->HasBailOutInfo() && this->HasAuxBailOut(), "ReplaceBailOutInfo is not used with InstrKindBranch for non-aux bailout");
  1122. oldBailOutInfo = ((BranchBailOutInstr *)this)->bailOutInfo;
  1123. ((BranchBailOutInstr *)this)->bailOutInfo = newBailOutInfo;
  1124. break;
  1125. default:
  1126. Assert(false);
  1127. __assume(UNREACHED);
  1128. }
  1129. if (oldBailOutInfo->bailOutInstr == this && !oldBailOutInfo->sharedBailOutKind)
  1130. {
  1131. Assert(!oldBailOutInfo->wasCloned && !oldBailOutInfo->wasCopied);
  1132. JitArenaAllocator * alloc = this->m_func->m_alloc;
  1133. oldBailOutInfo->Clear(alloc);
  1134. JitAdelete(alloc, oldBailOutInfo);
  1135. }
  1136. return;
  1137. }
  1138. IR::Instr *Instr::ShareBailOut()
  1139. {
  1140. BailOutInfo *const bailOutInfo = GetBailOutInfo();
  1141. bailOutInfo->bailOutInstr = nullptr;
  1142. #if DBG
  1143. bailOutInfo->wasCopied = true;
  1144. #endif
  1145. IR::Instr *const sharedBail =
  1146. IR::BailOutInstr::New(Js::OpCode::BailTarget, IR::BailOutShared, bailOutInfo, bailOutInfo->bailOutFunc);
  1147. sharedBail->SetByteCodeOffset(this);
  1148. InsertAfter(sharedBail);
  1149. Assert(bailOutInfo->bailOutInstr == sharedBail);
  1150. return sharedBail;
  1151. }
  1152. void
  1153. Instr::UnlinkStartCallFromBailOutInfo(IR::Instr *endInstr) const
  1154. {
  1155. #ifdef _M_IX86
  1156. // The StartCall instruction is being deleted, or is being moved and may later be deleted,
  1157. // so remove its references from bailouts in the given range.
  1158. // This only happens during cloning, which is rare, and only across the range of instructions
  1159. // that evaluate outgoing arguments, which is long only in synthetic cases.
  1160. Assert(this->m_opcode == Js::OpCode::StartCall);
  1161. if (!this->m_func->hasBailout)
  1162. {
  1163. return;
  1164. }
  1165. FOREACH_INSTR_IN_RANGE(instr, this->m_next, endInstr)
  1166. {
  1167. if (instr->HasBailOutInfo())
  1168. {
  1169. BailOutInfo *bailOutInfo = instr->GetBailOutInfo();
  1170. bailOutInfo->UnlinkStartCall(this);
  1171. }
  1172. }
  1173. NEXT_INSTR_IN_RANGE;
  1174. #endif
  1175. }
  1176. Opnd *Instr::FindCallArgumentOpnd(const Js::ArgSlot argSlot, IR::Instr * *const ownerInstrRef)
  1177. {
  1178. Assert(OpCodeAttr::CallInstr(m_opcode));
  1179. Assert(argSlot != static_cast<Js::ArgSlot>(0));
  1180. IR::Instr *argInstr = this;
  1181. Assert(argInstr->GetSrc2());
  1182. Assert(argInstr->GetSrc2()->IsSymOpnd());
  1183. do
  1184. {
  1185. StackSym *const linkSym = argInstr->GetSrc2()->AsSymOpnd()->m_sym->AsStackSym();
  1186. Assert(linkSym->IsSingleDef());
  1187. Assert(linkSym->IsArgSlotSym());
  1188. argInstr = linkSym->m_instrDef;
  1189. Assert(argInstr->GetSrc2());
  1190. if(argInstr->m_opcode == Js::OpCode::ArgOut_A_InlineSpecialized)
  1191. {
  1192. // This is a fake ArgOut, skip it
  1193. continue;
  1194. }
  1195. if(linkSym->GetArgSlotNum() == argSlot)
  1196. {
  1197. if(ownerInstrRef)
  1198. {
  1199. *ownerInstrRef = argInstr;
  1200. }
  1201. return argInstr->GetSrc1();
  1202. }
  1203. } while(argInstr->GetSrc2()->IsSymOpnd());
  1204. return nullptr;
  1205. }
  1206. bool
  1207. Instr::FetchOperands(_Out_writes_(argsOpndLength) IR::Opnd **argsOpnd, uint argsOpndLength)
  1208. {
  1209. return this->ForEachCallDirectArgOutInstrBackward([&](IR::Instr *argOutInstr, uint argNum)
  1210. {
  1211. argsOpnd[argNum] = argOutInstr->GetSrc1();
  1212. return argNum == 0;
  1213. }, argsOpndLength);
  1214. }
  1215. bool Instr::ShouldCheckForNegativeZero() const
  1216. {
  1217. return !ignoreNegativeZero;
  1218. }
  1219. bool Instr::IsDstNotAlwaysConvertedToInt32() const
  1220. {
  1221. return !dstIsAlwaysConvertedToInt32;
  1222. }
  1223. bool Instr::IsDstNotAlwaysConvertedToNumber() const
  1224. {
  1225. return !dstIsAlwaysConvertedToNumber;
  1226. }
  1227. bool Instr::ShouldCheckForIntOverflow() const
  1228. {
  1229. return ShouldCheckFor32BitOverflow() || ShouldCheckForNon32BitOverflow();
  1230. }
  1231. bool Instr::ShouldCheckFor32BitOverflow() const
  1232. {
  1233. return !(ignoreIntOverflow || ignoreIntOverflowInRange);
  1234. }
  1235. bool Instr::ShouldCheckForNon32BitOverflow() const
  1236. {
  1237. return ignoreOverflowBitCount != 32;
  1238. }
  1239. template <typename InstrType> struct IRKindMap;
  1240. template <> struct IRKindMap<IR::Instr> { static const IRKind InstrKind = InstrKindInstr; };
  1241. template <> struct IRKindMap<IR::ProfiledInstr> { static const IRKind InstrKind = InstrKindProfiled; };
  1242. template <> struct IRKindMap<IR::BranchInstr> { static const IRKind InstrKind = InstrKindBranch; };
  1243. template <typename InstrType>
  1244. BailOutInstrTemplate<InstrType> *
  1245. BailOutInstrTemplate<InstrType>::New(Js::OpCode opcode, BailOutKind kind, IR::Instr * bailOutTarget, Func * func)
  1246. {
  1247. Assert(func == bailOutTarget->m_func);
  1248. BailOutInfo * bailOutInfo = JitAnew(func->m_alloc, BailOutInfo, bailOutTarget->GetByteCodeOffset(), func);
  1249. #if ENABLE_DEBUG_CONFIG_OPTIONS
  1250. bailOutInfo->bailOutOpcode = opcode;
  1251. #endif
  1252. return BailOutInstrTemplate::New(opcode, kind, bailOutInfo, func);
  1253. }
  1254. template <typename InstrType>
  1255. BailOutInstrTemplate<InstrType> *
  1256. BailOutInstrTemplate<InstrType>::New(Js::OpCode opcode, IR::Opnd *dst, BailOutKind kind, IR::Instr * bailOutTarget, Func * func)
  1257. {
  1258. BailOutInstrTemplate *instr = BailOutInstrTemplate::New(opcode, kind, bailOutTarget, func);
  1259. instr->SetDst(dst);
  1260. return instr;
  1261. }
  1262. template <typename InstrType>
  1263. BailOutInstrTemplate<InstrType> *
  1264. BailOutInstrTemplate<InstrType>::New(Js::OpCode opcode, IR::Opnd *dst, IR::Opnd *src1, BailOutKind kind, IR::Instr * bailOutTarget, Func * func)
  1265. {
  1266. BailOutInstrTemplate *instr = BailOutInstrTemplate::New(opcode, dst, kind, bailOutTarget, func);
  1267. instr->SetSrc1(src1);
  1268. return instr;
  1269. }
  1270. template <typename InstrType>
  1271. BailOutInstrTemplate<InstrType> *
  1272. BailOutInstrTemplate<InstrType>::New(Js::OpCode opcode, IR::Opnd *dst, IR::Opnd *src1, IR::Opnd *src2, BailOutKind kind, IR::Instr * bailOutTarget, Func * func)
  1273. {
  1274. BailOutInstrTemplate *instr = BailOutInstrTemplate::New(opcode, dst, src1, kind, bailOutTarget, func);
  1275. instr->SetSrc2(src2);
  1276. return instr;
  1277. }
  1278. template <typename InstrType>
  1279. BailOutInstrTemplate<InstrType> *
  1280. BailOutInstrTemplate<InstrType>::New(Js::OpCode opcode, BailOutKind kind, BailOutInfo * bailOutInfo, Func * func)
  1281. {
  1282. Assert(func == bailOutInfo->bailOutFunc);
  1283. Assert(IsValidBailOutKindAndBits(kind));
  1284. BailOutInstrTemplate * bailOutInstr = JitAnew(func->m_alloc, BailOutInstrTemplate);
  1285. bailOutInstr->Init(opcode, IRKindMap<InstrType>::InstrKind, func);
  1286. #if ENABLE_DEBUG_CONFIG_OPTIONS
  1287. bailOutInfo->bailOutOpcode = opcode;
  1288. #endif
  1289. bailOutInstr->bailOutInfo = bailOutInfo;
  1290. bailOutInstr->bailOutKind = kind;
  1291. bailOutInstr->auxBailOutKind = BailOutInvalid;
  1292. if (bailOutInfo->bailOutInstr == nullptr)
  1293. {
  1294. bailOutInfo->bailOutInstr = bailOutInstr;
  1295. }
  1296. else if (bailOutInfo->sharedBailOutKind)
  1297. {
  1298. if (bailOutInfo->bailOutInstr->HasBailOutInfo())
  1299. {
  1300. bailOutInfo->sharedBailOutKind = bailOutInfo->bailOutInstr->GetBailOutKind() == kind;
  1301. }
  1302. else
  1303. {
  1304. // Rare cases where we have already generated the bailout record. Unlikely they share the same bailout kind as this is hit only when we try to
  1305. // share bailout in lowerer. See Instr::ShareBailOut.
  1306. bailOutInfo->sharedBailOutKind = false;
  1307. }
  1308. }
  1309. func->hasBailout = true;
  1310. // Indicate that the function has bailout instructions
  1311. // This information is used to determine whether to free jitted loop bodies
  1312. // If the function has bailout instructions, we keep the loop bodies alive
  1313. // in case we bail out to the interpreter, so that we can reuse the jitted
  1314. // loop bodies
  1315. func->GetJITOutput()->SetHasBailoutInstr(true);
  1316. return bailOutInstr;
  1317. }
  1318. template <typename InstrType>
  1319. BailOutInstrTemplate<InstrType> *
  1320. BailOutInstrTemplate<InstrType>::CloneBailOut() const
  1321. {
  1322. Assert(this->m_func->hasBailout);
  1323. Assert(!this->bailOutInfo->wasCloned);
  1324. BailOutInstrTemplate * bailOutInstr = BailOutInstrTemplate::New(this->m_opcode, this->bailOutKind, this->bailOutInfo, this->bailOutInfo->bailOutFunc);
  1325. bailOutInstr->hasAuxBailOut = this->hasAuxBailOut;
  1326. bailOutInstr->auxBailOutKind = this->auxBailOutKind;
  1327. bailOutInstr->bailOutInfo->wasCloned = true;
  1328. // the new copy is in the slow path and generate the real bailout
  1329. bailOutInstr->bailOutInfo->bailOutInstr = bailOutInstr;
  1330. return bailOutInstr;
  1331. }
  1332. template class BailOutInstrTemplate<IR::Instr>;
  1333. ///----------------------------------------------------------------------------
  1334. ///
  1335. /// EntryInstr::New
  1336. ///
  1337. /// Create an EntryInstr.
  1338. ///
  1339. ///----------------------------------------------------------------------------
  1340. EntryInstr *
  1341. EntryInstr::New(Js::OpCode opcode, Func *func)
  1342. {
  1343. EntryInstr * entryInstr;
  1344. entryInstr = JitAnew(func->m_alloc, IR::EntryInstr);
  1345. entryInstr->Init(opcode, InstrKindEntry, func);
  1346. return entryInstr;
  1347. }
  1348. ///----------------------------------------------------------------------------
  1349. ///
  1350. /// ExitInstr::New
  1351. ///
  1352. /// Create an ExitInstr.
  1353. ///
  1354. ///----------------------------------------------------------------------------
  1355. ExitInstr *
  1356. ExitInstr::New(Js::OpCode opcode, Func *func)
  1357. {
  1358. ExitInstr * exitInstr;
  1359. exitInstr = JitAnew(func->m_alloc, IR::ExitInstr);
  1360. exitInstr->Init(opcode, InstrKindExit, func);
  1361. return exitInstr;
  1362. }
  1363. ///----------------------------------------------------------------------------
  1364. ///
  1365. /// LabelInstr::New
  1366. ///
  1367. /// Create a label.
  1368. ///
  1369. ///----------------------------------------------------------------------------
  1370. LabelInstr *
  1371. LabelInstr::New(Js::OpCode opcode, Func *func, bool isOpHelper)
  1372. {
  1373. LabelInstr * labelInstr;
  1374. labelInstr = JitAnew(func->m_alloc, IR::LabelInstr, func->m_alloc);
  1375. labelInstr->Init(opcode, InstrKindLabel, func, isOpHelper);
  1376. return labelInstr;
  1377. }
  1378. void
  1379. LabelInstr::Init(Js::OpCode opcode, IRKind kind, Func *func, bool isOpHelper)
  1380. {
  1381. // Pass in the region when this is called from anywhere between the Lowerer and EHBailoutPatchUp code?
  1382. __super::Init(opcode, kind, func);
  1383. this->isOpHelper = isOpHelper;
  1384. this->m_pc.pc = nullptr;
  1385. this->m_id = ++(func->GetTopFunc()->m_labelCount);
  1386. AssertMsg(this->m_id != 0, "Label numbers wrapped around?");
  1387. }
  1388. ///----------------------------------------------------------------------------
  1389. ///
  1390. /// LabelInstr::AddLabelRef
  1391. ///
  1392. /// Add a branch to the list of label references.
  1393. ///
  1394. ///----------------------------------------------------------------------------
  1395. void
  1396. LabelInstr::AddLabelRef(BranchInstr *branchRef)
  1397. {
  1398. this->labelRefs.Prepend(branchRef);
  1399. }
  1400. ///----------------------------------------------------------------------------
  1401. ///
  1402. /// LabelInstr::RemoveLabelRef
  1403. ///
  1404. /// Remove a branch from the list of label references.
  1405. ///
  1406. ///----------------------------------------------------------------------------
  1407. void
  1408. LabelInstr::RemoveLabelRef(BranchInstr *branchRef)
  1409. {
  1410. FOREACH_SLISTCOUNTED_ENTRY_EDITING(BranchInstr*, branchEntry, &this->labelRefs, iter)
  1411. {
  1412. if (branchEntry == branchRef)
  1413. {
  1414. iter.RemoveCurrent();
  1415. return;
  1416. }
  1417. } NEXT_SLISTCOUNTED_ENTRY_EDITING;
  1418. AssertMsg(UNREACHED, "Branch not found on labelRef list");
  1419. }
  1420. ///----------------------------------------------------------------------------
  1421. ///
  1422. /// BranchInstr::New
  1423. ///
  1424. /// Create a Br (unconditional) BranchInstr.
  1425. ///
  1426. ///----------------------------------------------------------------------------
  1427. BranchInstr *
  1428. BranchInstr::New(Js::OpCode opcode, LabelInstr * branchTarget, Func *func)
  1429. {
  1430. BranchInstr * branchInstr;
  1431. branchInstr = JitAnew(func->m_alloc, IR::BranchInstr);
  1432. branchInstr->Init(opcode, InstrKindBranch, func);
  1433. branchInstr->SetTarget(branchTarget);
  1434. branchInstr->m_dst = nullptr;
  1435. branchInstr->m_src1 = nullptr;
  1436. branchInstr->m_src2 = nullptr;
  1437. branchInstr->m_byteCodeReg = Js::Constants::NoRegister;
  1438. #if DBG
  1439. branchInstr->m_isHelperToNonHelperBranch = false;
  1440. #endif
  1441. return branchInstr;
  1442. }
  1443. ///----------------------------------------------------------------------------
  1444. ///
  1445. /// BranchInstr::New
  1446. ///
  1447. /// Create a BrB BranchInstr (1-operand conditional branch).
  1448. ///
  1449. ///----------------------------------------------------------------------------
  1450. BranchInstr *
  1451. BranchInstr::New(Js::OpCode opcode, LabelInstr * branchTarget, Opnd *srcOpnd, Func *func)
  1452. {
  1453. BranchInstr * branchInstr;
  1454. branchInstr = BranchInstr::New(opcode, branchTarget, func);
  1455. branchInstr->SetSrc1(srcOpnd);
  1456. return branchInstr;
  1457. }
  1458. ///----------------------------------------------------------------------------
  1459. ///
  1460. /// BranchInstr::New
  1461. ///
  1462. /// Create a BrBReturn BranchInstr (1-operand conditional branch. If condition fails return the result of the condition).
  1463. ///
  1464. ///----------------------------------------------------------------------------
  1465. BranchInstr *
  1466. BranchInstr::New(Js::OpCode opcode, Opnd* destOpnd, LabelInstr * branchTarget, Opnd *srcOpnd, Func *func)
  1467. {
  1468. BranchInstr * branchInstr;
  1469. branchInstr = BranchInstr::New(opcode, branchTarget, func);
  1470. branchInstr->SetSrc1(srcOpnd);
  1471. branchInstr->SetDst(destOpnd);
  1472. return branchInstr;
  1473. }
  1474. ///----------------------------------------------------------------------------
  1475. ///
  1476. /// BranchInstr::New
  1477. ///
  1478. /// Create a BrReg2 BranchInstr (2-operand conditional branch).
  1479. ///
  1480. ///----------------------------------------------------------------------------
  1481. BranchInstr *
  1482. BranchInstr::New(Js::OpCode opcode, LabelInstr * branchTarget, Opnd *src1Opnd, Opnd *src2Opnd, Func *func)
  1483. {
  1484. BranchInstr * branchInstr;
  1485. branchInstr = BranchInstr::New(opcode, branchTarget, src1Opnd, func);
  1486. branchInstr->SetSrc2(src2Opnd);
  1487. return branchInstr;
  1488. }
  1489. ///----------------------------------------------------------------------------
  1490. ///
  1491. /// MultiBranchInstr::New
  1492. ///
  1493. /// Create a MultiBr BranchInstr (unconditional multi branch).
  1494. ///
  1495. ///----------------------------------------------------------------------------
  1496. MultiBranchInstr *
  1497. MultiBranchInstr::New(Js::OpCode opcode, IR::Opnd * srcOpnd, Func * func)
  1498. {
  1499. MultiBranchInstr * multiBranchInstr;
  1500. multiBranchInstr = MultiBranchInstr::New(opcode, func);
  1501. multiBranchInstr->SetSrc1(srcOpnd);
  1502. return multiBranchInstr;
  1503. }
  1504. MultiBranchInstr *
  1505. MultiBranchInstr::New(Js::OpCode opcode, Func * func)
  1506. {
  1507. JitArenaAllocator * m_funcAlloc = func->m_alloc;
  1508. MultiBranchInstr * multiBranchInstr;
  1509. multiBranchInstr = JitAnew(m_funcAlloc, IR::MultiBranchInstr);
  1510. multiBranchInstr->Init(opcode, InstrKindBranch, func);
  1511. return multiBranchInstr;
  1512. }
  1513. bool
  1514. BranchInstr::ReplaceTarget(IR::LabelInstr * oldLabelInstr, IR::LabelInstr * newLabelInstr)
  1515. {
  1516. if (this->IsMultiBranch())
  1517. {
  1518. return this->AsMultiBrInstr()->ReplaceTarget(oldLabelInstr, newLabelInstr);
  1519. }
  1520. if (this->GetTarget() == oldLabelInstr)
  1521. {
  1522. this->SetTarget(newLabelInstr);
  1523. return true;
  1524. }
  1525. return false;
  1526. }
  1527. bool
  1528. MultiBranchInstr::ReplaceTarget(IR::LabelInstr * oldLabelInstr, IR::LabelInstr * newLabelInstr)
  1529. {
  1530. Assert(this->IsMultiBranch());
  1531. bool remapped = false;
  1532. this->UpdateMultiBrLabels([=, &remapped](IR::LabelInstr * targetLabel) -> IR::LabelInstr *
  1533. {
  1534. if (targetLabel == oldLabelInstr)
  1535. {
  1536. this->ChangeLabelRef(targetLabel, newLabelInstr);
  1537. remapped = true;
  1538. return newLabelInstr;
  1539. }
  1540. return targetLabel;
  1541. });
  1542. return remapped;
  1543. }
  1544. void
  1545. MultiBranchInstr::ClearTarget()
  1546. {
  1547. Assert(IsMultiBranch());
  1548. MapMultiBrLabels([&](LabelInstr *const targetLabel)
  1549. {
  1550. ChangeLabelRef(targetLabel, nullptr);
  1551. });
  1552. m_branchTargets = nullptr;
  1553. }
  1554. BranchInstr *
  1555. BranchInstr::CloneBranchInstr() const
  1556. {
  1557. AssertMsg(!this->IsMultiBranch(),"Cloning Not supported for MultiBranchInstr");
  1558. Func * func = this->m_func;
  1559. // See if the target has already been cloned.
  1560. IR::LabelInstr * instrLabel = this->GetTarget()->CloneLabel(false);
  1561. if (instrLabel == nullptr)
  1562. {
  1563. // We didn't find a clone for this label.
  1564. // We'll go back and retarget the cloned branch if the target turns up in the cloned range.
  1565. instrLabel = this->GetTarget();
  1566. func->GetCloner()->fRetargetClonedBranch = TRUE;
  1567. }
  1568. return IR::BranchInstr::New(this->m_opcode, instrLabel, func);
  1569. }
  1570. void
  1571. BranchInstr::Invert()
  1572. {
  1573. /*
  1574. * If one of the operands to a relational operator is 'undefined', the result
  1575. * is always false. Don't invert such branches as they result in a jump to
  1576. * the wrong target.
  1577. */
  1578. switch (this->m_opcode)
  1579. {
  1580. case Js::OpCode::BrGt_A:
  1581. this->m_opcode = Js::OpCode::BrNotGt_A;
  1582. break;
  1583. case Js::OpCode::BrNotGt_A:
  1584. this->m_opcode = Js::OpCode::BrGt_A;
  1585. break;
  1586. case Js::OpCode::BrGe_A:
  1587. this->m_opcode = Js::OpCode::BrNotGe_A;
  1588. break;
  1589. case Js::OpCode::BrNotGe_A:
  1590. this->m_opcode = Js::OpCode::BrGe_A;
  1591. break;
  1592. case Js::OpCode::BrLt_A:
  1593. this->m_opcode = Js::OpCode::BrNotLt_A;
  1594. break;
  1595. case Js::OpCode::BrNotLt_A:
  1596. this->m_opcode = Js::OpCode::BrLt_A;
  1597. break;
  1598. case Js::OpCode::BrLe_A:
  1599. this->m_opcode = Js::OpCode::BrNotLe_A;
  1600. break;
  1601. case Js::OpCode::BrNotLe_A:
  1602. this->m_opcode = Js::OpCode::BrLe_A;
  1603. break;
  1604. case Js::OpCode::BrEq_A:
  1605. this->m_opcode = Js::OpCode::BrNotEq_A;
  1606. break;
  1607. case Js::OpCode::BrNotEq_A:
  1608. this->m_opcode = Js::OpCode::BrEq_A;
  1609. break;
  1610. case Js::OpCode::BrNeq_A:
  1611. this->m_opcode = Js::OpCode::BrNotNeq_A;
  1612. break;
  1613. case Js::OpCode::BrNotNeq_A:
  1614. this->m_opcode = Js::OpCode::BrNeq_A;
  1615. break;
  1616. case Js::OpCode::Br:
  1617. break;
  1618. case Js::OpCode::BrFalse_A:
  1619. this->m_opcode = Js::OpCode::BrTrue_A;
  1620. break;
  1621. case Js::OpCode::BrTrue_A:
  1622. this->m_opcode = Js::OpCode::BrFalse_A;
  1623. break;
  1624. case Js::OpCode::BrSrEq_A:
  1625. this->m_opcode = Js::OpCode::BrSrNotEq_A;
  1626. break;
  1627. case Js::OpCode::BrSrNotEq_A:
  1628. this->m_opcode = Js::OpCode::BrSrEq_A;
  1629. break;
  1630. case Js::OpCode::BrSrNeq_A:
  1631. this->m_opcode = Js::OpCode::BrSrNotNeq_A;
  1632. break;
  1633. case Js::OpCode::BrSrNotNeq_A:
  1634. this->m_opcode = Js::OpCode::BrSrNeq_A;
  1635. break;
  1636. case Js::OpCode::BrOnHasProperty:
  1637. this->m_opcode = Js::OpCode::BrOnNoProperty;
  1638. break;
  1639. case Js::OpCode::BrOnNoProperty:
  1640. this->m_opcode = Js::OpCode::BrOnHasProperty;
  1641. break;
  1642. case Js::OpCode::BrTrue_I4:
  1643. this->m_opcode = Js::OpCode::BrFalse_I4;
  1644. break;
  1645. case Js::OpCode::BrFalse_I4:
  1646. this->m_opcode = Js::OpCode::BrTrue_I4;
  1647. break;
  1648. case Js::OpCode::BrEq_I4:
  1649. this->m_opcode = Js::OpCode::BrNeq_I4;
  1650. break;
  1651. case Js::OpCode::BrNeq_I4:
  1652. this->m_opcode = Js::OpCode::BrEq_I4;
  1653. break;
  1654. case Js::OpCode::BrGe_I4:
  1655. this->m_opcode = Js::OpCode::BrLt_I4;
  1656. break;
  1657. case Js::OpCode::BrGt_I4:
  1658. this->m_opcode = Js::OpCode::BrLe_I4;
  1659. break;
  1660. case Js::OpCode::BrLe_I4:
  1661. this->m_opcode = Js::OpCode::BrGt_I4;
  1662. break;
  1663. case Js::OpCode::BrLt_I4:
  1664. this->m_opcode = Js::OpCode::BrGe_I4;
  1665. break;
  1666. case Js::OpCode::BrUnGe_A:
  1667. this->m_opcode = Js::OpCode::BrUnLt_A;
  1668. break;
  1669. case Js::OpCode::BrUnGt_A:
  1670. this->m_opcode = Js::OpCode::BrUnLe_A;
  1671. break;
  1672. case Js::OpCode::BrUnLe_A:
  1673. this->m_opcode = Js::OpCode::BrUnGt_A;
  1674. break;
  1675. case Js::OpCode::BrUnLt_A:
  1676. this->m_opcode = Js::OpCode::BrUnGe_A;
  1677. break;
  1678. case Js::OpCode::BrUnGe_I4:
  1679. this->m_opcode = Js::OpCode::BrUnLt_I4;
  1680. break;
  1681. case Js::OpCode::BrUnGt_I4:
  1682. this->m_opcode = Js::OpCode::BrUnLe_I4;
  1683. break;
  1684. case Js::OpCode::BrUnLe_I4:
  1685. this->m_opcode = Js::OpCode::BrUnGt_I4;
  1686. break;
  1687. case Js::OpCode::BrUnLt_I4:
  1688. this->m_opcode = Js::OpCode::BrUnGe_I4;
  1689. break;
  1690. case Js::OpCode::BrOnEmpty:
  1691. this->m_opcode = Js::OpCode::BrOnNotEmpty;
  1692. break;
  1693. case Js::OpCode::BrOnNotEmpty:
  1694. this->m_opcode = Js::OpCode::BrOnEmpty;
  1695. break;
  1696. case Js::OpCode::BrHasSideEffects:
  1697. this->m_opcode = Js::OpCode::BrNotHasSideEffects;
  1698. break;
  1699. case Js::OpCode::BrFncEqApply:
  1700. this->m_opcode = Js::OpCode::BrFncNeqApply;
  1701. break;
  1702. case Js::OpCode::BrFncNeqApply:
  1703. this->m_opcode = Js::OpCode::BrFncEqApply;
  1704. break;
  1705. case Js::OpCode::BrNotHasSideEffects:
  1706. this->m_opcode = Js::OpCode::BrHasSideEffects;
  1707. break;
  1708. case Js::OpCode::BrNotAddr_A:
  1709. this->m_opcode = Js::OpCode::BrAddr_A;
  1710. break;
  1711. case Js::OpCode::BrAddr_A:
  1712. this->m_opcode = Js::OpCode::BrNotAddr_A;
  1713. break;
  1714. case Js::OpCode::BrFncCachedScopeEq:
  1715. this->m_opcode = Js::OpCode::BrFncCachedScopeNeq;
  1716. break;
  1717. case Js::OpCode::BrFncCachedScopeNeq:
  1718. this->m_opcode = Js::OpCode::BrFncCachedScopeEq;
  1719. break;
  1720. case Js::OpCode::BrOnException:
  1721. this->m_opcode = Js::OpCode::BrOnNoException;
  1722. break;
  1723. default:
  1724. AssertMsg(UNREACHED, "Unhandled branch");
  1725. }
  1726. }
  1727. bool
  1728. BranchInstr::IsLoopTail(Func * func)
  1729. {
  1730. Assert(func->isPostLower);
  1731. IR::LabelInstr * target = this->GetTarget();
  1732. if (!target->m_isLoopTop)
  1733. {
  1734. return false;
  1735. }
  1736. IR::BranchInstr * lastBranchInstr = nullptr;
  1737. uint32 lastBranchNum = 0;
  1738. FOREACH_SLISTCOUNTED_ENTRY(IR::BranchInstr *, ref, &target->labelRefs)
  1739. {
  1740. if (ref->GetNumber() > lastBranchNum)
  1741. {
  1742. lastBranchInstr = ref;
  1743. lastBranchNum = lastBranchInstr->GetNumber();
  1744. }
  1745. }
  1746. NEXT_SLISTCOUNTED_ENTRY;
  1747. if (this == lastBranchInstr)
  1748. {
  1749. return true;
  1750. }
  1751. return false;
  1752. }
  1753. ///----------------------------------------------------------------------------
  1754. ///
  1755. /// PragmaInstr::New
  1756. ///
  1757. /// Create a PragmaInstr.
  1758. ///
  1759. ///----------------------------------------------------------------------------
  1760. PragmaInstr *
  1761. PragmaInstr::New(Js::OpCode opcode, uint32 index, Func *func)
  1762. {
  1763. PragmaInstr * pragmaInstr;
  1764. pragmaInstr = JitAnew(func->m_alloc, IR::PragmaInstr);
  1765. pragmaInstr->Init(opcode, InstrKindPragma, func);
  1766. pragmaInstr->m_statementIndex = index;
  1767. return pragmaInstr;
  1768. }
  1769. ///----------------------------------------------------------------------------
  1770. ///
  1771. /// PragmaInstr::Instr
  1772. ///
  1773. /// Record the information encoded in the pragma
  1774. ///
  1775. ///----------------------------------------------------------------------------
  1776. #if DBG_DUMP | defined(VTUNE_PROFILING)
  1777. void
  1778. PragmaInstr::Record(uint32 nativeBufferOffset)
  1779. {
  1780. // Currently the only pragma instructions are for Source Info
  1781. Assert(this->m_func->GetTopFunc()->DoRecordNativeMap());
  1782. if (!m_func->IsOOPJIT())
  1783. {
  1784. m_func->GetTopFunc()->GetInProcJITEntryPointInfo()->RecordNativeMap(nativeBufferOffset, m_statementIndex);
  1785. }
  1786. }
  1787. #endif
  1788. ///----------------------------------------------------------------------------
  1789. ///
  1790. /// Instr::New
  1791. ///
  1792. /// Create an Instr.
  1793. ///
  1794. ///----------------------------------------------------------------------------
  1795. Instr *
  1796. Instr::New(Js::OpCode opcode, Func *func)
  1797. {
  1798. Instr * instr;
  1799. instr = JitAnew(func->m_alloc, IR::Instr);
  1800. instr->Init(opcode, InstrKindInstr, func);
  1801. return instr;
  1802. }
  1803. ///----------------------------------------------------------------------------
  1804. ///
  1805. /// Instr::New
  1806. ///
  1807. /// Create an Instr with a byte code offset.
  1808. ///
  1809. ///----------------------------------------------------------------------------
  1810. Instr *
  1811. Instr::New(Js::OpCode opcode, Func *func, IR::Instr * bytecodeOffsetInstr)
  1812. {
  1813. Instr * instr = Instr::New(opcode, func);
  1814. instr->SetByteCodeOffset(bytecodeOffsetInstr);
  1815. return instr;
  1816. }
  1817. ///----------------------------------------------------------------------------
  1818. ///
  1819. /// Instr::New
  1820. ///
  1821. /// Create an Instr with dst.
  1822. ///
  1823. ///----------------------------------------------------------------------------
  1824. Instr *
  1825. Instr::New(Js::OpCode opcode, Opnd *dstOpnd, Func *func)
  1826. {
  1827. Instr * instr;
  1828. instr = Instr::New(opcode, func);
  1829. if (dstOpnd)
  1830. {
  1831. instr->SetDst(dstOpnd);
  1832. }
  1833. return instr;
  1834. }
  1835. ///----------------------------------------------------------------------------
  1836. ///
  1837. /// Instr::New
  1838. ///
  1839. /// Create an Instr with dst and a src.
  1840. ///
  1841. ///----------------------------------------------------------------------------
  1842. Instr *
  1843. Instr::New(Js::OpCode opcode, Opnd *dstOpnd, Opnd *src1Opnd, Func *func)
  1844. {
  1845. Instr * instr;
  1846. instr = Instr::New(opcode, dstOpnd, func);
  1847. instr->SetSrc1(src1Opnd);
  1848. return instr;
  1849. }
  1850. ///----------------------------------------------------------------------------
  1851. ///
  1852. /// Instr::New
  1853. ///
  1854. /// Create an Instr with dst and 2 srcs.
  1855. ///
  1856. ///----------------------------------------------------------------------------
  1857. Instr *
  1858. Instr::New(Js::OpCode opcode, Opnd *dstOpnd, Opnd *src1Opnd, Opnd *src2Opnd, Func *func)
  1859. {
  1860. Instr * instr;
  1861. instr = Instr::New(opcode, dstOpnd, src1Opnd, func);
  1862. instr->SetSrc2(src2Opnd);
  1863. return instr;
  1864. }
  1865. ///----------------------------------------------------------------------------
  1866. ///
  1867. /// Instr::SetDst
  1868. ///
  1869. /// Set the dst for 'this' instruction. Automatically maintain isSingleDef
  1870. /// and instrDef of stackSyms.
  1871. ///
  1872. ///----------------------------------------------------------------------------
  1873. Opnd *
  1874. Instr::SetDst(Opnd * newDst)
  1875. {
  1876. AssertMsg(newDst != nullptr, "Calling SetDst with a NULL dst");
  1877. AssertMsg(this->m_dst == nullptr, "Calling SetDst without unlinking/freeing the current dst");
  1878. Assert(!(newDst->IsRegOpnd() && newDst->AsRegOpnd()->IsSymValueFrozen()));
  1879. newDst = newDst->Use(m_func);
  1880. this->m_dst = newDst;
  1881. // If newDst isSingleDef, set instrDef
  1882. StackSym *stackSym;
  1883. if (newDst->IsRegOpnd() && newDst->AsRegOpnd()->m_sym)
  1884. {
  1885. stackSym = newDst->AsRegOpnd()->m_sym->AsStackSym();
  1886. }
  1887. else if (newDst->IsSymOpnd() && newDst->AsSymOpnd()->m_sym->IsStackSym())
  1888. {
  1889. stackSym = newDst->AsSymOpnd()->m_sym->AsStackSym();
  1890. }
  1891. else
  1892. {
  1893. stackSym = nullptr;
  1894. }
  1895. if (stackSym && stackSym->m_isSingleDef)
  1896. {
  1897. if (stackSym->m_instrDef)
  1898. {
  1899. AssertMsg(!stackSym->IsArgSlotSym(), "Arg Slot sym needs to be single def to maintain the StartCall arg links");
  1900. // Multiple defs, clear isSingleDef flag
  1901. stackSym->m_isSingleDef = false;
  1902. stackSym->m_instrDef = nullptr;
  1903. stackSym->m_isConst = false;
  1904. stackSym->m_isIntConst = false;
  1905. stackSym->m_isInt64Const= false;
  1906. stackSym->m_isTaggableIntConst = false;
  1907. stackSym->m_isNotNumber = false;
  1908. stackSym->m_isStrConst = false;
  1909. stackSym->m_isStrEmpty = false;
  1910. stackSym->m_isFltConst = false;
  1911. }
  1912. else
  1913. {
  1914. stackSym->m_instrDef = this;
  1915. }
  1916. }
  1917. return newDst;
  1918. }
  1919. Opnd *
  1920. Instr::SetFakeDst(Opnd * newDst)
  1921. {
  1922. AssertMsg(newDst != nullptr, "Calling SetDst with a NULL dst");
  1923. AssertMsg(this->m_dst == nullptr, "Calling SetDst without unlinking/freeing the current dst");
  1924. Assert(!(newDst->IsRegOpnd() && newDst->AsRegOpnd()->IsSymValueFrozen()));
  1925. newDst = newDst->Use(m_func);
  1926. this->m_dst = newDst;
  1927. #if DBG
  1928. newDst->isFakeDst = true;
  1929. #endif
  1930. return newDst;
  1931. }
  1932. ///----------------------------------------------------------------------------
  1933. ///
  1934. /// Instr::UnlinkDst
  1935. ///
  1936. /// Unlinks the dst for 'this' instruction. Automatically maintains
  1937. /// instrDef of stackSyms.
  1938. ///
  1939. ///----------------------------------------------------------------------------
  1940. Opnd *
  1941. Instr::UnlinkDst()
  1942. {
  1943. Opnd * oldDst = this->m_dst;
  1944. StackSym *stackSym = nullptr;
  1945. // If oldDst isSingleDef, clear instrDef
  1946. if (oldDst->IsRegOpnd())
  1947. {
  1948. stackSym = oldDst->AsRegOpnd()->m_sym;
  1949. }
  1950. else if (oldDst->IsSymOpnd())
  1951. {
  1952. Sym *sym = oldDst->AsSymOpnd()->m_sym;
  1953. if (sym->IsStackSym())
  1954. {
  1955. stackSym = sym->AsStackSym();
  1956. }
  1957. }
  1958. if (stackSym && stackSym->m_isSingleDef)
  1959. {
  1960. if (stackSym->m_instrDef == this)
  1961. {
  1962. stackSym->m_instrDef = nullptr;
  1963. }
  1964. else
  1965. {
  1966. Assert(oldDst->isFakeDst);
  1967. }
  1968. }
  1969. #if DBG
  1970. if (oldDst->isFakeDst)
  1971. {
  1972. oldDst->isFakeDst = false;
  1973. }
  1974. #endif
  1975. oldDst->UnUse();
  1976. this->m_dst = nullptr;
  1977. return oldDst;
  1978. }
  1979. ///----------------------------------------------------------------------------
  1980. ///
  1981. /// Instr::FreeDst
  1982. ///
  1983. /// Unlinks and free the dst for 'this' instruction.
  1984. ///
  1985. ///----------------------------------------------------------------------------
  1986. void
  1987. Instr::FreeDst()
  1988. {
  1989. Opnd * unlinkedDst;
  1990. unlinkedDst = this->UnlinkDst();
  1991. unlinkedDst->Free(this->m_func);
  1992. }
  1993. ///----------------------------------------------------------------------------
  1994. ///
  1995. /// Instr::ReplaceDst
  1996. ///
  1997. /// Unlink this dst from this instr, free it, and replace it with newDst.
  1998. /// The new dst is returned.
  1999. ///
  2000. ///----------------------------------------------------------------------------
  2001. Opnd *
  2002. Instr::ReplaceDst(Opnd * newDst)
  2003. {
  2004. this->FreeDst();
  2005. return this->SetDst(newDst);
  2006. }
  2007. ///----------------------------------------------------------------------------
  2008. ///
  2009. /// Instr::SinkDst
  2010. ///
  2011. /// Replace current dst with new symbol, and assign new symbol using the
  2012. /// given opcode to the previous dst.
  2013. ///
  2014. ///----------------------------------------------------------------------------
  2015. Instr *
  2016. Instr::SinkDst(Js::OpCode assignOpcode, RegNum regNum, IR::Instr *insertAfterInstr)
  2017. {
  2018. return SinkDst(assignOpcode, StackSym::New(this->GetDst()->GetType(), m_func), regNum, insertAfterInstr);
  2019. }
  2020. Instr *
  2021. Instr::SinkDst(Js::OpCode assignOpcode, StackSym * stackSym, RegNum regNum, IR::Instr *insertAfterInstr)
  2022. {
  2023. if(!insertAfterInstr)
  2024. {
  2025. insertAfterInstr = this;
  2026. }
  2027. Opnd *oldDst, *newDst;
  2028. Instr * newInstr;
  2029. IRType type;
  2030. oldDst = this->UnlinkDst();
  2031. type = oldDst->GetType();
  2032. newDst = this->SetDst(RegOpnd::New(stackSym, regNum, type, m_func));
  2033. newInstr = Instr::New(assignOpcode, oldDst, newDst, m_func);
  2034. insertAfterInstr->InsertAfter(newInstr);
  2035. return newInstr;
  2036. }
  2037. IR::Instr *
  2038. Instr::SinkInstrBefore(IR::Instr * instrTarget)
  2039. {
  2040. // Move this instruction down to the target location, preserving
  2041. // the use(s), if necessary, from redefinition between the original
  2042. // location and the new one.
  2043. if (this->m_next == instrTarget)
  2044. {
  2045. return this->m_prev;
  2046. }
  2047. StackSym *sym;
  2048. if (this->m_src1)
  2049. {
  2050. sym = this->m_src1->GetStackSym();
  2051. if (sym && !sym->m_isSingleDef)
  2052. {
  2053. this->HoistSrc1(Js::OpCode::Ld_A);
  2054. }
  2055. if (this->m_src2)
  2056. {
  2057. sym = this->m_src2->GetStackSym();
  2058. if (sym && !sym->m_isSingleDef)
  2059. {
  2060. this->HoistSrc2(Js::OpCode::Ld_A);
  2061. }
  2062. }
  2063. }
  2064. // Move the instruction down to the target. Return the instruction
  2065. // that preceded the sunk instruction at its original location.
  2066. // (This lets the caller find a Ld_A that this call inserted.)
  2067. IR::Instr * instrPrev = this->m_prev;
  2068. this->Unlink();
  2069. instrTarget->InsertBefore(this);
  2070. return instrPrev;
  2071. }
  2072. ///----------------------------------------------------------------------------
  2073. ///
  2074. /// Instr::UnlinkSrc1
  2075. ///
  2076. /// Unlinks the src1 for 'this' instruction.
  2077. ///
  2078. ///----------------------------------------------------------------------------
  2079. Opnd *
  2080. Instr::UnlinkSrc1()
  2081. {
  2082. Opnd * oldSrc = this->m_src1;
  2083. oldSrc->UnUse();
  2084. this->m_src1 = nullptr;
  2085. return oldSrc;
  2086. }
  2087. ///----------------------------------------------------------------------------
  2088. ///
  2089. /// Instr::FreeSrc1
  2090. ///
  2091. /// Unlinks and free the src1 for 'this' instruction.
  2092. ///
  2093. ///----------------------------------------------------------------------------
  2094. void
  2095. Instr::FreeSrc1()
  2096. {
  2097. Opnd * unlinkedSrc;
  2098. unlinkedSrc = this->UnlinkSrc1();
  2099. unlinkedSrc->Free(this->m_func);
  2100. }
  2101. ///----------------------------------------------------------------------------
  2102. ///
  2103. /// Instr::ReplaceSrc1
  2104. ///
  2105. /// Unlink src1 from this instr, free it, and replace it with newSrc.
  2106. /// The new src is returned.
  2107. ///
  2108. ///----------------------------------------------------------------------------
  2109. Opnd *
  2110. Instr::ReplaceSrc1(Opnd * newSrc)
  2111. {
  2112. this->FreeSrc1();
  2113. return this->SetSrc1(newSrc);
  2114. }
  2115. ///----------------------------------------------------------------------------
  2116. ///
  2117. /// Instr::HoistSrc1
  2118. ///
  2119. /// Replace current src with new symbol, and assign new symbol using the
  2120. /// given opcode from the previous src.
  2121. ///
  2122. ///----------------------------------------------------------------------------
  2123. Instr *
  2124. Instr::HoistSrc1(Js::OpCode assignOpcode, RegNum regNum, StackSym *newSym)
  2125. {
  2126. Opnd *oldSrc, *newSrc;
  2127. Instr * newInstr;
  2128. IRType type;
  2129. oldSrc = this->UnlinkSrc1();
  2130. type = oldSrc->GetType();
  2131. const bool creatingNewSym = !newSym;
  2132. if(creatingNewSym)
  2133. {
  2134. newSym = StackSym::New(type, m_func);
  2135. }
  2136. newSrc = this->SetSrc1(RegOpnd::New(newSym, regNum, type, m_func));
  2137. newSrc->SetValueType(oldSrc->GetValueType());
  2138. newInstr = Instr::New(assignOpcode, newSrc, oldSrc, m_func);
  2139. this->InsertBefore(newInstr);
  2140. if(creatingNewSym)
  2141. {
  2142. if (oldSrc->IsRegOpnd())
  2143. {
  2144. newSym->CopySymAttrs(oldSrc->AsRegOpnd()->m_sym);
  2145. }
  2146. else if (oldSrc->IsImmediateOpnd())
  2147. {
  2148. newSym->SetIsConst();
  2149. }
  2150. }
  2151. return newInstr;
  2152. }
  2153. ///----------------------------------------------------------------------------
  2154. ///
  2155. /// Instr::UnlinkSrc2
  2156. ///
  2157. /// Unlinks the src2 for 'this' instruction.
  2158. ///
  2159. ///----------------------------------------------------------------------------
  2160. Opnd *
  2161. Instr::UnlinkSrc2()
  2162. {
  2163. Opnd * oldSrc = this->m_src2;
  2164. oldSrc->UnUse();
  2165. this->m_src2 = nullptr;
  2166. return oldSrc;
  2167. }
  2168. ///----------------------------------------------------------------------------
  2169. ///
  2170. /// Instr::FreeSrc2
  2171. ///
  2172. /// Unlinks and free the src2 for 'this' instruction.
  2173. ///
  2174. ///----------------------------------------------------------------------------
  2175. void
  2176. Instr::FreeSrc2()
  2177. {
  2178. Opnd * unlinkedSrc;
  2179. unlinkedSrc = this->UnlinkSrc2();
  2180. unlinkedSrc->Free(this->m_func);
  2181. }
  2182. ///----------------------------------------------------------------------------
  2183. ///
  2184. /// Instr::ReplaceSrc2
  2185. ///
  2186. /// Unlink src2 from this instr, free it, and replace it with newSrc.
  2187. /// The new src is returned.
  2188. ///
  2189. ///----------------------------------------------------------------------------
  2190. Opnd *
  2191. Instr::ReplaceSrc2(Opnd * newSrc)
  2192. {
  2193. this->FreeSrc2();
  2194. return this->SetSrc2(newSrc);
  2195. }
  2196. ///----------------------------------------------------------------------------
  2197. ///
  2198. /// Instr::HoistSrc2
  2199. ///
  2200. /// Replace current src with new symbol, and assign new symbol using the
  2201. /// given opcode from the previous src.
  2202. ///
  2203. ///----------------------------------------------------------------------------
  2204. Instr *
  2205. Instr::HoistSrc2(Js::OpCode assignOpcode, RegNum regNum, StackSym *newSym)
  2206. {
  2207. Opnd *oldSrc, *newSrc;
  2208. Instr * newInstr;
  2209. IRType type;
  2210. oldSrc = this->UnlinkSrc2();
  2211. type = oldSrc->GetType();
  2212. const bool creatingNewSym = !newSym;
  2213. if(creatingNewSym)
  2214. {
  2215. newSym = StackSym::New(type, m_func);
  2216. }
  2217. newSrc = this->SetSrc2(RegOpnd::New(newSym, regNum, type, m_func));
  2218. newSrc->SetValueType(oldSrc->GetValueType());
  2219. newInstr = Instr::New(assignOpcode, newSrc, oldSrc, m_func);
  2220. this->InsertBefore(newInstr);
  2221. if(creatingNewSym)
  2222. {
  2223. if (oldSrc->IsRegOpnd())
  2224. {
  2225. newSym->CopySymAttrs(oldSrc->AsRegOpnd()->m_sym);
  2226. }
  2227. else if (oldSrc->IsIntConstOpnd())
  2228. {
  2229. newSym->SetIsIntConst(oldSrc->AsIntConstOpnd()->GetValue());
  2230. }
  2231. }
  2232. return newInstr;
  2233. }
  2234. IndirOpnd *
  2235. Instr::HoistMemRefAddress(MemRefOpnd *const memRefOpnd, const Js::OpCode loadOpCode)
  2236. {
  2237. Assert(memRefOpnd);
  2238. #if defined(_M_IX86) || defined(_M_X64)
  2239. Assert(!LowererMDArch::IsLegalMemLoc(memRefOpnd));
  2240. #endif
  2241. intptr_t address = memRefOpnd->GetMemLoc();
  2242. IR::AddrOpndKind kind = memRefOpnd->GetAddrKind();
  2243. Func *const func = m_func;
  2244. IR::AddrOpnd * addrOpnd = IR::AddrOpnd::New(address, kind, this->m_func, true);
  2245. IR::IndirOpnd * indirOpnd = func->GetTopFunc()->GetConstantAddressIndirOpnd(address, addrOpnd, kind, memRefOpnd->GetType(), loadOpCode);
  2246. if (indirOpnd == nullptr)
  2247. {
  2248. IR::RegOpnd * addressRegOpnd = IR::RegOpnd::New(TyMachPtr, func);
  2249. IR::Instr *const newInstr =
  2250. IR::Instr::New(
  2251. loadOpCode,
  2252. addressRegOpnd,
  2253. IR::AddrOpnd::New(address, kind, func, true),
  2254. func);
  2255. InsertBefore(newInstr);
  2256. indirOpnd = IR::IndirOpnd::New(addressRegOpnd, 0, memRefOpnd->GetType(), func, true);
  2257. #if DBG_DUMP
  2258. // TODO: michhol oop jit, make intptr
  2259. indirOpnd->SetAddrKind(kind, (void*)address);
  2260. #endif
  2261. }
  2262. return DeepReplace(memRefOpnd, indirOpnd)->AsIndirOpnd();
  2263. }
  2264. Opnd *
  2265. Instr::Replace(Opnd *oldOpnd, Opnd *newOpnd)
  2266. {
  2267. if (oldOpnd == this->GetDst())
  2268. {
  2269. return this->ReplaceDst(newOpnd);
  2270. }
  2271. else
  2272. {
  2273. return this->ReplaceSrc(oldOpnd, newOpnd);
  2274. }
  2275. }
  2276. Opnd *Instr::DeepReplace(Opnd *const oldOpnd, Opnd *const newOpnd)
  2277. {
  2278. Assert(oldOpnd);
  2279. Assert(newOpnd);
  2280. IR::Opnd *opnd = GetDst();
  2281. if(opnd && oldOpnd != opnd && oldOpnd->IsEqual(opnd))
  2282. {
  2283. ReplaceDst(newOpnd);
  2284. }
  2285. opnd = GetSrc1();
  2286. if(opnd && oldOpnd != opnd && oldOpnd->IsEqual(opnd))
  2287. {
  2288. ReplaceSrc1(newOpnd);
  2289. }
  2290. opnd = GetSrc2();
  2291. if(opnd && oldOpnd != opnd && oldOpnd->IsEqual(opnd))
  2292. {
  2293. ReplaceSrc2(newOpnd);
  2294. }
  2295. // Do this last because Replace will delete oldOpnd
  2296. return Replace(oldOpnd, newOpnd);
  2297. }
  2298. Opnd *
  2299. Instr::UnlinkSrc(Opnd *src)
  2300. {
  2301. if (src == this->GetSrc1())
  2302. {
  2303. return this->UnlinkSrc1();
  2304. }
  2305. else
  2306. {
  2307. AssertMsg(src == this->GetSrc2(), "Src not found");
  2308. return this->UnlinkSrc2();
  2309. }
  2310. }
  2311. ///----------------------------------------------------------------------------
  2312. ///
  2313. /// Instr::ReplaceSrc
  2314. ///
  2315. /// Unlink oldSrc from this instr, free it, and replace it with newSrc.
  2316. /// The new src is returned.
  2317. ///
  2318. ///----------------------------------------------------------------------------
  2319. Opnd *
  2320. Instr::ReplaceSrc(Opnd *oldSrc, Opnd * newSrc)
  2321. {
  2322. if (oldSrc == this->GetSrc1())
  2323. {
  2324. return this->ReplaceSrc1(newSrc);
  2325. }
  2326. else
  2327. {
  2328. AssertMsg(oldSrc == this->GetSrc2(), "OldSrc not found");
  2329. return this->ReplaceSrc2(newSrc);
  2330. }
  2331. }
  2332. ///----------------------------------------------------------------------------
  2333. ///
  2334. /// Instr::IsRealInstr
  2335. ///
  2336. /// Does this instr generate code?
  2337. ///
  2338. ///----------------------------------------------------------------------------
  2339. bool
  2340. Instr::IsRealInstr() const
  2341. {
  2342. switch (m_opcode)
  2343. {
  2344. case Js::OpCode::Label:
  2345. case Js::OpCode::StatementBoundary:
  2346. case Js::OpCode::NoImplicitCallUses:
  2347. case Js::OpCode::NoIntOverflowBoundary:
  2348. #if DBG
  2349. case Js::OpCode::CheckLowerIntBound:
  2350. case Js::OpCode::CheckUpperIntBound:
  2351. #endif
  2352. return false;
  2353. default:
  2354. return true;
  2355. }
  2356. }
  2357. ///----------------------------------------------------------------------------
  2358. ///
  2359. /// Instr::GetNextRealInstr
  2360. ///
  2361. ///----------------------------------------------------------------------------
  2362. IR::Instr *
  2363. Instr::GetNextRealInstr() const
  2364. {
  2365. IR::Instr *instr = this->m_next;
  2366. while (instr != nullptr && !instr->IsRealInstr())
  2367. {
  2368. AssertMsg(instr->m_next || instr->IsPragmaInstr(), "GetNextRealInstr() failed...");
  2369. instr = instr->m_next;
  2370. }
  2371. return instr;
  2372. }
  2373. #if DBG
  2374. IR::LabelInstr *
  2375. Instr::GetNextNonEmptyLabel() const
  2376. {
  2377. IR::Instr *instr = const_cast<Instr*>(this);
  2378. while (instr != nullptr && (!instr->IsLabelInstr() || instr->m_next->IsLabelInstr()))
  2379. {
  2380. instr = instr->m_next;
  2381. }
  2382. return instr->AsLabelInstr();
  2383. }
  2384. #endif
  2385. ///----------------------------------------------------------------------------
  2386. ///
  2387. /// Instr::GetNextRealInstrOrLabel
  2388. ///
  2389. ///----------------------------------------------------------------------------
  2390. IR::Instr *
  2391. Instr::GetNextRealInstrOrLabel() const
  2392. {
  2393. IR::Instr *instr = this->m_next;
  2394. while (instr != nullptr && !instr->IsLabelInstr() && !instr->IsRealInstr())
  2395. {
  2396. instr = instr->m_next;
  2397. AssertMsg(instr, "GetNextRealInstrOrLabel() failed...");
  2398. }
  2399. return instr;
  2400. }
  2401. IR::Instr *
  2402. Instr::GetNextBranchOrLabel() const
  2403. {
  2404. IR::Instr *instr = this->m_next;
  2405. while (instr != nullptr && !instr->IsLabelInstr() && !instr->IsBranchInstr())
  2406. {
  2407. instr = instr->m_next;
  2408. }
  2409. return instr;
  2410. }
  2411. IR::Instr *
  2412. Instr::GetNextByteCodeInstr() const
  2413. {
  2414. IR::Instr * nextInstr = GetNextRealInstrOrLabel();
  2415. uint32 currentOffset = GetByteCodeOffset();
  2416. const auto getNext = [](IR::Instr* nextInstr) -> IR::Instr*
  2417. {
  2418. if (nextInstr->IsBranchInstr())
  2419. {
  2420. IR::BranchInstr* branchInstr = nextInstr->AsBranchInstr();
  2421. AssertMsg(branchInstr->IsUnconditional(), "We can't know which branch to take on a conditionnal branch");
  2422. if (branchInstr->IsUnconditional())
  2423. {
  2424. return branchInstr->GetTarget();
  2425. }
  2426. }
  2427. return nextInstr->GetNextRealInstrOrLabel();
  2428. };
  2429. while (nextInstr->GetByteCodeOffset() == Js::Constants::NoByteCodeOffset ||
  2430. nextInstr->GetByteCodeOffset() == currentOffset)
  2431. {
  2432. nextInstr = getNext(nextInstr);
  2433. }
  2434. // Do not check if the instr trying to bailout is in the function prologue
  2435. // nextInstr->GetByteCodeOffset() < currentOffset would always be true and we would crash
  2436. if (currentOffset != Js::Constants::NoByteCodeOffset)
  2437. {
  2438. // This can happen due to break block removal
  2439. while (nextInstr->GetByteCodeOffset() == Js::Constants::NoByteCodeOffset ||
  2440. nextInstr->GetByteCodeOffset() < currentOffset)
  2441. {
  2442. nextInstr = getNext(nextInstr);
  2443. }
  2444. }
  2445. else
  2446. {
  2447. AssertMsg(nextInstr->GetByteCodeOffset() == 0, "Only instrs before the first one are allowed to not have a bytecode offset");
  2448. }
  2449. return nextInstr;
  2450. }
  2451. ///----------------------------------------------------------------------------
  2452. ///
  2453. /// Instr::GetPrevRealInstr
  2454. ///
  2455. ///----------------------------------------------------------------------------
  2456. IR::Instr *
  2457. Instr::GetPrevRealInstr() const
  2458. {
  2459. IR::Instr *instr = this->m_prev;
  2460. while (!instr->IsRealInstr())
  2461. {
  2462. instr = instr->m_prev;
  2463. AssertMsg(instr, "GetPrevRealInstr() failed...");
  2464. }
  2465. return instr;
  2466. }
  2467. ///----------------------------------------------------------------------------
  2468. ///
  2469. /// Instr::GetPrevRealInstrOrLabel
  2470. ///
  2471. ///----------------------------------------------------------------------------
  2472. IR::Instr *
  2473. Instr::GetPrevRealInstrOrLabel() const
  2474. {
  2475. IR::Instr *instr = this->m_prev;
  2476. while (!instr->IsLabelInstr() && !instr->IsRealInstr())
  2477. {
  2478. instr = instr->m_prev;
  2479. AssertMsg(instr, "GetPrevRealInstrOrLabel() failed...");
  2480. }
  2481. return instr;
  2482. }
  2483. ///----------------------------------------------------------------------------
  2484. ///
  2485. /// Instr::GetPrevLabelInstr
  2486. ///
  2487. ///----------------------------------------------------------------------------
  2488. IR::LabelInstr *
  2489. Instr::GetPrevLabelInstr() const
  2490. {
  2491. IR::Instr *instr = this->m_prev;
  2492. while (!instr->IsLabelInstr())
  2493. {
  2494. instr = instr->m_prev;
  2495. AssertMsg(instr, "GetPrevLabelInstr() failed...");
  2496. }
  2497. return instr->AsLabelInstr();
  2498. }
  2499. ///----------------------------------------------------------------------------
  2500. ///
  2501. /// Instr::GetPrevLabelInstr
  2502. ///
  2503. ///----------------------------------------------------------------------------
  2504. IR::Instr *
  2505. Instr::GetBlockStartInstr() const
  2506. {
  2507. IR::Instr *instr = this->m_prev;
  2508. while (!instr->StartsBasicBlock())
  2509. {
  2510. instr = instr->m_prev;
  2511. AssertMsg(instr, "GetBlockStartInstr() failed...");
  2512. }
  2513. return instr;
  2514. }
  2515. ///----------------------------------------------------------------------------
  2516. ///
  2517. /// Instr::GetInsertBeforeByteCodeUsesInstr
  2518. /// Finds the instruction before which new instructions can be inserted, by skipping ByteCodeUses instructions associated with
  2519. /// this instruction.
  2520. ///
  2521. ///----------------------------------------------------------------------------
  2522. IR::Instr *Instr::GetInsertBeforeByteCodeUsesInstr()
  2523. {
  2524. const uint32 byteCodeOffset = GetByteCodeOffset();
  2525. IR::Instr *insertBeforeInstr = this;
  2526. IR::Instr *prevInstr = insertBeforeInstr->m_prev;
  2527. while(prevInstr && prevInstr->IsByteCodeUsesInstr() && prevInstr->GetByteCodeOffset() == byteCodeOffset)
  2528. {
  2529. insertBeforeInstr = prevInstr;
  2530. prevInstr = prevInstr->m_prev;
  2531. }
  2532. return insertBeforeInstr;
  2533. }
  2534. bool
  2535. Instr::IsByteCodeUsesInstrFor(IR::Instr * instr) const
  2536. {
  2537. return this->IsByteCodeUsesInstr() && this->GetByteCodeOffset() == instr->GetByteCodeOffset();
  2538. }
  2539. ///----------------------------------------------------------------------------
  2540. ///
  2541. /// Instr::GetOrCreateContinueLabel
  2542. ///
  2543. ///----------------------------------------------------------------------------
  2544. IR::LabelInstr *
  2545. Instr::GetOrCreateContinueLabel(const bool isHelper)
  2546. {
  2547. if (m_next && m_next->IsLabelInstr() && m_next->AsLabelInstr()->isOpHelper == isHelper)
  2548. {
  2549. return m_next->AsLabelInstr();
  2550. }
  2551. IR::LabelInstr *const label = IR::LabelInstr::New(Js::OpCode::Label, m_func, isHelper);
  2552. InsertAfter(label);
  2553. return label;
  2554. }
  2555. bool
  2556. Instr::HasSymUseSrc(StackSym *sym, IR::Opnd* src)
  2557. {
  2558. if (!src)
  2559. {
  2560. return false;
  2561. }
  2562. if (src->IsRegOpnd())
  2563. {
  2564. RegOpnd *regOpnd = src->AsRegOpnd();
  2565. if (regOpnd->m_sym == sym)
  2566. {
  2567. return true;
  2568. }
  2569. }
  2570. else if (src->IsIndirOpnd())
  2571. {
  2572. IR::IndirOpnd *indirOpnd = src->AsIndirOpnd();
  2573. RegOpnd * baseOpnd = indirOpnd->GetBaseOpnd();
  2574. if (baseOpnd != nullptr && baseOpnd->m_sym == sym)
  2575. {
  2576. return true;
  2577. }
  2578. else if (indirOpnd->GetIndexOpnd() && indirOpnd->GetIndexOpnd()->m_sym == sym)
  2579. {
  2580. return true;
  2581. }
  2582. }
  2583. else if (src->IsListOpnd())
  2584. {
  2585. IR::ListOpnd* list = src->AsListOpnd();
  2586. for (int i = 0; i < list->Count(); ++i)
  2587. {
  2588. if (HasSymUseSrc(sym, list->Item(i)))
  2589. {
  2590. return true;
  2591. }
  2592. }
  2593. }
  2594. else if (src->IsSymOpnd())
  2595. {
  2596. SymOpnd* symOpnd = src->AsSymOpnd();
  2597. if (symOpnd->GetSym() == sym)
  2598. {
  2599. return true;
  2600. }
  2601. if (symOpnd->IsPropertySymOpnd())
  2602. {
  2603. PropertySymOpnd* propertySymOpnd = symOpnd->AsPropertySymOpnd();
  2604. if (propertySymOpnd->GetObjectSym() == sym)
  2605. {
  2606. return true;
  2607. }
  2608. }
  2609. }
  2610. return false;
  2611. }
  2612. bool
  2613. Instr::HasSymUseDst(StackSym *sym, IR::Opnd* dst)
  2614. {
  2615. if (!dst)
  2616. {
  2617. return false;
  2618. }
  2619. if (dst->IsIndirOpnd())
  2620. {
  2621. IR::IndirOpnd *indirOpnd = dst->AsIndirOpnd();
  2622. RegOpnd * baseOpnd = indirOpnd->GetBaseOpnd();
  2623. if (baseOpnd != nullptr && baseOpnd->m_sym == sym)
  2624. {
  2625. return true;
  2626. }
  2627. else if (indirOpnd->GetIndexOpnd() && indirOpnd->GetIndexOpnd()->m_sym == sym)
  2628. {
  2629. return true;
  2630. }
  2631. }
  2632. else if (dst->IsListOpnd())
  2633. {
  2634. IR::ListOpnd* list = dst->AsListOpnd();
  2635. for (int i = 0; i < list->Count(); ++i)
  2636. {
  2637. if (HasSymUseDst(sym, list->Item(i)))
  2638. {
  2639. return true;
  2640. }
  2641. }
  2642. }
  2643. else if (dst->IsSymOpnd())
  2644. {
  2645. SymOpnd* symOpnd = dst->AsSymOpnd();
  2646. if (symOpnd->GetSym() == sym)
  2647. {
  2648. return true;
  2649. }
  2650. if (symOpnd->IsPropertySymOpnd())
  2651. {
  2652. PropertySymOpnd* propertySymOpnd = symOpnd->AsPropertySymOpnd();
  2653. if (propertySymOpnd->GetObjectSym() == sym)
  2654. {
  2655. return true;
  2656. }
  2657. }
  2658. }
  2659. return false;
  2660. }
  2661. bool
  2662. Instr::HasSymUse(StackSym *sym)
  2663. {
  2664. if (HasSymUseSrc(sym, this->GetSrc1()))
  2665. {
  2666. return true;
  2667. }
  2668. if (HasSymUseSrc(sym, this->GetSrc2()))
  2669. {
  2670. return true;
  2671. }
  2672. if (HasSymUseDst(sym, this->GetDst()))
  2673. {
  2674. return true;
  2675. }
  2676. return false;
  2677. }
  2678. bool
  2679. Instr::HasSymUseInRange(StackSym *sym, IR::Instr *instrBegin, IR::Instr *instrEnd)
  2680. {
  2681. FOREACH_INSTR_IN_RANGE(instr, instrBegin, instrEnd)
  2682. {
  2683. Assert(instr);
  2684. if (instr->HasSymUse(sym))
  2685. {
  2686. return true;
  2687. }
  2688. }
  2689. NEXT_INSTR_IN_RANGE;
  2690. return false;
  2691. }
  2692. ///----------------------------------------------------------------------------
  2693. ///
  2694. /// Instr::FindRegDef
  2695. ///
  2696. /// Search a reg def of the given sym. Return the RegOpnd that defines it.
  2697. ///
  2698. ///----------------------------------------------------------------------------
  2699. IR::RegOpnd *
  2700. Instr::FindRegDef(StackSym *sym)
  2701. {
  2702. IR::Opnd *dst = this->GetDst();
  2703. if (dst)
  2704. {
  2705. if (dst->IsRegOpnd())
  2706. {
  2707. RegOpnd *regOpnd = dst->AsRegOpnd();
  2708. if (regOpnd->m_sym == sym)
  2709. {
  2710. return regOpnd;
  2711. }
  2712. }
  2713. }
  2714. return nullptr;
  2715. }
  2716. Instr*
  2717. Instr::FindSingleDefInstr(Js::OpCode opCode, Opnd* src)
  2718. {
  2719. RegOpnd* src1 = src->IsRegOpnd() ? src->AsRegOpnd() : nullptr;
  2720. return src1 &&
  2721. src1->m_sym->IsSingleDef() &&
  2722. src1->m_sym->GetInstrDef()->m_opcode == opCode ?
  2723. src1->m_sym->GetInstrDef() :
  2724. nullptr;
  2725. }
  2726. void
  2727. Instr::TransferDstAttributesTo(Instr * instr)
  2728. {
  2729. instr->dstIsTempNumber = this->dstIsTempNumber;
  2730. instr->dstIsTempNumberTransferred = this->dstIsTempNumberTransferred;
  2731. instr->dstIsTempObject = this->dstIsTempObject;
  2732. }
  2733. void
  2734. Instr::TransferTo(Instr * instr)
  2735. {
  2736. Assert(instr->m_dst == nullptr);
  2737. Assert(instr->m_src1 == nullptr);
  2738. Assert(instr->m_src2 == nullptr);
  2739. this->TransferDstAttributesTo(instr);
  2740. instr->usesStackArgumentsObject = this->usesStackArgumentsObject;
  2741. instr->isCloned = this->isCloned;
  2742. instr->ignoreNegativeZero = this->ignoreNegativeZero;
  2743. instr->ignoreIntOverflow = this->ignoreIntOverflow;
  2744. instr->ignoreIntOverflowInRange = this->ignoreIntOverflowInRange;
  2745. instr->ignoreOverflowBitCount = this->ignoreOverflowBitCount;
  2746. instr->loadedArrayHeadSegment = this->loadedArrayHeadSegment;
  2747. instr->loadedArrayHeadSegmentLength = this->loadedArrayHeadSegmentLength;
  2748. instr->extractedUpperBoundCheckWithoutHoisting = this->extractedUpperBoundCheckWithoutHoisting;
  2749. instr->m_number = this->m_number;
  2750. instr->m_src1 = this->m_src1;
  2751. instr->m_src2 = this->m_src2;
  2752. instr->dstIsAlwaysConvertedToInt32 = this->dstIsAlwaysConvertedToInt32;
  2753. instr->dstIsAlwaysConvertedToNumber = this->dstIsAlwaysConvertedToNumber;
  2754. instr->dataWidth = this->dataWidth;
  2755. instr->isCtorCall = this->isCtorCall;
  2756. instr->forcePreOpBailOutIfNeeded = this->forcePreOpBailOutIfNeeded;
  2757. IR::Opnd * dst = this->m_dst;
  2758. if (dst)
  2759. {
  2760. instr->m_dst = dst;
  2761. this->m_dst = nullptr;
  2762. if (dst->IsRegOpnd())
  2763. {
  2764. Sym * sym = dst->AsRegOpnd()->m_sym;
  2765. if (sym->IsStackSym() && sym->AsStackSym()->m_isSingleDef)
  2766. {
  2767. Assert(sym->AsStackSym()->m_instrDef == this);
  2768. StackSym * stackSym = sym->AsStackSym();
  2769. stackSym->m_instrDef = instr;
  2770. }
  2771. }
  2772. }
  2773. this->m_src1 = nullptr;
  2774. this->m_src2 = nullptr;
  2775. }
  2776. // Convert an instruction to a bailout instruction and perform a shallow copy of the input instruction's BailOutInfo.
  2777. // Can optionally change the BailOutKind, otherwise the input instruction's BailOutKind will be used instead.
  2778. IR::Instr *
  2779. Instr::ConvertToBailOutInstrWithBailOutInfoCopy(BailOutInfo *bailOutInfo, IR::BailOutKind bailOutKind)
  2780. {
  2781. BailOutInfo *bailOutInfoCopy = JitAnew(this->m_func->m_alloc, BailOutInfo, bailOutInfo->bailOutOffset, this->m_func);
  2782. bailOutInfo->PartialDeepCopyTo(bailOutInfoCopy);
  2783. return this->ConvertToBailOutInstr(bailOutInfoCopy, bailOutKind);
  2784. }
  2785. IR::Instr *
  2786. Instr::ConvertToBailOutInstr(IR::Instr * bailOutTarget, IR::BailOutKind kind, uint32 bailOutOffset)
  2787. {
  2788. Func * func = bailOutTarget->m_func;
  2789. BailOutInfo * bailOutInfo = JitAnew(func->m_alloc, BailOutInfo, bailOutOffset == Js::Constants::NoByteCodeOffset ? bailOutTarget->GetByteCodeOffset() : bailOutOffset , func);
  2790. #if ENABLE_DEBUG_CONFIG_OPTIONS
  2791. bailOutInfo->bailOutOpcode = this->m_opcode;
  2792. #endif
  2793. return this->ConvertToBailOutInstr(bailOutInfo, kind);
  2794. }
  2795. // Notes:
  2796. // - useAuxBailout = true specifies that this bailout further will be invisible to globopt, etc, and we'll use auxBailoutKind instead of BailoutKind.
  2797. // Currently this is used for BailOutIgnoreException for debugger.
  2798. //
  2799. // Here's typical workflow for scenario useAuxBailout = true.
  2800. // - IRBuilder::Build calls this with kind == BailOutIgnoreException
  2801. // - In here we save the kind to auxBailOut and save bail out info but set hasBailOutInfo to false.
  2802. // - During globopt optimizations presence of this bail out is not detected and instrs can add/remove bailouts as they need.
  2803. // - If they call to convert this instr to bail out instr, we set bailOutKind to what they want and replace bailOutInfo.
  2804. // ** This assumes that for aux bail out bailoutInfo does not really matter (if its pre/post op, etc) **
  2805. // - This is the case for ignore exception.
  2806. // - This will cause to share aux bail out with regular bail out.
  2807. // - In globopt right after OptInstr we check if there is aux bail out which wasn't shared with regular bail out,
  2808. // and if it's not, we convert it back to regular bail out.
  2809. IR::Instr *
  2810. Instr::ConvertToBailOutInstr(BailOutInfo * bailOutInfo, IR::BailOutKind kind, bool useAuxBailOut /* = false */)
  2811. {
  2812. Assert(!this->HasBailOutInfo());
  2813. AssertMsg(!useAuxBailOut || !this->HasAuxBailOut(), "Already aux bail out!");
  2814. Assert(!this->HasAuxBailOut() || this->GetAuxBailOutKind() != IR::BailOutInvalid);
  2815. IR::Instr * bailOutInstr = nullptr;
  2816. if (this->HasAuxBailOut())
  2817. {
  2818. // This instr has already been converted to bailout instr. Only possible with aux bail out.
  2819. // Typical scenario is when globopt calls to convert to e.g. BailOutOnImplicitCalls for the instr which
  2820. // was already converted to bail out instr with HasBailOutInfo() == false and HasAuxBailOutInfo() == true,
  2821. // so that aux bail out is hidden in between IRBuilder and lowerer.
  2822. AssertMsg((this->GetAuxBailOutKind() & ~(IR::BailOutIgnoreException | IR::BailOutForceByFlag)) == 0, "Only IR::BailOutIgnoreException|ForceByFlag supported here.");
  2823. // What we rely on here is:
  2824. // - bailout doesn't have any args.
  2825. // - bailout doesn't use offset as we get it from DebuggingFlags at time of bailout.
  2826. // Use prev debugger bailout kind as decoration, while keeping new kind as main.
  2827. this->SetBailOutKind_NoAssert(kind);
  2828. // Clear old (aux) info and set to the new bailOutInfo.
  2829. this->ReplaceBailOutInfo(bailOutInfo);
  2830. bailOutInfo->bailOutInstr = this;
  2831. this->hasBailOutInfo = true;
  2832. bailOutInstr = this;
  2833. }
  2834. else
  2835. {
  2836. switch (this->m_kind)
  2837. {
  2838. case InstrKindInstr:
  2839. bailOutInstr = IR::BailOutInstr::New(this->m_opcode, kind, bailOutInfo, bailOutInfo->bailOutFunc);
  2840. break;
  2841. case InstrKindProfiled:
  2842. bailOutInstr = IR::ProfiledBailOutInstr::New(this->m_opcode, kind, bailOutInfo, bailOutInfo->bailOutFunc);
  2843. bailOutInstr->AsProfiledInstr()->u = this->AsProfiledInstr()->u;
  2844. break;
  2845. case InstrKindBranch:
  2846. {
  2847. IR::BranchInstr * branchInstr = this->AsBranchInstr();
  2848. Assert(!branchInstr->IsMultiBranch());
  2849. IR::BranchBailOutInstr * branchBailOutInstr = IR::BranchBailOutInstr::New(this->m_opcode, kind, bailOutInfo, bailOutInfo->bailOutFunc);
  2850. branchBailOutInstr->SetTarget(branchInstr->GetTarget());
  2851. branchBailOutInstr->SetByteCodeReg(branchInstr->GetByteCodeReg());
  2852. bailOutInstr = branchBailOutInstr;
  2853. break;
  2854. }
  2855. default:
  2856. AnalysisAssert(false);
  2857. };
  2858. this->m_next->m_prev = bailOutInstr;
  2859. this->m_prev->m_next = bailOutInstr;
  2860. bailOutInstr->m_next = this->m_next;
  2861. bailOutInstr->m_prev = this->m_prev;
  2862. this->TransferTo(bailOutInstr);
  2863. this->Free();
  2864. }
  2865. if (useAuxBailOut)
  2866. {
  2867. // Move bail out kind from bailOutKind to auxBailOutKind and hide bailOutInfo as if this is not a bail out instr.
  2868. bailOutInstr->SetAuxBailOutKind(kind);
  2869. bailOutInstr->SetBailOutKind_NoAssert(IR::BailOutInvalid);
  2870. bailOutInstr->hasBailOutInfo = false;
  2871. bailOutInstr->hasAuxBailOut = true;
  2872. }
  2873. return bailOutInstr;
  2874. }
  2875. // Convert aux bailout to regular bail out.
  2876. // Called by globopt after all optimizations are done, in case we still have aux bail out on the instr.
  2877. void Instr::PromoteAuxBailOut()
  2878. {
  2879. Assert(!this->HasBailOutInfo());
  2880. Assert(this->GetAuxBailOutKind() != IR::BailOutInvalid);
  2881. this->SetBailOutKind_NoAssert(this->GetAuxBailOutKind());
  2882. this->SetAuxBailOutKind(IR::BailOutInvalid);
  2883. this->hasBailOutInfo = true;
  2884. this->hasAuxBailOut = false;
  2885. }
  2886. // Reset all tracks of aux bailout but don't rest the bail out info.
  2887. // Used after we extract aux bail out in lowerer.
  2888. void Instr::ResetAuxBailOut()
  2889. {
  2890. this->SetAuxBailOutKind(IR::BailOutInvalid);
  2891. this->hasAuxBailOut = false;
  2892. }
  2893. void
  2894. Instr::ClearBailOutInfo()
  2895. {
  2896. if (this->HasBailOutInfo() || this->HasAuxBailOut())
  2897. {
  2898. BailOutInfo * bailOutInfo = this->GetBailOutInfo();
  2899. Assert(bailOutInfo);
  2900. if (bailOutInfo->bailOutInstr == this)
  2901. {
  2902. JitArenaAllocator * alloc = this->m_func->m_alloc;
  2903. bailOutInfo->Clear(alloc);
  2904. JitAdelete(alloc, bailOutInfo);
  2905. }
  2906. this->hasBailOutInfo = false;
  2907. this->hasAuxBailOut = false;
  2908. }
  2909. }
  2910. bool Instr::HasAnyLoadHeapArgsOpCode()
  2911. {
  2912. switch (m_opcode)
  2913. {
  2914. case Js::OpCode::LdHeapArguments:
  2915. case Js::OpCode::LdHeapArgsCached:
  2916. case Js::OpCode::LdLetHeapArguments:
  2917. case Js::OpCode::LdLetHeapArgsCached:
  2918. return true;
  2919. }
  2920. return false;
  2921. }
  2922. bool Instr::CanHaveArgOutChain() const
  2923. {
  2924. return
  2925. this->m_opcode == Js::OpCode::CallI ||
  2926. this->m_opcode == Js::OpCode::CallIFixed ||
  2927. this->m_opcode == Js::OpCode::NewScObject ||
  2928. this->m_opcode == Js::OpCode::NewScObjectSpread ||
  2929. this->m_opcode == Js::OpCode::NewScObjArray ||
  2930. this->m_opcode == Js::OpCode::NewScObjArraySpread;
  2931. }
  2932. bool Instr::HasEmptyArgOutChain(IR::Instr** startCallInstrOut)
  2933. {
  2934. Assert(CanHaveArgOutChain());
  2935. if (GetSrc2()->IsRegOpnd())
  2936. {
  2937. IR::RegOpnd * argLinkOpnd = GetSrc2()->AsRegOpnd();
  2938. StackSym *argLinkSym = argLinkOpnd->m_sym->AsStackSym();
  2939. AssertMsg(!argLinkSym->IsArgSlotSym() && argLinkSym->m_isSingleDef, "Arg tree not single def...");
  2940. IR::Instr* startCallInstr = argLinkSym->m_instrDef;
  2941. AssertMsg(startCallInstr->m_opcode == Js::OpCode::StartCall, "Problem with arg chain.");
  2942. if (startCallInstrOut != nullptr)
  2943. {
  2944. *startCallInstrOut = startCallInstr;
  2945. }
  2946. return true;
  2947. }
  2948. return false;
  2949. }
  2950. bool Instr::HasFixedFunctionAddressTarget() const
  2951. {
  2952. Assert(
  2953. this->m_opcode == Js::OpCode::CallI ||
  2954. this->m_opcode == Js::OpCode::CallIFixed ||
  2955. this->m_opcode == Js::OpCode::NewScObject ||
  2956. this->m_opcode == Js::OpCode::NewScObjectSpread ||
  2957. this->m_opcode == Js::OpCode::NewScObjArray ||
  2958. this->m_opcode == Js::OpCode::NewScObjArraySpread ||
  2959. this->m_opcode == Js::OpCode::NewScObjectNoCtor);
  2960. return
  2961. this->GetSrc1() != nullptr &&
  2962. this->GetSrc1()->IsAddrOpnd() &&
  2963. this->GetSrc1()->AsAddrOpnd()->GetAddrOpndKind() == IR::AddrOpndKind::AddrOpndKindDynamicVar &&
  2964. this->GetSrc1()->AsAddrOpnd()->m_isFunction;
  2965. }
  2966. bool Instr::TransfersSrcValue()
  2967. {
  2968. // Return whether the instruction transfers a value to the destination.
  2969. // This is used to determine whether we should generate a value for the src so that it will
  2970. // match with the dst for copy prop.
  2971. // No point creating an unknown value for the src of a binary instr, as the dst will just be a different
  2972. // Don't create value for instruction without dst as well. The value doesn't go anywhere.
  2973. // Consider: Add opcode attribute to indicate whether the opcode would use the value or not
  2974. return this->GetDst() != nullptr && this->GetSrc2() == nullptr && !OpCodeAttr::DoNotTransfer(this->m_opcode) && !this->CallsAccessor();
  2975. }
  2976. void Instr::MoveArgs(bool generateByteCodeCapture)
  2977. {
  2978. Assert(this->m_opcode == Js::OpCode::InlineeStart || this->m_opcode == Js::OpCode::CallDirect ||
  2979. this->m_opcode == Js::OpCode::CallI || this->m_opcode == Js::OpCode::CallIFixed);
  2980. IR::Instr *argInsertInstr = this;
  2981. this->IterateArgInstrs([&](IR::Instr* argInstr)
  2982. {
  2983. if (generateByteCodeCapture)
  2984. {
  2985. argInstr->GenerateBytecodeArgOutCapture();
  2986. }
  2987. argInstr->Move(argInsertInstr);
  2988. argInsertInstr = argInstr;
  2989. return false;
  2990. });
  2991. }
  2992. void Instr::Move(IR::Instr* insertInstr)
  2993. {
  2994. this->Unlink();
  2995. this->ClearByteCodeOffset();
  2996. this->SetByteCodeOffset(insertInstr);
  2997. insertInstr->InsertBefore(this);
  2998. }
  2999. IR::Instr* Instr::GetBytecodeArgOutCapture()
  3000. {
  3001. Assert(this->m_opcode == Js::OpCode::ArgOut_A_Inline ||
  3002. this->m_opcode == Js::OpCode::ArgOut_A ||
  3003. this->m_opcode == Js::OpCode::ArgOut_A_InlineBuiltIn);
  3004. Assert(this->m_dst->GetStackSym()->m_isArgCaptured);
  3005. IR::Instr* instr = this->GetSrc1()->GetStackSym()->m_instrDef;
  3006. while (instr->m_opcode != Js::OpCode::BytecodeArgOutCapture)
  3007. {
  3008. Assert(instr->GetSrc1() && instr->GetSrc1()->GetStackSym() && instr->GetSrc1()->GetStackSym()->IsSingleDef());
  3009. instr = instr->GetSrc1()->GetStackSym()->m_instrDef;
  3010. }
  3011. Assert(instr->m_opcode == Js::OpCode::BytecodeArgOutCapture);
  3012. return instr;
  3013. }
  3014. bool Instr::HasByteCodeArgOutCapture()
  3015. {
  3016. Assert(this->m_opcode == Js::OpCode::ArgOut_A_FixupForStackArgs ||
  3017. this->m_opcode == Js::OpCode::ArgOut_A_Inline ||
  3018. this->m_opcode == Js::OpCode::ArgOut_A ||
  3019. this->m_opcode == Js::OpCode::ArgOut_A_InlineBuiltIn ||
  3020. this->m_opcode == Js::OpCode::ArgOut_A_FromStackArgs);
  3021. if (this->m_dst->GetStackSym()->m_isArgCaptured)
  3022. {
  3023. Assert(GetBytecodeArgOutCapture() != nullptr);
  3024. return true;
  3025. }
  3026. return false;
  3027. }
  3028. void Instr::GenerateBytecodeArgOutCapture()
  3029. {
  3030. if (!HasByteCodeArgOutCapture())
  3031. {
  3032. this->m_dst->GetStackSym()->m_isArgCaptured = true;
  3033. StackSym* tmpSym = StackSym::NewArgSlotRegSym(this->GetDst()->GetStackSym()->GetArgSlotNum(), this->m_func, this->GetDst()->GetType());
  3034. IR::Instr* instr = this->HoistSrc1(Js::OpCode::BytecodeArgOutCapture, RegNOREG, tmpSym);
  3035. instr->SetByteCodeOffset(this);
  3036. }
  3037. }
  3038. void Instr::GenerateArgOutSnapshot()
  3039. {
  3040. StackSym* tmpSym = StackSym::NewArgSlotRegSym(this->GetDst()->GetStackSym()->GetArgSlotNum(), this->m_func);
  3041. IR::Instr* instr = this->HoistSrc1(Js::OpCode::Ld_A, RegNOREG, tmpSym);
  3042. instr->SetByteCodeOffset(this);
  3043. }
  3044. IR::Instr* Instr::GetArgOutSnapshot()
  3045. {
  3046. Assert(this->m_opcode == Js::OpCode::ArgOut_A_FixupForStackArgs);
  3047. IR::Instr* instr = this->GetSrc1()->GetStackSym()->m_instrDef;
  3048. Assert(instr->m_opcode == Js::OpCode::Ld_A);
  3049. return instr;
  3050. }
  3051. bool Instr::OpndHasAnyImplicitCalls(IR::Opnd* opnd, bool isSrc)
  3052. {
  3053. return opnd && (
  3054. (opnd->IsSymOpnd() && opnd->AsSymOpnd()->m_sym->IsPropertySym()) ||
  3055. opnd->IsIndirOpnd() ||
  3056. (isSrc && !opnd->GetValueType().IsPrimitive()) ||
  3057. (opnd->IsListOpnd() && opnd->AsListOpnd()->Any([isSrc](IR::Opnd* lOpnd) { return OpndHasAnyImplicitCalls(lOpnd, isSrc); }))
  3058. );
  3059. }
  3060. bool Instr::HasAnyImplicitCalls() const
  3061. {
  3062. // there can be no implicit calls in asm.js
  3063. if (m_func->GetJITFunctionBody()->IsAsmJsMode())
  3064. {
  3065. return false;
  3066. }
  3067. if (OpCodeAttr::HasImplicitCall(this->m_opcode))
  3068. {
  3069. return true;
  3070. }
  3071. if (OpCodeAttr::OpndHasImplicitCall(this->m_opcode))
  3072. {
  3073. return (
  3074. OpndHasAnyImplicitCalls(this->GetDst(), false) ||
  3075. OpndHasAnyImplicitCalls(this->GetSrc1(), true) ||
  3076. OpndHasAnyImplicitCalls(this->GetSrc2(), true)
  3077. );
  3078. }
  3079. return false;
  3080. }
  3081. bool Instr::HasAnySideEffects() const
  3082. {
  3083. return (hasSideEffects ||
  3084. OpCodeAttr::HasSideEffects(this->m_opcode) ||
  3085. this->HasAnyImplicitCalls());
  3086. }
  3087. bool Instr::AreAllOpndInt64() const
  3088. {
  3089. bool isDstInt64 = !m_dst || IRType_IsInt64(m_dst->GetType());
  3090. bool isSrc1Int64 = !m_src1 || IRType_IsInt64(m_src1->GetType());
  3091. bool isSrc2Int64 = !m_src2 || IRType_IsInt64(m_src2->GetType());
  3092. return isDstInt64 && isSrc1Int64 && isSrc2Int64;
  3093. }
  3094. FixedFieldInfo* Instr::GetFixedFunction() const
  3095. {
  3096. Assert(HasFixedFunctionAddressTarget());
  3097. FixedFieldInfo* function = (FixedFieldInfo*)this->m_src1->AsAddrOpnd()->m_metadata;
  3098. return function;
  3099. }
  3100. IR::Instr* Instr::GetNextArg()
  3101. {
  3102. Assert(this->m_opcode == Js::OpCode::ArgOut_A_FixupForStackArgs ||
  3103. this->m_opcode == Js::OpCode::ArgOut_A_Inline ||
  3104. this->m_opcode == Js::OpCode::ArgOut_A ||
  3105. this->m_opcode == Js::OpCode::ArgOut_A_InlineBuiltIn ||
  3106. this->m_opcode == Js::OpCode::InlineeStart);
  3107. IR::Instr* argInstr = this;
  3108. while (true)
  3109. {
  3110. StackSym* linkSym;
  3111. if (argInstr->GetSrc2()->IsRegOpnd())
  3112. {
  3113. linkSym = argInstr->GetSrc2()->AsRegOpnd()->m_sym->AsStackSym();
  3114. }
  3115. else
  3116. {
  3117. linkSym = argInstr->GetSrc2()->AsSymOpnd()->m_sym->AsStackSym();
  3118. Assert(linkSym->IsArgSlotSym());
  3119. }
  3120. Assert(linkSym->IsSingleDef());
  3121. argInstr = linkSym->m_instrDef;
  3122. if (argInstr->m_opcode == Js::OpCode::ArgOut_A_InlineSpecialized)
  3123. {
  3124. continue;
  3125. }
  3126. if (argInstr->m_opcode == Js::OpCode::StartCall)
  3127. {
  3128. break;
  3129. }
  3130. return argInstr;
  3131. }
  3132. return nullptr;
  3133. }
  3134. uint Instr::GetArgOutCount(bool getInterpreterArgOutCount)
  3135. {
  3136. // There are cases of inlining like .apply and .call target inlining, where we muck around with the ArgOut sequence,
  3137. // and make it different from the one the interpreter sees (and expects, on a bailout).
  3138. // In such cases, we set the interpreter version of the number of ArgOuts as the src2 of StartCall,
  3139. // and any code that queries the argout count for bailout purposes should look at the src2 (if available) of these instructions.
  3140. // If the src2 is not set, that means that the interpreter and the JIT versions of the argout count are the same.
  3141. Js::OpCode opcode = this->m_opcode;
  3142. Assert(opcode == Js::OpCode::StartCall ||
  3143. opcode == Js::OpCode::InlineeEnd || opcode == Js::OpCode::InlineBuiltInEnd|| opcode == Js::OpCode::InlineNonTrackingBuiltInEnd ||
  3144. opcode == Js::OpCode::EndCallForPolymorphicInlinee || opcode == Js::OpCode::LoweredStartCall);
  3145. Assert(!getInterpreterArgOutCount || opcode == Js::OpCode::StartCall);
  3146. uint argOutCount = !this->GetSrc2() || !getInterpreterArgOutCount || m_func->GetJITFunctionBody()->IsAsmJsMode()
  3147. ? this->GetSrc1()->AsIntConstOpnd()->AsUint32()
  3148. : this->GetSrc2()->AsIntConstOpnd()->AsUint32();
  3149. return (uint)argOutCount;
  3150. }
  3151. uint Instr::GetAsmJsArgOutSize()
  3152. {
  3153. switch (m_opcode)
  3154. {
  3155. case Js::OpCode::StartCall:
  3156. case Js::OpCode::LoweredStartCall:
  3157. return GetSrc2()->AsIntConstOpnd()->AsUint32();
  3158. case Js::OpCode::InlineeEnd:
  3159. {
  3160. // StartCall instr has the size, so walk back to it
  3161. IR::Instr *argInstr = this;
  3162. while(argInstr->m_opcode != Js::OpCode::StartCall && argInstr->m_opcode != Js::OpCode::LoweredStartCall)
  3163. {
  3164. argInstr = argInstr->GetSrc2()->GetStackSym()->GetInstrDef();
  3165. }
  3166. // add StartCall arg size with inlinee meta args for full size
  3167. uint size = UInt32Math::Add(argInstr->GetSrc2()->AsIntConstOpnd()->AsUint32(), Js::Constants::InlineeMetaArgCount * MachPtr);
  3168. return size;
  3169. }
  3170. default:
  3171. Assert(UNREACHED);
  3172. return 0;
  3173. }
  3174. }
  3175. uint Instr::GetArgOutSize(bool getInterpreterArgOutCount)
  3176. {
  3177. Js::OpCode opcode = this->m_opcode;
  3178. Assert(opcode == Js::OpCode::StartCall ||
  3179. opcode == Js::OpCode::InlineeEnd || opcode == Js::OpCode::InlineBuiltInEnd || opcode == Js::OpCode::InlineNonTrackingBuiltInEnd ||
  3180. opcode == Js::OpCode::EndCallForPolymorphicInlinee || opcode == Js::OpCode::LoweredStartCall);
  3181. Assert(!getInterpreterArgOutCount || opcode == Js::OpCode::StartCall);
  3182. if (m_func->GetJITFunctionBody()->IsAsmJsMode())
  3183. {
  3184. return GetAsmJsArgOutSize();
  3185. }
  3186. return UInt32Math::Mul<MachPtr>(GetArgOutCount(getInterpreterArgOutCount));
  3187. }
  3188. PropertySymOpnd *Instr::GetPropertySymOpnd() const
  3189. {
  3190. if (m_src1 && m_src1->IsSymOpnd() && m_src1->AsSymOpnd()->IsPropertySymOpnd())
  3191. {
  3192. return m_src1->AsPropertySymOpnd();
  3193. }
  3194. if (m_dst && m_dst->IsSymOpnd() && m_dst->AsSymOpnd()->IsPropertySymOpnd())
  3195. {
  3196. return m_dst->AsPropertySymOpnd();
  3197. }
  3198. return nullptr;
  3199. }
  3200. bool Instr::CallsAccessor(IR::PropertySymOpnd * methodOpnd)
  3201. {
  3202. if (methodOpnd)
  3203. {
  3204. Assert(methodOpnd->HasObjTypeSpecFldInfo());
  3205. return methodOpnd->UsesAccessor();
  3206. }
  3207. return CallsGetter() || CallsSetter();
  3208. }
  3209. bool Instr::CallsSetter()
  3210. {
  3211. return
  3212. this->IsProfiledInstr() &&
  3213. (this->m_dst && this->m_dst->IsSymOpnd() && this->m_dst->AsSymOpnd()->IsPropertySymOpnd()) &&
  3214. ((this->AsProfiledInstr()->u.FldInfo().flags & Js::FldInfo_FromAccessor) != 0);
  3215. }
  3216. bool Instr::CallsGetter()
  3217. {
  3218. return
  3219. this->IsProfiledInstr() &&
  3220. (this->m_src1 && this->m_src1->IsSymOpnd() && this->m_src1->AsSymOpnd()->IsPropertySymOpnd()) &&
  3221. ((this->AsProfiledInstr()->u.FldInfo().flags & Js::FldInfo_FromAccessor) != 0);
  3222. }
  3223. IR::Instr* IR::Instr::NewConstantLoad(IR::RegOpnd* dstOpnd, intptr_t varConst, ValueType type, Func* func, Js::Var varLocal/* = nullptr*/)
  3224. {
  3225. IR::Opnd *srcOpnd = nullptr;
  3226. IR::Instr *instr;
  3227. if (Js::TaggedInt::Is(varConst))
  3228. {
  3229. IntConstType value = Js::TaggedInt::ToInt32((Js::Var)varConst);
  3230. instr = IR::Instr::New(Js::OpCode::LdC_A_I4, dstOpnd, IR::IntConstOpnd::New(value, TyInt32, func), func);
  3231. if (dstOpnd->m_sym->IsSingleDef())
  3232. {
  3233. dstOpnd->m_sym->SetIsIntConst(value);
  3234. }
  3235. }
  3236. else
  3237. {
  3238. if (varConst == func->GetThreadContextInfo()->GetNullFrameDisplayAddr())
  3239. {
  3240. instr = IR::Instr::New(
  3241. Js::OpCode::Ld_A,
  3242. dstOpnd,
  3243. IR::AddrOpnd::New(
  3244. func->GetThreadContextInfo()->GetNullFrameDisplayAddr(),
  3245. IR::AddrOpndKindDynamicMisc,
  3246. func),
  3247. func);
  3248. }
  3249. else if (varConst == func->GetThreadContextInfo()->GetStrictNullFrameDisplayAddr())
  3250. {
  3251. instr = IR::Instr::New(
  3252. Js::OpCode::Ld_A,
  3253. dstOpnd,
  3254. IR::AddrOpnd::New(
  3255. func->GetThreadContextInfo()->GetStrictNullFrameDisplayAddr(),
  3256. IR::AddrOpndKindDynamicMisc,
  3257. func),
  3258. func);
  3259. }
  3260. else
  3261. {
  3262. ValueType valueType;
  3263. if(type.IsString())
  3264. {
  3265. srcOpnd = IR::AddrOpnd::New(varConst, IR::AddrOpndKindDynamicVar, func, true, varLocal);
  3266. instr = IR::Instr::New(Js::OpCode::LdStr, dstOpnd, srcOpnd, func);
  3267. Assert(dstOpnd->m_sym->m_isSingleDef);
  3268. if (dstOpnd->m_sym->IsSingleDef())
  3269. {
  3270. dstOpnd->m_sym->SetIsStrConst();
  3271. }
  3272. dstOpnd->SetValueType(ValueType::String);
  3273. srcOpnd->SetValueType(ValueType::String);
  3274. }
  3275. else if(type.IsNumber())
  3276. {
  3277. // TODO (michhol): OOP JIT. we may need to unbox before sending over const table
  3278. if (!func->IsOOPJIT())
  3279. {
  3280. srcOpnd = IR::FloatConstOpnd::New((Js::Var)varConst, TyFloat64, func);
  3281. }
  3282. else
  3283. {
  3284. srcOpnd = IR::FloatConstOpnd::New((Js::Var)varConst, TyFloat64, func
  3285. #if !FLOATVAR
  3286. ,varLocal
  3287. #endif
  3288. );
  3289. }
  3290. instr = IR::Instr::New(Js::OpCode::LdC_A_R8, dstOpnd, srcOpnd, func);
  3291. if (dstOpnd->m_sym->IsSingleDef())
  3292. {
  3293. dstOpnd->m_sym->SetIsFloatConst();
  3294. #if FLOATVAR
  3295. dstOpnd->m_sym->m_isNotNumber = FALSE;
  3296. #else
  3297. // Don't set m_isNotNumber to true if the float constant value is an int32 or uint32. Uint32s may sometimes be
  3298. // treated as int32s for the purposes of int specialization.
  3299. dstOpnd->m_sym->m_isNotNumber = !Js::JavascriptNumber::IsInt32OrUInt32(((IR::FloatConstOpnd*)srcOpnd)->m_value);
  3300. #endif
  3301. }
  3302. }
  3303. else
  3304. {
  3305. if (type.IsUndefined() || type.IsNull() || type.IsBoolean())
  3306. {
  3307. valueType = type;
  3308. }
  3309. else
  3310. {
  3311. valueType = ValueType::GetObject(ObjectType::Object);
  3312. }
  3313. srcOpnd = IR::AddrOpnd::New(varConst, IR::AddrOpndKindDynamicVar, func, true, varLocal);
  3314. instr = IR::Instr::New(Js::OpCode::Ld_A, dstOpnd, srcOpnd, func);
  3315. if (dstOpnd->m_sym->IsSingleDef())
  3316. {
  3317. dstOpnd->m_sym->m_isConst = true;
  3318. }
  3319. dstOpnd->SetValueType(valueType);
  3320. srcOpnd->SetValueType(valueType);
  3321. }
  3322. }
  3323. }
  3324. return instr;
  3325. }
  3326. bool Instr::UsesAllFields()
  3327. {
  3328. return OpCodeAttr::UseAllFields(this->m_opcode) || this->CallsAccessor();
  3329. }
  3330. BranchInstr *
  3331. Instr::ChangeCmCCToBranchInstr(LabelInstr *targetInstr)
  3332. {
  3333. Js::OpCode newOpcode = Js::OpCode::InvalidOpCode;
  3334. switch (this->m_opcode)
  3335. {
  3336. case Js::OpCode::CmEq_A:
  3337. newOpcode = Js::OpCode::BrEq_A;
  3338. break;
  3339. case Js::OpCode::CmGe_A:
  3340. newOpcode = Js::OpCode::BrGe_A;
  3341. break;
  3342. case Js::OpCode::CmGt_A:
  3343. newOpcode = Js::OpCode::BrGt_A;
  3344. break;
  3345. case Js::OpCode::CmLt_A:
  3346. newOpcode = Js::OpCode::BrLt_A;
  3347. break;
  3348. case Js::OpCode::CmLe_A:
  3349. newOpcode = Js::OpCode::BrLe_A;
  3350. break;
  3351. case Js::OpCode::CmUnGe_A:
  3352. newOpcode = Js::OpCode::BrUnGe_A;
  3353. break;
  3354. case Js::OpCode::CmUnGt_A:
  3355. newOpcode = Js::OpCode::BrUnGt_A;
  3356. break;
  3357. case Js::OpCode::CmUnLt_A:
  3358. newOpcode = Js::OpCode::BrUnLt_A;
  3359. break;
  3360. case Js::OpCode::CmUnLe_A:
  3361. newOpcode = Js::OpCode::BrUnLe_A;
  3362. break;
  3363. case Js::OpCode::CmNeq_A:
  3364. newOpcode = Js::OpCode::BrNeq_A;
  3365. break;
  3366. case Js::OpCode::CmSrEq_A:
  3367. newOpcode = Js::OpCode::BrSrEq_A;
  3368. break;
  3369. case Js::OpCode::CmSrNeq_A:
  3370. newOpcode = Js::OpCode::BrSrNeq_A;
  3371. break;
  3372. case Js::OpCode::CmEq_I4:
  3373. newOpcode = Js::OpCode::BrEq_I4;
  3374. break;
  3375. case Js::OpCode::CmGe_I4:
  3376. newOpcode = Js::OpCode::BrGe_I4;
  3377. break;
  3378. case Js::OpCode::CmGt_I4:
  3379. newOpcode = Js::OpCode::BrGt_I4;
  3380. break;
  3381. case Js::OpCode::CmLt_I4:
  3382. newOpcode = Js::OpCode::BrLt_I4;
  3383. break;
  3384. case Js::OpCode::CmLe_I4:
  3385. newOpcode = Js::OpCode::BrLe_I4;
  3386. break;
  3387. case Js::OpCode::CmUnGe_I4:
  3388. newOpcode = Js::OpCode::BrUnGe_I4;
  3389. break;
  3390. case Js::OpCode::CmUnGt_I4:
  3391. newOpcode = Js::OpCode::BrUnGt_I4;
  3392. break;
  3393. case Js::OpCode::CmUnLt_I4:
  3394. newOpcode = Js::OpCode::BrUnLt_I4;
  3395. break;
  3396. case Js::OpCode::CmUnLe_I4:
  3397. newOpcode = Js::OpCode::BrUnLe_I4;
  3398. break;
  3399. case Js::OpCode::CmNeq_I4:
  3400. newOpcode = Js::OpCode::BrNeq_I4;
  3401. break;
  3402. default:
  3403. Assert(UNREACHED);
  3404. __assume(UNREACHED);
  3405. }
  3406. BranchInstr *instrBr = BranchInstr::New(newOpcode, targetInstr, this->m_func);
  3407. this->InsertBefore(instrBr);
  3408. instrBr->SetByteCodeOffset(this);
  3409. instrBr->SetSrc1(this->UnlinkSrc1());
  3410. instrBr->SetSrc2(this->UnlinkSrc2());
  3411. this->Remove();
  3412. return instrBr;
  3413. }
  3414. bool Instr::IsCmCC_A()
  3415. {
  3416. return (this->m_opcode >= Js::OpCode::CmEq_A && this->m_opcode <= Js::OpCode::CmSrNeq_A) && this->GetSrc1()->IsVar();
  3417. }
  3418. bool Instr::IsCmCC_R8()
  3419. {
  3420. return (this->m_opcode >= Js::OpCode::CmEq_A && this->m_opcode <= Js::OpCode::CmSrNeq_A) && this->GetSrc1()->IsFloat64();
  3421. }
  3422. bool Instr::IsCmCC_I4()
  3423. {
  3424. return (this->m_opcode >= Js::OpCode::CmEq_I4 && this->m_opcode <= Js::OpCode::CmUnGe_I4);
  3425. }
  3426. bool Instr::IsNeq()
  3427. {
  3428. switch (m_opcode)
  3429. {
  3430. case Js::OpCode::BrNeq_A:
  3431. case Js::OpCode::BrNeq_I4:
  3432. case Js::OpCode::BrNotEq_A:
  3433. case Js::OpCode::BrSrNeq_A:
  3434. case Js::OpCode::BrSrNotEq_A:
  3435. case Js::OpCode::CmNeq_A:
  3436. case Js::OpCode::CmNeq_I4:
  3437. case Js::OpCode::CmSrNeq_A:
  3438. return true;
  3439. default:
  3440. return false;
  3441. }
  3442. }
  3443. template <typename T>
  3444. bool Instr::BinaryCalculatorT(T src1Const, T src2Const, int64 *pResult, bool checkWouldTrap)
  3445. {
  3446. T value = 0;
  3447. switch (this->m_opcode)
  3448. {
  3449. #define DO_HANDLER(HANDLER, type) HANDLER(type##src1Const, type##src2Const)
  3450. #define BINARY_CASE_CHECK(OPCODE,HANDLER,CHECK_HANDLER,type) \
  3451. case Js::OpCode::##OPCODE: \
  3452. if (checkWouldTrap && DO_HANDLER(CHECK_HANDLER,type)) { return false; } \
  3453. value = DO_HANDLER(HANDLER,type); \
  3454. break;
  3455. #define BINARY_CASE(OPCODE,HANDLER,type) \
  3456. case Js::OpCode::##OPCODE: \
  3457. value = DO_HANDLER(HANDLER,type); \
  3458. break;
  3459. #define BINARY_U(OPCODE,HANDLER) BINARY_CASE(OPCODE,HANDLER,(typename SignedTypeTraits<T>::UnsignedType))
  3460. #define BINARY(OPCODE,HANDLER) BINARY_CASE(OPCODE,HANDLER,)
  3461. BINARY(CmEq_I4, Js::AsmJsMath::CmpEq)
  3462. BINARY(CmNeq_I4, Js::AsmJsMath::CmpNe)
  3463. BINARY(CmLt_I4, Js::AsmJsMath::CmpLt)
  3464. BINARY(CmGt_I4, Js::AsmJsMath::CmpGt)
  3465. BINARY(CmLe_I4, Js::AsmJsMath::CmpLe)
  3466. BINARY(CmGe_I4, Js::AsmJsMath::CmpGe)
  3467. BINARY_U(CmUnLt_I4, Js::AsmJsMath::CmpLt)
  3468. BINARY_U(CmUnGt_I4, Js::AsmJsMath::CmpGt)
  3469. BINARY_U(CmUnLe_I4, Js::AsmJsMath::CmpLe)
  3470. BINARY_U(CmUnGe_I4, Js::AsmJsMath::CmpGe)
  3471. BINARY(Add_I4, Js::AsmJsMath::Add)
  3472. BINARY(Sub_I4, Js::AsmJsMath::Sub)
  3473. BINARY(Mul_I4, Js::AsmJsMath::Mul)
  3474. BINARY(And_I4, Js::AsmJsMath::And)
  3475. BINARY(Or_I4, Js::AsmJsMath::Or)
  3476. BINARY(Xor_I4, Js::AsmJsMath::Xor)
  3477. BINARY(Shl_I4, Wasm::WasmMath::Shl)
  3478. BINARY(Shr_I4, Wasm::WasmMath::Shr)
  3479. BINARY_U(ShrU_I4, Wasm::WasmMath::ShrU)
  3480. BINARY_CASE_CHECK(DivU_I4, Js::AsmJsMath::DivChecked, Js::AsmJsMath::DivWouldTrap, (typename SignedTypeTraits<T>::UnsignedType))
  3481. BINARY_CASE_CHECK(Div_I4, Js::AsmJsMath::DivChecked, Js::AsmJsMath::DivWouldTrap, )
  3482. BINARY_CASE_CHECK(RemU_I4, Js::AsmJsMath::RemChecked, Js::AsmJsMath::RemWouldTrap, (typename SignedTypeTraits<T>::UnsignedType))
  3483. BINARY_CASE_CHECK(Rem_I4, Js::AsmJsMath::RemChecked, Js::AsmJsMath::RemWouldTrap, )
  3484. default:
  3485. return false;
  3486. #undef BINARY
  3487. #undef BINARY_U
  3488. }
  3489. *pResult = value;
  3490. return true;
  3491. }
  3492. template bool Instr::BinaryCalculatorT<int>(int src1Const64, int src2Const64, int64 *pResult, bool checkWouldTrap);
  3493. template bool Instr::BinaryCalculatorT<int64>(int64 src1Const64, int64 src2Const64, int64 *pResult, bool checkWouldTrap);
  3494. bool Instr::BinaryCalculator(IntConstType src1Const, IntConstType src2Const, IntConstType *pResult, IRType type)
  3495. {
  3496. IntConstType value = 0;
  3497. switch (this->m_opcode)
  3498. {
  3499. case Js::OpCode::Add_A:
  3500. if (IntConstMath::Add(src1Const, src2Const, type, &value))
  3501. {
  3502. return false;
  3503. }
  3504. break;
  3505. case Js::OpCode::Sub_A:
  3506. if (IntConstMath::Sub(src1Const, src2Const, type, &value))
  3507. {
  3508. return false;
  3509. }
  3510. break;
  3511. case Js::OpCode::Mul_A:
  3512. if (IntConstMath::Mul(src1Const, src2Const, type, &value))
  3513. {
  3514. return false;
  3515. }
  3516. if (value == 0)
  3517. {
  3518. // might be -0
  3519. // Bail for now...
  3520. return false;
  3521. }
  3522. break;
  3523. case Js::OpCode::Div_A:
  3524. if (src2Const == 0)
  3525. {
  3526. // Could fold to INF/-INF
  3527. // instr->HoistSrc1(Js::OpCode::Ld_A);
  3528. return false;
  3529. }
  3530. if (src1Const == 0 && src2Const < 0)
  3531. {
  3532. // folds to -0. Bail for now...
  3533. return false;
  3534. }
  3535. if (IntConstMath::Div(src1Const, src2Const, type, &value))
  3536. {
  3537. return false;
  3538. }
  3539. if (src1Const % src2Const != 0)
  3540. {
  3541. // Bail for now...
  3542. return false;
  3543. }
  3544. break;
  3545. case Js::OpCode::Rem_A:
  3546. if (src2Const == 0)
  3547. {
  3548. // Bail for now...
  3549. return false;
  3550. }
  3551. if (IntConstMath::Mod(src1Const, src2Const, type, &value))
  3552. {
  3553. return false;
  3554. }
  3555. if (value == 0)
  3556. {
  3557. // might be -0
  3558. // Bail for now...
  3559. return false;
  3560. }
  3561. break;
  3562. case Js::OpCode::Shl_A:
  3563. // We don't care about overflow here
  3564. value = src1Const << (src2Const & 0x1F);
  3565. break;
  3566. case Js::OpCode::Shr_A:
  3567. value = src1Const >> (src2Const & 0x1F);
  3568. break;
  3569. case Js::OpCode::ShrU_A:
  3570. value = ((UIntConstType)src1Const) >> (src2Const & 0x1F);
  3571. if (value < 0)
  3572. {
  3573. // ShrU produces a UInt32. If it doesn't fit in an Int32, bail as we don't
  3574. // track signs of int values.
  3575. return false;
  3576. }
  3577. break;
  3578. case Js::OpCode::And_A:
  3579. value = src1Const & src2Const;
  3580. break;
  3581. case Js::OpCode::Or_A:
  3582. value = src1Const | src2Const;
  3583. break;
  3584. case Js::OpCode::Xor_A:
  3585. value = src1Const ^ src2Const;
  3586. break;
  3587. case Js::OpCode::InlineMathMin:
  3588. value = src1Const < src2Const ? src1Const : src2Const;
  3589. break;
  3590. case Js::OpCode::InlineMathMax:
  3591. value = src1Const > src2Const ? src1Const : src2Const;
  3592. break;
  3593. default:
  3594. return false;
  3595. }
  3596. *pResult = value;
  3597. return true;
  3598. }
  3599. bool Instr::UnaryCalculator(IntConstType src1Const, IntConstType *pResult, IRType type)
  3600. {
  3601. IntConstType value = 0;
  3602. switch (this->m_opcode)
  3603. {
  3604. case Js::OpCode::Neg_A:
  3605. if (src1Const == 0)
  3606. {
  3607. // Could fold to -0.0
  3608. return false;
  3609. }
  3610. if (IntConstMath::Neg(src1Const, type, &value))
  3611. {
  3612. return false;
  3613. }
  3614. break;
  3615. case Js::OpCode::Not_A:
  3616. value = ~src1Const;
  3617. break;
  3618. case Js::OpCode::Ld_A:
  3619. if (this->HasBailOutInfo())
  3620. {
  3621. Assert(this->GetBailOutKind() == IR::BailOutExpectingInteger);
  3622. this->ClearBailOutInfo();
  3623. }
  3624. value = src1Const;
  3625. break;
  3626. case Js::OpCode::Conv_Num:
  3627. case Js::OpCode::Ld_I4:
  3628. value = src1Const;
  3629. break;
  3630. case Js::OpCode::Incr_A:
  3631. if (IntConstMath::Inc(src1Const, type, &value))
  3632. {
  3633. return false;
  3634. }
  3635. break;
  3636. case Js::OpCode::Decr_A:
  3637. if (IntConstMath::Dec(src1Const, type, &value))
  3638. {
  3639. return false;
  3640. }
  3641. break;
  3642. case Js::OpCode::InlineMathAbs:
  3643. if (src1Const == IntConstMin)
  3644. {
  3645. return false;
  3646. }
  3647. else
  3648. {
  3649. value = src1Const < 0 ? -src1Const : src1Const;
  3650. }
  3651. break;
  3652. case Js::OpCode::InlineMathClz:
  3653. DWORD clz;
  3654. DWORD src1Const32;
  3655. src1Const32 = (DWORD)src1Const;
  3656. if (_BitScanReverse(&clz, src1Const32))
  3657. {
  3658. value = 31 - clz;
  3659. }
  3660. else
  3661. {
  3662. value = 32;
  3663. }
  3664. this->ClearBailOutInfo();
  3665. break;
  3666. case Js::OpCode::InlineMathFloor:
  3667. value = src1Const;
  3668. this->ClearBailOutInfo();
  3669. break;
  3670. case Js::OpCode::InlineMathCeil:
  3671. value = src1Const;
  3672. this->ClearBailOutInfo();
  3673. break;
  3674. case Js::OpCode::InlineMathRound:
  3675. value = src1Const;
  3676. this->ClearBailOutInfo();
  3677. break;
  3678. case Js::OpCode::ToVar:
  3679. if (Js::TaggedInt::IsOverflow(src1Const))
  3680. {
  3681. return false;
  3682. }
  3683. else
  3684. {
  3685. value = src1Const;
  3686. this->ClearBailOutInfo();
  3687. break;
  3688. }
  3689. default:
  3690. return false;
  3691. }
  3692. *pResult = value;
  3693. return true;
  3694. }
  3695. #if ENABLE_DEBUG_CONFIG_OPTIONS
  3696. ///----------------------------------------------------------------------------
  3697. ///
  3698. /// Instr::DumpTestTrace
  3699. ///
  3700. /// Dump this instr in TestTrace.
  3701. ///
  3702. ///----------------------------------------------------------------------------
  3703. void
  3704. Instr::DumpTestTrace()
  3705. {
  3706. Output::Print(_u("opcode: %s "), Js::OpCodeUtil::GetOpCodeName(m_opcode));
  3707. SymOpnd * symOpnd;
  3708. if (this->m_opcode == Js::OpCode::NewScFunc || this->m_opcode == Js::OpCode::NewScGenFunc)
  3709. {
  3710. Output::Print(_u("\n"));
  3711. return;
  3712. }
  3713. Opnd * src1 = this->GetSrc1();
  3714. if (!src1)
  3715. {
  3716. Output::Print(_u("\n"));
  3717. return;
  3718. }
  3719. if (src1->GetKind() != OpndKindSym)
  3720. {
  3721. Output::Print(_u("\n"));
  3722. return;
  3723. }
  3724. symOpnd = src1->AsSymOpnd();
  3725. if (symOpnd->m_sym->IsPropertySym())
  3726. {
  3727. PropertySym *propertySym = symOpnd->m_sym->AsPropertySym();
  3728. switch (propertySym->m_fieldKind)
  3729. {
  3730. case PropertyKindData:
  3731. if (!JITManager::GetJITManager()->IsOOPJITEnabled())
  3732. {
  3733. Js::PropertyRecord const* fieldName = propertySym->GetFunc()->GetInProcThreadContext()->GetPropertyRecord(propertySym->m_propertyId);
  3734. Output::Print(_u("field: %s "), fieldName->GetBuffer());
  3735. break;
  3736. }
  3737. // else fall through
  3738. case PropertyKindSlots:
  3739. Output::Print(_u("field: [%d] "), propertySym->m_propertyId);
  3740. break;
  3741. case PropertyKindLocalSlots:
  3742. Output::Print(_u("field: l[%d] "), propertySym->m_propertyId);
  3743. break;
  3744. default:
  3745. break;
  3746. }
  3747. Output::Print(_u("\n"));
  3748. }
  3749. }
  3750. ///----------------------------------------------------------------------------
  3751. ///
  3752. /// Instr::DumpFieldCopyPropTestTrace
  3753. ///
  3754. /// Dump fieldcopyprop when testtrace is enabled.
  3755. ///
  3756. ///----------------------------------------------------------------------------
  3757. void
  3758. Instr::DumpFieldCopyPropTestTrace(bool inLandingPad)
  3759. {
  3760. switch (m_opcode)
  3761. {
  3762. case Js::OpCode::LdSlot:
  3763. case Js::OpCode::LdSlotArr:
  3764. case Js::OpCode::LdFld:
  3765. case Js::OpCode::LdFldForTypeOf:
  3766. case Js::OpCode::LdRootFld:
  3767. case Js::OpCode::LdRootFldForTypeOf:
  3768. case Js::OpCode::LdMethodFld:
  3769. case Js::OpCode::LdRootMethodFld:
  3770. case Js::OpCode::LdMethodFromFlags:
  3771. case Js::OpCode::ScopedLdMethodFld:
  3772. case Js::OpCode::TypeofElem:
  3773. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  3774. Output::Print(_u("TestTrace fieldcopyprop"));
  3775. if (inLandingPad)
  3776. {
  3777. Output::Print(_u(" [%s]"), _u("in landing pad"));
  3778. }
  3779. Output::Print(_u(": function %s (%s) "),
  3780. this->m_func->GetJITFunctionBody()->GetDisplayName(),
  3781. this->m_func->GetDebugNumberSet(debugStringBuffer));
  3782. if (this->IsInlined())
  3783. {
  3784. Output::Print(_u("inlined caller function %s (%s) "),
  3785. this->m_func->GetTopFunc()->GetJITFunctionBody()->GetDisplayName(),
  3786. this->m_func->GetTopFunc()->GetDebugNumberSet(debugStringBuffer));
  3787. }
  3788. this->DumpTestTrace();
  3789. default:
  3790. break;
  3791. }
  3792. }
  3793. #endif
  3794. #if ENABLE_DEBUG_CONFIG_OPTIONS
  3795. const char *
  3796. Instr::GetBailOutKindName() const
  3797. {
  3798. IR::BailOutKind kind = (IR::BailOutKind)0;
  3799. if (this->HasBailOutInfo())
  3800. {
  3801. kind |= this->GetBailOutKind();
  3802. }
  3803. if (this->HasAuxBailOut())
  3804. {
  3805. kind |= this->GetAuxBailOutKind();
  3806. }
  3807. return ::GetBailOutKindName(kind);
  3808. }
  3809. #endif
  3810. //
  3811. // Debug dumpers
  3812. //
  3813. #if DBG_DUMP
  3814. void
  3815. Instr::DumpByteCodeOffset()
  3816. {
  3817. if (m_func->HasByteCodeOffset())
  3818. {
  3819. Output::SkipToColumn(78);
  3820. Output::Print(_u("#"));
  3821. if (this->m_number != Js::Constants::NoByteCodeOffset)
  3822. {
  3823. Output::Print(_u("%04x"), this->m_number);
  3824. Output::Print(this->IsCloned()? _u("*") : _u(" "));
  3825. }
  3826. }
  3827. if (!this->m_func->IsTopFunc())
  3828. {
  3829. Output::SkipToColumn(78);
  3830. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  3831. Output::Print(_u(" Func #%s"), this->m_func->GetDebugNumberSet(debugStringBuffer));
  3832. }
  3833. #ifdef BAILOUT_INJECTION
  3834. if (this->bailOutByteCodeLocation != (uint)-1)
  3835. {
  3836. Output::SkipToColumn(85);
  3837. Output::Print(_u("@%4d"), this->bailOutByteCodeLocation);
  3838. }
  3839. #endif
  3840. if (this->m_opcode == Js::OpCode::InlineeStart)
  3841. {
  3842. Output::Print(_u(" %s"), this->m_func->GetJITFunctionBody()->GetDisplayName());
  3843. }
  3844. }
  3845. void
  3846. Instr::DumpGlobOptInstrString()
  3847. {
  3848. if(this->globOptInstrString && !PHASE_OFF(Js::DumpGlobOptInstrPhase, m_func))
  3849. {
  3850. Output::Print(_u("\n\n GLOBOPT INSTR: %s\n\n"), this->globOptInstrString);
  3851. }
  3852. }
  3853. ///----------------------------------------------------------------------------
  3854. ///
  3855. /// Instr::Dump
  3856. ///
  3857. /// Dump this instr.
  3858. ///
  3859. ///----------------------------------------------------------------------------
  3860. void
  3861. Instr::Dump(IRDumpFlags flags)
  3862. {
  3863. bool const AsmDumpMode = flags & IRDumpFlags_AsmDumpMode;
  3864. bool const SimpleForm = !!(flags & IRDumpFlags_SimpleForm);
  3865. bool const SkipByteCodeOffset = !!(flags & IRDumpFlags_SkipByteCodeOffset);
  3866. const auto PrintOpCodeName = [&]() {
  3867. Output::SkipToColumn(23);
  3868. #if DBG
  3869. WORD oldValue = 0;
  3870. if (this->highlight != 0)
  3871. {
  3872. oldValue = Output::SetConsoleForeground(this->highlight);
  3873. }
  3874. #endif
  3875. Output::Print(_u("%s "), Js::OpCodeUtil::GetOpCodeName(m_opcode));
  3876. #if DBG
  3877. if (this->highlight != 0)
  3878. {
  3879. Output::SetConsoleForeground(oldValue);
  3880. }
  3881. #endif
  3882. Output::SkipToColumn(38);
  3883. };
  3884. // forward decl before goto statement
  3885. Opnd * dst = nullptr;
  3886. if(m_opcode == Js::OpCode::BoundCheck || m_opcode == Js::OpCode::UnsignedBoundCheck)
  3887. {
  3888. PrintOpCodeName();
  3889. // src1 <= src2 + dst
  3890. Assert(GetSrc1());
  3891. if(GetSrc1()->IsIntConstOpnd())
  3892. {
  3893. Output::Print(_u("%d"), GetSrc1()->AsIntConstOpnd()->GetValue());
  3894. }
  3895. else
  3896. {
  3897. GetSrc1()->Dump(flags, m_func);
  3898. }
  3899. bool useLessThanOrEqual = true;
  3900. bool usePlus = true;
  3901. bool dumpSrc2 = false;
  3902. int32 offset = GetDst() ? GetDst()->AsIntConstOpnd()->AsInt32() : 0;
  3903. if(GetSrc2())
  3904. {
  3905. if(GetSrc2()->IsIntConstOpnd())
  3906. {
  3907. #if DBG
  3908. int32 temp;
  3909. Assert(!Int32Math::Add(offset, GetSrc2()->AsIntConstOpnd()->AsInt32(), &temp));
  3910. #endif
  3911. offset += GetSrc2()->AsIntConstOpnd()->AsInt32();
  3912. }
  3913. else
  3914. {
  3915. dumpSrc2 = true;
  3916. if(offset == -1)
  3917. {
  3918. useLessThanOrEqual = false; // < instead of <=
  3919. offset = 0;
  3920. }
  3921. else if(offset < 0 && offset != IntConstMin)
  3922. {
  3923. usePlus = false;
  3924. offset = -offset;
  3925. }
  3926. }
  3927. }
  3928. Output::Print(_u(" %S "), useLessThanOrEqual ? "<=" : "<");
  3929. if(dumpSrc2)
  3930. {
  3931. GetSrc2()->Dump(flags, m_func);
  3932. }
  3933. if(offset != 0)
  3934. {
  3935. if(dumpSrc2)
  3936. {
  3937. Output::Print(_u(" %C "), usePlus ? '+' : '-');
  3938. }
  3939. Output::Print(_u("%d"), offset);
  3940. }
  3941. goto PrintByteCodeOffsetEtc;
  3942. }
  3943. Output::SkipToColumn(4);
  3944. dst = this->GetDst();
  3945. if (dst)
  3946. {
  3947. dst->Dump(flags, this->m_func);
  3948. bool const dumpMarkTemp = PHASE_DUMP(Js::MarkTempPhase, m_func)
  3949. || PHASE_TRACE(Js::MarkTempPhase, m_func);
  3950. bool const dumpMarkTempNumber = dumpMarkTemp || PHASE_DUMP(Js::MarkTempNumberPhase, m_func)
  3951. || PHASE_TRACE(Js::MarkTempNumberPhase, m_func);
  3952. bool const dumpMarkTempObject = dumpMarkTemp || PHASE_DUMP(Js::MarkTempObjectPhase, m_func)
  3953. || PHASE_TRACE(Js::MarkTempObjectPhase, m_func);
  3954. if ((dumpMarkTempNumber && (this->dstIsTempNumberTransferred || this->dstIsTempNumber))
  3955. || (dumpMarkTempObject && this->dstIsTempObject))
  3956. {
  3957. Output::Print(_u("["));
  3958. if (dumpMarkTempNumber)
  3959. {
  3960. if (Js::Configuration::Global.flags.Verbose || OpCodeAttr::TempNumberProducing(this->m_opcode))
  3961. {
  3962. if (this->dstIsTempNumberTransferred)
  3963. {
  3964. Assert(this->dstIsTempNumber);
  3965. Output::Print(_u("x"));
  3966. }
  3967. else if (this->dstIsTempNumber)
  3968. {
  3969. Output::Print(_u("#"));
  3970. }
  3971. }
  3972. }
  3973. if (dumpMarkTempObject)
  3974. {
  3975. if (Js::Configuration::Global.flags.Verbose || OpCodeAttr::TempObjectProducing(this->m_opcode))
  3976. {
  3977. if (this->dstIsTempObject)
  3978. {
  3979. Output::Print(_u("o"));
  3980. }
  3981. }
  3982. }
  3983. Output::Print(_u("tmp]"));
  3984. }
  3985. if(PHASE_DUMP(Js::TrackNegativeZeroPhase, m_func->GetTopFunc()) && !ShouldCheckForNegativeZero())
  3986. {
  3987. Output::Print(_u("[-0]"));
  3988. }
  3989. if (PHASE_DUMP(Js::TypedArrayVirtualPhase, m_func->GetTopFunc()) && (!IsDstNotAlwaysConvertedToInt32() || !IsDstNotAlwaysConvertedToNumber()))
  3990. {
  3991. if (!IsDstNotAlwaysConvertedToInt32())
  3992. Output::Print(_u("[->i]"));
  3993. else
  3994. Output::Print(_u("[->n]"));
  3995. }
  3996. if(PHASE_DUMP(Js::TrackIntOverflowPhase, m_func->GetTopFunc()))
  3997. {
  3998. // ignoring 32-bit overflow ?
  3999. if(!ShouldCheckFor32BitOverflow())
  4000. {
  4001. // ignoring 32-bits or more ?
  4002. if(ShouldCheckForNon32BitOverflow())
  4003. Output::Print(_u("[OF %d]"), ignoreOverflowBitCount);
  4004. else
  4005. Output::Print(_u("[OF]"));
  4006. }
  4007. }
  4008. if (this->isSafeToSpeculate)
  4009. {
  4010. Output::SkipToColumn(19);
  4011. Output::Print(_u("<=="));
  4012. }
  4013. else
  4014. {
  4015. Output::SkipToColumn(20);
  4016. Output::Print(_u("="));
  4017. }
  4018. }
  4019. PrintOpCodeName();
  4020. if (this->IsBranchInstr())
  4021. {
  4022. BranchInstr * branchInstr = this->AsBranchInstr();
  4023. LabelInstr * targetInstr = branchInstr->GetTarget();
  4024. bool labelPrinted = true;
  4025. if (targetInstr == NULL)
  4026. {
  4027. // Checking the 'm_isMultiBranch' field here directly as well to bypass asserting when tracing IR builder
  4028. if(branchInstr->m_isMultiBranch && branchInstr->IsMultiBranch())
  4029. {
  4030. IR::MultiBranchInstr * multiBranchInstr = branchInstr->AsMultiBrInstr();
  4031. // If this MultiBranchInstr has been lowered to a machine instruction, which means
  4032. // its opcode is not Js::OpCode::MultiBr, there is no need to print the labels.
  4033. if (this->m_opcode == Js::OpCode::MultiBr)
  4034. {
  4035. multiBranchInstr->MapMultiBrLabels([](IR::LabelInstr * labelInstr) -> void
  4036. {
  4037. Output::Print(_u("$L%d "), labelInstr->m_id);
  4038. });
  4039. }
  4040. else
  4041. {
  4042. labelPrinted = false;
  4043. }
  4044. }
  4045. else
  4046. {
  4047. Output::Print(_u("??"));
  4048. }
  4049. }
  4050. else
  4051. {
  4052. Output::Print(_u("$L%d"), targetInstr->m_id);
  4053. }
  4054. if (this->GetSrc1() && labelPrinted)
  4055. {
  4056. Output::Print(_u(", "));
  4057. }
  4058. }
  4059. else if (this->IsPragmaInstr() && this->m_opcode == Js::OpCode::StatementBoundary)
  4060. {
  4061. Output::Print(_u("#%d"), this->AsPragmaInstr()->m_statementIndex);
  4062. }
  4063. // scope
  4064. {
  4065. Opnd * src1 = this->GetSrc1();
  4066. if (this->m_opcode == Js::OpCode::NewScFunc || this->m_opcode == Js::OpCode::NewScGenFunc)
  4067. {
  4068. Assert(src1->IsIntConstOpnd());
  4069. Js::ParseableFunctionInfo * function = nullptr;
  4070. if (!m_func->IsOOPJIT())
  4071. {
  4072. function = ((Js::ParseableFunctionInfo *)m_func->GetJITFunctionBody()->GetAddr())->GetNestedFunctionForExecution((uint)src1->AsIntConstOpnd()->GetValue())->GetParseableFunctionInfo();
  4073. }
  4074. Output::Print(_u("func:%s()"), function ? function->GetDisplayName() : _u("???"));
  4075. Output::Print(_u(", env:"));
  4076. this->GetSrc2()->AsRegOpnd()->m_sym->Dump(flags);
  4077. }
  4078. else if (src1)
  4079. {
  4080. src1->Dump(flags, this->m_func);
  4081. Opnd * src2 = this->GetSrc2();
  4082. if (src2)
  4083. {
  4084. Output::Print(_u(", "));
  4085. src2->Dump(flags, this->m_func);
  4086. }
  4087. }
  4088. }
  4089. if (this->IsByteCodeUsesInstr() || this->m_opcode == Js::OpCode::SpeculatedLoadFence)
  4090. {
  4091. ByteCodeUsesInstr* tempbcu = static_cast<ByteCodeUsesInstr*>(this);
  4092. if (tempbcu->GetByteCodeUpwardExposedUsed())
  4093. {
  4094. bool first = true;
  4095. FOREACH_BITSET_IN_SPARSEBV(id, tempbcu->GetByteCodeUpwardExposedUsed())
  4096. {
  4097. Output::Print(first? _u("s%d") : _u(", s%d"), id);
  4098. first = false;
  4099. }
  4100. NEXT_BITSET_IN_SPARSEBV;
  4101. }
  4102. if (tempbcu->propertySymUse)
  4103. {
  4104. Output::Print(_u(" PropSym: %d"), tempbcu->propertySymUse->m_id);
  4105. }
  4106. }
  4107. PrintByteCodeOffsetEtc:
  4108. if (!AsmDumpMode && !SkipByteCodeOffset)
  4109. {
  4110. this->DumpByteCodeOffset();
  4111. }
  4112. if (!SimpleForm)
  4113. {
  4114. if (this->HasBailOutInfo() || this->HasAuxBailOut())
  4115. {
  4116. BailOutInfo * bailOutInfo = this->GetBailOutInfo();
  4117. Output::SkipToColumn(85);
  4118. if (!AsmDumpMode)
  4119. {
  4120. Output::Print(_u("Bailout: #%04x"), bailOutInfo->bailOutOffset);
  4121. }
  4122. if (!bailOutInfo->bailOutFunc->IsTopFunc())
  4123. {
  4124. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  4125. Output::Print(_u(" Func %s"), bailOutInfo->bailOutFunc->GetDebugNumberSet(debugStringBuffer));
  4126. }
  4127. Output::Print(_u(" (%S)"), this->GetBailOutKindName());
  4128. }
  4129. }
  4130. if ((flags & IRDumpFlags_SkipEndLine) == 0)
  4131. {
  4132. Output::Print(_u("\n"));
  4133. }
  4134. }
  4135. #if DBG
  4136. bool
  4137. Instr::ShouldEmitIntRangeCheck()
  4138. {
  4139. // currently only emitting int range check for opnds of instructions with following opcodes:
  4140. return m_opcode == Js::OpCode::ToVar ||
  4141. m_opcode == Js::OpCode::LdElemI_A ||
  4142. m_opcode == Js::OpCode::LdMethodElem ||
  4143. m_opcode == Js::OpCode::StElemI_A ||
  4144. m_opcode == Js::OpCode::StElemI_A_Strict ||
  4145. m_opcode == Js::OpCode::StElemC;
  4146. }
  4147. #endif
  4148. ///----------------------------------------------------------------------------
  4149. ///
  4150. /// LabelInstr::Dump
  4151. ///
  4152. /// Dump this label.
  4153. ///
  4154. ///----------------------------------------------------------------------------
  4155. void
  4156. LabelInstr::Dump(IRDumpFlags flags)
  4157. {
  4158. if (this->m_block != NULL)
  4159. {
  4160. this->m_block->DumpHeader();
  4161. }
  4162. #if DBG
  4163. if (this->m_name != nullptr)
  4164. {
  4165. Output::Print(_u("$L%d (%s):"), this->m_id, this->m_name);
  4166. }
  4167. else
  4168. #endif
  4169. {
  4170. Output::Print(_u("$L%d:"), this->m_id);
  4171. }
  4172. if (this->isOpHelper)
  4173. {
  4174. Output::Print(_u(" [helper]"));
  4175. }
  4176. if (this->m_isLoopTop)
  4177. {
  4178. Output::Print(_u(" >>>>>>>>>>>>> LOOP TOP >>>>>>>>>>>>>"));
  4179. }
  4180. if (this->IsProfiledLabelInstr())
  4181. {
  4182. Output::SkipToColumn(50);
  4183. switch (this->AsProfiledLabelInstr()->loopImplicitCallFlags)
  4184. {
  4185. case Js::ImplicitCall_HasNoInfo:
  4186. Output::Print(_u("Implicit call: ???"));
  4187. break;
  4188. case Js::ImplicitCall_None:
  4189. Output::Print(_u("Implicit call: no"));
  4190. break;
  4191. default:
  4192. Output::Print(_u("Implicit call: yes"));
  4193. break;
  4194. }
  4195. }
  4196. if ((flags & (IRDumpFlags_AsmDumpMode | IRDumpFlags_SkipByteCodeOffset)) == 0)
  4197. {
  4198. this->DumpByteCodeOffset();
  4199. }
  4200. Output::Print(_u("\n"));
  4201. }
  4202. void
  4203. PragmaInstr::Dump(IRDumpFlags flags)
  4204. {
  4205. if (Js::Configuration::Global.flags.PrintSrcInDump && this->m_opcode == Js::OpCode::StatementBoundary)
  4206. {
  4207. Js::FunctionBody * functionBody = nullptr;
  4208. if (!m_func->IsOOPJIT())
  4209. {
  4210. functionBody = ((Js::FunctionBody*)m_func->GetJITFunctionBody()->GetAddr());
  4211. }
  4212. if (functionBody && !functionBody->GetUtf8SourceInfo()->GetIsLibraryCode())
  4213. {
  4214. functionBody->PrintStatementSourceLine(this->m_statementIndex);
  4215. }
  4216. }
  4217. __super::Dump(flags);
  4218. }
  4219. ///----------------------------------------------------------------------------
  4220. ///
  4221. /// Instr::Dump
  4222. ///
  4223. /// Dump a window of instructions around this instr.
  4224. ///
  4225. ///----------------------------------------------------------------------------
  4226. void
  4227. Instr::Dump(int window)
  4228. {
  4229. Instr * instr;
  4230. int i;
  4231. Output::Print(_u("-------------------------------------------------------------------------------"));
  4232. if (this == NULL)
  4233. {
  4234. return;
  4235. }
  4236. for (i = 0, instr = this; (instr->m_prev != NULL && i < window/2); instr = instr->m_prev, ++i)
  4237. {} // Nothing
  4238. for (i = 0; (instr != nullptr && i < window); instr = instr->m_next, ++i)
  4239. {
  4240. if (instr == this)
  4241. {
  4242. Output::Print(_u("=>"));
  4243. }
  4244. instr->Dump();
  4245. }
  4246. }
  4247. void
  4248. Instr::Dump()
  4249. {
  4250. this->Dump(IRDumpFlags_None);
  4251. }
  4252. void
  4253. Instr::DumpSimple()
  4254. {
  4255. this->Dump(IRDumpFlags_SimpleForm);
  4256. }
  4257. char16 *
  4258. Instr::DumpString()
  4259. {
  4260. Output::CaptureStart();
  4261. this->Dump();
  4262. return Output::CaptureEnd();
  4263. }
  4264. void
  4265. Instr::DumpRange(Instr *instrEnd)
  4266. {
  4267. Output::Print(_u("-------------------------------------------------------------------------------\n"));
  4268. FOREACH_INSTR_IN_RANGE(instr, this, instrEnd)
  4269. {
  4270. instr->Dump();
  4271. }
  4272. NEXT_INSTR_IN_RANGE;
  4273. Output::Print(_u("-------------------------------------------------------------------------------\n"));
  4274. }
  4275. #endif
  4276. } // namespace IR