LLVM 24.0.0git
SPIRVPreLegalizerCombiner.cpp
Go to the documentation of this file.
1//===-- SPIRVPreLegalizerCombiner.cpp - combine legalization ----*- 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// This pass does combining of machine instructions at the generic MI level,
10// before the legalizer.
11//
12//===----------------------------------------------------------------------===//
13
14#include "SPIRV.h"
15#include "SPIRVCombinerHelper.h"
28#include "llvm/IR/Analysis.h"
29#include "llvm/IR/IntrinsicsSPIRV.h"
30
31#define GET_GICOMBINER_DEPS
32#include "SPIRVGenPreLegalizeGICombiner.inc"
33#undef GET_GICOMBINER_DEPS
34
35#define DEBUG_TYPE "spirv-prelegalizer-combiner"
36
37using namespace llvm;
38using namespace MIPatternMatch;
39
40namespace {
41
42#define GET_GICOMBINER_TYPES
43#include "SPIRVGenPreLegalizeGICombiner.inc"
44#undef GET_GICOMBINER_TYPES
45
46class SPIRVPreLegalizerCombinerImpl : public Combiner {
47protected:
48 const SPIRVCombinerHelper Helper;
49 const SPIRVPreLegalizerCombinerImplRuleConfig &RuleConfig;
50 const SPIRVSubtarget &STI;
51
52public:
53 SPIRVPreLegalizerCombinerImpl(
55 GISelCSEInfo *CSEInfo,
56 const SPIRVPreLegalizerCombinerImplRuleConfig &RuleConfig,
57 const SPIRVSubtarget &STI, MachineDominatorTree *MDT,
58 const LegalizerInfo *LI);
59
60 static const char *getName() { return "SPIRVPreLegalizerCombiner"; }
61
62 bool tryCombineAll(MachineInstr &I) const override;
63
64 bool tryCombineAllImpl(MachineInstr &I) const;
65
66private:
67#define GET_GICOMBINER_CLASS_MEMBERS
68#include "SPIRVGenPreLegalizeGICombiner.inc"
69#undef GET_GICOMBINER_CLASS_MEMBERS
70};
71
72#define GET_GICOMBINER_IMPL
73#include "SPIRVGenPreLegalizeGICombiner.inc"
74#undef GET_GICOMBINER_IMPL
75
76SPIRVPreLegalizerCombinerImpl::SPIRVPreLegalizerCombinerImpl(
78 GISelCSEInfo *CSEInfo,
79 const SPIRVPreLegalizerCombinerImplRuleConfig &RuleConfig,
80 const SPIRVSubtarget &STI, MachineDominatorTree *MDT,
81 const LegalizerInfo *LI)
82 : Combiner(MF, CInfo, &VT, CSEInfo),
83 Helper(Observer, B, /*IsPreLegalize*/ true, &VT, MDT, LI, STI),
84 RuleConfig(RuleConfig), STI(STI),
86#include "SPIRVGenPreLegalizeGICombiner.inc"
88{
89}
90
91bool SPIRVPreLegalizerCombinerImpl::tryCombineAll(MachineInstr &MI) const {
92 return tryCombineAllImpl(MI);
93}
94
95// Pass boilerplate
96// ================
97
98class SPIRVPreLegalizerCombinerLegacy : public MachineFunctionPass {
99public:
100 static char ID;
101
102 SPIRVPreLegalizerCombinerLegacy();
103
104 StringRef getPassName() const override { return "SPIRVPreLegalizerCombiner"; }
105
106 bool runOnMachineFunction(MachineFunction &MF) override;
107
108 void getAnalysisUsage(AnalysisUsage &AU) const override;
109};
110
111} // end anonymous namespace
112
113void SPIRVPreLegalizerCombinerLegacy::getAnalysisUsage(
114 AnalysisUsage &AU) const {
115 AU.setPreservesCFG();
117 AU.addRequired<GISelValueTrackingAnalysisLegacy>();
118 AU.addPreserved<GISelValueTrackingAnalysisLegacy>();
119 AU.addRequired<MachineDominatorTreeWrapperPass>();
121}
122
123SPIRVPreLegalizerCombinerLegacy::SPIRVPreLegalizerCombinerLegacy()
124 : MachineFunctionPass(ID) {}
125
126static bool
130 if (MF.getProperties().hasFailedISel())
131 return false;
132
133 SPIRVPreLegalizerCombinerImplRuleConfig RuleConfig;
134 if (!RuleConfig.parseCommandLineOption())
135 reportFatalUsageError("Invalid rule identifier");
136
138 const auto *LI = ST.getLegalizerInfo();
139
140 const Function &F = MF.getFunction();
141 bool EnableOpt =
142 MF.getTarget().getOptLevel() != CodeGenOptLevel::None && !ShouldSkip;
143 GISelValueTracking *VT = GetVT();
144 MachineDominatorTree *MDT = GetMDT();
145 CombinerInfo CInfo(/*AllowIllegalOps*/ true, /*ShouldLegalizeIllegal*/ false,
146 /*LegalizerInfo*/ nullptr, EnableOpt, F.hasOptSize(),
147 F.hasMinSize());
148 // Disable fixed-point iteration to reduce compile-time
149 CInfo.MaxIterations = 1;
151 // This is the first Combiner, so the input IR might contain dead
152 // instructions.
153 CInfo.EnableFullDCE = false;
154 SPIRVPreLegalizerCombinerImpl Impl(MF, CInfo, *VT, /*CSEInfo*/ nullptr,
155 RuleConfig, ST, MDT, LI);
156 return Impl.combineMachineInstrs();
157}
158
159char SPIRVPreLegalizerCombinerLegacy::ID = 0;
160INITIALIZE_PASS_BEGIN(SPIRVPreLegalizerCombinerLegacy, DEBUG_TYPE,
161 "Combine SPIRV machine instrs before legalization", false,
162 false)
164INITIALIZE_PASS_END(SPIRVPreLegalizerCombinerLegacy, DEBUG_TYPE,
165 "Combine SPIRV machine instrs before legalization", false,
166 false)
167
168namespace llvm {
170 return new SPIRVPreLegalizerCombinerLegacy();
171}
172} // end namespace llvm
173
174bool SPIRVPreLegalizerCombinerLegacy::runOnMachineFunction(
175 MachineFunction &MF) {
177 MF, skipFunction(MF.getFunction()),
178 [&]() {
179 return &getAnalysis<GISelValueTrackingAnalysisLegacy>().get(MF);
180 },
181 [&]() {
182 return &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
183 });
184}
185
190 MF,
191 MF.getFunction().hasOptNone() ||
193 [&]() { return &MFAM.getResult<GISelValueTrackingAnalysis>(MF); },
194 [&]() { return &MFAM.getResult<MachineDominatorTreeAnalysis>(MF); });
195 if (!Changed)
196 return PreservedAnalyses::all();
199 .preserve<GISelValueTrackingAnalysis>();
200}
#define GET_GICOMBINER_CONSTRUCTOR_INITS
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Provides analysis for continuously CSEing during GISel passes.
Option class for Targets to specify which operations are combined how and when.
This contains the base class for all Combiners generated by TableGen.
This contains common code to allow clients to notify changes to machine instr.
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:54
#define I(x, y, z)
Definition MD5.cpp:57
Contains matchers for matching SSA Machine Instructions.
#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 runPreLegalizerCombiner(MachineFunction &MF, bool ShouldSkip, function_ref< GISelValueTracking *()> GetVT, function_ref< MachineDominatorTree *()> GetMDT)
Target-Independent Code Generator Pass Configuration Options 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:278
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:314
bool hasOptNone() const
Do not optimize this function (-O0).
Definition Function.h:686
The CSE Analysis object.
Definition CSEInfo.h:72
To use KnownBitsInfo analysis in a pass, KnownBitsInfo &Info = getAnalysis<GISelValueTrackingInfoAnal...
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
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.
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.
An efficient, type-erasing, non-owning reference to a callable.
Changed
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 * createSPIRVPreLegalizerCombinerLegacyPass()
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...