LLVM 17.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
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
31using namespace llvm;
32using namespace ore;
33
34Pass *MachineFunctionPass::createPrinterPass(raw_ostream &O,
35 const std::string &Banner) const {
36 return createMachineFunctionPrinterPass(O, Banner);
37}
38
39bool MachineFunctionPass::runOnFunction(Function &F) {
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",
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) {
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}
This is the interface for LLVM's primary stateless and local alias analysis.
This is the interface for a simple mod/ref and alias analysis over globals.
#define F(x, y, z)
Definition: MD5.cpp:55
===- MachineOptimizationRemarkEmitter.h - Opt Diagnostics -*- C++ -*-—===//
This is the interface for a SCEV-based alias analysis.
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Legacy wrapper pass to provide the BasicAAResult object.
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:314
DISubprogram * getSubprogram() const
Get the attached subprogram.
Definition: Metadata.cpp:1625
Legacy wrapper pass to provide the GlobalsAAResult object.
The legacy pass manager's analysis pass to compute loop information.
Definition: LoopInfo.h:1293
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Properties which a MachineFunction may have at a given point in time.
void print(raw_ostream &OS) const
Print the MachineFunctionProperties in human-readable form.
bool verifyRequiredProperties(const MachineFunctionProperties &V) const
MachineFunctionProperties & set(Property P)
MachineFunctionProperties & reset(Property P)
unsigned getInstructionCount() const
Return the number of MachineInstrs in this MachineFunction.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineFunctionProperties & getProperties() const
Get the function properties.
const MachineBasicBlock & front() const
void print(raw_ostream &OS, const SlotIndexes *=nullptr) const
print - Print out the MachineFunction in a format suitable for debugging to the specified stream.
This class contains meta information specific to a module.
MachineFunction & getOrCreateMachineFunction(Function &F)
Returns the MachineFunction constructed for the IR function F.
Diagnostic information for optimization analysis remarks.
A wrapper analysis pass for the legacy pass manager that exposes a MemoryDepnedenceResults instance.
PassInfo class - An instance of this class exists for every pass known by the system,...
Definition: PassInfo.h:30
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:91
AnalysisID getPassID() const
getPassID - Return the PassID number that corresponds to this pass.
Definition: Pass.h:110
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:98
static const PassInfo * lookupPassInfo(const void *TI)
Definition: Pass.cpp:188
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
Legacy wrapper pass to provide the SCEVAAResult object.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:672
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
DiagnosticInfoOptimizationBase::Argument NV
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
cl::opt< ChangePrinter > PrintChanged
bool isFunctionInPrintList(StringRef FunctionName)
bool isPassInPrintList(StringRef PassName)
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
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...
std::string doSystemDiff(StringRef Before, StringRef After, StringRef OldLineFormat, StringRef NewLineFormat, StringRef UnchangedLineFormat)
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:1869
#define MORE()
Definition: regcomp.c:252