LLVM  16.0.0git
MachineFunctionPass.cpp
Go to the documentation of this file.
1 //===-- MachineFunctionPass.cpp -------------------------------------------===//
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 contains the definitions of the MachineFunctionPass members.
10 //
11 //===----------------------------------------------------------------------===//
12 
17 #include "llvm/Analysis/IVUsers.h"
18 #include "llvm/Analysis/LoopInfo.h"
26 #include "llvm/CodeGen/Passes.h"
27 #include "llvm/IR/Dominators.h"
28 #include "llvm/IR/Function.h"
29 #include "llvm/IR/PrintPasses.h"
30 
31 using namespace llvm;
32 using namespace ore;
33 
34 Pass *MachineFunctionPass::createPrinterPass(raw_ostream &O,
35  const std::string &Banner) const {
36  return createMachineFunctionPrinterPass(O, Banner);
37 }
38 
40  // Do not codegen any 'available_externally' functions at all, they have
41  // definitions outside the translation unit.
42  if (F.hasAvailableExternallyLinkage())
43  return false;
44 
45  MachineModuleInfo &MMI = getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
47 
49 
50 #ifndef NDEBUG
51  if (!MFProps.verifyRequiredProperties(RequiredProperties)) {
52  errs() << "MachineFunctionProperties required by " << getPassName()
53  << " pass are not met by function " << F.getName() << ".\n"
54  << "Required properties: ";
55  RequiredProperties.print(errs());
56  errs() << "\nCurrent properties: ";
57  MFProps.print(errs());
58  errs() << "\n";
59  llvm_unreachable("MachineFunctionProperties check failed");
60  }
61 #endif
62  // Collect the MI count of the function before the pass.
63  unsigned CountBefore, CountAfter;
64 
65  // Check if the user asked for size remarks.
66  bool ShouldEmitSizeRemarks =
67  F.getParent()->shouldEmitInstrCountChangedRemark();
68 
69  // If we want size remarks, collect the number of MachineInstrs in our
70  // MachineFunction before the pass runs.
71  if (ShouldEmitSizeRemarks)
72  CountBefore = MF.getInstructionCount();
73 
74  // For --print-changed, if the function name is a candidate, save the
75  // serialized MF to be compared later.
76  SmallString<0> BeforeStr, AfterStr;
77  StringRef PassID;
79  if (const PassInfo *PI = Pass::lookupPassInfo(getPassID()))
80  PassID = PI->getPassArgument();
81  }
82  const bool IsInterestingPass = isPassInPrintList(PassID);
83  const bool ShouldPrintChanged = PrintChanged != ChangePrinter::None &&
84  IsInterestingPass &&
86  if (ShouldPrintChanged) {
87  raw_svector_ostream OS(BeforeStr);
88  MF.print(OS);
89  }
90 
91  bool RV = runOnMachineFunction(MF);
92 
93  if (ShouldEmitSizeRemarks) {
94  // We wanted size remarks. Check if there was a change to the number of
95  // MachineInstrs in the module. Emit a remark if there was a change.
96  CountAfter = MF.getInstructionCount();
97  if (CountBefore != CountAfter) {
99  MORE.emit([&]() {
100  int64_t Delta = static_cast<int64_t>(CountAfter) -
101  static_cast<int64_t>(CountBefore);
102  MachineOptimizationRemarkAnalysis R("size-info", "FunctionMISizeChange",
103  MF.getFunction().getSubprogram(),
104  &MF.front());
105  R << NV("Pass", getPassName())
106  << ": Function: " << NV("Function", F.getName()) << ": "
107  << "MI Instruction count changed from "
108  << NV("MIInstrsBefore", CountBefore) << " to "
109  << NV("MIInstrsAfter", CountAfter)
110  << "; Delta: " << NV("Delta", Delta);
111  return R;
112  });
113  }
114  }
115 
116  MFProps.set(SetProperties);
117  MFProps.reset(ClearedProperties);
118 
119  // For --print-changed, print if the serialized MF has changed. Modes other
120  // than quiet/verbose are unimplemented and treated the same as 'quiet'.
121  if (ShouldPrintChanged || !IsInterestingPass) {
122  if (ShouldPrintChanged) {
123  raw_svector_ostream OS(AfterStr);
124  MF.print(OS);
125  }
126  if (IsInterestingPass && BeforeStr != AfterStr) {
127  errs() << ("*** IR Dump After " + getPassName() + " (" + PassID +
128  ") on " + MF.getName() + " ***\n");
129  switch (PrintChanged) {
130  case ChangePrinter::None:
131  llvm_unreachable("");
134  case ChangePrinter::DotCfgQuiet: // unimplemented
135  case ChangePrinter::DotCfgVerbose: // unimplemented
136  errs() << AfterStr;
137  break;
142  bool Color = llvm::is_contained(
144  PrintChanged.getValue());
145  StringRef Removed = Color ? "\033[31m-%l\033[0m\n" : "-%l\n";
146  StringRef Added = Color ? "\033[32m+%l\033[0m\n" : "+%l\n";
147  StringRef NoChange = " %l\n";
148  errs() << doSystemDiff(BeforeStr, AfterStr, Removed, Added, NoChange);
149  break;
150  }
151  }
155  PrintChanged.getValue())) {
156  const char *Reason =
157  IsInterestingPass ? " omitted because no change" : " filtered out";
158  errs() << "*** IR Dump After " << getPassName();
159  if (!PassID.empty())
160  errs() << " (" << PassID << ")";
161  errs() << " on " << MF.getName() + Reason + " ***\n";
162  }
163  }
164  return RV;
165 }
166 
170 
171  // MachineFunctionPass preserves all LLVM IR passes, but there's no
172  // high-level way to express this. Instead, just list a bunch of
173  // passes explicitly. This does not include setPreservesCFG,
174  // because CodeGen overloads that to mean preserving the MachineBasicBlock
175  // CFG in addition to the LLVM IR CFG.
186 
188 }
llvm::PrintChanged
cl::opt< ChangePrinter > PrintChanged
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
DominanceFrontier.h
llvm::MachineFunctionProperties::print
void print(raw_ostream &OS) const
Print the MachineFunctionProperties in human-readable form.
Definition: MachineFunction.cpp:134
llvm::Function
Definition: Function.h:60
llvm::ChangePrinter::Quiet
@ Quiet
llvm::Function::getSubprogram
DISubprogram * getSubprogram() const
Get the attached subprogram.
Definition: Metadata.cpp:1583
llvm::MachineOptimizationRemarkEmitter
The optimization diagnostic interface.
Definition: MachineOptimizationRemarkEmitter.h:151
OptimizationRemarkEmitter.h
llvm::ChangePrinter::ColourDiffVerbose
@ ColourDiffVerbose
GlobalsModRef.h
ScalarEvolution.h
llvm::MachineFunction::getInstructionCount
unsigned getInstructionCount() const
Return the number of MachineInstrs in this MachineFunction.
Definition: MachineFunction.h:896
llvm::LoopInfoWrapperPass
The legacy pass manager's analysis pass to compute loop information.
Definition: LoopInfo.h:1290
llvm::PassInfo
PassInfo class - An instance of this class exists for every pass known by the system,...
Definition: PassInfo.h:30
llvm::MachineFunctionProperties
Properties which a MachineFunction may have at a given point in time.
Definition: MachineFunction.h:127
llvm::ore::NV
DiagnosticInfoOptimizationBase::Argument NV
Definition: OptimizationRemarkEmitter.h:136
llvm::errs
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Definition: raw_ostream.cpp:891
MemoryDependenceAnalysis.h
BasicAliasAnalysis.h
llvm::MachineFunctionPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Definition: MachineFunctionPass.cpp:167
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::RISCVFenceField::R
@ R
Definition: RISCVBaseInfo.h:265
llvm::MachineFunction::front
const MachineBasicBlock & front() const
Definition: MachineFunction.h:865
llvm::Pass::lookupPassInfo
static const PassInfo * lookupPassInfo(const void *TI)
Definition: Pass.cpp:186
llvm::MachineFunctionProperties::verifyRequiredProperties
bool verifyRequiredProperties(const MachineFunctionProperties &V) const
Definition: MachineFunction.h:224
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
llvm::ChangePrinter::Verbose
@ Verbose
llvm::MachineFunction::getProperties
const MachineFunctionProperties & getProperties() const
Get the function properties.
Definition: MachineFunction.h:747
llvm::ChangePrinter::None
@ None
llvm::ChangePrinter::DiffQuiet
@ DiffQuiet
llvm::MachineFunctionProperties::set
MachineFunctionProperties & set(Property P)
Definition: MachineFunction.h:196
llvm::MachineModuleInfo
This class contains meta information specific to a module.
Definition: MachineModuleInfo.h:74
llvm::DominatorTreeWrapperPass
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:302
llvm::MachineOptimizationRemarkAnalysis
Diagnostic information for optimization analysis remarks.
Definition: MachineOptimizationRemarkEmitter.h:109
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
llvm::ScalarEvolutionWrapperPass
Definition: ScalarEvolution.h:2166
PrintPasses.h
llvm::SmallString< 0 >
llvm::createMachineFunctionPrinterPass
MachineFunctionPass * createMachineFunctionPrinterPass(raw_ostream &OS, const std::string &Banner="")
MachineFunctionPrinter pass - This pass prints out the machine function to the given stream as a debu...
LoopInfo.h
MachineOptimizationRemarkEmitter.h
===- MachineOptimizationRemarkEmitter.h - Opt Diagnostics -*- C++ -*-—===//
Passes.h
llvm::StringRef::empty
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:264
llvm::SCEVAAWrapperPass
Legacy wrapper pass to provide the SCEVAAResult object.
Definition: ScalarEvolutionAliasAnalysis.h:56
llvm::MachineModuleInfoWrapperPass
Definition: MachineModuleInfo.h:214
llvm::BasicAAWrapperPass
Legacy wrapper pass to provide the BasicAAResult object.
Definition: BasicAliasAnalysis.h:173
llvm::ChangePrinter::DotCfgVerbose
@ DotCfgVerbose
llvm::ChangePrinter::DotCfgQuiet
@ DotCfgQuiet
llvm::is_contained
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.
Definition: STLExtras.h:1673
llvm::isFunctionInPrintList
bool isFunctionInPrintList(StringRef FunctionName)
Definition: PrintPasses.cpp:151
MachineFunctionPass.h
llvm::MachineFunction::getName
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
Definition: MachineFunction.cpp:567
MachineModuleInfo.h
llvm::MachineFunction
Definition: MachineFunction.h:257
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
llvm::AnalysisUsage::addPreserved
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Definition: PassAnalysisSupport.h:98
llvm::IVUsersWrapperPass
Definition: IVUsers.h:161
llvm::MachineModuleInfo::getOrCreateMachineFunction
MachineFunction & getOrCreateMachineFunction(Function &F)
Returns the MachineFunction constructed for the IR function F.
Definition: MachineModuleInfo.cpp:108
llvm::MachineFunctionProperties::reset
MachineFunctionProperties & reset(Property P)
Definition: MachineFunction.h:201
MORE
#define MORE()
Definition: regcomp.c:252
runOnFunction
static bool runOnFunction(Function &F, bool PostInlining)
Definition: EntryExitInstrumenter.cpp:69
llvm::MachineFunction::getFunction
Function & getFunction()
Return the LLVM function that this machine code represents.
Definition: MachineFunction.h:622
llvm::MemoryDependenceWrapperPass
A wrapper analysis pass for the legacy pass manager that exposes a MemoryDepnedenceResults instance.
Definition: MemoryDependenceAnalysis.h:526
llvm::doSystemDiff
std::string doSystemDiff(StringRef Before, StringRef After, StringRef OldLineFormat, StringRef NewLineFormat, StringRef UnchangedLineFormat)
Definition: PrintPasses.cpp:158
llvm::isPassInPrintList
bool isPassInPrintList(StringRef PassName)
Definition: PrintPasses.cpp:143
Function.h
llvm::MachineFunction::print
void print(raw_ostream &OS, const SlotIndexes *=nullptr) const
print - Print out the MachineFunction in a format suitable for debugging to the specified stream.
Definition: MachineFunction.cpp:571
ScalarEvolutionAliasAnalysis.h
llvm::Pass
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:91
IVUsers.h
llvm::ChangePrinter::ColourDiffQuiet
@ ColourDiffQuiet
Dominators.h
llvm::AAResultsWrapperPass
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
Definition: AliasAnalysis.h:1308
llvm::GlobalsAAWrapperPass
Legacy wrapper pass to provide the GlobalsAAResult object.
Definition: GlobalsModRef.h:144
llvm::raw_svector_ostream
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:658
llvm::Pass::getAnalysisUsage
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:97
llvm::ChangePrinter::DiffVerbose
@ DiffVerbose
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition: PassAnalysisSupport.h:75
llvm::DominanceFrontierWrapperPass
Definition: DominanceFrontier.h:155
MachineFunction.h