[ARM] MVE sext costs
[llvm-complete.git] / lib / Analysis / CFGPrinter.cpp
blob4f4103fefa25322001436fc62b8d4b19051184d7
1 //===- CFGPrinter.cpp - DOT printer for the control flow graph ------------===//
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 a `-dot-cfg` analysis pass, which emits the
10 // `<prefix>.<fnname>.dot` file for each function in the program, with a graph
11 // of the CFG for that function. The default value for `<prefix>` is `cfg` but
12 // can be customized as needed.
14 // The other main feature of this file is that it implements the
15 // Function::viewCFG method, which is useful for debugging passes which operate
16 // on the CFG.
18 //===----------------------------------------------------------------------===//
20 #include "llvm/Analysis/CFGPrinter.h"
21 #include "llvm/Pass.h"
22 #include "llvm/Support/FileSystem.h"
23 using namespace llvm;
25 static cl::opt<std::string> CFGFuncName(
26 "cfg-func-name", cl::Hidden,
27 cl::desc("The name of a function (or its substring)"
28 " whose CFG is viewed/printed."));
30 static cl::opt<std::string> CFGDotFilenamePrefix(
31 "cfg-dot-filename-prefix", cl::Hidden,
32 cl::desc("The prefix used for the CFG dot file names."));
34 namespace {
35 struct CFGViewerLegacyPass : public FunctionPass {
36 static char ID; // Pass identifcation, replacement for typeid
37 CFGViewerLegacyPass() : FunctionPass(ID) {
38 initializeCFGViewerLegacyPassPass(*PassRegistry::getPassRegistry());
41 bool runOnFunction(Function &F) override {
42 F.viewCFG();
43 return false;
46 void print(raw_ostream &OS, const Module* = nullptr) const override {}
48 void getAnalysisUsage(AnalysisUsage &AU) const override {
49 AU.setPreservesAll();
54 char CFGViewerLegacyPass::ID = 0;
55 INITIALIZE_PASS(CFGViewerLegacyPass, "view-cfg", "View CFG of function", false, true)
57 PreservedAnalyses CFGViewerPass::run(Function &F,
58 FunctionAnalysisManager &AM) {
59 F.viewCFG();
60 return PreservedAnalyses::all();
64 namespace {
65 struct CFGOnlyViewerLegacyPass : public FunctionPass {
66 static char ID; // Pass identifcation, replacement for typeid
67 CFGOnlyViewerLegacyPass() : FunctionPass(ID) {
68 initializeCFGOnlyViewerLegacyPassPass(*PassRegistry::getPassRegistry());
71 bool runOnFunction(Function &F) override {
72 F.viewCFGOnly();
73 return false;
76 void print(raw_ostream &OS, const Module* = nullptr) const override {}
78 void getAnalysisUsage(AnalysisUsage &AU) const override {
79 AU.setPreservesAll();
84 char CFGOnlyViewerLegacyPass::ID = 0;
85 INITIALIZE_PASS(CFGOnlyViewerLegacyPass, "view-cfg-only",
86 "View CFG of function (with no function bodies)", false, true)
88 PreservedAnalyses CFGOnlyViewerPass::run(Function &F,
89 FunctionAnalysisManager &AM) {
90 F.viewCFGOnly();
91 return PreservedAnalyses::all();
94 static void writeCFGToDotFile(Function &F, bool CFGOnly = false) {
95 if (!CFGFuncName.empty() && !F.getName().contains(CFGFuncName))
96 return;
97 std::string Filename =
98 (CFGDotFilenamePrefix + "." + F.getName() + ".dot").str();
99 errs() << "Writing '" << Filename << "'...";
101 std::error_code EC;
102 raw_fd_ostream File(Filename, EC, sys::fs::OF_Text);
104 if (!EC)
105 WriteGraph(File, (const Function*)&F, CFGOnly);
106 else
107 errs() << " error opening file for writing!";
108 errs() << "\n";
111 namespace {
112 struct CFGPrinterLegacyPass : public FunctionPass {
113 static char ID; // Pass identification, replacement for typeid
114 CFGPrinterLegacyPass() : FunctionPass(ID) {
115 initializeCFGPrinterLegacyPassPass(*PassRegistry::getPassRegistry());
118 bool runOnFunction(Function &F) override {
119 writeCFGToDotFile(F);
120 return false;
123 void print(raw_ostream &OS, const Module* = nullptr) const override {}
125 void getAnalysisUsage(AnalysisUsage &AU) const override {
126 AU.setPreservesAll();
131 char CFGPrinterLegacyPass::ID = 0;
132 INITIALIZE_PASS(CFGPrinterLegacyPass, "dot-cfg", "Print CFG of function to 'dot' file",
133 false, true)
135 PreservedAnalyses CFGPrinterPass::run(Function &F,
136 FunctionAnalysisManager &AM) {
137 writeCFGToDotFile(F);
138 return PreservedAnalyses::all();
141 namespace {
142 struct CFGOnlyPrinterLegacyPass : public FunctionPass {
143 static char ID; // Pass identification, replacement for typeid
144 CFGOnlyPrinterLegacyPass() : FunctionPass(ID) {
145 initializeCFGOnlyPrinterLegacyPassPass(*PassRegistry::getPassRegistry());
148 bool runOnFunction(Function &F) override {
149 writeCFGToDotFile(F, /*CFGOnly=*/true);
150 return false;
152 void print(raw_ostream &OS, const Module* = nullptr) const override {}
154 void getAnalysisUsage(AnalysisUsage &AU) const override {
155 AU.setPreservesAll();
160 char CFGOnlyPrinterLegacyPass::ID = 0;
161 INITIALIZE_PASS(CFGOnlyPrinterLegacyPass, "dot-cfg-only",
162 "Print CFG of function to 'dot' file (with no function bodies)",
163 false, true)
165 PreservedAnalyses CFGOnlyPrinterPass::run(Function &F,
166 FunctionAnalysisManager &AM) {
167 writeCFGToDotFile(F, /*CFGOnly=*/true);
168 return PreservedAnalyses::all();
171 /// viewCFG - This function is meant for use from the debugger. You can just
172 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
173 /// program, displaying the CFG of the current function. This depends on there
174 /// being a 'dot' and 'gv' program in your path.
176 void Function::viewCFG() const {
177 if (!CFGFuncName.empty() && !getName().contains(CFGFuncName))
178 return;
179 ViewGraph(this, "cfg" + getName());
182 /// viewCFGOnly - This function is meant for use from the debugger. It works
183 /// just like viewCFG, but it does not include the contents of basic blocks
184 /// into the nodes, just the label. If you are only interested in the CFG
185 /// this can make the graph smaller.
187 void Function::viewCFGOnly() const {
188 if (!CFGFuncName.empty() && !getName().contains(CFGFuncName))
189 return;
190 ViewGraph(this, "cfg" + getName(), true);
193 FunctionPass *llvm::createCFGPrinterLegacyPassPass () {
194 return new CFGPrinterLegacyPass();
197 FunctionPass *llvm::createCFGOnlyPrinterLegacyPassPass () {
198 return new CFGOnlyPrinterLegacyPass();