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