1//=== RISCVPostLegalizerCombiner.cpp --------------------------*- C++ -*-===//
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/// \file
10/// Post-legalization combines on generic MachineInstrs.
11///
12/// The combines here must preserve instruction legality.
13///
14/// Combines which don't rely on instruction legality should go in the
15/// RISCVPreLegalizerCombiner.
16///
17//===----------------------------------------------------------------------===//
18
19#include "RISCV.h"
20#include "RISCVTargetMachine.h"
21#include "llvm/CodeGen/GlobalISel/CSEInfo.h"
22#include "llvm/CodeGen/GlobalISel/Combiner.h"
23#include "llvm/CodeGen/GlobalISel/CombinerHelper.h"
24#include "llvm/CodeGen/GlobalISel/CombinerInfo.h"
25#include "llvm/CodeGen/GlobalISel/GIMatchTableExecutorImpl.h"
26#include "llvm/CodeGen/GlobalISel/GISelValueTracking.h"
27#include "llvm/CodeGen/GlobalISel/MIPatternMatch.h"
28#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
29#include "llvm/CodeGen/MachineDominators.h"
30#include "llvm/CodeGen/MachineFunctionAnalysisManager.h"
31#include "llvm/CodeGen/MachineFunctionPass.h"
32#include "llvm/CodeGen/MachinePassManager.h"
33#include "llvm/CodeGen/TargetPassConfig.h"
34#include "llvm/IR/Analysis.h"
35#include "llvm/Support/ErrorHandling.h"
36#include "llvm/Support/FormatVariadic.h"
37
38#define GET_GICOMBINER_DEPS
39#include "RISCVGenPostLegalizeGICombiner.inc"
40#undef GET_GICOMBINER_DEPS
41
42#define DEBUG_TYPE "riscv-postlegalizer-combiner"
43
44using namespace llvm;
45using namespace MIPatternMatch;
46
47namespace {
48
49#define GET_GICOMBINER_TYPES
50#include "RISCVGenPostLegalizeGICombiner.inc"
51#undef GET_GICOMBINER_TYPES
52
53/// Match: G_STORE (G_FCONSTANT +0.0), addr
54/// Return the source vreg in MatchInfo if matched.
55bool matchFoldFPZeroStore(MachineInstr &MI, MachineRegisterInfo &MRI,
56 const RISCVSubtarget &STI, Register &MatchInfo) {
57 if (MI.getOpcode() != TargetOpcode::G_STORE)
58 return false;
59
60 Register SrcReg = MI.getOperand(i: 0).getReg();
61 if (!SrcReg.isVirtual())
62 return false;
63
64 if (!mi_match(R: SrcReg, MRI, P: m_PosZeroFP()))
65 return false;
66
67 unsigned ValBits = MRI.getType(Reg: SrcReg).getSizeInBits();
68 if ((ValBits == 16 && !STI.hasStdExtZfh()) ||
69 (ValBits == 32 && !STI.hasStdExtF()) ||
70 (ValBits == 64 && (!STI.hasStdExtD() || !STI.is64Bit())))
71 return false;
72
73 MatchInfo = SrcReg;
74 return true;
75}
76
77/// Apply: rewrite to G_STORE (G_CONSTANT 0 [XLEN]), addr
78void applyFoldFPZeroStore(MachineInstr &MI, MachineRegisterInfo &MRI,
79 MachineIRBuilder &B, const RISCVSubtarget &STI,
80 Register &MatchInfo) {
81 const unsigned XLen = STI.getXLen();
82
83 auto Zero = B.buildConstant(Res: LLT::integer(SizeInBits: XLen), Val: 0);
84 MI.getOperand(i: 0).setReg(Zero.getReg(Idx: 0));
85
86 MachineInstr *Def = MRI.getVRegDef(Reg: MatchInfo);
87 if (Def && MRI.use_nodbg_empty(RegNo: MatchInfo))
88 Def->eraseFromParent();
89
90#ifndef NDEBUG
91 unsigned ValBits = MRI.getType(MatchInfo).getSizeInBits();
92 LLVM_DEBUG(dbgs() << formatv("[{0}] Fold FP zero store -> int zero "
93 "(XLEN={1}, ValBits={2}):\n {3}\n",
94 DEBUG_TYPE, XLen, ValBits, MI));
95#endif
96}
97
98class RISCVPostLegalizerCombinerImpl : public Combiner {
99protected:
100 const CombinerHelper Helper;
101 const RISCVPostLegalizerCombinerImplRuleConfig &RuleConfig;
102 const RISCVSubtarget &STI;
103
104public:
105 RISCVPostLegalizerCombinerImpl(
106 MachineFunction &MF, CombinerInfo &CInfo, GISelValueTracking &VT,
107 GISelCSEInfo *CSEInfo,
108 const RISCVPostLegalizerCombinerImplRuleConfig &RuleConfig,
109 const RISCVSubtarget &STI, MachineDominatorTree *MDT,
110 const LegalizerInfo *LI);
111
112 static const char *getName() { return "RISCVPostLegalizerCombiner"; }
113
114 bool tryCombineAll(MachineInstr &I) const override;
115
116private:
117#define GET_GICOMBINER_CLASS_MEMBERS
118#include "RISCVGenPostLegalizeGICombiner.inc"
119#undef GET_GICOMBINER_CLASS_MEMBERS
120};
121
122#define GET_GICOMBINER_IMPL
123#include "RISCVGenPostLegalizeGICombiner.inc"
124#undef GET_GICOMBINER_IMPL
125
126RISCVPostLegalizerCombinerImpl::RISCVPostLegalizerCombinerImpl(
127 MachineFunction &MF, CombinerInfo &CInfo, GISelValueTracking &VT,
128 GISelCSEInfo *CSEInfo,
129 const RISCVPostLegalizerCombinerImplRuleConfig &RuleConfig,
130 const RISCVSubtarget &STI, MachineDominatorTree *MDT,
131 const LegalizerInfo *LI)
132 : Combiner(MF, CInfo, &VT, CSEInfo),
133 Helper(Observer, B, /*IsPreLegalize*/ false, &VT, MDT, LI),
134 RuleConfig(RuleConfig), STI(STI),
135#define GET_GICOMBINER_CONSTRUCTOR_INITS
136#include "RISCVGenPostLegalizeGICombiner.inc"
137#undef GET_GICOMBINER_CONSTRUCTOR_INITS
138{
139}
140
141class RISCVPostLegalizerCombinerLegacy : public MachineFunctionPass {
142public:
143 static char ID;
144
145 RISCVPostLegalizerCombinerLegacy();
146
147 StringRef getPassName() const override {
148 return "RISCVPostLegalizerCombiner";
149 }
150
151 bool runOnMachineFunction(MachineFunction &MF) override;
152 void getAnalysisUsage(AnalysisUsage &AU) const override;
153};
154} // end anonymous namespace
155
156void RISCVPostLegalizerCombinerLegacy::getAnalysisUsage(
157 AnalysisUsage &AU) const {
158 AU.addRequired<TargetPassConfig>();
159 AU.setPreservesCFG();
160 getSelectionDAGFallbackAnalysisUsage(AU);
161 AU.addRequired<GISelValueTrackingAnalysisLegacy>();
162 AU.addPreserved<GISelValueTrackingAnalysisLegacy>();
163 AU.addRequired<MachineDominatorTreeWrapperPass>();
164 AU.addRequired<GISelCSEAnalysisWrapperPass>();
165 AU.addPreserved<GISelCSEAnalysisWrapperPass>();
166 MachineFunctionPass::getAnalysisUsage(AU);
167}
168
169RISCVPostLegalizerCombinerLegacy::RISCVPostLegalizerCombinerLegacy()
170 : MachineFunctionPass(ID) {}
171
172static bool
173runCombinerOnMachineFunction(MachineFunction &MF,
174 function_ref<bool()> ShouldSkip,
175 function_ref<GISelValueTracking *()> GetVT,
176 function_ref<MachineDominatorTree *()> GetMDT,
177 function_ref<GISelCSEInfo *()> GetCSEInfo) {
178 if (MF.getProperties().hasFailedISel())
179 return false;
180 assert(MF.getProperties().hasLegalized() && "Expected a legalized function?");
181 const Function &F = MF.getFunction();
182 bool EnableOpt =
183 MF.getTarget().getOptLevel() != CodeGenOptLevel::None && !ShouldSkip();
184
185 RISCVPostLegalizerCombinerImplRuleConfig RuleConfig;
186 if (!RuleConfig.parseCommandLineOption())
187 reportFatalUsageError(reason: "Invalid rule identifier");
188
189 const RISCVSubtarget &ST = MF.getSubtarget<RISCVSubtarget>();
190 const auto *LI = ST.getLegalizerInfo();
191
192 GISelValueTracking *VT = GetVT();
193 MachineDominatorTree *MDT = GetMDT();
194 GISelCSEInfo *CSEInfo = GetCSEInfo();
195
196 CombinerInfo CInfo(/*AllowIllegalOps*/ true, /*ShouldLegalizeIllegal*/ false,
197 /*LegalizerInfo*/ nullptr, EnableOpt, F.hasOptSize(),
198 F.hasMinSize());
199 RISCVPostLegalizerCombinerImpl Impl(MF, CInfo, *VT, CSEInfo, RuleConfig, ST,
200 MDT, LI);
201 return Impl.combineMachineInstrs();
202}
203
204char RISCVPostLegalizerCombinerLegacy::ID = 0;
205INITIALIZE_PASS_BEGIN(RISCVPostLegalizerCombinerLegacy, DEBUG_TYPE,
206 "Combine RISC-V MachineInstrs after legalization", false,
207 false)
208INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
209INITIALIZE_PASS_DEPENDENCY(GISelValueTrackingAnalysisLegacy)
210INITIALIZE_PASS_END(RISCVPostLegalizerCombinerLegacy, DEBUG_TYPE,
211 "Combine RISC-V MachineInstrs after legalization", false,
212 false)
213
214FunctionPass *llvm::createRISCVPostLegalizerCombinerLegacyPass() {
215 return new RISCVPostLegalizerCombinerLegacy();
216}
217
218bool RISCVPostLegalizerCombinerLegacy::runOnMachineFunction(
219 MachineFunction &MF) {
220 return runCombinerOnMachineFunction(
221 MF, ShouldSkip: [&]() { return skipFunction(F: MF.getFunction()); },
222 GetVT: [&]() {
223 return &getAnalysis<GISelValueTrackingAnalysisLegacy>().get(MF);
224 },
225 GetMDT: [&]() {
226 return &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
227 },
228 GetCSEInfo: [&]() {
229 TargetPassConfig *TPC = &getAnalysis<TargetPassConfig>();
230 GISelCSEAnalysisWrapper &Wrapper =
231 getAnalysis<GISelCSEAnalysisWrapperPass>().getCSEWrapper();
232 return &Wrapper.get(CSEOpt: TPC->getCSEConfig());
233 });
234}
235
236PreservedAnalyses
237RISCVPostLegalizerCombinerPass::run(MachineFunction &MF,
238 MachineFunctionAnalysisManager &MFAM) {
239 bool Changed = runCombinerOnMachineFunction(
240 MF,
241 ShouldSkip: [&]() {
242 return MF.getFunction().hasOptNone() ||
243 shouldSkipOptimizationForOptBisect(IR: MF.getFunction());
244 },
245 GetVT: [&]() { return &MFAM.getResult<GISelValueTrackingAnalysis>(IR&: MF); },
246 GetMDT: [&]() { return &MFAM.getResult<MachineDominatorTreeAnalysis>(IR&: MF); },
247 GetCSEInfo: [&]() { return MFAM.getResult<GISelCSEAnalysis>(IR&: MF).get(); });
248 return Changed ? getMachineFunctionPassPreservedAnalyses()
249 .preserveSet<CFGAnalyses>()
250 .preserve<GISelValueTrackingAnalysis>()
251 .preserve<GISelCSEAnalysis>()
252 : PreservedAnalyses::all();
253}
254