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