LLVM  13.0.0git
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/ADT/StringRef.h"
15 #include "llvm/IR/Function.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/IR/PrintPasses.h"
18 #include "llvm/InitializePasses.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Support/Debug.h"
22 
23 using namespace llvm;
24 
26 PrintModulePass::PrintModulePass(raw_ostream &OS, const std::string &Banner,
27  bool ShouldPreserveUseListOrder)
28  : OS(OS), Banner(Banner),
29  ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {}
30 
32  if (llvm::isFunctionInPrintList("*")) {
33  if (!Banner.empty())
34  OS << Banner << "\n";
35  M.print(OS, nullptr, ShouldPreserveUseListOrder);
36  }
37  else {
38  bool BannerPrinted = false;
39  for(const auto &F : M.functions()) {
40  if (llvm::isFunctionInPrintList(F.getName())) {
41  if (!BannerPrinted && !Banner.empty()) {
42  OS << Banner << "\n";
43  BannerPrinted = true;
44  }
45  F.print(OS);
46  }
47  }
48  }
49  return PreservedAnalyses::all();
50 }
51 
53 PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner)
54  : OS(OS), Banner(Banner) {}
55 
58  if (isFunctionInPrintList(F.getName())) {
59  if (forcePrintModuleIR())
60  OS << Banner << " (function: " << F.getName() << ")\n" << *F.getParent();
61  else
62  OS << Banner << '\n' << static_cast<Value &>(F);
63  }
64  return PreservedAnalyses::all();
65 }
66 
67 namespace {
68 
69 class PrintModulePassWrapper : public ModulePass {
71 
72 public:
73  static char ID;
74  PrintModulePassWrapper() : ModulePass(ID) {}
75  PrintModulePassWrapper(raw_ostream &OS, const std::string &Banner,
76  bool ShouldPreserveUseListOrder)
77  : ModulePass(ID), P(OS, Banner, ShouldPreserveUseListOrder) {}
78 
79  bool runOnModule(Module &M) override {
80  ModuleAnalysisManager DummyMAM;
81  P.run(M, DummyMAM);
82  return false;
83  }
84 
85  void getAnalysisUsage(AnalysisUsage &AU) const override {
86  AU.setPreservesAll();
87  }
88 
89  StringRef getPassName() const override { return "Print Module IR"; }
90 };
91 
92 class PrintFunctionPassWrapper : public FunctionPass {
94 
95 public:
96  static char ID;
97  PrintFunctionPassWrapper() : FunctionPass(ID) {}
98  PrintFunctionPassWrapper(raw_ostream &OS, const std::string &Banner)
99  : FunctionPass(ID), P(OS, Banner) {}
100 
101  // This pass just prints a banner followed by the function as it's processed.
102  bool runOnFunction(Function &F) override {
103  FunctionAnalysisManager DummyFAM;
104  P.run(F, DummyFAM);
105  return false;
106  }
107 
108  void getAnalysisUsage(AnalysisUsage &AU) const override {
109  AU.setPreservesAll();
110  }
111 
112  StringRef getPassName() const override { return "Print Function IR"; }
113 };
114 
115 }
116 
118 INITIALIZE_PASS(PrintModulePassWrapper, "print-module",
119  "Print module to stderr", false, true)
120 char PrintFunctionPassWrapper::ID = 0;
121 INITIALIZE_PASS(PrintFunctionPassWrapper, "print-function",
122  "Print function to stderr", false, true)
123 
125  const std::string &Banner,
126  bool ShouldPreserveUseListOrder) {
127  return new PrintModulePassWrapper(OS, Banner, ShouldPreserveUseListOrder);
128 }
129 
131  const std::string &Banner) {
132  return new PrintFunctionPassWrapper(OS, Banner);
133 }
134 
136  const char *PID = (const char*)P->getPassID();
137 
138  return (PID == &PrintModulePassWrapper::ID) ||
140 }
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm::PrintFunctionPass
Pass for printing a Function as LLVM's text IR assembly.
Definition: IRPrintingPasses.h:70
llvm
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
print
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
Definition: ArchiveWriter.cpp:147
llvm::ModulePass
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:238
llvm::Function
Definition: Function.h:61
StringRef.h
P
This currently compiles esp xmm0 movsd esp eax eax esp ret We should use not the dag combiner This is because dagcombine2 needs to be able to see through the X86ISD::Wrapper which DAGCombine can t really do The code for turning x load into a single vector load is target independent and should be moved to the dag combiner The code for turning x load into a vector load can only handle a direct load from a global or a direct load from the stack It should be generalized to handle any load from P
Definition: README-SSE.txt:411
Pass.h
llvm::PrintFunctionPass::run
PreservedAnalyses run(Function &F, AnalysisManager< Function > &)
Definition: IRPrintingPasses.cpp:56
to
Should compile to
Definition: README.txt:449
Module.h
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
llvm::PrintModulePass::PrintModulePass
PrintModulePass()
Definition: IRPrintingPasses.cpp:25
false
Definition: StackSlotColoring.cpp:142
llvm::PrintModulePass::run
PreservedAnalyses run(Module &M, AnalysisManager< Module > &)
Definition: IRPrintingPasses.cpp:31
llvm::PrintFunctionPass::PrintFunctionPass
PrintFunctionPass()
Definition: IRPrintingPasses.cpp:52
IRPrintingPasses.h
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:50
PrintPasses.h
const
aarch64 promote const
Definition: AArch64PromoteConstant.cpp:232
llvm::isFunctionInPrintList
bool isFunctionInPrintList(StringRef FunctionName)
Definition: PrintPasses.cpp:83
function
print Print MemDeps of function
Definition: MemDepPrinter.cpp:83
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
INITIALIZE_PASS
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
Definition: IRPrintingPasses.cpp:121
runOnFunction
static bool runOnFunction(Function &F, bool PostInlining)
Definition: EntryExitInstrumenter.cpp:69
std
Definition: BitVector.h:838
llvm::AnalysisUsage::setPreservesAll
void setPreservesAll()
Set by analyses that do not transform their input at all.
Definition: PassAnalysisSupport.h:130
llvm::isIRPrintingPass
bool isIRPrintingPass(Pass *P)
Return true if a pass is for IR printing.
Definition: IRPrintingPasses.cpp:135
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:161
Function.h
llvm::Pass
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:91
llvm::PrintModulePass
Pass for printing a Module as LLVM's text IR assembly.
Definition: IRPrintingPasses.h:52
llvm::forcePrintModuleIR
bool forcePrintModuleIR()
Definition: PrintPasses.cpp:81
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
raw_ostream.h
true
basic Basic Alias true
Definition: BasicAliasAnalysis.cpp:1799
InitializePasses.h
Debug.h
llvm::createPrintFunctionPass
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.
Definition: IRPrintingPasses.cpp:130
llvm::createPrintModulePass
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.
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38
INITIALIZE_PASS
TargetPassConfig.
Definition: TargetPassConfig.cpp:319