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/ADT/StringRef.h"
22 #include "llvm/IR/PassManager.h"
23 #include <string>
24 
25 namespace llvm {
26 
27 /// Create and return a pass that writes the module to the specified
28 /// \c raw_ostream.
29 ModulePass *createPrintModulePass(raw_ostream &OS,
30                                   const std::string &Banner = "",
31                                   bool ShouldPreserveUseListOrder = false);
32 
33 /// Create and return a pass that prints functions to the specified
34 /// \c raw_ostream as they are processed.
35 FunctionPass *createPrintFunctionPass(raw_ostream &OS,
36                                       const std::string &Banner = "");
37 
38 /// Print out a name of an LLVM value without any prefixes.
39 ///
40 /// The name is surrounded with ""'s and escaped if it has any special or
41 /// non-printable characters in it.
42 void printLLVMNameWithoutPrefix(raw_ostream &OS, StringRef Name);
43 
44 /// Return true if a pass is for IR printing.
45 bool isIRPrintingPass(Pass *P);
46 
47 /// isFunctionInPrintList - returns true if a function should be printed via
48 //  debugging options like -print-after-all/-print-before-all.
49 //  Tells if the function IR should be printed by PrinterPass.
50 extern bool isFunctionInPrintList(StringRef FunctionName);
51 
52 /// forcePrintModuleIR - returns true if IR printing passes should
53 //  be printing module IR (even for local-pass printers e.g. function-pass)
54 //  to provide more context, as enabled by debugging option -print-module-scope
55 //  Tells if IR printer should be printing module IR
56 extern bool forcePrintModuleIR();
57 
58 extern bool shouldPrintBeforePass();
59 extern bool shouldPrintBeforePass(StringRef);
60 extern bool shouldPrintAfterPass();
61 extern bool shouldPrintAfterPass(StringRef);
62 
63 /// Pass for printing a Module as LLVM's text IR assembly.
64 ///
65 /// Note: This pass is for use with the new pass manager. Use the create...Pass
66 /// functions above to create passes for use with the legacy pass manager.
67 class PrintModulePass : public PassInfoMixin<PrintModulePass> {
68   raw_ostream &OS;
69   std::string Banner;
70   bool ShouldPreserveUseListOrder;
71 
72 public:
73   PrintModulePass();
74   PrintModulePass(raw_ostream &OS, const std::string &Banner = "",
75                   bool ShouldPreserveUseListOrder = false);
76 
77   PreservedAnalyses run(Module &M, AnalysisManager<Module> &);
78 };
79 
80 /// Pass for printing a Function as LLVM's text IR assembly.
81 ///
82 /// Note: This pass is for use with the new pass manager. Use the create...Pass
83 /// functions above to create passes for use with the legacy pass manager.
84 class PrintFunctionPass : public PassInfoMixin<PrintFunctionPass> {
85   raw_ostream &OS;
86   std::string Banner;
87 
88 public:
89   PrintFunctionPass();
90   PrintFunctionPass(raw_ostream &OS, const std::string &Banner = "");
91 
92   PreservedAnalyses run(Function &F, AnalysisManager<Function> &);
93 };
94 
95 } // End llvm namespace
96 
97 #endif
98