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* cond = node->InputAt(0); | 29 Node* const cond = node->InputAt(0); |
30 Node* vthen = node->InputAt(1); | |
31 Node* velse = node->InputAt(2); | |
32 Node* merge = nullptr; | |
33 | 30 |
34 // Check if we already have a diamond for this condition. | 31 // Check if we already have a diamond for this condition. |
35 auto range = merges_.equal_range(cond); | 32 auto i = merges_.find(cond); |
36 for (auto i = range.first;; ++i) { | 33 if (i == merges_.end()) { |
37 if (i == range.second) { | 34 // Create a new diamond for this condition and remember its merge node. |
38 // Create a new diamond for this condition and remember its merge node. | 35 Diamond d(graph(), common(), cond, p.hint()); |
39 Diamond d(graph(), common(), cond, p.hint()); | 36 i = merges_.insert(std::make_pair(cond, d.merge)).first; |
40 merges_.insert(std::make_pair(cond, d.merge)); | 37 } |
41 merge = d.merge; | |
42 break; | |
43 } | |
44 | 38 |
45 // If the diamond is reachable from the Select, merging them would result in | 39 DCHECK_EQ(cond, i->first); |
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 } | |
51 } | |
52 | 40 |
53 // Create a Phi hanging off the previously determined merge. | 41 // Create a Phi hanging off the previously determined merge. |
54 node->set_op(common()->Phi(p.type(), 2)); | 42 node->set_op(common()->Phi(p.type(), 2)); |
55 node->ReplaceInput(0, vthen); | 43 node->ReplaceInput(0, node->InputAt(1)); |
56 node->ReplaceInput(1, velse); | 44 node->ReplaceInput(1, node->InputAt(2)); |
57 node->ReplaceInput(2, merge); | 45 node->ReplaceInput(2, i->second); |
58 return Changed(node); | 46 return Changed(node); |
59 } | 47 } |
60 | 48 |
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 | |
81 } // namespace compiler | 49 } // namespace compiler |
82 } // namespace internal | 50 } // namespace internal |
83 } // namespace v8 | 51 } // namespace v8 |
OLD | NEW |