1/*
2 * Copyright (C) 2013-2019 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 "DFGCompilationMode.h"
29#include <wtf/HashMap.h>
30
31namespace JSC {
32
33class CodeBlock;
34class CodeBlockSet;
35
36namespace DFG {
37
38class CompilationKey {
39public:
40 CompilationKey()
41 : m_profiledBlock(0)
42 , m_mode(InvalidCompilationMode)
43 {
44 }
45
46 CompilationKey(WTF::HashTableDeletedValueType)
47 : m_profiledBlock(0)
48 , m_mode(DFGMode)
49 {
50 }
51
52 CompilationKey(CodeBlock* profiledBlock, CompilationMode mode)
53 : m_profiledBlock(profiledBlock)
54 , m_mode(mode)
55 {
56 }
57
58 bool operator!() const
59 {
60 return !m_profiledBlock && m_mode == InvalidCompilationMode;
61 }
62
63 bool isHashTableDeletedValue() const
64 {
65 return !m_profiledBlock && m_mode != InvalidCompilationMode;
66 }
67
68 CodeBlock* profiledBlock() const { return m_profiledBlock; }
69 CompilationMode mode() const { return m_mode; }
70
71 bool operator==(const CompilationKey& other) const
72 {
73 return m_profiledBlock == other.m_profiledBlock
74 && m_mode == other.m_mode;
75 }
76
77 unsigned hash() const
78 {
79 return WTF::pairIntHash(WTF::PtrHash<CodeBlock*>::hash(m_profiledBlock), m_mode);
80 }
81
82 void dump(PrintStream&) const;
83
84private:
85 CodeBlock* m_profiledBlock;
86 CompilationMode m_mode;
87};
88
89struct CompilationKeyHash {
90 static unsigned hash(const CompilationKey& key) { return key.hash(); }
91 static bool equal(const CompilationKey& a, const CompilationKey& b) { return a == b; }
92 static constexpr bool safeToCompareToEmptyOrDeleted = true;
93};
94
95} } // namespace JSC::DFG
96
97namespace WTF {
98
99template<typename T> struct DefaultHash;
100template<> struct DefaultHash<JSC::DFG::CompilationKey> {
101 typedef JSC::DFG::CompilationKeyHash Hash;
102};
103
104template<typename T> struct HashTraits;
105template<> struct HashTraits<JSC::DFG::CompilationKey> : SimpleClassHashTraits<JSC::DFG::CompilationKey> { };
106
107} // namespace WTF
108