LLVM 24.0.0git
RISCVPreLegalizerCombiner.cpp
Go to the documentation of this file.
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"
29#include "llvm/IR/Analysis.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(
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(
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),
84#include "RISCVGenPreLegalizeGICombiner.inc"
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 {
109 AU.setPreservesCFG();
117}
118
119RISCVPreLegalizerCombinerLegacy::RISCVPreLegalizerCombinerLegacy()
120 : MachineFunctionPass(ID) {}
121
122static bool
124 function_ref<GISelCSEInfo *()> GetCSEInfo,
127 if (MF.getProperties().hasFailedISel())
128 return false;
129 RISCVPreLegalizerCombinerImplRuleConfig RuleConfig;
130 if (!RuleConfig.parseCommandLineOption())
131 reportFatalUsageError("Invalid rule identifier");
132
133 GISelCSEInfo *CSEInfo = GetCSEInfo();
134
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;
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)
164INITIALIZE_PASS_END(RISCVPreLegalizerCombinerLegacy, DEBUG_TYPE,
165 "Combine RISC-V machine instrs before legalization", false,
166 false)
167
169 return new RISCVPreLegalizerCombinerLegacy();
170}
171
172bool RISCVPreLegalizerCombinerLegacy::runOnMachineFunction(
173 MachineFunction &MF) {
175 MF, [&]() { return skipFunction(MF.getFunction()); },
176 [&]() {
177 TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
179 getAnalysis<GISelCSEAnalysisWrapperPass>().getCSEWrapper();
180 return &Wrapper.get(TPC.getCSEConfig());
181 },
182 [&]() {
183 return &getAnalysis<GISelValueTrackingAnalysisLegacy>().get(MF);
184 },
185 [&]() {
186 return &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
187 });
188}
189
194 MF,
195 [&]() {
196 return MF.getFunction().hasOptNone() ||
198 },
199 [&]() { return MFAM.getResult<GISelCSEAnalysis>(MF).get(); },
200 [&]() { return &MFAM.getResult<GISelValueTrackingAnalysis>(MF); },
201 [&]() { return &MFAM.getResult<MachineDominatorTreeAnalysis>(MF); });
202 if (!Changed)
203 return PreservedAnalyses::all();
206 .preserve<GISelCSEAnalysis>()
207 .preserve<GISelValueTrackingAnalysis>();
208}
#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
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
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)
static bool runVPreLegalizerCombiner(MachineFunction &MF, function_ref< bool()> ShouldSkip, function_ref< GISelCSEInfo *()> GetCSEInfo, function_ref< GISelValueTracking *()> GetVT, function_ref< MachineDominatorTree *()> GetMDT)
Target-Independent Code Generator Pass Configuration Options pass.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
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:280
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
Combiner implementation.
Definition Combiner.h:33
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
bool hasOptNone() const
Do not optimize this function (-O0).
Definition Function.h:686
The actual analysis pass wrapper.
Definition CSEInfo.h:244
Simple wrapper that does the following.
Definition CSEInfo.h:214
The CSE Analysis object.
Definition CSEInfo.h:72
To use KnownBitsInfo analysis in a pass, KnownBitsInfo &Info = getAnalysis<GISelValueTrackingInfoAnal...
Analysis pass which computes a MachineDominatorTree.
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
Representation of each machine instruction.
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition Pass.cpp:87
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
CodeGenOptLevel getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
Target-Independent Code Generator Pass Configuration Options.
virtual std::unique_ptr< CSEConfigBase > getCSEConfig() const
Returns the CSEConfig object to use for the current optimization level.
An efficient, type-erasing, non-owning reference to a callable.
Pass manager infrastructure for declaring and invalidating analyses.
This is an optimization pass for GlobalISel generic memory operations.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
FunctionPass * createRISCVPreLegalizerCombinerLegacyPass()
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
LLVM_ABI void getSelectionDAGFallbackAnalysisUsage(AnalysisUsage &AU)
Modify analysis usage so it preserves passes required for the SelectionDAG fallback.
Definition Utils.cpp:1137
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:177
unsigned MaxIterations
The maximum number of times the Combiner will iterate over the MachineFunction.
ObserverLevel ObserverLvl
Select how the Combiner acts on MIR changes.
bool EnableFullDCE
Whether dead code elimination is performed before each Combiner iteration.
@ SinglePass
Enables Observer-based DCE and additional heuristics that retry combining defined and used instructio...