1*0b57cec5SDimitry Andric //===-- MachineFunctionPass.cpp -------------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This file contains the definitions of the MachineFunctionPass members.
10*0b57cec5SDimitry Andric //
11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
12*0b57cec5SDimitry Andric 
13*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
14*0b57cec5SDimitry Andric #include "llvm/Analysis/BasicAliasAnalysis.h"
15*0b57cec5SDimitry Andric #include "llvm/Analysis/DominanceFrontier.h"
16*0b57cec5SDimitry Andric #include "llvm/Analysis/GlobalsModRef.h"
17*0b57cec5SDimitry Andric #include "llvm/Analysis/IVUsers.h"
18*0b57cec5SDimitry Andric #include "llvm/Analysis/LoopInfo.h"
19*0b57cec5SDimitry Andric #include "llvm/Analysis/MemoryDependenceAnalysis.h"
20*0b57cec5SDimitry Andric #include "llvm/Analysis/ScalarEvolution.h"
21*0b57cec5SDimitry Andric #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
22*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
23*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h"
24*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
25*0b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h"
26*0b57cec5SDimitry Andric #include "llvm/IR/Dominators.h"
27*0b57cec5SDimitry Andric #include "llvm/IR/Function.h"
28*0b57cec5SDimitry Andric 
29*0b57cec5SDimitry Andric using namespace llvm;
30*0b57cec5SDimitry Andric using namespace ore;
31*0b57cec5SDimitry Andric 
createPrinterPass(raw_ostream & O,const std::string & Banner) const32*0b57cec5SDimitry Andric Pass *MachineFunctionPass::createPrinterPass(raw_ostream &O,
33*0b57cec5SDimitry Andric                                              const std::string &Banner) const {
34*0b57cec5SDimitry Andric   return createMachineFunctionPrinterPass(O, Banner);
35*0b57cec5SDimitry Andric }
36*0b57cec5SDimitry Andric 
runOnFunction(Function & F)37*0b57cec5SDimitry Andric bool MachineFunctionPass::runOnFunction(Function &F) {
38*0b57cec5SDimitry Andric   // Do not codegen any 'available_externally' functions at all, they have
39*0b57cec5SDimitry Andric   // definitions outside the translation unit.
40*0b57cec5SDimitry Andric   if (F.hasAvailableExternallyLinkage())
41*0b57cec5SDimitry Andric     return false;
42*0b57cec5SDimitry Andric 
438bcb0991SDimitry Andric   MachineModuleInfo &MMI = getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
44*0b57cec5SDimitry Andric   MachineFunction &MF = MMI.getOrCreateMachineFunction(F);
45*0b57cec5SDimitry Andric 
46*0b57cec5SDimitry Andric   MachineFunctionProperties &MFProps = MF.getProperties();
47*0b57cec5SDimitry Andric 
48*0b57cec5SDimitry Andric #ifndef NDEBUG
49*0b57cec5SDimitry Andric   if (!MFProps.verifyRequiredProperties(RequiredProperties)) {
50*0b57cec5SDimitry Andric     errs() << "MachineFunctionProperties required by " << getPassName()
51*0b57cec5SDimitry Andric            << " pass are not met by function " << F.getName() << ".\n"
52*0b57cec5SDimitry Andric            << "Required properties: ";
53*0b57cec5SDimitry Andric     RequiredProperties.print(errs());
54*0b57cec5SDimitry Andric     errs() << "\nCurrent properties: ";
55*0b57cec5SDimitry Andric     MFProps.print(errs());
56*0b57cec5SDimitry Andric     errs() << "\n";
57*0b57cec5SDimitry Andric     llvm_unreachable("MachineFunctionProperties check failed");
58*0b57cec5SDimitry Andric   }
59*0b57cec5SDimitry Andric #endif
60*0b57cec5SDimitry Andric   // Collect the MI count of the function before the pass.
61*0b57cec5SDimitry Andric   unsigned CountBefore, CountAfter;
62*0b57cec5SDimitry Andric 
63*0b57cec5SDimitry Andric   // Check if the user asked for size remarks.
64*0b57cec5SDimitry Andric   bool ShouldEmitSizeRemarks =
65*0b57cec5SDimitry Andric       F.getParent()->shouldEmitInstrCountChangedRemark();
66*0b57cec5SDimitry Andric 
67*0b57cec5SDimitry Andric   // If we want size remarks, collect the number of MachineInstrs in our
68*0b57cec5SDimitry Andric   // MachineFunction before the pass runs.
69*0b57cec5SDimitry Andric   if (ShouldEmitSizeRemarks)
70*0b57cec5SDimitry Andric     CountBefore = MF.getInstructionCount();
71*0b57cec5SDimitry Andric 
72*0b57cec5SDimitry Andric   bool RV = runOnMachineFunction(MF);
73*0b57cec5SDimitry Andric 
74*0b57cec5SDimitry Andric   if (ShouldEmitSizeRemarks) {
75*0b57cec5SDimitry Andric     // We wanted size remarks. Check if there was a change to the number of
76*0b57cec5SDimitry Andric     // MachineInstrs in the module. Emit a remark if there was a change.
77*0b57cec5SDimitry Andric     CountAfter = MF.getInstructionCount();
78*0b57cec5SDimitry Andric     if (CountBefore != CountAfter) {
79*0b57cec5SDimitry Andric       MachineOptimizationRemarkEmitter MORE(MF, nullptr);
80*0b57cec5SDimitry Andric       MORE.emit([&]() {
81*0b57cec5SDimitry Andric         int64_t Delta = static_cast<int64_t>(CountAfter) -
82*0b57cec5SDimitry Andric                         static_cast<int64_t>(CountBefore);
83*0b57cec5SDimitry Andric         MachineOptimizationRemarkAnalysis R("size-info", "FunctionMISizeChange",
84*0b57cec5SDimitry Andric                                             MF.getFunction().getSubprogram(),
85*0b57cec5SDimitry Andric                                             &MF.front());
86*0b57cec5SDimitry Andric         R << NV("Pass", getPassName())
87*0b57cec5SDimitry Andric           << ": Function: " << NV("Function", F.getName()) << ": "
88*0b57cec5SDimitry Andric           << "MI Instruction count changed from "
89*0b57cec5SDimitry Andric           << NV("MIInstrsBefore", CountBefore) << " to "
90*0b57cec5SDimitry Andric           << NV("MIInstrsAfter", CountAfter)
91*0b57cec5SDimitry Andric           << "; Delta: " << NV("Delta", Delta);
92*0b57cec5SDimitry Andric         return R;
93*0b57cec5SDimitry Andric       });
94*0b57cec5SDimitry Andric     }
95*0b57cec5SDimitry Andric   }
96*0b57cec5SDimitry Andric 
97*0b57cec5SDimitry Andric   MFProps.set(SetProperties);
98*0b57cec5SDimitry Andric   MFProps.reset(ClearedProperties);
99*0b57cec5SDimitry Andric   return RV;
100*0b57cec5SDimitry Andric }
101*0b57cec5SDimitry Andric 
getAnalysisUsage(AnalysisUsage & AU) const102*0b57cec5SDimitry Andric void MachineFunctionPass::getAnalysisUsage(AnalysisUsage &AU) const {
1038bcb0991SDimitry Andric   AU.addRequired<MachineModuleInfoWrapperPass>();
1048bcb0991SDimitry Andric   AU.addPreserved<MachineModuleInfoWrapperPass>();
105*0b57cec5SDimitry Andric 
106*0b57cec5SDimitry Andric   // MachineFunctionPass preserves all LLVM IR passes, but there's no
107*0b57cec5SDimitry Andric   // high-level way to express this. Instead, just list a bunch of
108*0b57cec5SDimitry Andric   // passes explicitly. This does not include setPreservesCFG,
109*0b57cec5SDimitry Andric   // because CodeGen overloads that to mean preserving the MachineBasicBlock
110*0b57cec5SDimitry Andric   // CFG in addition to the LLVM IR CFG.
111*0b57cec5SDimitry Andric   AU.addPreserved<BasicAAWrapperPass>();
112*0b57cec5SDimitry Andric   AU.addPreserved<DominanceFrontierWrapperPass>();
113*0b57cec5SDimitry Andric   AU.addPreserved<DominatorTreeWrapperPass>();
114*0b57cec5SDimitry Andric   AU.addPreserved<AAResultsWrapperPass>();
115*0b57cec5SDimitry Andric   AU.addPreserved<GlobalsAAWrapperPass>();
116*0b57cec5SDimitry Andric   AU.addPreserved<IVUsersWrapperPass>();
117*0b57cec5SDimitry Andric   AU.addPreserved<LoopInfoWrapperPass>();
118*0b57cec5SDimitry Andric   AU.addPreserved<MemoryDependenceWrapperPass>();
119*0b57cec5SDimitry Andric   AU.addPreserved<ScalarEvolutionWrapperPass>();
120*0b57cec5SDimitry Andric   AU.addPreserved<SCEVAAWrapperPass>();
121*0b57cec5SDimitry Andric 
122*0b57cec5SDimitry Andric   FunctionPass::getAnalysisUsage(AU);
123*0b57cec5SDimitry Andric }
124