OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "src/compiler/common-operator.h" |
| 6 #include "src/compiler/control-reducer.h" |
| 7 #include "src/compiler/graph.h" |
| 8 #include "src/compiler/js-graph.h" |
| 9 #include "src/compiler/node-matchers.h" |
| 10 #include "src/compiler/node-properties-inl.h" |
| 11 #include "src/zone-containers.h" |
| 12 |
| 13 namespace v8 { |
| 14 namespace internal { |
| 15 namespace compiler { |
| 16 |
| 17 enum VisitState { kUnvisited, kOnStack, kRevisit, kVisited }; |
| 18 |
| 19 #define TRACE(x) \ |
| 20 if (FLAG_trace_turbo) PrintF x |
| 21 |
| 22 class ControlReducerImpl { |
| 23 public: |
| 24 ControlReducerImpl(JSGraph* jsgraph, CommonOperatorBuilder* common) |
| 25 : zone_(jsgraph->zone()->isolate()), |
| 26 jsgraph_(jsgraph), |
| 27 common_(common), |
| 28 state_(jsgraph->graph()->NodeCount(), kUnvisited, &zone_), |
| 29 stack_(&zone_), |
| 30 revisit_(&zone_), |
| 31 dead_(NULL) {} |
| 32 |
| 33 Zone zone_; |
| 34 JSGraph* jsgraph_; |
| 35 CommonOperatorBuilder* common_; |
| 36 ZoneVector<VisitState> state_; |
| 37 ZoneDeque<Node*> stack_; |
| 38 ZoneDeque<Node*> revisit_; |
| 39 Node* dead_; |
| 40 |
| 41 void Trim() { |
| 42 // Mark all nodes reachable from end. |
| 43 NodeVector nodes(&zone_); |
| 44 state_.assign(jsgraph_->graph()->NodeCount(), kUnvisited); |
| 45 Push(jsgraph_->graph()->end()); |
| 46 while (!stack_.empty()) { |
| 47 Node* node = stack_[stack_.size() - 1]; |
| 48 stack_.pop_back(); |
| 49 state_[node->id()] = kVisited; |
| 50 nodes.push_back(node); |
| 51 for (InputIter i = node->inputs().begin(); i != node->inputs().end(); |
| 52 ++i) { |
| 53 Recurse(*i); // pushes node onto the stack if necessary. |
| 54 } |
| 55 } |
| 56 // Process cached nodes in the JSGraph too. |
| 57 jsgraph_->GetCachedNodes(&nodes); |
| 58 // Remove dead->live edges. |
| 59 for (size_t j = 0; j < nodes.size(); j++) { |
| 60 Node* node = nodes[j]; |
| 61 for (UseIter i = node->uses().begin(); i != node->uses().end();) { |
| 62 size_t id = static_cast<size_t>((*i)->id()); |
| 63 if (state_[id] != kVisited) { |
| 64 TRACE(("DeadLink: #%d:%s(%d) -> #%d:%s\n", (*i)->id(), |
| 65 (*i)->op()->mnemonic(), i.index(), node->id(), |
| 66 node->op()->mnemonic())); |
| 67 i.UpdateToAndIncrement(NULL); |
| 68 } else { |
| 69 ++i; |
| 70 } |
| 71 } |
| 72 } |
| 73 #if DEBUG |
| 74 // Verify that no inputs to live nodes are NULL. |
| 75 for (size_t j = 0; j < nodes.size(); j++) { |
| 76 Node* node = nodes[j]; |
| 77 for (InputIter i = node->inputs().begin(); i != node->inputs().end(); |
| 78 ++i) { |
| 79 CHECK_NE(NULL, *i); |
| 80 } |
| 81 for (UseIter i = node->uses().begin(); i != node->uses().end(); ++i) { |
| 82 size_t id = static_cast<size_t>((*i)->id()); |
| 83 CHECK_EQ(kVisited, state_[id]); |
| 84 } |
| 85 } |
| 86 #endif |
| 87 } |
| 88 |
| 89 // Push a node onto the stack if its state is {kUnvisited} or {kRevisit}. |
| 90 bool Recurse(Node* node) { |
| 91 size_t id = static_cast<size_t>(node->id()); |
| 92 if (id < state_.size()) { |
| 93 if (state_[id] != kRevisit && state_[id] != kUnvisited) return false; |
| 94 } else { |
| 95 state_.resize((3 * id) / 2, kUnvisited); |
| 96 } |
| 97 Push(node); |
| 98 return true; |
| 99 } |
| 100 |
| 101 void Push(Node* node) { |
| 102 state_[node->id()] = kOnStack; |
| 103 stack_.push_back(node); |
| 104 } |
| 105 }; |
| 106 |
| 107 void ControlReducer::ReduceGraph(JSGraph* jsgraph, |
| 108 CommonOperatorBuilder* common) { |
| 109 ControlReducerImpl impl(jsgraph, NULL); |
| 110 // Only trim the graph for now. Control reduction can reduce non-terminating |
| 111 // loops to graphs that are unschedulable at the moment. |
| 112 impl.Trim(); |
| 113 } |
| 114 |
| 115 |
| 116 void ControlReducer::TrimGraph(JSGraph* jsgraph) { |
| 117 ControlReducerImpl impl(jsgraph, NULL); |
| 118 impl.Trim(); |
| 119 } |
| 120 } |
| 121 } |
| 122 } // namespace v8::internal::compiler |
OLD | NEW |