// 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 // NOLINT(readability/streams) #include #include #include "src/base/adapters.h" #include "src/base/platform/elapsed-timer.h" #include "src/compilation-info.h" #include "src/compiler.h" #include "src/compiler/ast-graph-builder.h" #include "src/compiler/ast-loop-assignment-analyzer.h" #include "src/compiler/basic-block-instrumentor.h" #include "src/compiler/branch-elimination.h" #include "src/compiler/bytecode-graph-builder.h" #include "src/compiler/checkpoint-elimination.h" #include "src/compiler/code-generator.h" #include "src/compiler/common-operator-reducer.h" #include "src/compiler/control-flow-optimizer.h" #include "src/compiler/dead-code-elimination.h" #include "src/compiler/effect-control-linearizer.h" #include "src/compiler/escape-analysis-reducer.h" #include "src/compiler/escape-analysis.h" #include "src/compiler/frame-elider.h" #include "src/compiler/graph-replay.h" #include "src/compiler/graph-trimmer.h" #include "src/compiler/graph-visualizer.h" #include "src/compiler/instruction-selector.h" #include "src/compiler/instruction.h" #include "src/compiler/js-builtin-reducer.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-frame-specialization.h" #include "src/compiler/js-generic-lowering.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/jump-threading.h" #include "src/compiler/live-range-separator.h" #include "src/compiler/load-elimination.h" #include "src/compiler/loop-analysis.h" #include "src/compiler/loop-peeling.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/move-optimizer.h" #include "src/compiler/osr.h" #include "src/compiler/pipeline-statistics.h" #include "src/compiler/redundancy-elimination.h" #include "src/compiler/register-allocator-verifier.h" #include "src/compiler/register-allocator.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/tail-call-optimization.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/isolate-inl.h" #include "src/ostreams.h" #include "src/parsing/parse-info.h" #include "src/register-configuration.h" #include "src/trap-handler/trap-handler.h" #include "src/type-info.h" #include "src/utils.h" namespace v8 { namespace internal { namespace compiler { class PipelineData { public: // For main entry point. PipelineData(ZoneStats* zone_stats, CompilationInfo* info, PipelineStatistics* pipeline_statistics) : isolate_(info->isolate()), info_(info), debug_name_(info_->GetDebugName()), outer_zone_(info_->zone()), zone_stats_(zone_stats), pipeline_statistics_(pipeline_statistics), graph_zone_scope_(zone_stats_, ZONE_NAME), graph_zone_(graph_zone_scope_.zone()), instruction_zone_scope_(zone_stats_, ZONE_NAME), instruction_zone_(instruction_zone_scope_.zone()), register_allocation_zone_scope_(zone_stats_, ZONE_NAME), register_allocation_zone_(register_allocation_zone_scope_.zone()) { PhaseScope scope(pipeline_statistics, "init pipeline data"); graph_ = new (graph_zone_) Graph(graph_zone_); source_positions_ = new (graph_zone_) SourcePositionTable(graph_); simplified_ = new (graph_zone_) SimplifiedOperatorBuilder(graph_zone_); machine_ = new (graph_zone_) MachineOperatorBuilder( graph_zone_, MachineType::PointerRepresentation(), InstructionSelector::SupportedMachineOperatorFlags(), InstructionSelector::AlignmentRequirements()); common_ = new (graph_zone_) CommonOperatorBuilder(graph_zone_); javascript_ = new (graph_zone_) JSOperatorBuilder(graph_zone_); jsgraph_ = new (graph_zone_) JSGraph(isolate_, graph_, common_, javascript_, simplified_, machine_); is_asm_ = info->shared_info()->asm_function(); } // For WASM compile entry point. PipelineData(ZoneStats* zone_stats, CompilationInfo* info, JSGraph* jsgraph, SourcePositionTable* source_positions, ZoneVector* protected_instructions) : isolate_(info->isolate()), info_(info), debug_name_(info_->GetDebugName()), zone_stats_(zone_stats), graph_zone_scope_(zone_stats_, ZONE_NAME), graph_(jsgraph->graph()), source_positions_(source_positions), machine_(jsgraph->machine()), common_(jsgraph->common()), javascript_(jsgraph->javascript()), jsgraph_(jsgraph), instruction_zone_scope_(zone_stats_, ZONE_NAME), instruction_zone_(instruction_zone_scope_.zone()), register_allocation_zone_scope_(zone_stats_, ZONE_NAME), register_allocation_zone_(register_allocation_zone_scope_.zone()), protected_instructions_(protected_instructions) { is_asm_ = info->has_shared_info() ? info->shared_info()->asm_function() : false; } // For machine graph testing entry point. PipelineData(ZoneStats* zone_stats, CompilationInfo* info, Graph* graph, Schedule* schedule, SourcePositionTable* source_positions) : isolate_(info->isolate()), info_(info), debug_name_(info_->GetDebugName()), zone_stats_(zone_stats), graph_zone_scope_(zone_stats_, ZONE_NAME), graph_(graph), source_positions_(source_positions), schedule_(schedule), instruction_zone_scope_(zone_stats_, ZONE_NAME), instruction_zone_(instruction_zone_scope_.zone()), register_allocation_zone_scope_(zone_stats_, ZONE_NAME), register_allocation_zone_(register_allocation_zone_scope_.zone()) { is_asm_ = false; } // For register allocation testing entry point. PipelineData(ZoneStats* zone_stats, CompilationInfo* info, InstructionSequence* sequence) : isolate_(info->isolate()), info_(info), debug_name_(info_->GetDebugName()), zone_stats_(zone_stats), graph_zone_scope_(zone_stats_, ZONE_NAME), instruction_zone_scope_(zone_stats_, ZONE_NAME), instruction_zone_(sequence->zone()), sequence_(sequence), register_allocation_zone_scope_(zone_stats_, ZONE_NAME), register_allocation_zone_(register_allocation_zone_scope_.zone()) { is_asm_ = info->has_shared_info() ? info->shared_info()->asm_function() : false; } ~PipelineData() { DeleteRegisterAllocationZone(); DeleteInstructionZone(); DeleteGraphZone(); } Isolate* isolate() const { return isolate_; } CompilationInfo* info() const { return info_; } ZoneStats* zone_stats() const { return zone_stats_; } PipelineStatistics* pipeline_statistics() { return pipeline_statistics_; } bool compilation_failed() const { return compilation_failed_; } void set_compilation_failed() { compilation_failed_ = true; } bool is_asm() const { return is_asm_; } bool verify_graph() const { return verify_graph_; } void set_verify_graph(bool value) { verify_graph_ = value; } Handle code() { return code_; } void set_code(Handle code) { DCHECK(code_.is_null()); code_ = code; } // RawMachineAssembler generally produces graphs which cannot be verified. bool MayHaveUnverifiableGraph() const { return outer_zone_ == nullptr; } Zone* graph_zone() const { return graph_zone_; } Graph* graph() const { return graph_; } SourcePositionTable* source_positions() const { return source_positions_; } MachineOperatorBuilder* machine() const { return machine_; } CommonOperatorBuilder* common() const { return common_; } JSOperatorBuilder* javascript() const { return javascript_; } JSGraph* jsgraph() const { return jsgraph_; } Handle native_context() const { return handle(info()->native_context(), isolate()); } Handle global_object() const { return handle(info()->global_object(), isolate()); } LoopAssignmentAnalysis* loop_assignment() const { return loop_assignment_; } void set_loop_assignment(LoopAssignmentAnalysis* loop_assignment) { DCHECK(!loop_assignment_); loop_assignment_ = loop_assignment; } Schedule* schedule() const { return schedule_; } void set_schedule(Schedule* schedule) { DCHECK(!schedule_); schedule_ = schedule; } void reset_schedule() { schedule_ = nullptr; } Zone* instruction_zone() const { return instruction_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_; } BasicBlockProfiler::Data* profiler_data() const { return profiler_data_; } void set_profiler_data(BasicBlockProfiler::Data* profiler_data) { profiler_data_ = profiler_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; } ZoneVector* protected_instructions() const { return protected_instructions_; } void DeleteGraphZone() { if (graph_zone_ == nullptr) return; graph_zone_scope_.Destroy(); graph_zone_ = nullptr; graph_ = nullptr; source_positions_ = nullptr; loop_assignment_ = nullptr; simplified_ = nullptr; machine_ = nullptr; common_ = nullptr; javascript_ = nullptr; jsgraph_ = nullptr; schedule_ = nullptr; } void DeleteInstructionZone() { if (instruction_zone_ == nullptr) return; instruction_zone_scope_.Destroy(); instruction_zone_ = nullptr; sequence_ = 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* descriptor) { DCHECK(sequence_ == nullptr); InstructionBlocks* instruction_blocks = InstructionSequence::InstructionBlocksFor(instruction_zone(), schedule()); sequence_ = new (instruction_zone()) InstructionSequence( info()->isolate(), instruction_zone(), instruction_blocks); if (descriptor && descriptor->RequiresFrameAsIncoming()) { sequence_->instruction_blocks()[0]->mark_needs_frame(); } else { DCHECK_EQ(0u, descriptor->CalleeSavedFPRegisters()); DCHECK_EQ(0u, descriptor->CalleeSavedRegisters()); } } void InitializeFrameData(CallDescriptor* descriptor) { DCHECK(frame_ == nullptr); int fixed_frame_size = 0; if (descriptor != nullptr) { fixed_frame_size = descriptor->CalculateFixedFrameSize(); } frame_ = new (instruction_zone()) Frame(fixed_frame_size); } void InitializeRegisterAllocationData(const RegisterConfiguration* config, CallDescriptor* descriptor) { DCHECK(register_allocation_data_ == nullptr); register_allocation_data_ = new (register_allocation_zone()) RegisterAllocationData(config, register_allocation_zone(), frame(), sequence(), debug_name()); } 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(); } private: Isolate* const isolate_; CompilationInfo* const info_; std::unique_ptr debug_name_; Zone* outer_zone_ = nullptr; ZoneStats* const zone_stats_; PipelineStatistics* pipeline_statistics_ = nullptr; bool compilation_failed_ = false; bool verify_graph_ = false; bool is_asm_ = false; Handle code_ = Handle::null(); // 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; LoopAssignmentAnalysis* loop_assignment_ = nullptr; SimplifiedOperatorBuilder* simplified_ = nullptr; MachineOperatorBuilder* machine_ = nullptr; CommonOperatorBuilder* common_ = nullptr; JSOperatorBuilder* javascript_ = nullptr; JSGraph* jsgraph_ = nullptr; Schedule* schedule_ = 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; 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; // Basic block profiling support. BasicBlockProfiler::Data* profiler_data_ = nullptr; // Source position output for --trace-turbo. std::string source_position_output_; ZoneVector* protected_instructions_ = nullptr; DISALLOW_COPY_AND_ASSIGN(PipelineData); }; class PipelineImpl final { public: explicit PipelineImpl(PipelineData* data) : data_(data) {} // Helpers for executing pipeline phases. template void Run(); template void Run(Arg0 arg_0); template void Run(Arg0 arg_0, Arg1 arg_1); // Run the graph creation and initial optimization passes. bool CreateGraph(); // Run the concurrent optimization passes. bool OptimizeGraph(Linkage* linkage); // Perform the actual code generation and return handle to a code object. Handle GenerateCode(Linkage* linkage); bool ScheduleAndSelectInstructions(Linkage* linkage, bool trim_graph); void RunPrintAndVerify(const char* phase, bool untyped = false); Handle ScheduleAndGenerateCode(CallDescriptor* call_descriptor); void AllocateRegisters(const RegisterConfiguration* config, CallDescriptor* descriptor, bool run_verifier); CompilationInfo* info() const; Isolate* isolate() const; PipelineData* const data_; }; namespace { struct TurboCfgFile : public std::ofstream { explicit TurboCfgFile(Isolate* isolate) : std::ofstream(isolate->GetTurboCfgFileName().c_str(), std::ios_base::app) {} }; struct TurboJsonFile : public std::ofstream { TurboJsonFile(CompilationInfo* info, std::ios_base::openmode mode) : std::ofstream(GetVisualizerLogFileName(info, nullptr, "json").get(), mode) {} }; void TraceSchedule(CompilationInfo* info, Schedule* schedule) { if (FLAG_trace_turbo) { AllowHandleDereference allow_deref; TurboJsonFile json_of(info, std::ios_base::app); json_of << "{\"name\":\"Schedule\",\"type\":\"schedule\",\"data\":\""; std::stringstream schedule_stream; schedule_stream << *schedule; std::string schedule_string(schedule_stream.str()); for (const auto& c : schedule_string) { json_of << AsEscapedUC16ForJSON(c); } json_of << "\"},\n"; } if (FLAG_trace_turbo_graph || FLAG_trace_turbo_scheduler) { AllowHandleDereference allow_deref; CodeTracer::Scope tracing_scope(info->isolate()->GetCodeTracer()); OFStream os(tracing_scope.file()); os << "-- Schedule --------------------------------------\n" << *schedule; } } class SourcePositionWrapper final : public Reducer { public: SourcePositionWrapper(Reducer* reducer, SourcePositionTable* table) : reducer_(reducer), table_(table) {} ~SourcePositionWrapper() final {} Reduction Reduce(Node* node) final { SourcePosition const pos = table_->GetSourcePosition(node); SourcePositionTable::Scope position(table_, pos); return reducer_->Reduce(node); } void Finalize() final { reducer_->Finalize(); } private: Reducer* const reducer_; SourcePositionTable* const table_; DISALLOW_COPY_AND_ASSIGN(SourcePositionWrapper); }; class JSGraphReducer final : public GraphReducer { public: JSGraphReducer(JSGraph* jsgraph, Zone* zone) : GraphReducer(zone, jsgraph->graph(), jsgraph->Dead()) {} ~JSGraphReducer() final {} }; void AddReducer(PipelineData* data, GraphReducer* graph_reducer, Reducer* reducer) { if (data->info()->is_source_positions_enabled()) { void* const buffer = data->graph_zone()->New(sizeof(SourcePositionWrapper)); SourcePositionWrapper* const wrapper = new (buffer) SourcePositionWrapper(reducer, data->source_positions()); graph_reducer->AddReducer(wrapper); } else { graph_reducer->AddReducer(reducer); } } class PipelineRunScope { public: PipelineRunScope(PipelineData* data, const char* phase_name) : phase_scope_( phase_name == nullptr ? nullptr : data->pipeline_statistics(), phase_name), zone_scope_(data->zone_stats(), ZONE_NAME) {} Zone* zone() { return zone_scope_.zone(); } private: PhaseScope phase_scope_; ZoneStats::Scope zone_scope_; }; PipelineStatistics* CreatePipelineStatistics(CompilationInfo* info, ZoneStats* zone_stats) { PipelineStatistics* pipeline_statistics = nullptr; if (FLAG_turbo_stats || FLAG_turbo_stats_nvp) { pipeline_statistics = new PipelineStatistics(info, zone_stats); pipeline_statistics->BeginPhaseKind("initializing"); } if (FLAG_trace_turbo) { TurboJsonFile json_of(info, std::ios_base::trunc); Handle