1 //===-- TargetPassConfig.h - Code Generation pass options -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// Target-Independent Code Generator Pass Configuration Options pass.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CODEGEN_TARGETPASSCONFIG_H
15 #define LLVM_CODEGEN_TARGETPASSCONFIG_H
17 #include "llvm/Pass.h"
18 #include "llvm/Support/CodeGen.h"
24 class ScheduleDAGInstrs;
26 struct MachineSchedContext;
28 // The old pass manager infrastructure is hidden in a legacy namespace now.
30 class PassManagerBase;
32 using legacy::PassManagerBase;
34 /// Discriminated union of Pass ID types.
36 /// The PassConfig API prefers dealing with IDs because they are safer and more
37 /// efficient. IDs decouple configuration from instantiation. This way, when a
38 /// pass is overriden, it isn't unnecessarily instantiated. It is also unsafe to
39 /// refer to a Pass pointer after adding it to a pass manager, which deletes
40 /// redundant pass instances.
42 /// However, it is convient to directly instantiate target passes with
43 /// non-default ctors. These often don't have a registered PassInfo. Rather than
44 /// force all target passes to implement the pass registry boilerplate, allow
45 /// the PassConfig API to handle either type.
47 /// AnalysisID is sadly char*, so PointerIntPair won't work.
48 class IdentifyingPassPtr {
55 IdentifyingPassPtr() : P(nullptr), IsInstance(false) {}
56 IdentifyingPassPtr(AnalysisID IDPtr) : ID(IDPtr), IsInstance(false) {}
57 IdentifyingPassPtr(Pass *InstancePtr) : P(InstancePtr), IsInstance(true) {}
59 bool isValid() const { return P; }
60 bool isInstance() const { return IsInstance; }
62 AnalysisID getID() const {
63 assert(!IsInstance && "Not a Pass ID");
66 Pass *getInstance() const {
67 assert(IsInstance && "Not a Pass Instance");
72 template <> struct isPodLike<IdentifyingPassPtr> {
73 static const bool value = true;
76 /// Target-Independent Code Generator Pass Configuration Options.
78 /// This is an ImmutablePass solely for the purpose of exposing CodeGen options
79 /// to the internals of other CodeGen passes.
80 class TargetPassConfig : public ImmutablePass {
82 /// Pseudo Pass IDs. These are defined within TargetPassConfig because they
83 /// are unregistered pass IDs. They are only useful for use with
84 /// TargetPassConfig APIs to identify multiple occurrences of the same pass.
87 /// EarlyTailDuplicate - A clone of the TailDuplicate pass that runs early
88 /// during codegen, on SSA form.
89 static char EarlyTailDuplicateID;
91 /// PostRAMachineLICM - A clone of the LICM pass that runs during late machine
92 /// optimization after regalloc.
93 static char PostRAMachineLICMID;
97 AnalysisID StartBefore = nullptr;
98 AnalysisID StartAfter = nullptr;
99 AnalysisID StopBefore = nullptr;
100 AnalysisID StopAfter = nullptr;
103 bool AddingMachinePasses;
107 PassConfigImpl *Impl; // Internal data structures
108 bool Initialized; // Flagged after all passes are configured.
110 // Target Pass Options
111 // Targets provide a default setting, user flags override.
115 /// Default setting for -enable-tail-merge on this target.
116 bool EnableTailMerge;
119 TargetPassConfig(TargetMachine *tm, PassManagerBase &pm);
120 // Dummy constructor.
123 ~TargetPassConfig() override;
127 /// Get the right type of TargetMachine for this target.
128 template<typename TMC> TMC &getTM() const {
129 return *static_cast<TMC*>(TM);
133 void setInitialized() { Initialized = true; }
135 CodeGenOpt::Level getOptLevel() const;
137 /// Set the StartAfter, StartBefore and StopAfter passes to allow running only
138 /// a portion of the normal code-gen pass sequence.
140 /// If the StartAfter and StartBefore pass ID is zero, then compilation will
141 /// begin at the normal point; otherwise, clear the Started flag to indicate
142 /// that passes should not be added until the starting pass is seen. If the
143 /// Stop pass ID is zero, then compilation will continue to the end.
145 /// This function expects that at least one of the StartAfter or the
146 /// StartBefore pass IDs is null.
147 void setStartStopPasses(AnalysisID StartBefore, AnalysisID StartAfter,
148 AnalysisID StopBefore, AnalysisID StopAfter) {
149 assert(!(StartBefore && StartAfter) &&
150 "Start after and start before passes are given");
151 assert(!(StopBefore && StopAfter) &&
152 "Stop after and stop before passed are given");
153 this->StartBefore = StartBefore;
154 this->StartAfter = StartAfter;
155 this->StopBefore = StopBefore;
156 this->StopAfter = StopAfter;
157 Started = (StartAfter == nullptr) && (StartBefore == nullptr);
160 void setDisableVerify(bool Disable) { setOpt(DisableVerify, Disable); }
162 bool getEnableTailMerge() const { return EnableTailMerge; }
163 void setEnableTailMerge(bool Enable) { setOpt(EnableTailMerge, Enable); }
165 /// Allow the target to override a specific pass without overriding the pass
166 /// pipeline. When passes are added to the standard pipeline at the
167 /// point where StandardID is expected, add TargetID in its place.
168 void substitutePass(AnalysisID StandardID, IdentifyingPassPtr TargetID);
170 /// Insert InsertedPassID pass after TargetPassID pass.
171 void insertPass(AnalysisID TargetPassID, IdentifyingPassPtr InsertedPassID,
172 bool VerifyAfter = true, bool PrintAfter = true);
174 /// Allow the target to enable a specific standard pass by default.
175 void enablePass(AnalysisID PassID) { substitutePass(PassID, PassID); }
177 /// Allow the target to disable a specific standard pass by default.
178 void disablePass(AnalysisID PassID) {
179 substitutePass(PassID, IdentifyingPassPtr());
182 /// Return the pass substituted for StandardID by the target.
183 /// If no substitution exists, return StandardID.
184 IdentifyingPassPtr getPassSubstitution(AnalysisID StandardID) const;
186 /// Return true if the pass has been substituted by the target or
187 /// overridden on the command line.
188 bool isPassSubstitutedOrOverridden(AnalysisID ID) const;
190 /// Return true if the optimized regalloc pipeline is enabled.
191 bool getOptimizeRegAlloc() const;
193 /// Return true if shrink wrapping is enabled.
194 bool getEnableShrinkWrap() const;
196 /// Return true if the default global register allocator is in use and
197 /// has not be overriden on the command line with '-regalloc=...'
198 bool usingDefaultRegAlloc() const;
200 /// Add common target configurable passes that perform LLVM IR to IR
201 /// transforms following machine independent optimization.
202 virtual void addIRPasses();
204 /// Add passes to lower exception handling for the code generator.
205 void addPassesToHandleExceptions();
207 /// Add pass to prepare the LLVM IR for code generation. This should be done
208 /// before exception handling preparation passes.
209 virtual void addCodeGenPrepare();
211 /// Add common passes that perform LLVM IR to IR transforms in preparation for
212 /// instruction selection.
213 virtual void addISelPrepare();
215 /// addInstSelector - This method should install an instruction selector pass,
216 /// which converts from LLVM code to machine instructions.
217 virtual bool addInstSelector() {
221 /// This method should install an IR translator pass, which converts from
222 /// LLVM code to machine instructions with possibly generic opcodes.
223 virtual bool addIRTranslator() { return true; }
225 /// This method may be implemented by targets that want to run passes
226 /// immediately before legalization.
227 virtual void addPreLegalizeMachineIR() {}
229 /// This method should install a legalize pass, which converts the instruction
230 /// sequence into one that can be selected by the target.
231 virtual bool addLegalizeMachineIR() { return true; }
233 /// This method may be implemented by targets that want to run passes
234 /// immediately before the register bank selection.
235 virtual void addPreRegBankSelect() {}
237 /// This method should install a register bank selector pass, which
238 /// assigns register banks to virtual registers without a register
239 /// class or register banks.
240 virtual bool addRegBankSelect() { return true; }
242 /// This method may be implemented by targets that want to run passes
243 /// immediately before the (global) instruction selection.
244 virtual void addPreGlobalInstructionSelect() {}
246 /// This method should install a (global) instruction selector pass, which
247 /// converts possibly generic instructions to fully target-specific
248 /// instructions, thereby constraining all generic virtual registers to
249 /// register classes.
250 virtual bool addGlobalInstructionSelect() { return true; }
252 /// Add the complete, standard set of LLVM CodeGen passes.
253 /// Fully developed targets will not generally override this.
254 virtual void addMachinePasses();
256 /// Create an instance of ScheduleDAGInstrs to be run within the standard
257 /// MachineScheduler pass for this function and target at the current
258 /// optimization level.
260 /// This can also be used to plug a new MachineSchedStrategy into an instance
261 /// of the standard ScheduleDAGMI:
262 /// return new ScheduleDAGMI(C, make_unique<MyStrategy>(C), /*RemoveKillFlags=*/false)
264 /// Return NULL to select the default (generic) machine scheduler.
265 virtual ScheduleDAGInstrs *
266 createMachineScheduler(MachineSchedContext *C) const {
270 /// Similar to createMachineScheduler but used when postRA machine scheduling
272 virtual ScheduleDAGInstrs *
273 createPostMachineScheduler(MachineSchedContext *C) const {
277 /// printAndVerify - Add a pass to dump then verify the machine function, if
278 /// those steps are enabled.
280 void printAndVerify(const std::string &Banner);
282 /// Add a pass to print the machine function if printing is enabled.
283 void addPrintPass(const std::string &Banner);
285 /// Add a pass to perform basic verification of the machine function if
286 /// verification is enabled.
287 void addVerifyPass(const std::string &Banner);
289 /// Check whether or not GlobalISel should abort on error.
290 /// When this is disable, GlobalISel will fall back on SDISel instead of
292 virtual bool isGlobalISelAbortEnabled() const;
294 /// Check whether or not a diagnostic should be emitted when GlobalISel
295 /// uses the fallback path. In other words, it will emit a diagnostic
296 /// when GlobalISel failed and isGlobalISelAbortEnabled is false.
297 virtual bool reportDiagnosticWhenGlobalISelFallback() const;
300 // Helper to verify the analysis is really immutable.
301 void setOpt(bool &Opt, bool Val);
303 /// Methods with trivial inline returns are convenient points in the common
304 /// codegen pass pipeline where targets may insert passes. Methods with
305 /// out-of-line standard implementations are major CodeGen stages called by
306 /// addMachinePasses. Some targets may override major stages when inserting
307 /// passes is insufficient, but maintaining overriden stages is more work.
310 /// addPreISelPasses - This method should add any "last minute" LLVM->LLVM
311 /// passes (which are run just before instruction selector).
312 virtual bool addPreISel() {
316 /// addMachineSSAOptimization - Add standard passes that optimize machine
317 /// instructions in SSA form.
318 virtual void addMachineSSAOptimization();
320 /// Add passes that optimize instruction level parallelism for out-of-order
321 /// targets. These passes are run while the machine code is still in SSA
322 /// form, so they can use MachineTraceMetrics to control their heuristics.
324 /// All passes added here should preserve the MachineDominatorTree,
325 /// MachineLoopInfo, and MachineTraceMetrics analyses.
326 virtual bool addILPOpts() {
330 /// This method may be implemented by targets that want to run passes
331 /// immediately before register allocation.
332 virtual void addPreRegAlloc() { }
334 /// createTargetRegisterAllocator - Create the register allocator pass for
335 /// this target at the current optimization level.
336 virtual FunctionPass *createTargetRegisterAllocator(bool Optimized);
338 /// addFastRegAlloc - Add the minimum set of target-independent passes that
339 /// are required for fast register allocation.
340 virtual void addFastRegAlloc(FunctionPass *RegAllocPass);
342 /// addOptimizedRegAlloc - Add passes related to register allocation.
343 /// LLVMTargetMachine provides standard regalloc passes for most targets.
344 virtual void addOptimizedRegAlloc(FunctionPass *RegAllocPass);
346 /// addPreRewrite - Add passes to the optimized register allocation pipeline
347 /// after register allocation is complete, but before virtual registers are
348 /// rewritten to physical registers.
350 /// These passes must preserve VirtRegMap and LiveIntervals, and when running
351 /// after RABasic or RAGreedy, they should take advantage of LiveRegMatrix.
352 /// When these passes run, VirtRegMap contains legal physreg assignments for
353 /// all virtual registers.
354 virtual bool addPreRewrite() {
358 /// This method may be implemented by targets that want to run passes after
359 /// register allocation pass pipeline but before prolog-epilog insertion.
360 virtual void addPostRegAlloc() { }
362 /// Add passes that optimize machine instructions after register allocation.
363 virtual void addMachineLateOptimization();
365 /// This method may be implemented by targets that want to run passes after
366 /// prolog-epilog insertion and before the second instruction scheduling pass.
367 virtual void addPreSched2() { }
369 /// addGCPasses - Add late codegen passes that analyze code for garbage
370 /// collection. This should return true if GC info should be printed after
372 virtual bool addGCPasses();
374 /// Add standard basic block placement passes.
375 virtual void addBlockPlacement();
377 /// This pass may be implemented by targets that want to run passes
378 /// immediately before machine code is emitted.
379 virtual void addPreEmitPass() { }
381 /// Utilities for targets to add passes to the pass manager.
384 /// Add a CodeGen pass at this point in the pipeline after checking overrides.
385 /// Return the pass that was added, or zero if no pass was added.
386 /// @p printAfter if true and adding a machine function pass add an extra
387 /// machine printer pass afterwards
388 /// @p verifyAfter if true and adding a machine function pass add an extra
389 /// machine verification pass afterwards.
390 AnalysisID addPass(AnalysisID PassID, bool verifyAfter = true,
391 bool printAfter = true);
393 /// Add a pass to the PassManager if that pass is supposed to be run, as
394 /// determined by the StartAfter and StopAfter options. Takes ownership of the
396 /// @p printAfter if true and adding a machine function pass add an extra
397 /// machine printer pass afterwards
398 /// @p verifyAfter if true and adding a machine function pass add an extra
399 /// machine verification pass afterwards.
400 void addPass(Pass *P, bool verifyAfter = true, bool printAfter = true);
402 /// addMachinePasses helper to create the target-selected or overriden
404 FunctionPass *createRegAllocPass(bool Optimized);
407 } // end namespace llvm