Path: blob/main/contrib/llvm-project/llvm/lib/Analysis/DomPrinter.cpp
35234 views
//===- DomPrinter.cpp - DOT printer for the dominance trees ------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file defines '-dot-dom' and '-dot-postdom' analysis passes, which emit9// a dom.<fnname>.dot or postdom.<fnname>.dot file for each function in the10// program, with a graph of the dominance/postdominance tree of that11// function.12//13// There are also passes available to directly call dotty ('-view-dom' or14// '-view-postdom'). By appending '-only' like '-dot-dom-only' only the15// names of the bbs are printed, but the content is hidden.16//17//===----------------------------------------------------------------------===//1819#include "llvm/Analysis/DomPrinter.h"20#include "llvm/Analysis/DOTGraphTraitsPass.h"21#include "llvm/Analysis/PostDominators.h"22#include "llvm/InitializePasses.h"2324using namespace llvm;252627void DominatorTree::viewGraph(const Twine &Name, const Twine &Title) {28#ifndef NDEBUG29ViewGraph(this, Name, false, Title);30#else31errs() << "DomTree dump not available, build with DEBUG\n";32#endif // NDEBUG33}3435void DominatorTree::viewGraph() {36#ifndef NDEBUG37this->viewGraph("domtree", "Dominator Tree for function");38#else39errs() << "DomTree dump not available, build with DEBUG\n";40#endif // NDEBUG41}4243namespace {44struct LegacyDominatorTreeWrapperPassAnalysisGraphTraits {45static DominatorTree *getGraph(DominatorTreeWrapperPass *DTWP) {46return &DTWP->getDomTree();47}48};4950struct DomViewerWrapperPass51: public DOTGraphTraitsViewerWrapperPass<52DominatorTreeWrapperPass, false, DominatorTree *,53LegacyDominatorTreeWrapperPassAnalysisGraphTraits> {54static char ID;55DomViewerWrapperPass()56: DOTGraphTraitsViewerWrapperPass<57DominatorTreeWrapperPass, false, DominatorTree *,58LegacyDominatorTreeWrapperPassAnalysisGraphTraits>("dom", ID) {59initializeDomViewerWrapperPassPass(*PassRegistry::getPassRegistry());60}61};6263struct DomOnlyViewerWrapperPass64: public DOTGraphTraitsViewerWrapperPass<65DominatorTreeWrapperPass, true, DominatorTree *,66LegacyDominatorTreeWrapperPassAnalysisGraphTraits> {67static char ID;68DomOnlyViewerWrapperPass()69: DOTGraphTraitsViewerWrapperPass<70DominatorTreeWrapperPass, true, DominatorTree *,71LegacyDominatorTreeWrapperPassAnalysisGraphTraits>("domonly", ID) {72initializeDomOnlyViewerWrapperPassPass(*PassRegistry::getPassRegistry());73}74};7576struct LegacyPostDominatorTreeWrapperPassAnalysisGraphTraits {77static PostDominatorTree *getGraph(PostDominatorTreeWrapperPass *PDTWP) {78return &PDTWP->getPostDomTree();79}80};8182struct PostDomViewerWrapperPass83: public DOTGraphTraitsViewerWrapperPass<84PostDominatorTreeWrapperPass, false, PostDominatorTree *,85LegacyPostDominatorTreeWrapperPassAnalysisGraphTraits> {86static char ID;87PostDomViewerWrapperPass()88: DOTGraphTraitsViewerWrapperPass<89PostDominatorTreeWrapperPass, false, PostDominatorTree *,90LegacyPostDominatorTreeWrapperPassAnalysisGraphTraits>("postdom",91ID) {92initializePostDomViewerWrapperPassPass(*PassRegistry::getPassRegistry());93}94};9596struct PostDomOnlyViewerWrapperPass97: public DOTGraphTraitsViewerWrapperPass<98PostDominatorTreeWrapperPass, true, PostDominatorTree *,99LegacyPostDominatorTreeWrapperPassAnalysisGraphTraits> {100static char ID;101PostDomOnlyViewerWrapperPass()102: DOTGraphTraitsViewerWrapperPass<103PostDominatorTreeWrapperPass, true, PostDominatorTree *,104LegacyPostDominatorTreeWrapperPassAnalysisGraphTraits>(105"postdomonly", ID) {106initializePostDomOnlyViewerWrapperPassPass(107*PassRegistry::getPassRegistry());108}109};110} // end anonymous namespace111112char DomViewerWrapperPass::ID = 0;113INITIALIZE_PASS(DomViewerWrapperPass, "view-dom",114"View dominance tree of function", false, false)115116char DomOnlyViewerWrapperPass::ID = 0;117INITIALIZE_PASS(DomOnlyViewerWrapperPass, "view-dom-only",118"View dominance tree of function (with no function bodies)",119false, false)120121char PostDomViewerWrapperPass::ID = 0;122INITIALIZE_PASS(PostDomViewerWrapperPass, "view-postdom",123"View postdominance tree of function", false, false)124125char PostDomOnlyViewerWrapperPass::ID = 0;126INITIALIZE_PASS(PostDomOnlyViewerWrapperPass, "view-postdom-only",127"View postdominance tree of function "128"(with no function bodies)",129false, false)130131namespace {132struct DomPrinterWrapperPass133: public DOTGraphTraitsPrinterWrapperPass<134DominatorTreeWrapperPass, false, DominatorTree *,135LegacyDominatorTreeWrapperPassAnalysisGraphTraits> {136static char ID;137DomPrinterWrapperPass()138: DOTGraphTraitsPrinterWrapperPass<139DominatorTreeWrapperPass, false, DominatorTree *,140LegacyDominatorTreeWrapperPassAnalysisGraphTraits>("dom", ID) {141initializeDomPrinterWrapperPassPass(*PassRegistry::getPassRegistry());142}143};144145struct DomOnlyPrinterWrapperPass146: public DOTGraphTraitsPrinterWrapperPass<147DominatorTreeWrapperPass, true, DominatorTree *,148LegacyDominatorTreeWrapperPassAnalysisGraphTraits> {149static char ID;150DomOnlyPrinterWrapperPass()151: DOTGraphTraitsPrinterWrapperPass<152DominatorTreeWrapperPass, true, DominatorTree *,153LegacyDominatorTreeWrapperPassAnalysisGraphTraits>("domonly", ID) {154initializeDomOnlyPrinterWrapperPassPass(*PassRegistry::getPassRegistry());155}156};157158struct PostDomPrinterWrapperPass159: public DOTGraphTraitsPrinterWrapperPass<160PostDominatorTreeWrapperPass, false, PostDominatorTree *,161LegacyPostDominatorTreeWrapperPassAnalysisGraphTraits> {162static char ID;163PostDomPrinterWrapperPass()164: DOTGraphTraitsPrinterWrapperPass<165PostDominatorTreeWrapperPass, false, PostDominatorTree *,166LegacyPostDominatorTreeWrapperPassAnalysisGraphTraits>("postdom",167ID) {168initializePostDomPrinterWrapperPassPass(*PassRegistry::getPassRegistry());169}170};171172struct PostDomOnlyPrinterWrapperPass173: public DOTGraphTraitsPrinterWrapperPass<174PostDominatorTreeWrapperPass, true, PostDominatorTree *,175LegacyPostDominatorTreeWrapperPassAnalysisGraphTraits> {176static char ID;177PostDomOnlyPrinterWrapperPass()178: DOTGraphTraitsPrinterWrapperPass<179PostDominatorTreeWrapperPass, true, PostDominatorTree *,180LegacyPostDominatorTreeWrapperPassAnalysisGraphTraits>(181"postdomonly", ID) {182initializePostDomOnlyPrinterWrapperPassPass(183*PassRegistry::getPassRegistry());184}185};186} // end anonymous namespace187188char DomPrinterWrapperPass::ID = 0;189INITIALIZE_PASS(DomPrinterWrapperPass, "dot-dom",190"Print dominance tree of function to 'dot' file", false, false)191192char DomOnlyPrinterWrapperPass::ID = 0;193INITIALIZE_PASS(DomOnlyPrinterWrapperPass, "dot-dom-only",194"Print dominance tree of function to 'dot' file "195"(with no function bodies)",196false, false)197198char PostDomPrinterWrapperPass::ID = 0;199INITIALIZE_PASS(PostDomPrinterWrapperPass, "dot-postdom",200"Print postdominance tree of function to 'dot' file", false,201false)202203char PostDomOnlyPrinterWrapperPass::ID = 0;204INITIALIZE_PASS(PostDomOnlyPrinterWrapperPass, "dot-postdom-only",205"Print postdominance tree of function to 'dot' file "206"(with no function bodies)",207false, false)208209// Create methods available outside of this file, to use them210// "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by211// the link time optimization.212213FunctionPass *llvm::createDomPrinterWrapperPassPass() {214return new DomPrinterWrapperPass();215}216217FunctionPass *llvm::createDomOnlyPrinterWrapperPassPass() {218return new DomOnlyPrinterWrapperPass();219}220221FunctionPass *llvm::createDomViewerWrapperPassPass() {222return new DomViewerWrapperPass();223}224225FunctionPass *llvm::createDomOnlyViewerWrapperPassPass() {226return new DomOnlyViewerWrapperPass();227}228229FunctionPass *llvm::createPostDomPrinterWrapperPassPass() {230return new PostDomPrinterWrapperPass();231}232233FunctionPass *llvm::createPostDomOnlyPrinterWrapperPassPass() {234return new PostDomOnlyPrinterWrapperPass();235}236237FunctionPass *llvm::createPostDomViewerWrapperPassPass() {238return new PostDomViewerWrapperPass();239}240241FunctionPass *llvm::createPostDomOnlyViewerWrapperPassPass() {242return new PostDomOnlyViewerWrapperPass();243}244245246