IR.cpp 132 KB

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