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/select-lowering.h" | 5 #include "src/compiler/select-lowering.h" |
6 | 6 |
7 #include "src/compiler/common-operator.h" | 7 #include "src/compiler/common-operator.h" |
8 #include "src/compiler/diamond.h" | 8 #include "src/compiler/diamond.h" |
9 #include "src/compiler/generic-node-inl.h" | 9 #include "src/compiler/generic-node-inl.h" |
10 #include "src/compiler/graph.h" | 10 #include "src/compiler/graph.h" |
11 | 11 |
12 namespace v8 { | 12 namespace v8 { |
13 namespace internal { | 13 namespace internal { |
14 namespace compiler { | 14 namespace compiler { |
15 | 15 |
16 SelectLowering::SelectLowering(Graph* graph, CommonOperatorBuilder* common) | 16 SelectLowering::SelectLowering(Graph* graph, CommonOperatorBuilder* common) |
17 : common_(common), | 17 : common_(common), |
18 graph_(graph), | 18 graph_(graph), |
19 merges_(Merges::key_compare(), Merges::allocator_type(graph->zone())) {} | 19 merges_(Merges::key_compare(), Merges::allocator_type(graph->zone())) {} |
20 | 20 |
21 | 21 |
22 SelectLowering::~SelectLowering() {} | 22 SelectLowering::~SelectLowering() {} |
23 | 23 |
24 | 24 |
25 Reduction SelectLowering::Reduce(Node* node) { | 25 Reduction SelectLowering::Reduce(Node* node) { |
26 if (node->opcode() != IrOpcode::kSelect) return NoChange(); | 26 if (node->opcode() != IrOpcode::kSelect) return NoChange(); |
27 SelectParameters const p = SelectParametersOf(node->op()); | 27 SelectParameters const p = SelectParametersOf(node->op()); |
28 | 28 |
29 Node* const cond = node->InputAt(0); | 29 Node* cond = node->InputAt(0); |
| 30 Node* vthen = node->InputAt(1); |
| 31 Node* velse = node->InputAt(2); |
| 32 Node* merge = nullptr; |
30 | 33 |
31 // Check if we already have a diamond for this condition. | 34 // Check if we already have a diamond for this condition. |
32 auto i = merges_.find(cond); | 35 auto range = merges_.equal_range(cond); |
33 if (i == merges_.end()) { | 36 for (auto i = range.first;; ++i) { |
34 // Create a new diamond for this condition and remember its merge node. | 37 if (i == range.second) { |
35 Diamond d(graph(), common(), cond, p.hint()); | 38 // Create a new diamond for this condition and remember its merge node. |
36 i = merges_.insert(std::make_pair(cond, d.merge)).first; | 39 Diamond d(graph(), common(), cond, p.hint()); |
| 40 merges_.insert(std::make_pair(cond, d.merge)); |
| 41 merge = d.merge; |
| 42 break; |
| 43 } |
| 44 |
| 45 // If the diamond is reachable from the Select, merging them would result in |
| 46 // an unschedulable graph, so we cannot reuse the diamond in that case. |
| 47 merge = i->second; |
| 48 if (!ReachableFrom(merge, node)) { |
| 49 break; |
| 50 } |
37 } | 51 } |
38 | 52 |
39 DCHECK_EQ(cond, i->first); | |
40 | |
41 // Create a Phi hanging off the previously determined merge. | 53 // Create a Phi hanging off the previously determined merge. |
42 node->set_op(common()->Phi(p.type(), 2)); | 54 node->set_op(common()->Phi(p.type(), 2)); |
43 node->ReplaceInput(0, node->InputAt(1)); | 55 node->ReplaceInput(0, vthen); |
44 node->ReplaceInput(1, node->InputAt(2)); | 56 node->ReplaceInput(1, velse); |
45 node->ReplaceInput(2, i->second); | 57 node->ReplaceInput(2, merge); |
46 return Changed(node); | 58 return Changed(node); |
47 } | 59 } |
48 | 60 |
| 61 |
| 62 bool SelectLowering::ReachableFrom(Node* const sink, Node* const source) { |
| 63 // TODO(turbofan): This is probably horribly expensive, and it should be moved |
| 64 // into node.h or somewhere else?! |
| 65 Zone zone(graph()->zone()->isolate()); |
| 66 std::queue<Node*, NodeDeque> pending((NodeDeque(&zone))); |
| 67 BoolVector visited(graph()->NodeCount(), false, &zone); |
| 68 pending.push(source); |
| 69 while (!pending.empty()) { |
| 70 Node* current = pending.front(); |
| 71 if (current == sink) return true; |
| 72 pending.pop(); |
| 73 visited[current->id()] = true; |
| 74 for (auto input : current->inputs()) { |
| 75 if (!visited[input->id()]) pending.push(input); |
| 76 } |
| 77 } |
| 78 return false; |
| 79 } |
| 80 |
49 } // namespace compiler | 81 } // namespace compiler |
50 } // namespace internal | 82 } // namespace internal |
51 } // namespace v8 | 83 } // namespace v8 |
OLD | NEW |