11cb0ef41Sopenharmony_ci// Copyright 2014 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#include "src/compiler/all-nodes.h"
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci#include "src/compiler/graph.h"
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_cinamespace v8 {
101cb0ef41Sopenharmony_cinamespace internal {
111cb0ef41Sopenharmony_cinamespace compiler {
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciAllNodes::AllNodes(Zone* local_zone, const Graph* graph, bool only_inputs)
141cb0ef41Sopenharmony_ci    : reachable(local_zone),
151cb0ef41Sopenharmony_ci      is_reachable_(graph->NodeCount(), false, local_zone),
161cb0ef41Sopenharmony_ci      only_inputs_(only_inputs) {
171cb0ef41Sopenharmony_ci  Mark(local_zone, graph->end(), graph);
181cb0ef41Sopenharmony_ci}
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciAllNodes::AllNodes(Zone* local_zone, Node* end, const Graph* graph,
211cb0ef41Sopenharmony_ci                   bool only_inputs)
221cb0ef41Sopenharmony_ci    : reachable(local_zone),
231cb0ef41Sopenharmony_ci      is_reachable_(graph->NodeCount(), false, local_zone),
241cb0ef41Sopenharmony_ci      only_inputs_(only_inputs) {
251cb0ef41Sopenharmony_ci  Mark(local_zone, end, graph);
261cb0ef41Sopenharmony_ci}
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_civoid AllNodes::Mark(Zone* local_zone, Node* end, const Graph* graph) {
291cb0ef41Sopenharmony_ci  DCHECK_LT(end->id(), graph->NodeCount());
301cb0ef41Sopenharmony_ci  is_reachable_[end->id()] = true;
311cb0ef41Sopenharmony_ci  reachable.push_back(end);
321cb0ef41Sopenharmony_ci  // Find all nodes reachable from {end}.
331cb0ef41Sopenharmony_ci  for (size_t i = 0; i < reachable.size(); i++) {
341cb0ef41Sopenharmony_ci    for (Node* const input : reachable[i]->inputs()) {
351cb0ef41Sopenharmony_ci      if (input == nullptr) {
361cb0ef41Sopenharmony_ci        // TODO(titzer): print a warning.
371cb0ef41Sopenharmony_ci        continue;
381cb0ef41Sopenharmony_ci      }
391cb0ef41Sopenharmony_ci      if (!is_reachable_[input->id()]) {
401cb0ef41Sopenharmony_ci        is_reachable_[input->id()] = true;
411cb0ef41Sopenharmony_ci        reachable.push_back(input);
421cb0ef41Sopenharmony_ci      }
431cb0ef41Sopenharmony_ci    }
441cb0ef41Sopenharmony_ci    if (!only_inputs_) {
451cb0ef41Sopenharmony_ci      for (Node* use : reachable[i]->uses()) {
461cb0ef41Sopenharmony_ci        if (use == nullptr || use->id() >= graph->NodeCount()) {
471cb0ef41Sopenharmony_ci          continue;
481cb0ef41Sopenharmony_ci        }
491cb0ef41Sopenharmony_ci        if (!is_reachable_[use->id()]) {
501cb0ef41Sopenharmony_ci          is_reachable_[use->id()] = true;
511cb0ef41Sopenharmony_ci          reachable.push_back(use);
521cb0ef41Sopenharmony_ci        }
531cb0ef41Sopenharmony_ci      }
541cb0ef41Sopenharmony_ci    }
551cb0ef41Sopenharmony_ci  }
561cb0ef41Sopenharmony_ci}
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci}  // namespace compiler
591cb0ef41Sopenharmony_ci}  // namespace internal
601cb0ef41Sopenharmony_ci}  // namespace v8
61