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