1//=== RISCVO0PreLegalizerCombiner.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/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/MachineFunctionAnalysisManager.h"
25#include "llvm/CodeGen/MachineFunctionPass.h"
26#include "llvm/CodeGen/MachinePassManager.h"
27#include "llvm/IR/Analysis.h"
28#include "llvm/Support/ErrorHandling.h"
29
30#define GET_GICOMBINER_DEPS
31#include "RISCVGenO0PreLegalizeGICombiner.inc"
32#undef GET_GICOMBINER_DEPS
33
34#define DEBUG_TYPE "riscv-O0-prelegalizer-combiner"
35
36using namespace llvm;
37
38namespace {
39#define GET_GICOMBINER_TYPES
40#include "RISCVGenO0PreLegalizeGICombiner.inc"
41#undef GET_GICOMBINER_TYPES
42
43class RISCVO0PreLegalizerCombinerImpl : public Combiner {
44protected:
45 const CombinerHelper Helper;
46 const RISCVO0PreLegalizerCombinerImplRuleConfig &RuleConfig;
47 const RISCVSubtarget &STI;
48
49public:
50 RISCVO0PreLegalizerCombinerImpl(
51 MachineFunction &MF, CombinerInfo &CInfo, GISelValueTracking &VT,
52 GISelCSEInfo *CSEInfo,
53 const RISCVO0PreLegalizerCombinerImplRuleConfig &RuleConfig,
54 const RISCVSubtarget &STI);
55
56 static const char *getName() { return "RISCVO0PreLegalizerCombiner"; }
57
58 bool tryCombineAll(MachineInstr &I) const override;
59
60private:
61#define GET_GICOMBINER_CLASS_MEMBERS
62#include "RISCVGenO0PreLegalizeGICombiner.inc"
63#undef GET_GICOMBINER_CLASS_MEMBERS
64};
65
66#define GET_GICOMBINER_IMPL
67#include "RISCVGenO0PreLegalizeGICombiner.inc"
68#undef GET_GICOMBINER_IMPL
69
70RISCVO0PreLegalizerCombinerImpl::RISCVO0PreLegalizerCombinerImpl(
71 MachineFunction &MF, CombinerInfo &CInfo, GISelValueTracking &VT,
72 GISelCSEInfo *CSEInfo,
73 const RISCVO0PreLegalizerCombinerImplRuleConfig &RuleConfig,
74 const RISCVSubtarget &STI)
75 : Combiner(MF, CInfo, &VT, CSEInfo),
76 Helper(Observer, B, /*IsPreLegalize*/ true, &VT), RuleConfig(RuleConfig),
77 STI(STI),
78#define GET_GICOMBINER_CONSTRUCTOR_INITS
79#include "RISCVGenO0PreLegalizeGICombiner.inc"
80#undef GET_GICOMBINER_CONSTRUCTOR_INITS
81{
82}
83
84// Pass boilerplate
85// ================
86
87class RISCVO0PreLegalizerCombinerLegacy : public MachineFunctionPass {
88public:
89 static char ID;
90
91 RISCVO0PreLegalizerCombinerLegacy();
92
93 StringRef getPassName() const override {
94 return "RISCVO0PreLegalizerCombiner";
95 }
96
97 bool runOnMachineFunction(MachineFunction &MF) override;
98
99 void getAnalysisUsage(AnalysisUsage &AU) const override;
100};
101} // end anonymous namespace
102
103void RISCVO0PreLegalizerCombinerLegacy::getAnalysisUsage(
104 AnalysisUsage &AU) const {
105 AU.setPreservesCFG();
106 getSelectionDAGFallbackAnalysisUsage(AU);
107 AU.addRequired<GISelValueTrackingAnalysisLegacy>();
108 AU.addPreserved<GISelValueTrackingAnalysisLegacy>();
109 MachineFunctionPass::getAnalysisUsage(AU);
110}
111
112RISCVO0PreLegalizerCombinerLegacy::RISCVO0PreLegalizerCombinerLegacy()
113 : MachineFunctionPass(ID) {}
114
115static bool
116runO0PreLegalizerCombiner(MachineFunction &MF,
117 function_ref<GISelValueTracking *()> GetVT) {
118 if (MF.getProperties().hasFailedISel())
119 return false;
120
121 RISCVO0PreLegalizerCombinerImplRuleConfig RuleConfig;
122 if (!RuleConfig.parseCommandLineOption())
123 reportFatalUsageError(reason: "Invalid rule identifier");
124
125 const Function &F = MF.getFunction();
126 GISelValueTracking *VT = GetVT();
127
128 const RISCVSubtarget &ST = MF.getSubtarget<RISCVSubtarget>();
129
130 CombinerInfo CInfo(/*AllowIllegalOps*/ true, /*ShouldLegalizeIllegal*/ false,
131 /*LegalizerInfo*/ nullptr, /*EnableOpt*/ false,
132 F.hasOptSize(), F.hasMinSize());
133 // Disable fixed-point iteration in the Combiner. This improves compile-time
134 // at the cost of possibly missing optimizations. See PR#94291 for details.
135 CInfo.MaxIterations = 1;
136
137 RISCVO0PreLegalizerCombinerImpl Impl(MF, CInfo, *VT,
138 /*CSEInfo*/ nullptr, RuleConfig, ST);
139 return Impl.combineMachineInstrs();
140}
141
142char RISCVO0PreLegalizerCombinerLegacy::ID = 0;
143INITIALIZE_PASS_BEGIN(RISCVO0PreLegalizerCombinerLegacy, DEBUG_TYPE,
144 "Combine RISC-V machine instrs before legalization",
145 false, false)
146INITIALIZE_PASS_DEPENDENCY(GISelValueTrackingAnalysisLegacy)
147INITIALIZE_PASS_DEPENDENCY(GISelCSEAnalysisWrapperPass)
148INITIALIZE_PASS_END(RISCVO0PreLegalizerCombinerLegacy, DEBUG_TYPE,
149 "Combine RISC-V machine instrs before legalization", false,
150 false)
151
152FunctionPass *llvm::createRISCVO0PreLegalizerCombinerLegacyPass() {
153 return new RISCVO0PreLegalizerCombinerLegacy();
154}
155
156bool RISCVO0PreLegalizerCombinerLegacy::runOnMachineFunction(
157 MachineFunction &MF) {
158 return runO0PreLegalizerCombiner(MF, GetVT: [&]() {
159 return &getAnalysis<GISelValueTrackingAnalysisLegacy>().get(MF);
160 });
161}
162
163PreservedAnalyses
164RISCVO0PreLegalizerCombinerPass::run(MachineFunction &MF,
165 MachineFunctionAnalysisManager &MFAM) {
166 bool Changed = runO0PreLegalizerCombiner(
167 MF, GetVT: [&]() { return &MFAM.getResult<GISelValueTrackingAnalysis>(IR&: MF); });
168 if (!Changed)
169 return PreservedAnalyses::all();
170 return getMachineFunctionPassPreservedAnalyses()
171 .preserveSet<CFGAnalyses>()
172 .preserve<GISelValueTrackingAnalysis>();
173}
174