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 10 matching lines...) Expand all Loading... |
21 dead_(graph->NewNode(common->Dead())) {} | 21 dead_(graph->NewNode(common->Dead())) {} |
22 | 22 |
23 | 23 |
24 Reduction DeadCodeElimination::Reduce(Node* node) { | 24 Reduction DeadCodeElimination::Reduce(Node* node) { |
25 switch (node->opcode()) { | 25 switch (node->opcode()) { |
26 case IrOpcode::kEnd: | 26 case IrOpcode::kEnd: |
27 return ReduceEnd(node); | 27 return ReduceEnd(node); |
28 case IrOpcode::kLoop: | 28 case IrOpcode::kLoop: |
29 case IrOpcode::kMerge: | 29 case IrOpcode::kMerge: |
30 return ReduceLoopOrMerge(node); | 30 return ReduceLoopOrMerge(node); |
| 31 case IrOpcode::kLoopExit: |
| 32 return ReduceLoopExit(node); |
31 default: | 33 default: |
32 return ReduceNode(node); | 34 return ReduceNode(node); |
33 } | 35 } |
34 UNREACHABLE(); | 36 UNREACHABLE(); |
35 return NoChange(); | 37 return NoChange(); |
36 } | 38 } |
37 | 39 |
38 | 40 |
39 Reduction DeadCodeElimination::ReduceEnd(Node* node) { | 41 Reduction DeadCodeElimination::ReduceEnd(Node* node) { |
40 DCHECK_EQ(IrOpcode::kEnd, node->opcode()); | 42 DCHECK_EQ(IrOpcode::kEnd, node->opcode()); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 ++live_input_count; | 91 ++live_input_count; |
90 } | 92 } |
91 } | 93 } |
92 if (live_input_count == 0) { | 94 if (live_input_count == 0) { |
93 return Replace(dead()); | 95 return Replace(dead()); |
94 } else if (live_input_count == 1) { | 96 } else if (live_input_count == 1) { |
95 // Due to compaction above, the live input is at offset 0. | 97 // Due to compaction above, the live input is at offset 0. |
96 for (Node* const use : node->uses()) { | 98 for (Node* const use : node->uses()) { |
97 if (NodeProperties::IsPhi(use)) { | 99 if (NodeProperties::IsPhi(use)) { |
98 Replace(use, use->InputAt(0)); | 100 Replace(use, use->InputAt(0)); |
| 101 } else if (use->opcode() == IrOpcode::kLoopExit && |
| 102 use->InputAt(1) == node) { |
| 103 RemoveLoopExit(use); |
99 } else if (use->opcode() == IrOpcode::kTerminate) { | 104 } else if (use->opcode() == IrOpcode::kTerminate) { |
100 DCHECK_EQ(IrOpcode::kLoop, node->opcode()); | 105 DCHECK_EQ(IrOpcode::kLoop, node->opcode()); |
101 Replace(use, dead()); | 106 Replace(use, dead()); |
102 } | 107 } |
103 } | 108 } |
104 return Replace(node->InputAt(0)); | 109 return Replace(node->InputAt(0)); |
105 } | 110 } |
106 DCHECK_LE(2, live_input_count); | 111 DCHECK_LE(2, live_input_count); |
107 DCHECK_LE(live_input_count, input_count); | 112 DCHECK_LE(live_input_count, input_count); |
108 // Trim input count for the {Merge} or {Loop} node. | 113 // Trim input count for the {Merge} or {Loop} node. |
109 if (live_input_count < input_count) { | 114 if (live_input_count < input_count) { |
110 // Trim input counts for all phi uses and revisit them. | 115 // Trim input counts for all phi uses and revisit them. |
111 for (Node* const use : node->uses()) { | 116 for (Node* const use : node->uses()) { |
112 if (NodeProperties::IsPhi(use)) { | 117 if (NodeProperties::IsPhi(use)) { |
113 use->ReplaceInput(live_input_count, node); | 118 use->ReplaceInput(live_input_count, node); |
114 TrimMergeOrPhi(use, live_input_count); | 119 TrimMergeOrPhi(use, live_input_count); |
115 Revisit(use); | 120 Revisit(use); |
116 } | 121 } |
117 } | 122 } |
118 TrimMergeOrPhi(node, live_input_count); | 123 TrimMergeOrPhi(node, live_input_count); |
119 return Changed(node); | 124 return Changed(node); |
120 } | 125 } |
121 return NoChange(); | 126 return NoChange(); |
122 } | 127 } |
123 | 128 |
| 129 Reduction DeadCodeElimination::RemoveLoopExit(Node* node) { |
| 130 DCHECK_EQ(IrOpcode::kLoopExit, node->opcode()); |
| 131 for (Node* const use : node->uses()) { |
| 132 if (use->opcode() == IrOpcode::kLoopExitValue || |
| 133 use->opcode() == IrOpcode::kLoopExitEffect) { |
| 134 Replace(use, use->InputAt(0)); |
| 135 } |
| 136 } |
| 137 Node* control = NodeProperties::GetControlInput(node, 0); |
| 138 Replace(node, control); |
| 139 return Replace(control); |
| 140 } |
124 | 141 |
125 Reduction DeadCodeElimination::ReduceNode(Node* node) { | 142 Reduction DeadCodeElimination::ReduceNode(Node* node) { |
126 // If {node} has exactly one control input and this is {Dead}, | 143 // If {node} has exactly one control input and this is {Dead}, |
127 // replace {node} with {Dead}. | 144 // replace {node} with {Dead}. |
128 int const control_input_count = node->op()->ControlInputCount(); | 145 int const control_input_count = node->op()->ControlInputCount(); |
129 if (control_input_count == 0) return NoChange(); | 146 if (control_input_count == 0) return NoChange(); |
130 DCHECK_EQ(1, control_input_count); | 147 DCHECK_EQ(1, control_input_count); |
131 Node* control = NodeProperties::GetControlInput(node); | 148 Node* control = NodeProperties::GetControlInput(node); |
132 if (control->opcode() == IrOpcode::kDead) return Replace(control); | 149 if (control->opcode() == IrOpcode::kDead) return Replace(control); |
133 return NoChange(); | 150 return NoChange(); |
134 } | 151 } |
135 | 152 |
| 153 Reduction DeadCodeElimination::ReduceLoopExit(Node* node) { |
| 154 Node* control = NodeProperties::GetControlInput(node, 0); |
| 155 Node* loop = NodeProperties::GetControlInput(node, 1); |
| 156 if (control->opcode() == IrOpcode::kDead || |
| 157 loop->opcode() == IrOpcode::kDead) { |
| 158 return RemoveLoopExit(node); |
| 159 } |
| 160 return NoChange(); |
| 161 } |
136 | 162 |
137 void DeadCodeElimination::TrimMergeOrPhi(Node* node, int size) { | 163 void DeadCodeElimination::TrimMergeOrPhi(Node* node, int size) { |
138 const Operator* const op = common()->ResizeMergeOrPhi(node->op(), size); | 164 const Operator* const op = common()->ResizeMergeOrPhi(node->op(), size); |
139 node->TrimInputCount(OperatorProperties::GetTotalInputCount(op)); | 165 node->TrimInputCount(OperatorProperties::GetTotalInputCount(op)); |
140 NodeProperties::ChangeOp(node, op); | 166 NodeProperties::ChangeOp(node, op); |
141 } | 167 } |
142 | 168 |
143 } // namespace compiler | 169 } // namespace compiler |
144 } // namespace internal | 170 } // namespace internal |
145 } // namespace v8 | 171 } // namespace v8 |
OLD | NEW |