1 //===-- MachineFunctionPass.cpp -------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file contains the definitions of the MachineFunctionPass members.
11 //===----------------------------------------------------------------------===//
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/ScalarEvolution.h"
21 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineModuleInfo.h"
24 #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
25 #include "llvm/CodeGen/Passes.h"
26 #include "llvm/IR/Dominators.h"
27 #include "llvm/IR/Function.h"
32 Pass
*MachineFunctionPass::createPrinterPass(raw_ostream
&O
,
33 const std::string
&Banner
) const {
34 return createMachineFunctionPrinterPass(O
, Banner
);
37 bool MachineFunctionPass::runOnFunction(Function
&F
) {
38 // Do not codegen any 'available_externally' functions at all, they have
39 // definitions outside the translation unit.
40 if (F
.hasAvailableExternallyLinkage())
43 MachineModuleInfo
&MMI
= getAnalysis
<MachineModuleInfoWrapperPass
>().getMMI();
44 MachineFunction
&MF
= MMI
.getOrCreateMachineFunction(F
);
46 MachineFunctionProperties
&MFProps
= MF
.getProperties();
49 if (!MFProps
.verifyRequiredProperties(RequiredProperties
)) {
50 errs() << "MachineFunctionProperties required by " << getPassName()
51 << " pass are not met by function " << F
.getName() << ".\n"
52 << "Required properties: ";
53 RequiredProperties
.print(errs());
54 errs() << "\nCurrent properties: ";
55 MFProps
.print(errs());
57 llvm_unreachable("MachineFunctionProperties check failed");
60 // Collect the MI count of the function before the pass.
61 unsigned CountBefore
, CountAfter
;
63 // Check if the user asked for size remarks.
64 bool ShouldEmitSizeRemarks
=
65 F
.getParent()->shouldEmitInstrCountChangedRemark();
67 // If we want size remarks, collect the number of MachineInstrs in our
68 // MachineFunction before the pass runs.
69 if (ShouldEmitSizeRemarks
)
70 CountBefore
= MF
.getInstructionCount();
72 bool RV
= runOnMachineFunction(MF
);
74 if (ShouldEmitSizeRemarks
) {
75 // We wanted size remarks. Check if there was a change to the number of
76 // MachineInstrs in the module. Emit a remark if there was a change.
77 CountAfter
= MF
.getInstructionCount();
78 if (CountBefore
!= CountAfter
) {
79 MachineOptimizationRemarkEmitter
MORE(MF
, nullptr);
81 int64_t Delta
= static_cast<int64_t>(CountAfter
) -
82 static_cast<int64_t>(CountBefore
);
83 MachineOptimizationRemarkAnalysis
R("size-info", "FunctionMISizeChange",
84 MF
.getFunction().getSubprogram(),
86 R
<< NV("Pass", getPassName())
87 << ": Function: " << NV("Function", F
.getName()) << ": "
88 << "MI Instruction count changed from "
89 << NV("MIInstrsBefore", CountBefore
) << " to "
90 << NV("MIInstrsAfter", CountAfter
)
91 << "; Delta: " << NV("Delta", Delta
);
97 MFProps
.set(SetProperties
);
98 MFProps
.reset(ClearedProperties
);
102 void MachineFunctionPass::getAnalysisUsage(AnalysisUsage
&AU
) const {
103 AU
.addRequired
<MachineModuleInfoWrapperPass
>();
104 AU
.addPreserved
<MachineModuleInfoWrapperPass
>();
106 // MachineFunctionPass preserves all LLVM IR passes, but there's no
107 // high-level way to express this. Instead, just list a bunch of
108 // passes explicitly. This does not include setPreservesCFG,
109 // because CodeGen overloads that to mean preserving the MachineBasicBlock
110 // CFG in addition to the LLVM IR CFG.
111 AU
.addPreserved
<BasicAAWrapperPass
>();
112 AU
.addPreserved
<DominanceFrontierWrapperPass
>();
113 AU
.addPreserved
<DominatorTreeWrapperPass
>();
114 AU
.addPreserved
<AAResultsWrapperPass
>();
115 AU
.addPreserved
<GlobalsAAWrapperPass
>();
116 AU
.addPreserved
<IVUsersWrapperPass
>();
117 AU
.addPreserved
<LoopInfoWrapperPass
>();
118 AU
.addPreserved
<MemoryDependenceWrapperPass
>();
119 AU
.addPreserved
<ScalarEvolutionWrapperPass
>();
120 AU
.addPreserved
<SCEVAAWrapperPass
>();
122 FunctionPass::getAnalysisUsage(AU
);