Movatterモバイル変換


[0]ホーム

URL:


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
13#include "llvm/CodeGen/MachineFunctionPass.h"
14#include "llvm/Analysis/BasicAliasAnalysis.h"
15#include "llvm/Analysis/DominanceFrontier.h"
16#include "llvm/Analysis/GlobalsModRef.h"
17#include "llvm/Analysis/IVUsers.h"
18#include "llvm/Analysis/LoopInfo.h"
19#include "llvm/Analysis/MemoryDependenceAnalysis.h"
20#include "llvm/Analysis/OptimizationRemarkEmitter.h"
21#include "llvm/Analysis/ScalarEvolution.h"
22#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineModuleInfo.h"
25#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
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 namespacellvm;
33using namespaceore;
34
35Pass *MachineFunctionPass::createPrinterPass(raw_ostream &O,
36const std::string &Banner) const{
37returncreateMachineFunctionPrinterPass(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.
43if (F.hasAvailableExternallyLinkage())
44returnfalse;
45
46MachineModuleInfo &MMI = getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
47MachineFunction &MF = MMI.getOrCreateMachineFunction(F);
48
49MachineFunctionProperties &MFProps = MF.getProperties();
50
51#ifndef NDEBUG
52if (!MFProps.verifyRequiredProperties(RequiredProperties)) {
53errs() <<"MachineFunctionProperties required by " <<getPassName()
54 <<" pass are not met by function " <<F.getName() <<".\n"
55 <<"Required properties: ";
56 RequiredProperties.print(errs());
57errs() <<"\nCurrent properties: ";
58 MFProps.print(errs());
59errs() <<"\n";
60llvm_unreachable("MachineFunctionProperties check failed");
61 }
62#endif
63// Collect the MI count of the function before the pass.
64unsigned CountBefore, CountAfter;
65
66// Check if the user asked for size remarks.
67bool ShouldEmitSizeRemarks =
68F.getParent()->shouldEmitInstrCountChangedRemark();
69
70// If we want size remarks, collect the number of MachineInstrs in our
71// MachineFunction before the pass runs.
72if (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.
77SmallString<0> BeforeStr, AfterStr;
78StringRef PassID;
79if (PrintChanged !=ChangePrinter::None) {
80if (constPassInfo *PI =Pass::lookupPassInfo(getPassID()))
81 PassID = PI->getPassArgument();
82 }
83constbool IsInterestingPass =isPassInPrintList(PassID);
84constbool ShouldPrintChanged =PrintChanged !=ChangePrinter::None &&
85 IsInterestingPass &&
86isFunctionInPrintList(MF.getName());
87if (ShouldPrintChanged) {
88raw_svector_ostreamOS(BeforeStr);
89 MF.print(OS);
90 }
91
92 MFProps.reset(ClearedProperties);
93
94bool RV =runOnMachineFunction(MF);
95
96if (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();
100if (CountBefore != CountAfter) {
101MachineOptimizationRemarkEmitterMORE(MF,nullptr);
102MORE.emit([&]() {
103 int64_t Delta =static_cast<int64_t>(CountAfter) -
104static_cast<int64_t>(CountBefore);
105MachineOptimizationRemarkAnalysisR("size-info","FunctionMISizeChange",
106 MF.getFunction().getSubprogram(),
107 &MF.front());
108R <<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);
114returnR;
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'.
123if (ShouldPrintChanged || !IsInterestingPass) {
124if (ShouldPrintChanged) {
125raw_svector_ostreamOS(AfterStr);
126 MF.print(OS);
127 }
128if (IsInterestingPass && BeforeStr != AfterStr) {
129errs() << ("*** IR Dump After " +getPassName() +" (" + PassID +
130") on " + MF.getName() +" ***\n");
131switch (PrintChanged) {
132caseChangePrinter::None:
133llvm_unreachable("");
134caseChangePrinter::Quiet:
135caseChangePrinter::Verbose:
136caseChangePrinter::DotCfgQuiet:// unimplemented
137caseChangePrinter::DotCfgVerbose:// unimplemented
138errs() << AfterStr;
139break;
140caseChangePrinter::DiffQuiet:
141caseChangePrinter::DiffVerbose:
142caseChangePrinter::ColourDiffQuiet:
143caseChangePrinter::ColourDiffVerbose: {
144bool Color =llvm::is_contained(
145 {ChangePrinter::ColourDiffQuiet,ChangePrinter::ColourDiffVerbose},
146PrintChanged.getValue());
147StringRef Removed = Color ?"\033[31m-%l\033[0m\n" :"-%l\n";
148StringRefAdded = Color ?"\033[32m+%l\033[0m\n" :"+%l\n";
149StringRef NoChange =" %l\n";
150errs() <<doSystemDiff(BeforeStr, AfterStr, Removed, Added, NoChange);
151break;
152 }
153 }
154 }elseif (llvm::is_contained({ChangePrinter::Verbose,
155ChangePrinter::DiffVerbose,
156ChangePrinter::ColourDiffVerbose},
157PrintChanged.getValue())) {
158constchar *Reason =
159 IsInterestingPass ?" omitted because no change" :" filtered out";
160errs() <<"*** IR Dump After " <<getPassName();
161if (!PassID.empty())
162errs() <<" (" << PassID <<")";
163errs() <<" on " << MF.getName() + Reason +" ***\n";
164 }
165 }
166return RV;
167}
168
169voidMachineFunctionPass::getAnalysisUsage(AnalysisUsage &AU) const{
170 AU.addRequired<MachineModuleInfoWrapperPass>();
171 AU.addPreserved<MachineModuleInfoWrapperPass>();
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.
178 AU.addPreserved<BasicAAWrapperPass>();
179 AU.addPreserved<DominanceFrontierWrapperPass>();
180 AU.addPreserved<DominatorTreeWrapperPass>();
181 AU.addPreserved<AAResultsWrapperPass>();
182 AU.addPreserved<GlobalsAAWrapperPass>();
183 AU.addPreserved<IVUsersWrapperPass>();
184 AU.addPreserved<LoopInfoWrapperPass>();
185 AU.addPreserved<MemoryDependenceWrapperPass>();
186 AU.addPreserved<ScalarEvolutionWrapperPass>();
187 AU.addPreserved<SCEVAAWrapperPass>();
188
189FunctionPass::getAnalysisUsage(AU);
190}
BasicAliasAnalysis.h
This is the interface for LLVM's primary stateless and local alias analysis.
Passes.h
DominanceFrontier.h
Dominators.h
GlobalsModRef.h
This is the interface for a simple mod/ref and alias analysis over globals.
Function.h
Module.h
Module.h This file contains the declarations for the Module class.
IVUsers.h
LoopInfo.h
F
#define F(x, y, z)
Definition:MD5.cpp:55
MachineFunctionPass.h
MachineFunction.h
MachineModuleInfo.h
MachineOptimizationRemarkEmitter.h
===- MachineOptimizationRemarkEmitter.h - Opt Diagnostics -*- C++ -*-—===//
MemoryDependenceAnalysis.h
OptimizationRemarkEmitter.h
PrintPasses.h
OS
raw_pwrite_stream & OS
Definition:SampleProfWriter.cpp:51
ScalarEvolutionAliasAnalysis.h
This is the interface for a SCEV-based alias analysis.
ScalarEvolution.h
llvm::AAResultsWrapperPass
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
Definition:AliasAnalysis.h:981
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition:PassAnalysisSupport.h:47
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition:PassAnalysisSupport.h:75
llvm::AnalysisUsage::addPreserved
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Definition:PassAnalysisSupport.h:98
llvm::BasicAAWrapperPass
Legacy wrapper pass to provide the BasicAAResult object.
Definition:BasicAliasAnalysis.h:164
llvm::DominanceFrontierWrapperPass
Definition:DominanceFrontier.h:137
llvm::DominatorTreeWrapperPass
Legacy analysis pass which computes a DominatorTree.
Definition:Dominators.h:317
llvm::Function
Definition:Function.h:63
llvm::Function::getSubprogram
DISubprogram * getSubprogram() const
Get the attached subprogram.
Definition:Metadata.cpp:1874
llvm::GlobalsAAWrapperPass
Legacy wrapper pass to provide the GlobalsAAResult object.
Definition:GlobalsModRef.h:142
llvm::IVUsersWrapperPass
Definition:IVUsers.h:163
llvm::LoopInfoWrapperPass
The legacy pass manager's analysis pass to compute loop information.
Definition:LoopInfo.h:593
llvm::MachineFunctionPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Definition:MachineFunctionPass.cpp:169
llvm::MachineFunctionPass::runOnMachineFunction
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
llvm::MachineFunctionProperties
Properties which a MachineFunction may have at a given point in time.
Definition:MachineFunction.h:137
llvm::MachineFunctionProperties::print
void print(raw_ostream &OS) const
Print the MachineFunctionProperties in human-readable form.
Definition:MachineFunction.cpp:136
llvm::MachineFunctionProperties::verifyRequiredProperties
bool verifyRequiredProperties(const MachineFunctionProperties &V) const
Definition:MachineFunction.h:235
llvm::MachineFunctionProperties::set
MachineFunctionProperties & set(Property P)
Definition:MachineFunction.h:207
llvm::MachineFunctionProperties::reset
MachineFunctionProperties & reset(Property P)
Definition:MachineFunction.h:212
llvm::MachineFunction
Definition:MachineFunction.h:267
llvm::MachineFunction::getInstructionCount
unsigned getInstructionCount() const
Return the number of MachineInstrs in this MachineFunction.
Definition:MachineFunction.h:990
llvm::MachineFunction::getName
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
Definition:MachineFunction.cpp:645
llvm::MachineFunction::getFunction
Function & getFunction()
Return the LLVM function that this machine code represents.
Definition:MachineFunction.h:704
llvm::MachineFunction::getProperties
const MachineFunctionProperties & getProperties() const
Get the function properties.
Definition:MachineFunction.h:824
llvm::MachineFunction::front
const MachineBasicBlock & front() const
Definition:MachineFunction.h:959
llvm::MachineFunction::print
void print(raw_ostream &OS, const SlotIndexes *=nullptr) const
print - Print out the MachineFunction in a format suitable for debugging to the specified stream.
Definition:MachineFunction.cpp:649
llvm::MachineModuleInfoWrapperPass
Definition:MachineModuleInfo.h:170
llvm::MachineModuleInfo
This class contains meta information specific to a module.
Definition:MachineModuleInfo.h:82
llvm::MachineModuleInfo::getOrCreateMachineFunction
MachineFunction & getOrCreateMachineFunction(Function &F)
Returns the MachineFunction constructed for the IR function F.
Definition:MachineModuleInfo.cpp:77
llvm::MachineOptimizationRemarkAnalysis
Diagnostic information for optimization analysis remarks.
Definition:MachineOptimizationRemarkEmitter.h:111
llvm::MachineOptimizationRemarkEmitter
The optimization diagnostic interface.
Definition:MachineOptimizationRemarkEmitter.h:153
llvm::MemoryDependenceWrapperPass
A wrapper analysis pass for the legacy pass manager that exposes a MemoryDepnedenceResults instance.
Definition:MemoryDependenceAnalysis.h:532
llvm::PassInfo
PassInfo class - An instance of this class exists for every pass known by the system,...
Definition:PassInfo.h:30
llvm::Pass
Pass interface - Implemented by all 'passes'.
Definition:Pass.h:94
llvm::Pass::getPassID
AnalysisID getPassID() const
getPassID - Return the PassID number that corresponds to this pass.
Definition:Pass.h:113
llvm::Pass::getAnalysisUsage
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition:Pass.cpp:98
llvm::Pass::lookupPassInfo
static const PassInfo * lookupPassInfo(const void *TI)
Definition:Pass.cpp:192
llvm::Pass::getPassName
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition:Pass.cpp:81
llvm::SCEVAAWrapperPass
Legacy wrapper pass to provide the SCEVAAResult object.
Definition:ScalarEvolutionAliasAnalysis.h:56
llvm::ScalarEvolutionWrapperPass
Definition:ScalarEvolution.h:2352
llvm::SmallString
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition:SmallString.h:26
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition:StringRef.h:51
llvm::StringRef::empty
constexpr bool empty() const
empty - Check if the string is empty.
Definition:StringRef.h:147
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition:raw_ostream.h:52
llvm::raw_svector_ostream
A raw_ostream that writes to an SmallVector or SmallString.
Definition:raw_ostream.h:691
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition:ErrorHandling.h:143
llvm::RISCVFenceField::R
@ R
Definition:RISCVBaseInfo.h:373
llvm::logicalview::LVComparePass::Added
@ Added
llvm::ore::NV
DiagnosticInfoOptimizationBase::Argument NV
Definition:OptimizationRemarkEmitter.h:135
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::ChangePrinter::Quiet
@ Quiet
llvm::ChangePrinter::DiffVerbose
@ DiffVerbose
llvm::ChangePrinter::DiffQuiet
@ DiffQuiet
llvm::ChangePrinter::DotCfgQuiet
@ DotCfgQuiet
llvm::ChangePrinter::None
@ None
llvm::ChangePrinter::DotCfgVerbose
@ DotCfgVerbose
llvm::ChangePrinter::ColourDiffVerbose
@ ColourDiffVerbose
llvm::ChangePrinter::Verbose
@ Verbose
llvm::ChangePrinter::ColourDiffQuiet
@ ColourDiffQuiet
llvm::PrintChanged
cl::opt< ChangePrinter > PrintChanged
llvm::isFunctionInPrintList
bool isFunctionInPrintList(StringRef FunctionName)
Definition:PrintPasses.cpp:160
llvm::isPassInPrintList
bool isPassInPrintList(StringRef PassName)
Definition:PrintPasses.cpp:152
llvm::errs
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Definition:raw_ostream.cpp:907
llvm::createMachineFunctionPrinterPass
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::doSystemDiff
std::string doSystemDiff(StringRef Before, StringRef After, StringRef OldLineFormat, StringRef NewLineFormat, StringRef UnchangedLineFormat)
Definition:PrintPasses.cpp:215
llvm::is_contained
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition:STLExtras.h:1903
MORE
#define MORE()
Definition:regcomp.c:252

Generated on Thu Jul 17 2025 11:31:03 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp