blob: 0da4cf3fc90c85e6c03af5e216b6811c5d38f967 [file] [log] [blame]
Dan Gohman5ea74d52009-07-31 18:16:33 +00001//===-- MachineFunctionPass.cpp -------------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Dan Gohman5ea74d52009-07-31 18:16:33 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the definitions of the MachineFunctionPass members.
10//
11//===----------------------------------------------------------------------===//
12
Dan Gohman5ea74d52009-07-31 18:16:33 +000013#include "llvm/CodeGen/MachineFunctionPass.h"
Chandler Carruthb81dfa62015-01-28 04:57:56 +000014#include "llvm/Analysis/AliasAnalysis.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000015#include "llvm/Analysis/BasicAliasAnalysis.h"
Chandler Carruthb81dfa62015-01-28 04:57:56 +000016#include "llvm/Analysis/DominanceFrontier.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000017#include "llvm/Analysis/GlobalsModRef.h"
Chandler Carruthb81dfa62015-01-28 04:57:56 +000018#include "llvm/Analysis/IVUsers.h"
19#include "llvm/Analysis/LoopInfo.h"
Chandler Carruthb81dfa62015-01-28 04:57:56 +000020#include "llvm/Analysis/MemoryDependenceAnalysis.h"
21#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000022#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Derek Schuffad154c82016-03-28 17:05:30 +000023#include "llvm/CodeGen/MachineFunction.h"
Matthias Braun733fe362016-08-24 01:52:46 +000024#include "llvm/CodeGen/MachineModuleInfo.h"
Jessica Paquette54fbfae2018-09-10 22:24:10 +000025#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
David Greene9b063df2010-04-02 23:17:14 +000026#include "llvm/CodeGen/Passes.h"
Chandler Carruthb81dfa62015-01-28 04:57:56 +000027#include "llvm/IR/Dominators.h"
28#include "llvm/IR/Function.h"
Derek Schuffad154c82016-03-28 17:05:30 +000029
Dan Gohman5ea74d52009-07-31 18:16:33 +000030using namespace llvm;
Jessica Paquette54fbfae2018-09-10 22:24:10 +000031using namespace ore;
Dan Gohman5ea74d52009-07-31 18:16:33 +000032
David Greene9b063df2010-04-02 23:17:14 +000033Pass *MachineFunctionPass::createPrinterPass(raw_ostream &O,
34 const std::string &Banner) const {
35 return createMachineFunctionPrinterPass(O, Banner);
36}
37
Dan Gohman5ea74d52009-07-31 18:16:33 +000038bool MachineFunctionPass::runOnFunction(Function &F) {
39 // Do not codegen any 'available_externally' functions at all, they have
40 // definitions outside the translation unit.
Serge Pavloved5eb932017-01-15 10:23:18 +000041 if (F.hasAvailableExternallyLinkage())
Dan Gohman5ea74d52009-07-31 18:16:33 +000042 return false;
43
Matthias Braun733fe362016-08-24 01:52:46 +000044 MachineModuleInfo &MMI = getAnalysis<MachineModuleInfo>();
Matthias Braun7bda1952017-06-06 00:44:35 +000045 MachineFunction &MF = MMI.getOrCreateMachineFunction(F);
Matthias Braun733fe362016-08-24 01:52:46 +000046
Derek Schuffad154c82016-03-28 17:05:30 +000047 MachineFunctionProperties &MFProps = MF.getProperties();
48
Derek Schuff07636cd2016-03-29 20:28:20 +000049#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: ";
Matthias Brauna7d6fc92016-08-19 22:31:45 +000054 RequiredProperties.print(errs());
Derek Schuff07636cd2016-03-29 20:28:20 +000055 errs() << "\nCurrent properties: ";
56 MFProps.print(errs());
57 errs() << "\n";
58 llvm_unreachable("MachineFunctionProperties check failed");
59 }
60#endif
Jessica Paquette54fbfae2018-09-10 22:24:10 +000061 // 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();
Derek Schuffad154c82016-03-28 17:05:30 +000072
73 bool RV = runOnMachineFunction(MF);
74
Jessica Paquette54fbfae2018-09-10 22:24:10 +000075 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
Derek Schuffad154c82016-03-28 17:05:30 +000098 MFProps.set(SetProperties);
Quentin Colombetc437aa92016-08-26 22:09:08 +000099 MFProps.reset(ClearedProperties);
Derek Schuffad154c82016-03-28 17:05:30 +0000100 return RV;
Dan Gohman5ea74d52009-07-31 18:16:33 +0000101}
102
103void MachineFunctionPass::getAnalysisUsage(AnalysisUsage &AU) const {
Matthias Braun733fe362016-08-24 01:52:46 +0000104 AU.addRequired<MachineModuleInfo>();
105 AU.addPreserved<MachineModuleInfo>();
Dan Gohman5ea74d52009-07-31 18:16:33 +0000106
107 // MachineFunctionPass preserves all LLVM IR passes, but there's no
108 // high-level way to express this. Instead, just list a bunch of
Dan Gohman09984272009-10-08 17:00:02 +0000109 // 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.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000112 AU.addPreserved<BasicAAWrapperPass>();
Hongbin Zheng751337f2016-02-25 17:54:15 +0000113 AU.addPreserved<DominanceFrontierWrapperPass>();
Chandler Carruthb81dfa62015-01-28 04:57:56 +0000114 AU.addPreserved<DominatorTreeWrapperPass>();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000115 AU.addPreserved<AAResultsWrapperPass>();
116 AU.addPreserved<GlobalsAAWrapperPass>();
Dehao Chen1a444522016-07-16 22:51:33 +0000117 AU.addPreserved<IVUsersWrapperPass>();
Chandler Carruthb81dfa62015-01-28 04:57:56 +0000118 AU.addPreserved<LoopInfoWrapperPass>();
Chandler Carruth61440d22016-03-10 00:55:30 +0000119 AU.addPreserved<MemoryDependenceWrapperPass>();
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000120 AU.addPreserved<ScalarEvolutionWrapperPass>();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000121 AU.addPreserved<SCEVAAWrapperPass>();
Dan Gohman5ea74d52009-07-31 18:16:33 +0000122
123 FunctionPass::getAnalysisUsage(AU);
124}