Path: blob/main/contrib/llvm-project/llvm/lib/Target/RISCV/GISel/RISCVPostLegalizerCombiner.cpp
35294 views
//=== RISCVPostLegalizerCombiner.cpp --------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7///8/// \file9/// Post-legalization combines on generic MachineInstrs.10///11/// The combines here must preserve instruction legality.12///13/// Combines which don't rely on instruction legality should go in the14/// RISCVPreLegalizerCombiner.15///16//===----------------------------------------------------------------------===//1718#include "RISCVTargetMachine.h"19#include "llvm/CodeGen/GlobalISel/CSEInfo.h"20#include "llvm/CodeGen/GlobalISel/Combiner.h"21#include "llvm/CodeGen/GlobalISel/CombinerHelper.h"22#include "llvm/CodeGen/GlobalISel/CombinerInfo.h"23#include "llvm/CodeGen/GlobalISel/GIMatchTableExecutorImpl.h"24#include "llvm/CodeGen/GlobalISel/GISelKnownBits.h"25#include "llvm/CodeGen/GlobalISel/GenericMachineInstrs.h"26#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"27#include "llvm/CodeGen/MachineDominators.h"28#include "llvm/CodeGen/MachineFunctionPass.h"29#include "llvm/CodeGen/MachineRegisterInfo.h"30#include "llvm/CodeGen/TargetPassConfig.h"3132#define GET_GICOMBINER_DEPS33#include "RISCVGenPostLegalizeGICombiner.inc"34#undef GET_GICOMBINER_DEPS3536#define DEBUG_TYPE "riscv-postlegalizer-combiner"3738using namespace llvm;3940namespace {4142#define GET_GICOMBINER_TYPES43#include "RISCVGenPostLegalizeGICombiner.inc"44#undef GET_GICOMBINER_TYPES4546class RISCVPostLegalizerCombinerImpl : public Combiner {47protected:48// TODO: Make CombinerHelper methods const.49mutable CombinerHelper Helper;50const RISCVPostLegalizerCombinerImplRuleConfig &RuleConfig;51const RISCVSubtarget &STI;5253public:54RISCVPostLegalizerCombinerImpl(55MachineFunction &MF, CombinerInfo &CInfo, const TargetPassConfig *TPC,56GISelKnownBits &KB, GISelCSEInfo *CSEInfo,57const RISCVPostLegalizerCombinerImplRuleConfig &RuleConfig,58const RISCVSubtarget &STI, MachineDominatorTree *MDT,59const LegalizerInfo *LI);6061static const char *getName() { return "RISCVPostLegalizerCombiner"; }6263bool tryCombineAll(MachineInstr &I) const override;6465private:66#define GET_GICOMBINER_CLASS_MEMBERS67#include "RISCVGenPostLegalizeGICombiner.inc"68#undef GET_GICOMBINER_CLASS_MEMBERS69};7071#define GET_GICOMBINER_IMPL72#include "RISCVGenPostLegalizeGICombiner.inc"73#undef GET_GICOMBINER_IMPL7475RISCVPostLegalizerCombinerImpl::RISCVPostLegalizerCombinerImpl(76MachineFunction &MF, CombinerInfo &CInfo, const TargetPassConfig *TPC,77GISelKnownBits &KB, GISelCSEInfo *CSEInfo,78const RISCVPostLegalizerCombinerImplRuleConfig &RuleConfig,79const RISCVSubtarget &STI, MachineDominatorTree *MDT,80const LegalizerInfo *LI)81: Combiner(MF, CInfo, TPC, &KB, CSEInfo),82Helper(Observer, B, /*IsPreLegalize*/ false, &KB, MDT, LI),83RuleConfig(RuleConfig), STI(STI),84#define GET_GICOMBINER_CONSTRUCTOR_INITS85#include "RISCVGenPostLegalizeGICombiner.inc"86#undef GET_GICOMBINER_CONSTRUCTOR_INITS87{88}8990class RISCVPostLegalizerCombiner : public MachineFunctionPass {91public:92static char ID;9394RISCVPostLegalizerCombiner();9596StringRef getPassName() const override {97return "RISCVPostLegalizerCombiner";98}99100bool runOnMachineFunction(MachineFunction &MF) override;101void getAnalysisUsage(AnalysisUsage &AU) const override;102103private:104RISCVPostLegalizerCombinerImplRuleConfig RuleConfig;105};106} // end anonymous namespace107108void RISCVPostLegalizerCombiner::getAnalysisUsage(AnalysisUsage &AU) const {109AU.addRequired<TargetPassConfig>();110AU.setPreservesCFG();111getSelectionDAGFallbackAnalysisUsage(AU);112AU.addRequired<GISelKnownBitsAnalysis>();113AU.addPreserved<GISelKnownBitsAnalysis>();114AU.addRequired<MachineDominatorTreeWrapperPass>();115AU.addPreserved<MachineDominatorTreeWrapperPass>();116AU.addRequired<GISelCSEAnalysisWrapperPass>();117AU.addPreserved<GISelCSEAnalysisWrapperPass>();118MachineFunctionPass::getAnalysisUsage(AU);119}120121RISCVPostLegalizerCombiner::RISCVPostLegalizerCombiner()122: MachineFunctionPass(ID) {123initializeRISCVPostLegalizerCombinerPass(*PassRegistry::getPassRegistry());124125if (!RuleConfig.parseCommandLineOption())126report_fatal_error("Invalid rule identifier");127}128129bool RISCVPostLegalizerCombiner::runOnMachineFunction(MachineFunction &MF) {130if (MF.getProperties().hasProperty(131MachineFunctionProperties::Property::FailedISel))132return false;133assert(MF.getProperties().hasProperty(134MachineFunctionProperties::Property::Legalized) &&135"Expected a legalized function?");136auto *TPC = &getAnalysis<TargetPassConfig>();137const Function &F = MF.getFunction();138bool EnableOpt =139MF.getTarget().getOptLevel() != CodeGenOptLevel::None && !skipFunction(F);140141const RISCVSubtarget &ST = MF.getSubtarget<RISCVSubtarget>();142const auto *LI = ST.getLegalizerInfo();143144GISelKnownBits *KB = &getAnalysis<GISelKnownBitsAnalysis>().get(MF);145MachineDominatorTree *MDT =146&getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();147GISelCSEAnalysisWrapper &Wrapper =148getAnalysis<GISelCSEAnalysisWrapperPass>().getCSEWrapper();149auto *CSEInfo = &Wrapper.get(TPC->getCSEConfig());150151CombinerInfo CInfo(/*AllowIllegalOps*/ true, /*ShouldLegalizeIllegal*/ false,152/*LegalizerInfo*/ nullptr, EnableOpt, F.hasOptSize(),153F.hasMinSize());154RISCVPostLegalizerCombinerImpl Impl(MF, CInfo, TPC, *KB, CSEInfo,155RuleConfig, ST, MDT, LI);156return Impl.combineMachineInstrs();157}158159char RISCVPostLegalizerCombiner::ID = 0;160INITIALIZE_PASS_BEGIN(RISCVPostLegalizerCombiner, DEBUG_TYPE,161"Combine RISC-V MachineInstrs after legalization", false,162false)163INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)164INITIALIZE_PASS_DEPENDENCY(GISelKnownBitsAnalysis)165INITIALIZE_PASS_END(RISCVPostLegalizerCombiner, DEBUG_TYPE,166"Combine RISC-V MachineInstrs after legalization", false,167false)168169namespace llvm {170FunctionPass *createRISCVPostLegalizerCombiner() {171return new RISCVPostLegalizerCombiner();172}173} // end namespace llvm174175176