LLVM 17.0.0git
MachineCFGPrinter.cpp
Go to the documentation of this file.
1//===- MachineCFGPrinter.cpp - DOT Printer for Machine Functions ----------===//
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//
10// This file defines the `-dot-machine-cfg` analysis pass, which emits
11// Machine Function in DOT format in file titled `<prefix>.<function-name>.dot.
12//===----------------------------------------------------------------------===//
13
19#include "llvm/Pass.h"
20#include "llvm/PassRegistry.h"
22
23using namespace llvm;
24
25#define DEBUG_TYPE "dot-machine-cfg"
26
28 MCFGFuncName("mcfg-func-name", cl::Hidden,
29 cl::desc("The name of a function (or its substring)"
30 " whose CFG is viewed/printed."));
31
33 "mcfg-dot-filename-prefix", cl::Hidden,
34 cl::desc("The prefix used for the Machine CFG dot file names."));
35
36static cl::opt<bool>
37 CFGOnly("dot-mcfg-only", cl::init(false), cl::Hidden,
38 cl::desc("Print only the CFG without blocks body"));
39
41 std::string Filename =
42 (MCFGDotFilenamePrefix + "." + MF.getName() + ".dot").str();
43 errs() << "Writing '" << Filename << "'...";
44
45 std::error_code EC;
46 raw_fd_ostream File(Filename, EC, sys::fs::OF_Text);
47
48 DOTMachineFuncInfo MCFGInfo(&MF);
49
50 if (!EC)
51 WriteGraph(File, &MCFGInfo, CFGOnly);
52 else
53 errs() << " error opening file for writing!";
54 errs() << '\n';
55}
56
57namespace {
58
59class MachineCFGPrinter : public MachineFunctionPass {
60public:
61 static char ID;
62
63 MachineCFGPrinter();
64
65 bool runOnMachineFunction(MachineFunction &MF) override;
66
67 void getAnalysisUsage(AnalysisUsage &AU) const override {
68 AU.setPreservesCFG();
70 }
71};
72
73} // namespace
74
75char MachineCFGPrinter::ID = 0;
76
77char &llvm::MachineCFGPrinterID = MachineCFGPrinter::ID;
78
79INITIALIZE_PASS(MachineCFGPrinter, DEBUG_TYPE, "Machine CFG Printer Pass",
80 false, true)
81
82/// Default construct and initialize the pass.
83MachineCFGPrinter::MachineCFGPrinter() : MachineFunctionPass(ID) {
85}
86
87bool MachineCFGPrinter::runOnMachineFunction(MachineFunction &MF) {
88 if (!MCFGFuncName.empty() && !MF.getName().contains(MCFGFuncName))
89 return false;
90 errs() << "Writing Machine CFG for function ";
91 errs().write_escaped(MF.getName()) << '\n';
92
94 return false;
95}
static cl::opt< std::string > MCFGDotFilenamePrefix("mcfg-dot-filename-prefix", cl::Hidden, cl::desc("The prefix used for the Machine CFG dot file names."))
static void writeMCFGToDotFile(MachineFunction &MF)
static cl::opt< std::string > MCFGFuncName("mcfg-func-name", cl::Hidden, cl::desc("The name of a function (or its substring)" " whose CFG is viewed/printed."))
#define DEBUG_TYPE
static cl::opt< bool > CFGOnly("dot-mcfg-only", cl::init(false), cl::Hidden, cl::desc("Print only the CFG without blocks body"))
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
Represent the analysis usage information of a pass.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:265
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
Definition: StringRef.h:423
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:454
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
Output Str, turning '\', '\t', ' ', '"', and anything that doesn't satisfy llvm::isPrint into an esca...
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:445
@ OF_Text
The file should be opened in text mode on platforms like z/OS that make this distinction.
Definition: FileSystem.h:761
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
char & MachineCFGPrinterID
MachineCFGPrinter pass.
raw_ostream & WriteGraph(raw_ostream &O, const GraphType &G, bool ShortNames=false, const Twine &Title="")
Definition: GraphWriter.h:359
void initializeMachineCFGPrinterPass(PassRegistry &)
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.