fpizlo@apple.com | 1996b4b | 2012-02-06 06:44:24 +0000 | [diff] [blame] | 1 | /* |
fpizlo@apple.com | 163291d | 2015-04-28 19:27:23 +0000 | [diff] [blame] | 2 | * Copyright (C) 2011, 2013, 2015 Apple Inc. All rights reserved. |
fpizlo@apple.com | 1996b4b | 2012-02-06 06:44:24 +0000 | [diff] [blame] | 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 | |
ryanhaddad@apple.com | 22104f5 | 2016-09-28 17:08:17 +0000 | [diff] [blame] | 26 | #pragma once |
fpizlo@apple.com | 1996b4b | 2012-02-06 06:44:24 +0000 | [diff] [blame] | 27 | |
fpizlo@apple.com | 1996b4b | 2012-02-06 06:44:24 +0000 | [diff] [blame] | 28 | #if ENABLE(DFG_JIT) |
| 29 | |
fpizlo@apple.com | e5abbae | 2012-03-19 21:44:23 +0000 | [diff] [blame] | 30 | #include "DFGEdge.h" |
fpizlo@apple.com | 1996b4b | 2012-02-06 06:44:24 +0000 | [diff] [blame] | 31 | |
| 32 | namespace JSC { namespace DFG { |
| 33 | |
fpizlo@apple.com | e5abbae | 2012-03-19 21:44:23 +0000 | [diff] [blame] | 34 | class AdjacencyList { |
fpizlo@apple.com | 1996b4b | 2012-02-06 06:44:24 +0000 | [diff] [blame] | 35 | public: |
| 36 | enum Kind { |
| 37 | Fixed, |
| 38 | Variable |
| 39 | }; |
fpizlo@apple.com | 79c51ee | 2012-05-18 22:30:24 +0000 | [diff] [blame] | 40 | |
| 41 | enum { Size = 3 }; |
| 42 | |
fpizlo@apple.com | d8dd053 | 2012-09-13 04:18:52 +0000 | [diff] [blame] | 43 | AdjacencyList() { } |
| 44 | |
fpizlo@apple.com | e5abbae | 2012-03-19 21:44:23 +0000 | [diff] [blame] | 45 | AdjacencyList(Kind kind) |
fpizlo@apple.com | 1996b4b | 2012-02-06 06:44:24 +0000 | [diff] [blame] | 46 | { |
| 47 | if (kind == Variable) { |
| 48 | m_words[0].m_encodedWord = UINT_MAX; |
| 49 | m_words[1].m_encodedWord = UINT_MAX; |
| 50 | } |
| 51 | } |
| 52 | |
fpizlo@apple.com | 2c4a7e9 | 2014-08-06 05:27:46 +0000 | [diff] [blame] | 53 | AdjacencyList(Kind kind, Edge child1, Edge child2 = Edge(), Edge child3 = Edge()) |
fpizlo@apple.com | 1996b4b | 2012-02-06 06:44:24 +0000 | [diff] [blame] | 54 | { |
| 55 | ASSERT_UNUSED(kind, kind == Fixed); |
| 56 | initialize(child1, child2, child3); |
| 57 | } |
| 58 | |
fpizlo@apple.com | e5abbae | 2012-03-19 21:44:23 +0000 | [diff] [blame] | 59 | AdjacencyList(Kind kind, unsigned firstChild, unsigned numChildren) |
fpizlo@apple.com | 1996b4b | 2012-02-06 06:44:24 +0000 | [diff] [blame] | 60 | { |
| 61 | ASSERT_UNUSED(kind, kind == Variable); |
| 62 | setFirstChild(firstChild); |
| 63 | setNumChildren(numChildren); |
sbarati@apple.com | e2cdd87 | 2018-02-13 01:12:28 +0000 | [diff] [blame] | 64 | // We need to make sure this is the empty value so equivalent adjacency |
| 65 | // lists produce identical hashes. |
| 66 | m_words[2] = Edge(); |
fpizlo@apple.com | 1996b4b | 2012-02-06 06:44:24 +0000 | [diff] [blame] | 67 | } |
| 68 | |
fpizlo@apple.com | b41e682 | 2014-07-25 20:55:17 +0000 | [diff] [blame] | 69 | bool isEmpty() const { return !child1(); } |
| 70 | |
fpizlo@apple.com | e5abbae | 2012-03-19 21:44:23 +0000 | [diff] [blame] | 71 | const Edge& child(unsigned i) const |
fpizlo@apple.com | 1996b4b | 2012-02-06 06:44:24 +0000 | [diff] [blame] | 72 | { |
fpizlo@apple.com | 79c51ee | 2012-05-18 22:30:24 +0000 | [diff] [blame] | 73 | ASSERT(i < Size); |
fpizlo@apple.com | 1996b4b | 2012-02-06 06:44:24 +0000 | [diff] [blame] | 74 | return m_words[i]; |
| 75 | } |
| 76 | |
fpizlo@apple.com | e5abbae | 2012-03-19 21:44:23 +0000 | [diff] [blame] | 77 | Edge& child(unsigned i) |
fpizlo@apple.com | 1996b4b | 2012-02-06 06:44:24 +0000 | [diff] [blame] | 78 | { |
fpizlo@apple.com | 79c51ee | 2012-05-18 22:30:24 +0000 | [diff] [blame] | 79 | ASSERT(i < Size); |
fpizlo@apple.com | 1996b4b | 2012-02-06 06:44:24 +0000 | [diff] [blame] | 80 | return m_words[i]; |
| 81 | } |
| 82 | |
fpizlo@apple.com | e5abbae | 2012-03-19 21:44:23 +0000 | [diff] [blame] | 83 | void setChild(unsigned i, Edge nodeUse) |
fpizlo@apple.com | 1996b4b | 2012-02-06 06:44:24 +0000 | [diff] [blame] | 84 | { |
fpizlo@apple.com | 79c51ee | 2012-05-18 22:30:24 +0000 | [diff] [blame] | 85 | ASSERT(i < Size); |
fpizlo@apple.com | 1996b4b | 2012-02-06 06:44:24 +0000 | [diff] [blame] | 86 | m_words[i] = nodeUse; |
| 87 | } |
| 88 | |
fpizlo@apple.com | e5abbae | 2012-03-19 21:44:23 +0000 | [diff] [blame] | 89 | Edge child1() const { return child(0); } |
| 90 | Edge child2() const { return child(1); } |
| 91 | Edge child3() const { return child(2); } |
fpizlo@apple.com | 1996b4b | 2012-02-06 06:44:24 +0000 | [diff] [blame] | 92 | |
fpizlo@apple.com | e5abbae | 2012-03-19 21:44:23 +0000 | [diff] [blame] | 93 | Edge& child1() { return child(0); } |
| 94 | Edge& child2() { return child(1); } |
| 95 | Edge& child3() { return child(2); } |
fpizlo@apple.com | 1996b4b | 2012-02-06 06:44:24 +0000 | [diff] [blame] | 96 | |
fpizlo@apple.com | e5abbae | 2012-03-19 21:44:23 +0000 | [diff] [blame] | 97 | void setChild1(Edge nodeUse) { setChild(0, nodeUse); } |
| 98 | void setChild2(Edge nodeUse) { setChild(1, nodeUse); } |
| 99 | void setChild3(Edge nodeUse) { setChild(2, nodeUse); } |
fpizlo@apple.com | 1996b4b | 2012-02-06 06:44:24 +0000 | [diff] [blame] | 100 | |
fpizlo@apple.com | e5abbae | 2012-03-19 21:44:23 +0000 | [diff] [blame] | 101 | Edge child1Unchecked() const { return m_words[0]; } |
fpizlo@apple.com | 1996b4b | 2012-02-06 06:44:24 +0000 | [diff] [blame] | 102 | |
oliver@apple.com | 827d2cf | 2013-07-25 04:04:45 +0000 | [diff] [blame] | 103 | Edge justOneChild() const |
| 104 | { |
| 105 | if (!!child1() && !child2()) { |
| 106 | ASSERT(!child3()); |
| 107 | return child1(); |
| 108 | } |
| 109 | return Edge(); |
| 110 | } |
| 111 | |
fpizlo@apple.com | e5abbae | 2012-03-19 21:44:23 +0000 | [diff] [blame] | 112 | void initialize(Edge child1, Edge child2, Edge child3) |
fpizlo@apple.com | 1996b4b | 2012-02-06 06:44:24 +0000 | [diff] [blame] | 113 | { |
| 114 | child(0) = child1; |
| 115 | child(1) = child2; |
| 116 | child(2) = child3; |
| 117 | } |
| 118 | |
fpizlo@apple.com | 8ff092f | 2013-01-29 08:01:03 +0000 | [diff] [blame] | 119 | void initialize(Node* child1 = 0, Node* child2 = 0, Node* child3 = 0) |
fpizlo@apple.com | 1996b4b | 2012-02-06 06:44:24 +0000 | [diff] [blame] | 120 | { |
fpizlo@apple.com | e5abbae | 2012-03-19 21:44:23 +0000 | [diff] [blame] | 121 | initialize(Edge(child1), Edge(child2), Edge(child3)); |
fpizlo@apple.com | 1996b4b | 2012-02-06 06:44:24 +0000 | [diff] [blame] | 122 | } |
fpizlo@apple.com | 3187c92 | 2012-05-18 21:47:53 +0000 | [diff] [blame] | 123 | |
| 124 | void reset() |
| 125 | { |
fpizlo@apple.com | 3187c92 | 2012-05-18 21:47:53 +0000 | [diff] [blame] | 126 | initialize(); |
| 127 | } |
fpizlo@apple.com | 9a548f1 | 2012-05-24 05:33:09 +0000 | [diff] [blame] | 128 | |
fpizlo@apple.com | 4463e44 | 2013-03-20 20:29:37 +0000 | [diff] [blame] | 129 | // Call this if you wish to remove an edge and the node treats the list of children. |
| 130 | void removeEdge(unsigned edgeIndex) |
fpizlo@apple.com | 9a548f1 | 2012-05-24 05:33:09 +0000 | [diff] [blame] | 131 | { |
| 132 | for (unsigned i = edgeIndex; i < Size - 1; ++i) |
| 133 | setChild(i, child(i + 1)); |
| 134 | setChild(Size - 1, Edge()); |
| 135 | } |
fpizlo@apple.com | 2c4a7e9 | 2014-08-06 05:27:46 +0000 | [diff] [blame] | 136 | |
fpizlo@apple.com | 1996b4b | 2012-02-06 06:44:24 +0000 | [diff] [blame] | 137 | unsigned firstChild() const |
| 138 | { |
fpizlo@apple.com | 1996b4b | 2012-02-06 06:44:24 +0000 | [diff] [blame] | 139 | return m_words[0].m_encodedWord; |
| 140 | } |
| 141 | void setFirstChild(unsigned firstChild) |
| 142 | { |
fpizlo@apple.com | 1996b4b | 2012-02-06 06:44:24 +0000 | [diff] [blame] | 143 | m_words[0].m_encodedWord = firstChild; |
| 144 | } |
| 145 | |
| 146 | unsigned numChildren() const |
| 147 | { |
fpizlo@apple.com | 1996b4b | 2012-02-06 06:44:24 +0000 | [diff] [blame] | 148 | return m_words[1].m_encodedWord; |
| 149 | } |
| 150 | void setNumChildren(unsigned numChildren) |
| 151 | { |
fpizlo@apple.com | 1996b4b | 2012-02-06 06:44:24 +0000 | [diff] [blame] | 152 | m_words[1].m_encodedWord = numChildren; |
| 153 | } |
| 154 | |
fpizlo@apple.com | 2c4a7e9 | 2014-08-06 05:27:46 +0000 | [diff] [blame] | 155 | AdjacencyList sanitized() const |
| 156 | { |
| 157 | return AdjacencyList(Fixed, child1().sanitized(), child2().sanitized(), child3().sanitized()); |
| 158 | } |
| 159 | |
fpizlo@apple.com | 163291d | 2015-04-28 19:27:23 +0000 | [diff] [blame] | 160 | AdjacencyList justChecks() const |
| 161 | { |
| 162 | AdjacencyList result(Fixed); |
| 163 | unsigned sourceIndex = 0; |
| 164 | unsigned targetIndex = 0; |
| 165 | while (sourceIndex < AdjacencyList::Size) { |
| 166 | Edge edge = child(sourceIndex++); |
| 167 | if (!edge) |
| 168 | break; |
| 169 | if (edge.willHaveCheck()) |
| 170 | result.child(targetIndex++) = edge; |
| 171 | } |
| 172 | return result; |
| 173 | } |
| 174 | |
fpizlo@apple.com | 2c4a7e9 | 2014-08-06 05:27:46 +0000 | [diff] [blame] | 175 | unsigned hash() const |
| 176 | { |
| 177 | unsigned result = 0; |
| 178 | if (!child1()) |
| 179 | return result; |
| 180 | |
| 181 | result += child1().hash(); |
| 182 | |
| 183 | if (!child2()) |
| 184 | return result; |
| 185 | |
| 186 | result *= 3; |
| 187 | result += child2().hash(); |
| 188 | |
| 189 | if (!child3()) |
| 190 | return result; |
| 191 | |
| 192 | result *= 3; |
| 193 | result += child3().hash(); |
| 194 | |
| 195 | return result; |
| 196 | } |
| 197 | |
| 198 | bool operator==(const AdjacencyList& other) const |
| 199 | { |
| 200 | return child1() == other.child1() |
| 201 | && child2() == other.child2() |
| 202 | && child3() == other.child3(); |
| 203 | } |
| 204 | |
fpizlo@apple.com | 1996b4b | 2012-02-06 06:44:24 +0000 | [diff] [blame] | 205 | private: |
fpizlo@apple.com | 79c51ee | 2012-05-18 22:30:24 +0000 | [diff] [blame] | 206 | Edge m_words[Size]; |
fpizlo@apple.com | 1996b4b | 2012-02-06 06:44:24 +0000 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | } } // namespace JSC::DFG |
| 210 | |
| 211 | #endif // ENABLE(DFG_JIT) |