[JSC] Handle new_async_func / new_async_func_exp in DFG / FTL
[WebKit-https.git] / Source / JavaScriptCore / dfg / DFGCapabilities.cpp
1 /*
2  * Copyright (C) 2011, 2013-2016 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 "DFGCapabilities.h"
28
29 #if ENABLE(DFG_JIT)
30
31 #include "CodeBlock.h"
32 #include "DFGCommon.h"
33 #include "Interpreter.h"
34 #include "JSCInlines.h"
35 #include "Options.h"
36
37 namespace JSC { namespace DFG {
38
39 bool isSupported()
40 {
41     return Options::useDFGJIT()
42         && MacroAssembler::supportsFloatingPoint();
43 }
44
45 bool isSupportedForInlining(CodeBlock* codeBlock)
46 {
47 #if ENABLE(WEBASSEMBLY)
48     if (codeBlock->ownerExecutable()->isWebAssemblyExecutable())
49         return false;
50 #endif
51     return codeBlock->ownerScriptExecutable()->isInliningCandidate();
52 }
53
54 bool mightCompileEval(CodeBlock* codeBlock)
55 {
56     return isSupported()
57         && codeBlock->instructionCount() <= Options::maximumOptimizationCandidateInstructionCount()
58         && codeBlock->ownerScriptExecutable()->isOkToOptimize();
59 }
60 bool mightCompileProgram(CodeBlock* codeBlock)
61 {
62     return isSupported()
63         && codeBlock->instructionCount() <= Options::maximumOptimizationCandidateInstructionCount()
64         && codeBlock->ownerScriptExecutable()->isOkToOptimize();
65 }
66 bool mightCompileFunctionForCall(CodeBlock* codeBlock)
67 {
68     return isSupported()
69         && codeBlock->instructionCount() <= Options::maximumOptimizationCandidateInstructionCount()
70         && codeBlock->ownerScriptExecutable()->isOkToOptimize();
71 }
72 bool mightCompileFunctionForConstruct(CodeBlock* codeBlock)
73 {
74     return isSupported()
75         && codeBlock->instructionCount() <= Options::maximumOptimizationCandidateInstructionCount()
76         && codeBlock->ownerScriptExecutable()->isOkToOptimize();
77 }
78
79 bool mightInlineFunctionForCall(CodeBlock* codeBlock)
80 {
81     return codeBlock->instructionCount() <= Options::maximumFunctionForCallInlineCandidateInstructionCount()
82         && isSupportedForInlining(codeBlock);
83 }
84 bool mightInlineFunctionForClosureCall(CodeBlock* codeBlock)
85 {
86     return codeBlock->instructionCount() <= Options::maximumFunctionForClosureCallInlineCandidateInstructionCount()
87         && isSupportedForInlining(codeBlock);
88 }
89 bool mightInlineFunctionForConstruct(CodeBlock* codeBlock)
90 {
91     return codeBlock->instructionCount() <= Options::maximumFunctionForConstructInlineCandidateInstructionCount()
92         && isSupportedForInlining(codeBlock);
93 }
94 bool canUseOSRExitFuzzing(CodeBlock* codeBlock)
95 {
96     return codeBlock->ownerScriptExecutable()->canUseOSRExitFuzzing();
97 }
98
99 inline void debugFail(CodeBlock* codeBlock, OpcodeID opcodeID, CapabilityLevel result)
100 {
101     if (Options::verboseCompilation() && !canCompile(result))
102         dataLog("Cannot compile code block ", *codeBlock, " because of opcode ", opcodeNames[opcodeID], "\n");
103 }
104
105 CapabilityLevel capabilityLevel(OpcodeID opcodeID, CodeBlock* codeBlock, Instruction* pc)
106 {
107     UNUSED_PARAM(codeBlock); // This function does some bytecode parsing. Ordinarily bytecode parsing requires the owning CodeBlock. It's sort of strange that we don't use it here right now.
108     UNUSED_PARAM(pc);
109     
110     switch (opcodeID) {
111     case op_enter:
112     case op_to_this:
113     case op_argument_count:
114     case op_check_tdz:
115     case op_create_this:
116     case op_bitand:
117     case op_bitor:
118     case op_bitxor:
119     case op_rshift:
120     case op_lshift:
121     case op_urshift:
122     case op_unsigned:
123     case op_inc:
124     case op_dec:
125     case op_add:
126     case op_sub:
127     case op_negate:
128     case op_mul:
129     case op_mod:
130     case op_pow:
131     case op_div:
132     case op_debug:
133     case op_profile_type:
134     case op_profile_control_flow:
135     case op_mov:
136     case op_overrides_has_instance:
137     case op_instanceof:
138     case op_instanceof_custom:
139     case op_is_empty:
140     case op_is_undefined:
141     case op_is_boolean:
142     case op_is_number:
143     case op_is_object:
144     case op_is_object_or_null:
145     case op_is_cell_with_type:
146     case op_is_function:
147     case op_not:
148     case op_less:
149     case op_lesseq:
150     case op_greater:
151     case op_greatereq:
152     case op_eq:
153     case op_eq_null:
154     case op_stricteq:
155     case op_neq:
156     case op_neq_null:
157     case op_nstricteq:
158     case op_get_by_val:
159     case op_put_by_val:
160     case op_put_by_val_direct:
161     case op_try_get_by_id:
162     case op_get_by_id:
163     case op_get_by_id_proto_load:
164     case op_get_by_id_unset:
165     case op_get_by_id_with_this:
166     case op_get_by_val_with_this:
167     case op_get_array_length:
168     case op_put_by_id:
169     case op_put_by_id_with_this:
170     case op_put_by_val_with_this:
171     case op_put_getter_by_id:
172     case op_put_setter_by_id:
173     case op_put_getter_setter_by_id:
174     case op_put_getter_by_val:
175     case op_put_setter_by_val:
176     case op_define_data_property:
177     case op_define_accessor_property:
178     case op_del_by_id:
179     case op_del_by_val:
180     case op_jmp:
181     case op_jtrue:
182     case op_jfalse:
183     case op_jeq_null:
184     case op_jneq_null:
185     case op_jless:
186     case op_jlesseq:
187     case op_jgreater:
188     case op_jgreatereq:
189     case op_jnless:
190     case op_jnlesseq:
191     case op_jngreater:
192     case op_jngreatereq:
193     case op_loop_hint:
194     case op_watchdog:
195     case op_ret:
196     case op_end:
197     case op_new_object:
198     case op_new_array:
199     case op_new_array_with_size:
200     case op_new_array_buffer:
201     case op_new_array_with_spread:
202     case op_spread:
203     case op_strcat:
204     case op_to_primitive:
205     case op_throw:
206     case op_throw_static_error:
207     case op_call:
208     case op_tail_call:
209     case op_construct:
210     case op_call_varargs:
211     case op_tail_call_varargs:
212     case op_tail_call_forward_arguments:
213     case op_construct_varargs:
214     case op_create_direct_arguments:
215     case op_create_scoped_arguments:
216     case op_create_cloned_arguments:
217     case op_get_from_arguments:
218     case op_put_to_arguments:
219     case op_get_argument:
220     case op_jneq_ptr:
221     case op_typeof:
222     case op_to_number:
223     case op_to_string:
224     case op_switch_imm:
225     case op_switch_char:
226     case op_in:
227     case op_get_scope:
228     case op_get_from_scope:
229     case op_get_enumerable_length:
230     case op_has_generic_property:
231     case op_has_structure_property:
232     case op_has_indexed_property:
233     case op_get_direct_pname:
234     case op_get_property_enumerator:
235     case op_enumerator_structure_pname:
236     case op_enumerator_generic_pname:
237     case op_to_index_string:
238     case op_new_func:
239     case op_new_func_exp:
240     case op_new_generator_func:
241     case op_new_generator_func_exp:
242     case op_new_async_func:
243     case op_new_async_func_exp:
244     case op_set_function_name:
245     case op_create_lexical_environment:
246     case op_get_parent_scope:
247     case op_catch:
248     case op_create_rest:
249     case op_get_rest_length:
250     case op_log_shadow_chicken_prologue:
251     case op_log_shadow_chicken_tail:
252     case op_put_to_scope:
253     case op_resolve_scope:
254     case op_new_regexp:
255         return CanCompileAndInline;
256
257     case op_switch_string: // Don't inline because we don't want to copy string tables in the concurrent JIT.
258     case op_call_eval:
259         return CanCompile;
260
261     default:
262         return CannotCompile;
263     }
264 }
265
266 CapabilityLevel capabilityLevel(CodeBlock* codeBlock)
267 {
268     Interpreter* interpreter = codeBlock->vm()->interpreter;
269     Instruction* instructionsBegin = codeBlock->instructions().begin();
270     unsigned instructionCount = codeBlock->instructions().size();
271     CapabilityLevel result = CanCompileAndInline;
272     
273     for (unsigned bytecodeOffset = 0; bytecodeOffset < instructionCount; ) {
274         switch (interpreter->getOpcodeID(instructionsBegin[bytecodeOffset].u.opcode)) {
275 #define DEFINE_OP(opcode, length) \
276         case opcode: { \
277             CapabilityLevel newResult = leastUpperBound(result, capabilityLevel(opcode, codeBlock, instructionsBegin + bytecodeOffset)); \
278             if (newResult != result) { \
279                 debugFail(codeBlock, opcode, newResult); \
280                 result = newResult; \
281             } \
282             bytecodeOffset += length; \
283             break; \
284         }
285             FOR_EACH_OPCODE_ID(DEFINE_OP)
286 #undef DEFINE_OP
287         default:
288             RELEASE_ASSERT_NOT_REACHED();
289             break;
290         }
291     }
292     
293     return result;
294 }
295
296 } } // namespace JSC::DFG
297
298 #endif