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