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/js-inlining-heuristic.h" | 5 #include "src/compiler/js-inlining-heuristic.h" |
6 | 6 |
7 #include "src/compiler.h" | 7 #include "src/compiler.h" |
8 #include "src/compiler/dead-code-elimination.h" // TODO(mstarzinger): Remove! | |
9 #include "src/compiler/node-matchers.h" | 8 #include "src/compiler/node-matchers.h" |
10 #include "src/objects-inl.h" | 9 #include "src/objects-inl.h" |
11 | 10 |
12 namespace v8 { | 11 namespace v8 { |
13 namespace internal { | 12 namespace internal { |
14 namespace compiler { | 13 namespace compiler { |
15 | 14 |
16 Reduction JSInliningHeuristic::Reduce(Node* node) { | 15 Reduction JSInliningHeuristic::Reduce(Node* node) { |
17 if (node->opcode() != IrOpcode::kJSCallFunction) return NoChange(); | 16 if (node->opcode() != IrOpcode::kJSCallFunction) return NoChange(); |
18 | 17 |
| 18 // Check if we already saw that {node} before, and if so, just skip it. |
| 19 if (seen_.find(node->id()) != seen_.end()) return NoChange(); |
| 20 seen_.insert(node->id()); |
| 21 |
19 Node* callee = node->InputAt(0); | 22 Node* callee = node->InputAt(0); |
20 HeapObjectMatcher match(callee); | 23 HeapObjectMatcher match(callee); |
21 if (!match.HasValue() || !match.Value()->IsJSFunction()) return NoChange(); | 24 if (!match.HasValue() || !match.Value()->IsJSFunction()) return NoChange(); |
22 Handle<JSFunction> function = Handle<JSFunction>::cast(match.Value()); | 25 Handle<JSFunction> function = Handle<JSFunction>::cast(match.Value()); |
23 | 26 |
24 // Functions marked with %SetForceInlineFlag are immediately inlined. | 27 // Functions marked with %SetForceInlineFlag are immediately inlined. |
25 if (function->shared()->force_inline()) { | 28 if (function->shared()->force_inline()) { |
26 return inliner_.ReduceJSCallFunction(node, function); | 29 return inliner_.ReduceJSCallFunction(node, function); |
27 } | 30 } |
28 | 31 |
(...skipping 23 matching lines...) Expand all Loading... |
52 | 55 |
53 // Quick check on the size of the AST to avoid parsing large candidate. | 56 // Quick check on the size of the AST to avoid parsing large candidate. |
54 if (function->shared()->ast_node_count() > FLAG_max_inlined_nodes) { | 57 if (function->shared()->ast_node_count() > FLAG_max_inlined_nodes) { |
55 return NoChange(); | 58 return NoChange(); |
56 } | 59 } |
57 | 60 |
58 // Avoid inlining within or across the boundary of asm.js code. | 61 // Avoid inlining within or across the boundary of asm.js code. |
59 if (info_->shared_info()->asm_function()) return NoChange(); | 62 if (info_->shared_info()->asm_function()) return NoChange(); |
60 if (function->shared()->asm_function()) return NoChange(); | 63 if (function->shared()->asm_function()) return NoChange(); |
61 | 64 |
| 65 // Stop inlinining once the maximum allowed level is reached. |
| 66 int level = 0; |
| 67 for (Node* frame_state = NodeProperties::GetFrameStateInput(node, 1); |
| 68 frame_state->opcode() == IrOpcode::kFrameState; |
| 69 frame_state = NodeProperties::GetFrameStateInput(frame_state, 0)) { |
| 70 if (++level > FLAG_max_inlining_levels) return NoChange(); |
| 71 } |
| 72 |
62 // Gather feedback on how often this call site has been hit before. | 73 // Gather feedback on how often this call site has been hit before. |
63 CallFunctionParameters p = CallFunctionParametersOf(node->op()); | 74 CallFunctionParameters p = CallFunctionParametersOf(node->op()); |
64 int calls = -1; // Same default as CallICNexus::ExtractCallCount. | 75 int calls = -1; // Same default as CallICNexus::ExtractCallCount. |
65 if (p.feedback().IsValid()) { | 76 if (p.feedback().IsValid()) { |
66 CallICNexus nexus(p.feedback().vector(), p.feedback().slot()); | 77 CallICNexus nexus(p.feedback().vector(), p.feedback().slot()); |
67 calls = nexus.ExtractCallCount(); | 78 calls = nexus.ExtractCallCount(); |
68 } | 79 } |
69 | 80 |
70 // --------------------------------------------------------------------------- | 81 // --------------------------------------------------------------------------- |
71 // Everything above this line is part of the inlining heuristic. | 82 // Everything above this line is part of the inlining heuristic. |
72 // --------------------------------------------------------------------------- | 83 // --------------------------------------------------------------------------- |
73 | 84 |
74 // In the general case we remember the candidate for later. | 85 // In the general case we remember the candidate for later. |
75 candidates_.insert({function, node, calls}); | 86 candidates_.insert({function, node, calls}); |
76 return NoChange(); | 87 return NoChange(); |
77 } | 88 } |
78 | 89 |
79 | 90 |
80 void JSInliningHeuristic::ProcessCandidates() { | 91 void JSInliningHeuristic::Finalize() { |
81 if (candidates_.empty()) return; // Nothing to do without candidates. | |
82 if (FLAG_trace_turbo_inlining) PrintCandidates(); | 92 if (FLAG_trace_turbo_inlining) PrintCandidates(); |
83 | 93 |
84 int cumulative_count = 0; | 94 while (!candidates_.empty()) { |
85 for (const Candidate& candidate : candidates_) { | 95 if (cumulative_count_ > FLAG_max_inlined_nodes_cumulative) break; |
86 if (cumulative_count > FLAG_max_inlined_nodes_cumulative) break; | 96 auto i = candidates_.begin(); |
| 97 Candidate const& candidate = *i; |
87 inliner_.ReduceJSCallFunction(candidate.node, candidate.function); | 98 inliner_.ReduceJSCallFunction(candidate.node, candidate.function); |
88 cumulative_count += candidate.function->shared()->ast_node_count(); | 99 cumulative_count_ += candidate.function->shared()->ast_node_count(); |
| 100 candidates_.erase(i); |
89 } | 101 } |
90 | |
91 // TODO(mstarzinger): Temporary workaround to eliminate dead control from the | |
92 // graph being introduced by the inliner. Make this part of the pipeline. | |
93 GraphReducer graph_reducer(local_zone_, jsgraph_->graph(), jsgraph_->Dead()); | |
94 DeadCodeElimination dead_code_elimination(&graph_reducer, jsgraph_->graph(), | |
95 jsgraph_->common()); | |
96 graph_reducer.AddReducer(&dead_code_elimination); | |
97 graph_reducer.ReduceGraph(); | |
98 } | 102 } |
99 | 103 |
100 | 104 |
101 bool JSInliningHeuristic::CandidateCompare::operator()( | 105 bool JSInliningHeuristic::CandidateCompare::operator()( |
102 const Candidate& left, const Candidate& right) const { | 106 const Candidate& left, const Candidate& right) const { |
103 return left.node != right.node && left.calls >= right.calls; | 107 return left.node != right.node && left.calls >= right.calls; |
104 } | 108 } |
105 | 109 |
106 | 110 |
107 void JSInliningHeuristic::PrintCandidates() { | 111 void JSInliningHeuristic::PrintCandidates() { |
108 PrintF("Candidates for inlining (size=%zu):\n", candidates_.size()); | 112 PrintF("Candidates for inlining (size=%zu):\n", candidates_.size()); |
109 for (const Candidate& candidate : candidates_) { | 113 for (const Candidate& candidate : candidates_) { |
110 PrintF(" id:%d, calls:%d, size[source]:%d, size[ast]:%d / %s\n", | 114 PrintF(" id:%d, calls:%d, size[source]:%d, size[ast]:%d / %s\n", |
111 candidate.node->id(), candidate.calls, | 115 candidate.node->id(), candidate.calls, |
112 candidate.function->shared()->SourceSize(), | 116 candidate.function->shared()->SourceSize(), |
113 candidate.function->shared()->ast_node_count(), | 117 candidate.function->shared()->ast_node_count(), |
114 candidate.function->shared()->DebugName()->ToCString().get()); | 118 candidate.function->shared()->DebugName()->ToCString().get()); |
115 } | 119 } |
116 } | 120 } |
117 | 121 |
118 } // namespace compiler | 122 } // namespace compiler |
119 } // namespace internal | 123 } // namespace internal |
120 } // namespace v8 | 124 } // namespace v8 |
OLD | NEW |