|
| 1 | +//===--------------- RISCVPostLegalizerLowering.cpp -------------*- C++ -*-===// |
| 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 | +/// \file |
| 10 | +/// Post-legalization lowering for instructions. |
| 11 | +/// |
| 12 | +/// This is used to offload pattern matching from the selector. |
| 13 | +/// |
| 14 | +/// General optimization combines should be handled by either the |
| 15 | +/// RISCVPostLegalizerCombiner or the RISCVPreLegalizerCombiner. |
| 16 | +/// |
| 17 | +//===----------------------------------------------------------------------===// |
| 18 | + |
| 19 | +#include "RISCVSubtarget.h" |
| 20 | + |
| 21 | +#include "llvm/CodeGen/GlobalISel/Combiner.h" |
| 22 | +#include "llvm/CodeGen/GlobalISel/CombinerHelper.h" |
| 23 | +#include "llvm/CodeGen/GlobalISel/GIMatchTableExecutorImpl.h" |
| 24 | +#include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h" |
| 25 | +#include "llvm/CodeGen/MachineFunctionPass.h" |
| 26 | +#include "llvm/CodeGen/TargetPassConfig.h" |
| 27 | +#include "llvm/InitializePasses.h" |
| 28 | +#include "llvm/Support/Debug.h" |
| 29 | + |
| 30 | +#define GET_GICOMBINER_DEPS |
| 31 | +#include "RISCVGenPostLegalizeGILowering.inc" |
| 32 | +#undef GET_GICOMBINER_DEPS |
| 33 | + |
| 34 | +#define DEBUG_TYPE "riscv-postlegalizer-lowering" |
| 35 | + |
| 36 | +using namespace llvm; |
| 37 | + |
| 38 | +namespace { |
| 39 | + |
| 40 | +#define GET_GICOMBINER_TYPES |
| 41 | +#include "RISCVGenPostLegalizeGILowering.inc" |
| 42 | +#undef GET_GICOMBINER_TYPES |
| 43 | + |
| 44 | +class RISCVPostLegalizerLoweringImpl : public Combiner { |
| 45 | +protected: |
| 46 | + // TODO: Make CombinerHelper methods const. |
| 47 | + mutable CombinerHelper Helper; |
| 48 | + const RISCVPostLegalizerLoweringImplRuleConfig &RuleConfig; |
| 49 | + const RISCVSubtarget &STI; |
| 50 | + |
| 51 | +public: |
| 52 | + RISCVPostLegalizerLoweringImpl( |
| 53 | + MachineFunction &MF, CombinerInfo &CInfo, const TargetPassConfig *TPC, |
| 54 | + GISelCSEInfo *CSEInfo, |
| 55 | + const RISCVPostLegalizerLoweringImplRuleConfig &RuleConfig, |
| 56 | + const RISCVSubtarget &STI); |
| 57 | + |
| 58 | + static const char *getName() { return "RISCVPreLegalizerCombiner"; } |
| 59 | + |
| 60 | + bool tryCombineAll(MachineInstr &I) const override; |
| 61 | + |
| 62 | +private: |
| 63 | +#define GET_GICOMBINER_CLASS_MEMBERS |
| 64 | +#include "RISCVGenPostLegalizeGILowering.inc" |
| 65 | +#undef GET_GICOMBINER_CLASS_MEMBERS |
| 66 | +}; |
| 67 | + |
| 68 | +#define GET_GICOMBINER_IMPL |
| 69 | +#include "RISCVGenPostLegalizeGILowering.inc" |
| 70 | +#undef GET_GICOMBINER_IMPL |
| 71 | + |
| 72 | +RISCVPostLegalizerLoweringImpl::RISCVPostLegalizerLoweringImpl( |
| 73 | + MachineFunction &MF, CombinerInfo &CInfo, const TargetPassConfig *TPC, |
| 74 | + GISelCSEInfo *CSEInfo, |
| 75 | + const RISCVPostLegalizerLoweringImplRuleConfig &RuleConfig, |
| 76 | + const RISCVSubtarget &STI) |
| 77 | + : Combiner(MF, CInfo, TPC, /*KB*/ nullptr, CSEInfo), |
| 78 | + Helper(Observer, B, /*IsPreLegalize*/ true), RuleConfig(RuleConfig), |
| 79 | + STI(STI), |
| 80 | +#define GET_GICOMBINER_CONSTRUCTOR_INITS |
| 81 | +#include "RISCVGenPostLegalizeGILowering.inc" |
| 82 | +#undef GET_GICOMBINER_CONSTRUCTOR_INITS |
| 83 | +{ |
| 84 | +} |
| 85 | + |
| 86 | +class RISCVPostLegalizerLowering : public MachineFunctionPass { |
| 87 | +public: |
| 88 | + static char ID; |
| 89 | + |
| 90 | + RISCVPostLegalizerLowering(); |
| 91 | + |
| 92 | + StringRef getPassName() const override { |
| 93 | + return "RISCVPostLegalizerLowering"; |
| 94 | + } |
| 95 | + |
| 96 | + bool runOnMachineFunction(MachineFunction &MF) override; |
| 97 | + void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 98 | + |
| 99 | +private: |
| 100 | + RISCVPostLegalizerLoweringImplRuleConfig RuleConfig; |
| 101 | +}; |
| 102 | +} // end anonymous namespace |
| 103 | + |
| 104 | +void RISCVPostLegalizerLowering::getAnalysisUsage(AnalysisUsage &AU) const { |
| 105 | + AU.addRequired<TargetPassConfig>(); |
| 106 | + AU.setPreservesCFG(); |
| 107 | + getSelectionDAGFallbackAnalysisUsage(AU); |
| 108 | + MachineFunctionPass::getAnalysisUsage(AU); |
| 109 | +} |
| 110 | + |
| 111 | +RISCVPostLegalizerLowering::RISCVPostLegalizerLowering() |
| 112 | + : MachineFunctionPass(ID) { |
| 113 | + if (!RuleConfig.parseCommandLineOption()) |
| 114 | + report_fatal_error("Invalid rule identifier"); |
| 115 | +} |
| 116 | + |
| 117 | +bool RISCVPostLegalizerLowering::runOnMachineFunction(MachineFunction &MF) { |
| 118 | + if (MF.getProperties().hasProperty( |
| 119 | + MachineFunctionProperties::Property::FailedISel)) |
| 120 | + return false; |
| 121 | + assert(MF.getProperties().hasProperty( |
| 122 | + MachineFunctionProperties::Property::Legalized) && |
| 123 | + "Expected a legalized function?"); |
| 124 | + auto *TPC = &getAnalysis<TargetPassConfig>(); |
| 125 | + const Function &F = MF.getFunction(); |
| 126 | + |
| 127 | + const RISCVSubtarget &ST = MF.getSubtarget<RISCVSubtarget>(); |
| 128 | + CombinerInfo CInfo(/*AllowIllegalOps*/ true, /*ShouldLegalizeIllegal*/ false, |
| 129 | + /*LegalizerInfo*/ nullptr, /*OptEnabled=*/true, |
| 130 | + F.hasOptSize(), F.hasMinSize()); |
| 131 | + // Disable fixed-point iteration to reduce compile-time |
| 132 | + CInfo.MaxIterations = 1; |
| 133 | + CInfo.ObserverLvl = CombinerInfo::ObserverLevel::SinglePass; |
| 134 | + // PostLegalizerCombiner performs DCE, so a full DCE pass is unnecessary. |
| 135 | + CInfo.EnableFullDCE = false; |
| 136 | + RISCVPostLegalizerLoweringImpl Impl(MF, CInfo, TPC, /*CSEInfo*/ nullptr, |
| 137 | + RuleConfig, ST); |
| 138 | + return Impl.combineMachineInstrs(); |
| 139 | +} |
| 140 | + |
| 141 | +char RISCVPostLegalizerLowering::ID = 0; |
| 142 | +INITIALIZE_PASS_BEGIN(RISCVPostLegalizerLowering, DEBUG_TYPE, |
| 143 | + "Lower RISC-V MachineInstrs after legalization", false, |
| 144 | + false) |
| 145 | +INITIALIZE_PASS_DEPENDENCY(TargetPassConfig) |
| 146 | +INITIALIZE_PASS_END(RISCVPostLegalizerLowering, DEBUG_TYPE, |
| 147 | + "Lower RISC-V MachineInstrs after legalization", false, |
| 148 | + false) |
| 149 | + |
| 150 | +namespace llvm { |
| 151 | +FunctionPass *createRISCVPostLegalizerLowering() { |
| 152 | + return new RISCVPostLegalizerLowering(); |
| 153 | +} |
| 154 | +} // end namespace llvm |
0 commit comments