1 //===- MachineCFGPrinter.cpp - DOT Printer for Machine Functions ----------===//
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
7 //===----------------------------------------------------------------------===//
8 //===----------------------------------------------------------------------===//
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 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/MachineCFGPrinter.h"
15 #include "llvm/CodeGen/MachineBasicBlock.h"
16 #include "llvm/CodeGen/MachineFunctionPass.h"
17 #include "llvm/CodeGen/TargetSubtargetInfo.h"
18 #include "llvm/InitializePasses.h"
19 #include "llvm/Pass.h"
20 #include "llvm/PassRegistry.h"
21 #include "llvm/Support/GraphWriter.h"
25 #define DEBUG_TYPE "dot-machine-cfg"
27 static cl::opt
<std::string
>
28 MCFGFuncName("mcfg-func-name", cl::Hidden
,
29 cl::desc("The name of a function (or its substring)"
30 " whose CFG is viewed/printed."));
32 static cl::opt
<std::string
> MCFGDotFilenamePrefix(
33 "mcfg-dot-filename-prefix", cl::Hidden
,
34 cl::desc("The prefix used for the Machine CFG dot file names."));
37 CFGOnly("dot-mcfg-only", cl::init(false), cl::Hidden
,
38 cl::desc("Print only the CFG without blocks body"));
40 static void writeMCFGToDotFile(MachineFunction
&MF
) {
41 std::string Filename
=
42 (MCFGDotFilenamePrefix
+ "." + MF
.getName() + ".dot").str();
43 errs() << "Writing '" << Filename
<< "'...";
46 raw_fd_ostream
File(Filename
, EC
, sys::fs::OF_Text
);
48 DOTMachineFuncInfo
MCFGInfo(&MF
);
51 WriteGraph(File
, &MCFGInfo
, CFGOnly
);
53 errs() << " error opening file for writing!";
59 class MachineCFGPrinter
: public MachineFunctionPass
{
65 bool runOnMachineFunction(MachineFunction
&MF
) override
;
67 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
69 MachineFunctionPass::getAnalysisUsage(AU
);
75 char MachineCFGPrinter::ID
= 0;
77 char &llvm::MachineCFGPrinterID
= MachineCFGPrinter::ID
;
79 INITIALIZE_PASS(MachineCFGPrinter
, DEBUG_TYPE
, "Machine CFG Printer Pass",
82 /// Default construct and initialize the pass.
83 MachineCFGPrinter::MachineCFGPrinter() : MachineFunctionPass(ID
) {
84 initializeMachineCFGPrinterPass(*PassRegistry::getPassRegistry());
87 bool MachineCFGPrinter::runOnMachineFunction(MachineFunction
&MF
) {
88 if (!MCFGFuncName
.empty() && !MF
.getName().contains(MCFGFuncName
))
90 errs() << "Writing Machine CFG for function ";
91 errs().write_escaped(MF
.getName()) << '\n';
93 writeMCFGToDotFile(MF
);