Opnd.h 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591
  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. Js::Type* monoGuardType;
  449. BVSparse<JitArenaAllocator>* guardedPropOps;
  450. BVSparse<JitArenaAllocator>* writeGuards;
  451. byte m_polyCacheUtil;
  452. private:
  453. bool usesAuxSlot : 1;
  454. Js::PropertyIndex slotIndex;
  455. uint16 checkedTypeSetIndex;
  456. public:
  457. union
  458. {
  459. struct
  460. {
  461. bool isTypeCheckOnly: 1;
  462. // Note that even usesFixedValue cannot live on ObjTypeSpecFldInfo, because we may share a cache between
  463. // e.g. Object.prototype and new Object(), and only the latter actually uses the fixed value, even though both have it.
  464. bool usesFixedValue: 1;
  465. union
  466. {
  467. struct
  468. {
  469. bool isTypeCheckSeqCandidate: 1;
  470. bool typeAvailable: 1;
  471. bool typeDead: 1;
  472. bool typeChecked: 1;
  473. bool initialTypeChecked: 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. return this->monoGuardType != nullptr;
  586. }
  587. Js::Type * GetMonoGuardType() const
  588. {
  589. return this->monoGuardType;
  590. }
  591. void SetMonoGuardType(Js::Type *type)
  592. {
  593. this->monoGuardType = type;
  594. }
  595. bool NeedsMonoCheck() const
  596. {
  597. Assert(HasObjTypeSpecFldInfo());
  598. return this->IsBeingAdded() || (this->HasFixedValue() && !this->IsLoadedFromProto());
  599. }
  600. bool IsBeingStored() const
  601. {
  602. return HasObjTypeSpecFldInfo() && this->objTypeSpecFldInfo->IsBeingStored();
  603. }
  604. void SetIsBeingStored(bool value)
  605. {
  606. Assert(HasObjTypeSpecFldInfo());
  607. this->objTypeSpecFldInfo->SetIsBeingStored(value);
  608. }
  609. bool IsBeingAdded() const
  610. {
  611. return HasObjTypeSpecFldInfo() && this->objTypeSpecFldInfo->IsBeingAdded();
  612. }
  613. void SetIsBeingAdded(bool value)
  614. {
  615. Assert(HasObjTypeSpecFldInfo());
  616. this->objTypeSpecFldInfo->SetIsBeingAdded(value);
  617. }
  618. bool IsRootObjectNonConfigurableField() const
  619. {
  620. return HasObjTypeSpecFldInfo() && this->objTypeSpecFldInfo->IsRootObjectNonConfigurableField();
  621. }
  622. bool IsRootObjectNonConfigurableFieldLoad() const
  623. {
  624. return HasObjTypeSpecFldInfo() && this->objTypeSpecFldInfo->IsRootObjectNonConfigurableFieldLoad();
  625. }
  626. uint16 GetSlotIndex() const
  627. {
  628. Assert(HasObjTypeSpecFldInfo());
  629. return slotIndex;
  630. }
  631. void SetSlotIndex(uint16 index)
  632. {
  633. Assert(HasObjTypeSpecFldInfo());
  634. slotIndex = index;
  635. }
  636. uint16 GetCheckedTypeSetIndex() const
  637. {
  638. Assert(HasEquivalentTypeSet());
  639. return checkedTypeSetIndex;
  640. }
  641. void SetCheckedTypeSetIndex(uint16 index)
  642. {
  643. Assert(HasEquivalentTypeSet());
  644. checkedTypeSetIndex = index;
  645. }
  646. Js::PropertyId GetPropertyId() const
  647. {
  648. Assert(HasObjTypeSpecFldInfo());
  649. return this->objTypeSpecFldInfo->GetPropertyId();
  650. }
  651. Js::DynamicObject* GetProtoObject() const
  652. {
  653. Assert(HasObjTypeSpecFldInfo());
  654. return this->objTypeSpecFldInfo->GetProtoObject();
  655. }
  656. Js::JavascriptFunction* GetFieldValueAsFixedFunction() const
  657. {
  658. Assert(HasObjTypeSpecFldInfo());
  659. return this->objTypeSpecFldInfo->GetFieldValueAsFixedFunctionIfAvailable();
  660. }
  661. Js::JavascriptFunction* GetFieldValueAsFixedFunction(uint i) const
  662. {
  663. Assert(HasObjTypeSpecFldInfo());
  664. return this->objTypeSpecFldInfo->GetFieldValueAsFixedFunctionIfAvailable(i);
  665. }
  666. Js::Var GetFieldValueAsFixedData() const
  667. {
  668. Assert(HasObjTypeSpecFldInfo());
  669. return this->objTypeSpecFldInfo->GetFieldValueAsFixedDataIfAvailable();
  670. }
  671. Js::Var GetFieldValue(uint i)
  672. {
  673. Assert(HasObjTypeSpecFldInfo());
  674. return this->objTypeSpecFldInfo->GetFieldValue(i);
  675. }
  676. Js::FixedFieldInfo* GetFixedFieldInfoArray()
  677. {
  678. Assert(HasObjTypeSpecFldInfo());
  679. return this->objTypeSpecFldInfo->GetFixedFieldInfoArray();
  680. }
  681. uint16 GetFixedFieldCount()
  682. {
  683. Assert(HasObjTypeSpecFldInfo());
  684. return this->objTypeSpecFldInfo->GetFixedFieldCount();
  685. }
  686. Js::JitTimeConstructorCache* GetCtorCache() const
  687. {
  688. Assert(HasObjTypeSpecFldInfo());
  689. return this->objTypeSpecFldInfo->GetCtorCache();
  690. }
  691. Js::PropertyGuard* GetPropertyGuard() const
  692. {
  693. Assert(HasObjTypeSpecFldInfo());
  694. return this->objTypeSpecFldInfo->GetPropertyGuard();
  695. }
  696. bool IsTypeCheckSeqCandidate() const
  697. {
  698. Assert(IsObjTypeSpecCandidate() || !this->isTypeCheckSeqCandidate);
  699. return this->isTypeCheckSeqCandidate;
  700. }
  701. void SetTypeCheckSeqCandidate(bool value)
  702. {
  703. Assert(IsObjTypeSpecCandidate() || !value);
  704. this->isTypeCheckSeqCandidate = value;
  705. }
  706. void SetTypeCheckSeqCandidateIfObjTypeSpecCandidate()
  707. {
  708. if (IsObjTypeSpecCandidate())
  709. {
  710. this->isTypeCheckSeqCandidate = true;
  711. }
  712. }
  713. bool IsTypeCheckOnly() const
  714. {
  715. return this->isTypeCheckOnly;
  716. }
  717. void SetTypeCheckOnly(bool value)
  718. {
  719. this->isTypeCheckOnly = value;
  720. }
  721. bool IsTypeAvailable() const
  722. {
  723. return this->typeAvailable;
  724. }
  725. void SetTypeAvailable(bool value)
  726. {
  727. Assert(IsTypeCheckSeqCandidate());
  728. this->typeAvailable = value;
  729. }
  730. bool IsTypeDead() const
  731. {
  732. return this->typeDead;
  733. }
  734. void SetTypeDead(bool value)
  735. {
  736. Assert(IsTypeCheckSeqCandidate());
  737. this->typeDead = value;
  738. }
  739. void SetTypeDeadIfTypeCheckSeqCandidate(bool value)
  740. {
  741. if (IsTypeCheckSeqCandidate())
  742. {
  743. this->typeDead = value;
  744. }
  745. }
  746. bool IsTypeChecked() const
  747. {
  748. return this->typeChecked;
  749. }
  750. void SetTypeChecked(bool value)
  751. {
  752. Assert(IsTypeCheckSeqCandidate());
  753. this->typeChecked = value;
  754. }
  755. bool IsInitialTypeChecked() const
  756. {
  757. return this->initialTypeChecked;
  758. }
  759. void SetInitialTypeChecked(bool value)
  760. {
  761. Assert(IsTypeCheckSeqCandidate());
  762. this->initialTypeChecked = value;
  763. }
  764. bool HasTypeMismatch() const
  765. {
  766. return this->typeMismatch;
  767. }
  768. void SetTypeMismatch(bool value)
  769. {
  770. Assert(IsTypeCheckSeqCandidate());
  771. this->typeMismatch = value;
  772. }
  773. bool IsWriteGuardChecked() const
  774. {
  775. return this->writeGuardChecked;
  776. }
  777. void SetWriteGuardChecked(bool value)
  778. {
  779. Assert(IsTypeCheckSeqCandidate());
  780. this->writeGuardChecked = value;
  781. }
  782. uint16 GetObjTypeSpecFlags() const
  783. {
  784. return this->objTypeSpecFlags;
  785. }
  786. void ClearObjTypeSpecFlags()
  787. {
  788. this->objTypeSpecFlags = 0;
  789. }
  790. uint16 GetTypeCheckSeqFlags() const
  791. {
  792. return this->typeCheckSeqFlags;
  793. }
  794. void ClearTypeCheckSeqFlags()
  795. {
  796. this->typeCheckSeqFlags = 0;
  797. }
  798. bool MayNeedTypeCheckProtection() const
  799. {
  800. return IsObjTypeSpecCandidate() && (IsTypeCheckSeqCandidate() || UsesFixedValue());
  801. }
  802. bool MayNeedWriteGuardProtection() const
  803. {
  804. return IsLoadedFromProto() || UsesFixedValue();
  805. }
  806. bool IsTypeCheckProtected() const
  807. {
  808. return IsTypeCheckSeqCandidate() && IsTypeChecked();
  809. }
  810. bool NeedsPrimaryTypeCheck() const
  811. {
  812. // Only indicate that we need a primary type check, i.e. the type isn't yet available but will be needed downstream.
  813. // Type checks and bailouts may still be needed in other places (e.g. loads from proto, fixed field checks, or
  814. // property adds), if a primary type check cannot protect them.
  815. Assert(MayNeedTypeCheckProtection());
  816. Assert(TypeCheckSeqBitsSetOnlyIfCandidate());
  817. return IsTypeCheckSeqCandidate() && !IsTypeDead() && !IsTypeChecked() && !HasTypeMismatch();
  818. }
  819. bool NeedsLocalTypeCheck() const
  820. {
  821. Assert(MayNeedTypeCheckProtection());
  822. Assert(TypeCheckSeqBitsSetOnlyIfCandidate());
  823. // Indicate whether this operation needs a type check for its own sake, since the type is dead and no downstream
  824. // operations require the type to be checked.
  825. return !PHASE_OFF1(Js::ObjTypeSpecIsolatedFldOpsPhase) &&
  826. IsTypeCheckSeqCandidate() && IsTypeDead() && !IsTypeCheckOnly() && !IsTypeChecked() && !HasTypeMismatch();
  827. }
  828. bool NeedsWriteGuardTypeCheck() const
  829. {
  830. Assert(MayNeedTypeCheckProtection());
  831. Assert(TypeCheckSeqBitsSetOnlyIfCandidate());
  832. // Type has been checked but property might have been written to since then.
  833. return !IsTypeCheckOnly() && !NeedsPrimaryTypeCheck() && IsTypeChecked() && !IsWriteGuardChecked();
  834. }
  835. bool NeedsLoadFromProtoTypeCheck() const
  836. {
  837. Assert(MayNeedTypeCheckProtection());
  838. Assert(TypeCheckSeqBitsSetOnlyIfCandidate());
  839. // Proto cache, where type has been checked but property might have been written to since then.
  840. return !IsTypeCheckOnly() && !NeedsPrimaryTypeCheck() && IsLoadedFromProto() && NeedsWriteGuardTypeCheck();
  841. }
  842. bool NeedsAddPropertyTypeCheck() const
  843. {
  844. Assert(MayNeedTypeCheckProtection());
  845. Assert(TypeCheckSeqBitsSetOnlyIfCandidate());
  846. // A property cannot become read-only without an explicit or implicit call (at least Object.defineProperty is needed), so if this
  847. // operation is protected by a primary type check upstream, there is no need for an additional local type check.
  848. return false;
  849. }
  850. bool NeedsCheckFixedFieldTypeCheck() const
  851. {
  852. Assert(MayNeedTypeCheckProtection());
  853. Assert(TypeCheckSeqBitsSetOnlyIfCandidate());
  854. return !IsTypeCheckOnly() && !NeedsPrimaryTypeCheck() && UsesFixedValue() && (!IsTypeChecked() || NeedsWriteGuardTypeCheck());
  855. }
  856. bool NeedsTypeCheck() const
  857. {
  858. return NeedsPrimaryTypeCheck() || NeedsLocalTypeCheck() ||
  859. NeedsLoadFromProtoTypeCheck() || NeedsAddPropertyTypeCheck() || NeedsCheckFixedFieldTypeCheck();
  860. }
  861. bool NeedsTypeCheckAndBailOut() const
  862. {
  863. return NeedsPrimaryTypeCheck() || (PHASE_ON1(Js::ObjTypeSpecIsolatedFldOpsWithBailOutPhase) && NeedsLocalTypeCheck()) || NeedsCheckFixedFieldTypeCheck();
  864. }
  865. // Is the instruction involving this operand optimized with a direct slot load or store? In other words, is it guarded
  866. // by a type check, either as part of the type check sequence, or explicitly on this instruction.
  867. bool IsObjTypeSpecOptimized() const
  868. {
  869. return MayNeedTypeCheckProtection() && (NeedsTypeCheckAndBailOut() || IsTypeCheckProtected());
  870. }
  871. // May the instruction involving this operand result in an implicit call? Note, that because in dead store pass we
  872. // may choose to remove a type check and fall back on a check against a live cache, instructions that have a primary
  873. // type check may still end up with implicit call bailout. However, if we are type check protected we will never
  874. // fall back on live cache. Similarly, for fixed method checks.
  875. bool MayHaveImplicitCall() const
  876. {
  877. return !IsRootObjectNonConfigurableFieldLoad() && !UsesFixedValue() && (!IsTypeCheckSeqCandidate() || !IsTypeCheckProtected());
  878. }
  879. // Is the instruction involving this operand part of a type check sequence? This is different from IsObjTypeSpecOptimized
  880. // in that an instruction such as CheckFixedFld may require a type check even if it is not part of a type check
  881. // sequence. In this case IsObjTypeSpecOptimized() == true, but IsTypeCheckSeqParticipant() == false.
  882. bool IsTypeCheckSeqParticipant() const
  883. {
  884. Assert(IsTypeCheckSeqCandidate());
  885. return NeedsPrimaryTypeCheck() || IsTypeCheckProtected();
  886. }
  887. bool HasFinalType() const
  888. {
  889. return this->finalType != nullptr;
  890. }
  891. Js::Type * GetFinalType() const
  892. {
  893. return this->finalType;
  894. }
  895. void SetFinalType(Js::Type* type)
  896. {
  897. Assert(type != nullptr);
  898. this->finalType = type;
  899. }
  900. void ClearFinalType()
  901. {
  902. this->finalType = nullptr;
  903. }
  904. BVSparse<JitArenaAllocator>* GetGuardedPropOps()
  905. {
  906. return this->guardedPropOps;
  907. }
  908. void EnsureGuardedPropOps(JitArenaAllocator* allocator)
  909. {
  910. if (this->guardedPropOps == nullptr)
  911. {
  912. this->guardedPropOps = JitAnew(allocator, BVSparse<JitArenaAllocator>, allocator);
  913. }
  914. }
  915. void SetGuardedPropOp(uint propOpId)
  916. {
  917. Assert(this->guardedPropOps != nullptr);
  918. this->guardedPropOps->Set(propOpId);
  919. }
  920. void AddGuardedPropOps(const BVSparse<JitArenaAllocator>* propOps)
  921. {
  922. Assert(this->guardedPropOps != nullptr);
  923. this->guardedPropOps->Or(propOps);
  924. }
  925. BVSparse<JitArenaAllocator>* GetWriteGuards()
  926. {
  927. return this->writeGuards;
  928. }
  929. void SetWriteGuards(BVSparse<JitArenaAllocator>* value)
  930. {
  931. Assert(this->writeGuards == nullptr);
  932. this->writeGuards = value;
  933. }
  934. void ClearWriteGuards()
  935. {
  936. this->writeGuards = nullptr;
  937. }
  938. #if DBG
  939. bool TypeCheckSeqBitsSetOnlyIfCandidate() const
  940. {
  941. return IsTypeCheckSeqCandidate() || (!IsTypeAvailable() && !IsTypeChecked() && !IsWriteGuardChecked() && !IsTypeDead());
  942. }
  943. #endif
  944. bool IsObjTypeSpecCandidate() const
  945. {
  946. return HasObjTypeSpecFldInfo() && this->objTypeSpecFldInfo->IsObjTypeSpecCandidate();
  947. }
  948. bool IsMonoObjTypeSpecCandidate() const
  949. {
  950. return HasObjTypeSpecFldInfo() && this->objTypeSpecFldInfo->IsMonoObjTypeSpecCandidate();
  951. }
  952. bool IsPolyObjTypeSpecCandidate() const
  953. {
  954. return HasObjTypeSpecFldInfo() && this->objTypeSpecFldInfo->IsPolyObjTypeSpecCandidate();
  955. }
  956. Js::TypeId GetTypeId() const
  957. {
  958. Assert(HasObjTypeSpecFldInfo());
  959. return this->objTypeSpecFldInfo->GetTypeId();
  960. }
  961. Js::TypeId GetTypeId(uint i) const
  962. {
  963. Assert(HasObjTypeSpecFldInfo());
  964. return this->objTypeSpecFldInfo->GetTypeId(i);
  965. }
  966. Js::Type * GetType() const
  967. {
  968. Assert(HasObjTypeSpecFldInfo());
  969. return this->objTypeSpecFldInfo->GetType();
  970. }
  971. Js::Type * GetType(uint i) const
  972. {
  973. Assert(HasObjTypeSpecFldInfo());
  974. return this->objTypeSpecFldInfo->GetType(i);
  975. }
  976. bool HasInitialType() const
  977. {
  978. Assert(HasObjTypeSpecFldInfo());
  979. return this->objTypeSpecFldInfo->HasInitialType();
  980. }
  981. Js::Type * GetInitialType() const
  982. {
  983. Assert(HasObjTypeSpecFldInfo());
  984. return this->objTypeSpecFldInfo->GetInitialType();
  985. }
  986. Js::EquivalentTypeSet * GetEquivalentTypeSet() const
  987. {
  988. Assert(HasObjTypeSpecFldInfo());
  989. return this->objTypeSpecFldInfo->GetEquivalentTypeSet();
  990. }
  991. Js::Type * GetFirstEquivalentType() const
  992. {
  993. Assert(HasObjTypeSpecFldInfo());
  994. return this->objTypeSpecFldInfo->GetFirstEquivalentType();
  995. }
  996. bool IsObjectHeaderInlined() const;
  997. void UpdateSlotForFinalType();
  998. bool ChangesObjectLayout() const;
  999. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  1000. const wchar_t* GetCacheLayoutString() const
  1001. {
  1002. return HasObjTypeSpecFldInfo() ? this->objTypeSpecFldInfo->GetCacheLayoutString() : L"empty";
  1003. }
  1004. #endif
  1005. };
  1006. ///---------------------------------------------------------------------------
  1007. ///
  1008. /// class RegOpnd
  1009. ///
  1010. ///---------------------------------------------------------------------------
  1011. class RegOpnd : public Opnd
  1012. {
  1013. protected:
  1014. RegOpnd(StackSym *sym, RegNum reg, IRType type);
  1015. RegOpnd(const RegOpnd &other, StackSym * sym);
  1016. private:
  1017. void Initialize(StackSym *sym, RegNum reg, IRType type);
  1018. public:
  1019. static RegOpnd * New(IRType type, Func *func);
  1020. static RegOpnd * New(StackSym *sym, IRType type, Func *func);
  1021. static RegOpnd * New(StackSym *sym, RegNum reg, IRType type, Func *func);
  1022. public:
  1023. bool IsArrayRegOpnd() const
  1024. {
  1025. Assert(m_isArrayRegOpnd == DbgIsArrayRegOpnd());
  1026. Assert(!m_isArrayRegOpnd || m_valueType.IsAnyOptimizedArray());
  1027. return m_isArrayRegOpnd;
  1028. }
  1029. ArrayRegOpnd * AsArrayRegOpnd();
  1030. RegNum GetReg() const;
  1031. void SetReg(RegNum reg);
  1032. //Note type: OpndKindReg
  1033. RegOpnd * CopyInternal(Func *func);
  1034. RegOpnd * CloneDefInternal(Func *func);
  1035. RegOpnd * CloneUseInternal(Func *func);
  1036. StackSym * GetStackSymInternal() const;
  1037. static StackSym * TryGetStackSym(Opnd *const opnd);
  1038. bool IsEqualInternal(Opnd *opnd);
  1039. void FreeInternal(Func * func);
  1040. bool IsSameReg(Opnd *opnd);
  1041. bool IsSameRegUntyped(Opnd *opnd);
  1042. #if DBG
  1043. void FreezeSymValue() { m_symValueFrozen = true; }
  1044. bool IsSymValueFrozen() const { return m_symValueFrozen; }
  1045. virtual bool DbgIsArrayRegOpnd() const { return false; }
  1046. #endif
  1047. private:
  1048. RegOpnd * CopyInternal(StackSym * sym, Func * func);
  1049. public:
  1050. StackSym * m_sym;
  1051. bool m_isTempLastUse:1;
  1052. bool m_isCallArg:1;
  1053. bool m_dontDeadStore: 1;
  1054. bool m_fgPeepTmp: 1;
  1055. bool m_wasNegativeZeroPreventedByBailout : 1;
  1056. bool m_isArrayRegOpnd : 1;
  1057. #if DBG
  1058. private:
  1059. bool m_symValueFrozen : 1; // if true, prevents this operand from being used as the destination operand in an instruction
  1060. #endif
  1061. private:
  1062. RegNum m_reg;
  1063. PREVENT_COPY(RegOpnd);
  1064. };
  1065. ///---------------------------------------------------------------------------
  1066. ///
  1067. /// class ArrayRegOpnd
  1068. ///
  1069. ///---------------------------------------------------------------------------
  1070. class ArrayRegOpnd sealed : public RegOpnd
  1071. {
  1072. private:
  1073. StackSym *headSegmentSym;
  1074. StackSym *headSegmentLengthSym;
  1075. StackSym *lengthSym;
  1076. const bool eliminatedLowerBoundCheck, eliminatedUpperBoundCheck;
  1077. protected:
  1078. ArrayRegOpnd(StackSym *const arraySym, const ValueType valueType, StackSym *const headSegmentSym, StackSym *const headSegmentLengthSym, StackSym *const lengthSym, const bool eliminatedLowerBoundCheck, const bool eliminatedUpperBoundCheck);
  1079. 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);
  1080. public:
  1081. 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);
  1082. 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);
  1083. public:
  1084. #if DBG
  1085. virtual bool DbgIsArrayRegOpnd() const { return true; }
  1086. #endif
  1087. StackSym *HeadSegmentSym() const
  1088. {
  1089. return headSegmentSym;
  1090. }
  1091. void RemoveHeadSegmentSym()
  1092. {
  1093. headSegmentSym = nullptr;
  1094. }
  1095. StackSym *HeadSegmentLengthSym() const
  1096. {
  1097. return headSegmentLengthSym;
  1098. }
  1099. void RemoveHeadSegmentLengthSym()
  1100. {
  1101. headSegmentLengthSym = nullptr;
  1102. }
  1103. StackSym *LengthSym() const
  1104. {
  1105. // For typed arrays, the head segment length is the same as the array length
  1106. Assert(!(m_valueType.IsLikelyTypedArray() && !m_valueType.IsOptimizedTypedArray()));
  1107. return m_valueType.IsLikelyTypedArray() ? HeadSegmentLengthSym() : lengthSym;
  1108. }
  1109. void RemoveLengthSym()
  1110. {
  1111. Assert(m_valueType.IsArray());
  1112. lengthSym = nullptr;
  1113. }
  1114. bool EliminatedLowerBoundCheck() const
  1115. {
  1116. return eliminatedLowerBoundCheck;
  1117. }
  1118. bool EliminatedUpperBoundCheck() const
  1119. {
  1120. return eliminatedUpperBoundCheck;
  1121. }
  1122. public:
  1123. RegOpnd *CopyAsRegOpnd(Func *func);
  1124. ArrayRegOpnd * CopyInternalSub(Func *func);
  1125. ArrayRegOpnd *CloneDefInternalSub(Func *func);
  1126. ArrayRegOpnd *CloneUseInternalSub(Func *func);
  1127. private:
  1128. ArrayRegOpnd *Clone(StackSym *const arraySym, StackSym *const headSegmentSym, StackSym *const headSegmentLengthSym, StackSym *const lengthSym, Func *const func) const;
  1129. public:
  1130. void FreeInternalSub(Func *func);
  1131. // IsEqual is not overridden because this opnd still primarily represents the array sym. Equality comparisons using IsEqual
  1132. // are used to determine whether opnds should be swapped, etc. and the extra information in this class should not affect
  1133. // that behavior.
  1134. // virtual bool IsEqual(Opnd *opnd) override;
  1135. PREVENT_COPY(ArrayRegOpnd);
  1136. };
  1137. ///---------------------------------------------------------------------------
  1138. ///
  1139. /// class AddrOpnd
  1140. ///
  1141. ///---------------------------------------------------------------------------
  1142. class AddrOpnd sealed : public Opnd
  1143. {
  1144. public:
  1145. static AddrOpnd * New(Js::Var address, AddrOpndKind addrOpndKind, Func *func, bool dontEncode = false);
  1146. static AddrOpnd * NewFromNumber(double value, Func *func, bool dontEncode = false);
  1147. static AddrOpnd * NewFromNumber(int32 value, Func *func, bool dontEncode = false);
  1148. static AddrOpnd * NewFromNumber(int64 value, Func *func, bool dontEncode = false);
  1149. static AddrOpnd * NewNull(Func * func);
  1150. public:
  1151. //Note type: OpndKindAddr
  1152. AddrOpnd * CopyInternal(Func *func);
  1153. bool IsEqualInternal(Opnd *opnd);
  1154. void FreeInternal(Func * func);
  1155. bool IsDynamic() const { return addrOpndKind > AddrOpndKindConstantVar; }
  1156. bool IsVar() const { return addrOpndKind == AddrOpndKindDynamicVar || addrOpndKind == AddrOpndKindConstantVar; }
  1157. void SetEncodedValue(Js::Var address, AddrOpndKind addrOpndKind);
  1158. AddrOpndKind GetAddrOpndKind() const { return addrOpndKind; }
  1159. void SetAddress(Js::Var address, AddrOpndKind addrOpndKind);
  1160. public:
  1161. Js::Var m_address;
  1162. bool m_dontEncode: 1;
  1163. bool m_isFunction: 1;
  1164. private:
  1165. AddrOpndKind addrOpndKind;
  1166. public:
  1167. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  1168. Js::Var decodedValue; // FIXME (t-doilij) set ENABLE_IR_VIEWER blocks where this is set
  1169. #endif
  1170. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  1171. bool wasVar;
  1172. #endif
  1173. };
  1174. ///---------------------------------------------------------------------------
  1175. ///
  1176. /// class IndirOpnd
  1177. ///
  1178. ///---------------------------------------------------------------------------
  1179. class IndirOpnd: public Opnd
  1180. {
  1181. public:
  1182. static IndirOpnd * New(RegOpnd * baseOpnd, RegOpnd * indexOpnd, IRType type, Func *func);
  1183. static IndirOpnd * New(RegOpnd * baseOpnd, RegOpnd * indexOpnd, byte scale, IRType type, Func *func);
  1184. static IndirOpnd * New(RegOpnd * baseOpnd, int32 offset, IRType type, Func *func, bool dontEncode = false);
  1185. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  1186. static IndirOpnd * New(RegOpnd * baseOpnd, int32 offset, IRType type, const wchar_t *desc, Func *func, bool dontEncode = false);
  1187. #endif
  1188. public:
  1189. IndirOpnd() : Opnd(), m_baseOpnd(nullptr), m_indexOpnd(nullptr), m_offset(0), m_scale(0), m_func(nullptr), m_dontEncode(false)
  1190. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  1191. , m_desc(nullptr)
  1192. #endif
  1193. #if DBG_DUMP
  1194. , m_addrKind((IR::AddrOpndKind)-1)
  1195. #endif
  1196. {
  1197. }
  1198. ~IndirOpnd();
  1199. // Note type: OpndKindIndir
  1200. IndirOpnd * CopyInternal(Func *func);
  1201. IndirOpnd * CloneDefInternal(Func *func);
  1202. IndirOpnd * CloneUseInternal(Func *func);
  1203. bool IsEqualInternal(Opnd *opnd);
  1204. void FreeInternal(Func * func);
  1205. RegOpnd * GetBaseOpnd() const;
  1206. void SetBaseOpnd(RegOpnd *baseOpnd);
  1207. RegOpnd * UnlinkBaseOpnd();
  1208. void ReplaceBaseOpnd(RegOpnd *newBase);
  1209. RegOpnd * GetIndexOpnd();
  1210. void SetIndexOpnd(RegOpnd *indexOpnd);
  1211. RegOpnd * GetIndexOpnd() const;
  1212. RegOpnd * UnlinkIndexOpnd();
  1213. void ReplaceIndexOpnd(RegOpnd *newIndex);
  1214. int32 GetOffset() const;
  1215. void SetOffset(int32 offset, bool dontEncode = false);
  1216. byte GetScale() const;
  1217. void SetScale(byte scale);
  1218. bool TryGetIntConstIndexValue(bool trySym, IntConstType *pValue, bool *pIsNotInt);
  1219. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  1220. const wchar_t * GetDescription();
  1221. IR::AddrOpndKind GetAddrKind() const;
  1222. bool HasAddrKind() const;
  1223. void * GetOriginalAddress() const;
  1224. #endif
  1225. bool m_dontEncode;
  1226. #if DBG_DUMP
  1227. void SetAddrKind(IR::AddrOpndKind kind, void * originalAddress);
  1228. #endif
  1229. private:
  1230. RegOpnd * m_baseOpnd;
  1231. RegOpnd * m_indexOpnd;
  1232. int32 m_offset;
  1233. byte m_scale;
  1234. Func * m_func; // We need the allocator to copy the base and index...
  1235. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  1236. const wchar_t * m_desc;
  1237. #endif
  1238. #if DBG_DUMP
  1239. IR::AddrOpndKind m_addrKind; // if m_addrKind != -1, than this used to be MemRefOpnd which has the address hoisted;
  1240. void * m_originalAddress;
  1241. #endif
  1242. };
  1243. ///---------------------------------------------------------------------------
  1244. ///
  1245. /// class MemRefOpnd - represents a reference to a fixed memory location
  1246. ///
  1247. ///---------------------------------------------------------------------------
  1248. class MemRefOpnd : public Opnd
  1249. {
  1250. public:
  1251. static MemRefOpnd * New(void * pMemLoc, IRType, Func * func, AddrOpndKind addrOpndKind = AddrOpndKindDynamicMisc);
  1252. public:
  1253. // Note type: OpndKindMemRef
  1254. MemRefOpnd * CopyInternal(Func * func);
  1255. bool IsEqualInternal(Opnd *opnd);
  1256. void FreeInternal(Func * func);
  1257. void * GetMemLoc() const;
  1258. void SetMemLoc(void * pMemLoc);
  1259. IR::AddrOpndKind GetAddrKind() const;
  1260. private:
  1261. void * m_memLoc;
  1262. #if DBG_DUMP
  1263. AddrOpndKind m_addrKind;
  1264. #endif
  1265. };
  1266. //
  1267. // class LabelOpnd - represents a reference to a local code address
  1268. //
  1269. class LabelOpnd : public Opnd
  1270. {
  1271. public:
  1272. static LabelOpnd * New(LabelInstr * labelInstr, Func * func);
  1273. public:
  1274. //Note type: OpndKindLabel
  1275. LabelOpnd * CopyInternal(Func * func);
  1276. bool IsEqualInternal(Opnd * opnd);
  1277. void FreeInternal(Func * func);
  1278. LabelInstr * GetLabel() const;
  1279. void SetLabel(LabelInstr * labelInstr);
  1280. private:
  1281. LabelInstr * m_label;
  1282. };
  1283. ///---------------------------------------------------------------------------
  1284. ///
  1285. /// class Bit Field vector
  1286. ///
  1287. ///---------------------------------------------------------------------------
  1288. class RegBVOpnd: public Opnd
  1289. {
  1290. public:
  1291. static RegBVOpnd * New(BVUnit32 value, IRType type, Func *func);
  1292. public:
  1293. //Note: type: OpndKindRegBV
  1294. RegBVOpnd * CopyInternal(Func *func);
  1295. bool IsEqualInternal(Opnd *opnd);
  1296. void FreeInternal(Func * func);
  1297. BVUnit32 GetValue() const;
  1298. public:
  1299. BVUnit32 m_value;
  1300. };
  1301. class AutoReuseOpnd
  1302. {
  1303. private:
  1304. Opnd *opnd;
  1305. Func *func;
  1306. bool autoDelete;
  1307. bool wasInUse;
  1308. public:
  1309. AutoReuseOpnd() : opnd(nullptr), wasInUse(true)
  1310. {
  1311. }
  1312. AutoReuseOpnd(Opnd *const opnd, Func *const func, const bool autoDelete = true) : opnd(nullptr)
  1313. {
  1314. Initialize(opnd, func, autoDelete);
  1315. }
  1316. void Initialize(Opnd *const opnd, Func *const func, const bool autoDelete = true)
  1317. {
  1318. Assert(!this->opnd);
  1319. Assert(func);
  1320. if(!opnd)
  1321. {
  1322. // Simulate the default constructor
  1323. wasInUse = true;
  1324. return;
  1325. }
  1326. this->opnd = opnd;
  1327. wasInUse = opnd->IsInUse();
  1328. if(wasInUse)
  1329. {
  1330. return;
  1331. }
  1332. this->func = func;
  1333. this->autoDelete = autoDelete;
  1334. // Create a fake use of the opnd to enable opnd reuse during lowering. One issue is that when an unused opnd is first
  1335. // used in an instruction and the instruction is legalized, the opnd may be replaced by legalization and the original
  1336. // opnd would be freed. By creating a fake use, it forces the opnd to be copied when used by the instruction, so the
  1337. // original opnd can continue to be reused for other instructions. Typically, any opnds used during lowering in more
  1338. // than one instruction can use this class to enable opnd reuse.
  1339. opnd->Use(func);
  1340. }
  1341. ~AutoReuseOpnd()
  1342. {
  1343. if(wasInUse)
  1344. {
  1345. return;
  1346. }
  1347. if(autoDelete)
  1348. {
  1349. opnd->Free(func);
  1350. }
  1351. else
  1352. {
  1353. opnd->UnUse();
  1354. }
  1355. }
  1356. PREVENT_COPY(AutoReuseOpnd)
  1357. };
  1358. } // namespace IR