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