1 //===-- MachineFunctionPass.cpp -------------------------------------------===// 2 // 3 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains the definitions of the MachineFunctionPass members. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/CodeGen/MachineFunctionPass.h" 14 #include "llvm/Analysis/BasicAliasAnalysis.h" 15 #include "llvm/Analysis/DominanceFrontier.h" 16 #include "llvm/Analysis/GlobalsModRef.h" 17 #include "llvm/Analysis/IVUsers.h" 18 #include "llvm/Analysis/LoopInfo.h" 19 #include "llvm/Analysis/MemoryDependenceAnalysis.h" 20 #include "llvm/Analysis/OptimizationRemarkEmitter.h" 21 #include "llvm/Analysis/ScalarEvolution.h" 22 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" 23 #include "llvm/CodeGen/MachineFunction.h" 24 #include "llvm/CodeGen/MachineModuleInfo.h" 25 #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h" 26 #include "llvm/CodeGen/Passes.h" 27 #include "llvm/IR/Dominators.h" 28 #include "llvm/IR/Function.h" 29 #include "llvm/IR/PrintPasses.h" 30 31 using namespace llvm; 32 using namespace ore; 33 34 Pass *MachineFunctionPass::createPrinterPass(raw_ostream &O, 35 const std::string &Banner) const { 36 return createMachineFunctionPrinterPass(O, Banner); 37 } 38 39 bool MachineFunctionPass::runOnFunction(Function &F) { 40 // Do not codegen any 'available_externally' functions at all, they have 41 // definitions outside the translation unit. 42 if (F.hasAvailableExternallyLinkage()) 43 return false; 44 45 MachineModuleInfo &MMI = getAnalysis<MachineModuleInfoWrapperPass>().getMMI(); 46 MachineFunction &MF = MMI.getOrCreateMachineFunction(F); 47 48 MachineFunctionProperties &MFProps = MF.getProperties(); 49 50 #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: "; 55 RequiredProperties.print(errs()); 56 errs() << "\nCurrent properties: "; 57 MFProps.print(errs()); 58 errs() << "\n"; 59 llvm_unreachable("MachineFunctionProperties check failed"); 60 } 61 #endif 62 // 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(); 73 74 // For --print-changed, if the function name is a candidate, save the 75 // serialized MF to be compared later. 76 // TODO Implement --filter-passes. 77 SmallString<0> BeforeStr, AfterStr; 78 bool ShouldPrintChanged = PrintChanged != ChangePrinter::None && 79 isFunctionInPrintList(MF.getName()); 80 if (ShouldPrintChanged) { 81 raw_svector_ostream OS(BeforeStr); 82 MF.print(OS); 83 } 84 85 bool RV = runOnMachineFunction(MF); 86 87 if (ShouldEmitSizeRemarks) { 88 // We wanted size remarks. Check if there was a change to the number of 89 // MachineInstrs in the module. Emit a remark if there was a change. 90 CountAfter = MF.getInstructionCount(); 91 if (CountBefore != CountAfter) { 92 MachineOptimizationRemarkEmitter MORE(MF, nullptr); 93 MORE.emit([&]() { 94 int64_t Delta = static_cast<int64_t>(CountAfter) - 95 static_cast<int64_t>(CountBefore); 96 MachineOptimizationRemarkAnalysis R("size-info", "FunctionMISizeChange", 97 MF.getFunction().getSubprogram(), 98 &MF.front()); 99 R << NV("Pass", getPassName()) 100 << ": Function: " << NV("Function", F.getName()) << ": " 101 << "MI Instruction count changed from " 102 << NV("MIInstrsBefore", CountBefore) << " to " 103 << NV("MIInstrsAfter", CountAfter) 104 << "; Delta: " << NV("Delta", Delta); 105 return R; 106 }); 107 } 108 } 109 110 MFProps.set(SetProperties); 111 MFProps.reset(ClearedProperties); 112 113 // For --print-changed, print if the serialized MF has changed. Modes other 114 // than quiet/verbose are unimplemented and treated the same as 'quiet'. 115 if (ShouldPrintChanged) { 116 raw_svector_ostream OS(AfterStr); 117 MF.print(OS); 118 if (BeforeStr != AfterStr) { 119 StringRef Arg; 120 if (const PassInfo *PI = Pass::lookupPassInfo(getPassID())) 121 Arg = PI->getPassArgument(); 122 errs() << ("*** IR Dump After " + getPassName() + " (" + Arg + ") on " + 123 MF.getName() + " ***\n"); 124 switch (PrintChanged) { 125 case ChangePrinter::None: 126 llvm_unreachable(""); 127 case ChangePrinter::Quiet: 128 case ChangePrinter::Verbose: 129 case ChangePrinter::DotCfgQuiet: // unimplemented 130 case ChangePrinter::DotCfgVerbose: // unimplemented 131 errs() << AfterStr; 132 break; 133 case ChangePrinter::DiffQuiet: 134 case ChangePrinter::DiffVerbose: 135 case ChangePrinter::ColourDiffQuiet: 136 case ChangePrinter::ColourDiffVerbose: { 137 bool Color = llvm::is_contained( 138 {ChangePrinter::ColourDiffQuiet, ChangePrinter::ColourDiffVerbose}, 139 PrintChanged.getValue()); 140 StringRef Removed = Color ? "\033[31m-%l\033[0m\n" : "-%l\n"; 141 StringRef Added = Color ? "\033[32m+%l\033[0m\n" : "+%l\n"; 142 StringRef NoChange = " %l\n"; 143 errs() << doSystemDiff(BeforeStr, AfterStr, Removed, Added, NoChange); 144 break; 145 } 146 } 147 } else if (llvm::is_contained({ChangePrinter::Verbose, 148 ChangePrinter::DiffVerbose, 149 ChangePrinter::ColourDiffVerbose}, 150 PrintChanged.getValue())) { 151 errs() << ("*** IR Dump After " + getPassName() + " on " + MF.getName() + 152 " omitted because no change ***\n"); 153 } 154 } 155 return RV; 156 } 157 158 void MachineFunctionPass::getAnalysisUsage(AnalysisUsage &AU) const { 159 AU.addRequired<MachineModuleInfoWrapperPass>(); 160 AU.addPreserved<MachineModuleInfoWrapperPass>(); 161 162 // MachineFunctionPass preserves all LLVM IR passes, but there's no 163 // high-level way to express this. Instead, just list a bunch of 164 // passes explicitly. This does not include setPreservesCFG, 165 // because CodeGen overloads that to mean preserving the MachineBasicBlock 166 // CFG in addition to the LLVM IR CFG. 167 AU.addPreserved<BasicAAWrapperPass>(); 168 AU.addPreserved<DominanceFrontierWrapperPass>(); 169 AU.addPreserved<DominatorTreeWrapperPass>(); 170 AU.addPreserved<AAResultsWrapperPass>(); 171 AU.addPreserved<GlobalsAAWrapperPass>(); 172 AU.addPreserved<IVUsersWrapperPass>(); 173 AU.addPreserved<LoopInfoWrapperPass>(); 174 AU.addPreserved<MemoryDependenceWrapperPass>(); 175 AU.addPreserved<ScalarEvolutionWrapperPass>(); 176 AU.addPreserved<SCEVAAWrapperPass>(); 177 178 FunctionPass::getAnalysisUsage(AU); 179 } 180