The DFG should be able to tier-up and OSR enter into the FTL
[WebKit-https.git] / Source / JavaScriptCore / dfg / DFGJITCode.h
1 /*
2  * Copyright (C) 2013 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 #ifndef DFGJITCode_h
27 #define DFGJITCode_h
28
29 #include <wtf/Platform.h>
30
31 #if ENABLE(DFG_JIT)
32
33 #include "CompilationResult.h"
34 #include "DFGCommonData.h"
35 #include "DFGMinifiedGraph.h"
36 #include "DFGOSREntry.h"
37 #include "DFGOSRExit.h"
38 #include "DFGVariableEventStream.h"
39 #include "ExecutionCounter.h"
40 #include "JITCode.h"
41 #include "JumpReplacementWatchpoint.h"
42 #include <wtf/SegmentedVector.h>
43
44 namespace JSC { namespace DFG {
45
46 class JITCompiler;
47
48 class JITCode : public DirectJITCode {
49 public:
50     JITCode();
51     ~JITCode();
52     
53     CommonData* dfgCommon();
54     JITCode* dfg();
55     
56     OSREntryData* appendOSREntryData(unsigned bytecodeIndex, unsigned machineCodeOffset)
57     {
58         DFG::OSREntryData entry;
59         entry.m_bytecodeIndex = bytecodeIndex;
60         entry.m_machineCodeOffset = machineCodeOffset;
61         osrEntry.append(entry);
62         return &osrEntry.last();
63     }
64     
65     OSREntryData* osrEntryDataForBytecodeIndex(unsigned bytecodeIndex)
66     {
67         return tryBinarySearch<OSREntryData, unsigned>(
68             osrEntry, osrEntry.size(), bytecodeIndex,
69             getOSREntryDataBytecodeIndex);
70     }
71     
72     unsigned appendOSRExit(const OSRExit& exit)
73     {
74         unsigned result = osrExit.size();
75         osrExit.append(exit);
76         return result;
77     }
78     
79     OSRExit& lastOSRExit()
80     {
81         return osrExit.last();
82     }
83     
84     unsigned appendSpeculationRecovery(const SpeculationRecovery& recovery)
85     {
86         unsigned result = speculationRecovery.size();
87         speculationRecovery.append(recovery);
88         return result;
89     }
90     
91     unsigned appendWatchpoint(const JumpReplacementWatchpoint& watchpoint)
92     {
93         unsigned result = watchpoints.size();
94         watchpoints.append(watchpoint);
95         return result;
96     }
97     
98     void reconstruct(
99         CodeBlock*, CodeOrigin, unsigned streamIndex, Operands<ValueRecovery>& result);
100     
101     // This is only applicable if we're at a point where all values are spilled to the
102     // stack. Currently, it also has the restriction that the values must be in their
103     // bytecode-designated stack slots.
104     void reconstruct(
105         ExecState*, CodeBlock*, CodeOrigin, unsigned streamIndex, Operands<JSValue>& result);
106
107 #if ENABLE(FTL_JIT)
108     // NB. All of these methods take CodeBlock* because they may want to use
109     // CodeBlock's logic about scaling thresholds. It should be a DFG CodeBlock.
110     
111     bool checkIfOptimizationThresholdReached(CodeBlock*);
112     void optimizeNextInvocation(CodeBlock*);
113     void dontOptimizeAnytimeSoon(CodeBlock*);
114     void optimizeAfterWarmUp(CodeBlock*);
115     void optimizeSoon(CodeBlock*);
116     void forceOptimizationSlowPathConcurrently(CodeBlock*);
117     void setOptimizationThresholdBasedOnCompilationResult(CodeBlock*, CompilationResult);
118 #endif // ENABLE(FTL_JIT)
119     
120     void shrinkToFit();
121     
122 private:
123     friend class JITCompiler; // Allow JITCompiler to call setCodeRef().
124
125 public:
126     CommonData common;
127     Vector<DFG::OSREntryData> osrEntry;
128     SegmentedVector<DFG::OSRExit, 8> osrExit;
129     Vector<DFG::SpeculationRecovery> speculationRecovery;
130     SegmentedVector<JumpReplacementWatchpoint, 1, 0> watchpoints;
131     DFG::VariableEventStream variableEventStream;
132     DFG::MinifiedGraph minifiedDFG;
133 #if ENABLE(FTL_JIT)
134     ExecutionCounter tierUpCounter;
135     RefPtr<CodeBlock> osrEntryBlock;
136 #endif // ENABLE(FTL_JIT)
137 };
138
139 } } // namespace JSC::DFG
140
141 #endif // ENABLE(DFG_JIT)
142
143 #endif // DFGJITCode_h
144