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/PrintPasses.h"
30
31 using namespace llvm;
32 using namespace ore;
33
createPrinterPass(raw_ostream & O,const std::string & Banner) const34 Pass *MachineFunctionPass::createPrinterPass(raw_ostream &O,
35 const std::string &Banner) const {
36 return createMachineFunctionPrinterPass(O, Banner);
37 }
38
runOnFunction(Function & F)39 bool MachineFunctionPass::runOnFunction(Function &F) {
40 // Do not codegen any 'available_externally' functions at all, they have
41 // definitions outside the translation unit.
42 if (F.hasAvailableExternallyLinkage())
43 return false;
44
45 MachineModuleInfo &MMI = getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
46 MachineFunction &MF = MMI.getOrCreateMachineFunction(F);
47
48 MachineFunctionProperties &MFProps = MF.getProperties();
49
50 #ifndef NDEBUG
51 if (!MFProps.verifyRequiredProperties(RequiredProperties)) {
52 errs() << "MachineFunctionProperties required by " << getPassName()
53 << " pass are not met by function " << F.getName() << ".\n"
54 << "Required properties: ";
55 RequiredProperties.print(errs());
56 errs() << "\nCurrent properties: ";
57 MFProps.print(errs());
58 errs() << "\n";
59 llvm_unreachable("MachineFunctionProperties check failed");
60 }
61 #endif
62 // Collect the MI count of the function before the pass.
63 unsigned CountBefore, CountAfter;
64
65 // Check if the user asked for size remarks.
66 bool ShouldEmitSizeRemarks =
67 F.getParent()->shouldEmitInstrCountChangedRemark();
68
69 // If we want size remarks, collect the number of MachineInstrs in our
70 // MachineFunction before the pass runs.
71 if (ShouldEmitSizeRemarks)
72 CountBefore = MF.getInstructionCount();
73
74 // For --print-changed, if the function name is a candidate, save the
75 // serialized MF to be compared later.
76 // TODO Implement --filter-passes.
77 SmallString<0> BeforeStr, AfterStr;
78 bool ShouldPrintChanged = PrintChanged != ChangePrinter::None &&
79 isFunctionInPrintList(MF.getName());
80 if (ShouldPrintChanged) {
81 raw_svector_ostream OS(BeforeStr);
82 MF.print(OS);
83 }
84
85 bool RV = runOnMachineFunction(MF);
86
87 if (ShouldEmitSizeRemarks) {
88 // We wanted size remarks. Check if there was a change to the number of
89 // MachineInstrs in the module. Emit a remark if there was a change.
90 CountAfter = MF.getInstructionCount();
91 if (CountBefore != CountAfter) {
92 MachineOptimizationRemarkEmitter MORE(MF, nullptr);
93 MORE.emit([&]() {
94 int64_t Delta = static_cast<int64_t>(CountAfter) -
95 static_cast<int64_t>(CountBefore);
96 MachineOptimizationRemarkAnalysis R("size-info", "FunctionMISizeChange",
97 MF.getFunction().getSubprogram(),
98 &MF.front());
99 R << NV("Pass", getPassName())
100 << ": Function: " << NV("Function", F.getName()) << ": "
101 << "MI Instruction count changed from "
102 << NV("MIInstrsBefore", CountBefore) << " to "
103 << NV("MIInstrsAfter", CountAfter)
104 << "; Delta: " << NV("Delta", Delta);
105 return R;
106 });
107 }
108 }
109
110 MFProps.set(SetProperties);
111 MFProps.reset(ClearedProperties);
112
113 // For --print-changed, print if the serialized MF has changed. Modes other
114 // than quiet/verbose are unimplemented and treated the same as 'quiet'.
115 if (ShouldPrintChanged) {
116 raw_svector_ostream OS(AfterStr);
117 MF.print(OS);
118 if (BeforeStr != AfterStr) {
119 StringRef Arg;
120 if (const PassInfo *PI = Pass::lookupPassInfo(getPassID()))
121 Arg = PI->getPassArgument();
122 errs() << ("*** IR Dump After " + getPassName() + " (" + Arg + ") on " +
123 MF.getName() + " ***\n" + AfterStr);
124 } else if (PrintChanged == ChangePrinter::Verbose) {
125 errs() << ("*** IR Dump After " + getPassName() + " on " + MF.getName() +
126 " omitted because no change ***\n");
127 }
128 }
129 return RV;
130 }
131
getAnalysisUsage(AnalysisUsage & AU) const132 void MachineFunctionPass::getAnalysisUsage(AnalysisUsage &AU) const {
133 AU.addRequired<MachineModuleInfoWrapperPass>();
134 AU.addPreserved<MachineModuleInfoWrapperPass>();
135
136 // MachineFunctionPass preserves all LLVM IR passes, but there's no
137 // high-level way to express this. Instead, just list a bunch of
138 // passes explicitly. This does not include setPreservesCFG,
139 // because CodeGen overloads that to mean preserving the MachineBasicBlock
140 // CFG in addition to the LLVM IR CFG.
141 AU.addPreserved<BasicAAWrapperPass>();
142 AU.addPreserved<DominanceFrontierWrapperPass>();
143 AU.addPreserved<DominatorTreeWrapperPass>();
144 AU.addPreserved<AAResultsWrapperPass>();
145 AU.addPreserved<GlobalsAAWrapperPass>();
146 AU.addPreserved<IVUsersWrapperPass>();
147 AU.addPreserved<LoopInfoWrapperPass>();
148 AU.addPreserved<MemoryDependenceWrapperPass>();
149 AU.addPreserved<ScalarEvolutionWrapperPass>();
150 AU.addPreserved<SCEVAAWrapperPass>();
151
152 FunctionPass::getAnalysisUsage(AU);
153 }
154