| 1 | //=== lib/CodeGen/GlobalISel/MipsPostLegalizerCombiner.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 pass does combining of machine instructions at the generic MI level, |
| 10 | // after the legalizer. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "MCTargetDesc/MipsMCTargetDesc.h" |
| 15 | #include "Mips.h" |
| 16 | #include "MipsLegalizerInfo.h" |
| 17 | #include "MipsSubtarget.h" |
| 18 | #include "llvm/CodeGen/GlobalISel/Combiner.h" |
| 19 | #include "llvm/CodeGen/GlobalISel/CombinerHelper.h" |
| 20 | #include "llvm/CodeGen/GlobalISel/CombinerInfo.h" |
| 21 | #include "llvm/CodeGen/GlobalISel/GIMatchTableExecutorImpl.h" |
| 22 | #include "llvm/CodeGen/GlobalISel/GISelValueTracking.h" |
| 23 | #include "llvm/CodeGen/GlobalISel/MIPatternMatch.h" |
| 24 | #include "llvm/CodeGen/MachineDominators.h" |
| 25 | #include "llvm/CodeGen/MachineFunctionAnalysisManager.h" |
| 26 | #include "llvm/CodeGen/MachinePassManager.h" |
| 27 | #include "llvm/CodeGen/TargetPassConfig.h" |
| 28 | #include "llvm/IR/Analysis.h" |
| 29 | #include "llvm/Target/TargetMachine.h" |
| 30 | |
| 31 | #define GET_GICOMBINER_DEPS |
| 32 | #include "MipsGenPostLegalizeGICombiner.inc" |
| 33 | #undef GET_GICOMBINER_DEPS |
| 34 | |
| 35 | #define DEBUG_TYPE "mips-postlegalizer-combiner" |
| 36 | |
| 37 | using namespace llvm; |
| 38 | using namespace MIPatternMatch; |
| 39 | |
| 40 | namespace { |
| 41 | #define GET_GICOMBINER_TYPES |
| 42 | #include "MipsGenPostLegalizeGICombiner.inc" |
| 43 | #undef GET_GICOMBINER_TYPES |
| 44 | |
| 45 | class MipsPostLegalizerCombinerImpl : public Combiner { |
| 46 | protected: |
| 47 | const MipsPostLegalizerCombinerImplRuleConfig &RuleConfig; |
| 48 | const MipsSubtarget &STI; |
| 49 | const CombinerHelper Helper; |
| 50 | |
| 51 | public: |
| 52 | MipsPostLegalizerCombinerImpl( |
| 53 | MachineFunction &MF, CombinerInfo &CInfo, GISelValueTracking &VT, |
| 54 | GISelCSEInfo *CSEInfo, |
| 55 | const MipsPostLegalizerCombinerImplRuleConfig &RuleConfig, |
| 56 | const MipsSubtarget &STI, MachineDominatorTree *MDT, |
| 57 | const LegalizerInfo *LI); |
| 58 | |
| 59 | static const char *getName() { return "MipsPostLegalizerCombiner" ; } |
| 60 | |
| 61 | bool tryCombineAll(MachineInstr &I) const override; |
| 62 | |
| 63 | private: |
| 64 | #define GET_GICOMBINER_CLASS_MEMBERS |
| 65 | #include "MipsGenPostLegalizeGICombiner.inc" |
| 66 | #undef GET_GICOMBINER_CLASS_MEMBERS |
| 67 | }; |
| 68 | |
| 69 | #define GET_GICOMBINER_IMPL |
| 70 | #include "MipsGenPostLegalizeGICombiner.inc" |
| 71 | #undef GET_GICOMBINER_IMPL |
| 72 | |
| 73 | MipsPostLegalizerCombinerImpl::MipsPostLegalizerCombinerImpl( |
| 74 | MachineFunction &MF, CombinerInfo &CInfo, GISelValueTracking &VT, |
| 75 | GISelCSEInfo *CSEInfo, |
| 76 | const MipsPostLegalizerCombinerImplRuleConfig &RuleConfig, |
| 77 | const MipsSubtarget &STI, MachineDominatorTree *MDT, |
| 78 | const LegalizerInfo *LI) |
| 79 | : Combiner(MF, CInfo, &VT, CSEInfo), RuleConfig(RuleConfig), STI(STI), |
| 80 | Helper(Observer, B, /*IsPreLegalize*/ false, &VT, MDT, LI), |
| 81 | #define GET_GICOMBINER_CONSTRUCTOR_INITS |
| 82 | #include "MipsGenPostLegalizeGICombiner.inc" |
| 83 | #undef GET_GICOMBINER_CONSTRUCTOR_INITS |
| 84 | { |
| 85 | } |
| 86 | |
| 87 | // Pass boilerplate |
| 88 | // ================ |
| 89 | |
| 90 | class MipsPostLegalizerCombinerLegacy : public MachineFunctionPass { |
| 91 | public: |
| 92 | static char ID; |
| 93 | |
| 94 | MipsPostLegalizerCombinerLegacy(bool IsOptNone = false); |
| 95 | |
| 96 | StringRef getPassName() const override { return "MipsPostLegalizerCombiner" ; } |
| 97 | |
| 98 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 99 | |
| 100 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 101 | |
| 102 | private: |
| 103 | bool IsOptNone; |
| 104 | }; |
| 105 | } // end anonymous namespace |
| 106 | |
| 107 | void MipsPostLegalizerCombinerLegacy::getAnalysisUsage( |
| 108 | AnalysisUsage &AU) const { |
| 109 | AU.setPreservesCFG(); |
| 110 | getSelectionDAGFallbackAnalysisUsage(AU); |
| 111 | AU.addRequired<GISelValueTrackingAnalysisLegacy>(); |
| 112 | AU.addPreserved<GISelValueTrackingAnalysisLegacy>(); |
| 113 | if (!IsOptNone) { |
| 114 | AU.addRequired<MachineDominatorTreeWrapperPass>(); |
| 115 | } |
| 116 | MachineFunctionPass::getAnalysisUsage(AU); |
| 117 | } |
| 118 | |
| 119 | MipsPostLegalizerCombinerLegacy::MipsPostLegalizerCombinerLegacy(bool IsOptNone) |
| 120 | : MachineFunctionPass(ID), IsOptNone(IsOptNone) {} |
| 121 | |
| 122 | static bool |
| 123 | runCombinerOnMachineFunction(MachineFunction &MF, bool IsOptNone, |
| 124 | function_ref<bool()> ShouldSkip, |
| 125 | function_ref<GISelValueTracking *()> GetVT, |
| 126 | function_ref<MachineDominatorTree *()> GetMDT) { |
| 127 | if (MF.getProperties().hasFailedISel()) |
| 128 | return false; |
| 129 | const Function &F = MF.getFunction(); |
| 130 | bool EnableOpt = |
| 131 | MF.getTarget().getOptLevel() != CodeGenOptLevel::None && !ShouldSkip(); |
| 132 | |
| 133 | MipsPostLegalizerCombinerImplRuleConfig RuleConfig; |
| 134 | if (!RuleConfig.parseCommandLineOption()) |
| 135 | report_fatal_error(reason: "Invalid rule identifier" ); |
| 136 | |
| 137 | const MipsSubtarget &ST = MF.getSubtarget<MipsSubtarget>(); |
| 138 | const MipsLegalizerInfo *LI = |
| 139 | static_cast<const MipsLegalizerInfo *>(ST.getLegalizerInfo()); |
| 140 | |
| 141 | GISelValueTracking *VT = GetVT(); |
| 142 | MachineDominatorTree *MDT = IsOptNone ? nullptr : GetMDT(); |
| 143 | CombinerInfo CInfo(/*AllowIllegalOps*/ false, /*ShouldLegalizeIllegal*/ true, |
| 144 | LI, EnableOpt, F.hasOptSize(), F.hasMinSize()); |
| 145 | MipsPostLegalizerCombinerImpl Impl(MF, CInfo, *VT, /*CSEInfo*/ nullptr, |
| 146 | RuleConfig, ST, MDT, LI); |
| 147 | return Impl.combineMachineInstrs(); |
| 148 | } |
| 149 | |
| 150 | char MipsPostLegalizerCombinerLegacy::ID = 0; |
| 151 | INITIALIZE_PASS_BEGIN(MipsPostLegalizerCombinerLegacy, DEBUG_TYPE, |
| 152 | "Combine Mips machine instrs after legalization" , false, |
| 153 | false) |
| 154 | INITIALIZE_PASS_DEPENDENCY(GISelValueTrackingAnalysisLegacy) |
| 155 | INITIALIZE_PASS_END(MipsPostLegalizerCombinerLegacy, DEBUG_TYPE, |
| 156 | "Combine Mips machine instrs after legalization" , false, |
| 157 | false) |
| 158 | |
| 159 | FunctionPass *llvm::createMipsPostLegalizeCombinerLegacyPass(bool IsOptNone) { |
| 160 | return new MipsPostLegalizerCombinerLegacy(IsOptNone); |
| 161 | } |
| 162 | |
| 163 | bool MipsPostLegalizerCombinerLegacy::runOnMachineFunction( |
| 164 | MachineFunction &MF) { |
| 165 | return runCombinerOnMachineFunction( |
| 166 | MF, IsOptNone, ShouldSkip: [&]() { return skipFunction(F: MF.getFunction()); }, |
| 167 | GetVT: [&]() { |
| 168 | return &getAnalysis<GISelValueTrackingAnalysisLegacy>().get(MF); |
| 169 | }, |
| 170 | GetMDT: [&]() { |
| 171 | return &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree(); |
| 172 | }); |
| 173 | } |
| 174 | |
| 175 | PreservedAnalyses |
| 176 | MipsPostLegalizerCombinerPass::run(MachineFunction &MF, |
| 177 | MachineFunctionAnalysisManager &MFAM) { |
| 178 | bool Changed = runCombinerOnMachineFunction( |
| 179 | MF, IsOptNone, |
| 180 | ShouldSkip: [&]() { |
| 181 | return MF.getFunction().hasOptNone() || |
| 182 | shouldSkipOptimizationForOptBisect(IR: MF.getFunction()); |
| 183 | }, |
| 184 | GetVT: [&]() { return &MFAM.getResult<GISelValueTrackingAnalysis>(IR&: MF); }, |
| 185 | GetMDT: [&]() { return &MFAM.getResult<MachineDominatorTreeAnalysis>(IR&: MF); }); |
| 186 | if (!Changed) |
| 187 | return PreservedAnalyses::all(); |
| 188 | return getMachineFunctionPassPreservedAnalyses() |
| 189 | .preserveSet<CFGAnalyses>() |
| 190 | .preserve<GISelValueTrackingAnalysis>(); |
| 191 | } |
| 192 | |