2
0

Opnd.cpp 99 KB

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