blob: d5920009115d34a8ec9220b49868b75897e378ac [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 Murdoch61f157c2016-09-16 13:49:30 +010017#include "src/compiler/checkpoint-elimination.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018#include "src/compiler/code-generator.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040019#include "src/compiler/common-operator-reducer.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000020#include "src/compiler/control-flow-optimizer.h"
21#include "src/compiler/dead-code-elimination.h"
Ben Murdochc5610432016-08-08 18:44:38 +010022#include "src/compiler/effect-control-linearizer.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000023#include "src/compiler/escape-analysis-reducer.h"
Ben Murdochc5610432016-08-08 18:44:38 +010024#include "src/compiler/escape-analysis.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000025#include "src/compiler/frame-elider.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000026#include "src/compiler/graph-replay.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000027#include "src/compiler/graph-trimmer.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000028#include "src/compiler/graph-visualizer.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"
Ben Murdoch61f157c2016-09-16 13:49:30 +010052#include "src/compiler/redundancy-elimination.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040053#include "src/compiler/register-allocator-verifier.h"
Ben Murdochc5610432016-08-08 18:44:38 +010054#include "src/compiler/register-allocator.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000055#include "src/compiler/schedule.h"
56#include "src/compiler/scheduler.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040057#include "src/compiler/select-lowering.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000058#include "src/compiler/simplified-lowering.h"
59#include "src/compiler/simplified-operator-reducer.h"
Ben Murdochc5610432016-08-08 18:44:38 +010060#include "src/compiler/simplified-operator.h"
Ben Murdoch61f157c2016-09-16 13:49:30 +010061#include "src/compiler/store-store-elimination.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000062#include "src/compiler/tail-call-optimization.h"
63#include "src/compiler/type-hint-analyzer.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000064#include "src/compiler/typer.h"
65#include "src/compiler/value-numbering-reducer.h"
66#include "src/compiler/verifier.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040067#include "src/compiler/zone-pool.h"
Ben Murdochc5610432016-08-08 18:44:38 +010068#include "src/isolate-inl.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000069#include "src/ostreams.h"
Ben Murdochc5610432016-08-08 18:44:38 +010070#include "src/parsing/parser.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000071#include "src/register-configuration.h"
72#include "src/type-info.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000073#include "src/utils.h"
74
75namespace v8 {
76namespace internal {
77namespace compiler {
78
Emily Bernierd0a1eb72015-03-24 16:35:39 -040079class PipelineData {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000080 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000081 // For main entry point.
82 PipelineData(ZonePool* zone_pool, CompilationInfo* info,
83 PipelineStatistics* pipeline_statistics)
84 : isolate_(info->isolate()),
85 info_(info),
Ben Murdochc5610432016-08-08 18:44:38 +010086 debug_name_(info_->GetDebugName()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000087 outer_zone_(info_->zone()),
88 zone_pool_(zone_pool),
89 pipeline_statistics_(pipeline_statistics),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000090 graph_zone_scope_(zone_pool_),
91 graph_zone_(graph_zone_scope_.zone()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000092 instruction_zone_scope_(zone_pool_),
93 instruction_zone_(instruction_zone_scope_.zone()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000094 register_allocation_zone_scope_(zone_pool_),
Ben Murdochc5610432016-08-08 18:44:38 +010095 register_allocation_zone_(register_allocation_zone_scope_.zone()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000096 PhaseScope scope(pipeline_statistics, "init pipeline data");
97 graph_ = new (graph_zone_) Graph(graph_zone_);
Ben Murdochc5610432016-08-08 18:44:38 +010098 source_positions_ = new (graph_zone_) SourcePositionTable(graph_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000099 simplified_ = new (graph_zone_) SimplifiedOperatorBuilder(graph_zone_);
100 machine_ = new (graph_zone_) MachineOperatorBuilder(
101 graph_zone_, MachineType::PointerRepresentation(),
102 InstructionSelector::SupportedMachineOperatorFlags());
103 common_ = new (graph_zone_) CommonOperatorBuilder(graph_zone_);
104 javascript_ = new (graph_zone_) JSOperatorBuilder(graph_zone_);
105 jsgraph_ = new (graph_zone_)
106 JSGraph(isolate_, graph_, common_, javascript_, simplified_, machine_);
107 }
108
Ben Murdochc5610432016-08-08 18:44:38 +0100109 // For WASM compile entry point.
110 PipelineData(ZonePool* zone_pool, CompilationInfo* info, Graph* graph,
111 SourcePositionTable* source_positions)
112 : isolate_(info->isolate()),
113 info_(info),
114 debug_name_(info_->GetDebugName()),
115 zone_pool_(zone_pool),
116 graph_zone_scope_(zone_pool_),
117 graph_(graph),
118 source_positions_(source_positions),
119 instruction_zone_scope_(zone_pool_),
120 instruction_zone_(instruction_zone_scope_.zone()),
121 register_allocation_zone_scope_(zone_pool_),
122 register_allocation_zone_(register_allocation_zone_scope_.zone()) {}
123
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000124 // For machine graph testing entry point.
125 PipelineData(ZonePool* zone_pool, CompilationInfo* info, Graph* graph,
126 Schedule* schedule)
127 : isolate_(info->isolate()),
128 info_(info),
Ben Murdochc5610432016-08-08 18:44:38 +0100129 debug_name_(info_->GetDebugName()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000130 zone_pool_(zone_pool),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000131 graph_zone_scope_(zone_pool_),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000132 graph_(graph),
Ben Murdochc5610432016-08-08 18:44:38 +0100133 source_positions_(new (info->zone()) SourcePositionTable(graph_)),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000134 schedule_(schedule),
135 instruction_zone_scope_(zone_pool_),
136 instruction_zone_(instruction_zone_scope_.zone()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000137 register_allocation_zone_scope_(zone_pool_),
Ben Murdochc5610432016-08-08 18:44:38 +0100138 register_allocation_zone_(register_allocation_zone_scope_.zone()) {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000139
140 // For register allocation testing entry point.
141 PipelineData(ZonePool* zone_pool, CompilationInfo* info,
142 InstructionSequence* sequence)
143 : isolate_(info->isolate()),
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400144 info_(info),
Ben Murdochc5610432016-08-08 18:44:38 +0100145 debug_name_(info_->GetDebugName()),
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400146 zone_pool_(zone_pool),
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400147 graph_zone_scope_(zone_pool_),
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400148 instruction_zone_scope_(zone_pool_),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000149 instruction_zone_(sequence->zone()),
150 sequence_(sequence),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000151 register_allocation_zone_scope_(zone_pool_),
Ben Murdochc5610432016-08-08 18:44:38 +0100152 register_allocation_zone_(register_allocation_zone_scope_.zone()) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000153
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400154 ~PipelineData() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000155 DeleteRegisterAllocationZone();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400156 DeleteInstructionZone();
157 DeleteGraphZone();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000158 }
159
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400160 Isolate* isolate() const { return isolate_; }
161 CompilationInfo* info() const { return info_; }
162 ZonePool* zone_pool() const { return zone_pool_; }
163 PipelineStatistics* pipeline_statistics() { return pipeline_statistics_; }
164 bool compilation_failed() const { return compilation_failed_; }
165 void set_compilation_failed() { compilation_failed_ = true; }
166 Handle<Code> code() { return code_; }
167 void set_code(Handle<Code> code) {
168 DCHECK(code_.is_null());
169 code_ = code;
170 }
171
172 // RawMachineAssembler generally produces graphs which cannot be verified.
173 bool MayHaveUnverifiableGraph() const { return outer_zone_ == nullptr; }
174
175 Zone* graph_zone() const { return graph_zone_; }
176 Graph* graph() const { return graph_; }
Ben Murdochc5610432016-08-08 18:44:38 +0100177 SourcePositionTable* source_positions() const { return source_positions_; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400178 MachineOperatorBuilder* machine() const { return machine_; }
179 CommonOperatorBuilder* common() const { return common_; }
180 JSOperatorBuilder* javascript() const { return javascript_; }
181 JSGraph* jsgraph() const { return jsgraph_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000182 MaybeHandle<Context> native_context() const {
183 if (info()->is_native_context_specializing()) {
184 return handle(info()->native_context(), isolate());
185 }
186 return MaybeHandle<Context>();
187 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400188
189 LoopAssignmentAnalysis* loop_assignment() const { return loop_assignment_; }
190 void set_loop_assignment(LoopAssignmentAnalysis* loop_assignment) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000191 DCHECK(!loop_assignment_);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400192 loop_assignment_ = loop_assignment;
193 }
194
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000195 TypeHintAnalysis* type_hint_analysis() const { return type_hint_analysis_; }
196 void set_type_hint_analysis(TypeHintAnalysis* type_hint_analysis) {
197 DCHECK_NULL(type_hint_analysis_);
198 type_hint_analysis_ = type_hint_analysis;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400199 }
200
201 Schedule* schedule() const { return schedule_; }
202 void set_schedule(Schedule* schedule) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000203 DCHECK(!schedule_);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400204 schedule_ = schedule;
205 }
Ben Murdochc5610432016-08-08 18:44:38 +0100206 void reset_schedule() { schedule_ = nullptr; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400207
208 Zone* instruction_zone() const { return instruction_zone_; }
209 InstructionSequence* sequence() const { return sequence_; }
210 Frame* frame() const { return frame_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000211
212 Zone* register_allocation_zone() const { return register_allocation_zone_; }
213 RegisterAllocationData* register_allocation_data() const {
214 return register_allocation_data_;
215 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400216
Ben Murdochc5610432016-08-08 18:44:38 +0100217 BasicBlockProfiler::Data* profiler_data() const { return profiler_data_; }
218 void set_profiler_data(BasicBlockProfiler::Data* profiler_data) {
219 profiler_data_ = profiler_data;
220 }
221
222 std::string const& source_position_output() const {
223 return source_position_output_;
224 }
225 void set_source_position_output(std::string const& source_position_output) {
226 source_position_output_ = source_position_output;
227 }
228
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400229 void DeleteGraphZone() {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400230 if (graph_zone_ == nullptr) return;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400231 graph_zone_scope_.Destroy();
232 graph_zone_ = nullptr;
233 graph_ = nullptr;
Ben Murdochc5610432016-08-08 18:44:38 +0100234 source_positions_ = nullptr;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400235 loop_assignment_ = nullptr;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000236 type_hint_analysis_ = nullptr;
237 simplified_ = nullptr;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400238 machine_ = nullptr;
239 common_ = nullptr;
240 javascript_ = nullptr;
241 jsgraph_ = nullptr;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400242 schedule_ = nullptr;
243 }
244
245 void DeleteInstructionZone() {
246 if (instruction_zone_ == nullptr) return;
247 instruction_zone_scope_.Destroy();
248 instruction_zone_ = nullptr;
249 sequence_ = nullptr;
250 frame_ = nullptr;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000251 }
252
253 void DeleteRegisterAllocationZone() {
254 if (register_allocation_zone_ == nullptr) return;
255 register_allocation_zone_scope_.Destroy();
256 register_allocation_zone_ = nullptr;
257 register_allocation_data_ = nullptr;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400258 }
259
Ben Murdochda12d292016-06-02 14:46:10 +0100260 void InitializeInstructionSequence(const CallDescriptor* descriptor) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000261 DCHECK(sequence_ == nullptr);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400262 InstructionBlocks* instruction_blocks =
263 InstructionSequence::InstructionBlocksFor(instruction_zone(),
264 schedule());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000265 sequence_ = new (instruction_zone()) InstructionSequence(
266 info()->isolate(), instruction_zone(), instruction_blocks);
Ben Murdochda12d292016-06-02 14:46:10 +0100267 if (descriptor && descriptor->RequiresFrameAsIncoming()) {
268 sequence_->instruction_blocks()[0]->mark_needs_frame();
269 } else {
270 DCHECK_EQ(0, descriptor->CalleeSavedFPRegisters());
271 DCHECK_EQ(0, descriptor->CalleeSavedRegisters());
272 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400273 }
274
Ben Murdoch097c5b22016-05-18 11:27:45 +0100275 void InitializeFrameData(CallDescriptor* descriptor) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000276 DCHECK(frame_ == nullptr);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000277 int fixed_frame_size = 0;
278 if (descriptor != nullptr) {
Ben Murdochda12d292016-06-02 14:46:10 +0100279 fixed_frame_size = CalculateFixedFrameSize(descriptor);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000280 }
Ben Murdochc5610432016-08-08 18:44:38 +0100281 frame_ = new (instruction_zone()) Frame(fixed_frame_size);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100282 }
283
284 void InitializeRegisterAllocationData(const RegisterConfiguration* config,
Ben Murdochc5610432016-08-08 18:44:38 +0100285 CallDescriptor* descriptor) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100286 DCHECK(register_allocation_data_ == nullptr);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000287 register_allocation_data_ = new (register_allocation_zone())
288 RegisterAllocationData(config, register_allocation_zone(), frame(),
Ben Murdochc5610432016-08-08 18:44:38 +0100289 sequence(), debug_name_.get());
290 }
291
292 void BeginPhaseKind(const char* phase_kind_name) {
293 if (pipeline_statistics() != nullptr) {
294 pipeline_statistics()->BeginPhaseKind(phase_kind_name);
295 }
296 }
297
298 void EndPhaseKind() {
299 if (pipeline_statistics() != nullptr) {
300 pipeline_statistics()->EndPhaseKind();
301 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000302 }
303
304 private:
Ben Murdochc5610432016-08-08 18:44:38 +0100305 Isolate* const isolate_;
306 CompilationInfo* const info_;
307 base::SmartArrayPointer<char> debug_name_;
308 Zone* outer_zone_ = nullptr;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400309 ZonePool* const zone_pool_;
Ben Murdochc5610432016-08-08 18:44:38 +0100310 PipelineStatistics* pipeline_statistics_ = nullptr;
311 bool compilation_failed_ = false;
312 Handle<Code> code_ = Handle<Code>::null();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400313
314 // All objects in the following group of fields are allocated in graph_zone_.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000315 // They are all set to nullptr when the graph_zone_ is destroyed.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400316 ZonePool::Scope graph_zone_scope_;
Ben Murdochc5610432016-08-08 18:44:38 +0100317 Zone* graph_zone_ = nullptr;
318 Graph* graph_ = nullptr;
319 SourcePositionTable* source_positions_ = nullptr;
320 LoopAssignmentAnalysis* loop_assignment_ = nullptr;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000321 TypeHintAnalysis* type_hint_analysis_ = nullptr;
Ben Murdochc5610432016-08-08 18:44:38 +0100322 SimplifiedOperatorBuilder* simplified_ = nullptr;
323 MachineOperatorBuilder* machine_ = nullptr;
324 CommonOperatorBuilder* common_ = nullptr;
325 JSOperatorBuilder* javascript_ = nullptr;
326 JSGraph* jsgraph_ = nullptr;
327 Schedule* schedule_ = nullptr;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400328
329 // All objects in the following group of fields are allocated in
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000330 // instruction_zone_. They are all set to nullptr when the instruction_zone_
331 // is
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400332 // destroyed.
333 ZonePool::Scope instruction_zone_scope_;
334 Zone* instruction_zone_;
Ben Murdochc5610432016-08-08 18:44:38 +0100335 InstructionSequence* sequence_ = nullptr;
336 Frame* frame_ = nullptr;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000337
338 // All objects in the following group of fields are allocated in
339 // register_allocation_zone_. They are all set to nullptr when the zone is
340 // destroyed.
341 ZonePool::Scope register_allocation_zone_scope_;
342 Zone* register_allocation_zone_;
Ben Murdochc5610432016-08-08 18:44:38 +0100343 RegisterAllocationData* register_allocation_data_ = nullptr;
344
345 // Basic block profiling support.
346 BasicBlockProfiler::Data* profiler_data_ = nullptr;
347
348 // Source position output for --trace-turbo.
349 std::string source_position_output_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400350
Ben Murdochda12d292016-06-02 14:46:10 +0100351 int CalculateFixedFrameSize(CallDescriptor* descriptor) {
352 if (descriptor->IsJSFunctionCall()) {
353 return StandardFrameConstants::kFixedSlotCount;
354 }
355 return descriptor->IsCFunctionCall()
356 ? (CommonFrameConstants::kFixedSlotCountAboveFp +
357 CommonFrameConstants::kCPSlotCount)
358 : TypedFrameConstants::kFixedSlotCount;
359 }
360
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400361 DISALLOW_COPY_AND_ASSIGN(PipelineData);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000362};
363
Ben Murdochc5610432016-08-08 18:44:38 +0100364class PipelineImpl final {
365 public:
366 explicit PipelineImpl(PipelineData* data) : data_(data) {}
367
368 // Helpers for executing pipeline phases.
369 template <typename Phase>
370 void Run();
371 template <typename Phase, typename Arg0>
372 void Run(Arg0 arg_0);
373 template <typename Phase, typename Arg0, typename Arg1>
374 void Run(Arg0 arg_0, Arg1 arg_1);
375
376 // Run the graph creation and initial optimization passes.
377 bool CreateGraph();
378
379 // Run the concurrent optimization passes.
380 bool OptimizeGraph(Linkage* linkage);
381
382 // Perform the actual code generation and return handle to a code object.
383 Handle<Code> GenerateCode(Linkage* linkage);
384
385 bool ScheduleAndSelectInstructions(Linkage* linkage);
386 void RunPrintAndVerify(const char* phase, bool untyped = false);
387 Handle<Code> ScheduleAndGenerateCode(CallDescriptor* call_descriptor);
388 void AllocateRegisters(const RegisterConfiguration* config,
389 CallDescriptor* descriptor, bool run_verifier);
390
391 CompilationInfo* info() const;
392 Isolate* isolate() const;
393
394 PipelineData* const data_;
395};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000396
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000397namespace {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000398
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400399struct TurboCfgFile : public std::ofstream {
400 explicit TurboCfgFile(Isolate* isolate)
401 : std::ofstream(isolate->GetTurboCfgFileName().c_str(),
402 std::ios_base::app) {}
403};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000404
Ben Murdochc5610432016-08-08 18:44:38 +0100405struct TurboJsonFile : public std::ofstream {
406 TurboJsonFile(CompilationInfo* info, std::ios_base::openmode mode)
407 : std::ofstream(GetVisualizerLogFileName(info, nullptr, "json").get(),
408 mode) {}
409};
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400410
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000411void TraceSchedule(CompilationInfo* info, Schedule* schedule) {
412 if (FLAG_trace_turbo) {
Ben Murdochc5610432016-08-08 18:44:38 +0100413 AllowHandleDereference allow_deref;
414 TurboJsonFile json_of(info, std::ios_base::app);
415 json_of << "{\"name\":\"Schedule\",\"type\":\"schedule\",\"data\":\"";
416 std::stringstream schedule_stream;
417 schedule_stream << *schedule;
418 std::string schedule_string(schedule_stream.str());
419 for (const auto& c : schedule_string) {
420 json_of << AsEscapedUC16ForJSON(c);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000421 }
Ben Murdochc5610432016-08-08 18:44:38 +0100422 json_of << "\"},\n";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000423 }
Ben Murdochc5610432016-08-08 18:44:38 +0100424 if (FLAG_trace_turbo_graph || FLAG_trace_turbo_scheduler) {
425 AllowHandleDereference allow_deref;
426 OFStream os(stdout);
427 os << "-- Schedule --------------------------------------\n" << *schedule;
428 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400429}
430
431
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000432class AstGraphBuilderWithPositions final : public AstGraphBuilder {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000433 public:
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400434 AstGraphBuilderWithPositions(Zone* local_zone, CompilationInfo* info,
435 JSGraph* jsgraph,
436 LoopAssignmentAnalysis* loop_assignment,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000437 TypeHintAnalysis* type_hint_analysis,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400438 SourcePositionTable* source_positions)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000439 : AstGraphBuilder(local_zone, info, jsgraph, loop_assignment,
440 type_hint_analysis),
441 source_positions_(source_positions),
442 start_position_(info->shared_info()->start_position()) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000443
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000444 bool CreateGraph(bool stack_check) {
445 SourcePositionTable::Scope pos_scope(source_positions_, start_position_);
446 return AstGraphBuilder::CreateGraph(stack_check);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000447 }
448
449#define DEF_VISIT(type) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000450 void Visit##type(type* node) override { \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000451 SourcePositionTable::Scope pos(source_positions_, \
452 SourcePosition(node->position())); \
453 AstGraphBuilder::Visit##type(node); \
454 }
455 AST_NODE_LIST(DEF_VISIT)
456#undef DEF_VISIT
457
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000458 private:
459 SourcePositionTable* const source_positions_;
460 SourcePosition const start_position_;
461};
462
463
464class SourcePositionWrapper final : public Reducer {
465 public:
466 SourcePositionWrapper(Reducer* reducer, SourcePositionTable* table)
467 : reducer_(reducer), table_(table) {}
468 ~SourcePositionWrapper() final {}
469
470 Reduction Reduce(Node* node) final {
471 SourcePosition const pos = table_->GetSourcePosition(node);
472 SourcePositionTable::Scope position(table_, pos);
473 return reducer_->Reduce(node);
474 }
475
476 void Finalize() final { reducer_->Finalize(); }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400477
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000478 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000479 Reducer* const reducer_;
480 SourcePositionTable* const table_;
481
482 DISALLOW_COPY_AND_ASSIGN(SourcePositionWrapper);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000483};
484
485
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000486class JSGraphReducer final : public GraphReducer {
487 public:
488 JSGraphReducer(JSGraph* jsgraph, Zone* zone)
489 : GraphReducer(zone, jsgraph->graph(), jsgraph->Dead()) {}
490 ~JSGraphReducer() final {}
491};
492
493
494void AddReducer(PipelineData* data, GraphReducer* graph_reducer,
495 Reducer* reducer) {
496 if (data->info()->is_source_positions_enabled()) {
497 void* const buffer = data->graph_zone()->New(sizeof(SourcePositionWrapper));
498 SourcePositionWrapper* const wrapper =
499 new (buffer) SourcePositionWrapper(reducer, data->source_positions());
500 graph_reducer->AddReducer(wrapper);
501 } else {
502 graph_reducer->AddReducer(reducer);
503 }
504}
505
506
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400507class PipelineRunScope {
508 public:
509 PipelineRunScope(PipelineData* data, const char* phase_name)
510 : phase_scope_(
511 phase_name == nullptr ? nullptr : data->pipeline_statistics(),
512 phase_name),
513 zone_scope_(data->zone_pool()) {}
514
515 Zone* zone() { return zone_scope_.zone(); }
516
517 private:
518 PhaseScope phase_scope_;
519 ZonePool::Scope zone_scope_;
520};
521
Ben Murdochc5610432016-08-08 18:44:38 +0100522PipelineStatistics* CreatePipelineStatistics(CompilationInfo* info,
523 ZonePool* zone_pool) {
524 PipelineStatistics* pipeline_statistics = nullptr;
525
Ben Murdoch61f157c2016-09-16 13:49:30 +0100526 if (FLAG_turbo_stats || FLAG_turbo_stats_nvp) {
Ben Murdochc5610432016-08-08 18:44:38 +0100527 pipeline_statistics = new PipelineStatistics(info, zone_pool);
528 pipeline_statistics->BeginPhaseKind("initializing");
529 }
530
531 if (FLAG_trace_turbo) {
532 TurboJsonFile json_of(info, std::ios_base::trunc);
533 Handle<Script> script = info->script();
534 base::SmartArrayPointer<char> function_name = info->GetDebugName();
535 int pos = info->shared_info()->start_position();
536 json_of << "{\"function\":\"" << function_name.get()
537 << "\", \"sourcePosition\":" << pos << ", \"source\":\"";
Ben Murdoch61f157c2016-09-16 13:49:30 +0100538 Isolate* isolate = info->isolate();
539 if (!script->IsUndefined(isolate) &&
540 !script->source()->IsUndefined(isolate)) {
Ben Murdochc5610432016-08-08 18:44:38 +0100541 DisallowHeapAllocation no_allocation;
542 int start = info->shared_info()->start_position();
543 int len = info->shared_info()->end_position() - start;
544 String::SubStringRange source(String::cast(script->source()), start, len);
545 for (const auto& c : source) {
546 json_of << AsEscapedUC16ForJSON(c);
547 }
548 }
549 json_of << "\",\n\"phases\":[";
550 }
551
552 return pipeline_statistics;
553}
554
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000555} // namespace
556
Ben Murdochc5610432016-08-08 18:44:38 +0100557class PipelineCompilationJob final : public CompilationJob {
558 public:
559 PipelineCompilationJob(Isolate* isolate, Handle<JSFunction> function)
560 // Note that the CompilationInfo is not initialized at the time we pass it
561 // to the CompilationJob constructor, but it is not dereferenced there.
562 : CompilationJob(&info_, "TurboFan"),
563 zone_(isolate->allocator()),
564 zone_pool_(isolate->allocator()),
565 parse_info_(&zone_, function),
566 info_(&parse_info_, function),
567 pipeline_statistics_(CreatePipelineStatistics(info(), &zone_pool_)),
568 data_(&zone_pool_, info(), pipeline_statistics_.get()),
569 pipeline_(&data_),
570 linkage_(nullptr) {}
571
572 protected:
573 Status CreateGraphImpl() final;
574 Status OptimizeGraphImpl() final;
575 Status GenerateCodeImpl() final;
576
577 private:
578 Zone zone_;
579 ZonePool zone_pool_;
580 ParseInfo parse_info_;
581 CompilationInfo info_;
582 base::SmartPointer<PipelineStatistics> pipeline_statistics_;
583 PipelineData data_;
584 PipelineImpl pipeline_;
585 Linkage* linkage_;
586};
587
588PipelineCompilationJob::Status PipelineCompilationJob::CreateGraphImpl() {
589 if (info()->shared_info()->asm_function()) {
590 if (info()->osr_frame()) info()->MarkAsFrameSpecializing();
591 info()->MarkAsFunctionContextSpecializing();
592 } else {
593 if (!FLAG_always_opt) {
594 info()->MarkAsBailoutOnUninitialized();
595 }
596 if (FLAG_native_context_specialization) {
597 info()->MarkAsNativeContextSpecializing();
598 }
599 }
600 if (!info()->shared_info()->asm_function() || FLAG_turbo_asm_deoptimization) {
601 info()->MarkAsDeoptimizationEnabled();
602 }
603 if (!info()->is_optimizing_from_bytecode()) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100604 if (info()->is_deoptimization_enabled() && FLAG_turbo_type_feedback) {
605 info()->MarkAsTypeFeedbackEnabled();
606 }
Ben Murdochc5610432016-08-08 18:44:38 +0100607 if (!Compiler::EnsureDeoptimizationSupport(info())) return FAILED;
608 }
609
610 linkage_ = new (&zone_) Linkage(Linkage::ComputeIncoming(&zone_, info()));
611
612 if (!pipeline_.CreateGraph()) {
613 if (isolate()->has_pending_exception()) return FAILED; // Stack overflowed.
614 return AbortOptimization(kGraphBuildingFailed);
615 }
616
617 return SUCCEEDED;
618}
619
620PipelineCompilationJob::Status PipelineCompilationJob::OptimizeGraphImpl() {
621 if (!pipeline_.OptimizeGraph(linkage_)) return FAILED;
622 return SUCCEEDED;
623}
624
625PipelineCompilationJob::Status PipelineCompilationJob::GenerateCodeImpl() {
626 Handle<Code> code = pipeline_.GenerateCode(linkage_);
627 if (code.is_null()) {
628 if (info()->bailout_reason() == kNoReason) {
629 return AbortOptimization(kCodeGenerationFailed);
630 }
631 return FAILED;
632 }
633 info()->dependencies()->Commit(code);
634 info()->SetCode(code);
635 if (info()->is_deoptimization_enabled()) {
636 info()->context()->native_context()->AddOptimizedCode(*code);
637 RegisterWeakObjectsInOptimizedCode(code);
638 }
639 return SUCCEEDED;
640}
641
642class PipelineWasmCompilationJob final : public CompilationJob {
643 public:
644 explicit PipelineWasmCompilationJob(CompilationInfo* info, Graph* graph,
645 CallDescriptor* descriptor,
646 SourcePositionTable* source_positions)
647 : CompilationJob(info, "TurboFan"),
648 zone_pool_(info->isolate()->allocator()),
649 data_(&zone_pool_, info, graph, source_positions),
650 pipeline_(&data_),
651 linkage_(descriptor) {}
652
653 protected:
654 Status CreateGraphImpl() final;
655 Status OptimizeGraphImpl() final;
656 Status GenerateCodeImpl() final;
657
658 private:
659 ZonePool zone_pool_;
660 PipelineData data_;
661 PipelineImpl pipeline_;
662 Linkage linkage_;
663};
664
665PipelineWasmCompilationJob::Status
666PipelineWasmCompilationJob::CreateGraphImpl() {
667 return SUCCEEDED;
668}
669
670PipelineWasmCompilationJob::Status
671PipelineWasmCompilationJob::OptimizeGraphImpl() {
672 if (FLAG_trace_turbo) {
673 TurboJsonFile json_of(info(), std::ios_base::trunc);
674 json_of << "{\"function\":\"" << info()->GetDebugName().get()
675 << "\", \"source\":\"\",\n\"phases\":[";
676 }
677
678 pipeline_.RunPrintAndVerify("Machine", true);
679
680 if (!pipeline_.ScheduleAndSelectInstructions(&linkage_)) return FAILED;
681 return SUCCEEDED;
682}
683
684PipelineWasmCompilationJob::Status
685PipelineWasmCompilationJob::GenerateCodeImpl() {
686 pipeline_.GenerateCode(&linkage_);
687 return SUCCEEDED;
688}
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400689
690template <typename Phase>
Ben Murdochc5610432016-08-08 18:44:38 +0100691void PipelineImpl::Run() {
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());
695}
696
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400697template <typename Phase, typename Arg0>
Ben Murdochc5610432016-08-08 18:44:38 +0100698void PipelineImpl::Run(Arg0 arg_0) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400699 PipelineRunScope scope(this->data_, Phase::phase_name());
700 Phase phase;
701 phase.Run(this->data_, scope.zone(), arg_0);
702}
703
Ben Murdochc5610432016-08-08 18:44:38 +0100704template <typename Phase, typename Arg0, typename Arg1>
705void PipelineImpl::Run(Arg0 arg_0, Arg1 arg_1) {
706 PipelineRunScope scope(this->data_, Phase::phase_name());
707 Phase phase;
708 phase.Run(this->data_, scope.zone(), arg_0, arg_1);
709}
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400710
711struct LoopAssignmentAnalysisPhase {
712 static const char* phase_name() { return "loop assignment analysis"; }
713
714 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdochc5610432016-08-08 18:44:38 +0100715 if (!data->info()->is_optimizing_from_bytecode()) {
716 AstLoopAssignmentAnalyzer analyzer(data->graph_zone(), data->info());
717 LoopAssignmentAnalysis* loop_assignment = analyzer.Analyze();
718 data->set_loop_assignment(loop_assignment);
719 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400720 }
721};
722
723
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000724struct TypeHintAnalysisPhase {
725 static const char* phase_name() { return "type hint analysis"; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400726
727 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100728 if (data->info()->is_type_feedback_enabled()) {
Ben Murdochc5610432016-08-08 18:44:38 +0100729 TypeHintAnalyzer analyzer(data->graph_zone());
730 Handle<Code> code(data->info()->shared_info()->code(), data->isolate());
731 TypeHintAnalysis* type_hint_analysis = analyzer.Analyze(code);
732 data->set_type_hint_analysis(type_hint_analysis);
733 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400734 }
735};
736
737
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000738struct GraphBuilderPhase {
739 static const char* phase_name() { return "graph builder"; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400740
741 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000742 bool stack_check = !data->info()->IsStub();
743 bool succeeded = false;
744
Ben Murdochc5610432016-08-08 18:44:38 +0100745 if (data->info()->is_optimizing_from_bytecode()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000746 BytecodeGraphBuilder graph_builder(temp_zone, data->info(),
747 data->jsgraph());
Ben Murdoch097c5b22016-05-18 11:27:45 +0100748 succeeded = graph_builder.CreateGraph();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000749 } else {
750 AstGraphBuilderWithPositions graph_builder(
751 temp_zone, data->info(), data->jsgraph(), data->loop_assignment(),
752 data->type_hint_analysis(), data->source_positions());
753 succeeded = graph_builder.CreateGraph(stack_check);
754 }
755
756 if (!succeeded) {
757 data->set_compilation_failed();
758 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400759 }
760};
761
762
763struct InliningPhase {
764 static const char* phase_name() { return "inlining"; }
765
766 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000767 JSGraphReducer graph_reducer(data->jsgraph(), temp_zone);
768 DeadCodeElimination dead_code_elimination(&graph_reducer, data->graph(),
769 data->common());
770 CommonOperatorReducer common_reducer(&graph_reducer, data->graph(),
771 data->common(), data->machine());
Ben Murdochda12d292016-06-02 14:46:10 +0100772 JSCallReducer call_reducer(data->jsgraph(),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000773 data->info()->is_deoptimization_enabled()
774 ? JSCallReducer::kDeoptimizationEnabled
775 : JSCallReducer::kNoFlags,
776 data->native_context());
777 JSContextSpecialization context_specialization(
778 &graph_reducer, data->jsgraph(),
779 data->info()->is_function_context_specializing()
780 ? data->info()->context()
781 : MaybeHandle<Context>());
782 JSFrameSpecialization frame_specialization(data->info()->osr_frame(),
783 data->jsgraph());
784 JSGlobalObjectSpecialization global_object_specialization(
Ben Murdoch097c5b22016-05-18 11:27:45 +0100785 &graph_reducer, data->jsgraph(), data->native_context(),
786 data->info()->dependencies());
787 JSNativeContextSpecialization::Flags flags =
788 JSNativeContextSpecialization::kNoFlags;
789 if (data->info()->is_bailout_on_uninitialized()) {
790 flags |= JSNativeContextSpecialization::kBailoutOnUninitialized;
791 }
792 if (data->info()->is_deoptimization_enabled()) {
793 flags |= JSNativeContextSpecialization::kDeoptimizationEnabled;
794 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000795 JSNativeContextSpecialization native_context_specialization(
Ben Murdoch097c5b22016-05-18 11:27:45 +0100796 &graph_reducer, data->jsgraph(), flags, data->native_context(),
797 data->info()->dependencies(), temp_zone);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000798 JSInliningHeuristic inlining(&graph_reducer,
799 data->info()->is_inlining_enabled()
800 ? JSInliningHeuristic::kGeneralInlining
801 : JSInliningHeuristic::kRestrictedInlining,
802 temp_zone, data->info(), data->jsgraph());
803 AddReducer(data, &graph_reducer, &dead_code_elimination);
804 AddReducer(data, &graph_reducer, &common_reducer);
805 if (data->info()->is_frame_specializing()) {
806 AddReducer(data, &graph_reducer, &frame_specialization);
807 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100808 if (data->info()->is_deoptimization_enabled()) {
809 AddReducer(data, &graph_reducer, &global_object_specialization);
810 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000811 AddReducer(data, &graph_reducer, &native_context_specialization);
812 AddReducer(data, &graph_reducer, &context_specialization);
813 AddReducer(data, &graph_reducer, &call_reducer);
Ben Murdoch61f157c2016-09-16 13:49:30 +0100814 if (!data->info()->is_optimizing_from_bytecode()) {
815 AddReducer(data, &graph_reducer, &inlining);
816 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000817 graph_reducer.ReduceGraph();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400818 }
819};
820
821
822struct TyperPhase {
823 static const char* phase_name() { return "typer"; }
824
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000825 void Run(PipelineData* data, Zone* temp_zone, Typer* typer) {
826 NodeVector roots(temp_zone);
827 data->jsgraph()->GetCachedNodes(&roots);
828 typer->Run(roots);
829 }
830};
831
Ben Murdochc5610432016-08-08 18:44:38 +0100832#ifdef DEBUG
833
834struct UntyperPhase {
835 static const char* phase_name() { return "untyper"; }
836
837 void Run(PipelineData* data, Zone* temp_zone) {
838 class RemoveTypeReducer final : public Reducer {
839 public:
840 Reduction Reduce(Node* node) final {
841 if (NodeProperties::IsTyped(node)) {
842 NodeProperties::RemoveType(node);
843 return Changed(node);
844 }
845 return NoChange();
846 }
847 };
848
849 JSGraphReducer graph_reducer(data->jsgraph(), temp_zone);
850 RemoveTypeReducer remove_type_reducer;
851 AddReducer(data, &graph_reducer, &remove_type_reducer);
852 graph_reducer.ReduceGraph();
853 }
854};
855
856#endif // DEBUG
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000857
858struct OsrDeconstructionPhase {
859 static const char* phase_name() { return "OSR deconstruction"; }
860
861 void Run(PipelineData* data, Zone* temp_zone) {
862 OsrHelper osr_helper(data->info());
863 osr_helper.Deconstruct(data->jsgraph(), data->common(), temp_zone);
864 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400865};
866
867
868struct TypedLoweringPhase {
869 static const char* phase_name() { return "typed lowering"; }
870
871 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000872 JSGraphReducer graph_reducer(data->jsgraph(), temp_zone);
873 DeadCodeElimination dead_code_elimination(&graph_reducer, data->graph(),
874 data->common());
Ben Murdochda12d292016-06-02 14:46:10 +0100875 LoadElimination load_elimination(&graph_reducer, data->graph(),
Ben Murdochc5610432016-08-08 18:44:38 +0100876 data->jsgraph()->simplified());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000877 JSBuiltinReducer builtin_reducer(&graph_reducer, data->jsgraph());
Ben Murdoch097c5b22016-05-18 11:27:45 +0100878 MaybeHandle<LiteralsArray> literals_array =
879 data->info()->is_native_context_specializing()
880 ? handle(data->info()->closure()->literals(), data->isolate())
881 : MaybeHandle<LiteralsArray>();
882 JSCreateLowering create_lowering(
883 &graph_reducer, data->info()->dependencies(), data->jsgraph(),
884 literals_array, temp_zone);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000885 JSTypedLowering::Flags typed_lowering_flags = JSTypedLowering::kNoFlags;
886 if (data->info()->is_deoptimization_enabled()) {
887 typed_lowering_flags |= JSTypedLowering::kDeoptimizationEnabled;
888 }
889 if (data->info()->shared_info()->HasBytecodeArray()) {
890 typed_lowering_flags |= JSTypedLowering::kDisableBinaryOpReduction;
891 }
Ben Murdoch61f157c2016-09-16 13:49:30 +0100892 if (data->info()->is_type_feedback_enabled()) {
893 typed_lowering_flags |= JSTypedLowering::kTypeFeedbackEnabled;
894 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000895 JSTypedLowering typed_lowering(&graph_reducer, data->info()->dependencies(),
896 typed_lowering_flags, data->jsgraph(),
897 temp_zone);
898 JSIntrinsicLowering intrinsic_lowering(
899 &graph_reducer, data->jsgraph(),
900 data->info()->is_deoptimization_enabled()
901 ? JSIntrinsicLowering::kDeoptimizationEnabled
902 : JSIntrinsicLowering::kDeoptimizationDisabled);
Ben Murdoch61f157c2016-09-16 13:49:30 +0100903 SimplifiedOperatorReducer simple_reducer(&graph_reducer, data->jsgraph());
904 CheckpointElimination checkpoint_elimination(&graph_reducer);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000905 CommonOperatorReducer common_reducer(&graph_reducer, data->graph(),
906 data->common(), data->machine());
907 AddReducer(data, &graph_reducer, &dead_code_elimination);
908 AddReducer(data, &graph_reducer, &builtin_reducer);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100909 if (data->info()->is_deoptimization_enabled()) {
910 AddReducer(data, &graph_reducer, &create_lowering);
911 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000912 AddReducer(data, &graph_reducer, &typed_lowering);
913 AddReducer(data, &graph_reducer, &intrinsic_lowering);
914 AddReducer(data, &graph_reducer, &load_elimination);
Ben Murdochda12d292016-06-02 14:46:10 +0100915 AddReducer(data, &graph_reducer, &simple_reducer);
Ben Murdoch61f157c2016-09-16 13:49:30 +0100916 AddReducer(data, &graph_reducer, &checkpoint_elimination);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000917 AddReducer(data, &graph_reducer, &common_reducer);
918 graph_reducer.ReduceGraph();
919 }
920};
921
922
923struct BranchEliminationPhase {
924 static const char* phase_name() { return "branch condition elimination"; }
925
926 void Run(PipelineData* data, Zone* temp_zone) {
927 JSGraphReducer graph_reducer(data->jsgraph(), temp_zone);
928 BranchElimination branch_condition_elimination(&graph_reducer,
929 data->jsgraph(), temp_zone);
930 DeadCodeElimination dead_code_elimination(&graph_reducer, data->graph(),
931 data->common());
932 AddReducer(data, &graph_reducer, &branch_condition_elimination);
933 AddReducer(data, &graph_reducer, &dead_code_elimination);
934 graph_reducer.ReduceGraph();
935 }
936};
937
938
939struct EscapeAnalysisPhase {
940 static const char* phase_name() { return "escape analysis"; }
941
942 void Run(PipelineData* data, Zone* temp_zone) {
943 EscapeAnalysis escape_analysis(data->graph(), data->jsgraph()->common(),
944 temp_zone);
945 escape_analysis.Run();
946 JSGraphReducer graph_reducer(data->jsgraph(), temp_zone);
947 EscapeAnalysisReducer escape_reducer(&graph_reducer, data->jsgraph(),
948 &escape_analysis, temp_zone);
949 AddReducer(data, &graph_reducer, &escape_reducer);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400950 graph_reducer.ReduceGraph();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100951 escape_reducer.VerifyReplacement();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400952 }
953};
954
Ben Murdochc5610432016-08-08 18:44:38 +0100955struct RepresentationSelectionPhase {
956 static const char* phase_name() { return "representation selection"; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400957
958 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100959 SimplifiedLowering::Flags flags =
960 data->info()->is_type_feedback_enabled()
961 ? SimplifiedLowering::kTypeFeedbackEnabled
962 : SimplifiedLowering::kNoFlag;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000963 SimplifiedLowering lowering(data->jsgraph(), temp_zone,
Ben Murdoch61f157c2016-09-16 13:49:30 +0100964 data->source_positions(), flags);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400965 lowering.LowerAllNodes();
Ben Murdochc5610432016-08-08 18:44:38 +0100966 }
967};
Ben Murdoch097c5b22016-05-18 11:27:45 +0100968
Ben Murdochc5610432016-08-08 18:44:38 +0100969struct EarlyOptimizationPhase {
970 static const char* phase_name() { return "early optimization"; }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100971
Ben Murdochc5610432016-08-08 18:44:38 +0100972 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000973 JSGraphReducer graph_reducer(data->jsgraph(), temp_zone);
Ben Murdochc5610432016-08-08 18:44:38 +0100974 JSGenericLowering generic_lowering(data->jsgraph());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000975 DeadCodeElimination dead_code_elimination(&graph_reducer, data->graph(),
976 data->common());
Ben Murdoch61f157c2016-09-16 13:49:30 +0100977 SimplifiedOperatorReducer simple_reducer(&graph_reducer, data->jsgraph());
978 RedundancyElimination redundancy_elimination(&graph_reducer, temp_zone);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000979 ValueNumberingReducer value_numbering(temp_zone);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400980 MachineOperatorReducer machine_reducer(data->jsgraph());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000981 CommonOperatorReducer common_reducer(&graph_reducer, data->graph(),
982 data->common(), data->machine());
983 AddReducer(data, &graph_reducer, &dead_code_elimination);
984 AddReducer(data, &graph_reducer, &simple_reducer);
Ben Murdoch61f157c2016-09-16 13:49:30 +0100985 AddReducer(data, &graph_reducer, &redundancy_elimination);
Ben Murdochc5610432016-08-08 18:44:38 +0100986 AddReducer(data, &graph_reducer, &generic_lowering);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000987 AddReducer(data, &graph_reducer, &value_numbering);
988 AddReducer(data, &graph_reducer, &machine_reducer);
989 AddReducer(data, &graph_reducer, &common_reducer);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400990 graph_reducer.ReduceGraph();
991 }
992};
993
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000994struct ControlFlowOptimizationPhase {
995 static const char* phase_name() { return "control flow optimization"; }
996
997 void Run(PipelineData* data, Zone* temp_zone) {
998 ControlFlowOptimizer optimizer(data->graph(), data->common(),
999 data->machine(), temp_zone);
1000 optimizer.Optimize();
1001 }
1002};
1003
Ben Murdochc5610432016-08-08 18:44:38 +01001004struct EffectControlLinearizationPhase {
1005 static const char* phase_name() { return "effect linearization"; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001006
Ben Murdochc5610432016-08-08 18:44:38 +01001007 void Run(PipelineData* data, Zone* temp_zone) {
1008 // The scheduler requires the graphs to be trimmed, so trim now.
1009 // TODO(jarin) Remove the trimming once the scheduler can handle untrimmed
1010 // graphs.
1011 GraphTrimmer trimmer(temp_zone, data->graph());
1012 NodeVector roots(temp_zone);
1013 data->jsgraph()->GetCachedNodes(&roots);
1014 trimmer.TrimGraph(roots.begin(), roots.end());
1015
1016 // Schedule the graph without node splitting so that we can
1017 // fix the effect and control flow for nodes with low-level side
1018 // effects (such as changing representation to tagged or
1019 // 'floating' allocation regions.)
1020 Schedule* schedule = Scheduler::ComputeSchedule(temp_zone, data->graph(),
1021 Scheduler::kNoFlags);
1022 if (FLAG_turbo_verify) ScheduleVerifier::Run(schedule);
1023 TraceSchedule(data->info(), schedule);
1024
1025 // Post-pass for wiring the control/effects
1026 // - connect allocating representation changes into the control&effect
1027 // chains and lower them,
1028 // - get rid of the region markers,
1029 // - introduce effect phis and rewire effects to get SSA again.
1030 EffectControlLinearizer linearizer(data->jsgraph(), schedule, temp_zone);
1031 linearizer.Run();
1032 }
1033};
1034
Ben Murdoch61f157c2016-09-16 13:49:30 +01001035struct StoreStoreEliminationPhase {
1036 static const char* phase_name() { return "Store-store elimination"; }
1037
1038 void Run(PipelineData* data, Zone* temp_zone) {
1039 StoreStoreElimination store_store_elimination(data->jsgraph(), temp_zone);
1040 store_store_elimination.Run();
1041 }
1042};
1043
Ben Murdochc5610432016-08-08 18:44:38 +01001044struct MemoryOptimizationPhase {
1045 static const char* phase_name() { return "memory optimization"; }
1046
1047 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001048 // The memory optimizer requires the graphs to be trimmed, so trim now.
1049 GraphTrimmer trimmer(temp_zone, data->graph());
1050 NodeVector roots(temp_zone);
1051 data->jsgraph()->GetCachedNodes(&roots);
1052 trimmer.TrimGraph(roots.begin(), roots.end());
1053
1054 // Optimize allocations and load/store operations.
Ben Murdochc5610432016-08-08 18:44:38 +01001055 MemoryOptimizer optimizer(data->jsgraph(), temp_zone);
1056 optimizer.Optimize();
1057 }
1058};
1059
1060struct LateOptimizationPhase {
1061 static const char* phase_name() { return "late optimization"; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001062
1063 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001064 JSGraphReducer graph_reducer(data->jsgraph(), temp_zone);
1065 DeadCodeElimination dead_code_elimination(&graph_reducer, data->graph(),
1066 data->common());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001067 ValueNumberingReducer value_numbering(temp_zone);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001068 MachineOperatorReducer machine_reducer(data->jsgraph());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001069 CommonOperatorReducer common_reducer(&graph_reducer, data->graph(),
1070 data->common(), data->machine());
Ben Murdochc5610432016-08-08 18:44:38 +01001071 SelectLowering select_lowering(data->jsgraph()->graph(),
1072 data->jsgraph()->common());
1073 TailCallOptimization tco(data->common(), data->graph());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001074 AddReducer(data, &graph_reducer, &dead_code_elimination);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001075 AddReducer(data, &graph_reducer, &value_numbering);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001076 AddReducer(data, &graph_reducer, &machine_reducer);
1077 AddReducer(data, &graph_reducer, &common_reducer);
Ben Murdochc5610432016-08-08 18:44:38 +01001078 AddReducer(data, &graph_reducer, &select_lowering);
1079 AddReducer(data, &graph_reducer, &tco);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001080 graph_reducer.ReduceGraph();
1081 }
1082};
1083
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001084struct EarlyGraphTrimmingPhase {
1085 static const char* phase_name() { return "early graph trimming"; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001086 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001087 GraphTrimmer trimmer(temp_zone, data->graph());
1088 NodeVector roots(temp_zone);
1089 data->jsgraph()->GetCachedNodes(&roots);
1090 trimmer.TrimGraph(roots.begin(), roots.end());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001091 }
1092};
1093
1094
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001095struct LateGraphTrimmingPhase {
1096 static const char* phase_name() { return "late graph trimming"; }
1097 void Run(PipelineData* data, Zone* temp_zone) {
1098 GraphTrimmer trimmer(temp_zone, data->graph());
1099 NodeVector roots(temp_zone);
1100 data->jsgraph()->GetCachedNodes(&roots);
1101 trimmer.TrimGraph(roots.begin(), roots.end());
1102 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001103};
1104
1105
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001106struct StressLoopPeelingPhase {
1107 static const char* phase_name() { return "stress loop peeling"; }
1108
1109 void Run(PipelineData* data, Zone* temp_zone) {
1110 // Peel the first outer loop for testing.
1111 // TODO(titzer): peel all loops? the N'th loop? Innermost loops?
1112 LoopTree* loop_tree = LoopFinder::BuildLoopTree(data->graph(), temp_zone);
1113 if (loop_tree != nullptr && loop_tree->outer_loops().size() > 0) {
1114 LoopPeeler::Peel(data->graph(), data->common(), loop_tree,
1115 loop_tree->outer_loops()[0], temp_zone);
1116 }
1117 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001118};
1119
1120
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001121struct ComputeSchedulePhase {
1122 static const char* phase_name() { return "scheduling"; }
1123
1124 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001125 Schedule* schedule = Scheduler::ComputeSchedule(
1126 temp_zone, data->graph(), data->info()->is_splitting_enabled()
1127 ? Scheduler::kSplitNodes
1128 : Scheduler::kNoFlags);
1129 if (FLAG_turbo_verify) ScheduleVerifier::Run(schedule);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001130 data->set_schedule(schedule);
1131 }
1132};
1133
1134
1135struct InstructionSelectionPhase {
1136 static const char* phase_name() { return "select instructions"; }
1137
1138 void Run(PipelineData* data, Zone* temp_zone, Linkage* linkage) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001139 InstructionSelector selector(
1140 temp_zone, data->graph()->NodeCount(), linkage, data->sequence(),
Ben Murdoch097c5b22016-05-18 11:27:45 +01001141 data->schedule(), data->source_positions(), data->frame(),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001142 data->info()->is_source_positions_enabled()
1143 ? InstructionSelector::kAllSourcePositions
1144 : InstructionSelector::kCallSourcePositions);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001145 selector.SelectInstructions();
1146 }
1147};
1148
1149
1150struct MeetRegisterConstraintsPhase {
1151 static const char* phase_name() { return "meet register constraints"; }
1152
1153 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001154 ConstraintBuilder builder(data->register_allocation_data());
1155 builder.MeetRegisterConstraints();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001156 }
1157};
1158
1159
1160struct ResolvePhisPhase {
1161 static const char* phase_name() { return "resolve phis"; }
1162
1163 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001164 ConstraintBuilder builder(data->register_allocation_data());
1165 builder.ResolvePhis();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001166 }
1167};
1168
1169
1170struct BuildLiveRangesPhase {
1171 static const char* phase_name() { return "build live ranges"; }
1172
1173 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001174 LiveRangeBuilder builder(data->register_allocation_data(), temp_zone);
1175 builder.BuildLiveRanges();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001176 }
1177};
1178
1179
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001180struct SplinterLiveRangesPhase {
1181 static const char* phase_name() { return "splinter live ranges"; }
1182
1183 void Run(PipelineData* data, Zone* temp_zone) {
1184 LiveRangeSeparator live_range_splinterer(data->register_allocation_data(),
1185 temp_zone);
1186 live_range_splinterer.Splinter();
1187 }
1188};
1189
1190
1191template <typename RegAllocator>
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001192struct AllocateGeneralRegistersPhase {
1193 static const char* phase_name() { return "allocate general registers"; }
1194
1195 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001196 RegAllocator allocator(data->register_allocation_data(), GENERAL_REGISTERS,
1197 temp_zone);
1198 allocator.AllocateRegisters();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001199 }
1200};
1201
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001202template <typename RegAllocator>
Ben Murdochc5610432016-08-08 18:44:38 +01001203struct AllocateFPRegistersPhase {
1204 static const char* phase_name() {
1205 return "allocate floating point registers";
1206 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001207
1208 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdochc5610432016-08-08 18:44:38 +01001209 RegAllocator allocator(data->register_allocation_data(), FP_REGISTERS,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001210 temp_zone);
1211 allocator.AllocateRegisters();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001212 }
1213};
1214
1215
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001216struct MergeSplintersPhase {
1217 static const char* phase_name() { return "merge splintered ranges"; }
1218 void Run(PipelineData* pipeline_data, Zone* temp_zone) {
1219 RegisterAllocationData* data = pipeline_data->register_allocation_data();
1220 LiveRangeMerger live_range_merger(data, temp_zone);
1221 live_range_merger.Merge();
1222 }
1223};
1224
1225
1226struct LocateSpillSlotsPhase {
1227 static const char* phase_name() { return "locate spill slots"; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001228
1229 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001230 SpillSlotLocator locator(data->register_allocation_data());
1231 locator.LocateSpillSlots();
1232 }
1233};
1234
1235
1236struct AssignSpillSlotsPhase {
1237 static const char* phase_name() { return "assign spill slots"; }
1238
1239 void Run(PipelineData* data, Zone* temp_zone) {
1240 OperandAssigner assigner(data->register_allocation_data());
1241 assigner.AssignSpillSlots();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001242 }
1243};
1244
1245
1246struct CommitAssignmentPhase {
1247 static const char* phase_name() { return "commit assignment"; }
1248
1249 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001250 OperandAssigner assigner(data->register_allocation_data());
1251 assigner.CommitAssignment();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001252 }
1253};
1254
1255
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001256struct PopulateReferenceMapsPhase {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001257 static const char* phase_name() { return "populate pointer maps"; }
1258
1259 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001260 ReferenceMapPopulator populator(data->register_allocation_data());
1261 populator.PopulateReferenceMaps();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001262 }
1263};
1264
1265
1266struct ConnectRangesPhase {
1267 static const char* phase_name() { return "connect ranges"; }
1268
1269 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001270 LiveRangeConnector connector(data->register_allocation_data());
1271 connector.ConnectRanges(temp_zone);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001272 }
1273};
1274
1275
1276struct ResolveControlFlowPhase {
1277 static const char* phase_name() { return "resolve control flow"; }
1278
1279 void Run(PipelineData* data, Zone* temp_zone) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001280 LiveRangeConnector connector(data->register_allocation_data());
1281 connector.ResolveControlFlow(temp_zone);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001282 }
1283};
1284
1285
1286struct OptimizeMovesPhase {
1287 static const char* phase_name() { return "optimize moves"; }
1288
1289 void Run(PipelineData* data, Zone* temp_zone) {
1290 MoveOptimizer move_optimizer(temp_zone, data->sequence());
1291 move_optimizer.Run();
1292 }
1293};
1294
1295
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001296struct FrameElisionPhase {
1297 static const char* phase_name() { return "frame elision"; }
1298
1299 void Run(PipelineData* data, Zone* temp_zone) {
1300 FrameElider(data->sequence()).Run();
1301 }
1302};
1303
1304
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001305struct JumpThreadingPhase {
1306 static const char* phase_name() { return "jump threading"; }
1307
Ben Murdoch097c5b22016-05-18 11:27:45 +01001308 void Run(PipelineData* data, Zone* temp_zone, bool frame_at_start) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001309 ZoneVector<RpoNumber> result(temp_zone);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001310 if (JumpThreading::ComputeForwarding(temp_zone, result, data->sequence(),
1311 frame_at_start)) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001312 JumpThreading::ApplyForwarding(result, data->sequence());
1313 }
1314 }
1315};
1316
1317
1318struct GenerateCodePhase {
1319 static const char* phase_name() { return "generate code"; }
1320
1321 void Run(PipelineData* data, Zone* temp_zone, Linkage* linkage) {
1322 CodeGenerator generator(data->frame(), linkage, data->sequence(),
1323 data->info());
1324 data->set_code(generator.GenerateCode());
1325 }
1326};
1327
1328
1329struct PrintGraphPhase {
1330 static const char* phase_name() { return nullptr; }
1331
1332 void Run(PipelineData* data, Zone* temp_zone, const char* phase) {
1333 CompilationInfo* info = data->info();
1334 Graph* graph = data->graph();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001335
1336 { // Print JSON.
Ben Murdochc5610432016-08-08 18:44:38 +01001337 AllowHandleDereference allow_deref;
1338 TurboJsonFile json_of(info, std::ios_base::app);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001339 json_of << "{\"name\":\"" << phase << "\",\"type\":\"graph\",\"data\":"
1340 << AsJSON(*graph, data->source_positions()) << "},\n";
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001341 }
1342
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001343 if (FLAG_trace_turbo_graph) { // Simple textual RPO.
Ben Murdochc5610432016-08-08 18:44:38 +01001344 AllowHandleDereference allow_deref;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001345 OFStream os(stdout);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001346 os << "-- Graph after " << phase << " -- " << std::endl;
1347 os << AsRPO(*graph);
1348 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001349 }
1350};
1351
1352
1353struct VerifyGraphPhase {
1354 static const char* phase_name() { return nullptr; }
1355
Ben Murdochc5610432016-08-08 18:44:38 +01001356 void Run(PipelineData* data, Zone* temp_zone, const bool untyped,
1357 bool values_only = false) {
1358 Verifier::Run(data->graph(), !untyped ? Verifier::TYPED : Verifier::UNTYPED,
1359 values_only ? Verifier::kValuesOnly : Verifier::kAll);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001360 }
1361};
1362
Ben Murdochc5610432016-08-08 18:44:38 +01001363void PipelineImpl::RunPrintAndVerify(const char* phase, bool untyped) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001364 if (FLAG_trace_turbo) {
1365 Run<PrintGraphPhase>(phase);
1366 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001367 if (FLAG_turbo_verify) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001368 Run<VerifyGraphPhase>(untyped);
1369 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001370}
1371
Ben Murdochc5610432016-08-08 18:44:38 +01001372bool PipelineImpl::CreateGraph() {
1373 PipelineData* data = this->data_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001374
Ben Murdochc5610432016-08-08 18:44:38 +01001375 data->BeginPhaseKind("graph creation");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001376
1377 if (FLAG_trace_turbo) {
1378 OFStream os(stdout);
1379 os << "---------------------------------------------------\n"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001380 << "Begin compiling method " << info()->GetDebugName().get()
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001381 << " using Turbofan" << std::endl;
1382 TurboCfgFile tcf(isolate());
1383 tcf << AsC1VCompilation(info());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001384 }
1385
Ben Murdochc5610432016-08-08 18:44:38 +01001386 data->source_positions()->AddDecorator();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001387
1388 if (FLAG_loop_assignment_analysis) {
1389 Run<LoopAssignmentAnalysisPhase>();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001390 }
1391
Ben Murdochc5610432016-08-08 18:44:38 +01001392 Run<TypeHintAnalysisPhase>();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001393
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001394 Run<GraphBuilderPhase>();
Ben Murdochc5610432016-08-08 18:44:38 +01001395 if (data->compilation_failed()) {
1396 data->EndPhaseKind();
1397 return false;
1398 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001399 RunPrintAndVerify("Initial untyped", true);
1400
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001401 // Perform OSR deconstruction.
1402 if (info()->is_osr()) {
1403 Run<OsrDeconstructionPhase>();
1404 RunPrintAndVerify("OSR deconstruction", true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001405 }
1406
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001407 // Perform function context specialization and inlining (if enabled).
1408 Run<InliningPhase>();
1409 RunPrintAndVerify("Inlined", true);
1410
1411 // Remove dead->live edges from the graph.
1412 Run<EarlyGraphTrimmingPhase>();
1413 RunPrintAndVerify("Early trimmed", true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001414
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001415 if (FLAG_print_turbo_replay) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001416 // Print a replay of the initial graph.
Ben Murdochc5610432016-08-08 18:44:38 +01001417 GraphReplayPrinter::PrintReplay(data->graph());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001418 }
1419
Ben Murdochc5610432016-08-08 18:44:38 +01001420 // Run the type-sensitive lowerings and optimizations on the graph.
1421 {
1422 // Type the graph and keep the Typer running on newly created nodes within
1423 // this scope; the Typer is automatically unlinked from the Graph once we
1424 // leave this scope below.
1425 Typer typer(isolate(), data->graph(), info()->is_deoptimization_enabled()
1426 ? Typer::kDeoptimizationEnabled
1427 : Typer::kNoFlags,
1428 info()->dependencies());
1429 Run<TyperPhase>(&typer);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001430 RunPrintAndVerify("Typed");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001431
Ben Murdochc5610432016-08-08 18:44:38 +01001432 data->BeginPhaseKind("lowering");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001433
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001434 // Lower JSOperators where we can determine types.
1435 Run<TypedLoweringPhase>();
1436 RunPrintAndVerify("Lowered typed");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001437
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001438 if (FLAG_turbo_stress_loop_peeling) {
1439 Run<StressLoopPeelingPhase>();
1440 RunPrintAndVerify("Loop peeled");
1441 }
1442
1443 if (FLAG_turbo_escape) {
1444 Run<EscapeAnalysisPhase>();
1445 RunPrintAndVerify("Escape Analysed");
1446 }
1447
Ben Murdochc5610432016-08-08 18:44:38 +01001448 // Select representations.
1449 Run<RepresentationSelectionPhase>();
Ben Murdoch61f157c2016-09-16 13:49:30 +01001450 RunPrintAndVerify("Representations selected", true);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001451 }
1452
Ben Murdochc5610432016-08-08 18:44:38 +01001453#ifdef DEBUG
1454 // From now on it is invalid to look at types on the nodes, because:
1455 //
1456 // (a) The remaining passes (might) run concurrent to the main thread and
1457 // therefore must not access the Heap or the Isolate in an uncontrolled
1458 // way (as done by the type system), and
1459 // (b) the types on the nodes might not make sense after representation
1460 // selection due to the way we handle truncations; if we'd want to look
1461 // at types afterwards we'd essentially need to re-type (large portions
1462 // of) the graph.
1463 //
1464 // In order to catch bugs related to type access after this point we remove
1465 // the types from the nodes at this point (currently only in Debug builds).
1466 Run<UntyperPhase>();
1467 RunPrintAndVerify("Untyped", true);
1468#endif
1469
Ben Murdoch61f157c2016-09-16 13:49:30 +01001470 // Run early optimization pass.
1471 Run<EarlyOptimizationPhase>();
1472 RunPrintAndVerify("Early optimized", true);
1473
Ben Murdochc5610432016-08-08 18:44:38 +01001474 data->EndPhaseKind();
1475
1476 return true;
1477}
1478
1479bool PipelineImpl::OptimizeGraph(Linkage* linkage) {
1480 PipelineData* data = this->data_;
1481
1482 data->BeginPhaseKind("block building");
1483
1484 Run<EffectControlLinearizationPhase>();
1485 RunPrintAndVerify("Effect and control linearized", true);
1486
Ben Murdoch61f157c2016-09-16 13:49:30 +01001487 if (FLAG_turbo_store_elimination) {
1488 Run<StoreStoreEliminationPhase>();
1489 RunPrintAndVerify("Store-store elimination", true);
1490 }
1491
Ben Murdochc5610432016-08-08 18:44:38 +01001492 Run<BranchEliminationPhase>();
1493 RunPrintAndVerify("Branch conditions eliminated", true);
1494
1495 // Optimize control flow.
1496 if (FLAG_turbo_cf_optimization) {
1497 Run<ControlFlowOptimizationPhase>();
1498 RunPrintAndVerify("Control flow optimized", true);
1499 }
1500
1501 // Optimize memory access and allocation operations.
1502 Run<MemoryOptimizationPhase>();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001503 // TODO(jarin, rossberg): Remove UNTYPED once machine typing works.
Ben Murdochc5610432016-08-08 18:44:38 +01001504 RunPrintAndVerify("Memory optimized", true);
1505
1506 // Lower changes that have been inserted before.
1507 Run<LateOptimizationPhase>();
1508 // TODO(jarin, rossberg): Remove UNTYPED once machine typing works.
1509 RunPrintAndVerify("Late optimized", true);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001510
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001511 Run<LateGraphTrimmingPhase>();
1512 // TODO(jarin, rossberg): Remove UNTYPED once machine typing works.
1513 RunPrintAndVerify("Late trimmed", true);
1514
Ben Murdochc5610432016-08-08 18:44:38 +01001515 data->source_positions()->RemoveDecorator();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001516
Ben Murdochc5610432016-08-08 18:44:38 +01001517 return ScheduleAndSelectInstructions(linkage);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001518}
1519
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001520Handle<Code> Pipeline::GenerateCodeForCodeStub(Isolate* isolate,
1521 CallDescriptor* call_descriptor,
1522 Graph* graph, Schedule* schedule,
Ben Murdoch097c5b22016-05-18 11:27:45 +01001523 Code::Flags flags,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001524 const char* debug_name) {
Ben Murdochc5610432016-08-08 18:44:38 +01001525 CompilationInfo info(CStrVector(debug_name), isolate, graph->zone(), flags);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001526
1527 // Construct a pipeline for scheduling and code generation.
Ben Murdochda12d292016-06-02 14:46:10 +01001528 ZonePool zone_pool(isolate->allocator());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001529 PipelineData data(&zone_pool, &info, graph, schedule);
1530 base::SmartPointer<PipelineStatistics> pipeline_statistics;
Ben Murdoch61f157c2016-09-16 13:49:30 +01001531 if (FLAG_turbo_stats || FLAG_turbo_stats_nvp) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001532 pipeline_statistics.Reset(new PipelineStatistics(&info, &zone_pool));
1533 pipeline_statistics->BeginPhaseKind("stub codegen");
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001534 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001535
Ben Murdochc5610432016-08-08 18:44:38 +01001536 PipelineImpl pipeline(&data);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001537 DCHECK_NOT_NULL(data.schedule());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001538
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001539 if (FLAG_trace_turbo) {
Ben Murdoch61f157c2016-09-16 13:49:30 +01001540 {
1541 TurboJsonFile json_of(&info, std::ios_base::trunc);
1542 json_of << "{\"function\":\"" << info.GetDebugName().get()
1543 << "\", \"source\":\"\",\n\"phases\":[";
1544 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001545 pipeline.Run<PrintGraphPhase>("Machine");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001546 }
1547
Ben Murdochc5610432016-08-08 18:44:38 +01001548 pipeline.Run<VerifyGraphPhase>(false, true);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001549 return pipeline.ScheduleAndGenerateCode(call_descriptor);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001550}
1551
Ben Murdochc5610432016-08-08 18:44:38 +01001552// static
1553Handle<Code> Pipeline::GenerateCodeForTesting(CompilationInfo* info) {
1554 ZonePool zone_pool(info->isolate()->allocator());
1555 base::SmartPointer<PipelineStatistics> pipeline_statistics(
1556 CreatePipelineStatistics(info, &zone_pool));
1557 PipelineData data(&zone_pool, info, pipeline_statistics.get());
1558 PipelineImpl pipeline(&data);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001559
Ben Murdochc5610432016-08-08 18:44:38 +01001560 Linkage linkage(Linkage::ComputeIncoming(data.instruction_zone(), info));
1561
1562 if (!pipeline.CreateGraph()) return Handle<Code>::null();
1563 if (!pipeline.OptimizeGraph(&linkage)) return Handle<Code>::null();
1564 return pipeline.GenerateCode(&linkage);
1565}
1566
1567// static
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001568Handle<Code> Pipeline::GenerateCodeForTesting(CompilationInfo* info,
1569 Graph* graph,
1570 Schedule* schedule) {
1571 CallDescriptor* call_descriptor =
1572 Linkage::ComputeIncoming(info->zone(), info);
1573 return GenerateCodeForTesting(info, call_descriptor, graph, schedule);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001574}
1575
Ben Murdochc5610432016-08-08 18:44:38 +01001576// static
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001577Handle<Code> Pipeline::GenerateCodeForTesting(CompilationInfo* info,
1578 CallDescriptor* call_descriptor,
1579 Graph* graph,
1580 Schedule* schedule) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001581 // Construct a pipeline for scheduling and code generation.
Ben Murdochda12d292016-06-02 14:46:10 +01001582 ZonePool zone_pool(info->isolate()->allocator());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001583 PipelineData data(&zone_pool, info, graph, schedule);
1584 base::SmartPointer<PipelineStatistics> pipeline_statistics;
Ben Murdoch61f157c2016-09-16 13:49:30 +01001585 if (FLAG_turbo_stats || FLAG_turbo_stats_nvp) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001586 pipeline_statistics.Reset(new PipelineStatistics(info, &zone_pool));
1587 pipeline_statistics->BeginPhaseKind("test codegen");
1588 }
1589
Ben Murdochc5610432016-08-08 18:44:38 +01001590 PipelineImpl pipeline(&data);
1591
1592 if (FLAG_trace_turbo) {
1593 TurboJsonFile json_of(info, std::ios_base::trunc);
1594 json_of << "{\"function\":\"" << info->GetDebugName().get()
1595 << "\", \"source\":\"\",\n\"phases\":[";
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001596 }
Ben Murdochc5610432016-08-08 18:44:38 +01001597 // TODO(rossberg): Should this really be untyped?
1598 pipeline.RunPrintAndVerify("Machine", true);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001599
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001600 return pipeline.ScheduleAndGenerateCode(call_descriptor);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001601}
1602
Ben Murdochc5610432016-08-08 18:44:38 +01001603// static
1604CompilationJob* Pipeline::NewCompilationJob(Handle<JSFunction> function) {
1605 return new PipelineCompilationJob(function->GetIsolate(), function);
1606}
1607
1608// static
1609CompilationJob* Pipeline::NewWasmCompilationJob(
1610 CompilationInfo* info, Graph* graph, CallDescriptor* descriptor,
1611 SourcePositionTable* source_positions) {
1612 return new PipelineWasmCompilationJob(info, graph, descriptor,
1613 source_positions);
1614}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001615
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001616bool Pipeline::AllocateRegistersForTesting(const RegisterConfiguration* config,
1617 InstructionSequence* sequence,
1618 bool run_verifier) {
Ben Murdochc5610432016-08-08 18:44:38 +01001619 CompilationInfo info(ArrayVector("testing"), sequence->isolate(),
1620 sequence->zone());
Ben Murdochda12d292016-06-02 14:46:10 +01001621 ZonePool zone_pool(sequence->isolate()->allocator());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001622 PipelineData data(&zone_pool, &info, sequence);
Ben Murdochc5610432016-08-08 18:44:38 +01001623 PipelineImpl pipeline(&data);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001624 pipeline.data_->InitializeFrameData(nullptr);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001625 pipeline.AllocateRegisters(config, nullptr, run_verifier);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001626 return !data.compilation_failed();
1627}
1628
Ben Murdochc5610432016-08-08 18:44:38 +01001629bool PipelineImpl::ScheduleAndSelectInstructions(Linkage* linkage) {
1630 CallDescriptor* call_descriptor = linkage->GetIncomingDescriptor();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001631 PipelineData* data = this->data_;
1632
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001633 DCHECK_NOT_NULL(data->graph());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001634
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001635 if (data->schedule() == nullptr) Run<ComputeSchedulePhase>();
1636 TraceSchedule(data->info(), data->schedule());
1637
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001638 if (FLAG_turbo_profiling) {
Ben Murdochc5610432016-08-08 18:44:38 +01001639 data->set_profiler_data(BasicBlockInstrumentor::Instrument(
1640 info(), data->graph(), data->schedule()));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001641 }
1642
Ben Murdochda12d292016-06-02 14:46:10 +01001643 data->InitializeInstructionSequence(call_descriptor);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001644
Ben Murdoch097c5b22016-05-18 11:27:45 +01001645 data->InitializeFrameData(call_descriptor);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001646 // Select and schedule instructions covering the scheduled graph.
Ben Murdochc5610432016-08-08 18:44:38 +01001647 Run<InstructionSelectionPhase>(linkage);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001648
1649 if (FLAG_trace_turbo && !data->MayHaveUnverifiableGraph()) {
Ben Murdochc5610432016-08-08 18:44:38 +01001650 AllowHandleDereference allow_deref;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001651 TurboCfgFile tcf(isolate());
1652 tcf << AsC1V("CodeGen", data->schedule(), data->source_positions(),
1653 data->sequence());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001654 }
1655
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001656 if (FLAG_trace_turbo) {
Ben Murdochc5610432016-08-08 18:44:38 +01001657 std::ostringstream source_position_output;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001658 // Output source position information before the graph is deleted.
1659 data_->source_positions()->Print(source_position_output);
Ben Murdochc5610432016-08-08 18:44:38 +01001660 data_->set_source_position_output(source_position_output.str());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001661 }
1662
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001663 data->DeleteGraphZone();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001664
Ben Murdochc5610432016-08-08 18:44:38 +01001665 data->BeginPhaseKind("register allocation");
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001666
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001667 bool run_verifier = FLAG_turbo_verify_allocation;
Ben Murdoch097c5b22016-05-18 11:27:45 +01001668
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001669 // Allocate registers.
Ben Murdoch61f157c2016-09-16 13:49:30 +01001670 AllocateRegisters(RegisterConfiguration::Turbofan(), call_descriptor,
1671 run_verifier);
Ben Murdochda12d292016-06-02 14:46:10 +01001672 Run<FrameElisionPhase>();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001673 if (data->compilation_failed()) {
1674 info()->AbortOptimization(kNotEnoughVirtualRegistersRegalloc);
Ben Murdochc5610432016-08-08 18:44:38 +01001675 data->EndPhaseKind();
1676 return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001677 }
1678
Ben Murdoch097c5b22016-05-18 11:27:45 +01001679 // TODO(mtrofin): move this off to the register allocator.
1680 bool generate_frame_at_start =
Ben Murdochda12d292016-06-02 14:46:10 +01001681 data_->sequence()->instruction_blocks().front()->must_construct_frame();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001682 // Optimimize jumps.
1683 if (FLAG_turbo_jt) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001684 Run<JumpThreadingPhase>(generate_frame_at_start);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001685 }
1686
Ben Murdochc5610432016-08-08 18:44:38 +01001687 data->EndPhaseKind();
1688
1689 return true;
1690}
1691
1692Handle<Code> PipelineImpl::GenerateCode(Linkage* linkage) {
1693 PipelineData* data = this->data_;
1694
1695 data->BeginPhaseKind("code generation");
1696
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001697 // Generate final machine code.
Ben Murdochc5610432016-08-08 18:44:38 +01001698 Run<GenerateCodePhase>(linkage);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001699
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001700 Handle<Code> code = data->code();
Ben Murdochc5610432016-08-08 18:44:38 +01001701 if (data->profiler_data()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001702#if ENABLE_DISASSEMBLER
1703 std::ostringstream os;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001704 code->Disassemble(nullptr, os);
Ben Murdochc5610432016-08-08 18:44:38 +01001705 data->profiler_data()->SetCode(&os);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001706#endif
1707 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001708
1709 info()->SetCode(code);
1710 v8::internal::CodeGenerator::PrintCode(code, info());
1711
1712 if (FLAG_trace_turbo) {
Ben Murdochc5610432016-08-08 18:44:38 +01001713 TurboJsonFile json_of(info(), std::ios_base::app);
1714 json_of << "{\"name\":\"disassembly\",\"type\":\"disassembly\",\"data\":\"";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001715#if ENABLE_DISASSEMBLER
Ben Murdochc5610432016-08-08 18:44:38 +01001716 std::stringstream disassembly_stream;
1717 code->Disassemble(nullptr, disassembly_stream);
1718 std::string disassembly_string(disassembly_stream.str());
1719 for (const auto& c : disassembly_string) {
1720 json_of << AsEscapedUC16ForJSON(c);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001721 }
Ben Murdochc5610432016-08-08 18:44:38 +01001722#endif // ENABLE_DISASSEMBLER
1723 json_of << "\"}\n],\n";
1724 json_of << "\"nodePositions\":";
1725 json_of << data->source_position_output();
1726 json_of << "}";
1727
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001728 OFStream os(stdout);
1729 os << "---------------------------------------------------\n"
1730 << "Finished compiling method " << info()->GetDebugName().get()
1731 << " using Turbofan" << std::endl;
1732 }
1733
1734 return code;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001735}
1736
Ben Murdochc5610432016-08-08 18:44:38 +01001737Handle<Code> PipelineImpl::ScheduleAndGenerateCode(
1738 CallDescriptor* call_descriptor) {
1739 Linkage linkage(call_descriptor);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001740
Ben Murdochc5610432016-08-08 18:44:38 +01001741 // Schedule the graph, perform instruction selection and register allocation.
1742 if (!ScheduleAndSelectInstructions(&linkage)) return Handle<Code>();
1743
1744 // Generate the final machine code.
1745 return GenerateCode(&linkage);
1746}
1747
1748void PipelineImpl::AllocateRegisters(const RegisterConfiguration* config,
1749 CallDescriptor* descriptor,
1750 bool run_verifier) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001751 PipelineData* data = this->data_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001752 // Don't track usage for this zone in compiler stats.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001753 base::SmartPointer<Zone> verifier_zone;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001754 RegisterAllocatorVerifier* verifier = nullptr;
1755 if (run_verifier) {
Ben Murdochda12d292016-06-02 14:46:10 +01001756 verifier_zone.Reset(new Zone(isolate()->allocator()));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001757 verifier = new (verifier_zone.get()) RegisterAllocatorVerifier(
1758 verifier_zone.get(), config, data->sequence());
1759 }
1760
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001761#ifdef DEBUG
Ben Murdochda12d292016-06-02 14:46:10 +01001762 data_->sequence()->ValidateEdgeSplitForm();
Ben Murdochc5610432016-08-08 18:44:38 +01001763 data_->sequence()->ValidateDeferredBlockEntryPaths();
Ben Murdochda12d292016-06-02 14:46:10 +01001764 data_->sequence()->ValidateDeferredBlockExitPaths();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001765#endif
1766
Ben Murdochc5610432016-08-08 18:44:38 +01001767 data->InitializeRegisterAllocationData(config, descriptor);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001768 if (info()->is_osr()) {
1769 OsrHelper osr_helper(info());
1770 osr_helper.SetupFrame(data->frame());
1771 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001772
1773 Run<MeetRegisterConstraintsPhase>();
1774 Run<ResolvePhisPhase>();
1775 Run<BuildLiveRangesPhase>();
1776 if (FLAG_trace_turbo_graph) {
Ben Murdochc5610432016-08-08 18:44:38 +01001777 AllowHandleDereference allow_deref;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001778 OFStream os(stdout);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001779 os << "----- Instruction sequence before register allocation -----\n"
Ben Murdochc5610432016-08-08 18:44:38 +01001780 << PrintableInstructionSequence({config, data->sequence()});
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001781 }
1782 if (verifier != nullptr) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001783 CHECK(!data->register_allocation_data()->ExistsUseWithoutDefinition());
1784 CHECK(data->register_allocation_data()
1785 ->RangesDefinedInDeferredStayInDeferred());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001786 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001787
1788 if (FLAG_turbo_preprocess_ranges) {
1789 Run<SplinterLiveRangesPhase>();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001790 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001791
Ben Murdoch61f157c2016-09-16 13:49:30 +01001792 Run<AllocateGeneralRegistersPhase<LinearScanAllocator>>();
1793 Run<AllocateFPRegistersPhase<LinearScanAllocator>>();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001794
1795 if (FLAG_turbo_preprocess_ranges) {
1796 Run<MergeSplintersPhase>();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001797 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001798
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001799 Run<AssignSpillSlotsPhase>();
1800
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001801 Run<CommitAssignmentPhase>();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001802 Run<PopulateReferenceMapsPhase>();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001803 Run<ConnectRangesPhase>();
1804 Run<ResolveControlFlowPhase>();
1805 if (FLAG_turbo_move_optimization) {
1806 Run<OptimizeMovesPhase>();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001807 }
1808
Ben Murdochda12d292016-06-02 14:46:10 +01001809 Run<LocateSpillSlotsPhase>();
1810
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001811 if (FLAG_trace_turbo_graph) {
Ben Murdochc5610432016-08-08 18:44:38 +01001812 AllowHandleDereference allow_deref;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001813 OFStream os(stdout);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001814 os << "----- Instruction sequence after register allocation -----\n"
Ben Murdochc5610432016-08-08 18:44:38 +01001815 << PrintableInstructionSequence({config, data->sequence()});
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001816 }
1817
1818 if (verifier != nullptr) {
1819 verifier->VerifyAssignment();
1820 verifier->VerifyGapMoves();
1821 }
1822
1823 if (FLAG_trace_turbo && !data->MayHaveUnverifiableGraph()) {
1824 TurboCfgFile tcf(data->isolate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001825 tcf << AsC1VRegisterAllocationData("CodeGen",
1826 data->register_allocation_data());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001827 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001828
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001829 data->DeleteRegisterAllocationZone();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001830}
1831
Ben Murdochc5610432016-08-08 18:44:38 +01001832CompilationInfo* PipelineImpl::info() const { return data_->info(); }
1833
1834Isolate* PipelineImpl::isolate() const { return info()->isolate(); }
Ben Murdoch097c5b22016-05-18 11:27:45 +01001835
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001836} // namespace compiler
1837} // namespace internal
1838} // namespace v8