//===- llvm/Analysis/LoopChainPrinter.h -------------------------------*- 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
//
//===----------------------------------------------------------------------===//

//===----------------------------------------------------------------------===//
//
// This file defines the DOT printer for the Data-Dependence Graph (LoopChain).
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_ANALYSIS_LOOPCHAINPRINTER_H
#define LLVM_ANALYSIS_LOOPCHAINPRINTER_H

#include "llvm/Transforms/Scalar/LoopChain.h"
#include "llvm/Support/DOTGraphTraits.h"


void writeLCToDotFile(llvm::LoopChainGraph &G, bool DOnly, int version);

namespace llvm {

//===--------------------------------------------------------------------===//
// Specialization of DOTGraphTraits.
//===--------------------------------------------------------------------===//
template <>
struct DOTGraphTraits<const LoopChainGraph *>
    : public DefaultDOTGraphTraits {

  DOTGraphTraits(bool IsSimple = false) : DefaultDOTGraphTraits(IsSimple) {}

  /// Generate a title for the graph in DOT format
  std::string getGraphName(const LoopChainGraph *G) {
    assert(G && "expected a valid pointer to the graph.");
    return "LCG for '" + std::string(G->getName()) + "'";
  }

  /// Print a LC node either in concise form (-ddg-dot-only) or
  /// verbose mode (-ddg-dot).
  std::string getNodeLabel(const LCNode *Node,
                           const LoopChainGraph *Graph);

  std::string getNodeAttributes(const LCNode *Node,
                                const LoopChainGraph *Graph);

  /// Print attributes of an edge in the LC graph. If the edge
  /// is a MemoryDependence edge, then detailed dependence info
  /// available from DependenceAnalysis is displayed.
  std::string
  getEdgeAttributes(const LCNode *Node,
                    GraphTraits<const LCNode *>::ChildIteratorType I,
                    const LoopChainGraph *G);

  /// Do not print nodes that are part of a pi-block separately. They
  /// will be printed when their containing pi-block is being printed.
  bool isNodeHidden(const LCNode *Node, const LoopChainGraph *G);

private:
  /// Print a LC node in concise form.
  static std::string getSimpleNodeLabel(const LCNode *Node,
                                        const LoopChainGraph *G);

  /// Print a LC node with more information including containing instructions
  /// and detailed information about the dependence edges.
  static std::string getVerboseNodeLabel(const LCNode *Node,
                                         const LoopChainGraph *G);

  /// Print a LC edge in concise form.
  static std::string getSimpleEdgeAttributes(const LCNode *Src,
                                             const LCEdge *Edge,
                                             const LoopChainGraph *G);

  /// Print a LC edge with more information including detailed information
  /// about the dependence edges.
  static std::string getVerboseEdgeAttributes(const LCNode *Src,
                                              const LCEdge *Edge,
                                              const LoopChainGraph *G);
};

using LCDotGraphTraits = DOTGraphTraits<const LoopChainGraph *>;

} // namespace llvm

#endif // LLVM_ANALYSIS_LCPRINTER_H
