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" 29*0fca6ea1SDimitry Andric #include "llvm/IR/Module.h" 30972a253aSDimitry Andric #include "llvm/IR/PrintPasses.h" 310b57cec5SDimitry Andric 320b57cec5SDimitry Andric using namespace llvm; 330b57cec5SDimitry Andric using namespace ore; 340b57cec5SDimitry Andric 350b57cec5SDimitry Andric Pass *MachineFunctionPass::createPrinterPass(raw_ostream &O, 360b57cec5SDimitry Andric const std::string &Banner) const { 370b57cec5SDimitry Andric return createMachineFunctionPrinterPass(O, Banner); 380b57cec5SDimitry Andric } 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric bool MachineFunctionPass::runOnFunction(Function &F) { 410b57cec5SDimitry Andric // Do not codegen any 'available_externally' functions at all, they have 420b57cec5SDimitry Andric // definitions outside the translation unit. 430b57cec5SDimitry Andric if (F.hasAvailableExternallyLinkage()) 440b57cec5SDimitry Andric return false; 450b57cec5SDimitry Andric 468bcb0991SDimitry Andric MachineModuleInfo &MMI = getAnalysis<MachineModuleInfoWrapperPass>().getMMI(); 470b57cec5SDimitry Andric MachineFunction &MF = MMI.getOrCreateMachineFunction(F); 480b57cec5SDimitry Andric 490b57cec5SDimitry Andric MachineFunctionProperties &MFProps = MF.getProperties(); 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric #ifndef NDEBUG 520b57cec5SDimitry Andric if (!MFProps.verifyRequiredProperties(RequiredProperties)) { 530b57cec5SDimitry Andric errs() << "MachineFunctionProperties required by " << getPassName() 540b57cec5SDimitry Andric << " pass are not met by function " << F.getName() << ".\n" 550b57cec5SDimitry Andric << "Required properties: "; 560b57cec5SDimitry Andric RequiredProperties.print(errs()); 570b57cec5SDimitry Andric errs() << "\nCurrent properties: "; 580b57cec5SDimitry Andric MFProps.print(errs()); 590b57cec5SDimitry Andric errs() << "\n"; 600b57cec5SDimitry Andric llvm_unreachable("MachineFunctionProperties check failed"); 610b57cec5SDimitry Andric } 620b57cec5SDimitry Andric #endif 630b57cec5SDimitry Andric // Collect the MI count of the function before the pass. 640b57cec5SDimitry Andric unsigned CountBefore, CountAfter; 650b57cec5SDimitry Andric 660b57cec5SDimitry Andric // Check if the user asked for size remarks. 670b57cec5SDimitry Andric bool ShouldEmitSizeRemarks = 680b57cec5SDimitry Andric F.getParent()->shouldEmitInstrCountChangedRemark(); 690b57cec5SDimitry Andric 700b57cec5SDimitry Andric // If we want size remarks, collect the number of MachineInstrs in our 710b57cec5SDimitry Andric // MachineFunction before the pass runs. 720b57cec5SDimitry Andric if (ShouldEmitSizeRemarks) 730b57cec5SDimitry Andric CountBefore = MF.getInstructionCount(); 740b57cec5SDimitry Andric 75972a253aSDimitry Andric // For --print-changed, if the function name is a candidate, save the 76972a253aSDimitry Andric // serialized MF to be compared later. 77972a253aSDimitry Andric SmallString<0> BeforeStr, AfterStr; 78bdd1243dSDimitry Andric StringRef PassID; 79bdd1243dSDimitry Andric if (PrintChanged != ChangePrinter::None) { 80bdd1243dSDimitry Andric if (const PassInfo *PI = Pass::lookupPassInfo(getPassID())) 81bdd1243dSDimitry Andric PassID = PI->getPassArgument(); 82bdd1243dSDimitry Andric } 83bdd1243dSDimitry Andric const bool IsInterestingPass = isPassInPrintList(PassID); 84bdd1243dSDimitry Andric const bool ShouldPrintChanged = PrintChanged != ChangePrinter::None && 85bdd1243dSDimitry Andric IsInterestingPass && 86972a253aSDimitry Andric isFunctionInPrintList(MF.getName()); 87972a253aSDimitry Andric if (ShouldPrintChanged) { 88972a253aSDimitry Andric raw_svector_ostream OS(BeforeStr); 89972a253aSDimitry Andric MF.print(OS); 90972a253aSDimitry Andric } 91972a253aSDimitry Andric 925f757f3fSDimitry Andric MFProps.reset(ClearedProperties); 935f757f3fSDimitry Andric 940b57cec5SDimitry Andric bool RV = runOnMachineFunction(MF); 950b57cec5SDimitry Andric 960b57cec5SDimitry Andric if (ShouldEmitSizeRemarks) { 970b57cec5SDimitry Andric // We wanted size remarks. Check if there was a change to the number of 980b57cec5SDimitry Andric // MachineInstrs in the module. Emit a remark if there was a change. 990b57cec5SDimitry Andric CountAfter = MF.getInstructionCount(); 1000b57cec5SDimitry Andric if (CountBefore != CountAfter) { 1010b57cec5SDimitry Andric MachineOptimizationRemarkEmitter MORE(MF, nullptr); 1020b57cec5SDimitry Andric MORE.emit([&]() { 1030b57cec5SDimitry Andric int64_t Delta = static_cast<int64_t>(CountAfter) - 1040b57cec5SDimitry Andric static_cast<int64_t>(CountBefore); 1050b57cec5SDimitry Andric MachineOptimizationRemarkAnalysis R("size-info", "FunctionMISizeChange", 1060b57cec5SDimitry Andric MF.getFunction().getSubprogram(), 1070b57cec5SDimitry Andric &MF.front()); 1080b57cec5SDimitry Andric R << NV("Pass", getPassName()) 1090b57cec5SDimitry Andric << ": Function: " << NV("Function", F.getName()) << ": " 1100b57cec5SDimitry Andric << "MI Instruction count changed from " 1110b57cec5SDimitry Andric << NV("MIInstrsBefore", CountBefore) << " to " 1120b57cec5SDimitry Andric << NV("MIInstrsAfter", CountAfter) 1130b57cec5SDimitry Andric << "; Delta: " << NV("Delta", Delta); 1140b57cec5SDimitry Andric return R; 1150b57cec5SDimitry Andric }); 1160b57cec5SDimitry Andric } 1170b57cec5SDimitry Andric } 1180b57cec5SDimitry Andric 1190b57cec5SDimitry Andric MFProps.set(SetProperties); 120972a253aSDimitry Andric 121972a253aSDimitry Andric // For --print-changed, print if the serialized MF has changed. Modes other 122972a253aSDimitry Andric // than quiet/verbose are unimplemented and treated the same as 'quiet'. 123bdd1243dSDimitry Andric if (ShouldPrintChanged || !IsInterestingPass) { 124972a253aSDimitry Andric if (ShouldPrintChanged) { 125972a253aSDimitry Andric raw_svector_ostream OS(AfterStr); 126972a253aSDimitry Andric MF.print(OS); 127bdd1243dSDimitry Andric } 128bdd1243dSDimitry Andric if (IsInterestingPass && BeforeStr != AfterStr) { 129bdd1243dSDimitry Andric errs() << ("*** IR Dump After " + getPassName() + " (" + PassID + 130bdd1243dSDimitry Andric ") on " + MF.getName() + " ***\n"); 131bdd1243dSDimitry Andric switch (PrintChanged) { 132bdd1243dSDimitry Andric case ChangePrinter::None: 133bdd1243dSDimitry Andric llvm_unreachable(""); 134bdd1243dSDimitry Andric case ChangePrinter::Quiet: 135bdd1243dSDimitry Andric case ChangePrinter::Verbose: 136bdd1243dSDimitry Andric case ChangePrinter::DotCfgQuiet: // unimplemented 137bdd1243dSDimitry Andric case ChangePrinter::DotCfgVerbose: // unimplemented 138bdd1243dSDimitry Andric errs() << AfterStr; 139bdd1243dSDimitry Andric break; 140bdd1243dSDimitry Andric case ChangePrinter::DiffQuiet: 141bdd1243dSDimitry Andric case ChangePrinter::DiffVerbose: 142bdd1243dSDimitry Andric case ChangePrinter::ColourDiffQuiet: 143bdd1243dSDimitry Andric case ChangePrinter::ColourDiffVerbose: { 144bdd1243dSDimitry Andric bool Color = llvm::is_contained( 145bdd1243dSDimitry Andric {ChangePrinter::ColourDiffQuiet, ChangePrinter::ColourDiffVerbose}, 146bdd1243dSDimitry Andric PrintChanged.getValue()); 147bdd1243dSDimitry Andric StringRef Removed = Color ? "\033[31m-%l\033[0m\n" : "-%l\n"; 148bdd1243dSDimitry Andric StringRef Added = Color ? "\033[32m+%l\033[0m\n" : "+%l\n"; 149bdd1243dSDimitry Andric StringRef NoChange = " %l\n"; 150bdd1243dSDimitry Andric errs() << doSystemDiff(BeforeStr, AfterStr, Removed, Added, NoChange); 151bdd1243dSDimitry Andric break; 152bdd1243dSDimitry Andric } 153bdd1243dSDimitry Andric } 154bdd1243dSDimitry Andric } else if (llvm::is_contained({ChangePrinter::Verbose, 155bdd1243dSDimitry Andric ChangePrinter::DiffVerbose, 156bdd1243dSDimitry Andric ChangePrinter::ColourDiffVerbose}, 157bdd1243dSDimitry Andric PrintChanged.getValue())) { 158bdd1243dSDimitry Andric const char *Reason = 159bdd1243dSDimitry Andric IsInterestingPass ? " omitted because no change" : " filtered out"; 160bdd1243dSDimitry Andric errs() << "*** IR Dump After " << getPassName(); 161bdd1243dSDimitry Andric if (!PassID.empty()) 162bdd1243dSDimitry Andric errs() << " (" << PassID << ")"; 163bdd1243dSDimitry Andric errs() << " on " << MF.getName() + Reason + " ***\n"; 164972a253aSDimitry Andric } 165972a253aSDimitry Andric } 1660b57cec5SDimitry Andric return RV; 1670b57cec5SDimitry Andric } 1680b57cec5SDimitry Andric 1690b57cec5SDimitry Andric void MachineFunctionPass::getAnalysisUsage(AnalysisUsage &AU) const { 1708bcb0991SDimitry Andric AU.addRequired<MachineModuleInfoWrapperPass>(); 1718bcb0991SDimitry Andric AU.addPreserved<MachineModuleInfoWrapperPass>(); 1720b57cec5SDimitry Andric 1730b57cec5SDimitry Andric // MachineFunctionPass preserves all LLVM IR passes, but there's no 1740b57cec5SDimitry Andric // high-level way to express this. Instead, just list a bunch of 1750b57cec5SDimitry Andric // passes explicitly. This does not include setPreservesCFG, 1760b57cec5SDimitry Andric // because CodeGen overloads that to mean preserving the MachineBasicBlock 1770b57cec5SDimitry Andric // CFG in addition to the LLVM IR CFG. 1780b57cec5SDimitry Andric AU.addPreserved<BasicAAWrapperPass>(); 1790b57cec5SDimitry Andric AU.addPreserved<DominanceFrontierWrapperPass>(); 1800b57cec5SDimitry Andric AU.addPreserved<DominatorTreeWrapperPass>(); 1810b57cec5SDimitry Andric AU.addPreserved<AAResultsWrapperPass>(); 1820b57cec5SDimitry Andric AU.addPreserved<GlobalsAAWrapperPass>(); 1830b57cec5SDimitry Andric AU.addPreserved<IVUsersWrapperPass>(); 1840b57cec5SDimitry Andric AU.addPreserved<LoopInfoWrapperPass>(); 1850b57cec5SDimitry Andric AU.addPreserved<MemoryDependenceWrapperPass>(); 1860b57cec5SDimitry Andric AU.addPreserved<ScalarEvolutionWrapperPass>(); 1870b57cec5SDimitry Andric AU.addPreserved<SCEVAAWrapperPass>(); 1880b57cec5SDimitry Andric 1890b57cec5SDimitry Andric FunctionPass::getAnalysisUsage(AU); 1900b57cec5SDimitry Andric } 191