| 1 | /* |
| 2 | * Copyright (C) 2016 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 | #pragma once |
| 27 | |
| 28 | #if ENABLE(DFG_JIT) |
| 29 | |
| 30 | #include "DFGBackwardsDominators.h" |
| 31 | #include "DFGDominators.h" |
| 32 | |
| 33 | namespace JSC { namespace DFG { |
| 34 | |
| 35 | class ControlEquivalenceAnalysis { |
| 36 | WTF_MAKE_NONCOPYABLE(ControlEquivalenceAnalysis); |
| 37 | WTF_MAKE_FAST_ALLOCATED; |
| 38 | public: |
| 39 | ControlEquivalenceAnalysis(Graph& graph) |
| 40 | : m_dominators(graph.ensureSSADominators()) |
| 41 | , m_backwardsDominators(graph.ensureBackwardsDominators()) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | // This returns true iff: |
| 46 | // |
| 47 | // - If b executes then a must have executed before it (a dominates b). |
| 48 | // - If a executes then b will execute after it (b backwards-dominates a). |
| 49 | // |
| 50 | // Note that like Dominators and BackwardsDominators, this analysis ignores OSR: |
| 51 | // |
| 52 | // - This may return true even if we OSR enter in beteen a and b. OSR entry would mean that b |
| 53 | // could execute even if a had not executed. This is impossible in DFG SSA but it's possible |
| 54 | // in DFG CPS. |
| 55 | // - This may return true even if we OSR exit in between a and b. OSR exit would mean that a |
| 56 | // could execute even though b will not execute. This is possible in all forms of DFG IR. |
| 57 | // |
| 58 | // In DFG SSA you only have to worry about the definition being weaked by exits. This is usually |
| 59 | // OK, since we use this analysis to determine the cost of moving exits from one block to |
| 60 | // another. If we move an exit from b to a and a equivalently dominates b then at worst we have |
| 61 | // made the exit happen sooner. If we move an exit from b to a and a dominates b but not |
| 62 | // equivalently then we've done something much worse: the program may now exit even if it would |
| 63 | // not have ever exited before. |
| 64 | bool dominatesEquivalently(BasicBlock* a, BasicBlock* b) |
| 65 | { |
| 66 | return m_dominators.dominates(a, b) |
| 67 | && m_backwardsDominators.dominates(b, a); |
| 68 | } |
| 69 | |
| 70 | // This returns true iff the execution of a implies that b also executes and vice-versa. |
| 71 | bool areEquivalent(BasicBlock* a, BasicBlock* b) |
| 72 | { |
| 73 | return dominatesEquivalently(a, b) |
| 74 | || dominatesEquivalently(b, a); |
| 75 | } |
| 76 | |
| 77 | private: |
| 78 | SSADominators& m_dominators; |
| 79 | BackwardsDominators& m_backwardsDominators; |
| 80 | }; |
| 81 | |
| 82 | } } // namespace JSC::DFG |
| 83 | |
| 84 | #endif // ENABLE(DFG_JIT) |
| 85 | |