llvm/llvm/lib/Analysis/RegionPrinter.cpp

//===- RegionPrinter.cpp - Print regions tree pass ------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// Print out the region tree of a function using dotty/graphviz.
//===----------------------------------------------------------------------===//

#include "llvm/Analysis/RegionPrinter.h"
#include "llvm/Analysis/DOTGraphTraitsPass.h"
#include "llvm/Analysis/RegionInfo.h"
#include "llvm/Analysis/RegionIterator.h"
#include "llvm/InitializePasses.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/raw_ostream.h"
#ifndef NDEBUG
#include "llvm/IR/LegacyPassManager.h"
#endif

usingnamespacellvm;

//===----------------------------------------------------------------------===//
/// onlySimpleRegion - Show only the simple regions in the RegionViewer.
static cl::opt<bool>
onlySimpleRegions("only-simple-regions",
                  cl::desc("Show only simple regions in the graphviz viewer"),
                  cl::Hidden,
                  cl::init(false));

namespace llvm {

std::string DOTGraphTraits<RegionNode *>::getNodeLabel(RegionNode *Node,
                                                       RegionNode *Graph) {}

template <>
struct DOTGraphTraits<RegionInfo *> : public DOTGraphTraits<RegionNode *> {};
} // end namespace llvm

namespace {

struct RegionInfoPassGraphTraits {};

struct RegionPrinter
    : public DOTGraphTraitsPrinterWrapperPass<
          RegionInfoPass, false, RegionInfo *, RegionInfoPassGraphTraits> {};
char RegionPrinter::ID =;

struct RegionOnlyPrinter
    : public DOTGraphTraitsPrinterWrapperPass<
          RegionInfoPass, true, RegionInfo *, RegionInfoPassGraphTraits> {};
char RegionOnlyPrinter::ID =;

struct RegionViewer
    : public DOTGraphTraitsViewerWrapperPass<
          RegionInfoPass, false, RegionInfo *, RegionInfoPassGraphTraits> {};
char RegionViewer::ID =;

struct RegionOnlyViewer
    : public DOTGraphTraitsViewerWrapperPass<RegionInfoPass, true, RegionInfo *,
                                             RegionInfoPassGraphTraits> {};
char RegionOnlyViewer::ID =;

} //end anonymous namespace

INITIALIZE_PASS()

INITIALIZE_PASS()

INITIALIZE_PASS()

INITIALIZE_PASS()

FunctionPass *llvm::createRegionPrinterPass() {}

FunctionPass *llvm::createRegionOnlyPrinterPass() {}

FunctionPass* llvm::createRegionViewerPass() {}

FunctionPass* llvm::createRegionOnlyViewerPass() {}

#ifndef NDEBUG
static void viewRegionInfo(RegionInfo *RI, bool ShortNames) {
  assert(RI && "Argument must be non-null");

  llvm::Function *F = RI->getTopLevelRegion()->getEntry()->getParent();
  std::string GraphName = DOTGraphTraits<RegionInfo *>::getGraphName(RI);

  llvm::ViewGraph(RI, "reg", ShortNames,
                  Twine(GraphName) + " for '" + F->getName() + "' function");
}

static void invokeFunctionPass(const Function *F, FunctionPass *ViewerPass) {
  assert(F && "Argument must be non-null");
  assert(!F->isDeclaration() && "Function must have an implementation");

  // The viewer and analysis passes do not modify anything, so we can safely
  // remove the const qualifier
  auto NonConstF = const_cast<Function *>(F);

  llvm::legacy::FunctionPassManager FPM(NonConstF->getParent());
  FPM.add(ViewerPass);
  FPM.doInitialization();
  FPM.run(*NonConstF);
  FPM.doFinalization();
}

void llvm::viewRegion(RegionInfo *RI) { viewRegionInfo(RI, false); }

void llvm::viewRegion(const Function *F) {
  invokeFunctionPass(F, createRegionViewerPass());
}

void llvm::viewRegionOnly(RegionInfo *RI) { viewRegionInfo(RI, true); }

void llvm::viewRegionOnly(const Function *F) {
  invokeFunctionPass(F, createRegionOnlyViewerPass());
}
#endif