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
30#define GET_GICOMBINER_DEPS
31#include "SPIRVGenPreLegalizeGICombiner.inc"
32#undef GET_GICOMBINER_DEPS
33
34#define DEBUG_TYPE "spirv-prelegalizer-combiner"
35
36using namespace llvm;
37using namespace MIPatternMatch;
38
39namespace {
40
41#define GET_GICOMBINER_TYPES
42#include "SPIRVGenPreLegalizeGICombiner.inc"
43#undef GET_GICOMBINER_TYPES
44
45class SPIRVPreLegalizerCombinerImpl : public Combiner {
46protected:
47 const SPIRVCombinerHelper Helper;
48 const SPIRVPreLegalizerCombinerImplRuleConfig &RuleConfig;
49 const SPIRVSubtarget &STI;
50
51public:
52 SPIRVPreLegalizerCombinerImpl(
54 GISelCSEInfo *CSEInfo,
55 const SPIRVPreLegalizerCombinerImplRuleConfig &RuleConfig,
56 const SPIRVSubtarget &STI, MachineDominatorTree *MDT,
57 const LegalizerInfo *LI);
58
59 static const char *getName() { return "SPIRVPreLegalizerCombiner"; }
60
61 bool tryCombineAll(MachineInstr &I) const override;
62
63 bool tryCombineAllImpl(MachineInstr &I) const;
64
65private:
66#define GET_GICOMBINER_CLASS_MEMBERS
67#include "SPIRVGenPreLegalizeGICombiner.inc"
68#undef GET_GICOMBINER_CLASS_MEMBERS
69};
70
71#define GET_GICOMBINER_IMPL
72#include "SPIRVGenPreLegalizeGICombiner.inc"
73#undef GET_GICOMBINER_IMPL
74
75SPIRVPreLegalizerCombinerImpl::SPIRVPreLegalizerCombinerImpl(
77 GISelCSEInfo *CSEInfo,
78 const SPIRVPreLegalizerCombinerImplRuleConfig &RuleConfig,
79 const SPIRVSubtarget &STI, MachineDominatorTree *MDT,
80 const LegalizerInfo *LI)
81 : Combiner(MF, CInfo, &VT, CSEInfo),
82 Helper(Observer, B, /*IsPreLegalize*/ true, &VT, MDT, LI, STI),
83 RuleConfig(RuleConfig), STI(STI),
85#include "SPIRVGenPreLegalizeGICombiner.inc"
87{
88}
89
90bool SPIRVPreLegalizerCombinerImpl::tryCombineAll(MachineInstr &MI) const {
91 return tryCombineAllImpl(MI);
92}
93
94// Pass boilerplate
95// ================
96
97class SPIRVPreLegalizerCombinerLegacy : public MachineFunctionPass {
98public:
99 static char ID;
100
101 SPIRVPreLegalizerCombinerLegacy();
102
103 StringRef getPassName() const override { return "SPIRVPreLegalizerCombiner"; }
104
105 bool runOnMachineFunction(MachineFunction &MF) override;
106
107 void getAnalysisUsage(AnalysisUsage &AU) const override;
108};
109
110} // end anonymous namespace
111
112void SPIRVPreLegalizerCombinerLegacy::getAnalysisUsage(
113 AnalysisUsage &AU) const {
114 AU.setPreservesCFG();
116 AU.addRequired<GISelValueTrackingAnalysisLegacy>();
117 AU.addPreserved<GISelValueTrackingAnalysisLegacy>();
118 AU.addRequired<MachineDominatorTreeWrapperPass>();
120}
121
122SPIRVPreLegalizerCombinerLegacy::SPIRVPreLegalizerCombinerLegacy()
123 : MachineFunctionPass(ID) {}
124
125static bool
129 if (MF.getProperties().hasFailedISel())
130 return false;
131
132 SPIRVPreLegalizerCombinerImplRuleConfig RuleConfig;
133 if (!RuleConfig.parseCommandLineOption())
134 reportFatalUsageError("Invalid rule identifier");
135
137 const auto *LI = ST.getLegalizerInfo();
138
139 const Function &F = MF.getFunction();
140 bool EnableOpt =
141 MF.getTarget().getOptLevel() != CodeGenOptLevel::None && !ShouldSkip;
142 GISelValueTracking *VT = GetVT();
143 MachineDominatorTree *MDT = GetMDT();
144 CombinerInfo CInfo(/*AllowIllegalOps*/ true, /*ShouldLegalizeIllegal*/ false,
145 /*LegalizerInfo*/ nullptr, EnableOpt, F.hasOptSize(),
146 F.hasMinSize());
147 // Disable fixed-point iteration to reduce compile-time
148 CInfo.MaxIterations = 1;
150 // This is the first Combiner, so the input IR might contain dead
151 // instructions.
152 CInfo.EnableFullDCE = false;
153 SPIRVPreLegalizerCombinerImpl Impl(MF, CInfo, *VT, /*CSEInfo*/ nullptr,
154 RuleConfig, ST, MDT, LI);
155 return Impl.combineMachineInstrs();
156}
157
158char SPIRVPreLegalizerCombinerLegacy::ID = 0;
159INITIALIZE_PASS_BEGIN(SPIRVPreLegalizerCombinerLegacy, DEBUG_TYPE,
160 "Combine SPIRV machine instrs before legalization", false,
161 false)
163INITIALIZE_PASS_END(SPIRVPreLegalizerCombinerLegacy, DEBUG_TYPE,
164 "Combine SPIRV machine instrs before legalization", false,
165 false)
166
167namespace llvm {
169 return new SPIRVPreLegalizerCombinerLegacy();
170}
171} // end namespace llvm
172
173bool SPIRVPreLegalizerCombinerLegacy::runOnMachineFunction(
174 MachineFunction &MF) {
176 MF, skipFunction(MF.getFunction()),
177 [&]() {
178 return &getAnalysis<GISelValueTrackingAnalysisLegacy>().get(MF);
179 },
180 [&]() {
181 return &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
182 });
183}
184
189 MF, MF.getFunction().hasOptNone(),
190 [&]() { return &MFAM.getResult<GISelValueTrackingAnalysis>(MF); },
191 [&]() { return &MFAM.getResult<MachineDominatorTreeAnalysis>(MF); });
192 if (!Changed)
193 return PreservedAnalyses::all();
196 .preserve<GISelValueTrackingAnalysis>();
197}
#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:275
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...