[AMDGPU][AsmParser][NFC] Translate parsed MIMG instructions to MCInsts automatically.
[llvm-project.git] / llvm / lib / CodeGen / MachineFunctionPass.cpp
blob3a1e1720be9c627fe26f0932bd2145290a6553fb
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.
11 //===----------------------------------------------------------------------===//
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/PrintPasses.h"
31 using namespace llvm;
32 using namespace ore;
34 Pass *MachineFunctionPass::createPrinterPass(raw_ostream &O,
35 const std::string &Banner) const {
36 return createMachineFunctionPrinterPass(O, Banner);
39 bool 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;
45 MachineModuleInfo &MMI = getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
46 MachineFunction &MF = MMI.getOrCreateMachineFunction(F);
48 MachineFunctionProperties &MFProps = MF.getProperties();
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");
61 #endif
62 // Collect the MI count of the function before the pass.
63 unsigned CountBefore, CountAfter;
65 // Check if the user asked for size remarks.
66 bool ShouldEmitSizeRemarks =
67 F.getParent()->shouldEmitInstrCountChangedRemark();
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();
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;
78 if (PrintChanged != ChangePrinter::None) {
79 if (const PassInfo *PI = Pass::lookupPassInfo(getPassID()))
80 PassID = PI->getPassArgument();
82 const bool IsInterestingPass = isPassInPrintList(PassID);
83 const bool ShouldPrintChanged = PrintChanged != ChangePrinter::None &&
84 IsInterestingPass &&
85 isFunctionInPrintList(MF.getName());
86 if (ShouldPrintChanged) {
87 raw_svector_ostream OS(BeforeStr);
88 MF.print(OS);
91 bool RV = runOnMachineFunction(MF);
93 if (ShouldEmitSizeRemarks) {
94 // We wanted size remarks. Check if there was a change to the number of
95 // MachineInstrs in the module. Emit a remark if there was a change.
96 CountAfter = MF.getInstructionCount();
97 if (CountBefore != CountAfter) {
98 MachineOptimizationRemarkEmitter MORE(MF, nullptr);
99 MORE.emit([&]() {
100 int64_t Delta = static_cast<int64_t>(CountAfter) -
101 static_cast<int64_t>(CountBefore);
102 MachineOptimizationRemarkAnalysis R("size-info", "FunctionMISizeChange",
103 MF.getFunction().getSubprogram(),
104 &MF.front());
105 R << NV("Pass", getPassName())
106 << ": Function: " << NV("Function", F.getName()) << ": "
107 << "MI Instruction count changed from "
108 << NV("MIInstrsBefore", CountBefore) << " to "
109 << NV("MIInstrsAfter", CountAfter)
110 << "; Delta: " << NV("Delta", Delta);
111 return R;
116 MFProps.set(SetProperties);
117 MFProps.reset(ClearedProperties);
119 // For --print-changed, print if the serialized MF has changed. Modes other
120 // than quiet/verbose are unimplemented and treated the same as 'quiet'.
121 if (ShouldPrintChanged || !IsInterestingPass) {
122 if (ShouldPrintChanged) {
123 raw_svector_ostream OS(AfterStr);
124 MF.print(OS);
126 if (IsInterestingPass && BeforeStr != AfterStr) {
127 errs() << ("*** IR Dump After " + getPassName() + " (" + PassID +
128 ") on " + MF.getName() + " ***\n");
129 switch (PrintChanged) {
130 case ChangePrinter::None:
131 llvm_unreachable("");
132 case ChangePrinter::Quiet:
133 case ChangePrinter::Verbose:
134 case ChangePrinter::DotCfgQuiet: // unimplemented
135 case ChangePrinter::DotCfgVerbose: // unimplemented
136 errs() << AfterStr;
137 break;
138 case ChangePrinter::DiffQuiet:
139 case ChangePrinter::DiffVerbose:
140 case ChangePrinter::ColourDiffQuiet:
141 case ChangePrinter::ColourDiffVerbose: {
142 bool Color = llvm::is_contained(
143 {ChangePrinter::ColourDiffQuiet, ChangePrinter::ColourDiffVerbose},
144 PrintChanged.getValue());
145 StringRef Removed = Color ? "\033[31m-%l\033[0m\n" : "-%l\n";
146 StringRef Added = Color ? "\033[32m+%l\033[0m\n" : "+%l\n";
147 StringRef NoChange = " %l\n";
148 errs() << doSystemDiff(BeforeStr, AfterStr, Removed, Added, NoChange);
149 break;
152 } else if (llvm::is_contained({ChangePrinter::Verbose,
153 ChangePrinter::DiffVerbose,
154 ChangePrinter::ColourDiffVerbose},
155 PrintChanged.getValue())) {
156 const char *Reason =
157 IsInterestingPass ? " omitted because no change" : " filtered out";
158 errs() << "*** IR Dump After " << getPassName();
159 if (!PassID.empty())
160 errs() << " (" << PassID << ")";
161 errs() << " on " << MF.getName() + Reason + " ***\n";
164 return RV;
167 void MachineFunctionPass::getAnalysisUsage(AnalysisUsage &AU) const {
168 AU.addRequired<MachineModuleInfoWrapperPass>();
169 AU.addPreserved<MachineModuleInfoWrapperPass>();
171 // MachineFunctionPass preserves all LLVM IR passes, but there's no
172 // high-level way to express this. Instead, just list a bunch of
173 // passes explicitly. This does not include setPreservesCFG,
174 // because CodeGen overloads that to mean preserving the MachineBasicBlock
175 // CFG in addition to the LLVM IR CFG.
176 AU.addPreserved<BasicAAWrapperPass>();
177 AU.addPreserved<DominanceFrontierWrapperPass>();
178 AU.addPreserved<DominatorTreeWrapperPass>();
179 AU.addPreserved<AAResultsWrapperPass>();
180 AU.addPreserved<GlobalsAAWrapperPass>();
181 AU.addPreserved<IVUsersWrapperPass>();
182 AU.addPreserved<LoopInfoWrapperPass>();
183 AU.addPreserved<MemoryDependenceWrapperPass>();
184 AU.addPreserved<ScalarEvolutionWrapperPass>();
185 AU.addPreserved<SCEVAAWrapperPass>();
187 FunctionPass::getAnalysisUsage(AU);