// 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 "src/base/adapters.h" #include "src/base/platform/elapsed-timer.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-global-object-specialization.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/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/type-hint-analyzer.h" #include "src/compiler/typer.h" #include "src/compiler/value-numbering-reducer.h" #include "src/compiler/verifier.h" #include "src/compiler/zone-pool.h" #include "src/isolate-inl.h" #include "src/ostreams.h" #include "src/parsing/parser.h" #include "src/register-configuration.h" #include "src/type-info.h" #include "src/utils.h" namespace v8 { namespace internal { namespace compiler { class PipelineData { public: // For main entry point. PipelineData(ZonePool* zone_pool, CompilationInfo* info, PipelineStatistics* pipeline_statistics) : isolate_(info->isolate()), info_(info), debug_name_(info_->GetDebugName()), outer_zone_(info_->zone()), zone_pool_(zone_pool), pipeline_statistics_(pipeline_statistics), graph_zone_scope_(zone_pool_), graph_zone_(graph_zone_scope_.zone()), instruction_zone_scope_(zone_pool_), instruction_zone_(instruction_zone_scope_.zone()), register_allocation_zone_scope_(zone_pool_), 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()); 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_); } // For WASM compile entry point. PipelineData(ZonePool* zone_pool, CompilationInfo* info, Graph* graph, SourcePositionTable* source_positions) : isolate_(info->isolate()), info_(info), debug_name_(info_->GetDebugName()), zone_pool_(zone_pool), graph_zone_scope_(zone_pool_), graph_(graph), source_positions_(source_positions), instruction_zone_scope_(zone_pool_), instruction_zone_(instruction_zone_scope_.zone()), register_allocation_zone_scope_(zone_pool_), register_allocation_zone_(register_allocation_zone_scope_.zone()) {} // For machine graph testing entry point. PipelineData(ZonePool* zone_pool, CompilationInfo* info, Graph* graph, Schedule* schedule) : isolate_(info->isolate()), info_(info), debug_name_(info_->GetDebugName()), zone_pool_(zone_pool), graph_zone_scope_(zone_pool_), graph_(graph), source_positions_(new (info->zone()) SourcePositionTable(graph_)), schedule_(schedule), instruction_zone_scope_(zone_pool_), instruction_zone_(instruction_zone_scope_.zone()), register_allocation_zone_scope_(zone_pool_), register_allocation_zone_(register_allocation_zone_scope_.zone()) {} // For register allocation testing entry point. PipelineData(ZonePool* zone_pool, CompilationInfo* info, InstructionSequence* sequence) : isolate_(info->isolate()), info_(info), debug_name_(info_->GetDebugName()), zone_pool_(zone_pool), graph_zone_scope_(zone_pool_), instruction_zone_scope_(zone_pool_), instruction_zone_(sequence->zone()), sequence_(sequence), register_allocation_zone_scope_(zone_pool_), register_allocation_zone_(register_allocation_zone_scope_.zone()) {} ~PipelineData() { DeleteRegisterAllocationZone(); DeleteInstructionZone(); DeleteGraphZone(); } Isolate* isolate() const { return isolate_; } CompilationInfo* info() const { return info_; } ZonePool* zone_pool() const { return zone_pool_; } PipelineStatistics* pipeline_statistics() { return pipeline_statistics_; } bool compilation_failed() const { return compilation_failed_; } void set_compilation_failed() { compilation_failed_ = true; } 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_; } MaybeHandle native_context() const { if (info()->is_native_context_specializing()) { return handle(info()->native_context(), isolate()); } return MaybeHandle(); } LoopAssignmentAnalysis* loop_assignment() const { return loop_assignment_; } void set_loop_assignment(LoopAssignmentAnalysis* loop_assignment) { DCHECK(!loop_assignment_); loop_assignment_ = loop_assignment; } TypeHintAnalysis* type_hint_analysis() const { return type_hint_analysis_; } void set_type_hint_analysis(TypeHintAnalysis* type_hint_analysis) { DCHECK_NULL(type_hint_analysis_); type_hint_analysis_ = type_hint_analysis; } 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; } void DeleteGraphZone() { if (graph_zone_ == nullptr) return; graph_zone_scope_.Destroy(); graph_zone_ = nullptr; graph_ = nullptr; source_positions_ = nullptr; loop_assignment_ = nullptr; type_hint_analysis_ = 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(0, descriptor->CalleeSavedFPRegisters()); DCHECK_EQ(0, descriptor->CalleeSavedRegisters()); } } void InitializeFrameData(CallDescriptor* descriptor) { DCHECK(frame_ == nullptr); int fixed_frame_size = 0; if (descriptor != nullptr) { fixed_frame_size = CalculateFixedFrameSize(descriptor); } 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_.get()); } 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(); } } private: Isolate* const isolate_; CompilationInfo* const info_; base::SmartArrayPointer debug_name_; Zone* outer_zone_ = nullptr; ZonePool* const zone_pool_; PipelineStatistics* pipeline_statistics_ = nullptr; bool compilation_failed_ = 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. ZonePool::Scope graph_zone_scope_; Zone* graph_zone_ = nullptr; Graph* graph_ = nullptr; SourcePositionTable* source_positions_ = nullptr; LoopAssignmentAnalysis* loop_assignment_ = nullptr; TypeHintAnalysis* type_hint_analysis_ = 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. ZonePool::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. ZonePool::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_; int CalculateFixedFrameSize(CallDescriptor* descriptor) { if (descriptor->IsJSFunctionCall()) { return StandardFrameConstants::kFixedSlotCount; } return descriptor->IsCFunctionCall() ? (CommonFrameConstants::kFixedSlotCountAboveFp + CommonFrameConstants::kCPSlotCount) : TypedFrameConstants::kFixedSlotCount; } 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); 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; OFStream os(stdout); os << "-- Schedule --------------------------------------\n" << *schedule; } } class AstGraphBuilderWithPositions final : public AstGraphBuilder { public: AstGraphBuilderWithPositions(Zone* local_zone, CompilationInfo* info, JSGraph* jsgraph, LoopAssignmentAnalysis* loop_assignment, TypeHintAnalysis* type_hint_analysis, SourcePositionTable* source_positions) : AstGraphBuilder(local_zone, info, jsgraph, loop_assignment, type_hint_analysis), source_positions_(source_positions), start_position_(info->shared_info()->start_position()) {} bool CreateGraph(bool stack_check) { SourcePositionTable::Scope pos_scope(source_positions_, start_position_); return AstGraphBuilder::CreateGraph(stack_check); } #define DEF_VISIT(type) \ void Visit##type(type* node) override { \ SourcePositionTable::Scope pos(source_positions_, \ SourcePosition(node->position())); \ AstGraphBuilder::Visit##type(node); \ } AST_NODE_LIST(DEF_VISIT) #undef DEF_VISIT private: SourcePositionTable* const source_positions_; SourcePosition const start_position_; }; 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_pool()) {} Zone* zone() { return zone_scope_.zone(); } private: PhaseScope phase_scope_; ZonePool::Scope zone_scope_; }; PipelineStatistics* CreatePipelineStatistics(CompilationInfo* info, ZonePool* zone_pool) { PipelineStatistics* pipeline_statistics = nullptr; if (FLAG_turbo_stats || FLAG_turbo_stats_nvp) { pipeline_statistics = new PipelineStatistics(info, zone_pool); pipeline_statistics->BeginPhaseKind("initializing"); } if (FLAG_trace_turbo) { TurboJsonFile json_of(info, std::ios_base::trunc); Handle