10b57cec5SDimitry Andric //===-- MachineFunctionPass.cpp -------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file contains the definitions of the MachineFunctionPass members.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
140b57cec5SDimitry Andric #include "llvm/Analysis/BasicAliasAnalysis.h"
150b57cec5SDimitry Andric #include "llvm/Analysis/DominanceFrontier.h"
160b57cec5SDimitry Andric #include "llvm/Analysis/GlobalsModRef.h"
170b57cec5SDimitry Andric #include "llvm/Analysis/IVUsers.h"
180b57cec5SDimitry Andric #include "llvm/Analysis/LoopInfo.h"
190b57cec5SDimitry Andric #include "llvm/Analysis/MemoryDependenceAnalysis.h"
2081ad6265SDimitry Andric #include "llvm/Analysis/OptimizationRemarkEmitter.h"
210b57cec5SDimitry Andric #include "llvm/Analysis/ScalarEvolution.h"
220b57cec5SDimitry Andric #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
260b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h"
270b57cec5SDimitry Andric #include "llvm/IR/Dominators.h"
280b57cec5SDimitry Andric #include "llvm/IR/Function.h"
29972a253aSDimitry Andric #include "llvm/IR/PrintPasses.h"
300b57cec5SDimitry Andric
310b57cec5SDimitry Andric using namespace llvm;
320b57cec5SDimitry Andric using namespace ore;
330b57cec5SDimitry Andric
createPrinterPass(raw_ostream & O,const std::string & Banner) const340b57cec5SDimitry Andric Pass *MachineFunctionPass::createPrinterPass(raw_ostream &O,
350b57cec5SDimitry Andric const std::string &Banner) const {
360b57cec5SDimitry Andric return createMachineFunctionPrinterPass(O, Banner);
370b57cec5SDimitry Andric }
380b57cec5SDimitry Andric
runOnFunction(Function & F)390b57cec5SDimitry Andric bool MachineFunctionPass::runOnFunction(Function &F) {
400b57cec5SDimitry Andric // Do not codegen any 'available_externally' functions at all, they have
410b57cec5SDimitry Andric // definitions outside the translation unit.
420b57cec5SDimitry Andric if (F.hasAvailableExternallyLinkage())
430b57cec5SDimitry Andric return false;
440b57cec5SDimitry Andric
458bcb0991SDimitry Andric MachineModuleInfo &MMI = getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
460b57cec5SDimitry Andric MachineFunction &MF = MMI.getOrCreateMachineFunction(F);
470b57cec5SDimitry Andric
480b57cec5SDimitry Andric MachineFunctionProperties &MFProps = MF.getProperties();
490b57cec5SDimitry Andric
500b57cec5SDimitry Andric #ifndef NDEBUG
510b57cec5SDimitry Andric if (!MFProps.verifyRequiredProperties(RequiredProperties)) {
520b57cec5SDimitry Andric errs() << "MachineFunctionProperties required by " << getPassName()
530b57cec5SDimitry Andric << " pass are not met by function " << F.getName() << ".\n"
540b57cec5SDimitry Andric << "Required properties: ";
550b57cec5SDimitry Andric RequiredProperties.print(errs());
560b57cec5SDimitry Andric errs() << "\nCurrent properties: ";
570b57cec5SDimitry Andric MFProps.print(errs());
580b57cec5SDimitry Andric errs() << "\n";
590b57cec5SDimitry Andric llvm_unreachable("MachineFunctionProperties check failed");
600b57cec5SDimitry Andric }
610b57cec5SDimitry Andric #endif
620b57cec5SDimitry Andric // Collect the MI count of the function before the pass.
630b57cec5SDimitry Andric unsigned CountBefore, CountAfter;
640b57cec5SDimitry Andric
650b57cec5SDimitry Andric // Check if the user asked for size remarks.
660b57cec5SDimitry Andric bool ShouldEmitSizeRemarks =
670b57cec5SDimitry Andric F.getParent()->shouldEmitInstrCountChangedRemark();
680b57cec5SDimitry Andric
690b57cec5SDimitry Andric // If we want size remarks, collect the number of MachineInstrs in our
700b57cec5SDimitry Andric // MachineFunction before the pass runs.
710b57cec5SDimitry Andric if (ShouldEmitSizeRemarks)
720b57cec5SDimitry Andric CountBefore = MF.getInstructionCount();
730b57cec5SDimitry Andric
74972a253aSDimitry Andric // For --print-changed, if the function name is a candidate, save the
75972a253aSDimitry Andric // serialized MF to be compared later.
76972a253aSDimitry Andric SmallString<0> BeforeStr, AfterStr;
77bdd1243dSDimitry Andric StringRef PassID;
78bdd1243dSDimitry Andric if (PrintChanged != ChangePrinter::None) {
79bdd1243dSDimitry Andric if (const PassInfo *PI = Pass::lookupPassInfo(getPassID()))
80bdd1243dSDimitry Andric PassID = PI->getPassArgument();
81bdd1243dSDimitry Andric }
82bdd1243dSDimitry Andric const bool IsInterestingPass = isPassInPrintList(PassID);
83bdd1243dSDimitry Andric const bool ShouldPrintChanged = PrintChanged != ChangePrinter::None &&
84bdd1243dSDimitry Andric IsInterestingPass &&
85972a253aSDimitry Andric isFunctionInPrintList(MF.getName());
86972a253aSDimitry Andric if (ShouldPrintChanged) {
87972a253aSDimitry Andric raw_svector_ostream OS(BeforeStr);
88972a253aSDimitry Andric MF.print(OS);
89972a253aSDimitry Andric }
90972a253aSDimitry Andric
91*c9157d92SDimitry Andric MFProps.reset(ClearedProperties);
92*c9157d92SDimitry Andric
930b57cec5SDimitry Andric bool RV = runOnMachineFunction(MF);
940b57cec5SDimitry Andric
950b57cec5SDimitry Andric if (ShouldEmitSizeRemarks) {
960b57cec5SDimitry Andric // We wanted size remarks. Check if there was a change to the number of
970b57cec5SDimitry Andric // MachineInstrs in the module. Emit a remark if there was a change.
980b57cec5SDimitry Andric CountAfter = MF.getInstructionCount();
990b57cec5SDimitry Andric if (CountBefore != CountAfter) {
1000b57cec5SDimitry Andric MachineOptimizationRemarkEmitter MORE(MF, nullptr);
1010b57cec5SDimitry Andric MORE.emit([&]() {
1020b57cec5SDimitry Andric int64_t Delta = static_cast<int64_t>(CountAfter) -
1030b57cec5SDimitry Andric static_cast<int64_t>(CountBefore);
1040b57cec5SDimitry Andric MachineOptimizationRemarkAnalysis R("size-info", "FunctionMISizeChange",
1050b57cec5SDimitry Andric MF.getFunction().getSubprogram(),
1060b57cec5SDimitry Andric &MF.front());
1070b57cec5SDimitry Andric R << NV("Pass", getPassName())
1080b57cec5SDimitry Andric << ": Function: " << NV("Function", F.getName()) << ": "
1090b57cec5SDimitry Andric << "MI Instruction count changed from "
1100b57cec5SDimitry Andric << NV("MIInstrsBefore", CountBefore) << " to "
1110b57cec5SDimitry Andric << NV("MIInstrsAfter", CountAfter)
1120b57cec5SDimitry Andric << "; Delta: " << NV("Delta", Delta);
1130b57cec5SDimitry Andric return R;
1140b57cec5SDimitry Andric });
1150b57cec5SDimitry Andric }
1160b57cec5SDimitry Andric }
1170b57cec5SDimitry Andric
1180b57cec5SDimitry Andric MFProps.set(SetProperties);
119972a253aSDimitry Andric
120972a253aSDimitry Andric // For --print-changed, print if the serialized MF has changed. Modes other
121972a253aSDimitry Andric // than quiet/verbose are unimplemented and treated the same as 'quiet'.
122bdd1243dSDimitry Andric if (ShouldPrintChanged || !IsInterestingPass) {
123972a253aSDimitry Andric if (ShouldPrintChanged) {
124972a253aSDimitry Andric raw_svector_ostream OS(AfterStr);
125972a253aSDimitry Andric MF.print(OS);
126bdd1243dSDimitry Andric }
127bdd1243dSDimitry Andric if (IsInterestingPass && BeforeStr != AfterStr) {
128bdd1243dSDimitry Andric errs() << ("*** IR Dump After " + getPassName() + " (" + PassID +
129bdd1243dSDimitry Andric ") on " + MF.getName() + " ***\n");
130bdd1243dSDimitry Andric switch (PrintChanged) {
131bdd1243dSDimitry Andric case ChangePrinter::None:
132bdd1243dSDimitry Andric llvm_unreachable("");
133bdd1243dSDimitry Andric case ChangePrinter::Quiet:
134bdd1243dSDimitry Andric case ChangePrinter::Verbose:
135bdd1243dSDimitry Andric case ChangePrinter::DotCfgQuiet: // unimplemented
136bdd1243dSDimitry Andric case ChangePrinter::DotCfgVerbose: // unimplemented
137bdd1243dSDimitry Andric errs() << AfterStr;
138bdd1243dSDimitry Andric break;
139bdd1243dSDimitry Andric case ChangePrinter::DiffQuiet:
140bdd1243dSDimitry Andric case ChangePrinter::DiffVerbose:
141bdd1243dSDimitry Andric case ChangePrinter::ColourDiffQuiet:
142bdd1243dSDimitry Andric case ChangePrinter::ColourDiffVerbose: {
143bdd1243dSDimitry Andric bool Color = llvm::is_contained(
144bdd1243dSDimitry Andric {ChangePrinter::ColourDiffQuiet, ChangePrinter::ColourDiffVerbose},
145bdd1243dSDimitry Andric PrintChanged.getValue());
146bdd1243dSDimitry Andric StringRef Removed = Color ? "\033[31m-%l\033[0m\n" : "-%l\n";
147bdd1243dSDimitry Andric StringRef Added = Color ? "\033[32m+%l\033[0m\n" : "+%l\n";
148bdd1243dSDimitry Andric StringRef NoChange = " %l\n";
149bdd1243dSDimitry Andric errs() << doSystemDiff(BeforeStr, AfterStr, Removed, Added, NoChange);
150bdd1243dSDimitry Andric break;
151bdd1243dSDimitry Andric }
152bdd1243dSDimitry Andric }
153bdd1243dSDimitry Andric } else if (llvm::is_contained({ChangePrinter::Verbose,
154bdd1243dSDimitry Andric ChangePrinter::DiffVerbose,
155bdd1243dSDimitry Andric ChangePrinter::ColourDiffVerbose},
156bdd1243dSDimitry Andric PrintChanged.getValue())) {
157bdd1243dSDimitry Andric const char *Reason =
158bdd1243dSDimitry Andric IsInterestingPass ? " omitted because no change" : " filtered out";
159bdd1243dSDimitry Andric errs() << "*** IR Dump After " << getPassName();
160bdd1243dSDimitry Andric if (!PassID.empty())
161bdd1243dSDimitry Andric errs() << " (" << PassID << ")";
162bdd1243dSDimitry Andric errs() << " on " << MF.getName() + Reason + " ***\n";
163972a253aSDimitry Andric }
164972a253aSDimitry Andric }
1650b57cec5SDimitry Andric return RV;
1660b57cec5SDimitry Andric }
1670b57cec5SDimitry Andric
getAnalysisUsage(AnalysisUsage & AU) const1680b57cec5SDimitry Andric void MachineFunctionPass::getAnalysisUsage(AnalysisUsage &AU) const {
1698bcb0991SDimitry Andric AU.addRequired<MachineModuleInfoWrapperPass>();
1708bcb0991SDimitry Andric AU.addPreserved<MachineModuleInfoWrapperPass>();
1710b57cec5SDimitry Andric
1720b57cec5SDimitry Andric // MachineFunctionPass preserves all LLVM IR passes, but there's no
1730b57cec5SDimitry Andric // high-level way to express this. Instead, just list a bunch of
1740b57cec5SDimitry Andric // passes explicitly. This does not include setPreservesCFG,
1750b57cec5SDimitry Andric // because CodeGen overloads that to mean preserving the MachineBasicBlock
1760b57cec5SDimitry Andric // CFG in addition to the LLVM IR CFG.
1770b57cec5SDimitry Andric AU.addPreserved<BasicAAWrapperPass>();
1780b57cec5SDimitry Andric AU.addPreserved<DominanceFrontierWrapperPass>();
1790b57cec5SDimitry Andric AU.addPreserved<DominatorTreeWrapperPass>();
1800b57cec5SDimitry Andric AU.addPreserved<AAResultsWrapperPass>();
1810b57cec5SDimitry Andric AU.addPreserved<GlobalsAAWrapperPass>();
1820b57cec5SDimitry Andric AU.addPreserved<IVUsersWrapperPass>();
1830b57cec5SDimitry Andric AU.addPreserved<LoopInfoWrapperPass>();
1840b57cec5SDimitry Andric AU.addPreserved<MemoryDependenceWrapperPass>();
1850b57cec5SDimitry Andric AU.addPreserved<ScalarEvolutionWrapperPass>();
1860b57cec5SDimitry Andric AU.addPreserved<SCEVAAWrapperPass>();
1870b57cec5SDimitry Andric
1880b57cec5SDimitry Andric FunctionPass::getAnalysisUsage(AU);
1890b57cec5SDimitry Andric }
190