V8 Project
graph-reducer.cc
Go to the documentation of this file.
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 
6 
7 #include <functional>
8 
10 
11 namespace v8 {
12 namespace internal {
13 namespace compiler {
14 
15 GraphReducer::GraphReducer(Graph* graph)
16  : graph_(graph), reducers_(graph->zone()) {}
17 
18 
19 static bool NodeIdIsLessThan(const Node* node, NodeId id) {
20  return node->id() < id;
21 }
22 
23 
24 void GraphReducer::ReduceNode(Node* node) {
25  static const unsigned kMaxAttempts = 16;
26  bool reduce = true;
27  for (unsigned attempts = 0; attempts <= kMaxAttempts; ++attempts) {
28  if (!reduce) return;
29  reduce = false; // Assume we don't need to rerun any reducers.
30  int before = graph_->NodeCount();
31  for (ZoneVector<Reducer*>::iterator i = reducers_.begin();
32  i != reducers_.end(); ++i) {
33  Reduction reduction = (*i)->Reduce(node);
34  Node* replacement = reduction.replacement();
35  if (replacement == NULL) {
36  // No change from this reducer.
37  } else if (replacement == node) {
38  // {replacement == node} represents an in-place reduction.
39  // Rerun all the reducers for this node, as now there may be more
40  // opportunities for reduction.
41  reduce = true;
42  break;
43  } else {
44  if (node == graph_->start()) graph_->SetStart(replacement);
45  if (node == graph_->end()) graph_->SetEnd(replacement);
46  // If {node} was replaced by an old node, unlink {node} and assume that
47  // {replacement} was already reduced and finish.
48  if (replacement->id() < before) {
49  node->ReplaceUses(replacement);
50  node->Kill();
51  return;
52  }
53  // Otherwise, {node} was replaced by a new node. Replace all old uses of
54  // {node} with {replacement}. New nodes created by this reduction can
55  // use {node}.
56  node->ReplaceUsesIf(
57  std::bind2nd(std::ptr_fun(&NodeIdIsLessThan), before), replacement);
58  // Unlink {node} if it's no longer used.
59  if (node->uses().empty()) {
60  node->Kill();
61  }
62  // Rerun all the reductions on the {replacement}.
63  node = replacement;
64  reduce = true;
65  break;
66  }
67  }
68  }
69 }
70 
71 
72 // A helper class to reuse the node traversal algorithm.
73 struct GraphReducerVisitor FINAL : public NullNodeVisitor {
74  explicit GraphReducerVisitor(GraphReducer* reducer) : reducer_(reducer) {}
76  reducer_->ReduceNode(node);
77  return GenericGraphVisit::CONTINUE;
78  }
79  GraphReducer* reducer_;
80 };
81 
82 
83 void GraphReducer::ReduceGraph() {
84  GraphReducerVisitor visitor(this);
85  // Perform a post-order reduction of all nodes starting from the end.
86  graph()->VisitNodeInputsFromEnd(&visitor);
87 }
88 
89 
90 // TODO(titzer): partial graph reductions.
91 
92 } // namespace compiler
93 } // namespace internal
94 } // namespace v8
GraphReducerVisitor(GraphReducer *reducer)
GenericGraphVisit::Control Post(Node *node)
#define FINAL
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be NULL
GenericGraphVisit::NullNodeVisitor< NodeData, Node > NullNodeVisitor
Definition: node.h:66
static bool NodeIdIsLessThan(const Node *node, NodeId id)
Debugger support for the V8 JavaScript engine.
Definition: accessors.cc:20