[clang][NFC] simplify the unset check in `ParseLabeledStatement` (#117430)
[llvm-project.git] / llvm / lib / CodeGen / MIRPrintingPass.cpp
blobfc79410f97b5896ba998e55696568ec7d055c63a
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.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/MIRPrinter.h"
15 #include "llvm/CodeGen/MachineFunctionPass.h"
16 #include "llvm/CodeGen/MachineModuleInfo.h"
17 #include "llvm/CodeGen/Passes.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/InitializePasses.h"
21 using namespace llvm;
23 PreservedAnalyses PrintMIRPreparePass::run(Module &M, ModuleAnalysisManager &) {
24 printMIR(OS, M);
25 return PreservedAnalyses::all();
28 PreservedAnalyses PrintMIRPass::run(MachineFunction &MF,
29 MachineFunctionAnalysisManager &MFAM) {
30 auto &MAMP = MFAM.getResult<ModuleAnalysisManagerMachineFunctionProxy>(MF);
31 Module *M = MF.getFunction().getParent();
32 const MachineModuleInfo &MMI =
33 MAMP.getCachedResult<MachineModuleAnalysis>(*M)->getMMI();
35 printMIR(OS, MMI, MF);
36 return PreservedAnalyses::all();
39 namespace {
41 /// This pass prints out the LLVM IR to an output stream using the MIR
42 /// serialization format.
43 struct MIRPrintingPass : public MachineFunctionPass {
44 static char ID;
45 raw_ostream &OS;
46 std::string MachineFunctions;
48 MIRPrintingPass() : MachineFunctionPass(ID), OS(dbgs()) {}
49 MIRPrintingPass(raw_ostream &OS) : MachineFunctionPass(ID), OS(OS) {}
51 StringRef getPassName() const override { return "MIR Printing Pass"; }
53 void getAnalysisUsage(AnalysisUsage &AU) const override {
54 AU.setPreservesAll();
55 MachineFunctionPass::getAnalysisUsage(AU);
58 bool runOnMachineFunction(MachineFunction &MF) override {
59 std::string Str;
60 raw_string_ostream StrOS(Str);
62 const MachineModuleInfo &MMI =
63 getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
65 printMIR(StrOS, MMI, MF);
66 MachineFunctions.append(Str);
67 return false;
70 bool doFinalization(Module &M) override {
71 printMIR(OS, M);
72 OS << MachineFunctions;
73 return false;
77 char MIRPrintingPass::ID = 0;
79 } // end anonymous namespace
81 char &llvm::MIRPrintingPassID = MIRPrintingPass::ID;
82 INITIALIZE_PASS(MIRPrintingPass, "mir-printer", "MIR Printer", false, false)
84 namespace llvm {
86 MachineFunctionPass *createPrintMIRPass(raw_ostream &OS) {
87 return new MIRPrintingPass(OS);
90 } // end namespace llvm