LLVM 23.0.0git
X86PostLegalizerCombiner.cpp
Go to the documentation of this file.
1//===--------------- X86PostLegalizerCombiner.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/// Lowering combines (e.g. pseudo matching) should be handled by
15/// X86PostLegalizerLowering.
16///
17/// Combines which don't rely on instruction legality should go in the
18/// X86PreLegalizerCombiner.
19///
20//===----------------------------------------------------------------------===//
21#include "X86.h"
22#include "X86TargetMachine.h"
23#include "llvm/ADT/STLExtras.h"
41#include "llvm/Support/Debug.h"
42
43#define GET_GICOMBINER_DEPS
44#include "X86GenPostLegalizeGICombiner.inc"
45#undef GET_GICOMBINER_DEPS
46
47#define DEBUG_TYPE "X86-postlegalizer-combiner"
48
49using namespace llvm;
50using namespace MIPatternMatch;
51
52namespace {
53
54CombinerInfo createCombinerInfo(bool OptEnabled, const Function &F) {
55 CombinerInfo CInfo(/*AllowIllegalOps=*/true,
56 /*ShouldLegalizeIllegal=*/false,
57 /*LInfo=*/nullptr, /*OptEnabled=*/OptEnabled,
58 /*OptSize=*/F.hasOptSize(), /*MinSize=*/F.hasMinSize());
59 // Disable fixed-point iteration to reduce compile-time
60 CInfo.MaxIterations = 1;
62 // Legalizer performs DCE, so a full DCE pass is unnecessary.
63 CInfo.EnableFullDCE = false;
64 return CInfo;
65}
66
67#define GET_GICOMBINER_TYPES
68#include "X86GenPostLegalizeGICombiner.inc"
69#undef GET_GICOMBINER_TYPES
70
71class X86PostLegalizerCombinerImpl : public Combiner {
72protected:
73 const CombinerHelper Helper;
74 const X86PostLegalizerCombinerImplRuleConfig &RuleConfig;
75 const X86Subtarget &STI;
76
77public:
78 X86PostLegalizerCombinerImpl(
79 MachineFunction &MF, CombinerInfo &CInfo, const TargetPassConfig *TPC,
80 GISelValueTracking &VT, GISelCSEInfo *CSEInfo,
81 const X86PostLegalizerCombinerImplRuleConfig &RuleConfig,
83
84 static const char *getName() { return "X86PostLegalizerCombiner"; }
85
86 bool tryCombineAll(MachineInstr &I) const override;
87 bool tryCombineAllImpl(MachineInstr &I) const;
88
89private:
90#define GET_GICOMBINER_CLASS_MEMBERS
91#include "X86GenPostLegalizeGICombiner.inc"
92#undef GET_GICOMBINER_CLASS_MEMBERS
93};
94
95#define GET_GICOMBINER_IMPL
96#include "X86GenPostLegalizeGICombiner.inc"
97#undef GET_GICOMBINER_IMPL
98
99X86PostLegalizerCombinerImpl::X86PostLegalizerCombinerImpl(
100 MachineFunction &MF, CombinerInfo &CInfo, const TargetPassConfig *TPC,
101 GISelValueTracking &VT, GISelCSEInfo *CSEInfo,
102 const X86PostLegalizerCombinerImplRuleConfig &RuleConfig,
104 : Combiner(MF, CInfo, TPC, &VT, CSEInfo),
105 Helper(Observer, B, /*IsPreLegalize=*/false, &VT, MDT,
106 MF.getSubtarget<X86Subtarget>().getLegalizerInfo()),
107 RuleConfig(RuleConfig), STI(MF.getSubtarget<X86Subtarget>()),
109#include "X86GenPostLegalizeGICombiner.inc"
111{
112}
113
114bool X86PostLegalizerCombinerImpl::tryCombineAll(MachineInstr &MI) const {
115 return tryCombineAllImpl(MI);
116}
117
118class X86PostLegalizerCombinerLegacy : public MachineFunctionPass {
119public:
120 static char ID;
121
122 X86PostLegalizerCombinerLegacy();
123
124 StringRef getPassName() const override {
125 return "X86PostLegalizerCombinerLegacy";
126 }
127
128 bool runOnMachineFunction(MachineFunction &MF) override;
129 void getAnalysisUsage(AnalysisUsage &AU) const override;
130
131private:
132 X86PostLegalizerCombinerImplRuleConfig RuleConfig;
133};
134} // end anonymous namespace
135
136void X86PostLegalizerCombinerLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
137 AU.addRequired<TargetPassConfig>();
138 AU.setPreservesCFG();
140 AU.addRequired<GISelValueTrackingAnalysisLegacy>();
141 AU.addPreserved<GISelValueTrackingAnalysisLegacy>();
142 // This is only added when processing level is not OptNone.
143 AU.addRequired<MachineDominatorTreeWrapperPass>();
144 AU.addPreserved<MachineDominatorTreeWrapperPass>();
145 AU.addRequired<GISelCSEAnalysisWrapperPass>();
146 AU.addPreserved<GISelCSEAnalysisWrapperPass>();
147
149}
150
151X86PostLegalizerCombinerLegacy::X86PostLegalizerCombinerLegacy()
152 : MachineFunctionPass(ID) {
153 if (!RuleConfig.parseCommandLineOption())
154 reportFatalInternalError("Invalid rule identifier");
155}
156
157bool X86PostLegalizerCombinerLegacy::runOnMachineFunction(MachineFunction &MF) {
158 if (MF.getProperties().hasFailedISel())
159 return false;
160 assert(MF.getProperties().hasLegalized() && "Expected a legalized function?");
161 auto *TPC = &getAnalysis<TargetPassConfig>();
162 const Function &F = MF.getFunction();
163
165 &getAnalysis<GISelValueTrackingAnalysisLegacy>().get(MF);
167 &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
169 getAnalysis<GISelCSEAnalysisWrapperPass>().getCSEWrapper();
170 auto *CSEInfo = &Wrapper.get(TPC->getCSEConfig());
171
172 CombinerInfo CInfo = createCombinerInfo(!skipFunction(F), F);
173
174 X86PostLegalizerCombinerImpl Impl(MF, CInfo, TPC, *VT, CSEInfo, RuleConfig,
175 MDT);
176 return Impl.combineMachineInstrs();
177}
178
179char X86PostLegalizerCombinerLegacy::ID = 0;
180INITIALIZE_PASS_BEGIN(X86PostLegalizerCombinerLegacy, DEBUG_TYPE,
181 "Combine X86 MachineInstrs after legalization", false,
182 false)
185INITIALIZE_PASS_END(X86PostLegalizerCombinerLegacy, DEBUG_TYPE,
186 "Combine X86 MachineInstrs after legalization", false,
187 false)
188
189namespace llvm {
190
194 if (MF.getProperties().hasFailedISel())
195 return PreservedAnalyses::all();
196 assert(MF.getProperties().hasLegalized() && "Expected a legalized function.");
197 const Function &F = MF.getFunction();
198
201 auto &CSEInfo = MFAM.getResult<GISelCSEAnalysis>(MF);
202
203 CombinerInfo CInfo = createCombinerInfo(true, F);
204
205 X86PostLegalizerCombinerImplRuleConfig RuleConfig;
206 if (!RuleConfig.parseCommandLineOption())
207 reportFatalInternalError("Invalid rule identifier");
208
209 X86PostLegalizerCombinerImpl Impl(MF, CInfo, nullptr, VT, CSEInfo.get(),
210 RuleConfig, &MDT);
211 if (!Impl.combineMachineInstrs())
212 return PreservedAnalyses::all();
213
218 return PA;
219}
220
222 return new X86PostLegalizerCombinerLegacy();
223}
224} // end namespace llvm
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 file implements a version of MachineIRBuilder which CSEs insts within a MachineBasicBlock.
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.
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
Declares convenience wrapper classes for interpreting MachineInstr instances as specific generic oper...
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.
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)
This file contains some templates that are useful if you are working with the STL at all.
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.
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
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
Combiner implementation.
Definition Combiner.h:34
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
Simple wrapper that does the following.
Definition CSEInfo.h:212
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.
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.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineFunctionProperties & getProperties() const
Get the function properties.
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 & preserve()
Mark an analysis as preserved.
Definition Analysis.h:132
Target-Independent Code Generator Pass Configuration Options.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI void reportFatalInternalError(Error Err)
Report a fatal error that indicates a bug in LLVM.
Definition Error.cpp:173
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
FunctionPass * createX86PostLegalizerCombinerLegacy()
LLVM_ABI void getSelectionDAGFallbackAnalysisUsage(AnalysisUsage &AU)
Modify analysis usage so it preserves passes required for the SelectionDAG fallback.
Definition Utils.cpp:1185
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...