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 "DFGWatchpointCollectionPhase.h"
28
29#if ENABLE(DFG_JIT)
30
31#include "ArrayPrototype.h"
32#include "DFGClobberize.h"
33#include "DFGGraph.h"
34#include "DFGPhase.h"
35#include "JSCInlines.h"
36
37// FIXME: Remove this phase entirely by moving the addLazily() calls into either the backend or
38// into the phase that performs the optimization. Moving the calls into the backend makes the most
39// sense when the intermediate phases don't need to know that the watchpoint was set. Moving the
40// calls earlier usually only makes sense if the node's only purpose was to convey the need for
41// the watchpoint (like VarInjectionWatchpoint). But, it can also make sense if the fact that the
42// watchpoint was set enables other optimizations.
43// https://bugs.webkit.org/show_bug.cgi?id=144669
44
45namespace JSC { namespace DFG {
46
47class WatchpointCollectionPhase : public Phase {
48 static const bool verbose = false;
49
50public:
51 WatchpointCollectionPhase(Graph& graph)
52 : Phase(graph, "watchpoint collection")
53 {
54 }
55
56 bool run()
57 {
58 for (BlockIndex blockIndex = m_graph.numBlocks(); blockIndex--;) {
59 BasicBlock* block = m_graph.block(blockIndex);
60 if (!block)
61 continue;
62
63 for (unsigned nodeIndex = block->size(); nodeIndex--;) {
64 m_node = block->at(nodeIndex);
65 handle();
66 }
67 }
68
69 return true;
70 }
71
72private:
73 void handle()
74 {
75 switch (m_node->op()) {
76 case IsUndefined:
77 handleMasqueradesAsUndefined();
78 break;
79
80 case CompareEq:
81 if (m_node->isBinaryUseKind(ObjectUse)
82 || (m_node->child1().useKind() == ObjectUse && m_node->child2().useKind() == ObjectOrOtherUse)
83 || (m_node->child1().useKind() == ObjectOrOtherUse && m_node->child2().useKind() == ObjectUse)
84 || (m_node->child1().useKind() == KnownOtherUse || m_node->child2().useKind() == KnownOtherUse))
85 handleMasqueradesAsUndefined();
86 break;
87
88 case LogicalNot:
89 case Branch:
90 switch (m_node->child1().useKind()) {
91 case ObjectOrOtherUse:
92 case UntypedUse:
93 handleMasqueradesAsUndefined();
94 break;
95 default:
96 break;
97 }
98 break;
99
100 default:
101 break;
102 }
103 }
104
105 void handleMasqueradesAsUndefined()
106 {
107 if (m_graph.masqueradesAsUndefinedWatchpointIsStillValid(m_node->origin.semantic))
108 addLazily(globalObject()->masqueradesAsUndefinedWatchpoint());
109 }
110
111 void addLazily(WatchpointSet* set)
112 {
113 m_graph.watchpoints().addLazily(set);
114 }
115
116 JSGlobalObject* globalObject()
117 {
118 return m_graph.globalObjectFor(m_node->origin.semantic);
119 }
120
121 Node* m_node;
122};
123
124bool performWatchpointCollection(Graph& graph)
125{
126 return runPhase<WatchpointCollectionPhase>(graph);
127}
128
129} } // namespace JSC::DFG
130
131#endif // ENABLE(DFG_JIT)
132
133