1 //===- IRPrintingPasses.h - Passes to print out IR constructs ---*- C++ -*-===//
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 /// \file
9 ///
10 /// This file defines passes to print out IR in various granularities. The
11 /// PrintModulePass pass simply prints out the entire module when it is
12 /// executed. The PrintFunctionPass class is designed to be pipelined with
13 /// other FunctionPass's, and prints out the functions of the module as they
14 /// are processed.
15 ///
16 //===----------------------------------------------------------------------===//
17 
18 #ifndef LLVM_IR_IRPRINTINGPASSES_H
19 #define LLVM_IR_IRPRINTINGPASSES_H
20 
21 #include "llvm/IR/PassManager.h"
22 #include <string>
23 
24 namespace llvm {
25 class raw_ostream;
26 class StringRef;
27 
28 /// Create and return a pass that writes the module to the specified
29 /// \c raw_ostream.
30 ModulePass *createPrintModulePass(raw_ostream &OS,
31                                   const std::string &Banner = "",
32                                   bool ShouldPreserveUseListOrder = false);
33 
34 /// Create and return a pass that prints functions to the specified
35 /// \c raw_ostream as they are processed.
36 FunctionPass *createPrintFunctionPass(raw_ostream &OS,
37                                       const std::string &Banner = "");
38 
39 /// Print out a name of an LLVM value without any prefixes.
40 ///
41 /// The name is surrounded with ""'s and escaped if it has any special or
42 /// non-printable characters in it.
43 void printLLVMNameWithoutPrefix(raw_ostream &OS, StringRef Name);
44 
45 /// Return true if a pass is for IR printing.
46 bool isIRPrintingPass(Pass *P);
47 
48 /// Pass for printing a Module as LLVM's text IR assembly.
49 ///
50 /// Note: This pass is for use with the new pass manager. Use the create...Pass
51 /// functions above to create passes for use with the legacy pass manager.
52 class PrintModulePass : public PassInfoMixin<PrintModulePass> {
53   raw_ostream &OS;
54   std::string Banner;
55   bool ShouldPreserveUseListOrder;
56 
57 public:
58   PrintModulePass();
59   PrintModulePass(raw_ostream &OS, const std::string &Banner = "",
60                   bool ShouldPreserveUseListOrder = false);
61 
62   PreservedAnalyses run(Module &M, AnalysisManager<Module> &);
63   static bool isRequired() { return true; }
64 };
65 
66 /// Pass for printing a Function as LLVM's text IR assembly.
67 ///
68 /// Note: This pass is for use with the new pass manager. Use the create...Pass
69 /// functions above to create passes for use with the legacy pass manager.
70 class PrintFunctionPass : public PassInfoMixin<PrintFunctionPass> {
71   raw_ostream &OS;
72   std::string Banner;
73 
74 public:
75   PrintFunctionPass();
76   PrintFunctionPass(raw_ostream &OS, const std::string &Banner = "");
77 
78   PreservedAnalyses run(Function &F, AnalysisManager<Function> &);
79   static bool isRequired() { return true; }
80 };
81 
82 } // namespace llvm
83 
84 #endif
85