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 <memory> | 7 #include <memory> |
8 | 8 |
9 #include "src/isolate-inl.h" | 9 #include "src/isolate-inl.h" |
10 | 10 |
11 #include "src/base/platform/elapsed-timer.h" | 11 #include "src/base/platform/elapsed-timer.h" |
12 #include "src/base/platform/platform.h" | 12 #include "src/base/platform/platform.h" |
13 | 13 |
14 #include "src/compiler/access-builder.h" | 14 #include "src/compiler/access-builder.h" |
15 #include "src/compiler/common-operator.h" | 15 #include "src/compiler/common-operator.h" |
16 #include "src/compiler/diamond.h" | 16 #include "src/compiler/diamond.h" |
17 #include "src/compiler/graph-visualizer.h" | 17 #include "src/compiler/graph-visualizer.h" |
18 #include "src/compiler/graph.h" | 18 #include "src/compiler/graph.h" |
19 #include "src/compiler/instruction-selector.h" | 19 #include "src/compiler/instruction-selector.h" |
20 #include "src/compiler/int64-lowering.h" | 20 #include "src/compiler/int64-lowering.h" |
21 #include "src/compiler/js-graph.h" | 21 #include "src/compiler/js-graph.h" |
22 #include "src/compiler/js-operator.h" | 22 #include "src/compiler/js-operator.h" |
23 #include "src/compiler/linkage.h" | 23 #include "src/compiler/linkage.h" |
24 #include "src/compiler/machine-operator.h" | 24 #include "src/compiler/machine-operator.h" |
25 #include "src/compiler/node-matchers.h" | 25 #include "src/compiler/node-matchers.h" |
26 #include "src/compiler/pipeline.h" | 26 #include "src/compiler/pipeline.h" |
27 #include "src/compiler/simd-lowering.h" | |
28 #include "src/compiler/source-position.h" | 27 #include "src/compiler/source-position.h" |
29 #include "src/compiler/zone-pool.h" | 28 #include "src/compiler/zone-pool.h" |
30 | 29 |
31 #include "src/code-factory.h" | 30 #include "src/code-factory.h" |
32 #include "src/code-stubs.h" | 31 #include "src/code-stubs.h" |
33 #include "src/factory.h" | 32 #include "src/factory.h" |
34 #include "src/log-inl.h" | 33 #include "src/log-inl.h" |
35 | 34 |
36 #include "src/wasm/ast-decoder.h" | 35 #include "src/wasm/ast-decoder.h" |
37 #include "src/wasm/wasm-module.h" | 36 #include "src/wasm/wasm-module.h" |
(...skipping 2166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2204 case wasm::kAstF64: | 2203 case wasm::kAstF64: |
2205 return BuildChangeFloat64ToTagged(node); | 2204 return BuildChangeFloat64ToTagged(node); |
2206 case wasm::kAstStmt: | 2205 case wasm::kAstStmt: |
2207 return jsgraph()->UndefinedConstant(); | 2206 return jsgraph()->UndefinedConstant(); |
2208 default: | 2207 default: |
2209 UNREACHABLE(); | 2208 UNREACHABLE(); |
2210 return nullptr; | 2209 return nullptr; |
2211 } | 2210 } |
2212 } | 2211 } |
2213 | 2212 |
2214 Node* WasmGraphBuilder::BuildChangeTaggedToInt32(Node* value) { | |
2215 value = BuildChangeTaggedToFloat64(value); | |
2216 value = graph()->NewNode(jsgraph()->machine()->ChangeFloat64ToInt32(), value); | |
2217 return value; | |
2218 } | |
2219 | |
2220 Node* WasmGraphBuilder::BuildJavaScriptToNumber(Node* node, Node* context, | 2213 Node* WasmGraphBuilder::BuildJavaScriptToNumber(Node* node, Node* context, |
2221 Node* effect, Node* control) { | 2214 Node* effect, Node* control) { |
2222 Callable callable = CodeFactory::ToNumber(jsgraph()->isolate()); | 2215 Callable callable = CodeFactory::ToNumber(jsgraph()->isolate()); |
2223 CallDescriptor* desc = Linkage::GetStubCallDescriptor( | 2216 CallDescriptor* desc = Linkage::GetStubCallDescriptor( |
2224 jsgraph()->isolate(), jsgraph()->zone(), callable.descriptor(), 0, | 2217 jsgraph()->isolate(), jsgraph()->zone(), callable.descriptor(), 0, |
2225 CallDescriptor::kNoFlags, Operator::kNoProperties); | 2218 CallDescriptor::kNoFlags, Operator::kNoProperties); |
2226 Node* stub_code = jsgraph()->HeapConstant(callable.code()); | 2219 Node* stub_code = jsgraph()->HeapConstant(callable.code()); |
2227 | 2220 |
2228 Node* result = graph()->NewNode(jsgraph()->common()->Call(desc), stub_code, | 2221 Node* result = graph()->NewNode(jsgraph()->common()->Call(desc), stub_code, |
2229 node, context, effect, control); | 2222 node, context, effect, control); |
(...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2649 if (!mem_size_) | 2642 if (!mem_size_) |
2650 mem_size_ = jsgraph()->RelocatableInt32Constant( | 2643 mem_size_ = jsgraph()->RelocatableInt32Constant( |
2651 size, RelocInfo::WASM_MEMORY_SIZE_REFERENCE); | 2644 size, RelocInfo::WASM_MEMORY_SIZE_REFERENCE); |
2652 return mem_size_; | 2645 return mem_size_; |
2653 } else { | 2646 } else { |
2654 return jsgraph()->RelocatableInt32Constant( | 2647 return jsgraph()->RelocatableInt32Constant( |
2655 size + offset, RelocInfo::WASM_MEMORY_SIZE_REFERENCE); | 2648 size + offset, RelocInfo::WASM_MEMORY_SIZE_REFERENCE); |
2656 } | 2649 } |
2657 } | 2650 } |
2658 | 2651 |
2659 Node* WasmGraphBuilder::DefaultS128Value() { | |
2660 Node* zero = jsgraph()->Int32Constant(0); | |
2661 return graph()->NewNode(jsgraph()->machine()->CreateInt32x4(), zero, zero, | |
2662 zero, zero); | |
2663 } | |
2664 | |
2665 Node* WasmGraphBuilder::FunctionTable(uint32_t index) { | 2652 Node* WasmGraphBuilder::FunctionTable(uint32_t index) { |
2666 DCHECK(module_ && module_->instance && | 2653 DCHECK(module_ && module_->instance && |
2667 index < module_->instance->function_tables.size()); | 2654 index < module_->instance->function_tables.size()); |
2668 if (!function_tables_.size()) { | 2655 if (!function_tables_.size()) { |
2669 for (size_t i = 0; i < module_->instance->function_tables.size(); ++i) { | 2656 for (size_t i = 0; i < module_->instance->function_tables.size(); ++i) { |
2670 DCHECK(!module_->instance->function_tables[i].is_null()); | 2657 DCHECK(!module_->instance->function_tables[i].is_null()); |
2671 function_tables_.push_back( | 2658 function_tables_.push_back( |
2672 HeapConstant(module_->instance->function_tables[i])); | 2659 HeapConstant(module_->instance->function_tables[i])); |
2673 } | 2660 } |
2674 } | 2661 } |
2675 return function_tables_[index]; | 2662 return function_tables_[index]; |
2676 } | 2663 } |
2677 | 2664 |
2678 Node* WasmGraphBuilder::ChangeToRuntimeCall(Node* node, | |
2679 Runtime::FunctionId function_id, | |
2680 Signature<Conversion>* signature) { | |
2681 SimplifiedOperatorBuilder simplified(jsgraph()->zone()); | |
2682 const Runtime::Function* function = Runtime::FunctionForId(function_id); | |
2683 CallDescriptor* desc = Linkage::GetRuntimeCallDescriptor( | |
2684 jsgraph()->zone(), function_id, function->nargs, Operator::kNoProperties, | |
2685 CallDescriptor::kNoFlags); | |
2686 const int kInputSize = 16; | |
2687 const int kDefaultFunctionParams = 6; | |
2688 Node* inputs[kInputSize + kDefaultFunctionParams]; | |
2689 DCHECK_LE(function->nargs + kDefaultFunctionParams, | |
2690 static_cast<int>(arraysize(inputs))); | |
2691 // Either there are control + effect or not. | |
2692 DCHECK(node->InputCount() == function->nargs || | |
2693 node->InputCount() == function->nargs + 2); | |
2694 int index = 0; | |
2695 inputs[index++] = jsgraph()->CEntryStubConstant(function->result_size); | |
2696 for (int i = 0; i < function->nargs; ++i) { | |
2697 Node* arg = node->InputAt(i); | |
2698 switch (signature->GetParam(i)) { | |
2699 case Conversion::kInt32: | |
2700 arg = BuildChangeInt32ToTagged(arg); | |
2701 break; | |
2702 case Conversion::kFloat32: | |
2703 arg = jsgraph()->graph()->NewNode( | |
2704 jsgraph()->machine()->ChangeFloat32ToFloat64(), arg); | |
2705 arg = BuildChangeFloat64ToTagged(arg); | |
2706 break; | |
2707 case Conversion::kFloat64: | |
2708 arg = BuildChangeFloat64ToTagged(arg); | |
2709 break; | |
2710 default: | |
2711 break; | |
2712 } | |
2713 inputs[index++] = arg; | |
2714 } | |
2715 inputs[index++] = jsgraph()->ExternalConstant( | |
2716 ExternalReference(function_id, jsgraph()->isolate())); | |
2717 inputs[index++] = jsgraph()->Int32Constant(function->nargs); | |
2718 inputs[index++] = jsgraph()->Constant(module_->instance->context); | |
2719 // Loads and stores have control and effect, others do not and use | |
2720 // the start node instead. | |
2721 if (node->InputCount() == function->nargs + 2) { | |
2722 inputs[index++] = node->InputAt(function->nargs + 1); // effect | |
2723 inputs[index++] = node->InputAt(function->nargs + 2); // control | |
2724 } else { | |
2725 inputs[index++] = jsgraph()->graph()->start(); // effect | |
2726 inputs[index++] = jsgraph()->graph()->start(); // control | |
2727 } | |
2728 Node* ret = jsgraph()->graph()->NewNode(jsgraph()->common()->Call(desc), | |
2729 index, inputs); | |
2730 | |
2731 Conversion return_type = signature->GetReturn(); | |
2732 switch (return_type) { | |
2733 case Conversion::kInt32: | |
2734 ret = BuildChangeTaggedToInt32(ret); | |
2735 break; | |
2736 case Conversion::kFloat32: | |
2737 NodeProperties::SetType(ret, Type::Number()); | |
2738 ret = BuildChangeTaggedToFloat64(ret); | |
2739 ret = jsgraph()->graph()->NewNode( | |
2740 jsgraph()->machine()->TruncateInt64ToInt32(), ret); | |
2741 break; | |
2742 case Conversion::kFloat64: | |
2743 ret = BuildChangeTaggedToFloat64(ret); | |
2744 break; | |
2745 default: | |
2746 break; | |
2747 } | |
2748 return ret; | |
2749 } | |
2750 | |
2751 Node* WasmGraphBuilder::GetGlobal(uint32_t index) { | 2665 Node* WasmGraphBuilder::GetGlobal(uint32_t index) { |
2752 MachineType mem_type = | 2666 MachineType mem_type = |
2753 wasm::WasmOpcodes::MachineTypeFor(module_->GetGlobalType(index)); | 2667 wasm::WasmOpcodes::MachineTypeFor(module_->GetGlobalType(index)); |
2754 Node* addr = jsgraph()->RelocatableIntPtrConstant( | 2668 Node* addr = jsgraph()->RelocatableIntPtrConstant( |
2755 reinterpret_cast<uintptr_t>(module_->instance->globals_start + | 2669 reinterpret_cast<uintptr_t>(module_->instance->globals_start + |
2756 module_->module->globals[index].offset), | 2670 module_->module->globals[index].offset), |
2757 RelocInfo::WASM_GLOBAL_REFERENCE); | 2671 RelocInfo::WASM_GLOBAL_REFERENCE); |
2758 const Operator* op = jsgraph()->machine()->Load(mem_type); | 2672 const Operator* op = jsgraph()->machine()->Load(mem_type); |
2759 Node* node = graph()->NewNode(op, addr, jsgraph()->Int32Constant(0), *effect_, | 2673 Node* node = graph()->NewNode(op, addr, jsgraph()->Int32Constant(0), *effect_, |
2760 *control_); | 2674 *control_); |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2930 } | 2844 } |
2931 | 2845 |
2932 void WasmGraphBuilder::SetSourcePosition(Node* node, | 2846 void WasmGraphBuilder::SetSourcePosition(Node* node, |
2933 wasm::WasmCodePosition position) { | 2847 wasm::WasmCodePosition position) { |
2934 DCHECK_NE(position, wasm::kNoCodePosition); | 2848 DCHECK_NE(position, wasm::kNoCodePosition); |
2935 compiler::SourcePosition pos(position); | 2849 compiler::SourcePosition pos(position); |
2936 if (source_position_table_) | 2850 if (source_position_table_) |
2937 source_position_table_->SetSourcePosition(node, pos); | 2851 source_position_table_->SetSourcePosition(node, pos); |
2938 } | 2852 } |
2939 | 2853 |
2940 MachineOperatorBuilder* WasmGraphBuilder::simd() { | |
2941 has_simd_ops_ = true; | |
2942 return jsgraph()->machine(); | |
2943 } | |
2944 | |
2945 Node* WasmGraphBuilder::SimdOp(wasm::WasmOpcode opcode, | 2854 Node* WasmGraphBuilder::SimdOp(wasm::WasmOpcode opcode, |
2946 const NodeVector& inputs) { | 2855 const NodeVector& inputs) { |
2947 switch (opcode) { | 2856 switch (opcode) { |
2948 case wasm::kExprI32x4ExtractLane: | 2857 case wasm::kExprI32x4ExtractLane: |
2949 return graph()->NewNode(simd()->Int32x4ExtractLane(), inputs[0], | 2858 return graph()->NewNode(jsgraph()->machine()->Int32x4ExtractLane(), |
2950 inputs[1]); | 2859 inputs[0], inputs[1]); |
2951 case wasm::kExprI32x4Splat: | 2860 case wasm::kExprI32x4Splat: |
2952 return graph()->NewNode(simd()->CreateInt32x4(), inputs[0], inputs[0], | 2861 return graph()->NewNode(jsgraph()->machine()->Int32x4ExtractLane(), |
2953 inputs[0], inputs[0]); | 2862 inputs[0], inputs[0], inputs[0], inputs[0]); |
2954 default: | 2863 default: |
2955 return graph()->NewNode(UnsupportedOpcode(opcode), nullptr); | 2864 return graph()->NewNode(UnsupportedOpcode(opcode), nullptr); |
2956 } | 2865 } |
2957 } | 2866 } |
2958 | 2867 |
2959 static void RecordFunctionCompilation(CodeEventListener::LogEventsAndTags tag, | 2868 static void RecordFunctionCompilation(CodeEventListener::LogEventsAndTags tag, |
2960 Isolate* isolate, Handle<Code> code, | 2869 Isolate* isolate, Handle<Code> code, |
2961 const char* message, uint32_t index, | 2870 const char* message, uint32_t index, |
2962 const wasm::WasmName& module_name, | 2871 const wasm::WasmName& module_name, |
2963 const wasm::WasmName& func_name) { | 2872 const wasm::WasmName& func_name) { |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3162 return nullptr; | 3071 return nullptr; |
3163 } | 3072 } |
3164 | 3073 |
3165 if (machine->Is32()) { | 3074 if (machine->Is32()) { |
3166 Int64Lowering r(graph, machine, common, jsgraph_->zone(), function_->sig); | 3075 Int64Lowering r(graph, machine, common, jsgraph_->zone(), function_->sig); |
3167 r.LowerGraph(); | 3076 r.LowerGraph(); |
3168 } | 3077 } |
3169 | 3078 |
3170 int index = static_cast<int>(function_->func_index); | 3079 int index = static_cast<int>(function_->func_index); |
3171 | 3080 |
3172 // Run lowering pass if SIMD ops are present in the function | |
3173 if (builder.has_simd_ops()) { | |
3174 SimdLowering simd(jsgraph_->zone(), &builder); | |
3175 GraphReducer graph_reducer(jsgraph_->zone(), graph); | |
3176 graph_reducer.AddReducer(&simd); | |
3177 graph_reducer.ReduceGraph(); | |
3178 | |
3179 if (FLAG_trace_turbo_graph) { // Simple textual RPO. | |
3180 OFStream os(stdout); | |
3181 os << "-- Graph after simd lowering -- " << std::endl; | |
3182 os << AsRPO(*graph); | |
3183 } | |
3184 } | |
3185 | |
3186 if (index >= FLAG_trace_wasm_ast_start && index < FLAG_trace_wasm_ast_end) { | 3081 if (index >= FLAG_trace_wasm_ast_start && index < FLAG_trace_wasm_ast_end) { |
3187 OFStream os(stdout); | 3082 OFStream os(stdout); |
3188 PrintAst(isolate_->allocator(), body, os, nullptr); | 3083 PrintAst(isolate_->allocator(), body, os, nullptr); |
3189 } | 3084 } |
3190 if (FLAG_trace_wasm_decode_time) { | 3085 if (FLAG_trace_wasm_decode_time) { |
3191 *decode_ms = decode_timer.Elapsed().InMillisecondsF(); | 3086 *decode_ms = decode_timer.Elapsed().InMillisecondsF(); |
3192 } | 3087 } |
3193 return source_position_table; | 3088 return source_position_table; |
3194 } | 3089 } |
3195 | 3090 |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3323 function_->code_start_offset), | 3218 function_->code_start_offset), |
3324 compile_ms); | 3219 compile_ms); |
3325 } | 3220 } |
3326 | 3221 |
3327 return code; | 3222 return code; |
3328 } | 3223 } |
3329 | 3224 |
3330 } // namespace compiler | 3225 } // namespace compiler |
3331 } // namespace internal | 3226 } // namespace internal |
3332 } // namespace v8 | 3227 } // namespace v8 |
OLD | NEW |