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 NodeVector stack_; | |
Benedikt Meurer
2014/10/17 09:46:44
Nit: Use deque here, or even better std::stack wit
titzer
2014/10/17 11:15:50
Done.
| |
38 NodeVector revisit_; | |
Benedikt Meurer
2014/10/17 09:46:44
Nit: Use deque here.
titzer
2014/10/17 11:15:50
Done.
| |
39 Node* dead_; | |
40 | |
41 void Trim() { | |
42 // Mark all nodes reachable from end. | |
43 state_.assign(jsgraph_->graph()->NodeCount(), kUnvisited); | |
44 Push(jsgraph_->graph()->end()); | |
45 while (!stack_.empty()) { | |
46 Node* node = stack_[stack_.size() - 1]; | |
47 stack_.pop_back(); | |
48 state_[node->id()] = kVisited; | |
49 revisit_.push_back(node); | |
50 for (InputIter i = node->inputs().begin(); i != node->inputs().end(); | |
51 ++i) { | |
52 Recurse(*i); // pushes node onto the stack if necessary. | |
53 } | |
54 } | |
55 // Process cached nodes in the JSGraph too. | |
56 jsgraph_->GetCachedNodes(&revisit_); | |
57 // Remove dead->live edges. | |
58 for (size_t j = 0; j < revisit_.size(); j++) { | |
59 Node* node = revisit_[j]; | |
60 for (UseIter i = node->uses().begin(); i != node->uses().end();) { | |
61 size_t id = static_cast<size_t>((*i)->id()); | |
62 if (state_[id] != kVisited) { | |
63 TRACE(("DeadLink: #%d:%s(%d) -> #%d:%s\n", (*i)->id(), | |
64 (*i)->op()->mnemonic(), i.index(), node->id(), | |
65 node->op()->mnemonic())); | |
66 i.UpdateToAndIncrement(NULL); | |
67 } else { | |
68 ++i; | |
69 } | |
70 } | |
71 } | |
72 #if DEBUG | |
73 // Verify that no inputs to live nodes are NULL. | |
74 for (size_t j = 0; j < revisit_.size(); j++) { | |
75 Node* node = revisit_[j]; | |
76 for (InputIter i = node->inputs().begin(); i != node->inputs().end(); | |
77 ++i) { | |
78 CHECK_NE(NULL, *i); | |
79 } | |
80 for (UseIter i = node->uses().begin(); i != node->uses().end(); ++i) { | |
81 size_t id = static_cast<size_t>((*i)->id()); | |
82 CHECK_EQ(kVisited, state_[id]); | |
83 } | |
84 } | |
85 #endif | |
86 revisit_.clear(); | |
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 |