LLVM 22.0.0git
RISCVPostLegalizerCombiner.cpp
Go to the documentation of this file.
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 "RISCVTargetMachine.h"
31
32#define GET_GICOMBINER_DEPS
33#include "RISCVGenPostLegalizeGICombiner.inc"
34#undef GET_GICOMBINER_DEPS
35
36#define DEBUG_TYPE "riscv-postlegalizer-combiner"
37
38using namespace llvm;
39
40namespace {
41
42#define GET_GICOMBINER_TYPES
43#include "RISCVGenPostLegalizeGICombiner.inc"
44#undef GET_GICOMBINER_TYPES
45
46/// Match: G_STORE (G_FCONSTANT +0.0), addr
47/// Return the source vreg in MatchInfo if matched.
48bool matchFoldFPZeroStore(MachineInstr &MI, MachineRegisterInfo &MRI,
49 const RISCVSubtarget &STI, Register &MatchInfo) {
50 if (MI.getOpcode() != TargetOpcode::G_STORE)
51 return false;
52
53 Register SrcReg = MI.getOperand(0).getReg();
54 if (!SrcReg.isVirtual())
55 return false;
56
57 MachineInstr *Def = MRI.getVRegDef(SrcReg);
58 if (!Def || Def->getOpcode() != TargetOpcode::G_FCONSTANT)
59 return false;
60
61 auto *CFP = Def->getOperand(1).getFPImm();
62 if (!CFP || !CFP->getValueAPF().isPosZero())
63 return false;
64
65 unsigned ValBits = MRI.getType(SrcReg).getSizeInBits();
66 if ((ValBits == 16 && !STI.hasStdExtZfh()) ||
67 (ValBits == 32 && !STI.hasStdExtF()) ||
68 (ValBits == 64 && (!STI.hasStdExtD() || !STI.is64Bit())))
69 return false;
70
71 MatchInfo = SrcReg;
72 return true;
73}
74
75/// Apply: rewrite to G_STORE (G_CONSTANT 0 [XLEN]), addr
76void applyFoldFPZeroStore(MachineInstr &MI, MachineRegisterInfo &MRI,
78 Register &MatchInfo) {
79 const unsigned XLen = STI.getXLen();
80
81 auto Zero = B.buildConstant(LLT::scalar(XLen), 0);
82 MI.getOperand(0).setReg(Zero.getReg(0));
83
84 MachineInstr *Def = MRI.getVRegDef(MatchInfo);
85 if (Def && MRI.use_nodbg_empty(MatchInfo))
86 Def->eraseFromParent();
87
88#ifndef NDEBUG
89 unsigned ValBits = MRI.getType(MatchInfo).getSizeInBits();
90 LLVM_DEBUG(dbgs() << formatv("[{0}] Fold FP zero store -> int zero "
91 "(XLEN={1}, ValBits={2}):\n {3}\n",
92 DEBUG_TYPE, XLen, ValBits, MI));
93#endif
94}
95
96class RISCVPostLegalizerCombinerImpl : public Combiner {
97protected:
98 const CombinerHelper Helper;
99 const RISCVPostLegalizerCombinerImplRuleConfig &RuleConfig;
100 const RISCVSubtarget &STI;
101
102public:
103 RISCVPostLegalizerCombinerImpl(
104 MachineFunction &MF, CombinerInfo &CInfo, const TargetPassConfig *TPC,
105 GISelValueTracking &VT, GISelCSEInfo *CSEInfo,
106 const RISCVPostLegalizerCombinerImplRuleConfig &RuleConfig,
107 const RISCVSubtarget &STI, MachineDominatorTree *MDT,
108 const LegalizerInfo *LI);
109
110 static const char *getName() { return "RISCVPostLegalizerCombiner"; }
111
112 bool tryCombineAll(MachineInstr &I) const override;
113
114private:
115#define GET_GICOMBINER_CLASS_MEMBERS
116#include "RISCVGenPostLegalizeGICombiner.inc"
117#undef GET_GICOMBINER_CLASS_MEMBERS
118};
119
120#define GET_GICOMBINER_IMPL
121#include "RISCVGenPostLegalizeGICombiner.inc"
122#undef GET_GICOMBINER_IMPL
123
124RISCVPostLegalizerCombinerImpl::RISCVPostLegalizerCombinerImpl(
125 MachineFunction &MF, CombinerInfo &CInfo, const TargetPassConfig *TPC,
126 GISelValueTracking &VT, GISelCSEInfo *CSEInfo,
127 const RISCVPostLegalizerCombinerImplRuleConfig &RuleConfig,
128 const RISCVSubtarget &STI, MachineDominatorTree *MDT,
129 const LegalizerInfo *LI)
130 : Combiner(MF, CInfo, TPC, &VT, CSEInfo),
131 Helper(Observer, B, /*IsPreLegalize*/ false, &VT, MDT, LI),
132 RuleConfig(RuleConfig), STI(STI),
134#include "RISCVGenPostLegalizeGICombiner.inc"
136{
137}
138
139class RISCVPostLegalizerCombiner : public MachineFunctionPass {
140public:
141 static char ID;
142
143 RISCVPostLegalizerCombiner();
144
145 StringRef getPassName() const override {
146 return "RISCVPostLegalizerCombiner";
147 }
148
149 bool runOnMachineFunction(MachineFunction &MF) override;
150 void getAnalysisUsage(AnalysisUsage &AU) const override;
151
152private:
153 RISCVPostLegalizerCombinerImplRuleConfig RuleConfig;
154};
155} // end anonymous namespace
156
157void RISCVPostLegalizerCombiner::getAnalysisUsage(AnalysisUsage &AU) const {
159 AU.setPreservesCFG();
168}
169
170RISCVPostLegalizerCombiner::RISCVPostLegalizerCombiner()
172 if (!RuleConfig.parseCommandLineOption())
173 report_fatal_error("Invalid rule identifier");
174}
175
176bool RISCVPostLegalizerCombiner::runOnMachineFunction(MachineFunction &MF) {
177 if (MF.getProperties().hasFailedISel())
178 return false;
179 assert(MF.getProperties().hasLegalized() && "Expected a legalized function?");
180 auto *TPC = &getAnalysis<TargetPassConfig>();
181 const Function &F = MF.getFunction();
182 bool EnableOpt =
183 MF.getTarget().getOptLevel() != CodeGenOptLevel::None && !skipFunction(F);
184
186 const auto *LI = ST.getLegalizerInfo();
187
189 &getAnalysis<GISelValueTrackingAnalysisLegacy>().get(MF);
191 &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
193 getAnalysis<GISelCSEAnalysisWrapperPass>().getCSEWrapper();
194 auto *CSEInfo = &Wrapper.get(TPC->getCSEConfig());
195
196 CombinerInfo CInfo(/*AllowIllegalOps*/ true, /*ShouldLegalizeIllegal*/ false,
197 /*LegalizerInfo*/ nullptr, EnableOpt, F.hasOptSize(),
198 F.hasMinSize());
199 RISCVPostLegalizerCombinerImpl Impl(MF, CInfo, TPC, *VT, CSEInfo, RuleConfig,
200 ST, MDT, LI);
201 return Impl.combineMachineInstrs();
202}
203
204char RISCVPostLegalizerCombiner::ID = 0;
205INITIALIZE_PASS_BEGIN(RISCVPostLegalizerCombiner, DEBUG_TYPE,
206 "Combine RISC-V MachineInstrs after legalization", false,
207 false)
210INITIALIZE_PASS_END(RISCVPostLegalizerCombiner, DEBUG_TYPE,
211 "Combine RISC-V MachineInstrs after legalization", false,
212 false)
213
215 return new RISCVPostLegalizerCombiner();
216}
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define GET_GICOMBINER_CONSTRUCTOR_INITS
amdgpu aa AMDGPU Address space based Alias Analysis Wrapper
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Provides analysis for continuously CSEing during GISel passes.
This contains common combine transformations that may be used in a combine pass,or by the target else...
Option class for Targets to specify which operations are combined how and when.
This contains the base class for all Combiners generated by TableGen.
Provides analysis for querying information about KnownBits during GISel passes.
#define DEBUG_TYPE
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
This file declares the MachineIRBuilder class.
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
static StringRef getName(Value *V)
#define LLVM_DEBUG(...)
Definition Debug.h:114
Target-Independent Code Generator Pass Configuration Options pass.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:270
Combiner implementation.
Definition Combiner.h:34
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
The actual analysis pass wrapper.
Definition CSEInfo.h:229
Simple wrapper that does the following.
Definition CSEInfo.h:211
The CSE Analysis object.
Definition CSEInfo.h:71
To use KnownBitsInfo analysis in a pass, KnownBitsInfo &Info = getAnalysis<GISelValueTrackingInfoAnal...
static constexpr LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
Analysis pass which computes a MachineDominatorTree.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineFunctionProperties & getProperties() const
Get the function properties.
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
Helper class to build MachineInstr.
Representation of each machine instruction.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition Pass.cpp:85
unsigned getXLen() const
Wrapper class representing virtual and physical registers.
Definition Register.h:19
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:74
CodeGenOptLevel getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
Target-Independent Code Generator Pass Configuration Options.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
NodeAddr< DefNode * > Def
Definition RDFGraph.h:384
This is an optimization pass for GlobalISel generic memory operations.
FunctionPass * createRISCVPostLegalizerCombiner()
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
LLVM_ABI void getSelectionDAGFallbackAnalysisUsage(AnalysisUsage &AU)
Modify analysis usage so it preserves passes required for the SelectionDAG fallback.
Definition Utils.cpp:1184