/ bytecode / ArrayProfile.h
ArrayProfile.h
  1  /*
  2   * Copyright (C) 2012-2018 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  
 26  #pragma once
 27  
 28  #include "ConcurrentJSLock.h"
 29  #include "Structure.h"
 30  
 31  namespace JSC {
 32  
 33  class CodeBlock;
 34  class LLIntOffsetsExtractor;
 35  
 36  // This is a bitfield where each bit represents an type of array access that we have seen.
 37  // There are 19 indexing types that use the lower bits.
 38  // There are 9 typed array types taking the bits 16 to 25.
 39  typedef unsigned ArrayModes;
 40  
 41  // The possible IndexingTypes are limited within (0 - 16, 21, 23, 25).
 42  // This is because CoW types only appear for JSArrays.
 43  static_assert(CopyOnWriteArrayWithInt32 == 21, "");
 44  static_assert(CopyOnWriteArrayWithDouble == 23, "");
 45  static_assert(CopyOnWriteArrayWithContiguous == 25, "");
 46  const ArrayModes CopyOnWriteArrayWithInt32ArrayMode = 1 << CopyOnWriteArrayWithInt32;
 47  const ArrayModes CopyOnWriteArrayWithDoubleArrayMode = 1 << CopyOnWriteArrayWithDouble;
 48  const ArrayModes CopyOnWriteArrayWithContiguousArrayMode = 1 << CopyOnWriteArrayWithContiguous;
 49  
 50  const ArrayModes Int8ArrayMode = 1 << 16;
 51  const ArrayModes Int16ArrayMode = 1 << 17;
 52  const ArrayModes Int32ArrayMode = 1 << 18;
 53  const ArrayModes Uint8ArrayMode = 1 << 19;
 54  const ArrayModes Uint8ClampedArrayMode = 1 << 20; // 21 - 25 are used for CoW arrays.
 55  const ArrayModes Uint16ArrayMode = 1 << 26;
 56  const ArrayModes Uint32ArrayMode = 1 << 27;
 57  const ArrayModes Float32ArrayMode = 1 << 28;
 58  const ArrayModes Float64ArrayMode = 1 << 29;
 59  
 60  JS_EXPORT_PRIVATE extern const ArrayModes typedArrayModes[NumberOfTypedArrayTypesExcludingDataView];
 61  
 62  constexpr ArrayModes asArrayModesIgnoringTypedArrays(IndexingType indexingMode)
 63  {
 64      return static_cast<unsigned>(1) << static_cast<unsigned>(indexingMode);
 65  }
 66  
 67  #define ALL_TYPED_ARRAY_MODES \
 68      (Int8ArrayMode            \
 69      | Int16ArrayMode          \
 70      | Int32ArrayMode          \
 71      | Uint8ArrayMode          \
 72      | Uint8ClampedArrayMode   \
 73      | Uint16ArrayMode         \
 74      | Uint32ArrayMode         \
 75      | Float32ArrayMode        \
 76      | Float64ArrayMode        \
 77      )
 78  
 79  #define ALL_NON_ARRAY_ARRAY_MODES                       \
 80      (asArrayModesIgnoringTypedArrays(NonArray)                             \
 81      | asArrayModesIgnoringTypedArrays(NonArrayWithInt32)                   \
 82      | asArrayModesIgnoringTypedArrays(NonArrayWithDouble)                  \
 83      | asArrayModesIgnoringTypedArrays(NonArrayWithContiguous)              \
 84      | asArrayModesIgnoringTypedArrays(NonArrayWithArrayStorage)            \
 85      | asArrayModesIgnoringTypedArrays(NonArrayWithSlowPutArrayStorage)     \
 86      | ALL_TYPED_ARRAY_MODES)
 87  
 88  #define ALL_COPY_ON_WRITE_ARRAY_MODES                   \
 89      (CopyOnWriteArrayWithInt32ArrayMode                 \
 90      | CopyOnWriteArrayWithDoubleArrayMode               \
 91      | CopyOnWriteArrayWithContiguousArrayMode)
 92  
 93  #define ALL_WRITABLE_ARRAY_ARRAY_MODES                  \
 94      (asArrayModesIgnoringTypedArrays(ArrayClass)                           \
 95      | asArrayModesIgnoringTypedArrays(ArrayWithUndecided)                  \
 96      | asArrayModesIgnoringTypedArrays(ArrayWithInt32)                      \
 97      | asArrayModesIgnoringTypedArrays(ArrayWithDouble)                     \
 98      | asArrayModesIgnoringTypedArrays(ArrayWithContiguous)                 \
 99      | asArrayModesIgnoringTypedArrays(ArrayWithArrayStorage)               \
100      | asArrayModesIgnoringTypedArrays(ArrayWithSlowPutArrayStorage))
101  
102  #define ALL_ARRAY_ARRAY_MODES                           \
103      (ALL_WRITABLE_ARRAY_ARRAY_MODES                     \
104      | ALL_COPY_ON_WRITE_ARRAY_MODES)
105  
106  #define ALL_ARRAY_MODES (ALL_NON_ARRAY_ARRAY_MODES | ALL_ARRAY_ARRAY_MODES)
107  
108  inline ArrayModes arrayModesFromStructure(Structure* structure)
109  {
110      JSType type = structure->typeInfo().type();
111      if (isTypedArrayType(type))
112          return typedArrayModes[type - FirstTypedArrayType];
113      return asArrayModesIgnoringTypedArrays(structure->indexingMode());
114  }
115  
116  void dumpArrayModes(PrintStream&, ArrayModes);
117  MAKE_PRINT_ADAPTOR(ArrayModesDump, ArrayModes, dumpArrayModes);
118  
119  inline bool mergeArrayModes(ArrayModes& left, ArrayModes right)
120  {
121      ArrayModes newModes = left | right;
122      if (newModes == left)
123          return false;
124      left = newModes;
125      return true;
126  }
127  
128  inline bool arrayModesAreClearOrTop(ArrayModes modes)
129  {
130      return !modes || modes == ALL_ARRAY_MODES;
131  }
132  
133  // Checks if proven is a subset of expected.
134  inline bool arrayModesAlreadyChecked(ArrayModes proven, ArrayModes expected)
135  {
136      return (expected | proven) == expected;
137  }
138  
139  inline bool arrayModesIncludeIgnoringTypedArrays(ArrayModes arrayModes, IndexingType shape)
140  {
141      ArrayModes modes = asArrayModesIgnoringTypedArrays(NonArray | shape) | asArrayModesIgnoringTypedArrays(ArrayClass | shape);
142      if (hasInt32(shape) || hasDouble(shape) || hasContiguous(shape))
143          modes |= asArrayModesIgnoringTypedArrays(ArrayClass | shape | CopyOnWrite);
144      return !!(arrayModes & modes);
145  }
146  
147  inline bool shouldUseSlowPutArrayStorage(ArrayModes arrayModes)
148  {
149      return arrayModesIncludeIgnoringTypedArrays(arrayModes, SlowPutArrayStorageShape);
150  }
151  
152  inline bool shouldUseFastArrayStorage(ArrayModes arrayModes)
153  {
154      return arrayModesIncludeIgnoringTypedArrays(arrayModes, ArrayStorageShape);
155  }
156  
157  inline bool shouldUseContiguous(ArrayModes arrayModes)
158  {
159      return arrayModesIncludeIgnoringTypedArrays(arrayModes, ContiguousShape);
160  }
161  
162  inline bool shouldUseDouble(ArrayModes arrayModes)
163  {
164      return arrayModesIncludeIgnoringTypedArrays(arrayModes, DoubleShape);
165  }
166  
167  inline bool shouldUseInt32(ArrayModes arrayModes)
168  {
169      return arrayModesIncludeIgnoringTypedArrays(arrayModes, Int32Shape);
170  }
171  
172  inline bool hasSeenArray(ArrayModes arrayModes)
173  {
174      return arrayModes & ALL_ARRAY_ARRAY_MODES;
175  }
176  
177  inline bool hasSeenNonArray(ArrayModes arrayModes)
178  {
179      return arrayModes & ALL_NON_ARRAY_ARRAY_MODES;
180  }
181  
182  inline bool hasSeenWritableArray(ArrayModes arrayModes)
183  {
184      return arrayModes & ALL_WRITABLE_ARRAY_ARRAY_MODES;
185  }
186  
187  inline bool hasSeenCopyOnWriteArray(ArrayModes arrayModes)
188  {
189      return arrayModes & ALL_COPY_ON_WRITE_ARRAY_MODES;
190  }
191  
192  class ArrayProfile {
193      friend class CodeBlock;
194  
195  public:
196      explicit ArrayProfile()
197          : m_mayInterceptIndexedAccesses(false)
198          , m_usesOriginalArrayStructures(true)
199          , m_didPerformFirstRunPruning(false)
200      {
201      }
202      
203      StructureID* addressOfLastSeenStructureID() { return &m_lastSeenStructureID; }
204      ArrayModes* addressOfArrayModes() { return &m_observedArrayModes; }
205      bool* addressOfMayStoreToHole() { return &m_mayStoreToHole; }
206  
207      void setOutOfBounds() { m_outOfBounds = true; }
208      bool* addressOfOutOfBounds() { return &m_outOfBounds; }
209      
210      void observeStructureID(StructureID structureID) { m_lastSeenStructureID = structureID; }
211      void observeStructure(Structure* structure) { m_lastSeenStructureID = structure->id(); }
212  
213      void computeUpdatedPrediction(const ConcurrentJSLocker&, CodeBlock*);
214      void computeUpdatedPrediction(const ConcurrentJSLocker&, CodeBlock*, Structure* lastSeenStructure);
215      
216      void observeArrayMode(ArrayModes mode) { m_observedArrayModes |= mode; }
217      void observeIndexedRead(VM&, JSCell*, unsigned index);
218  
219      ArrayModes observedArrayModes(const ConcurrentJSLocker&) const { return m_observedArrayModes; }
220      bool mayInterceptIndexedAccesses(const ConcurrentJSLocker&) const { return m_mayInterceptIndexedAccesses; }
221      
222      bool mayStoreToHole(const ConcurrentJSLocker&) const { return m_mayStoreToHole; }
223      bool outOfBounds(const ConcurrentJSLocker&) const { return m_outOfBounds; }
224      
225      bool usesOriginalArrayStructures(const ConcurrentJSLocker&) const { return m_usesOriginalArrayStructures; }
226  
227      CString briefDescription(const ConcurrentJSLocker&, CodeBlock*);
228      CString briefDescriptionWithoutUpdating(const ConcurrentJSLocker&);
229      
230  private:
231      friend class LLIntOffsetsExtractor;
232      
233      static Structure* polymorphicStructure() { return static_cast<Structure*>(reinterpret_cast<void*>(1)); }
234      
235      StructureID m_lastSeenStructureID { 0 };
236      bool m_mayStoreToHole { false }; // 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.
237      bool m_outOfBounds { false };
238      bool m_mayInterceptIndexedAccesses : 1;
239      bool m_usesOriginalArrayStructures : 1;
240      bool m_didPerformFirstRunPruning : 1;
241      ArrayModes m_observedArrayModes { 0 };
242  };
243  static_assert(sizeof(ArrayProfile) == 12);
244  
245  } // namespace JSC