LLVM 23.0.0git
AArch64O0PreLegalizerCombiner.cpp
Go to the documentation of this file.
1//=== lib/CodeGen/GlobalISel/AArch64O0PreLegalizerCombiner.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 "AArch64.h"
32#include <memory>
33
34#define GET_GICOMBINER_DEPS
35#include "AArch64GenO0PreLegalizeGICombiner.inc"
36#undef GET_GICOMBINER_DEPS
37
38#define DEBUG_TYPE "aarch64-O0-prelegalizer-combiner"
39
40using namespace llvm;
41using namespace MIPatternMatch;
42
43#define GET_GICOMBINER_TYPES
44#include "AArch64GenO0PreLegalizeGICombiner.inc"
45#undef GET_GICOMBINER_TYPES
46
47namespace {
48
49class AArch64O0PreLegalizerCombinerImpl : public Combiner {
50protected:
51 const CombinerHelper Helper;
52 const AArch64O0PreLegalizerCombinerImplRuleConfig &RuleConfig;
53 const AArch64Subtarget &STI;
54 const LibcallLoweringInfo &Libcalls;
55
56public:
57 AArch64O0PreLegalizerCombinerImpl(
58 MachineFunction &MF, CombinerInfo &CInfo, GISelCSEInfo *CSEInfo,
59 const AArch64O0PreLegalizerCombinerImplRuleConfig &RuleConfig,
60 const AArch64Subtarget &STI, const LibcallLoweringInfo &Libcalls);
61
62 static const char *getName() { return "AArch64O0PreLegalizerCombiner"; }
63
64 bool tryCombineAll(MachineInstr &I) const override;
65
66 bool tryCombineAllImpl(MachineInstr &I) const;
67
68private:
69#define GET_GICOMBINER_CLASS_MEMBERS
70#include "AArch64GenO0PreLegalizeGICombiner.inc"
71#undef GET_GICOMBINER_CLASS_MEMBERS
72};
73
74#define GET_GICOMBINER_IMPL
75#include "AArch64GenO0PreLegalizeGICombiner.inc"
76#undef GET_GICOMBINER_IMPL
77
78AArch64O0PreLegalizerCombinerImpl::AArch64O0PreLegalizerCombinerImpl(
79 MachineFunction &MF, CombinerInfo &CInfo, GISelCSEInfo *CSEInfo,
80 const AArch64O0PreLegalizerCombinerImplRuleConfig &RuleConfig,
81 const AArch64Subtarget &STI, const LibcallLoweringInfo &Libcalls)
82 : Combiner(MF, CInfo, /*VT=*/nullptr, CSEInfo),
83 Helper(Observer, B, /*IsPreLegalize*/ true, /*VT=*/nullptr),
84 RuleConfig(RuleConfig), STI(STI), Libcalls(Libcalls),
86#include "AArch64GenO0PreLegalizeGICombiner.inc"
88{
89}
90
91bool AArch64O0PreLegalizerCombinerImpl::tryCombineAll(MachineInstr &MI) const {
92 if (tryCombineAllImpl(MI))
93 return true;
94
95 unsigned Opc = MI.getOpcode();
96 switch (Opc) {
97 case TargetOpcode::G_SHUFFLE_VECTOR:
98 return Helper.tryCombineShuffleVector(MI);
99 case TargetOpcode::G_MEMCPY_INLINE:
100 return Helper.tryEmitMemcpyInline(MI);
101 case TargetOpcode::G_MEMCPY:
102 case TargetOpcode::G_MEMMOVE:
103 case TargetOpcode::G_MEMSET: {
104 // At -O0 set a maxlen of 32 to inline;
105 unsigned MaxLen = 32;
106 // Try to inline memcpy type calls if optimizations are enabled.
107 if (Helper.tryCombineMemCpyFamily(MI, MaxLen))
108 return true;
109 if (Opc == TargetOpcode::G_MEMSET)
111 CInfo.EnableMinSize);
112 return false;
113 }
114 }
115
116 return false;
117}
118
119bool runCombiner(
120 MachineFunction &MF, const LibcallLoweringInfo &Libcalls,
121 const AArch64O0PreLegalizerCombinerImplRuleConfig &RuleConfig) {
122 const Function &F = MF.getFunction();
123 const AArch64Subtarget &ST = MF.getSubtarget<AArch64Subtarget>();
124
125 CombinerInfo CInfo(/*AllowIllegalOps=*/true, /*ShouldLegalizeIllegal=*/false,
126 /*LegalizerInfo=*/nullptr, /*EnableOpt=*/false,
127 F.hasOptSize(), F.hasMinSize());
128 // Disable fixed-point iteration in the Combiner. This improves compile-time
129 // at the cost of possibly missing optimizations. See PR#94291 for details.
130 CInfo.MaxIterations = 1;
131
132 AArch64O0PreLegalizerCombinerImpl Impl(MF, CInfo,
133 /*CSEInfo*/ nullptr, RuleConfig, ST,
134 Libcalls);
135 return Impl.combineMachineInstrs();
136}
137
138// Pass boilerplate
139// ================
140
141class AArch64O0PreLegalizerCombinerLegacy : public MachineFunctionPass {
142public:
143 static char ID;
144
145 AArch64O0PreLegalizerCombinerLegacy();
146
147 StringRef getPassName() const override {
148 return "AArch64O0PreLegalizerCombiner";
149 }
150
151 bool runOnMachineFunction(MachineFunction &MF) override;
152
153 void getAnalysisUsage(AnalysisUsage &AU) const override;
154
155private:
156 AArch64O0PreLegalizerCombinerImplRuleConfig RuleConfig;
157};
158} // end anonymous namespace
159
160void AArch64O0PreLegalizerCombinerLegacy::getAnalysisUsage(
161 AnalysisUsage &AU) const {
162 AU.setPreservesCFG();
164 AU.addRequired<LibcallLoweringInfoWrapper>();
166}
167
168AArch64O0PreLegalizerCombinerLegacy::AArch64O0PreLegalizerCombinerLegacy()
169 : MachineFunctionPass(ID) {
170 if (!RuleConfig.parseCommandLineOption())
171 report_fatal_error("Invalid rule identifier");
172}
173
174bool AArch64O0PreLegalizerCombinerLegacy::runOnMachineFunction(
175 MachineFunction &MF) {
176 if (MF.getProperties().hasFailedISel())
177 return false;
178
179 const Function &F = MF.getFunction();
180
181 const AArch64Subtarget &ST = MF.getSubtarget<AArch64Subtarget>();
182 const LibcallLoweringInfo &Libcalls =
183 getAnalysis<LibcallLoweringInfoWrapper>().getLibcallLowering(
184 *F.getParent(), ST);
185
186 return runCombiner(MF, Libcalls, RuleConfig);
187}
188
189char AArch64O0PreLegalizerCombinerLegacy::ID = 0;
190INITIALIZE_PASS_BEGIN(AArch64O0PreLegalizerCombinerLegacy, DEBUG_TYPE,
191 "Combine AArch64 machine instrs before legalization",
192 false, false)
195INITIALIZE_PASS_END(AArch64O0PreLegalizerCombinerLegacy, DEBUG_TYPE,
196 "Combine AArch64 machine instrs before legalization", false,
197 false)
198
200 : RuleConfig(
201 std::make_unique<AArch64O0PreLegalizerCombinerImplRuleConfig>()) {
202 if (!RuleConfig->parseCommandLineOption())
203 report_fatal_error("Invalid rule identifier");
204}
205
208
210 default;
211
215 if (MF.getProperties().hasFailedISel())
216 return PreservedAnalyses::all();
217
219 auto &MAMProxy =
221 const LibcallLoweringModuleAnalysisResult *LibcallResult =
222 MAMProxy.getCachedResult<LibcallLoweringModuleAnalysis>(
223 *MF.getFunction().getParent());
224 if (!LibcallResult)
225 reportFatalUsageError("LibcallLoweringModuleAnalysis result not available");
226
227 const LibcallLoweringInfo &Libcalls = LibcallResult->getLibcallLowering(ST);
228
229 if (!runCombiner(MF, Libcalls, *RuleConfig))
230 return PreservedAnalyses::all();
231
234 return PA;
235}
236
237namespace llvm {
239 return new AArch64O0PreLegalizerCombinerLegacy();
240}
241} // end namespace llvm
#define GET_GICOMBINER_CONSTRUCTOR_INITS
static const Function * getParent(const Value *V)
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
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: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)
Target-Independent Code Generator Pass Configuration Options pass.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
AnalysisUsage & addRequired()
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
bool tryEmitMemcpyInline(MachineInstr &MI) const
Emit loads and stores that perform the given memcpy.
bool tryCombineShuffleVector(MachineInstr &MI) const
Try to combine G_SHUFFLE_VECTOR into G_CONCAT_VECTORS.
bool tryCombineMemCpyFamily(MachineInstr &MI, unsigned MaxLen=0) const
Optimize memcpy intrinsics et al, e.g.
Combiner implementation.
Definition Combiner.h:33
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
The actual analysis pass wrapper.
Definition CSEInfo.h:242
The CSE Analysis object.
Definition CSEInfo.h:72
Tracks which library functions to use for a particular subtarget.
Record a mapping from subtarget to LibcallLoweringInfo.
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.
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
bool tryEmitBZero(MachineInstr &MI, MachineIRBuilder &MIRBuilder, const LibcallLoweringInfo &Libcalls, bool MinSize)
Replace a G_MEMSET with a value of 0 with a G_BZERO instruction if it is supported and beneficial to ...
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.
OuterAnalysisManagerProxy< ModuleAnalysisManager, MachineFunction > ModuleAnalysisManagerMachineFunctionProxy
Provide the ModuleAnalysisManager to Function proxy.
FunctionPass * createAArch64O0PreLegalizerCombiner()
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
LLVM_ABI void getSelectionDAGFallbackAnalysisUsage(AnalysisUsage &AU)
Modify analysis usage so it preserves passes required for the SelectionDAG fallback.
Definition Utils.cpp:1142
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:177
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:870