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
14#include "llvm/CodeGen/MachineCFGPrinter.h"
15#include "llvm/CodeGen/MachineBasicBlock.h"
16#include "llvm/CodeGen/MachineFunctionPass.h"
17#include "llvm/CodeGen/MachinePassManager.h"
18#include "llvm/CodeGen/TargetSubtargetInfo.h"
19#include "llvm/InitializePasses.h"
20#include "llvm/Pass.h"
21#include "llvm/PassRegistry.h"
22#include "llvm/Support/GraphWriter.h"
23
24using namespace llvm;
25
26#define DEBUG_TYPE "dot-machine-cfg"
27
28static cl::opt<std::string>
29 MCFGFuncName("mcfg-func-name", cl::Hidden,
30 cl::desc("The name of a function (or its substring)"
31 " whose CFG is viewed/printed."));
32
33static cl::opt<std::string> MCFGDotFilenamePrefix(
34 "mcfg-dot-filename-prefix", cl::Hidden,
35 cl::desc("The prefix used for the Machine CFG dot file names."));
36
37static cl::opt<bool>
38 CFGOnly("dot-mcfg-only", cl::init(Val: false), cl::Hidden,
39 cl::desc("Print only the CFG without blocks body"));
40
41static void writeMCFGToDotFile(MachineFunction &MF) {
42 std::string Filename =
43 (MCFGDotFilenamePrefix + "." + MF.getName() + ".dot").str();
44 errs() << "Writing '" << Filename << "'...";
45
46 std::error_code EC;
47 raw_fd_ostream File(Filename, EC, sys::fs::OF_Text);
48
49 DOTMachineFuncInfo MCFGInfo(&MF);
50
51 if (!EC)
52 WriteGraph(O&: File, G: &MCFGInfo, ShortNames: CFGOnly);
53 else
54 errs() << " error opening file for writing!";
55 errs() << '\n';
56}
57
58namespace {
59
60class MachineCFGPrinterLegacy : public MachineFunctionPass {
61public:
62 static char ID;
63
64 MachineCFGPrinterLegacy();
65
66 bool runOnMachineFunction(MachineFunction &MF) override;
67
68 void getAnalysisUsage(AnalysisUsage &AU) const override {
69 AU.setPreservesCFG();
70 MachineFunctionPass::getAnalysisUsage(AU);
71 }
72};
73
74} // namespace
75
76char MachineCFGPrinterLegacy::ID = 0;
77
78char &llvm::MachineCFGPrinterID = MachineCFGPrinterLegacy::ID;
79
80INITIALIZE_PASS(MachineCFGPrinterLegacy, DEBUG_TYPE, "Machine CFG Printer Pass",
81 false, true)
82
83/// Default construct and initialize the pass.
84MachineCFGPrinterLegacy::MachineCFGPrinterLegacy() : MachineFunctionPass(ID) {}
85
86bool MachineCFGPrinterLegacy::runOnMachineFunction(MachineFunction &MF) {
87 if (!MCFGFuncName.empty() && !MF.getName().contains(Other: MCFGFuncName))
88 return false;
89 errs() << "Writing Machine CFG for function ";
90 errs().write_escaped(Str: MF.getName()) << '\n';
91
92 writeMCFGToDotFile(MF);
93 return false;
94}
95
96PreservedAnalyses
97MachineCFGPrinterPass::run(MachineFunction &MF,
98 MachineFunctionAnalysisManager &MFAM) {
99 if (!MCFGFuncName.empty() && !MF.getName().contains(Other: MCFGFuncName))
100 return PreservedAnalyses::all();
101 errs() << "Writing Machine CFG for function ";
102 errs().write_escaped(Str: MF.getName()) << '\n';
103
104 writeMCFGToDotFile(MF);
105 return PreservedAnalyses::all();
106}
107