1*d415bd75Srobert //===- MachineCFGPrinter.cpp - DOT Printer for Machine Functions ----------===//
2*d415bd75Srobert //
3*d415bd75Srobert // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*d415bd75Srobert // See https://llvm.org/LICENSE.txt for license information.
5*d415bd75Srobert // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*d415bd75Srobert //
7*d415bd75Srobert //===----------------------------------------------------------------------===//
8*d415bd75Srobert //===----------------------------------------------------------------------===//
9*d415bd75Srobert //
10*d415bd75Srobert // This file defines the `-dot-machine-cfg` analysis pass, which emits
11*d415bd75Srobert // Machine Function in DOT format in file titled `<prefix>.<function-name>.dot.
12*d415bd75Srobert //===----------------------------------------------------------------------===//
13*d415bd75Srobert
14*d415bd75Srobert #include "llvm/CodeGen/MachineCFGPrinter.h"
15*d415bd75Srobert #include "llvm/CodeGen/MachineBasicBlock.h"
16*d415bd75Srobert #include "llvm/CodeGen/MachineFunctionPass.h"
17*d415bd75Srobert #include "llvm/CodeGen/TargetSubtargetInfo.h"
18*d415bd75Srobert #include "llvm/InitializePasses.h"
19*d415bd75Srobert #include "llvm/Pass.h"
20*d415bd75Srobert #include "llvm/PassRegistry.h"
21*d415bd75Srobert #include "llvm/Support/GraphWriter.h"
22*d415bd75Srobert
23*d415bd75Srobert using namespace llvm;
24*d415bd75Srobert
25*d415bd75Srobert #define DEBUG_TYPE "dot-machine-cfg"
26*d415bd75Srobert
27*d415bd75Srobert static cl::opt<std::string>
28*d415bd75Srobert MCFGFuncName("mcfg-func-name", cl::Hidden,
29*d415bd75Srobert cl::desc("The name of a function (or its substring)"
30*d415bd75Srobert " whose CFG is viewed/printed."));
31*d415bd75Srobert
32*d415bd75Srobert static cl::opt<std::string> MCFGDotFilenamePrefix(
33*d415bd75Srobert "mcfg-dot-filename-prefix", cl::Hidden,
34*d415bd75Srobert cl::desc("The prefix used for the Machine CFG dot file names."));
35*d415bd75Srobert
36*d415bd75Srobert static cl::opt<bool>
37*d415bd75Srobert CFGOnly("dot-mcfg-only", cl::init(false), cl::Hidden,
38*d415bd75Srobert cl::desc("Print only the CFG without blocks body"));
39*d415bd75Srobert
writeMCFGToDotFile(MachineFunction & MF)40*d415bd75Srobert static void writeMCFGToDotFile(MachineFunction &MF) {
41*d415bd75Srobert std::string Filename =
42*d415bd75Srobert (MCFGDotFilenamePrefix + "." + MF.getName() + ".dot").str();
43*d415bd75Srobert errs() << "Writing '" << Filename << "'...";
44*d415bd75Srobert
45*d415bd75Srobert std::error_code EC;
46*d415bd75Srobert raw_fd_ostream File(Filename, EC, sys::fs::OF_Text);
47*d415bd75Srobert
48*d415bd75Srobert DOTMachineFuncInfo MCFGInfo(&MF);
49*d415bd75Srobert
50*d415bd75Srobert if (!EC)
51*d415bd75Srobert WriteGraph(File, &MCFGInfo, CFGOnly);
52*d415bd75Srobert else
53*d415bd75Srobert errs() << " error opening file for writing!";
54*d415bd75Srobert errs() << '\n';
55*d415bd75Srobert }
56*d415bd75Srobert
57*d415bd75Srobert namespace {
58*d415bd75Srobert
59*d415bd75Srobert class MachineCFGPrinter : public MachineFunctionPass {
60*d415bd75Srobert public:
61*d415bd75Srobert static char ID;
62*d415bd75Srobert
63*d415bd75Srobert MachineCFGPrinter();
64*d415bd75Srobert
65*d415bd75Srobert bool runOnMachineFunction(MachineFunction &MF) override;
66*d415bd75Srobert
getAnalysisUsage(AnalysisUsage & AU) const67*d415bd75Srobert void getAnalysisUsage(AnalysisUsage &AU) const override {
68*d415bd75Srobert AU.setPreservesCFG();
69*d415bd75Srobert MachineFunctionPass::getAnalysisUsage(AU);
70*d415bd75Srobert }
71*d415bd75Srobert };
72*d415bd75Srobert
73*d415bd75Srobert } // namespace
74*d415bd75Srobert
75*d415bd75Srobert char MachineCFGPrinter::ID = 0;
76*d415bd75Srobert
77*d415bd75Srobert char &llvm::MachineCFGPrinterID = MachineCFGPrinter::ID;
78*d415bd75Srobert
79*d415bd75Srobert INITIALIZE_PASS(MachineCFGPrinter, DEBUG_TYPE, "Machine CFG Printer Pass",
80*d415bd75Srobert false, true)
81*d415bd75Srobert
82*d415bd75Srobert /// Default construct and initialize the pass.
MachineCFGPrinter()83*d415bd75Srobert MachineCFGPrinter::MachineCFGPrinter() : MachineFunctionPass(ID) {
84*d415bd75Srobert initializeMachineCFGPrinterPass(*PassRegistry::getPassRegistry());
85*d415bd75Srobert }
86*d415bd75Srobert
runOnMachineFunction(MachineFunction & MF)87*d415bd75Srobert bool MachineCFGPrinter::runOnMachineFunction(MachineFunction &MF) {
88*d415bd75Srobert if (!MCFGFuncName.empty() && !MF.getName().contains(MCFGFuncName))
89*d415bd75Srobert return false;
90*d415bd75Srobert errs() << "Writing Machine CFG for function ";
91*d415bd75Srobert errs().write_escaped(MF.getName()) << '\n';
92*d415bd75Srobert
93*d415bd75Srobert writeMCFGToDotFile(MF);
94*d415bd75Srobert return false;
95*d415bd75Srobert }
96