dd7cf90124978b0401931bcd8425528a9ebe7015
[WebKit-https.git] / Source / JavaScriptCore / dfg / DFGCapabilities.cpp
1 /*
2  * Copyright (C) 2011, 2013, 2014 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
28 #if ENABLE(DFG_JIT)
29
30 #include "DFGCapabilities.h"
31
32 #include "CodeBlock.h"
33 #include "DFGCommon.h"
34 #include "Interpreter.h"
35 #include "JSCInlines.h"
36
37 namespace JSC { namespace DFG {
38
39 #if ENABLE(DFG_JIT)
40 bool mightCompileEval(CodeBlock* codeBlock)
41 {
42     return codeBlock->instructionCount() <= Options::maximumOptimizationCandidateInstructionCount();
43 }
44 bool mightCompileProgram(CodeBlock* codeBlock)
45 {
46     return codeBlock->instructionCount() <= Options::maximumOptimizationCandidateInstructionCount();
47 }
48 bool mightCompileFunctionForCall(CodeBlock* codeBlock)
49 {
50     return codeBlock->instructionCount() <= Options::maximumOptimizationCandidateInstructionCount();
51 }
52 bool mightCompileFunctionForConstruct(CodeBlock* codeBlock)
53 {
54     return codeBlock->instructionCount() <= Options::maximumOptimizationCandidateInstructionCount();
55 }
56
57 bool mightInlineFunctionForCall(CodeBlock* codeBlock)
58 {
59     return codeBlock->instructionCount() <= Options::maximumFunctionForCallInlineCandidateInstructionCount()
60         && !codeBlock->ownerExecutable()->needsActivation()
61         && codeBlock->ownerExecutable()->isInliningCandidate();
62 }
63 bool mightInlineFunctionForClosureCall(CodeBlock* codeBlock)
64 {
65     return codeBlock->instructionCount() <= Options::maximumFunctionForClosureCallInlineCandidateInstructionCount()
66         && !codeBlock->ownerExecutable()->needsActivation()
67         && codeBlock->ownerExecutable()->isInliningCandidate();
68 }
69 bool mightInlineFunctionForConstruct(CodeBlock* codeBlock)
70 {
71     return codeBlock->instructionCount() <= Options::maximumFunctionForConstructInlineCandidateInstructionCount()
72         && !codeBlock->ownerExecutable()->needsActivation()
73         && codeBlock->ownerExecutable()->isInliningCandidate();
74 }
75
76 inline void debugFail(CodeBlock* codeBlock, OpcodeID opcodeID, CapabilityLevel result)
77 {
78     if (Options::verboseCompilation() && !canCompile(result))
79         dataLog("Cannot compile code block ", *codeBlock, " because of opcode ", opcodeNames[opcodeID], "\n");
80 }
81
82 CapabilityLevel capabilityLevel(OpcodeID opcodeID, CodeBlock* codeBlock, Instruction* pc)
83 {
84     switch (opcodeID) {
85     case op_enter:
86     case op_touch_entry:
87     case op_to_this:
88     case op_create_this:
89     case op_get_callee:
90     case op_bitand:
91     case op_bitor:
92     case op_bitxor:
93     case op_rshift:
94     case op_lshift:
95     case op_urshift:
96     case op_unsigned:
97     case op_inc:
98     case op_dec:
99     case op_add:
100     case op_sub:
101     case op_negate:
102     case op_mul:
103     case op_mod:
104     case op_div:
105     case op_debug:
106     case op_profile_will_call:
107     case op_profile_did_call:
108     case op_mov:
109     case op_captured_mov:
110     case op_check_has_instance:
111     case op_instanceof:
112     case op_is_undefined:
113     case op_is_boolean:
114     case op_is_number:
115     case op_is_string:
116     case op_is_object:
117     case op_is_function:
118     case op_not:
119     case op_less:
120     case op_lesseq:
121     case op_greater:
122     case op_greatereq:
123     case op_eq:
124     case op_eq_null:
125     case op_stricteq:
126     case op_neq:
127     case op_neq_null:
128     case op_nstricteq:
129     case op_get_by_val:
130     case op_put_by_val:
131     case op_put_by_val_direct:
132     case op_get_by_id:
133     case op_get_by_id_out_of_line:
134     case op_get_array_length:
135     case op_put_by_id:
136     case op_put_by_id_out_of_line:
137     case op_put_by_id_transition_direct:
138     case op_put_by_id_transition_direct_out_of_line:
139     case op_put_by_id_transition_normal:
140     case op_put_by_id_transition_normal_out_of_line:
141     case op_init_global_const_nop:
142     case op_init_global_const:
143     case op_jmp:
144     case op_jtrue:
145     case op_jfalse:
146     case op_jeq_null:
147     case op_jneq_null:
148     case op_jless:
149     case op_jlesseq:
150     case op_jgreater:
151     case op_jgreatereq:
152     case op_jnless:
153     case op_jnlesseq:
154     case op_jngreater:
155     case op_jngreatereq:
156     case op_loop_hint:
157     case op_ret:
158     case op_end:
159     case op_new_object:
160     case op_new_array:
161     case op_new_array_with_size:
162     case op_new_array_buffer:
163     case op_strcat:
164     case op_to_primitive:
165     case op_throw:
166     case op_throw_static_error:
167     case op_call:
168     case op_construct:
169     case op_init_lazy_reg:
170     case op_create_arguments:
171     case op_tear_off_arguments:
172     case op_get_argument_by_val:
173     case op_get_arguments_length:
174     case op_jneq_ptr:
175     case op_typeof:
176     case op_to_number:
177     case op_switch_imm:
178     case op_switch_char:
179     case op_in:
180     case op_get_from_scope:
181         return CanCompileAndInline;
182
183     case op_put_to_scope: {
184         ResolveType resolveType = ResolveModeAndType(pc[4].u.operand).type();
185         // If we're writing to a readonly property we emit a Dynamic put that
186         // the DFG can't currently handle.
187         if (resolveType == Dynamic)
188             return CannotCompile;
189         return CanCompileAndInline;
190     }
191
192     case op_resolve_scope: {
193         // We don't compile 'catch' or 'with', so there's no point in compiling variable resolution within them.
194         ResolveType resolveType = ResolveModeAndType(pc[3].u.operand).type();
195         if (resolveType == Dynamic)
196             return CannotCompile;
197         return CanCompileAndInline;
198     }
199
200     case op_call_varargs:
201         if (codeBlock->usesArguments() && pc[4].u.operand == codeBlock->argumentsRegister().offset())
202             return CanInline;
203         // FIXME: We should handle this.
204         // https://bugs.webkit.org/show_bug.cgi?id=127626
205         return CannotCompile;
206
207     case op_new_regexp: 
208     case op_create_activation:
209     case op_tear_off_activation:
210     case op_new_func:
211     case op_new_captured_func:
212     case op_new_func_exp:
213     case op_switch_string: // Don't inline because we don't want to copy string tables in the concurrent JIT.
214         return CanCompile;
215
216     default:
217         return CannotCompile;
218     }
219 }
220
221 CapabilityLevel capabilityLevel(CodeBlock* codeBlock)
222 {
223     Interpreter* interpreter = codeBlock->vm()->interpreter;
224     Instruction* instructionsBegin = codeBlock->instructions().begin();
225     unsigned instructionCount = codeBlock->instructions().size();
226     CapabilityLevel result = CanCompileAndInline;
227     
228     for (unsigned bytecodeOffset = 0; bytecodeOffset < instructionCount; ) {
229         switch (interpreter->getOpcodeID(instructionsBegin[bytecodeOffset].u.opcode)) {
230 #define DEFINE_OP(opcode, length) \
231         case opcode: { \
232             CapabilityLevel newResult = leastUpperBound(result, capabilityLevel(opcode, codeBlock, instructionsBegin + bytecodeOffset)); \
233             if (newResult != result) { \
234                 debugFail(codeBlock, opcode, newResult); \
235                 result = newResult; \
236             } \
237             bytecodeOffset += length; \
238             break; \
239         }
240             FOR_EACH_OPCODE_ID(DEFINE_OP)
241 #undef DEFINE_OP
242         default:
243             RELEASE_ASSERT_NOT_REACHED();
244             break;
245         }
246     }
247     
248     return result;
249 }
250
251 #endif
252
253 } } // namespace JSC::DFG
254
255 #endif