// Copyright 2014 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "src/compiler/pipeline.h" #include #include #include #include #include "src/base/optional.h" #include "src/base/platform/elapsed-timer.h" #include "src/builtins/profile-data-reader.h" #include "src/codegen/assembler-inl.h" #include "src/codegen/compiler.h" #include "src/codegen/optimized-compilation-info.h" #include "src/codegen/register-configuration.h" #include "src/common/high-allocation-throughput-scope.h" #include "src/compiler/add-type-assertions-reducer.h" #include "src/compiler/backend/code-generator.h" #include "src/compiler/backend/frame-elider.h" #include "src/compiler/backend/instruction-selector.h" #include "src/compiler/backend/instruction.h" #include "src/compiler/backend/jump-threading.h" #include "src/compiler/backend/mid-tier-register-allocator.h" #include "src/compiler/backend/move-optimizer.h" #include "src/compiler/backend/register-allocator-verifier.h" #include "src/compiler/backend/register-allocator.h" #include "src/compiler/basic-block-instrumentor.h" #include "src/compiler/branch-condition-duplicator.h" #include "src/compiler/branch-elimination.h" #include "src/compiler/bytecode-graph-builder.h" #include "src/compiler/checkpoint-elimination.h" #include "src/compiler/common-operator-reducer.h" #include "src/compiler/common-operator.h" #include "src/compiler/compilation-dependencies.h" #include "src/compiler/compiler-source-position-table.h" #include "src/compiler/constant-folding-reducer.h" #include "src/compiler/control-flow-optimizer.h" #include "src/compiler/csa-load-elimination.h" #include "src/compiler/dead-code-elimination.h" #include "src/compiler/decompression-optimizer.h" #include "src/compiler/effect-control-linearizer.h" #include "src/compiler/escape-analysis-reducer.h" #include "src/compiler/escape-analysis.h" #include "src/compiler/graph-trimmer.h" #include "src/compiler/graph-visualizer.h" #include "src/compiler/js-call-reducer.h" #include "src/compiler/js-context-specialization.h" #include "src/compiler/js-create-lowering.h" #include "src/compiler/js-generic-lowering.h" #include "src/compiler/js-heap-broker.h" #include "src/compiler/js-inlining-heuristic.h" #include "src/compiler/js-intrinsic-lowering.h" #include "src/compiler/js-native-context-specialization.h" #include "src/compiler/js-typed-lowering.h" #include "src/compiler/load-elimination.h" #include "src/compiler/loop-analysis.h" #include "src/compiler/loop-peeling.h" #include "src/compiler/loop-unrolling.h" #include "src/compiler/loop-variable-optimizer.h" #include "src/compiler/machine-graph-verifier.h" #include "src/compiler/machine-operator-reducer.h" #include "src/compiler/memory-optimizer.h" #include "src/compiler/node-observer.h" #include "src/compiler/node-origin-table.h" #include "src/compiler/osr.h" #include "src/compiler/pipeline-statistics.h" #include "src/compiler/redundancy-elimination.h" #include "src/compiler/schedule.h" #include "src/compiler/scheduler.h" #include "src/compiler/select-lowering.h" #include "src/compiler/simplified-lowering.h" #include "src/compiler/simplified-operator-reducer.h" #include "src/compiler/simplified-operator.h" #include "src/compiler/store-store-elimination.h" #include "src/compiler/type-narrowing-reducer.h" #include "src/compiler/typed-optimization.h" #include "src/compiler/typer.h" #include "src/compiler/value-numbering-reducer.h" #include "src/compiler/verifier.h" #include "src/compiler/zone-stats.h" #include "src/diagnostics/code-tracer.h" #include "src/diagnostics/disassembler.h" #include "src/execution/isolate-inl.h" #include "src/heap/local-heap.h" #include "src/init/bootstrapper.h" #include "src/logging/code-events.h" #include "src/logging/counters.h" #include "src/logging/runtime-call-stats-scope.h" #include "src/objects/shared-function-info.h" #include "src/parsing/parse-info.h" #include "src/tracing/trace-event.h" #include "src/tracing/traced-value.h" #include "src/utils/ostreams.h" #include "src/utils/utils.h" #if V8_ENABLE_WEBASSEMBLY #include "src/compiler/wasm-compiler.h" #include "src/compiler/wasm-escape-analysis.h" #include "src/compiler/wasm-inlining.h" #include "src/compiler/wasm-loop-peeling.h" #include "src/wasm/function-body-decoder.h" #include "src/wasm/function-compiler.h" #include "src/wasm/wasm-engine.h" #endif // V8_ENABLE_WEBASSEMBLY namespace v8 { namespace internal { namespace compiler { static constexpr char kCodegenZoneName[] = "codegen-zone"; static constexpr char kGraphZoneName[] = "graph-zone"; static constexpr char kInstructionZoneName[] = "instruction-zone"; static constexpr char kMachineGraphVerifierZoneName[] = "machine-graph-verifier-zone"; static constexpr char kPipelineCompilationJobZoneName[] = "pipeline-compilation-job-zone"; static constexpr char kRegisterAllocationZoneName[] = "register-allocation-zone"; static constexpr char kRegisterAllocatorVerifierZoneName[] = "register-allocator-verifier-zone"; namespace { Maybe GetModuleContext(Handle closure) { Context current = closure->context(); size_t distance = 0; while (!current.IsNativeContext()) { if (current.IsModuleContext()) { return Just( OuterContext(handle(current, current.GetIsolate()), distance)); } current = current.previous(); distance++; } return Nothing(); } } // anonymous namespace class PipelineData { public: // For main entry point. PipelineData(ZoneStats* zone_stats, Isolate* isolate, OptimizedCompilationInfo* info, PipelineStatistics* pipeline_statistics) : isolate_(isolate), allocator_(isolate->allocator()), info_(info), debug_name_(info_->GetDebugName()), may_have_unverifiable_graph_(false), zone_stats_(zone_stats), pipeline_statistics_(pipeline_statistics), graph_zone_scope_(zone_stats_, kGraphZoneName, kCompressGraphZone), graph_zone_(graph_zone_scope_.zone()), instruction_zone_scope_(zone_stats_, kInstructionZoneName), instruction_zone_(instruction_zone_scope_.zone()), codegen_zone_scope_(zone_stats_, kCodegenZoneName), codegen_zone_(codegen_zone_scope_.zone()), broker_(new JSHeapBroker(isolate_, info_->zone(), info_->trace_heap_broker(), info->code_kind())), register_allocation_zone_scope_(zone_stats_, kRegisterAllocationZoneName), register_allocation_zone_(register_allocation_zone_scope_.zone()), assembler_options_(AssemblerOptions::Default(isolate)) { PhaseScope scope(pipeline_statistics, "V8.TFInitPipelineData"); graph_ = graph_zone_->New(graph_zone_); source_positions_ = graph_zone_->New(graph_); node_origins_ = info->trace_turbo_json() ? graph_zone_->New(graph_) : nullptr; simplified_ = graph_zone_->New(graph_zone_); machine_ = graph_zone_->New( graph_zone_, MachineType::PointerRepresentation(), InstructionSelector::SupportedMachineOperatorFlags(), InstructionSelector::AlignmentRequirements()); common_ = graph_zone_->New(graph_zone_); javascript_ = graph_zone_->New(graph_zone_); jsgraph_ = graph_zone_->New(isolate_, graph_, common_, javascript_, simplified_, machine_); observe_node_manager_ = info->node_observer() ? graph_zone_->New(graph_zone_) : nullptr; dependencies_ = info_->zone()->New(broker_, info_->zone()); } #if V8_ENABLE_WEBASSEMBLY // For WebAssembly compile entry point. PipelineData(ZoneStats* zone_stats, wasm::WasmEngine* wasm_engine, OptimizedCompilationInfo* info, MachineGraph* mcgraph, PipelineStatistics* pipeline_statistics, SourcePositionTable* source_positions, NodeOriginTable* node_origins, const AssemblerOptions& assembler_options) : isolate_(nullptr), wasm_engine_(wasm_engine), allocator_(wasm_engine->allocator()), info_(info), debug_name_(info_->GetDebugName()), may_have_unverifiable_graph_(false), zone_stats_(zone_stats), pipeline_statistics_(pipeline_statistics), graph_zone_scope_(zone_stats_, kGraphZoneName, kCompressGraphZone), graph_zone_(graph_zone_scope_.zone()), graph_(mcgraph->graph()), source_positions_(source_positions), node_origins_(node_origins), machine_(mcgraph->machine()), common_(mcgraph->common()), mcgraph_(mcgraph), instruction_zone_scope_(zone_stats_, kInstructionZoneName), instruction_zone_(instruction_zone_scope_.zone()), codegen_zone_scope_(zone_stats_, kCodegenZoneName), codegen_zone_(codegen_zone_scope_.zone()), register_allocation_zone_scope_(zone_stats_, kRegisterAllocationZoneName), register_allocation_zone_(register_allocation_zone_scope_.zone()), assembler_options_(assembler_options) { simplified_ = graph_zone_->New(graph_zone_); javascript_ = graph_zone_->New(graph_zone_); jsgraph_ = graph_zone_->New(isolate_, graph_, common_, javascript_, simplified_, machine_); } #endif // V8_ENABLE_WEBASSEMBLY // For CodeStubAssembler and machine graph testing entry point. PipelineData(ZoneStats* zone_stats, OptimizedCompilationInfo* info, Isolate* isolate, AccountingAllocator* allocator, Graph* graph, JSGraph* jsgraph, Schedule* schedule, SourcePositionTable* source_positions, NodeOriginTable* node_origins, JumpOptimizationInfo* jump_opt, const AssemblerOptions& assembler_options, const ProfileDataFromFile* profile_data) : isolate_(isolate), #if V8_ENABLE_WEBASSEMBLY // TODO(clemensb): Remove this field, use GetWasmEngine directly // instead. wasm_engine_(wasm::GetWasmEngine()), #endif // V8_ENABLE_WEBASSEMBLY allocator_(allocator), info_(info), debug_name_(info_->GetDebugName()), zone_stats_(zone_stats), graph_zone_scope_(zone_stats_, kGraphZoneName, kCompressGraphZone), graph_zone_(graph_zone_scope_.zone()), graph_(graph), source_positions_(source_positions), node_origins_(node_origins), schedule_(schedule), instruction_zone_scope_(zone_stats_, kInstructionZoneName), instruction_zone_(instruction_zone_scope_.zone()), codegen_zone_scope_(zone_stats_, kCodegenZoneName), codegen_zone_(codegen_zone_scope_.zone()), register_allocation_zone_scope_(zone_stats_, kRegisterAllocationZoneName), register_allocation_zone_(register_allocation_zone_scope_.zone()), jump_optimization_info_(jump_opt), assembler_options_(assembler_options), profile_data_(profile_data) { if (jsgraph) { jsgraph_ = jsgraph; simplified_ = jsgraph->simplified(); machine_ = jsgraph->machine(); common_ = jsgraph->common(); javascript_ = jsgraph->javascript(); } else { simplified_ = graph_zone_->New(graph_zone_); machine_ = graph_zone_->New( graph_zone_, MachineType::PointerRepresentation(), InstructionSelector::SupportedMachineOperatorFlags(), InstructionSelector::AlignmentRequirements()); common_ = graph_zone_->New(graph_zone_); javascript_ = graph_zone_->New(graph_zone_); jsgraph_ = graph_zone_->New(isolate_, graph_, common_, javascript_, simplified_, machine_); } } // For register allocation testing entry point. PipelineData(ZoneStats* zone_stats, OptimizedCompilationInfo* info, Isolate* isolate, InstructionSequence* sequence) : isolate_(isolate), allocator_(isolate->allocator()), info_(info), debug_name_(info_->GetDebugName()), zone_stats_(zone_stats), graph_zone_scope_(zone_stats_, kGraphZoneName, kCompressGraphZone), instruction_zone_scope_(zone_stats_, kInstructionZoneName), instruction_zone_(sequence->zone()), sequence_(sequence), codegen_zone_scope_(zone_stats_, kCodegenZoneName), codegen_zone_(codegen_zone_scope_.zone()), register_allocation_zone_scope_(zone_stats_, kRegisterAllocationZoneName), register_allocation_zone_(register_allocation_zone_scope_.zone()), assembler_options_(AssemblerOptions::Default(isolate)) {} ~PipelineData() { // Must happen before zones are destroyed. delete code_generator_; code_generator_ = nullptr; DeleteTyper(); DeleteRegisterAllocationZone(); DeleteInstructionZone(); DeleteCodegenZone(); DeleteGraphZone(); } PipelineData(const PipelineData&) = delete; PipelineData& operator=(const PipelineData&) = delete; Isolate* isolate() const { return isolate_; } AccountingAllocator* allocator() const { return allocator_; } OptimizedCompilationInfo* info() const { return info_; } ZoneStats* zone_stats() const { return zone_stats_; } CompilationDependencies* dependencies() const { return dependencies_; } PipelineStatistics* pipeline_statistics() { return pipeline_statistics_; } OsrHelper* osr_helper() { return &(*osr_helper_); } bool compilation_failed() const { return compilation_failed_; } void set_compilation_failed() { compilation_failed_ = true; } bool verify_graph() const { return verify_graph_; } void set_verify_graph(bool value) { verify_graph_ = value; } MaybeHandle code() { return code_; } void set_code(MaybeHandle code) { DCHECK(code_.is_null()); code_ = code; } CodeGenerator* code_generator() const { return code_generator_; } // RawMachineAssembler generally produces graphs which cannot be verified. bool MayHaveUnverifiableGraph() const { return may_have_unverifiable_graph_; } Zone* graph_zone() const { return graph_zone_; } Graph* graph() const { return graph_; } SourcePositionTable* source_positions() const { return source_positions_; } NodeOriginTable* node_origins() const { return node_origins_; } MachineOperatorBuilder* machine() const { return machine_; } CommonOperatorBuilder* common() const { return common_; } JSOperatorBuilder* javascript() const { return javascript_; } JSGraph* jsgraph() const { return jsgraph_; } MachineGraph* mcgraph() const { return mcgraph_; } Handle native_context() const { return handle(info()->native_context(), isolate()); } Handle global_object() const { return handle(info()->global_object(), isolate()); } JSHeapBroker* broker() const { return broker_; } std::unique_ptr ReleaseBroker() { std::unique_ptr broker(broker_); broker_ = nullptr; return broker; } Schedule* schedule() const { return schedule_; } void set_schedule(Schedule* schedule) { DCHECK(!schedule_); schedule_ = schedule; } void reset_schedule() { schedule_ = nullptr; } ObserveNodeManager* observe_node_manager() const { return observe_node_manager_; } Zone* instruction_zone() const { return instruction_zone_; } Zone* codegen_zone() const { return codegen_zone_; } InstructionSequence* sequence() const { return sequence_; } Frame* frame() const { return frame_; } Zone* register_allocation_zone() const { return register_allocation_zone_; } RegisterAllocationData* register_allocation_data() const { return register_allocation_data_; } TopTierRegisterAllocationData* top_tier_register_allocation_data() const { return TopTierRegisterAllocationData::cast(register_allocation_data_); } MidTierRegisterAllocationData* mid_tier_register_allocator_data() const { return MidTierRegisterAllocationData::cast(register_allocation_data_); } std::string const& source_position_output() const { return source_position_output_; } void set_source_position_output(std::string const& source_position_output) { source_position_output_ = source_position_output; } JumpOptimizationInfo* jump_optimization_info() const { return jump_optimization_info_; } const AssemblerOptions& assembler_options() const { return assembler_options_; } void ChooseSpecializationContext() { if (info()->function_context_specializing()) { DCHECK(info()->has_context()); specialization_context_ = Just(OuterContext(handle(info()->context(), isolate()), 0)); } else { specialization_context_ = GetModuleContext(info()->closure()); } } Maybe specialization_context() const { return specialization_context_; } size_t* address_of_max_unoptimized_frame_height() { return &max_unoptimized_frame_height_; } size_t max_unoptimized_frame_height() const { return max_unoptimized_frame_height_; } size_t* address_of_max_pushed_argument_count() { return &max_pushed_argument_count_; } size_t max_pushed_argument_count() const { return max_pushed_argument_count_; } CodeTracer* GetCodeTracer() const { #if V8_ENABLE_WEBASSEMBLY if (wasm_engine_) return wasm_engine_->GetCodeTracer(); #endif // V8_ENABLE_WEBASSEMBLY return isolate_->GetCodeTracer(); } Typer* CreateTyper() { DCHECK_NULL(typer_); typer_ = new Typer(broker(), typer_flags_, graph(), &info()->tick_counter()); return typer_; } void AddTyperFlag(Typer::Flag flag) { DCHECK_NULL(typer_); typer_flags_ |= flag; } void DeleteTyper() { delete typer_; typer_ = nullptr; } void DeleteGraphZone() { if (graph_zone_ == nullptr) return; graph_zone_scope_.Destroy(); graph_zone_ = nullptr; graph_ = nullptr; source_positions_ = nullptr; node_origins_ = nullptr; simplified_ = nullptr; machine_ = nullptr; common_ = nullptr; javascript_ = nullptr; jsgraph_ = nullptr; mcgraph_ = nullptr; schedule_ = nullptr; } void DeleteInstructionZone() { if (instruction_zone_ == nullptr) return; instruction_zone_scope_.Destroy(); instruction_zone_ = nullptr; sequence_ = nullptr; } void DeleteCodegenZone() { if (codegen_zone_ == nullptr) return; codegen_zone_scope_.Destroy(); codegen_zone_ = nullptr; dependencies_ = nullptr; delete broker_; broker_ = nullptr; frame_ = nullptr; } void DeleteRegisterAllocationZone() { if (register_allocation_zone_ == nullptr) return; register_allocation_zone_scope_.Destroy(); register_allocation_zone_ = nullptr; register_allocation_data_ = nullptr; } void InitializeInstructionSequence(const CallDescriptor* call_descriptor) { DCHECK_NULL(sequence_); InstructionBlocks* instruction_blocks = InstructionSequence::InstructionBlocksFor(instruction_zone(), schedule()); sequence_ = instruction_zone()->New( isolate(), instruction_zone(), instruction_blocks); if (call_descriptor && call_descriptor->RequiresFrameAsIncoming()) { sequence_->instruction_blocks()[0]->mark_needs_frame(); } else { DCHECK(call_descriptor->CalleeSavedFPRegisters().is_empty()); } } void InitializeFrameData(CallDescriptor* call_descriptor) { DCHECK_NULL(frame_); int fixed_frame_size = 0; if (call_descriptor != nullptr) { fixed_frame_size = call_descriptor->CalculateFixedFrameSize(info()->code_kind()); } frame_ = codegen_zone()->New(fixed_frame_size); if (osr_helper_.has_value()) osr_helper()->SetupFrame(frame()); } void InitializeTopTierRegisterAllocationData( const RegisterConfiguration* config, CallDescriptor* call_descriptor, RegisterAllocationFlags flags) { DCHECK_NULL(register_allocation_data_); register_allocation_data_ = register_allocation_zone()->New( config, register_allocation_zone(), frame(), sequence(), flags, &info()->tick_counter(), debug_name()); } void InitializeMidTierRegisterAllocationData( const RegisterConfiguration* config, CallDescriptor* call_descriptor) { DCHECK_NULL(register_allocation_data_); register_allocation_data_ = register_allocation_zone()->New( config, register_allocation_zone(), frame(), sequence(), &info()->tick_counter(), debug_name()); } void InitializeOsrHelper() { DCHECK(!osr_helper_.has_value()); osr_helper_.emplace(info()); } void set_start_source_position(int position) { DCHECK_EQ(start_source_position_, kNoSourcePosition); start_source_position_ = position; } void InitializeCodeGenerator(Linkage* linkage) { DCHECK_NULL(code_generator_); code_generator_ = new CodeGenerator( codegen_zone(), frame(), linkage, sequence(), info(), isolate(), osr_helper_, start_source_position_, jump_optimization_info_, assembler_options(), info_->builtin(), max_unoptimized_frame_height(), max_pushed_argument_count(), FLAG_trace_turbo_stack_accesses ? debug_name_.get() : nullptr); } void BeginPhaseKind(const char* phase_kind_name) { if (pipeline_statistics() != nullptr) { pipeline_statistics()->BeginPhaseKind(phase_kind_name); } } void EndPhaseKind() { if (pipeline_statistics() != nullptr) { pipeline_statistics()->EndPhaseKind(); } } const char* debug_name() const { return debug_name_.get(); } const ProfileDataFromFile* profile_data() const { return profile_data_; } void set_profile_data(const ProfileDataFromFile* profile_data) { profile_data_ = profile_data; } // RuntimeCallStats that is only available during job execution but not // finalization. // TODO(delphick): Currently even during execution this can be nullptr, due to // JSToWasmWrapperCompilationUnit::Execute. Once a table can be extracted // there, this method can DCHECK that it is never nullptr. RuntimeCallStats* runtime_call_stats() const { return runtime_call_stats_; } void set_runtime_call_stats(RuntimeCallStats* stats) { runtime_call_stats_ = stats; } // Used to skip the "wasm-inlining" phase when there are no JS-to-Wasm calls. bool has_js_wasm_calls() const { return has_js_wasm_calls_; } void set_has_js_wasm_calls(bool has_js_wasm_calls) { has_js_wasm_calls_ = has_js_wasm_calls; } private: Isolate* const isolate_; #if V8_ENABLE_WEBASSEMBLY wasm::WasmEngine* const wasm_engine_ = nullptr; #endif // V8_ENABLE_WEBASSEMBLY AccountingAllocator* const allocator_; OptimizedCompilationInfo* const info_; std::unique_ptr debug_name_; bool may_have_unverifiable_graph_ = true; ZoneStats* const zone_stats_; PipelineStatistics* pipeline_statistics_ = nullptr; bool compilation_failed_ = false; bool verify_graph_ = false; int start_source_position_ = kNoSourcePosition; base::Optional osr_helper_; MaybeHandle code_; CodeGenerator* code_generator_ = nullptr; Typer* typer_ = nullptr; Typer::Flags typer_flags_ = Typer::kNoFlags; // All objects in the following group of fields are allocated in graph_zone_. // They are all set to nullptr when the graph_zone_ is destroyed. ZoneStats::Scope graph_zone_scope_; Zone* graph_zone_ = nullptr; Graph* graph_ = nullptr; SourcePositionTable* source_positions_ = nullptr; NodeOriginTable* node_origins_ = nullptr; SimplifiedOperatorBuilder* simplified_ = nullptr; MachineOperatorBuilder* machine_ = nullptr; CommonOperatorBuilder* common_ = nullptr; JSOperatorBuilder* javascript_ = nullptr; JSGraph* jsgraph_ = nullptr; MachineGraph* mcgraph_ = nullptr; Schedule* schedule_ = nullptr; ObserveNodeManager* observe_node_manager_ = nullptr; // All objects in the following group of fields are allocated in // instruction_zone_. They are all set to nullptr when the instruction_zone_ // is destroyed. ZoneStats::Scope instruction_zone_scope_; Zone* instruction_zone_; InstructionSequence* sequence_ = nullptr; // All objects in the following group of fields are allocated in // codegen_zone_. They are all set to nullptr when the codegen_zone_ // is destroyed. ZoneStats::Scope codegen_zone_scope_; Zone* codegen_zone_; CompilationDependencies* dependencies_ = nullptr; JSHeapBroker* broker_ = nullptr; Frame* frame_ = nullptr; // All objects in the following group of fields are allocated in // register_allocation_zone_. They are all set to nullptr when the zone is // destroyed. ZoneStats::Scope register_allocation_zone_scope_; Zone* register_allocation_zone_; RegisterAllocationData* register_allocation_data_ = nullptr; // Source position output for --trace-turbo. std::string source_position_output_; JumpOptimizationInfo* jump_optimization_info_ = nullptr; AssemblerOptions assembler_options_; Maybe specialization_context_ = Nothing(); // The maximal combined height of all inlined frames in their unoptimized // state, and the maximal number of arguments pushed during function calls. // Calculated during instruction selection, applied during code generation. size_t max_unoptimized_frame_height_ = 0; size_t max_pushed_argument_count_ = 0; RuntimeCallStats* runtime_call_stats_ = nullptr; const ProfileDataFromFile* profile_data_ = nullptr; bool has_js_wasm_calls_ = false; }; class PipelineImpl final { public: explicit PipelineImpl(PipelineData* data) : data_(data) {} // Helpers for executing pipeline phases. template void Run(Args&&... args); // Step A.1. Initialize the heap broker. void InitializeHeapBroker(); // Step A.2. Run the graph creation and initial optimization passes. bool CreateGraph(); // Step B. Run the concurrent optimization passes. bool OptimizeGraph(Linkage* linkage); // Substep B.1. Produce a scheduled graph. void ComputeScheduledGraph(); // Substep B.2. Select instructions from a scheduled graph. bool SelectInstructions(Linkage* linkage); // Step C. Run the code assembly pass. void AssembleCode(Linkage* linkage); // Step D. Run the code finalization pass. MaybeHandle FinalizeCode(bool retire_broker = true); // Step E. Install any code dependencies. bool CommitDependencies(Handle code); void VerifyGeneratedCodeIsIdempotent(); void RunPrintAndVerify(const char* phase, bool untyped = false); bool SelectInstructionsAndAssemble(CallDescriptor* call_descriptor); MaybeHandle GenerateCode(CallDescriptor* call_descriptor); void AllocateRegistersForTopTier(const RegisterConfiguration* config, CallDescriptor* call_descriptor, bool run_verifier); void AllocateRegistersForMidTier(const RegisterConfiguration* config, CallDescriptor* call_descriptor, bool run_verifier); OptimizedCompilationInfo* info() const; Isolate* isolate() const; CodeGenerator* code_generator() const; ObserveNodeManager* observe_node_manager() const; private: PipelineData* const data_; }; namespace { class SourcePositionWrapper final : public Reducer { public: SourcePositionWrapper(Reducer* reducer, SourcePositionTable* table) : reducer_(reducer), table_(table) {} ~SourcePositionWrapper() final = default; SourcePositionWrapper(const SourcePositionWrapper&) = delete; SourcePositionWrapper& operator=(const SourcePositionWrapper&) = delete; const char* reducer_name() const override { return reducer_->reducer_name(); } Reduction Reduce(Node* node) final { SourcePosition const pos = table_->GetSourcePosition(node); SourcePositionTable::Scope position(table_, pos); return reducer_->Reduce(node, nullptr); } void Finalize() final { reducer_->Finalize(); } private: Reducer* const reducer_; SourcePositionTable* const table_; }; class NodeOriginsWrapper final : public Reducer { public: NodeOriginsWrapper(Reducer* reducer, NodeOriginTable* table) : reducer_(reducer), table_(table) {} ~NodeOriginsWrapper() final = default; NodeOriginsWrapper(const NodeOriginsWrapper&) = delete; NodeOriginsWrapper& operator=(const NodeOriginsWrapper&) = delete; const char* reducer_name() const override { return reducer_->reducer_name(); } Reduction Reduce(Node* node) final { NodeOriginTable::Scope position(table_, reducer_name(), node); return reducer_->Reduce(node, nullptr); } void Finalize() final { reducer_->Finalize(); } private: Reducer* const reducer_; NodeOriginTable* const table_; }; class V8_NODISCARD PipelineRunScope { public: #ifdef V8_RUNTIME_CALL_STATS PipelineRunScope( PipelineData* data, const char* phase_name, RuntimeCallCounterId runtime_call_counter_id, RuntimeCallStats::CounterMode counter_mode = RuntimeCallStats::kExact) : phase_scope_(data->pipeline_statistics(), phase_name), zone_scope_(data->zone_stats(), phase_name), origin_scope_(data->node_origins(), phase_name), runtime_call_timer_scope(data->runtime_call_stats(), runtime_call_counter_id, counter_mode) { DCHECK_NOT_NULL(phase_name); } #else // V8_RUNTIME_CALL_STATS PipelineRunScope(PipelineData* data, const char* phase_name) : phase_scope_(data->pipeline_statistics(), phase_name), zone_scope_(data->zone_stats(), phase_name), origin_scope_(data->node_origins(), phase_name) { DCHECK_NOT_NULL(phase_name); } #endif // V8_RUNTIME_CALL_STATS Zone* zone() { return zone_scope_.zone(); } private: PhaseScope phase_scope_; ZoneStats::Scope zone_scope_; NodeOriginTable::PhaseScope origin_scope_; #ifdef V8_RUNTIME_CALL_STATS RuntimeCallTimerScope runtime_call_timer_scope; #endif // V8_RUNTIME_CALL_STATS }; // LocalIsolateScope encapsulates the phase where persistent handles are // attached to the LocalHeap inside {local_isolate}. class V8_NODISCARD LocalIsolateScope { public: explicit LocalIsolateScope(JSHeapBroker* broker, OptimizedCompilationInfo* info, LocalIsolate* local_isolate) : broker_(broker), info_(info) { broker_->AttachLocalIsolate(info_, local_isolate); info_->tick_counter().AttachLocalHeap(local_isolate->heap()); } ~LocalIsolateScope() { info_->tick_counter().DetachLocalHeap(); broker_->DetachLocalIsolate(info_); } private: JSHeapBroker* broker_; OptimizedCompilationInfo* info_; }; void PrintFunctionSource(OptimizedCompilationInfo* info, Isolate* isolate, int source_id, Handle shared) { if (!shared->script().IsUndefined(isolate)) { Handle