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"
27#include "DFGInvalidationPointInjectionPhase.h"
28
29#if ENABLE(DFG_JIT)
30
31#include "DFGClobberize.h"
32#include "DFGGraph.h"
33#include "DFGInsertionSet.h"
34#include "DFGPhase.h"
35#include "JSCInlines.h"
36
37namespace JSC { namespace DFG {
38
39class InvalidationPointInjectionPhase : public Phase {
40    static const bool verbose = false;
41
42public:
43    InvalidationPointInjectionPhase(Graph& graph)
44        : Phase(graph, "invalidation point injection")
45        , m_insertionSet(graph)
46    {
47    }
48
49    bool run()
50    {
51        ASSERT(m_graph.m_form != SSA);
52
53        BitVector blocksThatNeedInvalidationPoints;
54
55        for (BlockIndex blockIndex = m_graph.numBlocks(); blockIndex--;) {
56            BasicBlock* block = m_graph.block(blockIndex);
57            if (!block)
58                continue;
59
60            for (unsigned nodeIndex = 0; nodeIndex < block->size(); ++nodeIndex)
61                handle(nodeIndex, block->at(nodeIndex));
62
63            // Note: this assumes that control flow occurs at bytecode instruction boundaries.
64            if (m_originThatHadFire.isSet()) {
65                for (unsigned i = block->numSuccessors(); i--;)
66                    blocksThatNeedInvalidationPoints.set(block->successor(i)->index);
67            }
68
69            m_insertionSet.execute(block);
70        }
71
72        for (BlockIndex blockIndex = m_graph.numBlocks(); blockIndex--;) {
73            if (!blocksThatNeedInvalidationPoints.get(blockIndex))
74                continue;
75
76            BasicBlock* block = m_graph.block(blockIndex);
77            insertInvalidationCheck(0, block->at(0));
78            m_insertionSet.execute(block);
79        }
80
81        return true;
82    }
83
84private:
85    void handle(unsigned nodeIndex, Node* node)
86    {
87        if (m_originThatHadFire.isSet() && m_originThatHadFire != node->origin.forExit) {
88            insertInvalidationCheck(nodeIndex, node);
89            m_originThatHadFire = CodeOrigin();
90        }
91
92        if (writesOverlap(m_graph, node, Watchpoint_fire))
93            m_originThatHadFire = node->origin.forExit;
94    }
95
96    void insertInvalidationCheck(unsigned nodeIndex, Node* node)
97    {
98        m_insertionSet.insertNode(nodeIndex, SpecNone, InvalidationPoint, node->origin);
99    }
100
101    CodeOrigin m_originThatHadFire;
102    InsertionSet m_insertionSet;
103};
104
105bool performInvalidationPointInjection(Graph& graph)
106{
107    SamplingRegion samplingRegion("DFG Invalidation Point Injection Phase");
108    return runPhase<InvalidationPointInjectionPhase>(graph);
109}
110
111} } // namespace JSC::DFG
112
113#endif // ENABLE(DFG_JIT)
114
115