fpizlo@apple.com | 3187c92 | 2012-05-18 21:47:53 +0000 | [diff] [blame] | 1 | /* |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 2 | * Copyright (C) 2012-2015 Apple Inc. All rights reserved. |
fpizlo@apple.com | 3187c92 | 2012-05-18 21:47:53 +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 | |
| 26 | #include "config.h" |
ossy@webkit.org | beb0de4 | 2014-02-17 19:00:03 +0000 | [diff] [blame] | 27 | #include "DFGConstantFoldingPhase.h" |
fpizlo@apple.com | 3187c92 | 2012-05-18 21:47:53 +0000 | [diff] [blame] | 28 | |
| 29 | #if ENABLE(DFG_JIT) |
| 30 | |
oliver@apple.com | 55d32d9 | 2013-07-25 04:05:03 +0000 | [diff] [blame] | 31 | #include "DFGAbstractInterpreterInlines.h" |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 32 | #include "DFGArgumentsUtilities.h" |
fpizlo@apple.com | ead5edd | 2015-10-09 00:13:58 +0000 | [diff] [blame] | 33 | #include "DFGBasicBlockInlines.h" |
fpizlo@apple.com | 0bef2a1 | 2014-02-10 19:26:29 +0000 | [diff] [blame] | 34 | #include "DFGGraph.h" |
oliver@apple.com | 55d32d9 | 2013-07-25 04:05:03 +0000 | [diff] [blame] | 35 | #include "DFGInPlaceAbstractState.h" |
fpizlo@apple.com | 1283577 | 2015-09-21 20:49:04 +0000 | [diff] [blame] | 36 | #include "DFGInferredTypeCheck.h" |
fpizlo@apple.com | 3187c92 | 2012-05-18 21:47:53 +0000 | [diff] [blame] | 37 | #include "DFGInsertionSet.h" |
| 38 | #include "DFGPhase.h" |
fpizlo@apple.com | 0bef2a1 | 2014-02-10 19:26:29 +0000 | [diff] [blame] | 39 | #include "GetByIdStatus.h" |
fpizlo@apple.com | fb7eff2 | 2014-02-11 01:45:50 +0000 | [diff] [blame] | 40 | #include "JSCInlines.h" |
fpizlo@apple.com | 0bef2a1 | 2014-02-10 19:26:29 +0000 | [diff] [blame] | 41 | #include "PutByIdStatus.h" |
fpizlo@apple.com | 3187c92 | 2012-05-18 21:47:53 +0000 | [diff] [blame] | 42 | |
| 43 | namespace JSC { namespace DFG { |
| 44 | |
| 45 | class ConstantFoldingPhase : public Phase { |
| 46 | public: |
| 47 | ConstantFoldingPhase(Graph& graph) |
| 48 | : Phase(graph, "constant folding") |
fpizlo@apple.com | edcb7a9 | 2012-07-13 05:31:05 +0000 | [diff] [blame] | 49 | , m_state(graph) |
oliver@apple.com | 55d32d9 | 2013-07-25 04:05:03 +0000 | [diff] [blame] | 50 | , m_interpreter(graph, m_state) |
fpizlo@apple.com | f45e88b | 2013-01-20 19:29:50 +0000 | [diff] [blame] | 51 | , m_insertionSet(graph) |
fpizlo@apple.com | 3187c92 | 2012-05-18 21:47:53 +0000 | [diff] [blame] | 52 | { |
| 53 | } |
| 54 | |
fpizlo@apple.com | 79c51ee | 2012-05-18 22:30:24 +0000 | [diff] [blame] | 55 | bool run() |
fpizlo@apple.com | 3187c92 | 2012-05-18 21:47:53 +0000 | [diff] [blame] | 56 | { |
fpizlo@apple.com | 79c51ee | 2012-05-18 22:30:24 +0000 | [diff] [blame] | 57 | bool changed = false; |
fpizlo@apple.com | ead5edd | 2015-10-09 00:13:58 +0000 | [diff] [blame] | 58 | |
| 59 | for (BasicBlock* block : m_graph.blocksInNaturalOrder()) { |
fpizlo@apple.com | edcb7a9 | 2012-07-13 05:31:05 +0000 | [diff] [blame] | 60 | if (block->cfaFoundConstants) |
oliver@apple.com | 426f5b0 | 2013-07-25 04:04:27 +0000 | [diff] [blame] | 61 | changed |= foldConstants(block); |
fpizlo@apple.com | 3187c92 | 2012-05-18 21:47:53 +0000 | [diff] [blame] | 62 | } |
fpizlo@apple.com | 79c51ee | 2012-05-18 22:30:24 +0000 | [diff] [blame] | 63 | |
fpizlo@apple.com | b41e682 | 2014-07-25 20:55:17 +0000 | [diff] [blame] | 64 | if (changed && m_graph.m_form == SSA) { |
| 65 | // It's now possible that we have Upsilons pointed at JSConstants. Fix that. |
fpizlo@apple.com | ead5edd | 2015-10-09 00:13:58 +0000 | [diff] [blame] | 66 | for (BasicBlock* block : m_graph.blocksInNaturalOrder()) |
fpizlo@apple.com | b41e682 | 2014-07-25 20:55:17 +0000 | [diff] [blame] | 67 | fixUpsilons(block); |
fpizlo@apple.com | ead5edd | 2015-10-09 00:13:58 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | if (m_graph.m_form == SSA) { |
| 71 | // It's now possible to simplify basic blocks by placing an Unreachable terminator right |
| 72 | // after anything that invalidates AI. |
| 73 | bool didClipBlock = false; |
benjamin@webkit.org | 0628f23 | 2016-07-27 23:22:55 +0000 | [diff] [blame] | 74 | Vector<Node*> nodesToDelete; |
fpizlo@apple.com | ead5edd | 2015-10-09 00:13:58 +0000 | [diff] [blame] | 75 | for (BasicBlock* block : m_graph.blocksInNaturalOrder()) { |
| 76 | m_state.beginBasicBlock(block); |
| 77 | for (unsigned nodeIndex = 0; nodeIndex < block->size(); ++nodeIndex) { |
| 78 | if (block->at(nodeIndex)->isTerminal()) { |
| 79 | // It's possible that we have something after the terminal. It could be a |
| 80 | // no-op Check node, for example. We don't want the logic below to turn that |
| 81 | // node into Unreachable, since then we'd have two terminators. |
| 82 | break; |
| 83 | } |
| 84 | if (!m_state.isValid()) { |
| 85 | NodeOrigin origin = block->at(nodeIndex)->origin; |
| 86 | for (unsigned killIndex = nodeIndex; killIndex < block->size(); ++killIndex) |
benjamin@webkit.org | 0628f23 | 2016-07-27 23:22:55 +0000 | [diff] [blame] | 87 | nodesToDelete.append(block->at(killIndex)); |
fpizlo@apple.com | ead5edd | 2015-10-09 00:13:58 +0000 | [diff] [blame] | 88 | block->resize(nodeIndex); |
| 89 | block->appendNode(m_graph, SpecNone, Unreachable, origin); |
| 90 | didClipBlock = true; |
| 91 | break; |
| 92 | } |
| 93 | m_interpreter.execute(nodeIndex); |
| 94 | } |
| 95 | m_state.reset(); |
| 96 | } |
| 97 | |
| 98 | if (didClipBlock) { |
| 99 | changed = true; |
benjamin@webkit.org | 0628f23 | 2016-07-27 23:22:55 +0000 | [diff] [blame] | 100 | |
| 101 | m_graph.invalidateNodeLiveness(); |
| 102 | |
| 103 | for (Node* node : nodesToDelete) |
commit-queue@webkit.org | aa93e08 | 2016-07-28 01:36:44 +0000 | [diff] [blame] | 104 | m_graph.deleteNode(node); |
benjamin@webkit.org | 0628f23 | 2016-07-27 23:22:55 +0000 | [diff] [blame] | 105 | |
fpizlo@apple.com | ead5edd | 2015-10-09 00:13:58 +0000 | [diff] [blame] | 106 | m_graph.invalidateCFG(); |
| 107 | m_graph.resetReachability(); |
| 108 | m_graph.killUnreachableBlocks(); |
fpizlo@apple.com | b41e682 | 2014-07-25 20:55:17 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
fpizlo@apple.com | 79c51ee | 2012-05-18 22:30:24 +0000 | [diff] [blame] | 112 | return changed; |
fpizlo@apple.com | 3187c92 | 2012-05-18 21:47:53 +0000 | [diff] [blame] | 113 | } |
fpizlo@apple.com | edcb7a9 | 2012-07-13 05:31:05 +0000 | [diff] [blame] | 114 | |
| 115 | private: |
oliver@apple.com | 426f5b0 | 2013-07-25 04:04:27 +0000 | [diff] [blame] | 116 | bool foldConstants(BasicBlock* block) |
fpizlo@apple.com | edcb7a9 | 2012-07-13 05:31:05 +0000 | [diff] [blame] | 117 | { |
fpizlo@apple.com | edcb7a9 | 2012-07-13 05:31:05 +0000 | [diff] [blame] | 118 | bool changed = false; |
| 119 | m_state.beginBasicBlock(block); |
| 120 | for (unsigned indexInBlock = 0; indexInBlock < block->size(); ++indexInBlock) { |
fpizlo@apple.com | edcb7a9 | 2012-07-13 05:31:05 +0000 | [diff] [blame] | 121 | if (!m_state.isValid()) |
| 122 | break; |
| 123 | |
fpizlo@apple.com | 8ff092f | 2013-01-29 08:01:03 +0000 | [diff] [blame] | 124 | Node* node = block->at(indexInBlock); |
| 125 | |
fpizlo@apple.com | a398a56 | 2014-08-06 21:32:55 +0000 | [diff] [blame] | 126 | bool alreadyHandled = false; |
fpizlo@apple.com | edcb7a9 | 2012-07-13 05:31:05 +0000 | [diff] [blame] | 127 | bool eliminated = false; |
| 128 | |
fpizlo@apple.com | 8ff092f | 2013-01-29 08:01:03 +0000 | [diff] [blame] | 129 | switch (node->op()) { |
fpizlo@apple.com | 96509b7 | 2014-05-26 17:43:41 +0000 | [diff] [blame] | 130 | case BooleanToNumber: { |
| 131 | if (node->child1().useKind() == UntypedUse |
| 132 | && !m_interpreter.needsTypeCheck(node->child1(), SpecBoolean)) |
| 133 | node->child1().setUseKind(BooleanUse); |
| 134 | break; |
| 135 | } |
benjamin@webkit.org | 32b8d0a | 2015-08-19 04:09:12 +0000 | [diff] [blame] | 136 | |
| 137 | case CompareEq: { |
| 138 | if (!m_interpreter.needsTypeCheck(node->child1(), SpecOther)) |
| 139 | node->child1().setUseKind(OtherUse); |
| 140 | if (!m_interpreter.needsTypeCheck(node->child2(), SpecOther)) |
| 141 | node->child2().setUseKind(OtherUse); |
| 142 | break; |
| 143 | } |
fpizlo@apple.com | 96509b7 | 2014-05-26 17:43:41 +0000 | [diff] [blame] | 144 | |
fpizlo@apple.com | eb3323d | 2012-08-20 06:11:24 +0000 | [diff] [blame] | 145 | case CheckStructure: |
fpizlo@apple.com | 99f3762 | 2012-10-29 04:02:08 +0000 | [diff] [blame] | 146 | case ArrayifyToStructure: { |
fpizlo@apple.com | 8ff092f | 2013-01-29 08:01:03 +0000 | [diff] [blame] | 147 | AbstractValue& value = m_state.forNode(node->child1()); |
fpizlo@apple.com | 99f3762 | 2012-10-29 04:02:08 +0000 | [diff] [blame] | 148 | StructureSet set; |
fpizlo@apple.com | 8ff092f | 2013-01-29 08:01:03 +0000 | [diff] [blame] | 149 | if (node->op() == ArrayifyToStructure) |
| 150 | set = node->structure(); |
fpizlo@apple.com | 99f3762 | 2012-10-29 04:02:08 +0000 | [diff] [blame] | 151 | else |
fpizlo@apple.com | 8ff092f | 2013-01-29 08:01:03 +0000 | [diff] [blame] | 152 | set = node->structureSet(); |
fpizlo@apple.com | 0728b8a | 2014-07-23 01:19:50 +0000 | [diff] [blame] | 153 | if (value.m_structure.isSubsetOf(set)) { |
oliver@apple.com | 55d32d9 | 2013-07-25 04:05:03 +0000 | [diff] [blame] | 154 | m_interpreter.execute(indexInBlock); // Catch the fact that we may filter on cell. |
fpizlo@apple.com | 163291d | 2015-04-28 19:27:23 +0000 | [diff] [blame] | 155 | node->remove(); |
fpizlo@apple.com | 99f3762 | 2012-10-29 04:02:08 +0000 | [diff] [blame] | 156 | eliminated = true; |
| 157 | break; |
| 158 | } |
fpizlo@apple.com | eb3323d | 2012-08-20 06:11:24 +0000 | [diff] [blame] | 159 | break; |
| 160 | } |
fpizlo@apple.com | 04c1974 | 2012-08-26 22:35:26 +0000 | [diff] [blame] | 161 | |
fpizlo@apple.com | 93373ba | 2015-04-07 22:09:15 +0000 | [diff] [blame] | 162 | case GetIndexedPropertyStorage: { |
| 163 | JSArrayBufferView* view = m_graph.tryGetFoldableView( |
| 164 | m_state.forNode(node->child1()).m_value, node->arrayMode()); |
| 165 | if (!view) |
| 166 | break; |
| 167 | |
| 168 | if (view->mode() == FastTypedArray) { |
| 169 | // FIXME: It would be awesome to be able to fold the property storage for |
| 170 | // these GC-allocated typed arrays. For now it doesn't matter because the |
| 171 | // most common use-cases for constant typed arrays involve large arrays with |
| 172 | // aliased buffer views. |
| 173 | // https://bugs.webkit.org/show_bug.cgi?id=125425 |
| 174 | break; |
| 175 | } |
| 176 | |
| 177 | m_interpreter.execute(indexInBlock); |
| 178 | eliminated = true; |
| 179 | |
fpizlo@apple.com | 163291d | 2015-04-28 19:27:23 +0000 | [diff] [blame] | 180 | m_insertionSet.insertCheck(indexInBlock, node->origin, node->children); |
fpizlo@apple.com | 93373ba | 2015-04-07 22:09:15 +0000 | [diff] [blame] | 181 | node->convertToConstantStoragePointer(view->vector()); |
| 182 | break; |
| 183 | } |
| 184 | |
fpizlo@apple.com | fc70ba6 | 2014-09-26 03:59:33 +0000 | [diff] [blame] | 185 | case CheckStructureImmediate: { |
| 186 | AbstractValue& value = m_state.forNode(node->child1()); |
| 187 | StructureSet& set = node->structureSet(); |
| 188 | |
| 189 | if (value.value()) { |
| 190 | if (Structure* structure = jsDynamicCast<Structure*>(value.value())) { |
| 191 | if (set.contains(structure)) { |
| 192 | m_interpreter.execute(indexInBlock); |
fpizlo@apple.com | 163291d | 2015-04-28 19:27:23 +0000 | [diff] [blame] | 193 | node->remove(); |
fpizlo@apple.com | fc70ba6 | 2014-09-26 03:59:33 +0000 | [diff] [blame] | 194 | eliminated = true; |
| 195 | break; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | if (PhiChildren* phiChildren = m_interpreter.phiChildren()) { |
| 201 | bool allGood = true; |
| 202 | phiChildren->forAllTransitiveIncomingValues( |
| 203 | node, |
| 204 | [&] (Node* incoming) { |
| 205 | if (Structure* structure = incoming->dynamicCastConstant<Structure*>()) { |
| 206 | if (set.contains(structure)) |
| 207 | return; |
| 208 | } |
| 209 | allGood = false; |
| 210 | }); |
| 211 | if (allGood) { |
| 212 | m_interpreter.execute(indexInBlock); |
fpizlo@apple.com | 163291d | 2015-04-28 19:27:23 +0000 | [diff] [blame] | 213 | node->remove(); |
fpizlo@apple.com | fc70ba6 | 2014-09-26 03:59:33 +0000 | [diff] [blame] | 214 | eliminated = true; |
| 215 | break; |
| 216 | } |
| 217 | } |
| 218 | break; |
| 219 | } |
| 220 | |
fpizlo@apple.com | 99f3762 | 2012-10-29 04:02:08 +0000 | [diff] [blame] | 221 | case CheckArray: |
| 222 | case Arrayify: { |
fpizlo@apple.com | 8ff092f | 2013-01-29 08:01:03 +0000 | [diff] [blame] | 223 | if (!node->arrayMode().alreadyChecked(m_graph, node, m_state.forNode(node->child1()))) |
fpizlo@apple.com | 04c1974 | 2012-08-26 22:35:26 +0000 | [diff] [blame] | 224 | break; |
fpizlo@apple.com | 163291d | 2015-04-28 19:27:23 +0000 | [diff] [blame] | 225 | node->remove(); |
fpizlo@apple.com | 04c1974 | 2012-08-26 22:35:26 +0000 | [diff] [blame] | 226 | eliminated = true; |
| 227 | break; |
| 228 | } |
| 229 | |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 230 | case PutStructure: { |
| 231 | if (m_state.forNode(node->child1()).m_structure.onlyStructure() != node->transition()->next) |
| 232 | break; |
| 233 | |
fpizlo@apple.com | 163291d | 2015-04-28 19:27:23 +0000 | [diff] [blame] | 234 | node->remove(); |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 235 | eliminated = true; |
| 236 | break; |
| 237 | } |
| 238 | |
fpizlo@apple.com | 29abafe | 2014-08-28 19:09:48 +0000 | [diff] [blame] | 239 | case CheckCell: { |
| 240 | if (m_state.forNode(node->child1()).value() != node->cellOperand()->value()) |
fpizlo@apple.com | 18e7bc1 | 2012-11-12 22:52:32 +0000 | [diff] [blame] | 241 | break; |
fpizlo@apple.com | 163291d | 2015-04-28 19:27:23 +0000 | [diff] [blame] | 242 | node->remove(); |
fpizlo@apple.com | 18e7bc1 | 2012-11-12 22:52:32 +0000 | [diff] [blame] | 243 | eliminated = true; |
| 244 | break; |
| 245 | } |
rniwa@webkit.org | eb7ac19 | 2015-03-13 01:11:15 +0000 | [diff] [blame] | 246 | |
| 247 | case CheckNotEmpty: { |
| 248 | if (m_state.forNode(node->child1()).m_type & SpecEmpty) |
| 249 | break; |
fpizlo@apple.com | 163291d | 2015-04-28 19:27:23 +0000 | [diff] [blame] | 250 | node->remove(); |
rniwa@webkit.org | eb7ac19 | 2015-03-13 01:11:15 +0000 | [diff] [blame] | 251 | eliminated = true; |
| 252 | break; |
| 253 | } |
| 254 | |
utatane.tea@gmail.com | 7f364f2 | 2016-07-29 07:15:01 +0000 | [diff] [blame] | 255 | case CheckStringIdent: { |
utatane.tea@gmail.com | fccd136 | 2015-08-11 22:02:09 +0000 | [diff] [blame] | 256 | UniquedStringImpl* uid = node->uidOperand(); |
utatane.tea@gmail.com | fccd136 | 2015-08-11 22:02:09 +0000 | [diff] [blame] | 257 | const UniquedStringImpl* constantUid = nullptr; |
utatane.tea@gmail.com | b4dc1e1 | 2015-10-13 16:03:22 +0000 | [diff] [blame] | 258 | |
| 259 | JSValue childConstant = m_state.forNode(node->child1()).value(); |
utatane.tea@gmail.com | fccd136 | 2015-08-11 22:02:09 +0000 | [diff] [blame] | 260 | if (childConstant) { |
utatane.tea@gmail.com | 7f364f2 | 2016-07-29 07:15:01 +0000 | [diff] [blame] | 261 | if (childConstant.isString()) { |
| 262 | if (const auto* impl = asString(childConstant)->tryGetValueImpl()) { |
| 263 | // Edge filtering requires that a value here should be StringIdent. |
| 264 | // However, a constant value propagated in DFG is not filtered. |
| 265 | // So here, we check the propagated value is actually an atomic string. |
| 266 | // And if it's not, we just ignore. |
| 267 | if (impl->isAtomic()) |
| 268 | constantUid = static_cast<const UniquedStringImpl*>(impl); |
utatane.tea@gmail.com | fccd136 | 2015-08-11 22:02:09 +0000 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | if (constantUid == uid) { |
| 274 | node->remove(); |
| 275 | eliminated = true; |
| 276 | } |
| 277 | break; |
| 278 | } |
| 279 | |
fpizlo@apple.com | 8624c4b | 2013-12-10 03:24:31 +0000 | [diff] [blame] | 280 | case CheckInBounds: { |
| 281 | JSValue left = m_state.forNode(node->child1()).value(); |
| 282 | JSValue right = m_state.forNode(node->child2()).value(); |
| 283 | if (left && right && left.isInt32() && right.isInt32() |
| 284 | && static_cast<uint32_t>(left.asInt32()) < static_cast<uint32_t>(right.asInt32())) { |
fpizlo@apple.com | 163291d | 2015-04-28 19:27:23 +0000 | [diff] [blame] | 285 | node->remove(); |
fpizlo@apple.com | 8624c4b | 2013-12-10 03:24:31 +0000 | [diff] [blame] | 286 | eliminated = true; |
| 287 | break; |
| 288 | } |
| 289 | |
| 290 | break; |
| 291 | } |
fpizlo@apple.com | 51614cc | 2014-02-17 06:35:32 +0000 | [diff] [blame] | 292 | |
fpizlo@apple.com | c2b8c09 | 2016-04-24 17:05:51 +0000 | [diff] [blame] | 293 | case GetMyArgumentByVal: |
| 294 | case GetMyArgumentByValOutOfBounds: { |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 295 | JSValue index = m_state.forNode(node->child2()).value(); |
| 296 | if (!index || !index.isInt32()) |
| 297 | break; |
| 298 | |
| 299 | Node* arguments = node->child1().node(); |
| 300 | InlineCallFrame* inlineCallFrame = arguments->origin.semantic.inlineCallFrame; |
| 301 | |
| 302 | // Don't try to do anything if the index is known to be outside our static bounds. Note |
| 303 | // that our static bounds are usually strictly larger than the dynamic bounds. The |
| 304 | // exception is something like this, assuming foo() is not inlined: |
| 305 | // |
| 306 | // function foo() { return arguments[5]; } |
| 307 | // |
| 308 | // Here the static bound on number of arguments is 0, and we're accessing index 5. We |
| 309 | // will not strength-reduce this to GetStack because GetStack is otherwise assumed by the |
| 310 | // compiler to access those variables that are statically accounted for; for example if |
| 311 | // we emitted a GetStack on arg6 we would have out-of-bounds access crashes anywhere that |
| 312 | // uses an Operands<> map. There is not much cost to continuing to use a |
| 313 | // GetMyArgumentByVal in such statically-out-of-bounds accesses; we just lose CFA unless |
| 314 | // GCSE removes the access entirely. |
| 315 | if (inlineCallFrame) { |
fpizlo@apple.com | a40cca7 | 2015-03-26 04:52:14 +0000 | [diff] [blame] | 316 | if (index.asUInt32() >= inlineCallFrame->arguments.size() - 1) |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 317 | break; |
| 318 | } else { |
fpizlo@apple.com | a40cca7 | 2015-03-26 04:52:14 +0000 | [diff] [blame] | 319 | if (index.asUInt32() >= m_state.variables().numberOfArguments() - 1) |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 320 | break; |
| 321 | } |
| 322 | |
| 323 | m_interpreter.execute(indexInBlock); // Push CFA over this node after we get the state before. |
| 324 | |
| 325 | StackAccessData* data; |
| 326 | if (inlineCallFrame) { |
| 327 | data = m_graph.m_stackAccessData.add( |
fpizlo@apple.com | 308b665 | 2015-03-26 07:17:08 +0000 | [diff] [blame] | 328 | VirtualRegister( |
| 329 | inlineCallFrame->stackOffset + |
| 330 | CallFrame::argumentOffset(index.asInt32())), |
| 331 | FlushedJSValue); |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 332 | } else { |
| 333 | data = m_graph.m_stackAccessData.add( |
| 334 | virtualRegisterForArgument(index.asInt32() + 1), FlushedJSValue); |
| 335 | } |
| 336 | |
| 337 | if (inlineCallFrame && !inlineCallFrame->isVarargs() |
| 338 | && index.asUInt32() < inlineCallFrame->arguments.size() - 1) { |
| 339 | node->convertToGetStack(data); |
| 340 | eliminated = true; |
| 341 | break; |
| 342 | } |
| 343 | |
fpizlo@apple.com | c2b8c09 | 2016-04-24 17:05:51 +0000 | [diff] [blame] | 344 | if (node->op() == GetMyArgumentByValOutOfBounds) |
| 345 | break; |
| 346 | |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 347 | Node* length = emitCodeToGetArgumentsArrayLength( |
| 348 | m_insertionSet, arguments, indexInBlock, node->origin); |
| 349 | m_insertionSet.insertNode( |
| 350 | indexInBlock, SpecNone, CheckInBounds, node->origin, |
| 351 | node->child2(), Edge(length, Int32Use)); |
| 352 | node->convertToGetStack(data); |
| 353 | eliminated = true; |
| 354 | break; |
| 355 | } |
| 356 | |
fpizlo@apple.com | 51614cc | 2014-02-17 06:35:32 +0000 | [diff] [blame] | 357 | case MultiGetByOffset: { |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 358 | Edge baseEdge = node->child1(); |
| 359 | Node* base = baseEdge.node(); |
fpizlo@apple.com | 51614cc | 2014-02-17 06:35:32 +0000 | [diff] [blame] | 360 | MultiGetByOffsetData& data = node->multiGetByOffsetData(); |
| 361 | |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 362 | // First prune the variants, then check if the MultiGetByOffset can be |
| 363 | // strength-reduced to a GetByOffset. |
| 364 | |
| 365 | AbstractValue baseValue = m_state.forNode(base); |
| 366 | |
| 367 | m_interpreter.execute(indexInBlock); // Push CFA over this node after we get the state before. |
fpizlo@apple.com | a398a56 | 2014-08-06 21:32:55 +0000 | [diff] [blame] | 368 | alreadyHandled = true; // Don't allow the default constant folder to do things to this. |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 369 | |
fpizlo@apple.com | 6b62eaf | 2015-08-03 23:13:56 +0000 | [diff] [blame] | 370 | for (unsigned i = 0; i < data.cases.size(); ++i) { |
| 371 | MultiGetByOffsetCase& getCase = data.cases[i]; |
| 372 | getCase.set().filter(baseValue); |
| 373 | if (getCase.set().isEmpty()) { |
| 374 | data.cases[i--] = data.cases.last(); |
| 375 | data.cases.removeLast(); |
fpizlo@apple.com | a398a56 | 2014-08-06 21:32:55 +0000 | [diff] [blame] | 376 | changed = true; |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
fpizlo@apple.com | 6b62eaf | 2015-08-03 23:13:56 +0000 | [diff] [blame] | 380 | if (data.cases.size() != 1) |
fpizlo@apple.com | 51614cc | 2014-02-17 06:35:32 +0000 | [diff] [blame] | 381 | break; |
| 382 | |
fpizlo@apple.com | 6b62eaf | 2015-08-03 23:13:56 +0000 | [diff] [blame] | 383 | emitGetByOffset(indexInBlock, node, baseValue, data.cases[0], data.identifierNumber); |
fpizlo@apple.com | a398a56 | 2014-08-06 21:32:55 +0000 | [diff] [blame] | 384 | changed = true; |
fpizlo@apple.com | 51614cc | 2014-02-17 06:35:32 +0000 | [diff] [blame] | 385 | break; |
| 386 | } |
fpizlo@apple.com | 4321952 | 2014-02-25 02:02:50 +0000 | [diff] [blame] | 387 | |
| 388 | case MultiPutByOffset: { |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 389 | Edge baseEdge = node->child1(); |
| 390 | Node* base = baseEdge.node(); |
fpizlo@apple.com | 4321952 | 2014-02-25 02:02:50 +0000 | [diff] [blame] | 391 | MultiPutByOffsetData& data = node->multiPutByOffsetData(); |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 392 | |
| 393 | AbstractValue baseValue = m_state.forNode(base); |
fpizlo@apple.com | 4321952 | 2014-02-25 02:02:50 +0000 | [diff] [blame] | 394 | |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 395 | m_interpreter.execute(indexInBlock); // Push CFA over this node after we get the state before. |
fpizlo@apple.com | a398a56 | 2014-08-06 21:32:55 +0000 | [diff] [blame] | 396 | alreadyHandled = true; // Don't allow the default constant folder to do things to this. |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 397 | |
| 398 | |
| 399 | for (unsigned i = 0; i < data.variants.size(); ++i) { |
| 400 | PutByIdVariant& variant = data.variants[i]; |
| 401 | variant.oldStructure().filter(baseValue); |
| 402 | |
| 403 | if (variant.oldStructure().isEmpty()) { |
| 404 | data.variants[i--] = data.variants.last(); |
| 405 | data.variants.removeLast(); |
fpizlo@apple.com | a398a56 | 2014-08-06 21:32:55 +0000 | [diff] [blame] | 406 | changed = true; |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 407 | continue; |
| 408 | } |
| 409 | |
| 410 | if (variant.kind() == PutByIdVariant::Transition |
| 411 | && variant.oldStructure().onlyStructure() == variant.newStructure()) { |
| 412 | variant = PutByIdVariant::replace( |
| 413 | variant.oldStructure(), |
fpizlo@apple.com | 1283577 | 2015-09-21 20:49:04 +0000 | [diff] [blame] | 414 | variant.offset(), |
| 415 | variant.requiredType()); |
fpizlo@apple.com | a398a56 | 2014-08-06 21:32:55 +0000 | [diff] [blame] | 416 | changed = true; |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | |
| 420 | if (data.variants.size() != 1) |
fpizlo@apple.com | 4321952 | 2014-02-25 02:02:50 +0000 | [diff] [blame] | 421 | break; |
| 422 | |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 423 | emitPutByOffset( |
| 424 | indexInBlock, node, baseValue, data.variants[0], data.identifierNumber); |
fpizlo@apple.com | a398a56 | 2014-08-06 21:32:55 +0000 | [diff] [blame] | 425 | changed = true; |
fpizlo@apple.com | 4321952 | 2014-02-25 02:02:50 +0000 | [diff] [blame] | 426 | break; |
| 427 | } |
fpizlo@apple.com | 8624c4b | 2013-12-10 03:24:31 +0000 | [diff] [blame] | 428 | |
fpizlo@apple.com | c2c6763 | 2012-11-17 08:37:14 +0000 | [diff] [blame] | 429 | case GetById: |
| 430 | case GetByIdFlush: { |
fpizlo@apple.com | 7a1964c | 2013-02-21 22:59:02 +0000 | [diff] [blame] | 431 | Edge childEdge = node->child1(); |
| 432 | Node* child = childEdge.node(); |
fpizlo@apple.com | 8ff092f | 2013-01-29 08:01:03 +0000 | [diff] [blame] | 433 | unsigned identifierNumber = node->identifierNumber(); |
fpizlo@apple.com | c2c6763 | 2012-11-17 08:37:14 +0000 | [diff] [blame] | 434 | |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 435 | AbstractValue baseValue = m_state.forNode(child); |
fpizlo@apple.com | 9f62443 | 2014-07-26 19:06:44 +0000 | [diff] [blame] | 436 | |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 437 | m_interpreter.execute(indexInBlock); // Push CFA over this node after we get the state before. |
fpizlo@apple.com | a398a56 | 2014-08-06 21:32:55 +0000 | [diff] [blame] | 438 | alreadyHandled = true; // Don't allow the default constant folder to do things to this. |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 439 | |
| 440 | if (baseValue.m_structure.isTop() || baseValue.m_structure.isClobbered() |
| 441 | || (node->child1().useKind() == UntypedUse || (baseValue.m_type & ~SpecCell))) |
| 442 | break; |
fpizlo@apple.com | 60d1abd | 2014-07-26 05:18:16 +0000 | [diff] [blame] | 443 | |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 444 | GetByIdStatus status = GetByIdStatus::computeFor( |
fpizlo@apple.com | 15ec1b2 | 2014-09-21 19:18:40 +0000 | [diff] [blame] | 445 | baseValue.m_structure.set(), m_graph.identifiers()[identifierNumber]); |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 446 | if (!status.isSimple()) |
| 447 | break; |
| 448 | |
| 449 | for (unsigned i = status.numVariants(); i--;) { |
fpizlo@apple.com | 6b62eaf | 2015-08-03 23:13:56 +0000 | [diff] [blame] | 450 | if (!status[i].conditionSet().isEmpty()) { |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 451 | // FIXME: We could handle prototype cases. |
| 452 | // https://bugs.webkit.org/show_bug.cgi?id=110386 |
| 453 | break; |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | if (status.numVariants() == 1) { |
| 458 | emitGetByOffset(indexInBlock, node, baseValue, status[0], identifierNumber); |
fpizlo@apple.com | a398a56 | 2014-08-06 21:32:55 +0000 | [diff] [blame] | 459 | changed = true; |
fpizlo@apple.com | c2c6763 | 2012-11-17 08:37:14 +0000 | [diff] [blame] | 460 | break; |
fpizlo@apple.com | 7a1964c | 2013-02-21 22:59:02 +0000 | [diff] [blame] | 461 | } |
fpizlo@apple.com | c2c6763 | 2012-11-17 08:37:14 +0000 | [diff] [blame] | 462 | |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 463 | if (!isFTL(m_graph.m_plan.mode)) |
| 464 | break; |
| 465 | |
| 466 | MultiGetByOffsetData* data = m_graph.m_multiGetByOffsetData.add(); |
fpizlo@apple.com | 6b62eaf | 2015-08-03 23:13:56 +0000 | [diff] [blame] | 467 | for (const GetByIdVariant& variant : status.variants()) { |
| 468 | data->cases.append( |
| 469 | MultiGetByOffsetCase( |
| 470 | variant.structureSet(), |
| 471 | GetByOffsetMethod::load(variant.offset()))); |
| 472 | } |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 473 | data->identifierNumber = identifierNumber; |
| 474 | node->convertToMultiGetByOffset(data); |
fpizlo@apple.com | a398a56 | 2014-08-06 21:32:55 +0000 | [diff] [blame] | 475 | changed = true; |
fpizlo@apple.com | c2c6763 | 2012-11-17 08:37:14 +0000 | [diff] [blame] | 476 | break; |
| 477 | } |
| 478 | |
| 479 | case PutById: |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 480 | case PutByIdDirect: |
| 481 | case PutByIdFlush: { |
fpizlo@apple.com | 6793a32 | 2014-02-12 05:42:32 +0000 | [diff] [blame] | 482 | NodeOrigin origin = node->origin; |
fpizlo@apple.com | 7a1964c | 2013-02-21 22:59:02 +0000 | [diff] [blame] | 483 | Edge childEdge = node->child1(); |
| 484 | Node* child = childEdge.node(); |
fpizlo@apple.com | 8ff092f | 2013-01-29 08:01:03 +0000 | [diff] [blame] | 485 | unsigned identifierNumber = node->identifierNumber(); |
fpizlo@apple.com | c2c6763 | 2012-11-17 08:37:14 +0000 | [diff] [blame] | 486 | |
fpizlo@apple.com | 7a1964c | 2013-02-21 22:59:02 +0000 | [diff] [blame] | 487 | ASSERT(childEdge.useKind() == CellUse); |
| 488 | |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 489 | AbstractValue baseValue = m_state.forNode(child); |
fpizlo@apple.com | 1283577 | 2015-09-21 20:49:04 +0000 | [diff] [blame] | 490 | AbstractValue valueValue = m_state.forNode(node->child2()); |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 491 | |
| 492 | m_interpreter.execute(indexInBlock); // Push CFA over this node after we get the state before. |
fpizlo@apple.com | a398a56 | 2014-08-06 21:32:55 +0000 | [diff] [blame] | 493 | alreadyHandled = true; // Don't allow the default constant folder to do things to this. |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 494 | |
| 495 | if (baseValue.m_structure.isTop() || baseValue.m_structure.isClobbered()) |
fpizlo@apple.com | c2c6763 | 2012-11-17 08:37:14 +0000 | [diff] [blame] | 496 | break; |
| 497 | |
fpizlo@apple.com | c2c6763 | 2012-11-17 08:37:14 +0000 | [diff] [blame] | 498 | PutByIdStatus status = PutByIdStatus::computeFor( |
fpizlo@apple.com | 6793a32 | 2014-02-12 05:42:32 +0000 | [diff] [blame] | 499 | m_graph.globalObjectFor(origin.semantic), |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 500 | baseValue.m_structure.set(), |
oliver@apple.com | 90fce82 | 2013-07-25 04:00:13 +0000 | [diff] [blame] | 501 | m_graph.identifiers()[identifierNumber], |
fpizlo@apple.com | 8ff092f | 2013-01-29 08:01:03 +0000 | [diff] [blame] | 502 | node->op() == PutByIdDirect); |
fpizlo@apple.com | c2c6763 | 2012-11-17 08:37:14 +0000 | [diff] [blame] | 503 | |
fpizlo@apple.com | 4321952 | 2014-02-25 02:02:50 +0000 | [diff] [blame] | 504 | if (!status.isSimple()) |
| 505 | break; |
fpizlo@apple.com | 1283577 | 2015-09-21 20:49:04 +0000 | [diff] [blame] | 506 | |
fpizlo@apple.com | a398a56 | 2014-08-06 21:32:55 +0000 | [diff] [blame] | 507 | ASSERT(status.numVariants()); |
| 508 | |
| 509 | if (status.numVariants() > 1 && !isFTL(m_graph.m_plan.mode)) |
| 510 | break; |
| 511 | |
| 512 | changed = true; |
fpizlo@apple.com | 6b62eaf | 2015-08-03 23:13:56 +0000 | [diff] [blame] | 513 | |
| 514 | bool allGood = true; |
| 515 | for (const PutByIdVariant& variant : status.variants()) { |
| 516 | if (!allGood) |
| 517 | break; |
| 518 | for (const ObjectPropertyCondition& condition : variant.conditionSet()) { |
| 519 | if (m_graph.watchCondition(condition)) |
| 520 | continue; |
| 521 | |
| 522 | Structure* structure = condition.object()->structure(); |
| 523 | if (!condition.structureEnsuresValidity(structure)) { |
| 524 | allGood = false; |
| 525 | break; |
| 526 | } |
| 527 | |
| 528 | m_insertionSet.insertNode( |
| 529 | indexInBlock, SpecNone, CheckStructure, node->origin, |
| 530 | OpInfo(m_graph.addStructureSet(structure)), |
| 531 | m_insertionSet.insertConstantForUse( |
| 532 | indexInBlock, node->origin, condition.object(), KnownCellUse)); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | if (!allGood) |
| 537 | break; |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 538 | |
| 539 | if (status.numVariants() == 1) { |
| 540 | emitPutByOffset(indexInBlock, node, baseValue, status[0], identifierNumber); |
| 541 | break; |
| 542 | } |
| 543 | |
fpizlo@apple.com | a398a56 | 2014-08-06 21:32:55 +0000 | [diff] [blame] | 544 | ASSERT(isFTL(m_graph.m_plan.mode)); |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 545 | |
| 546 | MultiPutByOffsetData* data = m_graph.m_multiPutByOffsetData.add(); |
| 547 | data->variants = status.variants(); |
| 548 | data->identifierNumber = identifierNumber; |
| 549 | node->convertToMultiPutByOffset(data); |
fpizlo@apple.com | c2c6763 | 2012-11-17 08:37:14 +0000 | [diff] [blame] | 550 | break; |
| 551 | } |
mhahnenberg@apple.com | 4968e1a | 2013-12-18 22:50:40 +0000 | [diff] [blame] | 552 | |
fpizlo@apple.com | 6e550a4 | 2014-02-17 20:23:34 +0000 | [diff] [blame] | 553 | case ToPrimitive: { |
utatane.tea@gmail.com | 23c2df7 | 2015-05-14 17:36:12 +0000 | [diff] [blame] | 554 | if (m_state.forNode(node->child1()).m_type & ~(SpecFullNumber | SpecBoolean | SpecString | SpecSymbol)) |
fpizlo@apple.com | 6e550a4 | 2014-02-17 20:23:34 +0000 | [diff] [blame] | 555 | break; |
| 556 | |
| 557 | node->convertToIdentity(); |
fpizlo@apple.com | a398a56 | 2014-08-06 21:32:55 +0000 | [diff] [blame] | 558 | changed = true; |
mhahnenberg@apple.com | 4968e1a | 2013-12-18 22:50:40 +0000 | [diff] [blame] | 559 | break; |
| 560 | } |
utatane.tea@gmail.com | 44616d0 | 2016-01-31 23:05:10 +0000 | [diff] [blame] | 561 | |
keith_miller@apple.com | d1a5d2f | 2016-05-02 17:38:15 +0000 | [diff] [blame] | 562 | case ToThis: { |
| 563 | if (!isToThisAnIdentity(m_graph.executableFor(node->origin.semantic)->isStrictMode(), m_state.forNode(node->child1()))) |
| 564 | break; |
| 565 | |
| 566 | node->convertToIdentity(); |
| 567 | changed = true; |
| 568 | break; |
| 569 | } |
| 570 | |
utatane.tea@gmail.com | db32c54 | 2016-06-30 15:26:47 +0000 | [diff] [blame] | 571 | case ToNumber: { |
| 572 | if (m_state.forNode(node->child1()).m_type & ~SpecBytecodeNumber) |
| 573 | break; |
| 574 | |
| 575 | node->convertToIdentity(); |
| 576 | changed = true; |
| 577 | break; |
| 578 | } |
| 579 | |
fpizlo@apple.com | a398a56 | 2014-08-06 21:32:55 +0000 | [diff] [blame] | 580 | case Check: { |
| 581 | alreadyHandled = true; |
| 582 | m_interpreter.execute(indexInBlock); |
| 583 | for (unsigned i = 0; i < AdjacencyList::Size; ++i) { |
| 584 | Edge edge = node->children.child(i); |
| 585 | if (!edge) |
| 586 | break; |
| 587 | if (edge.isProved() || edge.willNotHaveCheck()) { |
| 588 | node->children.removeEdge(i--); |
| 589 | changed = true; |
| 590 | } |
| 591 | } |
| 592 | break; |
| 593 | } |
fpizlo@apple.com | 29abafe | 2014-08-28 19:09:48 +0000 | [diff] [blame] | 594 | |
fpizlo@apple.com | edcb7a9 | 2012-07-13 05:31:05 +0000 | [diff] [blame] | 595 | default: |
| 596 | break; |
| 597 | } |
fpizlo@apple.com | a398a56 | 2014-08-06 21:32:55 +0000 | [diff] [blame] | 598 | |
fpizlo@apple.com | edcb7a9 | 2012-07-13 05:31:05 +0000 | [diff] [blame] | 599 | if (eliminated) { |
| 600 | changed = true; |
| 601 | continue; |
| 602 | } |
| 603 | |
fpizlo@apple.com | a398a56 | 2014-08-06 21:32:55 +0000 | [diff] [blame] | 604 | if (alreadyHandled) |
| 605 | continue; |
| 606 | |
oliver@apple.com | 55d32d9 | 2013-07-25 04:05:03 +0000 | [diff] [blame] | 607 | m_interpreter.execute(indexInBlock); |
fpizlo@apple.com | a0fb0905 | 2014-01-07 04:52:48 +0000 | [diff] [blame] | 608 | if (!m_state.isValid()) { |
| 609 | // If we invalidated then we shouldn't attempt to constant-fold. Here's an |
| 610 | // example: |
| 611 | // |
| 612 | // c: JSConstant(4.2) |
| 613 | // x: ValueToInt32(Check:Int32:@const) |
| 614 | // |
| 615 | // It would be correct for an analysis to assume that execution cannot |
| 616 | // proceed past @x. Therefore, constant-folding @x could be rather bad. But, |
| 617 | // the CFA may report that it found a constant even though it also reported |
| 618 | // that everything has been invalidated. This will only happen in a couple of |
| 619 | // the constant folding cases; most of them are also separately defensive |
| 620 | // about such things. |
| 621 | break; |
| 622 | } |
fpizlo@apple.com | 8ff092f | 2013-01-29 08:01:03 +0000 | [diff] [blame] | 623 | if (!node->shouldGenerate() || m_state.didClobber() || node->hasConstant()) |
fpizlo@apple.com | edcb7a9 | 2012-07-13 05:31:05 +0000 | [diff] [blame] | 624 | continue; |
fpizlo@apple.com | b41e682 | 2014-07-25 20:55:17 +0000 | [diff] [blame] | 625 | |
| 626 | // Interesting fact: this freezing that we do right here may turn an fragile value into |
| 627 | // a weak value. See DFGValueStrength.h. |
| 628 | FrozenValue* value = m_graph.freeze(m_state.forNode(node).value()); |
| 629 | if (!*value) |
fpizlo@apple.com | edcb7a9 | 2012-07-13 05:31:05 +0000 | [diff] [blame] | 630 | continue; |
fpizlo@apple.com | efa80dc | 2013-10-12 01:35:49 +0000 | [diff] [blame] | 631 | |
fpizlo@apple.com | 163291d | 2015-04-28 19:27:23 +0000 | [diff] [blame] | 632 | if (node->op() == GetLocal) { |
| 633 | // Need to preserve bytecode liveness in ThreadedCPS form. This wouldn't be necessary |
| 634 | // if it wasn't for https://bugs.webkit.org/show_bug.cgi?id=144086. |
fpizlo@apple.com | b41e682 | 2014-07-25 20:55:17 +0000 | [diff] [blame] | 635 | m_insertionSet.insertNode( |
fpizlo@apple.com | 163291d | 2015-04-28 19:27:23 +0000 | [diff] [blame] | 636 | indexInBlock, SpecNone, PhantomLocal, node->origin, |
| 637 | OpInfo(node->variableAccessData())); |
| 638 | m_graph.dethread(); |
| 639 | } else |
| 640 | m_insertionSet.insertCheck(indexInBlock, node->origin, node->children); |
| 641 | m_graph.convertToConstant(node, value); |
fpizlo@apple.com | 8ff092f | 2013-01-29 08:01:03 +0000 | [diff] [blame] | 642 | |
fpizlo@apple.com | edcb7a9 | 2012-07-13 05:31:05 +0000 | [diff] [blame] | 643 | changed = true; |
| 644 | } |
| 645 | m_state.reset(); |
fpizlo@apple.com | f45e88b | 2013-01-20 19:29:50 +0000 | [diff] [blame] | 646 | m_insertionSet.execute(block); |
fpizlo@apple.com | edcb7a9 | 2012-07-13 05:31:05 +0000 | [diff] [blame] | 647 | |
| 648 | return changed; |
| 649 | } |
fpizlo@apple.com | 6b62eaf | 2015-08-03 23:13:56 +0000 | [diff] [blame] | 650 | |
| 651 | void emitGetByOffset(unsigned indexInBlock, Node* node, const AbstractValue& baseValue, const MultiGetByOffsetCase& getCase, unsigned identifierNumber) |
| 652 | { |
| 653 | // When we get to here we have already emitted all of the requisite checks for everything. |
| 654 | // So, we just need to emit what the method object tells us to emit. |
fpizlo@apple.com | 51614cc | 2014-02-17 06:35:32 +0000 | [diff] [blame] | 655 | |
fpizlo@apple.com | 6b62eaf | 2015-08-03 23:13:56 +0000 | [diff] [blame] | 656 | addBaseCheck(indexInBlock, node, baseValue, getCase.set()); |
| 657 | |
| 658 | GetByOffsetMethod method = getCase.method(); |
| 659 | |
| 660 | switch (method.kind()) { |
| 661 | case GetByOffsetMethod::Invalid: |
| 662 | RELEASE_ASSERT_NOT_REACHED(); |
| 663 | return; |
| 664 | |
| 665 | case GetByOffsetMethod::Constant: |
| 666 | m_graph.convertToConstant(node, method.constant()); |
| 667 | return; |
| 668 | |
| 669 | case GetByOffsetMethod::Load: |
| 670 | emitGetByOffset(indexInBlock, node, node->child1(), identifierNumber, method.offset()); |
| 671 | return; |
| 672 | |
| 673 | case GetByOffsetMethod::LoadFromPrototype: { |
| 674 | Node* child = m_insertionSet.insertConstant( |
| 675 | indexInBlock, node->origin, method.prototype()); |
| 676 | emitGetByOffset( |
| 677 | indexInBlock, node, Edge(child, KnownCellUse), identifierNumber, method.offset()); |
| 678 | return; |
| 679 | } } |
| 680 | |
| 681 | RELEASE_ASSERT_NOT_REACHED(); |
| 682 | } |
| 683 | |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 684 | void emitGetByOffset(unsigned indexInBlock, Node* node, const AbstractValue& baseValue, const GetByIdVariant& variant, unsigned identifierNumber) |
fpizlo@apple.com | 51614cc | 2014-02-17 06:35:32 +0000 | [diff] [blame] | 685 | { |
fpizlo@apple.com | 51614cc | 2014-02-17 06:35:32 +0000 | [diff] [blame] | 686 | Edge childEdge = node->child1(); |
fpizlo@apple.com | 51614cc | 2014-02-17 06:35:32 +0000 | [diff] [blame] | 687 | |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 688 | addBaseCheck(indexInBlock, node, baseValue, variant.structureSet()); |
fpizlo@apple.com | 51614cc | 2014-02-17 06:35:32 +0000 | [diff] [blame] | 689 | |
fpizlo@apple.com | 6b62eaf | 2015-08-03 23:13:56 +0000 | [diff] [blame] | 690 | // We aren't set up to handle prototype stuff. |
| 691 | DFG_ASSERT(m_graph, node, variant.conditionSet().isEmpty()); |
| 692 | |
| 693 | if (JSValue value = m_graph.tryGetConstantProperty(baseValue.m_value, variant.structureSet(), variant.offset())) { |
fpizlo@apple.com | 2c4a7e9 | 2014-08-06 05:27:46 +0000 | [diff] [blame] | 694 | m_graph.convertToConstant(node, m_graph.freeze(value)); |
fpizlo@apple.com | 51614cc | 2014-02-17 06:35:32 +0000 | [diff] [blame] | 695 | return; |
| 696 | } |
| 697 | |
fpizlo@apple.com | 6b62eaf | 2015-08-03 23:13:56 +0000 | [diff] [blame] | 698 | emitGetByOffset(indexInBlock, node, childEdge, identifierNumber, variant.offset()); |
| 699 | } |
| 700 | |
fpizlo@apple.com | 1283577 | 2015-09-21 20:49:04 +0000 | [diff] [blame] | 701 | void emitGetByOffset( |
| 702 | unsigned indexInBlock, Node* node, Edge childEdge, unsigned identifierNumber, |
| 703 | PropertyOffset offset, const InferredType::Descriptor& inferredType = InferredType::Top) |
fpizlo@apple.com | 6b62eaf | 2015-08-03 23:13:56 +0000 | [diff] [blame] | 704 | { |
| 705 | childEdge.setUseKind(KnownCellUse); |
fpizlo@apple.com | 51614cc | 2014-02-17 06:35:32 +0000 | [diff] [blame] | 706 | |
| 707 | Edge propertyStorage; |
| 708 | |
fpizlo@apple.com | 6b62eaf | 2015-08-03 23:13:56 +0000 | [diff] [blame] | 709 | if (isInlineOffset(offset)) |
fpizlo@apple.com | 51614cc | 2014-02-17 06:35:32 +0000 | [diff] [blame] | 710 | propertyStorage = childEdge; |
| 711 | else { |
| 712 | propertyStorage = Edge(m_insertionSet.insertNode( |
fpizlo@apple.com | 6b62eaf | 2015-08-03 23:13:56 +0000 | [diff] [blame] | 713 | indexInBlock, SpecNone, GetButterfly, node->origin, childEdge)); |
fpizlo@apple.com | 51614cc | 2014-02-17 06:35:32 +0000 | [diff] [blame] | 714 | } |
| 715 | |
fpizlo@apple.com | 9800ede | 2014-09-20 17:59:58 +0000 | [diff] [blame] | 716 | StorageAccessData& data = *m_graph.m_storageAccessData.add(); |
fpizlo@apple.com | 6b62eaf | 2015-08-03 23:13:56 +0000 | [diff] [blame] | 717 | data.offset = offset; |
fpizlo@apple.com | 9800ede | 2014-09-20 17:59:58 +0000 | [diff] [blame] | 718 | data.identifierNumber = identifierNumber; |
fpizlo@apple.com | 1283577 | 2015-09-21 20:49:04 +0000 | [diff] [blame] | 719 | data.inferredType = inferredType; |
fpizlo@apple.com | 51614cc | 2014-02-17 06:35:32 +0000 | [diff] [blame] | 720 | |
fpizlo@apple.com | f224cdd | 2016-06-10 02:03:33 +0000 | [diff] [blame] | 721 | node->convertToGetByOffset(data, propertyStorage, childEdge); |
fpizlo@apple.com | 51614cc | 2014-02-17 06:35:32 +0000 | [diff] [blame] | 722 | } |
msaboff@apple.com | 62aa8b7 | 2013-09-26 22:53:54 +0000 | [diff] [blame] | 723 | |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 724 | void emitPutByOffset(unsigned indexInBlock, Node* node, const AbstractValue& baseValue, const PutByIdVariant& variant, unsigned identifierNumber) |
fpizlo@apple.com | 4321952 | 2014-02-25 02:02:50 +0000 | [diff] [blame] | 725 | { |
| 726 | NodeOrigin origin = node->origin; |
| 727 | Edge childEdge = node->child1(); |
fpizlo@apple.com | 1283577 | 2015-09-21 20:49:04 +0000 | [diff] [blame] | 728 | |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 729 | addBaseCheck(indexInBlock, node, baseValue, variant.oldStructure()); |
fpizlo@apple.com | 1283577 | 2015-09-21 20:49:04 +0000 | [diff] [blame] | 730 | insertInferredTypeCheck( |
| 731 | m_insertionSet, indexInBlock, origin, node->child2().node(), variant.requiredType()); |
fpizlo@apple.com | 4321952 | 2014-02-25 02:02:50 +0000 | [diff] [blame] | 732 | |
fpizlo@apple.com | f29186e | 2015-08-26 19:24:41 +0000 | [diff] [blame] | 733 | node->child1().setUseKind(KnownCellUse); |
fpizlo@apple.com | 4321952 | 2014-02-25 02:02:50 +0000 | [diff] [blame] | 734 | childEdge.setUseKind(KnownCellUse); |
| 735 | |
fpizlo@apple.com | 0728b8a | 2014-07-23 01:19:50 +0000 | [diff] [blame] | 736 | Transition* transition = 0; |
fpizlo@apple.com | 4321952 | 2014-02-25 02:02:50 +0000 | [diff] [blame] | 737 | if (variant.kind() == PutByIdVariant::Transition) { |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 738 | transition = m_graph.m_transitions.add( |
| 739 | variant.oldStructureForTransition(), variant.newStructure()); |
fpizlo@apple.com | 4321952 | 2014-02-25 02:02:50 +0000 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | Edge propertyStorage; |
| 743 | |
fpizlo@apple.com | f29186e | 2015-08-26 19:24:41 +0000 | [diff] [blame] | 744 | DFG_ASSERT(m_graph, node, origin.exitOK); |
| 745 | bool canExit = true; |
| 746 | |
fpizlo@apple.com | 4321952 | 2014-02-25 02:02:50 +0000 | [diff] [blame] | 747 | if (isInlineOffset(variant.offset())) |
| 748 | propertyStorage = childEdge; |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 749 | else if (!variant.reallocatesStorage()) { |
fpizlo@apple.com | 4321952 | 2014-02-25 02:02:50 +0000 | [diff] [blame] | 750 | propertyStorage = Edge(m_insertionSet.insertNode( |
| 751 | indexInBlock, SpecNone, GetButterfly, origin, childEdge)); |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 752 | } else if (!variant.oldStructureForTransition()->outOfLineCapacity()) { |
fpizlo@apple.com | 4321952 | 2014-02-25 02:02:50 +0000 | [diff] [blame] | 753 | ASSERT(variant.newStructure()->outOfLineCapacity()); |
| 754 | ASSERT(!isInlineOffset(variant.offset())); |
| 755 | Node* allocatePropertyStorage = m_insertionSet.insertNode( |
| 756 | indexInBlock, SpecNone, AllocatePropertyStorage, |
fpizlo@apple.com | f29186e | 2015-08-26 19:24:41 +0000 | [diff] [blame] | 757 | origin.takeValidExit(canExit), OpInfo(transition), childEdge); |
fpizlo@apple.com | 4321952 | 2014-02-25 02:02:50 +0000 | [diff] [blame] | 758 | propertyStorage = Edge(allocatePropertyStorage); |
| 759 | } else { |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 760 | ASSERT(variant.oldStructureForTransition()->outOfLineCapacity()); |
| 761 | ASSERT(variant.newStructure()->outOfLineCapacity() > variant.oldStructureForTransition()->outOfLineCapacity()); |
fpizlo@apple.com | 4321952 | 2014-02-25 02:02:50 +0000 | [diff] [blame] | 762 | ASSERT(!isInlineOffset(variant.offset())); |
| 763 | |
| 764 | Node* reallocatePropertyStorage = m_insertionSet.insertNode( |
fpizlo@apple.com | f29186e | 2015-08-26 19:24:41 +0000 | [diff] [blame] | 765 | indexInBlock, SpecNone, ReallocatePropertyStorage, origin.takeValidExit(canExit), |
fpizlo@apple.com | 0728b8a | 2014-07-23 01:19:50 +0000 | [diff] [blame] | 766 | OpInfo(transition), childEdge, |
fpizlo@apple.com | 4321952 | 2014-02-25 02:02:50 +0000 | [diff] [blame] | 767 | Edge(m_insertionSet.insertNode( |
| 768 | indexInBlock, SpecNone, GetButterfly, origin, childEdge))); |
fpizlo@apple.com | 4321952 | 2014-02-25 02:02:50 +0000 | [diff] [blame] | 769 | propertyStorage = Edge(reallocatePropertyStorage); |
| 770 | } |
| 771 | |
fpizlo@apple.com | 9800ede | 2014-09-20 17:59:58 +0000 | [diff] [blame] | 772 | StorageAccessData& data = *m_graph.m_storageAccessData.add(); |
| 773 | data.offset = variant.offset(); |
| 774 | data.identifierNumber = identifierNumber; |
| 775 | |
fpizlo@apple.com | f224cdd | 2016-06-10 02:03:33 +0000 | [diff] [blame] | 776 | node->convertToPutByOffset(data, propertyStorage, childEdge); |
fpizlo@apple.com | f29186e | 2015-08-26 19:24:41 +0000 | [diff] [blame] | 777 | node->origin.exitOK = canExit; |
fpizlo@apple.com | 3beeb7f | 2015-03-20 23:26:26 +0000 | [diff] [blame] | 778 | |
| 779 | if (variant.kind() == PutByIdVariant::Transition) { |
| 780 | // FIXME: PutStructure goes last until we fix either |
| 781 | // https://bugs.webkit.org/show_bug.cgi?id=142921 or |
| 782 | // https://bugs.webkit.org/show_bug.cgi?id=142924. |
| 783 | m_insertionSet.insertNode( |
fpizlo@apple.com | f29186e | 2015-08-26 19:24:41 +0000 | [diff] [blame] | 784 | indexInBlock + 1, SpecNone, PutStructure, origin.withInvalidExit(), OpInfo(transition), |
| 785 | childEdge); |
fpizlo@apple.com | 3beeb7f | 2015-03-20 23:26:26 +0000 | [diff] [blame] | 786 | } |
fpizlo@apple.com | 4321952 | 2014-02-25 02:02:50 +0000 | [diff] [blame] | 787 | } |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 788 | |
| 789 | void addBaseCheck( |
| 790 | unsigned indexInBlock, Node* node, const AbstractValue& baseValue, const StructureSet& set) |
| 791 | { |
| 792 | if (!baseValue.m_structure.isSubsetOf(set)) { |
| 793 | // Arises when we prune MultiGetByOffset. We could have a |
| 794 | // MultiGetByOffset with a single variant that checks for structure S, |
| 795 | // and the input has structures S and T, for example. |
fpizlo@apple.com | 1283577 | 2015-09-21 20:49:04 +0000 | [diff] [blame] | 796 | ASSERT(node->child1()); |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 797 | m_insertionSet.insertNode( |
| 798 | indexInBlock, SpecNone, CheckStructure, node->origin, |
| 799 | OpInfo(m_graph.addStructureSet(set)), node->child1()); |
| 800 | return; |
| 801 | } |
| 802 | |
fpizlo@apple.com | 163291d | 2015-04-28 19:27:23 +0000 | [diff] [blame] | 803 | if (baseValue.m_type & ~SpecCell) |
| 804 | m_insertionSet.insertCheck(indexInBlock, node->origin, node->child1()); |
fpizlo@apple.com | 3378c48 | 2014-07-27 23:14:40 +0000 | [diff] [blame] | 805 | } |
| 806 | |
fpizlo@apple.com | b41e682 | 2014-07-25 20:55:17 +0000 | [diff] [blame] | 807 | void addStructureTransitionCheck(NodeOrigin origin, unsigned indexInBlock, JSCell* cell, Structure* structure) |
fpizlo@apple.com | c2c6763 | 2012-11-17 08:37:14 +0000 | [diff] [blame] | 808 | { |
fpizlo@apple.com | 920c167 | 2014-08-19 00:55:31 +0000 | [diff] [blame] | 809 | if (m_graph.registerStructure(cell->structure()) == StructureRegisteredAndWatched) |
fpizlo@apple.com | 0728b8a | 2014-07-23 01:19:50 +0000 | [diff] [blame] | 810 | return; |
fpizlo@apple.com | 920c167 | 2014-08-19 00:55:31 +0000 | [diff] [blame] | 811 | |
| 812 | m_graph.registerStructure(structure); |
fpizlo@apple.com | 0728b8a | 2014-07-23 01:19:50 +0000 | [diff] [blame] | 813 | |
fpizlo@apple.com | 8ff092f | 2013-01-29 08:01:03 +0000 | [diff] [blame] | 814 | Node* weakConstant = m_insertionSet.insertNode( |
fpizlo@apple.com | b41e682 | 2014-07-25 20:55:17 +0000 | [diff] [blame] | 815 | indexInBlock, speculationFromValue(cell), JSConstant, origin, |
| 816 | OpInfo(m_graph.freeze(cell))); |
fpizlo@apple.com | c2c6763 | 2012-11-17 08:37:14 +0000 | [diff] [blame] | 817 | |
fpizlo@apple.com | f45e88b | 2013-01-20 19:29:50 +0000 | [diff] [blame] | 818 | m_insertionSet.insertNode( |
fpizlo@apple.com | 6793a32 | 2014-02-12 05:42:32 +0000 | [diff] [blame] | 819 | indexInBlock, SpecNone, CheckStructure, origin, |
fpizlo@apple.com | b41e682 | 2014-07-25 20:55:17 +0000 | [diff] [blame] | 820 | OpInfo(m_graph.addStructureSet(structure)), Edge(weakConstant, CellUse)); |
| 821 | } |
| 822 | |
| 823 | void fixUpsilons(BasicBlock* block) |
| 824 | { |
| 825 | for (unsigned nodeIndex = block->size(); nodeIndex--;) { |
| 826 | Node* node = block->at(nodeIndex); |
| 827 | if (node->op() != Upsilon) |
| 828 | continue; |
| 829 | switch (node->phi()->op()) { |
| 830 | case Phi: |
| 831 | break; |
| 832 | case JSConstant: |
| 833 | case DoubleConstant: |
| 834 | case Int52Constant: |
fpizlo@apple.com | 163291d | 2015-04-28 19:27:23 +0000 | [diff] [blame] | 835 | node->remove(); |
fpizlo@apple.com | b41e682 | 2014-07-25 20:55:17 +0000 | [diff] [blame] | 836 | break; |
| 837 | default: |
| 838 | DFG_CRASH(m_graph, node, "Bad Upsilon phi() pointer"); |
| 839 | break; |
| 840 | } |
| 841 | } |
fpizlo@apple.com | c2c6763 | 2012-11-17 08:37:14 +0000 | [diff] [blame] | 842 | } |
| 843 | |
oliver@apple.com | 55d32d9 | 2013-07-25 04:05:03 +0000 | [diff] [blame] | 844 | InPlaceAbstractState m_state; |
| 845 | AbstractInterpreter<InPlaceAbstractState> m_interpreter; |
fpizlo@apple.com | f45e88b | 2013-01-20 19:29:50 +0000 | [diff] [blame] | 846 | InsertionSet m_insertionSet; |
fpizlo@apple.com | 3187c92 | 2012-05-18 21:47:53 +0000 | [diff] [blame] | 847 | }; |
| 848 | |
fpizlo@apple.com | 79c51ee | 2012-05-18 22:30:24 +0000 | [diff] [blame] | 849 | bool performConstantFolding(Graph& graph) |
fpizlo@apple.com | 3187c92 | 2012-05-18 21:47:53 +0000 | [diff] [blame] | 850 | { |
fpizlo@apple.com | 79c51ee | 2012-05-18 22:30:24 +0000 | [diff] [blame] | 851 | return runPhase<ConstantFoldingPhase>(graph); |
fpizlo@apple.com | 3187c92 | 2012-05-18 21:47:53 +0000 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | } } // namespace JSC::DFG |
| 855 | |
| 856 | #endif // ENABLE(DFG_JIT) |
| 857 | |
| 858 | |