blob: 5db4e299fa701bdae32c3cd3bc4c4fb37d286291 [file] [log] [blame]
Dan Gohmanad2afc22009-07-31 18:16:33 +00001//===-- MachineFunctionPass.cpp -------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the definitions of the MachineFunctionPass members.
11//
12//===----------------------------------------------------------------------===//
13
Dan Gohmanad2afc22009-07-31 18:16:33 +000014#include "llvm/CodeGen/MachineFunctionPass.h"
Chandler Carruth52334f22015-01-28 04:57:56 +000015#include "llvm/Analysis/AliasAnalysis.h"
Chandler Carruth91468332015-09-09 17:55:00 +000016#include "llvm/Analysis/BasicAliasAnalysis.h"
Chandler Carruth52334f22015-01-28 04:57:56 +000017#include "llvm/Analysis/DominanceFrontier.h"
Chandler Carruth91468332015-09-09 17:55:00 +000018#include "llvm/Analysis/GlobalsModRef.h"
Chandler Carruth52334f22015-01-28 04:57:56 +000019#include "llvm/Analysis/IVUsers.h"
20#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth52334f22015-01-28 04:57:56 +000021#include "llvm/Analysis/MemoryDependenceAnalysis.h"
22#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth91468332015-09-09 17:55:00 +000023#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Derek Schufffadd1132016-03-28 17:05:30 +000024#include "llvm/CodeGen/MachineFunction.h"
Matthias Braunfa5c5c72016-08-24 01:52:46 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Jessica Paquette3ec83652018-09-10 22:24:10 +000026#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
David Greene5c8aa952010-04-02 23:17:14 +000027#include "llvm/CodeGen/Passes.h"
Chandler Carruth52334f22015-01-28 04:57:56 +000028#include "llvm/IR/Dominators.h"
29#include "llvm/IR/Function.h"
Derek Schufffadd1132016-03-28 17:05:30 +000030
Dan Gohmanad2afc22009-07-31 18:16:33 +000031using namespace llvm;
Jessica Paquette3ec83652018-09-10 22:24:10 +000032using namespace ore;
Dan Gohmanad2afc22009-07-31 18:16:33 +000033
David Greene5c8aa952010-04-02 23:17:14 +000034Pass *MachineFunctionPass::createPrinterPass(raw_ostream &O,
35 const std::string &Banner) const {
36 return createMachineFunctionPrinterPass(O, Banner);
37}
38
Dan Gohmanad2afc22009-07-31 18:16:33 +000039bool MachineFunctionPass::runOnFunction(Function &F) {
40 // Do not codegen any 'available_externally' functions at all, they have
41 // definitions outside the translation unit.
Serge Pavlov69a1a202017-01-15 10:23:18 +000042 if (F.hasAvailableExternallyLinkage())
Dan Gohmanad2afc22009-07-31 18:16:33 +000043 return false;
44
Matthias Braunfa5c5c72016-08-24 01:52:46 +000045 MachineModuleInfo &MMI = getAnalysis<MachineModuleInfo>();
Matthias Braun2144c522017-06-06 00:44:35 +000046 MachineFunction &MF = MMI.getOrCreateMachineFunction(F);
Matthias Braunfa5c5c72016-08-24 01:52:46 +000047
Derek Schufffadd1132016-03-28 17:05:30 +000048 MachineFunctionProperties &MFProps = MF.getProperties();
49
Derek Schuffd239bc52016-03-29 20:28:20 +000050#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: ";
Matthias Braun78efd692016-08-19 22:31:45 +000055 RequiredProperties.print(errs());
Derek Schuffd239bc52016-03-29 20:28:20 +000056 errs() << "\nCurrent properties: ";
57 MFProps.print(errs());
58 errs() << "\n";
59 llvm_unreachable("MachineFunctionProperties check failed");
60 }
61#endif
Jessica Paquette3ec83652018-09-10 22:24:10 +000062 // 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();
Derek Schufffadd1132016-03-28 17:05:30 +000073
74 bool RV = runOnMachineFunction(MF);
75
Jessica Paquette3ec83652018-09-10 22:24:10 +000076 if (ShouldEmitSizeRemarks) {
77 // We wanted size remarks. Check if there was a change to the number of
78 // MachineInstrs in the module. Emit a remark if there was a change.
79 CountAfter = MF.getInstructionCount();
80 if (CountBefore != CountAfter) {
81 MachineOptimizationRemarkEmitter MORE(MF, nullptr);
82 MORE.emit([&]() {
83 int64_t Delta = static_cast<int64_t>(CountAfter) -
84 static_cast<int64_t>(CountBefore);
85 MachineOptimizationRemarkAnalysis R("size-info", "FunctionMISizeChange",
86 MF.getFunction().getSubprogram(),
87 &MF.front());
88 R << NV("Pass", getPassName())
89 << ": Function: " << NV("Function", F.getName()) << ": "
90 << "MI Instruction count changed from "
91 << NV("MIInstrsBefore", CountBefore) << " to "
92 << NV("MIInstrsAfter", CountAfter)
93 << "; Delta: " << NV("Delta", Delta);
94 return R;
95 });
96 }
97 }
98
Derek Schufffadd1132016-03-28 17:05:30 +000099 MFProps.set(SetProperties);
Quentin Colombet1c4f0f92016-08-26 22:09:08 +0000100 MFProps.reset(ClearedProperties);
Derek Schufffadd1132016-03-28 17:05:30 +0000101 return RV;
Dan Gohmanad2afc22009-07-31 18:16:33 +0000102}
103
104void MachineFunctionPass::getAnalysisUsage(AnalysisUsage &AU) const {
Matthias Braunfa5c5c72016-08-24 01:52:46 +0000105 AU.addRequired<MachineModuleInfo>();
106 AU.addPreserved<MachineModuleInfo>();
Dan Gohmanad2afc22009-07-31 18:16:33 +0000107
108 // MachineFunctionPass preserves all LLVM IR passes, but there's no
109 // high-level way to express this. Instead, just list a bunch of
Dan Gohman8a261e42009-10-08 17:00:02 +0000110 // passes explicitly. This does not include setPreservesCFG,
111 // because CodeGen overloads that to mean preserving the MachineBasicBlock
112 // CFG in addition to the LLVM IR CFG.
Chandler Carruth91468332015-09-09 17:55:00 +0000113 AU.addPreserved<BasicAAWrapperPass>();
Hongbin Zheng15969222016-02-25 17:54:15 +0000114 AU.addPreserved<DominanceFrontierWrapperPass>();
Chandler Carruth52334f22015-01-28 04:57:56 +0000115 AU.addPreserved<DominatorTreeWrapperPass>();
Chandler Carruth91468332015-09-09 17:55:00 +0000116 AU.addPreserved<AAResultsWrapperPass>();
117 AU.addPreserved<GlobalsAAWrapperPass>();
Dehao Chen78ee4b62016-07-16 22:51:33 +0000118 AU.addPreserved<IVUsersWrapperPass>();
Chandler Carruth52334f22015-01-28 04:57:56 +0000119 AU.addPreserved<LoopInfoWrapperPass>();
Chandler Carruthc5266b52016-03-10 00:55:30 +0000120 AU.addPreserved<MemoryDependenceWrapperPass>();
Chandler Carruthbfe1f1c2015-08-17 02:08:17 +0000121 AU.addPreserved<ScalarEvolutionWrapperPass>();
Chandler Carruth91468332015-09-09 17:55:00 +0000122 AU.addPreserved<SCEVAAWrapperPass>();
Dan Gohmanad2afc22009-07-31 18:16:33 +0000123
124 FunctionPass::getAnalysisUsage(AU);
125}