Path: blob/main/contrib/llvm-project/llvm/lib/Target/RISCV/GISel/RISCVPreLegalizerCombiner.cpp
35294 views
//=== RISCVPreLegalizerCombiner.cpp ---------------------------------------===//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// This pass does combining of machine instructions at the generic MI level,9// before the legalizer.10//11//===----------------------------------------------------------------------===//1213#include "RISCVSubtarget.h"14#include "llvm/CodeGen/GlobalISel/CSEInfo.h"15#include "llvm/CodeGen/GlobalISel/Combiner.h"16#include "llvm/CodeGen/GlobalISel/CombinerHelper.h"17#include "llvm/CodeGen/GlobalISel/CombinerInfo.h"18#include "llvm/CodeGen/GlobalISel/GIMatchTableExecutorImpl.h"19#include "llvm/CodeGen/GlobalISel/GISelKnownBits.h"20#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"21#include "llvm/CodeGen/MachineDominators.h"22#include "llvm/CodeGen/MachineFunction.h"23#include "llvm/CodeGen/MachineFunctionPass.h"24#include "llvm/CodeGen/MachineRegisterInfo.h"25#include "llvm/CodeGen/TargetPassConfig.h"2627#define GET_GICOMBINER_DEPS28#include "RISCVGenPreLegalizeGICombiner.inc"29#undef GET_GICOMBINER_DEPS3031#define DEBUG_TYPE "riscv-prelegalizer-combiner"3233using namespace llvm;3435namespace {3637#define GET_GICOMBINER_TYPES38#include "RISCVGenPreLegalizeGICombiner.inc"39#undef GET_GICOMBINER_TYPES4041class RISCVPreLegalizerCombinerImpl : public Combiner {42protected:43// TODO: Make CombinerHelper methods const.44mutable CombinerHelper Helper;45const RISCVPreLegalizerCombinerImplRuleConfig &RuleConfig;46const RISCVSubtarget &STI;4748public:49RISCVPreLegalizerCombinerImpl(50MachineFunction &MF, CombinerInfo &CInfo, const TargetPassConfig *TPC,51GISelKnownBits &KB, GISelCSEInfo *CSEInfo,52const RISCVPreLegalizerCombinerImplRuleConfig &RuleConfig,53const RISCVSubtarget &STI, MachineDominatorTree *MDT,54const LegalizerInfo *LI);5556static const char *getName() { return "RISCV00PreLegalizerCombiner"; }5758bool tryCombineAll(MachineInstr &I) const override;5960private:61#define GET_GICOMBINER_CLASS_MEMBERS62#include "RISCVGenPreLegalizeGICombiner.inc"63#undef GET_GICOMBINER_CLASS_MEMBERS64};6566#define GET_GICOMBINER_IMPL67#include "RISCVGenPreLegalizeGICombiner.inc"68#undef GET_GICOMBINER_IMPL6970RISCVPreLegalizerCombinerImpl::RISCVPreLegalizerCombinerImpl(71MachineFunction &MF, CombinerInfo &CInfo, const TargetPassConfig *TPC,72GISelKnownBits &KB, GISelCSEInfo *CSEInfo,73const RISCVPreLegalizerCombinerImplRuleConfig &RuleConfig,74const RISCVSubtarget &STI, MachineDominatorTree *MDT,75const LegalizerInfo *LI)76: Combiner(MF, CInfo, TPC, &KB, CSEInfo),77Helper(Observer, B, /*IsPreLegalize*/ true, &KB, MDT, LI),78RuleConfig(RuleConfig), STI(STI),79#define GET_GICOMBINER_CONSTRUCTOR_INITS80#include "RISCVGenPreLegalizeGICombiner.inc"81#undef GET_GICOMBINER_CONSTRUCTOR_INITS82{83}8485// Pass boilerplate86// ================8788class RISCVPreLegalizerCombiner : public MachineFunctionPass {89public:90static char ID;9192RISCVPreLegalizerCombiner();9394StringRef getPassName() const override { return "RISCVPreLegalizerCombiner"; }9596bool runOnMachineFunction(MachineFunction &MF) override;9798void getAnalysisUsage(AnalysisUsage &AU) const override;99100private:101RISCVPreLegalizerCombinerImplRuleConfig RuleConfig;102};103} // end anonymous namespace104105void RISCVPreLegalizerCombiner::getAnalysisUsage(AnalysisUsage &AU) const {106AU.addRequired<TargetPassConfig>();107AU.setPreservesCFG();108getSelectionDAGFallbackAnalysisUsage(AU);109AU.addRequired<GISelKnownBitsAnalysis>();110AU.addPreserved<GISelKnownBitsAnalysis>();111AU.addRequired<MachineDominatorTreeWrapperPass>();112AU.addPreserved<MachineDominatorTreeWrapperPass>();113AU.addRequired<GISelCSEAnalysisWrapperPass>();114AU.addPreserved<GISelCSEAnalysisWrapperPass>();115MachineFunctionPass::getAnalysisUsage(AU);116}117118RISCVPreLegalizerCombiner::RISCVPreLegalizerCombiner()119: MachineFunctionPass(ID) {120initializeRISCVPreLegalizerCombinerPass(*PassRegistry::getPassRegistry());121122if (!RuleConfig.parseCommandLineOption())123report_fatal_error("Invalid rule identifier");124}125126bool RISCVPreLegalizerCombiner::runOnMachineFunction(MachineFunction &MF) {127if (MF.getProperties().hasProperty(128MachineFunctionProperties::Property::FailedISel))129return false;130auto &TPC = getAnalysis<TargetPassConfig>();131132// Enable CSE.133GISelCSEAnalysisWrapper &Wrapper =134getAnalysis<GISelCSEAnalysisWrapperPass>().getCSEWrapper();135auto *CSEInfo = &Wrapper.get(TPC.getCSEConfig());136137const RISCVSubtarget &ST = MF.getSubtarget<RISCVSubtarget>();138const auto *LI = ST.getLegalizerInfo();139140const Function &F = MF.getFunction();141bool EnableOpt =142MF.getTarget().getOptLevel() != CodeGenOptLevel::None && !skipFunction(F);143GISelKnownBits *KB = &getAnalysis<GISelKnownBitsAnalysis>().get(MF);144MachineDominatorTree *MDT =145&getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();146CombinerInfo CInfo(/*AllowIllegalOps*/ true, /*ShouldLegalizeIllegal*/ false,147/*LegalizerInfo*/ nullptr, EnableOpt, F.hasOptSize(),148F.hasMinSize());149RISCVPreLegalizerCombinerImpl Impl(MF, CInfo, &TPC, *KB, CSEInfo, RuleConfig,150ST, MDT, LI);151return Impl.combineMachineInstrs();152}153154char RISCVPreLegalizerCombiner::ID = 0;155INITIALIZE_PASS_BEGIN(RISCVPreLegalizerCombiner, DEBUG_TYPE,156"Combine RISC-V machine instrs before legalization", false,157false)158INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)159INITIALIZE_PASS_DEPENDENCY(GISelKnownBitsAnalysis)160INITIALIZE_PASS_DEPENDENCY(GISelCSEAnalysisWrapperPass)161INITIALIZE_PASS_END(RISCVPreLegalizerCombiner, DEBUG_TYPE,162"Combine RISC-V machine instrs before legalization", false,163false)164165namespace llvm {166FunctionPass *createRISCVPreLegalizerCombiner() {167return new RISCVPreLegalizerCombiner();168}169} // end namespace llvm170171172