DFGWatchpointCollectionPhase.cpp 7.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
/*
 * Copyright (C) 2013 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

#include "config.h"
#include "DFGWatchpointCollectionPhase.h"

#if ENABLE(DFG_JIT)

#include "ArrayPrototype.h"
#include "DFGClobberize.h"
#include "DFGGraph.h"
#include "DFGPhase.h"
#include "Operations.h"

namespace JSC { namespace DFG {

class WatchpointCollectionPhase : public Phase {
    static const bool verbose = false;
    
public:
    WatchpointCollectionPhase(Graph& graph)
        : Phase(graph, "watchpoint collection")
    {
    }
    
    bool run()
    {
        for (BlockIndex blockIndex = m_graph.numBlocks(); blockIndex--;) {
            BasicBlock* block = m_graph.block(blockIndex);
            if (!block)
                continue;
            
            for (unsigned nodeIndex = block->size(); nodeIndex--;) {
                m_node = block->at(nodeIndex);
                handle();
            }
        }
        
        return true;
    }

private:
    void handle()
    {
        DFG_NODE_DO_TO_CHILDREN(m_graph, m_node, handleEdge);
        
        switch (m_node->op()) {
        case CompareEqConstant:
        case IsUndefined:
            handleMasqueradesAsUndefined();
            break;
            
        case CompareEq:
            if (m_node->isBinaryUseKind(ObjectUse)
                || (m_node->child1().useKind() == ObjectUse && m_node->child2().useKind() == ObjectOrOtherUse)
                || (m_node->child1().useKind() == ObjectOrOtherUse && m_node->child2().useKind() == ObjectUse))
                handleMasqueradesAsUndefined();
            break;
            
        case LogicalNot:
        case Branch:
            if (m_node->child1().useKind() == ObjectOrOtherUse)
                handleMasqueradesAsUndefined();
            break;
            
        case GetByVal:
            if (m_node->arrayMode().type() == Array::Double
                && m_node->arrayMode().isSaneChain()) {
                addLazily(globalObject()->arrayPrototype()->structure()->transitionWatchpointSet());
                addLazily(globalObject()->objectPrototype()->structure()->transitionWatchpointSet());
            }
            
            if (m_node->arrayMode().type() == Array::String)
                handleStringGetByVal();
fpizlo@apple.com's avatar
fpizlo@apple.com committed
97 98 99 100 101 102 103 104 105 106 107 108 109

            if (JSArrayBufferView* view = m_graph.tryGetFoldableView(m_node->child1().node(), m_node->arrayMode()))
                addLazily(view);
            break;
            
        case PutByVal:
            if (JSArrayBufferView* view = m_graph.tryGetFoldableView(m_graph.varArgChild(m_node, 0).node(), m_node->arrayMode()))
                addLazily(view);
            break;
            
        case GetArrayLength:
            if (JSArrayBufferView* view = m_graph.tryGetFoldableView(m_node->child1().node(), m_node->arrayMode()))
                addLazily(view);
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
            break;
            
        case StringCharAt:
            handleStringGetByVal();
            break;
            
        case NewArray:
        case NewArrayWithSize:
        case NewArrayBuffer:
            if (!globalObject()->isHavingABadTime() && !hasArrayStorage(m_node->indexingType()))
                addLazily(globalObject()->havingABadTimeWatchpoint());
            break;
            
        case AllocationProfileWatchpoint:
            addLazily(jsCast<JSFunction*>(m_node->function())->allocationProfileWatchpointSet());
            break;
            
        case StructureTransitionWatchpoint:
            m_graph.watchpoints().addLazily(
                m_node->codeOrigin,
                m_node->child1()->op() == WeakJSConstant ? BadWeakConstantCacheWatchpoint : BadCacheWatchpoint,
                m_node->structure()->transitionWatchpointSet());
            break;
            
134 135
        case VariableWatchpoint:
            addLazily(m_node->variableWatchpointSet());
136 137 138 139 140 141
            break;
            
        case VarInjectionWatchpoint:
            addLazily(globalObject()->varInjectionWatchpoint());
            break;
            
142 143
        case FunctionReentryWatchpoint:
            addLazily(m_node->symbolTable()->m_functionEnteredOnce);
fpizlo@apple.com's avatar
fpizlo@apple.com committed
144 145
            break;
            
fpizlo@apple.com's avatar
fpizlo@apple.com committed
146 147 148 149 150 151 152 153 154 155 156 157
        case GetIndexedPropertyStorage:
            if (JSArrayBufferView* view = m_graph.tryGetFoldableView(m_node->child1().node(), m_node->arrayMode())) {
                // FIXME: It would be awesome to be able to fold the property storage for
                // these GC-allocated typed arrays. For now it doesn't matter because the
                // most common use-cases for constant typed arrays involve large arrays with
                // aliased buffer views.
                if (view->mode() == FastTypedArray)
                    break;
                addLazily(view);
            }
            break;
            
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
        default:
            break;
        }
    }
    
    void handleEdge(Node*, Edge edge)
    {
        switch (edge.useKind()) {
        case StringObjectUse:
        case StringOrStringObjectUse: {
            Structure* stringObjectStructure = globalObject()->stringObjectStructure();
            Structure* stringPrototypeStructure = stringObjectStructure->storedPrototype().asCell()->structure();
            ASSERT(m_graph.watchpoints().isValidOrMixed(stringPrototypeStructure->transitionWatchpointSet()));
            
            m_graph.watchpoints().addLazily(
                m_node->codeOrigin, NotStringObject,
                stringPrototypeStructure->transitionWatchpointSet());
            break;
        }
            
        default:
            break;
        }
    }
    
    void handleMasqueradesAsUndefined()
    {
        if (m_graph.masqueradesAsUndefinedWatchpointIsStillValid(m_node->codeOrigin))
            addLazily(globalObject()->masqueradesAsUndefinedWatchpoint());
    }
    
    void handleStringGetByVal()
    {
        if (!m_node->arrayMode().isOutOfBounds())
            return;
        if (!globalObject()->stringPrototypeChainIsSane())
            return;
        addLazily(globalObject()->stringPrototype()->structure()->transitionWatchpointSet());
        addLazily(globalObject()->objectPrototype()->structure()->transitionWatchpointSet());
    }

    void addLazily(WatchpointSet* set)
    {
        m_graph.watchpoints().addLazily(set);
    }
    void addLazily(InlineWatchpointSet& set)
    {
        m_graph.watchpoints().addLazily(set);
    }
fpizlo@apple.com's avatar
fpizlo@apple.com committed
207 208 209 210
    void addLazily(JSArrayBufferView* view)
    {
        m_graph.watchpoints().addLazily(view);
    }
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
    
    JSGlobalObject* globalObject()
    {
        return m_graph.globalObjectFor(m_node->codeOrigin);
    }
    
    Node* m_node;
};

bool performWatchpointCollection(Graph& graph)
{
    SamplingRegion samplingRegion("DFG Watchpoint Collection Phase");
    return runPhase<WatchpointCollectionPhase>(graph);
}

} } // namespace JSC::DFG

#endif // ENABLE(DFG_JIT)