1//===- MIRPrintingPass.cpp - Pass that prints out using the MIR format ----===//
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// This file implements a pass that prints out the LLVM module using the MIR
10// serialization format.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/MIRPrinter.h"
15#include "llvm/CodeGen/MachineFunctionAnalysis.h"
16#include "llvm/CodeGen/MachineFunctionPass.h"
17#include "llvm/CodeGen/MachineModuleInfo.h"
18#include "llvm/CodeGen/MachineModuleSlotTracker.h"
19#include "llvm/CodeGen/Passes.h"
20#include "llvm/CodeGen/VirtRegMap.h"
21#include "llvm/IR/Function.h"
22#include "llvm/IR/Module.h"
23
24using namespace llvm;
25
26void PrintMIRPass::printMachineFunction(MachineFunction &MF,
27 FunctionAnalysisManager &FAM,
28 MachineFunctionAnalysisManager &MFAM) {
29 const VirtRegMap *VRM = MFAM.getCachedResult<VirtRegMapAnalysis>(IR&: MF);
30 MachineModuleSlotTracker MST(
31 [&](const Function &F) {
32 return &FAM.getResult<MachineFunctionAnalysis>(
33 IR&: const_cast<Function &>(F))
34 .getMF();
35 },
36 &MF);
37 MST.renumberMetadataForAssembly();
38 printMIR(OS, FAM, MF, VRM);
39}
40
41PreservedAnalyses PrintMIRPass::run(Module &M, ModuleAnalysisManager &MAM) {
42 M.renumberMetadataForAssembly();
43 printMIR(OS, M);
44 auto &FAM = MAM.getResult<FunctionAnalysisManagerModuleProxy>(IR&: M).getManager();
45 for (Function &F : M) {
46 if (F.isDeclaration() || F.hasAvailableExternallyLinkage())
47 continue;
48 MachineFunction &MF = FAM.getResult<MachineFunctionAnalysis>(IR&: F).getMF();
49 auto &MFAM = FAM.getResult<MachineFunctionAnalysisManagerFunctionProxy>(IR&: F)
50 .getManager();
51 printMachineFunction(MF, FAM, MFAM);
52 }
53 return PreservedAnalyses::all();
54}
55
56namespace {
57
58/// This pass prints out the LLVM IR to an output stream using the MIR
59/// serialization format.
60struct MIRPrintingPass : public MachineFunctionPass {
61 static char ID;
62 raw_ostream &OS;
63 std::string MachineFunctions;
64
65 MIRPrintingPass() : MachineFunctionPass(ID), OS(dbgs()) {}
66 MIRPrintingPass(raw_ostream &OS) : MachineFunctionPass(ID), OS(OS) {}
67
68 StringRef getPassName() const override { return "MIR Printing Pass"; }
69
70 void getAnalysisUsage(AnalysisUsage &AU) const override {
71 AU.setPreservesAll();
72 AU.addUsedIfAvailable<VirtRegMapWrapperLegacy>();
73 MachineFunctionPass::getAnalysisUsage(AU);
74 }
75
76 bool runOnMachineFunction(MachineFunction &MF) override {
77 std::string Str;
78 raw_string_ostream StrOS(Str);
79
80 MachineModuleInfo *MMI =
81 &getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
82
83 const VirtRegMap *VRM = nullptr;
84 if (auto *W = getAnalysisIfAvailable<VirtRegMapWrapperLegacy>())
85 VRM = &W->getVRM();
86
87 MachineModuleSlotTracker MST(
88 [&](const Function &F) { return MMI->getMachineFunction(F); }, &MF);
89 MST.renumberMetadataForAssembly();
90 printMIR(OS&: StrOS, MMI: *MMI, MF, VRM);
91 MachineFunctions.append(str: Str);
92 return false;
93 }
94
95 bool doFinalization(Module &M) override {
96 M.renumberMetadataForAssembly();
97 printMIR(OS, M);
98 OS << MachineFunctions;
99 return false;
100 }
101};
102
103char MIRPrintingPass::ID = 0;
104
105} // end anonymous namespace
106
107MachineFunctionPass *llvm::createPrintMIRPass(raw_ostream &OS) {
108 return new MIRPrintingPass(OS);
109}
110