1/*
2 * Copyright (C) 2016 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#if ENABLE(DFG_JIT)
29
30#include "DFGNode.h"
31#include <wtf/HashTable.h>
32
33namespace JSC { namespace DFG {
34
35class NodeFlowProjection {
36public:
37 enum Kind {
38 Primary,
39 Shadow
40 };
41
42 NodeFlowProjection() { }
43
44 NodeFlowProjection(Node* node)
45 : m_word(bitwise_cast<uintptr_t>(node))
46 {
47 ASSERT(kind() == Primary);
48 }
49
50 NodeFlowProjection(Node* node, Kind kind)
51 : m_word(bitwise_cast<uintptr_t>(node) | (kind == Shadow ? shadowBit : 0))
52 {
53 ASSERT(this->kind() == kind);
54 }
55
56 NodeFlowProjection(WTF::HashTableDeletedValueType)
57 : m_word(shadowBit)
58 {
59 }
60
61 explicit operator bool() const { return !!m_word; }
62
63 Kind kind() const { return (m_word & shadowBit) ? Shadow : Primary; }
64
65 Node* node() const { return bitwise_cast<Node*>(m_word & ~shadowBit); }
66
67 Node& operator*() const { return *node(); }
68 Node* operator->() const { return node(); }
69
70 unsigned hash() const
71 {
72 return m_word;
73 }
74
75 bool operator==(NodeFlowProjection other) const
76 {
77 return m_word == other.m_word;
78 }
79
80 bool operator!=(NodeFlowProjection other) const
81 {
82 return !(*this == other);
83 }
84
85 bool operator<(NodeFlowProjection other) const
86 {
87 if (kind() != other.kind())
88 return kind() < other.kind();
89 return node() < other.node();
90 }
91
92 bool operator>(NodeFlowProjection other) const
93 {
94 return other < *this;
95 }
96
97 bool operator<=(NodeFlowProjection other) const
98 {
99 return !(*this > other);
100 }
101
102 bool operator>=(NodeFlowProjection other) const
103 {
104 return !(*this < other);
105 }
106
107 bool isHashTableDeletedValue() const
108 {
109 return *this == NodeFlowProjection(WTF::HashTableDeletedValue);
110 }
111
112 // Phi shadow projections can become invalid because the Phi might be folded to something else.
113 bool isStillValid() const
114 {
115 return *this && (kind() == Primary || node()->op() == Phi);
116 }
117
118 void dump(PrintStream&) const;
119
120 template<typename Func>
121 static void forEach(Node* node, const Func& func)
122 {
123 func(NodeFlowProjection(node));
124 if (node->op() == Phi)
125 func(NodeFlowProjection(node, Shadow));
126 }
127
128public:
129 static const uintptr_t shadowBit = 1;
130
131 uintptr_t m_word { 0 };
132};
133
134struct NodeFlowProjectionHash {
135 static unsigned hash(NodeFlowProjection key) { return key.hash(); }
136 static bool equal(NodeFlowProjection a, NodeFlowProjection b) { return a == b; }
137 static const bool safeToCompareToEmptyOrDeleted = true;
138};
139
140} } // namespace JSC::DFG
141
142namespace WTF {
143
144template<typename T> struct DefaultHash;
145template<> struct DefaultHash<JSC::DFG::NodeFlowProjection> {
146 typedef JSC::DFG::NodeFlowProjectionHash Hash;
147};
148
149template<typename T> struct HashTraits;
150template<> struct HashTraits<JSC::DFG::NodeFlowProjection> : SimpleClassHashTraits<JSC::DFG::NodeFlowProjection> { };
151
152} // namespace WTF
153
154#endif // ENABLE(DFG_JIT)
155
156