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