LLVM 24.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
14#include "llvm/ADT/STLExtras.h"
33#include "llvm/CodeGen/Passes.h"
34#include "llvm/IR/Dominators.h"
35#include "llvm/IR/Function.h"
36#include "llvm/IR/Module.h"
37#include "llvm/IR/PrintPasses.h"
39
40using namespace llvm;
41using namespace ore;
42
44 "dropped-variable-stats-mir", cl::Hidden,
45 cl::desc("Dump dropped debug variables stats for MIR passes"),
46 cl::init(false));
47
48Pass *MachineFunctionPass::createPrinterPass(raw_ostream &O,
49 const std::string &Banner) const {
50 return createMachineFunctionPrinterPass(O, Banner);
51}
52
53bool MachineFunctionPass::runOnFunction(Function &F) {
54 // Do not codegen any 'available_externally' functions at all, they have
55 // definitions outside the translation unit.
56 if (F.hasAvailableExternallyLinkage())
57 return false;
58
59 MachineModuleInfo &MMI = getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
61
62 MachineFunctionProperties &MFProps = MF.getProperties();
63
64#ifndef NDEBUG
65 if (!MFProps.verifyRequiredProperties(RequiredProperties)) {
66 errs() << "MachineFunctionProperties required by " << getPassName()
67 << " pass are not met by function " << F.getName() << ".\n"
68 << "Required properties: ";
69 RequiredProperties.print(errs());
70 errs() << "\nCurrent properties: ";
71 MFProps.print(errs());
72 errs() << "\n";
73 reportFatalUsageError("MachineFunctionProperties check failed");
74 }
75#endif
76 // Collect the MI count of the function before the pass.
77 unsigned CountBefore, CountAfter;
78
79 // Check if the user asked for size remarks.
80 bool ShouldEmitSizeRemarks =
81 F.getParent()->shouldEmitInstrCountChangedRemark();
82
83 // If we want size remarks, collect the number of MachineInstrs in our
84 // MachineFunction before the pass runs.
85 if (ShouldEmitSizeRemarks)
86 CountBefore = MF.getInstructionCount();
87
88 MFProps.reset(ClearedProperties);
89
90 bool RV;
92 DroppedVariableStatsMIR DroppedVarStatsMF;
93 auto PassName = getPassName();
94 DroppedVarStatsMF.runBeforePass(PassName, &MF);
95 RV = runOnMachineFunction(MF);
96 DroppedVarStatsMF.runAfterPass(PassName, &MF);
97 } else {
98 RV = runOnMachineFunction(MF);
99 }
100
101 if (ShouldEmitSizeRemarks) {
102 // We wanted size remarks. Check if there was a change to the number of
103 // MachineInstrs in the module. Emit a remark if there was a change.
104 CountAfter = MF.getInstructionCount();
105 if (CountBefore != CountAfter) {
106 MachineOptimizationRemarkEmitter MORE(MF, nullptr);
107 MORE.emit([&]() {
108 int64_t Delta = static_cast<int64_t>(CountAfter) -
109 static_cast<int64_t>(CountBefore);
110 MachineOptimizationRemarkAnalysis R("size-info", "FunctionMISizeChange",
112 &MF.front());
113 R << NV("Pass", getPassName())
114 << ": Function: " << NV("Function", F.getName()) << ": "
115 << "MI Instruction count changed from "
116 << NV("MIInstrsBefore", CountBefore) << " to "
117 << NV("MIInstrsAfter", CountAfter)
118 << "; Delta: " << NV("Delta", Delta);
119 return R;
120 });
121 }
122 }
123
124 MFProps.set(SetProperties);
125
126 return RV;
127}
128
129bool MachineFunctionPass::printIRUnit(raw_ostream &OS, Function &F) {
130 // available_externally functions are not codegen'd (see runOnFunction).
131 if (F.hasAvailableExternallyLinkage())
132 return false;
133 MachineModuleInfo &MMI = getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
135 bool SourceLocFilterEmpty = isSourceLocFilterEmpty();
137 return false;
138 if (!SourceLocFilterEmpty && none_of(MF, [](const MachineBasicBlock &MBB) {
139 return any_of(MBB, [](const MachineInstr &MI) {
140 return isSourceLocInPrintList(MI.getDebugLoc());
141 });
142 }))
143 return false;
144 MF.print(OS);
145 return true;
146}
147
151
152 // MachineFunctionPass preserves all LLVM IR passes, but there's no
153 // high-level way to express this. Instead, just list a bunch of
154 // passes explicitly. This does not include setPreservesCFG,
155 // because CodeGen overloads that to mean preserving the MachineBasicBlock
156 // CFG in addition to the LLVM IR CFG.
172
174}
MachineBasicBlock & MBB
This is the interface for LLVM's primary stateless and local alias analysis.
This file declares an analysis pass that computes CycleInfo for LLVM IR, specialized from GenericCycl...
===- DroppedVariableStatsMIR.h - Opt Diagnostics -*- C++ -*----------—===//
This is the interface for a simple mod/ref and alias analysis over globals.
IRTranslator LLVM IR MI
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 file contains some templates that are useful if you are working with the STL at all.
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 CycleInfo.
Legacy analysis pass which computes a DominatorTree.
Definition Dominators.h:277
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:619
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 ...
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition Pass.cpp:113
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition Pass.cpp:86
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
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.
LLVM_ABI bool isSourceLocInPrintList(const DebugLoc &Loc)
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1746
LLVM_ABI bool isFunctionInPrintList(StringRef FunctionName)
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1753
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 bool isSourceLocFilterEmpty()
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:177
#define MORE()
Definition regcomp.c:246