LLVM 24.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"
21
22using namespace llvm;
23
24#define DEBUG_TYPE "dot-machine-cfg"
25
27 MCFGFuncName("mcfg-func-name", cl::Hidden,
28 cl::desc("The name of a function (or its substring)"
29 " whose CFG is viewed/printed."));
30
32 "mcfg-dot-filename-prefix", cl::Hidden,
33 cl::desc("The prefix used for the Machine CFG dot file names."));
34
35static cl::opt<bool>
36 CFGOnly("dot-mcfg-only", cl::init(false), cl::Hidden,
37 cl::desc("Print only the CFG without blocks body"));
38
40 std::string Filename =
41 (MCFGDotFilenamePrefix + "." + MF.getName() + ".dot").str();
42 errs() << "Writing '" << Filename << "'...";
43
44 std::error_code EC;
46
47 DOTMachineFuncInfo MCFGInfo(&MF);
48
49 if (!EC)
50 WriteGraph(File, &MCFGInfo, CFGOnly);
51 else
52 errs() << " error opening file for writing!";
53 errs() << '\n';
54}
55
56namespace {
57
58class MachineCFGPrinterLegacy : public MachineFunctionPass {
59public:
60 static char ID;
61
62 MachineCFGPrinterLegacy();
63
64 bool runOnMachineFunction(MachineFunction &MF) override;
65
66 void getAnalysisUsage(AnalysisUsage &AU) const override {
67 AU.setPreservesCFG();
69 }
70};
71
72} // namespace
73
74char MachineCFGPrinterLegacy::ID = 0;
75
76char &llvm::MachineCFGPrinterID = MachineCFGPrinterLegacy::ID;
77
78INITIALIZE_PASS(MachineCFGPrinterLegacy, DEBUG_TYPE, "Machine CFG Printer Pass",
79 false, true)
80
81/// Default construct and initialize the pass.
82MachineCFGPrinterLegacy::MachineCFGPrinterLegacy() : MachineFunctionPass(ID) {}
83
84bool MachineCFGPrinterLegacy::runOnMachineFunction(MachineFunction &MF) {
85 if (!MCFGFuncName.empty() && !MF.getName().contains(MCFGFuncName))
86 return false;
87 errs() << "Writing Machine CFG for function ";
88 errs().write_escaped(MF.getName()) << '\n';
89
91 return false;
92}
93
94PreservedAnalyses
97 if (!MCFGFuncName.empty() && !MF.getName().contains(MCFGFuncName))
99 errs() << "Writing Machine CFG for function ";
100 errs().write_escaped(MF.getName()) << '\n';
101
103 return PreservedAnalyses::all();
104}
#define DEBUG_TYPE
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."))
static cl::opt< bool > CFGOnly("dot-mcfg-only", cl::init(false), cl::Hidden, cl::desc("Print only the CFG without blocks body"))
static constexpr StringLiteral Filename
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:275
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
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.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
Definition StringRef.h:446
A raw_ostream that writes to a file descriptor.
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
Output Str, turning '\', '\t', ' ', '"', and anything that doesn't satisfy llvm::isPrint into an esca...
initializer< Ty > init(const Ty &Val)
@ OF_Text
The file should be opened in text mode on platforms like z/OS that make this distinction.
Definition FileSystem.h:795
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI char & MachineCFGPrinterID
MachineCFGPrinter pass.
raw_ostream & WriteGraph(raw_ostream &O, const GraphType &G, bool ShortNames=false, const Twine &Title="")
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.