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