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" |
26 #include "src/compiler/source-position.h" | 27 #include "src/compiler/source-position.h" |
27 #include "src/compiler/zone-pool.h" | 28 #include "src/compiler/zone-pool.h" |
28 | 29 |
29 #include "src/code-factory.h" | 30 #include "src/code-factory.h" |
30 #include "src/code-stubs.h" | 31 #include "src/code-stubs.h" |
31 #include "src/factory.h" | 32 #include "src/factory.h" |
32 #include "src/log-inl.h" | 33 #include "src/log-inl.h" |
33 | 34 |
34 #include "src/wasm/ast-decoder.h" | 35 #include "src/wasm/ast-decoder.h" |
35 #include "src/wasm/wasm-module.h" | 36 #include "src/wasm/wasm-module.h" |
(...skipping 2205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2241 case wasm::kAstF64: | 2242 case wasm::kAstF64: |
2242 return BuildChangeFloat64ToTagged(node); | 2243 return BuildChangeFloat64ToTagged(node); |
2243 case wasm::kAstStmt: | 2244 case wasm::kAstStmt: |
2244 return jsgraph()->UndefinedConstant(); | 2245 return jsgraph()->UndefinedConstant(); |
2245 default: | 2246 default: |
2246 UNREACHABLE(); | 2247 UNREACHABLE(); |
2247 return nullptr; | 2248 return nullptr; |
2248 } | 2249 } |
2249 } | 2250 } |
2250 | 2251 |
| 2252 Node* WasmGraphBuilder::BuildChangeTaggedToInt32(Node* value) { |
| 2253 value = BuildChangeTaggedToFloat64(value); |
| 2254 value = graph()->NewNode(jsgraph()->machine()->ChangeFloat64ToInt32(), value); |
| 2255 return value; |
| 2256 } |
| 2257 |
2251 Node* WasmGraphBuilder::BuildJavaScriptToNumber(Node* node, Node* context, | 2258 Node* WasmGraphBuilder::BuildJavaScriptToNumber(Node* node, Node* context, |
2252 Node* effect, Node* control) { | 2259 Node* effect, Node* control) { |
2253 Callable callable = CodeFactory::ToNumber(jsgraph()->isolate()); | 2260 Callable callable = CodeFactory::ToNumber(jsgraph()->isolate()); |
2254 CallDescriptor* desc = Linkage::GetStubCallDescriptor( | 2261 CallDescriptor* desc = Linkage::GetStubCallDescriptor( |
2255 jsgraph()->isolate(), jsgraph()->zone(), callable.descriptor(), 0, | 2262 jsgraph()->isolate(), jsgraph()->zone(), callable.descriptor(), 0, |
2256 CallDescriptor::kNoFlags, Operator::kNoProperties); | 2263 CallDescriptor::kNoFlags, Operator::kNoProperties); |
2257 Node* stub_code = jsgraph()->HeapConstant(callable.code()); | 2264 Node* stub_code = jsgraph()->HeapConstant(callable.code()); |
2258 | 2265 |
2259 Node* result = graph()->NewNode(jsgraph()->common()->Call(desc), stub_code, | 2266 Node* result = graph()->NewNode(jsgraph()->common()->Call(desc), stub_code, |
2260 node, context, effect, control); | 2267 node, context, effect, control); |
(...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2680 if (!mem_size_) | 2687 if (!mem_size_) |
2681 mem_size_ = jsgraph()->RelocatableInt32Constant( | 2688 mem_size_ = jsgraph()->RelocatableInt32Constant( |
2682 size, RelocInfo::WASM_MEMORY_SIZE_REFERENCE); | 2689 size, RelocInfo::WASM_MEMORY_SIZE_REFERENCE); |
2683 return mem_size_; | 2690 return mem_size_; |
2684 } else { | 2691 } else { |
2685 return jsgraph()->RelocatableInt32Constant( | 2692 return jsgraph()->RelocatableInt32Constant( |
2686 size + offset, RelocInfo::WASM_MEMORY_SIZE_REFERENCE); | 2693 size + offset, RelocInfo::WASM_MEMORY_SIZE_REFERENCE); |
2687 } | 2694 } |
2688 } | 2695 } |
2689 | 2696 |
| 2697 Node* WasmGraphBuilder::DefaultS128Value() { |
| 2698 Node* zero = jsgraph()->Int32Constant(0); |
| 2699 return graph()->NewNode(jsgraph()->machine()->CreateInt32x4(), zero, zero, |
| 2700 zero, zero); |
| 2701 } |
| 2702 |
2690 Node* WasmGraphBuilder::FunctionTable() { | 2703 Node* WasmGraphBuilder::FunctionTable() { |
2691 DCHECK(module_ && module_->instance && | 2704 DCHECK(module_ && module_->instance && |
2692 !module_->instance->function_table.is_null()); | 2705 !module_->instance->function_table.is_null()); |
2693 if (!function_table_) { | 2706 if (!function_table_) { |
2694 function_table_ = HeapConstant(module_->instance->function_table); | 2707 function_table_ = HeapConstant(module_->instance->function_table); |
2695 } | 2708 } |
2696 return function_table_; | 2709 return function_table_; |
2697 } | 2710 } |
2698 | 2711 |
| 2712 Node* WasmGraphBuilder::ChangeToRuntimeCall(Node* node, |
| 2713 Runtime::FunctionId function_id, |
| 2714 Signature<Conversion>* signature) { |
| 2715 SimplifiedOperatorBuilder simplified(jsgraph()->zone()); |
| 2716 const Runtime::Function* function = Runtime::FunctionForId(function_id); |
| 2717 CallDescriptor* desc = Linkage::GetRuntimeCallDescriptor( |
| 2718 jsgraph()->zone(), function_id, function->nargs, Operator::kNoProperties, |
| 2719 CallDescriptor::kNoFlags); |
| 2720 const int kInputSize = 16; |
| 2721 const int kDefaultFunctionParams = 6; |
| 2722 Node* inputs[kInputSize + kDefaultFunctionParams]; |
| 2723 DCHECK_LE(function->nargs + kDefaultFunctionParams, |
| 2724 static_cast<int>(arraysize(inputs))); |
| 2725 // Either there are control + effect or not. |
| 2726 DCHECK(node->InputCount() == function->nargs || |
| 2727 node->InputCount() == function->nargs + 2); |
| 2728 int index = 0; |
| 2729 inputs[index++] = jsgraph()->CEntryStubConstant(function->result_size); |
| 2730 for (int i = 0; i < function->nargs; ++i) { |
| 2731 Node* arg = node->InputAt(i); |
| 2732 switch (signature->GetParam(i)) { |
| 2733 case Conversion::kInt32: |
| 2734 arg = BuildChangeInt32ToTagged(arg); |
| 2735 break; |
| 2736 case Conversion::kFloat32: |
| 2737 arg = jsgraph()->graph()->NewNode( |
| 2738 jsgraph()->machine()->ChangeFloat32ToFloat64(), arg); |
| 2739 arg = BuildChangeFloat64ToTagged(arg); |
| 2740 break; |
| 2741 case Conversion::kFloat64: |
| 2742 arg = BuildChangeFloat64ToTagged(arg); |
| 2743 break; |
| 2744 default: |
| 2745 break; |
| 2746 } |
| 2747 inputs[index++] = arg; |
| 2748 } |
| 2749 inputs[index++] = jsgraph()->ExternalConstant( |
| 2750 ExternalReference(function_id, jsgraph()->isolate())); |
| 2751 inputs[index++] = jsgraph()->Int32Constant(function->nargs); |
| 2752 inputs[index++] = jsgraph()->Constant(module_->instance->context); |
| 2753 // Loads and stores have control and effect, others do not and use |
| 2754 // the start node instead. |
| 2755 if (node->InputCount() == function->nargs + 2) { |
| 2756 inputs[index++] = node->InputAt(function->nargs + 1); // effect |
| 2757 inputs[index++] = node->InputAt(function->nargs + 2); // control |
| 2758 } else { |
| 2759 inputs[index++] = jsgraph()->graph()->start(); // effect |
| 2760 inputs[index++] = jsgraph()->graph()->start(); // control |
| 2761 } |
| 2762 Node* ret = jsgraph()->graph()->NewNode(jsgraph()->common()->Call(desc), |
| 2763 index, inputs); |
| 2764 |
| 2765 Conversion return_type = signature->GetReturn(); |
| 2766 switch (return_type) { |
| 2767 case Conversion::kInt32: |
| 2768 ret = BuildChangeTaggedToInt32(ret); |
| 2769 break; |
| 2770 case Conversion::kFloat32: |
| 2771 NodeProperties::SetType(ret, Type::Number()); |
| 2772 ret = BuildChangeTaggedToFloat64(ret); |
| 2773 ret = jsgraph()->graph()->NewNode( |
| 2774 jsgraph()->machine()->TruncateInt64ToInt32(), ret); |
| 2775 break; |
| 2776 case Conversion::kFloat64: |
| 2777 ret = BuildChangeTaggedToFloat64(ret); |
| 2778 break; |
| 2779 default: |
| 2780 break; |
| 2781 } |
| 2782 return ret; |
| 2783 } |
| 2784 |
2699 Node* WasmGraphBuilder::LoadGlobal(uint32_t index) { | 2785 Node* WasmGraphBuilder::LoadGlobal(uint32_t index) { |
2700 MachineType mem_type = module_->GetGlobalType(index); | 2786 MachineType mem_type = module_->GetGlobalType(index); |
2701 Node* addr = jsgraph()->RelocatableIntPtrConstant( | 2787 Node* addr = jsgraph()->RelocatableIntPtrConstant( |
2702 reinterpret_cast<uintptr_t>(module_->instance->globals_start + | 2788 reinterpret_cast<uintptr_t>(module_->instance->globals_start + |
2703 module_->module->globals[index].offset), | 2789 module_->module->globals[index].offset), |
2704 RelocInfo::WASM_GLOBAL_REFERENCE); | 2790 RelocInfo::WASM_GLOBAL_REFERENCE); |
2705 const Operator* op = jsgraph()->machine()->Load(mem_type); | 2791 const Operator* op = jsgraph()->machine()->Load(mem_type); |
2706 Node* node = graph()->NewNode(op, addr, jsgraph()->Int32Constant(0), *effect_, | 2792 Node* node = graph()->NewNode(op, addr, jsgraph()->Int32Constant(0), *effect_, |
2707 *control_); | 2793 *control_); |
2708 *effect_ = node; | 2794 *effect_ = node; |
(...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3085 } | 3171 } |
3086 | 3172 |
3087 void WasmGraphBuilder::SetSourcePosition(Node* node, | 3173 void WasmGraphBuilder::SetSourcePosition(Node* node, |
3088 wasm::WasmCodePosition position) { | 3174 wasm::WasmCodePosition position) { |
3089 DCHECK_NE(position, wasm::kNoCodePosition); | 3175 DCHECK_NE(position, wasm::kNoCodePosition); |
3090 compiler::SourcePosition pos(position); | 3176 compiler::SourcePosition pos(position); |
3091 if (source_position_table_) | 3177 if (source_position_table_) |
3092 source_position_table_->SetSourcePosition(node, pos); | 3178 source_position_table_->SetSourcePosition(node, pos); |
3093 } | 3179 } |
3094 | 3180 |
| 3181 MachineOperatorBuilder* WasmGraphBuilder::simd() { |
| 3182 has_simd_ops_ = true; |
| 3183 return jsgraph()->machine(); |
| 3184 } |
| 3185 |
| 3186 Node* WasmGraphBuilder::SimdOp(wasm::WasmOpcode opcode, |
| 3187 const NodeVector& inputs) { |
| 3188 switch (opcode) { |
| 3189 case wasm::kExprI32x4ExtractLane: |
| 3190 return graph()->NewNode(simd()->Int32x4ExtractLane(), inputs[0], |
| 3191 inputs[1]); |
| 3192 case wasm::kExprI32x4Splat: |
| 3193 return graph()->NewNode(simd()->CreateInt32x4(), inputs[0], inputs[0], |
| 3194 inputs[0], inputs[0]); |
| 3195 default: |
| 3196 return graph()->NewNode(UnsupportedOpcode(opcode), nullptr); |
| 3197 } |
| 3198 } |
| 3199 |
3095 static void RecordFunctionCompilation(CodeEventListener::LogEventsAndTags tag, | 3200 static void RecordFunctionCompilation(CodeEventListener::LogEventsAndTags tag, |
3096 Isolate* isolate, Handle<Code> code, | 3201 Isolate* isolate, Handle<Code> code, |
3097 const char* message, uint32_t index, | 3202 const char* message, uint32_t index, |
3098 const wasm::WasmName& module_name, | 3203 const wasm::WasmName& module_name, |
3099 const wasm::WasmName& func_name) { | 3204 const wasm::WasmName& func_name) { |
3100 DCHECK(isolate->logger()->is_logging_code_events() || | 3205 DCHECK(isolate->logger()->is_logging_code_events() || |
3101 isolate->is_profiling()); | 3206 isolate->is_profiling()); |
3102 | 3207 |
3103 ScopedVector<char> buffer(128); | 3208 ScopedVector<char> buffer(128); |
3104 SNPrintF(buffer, "%s#%d:%.*s:%.*s", message, index, module_name.length(), | 3209 SNPrintF(buffer, "%s#%d:%.*s:%.*s", message, index, module_name.length(), |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3297 } | 3402 } |
3298 return nullptr; | 3403 return nullptr; |
3299 } | 3404 } |
3300 | 3405 |
3301 if (machine->Is32()) { | 3406 if (machine->Is32()) { |
3302 Int64Lowering r(graph, machine, common, jsgraph_->zone(), function_->sig); | 3407 Int64Lowering r(graph, machine, common, jsgraph_->zone(), function_->sig); |
3303 r.LowerGraph(); | 3408 r.LowerGraph(); |
3304 } | 3409 } |
3305 | 3410 |
3306 int index = static_cast<int>(function_->func_index); | 3411 int index = static_cast<int>(function_->func_index); |
| 3412 |
| 3413 // Run lowering pass if SIMD ops are present in the function |
| 3414 if (builder.has_simd_ops()) { |
| 3415 SimdLowering simd(jsgraph_->zone(), &builder); |
| 3416 GraphReducer graph_reducer(jsgraph_->zone(), graph); |
| 3417 graph_reducer.AddReducer(&simd); |
| 3418 graph_reducer.ReduceGraph(); |
| 3419 |
| 3420 if (FLAG_trace_turbo_graph) { // Simple textual RPO. |
| 3421 OFStream os(stdout); |
| 3422 os << "-- Graph after simd lowering -- " << std::endl; |
| 3423 os << AsRPO(*graph); |
| 3424 } |
| 3425 } |
| 3426 |
3307 if (index >= FLAG_trace_wasm_ast_start && index < FLAG_trace_wasm_ast_end) { | 3427 if (index >= FLAG_trace_wasm_ast_start && index < FLAG_trace_wasm_ast_end) { |
3308 OFStream os(stdout); | 3428 OFStream os(stdout); |
3309 PrintAst(isolate_->allocator(), body, os, nullptr); | 3429 PrintAst(isolate_->allocator(), body, os, nullptr); |
3310 } | 3430 } |
3311 if (FLAG_trace_wasm_decode_time) { | 3431 if (FLAG_trace_wasm_decode_time) { |
3312 *decode_ms = decode_timer.Elapsed().InMillisecondsF(); | 3432 *decode_ms = decode_timer.Elapsed().InMillisecondsF(); |
3313 } | 3433 } |
3314 return source_position_table; | 3434 return source_position_table; |
3315 } | 3435 } |
3316 | 3436 |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3443 function_->code_start_offset), | 3563 function_->code_start_offset), |
3444 compile_ms); | 3564 compile_ms); |
3445 } | 3565 } |
3446 | 3566 |
3447 return code; | 3567 return code; |
3448 } | 3568 } |
3449 | 3569 |
3450 } // namespace compiler | 3570 } // namespace compiler |
3451 } // namespace internal | 3571 } // namespace internal |
3452 } // namespace v8 | 3572 } // namespace v8 |
OLD | NEW |