graph-trimmer.h 1.53 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_COMPILER_GRAPH_TRIMMER_H_
#define V8_COMPILER_GRAPH_TRIMMER_H_

8
#include "src/common/globals.h"
9 10 11 12 13 14 15 16 17 18
#include "src/compiler/node-marker.h"

namespace v8 {
namespace internal {
namespace compiler {

// Forward declarations.
class Graph;

// Trims dead nodes from the node graph.
19
class V8_EXPORT_PRIVATE GraphTrimmer final {
20 21 22 23 24 25 26 27 28 29 30
 public:
  GraphTrimmer(Zone* zone, Graph* graph);
  ~GraphTrimmer();

  // Trim nodes in the {graph} that are not reachable from {graph->end()}.
  void TrimGraph();

  // Trim nodes in the {graph} that are not reachable from either {graph->end()}
  // or any of the roots in the sequence [{begin},{end}[.
  template <typename ForwardIterator>
  void TrimGraph(ForwardIterator begin, ForwardIterator end) {
31 32 33 34
    while (begin != end) {
      Node* const node = *begin++;
      if (!node->IsDead()) MarkAsLive(node);
    }
35 36 37 38 39 40
    TrimGraph();
  }

 private:
  V8_INLINE bool IsLive(Node* const node) { return is_live_.Get(node); }
  V8_INLINE void MarkAsLive(Node* const node) {
41 42
    DCHECK(!node->IsDead());
    if (!IsLive(node)) {
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
      is_live_.Set(node, true);
      live_.push_back(node);
    }
  }

  Graph* graph() const { return graph_; }

  Graph* const graph_;
  NodeMarker<bool> is_live_;
  NodeVector live_;

  DISALLOW_COPY_AND_ASSIGN(GraphTrimmer);
};

}  // namespace compiler
}  // namespace internal
}  // namespace v8

#endif  // V8_COMPILER_GRAPH_TRIMMER_H_