DFG should have adaptive structure watchpoints
[WebKit-https.git] / Source / JavaScriptCore / dfg / DFGAdaptiveInferredPropertyValueWatchpoint.cpp
1 /*
2  * Copyright (C) 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 "DFGAdaptiveInferredPropertyValueWatchpoint.h"
28
29 #if ENABLE(DFG_JIT)
30
31 #include "JSCInlines.h"
32
33 namespace JSC { namespace DFG {
34
35 AdaptiveInferredPropertyValueWatchpoint::AdaptiveInferredPropertyValueWatchpoint(
36     const ObjectPropertyCondition& key,
37     CodeBlock* codeBlock)
38     : m_key(key)
39     , m_codeBlock(codeBlock)
40 {
41     RELEASE_ASSERT(key.kind() == PropertyCondition::Equivalence);
42 }
43
44 void AdaptiveInferredPropertyValueWatchpoint::install()
45 {
46     RELEASE_ASSERT(m_key.isWatchable());
47     
48     m_key.object()->structure()->addTransitionWatchpoint(&m_structureWatchpoint);
49     
50     PropertyOffset offset = m_key.object()->structure()->getConcurrently(m_key.uid());
51     WatchpointSet* set = m_key.object()->structure()->propertyReplacementWatchpointSet(offset);
52     set->add(&m_propertyWatchpoint);
53 }
54
55 void AdaptiveInferredPropertyValueWatchpoint::fire(const FireDetail& detail)
56 {
57     // One of the watchpoints fired, but the other one didn't. Make sure that neither of them are
58     // in any set anymore. This simplifies things by allowing us to reinstall the watchpoints
59     // wherever from scratch.
60     if (m_structureWatchpoint.isOnList())
61         m_structureWatchpoint.remove();
62     if (m_propertyWatchpoint.isOnList())
63         m_propertyWatchpoint.remove();
64     
65     if (m_key.isWatchable(PropertyCondition::EnsureWatchability)) {
66         install();
67         return;
68     }
69     
70     if (DFG::shouldShowDisassembly()) {
71         dataLog(
72             "Firing watchpoint ", RawPointer(this), " (", m_key, ") on ", *m_codeBlock, "\n");
73     }
74     
75     StringPrintStream out;
76     out.print("Adaptation of ", m_key, " failed: ", detail);
77     
78     StringFireDetail stringDetail(out.toCString().data());
79     
80     m_codeBlock->jettison(
81         Profiler::JettisonDueToUnprofiledWatchpoint, CountReoptimization, &stringDetail);
82 }
83
84 void AdaptiveInferredPropertyValueWatchpoint::StructureWatchpoint::fireInternal(
85     const FireDetail& detail)
86 {
87     ptrdiff_t myOffset = OBJECT_OFFSETOF(
88         AdaptiveInferredPropertyValueWatchpoint, m_structureWatchpoint);
89     
90     AdaptiveInferredPropertyValueWatchpoint* parent =
91         bitwise_cast<AdaptiveInferredPropertyValueWatchpoint*>(
92             bitwise_cast<char*>(this) - myOffset);
93     
94     parent->fire(detail);
95 }
96
97 void AdaptiveInferredPropertyValueWatchpoint::PropertyWatchpoint::fireInternal(
98     const FireDetail& detail)
99 {
100     ptrdiff_t myOffset = OBJECT_OFFSETOF(
101         AdaptiveInferredPropertyValueWatchpoint, m_propertyWatchpoint);
102     
103     AdaptiveInferredPropertyValueWatchpoint* parent =
104         bitwise_cast<AdaptiveInferredPropertyValueWatchpoint*>(
105             bitwise_cast<char*>(this) - myOffset);
106     
107     parent->fire(detail);
108 }
109
110 } } // namespace JSC::DFG
111
112 #endif // ENABLE(DFG_JIT)
113