| 1 | /* |
| 2 | * Copyright (C) 2013, 2015 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" |
| 27 | #include "DFGLoopPreHeaderCreationPhase.h" |
| 28 | |
| 29 | #if ENABLE(DFG_JIT) |
| 30 | |
| 31 | #include "DFGBasicBlockInlines.h" |
| 32 | #include "DFGBlockInsertionSet.h" |
| 33 | #include "DFGDominators.h" |
| 34 | #include "DFGGraph.h" |
| 35 | #include "DFGNaturalLoops.h" |
| 36 | #include "DFGPhase.h" |
| 37 | #include "JSCInlines.h" |
| 38 | #include <wtf/HashMap.h> |
| 39 | |
| 40 | namespace JSC { namespace DFG { |
| 41 | |
| 42 | BasicBlock* (Graph& graph, BlockInsertionSet& insertionSet, BasicBlock* block) |
| 43 | { |
| 44 | ASSERT_WITH_MESSAGE(!graph.isRoot(block), "A CFG root should not be in a loop" ); |
| 45 | |
| 46 | // FIXME: If we run this utility on SSA IR, then we may end up with a bizarre arrangement of |
| 47 | // Upsilons and Phis, like: |
| 48 | // |
| 49 | // BB#1: |
| 50 | // Upsilon(@a, ^p) |
| 51 | // Jump(#3) |
| 52 | // |
| 53 | // BB#2: |
| 54 | // Upsilon(@b, ^p) |
| 55 | // Jump(#3) |
| 56 | // |
| 57 | // BB#3: |
| 58 | // Jump(#4) |
| 59 | // |
| 60 | // BB#4: |
| 61 | // p: Phi() |
| 62 | // |
| 63 | // Notice how the Upsilons are not in the predecessor of the Phi anymore. It's not clear if this |
| 64 | // would be bad. Probably not, but it's weird anyway. We should add a validation rule, and we |
| 65 | // should implement a Upsilon/Phi canonicalization that handles this by calling into the |
| 66 | // SSACalculator and treating the Upsilons as Defs and rebuilding the Phis from scratch. |
| 67 | // |
| 68 | // https://bugs.webkit.org/show_bug.cgi?id=148587 |
| 69 | |
| 70 | // Determine a good frequency for the pre-header. It's definitely not the frequency of the loop body. |
| 71 | // Instead, we use the max of the frequencies of the loop body's non-loop predecessors. |
| 72 | float frequency = 0; |
| 73 | for (BasicBlock* predecessor : block->predecessors) { |
| 74 | ASSERT(graph.m_form != SSA); |
| 75 | if (graph.m_cpsDominators->dominates(block, predecessor)) |
| 76 | continue; |
| 77 | frequency = std::max(frequency, predecessor->executionCount); |
| 78 | } |
| 79 | BasicBlock* = insertionSet.insertBefore(block, frequency); |
| 80 | |
| 81 | // FIXME: It would be great if we put some effort into enabling exitOK at this origin, if it |
| 82 | // happens to be unset. It might not be set because the loop header (i.e. "block") has Phis in it. |
| 83 | // Phis have to have exitOK=false. There are a few ways to try to set exitOK: |
| 84 | // |
| 85 | // - Regenerate an exit origin by proving that we are at an exit origin boundary. If all of the |
| 86 | // predecessors' terminals have different exit origins than the exit origin of head of block, |
| 87 | // then we can leverage the assumption that exit origin boundaries can always exit. We could |
| 88 | // extend this further, and say that we will set exitOK even if a predecessor's terminal has the |
| 89 | // same exit origin, but so long it hadn't done anything that clobbers exit since the start of |
| 90 | // the origin. |
| 91 | // |
| 92 | // - Analyze the Phi's and MovHint's at the head of block. If prior to the ExitOK there are only |
| 93 | // Phi's and MovHint's, we could "roll them back" by proving that for each of the MovHints, the |
| 94 | // referenced Phi has a child that dominates the pre-header, and that child is the node that is |
| 95 | // OSR-available at the local being MovHinted. |
| 96 | // |
| 97 | // Note that there are some obviously wrong ways to try to set exitOK. For example, we cannot |
| 98 | // simply use the origin of our predecessors, since in bytecode that could be *any* kind of |
| 99 | // instruction. It may not even be a control flow construct, if we had lowered some non-control |
| 100 | // bytecode operation into DFG IR that has control flow. Hence, we really do need to try to use the |
| 101 | // origin of the head of the loop header. |
| 102 | // |
| 103 | // https://bugs.webkit.org/show_bug.cgi?id=148586 |
| 104 | preHeader->appendNode( |
| 105 | graph, SpecNone, Jump, block->at(0)->origin, OpInfo(block)); |
| 106 | |
| 107 | for (unsigned predecessorIndex = 0; predecessorIndex < block->predecessors.size(); predecessorIndex++) { |
| 108 | BasicBlock* predecessor = block->predecessors[predecessorIndex]; |
| 109 | if (graph.m_cpsDominators->dominates(block, predecessor)) |
| 110 | continue; |
| 111 | block->predecessors[predecessorIndex--] = block->predecessors.last(); |
| 112 | block->predecessors.removeLast(); |
| 113 | for (unsigned successorIndex = predecessor->numSuccessors(); successorIndex--;) { |
| 114 | BasicBlock*& successor = predecessor->successor(successorIndex); |
| 115 | if (successor != block) |
| 116 | continue; |
| 117 | successor = preHeader; |
| 118 | preHeader->predecessors.append(predecessor); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | block->predecessors.append(preHeader); |
| 123 | return preHeader; |
| 124 | } |
| 125 | |
| 126 | class : public Phase { |
| 127 | public: |
| 128 | (Graph& graph) |
| 129 | : Phase(graph, "loop pre-header creation" ) |
| 130 | , m_insertionSet(graph) |
| 131 | { |
| 132 | } |
| 133 | |
| 134 | bool () |
| 135 | { |
| 136 | m_graph.ensureCPSDominators(); |
| 137 | m_graph.ensureCPSNaturalLoops(); |
| 138 | |
| 139 | for (unsigned loopIndex = m_graph.m_cpsNaturalLoops->numLoops(); loopIndex--;) { |
| 140 | const CPSNaturalLoop& loop = m_graph.m_cpsNaturalLoops->loop(loopIndex); |
| 141 | BasicBlock* = nullptr; |
| 142 | bool = false; |
| 143 | for (unsigned predecessorIndex = loop.header().node()->predecessors.size(); predecessorIndex--;) { |
| 144 | BasicBlock* predecessor = loop.header().node()->predecessors[predecessorIndex]; |
| 145 | if (m_graph.m_cpsDominators->dominates(loop.header().node(), predecessor)) |
| 146 | continue; |
| 147 | if (!existingPreHeader) { |
| 148 | existingPreHeader = predecessor; |
| 149 | continue; |
| 150 | } |
| 151 | // We won't have duplicate entries in the predecessors list. |
| 152 | DFG_ASSERT(m_graph, nullptr, existingPreHeader != predecessor); |
| 153 | needsNewPreHeader = true; |
| 154 | break; |
| 155 | } |
| 156 | |
| 157 | // This phase should only be run on a DFG where unreachable blocks have been pruned. |
| 158 | // We also don't allow loops back to root. This means that every loop header has got |
| 159 | // to have a pre-header. |
| 160 | DFG_ASSERT(m_graph, nullptr, existingPreHeader); |
| 161 | |
| 162 | // We are looking at the predecessors of a loop header. A loop header has to have |
| 163 | // some predecessor other than the pre-header. We must have broken critical edges |
| 164 | // because that is the DFG SSA convention. Therefore, each predecessor of the loop |
| 165 | // header must have only one successor. |
| 166 | DFG_ASSERT(m_graph, nullptr, existingPreHeader->terminal()->op() == Jump, existingPreHeader->terminal()->op()); |
| 167 | |
| 168 | // A pre-header is most useful if it's possible to exit from its terminal. Hence |
| 169 | // if the terminal of the existing pre-header doesn't allow for exit, but the first |
| 170 | // origin of the loop header does, then we should create a new pre-header. |
| 171 | if (!needsNewPreHeader && loop.header().node()->at(0)->origin.exitOK |
| 172 | && !existingPreHeader->terminal()->origin.exitOK) |
| 173 | needsNewPreHeader = true; |
| 174 | |
| 175 | if (!needsNewPreHeader) |
| 176 | continue; |
| 177 | |
| 178 | createPreHeader(m_graph, m_insertionSet, loop.header().node()); |
| 179 | } |
| 180 | |
| 181 | return m_insertionSet.execute(); |
| 182 | } |
| 183 | |
| 184 | BlockInsertionSet ; |
| 185 | }; |
| 186 | |
| 187 | bool (Graph& graph) |
| 188 | { |
| 189 | return runPhase<LoopPreHeaderCreationPhase>(graph); |
| 190 | } |
| 191 | |
| 192 | } } // namespace JSC::DFG |
| 193 | |
| 194 | #endif // ENABLE(DFG_JIT) |
| 195 | |
| 196 | |
| 197 | |