| 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 // We don't to update the conditions here, because we're replacing with |
| 103 // the {control} node that already contains the right information. |
| 104 return Replace(control); |
| 105 } else { |
| 106 control = graph()->NewNode(common()->Deoptimize(DeoptimizeKind::kEager), |
| 107 frame_state, effect, control); |
| 108 // TODO(bmeurer): This should be on the AdvancedReducer somehow. |
| 109 NodeProperties::MergeControlToEnd(graph(), common(), control); |
| 110 Revisit(graph()->end()); |
| 111 return Replace(dead()); |
| 112 } |
| 113 } |
| 114 return UpdateConditions( |
| 115 node, conditions->AddCondition(zone_, condition, condition_is_true)); |
| 116 } |
| 79 | 117 |
| 80 Reduction BranchElimination::ReduceIf(Node* node, bool is_true_branch) { | 118 Reduction BranchElimination::ReduceIf(Node* node, bool is_true_branch) { |
| 81 // Add the condition to the list arriving from the input branch. | 119 // Add the condition to the list arriving from the input branch. |
| 82 Node* branch = NodeProperties::GetControlInput(node, 0); | 120 Node* branch = NodeProperties::GetControlInput(node, 0); |
| 83 const ControlPathConditions* from_branch = node_conditions_.Get(branch); | 121 const ControlPathConditions* from_branch = node_conditions_.Get(branch); |
| 84 // If we do not know anything about the predecessor, do not propagate just | 122 // 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 | 123 // yet because we will have to recompute anyway once we compute the |
| 86 // predecessor. | 124 // predecessor. |
| 87 if (from_branch == nullptr) { | 125 if (from_branch == nullptr) { |
| 88 DCHECK(node_conditions_.Get(node) == nullptr); | 126 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) { | 295 this_condition->is_true != other_condition->is_true) { |
| 258 return false; | 296 return false; |
| 259 } | 297 } |
| 260 this_condition = this_condition->next; | 298 this_condition = this_condition->next; |
| 261 other_condition = other_condition->next; | 299 other_condition = other_condition->next; |
| 262 } | 300 } |
| 263 UNREACHABLE(); | 301 UNREACHABLE(); |
| 264 return false; | 302 return false; |
| 265 } | 303 } |
| 266 | 304 |
| 305 Graph* BranchElimination::graph() const { return jsgraph()->graph(); } |
| 306 |
| 307 CommonOperatorBuilder* BranchElimination::common() const { |
| 308 return jsgraph()->common(); |
| 309 } |
| 310 |
| 267 } // namespace compiler | 311 } // namespace compiler |
| 268 } // namespace internal | 312 } // namespace internal |
| 269 } // namespace v8 | 313 } // namespace v8 |
| OLD | NEW |