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/BranchProbabilityInfo.h"
16#include "llvm/Analysis/DominanceFrontier.h"
17#include "llvm/Analysis/GlobalsModRef.h"
18#include "llvm/Analysis/IVUsers.h"
19#include "llvm/Analysis/LazyBlockFrequencyInfo.h"
20#include "llvm/Analysis/LazyBranchProbabilityInfo.h"
21#include "llvm/Analysis/LoopInfo.h"
22#include "llvm/Analysis/MemoryDependenceAnalysis.h"
23#include "llvm/Analysis/OptimizationRemarkEmitter.h"
24#include "llvm/Analysis/PostDominators.h"
25#include "llvm/Analysis/ScalarEvolution.h"
26#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
27#include "llvm/CodeGen/DroppedVariableStatsMIR.h"
28#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineModuleInfo.h"
30#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
31#include "llvm/CodeGen/Passes.h"
32#include "llvm/IR/Dominators.h"
33#include "llvm/IR/Function.h"
34#include "llvm/IR/Module.h"
35#include "llvm/Support/ErrorHandling.h"
36
37using namespace llvm;
38using namespace ore;
39
40static cl::opt<bool> DroppedVarStatsMIR(
41 "dropped-variable-stats-mir", cl::Hidden,
42 cl::desc("Dump dropped debug variables stats for MIR passes"),
43 cl::init(Val: false));
44
45Pass *MachineFunctionPass::createPrinterPass(raw_ostream &O,
46 const std::string &Banner) const {
47 return createMachineFunctionPrinterPass(OS&: O, Banner);
48}
49
50bool MachineFunctionPass::runOnFunction(Function &F) {
51 // Do not codegen any 'available_externally' functions at all, they have
52 // definitions outside the translation unit.
53 if (F.hasAvailableExternallyLinkage())
54 return false;
55
56 MachineModuleInfo &MMI = getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
57 MachineFunction &MF = MMI.getOrCreateMachineFunction(F);
58
59 MachineFunctionProperties &MFProps = MF.getProperties();
60
61#ifndef NDEBUG
62 if (!MFProps.verifyRequiredProperties(RequiredProperties)) {
63 errs() << "MachineFunctionProperties required by " << getPassName()
64 << " pass are not met by function " << F.getName() << ".\n"
65 << "Required properties: ";
66 RequiredProperties.print(errs());
67 errs() << "\nCurrent properties: ";
68 MFProps.print(errs());
69 errs() << "\n";
70 reportFatalUsageError("MachineFunctionProperties check failed");
71 }
72#endif
73 // Collect the MI count of the function before the pass.
74 unsigned CountBefore, CountAfter;
75
76 // Check if the user asked for size remarks.
77 bool ShouldEmitSizeRemarks =
78 F.getParent()->shouldEmitInstrCountChangedRemark();
79
80 // If we want size remarks, collect the number of MachineInstrs in our
81 // MachineFunction before the pass runs.
82 if (ShouldEmitSizeRemarks)
83 CountBefore = MF.getInstructionCount();
84
85 MFProps.reset(MFP: ClearedProperties);
86
87 bool RV;
88 if (DroppedVarStatsMIR) {
89 DroppedVariableStatsMIR DroppedVarStatsMF;
90 auto PassName = getPassName();
91 DroppedVarStatsMF.runBeforePass(PassID: PassName, MF: &MF);
92 RV = runOnMachineFunction(MF);
93 DroppedVarStatsMF.runAfterPass(PassID: PassName, MF: &MF);
94 } else {
95 RV = runOnMachineFunction(MF);
96 }
97
98 if (ShouldEmitSizeRemarks) {
99 // We wanted size remarks. Check if there was a change to the number of
100 // MachineInstrs in the module. Emit a remark if there was a change.
101 CountAfter = MF.getInstructionCount();
102 if (CountBefore != CountAfter) {
103 MachineOptimizationRemarkEmitter MORE(MF, nullptr);
104 MORE.emit(RemarkBuilder: [&]() {
105 int64_t Delta = static_cast<int64_t>(CountAfter) -
106 static_cast<int64_t>(CountBefore);
107 MachineOptimizationRemarkAnalysis R("size-info", "FunctionMISizeChange",
108 MF.getFunction().getSubprogram(),
109 &MF.front());
110 R << NV("Pass", getPassName())
111 << ": Function: " << NV("Function", F.getName()) << ": "
112 << "MI Instruction count changed from "
113 << NV("MIInstrsBefore", CountBefore) << " to "
114 << NV("MIInstrsAfter", CountAfter)
115 << "; Delta: " << NV("Delta", Delta);
116 return R;
117 });
118 }
119 }
120
121 MFProps.set(SetProperties);
122
123 return RV;
124}
125
126bool MachineFunctionPass::printIRUnit(raw_ostream &OS, Function &F) {
127 // available_externally functions are not codegen'd (see runOnFunction).
128 if (F.hasAvailableExternallyLinkage())
129 return false;
130 MachineModuleInfo &MMI = getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
131 MMI.getOrCreateMachineFunction(F).print(OS);
132 return true;
133}
134
135void MachineFunctionPass::getAnalysisUsage(AnalysisUsage &AU) const {
136 AU.addRequired<MachineModuleInfoWrapperPass>();
137 AU.addPreserved<MachineModuleInfoWrapperPass>();
138
139 // MachineFunctionPass preserves all LLVM IR passes, but there's no
140 // high-level way to express this. Instead, just list a bunch of
141 // passes explicitly. This does not include setPreservesCFG,
142 // because CodeGen overloads that to mean preserving the MachineBasicBlock
143 // CFG in addition to the LLVM IR CFG.
144 AU.addPreserved<BasicAAWrapperPass>();
145 AU.addPreserved<DominanceFrontierWrapperPass>();
146 AU.addPreserved<DominatorTreeWrapperPass>();
147 AU.addPreserved<PostDominatorTreeWrapperPass>();
148 AU.addPreserved<BranchProbabilityInfoWrapperPass>();
149 AU.addPreserved<LazyBranchProbabilityInfoPass>();
150 AU.addPreserved<LazyBlockFrequencyInfoPass>();
151 AU.addPreserved<AAResultsWrapperPass>();
152 AU.addPreserved<GlobalsAAWrapperPass>();
153 AU.addPreserved<IVUsersWrapperPass>();
154 AU.addPreserved<LoopInfoWrapperPass>();
155 AU.addPreserved<MemoryDependenceWrapperPass>();
156 AU.addPreserved<ScalarEvolutionWrapperPass>();
157 AU.addPreserved<SCEVAAWrapperPass>();
158
159 FunctionPass::getAnalysisUsage(AU);
160}
161