1/*
2 * Copyright (C) 2013-2018 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#pragma once
27
28#include "CompilationResult.h"
29#include "DFGCompilationKey.h"
30#include "DFGCompilationMode.h"
31#include "DFGDesiredGlobalProperties.h"
32#include "DFGDesiredIdentifiers.h"
33#include "DFGDesiredTransitions.h"
34#include "DFGDesiredWatchpoints.h"
35#include "DFGDesiredWeakReferences.h"
36#include "DFGFinalizer.h"
37#include "DeferredCompilationCallback.h"
38#include "Operands.h"
39#include "ProfilerCompilation.h"
40#include "RecordedStatuses.h"
41#include <wtf/HashMap.h>
42#include <wtf/ThreadSafeRefCounted.h>
43
44namespace JSC {
45
46class CodeBlock;
47class SlotVisitor;
48
49namespace DFG {
50
51class ThreadData;
52
53#if ENABLE(DFG_JIT)
54
55class Plan : public ThreadSafeRefCounted<Plan> {
56public:
57 Plan(
58 CodeBlock* codeBlockToCompile, CodeBlock* profiledDFGCodeBlock,
59 CompilationMode, BytecodeIndex osrEntryBytecodeIndex,
60 const Operands<Optional<JSValue>>& mustHandleValues);
61 ~Plan();
62
63 void compileInThread(ThreadData*);
64
65 CompilationResult finalizeWithoutNotifyingCallback();
66 void finalizeAndNotifyCallback();
67
68 void notifyCompiling();
69 void notifyReady();
70
71 CompilationKey key();
72
73 template<typename Func>
74 void iterateCodeBlocksForGC(const Func&);
75 void checkLivenessAndVisitChildren(SlotVisitor&);
76 bool isKnownToBeLiveDuringGC();
77 void finalizeInGC();
78 void cancel();
79
80 bool canTierUpAndOSREnter() const { return !m_tierUpAndOSREnterBytecodes.isEmpty(); }
81
82 void cleanMustHandleValuesIfNecessary();
83
84 VM* vm() const { return m_vm; }
85
86 CodeBlock* codeBlock() { return m_codeBlock; }
87
88 bool isFTL() const { return DFG::isFTL(m_mode); }
89 CompilationMode mode() const { return m_mode; }
90 BytecodeIndex osrEntryBytecodeIndex() const { return m_osrEntryBytecodeIndex; }
91 const Operands<Optional<JSValue>>& mustHandleValues() const { return m_mustHandleValues; }
92 ThreadData* threadData() const { return m_threadData; }
93 Profiler::Compilation* compilation() const { return m_compilation.get(); }
94
95 Finalizer* finalizer() const { return m_finalizer.get(); }
96 void setFinalizer(std::unique_ptr<Finalizer>&& finalizer) { m_finalizer = WTFMove(finalizer); }
97
98 RefPtr<InlineCallFrameSet> inlineCallFrames() const { return m_inlineCallFrames; }
99 DesiredWatchpoints& watchpoints() { return m_watchpoints; }
100 DesiredIdentifiers& identifiers() { return m_identifiers; }
101 DesiredWeakReferences& weakReferences() { return m_weakReferences; }
102 DesiredTransitions& transitions() { return m_transitions; }
103 DesiredGlobalProperties& globalProperties() { return m_globalProperties; }
104 RecordedStatuses& recordedStatuses() { return m_recordedStatuses; }
105
106 bool willTryToTierUp() const { return m_willTryToTierUp; }
107 void setWillTryToTierUp(bool willTryToTierUp) { m_willTryToTierUp = willTryToTierUp; }
108
109 HashMap<BytecodeIndex, Vector<BytecodeIndex>>& tierUpInLoopHierarchy() { return m_tierUpInLoopHierarchy; }
110 Vector<BytecodeIndex>& tierUpAndOSREnterBytecodes() { return m_tierUpAndOSREnterBytecodes; }
111
112 enum Stage { Preparing, Compiling, Ready, Cancelled };
113 Stage stage() const { return m_stage; }
114
115 DeferredCompilationCallback* callback() const { return m_callback.get(); }
116 void setCallback(Ref<DeferredCompilationCallback>&& callback) { m_callback = WTFMove(callback); }
117
118private:
119 bool computeCompileTimes() const;
120 bool reportCompileTimes() const;
121
122 enum CompilationPath { FailPath, DFGPath, FTLPath, CancelPath };
123 CompilationPath compileInThreadImpl();
124
125 bool isStillValidOnMainThread();
126 bool isStillValid();
127 void reallyAdd(CommonData*);
128
129 // Warning: pretty much all of the pointer fields in this object get nulled by cancel(). So, if
130 // you're writing code that is callable on the cancel path, be sure to null check everything!
131
132 CompilationMode m_mode;
133
134 VM* m_vm;
135
136 // These can be raw pointers because we visit them during every GC in checkLivenessAndVisitChildren.
137 CodeBlock* m_codeBlock;
138 CodeBlock* m_profiledDFGCodeBlock;
139
140 Operands<Optional<JSValue>> m_mustHandleValues;
141 bool m_mustHandleValuesMayIncludeGarbage { true };
142 Lock m_mustHandleValueCleaningLock;
143
144 bool m_willTryToTierUp { false };
145
146 const BytecodeIndex m_osrEntryBytecodeIndex;
147
148 ThreadData* m_threadData;
149
150 RefPtr<Profiler::Compilation> m_compilation;
151
152 std::unique_ptr<Finalizer> m_finalizer;
153
154 RefPtr<InlineCallFrameSet> m_inlineCallFrames;
155 DesiredWatchpoints m_watchpoints;
156 DesiredIdentifiers m_identifiers;
157 DesiredWeakReferences m_weakReferences;
158 DesiredTransitions m_transitions;
159 DesiredGlobalProperties m_globalProperties;
160 RecordedStatuses m_recordedStatuses;
161
162 HashMap<BytecodeIndex, Vector<BytecodeIndex>> m_tierUpInLoopHierarchy;
163 Vector<BytecodeIndex> m_tierUpAndOSREnterBytecodes;
164
165 Stage m_stage;
166
167 RefPtr<DeferredCompilationCallback> m_callback;
168
169 MonotonicTime m_timeBeforeFTL;
170};
171
172#endif // ENABLE(DFG_JIT)
173
174} } // namespace JSC::DFG
175