Dan Gohman | ad2afc2 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 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 | |
Dan Gohman | ad2afc2 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Chandler Carruth | 52334f2 | 2015-01-28 04:57:56 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/AliasAnalysis.h" |
Chandler Carruth | 9146833 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/BasicAliasAnalysis.h" |
Chandler Carruth | 52334f2 | 2015-01-28 04:57:56 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/DominanceFrontier.h" |
Chandler Carruth | 9146833 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/GlobalsModRef.h" |
Chandler Carruth | 52334f2 | 2015-01-28 04:57:56 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/IVUsers.h" |
| 20 | #include "llvm/Analysis/LoopInfo.h" |
Chandler Carruth | 52334f2 | 2015-01-28 04:57:56 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/MemoryDependenceAnalysis.h" |
| 22 | #include "llvm/Analysis/ScalarEvolution.h" |
Chandler Carruth | 9146833 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" |
Derek Schuff | fadd113 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineFunction.h" |
Matthias Braun | fa5c5c7 | 2016-08-24 01:52:46 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Jessica Paquette | 3ec8365 | 2018-09-10 22:24:10 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h" |
David Greene | 5c8aa95 | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/Passes.h" |
Chandler Carruth | 52334f2 | 2015-01-28 04:57:56 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Dominators.h" |
| 29 | #include "llvm/IR/Function.h" |
Derek Schuff | fadd113 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 30 | |
Dan Gohman | ad2afc2 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 31 | using namespace llvm; |
Jessica Paquette | 3ec8365 | 2018-09-10 22:24:10 +0000 | [diff] [blame] | 32 | using namespace ore; |
Dan Gohman | ad2afc2 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 33 | |
David Greene | 5c8aa95 | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 34 | Pass *MachineFunctionPass::createPrinterPass(raw_ostream &O, |
| 35 | const std::string &Banner) const { |
| 36 | return createMachineFunctionPrinterPass(O, Banner); |
| 37 | } |
| 38 | |
Dan Gohman | ad2afc2 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 39 | bool MachineFunctionPass::runOnFunction(Function &F) { |
| 40 | // Do not codegen any 'available_externally' functions at all, they have |
| 41 | // definitions outside the translation unit. |
Serge Pavlov | 69a1a20 | 2017-01-15 10:23:18 +0000 | [diff] [blame] | 42 | if (F.hasAvailableExternallyLinkage()) |
Dan Gohman | ad2afc2 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 43 | return false; |
| 44 | |
Matthias Braun | fa5c5c7 | 2016-08-24 01:52:46 +0000 | [diff] [blame] | 45 | MachineModuleInfo &MMI = getAnalysis<MachineModuleInfo>(); |
Matthias Braun | 2144c52 | 2017-06-06 00:44:35 +0000 | [diff] [blame] | 46 | MachineFunction &MF = MMI.getOrCreateMachineFunction(F); |
Matthias Braun | fa5c5c7 | 2016-08-24 01:52:46 +0000 | [diff] [blame] | 47 | |
Derek Schuff | fadd113 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 48 | MachineFunctionProperties &MFProps = MF.getProperties(); |
| 49 | |
Derek Schuff | d239bc5 | 2016-03-29 20:28:20 +0000 | [diff] [blame] | 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: "; |
Matthias Braun | 78efd69 | 2016-08-19 22:31:45 +0000 | [diff] [blame] | 55 | RequiredProperties.print(errs()); |
Derek Schuff | d239bc5 | 2016-03-29 20:28:20 +0000 | [diff] [blame] | 56 | errs() << "\nCurrent properties: "; |
| 57 | MFProps.print(errs()); |
| 58 | errs() << "\n"; |
| 59 | llvm_unreachable("MachineFunctionProperties check failed"); |
| 60 | } |
| 61 | #endif |
Jessica Paquette | 3ec8365 | 2018-09-10 22:24:10 +0000 | [diff] [blame] | 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(); |
Derek Schuff | fadd113 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 73 | |
| 74 | bool RV = runOnMachineFunction(MF); |
| 75 | |
Jessica Paquette | 3ec8365 | 2018-09-10 22:24:10 +0000 | [diff] [blame] | 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 | |
Derek Schuff | fadd113 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 99 | MFProps.set(SetProperties); |
Quentin Colombet | 1c4f0f9 | 2016-08-26 22:09:08 +0000 | [diff] [blame] | 100 | MFProps.reset(ClearedProperties); |
Derek Schuff | fadd113 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 101 | return RV; |
Dan Gohman | ad2afc2 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void MachineFunctionPass::getAnalysisUsage(AnalysisUsage &AU) const { |
Matthias Braun | fa5c5c7 | 2016-08-24 01:52:46 +0000 | [diff] [blame] | 105 | AU.addRequired<MachineModuleInfo>(); |
| 106 | AU.addPreserved<MachineModuleInfo>(); |
Dan Gohman | ad2afc2 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 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 |
Dan Gohman | 8a261e4 | 2009-10-08 17:00:02 +0000 | [diff] [blame] | 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. |
Chandler Carruth | 9146833 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 113 | AU.addPreserved<BasicAAWrapperPass>(); |
Hongbin Zheng | 1596922 | 2016-02-25 17:54:15 +0000 | [diff] [blame] | 114 | AU.addPreserved<DominanceFrontierWrapperPass>(); |
Chandler Carruth | 52334f2 | 2015-01-28 04:57:56 +0000 | [diff] [blame] | 115 | AU.addPreserved<DominatorTreeWrapperPass>(); |
Chandler Carruth | 9146833 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 116 | AU.addPreserved<AAResultsWrapperPass>(); |
| 117 | AU.addPreserved<GlobalsAAWrapperPass>(); |
Dehao Chen | 78ee4b6 | 2016-07-16 22:51:33 +0000 | [diff] [blame] | 118 | AU.addPreserved<IVUsersWrapperPass>(); |
Chandler Carruth | 52334f2 | 2015-01-28 04:57:56 +0000 | [diff] [blame] | 119 | AU.addPreserved<LoopInfoWrapperPass>(); |
Chandler Carruth | c5266b5 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 120 | AU.addPreserved<MemoryDependenceWrapperPass>(); |
Chandler Carruth | bfe1f1c | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 121 | AU.addPreserved<ScalarEvolutionWrapperPass>(); |
Chandler Carruth | 9146833 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 122 | AU.addPreserved<SCEVAAWrapperPass>(); |
Dan Gohman | ad2afc2 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 123 | |
| 124 | FunctionPass::getAnalysisUsage(AU); |
| 125 | } |