1*0b57cec5SDimitry Andric //===-- MachineFunctionPass.cpp -------------------------------------------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // This file contains the definitions of the MachineFunctionPass members. 10*0b57cec5SDimitry Andric // 11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 12*0b57cec5SDimitry Andric 13*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h" 14*0b57cec5SDimitry Andric #include "llvm/Analysis/BasicAliasAnalysis.h" 15*0b57cec5SDimitry Andric #include "llvm/Analysis/DominanceFrontier.h" 16*0b57cec5SDimitry Andric #include "llvm/Analysis/GlobalsModRef.h" 17*0b57cec5SDimitry Andric #include "llvm/Analysis/IVUsers.h" 18*0b57cec5SDimitry Andric #include "llvm/Analysis/LoopInfo.h" 19*0b57cec5SDimitry Andric #include "llvm/Analysis/MemoryDependenceAnalysis.h" 2081ad6265SDimitry Andric #include "llvm/Analysis/OptimizationRemarkEmitter.h" 21*0b57cec5SDimitry Andric #include "llvm/Analysis/ScalarEvolution.h" 22*0b57cec5SDimitry Andric #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" 23*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 24*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h" 25*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h" 26*0b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h" 27*0b57cec5SDimitry Andric #include "llvm/IR/Dominators.h" 28*0b57cec5SDimitry Andric #include "llvm/IR/Function.h" 29972a253aSDimitry Andric #include "llvm/IR/PrintPasses.h" 30*0b57cec5SDimitry Andric 31*0b57cec5SDimitry Andric using namespace llvm; 32*0b57cec5SDimitry Andric using namespace ore; 33*0b57cec5SDimitry Andric 34*0b57cec5SDimitry Andric Pass *MachineFunctionPass::createPrinterPass(raw_ostream &O, 35*0b57cec5SDimitry Andric const std::string &Banner) const { 36*0b57cec5SDimitry Andric return createMachineFunctionPrinterPass(O, Banner); 37*0b57cec5SDimitry Andric } 38*0b57cec5SDimitry Andric 39*0b57cec5SDimitry Andric bool MachineFunctionPass::runOnFunction(Function &F) { 40*0b57cec5SDimitry Andric // Do not codegen any 'available_externally' functions at all, they have 41*0b57cec5SDimitry Andric // definitions outside the translation unit. 42*0b57cec5SDimitry Andric if (F.hasAvailableExternallyLinkage()) 43*0b57cec5SDimitry Andric return false; 44*0b57cec5SDimitry Andric 458bcb0991SDimitry Andric MachineModuleInfo &MMI = getAnalysis<MachineModuleInfoWrapperPass>().getMMI(); 46*0b57cec5SDimitry Andric MachineFunction &MF = MMI.getOrCreateMachineFunction(F); 47*0b57cec5SDimitry Andric 48*0b57cec5SDimitry Andric MachineFunctionProperties &MFProps = MF.getProperties(); 49*0b57cec5SDimitry Andric 50*0b57cec5SDimitry Andric #ifndef NDEBUG 51*0b57cec5SDimitry Andric if (!MFProps.verifyRequiredProperties(RequiredProperties)) { 52*0b57cec5SDimitry Andric errs() << "MachineFunctionProperties required by " << getPassName() 53*0b57cec5SDimitry Andric << " pass are not met by function " << F.getName() << ".\n" 54*0b57cec5SDimitry Andric << "Required properties: "; 55*0b57cec5SDimitry Andric RequiredProperties.print(errs()); 56*0b57cec5SDimitry Andric errs() << "\nCurrent properties: "; 57*0b57cec5SDimitry Andric MFProps.print(errs()); 58*0b57cec5SDimitry Andric errs() << "\n"; 59*0b57cec5SDimitry Andric llvm_unreachable("MachineFunctionProperties check failed"); 60*0b57cec5SDimitry Andric } 61*0b57cec5SDimitry Andric #endif 62*0b57cec5SDimitry Andric // Collect the MI count of the function before the pass. 63*0b57cec5SDimitry Andric unsigned CountBefore, CountAfter; 64*0b57cec5SDimitry Andric 65*0b57cec5SDimitry Andric // Check if the user asked for size remarks. 66*0b57cec5SDimitry Andric bool ShouldEmitSizeRemarks = 67*0b57cec5SDimitry Andric F.getParent()->shouldEmitInstrCountChangedRemark(); 68*0b57cec5SDimitry Andric 69*0b57cec5SDimitry Andric // If we want size remarks, collect the number of MachineInstrs in our 70*0b57cec5SDimitry Andric // MachineFunction before the pass runs. 71*0b57cec5SDimitry Andric if (ShouldEmitSizeRemarks) 72*0b57cec5SDimitry Andric CountBefore = MF.getInstructionCount(); 73*0b57cec5SDimitry 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*0b57cec5SDimitry Andric bool RV = runOnMachineFunction(MF); 92*0b57cec5SDimitry Andric 93*0b57cec5SDimitry Andric if (ShouldEmitSizeRemarks) { 94*0b57cec5SDimitry Andric // We wanted size remarks. Check if there was a change to the number of 95*0b57cec5SDimitry Andric // MachineInstrs in the module. Emit a remark if there was a change. 96*0b57cec5SDimitry Andric CountAfter = MF.getInstructionCount(); 97*0b57cec5SDimitry Andric if (CountBefore != CountAfter) { 98*0b57cec5SDimitry Andric MachineOptimizationRemarkEmitter MORE(MF, nullptr); 99*0b57cec5SDimitry Andric MORE.emit([&]() { 100*0b57cec5SDimitry Andric int64_t Delta = static_cast<int64_t>(CountAfter) - 101*0b57cec5SDimitry Andric static_cast<int64_t>(CountBefore); 102*0b57cec5SDimitry Andric MachineOptimizationRemarkAnalysis R("size-info", "FunctionMISizeChange", 103*0b57cec5SDimitry Andric MF.getFunction().getSubprogram(), 104*0b57cec5SDimitry Andric &MF.front()); 105*0b57cec5SDimitry Andric R << NV("Pass", getPassName()) 106*0b57cec5SDimitry Andric << ": Function: " << NV("Function", F.getName()) << ": " 107*0b57cec5SDimitry Andric << "MI Instruction count changed from " 108*0b57cec5SDimitry Andric << NV("MIInstrsBefore", CountBefore) << " to " 109*0b57cec5SDimitry Andric << NV("MIInstrsAfter", CountAfter) 110*0b57cec5SDimitry Andric << "; Delta: " << NV("Delta", Delta); 111*0b57cec5SDimitry Andric return R; 112*0b57cec5SDimitry Andric }); 113*0b57cec5SDimitry Andric } 114*0b57cec5SDimitry Andric } 115*0b57cec5SDimitry Andric 116*0b57cec5SDimitry Andric MFProps.set(SetProperties); 117*0b57cec5SDimitry Andric MFProps.reset(ClearedProperties); 118972a253aSDimitry Andric 119972a253aSDimitry Andric // For --print-changed, print if the serialized MF has changed. Modes other 120972a253aSDimitry Andric // than quiet/verbose are unimplemented and treated the same as 'quiet'. 121bdd1243dSDimitry Andric if (ShouldPrintChanged || !IsInterestingPass) { 122972a253aSDimitry Andric if (ShouldPrintChanged) { 123972a253aSDimitry Andric raw_svector_ostream OS(AfterStr); 124972a253aSDimitry Andric MF.print(OS); 125bdd1243dSDimitry Andric } 126bdd1243dSDimitry Andric if (IsInterestingPass && BeforeStr != AfterStr) { 127bdd1243dSDimitry Andric errs() << ("*** IR Dump After " + getPassName() + " (" + PassID + 128bdd1243dSDimitry Andric ") on " + MF.getName() + " ***\n"); 129bdd1243dSDimitry Andric switch (PrintChanged) { 130bdd1243dSDimitry Andric case ChangePrinter::None: 131bdd1243dSDimitry Andric llvm_unreachable(""); 132bdd1243dSDimitry Andric case ChangePrinter::Quiet: 133bdd1243dSDimitry Andric case ChangePrinter::Verbose: 134bdd1243dSDimitry Andric case ChangePrinter::DotCfgQuiet: // unimplemented 135bdd1243dSDimitry Andric case ChangePrinter::DotCfgVerbose: // unimplemented 136bdd1243dSDimitry Andric errs() << AfterStr; 137bdd1243dSDimitry Andric break; 138bdd1243dSDimitry Andric case ChangePrinter::DiffQuiet: 139bdd1243dSDimitry Andric case ChangePrinter::DiffVerbose: 140bdd1243dSDimitry Andric case ChangePrinter::ColourDiffQuiet: 141bdd1243dSDimitry Andric case ChangePrinter::ColourDiffVerbose: { 142bdd1243dSDimitry Andric bool Color = llvm::is_contained( 143bdd1243dSDimitry Andric {ChangePrinter::ColourDiffQuiet, ChangePrinter::ColourDiffVerbose}, 144bdd1243dSDimitry Andric PrintChanged.getValue()); 145bdd1243dSDimitry Andric StringRef Removed = Color ? "\033[31m-%l\033[0m\n" : "-%l\n"; 146bdd1243dSDimitry Andric StringRef Added = Color ? "\033[32m+%l\033[0m\n" : "+%l\n"; 147bdd1243dSDimitry Andric StringRef NoChange = " %l\n"; 148bdd1243dSDimitry Andric errs() << doSystemDiff(BeforeStr, AfterStr, Removed, Added, NoChange); 149bdd1243dSDimitry Andric break; 150bdd1243dSDimitry Andric } 151bdd1243dSDimitry Andric } 152bdd1243dSDimitry Andric } else if (llvm::is_contained({ChangePrinter::Verbose, 153bdd1243dSDimitry Andric ChangePrinter::DiffVerbose, 154bdd1243dSDimitry Andric ChangePrinter::ColourDiffVerbose}, 155bdd1243dSDimitry Andric PrintChanged.getValue())) { 156bdd1243dSDimitry Andric const char *Reason = 157bdd1243dSDimitry Andric IsInterestingPass ? " omitted because no change" : " filtered out"; 158bdd1243dSDimitry Andric errs() << "*** IR Dump After " << getPassName(); 159bdd1243dSDimitry Andric if (!PassID.empty()) 160bdd1243dSDimitry Andric errs() << " (" << PassID << ")"; 161bdd1243dSDimitry Andric errs() << " on " << MF.getName() + Reason + " ***\n"; 162972a253aSDimitry Andric } 163972a253aSDimitry Andric } 164*0b57cec5SDimitry Andric return RV; 165*0b57cec5SDimitry Andric } 166*0b57cec5SDimitry Andric 167*0b57cec5SDimitry Andric void MachineFunctionPass::getAnalysisUsage(AnalysisUsage &AU) const { 1688bcb0991SDimitry Andric AU.addRequired<MachineModuleInfoWrapperPass>(); 1698bcb0991SDimitry Andric AU.addPreserved<MachineModuleInfoWrapperPass>(); 170*0b57cec5SDimitry Andric 171*0b57cec5SDimitry Andric // MachineFunctionPass preserves all LLVM IR passes, but there's no 172*0b57cec5SDimitry Andric // high-level way to express this. Instead, just list a bunch of 173*0b57cec5SDimitry Andric // passes explicitly. This does not include setPreservesCFG, 174*0b57cec5SDimitry Andric // because CodeGen overloads that to mean preserving the MachineBasicBlock 175*0b57cec5SDimitry Andric // CFG in addition to the LLVM IR CFG. 176*0b57cec5SDimitry Andric AU.addPreserved<BasicAAWrapperPass>(); 177*0b57cec5SDimitry Andric AU.addPreserved<DominanceFrontierWrapperPass>(); 178*0b57cec5SDimitry Andric AU.addPreserved<DominatorTreeWrapperPass>(); 179*0b57cec5SDimitry Andric AU.addPreserved<AAResultsWrapperPass>(); 180*0b57cec5SDimitry Andric AU.addPreserved<GlobalsAAWrapperPass>(); 181*0b57cec5SDimitry Andric AU.addPreserved<IVUsersWrapperPass>(); 182*0b57cec5SDimitry Andric AU.addPreserved<LoopInfoWrapperPass>(); 183*0b57cec5SDimitry Andric AU.addPreserved<MemoryDependenceWrapperPass>(); 184*0b57cec5SDimitry Andric AU.addPreserved<ScalarEvolutionWrapperPass>(); 185*0b57cec5SDimitry Andric AU.addPreserved<SCEVAAWrapperPass>(); 186*0b57cec5SDimitry Andric 187*0b57cec5SDimitry Andric FunctionPass::getAnalysisUsage(AU); 188*0b57cec5SDimitry Andric } 189