Line data Source code
1 : // Copyright 2016 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "src/optimized-compilation-info.h"
6 :
7 : #include "src/api.h"
8 : #include "src/debug/debug.h"
9 : #include "src/isolate.h"
10 : #include "src/objects-inl.h"
11 : #include "src/objects/shared-function-info.h"
12 : #include "src/source-position.h"
13 : #include "src/wasm/function-compiler.h"
14 :
15 : namespace v8 {
16 : namespace internal {
17 :
18 482571 : OptimizedCompilationInfo::OptimizedCompilationInfo(
19 : Zone* zone, Isolate* isolate, Handle<SharedFunctionInfo> shared,
20 : Handle<JSFunction> closure)
21 482571 : : OptimizedCompilationInfo(Code::OPTIMIZED_FUNCTION, zone) {
22 : DCHECK(shared->is_compiled());
23 965150 : bytecode_array_ = handle(shared->GetBytecodeArray(), isolate);
24 482575 : shared_info_ = shared;
25 482575 : closure_ = closure;
26 482575 : optimization_id_ = isolate->NextOptimizationId();
27 :
28 : // Collect source positions for optimized code when profiling or if debugger
29 : // is active, to be able to get more precise source positions at the price of
30 : // more memory consumption.
31 482575 : if (isolate->NeedsDetailedOptimizedCodeLineInfo()) {
32 : MarkAsSourcePositionsEnabled();
33 : }
34 :
35 1447734 : SetTracingFlags(shared->PassesFilter(FLAG_trace_turbo_filter));
36 482578 : }
37 :
38 2003365 : OptimizedCompilationInfo::OptimizedCompilationInfo(
39 : Vector<const char> debug_name, Zone* zone, Code::Kind code_kind)
40 2003365 : : OptimizedCompilationInfo(code_kind, zone) {
41 2003282 : debug_name_ = debug_name;
42 :
43 : SetTracingFlags(
44 4006564 : PassesFilter(debug_name, CStrVector(FLAG_trace_turbo_filter)));
45 2003346 : }
46 :
47 2485950 : OptimizedCompilationInfo::OptimizedCompilationInfo(Code::Kind code_kind,
48 : Zone* zone)
49 9943800 : : code_kind_(code_kind), zone_(zone) {
50 2485950 : ConfigureFlags();
51 2485859 : }
52 :
53 2485829 : void OptimizedCompilationInfo::ConfigureFlags() {
54 2485829 : if (FLAG_untrusted_code_mitigations) SetFlag(kUntrustedCodeMitigations);
55 :
56 2485829 : switch (code_kind_) {
57 : case Code::OPTIMIZED_FUNCTION:
58 : SetFlag(kCalledWithCodeStartRegister);
59 : SetFlag(kSwitchJumpTableEnabled);
60 482573 : if (FLAG_function_context_specialization) {
61 : MarkAsFunctionContextSpecializing();
62 : }
63 482573 : if (FLAG_turbo_splitting) {
64 : MarkAsSplittingEnabled();
65 : }
66 482573 : if (FLAG_untrusted_code_mitigations) {
67 : MarkAsPoisoningRegisterArguments();
68 : }
69 482573 : if (FLAG_analyze_environment_liveness) {
70 : // TODO(yangguo): Disable this in case of debugging for crbug.com/826613
71 : MarkAsAnalyzeEnvironmentLiveness();
72 : }
73 : break;
74 : case Code::BYTECODE_HANDLER:
75 : SetFlag(kCalledWithCodeStartRegister);
76 : break;
77 : case Code::BUILTIN:
78 : case Code::STUB:
79 : #if ENABLE_GDB_JIT_INTERFACE && DEBUG
80 : MarkAsSourcePositionsEnabled();
81 : #endif // ENABLE_GDB_JIT_INTERFACE && DEBUG
82 : break;
83 : case Code::WASM_FUNCTION:
84 : SetFlag(kSwitchJumpTableEnabled);
85 : break;
86 : default:
87 : break;
88 : }
89 2485829 : }
90 :
91 4970269 : OptimizedCompilationInfo::~OptimizedCompilationInfo() {
92 2485127 : if (GetFlag(kDisableFutureOptimization) && has_shared_info()) {
93 18687 : shared_info()->DisableOptimization(bailout_reason());
94 : }
95 2485111 : }
96 :
97 0 : void OptimizedCompilationInfo::set_deferred_handles(
98 : std::shared_ptr<DeferredHandles> deferred_handles) {
99 : DCHECK_NULL(deferred_handles_);
100 : deferred_handles_.swap(deferred_handles);
101 0 : }
102 :
103 7157 : void OptimizedCompilationInfo::set_deferred_handles(
104 : DeferredHandles* deferred_handles) {
105 : DCHECK_NULL(deferred_handles_);
106 7157 : deferred_handles_.reset(deferred_handles);
107 7157 : }
108 :
109 462449 : void OptimizedCompilationInfo::ReopenHandlesInNewHandleScope(Isolate* isolate) {
110 462449 : if (!shared_info_.is_null()) {
111 462454 : shared_info_ = Handle<SharedFunctionInfo>(*shared_info_, isolate);
112 : }
113 462454 : if (!bytecode_array_.is_null()) {
114 462456 : bytecode_array_ = Handle<BytecodeArray>(*bytecode_array_, isolate);
115 : }
116 462455 : if (!closure_.is_null()) {
117 462456 : closure_ = Handle<JSFunction>(*closure_, isolate);
118 : }
119 462455 : }
120 :
121 2544151 : std::unique_ptr<char[]> OptimizedCompilationInfo::GetDebugName() const {
122 2544151 : if (!shared_info().is_null()) {
123 482547 : return shared_info()->DebugName()->ToCString();
124 : }
125 2061604 : Vector<const char> name_vec = debug_name_;
126 2061604 : if (name_vec.is_empty()) name_vec = ArrayVector("unknown");
127 2061604 : std::unique_ptr<char[]> name(new char[name_vec.length() + 1]);
128 2062238 : memcpy(name.get(), name_vec.start(), name_vec.length());
129 2062238 : name[name_vec.length()] = '\0';
130 : return name;
131 : }
132 :
133 989386 : StackFrame::Type OptimizedCompilationInfo::GetOutputStackFrameType() const {
134 989386 : switch (code_kind()) {
135 : case Code::STUB:
136 : case Code::BYTECODE_HANDLER:
137 : case Code::BUILTIN:
138 : return StackFrame::STUB;
139 : case Code::WASM_FUNCTION:
140 496133 : return StackFrame::WASM_COMPILED;
141 : case Code::JS_TO_WASM_FUNCTION:
142 0 : return StackFrame::JS_TO_WASM;
143 : case Code::WASM_TO_JS_FUNCTION:
144 6490 : return StackFrame::WASM_TO_JS;
145 : case Code::WASM_INTERPRETER_ENTRY:
146 368034 : return StackFrame::WASM_INTERPRETER_ENTRY;
147 : default:
148 0 : UNIMPLEMENTED();
149 : return StackFrame::NONE;
150 : }
151 : }
152 :
153 495001 : void OptimizedCompilationInfo::SetWasmCompilationResult(
154 : std::unique_ptr<wasm::WasmCompilationResult> wasm_compilation_result) {
155 495001 : wasm_compilation_result_ = std::move(wasm_compilation_result);
156 494830 : }
157 :
158 : std::unique_ptr<wasm::WasmCompilationResult>
159 496182 : OptimizedCompilationInfo::ReleaseWasmCompilationResult() {
160 496182 : return std::move(wasm_compilation_result_);
161 : }
162 :
163 0 : bool OptimizedCompilationInfo::has_context() const {
164 0 : return !closure().is_null();
165 : }
166 :
167 435885 : Context OptimizedCompilationInfo::context() const {
168 : DCHECK(has_context());
169 435885 : return closure()->context();
170 : }
171 :
172 0 : bool OptimizedCompilationInfo::has_native_context() const {
173 0 : return !closure().is_null() && !closure()->native_context().is_null();
174 : }
175 :
176 1573796 : Context OptimizedCompilationInfo::native_context() const {
177 : DCHECK(has_native_context());
178 1573796 : return closure()->native_context();
179 : }
180 :
181 0 : bool OptimizedCompilationInfo::has_global_object() const {
182 0 : return has_native_context();
183 : }
184 :
185 0 : JSGlobalObject OptimizedCompilationInfo::global_object() const {
186 : DCHECK(has_global_object());
187 0 : return native_context()->global_object();
188 : }
189 :
190 65108 : int OptimizedCompilationInfo::AddInlinedFunction(
191 : Handle<SharedFunctionInfo> inlined_function,
192 : Handle<BytecodeArray> inlined_bytecode, SourcePosition pos) {
193 65108 : int id = static_cast<int>(inlined_functions_.size());
194 130216 : inlined_functions_.push_back(
195 : InlinedFunctionHolder(inlined_function, inlined_bytecode, pos));
196 65108 : return id;
197 : }
198 :
199 0 : void OptimizedCompilationInfo::SetTracingFlags(bool passes_filter) {
200 2485924 : if (!passes_filter) return;
201 2485918 : if (FLAG_trace_turbo) SetFlag(kTraceTurboJson);
202 2485918 : if (FLAG_trace_turbo_graph) SetFlag(kTraceTurboGraph);
203 2485918 : if (FLAG_trace_turbo_scheduled) SetFlag(kTraceTurboScheduled);
204 : }
205 :
206 0 : OptimizedCompilationInfo::InlinedFunctionHolder::InlinedFunctionHolder(
207 : Handle<SharedFunctionInfo> inlined_shared_info,
208 : Handle<BytecodeArray> inlined_bytecode, SourcePosition pos)
209 65108 : : shared_info(inlined_shared_info), bytecode_array(inlined_bytecode) {
210 : DCHECK_EQ(shared_info->GetBytecodeArray(), *bytecode_array);
211 65108 : position.position = pos;
212 : // initialized when generating the deoptimization literals
213 65108 : position.inlined_function_id = DeoptimizationData::kNotInlinedIndex;
214 0 : }
215 :
216 : } // namespace internal
217 120216 : } // namespace v8
|