2 * Copyright (C) 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef DFGExitProfile_h
27 #define DFGExitProfile_h
29 #include <wtf/HashSet.h>
30 #include <wtf/OwnPtr.h>
31 #include <wtf/Vector.h>
33 namespace JSC { namespace DFG {
37 BadType, // We exited because a type prediction was wrong.
38 BadCache, // We exited because an inline cache was wrong.
39 Overflow, // We exited because of overflow.
40 NegativeZero, // We exited because we encountered negative zero.
41 InadequateCoverage, // We exited because we ended up in code that didn't have profiling coverage.
42 ArgumentsEscaped, // We exited because arguments escaped but we didn't expect them to.
43 Uncountable, // We exited for none of the above reasons, and we should not count it. Most uses of this should be viewed as a FIXME.
44 UncountableWatchpoint // We exited because of a watchpoint, which isn't counted because watchpoints do tracking themselves.
47 inline const char* exitKindToString(ExitKind kind)
59 return "NegativeZero";
60 case InadequateCoverage:
61 return "InadequateCoverage";
62 case ArgumentsEscaped:
63 return "ArgumentsEscaped";
66 case UncountableWatchpoint:
67 return "UncountableWatchpoint";
73 inline bool exitKindIsCountable(ExitKind kind)
80 case UncountableWatchpoint:
87 class FrequentExitSite {
90 : m_bytecodeOffset(0) // 0 = empty value
91 , m_kind(ExitKindUnset)
95 FrequentExitSite(WTF::HashTableDeletedValueType)
96 : m_bytecodeOffset(1) // 1 = deleted value
97 , m_kind(ExitKindUnset)
101 explicit FrequentExitSite(unsigned bytecodeOffset, ExitKind kind)
102 : m_bytecodeOffset(bytecodeOffset)
105 ASSERT(exitKindIsCountable(kind));
108 // Use this constructor if you wish for the exit site to be counted globally within its
110 explicit FrequentExitSite(ExitKind kind)
111 : m_bytecodeOffset(0)
114 ASSERT(exitKindIsCountable(kind));
117 bool operator!() const
119 return m_kind == ExitKindUnset;
122 bool operator==(const FrequentExitSite& other) const
124 return m_bytecodeOffset == other.m_bytecodeOffset
125 && m_kind == other.m_kind;
128 unsigned hash() const
130 return WTF::intHash(m_bytecodeOffset) + m_kind;
133 unsigned bytecodeOffset() const { return m_bytecodeOffset; }
134 ExitKind kind() const { return m_kind; }
136 bool isHashTableDeletedValue() const
138 return m_kind == ExitKindUnset && m_bytecodeOffset;
142 unsigned m_bytecodeOffset;
146 struct FrequentExitSiteHash {
147 static unsigned hash(const FrequentExitSite& key) { return key.hash(); }
148 static bool equal(const FrequentExitSite& a, const FrequentExitSite& b) { return a == b; }
149 static const bool safeToCompareToEmptyOrDeleted = true;
152 } } // namespace JSC::DFG
156 template<typename T> struct DefaultHash;
157 template<> struct DefaultHash<JSC::DFG::FrequentExitSite> {
158 typedef JSC::DFG::FrequentExitSiteHash Hash;
161 template<typename T> struct HashTraits;
162 template<> struct HashTraits<JSC::DFG::FrequentExitSite> : SimpleClassHashTraits<JSC::DFG::FrequentExitSite> { };
166 namespace JSC { namespace DFG {
168 class QueryableExitProfile;
175 // Add a new frequent exit site. Return true if this is a new one, or false
176 // if we already knew about it. This is an O(n) operation, because it errs
177 // on the side of keeping the data structure compact. Also, this will only
178 // be called a fixed number of times per recompilation. Recompilation is
179 // rare to begin with, and implies doing O(n) operations on the CodeBlock
181 bool add(const FrequentExitSite&);
184 friend class QueryableExitProfile;
186 OwnPtr<Vector<FrequentExitSite> > m_frequentExitSites;
189 class QueryableExitProfile {
191 explicit QueryableExitProfile(const ExitProfile&);
192 ~QueryableExitProfile();
194 bool hasExitSite(const FrequentExitSite& site) const
196 return m_frequentExitSites.find(site) != m_frequentExitSites.end();
199 bool hasExitSite(ExitKind kind) const
201 return hasExitSite(FrequentExitSite(kind));
204 bool hasExitSite(unsigned bytecodeIndex, ExitKind kind) const
206 return hasExitSite(FrequentExitSite(bytecodeIndex, kind));
209 HashSet<FrequentExitSite> m_frequentExitSites;
212 } } // namespace JSC::DFG
214 #endif // DFGExitProfile_h