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"
23
24using namespace llvm;
25
26void PrintMIRPass::printMachineFunction(MachineFunction &MF,
29 const VirtRegMap *VRM = MFAM.getCachedResult<VirtRegMapAnalysis>(MF);
30 MachineModuleSlotTracker MST(
31 [&](const Function &F) {
32 return &FAM.getResult<MachineFunctionAnalysis>(
33 const_cast<Function &>(F))
34 .getMF();
35 },
36 &MF);
37 MST.renumberMetadataForAssembly();
38 printMIR(OS, FAM, MF, VRM);
39}
40
42 M.renumberMetadataForAssembly();
43 printMIR(OS, M);
44 auto &FAM = MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
45 for (Function &F : M) {
46 if (F.isDeclaration() || F.hasAvailableExternallyLinkage())
47 continue;
48 MachineFunction &MF = FAM.getResult<MachineFunctionAnalysis>(F).getMF();
50 .getManager();
51 printMachineFunction(MF, FAM, MFAM);
52 }
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>();
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(StrOS, *MMI, MF, VRM);
91 MachineFunctions.append(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
108 return new MIRPrintingPass(OS);
109}
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
ModuleAnalysisManager MAM
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.
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(Module &M, ModuleAnalysisManager &MAM)
Print the module and all its machine functions.
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...
InnerAnalysisManagerProxy< FunctionAnalysisManager, Module > FunctionAnalysisManagerModuleProxy
Provide the FunctionAnalysisManager to Module proxy.
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
InnerAnalysisManagerProxy< MachineFunctionAnalysisManager, Function > MachineFunctionAnalysisManagerFunctionProxy
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39