Path: blob/main/contrib/llvm-project/llvm/lib/Target/RISCV/GISel/RISCVO0PreLegalizerCombiner.cpp
35294 views
//=== RISCVO0PreLegalizerCombiner.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/Combiner.h"15#include "llvm/CodeGen/GlobalISel/CombinerHelper.h"16#include "llvm/CodeGen/GlobalISel/CombinerInfo.h"17#include "llvm/CodeGen/GlobalISel/GIMatchTableExecutorImpl.h"18#include "llvm/CodeGen/GlobalISel/GISelKnownBits.h"19#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"20#include "llvm/CodeGen/MachineDominators.h"21#include "llvm/CodeGen/MachineFunction.h"22#include "llvm/CodeGen/MachineFunctionPass.h"23#include "llvm/CodeGen/MachineRegisterInfo.h"24#include "llvm/CodeGen/TargetPassConfig.h"2526#define GET_GICOMBINER_DEPS27#include "RISCVGenO0PreLegalizeGICombiner.inc"28#undef GET_GICOMBINER_DEPS2930#define DEBUG_TYPE "riscv-O0-prelegalizer-combiner"3132using namespace llvm;3334namespace {35#define GET_GICOMBINER_TYPES36#include "RISCVGenO0PreLegalizeGICombiner.inc"37#undef GET_GICOMBINER_TYPES3839class RISCVO0PreLegalizerCombinerImpl : public Combiner {40protected:41// TODO: Make CombinerHelper methods const.42mutable CombinerHelper Helper;43const RISCVO0PreLegalizerCombinerImplRuleConfig &RuleConfig;44const RISCVSubtarget &STI;4546public:47RISCVO0PreLegalizerCombinerImpl(48MachineFunction &MF, CombinerInfo &CInfo, const TargetPassConfig *TPC,49GISelKnownBits &KB, GISelCSEInfo *CSEInfo,50const RISCVO0PreLegalizerCombinerImplRuleConfig &RuleConfig,51const RISCVSubtarget &STI);5253static const char *getName() { return "RISCVO0PreLegalizerCombiner"; }5455bool tryCombineAll(MachineInstr &I) const override;5657private:58#define GET_GICOMBINER_CLASS_MEMBERS59#include "RISCVGenO0PreLegalizeGICombiner.inc"60#undef GET_GICOMBINER_CLASS_MEMBERS61};6263#define GET_GICOMBINER_IMPL64#include "RISCVGenO0PreLegalizeGICombiner.inc"65#undef GET_GICOMBINER_IMPL6667RISCVO0PreLegalizerCombinerImpl::RISCVO0PreLegalizerCombinerImpl(68MachineFunction &MF, CombinerInfo &CInfo, const TargetPassConfig *TPC,69GISelKnownBits &KB, GISelCSEInfo *CSEInfo,70const RISCVO0PreLegalizerCombinerImplRuleConfig &RuleConfig,71const RISCVSubtarget &STI)72: Combiner(MF, CInfo, TPC, &KB, CSEInfo),73Helper(Observer, B, /*IsPreLegalize*/ true, &KB), RuleConfig(RuleConfig),74STI(STI),75#define GET_GICOMBINER_CONSTRUCTOR_INITS76#include "RISCVGenO0PreLegalizeGICombiner.inc"77#undef GET_GICOMBINER_CONSTRUCTOR_INITS78{79}8081// Pass boilerplate82// ================8384class RISCVO0PreLegalizerCombiner : public MachineFunctionPass {85public:86static char ID;8788RISCVO0PreLegalizerCombiner();8990StringRef getPassName() const override {91return "RISCVO0PreLegalizerCombiner";92}9394bool runOnMachineFunction(MachineFunction &MF) override;9596void getAnalysisUsage(AnalysisUsage &AU) const override;9798private:99RISCVO0PreLegalizerCombinerImplRuleConfig RuleConfig;100};101} // end anonymous namespace102103void RISCVO0PreLegalizerCombiner::getAnalysisUsage(AnalysisUsage &AU) const {104AU.addRequired<TargetPassConfig>();105AU.setPreservesCFG();106getSelectionDAGFallbackAnalysisUsage(AU);107AU.addRequired<GISelKnownBitsAnalysis>();108AU.addPreserved<GISelKnownBitsAnalysis>();109MachineFunctionPass::getAnalysisUsage(AU);110}111112RISCVO0PreLegalizerCombiner::RISCVO0PreLegalizerCombiner()113: MachineFunctionPass(ID) {114initializeRISCVO0PreLegalizerCombinerPass(*PassRegistry::getPassRegistry());115116if (!RuleConfig.parseCommandLineOption())117report_fatal_error("Invalid rule identifier");118}119120bool RISCVO0PreLegalizerCombiner::runOnMachineFunction(MachineFunction &MF) {121if (MF.getProperties().hasProperty(122MachineFunctionProperties::Property::FailedISel))123return false;124auto &TPC = getAnalysis<TargetPassConfig>();125126const Function &F = MF.getFunction();127GISelKnownBits *KB = &getAnalysis<GISelKnownBitsAnalysis>().get(MF);128129const RISCVSubtarget &ST = MF.getSubtarget<RISCVSubtarget>();130131CombinerInfo CInfo(/*AllowIllegalOps*/ true, /*ShouldLegalizeIllegal*/ false,132/*LegalizerInfo*/ nullptr, /*EnableOpt*/ false,133F.hasOptSize(), F.hasMinSize());134RISCVO0PreLegalizerCombinerImpl Impl(MF, CInfo, &TPC, *KB,135/*CSEInfo*/ nullptr, RuleConfig, ST);136return Impl.combineMachineInstrs();137}138139char RISCVO0PreLegalizerCombiner::ID = 0;140INITIALIZE_PASS_BEGIN(RISCVO0PreLegalizerCombiner, DEBUG_TYPE,141"Combine RISC-V machine instrs before legalization", false,142false)143INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)144INITIALIZE_PASS_DEPENDENCY(GISelKnownBitsAnalysis)145INITIALIZE_PASS_DEPENDENCY(GISelCSEAnalysisWrapperPass)146INITIALIZE_PASS_END(RISCVO0PreLegalizerCombiner, DEBUG_TYPE,147"Combine RISC-V machine instrs before legalization", false,148false)149150namespace llvm {151FunctionPass *createRISCVO0PreLegalizerCombiner() {152return new RISCVO0PreLegalizerCombiner();153}154} // end namespace llvm155156157