LLVM 24.0.0git
TargetPassConfig.h
Go to the documentation of this file.
1//===- TargetPassConfig.h - Code Generation pass options --------*- 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/// \file
9/// Target-Independent Code Generator Pass Configuration Options pass.
10///
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CODEGEN_TARGETPASSCONFIG_H
14#define LLVM_CODEGEN_TARGETPASSCONFIG_H
15
16#include "llvm/Pass.h"
19#include "llvm/Support/Error.h"
20#include <cassert>
21#include <string>
22
23namespace llvm {
24
25class TargetMachine;
26class PassConfigImpl;
27class CSEConfigBase;
29
30// The old pass manager infrastructure is hidden in a legacy namespace now.
31namespace legacy {
32
33class PassManagerBase;
34
35} // end namespace legacy
36
38
39/// Discriminated union of Pass ID types.
40///
41/// The PassConfig API prefers dealing with IDs because they are safer and more
42/// efficient. IDs decouple configuration from instantiation. This way, when a
43/// pass is overriden, it isn't unnecessarily instantiated. It is also unsafe to
44/// refer to a Pass pointer after adding it to a pass manager, which deletes
45/// redundant pass instances.
46///
47/// However, it is convient to directly instantiate target passes with
48/// non-default ctors. These often don't have a registered PassInfo. Rather than
49/// force all target passes to implement the pass registry boilerplate, allow
50/// the PassConfig API to handle either type.
51///
52/// AnalysisID is sadly char*, so PointerIntPair won't work.
54 union {
57 };
58 bool IsInstance = false;
59
60public:
61 IdentifyingPassPtr() : P(nullptr) {}
62 IdentifyingPassPtr(AnalysisID IDPtr) : ID(IDPtr) {}
63 IdentifyingPassPtr(Pass *InstancePtr) : P(InstancePtr), IsInstance(true) {}
64
65 bool isValid() const { return P; }
66 bool isInstance() const { return IsInstance; }
67
68 AnalysisID getID() const {
69 assert(!IsInstance && "Not a Pass ID");
70 return ID;
71 }
72
73 Pass *getInstance() const {
74 assert(IsInstance && "Not a Pass Instance");
75 return P;
76 }
77};
78
79
80/// Target-Independent Code Generator Pass Configuration Options.
81///
82/// This is an ImmutablePass solely for the purpose of exposing CodeGen options
83/// to the internals of other CodeGen passes.
85private:
86 PassManagerBase *PM = nullptr;
87 AnalysisID StartBefore = nullptr;
88 AnalysisID StartAfter = nullptr;
89 AnalysisID StopBefore = nullptr;
90 AnalysisID StopAfter = nullptr;
91
92 unsigned StartBeforeInstanceNum = 0;
93 unsigned StartBeforeCount = 0;
94
95 unsigned StartAfterInstanceNum = 0;
96 unsigned StartAfterCount = 0;
97
98 unsigned StopBeforeInstanceNum = 0;
99 unsigned StopBeforeCount = 0;
100
101 unsigned StopAfterInstanceNum = 0;
102 unsigned StopAfterCount = 0;
103
104 bool Started = true;
105 bool Stopped = false;
106 bool AddingMachinePasses = false;
107 bool DebugifyIsSafe = true;
108
109 /// Set the StartAfter, StartBefore and StopAfter passes to allow running only
110 /// a portion of the normal code-gen pass sequence.
111 ///
112 /// If the StartAfter and StartBefore pass ID is zero, then compilation will
113 /// begin at the normal point; otherwise, clear the Started flag to indicate
114 /// that passes should not be added until the starting pass is seen. If the
115 /// Stop pass ID is zero, then compilation will continue to the end.
116 ///
117 /// This function expects that at least one of the StartAfter or the
118 /// StartBefore pass IDs is null.
119 void setStartStopPasses();
120
121protected:
123 PassConfigImpl *Impl = nullptr; // Internal data structures
124 bool Initialized = false; // Flagged after all passes are configured.
125
126 // Target Pass Options
127 // Targets provide a default setting, user flags override.
128 bool DisableVerify = false;
129
131
132 /// Default setting for -enable-tail-merge on this target.
133 bool EnableTailMerge = true;
134
135 /// Enable sinking of instructions in MachineSink where a computation can be
136 /// folded into the addressing mode of a memory load/store instruction or
137 /// replace a copy.
138 bool EnableSinkAndFold = false;
139
140 /// Enable insertion of SSAMachineScheduler pass, this triggers early
141 /// computation of live intervals.
143
144 /// Require processing of functions such that callees are generated before
145 /// callers.
147
148 /// Enable LoopTermFold immediately after LSR
149 bool EnableLoopTermFold = false;
150
151 /// Add the actual instruction selection passes. This does not include
152 /// preparation passes on IR.
153 bool addCoreISelPasses();
154
155public:
157 // Dummy constructor.
159
160 ~TargetPassConfig() override;
161
162 static char ID;
163
164 /// Get the right type of TargetMachine for this target.
165 template<typename TMC> TMC &getTM() const {
166 return *static_cast<TMC*>(TM);
167 }
168
169 //
170 void setInitialized() { Initialized = true; }
171
172 CodeGenOptLevel getOptLevel() const;
173
174 /// Returns true if one of the `-start-after`, `-start-before`, `-stop-after`
175 /// or `-stop-before` options is set.
176 static bool hasLimitedCodeGenPipeline();
177
178 /// Returns true if none of the `-stop-before` and `-stop-after` options is
179 /// set.
180 static bool willCompleteCodeGenPipeline();
181
182 /// If hasLimitedCodeGenPipeline is true, this method returns
183 /// a string with the name of the options that caused this
184 /// pipeline to be limited.
185 static std::string getLimitedCodeGenPipelineReason();
186
195
196 /// Returns pass name in `-stop-before` or `-stop-after`
197 /// NOTE: New pass manager migration only
200
202
203 /// Return where the machine verifier runs in the pipeline.
205
206 bool getEnableTailMerge() const { return EnableTailMerge; }
208
211
216
223
224 /// Allow the target to override a specific pass without overriding the pass
225 /// pipeline. When passes are added to the standard pipeline at the
226 /// point where StandardID is expected, add TargetID in its place.
227 void substitutePass(AnalysisID StandardID, IdentifyingPassPtr TargetID);
228
229 /// Insert InsertedPassID pass after TargetPassID pass.
230 void insertPass(AnalysisID TargetPassID, IdentifyingPassPtr InsertedPassID);
231
232 /// Allow the target to enable a specific standard pass by default.
233 void enablePass(AnalysisID PassID) { substitutePass(PassID, PassID); }
234
235 /// Allow the target to disable a specific standard pass by default.
236 void disablePass(AnalysisID PassID) {
238 }
239
240 /// Return the pass substituted for StandardID by the target.
241 /// If no substitution exists, return StandardID.
242 IdentifyingPassPtr getPassSubstitution(AnalysisID StandardID) const;
243
244 /// Return true if the pass has been substituted by the target or
245 /// overridden on the command line.
246 bool isPassSubstitutedOrOverridden(AnalysisID ID) const;
247
248 /// Return true if the optimized regalloc pipeline is enabled.
249 bool getOptimizeRegAlloc() const;
250
251 /// Return true if the default global register allocator is in use and
252 /// has not be overriden on the command line with '-regalloc=...'
253 bool usingDefaultRegAlloc() const;
254
255 /// High level function that adds all passes necessary to go from llvm IR
256 /// representation to the MI representation.
257 /// Adds IR based lowering and target specific optimization passes and finally
258 /// the core instruction selection passes.
259 /// \returns true if an error occurred, false otherwise.
260 bool addISelPasses();
261
262 /// Add common target configurable passes that perform LLVM IR to IR
263 /// transforms following machine independent optimization.
264 virtual void addIRPasses();
265
266 /// Add passes to lower exception handling for the code generator.
267 void addPassesToHandleExceptions();
268
269 /// Add pass to prepare the LLVM IR for code generation. This should be done
270 /// before exception handling preparation passes.
271 virtual void addCodeGenPrepare();
272
273 /// Add common passes that perform LLVM IR to IR transforms in preparation for
274 /// instruction selection.
275 virtual void addISelPrepare();
276
277 /// addInstSelector - This method should install an instruction selector pass,
278 /// which converts from LLVM code to machine instructions.
279 virtual bool addInstSelector() {
280 return true;
281 }
282
283 /// This method should install an IR translator pass, which converts from
284 /// LLVM code to machine instructions with possibly generic opcodes.
285 virtual bool addIRTranslator() { return true; }
286
287 /// This method may be implemented by targets that want to run passes
288 /// immediately before legalization.
289 virtual void addPreLegalizeMachineIR() {}
290
291 /// This method should install a legalize pass, which converts the instruction
292 /// sequence into one that can be selected by the target.
293 virtual bool addLegalizeMachineIR() { return true; }
294
295 /// This method may be implemented by targets that want to run passes
296 /// immediately before the register bank selection.
297 virtual void addPreRegBankSelect() {}
298
299 /// This method should install a register bank selector pass, which
300 /// assigns register banks to virtual registers without a register
301 /// class or register banks.
302 virtual bool addRegBankSelect() { return true; }
303
304 /// This method may be implemented by targets that want to run passes
305 /// immediately before the (global) instruction selection.
307
308 /// This method should install a (global) instruction selector pass, which
309 /// converts possibly generic instructions to fully target-specific
310 /// instructions, thereby constraining all generic virtual registers to
311 /// register classes.
312 virtual bool addGlobalInstructionSelect() { return true; }
313
314 /// Add the complete, standard set of LLVM CodeGen passes.
315 /// Fully developed targets will not generally override this.
316 virtual void addMachinePasses();
317
318 /// printAndVerify - Add a pass to dump then verify the machine function, if
319 /// those steps are enabled.
320 void printAndVerify(const std::string &Banner);
321
322 /// Add a pass to print the machine function if printing is enabled.
323 void addPrintPass(const std::string &Banner);
324
325 /// Add a pass to perform basic verification of the machine function if
326 /// verification is enabled.
327 void addVerifyPass(const std::string &Banner);
328
329 /// Add a pass to add synthesized debug info to the MIR.
330 void addDebugifyPass();
331
332 /// Add a pass to remove debug info from the MIR.
333 void addStripDebugPass();
334
335 /// Add a pass to check synthesized debug info for MIR.
336 void addCheckDebugPass();
337
338 /// Add standard passes before a pass that's about to be added. For example,
339 /// the DebugifyMachineModulePass if it is enabled.
340 void addMachinePrePasses(bool AllowDebugify = true);
341
342 /// Add standard passes after a pass that has just been added. For example,
343 /// the MachineVerifier if it is enabled.
344 void addMachinePostPasses(const std::string &Banner);
345
346 /// Check whether or not GlobalISel should abort on error.
347 /// When this is disabled, GlobalISel will fall back on SDISel instead of
348 /// erroring out.
349 bool isGlobalISelAbortEnabled() const;
350
351 /// Check whether or not a diagnostic should be emitted when GlobalISel
352 /// uses the fallback path. In other words, it will emit a diagnostic
353 /// when GlobalISel failed and isGlobalISelAbortEnabled is false.
354 virtual bool reportDiagnosticWhenGlobalISelFallback() const;
355
356 /// Returns the CSEConfig object to use for the current optimization level.
357 virtual std::unique_ptr<CSEConfigBase> getCSEConfig() const;
358
359protected:
360 // Helper to verify the analysis is really immutable.
361 void setOpt(bool &Opt, bool Val);
362
363 /// Return true if register allocator is specified by -regalloc=override.
364 bool isCustomizedRegAlloc();
365
366 /// Methods with trivial inline returns are convenient points in the common
367 /// codegen pass pipeline where targets may insert passes. Methods with
368 /// out-of-line standard implementations are major CodeGen stages called by
369 /// addMachinePasses. Some targets may override major stages when inserting
370 /// passes is insufficient, but maintaining overriden stages is more work.
371 ///
372
373 /// addPreISelPasses - This method should add any "last minute" LLVM->LLVM
374 /// passes (which are run just before instruction selector).
375 virtual bool addPreISel() {
376 return true;
377 }
378
379 /// addMachineSSAOptimization - Add standard passes that optimize machine
380 /// instructions in SSA form.
381 virtual void addMachineSSAOptimization();
382
383 /// Add passes that optimize instruction level parallelism for out-of-order
384 /// targets. These passes are run while the machine code is still in SSA
385 /// form, so they can use MachineTraceMetrics to control their heuristics.
386 ///
387 /// All passes added here should preserve the MachineDominatorTree,
388 /// MachineLoopInfo, and MachineTraceMetrics analyses.
389 virtual bool addILPOpts() {
390 return false;
391 }
392
393 /// This method may be implemented by targets that want to run passes
394 /// immediately before register allocation.
395 virtual void addPreRegAlloc() { }
396
397 /// createTargetRegisterAllocator - Create the register allocator pass for
398 /// this target at the current optimization level.
399 virtual FunctionPass *createTargetRegisterAllocator(bool Optimized);
400
401 /// addFastRegAlloc - Add the minimum set of target-independent passes that
402 /// are required for fast register allocation.
403 virtual void addFastRegAlloc();
404
405 /// addOptimizedRegAlloc - Add passes related to register allocation.
406 /// CodeGenTargetMachineImpl provides standard regalloc passes for most
407 /// targets.
408 virtual void addOptimizedRegAlloc();
409
410 /// addPreRewrite - Add passes to the optimized register allocation pipeline
411 /// after register allocation is complete, but before virtual registers are
412 /// rewritten to physical registers.
413 ///
414 /// These passes must preserve VirtRegMap and LiveIntervals, and when running
415 /// after RABasic or RAGreedy, they should take advantage of LiveRegMatrix.
416 /// When these passes run, VirtRegMap contains legal physreg assignments for
417 /// all virtual registers.
418 ///
419 /// Note if the target overloads addRegAssignAndRewriteOptimized, this may not
420 /// be honored. This is also not generally used for the fast variant,
421 /// where the allocation and rewriting are done in one pass.
422 virtual bool addPreRewrite() {
423 return false;
424 }
425
426 /// addPostFastRegAllocRewrite - Add passes to the optimized register
427 /// allocation pipeline after fast register allocation is complete.
428 virtual bool addPostFastRegAllocRewrite() { return false; }
429
430 /// Add passes to be run immediately after virtual registers are rewritten
431 /// to physical registers.
432 virtual void addPostRewrite() { }
433
434 /// This method may be implemented by targets that want to run passes after
435 /// register allocation pass pipeline but before prolog-epilog insertion.
436 virtual void addPostRegAlloc() { }
437
438 /// Add passes that optimize machine instructions after register allocation.
439 virtual void addMachineLateOptimization();
440
441 /// This method may be implemented by targets that want to run passes after
442 /// prolog-epilog insertion and before the second instruction scheduling pass.
443 virtual void addPreSched2() { }
444
445 /// addGCPasses - Add late codegen passes that analyze code for garbage
446 /// collection. This should return true if GC info should be printed after
447 /// these passes.
448 virtual bool addGCPasses();
449
450 /// Add standard basic block placement passes.
451 virtual void addBlockPlacement();
452
453 /// This pass may be implemented by targets that want to run passes
454 /// immediately before machine code is emitted.
455 virtual void addPreEmitPass() { }
456
457 /// This pass may be implemented by targets that want to run passes
458 /// immediately after basic block sections are assigned.
459 virtual void addPostBBSections() {}
460
461 /// Targets may add passes immediately before machine code is emitted in this
462 /// callback. This is called even later than `addPreEmitPass`.
463 // FIXME: Rename `addPreEmitPass` to something more sensible given its actual
464 // position and remove the `2` suffix here as this callback is what
465 // `addPreEmitPass` *should* be but in reality isn't.
466 virtual void addPreEmitPass2() {}
467
468 /// Utilities for targets to add passes to the pass manager.
469 ///
470
471 /// Add a CodeGen pass at this point in the pipeline after checking overrides.
472 /// Return the pass that was added, or zero if no pass was added.
474
475 /// Add a pass to the PassManager if that pass is supposed to be run, as
476 /// determined by the StartAfter and StopAfter options. Takes ownership of the
477 /// pass.
478 void addPass(Pass *P);
479
480 /// addMachinePasses helper to create the target-selected or overriden
481 /// regalloc pass.
482 virtual FunctionPass *createRegAllocPass(bool Optimized);
483
484 /// Add core register allocator passes which do the actual register assignment
485 /// and rewriting. \returns true if any passes were added.
486 virtual bool addRegAssignAndRewriteFast();
487 virtual bool addRegAssignAndRewriteOptimized();
488};
489
490LLVM_ABI void registerCodeGenCallback(PassInstrumentationCallbacks &PIC,
491 TargetMachine &);
492
493} // end namespace llvm
494
495#endif // LLVM_CODEGEN_TARGETPASSCONFIG_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition Compiler.h:215
#define P(N)
PassInstrumentationCallbacks PIC
Tagged union holding either a T or a Error.
Definition Error.h:485
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
Discriminated union of Pass ID types.
AnalysisID getID() const
IdentifyingPassPtr(AnalysisID IDPtr)
IdentifyingPassPtr(Pass *InstancePtr)
ImmutablePass(char &pid)
Definition Pass.h:287
This class manages callbacks registration, as well as provides a way for PassInstrumentation to pass ...
Pass interface - Implemented by all 'passes'.
Definition Pass.h:99
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Primary interface to the complete machine description for the target machine.
void enablePass(AnalysisID PassID)
Allow the target to enable a specific standard pass by default.
bool requiresCodeGenSCCOrder() const
virtual void addPreLegalizeMachineIR()
This method may be implemented by targets that want to run passes immediately before legalization.
virtual void addPreEmitPass2()
Targets may add passes immediately before machine code is emitted in this callback.
bool RequireCodeGenSCCOrder
Require processing of functions such that callees are generated before callers.
bool EnableLoopTermFold
Enable LoopTermFold immediately after LSR.
static Expected< StartStopInfo > getStartStopInfo(PassInstrumentationCallbacks &PIC)
Returns pass name in -stop-before or -stop-after NOTE: New pass manager migration only.
bool EnableSSAMachineScheduler
Enable insertion of SSAMachineScheduler pass, this triggers early computation of live intervals.
virtual void addPreSched2()
This method may be implemented by targets that want to run passes after prolog-epilog insertion and b...
virtual bool addILPOpts()
Add passes that optimize instruction level parallelism for out-of-order targets.
virtual void addPostRegAlloc()
This method may be implemented by targets that want to run passes after register allocation pass pipe...
MachineVerifierMode getMachineVerifierMode() const
Return where the machine verifier runs in the pipeline.
virtual bool addInstSelector()
addInstSelector - This method should install an instruction selector pass, which converts from LLVM c...
bool getEnableSinkAndFold() const
virtual bool addPreISel()
Methods with trivial inline returns are convenient points in the common codegen pass pipeline where t...
void setOpt(bool &Opt, bool Val)
bool getEnableSSAMachineScheduler() const
virtual void addPostBBSections()
This pass may be implemented by targets that want to run passes immediately after basic block section...
virtual void addPreEmitPass()
This pass may be implemented by targets that want to run passes immediately before machine code is em...
virtual void addPreRegBankSelect()
This method may be implemented by targets that want to run passes immediately before the register ban...
virtual bool addPreRewrite()
addPreRewrite - Add passes to the optimized register allocation pipeline after register allocation is...
virtual bool addRegBankSelect()
This method should install a register bank selector pass, which assigns register banks to virtual reg...
void setEnableSinkAndFold(bool Enable)
void setRequiresCodeGenSCCOrder(bool Enable=true)
virtual void addPreGlobalInstructionSelect()
This method may be implemented by targets that want to run passes immediately before the (global) ins...
virtual bool addLegalizeMachineIR()
This method should install a legalize pass, which converts the instruction sequence into one that can...
void substitutePass(AnalysisID StandardID, IdentifyingPassPtr TargetID)
Allow the target to override a specific pass without overriding the pass pipeline.
void disablePass(AnalysisID PassID)
Allow the target to disable a specific standard pass by default.
virtual bool addGlobalInstructionSelect()
This method should install a (global) instruction selector pass, which converts possibly generic inst...
virtual void addPreRegAlloc()
This method may be implemented by targets that want to run passes immediately before register allocat...
bool EnableSinkAndFold
Enable sinking of instructions in MachineSink where a computation can be folded into the addressing m...
bool EnableTailMerge
Default setting for -enable-tail-merge on this target.
bool addCoreISelPasses()
Add the actual instruction selection passes.
void setEnableTailMerge(bool Enable)
TargetPassConfig(TargetMachine &TM, PassManagerBase &PM)
void setDisableVerify(bool Disable)
TMC & getTM() const
Get the right type of TargetMachine for this target.
void setEnableSSAMachineScheduler(bool Enable)
MachineVerifierMode VerifyMode
virtual bool addIRTranslator()
This method should install an IR translator pass, which converts from LLVM code to machine instructio...
virtual bool addPostFastRegAllocRewrite()
addPostFastRegAllocRewrite - Add passes to the optimized register allocation pipeline after fast regi...
virtual void addPostRewrite()
Add passes to be run immediately after virtual registers are rewritten to physical registers.
PassManagerBase - An abstract interface to allow code to add passes to a pass manager without having ...
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI void registerCodeGenCallback(PassInstrumentationCallbacks &PIC, TargetMachine &)
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:227
MachineVerifierMode
Specify where the machine verifier runs in the codegen pipeline.
Definition CodeGen.h:263
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:267
const void * AnalysisID
Definition Pass.h:51
@ Enable
Enable colors.
Definition WithColor.h:47
@ Disable
Disable colors.
Definition WithColor.h:49