LLVM 20.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"
18#include "llvm/IR/Function.h"
20
21using namespace llvm;
22
24 printMIR(OS, M);
26}
27
31 Module *M = MF.getFunction().getParent();
32 const MachineModuleInfo &MMI =
33 MAMP.getCachedResult<MachineModuleAnalysis>(*M)->getMMI();
34
35 printMIR(OS, MMI, MF);
37}
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;
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();
56 }
57
58 bool runOnMachineFunction(MachineFunction &MF) override {
59 std::string Str;
60 raw_string_ostream StrOS(Str);
61
62 const MachineModuleInfo &MMI =
63 getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
64
65 printMIR(StrOS, MMI, MF);
66 MachineFunctions.append(Str);
67 return false;
68 }
69
70 bool doFinalization(Module &M) override {
71 printMIR(OS, M);
72 OS << MachineFunctions;
73 return false;
74 }
75};
76
77char MIRPrintingPass::ID = 0;
78
79} // end anonymous namespace
80
81char &llvm::MIRPrintingPassID = MIRPrintingPass::ID;
82INITIALIZE_PASS(MIRPrintingPass, "mir-printer", "MIR Printer", false, false)
83
84namespace llvm {
85
87 return new MIRPrintingPass(OS);
88}
89
90} // end namespace llvm
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
raw_pwrite_stream & OS
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:405
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:656
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.
Function & getFunction()
Return the LLVM function that this machine code represents.
An analysis that produces MachineModuleInfo for a module.
This class contains meta information specific to a module.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
An analysis over an "inner" IR unit that provides access to an analysis manager over a "outer" IR uni...
Definition: PassManager.h:688
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MFAM)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:661
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.
Definition: AddressRanges.h:18
MachineFunctionPass * createPrintMIRPass(raw_ostream &OS)
MIRPrinting pass - this pass prints out the LLVM IR into the given stream using the MIR serialization...
void printMIR(raw_ostream &OS, const Module &M)
Print LLVM IR using the MIR serialization format to the given output stream.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
char & MIRPrintingPassID
MIRPrintingPass - this pass prints out the LLVM IR using the MIR serialization format.