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 | |
32 | using namespace llvm; |
33 | using namespace ore; |
34 | |
35 | Pass *MachineFunctionPass::createPrinterPass(raw_ostream &O, |
36 | const std::string &Banner) const { |
37 | return createMachineFunctionPrinterPass(OS&: O, Banner); |
38 | } |
39 | |
40 | bool 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(); |
47 | MachineFunction &MF = MMI.getOrCreateMachineFunction(F); |
48 | |
49 | MachineFunctionProperties &MFProps = MF.getProperties(); |
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 = |
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; |
79 | if (PrintChanged != ChangePrinter::None) { |
80 | if (const PassInfo *PI = Pass::lookupPassInfo(TI: getPassID())) |
81 | PassID = PI->getPassArgument(); |
82 | } |
83 | const bool IsInterestingPass = isPassInPrintList(PassName: PassID); |
84 | const bool ShouldPrintChanged = PrintChanged != ChangePrinter::None && |
85 | IsInterestingPass && |
86 | isFunctionInPrintList(FunctionName: MF.getName()); |
87 | if (ShouldPrintChanged) { |
88 | raw_svector_ostream OS(BeforeStr); |
89 | MF.print(OS); |
90 | } |
91 | |
92 | MFProps.reset(MFP: 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) { |
101 | MachineOptimizationRemarkEmitter MORE(MF, nullptr); |
102 | MORE.emit(RemarkBuilder: [&]() { |
103 | int64_t Delta = static_cast<int64_t>(CountAfter) - |
104 | static_cast<int64_t>(CountBefore); |
105 | MachineOptimizationRemarkAnalysis R("size-info" , "FunctionMISizeChange" , |
106 | MF.getFunction().getSubprogram(), |
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) { |
132 | case ChangePrinter::None: |
133 | llvm_unreachable("" ); |
134 | case ChangePrinter::Quiet: |
135 | case ChangePrinter::Verbose: |
136 | case ChangePrinter::DotCfgQuiet: // unimplemented |
137 | case ChangePrinter::DotCfgVerbose: // unimplemented |
138 | errs() << AfterStr; |
139 | break; |
140 | case ChangePrinter::DiffQuiet: |
141 | case ChangePrinter::DiffVerbose: |
142 | case ChangePrinter::ColourDiffQuiet: |
143 | case ChangePrinter::ColourDiffVerbose: { |
144 | bool Color = llvm::is_contained( |
145 | Set: {ChangePrinter::ColourDiffQuiet, ChangePrinter::ColourDiffVerbose}, |
146 | Element: 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(Before: BeforeStr, After: AfterStr, OldLineFormat: Removed, NewLineFormat: Added, UnchangedLineFormat: NoChange); |
151 | break; |
152 | } |
153 | } |
154 | } else if (llvm::is_contained(Set: {ChangePrinter::Verbose, |
155 | ChangePrinter::DiffVerbose, |
156 | ChangePrinter::ColourDiffVerbose}, |
157 | Element: 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 | |
169 | void MachineFunctionPass::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 | |
189 | FunctionPass::getAnalysisUsage(AU); |
190 | } |
191 | |