LLVM 20.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/Module.h"
30#include "llvm/IR/PrintPasses.h"
31
32using namespace llvm;
33using namespace ore;
34
35Pass *MachineFunctionPass::createPrinterPass(raw_ostream &O,
36 const std::string &Banner) const {
37 return createMachineFunctionPrinterPass(O, Banner);
38}
39
40bool MachineFunctionPass::runOnFunction(Function &F) {
41 // Do not codegen any 'available_externally' functions at all, they have
42 // definitions outside the translation unit.
43 if (F.hasAvailableExternallyLinkage())
44 return false;
45
46 MachineModuleInfo &MMI = getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
48
50
51#ifndef NDEBUG
52 if (!MFProps.verifyRequiredProperties(RequiredProperties)) {
53 errs() << "MachineFunctionProperties required by " << getPassName()
54 << " pass are not met by function " << F.getName() << ".\n"
55 << "Required properties: ";
56 RequiredProperties.print(errs());
57 errs() << "\nCurrent properties: ";
58 MFProps.print(errs());
59 errs() << "\n";
60 llvm_unreachable("MachineFunctionProperties check failed");
61 }
62#endif
63 // Collect the MI count of the function before the pass.
64 unsigned CountBefore, CountAfter;
65
66 // Check if the user asked for size remarks.
67 bool ShouldEmitSizeRemarks =
68 F.getParent()->shouldEmitInstrCountChangedRemark();
69
70 // If we want size remarks, collect the number of MachineInstrs in our
71 // MachineFunction before the pass runs.
72 if (ShouldEmitSizeRemarks)
73 CountBefore = MF.getInstructionCount();
74
75 // For --print-changed, if the function name is a candidate, save the
76 // serialized MF to be compared later.
77 SmallString<0> BeforeStr, AfterStr;
78 StringRef PassID;
80 if (const PassInfo *PI = Pass::lookupPassInfo(getPassID()))
81 PassID = PI->getPassArgument();
82 }
83 const bool IsInterestingPass = isPassInPrintList(PassID);
84 const bool ShouldPrintChanged = PrintChanged != ChangePrinter::None &&
85 IsInterestingPass &&
87 if (ShouldPrintChanged) {
88 raw_svector_ostream OS(BeforeStr);
89 MF.print(OS);
90 }
91
92 MFProps.reset(ClearedProperties);
93
94 bool RV = runOnMachineFunction(MF);
95
96 if (ShouldEmitSizeRemarks) {
97 // We wanted size remarks. Check if there was a change to the number of
98 // MachineInstrs in the module. Emit a remark if there was a change.
99 CountAfter = MF.getInstructionCount();
100 if (CountBefore != CountAfter) {
102 MORE.emit([&]() {
103 int64_t Delta = static_cast<int64_t>(CountAfter) -
104 static_cast<int64_t>(CountBefore);
105 MachineOptimizationRemarkAnalysis R("size-info", "FunctionMISizeChange",
107 &MF.front());
108 R << NV("Pass", getPassName())
109 << ": Function: " << NV("Function", F.getName()) << ": "
110 << "MI Instruction count changed from "
111 << NV("MIInstrsBefore", CountBefore) << " to "
112 << NV("MIInstrsAfter", CountAfter)
113 << "; Delta: " << NV("Delta", Delta);
114 return R;
115 });
116 }
117 }
118
119 MFProps.set(SetProperties);
120
121 // For --print-changed, print if the serialized MF has changed. Modes other
122 // than quiet/verbose are unimplemented and treated the same as 'quiet'.
123 if (ShouldPrintChanged || !IsInterestingPass) {
124 if (ShouldPrintChanged) {
125 raw_svector_ostream OS(AfterStr);
126 MF.print(OS);
127 }
128 if (IsInterestingPass && BeforeStr != AfterStr) {
129 errs() << ("*** IR Dump After " + getPassName() + " (" + PassID +
130 ") on " + MF.getName() + " ***\n");
131 switch (PrintChanged) {
136 case ChangePrinter::DotCfgQuiet: // unimplemented
137 case ChangePrinter::DotCfgVerbose: // unimplemented
138 errs() << AfterStr;
139 break;
144 bool Color = llvm::is_contained(
146 PrintChanged.getValue());
147 StringRef Removed = Color ? "\033[31m-%l\033[0m\n" : "-%l\n";
148 StringRef Added = Color ? "\033[32m+%l\033[0m\n" : "+%l\n";
149 StringRef NoChange = " %l\n";
150 errs() << doSystemDiff(BeforeStr, AfterStr, Removed, Added, NoChange);
151 break;
152 }
153 }
157 PrintChanged.getValue())) {
158 const char *Reason =
159 IsInterestingPass ? " omitted because no change" : " filtered out";
160 errs() << "*** IR Dump After " << getPassName();
161 if (!PassID.empty())
162 errs() << " (" << PassID << ")";
163 errs() << " on " << MF.getName() + Reason + " ***\n";
164 }
165 }
166 return RV;
167}
168
172
173 // MachineFunctionPass preserves all LLVM IR passes, but there's no
174 // high-level way to express this. Instead, just list a bunch of
175 // passes explicitly. This does not include setPreservesCFG,
176 // because CodeGen overloads that to mean preserving the MachineBasicBlock
177 // CFG in addition to the LLVM IR CFG.
188
190}
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.
Module.h This file contains the declarations for the Module class.
#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:1874
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:51
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:147
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:691
#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:1903
#define MORE()
Definition: regcomp.c:252