llvm/mlir/include/mlir/Analysis/SliceWalk.h

//===- SliceWalk.h - Helpers for performing IR slice walks ---*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef MLIR_ANALYSIS_SLICEWALK_H
#define MLIR_ANALYSIS_SLICEWALK_H

#include "mlir/IR/ValueRange.h"

namespace mlir {

/// A class to signal how to proceed with the walk of the backward slice:
/// - Interrupt: Stops the walk.
/// - AdvanceTo: Continues the walk to user-specified values.
/// - Skip: Continues the walk, but skips the predecessors of the current value.
class WalkContinuation {};

/// A callback that is invoked for each value encountered during the walk of the
/// slice. The callback takes the current value, and returns the walk
/// continuation, which determines if the walk should proceed and if yes, with
/// which values.
WalkCallback;

/// Walks the slice starting from the `rootValues` using a depth-first
/// traversal. The walk calls the provided `walkCallback` for each value
/// encountered in the slice and uses the returned walk continuation to
/// determine how to proceed.
WalkContinuation walkSlice(mlir::ValueRange rootValues,
                           WalkCallback walkCallback);

/// Computes a vector of all control predecessors of `value`. Relies on
/// RegionBranchOpInterface, BranchOpInterface, and SelectLikeOpInterface to
/// determine predecessors. Returns nullopt if `value` has no predecessors or
/// when the relevant operations are missing the interface implementations.
std::optional<SmallVector<Value>> getControlFlowPredecessors(Value value);

} // namespace mlir

#endif // MLIR_ANALYSIS_SLICEWALK_H