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