LLVM 24.0.0git
MIRPrintingPass.cpp
Go to the documentation of this file.
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
19#include "llvm/CodeGen/Passes.h"
21#include "llvm/IR/Function.h"
22#include "llvm/IR/Module.h"
24
25using namespace llvm;
26
28 M.renumberMetadataForAssembly();
29 printMIR(OS, M);
31}
32
36 .getManager();
37
38 const VirtRegMap *VRM = MFAM.getCachedResult<VirtRegMapAnalysis>(MF);
40 [&](const Function &F) {
41 return &FAM.getResult<MachineFunctionAnalysis>(
42 const_cast<Function &>(F))
43 .getMF();
44 },
45 &MF);
47 printMIR(OS, FAM, MF, VRM);
49}
50
51namespace {
52
53/// This pass prints out the LLVM IR to an output stream using the MIR
54/// serialization format.
55struct MIRPrintingPass : public MachineFunctionPass {
56 static char ID;
57 raw_ostream &OS;
58 std::string MachineFunctions;
59
60 MIRPrintingPass() : MachineFunctionPass(ID), OS(dbgs()) {}
61 MIRPrintingPass(raw_ostream &OS) : MachineFunctionPass(ID), OS(OS) {}
62
63 StringRef getPassName() const override { return "MIR Printing Pass"; }
64
65 void getAnalysisUsage(AnalysisUsage &AU) const override {
66 AU.setPreservesAll();
67 AU.addUsedIfAvailable<VirtRegMapWrapperLegacy>();
69 }
70
71 bool runOnMachineFunction(MachineFunction &MF) override {
72 std::string Str;
73 raw_string_ostream StrOS(Str);
74
75 MachineModuleInfo *MMI =
76 &getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
77
78 const VirtRegMap *VRM = nullptr;
79 if (auto *W = getAnalysisIfAvailable<VirtRegMapWrapperLegacy>())
80 VRM = &W->getVRM();
81
82 MachineModuleSlotTracker MST(
83 [&](const Function &F) { return MMI->getMachineFunction(F); }, &MF);
84 MST.renumberMetadataForAssembly();
85 printMIR(StrOS, *MMI, MF, VRM);
86 MachineFunctions.append(Str);
87 return false;
88 }
89
90 bool doFinalization(Module &M) override {
91 M.renumberMetadataForAssembly();
92 printMIR(OS, M);
93 OS << MachineFunctions;
94 return false;
95 }
96};
97
98char MIRPrintingPass::ID = 0;
99
100} // end anonymous namespace
101
102char &llvm::MIRPrintingPassID = MIRPrintingPass::ID;
103INITIALIZE_PASS(MIRPrintingPass, "mir-printer", "MIR Printer", false, false)
104
106 return new MIRPrintingPass(OS);
107}
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition MD5.cpp:54
Machine Check Debug Module
FunctionAnalysisManager FAM
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
AnalysisUsage & addUsedIfAvailable()
Add the specified Pass class to the set of analyses used by this pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
This analysis create MachineFunction for given Function.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
LLVM_ABI MachineFunction * getMachineFunction(const Function &F) const
Returns the MachineFunction associated to IR function F if there is one, otherwise nullptr.
void renumberMetadataForAssembly()
Renumber module and machine metadata for canonical MIR output.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &MFAM)
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI MachineFunctionPass * createPrintMIRPass(raw_ostream &OS)
MIRPrinting pass - this pass prints out the LLVM IR into the given stream using the MIR serialization...
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI void printMIR(raw_ostream &OS, const Module &M)
Print LLVM IR using the MIR serialization format to the given output stream.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI char & MIRPrintingPassID
MIRPrintingPass - this pass prints out the LLVM IR using the MIR serialization format.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39