ValueProfile.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (C) 2011, 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. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  14. * its contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #ifndef ValueProfile_h
  29. #define ValueProfile_h
  30. #include <wtf/Platform.h>
  31. #if ENABLE(VALUE_PROFILER)
  32. #include "Heap.h"
  33. #include "JSArray.h"
  34. #include "SpeculatedType.h"
  35. #include "Structure.h"
  36. #include "WriteBarrier.h"
  37. #include <wtf/PrintStream.h>
  38. #include <wtf/StringPrintStream.h>
  39. namespace JSC {
  40. template<unsigned numberOfBucketsArgument>
  41. struct ValueProfileBase {
  42. static const unsigned numberOfBuckets = numberOfBucketsArgument;
  43. static const unsigned numberOfSpecFailBuckets = 1;
  44. static const unsigned bucketIndexMask = numberOfBuckets - 1;
  45. static const unsigned totalNumberOfBuckets = numberOfBuckets + numberOfSpecFailBuckets;
  46. ValueProfileBase()
  47. : m_bytecodeOffset(-1)
  48. , m_prediction(SpecNone)
  49. , m_numberOfSamplesInPrediction(0)
  50. , m_singletonValueIsTop(false)
  51. {
  52. for (unsigned i = 0; i < totalNumberOfBuckets; ++i)
  53. m_buckets[i] = JSValue::encode(JSValue());
  54. }
  55. ValueProfileBase(int bytecodeOffset)
  56. : m_bytecodeOffset(bytecodeOffset)
  57. , m_prediction(SpecNone)
  58. , m_numberOfSamplesInPrediction(0)
  59. , m_singletonValueIsTop(false)
  60. {
  61. for (unsigned i = 0; i < totalNumberOfBuckets; ++i)
  62. m_buckets[i] = JSValue::encode(JSValue());
  63. }
  64. EncodedJSValue* specFailBucket(unsigned i)
  65. {
  66. ASSERT(numberOfBuckets + i < totalNumberOfBuckets);
  67. return m_buckets + numberOfBuckets + i;
  68. }
  69. const ClassInfo* classInfo(unsigned bucket) const
  70. {
  71. JSValue value = JSValue::decode(m_buckets[bucket]);
  72. if (!!value) {
  73. if (!value.isCell())
  74. return 0;
  75. return value.asCell()->structure()->classInfo();
  76. }
  77. return 0;
  78. }
  79. unsigned numberOfSamples() const
  80. {
  81. unsigned result = 0;
  82. for (unsigned i = 0; i < totalNumberOfBuckets; ++i) {
  83. if (!!JSValue::decode(m_buckets[i]))
  84. result++;
  85. }
  86. return result;
  87. }
  88. unsigned totalNumberOfSamples() const
  89. {
  90. return numberOfSamples() + m_numberOfSamplesInPrediction;
  91. }
  92. bool isLive() const
  93. {
  94. for (unsigned i = 0; i < totalNumberOfBuckets; ++i) {
  95. if (!!JSValue::decode(m_buckets[i]))
  96. return true;
  97. }
  98. return false;
  99. }
  100. CString briefDescription()
  101. {
  102. computeUpdatedPrediction();
  103. StringPrintStream out;
  104. if (m_singletonValueIsTop)
  105. out.print("predicting ", SpeculationDump(m_prediction));
  106. else if (m_singletonValue)
  107. out.print("predicting ", m_singletonValue);
  108. return out.toCString();
  109. }
  110. void dump(PrintStream& out)
  111. {
  112. out.print("samples = ", totalNumberOfSamples(), " prediction = ", SpeculationDump(m_prediction));
  113. out.printf(", value = ");
  114. if (m_singletonValueIsTop)
  115. out.printf("TOP");
  116. else
  117. out.print(m_singletonValue);
  118. bool first = true;
  119. for (unsigned i = 0; i < totalNumberOfBuckets; ++i) {
  120. JSValue value = JSValue::decode(m_buckets[i]);
  121. if (!!value) {
  122. if (first) {
  123. out.printf(": ");
  124. first = false;
  125. } else
  126. out.printf(", ");
  127. out.print(value);
  128. }
  129. }
  130. }
  131. // Updates the prediction and returns the new one.
  132. SpeculatedType computeUpdatedPrediction(OperationInProgress operation = NoOperation)
  133. {
  134. for (unsigned i = 0; i < totalNumberOfBuckets; ++i) {
  135. JSValue value = JSValue::decode(m_buckets[i]);
  136. if (!value)
  137. continue;
  138. m_numberOfSamplesInPrediction++;
  139. mergeSpeculation(m_prediction, speculationFromValue(value));
  140. if (!m_singletonValueIsTop && !!value) {
  141. if (!m_singletonValue)
  142. m_singletonValue = value;
  143. else if (m_singletonValue != value)
  144. m_singletonValueIsTop = true;
  145. }
  146. m_buckets[i] = JSValue::encode(JSValue());
  147. }
  148. if (operation == Collection
  149. && !m_singletonValueIsTop
  150. && !!m_singletonValue
  151. && m_singletonValue.isCell()
  152. && !Heap::isMarked(m_singletonValue.asCell()))
  153. m_singletonValueIsTop = true;
  154. return m_prediction;
  155. }
  156. int m_bytecodeOffset; // -1 for prologue
  157. SpeculatedType m_prediction;
  158. unsigned m_numberOfSamplesInPrediction;
  159. bool m_singletonValueIsTop;
  160. JSValue m_singletonValue;
  161. EncodedJSValue m_buckets[totalNumberOfBuckets];
  162. };
  163. struct MinimalValueProfile : public ValueProfileBase<0> {
  164. MinimalValueProfile(): ValueProfileBase<0>() { }
  165. MinimalValueProfile(int bytecodeOffset): ValueProfileBase<0>(bytecodeOffset) { }
  166. };
  167. template<unsigned logNumberOfBucketsArgument>
  168. struct ValueProfileWithLogNumberOfBuckets : public ValueProfileBase<1 << logNumberOfBucketsArgument> {
  169. static const unsigned logNumberOfBuckets = logNumberOfBucketsArgument;
  170. ValueProfileWithLogNumberOfBuckets()
  171. : ValueProfileBase<1 << logNumberOfBucketsArgument>()
  172. {
  173. }
  174. ValueProfileWithLogNumberOfBuckets(int bytecodeOffset)
  175. : ValueProfileBase<1 << logNumberOfBucketsArgument>(bytecodeOffset)
  176. {
  177. }
  178. };
  179. struct ValueProfile : public ValueProfileWithLogNumberOfBuckets<0> {
  180. ValueProfile(): ValueProfileWithLogNumberOfBuckets<0>() { }
  181. ValueProfile(int bytecodeOffset): ValueProfileWithLogNumberOfBuckets<0>(bytecodeOffset) { }
  182. };
  183. template<typename T>
  184. inline int getValueProfileBytecodeOffset(T* valueProfile)
  185. {
  186. return valueProfile->m_bytecodeOffset;
  187. }
  188. // This is a mini value profile to catch pathologies. It is a counter that gets
  189. // incremented when we take the slow path on any instruction.
  190. struct RareCaseProfile {
  191. RareCaseProfile(int bytecodeOffset)
  192. : m_bytecodeOffset(bytecodeOffset)
  193. , m_counter(0)
  194. {
  195. }
  196. int m_bytecodeOffset;
  197. uint32_t m_counter;
  198. };
  199. inline int getRareCaseProfileBytecodeOffset(RareCaseProfile* rareCaseProfile)
  200. {
  201. return rareCaseProfile->m_bytecodeOffset;
  202. }
  203. } // namespace JSC
  204. #endif // ENABLE(VALUE_PROFILER)
  205. #endif // ValueProfile_h