1 //===-- MachineFunctionPass.cpp -------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file contains the definitions of the MachineFunctionPass members. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/CodeGen/MachineFunctionPass.h" 15 #include "llvm/Analysis/AliasAnalysis.h" 16 #include "llvm/Analysis/BasicAliasAnalysis.h" 17 #include "llvm/Analysis/DominanceFrontier.h" 18 #include "llvm/Analysis/GlobalsModRef.h" 19 #include "llvm/Analysis/IVUsers.h" 20 #include "llvm/Analysis/LoopInfo.h" 21 #include "llvm/Analysis/MemoryDependenceAnalysis.h" 22 #include "llvm/Analysis/ScalarEvolution.h" 23 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" 24 #include "llvm/CodeGen/MachineFunction.h" 25 #include "llvm/CodeGen/MachineModuleInfo.h" 26 #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h" 27 #include "llvm/CodeGen/Passes.h" 28 #include "llvm/IR/Dominators.h" 29 #include "llvm/IR/Function.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<MachineModuleInfo>(); 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 bool RV = runOnMachineFunction(MF); 75 76 if (ShouldEmitSizeRemarks) { 77 // We wanted size remarks. Check if there was a change to the number of 78 // MachineInstrs in the module. Emit a remark if there was a change. 79 CountAfter = MF.getInstructionCount(); 80 if (CountBefore != CountAfter) { 81 MachineOptimizationRemarkEmitter MORE(MF, nullptr); 82 MORE.emit([&]() { 83 int64_t Delta = static_cast<int64_t>(CountAfter) - 84 static_cast<int64_t>(CountBefore); 85 MachineOptimizationRemarkAnalysis R("size-info", "FunctionMISizeChange", 86 MF.getFunction().getSubprogram(), 87 &MF.front()); 88 R << NV("Pass", getPassName()) 89 << ": Function: " << NV("Function", F.getName()) << ": " 90 << "MI Instruction count changed from " 91 << NV("MIInstrsBefore", CountBefore) << " to " 92 << NV("MIInstrsAfter", CountAfter) 93 << "; Delta: " << NV("Delta", Delta); 94 return R; 95 }); 96 } 97 } 98 99 MFProps.set(SetProperties); 100 MFProps.reset(ClearedProperties); 101 return RV; 102 } 103 104 void MachineFunctionPass::getAnalysisUsage(AnalysisUsage &AU) const { 105 AU.addRequired<MachineModuleInfo>(); 106 AU.addPreserved<MachineModuleInfo>(); 107 108 // MachineFunctionPass preserves all LLVM IR passes, but there's no 109 // high-level way to express this. Instead, just list a bunch of 110 // passes explicitly. This does not include setPreservesCFG, 111 // because CodeGen overloads that to mean preserving the MachineBasicBlock 112 // CFG in addition to the LLVM IR CFG. 113 AU.addPreserved<BasicAAWrapperPass>(); 114 AU.addPreserved<DominanceFrontierWrapperPass>(); 115 AU.addPreserved<DominatorTreeWrapperPass>(); 116 AU.addPreserved<AAResultsWrapperPass>(); 117 AU.addPreserved<GlobalsAAWrapperPass>(); 118 AU.addPreserved<IVUsersWrapperPass>(); 119 AU.addPreserved<LoopInfoWrapperPass>(); 120 AU.addPreserved<MemoryDependenceWrapperPass>(); 121 AU.addPreserved<ScalarEvolutionWrapperPass>(); 122 AU.addPreserved<SCEVAAWrapperPass>(); 123 124 FunctionPass::getAnalysisUsage(AU); 125 } 126