Opnd.cpp 116 KB

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