blob: 82583e914d8e6765b4bef1d7a89ec45facaaf7dd [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2014 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/compiler/pipeline.h"
6
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007#include <fstream> // NOLINT(readability/streams)
8#include <sstream>
9
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010#include "src/base/adapters.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011#include "src/base/platform/elapsed-timer.h"
12#include "src/compiler/ast-graph-builder.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040013#include "src/compiler/ast-loop-assignment-analyzer.h"
14#include "src/compiler/basic-block-instrumentor.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015#include "src/compiler/branch-elimination.h"
16#include "src/compiler/bytecode-graph-builder.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017#include "src/compiler/code-generator.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040018#include "src/compiler/common-operator-reducer.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019#include "src/compiler/control-flow-optimizer.h"
20#include "src/compiler/dead-code-elimination.h"
Ben Murdochc5610432016-08-08 18:44:38 +010021#include "src/compiler/effect-control-linearizer.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000022#include "src/compiler/escape-analysis-reducer.h"
Ben Murdochc5610432016-08-08 18:44:38 +010023#include "src/compiler/escape-analysis.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000024#include "src/compiler/frame-elider.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000025#include "src/compiler/graph-replay.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000026#include "src/compiler/graph-trimmer.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000027#include "src/compiler/graph-visualizer.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000028#include "src/compiler/greedy-allocator.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000029#include "src/compiler/instruction-selector.h"
Ben Murdochc5610432016-08-08 18:44:38 +010030#include "src/compiler/instruction.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040031#include "src/compiler/js-builtin-reducer.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000032#include "src/compiler/js-call-reducer.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000033#include "src/compiler/js-context-specialization.h"
Ben Murdoch097c5b22016-05-18 11:27:45 +010034#include "src/compiler/js-create-lowering.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000035#include "src/compiler/js-frame-specialization.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000036#include "src/compiler/js-generic-lowering.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000037#include "src/compiler/js-global-object-specialization.h"
38#include "src/compiler/js-inlining-heuristic.h"
39#include "src/compiler/js-intrinsic-lowering.h"
40#include "src/compiler/js-native-context-specialization.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000041#include "src/compiler/js-typed-lowering.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040042#include "src/compiler/jump-threading.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000043#include "src/compiler/live-range-separator.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040044#include "src/compiler/load-elimination.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000045#include "src/compiler/loop-analysis.h"
46#include "src/compiler/loop-peeling.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000047#include "src/compiler/machine-operator-reducer.h"
Ben Murdochc5610432016-08-08 18:44:38 +010048#include "src/compiler/memory-optimizer.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040049#include "src/compiler/move-optimizer.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000050#include "src/compiler/osr.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040051#include "src/compiler/pipeline-statistics.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040052#include "src/compiler/register-allocator-verifier.h"
Ben Murdochc5610432016-08-08 18:44:38 +010053#include "src/compiler/register-allocator.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000054#include "src/compiler/schedule.h"
55#include "src/compiler/scheduler.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040056#include "src/compiler/select-lowering.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000057#include "src/compiler/simplified-lowering.h"
58#include "src/compiler/simplified-operator-reducer.h"
Ben Murdochc5610432016-08-08 18:44:38 +010059#include "src/compiler/simplified-operator.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000060#include "src/compiler/tail-call-optimization.h"
61#include "src/compiler/type-hint-analyzer.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000062#include "src/compiler/typer.h"
63#include "src/compiler/value-numbering-reducer.h"
64#include "src/compiler/verifier.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040065#include "src/compiler/zone-pool.h"
Ben Murdochc5610432016-08-08 18:44:38 +010066#include "src/isolate-inl.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000067#include "src/ostreams.h"
Ben Murdochc5610432016-08-08 18:44:38 +010068#include "src/parsing/parser.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000069#include "src/register-configuration.h"
70#include "src/type-info.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000071#include "src/utils.h"
72
73namespace v8 {
74namespace internal {
75namespace compiler {
76
Emily Bernierd0a1eb72015-03-24 16:35:39 -040077class PipelineData {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000078 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000079 // For main entry point.
80 PipelineData(ZonePool* zone_pool, CompilationInfo* info,
81 PipelineStatistics* pipeline_statistics)
82 : isolate_(info->isolate()),
83 info_(info),
Ben Murdochc5610432016-08-08 18:44:38 +010084 debug_name_(info_->GetDebugName()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000085 outer_zone_(info_->zone()),
86 zone_pool_(zone_pool),
87 pipeline_statistics_(pipeline_statistics),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000088 graph_zone_scope_(zone_pool_),
89 graph_zone_(graph_zone_scope_.zone()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000090 instruction_zone_scope_(zone_pool_),
91 instruction_zone_(instruction_zone_scope_.zone()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000092 register_allocation_zone_scope_(zone_pool_),
Ben Murdochc5610432016-08-08 18:44:38 +010093 register_allocation_zone_(register_allocation_zone_scope_.zone()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000094 PhaseScope scope(pipeline_statistics, "init pipeline data");
95 graph_ = new (graph_zone_) Graph(graph_zone_);
Ben Murdochc5610432016-08-08 18:44:38 +010096 source_positions_ = new (graph_zone_) SourcePositionTable(graph_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000097 simplified_ = new (graph_zone_) SimplifiedOperatorBuilder(graph_zone_);
98 machine_ = new (graph_zone_) MachineOperatorBuilder(
99 graph_zone_, MachineType::PointerRepresentation(),
100 InstructionSelector::SupportedMachineOperatorFlags());
101 common_ = new (graph_zone_) CommonOperatorBuilder(graph_zone_);
102 javascript_ = new (graph_zone_) JSOperatorBuilder(graph_zone_);
103 jsgraph_ = new (graph_zone_)
104 JSGraph(isolate_, graph_, common_, javascript_, simplified_, machine_);
105 }
106
Ben Murdochc5610432016-08-08 18:44:38 +0100107 // For WASM compile entry point.
108 PipelineData(ZonePool* zone_pool, CompilationInfo* info, Graph* graph,
109 SourcePositionTable* source_positions)
110 : isolate_(info->isolate()),
111 info_(info),
112 debug_name_(info_->GetDebugName()),
113 zone_pool_(zone_pool),
114 graph_zone_scope_(zone_pool_),
115 graph_(graph),
116 source_positions_(source_positions),
117 instruction_zone_scope_(zone_pool_),
118 instruction_zone_(instruction_zone_scope_.zone()),
119 register_allocation_zone_scope_(zone_pool_),
120 register_allocation_zone_(register_allocation_zone_scope_.zone()) {}
121
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000122 // For machine graph testing entry point.
123 PipelineData(ZonePool* zone_pool, CompilationInfo* info, Graph* graph,
124 Schedule* schedule)
125 : isolate_(info->isolate()),
126 info_(info),
Ben Murdochc5610432016-08-08 18:44:38 +0100127 debug_name_(info_->GetDebugName()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000128 zone_pool_(zone_pool),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000129 graph_zone_scope_(zone_pool_),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000130 graph_(graph),
Ben Murdochc5610432016-08-08 18:44:38 +0100131 source_positions_(new (info->zone()) SourcePositionTable(graph_)),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000132 schedule_(schedule),
133 instruction_zone_scope_(zone_pool_),
134 instruction_zone_(instruction_zone_scope_.zone()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000135 register_allocation_zone_scope_(zone_pool_),
Ben Murdochc5610432016-08-08 18:44:38 +0100136 register_allocation_zone_(register_allocation_zone_scope_.zone()) {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000137
138 // For register allocation testing entry point.
139 PipelineData(ZonePool* zone_pool, CompilationInfo* info,
140 InstructionSequence* sequence)
141 : isolate_(info->isolate()),
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400142 info_(info),
Ben Murdochc5610432016-08-08 18:44:38 +0100143 debug_name_(info_->GetDebugName()),
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400144 zone_pool_(zone_pool),
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400145 graph_zone_scope_(zone_pool_),
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400146 instruction_zone_scope_(zone_pool_),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000147 instruction_zone_(sequence->zone()),
148 sequence_(sequence),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000149 register_allocation_zone_scope_(zone_pool_),
Ben Murdochc5610432016-08-08 18:44:38 +0100150 register_allocation_zone_(register_allocation_zone_scope_.zone()) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000151
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400152 ~PipelineData() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000153 DeleteRegisterAllocationZone();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400154 DeleteInstructionZone();
155 DeleteGraphZone();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000156 }
157
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400158 Isolate* isolate() const { return isolate_; }
159 CompilationInfo* info() const { return info_; }
160 ZonePool* zone_pool() const { return zone_pool_; }
161 PipelineStatistics* pipeline_statistics() { return pipeline_statistics_; }
162 bool compilation_failed() const { return compilation_failed_; }
163 void set_compilation_failed() { compilation_failed_ = true; }
164 Handle<Code> code() { return code_; }
165 void set_code(Handle<Code> code) {
166 DCHECK(code_.is_null());
167 code_ = code;
168 }
169
170 // RawMachineAssembler generally produces graphs which cannot be verified.
171 bool MayHaveUnverifiableGraph() const { return outer_zone_ == nullptr; }
172
173 Zone* graph_zone() const { return graph_zone_; }
174 Graph* graph() const { return graph_; }
Ben Murdochc5610432016-08-08 18:44:38 +0100175 SourcePositionTable* source_positions() const { return source_positions_; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400176 MachineOperatorBuilder* machine() const { return machine_; }
177 CommonOperatorBuilder* common() const { return common_; }
178 JSOperatorBuilder* javascript() const { return javascript_; }
179 JSGraph* jsgraph() const { return jsgraph_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000180 MaybeHandle<Context> native_context() const {
181 if (info()->is_native_context_specializing()) {
182 return handle(info()->native_context(), isolate());
183 }
184 return MaybeHandle<Context>();
185 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400186
187 LoopAssignmentAnalysis* loop_assignment() const { return loop_assignment_; }
188 void set_loop_assignment(LoopAssignmentAnalysis* loop_assignment) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000189 DCHECK(!loop_assignment_);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400190 loop_assignment_ = loop_assignment;
191 }
192
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000193 TypeHintAnalysis* type_hint_analysis() const { return type_hint_analysis_; }
194 void set_type_hint_analysis(TypeHintAnalysis* type_hint_analysis) {
195 DCHECK_NULL(type_hint_analysis_);
196 type_hint_analysis_ = type_hint_analysis;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400197 }
198
199 Schedule* schedule() const { return schedule_; }
200 void set_schedule(Schedule* schedule) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000201 DCHECK(!schedule_);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400202 schedule_ = schedule;
203 }
Ben Murdochc5610432016-08-08 18:44:38 +0100204 void reset_schedule() { schedule_ = nullptr; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400205
206 Zone* instruction_zone() const { return instruction_zone_; }
207 InstructionSequence* sequence() const { return sequence_; }
208 Frame* frame() const { return frame_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000209
210 Zone* register_allocation_zone() const { return register_allocation_zone_; }
211 RegisterAllocationData* register_allocation_data() const {
212 return register_allocation_data_;
213 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400214
Ben Murdochc5610432016-08-08 18:44:38 +0100215 BasicBlockProfiler::Data* profiler_data() const { return profiler_data_; }
216 void set_profiler_data(BasicBlockProfiler::Data* profiler_data) {
217 profiler_data_ = profiler_data;
218 }
219
220 std::string const& source_position_output() const {
221 return source_position_output_;
222 }
223 void set_source_position_output(std::string const& source_position_output) {
224 source_position_output_ = source_position_output;
225 }
226
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400227 void DeleteGraphZone() {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400228 if (graph_zone_ == nullptr) return;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400229 graph_zone_scope_.Destroy();
230 graph_zone_ = nullptr;
231 graph_ = nullptr;
Ben Murdochc5610432016-08-08 18:44:38 +0100232 source_positions_ = nullptr;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400233 loop_assignment_ = nullptr;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000234 type_hint_analysis_ = nullptr;
235 simplified_ = nullptr;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400236 machine_ = nullptr;
237 common_ = nullptr;
238 javascript_ = nullptr;
239 jsgraph_ = nullptr;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400240 schedule_ = nullptr;
241 }
242
243 void DeleteInstructionZone() {
244 if (instruction_zone_ == nullptr) return;
245 instruction_zone_scope_.Destroy();
246 instruction_zone_ = nullptr;
247 sequence_ = nullptr;
248 frame_ = nullptr;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000249 }
250
251 void DeleteRegisterAllocationZone() {
252 if (register_allocation_zone_ == nullptr) return;
253 register_allocation_zone_scope_.Destroy();
254 register_allocation_zone_ = nullptr;
255 register_allocation_data_ = nullptr;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400256 }
257
Ben Murdochda12d292016-06-02 14:46:10 +0100258 void InitializeInstructionSequence(const CallDescriptor* descriptor) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000259 DCHECK(sequence_ == nullptr);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400260 InstructionBlocks* instruction_blocks =
261 InstructionSequence::InstructionBlocksFor(instruction_zone(),
262 schedule());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000263 sequence_ = new (instruction_zone()) InstructionSequence(
264 info()->isolate(), instruction_zone(), instruction_blocks);
Ben Murdochda12d292016-06-02 14:46:10 +0100265 if (descriptor && descriptor->RequiresFrameAsIncoming()) {
266 sequence_->instruction_blocks()[0]->mark_needs_frame();
267 } else {
268 DCHECK_EQ(0, descriptor->CalleeSavedFPRegisters());
269 DCHECK_EQ(0, descriptor->CalleeSavedRegisters());
270 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400271 }
272
Ben Murdoch097c5b22016-05-18 11:27:45 +0100273 void InitializeFrameData(CallDescriptor* descriptor) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000274 DCHECK(frame_ == nullptr);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000275 int fixed_frame_size = 0;
276 if (descriptor != nullptr) {
Ben Murdochda12d292016-06-02 14:46:10 +0100277 fixed_frame_size = CalculateFixedFrameSize(descriptor);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000278 }
Ben Murdochc5610432016-08-08 18:44:38 +0100279 frame_ = new (instruction_zone()) Frame(fixed_frame_size);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100280 }
281
282 void InitializeRegisterAllocationData(const RegisterConfiguration* config,
Ben Murdochc5610432016-08-08 18:44:38 +0100283 CallDescriptor* descriptor) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100284 DCHECK(register_allocation_data_ == nullptr);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000285 register_allocation_data_ = new (register_allocation_zone())
286 RegisterAllocationData(config, register_allocation_zone(), frame(),
Ben Murdochc5610432016-08-08 18:44:38 +0100287 sequence(), debug_name_.get());
288 }
289
290 void BeginPhaseKind(const char* phase_kind_name) {
291 if (pipeline_statistics() != nullptr) {
292 pipeline_statistics()->BeginPhaseKind(phase_kind_name);
293 }
294 }
295
296 void EndPhaseKind() {
297 if (pipeline_statistics() != nullptr) {
298 pipeline_statistics()->EndPhaseKind();
299 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000300 }
301
302 private:
Ben Murdochc5610432016-08-08 18:44:38 +0100303 Isolate* const isolate_;
304 CompilationInfo* const info_;
305 base::SmartArrayPointer<char> debug_name_;
306 Zone* outer_zone_ = nullptr;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400307 ZonePool* const zone_pool_;
Ben Murdochc5610432016-08-08 18:44:38 +0100308 PipelineStatistics* pipeline_statistics_ = nullptr;
309 bool compilation_failed_ = false;
310 Handle<Code> code_ = Handle<Code>::null();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400311
312 // All objects in the following group of fields are allocated in graph_zone_.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000313 // They are all set to nullptr when the graph_zone_ is destroyed.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400314 ZonePool::Scope graph_zone_scope_;
Ben Murdochc5610432016-08-08 18:44:38 +0100315 Zone* graph_zone_ = nullptr;
316 Graph* graph_ = nullptr;
317 SourcePositionTable* source_positions_ = nullptr;
318 LoopAssignmentAnalysis* loop_assignment_ = nullptr;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000319 TypeHintAnalysis* type_hint_analysis_ = nullptr;
Ben Murdochc5610432016-08-08 18:44:38 +0100320 SimplifiedOperatorBuilder* simplified_ = nullptr;
321 MachineOperatorBuilder* machine_ = nullptr;
322 CommonOperatorBuilder* common_ = nullptr;
323 JSOperatorBuilder* javascript_ = nullptr;
324 JSGraph* jsgraph_ = nullptr;
325 Schedule* schedule_ = nullptr;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400326
327 // All objects in the following group of fields are allocated in
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000328 // instruction_zone_. They are all set to nullptr when the instruction_zone_
329 // is
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400330 // destroyed.
331 ZonePool::Scope instruction_zone_scope_;
332 Zone* instruction_zone_;
Ben Murdochc5610432016-08-08 18:44:38 +0100333 InstructionSequence* sequence_ = nullptr;
334 Frame* frame_ = nullptr;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000335
336 // All objects in the following group of fields are allocated in
337 // register_allocation_zone_. They are all set to nullptr when the zone is
338 // destroyed.
339 ZonePool::Scope register_allocation_zone_scope_;
340 Zone* register_allocation_zone_;
Ben Murdochc5610432016-08-08 18:44:38 +0100341 RegisterAllocationData* register_allocation_data_ = nullptr;
342
343 // Basic block profiling support.
344 BasicBlockProfiler::Data* profiler_data_ = nullptr;
345
346 // Source position output for --trace-turbo.
347 std::string source_position_output_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400348
Ben Murdochda12d292016-06-02 14:46:10 +0100349 int CalculateFixedFrameSize(CallDescriptor* descriptor) {
350 if (descriptor->IsJSFunctionCall()) {
351 return StandardFrameConstants::kFixedSlotCount;
352 }
353 return descriptor->IsCFunctionCall()
354 ? (CommonFrameConstants::kFixedSlotCountAboveFp +
355 CommonFrameConstants::kCPSlotCount)
356 : TypedFrameConstants::kFixedSlotCount;
357 }
358
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400359 DISALLOW_COPY_AND_ASSIGN(PipelineData);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000360};
361
Ben Murdochc5610432016-08-08 18:44:38 +0100362class PipelineImpl final {
363 public:
364 explicit PipelineImpl(PipelineData* data) : data_(data) {}
365
366 // Helpers for executing pipeline phases.
367 template <typename Phase>
368 void Run();
369 template <typename Phase, typename Arg0>
370 void Run(Arg0 arg_0);
371 template <typename Phase, typename Arg0, typename Arg1>
372 void Run(Arg0 arg_0, Arg1 arg_1);
373
374 // Run the graph creation and initial optimization passes.
375 bool CreateGraph();
376
377 // Run the concurrent optimization passes.
378 bool OptimizeGraph(Linkage* linkage);
379
380 // Perform the actual code generation and return handle to a code object.
381 Handle<Code> GenerateCode(Linkage* linkage);
382
383 bool ScheduleAndSelectInstructions(Linkage* linkage);
384 void RunPrintAndVerify(const char* phase, bool untyped = false);
385 Handle<Code> ScheduleAndGenerateCode(CallDescriptor* call_descriptor);
386 void AllocateRegisters(const RegisterConfiguration* config,
387 CallDescriptor* descriptor, bool run_verifier);
388
389 CompilationInfo* info() const;
390 Isolate* isolate() const;
391
392 PipelineData* const data_;
393};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000394
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000395namespace {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000396
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400397struct TurboCfgFile : public std::ofstream {
398 explicit TurboCfgFile(Isolate* isolate)
399 : std::ofstream(isolate->GetTurboCfgFileName().c_str(),
400 std::ios_base::app) {}
401};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000402
Ben Murdochc5610432016-08-08 18:44:38 +0100403struct TurboJsonFile : public std::ofstream {
404 TurboJsonFile(CompilationInfo* info, std::ios_base::openmode mode)
405 : std::ofstream(GetVisualizerLogFileName(info, nullptr, "json").get(),
406 mode) {}
407};
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400408
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000409void TraceSchedule(CompilationInfo* info, Schedule* schedule) {
410 if (FLAG_trace_turbo) {
Ben Murdochc5610432016-08-08 18:44:38 +0100411 AllowHandleDereference allow_deref;
412 TurboJsonFile json_of(info, std::ios_base::app);
413 json_of << "{\"name\":\"Schedule\",\"type\":\"schedule\",\"data\":\"";
414 std::stringstream schedule_stream;
415 schedule_stream << *schedule;
416 std::string schedule_string(schedule_stream.str());
417 for (const auto& c : schedule_string) {
418 json_of << AsEscapedUC16ForJSON(c);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000419 }
Ben Murdochc5610432016-08-08 18:44:38 +0100420 json_of << "\"},\n";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000421 }
Ben Murdochc5610432016-08-08 18:44:38 +0100422 if (FLAG_trace_turbo_graph || FLAG_trace_turbo_scheduler) {
423 AllowHandleDereference allow_deref;
424 OFStream os(stdout);
425 os << "-- Schedule --------------------------------------\n" << *schedule;
426 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400427}
428
429
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000430class AstGraphBuilderWithPositions final : public AstGraphBuilder {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000431 public:
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400432 AstGraphBuilderWithPositions(Zone* local_zone, CompilationInfo* info,
433 JSGraph* jsgraph,
434 LoopAssignmentAnalysis* loop_assignment,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000435 TypeHintAnalysis* type_hint_analysis,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400436 SourcePositionTable* source_positions)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000437 : AstGraphBuilder(local_zone, info, jsgraph, loop_assignment,
438 type_hint_analysis),
439 source_positions_(source_positions),
440 start_position_(info->shared_info()->start_position()) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000441
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000442 bool CreateGraph(bool stack_check) {
443 SourcePositionTable::Scope pos_scope(source_positions_, start_position_);
444 return AstGraphBuilder::CreateGraph(stack_check);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000445 }
446
447#define DEF_VISIT(type) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000448 void Visit##type(type* node) override { \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000449 SourcePositionTable::Scope pos(source_positions_, \
450 SourcePosition(node->position())); \
451 AstGraphBuilder::Visit##type(node); \
452 }
453 AST_NODE_LIST(DEF_VISIT)
454#undef DEF_VISIT
455
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000456 private:
457 SourcePositionTable* const source_positions_;
458 SourcePosition const start_position_;
459};
460
461
462class SourcePositionWrapper final : public Reducer {
463 public:
464 SourcePositionWrapper(Reducer* reducer, SourcePositionTable* table)
465 : reducer_(reducer), table_(table) {}
466 ~SourcePositionWrapper() final {}
467
468 Reduction Reduce(Node* node) final {
469 SourcePosition const pos = table_->GetSourcePosition(node);
470 SourcePositionTable::Scope position(table_, pos);
471 return reducer_->Reduce(node);
472 }
473
474 void Finalize() final { reducer_->Finalize(); }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400475
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000476 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000477 Reducer* const reducer_;
478 SourcePositionTable* const table_;
479
480 DISALLOW_COPY_AND_ASSIGN(SourcePositionWrapper);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000481};
482
483
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000484class JSGraphReducer final : public GraphReducer {
485 public:
486 JSGraphReducer(JSGraph* jsgraph, Zone* zone)
487 : GraphReducer(zone, jsgraph->graph(), jsgraph->Dead()) {}
488 ~JSGraphReducer() final {}
489};
490
491
492void AddReducer(PipelineData* data, GraphReducer* graph_reducer,
493 Reducer* reducer) {
494 if (data->info()->is_source_positions_enabled()) {
495 void* const buffer = data->graph_zone()->New(sizeof(SourcePositionWrapper));
496 SourcePositionWrapper* const wrapper =
497 new (buffer) SourcePositionWrapper(reducer, data->source_positions());
498 graph_reducer->AddReducer(wrapper);
499 } else {
500 graph_reducer->AddReducer(reducer);
501 }
502}
503
504
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400505class PipelineRunScope {
506 public:
507 PipelineRunScope(PipelineData* data, const char* phase_name)
508 : phase_scope_(
509 phase_name == nullptr ? nullptr : data->pipeline_statistics(),
510 phase_name),
511 zone_scope_(data->zone_pool()) {}
512
513 Zone* zone() { return zone_scope_.zone(); }
514
515 private:
516 PhaseScope phase_scope_;
517 ZonePool::Scope zone_scope_;
518};
519
Ben Murdochc5610432016-08-08 18:44:38 +0100520PipelineStatistics* CreatePipelineStatistics(CompilationInfo* info,
521 ZonePool* zone_pool) {
522 PipelineStatistics* pipeline_statistics = nullptr;
523
524 if (FLAG_turbo_stats) {
525 pipeline_statistics = new PipelineStatistics(info, zone_pool);
526 pipeline_statistics->BeginPhaseKind("initializing");
527 }
528
529 if (FLAG_trace_turbo) {
530 TurboJsonFile json_of(info, std::ios_base::trunc);
531 Handle<Script> script = info->script();
532 base::SmartArrayPointer<char> function_name = info->GetDebugName();
533 int pos = info->shared_info()->start_position();
534 json_of << "{\"function\":\"" << function_name.get()
535 << "\", \"sourcePosition\":" << pos << ", \"source\":\"";
536 if (!script->IsUndefined() && !script->source()->IsUndefined()) {
537 DisallowHeapAllocation no_allocation;
538 int start = info->shared_info()->start_position();
539 int len = info->shared_info()->end_position() - start;
540 String::SubStringRange source(String::cast(script->source()), start, len);
541 for (const auto& c : source) {
542 json_of << AsEscapedUC16ForJSON(c);
543 }
544 }
545 json_of << "\",\n\"phases\":[";
546 }
547
548 return pipeline_statistics;
549}
550
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000551} // namespace
552
Ben Murdochc5610432016-08-08 18:44:38 +0100553class PipelineCompilationJob final : public CompilationJob {
554 public:
555 PipelineCompilationJob(Isolate* isolate, Handle<JSFunction> function)
556 // Note that the CompilationInfo is not initialized at the time we pass it
557 // to the CompilationJob constructor, but it is not dereferenced there.
558 : CompilationJob(&info_, "TurboFan"),
559 zone_(isolate->allocator()),
560 zone_pool_(isolate->allocator()),
561 parse_info_(&zone_, function),
562 info_(&parse_info_, function),
563 pipeline_statistics_(CreatePipelineStatistics(info(), &zone_pool_)),
564 data_(&zone_pool_, info(), pipeline_statistics_.get()),
565 pipeline_(&data_),
566 linkage_(nullptr) {}
567
568 protected:
569 Status CreateGraphImpl() final;
570 Status OptimizeGraphImpl() final;
571 Status GenerateCodeImpl() final;
572
573 private:
574 Zone zone_;
575 ZonePool zone_pool_;
576 ParseInfo parse_info_;
577 CompilationInfo info_;
578 base::SmartPointer<PipelineStatistics> pipeline_statistics_;
579 PipelineData data_;
580 PipelineImpl pipeline_;
581 Linkage* linkage_;
582};
583
584PipelineCompilationJob::Status PipelineCompilationJob::CreateGraphImpl() {
585 if (info()->shared_info()->asm_function()) {
586 if (info()->osr_frame()) info()->MarkAsFrameSpecializing();
587 info()->MarkAsFunctionContextSpecializing();
588 } else {
589 if (!FLAG_always_opt) {
590 info()->MarkAsBailoutOnUninitialized();
591 }
592 if (FLAG_native_context_specialization) {
593 info()->MarkAsNativeContextSpecializing();
594 }
595 }
596 if (!info()->shared_info()->asm_function() || FLAG_turbo_asm_deoptimization) {
597 info()->MarkAsDeoptimizationEnabled();
598 }
599 if (!info()->is_optimizing_from_bytecode()) {
600 if (!Compiler::EnsureDeoptimizationSupport(info())) return FAILED;
601 }
602
603 linkage_ = new (&zone_) Linkage(Linkage::ComputeIncoming(&zone_, info()));
604
605 if (!pipeline_.CreateGraph()) {
606 if (isolate()->has_pending_exception()) return FAILED; // Stack overflowed.
607 return AbortOptimization(kGraphBuildingFailed);
608 }
609
610 return SUCCEEDED;
611}
612
613PipelineCompilationJob::Status PipelineCompilationJob::OptimizeGraphImpl() {
614 if (!pipeline_.OptimizeGraph(linkage_)) return FAILED;
615 return SUCCEEDED;
616}
617
618PipelineCompilationJob::Status PipelineCompilationJob::GenerateCodeImpl() {
619 Handle<Code> code = pipeline_.GenerateCode(linkage_);
620 if (code.is_null()) {
621 if (info()->bailout_reason() == kNoReason) {
622 return AbortOptimization(kCodeGenerationFailed);
623 }
624 return FAILED;
625 }
626 info()->dependencies()->Commit(code);
627 info()->SetCode(code);
628 if (info()->is_deoptimization_enabled()) {
629 info()->context()->native_context()->AddOptimizedCode(*code);
630 RegisterWeakObjectsInOptimizedCode(code);
631 }
632 return SUCCEEDED;
633}
634
635class PipelineWasmCompilationJob final : public CompilationJob {
636 public:
637 explicit PipelineWasmCompilationJob(CompilationInfo* info, Graph* graph,
638 CallDescriptor* descriptor,
639 SourcePositionTable* source_positions)
640 : CompilationJob(info, "TurboFan"),
641 zone_pool_(info->isolate()->allocator()),
642 data_(&zone_pool_, info, graph, source_positions),
643 pipeline_(&data_),
644 linkage_(descriptor) {}
645
646 protected:
647 Status CreateGraphImpl() final;
648 Status OptimizeGraphImpl() final;
649 Status GenerateCodeImpl() final;
650
651 private:
652 ZonePool zone_pool_;
653 PipelineData data_;
654 PipelineImpl pipeline_;
655 Linkage linkage_;
656};
657
658PipelineWasmCompilationJob::Status
659PipelineWasmCompilationJob::CreateGraphImpl() {
660 return SUCCEEDED;
661}
662
663PipelineWasmCompilationJob::Status
664PipelineWasmCompilationJob::OptimizeGraphImpl() {
665 if (FLAG_trace_turbo) {
666 TurboJsonFile json_of(info(), std::ios_base::trunc);
667 json_of << "{\"function\":\"" << info()->GetDebugName().get()
668 << "\", \"source\":\"\",\n\"phases\":[";
669 }
670
671 pipeline_.RunPrintAndVerify("Machine", true);
672
673 if (!pipeline_.ScheduleAndSelectInstructions(&linkage_)) return FAILED;
674 return SUCCEEDED;
675}
676
677PipelineWasmCompilationJob::Status
678PipelineWasmCompilationJob::GenerateCodeImpl() {
679 pipeline_.GenerateCode(&linkage_);
680 return SUCCEEDED;
681}
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400682
683template <typename Phase>
Ben Murdochc5610432016-08-08 18:44:38 +0100684void PipelineImpl::Run() {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400685 PipelineRunScope scope(this->data_, Phase::phase_name());
686 Phase phase;
687 phase.Run(this->data_, scope.zone());
688}
689
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400690template <typename Phase, typename Arg0>
Ben Murdochc5610432016-08-08 18:44:38 +0100691void PipelineImpl::Run(Arg0 arg_0) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400692 PipelineRunScope scope(this->data_, Phase::phase_name());
693 Phase phase;
694 phase.Run(this->data_, scope.zone(), arg_0);
695}
696
Ben Murdochc5610432016-08-08 18:44:38 +0100697template <typename Phase, typename Arg0, typename Arg1>
698void PipelineImpl::Run(Arg0 arg_0, Arg1 arg_1) {
699 PipelineRunScope scope(this->data_, Phase::phase_name());
700 Phase phase;
701 phase.Run(this->data_, scope.zone(), arg_0, arg_1);
702}
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400703
704struct LoopAssignmentAnalysisPhase {
705 static const char* phase_name() { return "loop assignment analysis"; }
706
707 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdochc5610432016-08-08 18:44:38 +0100708 if (!data->info()->is_optimizing_from_bytecode()) {
709 AstLoopAssignmentAnalyzer analyzer(data->graph_zone(), data->info());
710 LoopAssignmentAnalysis* loop_assignment = analyzer.Analyze();
711 data->set_loop_assignment(loop_assignment);
712 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400713 }
714};
715
716
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000717struct TypeHintAnalysisPhase {
718 static const char* phase_name() { return "type hint analysis"; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400719
720 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdochc5610432016-08-08 18:44:38 +0100721 if (!data->info()->is_optimizing_from_bytecode()) {
722 TypeHintAnalyzer analyzer(data->graph_zone());
723 Handle<Code> code(data->info()->shared_info()->code(), data->isolate());
724 TypeHintAnalysis* type_hint_analysis = analyzer.Analyze(code);
725 data->set_type_hint_analysis(type_hint_analysis);
726 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400727 }
728};
729
730
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000731struct GraphBuilderPhase {
732 static const char* phase_name() { return "graph builder"; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400733
734 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000735 bool stack_check = !data->info()->IsStub();
736 bool succeeded = false;
737
Ben Murdochc5610432016-08-08 18:44:38 +0100738 if (data->info()->is_optimizing_from_bytecode()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000739 BytecodeGraphBuilder graph_builder(temp_zone, data->info(),
740 data->jsgraph());
Ben Murdoch097c5b22016-05-18 11:27:45 +0100741 succeeded = graph_builder.CreateGraph();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000742 } else {
743 AstGraphBuilderWithPositions graph_builder(
744 temp_zone, data->info(), data->jsgraph(), data->loop_assignment(),
745 data->type_hint_analysis(), data->source_positions());
746 succeeded = graph_builder.CreateGraph(stack_check);
747 }
748
749 if (!succeeded) {
750 data->set_compilation_failed();
751 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400752 }
753};
754
755
756struct InliningPhase {
757 static const char* phase_name() { return "inlining"; }
758
759 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000760 JSGraphReducer graph_reducer(data->jsgraph(), temp_zone);
761 DeadCodeElimination dead_code_elimination(&graph_reducer, data->graph(),
762 data->common());
763 CommonOperatorReducer common_reducer(&graph_reducer, data->graph(),
764 data->common(), data->machine());
Ben Murdochda12d292016-06-02 14:46:10 +0100765 JSCallReducer call_reducer(data->jsgraph(),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000766 data->info()->is_deoptimization_enabled()
767 ? JSCallReducer::kDeoptimizationEnabled
768 : JSCallReducer::kNoFlags,
769 data->native_context());
770 JSContextSpecialization context_specialization(
771 &graph_reducer, data->jsgraph(),
772 data->info()->is_function_context_specializing()
773 ? data->info()->context()
774 : MaybeHandle<Context>());
775 JSFrameSpecialization frame_specialization(data->info()->osr_frame(),
776 data->jsgraph());
777 JSGlobalObjectSpecialization global_object_specialization(
Ben Murdoch097c5b22016-05-18 11:27:45 +0100778 &graph_reducer, data->jsgraph(), data->native_context(),
779 data->info()->dependencies());
780 JSNativeContextSpecialization::Flags flags =
781 JSNativeContextSpecialization::kNoFlags;
782 if (data->info()->is_bailout_on_uninitialized()) {
783 flags |= JSNativeContextSpecialization::kBailoutOnUninitialized;
784 }
785 if (data->info()->is_deoptimization_enabled()) {
786 flags |= JSNativeContextSpecialization::kDeoptimizationEnabled;
787 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000788 JSNativeContextSpecialization native_context_specialization(
Ben Murdoch097c5b22016-05-18 11:27:45 +0100789 &graph_reducer, data->jsgraph(), flags, data->native_context(),
790 data->info()->dependencies(), temp_zone);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000791 JSInliningHeuristic inlining(&graph_reducer,
792 data->info()->is_inlining_enabled()
793 ? JSInliningHeuristic::kGeneralInlining
794 : JSInliningHeuristic::kRestrictedInlining,
795 temp_zone, data->info(), data->jsgraph());
796 AddReducer(data, &graph_reducer, &dead_code_elimination);
797 AddReducer(data, &graph_reducer, &common_reducer);
798 if (data->info()->is_frame_specializing()) {
799 AddReducer(data, &graph_reducer, &frame_specialization);
800 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100801 if (data->info()->is_deoptimization_enabled()) {
802 AddReducer(data, &graph_reducer, &global_object_specialization);
803 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000804 AddReducer(data, &graph_reducer, &native_context_specialization);
805 AddReducer(data, &graph_reducer, &context_specialization);
806 AddReducer(data, &graph_reducer, &call_reducer);
807 AddReducer(data, &graph_reducer, &inlining);
808 graph_reducer.ReduceGraph();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400809 }
810};
811
812
813struct TyperPhase {
814 static const char* phase_name() { return "typer"; }
815
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000816 void Run(PipelineData* data, Zone* temp_zone, Typer* typer) {
817 NodeVector roots(temp_zone);
818 data->jsgraph()->GetCachedNodes(&roots);
819 typer->Run(roots);
820 }
821};
822
Ben Murdochc5610432016-08-08 18:44:38 +0100823#ifdef DEBUG
824
825struct UntyperPhase {
826 static const char* phase_name() { return "untyper"; }
827
828 void Run(PipelineData* data, Zone* temp_zone) {
829 class RemoveTypeReducer final : public Reducer {
830 public:
831 Reduction Reduce(Node* node) final {
832 if (NodeProperties::IsTyped(node)) {
833 NodeProperties::RemoveType(node);
834 return Changed(node);
835 }
836 return NoChange();
837 }
838 };
839
840 JSGraphReducer graph_reducer(data->jsgraph(), temp_zone);
841 RemoveTypeReducer remove_type_reducer;
842 AddReducer(data, &graph_reducer, &remove_type_reducer);
843 graph_reducer.ReduceGraph();
844 }
845};
846
847#endif // DEBUG
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000848
849struct OsrDeconstructionPhase {
850 static const char* phase_name() { return "OSR deconstruction"; }
851
852 void Run(PipelineData* data, Zone* temp_zone) {
853 OsrHelper osr_helper(data->info());
854 osr_helper.Deconstruct(data->jsgraph(), data->common(), temp_zone);
855 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400856};
857
858
859struct TypedLoweringPhase {
860 static const char* phase_name() { return "typed lowering"; }
861
862 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000863 JSGraphReducer graph_reducer(data->jsgraph(), temp_zone);
864 DeadCodeElimination dead_code_elimination(&graph_reducer, data->graph(),
865 data->common());
Ben Murdochda12d292016-06-02 14:46:10 +0100866 LoadElimination load_elimination(&graph_reducer, data->graph(),
Ben Murdochc5610432016-08-08 18:44:38 +0100867 data->jsgraph()->simplified());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000868 JSBuiltinReducer builtin_reducer(&graph_reducer, data->jsgraph());
Ben Murdoch097c5b22016-05-18 11:27:45 +0100869 MaybeHandle<LiteralsArray> literals_array =
870 data->info()->is_native_context_specializing()
871 ? handle(data->info()->closure()->literals(), data->isolate())
872 : MaybeHandle<LiteralsArray>();
873 JSCreateLowering create_lowering(
874 &graph_reducer, data->info()->dependencies(), data->jsgraph(),
875 literals_array, temp_zone);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000876 JSTypedLowering::Flags typed_lowering_flags = JSTypedLowering::kNoFlags;
877 if (data->info()->is_deoptimization_enabled()) {
878 typed_lowering_flags |= JSTypedLowering::kDeoptimizationEnabled;
879 }
880 if (data->info()->shared_info()->HasBytecodeArray()) {
881 typed_lowering_flags |= JSTypedLowering::kDisableBinaryOpReduction;
882 }
883 JSTypedLowering typed_lowering(&graph_reducer, data->info()->dependencies(),
884 typed_lowering_flags, data->jsgraph(),
885 temp_zone);
886 JSIntrinsicLowering intrinsic_lowering(
887 &graph_reducer, data->jsgraph(),
888 data->info()->is_deoptimization_enabled()
889 ? JSIntrinsicLowering::kDeoptimizationEnabled
890 : JSIntrinsicLowering::kDeoptimizationDisabled);
Ben Murdochda12d292016-06-02 14:46:10 +0100891 SimplifiedOperatorReducer simple_reducer(data->jsgraph());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000892 CommonOperatorReducer common_reducer(&graph_reducer, data->graph(),
893 data->common(), data->machine());
894 AddReducer(data, &graph_reducer, &dead_code_elimination);
895 AddReducer(data, &graph_reducer, &builtin_reducer);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100896 if (data->info()->is_deoptimization_enabled()) {
897 AddReducer(data, &graph_reducer, &create_lowering);
898 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000899 AddReducer(data, &graph_reducer, &typed_lowering);
900 AddReducer(data, &graph_reducer, &intrinsic_lowering);
901 AddReducer(data, &graph_reducer, &load_elimination);
Ben Murdochda12d292016-06-02 14:46:10 +0100902 AddReducer(data, &graph_reducer, &simple_reducer);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000903 AddReducer(data, &graph_reducer, &common_reducer);
904 graph_reducer.ReduceGraph();
905 }
906};
907
908
909struct BranchEliminationPhase {
910 static const char* phase_name() { return "branch condition elimination"; }
911
912 void Run(PipelineData* data, Zone* temp_zone) {
913 JSGraphReducer graph_reducer(data->jsgraph(), temp_zone);
914 BranchElimination branch_condition_elimination(&graph_reducer,
915 data->jsgraph(), temp_zone);
916 DeadCodeElimination dead_code_elimination(&graph_reducer, data->graph(),
917 data->common());
918 AddReducer(data, &graph_reducer, &branch_condition_elimination);
919 AddReducer(data, &graph_reducer, &dead_code_elimination);
920 graph_reducer.ReduceGraph();
921 }
922};
923
924
925struct EscapeAnalysisPhase {
926 static const char* phase_name() { return "escape analysis"; }
927
928 void Run(PipelineData* data, Zone* temp_zone) {
929 EscapeAnalysis escape_analysis(data->graph(), data->jsgraph()->common(),
930 temp_zone);
931 escape_analysis.Run();
932 JSGraphReducer graph_reducer(data->jsgraph(), temp_zone);
933 EscapeAnalysisReducer escape_reducer(&graph_reducer, data->jsgraph(),
934 &escape_analysis, temp_zone);
935 AddReducer(data, &graph_reducer, &escape_reducer);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400936 graph_reducer.ReduceGraph();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100937 escape_reducer.VerifyReplacement();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400938 }
939};
940
Ben Murdochc5610432016-08-08 18:44:38 +0100941struct RepresentationSelectionPhase {
942 static const char* phase_name() { return "representation selection"; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400943
944 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000945 SimplifiedLowering lowering(data->jsgraph(), temp_zone,
946 data->source_positions());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400947 lowering.LowerAllNodes();
Ben Murdochc5610432016-08-08 18:44:38 +0100948 }
949};
Ben Murdoch097c5b22016-05-18 11:27:45 +0100950
Ben Murdochc5610432016-08-08 18:44:38 +0100951struct EarlyOptimizationPhase {
952 static const char* phase_name() { return "early optimization"; }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100953
Ben Murdochc5610432016-08-08 18:44:38 +0100954 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000955 JSGraphReducer graph_reducer(data->jsgraph(), temp_zone);
Ben Murdochc5610432016-08-08 18:44:38 +0100956 JSGenericLowering generic_lowering(data->jsgraph());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000957 DeadCodeElimination dead_code_elimination(&graph_reducer, data->graph(),
958 data->common());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400959 SimplifiedOperatorReducer simple_reducer(data->jsgraph());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000960 ValueNumberingReducer value_numbering(temp_zone);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400961 MachineOperatorReducer machine_reducer(data->jsgraph());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000962 CommonOperatorReducer common_reducer(&graph_reducer, data->graph(),
963 data->common(), data->machine());
964 AddReducer(data, &graph_reducer, &dead_code_elimination);
965 AddReducer(data, &graph_reducer, &simple_reducer);
Ben Murdochc5610432016-08-08 18:44:38 +0100966 AddReducer(data, &graph_reducer, &generic_lowering);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000967 AddReducer(data, &graph_reducer, &value_numbering);
968 AddReducer(data, &graph_reducer, &machine_reducer);
969 AddReducer(data, &graph_reducer, &common_reducer);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400970 graph_reducer.ReduceGraph();
971 }
972};
973
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000974struct ControlFlowOptimizationPhase {
975 static const char* phase_name() { return "control flow optimization"; }
976
977 void Run(PipelineData* data, Zone* temp_zone) {
978 ControlFlowOptimizer optimizer(data->graph(), data->common(),
979 data->machine(), temp_zone);
980 optimizer.Optimize();
981 }
982};
983
Ben Murdochc5610432016-08-08 18:44:38 +0100984struct EffectControlLinearizationPhase {
985 static const char* phase_name() { return "effect linearization"; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000986
Ben Murdochc5610432016-08-08 18:44:38 +0100987 void Run(PipelineData* data, Zone* temp_zone) {
988 // The scheduler requires the graphs to be trimmed, so trim now.
989 // TODO(jarin) Remove the trimming once the scheduler can handle untrimmed
990 // graphs.
991 GraphTrimmer trimmer(temp_zone, data->graph());
992 NodeVector roots(temp_zone);
993 data->jsgraph()->GetCachedNodes(&roots);
994 trimmer.TrimGraph(roots.begin(), roots.end());
995
996 // Schedule the graph without node splitting so that we can
997 // fix the effect and control flow for nodes with low-level side
998 // effects (such as changing representation to tagged or
999 // 'floating' allocation regions.)
1000 Schedule* schedule = Scheduler::ComputeSchedule(temp_zone, data->graph(),
1001 Scheduler::kNoFlags);
1002 if (FLAG_turbo_verify) ScheduleVerifier::Run(schedule);
1003 TraceSchedule(data->info(), schedule);
1004
1005 // Post-pass for wiring the control/effects
1006 // - connect allocating representation changes into the control&effect
1007 // chains and lower them,
1008 // - get rid of the region markers,
1009 // - introduce effect phis and rewire effects to get SSA again.
1010 EffectControlLinearizer linearizer(data->jsgraph(), schedule, temp_zone);
1011 linearizer.Run();
1012 }
1013};
1014
1015struct MemoryOptimizationPhase {
1016 static const char* phase_name() { return "memory optimization"; }
1017
1018 void Run(PipelineData* data, Zone* temp_zone) {
1019 MemoryOptimizer optimizer(data->jsgraph(), temp_zone);
1020 optimizer.Optimize();
1021 }
1022};
1023
1024struct LateOptimizationPhase {
1025 static const char* phase_name() { return "late optimization"; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001026
1027 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001028 JSGraphReducer graph_reducer(data->jsgraph(), temp_zone);
1029 DeadCodeElimination dead_code_elimination(&graph_reducer, data->graph(),
1030 data->common());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001031 ValueNumberingReducer value_numbering(temp_zone);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001032 MachineOperatorReducer machine_reducer(data->jsgraph());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001033 CommonOperatorReducer common_reducer(&graph_reducer, data->graph(),
1034 data->common(), data->machine());
Ben Murdochc5610432016-08-08 18:44:38 +01001035 SelectLowering select_lowering(data->jsgraph()->graph(),
1036 data->jsgraph()->common());
1037 TailCallOptimization tco(data->common(), data->graph());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001038 AddReducer(data, &graph_reducer, &dead_code_elimination);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001039 AddReducer(data, &graph_reducer, &value_numbering);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001040 AddReducer(data, &graph_reducer, &machine_reducer);
1041 AddReducer(data, &graph_reducer, &common_reducer);
Ben Murdochc5610432016-08-08 18:44:38 +01001042 AddReducer(data, &graph_reducer, &select_lowering);
1043 AddReducer(data, &graph_reducer, &tco);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001044 graph_reducer.ReduceGraph();
1045 }
1046};
1047
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001048struct EarlyGraphTrimmingPhase {
1049 static const char* phase_name() { return "early graph trimming"; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001050 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001051 GraphTrimmer trimmer(temp_zone, data->graph());
1052 NodeVector roots(temp_zone);
1053 data->jsgraph()->GetCachedNodes(&roots);
1054 trimmer.TrimGraph(roots.begin(), roots.end());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001055 }
1056};
1057
1058
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001059struct LateGraphTrimmingPhase {
1060 static const char* phase_name() { return "late graph trimming"; }
1061 void Run(PipelineData* data, Zone* temp_zone) {
1062 GraphTrimmer trimmer(temp_zone, data->graph());
1063 NodeVector roots(temp_zone);
1064 data->jsgraph()->GetCachedNodes(&roots);
1065 trimmer.TrimGraph(roots.begin(), roots.end());
1066 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001067};
1068
1069
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001070struct StressLoopPeelingPhase {
1071 static const char* phase_name() { return "stress loop peeling"; }
1072
1073 void Run(PipelineData* data, Zone* temp_zone) {
1074 // Peel the first outer loop for testing.
1075 // TODO(titzer): peel all loops? the N'th loop? Innermost loops?
1076 LoopTree* loop_tree = LoopFinder::BuildLoopTree(data->graph(), temp_zone);
1077 if (loop_tree != nullptr && loop_tree->outer_loops().size() > 0) {
1078 LoopPeeler::Peel(data->graph(), data->common(), loop_tree,
1079 loop_tree->outer_loops()[0], temp_zone);
1080 }
1081 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001082};
1083
1084
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001085struct ComputeSchedulePhase {
1086 static const char* phase_name() { return "scheduling"; }
1087
1088 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001089 Schedule* schedule = Scheduler::ComputeSchedule(
1090 temp_zone, data->graph(), data->info()->is_splitting_enabled()
1091 ? Scheduler::kSplitNodes
1092 : Scheduler::kNoFlags);
1093 if (FLAG_turbo_verify) ScheduleVerifier::Run(schedule);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001094 data->set_schedule(schedule);
1095 }
1096};
1097
1098
1099struct InstructionSelectionPhase {
1100 static const char* phase_name() { return "select instructions"; }
1101
1102 void Run(PipelineData* data, Zone* temp_zone, Linkage* linkage) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001103 InstructionSelector selector(
1104 temp_zone, data->graph()->NodeCount(), linkage, data->sequence(),
Ben Murdoch097c5b22016-05-18 11:27:45 +01001105 data->schedule(), data->source_positions(), data->frame(),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001106 data->info()->is_source_positions_enabled()
1107 ? InstructionSelector::kAllSourcePositions
1108 : InstructionSelector::kCallSourcePositions);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001109 selector.SelectInstructions();
1110 }
1111};
1112
1113
1114struct MeetRegisterConstraintsPhase {
1115 static const char* phase_name() { return "meet register constraints"; }
1116
1117 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001118 ConstraintBuilder builder(data->register_allocation_data());
1119 builder.MeetRegisterConstraints();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001120 }
1121};
1122
1123
1124struct ResolvePhisPhase {
1125 static const char* phase_name() { return "resolve phis"; }
1126
1127 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001128 ConstraintBuilder builder(data->register_allocation_data());
1129 builder.ResolvePhis();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001130 }
1131};
1132
1133
1134struct BuildLiveRangesPhase {
1135 static const char* phase_name() { return "build live ranges"; }
1136
1137 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001138 LiveRangeBuilder builder(data->register_allocation_data(), temp_zone);
1139 builder.BuildLiveRanges();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001140 }
1141};
1142
1143
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001144struct SplinterLiveRangesPhase {
1145 static const char* phase_name() { return "splinter live ranges"; }
1146
1147 void Run(PipelineData* data, Zone* temp_zone) {
1148 LiveRangeSeparator live_range_splinterer(data->register_allocation_data(),
1149 temp_zone);
1150 live_range_splinterer.Splinter();
1151 }
1152};
1153
1154
1155template <typename RegAllocator>
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001156struct AllocateGeneralRegistersPhase {
1157 static const char* phase_name() { return "allocate general registers"; }
1158
1159 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001160 RegAllocator allocator(data->register_allocation_data(), GENERAL_REGISTERS,
1161 temp_zone);
1162 allocator.AllocateRegisters();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001163 }
1164};
1165
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001166template <typename RegAllocator>
Ben Murdochc5610432016-08-08 18:44:38 +01001167struct AllocateFPRegistersPhase {
1168 static const char* phase_name() {
1169 return "allocate floating point registers";
1170 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001171
1172 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdochc5610432016-08-08 18:44:38 +01001173 RegAllocator allocator(data->register_allocation_data(), FP_REGISTERS,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001174 temp_zone);
1175 allocator.AllocateRegisters();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001176 }
1177};
1178
1179
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001180struct MergeSplintersPhase {
1181 static const char* phase_name() { return "merge splintered ranges"; }
1182 void Run(PipelineData* pipeline_data, Zone* temp_zone) {
1183 RegisterAllocationData* data = pipeline_data->register_allocation_data();
1184 LiveRangeMerger live_range_merger(data, temp_zone);
1185 live_range_merger.Merge();
1186 }
1187};
1188
1189
1190struct LocateSpillSlotsPhase {
1191 static const char* phase_name() { return "locate spill slots"; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001192
1193 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001194 SpillSlotLocator locator(data->register_allocation_data());
1195 locator.LocateSpillSlots();
1196 }
1197};
1198
1199
1200struct AssignSpillSlotsPhase {
1201 static const char* phase_name() { return "assign spill slots"; }
1202
1203 void Run(PipelineData* data, Zone* temp_zone) {
1204 OperandAssigner assigner(data->register_allocation_data());
1205 assigner.AssignSpillSlots();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001206 }
1207};
1208
1209
1210struct CommitAssignmentPhase {
1211 static const char* phase_name() { return "commit assignment"; }
1212
1213 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001214 OperandAssigner assigner(data->register_allocation_data());
1215 assigner.CommitAssignment();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001216 }
1217};
1218
1219
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001220struct PopulateReferenceMapsPhase {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001221 static const char* phase_name() { return "populate pointer maps"; }
1222
1223 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001224 ReferenceMapPopulator populator(data->register_allocation_data());
1225 populator.PopulateReferenceMaps();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001226 }
1227};
1228
1229
1230struct ConnectRangesPhase {
1231 static const char* phase_name() { return "connect ranges"; }
1232
1233 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001234 LiveRangeConnector connector(data->register_allocation_data());
1235 connector.ConnectRanges(temp_zone);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001236 }
1237};
1238
1239
1240struct ResolveControlFlowPhase {
1241 static const char* phase_name() { return "resolve control flow"; }
1242
1243 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001244 LiveRangeConnector connector(data->register_allocation_data());
1245 connector.ResolveControlFlow(temp_zone);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001246 }
1247};
1248
1249
1250struct OptimizeMovesPhase {
1251 static const char* phase_name() { return "optimize moves"; }
1252
1253 void Run(PipelineData* data, Zone* temp_zone) {
1254 MoveOptimizer move_optimizer(temp_zone, data->sequence());
1255 move_optimizer.Run();
1256 }
1257};
1258
1259
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001260struct FrameElisionPhase {
1261 static const char* phase_name() { return "frame elision"; }
1262
1263 void Run(PipelineData* data, Zone* temp_zone) {
1264 FrameElider(data->sequence()).Run();
1265 }
1266};
1267
1268
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001269struct JumpThreadingPhase {
1270 static const char* phase_name() { return "jump threading"; }
1271
Ben Murdoch097c5b22016-05-18 11:27:45 +01001272 void Run(PipelineData* data, Zone* temp_zone, bool frame_at_start) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001273 ZoneVector<RpoNumber> result(temp_zone);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001274 if (JumpThreading::ComputeForwarding(temp_zone, result, data->sequence(),
1275 frame_at_start)) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001276 JumpThreading::ApplyForwarding(result, data->sequence());
1277 }
1278 }
1279};
1280
1281
1282struct GenerateCodePhase {
1283 static const char* phase_name() { return "generate code"; }
1284
1285 void Run(PipelineData* data, Zone* temp_zone, Linkage* linkage) {
1286 CodeGenerator generator(data->frame(), linkage, data->sequence(),
1287 data->info());
1288 data->set_code(generator.GenerateCode());
1289 }
1290};
1291
1292
1293struct PrintGraphPhase {
1294 static const char* phase_name() { return nullptr; }
1295
1296 void Run(PipelineData* data, Zone* temp_zone, const char* phase) {
1297 CompilationInfo* info = data->info();
1298 Graph* graph = data->graph();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001299
1300 { // Print JSON.
Ben Murdochc5610432016-08-08 18:44:38 +01001301 AllowHandleDereference allow_deref;
1302 TurboJsonFile json_of(info, std::ios_base::app);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001303 json_of << "{\"name\":\"" << phase << "\",\"type\":\"graph\",\"data\":"
1304 << AsJSON(*graph, data->source_positions()) << "},\n";
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001305 }
1306
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001307 if (FLAG_trace_turbo_graph) { // Simple textual RPO.
Ben Murdochc5610432016-08-08 18:44:38 +01001308 AllowHandleDereference allow_deref;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001309 OFStream os(stdout);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001310 os << "-- Graph after " << phase << " -- " << std::endl;
1311 os << AsRPO(*graph);
1312 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001313 }
1314};
1315
1316
1317struct VerifyGraphPhase {
1318 static const char* phase_name() { return nullptr; }
1319
Ben Murdochc5610432016-08-08 18:44:38 +01001320 void Run(PipelineData* data, Zone* temp_zone, const bool untyped,
1321 bool values_only = false) {
1322 Verifier::Run(data->graph(), !untyped ? Verifier::TYPED : Verifier::UNTYPED,
1323 values_only ? Verifier::kValuesOnly : Verifier::kAll);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001324 }
1325};
1326
Ben Murdochc5610432016-08-08 18:44:38 +01001327void PipelineImpl::RunPrintAndVerify(const char* phase, bool untyped) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001328 if (FLAG_trace_turbo) {
1329 Run<PrintGraphPhase>(phase);
1330 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001331 if (FLAG_turbo_verify) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001332 Run<VerifyGraphPhase>(untyped);
1333 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001334}
1335
Ben Murdochc5610432016-08-08 18:44:38 +01001336bool PipelineImpl::CreateGraph() {
1337 PipelineData* data = this->data_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001338
Ben Murdochc5610432016-08-08 18:44:38 +01001339 data->BeginPhaseKind("graph creation");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001340
1341 if (FLAG_trace_turbo) {
1342 OFStream os(stdout);
1343 os << "---------------------------------------------------\n"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001344 << "Begin compiling method " << info()->GetDebugName().get()
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001345 << " using Turbofan" << std::endl;
1346 TurboCfgFile tcf(isolate());
1347 tcf << AsC1VCompilation(info());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001348 }
1349
Ben Murdochc5610432016-08-08 18:44:38 +01001350 data->source_positions()->AddDecorator();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001351
1352 if (FLAG_loop_assignment_analysis) {
1353 Run<LoopAssignmentAnalysisPhase>();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001354 }
1355
Ben Murdochc5610432016-08-08 18:44:38 +01001356 Run<TypeHintAnalysisPhase>();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001357
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001358 Run<GraphBuilderPhase>();
Ben Murdochc5610432016-08-08 18:44:38 +01001359 if (data->compilation_failed()) {
1360 data->EndPhaseKind();
1361 return false;
1362 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001363 RunPrintAndVerify("Initial untyped", true);
1364
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001365 // Perform OSR deconstruction.
1366 if (info()->is_osr()) {
1367 Run<OsrDeconstructionPhase>();
1368 RunPrintAndVerify("OSR deconstruction", true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001369 }
1370
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001371 // Perform function context specialization and inlining (if enabled).
1372 Run<InliningPhase>();
1373 RunPrintAndVerify("Inlined", true);
1374
1375 // Remove dead->live edges from the graph.
1376 Run<EarlyGraphTrimmingPhase>();
1377 RunPrintAndVerify("Early trimmed", true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001378
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001379 if (FLAG_print_turbo_replay) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001380 // Print a replay of the initial graph.
Ben Murdochc5610432016-08-08 18:44:38 +01001381 GraphReplayPrinter::PrintReplay(data->graph());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001382 }
1383
Ben Murdochc5610432016-08-08 18:44:38 +01001384 // Run the type-sensitive lowerings and optimizations on the graph.
1385 {
1386 // Type the graph and keep the Typer running on newly created nodes within
1387 // this scope; the Typer is automatically unlinked from the Graph once we
1388 // leave this scope below.
1389 Typer typer(isolate(), data->graph(), info()->is_deoptimization_enabled()
1390 ? Typer::kDeoptimizationEnabled
1391 : Typer::kNoFlags,
1392 info()->dependencies());
1393 Run<TyperPhase>(&typer);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001394 RunPrintAndVerify("Typed");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001395
Ben Murdochc5610432016-08-08 18:44:38 +01001396 data->BeginPhaseKind("lowering");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001397
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001398 // Lower JSOperators where we can determine types.
1399 Run<TypedLoweringPhase>();
1400 RunPrintAndVerify("Lowered typed");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001401
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001402 if (FLAG_turbo_stress_loop_peeling) {
1403 Run<StressLoopPeelingPhase>();
1404 RunPrintAndVerify("Loop peeled");
1405 }
1406
1407 if (FLAG_turbo_escape) {
1408 Run<EscapeAnalysisPhase>();
1409 RunPrintAndVerify("Escape Analysed");
1410 }
1411
Ben Murdochc5610432016-08-08 18:44:38 +01001412 // Select representations.
1413 Run<RepresentationSelectionPhase>();
1414 RunPrintAndVerify("Representations selected");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001415
Ben Murdochc5610432016-08-08 18:44:38 +01001416 // Run early optimization pass.
1417 Run<EarlyOptimizationPhase>();
1418 RunPrintAndVerify("Early optimized");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001419 }
1420
Ben Murdochc5610432016-08-08 18:44:38 +01001421#ifdef DEBUG
1422 // From now on it is invalid to look at types on the nodes, because:
1423 //
1424 // (a) The remaining passes (might) run concurrent to the main thread and
1425 // therefore must not access the Heap or the Isolate in an uncontrolled
1426 // way (as done by the type system), and
1427 // (b) the types on the nodes might not make sense after representation
1428 // selection due to the way we handle truncations; if we'd want to look
1429 // at types afterwards we'd essentially need to re-type (large portions
1430 // of) the graph.
1431 //
1432 // In order to catch bugs related to type access after this point we remove
1433 // the types from the nodes at this point (currently only in Debug builds).
1434 Run<UntyperPhase>();
1435 RunPrintAndVerify("Untyped", true);
1436#endif
1437
1438 data->EndPhaseKind();
1439
1440 return true;
1441}
1442
1443bool PipelineImpl::OptimizeGraph(Linkage* linkage) {
1444 PipelineData* data = this->data_;
1445
1446 data->BeginPhaseKind("block building");
1447
1448 Run<EffectControlLinearizationPhase>();
1449 RunPrintAndVerify("Effect and control linearized", true);
1450
1451 Run<BranchEliminationPhase>();
1452 RunPrintAndVerify("Branch conditions eliminated", true);
1453
1454 // Optimize control flow.
1455 if (FLAG_turbo_cf_optimization) {
1456 Run<ControlFlowOptimizationPhase>();
1457 RunPrintAndVerify("Control flow optimized", true);
1458 }
1459
1460 // Optimize memory access and allocation operations.
1461 Run<MemoryOptimizationPhase>();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001462 // TODO(jarin, rossberg): Remove UNTYPED once machine typing works.
Ben Murdochc5610432016-08-08 18:44:38 +01001463 RunPrintAndVerify("Memory optimized", true);
1464
1465 // Lower changes that have been inserted before.
1466 Run<LateOptimizationPhase>();
1467 // TODO(jarin, rossberg): Remove UNTYPED once machine typing works.
1468 RunPrintAndVerify("Late optimized", true);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001469
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001470 Run<LateGraphTrimmingPhase>();
1471 // TODO(jarin, rossberg): Remove UNTYPED once machine typing works.
1472 RunPrintAndVerify("Late trimmed", true);
1473
Ben Murdochc5610432016-08-08 18:44:38 +01001474 data->source_positions()->RemoveDecorator();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001475
Ben Murdochc5610432016-08-08 18:44:38 +01001476 return ScheduleAndSelectInstructions(linkage);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001477}
1478
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001479Handle<Code> Pipeline::GenerateCodeForCodeStub(Isolate* isolate,
1480 CallDescriptor* call_descriptor,
1481 Graph* graph, Schedule* schedule,
Ben Murdoch097c5b22016-05-18 11:27:45 +01001482 Code::Flags flags,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001483 const char* debug_name) {
Ben Murdochc5610432016-08-08 18:44:38 +01001484 CompilationInfo info(CStrVector(debug_name), isolate, graph->zone(), flags);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001485
1486 // Construct a pipeline for scheduling and code generation.
Ben Murdochda12d292016-06-02 14:46:10 +01001487 ZonePool zone_pool(isolate->allocator());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001488 PipelineData data(&zone_pool, &info, graph, schedule);
1489 base::SmartPointer<PipelineStatistics> pipeline_statistics;
1490 if (FLAG_turbo_stats) {
1491 pipeline_statistics.Reset(new PipelineStatistics(&info, &zone_pool));
1492 pipeline_statistics->BeginPhaseKind("stub codegen");
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001493 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001494
Ben Murdochc5610432016-08-08 18:44:38 +01001495 PipelineImpl pipeline(&data);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001496 DCHECK_NOT_NULL(data.schedule());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001497
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001498 if (FLAG_trace_turbo) {
Ben Murdochc5610432016-08-08 18:44:38 +01001499 TurboJsonFile json_of(&info, std::ios_base::trunc);
1500 json_of << "{\"function\":\"" << info.GetDebugName().get()
1501 << "\", \"source\":\"\",\n\"phases\":[";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001502 pipeline.Run<PrintGraphPhase>("Machine");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001503 }
1504
Ben Murdochc5610432016-08-08 18:44:38 +01001505 pipeline.Run<VerifyGraphPhase>(false, true);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001506 return pipeline.ScheduleAndGenerateCode(call_descriptor);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001507}
1508
Ben Murdochc5610432016-08-08 18:44:38 +01001509// static
1510Handle<Code> Pipeline::GenerateCodeForTesting(CompilationInfo* info) {
1511 ZonePool zone_pool(info->isolate()->allocator());
1512 base::SmartPointer<PipelineStatistics> pipeline_statistics(
1513 CreatePipelineStatistics(info, &zone_pool));
1514 PipelineData data(&zone_pool, info, pipeline_statistics.get());
1515 PipelineImpl pipeline(&data);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001516
Ben Murdochc5610432016-08-08 18:44:38 +01001517 Linkage linkage(Linkage::ComputeIncoming(data.instruction_zone(), info));
1518
1519 if (!pipeline.CreateGraph()) return Handle<Code>::null();
1520 if (!pipeline.OptimizeGraph(&linkage)) return Handle<Code>::null();
1521 return pipeline.GenerateCode(&linkage);
1522}
1523
1524// static
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001525Handle<Code> Pipeline::GenerateCodeForTesting(CompilationInfo* info,
1526 Graph* graph,
1527 Schedule* schedule) {
1528 CallDescriptor* call_descriptor =
1529 Linkage::ComputeIncoming(info->zone(), info);
1530 return GenerateCodeForTesting(info, call_descriptor, graph, schedule);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001531}
1532
Ben Murdochc5610432016-08-08 18:44:38 +01001533// static
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001534Handle<Code> Pipeline::GenerateCodeForTesting(CompilationInfo* info,
1535 CallDescriptor* call_descriptor,
1536 Graph* graph,
1537 Schedule* schedule) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001538 // Construct a pipeline for scheduling and code generation.
Ben Murdochda12d292016-06-02 14:46:10 +01001539 ZonePool zone_pool(info->isolate()->allocator());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001540 PipelineData data(&zone_pool, info, graph, schedule);
1541 base::SmartPointer<PipelineStatistics> pipeline_statistics;
1542 if (FLAG_turbo_stats) {
1543 pipeline_statistics.Reset(new PipelineStatistics(info, &zone_pool));
1544 pipeline_statistics->BeginPhaseKind("test codegen");
1545 }
1546
Ben Murdochc5610432016-08-08 18:44:38 +01001547 PipelineImpl pipeline(&data);
1548
1549 if (FLAG_trace_turbo) {
1550 TurboJsonFile json_of(info, std::ios_base::trunc);
1551 json_of << "{\"function\":\"" << info->GetDebugName().get()
1552 << "\", \"source\":\"\",\n\"phases\":[";
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001553 }
Ben Murdochc5610432016-08-08 18:44:38 +01001554 // TODO(rossberg): Should this really be untyped?
1555 pipeline.RunPrintAndVerify("Machine", true);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001556
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001557 return pipeline.ScheduleAndGenerateCode(call_descriptor);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001558}
1559
Ben Murdochc5610432016-08-08 18:44:38 +01001560// static
1561CompilationJob* Pipeline::NewCompilationJob(Handle<JSFunction> function) {
1562 return new PipelineCompilationJob(function->GetIsolate(), function);
1563}
1564
1565// static
1566CompilationJob* Pipeline::NewWasmCompilationJob(
1567 CompilationInfo* info, Graph* graph, CallDescriptor* descriptor,
1568 SourcePositionTable* source_positions) {
1569 return new PipelineWasmCompilationJob(info, graph, descriptor,
1570 source_positions);
1571}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001572
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001573bool Pipeline::AllocateRegistersForTesting(const RegisterConfiguration* config,
1574 InstructionSequence* sequence,
1575 bool run_verifier) {
Ben Murdochc5610432016-08-08 18:44:38 +01001576 CompilationInfo info(ArrayVector("testing"), sequence->isolate(),
1577 sequence->zone());
Ben Murdochda12d292016-06-02 14:46:10 +01001578 ZonePool zone_pool(sequence->isolate()->allocator());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001579 PipelineData data(&zone_pool, &info, sequence);
Ben Murdochc5610432016-08-08 18:44:38 +01001580 PipelineImpl pipeline(&data);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001581 pipeline.data_->InitializeFrameData(nullptr);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001582 pipeline.AllocateRegisters(config, nullptr, run_verifier);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001583 return !data.compilation_failed();
1584}
1585
Ben Murdochc5610432016-08-08 18:44:38 +01001586bool PipelineImpl::ScheduleAndSelectInstructions(Linkage* linkage) {
1587 CallDescriptor* call_descriptor = linkage->GetIncomingDescriptor();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001588 PipelineData* data = this->data_;
1589
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001590 DCHECK_NOT_NULL(data->graph());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001591
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001592 if (data->schedule() == nullptr) Run<ComputeSchedulePhase>();
1593 TraceSchedule(data->info(), data->schedule());
1594
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001595 if (FLAG_turbo_profiling) {
Ben Murdochc5610432016-08-08 18:44:38 +01001596 data->set_profiler_data(BasicBlockInstrumentor::Instrument(
1597 info(), data->graph(), data->schedule()));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001598 }
1599
Ben Murdochda12d292016-06-02 14:46:10 +01001600 data->InitializeInstructionSequence(call_descriptor);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001601
Ben Murdoch097c5b22016-05-18 11:27:45 +01001602 data->InitializeFrameData(call_descriptor);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001603 // Select and schedule instructions covering the scheduled graph.
Ben Murdochc5610432016-08-08 18:44:38 +01001604 Run<InstructionSelectionPhase>(linkage);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001605
1606 if (FLAG_trace_turbo && !data->MayHaveUnverifiableGraph()) {
Ben Murdochc5610432016-08-08 18:44:38 +01001607 AllowHandleDereference allow_deref;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001608 TurboCfgFile tcf(isolate());
1609 tcf << AsC1V("CodeGen", data->schedule(), data->source_positions(),
1610 data->sequence());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001611 }
1612
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001613 if (FLAG_trace_turbo) {
Ben Murdochc5610432016-08-08 18:44:38 +01001614 std::ostringstream source_position_output;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001615 // Output source position information before the graph is deleted.
1616 data_->source_positions()->Print(source_position_output);
Ben Murdochc5610432016-08-08 18:44:38 +01001617 data_->set_source_position_output(source_position_output.str());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001618 }
1619
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001620 data->DeleteGraphZone();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001621
Ben Murdochc5610432016-08-08 18:44:38 +01001622 data->BeginPhaseKind("register allocation");
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001623
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001624 bool run_verifier = FLAG_turbo_verify_allocation;
Ben Murdoch097c5b22016-05-18 11:27:45 +01001625
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001626 // Allocate registers.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001627 AllocateRegisters(
1628 RegisterConfiguration::ArchDefault(RegisterConfiguration::TURBOFAN),
1629 call_descriptor, run_verifier);
Ben Murdochda12d292016-06-02 14:46:10 +01001630 Run<FrameElisionPhase>();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001631 if (data->compilation_failed()) {
1632 info()->AbortOptimization(kNotEnoughVirtualRegistersRegalloc);
Ben Murdochc5610432016-08-08 18:44:38 +01001633 data->EndPhaseKind();
1634 return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001635 }
1636
Ben Murdoch097c5b22016-05-18 11:27:45 +01001637 // TODO(mtrofin): move this off to the register allocator.
1638 bool generate_frame_at_start =
Ben Murdochda12d292016-06-02 14:46:10 +01001639 data_->sequence()->instruction_blocks().front()->must_construct_frame();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001640 // Optimimize jumps.
1641 if (FLAG_turbo_jt) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001642 Run<JumpThreadingPhase>(generate_frame_at_start);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001643 }
1644
Ben Murdochc5610432016-08-08 18:44:38 +01001645 data->EndPhaseKind();
1646
1647 return true;
1648}
1649
1650Handle<Code> PipelineImpl::GenerateCode(Linkage* linkage) {
1651 PipelineData* data = this->data_;
1652
1653 data->BeginPhaseKind("code generation");
1654
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001655 // Generate final machine code.
Ben Murdochc5610432016-08-08 18:44:38 +01001656 Run<GenerateCodePhase>(linkage);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001657
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001658 Handle<Code> code = data->code();
Ben Murdochc5610432016-08-08 18:44:38 +01001659 if (data->profiler_data()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001660#if ENABLE_DISASSEMBLER
1661 std::ostringstream os;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001662 code->Disassemble(nullptr, os);
Ben Murdochc5610432016-08-08 18:44:38 +01001663 data->profiler_data()->SetCode(&os);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001664#endif
1665 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001666
1667 info()->SetCode(code);
1668 v8::internal::CodeGenerator::PrintCode(code, info());
1669
1670 if (FLAG_trace_turbo) {
Ben Murdochc5610432016-08-08 18:44:38 +01001671 TurboJsonFile json_of(info(), std::ios_base::app);
1672 json_of << "{\"name\":\"disassembly\",\"type\":\"disassembly\",\"data\":\"";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001673#if ENABLE_DISASSEMBLER
Ben Murdochc5610432016-08-08 18:44:38 +01001674 std::stringstream disassembly_stream;
1675 code->Disassemble(nullptr, disassembly_stream);
1676 std::string disassembly_string(disassembly_stream.str());
1677 for (const auto& c : disassembly_string) {
1678 json_of << AsEscapedUC16ForJSON(c);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001679 }
Ben Murdochc5610432016-08-08 18:44:38 +01001680#endif // ENABLE_DISASSEMBLER
1681 json_of << "\"}\n],\n";
1682 json_of << "\"nodePositions\":";
1683 json_of << data->source_position_output();
1684 json_of << "}";
1685
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001686 OFStream os(stdout);
1687 os << "---------------------------------------------------\n"
1688 << "Finished compiling method " << info()->GetDebugName().get()
1689 << " using Turbofan" << std::endl;
1690 }
1691
1692 return code;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001693}
1694
Ben Murdochc5610432016-08-08 18:44:38 +01001695Handle<Code> PipelineImpl::ScheduleAndGenerateCode(
1696 CallDescriptor* call_descriptor) {
1697 Linkage linkage(call_descriptor);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001698
Ben Murdochc5610432016-08-08 18:44:38 +01001699 // Schedule the graph, perform instruction selection and register allocation.
1700 if (!ScheduleAndSelectInstructions(&linkage)) return Handle<Code>();
1701
1702 // Generate the final machine code.
1703 return GenerateCode(&linkage);
1704}
1705
1706void PipelineImpl::AllocateRegisters(const RegisterConfiguration* config,
1707 CallDescriptor* descriptor,
1708 bool run_verifier) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001709 PipelineData* data = this->data_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001710 // Don't track usage for this zone in compiler stats.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001711 base::SmartPointer<Zone> verifier_zone;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001712 RegisterAllocatorVerifier* verifier = nullptr;
1713 if (run_verifier) {
Ben Murdochda12d292016-06-02 14:46:10 +01001714 verifier_zone.Reset(new Zone(isolate()->allocator()));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001715 verifier = new (verifier_zone.get()) RegisterAllocatorVerifier(
1716 verifier_zone.get(), config, data->sequence());
1717 }
1718
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001719#ifdef DEBUG
Ben Murdochda12d292016-06-02 14:46:10 +01001720 data_->sequence()->ValidateEdgeSplitForm();
Ben Murdochc5610432016-08-08 18:44:38 +01001721 data_->sequence()->ValidateDeferredBlockEntryPaths();
Ben Murdochda12d292016-06-02 14:46:10 +01001722 data_->sequence()->ValidateDeferredBlockExitPaths();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001723#endif
1724
Ben Murdochc5610432016-08-08 18:44:38 +01001725 data->InitializeRegisterAllocationData(config, descriptor);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001726 if (info()->is_osr()) {
1727 OsrHelper osr_helper(info());
1728 osr_helper.SetupFrame(data->frame());
1729 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001730
1731 Run<MeetRegisterConstraintsPhase>();
1732 Run<ResolvePhisPhase>();
1733 Run<BuildLiveRangesPhase>();
1734 if (FLAG_trace_turbo_graph) {
Ben Murdochc5610432016-08-08 18:44:38 +01001735 AllowHandleDereference allow_deref;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001736 OFStream os(stdout);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001737 os << "----- Instruction sequence before register allocation -----\n"
Ben Murdochc5610432016-08-08 18:44:38 +01001738 << PrintableInstructionSequence({config, data->sequence()});
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001739 }
1740 if (verifier != nullptr) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001741 CHECK(!data->register_allocation_data()->ExistsUseWithoutDefinition());
1742 CHECK(data->register_allocation_data()
1743 ->RangesDefinedInDeferredStayInDeferred());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001744 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001745
1746 if (FLAG_turbo_preprocess_ranges) {
1747 Run<SplinterLiveRangesPhase>();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001748 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001749
1750 if (FLAG_turbo_greedy_regalloc) {
1751 Run<AllocateGeneralRegistersPhase<GreedyAllocator>>();
Ben Murdochc5610432016-08-08 18:44:38 +01001752 Run<AllocateFPRegistersPhase<GreedyAllocator>>();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001753 } else {
1754 Run<AllocateGeneralRegistersPhase<LinearScanAllocator>>();
Ben Murdochc5610432016-08-08 18:44:38 +01001755 Run<AllocateFPRegistersPhase<LinearScanAllocator>>();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001756 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001757
1758 if (FLAG_turbo_preprocess_ranges) {
1759 Run<MergeSplintersPhase>();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001760 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001761
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001762 Run<AssignSpillSlotsPhase>();
1763
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001764 Run<CommitAssignmentPhase>();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001765 Run<PopulateReferenceMapsPhase>();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001766 Run<ConnectRangesPhase>();
1767 Run<ResolveControlFlowPhase>();
1768 if (FLAG_turbo_move_optimization) {
1769 Run<OptimizeMovesPhase>();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001770 }
1771
Ben Murdochda12d292016-06-02 14:46:10 +01001772 Run<LocateSpillSlotsPhase>();
1773
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001774 if (FLAG_trace_turbo_graph) {
Ben Murdochc5610432016-08-08 18:44:38 +01001775 AllowHandleDereference allow_deref;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001776 OFStream os(stdout);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001777 os << "----- Instruction sequence after register allocation -----\n"
Ben Murdochc5610432016-08-08 18:44:38 +01001778 << PrintableInstructionSequence({config, data->sequence()});
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001779 }
1780
1781 if (verifier != nullptr) {
1782 verifier->VerifyAssignment();
1783 verifier->VerifyGapMoves();
1784 }
1785
1786 if (FLAG_trace_turbo && !data->MayHaveUnverifiableGraph()) {
1787 TurboCfgFile tcf(data->isolate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001788 tcf << AsC1VRegisterAllocationData("CodeGen",
1789 data->register_allocation_data());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001790 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001791
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001792 data->DeleteRegisterAllocationZone();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001793}
1794
Ben Murdochc5610432016-08-08 18:44:38 +01001795CompilationInfo* PipelineImpl::info() const { return data_->info(); }
1796
1797Isolate* PipelineImpl::isolate() const { return info()->isolate(); }
Ben Murdoch097c5b22016-05-18 11:27:45 +01001798
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001799} // namespace compiler
1800} // namespace internal
1801} // namespace v8