]> git.saurik.com Git - apple/javascriptcore.git/blame - bytecode/DFGExitProfile.h
JavaScriptCore-1218.tar.gz
[apple/javascriptcore.git] / bytecode / DFGExitProfile.h
CommitLineData
6fe7ccc8 1/*
93a37866 2 * Copyright (C) 2011, 2012 Apple Inc. All rights reserved.
6fe7ccc8
A
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
93a37866 29#include "ExitKind.h"
6fe7ccc8
A
30#include <wtf/HashSet.h>
31#include <wtf/OwnPtr.h>
32#include <wtf/Vector.h>
33
34namespace JSC { namespace DFG {
35
6fe7ccc8
A
36class FrequentExitSite {
37public:
38 FrequentExitSite()
39 : m_bytecodeOffset(0) // 0 = empty value
40 , m_kind(ExitKindUnset)
41 {
42 }
43
44 FrequentExitSite(WTF::HashTableDeletedValueType)
45 : m_bytecodeOffset(1) // 1 = deleted value
46 , m_kind(ExitKindUnset)
47 {
48 }
49
50 explicit FrequentExitSite(unsigned bytecodeOffset, ExitKind kind)
51 : m_bytecodeOffset(bytecodeOffset)
52 , m_kind(kind)
53 {
54 ASSERT(exitKindIsCountable(kind));
55 }
56
93a37866
A
57 // Use this constructor if you wish for the exit site to be counted globally within its
58 // code block.
59 explicit FrequentExitSite(ExitKind kind)
60 : m_bytecodeOffset(0)
61 , m_kind(kind)
62 {
63 ASSERT(exitKindIsCountable(kind));
64 }
65
6fe7ccc8
A
66 bool operator!() const
67 {
68 return m_kind == ExitKindUnset;
69 }
70
71 bool operator==(const FrequentExitSite& other) const
72 {
73 return m_bytecodeOffset == other.m_bytecodeOffset
74 && m_kind == other.m_kind;
75 }
76
77 unsigned hash() const
78 {
79 return WTF::intHash(m_bytecodeOffset) + m_kind;
80 }
81
82 unsigned bytecodeOffset() const { return m_bytecodeOffset; }
83 ExitKind kind() const { return m_kind; }
84
85 bool isHashTableDeletedValue() const
86 {
87 return m_kind == ExitKindUnset && m_bytecodeOffset;
88 }
89
90private:
91 unsigned m_bytecodeOffset;
92 ExitKind m_kind;
93};
94
95struct FrequentExitSiteHash {
96 static unsigned hash(const FrequentExitSite& key) { return key.hash(); }
97 static bool equal(const FrequentExitSite& a, const FrequentExitSite& b) { return a == b; }
98 static const bool safeToCompareToEmptyOrDeleted = true;
99};
100
101} } // namespace JSC::DFG
102
103namespace WTF {
104
105template<typename T> struct DefaultHash;
106template<> struct DefaultHash<JSC::DFG::FrequentExitSite> {
107 typedef JSC::DFG::FrequentExitSiteHash Hash;
108};
109
110template<typename T> struct HashTraits;
111template<> struct HashTraits<JSC::DFG::FrequentExitSite> : SimpleClassHashTraits<JSC::DFG::FrequentExitSite> { };
112
113} // namespace WTF
114
115namespace JSC { namespace DFG {
116
117class QueryableExitProfile;
118
119class ExitProfile {
120public:
121 ExitProfile();
122 ~ExitProfile();
123
124 // Add a new frequent exit site. Return true if this is a new one, or false
125 // if we already knew about it. This is an O(n) operation, because it errs
126 // on the side of keeping the data structure compact. Also, this will only
127 // be called a fixed number of times per recompilation. Recompilation is
128 // rare to begin with, and implies doing O(n) operations on the CodeBlock
129 // anyway.
130 bool add(const FrequentExitSite&);
131
93a37866
A
132 // Get the frequent exit sites for a bytecode index. This is O(n), and is
133 // meant to only be used from debugging/profiling code.
134 Vector<FrequentExitSite> exitSitesFor(unsigned bytecodeIndex);
135
136 // This is O(n) and should be called on less-frequently executed code paths
137 // in the compiler. It should be strictly cheaper than building a
138 // QueryableExitProfile, if you really expect this to be called infrequently
139 // and you believe that there are few exit sites.
140 bool hasExitSite(const FrequentExitSite&) const;
141 bool hasExitSite(ExitKind kind) const
142 {
143 return hasExitSite(FrequentExitSite(kind));
144 }
145 bool hasExitSite(unsigned bytecodeIndex, ExitKind kind) const
146 {
147 return hasExitSite(FrequentExitSite(bytecodeIndex, kind));
148 }
149
6fe7ccc8
A
150private:
151 friend class QueryableExitProfile;
152
153 OwnPtr<Vector<FrequentExitSite> > m_frequentExitSites;
154};
155
156class QueryableExitProfile {
157public:
158 explicit QueryableExitProfile(const ExitProfile&);
159 ~QueryableExitProfile();
160
161 bool hasExitSite(const FrequentExitSite& site) const
162 {
163 return m_frequentExitSites.find(site) != m_frequentExitSites.end();
164 }
165
93a37866
A
166 bool hasExitSite(ExitKind kind) const
167 {
168 return hasExitSite(FrequentExitSite(kind));
169 }
170
6fe7ccc8
A
171 bool hasExitSite(unsigned bytecodeIndex, ExitKind kind) const
172 {
173 return hasExitSite(FrequentExitSite(bytecodeIndex, kind));
174 }
175private:
176 HashSet<FrequentExitSite> m_frequentExitSites;
177};
178
179} } // namespace JSC::DFG
180
181#endif // DFGExitProfile_h