2 * Copyright (C) 2012 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 LazyOperandValueProfile_h
27 #define LazyOperandValueProfile_h
29 #include <wtf/Platform.h>
31 #if ENABLE(VALUE_PROFILER)
33 #include "ValueProfile.h"
34 #include <wtf/HashMap.h>
35 #include <wtf/Noncopyable.h>
36 #include <wtf/OwnPtr.h>
37 #include <wtf/SegmentedVector.h>
41 class ScriptExecutable
;
43 class LazyOperandValueProfileKey
{
45 LazyOperandValueProfileKey()
46 : m_bytecodeOffset(0) // 0 = empty value
47 , m_operand(-1) // not a valid operand index in our current scheme
51 LazyOperandValueProfileKey(WTF::HashTableDeletedValueType
)
52 : m_bytecodeOffset(1) // 1 = deleted value
53 , m_operand(-1) // not a valid operand index in our current scheme
57 LazyOperandValueProfileKey(unsigned bytecodeOffset
, int operand
)
58 : m_bytecodeOffset(bytecodeOffset
)
61 ASSERT(operand
!= -1);
64 bool operator!() const
66 return m_operand
== -1;
69 bool operator==(const LazyOperandValueProfileKey
& other
) const
71 return m_bytecodeOffset
== other
.m_bytecodeOffset
72 && m_operand
== other
.m_operand
;
77 return WTF::intHash(m_bytecodeOffset
) + m_operand
;
80 unsigned bytecodeOffset() const
83 return m_bytecodeOffset
;
91 bool isHashTableDeletedValue() const
93 return m_operand
== -1 && m_bytecodeOffset
;
96 unsigned m_bytecodeOffset
;
100 struct LazyOperandValueProfileKeyHash
{
101 static unsigned hash(const LazyOperandValueProfileKey
& key
) { return key
.hash(); }
103 const LazyOperandValueProfileKey
& a
,
104 const LazyOperandValueProfileKey
& b
) { return a
== b
; }
105 static const bool safeToCompareToEmptyOrDeleted
= true;
112 template<typename T
> struct DefaultHash
;
113 template<> struct DefaultHash
<JSC::LazyOperandValueProfileKey
> {
114 typedef JSC::LazyOperandValueProfileKeyHash Hash
;
117 template<typename T
> struct HashTraits
;
118 template<> struct HashTraits
<JSC::LazyOperandValueProfileKey
> : public GenericHashTraits
<JSC::LazyOperandValueProfileKey
> {
119 static void constructDeletedValue(JSC::LazyOperandValueProfileKey
& slot
) { new (NotNull
, &slot
) JSC::LazyOperandValueProfileKey(HashTableDeletedValue
); }
120 static bool isDeletedValue(const JSC::LazyOperandValueProfileKey
& value
) { return value
.isHashTableDeletedValue(); }
127 struct LazyOperandValueProfile
: public MinimalValueProfile
{
128 LazyOperandValueProfile()
129 : MinimalValueProfile()
134 explicit LazyOperandValueProfile(const LazyOperandValueProfileKey
& key
)
135 : MinimalValueProfile(key
.bytecodeOffset())
136 , m_operand(key
.operand())
140 LazyOperandValueProfileKey
key() const
142 return LazyOperandValueProfileKey(m_bytecodeOffset
, m_operand
);
147 typedef SegmentedVector
<LazyOperandValueProfile
, 8> List
;
150 class LazyOperandValueProfileParser
;
152 class CompressedLazyOperandValueProfileHolder
{
153 WTF_MAKE_NONCOPYABLE(CompressedLazyOperandValueProfileHolder
);
155 CompressedLazyOperandValueProfileHolder();
156 ~CompressedLazyOperandValueProfileHolder();
158 void computeUpdatedPredictions();
160 LazyOperandValueProfile
* add(const LazyOperandValueProfileKey
& key
);
163 friend class LazyOperandValueProfileParser
;
164 OwnPtr
<LazyOperandValueProfile::List
> m_data
;
167 class LazyOperandValueProfileParser
{
168 WTF_MAKE_NONCOPYABLE(LazyOperandValueProfileParser
);
170 explicit LazyOperandValueProfileParser(
171 CompressedLazyOperandValueProfileHolder
& holder
);
172 ~LazyOperandValueProfileParser();
174 LazyOperandValueProfile
* getIfPresent(
175 const LazyOperandValueProfileKey
& key
) const;
177 PredictedType
prediction(const LazyOperandValueProfileKey
& key
) const;
179 CompressedLazyOperandValueProfileHolder
& m_holder
;
180 HashMap
<LazyOperandValueProfileKey
, LazyOperandValueProfile
*> m_map
;
185 #endif // ENABLE(VALUE_PROFILER)
187 #endif // LazyOperandValueProfile_h