]> git.saurik.com Git - apple/javascriptcore.git/blob - bytecode/DFGExitProfile.h
31db084f59509f671d4daeba3514303a4c85e939
[apple/javascriptcore.git] / bytecode / DFGExitProfile.h
1 /*
2 * Copyright (C) 2011 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 #ifndef DFGExitProfile_h
27 #define DFGExitProfile_h
28
29 #include <wtf/HashSet.h>
30 #include <wtf/OwnPtr.h>
31 #include <wtf/Vector.h>
32
33 namespace JSC { namespace DFG {
34
35 enum ExitKind {
36 ExitKindUnset,
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 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.
43 };
44
45 inline const char* exitKindToString(ExitKind kind)
46 {
47 switch (kind) {
48 case ExitKindUnset:
49 return "Unset";
50 case BadType:
51 return "BadType";
52 case BadCache:
53 return "BadCache";
54 case Overflow:
55 return "Overflow";
56 case NegativeZero:
57 return "NegativeZero";
58 case InadequateCoverage:
59 return "InadequateCoverage";
60 default:
61 return "Unknown";
62 }
63 }
64
65 inline bool exitKindIsCountable(ExitKind kind)
66 {
67 switch (kind) {
68 case ExitKindUnset:
69 ASSERT_NOT_REACHED();
70 case BadType:
71 case Uncountable:
72 return false;
73 default:
74 return true;
75 }
76 }
77
78 class FrequentExitSite {
79 public:
80 FrequentExitSite()
81 : m_bytecodeOffset(0) // 0 = empty value
82 , m_kind(ExitKindUnset)
83 {
84 }
85
86 FrequentExitSite(WTF::HashTableDeletedValueType)
87 : m_bytecodeOffset(1) // 1 = deleted value
88 , m_kind(ExitKindUnset)
89 {
90 }
91
92 explicit FrequentExitSite(unsigned bytecodeOffset, ExitKind kind)
93 : m_bytecodeOffset(bytecodeOffset)
94 , m_kind(kind)
95 {
96 ASSERT(exitKindIsCountable(kind));
97 }
98
99 bool operator!() const
100 {
101 return m_kind == ExitKindUnset;
102 }
103
104 bool operator==(const FrequentExitSite& other) const
105 {
106 return m_bytecodeOffset == other.m_bytecodeOffset
107 && m_kind == other.m_kind;
108 }
109
110 unsigned hash() const
111 {
112 return WTF::intHash(m_bytecodeOffset) + m_kind;
113 }
114
115 unsigned bytecodeOffset() const { return m_bytecodeOffset; }
116 ExitKind kind() const { return m_kind; }
117
118 bool isHashTableDeletedValue() const
119 {
120 return m_kind == ExitKindUnset && m_bytecodeOffset;
121 }
122
123 private:
124 unsigned m_bytecodeOffset;
125 ExitKind m_kind;
126 };
127
128 struct FrequentExitSiteHash {
129 static unsigned hash(const FrequentExitSite& key) { return key.hash(); }
130 static bool equal(const FrequentExitSite& a, const FrequentExitSite& b) { return a == b; }
131 static const bool safeToCompareToEmptyOrDeleted = true;
132 };
133
134 } } // namespace JSC::DFG
135
136 namespace WTF {
137
138 template<typename T> struct DefaultHash;
139 template<> struct DefaultHash<JSC::DFG::FrequentExitSite> {
140 typedef JSC::DFG::FrequentExitSiteHash Hash;
141 };
142
143 template<typename T> struct HashTraits;
144 template<> struct HashTraits<JSC::DFG::FrequentExitSite> : SimpleClassHashTraits<JSC::DFG::FrequentExitSite> { };
145
146 } // namespace WTF
147
148 namespace JSC { namespace DFG {
149
150 class QueryableExitProfile;
151
152 class ExitProfile {
153 public:
154 ExitProfile();
155 ~ExitProfile();
156
157 // Add a new frequent exit site. Return true if this is a new one, or false
158 // if we already knew about it. This is an O(n) operation, because it errs
159 // on the side of keeping the data structure compact. Also, this will only
160 // be called a fixed number of times per recompilation. Recompilation is
161 // rare to begin with, and implies doing O(n) operations on the CodeBlock
162 // anyway.
163 bool add(const FrequentExitSite&);
164
165 private:
166 friend class QueryableExitProfile;
167
168 OwnPtr<Vector<FrequentExitSite> > m_frequentExitSites;
169 };
170
171 class QueryableExitProfile {
172 public:
173 explicit QueryableExitProfile(const ExitProfile&);
174 ~QueryableExitProfile();
175
176 bool hasExitSite(const FrequentExitSite& site) const
177 {
178 return m_frequentExitSites.find(site) != m_frequentExitSites.end();
179 }
180
181 bool hasExitSite(unsigned bytecodeIndex, ExitKind kind) const
182 {
183 return hasExitSite(FrequentExitSite(bytecodeIndex, kind));
184 }
185 private:
186 HashSet<FrequentExitSite> m_frequentExitSites;
187 };
188
189 } } // namespace JSC::DFG
190
191 #endif // DFGExitProfile_h