LLVM  10.0.0
IRPrintingPasses.cpp
Go to the documentation of this file.
1 //===--- IRPrintingPasses.cpp - Module and Function printing passes -------===//
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 // PrintModulePass and PrintFunctionPass implementations.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 #include "llvm/IR/Function.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/IR/PassManager.h"
17 #include "llvm/InitializePasses.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Support/Debug.h"
21 using namespace llvm;
22 
24 PrintModulePass::PrintModulePass(raw_ostream &OS, const std::string &Banner,
25  bool ShouldPreserveUseListOrder)
26  : OS(OS), Banner(Banner),
27  ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {}
28 
30  if (llvm::isFunctionInPrintList("*")) {
31  if (!Banner.empty())
32  OS << Banner << "\n";
33  M.print(OS, nullptr, ShouldPreserveUseListOrder);
34  }
35  else {
36  bool BannerPrinted = false;
37  for(const auto &F : M.functions()) {
38  if (llvm::isFunctionInPrintList(F.getName())) {
39  if (!BannerPrinted && !Banner.empty()) {
40  OS << Banner << "\n";
41  BannerPrinted = true;
42  }
43  F.print(OS);
44  }
45  }
46  }
47  return PreservedAnalyses::all();
48 }
49 
51 PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner)
52  : OS(OS), Banner(Banner) {}
53 
56  if (isFunctionInPrintList(F.getName())) {
57  if (forcePrintModuleIR())
58  OS << Banner << " (function: " << F.getName() << ")\n" << *F.getParent();
59  else
60  OS << Banner << '\n' << static_cast<Value &>(F);
61  }
62  return PreservedAnalyses::all();
63 }
64 
65 namespace {
66 
67 class PrintModulePassWrapper : public ModulePass {
69 
70 public:
71  static char ID;
72  PrintModulePassWrapper() : ModulePass(ID) {}
73  PrintModulePassWrapper(raw_ostream &OS, const std::string &Banner,
74  bool ShouldPreserveUseListOrder)
75  : ModulePass(ID), P(OS, Banner, ShouldPreserveUseListOrder) {}
76 
77  bool runOnModule(Module &M) override {
78  ModuleAnalysisManager DummyMAM;
79  P.run(M, DummyMAM);
80  return false;
81  }
82 
83  void getAnalysisUsage(AnalysisUsage &AU) const override {
84  AU.setPreservesAll();
85  }
86 
87  StringRef getPassName() const override { return "Print Module IR"; }
88 };
89 
90 class PrintFunctionPassWrapper : public FunctionPass {
92 
93 public:
94  static char ID;
95  PrintFunctionPassWrapper() : FunctionPass(ID) {}
96  PrintFunctionPassWrapper(raw_ostream &OS, const std::string &Banner)
97  : FunctionPass(ID), P(OS, Banner) {}
98 
99  // This pass just prints a banner followed by the function as it's processed.
100  bool runOnFunction(Function &F) override {
101  FunctionAnalysisManager DummyFAM;
102  P.run(F, DummyFAM);
103  return false;
104  }
105 
106  void getAnalysisUsage(AnalysisUsage &AU) const override {
107  AU.setPreservesAll();
108  }
109 
110  StringRef getPassName() const override { return "Print Function IR"; }
111 };
112 
113 }
114 
116 INITIALIZE_PASS(PrintModulePassWrapper, "print-module",
117  "Print module to stderr", false, true)
119 INITIALIZE_PASS(PrintFunctionPassWrapper, "print-function",
120  "Print function to stderr", false, true)
121 
123  const std::string &Banner,
124  bool ShouldPreserveUseListOrder) {
125  return new PrintModulePassWrapper(OS, Banner, ShouldPreserveUseListOrder);
126 }
127 
129  const std::string &Banner) {
130  return new PrintFunctionPassWrapper(OS, Banner);
131 }
132 
134  const char *PID = (const char*)P->getPassID();
135 
136  return (PID == &PrintModulePassWrapper::ID) ||
138 }
Pass interface - Implemented by all &#39;passes&#39;.
Definition: Pass.h:78
bool forcePrintModuleIR()
forcePrintModuleIR - returns true if IR printing passes should
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
Pass for printing a Module as LLVM&#39;s text IR assembly.
FunctionPass * createPrintFunctionPass(raw_ostream &OS, const std::string &Banner="")
Create and return a pass that prints functions to the specified raw_ostream as they are processed...
F(f)
Definition: BitVector.h:937
INITIALIZE_PASS(PrintModulePassWrapper, "print-module", "Print module to stderr", false, true) char PrintFunctionPassWrapper INITIALIZE_PASS(PrintFunctionPassWrapper, "print-function", "Print function to stderr", false, true) ModulePass *llvm
bool isIRPrintingPass(Pass *P)
Return true if a pass is for IR printing.
static bool runOnFunction(Function &F, bool PostInlining)
#define P(N)
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:154
iterator_range< iterator > functions()
Definition: Module.h:610
AnalysisID getPassID() const
getPassID - Return the PassID number that corresponds to this pass.
Definition: Pass.h:97
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:282
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:160
void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW, bool ShouldPreserveUseListOrder=false, bool IsForDebug=false) const
Print the module to an output stream with an optional AssemblyAnnotationWriter.
Definition: AsmWriter.cpp:4233
Module.h This file contains the declarations for the Module class.
bool isFunctionInPrintList(StringRef FunctionName)
isFunctionInPrintList - returns true if a function should be printed via
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
void setPreservesAll()
Set by analyses that do not transform their input at all.
PreservedAnalyses run(Module &M, AnalysisManager< Module > &)
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:215
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:222
ModulePass * createPrintModulePass(raw_ostream &OS, const std::string &Banner="", bool ShouldPreserveUseListOrder=false)
Create and return a pass that writes the module to the specified raw_ostream.
This file defines passes to print out IR in various granularities.
aarch64 promote const
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:575
PreservedAnalyses run(Function &F, AnalysisManager< Function > &)
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
A container for analyses that lazily runs them and caches their results.
This header defines various interfaces for pass management in LLVM.
Pass for printing a Function as LLVM&#39;s text IR assembly.