LazyOperandValueProfile.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 LazyOperandValueProfile_h
  26. #define LazyOperandValueProfile_h
  27. #include <wtf/Platform.h>
  28. #if ENABLE(VALUE_PROFILER)
  29. #include "ValueProfile.h"
  30. #include <wtf/HashMap.h>
  31. #include <wtf/Noncopyable.h>
  32. #include <wtf/OwnPtr.h>
  33. #include <wtf/SegmentedVector.h>
  34. namespace JSC {
  35. class ScriptExecutable;
  36. class LazyOperandValueProfileKey {
  37. public:
  38. LazyOperandValueProfileKey()
  39. : m_bytecodeOffset(0) // 0 = empty value
  40. , m_operand(-1) // not a valid operand index in our current scheme
  41. {
  42. }
  43. LazyOperandValueProfileKey(WTF::HashTableDeletedValueType)
  44. : m_bytecodeOffset(1) // 1 = deleted value
  45. , m_operand(-1) // not a valid operand index in our current scheme
  46. {
  47. }
  48. LazyOperandValueProfileKey(unsigned bytecodeOffset, int operand)
  49. : m_bytecodeOffset(bytecodeOffset)
  50. , m_operand(operand)
  51. {
  52. ASSERT(operand != -1);
  53. }
  54. bool operator!() const
  55. {
  56. return m_operand == -1;
  57. }
  58. bool operator==(const LazyOperandValueProfileKey& other) const
  59. {
  60. return m_bytecodeOffset == other.m_bytecodeOffset
  61. && m_operand == other.m_operand;
  62. }
  63. unsigned hash() const
  64. {
  65. return WTF::intHash(m_bytecodeOffset) + m_operand;
  66. }
  67. unsigned bytecodeOffset() const
  68. {
  69. ASSERT(!!*this);
  70. return m_bytecodeOffset;
  71. }
  72. int operand() const
  73. {
  74. ASSERT(!!*this);
  75. return m_operand;
  76. }
  77. bool isHashTableDeletedValue() const
  78. {
  79. return m_operand == -1 && m_bytecodeOffset;
  80. }
  81. private:
  82. unsigned m_bytecodeOffset;
  83. int m_operand;
  84. };
  85. struct LazyOperandValueProfileKeyHash {
  86. static unsigned hash(const LazyOperandValueProfileKey& key) { return key.hash(); }
  87. static bool equal(
  88. const LazyOperandValueProfileKey& a,
  89. const LazyOperandValueProfileKey& b) { return a == b; }
  90. static const bool safeToCompareToEmptyOrDeleted = true;
  91. };
  92. } // namespace JSC
  93. namespace WTF {
  94. template<typename T> struct DefaultHash;
  95. template<> struct DefaultHash<JSC::LazyOperandValueProfileKey> {
  96. typedef JSC::LazyOperandValueProfileKeyHash Hash;
  97. };
  98. template<typename T> struct HashTraits;
  99. template<> struct HashTraits<JSC::LazyOperandValueProfileKey> : public GenericHashTraits<JSC::LazyOperandValueProfileKey> {
  100. static void constructDeletedValue(JSC::LazyOperandValueProfileKey& slot) { new (NotNull, &slot) JSC::LazyOperandValueProfileKey(HashTableDeletedValue); }
  101. static bool isDeletedValue(const JSC::LazyOperandValueProfileKey& value) { return value.isHashTableDeletedValue(); }
  102. };
  103. } // namespace WTF
  104. namespace JSC {
  105. struct LazyOperandValueProfile : public MinimalValueProfile {
  106. LazyOperandValueProfile()
  107. : MinimalValueProfile()
  108. , m_operand(-1)
  109. {
  110. }
  111. explicit LazyOperandValueProfile(const LazyOperandValueProfileKey& key)
  112. : MinimalValueProfile(key.bytecodeOffset())
  113. , m_operand(key.operand())
  114. {
  115. }
  116. LazyOperandValueProfileKey key() const
  117. {
  118. return LazyOperandValueProfileKey(m_bytecodeOffset, m_operand);
  119. }
  120. int m_operand;
  121. typedef SegmentedVector_shared<LazyOperandValueProfile, 8> List;
  122. };
  123. class LazyOperandValueProfileParser;
  124. class CompressedLazyOperandValueProfileHolder {
  125. WTF_MAKE_NONCOPYABLE(CompressedLazyOperandValueProfileHolder);
  126. public:
  127. CompressedLazyOperandValueProfileHolder();
  128. ~CompressedLazyOperandValueProfileHolder();
  129. void computeUpdatedPredictions(OperationInProgress);
  130. LazyOperandValueProfile* add(const LazyOperandValueProfileKey& key);
  131. private:
  132. friend class LazyOperandValueProfileParser;
  133. OwnPtr<LazyOperandValueProfile::List> m_data;
  134. };
  135. class LazyOperandValueProfileParser {
  136. WTF_MAKE_NONCOPYABLE(LazyOperandValueProfileParser);
  137. public:
  138. explicit LazyOperandValueProfileParser(
  139. CompressedLazyOperandValueProfileHolder& holder);
  140. ~LazyOperandValueProfileParser();
  141. LazyOperandValueProfile* getIfPresent(
  142. const LazyOperandValueProfileKey& key) const;
  143. SpeculatedType prediction(const LazyOperandValueProfileKey& key) const;
  144. private:
  145. CompressedLazyOperandValueProfileHolder& m_holder;
  146. HashMap<LazyOperandValueProfileKey, LazyOperandValueProfile*> m_map;
  147. };
  148. } // namespace JSC
  149. #endif // ENABLE(VALUE_PROFILER)
  150. #endif // LazyOperandValueProfile_h