Opnd.h 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588
  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. #pragma once
  6. class Value;
  7. namespace IR {
  8. class IntConstOpnd;
  9. class FloatConstOpnd;
  10. class Simd128ConstOpnd;
  11. class HelperCallOpnd;
  12. class SymOpnd;
  13. class PropertySymOpnd;
  14. class RegOpnd;
  15. class ArrayRegOpnd;
  16. class AddrOpnd;
  17. class IndirOpnd;
  18. class LabelOpnd;
  19. class MemRefOpnd;
  20. class RegBVOpnd;
  21. enum OpndKind : BYTE {
  22. OpndKindInvalid,
  23. OpndKindIntConst,
  24. OpndKindFloatConst,
  25. OpndKindSimd128Const,
  26. OpndKindHelperCall,
  27. OpndKindSym,
  28. OpndKindReg,
  29. OpndKindAddr,
  30. OpndKindIndir,
  31. OpndKindLabel,
  32. OpndKindMemRef,
  33. OpndKindRegBV
  34. };
  35. enum AddrOpndKind : BYTE {
  36. // The following address kinds are safe for relocatable JIT and regular
  37. // JIT
  38. AddrOpndKindConstant,
  39. AddrOpndKindConstantVar, // a constant var value (null or tagged int)
  40. // NOTE: None of the following address kinds should be generated directly
  41. // or you WILL break relocatable JIT code. Each kind has a helper that
  42. // will generate correct code for relocatable code & non-relocatable code.
  43. // The only exception is places where it is KNOWN that we will never
  44. // generate the code in relocatable JIT.
  45. // use LoadScriptContextOpnd
  46. AddrOpndKindDynamicScriptContext,
  47. // use LoadVTableValueOpnd
  48. AddrOpndKindDynamicVtable,
  49. // use LoadLibraryValueOpnd
  50. AddrOpndKindDynamicCharStringCache,
  51. // use appropriate helper
  52. AddrOpndKindDynamicMisc,
  53. // no profiling in dynamic JIT
  54. AddrOpndKindDynamicFunctionBody,
  55. // use LoadRuntimeInlineCacheOpnd for runtime caches,
  56. // in relocatable JIT polymorphic inline caches aren't generated and can
  57. // be referenced directly (for now)
  58. AddrOpndKindDynamicInlineCache,
  59. // no bailouts in dynamic JIT
  60. AddrOpndKindDynamicBailOutRecord,
  61. // use appropriate helper
  62. AddrOpndKindDynamicVar,
  63. AddrOpndKindDynamicType,
  64. AddrOpndKindDynamicTypeHandler,
  65. AddrOpndKindDynamicFrameDisplay,
  66. AddrOpndKindDynamicGuardValueRef,
  67. AddrOpndKindDynamicArrayCallSiteInfo,
  68. AddrOpndKindDynamicFunctionBodyWeakRef,
  69. AddrOpndKindDynamicObjectTypeRef,
  70. AddrOpndKindDynamicTypeCheckGuard,
  71. AddrOpndKindDynamicRecyclerAllocatorEndAddressRef,
  72. AddrOpndKindDynamicRecyclerAllocatorFreeListRef,
  73. AddrOpndKindDynamicBailOutKindRef,
  74. AddrOpndKindDynamicAuxSlotArrayRef,
  75. AddrOpndKindDynamicPropertySlotRef,
  76. AddrOpndKindDynamicFunctionEnvironmentRef,
  77. AddrOpndKindDynamicIsInstInlineCacheFunctionRef,
  78. AddrOpndKindDynamicIsInstInlineCacheTypeRef,
  79. AddrOpndKindDynamicIsInstInlineCacheResultRef,
  80. AddrOpndKindSz,
  81. AddrOpndKindDynamicFloatRef,
  82. AddrOpndKindDynamicDoubleRef,
  83. };
  84. ///---------------------------------------------------------------------------
  85. ///
  86. /// class Opnd
  87. ///
  88. /// IntConstOpnd ; int values
  89. /// FLoatConstOpnd ; float values
  90. /// HelperCallOpnd ; lib helper address (more convenient dumps than AddrOpnd)
  91. /// SymOpnd ; stack symbol operand (not enregistered)
  92. /// RegOpnd ; register operand
  93. /// AddrOpnd ; address or var operand (includes TaggedInt's)
  94. /// IndirOpnd ; indirections operand (also used for JS array references)
  95. /// LabelOpnd ; label operand
  96. /// MemRefOpnd ; direct memory reference at a given memory address.
  97. /// RegBVOpnd ; unsigned int bit field used to denote a bit field vector. Example: Registers to push in STM.
  98. ///
  99. ///---------------------------------------------------------------------------
  100. class Opnd
  101. {
  102. protected:
  103. Opnd() :
  104. m_inUse(false),
  105. m_isDead(false),
  106. m_isValueTypeFixed(false),
  107. canStoreTemp(false),
  108. isDiagHelperCallOpnd(false),
  109. isPropertySymOpnd(false)
  110. {
  111. #if DBG
  112. isFakeDst = false;
  113. #endif
  114. m_kind = (OpndKind)0;
  115. }
  116. Opnd(const Opnd& oldOpnd) :
  117. m_type(oldOpnd.m_type),
  118. m_isDead(false),
  119. m_inUse(false),
  120. m_isValueTypeFixed(false),
  121. canStoreTemp(oldOpnd.canStoreTemp)
  122. {
  123. #if DBG
  124. isFakeDst = false;
  125. #endif
  126. m_kind = oldOpnd.m_kind;
  127. }
  128. public:
  129. bool IsConstOpnd() const;
  130. bool IsImmediateOpnd() const;
  131. bool IsMemoryOpnd() const;
  132. bool IsIntConstOpnd() const;
  133. IntConstOpnd * AsIntConstOpnd();
  134. bool IsFloatConstOpnd() const;
  135. FloatConstOpnd * AsFloatConstOpnd();
  136. bool IsSimd128ConstOpnd() const;
  137. Simd128ConstOpnd * AsSimd128ConstOpnd();
  138. bool IsHelperCallOpnd() const;
  139. HelperCallOpnd * AsHelperCallOpnd();
  140. bool IsSymOpnd() const;
  141. SymOpnd * AsSymOpnd();
  142. PropertySymOpnd * AsPropertySymOpnd();
  143. bool IsRegOpnd() const;
  144. const RegOpnd * AsRegOpnd() const;
  145. RegOpnd * AsRegOpnd();
  146. bool IsAddrOpnd() const;
  147. AddrOpnd * AsAddrOpnd();
  148. bool IsIndirOpnd() const;
  149. IndirOpnd * AsIndirOpnd();
  150. bool IsLabelOpnd() const;
  151. LabelOpnd * AsLabelOpnd();
  152. bool IsMemRefOpnd() const;
  153. MemRefOpnd * AsMemRefOpnd();
  154. bool IsRegBVOpnd() const;
  155. RegBVOpnd * AsRegBVOpnd();
  156. OpndKind GetKind() const;
  157. Opnd * Copy(Func *func);
  158. Opnd * CloneDef(Func *func);
  159. Opnd * CloneUse(Func *func);
  160. StackSym * GetStackSym() const;
  161. Opnd * UseWithNewType(IRType type, Func * func);
  162. bool IsEqual(Opnd *opnd);
  163. void Free(Func * func);
  164. bool IsInUse() const { return m_inUse; }
  165. Opnd * Use(Func * func);
  166. void UnUse();
  167. IRType GetType() const { return this->m_type; }
  168. void SetType(IRType type) { this->m_type = type; }
  169. bool IsSigned() const { return IRType_IsSignedInt(this->m_type); }
  170. bool IsUnsigned() const { return IRType_IsUnsignedInt(this->m_type); }
  171. int GetSize() const { return TySize[this->m_type]; }
  172. bool IsInt64() const { return this->m_type == TyInt64; }
  173. bool IsInt32() const { return this->m_type == TyInt32; }
  174. bool IsUInt32() const { return this->m_type == TyUint32; }
  175. bool IsFloat32() const { return this->m_type == TyFloat32; }
  176. bool IsFloat64() const { return this->m_type == TyFloat64; }
  177. bool IsFloat() const { return this->IsFloat32() || this->IsFloat64(); }
  178. bool IsSimd128() const { return IRType_IsSimd128(this->m_type); }
  179. bool IsSimd128F4() const { return this->m_type == TySimd128F4; }
  180. bool IsSimd128I4() const { return this->m_type == TySimd128I4; }
  181. bool IsSimd128D2() const { return this->m_type == TySimd128D2; }
  182. bool IsVar() const { return this->m_type == TyVar; }
  183. bool IsTaggedInt() const;
  184. bool IsTaggedValue() const;
  185. bool IsNotNumber() const;
  186. bool IsNotInt() const;
  187. bool IsNotTaggedValue() const;
  188. bool IsWriteBarrierTriggerableValue();
  189. void SetIsDead(const bool isDead = true) { this->m_isDead = isDead; }
  190. bool GetIsDead() { return this->m_isDead; }
  191. intptr_t GetImmediateValue();
  192. BailoutConstantValue GetConstValue();
  193. bool GetIsJITOptimizedReg() const { return m_isJITOptimizedReg; }
  194. void SetIsJITOptimizedReg(bool value) { Assert(!value || !this->IsIndirOpnd()); m_isJITOptimizedReg = value; }
  195. ValueType GetValueType() const { return m_valueType; }
  196. void SetValueType(const ValueType valueType);
  197. ValueType FindProfiledValueType();
  198. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  199. virtual void DummyFunction() {} // Note needed for the VS debugger to disambiguate the different classes.
  200. void DumpValueType();
  201. static void DumpValueType(const ValueType valueType);
  202. #endif
  203. bool IsValueTypeFixed() const { return m_isValueTypeFixed; }
  204. void SetValueTypeFixed() { m_isValueTypeFixed = true; }
  205. IR::RegOpnd * FindRegUse(IR::RegOpnd *regOpnd);
  206. bool IsArgumentsObject();
  207. static IntConstOpnd *CreateUint32Opnd(const uint i, Func *const func);
  208. static IntConstOpnd *CreateProfileIdOpnd(const Js::ProfileId profileId, Func *const func);
  209. static IntConstOpnd *CreateInlineCacheIndexOpnd(const Js::InlineCacheIndex inlineCacheIndex, Func *const func);
  210. static RegOpnd *CreateFramePointerOpnd(Func *const func);
  211. public:
  212. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  213. static void DumpAddress(void *address, bool printToConsole, bool skipMaskedAddress);
  214. static void DumpFunctionInfo(_Outptr_result_buffer_(*count) wchar_t ** buffer, size_t * count, Js::FunctionInfo * info, bool printToConsole, _In_opt_z_ wchar_t const * type = nullptr);
  215. void Dump(IRDumpFlags flags, Func *func);
  216. void DumpOpndKindAddr(bool AsmDumpMode, Func *func);
  217. void DumpOpndKindMemRef(bool AsmDumpMode, Func *func);
  218. static void WriteToBuffer(_Outptr_result_buffer_(*count) wchar_t **buffer, size_t *count, const wchar_t *fmt, ...);
  219. void GetAddrDescription(__out_ecount(count) wchar_t *const description, const size_t count, bool AsmDumpMode,
  220. bool printToConsole, Func *func);
  221. static void GetAddrDescription(__out_ecount(count) wchar_t *const description, const size_t count,
  222. void * address, IR::AddrOpndKind addressKind, bool AsmDumpMode, bool printToConsole, Func *func, bool skipMaskedAddress = false);
  223. void Dump();
  224. #endif
  225. bool CanStoreTemp() const { return canStoreTemp; }
  226. void SetCanStoreTemp() { Assert(this->IsSymOpnd() || this->IsIndirOpnd()); canStoreTemp = true; }
  227. protected:
  228. ValueType m_valueType;
  229. IRType m_type;
  230. // If true, it was deemed that the value type is definite (not likely) and shouldn't be changed. This is used for NewScArray
  231. // and the store-element instructions that follow it.
  232. bool m_isValueTypeFixed:1;
  233. bool m_inUse:1;
  234. bool m_isDead:1;
  235. // This def/use of a byte code sym is not in the original byte code, don't count them in the bailout
  236. bool m_isJITOptimizedReg:1;
  237. // For SymOpnd, this bit applies to the object pointer stack sym
  238. // For IndirOpnd, this bit applies to the base operand
  239. // If this opnd is a dst, that means that the object pointer is a stack object,
  240. // and we can store temp object/number on it
  241. // If the opnd is a src, that means that the object pointer may be a stack object
  242. // so the load may be a temp object/number and we need to track its use
  243. bool canStoreTemp : 1;
  244. bool isDiagHelperCallOpnd : 1;
  245. bool isPropertySymOpnd : 1;
  246. public:
  247. #if DBG
  248. bool isFakeDst:1;
  249. #endif
  250. OpndKind m_kind;
  251. };
  252. ///---------------------------------------------------------------------------
  253. ///
  254. /// class IntConstOpnd
  255. ///
  256. ///---------------------------------------------------------------------------
  257. class IntConstOpnd sealed : public Opnd
  258. {
  259. public:
  260. static IntConstOpnd * New(IntConstType value, IRType type, Func *func, bool dontEncode = false);
  261. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  262. static IntConstOpnd * New(IntConstType value, IRType type, const wchar_t * name, Func *func, bool dontEncode = false);
  263. #endif
  264. public:
  265. //Note: type OpndKindIntConst
  266. IntConstOpnd * CopyInternal(Func *func);
  267. bool IsEqualInternal(Opnd *opnd);
  268. void FreeInternal(Func * func) ;
  269. public:
  270. bool m_dontEncode; // Setting this to true turns off XOR encoding for this constant. Only set this on
  271. // constants not controllable by the user.
  272. IntConstType GetValue()
  273. {
  274. return m_value;
  275. }
  276. void IncrValue(IntConstType by)
  277. {
  278. SetValue(m_value + by);
  279. }
  280. void DecrValue(IntConstType by)
  281. {
  282. SetValue(m_value - by);
  283. }
  284. void SetValue(IntConstType value);
  285. int32 AsInt32();
  286. uint32 AsUint32();
  287. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  288. IntConstType decodedValue; // FIXME (t-doilij) set ENABLE_IR_VIEWER blocks where this is set
  289. wchar_t const * name; // FIXME (t-doilij) set ENABLE_IR_VIEWER blocks where this is set
  290. #endif
  291. private:
  292. IntConstType m_value;
  293. };
  294. ///---------------------------------------------------------------------------
  295. ///
  296. /// class FloatConstOpnd
  297. ///
  298. ///---------------------------------------------------------------------------
  299. class FloatConstOpnd: public Opnd
  300. {
  301. public:
  302. static FloatConstOpnd * New(FloatConstType value, IRType type, Func *func);
  303. static FloatConstOpnd * New(Js::Var floatVar, IRType type, Func *func);
  304. public:
  305. //Note: type OpndKindFloatConst
  306. FloatConstOpnd *CopyInternal(Func *func);
  307. bool IsEqualInternal(Opnd *opnd);
  308. void FreeInternal(Func * func);
  309. AddrOpnd *GetAddrOpnd(Func *func, bool dontEncode = false);
  310. public:
  311. FloatConstType m_value;
  312. private:
  313. #if !FLOATVAR
  314. Js::Var m_number;
  315. #endif
  316. };
  317. class Simd128ConstOpnd sealed : public Opnd
  318. {
  319. public:
  320. static Simd128ConstOpnd * New(AsmJsSIMDValue value, IRType type, Func *func);
  321. public:
  322. Simd128ConstOpnd * CopyInternal(Func *func);
  323. bool IsEqualInternal(Opnd *opnd);
  324. void FreeInternal(Func * func);
  325. public:
  326. AsmJsSIMDValue m_value;
  327. };
  328. ///---------------------------------------------------------------------------
  329. ///
  330. /// class HelperCallOpnd
  331. ///
  332. ///---------------------------------------------------------------------------
  333. class HelperCallOpnd: public Opnd
  334. {
  335. public:
  336. static HelperCallOpnd * New(JnHelperMethod fnHelper, Func *func);
  337. protected:
  338. void Init(JnHelperMethod fnHelper);
  339. public:
  340. //Note type : OpndKindHelperCall
  341. HelperCallOpnd *CopyInternal(Func *func);
  342. bool IsEqualInternal(Opnd *opnd);
  343. void FreeInternal(Func * func);
  344. bool IsDiagHelperCallOpnd() const
  345. {
  346. Assert(this->DbgIsDiagHelperCallOpnd() == isDiagHelperCallOpnd);
  347. return isDiagHelperCallOpnd;
  348. }
  349. public:
  350. JnHelperMethod m_fnHelper;
  351. #if DBG
  352. private:
  353. virtual bool DbgIsDiagHelperCallOpnd() const { return false; }
  354. #endif
  355. };
  356. ///---------------------------------------------------------------------------
  357. ///
  358. /// class DiagHelperCallOpnd
  359. /// Used in debug mode (Fast F12) for wrapping original helper method with try-catch wrapper.
  360. ///
  361. ///---------------------------------------------------------------------------
  362. class DiagHelperCallOpnd: public HelperCallOpnd
  363. {
  364. public:
  365. static DiagHelperCallOpnd * New(JnHelperMethod fnHelper, Func *func, int argCount);
  366. public:
  367. DiagHelperCallOpnd *CopyInternalSub(Func *func);
  368. bool IsEqualInternalSub(Opnd *opnd);
  369. public:
  370. int m_argCount;
  371. #if DBG
  372. private:
  373. virtual bool DbgIsDiagHelperCallOpnd() const override { return true; }
  374. #endif
  375. };
  376. ///---------------------------------------------------------------------------
  377. ///
  378. /// class SymOpnd
  379. ///
  380. ///---------------------------------------------------------------------------
  381. class SymOpnd: public Opnd
  382. {
  383. public:
  384. static SymOpnd * New(Sym *sym, IRType type, Func *func);
  385. static SymOpnd * New(Sym *sym, uint32 offset, IRType type, Func *func);
  386. public:
  387. // Note type: OpndKindSym
  388. SymOpnd * CopyInternal(Func *func);
  389. SymOpnd * CloneDefInternal(Func *func);
  390. SymOpnd * CloneUseInternal(Func *func);
  391. StackSym * GetStackSymInternal() const;
  392. bool IsEqualInternal(Opnd *opnd);
  393. void FreeInternal(Func * func);
  394. bool IsPropertySymOpnd() const
  395. {
  396. Assert(this->DbgIsPropertySymOpnd() == this->isPropertySymOpnd);
  397. return isPropertySymOpnd;
  398. }
  399. public:
  400. Sym * m_sym;
  401. uint32 m_offset;
  402. private:
  403. #if DBG
  404. virtual bool DbgIsPropertySymOpnd() const { return false; }
  405. #endif
  406. private:
  407. ValueType propertyOwnerValueType;
  408. public:
  409. ValueType GetPropertyOwnerValueType() const
  410. {
  411. return propertyOwnerValueType;
  412. }
  413. void SetPropertyOwnerValueType(const ValueType valueType)
  414. {
  415. propertyOwnerValueType = valueType;
  416. }
  417. RegOpnd *CreatePropertyOwnerOpnd(Func *const func) const;
  418. };
  419. class PropertySymOpnd sealed : public SymOpnd
  420. {
  421. protected:
  422. PropertySymOpnd() : SymOpnd() {}
  423. PropertySymOpnd(SymOpnd* symOpnd) : SymOpnd(*symOpnd) {}
  424. public:
  425. static PropertySymOpnd * New(PropertySym *propertySym, uint inlineCacheIndex, IRType type, Func *func);
  426. public:
  427. PropertySymOpnd * CopyCommon(Func *func);
  428. PropertySymOpnd * CopyWithoutFlowSensitiveInfo(Func *func);
  429. PropertySymOpnd * CopyForTypeCheckOnly(Func *func);
  430. PropertySymOpnd * CopyInternalSub(Func *func);
  431. PropertySymOpnd * CloneDefInternalSub(Func *func);
  432. PropertySymOpnd * CloneUseInternalSub(Func *func);
  433. void Init(uint inlineCacheIndex, Func *func);
  434. private:
  435. static PropertySymOpnd * New(PropertySym *propertySym, IRType type, Func *func);
  436. void Init(uint inlineCacheIndex, Js::InlineCache * runtimeInlineCache, Js::PolymorphicInlineCache * runtimePolymorphicInlineCache, Js::ObjTypeSpecFldInfo* objTypeSpecFldInfo, byte polyCacheUtil);
  437. #if DBG
  438. virtual bool DbgIsPropertySymOpnd() const override { return true; }
  439. #endif
  440. public:
  441. Js::InlineCacheIndex m_inlineCacheIndex;
  442. Js::InlineCache* m_runtimeInlineCache;
  443. Js::PolymorphicInlineCache* m_runtimePolymorphicInlineCache;
  444. private:
  445. Js::ObjTypeSpecFldInfo* objTypeSpecFldInfo;
  446. public:
  447. Js::Type* finalType;
  448. BVSparse<JitArenaAllocator>* guardedPropOps;
  449. BVSparse<JitArenaAllocator>* writeGuards;
  450. byte m_polyCacheUtil;
  451. private:
  452. bool usesAuxSlot : 1;
  453. Js::PropertyIndex slotIndex;
  454. uint16 checkedTypeSetIndex;
  455. public:
  456. union
  457. {
  458. struct
  459. {
  460. bool isTypeCheckOnly: 1;
  461. // Note that even usesFixedValue cannot live on ObjTypeSpecFldInfo, because we may share a cache between
  462. // e.g. Object.prototype and new Object(), and only the latter actually uses the fixed value, even though both have it.
  463. bool usesFixedValue: 1;
  464. union
  465. {
  466. struct
  467. {
  468. bool isTypeCheckSeqCandidate: 1;
  469. bool typeAvailable: 1;
  470. bool typeDead: 1;
  471. bool typeChecked: 1;
  472. bool initialTypeChecked: 1;
  473. bool mustDoMonoCheck: 1;
  474. bool typeMismatch: 1;
  475. bool writeGuardChecked: 1;
  476. };
  477. uint8 typeCheckSeqFlags;
  478. };
  479. };
  480. uint16 objTypeSpecFlags;
  481. };
  482. public:
  483. StackSym * GetObjectSym() const { return this->m_sym->AsPropertySym()->m_stackSym; };
  484. bool HasObjectTypeSym() const { return this->m_sym->AsPropertySym()->HasObjectTypeSym(); };
  485. StackSym * GetObjectTypeSym() const { return this->m_sym->AsPropertySym()->GetObjectTypeSym(); };
  486. PropertySym* GetPropertySym() const { return this->m_sym->AsPropertySym(); }
  487. void TryDisableRuntimePolymorphicCache()
  488. {
  489. if (this->m_runtimePolymorphicInlineCache && (this->m_polyCacheUtil < PolymorphicInlineCacheUtilizationThreshold))
  490. {
  491. this->m_runtimePolymorphicInlineCache = nullptr;
  492. }
  493. }
  494. bool HasObjTypeSpecFldInfo() const
  495. {
  496. return this->objTypeSpecFldInfo != nullptr;
  497. }
  498. void SetObjTypeSpecFldInfo(Js::ObjTypeSpecFldInfo *const objTypeSpecFldInfo)
  499. {
  500. this->objTypeSpecFldInfo = objTypeSpecFldInfo;
  501. // The following information may change in a flow-based manner, and an ObjTypeSpecFldInfo is shared among several
  502. // PropertySymOpnds, so copy the information to the opnd
  503. if(!objTypeSpecFldInfo)
  504. {
  505. usesAuxSlot = false;
  506. slotIndex = 0;
  507. return;
  508. }
  509. usesAuxSlot = objTypeSpecFldInfo->UsesAuxSlot();
  510. slotIndex = objTypeSpecFldInfo->GetSlotIndex();
  511. }
  512. void TryResetObjTypeSpecFldInfo()
  513. {
  514. if (this->ShouldResetObjTypeSpecFldInfo())
  515. {
  516. SetObjTypeSpecFldInfo(nullptr);
  517. }
  518. }
  519. bool ShouldResetObjTypeSpecFldInfo()
  520. {
  521. // If an objTypeSpecFldInfo was created just for the purpose of polymorphic inlining but didn't get used for the same (for some reason or the other), and the polymorphic cache it was created from, wasn't equivalent,
  522. // we should null out this info on the propertySymOpnd so that assumptions downstream around equivalent object type spec still hold.
  523. if (HasObjTypeSpecFldInfo() && IsPoly() && (DoesntHaveEquivalence() || !IsLoadedFromProto()))
  524. {
  525. return true;
  526. }
  527. return false;
  528. }
  529. Js::ObjTypeSpecFldInfo* GetObjTypeSpecInfo() const
  530. {
  531. return this->objTypeSpecFldInfo;
  532. }
  533. uint GetObjTypeSpecFldId() const
  534. {
  535. Assert(HasObjTypeSpecFldInfo());
  536. return this->objTypeSpecFldInfo->GetObjTypeSpecFldId();
  537. }
  538. bool IsMono() const
  539. {
  540. return HasObjTypeSpecFldInfo() && this->objTypeSpecFldInfo->IsMono();
  541. }
  542. bool IsPoly() const
  543. {
  544. return HasObjTypeSpecFldInfo() && this->objTypeSpecFldInfo->IsPoly();
  545. }
  546. bool HasEquivalentTypeSet() const
  547. {
  548. return HasObjTypeSpecFldInfo() && this->objTypeSpecFldInfo->HasEquivalentTypeSet();
  549. }
  550. bool DoesntHaveEquivalence() const
  551. {
  552. return HasObjTypeSpecFldInfo() && this->objTypeSpecFldInfo->DoesntHaveEquivalence();
  553. }
  554. bool UsesAuxSlot() const
  555. {
  556. return usesAuxSlot && HasObjTypeSpecFldInfo();
  557. }
  558. void SetUsesAuxSlot(bool value)
  559. {
  560. Assert(HasObjTypeSpecFldInfo());
  561. usesAuxSlot = value;
  562. }
  563. bool IsLoadedFromProto() const
  564. {
  565. return HasObjTypeSpecFldInfo() && this->objTypeSpecFldInfo->IsLoadedFromProto();
  566. }
  567. bool UsesAccessor() const
  568. {
  569. return HasObjTypeSpecFldInfo() && this->objTypeSpecFldInfo->UsesAccessor();
  570. }
  571. bool HasFixedValue() const
  572. {
  573. return HasObjTypeSpecFldInfo() && this->objTypeSpecFldInfo->HasFixedValue();
  574. }
  575. bool UsesFixedValue() const
  576. {
  577. return this->usesFixedValue;
  578. }
  579. void SetUsesFixedValue(bool value)
  580. {
  581. this->usesFixedValue = value;
  582. }
  583. bool MustDoMonoCheck() const
  584. {
  585. // Question: does this property access need to do a monomorphic check because of some other access
  586. // that this check protects?
  587. return this->mustDoMonoCheck;
  588. }
  589. void SetMustDoMonoCheck(bool value)
  590. {
  591. this->mustDoMonoCheck = value;
  592. }
  593. bool NeedsMonoCheck() const
  594. {
  595. Assert(HasObjTypeSpecFldInfo());
  596. return this->IsBeingAdded() || (this->HasFixedValue() && !this->IsLoadedFromProto());
  597. }
  598. bool IsBeingStored() const
  599. {
  600. return HasObjTypeSpecFldInfo() && this->objTypeSpecFldInfo->IsBeingStored();
  601. }
  602. void SetIsBeingStored(bool value)
  603. {
  604. Assert(HasObjTypeSpecFldInfo());
  605. this->objTypeSpecFldInfo->SetIsBeingStored(value);
  606. }
  607. bool IsBeingAdded() const
  608. {
  609. return HasObjTypeSpecFldInfo() && this->objTypeSpecFldInfo->IsBeingAdded();
  610. }
  611. void SetIsBeingAdded(bool value)
  612. {
  613. Assert(HasObjTypeSpecFldInfo());
  614. this->objTypeSpecFldInfo->SetIsBeingAdded(value);
  615. }
  616. bool IsRootObjectNonConfigurableField() const
  617. {
  618. return HasObjTypeSpecFldInfo() && this->objTypeSpecFldInfo->IsRootObjectNonConfigurableField();
  619. }
  620. bool IsRootObjectNonConfigurableFieldLoad() const
  621. {
  622. return HasObjTypeSpecFldInfo() && this->objTypeSpecFldInfo->IsRootObjectNonConfigurableFieldLoad();
  623. }
  624. uint16 GetSlotIndex() const
  625. {
  626. Assert(HasObjTypeSpecFldInfo());
  627. return slotIndex;
  628. }
  629. void SetSlotIndex(uint16 index)
  630. {
  631. Assert(HasObjTypeSpecFldInfo());
  632. slotIndex = index;
  633. }
  634. uint16 GetCheckedTypeSetIndex() const
  635. {
  636. Assert(HasEquivalentTypeSet());
  637. return checkedTypeSetIndex;
  638. }
  639. void SetCheckedTypeSetIndex(uint16 index)
  640. {
  641. Assert(HasEquivalentTypeSet());
  642. checkedTypeSetIndex = index;
  643. }
  644. Js::PropertyId GetPropertyId() const
  645. {
  646. Assert(HasObjTypeSpecFldInfo());
  647. return this->objTypeSpecFldInfo->GetPropertyId();
  648. }
  649. Js::DynamicObject* GetProtoObject() const
  650. {
  651. Assert(HasObjTypeSpecFldInfo());
  652. return this->objTypeSpecFldInfo->GetProtoObject();
  653. }
  654. Js::JavascriptFunction* GetFieldValueAsFixedFunction() const
  655. {
  656. Assert(HasObjTypeSpecFldInfo());
  657. return this->objTypeSpecFldInfo->GetFieldValueAsFixedFunctionIfAvailable();
  658. }
  659. Js::JavascriptFunction* GetFieldValueAsFixedFunction(uint i) const
  660. {
  661. Assert(HasObjTypeSpecFldInfo());
  662. return this->objTypeSpecFldInfo->GetFieldValueAsFixedFunctionIfAvailable(i);
  663. }
  664. Js::Var GetFieldValueAsFixedData() const
  665. {
  666. Assert(HasObjTypeSpecFldInfo());
  667. return this->objTypeSpecFldInfo->GetFieldValueAsFixedDataIfAvailable();
  668. }
  669. Js::Var GetFieldValue(uint i)
  670. {
  671. Assert(HasObjTypeSpecFldInfo());
  672. return this->objTypeSpecFldInfo->GetFieldValue(i);
  673. }
  674. Js::FixedFieldInfo* GetFixedFieldInfoArray()
  675. {
  676. Assert(HasObjTypeSpecFldInfo());
  677. return this->objTypeSpecFldInfo->GetFixedFieldInfoArray();
  678. }
  679. uint16 GetFixedFieldCount()
  680. {
  681. Assert(HasObjTypeSpecFldInfo());
  682. return this->objTypeSpecFldInfo->GetFixedFieldCount();
  683. }
  684. Js::JitTimeConstructorCache* GetCtorCache() const
  685. {
  686. Assert(HasObjTypeSpecFldInfo());
  687. return this->objTypeSpecFldInfo->GetCtorCache();
  688. }
  689. Js::PropertyGuard* GetPropertyGuard() const
  690. {
  691. Assert(HasObjTypeSpecFldInfo());
  692. return this->objTypeSpecFldInfo->GetPropertyGuard();
  693. }
  694. bool IsTypeCheckSeqCandidate() const
  695. {
  696. Assert(IsObjTypeSpecCandidate() || !this->isTypeCheckSeqCandidate);
  697. return this->isTypeCheckSeqCandidate;
  698. }
  699. void SetTypeCheckSeqCandidate(bool value)
  700. {
  701. Assert(IsObjTypeSpecCandidate() || !value);
  702. this->isTypeCheckSeqCandidate = value;
  703. }
  704. void SetTypeCheckSeqCandidateIfObjTypeSpecCandidate()
  705. {
  706. if (IsObjTypeSpecCandidate())
  707. {
  708. this->isTypeCheckSeqCandidate = true;
  709. }
  710. }
  711. bool IsTypeCheckOnly() const
  712. {
  713. return this->isTypeCheckOnly;
  714. }
  715. void SetTypeCheckOnly(bool value)
  716. {
  717. this->isTypeCheckOnly = value;
  718. }
  719. bool IsTypeAvailable() const
  720. {
  721. return this->typeAvailable;
  722. }
  723. void SetTypeAvailable(bool value)
  724. {
  725. Assert(IsTypeCheckSeqCandidate());
  726. this->typeAvailable = value;
  727. }
  728. bool IsTypeDead() const
  729. {
  730. return this->typeDead;
  731. }
  732. void SetTypeDead(bool value)
  733. {
  734. Assert(IsTypeCheckSeqCandidate());
  735. this->typeDead = value;
  736. }
  737. void SetTypeDeadIfTypeCheckSeqCandidate(bool value)
  738. {
  739. if (IsTypeCheckSeqCandidate())
  740. {
  741. this->typeDead = value;
  742. }
  743. }
  744. bool IsTypeChecked() const
  745. {
  746. return this->typeChecked;
  747. }
  748. void SetTypeChecked(bool value)
  749. {
  750. Assert(IsTypeCheckSeqCandidate());
  751. this->typeChecked = value;
  752. }
  753. bool IsInitialTypeChecked() const
  754. {
  755. return this->initialTypeChecked;
  756. }
  757. void SetInitialTypeChecked(bool value)
  758. {
  759. Assert(IsTypeCheckSeqCandidate());
  760. this->initialTypeChecked = value;
  761. }
  762. bool HasTypeMismatch() const
  763. {
  764. return this->typeMismatch;
  765. }
  766. void SetTypeMismatch(bool value)
  767. {
  768. Assert(IsTypeCheckSeqCandidate());
  769. this->typeMismatch = value;
  770. }
  771. bool IsWriteGuardChecked() const
  772. {
  773. return this->writeGuardChecked;
  774. }
  775. void SetWriteGuardChecked(bool value)
  776. {
  777. Assert(IsTypeCheckSeqCandidate());
  778. this->writeGuardChecked = value;
  779. }
  780. uint16 GetObjTypeSpecFlags() const
  781. {
  782. return this->objTypeSpecFlags;
  783. }
  784. void ClearObjTypeSpecFlags()
  785. {
  786. this->objTypeSpecFlags = 0;
  787. }
  788. uint16 GetTypeCheckSeqFlags() const
  789. {
  790. return this->typeCheckSeqFlags;
  791. }
  792. void ClearTypeCheckSeqFlags()
  793. {
  794. this->typeCheckSeqFlags = 0;
  795. }
  796. bool MayNeedTypeCheckProtection() const
  797. {
  798. return IsObjTypeSpecCandidate() && (IsTypeCheckSeqCandidate() || UsesFixedValue());
  799. }
  800. bool MayNeedWriteGuardProtection() const
  801. {
  802. return IsLoadedFromProto() || UsesFixedValue();
  803. }
  804. bool IsTypeCheckProtected() const
  805. {
  806. return IsTypeCheckSeqCandidate() && IsTypeChecked();
  807. }
  808. bool NeedsPrimaryTypeCheck() const
  809. {
  810. // Only indicate that we need a primary type check, i.e. the type isn't yet available but will be needed downstream.
  811. // Type checks and bailouts may still be needed in other places (e.g. loads from proto, fixed field checks, or
  812. // property adds), if a primary type check cannot protect them.
  813. Assert(MayNeedTypeCheckProtection());
  814. Assert(TypeCheckSeqBitsSetOnlyIfCandidate());
  815. return IsTypeCheckSeqCandidate() && !IsTypeDead() && !IsTypeChecked() && !HasTypeMismatch();
  816. }
  817. bool NeedsLocalTypeCheck() const
  818. {
  819. Assert(MayNeedTypeCheckProtection());
  820. Assert(TypeCheckSeqBitsSetOnlyIfCandidate());
  821. // Indicate whether this operation needs a type check for its own sake, since the type is dead and no downstream
  822. // operations require the type to be checked.
  823. return !PHASE_OFF1(Js::ObjTypeSpecIsolatedFldOpsPhase) &&
  824. IsTypeCheckSeqCandidate() && IsTypeDead() && !IsTypeCheckOnly() && !IsTypeChecked() && !HasTypeMismatch();
  825. }
  826. bool NeedsWriteGuardTypeCheck() const
  827. {
  828. Assert(MayNeedTypeCheckProtection());
  829. Assert(TypeCheckSeqBitsSetOnlyIfCandidate());
  830. // Type has been checked but property might have been written to since then.
  831. return !IsTypeCheckOnly() && !NeedsPrimaryTypeCheck() && IsTypeChecked() && !IsWriteGuardChecked();
  832. }
  833. bool NeedsLoadFromProtoTypeCheck() const
  834. {
  835. Assert(MayNeedTypeCheckProtection());
  836. Assert(TypeCheckSeqBitsSetOnlyIfCandidate());
  837. // Proto cache, where type has been checked but property might have been written to since then.
  838. return !IsTypeCheckOnly() && !NeedsPrimaryTypeCheck() && IsLoadedFromProto() && NeedsWriteGuardTypeCheck();
  839. }
  840. bool NeedsAddPropertyTypeCheck() const
  841. {
  842. Assert(MayNeedTypeCheckProtection());
  843. Assert(TypeCheckSeqBitsSetOnlyIfCandidate());
  844. // A property cannot become read-only without an explicit or implicit call (at least Object.defineProperty is needed), so if this
  845. // operation is protected by a primary type check upstream, there is no need for an additional local type check.
  846. return false;
  847. }
  848. bool NeedsCheckFixedFieldTypeCheck() const
  849. {
  850. Assert(MayNeedTypeCheckProtection());
  851. Assert(TypeCheckSeqBitsSetOnlyIfCandidate());
  852. return !IsTypeCheckOnly() && !NeedsPrimaryTypeCheck() && UsesFixedValue() && (!IsTypeChecked() || NeedsWriteGuardTypeCheck());
  853. }
  854. bool NeedsTypeCheck() const
  855. {
  856. return NeedsPrimaryTypeCheck() || NeedsLocalTypeCheck() ||
  857. NeedsLoadFromProtoTypeCheck() || NeedsAddPropertyTypeCheck() || NeedsCheckFixedFieldTypeCheck();
  858. }
  859. bool NeedsTypeCheckAndBailOut() const
  860. {
  861. return NeedsPrimaryTypeCheck() || (PHASE_ON1(Js::ObjTypeSpecIsolatedFldOpsWithBailOutPhase) && NeedsLocalTypeCheck()) || NeedsCheckFixedFieldTypeCheck();
  862. }
  863. // Is the instruction involving this operand optimized with a direct slot load or store? In other words, is it guarded
  864. // by a type check, either as part of the type check sequence, or explicitly on this instruction.
  865. bool IsObjTypeSpecOptimized() const
  866. {
  867. return MayNeedTypeCheckProtection() && (NeedsTypeCheckAndBailOut() || IsTypeCheckProtected());
  868. }
  869. // May the instruction involving this operand result in an implicit call? Note, that because in dead store pass we
  870. // may choose to remove a type check and fall back on a check against a live cache, instructions that have a primary
  871. // type check may still end up with implicit call bailout. However, if we are type check protected we will never
  872. // fall back on live cache. Similarly, for fixed method checks.
  873. bool MayHaveImplicitCall() const
  874. {
  875. return !IsRootObjectNonConfigurableFieldLoad() && !UsesFixedValue() && (!IsTypeCheckSeqCandidate() || !IsTypeCheckProtected());
  876. }
  877. // Is the instruction involving this operand part of a type check sequence? This is different from IsObjTypeSpecOptimized
  878. // in that an instruction such as CheckFixedFld may require a type check even if it is not part of a type check
  879. // sequence. In this case IsObjTypeSpecOptimized() == true, but IsTypeCheckSeqParticipant() == false.
  880. bool IsTypeCheckSeqParticipant() const
  881. {
  882. Assert(IsTypeCheckSeqCandidate());
  883. return NeedsPrimaryTypeCheck() || IsTypeCheckProtected();
  884. }
  885. bool HasFinalType() const
  886. {
  887. return this->finalType != nullptr;
  888. }
  889. Js::Type * GetFinalType() const
  890. {
  891. return this->finalType;
  892. }
  893. void SetFinalType(Js::Type* type)
  894. {
  895. Assert(type != nullptr);
  896. this->finalType = type;
  897. }
  898. void ClearFinalType()
  899. {
  900. this->finalType = nullptr;
  901. }
  902. BVSparse<JitArenaAllocator>* GetGuardedPropOps()
  903. {
  904. return this->guardedPropOps;
  905. }
  906. void EnsureGuardedPropOps(JitArenaAllocator* allocator)
  907. {
  908. if (this->guardedPropOps == nullptr)
  909. {
  910. this->guardedPropOps = JitAnew(allocator, BVSparse<JitArenaAllocator>, allocator);
  911. }
  912. }
  913. void SetGuardedPropOp(uint propOpId)
  914. {
  915. Assert(this->guardedPropOps != nullptr);
  916. this->guardedPropOps->Set(propOpId);
  917. }
  918. void AddGuardedPropOps(const BVSparse<JitArenaAllocator>* propOps)
  919. {
  920. Assert(this->guardedPropOps != nullptr);
  921. this->guardedPropOps->Or(propOps);
  922. }
  923. BVSparse<JitArenaAllocator>* GetWriteGuards()
  924. {
  925. return this->writeGuards;
  926. }
  927. void SetWriteGuards(BVSparse<JitArenaAllocator>* value)
  928. {
  929. Assert(this->writeGuards == nullptr);
  930. this->writeGuards = value;
  931. }
  932. void ClearWriteGuards()
  933. {
  934. this->writeGuards = nullptr;
  935. }
  936. #if DBG
  937. bool TypeCheckSeqBitsSetOnlyIfCandidate() const
  938. {
  939. return IsTypeCheckSeqCandidate() || (!IsTypeAvailable() && !IsTypeChecked() && !IsWriteGuardChecked() && !IsTypeDead());
  940. }
  941. #endif
  942. bool IsObjTypeSpecCandidate() const
  943. {
  944. return HasObjTypeSpecFldInfo() && this->objTypeSpecFldInfo->IsObjTypeSpecCandidate();
  945. }
  946. bool IsMonoObjTypeSpecCandidate() const
  947. {
  948. return HasObjTypeSpecFldInfo() && this->objTypeSpecFldInfo->IsMonoObjTypeSpecCandidate();
  949. }
  950. bool IsPolyObjTypeSpecCandidate() const
  951. {
  952. return HasObjTypeSpecFldInfo() && this->objTypeSpecFldInfo->IsPolyObjTypeSpecCandidate();
  953. }
  954. Js::TypeId GetTypeId() const
  955. {
  956. Assert(HasObjTypeSpecFldInfo());
  957. return this->objTypeSpecFldInfo->GetTypeId();
  958. }
  959. Js::TypeId GetTypeId(uint i) const
  960. {
  961. Assert(HasObjTypeSpecFldInfo());
  962. return this->objTypeSpecFldInfo->GetTypeId(i);
  963. }
  964. Js::Type * GetType() const
  965. {
  966. Assert(HasObjTypeSpecFldInfo());
  967. return this->objTypeSpecFldInfo->GetType();
  968. }
  969. Js::Type * GetType(uint i) const
  970. {
  971. Assert(HasObjTypeSpecFldInfo());
  972. return this->objTypeSpecFldInfo->GetType(i);
  973. }
  974. bool HasInitialType() const
  975. {
  976. Assert(HasObjTypeSpecFldInfo());
  977. return this->objTypeSpecFldInfo->HasInitialType();
  978. }
  979. Js::Type * GetInitialType() const
  980. {
  981. Assert(HasObjTypeSpecFldInfo());
  982. return this->objTypeSpecFldInfo->GetInitialType();
  983. }
  984. Js::EquivalentTypeSet * GetEquivalentTypeSet() const
  985. {
  986. Assert(HasObjTypeSpecFldInfo());
  987. return this->objTypeSpecFldInfo->GetEquivalentTypeSet();
  988. }
  989. Js::Type * GetFirstEquivalentType() const
  990. {
  991. Assert(HasObjTypeSpecFldInfo());
  992. return this->objTypeSpecFldInfo->GetFirstEquivalentType();
  993. }
  994. bool IsObjectHeaderInlined() const;
  995. void UpdateSlotForFinalType();
  996. bool ChangesObjectLayout() const;
  997. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  998. const wchar_t* GetCacheLayoutString() const
  999. {
  1000. return HasObjTypeSpecFldInfo() ? this->objTypeSpecFldInfo->GetCacheLayoutString() : L"empty";
  1001. }
  1002. #endif
  1003. };
  1004. ///---------------------------------------------------------------------------
  1005. ///
  1006. /// class RegOpnd
  1007. ///
  1008. ///---------------------------------------------------------------------------
  1009. class RegOpnd : public Opnd
  1010. {
  1011. protected:
  1012. RegOpnd(StackSym *sym, RegNum reg, IRType type);
  1013. RegOpnd(const RegOpnd &other, StackSym * sym);
  1014. private:
  1015. void Initialize(StackSym *sym, RegNum reg, IRType type);
  1016. public:
  1017. static RegOpnd * New(IRType type, Func *func);
  1018. static RegOpnd * New(StackSym *sym, IRType type, Func *func);
  1019. static RegOpnd * New(StackSym *sym, RegNum reg, IRType type, Func *func);
  1020. public:
  1021. bool IsArrayRegOpnd() const
  1022. {
  1023. Assert(m_isArrayRegOpnd == DbgIsArrayRegOpnd());
  1024. Assert(!m_isArrayRegOpnd || m_valueType.IsAnyOptimizedArray());
  1025. return m_isArrayRegOpnd;
  1026. }
  1027. ArrayRegOpnd * AsArrayRegOpnd();
  1028. RegNum GetReg() const;
  1029. void SetReg(RegNum reg);
  1030. //Note type: OpndKindReg
  1031. RegOpnd * CopyInternal(Func *func);
  1032. RegOpnd * CloneDefInternal(Func *func);
  1033. RegOpnd * CloneUseInternal(Func *func);
  1034. StackSym * GetStackSymInternal() const;
  1035. static StackSym * TryGetStackSym(Opnd *const opnd);
  1036. bool IsEqualInternal(Opnd *opnd);
  1037. void FreeInternal(Func * func);
  1038. bool IsSameReg(Opnd *opnd);
  1039. bool IsSameRegUntyped(Opnd *opnd);
  1040. #if DBG
  1041. void FreezeSymValue() { m_symValueFrozen = true; }
  1042. bool IsSymValueFrozen() const { return m_symValueFrozen; }
  1043. virtual bool DbgIsArrayRegOpnd() const { return false; }
  1044. #endif
  1045. private:
  1046. RegOpnd * CopyInternal(StackSym * sym, Func * func);
  1047. public:
  1048. StackSym * m_sym;
  1049. bool m_isTempLastUse:1;
  1050. bool m_isCallArg:1;
  1051. bool m_dontDeadStore: 1;
  1052. bool m_fgPeepTmp: 1;
  1053. bool m_wasNegativeZeroPreventedByBailout : 1;
  1054. bool m_isArrayRegOpnd : 1;
  1055. #if DBG
  1056. private:
  1057. bool m_symValueFrozen : 1; // if true, prevents this operand from being used as the destination operand in an instruction
  1058. #endif
  1059. private:
  1060. RegNum m_reg;
  1061. PREVENT_COPY(RegOpnd);
  1062. };
  1063. ///---------------------------------------------------------------------------
  1064. ///
  1065. /// class ArrayRegOpnd
  1066. ///
  1067. ///---------------------------------------------------------------------------
  1068. class ArrayRegOpnd sealed : public RegOpnd
  1069. {
  1070. private:
  1071. StackSym *headSegmentSym;
  1072. StackSym *headSegmentLengthSym;
  1073. StackSym *lengthSym;
  1074. const bool eliminatedLowerBoundCheck, eliminatedUpperBoundCheck;
  1075. protected:
  1076. ArrayRegOpnd(StackSym *const arraySym, const ValueType valueType, StackSym *const headSegmentSym, StackSym *const headSegmentLengthSym, StackSym *const lengthSym, const bool eliminatedLowerBoundCheck, const bool eliminatedUpperBoundCheck);
  1077. ArrayRegOpnd(const RegOpnd &other, StackSym *const arraySym, const ValueType valueType, StackSym *const headSegmentSym, StackSym *const headSegmentLengthSym, StackSym *const lengthSym, const bool eliminatedLowerBoundCheck, const bool eliminatedUpperBoundCheck);
  1078. public:
  1079. static ArrayRegOpnd *New(StackSym *const arraySym, const ValueType valueType, StackSym *const headSegmentSym, StackSym *const headSegmentLengthSym, StackSym *const lengthSym, const bool eliminatedLowerBoundCheck, const bool eliminatedUpperBoundCheck, Func *const func);
  1080. static ArrayRegOpnd *New(const RegOpnd *const other, const ValueType valueType, StackSym *const headSegmentSym, StackSym *const headSegmentLengthSym, StackSym *const lengthSym, const bool eliminatedLowerBoundCheck, const bool eliminatedUpperBoundCheck, Func *const func);
  1081. public:
  1082. #if DBG
  1083. virtual bool DbgIsArrayRegOpnd() const { return true; }
  1084. #endif
  1085. StackSym *HeadSegmentSym() const
  1086. {
  1087. return headSegmentSym;
  1088. }
  1089. void RemoveHeadSegmentSym()
  1090. {
  1091. headSegmentSym = nullptr;
  1092. }
  1093. StackSym *HeadSegmentLengthSym() const
  1094. {
  1095. return headSegmentLengthSym;
  1096. }
  1097. void RemoveHeadSegmentLengthSym()
  1098. {
  1099. headSegmentLengthSym = nullptr;
  1100. }
  1101. StackSym *LengthSym() const
  1102. {
  1103. // For typed arrays, the head segment length is the same as the array length
  1104. Assert(!(m_valueType.IsLikelyTypedArray() && !m_valueType.IsOptimizedTypedArray()));
  1105. return m_valueType.IsLikelyTypedArray() ? HeadSegmentLengthSym() : lengthSym;
  1106. }
  1107. void RemoveLengthSym()
  1108. {
  1109. Assert(m_valueType.IsArray());
  1110. lengthSym = nullptr;
  1111. }
  1112. bool EliminatedLowerBoundCheck() const
  1113. {
  1114. return eliminatedLowerBoundCheck;
  1115. }
  1116. bool EliminatedUpperBoundCheck() const
  1117. {
  1118. return eliminatedUpperBoundCheck;
  1119. }
  1120. public:
  1121. RegOpnd *CopyAsRegOpnd(Func *func);
  1122. ArrayRegOpnd * CopyInternalSub(Func *func);
  1123. ArrayRegOpnd *CloneDefInternalSub(Func *func);
  1124. ArrayRegOpnd *CloneUseInternalSub(Func *func);
  1125. private:
  1126. ArrayRegOpnd *Clone(StackSym *const arraySym, StackSym *const headSegmentSym, StackSym *const headSegmentLengthSym, StackSym *const lengthSym, Func *const func) const;
  1127. public:
  1128. void FreeInternalSub(Func *func);
  1129. // IsEqual is not overridden because this opnd still primarily represents the array sym. Equality comparisons using IsEqual
  1130. // are used to determine whether opnds should be swapped, etc. and the extra information in this class should not affect
  1131. // that behavior.
  1132. // virtual bool IsEqual(Opnd *opnd) override;
  1133. PREVENT_COPY(ArrayRegOpnd);
  1134. };
  1135. ///---------------------------------------------------------------------------
  1136. ///
  1137. /// class AddrOpnd
  1138. ///
  1139. ///---------------------------------------------------------------------------
  1140. class AddrOpnd sealed : public Opnd
  1141. {
  1142. public:
  1143. static AddrOpnd * New(Js::Var address, AddrOpndKind addrOpndKind, Func *func, bool dontEncode = false);
  1144. static AddrOpnd * NewFromNumber(double value, Func *func, bool dontEncode = false);
  1145. static AddrOpnd * NewFromNumber(int32 value, Func *func, bool dontEncode = false);
  1146. static AddrOpnd * NewFromNumber(int64 value, Func *func, bool dontEncode = false);
  1147. static AddrOpnd * NewNull(Func * func);
  1148. public:
  1149. //Note type: OpndKindAddr
  1150. AddrOpnd * CopyInternal(Func *func);
  1151. bool IsEqualInternal(Opnd *opnd);
  1152. void FreeInternal(Func * func);
  1153. bool IsDynamic() const { return addrOpndKind > AddrOpndKindConstantVar; }
  1154. bool IsVar() const { return addrOpndKind == AddrOpndKindDynamicVar || addrOpndKind == AddrOpndKindConstantVar; }
  1155. void SetEncodedValue(Js::Var address, AddrOpndKind addrOpndKind);
  1156. AddrOpndKind GetAddrOpndKind() const { return addrOpndKind; }
  1157. void SetAddress(Js::Var address, AddrOpndKind addrOpndKind);
  1158. public:
  1159. Js::Var m_address;
  1160. bool m_dontEncode: 1;
  1161. bool m_isFunction: 1;
  1162. private:
  1163. AddrOpndKind addrOpndKind;
  1164. public:
  1165. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  1166. Js::Var decodedValue; // FIXME (t-doilij) set ENABLE_IR_VIEWER blocks where this is set
  1167. #endif
  1168. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  1169. bool wasVar;
  1170. #endif
  1171. };
  1172. ///---------------------------------------------------------------------------
  1173. ///
  1174. /// class IndirOpnd
  1175. ///
  1176. ///---------------------------------------------------------------------------
  1177. class IndirOpnd: public Opnd
  1178. {
  1179. public:
  1180. static IndirOpnd * New(RegOpnd * baseOpnd, RegOpnd * indexOpnd, IRType type, Func *func);
  1181. static IndirOpnd * New(RegOpnd * baseOpnd, RegOpnd * indexOpnd, byte scale, IRType type, Func *func);
  1182. static IndirOpnd * New(RegOpnd * baseOpnd, int32 offset, IRType type, Func *func, bool dontEncode = false);
  1183. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  1184. static IndirOpnd * New(RegOpnd * baseOpnd, int32 offset, IRType type, const wchar_t *desc, Func *func, bool dontEncode = false);
  1185. #endif
  1186. public:
  1187. IndirOpnd() : Opnd(), m_baseOpnd(nullptr), m_indexOpnd(nullptr), m_offset(0), m_scale(0), m_func(nullptr), m_dontEncode(false)
  1188. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  1189. , m_desc(nullptr)
  1190. #endif
  1191. #if DBG_DUMP
  1192. , m_addrKind((IR::AddrOpndKind)-1)
  1193. #endif
  1194. {
  1195. }
  1196. ~IndirOpnd();
  1197. // Note type: OpndKindIndir
  1198. IndirOpnd * CopyInternal(Func *func);
  1199. IndirOpnd * CloneDefInternal(Func *func);
  1200. IndirOpnd * CloneUseInternal(Func *func);
  1201. bool IsEqualInternal(Opnd *opnd);
  1202. void FreeInternal(Func * func);
  1203. RegOpnd * GetBaseOpnd() const;
  1204. void SetBaseOpnd(RegOpnd *baseOpnd);
  1205. RegOpnd * UnlinkBaseOpnd();
  1206. void ReplaceBaseOpnd(RegOpnd *newBase);
  1207. RegOpnd * GetIndexOpnd();
  1208. void SetIndexOpnd(RegOpnd *indexOpnd);
  1209. RegOpnd * GetIndexOpnd() const;
  1210. RegOpnd * UnlinkIndexOpnd();
  1211. void ReplaceIndexOpnd(RegOpnd *newIndex);
  1212. int32 GetOffset() const;
  1213. void SetOffset(int32 offset, bool dontEncode = false);
  1214. byte GetScale() const;
  1215. void SetScale(byte scale);
  1216. bool TryGetIntConstIndexValue(bool trySym, IntConstType *pValue, bool *pIsNotInt);
  1217. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  1218. const wchar_t * GetDescription();
  1219. IR::AddrOpndKind GetAddrKind() const;
  1220. bool HasAddrKind() const;
  1221. void * GetOriginalAddress() const;
  1222. #endif
  1223. bool m_dontEncode;
  1224. #if DBG_DUMP
  1225. void SetAddrKind(IR::AddrOpndKind kind, void * originalAddress);
  1226. #endif
  1227. private:
  1228. RegOpnd * m_baseOpnd;
  1229. RegOpnd * m_indexOpnd;
  1230. int32 m_offset;
  1231. byte m_scale;
  1232. Func * m_func; // We need the allocator to copy the base and index...
  1233. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  1234. const wchar_t * m_desc;
  1235. #endif
  1236. #if DBG_DUMP
  1237. IR::AddrOpndKind m_addrKind; // if m_addrKind != -1, than this used to be MemRefOpnd which has the address hoisted;
  1238. void * m_originalAddress;
  1239. #endif
  1240. };
  1241. ///---------------------------------------------------------------------------
  1242. ///
  1243. /// class MemRefOpnd - represents a reference to a fixed memory location
  1244. ///
  1245. ///---------------------------------------------------------------------------
  1246. class MemRefOpnd : public Opnd
  1247. {
  1248. public:
  1249. static MemRefOpnd * New(void * pMemLoc, IRType, Func * func, AddrOpndKind addrOpndKind = AddrOpndKindDynamicMisc);
  1250. public:
  1251. // Note type: OpndKindMemRef
  1252. MemRefOpnd * CopyInternal(Func * func);
  1253. bool IsEqualInternal(Opnd *opnd);
  1254. void FreeInternal(Func * func);
  1255. void * GetMemLoc() const;
  1256. void SetMemLoc(void * pMemLoc);
  1257. IR::AddrOpndKind GetAddrKind() const;
  1258. private:
  1259. void * m_memLoc;
  1260. #if DBG_DUMP
  1261. AddrOpndKind m_addrKind;
  1262. #endif
  1263. };
  1264. //
  1265. // class LabelOpnd - represents a reference to a local code address
  1266. //
  1267. class LabelOpnd : public Opnd
  1268. {
  1269. public:
  1270. static LabelOpnd * New(LabelInstr * labelInstr, Func * func);
  1271. public:
  1272. //Note type: OpndKindLabel
  1273. LabelOpnd * CopyInternal(Func * func);
  1274. bool IsEqualInternal(Opnd * opnd);
  1275. void FreeInternal(Func * func);
  1276. LabelInstr * GetLabel() const;
  1277. void SetLabel(LabelInstr * labelInstr);
  1278. private:
  1279. LabelInstr * m_label;
  1280. };
  1281. ///---------------------------------------------------------------------------
  1282. ///
  1283. /// class Bit Field vector
  1284. ///
  1285. ///---------------------------------------------------------------------------
  1286. class RegBVOpnd: public Opnd
  1287. {
  1288. public:
  1289. static RegBVOpnd * New(BVUnit32 value, IRType type, Func *func);
  1290. public:
  1291. //Note: type: OpndKindRegBV
  1292. RegBVOpnd * CopyInternal(Func *func);
  1293. bool IsEqualInternal(Opnd *opnd);
  1294. void FreeInternal(Func * func);
  1295. BVUnit32 GetValue() const;
  1296. public:
  1297. BVUnit32 m_value;
  1298. };
  1299. class AutoReuseOpnd
  1300. {
  1301. private:
  1302. Opnd *opnd;
  1303. Func *func;
  1304. bool autoDelete;
  1305. bool wasInUse;
  1306. public:
  1307. AutoReuseOpnd() : opnd(nullptr), wasInUse(true)
  1308. {
  1309. }
  1310. AutoReuseOpnd(Opnd *const opnd, Func *const func, const bool autoDelete = true) : opnd(nullptr)
  1311. {
  1312. Initialize(opnd, func, autoDelete);
  1313. }
  1314. void Initialize(Opnd *const opnd, Func *const func, const bool autoDelete = true)
  1315. {
  1316. Assert(!this->opnd);
  1317. Assert(func);
  1318. if(!opnd)
  1319. {
  1320. // Simulate the default constructor
  1321. wasInUse = true;
  1322. return;
  1323. }
  1324. this->opnd = opnd;
  1325. wasInUse = opnd->IsInUse();
  1326. if(wasInUse)
  1327. {
  1328. return;
  1329. }
  1330. this->func = func;
  1331. this->autoDelete = autoDelete;
  1332. // Create a fake use of the opnd to enable opnd reuse during lowering. One issue is that when an unused opnd is first
  1333. // used in an instruction and the instruction is legalized, the opnd may be replaced by legalization and the original
  1334. // opnd would be freed. By creating a fake use, it forces the opnd to be copied when used by the instruction, so the
  1335. // original opnd can continue to be reused for other instructions. Typically, any opnds used during lowering in more
  1336. // than one instruction can use this class to enable opnd reuse.
  1337. opnd->Use(func);
  1338. }
  1339. ~AutoReuseOpnd()
  1340. {
  1341. if(wasInUse)
  1342. {
  1343. return;
  1344. }
  1345. if(autoDelete)
  1346. {
  1347. opnd->Free(func);
  1348. }
  1349. else
  1350. {
  1351. opnd->UnUse();
  1352. }
  1353. }
  1354. PREVENT_COPY(AutoReuseOpnd)
  1355. };
  1356. } // namespace IR