LLVM 19.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 MFProps.reset(ClearedProperties);
92
93 bool RV = runOnMachineFunction(MF);
94
95 if (ShouldEmitSizeRemarks) {
96 // We wanted size remarks. Check if there was a change to the number of
97 // MachineInstrs in the module. Emit a remark if there was a change.
98 CountAfter = MF.getInstructionCount();
99 if (CountBefore != CountAfter) {
101 MORE.emit([&]() {
102 int64_t Delta = static_cast<int64_t>(CountAfter) -
103 static_cast<int64_t>(CountBefore);
104 MachineOptimizationRemarkAnalysis R("size-info", "FunctionMISizeChange",
106 &MF.front());
107 R << NV("Pass", getPassName())
108 << ": Function: " << NV("Function", F.getName()) << ": "
109 << "MI Instruction count changed from "
110 << NV("MIInstrsBefore", CountBefore) << " to "
111 << NV("MIInstrsAfter", CountAfter)
112 << "; Delta: " << NV("Delta", Delta);
113 return R;
114 });
115 }
116 }
117
118 MFProps.set(SetProperties);
119
120 // For --print-changed, print if the serialized MF has changed. Modes other
121 // than quiet/verbose are unimplemented and treated the same as 'quiet'.
122 if (ShouldPrintChanged || !IsInterestingPass) {
123 if (ShouldPrintChanged) {
124 raw_svector_ostream OS(AfterStr);
125 MF.print(OS);
126 }
127 if (IsInterestingPass && BeforeStr != AfterStr) {
128 errs() << ("*** IR Dump After " + getPassName() + " (" + PassID +
129 ") on " + MF.getName() + " ***\n");
130 switch (PrintChanged) {
135 case ChangePrinter::DotCfgQuiet: // unimplemented
136 case ChangePrinter::DotCfgVerbose: // unimplemented
137 errs() << AfterStr;
138 break;
143 bool Color = llvm::is_contained(
145 PrintChanged.getValue());
146 StringRef Removed = Color ? "\033[31m-%l\033[0m\n" : "-%l\n";
147 StringRef Added = Color ? "\033[32m+%l\033[0m\n" : "+%l\n";
148 StringRef NoChange = " %l\n";
149 errs() << doSystemDiff(BeforeStr, AfterStr, Removed, Added, NoChange);
150 break;
151 }
152 }
156 PrintChanged.getValue())) {
157 const char *Reason =
158 IsInterestingPass ? " omitted because no change" : " filtered out";
159 errs() << "*** IR Dump After " << getPassName();
160 if (!PassID.empty())
161 errs() << " (" << PassID << ")";
162 errs() << " on " << MF.getName() + Reason + " ***\n";
163 }
164 }
165 return RV;
166}
167
171
172 // MachineFunctionPass preserves all LLVM IR passes, but there's no
173 // high-level way to express this. Instead, just list a bunch of
174 // passes explicitly. This does not include setPreservesCFG,
175 // because CodeGen overloads that to mean preserving the MachineBasicBlock
176 // CFG in addition to the LLVM IR CFG.
187
189}
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++ -*-—===//
raw_pwrite_stream & OS
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:317
DISubprogram * getSubprogram() const
Get the attached subprogram.
Definition: Metadata.cpp:1828
Legacy wrapper pass to provide the GlobalsAAResult object.
The legacy pass manager's analysis pass to compute loop information.
Definition: LoopInfo.h:593
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:94
AnalysisID getPassID() const
getPassID - Return the PassID number that corresponds to this pass.
Definition: Pass.h:113
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:192
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:690
#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)
Returns true if Element is found in Range.
Definition: STLExtras.h:1888
#define MORE()
Definition: regcomp.c:252