| 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/compilation-info.h" | 7 #include "src/compilation-info.h" |
| 8 #include "src/compiler/common-operator.h" | 8 #include "src/compiler/common-operator.h" |
| 9 #include "src/compiler/node-matchers.h" | 9 #include "src/compiler/node-matchers.h" |
| 10 #include "src/compiler/simplified-operator.h" | 10 #include "src/compiler/simplified-operator.h" |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 return NoChange(); | 142 return NoChange(); |
| 143 case kStressInlining: | 143 case kStressInlining: |
| 144 return InlineCandidate(candidate); | 144 return InlineCandidate(candidate); |
| 145 case kGeneralInlining: | 145 case kGeneralInlining: |
| 146 break; | 146 break; |
| 147 } | 147 } |
| 148 | 148 |
| 149 // Don't consider a {candidate} whose frequency is below the | 149 // Don't consider a {candidate} whose frequency is below the |
| 150 // threshold, i.e. a call site that is only hit once every N | 150 // threshold, i.e. a call site that is only hit once every N |
| 151 // invocations of the caller. | 151 // invocations of the caller. |
| 152 if (candidate.frequency < FLAG_min_inlining_frequency) { | 152 if (candidate.frequency.IsKnown() && |
| 153 candidate.frequency.value() < FLAG_min_inlining_frequency) { |
| 153 return NoChange(); | 154 return NoChange(); |
| 154 } | 155 } |
| 155 | 156 |
| 156 // In the general case we remember the candidate for later. | 157 // In the general case we remember the candidate for later. |
| 157 candidates_.insert(candidate); | 158 candidates_.insert(candidate); |
| 158 return NoChange(); | 159 return NoChange(); |
| 159 } | 160 } |
| 160 | 161 |
| 161 void JSInliningHeuristic::Finalize() { | 162 void JSInliningHeuristic::Finalize() { |
| 162 if (candidates_.empty()) return; // Nothing to do without candidates. | 163 if (candidates_.empty()) return; // Nothing to do without candidates. |
| 163 if (FLAG_trace_turbo_inlining) PrintCandidates(); | 164 if (FLAG_trace_turbo_inlining) PrintCandidates(); |
| 164 | 165 |
| 165 // We inline at most one candidate in every iteration of the fixpoint. | 166 // We inline at most one candidate in every iteration of the fixpoint. |
| 166 // This is to ensure that we don't consume the full inlining budget | 167 // This is to ensure that we don't consume the full inlining budget |
| 167 // on things that aren't called very often. | 168 // on things that aren't called very often. |
| 168 // TODO(bmeurer): Use std::priority_queue instead of std::set here. | 169 // TODO(bmeurer): Use std::priority_queue instead of std::set here. |
| 169 while (!candidates_.empty()) { | 170 while (!candidates_.empty()) { |
| 170 if (cumulative_count_ > FLAG_max_inlined_nodes_cumulative) return; | 171 if (cumulative_count_ > FLAG_max_inlined_nodes_cumulative) return; |
| 171 auto i = candidates_.begin(); | 172 auto i = candidates_.begin(); |
| 172 Candidate candidate = *i; | 173 Candidate candidate = *i; |
| 173 candidates_.erase(i); | 174 candidates_.erase(i); |
| 174 // Only include candidates that we've successfully called before. | |
| 175 // The candidate list is sorted, so we can exit at the first occurance of | |
| 176 // frequency 0 in the list. | |
| 177 if (candidate.frequency <= 0.0) return; | |
| 178 // Make sure we don't try to inline dead candidate nodes. | 175 // Make sure we don't try to inline dead candidate nodes. |
| 179 if (!candidate.node->IsDead()) { | 176 if (!candidate.node->IsDead()) { |
| 180 Reduction const reduction = InlineCandidate(candidate); | 177 Reduction const reduction = InlineCandidate(candidate); |
| 181 if (reduction.Changed()) return; | 178 if (reduction.Changed()) return; |
| 182 } | 179 } |
| 183 } | 180 } |
| 184 } | 181 } |
| 185 | 182 |
| 186 Reduction JSInliningHeuristic::InlineCandidate(Candidate const& candidate) { | 183 Reduction JSInliningHeuristic::InlineCandidate(Candidate const& candidate) { |
| 187 int const num_calls = candidate.num_functions; | 184 int const num_calls = candidate.num_functions; |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 node->Kill(); | 279 node->Kill(); |
| 283 cumulative_count_ += function->shared()->ast_node_count(); | 280 cumulative_count_ += function->shared()->ast_node_count(); |
| 284 } | 281 } |
| 285 } | 282 } |
| 286 | 283 |
| 287 return Replace(value); | 284 return Replace(value); |
| 288 } | 285 } |
| 289 | 286 |
| 290 bool JSInliningHeuristic::CandidateCompare::operator()( | 287 bool JSInliningHeuristic::CandidateCompare::operator()( |
| 291 const Candidate& left, const Candidate& right) const { | 288 const Candidate& left, const Candidate& right) const { |
| 292 if (left.frequency > right.frequency) { | 289 if (right.frequency.IsUnknown()) { |
| 293 return true; | 290 return true; |
| 294 } else if (left.frequency < right.frequency) { | 291 } else if (left.frequency.IsUnknown()) { |
| 292 return false; |
| 293 } else if (left.frequency.value() > right.frequency.value()) { |
| 294 return true; |
| 295 } else if (left.frequency.value() < right.frequency.value()) { |
| 295 return false; | 296 return false; |
| 296 } else { | 297 } else { |
| 297 return left.node->id() > right.node->id(); | 298 return left.node->id() > right.node->id(); |
| 298 } | 299 } |
| 299 } | 300 } |
| 300 | 301 |
| 301 void JSInliningHeuristic::PrintCandidates() { | 302 void JSInliningHeuristic::PrintCandidates() { |
| 302 PrintF("Candidates for inlining (size=%zu):\n", candidates_.size()); | 303 OFStream os(stdout); |
| 304 os << "Candidates for inlining (size=" << candidates_.size() << "):\n"; |
| 303 for (const Candidate& candidate : candidates_) { | 305 for (const Candidate& candidate : candidates_) { |
| 304 PrintF(" #%d:%s, frequency:%g\n", candidate.node->id(), | 306 os << " #" << candidate.node->id() << ":" |
| 305 candidate.node->op()->mnemonic(), candidate.frequency); | 307 << candidate.node->op()->mnemonic() |
| 308 << ", frequency: " << candidate.frequency << std::endl; |
| 306 for (int i = 0; i < candidate.num_functions; ++i) { | 309 for (int i = 0; i < candidate.num_functions; ++i) { |
| 307 Handle<SharedFunctionInfo> shared = | 310 Handle<SharedFunctionInfo> shared = |
| 308 candidate.functions[i].is_null() | 311 candidate.functions[i].is_null() |
| 309 ? candidate.shared_info | 312 ? candidate.shared_info |
| 310 : handle(candidate.functions[i]->shared()); | 313 : handle(candidate.functions[i]->shared()); |
| 311 PrintF(" - size:%d, name: %s\n", shared->ast_node_count(), | 314 PrintF(" - size:%d, name: %s\n", shared->ast_node_count(), |
| 312 shared->DebugName()->ToCString().get()); | 315 shared->DebugName()->ToCString().get()); |
| 313 } | 316 } |
| 314 } | 317 } |
| 315 } | 318 } |
| 316 | 319 |
| 317 Graph* JSInliningHeuristic::graph() const { return jsgraph()->graph(); } | 320 Graph* JSInliningHeuristic::graph() const { return jsgraph()->graph(); } |
| 318 | 321 |
| 319 CommonOperatorBuilder* JSInliningHeuristic::common() const { | 322 CommonOperatorBuilder* JSInliningHeuristic::common() const { |
| 320 return jsgraph()->common(); | 323 return jsgraph()->common(); |
| 321 } | 324 } |
| 322 | 325 |
| 323 SimplifiedOperatorBuilder* JSInliningHeuristic::simplified() const { | 326 SimplifiedOperatorBuilder* JSInliningHeuristic::simplified() const { |
| 324 return jsgraph()->simplified(); | 327 return jsgraph()->simplified(); |
| 325 } | 328 } |
| 326 | 329 |
| 327 } // namespace compiler | 330 } // namespace compiler |
| 328 } // namespace internal | 331 } // namespace internal |
| 329 } // namespace v8 | 332 } // namespace v8 |
| OLD | NEW |