Unreviewed, reland r201532. The associated regressions have been fixed
[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
95 inline void debugFail(CodeBlock* codeBlock, OpcodeID opcodeID, CapabilityLevel result)
96 {
97     if (Options::verboseCompilation() && !canCompile(result))
98         dataLog("Cannot compile code block ", *codeBlock, " because of opcode ", opcodeNames[opcodeID], "\n");
99 }
100
101 CapabilityLevel capabilityLevel(OpcodeID opcodeID, CodeBlock* codeBlock, Instruction* pc)
102 {
103     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.
104     UNUSED_PARAM(pc);
105     
106     switch (opcodeID) {
107     case op_enter:
108     case op_to_this:
109     case op_check_tdz:
110     case op_create_this:
111     case op_bitand:
112     case op_bitor:
113     case op_bitxor:
114     case op_rshift:
115     case op_lshift:
116     case op_urshift:
117     case op_unsigned:
118     case op_inc:
119     case op_dec:
120     case op_add:
121     case op_sub:
122     case op_negate:
123     case op_mul:
124     case op_mod:
125     case op_div:
126     case op_debug:
127     case op_profile_type:
128     case op_profile_control_flow:
129     case op_mov:
130     case op_overrides_has_instance:
131     case op_instanceof:
132     case op_instanceof_custom:
133     case op_is_empty:
134     case op_is_undefined:
135     case op_is_boolean:
136     case op_is_number:
137     case op_is_string:
138     case op_is_object:
139     case op_is_object_or_null:
140     case op_is_function:
141     case op_not:
142     case op_less:
143     case op_lesseq:
144     case op_greater:
145     case op_greatereq:
146     case op_eq:
147     case op_eq_null:
148     case op_stricteq:
149     case op_neq:
150     case op_neq_null:
151     case op_nstricteq:
152     case op_get_by_val:
153     case op_put_by_val:
154     case op_put_by_val_direct:
155     case op_try_get_by_id:
156     case op_get_by_id:
157     case op_get_by_id_proto_load:
158     case op_get_by_id_unset:
159     case op_get_by_id_with_this:
160     case op_get_by_val_with_this:
161     case op_get_array_length:
162     case op_put_by_id:
163     case op_put_by_id_with_this:
164     case op_put_by_val_with_this:
165     case op_put_getter_by_id:
166     case op_put_setter_by_id:
167     case op_put_getter_setter_by_id:
168     case op_put_getter_by_val:
169     case op_put_setter_by_val:
170     case op_del_by_id:
171     case op_del_by_val:
172     case op_jmp:
173     case op_jtrue:
174     case op_jfalse:
175     case op_jeq_null:
176     case op_jneq_null:
177     case op_jless:
178     case op_jlesseq:
179     case op_jgreater:
180     case op_jgreatereq:
181     case op_jnless:
182     case op_jnlesseq:
183     case op_jngreater:
184     case op_jngreatereq:
185     case op_loop_hint:
186     case op_watchdog:
187     case op_ret:
188     case op_end:
189     case op_new_object:
190     case op_new_array:
191     case op_new_array_with_size:
192     case op_new_array_buffer:
193     case op_strcat:
194     case op_to_primitive:
195     case op_throw:
196     case op_throw_static_error:
197     case op_call:
198     case op_tail_call:
199     case op_construct:
200     case op_call_varargs:
201     case op_tail_call_varargs:
202     case op_construct_varargs:
203     case op_create_direct_arguments:
204     case op_create_scoped_arguments:
205     case op_create_cloned_arguments:
206     case op_get_from_arguments:
207     case op_put_to_arguments:
208     case op_jneq_ptr:
209     case op_typeof:
210     case op_to_number:
211     case op_to_string:
212     case op_switch_imm:
213     case op_switch_char:
214     case op_in:
215     case op_get_scope:
216     case op_get_from_scope:
217     case op_get_enumerable_length:
218     case op_has_generic_property:
219     case op_has_structure_property:
220     case op_has_indexed_property:
221     case op_get_direct_pname:
222     case op_get_property_enumerator:
223     case op_enumerator_structure_pname:
224     case op_enumerator_generic_pname:
225     case op_to_index_string:
226     case op_new_func:
227     case op_new_func_exp:
228     case op_new_generator_func:
229     case op_new_generator_func_exp:
230     case op_set_function_name:
231     case op_create_lexical_environment:
232     case op_get_parent_scope:
233     case op_catch:
234     case op_copy_rest:
235     case op_get_rest_length:
236     case op_log_shadow_chicken_prologue:
237     case op_log_shadow_chicken_tail:
238     case op_put_to_scope:
239     case op_resolve_scope:
240         return CanCompileAndInline;
241
242     case op_new_regexp:
243     case op_switch_string: // Don't inline because we don't want to copy string tables in the concurrent JIT.
244         return CanCompile;
245
246     default:
247         return CannotCompile;
248     }
249 }
250
251 CapabilityLevel capabilityLevel(CodeBlock* codeBlock)
252 {
253     Interpreter* interpreter = codeBlock->vm()->interpreter;
254     Instruction* instructionsBegin = codeBlock->instructions().begin();
255     unsigned instructionCount = codeBlock->instructions().size();
256     CapabilityLevel result = CanCompileAndInline;
257     
258     for (unsigned bytecodeOffset = 0; bytecodeOffset < instructionCount; ) {
259         switch (interpreter->getOpcodeID(instructionsBegin[bytecodeOffset].u.opcode)) {
260 #define DEFINE_OP(opcode, length) \
261         case opcode: { \
262             CapabilityLevel newResult = leastUpperBound(result, capabilityLevel(opcode, codeBlock, instructionsBegin + bytecodeOffset)); \
263             if (newResult != result) { \
264                 debugFail(codeBlock, opcode, newResult); \
265                 result = newResult; \
266             } \
267             bytecodeOffset += length; \
268             break; \
269         }
270             FOR_EACH_OPCODE_ID(DEFINE_OP)
271 #undef DEFINE_OP
272         default:
273             RELEASE_ASSERT_NOT_REACHED();
274             break;
275         }
276     }
277     
278     return result;
279 }
280
281 } } // namespace JSC::DFG
282
283 #endif