Chromium Code Reviews| 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/branch-elimination.h" | 5 #include "src/compiler/branch-elimination.h" |
| 6 | 6 |
| 7 #include "src/compiler/js-graph.h" | 7 #include "src/compiler/js-graph.h" |
| 8 #include "src/compiler/node-properties.h" | 8 #include "src/compiler/node-properties.h" |
| 9 #include "src/compiler/simplified-operator.h" | 9 #include "src/compiler/simplified-operator.h" |
| 10 | 10 |
| 11 namespace v8 { | 11 namespace v8 { |
| 12 namespace internal { | 12 namespace internal { |
| 13 namespace compiler { | 13 namespace compiler { |
| 14 | 14 |
| 15 BranchElimination::BranchElimination(Editor* editor, JSGraph* js_graph, | 15 BranchElimination::BranchElimination(Editor* editor, JSGraph* js_graph, |
| 16 Zone* zone) | 16 Zone* zone) |
| 17 : AdvancedReducer(editor), | 17 : AdvancedReducer(editor), |
| 18 jsgraph_(js_graph), | |
| 18 node_conditions_(zone, js_graph->graph()->NodeCount()), | 19 node_conditions_(zone, js_graph->graph()->NodeCount()), |
| 19 zone_(zone), | 20 zone_(zone), |
| 20 dead_(js_graph->graph()->NewNode(js_graph->common()->Dead())) {} | 21 dead_(js_graph->graph()->NewNode(js_graph->common()->Dead())) {} |
| 21 | 22 |
| 22 | |
| 23 BranchElimination::~BranchElimination() {} | 23 BranchElimination::~BranchElimination() {} |
| 24 | 24 |
| 25 | 25 |
| 26 Reduction BranchElimination::Reduce(Node* node) { | 26 Reduction BranchElimination::Reduce(Node* node) { |
| 27 switch (node->opcode()) { | 27 switch (node->opcode()) { |
| 28 case IrOpcode::kDead: | 28 case IrOpcode::kDead: |
| 29 return NoChange(); | 29 return NoChange(); |
| 30 case IrOpcode::kDeoptimizeIf: | |
| 31 case IrOpcode::kDeoptimizeUnless: | |
| 32 return ReduceDeoptimizeConditional(node); | |
| 30 case IrOpcode::kMerge: | 33 case IrOpcode::kMerge: |
| 31 return ReduceMerge(node); | 34 return ReduceMerge(node); |
| 32 case IrOpcode::kLoop: | 35 case IrOpcode::kLoop: |
| 33 return ReduceLoop(node); | 36 return ReduceLoop(node); |
| 34 case IrOpcode::kBranch: | 37 case IrOpcode::kBranch: |
| 35 return ReduceBranch(node); | 38 return ReduceBranch(node); |
| 36 case IrOpcode::kIfFalse: | 39 case IrOpcode::kIfFalse: |
| 37 return ReduceIf(node, false); | 40 return ReduceIf(node, false); |
| 38 case IrOpcode::kIfTrue: | 41 case IrOpcode::kIfTrue: |
| 39 return ReduceIf(node, true); | 42 return ReduceIf(node, true); |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 69 default: | 72 default: |
| 70 UNREACHABLE(); | 73 UNREACHABLE(); |
| 71 } | 74 } |
| 72 } | 75 } |
| 73 return Replace(dead()); | 76 return Replace(dead()); |
| 74 } | 77 } |
| 75 } | 78 } |
| 76 return TakeConditionsFromFirstControl(node); | 79 return TakeConditionsFromFirstControl(node); |
| 77 } | 80 } |
| 78 | 81 |
| 82 Reduction BranchElimination::ReduceDeoptimizeConditional(Node* node) { | |
| 83 DCHECK(node->opcode() == IrOpcode::kDeoptimizeIf || | |
| 84 node->opcode() == IrOpcode::kDeoptimizeUnless); | |
| 85 bool condition_is_true = node->opcode() == IrOpcode::kDeoptimizeUnless; | |
| 86 Node* condition = NodeProperties::GetValueInput(node, 0); | |
| 87 Node* frame_state = NodeProperties::GetValueInput(node, 1); | |
| 88 Node* effect = NodeProperties::GetEffectInput(node); | |
| 89 Node* control = NodeProperties::GetControlInput(node); | |
| 90 ControlPathConditions const* conditions = node_conditions_.Get(control); | |
| 91 // If we do not know anything about the predecessor, do not propagate just | |
| 92 // yet because we will have to recompute anyway once we compute the | |
| 93 // predecessor. | |
| 94 if (conditions == nullptr) { | |
| 95 DCHECK_NULL(node_conditions_.Get(node)); | |
| 96 return NoChange(); | |
| 97 } | |
| 98 Maybe<bool> condition_value = conditions->LookupCondition(condition); | |
| 99 if (condition_value.IsJust()) { | |
| 100 // If we know the condition we can discard the branch. | |
| 101 if (condition_is_true == condition_value.FromJust()) { | |
| 102 return Replace(control); | |
|
Jarin
2016/02/24 08:56:38
Perhaps add a comment saying that we do not need t
Benedikt Meurer
2016/02/24 09:04:56
Done.
| |
| 103 } else { | |
| 104 control = graph()->NewNode(common()->Deoptimize(DeoptimizeKind::kEager), | |
| 105 frame_state, effect, control); | |
| 106 // TODO(bmeurer): This should be on the AdvancedReducer somehow. | |
| 107 NodeProperties::MergeControlToEnd(graph(), common(), control); | |
| 108 Revisit(graph()->end()); | |
| 109 return Replace(dead()); | |
| 110 } | |
| 111 } | |
| 112 return UpdateConditions( | |
| 113 node, conditions->AddCondition(zone_, condition, condition_is_true)); | |
| 114 } | |
| 79 | 115 |
| 80 Reduction BranchElimination::ReduceIf(Node* node, bool is_true_branch) { | 116 Reduction BranchElimination::ReduceIf(Node* node, bool is_true_branch) { |
| 81 // Add the condition to the list arriving from the input branch. | 117 // Add the condition to the list arriving from the input branch. |
| 82 Node* branch = NodeProperties::GetControlInput(node, 0); | 118 Node* branch = NodeProperties::GetControlInput(node, 0); |
| 83 const ControlPathConditions* from_branch = node_conditions_.Get(branch); | 119 const ControlPathConditions* from_branch = node_conditions_.Get(branch); |
| 84 // If we do not know anything about the predecessor, do not propagate just | 120 // If we do not know anything about the predecessor, do not propagate just |
| 85 // yet because we will have to recompute anyway once we compute the | 121 // yet because we will have to recompute anyway once we compute the |
| 86 // predecessor. | 122 // predecessor. |
| 87 if (from_branch == nullptr) { | 123 if (from_branch == nullptr) { |
| 88 DCHECK(node_conditions_.Get(node) == nullptr); | 124 DCHECK(node_conditions_.Get(node) == nullptr); |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 257 this_condition->is_true != other_condition->is_true) { | 293 this_condition->is_true != other_condition->is_true) { |
| 258 return false; | 294 return false; |
| 259 } | 295 } |
| 260 this_condition = this_condition->next; | 296 this_condition = this_condition->next; |
| 261 other_condition = other_condition->next; | 297 other_condition = other_condition->next; |
| 262 } | 298 } |
| 263 UNREACHABLE(); | 299 UNREACHABLE(); |
| 264 return false; | 300 return false; |
| 265 } | 301 } |
| 266 | 302 |
| 303 Graph* BranchElimination::graph() const { return jsgraph()->graph(); } | |
| 304 | |
| 305 CommonOperatorBuilder* BranchElimination::common() const { | |
| 306 return jsgraph()->common(); | |
| 307 } | |
| 308 | |
| 267 } // namespace compiler | 309 } // namespace compiler |
| 268 } // namespace internal | 310 } // namespace internal |
| 269 } // namespace v8 | 311 } // namespace v8 |
| OLD | NEW |