11cb0ef41Sopenharmony_ci// Copyright 2015 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#ifndef V8_COMPILER_GRAPH_TRIMMER_H_
61cb0ef41Sopenharmony_ci#define V8_COMPILER_GRAPH_TRIMMER_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include "src/common/globals.h"
91cb0ef41Sopenharmony_ci#include "src/compiler/node-marker.h"
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_cinamespace v8 {
121cb0ef41Sopenharmony_cinamespace internal {
131cb0ef41Sopenharmony_cinamespace compiler {
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci// Forward declarations.
161cb0ef41Sopenharmony_ciclass Graph;
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci// Trims dead nodes from the node graph.
191cb0ef41Sopenharmony_ciclass V8_EXPORT_PRIVATE GraphTrimmer final {
201cb0ef41Sopenharmony_ci public:
211cb0ef41Sopenharmony_ci  GraphTrimmer(Zone* zone, Graph* graph);
221cb0ef41Sopenharmony_ci  ~GraphTrimmer();
231cb0ef41Sopenharmony_ci  GraphTrimmer(const GraphTrimmer&) = delete;
241cb0ef41Sopenharmony_ci  GraphTrimmer& operator=(const GraphTrimmer&) = delete;
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  // Trim nodes in the {graph} that are not reachable from {graph->end()}.
271cb0ef41Sopenharmony_ci  void TrimGraph();
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  // Trim nodes in the {graph} that are not reachable from either {graph->end()}
301cb0ef41Sopenharmony_ci  // or any of the roots in the sequence [{begin},{end}[.
311cb0ef41Sopenharmony_ci  template <typename ForwardIterator>
321cb0ef41Sopenharmony_ci  void TrimGraph(ForwardIterator begin, ForwardIterator end) {
331cb0ef41Sopenharmony_ci    while (begin != end) {
341cb0ef41Sopenharmony_ci      Node* const node = *begin++;
351cb0ef41Sopenharmony_ci      if (!node->IsDead()) MarkAsLive(node);
361cb0ef41Sopenharmony_ci    }
371cb0ef41Sopenharmony_ci    TrimGraph();
381cb0ef41Sopenharmony_ci  }
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci private:
411cb0ef41Sopenharmony_ci  V8_INLINE bool IsLive(Node* const node) { return is_live_.Get(node); }
421cb0ef41Sopenharmony_ci  V8_INLINE void MarkAsLive(Node* const node) {
431cb0ef41Sopenharmony_ci    DCHECK(!node->IsDead());
441cb0ef41Sopenharmony_ci    if (!IsLive(node)) {
451cb0ef41Sopenharmony_ci      is_live_.Set(node, true);
461cb0ef41Sopenharmony_ci      live_.push_back(node);
471cb0ef41Sopenharmony_ci    }
481cb0ef41Sopenharmony_ci  }
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  Graph* graph() const { return graph_; }
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  Graph* const graph_;
531cb0ef41Sopenharmony_ci  NodeMarker<bool> is_live_;
541cb0ef41Sopenharmony_ci  NodeVector live_;
551cb0ef41Sopenharmony_ci};
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci}  // namespace compiler
581cb0ef41Sopenharmony_ci}  // namespace internal
591cb0ef41Sopenharmony_ci}  // namespace v8
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci#endif  // V8_COMPILER_GRAPH_TRIMMER_H_
62