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