| 1 | //=== WebAssemblyPreLegalizerCombiner.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 | // before the legalizer. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "WebAssembly.h" |
| 15 | #include "WebAssemblySubtarget.h" |
| 16 | #include "llvm/CodeGen/GlobalISel/CSEInfo.h" |
| 17 | #include "llvm/CodeGen/GlobalISel/Combiner.h" |
| 18 | #include "llvm/CodeGen/GlobalISel/CombinerHelper.h" |
| 19 | #include "llvm/CodeGen/GlobalISel/CombinerInfo.h" |
| 20 | #include "llvm/CodeGen/GlobalISel/GIMatchTableExecutorImpl.h" |
| 21 | #include "llvm/CodeGen/GlobalISel/GISelValueTracking.h" |
| 22 | #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h" |
| 23 | #include "llvm/CodeGen/MachineDominators.h" |
| 24 | #include "llvm/CodeGen/MachineFunction.h" |
| 25 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 26 | #include "llvm/CodeGen/TargetPassConfig.h" |
| 27 | #include "llvm/Target/TargetMachine.h" |
| 28 | |
| 29 | #define GET_GICOMBINER_DEPS |
| 30 | #include "WebAssemblyGenPreLegalizeGICombiner.inc" |
| 31 | #undef GET_GICOMBINER_DEPS |
| 32 | |
| 33 | #define DEBUG_TYPE "wasm-prelegalizer-combiner" |
| 34 | |
| 35 | using namespace llvm; |
| 36 | |
| 37 | namespace { |
| 38 | |
| 39 | #define GET_GICOMBINER_TYPES |
| 40 | #include "WebAssemblyGenPreLegalizeGICombiner.inc" |
| 41 | #undef GET_GICOMBINER_TYPES |
| 42 | |
| 43 | class WebAssemblyPreLegalizerCombinerImpl : public Combiner { |
| 44 | protected: |
| 45 | const CombinerHelper Helper; |
| 46 | const WebAssemblyPreLegalizerCombinerImplRuleConfig &RuleConfig; |
| 47 | const WebAssemblySubtarget &STI; |
| 48 | |
| 49 | public: |
| 50 | WebAssemblyPreLegalizerCombinerImpl( |
| 51 | MachineFunction &MF, CombinerInfo &CInfo, const TargetPassConfig *TPC, |
| 52 | GISelValueTracking &VT, GISelCSEInfo *CSEInfo, |
| 53 | const WebAssemblyPreLegalizerCombinerImplRuleConfig &RuleConfig, |
| 54 | const WebAssemblySubtarget &STI, MachineDominatorTree *MDT, |
| 55 | const LegalizerInfo *LI); |
| 56 | |
| 57 | static const char *getName() { return "WebAssembly00PreLegalizerCombiner" ; } |
| 58 | |
| 59 | bool tryCombineAll(MachineInstr &I) const override; |
| 60 | |
| 61 | private: |
| 62 | #define GET_GICOMBINER_CLASS_MEMBERS |
| 63 | #include "WebAssemblyGenPreLegalizeGICombiner.inc" |
| 64 | #undef GET_GICOMBINER_CLASS_MEMBERS |
| 65 | }; |
| 66 | |
| 67 | #define GET_GICOMBINER_IMPL |
| 68 | #include "WebAssemblyGenPreLegalizeGICombiner.inc" |
| 69 | #undef GET_GICOMBINER_IMPL |
| 70 | |
| 71 | WebAssemblyPreLegalizerCombinerImpl::WebAssemblyPreLegalizerCombinerImpl( |
| 72 | MachineFunction &MF, CombinerInfo &CInfo, const TargetPassConfig *TPC, |
| 73 | GISelValueTracking &VT, GISelCSEInfo *CSEInfo, |
| 74 | const WebAssemblyPreLegalizerCombinerImplRuleConfig &RuleConfig, |
| 75 | const WebAssemblySubtarget &STI, MachineDominatorTree *MDT, |
| 76 | const LegalizerInfo *LI) |
| 77 | : Combiner(MF, CInfo, TPC, &VT, CSEInfo), |
| 78 | Helper(Observer, B, /*IsPreLegalize*/ true, &VT, MDT, LI), |
| 79 | RuleConfig(RuleConfig), STI(STI), |
| 80 | #define GET_GICOMBINER_CONSTRUCTOR_INITS |
| 81 | #include "WebAssemblyGenPreLegalizeGICombiner.inc" |
| 82 | #undef GET_GICOMBINER_CONSTRUCTOR_INITS |
| 83 | { |
| 84 | } |
| 85 | |
| 86 | // Pass boilerplate |
| 87 | // ================ |
| 88 | |
| 89 | class WebAssemblyPreLegalizerCombiner : public MachineFunctionPass { |
| 90 | public: |
| 91 | static char ID; |
| 92 | |
| 93 | WebAssemblyPreLegalizerCombiner(); |
| 94 | |
| 95 | StringRef getPassName() const override { |
| 96 | return "WebAssemblyPreLegalizerCombiner" ; |
| 97 | } |
| 98 | |
| 99 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 100 | |
| 101 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 102 | |
| 103 | private: |
| 104 | WebAssemblyPreLegalizerCombinerImplRuleConfig RuleConfig; |
| 105 | }; |
| 106 | } // end anonymous namespace |
| 107 | |
| 108 | void WebAssemblyPreLegalizerCombiner::getAnalysisUsage( |
| 109 | AnalysisUsage &AU) const { |
| 110 | AU.addRequired<TargetPassConfig>(); |
| 111 | AU.setPreservesCFG(); |
| 112 | getSelectionDAGFallbackAnalysisUsage(AU); |
| 113 | AU.addRequired<GISelValueTrackingAnalysisLegacy>(); |
| 114 | AU.addPreserved<GISelValueTrackingAnalysisLegacy>(); |
| 115 | AU.addRequired<MachineDominatorTreeWrapperPass>(); |
| 116 | AU.addPreserved<MachineDominatorTreeWrapperPass>(); |
| 117 | AU.addRequired<GISelCSEAnalysisWrapperPass>(); |
| 118 | AU.addPreserved<GISelCSEAnalysisWrapperPass>(); |
| 119 | MachineFunctionPass::getAnalysisUsage(AU); |
| 120 | } |
| 121 | |
| 122 | WebAssemblyPreLegalizerCombiner::WebAssemblyPreLegalizerCombiner() |
| 123 | : MachineFunctionPass(ID) { |
| 124 | if (!RuleConfig.parseCommandLineOption()) |
| 125 | report_fatal_error(reason: "Invalid rule identifier" ); |
| 126 | } |
| 127 | |
| 128 | bool WebAssemblyPreLegalizerCombiner::runOnMachineFunction( |
| 129 | MachineFunction &MF) { |
| 130 | if (MF.getProperties().hasFailedISel()) |
| 131 | return false; |
| 132 | auto &TPC = getAnalysis<TargetPassConfig>(); |
| 133 | |
| 134 | // Enable CSE. |
| 135 | GISelCSEAnalysisWrapper &Wrapper = |
| 136 | getAnalysis<GISelCSEAnalysisWrapperPass>().getCSEWrapper(); |
| 137 | auto *CSEInfo = &Wrapper.get(CSEOpt: TPC.getCSEConfig()); |
| 138 | |
| 139 | const WebAssemblySubtarget &ST = MF.getSubtarget<WebAssemblySubtarget>(); |
| 140 | const auto *LI = ST.getLegalizerInfo(); |
| 141 | |
| 142 | const Function &F = MF.getFunction(); |
| 143 | bool EnableOpt = |
| 144 | MF.getTarget().getOptLevel() != CodeGenOptLevel::None && !skipFunction(F); |
| 145 | GISelValueTracking *VT = |
| 146 | &getAnalysis<GISelValueTrackingAnalysisLegacy>().get(MF); |
| 147 | MachineDominatorTree *MDT = |
| 148 | &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree(); |
| 149 | CombinerInfo CInfo(/*AllowIllegalOps*/ true, /*ShouldLegalizeIllegal*/ false, |
| 150 | /*LegalizerInfo*/ nullptr, EnableOpt, F.hasOptSize(), |
| 151 | F.hasMinSize()); |
| 152 | // Disable fixed-point iteration to reduce compile-time |
| 153 | CInfo.MaxIterations = 1; |
| 154 | CInfo.ObserverLvl = CombinerInfo::ObserverLevel::SinglePass; |
| 155 | // This is the first Combiner, so the input IR might contain dead |
| 156 | // instructions. |
| 157 | CInfo.EnableFullDCE = true; |
| 158 | WebAssemblyPreLegalizerCombinerImpl Impl(MF, CInfo, &TPC, *VT, CSEInfo, |
| 159 | RuleConfig, ST, MDT, LI); |
| 160 | return Impl.combineMachineInstrs(); |
| 161 | } |
| 162 | |
| 163 | char WebAssemblyPreLegalizerCombiner::ID = 0; |
| 164 | INITIALIZE_PASS_BEGIN(WebAssemblyPreLegalizerCombiner, DEBUG_TYPE, |
| 165 | "Combine WebAssembly machine instrs before legalization" , |
| 166 | false, false) |
| 167 | INITIALIZE_PASS_DEPENDENCY(TargetPassConfig) |
| 168 | INITIALIZE_PASS_DEPENDENCY(GISelValueTrackingAnalysisLegacy) |
| 169 | INITIALIZE_PASS_DEPENDENCY(GISelCSEAnalysisWrapperPass) |
| 170 | INITIALIZE_PASS_END(WebAssemblyPreLegalizerCombiner, DEBUG_TYPE, |
| 171 | "Combine WebAssembly machine instrs before legalization" , |
| 172 | false, false) |
| 173 | |
| 174 | FunctionPass *llvm::createWebAssemblyPreLegalizerCombiner() { |
| 175 | return new WebAssemblyPreLegalizerCombiner(); |
| 176 | } |
| 177 | |