| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/compiler/wasm-compiler.h" | 5 #include "src/compiler/wasm-compiler.h" |
| 6 | 6 |
| 7 #include "src/isolate-inl.h" | 7 #include "src/isolate-inl.h" |
| 8 | 8 |
| 9 #include "src/base/platform/elapsed-timer.h" | 9 #include "src/base/platform/elapsed-timer.h" |
| 10 #include "src/base/platform/platform.h" | 10 #include "src/base/platform/platform.h" |
| 11 | 11 |
| 12 #include "src/compiler/access-builder.h" | 12 #include "src/compiler/access-builder.h" |
| 13 #include "src/compiler/common-operator.h" | 13 #include "src/compiler/common-operator.h" |
| 14 #include "src/compiler/diamond.h" | 14 #include "src/compiler/diamond.h" |
| 15 #include "src/compiler/graph-visualizer.h" | 15 #include "src/compiler/graph-visualizer.h" |
| 16 #include "src/compiler/graph.h" | 16 #include "src/compiler/graph.h" |
| 17 #include "src/compiler/instruction-selector.h" | 17 #include "src/compiler/instruction-selector.h" |
| 18 #include "src/compiler/int64-lowering.h" | 18 #include "src/compiler/int64-lowering.h" |
| 19 #include "src/compiler/js-generic-lowering.h" | 19 #include "src/compiler/js-generic-lowering.h" |
| 20 #include "src/compiler/js-graph.h" | 20 #include "src/compiler/js-graph.h" |
| 21 #include "src/compiler/js-operator.h" | 21 #include "src/compiler/js-operator.h" |
| 22 #include "src/compiler/linkage.h" | 22 #include "src/compiler/linkage.h" |
| 23 #include "src/compiler/machine-operator.h" | 23 #include "src/compiler/machine-operator.h" |
| 24 #include "src/compiler/node-matchers.h" | 24 #include "src/compiler/node-matchers.h" |
| 25 #include "src/compiler/pipeline.h" | 25 #include "src/compiler/pipeline.h" |
| 26 #include "src/compiler/simd-lowering.h" | |
| 27 #include "src/compiler/source-position.h" | 26 #include "src/compiler/source-position.h" |
| 28 #include "src/compiler/zone-pool.h" | 27 #include "src/compiler/zone-pool.h" |
| 29 | 28 |
| 30 #include "src/code-factory.h" | 29 #include "src/code-factory.h" |
| 31 #include "src/code-stubs.h" | 30 #include "src/code-stubs.h" |
| 32 #include "src/factory.h" | 31 #include "src/factory.h" |
| 33 #include "src/log-inl.h" | 32 #include "src/log-inl.h" |
| 34 | 33 |
| 35 #include "src/wasm/ast-decoder.h" | 34 #include "src/wasm/ast-decoder.h" |
| 36 #include "src/wasm/wasm-module.h" | 35 #include "src/wasm/wasm-module.h" |
| (...skipping 2303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2340 case wasm::kAstF64: | 2339 case wasm::kAstF64: |
| 2341 return BuildChangeFloat64ToTagged(node); | 2340 return BuildChangeFloat64ToTagged(node); |
| 2342 case wasm::kAstStmt: | 2341 case wasm::kAstStmt: |
| 2343 return jsgraph()->UndefinedConstant(); | 2342 return jsgraph()->UndefinedConstant(); |
| 2344 default: | 2343 default: |
| 2345 UNREACHABLE(); | 2344 UNREACHABLE(); |
| 2346 return nullptr; | 2345 return nullptr; |
| 2347 } | 2346 } |
| 2348 } | 2347 } |
| 2349 | 2348 |
| 2350 Node* WasmGraphBuilder::BuildChangeTaggedToInt32(Node* value) { | |
| 2351 value = BuildChangeTaggedToFloat64(value); | |
| 2352 value = graph()->NewNode(jsgraph()->machine()->ChangeFloat64ToInt32(), value); | |
| 2353 return value; | |
| 2354 } | |
| 2355 | |
| 2356 Node* WasmGraphBuilder::BuildJavaScriptToNumber(Node* node, Node* context, | 2349 Node* WasmGraphBuilder::BuildJavaScriptToNumber(Node* node, Node* context, |
| 2357 Node* effect, Node* control) { | 2350 Node* effect, Node* control) { |
| 2358 Callable callable = CodeFactory::ToNumber(jsgraph()->isolate()); | 2351 Callable callable = CodeFactory::ToNumber(jsgraph()->isolate()); |
| 2359 CallDescriptor* desc = Linkage::GetStubCallDescriptor( | 2352 CallDescriptor* desc = Linkage::GetStubCallDescriptor( |
| 2360 jsgraph()->isolate(), jsgraph()->zone(), callable.descriptor(), 0, | 2353 jsgraph()->isolate(), jsgraph()->zone(), callable.descriptor(), 0, |
| 2361 CallDescriptor::kNoFlags, Operator::kNoProperties); | 2354 CallDescriptor::kNoFlags, Operator::kNoProperties); |
| 2362 Node* stub_code = jsgraph()->HeapConstant(callable.code()); | 2355 Node* stub_code = jsgraph()->HeapConstant(callable.code()); |
| 2363 | 2356 |
| 2364 Node* result = graph()->NewNode(jsgraph()->common()->Call(desc), stub_code, | 2357 Node* result = graph()->NewNode(jsgraph()->common()->Call(desc), stub_code, |
| 2365 node, context, effect, control); | 2358 node, context, effect, control); |
| (...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2785 if (!mem_size_) | 2778 if (!mem_size_) |
| 2786 mem_size_ = jsgraph()->RelocatableInt32Constant( | 2779 mem_size_ = jsgraph()->RelocatableInt32Constant( |
| 2787 size, RelocInfo::WASM_MEMORY_SIZE_REFERENCE); | 2780 size, RelocInfo::WASM_MEMORY_SIZE_REFERENCE); |
| 2788 return mem_size_; | 2781 return mem_size_; |
| 2789 } else { | 2782 } else { |
| 2790 return jsgraph()->RelocatableInt32Constant( | 2783 return jsgraph()->RelocatableInt32Constant( |
| 2791 size + offset, RelocInfo::WASM_MEMORY_SIZE_REFERENCE); | 2784 size + offset, RelocInfo::WASM_MEMORY_SIZE_REFERENCE); |
| 2792 } | 2785 } |
| 2793 } | 2786 } |
| 2794 | 2787 |
| 2795 Node* WasmGraphBuilder::DefaultS128Value() { | |
| 2796 Node* zero = jsgraph()->Int32Constant(0); | |
| 2797 return graph()->NewNode(jsgraph()->machine()->CreateInt32x4(), zero, zero, | |
| 2798 zero, zero); | |
| 2799 } | |
| 2800 | |
| 2801 Node* WasmGraphBuilder::FunctionTable() { | 2788 Node* WasmGraphBuilder::FunctionTable() { |
| 2802 DCHECK(module_ && module_->instance && | 2789 DCHECK(module_ && module_->instance && |
| 2803 !module_->instance->function_table.is_null()); | 2790 !module_->instance->function_table.is_null()); |
| 2804 if (!function_table_) { | 2791 if (!function_table_) { |
| 2805 function_table_ = HeapConstant(module_->instance->function_table); | 2792 function_table_ = HeapConstant(module_->instance->function_table); |
| 2806 } | 2793 } |
| 2807 return function_table_; | 2794 return function_table_; |
| 2808 } | 2795 } |
| 2809 | 2796 |
| 2810 Node* WasmGraphBuilder::ChangeToRuntimeCall(Node* node, | |
| 2811 Runtime::FunctionId function_id, | |
| 2812 Signature<Conversion>* signature) { | |
| 2813 SimplifiedOperatorBuilder simplified(jsgraph()->zone()); | |
| 2814 const Runtime::Function* function = Runtime::FunctionForId(function_id); | |
| 2815 CallDescriptor* desc = Linkage::GetRuntimeCallDescriptor( | |
| 2816 jsgraph()->zone(), function_id, function->nargs, Operator::kNoProperties, | |
| 2817 CallDescriptor::kNoFlags); | |
| 2818 const int kInputSize = 16; | |
| 2819 const int kDefaultFunctionParams = 6; | |
| 2820 Node* inputs[kInputSize + kDefaultFunctionParams]; | |
| 2821 DCHECK_LE(function->nargs + kDefaultFunctionParams, | |
| 2822 static_cast<int>(arraysize(inputs))); | |
| 2823 // Either there are control + effect or not. | |
| 2824 DCHECK(node->InputCount() == function->nargs || | |
| 2825 node->InputCount() == function->nargs + 2); | |
| 2826 int index = 0; | |
| 2827 inputs[index++] = jsgraph()->CEntryStubConstant(function->result_size); | |
| 2828 for (int i = 0; i < function->nargs; ++i) { | |
| 2829 Node* arg = node->InputAt(i); | |
| 2830 switch (signature->GetParam(i)) { | |
| 2831 case Conversion::kInt32: | |
| 2832 arg = BuildChangeInt32ToTagged(arg); | |
| 2833 break; | |
| 2834 case Conversion::kFloat32: | |
| 2835 arg = jsgraph()->graph()->NewNode( | |
| 2836 jsgraph()->machine()->ChangeFloat32ToFloat64(), arg); | |
| 2837 arg = BuildChangeFloat64ToTagged(arg); | |
| 2838 break; | |
| 2839 case Conversion::kFloat64: | |
| 2840 arg = BuildChangeFloat64ToTagged(arg); | |
| 2841 break; | |
| 2842 default: | |
| 2843 break; | |
| 2844 } | |
| 2845 inputs[index++] = arg; | |
| 2846 } | |
| 2847 inputs[index++] = jsgraph()->ExternalConstant( | |
| 2848 ExternalReference(function_id, jsgraph()->isolate())); | |
| 2849 inputs[index++] = jsgraph()->Int32Constant(function->nargs); | |
| 2850 inputs[index++] = jsgraph()->Constant(module_->instance->context); | |
| 2851 // Loads and stores have control and effect, others do not and use | |
| 2852 // the start node instead. | |
| 2853 if (node->InputCount() == function->nargs + 2) { | |
| 2854 inputs[index++] = node->InputAt(function->nargs + 1); // effect | |
| 2855 inputs[index++] = node->InputAt(function->nargs + 2); // control | |
| 2856 } else { | |
| 2857 inputs[index++] = jsgraph()->graph()->start(); // effect | |
| 2858 inputs[index++] = jsgraph()->graph()->start(); // control | |
| 2859 } | |
| 2860 Node* ret = jsgraph()->graph()->NewNode(jsgraph()->common()->Call(desc), | |
| 2861 index, inputs); | |
| 2862 | |
| 2863 Conversion return_type = signature->GetReturn(); | |
| 2864 switch (return_type) { | |
| 2865 case Conversion::kInt32: | |
| 2866 ret = BuildChangeTaggedToInt32(ret); | |
| 2867 break; | |
| 2868 case Conversion::kFloat32: | |
| 2869 NodeProperties::SetType(ret, Type::Number()); | |
| 2870 ret = BuildChangeTaggedToFloat64(ret); | |
| 2871 ret = jsgraph()->graph()->NewNode( | |
| 2872 jsgraph()->machine()->TruncateInt64ToInt32(), ret); | |
| 2873 break; | |
| 2874 case Conversion::kFloat64: | |
| 2875 ret = BuildChangeTaggedToFloat64(ret); | |
| 2876 break; | |
| 2877 default: | |
| 2878 break; | |
| 2879 } | |
| 2880 return ret; | |
| 2881 } | |
| 2882 | |
| 2883 Node* WasmGraphBuilder::LoadGlobal(uint32_t index) { | 2797 Node* WasmGraphBuilder::LoadGlobal(uint32_t index) { |
| 2884 MachineType mem_type = module_->GetGlobalType(index); | 2798 MachineType mem_type = module_->GetGlobalType(index); |
| 2885 Node* addr = jsgraph()->RelocatableIntPtrConstant( | 2799 Node* addr = jsgraph()->RelocatableIntPtrConstant( |
| 2886 reinterpret_cast<uintptr_t>(module_->instance->globals_start + | 2800 reinterpret_cast<uintptr_t>(module_->instance->globals_start + |
| 2887 module_->module->globals[index].offset), | 2801 module_->module->globals[index].offset), |
| 2888 RelocInfo::WASM_GLOBAL_REFERENCE); | 2802 RelocInfo::WASM_GLOBAL_REFERENCE); |
| 2889 const Operator* op = jsgraph()->machine()->Load(mem_type); | 2803 const Operator* op = jsgraph()->machine()->Load(mem_type); |
| 2890 Node* node = graph()->NewNode(op, addr, jsgraph()->Int32Constant(0), *effect_, | 2804 Node* node = graph()->NewNode(op, addr, jsgraph()->Int32Constant(0), *effect_, |
| 2891 *control_); | 2805 *control_); |
| 2892 *effect_ = node; | 2806 *effect_ = node; |
| (...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3269 } | 3183 } |
| 3270 | 3184 |
| 3271 void WasmGraphBuilder::SetSourcePosition(Node* node, | 3185 void WasmGraphBuilder::SetSourcePosition(Node* node, |
| 3272 wasm::WasmCodePosition position) { | 3186 wasm::WasmCodePosition position) { |
| 3273 DCHECK_NE(position, wasm::kNoCodePosition); | 3187 DCHECK_NE(position, wasm::kNoCodePosition); |
| 3274 compiler::SourcePosition pos(position); | 3188 compiler::SourcePosition pos(position); |
| 3275 if (source_position_table_) | 3189 if (source_position_table_) |
| 3276 source_position_table_->SetSourcePosition(node, pos); | 3190 source_position_table_->SetSourcePosition(node, pos); |
| 3277 } | 3191 } |
| 3278 | 3192 |
| 3279 MachineOperatorBuilder* WasmGraphBuilder::simd() { | |
| 3280 has_simd_ops_ = true; | |
| 3281 return jsgraph()->machine(); | |
| 3282 } | |
| 3283 | |
| 3284 Node* WasmGraphBuilder::SimdOp(wasm::WasmOpcode opcode, | |
| 3285 const NodeVector& inputs) { | |
| 3286 switch (opcode) { | |
| 3287 case wasm::kExprI32x4ExtractLane: | |
| 3288 return graph()->NewNode(simd()->Int32x4ExtractLane(), inputs[0], | |
| 3289 inputs[1]); | |
| 3290 case wasm::kExprI32x4Splat: | |
| 3291 return graph()->NewNode(simd()->CreateInt32x4(), inputs[0], inputs[0], | |
| 3292 inputs[0], inputs[0]); | |
| 3293 default: | |
| 3294 return graph()->NewNode(UnsupportedOpcode(opcode), nullptr); | |
| 3295 } | |
| 3296 } | |
| 3297 | |
| 3298 static void RecordFunctionCompilation(CodeEventListener::LogEventsAndTags tag, | 3193 static void RecordFunctionCompilation(CodeEventListener::LogEventsAndTags tag, |
| 3299 Isolate* isolate, Handle<Code> code, | 3194 Isolate* isolate, Handle<Code> code, |
| 3300 const char* message, uint32_t index, | 3195 const char* message, uint32_t index, |
| 3301 const wasm::WasmName& module_name, | 3196 const wasm::WasmName& module_name, |
| 3302 const wasm::WasmName& func_name) { | 3197 const wasm::WasmName& func_name) { |
| 3303 DCHECK(isolate->logger()->is_logging_code_events() || | 3198 DCHECK(isolate->logger()->is_logging_code_events() || |
| 3304 isolate->is_profiling()); | 3199 isolate->is_profiling()); |
| 3305 | 3200 |
| 3306 ScopedVector<char> buffer(128); | 3201 ScopedVector<char> buffer(128); |
| 3307 SNPrintF(buffer, "%s#%d:%.*s:%.*s", message, index, module_name.length(), | 3202 SNPrintF(buffer, "%s#%d:%.*s:%.*s", message, index, module_name.length(), |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3500 } | 3395 } |
| 3501 return nullptr; | 3396 return nullptr; |
| 3502 } | 3397 } |
| 3503 | 3398 |
| 3504 if (machine->Is32()) { | 3399 if (machine->Is32()) { |
| 3505 Int64Lowering r(graph, machine, common, jsgraph_->zone(), function_->sig); | 3400 Int64Lowering r(graph, machine, common, jsgraph_->zone(), function_->sig); |
| 3506 r.LowerGraph(); | 3401 r.LowerGraph(); |
| 3507 } | 3402 } |
| 3508 | 3403 |
| 3509 int index = static_cast<int>(function_->func_index); | 3404 int index = static_cast<int>(function_->func_index); |
| 3510 | |
| 3511 // Run lowering pass if SIMD ops are present in the function | |
| 3512 if (builder.has_simd_ops()) { | |
| 3513 SimdLowering simd(jsgraph_->zone(), &builder); | |
| 3514 GraphReducer graph_reducer(jsgraph_->zone(), graph); | |
| 3515 graph_reducer.AddReducer(&simd); | |
| 3516 graph_reducer.ReduceGraph(); | |
| 3517 | |
| 3518 if (FLAG_trace_turbo_graph) { // Simple textual RPO. | |
| 3519 OFStream os(stdout); | |
| 3520 os << "-- Graph after simd lowering -- " << std::endl; | |
| 3521 os << AsRPO(*graph); | |
| 3522 } | |
| 3523 } | |
| 3524 | |
| 3525 if (index >= FLAG_trace_wasm_ast_start && index < FLAG_trace_wasm_ast_end) { | 3405 if (index >= FLAG_trace_wasm_ast_start && index < FLAG_trace_wasm_ast_end) { |
| 3526 OFStream os(stdout); | 3406 OFStream os(stdout); |
| 3527 PrintAst(isolate_->allocator(), body, os, nullptr); | 3407 PrintAst(isolate_->allocator(), body, os, nullptr); |
| 3528 } | 3408 } |
| 3529 if (FLAG_trace_wasm_decode_time) { | 3409 if (FLAG_trace_wasm_decode_time) { |
| 3530 *decode_ms = decode_timer.Elapsed().InMillisecondsF(); | 3410 *decode_ms = decode_timer.Elapsed().InMillisecondsF(); |
| 3531 } | 3411 } |
| 3532 return source_position_table; | 3412 return source_position_table; |
| 3533 } | 3413 } |
| 3534 | 3414 |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3661 function_->code_start_offset), | 3541 function_->code_start_offset), |
| 3662 compile_ms); | 3542 compile_ms); |
| 3663 } | 3543 } |
| 3664 | 3544 |
| 3665 return code; | 3545 return code; |
| 3666 } | 3546 } |
| 3667 | 3547 |
| 3668 } // namespace compiler | 3548 } // namespace compiler |
| 3669 } // namespace internal | 3549 } // namespace internal |
| 3670 } // namespace v8 | 3550 } // namespace v8 |
| OLD | NEW |