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/ADT/STLExtras.h"
15#include "llvm/Analysis/BasicAliasAnalysis.h"
16#include "llvm/Analysis/BranchProbabilityInfo.h"
17#include "llvm/Analysis/CycleAnalysis.h"
18#include "llvm/Analysis/DominanceFrontier.h"
19#include "llvm/Analysis/GlobalsModRef.h"
20#include "llvm/Analysis/IVUsers.h"
21#include "llvm/Analysis/LazyBlockFrequencyInfo.h"
22#include "llvm/Analysis/LazyBranchProbabilityInfo.h"
23#include "llvm/Analysis/LoopInfo.h"
24#include "llvm/Analysis/MemoryDependenceAnalysis.h"
25#include "llvm/Analysis/OptimizationRemarkEmitter.h"
26#include "llvm/Analysis/PostDominators.h"
27#include "llvm/Analysis/ScalarEvolution.h"
28#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
29#include "llvm/CodeGen/DroppedVariableStatsMIR.h"
30#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineModuleInfo.h"
32#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.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"
38#include "llvm/Support/ErrorHandling.h"
39
40using namespace llvm;
41using namespace ore;
42
43static cl::opt<bool> DroppedVarStatsMIR(
44 "dropped-variable-stats-mir", cl::Hidden,
45 cl::desc("Dump dropped debug variables stats for MIR passes"),
46 cl::init(Val: false));
47
48Pass *MachineFunctionPass::createPrinterPass(raw_ostream &O,
49 const std::string &Banner) const {
50 return createMachineFunctionPrinterPass(OS&: 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();
60 MachineFunction &MF = MMI.getOrCreateMachineFunction(F);
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(MFP: ClearedProperties);
89
90 bool RV;
91 if (DroppedVarStatsMIR) {
92 DroppedVariableStatsMIR DroppedVarStatsMF;
93 auto PassName = getPassName();
94 DroppedVarStatsMF.runBeforePass(PassID: PassName, MF: &MF);
95 RV = runOnMachineFunction(MF);
96 DroppedVarStatsMF.runAfterPass(PassID: PassName, MF: &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(RemarkBuilder: [&]() {
108 int64_t Delta = static_cast<int64_t>(CountAfter) -
109 static_cast<int64_t>(CountBefore);
110 MachineOptimizationRemarkAnalysis R("size-info", "FunctionMISizeChange",
111 MF.getFunction().getSubprogram(),
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();
134 MachineFunction &MF = MMI.getOrCreateMachineFunction(F);
135 bool SourceLocFilterEmpty = isSourceLocFilterEmpty();
136 if (!isFunctionInPrintList(FunctionName: MF.getName()))
137 return false;
138 if (!SourceLocFilterEmpty && none_of(Range&: MF, P: [](const MachineBasicBlock &MBB) {
139 return any_of(Range: MBB, P: [](const MachineInstr &MI) {
140 return isSourceLocInPrintList(Loc: MI.getDebugLoc());
141 });
142 }))
143 return false;
144 MF.print(OS);
145 return true;
146}
147
148void MachineFunctionPass::getAnalysisUsage(AnalysisUsage &AU) const {
149 AU.addRequired<MachineModuleInfoWrapperPass>();
150 AU.addPreserved<MachineModuleInfoWrapperPass>();
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.
157 AU.addPreserved<BasicAAWrapperPass>();
158 AU.addPreserved<DominanceFrontierWrapperPass>();
159 AU.addPreserved<DominatorTreeWrapperPass>();
160 AU.addPreserved<PostDominatorTreeWrapperPass>();
161 AU.addPreserved<BranchProbabilityInfoWrapperPass>();
162 AU.addPreserved<LazyBranchProbabilityInfoPass>();
163 AU.addPreserved<LazyBlockFrequencyInfoPass>();
164 AU.addPreserved<AAResultsWrapperPass>();
165 AU.addPreserved<GlobalsAAWrapperPass>();
166 AU.addPreserved<IVUsersWrapperPass>();
167 AU.addPreserved<LoopInfoWrapperPass>();
168 AU.addPreserved<CycleInfoWrapperPass>();
169 AU.addPreserved<MemoryDependenceWrapperPass>();
170 AU.addPreserved<ScalarEvolutionWrapperPass>();
171 AU.addPreserved<SCEVAAWrapperPass>();
172
173 FunctionPass::getAnalysisUsage(AU);
174}
175