oliver@apple.com | 827d2cf | 2013-07-25 04:04:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | #include "config.h" |
ossy@webkit.org | beb0de4 | 2014-02-17 19:00:03 +0000 | [diff] [blame^] | 27 | #include "DFGFlushLivenessAnalysisPhase.h" |
oliver@apple.com | 827d2cf | 2013-07-25 04:04:45 +0000 | [diff] [blame] | 28 | |
| 29 | #if ENABLE(DFG_JIT) |
| 30 | |
| 31 | #include "DFGBasicBlockInlines.h" |
fpizlo@apple.com | 0bef2a1 | 2014-02-10 19:26:29 +0000 | [diff] [blame] | 32 | #include "DFGGraph.h" |
oliver@apple.com | 827d2cf | 2013-07-25 04:04:45 +0000 | [diff] [blame] | 33 | #include "DFGInsertionSet.h" |
| 34 | #include "DFGPhase.h" |
oliver@apple.com | 237b146 | 2013-07-25 04:05:36 +0000 | [diff] [blame] | 35 | #include "OperandsInlines.h" |
fpizlo@apple.com | fb7eff2 | 2014-02-11 01:45:50 +0000 | [diff] [blame] | 36 | #include "JSCInlines.h" |
oliver@apple.com | 827d2cf | 2013-07-25 04:04:45 +0000 | [diff] [blame] | 37 | |
| 38 | namespace JSC { namespace DFG { |
| 39 | |
| 40 | class FlushLivenessAnalysisPhase : public Phase { |
| 41 | public: |
| 42 | FlushLivenessAnalysisPhase(Graph& graph) |
| 43 | : Phase(graph, "flush-liveness analysis") |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | bool run() |
| 48 | { |
| 49 | ASSERT(m_graph.m_form == SSA); |
| 50 | |
| 51 | // Liveness is a backwards analysis; the roots are the blocks that |
oliver@apple.com | 1fc0418 | 2013-08-19 19:40:13 +0000 | [diff] [blame] | 52 | // end in a terminal (Return/Unreachable). For now, we |
oliver@apple.com | 827d2cf | 2013-07-25 04:04:45 +0000 | [diff] [blame] | 53 | // use a fixpoint formulation since liveness is a rapid analysis with |
| 54 | // convergence guaranteed after O(connectivity). |
| 55 | |
| 56 | // Start by assuming that everything is dead. |
| 57 | for (BlockIndex blockIndex = m_graph.numBlocks(); blockIndex--;) { |
| 58 | BasicBlock* block = m_graph.block(blockIndex); |
| 59 | if (!block) |
| 60 | continue; |
fpizlo@apple.com | a62d482 | 2013-10-06 04:22:43 +0000 | [diff] [blame] | 61 | block->ssa->flushAtHead.fill(FlushedAt()); |
| 62 | block->ssa->flushAtTail.fill(FlushedAt()); |
oliver@apple.com | 827d2cf | 2013-07-25 04:04:45 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | do { |
| 66 | m_changed = false; |
| 67 | for (BlockIndex blockIndex = m_graph.numBlocks(); blockIndex--;) |
| 68 | process(blockIndex); |
| 69 | } while (m_changed); |
| 70 | |
fpizlo@apple.com | a62d482 | 2013-10-06 04:22:43 +0000 | [diff] [blame] | 71 | Operands<FlushedAt>& root = m_graph.block(0)->ssa->flushAtHead; |
oliver@apple.com | 827d2cf | 2013-07-25 04:04:45 +0000 | [diff] [blame] | 72 | for (unsigned i = root.size(); i--;) { |
| 73 | if (root.isArgument(i)) { |
fpizlo@apple.com | a62d482 | 2013-10-06 04:22:43 +0000 | [diff] [blame] | 74 | if (!root[i] |
| 75 | || root[i] == FlushedAt(FlushedJSValue, VirtualRegister(root.operandForIndex(i)))) |
oliver@apple.com | 827d2cf | 2013-07-25 04:04:45 +0000 | [diff] [blame] | 76 | continue; |
| 77 | } else { |
fpizlo@apple.com | a62d482 | 2013-10-06 04:22:43 +0000 | [diff] [blame] | 78 | if (!root[i]) |
oliver@apple.com | 827d2cf | 2013-07-25 04:04:45 +0000 | [diff] [blame] | 79 | continue; |
| 80 | } |
| 81 | dataLog( |
| 82 | "Bad flush liveness analysis result: bad flush liveness at root: ", |
| 83 | root, "\n"); |
| 84 | dataLog("IR at time of error:\n"); |
| 85 | m_graph.dump(); |
| 86 | CRASH(); |
| 87 | } |
| 88 | |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | private: |
| 93 | void process(BlockIndex blockIndex) |
| 94 | { |
| 95 | BasicBlock* block = m_graph.block(blockIndex); |
| 96 | if (!block) |
| 97 | return; |
| 98 | |
fpizlo@apple.com | a62d482 | 2013-10-06 04:22:43 +0000 | [diff] [blame] | 99 | m_live = block->ssa->flushAtTail; |
oliver@apple.com | 827d2cf | 2013-07-25 04:04:45 +0000 | [diff] [blame] | 100 | |
| 101 | for (unsigned nodeIndex = block->size(); nodeIndex--;) { |
| 102 | Node* node = block->at(nodeIndex); |
| 103 | |
| 104 | switch (node->op()) { |
| 105 | case SetLocal: { |
| 106 | VariableAccessData* variable = node->variableAccessData(); |
fpizlo@apple.com | a62d482 | 2013-10-06 04:22:43 +0000 | [diff] [blame] | 107 | FlushedAt& current = m_live.operand(variable->local()); |
| 108 | if (!!current && current != variable->flushedAt()) |
| 109 | reportError(node); |
| 110 | current = FlushedAt(); |
oliver@apple.com | 827d2cf | 2013-07-25 04:04:45 +0000 | [diff] [blame] | 111 | break; |
| 112 | } |
| 113 | |
| 114 | case GetArgument: { |
| 115 | VariableAccessData* variable = node->variableAccessData(); |
fpizlo@apple.com | a62d482 | 2013-10-06 04:22:43 +0000 | [diff] [blame] | 116 | ASSERT(variable->local() == variable->machineLocal()); |
| 117 | ASSERT(variable->local().isArgument()); |
| 118 | FlushedAt& current = m_live.operand(variable->local()); |
| 119 | if (!!current && current != variable->flushedAt()) |
| 120 | reportError(node); |
| 121 | current = FlushedAt(FlushedJSValue, node->local()); |
oliver@apple.com | 827d2cf | 2013-07-25 04:04:45 +0000 | [diff] [blame] | 122 | break; |
| 123 | } |
| 124 | |
| 125 | case Flush: |
| 126 | case GetLocal: { |
| 127 | VariableAccessData* variable = node->variableAccessData(); |
fpizlo@apple.com | a62d482 | 2013-10-06 04:22:43 +0000 | [diff] [blame] | 128 | FlushedAt& current = m_live.operand(variable->local()); |
| 129 | if (!!current && current != variable->flushedAt()) |
| 130 | reportError(node); |
| 131 | current = variable->flushedAt(); |
oliver@apple.com | 827d2cf | 2013-07-25 04:04:45 +0000 | [diff] [blame] | 132 | break; |
| 133 | } |
| 134 | |
| 135 | default: |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | |
fpizlo@apple.com | a62d482 | 2013-10-06 04:22:43 +0000 | [diff] [blame] | 140 | if (m_live == block->ssa->flushAtHead) |
oliver@apple.com | 827d2cf | 2013-07-25 04:04:45 +0000 | [diff] [blame] | 141 | return; |
| 142 | |
| 143 | m_changed = true; |
fpizlo@apple.com | a62d482 | 2013-10-06 04:22:43 +0000 | [diff] [blame] | 144 | block->ssa->flushAtHead = m_live; |
oliver@apple.com | 827d2cf | 2013-07-25 04:04:45 +0000 | [diff] [blame] | 145 | for (unsigned i = block->predecessors.size(); i--;) { |
| 146 | BasicBlock* predecessor = block->predecessors[i]; |
| 147 | for (unsigned j = m_live.size(); j--;) { |
fpizlo@apple.com | a62d482 | 2013-10-06 04:22:43 +0000 | [diff] [blame] | 148 | FlushedAt& predecessorFlush = predecessor->ssa->flushAtTail[j]; |
| 149 | FlushedAt myFlush = m_live[j]; |
oliver@apple.com | 827d2cf | 2013-07-25 04:04:45 +0000 | [diff] [blame] | 150 | |
| 151 | // Three possibilities: |
| 152 | // 1) Predecessor format is Dead, in which case it acquires our format. |
commit-queue@webkit.org | 0bc8b5d4 | 2013-10-18 16:55:42 +0000 | [diff] [blame] | 153 | // 2) Predecessor format is not Dead but our format is dead, in which |
| 154 | // case we acquire the predecessor format. |
| 155 | // 3) Predecessor format is identical to our format, in which case we |
oliver@apple.com | 827d2cf | 2013-07-25 04:04:45 +0000 | [diff] [blame] | 156 | // do nothing. |
commit-queue@webkit.org | 0bc8b5d4 | 2013-10-18 16:55:42 +0000 | [diff] [blame] | 157 | // 4) Predecessor format is different from our format and it's not Dead, |
oliver@apple.com | 827d2cf | 2013-07-25 04:04:45 +0000 | [diff] [blame] | 158 | // in which case we have an erroneous set of Flushes and SetLocals. |
commit-queue@webkit.org | 0bc8b5d4 | 2013-10-18 16:55:42 +0000 | [diff] [blame] | 159 | |
fpizlo@apple.com | a62d482 | 2013-10-06 04:22:43 +0000 | [diff] [blame] | 160 | if (!predecessorFlush) { |
| 161 | predecessorFlush = myFlush; |
oliver@apple.com | 827d2cf | 2013-07-25 04:04:45 +0000 | [diff] [blame] | 162 | continue; |
| 163 | } |
commit-queue@webkit.org | 0bc8b5d4 | 2013-10-18 16:55:42 +0000 | [diff] [blame] | 164 | |
| 165 | if (!myFlush) { |
| 166 | m_live[j] = predecessorFlush; |
| 167 | continue; |
| 168 | } |
| 169 | |
fpizlo@apple.com | a62d482 | 2013-10-06 04:22:43 +0000 | [diff] [blame] | 170 | if (predecessorFlush == myFlush) |
oliver@apple.com | 827d2cf | 2013-07-25 04:04:45 +0000 | [diff] [blame] | 171 | continue; |
| 172 | |
| 173 | dataLog( |
| 174 | "Bad Flush merge at edge ", *predecessor, " -> ", *block, |
| 175 | ", local variable r", m_live.operandForIndex(j), ": ", *predecessor, |
fpizlo@apple.com | a62d482 | 2013-10-06 04:22:43 +0000 | [diff] [blame] | 176 | " has ", predecessorFlush, " and ", *block, " has ", myFlush, ".\n"); |
oliver@apple.com | 827d2cf | 2013-07-25 04:04:45 +0000 | [diff] [blame] | 177 | dataLog("IR at time of error:\n"); |
| 178 | m_graph.dump(); |
| 179 | CRASH(); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
fpizlo@apple.com | a62d482 | 2013-10-06 04:22:43 +0000 | [diff] [blame] | 184 | NO_RETURN_DUE_TO_CRASH void reportError(Node* node) |
oliver@apple.com | 827d2cf | 2013-07-25 04:04:45 +0000 | [diff] [blame] | 185 | { |
oliver@apple.com | 827d2cf | 2013-07-25 04:04:45 +0000 | [diff] [blame] | 186 | dataLog( |
fpizlo@apple.com | a62d482 | 2013-10-06 04:22:43 +0000 | [diff] [blame] | 187 | "Bad flush merge at node ", node, ", r", node->local(), ": node claims ", |
| 188 | node->variableAccessData()->flushedAt(), " but backwards flow claims ", |
| 189 | m_live.operand(node->local()), ".\n"); |
oliver@apple.com | 827d2cf | 2013-07-25 04:04:45 +0000 | [diff] [blame] | 190 | dataLog("IR at time of error:\n"); |
| 191 | m_graph.dump(); |
| 192 | CRASH(); |
| 193 | } |
| 194 | |
oliver@apple.com | 827d2cf | 2013-07-25 04:04:45 +0000 | [diff] [blame] | 195 | bool m_changed; |
fpizlo@apple.com | a62d482 | 2013-10-06 04:22:43 +0000 | [diff] [blame] | 196 | Operands<FlushedAt> m_live; |
oliver@apple.com | 827d2cf | 2013-07-25 04:04:45 +0000 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | bool performFlushLivenessAnalysis(Graph& graph) |
| 200 | { |
| 201 | SamplingRegion samplingRegion("DFG Flush-Liveness Analysis Phase"); |
| 202 | return runPhase<FlushLivenessAnalysisPhase>(graph); |
| 203 | } |
| 204 | |
| 205 | } } // namespace JSC::DFG |
| 206 | |
| 207 | #endif // ENABLE(DFG_JIT) |
| 208 | |