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