Opnd.cpp 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045
  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. ///----------------------------------------------------------------------------
  9. ///
  10. /// Opnd::UseWithNewType
  11. ///
  12. /// Creates a Use (a copy if already in use or returns the same)
  13. /// and sets it type
  14. ///
  15. ///----------------------------------------------------------------------------
  16. Opnd*
  17. Opnd::UseWithNewType(IRType type, Func * func)
  18. {
  19. Opnd * res = this->Use(func);
  20. res->SetType(type);
  21. StackSym* sym = res->GetStackSym();
  22. if (sym)
  23. {
  24. if (TySize[sym->GetType()] < TySize[type])
  25. {
  26. Assert(!sym->IsAllocated());
  27. sym->m_type = type;
  28. }
  29. }
  30. return res;
  31. }
  32. bool
  33. Opnd::IsTaggedInt() const
  34. {
  35. return GetValueType().IsTaggedInt();
  36. }
  37. bool
  38. Opnd::IsTaggedValue() const
  39. {
  40. CompileAssert(!FLOATVAR || INT32VAR);
  41. #if FLOATVAR
  42. return GetValueType().IsNumber();
  43. #else
  44. return IsTaggedInt();
  45. #endif
  46. }
  47. bool
  48. Opnd::IsNotNumber() const
  49. {
  50. if (this->GetValueType().IsNotNumber())
  51. {
  52. return true;
  53. }
  54. if (this->IsRegOpnd())
  55. {
  56. const IR::RegOpnd* regOpnd = this->AsRegOpnd();
  57. if (regOpnd->m_sym == nullptr)
  58. {
  59. return true;
  60. }
  61. if (regOpnd->m_sym->m_isNotInt)
  62. {
  63. // m_isNotInt actually means "is not number". It should not be set to true for definitely-float values.
  64. return true;
  65. }
  66. }
  67. return false;
  68. }
  69. bool
  70. Opnd::IsNotInt() const
  71. {
  72. return IsNotNumber() || IsFloat();
  73. }
  74. bool
  75. Opnd::IsNotTaggedValue() const
  76. {
  77. if (!PHASE_OFF1(Js::OptTagChecksPhase) && this->GetValueType().IsNotTaggedValue())
  78. {
  79. return true;
  80. }
  81. return this->IsNotNumber();
  82. }
  83. bool
  84. Opnd::IsWriteBarrierTriggerableValue()
  85. {
  86. // Determines whether if an operand is used as a source in a store instruction, whether the store needs a write barrier
  87. // If it's a tagged value, we don't need a write barrier
  88. if (this->IsTaggedValue())
  89. {
  90. return false;
  91. }
  92. if (TySize[this->GetType()] != sizeof(void*))
  93. {
  94. return false;
  95. }
  96. #if DBG
  97. if (CONFIG_FLAG(ForceSoftwareWriteBarrier) && CONFIG_FLAG(VerifyBarrierBit))
  98. {
  99. return true; // No further optimization if we are in verification
  100. }
  101. #endif
  102. // If this operand is known address, then it doesn't need a write barrier, the address is either not a GC address or is pinned
  103. if (this->IsAddrOpnd() && this->AsAddrOpnd()->GetAddrOpndKind() == AddrOpndKindDynamicVar)
  104. {
  105. return false;
  106. }
  107. // If its null/boolean/undefined, we don't need a write barrier since the javascript library will keep those guys alive
  108. return !(this->GetValueType().IsBoolean() || this->GetValueType().IsNull() || this->GetValueType().IsUndefined());
  109. }
  110. /*
  111. * This is a devirtualized functions See the note above Opnd:Copy()
  112. */
  113. OpndKind Opnd::GetKind() const
  114. {
  115. return this->m_kind;
  116. }
  117. /*
  118. * This is a devirtualized functions See the note above Opnd:Copy()
  119. */
  120. Opnd *
  121. Opnd::CloneDef(Func *func)
  122. {
  123. switch (this->m_kind)
  124. {
  125. case OpndKindSym:
  126. if ((*static_cast<SymOpnd*>(this)).IsPropertySymOpnd())
  127. {
  128. return static_cast<PropertySymOpnd*>(this)->CloneDefInternalSub(func);
  129. }
  130. return static_cast<SymOpnd*>(this)->CloneDefInternal(func);
  131. case OpndKindReg:
  132. if ((*static_cast<RegOpnd*>(this)).IsArrayRegOpnd())
  133. {
  134. return static_cast<ArrayRegOpnd*>(this)->CloneDefInternalSub(func);
  135. }
  136. return static_cast<RegOpnd*>(this)->CloneDefInternal(func);
  137. case OpndKindIndir:
  138. return static_cast<IndirOpnd*>(this)->CloneDefInternal(func);
  139. case OpndKindList:
  140. return static_cast<ListOpnd*>(this)->CloneDefInternal(func);
  141. default:
  142. return this->Copy(func);
  143. };
  144. }
  145. /*
  146. * This is a devirtualized functions See the note above Opnd:Copy()
  147. */
  148. Opnd *
  149. Opnd::CloneUse(Func *func)
  150. {
  151. switch (this->m_kind)
  152. {
  153. case OpndKindSym:
  154. if ((*static_cast<SymOpnd*>(this)).IsPropertySymOpnd())
  155. {
  156. return static_cast<PropertySymOpnd*>(this)->CloneUseInternalSub(func);
  157. }
  158. return static_cast<SymOpnd*>(this)->CloneUseInternal(func);
  159. case OpndKindReg:
  160. if ((*static_cast<RegOpnd*>(this)).IsArrayRegOpnd())
  161. {
  162. return static_cast<ArrayRegOpnd*>(this)->CloneUseInternalSub(func);
  163. }
  164. return static_cast<RegOpnd*>(this)->CloneUseInternal(func);
  165. case OpndKindIndir:
  166. return static_cast<IndirOpnd*>(this)->CloneUseInternal(func);
  167. case OpndKindList:
  168. return static_cast<ListOpnd*>(this)->CloneUseInternal(func);
  169. default:
  170. return this->Copy(func);
  171. };
  172. }
  173. /*
  174. * This is a devirtualized functions See the note above Opnd:Copy()
  175. */
  176. void Opnd::Free(Func *func)
  177. {
  178. AssertMsg(!IsInUse(), "Attempting to free in use operand.");
  179. switch (this->m_kind)
  180. {
  181. case OpndKindIntConst:
  182. //NOTE: use to be Sealed do not do sub class checks like in CloneUse
  183. static_cast<IntConstOpnd*>(this)->FreeInternal(func);
  184. break;
  185. case OpndKindInt64Const:
  186. return static_cast<Int64ConstOpnd*>(this)->FreeInternal(func);
  187. case OpndKindSimd128Const:
  188. static_cast<Simd128ConstOpnd*>(this)->FreeInternal(func);
  189. break;
  190. case OpndKindFloatConst:
  191. static_cast<FloatConstOpnd*>(this)->FreeInternal(func);
  192. break;
  193. case OpndKindFloat32Const:
  194. static_cast<Float32ConstOpnd*>(this)->FreeInternal(func);
  195. break;
  196. case OpndKindHelperCall:
  197. static_cast<HelperCallOpnd*>(this)->FreeInternal(func);
  198. break;
  199. case OpndKindSym:
  200. static_cast<SymOpnd*>(this)->FreeInternal(func);
  201. break;
  202. case OpndKindReg:
  203. if ((*static_cast<RegOpnd*>(this)).IsArrayRegOpnd())
  204. {
  205. static_cast<ArrayRegOpnd*>(this)->FreeInternalSub(func);
  206. break;
  207. }
  208. static_cast<RegOpnd*>(this)->FreeInternal(func);
  209. break;
  210. case OpndKindAddr:
  211. static_cast<AddrOpnd*>(this)->FreeInternal(func);
  212. break;
  213. case OpndKindIndir:
  214. static_cast<IndirOpnd*>(this)->FreeInternal(func);
  215. break;
  216. case OpndKindList:
  217. static_cast<ListOpnd*>(this)->FreeInternal(func);
  218. break;
  219. case OpndKindMemRef:
  220. static_cast<MemRefOpnd*>(this)->FreeInternal(func);
  221. break;
  222. case OpndKindLabel:
  223. static_cast<LabelOpnd*>(this)->FreeInternal(func);
  224. break;
  225. case OpndKindRegBV:
  226. static_cast<RegBVOpnd*>(this)->FreeInternal(func);
  227. break;
  228. default:
  229. Assert(UNREACHED);
  230. __assume(UNREACHED);
  231. };
  232. #if DBG
  233. if (func->m_alloc->HasDelayFreeList())
  234. {
  235. this->isDeleted = true;
  236. }
  237. #endif
  238. }
  239. /*
  240. * This is a devirtualized functions See the note above Opnd:Copy()
  241. */
  242. bool Opnd::IsEqual(Opnd *opnd)
  243. {
  244. switch (this->m_kind)
  245. {
  246. case OpndKindIntConst:
  247. return static_cast<IntConstOpnd*>(this)->IsEqualInternal(opnd);
  248. case OpndKindInt64Const:
  249. return static_cast<Int64ConstOpnd*>(this)->IsEqualInternal(opnd);
  250. case OpndKindFloatConst:
  251. return static_cast<FloatConstOpnd*>(this)->IsEqualInternal(opnd);
  252. case OpndKindFloat32Const:
  253. return static_cast<Float32ConstOpnd*>(this)->IsEqualInternal(opnd);
  254. case OpndKindHelperCall:
  255. if ((*static_cast<HelperCallOpnd*>(this)).IsDiagHelperCallOpnd())
  256. {
  257. return static_cast<DiagHelperCallOpnd*>(this)->IsEqualInternalSub(opnd);
  258. }
  259. return static_cast<HelperCallOpnd*>(this)->IsEqualInternal(opnd);
  260. case OpndKindSym:
  261. //NOTE: use to be Sealed do not do sub class checks like in CloneUse
  262. return static_cast<SymOpnd*>(this)->IsEqualInternal(opnd);
  263. case OpndKindReg:
  264. //NOTE: not sealed but ArrayRegOpnd::isEqual function does not exist, default to RegOpnd only
  265. return static_cast<RegOpnd*>(this)->IsEqualInternal(opnd);
  266. case OpndKindAddr:
  267. return static_cast<AddrOpnd*>(this)->IsEqualInternal(opnd);
  268. case OpndKindIndir:
  269. return static_cast<IndirOpnd*>(this)->IsEqualInternal(opnd);
  270. case OpndKindList:
  271. return static_cast<ListOpnd*>(this)->IsEqualInternal(opnd);
  272. case OpndKindMemRef:
  273. return static_cast<MemRefOpnd*>(this)->IsEqualInternal(opnd);
  274. case OpndKindLabel:
  275. return static_cast<LabelOpnd*>(this)->IsEqualInternal(opnd);
  276. case OpndKindRegBV:
  277. return static_cast<RegBVOpnd*>(this)->IsEqualInternal(opnd);
  278. default:
  279. Assert(UNREACHED);
  280. __assume(UNREACHED);
  281. };
  282. }
  283. /*
  284. * This is a devirtualized functions If you inherit from any of the child classes of Opnd
  285. * And would like to override the default method behavior you must add an
  286. * Is<your new Opnd Type>Opnd() call and check for it like in examples
  287. * HelperCallOpnd, PropertySymOpnd, & RegOpnd
  288. */
  289. Opnd * Opnd::Copy(Func *func)
  290. {
  291. switch (this->m_kind)
  292. {
  293. case OpndKindIntConst:
  294. return static_cast<IntConstOpnd*>(this)->CopyInternal(func);
  295. case OpndKindInt64Const:
  296. return static_cast<Int64ConstOpnd*>(this)->CopyInternal(func);
  297. case OpndKindFloatConst:
  298. return static_cast<FloatConstOpnd*>(this)->CopyInternal(func);
  299. case OpndKindHelperCall:
  300. if ((*static_cast<HelperCallOpnd*>(this)).IsDiagHelperCallOpnd())
  301. {
  302. return static_cast<DiagHelperCallOpnd*>(this)->CopyInternalSub(func);
  303. }
  304. return static_cast<HelperCallOpnd*>(this)->CopyInternal(func);
  305. case OpndKindSym:
  306. if ((*static_cast<SymOpnd*>(this)).IsPropertySymOpnd())
  307. {
  308. return static_cast<PropertySymOpnd*>(this)->CopyInternalSub(func);
  309. }
  310. return static_cast<SymOpnd*>(this)->CopyInternal(func);
  311. case OpndKindReg:
  312. if ((*static_cast<RegOpnd*>(this)).IsArrayRegOpnd())
  313. {
  314. return static_cast<ArrayRegOpnd*>(this)->CopyInternalSub(func);
  315. }
  316. return static_cast<RegOpnd*>(this)->CopyInternal(func);
  317. case OpndKindAddr:
  318. return static_cast<AddrOpnd*>(this)->CopyInternal(func);
  319. case OpndKindIndir:
  320. return static_cast<IndirOpnd*>(this)->CopyInternal(func);
  321. case OpndKindList:
  322. return static_cast<ListOpnd*>(this)->CopyInternal(func);
  323. case OpndKindMemRef:
  324. return static_cast<MemRefOpnd*>(this)->CopyInternal(func);
  325. case OpndKindLabel:
  326. return static_cast<LabelOpnd*>(this)->CopyInternal(func);
  327. case OpndKindRegBV:
  328. return static_cast<RegBVOpnd*>(this)->CopyInternal(func);
  329. default:
  330. Assert(UNREACHED);
  331. __assume(UNREACHED);
  332. };
  333. }
  334. StackSym *
  335. Opnd::GetStackSym() const
  336. {
  337. switch (this->GetKind())
  338. {
  339. case OpndKindSym:
  340. return static_cast<SymOpnd const *>(this)->GetStackSymInternal();
  341. case OpndKindReg:
  342. return static_cast<RegOpnd const *>(this)->GetStackSymInternal();
  343. default:
  344. return nullptr;
  345. }
  346. }
  347. Sym*
  348. Opnd::GetSym() const
  349. {
  350. switch (this->GetKind())
  351. {
  352. case OpndKindSym:
  353. return static_cast<SymOpnd const *>(this)->m_sym;
  354. case OpndKindReg:
  355. return static_cast<RegOpnd const *>(this)->m_sym;
  356. default:
  357. return nullptr;
  358. }
  359. }
  360. int64
  361. Opnd::GetImmediateValue(Func* func)
  362. {
  363. switch (this->GetKind())
  364. {
  365. case OpndKindIntConst:
  366. return this->AsIntConstOpnd()->GetValue();
  367. case OpndKindInt64Const:
  368. return this->AsInt64ConstOpnd()->GetValue();
  369. case OpndKindAddr:
  370. return (intptr_t)this->AsAddrOpnd()->m_address;
  371. case OpndKindHelperCall:
  372. return (intptr_t)IR::GetMethodAddress(func->GetThreadContextInfo(), this->AsHelperCallOpnd());
  373. default:
  374. AssertMsg(UNREACHED, "Unexpected immediate opnd kind");
  375. return 0;
  376. }
  377. }
  378. #if defined(_M_ARM)
  379. int32
  380. Opnd::GetImmediateValueAsInt32(Func * func)
  381. {
  382. Assert(!IRType_IsInt64(this->GetType()));
  383. Assert(this->GetKind() != OpndKindInt64Const);
  384. return (int32)this->GetImmediateValue(func);
  385. }
  386. #endif
  387. BailoutConstantValue Opnd::GetConstValue()
  388. {
  389. BailoutConstantValue value;
  390. if (this->IsIntConstOpnd())
  391. {
  392. value.InitIntConstValue(this->AsIntConstOpnd()->GetValue(), this->m_type);
  393. }
  394. else if (this->IsFloatConstOpnd())
  395. {
  396. value.InitFloatConstValue(this->AsFloatConstOpnd()->m_value);
  397. }
  398. else
  399. {
  400. AssertMsg(this->IsAddrOpnd(), "Unexpected const sym");
  401. value.InitVarConstValue(this->AsAddrOpnd()->m_address);
  402. }
  403. return value;
  404. }
  405. void Opnd::SetValueType(const ValueType valueType)
  406. {
  407. if(m_isValueTypeFixed)
  408. {
  409. return;
  410. }
  411. // ArrayRegOpnd has information specific to the array type, so make sure that doesn't change
  412. Assert(
  413. !IsRegOpnd() ||
  414. !AsRegOpnd()->IsArrayRegOpnd() ||
  415. valueType.IsObject() && valueType.GetObjectType() == m_valueType.GetObjectType());
  416. m_valueType = valueType;
  417. }
  418. bool Opnd::IsScopeObjOpnd(Func * func)
  419. {
  420. if (IsRegOpnd())
  421. {
  422. return this->GetStackSym() == func->GetScopeObjSym();
  423. }
  424. else if(IsSymOpnd() && AsSymOpnd()->m_sym->IsPropertySym())
  425. {
  426. return this->AsSymOpnd()->m_sym->AsPropertySym()->m_stackSym == func->GetScopeObjSym();
  427. }
  428. return false;
  429. }
  430. ValueType Opnd::FindProfiledValueType()
  431. {
  432. if (!this->GetValueType().IsUninitialized())
  433. {
  434. return this->GetValueType();
  435. }
  436. // could be expanded to cover additional opnd kinds as well.
  437. if (this->IsRegOpnd() && this->AsRegOpnd()->m_sym->IsSingleDef())
  438. {
  439. IR::Instr * defInstr = this->AsRegOpnd()->m_sym->GetInstrDef();
  440. IR::Opnd * src1 = defInstr->GetSrc1();
  441. while(defInstr->m_opcode == Js::OpCode::Ld_A)
  442. {
  443. if (!src1->IsRegOpnd() || !src1->AsRegOpnd()->m_sym->IsSingleDef())
  444. {
  445. return ValueType::Uninitialized;
  446. }
  447. defInstr = src1->AsRegOpnd()->m_sym->GetInstrDef();
  448. src1 = defInstr->GetSrc1();
  449. }
  450. if (defInstr->GetDst()->GetValueType().IsAnyArray())
  451. {
  452. return defInstr->GetDst()->GetValueType().ToLikely();
  453. }
  454. else
  455. {
  456. return defInstr->GetDst()->GetValueType();
  457. }
  458. }
  459. return ValueType::Uninitialized;
  460. }
  461. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  462. void Opnd::DumpValueType()
  463. {
  464. if(m_valueType.IsUninitialized())
  465. {
  466. return;
  467. }
  468. if(!CONFIG_FLAG(Verbose))
  469. {
  470. // Skip printing the value type when it's obvious since verbose mode is off
  471. switch(this->GetKind())
  472. {
  473. case OpndKindIntConst:
  474. case OpndKindInt64Const:
  475. case OpndKindFloatConst:
  476. return;
  477. case OpndKindReg:
  478. {
  479. StackSym *const sym = this->AsRegOpnd()->m_sym;
  480. if(sym && (
  481. sym->IsInt32() ||
  482. sym->IsFloat32() ||
  483. sym->IsFloat64() ||
  484. sym->IsInt64() ||
  485. sym->IsUint64()
  486. ))
  487. {
  488. return;
  489. }
  490. break;
  491. }
  492. case OpndKindAddr:
  493. if(this->AsAddrOpnd()->m_address && this->AsAddrOpnd()->IsVar())
  494. {
  495. IR::AddrOpnd *addrOpnd = this->AsAddrOpnd();
  496. Js::Var address = addrOpnd->decodedValue ? addrOpnd->decodedValue : addrOpnd->m_address;
  497. // Tagged int might be encoded here, so check the type
  498. if (addrOpnd->GetAddrOpndKind() == AddrOpndKindConstantVar
  499. || Js::TaggedInt::Is(address) || (
  500. #if !FLOATVAR
  501. !JITManager::GetJITManager()->IsOOPJITEnabled() &&
  502. #endif
  503. Js::JavascriptNumber::Is_NoTaggedIntCheck(address)))
  504. {
  505. return;
  506. }
  507. }
  508. break;
  509. }
  510. }
  511. DumpValueType(m_valueType);
  512. }
  513. void Opnd::DumpValueType(const ValueType valueType)
  514. {
  515. if(valueType.IsUninitialized())
  516. {
  517. return;
  518. }
  519. char valueTypeStr[VALUE_TYPE_MAX_STRING_SIZE];
  520. valueType.ToString(valueTypeStr);
  521. Output::Print(_u("[%S]"), valueTypeStr);
  522. }
  523. #endif
  524. IntConstOpnd *Opnd::CreateUint32Opnd(const uint i, Func *const func)
  525. {
  526. return IntConstOpnd::New(i, TyUint32, func, true);
  527. }
  528. IntConstOpnd *Opnd::CreateProfileIdOpnd(const Js::ProfileId profileId, Func *const func)
  529. {
  530. CompileAssert(sizeof(profileId) == sizeof(uint16));
  531. return IntConstOpnd::New(profileId, TyUint16, func, true);
  532. }
  533. IntConstOpnd *Opnd::CreateInlineCacheIndexOpnd(const Js::InlineCacheIndex inlineCacheIndex, Func *const func)
  534. {
  535. CompileAssert(sizeof(inlineCacheIndex) == sizeof(uint));
  536. return CreateUint32Opnd(inlineCacheIndex, func);
  537. }
  538. RegOpnd *Opnd::CreateFramePointerOpnd(Func *const func)
  539. {
  540. return RegOpnd::New(nullptr, LowererMD::GetRegFramePointer(), TyMachPtr, func);
  541. }
  542. ///----------------------------------------------------------------------------
  543. ///
  544. /// SymOpnd::New
  545. ///
  546. /// Creates a new SymOpnd.
  547. ///
  548. ///----------------------------------------------------------------------------
  549. SymOpnd *
  550. SymOpnd::New(Sym *sym, IRType type, Func *func)
  551. {
  552. return SymOpnd::New(sym, 0, type, func);
  553. }
  554. SymOpnd *
  555. SymOpnd::New(Sym *sym, uint32 offset, IRType type, Func *func)
  556. {
  557. SymOpnd * symOpnd;
  558. AssertMsg(sym, "A SymOpnd needs a valid symbol.");
  559. symOpnd = JitAnew(func->m_alloc, IR::SymOpnd);
  560. symOpnd->m_sym = sym;
  561. symOpnd->m_offset = offset;
  562. symOpnd->m_type = type;
  563. symOpnd->SetIsJITOptimizedReg(false);
  564. symOpnd->m_kind = OpndKindSym;
  565. return symOpnd;
  566. }
  567. ///----------------------------------------------------------------------------
  568. ///
  569. /// SymOpnd::Copy
  570. ///
  571. /// Returns a copy of this opnd.
  572. ///
  573. ///----------------------------------------------------------------------------
  574. SymOpnd *
  575. SymOpnd::CopyInternal(Func *func)
  576. {
  577. Assert(m_kind == OpndKindSym);
  578. SymOpnd * newOpnd;
  579. newOpnd = SymOpnd::New(m_sym, m_offset, m_type, func);
  580. newOpnd->m_valueType = m_valueType;
  581. newOpnd->canStoreTemp = this->canStoreTemp;
  582. newOpnd->SetIsJITOptimizedReg(this->GetIsJITOptimizedReg());
  583. return newOpnd;
  584. }
  585. SymOpnd *
  586. SymOpnd::CloneDefInternal(Func *func)
  587. {
  588. Assert(m_kind == OpndKindSym);
  589. Sym *sym = this->m_sym;
  590. if (sym->IsStackSym() && sym->AsStackSym()->m_isSingleDef)
  591. {
  592. StackSym * oldSym = sym->AsStackSym();
  593. StackSym * newSym = oldSym->CloneDef(func)->AsStackSym();
  594. if (func->GetCloner()->clonedInstrGetOrigArgSlotSym && oldSym->IsArgSlotSym())
  595. {
  596. Assert(newSym != oldSym);
  597. this->m_sym = newSym;
  598. newSym->m_instrDef = oldSym->m_instrDef;
  599. oldSym->m_instrDef = nullptr;
  600. sym = oldSym;
  601. }
  602. else
  603. {
  604. sym = newSym;
  605. }
  606. }
  607. SymOpnd * newOpnd = SymOpnd::New(sym, m_offset, m_type, func);
  608. return newOpnd;
  609. }
  610. SymOpnd *
  611. SymOpnd::CloneUseInternal(Func *func)
  612. {
  613. Assert(m_kind == OpndKindSym);
  614. Sym *sym = this->m_sym;
  615. if (sym->IsStackSym() && sym->AsStackSym()->m_isSingleDef)
  616. {
  617. StackSym * oldSym = sym->AsStackSym();
  618. StackSym * newSym = oldSym->CloneUse(func)->AsStackSym();
  619. if (func->GetCloner()->clonedInstrGetOrigArgSlotSym && oldSym->IsArgSlotSym())
  620. {
  621. Assert(newSym != oldSym);
  622. this->m_sym = newSym;
  623. sym = oldSym;
  624. }
  625. else
  626. {
  627. sym = newSym;
  628. }
  629. }
  630. SymOpnd * newOpnd = SymOpnd::New(sym, m_offset, m_type, func);
  631. return newOpnd;
  632. }
  633. StackSym *
  634. SymOpnd::GetStackSymInternal() const
  635. {
  636. return (this->m_sym && this->m_sym->IsStackSym()) ? this->m_sym->AsStackSym() : nullptr;
  637. }
  638. ///----------------------------------------------------------------------------
  639. ///
  640. /// SymOpnd::IsEqual
  641. /// The SymOpnd's offset is 0 if it is called before regalloc. For Stack symopnd,
  642. /// compare the type and symbol's offsets only when the symbol's isAllocated is true.
  643. /// For other cases, compare the type, syms and offsets.
  644. /// For example, following two instructions after RegAlloc phase:
  645. /// iarg65535(s534)<0>.i32 = MOV (NULL).var
  646. /// iarg65535(s533)<0>.i32 = MOV (NULL).var
  647. /// are actually same instructions after encoding: mov dword ptr[ebp-0x1c], 0x0
  648. /// Here for dst stack symOpnd, m_sym are different: s534 vs. s533, but offsets and
  649. /// types are the same. So this function will report true if isAllocated is true.
  650. /// Note: for property symopnd, still compare type, offset and sym.
  651. ///
  652. ///----------------------------------------------------------------------------
  653. bool
  654. SymOpnd::IsEqualInternal(Opnd *opnd)
  655. {
  656. Assert(m_kind == OpndKindSym);
  657. Assert(opnd);
  658. if (!opnd->IsSymOpnd() || this->GetType() != opnd->GetType())
  659. {
  660. return false;
  661. }
  662. SymOpnd *opndSym = opnd->AsSymOpnd();
  663. Assert(opndSym);
  664. StackSym *thisStackSym = this->GetStackSymInternal();
  665. StackSym *opndStackSym = opndSym->GetStackSymInternal();
  666. if (thisStackSym && opndStackSym && thisStackSym->IsAllocated() && opndStackSym->IsAllocated())
  667. {
  668. return thisStackSym->m_offset == opndStackSym->m_offset;
  669. }
  670. else
  671. {
  672. return m_sym == opndSym->m_sym && m_offset == opndSym->m_offset;
  673. }
  674. }
  675. void
  676. SymOpnd::FreeInternal(Func *func)
  677. {
  678. Assert(m_kind == OpndKindSym);
  679. JitAdelete(func->m_alloc, this);
  680. }
  681. RegOpnd *SymOpnd::CreatePropertyOwnerOpnd(Func *const func) const
  682. {
  683. Assert(m_sym->IsPropertySym());
  684. Assert(func);
  685. StackSym *const propertyOwnerSym = m_sym->AsPropertySym()->m_stackSym;
  686. RegOpnd *const propertyOwnerOpnd = RegOpnd::New(propertyOwnerSym, propertyOwnerSym->GetType(), func);
  687. propertyOwnerOpnd->SetValueType(GetPropertyOwnerValueType());
  688. return propertyOwnerOpnd;
  689. }
  690. PropertySymOpnd *
  691. PropertySymOpnd::New(PropertySym *propertySym, uint inlineCacheIndex, IRType type, Func *func)
  692. {
  693. PropertySymOpnd *newOpnd = IR::PropertySymOpnd::New(propertySym, type, func);
  694. newOpnd->Init(inlineCacheIndex, func);
  695. return newOpnd;
  696. }
  697. void
  698. PropertySymOpnd::Init(uint inlineCacheIndex, Func *func)
  699. {
  700. this->Init(inlineCacheIndex,
  701. inlineCacheIndex != -1 ? func->GetRuntimeInlineCache(inlineCacheIndex) : 0,
  702. inlineCacheIndex != -1 ? func->GetRuntimePolymorphicInlineCache(inlineCacheIndex) : nullptr,
  703. inlineCacheIndex != -1 ? func->GetObjTypeSpecFldInfo(inlineCacheIndex) : nullptr,
  704. inlineCacheIndex != -1 ? func->GetPolyCacheUtilToInitialize(inlineCacheIndex) : PolymorphicInlineCacheUtilizationMinValue);
  705. }
  706. PropertySymOpnd *
  707. PropertySymOpnd::New(PropertySym *propertySym, IRType type, Func *func)
  708. {
  709. PropertySymOpnd *newOpnd = JitAnew(func->m_alloc, IR::PropertySymOpnd);
  710. newOpnd->m_sym = propertySym;
  711. newOpnd->m_offset = 0;
  712. newOpnd->m_type = type;
  713. newOpnd->SetObjTypeSpecFldInfo(nullptr);
  714. newOpnd->finalType = JITTypeHolder(nullptr);
  715. newOpnd->monoGuardType = JITTypeHolder(nullptr);
  716. newOpnd->guardedPropOps = nullptr;
  717. newOpnd->writeGuards = nullptr;
  718. newOpnd->objTypeSpecFlags = 0;
  719. newOpnd->isPropertySymOpnd = true;
  720. newOpnd->checkedTypeSetIndex = (uint16)-1;
  721. newOpnd->m_kind = OpndKindSym;
  722. return newOpnd;
  723. }
  724. void
  725. PropertySymOpnd::Init(uint inlineCacheIndex, intptr_t runtimeInlineCache, JITTimePolymorphicInlineCache * runtimePolymorphicInlineCache, ObjTypeSpecFldInfo* objTypeSpecFldInfo, byte polyCacheUtil)
  726. {
  727. this->m_inlineCacheIndex = inlineCacheIndex;
  728. this->m_runtimeInlineCache = runtimeInlineCache;
  729. this->m_runtimePolymorphicInlineCache = runtimePolymorphicInlineCache;
  730. this->m_polyCacheUtil = polyCacheUtil;
  731. this->SetObjTypeSpecFldInfo(objTypeSpecFldInfo);
  732. this->SetIsJITOptimizedReg(false);
  733. }
  734. PropertySymOpnd *
  735. PropertySymOpnd::CopyCommon(Func *func)
  736. {
  737. PropertySymOpnd *newOpnd = PropertySymOpnd::New(this->m_sym->AsPropertySym(), this->m_type, func);
  738. newOpnd->m_valueType = this->m_valueType;
  739. newOpnd->m_inlineCacheIndex = this->m_inlineCacheIndex;
  740. newOpnd->m_runtimeInlineCache = this->m_runtimeInlineCache;
  741. newOpnd->m_runtimePolymorphicInlineCache = this->m_runtimePolymorphicInlineCache;
  742. newOpnd->canStoreTemp = this->canStoreTemp;
  743. return newOpnd;
  744. }
  745. PropertySymOpnd *
  746. PropertySymOpnd::CopyWithoutFlowSensitiveInfo(Func *func)
  747. {
  748. PropertySymOpnd *newOpnd = CopyCommon(func);
  749. newOpnd->SetObjTypeSpecFldInfo(this->objTypeSpecFldInfo);
  750. // This field is not flow sensitive. It is only on if the instruction is CheckFixedMethodFld. If we ever
  751. // hoist CheckFixedMethodFld (or otherwise copy it), we must make sure not to change the opcode.
  752. newOpnd->usesFixedValue = this->usesFixedValue;
  753. // Note that the following fields are flow sensitive. If we're cloning this operand in order to attach it to
  754. // an instruction elsewhere in the flow (e.g. field hoisting or copy propagation), these fields cannot be copied.
  755. // If the caller knows some of them can be safely copied, the caller must do so manually.
  756. Assert(newOpnd->typeCheckSeqFlags == 0);
  757. Assert(newOpnd->finalType == nullptr);
  758. Assert(newOpnd->guardedPropOps == nullptr);
  759. Assert(newOpnd->writeGuards == nullptr);
  760. newOpnd->SetIsJITOptimizedReg(this->GetIsJITOptimizedReg());
  761. return newOpnd;
  762. }
  763. PropertySymOpnd *
  764. PropertySymOpnd::CopyInternalSub(Func *func)
  765. {
  766. Assert(m_kind == OpndKindSym && this->IsPropertySymOpnd());
  767. PropertySymOpnd *newOpnd = CopyCommon(func);
  768. newOpnd->objTypeSpecFldInfo = this->objTypeSpecFldInfo;
  769. newOpnd->usesAuxSlot = usesAuxSlot;
  770. newOpnd->slotIndex = slotIndex;
  771. newOpnd->checkedTypeSetIndex = checkedTypeSetIndex;
  772. newOpnd->objTypeSpecFlags = this->objTypeSpecFlags;
  773. newOpnd->finalType = this->finalType;
  774. newOpnd->guardedPropOps = this->guardedPropOps != nullptr ? this->guardedPropOps->CopyNew() : nullptr;
  775. newOpnd->writeGuards = this->writeGuards != nullptr ? this->writeGuards->CopyNew() : nullptr;
  776. newOpnd->SetIsJITOptimizedReg(this->GetIsJITOptimizedReg());
  777. return newOpnd;
  778. }
  779. bool
  780. PropertySymOpnd::IsObjectHeaderInlined() const
  781. {
  782. JITTypeHolder type(nullptr);
  783. if (this->IsMono())
  784. {
  785. type = this->GetType();
  786. }
  787. else if (this->HasEquivalentTypeSet())
  788. {
  789. type = this->GetFirstEquivalentType();
  790. }
  791. if (type != nullptr && Js::DynamicType::Is(type->GetTypeId()))
  792. {
  793. return type->GetTypeHandler()->IsObjectHeaderInlinedTypeHandler();
  794. }
  795. return false;
  796. }
  797. bool
  798. PropertySymOpnd::ChangesObjectLayout() const
  799. {
  800. JITTypeHolder cachedType = this->IsMono() ? this->GetType() : this->GetFirstEquivalentType();
  801. JITTypeHolder finalType = this->GetFinalType();
  802. if (finalType != nullptr && Js::DynamicType::Is(finalType->GetTypeId()))
  803. {
  804. // This is the case where final type opt may cause pro-active type transition to take place.
  805. Assert(cachedType != nullptr && Js::DynamicType::Is(cachedType->GetTypeId()));
  806. return cachedType->GetTypeHandler()->GetInlineSlotCapacity() != finalType->GetTypeHandler()->GetInlineSlotCapacity() ||
  807. cachedType->GetTypeHandler()->GetOffsetOfInlineSlots() != finalType->GetTypeHandler()->GetOffsetOfInlineSlots();
  808. }
  809. if (!this->HasInitialType())
  810. {
  811. return false;
  812. }
  813. JITTypeHolder initialType = this->GetInitialType();
  814. if (initialType != nullptr && Js::DynamicType::Is(initialType->GetTypeId()))
  815. {
  816. // This is the case where the type transition actually occurs. (This is the only case that's detectable
  817. // during the loop pre-pass, since final types are not in place yet.)
  818. Assert(cachedType != nullptr && Js::DynamicType::Is(cachedType->GetTypeId()));
  819. const JITTypeHandler * cachedTypeHandler = cachedType->GetTypeHandler();
  820. const JITTypeHandler * initialTypeHandler = initialType->GetTypeHandler();
  821. return cachedTypeHandler->GetInlineSlotCapacity() != initialTypeHandler->GetInlineSlotCapacity() ||
  822. cachedTypeHandler->GetOffsetOfInlineSlots() != initialTypeHandler->GetOffsetOfInlineSlots();
  823. }
  824. return false;
  825. }
  826. void
  827. PropertySymOpnd::UpdateSlotForFinalType()
  828. {
  829. JITTypeHolder finalType = this->GetFinalType();
  830. Assert(this->IsMono() || this->checkedTypeSetIndex != (uint16)-1);
  831. JITTypeHolder cachedType =
  832. this->IsMono() ? this->GetType() : this->GetEquivalentTypeSet()->GetType(checkedTypeSetIndex);
  833. Assert(finalType != nullptr && Js::DynamicType::Is(finalType->GetTypeId()));
  834. Assert(cachedType != nullptr && Js::DynamicType::Is(cachedType->GetTypeId()));
  835. if (finalType == cachedType)
  836. {
  837. return;
  838. }
  839. // TODO: OOP JIT: should assert about runtime type handler addr
  840. Assert(cachedType->GetTypeHandler() != finalType->GetTypeHandler());
  841. if (cachedType->GetTypeHandler()->GetInlineSlotCapacity() == finalType->GetTypeHandler()->GetInlineSlotCapacity() &&
  842. cachedType->GetTypeHandler()->GetOffsetOfInlineSlots() == finalType->GetTypeHandler()->GetOffsetOfInlineSlots())
  843. {
  844. // Nothing can change, since the variables aren't changing.
  845. return;
  846. }
  847. // Get the slot index and figure out the property index
  848. uint16 index = this->GetSlotIndex();
  849. if (this->UsesAuxSlot())
  850. {
  851. index += cachedType->GetTypeHandler()->GetInlineSlotCapacity();
  852. }
  853. else
  854. {
  855. index -= cachedType->GetTypeHandler()->GetOffsetOfInlineSlots() / sizeof(Js::Var);
  856. }
  857. // Figure out the slot index and aux-ness from the property index
  858. if (index >= finalType->GetTypeHandler()->GetInlineSlotCapacity())
  859. {
  860. this->SetUsesAuxSlot(true);
  861. index -= finalType->GetTypeHandler()->GetInlineSlotCapacity();
  862. }
  863. else
  864. {
  865. this->SetUsesAuxSlot(false);
  866. index += finalType->GetTypeHandler()->GetOffsetOfInlineSlots() / sizeof(Js::Var);
  867. }
  868. this->SetSlotIndex(index);
  869. }
  870. bool PropertySymOpnd::HasFinalType() const
  871. {
  872. return this->finalType != nullptr;
  873. }
  874. PropertySymOpnd *
  875. PropertySymOpnd::CloneDefInternalSub(Func *func)
  876. {
  877. return this->CopyInternalSub(func);
  878. }
  879. PropertySymOpnd *
  880. PropertySymOpnd::CloneUseInternalSub(Func *func)
  881. {
  882. return this->CopyInternalSub(func);
  883. }
  884. RegOpnd::RegOpnd(StackSym *sym, RegNum reg, IRType type)
  885. {
  886. Initialize(sym, reg, type);
  887. }
  888. RegOpnd::RegOpnd(const RegOpnd &other, StackSym *const sym)
  889. {
  890. Initialize(sym, other.m_reg, other.m_type);
  891. m_valueType = other.m_valueType;
  892. SetIsJITOptimizedReg(other.GetIsJITOptimizedReg());
  893. m_dontDeadStore = other.m_dontDeadStore;
  894. m_wasNegativeZeroPreventedByBailout = other.m_wasNegativeZeroPreventedByBailout;
  895. #if DBG
  896. m_symValueFrozen = other.m_symValueFrozen;
  897. #endif
  898. }
  899. void RegOpnd::Initialize(StackSym *sym, RegNum reg, IRType type)
  900. {
  901. AssertMsg(sym || reg != RegNOREG, "A RegOpnd needs a valid symbol or register.");
  902. Assert(!sym || sym->GetType() != TyMisc);
  903. m_kind = OpndKindReg;
  904. m_sym = sym;
  905. SetReg(reg);
  906. m_type = type;
  907. m_isTempLastUse = false;
  908. m_isCallArg = false;
  909. SetIsJITOptimizedReg(false);
  910. m_dontDeadStore = false;
  911. m_fgPeepTmp = false;
  912. m_wasNegativeZeroPreventedByBailout = false;
  913. m_isArrayRegOpnd = false;
  914. #if DBG
  915. m_symValueFrozen = false;
  916. #endif
  917. }
  918. ///----------------------------------------------------------------------------
  919. ///
  920. /// RegOpnd::New
  921. ///
  922. /// Creates a new RegOpnd.
  923. ///
  924. ///----------------------------------------------------------------------------
  925. RegOpnd *
  926. RegOpnd::New(IRType type, Func *func)
  927. {
  928. return RegOpnd::New(StackSym::New(type, func), RegNOREG, type, func);
  929. }
  930. IR::RegOpnd *
  931. RegOpnd::New(RegNum reg, IRType type, Func *func)
  932. {
  933. return RegOpnd::New(StackSym::New(type, func), reg, type, func);
  934. }
  935. RegOpnd *
  936. RegOpnd::New(StackSym *sym, IRType type, Func *func)
  937. {
  938. return RegOpnd::New(sym, RegNOREG, type, func);
  939. }
  940. RegOpnd *
  941. RegOpnd::New(StackSym *sym, RegNum reg, IRType type, Func *func)
  942. {
  943. return JitAnew(func->m_alloc, IR::RegOpnd, sym, reg, type);
  944. }
  945. ///----------------------------------------------------------------------------
  946. ///
  947. /// RegOpnd::Copy
  948. ///
  949. /// Returns a copy of this opnd.
  950. ///
  951. ///----------------------------------------------------------------------------
  952. RegOpnd *
  953. RegOpnd::CopyInternal(StackSym * sym, Func *func)
  954. {
  955. Assert(m_kind == OpndKindReg);
  956. return JitAnew(func->m_alloc, IR::RegOpnd, *this, sym);
  957. }
  958. RegOpnd *
  959. RegOpnd::CopyInternal(Func *func)
  960. {
  961. return CopyInternal(m_sym, func);
  962. }
  963. RegOpnd *
  964. RegOpnd::CloneDefInternal(Func *func)
  965. {
  966. StackSym * sym = m_sym ? m_sym->CloneDef(func) : nullptr;
  967. return CopyInternal(sym, func);
  968. }
  969. RegOpnd *
  970. RegOpnd::CloneUseInternal(Func *func)
  971. {
  972. StackSym * sym = m_sym ? m_sym->CloneUse(func) : nullptr;
  973. return CopyInternal(sym, func);
  974. }
  975. StackSym *
  976. RegOpnd::GetStackSymInternal() const
  977. {
  978. return this->m_sym;
  979. }
  980. StackSym *
  981. RegOpnd::TryGetStackSym(Opnd *const opnd)
  982. {
  983. return opnd && opnd->IsRegOpnd() ? opnd->AsRegOpnd()->m_sym : nullptr;
  984. }
  985. ///----------------------------------------------------------------------------
  986. ///
  987. /// RegOpnd::IsEqual
  988. ///
  989. ///----------------------------------------------------------------------------
  990. bool
  991. RegOpnd::IsEqualInternal(Opnd *opnd)
  992. {
  993. Assert(m_kind == OpndKindReg);
  994. return IsSameRegUntyped(opnd) && (this->GetType() == opnd->GetType());
  995. }
  996. void
  997. RegOpnd::FreeInternal(Func *func)
  998. {
  999. Assert(m_kind == OpndKindReg);
  1000. JitAdelete(func->m_alloc, this);
  1001. }
  1002. ///----------------------------------------------------------------------------
  1003. ///
  1004. /// RegOpnd::IsSameReg
  1005. ///
  1006. /// Same as IsEqual except the type only need to be equal size
  1007. ///
  1008. ///----------------------------------------------------------------------------
  1009. bool
  1010. RegOpnd::IsSameReg(Opnd *opnd)
  1011. {
  1012. return IsSameRegUntyped(opnd) && (TySize[this->GetType()] == TySize[opnd->GetType()]);
  1013. }
  1014. ///----------------------------------------------------------------------------
  1015. ///
  1016. /// RegOpnd::IsSameRegUntyped
  1017. ///
  1018. /// Same as IsEqual but without any types comparison
  1019. ///
  1020. ///----------------------------------------------------------------------------
  1021. bool
  1022. RegOpnd::IsSameRegUntyped(Opnd *opnd)
  1023. {
  1024. if (!opnd->IsRegOpnd())
  1025. {
  1026. return false;
  1027. }
  1028. RegOpnd *regOpnd = opnd->AsRegOpnd();
  1029. if (m_reg != RegNOREG)
  1030. {
  1031. return m_reg == regOpnd->m_reg;
  1032. }
  1033. return m_sym == regOpnd->m_sym && regOpnd->m_reg == RegNOREG;
  1034. }
  1035. ///----------------------------------------------------------------------------
  1036. ///
  1037. /// ArrayRegOpnd
  1038. ///
  1039. ///----------------------------------------------------------------------------
  1040. ArrayRegOpnd::ArrayRegOpnd(
  1041. StackSym *const arraySym,
  1042. const ValueType valueType,
  1043. StackSym *const headSegmentSym,
  1044. StackSym *const headSegmentLengthSym,
  1045. StackSym *const lengthSym,
  1046. const bool eliminatedLowerBoundCheck,
  1047. const bool eliminatedUpperBoundCheck)
  1048. : RegOpnd(arraySym, RegNOREG, TyVar),
  1049. headSegmentSym(headSegmentSym),
  1050. headSegmentLengthSym(headSegmentLengthSym),
  1051. lengthSym(lengthSym),
  1052. eliminatedLowerBoundCheck(eliminatedLowerBoundCheck),
  1053. eliminatedUpperBoundCheck(eliminatedUpperBoundCheck)
  1054. {
  1055. Assert(valueType.IsAnyOptimizedArray());
  1056. m_valueType = valueType;
  1057. m_isArrayRegOpnd = true;
  1058. }
  1059. ArrayRegOpnd::ArrayRegOpnd(
  1060. const RegOpnd &other,
  1061. StackSym *const arraySym,
  1062. const ValueType valueType,
  1063. StackSym *const headSegmentSym,
  1064. StackSym *const headSegmentLengthSym,
  1065. StackSym *const lengthSym,
  1066. const bool eliminatedLowerBoundCheck,
  1067. const bool eliminatedUpperBoundCheck)
  1068. : RegOpnd(other, arraySym),
  1069. headSegmentSym(headSegmentSym),
  1070. headSegmentLengthSym(headSegmentLengthSym),
  1071. lengthSym(lengthSym),
  1072. eliminatedLowerBoundCheck(eliminatedLowerBoundCheck),
  1073. eliminatedUpperBoundCheck(eliminatedUpperBoundCheck)
  1074. {
  1075. Assert(valueType.IsAnyOptimizedArray());
  1076. m_valueType = valueType;
  1077. m_isArrayRegOpnd = true;
  1078. }
  1079. ArrayRegOpnd *ArrayRegOpnd::New(
  1080. StackSym *const arraySym,
  1081. const ValueType valueType,
  1082. StackSym *const headSegmentSym,
  1083. StackSym *const headSegmentLengthSym,
  1084. StackSym *const lengthSym,
  1085. const bool eliminatedLowerBoundCheck,
  1086. const bool eliminatedUpperBoundCheck,
  1087. Func *const func)
  1088. {
  1089. Assert(func);
  1090. return
  1091. JitAnew(
  1092. func->m_alloc,
  1093. ArrayRegOpnd,
  1094. arraySym,
  1095. valueType,
  1096. headSegmentSym,
  1097. headSegmentLengthSym,
  1098. lengthSym,
  1099. eliminatedLowerBoundCheck,
  1100. eliminatedUpperBoundCheck);
  1101. }
  1102. ArrayRegOpnd *ArrayRegOpnd::New(
  1103. const RegOpnd *const other,
  1104. const ValueType valueType,
  1105. StackSym *const headSegmentSym,
  1106. StackSym *const headSegmentLengthSym,
  1107. StackSym *const lengthSym,
  1108. const bool eliminatedLowerBoundCheck,
  1109. const bool eliminatedUpperBoundCheck,
  1110. Func *const func)
  1111. {
  1112. Assert(func);
  1113. return
  1114. JitAnew(
  1115. func->m_alloc,
  1116. ArrayRegOpnd,
  1117. *other,
  1118. other->m_sym,
  1119. valueType,
  1120. headSegmentSym,
  1121. headSegmentLengthSym,
  1122. lengthSym,
  1123. eliminatedLowerBoundCheck,
  1124. eliminatedUpperBoundCheck);
  1125. }
  1126. RegOpnd *ArrayRegOpnd::CopyAsRegOpnd(Func *func)
  1127. {
  1128. RegOpnd *const regOpndCopy = RegOpnd::CopyInternal(func);
  1129. Assert(!regOpndCopy->IsArrayRegOpnd());
  1130. return regOpndCopy;
  1131. }
  1132. ArrayRegOpnd *ArrayRegOpnd::CopyInternalSub(Func *func)
  1133. {
  1134. Assert(m_kind == OpndKindReg && this->IsArrayRegOpnd());
  1135. return Clone(m_sym, headSegmentSym, headSegmentLengthSym, lengthSym, func);
  1136. }
  1137. ArrayRegOpnd *ArrayRegOpnd::CloneDefInternalSub(Func *func)
  1138. {
  1139. Assert(m_kind == OpndKindReg && this->IsArrayRegOpnd());
  1140. return
  1141. Clone(
  1142. m_sym ? m_sym->CloneDef(func) : nullptr,
  1143. headSegmentSym ? headSegmentSym->CloneUse(func) : nullptr,
  1144. headSegmentLengthSym ? headSegmentLengthSym->CloneUse(func) : nullptr,
  1145. lengthSym ? lengthSym->CloneUse(func) : nullptr,
  1146. func);
  1147. }
  1148. ArrayRegOpnd *ArrayRegOpnd::CloneUseInternalSub(Func *func)
  1149. {
  1150. Assert(m_kind == OpndKindReg && this->IsArrayRegOpnd());
  1151. return
  1152. Clone(
  1153. m_sym ? m_sym->CloneUse(func) : nullptr,
  1154. headSegmentSym ? headSegmentSym->CloneUse(func) : nullptr,
  1155. headSegmentLengthSym ? headSegmentLengthSym->CloneUse(func) : nullptr,
  1156. lengthSym ? lengthSym->CloneUse(func) : nullptr,
  1157. func);
  1158. }
  1159. ArrayRegOpnd *ArrayRegOpnd::Clone(
  1160. StackSym *const arraySym,
  1161. StackSym *const headSegmentSym,
  1162. StackSym *const headSegmentLengthSym,
  1163. StackSym *const lengthSym,
  1164. Func *const func) const
  1165. {
  1166. Assert(func);
  1167. // Careful how clones are used. Only GlobOpt knows when it's valid to use the information in this class, so ideally cloning
  1168. // should be done only at lowering time.
  1169. return
  1170. JitAnew(
  1171. func->m_alloc,
  1172. ArrayRegOpnd,
  1173. *this,
  1174. arraySym,
  1175. m_valueType,
  1176. headSegmentSym,
  1177. headSegmentLengthSym,
  1178. lengthSym,
  1179. eliminatedLowerBoundCheck,
  1180. eliminatedUpperBoundCheck);
  1181. }
  1182. void ArrayRegOpnd::FreeInternalSub(Func *func)
  1183. {
  1184. Assert(m_kind == OpndKindReg && this->IsArrayRegOpnd());
  1185. JitAdelete(func->m_alloc, this);
  1186. }
  1187. ///----------------------------------------------------------------------------
  1188. ///
  1189. /// IntConstOpnd::New
  1190. ///
  1191. /// Creates a new IntConstOpnd.
  1192. ///
  1193. ///----------------------------------------------------------------------------
  1194. IntConstOpnd *
  1195. IntConstOpnd::New(IntConstType value, IRType type, Func *func, bool dontEncode)
  1196. {
  1197. IntConstOpnd * intConstOpnd;
  1198. Assert(TySize[type] <= sizeof(IntConstType));
  1199. intConstOpnd = JitAnew(func->m_alloc, IR::IntConstOpnd);
  1200. intConstOpnd->m_type = type;
  1201. intConstOpnd->m_kind = OpndKindIntConst;
  1202. intConstOpnd->m_dontEncode = dontEncode;
  1203. intConstOpnd->SetValue(value);
  1204. return intConstOpnd;
  1205. }
  1206. ///----------------------------------------------------------------------------
  1207. ///
  1208. /// IntConstOpnd::CreateIntConstOpndFromType
  1209. ///
  1210. /// Create an IntConstOpnd or Int64ConstOpnd depending on the IRType.
  1211. ///
  1212. ///----------------------------------------------------------------------------
  1213. IR::Opnd* IntConstOpnd::NewFromType(int64 value, IRType type, Func* func)
  1214. {
  1215. if (IRType_IsInt64(type))
  1216. {
  1217. return Int64ConstOpnd::New(value, type, func);
  1218. }
  1219. Assert(value < (int64)UINT_MAX);
  1220. return IntConstOpnd::New((IntConstType)value, type, func);
  1221. }
  1222. ///----------------------------------------------------------------------------
  1223. ///
  1224. /// IntConstOpnd::Copy
  1225. ///
  1226. /// Returns a copy of this opnd.
  1227. ///
  1228. ///----------------------------------------------------------------------------
  1229. IntConstOpnd *
  1230. IntConstOpnd::CopyInternal(Func *func)
  1231. {
  1232. Assert(m_kind == OpndKindIntConst);
  1233. IntConstOpnd * newOpnd;
  1234. newOpnd = IntConstOpnd::New(m_value, m_type, func, m_dontEncode);
  1235. newOpnd->m_valueType = m_valueType;
  1236. return newOpnd;
  1237. }
  1238. ///----------------------------------------------------------------------------
  1239. ///
  1240. /// IntConstOpnd::IsEqual
  1241. ///
  1242. ///----------------------------------------------------------------------------
  1243. bool
  1244. IntConstOpnd::IsEqualInternal(Opnd *opnd)
  1245. {
  1246. Assert(m_kind == OpndKindIntConst);
  1247. if (!opnd->IsIntConstOpnd() || this->GetType() != opnd->GetType())
  1248. {
  1249. return false;
  1250. }
  1251. return m_value == opnd->AsIntConstOpnd()->m_value;
  1252. }
  1253. void
  1254. IntConstOpnd::FreeInternal(Func *func)
  1255. {
  1256. Assert(m_kind == OpndKindIntConst);
  1257. JitAdelete(func->m_alloc, this);
  1258. }
  1259. ///----------------------------------------------------------------------------
  1260. ///
  1261. /// IntConstOpnd::SetValue
  1262. ///
  1263. /// Modifies the value of the IntConstOpnd
  1264. ///
  1265. ///----------------------------------------------------------------------------
  1266. void
  1267. IntConstOpnd::SetValue(IntConstType value)
  1268. {
  1269. if (sizeof(IntConstType) > sizeof(int32))
  1270. {
  1271. Assert(m_type != TyInt32 || (value >= INT32_MIN && value <= INT32_MAX));
  1272. Assert(m_type != TyUint32 || (value >= 0 && value <= UINT32_MAX));
  1273. }
  1274. // TODO: These should be uncommented, unfortunately, Lowerer::UseWithNewType
  1275. // can change m_type (by calling SetType) in such a way that it violates these constraints.
  1276. // If CopyInternal is later called on the IntConstOpnd, these will fail.
  1277. // Assert(m_type != TyInt16 || (value >= INT16_MIN && value <= INT16_MAX));
  1278. // Assert(m_type != TyUint16 || (value >= 0 && value <= UINT16_MAX));
  1279. // Assert(m_type != TyInt8 || (value >= INT8_MIN && value <= INT8_MAX));
  1280. // Assert(m_type != TyUint8 || (value >= 0 && value <= UINT8_MAX));
  1281. m_value = value;
  1282. }
  1283. ///----------------------------------------------------------------------------
  1284. ///
  1285. /// IntConstOpnd::AsInt32
  1286. ///
  1287. /// Retrieves the value of the int const opnd as a signed 32-bit integer.
  1288. ///
  1289. ///----------------------------------------------------------------------------
  1290. int32
  1291. IntConstOpnd::AsInt32()
  1292. {
  1293. // TODO: Currently, there are cases where we construct IntConstOpnd with TyInt32
  1294. // and retrieve value out as uint32 (or vice versa). Because of these, we allow
  1295. // AsInt32/AsUint32 to cast between int32/uint32 in a lossy manner for now.
  1296. // In the future, we should tighten up usage of IntConstOpnd to avoid these casts
  1297. if (sizeof(IntConstType) == sizeof(int32))
  1298. {
  1299. return (int32)m_value;
  1300. }
  1301. if (m_type == TyUint32)
  1302. {
  1303. Assert(m_value >= 0 && m_value <= UINT32_MAX);
  1304. return (int32)(uint32)m_value;
  1305. }
  1306. Assert(Math::FitsInDWord(m_value));
  1307. return (int32)m_value;
  1308. }
  1309. ///----------------------------------------------------------------------------
  1310. ///
  1311. /// IntConstOpnd::AsUint32
  1312. ///
  1313. /// Retrieves the value of the int const opnd as an unsigned 32-bit integer.
  1314. ///
  1315. ///----------------------------------------------------------------------------
  1316. uint32
  1317. IntConstOpnd::AsUint32()
  1318. {
  1319. // TODO: See comment in AsInt32() regarding casts from int32 to uint32
  1320. if (sizeof(uint32) == sizeof(IntConstType))
  1321. {
  1322. return (uint32)m_value;
  1323. }
  1324. Assert(sizeof(uint32) < sizeof(IntConstType));
  1325. Assert(m_value >= 0 && m_value <= UINT32_MAX);
  1326. return (uint32)m_value;
  1327. }
  1328. ///----------------------------------------------------------------------------
  1329. ///
  1330. /// Int64ConstOpnd Methods
  1331. ///
  1332. ///----------------------------------------------------------------------------
  1333. IR::Int64ConstOpnd* Int64ConstOpnd::New(int64 value, IRType type, Func *func)
  1334. {
  1335. AssertMsg(func->GetJITFunctionBody()->IsWasmFunction(), "Only WebAssembly functions should have int64 const operands. Use IntConstOpnd for size_t type");
  1336. Int64ConstOpnd * intConstOpnd;
  1337. Assert(TySize[type] == sizeof(int64));
  1338. intConstOpnd = JitAnew(func->m_alloc, IR::Int64ConstOpnd);
  1339. intConstOpnd->m_type = type;
  1340. intConstOpnd->m_kind = OpndKindInt64Const;
  1341. intConstOpnd->m_value = value;
  1342. return intConstOpnd;
  1343. }
  1344. IR::Int64ConstOpnd* Int64ConstOpnd::CopyInternal(Func *func)
  1345. {
  1346. Assert(m_kind == OpndKindInt64Const);
  1347. Int64ConstOpnd * newOpnd;
  1348. newOpnd = Int64ConstOpnd::New(m_value, m_type, func);
  1349. newOpnd->m_valueType = m_valueType;
  1350. return newOpnd;
  1351. }
  1352. bool Int64ConstOpnd::IsEqualInternal(Opnd *opnd)
  1353. {
  1354. Assert(m_kind == OpndKindInt64Const);
  1355. if (!opnd->IsInt64ConstOpnd() || this->GetType() != opnd->GetType())
  1356. {
  1357. return false;
  1358. }
  1359. return m_value == opnd->AsInt64ConstOpnd()->m_value;
  1360. }
  1361. void Int64ConstOpnd::FreeInternal(Func * func)
  1362. {
  1363. Assert(m_kind == OpndKindInt64Const);
  1364. JitAdelete(func->m_alloc, this);
  1365. }
  1366. ///----------------------------------------------------------------------------
  1367. ///
  1368. /// RegBVOpnd::New
  1369. ///
  1370. /// Creates a new IntConstOpnd.
  1371. ///
  1372. ///----------------------------------------------------------------------------
  1373. RegBVOpnd *
  1374. RegBVOpnd::New(BVUnit value, IRType type, Func *func)
  1375. {
  1376. RegBVOpnd * regBVOpnd;
  1377. regBVOpnd = JitAnew(func->m_alloc, IR::RegBVOpnd);
  1378. regBVOpnd->m_value.Copy(value);
  1379. regBVOpnd->m_type = type;
  1380. regBVOpnd->m_kind = OpndKindRegBV;
  1381. return regBVOpnd;
  1382. }
  1383. ///----------------------------------------------------------------------------
  1384. ///
  1385. /// RegBVOpnd::Copy
  1386. ///
  1387. /// Returns a copy of this opnd.
  1388. ///
  1389. ///----------------------------------------------------------------------------
  1390. RegBVOpnd *
  1391. RegBVOpnd::CopyInternal(Func *func)
  1392. {
  1393. Assert(m_kind == OpndKindRegBV);
  1394. RegBVOpnd * newOpnd;
  1395. newOpnd = RegBVOpnd::New(m_value, m_type, func);
  1396. newOpnd->m_valueType = m_valueType;
  1397. return newOpnd;
  1398. }
  1399. ///----------------------------------------------------------------------------
  1400. ///
  1401. /// RegBVOpnd::IsEqual
  1402. ///
  1403. ///----------------------------------------------------------------------------
  1404. bool
  1405. RegBVOpnd::IsEqualInternal(Opnd *opnd)
  1406. {
  1407. Assert(m_kind == OpndKindRegBV);
  1408. if (!opnd->IsRegBVOpnd() || this->GetType() != opnd->GetType())
  1409. {
  1410. return false;
  1411. }
  1412. return m_value.Equal(opnd->AsRegBVOpnd()->m_value);
  1413. }
  1414. void
  1415. RegBVOpnd::FreeInternal(Func *func)
  1416. {
  1417. Assert(m_kind == OpndKindRegBV);
  1418. JitAdelete(func->m_alloc, this);
  1419. }
  1420. ///----------------------------------------------------------------------------
  1421. ///
  1422. /// FloatConstOpnd::New
  1423. ///
  1424. /// Creates a new FloatConstOpnd.
  1425. ///
  1426. ///----------------------------------------------------------------------------
  1427. FloatConstOpnd *
  1428. FloatConstOpnd::New(FloatConstType value, IRType type, Func *func)
  1429. {
  1430. FloatConstOpnd * floatConstOpnd;
  1431. floatConstOpnd = JitAnew(func->m_alloc, IR::FloatConstOpnd);
  1432. floatConstOpnd->m_value = value;
  1433. floatConstOpnd->m_type = type;
  1434. #if !FLOATVAR
  1435. floatConstOpnd->m_number = nullptr;
  1436. #endif
  1437. floatConstOpnd->m_kind = OpndKindFloatConst;
  1438. return floatConstOpnd;
  1439. }
  1440. FloatConstOpnd *
  1441. FloatConstOpnd::New(Js::Var floatVar, IRType type, Func *func, Js::Var varLocal /*= nullptr*/)
  1442. {
  1443. Assert((varLocal && Js::JavascriptNumber::Is(varLocal)) || Js::JavascriptNumber::Is(floatVar));
  1444. FloatConstType value = Js::JavascriptNumber::GetValue(varLocal ? varLocal : floatVar);
  1445. FloatConstOpnd * floatConstOpnd = FloatConstOpnd::New(value, type, func);
  1446. #if !FLOATVAR
  1447. floatConstOpnd->m_number = floatVar;
  1448. floatConstOpnd->m_numberCopy = (Js::JavascriptNumber*)varLocal;
  1449. #endif
  1450. return floatConstOpnd;
  1451. }
  1452. AddrOpnd *
  1453. FloatConstOpnd::GetAddrOpnd(Func *func, bool dontEncode)
  1454. {
  1455. #if !FLOATVAR
  1456. if (this->m_number)
  1457. {
  1458. return AddrOpnd::New(this->m_number, (Js::TaggedNumber::Is(this->m_number) ? AddrOpndKindConstantVar : AddrOpndKindDynamicVar), func, dontEncode, this->m_numberCopy);
  1459. }
  1460. #endif
  1461. IR::AddrOpnd *opnd = AddrOpnd::NewFromNumber(this->m_value, func, dontEncode);
  1462. #if !FLOATVAR
  1463. this->m_number = opnd->m_address;
  1464. #endif
  1465. return opnd;
  1466. }
  1467. ///----------------------------------------------------------------------------
  1468. ///
  1469. /// FloatConstOpnd::Copy
  1470. ///
  1471. /// Returns a copy of this opnd.
  1472. ///
  1473. ///----------------------------------------------------------------------------
  1474. FloatConstOpnd *
  1475. FloatConstOpnd::CopyInternal(Func *func)
  1476. {
  1477. Assert(m_kind == OpndKindFloatConst);
  1478. FloatConstOpnd * newOpnd;
  1479. newOpnd = FloatConstOpnd::New(m_value, m_type, func);
  1480. newOpnd->m_valueType = m_valueType;
  1481. return newOpnd;
  1482. }
  1483. ///----------------------------------------------------------------------------
  1484. ///
  1485. /// FloatConstOpnd::IsEqual
  1486. ///
  1487. ///----------------------------------------------------------------------------
  1488. bool
  1489. FloatConstOpnd::IsEqualInternal(Opnd *opnd)
  1490. {
  1491. Assert(m_kind == OpndKindFloatConst);
  1492. if (!opnd->IsFloatConstOpnd() || this->GetType() != opnd->GetType())
  1493. {
  1494. return false;
  1495. }
  1496. return m_value == opnd->AsFloatConstOpnd()->m_value;
  1497. }
  1498. void
  1499. FloatConstOpnd::FreeInternal(Func *func)
  1500. {
  1501. Assert(m_kind == OpndKindFloatConst);
  1502. JitAdelete(func->m_alloc, this);
  1503. }
  1504. ///----------------------------------------------------------------------------
  1505. ///
  1506. /// Float32ConstOpnd::New
  1507. ///
  1508. /// Creates a new Float32ConstOpnd.
  1509. ///
  1510. ///----------------------------------------------------------------------------
  1511. Float32ConstOpnd *
  1512. Float32ConstOpnd::New(float value, IRType type, Func *func)
  1513. {
  1514. Assert(type == IRType::TyFloat32); //TODO: should we even allow specifying a type here? It should always be TyFloat32
  1515. Float32ConstOpnd * Float32ConstOpnd;
  1516. Float32ConstOpnd = JitAnew(func->m_alloc, IR::Float32ConstOpnd);
  1517. Float32ConstOpnd->m_value = value;
  1518. Float32ConstOpnd->m_type = type;
  1519. Float32ConstOpnd->m_kind = OpndKindFloat32Const;
  1520. return Float32ConstOpnd;
  1521. }
  1522. ///----------------------------------------------------------------------------
  1523. ///
  1524. /// Float32ConstOpnd::Copy
  1525. ///
  1526. /// Returns a copy of this opnd.
  1527. ///
  1528. ///----------------------------------------------------------------------------
  1529. Float32ConstOpnd *
  1530. Float32ConstOpnd::CopyInternal(Func *func)
  1531. {
  1532. Assert(m_kind == OpndKindFloat32Const);
  1533. Float32ConstOpnd * newOpnd;
  1534. newOpnd = Float32ConstOpnd::New(m_value, m_type, func);
  1535. newOpnd->m_valueType = m_valueType;
  1536. return newOpnd;
  1537. }
  1538. ///----------------------------------------------------------------------------
  1539. ///
  1540. /// Float32ConstOpnd::IsEqual
  1541. ///
  1542. ///----------------------------------------------------------------------------
  1543. bool
  1544. Float32ConstOpnd::IsEqualInternal(Opnd *opnd)
  1545. {
  1546. Assert(m_kind == OpndKindFloat32Const);
  1547. if (!opnd->IsFloat32ConstOpnd() || this->GetType() != opnd->GetType() /* TODO: could this be turned into an assert*/)
  1548. {
  1549. return false;
  1550. }
  1551. return m_value == opnd->AsFloat32ConstOpnd()->m_value;
  1552. }
  1553. void
  1554. Float32ConstOpnd::FreeInternal(Func *func)
  1555. {
  1556. Assert(m_kind == OpndKindFloat32Const);
  1557. JitAdelete(func->m_alloc, this);
  1558. }
  1559. ///----------------------------------------------------------------------------
  1560. ///
  1561. /// Simd128ConstOpnd::New
  1562. ///
  1563. /// Creates a new FloatConstOpnd.
  1564. ///
  1565. ///----------------------------------------------------------------------------
  1566. Simd128ConstOpnd *
  1567. Simd128ConstOpnd::New(AsmJsSIMDValue value, IRType type, Func *func)
  1568. {
  1569. Simd128ConstOpnd * simd128ConstOpnd;
  1570. simd128ConstOpnd = JitAnew(func->m_alloc, IR::Simd128ConstOpnd);
  1571. simd128ConstOpnd->m_value = value;
  1572. simd128ConstOpnd->m_type = type;
  1573. simd128ConstOpnd->m_kind = OpndKindSimd128Const;
  1574. return simd128ConstOpnd;
  1575. }
  1576. ///----------------------------------------------------------------------------
  1577. ///
  1578. /// Simd128ConstOpnd::Copy
  1579. ///
  1580. /// Returns a copy of this opnd.
  1581. ///
  1582. ///----------------------------------------------------------------------------
  1583. Simd128ConstOpnd *
  1584. Simd128ConstOpnd::CopyInternal(Func *func)
  1585. {
  1586. Assert(m_kind == OpndKindSimd128Const);
  1587. Simd128ConstOpnd * newOpnd;
  1588. newOpnd = Simd128ConstOpnd::New(m_value, m_type, func);
  1589. newOpnd->m_valueType = m_valueType;
  1590. return newOpnd;
  1591. }
  1592. ///----------------------------------------------------------------------------
  1593. ///
  1594. /// Simd128ConstOpnd::IsEqual
  1595. ///
  1596. ///----------------------------------------------------------------------------
  1597. bool
  1598. Simd128ConstOpnd::IsEqualInternal(Opnd *opnd)
  1599. {
  1600. Assert(m_kind == OpndKindSimd128Const);
  1601. if (!opnd->IsSimd128ConstOpnd() || this->GetType() != opnd->GetType())
  1602. {
  1603. return false;
  1604. }
  1605. return m_value == opnd->AsSimd128ConstOpnd()->m_value;
  1606. }
  1607. void
  1608. Simd128ConstOpnd::FreeInternal(Func *func)
  1609. {
  1610. Assert(m_kind == OpndKindSimd128Const);
  1611. JitAdelete(func->m_alloc, this);
  1612. }
  1613. ///----------------------------------------------------------------------------
  1614. ///
  1615. /// HelperCallOpnd::New
  1616. ///
  1617. /// Creates a new HelperCallOpnd.
  1618. ///
  1619. ///----------------------------------------------------------------------------
  1620. HelperCallOpnd *
  1621. HelperCallOpnd::New(JnHelperMethod fnHelper, Func *func)
  1622. {
  1623. HelperCallOpnd *helperCallOpnd = JitAnew(func->m_alloc, IR::HelperCallOpnd);
  1624. helperCallOpnd->Init(fnHelper);
  1625. return helperCallOpnd;
  1626. }
  1627. void
  1628. HelperCallOpnd::Init(JnHelperMethod fnHelper)
  1629. {
  1630. Assert(fnHelper != IR::HelperInvalid);
  1631. this->m_fnHelper = fnHelper;
  1632. this->m_type = TyMachPtr;
  1633. this->m_kind = OpndKindHelperCall;
  1634. }
  1635. ///----------------------------------------------------------------------------
  1636. ///
  1637. /// HelperCallOpnd::Copy
  1638. ///
  1639. /// Returns a copy of this opnd.
  1640. ///
  1641. ///----------------------------------------------------------------------------
  1642. HelperCallOpnd *
  1643. HelperCallOpnd::CopyInternal(Func *func)
  1644. {
  1645. Assert(m_kind == OpndKindHelperCall);
  1646. HelperCallOpnd *const newOpnd = HelperCallOpnd::New(m_fnHelper, func);
  1647. newOpnd->m_valueType = m_valueType;
  1648. return newOpnd;
  1649. }
  1650. ///----------------------------------------------------------------------------
  1651. ///
  1652. /// HelperCallOpnd::IsEqual
  1653. ///
  1654. ///----------------------------------------------------------------------------
  1655. bool
  1656. HelperCallOpnd::IsEqualInternal(Opnd *opnd)
  1657. {
  1658. Assert(m_kind == OpndKindHelperCall);
  1659. if (!opnd->IsHelperCallOpnd())
  1660. {
  1661. return false;
  1662. }
  1663. return m_fnHelper == opnd->AsHelperCallOpnd()->m_fnHelper;
  1664. }
  1665. void
  1666. HelperCallOpnd::FreeInternal(Func *func)
  1667. {
  1668. Assert(m_kind == OpndKindHelperCall);
  1669. JitAdelete(func->m_alloc, this);
  1670. }
  1671. DiagHelperCallOpnd *
  1672. DiagHelperCallOpnd::New(JnHelperMethod fnHelper, Func *func, int argCount)
  1673. {
  1674. DiagHelperCallOpnd *helperCallOpnd = JitAnew(func->m_alloc, IR::DiagHelperCallOpnd);
  1675. helperCallOpnd->Init(fnHelper);
  1676. helperCallOpnd->m_argCount = argCount;
  1677. helperCallOpnd->isDiagHelperCallOpnd = true;
  1678. return helperCallOpnd;
  1679. }
  1680. DiagHelperCallOpnd *
  1681. DiagHelperCallOpnd::CopyInternalSub(Func *func)
  1682. {
  1683. Assert(m_kind == OpndKindHelperCall && this->IsDiagHelperCallOpnd());
  1684. DiagHelperCallOpnd *const newOpnd = DiagHelperCallOpnd::New(m_fnHelper, func, m_argCount);
  1685. newOpnd->m_valueType = m_valueType;
  1686. return newOpnd;
  1687. }
  1688. bool
  1689. DiagHelperCallOpnd::IsEqualInternalSub(Opnd *opnd)
  1690. {
  1691. Assert(m_kind == OpndKindHelperCall && this->IsDiagHelperCallOpnd());
  1692. if (!opnd->IsHelperCallOpnd() || !opnd->AsHelperCallOpnd()->IsDiagHelperCallOpnd())
  1693. {
  1694. return false;
  1695. }
  1696. return
  1697. m_fnHelper == opnd->AsHelperCallOpnd()->m_fnHelper &&
  1698. m_argCount == static_cast<DiagHelperCallOpnd*>(opnd)->m_argCount;
  1699. }
  1700. ///----------------------------------------------------------------------------
  1701. ///
  1702. /// AddrOpnd::New
  1703. ///
  1704. /// Creates a new AddrOpnd.
  1705. ///
  1706. ///----------------------------------------------------------------------------
  1707. AddrOpnd *
  1708. AddrOpnd::New(intptr_t address, AddrOpndKind addrOpndKind, Func *func, bool dontEncode /* = false */, Js::Var varLocal /* = nullptr*/)
  1709. {
  1710. AddrOpnd * addrOpnd;
  1711. addrOpnd = JitAnew(func->m_alloc, IR::AddrOpnd);
  1712. // TODO (michhol): OOP JIT, use intptr_t instead of Js::Var by default so people don't try to dereference
  1713. addrOpnd->m_address = (Js::Var)address;
  1714. addrOpnd->m_localAddress = func->IsOOPJIT() ? varLocal : (Js::Var)address;
  1715. addrOpnd->addrOpndKind = addrOpndKind;
  1716. addrOpnd->m_type = addrOpnd->IsVar() ? TyVar : TyMachPtr;
  1717. addrOpnd->m_dontEncode = dontEncode;
  1718. addrOpnd->m_isFunction = false;
  1719. if (address && addrOpnd->IsVar())
  1720. {
  1721. if (Js::TaggedInt::Is(address))
  1722. {
  1723. addrOpnd->m_valueType = ValueType::GetTaggedInt();
  1724. addrOpnd->SetValueTypeFixed();
  1725. }
  1726. else if (
  1727. #if !FLOATVAR
  1728. !func->IsOOPJIT() && CONFIG_FLAG(OOPJITMissingOpts) &&
  1729. #endif
  1730. Js::JavascriptNumber::Is_NoTaggedIntCheck(addrOpnd->m_address))
  1731. {
  1732. addrOpnd->m_valueType =
  1733. Js::JavascriptNumber::IsInt32_NoChecks(addrOpnd->m_address)
  1734. ? ValueType::GetInt(false)
  1735. : ValueType::Float;
  1736. addrOpnd->SetValueTypeFixed();
  1737. }
  1738. }
  1739. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  1740. addrOpnd->decodedValue = 0;
  1741. addrOpnd->wasVar = addrOpnd->IsVar();
  1742. #endif
  1743. addrOpnd->m_kind = OpndKindAddr;
  1744. return addrOpnd;
  1745. }
  1746. AddrOpnd *
  1747. AddrOpnd::New(Js::Var address, AddrOpndKind addrOpndKind, Func *func, bool dontEncode /* = false */, Js::Var varLocal /* = nullptr*/)
  1748. {
  1749. AddrOpnd * addrOpnd;
  1750. addrOpnd = JitAnew(func->m_alloc, IR::AddrOpnd);
  1751. addrOpnd->m_address = address;
  1752. addrOpnd->m_localAddress = func->IsOOPJIT() ? varLocal : address;
  1753. addrOpnd->addrOpndKind = addrOpndKind;
  1754. addrOpnd->m_type = addrOpnd->IsVar()? TyVar : TyMachPtr;
  1755. addrOpnd->m_dontEncode = dontEncode;
  1756. addrOpnd->m_isFunction = false;
  1757. addrOpnd->m_metadata = nullptr;
  1758. if(address && addrOpnd->IsVar())
  1759. {
  1760. if(Js::TaggedInt::Is(address))
  1761. {
  1762. addrOpnd->m_valueType = ValueType::GetTaggedInt();
  1763. addrOpnd->SetValueTypeFixed();
  1764. }
  1765. else
  1766. {
  1767. Js::Var var = varLocal ? varLocal : address;
  1768. if (
  1769. #if !FLOATVAR
  1770. varLocal || (!func->IsOOPJIT() && CONFIG_FLAG(OOPJITMissingOpts)) &&
  1771. #endif
  1772. Js::JavascriptNumber::Is_NoTaggedIntCheck(var))
  1773. {
  1774. addrOpnd->m_valueType =
  1775. Js::JavascriptNumber::IsInt32_NoChecks(var)
  1776. ? ValueType::GetInt(false)
  1777. : ValueType::Float;
  1778. addrOpnd->SetValueTypeFixed();
  1779. }
  1780. }
  1781. }
  1782. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  1783. addrOpnd->decodedValue = 0;
  1784. addrOpnd->wasVar = addrOpnd->IsVar();
  1785. #endif
  1786. addrOpnd->m_kind = OpndKindAddr;
  1787. return addrOpnd;
  1788. }
  1789. AddrOpnd *
  1790. AddrOpnd::NewFromNumber(int32 value, Func *func, bool dontEncode /* = false */)
  1791. {
  1792. if (!Js::TaggedInt::IsOverflow(value))
  1793. {
  1794. return New(Js::TaggedInt::ToVarUnchecked(value), AddrOpndKindConstantVar, func, dontEncode);
  1795. }
  1796. else
  1797. {
  1798. return NewFromNumberVar(value, func, dontEncode);
  1799. }
  1800. }
  1801. AddrOpnd *
  1802. AddrOpnd::NewFromNumber(int64 value, Func *func, bool dontEncode /* = false */)
  1803. {
  1804. if (!Js::TaggedInt::IsOverflow(value))
  1805. {
  1806. return New(Js::TaggedInt::ToVarUnchecked((int)value), AddrOpndKindConstantVar, func, dontEncode);
  1807. }
  1808. else
  1809. {
  1810. return NewFromNumberVar((double)value, func, dontEncode);
  1811. }
  1812. }
  1813. AddrOpnd *
  1814. AddrOpnd::NewFromNumber(double value, Func *func, bool dontEncode /* = false */)
  1815. {
  1816. //
  1817. // Check if a well-known value:
  1818. // - This significantly cuts down on the below floating-point to integer conversions.
  1819. //
  1820. if (Js::JavascriptNumber::IsNegZero(value))
  1821. {
  1822. return New(func->GetScriptContextInfo()->GetNegativeZeroAddr(), AddrOpndKindDynamicVar, func, dontEncode);
  1823. }
  1824. if (value == +0.0)
  1825. {
  1826. return New(Js::TaggedInt::ToVarUnchecked(0), AddrOpndKindConstantVar, func, dontEncode);
  1827. }
  1828. if (value == 1.0)
  1829. {
  1830. return New(Js::TaggedInt::ToVarUnchecked(1), AddrOpndKindConstantVar, func, dontEncode);
  1831. }
  1832. //
  1833. // Check if number can be reduced back into a TaggedInt:
  1834. // - This avoids extra GC.
  1835. //
  1836. int nValue = (int) value;
  1837. double dblCheck = (double) nValue;
  1838. if ((dblCheck == value) && (!Js::TaggedInt::IsOverflow(nValue)))
  1839. {
  1840. return New(Js::TaggedInt::ToVarUnchecked(nValue), AddrOpndKindConstantVar, func, dontEncode);
  1841. }
  1842. return NewFromNumberVar(value, func, dontEncode);
  1843. }
  1844. AddrOpnd *
  1845. AddrOpnd::NewFromNumberVar(double value, Func *func, bool dontEncode /* = false */)
  1846. {
  1847. Js::Var var = func->AllocateNumber((double)value);
  1848. AddrOpnd* addrOpnd = New((intptr_t)var, AddrOpndKindDynamicVar, func, dontEncode);
  1849. addrOpnd->m_valueType =
  1850. Js::JavascriptNumber::IsInt32(value)
  1851. ? ValueType::GetInt(false)
  1852. : ValueType::Float;
  1853. addrOpnd->SetValueTypeFixed();
  1854. return addrOpnd;
  1855. }
  1856. AddrOpnd *
  1857. AddrOpnd::NewNull(Func *func)
  1858. {
  1859. return AddrOpnd::New((Js::Var)0, AddrOpndKindConstantAddress, func, true);
  1860. }
  1861. ///----------------------------------------------------------------------------
  1862. ///
  1863. /// AddrOpnd::Copy
  1864. ///
  1865. /// Returns a copy of this opnd.
  1866. ///
  1867. ///----------------------------------------------------------------------------
  1868. AddrOpnd *
  1869. AddrOpnd::CopyInternal(Func *func)
  1870. {
  1871. Assert(m_kind == OpndKindAddr);
  1872. AddrOpnd * newOpnd;
  1873. newOpnd = AddrOpnd::New(nullptr, addrOpndKind, func, m_dontEncode);
  1874. // Constructor evaluates address for type, but this is invalid if the address has been encoded, so we wait to set it
  1875. newOpnd->m_address = m_address;
  1876. newOpnd->m_valueType = m_valueType;
  1877. newOpnd->m_isFunction = m_isFunction;
  1878. newOpnd->m_metadata = m_metadata;
  1879. newOpnd->SetType(m_type);
  1880. if (IsValueTypeFixed())
  1881. {
  1882. newOpnd->SetValueTypeFixed();
  1883. }
  1884. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  1885. newOpnd->decodedValue = this->decodedValue;
  1886. newOpnd->wasVar = this->wasVar;
  1887. #endif
  1888. return newOpnd;
  1889. }
  1890. ///----------------------------------------------------------------------------
  1891. ///
  1892. /// AddrOpnd::IsEqual
  1893. ///
  1894. ///----------------------------------------------------------------------------
  1895. bool
  1896. AddrOpnd::IsEqualInternal(Opnd *opnd)
  1897. {
  1898. Assert(m_kind == OpndKindAddr);
  1899. if (!opnd->IsAddrOpnd())
  1900. {
  1901. return false;
  1902. }
  1903. return m_address == opnd->AsAddrOpnd()->m_address;
  1904. }
  1905. void
  1906. AddrOpnd::FreeInternal(Func *func)
  1907. {
  1908. Assert(m_kind == OpndKindAddr);
  1909. JitAdelete(func->m_alloc, this);
  1910. }
  1911. void
  1912. AddrOpnd::SetEncodedValue(Js::Var address, AddrOpndKind addrOpndKind)
  1913. {
  1914. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  1915. this->decodedValue = this->m_address;
  1916. #endif
  1917. this->SetAddress(address, addrOpndKind);
  1918. }
  1919. void
  1920. AddrOpnd::SetAddress(Js::Var address, AddrOpndKind addrOpndKind)
  1921. {
  1922. this->m_address = address;
  1923. this->addrOpndKind = addrOpndKind;
  1924. }
  1925. ///----------------------------------------------------------------------------
  1926. ///
  1927. /// ListOpnd
  1928. ///
  1929. /// ListOpnd API
  1930. ///
  1931. ///----------------------------------------------------------------------------
  1932. ListOpnd *
  1933. ListOpnd::New(Func *func, __in_ecount(count) ListOpndType** opnds, int count)
  1934. {
  1935. return JitAnew(func->m_alloc, ListOpnd, func, opnds, count);
  1936. }
  1937. ListOpnd::~ListOpnd()
  1938. {
  1939. Func* func = this->m_func;
  1940. for (int i = 0; i < Count(); ++i)
  1941. {
  1942. Item(i)->UnUse();
  1943. Item(i)->Free(func);
  1944. }
  1945. JitAdeleteArray(func->m_alloc, count, opnds);
  1946. }
  1947. ListOpnd::ListOpnd(Func* func, __in_ecount(_count) ListOpndType** _opnds, int _count):
  1948. Opnd(), m_func(func), count(_count)
  1949. {
  1950. AssertOrFailFast(count > 0);
  1951. Assert(func->isPostLower || func->IsInPhase(Js::LowererPhase));
  1952. m_kind = OpndKindList;
  1953. m_type = TyMisc;
  1954. opnds = JitAnewArray(func->m_alloc, ListOpndType*, count);
  1955. for (int i = 0; i < count; ++i)
  1956. {
  1957. opnds[i] = _opnds[i]->Use(func)->AsRegOpnd();
  1958. }
  1959. }
  1960. void ListOpnd::FreeInternal(Func * func)
  1961. {
  1962. Assert(m_kind == OpndKindList);
  1963. JitAdelete(func->m_alloc, this);
  1964. }
  1965. bool ListOpnd::IsEqualInternal(Opnd * opnd)
  1966. {
  1967. Assert(m_kind == OpndKindList);
  1968. if (!opnd->IsListOpnd())
  1969. {
  1970. return false;
  1971. }
  1972. ListOpnd* l2 = opnd->AsListOpnd();
  1973. if (l2->Count() != Count())
  1974. {
  1975. return false;
  1976. }
  1977. for (int i = 0; i < Count(); ++i)
  1978. {
  1979. if (!Item(i)->IsEqual(l2->Item(i)))
  1980. {
  1981. return false;
  1982. }
  1983. }
  1984. return true;
  1985. }
  1986. Opnd * ListOpnd::CloneUseInternal(Func * func)
  1987. {
  1988. Assert(m_kind == OpndKindList);
  1989. int count = Count();
  1990. ListOpndType** opnds = JitAnewArray(func->m_alloc, ListOpndType*, count);
  1991. for (int i = 0; i < count; ++i)
  1992. {
  1993. ListOpndType* newOpnd = Item(i)->CloneUse(func)->AsRegOpnd();
  1994. opnds[i] = newOpnd;
  1995. }
  1996. ListOpnd* newList = ListOpnd::New(func, opnds, count);
  1997. JitAdeleteArray(func->m_alloc, count, opnds);
  1998. return newList;
  1999. }
  2000. Opnd * ListOpnd::CloneDefInternal(Func * func)
  2001. {
  2002. Assert(m_kind == OpndKindList);
  2003. int count = Count();
  2004. ListOpndType** opnds = JitAnewArray(func->m_alloc, RegOpnd*, count);
  2005. for (int i = 0; i < count; ++i)
  2006. {
  2007. ListOpndType* newOpnd = Item(i)->CloneDef(func)->AsRegOpnd();
  2008. opnds[i] = newOpnd;
  2009. }
  2010. ListOpnd* newList = ListOpnd::New(func, opnds, count);
  2011. JitAdeleteArray(func->m_alloc, count, opnds);
  2012. return newList;
  2013. }
  2014. Opnd * ListOpnd::CopyInternal(Func * func)
  2015. {
  2016. Assert(m_kind == OpndKindList);
  2017. return ListOpnd::New(func, opnds, Count());
  2018. }
  2019. ///----------------------------------------------------------------------------
  2020. ///
  2021. /// IndirOpnd::New
  2022. ///
  2023. /// Creates a new IndirOpnd.
  2024. ///
  2025. ///----------------------------------------------------------------------------
  2026. IndirOpnd *
  2027. IndirOpnd::New(RegOpnd *baseOpnd, RegOpnd *indexOpnd, IRType type, Func *func)
  2028. {
  2029. IndirOpnd * indirOpnd;
  2030. AssertMsg(baseOpnd, "An IndirOpnd needs a valid baseOpnd.");
  2031. indirOpnd = JitAnew(func->m_alloc, IndirOpnd);
  2032. indirOpnd->m_func = func;
  2033. indirOpnd->SetBaseOpnd(baseOpnd);
  2034. indirOpnd->SetIndexOpnd(indexOpnd);
  2035. indirOpnd->m_type = type;
  2036. indirOpnd->SetIsJITOptimizedReg(false);
  2037. indirOpnd->m_kind = OpndKindIndir;
  2038. return indirOpnd;
  2039. }
  2040. ///----------------------------------------------------------------------------
  2041. ///
  2042. /// IndirOpnd::New
  2043. ///
  2044. /// Creates a new IndirOpnd.
  2045. ///
  2046. ///----------------------------------------------------------------------------
  2047. IndirOpnd *
  2048. IndirOpnd::New(RegOpnd *baseOpnd, RegOpnd *indexOpnd, byte scale, IRType type, Func *func)
  2049. {
  2050. IndirOpnd * indirOpnd = IndirOpnd::New(baseOpnd, indexOpnd, type, func);
  2051. indirOpnd->m_scale = scale;
  2052. return indirOpnd;
  2053. }
  2054. ///----------------------------------------------------------------------------
  2055. ///
  2056. /// IndirOpnd::New
  2057. ///
  2058. /// Creates a new IndirOpnd.
  2059. ///
  2060. ///----------------------------------------------------------------------------
  2061. IndirOpnd *
  2062. IndirOpnd::New(RegOpnd *indexOpnd, int32 offset, byte scale, IRType type, Func *func)
  2063. {
  2064. IndirOpnd * indirOpnd;
  2065. indirOpnd = JitAnew(func->m_alloc, IndirOpnd);
  2066. indirOpnd->m_func = func;
  2067. indirOpnd->SetBaseOpnd(nullptr);
  2068. indirOpnd->SetOffset(offset, true);
  2069. indirOpnd->SetIndexOpnd(indexOpnd);
  2070. indirOpnd->m_type = type;
  2071. indirOpnd->SetIsJITOptimizedReg(false);
  2072. indirOpnd->m_kind = OpndKindIndir;
  2073. indirOpnd->m_scale = scale;
  2074. return indirOpnd;
  2075. }
  2076. ///----------------------------------------------------------------------------
  2077. ///
  2078. /// IndirOpnd::New
  2079. ///
  2080. /// Creates a new IndirOpnd.
  2081. ///
  2082. ///----------------------------------------------------------------------------
  2083. IndirOpnd *
  2084. IndirOpnd::New(RegOpnd *baseOpnd, int32 offset, IRType type, Func *func, bool dontEncode /* = false */)
  2085. {
  2086. IndirOpnd * indirOpnd;
  2087. indirOpnd = JitAnew(func->m_alloc, IndirOpnd);
  2088. indirOpnd->m_func = func;
  2089. indirOpnd->SetBaseOpnd(baseOpnd);
  2090. indirOpnd->SetOffset(offset, dontEncode);
  2091. indirOpnd->m_type = type;
  2092. indirOpnd->SetIsJITOptimizedReg(false);
  2093. indirOpnd->m_kind = OpndKindIndir;
  2094. return indirOpnd;
  2095. }
  2096. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  2097. ///----------------------------------------------------------------------------
  2098. ///
  2099. /// IndirOpnd::New
  2100. ///
  2101. /// Creates a new IndirOpnd.
  2102. ///
  2103. ///----------------------------------------------------------------------------
  2104. IndirOpnd *
  2105. IndirOpnd::New(RegOpnd *baseOpnd, int32 offset, IRType type, const char16 *desc, Func *func, bool dontEncode /* = false */)
  2106. {
  2107. IndirOpnd * indirOpnd = IndirOpnd::New(baseOpnd, offset, type, func);
  2108. indirOpnd->m_desc = desc;
  2109. indirOpnd->m_dontEncode = dontEncode;
  2110. return indirOpnd;
  2111. }
  2112. #endif
  2113. IndirOpnd::~IndirOpnd()
  2114. {
  2115. if (m_baseOpnd != nullptr)
  2116. {
  2117. m_baseOpnd->UnUse();
  2118. m_baseOpnd->Free(m_func);
  2119. }
  2120. if (m_indexOpnd != nullptr)
  2121. {
  2122. m_indexOpnd->UnUse();
  2123. m_indexOpnd->Free(m_func);
  2124. }
  2125. }
  2126. ///----------------------------------------------------------------------------
  2127. ///
  2128. /// IndirOpnd::Copy
  2129. ///
  2130. /// Returns a copy of this opnd.
  2131. ///
  2132. ///----------------------------------------------------------------------------
  2133. IndirOpnd *
  2134. IndirOpnd::CopyInternal(Func *func)
  2135. {
  2136. Assert(m_kind == OpndKindIndir);
  2137. IndirOpnd * newOpnd;
  2138. newOpnd = IndirOpnd::New(m_baseOpnd, m_indexOpnd, m_scale, m_type, func);
  2139. newOpnd->m_valueType = m_valueType;
  2140. newOpnd->canStoreTemp = this->canStoreTemp;
  2141. newOpnd->SetOffset(m_offset, m_dontEncode);
  2142. newOpnd->SetIsJITOptimizedReg(this->GetIsJITOptimizedReg());
  2143. #if DBG_DUMP
  2144. newOpnd->m_addrKind = m_addrKind;
  2145. newOpnd->m_originalAddress = m_originalAddress;
  2146. #endif
  2147. return newOpnd;
  2148. }
  2149. IndirOpnd *
  2150. IndirOpnd::CloneDefInternal(Func *func)
  2151. {
  2152. Assert(m_kind == OpndKindIndir);
  2153. IndirOpnd * newOpnd;
  2154. // The components of an IndirOpnd are always uses, even if the IndirOpnd itself is a def.
  2155. RegOpnd * newBaseOpnd = m_baseOpnd ? m_baseOpnd->CloneUse(func)->AsRegOpnd() : nullptr;
  2156. RegOpnd * newIndexOpnd = m_indexOpnd ? m_indexOpnd->CloneUse(func)->AsRegOpnd() : nullptr;
  2157. newOpnd = IndirOpnd::New(newBaseOpnd, newIndexOpnd, m_scale, m_type, func);
  2158. newOpnd->SetOffset(m_offset, m_dontEncode);
  2159. #if DBG_DUMP
  2160. newOpnd->m_addrKind = m_addrKind;
  2161. newOpnd->m_originalAddress = m_originalAddress;
  2162. #endif
  2163. return newOpnd;
  2164. }
  2165. IndirOpnd *
  2166. IndirOpnd::CloneUseInternal(Func *func)
  2167. {
  2168. Assert(m_kind == OpndKindIndir);
  2169. IndirOpnd * newOpnd;
  2170. RegOpnd * newBaseOpnd = m_baseOpnd ? m_baseOpnd->CloneUse(func)->AsRegOpnd() : nullptr;
  2171. RegOpnd * newIndexOpnd = m_indexOpnd ? m_indexOpnd->CloneUse(func)->AsRegOpnd() : nullptr;
  2172. newOpnd = IndirOpnd::New(newBaseOpnd, newIndexOpnd, m_scale, m_type, func);
  2173. newOpnd->SetOffset(m_offset, m_dontEncode);
  2174. #if DBG_DUMP
  2175. newOpnd->m_addrKind = m_addrKind;
  2176. newOpnd->m_originalAddress = m_originalAddress;
  2177. #endif
  2178. return newOpnd;
  2179. }
  2180. bool
  2181. IndirOpnd::TryGetIntConstIndexValue(bool trySym, IntConstType *pValue, bool * pIsNotInt)
  2182. {
  2183. *pIsNotInt = false;
  2184. IR::RegOpnd * indexOpnd = this->GetIndexOpnd();
  2185. if (!indexOpnd)
  2186. {
  2187. *pValue = (IntConstType)this->GetOffset();
  2188. return true;
  2189. }
  2190. if (!trySym)
  2191. {
  2192. return false;
  2193. }
  2194. StackSym * indexSym = indexOpnd->m_sym;
  2195. *pIsNotInt = indexOpnd->IsNotInt();
  2196. // Const flags for type-specialized syms are not accurate during the forward pass, so the forward pass cannot use that info
  2197. // while the lowerer can. Additionally, due to value transfers being conservative in a loop prepass, the const flags can
  2198. // show that a sym has a constant value even though the value during the forward pass did not. Skip checking const flags for
  2199. // type-specialized index syms and instead, expect that once the above issues are fixed, that the forward pass would fold a
  2200. // constant index into the indir's offset.
  2201. if (!*pIsNotInt && !indexSym->IsTypeSpec() && indexSym->IsIntConst())
  2202. {
  2203. *pValue = indexSym->GetIntConstValue();
  2204. return true;
  2205. }
  2206. return false;
  2207. }
  2208. ///----------------------------------------------------------------------------
  2209. ///
  2210. /// IndirOpnd::IsEqual
  2211. ///
  2212. ///----------------------------------------------------------------------------
  2213. bool
  2214. IndirOpnd::IsEqualInternal(Opnd *opnd)
  2215. {
  2216. Assert(m_kind == OpndKindIndir);
  2217. Assert(opnd);
  2218. if (!opnd->IsIndirOpnd() || this->GetType() != opnd->GetType())
  2219. {
  2220. return false;
  2221. }
  2222. IndirOpnd *indirOpnd = opnd->AsIndirOpnd();
  2223. return m_offset == indirOpnd->m_offset
  2224. && ((m_baseOpnd == nullptr && indirOpnd->m_baseOpnd == nullptr) || (m_baseOpnd && indirOpnd->m_baseOpnd && m_baseOpnd->IsEqual(indirOpnd->m_baseOpnd)))
  2225. && ((m_indexOpnd == nullptr && indirOpnd->m_indexOpnd == nullptr) || (m_indexOpnd && indirOpnd->m_indexOpnd && m_indexOpnd->IsEqual(indirOpnd->m_indexOpnd)));
  2226. }
  2227. void
  2228. IndirOpnd::FreeInternal(Func *func)
  2229. {
  2230. Assert(m_kind == OpndKindIndir);
  2231. JitAdelete(func->m_alloc, this);
  2232. }
  2233. ///----------------------------------------------------------------------------
  2234. ///
  2235. /// IndirOpnd::SetBaseOpnd
  2236. ///
  2237. ///----------------------------------------------------------------------------
  2238. void
  2239. IndirOpnd::SetBaseOpnd(RegOpnd *baseOpnd)
  2240. {
  2241. if (m_baseOpnd)
  2242. {
  2243. m_baseOpnd->UnUse();
  2244. }
  2245. if (baseOpnd)
  2246. {
  2247. baseOpnd = baseOpnd->Use(m_func)->AsRegOpnd();
  2248. }
  2249. m_baseOpnd = baseOpnd;
  2250. }
  2251. ///----------------------------------------------------------------------------
  2252. ///
  2253. /// IndirOpnd::UnlinkBaseOpnd
  2254. ///
  2255. ///----------------------------------------------------------------------------
  2256. RegOpnd *
  2257. IndirOpnd::UnlinkBaseOpnd()
  2258. {
  2259. RegOpnd * baseOpnd = this->m_baseOpnd;
  2260. // This will also call UnUse()...
  2261. this->SetBaseOpnd(nullptr);
  2262. return baseOpnd;
  2263. }
  2264. void
  2265. IndirOpnd::ReplaceBaseOpnd(RegOpnd *newBase)
  2266. {
  2267. RegOpnd * baseOpnd = this->m_baseOpnd;
  2268. this->UnlinkBaseOpnd();
  2269. baseOpnd->Free(this->m_func);
  2270. this->SetBaseOpnd(newBase);
  2271. }
  2272. ///----------------------------------------------------------------------------
  2273. ///
  2274. /// IndirOpnd::SetIndexOpnd
  2275. ///
  2276. ///----------------------------------------------------------------------------
  2277. void
  2278. IndirOpnd::SetIndexOpnd(RegOpnd *indexOpnd)
  2279. {
  2280. if (m_indexOpnd)
  2281. {
  2282. m_indexOpnd->UnUse();
  2283. }
  2284. if (indexOpnd)
  2285. {
  2286. indexOpnd = indexOpnd->Use(m_func)->AsRegOpnd();
  2287. }
  2288. m_indexOpnd = indexOpnd;
  2289. }
  2290. ///----------------------------------------------------------------------------
  2291. ///
  2292. /// IndirOpnd::UnlinkIndexOpnd
  2293. ///
  2294. ///----------------------------------------------------------------------------
  2295. RegOpnd *
  2296. IndirOpnd::UnlinkIndexOpnd()
  2297. {
  2298. RegOpnd * indexOpnd = this->m_indexOpnd;
  2299. // This will also call UnUse()...
  2300. this->SetIndexOpnd(nullptr);
  2301. return indexOpnd;
  2302. }
  2303. void
  2304. IndirOpnd::ReplaceIndexOpnd(RegOpnd *newIndex)
  2305. {
  2306. RegOpnd * indexOpnd = this->m_indexOpnd;
  2307. this->UnlinkIndexOpnd();
  2308. indexOpnd->Free(this->m_func);
  2309. this->SetIndexOpnd(newIndex);
  2310. }
  2311. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  2312. const char16 *
  2313. IndirOpnd::GetDescription()
  2314. {
  2315. return this->m_desc;
  2316. }
  2317. bool
  2318. IndirOpnd::HasAddrKind() const
  2319. {
  2320. #if DBG_DUMP
  2321. return m_addrKind != (IR::AddrOpndKind) - 1;
  2322. #else
  2323. return false;
  2324. #endif
  2325. }
  2326. IR::AddrOpndKind
  2327. IndirOpnd::GetAddrKind() const
  2328. {
  2329. Assert(HasAddrKind());
  2330. #if DBG_DUMP
  2331. return m_addrKind;
  2332. #else
  2333. return IR::AddrOpndKindDynamicMisc;
  2334. #endif
  2335. }
  2336. void *
  2337. IndirOpnd::GetOriginalAddress() const
  2338. {
  2339. Assert(HasAddrKind());
  2340. #if DBG_DUMP
  2341. Assert(m_originalAddress != nullptr);
  2342. return m_originalAddress;
  2343. #else
  2344. return nullptr;
  2345. #endif
  2346. }
  2347. #endif
  2348. #if DBG_DUMP
  2349. void
  2350. IndirOpnd::SetAddrKind(IR::AddrOpndKind kind, void * originalAddress)
  2351. {
  2352. Assert(originalAddress != nullptr);
  2353. this->m_addrKind = kind;
  2354. this->m_originalAddress = originalAddress;
  2355. }
  2356. #endif
  2357. ///----------------------------------------------------------------------------
  2358. ///
  2359. /// MemRefOpnd::New
  2360. ///
  2361. /// Creates a new MemRefOpnd.
  2362. ///
  2363. ///----------------------------------------------------------------------------
  2364. MemRefOpnd *
  2365. MemRefOpnd::New(intptr_t pMemLoc, IRType type, Func *func, AddrOpndKind addrOpndKind)
  2366. {
  2367. MemRefOpnd * memRefOpnd = JitAnew(func->m_alloc, IR::MemRefOpnd);
  2368. memRefOpnd->m_memLoc = pMemLoc;
  2369. memRefOpnd->m_type = type;
  2370. memRefOpnd->m_kind = OpndKindMemRef;
  2371. #if DBG_DUMP
  2372. memRefOpnd->m_addrKind = addrOpndKind;
  2373. #endif
  2374. return memRefOpnd;
  2375. }
  2376. // TODO: michhol OOP JIT, remove this signature
  2377. MemRefOpnd *
  2378. MemRefOpnd::New(void * pMemLoc, IRType type, Func *func, AddrOpndKind addrOpndKind)
  2379. {
  2380. MemRefOpnd * memRefOpnd = JitAnew(func->m_alloc, IR::MemRefOpnd);
  2381. memRefOpnd->m_memLoc = (intptr_t)pMemLoc;
  2382. memRefOpnd->m_type = type;
  2383. memRefOpnd->m_kind = OpndKindMemRef;
  2384. #if DBG_DUMP
  2385. memRefOpnd->m_addrKind = addrOpndKind;
  2386. #endif
  2387. return memRefOpnd;
  2388. }
  2389. IR::AddrOpndKind
  2390. MemRefOpnd::GetAddrKind() const
  2391. {
  2392. #if DBG_DUMP
  2393. return this->m_addrKind;
  2394. #else
  2395. return AddrOpndKindDynamicMisc;
  2396. #endif
  2397. }
  2398. ///----------------------------------------------------------------------------
  2399. ///
  2400. /// MemRefOpnd::Copy
  2401. ///
  2402. /// Returns a copy of this opnd.
  2403. ///
  2404. ///----------------------------------------------------------------------------
  2405. MemRefOpnd *
  2406. MemRefOpnd::CopyInternal(Func *func)
  2407. {
  2408. Assert(m_kind == OpndKindMemRef);
  2409. MemRefOpnd * newOpnd;
  2410. newOpnd = MemRefOpnd::New(m_memLoc, m_type, func);
  2411. newOpnd->m_valueType = m_valueType;
  2412. newOpnd->m_memLoc = m_memLoc;
  2413. #if DBG_DUMP
  2414. newOpnd->m_addrKind = m_addrKind;
  2415. #endif
  2416. return newOpnd;
  2417. }
  2418. ///----------------------------------------------------------------------------
  2419. ///
  2420. /// MemRefOpnd::IsEqual
  2421. ///
  2422. ///----------------------------------------------------------------------------
  2423. bool
  2424. MemRefOpnd::IsEqualInternal(Opnd *opnd)
  2425. {
  2426. Assert(m_kind == OpndKindMemRef);
  2427. if (!opnd->IsMemRefOpnd() || this->GetType() != opnd->GetType())
  2428. {
  2429. return false;
  2430. }
  2431. MemRefOpnd *memRefOpnd = opnd->AsMemRefOpnd();
  2432. return m_memLoc == memRefOpnd->m_memLoc;
  2433. }
  2434. void
  2435. MemRefOpnd::FreeInternal(Func *func)
  2436. {
  2437. Assert(m_kind == OpndKindMemRef);
  2438. JitAdelete(func->m_alloc, this);
  2439. }
  2440. LabelOpnd *
  2441. LabelOpnd::New(LabelInstr * labelInstr, Func * func)
  2442. {
  2443. LabelOpnd * labelOpnd = JitAnew(func->m_alloc, IR::LabelOpnd);
  2444. labelOpnd->m_label = labelInstr;
  2445. labelOpnd->m_type = TyMachPtr;
  2446. labelInstr->m_hasNonBranchRef = true;
  2447. labelOpnd->m_kind = OpndKindLabel;
  2448. return labelOpnd;
  2449. }
  2450. LabelOpnd *
  2451. LabelOpnd::CopyInternal(Func * func)
  2452. {
  2453. Assert(m_kind == OpndKindLabel);
  2454. LabelOpnd * newOpnd;
  2455. newOpnd = LabelOpnd::New(m_label, func);
  2456. newOpnd->m_valueType = m_valueType;
  2457. return newOpnd;
  2458. }
  2459. bool
  2460. LabelOpnd::IsEqualInternal(Opnd * opnd)
  2461. {
  2462. Assert(m_kind == OpndKindLabel);
  2463. if (!opnd->IsLabelOpnd())
  2464. {
  2465. return false;
  2466. }
  2467. LabelOpnd * newOpnd = opnd->AsLabelOpnd();
  2468. return m_label == newOpnd->GetLabel();
  2469. }
  2470. void
  2471. LabelOpnd::FreeInternal(Func *func)
  2472. {
  2473. Assert(m_kind == OpndKindLabel);
  2474. JitAdelete(func->m_alloc, this);
  2475. }
  2476. IR::RegOpnd *
  2477. Opnd::FindRegUse(IR::RegOpnd *regOpnd)
  2478. {
  2479. StackSym *regSym = regOpnd->m_sym;
  2480. if (this->IsRegOpnd())
  2481. {
  2482. if (this->AsRegOpnd()->m_sym == regSym)
  2483. {
  2484. return this->AsRegOpnd();
  2485. }
  2486. }
  2487. else if (this->IsIndirOpnd())
  2488. {
  2489. IndirOpnd *indirOpnd = this->AsIndirOpnd();
  2490. if (indirOpnd->GetBaseOpnd() && indirOpnd->GetBaseOpnd()->m_sym == regSym)
  2491. {
  2492. return indirOpnd->GetBaseOpnd();
  2493. }
  2494. if (indirOpnd->GetIndexOpnd() && indirOpnd->GetIndexOpnd()->m_sym == regSym)
  2495. {
  2496. return indirOpnd->GetIndexOpnd();
  2497. }
  2498. }
  2499. return nullptr;
  2500. }
  2501. bool
  2502. Opnd::IsArgumentsObject()
  2503. {
  2504. // returns "false" if the sym is not single def (happens when the parent function has formals); the opnd can still be the arguments object.
  2505. // Since we need this information in the inliner where we don't track arguments object sym, going with single def is the best option.
  2506. StackSym * sym = this->GetStackSym();
  2507. return sym && sym->IsSingleDef() && sym->GetInstrDef()->HasAnyLoadHeapArgsOpCode();
  2508. }
  2509. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  2510. void
  2511. Opnd::DumpAddress(void *address, bool printToConsole, bool skipMaskedAddress)
  2512. {
  2513. if (!printToConsole)
  2514. {
  2515. return;
  2516. }
  2517. if (!Js::Configuration::Global.flags.DumpIRAddresses)
  2518. {
  2519. if (skipMaskedAddress)
  2520. {
  2521. return;
  2522. }
  2523. Output::Print(_u("0xXXXXXXXX"));
  2524. }
  2525. else
  2526. {
  2527. #ifdef TARGET_64
  2528. Output::Print(_u("0x%012I64X"), address);
  2529. #else
  2530. Output::Print(_u("0x%08X"), address);
  2531. #endif
  2532. }
  2533. }
  2534. void
  2535. Opnd::DumpFunctionInfo(_Outptr_result_buffer_(*count) char16 ** buffer, size_t * count, Js::FunctionInfo * info, bool printToConsole, _In_opt_z_ char16 const * type)
  2536. {
  2537. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  2538. if (info->HasBody())
  2539. {
  2540. if (type == nullptr)
  2541. {
  2542. type = _u("FunctionBody");
  2543. }
  2544. Js::FunctionProxy * proxy = info->GetFunctionProxy();
  2545. WriteToBuffer(buffer, count, _u(" (%s [%s%s])"), type, proxy->GetDisplayName(), proxy->GetDebugNumberSet(debugStringBuffer));
  2546. }
  2547. else
  2548. {
  2549. if (type == nullptr)
  2550. {
  2551. type = _u("FunctionInfo");
  2552. }
  2553. WriteToBuffer(buffer, count, _u(" (%s)"), type);
  2554. }
  2555. }
  2556. template<>
  2557. void EncodableOpnd<int32>::DumpEncodable() const
  2558. {
  2559. if (name != nullptr)
  2560. {
  2561. Output::Print(_u("<%s> (value: 0x%X)"), name, m_value);
  2562. }
  2563. else if (decodedValue != 0)
  2564. {
  2565. Output::Print(_u("%d (0x%X) [encoded: 0x%X]"), decodedValue, decodedValue, m_value);
  2566. }
  2567. else
  2568. {
  2569. Output::Print(_u("%d (0x%X)"), m_value, m_value);
  2570. }
  2571. }
  2572. template<>
  2573. void EncodableOpnd<int64>::DumpEncodable() const
  2574. {
  2575. if (name != nullptr)
  2576. {
  2577. Output::Print(_u("<%s> (value: 0x%llX)"), name, m_value);
  2578. }
  2579. else if (decodedValue != 0)
  2580. {
  2581. Output::Print(_u("%lld (0x%llX) [encoded: 0x%llX]"), decodedValue, decodedValue, m_value);
  2582. }
  2583. else
  2584. {
  2585. Output::Print(_u("%lld (0x%llX)"), m_value, m_value);
  2586. }
  2587. }
  2588. ///----------------------------------------------------------------------------
  2589. ///
  2590. /// Opnd::Dump
  2591. ///
  2592. /// Dump this opnd.
  2593. ///
  2594. ///----------------------------------------------------------------------------
  2595. void
  2596. Opnd::Dump(IRDumpFlags flags, Func *func)
  2597. {
  2598. bool const AsmDumpMode = flags & IRDumpFlags_AsmDumpMode;
  2599. bool const SimpleForm = !!(flags & IRDumpFlags_SimpleForm);
  2600. FloatConstType floatValue;
  2601. SymOpnd * symOpnd;
  2602. RegOpnd * regOpnd;
  2603. JnHelperMethod helperMethod;
  2604. bool dumpValueType = !SimpleForm;
  2605. switch(GetKind())
  2606. {
  2607. case OpndKindSym:
  2608. symOpnd = this->AsSymOpnd();
  2609. if(symOpnd->m_sym->IsPropertySym() && !SimpleForm)
  2610. {
  2611. symOpnd->m_sym->Dump(flags, symOpnd->GetPropertyOwnerValueType());
  2612. }
  2613. else
  2614. {
  2615. symOpnd->m_sym->Dump(flags);
  2616. }
  2617. if (symOpnd->m_sym->IsStackSym())
  2618. {
  2619. StackSym * stackSym = symOpnd->m_sym->AsStackSym();
  2620. bool hasOffset = stackSym->IsArgSlotSym()?
  2621. ((stackSym->m_offset != -1) || !stackSym->m_isInlinedArgSlot) :
  2622. (stackSym->m_offset != 0);
  2623. if (hasOffset)
  2624. {
  2625. int offset = stackSym->m_offset;
  2626. if(symOpnd->m_offset != 0)
  2627. {
  2628. Assert(static_cast<int>(offset + symOpnd->m_offset) >= offset);
  2629. offset += symOpnd->m_offset;
  2630. }
  2631. Output::Print(_u("<%d>"), offset);
  2632. }
  2633. }
  2634. else if (symOpnd->IsPropertySymOpnd() && !SimpleForm)
  2635. {
  2636. PropertySymOpnd *propertySymOpnd = symOpnd->AsPropertySymOpnd();
  2637. Output::Print(_u("<"));
  2638. if (propertySymOpnd->HasObjTypeSpecFldInfo())
  2639. {
  2640. Output::Print(_u("%u,%s%s%s%s,"), propertySymOpnd->GetObjTypeSpecFldId(), propertySymOpnd->IsPoly() ? _u("p") : _u("m"),
  2641. propertySymOpnd->IsLoadedFromProto() ? _u("~") : _u(""), propertySymOpnd->UsesFixedValue() ? _u("=") : _u(""),
  2642. propertySymOpnd->IsBeingAdded() ? _u("+") : _u(""));
  2643. }
  2644. else
  2645. {
  2646. Output::Print(_u("?,,"));
  2647. }
  2648. Output::Print(_u("%s%s,"), propertySymOpnd->MayNeedTypeCheckProtection() ?
  2649. propertySymOpnd->IsMono() ? _u("+") : _u("=") :
  2650. propertySymOpnd->IsRootObjectNonConfigurableFieldLoad() ? _u("~") : _u("-"),
  2651. propertySymOpnd->IsTypeCheckSeqCandidate() ? _u("+") : _u("-"));
  2652. if (propertySymOpnd->HasObjectTypeSym())
  2653. {
  2654. Output::Print(_u("s%d"), propertySymOpnd->GetObjectTypeSym()->m_id);
  2655. if (propertySymOpnd->IsTypeChecked())
  2656. {
  2657. Output::Print(_u("+%s"), propertySymOpnd->IsMono() ? _u("m") : _u("p"));
  2658. }
  2659. else if (propertySymOpnd->IsTypeAvailable())
  2660. {
  2661. Output::Print(_u("*"));
  2662. }
  2663. if (propertySymOpnd->IsTypeDead())
  2664. {
  2665. Output::Print(_u("!"));
  2666. }
  2667. }
  2668. else
  2669. {
  2670. Output::Print(_u("s?"));
  2671. }
  2672. if (propertySymOpnd->m_sym->AsPropertySym()->m_writeGuardSym != nullptr)
  2673. {
  2674. Output::Print(_u(",s%d"), propertySymOpnd->m_sym->AsPropertySym()->m_writeGuardSym->m_id);
  2675. if (propertySymOpnd->IsWriteGuardChecked())
  2676. {
  2677. Output::Print(_u("+"));
  2678. }
  2679. }
  2680. else
  2681. {
  2682. Output::Print(_u(",s?"));
  2683. }
  2684. if (propertySymOpnd->HasFinalType())
  2685. {
  2686. Output::Print(_u(",final:"));
  2687. this->DumpAddress((void*)propertySymOpnd->GetFinalType()->GetAddr(), /* printToConsole */ true, /* skipMaskedAddress */ false);
  2688. }
  2689. if (propertySymOpnd->GetGuardedPropOps() != nullptr)
  2690. {
  2691. Output::Print(_u(",{"));
  2692. if (func != nullptr)
  2693. {
  2694. int i = 0;
  2695. auto guardedPropOps = propertySymOpnd->GetGuardedPropOps();
  2696. FOREACH_BITSET_IN_SPARSEBV(propertyOpId, guardedPropOps)
  2697. {
  2698. if (i++ > 0)
  2699. {
  2700. Output::Print(_u(","));
  2701. }
  2702. const ObjTypeSpecFldInfo* propertyOpInfo = func->GetTopFunc()->GetGlobalObjTypeSpecFldInfo(propertyOpId);
  2703. if (!JITManager::GetJITManager()->IsOOPJITEnabled())
  2704. {
  2705. Output::Print(_u("%s"), func->GetInProcThreadContext()->GetPropertyRecord(propertyOpInfo->GetPropertyId())->GetBuffer(), propertyOpId);
  2706. }
  2707. Output::Print(_u("(%u)"), propertyOpId);
  2708. if (propertyOpInfo->IsLoadedFromProto())
  2709. {
  2710. Output::Print(_u("~"));
  2711. }
  2712. if (propertyOpInfo->HasFixedValue())
  2713. {
  2714. Output::Print(_u("="));
  2715. }
  2716. if (propertyOpInfo->IsBeingAdded())
  2717. {
  2718. Output::Print(_u("+"));
  2719. }
  2720. }
  2721. NEXT_BITSET_IN_SPARSEBV;
  2722. }
  2723. else
  2724. {
  2725. Output::Print(_u("(no func)"));
  2726. }
  2727. Output::Print(_u("}"));
  2728. }
  2729. if (propertySymOpnd->GetWriteGuards() != nullptr)
  2730. {
  2731. Output::Print(_u(",{"));
  2732. int i = 0;
  2733. auto writeGuards = propertySymOpnd->GetWriteGuards();
  2734. FOREACH_BITSET_IN_SPARSEBV(writeGuardSymId, writeGuards)
  2735. {
  2736. if (i++ > 0)
  2737. {
  2738. Output::Print(_u(","));
  2739. }
  2740. Output::Print(_u("s%d"), writeGuardSymId);
  2741. }
  2742. NEXT_BITSET_IN_SPARSEBV;
  2743. Output::Print(_u("}"));
  2744. }
  2745. if (propertySymOpnd->canStoreTemp)
  2746. {
  2747. Output::Print(_u(",t"));
  2748. }
  2749. Output::Print(_u(">"));
  2750. }
  2751. break;
  2752. case OpndKindReg:
  2753. regOpnd = this->AsRegOpnd();
  2754. if (regOpnd->m_sym)
  2755. {
  2756. regOpnd->m_sym->Dump(flags);
  2757. }
  2758. if(AsmDumpMode)
  2759. {
  2760. //
  2761. // Print no brackets
  2762. //
  2763. Output::Print(_u("%S"), RegNames[regOpnd->GetReg()]);
  2764. }
  2765. else
  2766. {
  2767. if (regOpnd->GetReg() != RegNOREG)
  2768. {
  2769. Output::Print(_u("(%S)"), RegNames[regOpnd->GetReg()]);
  2770. }
  2771. if (regOpnd->m_isTempLastUse)
  2772. {
  2773. Output::Print(_u("[isTempLastUse]"));
  2774. }
  2775. StackSym *sym = regOpnd->GetStackSym();
  2776. if (sym && func)
  2777. {
  2778. if (sym == func->GetScriptContextSym())
  2779. {
  2780. Output::Print(_u("[ScriptContext]"));
  2781. }
  2782. else if (sym == func->GetFuncObjSym())
  2783. {
  2784. Output::Print(_u("[FuncObj]"));
  2785. }
  2786. else if (sym == func->GetFunctionBodySym())
  2787. {
  2788. Output::Print(_u("[FunctionBody]"));
  2789. }
  2790. }
  2791. if(regOpnd->IsArrayRegOpnd())
  2792. {
  2793. if(dumpValueType)
  2794. {
  2795. // Dump the array value type before the associated syms
  2796. DumpValueType();
  2797. dumpValueType = false;
  2798. }
  2799. const ArrayRegOpnd *const arrayRegOpnd = regOpnd->AsArrayRegOpnd();
  2800. if(arrayRegOpnd->HeadSegmentSym())
  2801. {
  2802. Output::Print(_u("[seg: "));
  2803. arrayRegOpnd->HeadSegmentSym()->Dump();
  2804. Output::Print(_u("]"));
  2805. }
  2806. if(arrayRegOpnd->HeadSegmentLengthSym())
  2807. {
  2808. Output::Print(_u("[segLen: "));
  2809. arrayRegOpnd->HeadSegmentLengthSym()->Dump();
  2810. Output::Print(_u("]"));
  2811. }
  2812. if(arrayRegOpnd->LengthSym() && arrayRegOpnd->LengthSym() != arrayRegOpnd->HeadSegmentLengthSym())
  2813. {
  2814. Output::Print(_u("[len: "));
  2815. arrayRegOpnd->LengthSym()->Dump();
  2816. Output::Print(_u("]"));
  2817. }
  2818. if(arrayRegOpnd->EliminatedLowerBoundCheck() || arrayRegOpnd->EliminatedUpperBoundCheck())
  2819. {
  2820. Output::Print(_u("["));
  2821. if(arrayRegOpnd->EliminatedLowerBoundCheck())
  2822. {
  2823. Output::Print(_u(">"));
  2824. }
  2825. if(arrayRegOpnd->EliminatedUpperBoundCheck())
  2826. {
  2827. Output::Print(_u("<"));
  2828. }
  2829. Output::Print(_u("]"));
  2830. }
  2831. }
  2832. }
  2833. break;
  2834. case OpndKindInt64Const:
  2835. {
  2836. Int64ConstOpnd * intConstOpnd = this->AsInt64ConstOpnd();
  2837. intConstOpnd->DumpEncodable();
  2838. break;
  2839. }
  2840. case OpndKindIntConst:
  2841. {
  2842. IntConstOpnd * intConstOpnd = this->AsIntConstOpnd();
  2843. intConstOpnd->DumpEncodable();
  2844. break;
  2845. }
  2846. case OpndKindRegBV:
  2847. {
  2848. RegBVOpnd * regBVOpnd = this->AsRegBVOpnd();
  2849. regBVOpnd->m_value.Dump();
  2850. break;
  2851. }
  2852. case OpndKindHelperCall:
  2853. helperMethod = this->AsHelperCallOpnd()->m_fnHelper;
  2854. Output::Print(_u("%s"), IR::GetMethodName(helperMethod));
  2855. break;
  2856. case OpndKindFloatConst:
  2857. floatValue = this->AsFloatConstOpnd()->m_value;
  2858. Output::Print(_u("%G"), floatValue);
  2859. break;
  2860. case OpndKindFloat32Const:
  2861. Output::Print(_u("%G"), this->AsFloat32ConstOpnd()->m_value);
  2862. break;
  2863. case OpndKindAddr:
  2864. DumpOpndKindAddr(AsmDumpMode, func);
  2865. break;
  2866. case OpndKindIndir:
  2867. {
  2868. IndirOpnd * indirOpnd = this->AsIndirOpnd();
  2869. RegOpnd * baseOpnd = indirOpnd->GetBaseOpnd();
  2870. RegOpnd * indexOpnd = indirOpnd->GetIndexOpnd();
  2871. const int32 offset = indirOpnd->GetOffset();
  2872. Output::Print(_u("["));
  2873. if (baseOpnd != nullptr)
  2874. {
  2875. baseOpnd->Dump(flags, func);
  2876. }
  2877. else
  2878. {
  2879. Output::Print(_u("<null>"));
  2880. }
  2881. if (indexOpnd != nullptr)
  2882. {
  2883. Output::Print(_u("+"));
  2884. indexOpnd->Dump(flags, func);
  2885. if (indirOpnd->GetScale() > 0)
  2886. {
  2887. Output::Print(_u("*%d"), 1 << indirOpnd->GetScale());
  2888. }
  2889. }
  2890. if (offset != 0)
  2891. {
  2892. if (!Js::Configuration::Global.flags.DumpIRAddresses && indirOpnd->HasAddrKind())
  2893. {
  2894. Output::Print(_u("+XX"));
  2895. }
  2896. else
  2897. {
  2898. const auto sign = offset >= 0 ? _u("+") : _u("");
  2899. if (AsmDumpMode)
  2900. {
  2901. Output::Print(_u("%sXXXX%04d"), sign, offset & 0xffff);
  2902. }
  2903. else
  2904. {
  2905. Output::Print(_u("%s%d"), sign, offset);
  2906. }
  2907. }
  2908. }
  2909. if (indirOpnd->GetDescription())
  2910. {
  2911. Output::Print(_u(" <%s>"), indirOpnd->GetDescription());
  2912. }
  2913. if (indirOpnd->HasAddrKind())
  2914. {
  2915. INT_PTR address = (INT_PTR)indirOpnd->GetOriginalAddress();
  2916. Output::Print(_u(" <"));
  2917. const size_t BUFFER_LEN = 128;
  2918. char16 buffer[BUFFER_LEN];
  2919. GetAddrDescription(buffer, BUFFER_LEN, (void *)address, indirOpnd->GetAddrKind(), AsmDumpMode, /*printToConsole */ true, func, /* skipMaskedAddress */true);
  2920. Output::Print(_u("%s"), buffer);
  2921. Output::Print(_u(">"));
  2922. }
  2923. Output::Print(_u("]"));
  2924. break;
  2925. }
  2926. case IR::OpndKindList:
  2927. {
  2928. IR::ListOpnd* list = this->AsListOpnd();
  2929. Output::Print(_u("{"));
  2930. int count = list->Count();
  2931. list->Map([flags, func, count](int i, IR::Opnd* opnd)
  2932. {
  2933. opnd->Dump(flags, func);
  2934. if (i + 1 < count)
  2935. {
  2936. Output::Print(_u(","));
  2937. }
  2938. });
  2939. Output::Print(_u("}"));
  2940. break;
  2941. }
  2942. case OpndKindMemRef:
  2943. {
  2944. DumpOpndKindMemRef(AsmDumpMode, func);
  2945. break;
  2946. }
  2947. case OpndKindLabel:
  2948. {
  2949. LabelOpnd * labelOpnd = this->AsLabelOpnd();
  2950. LabelInstr * labelInstr = labelOpnd->GetLabel();
  2951. if (labelInstr == nullptr)
  2952. {
  2953. Output::Print(_u("??"));
  2954. }
  2955. else
  2956. {
  2957. Output::Print(_u("&$L%d"), labelInstr->m_id);
  2958. }
  2959. break;
  2960. }
  2961. }
  2962. if(!AsmDumpMode && dumpValueType)
  2963. {
  2964. DumpValueType();
  2965. }
  2966. if (!SimpleForm || this->GetType() != TyVar)
  2967. {
  2968. Output::Print(_u("."));
  2969. IRType_Dump(this->GetType());
  2970. }
  2971. if (this->m_isDead && !SimpleForm)
  2972. {
  2973. Output::Print(_u("!"));
  2974. }
  2975. }
  2976. ///----------------------------------------------------------------------------
  2977. ///
  2978. /// Opnd::DumpOpndKindAddr
  2979. ///
  2980. /// Dump this opnd as an address.
  2981. ///
  2982. ///----------------------------------------------------------------------------
  2983. void
  2984. Opnd::DumpOpndKindAddr(bool AsmDumpMode, Func *func)
  2985. {
  2986. const size_t BUFFER_LEN = 128;
  2987. char16 buffer[BUFFER_LEN];
  2988. GetAddrDescription(buffer, BUFFER_LEN, AsmDumpMode, true, func);
  2989. Output::Print(_u("%s"), buffer);
  2990. }
  2991. void
  2992. Opnd::DumpOpndKindMemRef(bool AsmDumpMode, Func *func)
  2993. {
  2994. MemRefOpnd *memRefOpnd = this->AsMemRefOpnd();
  2995. Output::Print(_u("["));
  2996. const size_t BUFFER_LEN = 128;
  2997. char16 buffer[BUFFER_LEN];
  2998. // TODO: michhol, make this intptr_t
  2999. GetAddrDescription(buffer, BUFFER_LEN, (void*)memRefOpnd->GetMemLoc(), memRefOpnd->GetAddrKind(), AsmDumpMode, true, func);
  3000. Output::Print(_u("%s"), buffer);
  3001. Output::Print(_u("]"));
  3002. }
  3003. /**
  3004. WriteToBuffer
  3005. Write <fmt> with applicable replacements into <buffer>.
  3006. Subtract the number of characters written from <count>, and increment the address
  3007. <buffer> so that subsequent calls to this function will continue writing at the point
  3008. in the buffer where this function left off and will respect the maximum length specified
  3009. by count.
  3010. @param buffer
  3011. A pointer to a buffer which will hold the result.
  3012. @param count
  3013. The maximum number of characters that should be returned in <buffer>.
  3014. @param fmt
  3015. A format string.
  3016. @param ...
  3017. Additional parameters to be passed to the formatter.
  3018. */
  3019. void
  3020. Opnd::WriteToBuffer(_Outptr_result_buffer_(*count) char16 **buffer, size_t *count, const char16 *fmt, ...)
  3021. {
  3022. va_list argptr;
  3023. va_start(argptr, fmt);
  3024. int len = _vsnwprintf_s(*buffer, *count, _TRUNCATE, fmt, argptr);
  3025. *count -= len;
  3026. *buffer += len;
  3027. va_end(argptr);
  3028. }
  3029. void
  3030. Opnd::GetAddrDescription(__out_ecount(count) char16 *const description, const size_t count,
  3031. void * address, IR::AddrOpndKind addressKind, bool AsmDumpMode, bool printToConsole, Func *func, bool skipMaskedAddress)
  3032. {
  3033. char16 *buffer = description;
  3034. size_t n = count;
  3035. if (address)
  3036. {
  3037. switch (addressKind)
  3038. {
  3039. case IR::AddrOpndKindConstantAddress:
  3040. {
  3041. #ifdef TARGET_64
  3042. char16 const * format = _u("0x%012I64X");
  3043. #else
  3044. char16 const * format = _u("0x%08X");
  3045. #endif
  3046. WriteToBuffer(&buffer, &n, format, address);
  3047. }
  3048. break;
  3049. case IR::AddrOpndKindDynamicVar:
  3050. if (Js::TaggedInt::Is(address))
  3051. {
  3052. #ifdef TARGET_64
  3053. char16 const * format = _u("0x%012I64X (value: %d)");
  3054. #else
  3055. char16 const * format = _u("0x%08X (value: %d)");
  3056. #endif
  3057. WriteToBuffer(&buffer, &n, format, address, Js::TaggedInt::ToInt32(address));
  3058. }
  3059. #if FLOATVAR
  3060. else if (Js::JavascriptNumber::Is_NoTaggedIntCheck(address))
  3061. #else
  3062. else if (!func->IsOOPJIT() && Js::JavascriptNumber::Is_NoTaggedIntCheck(address))
  3063. #endif
  3064. {
  3065. WriteToBuffer(&buffer, &n, _u(" (value: %f)"), Js::JavascriptNumber::GetValue(address));
  3066. }
  3067. else
  3068. {
  3069. DumpAddress(address, printToConsole, skipMaskedAddress);
  3070. // TODO: michhol OOP JIT, fix dumping these
  3071. if (func->IsOOPJIT())
  3072. {
  3073. WriteToBuffer(&buffer, &n, _u(" (unknown)"));
  3074. }
  3075. else
  3076. {
  3077. switch (Js::RecyclableObject::FromVar(address)->GetTypeId())
  3078. {
  3079. case Js::TypeIds_Boolean:
  3080. WriteToBuffer(&buffer, &n, Js::JavascriptBoolean::FromVar(address)->GetValue() ? _u(" (true)") : _u(" (false)"));
  3081. break;
  3082. case Js::TypeIds_String:
  3083. WriteToBuffer(&buffer, &n, _u(" (\"%s\")"), Js::JavascriptString::FromVar(address)->GetSz());
  3084. break;
  3085. case Js::TypeIds_Number:
  3086. WriteToBuffer(&buffer, &n, _u(" (value: %f)"), Js::JavascriptNumber::GetValue(address));
  3087. break;
  3088. case Js::TypeIds_Undefined:
  3089. WriteToBuffer(&buffer, &n, _u(" (undefined)"));
  3090. break;
  3091. case Js::TypeIds_Null:
  3092. WriteToBuffer(&buffer, &n, _u(" (null)"));
  3093. break;
  3094. case Js::TypeIds_GlobalObject:
  3095. WriteToBuffer(&buffer, &n, _u(" (GlobalObject)"));
  3096. break;
  3097. case Js::TypeIds_UndeclBlockVar:
  3098. WriteToBuffer(&buffer, &n, _u(" (UndeclBlockVar)"));
  3099. break;
  3100. case Js::TypeIds_Function:
  3101. DumpFunctionInfo(&buffer, &n, ((Js::JavascriptFunction *)address)->GetFunctionInfo(), printToConsole, _u("FunctionObject"));
  3102. break;
  3103. default:
  3104. WriteToBuffer(&buffer, &n, _u(" (DynamicObject)"));
  3105. break;
  3106. }
  3107. }
  3108. }
  3109. break;
  3110. case IR::AddrOpndKindConstantVar:
  3111. {
  3112. #ifdef TARGET_64
  3113. char16 const * format = _u("0x%012I64X%s");
  3114. #else
  3115. char16 const * format = _u("0x%08X%s");
  3116. #endif
  3117. char16 const * addressName = _u("");
  3118. if (address == Js::JavascriptArray::MissingItem)
  3119. {
  3120. addressName = _u(" (MissingItem)");
  3121. }
  3122. #if FLOATVAR
  3123. else if (address == (Js::Var)Js::FloatTag_Value)
  3124. {
  3125. addressName = _u(" (FloatTag)");
  3126. }
  3127. #endif
  3128. WriteToBuffer(&buffer, &n, format, address, addressName);
  3129. break;
  3130. }
  3131. case IR::AddrOpndKindDynamicScriptContext:
  3132. Assert(func == nullptr || (intptr_t)address == func->GetScriptContextInfo()->GetAddr());
  3133. // The script context pointer is unstable allocated from the CRT
  3134. DumpAddress(address, printToConsole, skipMaskedAddress);
  3135. WriteToBuffer(&buffer, &n, _u(" (ScriptContext)"));
  3136. break;
  3137. case IR::AddrOpndKindDynamicCharStringCache:
  3138. Assert(func == nullptr || (intptr_t)address == func->GetScriptContextInfo()->GetCharStringCacheAddr());
  3139. DumpAddress(address, printToConsole, skipMaskedAddress);
  3140. WriteToBuffer(&buffer, &n, _u(" (CharStringCache)"));
  3141. break;
  3142. case IR::AddrOpndKindDynamicBailOutRecord:
  3143. DumpAddress(address, printToConsole, skipMaskedAddress);
  3144. WriteToBuffer(&buffer, &n, _u(" (BailOutRecord)"));
  3145. break;
  3146. case IR::AddrOpndKindDynamicInlineCache:
  3147. DumpAddress(address, printToConsole, skipMaskedAddress);
  3148. WriteToBuffer(&buffer, &n, _u(" (InlineCache)"));
  3149. break;
  3150. case IR::AddrOpndKindDynamicIsInstInlineCacheFunctionRef:
  3151. DumpAddress(address, printToConsole, skipMaskedAddress);
  3152. WriteToBuffer(&buffer, &n, _u(" (&IsInstInlineCache.function)"));
  3153. break;
  3154. case IR::AddrOpndKindDynamicIsInstInlineCacheTypeRef:
  3155. DumpAddress(address, printToConsole, skipMaskedAddress);
  3156. WriteToBuffer(&buffer, &n, _u(" (&IsInstInlineCache.type)"));
  3157. break;
  3158. case IR::AddrOpndKindDynamicIsInstInlineCacheResultRef:
  3159. DumpAddress(address, printToConsole, skipMaskedAddress);
  3160. WriteToBuffer(&buffer, &n, _u(" (&IsInstInlineCache.result)"));
  3161. break;
  3162. case AddrOpndKindDynamicGuardValueRef:
  3163. DumpAddress(address, printToConsole, skipMaskedAddress);
  3164. WriteToBuffer(&buffer, &n, _u(" (&GuardValue)"));
  3165. break;
  3166. case AddrOpndKindDynamicAuxSlotArrayRef:
  3167. DumpAddress(address, printToConsole, skipMaskedAddress);
  3168. WriteToBuffer(&buffer, &n, _u(" (&AuxSlotArray)"));
  3169. break;
  3170. case AddrOpndKindDynamicPropertySlotRef:
  3171. DumpAddress(address, printToConsole, skipMaskedAddress);
  3172. WriteToBuffer(&buffer, &n, _u(" (&PropertySlot)"));
  3173. break;
  3174. case AddrOpndKindDynamicBailOutKindRef:
  3175. DumpAddress(address, printToConsole, skipMaskedAddress);
  3176. WriteToBuffer(&buffer, &n, _u(" (&BailOutKind)"));
  3177. break;
  3178. case AddrOpndKindDynamicArrayCallSiteInfo:
  3179. DumpAddress(address, printToConsole, skipMaskedAddress);
  3180. WriteToBuffer(&buffer, &n, _u(" (ArrayCallSiteInfo)"));
  3181. break;
  3182. case AddrOpndKindDynamicTypeCheckGuard:
  3183. DumpAddress(address, printToConsole, skipMaskedAddress);
  3184. WriteToBuffer(&buffer, &n, _u(" (TypeCheckGuard)"));
  3185. break;
  3186. case AddrOpndKindDynamicRecyclerAllocatorEndAddressRef:
  3187. DumpAddress(address, printToConsole, skipMaskedAddress);
  3188. WriteToBuffer(&buffer, &n, _u(" (&RecyclerAllocatorEndAddress)"));
  3189. break;
  3190. case AddrOpndKindDynamicAuxBufferRef:
  3191. DumpAddress(address, printToConsole, skipMaskedAddress);
  3192. WriteToBuffer(&buffer, &n, _u(" (AuxBufferRef)"));
  3193. break;
  3194. case AddrOpndKindDynamicRecyclerAllocatorFreeListRef:
  3195. DumpAddress(address, printToConsole, skipMaskedAddress);
  3196. WriteToBuffer(&buffer, &n, _u(" (&RecyclerAllocatorFreeList)"));
  3197. break;
  3198. case IR::AddrOpndKindDynamicFunctionInfo:
  3199. DumpAddress(address, printToConsole, skipMaskedAddress);
  3200. if (func->IsOOPJIT())
  3201. {
  3202. // TODO: OOP JIT, dump more info
  3203. WriteToBuffer(&buffer, &n, _u(" (FunctionInfo)"));
  3204. }
  3205. else
  3206. {
  3207. DumpFunctionInfo(&buffer, &n, (Js::FunctionInfo *)address, printToConsole);
  3208. }
  3209. break;
  3210. case IR::AddrOpndKindDynamicFunctionBody:
  3211. DumpAddress(address, printToConsole, skipMaskedAddress);
  3212. if (func->IsOOPJIT())
  3213. {
  3214. // TODO: OOP JIT, dump more info
  3215. WriteToBuffer(&buffer, &n, _u(" (FunctionBody)"));
  3216. }
  3217. else
  3218. {
  3219. DumpFunctionInfo(&buffer, &n, ((Js::FunctionBody *)address)->GetFunctionInfo(), printToConsole);
  3220. }
  3221. break;
  3222. case IR::AddrOpndKindDynamicFunctionBodyWeakRef:
  3223. DumpAddress(address, printToConsole, skipMaskedAddress);
  3224. if (func->IsOOPJIT())
  3225. {
  3226. // TODO: OOP JIT, dump more info
  3227. WriteToBuffer(&buffer, &n, _u(" (FunctionBodyWeakRef)"));
  3228. }
  3229. else
  3230. {
  3231. DumpFunctionInfo(&buffer, &n, ((RecyclerWeakReference<Js::FunctionBody> *)address)->FastGet()->GetFunctionInfo(), printToConsole, _u("FunctionBodyWeakRef"));
  3232. }
  3233. break;
  3234. case IR::AddrOpndKindDynamicFunctionEnvironmentRef:
  3235. DumpAddress(address, printToConsole, skipMaskedAddress);
  3236. DumpFunctionInfo(&buffer, &n, ((Js::ScriptFunction *)((intptr_t)address - Js::ScriptFunction::GetOffsetOfEnvironment()))->GetFunctionInfo(),
  3237. printToConsole, _u("ScriptFunctionEnvironmentRef"));
  3238. break;
  3239. case IR::AddrOpndKindDynamicVtable:
  3240. if ((INT_PTR)address == Js::ScriptContextOptimizationOverrideInfo::InvalidVtable)
  3241. {
  3242. WriteToBuffer(&buffer, &n, _u("%d (Invalid Vtable)"), Js::ScriptContextOptimizationOverrideInfo::InvalidVtable);
  3243. }
  3244. else
  3245. {
  3246. DumpAddress(address, printToConsole, skipMaskedAddress);
  3247. WriteToBuffer(&buffer, &n, _u(" (%S Vtable)"), func->GetVtableName((INT_PTR)address));
  3248. }
  3249. break;
  3250. case IR::AddrOpndKindDynamicTypeHandler:
  3251. DumpAddress(address, printToConsole, skipMaskedAddress);
  3252. WriteToBuffer(&buffer, &n, _u(" (TypeHandler)"));
  3253. break;
  3254. case IR::AddrOpndKindDynamicObjectTypeRef:
  3255. DumpAddress(address, printToConsole, skipMaskedAddress);
  3256. {
  3257. Js::RecyclableObject * dynamicObject = (Js::RecyclableObject *)((intptr_t)address - Js::RecyclableObject::GetOffsetOfType());
  3258. if (!func->IsOOPJIT() && Js::JavascriptFunction::Is(dynamicObject))
  3259. {
  3260. DumpFunctionInfo(&buffer, &n, Js::JavascriptFunction::FromVar((void *)((intptr_t)address - Js::RecyclableObject::GetOffsetOfType()))->GetFunctionInfo(),
  3261. printToConsole, _u("FunctionObjectTypeRef"));
  3262. }
  3263. else
  3264. {
  3265. // TODO: OOP JIT, dump more info
  3266. WriteToBuffer(&buffer, &n, _u(" (ObjectTypeRef)"));
  3267. }
  3268. }
  3269. break;
  3270. case IR::AddrOpndKindDynamicType:
  3271. DumpAddress(address, printToConsole, skipMaskedAddress);
  3272. // TODO: OOP JIT, dump more info
  3273. if(!func->IsOOPJIT())
  3274. {
  3275. Js::TypeId typeId = ((Js::Type*)address)->GetTypeId();
  3276. switch (typeId)
  3277. {
  3278. case Js::TypeIds_Number:
  3279. WriteToBuffer(&buffer, &n, _u(" (Type: StaticNumber)"));
  3280. break;
  3281. case Js::TypeIds_String:
  3282. WriteToBuffer(&buffer, &n, _u(" (Type: StaticString)"));
  3283. break;
  3284. case Js::TypeIds_Object:
  3285. WriteToBuffer(&buffer, &n, _u(" (Type: Object)"));
  3286. break;
  3287. case Js::TypeIds_RegEx:
  3288. WriteToBuffer(&buffer, &n, _u(" (Type: Regex)"));
  3289. break;
  3290. case Js::TypeIds_Array:
  3291. WriteToBuffer(&buffer, &n, _u(" (Type: Array)"));
  3292. break;
  3293. case Js::TypeIds_NativeIntArray:
  3294. WriteToBuffer(&buffer, &n, _u(" (Type: NativeIntArray)"));
  3295. break;
  3296. case Js::TypeIds_NativeFloatArray:
  3297. WriteToBuffer(&buffer, &n, _u(" (Type: NativeFltArray)"));
  3298. break;
  3299. default:
  3300. WriteToBuffer(&buffer, &n, _u(" (Type: Id %d)"), typeId);
  3301. break;
  3302. }
  3303. }
  3304. break;
  3305. case AddrOpndKindDynamicFrameDisplay:
  3306. DumpAddress(address, printToConsole, skipMaskedAddress);
  3307. if (!func->IsOOPJIT())
  3308. {
  3309. Js::FrameDisplay * frameDisplay = (Js::FrameDisplay *)address;
  3310. WriteToBuffer(&buffer, &n, (frameDisplay->GetStrictMode() ? _u(" (StrictFrameDisplay len %d)") : _u(" (FrameDisplay len %d)")),
  3311. frameDisplay->GetLength());
  3312. }
  3313. else
  3314. {
  3315. WriteToBuffer(&buffer, &n, _u(" (FrameDisplay)"));
  3316. }
  3317. break;
  3318. case AddrOpndKindSz:
  3319. WriteToBuffer(&buffer, &n, wcslen((char16 const *)address) > 30 ? _u("\"%.30s...\"") : _u("\"%.30s\""), address);
  3320. break;
  3321. case AddrOpndKindDynamicFloatRef:
  3322. DumpAddress(address, printToConsole, skipMaskedAddress);
  3323. WriteToBuffer(&buffer, &n, _u(" (&(float)%f)"), *(float *)address);
  3324. break;
  3325. case AddrOpndKindDynamicDoubleRef:
  3326. DumpAddress(address, printToConsole, skipMaskedAddress);
  3327. WriteToBuffer(&buffer, &n, _u(" (&(double)%f)"), *(double *)address);
  3328. break;
  3329. case AddrOpndKindForInCache:
  3330. DumpAddress(address, printToConsole, skipMaskedAddress);
  3331. WriteToBuffer(&buffer, &n, _u(" (ForInCache)"));
  3332. break;
  3333. case AddrOpndKindForInCacheType:
  3334. DumpAddress(address, printToConsole, skipMaskedAddress);
  3335. WriteToBuffer(&buffer, &n, _u(" (&ForInCache->type)"));
  3336. break;
  3337. case AddrOpndKindForInCacheData:
  3338. DumpAddress(address, printToConsole, skipMaskedAddress);
  3339. WriteToBuffer(&buffer, &n, _u(" (&ForInCache->data)"));
  3340. break;
  3341. case AddrOpndKindDynamicNativeCodeDataRef:
  3342. DumpAddress(address, printToConsole, skipMaskedAddress);
  3343. WriteToBuffer(&buffer, &n, _u(" (&NativeCodeData)"));
  3344. break;
  3345. case AddrOpndKindWriteBarrierCardTable:
  3346. DumpAddress(address, printToConsole, skipMaskedAddress);
  3347. WriteToBuffer(&buffer, &n, _u(" (&WriteBarrierCardTable)"));
  3348. break;
  3349. default:
  3350. DumpAddress(address, printToConsole, skipMaskedAddress);
  3351. if ((intptr_t)address == func->GetThreadContextInfo()->GetNullFrameDisplayAddr())
  3352. {
  3353. WriteToBuffer(&buffer, &n, _u(" (NullFrameDisplay)"));
  3354. }
  3355. else if ((intptr_t)address == func->GetThreadContextInfo()->GetStrictNullFrameDisplayAddr())
  3356. {
  3357. WriteToBuffer(&buffer, &n, _u(" (StrictNullFrameDisplay)"));
  3358. }
  3359. else if ((intptr_t)address == func->GetScriptContextInfo()->GetNumberAllocatorAddr())
  3360. {
  3361. WriteToBuffer(&buffer, &n, _u(" (NumberAllocator)"));
  3362. }
  3363. else if ((intptr_t)address == func->GetScriptContextInfo()->GetRecyclerAddr())
  3364. {
  3365. WriteToBuffer(&buffer, &n, _u(" (Recycler)"));
  3366. }
  3367. else if (func->GetWorkItem()->Type() == JsFunctionType && (intptr_t)address == func->GetWorkItem()->GetCallsCountAddress())
  3368. {
  3369. WriteToBuffer(&buffer, &n, _u(" (&CallCount)"));
  3370. }
  3371. else if ((intptr_t)address == func->GetThreadContextInfo()->GetImplicitCallFlagsAddr())
  3372. {
  3373. WriteToBuffer(&buffer, &n, _u(" (&ImplicitCallFlags)"));
  3374. }
  3375. else if ((intptr_t)address == func->GetThreadContextInfo()->GetDisableImplicitFlagsAddr())
  3376. {
  3377. WriteToBuffer(&buffer, &n, _u(" (&DisableImplicitCallFlags)"));
  3378. }
  3379. else if ((intptr_t)address == func->GetThreadContextInfo()->GetThreadStackLimitAddr())
  3380. {
  3381. WriteToBuffer(&buffer, &n, _u(" (&StackLimit)"));
  3382. }
  3383. else if (func->CanAllocInPreReservedHeapPageSegment() &&
  3384. #if ENABLE_OOP_NATIVE_CODEGEN
  3385. (func->IsOOPJIT()
  3386. ? func->GetOOPThreadContext()->GetPreReservedSectionAllocator()->IsPreReservedEndAddress(address)
  3387. : func->GetInProcThreadContext()->GetPreReservedVirtualAllocator()->IsPreReservedEndAddress(address)
  3388. )
  3389. #else
  3390. func->GetInProcThreadContext()->GetPreReservedVirtualAllocator()->IsPreReservedEndAddress(address)
  3391. #endif
  3392. )
  3393. {
  3394. WriteToBuffer(&buffer, &n, _u(" (PreReservedCodeSegmentEnd)"));
  3395. }
  3396. else if ((intptr_t)address == func->GetScriptContextInfo()->GetSideEffectsAddr())
  3397. {
  3398. WriteToBuffer(&buffer, &n, _u(" (&OptimizationOverrides_SideEffects)"));
  3399. }
  3400. else if ((intptr_t)address == func->GetScriptContextInfo()->GetArraySetElementFastPathVtableAddr())
  3401. {
  3402. WriteToBuffer(&buffer, &n, _u(" (&OptimizationOverrides_ArraySetElementFastPathVtable)"));
  3403. }
  3404. else if ((intptr_t)address == func->GetScriptContextInfo()->GetIntArraySetElementFastPathVtableAddr())
  3405. {
  3406. WriteToBuffer(&buffer, &n, _u(" (&OptimizationOverrides_IntArraySetElementFastPathVtable)"));
  3407. }
  3408. else if ((intptr_t)address == func->GetScriptContextInfo()->GetFloatArraySetElementFastPathVtableAddr())
  3409. {
  3410. WriteToBuffer(&buffer, &n, _u(" (&OptimizationOverrides_FloatArraySetElementFastPathVtable)"));
  3411. }
  3412. else
  3413. {
  3414. WriteToBuffer(&buffer, &n, _u(" (Unknown)"));
  3415. }
  3416. }
  3417. }
  3418. else
  3419. {
  3420. WriteToBuffer(&buffer, &n, _u("(NULL)"));
  3421. }
  3422. }
  3423. /**
  3424. GetAddrDescription
  3425. Determine the type of the address and place at most <count> wide chars of the
  3426. description into <description>.
  3427. Force null termination of <description>.
  3428. @param description
  3429. A buffer which will hold the description.
  3430. @param count
  3431. The maximum number of characters that should be returned in <description>.
  3432. @param AsmDumpMode
  3433. @param func
  3434. */
  3435. void
  3436. Opnd::GetAddrDescription(__out_ecount(count) char16 *const description, const size_t count, bool AsmDumpMode,
  3437. bool printToConsole, Func *func)
  3438. {
  3439. char16 *buffer = description;
  3440. size_t n = count;
  3441. IR::AddrOpnd * addrOpnd = this->AsAddrOpnd();
  3442. Js::Var address;
  3443. bool isEncoded = false;
  3444. if (addrOpnd->decodedValue != 0)
  3445. {
  3446. address = addrOpnd->decodedValue;
  3447. isEncoded = true;
  3448. }
  3449. else
  3450. {
  3451. address = addrOpnd->m_address;
  3452. }
  3453. GetAddrDescription(description, count, address, addrOpnd->GetAddrOpndKind(), AsmDumpMode, printToConsole, func);
  3454. if (isEncoded)
  3455. {
  3456. if (AsmDumpMode)
  3457. {
  3458. WriteToBuffer(&buffer, &n, _u(" [encoded]"));
  3459. }
  3460. else
  3461. {
  3462. WriteToBuffer(&buffer, &n, _u(" [encoded: 0x%08X"), addrOpnd->m_address);
  3463. }
  3464. }
  3465. description[count-1] = 0; // force null termination
  3466. }
  3467. void
  3468. Opnd::Dump()
  3469. {
  3470. this->Dump(IRDumpFlags_None, nullptr);
  3471. }
  3472. #endif
  3473. } // namespace IR