OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 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/osr.h" | 5 #include "src/compiler/osr.h" |
6 #include "src/ast/scopes.h" | 6 #include "src/ast/scopes.h" |
7 #include "src/compilation-info.h" | 7 #include "src/compilation-info.h" |
8 #include "src/compiler/all-nodes.h" | 8 #include "src/compiler/all-nodes.h" |
9 #include "src/compiler/common-operator-reducer.h" | 9 #include "src/compiler/common-operator-reducer.h" |
10 #include "src/compiler/common-operator.h" | 10 #include "src/compiler/common-operator.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 #define TRACE_COND (FLAG_trace_turbo_graph && FLAG_trace_osr) | 40 #define TRACE_COND (FLAG_trace_turbo_graph && FLAG_trace_osr) |
41 #else | 41 #else |
42 #define TRACE_COND false | 42 #define TRACE_COND false |
43 #endif | 43 #endif |
44 | 44 |
45 #define TRACE(...) \ | 45 #define TRACE(...) \ |
46 do { \ | 46 do { \ |
47 if (TRACE_COND) PrintF(__VA_ARGS__); \ | 47 if (TRACE_COND) PrintF(__VA_ARGS__); \ |
48 } while (false) | 48 } while (false) |
49 | 49 |
50 namespace { | |
51 | 50 |
52 // Peel outer loops and rewire the graph so that control reduction can | 51 // Peel outer loops and rewire the graph so that control reduction can |
53 // produce a properly formed graph. | 52 // produce a properly formed graph. |
54 void PeelOuterLoopsForOsr(Graph* graph, CommonOperatorBuilder* common, | 53 static void PeelOuterLoopsForOsr(Graph* graph, CommonOperatorBuilder* common, |
55 Zone* tmp_zone, Node* dead, LoopTree* loop_tree, | 54 Zone* tmp_zone, Node* dead, |
56 LoopTree::Loop* osr_loop, Node* osr_normal_entry, | 55 LoopTree* loop_tree, LoopTree::Loop* osr_loop, |
57 Node* osr_loop_entry) { | 56 Node* osr_normal_entry, Node* osr_loop_entry) { |
58 const size_t original_count = graph->NodeCount(); | 57 const size_t original_count = graph->NodeCount(); |
59 AllNodes all(tmp_zone, graph); | 58 AllNodes all(tmp_zone, graph); |
60 NodeVector tmp_inputs(tmp_zone); | 59 NodeVector tmp_inputs(tmp_zone); |
61 Node* sentinel = graph->NewNode(dead->op()); | 60 Node* sentinel = graph->NewNode(dead->op()); |
62 | 61 |
63 // Make a copy of the graph for each outer loop. | 62 // Make a copy of the graph for each outer loop. |
64 ZoneVector<NodeVector*> copies(tmp_zone); | 63 ZoneVector<NodeVector*> copies(tmp_zone); |
65 for (LoopTree::Loop* loop = osr_loop->parent(); loop; loop = loop->parent()) { | 64 for (LoopTree::Loop* loop = osr_loop->parent(); loop; loop = loop->parent()) { |
66 void* stuff = tmp_zone->New(sizeof(NodeVector)); | 65 void* stuff = tmp_zone->New(sizeof(NodeVector)); |
67 NodeVector* mapping = | 66 NodeVector* mapping = |
(...skipping 19 matching lines...) Expand all Loading... |
87 | 86 |
88 // Copy all nodes. | 87 // Copy all nodes. |
89 for (size_t i = 0; i < all.reachable.size(); i++) { | 88 for (size_t i = 0; i < all.reachable.size(); i++) { |
90 Node* orig = all.reachable[i]; | 89 Node* orig = all.reachable[i]; |
91 Node* copy = mapping->at(orig->id()); | 90 Node* copy = mapping->at(orig->id()); |
92 if (copy != sentinel) { | 91 if (copy != sentinel) { |
93 // Mapping already exists. | 92 // Mapping already exists. |
94 continue; | 93 continue; |
95 } | 94 } |
96 if (orig->InputCount() == 0 || orig->opcode() == IrOpcode::kParameter || | 95 if (orig->InputCount() == 0 || orig->opcode() == IrOpcode::kParameter || |
97 orig->opcode() == IrOpcode::kOsrValue || | 96 orig->opcode() == IrOpcode::kOsrValue) { |
98 orig->opcode() == IrOpcode::kOsrGuard) { | |
99 // No need to copy leaf nodes or parameters. | 97 // No need to copy leaf nodes or parameters. |
100 mapping->at(orig->id()) = orig; | 98 mapping->at(orig->id()) = orig; |
101 continue; | 99 continue; |
102 } | 100 } |
103 | 101 |
104 // Copy the node. | 102 // Copy the node. |
105 tmp_inputs.clear(); | 103 tmp_inputs.clear(); |
106 for (Node* input : orig->inputs()) { | 104 for (Node* input : orig->inputs()) { |
107 tmp_inputs.push_back(mapping->at(input->id())); | 105 tmp_inputs.push_back(mapping->at(input->id())); |
108 } | 106 } |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 } | 248 } |
251 } | 249 } |
252 | 250 |
253 if (FLAG_trace_turbo_graph) { // Simple textual RPO. | 251 if (FLAG_trace_turbo_graph) { // Simple textual RPO. |
254 OFStream os(stdout); | 252 OFStream os(stdout); |
255 os << "-- Graph after OSR duplication -- " << std::endl; | 253 os << "-- Graph after OSR duplication -- " << std::endl; |
256 os << AsRPO(*graph); | 254 os << AsRPO(*graph); |
257 } | 255 } |
258 } | 256 } |
259 | 257 |
260 void SetTypeForOsrValue(Node* osr_value, Node* loop, | |
261 CommonOperatorBuilder* common) { | |
262 Node* osr_guard = nullptr; | |
263 for (Node* use : osr_value->uses()) { | |
264 if (use->opcode() == IrOpcode::kOsrGuard) { | |
265 DCHECK_EQ(use->InputAt(0), osr_value); | |
266 osr_guard = use; | |
267 break; | |
268 } | |
269 } | |
270 | |
271 OsrGuardType guard_type = OsrGuardType::kAny; | |
272 // Find the phi that uses the OsrGuard node and get the type from | |
273 // there. Skip the search if the OsrGuard does not have value use | |
274 // (i.e., if there is other use beyond the effect use). | |
275 if (osr_guard->UseCount() > 1) { | |
276 Type* type = nullptr; | |
277 for (Node* use : osr_guard->uses()) { | |
278 if (use->opcode() == IrOpcode::kPhi) { | |
279 if (NodeProperties::GetControlInput(use) != loop) continue; | |
280 CHECK_NULL(type); | |
281 type = NodeProperties::GetType(use); | |
282 } | |
283 } | |
284 CHECK_NOT_NULL(type); | |
285 | |
286 if (type->Is(Type::SignedSmall())) { | |
287 guard_type = OsrGuardType::kSignedSmall; | |
288 } | |
289 } | |
290 | |
291 NodeProperties::ChangeOp(osr_guard, common->OsrGuard(guard_type)); | |
292 } | |
293 | |
294 } // namespace | |
295 | 258 |
296 void OsrHelper::Deconstruct(JSGraph* jsgraph, CommonOperatorBuilder* common, | 259 void OsrHelper::Deconstruct(JSGraph* jsgraph, CommonOperatorBuilder* common, |
297 Zone* tmp_zone) { | 260 Zone* tmp_zone) { |
298 Graph* graph = jsgraph->graph(); | 261 Graph* graph = jsgraph->graph(); |
299 Node* osr_normal_entry = nullptr; | 262 Node* osr_normal_entry = nullptr; |
300 Node* osr_loop_entry = nullptr; | 263 Node* osr_loop_entry = nullptr; |
301 Node* osr_loop = nullptr; | 264 Node* osr_loop = nullptr; |
302 | 265 |
303 for (Node* node : graph->start()->uses()) { | 266 for (Node* node : graph->start()->uses()) { |
304 if (node->opcode() == IrOpcode::kOsrLoopEntry) { | 267 if (node->opcode() == IrOpcode::kOsrLoopEntry) { |
305 osr_loop_entry = node; // found the OSR loop entry | 268 osr_loop_entry = node; // found the OSR loop entry |
306 } else if (node->opcode() == IrOpcode::kOsrNormalEntry) { | 269 } else if (node->opcode() == IrOpcode::kOsrNormalEntry) { |
307 osr_normal_entry = node; | 270 osr_normal_entry = node; |
308 } | 271 } |
309 } | 272 } |
310 | 273 |
311 CHECK_NOT_NULL(osr_normal_entry); // Should have found the OSR normal entry. | 274 CHECK_NOT_NULL(osr_normal_entry); // Should have found the OSR normal entry. |
312 CHECK_NOT_NULL(osr_loop_entry); // Should have found the OSR loop entry. | 275 CHECK_NOT_NULL(osr_loop_entry); // Should have found the OSR loop entry. |
313 | 276 |
314 for (Node* use : osr_loop_entry->uses()) { | 277 for (Node* use : osr_loop_entry->uses()) { |
315 if (use->opcode() == IrOpcode::kLoop) { | 278 if (use->opcode() == IrOpcode::kLoop) { |
316 CHECK(!osr_loop); // should be only one OSR loop. | 279 CHECK(!osr_loop); // should be only one OSR loop. |
317 osr_loop = use; // found the OSR loop. | 280 osr_loop = use; // found the OSR loop. |
318 } | 281 } |
319 } | 282 } |
320 | 283 |
321 CHECK(osr_loop); // Should have found the OSR loop. | 284 CHECK(osr_loop); // Should have found the OSR loop. |
322 | 285 |
323 for (Node* use : osr_loop_entry->uses()) { | |
324 if (use->opcode() == IrOpcode::kOsrValue) { | |
325 SetTypeForOsrValue(use, osr_loop, common); | |
326 } | |
327 } | |
328 | |
329 // Analyze the graph to determine how deeply nested the OSR loop is. | 286 // Analyze the graph to determine how deeply nested the OSR loop is. |
330 LoopTree* loop_tree = LoopFinder::BuildLoopTree(graph, tmp_zone); | 287 LoopTree* loop_tree = LoopFinder::BuildLoopTree(graph, tmp_zone); |
331 | 288 |
332 Node* dead = jsgraph->Dead(); | 289 Node* dead = jsgraph->Dead(); |
333 LoopTree::Loop* loop = loop_tree->ContainingLoop(osr_loop); | 290 LoopTree::Loop* loop = loop_tree->ContainingLoop(osr_loop); |
334 if (loop->depth() > 0) { | 291 if (loop->depth() > 0) { |
335 PeelOuterLoopsForOsr(graph, common, tmp_zone, dead, loop_tree, loop, | 292 PeelOuterLoopsForOsr(graph, common, tmp_zone, dead, loop_tree, loop, |
336 osr_normal_entry, osr_loop_entry); | 293 osr_normal_entry, osr_loop_entry); |
337 } | 294 } |
338 | 295 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 | 332 |
376 void OsrHelper::SetupFrame(Frame* frame) { | 333 void OsrHelper::SetupFrame(Frame* frame) { |
377 // The optimized frame will subsume the unoptimized frame. Do so by reserving | 334 // The optimized frame will subsume the unoptimized frame. Do so by reserving |
378 // the first spill slots. | 335 // the first spill slots. |
379 frame->ReserveSpillSlots(UnoptimizedFrameSlots()); | 336 frame->ReserveSpillSlots(UnoptimizedFrameSlots()); |
380 } | 337 } |
381 | 338 |
382 } // namespace compiler | 339 } // namespace compiler |
383 } // namespace internal | 340 } // namespace internal |
384 } // namespace v8 | 341 } // namespace v8 |
OLD | NEW |