1 //===-- DomPrinter.h - Dom printer external interface ------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines external functions that can be called to explicitly
10 // instantiate the dominance tree printer.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_ANALYSIS_DOMPRINTER_H
15 #define LLVM_ANALYSIS_DOMPRINTER_H
16 
17 #include "llvm/Analysis/DOTGraphTraitsPass.h"
18 #include "llvm/Analysis/PostDominators.h"
19 #include "llvm/IR/Dominators.h"
20 #include "llvm/IR/PassManager.h"
21 
22 namespace llvm {
23 
24 template <>
25 struct DOTGraphTraits<DomTreeNode *> : public DefaultDOTGraphTraits {
26 
27   DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
28 
29   std::string getNodeLabel(DomTreeNode *Node, DomTreeNode *Graph) {
30 
31     BasicBlock *BB = Node->getBlock();
32 
33     if (!BB)
34       return "Post dominance root node";
35 
36     if (isSimple())
37       return DOTGraphTraits<DOTFuncInfo *>::getSimpleNodeLabel(BB, nullptr);
38 
39     return DOTGraphTraits<DOTFuncInfo *>::getCompleteNodeLabel(BB, nullptr);
40   }
41 };
42 
43 template <>
44 struct DOTGraphTraits<DominatorTree *>
45     : public DOTGraphTraits<DomTreeNode *> {
46 
47   DOTGraphTraits(bool isSimple = false)
48       : DOTGraphTraits<DomTreeNode *>(isSimple) {}
49 
50   static std::string getGraphName(DominatorTree *DT) {
51     return "Dominator tree";
52   }
53 
54   std::string getNodeLabel(DomTreeNode *Node, DominatorTree *G) {
55     return DOTGraphTraits<DomTreeNode *>::getNodeLabel(Node,
56                                                              G->getRootNode());
57   }
58 };
59 
60 template<>
61 struct DOTGraphTraits<PostDominatorTree *>
62   : public DOTGraphTraits<DomTreeNode*> {
63 
64   DOTGraphTraits (bool isSimple=false)
65     : DOTGraphTraits<DomTreeNode*>(isSimple) {}
66 
67   static std::string getGraphName(PostDominatorTree *DT) {
68     return "Post dominator tree";
69   }
70 
71   std::string getNodeLabel(DomTreeNode *Node,
72                            PostDominatorTree *G) {
73     return DOTGraphTraits<DomTreeNode*>::getNodeLabel(Node, G->getRootNode());
74   }
75 };
76 
77 struct DomViewer final : DOTGraphTraitsViewer<DominatorTreeAnalysis, false> {
78   DomViewer() : DOTGraphTraitsViewer<DominatorTreeAnalysis, false>("dom") {}
79 };
80 
81 struct DomOnlyViewer final : DOTGraphTraitsViewer<DominatorTreeAnalysis, true> {
82   DomOnlyViewer()
83       : DOTGraphTraitsViewer<DominatorTreeAnalysis, true>("domonly") {}
84 };
85 
86 struct PostDomViewer final
87     : DOTGraphTraitsViewer<PostDominatorTreeAnalysis, false> {
88   PostDomViewer()
89       : DOTGraphTraitsViewer<PostDominatorTreeAnalysis, false>("postdom") {}
90 };
91 
92 struct PostDomOnlyViewer final
93     : DOTGraphTraitsViewer<PostDominatorTreeAnalysis, true> {
94   PostDomOnlyViewer()
95       : DOTGraphTraitsViewer<PostDominatorTreeAnalysis, true>("postdomonly") {}
96 };
97 
98 struct DomPrinter final : DOTGraphTraitsPrinter<DominatorTreeAnalysis, false> {
99   DomPrinter() : DOTGraphTraitsPrinter<DominatorTreeAnalysis, false>("dom") {}
100 };
101 
102 struct DomOnlyPrinter final
103     : DOTGraphTraitsPrinter<DominatorTreeAnalysis, true> {
104   DomOnlyPrinter()
105       : DOTGraphTraitsPrinter<DominatorTreeAnalysis, true>("domonly") {}
106 };
107 
108 struct PostDomPrinter final
109     : DOTGraphTraitsPrinter<PostDominatorTreeAnalysis, false> {
110   PostDomPrinter()
111       : DOTGraphTraitsPrinter<PostDominatorTreeAnalysis, false>("postdom") {}
112 };
113 
114 struct PostDomOnlyPrinter final
115     : DOTGraphTraitsPrinter<PostDominatorTreeAnalysis, true> {
116   PostDomOnlyPrinter()
117       : DOTGraphTraitsPrinter<PostDominatorTreeAnalysis, true>("postdomonly") {}
118 };
119 } // namespace llvm
120 
121 namespace llvm {
122   class FunctionPass;
123   FunctionPass *createDomPrinterWrapperPassPass();
124   FunctionPass *createDomOnlyPrinterWrapperPassPass();
125   FunctionPass *createDomViewerWrapperPassPass();
126   FunctionPass *createDomOnlyViewerWrapperPassPass();
127   FunctionPass *createPostDomPrinterWrapperPassPass();
128   FunctionPass *createPostDomOnlyPrinterWrapperPassPass();
129   FunctionPass *createPostDomViewerWrapperPassPass();
130   FunctionPass *createPostDomOnlyViewerWrapperPassPass();
131 } // End llvm namespace
132 
133 #endif
134