IR.cpp 122 KB

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