LLVM 23.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
17#include "llvm/CodeGen/Passes.h"
19#include "llvm/IR/Function.h"
21
22using namespace llvm;
23
28
38
39namespace {
40
41/// This pass prints out the LLVM IR to an output stream using the MIR
42/// serialization format.
43struct MIRPrintingPass : public MachineFunctionPass {
44 static char ID;
45 raw_ostream &OS;
46 std::string MachineFunctions;
47
48 MIRPrintingPass() : MachineFunctionPass(ID), OS(dbgs()) {}
49 MIRPrintingPass(raw_ostream &OS) : MachineFunctionPass(ID), OS(OS) {}
50
51 StringRef getPassName() const override { return "MIR Printing Pass"; }
52
53 void getAnalysisUsage(AnalysisUsage &AU) const override {
54 AU.setPreservesAll();
55 AU.addUsedIfAvailable<VirtRegMapWrapperLegacy>();
57 }
58
59 bool runOnMachineFunction(MachineFunction &MF) override {
60 std::string Str;
61 raw_string_ostream StrOS(Str);
62
63 MachineModuleInfo *MMI =
64 &getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
65
66 const VirtRegMap *VRM = nullptr;
67 if (auto *W = getAnalysisIfAvailable<VirtRegMapWrapperLegacy>())
68 VRM = &W->getVRM();
69
70 printMIR(StrOS, *MMI, MF, VRM);
71 MachineFunctions.append(Str);
72 return false;
73 }
74
75 bool doFinalization(Module &M) override {
76 printMIR(OS, M);
77 OS << MachineFunctions;
78 return false;
79 }
80};
81
82char MIRPrintingPass::ID = 0;
83
84} // end anonymous namespace
85
86char &llvm::MIRPrintingPassID = MIRPrintingPass::ID;
87INITIALIZE_PASS(MIRPrintingPass, "mir-printer", "MIR Printer", false, false)
88
90 return new MIRPrintingPass(OS);
91}
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.
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.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
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
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
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