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