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/Module.h" 30 #include "llvm/IR/PrintPasses.h" 31 32 using namespace llvm; 33 using namespace ore; 34 35 static cl::opt<bool> DroppedVarStatsMIR( 36 "dropped-variable-stats-mir", cl::Hidden, 37 cl::desc("Dump dropped debug variables stats for MIR passes"), 38 cl::init(false)); 39 40 Pass *MachineFunctionPass::createPrinterPass(raw_ostream &O, 41 const std::string &Banner) const { 42 return createMachineFunctionPrinterPass(O, Banner); 43 } 44 45 bool MachineFunctionPass::runOnFunction(Function &F) { 46 // Do not codegen any 'available_externally' functions at all, they have 47 // definitions outside the translation unit. 48 if (F.hasAvailableExternallyLinkage()) 49 return false; 50 51 MachineModuleInfo &MMI = getAnalysis<MachineModuleInfoWrapperPass>().getMMI(); 52 MachineFunction &MF = MMI.getOrCreateMachineFunction(F); 53 54 MachineFunctionProperties &MFProps = MF.getProperties(); 55 56 #ifndef NDEBUG 57 if (!MFProps.verifyRequiredProperties(RequiredProperties)) { 58 errs() << "MachineFunctionProperties required by " << getPassName() 59 << " pass are not met by function " << F.getName() << ".\n" 60 << "Required properties: "; 61 RequiredProperties.print(errs()); 62 errs() << "\nCurrent properties: "; 63 MFProps.print(errs()); 64 errs() << "\n"; 65 llvm_unreachable("MachineFunctionProperties check failed"); 66 } 67 #endif 68 // Collect the MI count of the function before the pass. 69 unsigned CountBefore, CountAfter; 70 71 // Check if the user asked for size remarks. 72 bool ShouldEmitSizeRemarks = 73 F.getParent()->shouldEmitInstrCountChangedRemark(); 74 75 // If we want size remarks, collect the number of MachineInstrs in our 76 // MachineFunction before the pass runs. 77 if (ShouldEmitSizeRemarks) 78 CountBefore = MF.getInstructionCount(); 79 80 // For --print-changed, if the function name is a candidate, save the 81 // serialized MF to be compared later. 82 SmallString<0> BeforeStr, AfterStr; 83 StringRef PassID; 84 if (PrintChanged != ChangePrinter::None) { 85 if (const PassInfo *PI = Pass::lookupPassInfo(getPassID())) 86 PassID = PI->getPassArgument(); 87 } 88 const bool IsInterestingPass = isPassInPrintList(PassID); 89 const bool ShouldPrintChanged = PrintChanged != ChangePrinter::None && 90 IsInterestingPass && 91 isFunctionInPrintList(MF.getName()); 92 if (ShouldPrintChanged) { 93 raw_svector_ostream OS(BeforeStr); 94 MF.print(OS); 95 } 96 97 MFProps.reset(ClearedProperties); 98 99 bool RV; 100 if (DroppedVarStatsMIR) { 101 auto PassName = getPassName(); 102 DroppedVarStatsMF.runBeforePass(PassName, &MF); 103 RV = runOnMachineFunction(MF); 104 DroppedVarStatsMF.runAfterPass(PassName, &MF); 105 } else { 106 RV = runOnMachineFunction(MF); 107 } 108 109 if (ShouldEmitSizeRemarks) { 110 // We wanted size remarks. Check if there was a change to the number of 111 // MachineInstrs in the module. Emit a remark if there was a change. 112 CountAfter = MF.getInstructionCount(); 113 if (CountBefore != CountAfter) { 114 MachineOptimizationRemarkEmitter MORE(MF, nullptr); 115 MORE.emit([&]() { 116 int64_t Delta = static_cast<int64_t>(CountAfter) - 117 static_cast<int64_t>(CountBefore); 118 MachineOptimizationRemarkAnalysis R("size-info", "FunctionMISizeChange", 119 MF.getFunction().getSubprogram(), 120 &MF.front()); 121 R << NV("Pass", getPassName()) 122 << ": Function: " << NV("Function", F.getName()) << ": " 123 << "MI Instruction count changed from " 124 << NV("MIInstrsBefore", CountBefore) << " to " 125 << NV("MIInstrsAfter", CountAfter) 126 << "; Delta: " << NV("Delta", Delta); 127 return R; 128 }); 129 } 130 } 131 132 MFProps.set(SetProperties); 133 134 // For --print-changed, print if the serialized MF has changed. Modes other 135 // than quiet/verbose are unimplemented and treated the same as 'quiet'. 136 if (ShouldPrintChanged || !IsInterestingPass) { 137 if (ShouldPrintChanged) { 138 raw_svector_ostream OS(AfterStr); 139 MF.print(OS); 140 } 141 if (IsInterestingPass && BeforeStr != AfterStr) { 142 errs() << ("*** IR Dump After " + getPassName() + " (" + PassID + 143 ") on " + MF.getName() + " ***\n"); 144 switch (PrintChanged) { 145 case ChangePrinter::None: 146 llvm_unreachable(""); 147 case ChangePrinter::Quiet: 148 case ChangePrinter::Verbose: 149 case ChangePrinter::DotCfgQuiet: // unimplemented 150 case ChangePrinter::DotCfgVerbose: // unimplemented 151 errs() << AfterStr; 152 break; 153 case ChangePrinter::DiffQuiet: 154 case ChangePrinter::DiffVerbose: 155 case ChangePrinter::ColourDiffQuiet: 156 case ChangePrinter::ColourDiffVerbose: { 157 bool Color = llvm::is_contained( 158 {ChangePrinter::ColourDiffQuiet, ChangePrinter::ColourDiffVerbose}, 159 PrintChanged.getValue()); 160 StringRef Removed = Color ? "\033[31m-%l\033[0m\n" : "-%l\n"; 161 StringRef Added = Color ? "\033[32m+%l\033[0m\n" : "+%l\n"; 162 StringRef NoChange = " %l\n"; 163 errs() << doSystemDiff(BeforeStr, AfterStr, Removed, Added, NoChange); 164 break; 165 } 166 } 167 } else if (llvm::is_contained({ChangePrinter::Verbose, 168 ChangePrinter::DiffVerbose, 169 ChangePrinter::ColourDiffVerbose}, 170 PrintChanged.getValue())) { 171 const char *Reason = 172 IsInterestingPass ? " omitted because no change" : " filtered out"; 173 errs() << "*** IR Dump After " << getPassName(); 174 if (!PassID.empty()) 175 errs() << " (" << PassID << ")"; 176 errs() << " on " << MF.getName() + Reason + " ***\n"; 177 } 178 } 179 return RV; 180 } 181 182 void MachineFunctionPass::getAnalysisUsage(AnalysisUsage &AU) const { 183 AU.addRequired<MachineModuleInfoWrapperPass>(); 184 AU.addPreserved<MachineModuleInfoWrapperPass>(); 185 186 // MachineFunctionPass preserves all LLVM IR passes, but there's no 187 // high-level way to express this. Instead, just list a bunch of 188 // passes explicitly. This does not include setPreservesCFG, 189 // because CodeGen overloads that to mean preserving the MachineBasicBlock 190 // CFG in addition to the LLVM IR CFG. 191 AU.addPreserved<BasicAAWrapperPass>(); 192 AU.addPreserved<DominanceFrontierWrapperPass>(); 193 AU.addPreserved<DominatorTreeWrapperPass>(); 194 AU.addPreserved<AAResultsWrapperPass>(); 195 AU.addPreserved<GlobalsAAWrapperPass>(); 196 AU.addPreserved<IVUsersWrapperPass>(); 197 AU.addPreserved<LoopInfoWrapperPass>(); 198 AU.addPreserved<MemoryDependenceWrapperPass>(); 199 AU.addPreserved<ScalarEvolutionWrapperPass>(); 200 AU.addPreserved<SCEVAAWrapperPass>(); 201 202 FunctionPass::getAnalysisUsage(AU); 203 } 204