Bmalloc and GC should put auxiliaries (butterflies, typed array backing stores) in...
[WebKit.git] / Source / JavaScriptCore / dfg / DFGFixedButterflyAccessUncagingPhase.cpp
1 /*
2  * Copyright (C) 2017 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. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "DFGFixedButterflyAccessUncagingPhase.h"
28
29 #if ENABLE(DFG_JIT)
30
31 #include "DFGClobberize.h"
32 #include "DFGGraph.h"
33 #include "DFGPhase.h"
34 #include "JSCInlines.h"
35 #include <wtf/IndexSet.h>
36
37 namespace JSC { namespace DFG {
38
39 namespace {
40
41 class FixedButterflyAccessUncagingPhase : public Phase {
42 public:
43     FixedButterflyAccessUncagingPhase(Graph& graph)
44         : Phase(graph, "fixed butterfly access uncaging")
45     {
46     }
47     
48     bool run()
49     {
50         IndexSet<Node*> needCaging;
51         
52         bool changed = true;
53         while (changed) {
54             changed = false;
55             for (BasicBlock* block : m_graph.blocksInNaturalOrder()) {
56                 for (Node* node : *block) {
57                     switch (node->op()) {
58                     // FIXME: Check again how badly we need this. It might not be worth it.
59                     // https://bugs.webkit.org/show_bug.cgi?id=175044
60                     case GetByOffset:
61                     case PutByOffset:
62                     case GetGetterSetterByOffset:
63                     case GetArrayLength:
64                     case GetVectorLength:
65                         break;
66                         
67                     case Upsilon:
68                         if (needCaging.contains(node->phi()))
69                             changed |= needCaging.add(node->child1().node());
70                         break;
71                         
72                     default:
73                         // FIXME: We could possibly make this more precise. We really only care about whether
74                         // this can read/write butterfly contents.
75                         // https://bugs.webkit.org/show_bug.cgi?id=174926
76                         if (!accessesOverlap(m_graph, node, Heap))
77                             break;
78                     
79                         m_graph.doToChildren(
80                             node,
81                             [&] (Edge& edge) {
82                                 changed |= needCaging.add(edge.node());
83                             });
84                         break;
85                     }
86                 }
87             }
88         }
89         
90         bool didOptimize = false;
91         for (BasicBlock* block : m_graph.blocksInNaturalOrder()) {
92             for (Node* node : *block) {
93                 if (node->op() == GetButterfly && !needCaging.contains(node)) {
94                     node->setOp(GetButterflyWithoutCaging);
95                     didOptimize = true;
96                 }
97             }
98         }
99         
100         return didOptimize;
101     }
102 };
103
104 } // anonymous namespace
105
106 bool performFixedButterflyAccessUncaging(Graph& graph)
107 {
108     return runPhase<FixedButterflyAccessUncagingPhase>(graph);
109 }
110
111 } } // namespace JSC::DFG
112
113 #endif // ENABLE(DFG_JIT)
114