Dan Gohman | 5ea74d5 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 1 | //===-- MachineFunctionPass.cpp -------------------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 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 |
Dan Gohman | 5ea74d5 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file contains the definitions of the MachineFunctionPass members. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Dan Gohman | 5ea74d5 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 13 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Chandler Carruth | b81dfa6 | 2015-01-28 04:57:56 +0000 | [diff] [blame] | 14 | #include "llvm/Analysis/AliasAnalysis.h" |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/BasicAliasAnalysis.h" |
Chandler Carruth | b81dfa6 | 2015-01-28 04:57:56 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/DominanceFrontier.h" |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/GlobalsModRef.h" |
Chandler Carruth | b81dfa6 | 2015-01-28 04:57:56 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/IVUsers.h" |
| 19 | #include "llvm/Analysis/LoopInfo.h" |
Chandler Carruth | b81dfa6 | 2015-01-28 04:57:56 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/MemoryDependenceAnalysis.h" |
| 21 | #include "llvm/Analysis/ScalarEvolution.h" |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" |
Derek Schuff | ad154c8 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineFunction.h" |
Matthias Braun | 733fe36 | 2016-08-24 01:52:46 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Jessica Paquette | 54fbfae | 2018-09-10 22:24:10 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h" |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/Passes.h" |
Chandler Carruth | b81dfa6 | 2015-01-28 04:57:56 +0000 | [diff] [blame] | 27 | #include "llvm/IR/Dominators.h" |
| 28 | #include "llvm/IR/Function.h" |
Derek Schuff | ad154c8 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 29 | |
Dan Gohman | 5ea74d5 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 30 | using namespace llvm; |
Jessica Paquette | 54fbfae | 2018-09-10 22:24:10 +0000 | [diff] [blame] | 31 | using namespace ore; |
Dan Gohman | 5ea74d5 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 32 | |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 33 | Pass *MachineFunctionPass::createPrinterPass(raw_ostream &O, |
| 34 | const std::string &Banner) const { |
| 35 | return createMachineFunctionPrinterPass(O, Banner); |
| 36 | } |
| 37 | |
Dan Gohman | 5ea74d5 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 38 | bool MachineFunctionPass::runOnFunction(Function &F) { |
| 39 | // Do not codegen any 'available_externally' functions at all, they have |
| 40 | // definitions outside the translation unit. |
Serge Pavlov | ed5eb93 | 2017-01-15 10:23:18 +0000 | [diff] [blame] | 41 | if (F.hasAvailableExternallyLinkage()) |
Dan Gohman | 5ea74d5 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 42 | return false; |
| 43 | |
Matthias Braun | 733fe36 | 2016-08-24 01:52:46 +0000 | [diff] [blame] | 44 | MachineModuleInfo &MMI = getAnalysis<MachineModuleInfo>(); |
Matthias Braun | 7bda195 | 2017-06-06 00:44:35 +0000 | [diff] [blame] | 45 | MachineFunction &MF = MMI.getOrCreateMachineFunction(F); |
Matthias Braun | 733fe36 | 2016-08-24 01:52:46 +0000 | [diff] [blame] | 46 | |
Derek Schuff | ad154c8 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 47 | MachineFunctionProperties &MFProps = MF.getProperties(); |
| 48 | |
Derek Schuff | 07636cd | 2016-03-29 20:28:20 +0000 | [diff] [blame] | 49 | #ifndef NDEBUG |
| 50 | if (!MFProps.verifyRequiredProperties(RequiredProperties)) { |
| 51 | errs() << "MachineFunctionProperties required by " << getPassName() |
| 52 | << " pass are not met by function " << F.getName() << ".\n" |
| 53 | << "Required properties: "; |
Matthias Braun | a7d6fc9 | 2016-08-19 22:31:45 +0000 | [diff] [blame] | 54 | RequiredProperties.print(errs()); |
Derek Schuff | 07636cd | 2016-03-29 20:28:20 +0000 | [diff] [blame] | 55 | errs() << "\nCurrent properties: "; |
| 56 | MFProps.print(errs()); |
| 57 | errs() << "\n"; |
| 58 | llvm_unreachable("MachineFunctionProperties check failed"); |
| 59 | } |
| 60 | #endif |
Jessica Paquette | 54fbfae | 2018-09-10 22:24:10 +0000 | [diff] [blame] | 61 | // Collect the MI count of the function before the pass. |
| 62 | unsigned CountBefore, CountAfter; |
| 63 | |
| 64 | // Check if the user asked for size remarks. |
| 65 | bool ShouldEmitSizeRemarks = |
| 66 | F.getParent()->shouldEmitInstrCountChangedRemark(); |
| 67 | |
| 68 | // If we want size remarks, collect the number of MachineInstrs in our |
| 69 | // MachineFunction before the pass runs. |
| 70 | if (ShouldEmitSizeRemarks) |
| 71 | CountBefore = MF.getInstructionCount(); |
Derek Schuff | ad154c8 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 72 | |
| 73 | bool RV = runOnMachineFunction(MF); |
| 74 | |
Jessica Paquette | 54fbfae | 2018-09-10 22:24:10 +0000 | [diff] [blame] | 75 | if (ShouldEmitSizeRemarks) { |
| 76 | // We wanted size remarks. Check if there was a change to the number of |
| 77 | // MachineInstrs in the module. Emit a remark if there was a change. |
| 78 | CountAfter = MF.getInstructionCount(); |
| 79 | if (CountBefore != CountAfter) { |
| 80 | MachineOptimizationRemarkEmitter MORE(MF, nullptr); |
| 81 | MORE.emit([&]() { |
| 82 | int64_t Delta = static_cast<int64_t>(CountAfter) - |
| 83 | static_cast<int64_t>(CountBefore); |
| 84 | MachineOptimizationRemarkAnalysis R("size-info", "FunctionMISizeChange", |
| 85 | MF.getFunction().getSubprogram(), |
| 86 | &MF.front()); |
| 87 | R << NV("Pass", getPassName()) |
| 88 | << ": Function: " << NV("Function", F.getName()) << ": " |
| 89 | << "MI Instruction count changed from " |
| 90 | << NV("MIInstrsBefore", CountBefore) << " to " |
| 91 | << NV("MIInstrsAfter", CountAfter) |
| 92 | << "; Delta: " << NV("Delta", Delta); |
| 93 | return R; |
| 94 | }); |
| 95 | } |
| 96 | } |
| 97 | |
Derek Schuff | ad154c8 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 98 | MFProps.set(SetProperties); |
Quentin Colombet | c437aa9 | 2016-08-26 22:09:08 +0000 | [diff] [blame] | 99 | MFProps.reset(ClearedProperties); |
Derek Schuff | ad154c8 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 100 | return RV; |
Dan Gohman | 5ea74d5 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void MachineFunctionPass::getAnalysisUsage(AnalysisUsage &AU) const { |
Matthias Braun | 733fe36 | 2016-08-24 01:52:46 +0000 | [diff] [blame] | 104 | AU.addRequired<MachineModuleInfo>(); |
| 105 | AU.addPreserved<MachineModuleInfo>(); |
Dan Gohman | 5ea74d5 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 106 | |
| 107 | // MachineFunctionPass preserves all LLVM IR passes, but there's no |
| 108 | // high-level way to express this. Instead, just list a bunch of |
Dan Gohman | 0998427 | 2009-10-08 17:00:02 +0000 | [diff] [blame] | 109 | // passes explicitly. This does not include setPreservesCFG, |
| 110 | // because CodeGen overloads that to mean preserving the MachineBasicBlock |
| 111 | // CFG in addition to the LLVM IR CFG. |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 112 | AU.addPreserved<BasicAAWrapperPass>(); |
Hongbin Zheng | 751337f | 2016-02-25 17:54:15 +0000 | [diff] [blame] | 113 | AU.addPreserved<DominanceFrontierWrapperPass>(); |
Chandler Carruth | b81dfa6 | 2015-01-28 04:57:56 +0000 | [diff] [blame] | 114 | AU.addPreserved<DominatorTreeWrapperPass>(); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 115 | AU.addPreserved<AAResultsWrapperPass>(); |
| 116 | AU.addPreserved<GlobalsAAWrapperPass>(); |
Dehao Chen | 1a44452 | 2016-07-16 22:51:33 +0000 | [diff] [blame] | 117 | AU.addPreserved<IVUsersWrapperPass>(); |
Chandler Carruth | b81dfa6 | 2015-01-28 04:57:56 +0000 | [diff] [blame] | 118 | AU.addPreserved<LoopInfoWrapperPass>(); |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 119 | AU.addPreserved<MemoryDependenceWrapperPass>(); |
Chandler Carruth | 2f1fd16 | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 120 | AU.addPreserved<ScalarEvolutionWrapperPass>(); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 121 | AU.addPreserved<SCEVAAWrapperPass>(); |
Dan Gohman | 5ea74d5 | 2009-07-31 18:16:33 +0000 | [diff] [blame] | 122 | |
| 123 | FunctionPass::getAnalysisUsage(AU); |
| 124 | } |