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
3// found in the LICENSE file.
4
5#ifndef V8_COMPILER_GRAPH_TRIMMER_H_
6#define V8_COMPILER_GRAPH_TRIMMER_H_
7
8#include "src/common/globals.h"
9#include "src/compiler/node-marker.h"
10
11namespace v8 {
12namespace internal {
13namespace compiler {
14
15// Forward declarations.
16class Graph;
17
18// Trims dead nodes from the node graph.
19class V8_EXPORT_PRIVATE GraphTrimmer final {
20 public:
21  GraphTrimmer(Zone* zone, Graph* graph);
22  ~GraphTrimmer();
23  GraphTrimmer(const GraphTrimmer&) = delete;
24  GraphTrimmer& operator=(const GraphTrimmer&) = delete;
25
26  // Trim nodes in the {graph} that are not reachable from {graph->end()}.
27  void TrimGraph();
28
29  // Trim nodes in the {graph} that are not reachable from either {graph->end()}
30  // or any of the roots in the sequence [{begin},{end}[.
31  template <typename ForwardIterator>
32  void TrimGraph(ForwardIterator begin, ForwardIterator end) {
33    while (begin != end) {
34      Node* const node = *begin++;
35      if (!node->IsDead()) MarkAsLive(node);
36    }
37    TrimGraph();
38  }
39
40 private:
41  V8_INLINE bool IsLive(Node* const node) { return is_live_.Get(node); }
42  V8_INLINE void MarkAsLive(Node* const node) {
43    DCHECK(!node->IsDead());
44    if (!IsLive(node)) {
45      is_live_.Set(node, true);
46      live_.push_back(node);
47    }
48  }
49
50  Graph* graph() const { return graph_; }
51
52  Graph* const graph_;
53  NodeMarker<bool> is_live_;
54  NodeVector live_;
55};
56
57}  // namespace compiler
58}  // namespace internal
59}  // namespace v8
60
61#endif  // V8_COMPILER_GRAPH_TRIMMER_H_
62