1/*
2 * Copyright (C) 2012, 2013 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#pragma once
27
28#include "ConcurrentJSLock.h"
29#include "ValueProfile.h"
30#include "VirtualRegister.h"
31#include <wtf/HashMap.h>
32#include <wtf/Noncopyable.h>
33#include <wtf/SegmentedVector.h>
34
35namespace JSC {
36
37class ScriptExecutable;
38
39class LazyOperandValueProfileKey {
40public:
41 LazyOperandValueProfileKey()
42 : m_bytecodeOffset(0) // 0 = empty value
43 , m_operand(VirtualRegister()) // not a valid operand index in our current scheme
44 {
45 }
46
47 LazyOperandValueProfileKey(WTF::HashTableDeletedValueType)
48 : m_bytecodeOffset(1) // 1 = deleted value
49 , m_operand(VirtualRegister()) // not a valid operand index in our current scheme
50 {
51 }
52
53 LazyOperandValueProfileKey(unsigned bytecodeOffset, VirtualRegister operand)
54 : m_bytecodeOffset(bytecodeOffset)
55 , m_operand(operand)
56 {
57 ASSERT(m_operand.isValid());
58 }
59
60 bool operator!() const
61 {
62 return !m_operand.isValid();
63 }
64
65 bool operator==(const LazyOperandValueProfileKey& other) const
66 {
67 return m_bytecodeOffset == other.m_bytecodeOffset
68 && m_operand == other.m_operand;
69 }
70
71 unsigned hash() const
72 {
73 return WTF::intHash(m_bytecodeOffset) + m_operand.offset();
74 }
75
76 unsigned bytecodeOffset() const
77 {
78 ASSERT(!!*this);
79 return m_bytecodeOffset;
80 }
81
82 VirtualRegister operand() const
83 {
84 ASSERT(!!*this);
85 return m_operand;
86 }
87
88 bool isHashTableDeletedValue() const
89 {
90 return !m_operand.isValid() && m_bytecodeOffset;
91 }
92private:
93 unsigned m_bytecodeOffset;
94 VirtualRegister m_operand;
95};
96
97struct LazyOperandValueProfileKeyHash {
98 static unsigned hash(const LazyOperandValueProfileKey& key) { return key.hash(); }
99 static bool equal(
100 const LazyOperandValueProfileKey& a,
101 const LazyOperandValueProfileKey& b) { return a == b; }
102 static const bool safeToCompareToEmptyOrDeleted = true;
103};
104
105} // namespace JSC
106
107namespace WTF {
108
109template<typename T> struct DefaultHash;
110template<> struct DefaultHash<JSC::LazyOperandValueProfileKey> {
111 typedef JSC::LazyOperandValueProfileKeyHash Hash;
112};
113
114template<typename T> struct HashTraits;
115template<> struct HashTraits<JSC::LazyOperandValueProfileKey> : public GenericHashTraits<JSC::LazyOperandValueProfileKey> {
116 static void constructDeletedValue(JSC::LazyOperandValueProfileKey& slot) { new (NotNull, &slot) JSC::LazyOperandValueProfileKey(HashTableDeletedValue); }
117 static bool isDeletedValue(const JSC::LazyOperandValueProfileKey& value) { return value.isHashTableDeletedValue(); }
118};
119
120} // namespace WTF
121
122namespace JSC {
123
124struct LazyOperandValueProfile : public MinimalValueProfile {
125 LazyOperandValueProfile()
126 : MinimalValueProfile()
127 , m_operand(VirtualRegister())
128 {
129 }
130
131 explicit LazyOperandValueProfile(const LazyOperandValueProfileKey& key)
132 : MinimalValueProfile()
133 , m_key(key)
134 {
135 }
136
137 LazyOperandValueProfileKey key() const
138 {
139 return m_key;
140 }
141
142 VirtualRegister m_operand;
143 LazyOperandValueProfileKey m_key;
144
145 typedef SegmentedVector<LazyOperandValueProfile, 8> List;
146};
147
148class LazyOperandValueProfileParser;
149
150class CompressedLazyOperandValueProfileHolder {
151 WTF_MAKE_NONCOPYABLE(CompressedLazyOperandValueProfileHolder);
152public:
153 CompressedLazyOperandValueProfileHolder();
154 ~CompressedLazyOperandValueProfileHolder();
155
156 void computeUpdatedPredictions(const ConcurrentJSLocker&);
157
158 LazyOperandValueProfile* add(
159 const ConcurrentJSLocker&, const LazyOperandValueProfileKey& key);
160
161private:
162 friend class LazyOperandValueProfileParser;
163 std::unique_ptr<LazyOperandValueProfile::List> m_data;
164};
165
166class LazyOperandValueProfileParser {
167 WTF_MAKE_NONCOPYABLE(LazyOperandValueProfileParser);
168public:
169 explicit LazyOperandValueProfileParser();
170 ~LazyOperandValueProfileParser();
171
172 void initialize(
173 const ConcurrentJSLocker&, CompressedLazyOperandValueProfileHolder& holder);
174
175 LazyOperandValueProfile* getIfPresent(
176 const LazyOperandValueProfileKey& key) const;
177
178 SpeculatedType prediction(
179 const ConcurrentJSLocker&, const LazyOperandValueProfileKey& key) const;
180private:
181 HashMap<LazyOperandValueProfileKey, LazyOperandValueProfile*> m_map;
182};
183
184} // namespace JSC
185