ArrayProfile.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (C) 2012 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef ArrayProfile_h
  26. #define ArrayProfile_h
  27. #include "JSArray.h"
  28. #include "Structure.h"
  29. #include <wtf/HashMap.h>
  30. #include <wtf/SegmentedVector.h>
  31. namespace JSC {
  32. class CodeBlock;
  33. class LLIntOffsetsExtractor;
  34. // This is a bitfield where each bit represents an IndexingType that we have seen.
  35. // There are 32 indexing types, so an unsigned is enough.
  36. typedef unsigned ArrayModes;
  37. #define asArrayModes(type) \
  38. (static_cast<unsigned>(1) << static_cast<unsigned>(type))
  39. #define ALL_NON_ARRAY_ARRAY_MODES \
  40. (asArrayModes(NonArray) \
  41. | asArrayModes(NonArrayWithInt32) \
  42. | asArrayModes(NonArrayWithDouble) \
  43. | asArrayModes(NonArrayWithContiguous) \
  44. | asArrayModes(NonArrayWithArrayStorage) \
  45. | asArrayModes(NonArrayWithSlowPutArrayStorage))
  46. #define ALL_ARRAY_ARRAY_MODES \
  47. (asArrayModes(ArrayClass) \
  48. | asArrayModes(ArrayWithUndecided) \
  49. | asArrayModes(ArrayWithInt32) \
  50. | asArrayModes(ArrayWithDouble) \
  51. | asArrayModes(ArrayWithContiguous) \
  52. | asArrayModes(ArrayWithArrayStorage) \
  53. | asArrayModes(ArrayWithSlowPutArrayStorage))
  54. #define ALL_ARRAY_MODES (ALL_NON_ARRAY_ARRAY_MODES | ALL_ARRAY_ARRAY_MODES)
  55. inline ArrayModes arrayModeFromStructure(Structure* structure)
  56. {
  57. return asArrayModes(structure->indexingType());
  58. }
  59. void dumpArrayModes(PrintStream&, ArrayModes);
  60. MAKE_PRINT_ADAPTOR(ArrayModesDump, ArrayModes, dumpArrayModes);
  61. inline bool mergeArrayModes(ArrayModes& left, ArrayModes right)
  62. {
  63. ArrayModes newModes = left | right;
  64. if (newModes == left)
  65. return false;
  66. left = newModes;
  67. return true;
  68. }
  69. // Checks if proven is a subset of expected.
  70. inline bool arrayModesAlreadyChecked(ArrayModes proven, ArrayModes expected)
  71. {
  72. return (expected | proven) == expected;
  73. }
  74. inline bool arrayModesInclude(ArrayModes arrayModes, IndexingType shape)
  75. {
  76. return !!(arrayModes & (asArrayModes(NonArray | shape) | asArrayModes(ArrayClass | shape)));
  77. }
  78. inline bool shouldUseSlowPutArrayStorage(ArrayModes arrayModes)
  79. {
  80. return arrayModesInclude(arrayModes, SlowPutArrayStorageShape);
  81. }
  82. inline bool shouldUseFastArrayStorage(ArrayModes arrayModes)
  83. {
  84. return arrayModesInclude(arrayModes, ArrayStorageShape);
  85. }
  86. inline bool shouldUseContiguous(ArrayModes arrayModes)
  87. {
  88. return arrayModesInclude(arrayModes, ContiguousShape);
  89. }
  90. inline bool shouldUseDouble(ArrayModes arrayModes)
  91. {
  92. return arrayModesInclude(arrayModes, DoubleShape);
  93. }
  94. inline bool shouldUseInt32(ArrayModes arrayModes)
  95. {
  96. return arrayModesInclude(arrayModes, Int32Shape);
  97. }
  98. inline bool hasSeenArray(ArrayModes arrayModes)
  99. {
  100. return arrayModes & ALL_ARRAY_ARRAY_MODES;
  101. }
  102. inline bool hasSeenNonArray(ArrayModes arrayModes)
  103. {
  104. return arrayModes & ALL_NON_ARRAY_ARRAY_MODES;
  105. }
  106. class ArrayProfile {
  107. public:
  108. ArrayProfile()
  109. : m_bytecodeOffset(std::numeric_limits<unsigned>::max())
  110. , m_lastSeenStructure(0)
  111. , m_expectedStructure(0)
  112. , m_mayStoreToHole(false)
  113. , m_outOfBounds(false)
  114. , m_mayInterceptIndexedAccesses(false)
  115. , m_usesOriginalArrayStructures(true)
  116. , m_observedArrayModes(0)
  117. {
  118. }
  119. ArrayProfile(unsigned bytecodeOffset)
  120. : m_bytecodeOffset(bytecodeOffset)
  121. , m_lastSeenStructure(0)
  122. , m_expectedStructure(0)
  123. , m_mayStoreToHole(false)
  124. , m_outOfBounds(false)
  125. , m_mayInterceptIndexedAccesses(false)
  126. , m_usesOriginalArrayStructures(true)
  127. , m_observedArrayModes(0)
  128. {
  129. }
  130. unsigned bytecodeOffset() const { return m_bytecodeOffset; }
  131. Structure** addressOfLastSeenStructure() { return &m_lastSeenStructure; }
  132. ArrayModes* addressOfArrayModes() { return &m_observedArrayModes; }
  133. bool* addressOfMayStoreToHole() { return &m_mayStoreToHole; }
  134. bool* addressOfOutOfBounds() { return &m_outOfBounds; }
  135. void observeStructure(Structure* structure)
  136. {
  137. m_lastSeenStructure = structure;
  138. }
  139. void computeUpdatedPrediction(CodeBlock*, OperationInProgress = NoOperation);
  140. Structure* expectedStructure() const
  141. {
  142. if (structureIsPolymorphic())
  143. return 0;
  144. return m_expectedStructure;
  145. }
  146. bool structureIsPolymorphic() const
  147. {
  148. return m_expectedStructure == polymorphicStructure();
  149. }
  150. bool hasDefiniteStructure() const
  151. {
  152. return !structureIsPolymorphic() && m_expectedStructure;
  153. }
  154. ArrayModes observedArrayModes() const { return m_observedArrayModes; }
  155. ArrayModes updatedObservedArrayModes() const; // Computes the observed array modes without updating the profile.
  156. bool mayInterceptIndexedAccesses() const { return m_mayInterceptIndexedAccesses; }
  157. bool mayStoreToHole() const { return m_mayStoreToHole; }
  158. bool outOfBounds() const { return m_outOfBounds; }
  159. bool usesOriginalArrayStructures() const { return m_usesOriginalArrayStructures; }
  160. CString briefDescription(CodeBlock*);
  161. private:
  162. friend class LLIntOffsetsExtractor;
  163. static Structure* polymorphicStructure() { return static_cast<Structure*>(reinterpret_cast<void*>(1)); }
  164. unsigned m_bytecodeOffset;
  165. Structure* m_lastSeenStructure;
  166. Structure* m_expectedStructure;
  167. bool m_mayStoreToHole; // This flag may become overloaded to indicate other special cases that were encountered during array access, as it depends on indexing type. Since we currently have basically just one indexing type (two variants of ArrayStorage), this flag for now just means exactly what its name implies.
  168. bool m_outOfBounds;
  169. bool m_mayInterceptIndexedAccesses;
  170. bool m_usesOriginalArrayStructures;
  171. ArrayModes m_observedArrayModes;
  172. };
  173. typedef SegmentedVector_shared<ArrayProfile, 4, 0> ArrayProfileVector;
  174. } // namespace JSC
  175. #endif // ArrayProfile_h