LLVM 23.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
31#include "llvm/CodeGen/Passes.h"
32#include "llvm/IR/Dominators.h"
33#include "llvm/IR/Function.h"
34#include "llvm/IR/Module.h"
35#include "llvm/IR/PrintPasses.h"
36
37using namespace llvm;
38using namespace ore;
39
41 "dropped-variable-stats-mir", cl::Hidden,
42 cl::desc("Dump dropped debug variables stats for MIR passes"),
43 cl::init(false));
44
45Pass *MachineFunctionPass::createPrinterPass(raw_ostream &O,
46 const std::string &Banner) const {
47 return createMachineFunctionPrinterPass(O, Banner);
48}
49
50bool MachineFunctionPass::runOnFunction(Function &F) {
51 // Do not codegen any 'available_externally' functions at all, they have
52 // definitions outside the translation unit.
53 if (F.hasAvailableExternallyLinkage())
54 return false;
55
56 MachineModuleInfo &MMI = getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
57 MachineFunction &MF = MMI.getOrCreateMachineFunction(F);
58
59 MachineFunctionProperties &MFProps = MF.getProperties();
60
61#ifndef NDEBUG
62 if (!MFProps.verifyRequiredProperties(RequiredProperties)) {
63 errs() << "MachineFunctionProperties required by " << getPassName()
64 << " pass are not met by function " << F.getName() << ".\n"
65 << "Required properties: ";
66 RequiredProperties.print(errs());
67 errs() << "\nCurrent properties: ";
68 MFProps.print(errs());
69 errs() << "\n";
70 llvm_unreachable("MachineFunctionProperties check failed");
71 }
72#endif
73 // Collect the MI count of the function before the pass.
74 unsigned CountBefore, CountAfter;
75
76 // Check if the user asked for size remarks.
77 bool ShouldEmitSizeRemarks =
78 F.getParent()->shouldEmitInstrCountChangedRemark();
79
80 // If we want size remarks, collect the number of MachineInstrs in our
81 // MachineFunction before the pass runs.
82 if (ShouldEmitSizeRemarks)
83 CountBefore = MF.getInstructionCount();
84
85 // For --print-changed, if the function name is a candidate, save the
86 // serialized MF to be compared later.
87 SmallString<0> BeforeStr, AfterStr;
88 StringRef PassID;
90 if (const PassInfo *PI = Pass::lookupPassInfo(getPassID()))
91 PassID = PI->getPassArgument();
92 }
93 const bool IsInterestingPass = isPassInPrintList(PassID);
94 const bool ShouldPrintChanged = PrintChanged != ChangePrinter::None &&
95 IsInterestingPass &&
97 if (ShouldPrintChanged) {
98 raw_svector_ostream OS(BeforeStr);
99 MF.print(OS);
100 }
101
102 MFProps.reset(ClearedProperties);
103
104 bool RV;
105 if (DroppedVarStatsMIR) {
106 DroppedVariableStatsMIR DroppedVarStatsMF;
107 auto PassName = getPassName();
108 DroppedVarStatsMF.runBeforePass(PassName, &MF);
109 RV = runOnMachineFunction(MF);
110 DroppedVarStatsMF.runAfterPass(PassName, &MF);
111 } else {
112 RV = runOnMachineFunction(MF);
113 }
114
115 if (ShouldEmitSizeRemarks) {
116 // We wanted size remarks. Check if there was a change to the number of
117 // MachineInstrs in the module. Emit a remark if there was a change.
118 CountAfter = MF.getInstructionCount();
119 if (CountBefore != CountAfter) {
120 MachineOptimizationRemarkEmitter MORE(MF, nullptr);
121 MORE.emit([&]() {
122 int64_t Delta = static_cast<int64_t>(CountAfter) -
123 static_cast<int64_t>(CountBefore);
124 MachineOptimizationRemarkAnalysis R("size-info", "FunctionMISizeChange",
126 &MF.front());
127 R << NV("Pass", getPassName())
128 << ": Function: " << NV("Function", F.getName()) << ": "
129 << "MI Instruction count changed from "
130 << NV("MIInstrsBefore", CountBefore) << " to "
131 << NV("MIInstrsAfter", CountAfter)
132 << "; Delta: " << NV("Delta", Delta);
133 return R;
134 });
135 }
136 }
137
138 MFProps.set(SetProperties);
139
140 // For --print-changed, print if the serialized MF has changed. Modes other
141 // than quiet/verbose are unimplemented and treated the same as 'quiet'.
142 if (ShouldPrintChanged || !IsInterestingPass) {
143 if (ShouldPrintChanged) {
144 raw_svector_ostream OS(AfterStr);
145 MF.print(OS);
146 }
147 if (IsInterestingPass && BeforeStr != AfterStr) {
148 errs() << ("*** IR Dump After " + getPassName() + " (" + PassID +
149 ") on " + MF.getName() + " ***\n");
150 switch (PrintChanged) {
155 case ChangePrinter::DotCfgQuiet: // unimplemented
156 case ChangePrinter::DotCfgVerbose: // unimplemented
157 errs() << AfterStr;
158 break;
163 bool Color = llvm::is_contained(
165 PrintChanged.getValue());
166 StringRef Removed = Color ? "\033[31m-%l\033[0m\n" : "-%l\n";
167 StringRef Added = Color ? "\033[32m+%l\033[0m\n" : "+%l\n";
168 StringRef NoChange = " %l\n";
169 errs() << doSystemDiff(BeforeStr, AfterStr, Removed, Added, NoChange);
170 break;
171 }
172 }
176 PrintChanged.getValue())) {
177 const char *Reason =
178 IsInterestingPass ? " omitted because no change" : " filtered out";
179 errs() << "*** IR Dump After " << getPassName();
180 if (!PassID.empty())
181 errs() << " (" << PassID << ")";
182 errs() << " on " << MF.getName() + Reason + " ***\n";
183 }
184 }
185 return RV;
186}
187
191
192 // MachineFunctionPass preserves all LLVM IR passes, but there's no
193 // high-level way to express this. Instead, just list a bunch of
194 // passes explicitly. This does not include setPreservesCFG,
195 // because CodeGen overloads that to mean preserving the MachineBasicBlock
196 // CFG in addition to the LLVM IR CFG.
211
213}
This is the interface for LLVM's primary stateless and local alias analysis.
===- DroppedVariableStatsMIR.h - Opt Diagnostics -*- C++ -*----------—===//
This is the interface for a simple mod/ref and alias analysis over globals.
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition MD5.cpp:54
static cl::opt< bool > DroppedVarStatsMIR("dropped-variable-stats-mir", cl::Hidden, cl::desc("Dump dropped debug variables stats for MIR passes"), cl::init(false))
===- MachineOptimizationRemarkEmitter.h - Opt Diagnostics -*- C++ -*-—===//
This is the interface for a SCEV-based alias analysis.
static const char PassName[]
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 BranchProbabilityInfo.
Legacy analysis pass which computes a DominatorTree.
Definition Dominators.h:321
void runBeforePass(StringRef PassID, MachineFunction *MF)
void runAfterPass(StringRef PassID, MachineFunction *MF)
DISubprogram * getSubprogram() const
Get the attached subprogram.
Legacy wrapper pass to provide the GlobalsAAResult object.
This is an alternative analysis pass to BlockFrequencyInfoWrapperPass.
This is an alternative analysis pass to BranchProbabilityInfoWrapperPass.
The legacy pass manager's analysis pass to compute loop information.
Definition LoopInfo.h:596
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...
LLVM_ABI 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.
LLVM_ABI MachineFunction & getOrCreateMachineFunction(Function &F)
Returns the MachineFunction constructed for the IR function F.
A wrapper analysis pass for the legacy pass manager that exposes a MemoryDepnedenceResults instance.
Pass interface - Implemented by all 'passes'.
Definition Pass.h:99
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
AnalysisID getPassID() const
getPassID - Return the PassID number that corresponds to this pass.
Definition Pass.h:122
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition Pass.cpp:112
static const PassInfo * lookupPassInfo(const void *TI)
Definition Pass.cpp:206
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition Pass.cpp:85
Legacy wrapper pass to provide the SCEVAAResult object.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
initializer< Ty > init(const Ty &Val)
Add a small namespace to avoid name clashes with the classes used in the streaming interface.
DiagnosticInfoOptimizationBase::Argument NV
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
cl::opt< ChangePrinter > PrintChanged
bool isFunctionInPrintList(StringRef FunctionName)
bool isPassInPrintList(StringRef PassName)
LLVM_ABI 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...
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
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:1947
#define MORE()
Definition regcomp.c:246