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/dead-code-elimination.h" | 5 #include "src/compiler/dead-code-elimination.h" |
6 | 6 |
7 #include "src/compiler/common-operator.h" | 7 #include "src/compiler/common-operator.h" |
8 #include "src/compiler/graph.h" | 8 #include "src/compiler/graph.h" |
9 #include "src/compiler/node-properties.h" | 9 #include "src/compiler/node-properties.h" |
10 #include "src/compiler/operator-properties.h" | 10 #include "src/compiler/operator-properties.h" |
(...skipping 23 matching lines...) Expand all Loading... |
34 default: | 34 default: |
35 return ReduceNode(node); | 35 return ReduceNode(node); |
36 } | 36 } |
37 UNREACHABLE(); | 37 UNREACHABLE(); |
38 return NoChange(); | 38 return NoChange(); |
39 } | 39 } |
40 | 40 |
41 | 41 |
42 Reduction DeadCodeElimination::ReduceEnd(Node* node) { | 42 Reduction DeadCodeElimination::ReduceEnd(Node* node) { |
43 DCHECK_EQ(IrOpcode::kEnd, node->opcode()); | 43 DCHECK_EQ(IrOpcode::kEnd, node->opcode()); |
44 int const input_count = node->InputCount(); | 44 Node::Inputs inputs = node->inputs(); |
45 DCHECK_LE(1, input_count); | 45 DCHECK_LE(1, inputs.count()); |
46 int live_input_count = 0; | 46 int live_input_count = 0; |
47 for (int i = 0; i < input_count; ++i) { | 47 for (int i = 0; i < inputs.count(); ++i) { |
48 Node* const input = node->InputAt(i); | 48 Node* const input = inputs[i]; |
49 // Skip dead inputs. | 49 // Skip dead inputs. |
50 if (input->opcode() == IrOpcode::kDead) continue; | 50 if (input->opcode() == IrOpcode::kDead) continue; |
51 // Compact live inputs. | 51 // Compact live inputs. |
52 if (i != live_input_count) node->ReplaceInput(live_input_count, input); | 52 if (i != live_input_count) node->ReplaceInput(live_input_count, input); |
53 ++live_input_count; | 53 ++live_input_count; |
54 } | 54 } |
55 if (live_input_count == 0) { | 55 if (live_input_count == 0) { |
56 return Replace(dead()); | 56 return Replace(dead()); |
57 } else if (live_input_count < input_count) { | 57 } else if (live_input_count < inputs.count()) { |
58 node->TrimInputCount(live_input_count); | 58 node->TrimInputCount(live_input_count); |
59 NodeProperties::ChangeOp(node, common()->End(live_input_count)); | 59 NodeProperties::ChangeOp(node, common()->End(live_input_count)); |
60 return Changed(node); | 60 return Changed(node); |
61 } | 61 } |
62 DCHECK_EQ(input_count, live_input_count); | 62 DCHECK_EQ(inputs.count(), live_input_count); |
63 return NoChange(); | 63 return NoChange(); |
64 } | 64 } |
65 | 65 |
66 | 66 |
67 Reduction DeadCodeElimination::ReduceLoopOrMerge(Node* node) { | 67 Reduction DeadCodeElimination::ReduceLoopOrMerge(Node* node) { |
68 DCHECK(IrOpcode::IsMergeOpcode(node->opcode())); | 68 DCHECK(IrOpcode::IsMergeOpcode(node->opcode())); |
69 int const input_count = node->InputCount(); | 69 Node::Inputs inputs = node->inputs(); |
70 DCHECK_LE(1, input_count); | 70 DCHECK_LE(1, inputs.count()); |
71 // Count the number of live inputs to {node} and compact them on the fly, also | 71 // Count the number of live inputs to {node} and compact them on the fly, also |
72 // compacting the inputs of the associated {Phi} and {EffectPhi} uses at the | 72 // compacting the inputs of the associated {Phi} and {EffectPhi} uses at the |
73 // same time. We consider {Loop}s dead even if only the first control input | 73 // same time. We consider {Loop}s dead even if only the first control input |
74 // is dead. | 74 // is dead. |
75 int live_input_count = 0; | 75 int live_input_count = 0; |
76 if (node->opcode() != IrOpcode::kLoop || | 76 if (node->opcode() != IrOpcode::kLoop || |
77 node->InputAt(0)->opcode() != IrOpcode::kDead) { | 77 node->InputAt(0)->opcode() != IrOpcode::kDead) { |
78 for (int i = 0; i < input_count; ++i) { | 78 for (int i = 0; i < inputs.count(); ++i) { |
79 Node* const input = node->InputAt(i); | 79 Node* const input = inputs[i]; |
80 // Skip dead inputs. | 80 // Skip dead inputs. |
81 if (input->opcode() == IrOpcode::kDead) continue; | 81 if (input->opcode() == IrOpcode::kDead) continue; |
82 // Compact live inputs. | 82 // Compact live inputs. |
83 if (live_input_count != i) { | 83 if (live_input_count != i) { |
84 node->ReplaceInput(live_input_count, input); | 84 node->ReplaceInput(live_input_count, input); |
85 for (Node* const use : node->uses()) { | 85 for (Node* const use : node->uses()) { |
86 if (NodeProperties::IsPhi(use)) { | 86 if (NodeProperties::IsPhi(use)) { |
87 DCHECK_EQ(input_count + 1, use->InputCount()); | 87 DCHECK_EQ(inputs.count() + 1, use->InputCount()); |
88 use->ReplaceInput(live_input_count, use->InputAt(i)); | 88 use->ReplaceInput(live_input_count, use->InputAt(i)); |
89 } | 89 } |
90 } | 90 } |
91 } | 91 } |
92 ++live_input_count; | 92 ++live_input_count; |
93 } | 93 } |
94 } | 94 } |
95 if (live_input_count == 0) { | 95 if (live_input_count == 0) { |
96 return Replace(dead()); | 96 return Replace(dead()); |
97 } else if (live_input_count == 1) { | 97 } else if (live_input_count == 1) { |
98 // Due to compaction above, the live input is at offset 0. | 98 // Due to compaction above, the live input is at offset 0. |
99 for (Node* const use : node->uses()) { | 99 for (Node* const use : node->uses()) { |
100 if (NodeProperties::IsPhi(use)) { | 100 if (NodeProperties::IsPhi(use)) { |
101 Replace(use, use->InputAt(0)); | 101 Replace(use, use->InputAt(0)); |
102 } else if (use->opcode() == IrOpcode::kLoopExit && | 102 } else if (use->opcode() == IrOpcode::kLoopExit && |
103 use->InputAt(1) == node) { | 103 use->InputAt(1) == node) { |
104 RemoveLoopExit(use); | 104 RemoveLoopExit(use); |
105 } else if (use->opcode() == IrOpcode::kTerminate) { | 105 } else if (use->opcode() == IrOpcode::kTerminate) { |
106 DCHECK_EQ(IrOpcode::kLoop, node->opcode()); | 106 DCHECK_EQ(IrOpcode::kLoop, node->opcode()); |
107 Replace(use, dead()); | 107 Replace(use, dead()); |
108 } | 108 } |
109 } | 109 } |
110 return Replace(node->InputAt(0)); | 110 return Replace(node->InputAt(0)); |
111 } | 111 } |
112 DCHECK_LE(2, live_input_count); | 112 DCHECK_LE(2, live_input_count); |
113 DCHECK_LE(live_input_count, input_count); | 113 DCHECK_LE(live_input_count, inputs.count()); |
114 // Trim input count for the {Merge} or {Loop} node. | 114 // Trim input count for the {Merge} or {Loop} node. |
115 if (live_input_count < input_count) { | 115 if (live_input_count < inputs.count()) { |
116 // Trim input counts for all phi uses and revisit them. | 116 // Trim input counts for all phi uses and revisit them. |
117 for (Node* const use : node->uses()) { | 117 for (Node* const use : node->uses()) { |
118 if (NodeProperties::IsPhi(use)) { | 118 if (NodeProperties::IsPhi(use)) { |
119 use->ReplaceInput(live_input_count, node); | 119 use->ReplaceInput(live_input_count, node); |
120 TrimMergeOrPhi(use, live_input_count); | 120 TrimMergeOrPhi(use, live_input_count); |
121 Revisit(use); | 121 Revisit(use); |
122 } | 122 } |
123 } | 123 } |
124 TrimMergeOrPhi(node, live_input_count); | 124 TrimMergeOrPhi(node, live_input_count); |
125 return Changed(node); | 125 return Changed(node); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 | 163 |
164 void DeadCodeElimination::TrimMergeOrPhi(Node* node, int size) { | 164 void DeadCodeElimination::TrimMergeOrPhi(Node* node, int size) { |
165 const Operator* const op = common()->ResizeMergeOrPhi(node->op(), size); | 165 const Operator* const op = common()->ResizeMergeOrPhi(node->op(), size); |
166 node->TrimInputCount(OperatorProperties::GetTotalInputCount(op)); | 166 node->TrimInputCount(OperatorProperties::GetTotalInputCount(op)); |
167 NodeProperties::ChangeOp(node, op); | 167 NodeProperties::ChangeOp(node, op); |
168 } | 168 } |
169 | 169 |
170 } // namespace compiler | 170 } // namespace compiler |
171 } // namespace internal | 171 } // namespace internal |
172 } // namespace v8 | 172 } // namespace v8 |
OLD | NEW |